@mingxy/cerebro 1.4.0

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.
Binary file
@@ -0,0 +1,27 @@
1
+ {
2
+ // omem server connection
3
+ "apiUrl": "https://www.mengxy.cc",
4
+ "apiKey": "your-tenant-id",
5
+
6
+ // request timeout in milliseconds
7
+ "requestTimeoutMs": 15000,
8
+
9
+ // content limits
10
+ "maxQueryLength": 200,
11
+ "maxContentChars": 30000,
12
+ "maxContentLength": 500,
13
+
14
+ // auto capture settings
15
+ "autoCaptureThreshold": 5,
16
+ "ingestMode": "smart",
17
+
18
+ // recall tuning
19
+ "similarityThreshold": 0.4,
20
+ "maxRecallResults": 10,
21
+
22
+ // UI settings
23
+ "toastDelayMs": 7000,
24
+
25
+ // container tag prefix (used for user/project scoping)
26
+ "containerTagPrefix": "omem"
27
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@mingxy/cerebro",
3
+ "version": "1.4.0",
4
+ "description": "Cerebro persistent memory plugin for OpenCode — auto-recall, auto-capture, 9 memory tools with clustering",
5
+ "type": "module",
6
+ "main": "src/index.ts",
7
+ "oc-plugin": [
8
+ "server"
9
+ ],
10
+ "keywords": [
11
+ "opencode",
12
+ "memory",
13
+ "ai-agent",
14
+ "persistent-memory",
15
+ "cerebro",
16
+ "clustering"
17
+ ],
18
+ "author": "mingxy-cerebro",
19
+ "license": "Apache-2.0",
20
+ "homepage": "https://github.com/mingxy-cerebro/cerebro-server",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/mingxy-cerebro/cerebro-server.git",
24
+ "directory": "plugins/opencode"
25
+ },
26
+ "dependencies": {
27
+ "@opencode-ai/plugin": "^1.0.162"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^25.5.0",
31
+ "typescript": "^5.7.0"
32
+ }
33
+ }
package/src/client.ts ADDED
@@ -0,0 +1,359 @@
1
+ import { logWarn, logError } from "./logger.js";
2
+ import type { OmemPluginConfig } from "./config.js";
3
+
4
+ function sanitizeContent(text: string, maxLen: number): string {
5
+ let clean = text.replace(/<[\w-]+[^>]*>[\s\S]*?<\/[\w-]+>/g, "");
6
+ clean = clean.replace(/<[\w-]+[^>]*\/>/g, "");
7
+ clean = clean.replace(/\s+/g, " ").trim();
8
+ if (clean.length <= maxLen) return clean;
9
+ return clean.slice(0, maxLen) + "…[truncated]";
10
+ }
11
+
12
+ function truncateQuery(query: string, maxLen: number): string {
13
+ if (query.length <= maxLen) return query;
14
+ return query.slice(0, maxLen);
15
+ }
16
+
17
+ export interface IngestOptions {
18
+ mode?: "smart" | "raw";
19
+ agentId?: string;
20
+ sessionId?: string;
21
+ entityContext?: string;
22
+ tags?: string[];
23
+ }
24
+
25
+ export interface SearchResult {
26
+ memory: MemoryDto;
27
+ score: number;
28
+ }
29
+
30
+ export interface SearchResponse {
31
+ results: SearchResult[];
32
+ trace?: unknown;
33
+ }
34
+
35
+ export interface ListResponse {
36
+ memories: MemoryDto[];
37
+ limit: number;
38
+ offset: number;
39
+ }
40
+
41
+ export interface ClusterSummary {
42
+ cluster_id: string;
43
+ title: string;
44
+ summary: string;
45
+ member_count: number;
46
+ relevance_score: number;
47
+ key_memories: MemoryDto[];
48
+ }
49
+
50
+ export interface ClusteredRecallResult {
51
+ cluster_summaries: ClusterSummary[];
52
+ standalone_memories: MemoryDto[];
53
+ }
54
+
55
+ export interface ShouldRecallResponse {
56
+ should_recall: boolean;
57
+ query?: string;
58
+ reason?: string;
59
+ similarity_score?: number;
60
+ confidence?: number;
61
+ memories?: SearchResult[];
62
+ clustered?: ClusteredRecallResult;
63
+ }
64
+
65
+ export interface SessionRecallRecord {
66
+ session_id: string;
67
+ memory_ids: string[];
68
+ recall_type: string;
69
+ created_at: string;
70
+ }
71
+
72
+ export interface SessionRecallListResponse {
73
+ recalls: SessionRecallRecord[];
74
+ }
75
+
76
+ export interface MemoryDto {
77
+ id: string;
78
+ content: string;
79
+ l2_content?: string;
80
+ category: string;
81
+ memory_type: string;
82
+ state: string;
83
+ tags: string[];
84
+ source?: string;
85
+ tenant_id: string;
86
+ agent_id?: string;
87
+ created_at: string;
88
+ updated_at: string;
89
+ }
90
+
91
+ export class OmemClient {
92
+ constructor(
93
+ private baseUrl: string,
94
+ private apiKey: string,
95
+ private config?: Partial<OmemPluginConfig>,
96
+ ) {
97
+ this.baseUrl = baseUrl.replace(/\/+$/, "");
98
+ }
99
+
100
+ private getCfg<K extends keyof OmemPluginConfig>(key: K, fallback: OmemPluginConfig[K]): OmemPluginConfig[K] {
101
+ return this.config?.[key] ?? fallback;
102
+ }
103
+
104
+ private async request<T>(
105
+ path: string,
106
+ init: RequestInit = {},
107
+ timeoutMs?: number,
108
+ ): Promise<T | null> {
109
+ const url = `${this.baseUrl}${path}`;
110
+ const controller = new AbortController();
111
+ const timeout = setTimeout(
112
+ () => controller.abort(),
113
+ timeoutMs ?? this.getCfg("requestTimeoutMs", 15000),
114
+ );
115
+
116
+ try {
117
+ const res = await fetch(url, {
118
+ ...init,
119
+ signal: controller.signal,
120
+ headers: {
121
+ "Content-Type": "application/json",
122
+ "X-API-Key": this.apiKey,
123
+ ...(init.headers as Record<string, string>),
124
+ },
125
+ });
126
+
127
+ if (!res.ok) {
128
+ const errorBody = await res.text().catch(() => "");
129
+ logWarn(`${init.method ?? "GET"} ${path} → ${res.status} ${res.statusText}: ${errorBody}`);
130
+ throw new Error(`[omem] ${res.status} ${res.statusText}${errorBody ? ": " + errorBody : ""}`);
131
+ }
132
+
133
+ if (res.status === 204) return null;
134
+
135
+ return (await res.json()) as T;
136
+ } catch (err) {
137
+ if ((err as Error).name === "AbortError") {
138
+ logWarn(`${init.method ?? "GET"} ${path} timed out`);
139
+ throw new Error(`[omem] Request timed out (${timeoutMs ?? this.getCfg("requestTimeoutMs", 15000)}ms)`);
140
+ } else {
141
+ logError(`${init.method ?? "GET"} ${path} failed:`, err);
142
+ throw err;
143
+ }
144
+ } finally {
145
+ clearTimeout(timeout);
146
+ }
147
+ }
148
+
149
+ private post<T>(path: string, body: unknown, timeoutMs?: number): Promise<T | null> {
150
+ return this.request<T>(path, {
151
+ method: "POST",
152
+ body: JSON.stringify(body),
153
+ }, timeoutMs);
154
+ }
155
+
156
+ private put<T>(path: string, body: unknown): Promise<T | null> {
157
+ return this.request<T>(path, {
158
+ method: "PUT",
159
+ body: JSON.stringify(body),
160
+ });
161
+ }
162
+
163
+ private del<T>(path: string): Promise<T | null> {
164
+ return this.request<T>(path, { method: "DELETE" });
165
+ }
166
+
167
+ async createMemory(
168
+ content: string,
169
+ tags?: string[],
170
+ source?: string,
171
+ scope?: string,
172
+ agentId?: string,
173
+ sessionId?: string,
174
+ ): Promise<MemoryDto | null> {
175
+ const safeContent = sanitizeContent(content, this.getCfg("maxContentChars", 30000));
176
+ return this.post<MemoryDto>("/v1/memories", {
177
+ content: safeContent,
178
+ tags,
179
+ source,
180
+ scope,
181
+ agent_id: agentId,
182
+ session_id: sessionId,
183
+ });
184
+ }
185
+
186
+ async searchMemories(
187
+ query: string,
188
+ limit = 10,
189
+ scope?: string,
190
+ tags?: string[],
191
+ ): Promise<SearchResult[]> {
192
+ const safeQ = truncateQuery(query, this.getCfg("maxQueryLength", 200));
193
+ const params = new URLSearchParams({ q: safeQ, limit: String(limit) });
194
+ if (scope) params.set("scope", scope);
195
+ if (tags && tags.length > 0) params.set("tags", tags.join(","));
196
+ const res = await this.request<SearchResponse>(
197
+ `/v1/memories/search?${params}`,
198
+ {},
199
+ 20_000,
200
+ );
201
+ return res?.results ?? [];
202
+ }
203
+
204
+ async getMemory(id: string): Promise<MemoryDto | null> {
205
+ return this.request<MemoryDto>(`/v1/memories/${encodeURIComponent(id)}`);
206
+ }
207
+
208
+ async updateMemory(
209
+ id: string,
210
+ content: string,
211
+ tags?: string[],
212
+ ): Promise<MemoryDto | null> {
213
+ return this.put<MemoryDto>(
214
+ `/v1/memories/${encodeURIComponent(id)}`,
215
+ { content, tags },
216
+ );
217
+ }
218
+
219
+ async deleteMemory(id: string): Promise<void> {
220
+ await this.del(`/v1/memories/${encodeURIComponent(id)}`);
221
+ }
222
+
223
+ async ingestMessages(
224
+ messages: Array<{ role: string; content: string }>,
225
+ opts: IngestOptions = {},
226
+ ): Promise<unknown> {
227
+ const safeMessages = messages.map(m => ({
228
+ role: m.role,
229
+ content: sanitizeContent(m.content, this.getCfg("maxContentChars", 30000)),
230
+ }));
231
+ return this.post("/v1/memories", {
232
+ messages: safeMessages,
233
+ mode: opts.mode ?? "smart",
234
+ agent_id: opts.agentId,
235
+ session_id: opts.sessionId,
236
+ entity_context: opts.entityContext,
237
+ tags: opts.tags,
238
+ });
239
+ }
240
+
241
+ async getProfile(_query?: string): Promise<unknown> {
242
+ return this.request("/v1/profile");
243
+ }
244
+
245
+ async getStats(): Promise<unknown> {
246
+ return this.request("/v1/stats");
247
+ }
248
+
249
+ async listRecent(limit = 20): Promise<MemoryDto[]> {
250
+ const res = await this.request<ListResponse>(
251
+ `/v1/memories?limit=${limit}&offset=0`,
252
+ );
253
+ return res?.memories ?? [];
254
+ }
255
+
256
+ async createSpace(
257
+ name: string,
258
+ spaceType: string,
259
+ members?: Array<{ user_id: string; role: string }>,
260
+ ): Promise<unknown> {
261
+ return this.post("/v1/spaces", { name, space_type: spaceType, members });
262
+ }
263
+
264
+ async listSpaces(): Promise<unknown[]> {
265
+ const res = await this.request<{ spaces: unknown[] }>("/v1/spaces");
266
+ return res?.spaces ?? [];
267
+ }
268
+
269
+ async addSpaceMember(
270
+ spaceId: string,
271
+ userId: string,
272
+ role: string,
273
+ ): Promise<unknown> {
274
+ return this.post(
275
+ `/v1/spaces/${encodeURIComponent(spaceId)}/members`,
276
+ { user_id: userId, role },
277
+ );
278
+ }
279
+
280
+ async shareMemory(
281
+ memoryId: string,
282
+ targetSpace: string,
283
+ ): Promise<unknown> {
284
+ return this.post(
285
+ `/v1/memories/${encodeURIComponent(memoryId)}/share`,
286
+ { target_space: targetSpace },
287
+ );
288
+ }
289
+
290
+ async pullMemory(
291
+ memoryId: string,
292
+ sourceSpace: string,
293
+ visibility?: string,
294
+ ): Promise<unknown> {
295
+ return this.post(
296
+ `/v1/memories/${encodeURIComponent(memoryId)}/pull`,
297
+ { source_space: sourceSpace, visibility },
298
+ );
299
+ }
300
+
301
+ async reshareMemory(
302
+ memoryId: string,
303
+ targetSpace?: string,
304
+ ): Promise<unknown> {
305
+ return this.post(
306
+ `/v1/memories/${encodeURIComponent(memoryId)}/reshare`,
307
+ { target_space: targetSpace },
308
+ );
309
+ }
310
+
311
+ async shouldRecall(
312
+ query_text: string,
313
+ last_query_text: string | undefined,
314
+ session_id: string,
315
+ similarity_threshold?: number,
316
+ max_results?: number,
317
+ project_tags?: string[],
318
+ ): Promise<ShouldRecallResponse | null> {
319
+ const res = await this.post<ShouldRecallResponse>("/v1/should-recall", {
320
+ query_text,
321
+ last_query_text,
322
+ session_id,
323
+ similarity_threshold,
324
+ max_results,
325
+ project_tags,
326
+ }, 20_000);
327
+ return res;
328
+ }
329
+
330
+ async recordSessionRecall(
331
+ session_id: string,
332
+ memory_ids: string[],
333
+ recall_type: string,
334
+ query_text?: string,
335
+ similarity_score?: number,
336
+ llm_confidence?: number,
337
+ ): Promise<unknown | null> {
338
+ const body = {
339
+ session_id,
340
+ memory_ids,
341
+ recall_type,
342
+ query_text: query_text ?? "",
343
+ similarity_score: similarity_score ?? 0,
344
+ llm_confidence: llm_confidence ?? 0,
345
+ };
346
+ const res = await this.post("/v1/session-recalls", body, 20_000);
347
+ return res;
348
+ }
349
+
350
+ async listSessionRecalls(
351
+ session_id: string,
352
+ ): Promise<SessionRecallRecord[]> {
353
+ const params = new URLSearchParams({ session_id });
354
+ const res = await this.request<SessionRecallListResponse>(
355
+ `/v1/session-recalls?${params}`,
356
+ );
357
+ return res?.recalls ?? [];
358
+ }
359
+ }
package/src/config.ts ADDED
@@ -0,0 +1,89 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { join } from "node:path";
4
+
5
+ export interface OmemPluginConfig {
6
+ // Connection
7
+ apiUrl: string;
8
+ apiKey: string;
9
+ // Timeouts (milliseconds)
10
+ requestTimeoutMs: number;
11
+ // Content limits
12
+ maxQueryLength: number;
13
+ maxContentChars: number;
14
+ maxContentLength: number;
15
+ // Auto capture
16
+ autoCaptureThreshold: number;
17
+ ingestMode: "smart" | "raw";
18
+ // Recall settings
19
+ similarityThreshold: number;
20
+ maxRecallResults: number;
21
+ // UI settings
22
+ toastDelayMs: number;
23
+ }
24
+
25
+ const DEFAULTS: OmemPluginConfig = {
26
+ apiUrl: "https://www.mengxy.cc",
27
+ apiKey: "",
28
+ requestTimeoutMs: 15000,
29
+ maxQueryLength: 200,
30
+ maxContentChars: 30000,
31
+ maxContentLength: 500,
32
+ autoCaptureThreshold: 5,
33
+ ingestMode: "smart",
34
+ similarityThreshold: 0.4,
35
+ maxRecallResults: 10,
36
+ toastDelayMs: 7000,
37
+ };
38
+
39
+ export function loadPluginConfig(overrides?: Partial<OmemPluginConfig>): OmemPluginConfig {
40
+ const config: Partial<OmemPluginConfig> = { ...DEFAULTS };
41
+
42
+ // Try loading from config file
43
+ try {
44
+ const cfgPath = join(homedir(), ".config", "ourmem", "config.json");
45
+ const cfg = JSON.parse(readFileSync(cfgPath, "utf-8"));
46
+
47
+ if (cfg.apiUrl) config.apiUrl = cfg.apiUrl;
48
+ if (cfg.apiKey) config.apiKey = cfg.apiKey;
49
+ if (typeof cfg.requestTimeoutMs === "number") config.requestTimeoutMs = cfg.requestTimeoutMs;
50
+ if (typeof cfg.maxQueryLength === "number") config.maxQueryLength = cfg.maxQueryLength;
51
+ if (typeof cfg.maxContentChars === "number") config.maxContentChars = cfg.maxContentChars;
52
+ if (typeof cfg.maxContentLength === "number") config.maxContentLength = cfg.maxContentLength;
53
+ if (typeof cfg.autoCaptureThreshold === "number") config.autoCaptureThreshold = cfg.autoCaptureThreshold;
54
+ if (cfg.ingestMode === "raw" || cfg.ingestMode === "smart") config.ingestMode = cfg.ingestMode;
55
+ if (typeof cfg.similarityThreshold === "number") config.similarityThreshold = cfg.similarityThreshold;
56
+ if (typeof cfg.maxRecallResults === "number") config.maxRecallResults = cfg.maxRecallResults;
57
+ if (typeof cfg.toastDelayMs === "number") config.toastDelayMs = cfg.toastDelayMs;
58
+ } catch {
59
+ // Config file doesn't exist or is invalid, use defaults
60
+ }
61
+
62
+ // Apply environment variable overrides
63
+ if (process.env.OMEM_API_URL) config.apiUrl = process.env.OMEM_API_URL;
64
+ if (process.env.OMEM_API_KEY) config.apiKey = process.env.OMEM_API_KEY;
65
+ if (process.env.OMEM_REQUEST_TIMEOUT_MS) {
66
+ config.requestTimeoutMs = parseInt(process.env.OMEM_REQUEST_TIMEOUT_MS, 10) || DEFAULTS.requestTimeoutMs;
67
+ }
68
+ if (process.env.OMEM_AUTO_CAPTURE_THRESHOLD) {
69
+ config.autoCaptureThreshold = parseInt(process.env.OMEM_AUTO_CAPTURE_THRESHOLD, 10) || DEFAULTS.autoCaptureThreshold;
70
+ }
71
+ if (process.env.OMEM_INGEST_MODE === "raw" || process.env.OMEM_INGEST_MODE === "smart") {
72
+ config.ingestMode = process.env.OMEM_INGEST_MODE;
73
+ }
74
+ if (process.env.OMEM_SIMILARITY_THRESHOLD) {
75
+ config.similarityThreshold = parseFloat(process.env.OMEM_SIMILARITY_THRESHOLD) || DEFAULTS.similarityThreshold;
76
+ }
77
+ if (process.env.OMEM_MAX_RECALL_RESULTS) {
78
+ config.maxRecallResults = parseInt(process.env.OMEM_MAX_RECALL_RESULTS, 10) || DEFAULTS.maxRecallResults;
79
+ }
80
+
81
+ // Apply explicit overrides (from opencode.json)
82
+ if (overrides) {
83
+ Object.assign(config, overrides);
84
+ }
85
+
86
+ return config as OmemPluginConfig;
87
+ }
88
+
89
+ export { DEFAULTS };