@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.
- package/.env.example +13 -5
- package/README.md +283 -91
- package/dist/capture/index.d.ts +5 -7
- package/dist/capture/index.d.ts.map +1 -1
- package/dist/capture/index.js +72 -43
- package/dist/capture/index.js.map +1 -1
- package/dist/ingest/dedup.d.ts +8 -0
- package/dist/ingest/dedup.d.ts.map +1 -1
- package/dist/ingest/dedup.js +21 -0
- package/dist/ingest/dedup.js.map +1 -1
- package/dist/ingest/providers/anthropic.d.ts +16 -0
- package/dist/ingest/providers/anthropic.d.ts.map +1 -1
- package/dist/ingest/providers/anthropic.js +214 -1
- package/dist/ingest/providers/anthropic.js.map +1 -1
- package/dist/ingest/providers/bedrock.d.ts +16 -5
- package/dist/ingest/providers/bedrock.d.ts.map +1 -1
- package/dist/ingest/providers/bedrock.js +210 -6
- package/dist/ingest/providers/bedrock.js.map +1 -1
- package/dist/ingest/providers/gemini.d.ts +16 -0
- package/dist/ingest/providers/gemini.d.ts.map +1 -1
- package/dist/ingest/providers/gemini.js +202 -1
- package/dist/ingest/providers/gemini.js.map +1 -1
- package/dist/ingest/providers/index.d.ts +31 -0
- package/dist/ingest/providers/index.d.ts.map +1 -1
- package/dist/ingest/providers/index.js +134 -4
- package/dist/ingest/providers/index.js.map +1 -1
- package/dist/ingest/providers/openai.d.ts +24 -0
- package/dist/ingest/providers/openai.d.ts.map +1 -1
- package/dist/ingest/providers/openai.js +255 -1
- package/dist/ingest/providers/openai.js.map +1 -1
- package/dist/ingest/task-processor.d.ts +65 -0
- package/dist/ingest/task-processor.d.ts.map +1 -0
- package/dist/ingest/task-processor.js +354 -0
- package/dist/ingest/task-processor.js.map +1 -0
- package/dist/ingest/worker.d.ts +3 -1
- package/dist/ingest/worker.d.ts.map +1 -1
- package/dist/ingest/worker.js +131 -23
- package/dist/ingest/worker.js.map +1 -1
- package/dist/recall/engine.d.ts +1 -0
- package/dist/recall/engine.d.ts.map +1 -1
- package/dist/recall/engine.js +22 -11
- package/dist/recall/engine.js.map +1 -1
- package/dist/recall/mmr.d.ts.map +1 -1
- package/dist/recall/mmr.js +3 -1
- package/dist/recall/mmr.js.map +1 -1
- package/dist/skill/bundled-memory-guide.d.ts +6 -0
- package/dist/skill/bundled-memory-guide.d.ts.map +1 -0
- package/dist/skill/bundled-memory-guide.js +95 -0
- package/dist/skill/bundled-memory-guide.js.map +1 -0
- package/dist/skill/evaluator.d.ts +31 -0
- package/dist/skill/evaluator.d.ts.map +1 -0
- package/dist/skill/evaluator.js +194 -0
- package/dist/skill/evaluator.js.map +1 -0
- package/dist/skill/evolver.d.ts +22 -0
- package/dist/skill/evolver.d.ts.map +1 -0
- package/dist/skill/evolver.js +193 -0
- package/dist/skill/evolver.js.map +1 -0
- package/dist/skill/generator.d.ts +25 -0
- package/dist/skill/generator.d.ts.map +1 -0
- package/dist/skill/generator.js +477 -0
- package/dist/skill/generator.js.map +1 -0
- package/dist/skill/installer.d.ts +16 -0
- package/dist/skill/installer.d.ts.map +1 -0
- package/dist/skill/installer.js +89 -0
- package/dist/skill/installer.js.map +1 -0
- package/dist/skill/upgrader.d.ts +19 -0
- package/dist/skill/upgrader.d.ts.map +1 -0
- package/dist/skill/upgrader.js +263 -0
- package/dist/skill/upgrader.js.map +1 -0
- package/dist/skill/validator.d.ts +29 -0
- package/dist/skill/validator.d.ts.map +1 -0
- package/dist/skill/validator.js +227 -0
- package/dist/skill/validator.js.map +1 -0
- package/dist/storage/sqlite.d.ts +141 -1
- package/dist/storage/sqlite.d.ts.map +1 -1
- package/dist/storage/sqlite.js +664 -7
- package/dist/storage/sqlite.js.map +1 -1
- package/dist/types.d.ts +93 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -1
- package/dist/viewer/html.d.ts +1 -1
- package/dist/viewer/html.d.ts.map +1 -1
- package/dist/viewer/html.js +2391 -159
- package/dist/viewer/html.js.map +1 -1
- package/dist/viewer/server.d.ts +16 -0
- package/dist/viewer/server.d.ts.map +1 -1
- package/dist/viewer/server.js +346 -3
- package/dist/viewer/server.js.map +1 -1
- package/index.ts +572 -89
- package/openclaw.plugin.json +20 -45
- package/package.json +3 -4
- package/skill/memos-memory-guide/SKILL.md +86 -0
- package/src/capture/index.ts +85 -45
- package/src/ingest/dedup.ts +29 -0
- package/src/ingest/providers/anthropic.ts +258 -1
- package/src/ingest/providers/bedrock.ts +256 -6
- package/src/ingest/providers/gemini.ts +252 -1
- package/src/ingest/providers/index.ts +156 -8
- package/src/ingest/providers/openai.ts +304 -1
- package/src/ingest/task-processor.ts +396 -0
- package/src/ingest/worker.ts +145 -34
- package/src/recall/engine.ts +23 -12
- package/src/recall/mmr.ts +3 -1
- package/src/skill/bundled-memory-guide.ts +91 -0
- package/src/skill/evaluator.ts +220 -0
- package/src/skill/evolver.ts +169 -0
- package/src/skill/generator.ts +506 -0
- package/src/skill/installer.ts +59 -0
- package/src/skill/upgrader.ts +257 -0
- package/src/skill/validator.ts +227 -0
- package/src/storage/sqlite.ts +802 -7
- package/src/types.ts +96 -0
- package/src/viewer/html.ts +2391 -159
- package/src/viewer/server.ts +346 -3
- package/SKILL.md +0 -43
- package/www/index.html +0 -632
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { SummarizerConfig, Logger } from "../../types";
|
|
2
|
-
import { summarizeOpenAI } from "./openai";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
import {
|
|
2
|
+
import { summarizeOpenAI, summarizeTaskOpenAI, judgeNewTopicOpenAI, filterRelevantOpenAI, judgeDedupOpenAI } from "./openai";
|
|
3
|
+
import type { FilterResult, DedupResult } from "./openai";
|
|
4
|
+
export type { FilterResult, DedupResult } from "./openai";
|
|
5
|
+
import { summarizeAnthropic, summarizeTaskAnthropic, judgeNewTopicAnthropic, filterRelevantAnthropic, judgeDedupAnthropic } from "./anthropic";
|
|
6
|
+
import { summarizeGemini, summarizeTaskGemini, judgeNewTopicGemini, filterRelevantGemini, judgeDedupGemini } from "./gemini";
|
|
7
|
+
import { summarizeBedrock, summarizeTaskBedrock, judgeNewTopicBedrock, filterRelevantBedrock, judgeDedupBedrock } from "./bedrock";
|
|
6
8
|
|
|
7
9
|
export class Summarizer {
|
|
8
10
|
constructor(
|
|
@@ -23,6 +25,19 @@ export class Summarizer {
|
|
|
23
25
|
}
|
|
24
26
|
}
|
|
25
27
|
|
|
28
|
+
async summarizeTask(text: string): Promise<string> {
|
|
29
|
+
if (!this.cfg) {
|
|
30
|
+
return taskFallback(text);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
return await this.callTaskProvider(text);
|
|
35
|
+
} catch (err) {
|
|
36
|
+
this.log.warn(`Task summarizer failed, using fallback: ${err}`);
|
|
37
|
+
return taskFallback(text);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
26
41
|
private async callProvider(text: string): Promise<string> {
|
|
27
42
|
const cfg = this.cfg!;
|
|
28
43
|
switch (cfg.provider) {
|
|
@@ -41,12 +56,145 @@ export class Summarizer {
|
|
|
41
56
|
throw new Error(`Unknown summarizer provider: ${cfg.provider}`);
|
|
42
57
|
}
|
|
43
58
|
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Ask the LLM whether the new message starts a different topic from the current conversation.
|
|
62
|
+
* Returns true if it's a new topic, false if it continues the current one.
|
|
63
|
+
* Returns null if no summarizer is configured (caller should fall back to heuristic).
|
|
64
|
+
*/
|
|
65
|
+
async judgeNewTopic(currentContext: string, newMessage: string): Promise<boolean | null> {
|
|
66
|
+
if (!this.cfg) return null;
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
return await this.callTopicJudge(currentContext, newMessage);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
this.log.warn(`Topic judge failed: ${err}`);
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private async callTopicJudge(currentContext: string, newMessage: string): Promise<boolean> {
|
|
77
|
+
const cfg = this.cfg!;
|
|
78
|
+
switch (cfg.provider) {
|
|
79
|
+
case "openai":
|
|
80
|
+
case "openai_compatible":
|
|
81
|
+
case "azure_openai":
|
|
82
|
+
return judgeNewTopicOpenAI(currentContext, newMessage, cfg, this.log);
|
|
83
|
+
case "anthropic":
|
|
84
|
+
return judgeNewTopicAnthropic(currentContext, newMessage, cfg, this.log);
|
|
85
|
+
case "gemini":
|
|
86
|
+
return judgeNewTopicGemini(currentContext, newMessage, cfg, this.log);
|
|
87
|
+
case "bedrock":
|
|
88
|
+
return judgeNewTopicBedrock(currentContext, newMessage, cfg, this.log);
|
|
89
|
+
default:
|
|
90
|
+
throw new Error(`Unknown summarizer provider: ${cfg.provider}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Filter search results by LLM relevance judgment.
|
|
96
|
+
* Returns { relevant: number[], sufficient: boolean } or null if no summarizer configured.
|
|
97
|
+
*/
|
|
98
|
+
async filterRelevant(
|
|
99
|
+
query: string,
|
|
100
|
+
candidates: Array<{ index: number; summary: string; role: string }>,
|
|
101
|
+
): Promise<FilterResult | null> {
|
|
102
|
+
if (!this.cfg) return null;
|
|
103
|
+
if (candidates.length === 0) return { relevant: [], sufficient: true };
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
return await this.callFilterRelevant(query, candidates);
|
|
107
|
+
} catch (err) {
|
|
108
|
+
this.log.warn(`filterRelevant failed, returning all candidates: ${err}`);
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private async callFilterRelevant(
|
|
114
|
+
query: string,
|
|
115
|
+
candidates: Array<{ index: number; summary: string; role: string }>,
|
|
116
|
+
): Promise<FilterResult> {
|
|
117
|
+
const cfg = this.cfg!;
|
|
118
|
+
switch (cfg.provider) {
|
|
119
|
+
case "openai":
|
|
120
|
+
case "openai_compatible":
|
|
121
|
+
case "azure_openai":
|
|
122
|
+
return filterRelevantOpenAI(query, candidates, cfg, this.log);
|
|
123
|
+
case "anthropic":
|
|
124
|
+
return filterRelevantAnthropic(query, candidates, cfg, this.log);
|
|
125
|
+
case "gemini":
|
|
126
|
+
return filterRelevantGemini(query, candidates, cfg, this.log);
|
|
127
|
+
case "bedrock":
|
|
128
|
+
return filterRelevantBedrock(query, candidates, cfg, this.log);
|
|
129
|
+
default:
|
|
130
|
+
throw new Error(`Unknown summarizer provider: ${cfg.provider}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Judge whether a new memory is DUPLICATE / UPDATE / NEW relative to similar existing memories.
|
|
136
|
+
* Returns null if no summarizer configured (caller should treat as NEW).
|
|
137
|
+
*/
|
|
138
|
+
async judgeDedup(
|
|
139
|
+
newSummary: string,
|
|
140
|
+
candidates: Array<{ index: number; summary: string; chunkId: string }>,
|
|
141
|
+
): Promise<DedupResult | null> {
|
|
142
|
+
if (!this.cfg) return null;
|
|
143
|
+
if (candidates.length === 0) return null;
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
return await this.callJudgeDedup(newSummary, candidates);
|
|
147
|
+
} catch (err) {
|
|
148
|
+
this.log.warn(`judgeDedup failed, treating as NEW: ${err}`);
|
|
149
|
+
return { action: "NEW", reason: "llm_error" };
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private async callJudgeDedup(
|
|
154
|
+
newSummary: string,
|
|
155
|
+
candidates: Array<{ index: number; summary: string; chunkId: string }>,
|
|
156
|
+
): Promise<DedupResult> {
|
|
157
|
+
const cfg = this.cfg!;
|
|
158
|
+
switch (cfg.provider) {
|
|
159
|
+
case "openai":
|
|
160
|
+
case "openai_compatible":
|
|
161
|
+
case "azure_openai":
|
|
162
|
+
return judgeDedupOpenAI(newSummary, candidates, cfg, this.log);
|
|
163
|
+
case "anthropic":
|
|
164
|
+
return judgeDedupAnthropic(newSummary, candidates, cfg, this.log);
|
|
165
|
+
case "gemini":
|
|
166
|
+
return judgeDedupGemini(newSummary, candidates, cfg, this.log);
|
|
167
|
+
case "bedrock":
|
|
168
|
+
return judgeDedupBedrock(newSummary, candidates, cfg, this.log);
|
|
169
|
+
default:
|
|
170
|
+
throw new Error(`Unknown summarizer provider: ${cfg.provider}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private async callTaskProvider(text: string): Promise<string> {
|
|
175
|
+
const cfg = this.cfg!;
|
|
176
|
+
switch (cfg.provider) {
|
|
177
|
+
case "openai":
|
|
178
|
+
case "openai_compatible":
|
|
179
|
+
case "azure_openai":
|
|
180
|
+
return summarizeTaskOpenAI(text, cfg, this.log);
|
|
181
|
+
case "anthropic":
|
|
182
|
+
return summarizeTaskAnthropic(text, cfg, this.log);
|
|
183
|
+
case "gemini":
|
|
184
|
+
return summarizeTaskGemini(text, cfg, this.log);
|
|
185
|
+
case "bedrock":
|
|
186
|
+
return summarizeTaskBedrock(text, cfg, this.log);
|
|
187
|
+
default:
|
|
188
|
+
throw new Error(`Unknown summarizer provider: ${cfg.provider}`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function taskFallback(text: string): string {
|
|
194
|
+
const lines = text.split("\n").filter((l) => l.trim().length > 10);
|
|
195
|
+
return lines.slice(0, 30).join("\n").slice(0, 2000);
|
|
44
196
|
}
|
|
45
197
|
|
|
46
|
-
/**
|
|
47
|
-
* Rule-based fallback: produce a single short sentence from the first
|
|
48
|
-
* meaningful line, appending any key entities found in the text.
|
|
49
|
-
*/
|
|
50
198
|
function ruleFallback(text: string): string {
|
|
51
199
|
const lines = text.split("\n").filter((l) => l.trim().length > 10);
|
|
52
200
|
const first = (lines[0] ?? text).trim();
|
|
@@ -1,6 +1,80 @@
|
|
|
1
1
|
import type { SummarizerConfig, Logger } from "../../types";
|
|
2
2
|
|
|
3
|
-
const SYSTEM_PROMPT = `Summarize the text in ONE concise sentence (max
|
|
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 summarizeTaskOpenAI(
|
|
43
|
+
text: string,
|
|
44
|
+
cfg: SummarizerConfig,
|
|
45
|
+
log: Logger,
|
|
46
|
+
): Promise<string> {
|
|
47
|
+
const endpoint = normalizeChatEndpoint(cfg.endpoint ?? "https://api.openai.com/v1/chat/completions");
|
|
48
|
+
const model = cfg.model ?? "gpt-4o-mini";
|
|
49
|
+
const headers: Record<string, string> = {
|
|
50
|
+
"Content-Type": "application/json",
|
|
51
|
+
Authorization: `Bearer ${cfg.apiKey}`,
|
|
52
|
+
...cfg.headers,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const resp = await fetch(endpoint, {
|
|
56
|
+
method: "POST",
|
|
57
|
+
headers,
|
|
58
|
+
body: JSON.stringify({
|
|
59
|
+
model,
|
|
60
|
+
temperature: cfg.temperature ?? 0.1,
|
|
61
|
+
max_tokens: 4096,
|
|
62
|
+
messages: [
|
|
63
|
+
{ role: "system", content: TASK_SUMMARY_PROMPT },
|
|
64
|
+
{ role: "user", content: text },
|
|
65
|
+
],
|
|
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(`OpenAI task-summarize failed (${resp.status}): ${body}`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const json = (await resp.json()) as { choices: Array<{ message: { content: string } }> };
|
|
76
|
+
return json.choices[0]?.message?.content?.trim() ?? "";
|
|
77
|
+
}
|
|
4
78
|
|
|
5
79
|
export async function summarizeOpenAI(
|
|
6
80
|
text: string,
|
|
@@ -40,6 +114,235 @@ export async function summarizeOpenAI(
|
|
|
40
114
|
return json.choices[0]?.message?.content?.trim() ?? "";
|
|
41
115
|
}
|
|
42
116
|
|
|
117
|
+
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.
|
|
118
|
+
|
|
119
|
+
Answer ONLY "NEW" or "SAME".
|
|
120
|
+
|
|
121
|
+
Rules:
|
|
122
|
+
- "NEW" = the new message is about a completely different subject, project, or task
|
|
123
|
+
- "SAME" = the new message continues, follows up on, or is closely related to the current topic
|
|
124
|
+
- Follow-up questions, clarifications, refinements, bug fixes, or next steps on the same task = SAME
|
|
125
|
+
- Greetings or meta-questions like "你好" or "谢谢" without new substance = SAME
|
|
126
|
+
- A clearly unrelated request (e.g., current topic is deployment, new message asks about cooking) = NEW
|
|
127
|
+
|
|
128
|
+
Output exactly one word: NEW or SAME`;
|
|
129
|
+
|
|
130
|
+
export async function judgeNewTopicOpenAI(
|
|
131
|
+
currentContext: string,
|
|
132
|
+
newMessage: string,
|
|
133
|
+
cfg: SummarizerConfig,
|
|
134
|
+
log: Logger,
|
|
135
|
+
): Promise<boolean> {
|
|
136
|
+
const endpoint = normalizeChatEndpoint(cfg.endpoint ?? "https://api.openai.com/v1/chat/completions");
|
|
137
|
+
const model = cfg.model ?? "gpt-4o-mini";
|
|
138
|
+
const headers: Record<string, string> = {
|
|
139
|
+
"Content-Type": "application/json",
|
|
140
|
+
Authorization: `Bearer ${cfg.apiKey}`,
|
|
141
|
+
...cfg.headers,
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const userContent = `CURRENT CONVERSATION SUMMARY:\n${currentContext}\n\nNEW USER MESSAGE:\n${newMessage}`;
|
|
145
|
+
|
|
146
|
+
const resp = await fetch(endpoint, {
|
|
147
|
+
method: "POST",
|
|
148
|
+
headers,
|
|
149
|
+
body: JSON.stringify({
|
|
150
|
+
model,
|
|
151
|
+
temperature: 0,
|
|
152
|
+
max_tokens: 10,
|
|
153
|
+
messages: [
|
|
154
|
+
{ role: "system", content: TOPIC_JUDGE_PROMPT },
|
|
155
|
+
{ role: "user", content: userContent },
|
|
156
|
+
],
|
|
157
|
+
}),
|
|
158
|
+
signal: AbortSignal.timeout(cfg.timeoutMs ?? 15_000),
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
if (!resp.ok) {
|
|
162
|
+
const body = await resp.text();
|
|
163
|
+
throw new Error(`OpenAI topic-judge failed (${resp.status}): ${body}`);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const json = (await resp.json()) as { choices: Array<{ message: { content: string } }> };
|
|
167
|
+
const answer = json.choices[0]?.message?.content?.trim().toUpperCase() ?? "";
|
|
168
|
+
log.debug(`Topic judge result: "${answer}"`);
|
|
169
|
+
return answer.startsWith("NEW");
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
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:
|
|
173
|
+
|
|
174
|
+
1. Select ALL candidates that could be useful for answering the query. When in doubt, INCLUDE the candidate.
|
|
175
|
+
- 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.
|
|
176
|
+
- For factual lookups (e.g. "what is the SSH port"), a single direct answer is enough.
|
|
177
|
+
2. Judge whether the selected memories are SUFFICIENT to fully answer the query WITHOUT fetching additional context.
|
|
178
|
+
|
|
179
|
+
IMPORTANT for "sufficient" judgment:
|
|
180
|
+
- sufficient=true ONLY when the memories contain a concrete ANSWER, fact, decision, or actionable information that directly addresses the query.
|
|
181
|
+
- sufficient=false when:
|
|
182
|
+
- The memories only repeat the same question the user asked before (echo, not answer).
|
|
183
|
+
- The memories show related topics but lack the specific detail needed.
|
|
184
|
+
- The memories contain partial information that would benefit from full task context, timeline, or related skills.
|
|
185
|
+
|
|
186
|
+
Output a JSON object with exactly two fields:
|
|
187
|
+
{"relevant":[1,3,5],"sufficient":true}
|
|
188
|
+
|
|
189
|
+
- "relevant": array of candidate numbers that are useful. Empty array [] if none are relevant.
|
|
190
|
+
- "sufficient": true ONLY if the memories contain a direct answer; false otherwise.
|
|
191
|
+
|
|
192
|
+
Output ONLY the JSON object, nothing else.`;
|
|
193
|
+
|
|
194
|
+
export interface FilterResult {
|
|
195
|
+
relevant: number[];
|
|
196
|
+
sufficient: boolean;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export async function filterRelevantOpenAI(
|
|
200
|
+
query: string,
|
|
201
|
+
candidates: Array<{ index: number; summary: string; role: string }>,
|
|
202
|
+
cfg: SummarizerConfig,
|
|
203
|
+
log: Logger,
|
|
204
|
+
): Promise<FilterResult> {
|
|
205
|
+
const endpoint = normalizeChatEndpoint(cfg.endpoint ?? "https://api.openai.com/v1/chat/completions");
|
|
206
|
+
const model = cfg.model ?? "gpt-4o-mini";
|
|
207
|
+
const headers: Record<string, string> = {
|
|
208
|
+
"Content-Type": "application/json",
|
|
209
|
+
Authorization: `Bearer ${cfg.apiKey}`,
|
|
210
|
+
...cfg.headers,
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const candidateText = candidates
|
|
214
|
+
.map((c) => `${c.index}. [${c.role}] ${c.summary}`)
|
|
215
|
+
.join("\n");
|
|
216
|
+
|
|
217
|
+
const resp = await fetch(endpoint, {
|
|
218
|
+
method: "POST",
|
|
219
|
+
headers,
|
|
220
|
+
body: JSON.stringify({
|
|
221
|
+
model,
|
|
222
|
+
temperature: 0,
|
|
223
|
+
max_tokens: 200,
|
|
224
|
+
messages: [
|
|
225
|
+
{ role: "system", content: FILTER_RELEVANT_PROMPT },
|
|
226
|
+
{ role: "user", content: `QUERY: ${query}\n\nCANDIDATES:\n${candidateText}` },
|
|
227
|
+
],
|
|
228
|
+
}),
|
|
229
|
+
signal: AbortSignal.timeout(cfg.timeoutMs ?? 15_000),
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
if (!resp.ok) {
|
|
233
|
+
const body = await resp.text();
|
|
234
|
+
throw new Error(`OpenAI filter-relevant failed (${resp.status}): ${body}`);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const json = (await resp.json()) as { choices: Array<{ message: { content: string } }> };
|
|
238
|
+
const raw = json.choices[0]?.message?.content?.trim() ?? "{}";
|
|
239
|
+
return parseFilterResult(raw, log);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function parseFilterResult(raw: string, log: Logger): FilterResult {
|
|
243
|
+
try {
|
|
244
|
+
const match = raw.match(/\{[\s\S]*\}/);
|
|
245
|
+
if (match) {
|
|
246
|
+
const obj = JSON.parse(match[0]);
|
|
247
|
+
if (obj && Array.isArray(obj.relevant)) {
|
|
248
|
+
return {
|
|
249
|
+
relevant: obj.relevant.filter((n: any) => typeof n === "number"),
|
|
250
|
+
sufficient: obj.sufficient === true,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
} catch {}
|
|
255
|
+
log.warn(`filterRelevant: failed to parse LLM output: "${raw}", fallback to all+insufficient`);
|
|
256
|
+
return { relevant: [], sufficient: false };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// ─── Smart Dedup: judge whether new memory is DUPLICATE / UPDATE / NEW ───
|
|
260
|
+
|
|
261
|
+
export const DEDUP_JUDGE_PROMPT = `You are a memory deduplication system. Given a NEW memory summary and several EXISTING memory summaries, determine the relationship.
|
|
262
|
+
|
|
263
|
+
For each EXISTING memory, the NEW memory is either:
|
|
264
|
+
- "DUPLICATE": NEW is fully covered by an EXISTING memory — no new information at all
|
|
265
|
+
- "UPDATE": NEW contains information that supplements or updates an EXISTING memory (new data, status change, additional detail)
|
|
266
|
+
- "NEW": NEW is a different topic/event despite surface similarity
|
|
267
|
+
|
|
268
|
+
Pick the BEST match among all candidates. If none match well, choose "NEW".
|
|
269
|
+
|
|
270
|
+
Output a single JSON object:
|
|
271
|
+
- If DUPLICATE: {"action":"DUPLICATE","targetIndex":2,"reason":"..."}
|
|
272
|
+
- If UPDATE: {"action":"UPDATE","targetIndex":3,"reason":"...","mergedSummary":"a combined summary preserving all info from both old and new, same language as input"}
|
|
273
|
+
- If NEW: {"action":"NEW","reason":"..."}
|
|
274
|
+
|
|
275
|
+
CRITICAL: mergedSummary must use the SAME language as the input. Output ONLY the JSON object.`;
|
|
276
|
+
|
|
277
|
+
export interface DedupResult {
|
|
278
|
+
action: "DUPLICATE" | "UPDATE" | "NEW";
|
|
279
|
+
targetIndex?: number;
|
|
280
|
+
reason: string;
|
|
281
|
+
mergedSummary?: string;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export async function judgeDedupOpenAI(
|
|
285
|
+
newSummary: string,
|
|
286
|
+
candidates: Array<{ index: number; summary: string; chunkId: string }>,
|
|
287
|
+
cfg: SummarizerConfig,
|
|
288
|
+
log: Logger,
|
|
289
|
+
): Promise<DedupResult> {
|
|
290
|
+
const endpoint = normalizeChatEndpoint(cfg.endpoint ?? "https://api.openai.com/v1/chat/completions");
|
|
291
|
+
const model = cfg.model ?? "gpt-4o-mini";
|
|
292
|
+
const headers: Record<string, string> = {
|
|
293
|
+
"Content-Type": "application/json",
|
|
294
|
+
Authorization: `Bearer ${cfg.apiKey}`,
|
|
295
|
+
...cfg.headers,
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
const candidateText = candidates
|
|
299
|
+
.map((c) => `${c.index}. ${c.summary}`)
|
|
300
|
+
.join("\n");
|
|
301
|
+
|
|
302
|
+
const resp = await fetch(endpoint, {
|
|
303
|
+
method: "POST",
|
|
304
|
+
headers,
|
|
305
|
+
body: JSON.stringify({
|
|
306
|
+
model,
|
|
307
|
+
temperature: 0,
|
|
308
|
+
max_tokens: 300,
|
|
309
|
+
messages: [
|
|
310
|
+
{ role: "system", content: DEDUP_JUDGE_PROMPT },
|
|
311
|
+
{ role: "user", content: `NEW MEMORY:\n${newSummary}\n\nEXISTING MEMORIES:\n${candidateText}` },
|
|
312
|
+
],
|
|
313
|
+
}),
|
|
314
|
+
signal: AbortSignal.timeout(cfg.timeoutMs ?? 15_000),
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
if (!resp.ok) {
|
|
318
|
+
const body = await resp.text();
|
|
319
|
+
throw new Error(`OpenAI dedup-judge failed (${resp.status}): ${body}`);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const json = (await resp.json()) as { choices: Array<{ message: { content: string } }> };
|
|
323
|
+
const raw = json.choices[0]?.message?.content?.trim() ?? "{}";
|
|
324
|
+
return parseDedupResult(raw, log);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
export function parseDedupResult(raw: string, log: Logger): DedupResult {
|
|
328
|
+
try {
|
|
329
|
+
const match = raw.match(/\{[\s\S]*\}/);
|
|
330
|
+
if (match) {
|
|
331
|
+
const obj = JSON.parse(match[0]);
|
|
332
|
+
if (obj && typeof obj.action === "string") {
|
|
333
|
+
return {
|
|
334
|
+
action: obj.action as DedupResult["action"],
|
|
335
|
+
targetIndex: typeof obj.targetIndex === "number" ? obj.targetIndex : undefined,
|
|
336
|
+
reason: obj.reason || "",
|
|
337
|
+
mergedSummary: obj.mergedSummary || undefined,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
} catch {}
|
|
342
|
+
log.warn(`judgeDedup: failed to parse LLM output: "${raw}", fallback to NEW`);
|
|
343
|
+
return { action: "NEW", reason: "parse_failed" };
|
|
344
|
+
}
|
|
345
|
+
|
|
43
346
|
function normalizeChatEndpoint(url: string): string {
|
|
44
347
|
const stripped = url.replace(/\/+$/, "");
|
|
45
348
|
if (stripped.endsWith("/chat/completions")) return stripped;
|