Files
oc_session/tui-plugins/oc-session-all.js
T

145 lines
4.9 KiB
JavaScript

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)
}
// 延时函数
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
export default Plugin.define({
id: "oc-session-all",
async setup(context) {
log("[oc-session-all] v3 Plugin loaded! Trying to register command via ui.slot...")
// 先写入测试日志,确保 fs 可用
log("[oc-session-all] FS test: log file created successfully")
// 延时 3 秒,避免 toast 被覆盖
await delay(3000)
// ── 探测 context.ui.slot ──────────────────────────────────────
const report = []
if (context.ui && context.ui.slot) {
report.push("ui.slot exists")
log("[oc-session-all] ui.slot type: " + typeof context.ui.slot)
// 尝试列出 slot 的所有方法
const slotMethods = Object.getOwnPropertyNames(context.ui.slot)
report.push("slot methods: " + slotMethods.join(", "))
log("[oc-session-all] slot methods: " + slotMethods.join(", "))
// 尝试调用 slot 的各种方法
for (const method of slotMethods) {
if (typeof context.ui.slot[method] === "function") {
log("[oc-session-all] slot." + method + " is a function")
}
}
} else {
report.push("ui.slot NOT found")
}
// ── 尝试通过 ui.slot 注册组件 ──────────────────────────────────────
try {
// 创建一个简单的组件,尝试在渲染时注册命令
const testComponent = () => {
log("[oc-session-all] Test component rendered!")
return null
}
// 尝试不同的注册方法
if (context.ui.slot && typeof context.ui.slot.register === "function") {
context.ui.slot.register(testComponent)
report.push("registered via slot.register()")
log("[oc-session-all] Registered component via slot.register()")
} else if (context.ui.slot && typeof context.ui.slot.add === "function") {
context.ui.slot.add(testComponent)
report.push("registered via slot.add()")
log("[oc-session-all] Registered component via slot.add()")
} else if (context.ui.slot && typeof context.ui.slot.append === "function") {
context.ui.slot.append(testComponent)
report.push("registered via slot.append()")
log("[oc-session-all] Registered component via slot.append()")
} else {
report.push("no known slot registration method")
log("[oc-session-all] No known slot registration method found")
}
} catch (e) {
report.push("slot registration ERROR: " + e.message)
log("[oc-session-all] slot registration ERROR: " + e.message)
}
// ── 尝试直接使用 keymap.layer(可能需要等待) ──────────────────────────────────────
try {
// 等待更长时间,让 Provider 可用
await delay(2000)
if (context.keymap && typeof context.keymap.layer === "function") {
log("[oc-session-all] Attempting keymap.layer call...")
try {
// 尝试注册一个测试命令
const layer = context.keymap.layer({
commands: [
{
id: "sessions-all",
title: "Show all sessions",
group: "Session",
}
],
bindings: [
{
key: "ctrl+o",
command: "sessions-all",
}
]
})
report.push("keymap.layer() SUCCESS")
log("[oc-session-all] keymap.layer() SUCCESS")
} catch (e) {
report.push("keymap.layer() ERROR: " + e.message)
log("[oc-session-all] keymap.layer() ERROR: " + e.message)
}
}
} catch (e) {
report.push("layer attempt ERROR: " + e.message)
log("[oc-session-all] layer attempt ERROR: " + e.message)
}
// 最终报告
const finalReport = report.join(" | ")
log("[oc-session-all] SLOT PROBE: " + finalReport)
// 延时后显示 toast
await delay(2000)
try {
context.ui.toast.show({
message: "Slot probe: " + finalReport,
variant: "info",
duration: 25000,
})
} catch (e) {
log("[oc-session-all] toast.show ERROR: " + e.message)
}
},
})