@kenhkl/agent-gate 0.9.0 → 0.9.1
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/shared/storage/db.d.ts +7 -0
- package/dist/shared/storage/db.js +212 -44
- package/dist/v1/cli.js +322 -50
- package/dist/v1/server.js +229 -50
- package/dist/v3/cli.js +273 -93
- package/package.json +1 -1
package/dist/v1/server.js
CHANGED
|
@@ -52,15 +52,29 @@ function sessionHistoryLimit() {
|
|
|
52
52
|
const v = Number.isFinite(n) ? n : DEFAULT_MAX_SESSIONS;
|
|
53
53
|
return Math.max(1, Math.min(v, 50));
|
|
54
54
|
}
|
|
55
|
-
function buildSessionHistory(queryToolCalls2, sessionId) {
|
|
55
|
+
function buildSessionHistory(queryToolCalls2, sessionId, listAgentTypes) {
|
|
56
56
|
if (!isMultiSessionEnabled()) return [];
|
|
57
57
|
const maxSessions = sessionHistoryLimit();
|
|
58
58
|
try {
|
|
59
59
|
const scanLimit = Math.max(DEFAULT_HISTORY_LIMIT, maxSessions * DEFAULT_ACTIONS_PER_SESSION);
|
|
60
|
-
const
|
|
60
|
+
const agentTypes = listAgentTypes?.() ?? [];
|
|
61
|
+
let recent;
|
|
62
|
+
if (agentTypes.length > 1) {
|
|
63
|
+
const perAgent = Math.max(
|
|
64
|
+
DEFAULT_ACTIONS_PER_SESSION,
|
|
65
|
+
Math.floor(DEFAULT_HISTORY_LIMIT / agentTypes.length)
|
|
66
|
+
);
|
|
67
|
+
recent = agentTypes.flatMap((a) => queryToolCalls2({ limit: perAgent, agentType: a }));
|
|
68
|
+
} else {
|
|
69
|
+
recent = queryToolCalls2({ limit: scanLimit });
|
|
70
|
+
}
|
|
61
71
|
const bySession = /* @__PURE__ */ new Map();
|
|
72
|
+
const sessionAgent = /* @__PURE__ */ new Map();
|
|
62
73
|
for (const tc of recent) {
|
|
63
74
|
if (tc.session_id === sessionId) continue;
|
|
75
|
+
if (tc.agent_type && !sessionAgent.has(tc.session_id)) {
|
|
76
|
+
sessionAgent.set(tc.session_id, tc.agent_type);
|
|
77
|
+
}
|
|
64
78
|
const list = bySession.get(tc.session_id) || [];
|
|
65
79
|
list.unshift({
|
|
66
80
|
action_type: tc.action_type,
|
|
@@ -71,6 +85,7 @@ function buildSessionHistory(queryToolCalls2, sessionId) {
|
|
|
71
85
|
}
|
|
72
86
|
return [...bySession.entries()].slice(0, maxSessions).map(([sid, actions]) => ({
|
|
73
87
|
session_id: sid,
|
|
88
|
+
...sessionAgent.get(sid) ? { agent_id: sessionAgent.get(sid) } : {},
|
|
74
89
|
action_history: actions.slice(0, DEFAULT_ACTIONS_PER_SESSION)
|
|
75
90
|
}));
|
|
76
91
|
} catch {
|
|
@@ -346,6 +361,12 @@ function bindAgent(principal, agentType) {
|
|
|
346
361
|
function maxBodyBytes() {
|
|
347
362
|
return envInt("AGENT_GATE_MAX_BODY_BYTES", 16 * 1024 * 1024);
|
|
348
363
|
}
|
|
364
|
+
function maxRecordBytes() {
|
|
365
|
+
return envInt("AGENT_GATE_MAX_RECORD_BYTES", 1024 * 1024);
|
|
366
|
+
}
|
|
367
|
+
function diskDegradePercent() {
|
|
368
|
+
return envInt("AGENT_GATE_DISK_DEGRADE_PERCENT", 2);
|
|
369
|
+
}
|
|
349
370
|
function rateLimitQps() {
|
|
350
371
|
return envInt("AGENT_GATE_RATE_LIMIT_QPS", 100);
|
|
351
372
|
}
|
|
@@ -622,6 +643,66 @@ var init_moss_client = __esm({
|
|
|
622
643
|
}
|
|
623
644
|
});
|
|
624
645
|
|
|
646
|
+
// src/shared/storage/record-guard.ts
|
|
647
|
+
function clipText(value, limit = maxRecordBytes()) {
|
|
648
|
+
const s = String(value ?? "");
|
|
649
|
+
if (limit <= 0 || s.length <= limit) return s;
|
|
650
|
+
const head = s.slice(0, limit);
|
|
651
|
+
const digest = (0, import_crypto.createHash)("sha256").update(s).digest("hex").slice(0, 16);
|
|
652
|
+
return `${head}\u2026[truncated ${s.length - limit}B sha256:${digest}]`;
|
|
653
|
+
}
|
|
654
|
+
function diskFreePercent(targetPath) {
|
|
655
|
+
try {
|
|
656
|
+
const st = (0, import_fs.statfsSync)((0, import_path.dirname)(targetPath));
|
|
657
|
+
const total = Number(st.blocks) || 0;
|
|
658
|
+
const avail = Number(st.bavail) || 0;
|
|
659
|
+
if (!total) return 100;
|
|
660
|
+
return avail / total * 100;
|
|
661
|
+
} catch {
|
|
662
|
+
return 100;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
function shouldDegradeWrites(targetPath) {
|
|
666
|
+
const degrade = diskDegradePercent();
|
|
667
|
+
if (degrade <= 0) return false;
|
|
668
|
+
return diskFreePercent(targetPath) < degrade;
|
|
669
|
+
}
|
|
670
|
+
function degradeRecord(rec) {
|
|
671
|
+
const command = String(rec.command ?? "");
|
|
672
|
+
const clipped = command.length > DEGRADE_COMMAND_BYTES ? `${command.slice(0, DEGRADE_COMMAND_BYTES)}\u2026[degraded]` : command;
|
|
673
|
+
return {
|
|
674
|
+
...rec,
|
|
675
|
+
command: clipped,
|
|
676
|
+
action_detail: String(rec.action_detail ?? "") > "" ? String(rec.action_detail).slice(0, DEGRADE_COMMAND_BYTES) : "",
|
|
677
|
+
description: "",
|
|
678
|
+
reason: String(rec.reason ?? "").slice(0, 256)
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
function prepareRecordForStorage(rec, targetPath) {
|
|
682
|
+
let out = {
|
|
683
|
+
...rec,
|
|
684
|
+
command: clipText(rec.command),
|
|
685
|
+
action_detail: clipText(rec.action_detail),
|
|
686
|
+
reason: clipText(rec.reason, 4096),
|
|
687
|
+
description: clipText(rec.description, 4096)
|
|
688
|
+
};
|
|
689
|
+
const degraded = shouldDegradeWrites(targetPath);
|
|
690
|
+
if (degraded) out = degradeRecord(out);
|
|
691
|
+
return { record: out, degraded };
|
|
692
|
+
}
|
|
693
|
+
var import_crypto, import_fs, import_path, DEGRADE_COMMAND_BYTES;
|
|
694
|
+
var init_record_guard = __esm({
|
|
695
|
+
"src/shared/storage/record-guard.ts"() {
|
|
696
|
+
"use strict";
|
|
697
|
+
init_cjs_shims();
|
|
698
|
+
import_crypto = require("crypto");
|
|
699
|
+
import_fs = require("fs");
|
|
700
|
+
import_path = require("path");
|
|
701
|
+
init_security();
|
|
702
|
+
DEGRADE_COMMAND_BYTES = 512;
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
|
|
625
706
|
// src/shared/storage/json-storage.ts
|
|
626
707
|
var json_storage_exports = {};
|
|
627
708
|
__export(json_storage_exports, {
|
|
@@ -632,17 +713,18 @@ function nowISO() {
|
|
|
632
713
|
}
|
|
633
714
|
function getDefaultDataDir() {
|
|
634
715
|
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
635
|
-
const xdgData = process.env.XDG_DATA_HOME || (0,
|
|
636
|
-
return (0,
|
|
716
|
+
const xdgData = process.env.XDG_DATA_HOME || (0, import_path2.join)(home, ".local", "share");
|
|
717
|
+
return (0, import_path2.join)(xdgData, "agent_gate", "storage");
|
|
637
718
|
}
|
|
638
|
-
var
|
|
719
|
+
var import_fs2, import_path2, JsonStorage;
|
|
639
720
|
var init_json_storage = __esm({
|
|
640
721
|
"src/shared/storage/json-storage.ts"() {
|
|
641
722
|
"use strict";
|
|
642
723
|
init_cjs_shims();
|
|
643
|
-
|
|
644
|
-
|
|
724
|
+
import_fs2 = require("fs");
|
|
725
|
+
import_path2 = require("path");
|
|
645
726
|
init_security();
|
|
727
|
+
init_record_guard();
|
|
646
728
|
JsonStorage = class {
|
|
647
729
|
dataDir;
|
|
648
730
|
agentsPath;
|
|
@@ -655,11 +737,11 @@ var init_json_storage = __esm({
|
|
|
655
737
|
sessionLocks = /* @__PURE__ */ new Map();
|
|
656
738
|
constructor(dataDir) {
|
|
657
739
|
this.dataDir = dataDir || process.env.AGENT_GATE_DATA_DIR || getDefaultDataDir();
|
|
658
|
-
this.agentsPath = (0,
|
|
659
|
-
this.sessionsPath = (0,
|
|
660
|
-
this.toolCallsDir = (0,
|
|
661
|
-
if (!(0,
|
|
662
|
-
if (!(0,
|
|
740
|
+
this.agentsPath = (0, import_path2.join)(this.dataDir, "agents.json");
|
|
741
|
+
this.sessionsPath = (0, import_path2.join)(this.dataDir, "sessions.json");
|
|
742
|
+
this.toolCallsDir = (0, import_path2.join)(this.dataDir, "tool_calls");
|
|
743
|
+
if (!(0, import_fs2.existsSync)(this.dataDir)) (0, import_fs2.mkdirSync)(this.dataDir, { recursive: true, mode: 448 });
|
|
744
|
+
if (!(0, import_fs2.existsSync)(this.toolCallsDir)) (0, import_fs2.mkdirSync)(this.toolCallsDir, { recursive: true, mode: 448 });
|
|
663
745
|
this.hardenStoragePermissions();
|
|
664
746
|
this.nextId = this.recoverNextId();
|
|
665
747
|
}
|
|
@@ -671,10 +753,10 @@ var init_json_storage = __esm({
|
|
|
671
753
|
const uid = process.getuid?.();
|
|
672
754
|
const tighten = (p, mode) => {
|
|
673
755
|
try {
|
|
674
|
-
const st = (0,
|
|
756
|
+
const st = (0, import_fs2.statSync)(p);
|
|
675
757
|
if (st.isSymbolicLink()) return;
|
|
676
758
|
if (uid !== void 0 && st.uid !== uid) return;
|
|
677
|
-
if ((st.mode & 511) !== mode) (0,
|
|
759
|
+
if ((st.mode & 511) !== mode) (0, import_fs2.chmodSync)(p, mode);
|
|
678
760
|
} catch {
|
|
679
761
|
}
|
|
680
762
|
};
|
|
@@ -683,9 +765,9 @@ var init_json_storage = __esm({
|
|
|
683
765
|
tighten(this.agentsPath, 384);
|
|
684
766
|
tighten(this.sessionsPath, 384);
|
|
685
767
|
try {
|
|
686
|
-
for (const f of (0,
|
|
768
|
+
for (const f of (0, import_fs2.readdirSync)(this.toolCallsDir)) {
|
|
687
769
|
if (f.endsWith(".json") || f.endsWith(".tmp")) {
|
|
688
|
-
tighten((0,
|
|
770
|
+
tighten((0, import_path2.join)(this.toolCallsDir, f), 384);
|
|
689
771
|
}
|
|
690
772
|
}
|
|
691
773
|
} catch {
|
|
@@ -695,9 +777,9 @@ var init_json_storage = __esm({
|
|
|
695
777
|
recoverNextId() {
|
|
696
778
|
let maxId = 0;
|
|
697
779
|
try {
|
|
698
|
-
const files = (0,
|
|
780
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
699
781
|
for (const file of files) {
|
|
700
|
-
const records = this.readJsonFile((0,
|
|
782
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
701
783
|
for (const r of records) {
|
|
702
784
|
if (r.id > maxId) maxId = r.id;
|
|
703
785
|
}
|
|
@@ -708,19 +790,19 @@ var init_json_storage = __esm({
|
|
|
708
790
|
}
|
|
709
791
|
readJsonFile(path, fallback) {
|
|
710
792
|
try {
|
|
711
|
-
if (!(0,
|
|
712
|
-
return JSON.parse((0,
|
|
793
|
+
if (!(0, import_fs2.existsSync)(path)) return fallback;
|
|
794
|
+
return JSON.parse((0, import_fs2.readFileSync)(path, "utf-8"));
|
|
713
795
|
} catch {
|
|
714
796
|
return fallback;
|
|
715
797
|
}
|
|
716
798
|
}
|
|
717
799
|
/** 原子写入:先写 .tmp 再 rename(rename 保留 tmp 的 0600 权限) */
|
|
718
800
|
writeJsonFile(path, data) {
|
|
719
|
-
const dir = (0,
|
|
720
|
-
if (!(0,
|
|
801
|
+
const dir = (0, import_path2.dirname)(path);
|
|
802
|
+
if (!(0, import_fs2.existsSync)(dir)) (0, import_fs2.mkdirSync)(dir, { recursive: true, mode: 448 });
|
|
721
803
|
const tmp = path + ".tmp";
|
|
722
|
-
(0,
|
|
723
|
-
(0,
|
|
804
|
+
(0, import_fs2.writeFileSync)(tmp, JSON.stringify(data, null, 2), { encoding: "utf-8", mode: 384 });
|
|
805
|
+
(0, import_fs2.renameSync)(tmp, path);
|
|
724
806
|
}
|
|
725
807
|
loadAgents() {
|
|
726
808
|
const raw = this.readJsonFile(this.agentsPath, {});
|
|
@@ -738,12 +820,12 @@ var init_json_storage = __esm({
|
|
|
738
820
|
}
|
|
739
821
|
loadToolCalls(sessionId) {
|
|
740
822
|
return this.readJsonFile(
|
|
741
|
-
(0,
|
|
823
|
+
(0, import_path2.join)(this.toolCallsDir, `${sessionId}.json`),
|
|
742
824
|
[]
|
|
743
825
|
);
|
|
744
826
|
}
|
|
745
827
|
saveToolCalls(sessionId, records) {
|
|
746
|
-
this.writeJsonFile((0,
|
|
828
|
+
this.writeJsonFile((0, import_path2.join)(this.toolCallsDir, `${sessionId}.json`), records);
|
|
747
829
|
}
|
|
748
830
|
getOrCreateAgent(agentType, hostname, version2 = "") {
|
|
749
831
|
const agents = this.loadAgents();
|
|
@@ -788,6 +870,10 @@ var init_json_storage = __esm({
|
|
|
788
870
|
return Math.max(...records.map((r) => r.seq)) + 1;
|
|
789
871
|
}
|
|
790
872
|
insertToolCall(record) {
|
|
873
|
+
record = prepareRecordForStorage(
|
|
874
|
+
record,
|
|
875
|
+
this.dataDir
|
|
876
|
+
).record;
|
|
791
877
|
const sessionId = record.session_id;
|
|
792
878
|
const prev = this.sessionLocks.get(sessionId) ?? Promise.resolve();
|
|
793
879
|
const next = prev.then(() => {
|
|
@@ -825,9 +911,9 @@ var init_json_storage = __esm({
|
|
|
825
911
|
allRecords = records.map((r) => this.enrichRecord(r, sessions, agents));
|
|
826
912
|
} else {
|
|
827
913
|
try {
|
|
828
|
-
const files = (0,
|
|
914
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
829
915
|
for (const file of files) {
|
|
830
|
-
const records = this.readJsonFile((0,
|
|
916
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
831
917
|
allRecords.push(...records.map((r) => this.enrichRecord(r, sessions, agents)));
|
|
832
918
|
}
|
|
833
919
|
} catch {
|
|
@@ -861,9 +947,9 @@ var init_json_storage = __esm({
|
|
|
861
947
|
const agents = this.loadAgents();
|
|
862
948
|
let allRecords = [];
|
|
863
949
|
try {
|
|
864
|
-
const files = (0,
|
|
950
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
865
951
|
for (const file of files) {
|
|
866
|
-
const records = this.readJsonFile((0,
|
|
952
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
867
953
|
allRecords.push(...records);
|
|
868
954
|
}
|
|
869
955
|
} catch {
|
|
@@ -902,12 +988,12 @@ var init_json_storage = __esm({
|
|
|
902
988
|
let jsonFiles = 0;
|
|
903
989
|
let jsonBytes = 0;
|
|
904
990
|
try {
|
|
905
|
-
const files = (0,
|
|
991
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
906
992
|
for (const file of files) {
|
|
907
|
-
const stat = (0,
|
|
993
|
+
const stat = (0, import_fs2.statSync)((0, import_path2.join)(this.toolCallsDir, file));
|
|
908
994
|
jsonFiles++;
|
|
909
995
|
jsonBytes += stat.size;
|
|
910
|
-
const records = this.readJsonFile((0,
|
|
996
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
911
997
|
toolCalls += records.length;
|
|
912
998
|
}
|
|
913
999
|
} catch {
|
|
@@ -917,9 +1003,9 @@ var init_json_storage = __esm({
|
|
|
917
1003
|
clearToolCalls() {
|
|
918
1004
|
let removed = 0;
|
|
919
1005
|
try {
|
|
920
|
-
const files = (0,
|
|
1006
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
921
1007
|
for (const file of files) {
|
|
922
|
-
(0,
|
|
1008
|
+
(0, import_fs2.unlinkSync)((0, import_path2.join)(this.toolCallsDir, file));
|
|
923
1009
|
removed++;
|
|
924
1010
|
}
|
|
925
1011
|
} catch {
|
|
@@ -937,6 +1023,49 @@ var init_json_storage = __esm({
|
|
|
937
1023
|
hostname: agent?.hostname || ""
|
|
938
1024
|
};
|
|
939
1025
|
}
|
|
1026
|
+
/**
|
|
1027
|
+
* 归档最老的记录:JSON 模式下每个 session 一个文件,这里把所有 session 的记录
|
|
1028
|
+
* 汇总排序,最老的 (total - keep) 条写进 `<dataDir>/archive/*.jsonl.gz`,
|
|
1029
|
+
* 再从各自的 session 文件里移除。数据不丢,只是不在主目录了。
|
|
1030
|
+
*/
|
|
1031
|
+
archiveOldRecords(keep) {
|
|
1032
|
+
if (keep <= 0 || !(0, import_fs2.existsSync)(this.toolCallsDir)) return { archived: 0, file: "" };
|
|
1033
|
+
const bySession = /* @__PURE__ */ new Map();
|
|
1034
|
+
let total = 0;
|
|
1035
|
+
for (const f of (0, import_fs2.readdirSync)(this.toolCallsDir)) {
|
|
1036
|
+
if (!f.endsWith(".json")) continue;
|
|
1037
|
+
const sid = f.slice(0, -".json".length);
|
|
1038
|
+
const recs = this.loadToolCalls(sid);
|
|
1039
|
+
if (recs.length) {
|
|
1040
|
+
bySession.set(sid, recs);
|
|
1041
|
+
total += recs.length;
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
if (total <= keep) return { archived: 0, file: "" };
|
|
1045
|
+
const flat = [];
|
|
1046
|
+
for (const [sid, recs] of bySession) for (const rec of recs) flat.push({ sid, rec });
|
|
1047
|
+
flat.sort((a, b) => String(a.rec.called_at).localeCompare(String(b.rec.called_at)));
|
|
1048
|
+
const archiveCount = total - keep;
|
|
1049
|
+
const toArchive = flat.slice(0, archiveCount);
|
|
1050
|
+
const archiveIds = new Set(toArchive.map((x) => String(x.rec.id)));
|
|
1051
|
+
const { mkdirSync: mkdirSync4, writeFileSync: writeFileSync3 } = require("fs");
|
|
1052
|
+
const { join: join4 } = require("path");
|
|
1053
|
+
const { gzipSync } = require("zlib");
|
|
1054
|
+
const archiveDir = join4(this.dataDir, "archive");
|
|
1055
|
+
if (!(0, import_fs2.existsSync)(archiveDir)) mkdirSync4(archiveDir, { recursive: true, mode: 448 });
|
|
1056
|
+
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
1057
|
+
const file = join4(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
|
|
1058
|
+
const payload = toArchive.map((x) => JSON.stringify(x.rec)).join("\n") + "\n";
|
|
1059
|
+
writeFileSync3(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
|
|
1060
|
+
for (const [sid, recs] of bySession) {
|
|
1061
|
+
const remain = recs.filter((r) => !archiveIds.has(String(r.id)));
|
|
1062
|
+
if (remain.length !== recs.length) this.saveToolCalls(sid, remain);
|
|
1063
|
+
}
|
|
1064
|
+
return { archived: toArchive.length, file };
|
|
1065
|
+
}
|
|
1066
|
+
/** JSON 模式没有需要回收的数据库页,归档已把文件挪走。 */
|
|
1067
|
+
vacuum() {
|
|
1068
|
+
}
|
|
940
1069
|
getAgentsList(agentType) {
|
|
941
1070
|
const agents = this.loadAgents();
|
|
942
1071
|
const sessions = this.loadSessions();
|
|
@@ -971,9 +1100,9 @@ var init_json_storage = __esm({
|
|
|
971
1100
|
getTimeline(limit = 100) {
|
|
972
1101
|
const buckets = /* @__PURE__ */ new Map();
|
|
973
1102
|
try {
|
|
974
|
-
const files = (0,
|
|
1103
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
975
1104
|
for (const file of files) {
|
|
976
|
-
const records = this.readJsonFile((0,
|
|
1105
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
977
1106
|
for (const r of records) {
|
|
978
1107
|
const minute = r.called_at.replace(/(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}):\d{2}/, "$1T$2");
|
|
979
1108
|
const bucket = buckets.get(minute) || { allow: 0, deny: 0 };
|
|
@@ -1004,16 +1133,17 @@ __export(sqlite_storage_exports, {
|
|
|
1004
1133
|
});
|
|
1005
1134
|
function getDefaultDbPath() {
|
|
1006
1135
|
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
1007
|
-
const xdgData = process.env.XDG_DATA_HOME || (0,
|
|
1008
|
-
return (0,
|
|
1136
|
+
const xdgData = process.env.XDG_DATA_HOME || (0, import_path3.join)(home, ".local", "share");
|
|
1137
|
+
return (0, import_path3.join)(xdgData, "agent_gate", "agent_gate_ts.db");
|
|
1009
1138
|
}
|
|
1010
|
-
var
|
|
1139
|
+
var import_fs3, import_path3, SCHEMA, SqliteStorage;
|
|
1011
1140
|
var init_sqlite_storage = __esm({
|
|
1012
1141
|
"src/shared/storage/sqlite-storage.ts"() {
|
|
1013
1142
|
"use strict";
|
|
1014
1143
|
init_cjs_shims();
|
|
1015
|
-
|
|
1016
|
-
|
|
1144
|
+
import_fs3 = require("fs");
|
|
1145
|
+
import_path3 = require("path");
|
|
1146
|
+
init_record_guard();
|
|
1017
1147
|
SCHEMA = `
|
|
1018
1148
|
CREATE TABLE IF NOT EXISTS agents (
|
|
1019
1149
|
id TEXT PRIMARY KEY,
|
|
@@ -1073,8 +1203,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1073
1203
|
);
|
|
1074
1204
|
}
|
|
1075
1205
|
const path = dbPath || getDefaultDbPath();
|
|
1076
|
-
const dir = (0,
|
|
1077
|
-
if (!(0,
|
|
1206
|
+
const dir = (0, import_path3.dirname)(path);
|
|
1207
|
+
if (!(0, import_fs3.existsSync)(dir)) (0, import_fs3.mkdirSync)(dir, { recursive: true });
|
|
1078
1208
|
this.dbPath = path;
|
|
1079
1209
|
this.db = new Database(path);
|
|
1080
1210
|
this.db.pragma("journal_mode = WAL");
|
|
@@ -1108,6 +1238,10 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1108
1238
|
return row.next;
|
|
1109
1239
|
}
|
|
1110
1240
|
insertToolCall(record) {
|
|
1241
|
+
record = prepareRecordForStorage(
|
|
1242
|
+
record,
|
|
1243
|
+
this.dbPath
|
|
1244
|
+
).record;
|
|
1111
1245
|
const result = this.db.transaction((rec) => {
|
|
1112
1246
|
const row = this.db.prepare(
|
|
1113
1247
|
"SELECT COALESCE(MAX(seq), 0) + 1 as next FROM tool_calls WHERE session_id = ?"
|
|
@@ -1248,6 +1382,49 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1248
1382
|
})();
|
|
1249
1383
|
return result;
|
|
1250
1384
|
}
|
|
1385
|
+
/**
|
|
1386
|
+
* 把最老的记录归档到本地压缩文件,主库只保留最近 `keep` 条。
|
|
1387
|
+
*
|
|
1388
|
+
* 归档文件是 **JSONL + gzip**(一行一条记录),放在 `<数据库目录>/archive/` 下,
|
|
1389
|
+
* 需要翻旧账时用 `zcat archive/xxx.jsonl.gz | grep ...` 即可 —— 数据不丢,只是
|
|
1390
|
+
* 不在主库里了。写完文件、校验落盘后才删主库行,最后 VACUUM 真正释放磁盘。
|
|
1391
|
+
*/
|
|
1392
|
+
archiveOldRecords(keep) {
|
|
1393
|
+
if (keep <= 0) return { archived: 0, file: "" };
|
|
1394
|
+
const total = this.db.prepare("SELECT COUNT(*) as n FROM tool_calls").get().n;
|
|
1395
|
+
if (total <= keep) return { archived: 0, file: "" };
|
|
1396
|
+
const cutoff = this.db.prepare(
|
|
1397
|
+
"SELECT id FROM tool_calls ORDER BY id LIMIT 1 OFFSET ?"
|
|
1398
|
+
).get(total - keep - 1);
|
|
1399
|
+
if (!cutoff) return { archived: 0, file: "" };
|
|
1400
|
+
const rows = this.db.prepare(
|
|
1401
|
+
"SELECT * FROM tool_calls WHERE id <= ? ORDER BY id"
|
|
1402
|
+
).all(cutoff.id);
|
|
1403
|
+
if (rows.length === 0) return { archived: 0, file: "" };
|
|
1404
|
+
const { writeFileSync: writeFileSync3, mkdirSync: mkdirSync4, existsSync: existsSync3 } = require("fs");
|
|
1405
|
+
const { dirname: dirname4, join: join4 } = require("path");
|
|
1406
|
+
const { gzipSync } = require("zlib");
|
|
1407
|
+
const archiveDir = join4(dirname4(this.dbPath), "archive");
|
|
1408
|
+
if (!existsSync3(archiveDir)) mkdirSync4(archiveDir, { recursive: true, mode: 448 });
|
|
1409
|
+
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
1410
|
+
const file = join4(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
|
|
1411
|
+
const payload = rows.map((r) => JSON.stringify(r)).join("\n") + "\n";
|
|
1412
|
+
writeFileSync3(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
|
|
1413
|
+
const deleted = this.db.transaction(() => {
|
|
1414
|
+
const res = this.db.prepare("DELETE FROM tool_calls WHERE id <= ?").run(cutoff.id);
|
|
1415
|
+
this.db.prepare(`
|
|
1416
|
+
UPDATE sessions SET tool_call_count = (
|
|
1417
|
+
SELECT COUNT(*) FROM tool_calls WHERE tool_calls.session_id = sessions.id
|
|
1418
|
+
)
|
|
1419
|
+
`).run();
|
|
1420
|
+
return Number(res.changes);
|
|
1421
|
+
})();
|
|
1422
|
+
return { archived: deleted, file };
|
|
1423
|
+
}
|
|
1424
|
+
/** 真正释放磁盘空间:DELETE 后必须 VACUUM,否则文件不会变小。 */
|
|
1425
|
+
vacuum() {
|
|
1426
|
+
this.db.exec("VACUUM");
|
|
1427
|
+
}
|
|
1251
1428
|
getAgentsList(agentType) {
|
|
1252
1429
|
const where = agentType ? "WHERE a.agent_type = ?" : "";
|
|
1253
1430
|
return this.db.prepare(`
|
|
@@ -1339,12 +1516,12 @@ function getDb(dbPath) {
|
|
|
1339
1516
|
try {
|
|
1340
1517
|
const Database = require("better-sqlite3");
|
|
1341
1518
|
const { existsSync: existsSync3, mkdirSync: mkdirSync4 } = require("fs");
|
|
1342
|
-
const { join: join4, dirname:
|
|
1519
|
+
const { join: join4, dirname: dirname4 } = require("path");
|
|
1343
1520
|
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
1344
1521
|
const xdgData = process.env.XDG_DATA_HOME || join4(home, ".local", "share");
|
|
1345
1522
|
const defaultPath = join4(xdgData, "agent_gate", "agent_gate_ts.db");
|
|
1346
1523
|
const path = dbPath || defaultPath;
|
|
1347
|
-
const dir =
|
|
1524
|
+
const dir = dirname4(path);
|
|
1348
1525
|
if (!existsSync3(dir)) mkdirSync4(dir, { recursive: true });
|
|
1349
1526
|
rawDb = new Database(path);
|
|
1350
1527
|
rawDb.pragma("journal_mode = WAL");
|
|
@@ -1409,7 +1586,7 @@ var import_zod = require("zod");
|
|
|
1409
1586
|
|
|
1410
1587
|
// src/version.ts
|
|
1411
1588
|
init_cjs_shims();
|
|
1412
|
-
var _version = true ? "0.9.
|
|
1589
|
+
var _version = true ? "0.9.1" : (() => {
|
|
1413
1590
|
try {
|
|
1414
1591
|
const require2 = createRequire(importMetaUrl);
|
|
1415
1592
|
const pkg = require2("../package.json");
|
|
@@ -1745,7 +1922,9 @@ async function analyzeRoute(c) {
|
|
|
1745
1922
|
if (isMultiSessionEnabled()) {
|
|
1746
1923
|
input.session_history = buildSessionHistory(
|
|
1747
1924
|
(params) => queryToolCalls(params),
|
|
1748
|
-
input.session_id
|
|
1925
|
+
input.session_id,
|
|
1926
|
+
// 多 agent 时按 agent 分组取历史,避免高频 agent 把别人的动作挤出窗口
|
|
1927
|
+
() => getAgentsList().map((a) => a.agent_type)
|
|
1749
1928
|
);
|
|
1750
1929
|
}
|
|
1751
1930
|
const mossRequest = normalize(input, context);
|
|
@@ -2043,9 +2222,9 @@ async function dashboardRoute(c) {
|
|
|
2043
2222
|
}
|
|
2044
2223
|
}
|
|
2045
2224
|
const { readFileSync: readFileSync3, existsSync: existsSync3 } = await import("fs");
|
|
2046
|
-
const { resolve, dirname:
|
|
2225
|
+
const { resolve, dirname: dirname4 } = await import("path");
|
|
2047
2226
|
const { fileURLToPath } = await import("url");
|
|
2048
|
-
const __dirname =
|
|
2227
|
+
const __dirname = dirname4(fileURLToPath(importMetaUrl));
|
|
2049
2228
|
const paths = [
|
|
2050
2229
|
resolve(__dirname, "dashboard.html"),
|
|
2051
2230
|
resolve(__dirname, "../dashboard.html"),
|