@emqo/claudebridge 0.1.5 → 0.1.7
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/dist/core/agent.js +16 -4
- package/dist/core/intent.js +33 -9
- package/dist/core/store.d.ts +1 -1
- package/dist/core/store.js +5 -0
- package/package.json +1 -1
package/dist/core/agent.js
CHANGED
|
@@ -197,6 +197,7 @@ export class AgentEngine {
|
|
|
197
197
|
const child = spawn("claude", args, { env, stdio: ["pipe", "pipe", "pipe"] });
|
|
198
198
|
child.stdin.end();
|
|
199
199
|
let result = "";
|
|
200
|
+
let cost = 0;
|
|
200
201
|
let buffer = "";
|
|
201
202
|
child.stdout.on("data", (data) => {
|
|
202
203
|
buffer += data.toString();
|
|
@@ -207,17 +208,28 @@ export class AgentEngine {
|
|
|
207
208
|
continue;
|
|
208
209
|
try {
|
|
209
210
|
const msg = JSON.parse(line);
|
|
210
|
-
if (msg.type === "result"
|
|
211
|
-
|
|
211
|
+
if (msg.type === "result") {
|
|
212
|
+
if (msg.result)
|
|
213
|
+
result = msg.result;
|
|
214
|
+
if (msg.total_cost_usd)
|
|
215
|
+
cost = msg.total_cost_usd;
|
|
216
|
+
}
|
|
212
217
|
}
|
|
213
218
|
catch { }
|
|
214
219
|
}
|
|
215
220
|
});
|
|
216
221
|
child.on("close", () => {
|
|
222
|
+
if (cost > 0) {
|
|
223
|
+
this.store.recordUsage(userId, "auto-summary", cost);
|
|
224
|
+
console.log(`[agent] auto-summary cost=$${cost.toFixed(4)} for ${userId}`);
|
|
225
|
+
}
|
|
217
226
|
if (result && !result.includes("NONE")) {
|
|
218
|
-
this.store.addMemory(userId, result.trim(), "auto");
|
|
227
|
+
const saved = this.store.addMemory(userId, result.trim(), "auto");
|
|
219
228
|
this.store.trimMemories(userId, this.config.agent.memory?.max_memories || 50);
|
|
220
|
-
|
|
229
|
+
if (saved)
|
|
230
|
+
console.log(`[agent] auto-summary saved for ${userId}`);
|
|
231
|
+
else
|
|
232
|
+
console.log(`[agent] auto-summary skipped (duplicate) for ${userId}`);
|
|
221
233
|
}
|
|
222
234
|
});
|
|
223
235
|
}
|
package/dist/core/intent.js
CHANGED
|
@@ -11,6 +11,18 @@ const patterns = [
|
|
|
11
11
|
[/^(?:新会话|新对话|new\s*session|clear\s*session)/i,
|
|
12
12
|
() => ({ type: "clear_session" })],
|
|
13
13
|
];
|
|
14
|
+
/** Loose keywords that hint the message *might* be an intent — worth a Claude check */
|
|
15
|
+
const hintPatterns = [
|
|
16
|
+
/提醒|醒我|remind/i,
|
|
17
|
+
/任务|待办|todo|task/i,
|
|
18
|
+
/记住|记下|记得|remember/i,
|
|
19
|
+
/忘记|忘掉|forget/i,
|
|
20
|
+
/新会话|新对话|new\s*session|clear\s*session/i,
|
|
21
|
+
/\d+\s*(?:分钟|min|m|小时|hour|h)\s*(?:后|later)/i,
|
|
22
|
+
];
|
|
23
|
+
function mightBeIntent(text) {
|
|
24
|
+
return hintPatterns.some(re => re.test(text));
|
|
25
|
+
}
|
|
14
26
|
export function regexDetect(text) {
|
|
15
27
|
for (const [re, fn] of patterns) {
|
|
16
28
|
const m = text.match(re);
|
|
@@ -22,18 +34,28 @@ export function regexDetect(text) {
|
|
|
22
34
|
export function claudeDetect(text, rotator) {
|
|
23
35
|
return new Promise(resolve => {
|
|
24
36
|
const timeout = setTimeout(() => resolve({ type: "none" }), 15000);
|
|
25
|
-
const ep = rotator.
|
|
37
|
+
const ep = rotator.count
|
|
38
|
+
? rotator.next()
|
|
39
|
+
: { name: "cli-default", api_key: "", base_url: "", model: "" };
|
|
26
40
|
const env = { ...process.env };
|
|
27
|
-
|
|
41
|
+
if (ep.api_key)
|
|
42
|
+
env.ANTHROPIC_API_KEY = ep.api_key;
|
|
28
43
|
if (ep.base_url)
|
|
29
44
|
env.ANTHROPIC_BASE_URL = ep.base_url;
|
|
30
|
-
const prompt = `Classify the user
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
{"type":"
|
|
34
|
-
{"type":"
|
|
45
|
+
const prompt = `You are an intent classifier. Classify the user message into exactly ONE of these types. Output ONLY a single JSON object, nothing else.
|
|
46
|
+
|
|
47
|
+
Types:
|
|
48
|
+
- "reminder": user wants to be reminded later. Extract minutes and description. Example: {"type":"reminder","minutes":5,"description":"check server"}
|
|
49
|
+
- "task": user wants to add a task/todo. Extract description. Example: {"type":"task","description":"buy milk"}
|
|
50
|
+
- "memory": user wants you to remember something. Extract description. Example: {"type":"memory","description":"I like TypeScript"}
|
|
51
|
+
- "none": normal conversation, not an intent. Output: {"type":"none"}
|
|
35
52
|
|
|
36
|
-
|
|
53
|
+
Rules:
|
|
54
|
+
- Output ONLY one JSON object on a single line
|
|
55
|
+
- Do NOT output any explanation or extra text
|
|
56
|
+
- If unsure, output {"type":"none"}
|
|
57
|
+
|
|
58
|
+
User message: ${text.slice(0, 500)}`;
|
|
37
59
|
const args = ["-p", prompt, "--output-format", "stream-json", "--max-turns", "1", "--max-budget-usd", "0.005"];
|
|
38
60
|
if (ep.model)
|
|
39
61
|
args.push("--model", ep.model);
|
|
@@ -78,7 +100,9 @@ export async function detectIntent(text, rotator, config) {
|
|
|
78
100
|
const r = regexDetect(text);
|
|
79
101
|
if (r.type !== "none")
|
|
80
102
|
return r;
|
|
81
|
-
if (
|
|
103
|
+
// Only call Claude fallback if text contains hint keywords (saves cost)
|
|
104
|
+
if (config?.use_claude_fallback !== false && mightBeIntent(text)) {
|
|
82
105
|
return claudeDetect(text, rotator);
|
|
106
|
+
}
|
|
83
107
|
return { type: "none" };
|
|
84
108
|
}
|
package/dist/core/store.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ export declare class Store {
|
|
|
20
20
|
content: string;
|
|
21
21
|
created_at: number;
|
|
22
22
|
}[];
|
|
23
|
-
addMemory(userId: string, content: string, source?: string):
|
|
23
|
+
addMemory(userId: string, content: string, source?: string): boolean;
|
|
24
24
|
getMemories(userId: string): {
|
|
25
25
|
id: number;
|
|
26
26
|
content: string;
|
package/dist/core/store.js
CHANGED
|
@@ -101,7 +101,12 @@ export class Store {
|
|
|
101
101
|
}
|
|
102
102
|
// --- memories ---
|
|
103
103
|
addMemory(userId, content, source = "manual") {
|
|
104
|
+
// Dedup: skip if identical content already exists for this user
|
|
105
|
+
const existing = this.db.prepare("SELECT id FROM memories WHERE user_id = ? AND content = ? LIMIT 1").get(userId, content);
|
|
106
|
+
if (existing)
|
|
107
|
+
return false;
|
|
104
108
|
this.db.prepare("INSERT INTO memories (user_id, content, source, created_at) VALUES (?, ?, ?, ?)").run(userId, content, source, Date.now());
|
|
109
|
+
return true;
|
|
105
110
|
}
|
|
106
111
|
getMemories(userId) {
|
|
107
112
|
return this.db.prepare("SELECT id, content, source, created_at FROM memories WHERE user_id = ? ORDER BY created_at DESC").all(userId);
|