@askexenow/exe-os 0.9.16 → 0.9.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/backfill-conversations.js +1242 -909
- package/dist/bin/backfill-responses.js +1245 -912
- package/dist/bin/backfill-vectors.js +1244 -906
- package/dist/bin/cleanup-stale-review-tasks.js +1870 -417
- package/dist/bin/cli.js +217 -107
- package/dist/bin/exe-agent-config.js +2 -2
- package/dist/bin/exe-agent.js +62 -0
- package/dist/bin/exe-assign.js +346 -10
- package/dist/bin/exe-boot.js +387 -32
- package/dist/bin/exe-call.js +72 -2
- package/dist/bin/exe-cloud.js +8 -0
- package/dist/bin/exe-dispatch.js +1821 -225
- package/dist/bin/exe-doctor.js +720 -52
- package/dist/bin/exe-export-behaviors.js +1429 -148
- package/dist/bin/exe-forget.js +1408 -34
- package/dist/bin/exe-gateway.js +1629 -1295
- package/dist/bin/exe-heartbeat.js +1899 -448
- package/dist/bin/exe-kill.js +1624 -346
- package/dist/bin/exe-launch-agent.js +726 -90
- package/dist/bin/exe-link.js +27 -8
- package/dist/bin/exe-new-employee.js +75 -9
- package/dist/bin/exe-pending-messages.js +2769 -1316
- package/dist/bin/exe-pending-notifications.js +2829 -1376
- package/dist/bin/exe-pending-reviews.js +2847 -1392
- package/dist/bin/exe-rename.js +89 -8
- package/dist/bin/exe-review.js +1494 -312
- package/dist/bin/exe-search.js +1608 -1300
- package/dist/bin/exe-session-cleanup.js +194 -91
- package/dist/bin/exe-settings.js +10 -2
- package/dist/bin/exe-start-codex.js +769 -120
- package/dist/bin/exe-start-opencode.js +763 -108
- package/dist/bin/exe-status.js +1887 -434
- package/dist/bin/exe-team.js +1782 -324
- package/dist/bin/git-sweep.js +408 -33
- package/dist/bin/graph-backfill.js +681 -27
- package/dist/bin/graph-export.js +1419 -141
- package/dist/bin/install.js +4 -8
- package/dist/bin/intercom-check.js +8641 -0
- package/dist/bin/scan-tasks.js +553 -38
- package/dist/bin/setup.js +82 -10
- package/dist/bin/shard-migrate.js +682 -28
- package/dist/gateway/index.js +1629 -1295
- package/dist/hooks/bug-report-worker.js +1136 -183
- package/dist/hooks/codex-stop-task-finalizer.js +1060 -107
- package/dist/hooks/commit-complete.js +408 -33
- package/dist/hooks/error-recall.js +1608 -1300
- package/dist/hooks/ingest-worker.js +250 -7966
- package/dist/hooks/ingest.js +707 -119
- package/dist/hooks/instructions-loaded.js +383 -20
- package/dist/hooks/notification.js +383 -20
- package/dist/hooks/post-compact.js +384 -21
- package/dist/hooks/{response-ingest-worker.js → post-tool-combined.js} +4157 -1716
- package/dist/hooks/pre-compact.js +486 -45
- package/dist/hooks/pre-tool-use.js +385 -22
- package/dist/hooks/prompt-submit.js +291 -96
- package/dist/hooks/session-end.js +490 -48
- package/dist/hooks/session-start.js +236 -22
- package/dist/hooks/stop.js +192 -50
- package/dist/hooks/subagent-stop.js +95 -18
- package/dist/hooks/summary-worker.js +205 -361
- package/dist/index.js +221 -105
- package/dist/lib/agent-config.js +2 -2
- package/dist/lib/cloud-sync.js +27 -8
- package/dist/lib/consolidation.js +437 -41
- package/dist/lib/database.js +20 -8
- package/dist/lib/db-daemon-client.js +2 -2
- package/dist/lib/db.js +20 -8
- package/dist/lib/device-registry.js +27 -8
- package/dist/lib/employee-templates.js +62 -0
- package/dist/lib/employees.js +2 -2
- package/dist/lib/exe-daemon.js +6703 -6259
- package/dist/lib/hybrid-search.js +1608 -1300
- package/dist/lib/identity.js +1 -1
- package/dist/lib/messaging.js +11 -3
- package/dist/lib/reminders.js +1 -1
- package/dist/lib/schedules.js +718 -26
- package/dist/lib/session-registry.js +11 -0
- package/dist/lib/skill-learning.js +639 -9
- package/dist/lib/store.js +676 -27
- package/dist/lib/tasks.js +665 -27
- package/dist/lib/tmux-routing.js +732 -94
- package/dist/lib/token-spend.js +1 -1
- package/dist/mcp/server.js +856 -383
- package/dist/mcp/tools/complete-reminder.js +1 -1
- package/dist/mcp/tools/create-reminder.js +1 -1
- package/dist/mcp/tools/create-task.js +616 -46
- package/dist/mcp/tools/deactivate-behavior.js +9 -1
- package/dist/mcp/tools/list-reminders.js +1 -1
- package/dist/mcp/tools/list-tasks.js +10 -2
- package/dist/mcp/tools/send-message.js +11 -3
- package/dist/mcp/tools/update-task.js +677 -39
- package/dist/runtime/index.js +425 -37
- package/dist/tui/App.js +365 -34
- package/package.json +5 -2
- package/src/commands/exe/intercom.md +6 -17
- package/dist/bin/wiki-sync.js +0 -2991
- package/dist/hooks/prompt-ingest-worker.js +0 -3979
package/dist/bin/graph-export.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
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
|
+
});
|
|
4
10
|
var __esm = (fn, res) => function __init() {
|
|
5
11
|
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
6
12
|
};
|
|
@@ -9,6 +15,15 @@ var __export = (target, all) => {
|
|
|
9
15
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
16
|
};
|
|
11
17
|
|
|
18
|
+
// src/types/memory.ts
|
|
19
|
+
var EMBEDDING_DIM;
|
|
20
|
+
var init_memory = __esm({
|
|
21
|
+
"src/types/memory.ts"() {
|
|
22
|
+
"use strict";
|
|
23
|
+
EMBEDDING_DIM = 1024;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
12
27
|
// src/lib/db-retry.ts
|
|
13
28
|
function isBusyError(err) {
|
|
14
29
|
if (err instanceof Error) {
|
|
@@ -58,9 +73,9 @@ var MAX_RETRIES, BASE_DELAY_MS, MAX_JITTER_MS;
|
|
|
58
73
|
var init_db_retry = __esm({
|
|
59
74
|
"src/lib/db-retry.ts"() {
|
|
60
75
|
"use strict";
|
|
61
|
-
MAX_RETRIES =
|
|
62
|
-
BASE_DELAY_MS =
|
|
63
|
-
MAX_JITTER_MS =
|
|
76
|
+
MAX_RETRIES = 5;
|
|
77
|
+
BASE_DELAY_MS = 250;
|
|
78
|
+
MAX_JITTER_MS = 400;
|
|
64
79
|
}
|
|
65
80
|
});
|
|
66
81
|
|
|
@@ -74,12 +89,25 @@ async function ensurePrivateDir(dirPath) {
|
|
|
74
89
|
} catch {
|
|
75
90
|
}
|
|
76
91
|
}
|
|
92
|
+
function ensurePrivateDirSync(dirPath) {
|
|
93
|
+
mkdirSync(dirPath, { recursive: true, mode: PRIVATE_DIR_MODE });
|
|
94
|
+
try {
|
|
95
|
+
chmodSync(dirPath, PRIVATE_DIR_MODE);
|
|
96
|
+
} catch {
|
|
97
|
+
}
|
|
98
|
+
}
|
|
77
99
|
async function enforcePrivateFile(filePath) {
|
|
78
100
|
try {
|
|
79
101
|
await chmod(filePath, PRIVATE_FILE_MODE);
|
|
80
102
|
} catch {
|
|
81
103
|
}
|
|
82
104
|
}
|
|
105
|
+
function enforcePrivateFileSync(filePath) {
|
|
106
|
+
try {
|
|
107
|
+
if (existsSync(filePath)) chmodSync(filePath, PRIVATE_FILE_MODE);
|
|
108
|
+
} catch {
|
|
109
|
+
}
|
|
110
|
+
}
|
|
83
111
|
var PRIVATE_DIR_MODE, PRIVATE_FILE_MODE;
|
|
84
112
|
var init_secure_files = __esm({
|
|
85
113
|
"src/lib/secure-files.ts"() {
|
|
@@ -896,7 +924,753 @@ var init_database_adapter = __esm({
|
|
|
896
924
|
}
|
|
897
925
|
});
|
|
898
926
|
|
|
927
|
+
// src/lib/daemon-auth.ts
|
|
928
|
+
import crypto from "crypto";
|
|
929
|
+
import path4 from "path";
|
|
930
|
+
import { existsSync as existsSync4, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
931
|
+
function normalizeToken(token) {
|
|
932
|
+
if (!token) return null;
|
|
933
|
+
const trimmed = token.trim();
|
|
934
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
935
|
+
}
|
|
936
|
+
function readDaemonToken() {
|
|
937
|
+
try {
|
|
938
|
+
if (!existsSync4(DAEMON_TOKEN_PATH)) return null;
|
|
939
|
+
return normalizeToken(readFileSync3(DAEMON_TOKEN_PATH, "utf8"));
|
|
940
|
+
} catch {
|
|
941
|
+
return null;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
function ensureDaemonToken(seed) {
|
|
945
|
+
const existing = readDaemonToken();
|
|
946
|
+
if (existing) return existing;
|
|
947
|
+
const token = normalizeToken(seed) ?? crypto.randomBytes(32).toString("hex");
|
|
948
|
+
ensurePrivateDirSync(EXE_AI_DIR);
|
|
949
|
+
writeFileSync2(DAEMON_TOKEN_PATH, `${token}
|
|
950
|
+
`, "utf8");
|
|
951
|
+
enforcePrivateFileSync(DAEMON_TOKEN_PATH);
|
|
952
|
+
return token;
|
|
953
|
+
}
|
|
954
|
+
var DAEMON_TOKEN_PATH;
|
|
955
|
+
var init_daemon_auth = __esm({
|
|
956
|
+
"src/lib/daemon-auth.ts"() {
|
|
957
|
+
"use strict";
|
|
958
|
+
init_config();
|
|
959
|
+
init_secure_files();
|
|
960
|
+
DAEMON_TOKEN_PATH = path4.join(EXE_AI_DIR, "exed.token");
|
|
961
|
+
}
|
|
962
|
+
});
|
|
963
|
+
|
|
964
|
+
// src/lib/exe-daemon-client.ts
|
|
965
|
+
var exe_daemon_client_exports = {};
|
|
966
|
+
__export(exe_daemon_client_exports, {
|
|
967
|
+
connectEmbedDaemon: () => connectEmbedDaemon,
|
|
968
|
+
disconnectClient: () => disconnectClient,
|
|
969
|
+
embedBatchViaClient: () => embedBatchViaClient,
|
|
970
|
+
embedViaClient: () => embedViaClient,
|
|
971
|
+
isClientConnected: () => isClientConnected,
|
|
972
|
+
pingDaemon: () => pingDaemon,
|
|
973
|
+
sendDaemonRequest: () => sendDaemonRequest,
|
|
974
|
+
sendIngestRequest: () => sendIngestRequest
|
|
975
|
+
});
|
|
976
|
+
import net from "net";
|
|
977
|
+
import os4 from "os";
|
|
978
|
+
import { spawn } from "child_process";
|
|
979
|
+
import { randomUUID } from "crypto";
|
|
980
|
+
import { existsSync as existsSync5, unlinkSync as unlinkSync2, readFileSync as readFileSync4, openSync, closeSync, statSync } from "fs";
|
|
981
|
+
import path5 from "path";
|
|
982
|
+
import { fileURLToPath } from "url";
|
|
983
|
+
function handleData(chunk) {
|
|
984
|
+
_buffer += chunk.toString();
|
|
985
|
+
if (_buffer.length > MAX_BUFFER) {
|
|
986
|
+
_buffer = "";
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
let newlineIdx;
|
|
990
|
+
while ((newlineIdx = _buffer.indexOf("\n")) !== -1) {
|
|
991
|
+
const line = _buffer.slice(0, newlineIdx).trim();
|
|
992
|
+
_buffer = _buffer.slice(newlineIdx + 1);
|
|
993
|
+
if (!line) continue;
|
|
994
|
+
try {
|
|
995
|
+
const response = JSON.parse(line);
|
|
996
|
+
const id = response.id;
|
|
997
|
+
if (!id) continue;
|
|
998
|
+
const entry = _pending.get(id);
|
|
999
|
+
if (entry) {
|
|
1000
|
+
clearTimeout(entry.timer);
|
|
1001
|
+
_pending.delete(id);
|
|
1002
|
+
entry.resolve(response);
|
|
1003
|
+
}
|
|
1004
|
+
} catch {
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
function cleanupStaleFiles() {
|
|
1009
|
+
if (existsSync5(PID_PATH)) {
|
|
1010
|
+
try {
|
|
1011
|
+
const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
|
|
1012
|
+
if (pid > 0) {
|
|
1013
|
+
try {
|
|
1014
|
+
process.kill(pid, 0);
|
|
1015
|
+
return;
|
|
1016
|
+
} catch {
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
} catch {
|
|
1020
|
+
}
|
|
1021
|
+
try {
|
|
1022
|
+
unlinkSync2(PID_PATH);
|
|
1023
|
+
} catch {
|
|
1024
|
+
}
|
|
1025
|
+
try {
|
|
1026
|
+
unlinkSync2(SOCKET_PATH);
|
|
1027
|
+
} catch {
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
function findPackageRoot() {
|
|
1032
|
+
let dir = path5.dirname(fileURLToPath(import.meta.url));
|
|
1033
|
+
const { root } = path5.parse(dir);
|
|
1034
|
+
while (dir !== root) {
|
|
1035
|
+
if (existsSync5(path5.join(dir, "package.json"))) return dir;
|
|
1036
|
+
dir = path5.dirname(dir);
|
|
1037
|
+
}
|
|
1038
|
+
return null;
|
|
1039
|
+
}
|
|
1040
|
+
function getAvailableMemoryGB() {
|
|
1041
|
+
if (process.platform === "darwin") {
|
|
1042
|
+
try {
|
|
1043
|
+
const { execSync: execSync2 } = __require("child_process");
|
|
1044
|
+
const vmstat = execSync2("vm_stat", { encoding: "utf8" });
|
|
1045
|
+
const pageSize = 16384;
|
|
1046
|
+
const pageSizeMatch = vmstat.match(/page size of (\d+) bytes/);
|
|
1047
|
+
const actualPageSize = pageSizeMatch ? parseInt(pageSizeMatch[1], 10) : pageSize;
|
|
1048
|
+
const free = vmstat.match(/Pages free:\s+(\d+)/);
|
|
1049
|
+
const inactive = vmstat.match(/Pages inactive:\s+(\d+)/);
|
|
1050
|
+
const speculative = vmstat.match(/Pages speculative:\s+(\d+)/);
|
|
1051
|
+
const freePages = free ? parseInt(free[1], 10) : 0;
|
|
1052
|
+
const inactivePages = inactive ? parseInt(inactive[1], 10) : 0;
|
|
1053
|
+
const speculativePages = speculative ? parseInt(speculative[1], 10) : 0;
|
|
1054
|
+
return (freePages + inactivePages + speculativePages) * actualPageSize / (1024 * 1024 * 1024);
|
|
1055
|
+
} catch {
|
|
1056
|
+
return os4.freemem() / (1024 * 1024 * 1024);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
return os4.freemem() / (1024 * 1024 * 1024);
|
|
1060
|
+
}
|
|
1061
|
+
function spawnDaemon() {
|
|
1062
|
+
const freeGB = getAvailableMemoryGB();
|
|
1063
|
+
const totalGB = os4.totalmem() / (1024 * 1024 * 1024);
|
|
1064
|
+
if (totalGB <= 8) {
|
|
1065
|
+
process.stderr.write(
|
|
1066
|
+
`[exed-client] SKIP: ${totalGB.toFixed(0)}GB system \u2014 embedding daemon disabled. Using keyword search only. Minimum 16GB recommended for vector search.
|
|
1067
|
+
`
|
|
1068
|
+
);
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
if (totalGB <= 16 && freeGB < 2) {
|
|
1072
|
+
process.stderr.write(
|
|
1073
|
+
`[exed-client] SKIP: low memory (${freeGB.toFixed(1)}GB available / ${totalGB.toFixed(0)}GB total). Embedding daemon not started \u2014 using keyword search only.
|
|
1074
|
+
`
|
|
1075
|
+
);
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
const pkgRoot = findPackageRoot();
|
|
1079
|
+
if (!pkgRoot) {
|
|
1080
|
+
process.stderr.write("[exed-client] WARN: cannot find package root\n");
|
|
1081
|
+
return;
|
|
1082
|
+
}
|
|
1083
|
+
const daemonPath = path5.join(pkgRoot, "dist", "lib", "exe-daemon.js");
|
|
1084
|
+
if (!existsSync5(daemonPath)) {
|
|
1085
|
+
process.stderr.write(`[exed-client] WARN: daemon script not found at ${daemonPath}
|
|
1086
|
+
`);
|
|
1087
|
+
return;
|
|
1088
|
+
}
|
|
1089
|
+
const resolvedPath = daemonPath;
|
|
1090
|
+
const daemonToken = ensureDaemonToken(process.env[DAEMON_TOKEN_ENV] ?? null);
|
|
1091
|
+
process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
|
|
1092
|
+
`);
|
|
1093
|
+
const logPath = path5.join(path5.dirname(SOCKET_PATH), "exed.log");
|
|
1094
|
+
let stderrFd = "ignore";
|
|
1095
|
+
try {
|
|
1096
|
+
stderrFd = openSync(logPath, "a");
|
|
1097
|
+
} catch {
|
|
1098
|
+
}
|
|
1099
|
+
const heapCapMB = totalGB <= 8 ? 256 : 512;
|
|
1100
|
+
const nodeArgs = [`--max-old-space-size=${heapCapMB}`, resolvedPath];
|
|
1101
|
+
const child = spawn(process.execPath, nodeArgs, {
|
|
1102
|
+
detached: true,
|
|
1103
|
+
stdio: ["ignore", "ignore", stderrFd],
|
|
1104
|
+
env: {
|
|
1105
|
+
...process.env,
|
|
1106
|
+
TMUX: void 0,
|
|
1107
|
+
// Daemon is global — must not inherit session scope
|
|
1108
|
+
TMUX_PANE: void 0,
|
|
1109
|
+
// Prevents resolveExeSession() from scoping to one session
|
|
1110
|
+
EXE_DAEMON_SOCK: SOCKET_PATH,
|
|
1111
|
+
EXE_DAEMON_PID: PID_PATH,
|
|
1112
|
+
[DAEMON_TOKEN_ENV]: daemonToken
|
|
1113
|
+
}
|
|
1114
|
+
});
|
|
1115
|
+
child.unref();
|
|
1116
|
+
if (typeof stderrFd === "number") {
|
|
1117
|
+
try {
|
|
1118
|
+
closeSync(stderrFd);
|
|
1119
|
+
} catch {
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
function acquireSpawnLock() {
|
|
1124
|
+
try {
|
|
1125
|
+
const fd = openSync(SPAWN_LOCK_PATH, "wx");
|
|
1126
|
+
closeSync(fd);
|
|
1127
|
+
return true;
|
|
1128
|
+
} catch {
|
|
1129
|
+
try {
|
|
1130
|
+
const stat = statSync(SPAWN_LOCK_PATH);
|
|
1131
|
+
if (Date.now() - stat.mtimeMs > SPAWN_LOCK_STALE_MS) {
|
|
1132
|
+
try {
|
|
1133
|
+
unlinkSync2(SPAWN_LOCK_PATH);
|
|
1134
|
+
} catch {
|
|
1135
|
+
}
|
|
1136
|
+
try {
|
|
1137
|
+
const fd = openSync(SPAWN_LOCK_PATH, "wx");
|
|
1138
|
+
closeSync(fd);
|
|
1139
|
+
return true;
|
|
1140
|
+
} catch {
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
} catch {
|
|
1144
|
+
}
|
|
1145
|
+
return false;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
function releaseSpawnLock() {
|
|
1149
|
+
try {
|
|
1150
|
+
unlinkSync2(SPAWN_LOCK_PATH);
|
|
1151
|
+
} catch {
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
function connectToSocket() {
|
|
1155
|
+
return new Promise((resolve) => {
|
|
1156
|
+
if (_socket && _connected) {
|
|
1157
|
+
resolve(true);
|
|
1158
|
+
return;
|
|
1159
|
+
}
|
|
1160
|
+
const socket = net.createConnection({ path: SOCKET_PATH });
|
|
1161
|
+
const connectTimeout = setTimeout(() => {
|
|
1162
|
+
socket.destroy();
|
|
1163
|
+
resolve(false);
|
|
1164
|
+
}, 2e3);
|
|
1165
|
+
socket.on("connect", () => {
|
|
1166
|
+
clearTimeout(connectTimeout);
|
|
1167
|
+
_socket = socket;
|
|
1168
|
+
_connected = true;
|
|
1169
|
+
_buffer = "";
|
|
1170
|
+
socket.on("data", handleData);
|
|
1171
|
+
socket.on("close", () => {
|
|
1172
|
+
_connected = false;
|
|
1173
|
+
_socket = null;
|
|
1174
|
+
for (const [id, entry] of _pending) {
|
|
1175
|
+
clearTimeout(entry.timer);
|
|
1176
|
+
_pending.delete(id);
|
|
1177
|
+
entry.resolve({ error: "Connection closed" });
|
|
1178
|
+
}
|
|
1179
|
+
});
|
|
1180
|
+
socket.on("error", () => {
|
|
1181
|
+
_connected = false;
|
|
1182
|
+
_socket = null;
|
|
1183
|
+
});
|
|
1184
|
+
resolve(true);
|
|
1185
|
+
});
|
|
1186
|
+
socket.on("error", () => {
|
|
1187
|
+
clearTimeout(connectTimeout);
|
|
1188
|
+
resolve(false);
|
|
1189
|
+
});
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
async function connectEmbedDaemon() {
|
|
1193
|
+
if (_socket && _connected) return true;
|
|
1194
|
+
if (await connectToSocket()) return true;
|
|
1195
|
+
if (acquireSpawnLock()) {
|
|
1196
|
+
try {
|
|
1197
|
+
cleanupStaleFiles();
|
|
1198
|
+
spawnDaemon();
|
|
1199
|
+
} finally {
|
|
1200
|
+
releaseSpawnLock();
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
const start = Date.now();
|
|
1204
|
+
let delay2 = 100;
|
|
1205
|
+
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
1206
|
+
await new Promise((r) => setTimeout(r, delay2));
|
|
1207
|
+
if (await connectToSocket()) return true;
|
|
1208
|
+
delay2 = Math.min(delay2 * 2, 3e3);
|
|
1209
|
+
}
|
|
1210
|
+
return false;
|
|
1211
|
+
}
|
|
1212
|
+
function sendRequest(texts, priority) {
|
|
1213
|
+
return sendDaemonRequest({ texts, priority });
|
|
1214
|
+
}
|
|
1215
|
+
function sendDaemonRequest(payload, timeoutMs = REQUEST_TIMEOUT_MS) {
|
|
1216
|
+
return new Promise((resolve) => {
|
|
1217
|
+
if (!_socket || !_connected) {
|
|
1218
|
+
resolve({ error: "Not connected" });
|
|
1219
|
+
return;
|
|
1220
|
+
}
|
|
1221
|
+
const id = randomUUID();
|
|
1222
|
+
const token = process.env[DAEMON_TOKEN_ENV] ?? readDaemonToken();
|
|
1223
|
+
const timer = setTimeout(() => {
|
|
1224
|
+
_pending.delete(id);
|
|
1225
|
+
resolve({ error: "Request timeout" });
|
|
1226
|
+
}, timeoutMs);
|
|
1227
|
+
_pending.set(id, { resolve, timer });
|
|
1228
|
+
try {
|
|
1229
|
+
_socket.write(JSON.stringify({ id, token, ...payload }) + "\n");
|
|
1230
|
+
} catch {
|
|
1231
|
+
clearTimeout(timer);
|
|
1232
|
+
_pending.delete(id);
|
|
1233
|
+
resolve({ error: "Write failed" });
|
|
1234
|
+
}
|
|
1235
|
+
});
|
|
1236
|
+
}
|
|
1237
|
+
async function pingDaemon() {
|
|
1238
|
+
if (!_socket || !_connected) return null;
|
|
1239
|
+
const response = await sendDaemonRequest({ type: "health" }, 5e3);
|
|
1240
|
+
if (response.health) {
|
|
1241
|
+
return response.health;
|
|
1242
|
+
}
|
|
1243
|
+
return null;
|
|
1244
|
+
}
|
|
1245
|
+
function killAndRespawnDaemon() {
|
|
1246
|
+
if (!acquireSpawnLock()) {
|
|
1247
|
+
process.stderr.write("[exed-client] Another process is already restarting daemon \u2014 skipping\n");
|
|
1248
|
+
if (_socket) {
|
|
1249
|
+
_socket.destroy();
|
|
1250
|
+
_socket = null;
|
|
1251
|
+
}
|
|
1252
|
+
_connected = false;
|
|
1253
|
+
_buffer = "";
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
try {
|
|
1257
|
+
process.stderr.write("[exed-client] Killing daemon for restart...\n");
|
|
1258
|
+
if (existsSync5(PID_PATH)) {
|
|
1259
|
+
try {
|
|
1260
|
+
const pid = parseInt(readFileSync4(PID_PATH, "utf8").trim(), 10);
|
|
1261
|
+
if (pid > 0) {
|
|
1262
|
+
try {
|
|
1263
|
+
process.kill(pid, "SIGKILL");
|
|
1264
|
+
} catch {
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
} catch {
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
if (_socket) {
|
|
1271
|
+
_socket.destroy();
|
|
1272
|
+
_socket = null;
|
|
1273
|
+
}
|
|
1274
|
+
_connected = false;
|
|
1275
|
+
_buffer = "";
|
|
1276
|
+
try {
|
|
1277
|
+
unlinkSync2(PID_PATH);
|
|
1278
|
+
} catch {
|
|
1279
|
+
}
|
|
1280
|
+
try {
|
|
1281
|
+
unlinkSync2(SOCKET_PATH);
|
|
1282
|
+
} catch {
|
|
1283
|
+
}
|
|
1284
|
+
spawnDaemon();
|
|
1285
|
+
} finally {
|
|
1286
|
+
releaseSpawnLock();
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
function isDaemonTooYoung() {
|
|
1290
|
+
try {
|
|
1291
|
+
const stat = statSync(PID_PATH);
|
|
1292
|
+
return Date.now() - stat.mtimeMs < MIN_DAEMON_AGE_MS;
|
|
1293
|
+
} catch {
|
|
1294
|
+
return false;
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
async function retryThenRestart(doRequest, label) {
|
|
1298
|
+
const result = await doRequest();
|
|
1299
|
+
if (!result.error) {
|
|
1300
|
+
_consecutiveFailures = 0;
|
|
1301
|
+
return result;
|
|
1302
|
+
}
|
|
1303
|
+
_consecutiveFailures++;
|
|
1304
|
+
for (let i = 0; i < MAX_RETRIES_BEFORE_RESTART; i++) {
|
|
1305
|
+
const delayMs = RETRY_DELAYS_MS[i] ?? 5e3;
|
|
1306
|
+
process.stderr.write(`[exed-client] ${label} failed (${result.error}), retry ${i + 1}/${MAX_RETRIES_BEFORE_RESTART} in ${delayMs}ms
|
|
1307
|
+
`);
|
|
1308
|
+
await new Promise((r) => setTimeout(r, delayMs));
|
|
1309
|
+
if (!_connected) {
|
|
1310
|
+
if (!await connectToSocket()) continue;
|
|
1311
|
+
}
|
|
1312
|
+
const retry = await doRequest();
|
|
1313
|
+
if (!retry.error) {
|
|
1314
|
+
_consecutiveFailures = 0;
|
|
1315
|
+
return retry;
|
|
1316
|
+
}
|
|
1317
|
+
_consecutiveFailures++;
|
|
1318
|
+
}
|
|
1319
|
+
if (isDaemonTooYoung()) {
|
|
1320
|
+
process.stderr.write(`[exed-client] ${label}: daemon too young (< ${MIN_DAEMON_AGE_MS / 1e3}s) \u2014 skipping restart
|
|
1321
|
+
`);
|
|
1322
|
+
return { error: result.error };
|
|
1323
|
+
}
|
|
1324
|
+
process.stderr.write(`[exed-client] ${label}: ${_consecutiveFailures} consecutive failures \u2014 restarting daemon
|
|
1325
|
+
`);
|
|
1326
|
+
killAndRespawnDaemon();
|
|
1327
|
+
const start = Date.now();
|
|
1328
|
+
let delay2 = 200;
|
|
1329
|
+
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
1330
|
+
await new Promise((r) => setTimeout(r, delay2));
|
|
1331
|
+
if (await connectToSocket()) break;
|
|
1332
|
+
delay2 = Math.min(delay2 * 2, 3e3);
|
|
1333
|
+
}
|
|
1334
|
+
if (!_connected) return { error: "Daemon restart failed" };
|
|
1335
|
+
const final = await doRequest();
|
|
1336
|
+
if (!final.error) _consecutiveFailures = 0;
|
|
1337
|
+
return final;
|
|
1338
|
+
}
|
|
1339
|
+
async function embedViaClient(text, priority = "high") {
|
|
1340
|
+
if (!_connected && !await connectEmbedDaemon()) return null;
|
|
1341
|
+
_requestCount++;
|
|
1342
|
+
if (_requestCount % HEALTH_CHECK_INTERVAL === 0) {
|
|
1343
|
+
const health = await pingDaemon();
|
|
1344
|
+
if (!health && !isDaemonTooYoung()) {
|
|
1345
|
+
process.stderr.write(`[exed-client] Periodic health check failed at request ${_requestCount} \u2014 restarting daemon
|
|
1346
|
+
`);
|
|
1347
|
+
killAndRespawnDaemon();
|
|
1348
|
+
const start = Date.now();
|
|
1349
|
+
let d = 200;
|
|
1350
|
+
while (Date.now() - start < CONNECT_TIMEOUT_MS) {
|
|
1351
|
+
await new Promise((r) => setTimeout(r, d));
|
|
1352
|
+
if (await connectToSocket()) break;
|
|
1353
|
+
d = Math.min(d * 2, 3e3);
|
|
1354
|
+
}
|
|
1355
|
+
if (!_connected) return null;
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
const result = await retryThenRestart(
|
|
1359
|
+
() => sendRequest([text], priority),
|
|
1360
|
+
"Embed"
|
|
1361
|
+
);
|
|
1362
|
+
return !result.error && result.vectors?.[0] ? result.vectors[0] : null;
|
|
1363
|
+
}
|
|
1364
|
+
async function embedBatchViaClient(texts, priority = "high") {
|
|
1365
|
+
if (!_connected && !await connectEmbedDaemon()) return null;
|
|
1366
|
+
_requestCount++;
|
|
1367
|
+
const result = await retryThenRestart(
|
|
1368
|
+
() => sendRequest(texts, priority),
|
|
1369
|
+
"Batch embed"
|
|
1370
|
+
);
|
|
1371
|
+
return !result.error && result.vectors ? result.vectors : null;
|
|
1372
|
+
}
|
|
1373
|
+
function disconnectClient() {
|
|
1374
|
+
if (_socket) {
|
|
1375
|
+
_socket.destroy();
|
|
1376
|
+
_socket = null;
|
|
1377
|
+
}
|
|
1378
|
+
_connected = false;
|
|
1379
|
+
_buffer = "";
|
|
1380
|
+
for (const [id, entry] of _pending) {
|
|
1381
|
+
clearTimeout(entry.timer);
|
|
1382
|
+
_pending.delete(id);
|
|
1383
|
+
entry.resolve({ error: "Client disconnected" });
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
function isClientConnected() {
|
|
1387
|
+
return _connected;
|
|
1388
|
+
}
|
|
1389
|
+
function sendIngestRequest(payload) {
|
|
1390
|
+
if (!_socket || !_connected) return false;
|
|
1391
|
+
try {
|
|
1392
|
+
const id = randomUUID();
|
|
1393
|
+
const token = process.env[DAEMON_TOKEN_ENV] ?? readDaemonToken();
|
|
1394
|
+
_socket.write(JSON.stringify({ id, token, type: "ingest", ...payload }) + "\n");
|
|
1395
|
+
return true;
|
|
1396
|
+
} catch {
|
|
1397
|
+
return false;
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
var SOCKET_PATH, PID_PATH, SPAWN_LOCK_PATH, SPAWN_LOCK_STALE_MS, CONNECT_TIMEOUT_MS, REQUEST_TIMEOUT_MS, DAEMON_TOKEN_ENV, _socket, _connected, _buffer, _requestCount, _consecutiveFailures, HEALTH_CHECK_INTERVAL, MAX_RETRIES_BEFORE_RESTART, RETRY_DELAYS_MS, MIN_DAEMON_AGE_MS, _pending, MAX_BUFFER;
|
|
1401
|
+
var init_exe_daemon_client = __esm({
|
|
1402
|
+
"src/lib/exe-daemon-client.ts"() {
|
|
1403
|
+
"use strict";
|
|
1404
|
+
init_config();
|
|
1405
|
+
init_daemon_auth();
|
|
1406
|
+
SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ?? path5.join(EXE_AI_DIR, "exed.sock");
|
|
1407
|
+
PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ?? path5.join(EXE_AI_DIR, "exed.pid");
|
|
1408
|
+
SPAWN_LOCK_PATH = path5.join(EXE_AI_DIR, "exed-spawn.lock");
|
|
1409
|
+
SPAWN_LOCK_STALE_MS = 3e4;
|
|
1410
|
+
CONNECT_TIMEOUT_MS = 15e3;
|
|
1411
|
+
REQUEST_TIMEOUT_MS = 3e4;
|
|
1412
|
+
DAEMON_TOKEN_ENV = "EXE_DAEMON_TOKEN";
|
|
1413
|
+
_socket = null;
|
|
1414
|
+
_connected = false;
|
|
1415
|
+
_buffer = "";
|
|
1416
|
+
_requestCount = 0;
|
|
1417
|
+
_consecutiveFailures = 0;
|
|
1418
|
+
HEALTH_CHECK_INTERVAL = 100;
|
|
1419
|
+
MAX_RETRIES_BEFORE_RESTART = 3;
|
|
1420
|
+
RETRY_DELAYS_MS = [1e3, 3e3, 5e3];
|
|
1421
|
+
MIN_DAEMON_AGE_MS = 3e4;
|
|
1422
|
+
_pending = /* @__PURE__ */ new Map();
|
|
1423
|
+
MAX_BUFFER = 1e7;
|
|
1424
|
+
}
|
|
1425
|
+
});
|
|
1426
|
+
|
|
1427
|
+
// src/lib/daemon-protocol.ts
|
|
1428
|
+
var daemon_protocol_exports = {};
|
|
1429
|
+
__export(daemon_protocol_exports, {
|
|
1430
|
+
deserializeArgs: () => deserializeArgs,
|
|
1431
|
+
deserializeResultSet: () => deserializeResultSet,
|
|
1432
|
+
deserializeValue: () => deserializeValue,
|
|
1433
|
+
serializeArgs: () => serializeArgs,
|
|
1434
|
+
serializeResultSet: () => serializeResultSet,
|
|
1435
|
+
serializeValue: () => serializeValue
|
|
1436
|
+
});
|
|
1437
|
+
function serializeValue(v) {
|
|
1438
|
+
if (v === null || v === void 0) return null;
|
|
1439
|
+
if (typeof v === "bigint") return Number(v);
|
|
1440
|
+
if (typeof v === "boolean") return v ? 1 : 0;
|
|
1441
|
+
if (v instanceof Uint8Array) {
|
|
1442
|
+
return { __blob: Buffer.from(v).toString("base64") };
|
|
1443
|
+
}
|
|
1444
|
+
if (ArrayBuffer.isView(v)) {
|
|
1445
|
+
return { __blob: Buffer.from(v.buffer, v.byteOffset, v.byteLength).toString("base64") };
|
|
1446
|
+
}
|
|
1447
|
+
if (v instanceof ArrayBuffer) {
|
|
1448
|
+
return { __blob: Buffer.from(v).toString("base64") };
|
|
1449
|
+
}
|
|
1450
|
+
if (typeof v === "string" || typeof v === "number") return v;
|
|
1451
|
+
return String(v);
|
|
1452
|
+
}
|
|
1453
|
+
function deserializeValue(v) {
|
|
1454
|
+
if (v === null) return null;
|
|
1455
|
+
if (typeof v === "object" && v !== null && "__blob" in v) {
|
|
1456
|
+
const buf = Buffer.from(v.__blob, "base64");
|
|
1457
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
|
|
1458
|
+
}
|
|
1459
|
+
return v;
|
|
1460
|
+
}
|
|
1461
|
+
function serializeArgs(args) {
|
|
1462
|
+
return args.map(serializeValue);
|
|
1463
|
+
}
|
|
1464
|
+
function deserializeArgs(args) {
|
|
1465
|
+
return args.map(deserializeValue);
|
|
1466
|
+
}
|
|
1467
|
+
function serializeResultSet(rs) {
|
|
1468
|
+
const rows = [];
|
|
1469
|
+
for (const row of rs.rows) {
|
|
1470
|
+
const obj = {};
|
|
1471
|
+
for (let i = 0; i < rs.columns.length; i++) {
|
|
1472
|
+
const col = rs.columns[i];
|
|
1473
|
+
if (col !== void 0) {
|
|
1474
|
+
obj[col] = serializeValue(row[i]);
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
rows.push(obj);
|
|
1478
|
+
}
|
|
1479
|
+
return {
|
|
1480
|
+
columns: [...rs.columns],
|
|
1481
|
+
columnTypes: [...rs.columnTypes ?? []],
|
|
1482
|
+
rows,
|
|
1483
|
+
rowsAffected: typeof rs.rowsAffected === "bigint" ? Number(rs.rowsAffected) : rs.rowsAffected ?? 0,
|
|
1484
|
+
lastInsertRowid: rs.lastInsertRowid != null ? typeof rs.lastInsertRowid === "bigint" ? Number(rs.lastInsertRowid) : rs.lastInsertRowid : null
|
|
1485
|
+
};
|
|
1486
|
+
}
|
|
1487
|
+
function deserializeResultSet(srs) {
|
|
1488
|
+
const rows = srs.rows.map((obj) => {
|
|
1489
|
+
const values = srs.columns.map(
|
|
1490
|
+
(col) => deserializeValue(obj[col] ?? null)
|
|
1491
|
+
);
|
|
1492
|
+
const row = values;
|
|
1493
|
+
for (let i = 0; i < srs.columns.length; i++) {
|
|
1494
|
+
const col = srs.columns[i];
|
|
1495
|
+
if (col !== void 0) {
|
|
1496
|
+
row[col] = values[i] ?? null;
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
Object.defineProperty(row, "length", {
|
|
1500
|
+
value: values.length,
|
|
1501
|
+
enumerable: false
|
|
1502
|
+
});
|
|
1503
|
+
return row;
|
|
1504
|
+
});
|
|
1505
|
+
return {
|
|
1506
|
+
columns: srs.columns,
|
|
1507
|
+
columnTypes: srs.columnTypes ?? [],
|
|
1508
|
+
rows,
|
|
1509
|
+
rowsAffected: srs.rowsAffected,
|
|
1510
|
+
lastInsertRowid: srs.lastInsertRowid != null ? BigInt(srs.lastInsertRowid) : void 0,
|
|
1511
|
+
toJSON: () => ({
|
|
1512
|
+
columns: srs.columns,
|
|
1513
|
+
columnTypes: srs.columnTypes ?? [],
|
|
1514
|
+
rows: srs.rows,
|
|
1515
|
+
rowsAffected: srs.rowsAffected,
|
|
1516
|
+
lastInsertRowid: srs.lastInsertRowid
|
|
1517
|
+
})
|
|
1518
|
+
};
|
|
1519
|
+
}
|
|
1520
|
+
var init_daemon_protocol = __esm({
|
|
1521
|
+
"src/lib/daemon-protocol.ts"() {
|
|
1522
|
+
"use strict";
|
|
1523
|
+
}
|
|
1524
|
+
});
|
|
1525
|
+
|
|
1526
|
+
// src/lib/db-daemon-client.ts
|
|
1527
|
+
var db_daemon_client_exports = {};
|
|
1528
|
+
__export(db_daemon_client_exports, {
|
|
1529
|
+
createDaemonDbClient: () => createDaemonDbClient,
|
|
1530
|
+
initDaemonDbClient: () => initDaemonDbClient
|
|
1531
|
+
});
|
|
1532
|
+
function normalizeStatement2(stmt) {
|
|
1533
|
+
if (typeof stmt === "string") {
|
|
1534
|
+
return { sql: stmt, args: [] };
|
|
1535
|
+
}
|
|
1536
|
+
const sql = stmt.sql;
|
|
1537
|
+
let args = [];
|
|
1538
|
+
if (Array.isArray(stmt.args)) {
|
|
1539
|
+
args = stmt.args.map((v) => serializeValue(v));
|
|
1540
|
+
} else if (stmt.args && typeof stmt.args === "object") {
|
|
1541
|
+
const named = {};
|
|
1542
|
+
for (const [key, val] of Object.entries(stmt.args)) {
|
|
1543
|
+
named[key] = serializeValue(val);
|
|
1544
|
+
}
|
|
1545
|
+
return { sql, args: named };
|
|
1546
|
+
}
|
|
1547
|
+
return { sql, args };
|
|
1548
|
+
}
|
|
1549
|
+
function createDaemonDbClient(fallbackClient) {
|
|
1550
|
+
let _useDaemon = false;
|
|
1551
|
+
const client = {
|
|
1552
|
+
async execute(stmt) {
|
|
1553
|
+
if (!_useDaemon || !isClientConnected()) {
|
|
1554
|
+
return fallbackClient.execute(stmt);
|
|
1555
|
+
}
|
|
1556
|
+
const { sql, args } = normalizeStatement2(stmt);
|
|
1557
|
+
const response = await sendDaemonRequest({
|
|
1558
|
+
type: "db-execute",
|
|
1559
|
+
sql,
|
|
1560
|
+
args
|
|
1561
|
+
});
|
|
1562
|
+
if (response.error) {
|
|
1563
|
+
const errMsg = String(response.error);
|
|
1564
|
+
if (errMsg === "Not connected" || errMsg === "Request timeout" || errMsg === "Write failed" || errMsg === "DB not initialized") {
|
|
1565
|
+
process.stderr.write(`[db-daemon] Transport error (${errMsg}), falling back to direct
|
|
1566
|
+
`);
|
|
1567
|
+
return fallbackClient.execute(stmt);
|
|
1568
|
+
}
|
|
1569
|
+
throw new Error(errMsg);
|
|
1570
|
+
}
|
|
1571
|
+
if (response.db) {
|
|
1572
|
+
return deserializeResultSet(response.db);
|
|
1573
|
+
}
|
|
1574
|
+
process.stderr.write("[db-daemon] Unexpected response shape, falling back to direct\n");
|
|
1575
|
+
return fallbackClient.execute(stmt);
|
|
1576
|
+
},
|
|
1577
|
+
async batch(stmts, mode) {
|
|
1578
|
+
if (!_useDaemon || !isClientConnected()) {
|
|
1579
|
+
return fallbackClient.batch(stmts, mode);
|
|
1580
|
+
}
|
|
1581
|
+
const statements = stmts.map(normalizeStatement2);
|
|
1582
|
+
const response = await sendDaemonRequest({
|
|
1583
|
+
type: "db-batch",
|
|
1584
|
+
statements,
|
|
1585
|
+
mode: mode ?? "deferred"
|
|
1586
|
+
});
|
|
1587
|
+
if (response.error) {
|
|
1588
|
+
const errMsg = String(response.error);
|
|
1589
|
+
if (errMsg === "Not connected" || errMsg === "Request timeout" || errMsg === "Write failed" || errMsg === "DB not initialized") {
|
|
1590
|
+
process.stderr.write(`[db-daemon] Batch transport error (${errMsg}), falling back to direct
|
|
1591
|
+
`);
|
|
1592
|
+
return fallbackClient.batch(stmts, mode);
|
|
1593
|
+
}
|
|
1594
|
+
throw new Error(errMsg);
|
|
1595
|
+
}
|
|
1596
|
+
const batchResults = response["db-batch"];
|
|
1597
|
+
if (batchResults) {
|
|
1598
|
+
return batchResults.map(deserializeResultSet);
|
|
1599
|
+
}
|
|
1600
|
+
process.stderr.write("[db-daemon] Unexpected batch response shape, falling back to direct\n");
|
|
1601
|
+
return fallbackClient.batch(stmts, mode);
|
|
1602
|
+
},
|
|
1603
|
+
// Transaction support — delegate to fallback (transactions need direct connection)
|
|
1604
|
+
async transaction(mode) {
|
|
1605
|
+
return fallbackClient.transaction(mode);
|
|
1606
|
+
},
|
|
1607
|
+
// executeMultiple — delegate to fallback (used only for schema migrations)
|
|
1608
|
+
async executeMultiple(sql) {
|
|
1609
|
+
return fallbackClient.executeMultiple(sql);
|
|
1610
|
+
},
|
|
1611
|
+
// migrate — delegate to fallback
|
|
1612
|
+
async migrate(stmts) {
|
|
1613
|
+
return fallbackClient.migrate(stmts);
|
|
1614
|
+
},
|
|
1615
|
+
// Sync mode — delegate to fallback
|
|
1616
|
+
sync() {
|
|
1617
|
+
return fallbackClient.sync();
|
|
1618
|
+
},
|
|
1619
|
+
close() {
|
|
1620
|
+
_useDaemon = false;
|
|
1621
|
+
},
|
|
1622
|
+
get closed() {
|
|
1623
|
+
return fallbackClient.closed;
|
|
1624
|
+
},
|
|
1625
|
+
get protocol() {
|
|
1626
|
+
return fallbackClient.protocol;
|
|
1627
|
+
}
|
|
1628
|
+
};
|
|
1629
|
+
return {
|
|
1630
|
+
...client,
|
|
1631
|
+
/** Enable daemon routing (call after confirming daemon is connected) */
|
|
1632
|
+
_enableDaemon() {
|
|
1633
|
+
_useDaemon = true;
|
|
1634
|
+
},
|
|
1635
|
+
/** Check if daemon routing is active */
|
|
1636
|
+
_isDaemonActive() {
|
|
1637
|
+
return _useDaemon && isClientConnected();
|
|
1638
|
+
}
|
|
1639
|
+
};
|
|
1640
|
+
}
|
|
1641
|
+
async function initDaemonDbClient(fallbackClient) {
|
|
1642
|
+
if (process.env.EXE_IS_DAEMON === "1") return null;
|
|
1643
|
+
const connected = await connectEmbedDaemon();
|
|
1644
|
+
if (!connected) {
|
|
1645
|
+
process.stderr.write("[db-daemon] Daemon unavailable \u2014 using direct SQLite\n");
|
|
1646
|
+
return null;
|
|
1647
|
+
}
|
|
1648
|
+
const client = createDaemonDbClient(fallbackClient);
|
|
1649
|
+
client._enableDaemon();
|
|
1650
|
+
process.stderr.write("[db-daemon] DB routing through daemon (single-writer)\n");
|
|
1651
|
+
return client;
|
|
1652
|
+
}
|
|
1653
|
+
var init_db_daemon_client = __esm({
|
|
1654
|
+
"src/lib/db-daemon-client.ts"() {
|
|
1655
|
+
"use strict";
|
|
1656
|
+
init_exe_daemon_client();
|
|
1657
|
+
init_daemon_protocol();
|
|
1658
|
+
}
|
|
1659
|
+
});
|
|
1660
|
+
|
|
899
1661
|
// src/lib/database.ts
|
|
1662
|
+
var database_exports = {};
|
|
1663
|
+
__export(database_exports, {
|
|
1664
|
+
disposeDatabase: () => disposeDatabase,
|
|
1665
|
+
disposeTurso: () => disposeTurso,
|
|
1666
|
+
ensureSchema: () => ensureSchema,
|
|
1667
|
+
getClient: () => getClient,
|
|
1668
|
+
getRawClient: () => getRawClient,
|
|
1669
|
+
initDaemonClient: () => initDaemonClient,
|
|
1670
|
+
initDatabase: () => initDatabase,
|
|
1671
|
+
initTurso: () => initTurso,
|
|
1672
|
+
isInitialized: () => isInitialized
|
|
1673
|
+
});
|
|
900
1674
|
import { createClient } from "@libsql/client";
|
|
901
1675
|
async function initDatabase(config) {
|
|
902
1676
|
if (_walCheckpointTimer) {
|
|
@@ -935,15 +1709,18 @@ async function initDatabase(config) {
|
|
|
935
1709
|
});
|
|
936
1710
|
}, 3e4);
|
|
937
1711
|
_walCheckpointTimer.unref();
|
|
938
|
-
if (process.env.DATABASE_URL) {
|
|
1712
|
+
if (process.env.DATABASE_URL && process.env.EXE_USE_POSTGRES === "1") {
|
|
939
1713
|
_adapterClient = await createPrismaDbAdapter(_resilientClient);
|
|
940
1714
|
}
|
|
941
1715
|
}
|
|
1716
|
+
function isInitialized() {
|
|
1717
|
+
return _adapterClient !== null || _client !== null;
|
|
1718
|
+
}
|
|
942
1719
|
function getClient() {
|
|
943
1720
|
if (!_adapterClient) {
|
|
944
1721
|
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
945
1722
|
}
|
|
946
|
-
if (process.env.DATABASE_URL) {
|
|
1723
|
+
if (process.env.DATABASE_URL && process.env.EXE_USE_POSTGRES === "1") {
|
|
947
1724
|
return _adapterClient;
|
|
948
1725
|
}
|
|
949
1726
|
if (process.env.EXE_IS_DAEMON === "1") {
|
|
@@ -954,6 +1731,22 @@ function getClient() {
|
|
|
954
1731
|
}
|
|
955
1732
|
return _resilientClient;
|
|
956
1733
|
}
|
|
1734
|
+
async function initDaemonClient() {
|
|
1735
|
+
if (process.env.DATABASE_URL && process.env.EXE_USE_POSTGRES === "1") return;
|
|
1736
|
+
if (process.env.EXE_IS_DAEMON === "1") return;
|
|
1737
|
+
if (process.env.VITEST) return;
|
|
1738
|
+
if (!_resilientClient) return;
|
|
1739
|
+
if (_daemonClient) return;
|
|
1740
|
+
try {
|
|
1741
|
+
const { initDaemonDbClient: initDaemonDbClient2 } = await Promise.resolve().then(() => (init_db_daemon_client(), db_daemon_client_exports));
|
|
1742
|
+
_daemonClient = await initDaemonDbClient2(_resilientClient);
|
|
1743
|
+
} catch (err) {
|
|
1744
|
+
process.stderr.write(
|
|
1745
|
+
`[database] Daemon client init failed (non-fatal): ${err instanceof Error ? err.message : String(err)}
|
|
1746
|
+
`
|
|
1747
|
+
);
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
957
1750
|
function getRawClient() {
|
|
958
1751
|
if (!_client) {
|
|
959
1752
|
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
@@ -1087,7 +1880,14 @@ async function ensureSchema() {
|
|
|
1087
1880
|
}
|
|
1088
1881
|
try {
|
|
1089
1882
|
await client.execute({
|
|
1090
|
-
sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
|
|
1883
|
+
sql: `ALTER TABLE behaviors ADD COLUMN priority TEXT DEFAULT 'p1'`,
|
|
1884
|
+
args: []
|
|
1885
|
+
});
|
|
1886
|
+
} catch {
|
|
1887
|
+
}
|
|
1888
|
+
try {
|
|
1889
|
+
await client.execute({
|
|
1890
|
+
sql: `ALTER TABLE behaviors ADD COLUMN vector F32_BLOB(${EMBEDDING_DIM})`,
|
|
1091
1891
|
args: []
|
|
1092
1892
|
});
|
|
1093
1893
|
} catch {
|
|
@@ -1962,6 +2762,7 @@ var init_database = __esm({
|
|
|
1962
2762
|
init_db_retry();
|
|
1963
2763
|
init_employees();
|
|
1964
2764
|
init_database_adapter();
|
|
2765
|
+
init_memory();
|
|
1965
2766
|
_client = null;
|
|
1966
2767
|
_resilientClient = null;
|
|
1967
2768
|
_walCheckpointTimer = null;
|
|
@@ -1972,6 +2773,118 @@ var init_database = __esm({
|
|
|
1972
2773
|
}
|
|
1973
2774
|
});
|
|
1974
2775
|
|
|
2776
|
+
// src/lib/keychain.ts
|
|
2777
|
+
import { readFile as readFile3, writeFile as writeFile3, unlink, mkdir as mkdir3, chmod as chmod2 } from "fs/promises";
|
|
2778
|
+
import { existsSync as existsSync6 } from "fs";
|
|
2779
|
+
import path6 from "path";
|
|
2780
|
+
import os5 from "os";
|
|
2781
|
+
function getKeyDir() {
|
|
2782
|
+
return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? path6.join(os5.homedir(), ".exe-os");
|
|
2783
|
+
}
|
|
2784
|
+
function getKeyPath() {
|
|
2785
|
+
return path6.join(getKeyDir(), "master.key");
|
|
2786
|
+
}
|
|
2787
|
+
async function tryKeytar() {
|
|
2788
|
+
try {
|
|
2789
|
+
return await import("keytar");
|
|
2790
|
+
} catch {
|
|
2791
|
+
return null;
|
|
2792
|
+
}
|
|
2793
|
+
}
|
|
2794
|
+
async function getMasterKey() {
|
|
2795
|
+
const keytar = await tryKeytar();
|
|
2796
|
+
if (keytar) {
|
|
2797
|
+
try {
|
|
2798
|
+
const stored = await keytar.getPassword(SERVICE, ACCOUNT);
|
|
2799
|
+
if (stored) {
|
|
2800
|
+
return Buffer.from(stored, "base64");
|
|
2801
|
+
}
|
|
2802
|
+
} catch {
|
|
2803
|
+
}
|
|
2804
|
+
}
|
|
2805
|
+
const keyPath = getKeyPath();
|
|
2806
|
+
if (!existsSync6(keyPath)) {
|
|
2807
|
+
process.stderr.write(
|
|
2808
|
+
`[keychain] Key not found at ${keyPath} (HOME=${os5.homedir()}, EXE_OS_DIR=${process.env.EXE_OS_DIR ?? "unset"})
|
|
2809
|
+
`
|
|
2810
|
+
);
|
|
2811
|
+
return null;
|
|
2812
|
+
}
|
|
2813
|
+
try {
|
|
2814
|
+
const content = await readFile3(keyPath, "utf-8");
|
|
2815
|
+
return Buffer.from(content.trim(), "base64");
|
|
2816
|
+
} catch (err) {
|
|
2817
|
+
process.stderr.write(
|
|
2818
|
+
`[keychain] Key read failed at ${keyPath}: ${err instanceof Error ? err.message : String(err)}
|
|
2819
|
+
`
|
|
2820
|
+
);
|
|
2821
|
+
return null;
|
|
2822
|
+
}
|
|
2823
|
+
}
|
|
2824
|
+
var SERVICE, ACCOUNT;
|
|
2825
|
+
var init_keychain = __esm({
|
|
2826
|
+
"src/lib/keychain.ts"() {
|
|
2827
|
+
"use strict";
|
|
2828
|
+
SERVICE = "exe-mem";
|
|
2829
|
+
ACCOUNT = "master-key";
|
|
2830
|
+
}
|
|
2831
|
+
});
|
|
2832
|
+
|
|
2833
|
+
// src/lib/state-bus.ts
|
|
2834
|
+
var StateBus, orgBus;
|
|
2835
|
+
var init_state_bus = __esm({
|
|
2836
|
+
"src/lib/state-bus.ts"() {
|
|
2837
|
+
"use strict";
|
|
2838
|
+
StateBus = class {
|
|
2839
|
+
handlers = /* @__PURE__ */ new Map();
|
|
2840
|
+
globalHandlers = /* @__PURE__ */ new Set();
|
|
2841
|
+
/** Emit an event to all subscribers */
|
|
2842
|
+
emit(event) {
|
|
2843
|
+
const typeHandlers = this.handlers.get(event.type);
|
|
2844
|
+
if (typeHandlers) {
|
|
2845
|
+
for (const handler of typeHandlers) {
|
|
2846
|
+
try {
|
|
2847
|
+
handler(event);
|
|
2848
|
+
} catch {
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2851
|
+
}
|
|
2852
|
+
for (const handler of this.globalHandlers) {
|
|
2853
|
+
try {
|
|
2854
|
+
handler(event);
|
|
2855
|
+
} catch {
|
|
2856
|
+
}
|
|
2857
|
+
}
|
|
2858
|
+
}
|
|
2859
|
+
/** Subscribe to a specific event type */
|
|
2860
|
+
on(type, handler) {
|
|
2861
|
+
if (!this.handlers.has(type)) {
|
|
2862
|
+
this.handlers.set(type, /* @__PURE__ */ new Set());
|
|
2863
|
+
}
|
|
2864
|
+
this.handlers.get(type).add(handler);
|
|
2865
|
+
}
|
|
2866
|
+
/** Subscribe to ALL events */
|
|
2867
|
+
onAny(handler) {
|
|
2868
|
+
this.globalHandlers.add(handler);
|
|
2869
|
+
}
|
|
2870
|
+
/** Unsubscribe from a specific event type */
|
|
2871
|
+
off(type, handler) {
|
|
2872
|
+
this.handlers.get(type)?.delete(handler);
|
|
2873
|
+
}
|
|
2874
|
+
/** Unsubscribe from ALL events */
|
|
2875
|
+
offAny(handler) {
|
|
2876
|
+
this.globalHandlers.delete(handler);
|
|
2877
|
+
}
|
|
2878
|
+
/** Remove all listeners */
|
|
2879
|
+
clear() {
|
|
2880
|
+
this.handlers.clear();
|
|
2881
|
+
this.globalHandlers.clear();
|
|
2882
|
+
}
|
|
2883
|
+
};
|
|
2884
|
+
orgBus = new StateBus();
|
|
2885
|
+
}
|
|
2886
|
+
});
|
|
2887
|
+
|
|
1975
2888
|
// src/lib/shard-manager.ts
|
|
1976
2889
|
var shard_manager_exports = {};
|
|
1977
2890
|
__export(shard_manager_exports, {
|
|
@@ -1986,12 +2899,12 @@ __export(shard_manager_exports, {
|
|
|
1986
2899
|
listShards: () => listShards,
|
|
1987
2900
|
shardExists: () => shardExists
|
|
1988
2901
|
});
|
|
1989
|
-
import
|
|
1990
|
-
import { existsSync as
|
|
2902
|
+
import path7 from "path";
|
|
2903
|
+
import { existsSync as existsSync7, mkdirSync as mkdirSync2, readdirSync } from "fs";
|
|
1991
2904
|
import { createClient as createClient2 } from "@libsql/client";
|
|
1992
2905
|
function initShardManager(encryptionKey) {
|
|
1993
2906
|
_encryptionKey = encryptionKey;
|
|
1994
|
-
if (!
|
|
2907
|
+
if (!existsSync7(SHARDS_DIR)) {
|
|
1995
2908
|
mkdirSync2(SHARDS_DIR, { recursive: true });
|
|
1996
2909
|
}
|
|
1997
2910
|
_shardingEnabled = true;
|
|
@@ -2021,7 +2934,7 @@ function getShardClient(projectName) {
|
|
|
2021
2934
|
while (_shards.size >= MAX_OPEN_SHARDS) {
|
|
2022
2935
|
evictLRU();
|
|
2023
2936
|
}
|
|
2024
|
-
const dbPath =
|
|
2937
|
+
const dbPath = path7.join(SHARDS_DIR, `${safeName}.db`);
|
|
2025
2938
|
const client = createClient2({
|
|
2026
2939
|
url: `file:${dbPath}`,
|
|
2027
2940
|
encryptionKey: _encryptionKey
|
|
@@ -2032,10 +2945,10 @@ function getShardClient(projectName) {
|
|
|
2032
2945
|
}
|
|
2033
2946
|
function shardExists(projectName) {
|
|
2034
2947
|
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
2035
|
-
return
|
|
2948
|
+
return existsSync7(path7.join(SHARDS_DIR, `${safeName}.db`));
|
|
2036
2949
|
}
|
|
2037
2950
|
function listShards() {
|
|
2038
|
-
if (!
|
|
2951
|
+
if (!existsSync7(SHARDS_DIR)) return [];
|
|
2039
2952
|
return readdirSync(SHARDS_DIR).filter((f) => f.endsWith(".db")).map((f) => f.replace(".db", ""));
|
|
2040
2953
|
}
|
|
2041
2954
|
async function ensureShardSchema(client) {
|
|
@@ -2282,7 +3195,7 @@ var init_shard_manager = __esm({
|
|
|
2282
3195
|
"src/lib/shard-manager.ts"() {
|
|
2283
3196
|
"use strict";
|
|
2284
3197
|
init_config();
|
|
2285
|
-
SHARDS_DIR =
|
|
3198
|
+
SHARDS_DIR = path7.join(EXE_AI_DIR, "shards");
|
|
2286
3199
|
SHARD_IDLE_MS = 5 * 60 * 1e3;
|
|
2287
3200
|
MAX_OPEN_SHARDS = 10;
|
|
2288
3201
|
EVICTION_INTERVAL_MS = 60 * 1e3;
|
|
@@ -2402,6 +3315,68 @@ var init_platform_procedures = __esm({
|
|
|
2402
3315
|
domain: "architecture",
|
|
2403
3316
|
priority: "p0",
|
|
2404
3317
|
content: "Tasks live in the DB. Intercom (tmux send-keys) is fire-and-forget \u2014 it may fail, get garbled, or arrive mid-work. Never rely on intercom for task delivery. The UserPromptSubmit hook checks the DB for new tasks on every prompt. Your operating procedures step 7 says check for next work. The daemon nudges idle agents as a speedup. If you have no tasks, you found them all."
|
|
3318
|
+
},
|
|
3319
|
+
// --- MCP is the ONLY data interface ---
|
|
3320
|
+
{
|
|
3321
|
+
title: "MCP disconnect \u2014 ask the user, never work around it",
|
|
3322
|
+
domain: "workflow",
|
|
3323
|
+
priority: "p0",
|
|
3324
|
+
content: "If MCP tools are unavailable, disconnected, or returning connection errors: STOP. Tell the user clearly: 'MCP server is disconnected. Please run /mcp to reconnect.' Do NOT attempt workarounds \u2014 no raw Node imports, no direct DB access, no CLI hacks, no daemon socket calls. MCP is the ONLY data interface. Working around it wastes time, hits bundling issues, and bypasses the contract boundary. Ask once, wait, proceed when reconnected."
|
|
3325
|
+
},
|
|
3326
|
+
// --- MCP Tool Catalog (Layer 0 — every agent knows what tools exist) ---
|
|
3327
|
+
{
|
|
3328
|
+
title: "MCP tools \u2014 memory and search",
|
|
3329
|
+
domain: "tool-use",
|
|
3330
|
+
priority: "p1",
|
|
3331
|
+
content: "recall_my_memory: search your own memories (semantic + FTS). ask_team_memory: search a colleague's memories by agent name. store_memory: persist a memory (decisions, summaries, context). commit_memory: high-importance memory that survives consolidation. search_everything: unified search across memories, tasks, entities, conversations. get_session_context: temporal window of memories around a timestamp. consolidate_memories: merge duplicate/related memories into insights. get_memory_cardinality: count memories per agent (health check)."
|
|
3332
|
+
},
|
|
3333
|
+
{
|
|
3334
|
+
title: "MCP tools \u2014 task orchestration",
|
|
3335
|
+
domain: "tool-use",
|
|
3336
|
+
priority: "p1",
|
|
3337
|
+
content: "create_task: dispatch work to an employee (auto-spawns session). The ONLY dispatch path. list_tasks: query tasks by status, assignee, project. get_task: fetch full task details by ID. update_task: change status (in_progress, done, blocked, cancelled) + add result summary. close_task: finalize a reviewed task (COO only). checkpoint_task: save progress state for crash recovery. resume_employee: re-spawn an employee session for an existing task."
|
|
3338
|
+
},
|
|
3339
|
+
{
|
|
3340
|
+
title: "MCP tools \u2014 knowledge graph (GraphRAG)",
|
|
3341
|
+
domain: "tool-use",
|
|
3342
|
+
priority: "p1",
|
|
3343
|
+
content: "query_relationships: find connections between entities in the knowledge graph. get_entity_neighbors: explore an entity's direct connections. get_hot_entities: find most-referenced entities (trending topics). get_graph_stats: graph health \u2014 entity/relationship counts, density. export_graph: export graph data for visualization. merge_entities: deduplicate entities (alias resolution). find_similar_trajectories: match tool-call patterns to past task solutions."
|
|
3344
|
+
},
|
|
3345
|
+
{
|
|
3346
|
+
title: "MCP tools \u2014 identity, behavior, and decisions",
|
|
3347
|
+
domain: "tool-use",
|
|
3348
|
+
priority: "p1",
|
|
3349
|
+
content: "get_identity: read an agent's exe.md (Layer 1 identity). update_identity: write an agent's exe.md. Identity > behavior \u2014 use for permanent rules. store_behavior: record a correction or pattern for an agent (Layer 2 expertise). list_behaviors: view an agent's active behaviors. deactivate_behavior: soft-delete a stale or conflicting behavior. store_decision: record an ADR (architectural decision record). get_decision: retrieve a past decision by query."
|
|
3350
|
+
},
|
|
3351
|
+
{
|
|
3352
|
+
title: "MCP tools \u2014 communication and messaging",
|
|
3353
|
+
domain: "tool-use",
|
|
3354
|
+
priority: "p1",
|
|
3355
|
+
content: "send_message: send supplementary context to another agent (NOT for actionable work \u2014 use create_task). acknowledge_messages: mark messages as read. send_whatsapp: send WhatsApp message via gateway (customer-facing alerts). query_conversations: search ingested conversations across all channels (WhatsApp, email, etc.)."
|
|
3356
|
+
},
|
|
3357
|
+
{
|
|
3358
|
+
title: "MCP tools \u2014 wiki, documents, and content",
|
|
3359
|
+
domain: "tool-use",
|
|
3360
|
+
priority: "p1",
|
|
3361
|
+
content: "create_wiki_page: create a wiki page in exe-wiki. list_wiki_pages: browse wiki pages. get_wiki_page: read a wiki page. update_wiki_page: edit a wiki page. ingest_document: import a file (PDF, MD, etc.) into memory as chunks. list_documents: browse ingested documents by workspace. purge_document: remove a document and its memory chunks. set_document_importance: adjust chunk importance scores. rerank_documents: re-score document relevance for a query."
|
|
3362
|
+
},
|
|
3363
|
+
{
|
|
3364
|
+
title: "MCP tools \u2014 system, operations, and admin",
|
|
3365
|
+
domain: "tool-use",
|
|
3366
|
+
priority: "p1",
|
|
3367
|
+
content: "get_agent_spend: token usage per agent/session (cost tracking). list_agent_sessions: view agent session history. get_session_kills: audit log of killed sessions. get_daemon_health: check exed daemon status. get_auto_wake_status: daemon auto-wake configuration. get_worker_gate: check worker deployment gates. run_memory_audit: health check \u2014 duplicates, null vectors, orphaned rows. run_consolidation: trigger sleep-time memory consolidation. cloud_sync: force a cloud sync cycle. backup_vps: trigger VPS backup."
|
|
3368
|
+
},
|
|
3369
|
+
{
|
|
3370
|
+
title: "MCP tools \u2014 config, licensing, and team",
|
|
3371
|
+
domain: "tool-use",
|
|
3372
|
+
priority: "p1",
|
|
3373
|
+
content: "set_agent_config: view/change per-agent runtime and model settings. list_employees: view the employee roster. add_person: add a person to the CRM contacts roster. list_people: browse CRM contacts. get_person: fetch contact details. get_license_status: check license validity. create_license: generate a new license key (admin). list_licenses: view all issued licenses. activate_license: activate a license on a device."
|
|
3374
|
+
},
|
|
3375
|
+
{
|
|
3376
|
+
title: "MCP tools \u2014 advanced (triggers, skills, orchestration)",
|
|
3377
|
+
domain: "tool-use",
|
|
3378
|
+
priority: "p1",
|
|
3379
|
+
content: "create_trigger: set up a scheduled recurring agent job (cron). list_triggers: view active triggers. load_skill: load a slash-command skill dynamically. apply_starter_pack: import a pre-built behavior + identity pack for a role. export_orchestration: export full org state (tasks, behaviors, identities) as portable JSON. import_orchestration: import org state into a new instance. deploy_client: deploy a customer client instance. query_company_brain: unified RAG query across all company knowledge. create_reminder: set a text reminder (shown in boot brief). list_reminders: view pending reminders. complete_reminder: mark a reminder done. global_procedure: manage Layer 0 procedures (actions: store, list, deactivate). Legacy aliases: store_global_procedure, list_global_procedures, deactivate_global_procedure."
|
|
2405
3380
|
}
|
|
2406
3381
|
];
|
|
2407
3382
|
PLATFORM_PROCEDURE_TITLES = new Set(
|
|
@@ -2418,7 +3393,7 @@ __export(global_procedures_exports, {
|
|
|
2418
3393
|
loadGlobalProcedures: () => loadGlobalProcedures,
|
|
2419
3394
|
storeGlobalProcedure: () => storeGlobalProcedure
|
|
2420
3395
|
});
|
|
2421
|
-
import { randomUUID } from "crypto";
|
|
3396
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
2422
3397
|
async function loadGlobalProcedures() {
|
|
2423
3398
|
const client = getClient();
|
|
2424
3399
|
const result = await client.execute({
|
|
@@ -2447,7 +3422,7 @@ ${sections.join("\n\n")}
|
|
|
2447
3422
|
`;
|
|
2448
3423
|
}
|
|
2449
3424
|
async function storeGlobalProcedure(input) {
|
|
2450
|
-
const id =
|
|
3425
|
+
const id = randomUUID2();
|
|
2451
3426
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
2452
3427
|
const client = getClient();
|
|
2453
3428
|
await client.execute({
|
|
@@ -2481,120 +3456,25 @@ ${p.content}`).join("\n\n");
|
|
|
2481
3456
|
}
|
|
2482
3457
|
});
|
|
2483
3458
|
|
|
2484
|
-
// src/bin/graph-export.ts
|
|
2485
|
-
import { writeFileSync as writeFileSync2, mkdirSync as mkdirSync3, existsSync as existsSync6 } from "fs";
|
|
2486
|
-
import path6 from "path";
|
|
2487
|
-
|
|
2488
3459
|
// src/lib/store.ts
|
|
3460
|
+
var store_exports = {};
|
|
3461
|
+
__export(store_exports, {
|
|
3462
|
+
attachDocumentMetadata: () => attachDocumentMetadata,
|
|
3463
|
+
buildRawVisibilityFilter: () => buildRawVisibilityFilter,
|
|
3464
|
+
buildWikiScopeFilter: () => buildWikiScopeFilter,
|
|
3465
|
+
classifyTier: () => classifyTier,
|
|
3466
|
+
disposeStore: () => disposeStore,
|
|
3467
|
+
flushBatch: () => flushBatch,
|
|
3468
|
+
flushTier3: () => flushTier3,
|
|
3469
|
+
getMemoryCardinality: () => getMemoryCardinality,
|
|
3470
|
+
initStore: () => initStore,
|
|
3471
|
+
reserveVersions: () => reserveVersions,
|
|
3472
|
+
searchMemories: () => searchMemories,
|
|
3473
|
+
updateMemoryStatus: () => updateMemoryStatus,
|
|
3474
|
+
vectorToBlob: () => vectorToBlob,
|
|
3475
|
+
writeMemory: () => writeMemory
|
|
3476
|
+
});
|
|
2489
3477
|
import { createHash } from "crypto";
|
|
2490
|
-
init_database();
|
|
2491
|
-
|
|
2492
|
-
// src/lib/keychain.ts
|
|
2493
|
-
import { readFile as readFile3, writeFile as writeFile3, unlink, mkdir as mkdir3, chmod as chmod2 } from "fs/promises";
|
|
2494
|
-
import { existsSync as existsSync4 } from "fs";
|
|
2495
|
-
import path4 from "path";
|
|
2496
|
-
import os4 from "os";
|
|
2497
|
-
var SERVICE = "exe-mem";
|
|
2498
|
-
var ACCOUNT = "master-key";
|
|
2499
|
-
function getKeyDir() {
|
|
2500
|
-
return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? path4.join(os4.homedir(), ".exe-os");
|
|
2501
|
-
}
|
|
2502
|
-
function getKeyPath() {
|
|
2503
|
-
return path4.join(getKeyDir(), "master.key");
|
|
2504
|
-
}
|
|
2505
|
-
async function tryKeytar() {
|
|
2506
|
-
try {
|
|
2507
|
-
return await import("keytar");
|
|
2508
|
-
} catch {
|
|
2509
|
-
return null;
|
|
2510
|
-
}
|
|
2511
|
-
}
|
|
2512
|
-
async function getMasterKey() {
|
|
2513
|
-
const keytar = await tryKeytar();
|
|
2514
|
-
if (keytar) {
|
|
2515
|
-
try {
|
|
2516
|
-
const stored = await keytar.getPassword(SERVICE, ACCOUNT);
|
|
2517
|
-
if (stored) {
|
|
2518
|
-
return Buffer.from(stored, "base64");
|
|
2519
|
-
}
|
|
2520
|
-
} catch {
|
|
2521
|
-
}
|
|
2522
|
-
}
|
|
2523
|
-
const keyPath = getKeyPath();
|
|
2524
|
-
if (!existsSync4(keyPath)) {
|
|
2525
|
-
process.stderr.write(
|
|
2526
|
-
`[keychain] Key not found at ${keyPath} (HOME=${os4.homedir()}, EXE_OS_DIR=${process.env.EXE_OS_DIR ?? "unset"})
|
|
2527
|
-
`
|
|
2528
|
-
);
|
|
2529
|
-
return null;
|
|
2530
|
-
}
|
|
2531
|
-
try {
|
|
2532
|
-
const content = await readFile3(keyPath, "utf-8");
|
|
2533
|
-
return Buffer.from(content.trim(), "base64");
|
|
2534
|
-
} catch (err) {
|
|
2535
|
-
process.stderr.write(
|
|
2536
|
-
`[keychain] Key read failed at ${keyPath}: ${err instanceof Error ? err.message : String(err)}
|
|
2537
|
-
`
|
|
2538
|
-
);
|
|
2539
|
-
return null;
|
|
2540
|
-
}
|
|
2541
|
-
}
|
|
2542
|
-
|
|
2543
|
-
// src/lib/store.ts
|
|
2544
|
-
init_config();
|
|
2545
|
-
|
|
2546
|
-
// src/lib/state-bus.ts
|
|
2547
|
-
var StateBus = class {
|
|
2548
|
-
handlers = /* @__PURE__ */ new Map();
|
|
2549
|
-
globalHandlers = /* @__PURE__ */ new Set();
|
|
2550
|
-
/** Emit an event to all subscribers */
|
|
2551
|
-
emit(event) {
|
|
2552
|
-
const typeHandlers = this.handlers.get(event.type);
|
|
2553
|
-
if (typeHandlers) {
|
|
2554
|
-
for (const handler of typeHandlers) {
|
|
2555
|
-
try {
|
|
2556
|
-
handler(event);
|
|
2557
|
-
} catch {
|
|
2558
|
-
}
|
|
2559
|
-
}
|
|
2560
|
-
}
|
|
2561
|
-
for (const handler of this.globalHandlers) {
|
|
2562
|
-
try {
|
|
2563
|
-
handler(event);
|
|
2564
|
-
} catch {
|
|
2565
|
-
}
|
|
2566
|
-
}
|
|
2567
|
-
}
|
|
2568
|
-
/** Subscribe to a specific event type */
|
|
2569
|
-
on(type, handler) {
|
|
2570
|
-
if (!this.handlers.has(type)) {
|
|
2571
|
-
this.handlers.set(type, /* @__PURE__ */ new Set());
|
|
2572
|
-
}
|
|
2573
|
-
this.handlers.get(type).add(handler);
|
|
2574
|
-
}
|
|
2575
|
-
/** Subscribe to ALL events */
|
|
2576
|
-
onAny(handler) {
|
|
2577
|
-
this.globalHandlers.add(handler);
|
|
2578
|
-
}
|
|
2579
|
-
/** Unsubscribe from a specific event type */
|
|
2580
|
-
off(type, handler) {
|
|
2581
|
-
this.handlers.get(type)?.delete(handler);
|
|
2582
|
-
}
|
|
2583
|
-
/** Unsubscribe from ALL events */
|
|
2584
|
-
offAny(handler) {
|
|
2585
|
-
this.globalHandlers.delete(handler);
|
|
2586
|
-
}
|
|
2587
|
-
/** Remove all listeners */
|
|
2588
|
-
clear() {
|
|
2589
|
-
this.handlers.clear();
|
|
2590
|
-
this.globalHandlers.clear();
|
|
2591
|
-
}
|
|
2592
|
-
};
|
|
2593
|
-
var orgBus = new StateBus();
|
|
2594
|
-
|
|
2595
|
-
// src/lib/store.ts
|
|
2596
|
-
var INIT_MAX_RETRIES = 3;
|
|
2597
|
-
var INIT_RETRY_DELAY_MS = 1e3;
|
|
2598
3478
|
function isBusyError2(err) {
|
|
2599
3479
|
if (err instanceof Error) {
|
|
2600
3480
|
const msg = err.message.toLowerCase();
|
|
@@ -2617,12 +3497,6 @@ async function retryOnBusy2(fn, label) {
|
|
|
2617
3497
|
}
|
|
2618
3498
|
throw new Error("unreachable");
|
|
2619
3499
|
}
|
|
2620
|
-
var _pendingRecords = [];
|
|
2621
|
-
var _batchSize = 20;
|
|
2622
|
-
var _flushIntervalMs = 1e4;
|
|
2623
|
-
var _flushTimer = null;
|
|
2624
|
-
var _flushing = false;
|
|
2625
|
-
var _nextVersion = 1;
|
|
2626
3500
|
async function initStore(options) {
|
|
2627
3501
|
if (_flushTimer !== null) {
|
|
2628
3502
|
clearInterval(_flushTimer);
|
|
@@ -2652,6 +3526,11 @@ async function initStore(options) {
|
|
|
2652
3526
|
encryptionKey: hexKey
|
|
2653
3527
|
});
|
|
2654
3528
|
await retryOnBusy2(() => ensureSchema(), "ensureSchema");
|
|
3529
|
+
try {
|
|
3530
|
+
const { initDaemonClient: initDaemonClient2 } = await Promise.resolve().then(() => (init_database(), database_exports));
|
|
3531
|
+
await initDaemonClient2();
|
|
3532
|
+
} catch {
|
|
3533
|
+
}
|
|
2655
3534
|
if (!options?.lightweight) {
|
|
2656
3535
|
try {
|
|
2657
3536
|
const { initShardManager: initShardManager2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|
|
@@ -2671,6 +3550,129 @@ async function initStore(options) {
|
|
|
2671
3550
|
}
|
|
2672
3551
|
}
|
|
2673
3552
|
}
|
|
3553
|
+
function classifyTier(record) {
|
|
3554
|
+
if (record.tool_name === "commit_to_long_term_memory" && (record.importance ?? 0) >= 8) return 1;
|
|
3555
|
+
if (["store_memory", "manual"].includes(record.tool_name ?? "") && (record.importance ?? 0) >= 5) return 2;
|
|
3556
|
+
return 3;
|
|
3557
|
+
}
|
|
3558
|
+
function inferFilePaths(record) {
|
|
3559
|
+
if (!["Read", "Write", "Edit"].includes(record.tool_name)) return null;
|
|
3560
|
+
const firstLine = record.raw_text.split("\n")[0] ?? "";
|
|
3561
|
+
const match = firstLine.match(/(\/[\w./-]+\.\w+)/);
|
|
3562
|
+
return match ? JSON.stringify([match[1]]) : null;
|
|
3563
|
+
}
|
|
3564
|
+
function inferCommitHash(record) {
|
|
3565
|
+
if (record.tool_name !== "Bash") return null;
|
|
3566
|
+
const match = record.raw_text.match(/\b([a-f0-9]{7,40})\b/);
|
|
3567
|
+
return match ? match[1] : null;
|
|
3568
|
+
}
|
|
3569
|
+
function inferLanguageType(record) {
|
|
3570
|
+
const text = record.raw_text;
|
|
3571
|
+
if (!text || text.length < 10) return null;
|
|
3572
|
+
const trimmed = text.trimStart();
|
|
3573
|
+
if (trimmed.startsWith("{") || trimmed.startsWith("[")) return "json";
|
|
3574
|
+
if (/\b(SELECT|INSERT|UPDATE|DELETE|CREATE TABLE|ALTER TABLE)\b/i.test(text)) return "sql";
|
|
3575
|
+
if (/\b(function |const |import |export |class |def |async |=>)\b/.test(text)) return "code";
|
|
3576
|
+
if (trimmed.startsWith("#") || trimmed.startsWith("*")) return "prose";
|
|
3577
|
+
return "mixed";
|
|
3578
|
+
}
|
|
3579
|
+
function inferDomain(record) {
|
|
3580
|
+
const proj = (record.project_name ?? "").toLowerCase();
|
|
3581
|
+
if (proj.includes("marketing") || proj.includes("content")) return "marketing";
|
|
3582
|
+
if (proj.includes("crm") || proj.includes("customer")) return "customer";
|
|
3583
|
+
return null;
|
|
3584
|
+
}
|
|
3585
|
+
async function writeMemory(record) {
|
|
3586
|
+
if (record.vector !== null && record.vector.length !== EMBEDDING_DIM) {
|
|
3587
|
+
throw new Error(
|
|
3588
|
+
`Expected ${EMBEDDING_DIM}-dim vector, got ${record.vector.length}`
|
|
3589
|
+
);
|
|
3590
|
+
}
|
|
3591
|
+
const contentHash = createHash("md5").update(record.raw_text).digest("hex");
|
|
3592
|
+
if (_pendingRecords.some((r) => r.content_hash === contentHash && r.agent_id === record.agent_id)) {
|
|
3593
|
+
return;
|
|
3594
|
+
}
|
|
3595
|
+
try {
|
|
3596
|
+
const client = getClient();
|
|
3597
|
+
const existing = await client.execute({
|
|
3598
|
+
sql: "SELECT id FROM memories WHERE content_hash = ? AND agent_id = ? LIMIT 1",
|
|
3599
|
+
args: [contentHash, record.agent_id]
|
|
3600
|
+
});
|
|
3601
|
+
if (existing.rows.length > 0) return;
|
|
3602
|
+
} catch {
|
|
3603
|
+
}
|
|
3604
|
+
const dbRow = {
|
|
3605
|
+
id: record.id,
|
|
3606
|
+
agent_id: record.agent_id,
|
|
3607
|
+
agent_role: record.agent_role,
|
|
3608
|
+
session_id: record.session_id,
|
|
3609
|
+
timestamp: record.timestamp,
|
|
3610
|
+
tool_name: record.tool_name,
|
|
3611
|
+
project_name: record.project_name,
|
|
3612
|
+
has_error: record.has_error ? 1 : 0,
|
|
3613
|
+
raw_text: record.raw_text,
|
|
3614
|
+
vector: record.vector,
|
|
3615
|
+
version: 0,
|
|
3616
|
+
// Placeholder — assigned atomically at flush time
|
|
3617
|
+
task_id: record.task_id ?? null,
|
|
3618
|
+
importance: record.importance ?? 5,
|
|
3619
|
+
status: record.status ?? "active",
|
|
3620
|
+
confidence: record.confidence ?? 0.7,
|
|
3621
|
+
last_accessed: record.last_accessed ?? record.timestamp,
|
|
3622
|
+
workspace_id: record.workspace_id ?? null,
|
|
3623
|
+
document_id: record.document_id ?? null,
|
|
3624
|
+
user_id: record.user_id ?? null,
|
|
3625
|
+
char_offset: record.char_offset ?? null,
|
|
3626
|
+
page_number: record.page_number ?? null,
|
|
3627
|
+
source_path: record.source_path ?? null,
|
|
3628
|
+
source_type: record.source_type ?? null,
|
|
3629
|
+
tier: record.tier ?? classifyTier(record),
|
|
3630
|
+
supersedes_id: record.supersedes_id ?? null,
|
|
3631
|
+
draft: record.draft ? 1 : 0,
|
|
3632
|
+
memory_type: record.memory_type ?? "raw",
|
|
3633
|
+
trajectory: record.trajectory ? JSON.stringify(record.trajectory) : null,
|
|
3634
|
+
content_hash: contentHash,
|
|
3635
|
+
intent: record.intent ?? null,
|
|
3636
|
+
outcome: record.outcome ?? null,
|
|
3637
|
+
domain: record.domain ?? inferDomain(record),
|
|
3638
|
+
referenced_entities: record.referenced_entities ?? null,
|
|
3639
|
+
retrieval_count: record.retrieval_count ?? 0,
|
|
3640
|
+
chain_position: record.chain_position ?? null,
|
|
3641
|
+
review_status: record.review_status ?? null,
|
|
3642
|
+
context_window_pct: record.context_window_pct ?? null,
|
|
3643
|
+
file_paths: record.file_paths ?? inferFilePaths(record),
|
|
3644
|
+
commit_hash: record.commit_hash ?? inferCommitHash(record),
|
|
3645
|
+
duration_ms: record.duration_ms ?? null,
|
|
3646
|
+
token_cost: record.token_cost ?? null,
|
|
3647
|
+
audience: record.audience ?? null,
|
|
3648
|
+
language_type: record.language_type ?? inferLanguageType(record),
|
|
3649
|
+
parent_memory_id: record.parent_memory_id ?? null
|
|
3650
|
+
};
|
|
3651
|
+
_pendingRecords.push(dbRow);
|
|
3652
|
+
orgBus.emit({
|
|
3653
|
+
type: "memory_stored",
|
|
3654
|
+
agentId: record.agent_id,
|
|
3655
|
+
project: record.project_name,
|
|
3656
|
+
timestamp: record.timestamp
|
|
3657
|
+
});
|
|
3658
|
+
const MAX_PENDING = 1e3;
|
|
3659
|
+
if (_pendingRecords.length > MAX_PENDING) {
|
|
3660
|
+
const dropped = _pendingRecords.length - MAX_PENDING;
|
|
3661
|
+
_pendingRecords = _pendingRecords.slice(-MAX_PENDING);
|
|
3662
|
+
console.warn(`[store] Dropped ${dropped} oldest pending records (overflow)`);
|
|
3663
|
+
}
|
|
3664
|
+
if (_flushTimer === null) {
|
|
3665
|
+
_flushTimer = setInterval(() => {
|
|
3666
|
+
void flushBatch();
|
|
3667
|
+
}, _flushIntervalMs);
|
|
3668
|
+
if (_flushTimer && typeof _flushTimer === "object" && "unref" in _flushTimer) {
|
|
3669
|
+
_flushTimer.unref();
|
|
3670
|
+
}
|
|
3671
|
+
}
|
|
3672
|
+
if (_pendingRecords.length >= _batchSize) {
|
|
3673
|
+
await flushBatch();
|
|
3674
|
+
}
|
|
3675
|
+
}
|
|
2674
3676
|
async function flushBatch() {
|
|
2675
3677
|
if (_flushing || _pendingRecords.length === 0) return 0;
|
|
2676
3678
|
_flushing = true;
|
|
@@ -2816,6 +3818,170 @@ async function flushBatch() {
|
|
|
2816
3818
|
_flushing = false;
|
|
2817
3819
|
}
|
|
2818
3820
|
}
|
|
3821
|
+
function buildWikiScopeFilter(options, columnPrefix) {
|
|
3822
|
+
const args = [];
|
|
3823
|
+
let clause = "";
|
|
3824
|
+
if (options?.workspaceId !== void 0) {
|
|
3825
|
+
clause += ` AND ${columnPrefix}workspace_id = ?`;
|
|
3826
|
+
args.push(options.workspaceId);
|
|
3827
|
+
}
|
|
3828
|
+
if (options?.userId === void 0) {
|
|
3829
|
+
clause += ` AND ${columnPrefix}user_id IS NULL`;
|
|
3830
|
+
} else if (options.userId === null) {
|
|
3831
|
+
clause += ` AND ${columnPrefix}user_id IS NULL`;
|
|
3832
|
+
} else {
|
|
3833
|
+
clause += ` AND (${columnPrefix}user_id = ? OR ${columnPrefix}user_id IS NULL)`;
|
|
3834
|
+
args.push(options.userId);
|
|
3835
|
+
}
|
|
3836
|
+
return { clause, args };
|
|
3837
|
+
}
|
|
3838
|
+
function buildRawVisibilityFilter(options, columnPrefix) {
|
|
3839
|
+
if (options?.includeRaw === false) {
|
|
3840
|
+
return {
|
|
3841
|
+
clause: ` AND COALESCE(${columnPrefix}memory_type, 'raw') != 'raw'`,
|
|
3842
|
+
args: []
|
|
3843
|
+
};
|
|
3844
|
+
}
|
|
3845
|
+
return { clause: "", args: [] };
|
|
3846
|
+
}
|
|
3847
|
+
async function searchMemories(queryVector, agentId, options) {
|
|
3848
|
+
let client;
|
|
3849
|
+
try {
|
|
3850
|
+
const { isShardingEnabled: isShardingEnabled2, shardExists: shardExists2, getReadyShardClient: getReadyShardClient2 } = await Promise.resolve().then(() => (init_shard_manager(), shard_manager_exports));
|
|
3851
|
+
if (isShardingEnabled2() && options?.projectName && shardExists2(options.projectName)) {
|
|
3852
|
+
client = await getReadyShardClient2(options.projectName);
|
|
3853
|
+
} else {
|
|
3854
|
+
client = getClient();
|
|
3855
|
+
}
|
|
3856
|
+
} catch {
|
|
3857
|
+
client = getClient();
|
|
3858
|
+
}
|
|
3859
|
+
const limit = options?.limit ?? 10;
|
|
3860
|
+
const statusFilter = options?.includeArchived ? "" : `
|
|
3861
|
+
AND COALESCE(status, 'active') = 'active'`;
|
|
3862
|
+
const draftFilter = options?.includeDrafts ? "" : `
|
|
3863
|
+
AND (draft = 0 OR draft IS NULL)`;
|
|
3864
|
+
let sql = `SELECT id, agent_id, agent_role, session_id, timestamp,
|
|
3865
|
+
tool_name, project_name,
|
|
3866
|
+
has_error, raw_text, vector, importance, status,
|
|
3867
|
+
confidence, last_accessed,
|
|
3868
|
+
workspace_id, document_id, user_id,
|
|
3869
|
+
char_offset, page_number,
|
|
3870
|
+
source_path, source_type
|
|
3871
|
+
FROM memories
|
|
3872
|
+
WHERE agent_id = ?
|
|
3873
|
+
AND vector IS NOT NULL${statusFilter}${draftFilter}
|
|
3874
|
+
AND COALESCE(confidence, 0.7) >= 0.3`;
|
|
3875
|
+
const args = [agentId];
|
|
3876
|
+
const scope = buildWikiScopeFilter(options, "");
|
|
3877
|
+
sql += scope.clause;
|
|
3878
|
+
args.push(...scope.args);
|
|
3879
|
+
const rawVisibility = buildRawVisibilityFilter(options, "");
|
|
3880
|
+
sql += rawVisibility.clause;
|
|
3881
|
+
args.push(...rawVisibility.args);
|
|
3882
|
+
if (options?.projectName) {
|
|
3883
|
+
sql += ` AND project_name = ?`;
|
|
3884
|
+
args.push(options.projectName);
|
|
3885
|
+
}
|
|
3886
|
+
if (options?.toolName) {
|
|
3887
|
+
sql += ` AND tool_name = ?`;
|
|
3888
|
+
args.push(options.toolName);
|
|
3889
|
+
}
|
|
3890
|
+
if (options?.hasError !== void 0) {
|
|
3891
|
+
sql += ` AND has_error = ?`;
|
|
3892
|
+
args.push(options.hasError ? 1 : 0);
|
|
3893
|
+
}
|
|
3894
|
+
if (options?.since) {
|
|
3895
|
+
sql += ` AND timestamp >= ?`;
|
|
3896
|
+
args.push(options.since);
|
|
3897
|
+
}
|
|
3898
|
+
if (options?.memoryType) {
|
|
3899
|
+
sql += ` AND memory_type = ?`;
|
|
3900
|
+
args.push(options.memoryType);
|
|
3901
|
+
}
|
|
3902
|
+
sql += ` ORDER BY vector_distance_cos(vector, vector32(?))`;
|
|
3903
|
+
args.push(vectorToBlob(queryVector));
|
|
3904
|
+
sql += ` LIMIT ?`;
|
|
3905
|
+
args.push(limit);
|
|
3906
|
+
const result = await client.execute({ sql, args });
|
|
3907
|
+
return result.rows.map((row) => ({
|
|
3908
|
+
id: row.id,
|
|
3909
|
+
agent_id: row.agent_id,
|
|
3910
|
+
agent_role: row.agent_role,
|
|
3911
|
+
session_id: row.session_id,
|
|
3912
|
+
timestamp: row.timestamp,
|
|
3913
|
+
tool_name: row.tool_name,
|
|
3914
|
+
project_name: row.project_name,
|
|
3915
|
+
has_error: row.has_error === 1,
|
|
3916
|
+
raw_text: row.raw_text,
|
|
3917
|
+
vector: row.vector == null ? [] : Array.isArray(row.vector) ? row.vector : Array.from(row.vector),
|
|
3918
|
+
importance: row.importance ?? 5,
|
|
3919
|
+
status: row.status ?? "active",
|
|
3920
|
+
confidence: row.confidence ?? 0.7,
|
|
3921
|
+
last_accessed: row.last_accessed ?? row.timestamp,
|
|
3922
|
+
workspace_id: row.workspace_id ?? null,
|
|
3923
|
+
document_id: row.document_id ?? null,
|
|
3924
|
+
user_id: row.user_id ?? null,
|
|
3925
|
+
char_offset: row.char_offset ?? null,
|
|
3926
|
+
page_number: row.page_number ?? null,
|
|
3927
|
+
source_path: row.source_path ?? null,
|
|
3928
|
+
source_type: row.source_type ?? null
|
|
3929
|
+
}));
|
|
3930
|
+
}
|
|
3931
|
+
async function attachDocumentMetadata(records) {
|
|
3932
|
+
const docIds = [
|
|
3933
|
+
...new Set(
|
|
3934
|
+
records.map((r) => r.document_id).filter((id) => typeof id === "string" && id.length > 0)
|
|
3935
|
+
)
|
|
3936
|
+
];
|
|
3937
|
+
if (docIds.length === 0) return records;
|
|
3938
|
+
try {
|
|
3939
|
+
const client = getClient();
|
|
3940
|
+
const placeholders = docIds.map(() => "?").join(",");
|
|
3941
|
+
const result = await client.execute({
|
|
3942
|
+
sql: `SELECT id, filename, mime, source_type, uploaded_at
|
|
3943
|
+
FROM documents
|
|
3944
|
+
WHERE id IN (${placeholders})`,
|
|
3945
|
+
args: docIds
|
|
3946
|
+
});
|
|
3947
|
+
const byId = /* @__PURE__ */ new Map();
|
|
3948
|
+
for (const row of result.rows) {
|
|
3949
|
+
const id = row.id;
|
|
3950
|
+
byId.set(id, {
|
|
3951
|
+
document_id: id,
|
|
3952
|
+
filename: row.filename,
|
|
3953
|
+
mime: row.mime ?? null,
|
|
3954
|
+
source_type: row.source_type ?? null,
|
|
3955
|
+
uploaded_at: row.uploaded_at
|
|
3956
|
+
});
|
|
3957
|
+
}
|
|
3958
|
+
for (const record of records) {
|
|
3959
|
+
if (!record.document_id) continue;
|
|
3960
|
+
record.document_metadata = byId.get(record.document_id) ?? null;
|
|
3961
|
+
}
|
|
3962
|
+
} catch {
|
|
3963
|
+
}
|
|
3964
|
+
return records;
|
|
3965
|
+
}
|
|
3966
|
+
async function flushTier3(agentId, options) {
|
|
3967
|
+
const client = getClient();
|
|
3968
|
+
const maxAge = options?.maxAgeHours ?? 72;
|
|
3969
|
+
const cutoff = new Date(Date.now() - maxAge * 36e5).toISOString();
|
|
3970
|
+
if (options?.dryRun) {
|
|
3971
|
+
const result2 = await client.execute({
|
|
3972
|
+
sql: `SELECT COUNT(*) as cnt FROM memories
|
|
3973
|
+
WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
|
|
3974
|
+
args: [agentId, cutoff]
|
|
3975
|
+
});
|
|
3976
|
+
return { archived: Number(result2.rows[0]?.cnt ?? 0) };
|
|
3977
|
+
}
|
|
3978
|
+
const result = await client.execute({
|
|
3979
|
+
sql: `UPDATE memories SET status = 'archived'
|
|
3980
|
+
WHERE agent_id = ? AND tier = 3 AND status = 'active' AND timestamp < ?`,
|
|
3981
|
+
args: [agentId, cutoff]
|
|
3982
|
+
});
|
|
3983
|
+
return { archived: result.rowsAffected };
|
|
3984
|
+
}
|
|
2819
3985
|
async function disposeStore() {
|
|
2820
3986
|
if (_flushTimer !== null) {
|
|
2821
3987
|
clearInterval(_flushTimer);
|
|
@@ -2832,6 +3998,118 @@ function vectorToBlob(vector) {
|
|
|
2832
3998
|
const f32 = vector instanceof Float32Array ? vector : new Float32Array(vector);
|
|
2833
3999
|
return JSON.stringify(Array.from(f32));
|
|
2834
4000
|
}
|
|
4001
|
+
async function updateMemoryStatus(id, status) {
|
|
4002
|
+
const client = getClient();
|
|
4003
|
+
await client.execute({
|
|
4004
|
+
sql: `UPDATE memories SET status = ? WHERE id = ?`,
|
|
4005
|
+
args: [status, id]
|
|
4006
|
+
});
|
|
4007
|
+
}
|
|
4008
|
+
function reserveVersions(count) {
|
|
4009
|
+
const reserved = [];
|
|
4010
|
+
for (let i = 0; i < count; i++) {
|
|
4011
|
+
reserved.push(_nextVersion++);
|
|
4012
|
+
}
|
|
4013
|
+
return reserved;
|
|
4014
|
+
}
|
|
4015
|
+
async function getMemoryCardinality(agentId) {
|
|
4016
|
+
try {
|
|
4017
|
+
const client = getClient();
|
|
4018
|
+
const result = await client.execute({
|
|
4019
|
+
sql: `SELECT COUNT(*) as cnt FROM memories WHERE agent_id = ? AND COALESCE(status, 'active') = 'active'`,
|
|
4020
|
+
args: [agentId]
|
|
4021
|
+
});
|
|
4022
|
+
return Number(result.rows[0]?.cnt) || 0;
|
|
4023
|
+
} catch {
|
|
4024
|
+
return 0;
|
|
4025
|
+
}
|
|
4026
|
+
}
|
|
4027
|
+
var INIT_MAX_RETRIES, INIT_RETRY_DELAY_MS, _pendingRecords, _batchSize, _flushIntervalMs, _flushTimer, _flushing, _nextVersion;
|
|
4028
|
+
var init_store = __esm({
|
|
4029
|
+
"src/lib/store.ts"() {
|
|
4030
|
+
"use strict";
|
|
4031
|
+
init_memory();
|
|
4032
|
+
init_database();
|
|
4033
|
+
init_keychain();
|
|
4034
|
+
init_config();
|
|
4035
|
+
init_state_bus();
|
|
4036
|
+
INIT_MAX_RETRIES = 3;
|
|
4037
|
+
INIT_RETRY_DELAY_MS = 1e3;
|
|
4038
|
+
_pendingRecords = [];
|
|
4039
|
+
_batchSize = 20;
|
|
4040
|
+
_flushIntervalMs = 1e4;
|
|
4041
|
+
_flushTimer = null;
|
|
4042
|
+
_flushing = false;
|
|
4043
|
+
_nextVersion = 1;
|
|
4044
|
+
}
|
|
4045
|
+
});
|
|
4046
|
+
|
|
4047
|
+
// src/bin/graph-export.ts
|
|
4048
|
+
init_store();
|
|
4049
|
+
import { writeFileSync as writeFileSync3, mkdirSync as mkdirSync3, existsSync as existsSync8 } from "fs";
|
|
4050
|
+
import path8 from "path";
|
|
4051
|
+
|
|
4052
|
+
// src/bin/fast-db-init.ts
|
|
4053
|
+
async function fastDbInit() {
|
|
4054
|
+
const { isInitialized: isInitialized2, getClient: getClient2 } = await Promise.resolve().then(() => (init_database(), database_exports));
|
|
4055
|
+
if (isInitialized2()) {
|
|
4056
|
+
return getClient2();
|
|
4057
|
+
}
|
|
4058
|
+
try {
|
|
4059
|
+
const { connectEmbedDaemon: connectEmbedDaemon2, sendDaemonRequest: sendDaemonRequest2, isClientConnected: isClientConnected2 } = await Promise.resolve().then(() => (init_exe_daemon_client(), exe_daemon_client_exports));
|
|
4060
|
+
const { deserializeResultSet: deserializeResultSet2 } = await Promise.resolve().then(() => (init_daemon_protocol(), daemon_protocol_exports));
|
|
4061
|
+
await connectEmbedDaemon2();
|
|
4062
|
+
if (isClientConnected2()) {
|
|
4063
|
+
const daemonClient = {
|
|
4064
|
+
async execute(stmt) {
|
|
4065
|
+
const sql = typeof stmt === "string" ? stmt : stmt.sql;
|
|
4066
|
+
const args = typeof stmt === "string" ? [] : Array.isArray(stmt.args) ? stmt.args : [];
|
|
4067
|
+
const resp = await sendDaemonRequest2({ type: "db-execute", sql, args });
|
|
4068
|
+
if (resp.error) throw new Error(String(resp.error));
|
|
4069
|
+
if (resp.db) return deserializeResultSet2(resp.db);
|
|
4070
|
+
throw new Error("Unexpected daemon response");
|
|
4071
|
+
},
|
|
4072
|
+
async batch(stmts, mode) {
|
|
4073
|
+
const statements = stmts.map((s) => {
|
|
4074
|
+
const sql = typeof s === "string" ? s : s.sql;
|
|
4075
|
+
const args = typeof s === "string" ? [] : Array.isArray(s.args) ? s.args : [];
|
|
4076
|
+
return { sql, args };
|
|
4077
|
+
});
|
|
4078
|
+
const resp = await sendDaemonRequest2({ type: "db-batch", statements, mode: mode ?? "deferred" });
|
|
4079
|
+
if (resp.error) throw new Error(String(resp.error));
|
|
4080
|
+
const batchResults = resp["db-batch"];
|
|
4081
|
+
if (batchResults) return batchResults.map(deserializeResultSet2);
|
|
4082
|
+
throw new Error("Unexpected daemon batch response");
|
|
4083
|
+
},
|
|
4084
|
+
async transaction(_mode) {
|
|
4085
|
+
throw new Error("Transactions not supported via daemon socket");
|
|
4086
|
+
},
|
|
4087
|
+
async executeMultiple(_sql) {
|
|
4088
|
+
throw new Error("executeMultiple not supported via daemon socket");
|
|
4089
|
+
},
|
|
4090
|
+
async migrate(_stmts) {
|
|
4091
|
+
throw new Error("migrate not supported via daemon socket");
|
|
4092
|
+
},
|
|
4093
|
+
sync() {
|
|
4094
|
+
return Promise.resolve(void 0);
|
|
4095
|
+
},
|
|
4096
|
+
close() {
|
|
4097
|
+
},
|
|
4098
|
+
get closed() {
|
|
4099
|
+
return false;
|
|
4100
|
+
},
|
|
4101
|
+
get protocol() {
|
|
4102
|
+
return "file";
|
|
4103
|
+
}
|
|
4104
|
+
};
|
|
4105
|
+
return daemonClient;
|
|
4106
|
+
}
|
|
4107
|
+
} catch {
|
|
4108
|
+
}
|
|
4109
|
+
const { initStore: initStore2 } = await Promise.resolve().then(() => (init_store(), store_exports));
|
|
4110
|
+
await initStore2({ lightweight: true });
|
|
4111
|
+
return getClient2();
|
|
4112
|
+
}
|
|
2835
4113
|
|
|
2836
4114
|
// src/bin/graph-export.ts
|
|
2837
4115
|
init_database();
|
|
@@ -3047,27 +4325,27 @@ async function main() {
|
|
|
3047
4325
|
const outIdx = args.indexOf("--output");
|
|
3048
4326
|
const output = outIdx >= 0 ? args[outIdx + 1] : void 0;
|
|
3049
4327
|
const reportOnly = args.includes("--report");
|
|
3050
|
-
await
|
|
4328
|
+
await fastDbInit();
|
|
3051
4329
|
const client = getClient();
|
|
3052
4330
|
if (reportOnly) {
|
|
3053
4331
|
const report = await generateGraphReport(client, project);
|
|
3054
4332
|
const reportPath = output ?? "exe/output/GRAPH_REPORT.md";
|
|
3055
|
-
const dir =
|
|
3056
|
-
if (!
|
|
3057
|
-
|
|
4333
|
+
const dir = path8.dirname(reportPath);
|
|
4334
|
+
if (!existsSync8(dir)) mkdirSync3(dir, { recursive: true });
|
|
4335
|
+
writeFileSync3(reportPath, report);
|
|
3058
4336
|
process.stderr.write(`[graph-export] Report written to ${reportPath}
|
|
3059
4337
|
`);
|
|
3060
4338
|
} else {
|
|
3061
4339
|
const html = await exportGraphHTML(client, project);
|
|
3062
4340
|
const htmlPath = output ?? "exe/output/graph.html";
|
|
3063
|
-
const dir =
|
|
3064
|
-
if (!
|
|
3065
|
-
|
|
4341
|
+
const dir = path8.dirname(htmlPath);
|
|
4342
|
+
if (!existsSync8(dir)) mkdirSync3(dir, { recursive: true });
|
|
4343
|
+
writeFileSync3(htmlPath, html);
|
|
3066
4344
|
process.stderr.write(`[graph-export] HTML written to ${htmlPath}
|
|
3067
4345
|
`);
|
|
3068
4346
|
const report = await generateGraphReport(client, project);
|
|
3069
4347
|
const reportPath = htmlPath.replace(".html", "-report.md");
|
|
3070
|
-
|
|
4348
|
+
writeFileSync3(reportPath, report);
|
|
3071
4349
|
process.stderr.write(`[graph-export] Report written to ${reportPath}
|
|
3072
4350
|
`);
|
|
3073
4351
|
}
|