diff --git a/index.ts b/index.ts index 8c301ca..6785b24 100644 --- a/index.ts +++ b/index.ts @@ -1,17 +1,22 @@ import fs from "fs" +import os from "os" import { MatrixBotClient } from "./matrix-client.js" import { SessionManager } from "./session-manager.js" import { loadConfig } from "./config-loader.js" import { log } from "./logger.js" +const PLUGIN_DATA_DIR = fs.mkdtempSync(`${os.homedir()}/.opencode-matrix-plugin-`) +const LOG_FILE = `${PLUGIN_DATA_DIR}/plugin.log` +const SETUP_STARTED_LOG = `${PLUGIN_DATA_DIR}/setup-started.log` +const SETUP_ERROR_LOG = `${PLUGIN_DATA_DIR}/setup-error.log` +const LOCK_FILE = `${PLUGIN_DATA_DIR}/lock` + log("Plugin module loaded") // ============================================================================= // Global mutex — prevents double startup (server + client both load plugins) // ============================================================================= -const LOCK_FILE = "/tmp/opencode-matrix-plugin.lock" - function tryAcquireLock(): boolean { try { // Check if an existing lock is stale (process no longer running) @@ -63,7 +68,7 @@ async function runPlugin(ctx: any, options: any) { try { const directory = ctx?.location?.directory || process.cwd() - fs.appendFileSync("/tmp/opencode-matrix-plugin/setup-started.log", `[${new Date().toISOString()}] setup() called, dir=${directory}\n`) + fs.appendFileSync(SETUP_STARTED_LOG, `[${new Date().toISOString()}] setup() called, dir=${directory}\n`) log(`Plugin setup started, directory=${directory}`) if (!options.homeserver) { @@ -265,7 +270,7 @@ async function runPlugin(ctx: any, options: any) { return cleanup } catch (err) { - fs.appendFileSync("/tmp/opencode-matrix-plugin/setup-error.log", `[${new Date().toISOString()}] ${String(err)}\n`) + fs.appendFileSync(SETUP_ERROR_LOG, `[${new Date().toISOString()}] ${String(err)}\n`) log(`Setup error: ${String(err)}`) releaseLock() return () => {} @@ -281,7 +286,7 @@ async function v2Setup(ctx: any) { const { options } = await loadConfig(ctx.options) return await runPlugin(ctx, options) } catch (err) { - fs.appendFileSync("/tmp/opencode-matrix-plugin/setup-error.log", `[${new Date().toISOString()}] ${String(err)}\n`) + fs.appendFileSync(SETUP_ERROR_LOG, `[${new Date().toISOString()}] ${String(err)}\n`) log(`V2 setup error: ${String(err)}`) return () => {} }