@openacp/cli 0.6.10 → 2026.326.2
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/{action-detect-P7ZE4NEM.js → action-detect-QPA775HB.js} +2 -2
- package/dist/adapter-6ANPBSVU.js +16 -0
- package/dist/{discord-OMC52Y54.js → adapter-77ZCVABT.js} +520 -365
- package/dist/adapter-77ZCVABT.js.map +1 -0
- package/dist/{adapter-ZOANORGM.js → adapter-PQGHVG4K.js} +300 -93
- package/dist/adapter-PQGHVG4K.js.map +1 -0
- package/dist/{admin-6SYB6XCZ.js → admin-GBPZFFAU.js} +3 -3
- package/dist/agent-catalog-YHBFERYO.js +11 -0
- package/dist/{agent-dependencies-4OWBMZWZ.js → agent-dependencies-WS7Z2DFW.js} +2 -2
- package/dist/agent-registry-5LZT7CUB.js +9 -0
- package/dist/agent-store-VSHNY5GT.js +9 -0
- package/dist/{agents-QO7DKARJ.js → agents-BWU4MRRD.js} +3 -3
- package/dist/{api-client-CFQT5U7D.js → api-client-AQPNKXI2.js} +2 -2
- package/dist/api-server-3PYLRBCN.js +8 -0
- package/dist/api-server-CHVSUDBX.js +11 -0
- package/dist/{autostart-X33OGMX6.js → autostart-6JS565RY.js} +3 -3
- package/dist/chunk-2CX4IEEC.js +124 -0
- package/dist/chunk-2CX4IEEC.js.map +1 -0
- package/dist/{chunk-O7CPGUAI.js → chunk-4KGLKKQK.js} +4 -4
- package/dist/chunk-4KGLKKQK.js.map +1 -0
- package/dist/{chunk-W3EYKZNQ.js → chunk-4WXALZA3.js} +2 -2
- package/dist/chunk-4WXALZA3.js.map +1 -0
- package/dist/chunk-5OCGO27U.js +125 -0
- package/dist/chunk-5OCGO27U.js.map +1 -0
- package/dist/{chunk-OWP7RZ62.js → chunk-5ZOFBTOR.js} +118 -262
- package/dist/chunk-5ZOFBTOR.js.map +1 -0
- package/dist/chunk-6RXVEXF3.js +23 -0
- package/dist/chunk-6RXVEXF3.js.map +1 -0
- package/dist/{chunk-34M4OS5P.js → chunk-A6Y4GZM3.js} +3 -3
- package/dist/chunk-A6Y4GZM3.js.map +1 -0
- package/dist/chunk-AD3X6DGK.js +166 -0
- package/dist/chunk-AD3X6DGK.js.map +1 -0
- package/dist/{chunk-7QJS2XBD.js → chunk-AFKX424Q.js} +2 -2
- package/dist/chunk-AFKX424Q.js.map +1 -0
- package/dist/chunk-APS6UEFU.js +259 -0
- package/dist/chunk-APS6UEFU.js.map +1 -0
- package/dist/chunk-BLQUXO7S.js +113 -0
- package/dist/chunk-BLQUXO7S.js.map +1 -0
- package/dist/{chunk-WTZDAYZX.js → chunk-BQ6FR32N.js} +3 -3
- package/dist/chunk-BQ6FR32N.js.map +1 -0
- package/dist/chunk-FNRSWA2K.js +1 -0
- package/dist/chunk-FQEBWOZR.js +3557 -0
- package/dist/chunk-FQEBWOZR.js.map +1 -0
- package/dist/{chunk-4CTX774K.js → chunk-GJOY37U7.js} +4 -4
- package/dist/chunk-GJOY37U7.js.map +1 -0
- package/dist/{chunk-I7WC6E5S.js → chunk-HVBNCPAY.js} +2 -2
- package/dist/chunk-HVBNCPAY.js.map +1 -0
- package/dist/{chunk-2HMQOC7N.js → chunk-I3CGU5W7.js} +4 -4
- package/dist/chunk-I3CGU5W7.js.map +1 -0
- package/dist/{chunk-NVPG6JCL.js → chunk-L7YNNBI5.js} +3 -3
- package/dist/chunk-L7YNNBI5.js.map +1 -0
- package/dist/chunk-LGFWH3AE.js +26 -0
- package/dist/chunk-LGFWH3AE.js.map +1 -0
- package/dist/chunk-MLF4W5R6.js +101 -0
- package/dist/chunk-MLF4W5R6.js.map +1 -0
- package/dist/{chunk-KIRH7TUJ.js → chunk-MTSDOSXS.js} +3 -3
- package/dist/chunk-MTSDOSXS.js.map +1 -0
- package/dist/{chunk-J4SJTKIK.js → chunk-NAM4ERUW.js} +3 -3
- package/dist/chunk-NAM4ERUW.js.map +1 -0
- package/dist/{chunk-MKHUZLII.js → chunk-NBFIBGAT.js} +39 -25
- package/dist/chunk-NBFIBGAT.js.map +1 -0
- package/dist/{chunk-BNLGTZ34.js → chunk-O5RG4YZY.js} +3 -3
- package/dist/chunk-O5RG4YZY.js.map +1 -0
- package/dist/{chunk-JHYXKVV2.js → chunk-ODUM3D6X.js} +2 -2
- package/dist/chunk-ODUM3D6X.js.map +1 -0
- package/dist/chunk-OYSAN7UX.js +15 -0
- package/dist/chunk-OYSAN7UX.js.map +1 -0
- package/dist/chunk-P4SNGQNI.js +158 -0
- package/dist/chunk-P4SNGQNI.js.map +1 -0
- package/dist/{chunk-2CJ46J3C.js → chunk-PPSMUECX.js} +3 -3
- package/dist/chunk-PPSMUECX.js.map +1 -0
- package/dist/chunk-Q6ZXJTZB.js +56 -0
- package/dist/chunk-Q6ZXJTZB.js.map +1 -0
- package/dist/{chunk-XANPHG7W.js → chunk-QSDZDHNS.js} +7 -7
- package/dist/chunk-QSDZDHNS.js.map +1 -0
- package/dist/{chunk-33RP6K2O.js → chunk-QVMEF6FB.js} +6 -6
- package/dist/chunk-QVMEF6FB.js.map +1 -0
- package/dist/chunk-QWP76EBW.js +536 -0
- package/dist/chunk-QWP76EBW.js.map +1 -0
- package/dist/{chunk-V5GZQEIY.js → chunk-RBYBSSGO.js} +4 -4
- package/dist/chunk-RBYBSSGO.js.map +1 -0
- package/dist/{chunk-CS3KCJ5D.js → chunk-RKB2ZK6S.js} +555 -383
- package/dist/chunk-RKB2ZK6S.js.map +1 -0
- package/dist/{chunk-UKT3G5IA.js → chunk-SHTGQGAU.js} +7 -7
- package/dist/chunk-SHTGQGAU.js.map +1 -0
- package/dist/chunk-SNPYTMPR.js +51 -0
- package/dist/chunk-SNPYTMPR.js.map +1 -0
- package/dist/chunk-UB2QB6DE.js +124 -0
- package/dist/chunk-UB2QB6DE.js.map +1 -0
- package/dist/chunk-UNJUWWQO.js +1108 -0
- package/dist/chunk-UNJUWWQO.js.map +1 -0
- package/dist/chunk-V2M243KZ.js +445 -0
- package/dist/chunk-V2M243KZ.js.map +1 -0
- package/dist/chunk-V5JT5TPD.js +97 -0
- package/dist/chunk-V5JT5TPD.js.map +1 -0
- package/dist/chunk-W26AUH5B.js +61 -0
- package/dist/chunk-W26AUH5B.js.map +1 -0
- package/dist/chunk-WAAD23KY.js +222 -0
- package/dist/chunk-WAAD23KY.js.map +1 -0
- package/dist/chunk-WIIZNPCR.js +150 -0
- package/dist/chunk-WIIZNPCR.js.map +1 -0
- package/dist/chunk-WQCJTU2C.js +84 -0
- package/dist/chunk-WQCJTU2C.js.map +1 -0
- package/dist/chunk-WVLDNYOJ.js +150 -0
- package/dist/chunk-WVLDNYOJ.js.map +1 -0
- package/dist/chunk-WXVT3AOY.js +22 -0
- package/dist/chunk-WXVT3AOY.js.map +1 -0
- package/dist/{chunk-GAK6PIBW.js → chunk-XMMAGAT4.js} +2 -2
- package/dist/chunk-XMMAGAT4.js.map +1 -0
- package/dist/chunk-Y64XWMJ4.js +212 -0
- package/dist/chunk-Y64XWMJ4.js.map +1 -0
- package/dist/chunk-YEULD3SG.js +62 -0
- package/dist/chunk-YEULD3SG.js.map +1 -0
- package/dist/chunk-ZHGPZBS4.js +49 -0
- package/dist/chunk-ZHGPZBS4.js.map +1 -0
- package/dist/{chunk-JKBFUAJK.js → chunk-ZSLHHQPQ.js} +2 -2
- package/dist/chunk-ZSLHHQPQ.js.map +1 -0
- package/dist/cli.js +496 -150
- package/dist/cli.js.map +1 -1
- package/dist/{config-6S355X75.js → config-I4FMCJGZ.js} +3 -3
- package/dist/config-editor-HNEKXRLQ.js +11 -0
- package/dist/{config-registry-AHYI4MYL.js → config-registry-CUMNXFGK.js} +2 -2
- package/dist/context-XM6E22LM.js +10 -0
- package/dist/core-plugins-VEUNFTMB.js +27 -0
- package/dist/{daemon-4CS6HMB5.js → daemon-PXO5QPCR.js} +4 -4
- package/dist/dev-loader-RDC5E2CW.js +50 -0
- package/dist/dev-loader-RDC5E2CW.js.map +1 -0
- package/dist/discord-NOJQ5PZO.js +8 -0
- package/dist/doctor-H72BZOPA.js +10 -0
- package/dist/{doctor-OLYBO3V3.js → doctor-RF6BHMCC.js} +5 -5
- package/dist/file-service-EUODJAIT.js +9 -0
- package/dist/file-service-EUODJAIT.js.map +1 -0
- package/dist/index.d.ts +1293 -188
- package/dist/index.js +387 -48
- package/dist/index.js.map +1 -1
- package/dist/{install-cloudflared-Z7VCGOVG.js → install-cloudflared-AN24L4DP.js} +5 -5
- package/dist/install-cloudflared-AN24L4DP.js.map +1 -0
- package/dist/install-context-XPWTFT3J.js +78 -0
- package/dist/install-context-XPWTFT3J.js.map +1 -0
- package/dist/{install-jq-HUYSQWKR.js → install-jq-CRVDJGF3.js} +5 -5
- package/dist/install-jq-CRVDJGF3.js.map +1 -0
- package/dist/{integrate-PNEHRY2I.js → integrate-5C6KSU6D.js} +2 -2
- package/dist/integrate-5C6KSU6D.js.map +1 -0
- package/dist/{log-NXABYJTT.js → log-LZ7FTRKG.js} +2 -2
- package/dist/log-LZ7FTRKG.js.map +1 -0
- package/dist/main-T5WVCCFN.js +715 -0
- package/dist/main-T5WVCCFN.js.map +1 -0
- package/dist/{menu-YY5MKHEK.js → menu-YDQ2LWAR.js} +2 -2
- package/dist/menu-YDQ2LWAR.js.map +1 -0
- package/dist/{new-session-FEO4J4VU.js → new-session-AVQCNXRG.js} +5 -5
- package/dist/new-session-AVQCNXRG.js.map +1 -0
- package/dist/notifications-D5BRDNSU.js +9 -0
- package/dist/notifications-D5BRDNSU.js.map +1 -0
- package/dist/plugin-create-JVCVUG6V.js +331 -0
- package/dist/plugin-create-JVCVUG6V.js.map +1 -0
- package/dist/plugin-registry-WB3DR67H.js +8 -0
- package/dist/plugin-registry-WB3DR67H.js.map +1 -0
- package/dist/{post-upgrade-CJG5I7M2.js → post-upgrade-XLHZ6ZB7.js} +8 -8
- package/dist/post-upgrade-XLHZ6ZB7.js.map +1 -0
- package/dist/read-text-file-IRZM3QLM.js +8 -0
- package/dist/read-text-file-IRZM3QLM.js.map +1 -0
- package/dist/security-YNRBW6S7.js +9 -0
- package/dist/security-YNRBW6S7.js.map +1 -0
- package/dist/{session-IUSI7P5S.js → session-KZFA6Z26.js} +4 -4
- package/dist/session-KZFA6Z26.js.map +1 -0
- package/dist/{settings-RQPAM4KC.js → settings-MFYM7CZO.js} +4 -4
- package/dist/settings-MFYM7CZO.js.map +1 -0
- package/dist/settings-manager-MD2U4ZV2.js +8 -0
- package/dist/settings-manager-MD2U4ZV2.js.map +1 -0
- package/dist/{chunk-LCRLAV4G.js → setup-BAI2F24H.js} +154 -492
- package/dist/setup-BAI2F24H.js.map +1 -0
- package/dist/slack-KH7E3VBS.js +8 -0
- package/dist/slack-KH7E3VBS.js.map +1 -0
- package/dist/speech-2GHQNRIO.js +9 -0
- package/dist/speech-2GHQNRIO.js.map +1 -0
- package/dist/telegram-ZDC3JQF2.js +8 -0
- package/dist/telegram-ZDC3JQF2.js.map +1 -0
- package/dist/tunnel-M47I7H4B.js +8 -0
- package/dist/tunnel-M47I7H4B.js.map +1 -0
- package/dist/{tunnel-service-CJLUH6SZ.js → tunnel-service-WADYHREX.js} +17 -17
- package/dist/tunnel-service-WADYHREX.js.map +1 -0
- package/dist/usage-WYNK6ZC5.js +10 -0
- package/dist/usage-WYNK6ZC5.js.map +1 -0
- package/dist/validators-6CLEZUBD.js +8 -0
- package/dist/validators-6CLEZUBD.js.map +1 -0
- package/dist/validators-WSTBNKRW.js +12 -0
- package/dist/validators-WSTBNKRW.js.map +1 -0
- package/package.json +1 -1
- package/dist/adapter-ZOANORGM.js.map +0 -1
- package/dist/agent-catalog-FC3HGDEQ.js +0 -11
- package/dist/agent-registry-WT4NXPYG.js +0 -9
- package/dist/agent-store-VZLFPTZU.js +0 -9
- package/dist/chunk-2CJ46J3C.js.map +0 -1
- package/dist/chunk-2HMQOC7N.js.map +0 -1
- package/dist/chunk-33RP6K2O.js.map +0 -1
- package/dist/chunk-34M4OS5P.js.map +0 -1
- package/dist/chunk-4CTX774K.js.map +0 -1
- package/dist/chunk-7QJS2XBD.js.map +0 -1
- package/dist/chunk-BNLGTZ34.js.map +0 -1
- package/dist/chunk-CS3KCJ5D.js.map +0 -1
- package/dist/chunk-GAK6PIBW.js.map +0 -1
- package/dist/chunk-I7WC6E5S.js.map +0 -1
- package/dist/chunk-J4SJTKIK.js.map +0 -1
- package/dist/chunk-JHYXKVV2.js.map +0 -1
- package/dist/chunk-JKBFUAJK.js.map +0 -1
- package/dist/chunk-KIRH7TUJ.js.map +0 -1
- package/dist/chunk-LBIKITQT.js +0 -22
- package/dist/chunk-LBIKITQT.js.map +0 -1
- package/dist/chunk-LCRLAV4G.js.map +0 -1
- package/dist/chunk-LGP2YGRL.js +0 -4880
- package/dist/chunk-LGP2YGRL.js.map +0 -1
- package/dist/chunk-MKHUZLII.js.map +0 -1
- package/dist/chunk-NAMYZIS5.js +0 -1
- package/dist/chunk-NVPG6JCL.js.map +0 -1
- package/dist/chunk-O7CPGUAI.js.map +0 -1
- package/dist/chunk-OWP7RZ62.js.map +0 -1
- package/dist/chunk-UKT3G5IA.js.map +0 -1
- package/dist/chunk-V5GZQEIY.js.map +0 -1
- package/dist/chunk-VOIJ6OY4.js +0 -63
- package/dist/chunk-VOIJ6OY4.js.map +0 -1
- package/dist/chunk-W3EYKZNQ.js.map +0 -1
- package/dist/chunk-WTZDAYZX.js.map +0 -1
- package/dist/chunk-XANPHG7W.js.map +0 -1
- package/dist/config-editor-QQTZMWGD.js +0 -13
- package/dist/discord-OMC52Y54.js.map +0 -1
- package/dist/doctor-HZZ5BSHB.js +0 -10
- package/dist/install-cloudflared-Z7VCGOVG.js.map +0 -1
- package/dist/install-jq-HUYSQWKR.js.map +0 -1
- package/dist/integrate-PNEHRY2I.js.map +0 -1
- package/dist/main-XOZCLFUK.js +0 -238
- package/dist/main-XOZCLFUK.js.map +0 -1
- package/dist/post-upgrade-CJG5I7M2.js.map +0 -1
- package/dist/setup-XHS4OMPM.js +0 -37
- package/dist/tunnel-service-CJLUH6SZ.js.map +0 -1
- /package/dist/{action-detect-P7ZE4NEM.js.map → action-detect-QPA775HB.js.map} +0 -0
- /package/dist/{admin-6SYB6XCZ.js.map → adapter-6ANPBSVU.js.map} +0 -0
- /package/dist/{agent-catalog-FC3HGDEQ.js.map → admin-GBPZFFAU.js.map} +0 -0
- /package/dist/{agent-dependencies-4OWBMZWZ.js.map → agent-catalog-YHBFERYO.js.map} +0 -0
- /package/dist/{agent-registry-WT4NXPYG.js.map → agent-dependencies-WS7Z2DFW.js.map} +0 -0
- /package/dist/{agent-store-VZLFPTZU.js.map → agent-registry-5LZT7CUB.js.map} +0 -0
- /package/dist/{agents-QO7DKARJ.js.map → agent-store-VSHNY5GT.js.map} +0 -0
- /package/dist/{api-client-CFQT5U7D.js.map → agents-BWU4MRRD.js.map} +0 -0
- /package/dist/{autostart-X33OGMX6.js.map → api-client-AQPNKXI2.js.map} +0 -0
- /package/dist/{chunk-NAMYZIS5.js.map → api-server-3PYLRBCN.js.map} +0 -0
- /package/dist/{config-6S355X75.js.map → api-server-CHVSUDBX.js.map} +0 -0
- /package/dist/{config-editor-QQTZMWGD.js.map → autostart-6JS565RY.js.map} +0 -0
- /package/dist/{config-registry-AHYI4MYL.js.map → chunk-FNRSWA2K.js.map} +0 -0
- /package/dist/{daemon-4CS6HMB5.js.map → config-I4FMCJGZ.js.map} +0 -0
- /package/dist/{doctor-HZZ5BSHB.js.map → config-editor-HNEKXRLQ.js.map} +0 -0
- /package/dist/{doctor-OLYBO3V3.js.map → config-registry-CUMNXFGK.js.map} +0 -0
- /package/dist/{log-NXABYJTT.js.map → context-XM6E22LM.js.map} +0 -0
- /package/dist/{menu-YY5MKHEK.js.map → core-plugins-VEUNFTMB.js.map} +0 -0
- /package/dist/{new-session-FEO4J4VU.js.map → daemon-PXO5QPCR.js.map} +0 -0
- /package/dist/{session-IUSI7P5S.js.map → discord-NOJQ5PZO.js.map} +0 -0
- /package/dist/{settings-RQPAM4KC.js.map → doctor-H72BZOPA.js.map} +0 -0
- /package/dist/{setup-XHS4OMPM.js.map → doctor-RF6BHMCC.js.map} +0 -0
|
@@ -0,0 +1,1108 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getAgentCapabilities
|
|
3
|
+
} from "./chunk-ZSLHHQPQ.js";
|
|
4
|
+
import {
|
|
5
|
+
createChildLogger
|
|
6
|
+
} from "./chunk-XMMAGAT4.js";
|
|
7
|
+
|
|
8
|
+
// src/plugins/api-server/api-server.ts
|
|
9
|
+
import * as http from "http";
|
|
10
|
+
import * as fs2 from "fs";
|
|
11
|
+
import * as path2 from "path";
|
|
12
|
+
import * as os from "os";
|
|
13
|
+
import * as crypto from "crypto";
|
|
14
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
15
|
+
|
|
16
|
+
// src/plugins/api-server/sse-manager.ts
|
|
17
|
+
var SSEManager = class {
|
|
18
|
+
constructor(eventBus, getSessionStats, startedAt) {
|
|
19
|
+
this.eventBus = eventBus;
|
|
20
|
+
this.getSessionStats = getSessionStats;
|
|
21
|
+
this.startedAt = startedAt;
|
|
22
|
+
}
|
|
23
|
+
sseConnections = /* @__PURE__ */ new Set();
|
|
24
|
+
sseCleanupHandlers = /* @__PURE__ */ new Map();
|
|
25
|
+
healthInterval;
|
|
26
|
+
boundHandlers = [];
|
|
27
|
+
setup() {
|
|
28
|
+
if (!this.eventBus) return;
|
|
29
|
+
const events = [
|
|
30
|
+
"session:created",
|
|
31
|
+
"session:updated",
|
|
32
|
+
"session:deleted",
|
|
33
|
+
"agent:event",
|
|
34
|
+
"permission:request"
|
|
35
|
+
];
|
|
36
|
+
for (const eventName of events) {
|
|
37
|
+
const handler = (data) => {
|
|
38
|
+
this.broadcast(eventName, data);
|
|
39
|
+
};
|
|
40
|
+
this.eventBus.on(eventName, handler);
|
|
41
|
+
this.boundHandlers.push({ event: eventName, handler });
|
|
42
|
+
}
|
|
43
|
+
this.healthInterval = setInterval(() => {
|
|
44
|
+
const mem = process.memoryUsage();
|
|
45
|
+
const stats = this.getSessionStats();
|
|
46
|
+
this.broadcast("health", {
|
|
47
|
+
uptime: Date.now() - this.startedAt,
|
|
48
|
+
memory: {
|
|
49
|
+
rss: mem.rss,
|
|
50
|
+
heapUsed: mem.heapUsed,
|
|
51
|
+
heapTotal: mem.heapTotal
|
|
52
|
+
},
|
|
53
|
+
sessions: stats
|
|
54
|
+
});
|
|
55
|
+
}, 3e4);
|
|
56
|
+
}
|
|
57
|
+
handleRequest(req, res) {
|
|
58
|
+
const parsedUrl = new URL(req.url || "", "http://localhost");
|
|
59
|
+
const sessionFilter = parsedUrl.searchParams.get("sessionId");
|
|
60
|
+
res.writeHead(200, {
|
|
61
|
+
"Content-Type": "text/event-stream",
|
|
62
|
+
"Cache-Control": "no-cache",
|
|
63
|
+
Connection: "keep-alive"
|
|
64
|
+
});
|
|
65
|
+
res.flushHeaders();
|
|
66
|
+
res.sessionFilter = sessionFilter ?? void 0;
|
|
67
|
+
this.sseConnections.add(res);
|
|
68
|
+
const cleanup = () => {
|
|
69
|
+
this.sseConnections.delete(res);
|
|
70
|
+
this.sseCleanupHandlers.delete(res);
|
|
71
|
+
};
|
|
72
|
+
this.sseCleanupHandlers.set(res, cleanup);
|
|
73
|
+
req.on("close", cleanup);
|
|
74
|
+
}
|
|
75
|
+
broadcast(event, data) {
|
|
76
|
+
const payload = `event: ${event}
|
|
77
|
+
data: ${JSON.stringify(data)}
|
|
78
|
+
|
|
79
|
+
`;
|
|
80
|
+
const sessionEvents = [
|
|
81
|
+
"agent:event",
|
|
82
|
+
"permission:request",
|
|
83
|
+
"session:updated"
|
|
84
|
+
];
|
|
85
|
+
for (const res of this.sseConnections) {
|
|
86
|
+
const filter = res.sessionFilter;
|
|
87
|
+
if (filter && sessionEvents.includes(event)) {
|
|
88
|
+
const eventData = data;
|
|
89
|
+
if (eventData.sessionId !== filter) continue;
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
if (res.writable) res.write(payload);
|
|
93
|
+
} catch {
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
stop() {
|
|
98
|
+
if (this.healthInterval) clearInterval(this.healthInterval);
|
|
99
|
+
if (this.eventBus) {
|
|
100
|
+
for (const { event, handler } of this.boundHandlers) {
|
|
101
|
+
this.eventBus.off(event, handler);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
this.boundHandlers = [];
|
|
105
|
+
const entries = [...this.sseCleanupHandlers];
|
|
106
|
+
for (const [res, cleanup] of entries) {
|
|
107
|
+
res.end();
|
|
108
|
+
cleanup();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// src/plugins/api-server/static-server.ts
|
|
114
|
+
import * as fs from "fs";
|
|
115
|
+
import * as path from "path";
|
|
116
|
+
import { fileURLToPath } from "url";
|
|
117
|
+
var MIME_TYPES = {
|
|
118
|
+
".html": "text/html; charset=utf-8",
|
|
119
|
+
".js": "application/javascript; charset=utf-8",
|
|
120
|
+
".css": "text/css; charset=utf-8",
|
|
121
|
+
".json": "application/json; charset=utf-8",
|
|
122
|
+
".png": "image/png",
|
|
123
|
+
".jpg": "image/jpeg",
|
|
124
|
+
".svg": "image/svg+xml",
|
|
125
|
+
".ico": "image/x-icon",
|
|
126
|
+
".woff": "font/woff",
|
|
127
|
+
".woff2": "font/woff2"
|
|
128
|
+
};
|
|
129
|
+
var StaticServer = class {
|
|
130
|
+
uiDir;
|
|
131
|
+
constructor(uiDir) {
|
|
132
|
+
this.uiDir = uiDir;
|
|
133
|
+
if (!this.uiDir) {
|
|
134
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
135
|
+
const candidate = path.resolve(path.dirname(__filename), "../../ui/dist");
|
|
136
|
+
if (fs.existsSync(path.join(candidate, "index.html"))) {
|
|
137
|
+
this.uiDir = candidate;
|
|
138
|
+
}
|
|
139
|
+
if (!this.uiDir) {
|
|
140
|
+
const publishCandidate = path.resolve(
|
|
141
|
+
path.dirname(__filename),
|
|
142
|
+
"../ui"
|
|
143
|
+
);
|
|
144
|
+
if (fs.existsSync(path.join(publishCandidate, "index.html"))) {
|
|
145
|
+
this.uiDir = publishCandidate;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
isAvailable() {
|
|
151
|
+
return this.uiDir !== void 0;
|
|
152
|
+
}
|
|
153
|
+
serve(req, res) {
|
|
154
|
+
if (!this.uiDir) return false;
|
|
155
|
+
const urlPath = (req.url || "/").split("?")[0];
|
|
156
|
+
const safePath = path.normalize(urlPath);
|
|
157
|
+
const filePath = path.join(this.uiDir, safePath);
|
|
158
|
+
if (!filePath.startsWith(this.uiDir + path.sep) && filePath !== this.uiDir)
|
|
159
|
+
return false;
|
|
160
|
+
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
|
|
161
|
+
const ext = path.extname(filePath);
|
|
162
|
+
const contentType = MIME_TYPES[ext] ?? "application/octet-stream";
|
|
163
|
+
const isHashed = /\.[a-zA-Z0-9]{8,}\.(js|css)$/.test(filePath);
|
|
164
|
+
const cacheControl = isHashed ? "public, max-age=31536000, immutable" : "no-cache";
|
|
165
|
+
res.writeHead(200, {
|
|
166
|
+
"Content-Type": contentType,
|
|
167
|
+
"Cache-Control": cacheControl
|
|
168
|
+
});
|
|
169
|
+
fs.createReadStream(filePath).pipe(res);
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
const indexPath = path.join(this.uiDir, "index.html");
|
|
173
|
+
if (fs.existsSync(indexPath)) {
|
|
174
|
+
res.writeHead(200, {
|
|
175
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
176
|
+
"Cache-Control": "no-cache"
|
|
177
|
+
});
|
|
178
|
+
fs.createReadStream(indexPath).pipe(res);
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// src/plugins/api-server/router.ts
|
|
186
|
+
var Router = class {
|
|
187
|
+
routes = [];
|
|
188
|
+
get(path3, handler) {
|
|
189
|
+
this.add("GET", path3, handler);
|
|
190
|
+
}
|
|
191
|
+
post(path3, handler) {
|
|
192
|
+
this.add("POST", path3, handler);
|
|
193
|
+
}
|
|
194
|
+
put(path3, handler) {
|
|
195
|
+
this.add("PUT", path3, handler);
|
|
196
|
+
}
|
|
197
|
+
patch(path3, handler) {
|
|
198
|
+
this.add("PATCH", path3, handler);
|
|
199
|
+
}
|
|
200
|
+
delete(path3, handler) {
|
|
201
|
+
this.add("DELETE", path3, handler);
|
|
202
|
+
}
|
|
203
|
+
match(method, url) {
|
|
204
|
+
const pathname = url.split("?")[0];
|
|
205
|
+
for (const route of this.routes) {
|
|
206
|
+
if (route.method !== method) continue;
|
|
207
|
+
const m = pathname.match(route.pattern);
|
|
208
|
+
if (!m) continue;
|
|
209
|
+
const params = {};
|
|
210
|
+
for (let i = 0; i < route.keys.length; i++) {
|
|
211
|
+
params[route.keys[i]] = m[i + 1];
|
|
212
|
+
}
|
|
213
|
+
return { handler: route.handler, params };
|
|
214
|
+
}
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
add(method, path3, handler) {
|
|
218
|
+
const keys = [];
|
|
219
|
+
const pattern = path3.replace(/:(\w+)/g, (_, key) => {
|
|
220
|
+
keys.push(key);
|
|
221
|
+
return "([^/]+)";
|
|
222
|
+
});
|
|
223
|
+
this.routes.push({
|
|
224
|
+
method,
|
|
225
|
+
pattern: new RegExp(`^${pattern}$`),
|
|
226
|
+
keys,
|
|
227
|
+
handler
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// src/plugins/api-server/routes/health.ts
|
|
233
|
+
function registerHealthRoutes(router, deps) {
|
|
234
|
+
router.get("/api/health", async (_req, res) => {
|
|
235
|
+
const activeSessions = deps.core.sessionManager.listSessions();
|
|
236
|
+
const allRecords = deps.core.sessionManager.listRecords();
|
|
237
|
+
const mem = process.memoryUsage();
|
|
238
|
+
const tunnel = deps.core.tunnelService;
|
|
239
|
+
deps.sendJson(res, 200, {
|
|
240
|
+
status: "ok",
|
|
241
|
+
uptime: Date.now() - deps.startedAt,
|
|
242
|
+
version: deps.getVersion(),
|
|
243
|
+
memory: {
|
|
244
|
+
rss: mem.rss,
|
|
245
|
+
heapUsed: mem.heapUsed,
|
|
246
|
+
heapTotal: mem.heapTotal
|
|
247
|
+
},
|
|
248
|
+
sessions: {
|
|
249
|
+
active: activeSessions.filter(
|
|
250
|
+
(s) => s.status === "active" || s.status === "initializing"
|
|
251
|
+
).length,
|
|
252
|
+
total: allRecords.length
|
|
253
|
+
},
|
|
254
|
+
adapters: Array.from(deps.core.adapters.keys()),
|
|
255
|
+
tunnel: tunnel ? { enabled: true, url: tunnel.getPublicUrl() } : { enabled: false }
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
router.get("/api/version", async (_req, res) => {
|
|
259
|
+
deps.sendJson(res, 200, { version: deps.getVersion() });
|
|
260
|
+
});
|
|
261
|
+
router.post("/api/restart", async (_req, res) => {
|
|
262
|
+
if (!deps.core.requestRestart) {
|
|
263
|
+
deps.sendJson(res, 501, { error: "Restart not available" });
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
deps.sendJson(res, 200, { ok: true, message: "Restarting..." });
|
|
267
|
+
setImmediate(() => deps.core.requestRestart());
|
|
268
|
+
});
|
|
269
|
+
router.get("/api/adapters", async (_req, res) => {
|
|
270
|
+
const adapters = Array.from(deps.core.adapters.entries()).map(([name]) => ({
|
|
271
|
+
name,
|
|
272
|
+
type: "built-in"
|
|
273
|
+
}));
|
|
274
|
+
deps.sendJson(res, 200, { adapters });
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// src/plugins/api-server/routes/sessions.ts
|
|
279
|
+
var log = createChildLogger({ module: "api-server" });
|
|
280
|
+
function registerSessionRoutes(router, deps) {
|
|
281
|
+
router.post("/api/sessions/adopt", async (req, res) => {
|
|
282
|
+
const body = await deps.readBody(req);
|
|
283
|
+
if (body === null) {
|
|
284
|
+
return deps.sendJson(res, 413, { error: "Request body too large" });
|
|
285
|
+
}
|
|
286
|
+
if (!body) {
|
|
287
|
+
return deps.sendJson(res, 400, {
|
|
288
|
+
error: "bad_request",
|
|
289
|
+
message: "Empty request body"
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
let parsed;
|
|
293
|
+
try {
|
|
294
|
+
parsed = JSON.parse(body);
|
|
295
|
+
} catch {
|
|
296
|
+
return deps.sendJson(res, 400, {
|
|
297
|
+
error: "bad_request",
|
|
298
|
+
message: "Invalid JSON"
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
const { agent, agentSessionId, cwd, channel } = parsed;
|
|
302
|
+
if (!agent || !agentSessionId) {
|
|
303
|
+
return deps.sendJson(res, 400, {
|
|
304
|
+
error: "bad_request",
|
|
305
|
+
message: "Missing required fields: agent, agentSessionId"
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
const result = await deps.core.adoptSession(
|
|
309
|
+
agent,
|
|
310
|
+
agentSessionId,
|
|
311
|
+
cwd ?? process.cwd(),
|
|
312
|
+
channel
|
|
313
|
+
);
|
|
314
|
+
if (result.ok) {
|
|
315
|
+
return deps.sendJson(res, 200, result);
|
|
316
|
+
} else {
|
|
317
|
+
const status = result.error === "session_limit" ? 429 : result.error === "agent_not_supported" ? 400 : 500;
|
|
318
|
+
return deps.sendJson(res, status, result);
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
router.post("/api/sessions", async (req, res) => {
|
|
322
|
+
const body = await deps.readBody(req);
|
|
323
|
+
let agent;
|
|
324
|
+
let workspace;
|
|
325
|
+
if (body) {
|
|
326
|
+
try {
|
|
327
|
+
const parsed = JSON.parse(body);
|
|
328
|
+
agent = parsed.agent;
|
|
329
|
+
workspace = parsed.workspace;
|
|
330
|
+
} catch {
|
|
331
|
+
deps.sendJson(res, 400, { error: "Invalid JSON body" });
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
const config = deps.core.configManager.get();
|
|
336
|
+
const activeSessions = deps.core.sessionManager.listSessions().filter((s) => s.status === "active" || s.status === "initializing");
|
|
337
|
+
if (activeSessions.length >= config.security.maxConcurrentSessions) {
|
|
338
|
+
deps.sendJson(res, 429, {
|
|
339
|
+
error: `Max concurrent sessions (${config.security.maxConcurrentSessions}) reached. Cancel a session first.`
|
|
340
|
+
});
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
const [adapterId, adapter] = deps.core.adapters.entries().next().value ?? [
|
|
344
|
+
null,
|
|
345
|
+
null
|
|
346
|
+
];
|
|
347
|
+
const channelId = adapterId ?? "api";
|
|
348
|
+
const resolvedAgent = agent || config.defaultAgent;
|
|
349
|
+
const resolvedWorkspace = deps.core.configManager.resolveWorkspace(
|
|
350
|
+
workspace || config.agents[resolvedAgent]?.workingDirectory
|
|
351
|
+
);
|
|
352
|
+
const session = await deps.core.createSession({
|
|
353
|
+
channelId,
|
|
354
|
+
agentName: resolvedAgent,
|
|
355
|
+
workingDirectory: resolvedWorkspace,
|
|
356
|
+
createThread: !!adapter,
|
|
357
|
+
initialName: `\u{1F504} ${resolvedAgent} \u2014 New Session`
|
|
358
|
+
});
|
|
359
|
+
if (!adapter) {
|
|
360
|
+
session.agentInstance.onPermissionRequest = async (request) => {
|
|
361
|
+
const allowOption = request.options.find((o) => o.isAllow);
|
|
362
|
+
log.debug(
|
|
363
|
+
{
|
|
364
|
+
sessionId: session.id,
|
|
365
|
+
permissionId: request.id,
|
|
366
|
+
option: allowOption?.id
|
|
367
|
+
},
|
|
368
|
+
"Auto-approving permission for API session"
|
|
369
|
+
);
|
|
370
|
+
return allowOption?.id ?? request.options[0]?.id ?? "";
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
session.warmup().catch(
|
|
374
|
+
(err) => log.warn({ err, sessionId: session.id }, "API session warmup failed")
|
|
375
|
+
);
|
|
376
|
+
deps.sendJson(res, 200, {
|
|
377
|
+
sessionId: session.id,
|
|
378
|
+
agent: session.agentName,
|
|
379
|
+
status: session.status,
|
|
380
|
+
workspace: session.workingDirectory
|
|
381
|
+
});
|
|
382
|
+
});
|
|
383
|
+
router.post("/api/sessions/:sessionId/prompt", async (req, res, params) => {
|
|
384
|
+
const sessionId = decodeURIComponent(params.sessionId);
|
|
385
|
+
const session = deps.core.sessionManager.getSession(sessionId);
|
|
386
|
+
if (!session) {
|
|
387
|
+
deps.sendJson(res, 404, { error: `Session "${sessionId}" not found` });
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
if (session.status === "cancelled" || session.status === "finished" || session.status === "error") {
|
|
391
|
+
deps.sendJson(res, 400, { error: `Session is ${session.status}` });
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
const body = await deps.readBody(req);
|
|
395
|
+
let prompt;
|
|
396
|
+
if (body) {
|
|
397
|
+
try {
|
|
398
|
+
const parsed = JSON.parse(body);
|
|
399
|
+
prompt = parsed.prompt;
|
|
400
|
+
} catch {
|
|
401
|
+
deps.sendJson(res, 400, { error: "Invalid JSON body" });
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (!prompt) {
|
|
406
|
+
deps.sendJson(res, 400, { error: "Missing prompt" });
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
session.enqueuePrompt(prompt).catch(() => {
|
|
410
|
+
});
|
|
411
|
+
deps.sendJson(res, 200, {
|
|
412
|
+
ok: true,
|
|
413
|
+
sessionId,
|
|
414
|
+
queueDepth: session.queueDepth
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
router.post(
|
|
418
|
+
"/api/sessions/:sessionId/permission",
|
|
419
|
+
async (req, res, params) => {
|
|
420
|
+
const sessionId = decodeURIComponent(params.sessionId);
|
|
421
|
+
const session = deps.core.sessionManager.getSession(sessionId);
|
|
422
|
+
if (!session) {
|
|
423
|
+
deps.sendJson(res, 404, { error: `Session "${sessionId}" not found` });
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
const body = await deps.readBody(req);
|
|
427
|
+
let permissionId;
|
|
428
|
+
let optionId;
|
|
429
|
+
if (body) {
|
|
430
|
+
try {
|
|
431
|
+
const parsed = JSON.parse(body);
|
|
432
|
+
permissionId = parsed.permissionId;
|
|
433
|
+
optionId = parsed.optionId;
|
|
434
|
+
} catch {
|
|
435
|
+
deps.sendJson(res, 400, { error: "Invalid JSON body" });
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
if (!permissionId || !optionId) {
|
|
440
|
+
deps.sendJson(res, 400, {
|
|
441
|
+
error: "Missing permissionId or optionId"
|
|
442
|
+
});
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
if (!session.permissionGate.isPending || session.permissionGate.requestId !== permissionId) {
|
|
446
|
+
deps.sendJson(res, 400, {
|
|
447
|
+
error: "No matching pending permission request"
|
|
448
|
+
});
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
session.permissionGate.resolve(optionId);
|
|
452
|
+
deps.sendJson(res, 200, { ok: true });
|
|
453
|
+
}
|
|
454
|
+
);
|
|
455
|
+
router.patch(
|
|
456
|
+
"/api/sessions/:sessionId/dangerous",
|
|
457
|
+
async (req, res, params) => {
|
|
458
|
+
const sessionId = decodeURIComponent(params.sessionId);
|
|
459
|
+
const session = deps.core.sessionManager.getSession(sessionId);
|
|
460
|
+
if (!session) {
|
|
461
|
+
deps.sendJson(res, 404, { error: `Session "${sessionId}" not found` });
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
const body = await deps.readBody(req);
|
|
465
|
+
let enabled;
|
|
466
|
+
if (body) {
|
|
467
|
+
try {
|
|
468
|
+
const parsed = JSON.parse(body);
|
|
469
|
+
enabled = parsed.enabled;
|
|
470
|
+
} catch {
|
|
471
|
+
deps.sendJson(res, 400, { error: "Invalid JSON body" });
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
if (typeof enabled !== "boolean") {
|
|
476
|
+
deps.sendJson(res, 400, { error: "Missing enabled boolean" });
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
session.dangerousMode = enabled;
|
|
480
|
+
await deps.core.sessionManager.patchRecord(sessionId, {
|
|
481
|
+
dangerousMode: enabled
|
|
482
|
+
});
|
|
483
|
+
deps.sendJson(res, 200, { ok: true, dangerousMode: enabled });
|
|
484
|
+
}
|
|
485
|
+
);
|
|
486
|
+
router.get("/api/sessions/:sessionId", async (_req, res, params) => {
|
|
487
|
+
const sessionId = decodeURIComponent(params.sessionId);
|
|
488
|
+
const session = deps.core.sessionManager.getSession(sessionId);
|
|
489
|
+
if (!session) {
|
|
490
|
+
deps.sendJson(res, 404, { error: `Session "${sessionId}" not found` });
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
deps.sendJson(res, 200, {
|
|
494
|
+
session: {
|
|
495
|
+
id: session.id,
|
|
496
|
+
agent: session.agentName,
|
|
497
|
+
status: session.status,
|
|
498
|
+
name: session.name ?? null,
|
|
499
|
+
workspace: session.workingDirectory,
|
|
500
|
+
createdAt: session.createdAt.toISOString(),
|
|
501
|
+
dangerousMode: session.dangerousMode,
|
|
502
|
+
queueDepth: session.queueDepth,
|
|
503
|
+
promptRunning: session.promptRunning,
|
|
504
|
+
threadId: session.threadId,
|
|
505
|
+
channelId: session.channelId,
|
|
506
|
+
agentSessionId: session.agentSessionId
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
});
|
|
510
|
+
router.post("/api/sessions/:sessionId/summary", async (_req, res, params) => {
|
|
511
|
+
const sessionId = decodeURIComponent(params.sessionId);
|
|
512
|
+
const result = await deps.core.summarizeSession(sessionId);
|
|
513
|
+
if (result.ok) {
|
|
514
|
+
deps.sendJson(res, 200, result);
|
|
515
|
+
} else {
|
|
516
|
+
deps.sendJson(res, 400, result);
|
|
517
|
+
}
|
|
518
|
+
});
|
|
519
|
+
router.post("/api/sessions/:sessionId/archive", async (_req, res, params) => {
|
|
520
|
+
const sessionId = decodeURIComponent(params.sessionId);
|
|
521
|
+
const result = await deps.core.archiveSession(sessionId);
|
|
522
|
+
if (result.ok) {
|
|
523
|
+
deps.sendJson(res, 200, result);
|
|
524
|
+
} else {
|
|
525
|
+
deps.sendJson(res, 400, result);
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
router.delete("/api/sessions/:sessionId", async (_req, res, params) => {
|
|
529
|
+
const sessionId = decodeURIComponent(params.sessionId);
|
|
530
|
+
const session = deps.core.sessionManager.getSession(sessionId);
|
|
531
|
+
if (!session) {
|
|
532
|
+
deps.sendJson(res, 404, { error: `Session "${sessionId}" not found` });
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
await deps.core.sessionManager.cancelSession(sessionId);
|
|
536
|
+
deps.sendJson(res, 200, { ok: true });
|
|
537
|
+
});
|
|
538
|
+
router.get("/api/sessions", async (_req, res) => {
|
|
539
|
+
const sessions = deps.core.sessionManager.listSessions();
|
|
540
|
+
deps.sendJson(res, 200, {
|
|
541
|
+
sessions: sessions.map((s) => ({
|
|
542
|
+
id: s.id,
|
|
543
|
+
agent: s.agentName,
|
|
544
|
+
status: s.status,
|
|
545
|
+
name: s.name ?? null,
|
|
546
|
+
workspace: s.workingDirectory,
|
|
547
|
+
createdAt: s.createdAt.toISOString(),
|
|
548
|
+
dangerousMode: s.dangerousMode,
|
|
549
|
+
queueDepth: s.queueDepth,
|
|
550
|
+
promptRunning: s.promptRunning,
|
|
551
|
+
lastActiveAt: deps.core.sessionManager.getSessionRecord(s.id)?.lastActiveAt ?? null
|
|
552
|
+
}))
|
|
553
|
+
});
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// src/plugins/api-server/routes/config.ts
|
|
558
|
+
var SENSITIVE_KEYS = [
|
|
559
|
+
"botToken",
|
|
560
|
+
"token",
|
|
561
|
+
"apiKey",
|
|
562
|
+
"secret",
|
|
563
|
+
"password",
|
|
564
|
+
"webhookSecret"
|
|
565
|
+
];
|
|
566
|
+
function redactConfig(config) {
|
|
567
|
+
const redacted = structuredClone(config);
|
|
568
|
+
redactDeep(redacted);
|
|
569
|
+
return redacted;
|
|
570
|
+
}
|
|
571
|
+
function redactDeep(obj) {
|
|
572
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
573
|
+
if (SENSITIVE_KEYS.includes(key) && typeof value === "string") {
|
|
574
|
+
obj[key] = "***";
|
|
575
|
+
} else if (Array.isArray(value)) {
|
|
576
|
+
for (const item of value) {
|
|
577
|
+
if (item && typeof item === "object")
|
|
578
|
+
redactDeep(item);
|
|
579
|
+
}
|
|
580
|
+
} else if (value && typeof value === "object") {
|
|
581
|
+
redactDeep(value);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
function registerConfigRoutes(router, deps) {
|
|
586
|
+
router.get("/api/config/editable", async (_req, res) => {
|
|
587
|
+
const { getSafeFields, resolveOptions, getConfigValue } = await import("./config-registry-CUMNXFGK.js");
|
|
588
|
+
const config = deps.core.configManager.get();
|
|
589
|
+
const safeFields = getSafeFields();
|
|
590
|
+
const fields = safeFields.map((def) => ({
|
|
591
|
+
path: def.path,
|
|
592
|
+
displayName: def.displayName,
|
|
593
|
+
group: def.group,
|
|
594
|
+
type: def.type,
|
|
595
|
+
options: resolveOptions(def, config),
|
|
596
|
+
value: getConfigValue(config, def.path),
|
|
597
|
+
hotReload: def.hotReload
|
|
598
|
+
}));
|
|
599
|
+
deps.sendJson(res, 200, { fields });
|
|
600
|
+
});
|
|
601
|
+
router.get("/api/config", async (_req, res) => {
|
|
602
|
+
const config = deps.core.configManager.get();
|
|
603
|
+
deps.sendJson(res, 200, { config: redactConfig(config) });
|
|
604
|
+
});
|
|
605
|
+
router.patch("/api/config", async (req, res) => {
|
|
606
|
+
const body = await deps.readBody(req);
|
|
607
|
+
let configPath;
|
|
608
|
+
let value;
|
|
609
|
+
if (body) {
|
|
610
|
+
try {
|
|
611
|
+
const parsed = JSON.parse(body);
|
|
612
|
+
configPath = parsed.path;
|
|
613
|
+
value = parsed.value;
|
|
614
|
+
} catch {
|
|
615
|
+
deps.sendJson(res, 400, { error: "Invalid JSON body" });
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
if (!configPath) {
|
|
620
|
+
deps.sendJson(res, 400, { error: "Missing path" });
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
const BLOCKED_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
624
|
+
const parts = configPath.split(".");
|
|
625
|
+
if (parts.some((p) => BLOCKED_KEYS.has(p))) {
|
|
626
|
+
deps.sendJson(res, 400, { error: "Invalid config path" });
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
const { getFieldDef } = await import("./config-registry-CUMNXFGK.js");
|
|
630
|
+
const fieldDef = getFieldDef(configPath);
|
|
631
|
+
if (!fieldDef || fieldDef.scope !== "safe") {
|
|
632
|
+
deps.sendJson(res, 403, {
|
|
633
|
+
error: "This config field cannot be modified via the API"
|
|
634
|
+
});
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
const currentConfig = deps.core.configManager.get();
|
|
638
|
+
const cloned = structuredClone(currentConfig);
|
|
639
|
+
let target = cloned;
|
|
640
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
641
|
+
const part = parts[i];
|
|
642
|
+
if (target[part] && typeof target[part] === "object" && !Array.isArray(target[part])) {
|
|
643
|
+
target = target[part];
|
|
644
|
+
} else if (target[part] === void 0 || target[part] === null) {
|
|
645
|
+
target[part] = {};
|
|
646
|
+
target = target[part];
|
|
647
|
+
} else {
|
|
648
|
+
deps.sendJson(res, 400, { error: "Invalid config path" });
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
const lastKey = parts[parts.length - 1];
|
|
653
|
+
target[lastKey] = value;
|
|
654
|
+
const { ConfigSchema } = await import("./config-I4FMCJGZ.js");
|
|
655
|
+
const result = ConfigSchema.safeParse(cloned);
|
|
656
|
+
if (!result.success) {
|
|
657
|
+
deps.sendJson(res, 400, {
|
|
658
|
+
error: "Validation failed",
|
|
659
|
+
details: result.error.issues.map((i) => ({
|
|
660
|
+
path: i.path.join("."),
|
|
661
|
+
message: i.message
|
|
662
|
+
}))
|
|
663
|
+
});
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
const updates = {};
|
|
667
|
+
let updateTarget = updates;
|
|
668
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
669
|
+
updateTarget[parts[i]] = {};
|
|
670
|
+
updateTarget = updateTarget[parts[i]];
|
|
671
|
+
}
|
|
672
|
+
updateTarget[lastKey] = value;
|
|
673
|
+
await deps.core.configManager.save(updates, configPath);
|
|
674
|
+
const { isHotReloadable } = await import("./config-registry-CUMNXFGK.js");
|
|
675
|
+
const needsRestart = !isHotReloadable(configPath);
|
|
676
|
+
deps.sendJson(res, 200, {
|
|
677
|
+
ok: true,
|
|
678
|
+
needsRestart,
|
|
679
|
+
config: redactConfig(deps.core.configManager.get())
|
|
680
|
+
});
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// src/plugins/api-server/routes/topics.ts
|
|
685
|
+
function registerTopicRoutes(router, deps) {
|
|
686
|
+
router.get("/api/topics", async (req, res) => {
|
|
687
|
+
if (!deps.topicManager) {
|
|
688
|
+
deps.sendJson(res, 501, { error: "Topic management not available" });
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
const url = req.url || "";
|
|
692
|
+
const params = new URL(url, "http://localhost").searchParams;
|
|
693
|
+
const statusParam = params.get("status");
|
|
694
|
+
const filter = statusParam ? { statuses: statusParam.split(",") } : void 0;
|
|
695
|
+
const topics = deps.topicManager.listTopics(filter);
|
|
696
|
+
deps.sendJson(res, 200, { topics });
|
|
697
|
+
});
|
|
698
|
+
router.post("/api/topics/cleanup", async (req, res) => {
|
|
699
|
+
if (!deps.topicManager) {
|
|
700
|
+
deps.sendJson(res, 501, { error: "Topic management not available" });
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
const body = await deps.readBody(req);
|
|
704
|
+
let statuses;
|
|
705
|
+
if (body) {
|
|
706
|
+
try {
|
|
707
|
+
statuses = JSON.parse(body).statuses;
|
|
708
|
+
} catch {
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
const result = await deps.topicManager.cleanup(statuses);
|
|
712
|
+
deps.sendJson(res, 200, result);
|
|
713
|
+
});
|
|
714
|
+
router.delete("/api/topics/:sessionId", async (req, res, params) => {
|
|
715
|
+
if (!deps.topicManager) {
|
|
716
|
+
deps.sendJson(res, 501, { error: "Topic management not available" });
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
const sessionId = decodeURIComponent(params.sessionId);
|
|
720
|
+
const url = req.url || "";
|
|
721
|
+
const urlParams = new URL(url, "http://localhost").searchParams;
|
|
722
|
+
const force = urlParams.get("force") === "true";
|
|
723
|
+
const result = await deps.topicManager.deleteTopic(
|
|
724
|
+
sessionId,
|
|
725
|
+
force ? { confirmed: true } : void 0
|
|
726
|
+
);
|
|
727
|
+
if (result.ok) {
|
|
728
|
+
deps.sendJson(res, 200, result);
|
|
729
|
+
} else if (result.needsConfirmation) {
|
|
730
|
+
deps.sendJson(res, 409, {
|
|
731
|
+
error: "Session is active",
|
|
732
|
+
needsConfirmation: true,
|
|
733
|
+
session: result.session
|
|
734
|
+
});
|
|
735
|
+
} else if (result.error === "Cannot delete system topic") {
|
|
736
|
+
deps.sendJson(res, 403, { error: result.error });
|
|
737
|
+
} else {
|
|
738
|
+
deps.sendJson(res, 404, { error: result.error ?? "Not found" });
|
|
739
|
+
}
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// src/plugins/api-server/routes/tunnel.ts
|
|
744
|
+
function registerTunnelRoutes(router, deps) {
|
|
745
|
+
router.get("/api/tunnel", async (_req, res) => {
|
|
746
|
+
const tunnel = deps.core.tunnelService;
|
|
747
|
+
if (tunnel) {
|
|
748
|
+
deps.sendJson(res, 200, {
|
|
749
|
+
enabled: true,
|
|
750
|
+
url: tunnel.getPublicUrl(),
|
|
751
|
+
provider: deps.core.configManager.get().tunnel.provider
|
|
752
|
+
});
|
|
753
|
+
} else {
|
|
754
|
+
deps.sendJson(res, 200, { enabled: false });
|
|
755
|
+
}
|
|
756
|
+
});
|
|
757
|
+
router.get("/api/tunnel/list", async (_req, res) => {
|
|
758
|
+
const tunnel = deps.core.tunnelService;
|
|
759
|
+
if (!tunnel) {
|
|
760
|
+
deps.sendJson(res, 200, []);
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
deps.sendJson(res, 200, tunnel.listTunnels());
|
|
764
|
+
});
|
|
765
|
+
router.post("/api/tunnel", async (req, res) => {
|
|
766
|
+
const tunnel = deps.core.tunnelService;
|
|
767
|
+
if (!tunnel) {
|
|
768
|
+
deps.sendJson(res, 400, { error: "Tunnel service is not enabled" });
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
const body = await deps.readBody(req);
|
|
772
|
+
if (body === null) {
|
|
773
|
+
deps.sendJson(res, 413, { error: "Request body too large" });
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
if (!body) {
|
|
777
|
+
deps.sendJson(res, 400, { error: "Missing request body" });
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
try {
|
|
781
|
+
const { port, label, sessionId } = JSON.parse(body);
|
|
782
|
+
if (!port || typeof port !== "number") {
|
|
783
|
+
deps.sendJson(res, 400, {
|
|
784
|
+
error: "port is required and must be a number"
|
|
785
|
+
});
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
const entry = await tunnel.addTunnel(port, { label, sessionId });
|
|
789
|
+
deps.sendJson(res, 200, entry);
|
|
790
|
+
} catch (err) {
|
|
791
|
+
deps.sendJson(res, 400, { error: err.message });
|
|
792
|
+
}
|
|
793
|
+
});
|
|
794
|
+
router.delete("/api/tunnel/:port", async (_req, res, params) => {
|
|
795
|
+
const tunnel = deps.core.tunnelService;
|
|
796
|
+
if (!tunnel) {
|
|
797
|
+
deps.sendJson(res, 400, { error: "Tunnel service is not enabled" });
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
const port = parseInt(params.port, 10);
|
|
801
|
+
try {
|
|
802
|
+
await tunnel.stopTunnel(port);
|
|
803
|
+
deps.sendJson(res, 200, { ok: true });
|
|
804
|
+
} catch (err) {
|
|
805
|
+
deps.sendJson(res, 400, { error: err.message });
|
|
806
|
+
}
|
|
807
|
+
});
|
|
808
|
+
router.delete("/api/tunnel", async (_req, res) => {
|
|
809
|
+
const tunnel = deps.core.tunnelService;
|
|
810
|
+
if (!tunnel) {
|
|
811
|
+
deps.sendJson(res, 400, { error: "Tunnel service is not enabled" });
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
const count = tunnel.listTunnels().length;
|
|
815
|
+
await tunnel.stopAllUser();
|
|
816
|
+
deps.sendJson(res, 200, { ok: true, stopped: count });
|
|
817
|
+
});
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
// src/plugins/api-server/routes/agents.ts
|
|
821
|
+
function registerAgentRoutes(router, deps) {
|
|
822
|
+
router.get("/api/agents", async (_req, res) => {
|
|
823
|
+
const agents = deps.core.agentManager.getAvailableAgents();
|
|
824
|
+
const defaultAgent = deps.core.configManager.get().defaultAgent;
|
|
825
|
+
const agentsWithCaps = agents.map((a) => ({
|
|
826
|
+
...a,
|
|
827
|
+
capabilities: getAgentCapabilities(a.name)
|
|
828
|
+
}));
|
|
829
|
+
deps.sendJson(res, 200, { agents: agentsWithCaps, default: defaultAgent });
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// src/plugins/api-server/routes/notify.ts
|
|
834
|
+
function registerNotifyRoutes(router, deps) {
|
|
835
|
+
router.post("/api/notify", async (req, res) => {
|
|
836
|
+
const body = await deps.readBody(req);
|
|
837
|
+
let message;
|
|
838
|
+
if (body) {
|
|
839
|
+
try {
|
|
840
|
+
const parsed = JSON.parse(body);
|
|
841
|
+
message = parsed.message;
|
|
842
|
+
} catch {
|
|
843
|
+
deps.sendJson(res, 400, { error: "Invalid JSON body" });
|
|
844
|
+
return;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
if (!message) {
|
|
848
|
+
deps.sendJson(res, 400, { error: "Missing message" });
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
await deps.core.notificationManager.notifyAll({
|
|
852
|
+
sessionId: "system",
|
|
853
|
+
type: "completed",
|
|
854
|
+
summary: message
|
|
855
|
+
});
|
|
856
|
+
deps.sendJson(res, 200, { ok: true });
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// src/plugins/api-server/api-server.ts
|
|
861
|
+
var log2 = createChildLogger({ module: "api-server" });
|
|
862
|
+
var DEFAULT_PORT_FILE = path2.join(os.homedir(), ".openacp", "api.port");
|
|
863
|
+
var cachedVersion;
|
|
864
|
+
function getVersion() {
|
|
865
|
+
if (cachedVersion) return cachedVersion;
|
|
866
|
+
try {
|
|
867
|
+
const __filename = fileURLToPath2(import.meta.url);
|
|
868
|
+
const pkgPath = path2.resolve(
|
|
869
|
+
path2.dirname(__filename),
|
|
870
|
+
"../../../package.json"
|
|
871
|
+
);
|
|
872
|
+
const pkg = JSON.parse(fs2.readFileSync(pkgPath, "utf-8"));
|
|
873
|
+
cachedVersion = pkg.version ?? "0.0.0-dev";
|
|
874
|
+
} catch {
|
|
875
|
+
cachedVersion = "0.0.0-dev";
|
|
876
|
+
}
|
|
877
|
+
return cachedVersion;
|
|
878
|
+
}
|
|
879
|
+
var ApiServer = class {
|
|
880
|
+
constructor(core, config, portFilePath, topicManager, secretFilePath, uiDir) {
|
|
881
|
+
this.core = core;
|
|
882
|
+
this.config = config;
|
|
883
|
+
this.topicManager = topicManager;
|
|
884
|
+
this.portFilePath = portFilePath ?? DEFAULT_PORT_FILE;
|
|
885
|
+
this.secretFilePath = secretFilePath ?? path2.join(os.homedir(), ".openacp", "api-secret");
|
|
886
|
+
this.staticServer = new StaticServer(uiDir);
|
|
887
|
+
this.sseManager = new SSEManager(
|
|
888
|
+
core.eventBus,
|
|
889
|
+
() => {
|
|
890
|
+
const sessions = this.core.sessionManager.listSessions();
|
|
891
|
+
return {
|
|
892
|
+
active: sessions.filter(
|
|
893
|
+
(s) => s.status === "active" || s.status === "initializing"
|
|
894
|
+
).length,
|
|
895
|
+
total: sessions.length
|
|
896
|
+
};
|
|
897
|
+
},
|
|
898
|
+
this.startedAt
|
|
899
|
+
);
|
|
900
|
+
this.router = new Router();
|
|
901
|
+
const deps = {
|
|
902
|
+
core: this.core,
|
|
903
|
+
topicManager: this.topicManager,
|
|
904
|
+
startedAt: this.startedAt,
|
|
905
|
+
getVersion,
|
|
906
|
+
sendJson: this.sendJson.bind(this),
|
|
907
|
+
readBody: this.readBody.bind(this)
|
|
908
|
+
};
|
|
909
|
+
registerHealthRoutes(this.router, deps);
|
|
910
|
+
registerSessionRoutes(this.router, deps);
|
|
911
|
+
registerConfigRoutes(this.router, deps);
|
|
912
|
+
registerTopicRoutes(this.router, deps);
|
|
913
|
+
registerTunnelRoutes(this.router, deps);
|
|
914
|
+
registerAgentRoutes(this.router, deps);
|
|
915
|
+
registerNotifyRoutes(this.router, deps);
|
|
916
|
+
}
|
|
917
|
+
server = null;
|
|
918
|
+
actualPort = 0;
|
|
919
|
+
portFilePath;
|
|
920
|
+
startedAt = Date.now();
|
|
921
|
+
secret = "";
|
|
922
|
+
secretFilePath;
|
|
923
|
+
sseManager;
|
|
924
|
+
staticServer;
|
|
925
|
+
router;
|
|
926
|
+
async start() {
|
|
927
|
+
this.loadOrCreateSecret();
|
|
928
|
+
this.server = http.createServer((req, res) => this.handleRequest(req, res));
|
|
929
|
+
await new Promise((resolve3, reject) => {
|
|
930
|
+
this.server.on("error", (err) => {
|
|
931
|
+
if (err.code === "EADDRINUSE") {
|
|
932
|
+
log2.warn(
|
|
933
|
+
{ port: this.config.port },
|
|
934
|
+
"API port in use, continuing without API server"
|
|
935
|
+
);
|
|
936
|
+
this.server = null;
|
|
937
|
+
resolve3();
|
|
938
|
+
} else {
|
|
939
|
+
reject(err);
|
|
940
|
+
}
|
|
941
|
+
});
|
|
942
|
+
this.server.listen(this.config.port, this.config.host, () => {
|
|
943
|
+
const addr = this.server.address();
|
|
944
|
+
if (addr && typeof addr === "object") {
|
|
945
|
+
this.actualPort = addr.port;
|
|
946
|
+
}
|
|
947
|
+
this.writePortFile();
|
|
948
|
+
log2.info(
|
|
949
|
+
{ host: this.config.host, port: this.actualPort },
|
|
950
|
+
"API server listening"
|
|
951
|
+
);
|
|
952
|
+
this.sseManager.setup();
|
|
953
|
+
if (this.config.host !== "127.0.0.1" && this.config.host !== "localhost") {
|
|
954
|
+
log2.warn(
|
|
955
|
+
"API server binding to non-localhost. Ensure api-secret file is secured."
|
|
956
|
+
);
|
|
957
|
+
}
|
|
958
|
+
resolve3();
|
|
959
|
+
});
|
|
960
|
+
});
|
|
961
|
+
}
|
|
962
|
+
async stop() {
|
|
963
|
+
this.sseManager.stop();
|
|
964
|
+
this.removePortFile();
|
|
965
|
+
if (this.server) {
|
|
966
|
+
await new Promise((resolve3) => {
|
|
967
|
+
this.server.close(() => resolve3());
|
|
968
|
+
});
|
|
969
|
+
this.server = null;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
getPort() {
|
|
973
|
+
return this.actualPort;
|
|
974
|
+
}
|
|
975
|
+
getSecret() {
|
|
976
|
+
return this.secret;
|
|
977
|
+
}
|
|
978
|
+
writePortFile() {
|
|
979
|
+
const dir = path2.dirname(this.portFilePath);
|
|
980
|
+
fs2.mkdirSync(dir, { recursive: true });
|
|
981
|
+
fs2.writeFileSync(this.portFilePath, String(this.actualPort));
|
|
982
|
+
}
|
|
983
|
+
removePortFile() {
|
|
984
|
+
try {
|
|
985
|
+
fs2.unlinkSync(this.portFilePath);
|
|
986
|
+
} catch {
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
loadOrCreateSecret() {
|
|
990
|
+
const dir = path2.dirname(this.secretFilePath);
|
|
991
|
+
fs2.mkdirSync(dir, { recursive: true });
|
|
992
|
+
try {
|
|
993
|
+
this.secret = fs2.readFileSync(this.secretFilePath, "utf-8").trim();
|
|
994
|
+
if (this.secret) {
|
|
995
|
+
try {
|
|
996
|
+
const stat = fs2.statSync(this.secretFilePath);
|
|
997
|
+
const mode = stat.mode & 511;
|
|
998
|
+
if (mode & 63) {
|
|
999
|
+
log2.warn(
|
|
1000
|
+
{ path: this.secretFilePath, mode: "0" + mode.toString(8) },
|
|
1001
|
+
"API secret file has insecure permissions (should be 0600). Run: chmod 600 %s",
|
|
1002
|
+
this.secretFilePath
|
|
1003
|
+
);
|
|
1004
|
+
}
|
|
1005
|
+
} catch {
|
|
1006
|
+
}
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
} catch {
|
|
1010
|
+
}
|
|
1011
|
+
this.secret = crypto.randomBytes(32).toString("hex");
|
|
1012
|
+
fs2.writeFileSync(this.secretFilePath, this.secret, { mode: 384 });
|
|
1013
|
+
}
|
|
1014
|
+
authenticate(req, allowQueryParam = false) {
|
|
1015
|
+
const authHeader = req.headers.authorization;
|
|
1016
|
+
if (authHeader?.startsWith("Bearer ")) {
|
|
1017
|
+
const token = authHeader.slice(7);
|
|
1018
|
+
if (token.length === this.secret.length && crypto.timingSafeEqual(
|
|
1019
|
+
Buffer.from(token, "utf-8"),
|
|
1020
|
+
Buffer.from(this.secret, "utf-8")
|
|
1021
|
+
)) {
|
|
1022
|
+
return true;
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
if (allowQueryParam) {
|
|
1026
|
+
const parsedUrl = new URL(req.url || "", "http://localhost");
|
|
1027
|
+
const qToken = parsedUrl.searchParams.get("token");
|
|
1028
|
+
if (qToken && qToken.length === this.secret.length && crypto.timingSafeEqual(
|
|
1029
|
+
Buffer.from(qToken, "utf-8"),
|
|
1030
|
+
Buffer.from(this.secret, "utf-8")
|
|
1031
|
+
)) {
|
|
1032
|
+
return true;
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
return false;
|
|
1036
|
+
}
|
|
1037
|
+
async handleRequest(req, res) {
|
|
1038
|
+
const method = req.method?.toUpperCase();
|
|
1039
|
+
const url = req.url || "";
|
|
1040
|
+
if (url.startsWith("/api/")) {
|
|
1041
|
+
const isExempt = method === "GET" && (url === "/api/health" || url === "/api/version" || url.startsWith("/api/events"));
|
|
1042
|
+
if (!isExempt && !this.authenticate(req)) {
|
|
1043
|
+
this.sendJson(res, 401, { error: "Unauthorized" });
|
|
1044
|
+
return;
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
try {
|
|
1048
|
+
if (method === "GET" && url.startsWith("/api/events")) {
|
|
1049
|
+
if (!this.authenticate(req, true)) {
|
|
1050
|
+
this.sendJson(res, 401, { error: "Unauthorized" });
|
|
1051
|
+
return;
|
|
1052
|
+
}
|
|
1053
|
+
this.sseManager.handleRequest(req, res);
|
|
1054
|
+
return;
|
|
1055
|
+
}
|
|
1056
|
+
if (url.startsWith("/api/")) {
|
|
1057
|
+
const match = this.router.match(method, url);
|
|
1058
|
+
if (match) {
|
|
1059
|
+
await match.handler(req, res, match.params);
|
|
1060
|
+
} else {
|
|
1061
|
+
this.sendJson(res, 404, { error: "Not found" });
|
|
1062
|
+
}
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
1065
|
+
if (!this.staticServer.serve(req, res)) {
|
|
1066
|
+
this.sendJson(res, 404, { error: "Not found" });
|
|
1067
|
+
}
|
|
1068
|
+
} catch (err) {
|
|
1069
|
+
log2.error({ err }, "API request error");
|
|
1070
|
+
this.sendJson(res, 500, { error: "Internal server error" });
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
sendJson(res, status, data) {
|
|
1074
|
+
res.writeHead(status, { "Content-Type": "application/json" });
|
|
1075
|
+
res.end(JSON.stringify(data));
|
|
1076
|
+
}
|
|
1077
|
+
readBody(req) {
|
|
1078
|
+
const MAX_BODY_SIZE = 1024 * 1024;
|
|
1079
|
+
return new Promise((resolve3) => {
|
|
1080
|
+
let data = "";
|
|
1081
|
+
let size = 0;
|
|
1082
|
+
let destroyed = false;
|
|
1083
|
+
req.on("data", (chunk) => {
|
|
1084
|
+
size += chunk.length;
|
|
1085
|
+
if (size > MAX_BODY_SIZE && !destroyed) {
|
|
1086
|
+
destroyed = true;
|
|
1087
|
+
req.destroy();
|
|
1088
|
+
resolve3(null);
|
|
1089
|
+
return;
|
|
1090
|
+
}
|
|
1091
|
+
if (!destroyed) data += chunk;
|
|
1092
|
+
});
|
|
1093
|
+
req.on("end", () => {
|
|
1094
|
+
if (!destroyed) resolve3(data);
|
|
1095
|
+
});
|
|
1096
|
+
req.on("error", () => {
|
|
1097
|
+
if (!destroyed) resolve3("");
|
|
1098
|
+
});
|
|
1099
|
+
});
|
|
1100
|
+
}
|
|
1101
|
+
};
|
|
1102
|
+
|
|
1103
|
+
export {
|
|
1104
|
+
SSEManager,
|
|
1105
|
+
StaticServer,
|
|
1106
|
+
ApiServer
|
|
1107
|
+
};
|
|
1108
|
+
//# sourceMappingURL=chunk-UNJUWWQO.js.map
|