import { Plugin } from "@opencode/plugin/tui" import { appendFileSync, mkdirSync, existsSync } from "fs" import { join } from "path" // 日志文件路径 const LOG_DIR = "C:\\Users\\ypshidifu\\.config\\opencode" const LOG_FILE = join(LOG_DIR, "oc-session-all.log") // 确保日志目录存在 if (!existsSync(LOG_DIR)) { mkdirSync(LOG_DIR, { recursive: true }) } function log(msg) { const timestamp = new Date().toISOString() const line = `[${timestamp}] ${msg}\n` try { appendFileSync(LOG_FILE, line) } catch (e) { // 忽略写入错误 } console.log(msg) } export default Plugin.define({ id: "oc-session-all", setup(context) { log("[oc-session-all] v2 Plugin loaded! Attempting command registration...") // ── 探测 context 结构 ────────────────────────────────────── const report = [] report.push("context keys: " + Object.keys(context).join(", ")) // 探测 context.command if (context.command) { const cmdKeys = Object.getOwnPropertyNames(context.command) report.push("command props: " + cmdKeys.join(", ")) const cmdMethods = cmdKeys.filter(k => typeof context.command[k] === "function") report.push("command methods: " + cmdMethods.join(", ")) // 尝试 command.register if (typeof context.command.register === "function") { try { const unregister = context.command.register(() => [ { title: "Show All Sessions", value: "sessions-all", slash: { name: "sessions-all", aliases: ["sa"] }, description: "Display all session records in a dialog", onSelect: async () => { log("[oc-session-all] /sessions-all triggered") context.ui.toast.show({ message: "Sessions-all command executed!", variant: "success", duration: 3000, }) }, }, ]) report.push("command.register: SUCCESS, unregister type: " + typeof unregister) log("[oc-session-all] command.register succeeded") } catch (e) { report.push("command.register ERROR: " + e.message) log("[oc-session-all] command.register ERROR: " + e.message) } } else { report.push("command.register: NOT A FUNCTION") log("[oc-session-all] command.register is not a function") } } else { report.push("context.command: undefined") log("[oc-session-all] context.command is undefined") } // 探测 context.keymap if (context.keymap) { const kmKeys = Object.getOwnPropertyNames(context.keymap) report.push("keymap props: " + kmKeys.join(", ")) const kmMethods = kmKeys.filter(k => typeof context.keymap[k] === "function") report.push("keymap methods: " + kmMethods.join(", ")) } // 探测 context.ui if (context.ui) { const uiKeys = Object.keys(context.ui) report.push("ui keys: " + uiKeys.join(", ")) } const msg = report.join(" | ") log("[oc-session-all] FULL PROBE: " + msg) try { context.ui.toast.show({ message: "Full probe: " + msg, variant: "info", duration: 20000, }) } catch (e) { log("[oc-session-all] toast.show ERROR: " + e.message) } }, })