@askexenow/exe-os 0.8.0 → 0.8.2
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/package.json +1 -1
- package/dist/bin/backfill-responses.js +0 -1912
- package/dist/bin/backfill-vectors.js +0 -1642
- package/dist/bin/cleanup-stale-review-tasks.js +0 -1339
- package/dist/bin/cli.js +0 -18800
- package/dist/bin/exe-agent.js +0 -1858
- package/dist/bin/exe-assign.js +0 -1957
- package/dist/bin/exe-boot.js +0 -6460
- package/dist/bin/exe-call.js +0 -197
- package/dist/bin/exe-cloud.js +0 -850
- package/dist/bin/exe-dispatch.js +0 -1146
- package/dist/bin/exe-doctor.js +0 -1657
- package/dist/bin/exe-export-behaviors.js +0 -1494
- package/dist/bin/exe-forget.js +0 -1627
- package/dist/bin/exe-gateway.js +0 -7732
- package/dist/bin/exe-healthcheck.js +0 -207
- package/dist/bin/exe-heartbeat.js +0 -1647
- package/dist/bin/exe-kill.js +0 -1479
- package/dist/bin/exe-launch-agent.js +0 -1704
- package/dist/bin/exe-link.js +0 -192
- package/dist/bin/exe-new-employee.js +0 -852
- package/dist/bin/exe-pending-messages.js +0 -1446
- package/dist/bin/exe-pending-notifications.js +0 -1321
- package/dist/bin/exe-pending-reviews.js +0 -1468
- package/dist/bin/exe-repo-drift.js +0 -95
- package/dist/bin/exe-review.js +0 -1590
- package/dist/bin/exe-search.js +0 -2651
- package/dist/bin/exe-session-cleanup.js +0 -3173
- package/dist/bin/exe-settings.js +0 -354
- package/dist/bin/exe-status.js +0 -1532
- package/dist/bin/exe-team.js +0 -1324
- package/dist/bin/git-sweep.js +0 -2185
- package/dist/bin/graph-backfill.js +0 -1968
- package/dist/bin/graph-export.js +0 -1604
- package/dist/bin/install.js +0 -656
- package/dist/bin/list-providers.js +0 -140
- package/dist/bin/scan-tasks.js +0 -1820
- package/dist/bin/setup.js +0 -951
- package/dist/bin/shard-migrate.js +0 -1494
- package/dist/bin/update.js +0 -95
- package/dist/bin/wiki-sync.js +0 -1514
- package/dist/gateway/index.js +0 -8848
- package/dist/hooks/bug-report-worker.js +0 -2743
- package/dist/hooks/commit-complete.js +0 -2108
- package/dist/hooks/error-recall.js +0 -2861
- package/dist/hooks/exe-heartbeat-hook.js +0 -232
- package/dist/hooks/ingest-worker.js +0 -4793
- package/dist/hooks/ingest.js +0 -684
- package/dist/hooks/instructions-loaded.js +0 -1880
- package/dist/hooks/notification.js +0 -1726
- package/dist/hooks/post-compact.js +0 -1751
- package/dist/hooks/pre-compact.js +0 -1746
- package/dist/hooks/pre-tool-use.js +0 -2191
- package/dist/hooks/prompt-ingest-worker.js +0 -2126
- package/dist/hooks/prompt-submit.js +0 -4693
- package/dist/hooks/response-ingest-worker.js +0 -1936
- package/dist/hooks/session-end.js +0 -1752
- package/dist/hooks/session-start.js +0 -2795
- package/dist/hooks/stop.js +0 -1835
- package/dist/hooks/subagent-stop.js +0 -1726
- package/dist/hooks/summary-worker.js +0 -2661
- package/dist/index.js +0 -11834
- package/dist/lib/cloud-sync.js +0 -495
- package/dist/lib/config.js +0 -222
- package/dist/lib/consolidation.js +0 -476
- package/dist/lib/crypto.js +0 -51
- package/dist/lib/database.js +0 -730
- package/dist/lib/device-registry.js +0 -900
- package/dist/lib/embedder.js +0 -632
- package/dist/lib/employee-templates.js +0 -543
- package/dist/lib/employees.js +0 -177
- package/dist/lib/error-detector.js +0 -156
- package/dist/lib/exe-daemon-client.js +0 -451
- package/dist/lib/exe-daemon.js +0 -8285
- package/dist/lib/file-grep.js +0 -199
- package/dist/lib/hybrid-search.js +0 -1819
- package/dist/lib/identity-templates.js +0 -320
- package/dist/lib/identity.js +0 -223
- package/dist/lib/keychain.js +0 -145
- package/dist/lib/license.js +0 -377
- package/dist/lib/messaging.js +0 -1376
- package/dist/lib/reminders.js +0 -63
- package/dist/lib/schedules.js +0 -1396
- package/dist/lib/session-registry.js +0 -52
- package/dist/lib/skill-learning.js +0 -477
- package/dist/lib/status-brief.js +0 -235
- package/dist/lib/store.js +0 -1551
- package/dist/lib/task-router.js +0 -62
- package/dist/lib/tasks.js +0 -2456
- package/dist/lib/tmux-routing.js +0 -2836
- package/dist/lib/tmux-status.js +0 -261
- package/dist/lib/tmux-transport.js +0 -83
- package/dist/lib/transport.js +0 -128
- package/dist/lib/ws-auth.js +0 -19
- package/dist/lib/ws-client.js +0 -160
- package/dist/mcp/server.js +0 -10538
- package/dist/mcp/tools/complete-reminder.js +0 -67
- package/dist/mcp/tools/create-reminder.js +0 -52
- package/dist/mcp/tools/create-task.js +0 -1853
- package/dist/mcp/tools/deactivate-behavior.js +0 -263
- package/dist/mcp/tools/list-reminders.js +0 -62
- package/dist/mcp/tools/list-tasks.js +0 -463
- package/dist/mcp/tools/send-message.js +0 -1382
- package/dist/mcp/tools/update-task.js +0 -1692
- package/dist/runtime/index.js +0 -6809
- package/dist/tui/App.js +0 -17479
|
@@ -1,1968 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
5
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
6
|
-
}) : x)(function(x) {
|
|
7
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
8
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
9
|
-
});
|
|
10
|
-
var __esm = (fn, res) => function __init() {
|
|
11
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
12
|
-
};
|
|
13
|
-
var __export = (target, all) => {
|
|
14
|
-
for (var name in all)
|
|
15
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
// src/lib/config.ts
|
|
19
|
-
import { readFile as readFile2, writeFile as writeFile2, mkdir as mkdir2 } from "fs/promises";
|
|
20
|
-
import { readFileSync, existsSync as existsSync2, renameSync } from "fs";
|
|
21
|
-
import path2 from "path";
|
|
22
|
-
import os from "os";
|
|
23
|
-
function resolveDataDir() {
|
|
24
|
-
if (process.env.EXE_OS_DIR) return process.env.EXE_OS_DIR;
|
|
25
|
-
if (process.env.EXE_MEM_DIR) return process.env.EXE_MEM_DIR;
|
|
26
|
-
const newDir = path2.join(os.homedir(), ".exe-os");
|
|
27
|
-
const legacyDir = path2.join(os.homedir(), ".exe-mem");
|
|
28
|
-
if (!existsSync2(newDir) && existsSync2(legacyDir)) {
|
|
29
|
-
try {
|
|
30
|
-
renameSync(legacyDir, newDir);
|
|
31
|
-
process.stderr.write(`[exe-os] Migrated data directory: ~/.exe-mem \u2192 ~/.exe-os
|
|
32
|
-
`);
|
|
33
|
-
} catch {
|
|
34
|
-
return legacyDir;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return newDir;
|
|
38
|
-
}
|
|
39
|
-
function migrateLegacyConfig(raw) {
|
|
40
|
-
if ("r2" in raw) {
|
|
41
|
-
process.stderr.write(
|
|
42
|
-
"[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"
|
|
43
|
-
);
|
|
44
|
-
delete raw.r2;
|
|
45
|
-
}
|
|
46
|
-
if ("syncIntervalMs" in raw) {
|
|
47
|
-
delete raw.syncIntervalMs;
|
|
48
|
-
}
|
|
49
|
-
return raw;
|
|
50
|
-
}
|
|
51
|
-
function migrateConfig(raw) {
|
|
52
|
-
const fromVersion = typeof raw.config_version === "number" ? raw.config_version : 0;
|
|
53
|
-
let currentVersion = fromVersion;
|
|
54
|
-
let migrated = false;
|
|
55
|
-
if (currentVersion > CURRENT_CONFIG_VERSION) {
|
|
56
|
-
return { config: raw, migrated: false, fromVersion };
|
|
57
|
-
}
|
|
58
|
-
for (const migration of CONFIG_MIGRATIONS) {
|
|
59
|
-
if (currentVersion === migration.from && migration.to <= CURRENT_CONFIG_VERSION) {
|
|
60
|
-
raw = migration.migrate(raw);
|
|
61
|
-
currentVersion = migration.to;
|
|
62
|
-
migrated = true;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return { config: raw, migrated, fromVersion };
|
|
66
|
-
}
|
|
67
|
-
function normalizeScalingRoadmap(raw) {
|
|
68
|
-
const defaultAuto = DEFAULT_CONFIG.scalingRoadmap.rerankerAutoTrigger;
|
|
69
|
-
const userRoadmap = raw.scalingRoadmap ?? {};
|
|
70
|
-
const userAuto = userRoadmap.rerankerAutoTrigger ?? {};
|
|
71
|
-
if (userAuto.enabled === void 0 && raw.rerankerEnabled !== void 0) {
|
|
72
|
-
userAuto.enabled = raw.rerankerEnabled;
|
|
73
|
-
}
|
|
74
|
-
raw.scalingRoadmap = {
|
|
75
|
-
...userRoadmap,
|
|
76
|
-
rerankerAutoTrigger: { ...defaultAuto, ...userAuto }
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
function normalizeSessionLifecycle(raw) {
|
|
80
|
-
const defaultSL = DEFAULT_CONFIG.sessionLifecycle;
|
|
81
|
-
const userSL = raw.sessionLifecycle ?? {};
|
|
82
|
-
raw.sessionLifecycle = { ...defaultSL, ...userSL };
|
|
83
|
-
}
|
|
84
|
-
async function loadConfig() {
|
|
85
|
-
const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
|
|
86
|
-
await mkdir2(dir, { recursive: true });
|
|
87
|
-
const configPath = path2.join(dir, "config.json");
|
|
88
|
-
if (!existsSync2(configPath)) {
|
|
89
|
-
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
|
|
90
|
-
}
|
|
91
|
-
const raw = await readFile2(configPath, "utf-8");
|
|
92
|
-
try {
|
|
93
|
-
let parsed = JSON.parse(raw);
|
|
94
|
-
parsed = migrateLegacyConfig(parsed);
|
|
95
|
-
const { config: migratedCfg, migrated, fromVersion } = migrateConfig(parsed);
|
|
96
|
-
if (migrated) {
|
|
97
|
-
process.stderr.write(`[exe-os] Config migrated from v${fromVersion} to v${migratedCfg.config_version}
|
|
98
|
-
`);
|
|
99
|
-
try {
|
|
100
|
-
await writeFile2(configPath, JSON.stringify(migratedCfg, null, 2) + "\n");
|
|
101
|
-
} catch {
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
normalizeScalingRoadmap(migratedCfg);
|
|
105
|
-
normalizeSessionLifecycle(migratedCfg);
|
|
106
|
-
const config = { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db"), ...migratedCfg };
|
|
107
|
-
if (config.dbPath.startsWith("~")) {
|
|
108
|
-
config.dbPath = config.dbPath.replace(/^~/, os.homedir());
|
|
109
|
-
}
|
|
110
|
-
return config;
|
|
111
|
-
} catch {
|
|
112
|
-
return { ...DEFAULT_CONFIG, dbPath: path2.join(dir, "memories.db") };
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
var EXE_AI_DIR, DB_PATH, MODELS_DIR, CONFIG_PATH, LEGACY_LANCE_PATH, CURRENT_CONFIG_VERSION, DEFAULT_CONFIG, CONFIG_MIGRATIONS;
|
|
116
|
-
var init_config = __esm({
|
|
117
|
-
"src/lib/config.ts"() {
|
|
118
|
-
"use strict";
|
|
119
|
-
EXE_AI_DIR = resolveDataDir();
|
|
120
|
-
DB_PATH = path2.join(EXE_AI_DIR, "memories.db");
|
|
121
|
-
MODELS_DIR = path2.join(EXE_AI_DIR, "models");
|
|
122
|
-
CONFIG_PATH = path2.join(EXE_AI_DIR, "config.json");
|
|
123
|
-
LEGACY_LANCE_PATH = path2.join(EXE_AI_DIR, "local.lance");
|
|
124
|
-
CURRENT_CONFIG_VERSION = 1;
|
|
125
|
-
DEFAULT_CONFIG = {
|
|
126
|
-
config_version: CURRENT_CONFIG_VERSION,
|
|
127
|
-
dbPath: DB_PATH,
|
|
128
|
-
modelFile: "jina-embeddings-v5-small-q4_k_m.gguf",
|
|
129
|
-
embeddingDim: 1024,
|
|
130
|
-
batchSize: 20,
|
|
131
|
-
flushIntervalMs: 1e4,
|
|
132
|
-
autoIngestion: true,
|
|
133
|
-
autoRetrieval: true,
|
|
134
|
-
searchMode: "hybrid",
|
|
135
|
-
hookSearchMode: "hybrid",
|
|
136
|
-
fileGrepEnabled: true,
|
|
137
|
-
splashEffect: true,
|
|
138
|
-
consolidationEnabled: true,
|
|
139
|
-
consolidationIntervalMs: 6 * 60 * 60 * 1e3,
|
|
140
|
-
consolidationModel: "claude-haiku-4-5-20251001",
|
|
141
|
-
consolidationMaxCallsPerRun: 20,
|
|
142
|
-
selfQueryRouter: true,
|
|
143
|
-
selfQueryModel: "claude-haiku-4-5-20251001",
|
|
144
|
-
rerankerEnabled: true,
|
|
145
|
-
scalingRoadmap: {
|
|
146
|
-
rerankerAutoTrigger: {
|
|
147
|
-
enabled: true,
|
|
148
|
-
broadQueryMinCardinality: 5e4,
|
|
149
|
-
fetchTopK: 150,
|
|
150
|
-
returnTopK: 5
|
|
151
|
-
}
|
|
152
|
-
},
|
|
153
|
-
graphRagEnabled: true,
|
|
154
|
-
wikiEnabled: false,
|
|
155
|
-
wikiUrl: "",
|
|
156
|
-
wikiApiKey: "",
|
|
157
|
-
wikiSyncIntervalMs: 30 * 60 * 1e3,
|
|
158
|
-
wikiWorkspaceMapping: {
|
|
159
|
-
exe: "Executive",
|
|
160
|
-
yoshi: "Engineering",
|
|
161
|
-
mari: "Marketing",
|
|
162
|
-
tom: "Engineering",
|
|
163
|
-
sasha: "Production"
|
|
164
|
-
},
|
|
165
|
-
wikiAutoUpdate: true,
|
|
166
|
-
wikiAutoUpdateThreshold: 0.5,
|
|
167
|
-
wikiAutoUpdateCreateNew: true,
|
|
168
|
-
skillLearning: true,
|
|
169
|
-
skillThreshold: 3,
|
|
170
|
-
skillModel: "claude-haiku-4-5-20251001",
|
|
171
|
-
exeHeartbeat: {
|
|
172
|
-
enabled: true,
|
|
173
|
-
intervalSeconds: 60,
|
|
174
|
-
staleInProgressThresholdHours: 2
|
|
175
|
-
},
|
|
176
|
-
sessionLifecycle: {
|
|
177
|
-
idleKillEnabled: true,
|
|
178
|
-
idleKillTicksRequired: 3,
|
|
179
|
-
idleKillIntercomAckWindowMs: 1e4,
|
|
180
|
-
maxAutoInstances: 10
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
CONFIG_MIGRATIONS = [
|
|
184
|
-
{
|
|
185
|
-
from: 0,
|
|
186
|
-
to: 1,
|
|
187
|
-
migrate: (cfg) => {
|
|
188
|
-
cfg.config_version = 1;
|
|
189
|
-
return cfg;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
];
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
// src/lib/shard-manager.ts
|
|
197
|
-
var shard_manager_exports = {};
|
|
198
|
-
__export(shard_manager_exports, {
|
|
199
|
-
disposeShards: () => disposeShards,
|
|
200
|
-
ensureShardSchema: () => ensureShardSchema,
|
|
201
|
-
getReadyShardClient: () => getReadyShardClient,
|
|
202
|
-
getShardClient: () => getShardClient,
|
|
203
|
-
getShardsDir: () => getShardsDir,
|
|
204
|
-
initShardManager: () => initShardManager,
|
|
205
|
-
isShardingEnabled: () => isShardingEnabled,
|
|
206
|
-
listShards: () => listShards,
|
|
207
|
-
shardExists: () => shardExists
|
|
208
|
-
});
|
|
209
|
-
import path3 from "path";
|
|
210
|
-
import { existsSync as existsSync3, mkdirSync } from "fs";
|
|
211
|
-
import { createClient as createClient2 } from "@libsql/client";
|
|
212
|
-
function initShardManager(encryptionKey) {
|
|
213
|
-
_encryptionKey = encryptionKey;
|
|
214
|
-
if (!existsSync3(SHARDS_DIR)) {
|
|
215
|
-
mkdirSync(SHARDS_DIR, { recursive: true });
|
|
216
|
-
}
|
|
217
|
-
_shardingEnabled = true;
|
|
218
|
-
}
|
|
219
|
-
function isShardingEnabled() {
|
|
220
|
-
return _shardingEnabled;
|
|
221
|
-
}
|
|
222
|
-
function getShardsDir() {
|
|
223
|
-
return SHARDS_DIR;
|
|
224
|
-
}
|
|
225
|
-
function getShardClient(projectName) {
|
|
226
|
-
if (!_encryptionKey) {
|
|
227
|
-
throw new Error("Shard manager not initialized. Call initShardManager() first.");
|
|
228
|
-
}
|
|
229
|
-
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
230
|
-
if (!safeName) {
|
|
231
|
-
throw new Error(`Invalid project name for shard: "${projectName}"`);
|
|
232
|
-
}
|
|
233
|
-
const cached = _shards.get(safeName);
|
|
234
|
-
if (cached) return cached;
|
|
235
|
-
const dbPath = path3.join(SHARDS_DIR, `${safeName}.db`);
|
|
236
|
-
const client = createClient2({
|
|
237
|
-
url: `file:${dbPath}`,
|
|
238
|
-
encryptionKey: _encryptionKey
|
|
239
|
-
});
|
|
240
|
-
_shards.set(safeName, client);
|
|
241
|
-
return client;
|
|
242
|
-
}
|
|
243
|
-
function shardExists(projectName) {
|
|
244
|
-
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
245
|
-
return existsSync3(path3.join(SHARDS_DIR, `${safeName}.db`));
|
|
246
|
-
}
|
|
247
|
-
function listShards() {
|
|
248
|
-
if (!existsSync3(SHARDS_DIR)) return [];
|
|
249
|
-
const { readdirSync } = __require("fs");
|
|
250
|
-
return readdirSync(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
|
|
251
|
-
}
|
|
252
|
-
async function ensureShardSchema(client) {
|
|
253
|
-
await client.execute("PRAGMA journal_mode = WAL");
|
|
254
|
-
await client.execute("PRAGMA busy_timeout = 5000");
|
|
255
|
-
try {
|
|
256
|
-
await client.execute("PRAGMA libsql_vector_search_ef = 128");
|
|
257
|
-
} catch {
|
|
258
|
-
}
|
|
259
|
-
await client.executeMultiple(`
|
|
260
|
-
CREATE TABLE IF NOT EXISTS memories (
|
|
261
|
-
id TEXT PRIMARY KEY,
|
|
262
|
-
agent_id TEXT NOT NULL,
|
|
263
|
-
agent_role TEXT NOT NULL,
|
|
264
|
-
session_id TEXT NOT NULL,
|
|
265
|
-
timestamp TEXT NOT NULL,
|
|
266
|
-
tool_name TEXT NOT NULL,
|
|
267
|
-
project_name TEXT NOT NULL,
|
|
268
|
-
has_error INTEGER NOT NULL DEFAULT 0,
|
|
269
|
-
raw_text TEXT NOT NULL,
|
|
270
|
-
vector F32_BLOB(1024),
|
|
271
|
-
version INTEGER NOT NULL DEFAULT 0
|
|
272
|
-
);
|
|
273
|
-
|
|
274
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent ON memories(agent_id);
|
|
275
|
-
CREATE INDEX IF NOT EXISTS idx_memories_timestamp ON memories(timestamp);
|
|
276
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent_project ON memories(agent_id, project_name);
|
|
277
|
-
`);
|
|
278
|
-
await client.executeMultiple(`
|
|
279
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
280
|
-
raw_text,
|
|
281
|
-
content='memories',
|
|
282
|
-
content_rowid='rowid'
|
|
283
|
-
);
|
|
284
|
-
|
|
285
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
|
|
286
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
287
|
-
END;
|
|
288
|
-
|
|
289
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
|
|
290
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
291
|
-
END;
|
|
292
|
-
|
|
293
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
|
|
294
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
295
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
296
|
-
END;
|
|
297
|
-
`);
|
|
298
|
-
for (const col of [
|
|
299
|
-
"ALTER TABLE memories ADD COLUMN task_id TEXT",
|
|
300
|
-
"ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0",
|
|
301
|
-
"ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5",
|
|
302
|
-
"ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'",
|
|
303
|
-
"ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0",
|
|
304
|
-
"ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0",
|
|
305
|
-
"ALTER TABLE memories ADD COLUMN content_hash TEXT",
|
|
306
|
-
"ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT",
|
|
307
|
-
"ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7",
|
|
308
|
-
"ALTER TABLE memories ADD COLUMN last_accessed TEXT",
|
|
309
|
-
// Wiki linkage columns (must match database.ts)
|
|
310
|
-
"ALTER TABLE memories ADD COLUMN workspace_id TEXT",
|
|
311
|
-
"ALTER TABLE memories ADD COLUMN document_id TEXT",
|
|
312
|
-
"ALTER TABLE memories ADD COLUMN user_id TEXT",
|
|
313
|
-
"ALTER TABLE memories ADD COLUMN char_offset INTEGER",
|
|
314
|
-
"ALTER TABLE memories ADD COLUMN page_number INTEGER"
|
|
315
|
-
]) {
|
|
316
|
-
try {
|
|
317
|
-
await client.execute(col);
|
|
318
|
-
} catch {
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
try {
|
|
322
|
-
await client.execute("CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)");
|
|
323
|
-
} catch {
|
|
324
|
-
}
|
|
325
|
-
for (const idx of [
|
|
326
|
-
"CREATE INDEX IF NOT EXISTS idx_memories_workspace ON memories(workspace_id)",
|
|
327
|
-
"CREATE INDEX IF NOT EXISTS idx_memories_document ON memories(document_id)",
|
|
328
|
-
"CREATE INDEX IF NOT EXISTS idx_memories_user ON memories(user_id)"
|
|
329
|
-
]) {
|
|
330
|
-
try {
|
|
331
|
-
await client.execute(idx);
|
|
332
|
-
} catch {
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
await client.executeMultiple(`
|
|
336
|
-
CREATE TABLE IF NOT EXISTS entities (
|
|
337
|
-
id TEXT PRIMARY KEY,
|
|
338
|
-
name TEXT NOT NULL,
|
|
339
|
-
type TEXT NOT NULL,
|
|
340
|
-
first_seen TEXT NOT NULL,
|
|
341
|
-
last_seen TEXT NOT NULL,
|
|
342
|
-
properties TEXT DEFAULT '{}',
|
|
343
|
-
UNIQUE(name, type)
|
|
344
|
-
);
|
|
345
|
-
|
|
346
|
-
CREATE TABLE IF NOT EXISTS relationships (
|
|
347
|
-
id TEXT PRIMARY KEY,
|
|
348
|
-
source_entity_id TEXT NOT NULL,
|
|
349
|
-
target_entity_id TEXT NOT NULL,
|
|
350
|
-
type TEXT NOT NULL,
|
|
351
|
-
weight REAL DEFAULT 1.0,
|
|
352
|
-
timestamp TEXT NOT NULL,
|
|
353
|
-
properties TEXT DEFAULT '{}',
|
|
354
|
-
UNIQUE(source_entity_id, target_entity_id, type)
|
|
355
|
-
);
|
|
356
|
-
|
|
357
|
-
CREATE TABLE IF NOT EXISTS entity_memories (
|
|
358
|
-
entity_id TEXT NOT NULL,
|
|
359
|
-
memory_id TEXT NOT NULL,
|
|
360
|
-
PRIMARY KEY (entity_id, memory_id)
|
|
361
|
-
);
|
|
362
|
-
|
|
363
|
-
CREATE TABLE IF NOT EXISTS relationship_memories (
|
|
364
|
-
relationship_id TEXT NOT NULL,
|
|
365
|
-
memory_id TEXT NOT NULL,
|
|
366
|
-
PRIMARY KEY (relationship_id, memory_id)
|
|
367
|
-
);
|
|
368
|
-
|
|
369
|
-
CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
|
|
370
|
-
CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
|
|
371
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
|
|
372
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
|
|
373
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_type ON relationships(type);
|
|
374
|
-
|
|
375
|
-
CREATE TABLE IF NOT EXISTS hyperedges (
|
|
376
|
-
id TEXT PRIMARY KEY,
|
|
377
|
-
label TEXT NOT NULL,
|
|
378
|
-
relation TEXT NOT NULL,
|
|
379
|
-
confidence REAL DEFAULT 1.0,
|
|
380
|
-
timestamp TEXT NOT NULL
|
|
381
|
-
);
|
|
382
|
-
|
|
383
|
-
CREATE TABLE IF NOT EXISTS hyperedge_nodes (
|
|
384
|
-
hyperedge_id TEXT NOT NULL,
|
|
385
|
-
entity_id TEXT NOT NULL,
|
|
386
|
-
PRIMARY KEY (hyperedge_id, entity_id)
|
|
387
|
-
);
|
|
388
|
-
`);
|
|
389
|
-
for (const col of [
|
|
390
|
-
"ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
|
|
391
|
-
"ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
|
|
392
|
-
]) {
|
|
393
|
-
try {
|
|
394
|
-
await client.execute(col);
|
|
395
|
-
} catch {
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
async function getReadyShardClient(projectName) {
|
|
400
|
-
const client = getShardClient(projectName);
|
|
401
|
-
await ensureShardSchema(client);
|
|
402
|
-
return client;
|
|
403
|
-
}
|
|
404
|
-
function disposeShards() {
|
|
405
|
-
for (const [, client] of _shards) {
|
|
406
|
-
client.close();
|
|
407
|
-
}
|
|
408
|
-
_shards.clear();
|
|
409
|
-
_shardingEnabled = false;
|
|
410
|
-
_encryptionKey = null;
|
|
411
|
-
}
|
|
412
|
-
var SHARDS_DIR, _shards, _encryptionKey, _shardingEnabled;
|
|
413
|
-
var init_shard_manager = __esm({
|
|
414
|
-
"src/lib/shard-manager.ts"() {
|
|
415
|
-
"use strict";
|
|
416
|
-
init_config();
|
|
417
|
-
SHARDS_DIR = path3.join(EXE_AI_DIR, "shards");
|
|
418
|
-
_shards = /* @__PURE__ */ new Map();
|
|
419
|
-
_encryptionKey = null;
|
|
420
|
-
_shardingEnabled = false;
|
|
421
|
-
}
|
|
422
|
-
});
|
|
423
|
-
|
|
424
|
-
// src/lib/database.ts
|
|
425
|
-
import { createClient } from "@libsql/client";
|
|
426
|
-
var _client = null;
|
|
427
|
-
var initTurso = initDatabase;
|
|
428
|
-
async function initDatabase(config) {
|
|
429
|
-
if (_client) {
|
|
430
|
-
_client.close();
|
|
431
|
-
_client = null;
|
|
432
|
-
}
|
|
433
|
-
const opts = {
|
|
434
|
-
url: `file:${config.dbPath}`
|
|
435
|
-
};
|
|
436
|
-
if (config.encryptionKey) {
|
|
437
|
-
opts.encryptionKey = config.encryptionKey;
|
|
438
|
-
}
|
|
439
|
-
_client = createClient(opts);
|
|
440
|
-
}
|
|
441
|
-
function getClient() {
|
|
442
|
-
if (!_client) {
|
|
443
|
-
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
444
|
-
}
|
|
445
|
-
return _client;
|
|
446
|
-
}
|
|
447
|
-
async function ensureSchema() {
|
|
448
|
-
const client = getClient();
|
|
449
|
-
await client.execute("PRAGMA journal_mode = WAL");
|
|
450
|
-
await client.execute("PRAGMA busy_timeout = 5000");
|
|
451
|
-
try {
|
|
452
|
-
await client.execute("PRAGMA libsql_vector_search_ef = 128");
|
|
453
|
-
} catch {
|
|
454
|
-
}
|
|
455
|
-
await client.executeMultiple(`
|
|
456
|
-
CREATE TABLE IF NOT EXISTS memories (
|
|
457
|
-
id TEXT PRIMARY KEY,
|
|
458
|
-
agent_id TEXT NOT NULL,
|
|
459
|
-
agent_role TEXT NOT NULL,
|
|
460
|
-
session_id TEXT NOT NULL,
|
|
461
|
-
timestamp TEXT NOT NULL,
|
|
462
|
-
tool_name TEXT NOT NULL,
|
|
463
|
-
project_name TEXT NOT NULL,
|
|
464
|
-
has_error INTEGER NOT NULL DEFAULT 0,
|
|
465
|
-
raw_text TEXT NOT NULL,
|
|
466
|
-
vector F32_BLOB(1024),
|
|
467
|
-
version INTEGER NOT NULL DEFAULT 0
|
|
468
|
-
);
|
|
469
|
-
|
|
470
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent
|
|
471
|
-
ON memories(agent_id);
|
|
472
|
-
|
|
473
|
-
CREATE INDEX IF NOT EXISTS idx_memories_timestamp
|
|
474
|
-
ON memories(timestamp);
|
|
475
|
-
|
|
476
|
-
CREATE INDEX IF NOT EXISTS idx_memories_session
|
|
477
|
-
ON memories(session_id);
|
|
478
|
-
|
|
479
|
-
CREATE INDEX IF NOT EXISTS idx_memories_project
|
|
480
|
-
ON memories(project_name);
|
|
481
|
-
|
|
482
|
-
CREATE INDEX IF NOT EXISTS idx_memories_tool
|
|
483
|
-
ON memories(tool_name);
|
|
484
|
-
|
|
485
|
-
CREATE INDEX IF NOT EXISTS idx_memories_version
|
|
486
|
-
ON memories(version);
|
|
487
|
-
|
|
488
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent_project
|
|
489
|
-
ON memories(agent_id, project_name);
|
|
490
|
-
`);
|
|
491
|
-
await client.executeMultiple(`
|
|
492
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
493
|
-
raw_text,
|
|
494
|
-
content='memories',
|
|
495
|
-
content_rowid='rowid'
|
|
496
|
-
);
|
|
497
|
-
|
|
498
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
|
|
499
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
500
|
-
END;
|
|
501
|
-
|
|
502
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
|
|
503
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
504
|
-
END;
|
|
505
|
-
|
|
506
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
|
|
507
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
508
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
509
|
-
END;
|
|
510
|
-
`);
|
|
511
|
-
await client.executeMultiple(`
|
|
512
|
-
CREATE TABLE IF NOT EXISTS sync_meta (
|
|
513
|
-
key TEXT PRIMARY KEY,
|
|
514
|
-
value TEXT NOT NULL
|
|
515
|
-
);
|
|
516
|
-
`);
|
|
517
|
-
await client.executeMultiple(`
|
|
518
|
-
CREATE TABLE IF NOT EXISTS tasks (
|
|
519
|
-
id TEXT PRIMARY KEY,
|
|
520
|
-
title TEXT NOT NULL,
|
|
521
|
-
assigned_to TEXT NOT NULL,
|
|
522
|
-
assigned_by TEXT NOT NULL,
|
|
523
|
-
project_name TEXT NOT NULL,
|
|
524
|
-
priority TEXT NOT NULL DEFAULT 'p1',
|
|
525
|
-
status TEXT NOT NULL DEFAULT 'open',
|
|
526
|
-
task_file TEXT,
|
|
527
|
-
created_at TEXT NOT NULL,
|
|
528
|
-
updated_at TEXT NOT NULL
|
|
529
|
-
);
|
|
530
|
-
|
|
531
|
-
CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status
|
|
532
|
-
ON tasks(assigned_to, status);
|
|
533
|
-
`);
|
|
534
|
-
await client.executeMultiple(`
|
|
535
|
-
CREATE TABLE IF NOT EXISTS behaviors (
|
|
536
|
-
id TEXT PRIMARY KEY,
|
|
537
|
-
agent_id TEXT NOT NULL,
|
|
538
|
-
project_name TEXT,
|
|
539
|
-
domain TEXT,
|
|
540
|
-
content TEXT NOT NULL,
|
|
541
|
-
active INTEGER NOT NULL DEFAULT 1,
|
|
542
|
-
created_at TEXT NOT NULL,
|
|
543
|
-
updated_at TEXT NOT NULL
|
|
544
|
-
);
|
|
545
|
-
|
|
546
|
-
CREATE INDEX IF NOT EXISTS idx_behaviors_agent
|
|
547
|
-
ON behaviors(agent_id, active);
|
|
548
|
-
`);
|
|
549
|
-
try {
|
|
550
|
-
const existing = await client.execute({
|
|
551
|
-
sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = 'exe'",
|
|
552
|
-
args: []
|
|
553
|
-
});
|
|
554
|
-
if (Number(existing.rows[0]?.cnt) === 0) {
|
|
555
|
-
await client.executeMultiple(`
|
|
556
|
-
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
557
|
-
VALUES
|
|
558
|
-
(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');
|
|
559
|
-
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
560
|
-
VALUES
|
|
561
|
-
(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');
|
|
562
|
-
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
563
|
-
VALUES
|
|
564
|
-
(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');
|
|
565
|
-
`);
|
|
566
|
-
}
|
|
567
|
-
} catch {
|
|
568
|
-
}
|
|
569
|
-
try {
|
|
570
|
-
await client.execute({
|
|
571
|
-
sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
|
|
572
|
-
args: []
|
|
573
|
-
});
|
|
574
|
-
} catch {
|
|
575
|
-
}
|
|
576
|
-
try {
|
|
577
|
-
await client.execute({
|
|
578
|
-
sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
|
|
579
|
-
args: []
|
|
580
|
-
});
|
|
581
|
-
} catch {
|
|
582
|
-
}
|
|
583
|
-
try {
|
|
584
|
-
await client.execute({
|
|
585
|
-
sql: `ALTER TABLE tasks ADD COLUMN parent_task_id TEXT`,
|
|
586
|
-
args: []
|
|
587
|
-
});
|
|
588
|
-
} catch {
|
|
589
|
-
}
|
|
590
|
-
try {
|
|
591
|
-
await client.execute({
|
|
592
|
-
sql: `CREATE INDEX IF NOT EXISTS idx_tasks_parent_task_id
|
|
593
|
-
ON tasks(parent_task_id)
|
|
594
|
-
WHERE parent_task_id IS NOT NULL`,
|
|
595
|
-
args: []
|
|
596
|
-
});
|
|
597
|
-
} catch {
|
|
598
|
-
}
|
|
599
|
-
try {
|
|
600
|
-
await client.execute({
|
|
601
|
-
sql: `UPDATE tasks SET status = 'done' WHERE status = 'completed'`,
|
|
602
|
-
args: []
|
|
603
|
-
});
|
|
604
|
-
} catch {
|
|
605
|
-
}
|
|
606
|
-
try {
|
|
607
|
-
await client.execute({
|
|
608
|
-
sql: `ALTER TABLE tasks ADD COLUMN reviewer TEXT`,
|
|
609
|
-
args: []
|
|
610
|
-
});
|
|
611
|
-
} catch {
|
|
612
|
-
}
|
|
613
|
-
try {
|
|
614
|
-
await client.execute({
|
|
615
|
-
sql: `ALTER TABLE tasks ADD COLUMN context TEXT`,
|
|
616
|
-
args: []
|
|
617
|
-
});
|
|
618
|
-
} catch {
|
|
619
|
-
}
|
|
620
|
-
try {
|
|
621
|
-
await client.execute({
|
|
622
|
-
sql: `ALTER TABLE tasks ADD COLUMN result TEXT`,
|
|
623
|
-
args: []
|
|
624
|
-
});
|
|
625
|
-
} catch {
|
|
626
|
-
}
|
|
627
|
-
try {
|
|
628
|
-
await client.execute({
|
|
629
|
-
sql: `ALTER TABLE tasks ADD COLUMN assigned_tmux TEXT`,
|
|
630
|
-
args: []
|
|
631
|
-
});
|
|
632
|
-
} catch {
|
|
633
|
-
}
|
|
634
|
-
try {
|
|
635
|
-
await client.execute({
|
|
636
|
-
sql: `ALTER TABLE memories ADD COLUMN task_id TEXT`,
|
|
637
|
-
args: []
|
|
638
|
-
});
|
|
639
|
-
} catch {
|
|
640
|
-
}
|
|
641
|
-
try {
|
|
642
|
-
await client.execute({
|
|
643
|
-
sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
|
|
644
|
-
args: []
|
|
645
|
-
});
|
|
646
|
-
} catch {
|
|
647
|
-
}
|
|
648
|
-
try {
|
|
649
|
-
await client.execute({
|
|
650
|
-
sql: `ALTER TABLE memories ADD COLUMN author_device_id TEXT`,
|
|
651
|
-
args: []
|
|
652
|
-
});
|
|
653
|
-
} catch {
|
|
654
|
-
}
|
|
655
|
-
try {
|
|
656
|
-
await client.execute({
|
|
657
|
-
sql: `ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'`,
|
|
658
|
-
args: []
|
|
659
|
-
});
|
|
660
|
-
} catch {
|
|
661
|
-
}
|
|
662
|
-
await client.executeMultiple(`
|
|
663
|
-
CREATE TABLE IF NOT EXISTS consolidations (
|
|
664
|
-
id TEXT PRIMARY KEY,
|
|
665
|
-
consolidated_memory_id TEXT NOT NULL,
|
|
666
|
-
source_memory_id TEXT NOT NULL,
|
|
667
|
-
created_at TEXT NOT NULL
|
|
668
|
-
);
|
|
669
|
-
|
|
670
|
-
CREATE INDEX IF NOT EXISTS idx_consolidations_source
|
|
671
|
-
ON consolidations(source_memory_id);
|
|
672
|
-
|
|
673
|
-
CREATE INDEX IF NOT EXISTS idx_consolidations_consolidated
|
|
674
|
-
ON consolidations(consolidated_memory_id);
|
|
675
|
-
`);
|
|
676
|
-
await client.executeMultiple(`
|
|
677
|
-
CREATE TABLE IF NOT EXISTS reminders (
|
|
678
|
-
id TEXT PRIMARY KEY,
|
|
679
|
-
text TEXT NOT NULL,
|
|
680
|
-
created_at TEXT NOT NULL,
|
|
681
|
-
due_date TEXT,
|
|
682
|
-
completed_at TEXT
|
|
683
|
-
);
|
|
684
|
-
`);
|
|
685
|
-
await client.executeMultiple(`
|
|
686
|
-
CREATE TABLE IF NOT EXISTS notifications (
|
|
687
|
-
id TEXT PRIMARY KEY,
|
|
688
|
-
agent_id TEXT NOT NULL,
|
|
689
|
-
agent_role TEXT NOT NULL,
|
|
690
|
-
event TEXT NOT NULL,
|
|
691
|
-
project TEXT NOT NULL,
|
|
692
|
-
summary TEXT NOT NULL,
|
|
693
|
-
task_file TEXT,
|
|
694
|
-
read INTEGER NOT NULL DEFAULT 0,
|
|
695
|
-
created_at TEXT NOT NULL
|
|
696
|
-
);
|
|
697
|
-
|
|
698
|
-
CREATE INDEX IF NOT EXISTS idx_notifications_read
|
|
699
|
-
ON notifications(read);
|
|
700
|
-
|
|
701
|
-
CREATE INDEX IF NOT EXISTS idx_notifications_agent
|
|
702
|
-
ON notifications(agent_id);
|
|
703
|
-
|
|
704
|
-
CREATE INDEX IF NOT EXISTS idx_notifications_task_file
|
|
705
|
-
ON notifications(task_file);
|
|
706
|
-
`);
|
|
707
|
-
await client.executeMultiple(`
|
|
708
|
-
CREATE TABLE IF NOT EXISTS schedules (
|
|
709
|
-
id TEXT PRIMARY KEY,
|
|
710
|
-
cron TEXT NOT NULL,
|
|
711
|
-
description TEXT NOT NULL,
|
|
712
|
-
job_type TEXT NOT NULL DEFAULT 'report',
|
|
713
|
-
prompt TEXT,
|
|
714
|
-
assigned_to TEXT,
|
|
715
|
-
project_name TEXT,
|
|
716
|
-
active INTEGER NOT NULL DEFAULT 1,
|
|
717
|
-
use_crontab INTEGER NOT NULL DEFAULT 0,
|
|
718
|
-
created_at TEXT NOT NULL
|
|
719
|
-
);
|
|
720
|
-
`);
|
|
721
|
-
await client.executeMultiple(`
|
|
722
|
-
CREATE TABLE IF NOT EXISTS device_registry (
|
|
723
|
-
device_id TEXT PRIMARY KEY,
|
|
724
|
-
friendly_name TEXT NOT NULL,
|
|
725
|
-
hostname TEXT NOT NULL,
|
|
726
|
-
projects TEXT NOT NULL DEFAULT '[]',
|
|
727
|
-
agents TEXT NOT NULL DEFAULT '[]',
|
|
728
|
-
connected INTEGER DEFAULT 0,
|
|
729
|
-
last_seen TEXT NOT NULL
|
|
730
|
-
);
|
|
731
|
-
`);
|
|
732
|
-
await client.executeMultiple(`
|
|
733
|
-
CREATE TABLE IF NOT EXISTS messages (
|
|
734
|
-
id TEXT PRIMARY KEY,
|
|
735
|
-
from_agent TEXT NOT NULL,
|
|
736
|
-
from_device TEXT NOT NULL DEFAULT 'local',
|
|
737
|
-
target_agent TEXT NOT NULL,
|
|
738
|
-
target_project TEXT,
|
|
739
|
-
target_device TEXT NOT NULL DEFAULT 'local',
|
|
740
|
-
content TEXT NOT NULL,
|
|
741
|
-
priority TEXT DEFAULT 'normal',
|
|
742
|
-
status TEXT DEFAULT 'pending',
|
|
743
|
-
server_seq INTEGER,
|
|
744
|
-
retry_count INTEGER DEFAULT 0,
|
|
745
|
-
created_at TEXT NOT NULL,
|
|
746
|
-
delivered_at TEXT,
|
|
747
|
-
processed_at TEXT,
|
|
748
|
-
failed_at TEXT,
|
|
749
|
-
failure_reason TEXT
|
|
750
|
-
);
|
|
751
|
-
|
|
752
|
-
CREATE INDEX IF NOT EXISTS idx_messages_target
|
|
753
|
-
ON messages(target_agent, status);
|
|
754
|
-
|
|
755
|
-
CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
|
|
756
|
-
ON messages(target_agent, from_agent, server_seq);
|
|
757
|
-
`);
|
|
758
|
-
try {
|
|
759
|
-
await client.execute({
|
|
760
|
-
sql: `UPDATE memories SET project_name = 'exe-create' WHERE project_name = 'web'`,
|
|
761
|
-
args: []
|
|
762
|
-
});
|
|
763
|
-
await client.execute({
|
|
764
|
-
sql: `UPDATE memories SET project_name = 'exe-os' WHERE project_name = 'worker'`,
|
|
765
|
-
args: []
|
|
766
|
-
});
|
|
767
|
-
await client.execute({
|
|
768
|
-
sql: `UPDATE tasks SET project_name = 'exe-create' WHERE project_name = 'web'`,
|
|
769
|
-
args: []
|
|
770
|
-
});
|
|
771
|
-
await client.execute({
|
|
772
|
-
sql: `UPDATE tasks SET project_name = 'exe-os' WHERE project_name = 'worker'`,
|
|
773
|
-
args: []
|
|
774
|
-
});
|
|
775
|
-
} catch {
|
|
776
|
-
}
|
|
777
|
-
await client.executeMultiple(`
|
|
778
|
-
CREATE TABLE IF NOT EXISTS trajectories (
|
|
779
|
-
id TEXT PRIMARY KEY,
|
|
780
|
-
task_id TEXT NOT NULL,
|
|
781
|
-
agent_id TEXT NOT NULL,
|
|
782
|
-
project_name TEXT NOT NULL,
|
|
783
|
-
task_title TEXT NOT NULL,
|
|
784
|
-
signature TEXT NOT NULL,
|
|
785
|
-
signature_hash TEXT NOT NULL,
|
|
786
|
-
tool_count INTEGER NOT NULL,
|
|
787
|
-
skill_id TEXT,
|
|
788
|
-
created_at TEXT NOT NULL
|
|
789
|
-
);
|
|
790
|
-
|
|
791
|
-
CREATE INDEX IF NOT EXISTS idx_trajectories_hash
|
|
792
|
-
ON trajectories(signature_hash);
|
|
793
|
-
|
|
794
|
-
CREATE INDEX IF NOT EXISTS idx_trajectories_agent
|
|
795
|
-
ON trajectories(agent_id);
|
|
796
|
-
`);
|
|
797
|
-
try {
|
|
798
|
-
await client.execute("ALTER TABLE trajectories ADD COLUMN skill_id TEXT");
|
|
799
|
-
} catch {
|
|
800
|
-
}
|
|
801
|
-
await client.executeMultiple(`
|
|
802
|
-
CREATE TABLE IF NOT EXISTS consolidations (
|
|
803
|
-
id TEXT PRIMARY KEY,
|
|
804
|
-
consolidated_memory_id TEXT NOT NULL,
|
|
805
|
-
source_memory_id TEXT NOT NULL,
|
|
806
|
-
created_at TEXT NOT NULL
|
|
807
|
-
);
|
|
808
|
-
|
|
809
|
-
CREATE INDEX IF NOT EXISTS idx_consolidations_source
|
|
810
|
-
ON consolidations(source_memory_id);
|
|
811
|
-
`);
|
|
812
|
-
await client.executeMultiple(`
|
|
813
|
-
CREATE TABLE IF NOT EXISTS audit_trail (
|
|
814
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
815
|
-
timestamp TEXT NOT NULL,
|
|
816
|
-
session_id TEXT NOT NULL,
|
|
817
|
-
agent_id TEXT NOT NULL,
|
|
818
|
-
tool TEXT NOT NULL,
|
|
819
|
-
input TEXT,
|
|
820
|
-
decision TEXT NOT NULL,
|
|
821
|
-
reason TEXT,
|
|
822
|
-
is_customer_facing INTEGER NOT NULL DEFAULT 0
|
|
823
|
-
);
|
|
824
|
-
|
|
825
|
-
CREATE INDEX IF NOT EXISTS idx_audit_trail_agent
|
|
826
|
-
ON audit_trail(agent_id, timestamp);
|
|
827
|
-
|
|
828
|
-
CREATE INDEX IF NOT EXISTS idx_audit_trail_session
|
|
829
|
-
ON audit_trail(session_id);
|
|
830
|
-
`);
|
|
831
|
-
try {
|
|
832
|
-
await client.execute({
|
|
833
|
-
sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
|
|
834
|
-
args: []
|
|
835
|
-
});
|
|
836
|
-
} catch {
|
|
837
|
-
}
|
|
838
|
-
try {
|
|
839
|
-
await client.execute({
|
|
840
|
-
sql: `ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5`,
|
|
841
|
-
args: []
|
|
842
|
-
});
|
|
843
|
-
} catch {
|
|
844
|
-
}
|
|
845
|
-
try {
|
|
846
|
-
await client.execute({
|
|
847
|
-
sql: `ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'`,
|
|
848
|
-
args: []
|
|
849
|
-
});
|
|
850
|
-
} catch {
|
|
851
|
-
}
|
|
852
|
-
try {
|
|
853
|
-
await client.execute({
|
|
854
|
-
sql: `ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7`,
|
|
855
|
-
args: []
|
|
856
|
-
});
|
|
857
|
-
} catch {
|
|
858
|
-
}
|
|
859
|
-
try {
|
|
860
|
-
await client.execute({
|
|
861
|
-
sql: `ALTER TABLE memories ADD COLUMN last_accessed TEXT`,
|
|
862
|
-
args: []
|
|
863
|
-
});
|
|
864
|
-
} catch {
|
|
865
|
-
}
|
|
866
|
-
try {
|
|
867
|
-
await client.execute({
|
|
868
|
-
sql: `UPDATE memories SET last_accessed = timestamp WHERE last_accessed IS NULL`,
|
|
869
|
-
args: []
|
|
870
|
-
});
|
|
871
|
-
} catch {
|
|
872
|
-
}
|
|
873
|
-
try {
|
|
874
|
-
await client.execute({
|
|
875
|
-
sql: `ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0`,
|
|
876
|
-
args: []
|
|
877
|
-
});
|
|
878
|
-
} catch {
|
|
879
|
-
}
|
|
880
|
-
try {
|
|
881
|
-
await client.execute({
|
|
882
|
-
sql: `ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0`,
|
|
883
|
-
args: []
|
|
884
|
-
});
|
|
885
|
-
} catch {
|
|
886
|
-
}
|
|
887
|
-
for (const col of [
|
|
888
|
-
"ALTER TABLE memories ADD COLUMN content_hash TEXT",
|
|
889
|
-
"ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT"
|
|
890
|
-
]) {
|
|
891
|
-
try {
|
|
892
|
-
await client.execute(col);
|
|
893
|
-
} catch {
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
await client.executeMultiple(`
|
|
897
|
-
CREATE TABLE IF NOT EXISTS entities (
|
|
898
|
-
id TEXT PRIMARY KEY,
|
|
899
|
-
name TEXT NOT NULL,
|
|
900
|
-
type TEXT NOT NULL,
|
|
901
|
-
first_seen TEXT NOT NULL,
|
|
902
|
-
last_seen TEXT NOT NULL,
|
|
903
|
-
properties TEXT DEFAULT '{}',
|
|
904
|
-
UNIQUE(name, type)
|
|
905
|
-
);
|
|
906
|
-
|
|
907
|
-
CREATE TABLE IF NOT EXISTS relationships (
|
|
908
|
-
id TEXT PRIMARY KEY,
|
|
909
|
-
source_entity_id TEXT NOT NULL,
|
|
910
|
-
target_entity_id TEXT NOT NULL,
|
|
911
|
-
type TEXT NOT NULL,
|
|
912
|
-
weight REAL DEFAULT 1.0,
|
|
913
|
-
timestamp TEXT NOT NULL,
|
|
914
|
-
properties TEXT DEFAULT '{}',
|
|
915
|
-
UNIQUE(source_entity_id, target_entity_id, type)
|
|
916
|
-
);
|
|
917
|
-
|
|
918
|
-
CREATE TABLE IF NOT EXISTS entity_memories (
|
|
919
|
-
entity_id TEXT NOT NULL,
|
|
920
|
-
memory_id TEXT NOT NULL,
|
|
921
|
-
PRIMARY KEY (entity_id, memory_id)
|
|
922
|
-
);
|
|
923
|
-
|
|
924
|
-
CREATE TABLE IF NOT EXISTS relationship_memories (
|
|
925
|
-
relationship_id TEXT NOT NULL,
|
|
926
|
-
memory_id TEXT NOT NULL,
|
|
927
|
-
PRIMARY KEY (relationship_id, memory_id)
|
|
928
|
-
);
|
|
929
|
-
|
|
930
|
-
CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
|
|
931
|
-
CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
|
|
932
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
|
|
933
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
|
|
934
|
-
|
|
935
|
-
CREATE TABLE IF NOT EXISTS hyperedges (
|
|
936
|
-
id TEXT PRIMARY KEY,
|
|
937
|
-
label TEXT NOT NULL,
|
|
938
|
-
relation TEXT NOT NULL,
|
|
939
|
-
confidence REAL DEFAULT 1.0,
|
|
940
|
-
timestamp TEXT NOT NULL
|
|
941
|
-
);
|
|
942
|
-
|
|
943
|
-
CREATE TABLE IF NOT EXISTS hyperedge_nodes (
|
|
944
|
-
hyperedge_id TEXT NOT NULL,
|
|
945
|
-
entity_id TEXT NOT NULL,
|
|
946
|
-
PRIMARY KEY (hyperedge_id, entity_id)
|
|
947
|
-
);
|
|
948
|
-
`);
|
|
949
|
-
await client.executeMultiple(`
|
|
950
|
-
CREATE TABLE IF NOT EXISTS entity_aliases (
|
|
951
|
-
alias TEXT NOT NULL PRIMARY KEY,
|
|
952
|
-
canonical_entity_id TEXT NOT NULL
|
|
953
|
-
);
|
|
954
|
-
CREATE INDEX IF NOT EXISTS idx_entity_aliases_canonical ON entity_aliases(canonical_entity_id);
|
|
955
|
-
`);
|
|
956
|
-
for (const col of [
|
|
957
|
-
"ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
|
|
958
|
-
"ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
|
|
959
|
-
]) {
|
|
960
|
-
try {
|
|
961
|
-
await client.execute(col);
|
|
962
|
-
} catch {
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
|
-
try {
|
|
966
|
-
await client.execute(
|
|
967
|
-
`CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)`
|
|
968
|
-
);
|
|
969
|
-
} catch {
|
|
970
|
-
}
|
|
971
|
-
await client.executeMultiple(`
|
|
972
|
-
CREATE TABLE IF NOT EXISTS identity (
|
|
973
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
974
|
-
agent_id TEXT NOT NULL UNIQUE,
|
|
975
|
-
content_hash TEXT NOT NULL,
|
|
976
|
-
updated_at TEXT NOT NULL,
|
|
977
|
-
updated_by TEXT NOT NULL
|
|
978
|
-
);
|
|
979
|
-
|
|
980
|
-
CREATE INDEX IF NOT EXISTS idx_identity_agent ON identity(agent_id);
|
|
981
|
-
`);
|
|
982
|
-
await client.executeMultiple(`
|
|
983
|
-
CREATE TABLE IF NOT EXISTS chat_history (
|
|
984
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
985
|
-
session_id TEXT NOT NULL,
|
|
986
|
-
role TEXT NOT NULL,
|
|
987
|
-
content TEXT NOT NULL,
|
|
988
|
-
tool_name TEXT,
|
|
989
|
-
tool_id TEXT,
|
|
990
|
-
is_error INTEGER NOT NULL DEFAULT 0,
|
|
991
|
-
timestamp INTEGER NOT NULL
|
|
992
|
-
);
|
|
993
|
-
|
|
994
|
-
CREATE INDEX IF NOT EXISTS idx_chat_history_session
|
|
995
|
-
ON chat_history(session_id, id);
|
|
996
|
-
`);
|
|
997
|
-
await client.executeMultiple(`
|
|
998
|
-
CREATE TABLE IF NOT EXISTS workspaces (
|
|
999
|
-
id TEXT PRIMARY KEY,
|
|
1000
|
-
slug TEXT NOT NULL UNIQUE,
|
|
1001
|
-
name TEXT NOT NULL,
|
|
1002
|
-
owner_agent_id TEXT,
|
|
1003
|
-
created_at TEXT NOT NULL,
|
|
1004
|
-
metadata TEXT
|
|
1005
|
-
);
|
|
1006
|
-
|
|
1007
|
-
CREATE INDEX IF NOT EXISTS idx_workspaces_slug
|
|
1008
|
-
ON workspaces(slug);
|
|
1009
|
-
`);
|
|
1010
|
-
await client.executeMultiple(`
|
|
1011
|
-
CREATE TABLE IF NOT EXISTS documents (
|
|
1012
|
-
id TEXT PRIMARY KEY,
|
|
1013
|
-
workspace_id TEXT NOT NULL,
|
|
1014
|
-
filename TEXT NOT NULL,
|
|
1015
|
-
mime TEXT,
|
|
1016
|
-
source_type TEXT,
|
|
1017
|
-
user_id TEXT,
|
|
1018
|
-
uploaded_at TEXT NOT NULL,
|
|
1019
|
-
metadata TEXT,
|
|
1020
|
-
FOREIGN KEY (workspace_id) REFERENCES workspaces(id)
|
|
1021
|
-
);
|
|
1022
|
-
|
|
1023
|
-
CREATE INDEX IF NOT EXISTS idx_documents_workspace
|
|
1024
|
-
ON documents(workspace_id);
|
|
1025
|
-
|
|
1026
|
-
CREATE INDEX IF NOT EXISTS idx_documents_user
|
|
1027
|
-
ON documents(user_id);
|
|
1028
|
-
`);
|
|
1029
|
-
for (const column of [
|
|
1030
|
-
"workspace_id TEXT",
|
|
1031
|
-
"document_id TEXT",
|
|
1032
|
-
"user_id TEXT",
|
|
1033
|
-
"char_offset INTEGER",
|
|
1034
|
-
"page_number INTEGER"
|
|
1035
|
-
]) {
|
|
1036
|
-
try {
|
|
1037
|
-
await client.execute({
|
|
1038
|
-
sql: `ALTER TABLE memories ADD COLUMN ${column}`,
|
|
1039
|
-
args: []
|
|
1040
|
-
});
|
|
1041
|
-
} catch {
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
await client.executeMultiple(`
|
|
1045
|
-
CREATE INDEX IF NOT EXISTS idx_memories_workspace
|
|
1046
|
-
ON memories(workspace_id);
|
|
1047
|
-
|
|
1048
|
-
CREATE INDEX IF NOT EXISTS idx_memories_document
|
|
1049
|
-
ON memories(document_id);
|
|
1050
|
-
|
|
1051
|
-
CREATE INDEX IF NOT EXISTS idx_memories_user
|
|
1052
|
-
ON memories(user_id);
|
|
1053
|
-
`);
|
|
1054
|
-
await client.executeMultiple(`
|
|
1055
|
-
CREATE TABLE IF NOT EXISTS session_kills (
|
|
1056
|
-
id TEXT PRIMARY KEY,
|
|
1057
|
-
session_name TEXT NOT NULL,
|
|
1058
|
-
agent_id TEXT NOT NULL,
|
|
1059
|
-
killed_at TIMESTAMP NOT NULL,
|
|
1060
|
-
reason TEXT NOT NULL,
|
|
1061
|
-
ticks_idle INTEGER,
|
|
1062
|
-
estimated_tokens_saved INTEGER
|
|
1063
|
-
);
|
|
1064
|
-
|
|
1065
|
-
CREATE INDEX IF NOT EXISTS idx_session_kills_killed_at
|
|
1066
|
-
ON session_kills(killed_at);
|
|
1067
|
-
|
|
1068
|
-
CREATE INDEX IF NOT EXISTS idx_session_kills_agent
|
|
1069
|
-
ON session_kills(agent_id);
|
|
1070
|
-
`);
|
|
1071
|
-
await client.executeMultiple(`
|
|
1072
|
-
CREATE TABLE IF NOT EXISTS conversations (
|
|
1073
|
-
id TEXT PRIMARY KEY,
|
|
1074
|
-
platform TEXT NOT NULL,
|
|
1075
|
-
external_id TEXT,
|
|
1076
|
-
sender_id TEXT NOT NULL,
|
|
1077
|
-
sender_name TEXT,
|
|
1078
|
-
sender_phone TEXT,
|
|
1079
|
-
sender_email TEXT,
|
|
1080
|
-
recipient_id TEXT,
|
|
1081
|
-
channel_id TEXT NOT NULL,
|
|
1082
|
-
thread_id TEXT,
|
|
1083
|
-
reply_to_id TEXT,
|
|
1084
|
-
content_text TEXT,
|
|
1085
|
-
content_media TEXT,
|
|
1086
|
-
content_metadata TEXT,
|
|
1087
|
-
agent_response TEXT,
|
|
1088
|
-
agent_name TEXT,
|
|
1089
|
-
timestamp TEXT NOT NULL,
|
|
1090
|
-
ingested_at TEXT NOT NULL
|
|
1091
|
-
);
|
|
1092
|
-
|
|
1093
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_platform
|
|
1094
|
-
ON conversations(platform);
|
|
1095
|
-
|
|
1096
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_sender
|
|
1097
|
-
ON conversations(sender_id);
|
|
1098
|
-
|
|
1099
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_timestamp
|
|
1100
|
-
ON conversations(timestamp);
|
|
1101
|
-
|
|
1102
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_thread
|
|
1103
|
-
ON conversations(thread_id);
|
|
1104
|
-
|
|
1105
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_channel
|
|
1106
|
-
ON conversations(channel_id);
|
|
1107
|
-
`);
|
|
1108
|
-
await client.executeMultiple(`
|
|
1109
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS conversations_fts USING fts5(
|
|
1110
|
-
content_text,
|
|
1111
|
-
sender_name,
|
|
1112
|
-
agent_response,
|
|
1113
|
-
content='conversations',
|
|
1114
|
-
content_rowid='rowid'
|
|
1115
|
-
);
|
|
1116
|
-
|
|
1117
|
-
CREATE TRIGGER IF NOT EXISTS conversations_fts_ai AFTER INSERT ON conversations BEGIN
|
|
1118
|
-
INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
|
|
1119
|
-
VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
|
|
1120
|
-
END;
|
|
1121
|
-
|
|
1122
|
-
CREATE TRIGGER IF NOT EXISTS conversations_fts_ad AFTER DELETE ON conversations BEGIN
|
|
1123
|
-
INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
|
|
1124
|
-
VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
|
|
1125
|
-
END;
|
|
1126
|
-
|
|
1127
|
-
CREATE TRIGGER IF NOT EXISTS conversations_fts_au AFTER UPDATE ON conversations BEGIN
|
|
1128
|
-
INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
|
|
1129
|
-
VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
|
|
1130
|
-
INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
|
|
1131
|
-
VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
|
|
1132
|
-
END;
|
|
1133
|
-
`);
|
|
1134
|
-
}
|
|
1135
|
-
var disposeTurso = disposeDatabase;
|
|
1136
|
-
async function disposeDatabase() {
|
|
1137
|
-
if (_client) {
|
|
1138
|
-
_client.close();
|
|
1139
|
-
_client = null;
|
|
1140
|
-
}
|
|
1141
|
-
}
|
|
1142
|
-
|
|
1143
|
-
// src/lib/keychain.ts
|
|
1144
|
-
import { readFile, writeFile, unlink, mkdir, chmod } from "fs/promises";
|
|
1145
|
-
import { existsSync } from "fs";
|
|
1146
|
-
import path from "path";
|
|
1147
|
-
import crypto from "crypto";
|
|
1148
|
-
var SERVICE = "exe-mem";
|
|
1149
|
-
var ACCOUNT = "master-key";
|
|
1150
|
-
function getKeyDir() {
|
|
1151
|
-
return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? path.join(process.env.HOME ?? "/tmp", ".exe-os");
|
|
1152
|
-
}
|
|
1153
|
-
function getKeyPath() {
|
|
1154
|
-
return path.join(getKeyDir(), "master.key");
|
|
1155
|
-
}
|
|
1156
|
-
async function tryKeytar() {
|
|
1157
|
-
try {
|
|
1158
|
-
return await import("keytar");
|
|
1159
|
-
} catch {
|
|
1160
|
-
return null;
|
|
1161
|
-
}
|
|
1162
|
-
}
|
|
1163
|
-
async function getMasterKey() {
|
|
1164
|
-
const keytar = await tryKeytar();
|
|
1165
|
-
if (keytar) {
|
|
1166
|
-
try {
|
|
1167
|
-
const stored = await keytar.getPassword(SERVICE, ACCOUNT);
|
|
1168
|
-
if (stored) {
|
|
1169
|
-
return Buffer.from(stored, "base64");
|
|
1170
|
-
}
|
|
1171
|
-
} catch {
|
|
1172
|
-
}
|
|
1173
|
-
}
|
|
1174
|
-
const keyPath = getKeyPath();
|
|
1175
|
-
if (!existsSync(keyPath)) {
|
|
1176
|
-
return null;
|
|
1177
|
-
}
|
|
1178
|
-
try {
|
|
1179
|
-
const content = await readFile(keyPath, "utf-8");
|
|
1180
|
-
return Buffer.from(content.trim(), "base64");
|
|
1181
|
-
} catch {
|
|
1182
|
-
return null;
|
|
1183
|
-
}
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
|
-
// src/lib/store.ts
|
|
1187
|
-
init_config();
|
|
1188
|
-
var _pendingRecords = [];
|
|
1189
|
-
var _batchSize = 20;
|
|
1190
|
-
var _flushIntervalMs = 1e4;
|
|
1191
|
-
var _flushTimer = null;
|
|
1192
|
-
var _flushing = false;
|
|
1193
|
-
var _nextVersion = 1;
|
|
1194
|
-
async function initStore(options) {
|
|
1195
|
-
if (_flushTimer !== null) {
|
|
1196
|
-
clearInterval(_flushTimer);
|
|
1197
|
-
_flushTimer = null;
|
|
1198
|
-
}
|
|
1199
|
-
_pendingRecords = [];
|
|
1200
|
-
_flushing = false;
|
|
1201
|
-
_batchSize = options?.batchSize ?? 20;
|
|
1202
|
-
_flushIntervalMs = options?.flushIntervalMs ?? 1e4;
|
|
1203
|
-
let dbPath = options?.dbPath;
|
|
1204
|
-
if (!dbPath) {
|
|
1205
|
-
const config = await loadConfig();
|
|
1206
|
-
dbPath = config.dbPath;
|
|
1207
|
-
}
|
|
1208
|
-
let masterKey = options?.masterKey ?? null;
|
|
1209
|
-
if (!masterKey) {
|
|
1210
|
-
masterKey = await getMasterKey();
|
|
1211
|
-
if (!masterKey) {
|
|
1212
|
-
throw new Error(
|
|
1213
|
-
"No encryption key found. Run /exe-setup to generate one."
|
|
1214
|
-
);
|
|
1215
|
-
}
|
|
1216
|
-
}
|
|
1217
|
-
const hexKey = masterKey.toString("hex");
|
|
1218
|
-
await initTurso({
|
|
1219
|
-
dbPath,
|
|
1220
|
-
encryptionKey: hexKey
|
|
1221
|
-
});
|
|
1222
|
-
await ensureSchema();
|
|
1223
|
-
try {
|
|
1224
|
-
const { initShardManager: initShardManager2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|
|
1225
|
-
initShardManager2(hexKey);
|
|
1226
|
-
} catch {
|
|
1227
|
-
}
|
|
1228
|
-
const client = getClient();
|
|
1229
|
-
const vResult = await client.execute("SELECT MAX(version) as max_v FROM memories");
|
|
1230
|
-
_nextVersion = (Number(vResult.rows[0]?.max_v) || 0) + 1;
|
|
1231
|
-
}
|
|
1232
|
-
async function flushBatch() {
|
|
1233
|
-
if (_flushing || _pendingRecords.length === 0) return 0;
|
|
1234
|
-
_flushing = true;
|
|
1235
|
-
try {
|
|
1236
|
-
const batch = _pendingRecords.slice(0);
|
|
1237
|
-
const buildStmt = (row) => {
|
|
1238
|
-
const hasVector = row.vector !== null;
|
|
1239
|
-
const taskId = row.task_id ?? null;
|
|
1240
|
-
const importance = row.importance ?? 5;
|
|
1241
|
-
const status = row.status ?? "active";
|
|
1242
|
-
const confidence = row.confidence ?? 0.7;
|
|
1243
|
-
const lastAccessed = row.last_accessed ?? row.timestamp;
|
|
1244
|
-
const workspaceId = row.workspace_id ?? null;
|
|
1245
|
-
const documentId = row.document_id ?? null;
|
|
1246
|
-
const userId = row.user_id ?? null;
|
|
1247
|
-
const charOffset = row.char_offset ?? null;
|
|
1248
|
-
const pageNumber = row.page_number ?? null;
|
|
1249
|
-
return {
|
|
1250
|
-
sql: hasVector ? `INSERT OR IGNORE INTO memories
|
|
1251
|
-
(id, agent_id, agent_role, session_id, timestamp,
|
|
1252
|
-
tool_name, project_name,
|
|
1253
|
-
has_error, raw_text, vector, version, task_id, importance, status,
|
|
1254
|
-
confidence, last_accessed,
|
|
1255
|
-
workspace_id, document_id, user_id, char_offset, page_number)
|
|
1256
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, vector32(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` : `INSERT OR IGNORE INTO memories
|
|
1257
|
-
(id, agent_id, agent_role, session_id, timestamp,
|
|
1258
|
-
tool_name, project_name,
|
|
1259
|
-
has_error, raw_text, vector, version, task_id, importance, status,
|
|
1260
|
-
confidence, last_accessed,
|
|
1261
|
-
workspace_id, document_id, user_id, char_offset, page_number)
|
|
1262
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
1263
|
-
args: hasVector ? [
|
|
1264
|
-
row.id,
|
|
1265
|
-
row.agent_id,
|
|
1266
|
-
row.agent_role,
|
|
1267
|
-
row.session_id,
|
|
1268
|
-
row.timestamp,
|
|
1269
|
-
row.tool_name,
|
|
1270
|
-
row.project_name,
|
|
1271
|
-
row.has_error,
|
|
1272
|
-
row.raw_text,
|
|
1273
|
-
vectorToBlob(row.vector),
|
|
1274
|
-
row.version,
|
|
1275
|
-
taskId,
|
|
1276
|
-
importance,
|
|
1277
|
-
status,
|
|
1278
|
-
confidence,
|
|
1279
|
-
lastAccessed,
|
|
1280
|
-
workspaceId,
|
|
1281
|
-
documentId,
|
|
1282
|
-
userId,
|
|
1283
|
-
charOffset,
|
|
1284
|
-
pageNumber
|
|
1285
|
-
] : [
|
|
1286
|
-
row.id,
|
|
1287
|
-
row.agent_id,
|
|
1288
|
-
row.agent_role,
|
|
1289
|
-
row.session_id,
|
|
1290
|
-
row.timestamp,
|
|
1291
|
-
row.tool_name,
|
|
1292
|
-
row.project_name,
|
|
1293
|
-
row.has_error,
|
|
1294
|
-
row.raw_text,
|
|
1295
|
-
row.version,
|
|
1296
|
-
taskId,
|
|
1297
|
-
importance,
|
|
1298
|
-
status,
|
|
1299
|
-
confidence,
|
|
1300
|
-
lastAccessed,
|
|
1301
|
-
workspaceId,
|
|
1302
|
-
documentId,
|
|
1303
|
-
userId,
|
|
1304
|
-
charOffset,
|
|
1305
|
-
pageNumber
|
|
1306
|
-
]
|
|
1307
|
-
};
|
|
1308
|
-
};
|
|
1309
|
-
const globalClient = getClient();
|
|
1310
|
-
const globalStmts = batch.map(buildStmt);
|
|
1311
|
-
await globalClient.batch(globalStmts, "write");
|
|
1312
|
-
_pendingRecords.splice(0, batch.length);
|
|
1313
|
-
try {
|
|
1314
|
-
const { isShardingEnabled: isShardingEnabled2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|
|
1315
|
-
if (isShardingEnabled2()) {
|
|
1316
|
-
const byProject = /* @__PURE__ */ new Map();
|
|
1317
|
-
for (const row of batch) {
|
|
1318
|
-
const proj = row.project_name || "unknown";
|
|
1319
|
-
if (!byProject.has(proj)) byProject.set(proj, []);
|
|
1320
|
-
byProject.get(proj).push(row);
|
|
1321
|
-
}
|
|
1322
|
-
for (const [project, rows] of byProject) {
|
|
1323
|
-
try {
|
|
1324
|
-
const shardClient = await getReadyShardClient2(project);
|
|
1325
|
-
const shardStmts = rows.map(buildStmt);
|
|
1326
|
-
await shardClient.batch(shardStmts, "write");
|
|
1327
|
-
} catch (err) {
|
|
1328
|
-
process.stderr.write(
|
|
1329
|
-
`[store] Shard write failed for ${project}: ${err instanceof Error ? err.message : String(err)}
|
|
1330
|
-
`
|
|
1331
|
-
);
|
|
1332
|
-
}
|
|
1333
|
-
}
|
|
1334
|
-
}
|
|
1335
|
-
} catch {
|
|
1336
|
-
}
|
|
1337
|
-
return batch.length;
|
|
1338
|
-
} finally {
|
|
1339
|
-
_flushing = false;
|
|
1340
|
-
}
|
|
1341
|
-
}
|
|
1342
|
-
async function disposeStore() {
|
|
1343
|
-
if (_flushTimer !== null) {
|
|
1344
|
-
clearInterval(_flushTimer);
|
|
1345
|
-
_flushTimer = null;
|
|
1346
|
-
}
|
|
1347
|
-
if (_pendingRecords.length > 0) {
|
|
1348
|
-
await flushBatch();
|
|
1349
|
-
}
|
|
1350
|
-
await disposeTurso();
|
|
1351
|
-
_pendingRecords = [];
|
|
1352
|
-
_nextVersion = 1;
|
|
1353
|
-
}
|
|
1354
|
-
function vectorToBlob(vector) {
|
|
1355
|
-
const f32 = vector instanceof Float32Array ? vector : new Float32Array(vector);
|
|
1356
|
-
return JSON.stringify(Array.from(f32));
|
|
1357
|
-
}
|
|
1358
|
-
|
|
1359
|
-
// src/lib/graph-rag.ts
|
|
1360
|
-
import crypto2 from "crypto";
|
|
1361
|
-
|
|
1362
|
-
// src/lib/code-chunker.ts
|
|
1363
|
-
import ts from "typescript";
|
|
1364
|
-
function chunkSourceFile(source, fileName = "file.ts") {
|
|
1365
|
-
const sourceFile = ts.createSourceFile(
|
|
1366
|
-
fileName,
|
|
1367
|
-
source,
|
|
1368
|
-
ts.ScriptTarget.Latest,
|
|
1369
|
-
true,
|
|
1370
|
-
// setParentNodes
|
|
1371
|
-
fileName.endsWith(".tsx") || fileName.endsWith(".jsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS
|
|
1372
|
-
);
|
|
1373
|
-
const chunks = [];
|
|
1374
|
-
const lines = source.split("\n");
|
|
1375
|
-
const importLines = [];
|
|
1376
|
-
function getLineNumber(pos) {
|
|
1377
|
-
return sourceFile.getLineAndCharacterOfPosition(pos).line + 1;
|
|
1378
|
-
}
|
|
1379
|
-
function getLeadingComment(node) {
|
|
1380
|
-
const fullText = sourceFile.getFullText();
|
|
1381
|
-
const ranges = ts.getLeadingCommentRanges(fullText, node.getFullStart());
|
|
1382
|
-
if (!ranges || ranges.length === 0) return void 0;
|
|
1383
|
-
return ranges.map((r) => fullText.slice(r.pos, r.end)).join("\n");
|
|
1384
|
-
}
|
|
1385
|
-
function getNodeText(node) {
|
|
1386
|
-
return node.getText(sourceFile);
|
|
1387
|
-
}
|
|
1388
|
-
function getName(node) {
|
|
1389
|
-
if (ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) {
|
|
1390
|
-
return node.name?.getText(sourceFile) ?? "(anonymous)";
|
|
1391
|
-
}
|
|
1392
|
-
if (ts.isClassDeclaration(node)) {
|
|
1393
|
-
return node.name?.getText(sourceFile) ?? "(anonymous class)";
|
|
1394
|
-
}
|
|
1395
|
-
if (ts.isInterfaceDeclaration(node)) {
|
|
1396
|
-
return node.name.getText(sourceFile);
|
|
1397
|
-
}
|
|
1398
|
-
if (ts.isTypeAliasDeclaration(node)) {
|
|
1399
|
-
return node.name.getText(sourceFile);
|
|
1400
|
-
}
|
|
1401
|
-
if (ts.isEnumDeclaration(node)) {
|
|
1402
|
-
return node.name.getText(sourceFile);
|
|
1403
|
-
}
|
|
1404
|
-
if (ts.isVariableStatement(node)) {
|
|
1405
|
-
const decls = node.declarationList.declarations;
|
|
1406
|
-
return decls.map((d) => d.name.getText(sourceFile)).join(", ");
|
|
1407
|
-
}
|
|
1408
|
-
if (ts.isExportAssignment(node)) {
|
|
1409
|
-
return "default export";
|
|
1410
|
-
}
|
|
1411
|
-
return "(unknown)";
|
|
1412
|
-
}
|
|
1413
|
-
function visitTopLevel(node) {
|
|
1414
|
-
if (ts.isImportDeclaration(node)) {
|
|
1415
|
-
importLines.push({
|
|
1416
|
-
start: getLineNumber(node.getStart(sourceFile)),
|
|
1417
|
-
end: getLineNumber(node.getEnd())
|
|
1418
|
-
});
|
|
1419
|
-
return;
|
|
1420
|
-
}
|
|
1421
|
-
if (ts.isFunctionDeclaration(node)) {
|
|
1422
|
-
chunks.push({
|
|
1423
|
-
kind: "function",
|
|
1424
|
-
name: getName(node),
|
|
1425
|
-
text: getNodeText(node),
|
|
1426
|
-
startLine: getLineNumber(node.getStart(sourceFile)),
|
|
1427
|
-
endLine: getLineNumber(node.getEnd()),
|
|
1428
|
-
comment: getLeadingComment(node)
|
|
1429
|
-
});
|
|
1430
|
-
return;
|
|
1431
|
-
}
|
|
1432
|
-
if (ts.isClassDeclaration(node)) {
|
|
1433
|
-
chunks.push({
|
|
1434
|
-
kind: "class",
|
|
1435
|
-
name: getName(node),
|
|
1436
|
-
text: getNodeText(node),
|
|
1437
|
-
startLine: getLineNumber(node.getStart(sourceFile)),
|
|
1438
|
-
endLine: getLineNumber(node.getEnd()),
|
|
1439
|
-
comment: getLeadingComment(node)
|
|
1440
|
-
});
|
|
1441
|
-
return;
|
|
1442
|
-
}
|
|
1443
|
-
if (ts.isInterfaceDeclaration(node)) {
|
|
1444
|
-
chunks.push({
|
|
1445
|
-
kind: "type",
|
|
1446
|
-
name: getName(node),
|
|
1447
|
-
text: getNodeText(node),
|
|
1448
|
-
startLine: getLineNumber(node.getStart(sourceFile)),
|
|
1449
|
-
endLine: getLineNumber(node.getEnd()),
|
|
1450
|
-
comment: getLeadingComment(node)
|
|
1451
|
-
});
|
|
1452
|
-
return;
|
|
1453
|
-
}
|
|
1454
|
-
if (ts.isTypeAliasDeclaration(node)) {
|
|
1455
|
-
chunks.push({
|
|
1456
|
-
kind: "type",
|
|
1457
|
-
name: getName(node),
|
|
1458
|
-
text: getNodeText(node),
|
|
1459
|
-
startLine: getLineNumber(node.getStart(sourceFile)),
|
|
1460
|
-
endLine: getLineNumber(node.getEnd()),
|
|
1461
|
-
comment: getLeadingComment(node)
|
|
1462
|
-
});
|
|
1463
|
-
return;
|
|
1464
|
-
}
|
|
1465
|
-
if (ts.isEnumDeclaration(node)) {
|
|
1466
|
-
chunks.push({
|
|
1467
|
-
kind: "type",
|
|
1468
|
-
name: getName(node),
|
|
1469
|
-
text: getNodeText(node),
|
|
1470
|
-
startLine: getLineNumber(node.getStart(sourceFile)),
|
|
1471
|
-
endLine: getLineNumber(node.getEnd()),
|
|
1472
|
-
comment: getLeadingComment(node)
|
|
1473
|
-
});
|
|
1474
|
-
return;
|
|
1475
|
-
}
|
|
1476
|
-
if (ts.isVariableStatement(node)) {
|
|
1477
|
-
const decls = node.declarationList.declarations;
|
|
1478
|
-
const isFnLike = decls.some(
|
|
1479
|
-
(d) => d.initializer && (ts.isArrowFunction(d.initializer) || ts.isFunctionExpression(d.initializer))
|
|
1480
|
-
);
|
|
1481
|
-
chunks.push({
|
|
1482
|
-
kind: isFnLike ? "function" : "variable",
|
|
1483
|
-
name: getName(node),
|
|
1484
|
-
text: getNodeText(node),
|
|
1485
|
-
startLine: getLineNumber(node.getStart(sourceFile)),
|
|
1486
|
-
endLine: getLineNumber(node.getEnd()),
|
|
1487
|
-
comment: getLeadingComment(node)
|
|
1488
|
-
});
|
|
1489
|
-
return;
|
|
1490
|
-
}
|
|
1491
|
-
if (ts.isExportAssignment(node)) {
|
|
1492
|
-
chunks.push({
|
|
1493
|
-
kind: "export",
|
|
1494
|
-
name: "default export",
|
|
1495
|
-
text: getNodeText(node),
|
|
1496
|
-
startLine: getLineNumber(node.getStart(sourceFile)),
|
|
1497
|
-
endLine: getLineNumber(node.getEnd()),
|
|
1498
|
-
comment: getLeadingComment(node)
|
|
1499
|
-
});
|
|
1500
|
-
return;
|
|
1501
|
-
}
|
|
1502
|
-
if (ts.isExpressionStatement(node)) {
|
|
1503
|
-
const text = getNodeText(node);
|
|
1504
|
-
if (text.length > 10) {
|
|
1505
|
-
chunks.push({
|
|
1506
|
-
kind: "other",
|
|
1507
|
-
name: text.slice(0, 40).replace(/\n/g, " "),
|
|
1508
|
-
text,
|
|
1509
|
-
startLine: getLineNumber(node.getStart(sourceFile)),
|
|
1510
|
-
endLine: getLineNumber(node.getEnd())
|
|
1511
|
-
});
|
|
1512
|
-
}
|
|
1513
|
-
return;
|
|
1514
|
-
}
|
|
1515
|
-
}
|
|
1516
|
-
sourceFile.statements.forEach(visitTopLevel);
|
|
1517
|
-
if (importLines.length > 0) {
|
|
1518
|
-
const startLine = importLines[0].start;
|
|
1519
|
-
const endLine = importLines[importLines.length - 1].end;
|
|
1520
|
-
const importText = lines.slice(startLine - 1, endLine).join("\n");
|
|
1521
|
-
chunks.unshift({
|
|
1522
|
-
kind: "import",
|
|
1523
|
-
name: `${importLines.length} imports`,
|
|
1524
|
-
text: importText,
|
|
1525
|
-
startLine,
|
|
1526
|
-
endLine
|
|
1527
|
-
});
|
|
1528
|
-
}
|
|
1529
|
-
chunks.sort((a, b) => a.startLine - b.startLine);
|
|
1530
|
-
return chunks;
|
|
1531
|
-
}
|
|
1532
|
-
function isChunkable(filePath) {
|
|
1533
|
-
const ext = filePath.split(".").pop()?.toLowerCase();
|
|
1534
|
-
return ext === "ts" || ext === "tsx" || ext === "js" || ext === "jsx";
|
|
1535
|
-
}
|
|
1536
|
-
|
|
1537
|
-
// src/lib/graph-rag.ts
|
|
1538
|
-
function normalizeEntityName(name) {
|
|
1539
|
-
return name.replace(/\s*\([^)]*\)\s*/g, "").trim().toLowerCase();
|
|
1540
|
-
}
|
|
1541
|
-
function entityId(name, type) {
|
|
1542
|
-
const normalized = normalizeEntityName(name);
|
|
1543
|
-
return crypto2.createHash("sha256").update(`${normalized}::${type.toLowerCase()}`).digest("hex").slice(0, 16);
|
|
1544
|
-
}
|
|
1545
|
-
async function resolveAlias(client, name) {
|
|
1546
|
-
const normalized = normalizeEntityName(name);
|
|
1547
|
-
try {
|
|
1548
|
-
const result = await client.execute({
|
|
1549
|
-
sql: "SELECT canonical_entity_id FROM entity_aliases WHERE alias = ?",
|
|
1550
|
-
args: [normalized]
|
|
1551
|
-
});
|
|
1552
|
-
if (result.rows.length > 0) {
|
|
1553
|
-
return String(result.rows[0].canonical_entity_id);
|
|
1554
|
-
}
|
|
1555
|
-
} catch {
|
|
1556
|
-
}
|
|
1557
|
-
return null;
|
|
1558
|
-
}
|
|
1559
|
-
var EXTRACT_TOOL = {
|
|
1560
|
-
name: "extract_entities_and_relationships",
|
|
1561
|
-
description: "Extract entities, relationships, and group connections from a memory record. Include confidence scoring.",
|
|
1562
|
-
input_schema: {
|
|
1563
|
-
type: "object",
|
|
1564
|
-
properties: {
|
|
1565
|
-
entities: {
|
|
1566
|
-
type: "array",
|
|
1567
|
-
items: {
|
|
1568
|
-
type: "object",
|
|
1569
|
-
properties: {
|
|
1570
|
-
name: { type: "string", description: "Entity name" },
|
|
1571
|
-
type: { type: "string", enum: ["person", "tool", "project", "decision", "concept", "file", "rationale", "product", "company", "location", "order", "supplier", "category"] }
|
|
1572
|
-
},
|
|
1573
|
-
required: ["name", "type"]
|
|
1574
|
-
}
|
|
1575
|
-
},
|
|
1576
|
-
relationships: {
|
|
1577
|
-
type: "array",
|
|
1578
|
-
items: {
|
|
1579
|
-
type: "object",
|
|
1580
|
-
properties: {
|
|
1581
|
-
source: { type: "string" },
|
|
1582
|
-
source_type: { type: "string" },
|
|
1583
|
-
target: { type: "string" },
|
|
1584
|
-
target_type: { type: "string" },
|
|
1585
|
-
relationship: {
|
|
1586
|
-
type: "string",
|
|
1587
|
-
enum: [
|
|
1588
|
-
// Engineering
|
|
1589
|
-
"implemented",
|
|
1590
|
-
"superseded_by",
|
|
1591
|
-
"depends_on",
|
|
1592
|
-
"blocked_by",
|
|
1593
|
-
"worked_on",
|
|
1594
|
-
"uses",
|
|
1595
|
-
"decided",
|
|
1596
|
-
"created",
|
|
1597
|
-
"fixed",
|
|
1598
|
-
"reviewed",
|
|
1599
|
-
"rationale_for",
|
|
1600
|
-
// Business / organizational
|
|
1601
|
-
"supplies",
|
|
1602
|
-
"ordered_from",
|
|
1603
|
-
"reports_to",
|
|
1604
|
-
"manages",
|
|
1605
|
-
"located_at",
|
|
1606
|
-
"ships_to",
|
|
1607
|
-
"categorized_as",
|
|
1608
|
-
"priced_at",
|
|
1609
|
-
"partnered_with",
|
|
1610
|
-
"contracted_by",
|
|
1611
|
-
// General purpose
|
|
1612
|
-
"related_to",
|
|
1613
|
-
"part_of",
|
|
1614
|
-
"owns",
|
|
1615
|
-
"communicates_with"
|
|
1616
|
-
]
|
|
1617
|
-
},
|
|
1618
|
-
confidence: { type: "number", description: "0.0-1.0. 1.0=explicitly stated, 0.6-0.9=inferred, 0.1-0.3=ambiguous" },
|
|
1619
|
-
confidence_label: { type: "string", enum: ["extracted", "inferred", "ambiguous"] }
|
|
1620
|
-
},
|
|
1621
|
-
required: ["source", "source_type", "target", "target_type", "relationship", "confidence", "confidence_label"]
|
|
1622
|
-
}
|
|
1623
|
-
},
|
|
1624
|
-
hyperedges: {
|
|
1625
|
-
type: "array",
|
|
1626
|
-
items: {
|
|
1627
|
-
type: "object",
|
|
1628
|
-
properties: {
|
|
1629
|
-
label: { type: "string", description: "Group label (e.g., 'auth flow files', 'v1.3 contributors')" },
|
|
1630
|
-
relation: { type: "string", description: "What connects them (e.g., 'part_of', 'contributed_to')" },
|
|
1631
|
-
entity_names: { type: "array", items: { type: "string" }, description: "3+ entity names in this group" },
|
|
1632
|
-
confidence: { type: "number" }
|
|
1633
|
-
},
|
|
1634
|
-
required: ["label", "relation", "entity_names", "confidence"]
|
|
1635
|
-
},
|
|
1636
|
-
description: "Group relationships connecting 3+ entities"
|
|
1637
|
-
}
|
|
1638
|
-
},
|
|
1639
|
-
required: ["entities", "relationships", "hyperedges"]
|
|
1640
|
-
}
|
|
1641
|
-
};
|
|
1642
|
-
var READ_MEMORY_PATH_PATTERN = /^Read\s+(\S+\.(?:ts|tsx|js|jsx))\s*\n/;
|
|
1643
|
-
function parseImportPaths(importText) {
|
|
1644
|
-
const paths = [];
|
|
1645
|
-
const fromRegex = /from\s+["']([^"']+)["']/g;
|
|
1646
|
-
let match;
|
|
1647
|
-
while ((match = fromRegex.exec(importText)) !== null) {
|
|
1648
|
-
paths.push(match[1]);
|
|
1649
|
-
}
|
|
1650
|
-
const bareRegex = /^import\s+["']([^"']+)["']/gm;
|
|
1651
|
-
while ((match = bareRegex.exec(importText)) !== null) {
|
|
1652
|
-
if (!paths.includes(match[1])) paths.push(match[1]);
|
|
1653
|
-
}
|
|
1654
|
-
return paths;
|
|
1655
|
-
}
|
|
1656
|
-
function moduleNameFromPath(importPath) {
|
|
1657
|
-
const base = importPath.split("/").pop() ?? importPath;
|
|
1658
|
-
return base.replace(/\.\w+$/, "") || base;
|
|
1659
|
-
}
|
|
1660
|
-
function extractFromCode(source, filePath) {
|
|
1661
|
-
let chunks;
|
|
1662
|
-
try {
|
|
1663
|
-
chunks = chunkSourceFile(source, filePath);
|
|
1664
|
-
} catch {
|
|
1665
|
-
return { entities: [], relationships: [], hyperedges: [] };
|
|
1666
|
-
}
|
|
1667
|
-
if (chunks.length === 0) {
|
|
1668
|
-
return { entities: [], relationships: [], hyperedges: [] };
|
|
1669
|
-
}
|
|
1670
|
-
const entities = [];
|
|
1671
|
-
const relationships = [];
|
|
1672
|
-
const fileName = filePath.split("/").pop() ?? filePath;
|
|
1673
|
-
entities.push({ name: fileName, type: "file" });
|
|
1674
|
-
for (const chunk of chunks) {
|
|
1675
|
-
if (chunk.kind === "import") {
|
|
1676
|
-
const importPaths = parseImportPaths(chunk.text);
|
|
1677
|
-
for (const importPath of importPaths) {
|
|
1678
|
-
const moduleName = moduleNameFromPath(importPath);
|
|
1679
|
-
entities.push({ name: moduleName, type: "file" });
|
|
1680
|
-
relationships.push({
|
|
1681
|
-
source: fileName,
|
|
1682
|
-
sourceType: "file",
|
|
1683
|
-
target: moduleName,
|
|
1684
|
-
targetType: "file",
|
|
1685
|
-
relationship: "depends_on",
|
|
1686
|
-
confidence: 1,
|
|
1687
|
-
confidenceLabel: "extracted"
|
|
1688
|
-
});
|
|
1689
|
-
}
|
|
1690
|
-
continue;
|
|
1691
|
-
}
|
|
1692
|
-
if (chunk.kind === "other" || chunk.name === "(unknown)") continue;
|
|
1693
|
-
entities.push({ name: chunk.name, type: "concept" });
|
|
1694
|
-
relationships.push({
|
|
1695
|
-
source: fileName,
|
|
1696
|
-
sourceType: "file",
|
|
1697
|
-
target: chunk.name,
|
|
1698
|
-
targetType: "concept",
|
|
1699
|
-
relationship: "uses",
|
|
1700
|
-
confidence: 1,
|
|
1701
|
-
confidenceLabel: "extracted"
|
|
1702
|
-
});
|
|
1703
|
-
}
|
|
1704
|
-
return { entities, relationships, hyperedges: [] };
|
|
1705
|
-
}
|
|
1706
|
-
async function extractFromMemory(content, agentId, model = "claude-haiku-4-5-20251001", filePath) {
|
|
1707
|
-
if (content.length < 50) {
|
|
1708
|
-
return { entities: [], relationships: [], hyperedges: [] };
|
|
1709
|
-
}
|
|
1710
|
-
if (filePath && isChunkable(filePath)) {
|
|
1711
|
-
const astResult = extractFromCode(content, filePath);
|
|
1712
|
-
if (astResult.entities.length > 0) {
|
|
1713
|
-
return astResult;
|
|
1714
|
-
}
|
|
1715
|
-
}
|
|
1716
|
-
try {
|
|
1717
|
-
const Anthropic = (await import("@anthropic-ai/sdk")).default;
|
|
1718
|
-
const client = new Anthropic();
|
|
1719
|
-
const response = await client.messages.create({
|
|
1720
|
-
model,
|
|
1721
|
-
max_tokens: 512,
|
|
1722
|
-
system: `Extract entities and relationships from this AI agent work memory. Agent: ${agentId}. Focus on: people, tools, projects, decisions, files, rationale \u2014 and for business contexts: products, companies, suppliers, locations, orders, categories. Only extract what's clearly stated \u2014 don't infer. Use the simplest form of names (e.g., "Lenny" not "Lenny (HYGO)"). When content says "we chose X because Y", extract a rationale entity with rationale_for edge. Business examples: "Lenny supplies organic flour to HYGO" \u2192 (Lenny, supplies, HYGO). "Order #1234 shipped to Auckland" \u2192 (Order #1234, ships_to, Auckland).`,
|
|
1723
|
-
messages: [{ role: "user", content: content.slice(0, 1e3) }],
|
|
1724
|
-
tools: [EXTRACT_TOOL],
|
|
1725
|
-
tool_choice: { type: "tool", name: "extract_entities_and_relationships" }
|
|
1726
|
-
});
|
|
1727
|
-
const toolBlock = response.content.find((b) => b.type === "tool_use");
|
|
1728
|
-
if (toolBlock && toolBlock.type === "tool_use") {
|
|
1729
|
-
const input = toolBlock.input;
|
|
1730
|
-
const rawEntities = input.entities ?? [];
|
|
1731
|
-
const rawRels = input.relationships ?? [];
|
|
1732
|
-
const rawHyperedges = input.hyperedges ?? [];
|
|
1733
|
-
return {
|
|
1734
|
-
entities: rawEntities.map((e) => ({
|
|
1735
|
-
name: e.name ?? "",
|
|
1736
|
-
type: e.type ?? "concept"
|
|
1737
|
-
})).filter((e) => e.name.length > 0),
|
|
1738
|
-
relationships: rawRels.map((r) => ({
|
|
1739
|
-
source: r.source ?? "",
|
|
1740
|
-
sourceType: r.source_type ?? "concept",
|
|
1741
|
-
target: r.target ?? "",
|
|
1742
|
-
targetType: r.target_type ?? "concept",
|
|
1743
|
-
relationship: r.relationship ?? "uses",
|
|
1744
|
-
confidence: Number(r.confidence ?? 1),
|
|
1745
|
-
confidenceLabel: String(r.confidence_label ?? "extracted")
|
|
1746
|
-
})).filter((r) => r.source.length > 0 && r.target.length > 0),
|
|
1747
|
-
hyperedges: rawHyperedges.map((h) => ({
|
|
1748
|
-
label: String(h.label ?? ""),
|
|
1749
|
-
relation: String(h.relation ?? ""),
|
|
1750
|
-
entityNames: h.entity_names ?? [],
|
|
1751
|
-
confidence: Number(h.confidence ?? 1)
|
|
1752
|
-
})).filter((h) => h.label.length > 0 && h.entityNames.length >= 3)
|
|
1753
|
-
};
|
|
1754
|
-
}
|
|
1755
|
-
} catch (err) {
|
|
1756
|
-
process.stderr.write(
|
|
1757
|
-
`[graph-rag] Extraction failed: ${err instanceof Error ? err.message : String(err)}
|
|
1758
|
-
`
|
|
1759
|
-
);
|
|
1760
|
-
}
|
|
1761
|
-
return { entities: [], relationships: [], hyperedges: [] };
|
|
1762
|
-
}
|
|
1763
|
-
async function storeExtraction(client, extraction, memoryId, timestamp) {
|
|
1764
|
-
let entitiesStored = 0;
|
|
1765
|
-
let relationshipsStored = 0;
|
|
1766
|
-
for (const e of extraction.entities) {
|
|
1767
|
-
const aliasTarget = await resolveAlias(client, e.name);
|
|
1768
|
-
const id = aliasTarget ?? entityId(e.name, e.type);
|
|
1769
|
-
const normalizedName = normalizeEntityName(e.name);
|
|
1770
|
-
try {
|
|
1771
|
-
if (aliasTarget) {
|
|
1772
|
-
await client.execute({
|
|
1773
|
-
sql: `UPDATE entities SET last_seen = ? WHERE id = ?`,
|
|
1774
|
-
args: [timestamp, aliasTarget]
|
|
1775
|
-
});
|
|
1776
|
-
} else {
|
|
1777
|
-
await client.execute({
|
|
1778
|
-
sql: `INSERT INTO entities (id, name, type, first_seen, last_seen)
|
|
1779
|
-
VALUES (?, ?, ?, ?, ?)
|
|
1780
|
-
ON CONFLICT(name, type) DO UPDATE SET last_seen = ?`,
|
|
1781
|
-
args: [id, normalizedName, e.type, timestamp, timestamp, timestamp]
|
|
1782
|
-
});
|
|
1783
|
-
}
|
|
1784
|
-
await client.execute({
|
|
1785
|
-
sql: `INSERT OR IGNORE INTO entity_memories (entity_id, memory_id)
|
|
1786
|
-
VALUES (?, ?)`,
|
|
1787
|
-
args: [id, memoryId]
|
|
1788
|
-
});
|
|
1789
|
-
entitiesStored++;
|
|
1790
|
-
} catch {
|
|
1791
|
-
}
|
|
1792
|
-
}
|
|
1793
|
-
for (const r of extraction.relationships) {
|
|
1794
|
-
const sourceAlias = await resolveAlias(client, r.source);
|
|
1795
|
-
const targetAlias = await resolveAlias(client, r.target);
|
|
1796
|
-
const sourceId = sourceAlias ?? entityId(r.source, r.sourceType);
|
|
1797
|
-
const targetId = targetAlias ?? entityId(r.target, r.targetType);
|
|
1798
|
-
const relId = crypto2.randomUUID().slice(0, 16);
|
|
1799
|
-
try {
|
|
1800
|
-
await client.execute({
|
|
1801
|
-
sql: `INSERT OR IGNORE INTO entities (id, name, type, first_seen, last_seen)
|
|
1802
|
-
VALUES (?, ?, ?, ?, ?)`,
|
|
1803
|
-
args: [sourceId, r.source, r.sourceType, timestamp, timestamp]
|
|
1804
|
-
});
|
|
1805
|
-
await client.execute({
|
|
1806
|
-
sql: `INSERT OR IGNORE INTO entities (id, name, type, first_seen, last_seen)
|
|
1807
|
-
VALUES (?, ?, ?, ?, ?)`,
|
|
1808
|
-
args: [targetId, r.target, r.targetType, timestamp, timestamp]
|
|
1809
|
-
});
|
|
1810
|
-
await client.execute({
|
|
1811
|
-
sql: `INSERT INTO relationships (id, source_entity_id, target_entity_id, type, weight, timestamp, confidence, confidence_label)
|
|
1812
|
-
VALUES (?, ?, ?, ?, 1.0, ?, ?, ?)
|
|
1813
|
-
ON CONFLICT(source_entity_id, target_entity_id, type)
|
|
1814
|
-
DO UPDATE SET weight = MIN(weight + 0.1, 2.0), timestamp = ?,
|
|
1815
|
-
confidence = MAX(confidence, ?), confidence_label = ?`,
|
|
1816
|
-
args: [
|
|
1817
|
-
relId,
|
|
1818
|
-
sourceId,
|
|
1819
|
-
targetId,
|
|
1820
|
-
r.relationship,
|
|
1821
|
-
timestamp,
|
|
1822
|
-
r.confidence,
|
|
1823
|
-
r.confidenceLabel,
|
|
1824
|
-
timestamp,
|
|
1825
|
-
r.confidence,
|
|
1826
|
-
r.confidenceLabel
|
|
1827
|
-
]
|
|
1828
|
-
});
|
|
1829
|
-
const existingRel = await client.execute({
|
|
1830
|
-
sql: `SELECT id FROM relationships WHERE source_entity_id = ? AND target_entity_id = ? AND type = ?`,
|
|
1831
|
-
args: [sourceId, targetId, r.relationship]
|
|
1832
|
-
});
|
|
1833
|
-
const actualRelId = existingRel.rows[0]?.id ? String(existingRel.rows[0].id) : relId;
|
|
1834
|
-
await client.execute({
|
|
1835
|
-
sql: `INSERT OR IGNORE INTO relationship_memories (relationship_id, memory_id)
|
|
1836
|
-
VALUES (?, ?)`,
|
|
1837
|
-
args: [actualRelId, memoryId]
|
|
1838
|
-
});
|
|
1839
|
-
relationshipsStored++;
|
|
1840
|
-
} catch {
|
|
1841
|
-
}
|
|
1842
|
-
}
|
|
1843
|
-
for (const h of extraction.hyperedges) {
|
|
1844
|
-
const hId = crypto2.randomUUID().slice(0, 16);
|
|
1845
|
-
try {
|
|
1846
|
-
await client.execute({
|
|
1847
|
-
sql: `INSERT OR IGNORE INTO hyperedges (id, label, relation, confidence, timestamp)
|
|
1848
|
-
VALUES (?, ?, ?, ?, ?)`,
|
|
1849
|
-
args: [hId, h.label, h.relation, h.confidence, timestamp]
|
|
1850
|
-
});
|
|
1851
|
-
for (const entityName of h.entityNames) {
|
|
1852
|
-
const eType = extraction.entities.find((e) => e.name === entityName)?.type ?? "concept";
|
|
1853
|
-
const eId = entityId(entityName, eType);
|
|
1854
|
-
await client.execute({
|
|
1855
|
-
sql: `INSERT OR IGNORE INTO entities (id, name, type, first_seen, last_seen)
|
|
1856
|
-
VALUES (?, ?, ?, ?, ?)`,
|
|
1857
|
-
args: [eId, entityName, eType, timestamp, timestamp]
|
|
1858
|
-
});
|
|
1859
|
-
await client.execute({
|
|
1860
|
-
sql: `INSERT OR IGNORE INTO hyperedge_nodes (hyperedge_id, entity_id) VALUES (?, ?)`,
|
|
1861
|
-
args: [hId, eId]
|
|
1862
|
-
});
|
|
1863
|
-
}
|
|
1864
|
-
} catch {
|
|
1865
|
-
}
|
|
1866
|
-
}
|
|
1867
|
-
return { entitiesStored, relationshipsStored };
|
|
1868
|
-
}
|
|
1869
|
-
async function extractBatch(client, batchSize = 50, model = "claude-haiku-4-5-20251001") {
|
|
1870
|
-
const result = await client.execute({
|
|
1871
|
-
sql: `SELECT id, agent_id, raw_text, tool_name, timestamp, content_hash, graph_extracted_hash
|
|
1872
|
-
FROM memories
|
|
1873
|
-
WHERE (COALESCE(graph_extracted, 0) = 0
|
|
1874
|
-
OR (content_hash IS NOT NULL AND content_hash != COALESCE(graph_extracted_hash, '')))
|
|
1875
|
-
AND COALESCE(status, 'active') = 'active'
|
|
1876
|
-
AND LENGTH(raw_text) >= 50
|
|
1877
|
-
ORDER BY timestamp DESC
|
|
1878
|
-
LIMIT ?`,
|
|
1879
|
-
args: [batchSize]
|
|
1880
|
-
});
|
|
1881
|
-
if (result.rows.length === 0) {
|
|
1882
|
-
return { processed: 0, entities: 0, relationships: 0 };
|
|
1883
|
-
}
|
|
1884
|
-
let totalEntities = 0;
|
|
1885
|
-
let totalRelationships = 0;
|
|
1886
|
-
for (const row of result.rows) {
|
|
1887
|
-
const memoryId = String(row.id);
|
|
1888
|
-
const agentId = String(row.agent_id);
|
|
1889
|
-
const rawContent = String(row.raw_text);
|
|
1890
|
-
const toolName = String(row.tool_name ?? "");
|
|
1891
|
-
const timestamp = String(row.timestamp);
|
|
1892
|
-
let extractionContent = rawContent;
|
|
1893
|
-
let detectedFilePath;
|
|
1894
|
-
if (toolName === "Read") {
|
|
1895
|
-
const pathMatch = rawContent.match(READ_MEMORY_PATH_PATTERN);
|
|
1896
|
-
if (pathMatch) {
|
|
1897
|
-
detectedFilePath = pathMatch[1];
|
|
1898
|
-
extractionContent = rawContent.slice(pathMatch[0].length);
|
|
1899
|
-
}
|
|
1900
|
-
}
|
|
1901
|
-
try {
|
|
1902
|
-
const extraction = await extractFromMemory(extractionContent, agentId, model, detectedFilePath);
|
|
1903
|
-
if (extraction.entities.length > 0 || extraction.relationships.length > 0) {
|
|
1904
|
-
const stored = await storeExtraction(client, extraction, memoryId, timestamp);
|
|
1905
|
-
totalEntities += stored.entitiesStored;
|
|
1906
|
-
totalRelationships += stored.relationshipsStored;
|
|
1907
|
-
}
|
|
1908
|
-
const contentHash = crypto2.createHash("sha256").update(rawContent).digest("hex").slice(0, 32);
|
|
1909
|
-
await client.execute({
|
|
1910
|
-
sql: "UPDATE memories SET graph_extracted = 1, content_hash = ?, graph_extracted_hash = ? WHERE id = ?",
|
|
1911
|
-
args: [contentHash, contentHash, memoryId]
|
|
1912
|
-
});
|
|
1913
|
-
} catch (err) {
|
|
1914
|
-
process.stderr.write(
|
|
1915
|
-
`[graph-rag] Batch extraction failed for ${memoryId}: ${err instanceof Error ? err.message : String(err)}
|
|
1916
|
-
`
|
|
1917
|
-
);
|
|
1918
|
-
await client.execute({
|
|
1919
|
-
sql: "UPDATE memories SET graph_extracted = 1 WHERE id = ?",
|
|
1920
|
-
args: [memoryId]
|
|
1921
|
-
});
|
|
1922
|
-
}
|
|
1923
|
-
}
|
|
1924
|
-
return { processed: result.rows.length, entities: totalEntities, relationships: totalRelationships };
|
|
1925
|
-
}
|
|
1926
|
-
|
|
1927
|
-
// src/bin/graph-backfill.ts
|
|
1928
|
-
async function main() {
|
|
1929
|
-
await initStore();
|
|
1930
|
-
const client = getClient();
|
|
1931
|
-
const countResult = await client.execute(
|
|
1932
|
-
"SELECT COUNT(*) as cnt FROM memories WHERE COALESCE(graph_extracted, 0) = 0 AND LENGTH(raw_text) >= 50"
|
|
1933
|
-
);
|
|
1934
|
-
const total = Number(countResult.rows[0]?.cnt ?? 0);
|
|
1935
|
-
if (total === 0) {
|
|
1936
|
-
process.stderr.write("[graph-backfill] All memories already extracted.\n");
|
|
1937
|
-
await disposeStore();
|
|
1938
|
-
return;
|
|
1939
|
-
}
|
|
1940
|
-
process.stderr.write(`[graph-backfill] ${total} memories to process.
|
|
1941
|
-
`);
|
|
1942
|
-
let totalProcessed = 0;
|
|
1943
|
-
let totalEntities = 0;
|
|
1944
|
-
let totalRelationships = 0;
|
|
1945
|
-
while (totalProcessed < total) {
|
|
1946
|
-
const result = await extractBatch(client, 50);
|
|
1947
|
-
if (result.processed === 0) break;
|
|
1948
|
-
totalProcessed += result.processed;
|
|
1949
|
-
totalEntities += result.entities;
|
|
1950
|
-
totalRelationships += result.relationships;
|
|
1951
|
-
process.stderr.write(
|
|
1952
|
-
`[graph-backfill] Progress: ${totalProcessed}/${total} | +${result.entities} entities, +${result.relationships} relationships
|
|
1953
|
-
`
|
|
1954
|
-
);
|
|
1955
|
-
}
|
|
1956
|
-
process.stderr.write(
|
|
1957
|
-
`[graph-backfill] Complete: ${totalProcessed} memories, ${totalEntities} entities, ${totalRelationships} relationships.
|
|
1958
|
-
`
|
|
1959
|
-
);
|
|
1960
|
-
await disposeStore();
|
|
1961
|
-
}
|
|
1962
|
-
main().catch((err) => {
|
|
1963
|
-
process.stderr.write(
|
|
1964
|
-
`[graph-backfill] FATAL: ${err instanceof Error ? err.message : String(err)}
|
|
1965
|
-
`
|
|
1966
|
-
);
|
|
1967
|
-
process.exit(1);
|
|
1968
|
-
});
|