@mingxy/cerebro 2.2.2 → 2.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +19 -14
  3. package/src/tui.tsx +102 -93
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mingxy/cerebro",
3
- "version": "2.2.2",
3
+ "version": "2.2.4",
4
4
  "description": "Cerebro persistent memory plugin for OpenCode — auto-recall, auto-capture, 9 memory tools with clustering, project-scoped memory isolation",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
package/src/index.ts CHANGED
@@ -59,16 +59,13 @@ const OmemPlugin: Plugin = async (input) => {
59
59
  // Normalize to git root: worktree=git root, directory=cwd.
60
60
  // project_path must be git root so parent/child dirs share memories.
61
61
  const { directory: _directory, worktree, client } = input;
62
- const directory = worktree || _directory;
63
- // Proxy: dynamically resolve client.tui on each access so toast works
64
- // even if client.tui isn't ready yet at plugin init time
65
- const tui = new Proxy({} as any, {
66
- get(_, prop) {
67
- const realTui = (client as any)?.tui;
68
- const val = realTui?.[prop];
69
- return typeof val === "function" ? val.bind(realTui) : val;
70
- },
71
- });
62
+ // opencode 在非git目录启动时给 worktree="/"(global project,见 opencode 源码
63
+ // database-migration.test.ts:448 `SELECT worktree FROM project WHERE id='global' '/'`)。
64
+ // 这种情况直接用 `/` 会让服务端 SQL `LIKE '/%'` 命中所有绝对路径记忆,污染注入。
65
+ // fallback cwd(_directory),让前缀匹配只命中 cwd 子树。
66
+ const isWorktreeValid = worktree && worktree !== "/" && worktree !== "." && worktree !== "";
67
+ const directory = isWorktreeValid ? worktree! : _directory;
68
+ const tui = (client as any)?.tui;
72
69
 
73
70
  // Load overrides from opencode.json plugin_config
74
71
  let overrides: Record<string, unknown> = {};
@@ -98,7 +95,7 @@ const OmemPlugin: Plugin = async (input) => {
98
95
  } else {
99
96
  statusMessage = `Unable to reach ${config.connection.apiUrl}`;
100
97
  }
101
- }
98
+ }
102
99
 
103
100
  const email = process.env.GIT_AUTHOR_EMAIL || process.env.USER || "unknown";
104
101
  const cwd = directory || process.cwd();
@@ -136,9 +133,17 @@ const OmemPlugin: Plugin = async (input) => {
136
133
  ? { variant: "success" as const, title: `🧠 Cerebro Connected · v${pluginVersion}`, message: `🌐 Open in browser http://localhost:${webPort}` }
137
134
  : { variant: "success" as const, title: `🧠 Cerebro Connected · v${pluginVersion}`, message: "No web server" };
138
135
 
139
- try {
140
- writeFileSync(join(tmpdir(), "cerebro_startup_toast.json"), JSON.stringify(startupToast));
141
- } catch {}
136
+ // Direct toast — same pattern as opencode-acp (client.tui.showToast, fire-and-forget, 5s delay)
137
+ setTimeout(() => {
138
+ (client as any)?.tui?.showToast({
139
+ body: {
140
+ title: startupToast.title,
141
+ message: startupToast.message,
142
+ variant: startupToast.variant,
143
+ duration: 7000,
144
+ },
145
+ });
146
+ }, 5000);
142
147
 
143
148
  // Auto-update check (fire-and-forget, non-blocking)
144
149
  checkAndUpdate(tui, pluginVersion).catch(() => {});
package/src/tui.tsx CHANGED
@@ -1,93 +1,102 @@
1
- // @ts-nocheck — TUI JSX is resolved at runtime by opencode (same as quota plugin)
2
- /** @jsxImportSource @opentui/solid */
3
- import type { TuiPlugin, TuiPluginApi, TuiPluginModule } from "@opencode-ai/plugin/tui";
4
- import { createEffect, createSignal, onCleanup } from "solid-js";
5
- import { readFileSync, unlinkSync } from "node:fs";
6
- import { join } from "node:path";
7
- import { tmpdir } from "node:os";
8
-
9
- const id = "@mingxy/cerebro";
10
- const SIDEBAR_ORDER = 160;
11
-
12
- function readAutoStoreFromFile(sessionId: string | undefined): boolean {
13
- if (!sessionId) return true;
14
- try {
15
- const filePath = join(tmpdir(), `cerebro_autostore_${sessionId}.json`);
16
- const data = JSON.parse(readFileSync(filePath, "utf-8"));
17
- return data.enabled ?? true;
18
- } catch {
19
- return true;
20
- }
21
- }
22
-
23
- function SidebarContentView(props: {
24
- api: TuiPluginApi;
25
- sessionID: string;
26
- }) {
27
- const [autoStore, setAutoStore] = createSignal(true);
28
- const theme = () => props.api.theme.current;
29
-
30
- const readAutoStore = () => readAutoStoreFromFile(props.sessionID);
31
-
32
- const unsubscribers = [
33
- props.api.event.on("session.updated", () => {
34
- setAutoStore(readAutoStore());
35
- }),
36
- props.api.event.on("tui.session.select", (event) => {
37
- if (event.properties?.sessionID === props.sessionID) {
38
- setAutoStore(readAutoStore());
39
- }
40
- }),
41
- ];
42
-
43
- createEffect(() => {
44
- props.sessionID;
45
- setAutoStore(readAutoStore());
46
- });
47
-
48
- const interval = setInterval(() => {
49
- setAutoStore(readAutoStore());
50
- }, 2000);
51
-
52
- onCleanup(() => {
53
- clearInterval(interval);
54
- for (const unsubscribe of unsubscribers) unsubscribe();
55
- });
56
-
57
- return (
58
- <box gap={0}>
59
- <text fg={theme()?.text} wrapMode="none">
60
- Cerebro
61
- </text>
62
- <box flexDirection="row" gap={1}>
63
- <text
64
- flexShrink={0}
65
- style={{ fg: autoStore() ? theme()?.success : theme()?.textMuted }}
66
- >
67
-
68
- </text>
69
- <text fg={theme()?.textMuted} wrapMode="none">
70
- {"Auto-store: " + (autoStore() ? "ON" : "OFF")}
71
- </text>
72
- </box>
73
- </box>
74
- );
75
- }
76
-
77
- const tui: TuiPlugin = async (api) => {
78
- try {
79
- const raw = readFileSync(join(tmpdir(), "cerebro_startup_toast.json"), "utf-8");
80
- const toast = JSON.parse(raw);
81
- setTimeout(() => {
82
- try { api.ui.toast(toast); } catch {}
83
- try { unlinkSync(join(tmpdir(), "cerebro_startup_toast.json")); } catch {}
84
- }, 2000);
85
- } catch {}
86
- };
87
-
88
- const pluginModule: TuiPluginModule & { id: string } = {
89
- id,
90
- tui,
91
- };
92
-
93
- export default pluginModule;
1
+ // @ts-nocheck — TUI JSX is resolved at runtime by opencode (same as quota plugin)
2
+ /** @jsxImportSource @opentui/solid */
3
+ import type { TuiPlugin, TuiPluginApi, TuiPluginModule } from "@opencode-ai/plugin/tui";
4
+ import { createEffect, createSignal, onCleanup } from "solid-js";
5
+ import { readFileSync, unlinkSync } from "node:fs";
6
+ import { join } from "node:path";
7
+ import { tmpdir } from "node:os";
8
+
9
+ const id = "@mingxy/cerebro";
10
+ const SIDEBAR_ORDER = 160;
11
+
12
+ function readAutoStoreFromFile(sessionId: string | undefined): boolean {
13
+ if (!sessionId) return true;
14
+ try {
15
+ const filePath = join(tmpdir(), `cerebro_autostore_${sessionId}.json`);
16
+ const data = JSON.parse(readFileSync(filePath, "utf-8"));
17
+ return data.enabled ?? true;
18
+ } catch {
19
+ return true;
20
+ }
21
+ }
22
+
23
+ function SidebarContentView(props: {
24
+ api: TuiPluginApi;
25
+ sessionID: string;
26
+ }) {
27
+ const [autoStore, setAutoStore] = createSignal(true);
28
+ const theme = () => props.api.theme.current;
29
+
30
+ const readAutoStore = () => readAutoStoreFromFile(props.sessionID);
31
+
32
+ const unsubscribers = [
33
+ props.api.event.on("session.updated", () => {
34
+ setAutoStore(readAutoStore());
35
+ }),
36
+ props.api.event.on("tui.session.select", (event) => {
37
+ if (event.properties?.sessionID === props.sessionID) {
38
+ setAutoStore(readAutoStore());
39
+ }
40
+ }),
41
+ ];
42
+
43
+ createEffect(() => {
44
+ props.sessionID;
45
+ setAutoStore(readAutoStore());
46
+ });
47
+
48
+ const interval = setInterval(() => {
49
+ setAutoStore(readAutoStore());
50
+ }, 2000);
51
+
52
+ onCleanup(() => {
53
+ clearInterval(interval);
54
+ for (const unsubscribe of unsubscribers) unsubscribe();
55
+ });
56
+
57
+ return (
58
+ <box gap={0}>
59
+ <text fg={theme()?.text} wrapMode="none">
60
+ Cerebro
61
+ </text>
62
+ <box flexDirection="row" gap={1}>
63
+ <text
64
+ flexShrink={0}
65
+ style={{ fg: autoStore() ? theme()?.success : theme()?.textMuted }}
66
+ >
67
+
68
+ </text>
69
+ <text fg={theme()?.textMuted} wrapMode="none">
70
+ {"Auto-store: " + (autoStore() ? "ON" : "OFF")}
71
+ </text>
72
+ </box>
73
+ </box>
74
+ );
75
+ }
76
+
77
+ const tui: TuiPlugin = async (api) => {
78
+ try {
79
+ const raw = readFileSync(join(tmpdir(), "cerebro_startup_toast.json"), "utf-8");
80
+ const toast = JSON.parse(raw);
81
+ // Retry: opencode 1.17.14+ delays TUI init; fixed timeout may fire before endpoint is ready
82
+ let attempts = 0;
83
+ const maxAttempts = 16;
84
+ const tryToast = () => {
85
+ attempts++;
86
+ try {
87
+ api.ui.toast(toast);
88
+ try { unlinkSync(join(tmpdir(), "cerebro_startup_toast.json")); } catch {}
89
+ } catch {
90
+ if (attempts < maxAttempts) setTimeout(tryToast, 500);
91
+ }
92
+ };
93
+ setTimeout(tryToast, 1000);
94
+ } catch {}
95
+ };
96
+
97
+ const pluginModule: TuiPluginModule & { id: string } = {
98
+ id,
99
+ tui,
100
+ };
101
+
102
+ export default pluginModule;