@askexenow/exe-os 0.8.1 → 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/package.json +1 -1
- package/dist/bin/backfill-responses.js +0 -2064
- package/dist/bin/backfill-vectors.js +0 -1771
- package/dist/bin/cleanup-stale-review-tasks.js +0 -1468
- package/dist/bin/cli.js +0 -21371
- package/dist/bin/exe-agent.js +0 -2016
- package/dist/bin/exe-assign.js +0 -2176
- package/dist/bin/exe-boot.js +0 -6332
- package/dist/bin/exe-call.js +0 -341
- package/dist/bin/exe-cloud.js +0 -861
- package/dist/bin/exe-dispatch.js +0 -1159
- package/dist/bin/exe-doctor.js +0 -1786
- package/dist/bin/exe-export-behaviors.js +0 -1637
- package/dist/bin/exe-forget.js +0 -1784
- package/dist/bin/exe-gateway.js +0 -8140
- package/dist/bin/exe-healthcheck.js +0 -207
- package/dist/bin/exe-heartbeat.js +0 -1778
- package/dist/bin/exe-kill.js +0 -1622
- package/dist/bin/exe-launch-agent.js +0 -1847
- package/dist/bin/exe-link.js +0 -192
- package/dist/bin/exe-new-employee.js +0 -1384
- package/dist/bin/exe-pending-messages.js +0 -1576
- package/dist/bin/exe-pending-notifications.js +0 -1450
- package/dist/bin/exe-pending-reviews.js +0 -1598
- package/dist/bin/exe-repo-drift.js +0 -95
- package/dist/bin/exe-review.js +0 -1742
- package/dist/bin/exe-search.js +0 -3116
- package/dist/bin/exe-session-cleanup.js +0 -3360
- package/dist/bin/exe-settings.js +0 -365
- package/dist/bin/exe-status.js +0 -1661
- package/dist/bin/exe-team.js +0 -1453
- package/dist/bin/git-sweep.js +0 -2441
- package/dist/bin/graph-backfill.js +0 -2111
- package/dist/bin/graph-export.js +0 -1747
- package/dist/bin/install.js +0 -661
- package/dist/bin/list-providers.js +0 -140
- package/dist/bin/scan-tasks.js +0 -2039
- package/dist/bin/setup.js +0 -2717
- package/dist/bin/shard-migrate.js +0 -1637
- package/dist/bin/update.js +0 -98
- package/dist/bin/wiki-sync.js +0 -1657
- package/dist/gateway/index.js +0 -9256
- package/dist/hooks/bug-report-worker.js +0 -2903
- package/dist/hooks/commit-complete.js +0 -2364
- package/dist/hooks/error-recall.js +0 -3327
- package/dist/hooks/exe-heartbeat-hook.js +0 -237
- package/dist/hooks/ingest-worker.js +0 -5065
- package/dist/hooks/ingest.js +0 -695
- package/dist/hooks/instructions-loaded.js +0 -2069
- package/dist/hooks/notification.js +0 -1915
- package/dist/hooks/post-compact.js +0 -1940
- package/dist/hooks/pre-compact.js +0 -1935
- package/dist/hooks/pre-tool-use.js +0 -2380
- package/dist/hooks/prompt-ingest-worker.js +0 -2291
- package/dist/hooks/prompt-submit.js +0 -5189
- package/dist/hooks/response-ingest-worker.js +0 -2431
- package/dist/hooks/session-end.js +0 -2196
- package/dist/hooks/session-start.js +0 -3259
- package/dist/hooks/stop.js +0 -2025
- package/dist/hooks/subagent-stop.js +0 -1915
- package/dist/hooks/summary-worker.js +0 -2931
- package/dist/index.js +0 -12327
- package/dist/lib/cloud-sync.js +0 -500
- package/dist/lib/config.js +0 -235
- package/dist/lib/consolidation.js +0 -481
- package/dist/lib/crypto.js +0 -51
- package/dist/lib/database.js +0 -834
- package/dist/lib/device-registry.js +0 -1009
- package/dist/lib/embedder.js +0 -645
- package/dist/lib/employee-templates.js +0 -570
- package/dist/lib/employees.js +0 -182
- package/dist/lib/error-detector.js +0 -156
- package/dist/lib/exe-daemon-client.js +0 -456
- package/dist/lib/exe-daemon.js +0 -8699
- package/dist/lib/file-grep.js +0 -215
- package/dist/lib/hybrid-search.js +0 -3064
- package/dist/lib/identity-templates.js +0 -441
- package/dist/lib/identity.js +0 -228
- package/dist/lib/keychain.js +0 -145
- package/dist/lib/license.js +0 -382
- package/dist/lib/messaging.js +0 -1389
- package/dist/lib/reminders.js +0 -63
- package/dist/lib/schedules.js +0 -1525
- package/dist/lib/session-registry.js +0 -52
- package/dist/lib/skill-learning.js +0 -488
- package/dist/lib/status-brief.js +0 -240
- package/dist/lib/store.js +0 -1740
- package/dist/lib/task-router.js +0 -128
- package/dist/lib/tasks.js +0 -2585
- package/dist/lib/tmux-routing.js +0 -2969
- 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 -10812
- 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 -1903
- package/dist/mcp/tools/deactivate-behavior.js +0 -268
- package/dist/mcp/tools/list-reminders.js +0 -62
- package/dist/mcp/tools/list-tasks.js +0 -491
- package/dist/mcp/tools/send-message.js +0 -1395
- package/dist/mcp/tools/update-task.js +0 -1807
- package/dist/runtime/index.js +0 -7201
- package/dist/tui/App.js +0 -17874
package/dist/bin/exe-assign.js
DELETED
|
@@ -1,2176 +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, writeFile, mkdir } from "fs/promises";
|
|
20
|
-
import { readFileSync, existsSync, renameSync } from "fs";
|
|
21
|
-
import path 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 = path.join(os.homedir(), ".exe-os");
|
|
27
|
-
const legacyDir = path.join(os.homedir(), ".exe-mem");
|
|
28
|
-
if (!existsSync(newDir) && existsSync(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
|
-
function normalizeAutoUpdate(raw) {
|
|
85
|
-
const defaultAU = DEFAULT_CONFIG.autoUpdate;
|
|
86
|
-
const userAU = raw.autoUpdate ?? {};
|
|
87
|
-
raw.autoUpdate = { ...defaultAU, ...userAU };
|
|
88
|
-
}
|
|
89
|
-
async function loadConfig() {
|
|
90
|
-
const dir = process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? EXE_AI_DIR;
|
|
91
|
-
await mkdir(dir, { recursive: true });
|
|
92
|
-
const configPath = path.join(dir, "config.json");
|
|
93
|
-
if (!existsSync(configPath)) {
|
|
94
|
-
return { ...DEFAULT_CONFIG, dbPath: path.join(dir, "memories.db") };
|
|
95
|
-
}
|
|
96
|
-
const raw = await readFile(configPath, "utf-8");
|
|
97
|
-
try {
|
|
98
|
-
let parsed = JSON.parse(raw);
|
|
99
|
-
parsed = migrateLegacyConfig(parsed);
|
|
100
|
-
const { config: migratedCfg, migrated, fromVersion } = migrateConfig(parsed);
|
|
101
|
-
if (migrated) {
|
|
102
|
-
process.stderr.write(`[exe-os] Config migrated from v${fromVersion} to v${migratedCfg.config_version}
|
|
103
|
-
`);
|
|
104
|
-
try {
|
|
105
|
-
await writeFile(configPath, JSON.stringify(migratedCfg, null, 2) + "\n");
|
|
106
|
-
} catch {
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
normalizeScalingRoadmap(migratedCfg);
|
|
110
|
-
normalizeSessionLifecycle(migratedCfg);
|
|
111
|
-
normalizeAutoUpdate(migratedCfg);
|
|
112
|
-
const config = { ...DEFAULT_CONFIG, dbPath: path.join(dir, "memories.db"), ...migratedCfg };
|
|
113
|
-
if (config.dbPath.startsWith("~")) {
|
|
114
|
-
config.dbPath = config.dbPath.replace(/^~/, os.homedir());
|
|
115
|
-
}
|
|
116
|
-
return config;
|
|
117
|
-
} catch {
|
|
118
|
-
return { ...DEFAULT_CONFIG, dbPath: path.join(dir, "memories.db") };
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
var EXE_AI_DIR, DB_PATH, MODELS_DIR, CONFIG_PATH, LEGACY_LANCE_PATH, CURRENT_CONFIG_VERSION, DEFAULT_CONFIG, CONFIG_MIGRATIONS;
|
|
122
|
-
var init_config = __esm({
|
|
123
|
-
"src/lib/config.ts"() {
|
|
124
|
-
"use strict";
|
|
125
|
-
EXE_AI_DIR = resolveDataDir();
|
|
126
|
-
DB_PATH = path.join(EXE_AI_DIR, "memories.db");
|
|
127
|
-
MODELS_DIR = path.join(EXE_AI_DIR, "models");
|
|
128
|
-
CONFIG_PATH = path.join(EXE_AI_DIR, "config.json");
|
|
129
|
-
LEGACY_LANCE_PATH = path.join(EXE_AI_DIR, "local.lance");
|
|
130
|
-
CURRENT_CONFIG_VERSION = 1;
|
|
131
|
-
DEFAULT_CONFIG = {
|
|
132
|
-
config_version: CURRENT_CONFIG_VERSION,
|
|
133
|
-
dbPath: DB_PATH,
|
|
134
|
-
modelFile: "jina-embeddings-v5-small-q4_k_m.gguf",
|
|
135
|
-
embeddingDim: 1024,
|
|
136
|
-
batchSize: 20,
|
|
137
|
-
flushIntervalMs: 1e4,
|
|
138
|
-
autoIngestion: true,
|
|
139
|
-
autoRetrieval: true,
|
|
140
|
-
searchMode: "hybrid",
|
|
141
|
-
hookSearchMode: "hybrid",
|
|
142
|
-
fileGrepEnabled: true,
|
|
143
|
-
splashEffect: true,
|
|
144
|
-
consolidationEnabled: true,
|
|
145
|
-
consolidationIntervalMs: 6 * 60 * 60 * 1e3,
|
|
146
|
-
consolidationModel: "claude-haiku-4-5-20251001",
|
|
147
|
-
consolidationMaxCallsPerRun: 20,
|
|
148
|
-
selfQueryRouter: true,
|
|
149
|
-
selfQueryModel: "claude-haiku-4-5-20251001",
|
|
150
|
-
rerankerEnabled: true,
|
|
151
|
-
scalingRoadmap: {
|
|
152
|
-
rerankerAutoTrigger: {
|
|
153
|
-
enabled: true,
|
|
154
|
-
broadQueryMinCardinality: 5e4,
|
|
155
|
-
fetchTopK: 150,
|
|
156
|
-
returnTopK: 5
|
|
157
|
-
}
|
|
158
|
-
},
|
|
159
|
-
graphRagEnabled: true,
|
|
160
|
-
wikiEnabled: false,
|
|
161
|
-
wikiUrl: "",
|
|
162
|
-
wikiApiKey: "",
|
|
163
|
-
wikiSyncIntervalMs: 30 * 60 * 1e3,
|
|
164
|
-
wikiWorkspaceMapping: {
|
|
165
|
-
exe: "Executive",
|
|
166
|
-
yoshi: "Engineering",
|
|
167
|
-
mari: "Marketing",
|
|
168
|
-
tom: "Engineering",
|
|
169
|
-
sasha: "Production"
|
|
170
|
-
},
|
|
171
|
-
wikiAutoUpdate: true,
|
|
172
|
-
wikiAutoUpdateThreshold: 0.5,
|
|
173
|
-
wikiAutoUpdateCreateNew: true,
|
|
174
|
-
skillLearning: true,
|
|
175
|
-
skillThreshold: 3,
|
|
176
|
-
skillModel: "claude-haiku-4-5-20251001",
|
|
177
|
-
exeHeartbeat: {
|
|
178
|
-
enabled: true,
|
|
179
|
-
intervalSeconds: 60,
|
|
180
|
-
staleInProgressThresholdHours: 2
|
|
181
|
-
},
|
|
182
|
-
sessionLifecycle: {
|
|
183
|
-
idleKillEnabled: true,
|
|
184
|
-
idleKillTicksRequired: 3,
|
|
185
|
-
idleKillIntercomAckWindowMs: 1e4,
|
|
186
|
-
maxAutoInstances: 10
|
|
187
|
-
},
|
|
188
|
-
autoUpdate: {
|
|
189
|
-
checkOnBoot: true,
|
|
190
|
-
autoInstall: false,
|
|
191
|
-
checkIntervalMs: 24 * 60 * 60 * 1e3
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
CONFIG_MIGRATIONS = [
|
|
195
|
-
{
|
|
196
|
-
from: 0,
|
|
197
|
-
to: 1,
|
|
198
|
-
migrate: (cfg) => {
|
|
199
|
-
cfg.config_version = 1;
|
|
200
|
-
return cfg;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
];
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
// src/lib/shard-manager.ts
|
|
208
|
-
var shard_manager_exports = {};
|
|
209
|
-
__export(shard_manager_exports, {
|
|
210
|
-
disposeShards: () => disposeShards,
|
|
211
|
-
ensureShardSchema: () => ensureShardSchema,
|
|
212
|
-
getReadyShardClient: () => getReadyShardClient,
|
|
213
|
-
getShardClient: () => getShardClient,
|
|
214
|
-
getShardsDir: () => getShardsDir,
|
|
215
|
-
initShardManager: () => initShardManager,
|
|
216
|
-
isShardingEnabled: () => isShardingEnabled,
|
|
217
|
-
listShards: () => listShards,
|
|
218
|
-
shardExists: () => shardExists
|
|
219
|
-
});
|
|
220
|
-
import path5 from "path";
|
|
221
|
-
import { existsSync as existsSync5, mkdirSync } from "fs";
|
|
222
|
-
import { createClient as createClient2 } from "@libsql/client";
|
|
223
|
-
function initShardManager(encryptionKey) {
|
|
224
|
-
_encryptionKey = encryptionKey;
|
|
225
|
-
if (!existsSync5(SHARDS_DIR)) {
|
|
226
|
-
mkdirSync(SHARDS_DIR, { recursive: true });
|
|
227
|
-
}
|
|
228
|
-
_shardingEnabled = true;
|
|
229
|
-
}
|
|
230
|
-
function isShardingEnabled() {
|
|
231
|
-
return _shardingEnabled;
|
|
232
|
-
}
|
|
233
|
-
function getShardsDir() {
|
|
234
|
-
return SHARDS_DIR;
|
|
235
|
-
}
|
|
236
|
-
function getShardClient(projectName) {
|
|
237
|
-
if (!_encryptionKey) {
|
|
238
|
-
throw new Error("Shard manager not initialized. Call initShardManager() first.");
|
|
239
|
-
}
|
|
240
|
-
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
241
|
-
if (!safeName) {
|
|
242
|
-
throw new Error(`Invalid project name for shard: "${projectName}"`);
|
|
243
|
-
}
|
|
244
|
-
const cached = _shards.get(safeName);
|
|
245
|
-
if (cached) return cached;
|
|
246
|
-
const dbPath = path5.join(SHARDS_DIR, `${safeName}.db`);
|
|
247
|
-
const client = createClient2({
|
|
248
|
-
url: `file:${dbPath}`,
|
|
249
|
-
encryptionKey: _encryptionKey
|
|
250
|
-
});
|
|
251
|
-
_shards.set(safeName, client);
|
|
252
|
-
return client;
|
|
253
|
-
}
|
|
254
|
-
function shardExists(projectName) {
|
|
255
|
-
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
256
|
-
return existsSync5(path5.join(SHARDS_DIR, `${safeName}.db`));
|
|
257
|
-
}
|
|
258
|
-
function listShards() {
|
|
259
|
-
if (!existsSync5(SHARDS_DIR)) return [];
|
|
260
|
-
const { readdirSync } = __require("fs");
|
|
261
|
-
return readdirSync(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
|
|
262
|
-
}
|
|
263
|
-
async function ensureShardSchema(client) {
|
|
264
|
-
await client.execute("PRAGMA journal_mode = WAL");
|
|
265
|
-
await client.execute("PRAGMA busy_timeout = 5000");
|
|
266
|
-
try {
|
|
267
|
-
await client.execute("PRAGMA libsql_vector_search_ef = 128");
|
|
268
|
-
} catch {
|
|
269
|
-
}
|
|
270
|
-
await client.executeMultiple(`
|
|
271
|
-
CREATE TABLE IF NOT EXISTS memories (
|
|
272
|
-
id TEXT PRIMARY KEY,
|
|
273
|
-
agent_id TEXT NOT NULL,
|
|
274
|
-
agent_role TEXT NOT NULL,
|
|
275
|
-
session_id TEXT NOT NULL,
|
|
276
|
-
timestamp TEXT NOT NULL,
|
|
277
|
-
tool_name TEXT NOT NULL,
|
|
278
|
-
project_name TEXT NOT NULL,
|
|
279
|
-
has_error INTEGER NOT NULL DEFAULT 0,
|
|
280
|
-
raw_text TEXT NOT NULL,
|
|
281
|
-
vector F32_BLOB(1024),
|
|
282
|
-
version INTEGER NOT NULL DEFAULT 0
|
|
283
|
-
);
|
|
284
|
-
|
|
285
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent ON memories(agent_id);
|
|
286
|
-
CREATE INDEX IF NOT EXISTS idx_memories_timestamp ON memories(timestamp);
|
|
287
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent_project ON memories(agent_id, project_name);
|
|
288
|
-
`);
|
|
289
|
-
await client.executeMultiple(`
|
|
290
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
291
|
-
raw_text,
|
|
292
|
-
content='memories',
|
|
293
|
-
content_rowid='rowid'
|
|
294
|
-
);
|
|
295
|
-
|
|
296
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
|
|
297
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
298
|
-
END;
|
|
299
|
-
|
|
300
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
|
|
301
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
302
|
-
END;
|
|
303
|
-
|
|
304
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
|
|
305
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
306
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
307
|
-
END;
|
|
308
|
-
`);
|
|
309
|
-
for (const col of [
|
|
310
|
-
"ALTER TABLE memories ADD COLUMN task_id TEXT",
|
|
311
|
-
"ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0",
|
|
312
|
-
"ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5",
|
|
313
|
-
"ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'",
|
|
314
|
-
"ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0",
|
|
315
|
-
"ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0",
|
|
316
|
-
"ALTER TABLE memories ADD COLUMN content_hash TEXT",
|
|
317
|
-
"ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT",
|
|
318
|
-
"ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7",
|
|
319
|
-
"ALTER TABLE memories ADD COLUMN last_accessed TEXT",
|
|
320
|
-
// Wiki linkage columns (must match database.ts)
|
|
321
|
-
"ALTER TABLE memories ADD COLUMN workspace_id TEXT",
|
|
322
|
-
"ALTER TABLE memories ADD COLUMN document_id TEXT",
|
|
323
|
-
"ALTER TABLE memories ADD COLUMN user_id TEXT",
|
|
324
|
-
"ALTER TABLE memories ADD COLUMN char_offset INTEGER",
|
|
325
|
-
"ALTER TABLE memories ADD COLUMN page_number INTEGER",
|
|
326
|
-
// Source provenance columns (must match database.ts)
|
|
327
|
-
"ALTER TABLE memories ADD COLUMN source_path TEXT",
|
|
328
|
-
"ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'",
|
|
329
|
-
"ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3",
|
|
330
|
-
"ALTER TABLE memories ADD COLUMN supersedes_id TEXT"
|
|
331
|
-
]) {
|
|
332
|
-
try {
|
|
333
|
-
await client.execute(col);
|
|
334
|
-
} catch {
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
for (const idx of [
|
|
338
|
-
"CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)",
|
|
339
|
-
"CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL"
|
|
340
|
-
]) {
|
|
341
|
-
try {
|
|
342
|
-
await client.execute(idx);
|
|
343
|
-
} catch {
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
try {
|
|
347
|
-
await client.execute("CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)");
|
|
348
|
-
} catch {
|
|
349
|
-
}
|
|
350
|
-
for (const idx of [
|
|
351
|
-
"CREATE INDEX IF NOT EXISTS idx_memories_workspace ON memories(workspace_id)",
|
|
352
|
-
"CREATE INDEX IF NOT EXISTS idx_memories_document ON memories(document_id)",
|
|
353
|
-
"CREATE INDEX IF NOT EXISTS idx_memories_user ON memories(user_id)"
|
|
354
|
-
]) {
|
|
355
|
-
try {
|
|
356
|
-
await client.execute(idx);
|
|
357
|
-
} catch {
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
await client.executeMultiple(`
|
|
361
|
-
CREATE TABLE IF NOT EXISTS entities (
|
|
362
|
-
id TEXT PRIMARY KEY,
|
|
363
|
-
name TEXT NOT NULL,
|
|
364
|
-
type TEXT NOT NULL,
|
|
365
|
-
first_seen TEXT NOT NULL,
|
|
366
|
-
last_seen TEXT NOT NULL,
|
|
367
|
-
properties TEXT DEFAULT '{}',
|
|
368
|
-
UNIQUE(name, type)
|
|
369
|
-
);
|
|
370
|
-
|
|
371
|
-
CREATE TABLE IF NOT EXISTS relationships (
|
|
372
|
-
id TEXT PRIMARY KEY,
|
|
373
|
-
source_entity_id TEXT NOT NULL,
|
|
374
|
-
target_entity_id TEXT NOT NULL,
|
|
375
|
-
type TEXT NOT NULL,
|
|
376
|
-
weight REAL DEFAULT 1.0,
|
|
377
|
-
timestamp TEXT NOT NULL,
|
|
378
|
-
properties TEXT DEFAULT '{}',
|
|
379
|
-
UNIQUE(source_entity_id, target_entity_id, type)
|
|
380
|
-
);
|
|
381
|
-
|
|
382
|
-
CREATE TABLE IF NOT EXISTS entity_memories (
|
|
383
|
-
entity_id TEXT NOT NULL,
|
|
384
|
-
memory_id TEXT NOT NULL,
|
|
385
|
-
PRIMARY KEY (entity_id, memory_id)
|
|
386
|
-
);
|
|
387
|
-
|
|
388
|
-
CREATE TABLE IF NOT EXISTS relationship_memories (
|
|
389
|
-
relationship_id TEXT NOT NULL,
|
|
390
|
-
memory_id TEXT NOT NULL,
|
|
391
|
-
PRIMARY KEY (relationship_id, memory_id)
|
|
392
|
-
);
|
|
393
|
-
|
|
394
|
-
CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
|
|
395
|
-
CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
|
|
396
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
|
|
397
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
|
|
398
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_type ON relationships(type);
|
|
399
|
-
|
|
400
|
-
CREATE TABLE IF NOT EXISTS hyperedges (
|
|
401
|
-
id TEXT PRIMARY KEY,
|
|
402
|
-
label TEXT NOT NULL,
|
|
403
|
-
relation TEXT NOT NULL,
|
|
404
|
-
confidence REAL DEFAULT 1.0,
|
|
405
|
-
timestamp TEXT NOT NULL
|
|
406
|
-
);
|
|
407
|
-
|
|
408
|
-
CREATE TABLE IF NOT EXISTS hyperedge_nodes (
|
|
409
|
-
hyperedge_id TEXT NOT NULL,
|
|
410
|
-
entity_id TEXT NOT NULL,
|
|
411
|
-
PRIMARY KEY (hyperedge_id, entity_id)
|
|
412
|
-
);
|
|
413
|
-
`);
|
|
414
|
-
for (const col of [
|
|
415
|
-
"ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
|
|
416
|
-
"ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
|
|
417
|
-
]) {
|
|
418
|
-
try {
|
|
419
|
-
await client.execute(col);
|
|
420
|
-
} catch {
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
async function getReadyShardClient(projectName) {
|
|
425
|
-
const client = getShardClient(projectName);
|
|
426
|
-
await ensureShardSchema(client);
|
|
427
|
-
return client;
|
|
428
|
-
}
|
|
429
|
-
function disposeShards() {
|
|
430
|
-
for (const [, client] of _shards) {
|
|
431
|
-
client.close();
|
|
432
|
-
}
|
|
433
|
-
_shards.clear();
|
|
434
|
-
_shardingEnabled = false;
|
|
435
|
-
_encryptionKey = null;
|
|
436
|
-
}
|
|
437
|
-
var SHARDS_DIR, _shards, _encryptionKey, _shardingEnabled;
|
|
438
|
-
var init_shard_manager = __esm({
|
|
439
|
-
"src/lib/shard-manager.ts"() {
|
|
440
|
-
"use strict";
|
|
441
|
-
init_config();
|
|
442
|
-
SHARDS_DIR = path5.join(EXE_AI_DIR, "shards");
|
|
443
|
-
_shards = /* @__PURE__ */ new Map();
|
|
444
|
-
_encryptionKey = null;
|
|
445
|
-
_shardingEnabled = false;
|
|
446
|
-
}
|
|
447
|
-
});
|
|
448
|
-
|
|
449
|
-
// src/lib/employees.ts
|
|
450
|
-
init_config();
|
|
451
|
-
import { readFile as readFile2, writeFile as writeFile2, mkdir as mkdir2 } from "fs/promises";
|
|
452
|
-
import { existsSync as existsSync2, symlinkSync, readlinkSync } from "fs";
|
|
453
|
-
import { execSync } from "child_process";
|
|
454
|
-
import path2 from "path";
|
|
455
|
-
var EMPLOYEES_PATH = path2.join(EXE_AI_DIR, "exe-employees.json");
|
|
456
|
-
async function loadEmployees(employeesPath = EMPLOYEES_PATH) {
|
|
457
|
-
if (!existsSync2(employeesPath)) {
|
|
458
|
-
return [];
|
|
459
|
-
}
|
|
460
|
-
const raw = await readFile2(employeesPath, "utf-8");
|
|
461
|
-
try {
|
|
462
|
-
return JSON.parse(raw);
|
|
463
|
-
} catch {
|
|
464
|
-
return [];
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
function getEmployee(employees, name) {
|
|
468
|
-
return employees.find((e) => e.name === name);
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
// src/lib/task-router.ts
|
|
472
|
-
import { randomUUID } from "crypto";
|
|
473
|
-
var DEFAULT_BLOOM_CONFIG = {
|
|
474
|
-
complexityToTier: {
|
|
475
|
-
routine: "junior",
|
|
476
|
-
standard: "standard",
|
|
477
|
-
complex: "senior",
|
|
478
|
-
critical: "specialist"
|
|
479
|
-
},
|
|
480
|
-
tierRules: {
|
|
481
|
-
junior: {
|
|
482
|
-
eligible: ["tom"],
|
|
483
|
-
reviewRequired: false,
|
|
484
|
-
manualOnly: false
|
|
485
|
-
},
|
|
486
|
-
standard: {
|
|
487
|
-
eligible: ["tom"],
|
|
488
|
-
reviewRequired: false,
|
|
489
|
-
manualOnly: false
|
|
490
|
-
},
|
|
491
|
-
senior: {
|
|
492
|
-
eligible: [],
|
|
493
|
-
// any specialist, but review required
|
|
494
|
-
reviewRequired: true,
|
|
495
|
-
manualOnly: false
|
|
496
|
-
},
|
|
497
|
-
specialist: {
|
|
498
|
-
eligible: [],
|
|
499
|
-
reviewRequired: true,
|
|
500
|
-
manualOnly: true
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
};
|
|
504
|
-
function resolveBloomRouting(complexity, config = DEFAULT_BLOOM_CONFIG) {
|
|
505
|
-
const tier = config.complexityToTier[complexity];
|
|
506
|
-
const rule = config.tierRules[tier];
|
|
507
|
-
return {
|
|
508
|
-
tier,
|
|
509
|
-
reviewRequired: rule.reviewRequired,
|
|
510
|
-
manualOnly: rule.manualOnly,
|
|
511
|
-
eligible: rule.eligible
|
|
512
|
-
};
|
|
513
|
-
}
|
|
514
|
-
async function scoreEmployee(taskVector, agentId, searchFn) {
|
|
515
|
-
const results = await searchFn(taskVector, agentId, { limit: 5 });
|
|
516
|
-
if (results.length === 0) {
|
|
517
|
-
return { agentId, score: 0 };
|
|
518
|
-
}
|
|
519
|
-
const distances = results.map((r) => r["_distance"]).filter((d) => typeof d === "number");
|
|
520
|
-
if (distances.length > 0) {
|
|
521
|
-
const avgDistance = distances.reduce((sum, d) => sum + d, 0) / distances.length;
|
|
522
|
-
return { agentId, score: 1 / (1 + avgDistance) };
|
|
523
|
-
}
|
|
524
|
-
return { agentId, score: results.length / 5 };
|
|
525
|
-
}
|
|
526
|
-
async function routeTask(taskDescription, employees, embedFn, searchFn, options) {
|
|
527
|
-
let specialists = employees.filter((e) => e.name !== "exe");
|
|
528
|
-
if (specialists.length === 0) {
|
|
529
|
-
throw new Error(
|
|
530
|
-
"No specialist employees available. Create one with /exe-new-employee."
|
|
531
|
-
);
|
|
532
|
-
}
|
|
533
|
-
let bloomRouting;
|
|
534
|
-
if (options?.complexity) {
|
|
535
|
-
bloomRouting = resolveBloomRouting(options.complexity, options.bloomConfig);
|
|
536
|
-
if (bloomRouting.manualOnly) {
|
|
537
|
-
throw new Error(
|
|
538
|
-
`Task complexity "${options.complexity}" requires manual assignment (tier: ${bloomRouting.tier}).`
|
|
539
|
-
);
|
|
540
|
-
}
|
|
541
|
-
if (bloomRouting.eligible.length > 0) {
|
|
542
|
-
const eligible = new Set(bloomRouting.eligible);
|
|
543
|
-
const filtered = specialists.filter((e) => eligible.has(e.name));
|
|
544
|
-
if (filtered.length > 0) {
|
|
545
|
-
specialists = filtered;
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
const taskVector = await embedFn(taskDescription);
|
|
550
|
-
const scored = await Promise.all(
|
|
551
|
-
specialists.map(async (emp) => {
|
|
552
|
-
const { score } = await scoreEmployee(taskVector, emp.name, searchFn);
|
|
553
|
-
return { employee: emp, score };
|
|
554
|
-
})
|
|
555
|
-
);
|
|
556
|
-
scored.sort((a, b) => b.score - a.score);
|
|
557
|
-
return { ...scored[0], bloomRouting };
|
|
558
|
-
}
|
|
559
|
-
function createAssignmentMemory(taskDescription, assignedTo, assignedBy) {
|
|
560
|
-
return {
|
|
561
|
-
id: randomUUID(),
|
|
562
|
-
agent_id: assignedTo.name,
|
|
563
|
-
agent_role: assignedTo.role,
|
|
564
|
-
session_id: `assign-${Date.now()}`,
|
|
565
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
566
|
-
tool_name: "assignment",
|
|
567
|
-
project_name: "",
|
|
568
|
-
has_error: false,
|
|
569
|
-
raw_text: `Task assigned by ${assignedBy}: ${taskDescription}`,
|
|
570
|
-
vector: null
|
|
571
|
-
// Will be backfilled
|
|
572
|
-
};
|
|
573
|
-
}
|
|
574
|
-
function formatAssignmentOutput(employee, task, score, mode, bloomRouting) {
|
|
575
|
-
const modeLabel = mode === "auto" ? "auto-routed" : "direct";
|
|
576
|
-
const percent = Math.round(score * 100);
|
|
577
|
-
const lines = [
|
|
578
|
-
`Assigned to ${employee.name} (${employee.role})`,
|
|
579
|
-
`Mode: ${modeLabel}`,
|
|
580
|
-
`Relevance: ${percent}%`,
|
|
581
|
-
`Task: ${task}`
|
|
582
|
-
];
|
|
583
|
-
if (bloomRouting) {
|
|
584
|
-
lines.push(`Complexity tier: ${bloomRouting.tier}`);
|
|
585
|
-
if (bloomRouting.reviewRequired) {
|
|
586
|
-
lines.push(`Review: required`);
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
return lines.join("\n");
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
// src/types/memory.ts
|
|
593
|
-
var EMBEDDING_DIM = 1024;
|
|
594
|
-
|
|
595
|
-
// src/lib/exe-daemon-client.ts
|
|
596
|
-
init_config();
|
|
597
|
-
import net from "net";
|
|
598
|
-
import { spawn } from "child_process";
|
|
599
|
-
import { randomUUID as randomUUID2 } from "crypto";
|
|
600
|
-
import { existsSync as existsSync3, unlinkSync, readFileSync as readFileSync2, openSync, closeSync, statSync } from "fs";
|
|
601
|
-
import path3 from "path";
|
|
602
|
-
import { fileURLToPath } from "url";
|
|
603
|
-
var SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ?? path3.join(EXE_AI_DIR, "exed.sock");
|
|
604
|
-
var PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ?? path3.join(EXE_AI_DIR, "exed.pid");
|
|
605
|
-
var SPAWN_LOCK_PATH = path3.join(EXE_AI_DIR, "exed-spawn.lock");
|
|
606
|
-
var SPAWN_LOCK_STALE_MS = 3e4;
|
|
607
|
-
var CONNECT_TIMEOUT_MS = 15e3;
|
|
608
|
-
var REQUEST_TIMEOUT_MS = 3e4;
|
|
609
|
-
var _socket = null;
|
|
610
|
-
var _connected = false;
|
|
611
|
-
var _buffer = "";
|
|
612
|
-
var _requestCount = 0;
|
|
613
|
-
var HEALTH_CHECK_INTERVAL = 100;
|
|
614
|
-
var _pending = /* @__PURE__ */ new Map();
|
|
615
|
-
function handleData(chunk) {
|
|
616
|
-
_buffer += chunk.toString();
|
|
617
|
-
let newlineIdx;
|
|
618
|
-
while ((newlineIdx = _buffer.indexOf("\n")) !== -1) {
|
|
619
|
-
const line = _buffer.slice(0, newlineIdx).trim();
|
|
620
|
-
_buffer = _buffer.slice(newlineIdx + 1);
|
|
621
|
-
if (!line) continue;
|
|
622
|
-
try {
|
|
623
|
-
const response = JSON.parse(line);
|
|
624
|
-
const entry = _pending.get(response.id);
|
|
625
|
-
if (entry) {
|
|
626
|
-
clearTimeout(entry.timer);
|
|
627
|
-
_pending.delete(response.id);
|
|
628
|
-
entry.resolve(response);
|
|
629
|
-
}
|
|
630
|
-
} catch {
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
function cleanupStaleFiles() {
|
|
635
|
-
if (existsSync3(PID_PATH)) {
|
|
636
|
-
try {
|
|
637
|
-
const pid = parseInt(readFileSync2(PID_PATH, "utf8").trim(), 10);
|
|
638
|
-
if (pid > 0) {
|
|
639
|
-
try {
|
|
640
|
-
process.kill(pid, 0);
|
|
641
|
-
return;
|
|
642
|
-
} catch {
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
} catch {
|
|
646
|
-
}
|
|
647
|
-
try {
|
|
648
|
-
unlinkSync(PID_PATH);
|
|
649
|
-
} catch {
|
|
650
|
-
}
|
|
651
|
-
try {
|
|
652
|
-
unlinkSync(SOCKET_PATH);
|
|
653
|
-
} catch {
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
function findPackageRoot() {
|
|
658
|
-
let dir = path3.dirname(fileURLToPath(import.meta.url));
|
|
659
|
-
const { root } = path3.parse(dir);
|
|
660
|
-
while (dir !== root) {
|
|
661
|
-
if (existsSync3(path3.join(dir, "package.json"))) return dir;
|
|
662
|
-
dir = path3.dirname(dir);
|
|
663
|
-
}
|
|
664
|
-
return null;
|
|
665
|
-
}
|
|
666
|
-
function spawnDaemon() {
|
|
667
|
-
const pkgRoot = findPackageRoot();
|
|
668
|
-
if (!pkgRoot) {
|
|
669
|
-
process.stderr.write("[exed-client] WARN: cannot find package root\n");
|
|
670
|
-
return;
|
|
671
|
-
}
|
|
672
|
-
const daemonPath = path3.join(pkgRoot, "dist", "lib", "exe-daemon.js");
|
|
673
|
-
if (!existsSync3(daemonPath)) {
|
|
674
|
-
process.stderr.write(`[exed-client] WARN: daemon script not found at ${daemonPath}
|
|
675
|
-
`);
|
|
676
|
-
return;
|
|
677
|
-
}
|
|
678
|
-
const resolvedPath = daemonPath;
|
|
679
|
-
process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
|
|
680
|
-
`);
|
|
681
|
-
const logPath = path3.join(path3.dirname(SOCKET_PATH), "exed.log");
|
|
682
|
-
let stderrFd = "ignore";
|
|
683
|
-
try {
|
|
684
|
-
stderrFd = openSync(logPath, "a");
|
|
685
|
-
} catch {
|
|
686
|
-
}
|
|
687
|
-
const child = spawn(process.execPath, [resolvedPath], {
|
|
688
|
-
detached: true,
|
|
689
|
-
stdio: ["ignore", "ignore", stderrFd],
|
|
690
|
-
env: {
|
|
691
|
-
...process.env,
|
|
692
|
-
EXE_DAEMON_SOCK: SOCKET_PATH,
|
|
693
|
-
EXE_DAEMON_PID: PID_PATH
|
|
694
|
-
}
|
|
695
|
-
});
|
|
696
|
-
child.unref();
|
|
697
|
-
if (typeof stderrFd === "number") {
|
|
698
|
-
try {
|
|
699
|
-
closeSync(stderrFd);
|
|
700
|
-
} catch {
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
function acquireSpawnLock() {
|
|
705
|
-
try {
|
|
706
|
-
const fd = openSync(SPAWN_LOCK_PATH, "wx");
|
|
707
|
-
closeSync(fd);
|
|
708
|
-
return true;
|
|
709
|
-
} catch {
|
|
710
|
-
try {
|
|
711
|
-
const stat = statSync(SPAWN_LOCK_PATH);
|
|
712
|
-
if (Date.now() - stat.mtimeMs > SPAWN_LOCK_STALE_MS) {
|
|
713
|
-
try {
|
|
714
|
-
unlinkSync(SPAWN_LOCK_PATH);
|
|
715
|
-
} catch {
|
|
716
|
-
}
|
|
717
|
-
try {
|
|
718
|
-
const fd = openSync(SPAWN_LOCK_PATH, "wx");
|
|
719
|
-
closeSync(fd);
|
|
720
|
-
return true;
|
|
721
|
-
} catch {
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
} catch {
|
|
725
|
-
}
|
|
726
|
-
return false;
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
function releaseSpawnLock() {
|
|
730
|
-
try {
|
|
731
|
-
unlinkSync(SPAWN_LOCK_PATH);
|
|
732
|
-
} catch {
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
function connectToSocket() {
|
|
736
|
-
return new Promise((resolve) => {
|
|
737
|
-
if (_socket && _connected) {
|
|
738
|
-
resolve(true);
|
|
739
|
-
return;
|
|
740
|
-
}
|
|
741
|
-
const socket = net.createConnection({ path: SOCKET_PATH });
|
|
742
|
-
const connectTimeout = setTimeout(() => {
|
|
743
|
-
socket.destroy();
|
|
744
|
-
resolve(false);
|
|
745
|
-
}, 2e3);
|
|
746
|
-
socket.on("connect", () => {
|
|
747
|
-
clearTimeout(connectTimeout);
|
|
748
|
-
_socket = socket;
|
|
749
|
-
_connected = true;
|
|
750
|
-
_buffer = "";
|
|
751
|
-
socket.on("data", handleData);
|
|
752
|
-
socket.on("close", () => {
|
|
753
|
-
_connected = false;
|
|
754
|
-
_socket = null;
|
|
755
|
-
for (const [id, entry] of _pending) {
|
|
756
|
-
clearTimeout(entry.timer);
|
|
757
|
-
_pending.delete(id);
|
|
758
|
-
entry.resolve({ error: "Connection closed" });
|
|
759
|
-
}
|
|
760
|
-
});
|
|
761
|
-
socket.on("error", () => {
|
|
762
|
-
_connected = false;
|
|
763
|
-
_socket = null;
|
|
764
|
-
});
|
|
765
|
-
resolve(true);
|
|
766
|
-
});
|
|
767
|
-
socket.on("error", () => {
|
|
768
|
-
clearTimeout(connectTimeout);
|
|
769
|
-
resolve(false);
|
|
770
|
-
});
|
|
771
|
-
});
|
|
772
|
-
}
|
|
773
|
-
async function connectEmbedDaemon() {
|
|
774
|
-
if (_socket && _connected) return true;
|
|
775
|
-
if (await connectToSocket()) return true;
|
|
776
|
-
if (acquireSpawnLock()) {
|
|
777
|
-
try {
|
|
778
|
-
cleanupStaleFiles();
|
|
779
|
-
spawnDaemon();
|
|
780
|
-
} finally {
|
|
781
|
-
releaseSpawnLock();
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
const start = Date.now();
|
|
785
|
-
let delay = 100;
|
|
786
|
-
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
787
|
-
await new Promise((r) => setTimeout(r, delay));
|
|
788
|
-
if (await connectToSocket()) return true;
|
|
789
|
-
delay = Math.min(delay * 2, 3e3);
|
|
790
|
-
}
|
|
791
|
-
return false;
|
|
792
|
-
}
|
|
793
|
-
function sendRequest(texts, priority) {
|
|
794
|
-
return new Promise((resolve) => {
|
|
795
|
-
if (!_socket || !_connected) {
|
|
796
|
-
resolve({ error: "Not connected" });
|
|
797
|
-
return;
|
|
798
|
-
}
|
|
799
|
-
const id = randomUUID2();
|
|
800
|
-
const timer = setTimeout(() => {
|
|
801
|
-
_pending.delete(id);
|
|
802
|
-
resolve({ error: "Request timeout" });
|
|
803
|
-
}, REQUEST_TIMEOUT_MS);
|
|
804
|
-
_pending.set(id, { resolve, timer });
|
|
805
|
-
try {
|
|
806
|
-
_socket.write(JSON.stringify({ id, texts, priority }) + "\n");
|
|
807
|
-
} catch {
|
|
808
|
-
clearTimeout(timer);
|
|
809
|
-
_pending.delete(id);
|
|
810
|
-
resolve({ error: "Write failed" });
|
|
811
|
-
}
|
|
812
|
-
});
|
|
813
|
-
}
|
|
814
|
-
async function pingDaemon() {
|
|
815
|
-
if (!_socket || !_connected) return null;
|
|
816
|
-
return new Promise((resolve) => {
|
|
817
|
-
const id = randomUUID2();
|
|
818
|
-
const timer = setTimeout(() => {
|
|
819
|
-
_pending.delete(id);
|
|
820
|
-
resolve(null);
|
|
821
|
-
}, 5e3);
|
|
822
|
-
_pending.set(id, {
|
|
823
|
-
resolve: (data) => {
|
|
824
|
-
if (data.health) {
|
|
825
|
-
resolve(data.health);
|
|
826
|
-
} else {
|
|
827
|
-
resolve(null);
|
|
828
|
-
}
|
|
829
|
-
},
|
|
830
|
-
timer
|
|
831
|
-
});
|
|
832
|
-
try {
|
|
833
|
-
_socket.write(JSON.stringify({ id, type: "health" }) + "\n");
|
|
834
|
-
} catch {
|
|
835
|
-
clearTimeout(timer);
|
|
836
|
-
_pending.delete(id);
|
|
837
|
-
resolve(null);
|
|
838
|
-
}
|
|
839
|
-
});
|
|
840
|
-
}
|
|
841
|
-
function killAndRespawnDaemon() {
|
|
842
|
-
process.stderr.write("[exed-client] Killing daemon for restart...\n");
|
|
843
|
-
if (existsSync3(PID_PATH)) {
|
|
844
|
-
try {
|
|
845
|
-
const pid = parseInt(readFileSync2(PID_PATH, "utf8").trim(), 10);
|
|
846
|
-
if (pid > 0) {
|
|
847
|
-
try {
|
|
848
|
-
process.kill(pid, "SIGKILL");
|
|
849
|
-
} catch {
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
} catch {
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
if (_socket) {
|
|
856
|
-
_socket.destroy();
|
|
857
|
-
_socket = null;
|
|
858
|
-
}
|
|
859
|
-
_connected = false;
|
|
860
|
-
_buffer = "";
|
|
861
|
-
try {
|
|
862
|
-
unlinkSync(PID_PATH);
|
|
863
|
-
} catch {
|
|
864
|
-
}
|
|
865
|
-
try {
|
|
866
|
-
unlinkSync(SOCKET_PATH);
|
|
867
|
-
} catch {
|
|
868
|
-
}
|
|
869
|
-
spawnDaemon();
|
|
870
|
-
}
|
|
871
|
-
async function embedViaClient(text, priority = "high") {
|
|
872
|
-
if (!_connected && !await connectEmbedDaemon()) return null;
|
|
873
|
-
_requestCount++;
|
|
874
|
-
if (_requestCount % HEALTH_CHECK_INTERVAL === 0) {
|
|
875
|
-
const health = await pingDaemon();
|
|
876
|
-
if (!health) {
|
|
877
|
-
process.stderr.write(`[exed-client] Periodic health check failed at request ${_requestCount} \u2014 restarting daemon
|
|
878
|
-
`);
|
|
879
|
-
killAndRespawnDaemon();
|
|
880
|
-
const start = Date.now();
|
|
881
|
-
let delay = 200;
|
|
882
|
-
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
883
|
-
await new Promise((r) => setTimeout(r, delay));
|
|
884
|
-
if (await connectToSocket()) break;
|
|
885
|
-
delay = Math.min(delay * 2, 3e3);
|
|
886
|
-
}
|
|
887
|
-
if (!_connected) return null;
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
const result = await sendRequest([text], priority);
|
|
891
|
-
if (!result.error && result.vectors?.[0]) return result.vectors[0];
|
|
892
|
-
if (result.error) {
|
|
893
|
-
process.stderr.write(`[exed-client] Embed failed (${result.error}) \u2014 attempting restart
|
|
894
|
-
`);
|
|
895
|
-
killAndRespawnDaemon();
|
|
896
|
-
const start = Date.now();
|
|
897
|
-
let delay = 200;
|
|
898
|
-
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
899
|
-
await new Promise((r) => setTimeout(r, delay));
|
|
900
|
-
if (await connectToSocket()) break;
|
|
901
|
-
delay = Math.min(delay * 2, 3e3);
|
|
902
|
-
}
|
|
903
|
-
if (!_connected) return null;
|
|
904
|
-
const retry = await sendRequest([text], priority);
|
|
905
|
-
if (!retry.error && retry.vectors?.[0]) return retry.vectors[0];
|
|
906
|
-
process.stderr.write(`[exed-client] Embed retry also failed: ${retry.error ?? "no vector"}
|
|
907
|
-
`);
|
|
908
|
-
}
|
|
909
|
-
return null;
|
|
910
|
-
}
|
|
911
|
-
|
|
912
|
-
// src/lib/embedder.ts
|
|
913
|
-
async function embed(text) {
|
|
914
|
-
const priority = process.env.EXE_EMBED_PRIORITY === "low" ? "low" : "high";
|
|
915
|
-
const vector = await embedViaClient(text, priority);
|
|
916
|
-
if (!vector) {
|
|
917
|
-
throw new Error(
|
|
918
|
-
"Embedding failed: daemon unavailable. Run /exe-setup to verify model installation."
|
|
919
|
-
);
|
|
920
|
-
}
|
|
921
|
-
if (vector.length !== EMBEDDING_DIM) {
|
|
922
|
-
throw new Error(
|
|
923
|
-
`Embedding dimension mismatch: expected ${EMBEDDING_DIM}, got ${vector.length}. Ensure the correct Jina v5-small Q4_K_M GGUF model is installed.`
|
|
924
|
-
);
|
|
925
|
-
}
|
|
926
|
-
return vector;
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
// src/lib/database.ts
|
|
930
|
-
import { createClient } from "@libsql/client";
|
|
931
|
-
var _client = null;
|
|
932
|
-
var initTurso = initDatabase;
|
|
933
|
-
async function initDatabase(config) {
|
|
934
|
-
if (_client) {
|
|
935
|
-
_client.close();
|
|
936
|
-
_client = null;
|
|
937
|
-
}
|
|
938
|
-
const opts = {
|
|
939
|
-
url: `file:${config.dbPath}`
|
|
940
|
-
};
|
|
941
|
-
if (config.encryptionKey) {
|
|
942
|
-
opts.encryptionKey = config.encryptionKey;
|
|
943
|
-
}
|
|
944
|
-
_client = createClient(opts);
|
|
945
|
-
}
|
|
946
|
-
function getClient() {
|
|
947
|
-
if (!_client) {
|
|
948
|
-
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
949
|
-
}
|
|
950
|
-
return _client;
|
|
951
|
-
}
|
|
952
|
-
async function ensureSchema() {
|
|
953
|
-
const client = getClient();
|
|
954
|
-
await client.execute("PRAGMA journal_mode = WAL");
|
|
955
|
-
await client.execute("PRAGMA busy_timeout = 5000");
|
|
956
|
-
try {
|
|
957
|
-
await client.execute("PRAGMA libsql_vector_search_ef = 128");
|
|
958
|
-
} catch {
|
|
959
|
-
}
|
|
960
|
-
await client.executeMultiple(`
|
|
961
|
-
CREATE TABLE IF NOT EXISTS memories (
|
|
962
|
-
id TEXT PRIMARY KEY,
|
|
963
|
-
agent_id TEXT NOT NULL,
|
|
964
|
-
agent_role TEXT NOT NULL,
|
|
965
|
-
session_id TEXT NOT NULL,
|
|
966
|
-
timestamp TEXT NOT NULL,
|
|
967
|
-
tool_name TEXT NOT NULL,
|
|
968
|
-
project_name TEXT NOT NULL,
|
|
969
|
-
has_error INTEGER NOT NULL DEFAULT 0,
|
|
970
|
-
raw_text TEXT NOT NULL,
|
|
971
|
-
vector F32_BLOB(1024),
|
|
972
|
-
version INTEGER NOT NULL DEFAULT 0
|
|
973
|
-
);
|
|
974
|
-
|
|
975
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent
|
|
976
|
-
ON memories(agent_id);
|
|
977
|
-
|
|
978
|
-
CREATE INDEX IF NOT EXISTS idx_memories_timestamp
|
|
979
|
-
ON memories(timestamp);
|
|
980
|
-
|
|
981
|
-
CREATE INDEX IF NOT EXISTS idx_memories_session
|
|
982
|
-
ON memories(session_id);
|
|
983
|
-
|
|
984
|
-
CREATE INDEX IF NOT EXISTS idx_memories_project
|
|
985
|
-
ON memories(project_name);
|
|
986
|
-
|
|
987
|
-
CREATE INDEX IF NOT EXISTS idx_memories_tool
|
|
988
|
-
ON memories(tool_name);
|
|
989
|
-
|
|
990
|
-
CREATE INDEX IF NOT EXISTS idx_memories_version
|
|
991
|
-
ON memories(version);
|
|
992
|
-
|
|
993
|
-
CREATE INDEX IF NOT EXISTS idx_memories_agent_project
|
|
994
|
-
ON memories(agent_id, project_name);
|
|
995
|
-
`);
|
|
996
|
-
await client.executeMultiple(`
|
|
997
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
998
|
-
raw_text,
|
|
999
|
-
content='memories',
|
|
1000
|
-
content_rowid='rowid'
|
|
1001
|
-
);
|
|
1002
|
-
|
|
1003
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ai AFTER INSERT ON memories BEGIN
|
|
1004
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
1005
|
-
END;
|
|
1006
|
-
|
|
1007
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_ad AFTER DELETE ON memories BEGIN
|
|
1008
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
1009
|
-
END;
|
|
1010
|
-
|
|
1011
|
-
CREATE TRIGGER IF NOT EXISTS memories_fts_au AFTER UPDATE ON memories BEGIN
|
|
1012
|
-
INSERT INTO memories_fts(memories_fts, rowid, raw_text) VALUES('delete', old.rowid, old.raw_text);
|
|
1013
|
-
INSERT INTO memories_fts(rowid, raw_text) VALUES (new.rowid, new.raw_text);
|
|
1014
|
-
END;
|
|
1015
|
-
`);
|
|
1016
|
-
await client.executeMultiple(`
|
|
1017
|
-
CREATE TABLE IF NOT EXISTS sync_meta (
|
|
1018
|
-
key TEXT PRIMARY KEY,
|
|
1019
|
-
value TEXT NOT NULL
|
|
1020
|
-
);
|
|
1021
|
-
`);
|
|
1022
|
-
await client.executeMultiple(`
|
|
1023
|
-
CREATE TABLE IF NOT EXISTS tasks (
|
|
1024
|
-
id TEXT PRIMARY KEY,
|
|
1025
|
-
title TEXT NOT NULL,
|
|
1026
|
-
assigned_to TEXT NOT NULL,
|
|
1027
|
-
assigned_by TEXT NOT NULL,
|
|
1028
|
-
project_name TEXT NOT NULL,
|
|
1029
|
-
priority TEXT NOT NULL DEFAULT 'p1',
|
|
1030
|
-
status TEXT NOT NULL DEFAULT 'open',
|
|
1031
|
-
task_file TEXT,
|
|
1032
|
-
created_at TEXT NOT NULL,
|
|
1033
|
-
updated_at TEXT NOT NULL
|
|
1034
|
-
);
|
|
1035
|
-
|
|
1036
|
-
CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status
|
|
1037
|
-
ON tasks(assigned_to, status);
|
|
1038
|
-
`);
|
|
1039
|
-
await client.executeMultiple(`
|
|
1040
|
-
CREATE TABLE IF NOT EXISTS behaviors (
|
|
1041
|
-
id TEXT PRIMARY KEY,
|
|
1042
|
-
agent_id TEXT NOT NULL,
|
|
1043
|
-
project_name TEXT,
|
|
1044
|
-
domain TEXT,
|
|
1045
|
-
content TEXT NOT NULL,
|
|
1046
|
-
active INTEGER NOT NULL DEFAULT 1,
|
|
1047
|
-
created_at TEXT NOT NULL,
|
|
1048
|
-
updated_at TEXT NOT NULL
|
|
1049
|
-
);
|
|
1050
|
-
|
|
1051
|
-
CREATE INDEX IF NOT EXISTS idx_behaviors_agent
|
|
1052
|
-
ON behaviors(agent_id, active);
|
|
1053
|
-
`);
|
|
1054
|
-
try {
|
|
1055
|
-
const existing = await client.execute({
|
|
1056
|
-
sql: "SELECT COUNT(*) as cnt FROM behaviors WHERE agent_id = 'exe'",
|
|
1057
|
-
args: []
|
|
1058
|
-
});
|
|
1059
|
-
if (Number(existing.rows[0]?.cnt) === 0) {
|
|
1060
|
-
await client.executeMultiple(`
|
|
1061
|
-
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
1062
|
-
VALUES
|
|
1063
|
-
(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');
|
|
1064
|
-
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
1065
|
-
VALUES
|
|
1066
|
-
(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');
|
|
1067
|
-
INSERT INTO behaviors (id, agent_id, project_name, domain, content, active, created_at, updated_at)
|
|
1068
|
-
VALUES
|
|
1069
|
-
(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');
|
|
1070
|
-
`);
|
|
1071
|
-
}
|
|
1072
|
-
} catch {
|
|
1073
|
-
}
|
|
1074
|
-
try {
|
|
1075
|
-
await client.execute({
|
|
1076
|
-
sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
|
|
1077
|
-
args: []
|
|
1078
|
-
});
|
|
1079
|
-
} catch {
|
|
1080
|
-
}
|
|
1081
|
-
try {
|
|
1082
|
-
await client.execute({
|
|
1083
|
-
sql: `ALTER TABLE tasks ADD COLUMN blocked_by TEXT`,
|
|
1084
|
-
args: []
|
|
1085
|
-
});
|
|
1086
|
-
} catch {
|
|
1087
|
-
}
|
|
1088
|
-
try {
|
|
1089
|
-
await client.execute({
|
|
1090
|
-
sql: `ALTER TABLE tasks ADD COLUMN parent_task_id TEXT`,
|
|
1091
|
-
args: []
|
|
1092
|
-
});
|
|
1093
|
-
} catch {
|
|
1094
|
-
}
|
|
1095
|
-
try {
|
|
1096
|
-
await client.execute({
|
|
1097
|
-
sql: `CREATE INDEX IF NOT EXISTS idx_tasks_parent_task_id
|
|
1098
|
-
ON tasks(parent_task_id)
|
|
1099
|
-
WHERE parent_task_id IS NOT NULL`,
|
|
1100
|
-
args: []
|
|
1101
|
-
});
|
|
1102
|
-
} catch {
|
|
1103
|
-
}
|
|
1104
|
-
try {
|
|
1105
|
-
await client.execute({
|
|
1106
|
-
sql: `UPDATE tasks SET status = 'done' WHERE status = 'completed'`,
|
|
1107
|
-
args: []
|
|
1108
|
-
});
|
|
1109
|
-
} catch {
|
|
1110
|
-
}
|
|
1111
|
-
try {
|
|
1112
|
-
await client.execute({
|
|
1113
|
-
sql: `ALTER TABLE tasks ADD COLUMN reviewer TEXT`,
|
|
1114
|
-
args: []
|
|
1115
|
-
});
|
|
1116
|
-
} catch {
|
|
1117
|
-
}
|
|
1118
|
-
try {
|
|
1119
|
-
await client.execute({
|
|
1120
|
-
sql: `ALTER TABLE tasks ADD COLUMN context TEXT`,
|
|
1121
|
-
args: []
|
|
1122
|
-
});
|
|
1123
|
-
} catch {
|
|
1124
|
-
}
|
|
1125
|
-
try {
|
|
1126
|
-
await client.execute({
|
|
1127
|
-
sql: `ALTER TABLE tasks ADD COLUMN result TEXT`,
|
|
1128
|
-
args: []
|
|
1129
|
-
});
|
|
1130
|
-
} catch {
|
|
1131
|
-
}
|
|
1132
|
-
try {
|
|
1133
|
-
await client.execute({
|
|
1134
|
-
sql: `ALTER TABLE tasks ADD COLUMN assigned_tmux TEXT`,
|
|
1135
|
-
args: []
|
|
1136
|
-
});
|
|
1137
|
-
} catch {
|
|
1138
|
-
}
|
|
1139
|
-
try {
|
|
1140
|
-
await client.execute({
|
|
1141
|
-
sql: `ALTER TABLE tasks ADD COLUMN checkpoint TEXT`,
|
|
1142
|
-
args: []
|
|
1143
|
-
});
|
|
1144
|
-
} catch {
|
|
1145
|
-
}
|
|
1146
|
-
try {
|
|
1147
|
-
await client.execute({
|
|
1148
|
-
sql: `ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER NOT NULL DEFAULT 0`,
|
|
1149
|
-
args: []
|
|
1150
|
-
});
|
|
1151
|
-
} catch {
|
|
1152
|
-
}
|
|
1153
|
-
try {
|
|
1154
|
-
await client.execute({
|
|
1155
|
-
sql: `ALTER TABLE tasks ADD COLUMN complexity TEXT NOT NULL DEFAULT 'standard'`,
|
|
1156
|
-
args: []
|
|
1157
|
-
});
|
|
1158
|
-
} catch {
|
|
1159
|
-
}
|
|
1160
|
-
try {
|
|
1161
|
-
await client.execute({
|
|
1162
|
-
sql: `ALTER TABLE memories ADD COLUMN task_id TEXT`,
|
|
1163
|
-
args: []
|
|
1164
|
-
});
|
|
1165
|
-
} catch {
|
|
1166
|
-
}
|
|
1167
|
-
try {
|
|
1168
|
-
await client.execute({
|
|
1169
|
-
sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
|
|
1170
|
-
args: []
|
|
1171
|
-
});
|
|
1172
|
-
} catch {
|
|
1173
|
-
}
|
|
1174
|
-
try {
|
|
1175
|
-
await client.execute({
|
|
1176
|
-
sql: `ALTER TABLE memories ADD COLUMN author_device_id TEXT`,
|
|
1177
|
-
args: []
|
|
1178
|
-
});
|
|
1179
|
-
} catch {
|
|
1180
|
-
}
|
|
1181
|
-
try {
|
|
1182
|
-
await client.execute({
|
|
1183
|
-
sql: `ALTER TABLE memories ADD COLUMN scope TEXT NOT NULL DEFAULT 'business'`,
|
|
1184
|
-
args: []
|
|
1185
|
-
});
|
|
1186
|
-
} catch {
|
|
1187
|
-
}
|
|
1188
|
-
await client.executeMultiple(`
|
|
1189
|
-
CREATE TABLE IF NOT EXISTS consolidations (
|
|
1190
|
-
id TEXT PRIMARY KEY,
|
|
1191
|
-
consolidated_memory_id TEXT NOT NULL,
|
|
1192
|
-
source_memory_id TEXT NOT NULL,
|
|
1193
|
-
created_at TEXT NOT NULL
|
|
1194
|
-
);
|
|
1195
|
-
|
|
1196
|
-
CREATE INDEX IF NOT EXISTS idx_consolidations_source
|
|
1197
|
-
ON consolidations(source_memory_id);
|
|
1198
|
-
|
|
1199
|
-
CREATE INDEX IF NOT EXISTS idx_consolidations_consolidated
|
|
1200
|
-
ON consolidations(consolidated_memory_id);
|
|
1201
|
-
`);
|
|
1202
|
-
await client.executeMultiple(`
|
|
1203
|
-
CREATE TABLE IF NOT EXISTS reminders (
|
|
1204
|
-
id TEXT PRIMARY KEY,
|
|
1205
|
-
text TEXT NOT NULL,
|
|
1206
|
-
created_at TEXT NOT NULL,
|
|
1207
|
-
due_date TEXT,
|
|
1208
|
-
completed_at TEXT
|
|
1209
|
-
);
|
|
1210
|
-
`);
|
|
1211
|
-
await client.executeMultiple(`
|
|
1212
|
-
CREATE TABLE IF NOT EXISTS notifications (
|
|
1213
|
-
id TEXT PRIMARY KEY,
|
|
1214
|
-
agent_id TEXT NOT NULL,
|
|
1215
|
-
agent_role TEXT NOT NULL,
|
|
1216
|
-
event TEXT NOT NULL,
|
|
1217
|
-
project TEXT NOT NULL,
|
|
1218
|
-
summary TEXT NOT NULL,
|
|
1219
|
-
task_file TEXT,
|
|
1220
|
-
read INTEGER NOT NULL DEFAULT 0,
|
|
1221
|
-
created_at TEXT NOT NULL
|
|
1222
|
-
);
|
|
1223
|
-
|
|
1224
|
-
CREATE INDEX IF NOT EXISTS idx_notifications_read
|
|
1225
|
-
ON notifications(read);
|
|
1226
|
-
|
|
1227
|
-
CREATE INDEX IF NOT EXISTS idx_notifications_agent
|
|
1228
|
-
ON notifications(agent_id);
|
|
1229
|
-
|
|
1230
|
-
CREATE INDEX IF NOT EXISTS idx_notifications_task_file
|
|
1231
|
-
ON notifications(task_file);
|
|
1232
|
-
`);
|
|
1233
|
-
await client.executeMultiple(`
|
|
1234
|
-
CREATE TABLE IF NOT EXISTS schedules (
|
|
1235
|
-
id TEXT PRIMARY KEY,
|
|
1236
|
-
cron TEXT NOT NULL,
|
|
1237
|
-
description TEXT NOT NULL,
|
|
1238
|
-
job_type TEXT NOT NULL DEFAULT 'report',
|
|
1239
|
-
prompt TEXT,
|
|
1240
|
-
assigned_to TEXT,
|
|
1241
|
-
project_name TEXT,
|
|
1242
|
-
active INTEGER NOT NULL DEFAULT 1,
|
|
1243
|
-
use_crontab INTEGER NOT NULL DEFAULT 0,
|
|
1244
|
-
created_at TEXT NOT NULL
|
|
1245
|
-
);
|
|
1246
|
-
`);
|
|
1247
|
-
await client.executeMultiple(`
|
|
1248
|
-
CREATE TABLE IF NOT EXISTS device_registry (
|
|
1249
|
-
device_id TEXT PRIMARY KEY,
|
|
1250
|
-
friendly_name TEXT NOT NULL,
|
|
1251
|
-
hostname TEXT NOT NULL,
|
|
1252
|
-
projects TEXT NOT NULL DEFAULT '[]',
|
|
1253
|
-
agents TEXT NOT NULL DEFAULT '[]',
|
|
1254
|
-
connected INTEGER DEFAULT 0,
|
|
1255
|
-
last_seen TEXT NOT NULL
|
|
1256
|
-
);
|
|
1257
|
-
`);
|
|
1258
|
-
await client.executeMultiple(`
|
|
1259
|
-
CREATE TABLE IF NOT EXISTS messages (
|
|
1260
|
-
id TEXT PRIMARY KEY,
|
|
1261
|
-
from_agent TEXT NOT NULL,
|
|
1262
|
-
from_device TEXT NOT NULL DEFAULT 'local',
|
|
1263
|
-
target_agent TEXT NOT NULL,
|
|
1264
|
-
target_project TEXT,
|
|
1265
|
-
target_device TEXT NOT NULL DEFAULT 'local',
|
|
1266
|
-
content TEXT NOT NULL,
|
|
1267
|
-
priority TEXT DEFAULT 'normal',
|
|
1268
|
-
status TEXT DEFAULT 'pending',
|
|
1269
|
-
server_seq INTEGER,
|
|
1270
|
-
retry_count INTEGER DEFAULT 0,
|
|
1271
|
-
created_at TEXT NOT NULL,
|
|
1272
|
-
delivered_at TEXT,
|
|
1273
|
-
processed_at TEXT,
|
|
1274
|
-
failed_at TEXT,
|
|
1275
|
-
failure_reason TEXT
|
|
1276
|
-
);
|
|
1277
|
-
|
|
1278
|
-
CREATE INDEX IF NOT EXISTS idx_messages_target
|
|
1279
|
-
ON messages(target_agent, status);
|
|
1280
|
-
|
|
1281
|
-
CREATE INDEX IF NOT EXISTS idx_messages_conversation_order
|
|
1282
|
-
ON messages(target_agent, from_agent, server_seq);
|
|
1283
|
-
`);
|
|
1284
|
-
try {
|
|
1285
|
-
await client.execute({
|
|
1286
|
-
sql: `UPDATE memories SET project_name = 'exe-create' WHERE project_name = 'web'`,
|
|
1287
|
-
args: []
|
|
1288
|
-
});
|
|
1289
|
-
await client.execute({
|
|
1290
|
-
sql: `UPDATE memories SET project_name = 'exe-os' WHERE project_name = 'worker'`,
|
|
1291
|
-
args: []
|
|
1292
|
-
});
|
|
1293
|
-
await client.execute({
|
|
1294
|
-
sql: `UPDATE tasks SET project_name = 'exe-create' WHERE project_name = 'web'`,
|
|
1295
|
-
args: []
|
|
1296
|
-
});
|
|
1297
|
-
await client.execute({
|
|
1298
|
-
sql: `UPDATE tasks SET project_name = 'exe-os' WHERE project_name = 'worker'`,
|
|
1299
|
-
args: []
|
|
1300
|
-
});
|
|
1301
|
-
} catch {
|
|
1302
|
-
}
|
|
1303
|
-
await client.executeMultiple(`
|
|
1304
|
-
CREATE TABLE IF NOT EXISTS trajectories (
|
|
1305
|
-
id TEXT PRIMARY KEY,
|
|
1306
|
-
task_id TEXT NOT NULL,
|
|
1307
|
-
agent_id TEXT NOT NULL,
|
|
1308
|
-
project_name TEXT NOT NULL,
|
|
1309
|
-
task_title TEXT NOT NULL,
|
|
1310
|
-
signature TEXT NOT NULL,
|
|
1311
|
-
signature_hash TEXT NOT NULL,
|
|
1312
|
-
tool_count INTEGER NOT NULL,
|
|
1313
|
-
skill_id TEXT,
|
|
1314
|
-
created_at TEXT NOT NULL
|
|
1315
|
-
);
|
|
1316
|
-
|
|
1317
|
-
CREATE INDEX IF NOT EXISTS idx_trajectories_hash
|
|
1318
|
-
ON trajectories(signature_hash);
|
|
1319
|
-
|
|
1320
|
-
CREATE INDEX IF NOT EXISTS idx_trajectories_agent
|
|
1321
|
-
ON trajectories(agent_id);
|
|
1322
|
-
`);
|
|
1323
|
-
try {
|
|
1324
|
-
await client.execute("ALTER TABLE trajectories ADD COLUMN skill_id TEXT");
|
|
1325
|
-
} catch {
|
|
1326
|
-
}
|
|
1327
|
-
await client.executeMultiple(`
|
|
1328
|
-
CREATE TABLE IF NOT EXISTS consolidations (
|
|
1329
|
-
id TEXT PRIMARY KEY,
|
|
1330
|
-
consolidated_memory_id TEXT NOT NULL,
|
|
1331
|
-
source_memory_id TEXT NOT NULL,
|
|
1332
|
-
created_at TEXT NOT NULL
|
|
1333
|
-
);
|
|
1334
|
-
|
|
1335
|
-
CREATE INDEX IF NOT EXISTS idx_consolidations_source
|
|
1336
|
-
ON consolidations(source_memory_id);
|
|
1337
|
-
`);
|
|
1338
|
-
await client.executeMultiple(`
|
|
1339
|
-
CREATE TABLE IF NOT EXISTS audit_trail (
|
|
1340
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
1341
|
-
timestamp TEXT NOT NULL,
|
|
1342
|
-
session_id TEXT NOT NULL,
|
|
1343
|
-
agent_id TEXT NOT NULL,
|
|
1344
|
-
tool TEXT NOT NULL,
|
|
1345
|
-
input TEXT,
|
|
1346
|
-
decision TEXT NOT NULL,
|
|
1347
|
-
reason TEXT,
|
|
1348
|
-
is_customer_facing INTEGER NOT NULL DEFAULT 0
|
|
1349
|
-
);
|
|
1350
|
-
|
|
1351
|
-
CREATE INDEX IF NOT EXISTS idx_audit_trail_agent
|
|
1352
|
-
ON audit_trail(agent_id, timestamp);
|
|
1353
|
-
|
|
1354
|
-
CREATE INDEX IF NOT EXISTS idx_audit_trail_session
|
|
1355
|
-
ON audit_trail(session_id);
|
|
1356
|
-
`);
|
|
1357
|
-
try {
|
|
1358
|
-
await client.execute({
|
|
1359
|
-
sql: `ALTER TABLE memories ADD COLUMN consolidated INTEGER NOT NULL DEFAULT 0`,
|
|
1360
|
-
args: []
|
|
1361
|
-
});
|
|
1362
|
-
} catch {
|
|
1363
|
-
}
|
|
1364
|
-
try {
|
|
1365
|
-
await client.execute({
|
|
1366
|
-
sql: `ALTER TABLE memories ADD COLUMN importance INTEGER DEFAULT 5`,
|
|
1367
|
-
args: []
|
|
1368
|
-
});
|
|
1369
|
-
} catch {
|
|
1370
|
-
}
|
|
1371
|
-
try {
|
|
1372
|
-
await client.execute({
|
|
1373
|
-
sql: `ALTER TABLE memories ADD COLUMN status TEXT DEFAULT 'active'`,
|
|
1374
|
-
args: []
|
|
1375
|
-
});
|
|
1376
|
-
} catch {
|
|
1377
|
-
}
|
|
1378
|
-
try {
|
|
1379
|
-
await client.execute({
|
|
1380
|
-
sql: `ALTER TABLE memories ADD COLUMN confidence REAL DEFAULT 0.7`,
|
|
1381
|
-
args: []
|
|
1382
|
-
});
|
|
1383
|
-
} catch {
|
|
1384
|
-
}
|
|
1385
|
-
try {
|
|
1386
|
-
await client.execute({
|
|
1387
|
-
sql: `ALTER TABLE memories ADD COLUMN last_accessed TEXT`,
|
|
1388
|
-
args: []
|
|
1389
|
-
});
|
|
1390
|
-
} catch {
|
|
1391
|
-
}
|
|
1392
|
-
try {
|
|
1393
|
-
await client.execute({
|
|
1394
|
-
sql: `UPDATE memories SET last_accessed = timestamp WHERE last_accessed IS NULL`,
|
|
1395
|
-
args: []
|
|
1396
|
-
});
|
|
1397
|
-
} catch {
|
|
1398
|
-
}
|
|
1399
|
-
try {
|
|
1400
|
-
await client.execute({
|
|
1401
|
-
sql: `ALTER TABLE memories ADD COLUMN wiki_synced INTEGER DEFAULT 0`,
|
|
1402
|
-
args: []
|
|
1403
|
-
});
|
|
1404
|
-
} catch {
|
|
1405
|
-
}
|
|
1406
|
-
try {
|
|
1407
|
-
await client.execute({
|
|
1408
|
-
sql: `ALTER TABLE memories ADD COLUMN graph_extracted INTEGER DEFAULT 0`,
|
|
1409
|
-
args: []
|
|
1410
|
-
});
|
|
1411
|
-
} catch {
|
|
1412
|
-
}
|
|
1413
|
-
for (const col of [
|
|
1414
|
-
"ALTER TABLE memories ADD COLUMN content_hash TEXT",
|
|
1415
|
-
"ALTER TABLE memories ADD COLUMN graph_extracted_hash TEXT"
|
|
1416
|
-
]) {
|
|
1417
|
-
try {
|
|
1418
|
-
await client.execute(col);
|
|
1419
|
-
} catch {
|
|
1420
|
-
}
|
|
1421
|
-
}
|
|
1422
|
-
await client.executeMultiple(`
|
|
1423
|
-
CREATE TABLE IF NOT EXISTS entities (
|
|
1424
|
-
id TEXT PRIMARY KEY,
|
|
1425
|
-
name TEXT NOT NULL,
|
|
1426
|
-
type TEXT NOT NULL,
|
|
1427
|
-
first_seen TEXT NOT NULL,
|
|
1428
|
-
last_seen TEXT NOT NULL,
|
|
1429
|
-
properties TEXT DEFAULT '{}',
|
|
1430
|
-
UNIQUE(name, type)
|
|
1431
|
-
);
|
|
1432
|
-
|
|
1433
|
-
CREATE TABLE IF NOT EXISTS relationships (
|
|
1434
|
-
id TEXT PRIMARY KEY,
|
|
1435
|
-
source_entity_id TEXT NOT NULL,
|
|
1436
|
-
target_entity_id TEXT NOT NULL,
|
|
1437
|
-
type TEXT NOT NULL,
|
|
1438
|
-
weight REAL DEFAULT 1.0,
|
|
1439
|
-
timestamp TEXT NOT NULL,
|
|
1440
|
-
properties TEXT DEFAULT '{}',
|
|
1441
|
-
UNIQUE(source_entity_id, target_entity_id, type)
|
|
1442
|
-
);
|
|
1443
|
-
|
|
1444
|
-
CREATE TABLE IF NOT EXISTS entity_memories (
|
|
1445
|
-
entity_id TEXT NOT NULL,
|
|
1446
|
-
memory_id TEXT NOT NULL,
|
|
1447
|
-
PRIMARY KEY (entity_id, memory_id)
|
|
1448
|
-
);
|
|
1449
|
-
|
|
1450
|
-
CREATE TABLE IF NOT EXISTS relationship_memories (
|
|
1451
|
-
relationship_id TEXT NOT NULL,
|
|
1452
|
-
memory_id TEXT NOT NULL,
|
|
1453
|
-
PRIMARY KEY (relationship_id, memory_id)
|
|
1454
|
-
);
|
|
1455
|
-
|
|
1456
|
-
CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
|
|
1457
|
-
CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type);
|
|
1458
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_entity_id);
|
|
1459
|
-
CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_entity_id);
|
|
1460
|
-
|
|
1461
|
-
CREATE TABLE IF NOT EXISTS hyperedges (
|
|
1462
|
-
id TEXT PRIMARY KEY,
|
|
1463
|
-
label TEXT NOT NULL,
|
|
1464
|
-
relation TEXT NOT NULL,
|
|
1465
|
-
confidence REAL DEFAULT 1.0,
|
|
1466
|
-
timestamp TEXT NOT NULL
|
|
1467
|
-
);
|
|
1468
|
-
|
|
1469
|
-
CREATE TABLE IF NOT EXISTS hyperedge_nodes (
|
|
1470
|
-
hyperedge_id TEXT NOT NULL,
|
|
1471
|
-
entity_id TEXT NOT NULL,
|
|
1472
|
-
PRIMARY KEY (hyperedge_id, entity_id)
|
|
1473
|
-
);
|
|
1474
|
-
`);
|
|
1475
|
-
await client.executeMultiple(`
|
|
1476
|
-
CREATE TABLE IF NOT EXISTS entity_aliases (
|
|
1477
|
-
alias TEXT NOT NULL PRIMARY KEY,
|
|
1478
|
-
canonical_entity_id TEXT NOT NULL
|
|
1479
|
-
);
|
|
1480
|
-
CREATE INDEX IF NOT EXISTS idx_entity_aliases_canonical ON entity_aliases(canonical_entity_id);
|
|
1481
|
-
`);
|
|
1482
|
-
for (const col of [
|
|
1483
|
-
"ALTER TABLE relationships ADD COLUMN confidence REAL DEFAULT 1.0",
|
|
1484
|
-
"ALTER TABLE relationships ADD COLUMN confidence_label TEXT DEFAULT 'extracted'"
|
|
1485
|
-
]) {
|
|
1486
|
-
try {
|
|
1487
|
-
await client.execute(col);
|
|
1488
|
-
} catch {
|
|
1489
|
-
}
|
|
1490
|
-
}
|
|
1491
|
-
try {
|
|
1492
|
-
await client.execute(
|
|
1493
|
-
`CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status)`
|
|
1494
|
-
);
|
|
1495
|
-
} catch {
|
|
1496
|
-
}
|
|
1497
|
-
await client.executeMultiple(`
|
|
1498
|
-
CREATE TABLE IF NOT EXISTS identity (
|
|
1499
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
1500
|
-
agent_id TEXT NOT NULL UNIQUE,
|
|
1501
|
-
content_hash TEXT NOT NULL,
|
|
1502
|
-
updated_at TEXT NOT NULL,
|
|
1503
|
-
updated_by TEXT NOT NULL
|
|
1504
|
-
);
|
|
1505
|
-
|
|
1506
|
-
CREATE INDEX IF NOT EXISTS idx_identity_agent ON identity(agent_id);
|
|
1507
|
-
`);
|
|
1508
|
-
await client.executeMultiple(`
|
|
1509
|
-
CREATE TABLE IF NOT EXISTS chat_history (
|
|
1510
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
1511
|
-
session_id TEXT NOT NULL,
|
|
1512
|
-
role TEXT NOT NULL,
|
|
1513
|
-
content TEXT NOT NULL,
|
|
1514
|
-
tool_name TEXT,
|
|
1515
|
-
tool_id TEXT,
|
|
1516
|
-
is_error INTEGER NOT NULL DEFAULT 0,
|
|
1517
|
-
timestamp INTEGER NOT NULL
|
|
1518
|
-
);
|
|
1519
|
-
|
|
1520
|
-
CREATE INDEX IF NOT EXISTS idx_chat_history_session
|
|
1521
|
-
ON chat_history(session_id, id);
|
|
1522
|
-
`);
|
|
1523
|
-
await client.executeMultiple(`
|
|
1524
|
-
CREATE TABLE IF NOT EXISTS workspaces (
|
|
1525
|
-
id TEXT PRIMARY KEY,
|
|
1526
|
-
slug TEXT NOT NULL UNIQUE,
|
|
1527
|
-
name TEXT NOT NULL,
|
|
1528
|
-
owner_agent_id TEXT,
|
|
1529
|
-
created_at TEXT NOT NULL,
|
|
1530
|
-
metadata TEXT
|
|
1531
|
-
);
|
|
1532
|
-
|
|
1533
|
-
CREATE INDEX IF NOT EXISTS idx_workspaces_slug
|
|
1534
|
-
ON workspaces(slug);
|
|
1535
|
-
`);
|
|
1536
|
-
await client.executeMultiple(`
|
|
1537
|
-
CREATE TABLE IF NOT EXISTS documents (
|
|
1538
|
-
id TEXT PRIMARY KEY,
|
|
1539
|
-
workspace_id TEXT NOT NULL,
|
|
1540
|
-
filename TEXT NOT NULL,
|
|
1541
|
-
mime TEXT,
|
|
1542
|
-
source_type TEXT,
|
|
1543
|
-
user_id TEXT,
|
|
1544
|
-
uploaded_at TEXT NOT NULL,
|
|
1545
|
-
metadata TEXT,
|
|
1546
|
-
FOREIGN KEY (workspace_id) REFERENCES workspaces(id)
|
|
1547
|
-
);
|
|
1548
|
-
|
|
1549
|
-
CREATE INDEX IF NOT EXISTS idx_documents_workspace
|
|
1550
|
-
ON documents(workspace_id);
|
|
1551
|
-
|
|
1552
|
-
CREATE INDEX IF NOT EXISTS idx_documents_user
|
|
1553
|
-
ON documents(user_id);
|
|
1554
|
-
`);
|
|
1555
|
-
for (const column of [
|
|
1556
|
-
"workspace_id TEXT",
|
|
1557
|
-
"document_id TEXT",
|
|
1558
|
-
"user_id TEXT",
|
|
1559
|
-
"char_offset INTEGER",
|
|
1560
|
-
"page_number INTEGER"
|
|
1561
|
-
]) {
|
|
1562
|
-
try {
|
|
1563
|
-
await client.execute({
|
|
1564
|
-
sql: `ALTER TABLE memories ADD COLUMN ${column}`,
|
|
1565
|
-
args: []
|
|
1566
|
-
});
|
|
1567
|
-
} catch {
|
|
1568
|
-
}
|
|
1569
|
-
}
|
|
1570
|
-
for (const col of [
|
|
1571
|
-
"ALTER TABLE memories ADD COLUMN source_path TEXT",
|
|
1572
|
-
"ALTER TABLE memories ADD COLUMN source_type TEXT DEFAULT 'text'"
|
|
1573
|
-
]) {
|
|
1574
|
-
try {
|
|
1575
|
-
await client.execute(col);
|
|
1576
|
-
} catch {
|
|
1577
|
-
}
|
|
1578
|
-
}
|
|
1579
|
-
await client.executeMultiple(`
|
|
1580
|
-
CREATE INDEX IF NOT EXISTS idx_memories_workspace
|
|
1581
|
-
ON memories(workspace_id);
|
|
1582
|
-
|
|
1583
|
-
CREATE INDEX IF NOT EXISTS idx_memories_document
|
|
1584
|
-
ON memories(document_id);
|
|
1585
|
-
|
|
1586
|
-
CREATE INDEX IF NOT EXISTS idx_memories_user
|
|
1587
|
-
ON memories(user_id);
|
|
1588
|
-
`);
|
|
1589
|
-
await client.executeMultiple(`
|
|
1590
|
-
CREATE TABLE IF NOT EXISTS session_kills (
|
|
1591
|
-
id TEXT PRIMARY KEY,
|
|
1592
|
-
session_name TEXT NOT NULL,
|
|
1593
|
-
agent_id TEXT NOT NULL,
|
|
1594
|
-
killed_at TIMESTAMP NOT NULL,
|
|
1595
|
-
reason TEXT NOT NULL,
|
|
1596
|
-
ticks_idle INTEGER,
|
|
1597
|
-
estimated_tokens_saved INTEGER
|
|
1598
|
-
);
|
|
1599
|
-
|
|
1600
|
-
CREATE INDEX IF NOT EXISTS idx_session_kills_killed_at
|
|
1601
|
-
ON session_kills(killed_at);
|
|
1602
|
-
|
|
1603
|
-
CREATE INDEX IF NOT EXISTS idx_session_kills_agent
|
|
1604
|
-
ON session_kills(agent_id);
|
|
1605
|
-
`);
|
|
1606
|
-
await client.executeMultiple(`
|
|
1607
|
-
CREATE TABLE IF NOT EXISTS conversations (
|
|
1608
|
-
id TEXT PRIMARY KEY,
|
|
1609
|
-
platform TEXT NOT NULL,
|
|
1610
|
-
external_id TEXT,
|
|
1611
|
-
sender_id TEXT NOT NULL,
|
|
1612
|
-
sender_name TEXT,
|
|
1613
|
-
sender_phone TEXT,
|
|
1614
|
-
sender_email TEXT,
|
|
1615
|
-
recipient_id TEXT,
|
|
1616
|
-
channel_id TEXT NOT NULL,
|
|
1617
|
-
thread_id TEXT,
|
|
1618
|
-
reply_to_id TEXT,
|
|
1619
|
-
content_text TEXT,
|
|
1620
|
-
content_media TEXT,
|
|
1621
|
-
content_metadata TEXT,
|
|
1622
|
-
agent_response TEXT,
|
|
1623
|
-
agent_name TEXT,
|
|
1624
|
-
timestamp TEXT NOT NULL,
|
|
1625
|
-
ingested_at TEXT NOT NULL
|
|
1626
|
-
);
|
|
1627
|
-
|
|
1628
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_platform
|
|
1629
|
-
ON conversations(platform);
|
|
1630
|
-
|
|
1631
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_sender
|
|
1632
|
-
ON conversations(sender_id);
|
|
1633
|
-
|
|
1634
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_timestamp
|
|
1635
|
-
ON conversations(timestamp);
|
|
1636
|
-
|
|
1637
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_thread
|
|
1638
|
-
ON conversations(thread_id);
|
|
1639
|
-
|
|
1640
|
-
CREATE INDEX IF NOT EXISTS idx_conversations_channel
|
|
1641
|
-
ON conversations(channel_id);
|
|
1642
|
-
`);
|
|
1643
|
-
try {
|
|
1644
|
-
await client.execute({
|
|
1645
|
-
sql: `ALTER TABLE tasks ADD COLUMN budget_tokens INTEGER`,
|
|
1646
|
-
args: []
|
|
1647
|
-
});
|
|
1648
|
-
} catch {
|
|
1649
|
-
}
|
|
1650
|
-
try {
|
|
1651
|
-
await client.execute({
|
|
1652
|
-
sql: `ALTER TABLE tasks ADD COLUMN budget_fallback_model TEXT`,
|
|
1653
|
-
args: []
|
|
1654
|
-
});
|
|
1655
|
-
} catch {
|
|
1656
|
-
}
|
|
1657
|
-
try {
|
|
1658
|
-
await client.execute({
|
|
1659
|
-
sql: `ALTER TABLE tasks ADD COLUMN tokens_used INTEGER DEFAULT 0`,
|
|
1660
|
-
args: []
|
|
1661
|
-
});
|
|
1662
|
-
} catch {
|
|
1663
|
-
}
|
|
1664
|
-
try {
|
|
1665
|
-
await client.execute({
|
|
1666
|
-
sql: `ALTER TABLE tasks ADD COLUMN tokens_warned_at INTEGER`,
|
|
1667
|
-
args: []
|
|
1668
|
-
});
|
|
1669
|
-
} catch {
|
|
1670
|
-
}
|
|
1671
|
-
await client.executeMultiple(`
|
|
1672
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS conversations_fts USING fts5(
|
|
1673
|
-
content_text,
|
|
1674
|
-
sender_name,
|
|
1675
|
-
agent_response,
|
|
1676
|
-
content='conversations',
|
|
1677
|
-
content_rowid='rowid'
|
|
1678
|
-
);
|
|
1679
|
-
|
|
1680
|
-
CREATE TRIGGER IF NOT EXISTS conversations_fts_ai AFTER INSERT ON conversations BEGIN
|
|
1681
|
-
INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
|
|
1682
|
-
VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
|
|
1683
|
-
END;
|
|
1684
|
-
|
|
1685
|
-
CREATE TRIGGER IF NOT EXISTS conversations_fts_ad AFTER DELETE ON conversations BEGIN
|
|
1686
|
-
INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
|
|
1687
|
-
VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
|
|
1688
|
-
END;
|
|
1689
|
-
|
|
1690
|
-
CREATE TRIGGER IF NOT EXISTS conversations_fts_au AFTER UPDATE ON conversations BEGIN
|
|
1691
|
-
INSERT INTO conversations_fts(conversations_fts, rowid, content_text, sender_name, agent_response)
|
|
1692
|
-
VALUES('delete', old.rowid, old.content_text, old.sender_name, old.agent_response);
|
|
1693
|
-
INSERT INTO conversations_fts(rowid, content_text, sender_name, agent_response)
|
|
1694
|
-
VALUES (new.rowid, new.content_text, new.sender_name, new.agent_response);
|
|
1695
|
-
END;
|
|
1696
|
-
`);
|
|
1697
|
-
try {
|
|
1698
|
-
await client.execute({
|
|
1699
|
-
sql: `ALTER TABLE memories ADD COLUMN tier INTEGER DEFAULT 3`,
|
|
1700
|
-
args: []
|
|
1701
|
-
});
|
|
1702
|
-
} catch {
|
|
1703
|
-
}
|
|
1704
|
-
try {
|
|
1705
|
-
await client.execute(
|
|
1706
|
-
`CREATE INDEX IF NOT EXISTS idx_memories_tier ON memories(tier)`
|
|
1707
|
-
);
|
|
1708
|
-
} catch {
|
|
1709
|
-
}
|
|
1710
|
-
try {
|
|
1711
|
-
await client.execute({
|
|
1712
|
-
sql: `UPDATE memories SET tier = 1 WHERE tool_name = 'commit_to_long_term_memory' AND importance >= 8 AND tier = 3`,
|
|
1713
|
-
args: []
|
|
1714
|
-
});
|
|
1715
|
-
await client.execute({
|
|
1716
|
-
sql: `UPDATE memories SET tier = 2 WHERE tool_name IN ('store_memory', 'manual') AND importance >= 5 AND tier = 3`,
|
|
1717
|
-
args: []
|
|
1718
|
-
});
|
|
1719
|
-
} catch {
|
|
1720
|
-
}
|
|
1721
|
-
try {
|
|
1722
|
-
await client.execute({
|
|
1723
|
-
sql: `ALTER TABLE memories ADD COLUMN supersedes_id TEXT`,
|
|
1724
|
-
args: []
|
|
1725
|
-
});
|
|
1726
|
-
} catch {
|
|
1727
|
-
}
|
|
1728
|
-
try {
|
|
1729
|
-
await client.execute(
|
|
1730
|
-
`CREATE INDEX IF NOT EXISTS idx_memories_supersedes ON memories(supersedes_id) WHERE supersedes_id IS NOT NULL`
|
|
1731
|
-
);
|
|
1732
|
-
} catch {
|
|
1733
|
-
}
|
|
1734
|
-
for (const col of [
|
|
1735
|
-
"ALTER TABLE tasks ADD COLUMN checkpoint TEXT",
|
|
1736
|
-
"ALTER TABLE tasks ADD COLUMN checkpoint_count INTEGER DEFAULT 0"
|
|
1737
|
-
]) {
|
|
1738
|
-
try {
|
|
1739
|
-
await client.execute(col);
|
|
1740
|
-
} catch {
|
|
1741
|
-
}
|
|
1742
|
-
}
|
|
1743
|
-
}
|
|
1744
|
-
|
|
1745
|
-
// src/lib/keychain.ts
|
|
1746
|
-
import { readFile as readFile3, writeFile as writeFile3, unlink, mkdir as mkdir3, chmod } from "fs/promises";
|
|
1747
|
-
import { existsSync as existsSync4 } from "fs";
|
|
1748
|
-
import path4 from "path";
|
|
1749
|
-
import crypto from "crypto";
|
|
1750
|
-
var SERVICE = "exe-mem";
|
|
1751
|
-
var ACCOUNT = "master-key";
|
|
1752
|
-
function getKeyDir() {
|
|
1753
|
-
return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? path4.join(process.env.HOME ?? "/tmp", ".exe-os");
|
|
1754
|
-
}
|
|
1755
|
-
function getKeyPath() {
|
|
1756
|
-
return path4.join(getKeyDir(), "master.key");
|
|
1757
|
-
}
|
|
1758
|
-
async function tryKeytar() {
|
|
1759
|
-
try {
|
|
1760
|
-
return await import("keytar");
|
|
1761
|
-
} catch {
|
|
1762
|
-
return null;
|
|
1763
|
-
}
|
|
1764
|
-
}
|
|
1765
|
-
async function getMasterKey() {
|
|
1766
|
-
const keytar = await tryKeytar();
|
|
1767
|
-
if (keytar) {
|
|
1768
|
-
try {
|
|
1769
|
-
const stored = await keytar.getPassword(SERVICE, ACCOUNT);
|
|
1770
|
-
if (stored) {
|
|
1771
|
-
return Buffer.from(stored, "base64");
|
|
1772
|
-
}
|
|
1773
|
-
} catch {
|
|
1774
|
-
}
|
|
1775
|
-
}
|
|
1776
|
-
const keyPath = getKeyPath();
|
|
1777
|
-
if (!existsSync4(keyPath)) {
|
|
1778
|
-
return null;
|
|
1779
|
-
}
|
|
1780
|
-
try {
|
|
1781
|
-
const content = await readFile3(keyPath, "utf-8");
|
|
1782
|
-
return Buffer.from(content.trim(), "base64");
|
|
1783
|
-
} catch {
|
|
1784
|
-
return null;
|
|
1785
|
-
}
|
|
1786
|
-
}
|
|
1787
|
-
|
|
1788
|
-
// src/lib/store.ts
|
|
1789
|
-
init_config();
|
|
1790
|
-
var _pendingRecords = [];
|
|
1791
|
-
var _batchSize = 20;
|
|
1792
|
-
var _flushIntervalMs = 1e4;
|
|
1793
|
-
var _flushTimer = null;
|
|
1794
|
-
var _flushing = false;
|
|
1795
|
-
var _nextVersion = 1;
|
|
1796
|
-
async function initStore(options) {
|
|
1797
|
-
if (_flushTimer !== null) {
|
|
1798
|
-
clearInterval(_flushTimer);
|
|
1799
|
-
_flushTimer = null;
|
|
1800
|
-
}
|
|
1801
|
-
_pendingRecords = [];
|
|
1802
|
-
_flushing = false;
|
|
1803
|
-
_batchSize = options?.batchSize ?? 20;
|
|
1804
|
-
_flushIntervalMs = options?.flushIntervalMs ?? 1e4;
|
|
1805
|
-
let dbPath = options?.dbPath;
|
|
1806
|
-
if (!dbPath) {
|
|
1807
|
-
const config = await loadConfig();
|
|
1808
|
-
dbPath = config.dbPath;
|
|
1809
|
-
}
|
|
1810
|
-
let masterKey = options?.masterKey ?? null;
|
|
1811
|
-
if (!masterKey) {
|
|
1812
|
-
masterKey = await getMasterKey();
|
|
1813
|
-
if (!masterKey) {
|
|
1814
|
-
throw new Error(
|
|
1815
|
-
"No encryption key found. Run /exe-setup to generate one."
|
|
1816
|
-
);
|
|
1817
|
-
}
|
|
1818
|
-
}
|
|
1819
|
-
const hexKey = masterKey.toString("hex");
|
|
1820
|
-
await initTurso({
|
|
1821
|
-
dbPath,
|
|
1822
|
-
encryptionKey: hexKey
|
|
1823
|
-
});
|
|
1824
|
-
await ensureSchema();
|
|
1825
|
-
try {
|
|
1826
|
-
const { initShardManager: initShardManager2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|
|
1827
|
-
initShardManager2(hexKey);
|
|
1828
|
-
} catch {
|
|
1829
|
-
}
|
|
1830
|
-
const client = getClient();
|
|
1831
|
-
const vResult = await client.execute("SELECT MAX(version) as max_v FROM memories");
|
|
1832
|
-
_nextVersion = (Number(vResult.rows[0]?.max_v) || 0) + 1;
|
|
1833
|
-
}
|
|
1834
|
-
function classifyTier(record) {
|
|
1835
|
-
if (record.tool_name === "commit_to_long_term_memory" && (record.importance ?? 0) >= 8) return 1;
|
|
1836
|
-
if (["store_memory", "manual"].includes(record.tool_name ?? "") && (record.importance ?? 0) >= 5) return 2;
|
|
1837
|
-
return 3;
|
|
1838
|
-
}
|
|
1839
|
-
async function writeMemory(record) {
|
|
1840
|
-
if (record.vector !== null && record.vector.length !== EMBEDDING_DIM) {
|
|
1841
|
-
throw new Error(
|
|
1842
|
-
`Expected ${EMBEDDING_DIM}-dim vector, got ${record.vector.length}`
|
|
1843
|
-
);
|
|
1844
|
-
}
|
|
1845
|
-
const dbRow = {
|
|
1846
|
-
id: record.id,
|
|
1847
|
-
agent_id: record.agent_id,
|
|
1848
|
-
agent_role: record.agent_role,
|
|
1849
|
-
session_id: record.session_id,
|
|
1850
|
-
timestamp: record.timestamp,
|
|
1851
|
-
tool_name: record.tool_name,
|
|
1852
|
-
project_name: record.project_name,
|
|
1853
|
-
has_error: record.has_error ? 1 : 0,
|
|
1854
|
-
raw_text: record.raw_text,
|
|
1855
|
-
vector: record.vector,
|
|
1856
|
-
version: _nextVersion++,
|
|
1857
|
-
task_id: record.task_id ?? null,
|
|
1858
|
-
importance: record.importance ?? 5,
|
|
1859
|
-
status: record.status ?? "active",
|
|
1860
|
-
confidence: record.confidence ?? 0.7,
|
|
1861
|
-
last_accessed: record.last_accessed ?? record.timestamp,
|
|
1862
|
-
workspace_id: record.workspace_id ?? null,
|
|
1863
|
-
document_id: record.document_id ?? null,
|
|
1864
|
-
user_id: record.user_id ?? null,
|
|
1865
|
-
char_offset: record.char_offset ?? null,
|
|
1866
|
-
page_number: record.page_number ?? null,
|
|
1867
|
-
source_path: record.source_path ?? null,
|
|
1868
|
-
source_type: record.source_type ?? null,
|
|
1869
|
-
tier: record.tier ?? classifyTier(record),
|
|
1870
|
-
supersedes_id: record.supersedes_id ?? null
|
|
1871
|
-
};
|
|
1872
|
-
_pendingRecords.push(dbRow);
|
|
1873
|
-
if (_flushTimer === null) {
|
|
1874
|
-
_flushTimer = setInterval(() => {
|
|
1875
|
-
void flushBatch();
|
|
1876
|
-
}, _flushIntervalMs);
|
|
1877
|
-
if (_flushTimer && typeof _flushTimer === "object" && "unref" in _flushTimer) {
|
|
1878
|
-
_flushTimer.unref();
|
|
1879
|
-
}
|
|
1880
|
-
}
|
|
1881
|
-
if (_pendingRecords.length >= _batchSize) {
|
|
1882
|
-
await flushBatch();
|
|
1883
|
-
}
|
|
1884
|
-
}
|
|
1885
|
-
async function flushBatch() {
|
|
1886
|
-
if (_flushing || _pendingRecords.length === 0) return 0;
|
|
1887
|
-
_flushing = true;
|
|
1888
|
-
try {
|
|
1889
|
-
const batch = _pendingRecords.slice(0);
|
|
1890
|
-
const buildStmt = (row) => {
|
|
1891
|
-
const hasVector = row.vector !== null;
|
|
1892
|
-
const taskId = row.task_id ?? null;
|
|
1893
|
-
const importance = row.importance ?? 5;
|
|
1894
|
-
const status = row.status ?? "active";
|
|
1895
|
-
const confidence = row.confidence ?? 0.7;
|
|
1896
|
-
const lastAccessed = row.last_accessed ?? row.timestamp;
|
|
1897
|
-
const workspaceId = row.workspace_id ?? null;
|
|
1898
|
-
const documentId = row.document_id ?? null;
|
|
1899
|
-
const userId = row.user_id ?? null;
|
|
1900
|
-
const charOffset = row.char_offset ?? null;
|
|
1901
|
-
const pageNumber = row.page_number ?? null;
|
|
1902
|
-
const sourcePath = row.source_path ?? null;
|
|
1903
|
-
const sourceType = row.source_type ?? null;
|
|
1904
|
-
const tier = row.tier ?? 3;
|
|
1905
|
-
const supersedesId = row.supersedes_id ?? null;
|
|
1906
|
-
return {
|
|
1907
|
-
sql: hasVector ? `INSERT OR IGNORE INTO memories
|
|
1908
|
-
(id, agent_id, agent_role, session_id, timestamp,
|
|
1909
|
-
tool_name, project_name,
|
|
1910
|
-
has_error, raw_text, vector, version, task_id, importance, status,
|
|
1911
|
-
confidence, last_accessed,
|
|
1912
|
-
workspace_id, document_id, user_id, char_offset, page_number,
|
|
1913
|
-
source_path, source_type, tier, supersedes_id)
|
|
1914
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, vector32(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` : `INSERT OR IGNORE INTO memories
|
|
1915
|
-
(id, agent_id, agent_role, session_id, timestamp,
|
|
1916
|
-
tool_name, project_name,
|
|
1917
|
-
has_error, raw_text, vector, version, task_id, importance, status,
|
|
1918
|
-
confidence, last_accessed,
|
|
1919
|
-
workspace_id, document_id, user_id, char_offset, page_number,
|
|
1920
|
-
source_path, source_type, tier, supersedes_id)
|
|
1921
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
1922
|
-
args: hasVector ? [
|
|
1923
|
-
row.id,
|
|
1924
|
-
row.agent_id,
|
|
1925
|
-
row.agent_role,
|
|
1926
|
-
row.session_id,
|
|
1927
|
-
row.timestamp,
|
|
1928
|
-
row.tool_name,
|
|
1929
|
-
row.project_name,
|
|
1930
|
-
row.has_error,
|
|
1931
|
-
row.raw_text,
|
|
1932
|
-
vectorToBlob(row.vector),
|
|
1933
|
-
row.version,
|
|
1934
|
-
taskId,
|
|
1935
|
-
importance,
|
|
1936
|
-
status,
|
|
1937
|
-
confidence,
|
|
1938
|
-
lastAccessed,
|
|
1939
|
-
workspaceId,
|
|
1940
|
-
documentId,
|
|
1941
|
-
userId,
|
|
1942
|
-
charOffset,
|
|
1943
|
-
pageNumber,
|
|
1944
|
-
sourcePath,
|
|
1945
|
-
sourceType,
|
|
1946
|
-
tier,
|
|
1947
|
-
supersedesId
|
|
1948
|
-
] : [
|
|
1949
|
-
row.id,
|
|
1950
|
-
row.agent_id,
|
|
1951
|
-
row.agent_role,
|
|
1952
|
-
row.session_id,
|
|
1953
|
-
row.timestamp,
|
|
1954
|
-
row.tool_name,
|
|
1955
|
-
row.project_name,
|
|
1956
|
-
row.has_error,
|
|
1957
|
-
row.raw_text,
|
|
1958
|
-
row.version,
|
|
1959
|
-
taskId,
|
|
1960
|
-
importance,
|
|
1961
|
-
status,
|
|
1962
|
-
confidence,
|
|
1963
|
-
lastAccessed,
|
|
1964
|
-
workspaceId,
|
|
1965
|
-
documentId,
|
|
1966
|
-
userId,
|
|
1967
|
-
charOffset,
|
|
1968
|
-
pageNumber,
|
|
1969
|
-
sourcePath,
|
|
1970
|
-
sourceType,
|
|
1971
|
-
tier,
|
|
1972
|
-
supersedesId
|
|
1973
|
-
]
|
|
1974
|
-
};
|
|
1975
|
-
};
|
|
1976
|
-
const globalClient = getClient();
|
|
1977
|
-
const globalStmts = batch.map(buildStmt);
|
|
1978
|
-
await globalClient.batch(globalStmts, "write");
|
|
1979
|
-
_pendingRecords.splice(0, batch.length);
|
|
1980
|
-
try {
|
|
1981
|
-
const { isShardingEnabled: isShardingEnabled2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|
|
1982
|
-
if (isShardingEnabled2()) {
|
|
1983
|
-
const byProject = /* @__PURE__ */ new Map();
|
|
1984
|
-
for (const row of batch) {
|
|
1985
|
-
const proj = row.project_name || "unknown";
|
|
1986
|
-
if (!byProject.has(proj)) byProject.set(proj, []);
|
|
1987
|
-
byProject.get(proj).push(row);
|
|
1988
|
-
}
|
|
1989
|
-
for (const [project, rows] of byProject) {
|
|
1990
|
-
try {
|
|
1991
|
-
const shardClient = await getReadyShardClient2(project);
|
|
1992
|
-
const shardStmts = rows.map(buildStmt);
|
|
1993
|
-
await shardClient.batch(shardStmts, "write");
|
|
1994
|
-
} catch (err) {
|
|
1995
|
-
process.stderr.write(
|
|
1996
|
-
`[store] Shard write failed for ${project}: ${err instanceof Error ? err.message : String(err)}
|
|
1997
|
-
`
|
|
1998
|
-
);
|
|
1999
|
-
}
|
|
2000
|
-
}
|
|
2001
|
-
}
|
|
2002
|
-
} catch {
|
|
2003
|
-
}
|
|
2004
|
-
return batch.length;
|
|
2005
|
-
} finally {
|
|
2006
|
-
_flushing = false;
|
|
2007
|
-
}
|
|
2008
|
-
}
|
|
2009
|
-
function buildWikiScopeFilter(options, columnPrefix) {
|
|
2010
|
-
const args = [];
|
|
2011
|
-
let clause = "";
|
|
2012
|
-
if (options?.workspaceId !== void 0) {
|
|
2013
|
-
clause += ` AND ${columnPrefix}workspace_id = ?`;
|
|
2014
|
-
args.push(options.workspaceId);
|
|
2015
|
-
}
|
|
2016
|
-
if (options?.userId === void 0) {
|
|
2017
|
-
clause += ` AND ${columnPrefix}user_id IS NULL`;
|
|
2018
|
-
} else if (options.userId === null) {
|
|
2019
|
-
clause += ` AND ${columnPrefix}user_id IS NULL`;
|
|
2020
|
-
} else {
|
|
2021
|
-
clause += ` AND (${columnPrefix}user_id = ? OR ${columnPrefix}user_id IS NULL)`;
|
|
2022
|
-
args.push(options.userId);
|
|
2023
|
-
}
|
|
2024
|
-
return { clause, args };
|
|
2025
|
-
}
|
|
2026
|
-
async function searchMemories(queryVector, agentId, options) {
|
|
2027
|
-
let client;
|
|
2028
|
-
try {
|
|
2029
|
-
const { isShardingEnabled: isShardingEnabled2, shardExists: shardExists2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|
|
2030
|
-
if (isShardingEnabled2() && options?.projectName && shardExists2(options.projectName)) {
|
|
2031
|
-
client = await getReadyShardClient2(options.projectName);
|
|
2032
|
-
} else {
|
|
2033
|
-
client = getClient();
|
|
2034
|
-
}
|
|
2035
|
-
} catch {
|
|
2036
|
-
client = getClient();
|
|
2037
|
-
}
|
|
2038
|
-
const limit = options?.limit ?? 10;
|
|
2039
|
-
const statusFilter = options?.includeArchived ? "" : `
|
|
2040
|
-
AND COALESCE(status, 'active') = 'active'`;
|
|
2041
|
-
let sql = `SELECT id, agent_id, agent_role, session_id, timestamp,
|
|
2042
|
-
tool_name, project_name,
|
|
2043
|
-
has_error, raw_text, vector, importance, status,
|
|
2044
|
-
confidence, last_accessed,
|
|
2045
|
-
workspace_id, document_id, user_id,
|
|
2046
|
-
char_offset, page_number,
|
|
2047
|
-
source_path, source_type
|
|
2048
|
-
FROM memories
|
|
2049
|
-
WHERE agent_id = ?
|
|
2050
|
-
AND vector IS NOT NULL${statusFilter}
|
|
2051
|
-
AND COALESCE(confidence, 0.7) >= 0.3`;
|
|
2052
|
-
const args = [agentId];
|
|
2053
|
-
const scope = buildWikiScopeFilter(options, "");
|
|
2054
|
-
sql += scope.clause;
|
|
2055
|
-
args.push(...scope.args);
|
|
2056
|
-
if (options?.projectName) {
|
|
2057
|
-
sql += ` AND project_name = ?`;
|
|
2058
|
-
args.push(options.projectName);
|
|
2059
|
-
}
|
|
2060
|
-
if (options?.toolName) {
|
|
2061
|
-
sql += ` AND tool_name = ?`;
|
|
2062
|
-
args.push(options.toolName);
|
|
2063
|
-
}
|
|
2064
|
-
if (options?.hasError !== void 0) {
|
|
2065
|
-
sql += ` AND has_error = ?`;
|
|
2066
|
-
args.push(options.hasError ? 1 : 0);
|
|
2067
|
-
}
|
|
2068
|
-
if (options?.since) {
|
|
2069
|
-
sql += ` AND timestamp >= ?`;
|
|
2070
|
-
args.push(options.since);
|
|
2071
|
-
}
|
|
2072
|
-
sql += ` ORDER BY vector_distance_cos(vector, vector32(?))`;
|
|
2073
|
-
args.push(vectorToBlob(queryVector));
|
|
2074
|
-
sql += ` LIMIT ?`;
|
|
2075
|
-
args.push(limit);
|
|
2076
|
-
const result = await client.execute({ sql, args });
|
|
2077
|
-
return result.rows.map((row) => ({
|
|
2078
|
-
id: row.id,
|
|
2079
|
-
agent_id: row.agent_id,
|
|
2080
|
-
agent_role: row.agent_role,
|
|
2081
|
-
session_id: row.session_id,
|
|
2082
|
-
timestamp: row.timestamp,
|
|
2083
|
-
tool_name: row.tool_name,
|
|
2084
|
-
project_name: row.project_name,
|
|
2085
|
-
has_error: row.has_error === 1,
|
|
2086
|
-
raw_text: row.raw_text,
|
|
2087
|
-
vector: row.vector == null ? [] : Array.isArray(row.vector) ? row.vector : Array.from(row.vector),
|
|
2088
|
-
importance: row.importance ?? 5,
|
|
2089
|
-
status: row.status ?? "active",
|
|
2090
|
-
confidence: row.confidence ?? 0.7,
|
|
2091
|
-
last_accessed: row.last_accessed ?? row.timestamp,
|
|
2092
|
-
workspace_id: row.workspace_id ?? null,
|
|
2093
|
-
document_id: row.document_id ?? null,
|
|
2094
|
-
user_id: row.user_id ?? null,
|
|
2095
|
-
char_offset: row.char_offset ?? null,
|
|
2096
|
-
page_number: row.page_number ?? null,
|
|
2097
|
-
source_path: row.source_path ?? null,
|
|
2098
|
-
source_type: row.source_type ?? null
|
|
2099
|
-
}));
|
|
2100
|
-
}
|
|
2101
|
-
function vectorToBlob(vector) {
|
|
2102
|
-
const f32 = vector instanceof Float32Array ? vector : new Float32Array(vector);
|
|
2103
|
-
return JSON.stringify(Array.from(f32));
|
|
2104
|
-
}
|
|
2105
|
-
|
|
2106
|
-
// src/lib/is-main.ts
|
|
2107
|
-
import { realpathSync } from "fs";
|
|
2108
|
-
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
2109
|
-
function isMainModule(importMetaUrl) {
|
|
2110
|
-
if (process.argv[1] == null) return false;
|
|
2111
|
-
try {
|
|
2112
|
-
const scriptPath = realpathSync(process.argv[1]);
|
|
2113
|
-
const modulePath = realpathSync(fileURLToPath2(importMetaUrl));
|
|
2114
|
-
return scriptPath === modulePath;
|
|
2115
|
-
} catch {
|
|
2116
|
-
return importMetaUrl === `file://${process.argv[1]}` || importMetaUrl === new URL(process.argv[1], "file://").href;
|
|
2117
|
-
}
|
|
2118
|
-
}
|
|
2119
|
-
|
|
2120
|
-
// src/bin/exe-assign.ts
|
|
2121
|
-
async function main() {
|
|
2122
|
-
const args = process.argv.slice(2);
|
|
2123
|
-
if (args.length === 0) {
|
|
2124
|
-
console.error(
|
|
2125
|
-
'Usage:\n exe-assign "task description" (auto-route)\n exe-assign <employee> "task description" (direct)'
|
|
2126
|
-
);
|
|
2127
|
-
process.exit(1);
|
|
2128
|
-
}
|
|
2129
|
-
const employees = await loadEmployees();
|
|
2130
|
-
if (employees.length === 0) {
|
|
2131
|
-
console.error(
|
|
2132
|
-
"No employees registered. Run /exe-new-employee to create one."
|
|
2133
|
-
);
|
|
2134
|
-
process.exit(1);
|
|
2135
|
-
}
|
|
2136
|
-
const possibleName = args[0] ?? "";
|
|
2137
|
-
const matchedEmployee = getEmployee(employees, possibleName);
|
|
2138
|
-
let task;
|
|
2139
|
-
let mode;
|
|
2140
|
-
let assignedEmployee;
|
|
2141
|
-
let score;
|
|
2142
|
-
if (matchedEmployee && args.length >= 2) {
|
|
2143
|
-
mode = "direct";
|
|
2144
|
-
task = args.slice(1).join(" ");
|
|
2145
|
-
assignedEmployee = matchedEmployee;
|
|
2146
|
-
score = 1;
|
|
2147
|
-
} else {
|
|
2148
|
-
mode = "auto";
|
|
2149
|
-
task = args.join(" ");
|
|
2150
|
-
await initStore();
|
|
2151
|
-
const result = await routeTask(task, employees, embed, searchMemories);
|
|
2152
|
-
assignedEmployee = result.employee;
|
|
2153
|
-
score = result.score;
|
|
2154
|
-
}
|
|
2155
|
-
if (!task) {
|
|
2156
|
-
console.error("Error: Task description is required.");
|
|
2157
|
-
process.exit(1);
|
|
2158
|
-
}
|
|
2159
|
-
if (mode === "direct") {
|
|
2160
|
-
await initStore();
|
|
2161
|
-
}
|
|
2162
|
-
const memory = createAssignmentMemory(task, assignedEmployee, "founder");
|
|
2163
|
-
memory.vector = await embed(task);
|
|
2164
|
-
await writeMemory(memory);
|
|
2165
|
-
await flushBatch();
|
|
2166
|
-
console.log(formatAssignmentOutput(assignedEmployee, task, score, mode));
|
|
2167
|
-
}
|
|
2168
|
-
if (isMainModule(import.meta.url)) {
|
|
2169
|
-
main().catch((err) => {
|
|
2170
|
-
console.error(err instanceof Error ? err.message : String(err));
|
|
2171
|
-
process.exit(1);
|
|
2172
|
-
});
|
|
2173
|
-
}
|
|
2174
|
-
export {
|
|
2175
|
-
main
|
|
2176
|
-
};
|