teleton 0.7.4 → 0.7.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -26
- package/dist/{chunk-XDYDA2KV.js → chunk-2GLHOJ5C.js} +268 -59
- package/dist/chunk-5UVXJMOX.js +292 -0
- package/dist/{chunk-BGC2IUM5.js → chunk-AVDWXYQ7.js} +65 -20
- package/dist/{chunk-RMLQS3X6.js → chunk-CB2Y45HA.js} +106 -1
- package/dist/{chunk-5PLZ3KSO.js → chunk-DMXTIRUW.js} +5 -6
- package/dist/{chunk-YFG2QHLA.js → chunk-G2LLMJXJ.js} +1578 -115
- package/dist/{chunk-EK7M5K26.js → chunk-LCCVZ4D2.js} +3 -3
- package/dist/{chunk-LAQOUFOJ.js → chunk-OGMVWDVU.js} +3517 -3620
- package/dist/{chunk-4DU3C27M.js → chunk-R4YSJ4EY.js} +5 -1
- package/dist/{chunk-XBKSS6DM.js → chunk-VFA7QMCZ.js} +5 -3
- package/dist/{chunk-VAUJSSD3.js → chunk-XQUHC3JZ.js} +1 -1
- package/dist/{chunk-RO62LO6Z.js → chunk-YP25WTQK.js} +2 -0
- package/dist/cli/index.js +92 -28
- package/dist/{client-RTNALK7W.js → client-O37XDCJB.js} +4 -5
- package/dist/index.js +12 -13
- package/dist/{memory-JQZ6MTRU.js → memory-KQALFUV3.js} +6 -7
- package/dist/{migrate-GS5ACQDA.js → migrate-UV3WEL5D.js} +6 -7
- package/dist/{server-TCJOBV3D.js → server-BHHJGUDF.js} +35 -9
- package/dist/{setup-server-YHYJLAMA.js → setup-server-G7UG2DI3.js} +21 -9
- package/dist/store-H4XPNGC2.js +34 -0
- package/dist/{task-dependency-resolver-WKZWJLLM.js → task-dependency-resolver-VMEVJRPO.js} +2 -2
- package/dist/{task-executor-PD3H4MLO.js → task-executor-WWSPBJ4V.js} +1 -1
- package/dist/{tool-index-6HBRVXVG.js → tool-index-2KH3OB6X.js} +5 -5
- package/dist/web/assets/index-BrVqauzj.css +1 -0
- package/dist/web/assets/index-Bx8JW3gV.js +72 -0
- package/dist/web/assets/{index.es-CqZHj0tz.js → index.es-Pet5-M13.js} +1 -1
- package/dist/web/index.html +2 -2
- package/package.json +2 -2
- package/dist/chunk-JQDLW7IE.js +0 -107
- package/dist/chunk-UCN6TI25.js +0 -143
- package/dist/web/assets/index-B6M9knfJ.css +0 -1
- package/dist/web/assets/index-DAGeQfVZ.js +0 -72
- package/scripts/patch-gramjs.sh +0 -46
- package/scripts/postinstall.mjs +0 -16
|
@@ -1,30 +1,159 @@
|
|
|
1
|
-
import {
|
|
2
|
-
JOURNAL_SCHEMA
|
|
3
|
-
} from "./chunk-UCN6TI25.js";
|
|
4
1
|
import {
|
|
5
2
|
CachedEmbeddingProvider,
|
|
6
3
|
createEmbeddingProvider,
|
|
7
4
|
hashText,
|
|
8
5
|
serializeEmbedding
|
|
9
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-LCCVZ4D2.js";
|
|
10
7
|
import {
|
|
11
8
|
HYBRID_SEARCH_MIN_SCORE,
|
|
12
9
|
KNOWLEDGE_CHUNK_SIZE,
|
|
13
10
|
SQLITE_CACHE_SIZE_KB,
|
|
14
11
|
SQLITE_MMAP_SIZE
|
|
15
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-YP25WTQK.js";
|
|
13
|
+
import {
|
|
14
|
+
TELETON_ROOT
|
|
15
|
+
} from "./chunk-EYWNOHMJ.js";
|
|
16
16
|
import {
|
|
17
17
|
createLogger
|
|
18
18
|
} from "./chunk-RCMD3U65.js";
|
|
19
19
|
|
|
20
20
|
// src/memory/database.ts
|
|
21
|
+
import Database2 from "better-sqlite3";
|
|
22
|
+
import { existsSync as existsSync2, mkdirSync as mkdirSync2, chmodSync as chmodSync2 } from "fs";
|
|
23
|
+
import { dirname as dirname2 } from "path";
|
|
24
|
+
import * as sqliteVec from "sqlite-vec";
|
|
25
|
+
|
|
26
|
+
// src/utils/module-db.ts
|
|
21
27
|
import Database from "better-sqlite3";
|
|
22
28
|
import { existsSync, mkdirSync, chmodSync } from "fs";
|
|
23
|
-
import { dirname } from "path";
|
|
24
|
-
|
|
29
|
+
import { dirname, join } from "path";
|
|
30
|
+
var log = createLogger("Utils");
|
|
31
|
+
var JOURNAL_SCHEMA = `
|
|
32
|
+
CREATE TABLE IF NOT EXISTS journal (
|
|
33
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
34
|
+
timestamp INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
35
|
+
type TEXT NOT NULL CHECK(type IN ('trade', 'gift', 'middleman', 'kol')),
|
|
36
|
+
action TEXT NOT NULL,
|
|
37
|
+
asset_from TEXT,
|
|
38
|
+
asset_to TEXT,
|
|
39
|
+
amount_from REAL,
|
|
40
|
+
amount_to REAL,
|
|
41
|
+
price_ton REAL,
|
|
42
|
+
counterparty TEXT,
|
|
43
|
+
platform TEXT,
|
|
44
|
+
reasoning TEXT,
|
|
45
|
+
outcome TEXT CHECK(outcome IN ('pending', 'profit', 'loss', 'neutral', 'cancelled')),
|
|
46
|
+
pnl_ton REAL,
|
|
47
|
+
pnl_pct REAL,
|
|
48
|
+
tx_hash TEXT,
|
|
49
|
+
tool_used TEXT,
|
|
50
|
+
chat_id TEXT,
|
|
51
|
+
user_id INTEGER,
|
|
52
|
+
closed_at INTEGER,
|
|
53
|
+
created_at INTEGER NOT NULL DEFAULT (unixepoch())
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
CREATE INDEX IF NOT EXISTS idx_journal_type ON journal(type);
|
|
57
|
+
CREATE INDEX IF NOT EXISTS idx_journal_timestamp ON journal(timestamp DESC);
|
|
58
|
+
CREATE INDEX IF NOT EXISTS idx_journal_asset_from ON journal(asset_from);
|
|
59
|
+
CREATE INDEX IF NOT EXISTS idx_journal_outcome ON journal(outcome);
|
|
60
|
+
CREATE INDEX IF NOT EXISTS idx_journal_type_timestamp ON journal(type, timestamp DESC);
|
|
61
|
+
`;
|
|
62
|
+
var USED_TRANSACTIONS_SCHEMA = `
|
|
63
|
+
CREATE TABLE IF NOT EXISTS used_transactions (
|
|
64
|
+
tx_hash TEXT PRIMARY KEY,
|
|
65
|
+
user_id TEXT NOT NULL,
|
|
66
|
+
amount REAL NOT NULL,
|
|
67
|
+
game_type TEXT NOT NULL,
|
|
68
|
+
used_at INTEGER NOT NULL DEFAULT (unixepoch())
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
CREATE INDEX IF NOT EXISTS idx_used_tx_user ON used_transactions(user_id);
|
|
72
|
+
CREATE INDEX IF NOT EXISTS idx_used_tx_used_at ON used_transactions(used_at);
|
|
73
|
+
`;
|
|
74
|
+
function openModuleDb(path) {
|
|
75
|
+
const dir = dirname(path);
|
|
76
|
+
if (!existsSync(dir)) {
|
|
77
|
+
mkdirSync(dir, { recursive: true });
|
|
78
|
+
}
|
|
79
|
+
const db = new Database(path);
|
|
80
|
+
try {
|
|
81
|
+
chmodSync(path, 384);
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
db.pragma("journal_mode = WAL");
|
|
85
|
+
return db;
|
|
86
|
+
}
|
|
87
|
+
function createDbWrapper(getDb, moduleName) {
|
|
88
|
+
return function withDb(executor) {
|
|
89
|
+
return (params, context) => {
|
|
90
|
+
const moduleDb = getDb();
|
|
91
|
+
if (!moduleDb) {
|
|
92
|
+
return Promise.resolve({
|
|
93
|
+
success: false,
|
|
94
|
+
error: `${moduleName} module not started`
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return executor(params, { ...context, db: moduleDb });
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
var MAIN_DB_PATH = join(TELETON_ROOT, "memory.db");
|
|
102
|
+
function migrateFromMainDb(moduleDb, tables) {
|
|
103
|
+
let totalMigrated = 0;
|
|
104
|
+
for (const table of tables) {
|
|
105
|
+
if (!/^[a-z_]+$/.test(table)) {
|
|
106
|
+
throw new Error(`Invalid table name for migration: "${table}"`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
for (const table of tables) {
|
|
110
|
+
try {
|
|
111
|
+
const row = moduleDb.prepare(`SELECT COUNT(*) as c FROM ${table}`).get();
|
|
112
|
+
if (row.c > 0) return 0;
|
|
113
|
+
} catch {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (!existsSync(MAIN_DB_PATH)) return 0;
|
|
118
|
+
try {
|
|
119
|
+
moduleDb.exec(`ATTACH DATABASE '${MAIN_DB_PATH}' AS main_db`);
|
|
120
|
+
for (const table of tables) {
|
|
121
|
+
try {
|
|
122
|
+
const exists = moduleDb.prepare(`SELECT name FROM main_db.sqlite_master WHERE type='table' AND name=?`).get(table);
|
|
123
|
+
if (!exists) continue;
|
|
124
|
+
const src = moduleDb.prepare(`SELECT COUNT(*) as c FROM main_db.${table}`).get();
|
|
125
|
+
if (src.c === 0) continue;
|
|
126
|
+
const dstCols = moduleDb.prepare(`PRAGMA table_info(${table})`).all().map((r) => r.name);
|
|
127
|
+
const srcCols = moduleDb.prepare(`PRAGMA main_db.table_info(${table})`).all().map((r) => r.name);
|
|
128
|
+
const shared = dstCols.filter((c) => srcCols.includes(c));
|
|
129
|
+
if (shared.length === 0) continue;
|
|
130
|
+
const cols = shared.join(", ");
|
|
131
|
+
moduleDb.exec(
|
|
132
|
+
`INSERT OR IGNORE INTO ${table} (${cols}) SELECT ${cols} FROM main_db.${table}`
|
|
133
|
+
);
|
|
134
|
+
totalMigrated += src.c;
|
|
135
|
+
try {
|
|
136
|
+
moduleDb.exec(`DROP TABLE main_db.${table}`);
|
|
137
|
+
} catch {
|
|
138
|
+
}
|
|
139
|
+
log.info(`Migrated ${src.c} rows from memory.db \u2192 ${table} (source dropped)`);
|
|
140
|
+
} catch (e) {
|
|
141
|
+
log.warn({ err: e }, `Could not migrate table ${table}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
moduleDb.exec(`DETACH DATABASE main_db`);
|
|
145
|
+
} catch (e) {
|
|
146
|
+
log.warn({ err: e }, `Migration from memory.db failed`);
|
|
147
|
+
try {
|
|
148
|
+
moduleDb.exec(`DETACH DATABASE main_db`);
|
|
149
|
+
} catch {
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return totalMigrated;
|
|
153
|
+
}
|
|
25
154
|
|
|
26
155
|
// src/memory/schema.ts
|
|
27
|
-
var
|
|
156
|
+
var log2 = createLogger("Memory");
|
|
28
157
|
function compareSemver(a, b) {
|
|
29
158
|
const parseVersion = (v) => {
|
|
30
159
|
const parts = v.split("-")[0].split(".").map(Number);
|
|
@@ -119,7 +248,9 @@ function ensureSchema(db) {
|
|
|
119
248
|
context_tokens INTEGER, -- Current context size
|
|
120
249
|
model TEXT, -- Model used (claude-opus-4-5-20251101)
|
|
121
250
|
provider TEXT, -- Provider (anthropic)
|
|
122
|
-
last_reset_date TEXT
|
|
251
|
+
last_reset_date TEXT, -- YYYY-MM-DD of last daily reset
|
|
252
|
+
input_tokens INTEGER DEFAULT 0, -- Accumulated input tokens
|
|
253
|
+
output_tokens INTEGER DEFAULT 0 -- Accumulated output tokens
|
|
123
254
|
);
|
|
124
255
|
|
|
125
256
|
CREATE INDEX IF NOT EXISTS idx_sessions_chat ON sessions(chat_id);
|
|
@@ -271,6 +402,30 @@ function ensureSchema(db) {
|
|
|
271
402
|
|
|
272
403
|
CREATE INDEX IF NOT EXISTS idx_embedding_cache_accessed ON embedding_cache(accessed_at);
|
|
273
404
|
|
|
405
|
+
-- =====================================================
|
|
406
|
+
-- EXEC AUDIT (Command Execution History)
|
|
407
|
+
-- =====================================================
|
|
408
|
+
|
|
409
|
+
CREATE TABLE IF NOT EXISTS exec_audit (
|
|
410
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
411
|
+
timestamp INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
412
|
+
user_id INTEGER NOT NULL,
|
|
413
|
+
username TEXT,
|
|
414
|
+
tool TEXT NOT NULL,
|
|
415
|
+
command TEXT NOT NULL,
|
|
416
|
+
status TEXT NOT NULL DEFAULT 'running'
|
|
417
|
+
CHECK(status IN ('running', 'success', 'failed', 'timeout', 'killed')),
|
|
418
|
+
exit_code INTEGER,
|
|
419
|
+
signal TEXT,
|
|
420
|
+
duration_ms INTEGER,
|
|
421
|
+
stdout TEXT,
|
|
422
|
+
stderr TEXT,
|
|
423
|
+
truncated INTEGER NOT NULL DEFAULT 0
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
CREATE INDEX IF NOT EXISTS idx_exec_audit_timestamp ON exec_audit(timestamp DESC);
|
|
427
|
+
CREATE INDEX IF NOT EXISTS idx_exec_audit_user ON exec_audit(user_id);
|
|
428
|
+
|
|
274
429
|
-- =====================================================
|
|
275
430
|
-- JOURNAL (Trading & Business Operations)
|
|
276
431
|
-- =====================================================
|
|
@@ -313,15 +468,15 @@ function setSchemaVersion(db, version) {
|
|
|
313
468
|
`
|
|
314
469
|
).run(version);
|
|
315
470
|
}
|
|
316
|
-
var CURRENT_SCHEMA_VERSION = "1.
|
|
471
|
+
var CURRENT_SCHEMA_VERSION = "1.13.0";
|
|
317
472
|
function runMigrations(db) {
|
|
318
473
|
const currentVersion = getSchemaVersion(db);
|
|
319
474
|
if (!currentVersion || versionLessThan(currentVersion, "1.1.0")) {
|
|
320
|
-
|
|
475
|
+
log2.info("Running migration: Adding scheduled task columns...");
|
|
321
476
|
try {
|
|
322
477
|
const tableExists = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='tasks'").get();
|
|
323
478
|
if (!tableExists) {
|
|
324
|
-
|
|
479
|
+
log2.info("Tasks table doesn't exist yet, skipping column migration");
|
|
325
480
|
setSchemaVersion(db, CURRENT_SCHEMA_VERSION);
|
|
326
481
|
return;
|
|
327
482
|
}
|
|
@@ -354,15 +509,15 @@ function runMigrations(db) {
|
|
|
354
509
|
CREATE INDEX IF NOT EXISTS idx_task_deps_task ON task_dependencies(task_id);
|
|
355
510
|
CREATE INDEX IF NOT EXISTS idx_task_deps_parent ON task_dependencies(depends_on_task_id);
|
|
356
511
|
`);
|
|
357
|
-
|
|
512
|
+
log2.info("Migration 1.1.0 complete: Scheduled tasks support added");
|
|
358
513
|
} catch (error) {
|
|
359
|
-
|
|
514
|
+
log2.error({ err: error }, "Migration 1.1.0 failed");
|
|
360
515
|
throw error;
|
|
361
516
|
}
|
|
362
517
|
}
|
|
363
518
|
if (!currentVersion || versionLessThan(currentVersion, "1.2.0")) {
|
|
364
519
|
try {
|
|
365
|
-
|
|
520
|
+
log2.info("Running migration 1.2.0: Extend sessions table for SQLite backend");
|
|
366
521
|
const addColumnIfNotExists = (table, column, type) => {
|
|
367
522
|
try {
|
|
368
523
|
db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${type}`);
|
|
@@ -391,14 +546,14 @@ function runMigrations(db) {
|
|
|
391
546
|
);
|
|
392
547
|
}
|
|
393
548
|
db.exec("CREATE INDEX IF NOT EXISTS idx_sessions_updated ON sessions(updated_at DESC)");
|
|
394
|
-
|
|
549
|
+
log2.info("Migration 1.2.0 complete: Sessions table extended");
|
|
395
550
|
} catch (error) {
|
|
396
|
-
|
|
551
|
+
log2.error({ err: error }, "Migration 1.2.0 failed");
|
|
397
552
|
throw error;
|
|
398
553
|
}
|
|
399
554
|
}
|
|
400
555
|
if (!currentVersion || versionLessThan(currentVersion, "1.9.0")) {
|
|
401
|
-
|
|
556
|
+
log2.info("Running migration 1.9.0: Upgrade embedding_cache to BLOB storage");
|
|
402
557
|
try {
|
|
403
558
|
db.exec(`DROP TABLE IF EXISTS embedding_cache`);
|
|
404
559
|
db.exec(`
|
|
@@ -414,14 +569,14 @@ function runMigrations(db) {
|
|
|
414
569
|
);
|
|
415
570
|
CREATE INDEX IF NOT EXISTS idx_embedding_cache_accessed ON embedding_cache(accessed_at);
|
|
416
571
|
`);
|
|
417
|
-
|
|
572
|
+
log2.info("Migration 1.9.0 complete: embedding_cache upgraded to BLOB storage");
|
|
418
573
|
} catch (error) {
|
|
419
|
-
|
|
574
|
+
log2.error({ err: error }, "Migration 1.9.0 failed");
|
|
420
575
|
throw error;
|
|
421
576
|
}
|
|
422
577
|
}
|
|
423
578
|
if (!currentVersion || versionLessThan(currentVersion, "1.10.0")) {
|
|
424
|
-
|
|
579
|
+
log2.info("Running migration 1.10.0: Add tool_config table for runtime tool management");
|
|
425
580
|
try {
|
|
426
581
|
db.exec(`
|
|
427
582
|
CREATE TABLE IF NOT EXISTS tool_config (
|
|
@@ -432,14 +587,14 @@ function runMigrations(db) {
|
|
|
432
587
|
updated_by INTEGER
|
|
433
588
|
);
|
|
434
589
|
`);
|
|
435
|
-
|
|
590
|
+
log2.info("Migration 1.10.0 complete: tool_config table created");
|
|
436
591
|
} catch (error) {
|
|
437
|
-
|
|
592
|
+
log2.error({ err: error }, "Migration 1.10.0 failed");
|
|
438
593
|
throw error;
|
|
439
594
|
}
|
|
440
595
|
}
|
|
441
596
|
if (!currentVersion || versionLessThan(currentVersion, "1.10.1")) {
|
|
442
|
-
|
|
597
|
+
log2.info("Running migration 1.10.1: Fix tool_config scope CHECK constraint (add admin-only)");
|
|
443
598
|
try {
|
|
444
599
|
db.transaction(() => {
|
|
445
600
|
db.exec(`
|
|
@@ -455,14 +610,14 @@ function runMigrations(db) {
|
|
|
455
610
|
ALTER TABLE tool_config_new RENAME TO tool_config;
|
|
456
611
|
`);
|
|
457
612
|
})();
|
|
458
|
-
|
|
613
|
+
log2.info("Migration 1.10.1 complete: tool_config CHECK constraint updated");
|
|
459
614
|
} catch (error) {
|
|
460
|
-
|
|
615
|
+
log2.error({ err: error }, "Migration 1.10.1 failed");
|
|
461
616
|
throw error;
|
|
462
617
|
}
|
|
463
618
|
}
|
|
464
619
|
if (!currentVersion || versionLessThan(currentVersion, "1.11.0")) {
|
|
465
|
-
|
|
620
|
+
log2.info("Running migration 1.11.0: Add tool_index tables for Tool RAG");
|
|
466
621
|
try {
|
|
467
622
|
db.exec(`
|
|
468
623
|
CREATE TABLE IF NOT EXISTS tool_index (
|
|
@@ -494,9 +649,58 @@ function runMigrations(db) {
|
|
|
494
649
|
VALUES (new.rowid, new.search_text, new.name);
|
|
495
650
|
END;
|
|
496
651
|
`);
|
|
497
|
-
|
|
652
|
+
log2.info("Migration 1.11.0 complete: tool_index tables created");
|
|
653
|
+
} catch (error) {
|
|
654
|
+
log2.error({ err: error }, "Migration 1.11.0 failed");
|
|
655
|
+
throw error;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
if (!currentVersion || versionLessThan(currentVersion, "1.12.0")) {
|
|
659
|
+
log2.info("Running migration 1.12.0: Add exec_audit table");
|
|
660
|
+
try {
|
|
661
|
+
db.exec(`
|
|
662
|
+
CREATE TABLE IF NOT EXISTS exec_audit (
|
|
663
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
664
|
+
timestamp INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
665
|
+
user_id INTEGER NOT NULL,
|
|
666
|
+
username TEXT,
|
|
667
|
+
tool TEXT NOT NULL,
|
|
668
|
+
command TEXT NOT NULL,
|
|
669
|
+
status TEXT NOT NULL DEFAULT 'pending',
|
|
670
|
+
exit_code INTEGER,
|
|
671
|
+
signal TEXT,
|
|
672
|
+
duration_ms INTEGER,
|
|
673
|
+
stdout TEXT,
|
|
674
|
+
stderr TEXT,
|
|
675
|
+
truncated INTEGER NOT NULL DEFAULT 0
|
|
676
|
+
);
|
|
677
|
+
|
|
678
|
+
CREATE INDEX IF NOT EXISTS idx_exec_audit_timestamp ON exec_audit(timestamp DESC);
|
|
679
|
+
CREATE INDEX IF NOT EXISTS idx_exec_audit_user ON exec_audit(user_id);
|
|
680
|
+
`);
|
|
681
|
+
log2.info("Migration 1.12.0 complete: exec_audit table created");
|
|
682
|
+
} catch (error) {
|
|
683
|
+
log2.error({ err: error }, "Migration 1.12.0 failed");
|
|
684
|
+
throw error;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
if (!currentVersion || versionLessThan(currentVersion, "1.13.0")) {
|
|
688
|
+
log2.info("Running migration 1.13.0: Add token usage columns to sessions");
|
|
689
|
+
try {
|
|
690
|
+
const addColumnIfNotExists = (table, column, type) => {
|
|
691
|
+
try {
|
|
692
|
+
db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${type}`);
|
|
693
|
+
} catch (e) {
|
|
694
|
+
if (!e.message.includes("duplicate column name")) {
|
|
695
|
+
throw e;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
addColumnIfNotExists("sessions", "input_tokens", "INTEGER DEFAULT 0");
|
|
700
|
+
addColumnIfNotExists("sessions", "output_tokens", "INTEGER DEFAULT 0");
|
|
701
|
+
log2.info("Migration 1.13.0 complete: Token usage columns added to sessions");
|
|
498
702
|
} catch (error) {
|
|
499
|
-
|
|
703
|
+
log2.error({ err: error }, "Migration 1.13.0 failed");
|
|
500
704
|
throw error;
|
|
501
705
|
}
|
|
502
706
|
}
|
|
@@ -504,22 +708,22 @@ function runMigrations(db) {
|
|
|
504
708
|
}
|
|
505
709
|
|
|
506
710
|
// src/memory/database.ts
|
|
507
|
-
var
|
|
711
|
+
var log3 = createLogger("Memory");
|
|
508
712
|
var MemoryDatabase = class {
|
|
509
713
|
db;
|
|
510
714
|
config;
|
|
511
715
|
vectorReady = false;
|
|
512
716
|
constructor(config) {
|
|
513
717
|
this.config = config;
|
|
514
|
-
const dir =
|
|
515
|
-
if (!
|
|
516
|
-
|
|
718
|
+
const dir = dirname2(config.path);
|
|
719
|
+
if (!existsSync2(dir)) {
|
|
720
|
+
mkdirSync2(dir, { recursive: true });
|
|
517
721
|
}
|
|
518
|
-
this.db = new
|
|
519
|
-
verbose: process.env.DEBUG_SQL ? (msg) =>
|
|
722
|
+
this.db = new Database2(config.path, {
|
|
723
|
+
verbose: process.env.DEBUG_SQL ? (msg) => log3.debug(String(msg)) : void 0
|
|
520
724
|
});
|
|
521
725
|
try {
|
|
522
|
-
|
|
726
|
+
chmodSync2(config.path, 384);
|
|
523
727
|
} catch {
|
|
524
728
|
}
|
|
525
729
|
this.db.pragma("journal_mode = WAL");
|
|
@@ -556,16 +760,16 @@ var MemoryDatabase = class {
|
|
|
556
760
|
ensureVectorTables(this.db, dims);
|
|
557
761
|
this.vectorReady = true;
|
|
558
762
|
} catch (error) {
|
|
559
|
-
|
|
560
|
-
|
|
763
|
+
log3.warn(`sqlite-vec not available, vector search disabled: ${error.message}`);
|
|
764
|
+
log3.warn("Falling back to keyword-only search");
|
|
561
765
|
this.config.enableVectorSearch = false;
|
|
562
766
|
}
|
|
563
767
|
}
|
|
564
768
|
migrate(from, to) {
|
|
565
|
-
|
|
769
|
+
log3.info(`Migrating database from ${from} to ${to}...`);
|
|
566
770
|
runMigrations(this.db);
|
|
567
771
|
ensureSchema(this.db);
|
|
568
|
-
|
|
772
|
+
log3.info("Migration complete");
|
|
569
773
|
}
|
|
570
774
|
getDb() {
|
|
571
775
|
return this.db;
|
|
@@ -667,8 +871,8 @@ function closeDatabase() {
|
|
|
667
871
|
}
|
|
668
872
|
|
|
669
873
|
// src/memory/agent/knowledge.ts
|
|
670
|
-
import { readFileSync, existsSync as
|
|
671
|
-
import { join } from "path";
|
|
874
|
+
import { readFileSync, existsSync as existsSync3, readdirSync, statSync } from "fs";
|
|
875
|
+
import { join as join2 } from "path";
|
|
672
876
|
var KnowledgeIndexer = class {
|
|
673
877
|
constructor(db, workspaceDir, embedder, vectorEnabled) {
|
|
674
878
|
this.db = db;
|
|
@@ -691,7 +895,7 @@ var KnowledgeIndexer = class {
|
|
|
691
895
|
return { indexed, skipped };
|
|
692
896
|
}
|
|
693
897
|
async indexFile(absPath) {
|
|
694
|
-
if (!
|
|
898
|
+
if (!existsSync3(absPath) || !absPath.endsWith(".md")) {
|
|
695
899
|
return false;
|
|
696
900
|
}
|
|
697
901
|
const content = readFileSync(absPath, "utf-8");
|
|
@@ -739,15 +943,15 @@ var KnowledgeIndexer = class {
|
|
|
739
943
|
}
|
|
740
944
|
listMemoryFiles() {
|
|
741
945
|
const files = [];
|
|
742
|
-
const memoryMd =
|
|
743
|
-
if (
|
|
946
|
+
const memoryMd = join2(this.workspaceDir, "MEMORY.md");
|
|
947
|
+
if (existsSync3(memoryMd)) {
|
|
744
948
|
files.push(memoryMd);
|
|
745
949
|
}
|
|
746
|
-
const memoryDir =
|
|
747
|
-
if (
|
|
950
|
+
const memoryDir = join2(this.workspaceDir, "memory");
|
|
951
|
+
if (existsSync3(memoryDir)) {
|
|
748
952
|
const entries = readdirSync(memoryDir);
|
|
749
953
|
for (const entry of entries) {
|
|
750
|
-
const absPath =
|
|
954
|
+
const absPath = join2(memoryDir, entry);
|
|
751
955
|
if (statSync(absPath).isFile() && entry.endsWith(".md")) {
|
|
752
956
|
files.push(absPath);
|
|
753
957
|
}
|
|
@@ -814,7 +1018,7 @@ var KnowledgeIndexer = class {
|
|
|
814
1018
|
|
|
815
1019
|
// src/memory/agent/sessions.ts
|
|
816
1020
|
import { randomUUID } from "crypto";
|
|
817
|
-
var
|
|
1021
|
+
var log4 = createLogger("Memory");
|
|
818
1022
|
var SessionStore = class {
|
|
819
1023
|
constructor(db, embedder, vectorEnabled) {
|
|
820
1024
|
this.db = db;
|
|
@@ -938,9 +1142,9 @@ ${session.summary}`;
|
|
|
938
1142
|
this.db.prepare(`DELETE FROM knowledge_vec WHERE id = ?`).run(knowledgeId);
|
|
939
1143
|
this.db.prepare(`INSERT INTO knowledge_vec (id, embedding) VALUES (?, ?)`).run(knowledgeId, embeddingBuffer);
|
|
940
1144
|
}
|
|
941
|
-
|
|
1145
|
+
log4.info(`Indexed session ${sessionId} to knowledge base`);
|
|
942
1146
|
} catch (error) {
|
|
943
|
-
|
|
1147
|
+
log4.error({ err: error }, "Error indexing session");
|
|
944
1148
|
}
|
|
945
1149
|
}
|
|
946
1150
|
deleteSession(sessionId) {
|
|
@@ -1305,7 +1509,7 @@ var UserStore = class {
|
|
|
1305
1509
|
};
|
|
1306
1510
|
|
|
1307
1511
|
// src/memory/search/hybrid.ts
|
|
1308
|
-
var
|
|
1512
|
+
var log5 = createLogger("Memory");
|
|
1309
1513
|
function escapeFts5Query(query) {
|
|
1310
1514
|
return query.replace(/["\*\-\+\(\)\:\^\~\?\.\@\#\$\%\&\!\[\]\{\}\|\\\/<>=,;'`]/g, " ").replace(/\s+/g, " ").trim();
|
|
1311
1515
|
}
|
|
@@ -1353,7 +1557,7 @@ var HybridSearch = class {
|
|
|
1353
1557
|
vectorScore: 1 - row.distance
|
|
1354
1558
|
}));
|
|
1355
1559
|
} catch (error) {
|
|
1356
|
-
|
|
1560
|
+
log5.error({ err: error }, "Vector search error (knowledge)");
|
|
1357
1561
|
return [];
|
|
1358
1562
|
}
|
|
1359
1563
|
}
|
|
@@ -1376,7 +1580,7 @@ var HybridSearch = class {
|
|
|
1376
1580
|
keywordScore: this.bm25ToScore(row.score)
|
|
1377
1581
|
}));
|
|
1378
1582
|
} catch (error) {
|
|
1379
|
-
|
|
1583
|
+
log5.error({ err: error }, "FTS5 search error (knowledge)");
|
|
1380
1584
|
return [];
|
|
1381
1585
|
}
|
|
1382
1586
|
}
|
|
@@ -1411,7 +1615,7 @@ var HybridSearch = class {
|
|
|
1411
1615
|
vectorScore: 1 - row.distance
|
|
1412
1616
|
}));
|
|
1413
1617
|
} catch (error) {
|
|
1414
|
-
|
|
1618
|
+
log5.error({ err: error }, "Vector search error (messages)");
|
|
1415
1619
|
return [];
|
|
1416
1620
|
}
|
|
1417
1621
|
}
|
|
@@ -1441,7 +1645,7 @@ var HybridSearch = class {
|
|
|
1441
1645
|
keywordScore: this.bm25ToScore(row.score)
|
|
1442
1646
|
}));
|
|
1443
1647
|
} catch (error) {
|
|
1444
|
-
|
|
1648
|
+
log5.error({ err: error }, "FTS5 search error (messages)");
|
|
1445
1649
|
return [];
|
|
1446
1650
|
}
|
|
1447
1651
|
}
|
|
@@ -1471,7 +1675,7 @@ var HybridSearch = class {
|
|
|
1471
1675
|
};
|
|
1472
1676
|
|
|
1473
1677
|
// src/memory/search/context.ts
|
|
1474
|
-
var
|
|
1678
|
+
var log6 = createLogger("Memory");
|
|
1475
1679
|
var ContextBuilder = class {
|
|
1476
1680
|
constructor(db, embedder, vectorEnabled) {
|
|
1477
1681
|
this.db = db;
|
|
@@ -1505,7 +1709,7 @@ var ContextBuilder = class {
|
|
|
1505
1709
|
});
|
|
1506
1710
|
relevantKnowledge.push(...knowledgeResults.map((r) => r.text));
|
|
1507
1711
|
} catch (error) {
|
|
1508
|
-
|
|
1712
|
+
log6.warn({ err: error }, "Knowledge search failed");
|
|
1509
1713
|
}
|
|
1510
1714
|
}
|
|
1511
1715
|
const recentTextsSet = new Set(
|
|
@@ -1535,7 +1739,7 @@ var ContextBuilder = class {
|
|
|
1535
1739
|
}
|
|
1536
1740
|
}
|
|
1537
1741
|
} catch (error) {
|
|
1538
|
-
|
|
1742
|
+
log6.warn({ err: error }, "Feed search failed");
|
|
1539
1743
|
}
|
|
1540
1744
|
if (relevantFeed.length === 0 && recentTgMessages.length > 0) {
|
|
1541
1745
|
const recentTexts = recentTgMessages.filter((m) => m.text && m.text.length > 0).slice(-maxRelevantChunks).map((m) => {
|
|
@@ -1573,6 +1777,11 @@ function initializeMemory(config) {
|
|
|
1573
1777
|
}
|
|
1574
1778
|
|
|
1575
1779
|
export {
|
|
1780
|
+
JOURNAL_SCHEMA,
|
|
1781
|
+
USED_TRANSACTIONS_SCHEMA,
|
|
1782
|
+
openModuleDb,
|
|
1783
|
+
createDbWrapper,
|
|
1784
|
+
migrateFromMainDb,
|
|
1576
1785
|
ensureSchema,
|
|
1577
1786
|
ensureVectorTables,
|
|
1578
1787
|
getSchemaVersion,
|