@mingxy/cerebro 2.2.0 → 2.2.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mingxy/cerebro",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
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/config.ts CHANGED
@@ -20,6 +20,9 @@ export interface CerebroPluginConfig {
20
20
  searchCount: number;
21
21
  recentTruncateChars: number;
22
22
  searchTruncateChars: number;
23
+ recentTimeoutMs: number;
24
+ searchTimeoutMs: number;
25
+ profileTimeoutMs: number;
23
26
  };
24
27
  ingest: {
25
28
  autoCaptureThreshold: number;
@@ -60,6 +63,9 @@ const DEFAULTS: CerebroPluginConfig = {
60
63
  searchCount: 10,
61
64
  recentTruncateChars: 0, // 0 = 不截断
62
65
  searchTruncateChars: 0, // 0 = 不截断
66
+ recentTimeoutMs: 3000,
67
+ searchTimeoutMs: 5000,
68
+ profileTimeoutMs: 2000,
63
69
  },
64
70
  ingest: {
65
71
  autoCaptureThreshold: 5,
package/src/hooks.ts CHANGED
@@ -255,20 +255,23 @@ export async function buildMemoryInjection(
255
255
  const searchCount = ic.searchCount || DEFAULTS.injection.searchCount;
256
256
  const recentTruncate = ic.recentTruncateChars || 0; // 0 = 不截断
257
257
  const searchTruncate = ic.searchTruncateChars || 0; // 0 = 不截断
258
+ const profileTimeout = ic.profileTimeoutMs || DEFAULTS.injection.profileTimeoutMs;
259
+ const recentTimeout = ic.recentTimeoutMs || DEFAULTS.injection.recentTimeoutMs;
260
+ const searchTimeout = ic.searchTimeoutMs || DEFAULTS.injection.searchTimeoutMs;
258
261
 
259
262
  const [profile, projectMemories, searchResults] = await Promise.all([
260
263
  Promise.race([
261
264
  client.getInjection(),
262
- new Promise<null>((resolve) => setTimeout(() => resolve(null), 1000)),
265
+ new Promise<null>((resolve) => setTimeout(() => resolve(null), profileTimeout)),
263
266
  ]).catch(() => null),
264
267
  Promise.race([
265
268
  client.listRecent(recentCount, projectPath),
266
- new Promise<never[]>((resolve) => setTimeout(() => resolve([]), 1000)),
269
+ new Promise<never[]>((resolve) => setTimeout(() => resolve([]), recentTimeout)),
267
270
  ]).catch(() => []),
268
271
  query
269
272
  ? Promise.race([
270
273
  client.searchMemories(query, searchCount, undefined, undefined, projectPath),
271
- new Promise<never[]>((resolve) => setTimeout(() => resolve([]), 1500)),
274
+ new Promise<never[]>((resolve) => setTimeout(() => resolve([]), searchTimeout)),
272
275
  ]).catch(() => [])
273
276
  : Promise.resolve([]),
274
277
  ]);
package/src/index.ts CHANGED
@@ -59,13 +59,16 @@ 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
- // 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;
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
+ });
69
72
 
70
73
  // Load overrides from opencode.json plugin_config
71
74
  let overrides: Record<string, unknown> = {};
@@ -95,7 +98,7 @@ const OmemPlugin: Plugin = async (input) => {
95
98
  } else {
96
99
  statusMessage = `Unable to reach ${config.connection.apiUrl}`;
97
100
  }
98
- }
101
+ }
99
102
 
100
103
  const email = process.env.GIT_AUTHOR_EMAIL || process.env.USER || "unknown";
101
104
  const cwd = directory || process.cwd();
@@ -133,17 +136,9 @@ const OmemPlugin: Plugin = async (input) => {
133
136
  ? { variant: "success" as const, title: `🧠 Cerebro Connected · v${pluginVersion}`, message: `🌐 Open in browser http://localhost:${webPort}` }
134
137
  : { variant: "success" as const, title: `🧠 Cerebro Connected · v${pluginVersion}`, message: "No web server" };
135
138
 
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);
139
+ try {
140
+ writeFileSync(join(tmpdir(), "cerebro_startup_toast.json"), JSON.stringify(startupToast));
141
+ } catch {}
147
142
 
148
143
  // Auto-update check (fire-and-forget, non-blocking)
149
144
  checkAndUpdate(tui, pluginVersion).catch(() => {});
package/src/tui.tsx CHANGED
@@ -1,102 +1,93 @@
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;
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;