@askexenow/exe-os 0.9.7 → 0.9.8
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 +754 -79
- package/dist/bin/backfill-responses.js +752 -77
- package/dist/bin/backfill-vectors.js +752 -77
- package/dist/bin/cleanup-stale-review-tasks.js +657 -35
- package/dist/bin/cli.js +1388 -605
- package/dist/bin/exe-agent-config.js +123 -95
- package/dist/bin/exe-agent.js +41 -25
- package/dist/bin/exe-assign.js +732 -57
- package/dist/bin/exe-boot.js +784 -153
- package/dist/bin/exe-call.js +209 -138
- package/dist/bin/exe-cloud.js +35 -12
- package/dist/bin/exe-dispatch.js +692 -70
- package/dist/bin/exe-doctor.js +648 -26
- package/dist/bin/exe-export-behaviors.js +650 -20
- package/dist/bin/exe-forget.js +635 -13
- package/dist/bin/exe-gateway.js +1053 -271
- package/dist/bin/exe-heartbeat.js +665 -43
- package/dist/bin/exe-kill.js +646 -16
- package/dist/bin/exe-launch-agent.js +887 -97
- package/dist/bin/exe-link.js +658 -43
- package/dist/bin/exe-new-employee.js +378 -177
- package/dist/bin/exe-pending-messages.js +656 -34
- package/dist/bin/exe-pending-notifications.js +635 -13
- package/dist/bin/exe-pending-reviews.js +659 -37
- package/dist/bin/exe-rename.js +645 -30
- package/dist/bin/exe-review.js +635 -13
- package/dist/bin/exe-search.js +771 -88
- package/dist/bin/exe-session-cleanup.js +834 -150
- package/dist/bin/exe-settings.js +127 -91
- package/dist/bin/exe-start-codex.js +729 -94
- package/dist/bin/exe-start-opencode.js +717 -82
- package/dist/bin/exe-status.js +657 -35
- package/dist/bin/exe-team.js +635 -13
- package/dist/bin/git-sweep.js +720 -89
- package/dist/bin/graph-backfill.js +643 -13
- package/dist/bin/graph-export.js +646 -16
- package/dist/bin/install.js +596 -193
- package/dist/bin/scan-tasks.js +724 -93
- package/dist/bin/setup.js +1038 -210
- package/dist/bin/shard-migrate.js +645 -15
- package/dist/bin/wiki-sync.js +646 -16
- package/dist/gateway/index.js +1027 -245
- package/dist/hooks/bug-report-worker.js +891 -170
- package/dist/hooks/commit-complete.js +718 -87
- package/dist/hooks/error-recall.js +776 -93
- package/dist/hooks/exe-heartbeat-hook.js +85 -71
- package/dist/hooks/ingest-worker.js +840 -156
- package/dist/hooks/ingest.js +90 -73
- package/dist/hooks/instructions-loaded.js +669 -38
- package/dist/hooks/notification.js +661 -30
- package/dist/hooks/post-compact.js +674 -43
- package/dist/hooks/pre-compact.js +718 -87
- package/dist/hooks/pre-tool-use.js +872 -125
- package/dist/hooks/prompt-ingest-worker.js +758 -83
- package/dist/hooks/prompt-submit.js +1060 -319
- package/dist/hooks/response-ingest-worker.js +758 -83
- package/dist/hooks/session-end.js +721 -90
- package/dist/hooks/session-start.js +1031 -207
- package/dist/hooks/stop.js +680 -49
- package/dist/hooks/subagent-stop.js +674 -43
- package/dist/hooks/summary-worker.js +816 -132
- package/dist/index.js +1015 -232
- package/dist/lib/cloud-sync.js +663 -48
- package/dist/lib/consolidation.js +26 -3
- package/dist/lib/database.js +626 -18
- package/dist/lib/db.js +2261 -0
- package/dist/lib/device-registry.js +640 -25
- package/dist/lib/embedder.js +96 -43
- package/dist/lib/employee-templates.js +16 -0
- package/dist/lib/employees.js +259 -83
- package/dist/lib/exe-daemon-client.js +101 -63
- package/dist/lib/exe-daemon.js +894 -162
- package/dist/lib/hybrid-search.js +771 -88
- package/dist/lib/identity.js +27 -7
- package/dist/lib/messaging.js +55 -28
- package/dist/lib/reminders.js +21 -1
- package/dist/lib/schedules.js +636 -14
- package/dist/lib/skill-learning.js +21 -1
- package/dist/lib/store.js +643 -13
- package/dist/lib/task-router.js +82 -71
- package/dist/lib/tasks.js +98 -71
- package/dist/lib/tmux-routing.js +87 -60
- package/dist/lib/token-spend.js +26 -6
- package/dist/mcp/server.js +1784 -458
- package/dist/mcp/tools/complete-reminder.js +21 -1
- package/dist/mcp/tools/create-reminder.js +21 -1
- package/dist/mcp/tools/create-task.js +290 -164
- package/dist/mcp/tools/deactivate-behavior.js +24 -4
- package/dist/mcp/tools/list-reminders.js +21 -1
- package/dist/mcp/tools/list-tasks.js +195 -38
- package/dist/mcp/tools/send-message.js +58 -31
- package/dist/mcp/tools/update-task.js +75 -48
- package/dist/runtime/index.js +720 -89
- package/dist/tui/App.js +853 -123
- package/package.json +3 -2
|
@@ -220,7 +220,7 @@ function loadEmployeesSync(employeesPath = EMPLOYEES_PATH) {
|
|
|
220
220
|
function getEmployee(employees, name) {
|
|
221
221
|
return employees.find((e) => e.name.toLowerCase() === name.toLowerCase());
|
|
222
222
|
}
|
|
223
|
-
var EMPLOYEES_PATH, DEFAULT_COORDINATOR_TEMPLATE_NAME, COORDINATOR_ROLE;
|
|
223
|
+
var EMPLOYEES_PATH, DEFAULT_COORDINATOR_TEMPLATE_NAME, COORDINATOR_ROLE, IDENTITY_DIR;
|
|
224
224
|
var init_employees = __esm({
|
|
225
225
|
"src/lib/employees.ts"() {
|
|
226
226
|
"use strict";
|
|
@@ -228,6 +228,7 @@ var init_employees = __esm({
|
|
|
228
228
|
EMPLOYEES_PATH = path2.join(EXE_AI_DIR, "exe-employees.json");
|
|
229
229
|
DEFAULT_COORDINATOR_TEMPLATE_NAME = "exe";
|
|
230
230
|
COORDINATOR_ROLE = "COO";
|
|
231
|
+
IDENTITY_DIR = path2.join(EXE_AI_DIR, "identity");
|
|
231
232
|
}
|
|
232
233
|
});
|
|
233
234
|
|
|
@@ -286,13 +287,597 @@ var init_db_retry = __esm({
|
|
|
286
287
|
}
|
|
287
288
|
});
|
|
288
289
|
|
|
290
|
+
// src/lib/database-adapter.ts
|
|
291
|
+
import os3 from "os";
|
|
292
|
+
import path4 from "path";
|
|
293
|
+
import { createRequire } from "module";
|
|
294
|
+
import { pathToFileURL } from "url";
|
|
295
|
+
function quotedIdentifier(identifier) {
|
|
296
|
+
return `"${identifier.replace(/"/g, '""')}"`;
|
|
297
|
+
}
|
|
298
|
+
function unqualifiedTableName(name) {
|
|
299
|
+
const raw = name.trim().replace(/^"|"$/g, "");
|
|
300
|
+
const parts = raw.split(".");
|
|
301
|
+
return parts[parts.length - 1].replace(/^"|"$/g, "").toLowerCase();
|
|
302
|
+
}
|
|
303
|
+
function stripTrailingSemicolon(sql) {
|
|
304
|
+
return sql.trim().replace(/;+\s*$/u, "");
|
|
305
|
+
}
|
|
306
|
+
function appendClause(sql, clause) {
|
|
307
|
+
const trimmed = stripTrailingSemicolon(sql);
|
|
308
|
+
const returningMatch = /\sRETURNING\b[\s\S]*$/iu.exec(trimmed);
|
|
309
|
+
if (!returningMatch) {
|
|
310
|
+
return `${trimmed}${clause}`;
|
|
311
|
+
}
|
|
312
|
+
const idx = returningMatch.index;
|
|
313
|
+
return `${trimmed.slice(0, idx)}${clause}${trimmed.slice(idx)}`;
|
|
314
|
+
}
|
|
315
|
+
function normalizeStatement(stmt) {
|
|
316
|
+
if (typeof stmt === "string") {
|
|
317
|
+
return { kind: "positional", sql: stmt, args: [] };
|
|
318
|
+
}
|
|
319
|
+
const sql = stmt.sql;
|
|
320
|
+
if (Array.isArray(stmt.args) || stmt.args === void 0) {
|
|
321
|
+
return { kind: "positional", sql, args: stmt.args ?? [] };
|
|
322
|
+
}
|
|
323
|
+
return { kind: "named", sql, args: stmt.args };
|
|
324
|
+
}
|
|
325
|
+
function rewriteBooleanLiterals(sql) {
|
|
326
|
+
let out = sql;
|
|
327
|
+
for (const column of BOOLEAN_COLUMN_NAMES) {
|
|
328
|
+
const scoped = `((?:\\b[a-z_][a-z0-9_]*\\.)?${column})`;
|
|
329
|
+
out = out.replace(new RegExp(`${scoped}\\s*=\\s*0\\b`, "giu"), "$1 = FALSE");
|
|
330
|
+
out = out.replace(new RegExp(`${scoped}\\s*=\\s*1\\b`, "giu"), "$1 = TRUE");
|
|
331
|
+
out = out.replace(new RegExp(`${scoped}\\s*!=\\s*0\\b`, "giu"), "$1 != FALSE");
|
|
332
|
+
out = out.replace(new RegExp(`${scoped}\\s*!=\\s*1\\b`, "giu"), "$1 != TRUE");
|
|
333
|
+
out = out.replace(new RegExp(`${scoped}\\s*<>\\s*0\\b`, "giu"), "$1 <> FALSE");
|
|
334
|
+
out = out.replace(new RegExp(`${scoped}\\s*<>\\s*1\\b`, "giu"), "$1 <> TRUE");
|
|
335
|
+
}
|
|
336
|
+
return out;
|
|
337
|
+
}
|
|
338
|
+
function rewriteInsertOrIgnore(sql) {
|
|
339
|
+
if (!/^\s*INSERT\s+OR\s+IGNORE\s+INTO\b/iu.test(sql)) {
|
|
340
|
+
return sql;
|
|
341
|
+
}
|
|
342
|
+
const replaced = sql.replace(/^\s*INSERT\s+OR\s+IGNORE\s+INTO\b/iu, "INSERT INTO");
|
|
343
|
+
return /\bON\s+CONFLICT\b/iu.test(replaced) ? replaced : appendClause(replaced, " ON CONFLICT DO NOTHING");
|
|
344
|
+
}
|
|
345
|
+
function rewriteInsertOrReplace(sql) {
|
|
346
|
+
const match = /^\s*INSERT\s+OR\s+REPLACE\s+INTO\s+([A-Za-z0-9_."]+)\s*\(([^)]+)\)([\s\S]*)$/iu.exec(sql);
|
|
347
|
+
if (!match) {
|
|
348
|
+
return sql;
|
|
349
|
+
}
|
|
350
|
+
const rawTable = match[1];
|
|
351
|
+
const rawColumns = match[2];
|
|
352
|
+
const remainder = match[3];
|
|
353
|
+
const tableName = unqualifiedTableName(rawTable);
|
|
354
|
+
const conflictKeys = UPSERT_KEYS[tableName];
|
|
355
|
+
if (!conflictKeys?.length) {
|
|
356
|
+
return sql;
|
|
357
|
+
}
|
|
358
|
+
const columns = rawColumns.split(",").map((col) => col.trim().replace(/^"|"$/g, ""));
|
|
359
|
+
const updateColumns = columns.filter((col) => !conflictKeys.includes(col));
|
|
360
|
+
const conflictTarget = conflictKeys.map(quotedIdentifier).join(", ");
|
|
361
|
+
const updateClause = updateColumns.length === 0 ? " DO NOTHING" : ` DO UPDATE SET ${updateColumns.map((col) => `${quotedIdentifier(col)} = EXCLUDED.${quotedIdentifier(col)}`).join(", ")}`;
|
|
362
|
+
return `INSERT INTO ${rawTable} (${rawColumns})${appendClause(remainder, ` ON CONFLICT (${conflictTarget})${updateClause}`)}`;
|
|
363
|
+
}
|
|
364
|
+
function rewriteSql(sql) {
|
|
365
|
+
let out = sql;
|
|
366
|
+
out = out.replace(/\bdatetime\(\s*['"]now['"]\s*\)/giu, "CURRENT_TIMESTAMP");
|
|
367
|
+
out = out.replace(/\bvector32\s*\(\s*\?\s*\)/giu, "?");
|
|
368
|
+
out = rewriteBooleanLiterals(out);
|
|
369
|
+
out = rewriteInsertOrReplace(out);
|
|
370
|
+
out = rewriteInsertOrIgnore(out);
|
|
371
|
+
return stripTrailingSemicolon(out);
|
|
372
|
+
}
|
|
373
|
+
function toBoolean(value) {
|
|
374
|
+
if (value === null || value === void 0) return value;
|
|
375
|
+
if (typeof value === "boolean") return value;
|
|
376
|
+
if (typeof value === "number") return value !== 0;
|
|
377
|
+
if (typeof value === "bigint") return value !== 0n;
|
|
378
|
+
if (typeof value === "string") {
|
|
379
|
+
const normalized = value.trim().toLowerCase();
|
|
380
|
+
if (normalized === "0" || normalized === "false") return false;
|
|
381
|
+
if (normalized === "1" || normalized === "true") return true;
|
|
382
|
+
}
|
|
383
|
+
return Boolean(value);
|
|
384
|
+
}
|
|
385
|
+
function countQuestionMarks(sql, end) {
|
|
386
|
+
let count = 0;
|
|
387
|
+
let inSingle = false;
|
|
388
|
+
let inDouble = false;
|
|
389
|
+
let inLineComment = false;
|
|
390
|
+
let inBlockComment = false;
|
|
391
|
+
for (let i = 0; i < end; i++) {
|
|
392
|
+
const ch = sql[i];
|
|
393
|
+
const next = sql[i + 1];
|
|
394
|
+
if (inLineComment) {
|
|
395
|
+
if (ch === "\n") inLineComment = false;
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
if (inBlockComment) {
|
|
399
|
+
if (ch === "*" && next === "/") {
|
|
400
|
+
inBlockComment = false;
|
|
401
|
+
i += 1;
|
|
402
|
+
}
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
if (!inSingle && !inDouble && ch === "-" && next === "-") {
|
|
406
|
+
inLineComment = true;
|
|
407
|
+
i += 1;
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
if (!inSingle && !inDouble && ch === "/" && next === "*") {
|
|
411
|
+
inBlockComment = true;
|
|
412
|
+
i += 1;
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
if (!inDouble && ch === "'" && sql[i - 1] !== "\\") {
|
|
416
|
+
inSingle = !inSingle;
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
if (!inSingle && ch === '"' && sql[i - 1] !== "\\") {
|
|
420
|
+
inDouble = !inDouble;
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
if (!inSingle && !inDouble && ch === "?") {
|
|
424
|
+
count += 1;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return count;
|
|
428
|
+
}
|
|
429
|
+
function findBooleanPlaceholderIndexes(sql) {
|
|
430
|
+
const indexes = /* @__PURE__ */ new Set();
|
|
431
|
+
for (const column of BOOLEAN_COLUMN_NAMES) {
|
|
432
|
+
const pattern = new RegExp(`(?:\\b[a-z_][a-z0-9_]*\\.)?${column}\\s*=\\s*\\?`, "giu");
|
|
433
|
+
for (const match of sql.matchAll(pattern)) {
|
|
434
|
+
const matchText = match[0];
|
|
435
|
+
const qIndex = match.index + matchText.lastIndexOf("?");
|
|
436
|
+
indexes.add(countQuestionMarks(sql, qIndex + 1));
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return indexes;
|
|
440
|
+
}
|
|
441
|
+
function coerceInsertBooleanArgs(sql, args) {
|
|
442
|
+
const match = /^\s*INSERT(?:\s+OR\s+(?:IGNORE|REPLACE))?\s+INTO\s+([A-Za-z0-9_."]+)\s*\(([^)]+)\)/iu.exec(sql);
|
|
443
|
+
if (!match) return;
|
|
444
|
+
const rawTable = match[1];
|
|
445
|
+
const rawColumns = match[2];
|
|
446
|
+
const boolColumns = BOOLEAN_COLUMNS_BY_TABLE[unqualifiedTableName(rawTable)];
|
|
447
|
+
if (!boolColumns?.size) return;
|
|
448
|
+
const columns = rawColumns.split(",").map((col) => col.trim().replace(/^"|"$/g, ""));
|
|
449
|
+
for (const [index, column] of columns.entries()) {
|
|
450
|
+
if (boolColumns.has(column) && index < args.length) {
|
|
451
|
+
args[index] = toBoolean(args[index]);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
function coerceUpdateBooleanArgs(sql, args) {
|
|
456
|
+
const match = /^\s*UPDATE\s+([A-Za-z0-9_."]+)\s+SET\s+([\s\S]+?)(?:\s+WHERE\b|$)/iu.exec(sql);
|
|
457
|
+
if (!match) return;
|
|
458
|
+
const rawTable = match[1];
|
|
459
|
+
const setClause = match[2];
|
|
460
|
+
const boolColumns = BOOLEAN_COLUMNS_BY_TABLE[unqualifiedTableName(rawTable)];
|
|
461
|
+
if (!boolColumns?.size) return;
|
|
462
|
+
const assignments = setClause.split(",");
|
|
463
|
+
let placeholderIndex = 0;
|
|
464
|
+
for (const assignment of assignments) {
|
|
465
|
+
if (!assignment.includes("?")) continue;
|
|
466
|
+
placeholderIndex += 1;
|
|
467
|
+
const colMatch = /^\s*(?:[A-Za-z_][A-Za-z0-9_]*\.)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*\?/iu.exec(assignment);
|
|
468
|
+
if (colMatch && boolColumns.has(colMatch[1])) {
|
|
469
|
+
args[placeholderIndex - 1] = toBoolean(args[placeholderIndex - 1]);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
function coerceBooleanArgs(sql, args) {
|
|
474
|
+
const nextArgs = [...args];
|
|
475
|
+
coerceInsertBooleanArgs(sql, nextArgs);
|
|
476
|
+
coerceUpdateBooleanArgs(sql, nextArgs);
|
|
477
|
+
const placeholderIndexes = findBooleanPlaceholderIndexes(sql);
|
|
478
|
+
for (const index of placeholderIndexes) {
|
|
479
|
+
if (index > 0 && index <= nextArgs.length) {
|
|
480
|
+
nextArgs[index - 1] = toBoolean(nextArgs[index - 1]);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return nextArgs;
|
|
484
|
+
}
|
|
485
|
+
function convertQuestionMarksToDollarParams(sql) {
|
|
486
|
+
let out = "";
|
|
487
|
+
let placeholder = 0;
|
|
488
|
+
let inSingle = false;
|
|
489
|
+
let inDouble = false;
|
|
490
|
+
let inLineComment = false;
|
|
491
|
+
let inBlockComment = false;
|
|
492
|
+
for (let i = 0; i < sql.length; i++) {
|
|
493
|
+
const ch = sql[i];
|
|
494
|
+
const next = sql[i + 1];
|
|
495
|
+
if (inLineComment) {
|
|
496
|
+
out += ch;
|
|
497
|
+
if (ch === "\n") inLineComment = false;
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
if (inBlockComment) {
|
|
501
|
+
out += ch;
|
|
502
|
+
if (ch === "*" && next === "/") {
|
|
503
|
+
out += next;
|
|
504
|
+
inBlockComment = false;
|
|
505
|
+
i += 1;
|
|
506
|
+
}
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
if (!inSingle && !inDouble && ch === "-" && next === "-") {
|
|
510
|
+
out += ch + next;
|
|
511
|
+
inLineComment = true;
|
|
512
|
+
i += 1;
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
if (!inSingle && !inDouble && ch === "/" && next === "*") {
|
|
516
|
+
out += ch + next;
|
|
517
|
+
inBlockComment = true;
|
|
518
|
+
i += 1;
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
if (!inDouble && ch === "'" && sql[i - 1] !== "\\") {
|
|
522
|
+
inSingle = !inSingle;
|
|
523
|
+
out += ch;
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
if (!inSingle && ch === '"' && sql[i - 1] !== "\\") {
|
|
527
|
+
inDouble = !inDouble;
|
|
528
|
+
out += ch;
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
if (!inSingle && !inDouble && ch === "?") {
|
|
532
|
+
placeholder += 1;
|
|
533
|
+
out += `$${placeholder}`;
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
out += ch;
|
|
537
|
+
}
|
|
538
|
+
return out;
|
|
539
|
+
}
|
|
540
|
+
function translateStatementForPostgres(stmt) {
|
|
541
|
+
const normalized = normalizeStatement(stmt);
|
|
542
|
+
if (normalized.kind === "named") {
|
|
543
|
+
throw new Error("Named SQL parameters are not supported by the Prisma adapter.");
|
|
544
|
+
}
|
|
545
|
+
const rewrittenSql = rewriteSql(normalized.sql);
|
|
546
|
+
const coercedArgs = coerceBooleanArgs(rewrittenSql, normalized.args);
|
|
547
|
+
return {
|
|
548
|
+
sql: convertQuestionMarksToDollarParams(rewrittenSql),
|
|
549
|
+
args: coercedArgs
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
function shouldBypassPostgres(stmt) {
|
|
553
|
+
const normalized = normalizeStatement(stmt);
|
|
554
|
+
if (normalized.kind === "named") {
|
|
555
|
+
return true;
|
|
556
|
+
}
|
|
557
|
+
return IMMEDIATE_FALLBACK_PATTERNS.some((pattern) => pattern.test(normalized.sql));
|
|
558
|
+
}
|
|
559
|
+
function shouldFallbackOnError(error) {
|
|
560
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
561
|
+
return /42P01|42883|42601|does not exist|syntax error|not supported|Named SQL parameters are not supported/iu.test(message);
|
|
562
|
+
}
|
|
563
|
+
function isReadQuery(sql) {
|
|
564
|
+
const trimmed = sql.trimStart();
|
|
565
|
+
return /^(SELECT|WITH|SHOW|EXPLAIN|VALUES)\b/iu.test(trimmed) || /\bRETURNING\b/iu.test(trimmed);
|
|
566
|
+
}
|
|
567
|
+
function buildRow(row, columns) {
|
|
568
|
+
const values = columns.map((column) => row[column]);
|
|
569
|
+
return Object.assign(values, row);
|
|
570
|
+
}
|
|
571
|
+
function buildResultSet(rows, rowsAffected = 0) {
|
|
572
|
+
const columns = rows[0] ? Object.keys(rows[0]) : [];
|
|
573
|
+
const resultRows = rows.map((row) => buildRow(row, columns));
|
|
574
|
+
return {
|
|
575
|
+
columns,
|
|
576
|
+
columnTypes: columns.map(() => ""),
|
|
577
|
+
rows: resultRows,
|
|
578
|
+
rowsAffected,
|
|
579
|
+
lastInsertRowid: void 0,
|
|
580
|
+
toJSON() {
|
|
581
|
+
return {
|
|
582
|
+
columns,
|
|
583
|
+
columnTypes: columns.map(() => ""),
|
|
584
|
+
rows,
|
|
585
|
+
rowsAffected,
|
|
586
|
+
lastInsertRowid: void 0
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
async function loadPrismaClient() {
|
|
592
|
+
if (!prismaClientPromise) {
|
|
593
|
+
prismaClientPromise = (async () => {
|
|
594
|
+
const explicitPath = process.env.EXE_OS_PRISMA_CLIENT_PATH;
|
|
595
|
+
if (explicitPath) {
|
|
596
|
+
const module2 = await import(pathToFileURL(explicitPath).href);
|
|
597
|
+
const PrismaClient2 = module2.PrismaClient ?? module2.default?.PrismaClient;
|
|
598
|
+
if (!PrismaClient2) {
|
|
599
|
+
throw new Error(`No PrismaClient export found at ${explicitPath}`);
|
|
600
|
+
}
|
|
601
|
+
return new PrismaClient2();
|
|
602
|
+
}
|
|
603
|
+
const exeDbRoot = process.env.EXE_DB_ROOT ?? path4.join(os3.homedir(), "exe-db");
|
|
604
|
+
const requireFromExeDb = createRequire(path4.join(exeDbRoot, "package.json"));
|
|
605
|
+
const prismaEntry = requireFromExeDb.resolve("@prisma/client");
|
|
606
|
+
const module = await import(pathToFileURL(prismaEntry).href);
|
|
607
|
+
const PrismaClient = module.PrismaClient ?? module.default?.PrismaClient;
|
|
608
|
+
if (!PrismaClient) {
|
|
609
|
+
throw new Error(`No PrismaClient export found in ${prismaEntry}`);
|
|
610
|
+
}
|
|
611
|
+
return new PrismaClient();
|
|
612
|
+
})();
|
|
613
|
+
}
|
|
614
|
+
return prismaClientPromise;
|
|
615
|
+
}
|
|
616
|
+
async function ensureCompatibilityViews(prisma) {
|
|
617
|
+
if (!compatibilityBootstrapPromise) {
|
|
618
|
+
compatibilityBootstrapPromise = (async () => {
|
|
619
|
+
for (const mapping of VIEW_MAPPINGS) {
|
|
620
|
+
const relation = mapping.source.replace(/"/g, "");
|
|
621
|
+
const rows = await prisma.$queryRawUnsafe(
|
|
622
|
+
"SELECT to_regclass($1) AS regclass",
|
|
623
|
+
relation
|
|
624
|
+
);
|
|
625
|
+
if (!rows[0]?.regclass) {
|
|
626
|
+
continue;
|
|
627
|
+
}
|
|
628
|
+
await prisma.$executeRawUnsafe(
|
|
629
|
+
`CREATE OR REPLACE VIEW public.${quotedIdentifier(mapping.view)} AS SELECT * FROM ${mapping.source}`
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
})();
|
|
633
|
+
}
|
|
634
|
+
return compatibilityBootstrapPromise;
|
|
635
|
+
}
|
|
636
|
+
async function executeOnPrisma(executor, stmt) {
|
|
637
|
+
const translated = translateStatementForPostgres(stmt);
|
|
638
|
+
if (isReadQuery(translated.sql)) {
|
|
639
|
+
const rows = await executor.$queryRawUnsafe(
|
|
640
|
+
translated.sql,
|
|
641
|
+
...translated.args
|
|
642
|
+
);
|
|
643
|
+
return buildResultSet(rows, /\bRETURNING\b/iu.test(translated.sql) ? rows.length : 0);
|
|
644
|
+
}
|
|
645
|
+
const rowsAffected = await executor.$executeRawUnsafe(translated.sql, ...translated.args);
|
|
646
|
+
return buildResultSet([], rowsAffected);
|
|
647
|
+
}
|
|
648
|
+
function splitSqlStatements(sql) {
|
|
649
|
+
const parts = [];
|
|
650
|
+
let current = "";
|
|
651
|
+
let inSingle = false;
|
|
652
|
+
let inDouble = false;
|
|
653
|
+
let inLineComment = false;
|
|
654
|
+
let inBlockComment = false;
|
|
655
|
+
for (let i = 0; i < sql.length; i++) {
|
|
656
|
+
const ch = sql[i];
|
|
657
|
+
const next = sql[i + 1];
|
|
658
|
+
if (inLineComment) {
|
|
659
|
+
current += ch;
|
|
660
|
+
if (ch === "\n") inLineComment = false;
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
if (inBlockComment) {
|
|
664
|
+
current += ch;
|
|
665
|
+
if (ch === "*" && next === "/") {
|
|
666
|
+
current += next;
|
|
667
|
+
inBlockComment = false;
|
|
668
|
+
i += 1;
|
|
669
|
+
}
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
if (!inSingle && !inDouble && ch === "-" && next === "-") {
|
|
673
|
+
current += ch + next;
|
|
674
|
+
inLineComment = true;
|
|
675
|
+
i += 1;
|
|
676
|
+
continue;
|
|
677
|
+
}
|
|
678
|
+
if (!inSingle && !inDouble && ch === "/" && next === "*") {
|
|
679
|
+
current += ch + next;
|
|
680
|
+
inBlockComment = true;
|
|
681
|
+
i += 1;
|
|
682
|
+
continue;
|
|
683
|
+
}
|
|
684
|
+
if (!inDouble && ch === "'" && sql[i - 1] !== "\\") {
|
|
685
|
+
inSingle = !inSingle;
|
|
686
|
+
current += ch;
|
|
687
|
+
continue;
|
|
688
|
+
}
|
|
689
|
+
if (!inSingle && ch === '"' && sql[i - 1] !== "\\") {
|
|
690
|
+
inDouble = !inDouble;
|
|
691
|
+
current += ch;
|
|
692
|
+
continue;
|
|
693
|
+
}
|
|
694
|
+
if (!inSingle && !inDouble && ch === ";") {
|
|
695
|
+
if (current.trim()) {
|
|
696
|
+
parts.push(current.trim());
|
|
697
|
+
}
|
|
698
|
+
current = "";
|
|
699
|
+
continue;
|
|
700
|
+
}
|
|
701
|
+
current += ch;
|
|
702
|
+
}
|
|
703
|
+
if (current.trim()) {
|
|
704
|
+
parts.push(current.trim());
|
|
705
|
+
}
|
|
706
|
+
return parts;
|
|
707
|
+
}
|
|
708
|
+
async function createPrismaDbAdapter(fallbackClient) {
|
|
709
|
+
const prisma = await loadPrismaClient();
|
|
710
|
+
await ensureCompatibilityViews(prisma);
|
|
711
|
+
let closed = false;
|
|
712
|
+
let adapter;
|
|
713
|
+
const fallbackExecute = async (stmt, error) => {
|
|
714
|
+
if (!fallbackClient) {
|
|
715
|
+
if (error) throw error;
|
|
716
|
+
throw new Error("No fallback SQLite client is available for this Prisma-routed query.");
|
|
717
|
+
}
|
|
718
|
+
if (error) {
|
|
719
|
+
process.stderr.write(
|
|
720
|
+
`[database-adapter] Falling back to SQLite: ${error instanceof Error ? error.message : String(error)}
|
|
721
|
+
`
|
|
722
|
+
);
|
|
723
|
+
}
|
|
724
|
+
return fallbackClient.execute(stmt);
|
|
725
|
+
};
|
|
726
|
+
adapter = {
|
|
727
|
+
async execute(stmt) {
|
|
728
|
+
if (shouldBypassPostgres(stmt)) {
|
|
729
|
+
return fallbackExecute(stmt);
|
|
730
|
+
}
|
|
731
|
+
try {
|
|
732
|
+
return await executeOnPrisma(prisma, stmt);
|
|
733
|
+
} catch (error) {
|
|
734
|
+
if (shouldFallbackOnError(error)) {
|
|
735
|
+
return fallbackExecute(stmt, error);
|
|
736
|
+
}
|
|
737
|
+
throw error;
|
|
738
|
+
}
|
|
739
|
+
},
|
|
740
|
+
async batch(stmts, mode) {
|
|
741
|
+
if (stmts.some((stmt) => shouldBypassPostgres(stmt))) {
|
|
742
|
+
if (!fallbackClient) {
|
|
743
|
+
throw new Error("Cannot batch unsupported SQLite-only statements without a fallback client.");
|
|
744
|
+
}
|
|
745
|
+
return fallbackClient.batch(stmts, mode);
|
|
746
|
+
}
|
|
747
|
+
try {
|
|
748
|
+
if (prisma.$transaction) {
|
|
749
|
+
return await prisma.$transaction(async (tx) => {
|
|
750
|
+
const results2 = [];
|
|
751
|
+
for (const stmt of stmts) {
|
|
752
|
+
results2.push(await executeOnPrisma(tx, stmt));
|
|
753
|
+
}
|
|
754
|
+
return results2;
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
const results = [];
|
|
758
|
+
for (const stmt of stmts) {
|
|
759
|
+
results.push(await executeOnPrisma(prisma, stmt));
|
|
760
|
+
}
|
|
761
|
+
return results;
|
|
762
|
+
} catch (error) {
|
|
763
|
+
if (fallbackClient && shouldFallbackOnError(error)) {
|
|
764
|
+
process.stderr.write(
|
|
765
|
+
`[database-adapter] Falling back batch to SQLite: ${error instanceof Error ? error.message : String(error)}
|
|
766
|
+
`
|
|
767
|
+
);
|
|
768
|
+
return fallbackClient.batch(stmts, mode);
|
|
769
|
+
}
|
|
770
|
+
throw error;
|
|
771
|
+
}
|
|
772
|
+
},
|
|
773
|
+
async migrate(stmts) {
|
|
774
|
+
if (fallbackClient) {
|
|
775
|
+
return fallbackClient.migrate(stmts);
|
|
776
|
+
}
|
|
777
|
+
return adapter.batch(stmts, "deferred");
|
|
778
|
+
},
|
|
779
|
+
async transaction(mode) {
|
|
780
|
+
if (!fallbackClient) {
|
|
781
|
+
throw new Error("Interactive transactions are only supported on the SQLite fallback client.");
|
|
782
|
+
}
|
|
783
|
+
return fallbackClient.transaction(mode);
|
|
784
|
+
},
|
|
785
|
+
async executeMultiple(sql) {
|
|
786
|
+
if (fallbackClient && shouldBypassPostgres(sql)) {
|
|
787
|
+
return fallbackClient.executeMultiple(sql);
|
|
788
|
+
}
|
|
789
|
+
for (const statement of splitSqlStatements(sql)) {
|
|
790
|
+
await adapter.execute(statement);
|
|
791
|
+
}
|
|
792
|
+
},
|
|
793
|
+
async sync() {
|
|
794
|
+
if (fallbackClient) {
|
|
795
|
+
return fallbackClient.sync();
|
|
796
|
+
}
|
|
797
|
+
return { frame_no: 0, frames_synced: 0 };
|
|
798
|
+
},
|
|
799
|
+
close() {
|
|
800
|
+
closed = true;
|
|
801
|
+
prismaClientPromise = null;
|
|
802
|
+
compatibilityBootstrapPromise = null;
|
|
803
|
+
void prisma.$disconnect?.();
|
|
804
|
+
},
|
|
805
|
+
get closed() {
|
|
806
|
+
return closed;
|
|
807
|
+
},
|
|
808
|
+
get protocol() {
|
|
809
|
+
return "prisma-postgres";
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
return adapter;
|
|
813
|
+
}
|
|
814
|
+
var VIEW_MAPPINGS, UPSERT_KEYS, BOOLEAN_COLUMNS_BY_TABLE, BOOLEAN_COLUMN_NAMES, IMMEDIATE_FALLBACK_PATTERNS, prismaClientPromise, compatibilityBootstrapPromise;
|
|
815
|
+
var init_database_adapter = __esm({
|
|
816
|
+
"src/lib/database-adapter.ts"() {
|
|
817
|
+
"use strict";
|
|
818
|
+
VIEW_MAPPINGS = [
|
|
819
|
+
{ view: "memories", source: "memory.memory_records" },
|
|
820
|
+
{ view: "tasks", source: "memory.tasks" },
|
|
821
|
+
{ view: "behaviors", source: "memory.behaviors" },
|
|
822
|
+
{ view: "entities", source: "memory.entities" },
|
|
823
|
+
{ view: "relationships", source: "memory.relationships" },
|
|
824
|
+
{ view: "entity_memories", source: "memory.entity_memories" },
|
|
825
|
+
{ view: "entity_aliases", source: "memory.entity_aliases" },
|
|
826
|
+
{ view: "notifications", source: "memory.notifications" },
|
|
827
|
+
{ view: "messages", source: "memory.messages" },
|
|
828
|
+
{ view: "users", source: "wiki.users" },
|
|
829
|
+
{ view: "workspaces", source: "wiki.workspaces" },
|
|
830
|
+
{ view: "workspace_users", source: "wiki.workspace_users" },
|
|
831
|
+
{ view: "documents", source: "wiki.workspace_documents" },
|
|
832
|
+
{ view: "chats", source: "wiki.workspace_chats" }
|
|
833
|
+
];
|
|
834
|
+
UPSERT_KEYS = {
|
|
835
|
+
memories: ["id"],
|
|
836
|
+
tasks: ["id"],
|
|
837
|
+
behaviors: ["id"],
|
|
838
|
+
entities: ["id"],
|
|
839
|
+
relationships: ["id"],
|
|
840
|
+
entity_aliases: ["alias"],
|
|
841
|
+
notifications: ["id"],
|
|
842
|
+
messages: ["id"],
|
|
843
|
+
users: ["id"],
|
|
844
|
+
workspaces: ["id"],
|
|
845
|
+
workspace_users: ["id"],
|
|
846
|
+
documents: ["id"],
|
|
847
|
+
chats: ["id"]
|
|
848
|
+
};
|
|
849
|
+
BOOLEAN_COLUMNS_BY_TABLE = {
|
|
850
|
+
memories: /* @__PURE__ */ new Set(["has_error", "draft"]),
|
|
851
|
+
behaviors: /* @__PURE__ */ new Set(["active"]),
|
|
852
|
+
notifications: /* @__PURE__ */ new Set(["read"]),
|
|
853
|
+
users: /* @__PURE__ */ new Set(["has_personal_memory"])
|
|
854
|
+
};
|
|
855
|
+
BOOLEAN_COLUMN_NAMES = new Set(
|
|
856
|
+
Object.values(BOOLEAN_COLUMNS_BY_TABLE).flatMap((cols) => [...cols])
|
|
857
|
+
);
|
|
858
|
+
IMMEDIATE_FALLBACK_PATTERNS = [
|
|
859
|
+
/\bPRAGMA\b/i,
|
|
860
|
+
/\bsqlite_master\b/i,
|
|
861
|
+
/(?:^|[.\s])(?:memories|conversations|entities)_fts\b/i,
|
|
862
|
+
/\bMATCH\b/i,
|
|
863
|
+
/\bvector_distance_cos\s*\(/i,
|
|
864
|
+
/\bjson_extract\s*\(/i,
|
|
865
|
+
/\bjulianday\s*\(/i,
|
|
866
|
+
/\bstrftime\s*\(/i,
|
|
867
|
+
/\blast_insert_rowid\s*\(/i
|
|
868
|
+
];
|
|
869
|
+
prismaClientPromise = null;
|
|
870
|
+
compatibilityBootstrapPromise = null;
|
|
871
|
+
}
|
|
872
|
+
});
|
|
873
|
+
|
|
289
874
|
// src/lib/exe-daemon-client.ts
|
|
290
875
|
import net from "net";
|
|
291
|
-
import
|
|
876
|
+
import os4 from "os";
|
|
292
877
|
import { spawn } from "child_process";
|
|
293
878
|
import { randomUUID } from "crypto";
|
|
294
879
|
import { existsSync as existsSync3, unlinkSync as unlinkSync3, readFileSync as readFileSync4, openSync, closeSync, statSync } from "fs";
|
|
295
|
-
import
|
|
880
|
+
import path5 from "path";
|
|
296
881
|
import { fileURLToPath } from "url";
|
|
297
882
|
function handleData(chunk) {
|
|
298
883
|
_buffer += chunk.toString();
|
|
@@ -343,17 +928,17 @@ function cleanupStaleFiles() {
|
|
|
343
928
|
}
|
|
344
929
|
}
|
|
345
930
|
function findPackageRoot() {
|
|
346
|
-
let dir =
|
|
347
|
-
const { root } =
|
|
931
|
+
let dir = path5.dirname(fileURLToPath(import.meta.url));
|
|
932
|
+
const { root } = path5.parse(dir);
|
|
348
933
|
while (dir !== root) {
|
|
349
|
-
if (existsSync3(
|
|
350
|
-
dir =
|
|
934
|
+
if (existsSync3(path5.join(dir, "package.json"))) return dir;
|
|
935
|
+
dir = path5.dirname(dir);
|
|
351
936
|
}
|
|
352
937
|
return null;
|
|
353
938
|
}
|
|
354
939
|
function spawnDaemon() {
|
|
355
|
-
const freeGB =
|
|
356
|
-
const totalGB =
|
|
940
|
+
const freeGB = os4.freemem() / (1024 * 1024 * 1024);
|
|
941
|
+
const totalGB = os4.totalmem() / (1024 * 1024 * 1024);
|
|
357
942
|
if (totalGB <= 8) {
|
|
358
943
|
process.stderr.write(
|
|
359
944
|
`[exed-client] SKIP: ${totalGB.toFixed(0)}GB system \u2014 embedding daemon disabled. Using keyword search only. Minimum 16GB recommended for vector search.
|
|
@@ -373,7 +958,7 @@ function spawnDaemon() {
|
|
|
373
958
|
process.stderr.write("[exed-client] WARN: cannot find package root\n");
|
|
374
959
|
return;
|
|
375
960
|
}
|
|
376
|
-
const daemonPath =
|
|
961
|
+
const daemonPath = path5.join(pkgRoot, "dist", "lib", "exe-daemon.js");
|
|
377
962
|
if (!existsSync3(daemonPath)) {
|
|
378
963
|
process.stderr.write(`[exed-client] WARN: daemon script not found at ${daemonPath}
|
|
379
964
|
`);
|
|
@@ -382,7 +967,7 @@ function spawnDaemon() {
|
|
|
382
967
|
const resolvedPath = daemonPath;
|
|
383
968
|
process.stderr.write(`[exed-client] Spawning daemon: ${resolvedPath}
|
|
384
969
|
`);
|
|
385
|
-
const logPath =
|
|
970
|
+
const logPath = path5.join(path5.dirname(SOCKET_PATH), "exed.log");
|
|
386
971
|
let stderrFd = "ignore";
|
|
387
972
|
try {
|
|
388
973
|
stderrFd = openSync(logPath, "a");
|
|
@@ -529,9 +1114,9 @@ var init_exe_daemon_client = __esm({
|
|
|
529
1114
|
"src/lib/exe-daemon-client.ts"() {
|
|
530
1115
|
"use strict";
|
|
531
1116
|
init_config();
|
|
532
|
-
SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ??
|
|
533
|
-
PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ??
|
|
534
|
-
SPAWN_LOCK_PATH =
|
|
1117
|
+
SOCKET_PATH = process.env.EXE_DAEMON_SOCK ?? process.env.EXE_EMBED_SOCK ?? path5.join(EXE_AI_DIR, "exed.sock");
|
|
1118
|
+
PID_PATH = process.env.EXE_DAEMON_PID ?? process.env.EXE_EMBED_PID ?? path5.join(EXE_AI_DIR, "exed.pid");
|
|
1119
|
+
SPAWN_LOCK_PATH = path5.join(EXE_AI_DIR, "exed-spawn.lock");
|
|
535
1120
|
SPAWN_LOCK_STALE_MS = 3e4;
|
|
536
1121
|
CONNECT_TIMEOUT_MS = 15e3;
|
|
537
1122
|
REQUEST_TIMEOUT_MS = 3e4;
|
|
@@ -613,7 +1198,7 @@ __export(db_daemon_client_exports, {
|
|
|
613
1198
|
createDaemonDbClient: () => createDaemonDbClient,
|
|
614
1199
|
initDaemonDbClient: () => initDaemonDbClient
|
|
615
1200
|
});
|
|
616
|
-
function
|
|
1201
|
+
function normalizeStatement2(stmt) {
|
|
617
1202
|
if (typeof stmt === "string") {
|
|
618
1203
|
return { sql: stmt, args: [] };
|
|
619
1204
|
}
|
|
@@ -637,7 +1222,7 @@ function createDaemonDbClient(fallbackClient) {
|
|
|
637
1222
|
if (!_useDaemon || !isClientConnected()) {
|
|
638
1223
|
return fallbackClient.execute(stmt);
|
|
639
1224
|
}
|
|
640
|
-
const { sql, args } =
|
|
1225
|
+
const { sql, args } = normalizeStatement2(stmt);
|
|
641
1226
|
const response = await sendDaemonRequest({
|
|
642
1227
|
type: "db-execute",
|
|
643
1228
|
sql,
|
|
@@ -662,7 +1247,7 @@ function createDaemonDbClient(fallbackClient) {
|
|
|
662
1247
|
if (!_useDaemon || !isClientConnected()) {
|
|
663
1248
|
return fallbackClient.batch(stmts, mode);
|
|
664
1249
|
}
|
|
665
|
-
const statements = stmts.map(
|
|
1250
|
+
const statements = stmts.map(normalizeStatement2);
|
|
666
1251
|
const response = await sendDaemonRequest({
|
|
667
1252
|
type: "db-batch",
|
|
668
1253
|
statements,
|
|
@@ -757,6 +1342,18 @@ __export(database_exports, {
|
|
|
757
1342
|
});
|
|
758
1343
|
import { createClient } from "@libsql/client";
|
|
759
1344
|
async function initDatabase(config) {
|
|
1345
|
+
if (_walCheckpointTimer) {
|
|
1346
|
+
clearInterval(_walCheckpointTimer);
|
|
1347
|
+
_walCheckpointTimer = null;
|
|
1348
|
+
}
|
|
1349
|
+
if (_daemonClient) {
|
|
1350
|
+
_daemonClient.close();
|
|
1351
|
+
_daemonClient = null;
|
|
1352
|
+
}
|
|
1353
|
+
if (_adapterClient && _adapterClient !== _resilientClient) {
|
|
1354
|
+
_adapterClient.close();
|
|
1355
|
+
}
|
|
1356
|
+
_adapterClient = null;
|
|
760
1357
|
if (_client) {
|
|
761
1358
|
_client.close();
|
|
762
1359
|
_client = null;
|
|
@@ -770,6 +1367,7 @@ async function initDatabase(config) {
|
|
|
770
1367
|
}
|
|
771
1368
|
_client = createClient(opts);
|
|
772
1369
|
_resilientClient = wrapWithRetry(_client);
|
|
1370
|
+
_adapterClient = _resilientClient;
|
|
773
1371
|
_client.execute("PRAGMA busy_timeout = 30000").catch(() => {
|
|
774
1372
|
});
|
|
775
1373
|
_client.execute("PRAGMA journal_mode = WAL").catch(() => {
|
|
@@ -780,14 +1378,20 @@ async function initDatabase(config) {
|
|
|
780
1378
|
});
|
|
781
1379
|
}, 3e4);
|
|
782
1380
|
_walCheckpointTimer.unref();
|
|
1381
|
+
if (process.env.DATABASE_URL) {
|
|
1382
|
+
_adapterClient = await createPrismaDbAdapter(_resilientClient);
|
|
1383
|
+
}
|
|
783
1384
|
}
|
|
784
1385
|
function isInitialized() {
|
|
785
|
-
return _client !== null;
|
|
1386
|
+
return _adapterClient !== null || _client !== null;
|
|
786
1387
|
}
|
|
787
1388
|
function getClient() {
|
|
788
|
-
if (!
|
|
1389
|
+
if (!_adapterClient) {
|
|
789
1390
|
throw new Error("Database client not initialized. Call initDatabase() first.");
|
|
790
1391
|
}
|
|
1392
|
+
if (process.env.DATABASE_URL) {
|
|
1393
|
+
return _adapterClient;
|
|
1394
|
+
}
|
|
791
1395
|
if (process.env.EXE_IS_DAEMON === "1") {
|
|
792
1396
|
return _resilientClient;
|
|
793
1397
|
}
|
|
@@ -797,6 +1401,7 @@ function getClient() {
|
|
|
797
1401
|
return _resilientClient;
|
|
798
1402
|
}
|
|
799
1403
|
async function initDaemonClient() {
|
|
1404
|
+
if (process.env.DATABASE_URL) return;
|
|
800
1405
|
if (process.env.EXE_IS_DAEMON === "1") return;
|
|
801
1406
|
if (!_resilientClient) return;
|
|
802
1407
|
try {
|
|
@@ -1741,26 +2346,36 @@ async function ensureSchema() {
|
|
|
1741
2346
|
}
|
|
1742
2347
|
}
|
|
1743
2348
|
async function disposeDatabase() {
|
|
2349
|
+
if (_walCheckpointTimer) {
|
|
2350
|
+
clearInterval(_walCheckpointTimer);
|
|
2351
|
+
_walCheckpointTimer = null;
|
|
2352
|
+
}
|
|
1744
2353
|
if (_daemonClient) {
|
|
1745
2354
|
_daemonClient.close();
|
|
1746
2355
|
_daemonClient = null;
|
|
1747
2356
|
}
|
|
2357
|
+
if (_adapterClient && _adapterClient !== _resilientClient) {
|
|
2358
|
+
_adapterClient.close();
|
|
2359
|
+
}
|
|
2360
|
+
_adapterClient = null;
|
|
1748
2361
|
if (_client) {
|
|
1749
2362
|
_client.close();
|
|
1750
2363
|
_client = null;
|
|
1751
2364
|
_resilientClient = null;
|
|
1752
2365
|
}
|
|
1753
2366
|
}
|
|
1754
|
-
var _client, _resilientClient, _walCheckpointTimer, _daemonClient, initTurso, disposeTurso;
|
|
2367
|
+
var _client, _resilientClient, _walCheckpointTimer, _daemonClient, _adapterClient, initTurso, disposeTurso;
|
|
1755
2368
|
var init_database = __esm({
|
|
1756
2369
|
"src/lib/database.ts"() {
|
|
1757
2370
|
"use strict";
|
|
1758
2371
|
init_db_retry();
|
|
1759
2372
|
init_employees();
|
|
2373
|
+
init_database_adapter();
|
|
1760
2374
|
_client = null;
|
|
1761
2375
|
_resilientClient = null;
|
|
1762
2376
|
_walCheckpointTimer = null;
|
|
1763
2377
|
_daemonClient = null;
|
|
2378
|
+
_adapterClient = null;
|
|
1764
2379
|
initTurso = initDatabase;
|
|
1765
2380
|
disposeTurso = disposeDatabase;
|
|
1766
2381
|
}
|
|
@@ -1778,13 +2393,13 @@ var init_memory = __esm({
|
|
|
1778
2393
|
// src/lib/keychain.ts
|
|
1779
2394
|
import { readFile as readFile3, writeFile as writeFile3, unlink, mkdir as mkdir3, chmod as chmod2 } from "fs/promises";
|
|
1780
2395
|
import { existsSync as existsSync5 } from "fs";
|
|
1781
|
-
import
|
|
1782
|
-
import
|
|
2396
|
+
import path7 from "path";
|
|
2397
|
+
import os5 from "os";
|
|
1783
2398
|
function getKeyDir() {
|
|
1784
|
-
return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ??
|
|
2399
|
+
return process.env.EXE_OS_DIR ?? process.env.EXE_MEM_DIR ?? path7.join(os5.homedir(), ".exe-os");
|
|
1785
2400
|
}
|
|
1786
2401
|
function getKeyPath() {
|
|
1787
|
-
return
|
|
2402
|
+
return path7.join(getKeyDir(), "master.key");
|
|
1788
2403
|
}
|
|
1789
2404
|
async function tryKeytar() {
|
|
1790
2405
|
try {
|
|
@@ -1807,7 +2422,7 @@ async function getMasterKey() {
|
|
|
1807
2422
|
const keyPath = getKeyPath();
|
|
1808
2423
|
if (!existsSync5(keyPath)) {
|
|
1809
2424
|
process.stderr.write(
|
|
1810
|
-
`[keychain] Key not found at ${keyPath} (HOME=${
|
|
2425
|
+
`[keychain] Key not found at ${keyPath} (HOME=${os5.homedir()}, EXE_OS_DIR=${process.env.EXE_OS_DIR ?? "unset"})
|
|
1811
2426
|
`
|
|
1812
2427
|
);
|
|
1813
2428
|
return null;
|
|
@@ -1900,7 +2515,7 @@ __export(shard_manager_exports, {
|
|
|
1900
2515
|
listShards: () => listShards,
|
|
1901
2516
|
shardExists: () => shardExists
|
|
1902
2517
|
});
|
|
1903
|
-
import
|
|
2518
|
+
import path8 from "path";
|
|
1904
2519
|
import { existsSync as existsSync6, mkdirSync as mkdirSync3, readdirSync as readdirSync3 } from "fs";
|
|
1905
2520
|
import { createClient as createClient2 } from "@libsql/client";
|
|
1906
2521
|
function initShardManager(encryptionKey) {
|
|
@@ -1926,7 +2541,7 @@ function getShardClient(projectName) {
|
|
|
1926
2541
|
}
|
|
1927
2542
|
const cached = _shards.get(safeName);
|
|
1928
2543
|
if (cached) return cached;
|
|
1929
|
-
const dbPath =
|
|
2544
|
+
const dbPath = path8.join(SHARDS_DIR, `${safeName}.db`);
|
|
1930
2545
|
const client = createClient2({
|
|
1931
2546
|
url: `file:${dbPath}`,
|
|
1932
2547
|
encryptionKey: _encryptionKey
|
|
@@ -1936,7 +2551,7 @@ function getShardClient(projectName) {
|
|
|
1936
2551
|
}
|
|
1937
2552
|
function shardExists(projectName) {
|
|
1938
2553
|
const safeName = projectName.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
1939
|
-
return existsSync6(
|
|
2554
|
+
return existsSync6(path8.join(SHARDS_DIR, `${safeName}.db`));
|
|
1940
2555
|
}
|
|
1941
2556
|
function listShards() {
|
|
1942
2557
|
if (!existsSync6(SHARDS_DIR)) return [];
|
|
@@ -2013,7 +2628,23 @@ async function ensureShardSchema(client) {
|
|
|
2013
2628
|
// MS-11: draft staging, MS-6a: memory_type, MS-7: trajectory
|
|
2014
2629
|
"ALTER TABLE memories ADD COLUMN draft INTEGER DEFAULT 0",
|
|
2015
2630
|
"ALTER TABLE memories ADD COLUMN memory_type TEXT DEFAULT 'raw'",
|
|
2016
|
-
"ALTER TABLE memories ADD COLUMN trajectory TEXT"
|
|
2631
|
+
"ALTER TABLE memories ADD COLUMN trajectory TEXT",
|
|
2632
|
+
// Metadata enrichment columns (must match database.ts)
|
|
2633
|
+
"ALTER TABLE memories ADD COLUMN intent TEXT",
|
|
2634
|
+
"ALTER TABLE memories ADD COLUMN outcome TEXT",
|
|
2635
|
+
"ALTER TABLE memories ADD COLUMN domain TEXT",
|
|
2636
|
+
"ALTER TABLE memories ADD COLUMN referenced_entities TEXT",
|
|
2637
|
+
"ALTER TABLE memories ADD COLUMN retrieval_count INTEGER DEFAULT 0",
|
|
2638
|
+
"ALTER TABLE memories ADD COLUMN chain_position TEXT",
|
|
2639
|
+
"ALTER TABLE memories ADD COLUMN review_status TEXT",
|
|
2640
|
+
"ALTER TABLE memories ADD COLUMN context_window_pct INTEGER",
|
|
2641
|
+
"ALTER TABLE memories ADD COLUMN file_paths TEXT",
|
|
2642
|
+
"ALTER TABLE memories ADD COLUMN commit_hash TEXT",
|
|
2643
|
+
"ALTER TABLE memories ADD COLUMN duration_ms INTEGER",
|
|
2644
|
+
"ALTER TABLE memories ADD COLUMN token_cost REAL",
|
|
2645
|
+
"ALTER TABLE memories ADD COLUMN audience TEXT",
|
|
2646
|
+
"ALTER TABLE memories ADD COLUMN language_type TEXT",
|
|
2647
|
+
"ALTER TABLE memories ADD COLUMN parent_memory_id TEXT"
|
|
2017
2648
|
]) {
|
|
2018
2649
|
try {
|
|
2019
2650
|
await client.execute(col);
|
|
@@ -2125,7 +2756,7 @@ var init_shard_manager = __esm({
|
|
|
2125
2756
|
"src/lib/shard-manager.ts"() {
|
|
2126
2757
|
"use strict";
|
|
2127
2758
|
init_config();
|
|
2128
|
-
SHARDS_DIR =
|
|
2759
|
+
SHARDS_DIR = path8.join(EXE_AI_DIR, "shards");
|
|
2129
2760
|
_shards = /* @__PURE__ */ new Map();
|
|
2130
2761
|
_encryptionKey = null;
|
|
2131
2762
|
_shardingEnabled = false;
|
|
@@ -3054,16 +3685,16 @@ init_config();
|
|
|
3054
3685
|
init_database();
|
|
3055
3686
|
import { existsSync as existsSync4, mkdirSync as mkdirSync2, readFileSync as readFileSync5, writeFileSync as writeFileSync3 } from "fs";
|
|
3056
3687
|
import { readdirSync as readdirSync2 } from "fs";
|
|
3057
|
-
import
|
|
3688
|
+
import path6 from "path";
|
|
3058
3689
|
import { createHash } from "crypto";
|
|
3059
|
-
var
|
|
3690
|
+
var IDENTITY_DIR2 = path6.join(EXE_AI_DIR, "identity");
|
|
3060
3691
|
function ensureDir() {
|
|
3061
|
-
if (!existsSync4(
|
|
3062
|
-
mkdirSync2(
|
|
3692
|
+
if (!existsSync4(IDENTITY_DIR2)) {
|
|
3693
|
+
mkdirSync2(IDENTITY_DIR2, { recursive: true });
|
|
3063
3694
|
}
|
|
3064
3695
|
}
|
|
3065
3696
|
function identityPath(agentId) {
|
|
3066
|
-
return
|
|
3697
|
+
return path6.join(IDENTITY_DIR2, `${agentId}.md`);
|
|
3067
3698
|
}
|
|
3068
3699
|
function parseFrontmatter(raw) {
|
|
3069
3700
|
const match = raw.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
|
|
@@ -3117,7 +3748,7 @@ function getIdentity(agentId) {
|
|
|
3117
3748
|
}
|
|
3118
3749
|
function listIdentities() {
|
|
3119
3750
|
ensureDir();
|
|
3120
|
-
const files = readdirSync2(
|
|
3751
|
+
const files = readdirSync2(IDENTITY_DIR2).filter((f) => f.endsWith(".md"));
|
|
3121
3752
|
const results = [];
|
|
3122
3753
|
for (const file of files) {
|
|
3123
3754
|
const agentId = file.replace(".md", "");
|
|
@@ -3207,8 +3838,8 @@ process.stdin.on("end", async () => {
|
|
|
3207
3838
|
try {
|
|
3208
3839
|
const { readFileSync: readFileSync6, existsSync: existsSync7 } = await import("fs");
|
|
3209
3840
|
const { join } = await import("path");
|
|
3210
|
-
const
|
|
3211
|
-
const claudeMd = join(
|
|
3841
|
+
const os6 = await import("os");
|
|
3842
|
+
const claudeMd = join(os6.homedir(), ".claude", "CLAUDE.md");
|
|
3212
3843
|
if (existsSync7(claudeMd)) {
|
|
3213
3844
|
const content = readFileSync6(claudeMd, "utf8");
|
|
3214
3845
|
const hasOrchRules = content.includes("exe-os:orchestration-start");
|