@kenhkl/agent-gate 0.9.2 → 0.9.4
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/v1/cli.js +93 -44
- package/dist/v1/server.js +141 -92
- package/dist/v3/cli.js +121 -72
- package/package.json +1 -1
package/dist/v1/cli.js
CHANGED
|
@@ -177,8 +177,8 @@ function readFdToken() {
|
|
|
177
177
|
if (!raw) return "";
|
|
178
178
|
const fd = parseInt(raw, 10);
|
|
179
179
|
if (!Number.isFinite(fd)) return "";
|
|
180
|
-
const
|
|
181
|
-
if (
|
|
180
|
+
const cached2 = fdTokenCache.get(fd);
|
|
181
|
+
if (cached2 !== void 0) return cached2;
|
|
182
182
|
try {
|
|
183
183
|
const buf = Buffer.alloc(8192);
|
|
184
184
|
const n = (0, import_node_fs.readSync)(fd, buf, 0, buf.length, null);
|
|
@@ -420,7 +420,7 @@ function agentInflight() {
|
|
|
420
420
|
return envInt("AGENT_GATE_MAX_AGENT_INFLIGHT", 8);
|
|
421
421
|
}
|
|
422
422
|
function bump(kind, key, windowSec, extra) {
|
|
423
|
-
const now =
|
|
423
|
+
const now = performance.now() / 1e3;
|
|
424
424
|
const k = `${kind}|${key}`;
|
|
425
425
|
let b = counters.get(k);
|
|
426
426
|
if (!b || now - b.start >= windowSec) {
|
|
@@ -487,7 +487,7 @@ function mossTokenPath() {
|
|
|
487
487
|
return (0, import_node_path.join)(home, ".config", "agentmoss", "auth_token");
|
|
488
488
|
}
|
|
489
489
|
function resolveMossToken(force = false) {
|
|
490
|
-
const now =
|
|
490
|
+
const now = performance.now() / 1e3;
|
|
491
491
|
if (!force && mossTokenCache.expires > now) return mossTokenCache.value;
|
|
492
492
|
let tok = env("AGENT_GATE_MOSS_TOKEN");
|
|
493
493
|
if (!tok) tok = env("AGENT_MOSS_AUTH_TOKEN");
|
|
@@ -572,7 +572,7 @@ var init_security = __esm({
|
|
|
572
572
|
return this.qps > 0;
|
|
573
573
|
}
|
|
574
574
|
allow(key) {
|
|
575
|
-
const now =
|
|
575
|
+
const now = performance.now() / 1e3;
|
|
576
576
|
const cur = this.buckets.get(key) ?? { tokens: this.burst, last: now };
|
|
577
577
|
const tokens = Math.min(this.burst, cur.tokens + (now - cur.last) * this.qps);
|
|
578
578
|
if (tokens < 1) {
|
|
@@ -1006,15 +1006,15 @@ var init_json_storage = __esm({
|
|
|
1006
1006
|
const archiveCount = total - keep;
|
|
1007
1007
|
const toArchive = flat.slice(0, archiveCount);
|
|
1008
1008
|
const archiveIds = new Set(toArchive.map((x) => String(x.rec.id)));
|
|
1009
|
-
const { mkdirSync:
|
|
1010
|
-
const { join:
|
|
1009
|
+
const { mkdirSync: mkdirSync5, writeFileSync: writeFileSync4 } = require("fs");
|
|
1010
|
+
const { join: join5 } = require("path");
|
|
1011
1011
|
const { gzipSync } = require("zlib");
|
|
1012
|
-
const archiveDir =
|
|
1013
|
-
if (!(0, import_fs2.existsSync)(archiveDir))
|
|
1012
|
+
const archiveDir = join5(this.dataDir, "archive");
|
|
1013
|
+
if (!(0, import_fs2.existsSync)(archiveDir)) mkdirSync5(archiveDir, { recursive: true, mode: 448 });
|
|
1014
1014
|
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
1015
|
-
const file =
|
|
1015
|
+
const file = join5(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
|
|
1016
1016
|
const payload = toArchive.map((x) => JSON.stringify(x.rec)).join("\n") + "\n";
|
|
1017
|
-
|
|
1017
|
+
writeFileSync4(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
|
|
1018
1018
|
for (const [sid, recs] of bySession) {
|
|
1019
1019
|
const remain = recs.filter((r) => !archiveIds.has(String(r.id)));
|
|
1020
1020
|
if (remain.length !== recs.length) this.saveToolCalls(sid, remain);
|
|
@@ -1335,11 +1335,11 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1335
1335
|
const row = this.db.prepare("SELECT COUNT(*) as total FROM tool_calls").get();
|
|
1336
1336
|
let dbBytes = 0;
|
|
1337
1337
|
try {
|
|
1338
|
-
const { statSync: statSync3, existsSync:
|
|
1338
|
+
const { statSync: statSync3, existsSync: existsSync4 } = require("fs");
|
|
1339
1339
|
dbBytes = statSync3(this.dbPath).size;
|
|
1340
1340
|
for (const suffix of ["-wal", "-shm"]) {
|
|
1341
1341
|
const p = this.dbPath + suffix;
|
|
1342
|
-
if (
|
|
1342
|
+
if (existsSync4(p)) dbBytes += statSync3(p).size;
|
|
1343
1343
|
}
|
|
1344
1344
|
} catch {
|
|
1345
1345
|
}
|
|
@@ -1372,15 +1372,15 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1372
1372
|
"SELECT * FROM tool_calls WHERE id <= ? ORDER BY id"
|
|
1373
1373
|
).all(cutoff.id);
|
|
1374
1374
|
if (rows.length === 0) return { archived: 0, file: "" };
|
|
1375
|
-
const { writeFileSync:
|
|
1376
|
-
const { dirname:
|
|
1375
|
+
const { writeFileSync: writeFileSync4, mkdirSync: mkdirSync5, existsSync: existsSync4 } = require("fs");
|
|
1376
|
+
const { dirname: dirname5, join: join5 } = require("path");
|
|
1377
1377
|
const { gzipSync } = require("zlib");
|
|
1378
|
-
const archiveDir =
|
|
1379
|
-
if (!
|
|
1378
|
+
const archiveDir = join5(dirname5(this.dbPath), "archive");
|
|
1379
|
+
if (!existsSync4(archiveDir)) mkdirSync5(archiveDir, { recursive: true, mode: 448 });
|
|
1380
1380
|
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
1381
|
-
const file =
|
|
1381
|
+
const file = join5(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
|
|
1382
1382
|
const payload = rows.map((r) => JSON.stringify(r)).join("\n") + "\n";
|
|
1383
|
-
|
|
1383
|
+
writeFileSync4(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
|
|
1384
1384
|
const deleted = this.db.transaction(() => {
|
|
1385
1385
|
const res = this.db.prepare("DELETE FROM tool_calls WHERE id <= ?").run(cutoff.id);
|
|
1386
1386
|
this.db.prepare(`
|
|
@@ -1486,14 +1486,14 @@ function getDb(dbPath) {
|
|
|
1486
1486
|
if (rawDb) return rawDb;
|
|
1487
1487
|
try {
|
|
1488
1488
|
const Database = require("better-sqlite3");
|
|
1489
|
-
const { existsSync:
|
|
1490
|
-
const { join:
|
|
1489
|
+
const { existsSync: existsSync4, mkdirSync: mkdirSync5 } = require("fs");
|
|
1490
|
+
const { join: join5, dirname: dirname5 } = require("path");
|
|
1491
1491
|
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
1492
|
-
const xdgData = process.env.XDG_DATA_HOME ||
|
|
1493
|
-
const defaultPath =
|
|
1492
|
+
const xdgData = process.env.XDG_DATA_HOME || join5(home, ".local", "share");
|
|
1493
|
+
const defaultPath = join5(xdgData, "agent_gate", "agent_gate_ts.db");
|
|
1494
1494
|
const path = dbPath || defaultPath;
|
|
1495
|
-
const dir =
|
|
1496
|
-
if (!
|
|
1495
|
+
const dir = dirname5(path);
|
|
1496
|
+
if (!existsSync4(dir)) mkdirSync5(dir, { recursive: true });
|
|
1497
1497
|
rawDb = new Database(path);
|
|
1498
1498
|
rawDb.pragma("journal_mode = WAL");
|
|
1499
1499
|
return rawDb;
|
|
@@ -1536,6 +1536,46 @@ var init_db = __esm({
|
|
|
1536
1536
|
}
|
|
1537
1537
|
});
|
|
1538
1538
|
|
|
1539
|
+
// src/shared/instance-id.ts
|
|
1540
|
+
function getInstanceId() {
|
|
1541
|
+
if (cached) return cached;
|
|
1542
|
+
const env2 = (process.env.AGENT_GATE_INSTANCE_ID || "").trim();
|
|
1543
|
+
if (env2) {
|
|
1544
|
+
cached = env2;
|
|
1545
|
+
return cached;
|
|
1546
|
+
}
|
|
1547
|
+
const filePath = process.env.AGENT_GATE_INSTANCE_ID_PATH || (0, import_path4.join)((0, import_os.homedir)(), ".config", "agentgate", "instance_id");
|
|
1548
|
+
try {
|
|
1549
|
+
if ((0, import_fs4.existsSync)(filePath)) {
|
|
1550
|
+
const v2 = (0, import_fs4.readFileSync)(filePath, "utf-8").trim();
|
|
1551
|
+
if (v2) {
|
|
1552
|
+
cached = v2;
|
|
1553
|
+
return cached;
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
const v = (0, import_crypto2.randomUUID)();
|
|
1557
|
+
(0, import_fs4.mkdirSync)((0, import_path4.dirname)(filePath), { recursive: true });
|
|
1558
|
+
(0, import_fs4.writeFileSync)(filePath, v + "\n", { mode: 384 });
|
|
1559
|
+
cached = v;
|
|
1560
|
+
return cached;
|
|
1561
|
+
} catch {
|
|
1562
|
+
cached = `ephemeral-${(0, import_crypto2.randomUUID)()}`;
|
|
1563
|
+
return cached;
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
var import_fs4, import_os, import_path4, import_crypto2, cached;
|
|
1567
|
+
var init_instance_id = __esm({
|
|
1568
|
+
"src/shared/instance-id.ts"() {
|
|
1569
|
+
"use strict";
|
|
1570
|
+
init_cjs_shims();
|
|
1571
|
+
import_fs4 = require("fs");
|
|
1572
|
+
import_os = require("os");
|
|
1573
|
+
import_path4 = require("path");
|
|
1574
|
+
import_crypto2 = require("crypto");
|
|
1575
|
+
cached = "";
|
|
1576
|
+
}
|
|
1577
|
+
});
|
|
1578
|
+
|
|
1539
1579
|
// src/shared/multi-session.ts
|
|
1540
1580
|
function isMultiSessionEnabled() {
|
|
1541
1581
|
const raw = (process.env.AGENT_GATE_MULTI_SESSION ?? "").trim().toLowerCase();
|
|
@@ -1582,6 +1622,9 @@ function buildSessionHistory(queryToolCalls2, sessionId, listAgentTypes) {
|
|
|
1582
1622
|
return [...bySession.entries()].slice(0, maxSessions).map(([sid, actions]) => ({
|
|
1583
1623
|
session_id: sid,
|
|
1584
1624
|
...sessionAgent.get(sid) ? { agent_id: sessionAgent.get(sid) } : {},
|
|
1625
|
+
// AgentGate 自己的存储属于本安装实例;带上实例 ID 让 AgentMoss 在
|
|
1626
|
+
// 展示层按「同实例不隐藏」处理(否则用户自己的历史会话也会被脱敏)。
|
|
1627
|
+
instance_id: getInstanceId(),
|
|
1585
1628
|
action_history: actions.slice(0, DEFAULT_ACTIONS_PER_SESSION)
|
|
1586
1629
|
}));
|
|
1587
1630
|
} catch {
|
|
@@ -1596,6 +1639,7 @@ var init_multi_session = __esm({
|
|
|
1596
1639
|
"src/shared/multi-session.ts"() {
|
|
1597
1640
|
"use strict";
|
|
1598
1641
|
init_cjs_shims();
|
|
1642
|
+
init_instance_id();
|
|
1599
1643
|
DEFAULT_HISTORY_LIMIT = 500;
|
|
1600
1644
|
DEFAULT_MAX_SESSIONS = 5;
|
|
1601
1645
|
DEFAULT_ACTIONS_PER_SESSION = 100;
|
|
@@ -1723,7 +1767,7 @@ var init_version = __esm({
|
|
|
1723
1767
|
"src/version.ts"() {
|
|
1724
1768
|
"use strict";
|
|
1725
1769
|
init_cjs_shims();
|
|
1726
|
-
_version = true ? "0.9.
|
|
1770
|
+
_version = true ? "0.9.4" : (() => {
|
|
1727
1771
|
try {
|
|
1728
1772
|
const require2 = createRequire(importMetaUrl);
|
|
1729
1773
|
const pkg = require2("../package.json");
|
|
@@ -1748,6 +1792,8 @@ var init_models = __esm({
|
|
|
1748
1792
|
agent_type: import_zod.z.string().describe("Agent identifier, e.g. opendesk, claude-code"),
|
|
1749
1793
|
agent_version: import_zod.z.string().default("0.1.0"),
|
|
1750
1794
|
session_id: import_zod.z.string().describe("Session unique identifier"),
|
|
1795
|
+
/** 安装级实例 ID(仅展示层脱敏用;缺省由 AgentGate 自动生成)。 */
|
|
1796
|
+
instance_id: import_zod.z.string().optional(),
|
|
1751
1797
|
tool_name: import_zod.z.string().optional().describe("Tool name, e.g. bash, file_write"),
|
|
1752
1798
|
command: import_zod.z.string().optional().describe("Tool command or description"),
|
|
1753
1799
|
a_next: import_zod.z.object({
|
|
@@ -1864,6 +1910,8 @@ function normalize(input, systemContext) {
|
|
|
1864
1910
|
// 历史上只在 metadata.agent_type 里带,AgentMoss 读顶层 req.agent_id → 空 →
|
|
1865
1911
|
// 定制规则从未生效(OpenDesk memory-* 操作走 L3 耗时 1~137s)。
|
|
1866
1912
|
agent_id: input.agent_type || "",
|
|
1913
|
+
// 实例 ID:仅用于 AgentMoss 展示层脱敏(同实例不隐藏),不参与判定。
|
|
1914
|
+
instance_id: input.instance_id || getInstanceId(),
|
|
1867
1915
|
metadata: {
|
|
1868
1916
|
...input.metadata,
|
|
1869
1917
|
agent_type: input.agent_type,
|
|
@@ -1882,6 +1930,7 @@ var init_normalizer = __esm({
|
|
|
1882
1930
|
"use strict";
|
|
1883
1931
|
init_cjs_shims();
|
|
1884
1932
|
init_multi_session();
|
|
1933
|
+
init_instance_id();
|
|
1885
1934
|
}
|
|
1886
1935
|
});
|
|
1887
1936
|
|
|
@@ -1890,22 +1939,22 @@ async function collectSystemInfo() {
|
|
|
1890
1939
|
try {
|
|
1891
1940
|
return {
|
|
1892
1941
|
system: process.platform,
|
|
1893
|
-
distribution: `${
|
|
1894
|
-
hostname:
|
|
1895
|
-
arch:
|
|
1896
|
-
cpu_count:
|
|
1897
|
-
memory_total_mb: Math.round(
|
|
1942
|
+
distribution: `${import_os2.default.type()} ${import_os2.default.release()}`,
|
|
1943
|
+
hostname: import_os2.default.hostname(),
|
|
1944
|
+
arch: import_os2.default.arch(),
|
|
1945
|
+
cpu_count: import_os2.default.cpus().length,
|
|
1946
|
+
memory_total_mb: Math.round(import_os2.default.totalmem() / (1024 * 1024))
|
|
1898
1947
|
};
|
|
1899
1948
|
} catch {
|
|
1900
1949
|
return { system: "unknown" };
|
|
1901
1950
|
}
|
|
1902
1951
|
}
|
|
1903
|
-
var
|
|
1952
|
+
var import_os2;
|
|
1904
1953
|
var init_system_info = __esm({
|
|
1905
1954
|
"src/shared/collector/system-info.ts"() {
|
|
1906
1955
|
"use strict";
|
|
1907
1956
|
init_cjs_shims();
|
|
1908
|
-
|
|
1957
|
+
import_os2 = __toESM(require("os"));
|
|
1909
1958
|
}
|
|
1910
1959
|
});
|
|
1911
1960
|
|
|
@@ -1932,7 +1981,7 @@ var init_process_info = __esm({
|
|
|
1932
1981
|
// src/shared/collector/user-info.ts
|
|
1933
1982
|
async function collectUserInfo() {
|
|
1934
1983
|
try {
|
|
1935
|
-
const userInfo =
|
|
1984
|
+
const userInfo = import_os3.default.userInfo();
|
|
1936
1985
|
return {
|
|
1937
1986
|
uid: userInfo.uid,
|
|
1938
1987
|
gid: userInfo.gid,
|
|
@@ -1943,12 +1992,12 @@ async function collectUserInfo() {
|
|
|
1943
1992
|
return { uid: -1 };
|
|
1944
1993
|
}
|
|
1945
1994
|
}
|
|
1946
|
-
var
|
|
1995
|
+
var import_os3;
|
|
1947
1996
|
var init_user_info = __esm({
|
|
1948
1997
|
"src/shared/collector/user-info.ts"() {
|
|
1949
1998
|
"use strict";
|
|
1950
1999
|
init_cjs_shims();
|
|
1951
|
-
|
|
2000
|
+
import_os3 = __toESM(require("os"));
|
|
1952
2001
|
}
|
|
1953
2002
|
});
|
|
1954
2003
|
|
|
@@ -2101,10 +2150,10 @@ async function analyzeRoute(c) {
|
|
|
2101
2150
|
const toolName = input.tool_name || input.a_next?.action_type || "unknown";
|
|
2102
2151
|
const command = input.command || input.a_next?.action_detail || "";
|
|
2103
2152
|
const key = cacheKey(input.session_id, input.cwd, command);
|
|
2104
|
-
const
|
|
2105
|
-
if (
|
|
2153
|
+
const cached2 = getCached(key);
|
|
2154
|
+
if (cached2) {
|
|
2106
2155
|
return c.json({
|
|
2107
|
-
...
|
|
2156
|
+
...cached2,
|
|
2108
2157
|
analysis_duration_ms: 0,
|
|
2109
2158
|
collected_context: {},
|
|
2110
2159
|
...storageOversizeInfo()
|
|
@@ -2406,17 +2455,17 @@ async function dashboardRoute(c) {
|
|
|
2406
2455
|
return c.html(UNAUTHORIZED_HTML, 401, DASHBOARD_SECURITY_HEADERS);
|
|
2407
2456
|
}
|
|
2408
2457
|
}
|
|
2409
|
-
const { readFileSync:
|
|
2410
|
-
const { resolve, dirname:
|
|
2458
|
+
const { readFileSync: readFileSync4, existsSync: existsSync4 } = await import("fs");
|
|
2459
|
+
const { resolve, dirname: dirname5 } = await import("path");
|
|
2411
2460
|
const { fileURLToPath } = await import("url");
|
|
2412
|
-
const __dirname =
|
|
2461
|
+
const __dirname = dirname5(fileURLToPath(importMetaUrl));
|
|
2413
2462
|
const paths = [
|
|
2414
2463
|
resolve(__dirname, "dashboard.html"),
|
|
2415
2464
|
resolve(__dirname, "../dashboard.html"),
|
|
2416
2465
|
resolve(__dirname, "../src/v1/dashboard.html")
|
|
2417
2466
|
];
|
|
2418
|
-
const htmlPath = paths.find((p) =>
|
|
2419
|
-
const html =
|
|
2467
|
+
const htmlPath = paths.find((p) => existsSync4(p)) || paths[0];
|
|
2468
|
+
const html = readFileSync4(htmlPath, "utf-8");
|
|
2420
2469
|
return c.html(html, 200, DASHBOARD_SECURITY_HEADERS);
|
|
2421
2470
|
}
|
|
2422
2471
|
var import_hono, import_cors, import_logger, import_cookie, PUBLIC_PAGE_PATHS, UNAUTHORIZED_HTML, AGENT_FORBIDDEN_HTML;
|