@memtensor/memos-local-openclaw-plugin 0.1.3 → 0.1.5

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 (117) hide show
  1. package/.env.example +13 -5
  2. package/README.md +283 -91
  3. package/dist/capture/index.d.ts +5 -7
  4. package/dist/capture/index.d.ts.map +1 -1
  5. package/dist/capture/index.js +72 -43
  6. package/dist/capture/index.js.map +1 -1
  7. package/dist/ingest/dedup.d.ts +8 -0
  8. package/dist/ingest/dedup.d.ts.map +1 -1
  9. package/dist/ingest/dedup.js +21 -0
  10. package/dist/ingest/dedup.js.map +1 -1
  11. package/dist/ingest/providers/anthropic.d.ts +16 -0
  12. package/dist/ingest/providers/anthropic.d.ts.map +1 -1
  13. package/dist/ingest/providers/anthropic.js +214 -1
  14. package/dist/ingest/providers/anthropic.js.map +1 -1
  15. package/dist/ingest/providers/bedrock.d.ts +16 -5
  16. package/dist/ingest/providers/bedrock.d.ts.map +1 -1
  17. package/dist/ingest/providers/bedrock.js +210 -6
  18. package/dist/ingest/providers/bedrock.js.map +1 -1
  19. package/dist/ingest/providers/gemini.d.ts +16 -0
  20. package/dist/ingest/providers/gemini.d.ts.map +1 -1
  21. package/dist/ingest/providers/gemini.js +202 -1
  22. package/dist/ingest/providers/gemini.js.map +1 -1
  23. package/dist/ingest/providers/index.d.ts +31 -0
  24. package/dist/ingest/providers/index.d.ts.map +1 -1
  25. package/dist/ingest/providers/index.js +134 -4
  26. package/dist/ingest/providers/index.js.map +1 -1
  27. package/dist/ingest/providers/openai.d.ts +24 -0
  28. package/dist/ingest/providers/openai.d.ts.map +1 -1
  29. package/dist/ingest/providers/openai.js +255 -1
  30. package/dist/ingest/providers/openai.js.map +1 -1
  31. package/dist/ingest/task-processor.d.ts +65 -0
  32. package/dist/ingest/task-processor.d.ts.map +1 -0
  33. package/dist/ingest/task-processor.js +354 -0
  34. package/dist/ingest/task-processor.js.map +1 -0
  35. package/dist/ingest/worker.d.ts +3 -1
  36. package/dist/ingest/worker.d.ts.map +1 -1
  37. package/dist/ingest/worker.js +131 -23
  38. package/dist/ingest/worker.js.map +1 -1
  39. package/dist/recall/engine.d.ts +1 -0
  40. package/dist/recall/engine.d.ts.map +1 -1
  41. package/dist/recall/engine.js +22 -11
  42. package/dist/recall/engine.js.map +1 -1
  43. package/dist/recall/mmr.d.ts.map +1 -1
  44. package/dist/recall/mmr.js +3 -1
  45. package/dist/recall/mmr.js.map +1 -1
  46. package/dist/skill/bundled-memory-guide.d.ts +6 -0
  47. package/dist/skill/bundled-memory-guide.d.ts.map +1 -0
  48. package/dist/skill/bundled-memory-guide.js +95 -0
  49. package/dist/skill/bundled-memory-guide.js.map +1 -0
  50. package/dist/skill/evaluator.d.ts +31 -0
  51. package/dist/skill/evaluator.d.ts.map +1 -0
  52. package/dist/skill/evaluator.js +194 -0
  53. package/dist/skill/evaluator.js.map +1 -0
  54. package/dist/skill/evolver.d.ts +22 -0
  55. package/dist/skill/evolver.d.ts.map +1 -0
  56. package/dist/skill/evolver.js +193 -0
  57. package/dist/skill/evolver.js.map +1 -0
  58. package/dist/skill/generator.d.ts +25 -0
  59. package/dist/skill/generator.d.ts.map +1 -0
  60. package/dist/skill/generator.js +477 -0
  61. package/dist/skill/generator.js.map +1 -0
  62. package/dist/skill/installer.d.ts +16 -0
  63. package/dist/skill/installer.d.ts.map +1 -0
  64. package/dist/skill/installer.js +89 -0
  65. package/dist/skill/installer.js.map +1 -0
  66. package/dist/skill/upgrader.d.ts +19 -0
  67. package/dist/skill/upgrader.d.ts.map +1 -0
  68. package/dist/skill/upgrader.js +263 -0
  69. package/dist/skill/upgrader.js.map +1 -0
  70. package/dist/skill/validator.d.ts +29 -0
  71. package/dist/skill/validator.d.ts.map +1 -0
  72. package/dist/skill/validator.js +227 -0
  73. package/dist/skill/validator.js.map +1 -0
  74. package/dist/storage/sqlite.d.ts +141 -1
  75. package/dist/storage/sqlite.d.ts.map +1 -1
  76. package/dist/storage/sqlite.js +664 -7
  77. package/dist/storage/sqlite.js.map +1 -1
  78. package/dist/types.d.ts +93 -0
  79. package/dist/types.d.ts.map +1 -1
  80. package/dist/types.js +8 -0
  81. package/dist/types.js.map +1 -1
  82. package/dist/viewer/html.d.ts +1 -1
  83. package/dist/viewer/html.d.ts.map +1 -1
  84. package/dist/viewer/html.js +2391 -159
  85. package/dist/viewer/html.js.map +1 -1
  86. package/dist/viewer/server.d.ts +16 -0
  87. package/dist/viewer/server.d.ts.map +1 -1
  88. package/dist/viewer/server.js +346 -3
  89. package/dist/viewer/server.js.map +1 -1
  90. package/index.ts +572 -89
  91. package/openclaw.plugin.json +20 -45
  92. package/package.json +3 -4
  93. package/skill/memos-memory-guide/SKILL.md +86 -0
  94. package/src/capture/index.ts +85 -45
  95. package/src/ingest/dedup.ts +29 -0
  96. package/src/ingest/providers/anthropic.ts +258 -1
  97. package/src/ingest/providers/bedrock.ts +256 -6
  98. package/src/ingest/providers/gemini.ts +252 -1
  99. package/src/ingest/providers/index.ts +156 -8
  100. package/src/ingest/providers/openai.ts +304 -1
  101. package/src/ingest/task-processor.ts +396 -0
  102. package/src/ingest/worker.ts +145 -34
  103. package/src/recall/engine.ts +23 -12
  104. package/src/recall/mmr.ts +3 -1
  105. package/src/skill/bundled-memory-guide.ts +91 -0
  106. package/src/skill/evaluator.ts +220 -0
  107. package/src/skill/evolver.ts +169 -0
  108. package/src/skill/generator.ts +506 -0
  109. package/src/skill/installer.ts +59 -0
  110. package/src/skill/upgrader.ts +257 -0
  111. package/src/skill/validator.ts +227 -0
  112. package/src/storage/sqlite.ts +802 -7
  113. package/src/types.ts +96 -0
  114. package/src/viewer/html.ts +2391 -159
  115. package/src/viewer/server.ts +346 -3
  116. package/SKILL.md +0 -43
  117. package/www/index.html +0 -632
@@ -1,12 +1,221 @@
1
1
  import type { SummarizerConfig, Logger } from "../../types";
2
2
 
3
- const SYSTEM_PROMPT = `Summarize the text in ONE concise sentence (max 60 tokens). Preserve exact names, commands, error codes. No bullet points, no preamble — output only the sentence.`;
3
+ const SYSTEM_PROMPT = `Summarize the text in ONE concise sentence (max 120 characters). IMPORTANT: Use the SAME language as the input text — if the input is Chinese, write Chinese; if English, write English. Preserve exact names, commands, error codes. No bullet points, no preamble — output only the sentence.`;
4
+
5
+ const TASK_SUMMARY_PROMPT = `You create a DETAILED task summary from a multi-turn conversation. This summary will be the ONLY record of this conversation, so it must preserve ALL important information.
6
+
7
+ CRITICAL LANGUAGE RULE: You MUST write in the SAME language as the user's messages. Chinese input → Chinese output. English input → English output. NEVER mix languages.
8
+
9
+ Output EXACTLY this structure:
10
+
11
+ 📌 Title
12
+ A short, descriptive title (10-30 characters). Like a chat group name.
13
+
14
+ 🎯 Goal
15
+ One sentence: what the user wanted to accomplish.
16
+
17
+ 📋 Key Steps
18
+ - Describe each meaningful step in detail
19
+ - Include the ACTUAL content produced: code snippets, commands, config blocks, formulas, key paragraphs
20
+ - For code: include the function signature and core logic (up to ~30 lines per block), use fenced code blocks
21
+ - For configs: include the actual config values and structure
22
+ - For lists/instructions: include the actual items, not just "provided a list"
23
+ - Merge only truly trivial back-and-forth (like "ok" / "sure")
24
+ - Do NOT over-summarize: "provided a function" is BAD; show the actual function
25
+
26
+ ✅ Result
27
+ What was the final outcome? Include the final version of any code/config/content produced.
28
+
29
+ 💡 Key Details
30
+ - Decisions made, trade-offs discussed, caveats noted, alternative approaches mentioned
31
+ - Specific values: numbers, versions, thresholds, URLs, file paths, model names
32
+ - Omit this section only if there truly are no noteworthy details
33
+
34
+ RULES:
35
+ - This summary is a KNOWLEDGE BASE ENTRY, not a brief note. Be thorough.
36
+ - PRESERVE verbatim: code, commands, URLs, file paths, error messages, config values, version numbers, names, amounts
37
+ - DISCARD only: greetings, filler, the assistant explaining what it will do before doing it
38
+ - Replace secrets (API keys, tokens, passwords) with [REDACTED]
39
+ - Target length: 30-50% of the original conversation length. Longer conversations need longer summaries.
40
+ - Output summary only, no preamble.`;
41
+
42
+ export async function summarizeTaskBedrock(
43
+ text: string,
44
+ cfg: SummarizerConfig,
45
+ log: Logger,
46
+ ): Promise<string> {
47
+ const model = cfg.model ?? "anthropic.claude-3-haiku-20240307-v1:0";
48
+ const endpoint = cfg.endpoint;
49
+ if (!endpoint) {
50
+ throw new Error("Bedrock task-summarizer requires 'endpoint'");
51
+ }
52
+
53
+ const url = `${endpoint}/model/${model}/converse`;
54
+ const headers: Record<string, string> = {
55
+ "Content-Type": "application/json",
56
+ ...cfg.headers,
57
+ };
58
+
59
+ const resp = await fetch(url, {
60
+ method: "POST",
61
+ headers,
62
+ body: JSON.stringify({
63
+ system: [{ text: TASK_SUMMARY_PROMPT }],
64
+ messages: [{ role: "user", content: [{ text }] }],
65
+ inferenceConfig: { temperature: cfg.temperature ?? 0.1, maxTokens: 4096 },
66
+ }),
67
+ signal: AbortSignal.timeout(cfg.timeoutMs ?? 60_000),
68
+ });
69
+
70
+ if (!resp.ok) {
71
+ const body = await resp.text();
72
+ throw new Error(`Bedrock task-summarize failed (${resp.status}): ${body}`);
73
+ }
74
+
75
+ const json = (await resp.json()) as { output: { message: { content: Array<{ text: string }> } } };
76
+ return json.output?.message?.content?.[0]?.text?.trim() ?? "";
77
+ }
78
+
79
+ const TOPIC_JUDGE_PROMPT = `You are a conversation topic boundary detector. Given a summary of the CURRENT conversation and a NEW user message, determine if the new message starts a DIFFERENT topic/task.
80
+
81
+ Answer ONLY "NEW" or "SAME".
82
+
83
+ Rules:
84
+ - "NEW" = the new message is about a completely different subject, project, or task
85
+ - "SAME" = the new message continues, follows up on, or is closely related to the current topic
86
+ - Follow-up questions, clarifications, refinements, bug fixes, or next steps on the same task = SAME
87
+ - Greetings or meta-questions like "你好" or "谢谢" without new substance = SAME
88
+ - A clearly unrelated request (e.g., current topic is deployment, new message asks about cooking) = NEW
89
+
90
+ Output exactly one word: NEW or SAME`;
91
+
92
+ export async function judgeNewTopicBedrock(
93
+ currentContext: string,
94
+ newMessage: string,
95
+ cfg: SummarizerConfig,
96
+ log: Logger,
97
+ ): Promise<boolean> {
98
+ const model = cfg.model ?? "anthropic.claude-3-haiku-20240307-v1:0";
99
+ const endpoint = cfg.endpoint;
100
+ if (!endpoint) {
101
+ throw new Error("Bedrock topic-judge requires 'endpoint'");
102
+ }
103
+
104
+ const url = `${endpoint}/model/${model}/converse`;
105
+ const headers: Record<string, string> = {
106
+ "Content-Type": "application/json",
107
+ ...cfg.headers,
108
+ };
109
+
110
+ const userContent = `CURRENT CONVERSATION SUMMARY:\n${currentContext}\n\nNEW USER MESSAGE:\n${newMessage}`;
111
+
112
+ const resp = await fetch(url, {
113
+ method: "POST",
114
+ headers,
115
+ body: JSON.stringify({
116
+ system: [{ text: TOPIC_JUDGE_PROMPT }],
117
+ messages: [{ role: "user", content: [{ text: userContent }] }],
118
+ inferenceConfig: { temperature: 0, maxTokens: 10 },
119
+ }),
120
+ signal: AbortSignal.timeout(cfg.timeoutMs ?? 15_000),
121
+ });
122
+
123
+ if (!resp.ok) {
124
+ const body = await resp.text();
125
+ throw new Error(`Bedrock topic-judge failed (${resp.status}): ${body}`);
126
+ }
127
+
128
+ const json = (await resp.json()) as { output: { message: { content: Array<{ text: string }> } } };
129
+ const answer = json.output?.message?.content?.[0]?.text?.trim().toUpperCase() ?? "";
130
+ log.debug(`Topic judge result: "${answer}"`);
131
+ return answer.startsWith("NEW");
132
+ }
133
+
134
+ const FILTER_RELEVANT_PROMPT = `You are a memory relevance judge. Given a user's QUERY and a list of CANDIDATE memory summaries, do two things:
135
+
136
+ 1. Select ALL candidates that could be useful for answering the query. When in doubt, INCLUDE the candidate.
137
+ - For questions about lists, history, or "what/where/who" across multiple items (e.g. "which companies did I work at"), include ALL matching items — do NOT stop at the first match.
138
+ - For factual lookups (e.g. "what is the SSH port"), a single direct answer is enough.
139
+ 2. Judge whether the selected memories are SUFFICIENT to fully answer the query WITHOUT fetching additional context.
140
+
141
+ IMPORTANT for "sufficient" judgment:
142
+ - sufficient=true ONLY when the memories contain a concrete ANSWER, fact, decision, or actionable information that directly addresses the query.
143
+ - sufficient=false when:
144
+ - The memories only repeat the same question the user asked before (echo, not answer).
145
+ - The memories show related topics but lack the specific detail needed.
146
+ - The memories contain partial information that would benefit from full task context, timeline, or related skills.
147
+
148
+ Output a JSON object with exactly two fields:
149
+ {"relevant":[1,3,5],"sufficient":true}
150
+
151
+ - "relevant": array of candidate numbers that are useful. Empty array [] if none are relevant.
152
+ - "sufficient": true ONLY if the memories contain a direct answer; false otherwise.
153
+
154
+ Output ONLY the JSON object, nothing else.`;
155
+
156
+ import type { FilterResult } from "./openai";
157
+ export type { FilterResult } from "./openai";
158
+
159
+ export async function filterRelevantBedrock(
160
+ query: string,
161
+ candidates: Array<{ index: number; summary: string; role: string }>,
162
+ cfg: SummarizerConfig,
163
+ log: Logger,
164
+ ): Promise<FilterResult> {
165
+ const model = cfg.model ?? "anthropic.claude-3-haiku-20240307-v1:0";
166
+ const endpoint = cfg.endpoint;
167
+ if (!endpoint) {
168
+ throw new Error("Bedrock filter-relevant requires 'endpoint'");
169
+ }
170
+
171
+ const url = `${endpoint}/model/${model}/converse`;
172
+ const headers: Record<string, string> = {
173
+ "Content-Type": "application/json",
174
+ ...cfg.headers,
175
+ };
176
+
177
+ const candidateText = candidates
178
+ .map((c) => `${c.index}. [${c.role}] ${c.summary}`)
179
+ .join("\n");
180
+
181
+ const resp = await fetch(url, {
182
+ method: "POST",
183
+ headers,
184
+ body: JSON.stringify({
185
+ system: [{ text: FILTER_RELEVANT_PROMPT }],
186
+ messages: [{ role: "user", content: [{ text: `QUERY: ${query}\n\nCANDIDATES:\n${candidateText}` }] }],
187
+ inferenceConfig: { temperature: 0, maxTokens: 200 },
188
+ }),
189
+ signal: AbortSignal.timeout(cfg.timeoutMs ?? 15_000),
190
+ });
191
+
192
+ if (!resp.ok) {
193
+ const body = await resp.text();
194
+ throw new Error(`Bedrock filter-relevant failed (${resp.status}): ${body}`);
195
+ }
196
+
197
+ const json = (await resp.json()) as { output: { message: { content: Array<{ text: string }> } } };
198
+ const raw = json.output?.message?.content?.[0]?.text?.trim() ?? "{}";
199
+ return parseFilterResult(raw, log);
200
+ }
201
+
202
+ function parseFilterResult(raw: string, log: Logger): FilterResult {
203
+ try {
204
+ const match = raw.match(/\{[\s\S]*\}/);
205
+ if (match) {
206
+ const obj = JSON.parse(match[0]);
207
+ if (obj && Array.isArray(obj.relevant)) {
208
+ return {
209
+ relevant: obj.relevant.filter((n: any) => typeof n === "number"),
210
+ sufficient: obj.sufficient === true,
211
+ };
212
+ }
213
+ }
214
+ } catch {}
215
+ log.warn(`filterRelevant: failed to parse LLM output: "${raw}", fallback to all+insufficient`);
216
+ return { relevant: [], sufficient: false };
217
+ }
4
218
 
5
- /**
6
- * AWS Bedrock Converse API adapter.
7
- * Expects cfg.endpoint to be the full Bedrock invoke URL and
8
- * authentication handled via AWS SDK credential chain (env vars / IAM role).
9
- */
10
219
  export async function summarizeBedrock(
11
220
  text: string,
12
221
  cfg: SummarizerConfig,
@@ -48,3 +257,44 @@ export async function summarizeBedrock(
48
257
  };
49
258
  return json.output?.message?.content?.[0]?.text?.trim() ?? "";
50
259
  }
260
+
261
+ // ─── Smart Dedup ───
262
+
263
+ import { DEDUP_JUDGE_PROMPT, parseDedupResult } from "./openai";
264
+ import type { DedupResult } from "./openai";
265
+ export type { DedupResult } from "./openai";
266
+
267
+ export async function judgeDedupBedrock(
268
+ newSummary: string,
269
+ candidates: Array<{ index: number; summary: string; chunkId: string }>,
270
+ cfg: SummarizerConfig,
271
+ log: Logger,
272
+ ): Promise<DedupResult> {
273
+ const model = cfg.model ?? "anthropic.claude-3-haiku-20240307-v1:0";
274
+ const endpoint = cfg.endpoint;
275
+ if (!endpoint) throw new Error("Bedrock dedup-judge requires 'endpoint'");
276
+
277
+ const url = `${endpoint}/model/${model}/converse`;
278
+ const headers: Record<string, string> = { "Content-Type": "application/json", ...cfg.headers };
279
+ const candidateText = candidates.map((c) => `${c.index}. ${c.summary}`).join("\n");
280
+
281
+ const resp = await fetch(url, {
282
+ method: "POST",
283
+ headers,
284
+ body: JSON.stringify({
285
+ system: [{ text: DEDUP_JUDGE_PROMPT }],
286
+ messages: [{ role: "user", content: [{ text: `NEW MEMORY:\n${newSummary}\n\nEXISTING MEMORIES:\n${candidateText}` }] }],
287
+ inferenceConfig: { temperature: 0, maxTokens: 300 },
288
+ }),
289
+ signal: AbortSignal.timeout(cfg.timeoutMs ?? 15_000),
290
+ });
291
+
292
+ if (!resp.ok) {
293
+ const body = await resp.text();
294
+ throw new Error(`Bedrock dedup-judge failed (${resp.status}): ${body}`);
295
+ }
296
+
297
+ const json = (await resp.json()) as { output: { message: { content: Array<{ text: string }> } } };
298
+ const raw = json.output?.message?.content?.[0]?.text?.trim() ?? "{}";
299
+ return parseDedupResult(raw, log);
300
+ }
@@ -1,6 +1,217 @@
1
1
  import type { SummarizerConfig, Logger } from "../../types";
2
2
 
3
- const SYSTEM_PROMPT = `Summarize the text in ONE concise sentence (max 60 tokens). Preserve exact names, commands, error codes. No bullet points, no preamble — output only the sentence.`;
3
+ const SYSTEM_PROMPT = `Summarize the text in ONE concise sentence (max 120 characters). IMPORTANT: Use the SAME language as the input text — if the input is Chinese, write Chinese; if English, write English. Preserve exact names, commands, error codes. No bullet points, no preamble — output only the sentence.`;
4
+
5
+ const TASK_SUMMARY_PROMPT = `You create a DETAILED task summary from a multi-turn conversation. This summary will be the ONLY record of this conversation, so it must preserve ALL important information.
6
+
7
+ CRITICAL LANGUAGE RULE: You MUST write in the SAME language as the user's messages. Chinese input → Chinese output. English input → English output. NEVER mix languages.
8
+
9
+ Output EXACTLY this structure:
10
+
11
+ 📌 Title
12
+ A short, descriptive title (10-30 characters). Like a chat group name.
13
+
14
+ 🎯 Goal
15
+ One sentence: what the user wanted to accomplish.
16
+
17
+ 📋 Key Steps
18
+ - Describe each meaningful step in detail
19
+ - Include the ACTUAL content produced: code snippets, commands, config blocks, formulas, key paragraphs
20
+ - For code: include the function signature and core logic (up to ~30 lines per block), use fenced code blocks
21
+ - For configs: include the actual config values and structure
22
+ - For lists/instructions: include the actual items, not just "provided a list"
23
+ - Merge only truly trivial back-and-forth (like "ok" / "sure")
24
+ - Do NOT over-summarize: "provided a function" is BAD; show the actual function
25
+
26
+ ✅ Result
27
+ What was the final outcome? Include the final version of any code/config/content produced.
28
+
29
+ 💡 Key Details
30
+ - Decisions made, trade-offs discussed, caveats noted, alternative approaches mentioned
31
+ - Specific values: numbers, versions, thresholds, URLs, file paths, model names
32
+ - Omit this section only if there truly are no noteworthy details
33
+
34
+ RULES:
35
+ - This summary is a KNOWLEDGE BASE ENTRY, not a brief note. Be thorough.
36
+ - PRESERVE verbatim: code, commands, URLs, file paths, error messages, config values, version numbers, names, amounts
37
+ - DISCARD only: greetings, filler, the assistant explaining what it will do before doing it
38
+ - Replace secrets (API keys, tokens, passwords) with [REDACTED]
39
+ - Target length: 30-50% of the original conversation length. Longer conversations need longer summaries.
40
+ - Output summary only, no preamble.`;
41
+
42
+ export async function summarizeTaskGemini(
43
+ text: string,
44
+ cfg: SummarizerConfig,
45
+ log: Logger,
46
+ ): Promise<string> {
47
+ const model = cfg.model ?? "gemini-1.5-flash";
48
+ const endpoint =
49
+ cfg.endpoint ??
50
+ `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`;
51
+
52
+ const url = `${endpoint}?key=${cfg.apiKey}`;
53
+ const headers: Record<string, string> = {
54
+ "Content-Type": "application/json",
55
+ ...cfg.headers,
56
+ };
57
+
58
+ const resp = await fetch(url, {
59
+ method: "POST",
60
+ headers,
61
+ body: JSON.stringify({
62
+ systemInstruction: { parts: [{ text: TASK_SUMMARY_PROMPT }] },
63
+ contents: [{ parts: [{ text }] }],
64
+ generationConfig: { temperature: cfg.temperature ?? 0.1, maxOutputTokens: 4096 },
65
+ }),
66
+ signal: AbortSignal.timeout(cfg.timeoutMs ?? 60_000),
67
+ });
68
+
69
+ if (!resp.ok) {
70
+ const body = await resp.text();
71
+ throw new Error(`Gemini task-summarize failed (${resp.status}): ${body}`);
72
+ }
73
+
74
+ const json = (await resp.json()) as { candidates: Array<{ content: { parts: Array<{ text: string }> } }> };
75
+ return json.candidates?.[0]?.content?.parts?.[0]?.text?.trim() ?? "";
76
+ }
77
+
78
+ const TOPIC_JUDGE_PROMPT = `You are a conversation topic boundary detector. Given a summary of the CURRENT conversation and a NEW user message, determine if the new message starts a DIFFERENT topic/task.
79
+
80
+ Answer ONLY "NEW" or "SAME".
81
+
82
+ Rules:
83
+ - "NEW" = the new message is about a completely different subject, project, or task
84
+ - "SAME" = the new message continues, follows up on, or is closely related to the current topic
85
+ - Follow-up questions, clarifications, refinements, bug fixes, or next steps on the same task = SAME
86
+ - Greetings or meta-questions like "你好" or "谢谢" without new substance = SAME
87
+ - A clearly unrelated request (e.g., current topic is deployment, new message asks about cooking) = NEW
88
+
89
+ Output exactly one word: NEW or SAME`;
90
+
91
+ export async function judgeNewTopicGemini(
92
+ currentContext: string,
93
+ newMessage: string,
94
+ cfg: SummarizerConfig,
95
+ log: Logger,
96
+ ): Promise<boolean> {
97
+ const model = cfg.model ?? "gemini-1.5-flash";
98
+ const endpoint =
99
+ cfg.endpoint ??
100
+ `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`;
101
+
102
+ const url = `${endpoint}?key=${cfg.apiKey}`;
103
+ const headers: Record<string, string> = {
104
+ "Content-Type": "application/json",
105
+ ...cfg.headers,
106
+ };
107
+
108
+ const userContent = `CURRENT CONVERSATION SUMMARY:\n${currentContext}\n\nNEW USER MESSAGE:\n${newMessage}`;
109
+
110
+ const resp = await fetch(url, {
111
+ method: "POST",
112
+ headers,
113
+ body: JSON.stringify({
114
+ systemInstruction: { parts: [{ text: TOPIC_JUDGE_PROMPT }] },
115
+ contents: [{ parts: [{ text: userContent }] }],
116
+ generationConfig: { temperature: 0, maxOutputTokens: 10 },
117
+ }),
118
+ signal: AbortSignal.timeout(cfg.timeoutMs ?? 15_000),
119
+ });
120
+
121
+ if (!resp.ok) {
122
+ const body = await resp.text();
123
+ throw new Error(`Gemini topic-judge failed (${resp.status}): ${body}`);
124
+ }
125
+
126
+ const json = (await resp.json()) as { candidates: Array<{ content: { parts: Array<{ text: string }> } }> };
127
+ const answer = json.candidates?.[0]?.content?.parts?.[0]?.text?.trim().toUpperCase() ?? "";
128
+ log.debug(`Topic judge result: "${answer}"`);
129
+ return answer.startsWith("NEW");
130
+ }
131
+
132
+ const FILTER_RELEVANT_PROMPT = `You are a memory relevance judge. Given a user's QUERY and a list of CANDIDATE memory summaries, do two things:
133
+
134
+ 1. Select ALL candidates that could be useful for answering the query. When in doubt, INCLUDE the candidate.
135
+ - For questions about lists, history, or "what/where/who" across multiple items (e.g. "which companies did I work at"), include ALL matching items — do NOT stop at the first match.
136
+ - For factual lookups (e.g. "what is the SSH port"), a single direct answer is enough.
137
+ 2. Judge whether the selected memories are SUFFICIENT to fully answer the query WITHOUT fetching additional context.
138
+
139
+ IMPORTANT for "sufficient" judgment:
140
+ - sufficient=true ONLY when the memories contain a concrete ANSWER, fact, decision, or actionable information that directly addresses the query.
141
+ - sufficient=false when:
142
+ - The memories only repeat the same question the user asked before (echo, not answer).
143
+ - The memories show related topics but lack the specific detail needed.
144
+ - The memories contain partial information that would benefit from full task context, timeline, or related skills.
145
+
146
+ Output a JSON object with exactly two fields:
147
+ {"relevant":[1,3,5],"sufficient":true}
148
+
149
+ - "relevant": array of candidate numbers that are useful. Empty array [] if none are relevant.
150
+ - "sufficient": true ONLY if the memories contain a direct answer; false otherwise.
151
+
152
+ Output ONLY the JSON object, nothing else.`;
153
+
154
+ import type { FilterResult } from "./openai";
155
+ export type { FilterResult } from "./openai";
156
+
157
+ export async function filterRelevantGemini(
158
+ query: string,
159
+ candidates: Array<{ index: number; summary: string; role: string }>,
160
+ cfg: SummarizerConfig,
161
+ log: Logger,
162
+ ): Promise<FilterResult> {
163
+ const model = cfg.model ?? "gemini-1.5-flash";
164
+ const endpoint =
165
+ cfg.endpoint ??
166
+ `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`;
167
+
168
+ const url = `${endpoint}?key=${cfg.apiKey}`;
169
+ const headers: Record<string, string> = {
170
+ "Content-Type": "application/json",
171
+ ...cfg.headers,
172
+ };
173
+
174
+ const candidateText = candidates
175
+ .map((c) => `${c.index}. [${c.role}] ${c.summary}`)
176
+ .join("\n");
177
+
178
+ const resp = await fetch(url, {
179
+ method: "POST",
180
+ headers,
181
+ body: JSON.stringify({
182
+ systemInstruction: { parts: [{ text: FILTER_RELEVANT_PROMPT }] },
183
+ contents: [{ parts: [{ text: `QUERY: ${query}\n\nCANDIDATES:\n${candidateText}` }] }],
184
+ generationConfig: { temperature: 0, maxOutputTokens: 200 },
185
+ }),
186
+ signal: AbortSignal.timeout(cfg.timeoutMs ?? 15_000),
187
+ });
188
+
189
+ if (!resp.ok) {
190
+ const body = await resp.text();
191
+ throw new Error(`Gemini filter-relevant failed (${resp.status}): ${body}`);
192
+ }
193
+
194
+ const json = (await resp.json()) as { candidates: Array<{ content: { parts: Array<{ text: string }> } }> };
195
+ const raw = json.candidates?.[0]?.content?.parts?.[0]?.text?.trim() ?? "{}";
196
+ return parseFilterResult(raw, log);
197
+ }
198
+
199
+ function parseFilterResult(raw: string, log: Logger): FilterResult {
200
+ try {
201
+ const match = raw.match(/\{[\s\S]*\}/);
202
+ if (match) {
203
+ const obj = JSON.parse(match[0]);
204
+ if (obj && Array.isArray(obj.relevant)) {
205
+ return {
206
+ relevant: obj.relevant.filter((n: any) => typeof n === "number"),
207
+ sufficient: obj.sufficient === true,
208
+ };
209
+ }
210
+ }
211
+ } catch {}
212
+ log.warn(`filterRelevant: failed to parse LLM output: "${raw}", fallback to all+insufficient`);
213
+ return { relevant: [], sufficient: false };
214
+ }
4
215
 
5
216
  export async function summarizeGemini(
6
217
  text: string,
@@ -39,3 +250,43 @@ export async function summarizeGemini(
39
250
  };
40
251
  return json.candidates?.[0]?.content?.parts?.[0]?.text?.trim() ?? "";
41
252
  }
253
+
254
+ // ─── Smart Dedup ───
255
+
256
+ import { DEDUP_JUDGE_PROMPT, parseDedupResult } from "./openai";
257
+ import type { DedupResult } from "./openai";
258
+ export type { DedupResult } from "./openai";
259
+
260
+ export async function judgeDedupGemini(
261
+ newSummary: string,
262
+ candidates: Array<{ index: number; summary: string; chunkId: string }>,
263
+ cfg: SummarizerConfig,
264
+ log: Logger,
265
+ ): Promise<DedupResult> {
266
+ const model = cfg.model ?? "gemini-1.5-flash";
267
+ const endpoint = cfg.endpoint ?? `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`;
268
+ const url = `${endpoint}?key=${cfg.apiKey}`;
269
+ const headers: Record<string, string> = { "Content-Type": "application/json", ...cfg.headers };
270
+
271
+ const candidateText = candidates.map((c) => `${c.index}. ${c.summary}`).join("\n");
272
+
273
+ const resp = await fetch(url, {
274
+ method: "POST",
275
+ headers,
276
+ body: JSON.stringify({
277
+ systemInstruction: { parts: [{ text: DEDUP_JUDGE_PROMPT }] },
278
+ contents: [{ parts: [{ text: `NEW MEMORY:\n${newSummary}\n\nEXISTING MEMORIES:\n${candidateText}` }] }],
279
+ generationConfig: { temperature: 0, maxOutputTokens: 300 },
280
+ }),
281
+ signal: AbortSignal.timeout(cfg.timeoutMs ?? 15_000),
282
+ });
283
+
284
+ if (!resp.ok) {
285
+ const body = await resp.text();
286
+ throw new Error(`Gemini dedup-judge failed (${resp.status}): ${body}`);
287
+ }
288
+
289
+ const json = (await resp.json()) as { candidates: Array<{ content: { parts: Array<{ text: string }> } }> };
290
+ const raw = json.candidates?.[0]?.content?.parts?.[0]?.text?.trim() ?? "{}";
291
+ return parseDedupResult(raw, log);
292
+ }