cf-claw 3.0.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.
- package/dist/agent.d.ts +15 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +262 -0
- package/dist/agent.js.map +1 -0
- package/dist/agents.d.ts +51 -0
- package/dist/agents.d.ts.map +1 -0
- package/dist/agents.js +478 -0
- package/dist/agents.js.map +1 -0
- package/dist/api/routes.d.ts +3 -0
- package/dist/api/routes.d.ts.map +1 -0
- package/dist/api/routes.js +491 -0
- package/dist/api/routes.js.map +1 -0
- package/dist/bot.d.ts +4 -0
- package/dist/bot.d.ts.map +1 -0
- package/dist/bot.js +295 -0
- package/dist/bot.js.map +1 -0
- package/dist/canvas.d.ts +37 -0
- package/dist/canvas.d.ts.map +1 -0
- package/dist/canvas.js +47 -0
- package/dist/canvas.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +202 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands.d.ts +6 -0
- package/dist/commands.d.ts.map +1 -0
- package/dist/commands.js +384 -0
- package/dist/commands.js.map +1 -0
- package/dist/components/TaskList.d.ts +7 -0
- package/dist/components/TaskList.d.ts.map +1 -0
- package/dist/components/TaskList.js +37 -0
- package/dist/components/TaskList.js.map +1 -0
- package/dist/config/encryption.d.ts +10 -0
- package/dist/config/encryption.d.ts.map +1 -0
- package/dist/config/encryption.js +111 -0
- package/dist/config/encryption.js.map +1 -0
- package/dist/config/json-config.d.ts +114 -0
- package/dist/config/json-config.d.ts.map +1 -0
- package/dist/config/json-config.js +388 -0
- package/dist/config/json-config.js.map +1 -0
- package/dist/config.d.ts +51 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +137 -0
- package/dist/config.js.map +1 -0
- package/dist/context/pruning.d.ts +30 -0
- package/dist/context/pruning.d.ts.map +1 -0
- package/dist/context/pruning.js +132 -0
- package/dist/context/pruning.js.map +1 -0
- package/dist/dashboard/404/index.html +1 -0
- package/dist/dashboard/404.html +1 -0
- package/dist/dashboard/_next/static/chunks/117-c657912d4a6fa056.js +2 -0
- package/dist/dashboard/_next/static/chunks/191-a6922264096cb3ad.js +11 -0
- package/dist/dashboard/_next/static/chunks/343-71498a8257bc1e81.js +1 -0
- package/dist/dashboard/_next/static/chunks/app/_not-found/page-15cbe4395e02a084.js +1 -0
- package/dist/dashboard/_next/static/chunks/app/layout-191efbc962809bb4.js +1 -0
- package/dist/dashboard/_next/static/chunks/app/manual/page-3c401ecf89979cd7.js +1 -0
- package/dist/dashboard/_next/static/chunks/app/page-dff55e58941a3c4d.js +1 -0
- package/dist/dashboard/_next/static/chunks/fd9d1056-9583fa19bc194043.js +1 -0
- package/dist/dashboard/_next/static/chunks/framework-f66176bb897dc684.js +1 -0
- package/dist/dashboard/_next/static/chunks/main-2461f93106bcf687.js +1 -0
- package/dist/dashboard/_next/static/chunks/main-app-89f5ec28b3bb0e7f.js +1 -0
- package/dist/dashboard/_next/static/chunks/pages/_app-72b849fbd24ac258.js +1 -0
- package/dist/dashboard/_next/static/chunks/pages/_error-7ba65e1336b92748.js +1 -0
- package/dist/dashboard/_next/static/chunks/polyfills-42372ed130431b0a.js +1 -0
- package/dist/dashboard/_next/static/chunks/webpack-616e068a201ad621.js +1 -0
- package/dist/dashboard/_next/static/css/baff0f221c10680b.css +3 -0
- package/dist/dashboard/_next/static/pyqPyo6dkz4uTWdfdFAOJ/_buildManifest.js +1 -0
- package/dist/dashboard/_next/static/pyqPyo6dkz4uTWdfdFAOJ/_ssgManifest.js +1 -0
- package/dist/dashboard/index.html +1 -0
- package/dist/dashboard/index.txt +7 -0
- package/dist/dashboard/manual/index.html +1 -0
- package/dist/dashboard/manual/index.txt +7 -0
- package/dist/documents.d.ts +20 -0
- package/dist/documents.d.ts.map +1 -0
- package/dist/documents.js +227 -0
- package/dist/documents.js.map +1 -0
- package/dist/embeddings.d.ts +15 -0
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +40 -0
- package/dist/embeddings.js.map +1 -0
- package/dist/factory.d.ts +72 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +2010 -0
- package/dist/factory.js.map +1 -0
- package/dist/groups.d.ts +13 -0
- package/dist/groups.d.ts.map +1 -0
- package/dist/groups.js +42 -0
- package/dist/groups.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +223 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/taskStorage.d.ts +4 -0
- package/dist/lib/taskStorage.d.ts.map +1 -0
- package/dist/lib/taskStorage.js +28 -0
- package/dist/lib/taskStorage.js.map +1 -0
- package/dist/llm/anthropic.d.ts +13 -0
- package/dist/llm/anthropic.d.ts.map +1 -0
- package/dist/llm/anthropic.js +96 -0
- package/dist/llm/anthropic.js.map +1 -0
- package/dist/llm/failover.d.ts +13 -0
- package/dist/llm/failover.d.ts.map +1 -0
- package/dist/llm/failover.js +42 -0
- package/dist/llm/failover.js.map +1 -0
- package/dist/llm/google.d.ts +13 -0
- package/dist/llm/google.d.ts.map +1 -0
- package/dist/llm/google.js +112 -0
- package/dist/llm/google.js.map +1 -0
- package/dist/llm/groq.d.ts +8 -0
- package/dist/llm/groq.d.ts.map +1 -0
- package/dist/llm/groq.js +13 -0
- package/dist/llm/groq.js.map +1 -0
- package/dist/llm/index.d.ts +11 -0
- package/dist/llm/index.d.ts.map +1 -0
- package/dist/llm/index.js +10 -0
- package/dist/llm/index.js.map +1 -0
- package/dist/llm/ollama.d.ts +9 -0
- package/dist/llm/ollama.d.ts.map +1 -0
- package/dist/llm/ollama.js +27 -0
- package/dist/llm/ollama.js.map +1 -0
- package/dist/llm/openai-compat.d.ts +17 -0
- package/dist/llm/openai-compat.d.ts.map +1 -0
- package/dist/llm/openai-compat.js +69 -0
- package/dist/llm/openai-compat.js.map +1 -0
- package/dist/llm/openrouter.d.ts +8 -0
- package/dist/llm/openrouter.d.ts.map +1 -0
- package/dist/llm/openrouter.js +20 -0
- package/dist/llm/openrouter.js.map +1 -0
- package/dist/llm/provider.d.ts +41 -0
- package/dist/llm/provider.d.ts.map +1 -0
- package/dist/llm/provider.js +2 -0
- package/dist/llm/provider.js.map +1 -0
- package/dist/llm/registry.d.ts +10 -0
- package/dist/llm/registry.d.ts.map +1 -0
- package/dist/llm/registry.js +90 -0
- package/dist/llm/registry.js.map +1 -0
- package/dist/llm/thinking.d.ts +7 -0
- package/dist/llm/thinking.d.ts.map +1 -0
- package/dist/llm/thinking.js +34 -0
- package/dist/llm/thinking.js.map +1 -0
- package/dist/llm.d.ts +17 -0
- package/dist/llm.d.ts.map +1 -0
- package/dist/llm.js +184 -0
- package/dist/llm.js.map +1 -0
- package/dist/logs.d.ts +11 -0
- package/dist/logs.d.ts.map +1 -0
- package/dist/logs.js +54 -0
- package/dist/logs.js.map +1 -0
- package/dist/memory/knowledge-graph.d.ts +34 -0
- package/dist/memory/knowledge-graph.d.ts.map +1 -0
- package/dist/memory/knowledge-graph.js +137 -0
- package/dist/memory/knowledge-graph.js.map +1 -0
- package/dist/memory.d.ts +73 -0
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +320 -0
- package/dist/memory.js.map +1 -0
- package/dist/mesh.d.ts +18 -0
- package/dist/mesh.d.ts.map +1 -0
- package/dist/mesh.js +120 -0
- package/dist/mesh.js.map +1 -0
- package/dist/paths.d.ts +11 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +51 -0
- package/dist/paths.js.map +1 -0
- package/dist/proactive/heartbeat.d.ts +4 -0
- package/dist/proactive/heartbeat.d.ts.map +1 -0
- package/dist/proactive/heartbeat.js +56 -0
- package/dist/proactive/heartbeat.js.map +1 -0
- package/dist/proactive/recap.d.ts +12 -0
- package/dist/proactive/recap.d.ts.map +1 -0
- package/dist/proactive/recap.js +90 -0
- package/dist/proactive/recap.js.map +1 -0
- package/dist/proactive/recommendations.d.ts +11 -0
- package/dist/proactive/recommendations.d.ts.map +1 -0
- package/dist/proactive/recommendations.js +92 -0
- package/dist/proactive/recommendations.js.map +1 -0
- package/dist/projects.d.ts +44 -0
- package/dist/projects.d.ts.map +1 -0
- package/dist/projects.js +101 -0
- package/dist/projects.js.map +1 -0
- package/dist/scheduler/index.d.ts +17 -0
- package/dist/scheduler/index.d.ts.map +1 -0
- package/dist/scheduler/index.js +116 -0
- package/dist/scheduler/index.js.map +1 -0
- package/dist/sessions.d.ts +19 -0
- package/dist/sessions.d.ts.map +1 -0
- package/dist/sessions.js +176 -0
- package/dist/sessions.js.map +1 -0
- package/dist/skills/index.d.ts +14 -0
- package/dist/skills/index.d.ts.map +1 -0
- package/dist/skills/index.js +126 -0
- package/dist/skills/index.js.map +1 -0
- package/dist/swarm.d.ts +18 -0
- package/dist/swarm.d.ts.map +1 -0
- package/dist/swarm.js +146 -0
- package/dist/swarm.js.map +1 -0
- package/dist/taskListWidget.d.ts +76 -0
- package/dist/taskListWidget.d.ts.map +1 -0
- package/dist/taskListWidget.js +312 -0
- package/dist/taskListWidget.js.map +1 -0
- package/dist/tools/browser.d.ts +5 -0
- package/dist/tools/browser.d.ts.map +1 -0
- package/dist/tools/browser.js +104 -0
- package/dist/tools/browser.js.map +1 -0
- package/dist/tools/config-tools.d.ts +4 -0
- package/dist/tools/config-tools.d.ts.map +1 -0
- package/dist/tools/config-tools.js +154 -0
- package/dist/tools/config-tools.js.map +1 -0
- package/dist/tools/files.d.ts +3 -0
- package/dist/tools/files.d.ts.map +1 -0
- package/dist/tools/files.js +208 -0
- package/dist/tools/files.js.map +1 -0
- package/dist/tools/index.d.ts +11 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +1109 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/mcp-bridge.d.ts +5 -0
- package/dist/tools/mcp-bridge.d.ts.map +1 -0
- package/dist/tools/mcp-bridge.js +200 -0
- package/dist/tools/mcp-bridge.js.map +1 -0
- package/dist/tools/shell.d.ts +8 -0
- package/dist/tools/shell.d.ts.map +1 -0
- package/dist/tools/shell.js +78 -0
- package/dist/tools/shell.js.map +1 -0
- package/dist/tools/skills-manage.d.ts +25 -0
- package/dist/tools/skills-manage.d.ts.map +1 -0
- package/dist/tools/skills-manage.js +155 -0
- package/dist/tools/skills-manage.js.map +1 -0
- package/dist/tools/web-search.d.ts +4 -0
- package/dist/tools/web-search.d.ts.map +1 -0
- package/dist/tools/web-search.js +60 -0
- package/dist/tools/web-search.js.map +1 -0
- package/dist/tts.d.ts +6 -0
- package/dist/tts.d.ts.map +1 -0
- package/dist/tts.js +42 -0
- package/dist/tts.js.map +1 -0
- package/dist/types/task.d.ts +7 -0
- package/dist/types/task.d.ts.map +1 -0
- package/dist/types/task.js +2 -0
- package/dist/types/task.js.map +1 -0
- package/dist/typing.d.ts +18 -0
- package/dist/typing.d.ts.map +1 -0
- package/dist/typing.js +63 -0
- package/dist/typing.js.map +1 -0
- package/dist/usage.d.ts +28 -0
- package/dist/usage.d.ts.map +1 -0
- package/dist/usage.js +69 -0
- package/dist/usage.js.map +1 -0
- package/dist/voice.d.ts +6 -0
- package/dist/voice.d.ts.map +1 -0
- package/dist/voice.js +47 -0
- package/dist/voice.js.map +1 -0
- package/dist/webchat/index.d.ts +3 -0
- package/dist/webchat/index.d.ts.map +1 -0
- package/dist/webchat/index.js +3 -0
- package/dist/webchat/index.js.map +1 -0
- package/dist/webchat/public/index.html +344 -0
- package/dist/webchat/public/public/index.html +344 -0
- package/dist/webchat/public/public/task-list-widget.js +410 -0
- package/dist/webchat/public/task-list-widget.js +410 -0
- package/dist/webchat/server.d.ts +3 -0
- package/dist/webchat/server.d.ts.map +1 -0
- package/dist/webchat/server.js +80 -0
- package/dist/webchat/server.js.map +1 -0
- package/dist/webchat/ws.d.ts +4 -0
- package/dist/webchat/ws.d.ts.map +1 -0
- package/dist/webchat/ws.js +232 -0
- package/dist/webchat/ws.js.map +1 -0
- package/dist/webhooks/index.d.ts +14 -0
- package/dist/webhooks/index.d.ts.map +1 -0
- package/dist/webhooks/index.js +86 -0
- package/dist/webhooks/index.js.map +1 -0
- package/package.json +53 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { config } from "../config.js";
|
|
2
|
+
import { FailoverError } from "./failover.js";
|
|
3
|
+
const providers = new Map();
|
|
4
|
+
let activeProviderName = null;
|
|
5
|
+
let activeModelName = null;
|
|
6
|
+
export function registerProvider(provider) {
|
|
7
|
+
for (const model of provider.models) {
|
|
8
|
+
providers.set(model, provider);
|
|
9
|
+
}
|
|
10
|
+
console.log(`📦 Registered provider "${provider.name}" with models: ${provider.models.join(", ")}`);
|
|
11
|
+
}
|
|
12
|
+
export function setActiveModel(model) {
|
|
13
|
+
const provider = providers.get(model);
|
|
14
|
+
if (!provider) {
|
|
15
|
+
const available = [...providers.keys()];
|
|
16
|
+
throw new Error(`Unknown model "${model}". Available: ${available.join(", ")}`);
|
|
17
|
+
}
|
|
18
|
+
activeProviderName = provider.name;
|
|
19
|
+
activeModelName = model;
|
|
20
|
+
console.log(`🔄 Active model set to: ${model} (via ${provider.name})`);
|
|
21
|
+
}
|
|
22
|
+
export function getActiveModel() {
|
|
23
|
+
return activeModelName || "unknown";
|
|
24
|
+
}
|
|
25
|
+
export function getActiveProviderName() {
|
|
26
|
+
return activeProviderName || "unknown";
|
|
27
|
+
}
|
|
28
|
+
export function getAvailableModels() {
|
|
29
|
+
return [...providers.keys()];
|
|
30
|
+
}
|
|
31
|
+
export async function chatCompletion(options) {
|
|
32
|
+
if (!activeModelName)
|
|
33
|
+
throw new Error("No active model set");
|
|
34
|
+
const provider = providers.get(activeModelName);
|
|
35
|
+
if (!provider)
|
|
36
|
+
throw new Error(`No provider for model ${activeModelName}`);
|
|
37
|
+
return provider.chatCompletion({ ...options, model: activeModelName });
|
|
38
|
+
}
|
|
39
|
+
export function getProviderForModel(model) {
|
|
40
|
+
return providers.get(model);
|
|
41
|
+
}
|
|
42
|
+
export async function chatCompletionWithFallback(primaryModel, fallbackModel, options) {
|
|
43
|
+
const attempts = [];
|
|
44
|
+
const modelsToTry = [];
|
|
45
|
+
if (primaryModel) {
|
|
46
|
+
modelsToTry.push(primaryModel);
|
|
47
|
+
}
|
|
48
|
+
else if (activeModelName) {
|
|
49
|
+
modelsToTry.push(activeModelName);
|
|
50
|
+
}
|
|
51
|
+
if (fallbackModel && !modelsToTry.includes(fallbackModel)) {
|
|
52
|
+
modelsToTry.push(fallbackModel);
|
|
53
|
+
}
|
|
54
|
+
if (config.failoverChain.length > 0) {
|
|
55
|
+
for (const m of config.failoverChain) {
|
|
56
|
+
if (!modelsToTry.includes(m)) {
|
|
57
|
+
modelsToTry.push(m);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const model of modelsToTry) {
|
|
62
|
+
try {
|
|
63
|
+
const provider = providers.get(model);
|
|
64
|
+
if (!provider) {
|
|
65
|
+
attempts.push({ model, error: new Error(`No provider for model "${model}"`) });
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const result = await provider.chatCompletion({ ...options, model });
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
73
|
+
attempts.push({ model, error });
|
|
74
|
+
console.warn(`⚠️ Model "${model}" failed: ${error.message}`);
|
|
75
|
+
const isRetryable = error.message.includes("429") ||
|
|
76
|
+
error.message.includes("rate") ||
|
|
77
|
+
error.message.includes("timeout") ||
|
|
78
|
+
error.message.includes("ECONNREFUSED") ||
|
|
79
|
+
error.message.includes("500") ||
|
|
80
|
+
error.message.includes("502") ||
|
|
81
|
+
error.message.includes("503") ||
|
|
82
|
+
error.message.includes("504");
|
|
83
|
+
if (!isRetryable && attempts.length === 1) {
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
throw new FailoverError(attempts);
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=registry.js.map
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CAAC,QAAQ,CAAC,KAAK,CAAC;gBAC7B,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC;gBAC7B,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC;gBAC7B,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;YAEhC,IAAI,CAAC,WAAW,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC1C,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;IACH,CAAC;IAED,MAAM,IAAI,aAAa,CAAC,QAAQ,CAAC,CAAC;AACpC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type ThinkingLevel = "off" | "low" | "medium" | "high";
|
|
2
|
+
export declare function setThinkingLevel(level: ThinkingLevel): void;
|
|
3
|
+
export declare function getThinkingLevel(): ThinkingLevel;
|
|
4
|
+
export declare function getThinkingPrompt(): string;
|
|
5
|
+
export declare function getExtendedThinkingBudget(): number | null;
|
|
6
|
+
export declare function cycleThinkingLevel(): ThinkingLevel;
|
|
7
|
+
//# sourceMappingURL=thinking.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thinking.d.ts","sourceRoot":"","sources":["../../src/llm/thinking.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;AAI9D,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,aAAa,GAAG,IAAI,CAE3D;AAED,wBAAgB,gBAAgB,IAAI,aAAa,CAEhD;AAED,wBAAgB,iBAAiB,IAAI,MAAM,CAW1C;AAED,wBAAgB,yBAAyB,IAAI,MAAM,GAAG,IAAI,CAOzD;AAED,wBAAgB,kBAAkB,IAAI,aAAa,CAKlD"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
let currentLevel = "medium";
|
|
2
|
+
export function setThinkingLevel(level) {
|
|
3
|
+
currentLevel = level;
|
|
4
|
+
}
|
|
5
|
+
export function getThinkingLevel() {
|
|
6
|
+
return currentLevel;
|
|
7
|
+
}
|
|
8
|
+
export function getThinkingPrompt() {
|
|
9
|
+
switch (currentLevel) {
|
|
10
|
+
case "off":
|
|
11
|
+
return "";
|
|
12
|
+
case "low":
|
|
13
|
+
return "\n\n## Thinking Mode: Brief\nThink briefly before responding. Keep internal reasoning short — 1-2 sentences max.";
|
|
14
|
+
case "medium":
|
|
15
|
+
return "\n\n## Thinking Mode: Moderate\nThink step-by-step before responding. Show your reasoning process clearly but concisely.";
|
|
16
|
+
case "high":
|
|
17
|
+
return "\n\n## Thinking Mode: Deep\nThink deeply and thoroughly before responding. Analyze the problem from multiple angles. Consider edge cases. Show complete reasoning chain. Take your time to arrive at the best answer.";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function getExtendedThinkingBudget() {
|
|
21
|
+
switch (currentLevel) {
|
|
22
|
+
case "off": return null;
|
|
23
|
+
case "low": return 1024;
|
|
24
|
+
case "medium": return 4096;
|
|
25
|
+
case "high": return 16384;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function cycleThinkingLevel() {
|
|
29
|
+
const levels = ["off", "low", "medium", "high"];
|
|
30
|
+
const idx = levels.indexOf(currentLevel);
|
|
31
|
+
currentLevel = levels[(idx + 1) % levels.length];
|
|
32
|
+
return currentLevel;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=thinking.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thinking.js","sourceRoot":"","sources":["../../src/llm/thinking.ts"],"names":[],"mappings":"AAEA,IAAI,YAAY,GAAkB,QAAQ,CAAC;AAE3C,MAAM,UAAU,gBAAgB,CAAC,KAAoB;IACnD,YAAY,GAAG,KAAK,CAAC;AACvB,CAAC;AAED,MAAM,UAAU,gBAAgB;IAC9B,OAAO,YAAY,CAAC;AACtB,CAAC;AAED,MAAM,UAAU,iBAAiB;IAC/B,QAAQ,YAAY,EAAE,CAAC;QACrB,KAAK,KAAK;YACR,OAAO,EAAE,CAAC;QACZ,KAAK,KAAK;YACR,OAAO,kHAAkH,CAAC;QAC5H,KAAK,QAAQ;YACX,OAAO,0HAA0H,CAAC;QACpI,KAAK,MAAM;YACT,OAAO,uNAAuN,CAAC;IACnO,CAAC;AACH,CAAC;AAED,MAAM,UAAU,yBAAyB;IACvC,QAAQ,YAAY,EAAE,CAAC;QACrB,KAAK,KAAK,CAAC,CAAC,OAAO,IAAI,CAAC;QACxB,KAAK,KAAK,CAAC,CAAC,OAAO,IAAI,CAAC;QACxB,KAAK,QAAQ,CAAC,CAAC,OAAO,IAAI,CAAC;QAC3B,KAAK,MAAM,CAAC,CAAC,OAAO,KAAK,CAAC;IAC5B,CAAC;AACH,CAAC;AAED,MAAM,UAAU,kBAAkB;IAChC,MAAM,MAAM,GAAoB,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;IACjE,MAAM,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IACzC,YAAY,GAAG,MAAM,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;IACjD,OAAO,YAAY,CAAC;AACtB,CAAC"}
|
package/dist/llm.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ConversationMessage } from "./llm/provider.js";
|
|
2
|
+
import type { Tool } from "./tools/index.js";
|
|
3
|
+
export type { ConversationMessage };
|
|
4
|
+
export declare function chatCompletion(messages: ConversationMessage[], tools: Tool[]): Promise<{
|
|
5
|
+
content: string | null;
|
|
6
|
+
toolCalls?: {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
arguments: string;
|
|
10
|
+
}[];
|
|
11
|
+
}>;
|
|
12
|
+
export declare function factExtraction(userMessage: string, assistantReply: string): Promise<{
|
|
13
|
+
content: string;
|
|
14
|
+
category: string;
|
|
15
|
+
}[]>;
|
|
16
|
+
export declare function summarizeConversation(transcript: string): Promise<string | null>;
|
|
17
|
+
//# sourceMappingURL=llm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm.d.ts","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,mBAAmB,EAAkB,MAAM,mBAAmB,CAAC;AAE7E,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AA2G7C,YAAY,EAAE,mBAAmB,EAAE,CAAC;AAUpC,wBAAsB,cAAc,CAClC,QAAQ,EAAE,mBAAmB,EAAE,EAC/B,KAAK,EAAE,IAAI,EAAE,GACZ,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,SAAS,CAAC,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;CAAE,CAAC,CAyBpG;AAeD,wBAAsB,cAAc,CAClC,WAAW,EAAE,MAAM,EACnB,cAAc,EAAE,MAAM,GACrB,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,EAAE,CAAC,CAkBlD;AAWD,wBAAsB,qBAAqB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAYtF"}
|
package/dist/llm.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { chatCompletion as registryChatCompletion } from "./llm/index.js";
|
|
2
|
+
import { chatCompletionWithFailover } from "./llm/failover.js";
|
|
3
|
+
import { getThinkingPrompt } from "./llm/thinking.js";
|
|
4
|
+
import { config } from "./config.js";
|
|
5
|
+
const SYSTEM_PROMPT = `You are CF Claw, a personal AI assistant running on Telegram.
|
|
6
|
+
You are helpful, concise, and direct. Keep responses conversational and to the point.
|
|
7
|
+
|
|
8
|
+
## Self-Improvement
|
|
9
|
+
You are a self-improving agent. Every conversation makes you slightly better. You proactively learn from experience and save knowledge for future sessions.
|
|
10
|
+
|
|
11
|
+
### Memory Guidance
|
|
12
|
+
You have persistent memory across sessions. Save durable facts proactively — don't wait to be asked:
|
|
13
|
+
- When the user corrects you or says "remember this" / "don't do that again"
|
|
14
|
+
- When the user shares a preference, habit, or personal detail
|
|
15
|
+
- When you discover tool quirks, environment details, or stable conventions
|
|
16
|
+
- When you learn something about the user's workflow or projects
|
|
17
|
+
Use the **remember** tool to save facts. Most facts are auto-extracted, but use remember for anything the auto-extraction might miss.
|
|
18
|
+
|
|
19
|
+
### Skills Guidance
|
|
20
|
+
You can create and improve skills — reusable procedures saved for future use:
|
|
21
|
+
- After completing a **complex task** (5+ tool calls), fixing a **tricky error**, or discovering a **non-trivial workflow**, save the approach as a skill with **skill_create**
|
|
22
|
+
- When using a skill and finding it **outdated, incomplete, or wrong**, patch it immediately with **skill_patch** — don't wait to be asked
|
|
23
|
+
- Skills that aren't maintained become liabilities. Keep them accurate.
|
|
24
|
+
- Use **skill_view** to load a skill's full instructions when you need to follow a procedure
|
|
25
|
+
- Use **skill_list** to see what skills are available
|
|
26
|
+
|
|
27
|
+
### Self-Improve Principles
|
|
28
|
+
- Every complex task is a learning opportunity
|
|
29
|
+
- Patterns you discover should be saved as skills
|
|
30
|
+
- User corrections are the most valuable training signal — always save them
|
|
31
|
+
- When you solve a problem creatively, capture the approach
|
|
32
|
+
- Review and update your skills when you find better approaches
|
|
33
|
+
|
|
34
|
+
## Memory System
|
|
35
|
+
You have a sophisticated 3-tier memory system:
|
|
36
|
+
|
|
37
|
+
1. **Core Facts** — Your persistent knowledge about the user is automatically loaded into context. You don't need to search for these.
|
|
38
|
+
2. **Summaries** — Past conversation summaries are loaded for broad context.
|
|
39
|
+
3. **Semantic Search** — Relevant past conversations and facts are automatically retrieved based on what the user says.
|
|
40
|
+
|
|
41
|
+
### Memory Tools
|
|
42
|
+
- **remember**: Manually save a specific fact if the auto-extraction might miss it. Use sparingly — most facts are auto-extracted.
|
|
43
|
+
- **recall**: Search past conversations and facts when you need specific details. Uses hybrid keyword + semantic search.
|
|
44
|
+
- **forget**: Delete an outdated or incorrect fact. Use recall first to find the ID.
|
|
45
|
+
- **search_conversations**: Deep search through full conversation history.
|
|
46
|
+
|
|
47
|
+
Your memory auto-extracts important facts after each conversation turn. You should feel like you never forget anything.
|
|
48
|
+
|
|
49
|
+
## Knowledge Graph
|
|
50
|
+
You have access to a knowledge graph for tracking entities and their relationships.
|
|
51
|
+
- **knowledge_add**: Add entities and relationships to the knowledge graph.
|
|
52
|
+
- **knowledge_query**: Search and traverse the knowledge graph.
|
|
53
|
+
|
|
54
|
+
## Agent Sessions (A2A)
|
|
55
|
+
You can spawn isolated sub-agent sessions for parallel tasks:
|
|
56
|
+
- **sessions_create**: Create a new session with optional system prompt.
|
|
57
|
+
- **sessions_list**: List all active sessions.
|
|
58
|
+
- **sessions_history**: Get conversation history of a session.
|
|
59
|
+
- **sessions_send**: Send a message to a session and get its response.
|
|
60
|
+
- **sessions_delete**: Delete a session.
|
|
61
|
+
|
|
62
|
+
## Live Canvas
|
|
63
|
+
You can push interactive widgets to the WebChat UI:
|
|
64
|
+
- **canvas_render**: Push HTML/JS widgets, tables, charts, or forms.
|
|
65
|
+
|
|
66
|
+
## Document Store
|
|
67
|
+
You can save, read, search, and manage documents:
|
|
68
|
+
- **doc_save**: Save a document (markdown or HTML) with title, category, and tags.
|
|
69
|
+
- **doc_list**: List documents, optionally filtered by category.
|
|
70
|
+
- **doc_read**: Read a document's content by ID.
|
|
71
|
+
- **doc_delete**: Delete a document by ID.
|
|
72
|
+
- **doc_search**: Search documents by semantic similarity and keywords.
|
|
73
|
+
|
|
74
|
+
## Agent Registry
|
|
75
|
+
There are specialized agents available for different tasks. You can list, inspect, and create agents:
|
|
76
|
+
- **agent_list**: List all agents or filter by team.
|
|
77
|
+
- **agent_show**: Show details of a specific agent.
|
|
78
|
+
- **agent_create**: Create a new custom agent with role, system prompt, tools, and rules.
|
|
79
|
+
- **agent_delete**: Delete a custom agent (built-in agents cannot be deleted).
|
|
80
|
+
|
|
81
|
+
Teams: content, strategy, design, development, quality, operations, support.
|
|
82
|
+
|
|
83
|
+
## Autonomy Principles
|
|
84
|
+
You are empowered to make decisions and take action autonomously. Follow these guidelines:
|
|
85
|
+
|
|
86
|
+
### Proactive Action
|
|
87
|
+
- **Take action without asking for confirmation** for routine tasks (file operations, git commands, running scripts, npm commands, etc.)
|
|
88
|
+
- When the user's intent is clear, execute it directly without unnecessary back-and-forth
|
|
89
|
+
- Only ask for confirmation when the user explicitly asks "should I..." or the operation is truly destructive
|
|
90
|
+
|
|
91
|
+
### Decision Making
|
|
92
|
+
- **Make decisions autonomously** for standard operations within your allowed tools
|
|
93
|
+
- If something is safe and obviously needed to complete the task, just do it
|
|
94
|
+
- When you encounter fixable errors, fix them and continue rather than stopping to ask
|
|
95
|
+
|
|
96
|
+
### Escalation
|
|
97
|
+
Only escalate to the user for:
|
|
98
|
+
- Truly consequential decisions (major system changes, deleting important data)
|
|
99
|
+
- Operations that are irreversible and significant
|
|
100
|
+
- When you genuinely don't understand what the user wants
|
|
101
|
+
|
|
102
|
+
### Shell Commands
|
|
103
|
+
The shell has a pre-approved allowlist. Commands in the allowlist execute automatically without asking:
|
|
104
|
+
- ls, cat, head, tail, pwd, date, echo, whoami, git, npm, mkdir, cd, and more
|
|
105
|
+
- Unknown commands require explicit confirmation (unless YOLO mode is enabled)
|
|
106
|
+
- When confirmation is needed and the user approves, the command runs automatically
|
|
107
|
+
|
|
108
|
+
Be decisive. When you know what to do, do it.`;
|
|
109
|
+
function toolsToDefinitions(tools) {
|
|
110
|
+
return tools.map((t) => ({
|
|
111
|
+
name: t.name,
|
|
112
|
+
description: t.description,
|
|
113
|
+
parameters: t.input_schema,
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
export async function chatCompletion(messages, tools) {
|
|
117
|
+
const thinkingPrompt = getThinkingPrompt();
|
|
118
|
+
const systemContent = SYSTEM_PROMPT + thinkingPrompt;
|
|
119
|
+
const fullMessages = [
|
|
120
|
+
{ role: "system", content: systemContent },
|
|
121
|
+
...messages,
|
|
122
|
+
];
|
|
123
|
+
const toolDefs = toolsToDefinitions(tools);
|
|
124
|
+
if (config.failoverChain.length > 0) {
|
|
125
|
+
const result = await chatCompletionWithFailover({ messages: fullMessages, tools: toolDefs }, config.failoverChain);
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
const result = await registryChatCompletion({
|
|
129
|
+
messages: fullMessages,
|
|
130
|
+
tools: toolDefs,
|
|
131
|
+
});
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
const FACT_EXTRACTION_PROMPT = `Analyze this conversation turn and extract any important facts worth remembering long-term.
|
|
135
|
+
Return a JSON array of facts. Each fact should have:
|
|
136
|
+
- "content": a clear, self-contained statement
|
|
137
|
+
- "category": one of "preference", "personal", "decision", "project", "technical", "relationship", "general"
|
|
138
|
+
|
|
139
|
+
Only extract genuinely useful, non-obvious facts. If nothing worth remembering, return an empty array [].
|
|
140
|
+
Do NOT extract greetings, small talk, or transient information.
|
|
141
|
+
|
|
142
|
+
User said: {user_message}
|
|
143
|
+
Assistant replied: {assistant_reply}
|
|
144
|
+
|
|
145
|
+
Return ONLY the JSON array, nothing else.`;
|
|
146
|
+
export async function factExtraction(userMessage, assistantReply) {
|
|
147
|
+
try {
|
|
148
|
+
const prompt = FACT_EXTRACTION_PROMPT
|
|
149
|
+
.replace("{user_message}", userMessage)
|
|
150
|
+
.replace("{assistant_reply}", assistantReply);
|
|
151
|
+
const result = await registryChatCompletion({
|
|
152
|
+
messages: [{ role: "user", content: prompt }],
|
|
153
|
+
});
|
|
154
|
+
const text = result.content?.trim() || "[]";
|
|
155
|
+
const jsonMatch = text.match(/\[[\s\S]*\]/);
|
|
156
|
+
if (!jsonMatch)
|
|
157
|
+
return [];
|
|
158
|
+
return JSON.parse(jsonMatch[0]);
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return [];
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const SUMMARIZE_PROMPT = `Summarize this conversation segment into a concise paragraph (2-4 sentences).
|
|
165
|
+
Capture the key topics discussed, decisions made, and any important context.
|
|
166
|
+
Do NOT include greetings or filler. Focus on substance.
|
|
167
|
+
|
|
168
|
+
Conversation:
|
|
169
|
+
{transcript}
|
|
170
|
+
|
|
171
|
+
Return ONLY the summary paragraph, nothing else.`;
|
|
172
|
+
export async function summarizeConversation(transcript) {
|
|
173
|
+
try {
|
|
174
|
+
const prompt = SUMMARIZE_PROMPT.replace("{transcript}", transcript);
|
|
175
|
+
const result = await registryChatCompletion({
|
|
176
|
+
messages: [{ role: "user", content: prompt }],
|
|
177
|
+
});
|
|
178
|
+
return result.content?.trim() || null;
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
//# sourceMappingURL=llm.js.map
|
package/dist/llm.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm.js","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,IAAI,sBAAsB,EAAkB,MAAM,gBAAgB,CAAC;AAC1F,OAAO,EAAE,0BAA0B,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,MAAM,aAAa,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;8CAuGwB,CAAC;AAI/C,SAAS,kBAAkB,CAAC,KAAa;IACvC,OAAO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AAC;YAC1C,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;SAC9C,CAAC,CAAC;QAEH,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,IAAI,CAAC;QAC5C,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC;QAC5C,IAAI,CAAC,SAAS;YAAE,OAAO,EAAE,CAAC;QAE1B,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;IAClC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC;AAED,MAAM,gBAAgB,GAAG;;;;;;;iDAOwB,CAAC;AAElD,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,UAAkB;IAC5D,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,gBAAgB,CAAC,OAAO,CAAC,cAAc,EAAE,UAAU,CAAC,CAAC;QAEpE,MAAM,MAAM,GAAG,MAAM,sBAAsB,CAAC;YAC1C,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;SAC9C,CAAC,CAAC;QAEH,OAAO,MAAM,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,IAAI,CAAC;IACxC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
package/dist/logs.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type LogEntry = {
|
|
2
|
+
id: number;
|
|
3
|
+
time: string;
|
|
4
|
+
source: string;
|
|
5
|
+
msg: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function addLog(msg: string): void;
|
|
8
|
+
export declare function getLogs(limit?: number, offset?: number): LogEntry[];
|
|
9
|
+
export declare function onLog(listener: (entry: LogEntry) => void): () => void;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=logs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logs.d.ts","sourceRoot":"","sources":["../src/logs.ts"],"names":[],"mappings":"AAAA,KAAK,QAAQ,GAAG;IACd,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAsBF,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAYxC;AAED,wBAAgB,OAAO,CAAC,KAAK,GAAE,MAAY,EAAE,MAAM,GAAE,MAAU,GAAG,QAAQ,EAAE,CAE3E;AAED,wBAAgB,KAAK,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,QAAQ,KAAK,IAAI,GAAG,MAAM,IAAI,CAGrE"}
|
package/dist/logs.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const MAX_LOGS = 500;
|
|
2
|
+
const logs = [];
|
|
3
|
+
let logIdCounter = 0;
|
|
4
|
+
const listeners = new Set();
|
|
5
|
+
function parseSource(msg) {
|
|
6
|
+
if (msg.includes("⏰"))
|
|
7
|
+
return "Scheduler";
|
|
8
|
+
if (msg.includes("🕸️") || msg.includes("Mesh"))
|
|
9
|
+
return "Mesh";
|
|
10
|
+
if (msg.includes("🐝") || msg.includes("Swarm"))
|
|
11
|
+
return "Swarm";
|
|
12
|
+
if (msg.includes("🌐"))
|
|
13
|
+
return "WebChat";
|
|
14
|
+
if (msg.includes("🔔"))
|
|
15
|
+
return "Webhooks";
|
|
16
|
+
if (msg.includes("📋"))
|
|
17
|
+
return "Agents";
|
|
18
|
+
if (msg.includes("🔄") || msg.includes("Model"))
|
|
19
|
+
return "LLM";
|
|
20
|
+
if (msg.includes("⌨️"))
|
|
21
|
+
return "Command";
|
|
22
|
+
if (msg.includes("🤖") || msg.includes("Claw"))
|
|
23
|
+
return "Agent";
|
|
24
|
+
if (msg.includes("📂"))
|
|
25
|
+
return "Files";
|
|
26
|
+
if (msg.includes("❌") || msg.includes("error"))
|
|
27
|
+
return "Error";
|
|
28
|
+
return "Core";
|
|
29
|
+
}
|
|
30
|
+
export function addLog(msg) {
|
|
31
|
+
const entry = {
|
|
32
|
+
id: ++logIdCounter,
|
|
33
|
+
time: new Date().toLocaleTimeString("sv-SE"),
|
|
34
|
+
source: parseSource(msg),
|
|
35
|
+
msg: msg.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "").trim(),
|
|
36
|
+
};
|
|
37
|
+
logs.push(entry);
|
|
38
|
+
if (logs.length > MAX_LOGS)
|
|
39
|
+
logs.shift();
|
|
40
|
+
for (const listener of listeners) {
|
|
41
|
+
try {
|
|
42
|
+
listener(entry);
|
|
43
|
+
}
|
|
44
|
+
catch { }
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export function getLogs(limit = 100, offset = 0) {
|
|
48
|
+
return logs.slice(-(limit + offset)).slice(offset);
|
|
49
|
+
}
|
|
50
|
+
export function onLog(listener) {
|
|
51
|
+
listeners.add(listener);
|
|
52
|
+
return () => listeners.delete(listener);
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=logs.js.map
|
package/dist/logs.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logs.js","sourceRoot":"","sources":["../src/logs.ts"],"names":[],"mappings":"AAOA,MAAM,QAAQ,GAAG,GAAG,CAAC;AACrB,MAAM,IAAI,GAAe,EAAE,CAAC;AAC5B,IAAI,YAAY,GAAG,CAAC,CAAC;AACrB,MAAM,SAAS,GAAmC,IAAI,GAAG,EAAE,CAAC;AAE5D,SAAS,WAAW,CAAC,GAAW;IAC9B,IAAI,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,OAAO,WAAW,CAAC;IAC1C,IAAI,GAAG,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,EAAE,CAAC,CAAC,IAAI,EAAE;KAC3G,CAAC;IACF,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACjB,IAAI,IAAI,CAAC,MAAM,GAAG,QAAQ;QAAE,IAAI,CAAC,KAAK,EAAE,CAAC;IACzC,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QACjC,IAAI,CAAC;YAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAAC,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;IACnC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,QAAgB,GAAG,EAAE,SAAiB,CAAC;IAC7D,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AACrD,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,QAAmC;IACvD,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACxB,OAAO,GAAG,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAC1C,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface KGEntity {
|
|
2
|
+
id: number;
|
|
3
|
+
name: string;
|
|
4
|
+
type: string;
|
|
5
|
+
properties: Record<string, unknown>;
|
|
6
|
+
}
|
|
7
|
+
export interface KGRelation {
|
|
8
|
+
id: number;
|
|
9
|
+
relation_type: string;
|
|
10
|
+
properties: Record<string, unknown>;
|
|
11
|
+
target_name?: string;
|
|
12
|
+
target_type?: string;
|
|
13
|
+
source_name?: string;
|
|
14
|
+
source_type?: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function addEntity(name: string, type?: string, properties?: Record<string, unknown>): Promise<KGEntity>;
|
|
17
|
+
export declare function addRelation(fromName: string, toName: string, relationType: string, properties?: Record<string, unknown>): Promise<{
|
|
18
|
+
id: number;
|
|
19
|
+
} | null>;
|
|
20
|
+
export declare function getEntity(name: string): KGEntity | null;
|
|
21
|
+
export declare function searchEntities(query: string, limit?: number): KGEntity[];
|
|
22
|
+
export declare function getEntityRelations(name: string): {
|
|
23
|
+
outgoing: KGRelation[];
|
|
24
|
+
incoming: KGRelation[];
|
|
25
|
+
};
|
|
26
|
+
export declare function traverseGraph(startName: string, maxDepth?: number): {
|
|
27
|
+
entities: KGEntity[];
|
|
28
|
+
relations: {
|
|
29
|
+
from: string;
|
|
30
|
+
to: string;
|
|
31
|
+
type: string;
|
|
32
|
+
}[];
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=knowledge-graph.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"knowledge-graph.d.ts","sourceRoot":"","sources":["../../src/memory/knowledge-graph.ts"],"names":[],"mappings":"AAwEA,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACrC;AAED,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,aAAa,EAAE,MAAM,CAAC;IACtB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACpC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wBAAsB,SAAS,CAC7B,IAAI,EAAE,MAAM,EACZ,IAAI,GAAE,MAAgB,EACtB,UAAU,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GACvC,OAAO,CAAC,QAAQ,CAAC,CAoBnB;AAED,wBAAsB,WAAW,CAC/B,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,MAAM,EACpB,UAAU,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GACvC,OAAO,CAAC;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC,CAUhC;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,GAAG,IAAI,CAIvD;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,GAAE,MAAW,GAAG,QAAQ,EAAE,CAG5E;AAED,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG;IAAE,QAAQ,EAAE,UAAU,EAAE,CAAC;IAAC,QAAQ,EAAE,UAAU,EAAE,CAAA;CAAE,CAuBnG;AAED,wBAAgB,aAAa,CAC3B,SAAS,EAAE,MAAM,EACjB,QAAQ,GAAE,MAAU,GACnB;IAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC;IAAC,SAAS,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;CAAE,CA0BnF"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import Database from "better-sqlite3";
|
|
2
|
+
import * as sqliteVec from "sqlite-vec";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import { embed, vecToBuffer, EMBEDDING_DIM } from "../embeddings.js";
|
|
6
|
+
import { getDataDir } from "../paths.js";
|
|
7
|
+
const DATA_DIR = getDataDir();
|
|
8
|
+
fs.mkdirSync(DATA_DIR, { recursive: true });
|
|
9
|
+
const dbPath = path.join(DATA_DIR, "cf-claw.db");
|
|
10
|
+
const db = new Database(dbPath);
|
|
11
|
+
db.pragma("journal_mode = WAL");
|
|
12
|
+
sqliteVec.load(db);
|
|
13
|
+
db.exec(`
|
|
14
|
+
CREATE TABLE IF NOT EXISTS kg_entities (
|
|
15
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
16
|
+
name TEXT NOT NULL UNIQUE,
|
|
17
|
+
type TEXT DEFAULT 'thing',
|
|
18
|
+
properties TEXT DEFAULT '{}',
|
|
19
|
+
embedding BLOB,
|
|
20
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
21
|
+
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS kg_entities_vec USING vec0(
|
|
25
|
+
embedding float[${EMBEDDING_DIM}]
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
CREATE TABLE IF NOT EXISTS kg_relations (
|
|
29
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
30
|
+
from_entity_id INTEGER NOT NULL,
|
|
31
|
+
to_entity_id INTEGER NOT NULL,
|
|
32
|
+
relation_type TEXT NOT NULL,
|
|
33
|
+
properties TEXT DEFAULT '{}',
|
|
34
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
35
|
+
FOREIGN KEY (from_entity_id) REFERENCES kg_entities(id),
|
|
36
|
+
FOREIGN KEY (to_entity_id) REFERENCES kg_entities(id)
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
CREATE INDEX IF NOT EXISTS idx_kg_relations_from ON kg_relations(from_entity_id);
|
|
40
|
+
CREATE INDEX IF NOT EXISTS idx_kg_relations_to ON kg_relations(to_entity_id);
|
|
41
|
+
CREATE INDEX IF NOT EXISTS idx_kg_relations_type ON kg_relations(relation_type);
|
|
42
|
+
`);
|
|
43
|
+
const insertEntity = db.prepare("INSERT OR REPLACE INTO kg_entities (name, type, properties, embedding, updated_at) VALUES (?, ?, ?, ?, datetime('now'))");
|
|
44
|
+
const insertEntityVec = db.prepare("INSERT OR REPLACE INTO kg_entities_vec (rowid, embedding) VALUES (?, ?)");
|
|
45
|
+
const getEntityByName = db.prepare("SELECT id, name, type, properties FROM kg_entities WHERE name = ?");
|
|
46
|
+
const getEntityById = db.prepare("SELECT id, name, type, properties FROM kg_entities WHERE id = ?");
|
|
47
|
+
const insertRelation = db.prepare("INSERT INTO kg_relations (from_entity_id, to_entity_id, relation_type, properties) VALUES (?, ?, ?, ?)");
|
|
48
|
+
const getRelationsFrom = db.prepare("SELECT r.id, r.relation_type, r.properties, e.name as to_name, e.type as to_type FROM kg_relations r JOIN kg_entities e ON r.to_entity_id = e.id WHERE r.from_entity_id = ?");
|
|
49
|
+
const getRelationsTo = db.prepare("SELECT r.id, r.relation_type, r.properties, e.name as from_name, e.type as from_type FROM kg_relations r JOIN kg_entities e ON r.from_entity_id = e.id WHERE r.to_entity_id = ?");
|
|
50
|
+
const searchEntitiesStmt = db.prepare("SELECT id, name, type, properties FROM kg_entities WHERE name LIKE ? LIMIT ?");
|
|
51
|
+
export async function addEntity(name, type = "thing", properties = {}) {
|
|
52
|
+
const vec = await embed(name);
|
|
53
|
+
const buf = vecToBuffer(vec);
|
|
54
|
+
const propsJson = JSON.stringify(properties);
|
|
55
|
+
const existing = getEntityByName.get(name);
|
|
56
|
+
if (existing) {
|
|
57
|
+
const merged = { ...JSON.parse(existing.properties || "{}"), ...properties };
|
|
58
|
+
insertEntity.run(name, type, JSON.stringify(merged), buf);
|
|
59
|
+
const updated = getEntityByName.get(name);
|
|
60
|
+
insertEntityVec.run(BigInt(updated.id), buf);
|
|
61
|
+
updated.properties = merged;
|
|
62
|
+
return updated;
|
|
63
|
+
}
|
|
64
|
+
insertEntity.run(name, type, propsJson, buf);
|
|
65
|
+
const entity = getEntityByName.get(name);
|
|
66
|
+
insertEntityVec.run(BigInt(entity.id), buf);
|
|
67
|
+
entity.properties = properties;
|
|
68
|
+
return entity;
|
|
69
|
+
}
|
|
70
|
+
export async function addRelation(fromName, toName, relationType, properties = {}) {
|
|
71
|
+
const from = getEntityByName.get(fromName);
|
|
72
|
+
const to = getEntityByName.get(toName);
|
|
73
|
+
if (!from || !to) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
const result = insertRelation.run(from.id, to.id, relationType, JSON.stringify(properties));
|
|
77
|
+
return { id: Number(result.lastInsertRowid) };
|
|
78
|
+
}
|
|
79
|
+
export function getEntity(name) {
|
|
80
|
+
const row = getEntityByName.get(name);
|
|
81
|
+
if (!row)
|
|
82
|
+
return null;
|
|
83
|
+
return { ...row, properties: JSON.parse(row.properties || "{}") };
|
|
84
|
+
}
|
|
85
|
+
export function searchEntities(query, limit = 10) {
|
|
86
|
+
const rows = searchEntitiesStmt.all(`%${query}%`, limit);
|
|
87
|
+
return rows.map((r) => ({ ...r, properties: JSON.parse(r.properties || "{}") }));
|
|
88
|
+
}
|
|
89
|
+
export function getEntityRelations(name) {
|
|
90
|
+
const entity = getEntityByName.get(name);
|
|
91
|
+
if (!entity)
|
|
92
|
+
return { outgoing: [], incoming: [] };
|
|
93
|
+
const outgoing = getRelationsFrom.all(entity.id);
|
|
94
|
+
const incoming = getRelationsTo.all(entity.id);
|
|
95
|
+
return {
|
|
96
|
+
outgoing: outgoing.map((r) => ({
|
|
97
|
+
id: r.id,
|
|
98
|
+
relation_type: r.relation_type,
|
|
99
|
+
properties: JSON.parse(r.properties || "{}"),
|
|
100
|
+
target_name: r.to_name,
|
|
101
|
+
target_type: r.to_type,
|
|
102
|
+
})),
|
|
103
|
+
incoming: incoming.map((r) => ({
|
|
104
|
+
id: r.id,
|
|
105
|
+
relation_type: r.relation_type,
|
|
106
|
+
properties: JSON.parse(r.properties || "{}"),
|
|
107
|
+
source_name: r.from_name,
|
|
108
|
+
source_type: r.from_type,
|
|
109
|
+
})),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
export function traverseGraph(startName, maxDepth = 3) {
|
|
113
|
+
const visited = new Set();
|
|
114
|
+
const entities = [];
|
|
115
|
+
const relations = [];
|
|
116
|
+
function dfs(entityId, depth) {
|
|
117
|
+
if (depth > maxDepth || visited.has(entityId))
|
|
118
|
+
return;
|
|
119
|
+
visited.add(entityId);
|
|
120
|
+
const entity = getEntityById.get(entityId);
|
|
121
|
+
if (!entity)
|
|
122
|
+
return;
|
|
123
|
+
entities.push({ ...entity, properties: JSON.parse(entity.properties || "{}") });
|
|
124
|
+
const outgoing = getRelationsFrom.all(entityId);
|
|
125
|
+
for (const rel of outgoing) {
|
|
126
|
+
relations.push({ from: entity.name, to: rel.to_name, type: rel.relation_type });
|
|
127
|
+
dfs(rel.to_entity_id, depth + 1);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const start = getEntityByName.get(startName);
|
|
131
|
+
if (start) {
|
|
132
|
+
dfs(start.id, 0);
|
|
133
|
+
}
|
|
134
|
+
return { entities, relations };
|
|
135
|
+
}
|
|
136
|
+
process.on("exit", () => { });
|
|
137
|
+
//# sourceMappingURL=knowledge-graph.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"knowledge-graph.js","sourceRoot":"","sources":["../../src/memory/knowledge-graph.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,SAAS,MAAM,YAAY,CAAC;AACxC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,MAAM,QAAQ,GAAG,UAAU,EAAE,CAAC;AAC9B,EAAE,CAAC,SAAS,CAAC,QAAQ,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;AAE5C,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;AACjD,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC;AAEhC,EAAE,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;AAChC,SAAS,CAAC,IAAI,CAAC,EAA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