@askexenow/exe-os 0.8.0 → 0.8.1
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/README.md +178 -79
- package/dist/bin/backfill-responses.js +160 -8
- package/dist/bin/backfill-vectors.js +130 -1
- package/dist/bin/cleanup-stale-review-tasks.js +130 -1
- package/dist/bin/cli.js +10111 -7540
- package/dist/bin/exe-agent.js +159 -1
- package/dist/bin/exe-assign.js +235 -16
- package/dist/bin/exe-boot.js +344 -472
- package/dist/bin/exe-call.js +145 -1
- package/dist/bin/exe-cloud.js +11 -0
- package/dist/bin/exe-dispatch.js +37 -24
- package/dist/bin/exe-doctor.js +130 -1
- package/dist/bin/exe-export-behaviors.js +150 -7
- package/dist/bin/exe-forget.js +822 -665
- package/dist/bin/exe-gateway.js +470 -62
- package/dist/bin/exe-heartbeat.js +133 -2
- package/dist/bin/exe-kill.js +150 -7
- package/dist/bin/exe-launch-agent.js +150 -7
- package/dist/bin/exe-new-employee.js +756 -224
- package/dist/bin/exe-pending-messages.js +132 -2
- package/dist/bin/exe-pending-notifications.js +130 -1
- package/dist/bin/exe-pending-reviews.js +132 -2
- package/dist/bin/exe-review.js +160 -8
- package/dist/bin/exe-search.js +2473 -2008
- package/dist/bin/exe-session-cleanup.js +238 -51
- package/dist/bin/exe-settings.js +11 -0
- package/dist/bin/exe-status.js +130 -1
- package/dist/bin/exe-team.js +130 -1
- package/dist/bin/git-sweep.js +272 -16
- package/dist/bin/graph-backfill.js +150 -7
- package/dist/bin/graph-export.js +150 -7
- package/dist/bin/install.js +5 -0
- package/dist/bin/scan-tasks.js +238 -19
- package/dist/bin/setup.js +1776 -10
- package/dist/bin/shard-migrate.js +150 -7
- package/dist/bin/update.js +9 -6
- package/dist/bin/wiki-sync.js +150 -7
- package/dist/gateway/index.js +470 -62
- package/dist/hooks/bug-report-worker.js +195 -35
- package/dist/hooks/commit-complete.js +272 -16
- package/dist/hooks/error-recall.js +2313 -1847
- package/dist/hooks/exe-heartbeat-hook.js +5 -0
- package/dist/hooks/ingest-worker.js +330 -58
- package/dist/hooks/ingest.js +11 -0
- package/dist/hooks/instructions-loaded.js +199 -10
- package/dist/hooks/notification.js +199 -10
- package/dist/hooks/post-compact.js +199 -10
- package/dist/hooks/pre-compact.js +199 -10
- package/dist/hooks/pre-tool-use.js +199 -10
- package/dist/hooks/prompt-ingest-worker.js +179 -14
- package/dist/hooks/prompt-submit.js +781 -285
- package/dist/hooks/response-ingest-worker.js +1900 -1405
- package/dist/hooks/session-end.js +456 -12
- package/dist/hooks/session-start.js +2188 -1724
- package/dist/hooks/stop.js +200 -10
- package/dist/hooks/subagent-stop.js +199 -10
- package/dist/hooks/summary-worker.js +604 -334
- package/dist/index.js +554 -61
- package/dist/lib/cloud-sync.js +5 -0
- package/dist/lib/config.js +13 -0
- package/dist/lib/consolidation.js +5 -0
- package/dist/lib/database.js +104 -0
- package/dist/lib/device-registry.js +109 -0
- package/dist/lib/embedder.js +13 -0
- package/dist/lib/employee-templates.js +53 -26
- package/dist/lib/employees.js +5 -0
- package/dist/lib/exe-daemon-client.js +5 -0
- package/dist/lib/exe-daemon.js +493 -79
- package/dist/lib/file-grep.js +20 -4
- package/dist/lib/hybrid-search.js +1435 -190
- package/dist/lib/identity-templates.js +126 -5
- package/dist/lib/identity.js +5 -0
- package/dist/lib/license.js +5 -0
- package/dist/lib/messaging.js +37 -24
- package/dist/lib/schedules.js +130 -1
- package/dist/lib/skill-learning.js +11 -0
- package/dist/lib/status-brief.js +5 -0
- package/dist/lib/store.js +199 -10
- package/dist/lib/task-router.js +72 -6
- package/dist/lib/tasks.js +179 -50
- package/dist/lib/tmux-routing.js +179 -46
- package/dist/mcp/server.js +2129 -1855
- package/dist/mcp/tools/create-task.js +86 -36
- package/dist/mcp/tools/deactivate-behavior.js +5 -0
- package/dist/mcp/tools/list-tasks.js +39 -11
- package/dist/mcp/tools/send-message.js +37 -24
- package/dist/mcp/tools/update-task.js +153 -38
- package/dist/runtime/index.js +451 -59
- package/dist/tui/App.js +454 -59
- package/package.json +1 -1
package/dist/bin/exe-search.js
CHANGED
|
@@ -24,1381 +24,1208 @@ var init_memory = __esm({
|
|
|
24
24
|
}
|
|
25
25
|
});
|
|
26
26
|
|
|
27
|
-
// src/lib/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
DB_PATH: () => DB_PATH,
|
|
34
|
-
EXE_AI_DIR: () => EXE_AI_DIR,
|
|
35
|
-
LEGACY_LANCE_PATH: () => LEGACY_LANCE_PATH,
|
|
36
|
-
MODELS_DIR: () => MODELS_DIR,
|
|
37
|
-
loadConfig: () => loadConfig,
|
|
38
|
-
loadConfigFrom: () => loadConfigFrom,
|
|
39
|
-
loadConfigSync: () => loadConfigSync,
|
|
40
|
-
migrateConfig: () => migrateConfig,
|
|
41
|
-
saveConfig: () => saveConfig
|
|
42
|
-
});
|
|
43
|
-
import { readFile as readFile2, writeFile as writeFile2, mkdir as mkdir2 } from "fs/promises";
|
|
44
|
-
import { readFileSync, existsSync as existsSync2, renameSync } from "fs";
|
|
45
|
-
import path2 from "path";
|
|
46
|
-
import os from "os";
|
|
47
|
-
function resolveDataDir() {
|
|
48
|
-
if (process.env.EXE_OS_DIR) return process.env.EXE_OS_DIR;
|
|
49
|
-
if (process.env.EXE_MEM_DIR) return process.env.EXE_MEM_DIR;
|
|
50
|
-
const newDir = path2.join(os.homedir(), ".exe-os");
|
|
51
|
-
const legacyDir = path2.join(os.homedir(), ".exe-mem");
|
|
52
|
-
if (!existsSync2(newDir) && existsSync2(legacyDir)) {
|
|
53
|
-
try {
|
|
54
|
-
renameSync(legacyDir, newDir);
|
|
55
|
-
process.stderr.write(`[exe-os] Migrated data directory: ~/.exe-mem \u2192 ~/.exe-os
|
|
56
|
-
`);
|
|
57
|
-
} catch {
|
|
58
|
-
return legacyDir;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return newDir;
|
|
62
|
-
}
|
|
63
|
-
function migrateLegacyConfig(raw) {
|
|
64
|
-
if ("r2" in raw) {
|
|
65
|
-
process.stderr.write(
|
|
66
|
-
"[exe-os] Warning: config.json contains deprecated 'r2' field from v1.0. R2 sync has been replaced in v1.1. The 'r2' field will be ignored.\n"
|
|
67
|
-
);
|
|
68
|
-
delete raw.r2;
|
|
69
|
-
}
|
|
70
|
-
if ("syncIntervalMs" in raw) {
|
|
71
|
-
delete raw.syncIntervalMs;
|
|
72
|
-
}
|
|
73
|
-
return raw;
|
|
74
|
-
}
|
|
75
|
-
function migrateConfig(raw) {
|
|
76
|
-
const fromVersion = typeof raw.config_version === "number" ? raw.config_version : 0;
|
|
77
|
-
let currentVersion = fromVersion;
|
|
78
|
-
let migrated = false;
|
|
79
|
-
if (currentVersion > CURRENT_CONFIG_VERSION) {
|
|
80
|
-
return { config: raw, migrated: false, fromVersion };
|
|
27
|
+
// src/lib/database.ts
|
|
28
|
+
import { createClient } from "@libsql/client";
|
|
29
|
+
async function initDatabase(config) {
|
|
30
|
+
if (_client) {
|
|
31
|
+
_client.close();
|
|
32
|
+
_client = null;
|
|
81
33
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
34
|
+
const opts = {
|
|
35
|
+
url: `file:${config.dbPath}`
|
|
36
|
+
};
|
|
37
|
+
if (config.encryptionKey) {
|
|
38
|
+
opts.encryptionKey = config.encryptionKey;
|
|
88
39
|
}
|
|
89
|
-
|
|
40
|
+
_client = createClient(opts);
|
|
90
41
|
}
|
|
91
|
-
function
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const userAuto = userRoadmap.rerankerAutoTrigger ?? {};
|
|
95
|
-
if (userAuto.enabled === void 0 && raw.rerankerEnabled !== void 0) {
|
|
96
|
-
userAuto.enabled = raw.rerankerEnabled;
|
|
42
|
+
function getClient() {
|
|
43
|
+
if (!_client) {
|
|
44
|
+
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
97
45
|
}
|
|
98
|
-
|
|
99
|
-
...userRoadmap,
|
|
100
|
-
rerankerAutoTrigger: { ...defaultAuto, ...userAuto }
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
function normalizeSessionLifecycle(raw) {
|
|
104
|
-
const defaultSL = DEFAULT_CONFIG.sessionLifecycle;
|
|
105
|
-
const userSL = raw.sessionLifecycle ?? {};
|
|
106
|
-
raw.sessionLifecycle = { ...defaultSL, ...userSL };
|
|
46
|
+
return _client;
|
|
107
47
|
}
|
|
108
|
-
async function
|
|
109
|
-
const
|
|
110
|
-
await
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
48
|
+
async function ensureSchema() {
|
|
49
|
+
const client = getClient();
|
|
50
|
+
await client.execute("PRAGMA journal_mode = WAL");
|
|
51
|
+
await client.execute("PRAGMA busy_timeout = 5000");
|
|
52
|
+
try {
|
|
53
|
+
await client.execute("PRAGMA libsql_vector_search_ef = 128");
|
|
54
|
+
} catch {
|
|
114
55
|
}
|
|
115
|
-
|
|
56
|
+
await client.executeMultiple(`
|
|
57
|
+
CREATE TABLE IF NOT EXISTS memories (
|
|
58
|
+
id TEXT PRIMARY KEY,
|
|
59
|
+
agent_id TEXT NOT NULL,
|
|
60
|
+
agent_role TEXT NOT NULL,
|
|
61
|
+
session_id TEXT NOT NULL,
|
|
62
|
+
timestamp TEXT NOT NULL,
|
|
63
|
+
tool_name TEXT NOT NULL,
|
|
64
|
+
project_name TEXT NOT NULL,
|
|
65
|
+
has_error INTEGER NOT NULL DEFAULT 0,
|
|
66
|
+
raw_text TEXT NOT NULL,
|
|
67
|
+
vector F32_BLOB(1024),
|
|
68
|
+
version INTEGER NOT NULL DEFAULT 0
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
CREATE INDEX IF NOT EXISTS idx_memories_agent
|
|
72
|
+
ON memories(agent_id);
|
|
73
|
+
|
|
74
|
+
CREATE INDEX IF NOT EXISTS idx_memories_timestamp
|
|
75
|
+
ON memories(timestamp);
|
|
76
|
+
|
|
77
|
+
CREATE INDEX IF NOT EXISTS idx_memories_session
|
|
78
|
+
ON memories(session_id);
|
|
79
|
+
|
|
80
|
+
CREATE INDEX IF NOT EXISTS idx_memories_project
|
|
81
|
+
ON memories(project_name);
|
|
82
|
+
|
|
83
|
+
CREATE INDEX IF NOT EXISTS idx_memories_tool
|
|
84
|
+
ON memories(tool_name);
|
|
85
|
+
|
|
86
|
+
CREATE INDEX IF NOT EXISTS idx_memories_version
|
|
87
|
+
ON memories(version);
|
|
88
|
+
|
|
89
|
+
CREATE INDEX IF NOT EXISTS idx_memories_agent_project
|
|
90
|
+
ON memories(agent_id, project_name);
|
|
91
|
+
`);
|
|
92
|
+
await client.executeMultiple(`
|
|
93
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
94
|
+
raw_text,
|
|
95
|
+
content='memories',
|
|
96
|
+
content_rowid='rowid'
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
|
|
100
|
+
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
101
|
+
END;
|
|
102
|
+
|
|
103
|
+
CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
|
|
104
|
+
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
105
|
+
END;
|
|
106
|
+
|
|
107
|
+
CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
|
|
108
|
+
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
109
|
+
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
110
|
+
END;
|
|
111
|
+
`);
|
|
112
|
+
await client.executeMultiple(`
|
|
113
|
+
CREATE TABLE IF NOT EXISTS sync_meta (
|
|
114
|
+
key TEXT PRIMARY KEY,
|
|
115
|
+
value TEXT NOT NULL
|
|
116
|
+
);
|
|
117
|
+
`);
|
|
118
|
+
await client.executeMultiple(`
|
|
119
|
+
CREATE TABLE IF NOT EXISTS tasks (
|
|
120
|
+
id TEXT PRIMARY KEY,
|
|
121
|
+
title TEXT NOT NULL,
|
|
122
|
+
assigned_to TEXT NOT NULL,
|
|
123
|
+
assigned_by TEXT NOT NULL,
|
|
124
|
+
project_name TEXT NOT NULL,
|
|
125
|
+
priority TEXT NOT NULL DEFAULT 'p1',
|
|
126
|
+
status TEXT NOT NULL DEFAULT 'open',
|
|
127
|
+
task_file TEXT,
|
|
128
|
+
created_at TEXT NOT NULL,
|
|
129
|
+
updated_at TEXT NOT NULL
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status
|
|
133
|
+
ON tasks(assigned_to, status);
|
|
134
|
+
`);
|
|
135
|
+
await client.executeMultiple(`
|
|
136
|
+
CREATE TABLE IF NOT EXISTS behaviors (
|
|
137
|
+
id TEXT PRIMARY KEY,
|
|
138
|
+
agent_id TEXT NOT NULL,
|
|
139
|
+
project_name TEXT,
|
|
140
|
+
domain TEXT,
|
|
141
|
+
content TEXT NOT NULL,
|
|
142
|
+
active INTEGER NOT NULL DEFAULT 1,
|
|
143
|
+
created_at TEXT NOT NULL,
|
|
144
|
+
updated_at TEXT NOT NULL
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
CREATE INDEX IF NOT EXISTS idx_behaviors_agent
|
|
148
|
+
ON behaviors(agent_id, active);
|
|
149
|
+
`);
|
|
116
150
|
try {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
`
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
151
|
+
const existing = await client.execute({
|
|
152
|
+
sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = 'exe'",
|
|
153
|
+
args: []
|
|
154
|
+
});
|
|
155
|
+
if (Number(existing.rows[0]?.cnt) === 0) {
|
|
156
|
+
await client.executeMultiple(`
|
|
157
|
+
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
158
|
+
VALUES
|
|
159
|
+
(hex(randomblob(16)), 'exe', NULL, 'workflow', 'Don''t ask "keep going?" \u2014 just keep executing phases/plans autonomously', 1, '2026-03-25T00:00:00Z', '2026-03-25T00:00:00Z');
|
|
160
|
+
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
161
|
+
VALUES
|
|
162
|
+
(hex(randomblob(16)), 'exe', NULL, 'tool-use', 'Always use create_task MCP tool, never write .md files directly for task creation', 1, '2026-03-25T00:00:00Z', '2026-03-25T00:00:00Z');
|
|
163
|
+
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
164
|
+
VALUES
|
|
165
|
+
(hex(randomblob(16)), 'exe', NULL, 'workflow', 'Auto-start reviewing when idle and reviews are pending \u2014 never ask founder for permission', 1, '2026-03-25T00:00:00Z', '2026-03-25T00:00:00Z');
|
|
166
|
+
`);
|
|
133
167
|
}
|
|
134
|
-
return config;
|
|
135
168
|
} catch {
|
|
136
|
-
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
function loadConfigSync() {
|
|
140
|
-
const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
|
|
141
|
-
const configPath = path2.join(dir, "config.json");
|
|
142
|
-
if (!existsSync2(configPath)) {
|
|
143
|
-
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
|
|
144
169
|
}
|
|
145
170
|
try {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
normalizeScalingRoadmap(migratedCfg);
|
|
151
|
-
normalizeSessionLifecycle(migratedCfg);
|
|
152
|
-
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db"), ...migratedCfg };
|
|
171
|
+
await client.execute({
|
|
172
|
+
sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
|
|
173
|
+
args: []
|
|
174
|
+
});
|
|
153
175
|
} catch {
|
|
154
|
-
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
|
|
155
176
|
}
|
|
156
|
-
}
|
|
157
|
-
async function saveConfig(config) {
|
|
158
|
-
const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
|
|
159
|
-
await mkdir2(dir, { recursive: true });
|
|
160
|
-
const configPath = path2.join(dir, "config.json");
|
|
161
|
-
await writeFile2(configPath, JSON.stringify(config, null, 2) + "\n");
|
|
162
|
-
}
|
|
163
|
-
async function loadConfigFrom(configPath) {
|
|
164
|
-
const raw = await readFile2(configPath, "utf-8");
|
|
165
177
|
try {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
normalizeSessionLifecycle(migratedCfg);
|
|
171
|
-
return { ...DEFAULT_CONFIG, ...migratedCfg };
|
|
178
|
+
await client.execute({
|
|
179
|
+
sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
|
|
180
|
+
args: []
|
|
181
|
+
});
|
|
172
182
|
} catch {
|
|
173
|
-
return { ...DEFAULT_CONFIG };
|
|
174
183
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
DB_PATH = path2.join(EXE_AI_DIR, "memories.db");
|
|
182
|
-
MODELS_DIR = path2.join(EXE_AI_DIR, "models");
|
|
183
|
-
CONFIG_PATH = path2.join(EXE_AI_DIR, "config.json");
|
|
184
|
-
LEGACY_LANCE_PATH = path2.join(EXE_AI_DIR, "local.lance");
|
|
185
|
-
CURRENT_CONFIG_VERSION = 1;
|
|
186
|
-
DEFAULT_CONFIG = {
|
|
187
|
-
config_version: CURRENT_CONFIG_VERSION,
|
|
188
|
-
dbPath: DB_PATH,
|
|
189
|
-
modelFile: "jina-embeddings-v5-small-q4_k_m.gguf",
|
|
190
|
-
embeddingDim: 1024,
|
|
191
|
-
batchSize: 20,
|
|
192
|
-
flushIntervalMs: 1e4,
|
|
193
|
-
autoIngestion: true,
|
|
194
|
-
autoRetrieval: true,
|
|
195
|
-
searchMode: "hybrid",
|
|
196
|
-
hookSearchMode: "hybrid",
|
|
197
|
-
fileGrepEnabled: true,
|
|
198
|
-
splashEffect: true,
|
|
199
|
-
consolidationEnabled: true,
|
|
200
|
-
consolidationIntervalMs: 6 * 60 * 60 * 1e3,
|
|
201
|
-
consolidationModel: "claude-haiku-4-5-20251001",
|
|
202
|
-
consolidationMaxCallsPerRun: 20,
|
|
203
|
-
selfQueryRouter: true,
|
|
204
|
-
selfQueryModel: "claude-haiku-4-5-20251001",
|
|
205
|
-
rerankerEnabled: true,
|
|
206
|
-
scalingRoadmap: {
|
|
207
|
-
rerankerAutoTrigger: {
|
|
208
|
-
enabled: true,
|
|
209
|
-
broadQueryMinCardinality: 5e4,
|
|
210
|
-
fetchTopK: 150,
|
|
211
|
-
returnTopK: 5
|
|
212
|
-
}
|
|
213
|
-
},
|
|
214
|
-
graphRagEnabled: true,
|
|
215
|
-
wikiEnabled: false,
|
|
216
|
-
wikiUrl: "",
|
|
217
|
-
wikiApiKey: "",
|
|
218
|
-
wikiSyncIntervalMs: 30 * 60 * 1e3,
|
|
219
|
-
wikiWorkspaceMapping: {
|
|
220
|
-
exe: "Executive",
|
|
221
|
-
yoshi: "Engineering",
|
|
222
|
-
mari: "Marketing",
|
|
223
|
-
tom: "Engineering",
|
|
224
|
-
sasha: "Production"
|
|
225
|
-
},
|
|
226
|
-
wikiAutoUpdate: true,
|
|
227
|
-
wikiAutoUpdateThreshold: 0.5,
|
|
228
|
-
wikiAutoUpdateCreateNew: true,
|
|
229
|
-
skillLearning: true,
|
|
230
|
-
skillThreshold: 3,
|
|
231
|
-
skillModel: "claude-haiku-4-5-20251001",
|
|
232
|
-
exeHeartbeat: {
|
|
233
|
-
enabled: true,
|
|
234
|
-
intervalSeconds: 60,
|
|
235
|
-
staleInProgressThresholdHours: 2
|
|
236
|
-
},
|
|
237
|
-
sessionLifecycle: {
|
|
238
|
-
idleKillEnabled: true,
|
|
239
|
-
idleKillTicksRequired: 3,
|
|
240
|
-
idleKillIntercomAckWindowMs: 1e4,
|
|
241
|
-
maxAutoInstances: 10
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
CONFIG_MIGRATIONS = [
|
|
245
|
-
{
|
|
246
|
-
from: 0,
|
|
247
|
-
to: 1,
|
|
248
|
-
migrate: (cfg) => {
|
|
249
|
-
cfg.config_version = 1;
|
|
250
|
-
return cfg;
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
];
|
|
184
|
+
try {
|
|
185
|
+
await client.execute({
|
|
186
|
+
sql: `ALTER TABLE tasks ADD COLUMN parent_task_id TEXT`,
|
|
187
|
+
args: []
|
|
188
|
+
});
|
|
189
|
+
} catch {
|
|
254
190
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
getShardClient: () => getShardClient,
|
|
264
|
-
getShardsDir: () => getShardsDir,
|
|
265
|
-
initShardManager: () => initShardManager,
|
|
266
|
-
isShardingEnabled: () => isShardingEnabled,
|
|
267
|
-
listShards: () => listShards,
|
|
268
|
-
shardExists: () => shardExists
|
|
269
|
-
});
|
|
270
|
-
import path3 from "path";
|
|
271
|
-
import { existsSync as existsSync3, mkdirSync } from "fs";
|
|
272
|
-
import { createClient as createClient2 } from "@libsql/client";
|
|
273
|
-
function initShardManager(encryptionKey) {
|
|
274
|
-
_encryptionKey = encryptionKey;
|
|
275
|
-
if (!existsSync3(SHARDS_DIR)) {
|
|
276
|
-
mkdirSync(SHARDS_DIR, { recursive: true });
|
|
191
|
+
try {
|
|
192
|
+
await client.execute({
|
|
193
|
+
sql: `CREATE INDEX IF NOT EXISTS idx_tasks_parent_task_id
|
|
194
|
+
ON tasks(parent_task_id)
|
|
195
|
+
WHERE parent_task_id IS NOT NULL`,
|
|
196
|
+
args: []
|
|
197
|
+
});
|
|
198
|
+
} catch {
|
|
277
199
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
return SHARDS_DIR;
|
|
285
|
-
}
|
|
286
|
-
function getShardClient(projectName) {
|
|
287
|
-
if (!_encryptionKey) {
|
|
288
|
-
throw new Error("Shard manager not initialized. Call initShardManager() first.");
|
|
200
|
+
try {
|
|
201
|
+
await client.execute({
|
|
202
|
+
sql: `UPDATE tasks SET status = 'done' WHERE status = 'completed'`,
|
|
203
|
+
args: []
|
|
204
|
+
});
|
|
205
|
+
} catch {
|
|
289
206
|
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
207
|
+
try {
|
|
208
|
+
await client.execute({
|
|
209
|
+
sql: `ALTER TABLE tasks ADD COLUMN reviewer TEXT`,
|
|
210
|
+
args: []
|
|
211
|
+
});
|
|
212
|
+
} catch {
|
|
293
213
|
}
|
|
294
|
-
const cached = _shards.get(safeName);
|
|
295
|
-
if (cached) return cached;
|
|
296
|
-
const dbPath = path3.join(SHARDS_DIR, `${safeName}.db`);
|
|
297
|
-
const client = createClient2({
|
|
298
|
-
url: `file:${dbPath}`,
|
|
299
|
-
encryptionKey: _encryptionKey
|
|
300
|
-
});
|
|
301
|
-
_shards.set(safeName, client);
|
|
302
|
-
return client;
|
|
303
|
-
}
|
|
304
|
-
function shardExists(projectName) {
|
|
305
|
-
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
306
|
-
return existsSync3(path3.join(SHARDS_DIR, `${safeName}.db`));
|
|
307
|
-
}
|
|
308
|
-
function listShards() {
|
|
309
|
-
if (!existsSync3(SHARDS_DIR)) return [];
|
|
310
|
-
const { readdirSync: readdirSync2 } = __require("fs");
|
|
311
|
-
return readdirSync2(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
|
|
312
|
-
}
|
|
313
|
-
async function ensureShardSchema(client) {
|
|
314
|
-
await client.execute("PRAGMA journal_mode = WAL");
|
|
315
|
-
await client.execute("PRAGMA busy_timeout = 5000");
|
|
316
214
|
try {
|
|
317
|
-
await client.execute(
|
|
215
|
+
await client.execute({
|
|
216
|
+
sql: `ALTER TABLE tasks ADD COLUMN context TEXT`,
|
|
217
|
+
args: []
|
|
218
|
+
});
|
|
318
219
|
} catch {
|
|
319
220
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
timestamp TEXT NOT NULL,
|
|
327
|
-
tool_name TEXT NOT NULL,
|
|
328
|
-
project_name TEXT NOT NULL,
|
|
329
|
-
has_error INTEGER NOT NULL DEFAULT 0,
|
|
330
|
-
raw_text TEXT NOT NULL,
|
|
331
|
-
vector F32_BLOB(1024),
|
|
332
|
-
version INTEGER NOT NULL DEFAULT 0
|
|
333
|
-
);
|
|
334
|
-
|
|
335
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent ON memories(agent_id);
|
|
336
|
-
CREATE INDEX IF NOT EXISTS idx_memories_timestamp ON memories(timestamp);
|
|
337
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent_project ON memories(agent_id, project_name);
|
|
338
|
-
`);
|
|
339
|
-
await client.executeMultiple(`
|
|
340
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
341
|
-
raw_text,
|
|
342
|
-
content='memories',
|
|
343
|
-
content_rowid='rowid'
|
|
344
|
-
);
|
|
345
|
-
|
|
346
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
|
|
347
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
348
|
-
END;
|
|
349
|
-
|
|
350
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
|
|
351
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
352
|
-
END;
|
|
353
|
-
|
|
354
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
|
|
355
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
356
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
357
|
-
END;
|
|
358
|
-
`);
|
|
359
|
-
for (const col of [
|
|
360
|
-
"ALTER TABLE memories ADD COLUMN task_id TEXT",
|
|
361
|
-
"ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0",
|
|
362
|
-
"ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5",
|
|
363
|
-
"ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'",
|
|
364
|
-
"ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0",
|
|
365
|
-
"ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0",
|
|
366
|
-
"ALTER TABLE memories ADD COLUMN content_hash TEXT",
|
|
367
|
-
"ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT",
|
|
368
|
-
"ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7",
|
|
369
|
-
"ALTER TABLE memories ADD COLUMN last_accessed TEXT",
|
|
370
|
-
// Wiki linkage columns (must match database.ts)
|
|
371
|
-
"ALTER TABLE memories ADD COLUMN workspace_id TEXT",
|
|
372
|
-
"ALTER TABLE memories ADD COLUMN document_id TEXT",
|
|
373
|
-
"ALTER TABLE memories ADD COLUMN user_id TEXT",
|
|
374
|
-
"ALTER TABLE memories ADD COLUMN char_offset INTEGER",
|
|
375
|
-
"ALTER TABLE memories ADD COLUMN page_number INTEGER"
|
|
376
|
-
]) {
|
|
377
|
-
try {
|
|
378
|
-
await client.execute(col);
|
|
379
|
-
} catch {
|
|
380
|
-
}
|
|
221
|
+
try {
|
|
222
|
+
await client.execute({
|
|
223
|
+
sql: `ALTER TABLE tasks ADD COLUMN result TEXT`,
|
|
224
|
+
args: []
|
|
225
|
+
});
|
|
226
|
+
} catch {
|
|
381
227
|
}
|
|
382
228
|
try {
|
|
383
|
-
await client.execute(
|
|
229
|
+
await client.execute({
|
|
230
|
+
sql: `ALTER TABLE tasks ADD COLUMN assigned_tmux TEXT`,
|
|
231
|
+
args: []
|
|
232
|
+
});
|
|
384
233
|
} catch {
|
|
385
234
|
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
235
|
+
try {
|
|
236
|
+
await client.execute({
|
|
237
|
+
sql: `ALTER TABLE tasks ADD COLUMN checkpoint TEXT`,
|
|
238
|
+
args: []
|
|
239
|
+
});
|
|
240
|
+
} catch {
|
|
241
|
+
}
|
|
242
|
+
try {
|
|
243
|
+
await client.execute({
|
|
244
|
+
sql: `ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER NOT NULL DEFAULT 0`,
|
|
245
|
+
args: []
|
|
246
|
+
});
|
|
247
|
+
} catch {
|
|
248
|
+
}
|
|
249
|
+
try {
|
|
250
|
+
await client.execute({
|
|
251
|
+
sql: `ALTER TABLE tasks ADD COLUMN complexity TEXT NOT NULL DEFAULT 'standard'`,
|
|
252
|
+
args: []
|
|
253
|
+
});
|
|
254
|
+
} catch {
|
|
255
|
+
}
|
|
256
|
+
try {
|
|
257
|
+
await client.execute({
|
|
258
|
+
sql: `ALTER TABLE memories ADD COLUMN task_id TEXT`,
|
|
259
|
+
args: []
|
|
260
|
+
});
|
|
261
|
+
} catch {
|
|
262
|
+
}
|
|
263
|
+
try {
|
|
264
|
+
await client.execute({
|
|
265
|
+
sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
|
|
266
|
+
args: []
|
|
267
|
+
});
|
|
268
|
+
} catch {
|
|
269
|
+
}
|
|
270
|
+
try {
|
|
271
|
+
await client.execute({
|
|
272
|
+
sql: `ALTER TABLE memories ADD COLUMN author_device_id TEXT`,
|
|
273
|
+
args: []
|
|
274
|
+
});
|
|
275
|
+
} catch {
|
|
276
|
+
}
|
|
277
|
+
try {
|
|
278
|
+
await client.execute({
|
|
279
|
+
sql: `ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'`,
|
|
280
|
+
args: []
|
|
281
|
+
});
|
|
282
|
+
} catch {
|
|
395
283
|
}
|
|
396
284
|
await client.executeMultiple(`
|
|
397
|
-
CREATE TABLE IF NOT EXISTS
|
|
398
|
-
id
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
last_seen TEXT NOT NULL,
|
|
403
|
-
properties TEXT DEFAULT '{}',
|
|
404
|
-
UNIQUE(name, type)
|
|
285
|
+
CREATE TABLE IF NOT EXISTS consolidations (
|
|
286
|
+
id TEXT PRIMARY KEY,
|
|
287
|
+
consolidated_memory_id TEXT NOT NULL,
|
|
288
|
+
source_memory_id TEXT NOT NULL,
|
|
289
|
+
created_at TEXT NOT NULL
|
|
405
290
|
);
|
|
406
291
|
|
|
407
|
-
CREATE
|
|
408
|
-
|
|
409
|
-
source_entity_id TEXT NOT NULL,
|
|
410
|
-
target_entity_id TEXT NOT NULL,
|
|
411
|
-
type TEXT NOT NULL,
|
|
412
|
-
weight REAL DEFAULT 1.0,
|
|
413
|
-
timestamp TEXT NOT NULL,
|
|
414
|
-
properties TEXT DEFAULT '{}',
|
|
415
|
-
UNIQUE(source_entity_id, target_entity_id, type)
|
|
416
|
-
);
|
|
292
|
+
CREATE INDEX IF NOT EXISTS idx_consolidations_source
|
|
293
|
+
ON consolidations(source_memory_id);
|
|
417
294
|
|
|
418
|
-
CREATE
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
295
|
+
CREATE INDEX IF NOT EXISTS idx_consolidations_consolidated
|
|
296
|
+
ON consolidations(consolidated_memory_id);
|
|
297
|
+
`);
|
|
298
|
+
await client.executeMultiple(`
|
|
299
|
+
CREATE TABLE IF NOT EXISTS reminders (
|
|
300
|
+
id TEXT PRIMARY KEY,
|
|
301
|
+
text TEXT NOT NULL,
|
|
302
|
+
created_at TEXT NOT NULL,
|
|
303
|
+
due_date TEXT,
|
|
304
|
+
completed_at TEXT
|
|
422
305
|
);
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
306
|
+
`);
|
|
307
|
+
await client.executeMultiple(`
|
|
308
|
+
CREATE TABLE IF NOT EXISTS notifications (
|
|
309
|
+
id TEXT PRIMARY KEY,
|
|
310
|
+
agent_id TEXT NOT NULL,
|
|
311
|
+
agent_role TEXT NOT NULL,
|
|
312
|
+
event TEXT NOT NULL,
|
|
313
|
+
project TEXT NOT NULL,
|
|
314
|
+
summary TEXT NOT NULL,
|
|
315
|
+
task_file TEXT,
|
|
316
|
+
read INTEGER NOT NULL DEFAULT 0,
|
|
317
|
+
created_at TEXT NOT NULL
|
|
428
318
|
);
|
|
429
319
|
|
|
430
|
-
CREATE INDEX IF NOT EXISTS
|
|
431
|
-
|
|
432
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
|
|
433
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
|
|
434
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_type ON relationships(type);
|
|
320
|
+
CREATE INDEX IF NOT EXISTS idx_notifications_read
|
|
321
|
+
ON notifications(read);
|
|
435
322
|
|
|
436
|
-
CREATE
|
|
437
|
-
|
|
438
|
-
label TEXT NOT NULL,
|
|
439
|
-
relation TEXT NOT NULL,
|
|
440
|
-
confidence REAL DEFAULT 1.0,
|
|
441
|
-
timestamp TEXT NOT NULL
|
|
442
|
-
);
|
|
323
|
+
CREATE INDEX IF NOT EXISTS idx_notifications_agent
|
|
324
|
+
ON notifications(agent_id);
|
|
443
325
|
|
|
444
|
-
CREATE
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
326
|
+
CREATE INDEX IF NOT EXISTS idx_notifications_task_file
|
|
327
|
+
ON notifications(task_file);
|
|
328
|
+
`);
|
|
329
|
+
await client.executeMultiple(`
|
|
330
|
+
CREATE TABLE IF NOT EXISTS schedules (
|
|
331
|
+
id TEXT PRIMARY KEY,
|
|
332
|
+
cron TEXT NOT NULL,
|
|
333
|
+
description TEXT NOT NULL,
|
|
334
|
+
job_type TEXT NOT NULL DEFAULT 'report',
|
|
335
|
+
prompt TEXT,
|
|
336
|
+
assigned_to TEXT,
|
|
337
|
+
project_name TEXT,
|
|
338
|
+
active INTEGER NOT NULL DEFAULT 1,
|
|
339
|
+
use_crontab INTEGER NOT NULL DEFAULT 0,
|
|
340
|
+
created_at TEXT NOT NULL
|
|
448
341
|
);
|
|
449
342
|
`);
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
_encryptionKey = null;
|
|
481
|
-
_shardingEnabled = false;
|
|
482
|
-
}
|
|
483
|
-
});
|
|
343
|
+
await client.executeMultiple(`
|
|
344
|
+
CREATE TABLE IF NOT EXISTS device_registry (
|
|
345
|
+
device_id TEXT PRIMARY KEY,
|
|
346
|
+
friendly_name TEXT NOT NULL,
|
|
347
|
+
hostname TEXT NOT NULL,
|
|
348
|
+
projects TEXT NOT NULL DEFAULT '[]',
|
|
349
|
+
agents TEXT NOT NULL DEFAULT '[]',
|
|
350
|
+
connected INTEGER DEFAULT 0,
|
|
351
|
+
last_seen TEXT NOT NULL
|
|
352
|
+
);
|
|
353
|
+
`);
|
|
354
|
+
await client.executeMultiple(`
|
|
355
|
+
CREATE TABLE IF NOT EXISTS messages (
|
|
356
|
+
id TEXT PRIMARY KEY,
|
|
357
|
+
from_agent TEXT NOT NULL,
|
|
358
|
+
from_device TEXT NOT NULL DEFAULT 'local',
|
|
359
|
+
target_agent TEXT NOT NULL,
|
|
360
|
+
target_project TEXT,
|
|
361
|
+
target_device TEXT NOT NULL DEFAULT 'local',
|
|
362
|
+
content TEXT NOT NULL,
|
|
363
|
+
priority TEXT DEFAULT 'normal',
|
|
364
|
+
status TEXT DEFAULT 'pending',
|
|
365
|
+
server_seq INTEGER,
|
|
366
|
+
retry_count INTEGER DEFAULT 0,
|
|
367
|
+
created_at TEXT NOT NULL,
|
|
368
|
+
delivered_at TEXT,
|
|
369
|
+
processed_at TEXT,
|
|
370
|
+
failed_at TEXT,
|
|
371
|
+
failure_reason TEXT
|
|
372
|
+
);
|
|
484
373
|
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
if (query.length < 10) {
|
|
492
|
-
return {
|
|
493
|
-
semanticQuery: query,
|
|
494
|
-
projectFilter: null,
|
|
495
|
-
roleFilter: null,
|
|
496
|
-
timeFilter: null,
|
|
497
|
-
isBroadQuery: false
|
|
498
|
-
};
|
|
499
|
-
}
|
|
374
|
+
CREATE INDEX IF NOT EXISTS idx_messages_target
|
|
375
|
+
ON messages(target_agent, status);
|
|
376
|
+
|
|
377
|
+
CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
|
|
378
|
+
ON messages(target_agent, from_agent, server_seq);
|
|
379
|
+
`);
|
|
500
380
|
try {
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
const response = await client.messages.create({
|
|
505
|
-
model,
|
|
506
|
-
max_tokens: 256,
|
|
507
|
-
system: `You are a search query router. Extract metadata filters from the user's memory search query. Today is ${now}. Convert relative time references (yesterday, last week) to ISO timestamps.`,
|
|
508
|
-
messages: [{ role: "user", content: query }],
|
|
509
|
-
tools: [EXTRACT_TOOL],
|
|
510
|
-
tool_choice: { type: "tool", name: "extract_search_filters" }
|
|
381
|
+
await client.execute({
|
|
382
|
+
sql: `UPDATE memories SET project_name = 'exe-create' WHERE project_name = 'web'`,
|
|
383
|
+
args: []
|
|
511
384
|
});
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
`[self-query-router] LLM extraction failed, using passthrough: ${err instanceof Error ? err.message : String(err)}
|
|
526
|
-
`
|
|
527
|
-
);
|
|
528
|
-
}
|
|
529
|
-
return {
|
|
530
|
-
semanticQuery: query,
|
|
531
|
-
projectFilter: null,
|
|
532
|
-
roleFilter: null,
|
|
533
|
-
timeFilter: null,
|
|
534
|
-
isBroadQuery: false
|
|
535
|
-
};
|
|
536
|
-
}
|
|
537
|
-
var EXTRACT_TOOL;
|
|
538
|
-
var init_self_query_router = __esm({
|
|
539
|
-
"src/lib/self-query-router.ts"() {
|
|
540
|
-
"use strict";
|
|
541
|
-
EXTRACT_TOOL = {
|
|
542
|
-
name: "extract_search_filters",
|
|
543
|
-
description: "Extract metadata filters from a memory search query to improve retrieval precision.",
|
|
544
|
-
input_schema: {
|
|
545
|
-
type: "object",
|
|
546
|
-
properties: {
|
|
547
|
-
semantic_query: {
|
|
548
|
-
type: "string",
|
|
549
|
-
description: "The core semantic meaning of the query, stripped of metadata references. This is used for embedding search."
|
|
550
|
-
},
|
|
551
|
-
project_filter: {
|
|
552
|
-
type: ["string", "null"],
|
|
553
|
-
description: "Project name if the query references a specific project (e.g., 'exe-os', 'exe-create'). Null if no project specified."
|
|
554
|
-
},
|
|
555
|
-
role_filter: {
|
|
556
|
-
type: ["string", "null"],
|
|
557
|
-
description: "Agent role if the query targets a specific role (e.g., 'CTO', 'CMO'). Null if no role specified."
|
|
558
|
-
},
|
|
559
|
-
time_filter: {
|
|
560
|
-
type: ["string", "null"],
|
|
561
|
-
description: "ISO 8601 timestamp lower bound if the query references a time period (e.g., 'last week', 'yesterday'). Null if no time reference."
|
|
562
|
-
},
|
|
563
|
-
is_broad_query: {
|
|
564
|
-
type: "boolean",
|
|
565
|
-
description: "True if the query is exploratory/broad (e.g., 'what has yoshi been working on', 'summarize recent activity'). False if targeted (e.g., 'how did we fix the auth bug')."
|
|
566
|
-
}
|
|
567
|
-
},
|
|
568
|
-
required: ["semantic_query", "project_filter", "role_filter", "time_filter", "is_broad_query"]
|
|
569
|
-
}
|
|
570
|
-
};
|
|
385
|
+
await client.execute({
|
|
386
|
+
sql: `UPDATE memories SET project_name = 'exe-os' WHERE project_name = 'worker'`,
|
|
387
|
+
args: []
|
|
388
|
+
});
|
|
389
|
+
await client.execute({
|
|
390
|
+
sql: `UPDATE tasks SET project_name = 'exe-create' WHERE project_name = 'web'`,
|
|
391
|
+
args: []
|
|
392
|
+
});
|
|
393
|
+
await client.execute({
|
|
394
|
+
sql: `UPDATE tasks SET project_name = 'exe-os' WHERE project_name = 'worker'`,
|
|
395
|
+
args: []
|
|
396
|
+
});
|
|
397
|
+
} catch {
|
|
571
398
|
}
|
|
572
|
-
|
|
399
|
+
await client.executeMultiple(`
|
|
400
|
+
CREATE TABLE IF NOT EXISTS trajectories (
|
|
401
|
+
id TEXT PRIMARY KEY,
|
|
402
|
+
task_id TEXT NOT NULL,
|
|
403
|
+
agent_id TEXT NOT NULL,
|
|
404
|
+
project_name TEXT NOT NULL,
|
|
405
|
+
task_title TEXT NOT NULL,
|
|
406
|
+
signature TEXT NOT NULL,
|
|
407
|
+
signature_hash TEXT NOT NULL,
|
|
408
|
+
tool_count INTEGER NOT NULL,
|
|
409
|
+
skill_id TEXT,
|
|
410
|
+
created_at TEXT NOT NULL
|
|
411
|
+
);
|
|
573
412
|
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
let newlineIdx;
|
|
584
|
-
while ((newlineIdx = _buffer.indexOf("\n")) !== -1) {
|
|
585
|
-
const line = _buffer.slice(0, newlineIdx).trim();
|
|
586
|
-
_buffer = _buffer.slice(newlineIdx + 1);
|
|
587
|
-
if (!line) continue;
|
|
588
|
-
try {
|
|
589
|
-
const response = JSON.parse(line);
|
|
590
|
-
const entry = _pending.get(response.id);
|
|
591
|
-
if (entry) {
|
|
592
|
-
clearTimeout(entry.timer);
|
|
593
|
-
_pending.delete(response.id);
|
|
594
|
-
entry.resolve(response);
|
|
595
|
-
}
|
|
596
|
-
} catch {
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
function cleanupStaleFiles() {
|
|
601
|
-
if (existsSync4(PID_PATH)) {
|
|
602
|
-
try {
|
|
603
|
-
const pid = parseInt(readFileSync2(PID_PATH, "utf8").trim(), 10);
|
|
604
|
-
if (pid > 0) {
|
|
605
|
-
try {
|
|
606
|
-
process.kill(pid, 0);
|
|
607
|
-
return;
|
|
608
|
-
} catch {
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
} catch {
|
|
612
|
-
}
|
|
613
|
-
try {
|
|
614
|
-
unlinkSync(PID_PATH);
|
|
615
|
-
} catch {
|
|
616
|
-
}
|
|
617
|
-
try {
|
|
618
|
-
unlinkSync(SOCKET_PATH);
|
|
619
|
-
} catch {
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
function findPackageRoot() {
|
|
624
|
-
let dir = path4.dirname(fileURLToPath(import.meta.url));
|
|
625
|
-
const { root } = path4.parse(dir);
|
|
626
|
-
while (dir !== root) {
|
|
627
|
-
if (existsSync4(path4.join(dir, "package.json"))) return dir;
|
|
628
|
-
dir = path4.dirname(dir);
|
|
413
|
+
CREATE INDEX IF NOT EXISTS idx_trajectories_hash
|
|
414
|
+
ON trajectories(signature_hash);
|
|
415
|
+
|
|
416
|
+
CREATE INDEX IF NOT EXISTS idx_trajectories_agent
|
|
417
|
+
ON trajectories(agent_id);
|
|
418
|
+
`);
|
|
419
|
+
try {
|
|
420
|
+
await client.execute("ALTER TABLE trajectories ADD COLUMN skill_id TEXT");
|
|
421
|
+
} catch {
|
|
629
422
|
}
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
423
|
+
await client.executeMultiple(`
|
|
424
|
+
CREATE TABLE IF NOT EXISTS consolidations (
|
|
425
|
+
id TEXT PRIMARY KEY,
|
|
426
|
+
consolidated_memory_id TEXT NOT NULL,
|
|
427
|
+
source_memory_id TEXT NOT NULL,
|
|
428
|
+
created_at TEXT NOT NULL
|
|
429
|
+
);
|
|
430
|
+
|
|
431
|
+
CREATE INDEX IF NOT EXISTS idx_consolidations_source
|
|
432
|
+
ON consolidations(source_memory_id);
|
|
433
|
+
`);
|
|
434
|
+
await client.executeMultiple(`
|
|
435
|
+
CREATE TABLE IF NOT EXISTS audit_trail (
|
|
436
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
437
|
+
timestamp TEXT NOT NULL,
|
|
438
|
+
session_id TEXT NOT NULL,
|
|
439
|
+
agent_id TEXT NOT NULL,
|
|
440
|
+
tool TEXT NOT NULL,
|
|
441
|
+
input TEXT,
|
|
442
|
+
decision TEXT NOT NULL,
|
|
443
|
+
reason TEXT,
|
|
444
|
+
is_customer_facing INTEGER NOT NULL DEFAULT 0
|
|
445
|
+
);
|
|
446
|
+
|
|
447
|
+
CREATE INDEX IF NOT EXISTS idx_audit_trail_agent
|
|
448
|
+
ON audit_trail(agent_id, timestamp);
|
|
449
|
+
|
|
450
|
+
CREATE INDEX IF NOT EXISTS idx_audit_trail_session
|
|
451
|
+
ON audit_trail(session_id);
|
|
452
|
+
`);
|
|
453
|
+
try {
|
|
454
|
+
await client.execute({
|
|
455
|
+
sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
|
|
456
|
+
args: []
|
|
457
|
+
});
|
|
458
|
+
} catch {
|
|
637
459
|
}
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
460
|
+
try {
|
|
461
|
+
await client.execute({
|
|
462
|
+
sql: `ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5`,
|
|
463
|
+
args: []
|
|
464
|
+
});
|
|
465
|
+
} catch {
|
|
643
466
|
}
|
|
644
|
-
const resolvedPath = daemonPath;
|
|
645
|
-
process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
|
|
646
|
-
`);
|
|
647
|
-
const logPath = path4.join(path4.dirname(SOCKET_PATH), "exed.log");
|
|
648
|
-
let stderrFd = "ignore";
|
|
649
467
|
try {
|
|
650
|
-
|
|
468
|
+
await client.execute({
|
|
469
|
+
sql: `ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'`,
|
|
470
|
+
args: []
|
|
471
|
+
});
|
|
651
472
|
} catch {
|
|
652
473
|
}
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
EXE_DAEMON_PID: PID_PATH
|
|
660
|
-
}
|
|
661
|
-
});
|
|
662
|
-
child.unref();
|
|
663
|
-
if (typeof stderrFd === "number") {
|
|
664
|
-
try {
|
|
665
|
-
closeSync(stderrFd);
|
|
666
|
-
} catch {
|
|
667
|
-
}
|
|
474
|
+
try {
|
|
475
|
+
await client.execute({
|
|
476
|
+
sql: `ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7`,
|
|
477
|
+
args: []
|
|
478
|
+
});
|
|
479
|
+
} catch {
|
|
668
480
|
}
|
|
669
|
-
}
|
|
670
|
-
function acquireSpawnLock() {
|
|
671
481
|
try {
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
482
|
+
await client.execute({
|
|
483
|
+
sql: `ALTER TABLE memories ADD COLUMN last_accessed TEXT`,
|
|
484
|
+
args: []
|
|
485
|
+
});
|
|
675
486
|
} catch {
|
|
676
|
-
try {
|
|
677
|
-
const stat = statSync(SPAWN_LOCK_PATH);
|
|
678
|
-
if (Date.now() - stat.mtimeMs > SPAWN_LOCK_STALE_MS) {
|
|
679
|
-
try {
|
|
680
|
-
unlinkSync(SPAWN_LOCK_PATH);
|
|
681
|
-
} catch {
|
|
682
|
-
}
|
|
683
|
-
try {
|
|
684
|
-
const fd = openSync(SPAWN_LOCK_PATH, "wx");
|
|
685
|
-
closeSync(fd);
|
|
686
|
-
return true;
|
|
687
|
-
} catch {
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
} catch {
|
|
691
|
-
}
|
|
692
|
-
return false;
|
|
693
487
|
}
|
|
694
|
-
}
|
|
695
|
-
function releaseSpawnLock() {
|
|
696
488
|
try {
|
|
697
|
-
|
|
489
|
+
await client.execute({
|
|
490
|
+
sql: `UPDATE memories SET last_accessed = timestamp WHERE last_accessed IS NULL`,
|
|
491
|
+
args: []
|
|
492
|
+
});
|
|
698
493
|
} catch {
|
|
699
494
|
}
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
resolve(true);
|
|
705
|
-
return;
|
|
706
|
-
}
|
|
707
|
-
const socket = net.createConnection({ path: SOCKET_PATH });
|
|
708
|
-
const connectTimeout = setTimeout(() => {
|
|
709
|
-
socket.destroy();
|
|
710
|
-
resolve(false);
|
|
711
|
-
}, 2e3);
|
|
712
|
-
socket.on("connect", () => {
|
|
713
|
-
clearTimeout(connectTimeout);
|
|
714
|
-
_socket = socket;
|
|
715
|
-
_connected = true;
|
|
716
|
-
_buffer = "";
|
|
717
|
-
socket.on("data", handleData);
|
|
718
|
-
socket.on("close", () => {
|
|
719
|
-
_connected = false;
|
|
720
|
-
_socket = null;
|
|
721
|
-
for (const [id, entry] of _pending) {
|
|
722
|
-
clearTimeout(entry.timer);
|
|
723
|
-
_pending.delete(id);
|
|
724
|
-
entry.resolve({ error: "Connection closed" });
|
|
725
|
-
}
|
|
726
|
-
});
|
|
727
|
-
socket.on("error", () => {
|
|
728
|
-
_connected = false;
|
|
729
|
-
_socket = null;
|
|
730
|
-
});
|
|
731
|
-
resolve(true);
|
|
732
|
-
});
|
|
733
|
-
socket.on("error", () => {
|
|
734
|
-
clearTimeout(connectTimeout);
|
|
735
|
-
resolve(false);
|
|
495
|
+
try {
|
|
496
|
+
await client.execute({
|
|
497
|
+
sql: `ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0`,
|
|
498
|
+
args: []
|
|
736
499
|
});
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
async function connectEmbedDaemon() {
|
|
740
|
-
if (_socket && _connected) return true;
|
|
741
|
-
if (await connectToSocket()) return true;
|
|
742
|
-
if (acquireSpawnLock()) {
|
|
743
|
-
try {
|
|
744
|
-
cleanupStaleFiles();
|
|
745
|
-
spawnDaemon();
|
|
746
|
-
} finally {
|
|
747
|
-
releaseSpawnLock();
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
const start = Date.now();
|
|
751
|
-
let delay = 100;
|
|
752
|
-
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
753
|
-
await new Promise((r) => setTimeout(r, delay));
|
|
754
|
-
if (await connectToSocket()) return true;
|
|
755
|
-
delay = Math.min(delay * 2, 3e3);
|
|
500
|
+
} catch {
|
|
756
501
|
}
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
if (!_socket || !_connected) {
|
|
762
|
-
resolve({ error: "Not connected" });
|
|
763
|
-
return;
|
|
764
|
-
}
|
|
765
|
-
const id = randomUUID();
|
|
766
|
-
const timer = setTimeout(() => {
|
|
767
|
-
_pending.delete(id);
|
|
768
|
-
resolve({ error: "Request timeout" });
|
|
769
|
-
}, REQUEST_TIMEOUT_MS);
|
|
770
|
-
_pending.set(id, { resolve, timer });
|
|
771
|
-
try {
|
|
772
|
-
_socket.write(JSON.stringify({ id, texts, priority }) + "\n");
|
|
773
|
-
} catch {
|
|
774
|
-
clearTimeout(timer);
|
|
775
|
-
_pending.delete(id);
|
|
776
|
-
resolve({ error: "Write failed" });
|
|
777
|
-
}
|
|
778
|
-
});
|
|
779
|
-
}
|
|
780
|
-
async function pingDaemon() {
|
|
781
|
-
if (!_socket || !_connected) return null;
|
|
782
|
-
return new Promise((resolve) => {
|
|
783
|
-
const id = randomUUID();
|
|
784
|
-
const timer = setTimeout(() => {
|
|
785
|
-
_pending.delete(id);
|
|
786
|
-
resolve(null);
|
|
787
|
-
}, 5e3);
|
|
788
|
-
_pending.set(id, {
|
|
789
|
-
resolve: (data) => {
|
|
790
|
-
if (data.health) {
|
|
791
|
-
resolve(data.health);
|
|
792
|
-
} else {
|
|
793
|
-
resolve(null);
|
|
794
|
-
}
|
|
795
|
-
},
|
|
796
|
-
timer
|
|
502
|
+
try {
|
|
503
|
+
await client.execute({
|
|
504
|
+
sql: `ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0`,
|
|
505
|
+
args: []
|
|
797
506
|
});
|
|
507
|
+
} catch {
|
|
508
|
+
}
|
|
509
|
+
for (const col of [
|
|
510
|
+
"ALTER TABLE memories ADD COLUMN content_hash TEXT",
|
|
511
|
+
"ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT"
|
|
512
|
+
]) {
|
|
798
513
|
try {
|
|
799
|
-
|
|
514
|
+
await client.execute(col);
|
|
800
515
|
} catch {
|
|
801
|
-
clearTimeout(timer);
|
|
802
|
-
_pending.delete(id);
|
|
803
|
-
resolve(null);
|
|
804
516
|
}
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
517
|
+
}
|
|
518
|
+
await client.executeMultiple(`
|
|
519
|
+
CREATE TABLE IF NOT EXISTS entities (
|
|
520
|
+
id TEXT PRIMARY KEY,
|
|
521
|
+
name TEXT NOT NULL,
|
|
522
|
+
type TEXT NOT NULL,
|
|
523
|
+
first_seen TEXT NOT NULL,
|
|
524
|
+
last_seen TEXT NOT NULL,
|
|
525
|
+
properties TEXT DEFAULT '{}',
|
|
526
|
+
UNIQUE(name, type)
|
|
527
|
+
);
|
|
528
|
+
|
|
529
|
+
CREATE TABLE IF NOT EXISTS relationships (
|
|
530
|
+
id TEXT PRIMARY KEY,
|
|
531
|
+
source_entity_id TEXT NOT NULL,
|
|
532
|
+
target_entity_id TEXT NOT NULL,
|
|
533
|
+
type TEXT NOT NULL,
|
|
534
|
+
weight REAL DEFAULT 1.0,
|
|
535
|
+
timestamp TEXT NOT NULL,
|
|
536
|
+
properties TEXT DEFAULT '{}',
|
|
537
|
+
UNIQUE(source_entity_id, target_entity_id, type)
|
|
538
|
+
);
|
|
539
|
+
|
|
540
|
+
CREATE TABLE IF NOT EXISTS entity_memories (
|
|
541
|
+
entity_id TEXT NOT NULL,
|
|
542
|
+
memory_id TEXT NOT NULL,
|
|
543
|
+
PRIMARY KEY (entity_id, memory_id)
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
CREATE TABLE IF NOT EXISTS relationship_memories (
|
|
547
|
+
relationship_id TEXT NOT NULL,
|
|
548
|
+
memory_id TEXT NOT NULL,
|
|
549
|
+
PRIMARY KEY (relationship_id, memory_id)
|
|
550
|
+
);
|
|
551
|
+
|
|
552
|
+
CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
|
|
553
|
+
CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
|
|
554
|
+
CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
|
|
555
|
+
CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
|
|
556
|
+
|
|
557
|
+
CREATE TABLE IF NOT EXISTS hyperedges (
|
|
558
|
+
id TEXT PRIMARY KEY,
|
|
559
|
+
label TEXT NOT NULL,
|
|
560
|
+
relation TEXT NOT NULL,
|
|
561
|
+
confidence REAL DEFAULT 1.0,
|
|
562
|
+
timestamp TEXT NOT NULL
|
|
563
|
+
);
|
|
564
|
+
|
|
565
|
+
CREATE TABLE IF NOT EXISTS hyperedge_nodes (
|
|
566
|
+
hyperedge_id TEXT NOT NULL,
|
|
567
|
+
entity_id TEXT NOT NULL,
|
|
568
|
+
PRIMARY KEY (hyperedge_id, entity_id)
|
|
569
|
+
);
|
|
570
|
+
`);
|
|
571
|
+
await client.executeMultiple(`
|
|
572
|
+
CREATE TABLE IF NOT EXISTS entity_aliases (
|
|
573
|
+
alias TEXT NOT NULL PRIMARY KEY,
|
|
574
|
+
canonical_entity_id TEXT NOT NULL
|
|
575
|
+
);
|
|
576
|
+
CREATE INDEX IF NOT EXISTS idx_entity_aliases_canonical ON entity_aliases(canonical_entity_id);
|
|
577
|
+
`);
|
|
578
|
+
for (const col of [
|
|
579
|
+
"ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
|
|
580
|
+
"ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
|
|
581
|
+
]) {
|
|
810
582
|
try {
|
|
811
|
-
|
|
812
|
-
if (pid > 0) {
|
|
813
|
-
try {
|
|
814
|
-
process.kill(pid, "SIGKILL");
|
|
815
|
-
} catch {
|
|
816
|
-
}
|
|
817
|
-
}
|
|
583
|
+
await client.execute(col);
|
|
818
584
|
} catch {
|
|
819
585
|
}
|
|
820
586
|
}
|
|
821
|
-
if (_socket) {
|
|
822
|
-
_socket.destroy();
|
|
823
|
-
_socket = null;
|
|
824
|
-
}
|
|
825
|
-
_connected = false;
|
|
826
|
-
_buffer = "";
|
|
827
|
-
try {
|
|
828
|
-
unlinkSync(PID_PATH);
|
|
829
|
-
} catch {
|
|
830
|
-
}
|
|
831
587
|
try {
|
|
832
|
-
|
|
588
|
+
await client.execute(
|
|
589
|
+
`CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)`
|
|
590
|
+
);
|
|
833
591
|
} catch {
|
|
834
592
|
}
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
process.stderr.write(`[exed-client] Periodic health check failed at request ${_requestCount} \u2014 restarting daemon
|
|
844
|
-
`);
|
|
845
|
-
killAndRespawnDaemon();
|
|
846
|
-
const start = Date.now();
|
|
847
|
-
let delay = 200;
|
|
848
|
-
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
849
|
-
await new Promise((r) => setTimeout(r, delay));
|
|
850
|
-
if (await connectToSocket()) break;
|
|
851
|
-
delay = Math.min(delay * 2, 3e3);
|
|
852
|
-
}
|
|
853
|
-
if (!_connected) return null;
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
const result = await sendRequest([text], priority);
|
|
857
|
-
if (!result.error && result.vectors?.[0]) return result.vectors[0];
|
|
858
|
-
if (result.error) {
|
|
859
|
-
process.stderr.write(`[exed-client] Embed failed (${result.error}) \u2014 attempting restart
|
|
860
|
-
`);
|
|
861
|
-
killAndRespawnDaemon();
|
|
862
|
-
const start = Date.now();
|
|
863
|
-
let delay = 200;
|
|
864
|
-
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
865
|
-
await new Promise((r) => setTimeout(r, delay));
|
|
866
|
-
if (await connectToSocket()) break;
|
|
867
|
-
delay = Math.min(delay * 2, 3e3);
|
|
868
|
-
}
|
|
869
|
-
if (!_connected) return null;
|
|
870
|
-
const retry = await sendRequest([text], priority);
|
|
871
|
-
if (!retry.error && retry.vectors?.[0]) return retry.vectors[0];
|
|
872
|
-
process.stderr.write(`[exed-client] Embed retry also failed: ${retry.error ?? "no vector"}
|
|
873
|
-
`);
|
|
874
|
-
}
|
|
875
|
-
return null;
|
|
876
|
-
}
|
|
877
|
-
function disconnectClient() {
|
|
878
|
-
if (_socket) {
|
|
879
|
-
_socket.destroy();
|
|
880
|
-
_socket = null;
|
|
881
|
-
}
|
|
882
|
-
_connected = false;
|
|
883
|
-
_buffer = "";
|
|
884
|
-
for (const [id, entry] of _pending) {
|
|
885
|
-
clearTimeout(entry.timer);
|
|
886
|
-
_pending.delete(id);
|
|
887
|
-
entry.resolve({ error: "Client disconnected" });
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
var SOCKET_PATH, PID_PATH, SPAWN_LOCK_PATH, SPAWN_LOCK_STALE_MS, CONNECT_TIMEOUT_MS, REQUEST_TIMEOUT_MS, _socket, _connected, _buffer, _requestCount, HEALTH_CHECK_INTERVAL, _pending;
|
|
891
|
-
var init_exe_daemon_client = __esm({
|
|
892
|
-
"src/lib/exe-daemon-client.ts"() {
|
|
893
|
-
"use strict";
|
|
894
|
-
init_config();
|
|
895
|
-
SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ?? path4.join(EXE_AI_DIR, "exed.sock");
|
|
896
|
-
PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ?? path4.join(EXE_AI_DIR, "exed.pid");
|
|
897
|
-
SPAWN_LOCK_PATH = path4.join(EXE_AI_DIR, "exed-spawn.lock");
|
|
898
|
-
SPAWN_LOCK_STALE_MS = 3e4;
|
|
899
|
-
CONNECT_TIMEOUT_MS = 15e3;
|
|
900
|
-
REQUEST_TIMEOUT_MS = 3e4;
|
|
901
|
-
_socket = null;
|
|
902
|
-
_connected = false;
|
|
903
|
-
_buffer = "";
|
|
904
|
-
_requestCount = 0;
|
|
905
|
-
HEALTH_CHECK_INTERVAL = 100;
|
|
906
|
-
_pending = /* @__PURE__ */ new Map();
|
|
907
|
-
}
|
|
908
|
-
});
|
|
593
|
+
await client.executeMultiple(`
|
|
594
|
+
CREATE TABLE IF NOT EXISTS identity (
|
|
595
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
596
|
+
agent_id TEXT NOT NULL UNIQUE,
|
|
597
|
+
content_hash TEXT NOT NULL,
|
|
598
|
+
updated_at TEXT NOT NULL,
|
|
599
|
+
updated_by TEXT NOT NULL
|
|
600
|
+
);
|
|
909
601
|
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
"Could not connect to embedding daemon. Ensure the model is installed (run /exe-setup)."
|
|
602
|
+
CREATE INDEX IF NOT EXISTS idx_identity_agent ON identity(agent_id);
|
|
603
|
+
`);
|
|
604
|
+
await client.executeMultiple(`
|
|
605
|
+
CREATE TABLE IF NOT EXISTS chat_history (
|
|
606
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
607
|
+
session_id TEXT NOT NULL,
|
|
608
|
+
role TEXT NOT NULL,
|
|
609
|
+
content TEXT NOT NULL,
|
|
610
|
+
tool_name TEXT,
|
|
611
|
+
tool_id TEXT,
|
|
612
|
+
is_error INTEGER NOT NULL DEFAULT 0,
|
|
613
|
+
timestamp INTEGER NOT NULL
|
|
923
614
|
);
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
615
|
+
|
|
616
|
+
CREATE INDEX IF NOT EXISTS idx_chat_history_session
|
|
617
|
+
ON chat_history(session_id, id);
|
|
618
|
+
`);
|
|
619
|
+
await client.executeMultiple(`
|
|
620
|
+
CREATE TABLE IF NOT EXISTS workspaces (
|
|
621
|
+
id TEXT PRIMARY KEY,
|
|
622
|
+
slug TEXT NOT NULL UNIQUE,
|
|
623
|
+
name TEXT NOT NULL,
|
|
624
|
+
owner_agent_id TEXT,
|
|
625
|
+
created_at TEXT NOT NULL,
|
|
626
|
+
metadata TEXT
|
|
932
627
|
);
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
628
|
+
|
|
629
|
+
CREATE INDEX IF NOT EXISTS idx_workspaces_slug
|
|
630
|
+
ON workspaces(slug);
|
|
631
|
+
`);
|
|
632
|
+
await client.executeMultiple(`
|
|
633
|
+
CREATE TABLE IF NOT EXISTS documents (
|
|
634
|
+
id TEXT PRIMARY KEY,
|
|
635
|
+
workspace_id TEXT NOT NULL,
|
|
636
|
+
filename TEXT NOT NULL,
|
|
637
|
+
mime TEXT,
|
|
638
|
+
source_type TEXT,
|
|
639
|
+
user_id TEXT,
|
|
640
|
+
uploaded_at TEXT NOT NULL,
|
|
641
|
+
metadata TEXT,
|
|
642
|
+
FOREIGN KEY (workspace_id) REFERENCES workspaces(id)
|
|
937
643
|
);
|
|
644
|
+
|
|
645
|
+
CREATE INDEX IF NOT EXISTS idx_documents_workspace
|
|
646
|
+
ON documents(workspace_id);
|
|
647
|
+
|
|
648
|
+
CREATE INDEX IF NOT EXISTS idx_documents_user
|
|
649
|
+
ON documents(user_id);
|
|
650
|
+
`);
|
|
651
|
+
for (const column of [
|
|
652
|
+
"workspace_id TEXT",
|
|
653
|
+
"document_id TEXT",
|
|
654
|
+
"user_id TEXT",
|
|
655
|
+
"char_offset INTEGER",
|
|
656
|
+
"page_number INTEGER"
|
|
657
|
+
]) {
|
|
658
|
+
try {
|
|
659
|
+
await client.execute({
|
|
660
|
+
sql: `ALTER TABLE memories ADD COLUMN ${column}`,
|
|
661
|
+
args: []
|
|
662
|
+
});
|
|
663
|
+
} catch {
|
|
664
|
+
}
|
|
938
665
|
}
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
const { existsSync: existsSync7 } = await import("fs");
|
|
948
|
-
const path8 = await import("path");
|
|
949
|
-
const modelPath = path8.join(MODELS_DIR2, "jina-embeddings-v5-small-q4_k_m.gguf");
|
|
950
|
-
if (!existsSync7(modelPath)) {
|
|
951
|
-
throw new Error(`Embedding model not found at ${modelPath}. Run '/exe-setup' to download it.`);
|
|
666
|
+
for (const col of [
|
|
667
|
+
"ALTER TABLE memories ADD COLUMN source_path TEXT",
|
|
668
|
+
"ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'"
|
|
669
|
+
]) {
|
|
670
|
+
try {
|
|
671
|
+
await client.execute(col);
|
|
672
|
+
} catch {
|
|
673
|
+
}
|
|
952
674
|
}
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
675
|
+
await client.executeMultiple(`
|
|
676
|
+
CREATE INDEX IF NOT EXISTS idx_memories_workspace
|
|
677
|
+
ON memories(workspace_id);
|
|
678
|
+
|
|
679
|
+
CREATE INDEX IF NOT EXISTS idx_memories_document
|
|
680
|
+
ON memories(document_id);
|
|
681
|
+
|
|
682
|
+
CREATE INDEX IF NOT EXISTS idx_memories_user
|
|
683
|
+
ON memories(user_id);
|
|
684
|
+
`);
|
|
685
|
+
await client.executeMultiple(`
|
|
686
|
+
CREATE TABLE IF NOT EXISTS session_kills (
|
|
687
|
+
id TEXT PRIMARY KEY,
|
|
688
|
+
session_name TEXT NOT NULL,
|
|
689
|
+
agent_id TEXT NOT NULL,
|
|
690
|
+
killed_at TIMESTAMP NOT NULL,
|
|
691
|
+
reason TEXT NOT NULL,
|
|
692
|
+
ticks_idle INTEGER,
|
|
693
|
+
estimated_tokens_saved INTEGER
|
|
694
|
+
);
|
|
695
|
+
|
|
696
|
+
CREATE INDEX IF NOT EXISTS idx_session_kills_killed_at
|
|
697
|
+
ON session_kills(killed_at);
|
|
698
|
+
|
|
699
|
+
CREATE INDEX IF NOT EXISTS idx_session_kills_agent
|
|
700
|
+
ON session_kills(agent_id);
|
|
701
|
+
`);
|
|
702
|
+
await client.executeMultiple(`
|
|
703
|
+
CREATE TABLE IF NOT EXISTS conversations (
|
|
704
|
+
id TEXT PRIMARY KEY,
|
|
705
|
+
platform TEXT NOT NULL,
|
|
706
|
+
external_id TEXT,
|
|
707
|
+
sender_id TEXT NOT NULL,
|
|
708
|
+
sender_name TEXT,
|
|
709
|
+
sender_phone TEXT,
|
|
710
|
+
sender_email TEXT,
|
|
711
|
+
recipient_id TEXT,
|
|
712
|
+
channel_id TEXT NOT NULL,
|
|
713
|
+
thread_id TEXT,
|
|
714
|
+
reply_to_id TEXT,
|
|
715
|
+
content_text TEXT,
|
|
716
|
+
content_media TEXT,
|
|
717
|
+
content_metadata TEXT,
|
|
718
|
+
agent_response TEXT,
|
|
719
|
+
agent_name TEXT,
|
|
720
|
+
timestamp TEXT NOT NULL,
|
|
721
|
+
ingested_at TEXT NOT NULL
|
|
722
|
+
);
|
|
723
|
+
|
|
724
|
+
CREATE INDEX IF NOT EXISTS idx_conversations_platform
|
|
725
|
+
ON conversations(platform);
|
|
726
|
+
|
|
727
|
+
CREATE INDEX IF NOT EXISTS idx_conversations_sender
|
|
728
|
+
ON conversations(sender_id);
|
|
729
|
+
|
|
730
|
+
CREATE INDEX IF NOT EXISTS idx_conversations_timestamp
|
|
731
|
+
ON conversations(timestamp);
|
|
732
|
+
|
|
733
|
+
CREATE INDEX IF NOT EXISTS idx_conversations_thread
|
|
734
|
+
ON conversations(thread_id);
|
|
735
|
+
|
|
736
|
+
CREATE INDEX IF NOT EXISTS idx_conversations_channel
|
|
737
|
+
ON conversations(channel_id);
|
|
738
|
+
`);
|
|
956
739
|
try {
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
);
|
|
963
|
-
}
|
|
964
|
-
return vector;
|
|
965
|
-
} finally {
|
|
966
|
-
await context.dispose();
|
|
967
|
-
await model.dispose();
|
|
740
|
+
await client.execute({
|
|
741
|
+
sql: `ALTER TABLE tasks ADD COLUMN budget_tokens INTEGER`,
|
|
742
|
+
args: []
|
|
743
|
+
});
|
|
744
|
+
} catch {
|
|
968
745
|
}
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
746
|
+
try {
|
|
747
|
+
await client.execute({
|
|
748
|
+
sql: `ALTER TABLE tasks ADD COLUMN budget_fallback_model TEXT`,
|
|
749
|
+
args: []
|
|
750
|
+
});
|
|
751
|
+
} catch {
|
|
975
752
|
}
|
|
976
|
-
});
|
|
977
|
-
|
|
978
|
-
// src/lib/project-name.ts
|
|
979
|
-
var project_name_exports = {};
|
|
980
|
-
__export(project_name_exports, {
|
|
981
|
-
_resetCache: () => _resetCache,
|
|
982
|
-
getProjectName: () => getProjectName
|
|
983
|
-
});
|
|
984
|
-
import { execSync } from "child_process";
|
|
985
|
-
import path5 from "path";
|
|
986
|
-
function getProjectName(cwd) {
|
|
987
|
-
const dir = cwd ?? process.cwd();
|
|
988
|
-
if (_cached && _cachedCwd === dir) return _cached;
|
|
989
753
|
try {
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
995
|
-
}).trim();
|
|
996
|
-
_cached = path5.basename(repoRoot);
|
|
997
|
-
_cachedCwd = dir;
|
|
998
|
-
return _cached;
|
|
754
|
+
await client.execute({
|
|
755
|
+
sql: `ALTER TABLE tasks ADD COLUMN tokens_used INTEGER DEFAULT 0`,
|
|
756
|
+
args: []
|
|
757
|
+
});
|
|
999
758
|
} catch {
|
|
1000
|
-
_cached = path5.basename(dir);
|
|
1001
|
-
_cachedCwd = dir;
|
|
1002
|
-
return _cached;
|
|
1003
759
|
}
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
|
-
var init_project_name = __esm({
|
|
1011
|
-
"src/lib/project-name.ts"() {
|
|
1012
|
-
"use strict";
|
|
1013
|
-
_cached = null;
|
|
1014
|
-
_cachedCwd = null;
|
|
760
|
+
try {
|
|
761
|
+
await client.execute({
|
|
762
|
+
sql: `ALTER TABLE tasks ADD COLUMN tokens_warned_at INTEGER`,
|
|
763
|
+
args: []
|
|
764
|
+
});
|
|
765
|
+
} catch {
|
|
1015
766
|
}
|
|
1016
|
-
|
|
767
|
+
await client.executeMultiple(`
|
|
768
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS conversations_fts USING fts5(
|
|
769
|
+
content_text,
|
|
770
|
+
sender_name,
|
|
771
|
+
agent_response,
|
|
772
|
+
content='conversations',
|
|
773
|
+
content_rowid='rowid'
|
|
774
|
+
);
|
|
1017
775
|
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
776
|
+
CREATE TRIGGER IF NOT EXISTS conversations_fts_ai AFTER INSERT ON conversations BEGIN
|
|
777
|
+
INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
|
|
778
|
+
VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
|
|
779
|
+
END;
|
|
780
|
+
|
|
781
|
+
CREATE TRIGGER IF NOT EXISTS conversations_fts_ad AFTER DELETE ON conversations BEGIN
|
|
782
|
+
INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
|
|
783
|
+
VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
|
|
784
|
+
END;
|
|
785
|
+
|
|
786
|
+
CREATE TRIGGER IF NOT EXISTS conversations_fts_au AFTER UPDATE ON conversations BEGIN
|
|
787
|
+
INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
|
|
788
|
+
VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
|
|
789
|
+
INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
|
|
790
|
+
VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
|
|
791
|
+
END;
|
|
792
|
+
`);
|
|
1033
793
|
try {
|
|
1034
|
-
|
|
794
|
+
await client.execute({
|
|
795
|
+
sql: `ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3`,
|
|
796
|
+
args: []
|
|
797
|
+
});
|
|
1035
798
|
} catch {
|
|
1036
|
-
hits = grepWithNodeFs(pattern, projectRoot, options?.patterns);
|
|
1037
799
|
}
|
|
1038
|
-
hits.sort((a, b) => b.density - a.density);
|
|
1039
|
-
const topHits = hits.slice(0, maxResults);
|
|
1040
|
-
return topHits.map((hit) => {
|
|
1041
|
-
const chunkCtx = getChunkContext(hit.filePath, hit.lineNumber);
|
|
1042
|
-
const prefix = chunkCtx ? `[file: ${hit.filePath}:${hit.lineNumber} in ${chunkCtx}]` : `[file: ${hit.filePath}:${hit.lineNumber}]`;
|
|
1043
|
-
return {
|
|
1044
|
-
id: crypto2.createHash("sha256").update(`${hit.filePath}:${hit.lineNumber}`).digest("hex").slice(0, 36),
|
|
1045
|
-
agent_id: "project",
|
|
1046
|
-
agent_role: "file",
|
|
1047
|
-
session_id: "file-grep",
|
|
1048
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1049
|
-
tool_name: "file_grep",
|
|
1050
|
-
project_name: path6.basename(projectRoot),
|
|
1051
|
-
has_error: false,
|
|
1052
|
-
raw_text: `${prefix} ${buildSnippet(hit, projectRoot)}`,
|
|
1053
|
-
vector: null,
|
|
1054
|
-
task_id: null
|
|
1055
|
-
};
|
|
1056
|
-
});
|
|
1057
|
-
}
|
|
1058
|
-
function getChunkContext(filePath, lineNumber) {
|
|
1059
800
|
try {
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
const lines = source.split("\n");
|
|
1064
|
-
for (let i = Math.min(lineNumber - 1, lines.length - 1); i >= 0; i--) {
|
|
1065
|
-
const line = lines[i];
|
|
1066
|
-
const fnMatch = line.match(/(?:export\s+)?(?:async\s+)?function\s+(\w+)/);
|
|
1067
|
-
if (fnMatch) return `function ${fnMatch[1]}`;
|
|
1068
|
-
const classMatch = line.match(/(?:export\s+)?class\s+(\w+)/);
|
|
1069
|
-
if (classMatch) return `class ${classMatch[1]}`;
|
|
1070
|
-
const arrowMatch = line.match(/(?:export\s+)?(?:const|let)\s+(\w+)\s*=\s*(?:async\s+)?\(/);
|
|
1071
|
-
if (arrowMatch) return `function ${arrowMatch[1]}`;
|
|
1072
|
-
}
|
|
801
|
+
await client.execute(
|
|
802
|
+
`CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)`
|
|
803
|
+
);
|
|
1073
804
|
} catch {
|
|
1074
805
|
}
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
maxBuffer: 1024 * 1024
|
|
1086
|
-
});
|
|
1087
|
-
if (!output.trim()) return [];
|
|
1088
|
-
const hits = [];
|
|
1089
|
-
for (const line of output.trim().split("\n")) {
|
|
1090
|
-
const colonIdx = line.lastIndexOf(":");
|
|
1091
|
-
if (colonIdx === -1) continue;
|
|
1092
|
-
const filePath = line.slice(0, colonIdx);
|
|
1093
|
-
const matchCount = parseInt(line.slice(colonIdx + 1));
|
|
1094
|
-
if (isNaN(matchCount) || matchCount === 0) continue;
|
|
1095
|
-
try {
|
|
1096
|
-
const firstMatch = execSync2(
|
|
1097
|
-
`rg -i -n --hidden '${pattern.replace(/'/g, "\\'")}' '${filePath}' --max-count 1 2>/dev/null | head -1`,
|
|
1098
|
-
{ cwd: projectRoot, encoding: "utf8", timeout: 1e3 }
|
|
1099
|
-
).trim();
|
|
1100
|
-
const lineNum = parseInt(firstMatch.split(":")[0] ?? "1");
|
|
1101
|
-
const totalLines = execSync2(`wc -l < '${filePath}'`, {
|
|
1102
|
-
cwd: projectRoot,
|
|
1103
|
-
encoding: "utf8",
|
|
1104
|
-
timeout: 1e3
|
|
1105
|
-
}).trim();
|
|
1106
|
-
hits.push({
|
|
1107
|
-
filePath,
|
|
1108
|
-
lineNumber: isNaN(lineNum) ? 1 : lineNum,
|
|
1109
|
-
matchLine: firstMatch.slice(firstMatch.indexOf(":") + 1).slice(0, 200),
|
|
1110
|
-
matchCount,
|
|
1111
|
-
totalLines: parseInt(totalLines) || 1,
|
|
1112
|
-
density: matchCount / (parseInt(totalLines) || 1)
|
|
1113
|
-
});
|
|
1114
|
-
} catch {
|
|
1115
|
-
}
|
|
806
|
+
try {
|
|
807
|
+
await client.execute({
|
|
808
|
+
sql: `UPDATE memories SET tier = 1 WHERE tool_name = 'commit_to_long_term_memory' AND importance >= 8 AND tier = 3`,
|
|
809
|
+
args: []
|
|
810
|
+
});
|
|
811
|
+
await client.execute({
|
|
812
|
+
sql: `UPDATE memories SET tier = 2 WHERE tool_name IN ('store_memory', 'manual') AND importance >= 5 AND tier = 3`,
|
|
813
|
+
args: []
|
|
814
|
+
});
|
|
815
|
+
} catch {
|
|
1116
816
|
}
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
for (const filePath of files.slice(0, MAX_FILES)) {
|
|
1124
|
-
const absPath = path6.join(projectRoot, filePath);
|
|
1125
|
-
try {
|
|
1126
|
-
const stat = statSync2(absPath);
|
|
1127
|
-
if (stat.size > MAX_FILE_SIZE) continue;
|
|
1128
|
-
const content = readFileSync3(absPath, "utf8");
|
|
1129
|
-
const lines = content.split("\n");
|
|
1130
|
-
const matches = content.match(regex);
|
|
1131
|
-
if (!matches || matches.length === 0) continue;
|
|
1132
|
-
const firstMatchIdx = lines.findIndex((l) => regex.test(l));
|
|
1133
|
-
regex.lastIndex = 0;
|
|
1134
|
-
hits.push({
|
|
1135
|
-
filePath,
|
|
1136
|
-
lineNumber: firstMatchIdx >= 0 ? firstMatchIdx + 1 : 1,
|
|
1137
|
-
matchLine: firstMatchIdx >= 0 ? lines[firstMatchIdx].slice(0, 200) : "",
|
|
1138
|
-
matchCount: matches.length,
|
|
1139
|
-
totalLines: lines.length,
|
|
1140
|
-
density: matches.length / lines.length
|
|
1141
|
-
});
|
|
1142
|
-
} catch {
|
|
1143
|
-
}
|
|
817
|
+
try {
|
|
818
|
+
await client.execute({
|
|
819
|
+
sql: `ALTER TABLE memories ADD COLUMN supersedes_id TEXT`,
|
|
820
|
+
args: []
|
|
821
|
+
});
|
|
822
|
+
} catch {
|
|
1144
823
|
}
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
824
|
+
try {
|
|
825
|
+
await client.execute(
|
|
826
|
+
`CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL`
|
|
827
|
+
);
|
|
828
|
+
} catch {
|
|
829
|
+
}
|
|
830
|
+
for (const col of [
|
|
831
|
+
"ALTER TABLE tasks ADD COLUMN checkpoint TEXT",
|
|
832
|
+
"ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER DEFAULT 0"
|
|
833
|
+
]) {
|
|
1153
834
|
try {
|
|
1154
|
-
|
|
1155
|
-
for (const entry of entries) {
|
|
1156
|
-
if (files.length >= MAX_FILES) return;
|
|
1157
|
-
const rel = path6.join(relative, entry.name);
|
|
1158
|
-
if (entry.isDirectory()) {
|
|
1159
|
-
walk(path6.join(dir, entry.name), rel);
|
|
1160
|
-
} else if (entry.isFile()) {
|
|
1161
|
-
for (const pat of patterns) {
|
|
1162
|
-
if (matchGlob(rel, pat)) {
|
|
1163
|
-
files.push(rel);
|
|
1164
|
-
break;
|
|
1165
|
-
}
|
|
1166
|
-
}
|
|
1167
|
-
}
|
|
1168
|
-
}
|
|
835
|
+
await client.execute(col);
|
|
1169
836
|
} catch {
|
|
1170
837
|
}
|
|
1171
838
|
}
|
|
1172
|
-
walk(root, "");
|
|
1173
|
-
return files;
|
|
1174
839
|
}
|
|
1175
|
-
function
|
|
1176
|
-
if (
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
const dir = doubleStarMatch[1];
|
|
1180
|
-
const ext2 = doubleStarMatch[2];
|
|
1181
|
-
return filePath.startsWith(dir + "/") && filePath.endsWith(ext2);
|
|
1182
|
-
}
|
|
1183
|
-
if (pattern.startsWith("**/")) {
|
|
1184
|
-
const ext2 = pattern.slice(3).replace("*", "");
|
|
1185
|
-
return filePath.endsWith(ext2);
|
|
840
|
+
async function disposeDatabase() {
|
|
841
|
+
if (_client) {
|
|
842
|
+
_client.close();
|
|
843
|
+
_client = null;
|
|
1186
844
|
}
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
845
|
+
}
|
|
846
|
+
var _client, initTurso, disposeTurso;
|
|
847
|
+
var init_database = __esm({
|
|
848
|
+
"src/lib/database.ts"() {
|
|
849
|
+
"use strict";
|
|
850
|
+
_client = null;
|
|
851
|
+
initTurso = initDatabase;
|
|
852
|
+
disposeTurso = disposeDatabase;
|
|
1193
853
|
}
|
|
1194
|
-
|
|
1195
|
-
|
|
854
|
+
});
|
|
855
|
+
|
|
856
|
+
// src/lib/keychain.ts
|
|
857
|
+
import { readFile, writeFile, unlink, mkdir, chmod } from "fs/promises";
|
|
858
|
+
import { existsSync } from "fs";
|
|
859
|
+
import path from "path";
|
|
860
|
+
import crypto from "crypto";
|
|
861
|
+
function getKeyDir() {
|
|
862
|
+
return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? path.join(process.env.HOME ?? "/tmp", ".exe-os");
|
|
1196
863
|
}
|
|
1197
|
-
function
|
|
864
|
+
function getKeyPath() {
|
|
865
|
+
return path.join(getKeyDir(), "master.key");
|
|
866
|
+
}
|
|
867
|
+
async function tryKeytar() {
|
|
1198
868
|
try {
|
|
1199
|
-
|
|
1200
|
-
if (!existsSync5(absPath)) return hit.matchLine;
|
|
1201
|
-
const lines = readFileSync3(absPath, "utf8").split("\n");
|
|
1202
|
-
const start = Math.max(0, hit.lineNumber - 3);
|
|
1203
|
-
const end = Math.min(lines.length, hit.lineNumber + 2);
|
|
1204
|
-
return lines.slice(start, end).join("\n").slice(0, 500);
|
|
869
|
+
return await import("keytar");
|
|
1205
870
|
} catch {
|
|
1206
|
-
return
|
|
871
|
+
return null;
|
|
1207
872
|
}
|
|
1208
873
|
}
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
EXCLUDE_DIRS = ["node_modules", "dist", ".git", "coverage", ".worktrees"];
|
|
1220
|
-
MAX_FILE_SIZE = 100 * 1024;
|
|
1221
|
-
MAX_FILES = 500;
|
|
1222
|
-
}
|
|
1223
|
-
});
|
|
1224
|
-
|
|
1225
|
-
// src/lib/reranker.ts
|
|
1226
|
-
var reranker_exports = {};
|
|
1227
|
-
__export(reranker_exports, {
|
|
1228
|
-
disposeReranker: () => disposeReranker,
|
|
1229
|
-
getRerankerModelPath: () => getRerankerModelPath,
|
|
1230
|
-
isRerankerAvailable: () => isRerankerAvailable,
|
|
1231
|
-
rerank: () => rerank,
|
|
1232
|
-
rerankWithScores: () => rerankWithScores
|
|
1233
|
-
});
|
|
1234
|
-
import path7 from "path";
|
|
1235
|
-
import { existsSync as existsSync6 } from "fs";
|
|
1236
|
-
function resetIdleTimer() {
|
|
1237
|
-
if (_idleTimer) clearTimeout(_idleTimer);
|
|
1238
|
-
_idleTimer = setTimeout(() => {
|
|
1239
|
-
void disposeReranker();
|
|
1240
|
-
}, IDLE_TIMEOUT_MS);
|
|
1241
|
-
if (_idleTimer && typeof _idleTimer === "object" && "unref" in _idleTimer) {
|
|
1242
|
-
_idleTimer.unref();
|
|
874
|
+
async function getMasterKey() {
|
|
875
|
+
const keytar = await tryKeytar();
|
|
876
|
+
if (keytar) {
|
|
877
|
+
try {
|
|
878
|
+
const stored = await keytar.getPassword(SERVICE, ACCOUNT);
|
|
879
|
+
if (stored) {
|
|
880
|
+
return Buffer.from(stored, "base64");
|
|
881
|
+
}
|
|
882
|
+
} catch {
|
|
883
|
+
}
|
|
1243
884
|
}
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
}
|
|
1248
|
-
function getRerankerModelPath() {
|
|
1249
|
-
return path7.join(MODELS_DIR, RERANKER_MODEL_FILE);
|
|
1250
|
-
}
|
|
1251
|
-
async function ensureLoaded() {
|
|
1252
|
-
if (_rerankerContext) {
|
|
1253
|
-
resetIdleTimer();
|
|
1254
|
-
return;
|
|
885
|
+
const keyPath = getKeyPath();
|
|
886
|
+
if (!existsSync(keyPath)) {
|
|
887
|
+
return null;
|
|
1255
888
|
}
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
889
|
+
try {
|
|
890
|
+
const content = await readFile(keyPath, "utf-8");
|
|
891
|
+
return Buffer.from(content.trim(), "base64");
|
|
892
|
+
} catch {
|
|
893
|
+
return null;
|
|
1261
894
|
}
|
|
1262
|
-
process.stderr.write("[reranker] Loading Jina Reranker v3...\n");
|
|
1263
|
-
const { getLlama } = await import("node-llama-cpp");
|
|
1264
|
-
const llama = await getLlama();
|
|
1265
|
-
_rerankerModel = await llama.loadModel({ modelPath });
|
|
1266
|
-
_rerankerContext = await _rerankerModel.createEmbeddingContext();
|
|
1267
|
-
process.stderr.write("[reranker] Jina Reranker v3 loaded.\n");
|
|
1268
|
-
resetIdleTimer();
|
|
1269
895
|
}
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
896
|
+
var SERVICE, ACCOUNT;
|
|
897
|
+
var init_keychain = __esm({
|
|
898
|
+
"src/lib/keychain.ts"() {
|
|
899
|
+
"use strict";
|
|
900
|
+
SERVICE = "exe-mem";
|
|
901
|
+
ACCOUNT = "master-key";
|
|
1274
902
|
}
|
|
1275
|
-
|
|
903
|
+
});
|
|
904
|
+
|
|
905
|
+
// src/lib/config.ts
|
|
906
|
+
var config_exports = {};
|
|
907
|
+
__export(config_exports, {
|
|
908
|
+
CONFIG_MIGRATIONS: () => CONFIG_MIGRATIONS,
|
|
909
|
+
CONFIG_PATH: () => CONFIG_PATH,
|
|
910
|
+
CURRENT_CONFIG_VERSION: () => CURRENT_CONFIG_VERSION,
|
|
911
|
+
DB_PATH: () => DB_PATH,
|
|
912
|
+
EXE_AI_DIR: () => EXE_AI_DIR,
|
|
913
|
+
LEGACY_LANCE_PATH: () => LEGACY_LANCE_PATH,
|
|
914
|
+
MODELS_DIR: () => MODELS_DIR,
|
|
915
|
+
loadConfig: () => loadConfig,
|
|
916
|
+
loadConfigFrom: () => loadConfigFrom,
|
|
917
|
+
loadConfigSync: () => loadConfigSync,
|
|
918
|
+
migrateConfig: () => migrateConfig,
|
|
919
|
+
saveConfig: () => saveConfig
|
|
920
|
+
});
|
|
921
|
+
import { readFile as readFile2, writeFile as writeFile2, mkdir as mkdir2 } from "fs/promises";
|
|
922
|
+
import { readFileSync, existsSync as existsSync2, renameSync } from "fs";
|
|
923
|
+
import path2 from "path";
|
|
924
|
+
import os from "os";
|
|
925
|
+
function resolveDataDir() {
|
|
926
|
+
if (process.env.EXE_OS_DIR) return process.env.EXE_OS_DIR;
|
|
927
|
+
if (process.env.EXE_MEM_DIR) return process.env.EXE_MEM_DIR;
|
|
928
|
+
const newDir = path2.join(os.homedir(), ".exe-os");
|
|
929
|
+
const legacyDir = path2.join(os.homedir(), ".exe-mem");
|
|
930
|
+
if (!existsSync2(newDir) && existsSync2(legacyDir)) {
|
|
1276
931
|
try {
|
|
1277
|
-
|
|
932
|
+
renameSync(legacyDir, newDir);
|
|
933
|
+
process.stderr.write(`[exe-os] Migrated data directory: ~/.exe-mem \u2192 ~/.exe-os
|
|
934
|
+
`);
|
|
1278
935
|
} catch {
|
|
936
|
+
return legacyDir;
|
|
1279
937
|
}
|
|
1280
|
-
_rerankerContext = null;
|
|
1281
938
|
}
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
939
|
+
return newDir;
|
|
940
|
+
}
|
|
941
|
+
function migrateLegacyConfig(raw) {
|
|
942
|
+
if ("r2" in raw) {
|
|
943
|
+
process.stderr.write(
|
|
944
|
+
"[exe-os] Warning: config.json contains deprecated 'r2' field from v1.0. R2 sync has been replaced in v1.1. The 'r2' field will be ignored.\n"
|
|
945
|
+
);
|
|
946
|
+
delete raw.r2;
|
|
1288
947
|
}
|
|
1289
|
-
|
|
948
|
+
if ("syncIntervalMs" in raw) {
|
|
949
|
+
delete raw.syncIntervalMs;
|
|
950
|
+
}
|
|
951
|
+
return raw;
|
|
1290
952
|
}
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
} catch {
|
|
1304
|
-
scored.push({ text, score: -1, index: i });
|
|
953
|
+
function migrateConfig(raw) {
|
|
954
|
+
const fromVersion = typeof raw.config_version === "number" ? raw.config_version : 0;
|
|
955
|
+
let currentVersion = fromVersion;
|
|
956
|
+
let migrated = false;
|
|
957
|
+
if (currentVersion > CURRENT_CONFIG_VERSION) {
|
|
958
|
+
return { config: raw, migrated: false, fromVersion };
|
|
959
|
+
}
|
|
960
|
+
for (const migration of CONFIG_MIGRATIONS) {
|
|
961
|
+
if (currentVersion === migration.from && migration.to <= CURRENT_CONFIG_VERSION) {
|
|
962
|
+
raw = migration.migrate(raw);
|
|
963
|
+
currentVersion = migration.to;
|
|
964
|
+
migrated = true;
|
|
1305
965
|
}
|
|
1306
966
|
}
|
|
1307
|
-
|
|
1308
|
-
return typeof topK === "number" ? scored.slice(0, topK) : scored;
|
|
1309
|
-
}
|
|
1310
|
-
async function rerank(query, candidates, topK = 5) {
|
|
1311
|
-
if (candidates.length === 0) return [];
|
|
1312
|
-
if (candidates.length <= topK) return candidates;
|
|
1313
|
-
const scored = await rerankWithScores(
|
|
1314
|
-
query,
|
|
1315
|
-
candidates.map((c) => c.raw_text),
|
|
1316
|
-
topK
|
|
1317
|
-
);
|
|
1318
|
-
return scored.map((s) => candidates[s.index]);
|
|
967
|
+
return { config: raw, migrated, fromVersion };
|
|
1319
968
|
}
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
IDLE_TIMEOUT_MS = 6e4;
|
|
1327
|
-
_rerankerContext = null;
|
|
1328
|
-
_rerankerModel = null;
|
|
1329
|
-
_idleTimer = null;
|
|
1330
|
-
}
|
|
1331
|
-
});
|
|
1332
|
-
|
|
1333
|
-
// src/lib/store.ts
|
|
1334
|
-
init_memory();
|
|
1335
|
-
|
|
1336
|
-
// src/lib/database.ts
|
|
1337
|
-
import { createClient } from "@libsql/client";
|
|
1338
|
-
var _client = null;
|
|
1339
|
-
var initTurso = initDatabase;
|
|
1340
|
-
async function initDatabase(config) {
|
|
1341
|
-
if (_client) {
|
|
1342
|
-
_client.close();
|
|
1343
|
-
_client = null;
|
|
969
|
+
function normalizeScalingRoadmap(raw) {
|
|
970
|
+
const defaultAuto = DEFAULT_CONFIG.scalingRoadmap.rerankerAutoTrigger;
|
|
971
|
+
const userRoadmap = raw.scalingRoadmap ?? {};
|
|
972
|
+
const userAuto = userRoadmap.rerankerAutoTrigger ?? {};
|
|
973
|
+
if (userAuto.enabled === void 0 && raw.rerankerEnabled !== void 0) {
|
|
974
|
+
userAuto.enabled = raw.rerankerEnabled;
|
|
1344
975
|
}
|
|
1345
|
-
|
|
1346
|
-
|
|
976
|
+
raw.scalingRoadmap = {
|
|
977
|
+
...userRoadmap,
|
|
978
|
+
rerankerAutoTrigger: { ...defaultAuto, ...userAuto }
|
|
1347
979
|
};
|
|
1348
|
-
if (config.encryptionKey) {
|
|
1349
|
-
opts.encryptionKey = config.encryptionKey;
|
|
1350
|
-
}
|
|
1351
|
-
_client = createClient(opts);
|
|
1352
980
|
}
|
|
1353
|
-
function
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
}
|
|
1357
|
-
return _client;
|
|
981
|
+
function normalizeSessionLifecycle(raw) {
|
|
982
|
+
const defaultSL = DEFAULT_CONFIG.sessionLifecycle;
|
|
983
|
+
const userSL = raw.sessionLifecycle ?? {};
|
|
984
|
+
raw.sessionLifecycle = { ...defaultSL, ...userSL };
|
|
1358
985
|
}
|
|
1359
|
-
|
|
1360
|
-
const
|
|
1361
|
-
|
|
1362
|
-
|
|
986
|
+
function normalizeAutoUpdate(raw) {
|
|
987
|
+
const defaultAU = DEFAULT_CONFIG.autoUpdate;
|
|
988
|
+
const userAU = raw.autoUpdate ?? {};
|
|
989
|
+
raw.autoUpdate = { ...defaultAU, ...userAU };
|
|
990
|
+
}
|
|
991
|
+
async function loadConfig() {
|
|
992
|
+
const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
|
|
993
|
+
await mkdir2(dir, { recursive: true });
|
|
994
|
+
const configPath = path2.join(dir, "config.json");
|
|
995
|
+
if (!existsSync2(configPath)) {
|
|
996
|
+
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
|
|
997
|
+
}
|
|
998
|
+
const raw = await readFile2(configPath, "utf-8");
|
|
1363
999
|
try {
|
|
1364
|
-
|
|
1000
|
+
let parsed = JSON.parse(raw);
|
|
1001
|
+
parsed = migrateLegacyConfig(parsed);
|
|
1002
|
+
const { config: migratedCfg, migrated, fromVersion } = migrateConfig(parsed);
|
|
1003
|
+
if (migrated) {
|
|
1004
|
+
process.stderr.write(`[exe-os] Config migrated from v${fromVersion} to v${migratedCfg.config_version}
|
|
1005
|
+
`);
|
|
1006
|
+
try {
|
|
1007
|
+
await writeFile2(configPath, JSON.stringify(migratedCfg, null, 2) + "\n");
|
|
1008
|
+
} catch {
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
normalizeScalingRoadmap(migratedCfg);
|
|
1012
|
+
normalizeSessionLifecycle(migratedCfg);
|
|
1013
|
+
normalizeAutoUpdate(migratedCfg);
|
|
1014
|
+
const config = { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db"), ...migratedCfg };
|
|
1015
|
+
if (config.dbPath.startsWith("~")) {
|
|
1016
|
+
config.dbPath = config.dbPath.replace(/^~/, os.homedir());
|
|
1017
|
+
}
|
|
1018
|
+
return config;
|
|
1365
1019
|
} catch {
|
|
1020
|
+
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
|
|
1366
1021
|
}
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1022
|
+
}
|
|
1023
|
+
function loadConfigSync() {
|
|
1024
|
+
const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
|
|
1025
|
+
const configPath = path2.join(dir, "config.json");
|
|
1026
|
+
if (!existsSync2(configPath)) {
|
|
1027
|
+
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
|
|
1028
|
+
}
|
|
1029
|
+
try {
|
|
1030
|
+
const raw = readFileSync(configPath, "utf-8");
|
|
1031
|
+
let parsed = JSON.parse(raw);
|
|
1032
|
+
parsed = migrateLegacyConfig(parsed);
|
|
1033
|
+
const { config: migratedCfg } = migrateConfig(parsed);
|
|
1034
|
+
normalizeScalingRoadmap(migratedCfg);
|
|
1035
|
+
normalizeSessionLifecycle(migratedCfg);
|
|
1036
|
+
normalizeAutoUpdate(migratedCfg);
|
|
1037
|
+
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db"), ...migratedCfg };
|
|
1038
|
+
} catch {
|
|
1039
|
+
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
async function saveConfig(config) {
|
|
1043
|
+
const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
|
|
1044
|
+
await mkdir2(dir, { recursive: true });
|
|
1045
|
+
const configPath = path2.join(dir, "config.json");
|
|
1046
|
+
await writeFile2(configPath, JSON.stringify(config, null, 2) + "\n");
|
|
1047
|
+
}
|
|
1048
|
+
async function loadConfigFrom(configPath) {
|
|
1049
|
+
const raw = await readFile2(configPath, "utf-8");
|
|
1050
|
+
try {
|
|
1051
|
+
let parsed = JSON.parse(raw);
|
|
1052
|
+
parsed = migrateLegacyConfig(parsed);
|
|
1053
|
+
const { config: migratedCfg } = migrateConfig(parsed);
|
|
1054
|
+
normalizeScalingRoadmap(migratedCfg);
|
|
1055
|
+
normalizeSessionLifecycle(migratedCfg);
|
|
1056
|
+
normalizeAutoUpdate(migratedCfg);
|
|
1057
|
+
return { ...DEFAULT_CONFIG, ...migratedCfg };
|
|
1058
|
+
} catch {
|
|
1059
|
+
return { ...DEFAULT_CONFIG };
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
var EXE_AI_DIR, DB_PATH, MODELS_DIR, CONFIG_PATH, LEGACY_LANCE_PATH, CURRENT_CONFIG_VERSION, DEFAULT_CONFIG, CONFIG_MIGRATIONS;
|
|
1063
|
+
var init_config = __esm({
|
|
1064
|
+
"src/lib/config.ts"() {
|
|
1065
|
+
"use strict";
|
|
1066
|
+
EXE_AI_DIR = resolveDataDir();
|
|
1067
|
+
DB_PATH = path2.join(EXE_AI_DIR, "memories.db");
|
|
1068
|
+
MODELS_DIR = path2.join(EXE_AI_DIR, "models");
|
|
1069
|
+
CONFIG_PATH = path2.join(EXE_AI_DIR, "config.json");
|
|
1070
|
+
LEGACY_LANCE_PATH = path2.join(EXE_AI_DIR, "local.lance");
|
|
1071
|
+
CURRENT_CONFIG_VERSION = 1;
|
|
1072
|
+
DEFAULT_CONFIG = {
|
|
1073
|
+
config_version: CURRENT_CONFIG_VERSION,
|
|
1074
|
+
dbPath: DB_PATH,
|
|
1075
|
+
modelFile: "jina-embeddings-v5-small-q4_k_m.gguf",
|
|
1076
|
+
embeddingDim: 1024,
|
|
1077
|
+
batchSize: 20,
|
|
1078
|
+
flushIntervalMs: 1e4,
|
|
1079
|
+
autoIngestion: true,
|
|
1080
|
+
autoRetrieval: true,
|
|
1081
|
+
searchMode: "hybrid",
|
|
1082
|
+
hookSearchMode: "hybrid",
|
|
1083
|
+
fileGrepEnabled: true,
|
|
1084
|
+
splashEffect: true,
|
|
1085
|
+
consolidationEnabled: true,
|
|
1086
|
+
consolidationIntervalMs: 6 * 60 * 60 * 1e3,
|
|
1087
|
+
consolidationModel: "claude-haiku-4-5-20251001",
|
|
1088
|
+
consolidationMaxCallsPerRun: 20,
|
|
1089
|
+
selfQueryRouter: true,
|
|
1090
|
+
selfQueryModel: "claude-haiku-4-5-20251001",
|
|
1091
|
+
rerankerEnabled: true,
|
|
1092
|
+
scalingRoadmap: {
|
|
1093
|
+
rerankerAutoTrigger: {
|
|
1094
|
+
enabled: true,
|
|
1095
|
+
broadQueryMinCardinality: 5e4,
|
|
1096
|
+
fetchTopK: 150,
|
|
1097
|
+
returnTopK: 5
|
|
1098
|
+
}
|
|
1099
|
+
},
|
|
1100
|
+
graphRagEnabled: true,
|
|
1101
|
+
wikiEnabled: false,
|
|
1102
|
+
wikiUrl: "",
|
|
1103
|
+
wikiApiKey: "",
|
|
1104
|
+
wikiSyncIntervalMs: 30 * 60 * 1e3,
|
|
1105
|
+
wikiWorkspaceMapping: {
|
|
1106
|
+
exe: "Executive",
|
|
1107
|
+
yoshi: "Engineering",
|
|
1108
|
+
mari: "Marketing",
|
|
1109
|
+
tom: "Engineering",
|
|
1110
|
+
sasha: "Production"
|
|
1111
|
+
},
|
|
1112
|
+
wikiAutoUpdate: true,
|
|
1113
|
+
wikiAutoUpdateThreshold: 0.5,
|
|
1114
|
+
wikiAutoUpdateCreateNew: true,
|
|
1115
|
+
skillLearning: true,
|
|
1116
|
+
skillThreshold: 3,
|
|
1117
|
+
skillModel: "claude-haiku-4-5-20251001",
|
|
1118
|
+
exeHeartbeat: {
|
|
1119
|
+
enabled: true,
|
|
1120
|
+
intervalSeconds: 60,
|
|
1121
|
+
staleInProgressThresholdHours: 2
|
|
1122
|
+
},
|
|
1123
|
+
sessionLifecycle: {
|
|
1124
|
+
idleKillEnabled: true,
|
|
1125
|
+
idleKillTicksRequired: 3,
|
|
1126
|
+
idleKillIntercomAckWindowMs: 1e4,
|
|
1127
|
+
maxAutoInstances: 10
|
|
1128
|
+
},
|
|
1129
|
+
autoUpdate: {
|
|
1130
|
+
checkOnBoot: true,
|
|
1131
|
+
autoInstall: false,
|
|
1132
|
+
checkIntervalMs: 24 * 60 * 60 * 1e3
|
|
1133
|
+
}
|
|
1134
|
+
};
|
|
1135
|
+
CONFIG_MIGRATIONS = [
|
|
1136
|
+
{
|
|
1137
|
+
from: 0,
|
|
1138
|
+
to: 1,
|
|
1139
|
+
migrate: (cfg) => {
|
|
1140
|
+
cfg.config_version = 1;
|
|
1141
|
+
return cfg;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
];
|
|
1145
|
+
}
|
|
1146
|
+
});
|
|
1147
|
+
|
|
1148
|
+
// src/lib/shard-manager.ts
|
|
1149
|
+
var shard_manager_exports = {};
|
|
1150
|
+
__export(shard_manager_exports, {
|
|
1151
|
+
disposeShards: () => disposeShards,
|
|
1152
|
+
ensureShardSchema: () => ensureShardSchema,
|
|
1153
|
+
getReadyShardClient: () => getReadyShardClient,
|
|
1154
|
+
getShardClient: () => getShardClient,
|
|
1155
|
+
getShardsDir: () => getShardsDir,
|
|
1156
|
+
initShardManager: () => initShardManager,
|
|
1157
|
+
isShardingEnabled: () => isShardingEnabled,
|
|
1158
|
+
listShards: () => listShards,
|
|
1159
|
+
shardExists: () => shardExists
|
|
1160
|
+
});
|
|
1161
|
+
import path3 from "path";
|
|
1162
|
+
import { existsSync as existsSync3, mkdirSync } from "fs";
|
|
1163
|
+
import { createClient as createClient2 } from "@libsql/client";
|
|
1164
|
+
function initShardManager(encryptionKey) {
|
|
1165
|
+
_encryptionKey = encryptionKey;
|
|
1166
|
+
if (!existsSync3(SHARDS_DIR)) {
|
|
1167
|
+
mkdirSync(SHARDS_DIR, { recursive: true });
|
|
1168
|
+
}
|
|
1169
|
+
_shardingEnabled = true;
|
|
1170
|
+
}
|
|
1171
|
+
function isShardingEnabled() {
|
|
1172
|
+
return _shardingEnabled;
|
|
1173
|
+
}
|
|
1174
|
+
function getShardsDir() {
|
|
1175
|
+
return SHARDS_DIR;
|
|
1176
|
+
}
|
|
1177
|
+
function getShardClient(projectName) {
|
|
1178
|
+
if (!_encryptionKey) {
|
|
1179
|
+
throw new Error("Shard manager not initialized. Call initShardManager() first.");
|
|
1180
|
+
}
|
|
1181
|
+
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
1182
|
+
if (!safeName) {
|
|
1183
|
+
throw new Error(`Invalid project name for shard: "${projectName}"`);
|
|
1184
|
+
}
|
|
1185
|
+
const cached = _shards.get(safeName);
|
|
1186
|
+
if (cached) return cached;
|
|
1187
|
+
const dbPath = path3.join(SHARDS_DIR, `${safeName}.db`);
|
|
1188
|
+
const client = createClient2({
|
|
1189
|
+
url: `file:${dbPath}`,
|
|
1190
|
+
encryptionKey: _encryptionKey
|
|
1191
|
+
});
|
|
1192
|
+
_shards.set(safeName, client);
|
|
1193
|
+
return client;
|
|
1194
|
+
}
|
|
1195
|
+
function shardExists(projectName) {
|
|
1196
|
+
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
1197
|
+
return existsSync3(path3.join(SHARDS_DIR, `${safeName}.db`));
|
|
1198
|
+
}
|
|
1199
|
+
function listShards() {
|
|
1200
|
+
if (!existsSync3(SHARDS_DIR)) return [];
|
|
1201
|
+
const { readdirSync: readdirSync2 } = __require("fs");
|
|
1202
|
+
return readdirSync2(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
|
|
1203
|
+
}
|
|
1204
|
+
async function ensureShardSchema(client) {
|
|
1205
|
+
await client.execute("PRAGMA journal_mode = WAL");
|
|
1206
|
+
await client.execute("PRAGMA busy_timeout = 5000");
|
|
1207
|
+
try {
|
|
1208
|
+
await client.execute("PRAGMA libsql_vector_search_ef = 128");
|
|
1209
|
+
} catch {
|
|
1210
|
+
}
|
|
1211
|
+
await client.executeMultiple(`
|
|
1212
|
+
CREATE TABLE IF NOT EXISTS memories (
|
|
1213
|
+
id TEXT PRIMARY KEY,
|
|
1214
|
+
agent_id TEXT NOT NULL,
|
|
1215
|
+
agent_role TEXT NOT NULL,
|
|
1216
|
+
session_id TEXT NOT NULL,
|
|
1217
|
+
timestamp TEXT NOT NULL,
|
|
1218
|
+
tool_name TEXT NOT NULL,
|
|
1219
|
+
project_name TEXT NOT NULL,
|
|
1376
1220
|
has_error INTEGER NOT NULL DEFAULT 0,
|
|
1377
1221
|
raw_text TEXT NOT NULL,
|
|
1378
1222
|
vector F32_BLOB(1024),
|
|
1379
1223
|
version INTEGER NOT NULL DEFAULT 0
|
|
1380
1224
|
);
|
|
1381
1225
|
|
|
1382
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
CREATE INDEX IF NOT EXISTS idx_memories_timestamp
|
|
1386
|
-
ON memories(timestamp);
|
|
1387
|
-
|
|
1388
|
-
CREATE INDEX IF NOT EXISTS idx_memories_session
|
|
1389
|
-
ON memories(session_id);
|
|
1390
|
-
|
|
1391
|
-
CREATE INDEX IF NOT EXISTS idx_memories_project
|
|
1392
|
-
ON memories(project_name);
|
|
1393
|
-
|
|
1394
|
-
CREATE INDEX IF NOT EXISTS idx_memories_tool
|
|
1395
|
-
ON memories(tool_name);
|
|
1396
|
-
|
|
1397
|
-
CREATE INDEX IF NOT EXISTS idx_memories_version
|
|
1398
|
-
ON memories(version);
|
|
1399
|
-
|
|
1400
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent_project
|
|
1401
|
-
ON memories(agent_id, project_name);
|
|
1226
|
+
CREATE INDEX IF NOT EXISTS idx_memories_agent ON memories(agent_id);
|
|
1227
|
+
CREATE INDEX IF NOT EXISTS idx_memories_timestamp ON memories(timestamp);
|
|
1228
|
+
CREATE INDEX IF NOT EXISTS idx_memories_agent_project ON memories(agent_id, project_name);
|
|
1402
1229
|
`);
|
|
1403
1230
|
await client.executeMultiple(`
|
|
1404
1231
|
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
@@ -1420,850 +1247,1462 @@ async function ensureSchema() {
|
|
|
1420
1247
|
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
1421
1248
|
END;
|
|
1422
1249
|
`);
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
agent_id TEXT NOT NULL,
|
|
1450
|
-
project_name TEXT,
|
|
1451
|
-
domain TEXT,
|
|
1452
|
-
content TEXT NOT NULL,
|
|
1453
|
-
active INTEGER NOT NULL DEFAULT 1,
|
|
1454
|
-
created_at TEXT NOT NULL,
|
|
1455
|
-
updated_at TEXT NOT NULL
|
|
1456
|
-
);
|
|
1457
|
-
|
|
1458
|
-
CREATE INDEX IF NOT EXISTS idx_behaviors_agent
|
|
1459
|
-
ON behaviors(agent_id, active);
|
|
1460
|
-
`);
|
|
1461
|
-
try {
|
|
1462
|
-
const existing = await client.execute({
|
|
1463
|
-
sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = 'exe'",
|
|
1464
|
-
args: []
|
|
1465
|
-
});
|
|
1466
|
-
if (Number(existing.rows[0]?.cnt) === 0) {
|
|
1467
|
-
await client.executeMultiple(`
|
|
1468
|
-
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
1469
|
-
VALUES
|
|
1470
|
-
(hex(randomblob(16)), 'exe', NULL, 'workflow', 'Don''t ask "keep going?" \u2014 just keep executing phases/plans autonomously', 1, '2026-03-25T00:00:00Z', '2026-03-25T00:00:00Z');
|
|
1471
|
-
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
1472
|
-
VALUES
|
|
1473
|
-
(hex(randomblob(16)), 'exe', NULL, 'tool-use', 'Always use create_task MCP tool, never write .md files directly for task creation', 1, '2026-03-25T00:00:00Z', '2026-03-25T00:00:00Z');
|
|
1474
|
-
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
1475
|
-
VALUES
|
|
1476
|
-
(hex(randomblob(16)), 'exe', NULL, 'workflow', 'Auto-start reviewing when idle and reviews are pending \u2014 never ask founder for permission', 1, '2026-03-25T00:00:00Z', '2026-03-25T00:00:00Z');
|
|
1477
|
-
`);
|
|
1250
|
+
for (const col of [
|
|
1251
|
+
"ALTER TABLE memories ADD COLUMN task_id TEXT",
|
|
1252
|
+
"ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0",
|
|
1253
|
+
"ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5",
|
|
1254
|
+
"ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'",
|
|
1255
|
+
"ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0",
|
|
1256
|
+
"ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0",
|
|
1257
|
+
"ALTER TABLE memories ADD COLUMN content_hash TEXT",
|
|
1258
|
+
"ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT",
|
|
1259
|
+
"ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7",
|
|
1260
|
+
"ALTER TABLE memories ADD COLUMN last_accessed TEXT",
|
|
1261
|
+
// Wiki linkage columns (must match database.ts)
|
|
1262
|
+
"ALTER TABLE memories ADD COLUMN workspace_id TEXT",
|
|
1263
|
+
"ALTER TABLE memories ADD COLUMN document_id TEXT",
|
|
1264
|
+
"ALTER TABLE memories ADD COLUMN user_id TEXT",
|
|
1265
|
+
"ALTER TABLE memories ADD COLUMN char_offset INTEGER",
|
|
1266
|
+
"ALTER TABLE memories ADD COLUMN page_number INTEGER",
|
|
1267
|
+
// Source provenance columns (must match database.ts)
|
|
1268
|
+
"ALTER TABLE memories ADD COLUMN source_path TEXT",
|
|
1269
|
+
"ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'",
|
|
1270
|
+
"ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3",
|
|
1271
|
+
"ALTER TABLE memories ADD COLUMN supersedes_id TEXT"
|
|
1272
|
+
]) {
|
|
1273
|
+
try {
|
|
1274
|
+
await client.execute(col);
|
|
1275
|
+
} catch {
|
|
1478
1276
|
}
|
|
1479
|
-
} catch {
|
|
1480
1277
|
}
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1278
|
+
for (const idx of [
|
|
1279
|
+
"CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)",
|
|
1280
|
+
"CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL"
|
|
1281
|
+
]) {
|
|
1282
|
+
try {
|
|
1283
|
+
await client.execute(idx);
|
|
1284
|
+
} catch {
|
|
1285
|
+
}
|
|
1487
1286
|
}
|
|
1488
1287
|
try {
|
|
1489
|
-
await client.execute(
|
|
1490
|
-
sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
|
|
1491
|
-
args: []
|
|
1492
|
-
});
|
|
1288
|
+
await client.execute("CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)");
|
|
1493
1289
|
} catch {
|
|
1494
1290
|
}
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1291
|
+
for (const idx of [
|
|
1292
|
+
"CREATE INDEX IF NOT EXISTS idx_memories_workspace ON memories(workspace_id)",
|
|
1293
|
+
"CREATE INDEX IF NOT EXISTS idx_memories_document ON memories(document_id)",
|
|
1294
|
+
"CREATE INDEX IF NOT EXISTS idx_memories_user ON memories(user_id)"
|
|
1295
|
+
]) {
|
|
1296
|
+
try {
|
|
1297
|
+
await client.execute(idx);
|
|
1298
|
+
} catch {
|
|
1299
|
+
}
|
|
1501
1300
|
}
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1301
|
+
await client.executeMultiple(`
|
|
1302
|
+
CREATE TABLE IF NOT EXISTS entities (
|
|
1303
|
+
id TEXT PRIMARY KEY,
|
|
1304
|
+
name TEXT NOT NULL,
|
|
1305
|
+
type TEXT NOT NULL,
|
|
1306
|
+
first_seen TEXT NOT NULL,
|
|
1307
|
+
last_seen TEXT NOT NULL,
|
|
1308
|
+
properties TEXT DEFAULT '{}',
|
|
1309
|
+
UNIQUE(name, type)
|
|
1310
|
+
);
|
|
1311
|
+
|
|
1312
|
+
CREATE TABLE IF NOT EXISTS relationships (
|
|
1313
|
+
id TEXT PRIMARY KEY,
|
|
1314
|
+
source_entity_id TEXT NOT NULL,
|
|
1315
|
+
target_entity_id TEXT NOT NULL,
|
|
1316
|
+
type TEXT NOT NULL,
|
|
1317
|
+
weight REAL DEFAULT 1.0,
|
|
1318
|
+
timestamp TEXT NOT NULL,
|
|
1319
|
+
properties TEXT DEFAULT '{}',
|
|
1320
|
+
UNIQUE(source_entity_id, target_entity_id, type)
|
|
1321
|
+
);
|
|
1322
|
+
|
|
1323
|
+
CREATE TABLE IF NOT EXISTS entity_memories (
|
|
1324
|
+
entity_id TEXT NOT NULL,
|
|
1325
|
+
memory_id TEXT NOT NULL,
|
|
1326
|
+
PRIMARY KEY (entity_id, memory_id)
|
|
1327
|
+
);
|
|
1328
|
+
|
|
1329
|
+
CREATE TABLE IF NOT EXISTS relationship_memories (
|
|
1330
|
+
relationship_id TEXT NOT NULL,
|
|
1331
|
+
memory_id TEXT NOT NULL,
|
|
1332
|
+
PRIMARY KEY (relationship_id, memory_id)
|
|
1333
|
+
);
|
|
1334
|
+
|
|
1335
|
+
CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
|
|
1336
|
+
CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
|
|
1337
|
+
CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
|
|
1338
|
+
CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
|
|
1339
|
+
CREATE INDEX IF NOT EXISTS idx_relationships_type ON relationships(type);
|
|
1340
|
+
|
|
1341
|
+
CREATE TABLE IF NOT EXISTS hyperedges (
|
|
1342
|
+
id TEXT PRIMARY KEY,
|
|
1343
|
+
label TEXT NOT NULL,
|
|
1344
|
+
relation TEXT NOT NULL,
|
|
1345
|
+
confidence REAL DEFAULT 1.0,
|
|
1346
|
+
timestamp TEXT NOT NULL
|
|
1347
|
+
);
|
|
1348
|
+
|
|
1349
|
+
CREATE TABLE IF NOT EXISTS hyperedge_nodes (
|
|
1350
|
+
hyperedge_id TEXT NOT NULL,
|
|
1351
|
+
entity_id TEXT NOT NULL,
|
|
1352
|
+
PRIMARY KEY (hyperedge_id, entity_id)
|
|
1353
|
+
);
|
|
1354
|
+
`);
|
|
1355
|
+
for (const col of [
|
|
1356
|
+
"ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
|
|
1357
|
+
"ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
|
|
1358
|
+
]) {
|
|
1359
|
+
try {
|
|
1360
|
+
await client.execute(col);
|
|
1361
|
+
} catch {
|
|
1362
|
+
}
|
|
1510
1363
|
}
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1364
|
+
}
|
|
1365
|
+
async function getReadyShardClient(projectName) {
|
|
1366
|
+
const client = getShardClient(projectName);
|
|
1367
|
+
await ensureShardSchema(client);
|
|
1368
|
+
return client;
|
|
1369
|
+
}
|
|
1370
|
+
function disposeShards() {
|
|
1371
|
+
for (const [, client] of _shards) {
|
|
1372
|
+
client.close();
|
|
1517
1373
|
}
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1374
|
+
_shards.clear();
|
|
1375
|
+
_shardingEnabled = false;
|
|
1376
|
+
_encryptionKey = null;
|
|
1377
|
+
}
|
|
1378
|
+
var SHARDS_DIR, _shards, _encryptionKey, _shardingEnabled;
|
|
1379
|
+
var init_shard_manager = __esm({
|
|
1380
|
+
"src/lib/shard-manager.ts"() {
|
|
1381
|
+
"use strict";
|
|
1382
|
+
init_config();
|
|
1383
|
+
SHARDS_DIR = path3.join(EXE_AI_DIR, "shards");
|
|
1384
|
+
_shards = /* @__PURE__ */ new Map();
|
|
1385
|
+
_encryptionKey = null;
|
|
1386
|
+
_shardingEnabled = false;
|
|
1524
1387
|
}
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1388
|
+
});
|
|
1389
|
+
|
|
1390
|
+
// src/lib/store.ts
|
|
1391
|
+
var store_exports = {};
|
|
1392
|
+
__export(store_exports, {
|
|
1393
|
+
attachDocumentMetadata: () => attachDocumentMetadata,
|
|
1394
|
+
buildWikiScopeFilter: () => buildWikiScopeFilter,
|
|
1395
|
+
classifyTier: () => classifyTier,
|
|
1396
|
+
disposeStore: () => disposeStore,
|
|
1397
|
+
flushBatch: () => flushBatch,
|
|
1398
|
+
flushTier3: () => flushTier3,
|
|
1399
|
+
getMemoryCardinality: () => getMemoryCardinality,
|
|
1400
|
+
initStore: () => initStore,
|
|
1401
|
+
reserveVersions: () => reserveVersions,
|
|
1402
|
+
searchMemories: () => searchMemories,
|
|
1403
|
+
updateMemoryStatus: () => updateMemoryStatus,
|
|
1404
|
+
vectorToBlob: () => vectorToBlob,
|
|
1405
|
+
writeMemory: () => writeMemory
|
|
1406
|
+
});
|
|
1407
|
+
async function initStore(options) {
|
|
1408
|
+
if (_flushTimer !== null) {
|
|
1409
|
+
clearInterval(_flushTimer);
|
|
1410
|
+
_flushTimer = null;
|
|
1531
1411
|
}
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1412
|
+
_pendingRecords = [];
|
|
1413
|
+
_flushing = false;
|
|
1414
|
+
_batchSize = options?.batchSize ?? 20;
|
|
1415
|
+
_flushIntervalMs = options?.flushIntervalMs ?? 1e4;
|
|
1416
|
+
let dbPath = options?.dbPath;
|
|
1417
|
+
if (!dbPath) {
|
|
1418
|
+
const config = await loadConfig();
|
|
1419
|
+
dbPath = config.dbPath;
|
|
1538
1420
|
}
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1421
|
+
let masterKey = options?.masterKey ?? null;
|
|
1422
|
+
if (!masterKey) {
|
|
1423
|
+
masterKey = await getMasterKey();
|
|
1424
|
+
if (!masterKey) {
|
|
1425
|
+
throw new Error(
|
|
1426
|
+
"No encryption key found. Run /exe-setup to generate one."
|
|
1427
|
+
);
|
|
1428
|
+
}
|
|
1545
1429
|
}
|
|
1430
|
+
const hexKey = masterKey.toString("hex");
|
|
1431
|
+
await initTurso({
|
|
1432
|
+
dbPath,
|
|
1433
|
+
encryptionKey: hexKey
|
|
1434
|
+
});
|
|
1435
|
+
await ensureSchema();
|
|
1546
1436
|
try {
|
|
1547
|
-
await
|
|
1548
|
-
|
|
1549
|
-
args: []
|
|
1550
|
-
});
|
|
1437
|
+
const { initShardManager: initShardManager2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|
|
1438
|
+
initShardManager2(hexKey);
|
|
1551
1439
|
} catch {
|
|
1552
1440
|
}
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1441
|
+
const client = getClient();
|
|
1442
|
+
const vResult = await client.execute("SELECT MAX(version) as max_v FROM memories");
|
|
1443
|
+
_nextVersion = (Number(vResult.rows[0]?.max_v) || 0) + 1;
|
|
1444
|
+
}
|
|
1445
|
+
function classifyTier(record) {
|
|
1446
|
+
if (record.tool_name === "commit_to_long_term_memory" && (record.importance ?? 0) >= 8) return 1;
|
|
1447
|
+
if (["store_memory", "manual"].includes(record.tool_name ?? "") && (record.importance ?? 0) >= 5) return 2;
|
|
1448
|
+
return 3;
|
|
1449
|
+
}
|
|
1450
|
+
async function writeMemory(record) {
|
|
1451
|
+
if (record.vector !== null && record.vector.length !== EMBEDDING_DIM) {
|
|
1452
|
+
throw new Error(
|
|
1453
|
+
`Expected ${EMBEDDING_DIM}-dim vector, got ${record.vector.length}`
|
|
1454
|
+
);
|
|
1559
1455
|
}
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1456
|
+
const dbRow = {
|
|
1457
|
+
id: record.id,
|
|
1458
|
+
agent_id: record.agent_id,
|
|
1459
|
+
agent_role: record.agent_role,
|
|
1460
|
+
session_id: record.session_id,
|
|
1461
|
+
timestamp: record.timestamp,
|
|
1462
|
+
tool_name: record.tool_name,
|
|
1463
|
+
project_name: record.project_name,
|
|
1464
|
+
has_error: record.has_error ? 1 : 0,
|
|
1465
|
+
raw_text: record.raw_text,
|
|
1466
|
+
vector: record.vector,
|
|
1467
|
+
version: _nextVersion++,
|
|
1468
|
+
task_id: record.task_id ?? null,
|
|
1469
|
+
importance: record.importance ?? 5,
|
|
1470
|
+
status: record.status ?? "active",
|
|
1471
|
+
confidence: record.confidence ?? 0.7,
|
|
1472
|
+
last_accessed: record.last_accessed ?? record.timestamp,
|
|
1473
|
+
workspace_id: record.workspace_id ?? null,
|
|
1474
|
+
document_id: record.document_id ?? null,
|
|
1475
|
+
user_id: record.user_id ?? null,
|
|
1476
|
+
char_offset: record.char_offset ?? null,
|
|
1477
|
+
page_number: record.page_number ?? null,
|
|
1478
|
+
source_path: record.source_path ?? null,
|
|
1479
|
+
source_type: record.source_type ?? null,
|
|
1480
|
+
tier: record.tier ?? classifyTier(record),
|
|
1481
|
+
supersedes_id: record.supersedes_id ?? null
|
|
1482
|
+
};
|
|
1483
|
+
_pendingRecords.push(dbRow);
|
|
1484
|
+
if (_flushTimer === null) {
|
|
1485
|
+
_flushTimer = setInterval(() => {
|
|
1486
|
+
void flushBatch();
|
|
1487
|
+
}, _flushIntervalMs);
|
|
1488
|
+
if (_flushTimer && typeof _flushTimer === "object" && "unref" in _flushTimer) {
|
|
1489
|
+
_flushTimer.unref();
|
|
1490
|
+
}
|
|
1566
1491
|
}
|
|
1567
|
-
|
|
1568
|
-
await
|
|
1569
|
-
sql: `ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'`,
|
|
1570
|
-
args: []
|
|
1571
|
-
});
|
|
1572
|
-
} catch {
|
|
1492
|
+
if (_pendingRecords.length >= _batchSize) {
|
|
1493
|
+
await flushBatch();
|
|
1573
1494
|
}
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
source_memory_id TEXT NOT NULL,
|
|
1579
|
-
created_at TEXT NOT NULL
|
|
1580
|
-
);
|
|
1581
|
-
|
|
1582
|
-
CREATE INDEX IF NOT EXISTS idx_consolidations_source
|
|
1583
|
-
ON consolidations(source_memory_id);
|
|
1584
|
-
|
|
1585
|
-
CREATE INDEX IF NOT EXISTS idx_consolidations_consolidated
|
|
1586
|
-
ON consolidations(consolidated_memory_id);
|
|
1587
|
-
`);
|
|
1588
|
-
await client.executeMultiple(`
|
|
1589
|
-
CREATE TABLE IF NOT EXISTS reminders (
|
|
1590
|
-
id TEXT PRIMARY KEY,
|
|
1591
|
-
text TEXT NOT NULL,
|
|
1592
|
-
created_at TEXT NOT NULL,
|
|
1593
|
-
due_date TEXT,
|
|
1594
|
-
completed_at TEXT
|
|
1595
|
-
);
|
|
1596
|
-
`);
|
|
1597
|
-
await client.executeMultiple(`
|
|
1598
|
-
CREATE TABLE IF NOT EXISTS notifications (
|
|
1599
|
-
id TEXT PRIMARY KEY,
|
|
1600
|
-
agent_id TEXT NOT NULL,
|
|
1601
|
-
agent_role TEXT NOT NULL,
|
|
1602
|
-
event TEXT NOT NULL,
|
|
1603
|
-
project TEXT NOT NULL,
|
|
1604
|
-
summary TEXT NOT NULL,
|
|
1605
|
-
task_file TEXT,
|
|
1606
|
-
read INTEGER NOT NULL DEFAULT 0,
|
|
1607
|
-
created_at TEXT NOT NULL
|
|
1608
|
-
);
|
|
1609
|
-
|
|
1610
|
-
CREATE INDEX IF NOT EXISTS idx_notifications_read
|
|
1611
|
-
ON notifications(read);
|
|
1612
|
-
|
|
1613
|
-
CREATE INDEX IF NOT EXISTS idx_notifications_agent
|
|
1614
|
-
ON notifications(agent_id);
|
|
1615
|
-
|
|
1616
|
-
CREATE INDEX IF NOT EXISTS idx_notifications_task_file
|
|
1617
|
-
ON notifications(task_file);
|
|
1618
|
-
`);
|
|
1619
|
-
await client.executeMultiple(`
|
|
1620
|
-
CREATE TABLE IF NOT EXISTS schedules (
|
|
1621
|
-
id TEXT PRIMARY KEY,
|
|
1622
|
-
cron TEXT NOT NULL,
|
|
1623
|
-
description TEXT NOT NULL,
|
|
1624
|
-
job_type TEXT NOT NULL DEFAULT 'report',
|
|
1625
|
-
prompt TEXT,
|
|
1626
|
-
assigned_to TEXT,
|
|
1627
|
-
project_name TEXT,
|
|
1628
|
-
active INTEGER NOT NULL DEFAULT 1,
|
|
1629
|
-
use_crontab INTEGER NOT NULL DEFAULT 0,
|
|
1630
|
-
created_at TEXT NOT NULL
|
|
1631
|
-
);
|
|
1632
|
-
`);
|
|
1633
|
-
await client.executeMultiple(`
|
|
1634
|
-
CREATE TABLE IF NOT EXISTS device_registry (
|
|
1635
|
-
device_id TEXT PRIMARY KEY,
|
|
1636
|
-
friendly_name TEXT NOT NULL,
|
|
1637
|
-
hostname TEXT NOT NULL,
|
|
1638
|
-
projects TEXT NOT NULL DEFAULT '[]',
|
|
1639
|
-
agents TEXT NOT NULL DEFAULT '[]',
|
|
1640
|
-
connected INTEGER DEFAULT 0,
|
|
1641
|
-
last_seen TEXT NOT NULL
|
|
1642
|
-
);
|
|
1643
|
-
`);
|
|
1644
|
-
await client.executeMultiple(`
|
|
1645
|
-
CREATE TABLE IF NOT EXISTS messages (
|
|
1646
|
-
id TEXT PRIMARY KEY,
|
|
1647
|
-
from_agent TEXT NOT NULL,
|
|
1648
|
-
from_device TEXT NOT NULL DEFAULT 'local',
|
|
1649
|
-
target_agent TEXT NOT NULL,
|
|
1650
|
-
target_project TEXT,
|
|
1651
|
-
target_device TEXT NOT NULL DEFAULT 'local',
|
|
1652
|
-
content TEXT NOT NULL,
|
|
1653
|
-
priority TEXT DEFAULT 'normal',
|
|
1654
|
-
status TEXT DEFAULT 'pending',
|
|
1655
|
-
server_seq INTEGER,
|
|
1656
|
-
retry_count INTEGER DEFAULT 0,
|
|
1657
|
-
created_at TEXT NOT NULL,
|
|
1658
|
-
delivered_at TEXT,
|
|
1659
|
-
processed_at TEXT,
|
|
1660
|
-
failed_at TEXT,
|
|
1661
|
-
failure_reason TEXT
|
|
1662
|
-
);
|
|
1663
|
-
|
|
1664
|
-
CREATE INDEX IF NOT EXISTS idx_messages_target
|
|
1665
|
-
ON messages(target_agent, status);
|
|
1666
|
-
|
|
1667
|
-
CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
|
|
1668
|
-
ON messages(target_agent, from_agent, server_seq);
|
|
1669
|
-
`);
|
|
1495
|
+
}
|
|
1496
|
+
async function flushBatch() {
|
|
1497
|
+
if (_flushing || _pendingRecords.length === 0) return 0;
|
|
1498
|
+
_flushing = true;
|
|
1670
1499
|
try {
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1500
|
+
const batch = _pendingRecords.slice(0);
|
|
1501
|
+
const buildStmt = (row) => {
|
|
1502
|
+
const hasVector = row.vector !== null;
|
|
1503
|
+
const taskId = row.task_id ?? null;
|
|
1504
|
+
const importance = row.importance ?? 5;
|
|
1505
|
+
const status = row.status ?? "active";
|
|
1506
|
+
const confidence = row.confidence ?? 0.7;
|
|
1507
|
+
const lastAccessed = row.last_accessed ?? row.timestamp;
|
|
1508
|
+
const workspaceId = row.workspace_id ?? null;
|
|
1509
|
+
const documentId = row.document_id ?? null;
|
|
1510
|
+
const userId = row.user_id ?? null;
|
|
1511
|
+
const charOffset = row.char_offset ?? null;
|
|
1512
|
+
const pageNumber = row.page_number ?? null;
|
|
1513
|
+
const sourcePath = row.source_path ?? null;
|
|
1514
|
+
const sourceType = row.source_type ?? null;
|
|
1515
|
+
const tier = row.tier ?? 3;
|
|
1516
|
+
const supersedesId = row.supersedes_id ?? null;
|
|
1517
|
+
return {
|
|
1518
|
+
sql: hasVector ? `INSERT OR IGNORE INTO memories
|
|
1519
|
+
(id, agent_id, agent_role, session_id, timestamp,
|
|
1520
|
+
tool_name, project_name,
|
|
1521
|
+
has_error, raw_text, vector, version, task_id, importance, status,
|
|
1522
|
+
confidence, last_accessed,
|
|
1523
|
+
workspace_id, document_id, user_id, char_offset, page_number,
|
|
1524
|
+
source_path, source_type, tier, supersedes_id)
|
|
1525
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, vector32(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` : `INSERT OR IGNORE INTO memories
|
|
1526
|
+
(id, agent_id, agent_role, session_id, timestamp,
|
|
1527
|
+
tool_name, project_name,
|
|
1528
|
+
has_error, raw_text, vector, version, task_id, importance, status,
|
|
1529
|
+
confidence, last_accessed,
|
|
1530
|
+
workspace_id, document_id, user_id, char_offset, page_number,
|
|
1531
|
+
source_path, source_type, tier, supersedes_id)
|
|
1532
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
1533
|
+
args: hasVector ? [
|
|
1534
|
+
row.id,
|
|
1535
|
+
row.agent_id,
|
|
1536
|
+
row.agent_role,
|
|
1537
|
+
row.session_id,
|
|
1538
|
+
row.timestamp,
|
|
1539
|
+
row.tool_name,
|
|
1540
|
+
row.project_name,
|
|
1541
|
+
row.has_error,
|
|
1542
|
+
row.raw_text,
|
|
1543
|
+
vectorToBlob(row.vector),
|
|
1544
|
+
row.version,
|
|
1545
|
+
taskId,
|
|
1546
|
+
importance,
|
|
1547
|
+
status,
|
|
1548
|
+
confidence,
|
|
1549
|
+
lastAccessed,
|
|
1550
|
+
workspaceId,
|
|
1551
|
+
documentId,
|
|
1552
|
+
userId,
|
|
1553
|
+
charOffset,
|
|
1554
|
+
pageNumber,
|
|
1555
|
+
sourcePath,
|
|
1556
|
+
sourceType,
|
|
1557
|
+
tier,
|
|
1558
|
+
supersedesId
|
|
1559
|
+
] : [
|
|
1560
|
+
row.id,
|
|
1561
|
+
row.agent_id,
|
|
1562
|
+
row.agent_role,
|
|
1563
|
+
row.session_id,
|
|
1564
|
+
row.timestamp,
|
|
1565
|
+
row.tool_name,
|
|
1566
|
+
row.project_name,
|
|
1567
|
+
row.has_error,
|
|
1568
|
+
row.raw_text,
|
|
1569
|
+
row.version,
|
|
1570
|
+
taskId,
|
|
1571
|
+
importance,
|
|
1572
|
+
status,
|
|
1573
|
+
confidence,
|
|
1574
|
+
lastAccessed,
|
|
1575
|
+
workspaceId,
|
|
1576
|
+
documentId,
|
|
1577
|
+
userId,
|
|
1578
|
+
charOffset,
|
|
1579
|
+
pageNumber,
|
|
1580
|
+
sourcePath,
|
|
1581
|
+
sourceType,
|
|
1582
|
+
tier,
|
|
1583
|
+
supersedesId
|
|
1584
|
+
]
|
|
1585
|
+
};
|
|
1586
|
+
};
|
|
1587
|
+
const globalClient = getClient();
|
|
1588
|
+
const globalStmts = batch.map(buildStmt);
|
|
1589
|
+
await globalClient.batch(globalStmts, "write");
|
|
1590
|
+
_pendingRecords.splice(0, batch.length);
|
|
1591
|
+
try {
|
|
1592
|
+
const { isShardingEnabled: isShardingEnabled2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|
|
1593
|
+
if (isShardingEnabled2()) {
|
|
1594
|
+
const byProject = /* @__PURE__ */ new Map();
|
|
1595
|
+
for (const row of batch) {
|
|
1596
|
+
const proj = row.project_name || "unknown";
|
|
1597
|
+
if (!byProject.has(proj)) byProject.set(proj, []);
|
|
1598
|
+
byProject.get(proj).push(row);
|
|
1599
|
+
}
|
|
1600
|
+
for (const [project, rows] of byProject) {
|
|
1601
|
+
try {
|
|
1602
|
+
const shardClient = await getReadyShardClient2(project);
|
|
1603
|
+
const shardStmts = rows.map(buildStmt);
|
|
1604
|
+
await shardClient.batch(shardStmts, "write");
|
|
1605
|
+
} catch (err) {
|
|
1606
|
+
process.stderr.write(
|
|
1607
|
+
`[store] Shard write failed for ${project}: ${err instanceof Error ? err.message : String(err)}
|
|
1608
|
+
`
|
|
1609
|
+
);
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
} catch {
|
|
1614
|
+
}
|
|
1615
|
+
return batch.length;
|
|
1616
|
+
} finally {
|
|
1617
|
+
_flushing = false;
|
|
1688
1618
|
}
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
signature TEXT NOT NULL,
|
|
1697
|
-
signature_hash TEXT NOT NULL,
|
|
1698
|
-
tool_count INTEGER NOT NULL,
|
|
1699
|
-
skill_id TEXT,
|
|
1700
|
-
created_at TEXT NOT NULL
|
|
1701
|
-
);
|
|
1702
|
-
|
|
1703
|
-
CREATE INDEX IF NOT EXISTS idx_trajectories_hash
|
|
1704
|
-
ON trajectories(signature_hash);
|
|
1705
|
-
|
|
1706
|
-
CREATE INDEX IF NOT EXISTS idx_trajectories_agent
|
|
1707
|
-
ON trajectories(agent_id);
|
|
1708
|
-
`);
|
|
1709
|
-
try {
|
|
1710
|
-
await client.execute("ALTER TABLE trajectories ADD COLUMN skill_id TEXT");
|
|
1711
|
-
} catch {
|
|
1619
|
+
}
|
|
1620
|
+
function buildWikiScopeFilter(options, columnPrefix) {
|
|
1621
|
+
const args = [];
|
|
1622
|
+
let clause = "";
|
|
1623
|
+
if (options?.workspaceId !== void 0) {
|
|
1624
|
+
clause += ` AND ${columnPrefix}workspace_id = ?`;
|
|
1625
|
+
args.push(options.workspaceId);
|
|
1712
1626
|
}
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
);
|
|
1720
|
-
|
|
1721
|
-
CREATE INDEX IF NOT EXISTS idx_consolidations_source
|
|
1722
|
-
ON consolidations(source_memory_id);
|
|
1723
|
-
`);
|
|
1724
|
-
await client.executeMultiple(`
|
|
1725
|
-
CREATE TABLE IF NOT EXISTS audit_trail (
|
|
1726
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
1727
|
-
timestamp TEXT NOT NULL,
|
|
1728
|
-
session_id TEXT NOT NULL,
|
|
1729
|
-
agent_id TEXT NOT NULL,
|
|
1730
|
-
tool TEXT NOT NULL,
|
|
1731
|
-
input TEXT,
|
|
1732
|
-
decision TEXT NOT NULL,
|
|
1733
|
-
reason TEXT,
|
|
1734
|
-
is_customer_facing INTEGER NOT NULL DEFAULT 0
|
|
1735
|
-
);
|
|
1736
|
-
|
|
1737
|
-
CREATE INDEX IF NOT EXISTS idx_audit_trail_agent
|
|
1738
|
-
ON audit_trail(agent_id, timestamp);
|
|
1739
|
-
|
|
1740
|
-
CREATE INDEX IF NOT EXISTS idx_audit_trail_session
|
|
1741
|
-
ON audit_trail(session_id);
|
|
1742
|
-
`);
|
|
1743
|
-
try {
|
|
1744
|
-
await client.execute({
|
|
1745
|
-
sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
|
|
1746
|
-
args: []
|
|
1747
|
-
});
|
|
1748
|
-
} catch {
|
|
1627
|
+
if (options?.userId === void 0) {
|
|
1628
|
+
clause += ` AND ${columnPrefix}user_id IS NULL`;
|
|
1629
|
+
} else if (options.userId === null) {
|
|
1630
|
+
clause += ` AND ${columnPrefix}user_id IS NULL`;
|
|
1631
|
+
} else {
|
|
1632
|
+
clause += ` AND (${columnPrefix}user_id = ? OR ${columnPrefix}user_id IS NULL)`;
|
|
1633
|
+
args.push(options.userId);
|
|
1749
1634
|
}
|
|
1635
|
+
return { clause, args };
|
|
1636
|
+
}
|
|
1637
|
+
async function searchMemories(queryVector, agentId, options) {
|
|
1638
|
+
let client;
|
|
1750
1639
|
try {
|
|
1751
|
-
await
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
}
|
|
1640
|
+
const { isShardingEnabled: isShardingEnabled2, shardExists: shardExists2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|
|
1641
|
+
if (isShardingEnabled2() && options?.projectName && shardExists2(options.projectName)) {
|
|
1642
|
+
client = await getReadyShardClient2(options.projectName);
|
|
1643
|
+
} else {
|
|
1644
|
+
client = getClient();
|
|
1645
|
+
}
|
|
1755
1646
|
} catch {
|
|
1647
|
+
client = getClient();
|
|
1756
1648
|
}
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1649
|
+
const limit = options?.limit ?? 10;
|
|
1650
|
+
const statusFilter = options?.includeArchived ? "" : `
|
|
1651
|
+
AND COALESCE(status, 'active') = 'active'`;
|
|
1652
|
+
let sql = `SELECT id, agent_id, agent_role, session_id, timestamp,
|
|
1653
|
+
tool_name, project_name,
|
|
1654
|
+
has_error, raw_text, vector, importance, status,
|
|
1655
|
+
confidence, last_accessed,
|
|
1656
|
+
workspace_id, document_id, user_id,
|
|
1657
|
+
char_offset, page_number,
|
|
1658
|
+
source_path, source_type
|
|
1659
|
+
FROM memories
|
|
1660
|
+
WHERE agent_id = ?
|
|
1661
|
+
AND vector IS NOT NULL${statusFilter}
|
|
1662
|
+
AND COALESCE(confidence, 0.7) >= 0.3`;
|
|
1663
|
+
const args = [agentId];
|
|
1664
|
+
const scope = buildWikiScopeFilter(options, "");
|
|
1665
|
+
sql += scope.clause;
|
|
1666
|
+
args.push(...scope.args);
|
|
1667
|
+
if (options?.projectName) {
|
|
1668
|
+
sql += ` AND project_name = ?`;
|
|
1669
|
+
args.push(options.projectName);
|
|
1763
1670
|
}
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
args: []
|
|
1768
|
-
});
|
|
1769
|
-
} catch {
|
|
1671
|
+
if (options?.toolName) {
|
|
1672
|
+
sql += ` AND tool_name = ?`;
|
|
1673
|
+
args.push(options.toolName);
|
|
1770
1674
|
}
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
args: []
|
|
1775
|
-
});
|
|
1776
|
-
} catch {
|
|
1675
|
+
if (options?.hasError !== void 0) {
|
|
1676
|
+
sql += ` AND has_error = ?`;
|
|
1677
|
+
args.push(options.hasError ? 1 : 0);
|
|
1777
1678
|
}
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
args: []
|
|
1782
|
-
});
|
|
1783
|
-
} catch {
|
|
1679
|
+
if (options?.since) {
|
|
1680
|
+
sql += ` AND timestamp >= ?`;
|
|
1681
|
+
args.push(options.since);
|
|
1784
1682
|
}
|
|
1683
|
+
sql += ` ORDER BY vector_distance_cos(vector, vector32(?))`;
|
|
1684
|
+
args.push(vectorToBlob(queryVector));
|
|
1685
|
+
sql += ` LIMIT ?`;
|
|
1686
|
+
args.push(limit);
|
|
1687
|
+
const result = await client.execute({ sql, args });
|
|
1688
|
+
return result.rows.map((row) => ({
|
|
1689
|
+
id: row.id,
|
|
1690
|
+
agent_id: row.agent_id,
|
|
1691
|
+
agent_role: row.agent_role,
|
|
1692
|
+
session_id: row.session_id,
|
|
1693
|
+
timestamp: row.timestamp,
|
|
1694
|
+
tool_name: row.tool_name,
|
|
1695
|
+
project_name: row.project_name,
|
|
1696
|
+
has_error: row.has_error === 1,
|
|
1697
|
+
raw_text: row.raw_text,
|
|
1698
|
+
vector: row.vector == null ? [] : Array.isArray(row.vector) ? row.vector : Array.from(row.vector),
|
|
1699
|
+
importance: row.importance ?? 5,
|
|
1700
|
+
status: row.status ?? "active",
|
|
1701
|
+
confidence: row.confidence ?? 0.7,
|
|
1702
|
+
last_accessed: row.last_accessed ?? row.timestamp,
|
|
1703
|
+
workspace_id: row.workspace_id ?? null,
|
|
1704
|
+
document_id: row.document_id ?? null,
|
|
1705
|
+
user_id: row.user_id ?? null,
|
|
1706
|
+
char_offset: row.char_offset ?? null,
|
|
1707
|
+
page_number: row.page_number ?? null,
|
|
1708
|
+
source_path: row.source_path ?? null,
|
|
1709
|
+
source_type: row.source_type ?? null
|
|
1710
|
+
}));
|
|
1711
|
+
}
|
|
1712
|
+
async function attachDocumentMetadata(records) {
|
|
1713
|
+
const docIds = [
|
|
1714
|
+
...new Set(
|
|
1715
|
+
records.map((r) => r.document_id).filter((id) => typeof id === "string" && id.length > 0)
|
|
1716
|
+
)
|
|
1717
|
+
];
|
|
1718
|
+
if (docIds.length === 0) return records;
|
|
1785
1719
|
try {
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1720
|
+
const client = getClient();
|
|
1721
|
+
const placeholders = docIds.map(() => "?").join(",");
|
|
1722
|
+
const result = await client.execute({
|
|
1723
|
+
sql: `SELECT id, filename, mime, source_type, uploaded_at
|
|
1724
|
+
FROM documents
|
|
1725
|
+
WHERE id IN (${placeholders})`,
|
|
1726
|
+
args: docIds
|
|
1789
1727
|
});
|
|
1728
|
+
const byId = /* @__PURE__ */ new Map();
|
|
1729
|
+
for (const row of result.rows) {
|
|
1730
|
+
const id = row.id;
|
|
1731
|
+
byId.set(id, {
|
|
1732
|
+
document_id: id,
|
|
1733
|
+
filename: row.filename,
|
|
1734
|
+
mime: row.mime ?? null,
|
|
1735
|
+
source_type: row.source_type ?? null,
|
|
1736
|
+
uploaded_at: row.uploaded_at
|
|
1737
|
+
});
|
|
1738
|
+
}
|
|
1739
|
+
for (const record of records) {
|
|
1740
|
+
if (!record.document_id) continue;
|
|
1741
|
+
record.document_metadata = byId.get(record.document_id) ?? null;
|
|
1742
|
+
}
|
|
1790
1743
|
} catch {
|
|
1791
1744
|
}
|
|
1745
|
+
return records;
|
|
1746
|
+
}
|
|
1747
|
+
async function flushTier3(agentId, options) {
|
|
1748
|
+
const client = getClient();
|
|
1749
|
+
const maxAge = options?.maxAgeHours ?? 72;
|
|
1750
|
+
const cutoff = new Date(Date.now() - maxAge * 36e5).toISOString();
|
|
1751
|
+
if (options?.dryRun) {
|
|
1752
|
+
const result2 = await client.execute({
|
|
1753
|
+
sql: `SELECT COUNT(*) as cnt FROM memories
|
|
1754
|
+
WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
|
|
1755
|
+
args: [agentId, cutoff]
|
|
1756
|
+
});
|
|
1757
|
+
return { archived: Number(result2.rows[0]?.cnt ?? 0) };
|
|
1758
|
+
}
|
|
1759
|
+
const result = await client.execute({
|
|
1760
|
+
sql: `UPDATE memories SET status = 'archived'
|
|
1761
|
+
WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
|
|
1762
|
+
args: [agentId, cutoff]
|
|
1763
|
+
});
|
|
1764
|
+
return { archived: result.rowsAffected };
|
|
1765
|
+
}
|
|
1766
|
+
async function disposeStore() {
|
|
1767
|
+
if (_flushTimer !== null) {
|
|
1768
|
+
clearInterval(_flushTimer);
|
|
1769
|
+
_flushTimer = null;
|
|
1770
|
+
}
|
|
1771
|
+
if (_pendingRecords.length > 0) {
|
|
1772
|
+
await flushBatch();
|
|
1773
|
+
}
|
|
1774
|
+
await disposeTurso();
|
|
1775
|
+
_pendingRecords = [];
|
|
1776
|
+
_nextVersion = 1;
|
|
1777
|
+
}
|
|
1778
|
+
function vectorToBlob(vector) {
|
|
1779
|
+
const f32 = vector instanceof Float32Array ? vector : new Float32Array(vector);
|
|
1780
|
+
return JSON.stringify(Array.from(f32));
|
|
1781
|
+
}
|
|
1782
|
+
async function updateMemoryStatus(id, status) {
|
|
1783
|
+
const client = getClient();
|
|
1784
|
+
await client.execute({
|
|
1785
|
+
sql: `UPDATE memories SET status = ? WHERE id = ?`,
|
|
1786
|
+
args: [status, id]
|
|
1787
|
+
});
|
|
1788
|
+
}
|
|
1789
|
+
function reserveVersions(count) {
|
|
1790
|
+
const reserved = [];
|
|
1791
|
+
for (let i = 0; i < count; i++) {
|
|
1792
|
+
reserved.push(_nextVersion++);
|
|
1793
|
+
}
|
|
1794
|
+
return reserved;
|
|
1795
|
+
}
|
|
1796
|
+
async function getMemoryCardinality(agentId) {
|
|
1792
1797
|
try {
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1798
|
+
const client = getClient();
|
|
1799
|
+
const result = await client.execute({
|
|
1800
|
+
sql: `SELECT COUNT(*) as cnt FROM memories WHERE agent_id = ? AND COALESCE(status, 'active') = 'active'`,
|
|
1801
|
+
args: [agentId]
|
|
1796
1802
|
});
|
|
1803
|
+
return Number(result.rows[0]?.cnt) || 0;
|
|
1797
1804
|
} catch {
|
|
1805
|
+
return 0;
|
|
1798
1806
|
}
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
+
}
|
|
1808
|
+
var _pendingRecords, _batchSize, _flushIntervalMs, _flushTimer, _flushing, _nextVersion;
|
|
1809
|
+
var init_store = __esm({
|
|
1810
|
+
"src/lib/store.ts"() {
|
|
1811
|
+
"use strict";
|
|
1812
|
+
init_memory();
|
|
1813
|
+
init_database();
|
|
1814
|
+
init_keychain();
|
|
1815
|
+
init_config();
|
|
1816
|
+
_pendingRecords = [];
|
|
1817
|
+
_batchSize = 20;
|
|
1818
|
+
_flushIntervalMs = 1e4;
|
|
1819
|
+
_flushTimer = null;
|
|
1820
|
+
_flushing = false;
|
|
1821
|
+
_nextVersion = 1;
|
|
1807
1822
|
}
|
|
1808
|
-
|
|
1809
|
-
CREATE TABLE IF NOT EXISTS entities (
|
|
1810
|
-
id TEXT PRIMARY KEY,
|
|
1811
|
-
name TEXT NOT NULL,
|
|
1812
|
-
type TEXT NOT NULL,
|
|
1813
|
-
first_seen TEXT NOT NULL,
|
|
1814
|
-
last_seen TEXT NOT NULL,
|
|
1815
|
-
properties TEXT DEFAULT '{}',
|
|
1816
|
-
UNIQUE(name, type)
|
|
1817
|
-
);
|
|
1818
|
-
|
|
1819
|
-
CREATE TABLE IF NOT EXISTS relationships (
|
|
1820
|
-
id TEXT PRIMARY KEY,
|
|
1821
|
-
source_entity_id TEXT NOT NULL,
|
|
1822
|
-
target_entity_id TEXT NOT NULL,
|
|
1823
|
-
type TEXT NOT NULL,
|
|
1824
|
-
weight REAL DEFAULT 1.0,
|
|
1825
|
-
timestamp TEXT NOT NULL,
|
|
1826
|
-
properties TEXT DEFAULT '{}',
|
|
1827
|
-
UNIQUE(source_entity_id, target_entity_id, type)
|
|
1828
|
-
);
|
|
1829
|
-
|
|
1830
|
-
CREATE TABLE IF NOT EXISTS entity_memories (
|
|
1831
|
-
entity_id TEXT NOT NULL,
|
|
1832
|
-
memory_id TEXT NOT NULL,
|
|
1833
|
-
PRIMARY KEY (entity_id, memory_id)
|
|
1834
|
-
);
|
|
1835
|
-
|
|
1836
|
-
CREATE TABLE IF NOT EXISTS relationship_memories (
|
|
1837
|
-
relationship_id TEXT NOT NULL,
|
|
1838
|
-
memory_id TEXT NOT NULL,
|
|
1839
|
-
PRIMARY KEY (relationship_id, memory_id)
|
|
1840
|
-
);
|
|
1841
|
-
|
|
1842
|
-
CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
|
|
1843
|
-
CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
|
|
1844
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
|
|
1845
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
|
|
1846
|
-
|
|
1847
|
-
CREATE TABLE IF NOT EXISTS hyperedges (
|
|
1848
|
-
id TEXT PRIMARY KEY,
|
|
1849
|
-
label TEXT NOT NULL,
|
|
1850
|
-
relation TEXT NOT NULL,
|
|
1851
|
-
confidence REAL DEFAULT 1.0,
|
|
1852
|
-
timestamp TEXT NOT NULL
|
|
1853
|
-
);
|
|
1823
|
+
});
|
|
1854
1824
|
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
"ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
|
|
1870
|
-
"ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
|
|
1871
|
-
]) {
|
|
1872
|
-
try {
|
|
1873
|
-
await client.execute(col);
|
|
1874
|
-
} catch {
|
|
1875
|
-
}
|
|
1825
|
+
// src/lib/self-query-router.ts
|
|
1826
|
+
var self_query_router_exports = {};
|
|
1827
|
+
__export(self_query_router_exports, {
|
|
1828
|
+
routeQuery: () => routeQuery
|
|
1829
|
+
});
|
|
1830
|
+
async function routeQuery(query, model = "claude-haiku-4-5-20251001") {
|
|
1831
|
+
if (query.length < 10) {
|
|
1832
|
+
return {
|
|
1833
|
+
semanticQuery: query,
|
|
1834
|
+
projectFilter: null,
|
|
1835
|
+
roleFilter: null,
|
|
1836
|
+
timeFilter: null,
|
|
1837
|
+
isBroadQuery: false
|
|
1838
|
+
};
|
|
1876
1839
|
}
|
|
1877
1840
|
try {
|
|
1878
|
-
await
|
|
1879
|
-
|
|
1841
|
+
const Anthropic = (await import("@anthropic-ai/sdk")).default;
|
|
1842
|
+
const client = new Anthropic();
|
|
1843
|
+
const now = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
|
|
1844
|
+
const response = await client.messages.create({
|
|
1845
|
+
model,
|
|
1846
|
+
max_tokens: 256,
|
|
1847
|
+
system: `You are a search query router. Extract metadata filters from the user's memory search query. Today is ${now}. Convert relative time references (yesterday, last week) to ISO timestamps.`,
|
|
1848
|
+
messages: [{ role: "user", content: query }],
|
|
1849
|
+
tools: [EXTRACT_TOOL],
|
|
1850
|
+
tool_choice: { type: "tool", name: "extract_search_filters" }
|
|
1851
|
+
});
|
|
1852
|
+
const toolBlock = response.content.find((b) => b.type === "tool_use");
|
|
1853
|
+
if (toolBlock && toolBlock.type === "tool_use") {
|
|
1854
|
+
const input = toolBlock.input;
|
|
1855
|
+
return {
|
|
1856
|
+
semanticQuery: input.semantic_query || query,
|
|
1857
|
+
projectFilter: input.project_filter || null,
|
|
1858
|
+
roleFilter: input.role_filter || null,
|
|
1859
|
+
timeFilter: input.time_filter || null,
|
|
1860
|
+
isBroadQuery: Boolean(input.is_broad_query)
|
|
1861
|
+
};
|
|
1862
|
+
}
|
|
1863
|
+
} catch (err) {
|
|
1864
|
+
process.stderr.write(
|
|
1865
|
+
`[self-query-router] LLM extraction failed, using passthrough: ${err instanceof Error ? err.message : String(err)}
|
|
1866
|
+
`
|
|
1880
1867
|
);
|
|
1881
|
-
} catch {
|
|
1882
1868
|
}
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
mime TEXT,
|
|
1928
|
-
source_type TEXT,
|
|
1929
|
-
user_id TEXT,
|
|
1930
|
-
uploaded_at TEXT NOT NULL,
|
|
1931
|
-
metadata TEXT,
|
|
1932
|
-
FOREIGN KEY (workspace_id) REFERENCES workspaces(id)
|
|
1933
|
-
);
|
|
1934
|
-
|
|
1935
|
-
CREATE INDEX IF NOT EXISTS idx_documents_workspace
|
|
1936
|
-
ON documents(workspace_id);
|
|
1869
|
+
return {
|
|
1870
|
+
semanticQuery: query,
|
|
1871
|
+
projectFilter: null,
|
|
1872
|
+
roleFilter: null,
|
|
1873
|
+
timeFilter: null,
|
|
1874
|
+
isBroadQuery: false
|
|
1875
|
+
};
|
|
1876
|
+
}
|
|
1877
|
+
var EXTRACT_TOOL;
|
|
1878
|
+
var init_self_query_router = __esm({
|
|
1879
|
+
"src/lib/self-query-router.ts"() {
|
|
1880
|
+
"use strict";
|
|
1881
|
+
EXTRACT_TOOL = {
|
|
1882
|
+
name: "extract_search_filters",
|
|
1883
|
+
description: "Extract metadata filters from a memory search query to improve retrieval precision.",
|
|
1884
|
+
input_schema: {
|
|
1885
|
+
type: "object",
|
|
1886
|
+
properties: {
|
|
1887
|
+
semantic_query: {
|
|
1888
|
+
type: "string",
|
|
1889
|
+
description: "The core semantic meaning of the query, stripped of metadata references. This is used for embedding search."
|
|
1890
|
+
},
|
|
1891
|
+
project_filter: {
|
|
1892
|
+
type: ["string", "null"],
|
|
1893
|
+
description: "Project name if the query references a specific project (e.g., 'exe-os', 'exe-create'). Null if no project specified."
|
|
1894
|
+
},
|
|
1895
|
+
role_filter: {
|
|
1896
|
+
type: ["string", "null"],
|
|
1897
|
+
description: "Agent role if the query targets a specific role (e.g., 'CTO', 'CMO'). Null if no role specified."
|
|
1898
|
+
},
|
|
1899
|
+
time_filter: {
|
|
1900
|
+
type: ["string", "null"],
|
|
1901
|
+
description: "ISO 8601 timestamp lower bound if the query references a time period (e.g., 'last week', 'yesterday'). Null if no time reference."
|
|
1902
|
+
},
|
|
1903
|
+
is_broad_query: {
|
|
1904
|
+
type: "boolean",
|
|
1905
|
+
description: "True if the query is exploratory/broad (e.g., 'what has yoshi been working on', 'summarize recent activity'). False if targeted (e.g., 'how did we fix the auth bug')."
|
|
1906
|
+
}
|
|
1907
|
+
},
|
|
1908
|
+
required: ["semantic_query", "project_filter", "role_filter", "time_filter", "is_broad_query"]
|
|
1909
|
+
}
|
|
1910
|
+
};
|
|
1911
|
+
}
|
|
1912
|
+
});
|
|
1937
1913
|
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1914
|
+
// src/lib/exe-daemon-client.ts
|
|
1915
|
+
import net from "net";
|
|
1916
|
+
import { spawn } from "child_process";
|
|
1917
|
+
import { randomUUID } from "crypto";
|
|
1918
|
+
import { existsSync as existsSync4, unlinkSync, readFileSync as readFileSync2, openSync, closeSync, statSync } from "fs";
|
|
1919
|
+
import path4 from "path";
|
|
1920
|
+
import { fileURLToPath } from "url";
|
|
1921
|
+
function handleData(chunk) {
|
|
1922
|
+
_buffer += chunk.toString();
|
|
1923
|
+
let newlineIdx;
|
|
1924
|
+
while ((newlineIdx = _buffer.indexOf("\n")) !== -1) {
|
|
1925
|
+
const line = _buffer.slice(0, newlineIdx).trim();
|
|
1926
|
+
_buffer = _buffer.slice(newlineIdx + 1);
|
|
1927
|
+
if (!line) continue;
|
|
1928
|
+
try {
|
|
1929
|
+
const response = JSON.parse(line);
|
|
1930
|
+
const entry = _pending.get(response.id);
|
|
1931
|
+
if (entry) {
|
|
1932
|
+
clearTimeout(entry.timer);
|
|
1933
|
+
_pending.delete(response.id);
|
|
1934
|
+
entry.resolve(response);
|
|
1935
|
+
}
|
|
1936
|
+
} catch {
|
|
1937
|
+
}
|
|
1938
|
+
}
|
|
1939
|
+
}
|
|
1940
|
+
function cleanupStaleFiles() {
|
|
1941
|
+
if (existsSync4(PID_PATH)) {
|
|
1942
|
+
try {
|
|
1943
|
+
const pid = parseInt(readFileSync2(PID_PATH, "utf8").trim(), 10);
|
|
1944
|
+
if (pid > 0) {
|
|
1945
|
+
try {
|
|
1946
|
+
process.kill(pid, 0);
|
|
1947
|
+
return;
|
|
1948
|
+
} catch {
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
} catch {
|
|
1952
|
+
}
|
|
1953
|
+
try {
|
|
1954
|
+
unlinkSync(PID_PATH);
|
|
1955
|
+
} catch {
|
|
1956
|
+
}
|
|
1957
|
+
try {
|
|
1958
|
+
unlinkSync(SOCKET_PATH);
|
|
1959
|
+
} catch {
|
|
1960
|
+
}
|
|
1961
|
+
}
|
|
1962
|
+
}
|
|
1963
|
+
function findPackageRoot() {
|
|
1964
|
+
let dir = path4.dirname(fileURLToPath(import.meta.url));
|
|
1965
|
+
const { root } = path4.parse(dir);
|
|
1966
|
+
while (dir !== root) {
|
|
1967
|
+
if (existsSync4(path4.join(dir, "package.json"))) return dir;
|
|
1968
|
+
dir = path4.dirname(dir);
|
|
1969
|
+
}
|
|
1970
|
+
return null;
|
|
1971
|
+
}
|
|
1972
|
+
function spawnDaemon() {
|
|
1973
|
+
const pkgRoot = findPackageRoot();
|
|
1974
|
+
if (!pkgRoot) {
|
|
1975
|
+
process.stderr.write("[exed-client] WARN: cannot find package root\n");
|
|
1976
|
+
return;
|
|
1977
|
+
}
|
|
1978
|
+
const daemonPath = path4.join(pkgRoot, "dist", "lib", "exe-daemon.js");
|
|
1979
|
+
if (!existsSync4(daemonPath)) {
|
|
1980
|
+
process.stderr.write(`[exed-client] WARN: daemon script not found at ${daemonPath}
|
|
1981
|
+
`);
|
|
1982
|
+
return;
|
|
1983
|
+
}
|
|
1984
|
+
const resolvedPath = daemonPath;
|
|
1985
|
+
process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
|
|
1986
|
+
`);
|
|
1987
|
+
const logPath = path4.join(path4.dirname(SOCKET_PATH), "exed.log");
|
|
1988
|
+
let stderrFd = "ignore";
|
|
1989
|
+
try {
|
|
1990
|
+
stderrFd = openSync(logPath, "a");
|
|
1991
|
+
} catch {
|
|
1992
|
+
}
|
|
1993
|
+
const child = spawn(process.execPath, [resolvedPath], {
|
|
1994
|
+
detached: true,
|
|
1995
|
+
stdio: ["ignore", "ignore", stderrFd],
|
|
1996
|
+
env: {
|
|
1997
|
+
...process.env,
|
|
1998
|
+
EXE_DAEMON_SOCK: SOCKET_PATH,
|
|
1999
|
+
EXE_DAEMON_PID: PID_PATH
|
|
2000
|
+
}
|
|
2001
|
+
});
|
|
2002
|
+
child.unref();
|
|
2003
|
+
if (typeof stderrFd === "number") {
|
|
2004
|
+
try {
|
|
2005
|
+
closeSync(stderrFd);
|
|
2006
|
+
} catch {
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
function acquireSpawnLock() {
|
|
2011
|
+
try {
|
|
2012
|
+
const fd = openSync(SPAWN_LOCK_PATH, "wx");
|
|
2013
|
+
closeSync(fd);
|
|
2014
|
+
return true;
|
|
2015
|
+
} catch {
|
|
2016
|
+
try {
|
|
2017
|
+
const stat = statSync(SPAWN_LOCK_PATH);
|
|
2018
|
+
if (Date.now() - stat.mtimeMs > SPAWN_LOCK_STALE_MS) {
|
|
2019
|
+
try {
|
|
2020
|
+
unlinkSync(SPAWN_LOCK_PATH);
|
|
2021
|
+
} catch {
|
|
2022
|
+
}
|
|
2023
|
+
try {
|
|
2024
|
+
const fd = openSync(SPAWN_LOCK_PATH, "wx");
|
|
2025
|
+
closeSync(fd);
|
|
2026
|
+
return true;
|
|
2027
|
+
} catch {
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
} catch {
|
|
2031
|
+
}
|
|
2032
|
+
return false;
|
|
2033
|
+
}
|
|
2034
|
+
}
|
|
2035
|
+
function releaseSpawnLock() {
|
|
2036
|
+
try {
|
|
2037
|
+
unlinkSync(SPAWN_LOCK_PATH);
|
|
2038
|
+
} catch {
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
function connectToSocket() {
|
|
2042
|
+
return new Promise((resolve) => {
|
|
2043
|
+
if (_socket && _connected) {
|
|
2044
|
+
resolve(true);
|
|
2045
|
+
return;
|
|
2046
|
+
}
|
|
2047
|
+
const socket = net.createConnection({ path: SOCKET_PATH });
|
|
2048
|
+
const connectTimeout = setTimeout(() => {
|
|
2049
|
+
socket.destroy();
|
|
2050
|
+
resolve(false);
|
|
2051
|
+
}, 2e3);
|
|
2052
|
+
socket.on("connect", () => {
|
|
2053
|
+
clearTimeout(connectTimeout);
|
|
2054
|
+
_socket = socket;
|
|
2055
|
+
_connected = true;
|
|
2056
|
+
_buffer = "";
|
|
2057
|
+
socket.on("data", handleData);
|
|
2058
|
+
socket.on("close", () => {
|
|
2059
|
+
_connected = false;
|
|
2060
|
+
_socket = null;
|
|
2061
|
+
for (const [id, entry] of _pending) {
|
|
2062
|
+
clearTimeout(entry.timer);
|
|
2063
|
+
_pending.delete(id);
|
|
2064
|
+
entry.resolve({ error: "Connection closed" });
|
|
2065
|
+
}
|
|
2066
|
+
});
|
|
2067
|
+
socket.on("error", () => {
|
|
2068
|
+
_connected = false;
|
|
2069
|
+
_socket = null;
|
|
2070
|
+
});
|
|
2071
|
+
resolve(true);
|
|
2072
|
+
});
|
|
2073
|
+
socket.on("error", () => {
|
|
2074
|
+
clearTimeout(connectTimeout);
|
|
2075
|
+
resolve(false);
|
|
2076
|
+
});
|
|
2077
|
+
});
|
|
2078
|
+
}
|
|
2079
|
+
async function connectEmbedDaemon() {
|
|
2080
|
+
if (_socket && _connected) return true;
|
|
2081
|
+
if (await connectToSocket()) return true;
|
|
2082
|
+
if (acquireSpawnLock()) {
|
|
2083
|
+
try {
|
|
2084
|
+
cleanupStaleFiles();
|
|
2085
|
+
spawnDaemon();
|
|
2086
|
+
} finally {
|
|
2087
|
+
releaseSpawnLock();
|
|
2088
|
+
}
|
|
2089
|
+
}
|
|
2090
|
+
const start = Date.now();
|
|
2091
|
+
let delay = 100;
|
|
2092
|
+
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
2093
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
2094
|
+
if (await connectToSocket()) return true;
|
|
2095
|
+
delay = Math.min(delay * 2, 3e3);
|
|
2096
|
+
}
|
|
2097
|
+
return false;
|
|
2098
|
+
}
|
|
2099
|
+
function sendRequest(texts, priority) {
|
|
2100
|
+
return new Promise((resolve) => {
|
|
2101
|
+
if (!_socket || !_connected) {
|
|
2102
|
+
resolve({ error: "Not connected" });
|
|
2103
|
+
return;
|
|
2104
|
+
}
|
|
2105
|
+
const id = randomUUID();
|
|
2106
|
+
const timer = setTimeout(() => {
|
|
2107
|
+
_pending.delete(id);
|
|
2108
|
+
resolve({ error: "Request timeout" });
|
|
2109
|
+
}, REQUEST_TIMEOUT_MS);
|
|
2110
|
+
_pending.set(id, { resolve, timer });
|
|
2111
|
+
try {
|
|
2112
|
+
_socket.write(JSON.stringify({ id, texts, priority }) + "\n");
|
|
2113
|
+
} catch {
|
|
2114
|
+
clearTimeout(timer);
|
|
2115
|
+
_pending.delete(id);
|
|
2116
|
+
resolve({ error: "Write failed" });
|
|
2117
|
+
}
|
|
2118
|
+
});
|
|
2119
|
+
}
|
|
2120
|
+
async function pingDaemon() {
|
|
2121
|
+
if (!_socket || !_connected) return null;
|
|
2122
|
+
return new Promise((resolve) => {
|
|
2123
|
+
const id = randomUUID();
|
|
2124
|
+
const timer = setTimeout(() => {
|
|
2125
|
+
_pending.delete(id);
|
|
2126
|
+
resolve(null);
|
|
2127
|
+
}, 5e3);
|
|
2128
|
+
_pending.set(id, {
|
|
2129
|
+
resolve: (data) => {
|
|
2130
|
+
if (data.health) {
|
|
2131
|
+
resolve(data.health);
|
|
2132
|
+
} else {
|
|
2133
|
+
resolve(null);
|
|
2134
|
+
}
|
|
2135
|
+
},
|
|
2136
|
+
timer
|
|
2137
|
+
});
|
|
1948
2138
|
try {
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
2139
|
+
_socket.write(JSON.stringify({ id, type: "health" }) + "\n");
|
|
2140
|
+
} catch {
|
|
2141
|
+
clearTimeout(timer);
|
|
2142
|
+
_pending.delete(id);
|
|
2143
|
+
resolve(null);
|
|
2144
|
+
}
|
|
2145
|
+
});
|
|
2146
|
+
}
|
|
2147
|
+
function killAndRespawnDaemon() {
|
|
2148
|
+
process.stderr.write("[exed-client] Killing daemon for restart...\n");
|
|
2149
|
+
if (existsSync4(PID_PATH)) {
|
|
2150
|
+
try {
|
|
2151
|
+
const pid = parseInt(readFileSync2(PID_PATH, "utf8").trim(), 10);
|
|
2152
|
+
if (pid > 0) {
|
|
2153
|
+
try {
|
|
2154
|
+
process.kill(pid, "SIGKILL");
|
|
2155
|
+
} catch {
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
1953
2158
|
} catch {
|
|
1954
2159
|
}
|
|
1955
2160
|
}
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
2161
|
+
if (_socket) {
|
|
2162
|
+
_socket.destroy();
|
|
2163
|
+
_socket = null;
|
|
2164
|
+
}
|
|
2165
|
+
_connected = false;
|
|
2166
|
+
_buffer = "";
|
|
2167
|
+
try {
|
|
2168
|
+
unlinkSync(PID_PATH);
|
|
2169
|
+
} catch {
|
|
2170
|
+
}
|
|
2171
|
+
try {
|
|
2172
|
+
unlinkSync(SOCKET_PATH);
|
|
2173
|
+
} catch {
|
|
2174
|
+
}
|
|
2175
|
+
spawnDaemon();
|
|
2176
|
+
}
|
|
2177
|
+
async function embedViaClient(text, priority = "high") {
|
|
2178
|
+
if (!_connected && !await connectEmbedDaemon()) return null;
|
|
2179
|
+
_requestCount++;
|
|
2180
|
+
if (_requestCount % HEALTH_CHECK_INTERVAL === 0) {
|
|
2181
|
+
const health = await pingDaemon();
|
|
2182
|
+
if (!health) {
|
|
2183
|
+
process.stderr.write(`[exed-client] Periodic health check failed at request ${_requestCount} \u2014 restarting daemon
|
|
2184
|
+
`);
|
|
2185
|
+
killAndRespawnDaemon();
|
|
2186
|
+
const start = Date.now();
|
|
2187
|
+
let delay = 200;
|
|
2188
|
+
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
2189
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
2190
|
+
if (await connectToSocket()) break;
|
|
2191
|
+
delay = Math.min(delay * 2, 3e3);
|
|
2192
|
+
}
|
|
2193
|
+
if (!_connected) return null;
|
|
2194
|
+
}
|
|
2195
|
+
}
|
|
2196
|
+
const result = await sendRequest([text], priority);
|
|
2197
|
+
if (!result.error && result.vectors?.[0]) return result.vectors[0];
|
|
2198
|
+
if (result.error) {
|
|
2199
|
+
process.stderr.write(`[exed-client] Embed failed (${result.error}) \u2014 attempting restart
|
|
2200
|
+
`);
|
|
2201
|
+
killAndRespawnDaemon();
|
|
2202
|
+
const start = Date.now();
|
|
2203
|
+
let delay = 200;
|
|
2204
|
+
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
2205
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
2206
|
+
if (await connectToSocket()) break;
|
|
2207
|
+
delay = Math.min(delay * 2, 3e3);
|
|
2208
|
+
}
|
|
2209
|
+
if (!_connected) return null;
|
|
2210
|
+
const retry = await sendRequest([text], priority);
|
|
2211
|
+
if (!retry.error && retry.vectors?.[0]) return retry.vectors[0];
|
|
2212
|
+
process.stderr.write(`[exed-client] Embed retry also failed: ${retry.error ?? "no vector"}
|
|
2213
|
+
`);
|
|
2214
|
+
}
|
|
2215
|
+
return null;
|
|
2216
|
+
}
|
|
2217
|
+
function disconnectClient() {
|
|
2218
|
+
if (_socket) {
|
|
2219
|
+
_socket.destroy();
|
|
2220
|
+
_socket = null;
|
|
2221
|
+
}
|
|
2222
|
+
_connected = false;
|
|
2223
|
+
_buffer = "";
|
|
2224
|
+
for (const [id, entry] of _pending) {
|
|
2225
|
+
clearTimeout(entry.timer);
|
|
2226
|
+
_pending.delete(id);
|
|
2227
|
+
entry.resolve({ error: "Client disconnected" });
|
|
2228
|
+
}
|
|
2229
|
+
}
|
|
2230
|
+
var SOCKET_PATH, PID_PATH, SPAWN_LOCK_PATH, SPAWN_LOCK_STALE_MS, CONNECT_TIMEOUT_MS, REQUEST_TIMEOUT_MS, _socket, _connected, _buffer, _requestCount, HEALTH_CHECK_INTERVAL, _pending;
|
|
2231
|
+
var init_exe_daemon_client = __esm({
|
|
2232
|
+
"src/lib/exe-daemon-client.ts"() {
|
|
2233
|
+
"use strict";
|
|
2234
|
+
init_config();
|
|
2235
|
+
SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ?? path4.join(EXE_AI_DIR, "exed.sock");
|
|
2236
|
+
PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ?? path4.join(EXE_AI_DIR, "exed.pid");
|
|
2237
|
+
SPAWN_LOCK_PATH = path4.join(EXE_AI_DIR, "exed-spawn.lock");
|
|
2238
|
+
SPAWN_LOCK_STALE_MS = 3e4;
|
|
2239
|
+
CONNECT_TIMEOUT_MS = 15e3;
|
|
2240
|
+
REQUEST_TIMEOUT_MS = 3e4;
|
|
2241
|
+
_socket = null;
|
|
2242
|
+
_connected = false;
|
|
2243
|
+
_buffer = "";
|
|
2244
|
+
_requestCount = 0;
|
|
2245
|
+
HEALTH_CHECK_INTERVAL = 100;
|
|
2246
|
+
_pending = /* @__PURE__ */ new Map();
|
|
2247
|
+
}
|
|
2248
|
+
});
|
|
1962
2249
|
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
2250
|
+
// src/lib/embedder.ts
|
|
2251
|
+
var embedder_exports = {};
|
|
2252
|
+
__export(embedder_exports, {
|
|
2253
|
+
disposeEmbedder: () => disposeEmbedder,
|
|
2254
|
+
embed: () => embed,
|
|
2255
|
+
embedDirect: () => embedDirect,
|
|
2256
|
+
getEmbedder: () => getEmbedder
|
|
2257
|
+
});
|
|
2258
|
+
async function getEmbedder() {
|
|
2259
|
+
const ok = await connectEmbedDaemon();
|
|
2260
|
+
if (!ok) {
|
|
2261
|
+
throw new Error(
|
|
2262
|
+
"Could not connect to embedding daemon. Ensure the model is installed (run /exe-setup)."
|
|
1975
2263
|
);
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
CREATE TABLE IF NOT EXISTS conversations (
|
|
1985
|
-
id TEXT PRIMARY KEY,
|
|
1986
|
-
platform TEXT NOT NULL,
|
|
1987
|
-
external_id TEXT,
|
|
1988
|
-
sender_id TEXT NOT NULL,
|
|
1989
|
-
sender_name TEXT,
|
|
1990
|
-
sender_phone TEXT,
|
|
1991
|
-
sender_email TEXT,
|
|
1992
|
-
recipient_id TEXT,
|
|
1993
|
-
channel_id TEXT NOT NULL,
|
|
1994
|
-
thread_id TEXT,
|
|
1995
|
-
reply_to_id TEXT,
|
|
1996
|
-
content_text TEXT,
|
|
1997
|
-
content_media TEXT,
|
|
1998
|
-
content_metadata TEXT,
|
|
1999
|
-
agent_response TEXT,
|
|
2000
|
-
agent_name TEXT,
|
|
2001
|
-
timestamp TEXT NOT NULL,
|
|
2002
|
-
ingested_at TEXT NOT NULL
|
|
2264
|
+
}
|
|
2265
|
+
}
|
|
2266
|
+
async function embed(text) {
|
|
2267
|
+
const priority = process.env.EXE_EMBED_PRIORITY === "low" ? "low" : "high";
|
|
2268
|
+
const vector = await embedViaClient(text, priority);
|
|
2269
|
+
if (!vector) {
|
|
2270
|
+
throw new Error(
|
|
2271
|
+
"Embedding failed: daemon unavailable. Run /exe-setup to verify model installation."
|
|
2003
2272
|
);
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_sender
|
|
2009
|
-
ON conversations(sender_id);
|
|
2010
|
-
|
|
2011
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_timestamp
|
|
2012
|
-
ON conversations(timestamp);
|
|
2013
|
-
|
|
2014
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_thread
|
|
2015
|
-
ON conversations(thread_id);
|
|
2016
|
-
|
|
2017
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_channel
|
|
2018
|
-
ON conversations(channel_id);
|
|
2019
|
-
`);
|
|
2020
|
-
await client.executeMultiple(`
|
|
2021
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS conversations_fts USING fts5(
|
|
2022
|
-
content_text,
|
|
2023
|
-
sender_name,
|
|
2024
|
-
agent_response,
|
|
2025
|
-
content='conversations',
|
|
2026
|
-
content_rowid='rowid'
|
|
2273
|
+
}
|
|
2274
|
+
if (vector.length !== EMBEDDING_DIM) {
|
|
2275
|
+
throw new Error(
|
|
2276
|
+
`Embedding dimension mismatch: expected ${EMBEDDING_DIM}, got ${vector.length}. Ensure the correct Jina v5-small Q4_K_M GGUF model is installed.`
|
|
2027
2277
|
);
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2278
|
+
}
|
|
2279
|
+
return vector;
|
|
2280
|
+
}
|
|
2281
|
+
async function disposeEmbedder() {
|
|
2282
|
+
disconnectClient();
|
|
2283
|
+
}
|
|
2284
|
+
async function embedDirect(text) {
|
|
2285
|
+
const llamaCpp = await import("node-llama-cpp");
|
|
2286
|
+
const { MODELS_DIR: MODELS_DIR2 } = await Promise.resolve().then(() => (init_config(), config_exports));
|
|
2287
|
+
const { existsSync: existsSync7 } = await import("fs");
|
|
2288
|
+
const path8 = await import("path");
|
|
2289
|
+
const modelPath = path8.join(MODELS_DIR2, "jina-embeddings-v5-small-q4_k_m.gguf");
|
|
2290
|
+
if (!existsSync7(modelPath)) {
|
|
2291
|
+
throw new Error(`Embedding model not found at ${modelPath}. Run '/exe-setup' to download it.`);
|
|
2292
|
+
}
|
|
2293
|
+
const llama = await llamaCpp.getLlama();
|
|
2294
|
+
const model = await llama.loadModel({ modelPath });
|
|
2295
|
+
const context = await model.createEmbeddingContext();
|
|
2296
|
+
try {
|
|
2297
|
+
const embedding = await context.getEmbeddingFor(text);
|
|
2298
|
+
const vector = Array.from(embedding.vector);
|
|
2299
|
+
if (vector.length !== EMBEDDING_DIM) {
|
|
2300
|
+
throw new Error(
|
|
2301
|
+
`Embedding dimension mismatch: expected ${EMBEDDING_DIM}, got ${vector.length}.`
|
|
2302
|
+
);
|
|
2303
|
+
}
|
|
2304
|
+
return vector;
|
|
2305
|
+
} finally {
|
|
2306
|
+
await context.dispose();
|
|
2307
|
+
await model.dispose();
|
|
2308
|
+
}
|
|
2046
2309
|
}
|
|
2310
|
+
var init_embedder = __esm({
|
|
2311
|
+
"src/lib/embedder.ts"() {
|
|
2312
|
+
"use strict";
|
|
2313
|
+
init_memory();
|
|
2314
|
+
init_exe_daemon_client();
|
|
2315
|
+
}
|
|
2316
|
+
});
|
|
2047
2317
|
|
|
2048
|
-
// src/lib/
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
return path.join(getKeyDir(), "master.key");
|
|
2060
|
-
}
|
|
2061
|
-
async function tryKeytar() {
|
|
2318
|
+
// src/lib/project-name.ts
|
|
2319
|
+
var project_name_exports = {};
|
|
2320
|
+
__export(project_name_exports, {
|
|
2321
|
+
_resetCache: () => _resetCache,
|
|
2322
|
+
getProjectName: () => getProjectName
|
|
2323
|
+
});
|
|
2324
|
+
import { execSync } from "child_process";
|
|
2325
|
+
import path5 from "path";
|
|
2326
|
+
function getProjectName(cwd) {
|
|
2327
|
+
const dir = cwd ?? process.cwd();
|
|
2328
|
+
if (_cached && _cachedCwd === dir) return _cached;
|
|
2062
2329
|
try {
|
|
2063
|
-
|
|
2330
|
+
let repoRoot;
|
|
2331
|
+
try {
|
|
2332
|
+
const gitCommonDir = execSync("git rev-parse --path-format=absolute --git-common-dir", {
|
|
2333
|
+
cwd: dir,
|
|
2334
|
+
encoding: "utf8",
|
|
2335
|
+
timeout: 2e3,
|
|
2336
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
2337
|
+
}).trim();
|
|
2338
|
+
repoRoot = path5.dirname(gitCommonDir);
|
|
2339
|
+
} catch {
|
|
2340
|
+
repoRoot = execSync("git rev-parse --show-toplevel", {
|
|
2341
|
+
cwd: dir,
|
|
2342
|
+
encoding: "utf8",
|
|
2343
|
+
timeout: 2e3,
|
|
2344
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
2345
|
+
}).trim();
|
|
2346
|
+
}
|
|
2347
|
+
_cached = path5.basename(repoRoot);
|
|
2348
|
+
_cachedCwd = dir;
|
|
2349
|
+
return _cached;
|
|
2064
2350
|
} catch {
|
|
2065
|
-
|
|
2351
|
+
_cached = path5.basename(dir);
|
|
2352
|
+
_cachedCwd = dir;
|
|
2353
|
+
return _cached;
|
|
2066
2354
|
}
|
|
2067
2355
|
}
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2356
|
+
function _resetCache() {
|
|
2357
|
+
_cached = null;
|
|
2358
|
+
_cachedCwd = null;
|
|
2359
|
+
}
|
|
2360
|
+
var _cached, _cachedCwd;
|
|
2361
|
+
var init_project_name = __esm({
|
|
2362
|
+
"src/lib/project-name.ts"() {
|
|
2363
|
+
"use strict";
|
|
2364
|
+
_cached = null;
|
|
2365
|
+
_cachedCwd = null;
|
|
2366
|
+
}
|
|
2367
|
+
});
|
|
2368
|
+
|
|
2369
|
+
// src/lib/file-grep.ts
|
|
2370
|
+
var file_grep_exports = {};
|
|
2371
|
+
__export(file_grep_exports, {
|
|
2372
|
+
grepProjectFiles: () => grepProjectFiles
|
|
2373
|
+
});
|
|
2374
|
+
import { execSync as execSync2 } from "child_process";
|
|
2375
|
+
import { readFileSync as readFileSync3, readdirSync, statSync as statSync2, existsSync as existsSync5 } from "fs";
|
|
2376
|
+
import path6 from "path";
|
|
2377
|
+
import crypto2 from "crypto";
|
|
2378
|
+
function hasRipgrep() {
|
|
2379
|
+
if (_hasRg === null) {
|
|
2071
2380
|
try {
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
return Buffer.from(stored, "base64");
|
|
2075
|
-
}
|
|
2381
|
+
execSync2("rg --version", { stdio: "ignore", timeout: 2e3 });
|
|
2382
|
+
_hasRg = true;
|
|
2076
2383
|
} catch {
|
|
2384
|
+
_hasRg = false;
|
|
2077
2385
|
}
|
|
2078
2386
|
}
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2387
|
+
return _hasRg;
|
|
2388
|
+
}
|
|
2389
|
+
async function grepProjectFiles(query, projectRoot, options) {
|
|
2390
|
+
const maxResults = options?.maxResults ?? 10;
|
|
2391
|
+
const terms = query.toLowerCase().split(/\s+/).filter((t) => t.length >= 3).map((t) => t.replace(/[^a-z0-9_-]/g, "")).filter((t) => t.length >= 3);
|
|
2392
|
+
if (terms.length === 0) return [];
|
|
2393
|
+
const pattern = terms.join("|");
|
|
2394
|
+
let hits;
|
|
2395
|
+
if (hasRipgrep()) {
|
|
2396
|
+
try {
|
|
2397
|
+
hits = grepWithRipgrep(pattern, projectRoot, options?.patterns);
|
|
2398
|
+
} catch {
|
|
2399
|
+
hits = grepWithNodeFs(pattern, projectRoot, options?.patterns);
|
|
2400
|
+
}
|
|
2401
|
+
} else {
|
|
2402
|
+
hits = grepWithNodeFs(pattern, projectRoot, options?.patterns);
|
|
2082
2403
|
}
|
|
2404
|
+
hits.sort((a, b) => b.density - a.density);
|
|
2405
|
+
const topHits = hits.slice(0, maxResults);
|
|
2406
|
+
return topHits.map((hit) => {
|
|
2407
|
+
const chunkCtx = getChunkContext(hit.filePath, hit.lineNumber);
|
|
2408
|
+
const prefix = chunkCtx ? `[file: ${hit.filePath}:${hit.lineNumber} in ${chunkCtx}]` : `[file: ${hit.filePath}:${hit.lineNumber}]`;
|
|
2409
|
+
return {
|
|
2410
|
+
id: crypto2.createHash("sha256").update(`${hit.filePath}:${hit.lineNumber}`).digest("hex").slice(0, 36),
|
|
2411
|
+
agent_id: "project",
|
|
2412
|
+
agent_role: "file",
|
|
2413
|
+
session_id: "file-grep",
|
|
2414
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2415
|
+
tool_name: "file_grep",
|
|
2416
|
+
project_name: path6.basename(projectRoot),
|
|
2417
|
+
has_error: false,
|
|
2418
|
+
raw_text: `${prefix} ${buildSnippet(hit, projectRoot)}`,
|
|
2419
|
+
vector: null,
|
|
2420
|
+
task_id: null
|
|
2421
|
+
};
|
|
2422
|
+
});
|
|
2423
|
+
}
|
|
2424
|
+
function getChunkContext(filePath, lineNumber) {
|
|
2083
2425
|
try {
|
|
2084
|
-
const
|
|
2085
|
-
|
|
2426
|
+
const ext = filePath.split(".").pop()?.toLowerCase();
|
|
2427
|
+
if (ext !== "ts" && ext !== "tsx" && ext !== "js" && ext !== "jsx") return "";
|
|
2428
|
+
const source = readFileSync3(filePath, "utf8");
|
|
2429
|
+
const lines = source.split("\n");
|
|
2430
|
+
for (let i = Math.min(lineNumber - 1, lines.length - 1); i >= 0; i--) {
|
|
2431
|
+
const line = lines[i];
|
|
2432
|
+
const fnMatch = line.match(/(?:export\s+)?(?:async\s+)?function\s+(\w+)/);
|
|
2433
|
+
if (fnMatch) return `function ${fnMatch[1]}`;
|
|
2434
|
+
const classMatch = line.match(/(?:export\s+)?class\s+(\w+)/);
|
|
2435
|
+
if (classMatch) return `class ${classMatch[1]}`;
|
|
2436
|
+
const arrowMatch = line.match(/(?:export\s+)?(?:const|let)\s+(\w+)\s*=\s*(?:async\s+)?\(/);
|
|
2437
|
+
if (arrowMatch) return `function ${arrowMatch[1]}`;
|
|
2438
|
+
}
|
|
2086
2439
|
} catch {
|
|
2087
|
-
return null;
|
|
2088
2440
|
}
|
|
2441
|
+
return "";
|
|
2089
2442
|
}
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
if (
|
|
2101
|
-
|
|
2102
|
-
|
|
2443
|
+
function grepWithRipgrep(pattern, projectRoot, patterns) {
|
|
2444
|
+
const globs = (patterns ?? DEFAULT_PATTERNS).map((p) => `--glob '${p}'`).join(" ");
|
|
2445
|
+
const excludes = EXCLUDE_DIRS.map((d) => `--glob '!${d}'`).join(" ");
|
|
2446
|
+
const cmd = `rg -i -c --hidden --no-config --no-ignore '${pattern.replace(/'/g, "\\'")}' . ${globs} ${excludes} --max-filesize ${MAX_FILE_SIZE} 2>/dev/null || true`;
|
|
2447
|
+
const output = execSync2(cmd, {
|
|
2448
|
+
cwd: projectRoot,
|
|
2449
|
+
encoding: "utf8",
|
|
2450
|
+
timeout: 3e3,
|
|
2451
|
+
maxBuffer: 1024 * 1024
|
|
2452
|
+
});
|
|
2453
|
+
if (!output.trim()) return [];
|
|
2454
|
+
const hits = [];
|
|
2455
|
+
for (const line of output.trim().split("\n")) {
|
|
2456
|
+
const colonIdx = line.lastIndexOf(":");
|
|
2457
|
+
if (colonIdx === -1) continue;
|
|
2458
|
+
const filePath = line.slice(0, colonIdx);
|
|
2459
|
+
const matchCount = parseInt(line.slice(colonIdx + 1));
|
|
2460
|
+
if (isNaN(matchCount) || matchCount === 0) continue;
|
|
2461
|
+
try {
|
|
2462
|
+
const firstMatch = execSync2(
|
|
2463
|
+
`rg -i -n --hidden '${pattern.replace(/'/g, "\\'")}' '${filePath}' --max-count 1 2>/dev/null | head -1`,
|
|
2464
|
+
{ cwd: projectRoot, encoding: "utf8", timeout: 1e3 }
|
|
2465
|
+
).trim();
|
|
2466
|
+
const lineNum = parseInt(firstMatch.split(":")[0] ?? "1");
|
|
2467
|
+
const totalLines = execSync2(`wc -l < '${filePath}'`, {
|
|
2468
|
+
cwd: projectRoot,
|
|
2469
|
+
encoding: "utf8",
|
|
2470
|
+
timeout: 1e3
|
|
2471
|
+
}).trim();
|
|
2472
|
+
hits.push({
|
|
2473
|
+
filePath,
|
|
2474
|
+
lineNumber: isNaN(lineNum) ? 1 : lineNum,
|
|
2475
|
+
matchLine: firstMatch.slice(firstMatch.indexOf(":") + 1).slice(0, 200),
|
|
2476
|
+
matchCount,
|
|
2477
|
+
totalLines: parseInt(totalLines) || 1,
|
|
2478
|
+
density: matchCount / (parseInt(totalLines) || 1)
|
|
2479
|
+
});
|
|
2480
|
+
} catch {
|
|
2481
|
+
}
|
|
2103
2482
|
}
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2483
|
+
return hits;
|
|
2484
|
+
}
|
|
2485
|
+
function grepWithNodeFs(pattern, projectRoot, patterns) {
|
|
2486
|
+
const regex = new RegExp(pattern, "gi");
|
|
2487
|
+
const files = collectFiles(projectRoot, patterns ?? DEFAULT_PATTERNS);
|
|
2488
|
+
const hits = [];
|
|
2489
|
+
for (const filePath of files.slice(0, MAX_FILES)) {
|
|
2490
|
+
const absPath = path6.join(projectRoot, filePath);
|
|
2491
|
+
try {
|
|
2492
|
+
const stat = statSync2(absPath);
|
|
2493
|
+
if (stat.size > MAX_FILE_SIZE) continue;
|
|
2494
|
+
const content = readFileSync3(absPath, "utf8");
|
|
2495
|
+
const lines = content.split("\n");
|
|
2496
|
+
const matches = content.match(regex);
|
|
2497
|
+
if (!matches || matches.length === 0) continue;
|
|
2498
|
+
const firstMatchIdx = lines.findIndex((l) => regex.test(l));
|
|
2499
|
+
regex.lastIndex = 0;
|
|
2500
|
+
hits.push({
|
|
2501
|
+
filePath,
|
|
2502
|
+
lineNumber: firstMatchIdx >= 0 ? firstMatchIdx + 1 : 1,
|
|
2503
|
+
matchLine: firstMatchIdx >= 0 ? lines[firstMatchIdx].slice(0, 200) : "",
|
|
2504
|
+
matchCount: matches.length,
|
|
2505
|
+
totalLines: lines.length,
|
|
2506
|
+
density: matches.length / lines.length
|
|
2507
|
+
});
|
|
2508
|
+
} catch {
|
|
2509
|
+
}
|
|
2112
2510
|
}
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2511
|
+
return hits;
|
|
2512
|
+
}
|
|
2513
|
+
function collectFiles(root, patterns) {
|
|
2514
|
+
const files = [];
|
|
2515
|
+
function walk(dir, relative) {
|
|
2516
|
+
if (files.length >= MAX_FILES) return;
|
|
2517
|
+
const basename = path6.basename(dir);
|
|
2518
|
+
if (EXCLUDE_DIRS.includes(basename)) return;
|
|
2519
|
+
try {
|
|
2520
|
+
const entries = readdirSync(dir, { withFileTypes: true });
|
|
2521
|
+
for (const entry of entries) {
|
|
2522
|
+
if (files.length >= MAX_FILES) return;
|
|
2523
|
+
const rel = path6.join(relative, entry.name);
|
|
2524
|
+
if (entry.isDirectory()) {
|
|
2525
|
+
walk(path6.join(dir, entry.name), rel);
|
|
2526
|
+
} else if (entry.isFile()) {
|
|
2527
|
+
for (const pat of patterns) {
|
|
2528
|
+
if (matchGlob(rel, pat)) {
|
|
2529
|
+
files.push(rel);
|
|
2530
|
+
break;
|
|
2531
|
+
}
|
|
2532
|
+
}
|
|
2533
|
+
}
|
|
2534
|
+
}
|
|
2535
|
+
} catch {
|
|
2120
2536
|
}
|
|
2121
2537
|
}
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2538
|
+
walk(root, "");
|
|
2539
|
+
return files;
|
|
2540
|
+
}
|
|
2541
|
+
function matchGlob(filePath, pattern) {
|
|
2542
|
+
if (!pattern.includes("*")) return filePath === pattern;
|
|
2543
|
+
const doubleStarMatch = pattern.match(/^(.+?)\/\*\*\/\*(\.\w+)$/);
|
|
2544
|
+
if (doubleStarMatch) {
|
|
2545
|
+
const dir = doubleStarMatch[1];
|
|
2546
|
+
const ext2 = doubleStarMatch[2];
|
|
2547
|
+
return filePath.startsWith(dir + "/") && filePath.endsWith(ext2);
|
|
2548
|
+
}
|
|
2549
|
+
if (pattern.startsWith("**/")) {
|
|
2550
|
+
const ext2 = pattern.slice(3).replace("*", "");
|
|
2551
|
+
return filePath.endsWith(ext2);
|
|
2552
|
+
}
|
|
2553
|
+
const slashIdx = pattern.lastIndexOf("/");
|
|
2554
|
+
if (slashIdx !== -1) {
|
|
2555
|
+
const dir = pattern.slice(0, slashIdx);
|
|
2556
|
+
const ext2 = pattern.slice(slashIdx + 1).replace("*", "");
|
|
2557
|
+
const fileDir = path6.dirname(filePath);
|
|
2558
|
+
return fileDir === dir && filePath.endsWith(ext2);
|
|
2559
|
+
}
|
|
2560
|
+
const ext = pattern.replace("*", "");
|
|
2561
|
+
return filePath.endsWith(ext) && !filePath.includes("/");
|
|
2562
|
+
}
|
|
2563
|
+
function buildSnippet(hit, projectRoot) {
|
|
2128
2564
|
try {
|
|
2129
|
-
const
|
|
2130
|
-
|
|
2565
|
+
const absPath = path6.join(projectRoot, hit.filePath);
|
|
2566
|
+
if (!existsSync5(absPath)) return hit.matchLine;
|
|
2567
|
+
const lines = readFileSync3(absPath, "utf8").split("\n");
|
|
2568
|
+
const start = Math.max(0, hit.lineNumber - 3);
|
|
2569
|
+
const end = Math.min(lines.length, hit.lineNumber + 2);
|
|
2570
|
+
return lines.slice(start, end).join("\n").slice(0, 500);
|
|
2131
2571
|
} catch {
|
|
2572
|
+
return hit.matchLine;
|
|
2132
2573
|
}
|
|
2133
|
-
const client = getClient();
|
|
2134
|
-
const vResult = await client.execute("SELECT MAX(version) as max_v FROM memories");
|
|
2135
|
-
_nextVersion = (Number(vResult.rows[0]?.max_v) || 0) + 1;
|
|
2136
2574
|
}
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2575
|
+
var _hasRg, DEFAULT_PATTERNS, EXCLUDE_DIRS, MAX_FILE_SIZE, MAX_FILES;
|
|
2576
|
+
var init_file_grep = __esm({
|
|
2577
|
+
"src/lib/file-grep.ts"() {
|
|
2578
|
+
"use strict";
|
|
2579
|
+
_hasRg = null;
|
|
2580
|
+
DEFAULT_PATTERNS = [
|
|
2581
|
+
".planning/*.md",
|
|
2582
|
+
"exe/output/*.md",
|
|
2583
|
+
"README.md",
|
|
2584
|
+
"src/**/*.ts"
|
|
2585
|
+
];
|
|
2586
|
+
EXCLUDE_DIRS = ["node_modules", "dist", ".git", "coverage", ".worktrees"];
|
|
2587
|
+
MAX_FILE_SIZE = 100 * 1024;
|
|
2588
|
+
MAX_FILES = 500;
|
|
2143
2589
|
}
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2590
|
+
});
|
|
2591
|
+
|
|
2592
|
+
// src/lib/reranker.ts
|
|
2593
|
+
var reranker_exports = {};
|
|
2594
|
+
__export(reranker_exports, {
|
|
2595
|
+
disposeReranker: () => disposeReranker,
|
|
2596
|
+
getRerankerModelPath: () => getRerankerModelPath,
|
|
2597
|
+
isRerankerAvailable: () => isRerankerAvailable,
|
|
2598
|
+
rerank: () => rerank,
|
|
2599
|
+
rerankWithScores: () => rerankWithScores
|
|
2600
|
+
});
|
|
2601
|
+
import path7 from "path";
|
|
2602
|
+
import { existsSync as existsSync6 } from "fs";
|
|
2603
|
+
function resetIdleTimer() {
|
|
2604
|
+
if (_idleTimer) clearTimeout(_idleTimer);
|
|
2605
|
+
_idleTimer = setTimeout(() => {
|
|
2606
|
+
void disposeReranker();
|
|
2607
|
+
}, IDLE_TIMEOUT_MS);
|
|
2608
|
+
if (_idleTimer && typeof _idleTimer === "object" && "unref" in _idleTimer) {
|
|
2609
|
+
_idleTimer.unref();
|
|
2151
2610
|
}
|
|
2152
|
-
return { clause, args };
|
|
2153
2611
|
}
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
client = getClient();
|
|
2612
|
+
function isRerankerAvailable() {
|
|
2613
|
+
return existsSync6(path7.join(MODELS_DIR, RERANKER_MODEL_FILE));
|
|
2614
|
+
}
|
|
2615
|
+
function getRerankerModelPath() {
|
|
2616
|
+
return path7.join(MODELS_DIR, RERANKER_MODEL_FILE);
|
|
2617
|
+
}
|
|
2618
|
+
async function ensureLoaded() {
|
|
2619
|
+
if (_rerankerContext) {
|
|
2620
|
+
resetIdleTimer();
|
|
2621
|
+
return;
|
|
2165
2622
|
}
|
|
2166
|
-
const
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
has_error, raw_text, vector, importance, status,
|
|
2172
|
-
confidence, last_accessed,
|
|
2173
|
-
workspace_id, document_id, user_id,
|
|
2174
|
-
char_offset, page_number
|
|
2175
|
-
FROM memories
|
|
2176
|
-
WHERE agent_id = ?
|
|
2177
|
-
AND vector IS NOT NULL${statusFilter}
|
|
2178
|
-
AND COALESCE(confidence, 0.7) >= 0.3`;
|
|
2179
|
-
const args = [agentId];
|
|
2180
|
-
const scope = buildWikiScopeFilter(options, "");
|
|
2181
|
-
sql += scope.clause;
|
|
2182
|
-
args.push(...scope.args);
|
|
2183
|
-
if (options?.projectName) {
|
|
2184
|
-
sql += ` AND project_name = ?`;
|
|
2185
|
-
args.push(options.projectName);
|
|
2623
|
+
const modelPath = path7.join(MODELS_DIR, RERANKER_MODEL_FILE);
|
|
2624
|
+
if (!existsSync6(modelPath)) {
|
|
2625
|
+
throw new Error(
|
|
2626
|
+
`Reranker model not found at ${modelPath}. Run /exe-setup to download it.`
|
|
2627
|
+
);
|
|
2186
2628
|
}
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2629
|
+
process.stderr.write("[reranker] Loading Jina Reranker v3...\n");
|
|
2630
|
+
const { getLlama } = await import("node-llama-cpp");
|
|
2631
|
+
const llama = await getLlama();
|
|
2632
|
+
_rerankerModel = await llama.loadModel({ modelPath });
|
|
2633
|
+
_rerankerContext = await _rerankerModel.createEmbeddingContext();
|
|
2634
|
+
process.stderr.write("[reranker] Jina Reranker v3 loaded.\n");
|
|
2635
|
+
resetIdleTimer();
|
|
2636
|
+
}
|
|
2637
|
+
async function disposeReranker() {
|
|
2638
|
+
if (_idleTimer) {
|
|
2639
|
+
clearTimeout(_idleTimer);
|
|
2640
|
+
_idleTimer = null;
|
|
2190
2641
|
}
|
|
2191
|
-
if (
|
|
2192
|
-
|
|
2193
|
-
|
|
2642
|
+
if (_rerankerContext) {
|
|
2643
|
+
try {
|
|
2644
|
+
await _rerankerContext.dispose();
|
|
2645
|
+
} catch {
|
|
2646
|
+
}
|
|
2647
|
+
_rerankerContext = null;
|
|
2194
2648
|
}
|
|
2195
|
-
if (
|
|
2196
|
-
|
|
2197
|
-
|
|
2649
|
+
if (_rerankerModel) {
|
|
2650
|
+
try {
|
|
2651
|
+
await _rerankerModel.dispose();
|
|
2652
|
+
} catch {
|
|
2653
|
+
}
|
|
2654
|
+
_rerankerModel = null;
|
|
2198
2655
|
}
|
|
2199
|
-
|
|
2200
|
-
args.push(vectorToBlob(queryVector));
|
|
2201
|
-
sql += ` LIMIT ?`;
|
|
2202
|
-
args.push(limit);
|
|
2203
|
-
const result = await client.execute({ sql, args });
|
|
2204
|
-
return result.rows.map((row) => ({
|
|
2205
|
-
id: row.id,
|
|
2206
|
-
agent_id: row.agent_id,
|
|
2207
|
-
agent_role: row.agent_role,
|
|
2208
|
-
session_id: row.session_id,
|
|
2209
|
-
timestamp: row.timestamp,
|
|
2210
|
-
tool_name: row.tool_name,
|
|
2211
|
-
project_name: row.project_name,
|
|
2212
|
-
has_error: row.has_error === 1,
|
|
2213
|
-
raw_text: row.raw_text,
|
|
2214
|
-
vector: row.vector == null ? [] : Array.isArray(row.vector) ? row.vector : Array.from(row.vector),
|
|
2215
|
-
importance: row.importance ?? 5,
|
|
2216
|
-
status: row.status ?? "active",
|
|
2217
|
-
confidence: row.confidence ?? 0.7,
|
|
2218
|
-
last_accessed: row.last_accessed ?? row.timestamp,
|
|
2219
|
-
workspace_id: row.workspace_id ?? null,
|
|
2220
|
-
document_id: row.document_id ?? null,
|
|
2221
|
-
user_id: row.user_id ?? null,
|
|
2222
|
-
char_offset: row.char_offset ?? null,
|
|
2223
|
-
page_number: row.page_number ?? null
|
|
2224
|
-
}));
|
|
2656
|
+
process.stderr.write("[reranker] Unloaded (idle timeout).\n");
|
|
2225
2657
|
}
|
|
2226
|
-
async function
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
args: docIds
|
|
2241
|
-
});
|
|
2242
|
-
const byId = /* @__PURE__ */ new Map();
|
|
2243
|
-
for (const row of result.rows) {
|
|
2244
|
-
const id = row.id;
|
|
2245
|
-
byId.set(id, {
|
|
2246
|
-
document_id: id,
|
|
2247
|
-
filename: row.filename,
|
|
2248
|
-
mime: row.mime ?? null,
|
|
2249
|
-
source_type: row.source_type ?? null,
|
|
2250
|
-
uploaded_at: row.uploaded_at
|
|
2251
|
-
});
|
|
2252
|
-
}
|
|
2253
|
-
for (const record of records) {
|
|
2254
|
-
if (!record.document_id) continue;
|
|
2255
|
-
record.document_metadata = byId.get(record.document_id) ?? null;
|
|
2658
|
+
async function rerankWithScores(query, texts, topK) {
|
|
2659
|
+
if (texts.length === 0) return [];
|
|
2660
|
+
await ensureLoaded();
|
|
2661
|
+
const ctx = _rerankerContext;
|
|
2662
|
+
const scored = [];
|
|
2663
|
+
for (let i = 0; i < texts.length; i++) {
|
|
2664
|
+
const text = texts[i] ?? "";
|
|
2665
|
+
try {
|
|
2666
|
+
const input = `query: ${query} document: ${text.slice(0, 512)}`;
|
|
2667
|
+
const embedding = await ctx.getEmbeddingFor(input);
|
|
2668
|
+
const score = embedding.vector[0] ?? 0;
|
|
2669
|
+
scored.push({ text, score, index: i });
|
|
2670
|
+
} catch {
|
|
2671
|
+
scored.push({ text, score: -1, index: i });
|
|
2256
2672
|
}
|
|
2257
|
-
} catch {
|
|
2258
2673
|
}
|
|
2259
|
-
|
|
2674
|
+
scored.sort((a, b) => b.score - a.score);
|
|
2675
|
+
return typeof topK === "number" ? scored.slice(0, topK) : scored;
|
|
2260
2676
|
}
|
|
2261
|
-
function
|
|
2262
|
-
|
|
2263
|
-
|
|
2677
|
+
async function rerank(query, candidates, topK = 5) {
|
|
2678
|
+
if (candidates.length === 0) return [];
|
|
2679
|
+
if (candidates.length <= topK) return candidates;
|
|
2680
|
+
const scored = await rerankWithScores(
|
|
2681
|
+
query,
|
|
2682
|
+
candidates.map((c) => c.raw_text),
|
|
2683
|
+
topK
|
|
2684
|
+
);
|
|
2685
|
+
return scored.map((s) => candidates[s.index]);
|
|
2264
2686
|
}
|
|
2687
|
+
var RERANKER_MODEL_FILE, IDLE_TIMEOUT_MS, _rerankerContext, _rerankerModel, _idleTimer;
|
|
2688
|
+
var init_reranker = __esm({
|
|
2689
|
+
"src/lib/reranker.ts"() {
|
|
2690
|
+
"use strict";
|
|
2691
|
+
init_config();
|
|
2692
|
+
RERANKER_MODEL_FILE = "jina-reranker-v3-q4_k_m.gguf";
|
|
2693
|
+
IDLE_TIMEOUT_MS = 6e4;
|
|
2694
|
+
_rerankerContext = null;
|
|
2695
|
+
_rerankerModel = null;
|
|
2696
|
+
_idleTimer = null;
|
|
2697
|
+
}
|
|
2698
|
+
});
|
|
2699
|
+
|
|
2700
|
+
// src/bin/exe-search.ts
|
|
2701
|
+
init_store();
|
|
2265
2702
|
|
|
2266
2703
|
// src/lib/hybrid-search.ts
|
|
2704
|
+
init_store();
|
|
2705
|
+
init_database();
|
|
2267
2706
|
var RRF_K = 60;
|
|
2268
2707
|
async function hybridSearch(queryText, agentId, options) {
|
|
2269
2708
|
const { loadConfig: loadConfig2 } = await Promise.resolve().then(() => (init_config(), config_exports));
|
|
@@ -2290,8 +2729,21 @@ async function hybridSearch(queryText, agentId, options) {
|
|
|
2290
2729
|
} catch {
|
|
2291
2730
|
}
|
|
2292
2731
|
}
|
|
2732
|
+
const { getMemoryCardinality: getMemoryCardinality2 } = await Promise.resolve().then(() => (init_store(), store_exports));
|
|
2733
|
+
const cardinality = await getMemoryCardinality2(agentId);
|
|
2734
|
+
const { rerankerAutoTrigger } = config.scalingRoadmap ?? {};
|
|
2735
|
+
const minCardForBroad = rerankerAutoTrigger?.broadQueryMinCardinality ?? 5e4;
|
|
2736
|
+
const useNarrowPath = cardinality < 1e4;
|
|
2737
|
+
const useBroadPath = cardinality > minCardForBroad || _isBroadQuery && cardinality >= 1e4;
|
|
2738
|
+
const effectiveIsBroad = useBroadPath && !useNarrowPath;
|
|
2739
|
+
if (effectiveIsBroad !== _isBroadQuery) {
|
|
2740
|
+
process.stderr.write(
|
|
2741
|
+
`[hybrid-search] Adaptive routing override: cardinality=${cardinality}, router=${_isBroadQuery ? "broad" : "narrow"} \u2192 ${effectiveIsBroad ? "broad" : "narrow"}
|
|
2742
|
+
`
|
|
2743
|
+
);
|
|
2744
|
+
}
|
|
2293
2745
|
const broadFetchTopK = config.scalingRoadmap?.rerankerAutoTrigger?.fetchTopK ?? 150;
|
|
2294
|
-
const fetchLimit =
|
|
2746
|
+
const fetchLimit = effectiveIsBroad ? Math.max(limit * 5, broadFetchTopK) : Math.max(limit * 3, 30);
|
|
2295
2747
|
const fetchOptions = { ...effectiveOptions, limit: fetchLimit, includeSource: false };
|
|
2296
2748
|
let queryVector = null;
|
|
2297
2749
|
try {
|
|
@@ -2335,8 +2787,8 @@ async function hybridSearch(queryText, agentId, options) {
|
|
|
2335
2787
|
weights.push(0.5);
|
|
2336
2788
|
}
|
|
2337
2789
|
if (lists.length === 0) return [];
|
|
2338
|
-
if (lists.length === 1 && !
|
|
2339
|
-
const rrfLimit =
|
|
2790
|
+
if (lists.length === 1 && !effectiveIsBroad) return lists[0].slice(0, limit);
|
|
2791
|
+
const rrfLimit = effectiveIsBroad ? Math.max(limit * 5, 150) : limit;
|
|
2340
2792
|
const merged = lists.length === 1 ? lists[0].slice(0, rrfLimit) : rrfMergeMulti(lists, rrfLimit, RRF_K, weights);
|
|
2341
2793
|
const auto = config.scalingRoadmap?.rerankerAutoTrigger ?? {
|
|
2342
2794
|
enabled: config.rerankerEnabled ?? true,
|
|
@@ -2345,9 +2797,9 @@ async function hybridSearch(queryText, agentId, options) {
|
|
|
2345
2797
|
returnTopK: 5
|
|
2346
2798
|
};
|
|
2347
2799
|
let rerankedAndBlended = null;
|
|
2348
|
-
if (
|
|
2349
|
-
const
|
|
2350
|
-
if (
|
|
2800
|
+
if (effectiveIsBroad && auto.enabled) {
|
|
2801
|
+
const cardinality2 = await estimateCardinality(agentId, effectiveOptions);
|
|
2802
|
+
if (cardinality2 > auto.broadQueryMinCardinality) {
|
|
2351
2803
|
try {
|
|
2352
2804
|
const { isRerankerAvailable: isRerankerAvailable2, rerank: rerank2 } = await Promise.resolve().then(() => (init_reranker(), reranker_exports));
|
|
2353
2805
|
if (isRerankerAvailable2()) {
|
|
@@ -2424,6 +2876,11 @@ function recencyScore(timestamp) {
|
|
|
2424
2876
|
function normalizedImportance(importance) {
|
|
2425
2877
|
return ((importance ?? 5) - 1) / 9;
|
|
2426
2878
|
}
|
|
2879
|
+
function frecencyBoost(lastAccessed, timestamp) {
|
|
2880
|
+
const accessTime = lastAccessed ? new Date(lastAccessed).getTime() : new Date(timestamp).getTime();
|
|
2881
|
+
const hoursSince = Math.max(0, (Date.now() - accessTime) / (1e3 * 60 * 60));
|
|
2882
|
+
return Math.exp(-0.01 * hoursSince);
|
|
2883
|
+
}
|
|
2427
2884
|
function rrfMergeMulti(lists, limit, k = RRF_K, weights) {
|
|
2428
2885
|
const scores = /* @__PURE__ */ new Map();
|
|
2429
2886
|
for (let listIdx = 0; listIdx < lists.length; listIdx++) {
|
|
@@ -2440,7 +2897,9 @@ function rrfMergeMulti(lists, limit, k = RRF_K, weights) {
|
|
|
2440
2897
|
const recency = recencyScore(e.record.timestamp);
|
|
2441
2898
|
const importance = normalizedImportance(e.record.importance);
|
|
2442
2899
|
const confidence = e.record.confidence ?? 0.7;
|
|
2443
|
-
const
|
|
2900
|
+
const frecency = frecencyBoost(e.record.last_accessed, e.record.timestamp);
|
|
2901
|
+
const baseScore = e.rrfScore * 0.35 + recency * 0.14 + importance * 0.21 + confidence * 0.3;
|
|
2902
|
+
const finalScore = baseScore * (1 + 0.3 * frecency);
|
|
2444
2903
|
return { score: finalScore, record: e.record };
|
|
2445
2904
|
});
|
|
2446
2905
|
return entries.sort((a, b) => b.score - a.score).slice(0, limit).map((e) => e.record);
|
|
@@ -2480,7 +2939,8 @@ async function ftsQuery(client, matchExpr, agentId, options, limit) {
|
|
|
2480
2939
|
m.has_error, m.raw_text, m.vector, m.task_id,
|
|
2481
2940
|
m.importance, m.status, m.confidence, m.last_accessed,
|
|
2482
2941
|
m.workspace_id, m.document_id, m.user_id,
|
|
2483
|
-
m.char_offset, m.page_number
|
|
2942
|
+
m.char_offset, m.page_number,
|
|
2943
|
+
m.source_path, m.source_type
|
|
2484
2944
|
FROM memories m
|
|
2485
2945
|
JOIN memories_fts fts ON m.rowid = fts.rowid
|
|
2486
2946
|
WHERE memories_fts MATCH ?
|
|
@@ -2529,7 +2989,9 @@ async function ftsQuery(client, matchExpr, agentId, options, limit) {
|
|
|
2529
2989
|
document_id: row.document_id ?? null,
|
|
2530
2990
|
user_id: row.user_id ?? null,
|
|
2531
2991
|
char_offset: row.char_offset ?? null,
|
|
2532
|
-
page_number: row.page_number ?? null
|
|
2992
|
+
page_number: row.page_number ?? null,
|
|
2993
|
+
source_path: row.source_path ?? null,
|
|
2994
|
+
source_type: row.source_type ?? null
|
|
2533
2995
|
}));
|
|
2534
2996
|
}
|
|
2535
2997
|
async function recentRecords(agentId, options, limit) {
|
|
@@ -2541,7 +3003,8 @@ async function recentRecords(agentId, options, limit) {
|
|
|
2541
3003
|
has_error, raw_text, vector, task_id,
|
|
2542
3004
|
importance, status, confidence, last_accessed,
|
|
2543
3005
|
workspace_id, document_id, user_id,
|
|
2544
|
-
char_offset, page_number
|
|
3006
|
+
char_offset, page_number,
|
|
3007
|
+
source_path, source_type
|
|
2545
3008
|
FROM memories
|
|
2546
3009
|
WHERE agent_id = ?${statusFilter}
|
|
2547
3010
|
AND COALESCE(confidence, 0.7) >= 0.3`;
|
|
@@ -2588,7 +3051,9 @@ async function recentRecords(agentId, options, limit) {
|
|
|
2588
3051
|
document_id: row.document_id ?? null,
|
|
2589
3052
|
user_id: row.user_id ?? null,
|
|
2590
3053
|
char_offset: row.char_offset ?? null,
|
|
2591
|
-
page_number: row.page_number ?? null
|
|
3054
|
+
page_number: row.page_number ?? null,
|
|
3055
|
+
source_path: row.source_path ?? null,
|
|
3056
|
+
source_type: row.source_type ?? null
|
|
2592
3057
|
}));
|
|
2593
3058
|
}
|
|
2594
3059
|
|