@celljs/ta 4.15.1 → 4.16.0
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/cli.mjs +7 -3
- package/dist/gateway.mjs +2047 -288
- package/dist/pi/CHANGELOG.md +15 -0
- package/dist/pi/package.json +1 -1
- package/dist/pi/packages/ta-3d/package.json +1 -1
- package/dist/pi/packages/ta-agents/extension/index.mjs +8 -6
- package/dist/pi/packages/ta-agents/lib/acp-protocol.d.ts +2 -0
- package/dist/pi/packages/ta-agents/lib/acp-protocol.js +5 -0
- package/dist/pi/packages/ta-agents/lib/agent-session.d.ts +0 -2
- package/dist/pi/packages/ta-agents/lib/agent-session.js +1 -5
- package/dist/pi/packages/ta-agents/lib/agent-turn.d.ts +7 -2
- package/dist/pi/packages/ta-agents/lib/agent-turn.js +8 -4
- package/dist/pi/packages/ta-agents/package.json +1 -1
- package/dist/pi/packages/ta-bots/host/index.mjs +40 -3
- package/dist/pi/packages/ta-bots/package.json +1 -1
- package/dist/pi/packages/ta-changes/package.json +1 -1
- package/dist/pi/packages/ta-channels/host/index.mjs +41 -3
- package/dist/pi/packages/ta-channels/package.json +1 -1
- package/dist/pi/packages/ta-computer-use/package.json +1 -1
- package/dist/pi/packages/ta-cron/package.json +1 -1
- package/dist/pi/packages/ta-extensions/package.json +1 -1
- package/dist/pi/packages/ta-extensions/web/index.js +40 -3
- package/dist/pi/packages/ta-games/package.json +1 -1
- package/dist/pi/packages/ta-glance/README.en.md +2 -0
- package/dist/pi/packages/ta-glance/README.md +2 -0
- package/dist/pi/packages/ta-glance/extension/index.mjs +110 -21
- package/dist/pi/packages/ta-glance/lib/card.js +88 -8
- package/dist/pi/packages/ta-glance/lib/glance-prompt.d.ts +1 -1
- package/dist/pi/packages/ta-glance/lib/glance-prompt.js +7 -4
- package/dist/pi/packages/ta-glance/lib/glance-protocol.d.ts +34 -2
- package/dist/pi/packages/ta-glance/lib/glance-protocol.js +39 -1
- package/dist/pi/packages/ta-glance/lib/glance-spec.js +93 -19
- package/dist/pi/packages/ta-glance/lib/glance-util.d.ts +13 -0
- package/dist/pi/packages/ta-glance/lib/glance-util.js +28 -0
- package/dist/pi/packages/ta-glance/lib/style.d.ts +1 -1
- package/dist/pi/packages/ta-glance/lib/style.js +65 -2
- package/dist/pi/packages/ta-glance/package.json +1 -1
- package/dist/pi/packages/ta-glance/web/index.js +330 -34
- package/dist/pi/packages/ta-initiative/package.json +1 -1
- package/dist/pi/packages/ta-launcher/package.json +1 -1
- package/dist/pi/packages/ta-manual/package.json +1 -1
- package/dist/pi/packages/ta-moments/extension/index.mjs +40 -3
- package/dist/pi/packages/ta-moments/host/index.mjs +40 -3
- package/dist/pi/packages/ta-moments/package.json +1 -1
- package/dist/pi/packages/ta-moments/web/index.js +40 -3
- package/dist/pi/packages/ta-nearby/package.json +1 -1
- package/dist/pi/packages/ta-notify/package.json +1 -1
- package/dist/pi/packages/ta-plot/package.json +1 -1
- package/dist/pi/packages/ta-recall/package.json +1 -1
- package/dist/pi/packages/ta-rooms/package.json +1 -1
- package/dist/pi/packages/ta-share/package.json +1 -1
- package/dist/pi/packages/ta-sync/package.json +1 -1
- package/dist/pi/packages/ta-sync-cloud/package.json +1 -1
- package/dist/pi/packages/ta-team/package.json +1 -1
- package/dist/pi/packages/ta-workspace/package.json +1 -1
- package/dist/pi/packages/ta-workspace/web/index.js +40 -3
- package/dist/public/assets/{abnfDiagram-S7VZAUFO-CD8CaArq.js → abnfDiagram-S7VZAUFO-FSEuUCgT.js} +1 -1
- package/dist/public/assets/{architecture-WOLXFQ4H-f2a9bodL.js → architecture-WOLXFQ4H-Do_DyWCg.js} +1 -1
- package/dist/public/assets/{architectureDiagram-YGSQEFNW-BCWH9CwO.js → architectureDiagram-YGSQEFNW-D-N2qDs4.js} +1 -1
- package/dist/public/assets/{blockDiagram-APKKGZFW-fUamQMeJ.js → blockDiagram-APKKGZFW-Ds6DzHNZ.js} +1 -1
- package/dist/public/assets/{browser-CrMa174j.js → browser-KlrZxPbE.js} +1 -1
- package/dist/public/assets/{c4Diagram-KJ2TOQU5-CkLtnwp1.js → c4Diagram-KJ2TOQU5-D5ZdqxrP.js} +1 -1
- package/dist/public/assets/{chunk-6MUBBAK2-cwcGPuAH.js → chunk-6MUBBAK2-CWOwhB3b.js} +1 -1
- package/dist/public/assets/{chunk-AXFCVDMN-DNkmkYfC.js → chunk-AXFCVDMN-Dr6p2RGg.js} +1 -1
- package/dist/public/assets/{chunk-BG4553SI-C8qbbr2Z.js → chunk-BG4553SI-DUCV_pmW.js} +1 -1
- package/dist/public/assets/{chunk-DQASFODM-C-PJ2CrJ.js → chunk-DQASFODM-Yxd3jIXM.js} +1 -1
- package/dist/public/assets/{chunk-HFQBQIA3-BYcgxNEL.js → chunk-HFQBQIA3-C24I15MF.js} +1 -1
- package/dist/public/assets/{chunk-KQLB2F66-BTDNzrRl.js → chunk-KQLB2F66-Dxk-aejD.js} +1 -1
- package/dist/public/assets/{chunk-KS47KZWR-1sGg1pKP.js → chunk-KS47KZWR-DPyr7gb8.js} +1 -1
- package/dist/public/assets/{chunk-LEVMRMFU-7lAsFwHT.js → chunk-LEVMRMFU-D_mEFJGR.js} +1 -1
- package/dist/public/assets/{chunk-M57EVK23-C8K-ltig.js → chunk-M57EVK23-DfMFxQib.js} +1 -1
- package/dist/public/assets/{chunk-OY52VR5R-CBe1QL_V.js → chunk-OY52VR5R-O9W83Laa.js} +1 -1
- package/dist/public/assets/{chunk-QB3YPZ4I-COhYyyE_.js → chunk-QB3YPZ4I-IdikGcw1.js} +1 -1
- package/dist/public/assets/{chunk-R3KSYZAL-CREAgaSS.js → chunk-R3KSYZAL-DLpaFCvz.js} +1 -1
- package/dist/public/assets/{chunk-STOXU5M4-CTl0P8_C.js → chunk-STOXU5M4-Lx-CZ4Ql.js} +1 -1
- package/dist/public/assets/{chunk-TU62E6Q5-B_GG7baF.js → chunk-TU62E6Q5-DCnr5NsA.js} +1 -1
- package/dist/public/assets/{chunk-X4PVCEUW-CDrSpVOj.js → chunk-X4PVCEUW-Ct7aUJFY.js} +1 -1
- package/dist/public/assets/{chunk-XXDRQBXY-sKeiDEnC.js → chunk-XXDRQBXY-CQhxfoYw.js} +1 -1
- package/dist/public/assets/{classDiagram-v2-L25IHNXF-Cqo1O5FQ.js → classDiagram-v2-L25IHNXF-DPSu5qYq.js} +1 -1
- package/dist/public/assets/{cose-bilkent-JH36ORCC-CTSKeRBe.js → cose-bilkent-JH36ORCC-CFimsFsN.js} +1 -1
- package/dist/public/assets/cynefin-EF2NZ3EQ-Dcc0sCHf.js +1 -0
- package/dist/public/assets/{cynefinDiagram-L36ESFBY-67xEPyP9.js → cynefinDiagram-L36ESFBY-CiAWhkVp.js} +1 -1
- package/dist/public/assets/{d3-selection-BUiX-3qh.js → d3-selection-C8RIDfkO.js} +1 -1
- package/dist/public/assets/{dagre-SPEB5RXH-DDw-u3Kg.js → dagre-SPEB5RXH-l1nRD1Fx.js} +1 -1
- package/dist/public/assets/{diagram-3B7GL73J-BBFInaCs.js → diagram-3B7GL73J-ipqtnhoX.js} +1 -1
- package/dist/public/assets/{diagram-DNEMPZML-ZdnFKM7-.js → diagram-DNEMPZML-VpIcHRUP.js} +1 -1
- package/dist/public/assets/{diagram-L3MUGCDN-CPb04hOv.js → diagram-L3MUGCDN-8VdVmcS6.js} +1 -1
- package/dist/public/assets/{diagram-LA4NIPWU-P9wHFGDS.js → diagram-LA4NIPWU-Bej-aiF0.js} +1 -1
- package/dist/public/assets/{diagram-MHCOC4E2-Ce_Cr-t6.js → diagram-MHCOC4E2-D8gW4bVQ.js} +1 -1
- package/dist/public/assets/{diagram-XPFHEONP-CfS34OHW.js → diagram-XPFHEONP-BMevAJ1M.js} +1 -1
- package/dist/public/assets/{dist-6pZqZEaM.js → dist-DBKJcjvW.js} +1 -1
- package/dist/public/assets/{ebnfDiagram-UZSYDL2S-BkHabCn2.js → ebnfDiagram-UZSYDL2S-C3HDTOzn.js} +1 -1
- package/dist/public/assets/{editor.api-CO4Q0NKb.js → editor.api-BMWdjwhK.js} +1 -1
- package/dist/public/assets/{elk-XV6XMXQE-JpreOVeR.js → elk-XV6XMXQE-XOOPrVin.js} +1 -1
- package/dist/public/assets/{erDiagram-EYBAPDEH-DDFJwzHx.js → erDiagram-EYBAPDEH-Cr-vwx1l.js} +1 -1
- package/dist/public/assets/{eventmodeling-K75KTNOO-DsnkF6Zc.js → eventmodeling-K75KTNOO-DbPhpfxn.js} +1 -1
- package/dist/public/assets/flowDiagram-MALGAFMO-CDXqnHAQ.js +1 -0
- package/dist/public/assets/{freemarker2-Cm4NK652.js → freemarker2-6h_1-8qB.js} +1 -1
- package/dist/public/assets/{ganttDiagram-23REXITY-C7eedoYb.js → ganttDiagram-23REXITY-C7kNPXx3.js} +1 -1
- package/dist/public/assets/gitGraph-VSP46ZUC-KPsoB6Z1.js +1 -0
- package/dist/public/assets/{gitGraphDiagram-STJD7YCW-Cuh3ih-z.js → gitGraphDiagram-STJD7YCW-CVnEJEZr.js} +1 -1
- package/dist/public/assets/{handlebars-BnE51DUi.js → handlebars-F4lp3oLr.js} +1 -1
- package/dist/public/assets/{html-7koekDfa.js → html-Dj6f8B_g.js} +1 -1
- package/dist/public/assets/{index-CQG1XddL.js → index-DnNrrk68.js} +2 -2
- package/dist/public/assets/info-OHQRW6UA-CtM6s0SQ.js +1 -0
- package/dist/public/assets/{infoDiagram-KBXO4MBG-BY3iq7I8.js → infoDiagram-KBXO4MBG-DcttQWdp.js} +1 -1
- package/dist/public/assets/{ishikawaDiagram-M35NCXHK-COjlz24y.js → ishikawaDiagram-M35NCXHK-BSUYTZf6.js} +1 -1
- package/dist/public/assets/{javascript-BUZgUhdn.js → javascript-b_BkOE5Y.js} +1 -1
- package/dist/public/assets/{journeyDiagram-OPLOXDXO-DReJE04Q.js → journeyDiagram-OPLOXDXO-BCfd5-6n.js} +1 -1
- package/dist/public/assets/{jsonMode-qPeVDBDp.js → jsonMode-Cq5QJbVZ.js} +1 -1
- package/dist/public/assets/{kanban-definition-BZKZHSQY-BB0_ePvg.js → kanban-definition-BZKZHSQY-B3nenaw_.js} +1 -1
- package/dist/public/assets/{liquid-CNKL9dLX.js → liquid-CGuZI_Aa.js} +1 -1
- package/dist/public/assets/{mdx-D1_UzrBc.js → mdx-Dt9yTqnf.js} +1 -1
- package/dist/public/assets/{mermaid-parser.core-DiE3z_tV.js → mermaid-parser.core-CquzROwT.js} +3 -3
- package/dist/public/assets/{mermaid.core-CBCRrLdl.js → mermaid.core-Bx-e_Scq.js} +4 -4
- package/dist/public/assets/{mindmap-definition-I2QAS2VK-D-Y5fKl1.js → mindmap-definition-I2QAS2VK-DyeewbnH.js} +1 -1
- package/dist/public/assets/module-C-ofp7U4.js +1 -0
- package/dist/public/assets/module-dnDlIzeV.js +2616 -0
- package/dist/public/assets/{monaco--czRbHhk.js → monaco-nvIdDTWu.js} +3 -3
- package/dist/public/assets/packet-JDAUHWVQ-XKjiwdar.js +1 -0
- package/dist/public/assets/{pegDiagram-FD4XPPHP-1jx5jc73.js → pegDiagram-FD4XPPHP-B884_KoR.js} +1 -1
- package/dist/public/assets/pie-XZMESJXO-MuCMavHL.js +1 -0
- package/dist/public/assets/{pieDiagram-J4AKIIQI-DGv_WCCu.js → pieDiagram-J4AKIIQI-I40wRwyy.js} +1 -1
- package/dist/public/assets/{python-DNZu5LjV.js → python-Djw4wvG9.js} +1 -1
- package/dist/public/assets/{quadrantDiagram-XTYBLID2-BgY0lf8V.js → quadrantDiagram-XTYBLID2-DyNOImfk.js} +1 -1
- package/dist/public/assets/radar-ABXABTNO-Cq12GHJS.js +1 -0
- package/dist/public/assets/railroad-I3PHUGI6-CfeMswZU.js +1 -0
- package/dist/public/assets/{railroad-abnf-LNEFI6M7-tZ7rJxwM.js → railroad-abnf-LNEFI6M7-3G0zWzgf.js} +1 -1
- package/dist/public/assets/{railroad-ebnf-DWYD2UWJ-JO6n_1TK.js → railroad-ebnf-DWYD2UWJ-B5XRDRWS.js} +1 -1
- package/dist/public/assets/{railroad-peg-JR7BVNR7-Bf0PG2YJ.js → railroad-peg-JR7BVNR7-rcIioIyH.js} +1 -1
- package/dist/public/assets/{railroadDiagram-MRQ6NCDL-BMjog84X.js → railroadDiagram-MRQ6NCDL-CDM2ahQM.js} +1 -1
- package/dist/public/assets/{razor-DU-x8qSP.js → razor-CStezNUn.js} +1 -1
- package/dist/public/assets/{requirementDiagram-Y7LWUY3S-CktbbWjr.js → requirementDiagram-Y7LWUY3S-hbb6WtfM.js} +1 -1
- package/dist/public/assets/{sankeyDiagram-WGJFYUXA-CQonOre7.js → sankeyDiagram-WGJFYUXA-DLV3Aoae.js} +1 -1
- package/dist/public/assets/{sequenceDiagram-5NDRBG5M-CAe8-JvA.js → sequenceDiagram-5NDRBG5M-BWxOV5U9.js} +1 -1
- package/dist/public/assets/{stateDiagram-v2-FJ4FJNEA-BrkqOuRm.js → stateDiagram-v2-FJ4FJNEA-C-TaUPaQ.js} +1 -1
- package/dist/public/assets/{swimlanes-UMQ2AFYF-sLpgHBKo.js → swimlanes-UMQ2AFYF-CJY0BFmc.js} +1 -1
- package/dist/public/assets/swimlanesDiagram-OMPLPG4O-DdFDl6aK.js +8 -0
- package/dist/public/assets/{timeline-definition-SEUHKTQE-CHE0x-WN.js → timeline-definition-SEUHKTQE-CUfQb8Gv.js} +1 -1
- package/dist/public/assets/treeView-D4JQ5SDB-BQ4uUy4s.js +1 -0
- package/dist/public/assets/treemap-SAJKECNS-BFTTkjF9.js +1 -0
- package/dist/public/assets/{typescript-BrNT2oWW.js → typescript-B7kmi8MF.js} +1 -1
- package/dist/public/assets/{usecaseDiagram-AFA6YXN4-DwkFRI5R.js → usecaseDiagram-AFA6YXN4-BKTVAp-G.js} +1 -1
- package/dist/public/assets/{vennDiagram-NNMJR3RB-BquQVmJq.js → vennDiagram-NNMJR3RB-CN1RxNTS.js} +1 -1
- package/dist/public/assets/wardley-7MLQ67FV-BbyzsA8S.js +1 -0
- package/dist/public/assets/{wardleyDiagram-S7YFUSEH-sNYFUfMe.js → wardleyDiagram-S7YFUSEH-JJ6qcpu1.js} +1 -1
- package/dist/public/assets/{xml-BY65QKv7.js → xml-DVamlHEU.js} +1 -1
- package/dist/public/assets/{xychartDiagram-GGNKY7IX-CnWEYuid.js → xychartDiagram-GGNKY7IX-BNakYgUZ.js} +1 -1
- package/dist/public/assets/{yaml-BMovUwzO.js → yaml-BihGeFrM.js} +1 -1
- package/dist/public/index.html +1 -1
- package/dist/serve/index.mjs +5965 -5227
- package/package.json +39 -39
- package/dist/public/assets/cynefin-EF2NZ3EQ-CP12ndt7.js +0 -1
- package/dist/public/assets/flowDiagram-MALGAFMO-Djxeacjw.js +0 -1
- package/dist/public/assets/gitGraph-VSP46ZUC-qlWN7ijl.js +0 -1
- package/dist/public/assets/info-OHQRW6UA-jN69V28z.js +0 -1
- package/dist/public/assets/module-BEbKICB1.js +0 -2475
- package/dist/public/assets/module-Egyx6Otv.js +0 -1
- package/dist/public/assets/packet-JDAUHWVQ-Cl8QuXQJ.js +0 -1
- package/dist/public/assets/pie-XZMESJXO-kVGJsGZe.js +0 -1
- package/dist/public/assets/radar-ABXABTNO-Dw71L-n8.js +0 -1
- package/dist/public/assets/railroad-I3PHUGI6-DkaUvI4_.js +0 -1
- package/dist/public/assets/swimlanesDiagram-OMPLPG4O-amBdr5Wc.js +0 -8
- package/dist/public/assets/treeView-D4JQ5SDB-DKLyFqZk.js +0 -1
- package/dist/public/assets/treemap-SAJKECNS-CRG6ogj8.js +0 -1
- package/dist/public/assets/wardley-7MLQ67FV-D2jQfflX.js +0 -1
package/dist/gateway.mjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
// ../../ta-packages/ta-gateway/esm/node/gateway-cli.js
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
|
-
import { join as
|
|
3
|
+
import { join as join10 } from "node:path";
|
|
4
4
|
import { parseArgs } from "node:util";
|
|
5
5
|
|
|
6
6
|
// ../../ta-packages/ta-gateway/esm/node/gateway-server.js
|
|
7
|
-
import { createReadStream, existsSync as
|
|
7
|
+
import { createReadStream, existsSync as existsSync4, mkdirSync as mkdirSync5, statSync } from "node:fs";
|
|
8
8
|
import { createServer as createServer2, request as httpRequest } from "node:http";
|
|
9
9
|
import { request as httpsRequest } from "node:https";
|
|
10
|
-
import { extname, join as
|
|
10
|
+
import { extname, join as join9, normalize, sep } from "node:path";
|
|
11
11
|
import { pipeline } from "node:stream";
|
|
12
12
|
|
|
13
13
|
// ../../packages/rpc/esm/node/websocket/websocket-connection.js
|
|
@@ -810,15 +810,47 @@ var TaLanguageUtil;
|
|
|
810
810
|
var TaLocation;
|
|
811
811
|
(function(TaLocation2) {
|
|
812
812
|
TaLocation2.SSH = "ssh://";
|
|
813
|
+
TaLocation2.CLOUD = "ta://";
|
|
814
|
+
const CLOUD_NAME = /^[a-z0-9][a-z0-9-]{0,39}$/;
|
|
813
815
|
const HOST = /^(?!-)[\w.@:%+[\]-]+$/;
|
|
814
816
|
function isRemote(location) {
|
|
815
|
-
return location.startsWith(TaLocation2.SSH);
|
|
817
|
+
return location.startsWith(TaLocation2.SSH) || location.startsWith(TaLocation2.CLOUD);
|
|
816
818
|
}
|
|
817
819
|
TaLocation2.isRemote = isRemote;
|
|
820
|
+
function isCloud(host2) {
|
|
821
|
+
return !!host2 && host2.startsWith(TaLocation2.CLOUD);
|
|
822
|
+
}
|
|
823
|
+
TaLocation2.isCloud = isCloud;
|
|
824
|
+
function isCloudName(name) {
|
|
825
|
+
return CLOUD_NAME.test(name);
|
|
826
|
+
}
|
|
827
|
+
TaLocation2.isCloudName = isCloudName;
|
|
828
|
+
function cloud(name, path2 = "") {
|
|
829
|
+
return `${TaLocation2.CLOUD}${name}${path2 ? normalize(path2) : ""}`;
|
|
830
|
+
}
|
|
831
|
+
TaLocation2.cloud = cloud;
|
|
832
|
+
function cloudName(hostOrLocation) {
|
|
833
|
+
if (!hostOrLocation.startsWith(TaLocation2.CLOUD)) {
|
|
834
|
+
return;
|
|
835
|
+
}
|
|
836
|
+
const rest = hostOrLocation.slice(TaLocation2.CLOUD.length);
|
|
837
|
+
const slash = rest.indexOf("/");
|
|
838
|
+
return slash < 0 ? rest : rest.slice(0, slash);
|
|
839
|
+
}
|
|
840
|
+
TaLocation2.cloudName = cloudName;
|
|
818
841
|
function parse(location) {
|
|
842
|
+
var _a;
|
|
819
843
|
if (!isRemote(location)) {
|
|
820
844
|
return;
|
|
821
845
|
}
|
|
846
|
+
if (location.startsWith(TaLocation2.CLOUD)) {
|
|
847
|
+
const name = (_a = cloudName(location)) !== null && _a !== undefined ? _a : "";
|
|
848
|
+
if (!isCloudName(name)) {
|
|
849
|
+
throw new Error(`${JSON.stringify(location)} names no cloud workspace: write ta://<name>/<path>.`);
|
|
850
|
+
}
|
|
851
|
+
const path3 = location.slice(TaLocation2.CLOUD.length + name.length);
|
|
852
|
+
return { host: `${TaLocation2.CLOUD}${name}`, path: path3 ? normalize(path3) : "" };
|
|
853
|
+
}
|
|
822
854
|
const rest = location.slice(TaLocation2.SSH.length);
|
|
823
855
|
const slash = rest.indexOf("/");
|
|
824
856
|
const host2 = slash < 0 ? rest : rest.slice(0, slash);
|
|
@@ -830,7 +862,7 @@ var TaLocation;
|
|
|
830
862
|
}
|
|
831
863
|
TaLocation2.parse = parse;
|
|
832
864
|
function isHost(host2) {
|
|
833
|
-
return HOST.test(host2);
|
|
865
|
+
return isCloud(host2) ? isCloudName(host2.slice(TaLocation2.CLOUD.length)) : HOST.test(host2);
|
|
834
866
|
}
|
|
835
867
|
TaLocation2.isHost = isHost;
|
|
836
868
|
const FLAGS = /^-[46AaCfGgKkMNnqsTtVvXxYy]+$/;
|
|
@@ -894,10 +926,13 @@ var TaLocation;
|
|
|
894
926
|
}
|
|
895
927
|
TaLocation2.readHost = readHost;
|
|
896
928
|
function ssh(host2, path2 = "") {
|
|
897
|
-
return `${TaLocation2.SSH}${host2}${path2 ? normalize(path2) : ""}`;
|
|
929
|
+
return isCloud(host2) ? `${host2}${path2 ? normalize(path2) : ""}` : `${TaLocation2.SSH}${host2}${path2 ? normalize(path2) : ""}`;
|
|
898
930
|
}
|
|
899
931
|
TaLocation2.ssh = ssh;
|
|
900
932
|
function hostLabel(host2) {
|
|
933
|
+
if (isCloud(host2)) {
|
|
934
|
+
return host2.slice(TaLocation2.CLOUD.length);
|
|
935
|
+
}
|
|
901
936
|
const name = host2.replace(/^[^@]*@/, "").replace(/^\[(.*)\](?::\d+)?$/, "$1").replace(/^([^:]*):\d+$/, "$1");
|
|
902
937
|
return /^[\d.]+$/.test(name) || name.includes(":") ? name : name.split(".")[0] || name;
|
|
903
938
|
}
|
|
@@ -1003,6 +1038,7 @@ var TaErrorCode = {
|
|
|
1003
1038
|
CANCELLED: "CELL_TA_CANCELLED",
|
|
1004
1039
|
NOT_SIGNED_IN: "CELL_TA_NOT_SIGNED_IN",
|
|
1005
1040
|
ENVIRONMENT_FAILED: "CELL_TA_ENVIRONMENT_FAILED",
|
|
1041
|
+
RATE_LIMITED: "CELL_TA_RATE_LIMITED",
|
|
1006
1042
|
FORBIDDEN: "CELL_TA_FORBIDDEN",
|
|
1007
1043
|
LIMIT: "CELL_TA_LIMIT",
|
|
1008
1044
|
CONNECTION_LOST: "CELL_TA_CONNECTION_LOST"
|
|
@@ -1449,6 +1485,8 @@ var TaActionUtil;
|
|
|
1449
1485
|
}
|
|
1450
1486
|
TaActionUtil2.inside = inside;
|
|
1451
1487
|
})(TaActionUtil || (TaActionUtil = {}));
|
|
1488
|
+
// ../../ta-packages/ta-protocol/esm/common/usage-protocol.js
|
|
1489
|
+
var TaUsageServer = Symbol("TaUsageServer");
|
|
1452
1490
|
// ../../ta-packages/ta-protocol/esm/common/workspace-protocol.js
|
|
1453
1491
|
var TA_FILES_PATH = "/api/files";
|
|
1454
1492
|
var TaWorkspaceServer = Symbol("TaWorkspaceServer");
|
|
@@ -2169,12 +2207,24 @@ class CommandSandbox {
|
|
|
2169
2207
|
this.id = id;
|
|
2170
2208
|
this.type = "command";
|
|
2171
2209
|
this.name = SandboxProviders.string(config, "name", id);
|
|
2210
|
+
this.gatewayUrl = SandboxProviders.string(config, "gatewayUrl");
|
|
2172
2211
|
const start = SandboxProviders.string(config, "start");
|
|
2173
2212
|
if (!start) {
|
|
2174
2213
|
throw new Error(`The provider "${id}" of the type command needs "start", a command that prints the address of the Ta service.`);
|
|
2175
2214
|
}
|
|
2176
|
-
this.commands = {
|
|
2215
|
+
this.commands = {
|
|
2216
|
+
start,
|
|
2217
|
+
stop: SandboxProviders.string(config, "stop"),
|
|
2218
|
+
state: SandboxProviders.string(config, "state"),
|
|
2219
|
+
logs: SandboxProviders.string(config, "logs")
|
|
2220
|
+
};
|
|
2177
2221
|
this.env = SandboxProviders.env(config);
|
|
2222
|
+
if (!this.commands.logs) {
|
|
2223
|
+
this.logs = undefined;
|
|
2224
|
+
}
|
|
2225
|
+
}
|
|
2226
|
+
logs(tenant, lines) {
|
|
2227
|
+
return this.run(this.commands.logs, tenant, { TA_LOG_LINES: String(lines) });
|
|
2178
2228
|
}
|
|
2179
2229
|
async start(tenant) {
|
|
2180
2230
|
const stdout = await this.run(this.commands.start, tenant);
|
|
@@ -2196,10 +2246,18 @@ class CommandSandbox {
|
|
|
2196
2246
|
const word = (await this.run(this.commands.state, tenant)).trim().split(/\s+/).pop();
|
|
2197
2247
|
return word === "running" || word === "stopped" ? word : "missing";
|
|
2198
2248
|
}
|
|
2199
|
-
run(command, tenant) {
|
|
2249
|
+
run(command, tenant, extra = {}) {
|
|
2200
2250
|
return new Promise((resolve, reject) => {
|
|
2201
2251
|
const child = spawn("sh", ["-c", command], {
|
|
2202
|
-
env: {
|
|
2252
|
+
env: {
|
|
2253
|
+
...process.env,
|
|
2254
|
+
...this.env,
|
|
2255
|
+
...tenant.env,
|
|
2256
|
+
TA_TENANT_ID: tenant.id,
|
|
2257
|
+
TA_TENANT_ACCOUNT: tenant.account,
|
|
2258
|
+
TA_SERVE_TOKEN: tenant.token,
|
|
2259
|
+
...extra
|
|
2260
|
+
},
|
|
2203
2261
|
stdio: ["ignore", "pipe", "pipe"]
|
|
2204
2262
|
});
|
|
2205
2263
|
let stdout = "";
|
|
@@ -2245,7 +2303,16 @@ class DockerEngine {
|
|
|
2245
2303
|
throw new DockerError(`${host} is not a Docker host: write unix://<socket> or tcp://<host>:<port>.`, 0);
|
|
2246
2304
|
}
|
|
2247
2305
|
}
|
|
2248
|
-
call(method, path, body, timeoutMs = 120000) {
|
|
2306
|
+
async call(method, path, body, timeoutMs = 120000) {
|
|
2307
|
+
const answer = await this.raw(method, path, body, timeoutMs);
|
|
2308
|
+
const text = answer.body.toString("utf8");
|
|
2309
|
+
let parsed = text;
|
|
2310
|
+
try {
|
|
2311
|
+
parsed = text ? JSON.parse(text) : undefined;
|
|
2312
|
+
} catch (_a) {}
|
|
2313
|
+
return { status: answer.status, body: parsed };
|
|
2314
|
+
}
|
|
2315
|
+
raw(method, path, body, timeoutMs = 120000) {
|
|
2249
2316
|
return new Promise((resolve, reject) => {
|
|
2250
2317
|
const payload = body === undefined ? undefined : JSON.stringify(body);
|
|
2251
2318
|
const req = request({
|
|
@@ -2259,17 +2326,17 @@ class DockerEngine {
|
|
|
2259
2326
|
res.on("data", (chunk) => chunks.push(chunk));
|
|
2260
2327
|
res.on("end", () => {
|
|
2261
2328
|
var _a, _b;
|
|
2262
|
-
const
|
|
2263
|
-
let parsed = text;
|
|
2264
|
-
try {
|
|
2265
|
-
parsed = text ? JSON.parse(text) : undefined;
|
|
2266
|
-
} catch (_c) {}
|
|
2329
|
+
const buffer = Buffer.concat(chunks);
|
|
2267
2330
|
const status = (_a = res.statusCode) !== null && _a !== undefined ? _a : 0;
|
|
2268
2331
|
if (status >= 400) {
|
|
2269
|
-
const
|
|
2332
|
+
const text = buffer.toString("utf8");
|
|
2333
|
+
let message = text;
|
|
2334
|
+
try {
|
|
2335
|
+
message = (_b = JSON.parse(text).message) !== null && _b !== undefined ? _b : text;
|
|
2336
|
+
} catch (_c) {}
|
|
2270
2337
|
reject(new DockerError(`Docker: ${method} ${path.split("?")[0]} answered ${status}: ${message}`, status));
|
|
2271
2338
|
} else {
|
|
2272
|
-
resolve({ status, body:
|
|
2339
|
+
resolve({ status, body: buffer });
|
|
2273
2340
|
}
|
|
2274
2341
|
});
|
|
2275
2342
|
});
|
|
@@ -2303,6 +2370,37 @@ class DockerEngine {
|
|
|
2303
2370
|
}
|
|
2304
2371
|
}
|
|
2305
2372
|
}
|
|
2373
|
+
async stats(name) {
|
|
2374
|
+
try {
|
|
2375
|
+
const { body } = await this.call("GET", `/containers/${encodeURIComponent(name)}/stats?stream=false`, undefined, 15000);
|
|
2376
|
+
return DockerEngineUtil.usage(body);
|
|
2377
|
+
} catch (error) {
|
|
2378
|
+
if (error instanceof DockerError && (error.status === 404 || error.status === 409)) {
|
|
2379
|
+
return;
|
|
2380
|
+
}
|
|
2381
|
+
throw error;
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
async logs(name, lines) {
|
|
2385
|
+
try {
|
|
2386
|
+
const { body } = await this.raw("GET", `/containers/${encodeURIComponent(name)}/logs?stdout=1&stderr=1&tail=${lines}`, undefined, 15000);
|
|
2387
|
+
return DockerEngineUtil.demux(body);
|
|
2388
|
+
} catch (error) {
|
|
2389
|
+
if (error instanceof DockerError && error.status === 404) {
|
|
2390
|
+
return "";
|
|
2391
|
+
}
|
|
2392
|
+
throw error;
|
|
2393
|
+
}
|
|
2394
|
+
}
|
|
2395
|
+
async remove(name) {
|
|
2396
|
+
try {
|
|
2397
|
+
await this.call("DELETE", `/containers/${encodeURIComponent(name)}?force=true`);
|
|
2398
|
+
} catch (error) {
|
|
2399
|
+
if (!(error instanceof DockerError && error.status === 404)) {
|
|
2400
|
+
throw error;
|
|
2401
|
+
}
|
|
2402
|
+
}
|
|
2403
|
+
}
|
|
2306
2404
|
async ensureNetwork(name, isolated) {
|
|
2307
2405
|
try {
|
|
2308
2406
|
await this.call("GET", `/networks/${encodeURIComponent(name)}`);
|
|
@@ -2330,6 +2428,36 @@ class DockerEngine {
|
|
|
2330
2428
|
}
|
|
2331
2429
|
var DockerEngineUtil;
|
|
2332
2430
|
(function(DockerEngineUtil2) {
|
|
2431
|
+
function usage(stats) {
|
|
2432
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
|
2433
|
+
var _r, _s, _t, _u, _v, _w;
|
|
2434
|
+
if (!((_a = stats === null || stats === undefined ? undefined : stats.memory_stats) === null || _a === undefined ? undefined : _a.usage) && !((_b = stats === null || stats === undefined ? undefined : stats.cpu_stats) === null || _b === undefined ? undefined : _b.system_cpu_usage)) {
|
|
2435
|
+
return;
|
|
2436
|
+
}
|
|
2437
|
+
const cpuDelta = ((_r = (_d = (_c = stats.cpu_stats) === null || _c === undefined ? undefined : _c.cpu_usage) === null || _d === undefined ? undefined : _d.total_usage) !== null && _r !== undefined ? _r : 0) - ((_s = (_f = (_e = stats.precpu_stats) === null || _e === undefined ? undefined : _e.cpu_usage) === null || _f === undefined ? undefined : _f.total_usage) !== null && _s !== undefined ? _s : 0);
|
|
2438
|
+
const systemDelta = ((_t = (_g = stats.cpu_stats) === null || _g === undefined ? undefined : _g.system_cpu_usage) !== null && _t !== undefined ? _t : 0) - ((_u = (_h = stats.precpu_stats) === null || _h === undefined ? undefined : _h.system_cpu_usage) !== null && _u !== undefined ? _u : 0);
|
|
2439
|
+
const cpus = ((_j = stats.cpu_stats) === null || _j === undefined ? undefined : _j.online_cpus) || 1;
|
|
2440
|
+
const cpu = systemDelta > 0 && cpuDelta >= 0 ? Math.round(cpuDelta / systemDelta * cpus * 1000) / 10 : undefined;
|
|
2441
|
+
const memory = (_k = stats.memory_stats) === null || _k === undefined ? undefined : _k.usage;
|
|
2442
|
+
const cache = (_w = (_v = (_m = (_l = stats.memory_stats) === null || _l === undefined ? undefined : _l.stats) === null || _m === undefined ? undefined : _m.inactive_file) !== null && _v !== undefined ? _v : (_p = (_o = stats.memory_stats) === null || _o === undefined ? undefined : _o.stats) === null || _p === undefined ? undefined : _p.cache) !== null && _w !== undefined ? _w : 0;
|
|
2443
|
+
return { cpu, memory: memory === undefined ? undefined : Math.max(0, memory - cache), memoryLimit: (_q = stats.memory_stats) === null || _q === undefined ? undefined : _q.limit };
|
|
2444
|
+
}
|
|
2445
|
+
DockerEngineUtil2.usage = usage;
|
|
2446
|
+
function demux(buffer) {
|
|
2447
|
+
const parts = [];
|
|
2448
|
+
let offset = 0;
|
|
2449
|
+
while (offset + 8 <= buffer.length) {
|
|
2450
|
+
const stream = buffer[offset];
|
|
2451
|
+
const size = buffer.readUInt32BE(offset + 4);
|
|
2452
|
+
if (stream > 2 || buffer[offset + 1] !== 0 || buffer[offset + 2] !== 0 || buffer[offset + 3] !== 0 || offset + 8 + size > buffer.length) {
|
|
2453
|
+
return buffer.toString("utf8");
|
|
2454
|
+
}
|
|
2455
|
+
parts.push(buffer.subarray(offset + 8, offset + 8 + size));
|
|
2456
|
+
offset += 8 + size;
|
|
2457
|
+
}
|
|
2458
|
+
return offset === buffer.length ? Buffer.concat(parts).toString("utf8") : buffer.toString("utf8");
|
|
2459
|
+
}
|
|
2460
|
+
DockerEngineUtil2.demux = demux;
|
|
2333
2461
|
function splitImage(image) {
|
|
2334
2462
|
const slash = image.lastIndexOf("/");
|
|
2335
2463
|
const colon = image.lastIndexOf(":");
|
|
@@ -2356,6 +2484,11 @@ var DockerEngineUtil;
|
|
|
2356
2484
|
// ../../ta-packages/ta-gateway/esm/node/docker-sandbox.js
|
|
2357
2485
|
var DockerSandboxUtil;
|
|
2358
2486
|
(function(DockerSandboxUtil2) {
|
|
2487
|
+
function hasEnv(have, want) {
|
|
2488
|
+
const set = new Set(have !== null && have !== undefined ? have : []);
|
|
2489
|
+
return Object.entries(want !== null && want !== undefined ? want : {}).every(([key, value]) => set.has(`${key}=${value}`));
|
|
2490
|
+
}
|
|
2491
|
+
DockerSandboxUtil2.hasEnv = hasEnv;
|
|
2359
2492
|
function hostname(account) {
|
|
2360
2493
|
const name = account.slice(account.indexOf(":") + 1).split("@")[0].toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 48).replace(/-+$/, "");
|
|
2361
2494
|
return name || "ta";
|
|
@@ -2393,12 +2526,17 @@ class DockerSandbox {
|
|
|
2393
2526
|
this.options = options;
|
|
2394
2527
|
this.type = "docker";
|
|
2395
2528
|
this.name = SandboxProviders.string(config, "name", "Docker");
|
|
2529
|
+
this.gatewayUrl = SandboxProviders.string(config, "gatewayUrl");
|
|
2396
2530
|
this.engine = engine !== null && engine !== undefined ? engine : new DockerEngine({ host: options.host });
|
|
2397
2531
|
}
|
|
2398
2532
|
async start(tenant) {
|
|
2399
|
-
var _a, _b, _c;
|
|
2533
|
+
var _a, _b, _c, _d;
|
|
2400
2534
|
const name = this.containerName(tenant);
|
|
2401
2535
|
let container = await this.engine.inspect(name);
|
|
2536
|
+
if (container && !DockerSandboxUtil.hasEnv((_a = container.Config) === null || _a === undefined ? undefined : _a.Env, tenant.env)) {
|
|
2537
|
+
await this.engine.remove(name);
|
|
2538
|
+
container = undefined;
|
|
2539
|
+
}
|
|
2402
2540
|
if (!container) {
|
|
2403
2541
|
await this.engine.ensureImage(this.options.image);
|
|
2404
2542
|
await this.engine.ensureNetwork(this.options.network, true);
|
|
@@ -2409,7 +2547,7 @@ class DockerSandbox {
|
|
|
2409
2547
|
await this.engine.start(name);
|
|
2410
2548
|
container = await this.engine.inspect(name);
|
|
2411
2549
|
}
|
|
2412
|
-
const port = (
|
|
2550
|
+
const port = (_d = (_c = (_b = container === null || container === undefined ? undefined : container.NetworkSettings.Ports) === null || _b === undefined ? undefined : _b[PORT]) === null || _c === undefined ? undefined : _c[0]) === null || _d === undefined ? undefined : _d.HostPort;
|
|
2413
2551
|
if (!port) {
|
|
2414
2552
|
throw new Error(`The container ${name} publishes no port for ${PORT}.`);
|
|
2415
2553
|
}
|
|
@@ -2422,16 +2560,23 @@ class DockerSandbox {
|
|
|
2422
2560
|
const container = await this.engine.inspect(this.containerName(tenant));
|
|
2423
2561
|
return !container ? "missing" : container.State.Running ? "running" : "stopped";
|
|
2424
2562
|
}
|
|
2563
|
+
usage(tenant) {
|
|
2564
|
+
return this.engine.stats(this.containerName(tenant));
|
|
2565
|
+
}
|
|
2566
|
+
logs(tenant, lines) {
|
|
2567
|
+
return this.engine.logs(this.containerName(tenant), lines);
|
|
2568
|
+
}
|
|
2425
2569
|
spec(tenant) {
|
|
2426
2570
|
const name = this.containerName(tenant);
|
|
2427
2571
|
return {
|
|
2428
2572
|
Image: this.options.image,
|
|
2429
2573
|
Hostname: DockerSandboxUtil.hostname(tenant.account),
|
|
2430
|
-
Env: [`TA_SERVE_TOKEN=${tenant.token}`, ...Object.entries(this.options.env).map(([key, value]) => `${key}=${value}`)],
|
|
2574
|
+
Env: [`TA_SERVE_TOKEN=${tenant.token}`, ...Object.entries({ ...this.options.env, ...tenant.env }).map(([key, value]) => `${key}=${value}`)],
|
|
2431
2575
|
Labels: { "cell.ta.tenant": tenant.id, "cell.ta.account": tenant.account },
|
|
2432
2576
|
ExposedPorts: { [PORT]: {} },
|
|
2433
2577
|
HostConfig: {
|
|
2434
2578
|
PortBindings: { [PORT]: [{ HostIp: this.options.bind, HostPort: "" }] },
|
|
2579
|
+
ExtraHosts: ["host.docker.internal:host-gateway"],
|
|
2435
2580
|
Mounts: [{ Type: "volume", Source: name, Target: "/home/ta" }],
|
|
2436
2581
|
NetworkMode: this.options.network,
|
|
2437
2582
|
Memory: this.options.memory,
|
|
@@ -2512,34 +2657,122 @@ var EnvironmentsUtil;
|
|
|
2512
2657
|
throw new EnvironmentError(`Nothing answered at ${url} within ${Math.round(timeoutMs / 1000)} seconds${last instanceof Error ? `: ${last.message}` : ""}.`);
|
|
2513
2658
|
}
|
|
2514
2659
|
EnvironmentsUtil2.waitReady = waitReady;
|
|
2515
|
-
function call(endpoint, method, params = [], timeoutMs = 1e4) {
|
|
2660
|
+
async function call(endpoint, method, params = [], timeoutMs = 1e4) {
|
|
2661
|
+
const [result] = await calls(endpoint, [[method, params]], timeoutMs);
|
|
2662
|
+
return result;
|
|
2663
|
+
}
|
|
2664
|
+
EnvironmentsUtil2.call = call;
|
|
2665
|
+
function calls(endpoint, list, timeoutMs = 1e4) {
|
|
2516
2666
|
return new Promise((resolve, reject) => {
|
|
2517
2667
|
const url = new URL(`${endpoint.url.replace(/^http/, "ws")}${TA_WEBSOCKET_PATH}`);
|
|
2518
2668
|
url.searchParams.set("token", endpoint.token);
|
|
2519
2669
|
const socket = new WebSocket(url);
|
|
2520
|
-
const
|
|
2521
|
-
|
|
2670
|
+
const results = new Array(list.length);
|
|
2671
|
+
let left = list.length;
|
|
2672
|
+
let done = false;
|
|
2673
|
+
const timer = setTimeout(() => {
|
|
2674
|
+
var _a;
|
|
2675
|
+
var _b;
|
|
2676
|
+
return finish(new EnvironmentError(`${(_b = (_a = list[0]) === null || _a === undefined ? undefined : _a[0]) !== null && _b !== undefined ? _b : "A call"} took too long`));
|
|
2677
|
+
}, timeoutMs);
|
|
2678
|
+
const finish = (error) => {
|
|
2679
|
+
if (done) {
|
|
2680
|
+
return;
|
|
2681
|
+
}
|
|
2682
|
+
done = true;
|
|
2522
2683
|
clearTimeout(timer);
|
|
2523
2684
|
socket.close();
|
|
2524
2685
|
if (error) {
|
|
2525
2686
|
reject(error);
|
|
2526
2687
|
} else {
|
|
2527
|
-
resolve(
|
|
2688
|
+
resolve(results);
|
|
2689
|
+
}
|
|
2690
|
+
};
|
|
2691
|
+
if (left === 0) {
|
|
2692
|
+
queueMicrotask(() => finish());
|
|
2693
|
+
}
|
|
2694
|
+
socket.onopen = () => {
|
|
2695
|
+
for (const [index, [method, params]] of list.entries()) {
|
|
2696
|
+
socket.send(JSON.stringify({ jsonrpc: "2.0", id: index + 1, method, params }));
|
|
2528
2697
|
}
|
|
2529
2698
|
};
|
|
2530
|
-
socket.onopen = () => socket.send(JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }));
|
|
2531
2699
|
socket.onerror = () => finish(new EnvironmentError(`Cannot reach ${endpoint.url}`));
|
|
2532
2700
|
socket.onmessage = (message) => {
|
|
2701
|
+
var _a;
|
|
2533
2702
|
const answer = JSON.parse(String(message.data));
|
|
2534
|
-
if (answer.id
|
|
2535
|
-
finish(answer.error ? new EnvironmentError(answer.error.message) : undefined, answer.result);
|
|
2536
|
-
} else if (answer.method !== undefined && answer.id !== undefined) {
|
|
2703
|
+
if (answer.method !== undefined && answer.id !== undefined) {
|
|
2537
2704
|
socket.send(JSON.stringify({ jsonrpc: "2.0", id: answer.id, result: null }));
|
|
2705
|
+
return;
|
|
2706
|
+
}
|
|
2707
|
+
const index = typeof answer.id === "number" ? answer.id - 1 : -1;
|
|
2708
|
+
if (index < 0 || index >= list.length || results[index] !== undefined) {
|
|
2709
|
+
return;
|
|
2710
|
+
}
|
|
2711
|
+
if (list.length === 1 && answer.error) {
|
|
2712
|
+
finish(new EnvironmentError(answer.error.message));
|
|
2713
|
+
return;
|
|
2714
|
+
}
|
|
2715
|
+
results[index] = answer.error ? new EnvironmentError(answer.error.message) : (_a = answer.result) !== null && _a !== undefined ? _a : null;
|
|
2716
|
+
if (--left === 0) {
|
|
2717
|
+
finish();
|
|
2538
2718
|
}
|
|
2539
2719
|
};
|
|
2540
2720
|
});
|
|
2541
2721
|
}
|
|
2542
|
-
EnvironmentsUtil2.
|
|
2722
|
+
EnvironmentsUtil2.calls = calls;
|
|
2723
|
+
function status(session) {
|
|
2724
|
+
return session.waiting ? "waiting" : session.isStreaming ? "working" : session.asking ? "asking" : "idle";
|
|
2725
|
+
}
|
|
2726
|
+
EnvironmentsUtil2.status = status;
|
|
2727
|
+
function summary(sessions) {
|
|
2728
|
+
const statuses = sessions.map(status);
|
|
2729
|
+
return {
|
|
2730
|
+
sessions: sessions.length,
|
|
2731
|
+
working: statuses.filter((each) => each === "working").length,
|
|
2732
|
+
waiting: statuses.filter((each) => each === "waiting" || each === "asking").length
|
|
2733
|
+
};
|
|
2734
|
+
}
|
|
2735
|
+
EnvironmentsUtil2.summary = summary;
|
|
2736
|
+
function stats(value) {
|
|
2737
|
+
var _a;
|
|
2738
|
+
var _b, _c, _d, _e, _f, _g, _h, _j;
|
|
2739
|
+
const given = value;
|
|
2740
|
+
if (!given || typeof given !== "object" || !given.tokens || typeof given.cost !== "number") {
|
|
2741
|
+
return;
|
|
2742
|
+
}
|
|
2743
|
+
const percent = (_a = given.contextUsage) === null || _a === undefined ? undefined : _a.percent;
|
|
2744
|
+
return {
|
|
2745
|
+
userMessages: (_b = given.userMessages) !== null && _b !== undefined ? _b : 0,
|
|
2746
|
+
assistantMessages: (_c = given.assistantMessages) !== null && _c !== undefined ? _c : 0,
|
|
2747
|
+
toolCalls: (_d = given.toolCalls) !== null && _d !== undefined ? _d : 0,
|
|
2748
|
+
tokens: {
|
|
2749
|
+
input: (_e = given.tokens.input) !== null && _e !== undefined ? _e : 0,
|
|
2750
|
+
output: (_f = given.tokens.output) !== null && _f !== undefined ? _f : 0,
|
|
2751
|
+
cacheRead: (_g = given.tokens.cacheRead) !== null && _g !== undefined ? _g : 0,
|
|
2752
|
+
cacheWrite: (_h = given.tokens.cacheWrite) !== null && _h !== undefined ? _h : 0,
|
|
2753
|
+
total: (_j = given.tokens.total) !== null && _j !== undefined ? _j : 0
|
|
2754
|
+
},
|
|
2755
|
+
cost: given.cost,
|
|
2756
|
+
context: typeof percent === "number" ? Math.round(percent) : undefined
|
|
2757
|
+
};
|
|
2758
|
+
}
|
|
2759
|
+
EnvironmentsUtil2.stats = stats;
|
|
2760
|
+
function agentSession(session, stats2) {
|
|
2761
|
+
return {
|
|
2762
|
+
sessionId: session.sessionId,
|
|
2763
|
+
name: session.sessionName || undefined,
|
|
2764
|
+
cwd: session.cwd,
|
|
2765
|
+
model: session.model,
|
|
2766
|
+
status: status(session),
|
|
2767
|
+
messageCount: session.messageCount,
|
|
2768
|
+
attached: session.attached,
|
|
2769
|
+
createdAt: session.createdAt,
|
|
2770
|
+
updatedAt: session.updatedAt,
|
|
2771
|
+
group: session.group,
|
|
2772
|
+
stats: stats2
|
|
2773
|
+
};
|
|
2774
|
+
}
|
|
2775
|
+
EnvironmentsUtil2.agentSession = agentSession;
|
|
2543
2776
|
function sandboxTenant(tenant) {
|
|
2544
2777
|
return { id: tenant.id, account: tenant.account, token: tenant.token };
|
|
2545
2778
|
}
|
|
@@ -2591,12 +2824,97 @@ class Environments {
|
|
|
2591
2824
|
url: own && provider instanceof TaSandbox ? provider.url : undefined
|
|
2592
2825
|
};
|
|
2593
2826
|
}
|
|
2827
|
+
async usage(tenant) {
|
|
2828
|
+
var _a;
|
|
2829
|
+
if (((_a = this.running.get(tenant.id)) === null || _a === undefined ? undefined : _a.status) !== "running") {
|
|
2830
|
+
return;
|
|
2831
|
+
}
|
|
2832
|
+
const { provider, own } = this.provider(tenant);
|
|
2833
|
+
if (own || !provider.usage) {
|
|
2834
|
+
return;
|
|
2835
|
+
}
|
|
2836
|
+
return provider.usage(EnvironmentsUtil.sandboxTenant(tenant)).catch(() => {
|
|
2837
|
+
return;
|
|
2838
|
+
});
|
|
2839
|
+
}
|
|
2840
|
+
async logs(tenant, lines) {
|
|
2841
|
+
const { provider, own } = this.provider(tenant);
|
|
2842
|
+
if (own || !provider.logs) {
|
|
2843
|
+
throw new EnvironmentError(`The sandboxes of ${own ? "a Ta service of the user" : provider.name} keep no logs for the gateway.`);
|
|
2844
|
+
}
|
|
2845
|
+
return provider.logs(EnvironmentsUtil.sandboxTenant(tenant), lines);
|
|
2846
|
+
}
|
|
2847
|
+
watched(tenant) {
|
|
2848
|
+
const running = this.running.get(tenant.id);
|
|
2849
|
+
return (running === null || running === undefined ? undefined : running.status) === "running" && running.endpoint && !this.provider(tenant).own ? running.endpoint : undefined;
|
|
2850
|
+
}
|
|
2851
|
+
runningIds() {
|
|
2852
|
+
return [...this.running.entries()].filter(([id, running]) => {
|
|
2853
|
+
const tenant = running.status === "running" ? this.tenants.get(id) : undefined;
|
|
2854
|
+
return tenant !== undefined && !this.provider(tenant).own;
|
|
2855
|
+
}).map(([id]) => id);
|
|
2856
|
+
}
|
|
2857
|
+
usageReader(tenant) {
|
|
2858
|
+
const endpoint = this.watched(tenant);
|
|
2859
|
+
return endpoint && ((since) => EnvironmentsUtil.call(endpoint, "TaUsageServer/getUsage", [since], 20000));
|
|
2860
|
+
}
|
|
2861
|
+
async activity(tenant, sessionId) {
|
|
2862
|
+
const endpoint = this.watched(tenant);
|
|
2863
|
+
if (!endpoint) {
|
|
2864
|
+
throw new EnvironmentError("Its sandbox does not run: start it to see what its agents did.");
|
|
2865
|
+
}
|
|
2866
|
+
return EnvironmentsUtil.call(endpoint, "TaUsageServer/getActivity", [sessionId, 50]);
|
|
2867
|
+
}
|
|
2868
|
+
async agentSummary(tenant, timeoutMs = 3000) {
|
|
2869
|
+
const endpoint = this.watched(tenant);
|
|
2870
|
+
if (!endpoint) {
|
|
2871
|
+
return;
|
|
2872
|
+
}
|
|
2873
|
+
const sessions = await EnvironmentsUtil.call(endpoint, "TaServer/listSessions", [], timeoutMs).catch(() => {
|
|
2874
|
+
return;
|
|
2875
|
+
});
|
|
2876
|
+
return sessions && EnvironmentsUtil.summary(sessions);
|
|
2877
|
+
}
|
|
2878
|
+
async agents(tenant, now = Date.now()) {
|
|
2879
|
+
const endpoint = this.watched(tenant);
|
|
2880
|
+
if (!endpoint) {
|
|
2881
|
+
throw new EnvironmentError(this.provider(tenant).own ? "The Ta service of the user is theirs: the gateway does not watch it." : "Its sandbox does not run: start it to see its agents.");
|
|
2882
|
+
}
|
|
2883
|
+
const [sessions] = await EnvironmentsUtil.calls(endpoint, [["TaServer/listSessions", []]]);
|
|
2884
|
+
const stats = await EnvironmentsUtil.calls(endpoint, sessions.map((session) => ["TaServer/getSessionStats", [session.sessionId]])).catch(() => sessions.map(() => {
|
|
2885
|
+
return;
|
|
2886
|
+
}));
|
|
2887
|
+
const list = sessions.map((session, index) => EnvironmentsUtil.agentSession(session, EnvironmentsUtil.stats(stats[index]))).sort((a, b) => b.updatedAt - a.updatedAt);
|
|
2888
|
+
return {
|
|
2889
|
+
sessions: list,
|
|
2890
|
+
totals: {
|
|
2891
|
+
...EnvironmentsUtil.summary(sessions),
|
|
2892
|
+
toolCalls: list.reduce((sum, each) => {
|
|
2893
|
+
var _a;
|
|
2894
|
+
var _b;
|
|
2895
|
+
return sum + ((_b = (_a = each.stats) === null || _a === undefined ? undefined : _a.toolCalls) !== null && _b !== undefined ? _b : 0);
|
|
2896
|
+
}, 0),
|
|
2897
|
+
tokens: list.reduce((sum, each) => {
|
|
2898
|
+
var _a;
|
|
2899
|
+
var _b;
|
|
2900
|
+
return sum + ((_b = (_a = each.stats) === null || _a === undefined ? undefined : _a.tokens.total) !== null && _b !== undefined ? _b : 0);
|
|
2901
|
+
}, 0),
|
|
2902
|
+
cost: list.reduce((sum, each) => {
|
|
2903
|
+
var _a;
|
|
2904
|
+
var _b;
|
|
2905
|
+
return sum + ((_b = (_a = each.stats) === null || _a === undefined ? undefined : _a.cost) !== null && _b !== undefined ? _b : 0);
|
|
2906
|
+
}, 0)
|
|
2907
|
+
},
|
|
2908
|
+
readAt: now
|
|
2909
|
+
};
|
|
2910
|
+
}
|
|
2594
2911
|
connections(tenantId) {
|
|
2595
2912
|
var _a;
|
|
2596
2913
|
var _b;
|
|
2597
2914
|
return (_b = (_a = this.running.get(tenantId)) === null || _a === undefined ? undefined : _a.connections) !== null && _b !== undefined ? _b : 0;
|
|
2598
2915
|
}
|
|
2599
2916
|
ensure(tenant) {
|
|
2917
|
+
var _a;
|
|
2600
2918
|
const running = this.state(tenant.id);
|
|
2601
2919
|
if (running.status === "running" && running.endpoint) {
|
|
2602
2920
|
return Promise.resolve(running.endpoint);
|
|
@@ -2604,13 +2922,20 @@ class Environments {
|
|
|
2604
2922
|
if (running.starting) {
|
|
2605
2923
|
return running.starting;
|
|
2606
2924
|
}
|
|
2925
|
+
const blocked = (_a = this.guard) === null || _a === undefined ? undefined : _a.call(this, tenant);
|
|
2926
|
+
if (blocked) {
|
|
2927
|
+
Object.assign(running, { status: "failed", error: blocked });
|
|
2928
|
+
return Promise.reject(new EnvironmentError(blocked));
|
|
2929
|
+
}
|
|
2607
2930
|
running.status = "starting";
|
|
2608
2931
|
running.error = undefined;
|
|
2609
2932
|
const starting = (async () => {
|
|
2610
|
-
var
|
|
2611
|
-
|
|
2612
|
-
const
|
|
2613
|
-
|
|
2933
|
+
var _a2;
|
|
2934
|
+
var _b;
|
|
2935
|
+
const { provider, own } = this.provider(tenant);
|
|
2936
|
+
const env = own ? undefined : (_a2 = this.envOf) === null || _a2 === undefined ? undefined : _a2.call(this, tenant, provider);
|
|
2937
|
+
const endpoint = await provider.start({ ...EnvironmentsUtil.sandboxTenant(tenant), ...env ? { env } : {} });
|
|
2938
|
+
await EnvironmentsUtil.waitReady(endpoint.url, (_b = this.settings.readyTimeoutMs) !== null && _b !== undefined ? _b : provider.readyTimeoutMs);
|
|
2614
2939
|
return endpoint;
|
|
2615
2940
|
})();
|
|
2616
2941
|
running.starting = starting;
|
|
@@ -2710,158 +3035,1503 @@ class Environments {
|
|
|
2710
3035
|
}
|
|
2711
3036
|
}
|
|
2712
3037
|
|
|
2713
|
-
// ../../ta-packages/ta-gateway/esm/node/gateway-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
var GatewayServiceUtil;
|
|
2722
|
-
(function(GatewayServiceUtil2) {
|
|
2723
|
-
GatewayServiceUtil2.SERVICES = ["TaGatewayServer", "TaAccessServer"];
|
|
2724
|
-
function owns(method) {
|
|
2725
|
-
return typeof method === "string" && GatewayServiceUtil2.SERVICES.includes(method.split("/")[0]);
|
|
2726
|
-
}
|
|
2727
|
-
GatewayServiceUtil2.owns = owns;
|
|
2728
|
-
function mayOwn(text) {
|
|
2729
|
-
return GatewayServiceUtil2.SERVICES.some((service) => text.includes(`"${service}/`));
|
|
2730
|
-
}
|
|
2731
|
-
GatewayServiceUtil2.mayOwn = mayOwn;
|
|
2732
|
-
function error(id, message, code, hint) {
|
|
2733
|
-
return JSON.stringify({ jsonrpc: "2.0", id, error: { code: TA_RPC_ERROR_CODE, message, data: { kind: "application", data: { code, hint } } } });
|
|
2734
|
-
}
|
|
2735
|
-
GatewayServiceUtil2.error = error;
|
|
2736
|
-
function result(id, result2) {
|
|
2737
|
-
return JSON.stringify({ jsonrpc: "2.0", id, result: result2 !== null && result2 !== undefined ? result2 : null });
|
|
3038
|
+
// ../../ta-packages/ta-gateway/esm/node/gateway-audit.js
|
|
3039
|
+
import { appendFileSync, chmodSync as chmodSync2, existsSync as existsSync2, readFileSync as readFileSync3 } from "node:fs";
|
|
3040
|
+
import { join as join2 } from "node:path";
|
|
3041
|
+
var TAIL_BYTES = 2 * 1024 * 1024;
|
|
3042
|
+
|
|
3043
|
+
class GatewayAudit {
|
|
3044
|
+
constructor(dir) {
|
|
3045
|
+
this.file = join2(dir, "audit.jsonl");
|
|
2738
3046
|
}
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
3047
|
+
add(entry, now = Date.now()) {
|
|
3048
|
+
const fresh = !existsSync2(this.file);
|
|
3049
|
+
appendFileSync(this.file, `${JSON.stringify({ at: now, ...entry })}
|
|
3050
|
+
`, { mode: 384 });
|
|
3051
|
+
if (fresh) {
|
|
3052
|
+
chmodSync2(this.file, 384);
|
|
3053
|
+
}
|
|
2742
3054
|
}
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
}
|
|
2750
|
-
return { provider: given.provider };
|
|
3055
|
+
list(limit) {
|
|
3056
|
+
let text;
|
|
3057
|
+
try {
|
|
3058
|
+
text = readFileSync3(this.file, "utf8");
|
|
3059
|
+
} catch (_a) {
|
|
3060
|
+
return [];
|
|
2751
3061
|
}
|
|
2752
|
-
if (
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
3062
|
+
if (text.length > TAIL_BYTES) {
|
|
3063
|
+
text = text.slice(text.indexOf(`
|
|
3064
|
+
`, text.length - TAIL_BYTES) + 1);
|
|
3065
|
+
}
|
|
3066
|
+
const entries = [];
|
|
3067
|
+
const lines = text.split(`
|
|
3068
|
+
`);
|
|
3069
|
+
for (let index = lines.length - 1;index >= 0 && entries.length < limit; index--) {
|
|
2757
3070
|
try {
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
}
|
|
2762
|
-
if (typeof given.token !== "string" || !given.token.trim()) {
|
|
2763
|
-
throw invalid("A Ta service of your own is reached with its access token.", "Copy it from ~/.ta/agent/serve.json there, or the link ta serve printed.");
|
|
2764
|
-
}
|
|
2765
|
-
return { type: "ta", url, token: given.token.trim(), name: typeof given.name === "string" && given.name.trim() ? given.name.trim() : undefined };
|
|
3071
|
+
if (lines[index]) {
|
|
3072
|
+
entries.push(JSON.parse(lines[index]));
|
|
3073
|
+
}
|
|
3074
|
+
} catch (_b) {}
|
|
2766
3075
|
}
|
|
2767
|
-
|
|
3076
|
+
return entries;
|
|
2768
3077
|
}
|
|
2769
|
-
|
|
2770
|
-
})(GatewayServiceUtil || (GatewayServiceUtil = {}));
|
|
3078
|
+
}
|
|
2771
3079
|
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
this.
|
|
3080
|
+
// ../../ta-packages/ta-gateway/esm/node/gateway-exec.js
|
|
3081
|
+
var MAX_QUEUED_BYTES = 16 * 1024 * 1024;
|
|
3082
|
+
|
|
3083
|
+
class GatewayExecProxy {
|
|
3084
|
+
constructor(outer, endpoint, release) {
|
|
3085
|
+
this.outer = outer;
|
|
3086
|
+
this.release = release;
|
|
3087
|
+
this.queued = [];
|
|
3088
|
+
this.queuedBytes = 0;
|
|
3089
|
+
this.closed = false;
|
|
3090
|
+
outer.onMessage((text) => this.send(text));
|
|
3091
|
+
outer.onClose(() => this.dispose());
|
|
3092
|
+
endpoint.then((found) => this.open(found), (error) => this.close(1011, `Your environment did not start: ${error.message}`.slice(0, 120)));
|
|
2778
3093
|
}
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
getGateway: async () => this.info(caller),
|
|
2783
|
-
setEnvironment: async (choice) => this.setEnvironment(caller, choice),
|
|
2784
|
-
restartEnvironment: async () => this.restart(caller),
|
|
2785
|
-
listTenants: async () => this.listTenants(caller),
|
|
2786
|
-
stopTenant: async (id) => this.stopTenant(caller, id)
|
|
2787
|
-
} : {
|
|
2788
|
-
getAccess: async () => this.access(caller),
|
|
2789
|
-
bindUrl: async (provider) => this.admin(caller, () => this.flow.bindUrl(String(provider))),
|
|
2790
|
-
allow: async (account, name2) => this.admin(caller, () => this.allow(account, name2)),
|
|
2791
|
-
dismiss: async (account) => this.admin(caller, () => this.flow.store.dismiss(String(account))),
|
|
2792
|
-
disallow: async (account) => this.admin(caller, () => this.disallow(String(account))),
|
|
2793
|
-
signOut: async (id) => this.signOut(caller, String(id))
|
|
2794
|
-
};
|
|
2795
|
-
const handler = new Map(Object.entries(handlers)).get(name);
|
|
2796
|
-
if (!handler) {
|
|
2797
|
-
throw new GatewayCallError(`${method} is not a method of the gateway.`, TaErrorCode.INVALID_ARGUMENT, "Call a method of TaGatewayServer or TaAccessServer.");
|
|
3094
|
+
open(endpoint) {
|
|
3095
|
+
if (this.closed) {
|
|
3096
|
+
return;
|
|
2798
3097
|
}
|
|
2799
|
-
|
|
3098
|
+
const url = new URL(`${endpoint.url.replace(/^http/, "ws")}/api/exec`);
|
|
3099
|
+
url.searchParams.set("token", endpoint.token);
|
|
3100
|
+
const inner = new WebSocket(url);
|
|
3101
|
+
this.inner = inner;
|
|
3102
|
+
inner.onopen = () => {
|
|
3103
|
+
for (const text of this.queued.splice(0)) {
|
|
3104
|
+
inner.send(text);
|
|
3105
|
+
}
|
|
3106
|
+
this.queuedBytes = 0;
|
|
3107
|
+
};
|
|
3108
|
+
inner.onmessage = (message) => this.outer.open && this.outer.send(String(message.data));
|
|
3109
|
+
inner.onclose = (event) => this.close(event.code === 1000 || event.code === 1001 ? 1001 : 1011, "The sandbox closed the connection.");
|
|
3110
|
+
inner.onerror = () => {
|
|
3111
|
+
return;
|
|
3112
|
+
};
|
|
2800
3113
|
}
|
|
2801
|
-
|
|
3114
|
+
send(text) {
|
|
2802
3115
|
var _a;
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
};
|
|
3116
|
+
if (((_a = this.inner) === null || _a === undefined ? undefined : _a.readyState) === WebSocket.OPEN) {
|
|
3117
|
+
this.inner.send(text);
|
|
3118
|
+
} else if (this.queuedBytes + text.length <= MAX_QUEUED_BYTES) {
|
|
3119
|
+
this.queued.push(text);
|
|
3120
|
+
this.queuedBytes += text.length;
|
|
3121
|
+
} else {
|
|
3122
|
+
this.close(1013, "The environment is still starting.");
|
|
3123
|
+
}
|
|
2812
3124
|
}
|
|
2813
|
-
|
|
2814
|
-
|
|
3125
|
+
close(code, reason) {
|
|
3126
|
+
if (this.outer.open) {
|
|
3127
|
+
this.outer.close(code, reason);
|
|
3128
|
+
}
|
|
3129
|
+
this.dispose();
|
|
2815
3130
|
}
|
|
2816
|
-
|
|
3131
|
+
dispose() {
|
|
2817
3132
|
var _a;
|
|
2818
|
-
|
|
2819
|
-
const tenant = (_a = this.tenants.get(caller.tenant.id)) !== null && _a !== undefined ? _a : caller.tenant;
|
|
2820
|
-
const next = await this.environments.change(tenant, choice);
|
|
2821
|
-
caller.tenant = next;
|
|
2822
|
-
this.hooks.reset(next.id);
|
|
2823
|
-
return this.info(caller);
|
|
2824
|
-
}
|
|
2825
|
-
async restart(caller) {
|
|
2826
|
-
await this.environments.stop(caller.tenant.id).catch(() => {
|
|
3133
|
+
if (this.closed) {
|
|
2827
3134
|
return;
|
|
2828
|
-
});
|
|
2829
|
-
this.hooks.reset(caller.tenant.id);
|
|
2830
|
-
return this.info(caller);
|
|
2831
|
-
}
|
|
2832
|
-
async listTenants(caller) {
|
|
2833
|
-
return this.admin(caller, () => this.tenants.list().map((tenant) => ({
|
|
2834
|
-
id: tenant.id,
|
|
2835
|
-
account: tenant.account,
|
|
2836
|
-
name: tenant.name,
|
|
2837
|
-
environment: this.environments.info(tenant),
|
|
2838
|
-
connections: this.environments.connections(tenant.id),
|
|
2839
|
-
createdAt: tenant.createdAt,
|
|
2840
|
-
lastActiveAt: tenant.lastActiveAt
|
|
2841
|
-
})));
|
|
2842
|
-
}
|
|
2843
|
-
async stopTenant(caller, id) {
|
|
2844
|
-
await this.admin(caller, () => this.environments.stop(String(id)));
|
|
2845
|
-
this.hooks.reset(String(id));
|
|
2846
|
-
}
|
|
2847
|
-
access(caller) {
|
|
2848
|
-
const providers = this.flow.providers.map(({ id, name }) => ({ id, name }));
|
|
2849
|
-
const info = this.flow.store.info(caller.token);
|
|
2850
|
-
if (caller.admin) {
|
|
2851
|
-
return { providers, canManage: true, ...info };
|
|
2852
3135
|
}
|
|
2853
|
-
|
|
3136
|
+
this.closed = true;
|
|
3137
|
+
this.release();
|
|
3138
|
+
(_a = this.inner) === null || _a === undefined || _a.close();
|
|
2854
3139
|
}
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
3140
|
+
}
|
|
3141
|
+
|
|
3142
|
+
// ../../ta-packages/ta-gateway/esm/node/gateway-policy.js
|
|
3143
|
+
import { readFileSync as readFileSync5 } from "node:fs";
|
|
3144
|
+
import { join as join4 } from "node:path";
|
|
3145
|
+
|
|
3146
|
+
// ../../ta-packages/ta-gateway/esm/node/tenant-store.js
|
|
3147
|
+
import { createHash as createHash4, randomBytes as randomBytes3 } from "node:crypto";
|
|
3148
|
+
import { chmodSync as chmodSync3, mkdirSync as mkdirSync2, readdirSync, readFileSync as readFileSync4, renameSync as renameSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
3149
|
+
import { join as join3 } from "node:path";
|
|
3150
|
+
function writePrivate(file, data) {
|
|
3151
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
3152
|
+
writeFileSync2(temporary, JSON.stringify(data, undefined, 2), { mode: 384 });
|
|
3153
|
+
chmodSync3(temporary, 384);
|
|
3154
|
+
renameSync2(temporary, file);
|
|
3155
|
+
}
|
|
3156
|
+
var TenantUtil;
|
|
3157
|
+
(function(TenantUtil2) {
|
|
3158
|
+
function id(account) {
|
|
3159
|
+
return createHash4("sha256").update(account).digest("hex").slice(0, 16);
|
|
3160
|
+
}
|
|
3161
|
+
TenantUtil2.id = id;
|
|
3162
|
+
})(TenantUtil || (TenantUtil = {}));
|
|
3163
|
+
|
|
3164
|
+
class TenantStore {
|
|
3165
|
+
constructor(root) {
|
|
3166
|
+
this.cache = new Map;
|
|
3167
|
+
this.dir = join3(root, "tenants");
|
|
3168
|
+
mkdirSync2(this.dir, { recursive: true, mode: 448 });
|
|
3169
|
+
}
|
|
3170
|
+
get(id) {
|
|
3171
|
+
if (!/^[0-9a-f]{16}$/.test(id)) {
|
|
3172
|
+
return;
|
|
3173
|
+
}
|
|
3174
|
+
let tenant = this.cache.get(id);
|
|
3175
|
+
if (!tenant) {
|
|
3176
|
+
try {
|
|
3177
|
+
tenant = JSON.parse(readFileSync4(join3(this.dir, `${id}.json`), "utf8"));
|
|
3178
|
+
this.cache.set(id, tenant);
|
|
3179
|
+
} catch (_a) {
|
|
3180
|
+
return;
|
|
3181
|
+
}
|
|
3182
|
+
}
|
|
3183
|
+
return tenant;
|
|
3184
|
+
}
|
|
3185
|
+
ensure(account, now = Date.now()) {
|
|
3186
|
+
const id = TenantUtil.id(account.id);
|
|
3187
|
+
const known = this.get(id);
|
|
3188
|
+
if (known) {
|
|
3189
|
+
if (account.name && (known.name !== account.name || known.avatar !== account.avatar)) {
|
|
3190
|
+
return this.update(id, { name: account.name, avatar: account.avatar });
|
|
3191
|
+
}
|
|
3192
|
+
return known;
|
|
3193
|
+
}
|
|
3194
|
+
const tenant = {
|
|
3195
|
+
id,
|
|
3196
|
+
account: account.id,
|
|
3197
|
+
name: account.name,
|
|
3198
|
+
avatar: account.avatar,
|
|
3199
|
+
token: randomBytes3(32).toString("base64url"),
|
|
3200
|
+
modelKey: `tam-${randomBytes3(24).toString("base64url")}`,
|
|
3201
|
+
createdAt: now
|
|
3202
|
+
};
|
|
3203
|
+
this.save(tenant);
|
|
3204
|
+
return tenant;
|
|
3205
|
+
}
|
|
3206
|
+
update(id, change) {
|
|
3207
|
+
const tenant = this.get(id);
|
|
3208
|
+
if (!tenant) {
|
|
3209
|
+
throw new Error(`No tenant ${id}`);
|
|
3210
|
+
}
|
|
3211
|
+
const next = { ...tenant, ...change };
|
|
3212
|
+
this.save(next);
|
|
3213
|
+
return next;
|
|
3214
|
+
}
|
|
3215
|
+
modelKey(id) {
|
|
3216
|
+
var _a;
|
|
3217
|
+
const tenant = this.get(id);
|
|
3218
|
+
if (!tenant) {
|
|
3219
|
+
throw new Error(`No tenant ${id}`);
|
|
3220
|
+
}
|
|
3221
|
+
return (_a = tenant.modelKey) !== null && _a !== undefined ? _a : this.update(id, { modelKey: `tam-${randomBytes3(24).toString("base64url")}` }).modelKey;
|
|
3222
|
+
}
|
|
3223
|
+
list() {
|
|
3224
|
+
return readdirSync(this.dir).filter((name) => /^[0-9a-f]{16}\.json$/.test(name)).map((name) => this.get(name.slice(0, -5))).filter((tenant) => tenant !== undefined);
|
|
3225
|
+
}
|
|
3226
|
+
save(tenant) {
|
|
3227
|
+
writePrivate(join3(this.dir, `${tenant.id}.json`), tenant);
|
|
3228
|
+
this.cache.set(tenant.id, tenant);
|
|
3229
|
+
}
|
|
3230
|
+
}
|
|
3231
|
+
|
|
3232
|
+
// ../../ta-packages/ta-gateway/esm/node/gateway-policy.js
|
|
3233
|
+
class PolicyError extends Error {
|
|
3234
|
+
}
|
|
3235
|
+
var GatewayPolicyUtil;
|
|
3236
|
+
(function(GatewayPolicyUtil2) {
|
|
3237
|
+
const NUMBERS = ["freeMinutes", "monthlyTokens", "promptsPerMinute", "connections"];
|
|
3238
|
+
const RANK = { member: 0, admin: 1, owner: 2 };
|
|
3239
|
+
function rank(role) {
|
|
3240
|
+
return RANK[role];
|
|
3241
|
+
}
|
|
3242
|
+
GatewayPolicyUtil2.rank = rank;
|
|
3243
|
+
function limits(value, providers, nullable) {
|
|
3244
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
3245
|
+
throw new PolicyError('Limits are an object, such as { "freeMinutes": 600 }.');
|
|
3246
|
+
}
|
|
3247
|
+
const given = value;
|
|
3248
|
+
const out = {};
|
|
3249
|
+
for (const key of Object.keys(given)) {
|
|
3250
|
+
if (![...NUMBERS, "providers", "own"].includes(key)) {
|
|
3251
|
+
throw new PolicyError(`"${key}" is no limit: those there are are ${[...NUMBERS, "providers", "own"].join(", ")}.`);
|
|
3252
|
+
}
|
|
3253
|
+
}
|
|
3254
|
+
for (const key of NUMBERS) {
|
|
3255
|
+
const item = given[key];
|
|
3256
|
+
if (item === undefined) {
|
|
3257
|
+
continue;
|
|
3258
|
+
}
|
|
3259
|
+
if (item === null) {
|
|
3260
|
+
if (nullable) {
|
|
3261
|
+
out[key] = null;
|
|
3262
|
+
}
|
|
3263
|
+
continue;
|
|
3264
|
+
}
|
|
3265
|
+
if (typeof item !== "number" || !Number.isInteger(item) || item < 0 || item > 1e7) {
|
|
3266
|
+
throw new PolicyError(`"${key}" is a whole number from 0, or null for none.`);
|
|
3267
|
+
}
|
|
3268
|
+
out[key] = item;
|
|
3269
|
+
}
|
|
3270
|
+
if (given.providers !== undefined) {
|
|
3271
|
+
if (given.providers === null) {
|
|
3272
|
+
if (nullable) {
|
|
3273
|
+
out.providers = null;
|
|
3274
|
+
}
|
|
3275
|
+
} else if (!Array.isArray(given.providers) || given.providers.some((id) => typeof id !== "string" || !providers.includes(id))) {
|
|
3276
|
+
throw new PolicyError(`"providers" lists providers of the gateway: ${providers.join(", ")}.`);
|
|
3277
|
+
} else {
|
|
3278
|
+
out.providers = [...new Set(given.providers)];
|
|
3279
|
+
}
|
|
3280
|
+
}
|
|
3281
|
+
if (given.own !== undefined) {
|
|
3282
|
+
if (given.own === null) {
|
|
3283
|
+
if (nullable) {
|
|
3284
|
+
out.own = null;
|
|
3285
|
+
}
|
|
3286
|
+
} else if (typeof given.own !== "boolean") {
|
|
3287
|
+
throw new PolicyError('"own" is true or false.');
|
|
3288
|
+
} else {
|
|
3289
|
+
out.own = given.own;
|
|
3290
|
+
}
|
|
3291
|
+
}
|
|
3292
|
+
return out;
|
|
3293
|
+
}
|
|
3294
|
+
GatewayPolicyUtil2.limits = limits;
|
|
3295
|
+
function resolve(defaults, own, ownAllowed) {
|
|
3296
|
+
const pick = (key) => own && own[key] !== undefined ? own[key] : defaults[key];
|
|
3297
|
+
const number = (key) => {
|
|
3298
|
+
const value = pick(key);
|
|
3299
|
+
return typeof value === "number" ? value : undefined;
|
|
3300
|
+
};
|
|
3301
|
+
const providers = pick("providers");
|
|
3302
|
+
const own_ = pick("own");
|
|
3303
|
+
return {
|
|
3304
|
+
freeMinutes: number("freeMinutes"),
|
|
3305
|
+
monthlyTokens: number("monthlyTokens"),
|
|
3306
|
+
promptsPerMinute: number("promptsPerMinute"),
|
|
3307
|
+
connections: number("connections"),
|
|
3308
|
+
providers: Array.isArray(providers) ? providers : undefined,
|
|
3309
|
+
own: ownAllowed && own_ !== false
|
|
3310
|
+
};
|
|
3311
|
+
}
|
|
3312
|
+
GatewayPolicyUtil2.resolve = resolve;
|
|
3313
|
+
})(GatewayPolicyUtil || (GatewayPolicyUtil = {}));
|
|
3314
|
+
|
|
3315
|
+
class GatewayPolicy {
|
|
3316
|
+
constructor(dir, owners) {
|
|
3317
|
+
this.owners = owners;
|
|
3318
|
+
this.file = join4(dir, "policy.json");
|
|
3319
|
+
this.stored = this.read();
|
|
3320
|
+
}
|
|
3321
|
+
role(account) {
|
|
3322
|
+
var _a;
|
|
3323
|
+
if (this.owners.some((pattern) => AccessUtil.matches(pattern, account))) {
|
|
3324
|
+
return "owner";
|
|
3325
|
+
}
|
|
3326
|
+
return ((_a = this.stored.admins) === null || _a === undefined ? undefined : _a.includes(account)) ? "admin" : "member";
|
|
3327
|
+
}
|
|
3328
|
+
lets(account) {
|
|
3329
|
+
return this.role(account) !== "member";
|
|
3330
|
+
}
|
|
3331
|
+
get defaults() {
|
|
3332
|
+
var _a;
|
|
3333
|
+
return (_a = this.stored.defaults) !== null && _a !== undefined ? _a : {};
|
|
3334
|
+
}
|
|
3335
|
+
setDefaults(limits) {
|
|
3336
|
+
this.stored = { ...this.stored, defaults: limits };
|
|
3337
|
+
writePrivate(this.file, this.stored);
|
|
3338
|
+
}
|
|
3339
|
+
setRole(account, role) {
|
|
3340
|
+
var _a;
|
|
3341
|
+
const admins = ((_a = this.stored.admins) !== null && _a !== undefined ? _a : []).filter((other) => other !== account);
|
|
3342
|
+
this.stored = { ...this.stored, admins: role === "admin" ? [...admins, account] : admins };
|
|
3343
|
+
writePrivate(this.file, this.stored);
|
|
3344
|
+
}
|
|
3345
|
+
view(idleMinutes) {
|
|
3346
|
+
var _a;
|
|
3347
|
+
return {
|
|
3348
|
+
defaults: this.defaults,
|
|
3349
|
+
admins: [
|
|
3350
|
+
...this.owners.map((account) => ({ account, role: "owner" })),
|
|
3351
|
+
...((_a = this.stored.admins) !== null && _a !== undefined ? _a : []).filter((account) => this.role(account) === "admin").map((account) => ({ account, role: "admin" }))
|
|
3352
|
+
],
|
|
3353
|
+
idleMinutes
|
|
3354
|
+
};
|
|
3355
|
+
}
|
|
3356
|
+
read() {
|
|
3357
|
+
try {
|
|
3358
|
+
const value = JSON.parse(readFileSync5(this.file, "utf8"));
|
|
3359
|
+
return value && typeof value === "object" ? value : {};
|
|
3360
|
+
} catch (_a) {
|
|
3361
|
+
return {};
|
|
3362
|
+
}
|
|
3363
|
+
}
|
|
3364
|
+
}
|
|
3365
|
+
|
|
3366
|
+
// ../../ta-packages/ta-gateway/esm/node/gateway-usage.js
|
|
3367
|
+
import { mkdirSync as mkdirSync3, readFileSync as readFileSync6 } from "node:fs";
|
|
3368
|
+
import { join as join5 } from "node:path";
|
|
3369
|
+
var HOUR = 3600000;
|
|
3370
|
+
var DAY = 24 * HOUR;
|
|
3371
|
+
var KEPT_DAYS = 32;
|
|
3372
|
+
var FRESH_MS = 60000;
|
|
3373
|
+
var GatewayUsageUtil;
|
|
3374
|
+
(function(GatewayUsageUtil2) {
|
|
3375
|
+
GatewayUsageUtil2.KEEP_MS = KEPT_DAYS * DAY;
|
|
3376
|
+
function month(now) {
|
|
3377
|
+
const date = new Date(now);
|
|
3378
|
+
const start = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1);
|
|
3379
|
+
const next = Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + 1, 1);
|
|
3380
|
+
return { key: new Date(start).toISOString().slice(0, 7), start, next };
|
|
3381
|
+
}
|
|
3382
|
+
GatewayUsageUtil2.month = month;
|
|
3383
|
+
function tokens(bucket) {
|
|
3384
|
+
return bucket.input + bucket.output + bucket.cacheRead + bucket.cacheWrite;
|
|
3385
|
+
}
|
|
3386
|
+
GatewayUsageUtil2.tokens = tokens;
|
|
3387
|
+
function trend(byTenant, since, stale, now) {
|
|
3388
|
+
var _a, _b;
|
|
3389
|
+
const hours = new Map;
|
|
3390
|
+
const models = new Map;
|
|
3391
|
+
const tenants = [];
|
|
3392
|
+
for (const [tenantId, buckets] of byTenant) {
|
|
3393
|
+
const share = { tenantId, tokens: 0, cost: 0, turns: 0 };
|
|
3394
|
+
for (const bucket of buckets) {
|
|
3395
|
+
if (bucket.at + HOUR <= since) {
|
|
3396
|
+
continue;
|
|
3397
|
+
}
|
|
3398
|
+
const count = tokens(bucket);
|
|
3399
|
+
const hour = (_a = hours.get(bucket.at)) !== null && _a !== undefined ? _a : { at: bucket.at, input: 0, output: 0, tokens: 0, cost: 0, turns: 0 };
|
|
3400
|
+
hour.input += bucket.input;
|
|
3401
|
+
hour.output += bucket.output;
|
|
3402
|
+
hour.tokens += count;
|
|
3403
|
+
hour.cost += bucket.cost;
|
|
3404
|
+
hour.turns += bucket.turns;
|
|
3405
|
+
hours.set(bucket.at, hour);
|
|
3406
|
+
const model = (_b = models.get(bucket.model)) !== null && _b !== undefined ? _b : { model: bucket.model, tokens: 0, cost: 0, turns: 0 };
|
|
3407
|
+
model.tokens += count;
|
|
3408
|
+
model.cost += bucket.cost;
|
|
3409
|
+
model.turns += bucket.turns;
|
|
3410
|
+
models.set(bucket.model, model);
|
|
3411
|
+
share.tokens += count;
|
|
3412
|
+
share.cost += bucket.cost;
|
|
3413
|
+
share.turns += bucket.turns;
|
|
3414
|
+
}
|
|
3415
|
+
if (share.turns > 0) {
|
|
3416
|
+
tenants.push(share);
|
|
3417
|
+
}
|
|
3418
|
+
}
|
|
3419
|
+
return {
|
|
3420
|
+
since,
|
|
3421
|
+
hours: [...hours.values()].sort((a, b) => a.at - b.at),
|
|
3422
|
+
byModel: [...models.values()].sort((a, b) => b.cost - a.cost || b.tokens - a.tokens),
|
|
3423
|
+
byTenant: tenants.sort((a, b) => b.cost - a.cost || b.tokens - a.tokens),
|
|
3424
|
+
stale,
|
|
3425
|
+
readAt: now
|
|
3426
|
+
};
|
|
3427
|
+
}
|
|
3428
|
+
GatewayUsageUtil2.trend = trend;
|
|
3429
|
+
})(GatewayUsageUtil || (GatewayUsageUtil = {}));
|
|
3430
|
+
|
|
3431
|
+
class GatewayUsage {
|
|
3432
|
+
constructor(root) {
|
|
3433
|
+
this.kept = new Map;
|
|
3434
|
+
this.reading = new Map;
|
|
3435
|
+
this.dir = join5(root, "usage");
|
|
3436
|
+
mkdirSync3(this.dir, { recursive: true, mode: 448 });
|
|
3437
|
+
}
|
|
3438
|
+
get(tenantId) {
|
|
3439
|
+
let kept = this.kept.get(tenantId);
|
|
3440
|
+
if (!kept && /^[0-9a-f]{16}$/.test(tenantId)) {
|
|
3441
|
+
try {
|
|
3442
|
+
kept = JSON.parse(readFileSync6(join5(this.dir, `${tenantId}.json`), "utf8"));
|
|
3443
|
+
this.kept.set(tenantId, kept);
|
|
3444
|
+
} catch (_a) {
|
|
3445
|
+
return;
|
|
3446
|
+
}
|
|
3447
|
+
}
|
|
3448
|
+
return kept;
|
|
3449
|
+
}
|
|
3450
|
+
async refresh(tenantId, read, now = Date.now(), maxAge = FRESH_MS) {
|
|
3451
|
+
const kept = this.get(tenantId);
|
|
3452
|
+
if (!read) {
|
|
3453
|
+
return false;
|
|
3454
|
+
}
|
|
3455
|
+
if (kept && now - kept.readAt < maxAge) {
|
|
3456
|
+
return true;
|
|
3457
|
+
}
|
|
3458
|
+
let reading = this.reading.get(tenantId);
|
|
3459
|
+
if (!reading) {
|
|
3460
|
+
reading = read(now - GatewayUsageUtil.KEEP_MS).then((buckets) => {
|
|
3461
|
+
const next = { readAt: now, buckets };
|
|
3462
|
+
this.kept.set(tenantId, next);
|
|
3463
|
+
writePrivate(join5(this.dir, `${tenantId}.json`), next);
|
|
3464
|
+
});
|
|
3465
|
+
this.reading.set(tenantId, reading);
|
|
3466
|
+
reading.finally(() => this.reading.delete(tenantId)).catch(() => {
|
|
3467
|
+
return;
|
|
3468
|
+
});
|
|
3469
|
+
}
|
|
3470
|
+
try {
|
|
3471
|
+
await reading;
|
|
3472
|
+
return true;
|
|
3473
|
+
} catch (_a) {
|
|
3474
|
+
return false;
|
|
3475
|
+
}
|
|
3476
|
+
}
|
|
3477
|
+
month(tenantId, now = Date.now()) {
|
|
3478
|
+
var _a;
|
|
3479
|
+
var _b;
|
|
3480
|
+
const { start } = GatewayUsageUtil.month(now);
|
|
3481
|
+
let tokens = 0;
|
|
3482
|
+
let cost = 0;
|
|
3483
|
+
for (const bucket of (_b = (_a = this.get(tenantId)) === null || _a === undefined ? undefined : _a.buckets) !== null && _b !== undefined ? _b : []) {
|
|
3484
|
+
if (bucket.at >= start) {
|
|
3485
|
+
tokens += GatewayUsageUtil.tokens(bucket);
|
|
3486
|
+
cost += bucket.cost;
|
|
3487
|
+
}
|
|
3488
|
+
}
|
|
3489
|
+
return { tokens, cost };
|
|
3490
|
+
}
|
|
3491
|
+
}
|
|
3492
|
+
|
|
3493
|
+
// ../../ta-packages/ta-gateway/esm/node/gateway-quotas.js
|
|
3494
|
+
var MINUTE = 60000;
|
|
3495
|
+
var MAX_TICK_MS = 2 * MINUTE;
|
|
3496
|
+
|
|
3497
|
+
class GatewayQuotas {
|
|
3498
|
+
constructor(policy, tenants, usage, ownAllowed, metered = () => false) {
|
|
3499
|
+
this.policy = policy;
|
|
3500
|
+
this.tenants = tenants;
|
|
3501
|
+
this.usage = usage;
|
|
3502
|
+
this.ownAllowed = ownAllowed;
|
|
3503
|
+
this.metered = metered;
|
|
3504
|
+
this.lastTick = Date.now();
|
|
3505
|
+
this.prompts = new Map;
|
|
3506
|
+
}
|
|
3507
|
+
role(tenant) {
|
|
3508
|
+
return this.policy.role(tenant.account);
|
|
3509
|
+
}
|
|
3510
|
+
limits(tenant) {
|
|
3511
|
+
if (this.role(tenant) === "owner") {
|
|
3512
|
+
return { own: this.ownAllowed() };
|
|
3513
|
+
}
|
|
3514
|
+
return GatewayPolicyUtil.resolve(this.policy.defaults, tenant.limits, this.ownAllowed());
|
|
3515
|
+
}
|
|
3516
|
+
meter(tenant, now = Date.now()) {
|
|
3517
|
+
var _a;
|
|
3518
|
+
const { key } = GatewayUsageUtil.month(now);
|
|
3519
|
+
return ((_a = tenant.meter) === null || _a === undefined ? undefined : _a.month) === key ? tenant.meter : { month: key, runMs: 0, bonusMinutes: 0 };
|
|
3520
|
+
}
|
|
3521
|
+
blocked(tenant, now = Date.now()) {
|
|
3522
|
+
if (tenant.suspended && this.role(tenant) !== "owner") {
|
|
3523
|
+
return "suspended";
|
|
3524
|
+
}
|
|
3525
|
+
const { freeMinutes } = this.limits(tenant);
|
|
3526
|
+
const meter = this.meter(tenant, now);
|
|
3527
|
+
return freeMinutes !== undefined && meter.runMs >= (freeMinutes + meter.bonusMinutes) * MINUTE ? "minutes" : undefined;
|
|
3528
|
+
}
|
|
3529
|
+
state(tenant, now = Date.now()) {
|
|
3530
|
+
var _a, _b;
|
|
3531
|
+
const { key, next } = GatewayUsageUtil.month(now);
|
|
3532
|
+
const meter = this.meter(tenant, now);
|
|
3533
|
+
const limits = this.limits(tenant);
|
|
3534
|
+
const metered = this.metered();
|
|
3535
|
+
const month = metered ? { tokens: (_a = meter.tokens) !== null && _a !== undefined ? _a : 0, cost: (_b = meter.cost) !== null && _b !== undefined ? _b : 0 } : this.usage.month(tenant.id, now);
|
|
3536
|
+
return {
|
|
3537
|
+
month: key,
|
|
3538
|
+
resetsAt: next,
|
|
3539
|
+
minutes: { used: Math.floor(meter.runMs / MINUTE), limit: limits.freeMinutes, bonus: meter.bonusMinutes },
|
|
3540
|
+
tokens: { used: month.tokens, cost: month.cost, limit: metered ? limits.monthlyTokens : undefined, metered },
|
|
3541
|
+
promptsPerMinute: limits.promptsPerMinute,
|
|
3542
|
+
connections: limits.connections,
|
|
3543
|
+
blocked: this.blocked(tenant, now)
|
|
3544
|
+
};
|
|
3545
|
+
}
|
|
3546
|
+
refuseModels(tenant, now = Date.now()) {
|
|
3547
|
+
var _a;
|
|
3548
|
+
if (this.role(tenant) === "owner") {
|
|
3549
|
+
return;
|
|
3550
|
+
}
|
|
3551
|
+
if (tenant.suspended) {
|
|
3552
|
+
return "Your account is suspended by an administrator of the gateway.";
|
|
3553
|
+
}
|
|
3554
|
+
const limit = this.limits(tenant).monthlyTokens;
|
|
3555
|
+
const used = (_a = this.meter(tenant, now).tokens) !== null && _a !== undefined ? _a : 0;
|
|
3556
|
+
return limit !== undefined && used >= limit ? `Monthly usage limit reached: ${used} of ${limit} tokens this month; it starts again next month, or ask an administrator.` : undefined;
|
|
3557
|
+
}
|
|
3558
|
+
addTokens(tenantId, tokens, cost, now = Date.now()) {
|
|
3559
|
+
var _a, _b;
|
|
3560
|
+
const tenant = this.tenants.get(tenantId);
|
|
3561
|
+
if (!tenant) {
|
|
3562
|
+
return;
|
|
3563
|
+
}
|
|
3564
|
+
const meter = this.meter(tenant, now);
|
|
3565
|
+
this.tenants.update(tenantId, { meter: { ...meter, tokens: ((_a = meter.tokens) !== null && _a !== undefined ? _a : 0) + tokens, cost: ((_b = meter.cost) !== null && _b !== undefined ? _b : 0) + cost } });
|
|
3566
|
+
}
|
|
3567
|
+
takePrompt(tenant, now = Date.now()) {
|
|
3568
|
+
var _a;
|
|
3569
|
+
const limit = this.limits(tenant).promptsPerMinute;
|
|
3570
|
+
if (limit === undefined) {
|
|
3571
|
+
return true;
|
|
3572
|
+
}
|
|
3573
|
+
const times = ((_a = this.prompts.get(tenant.id)) !== null && _a !== undefined ? _a : []).filter((time) => now - time < MINUTE);
|
|
3574
|
+
if (times.length >= limit) {
|
|
3575
|
+
this.prompts.set(tenant.id, times);
|
|
3576
|
+
return false;
|
|
3577
|
+
}
|
|
3578
|
+
times.push(now);
|
|
3579
|
+
this.prompts.set(tenant.id, times);
|
|
3580
|
+
return true;
|
|
3581
|
+
}
|
|
3582
|
+
retryAfter(tenant, now = Date.now()) {
|
|
3583
|
+
var _a;
|
|
3584
|
+
const oldest = (_a = this.prompts.get(tenant.id)) === null || _a === undefined ? undefined : _a[0];
|
|
3585
|
+
return oldest === undefined ? 0 : Math.max(0, MINUTE - (now - oldest));
|
|
3586
|
+
}
|
|
3587
|
+
tick(running, now = Date.now()) {
|
|
3588
|
+
const elapsed = Math.min(Math.max(0, now - this.lastTick), MAX_TICK_MS);
|
|
3589
|
+
this.lastTick = now;
|
|
3590
|
+
const blocked = [];
|
|
3591
|
+
for (const id of running) {
|
|
3592
|
+
const tenant = this.tenants.get(id);
|
|
3593
|
+
if (!tenant) {
|
|
3594
|
+
continue;
|
|
3595
|
+
}
|
|
3596
|
+
const meter = this.meter(tenant, now);
|
|
3597
|
+
const next = this.tenants.update(id, { meter: { ...meter, runMs: meter.runMs + elapsed } });
|
|
3598
|
+
if (this.blocked(next, now)) {
|
|
3599
|
+
blocked.push(next);
|
|
3600
|
+
}
|
|
3601
|
+
}
|
|
3602
|
+
return blocked;
|
|
3603
|
+
}
|
|
3604
|
+
grant(tenant, minutes, now = Date.now()) {
|
|
3605
|
+
const meter = this.meter(tenant, now);
|
|
3606
|
+
return this.tenants.update(tenant.id, { meter: { ...meter, bonusMinutes: Math.max(0, meter.bonusMinutes + minutes) } });
|
|
3607
|
+
}
|
|
3608
|
+
}
|
|
3609
|
+
|
|
3610
|
+
// ../../ta-packages/ta-protocol/esm/common/redactor.js
|
|
3611
|
+
var REDACTED = "[redacted]";
|
|
3612
|
+
var WHOLE = [
|
|
3613
|
+
/-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 ]*PRIVATE KEY-----/g,
|
|
3614
|
+
/\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{20,}/g,
|
|
3615
|
+
/\bgh[pousr]_[A-Za-z0-9]{30,}\b/g,
|
|
3616
|
+
/\bgithub_pat_[A-Za-z0-9_]{40,}\b/g,
|
|
3617
|
+
/\bglpat-[A-Za-z0-9_-]{20,}\b/g,
|
|
3618
|
+
/\bxox[abprs]-[A-Za-z0-9-]{10,}\b/g,
|
|
3619
|
+
/\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g,
|
|
3620
|
+
/\bAIza[0-9A-Za-z_-]{35}\b/g,
|
|
3621
|
+
/\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/g
|
|
3622
|
+
];
|
|
3623
|
+
var NAMED = [
|
|
3624
|
+
/(\bBearer\s+)[A-Za-z0-9._~+/-]{16,}=*/gi,
|
|
3625
|
+
/(\b[A-Za-z0-9_.-]*(?:password|passwd|pwd|secret|token|api[_-]?key|access[_-]?key|private[_-]?key)[A-Za-z0-9_.-]*["']?\s*[:=]\s*["']?)([^\s"'`,;]{6,})/gi,
|
|
3626
|
+
/(:\/\/[^\s:/@]+:)([^\s@/]{3,})(@)/g
|
|
3627
|
+
];
|
|
3628
|
+
|
|
3629
|
+
class Redactor {
|
|
3630
|
+
constructor() {
|
|
3631
|
+
this.count = 0;
|
|
3632
|
+
}
|
|
3633
|
+
text(text) {
|
|
3634
|
+
let out = text;
|
|
3635
|
+
for (const pattern of WHOLE) {
|
|
3636
|
+
out = out.replace(pattern, () => this.hide());
|
|
3637
|
+
}
|
|
3638
|
+
out = out.replace(NAMED[0], (_match, before) => before + this.hide());
|
|
3639
|
+
out = out.replace(NAMED[1], (match, before, value) => Redactor.placeholder(value) ? match : before + this.hide());
|
|
3640
|
+
out = out.replace(NAMED[2], (_match, before, _value, after) => before + this.hide() + after);
|
|
3641
|
+
return out;
|
|
3642
|
+
}
|
|
3643
|
+
value(value, max) {
|
|
3644
|
+
if (typeof value === "string") {
|
|
3645
|
+
return Redactor.cut(this.text(value), max);
|
|
3646
|
+
}
|
|
3647
|
+
if (Array.isArray(value)) {
|
|
3648
|
+
return value.map((item) => this.value(item, max));
|
|
3649
|
+
}
|
|
3650
|
+
if (value && typeof value === "object") {
|
|
3651
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, this.value(item, max)]));
|
|
3652
|
+
}
|
|
3653
|
+
return value;
|
|
3654
|
+
}
|
|
3655
|
+
static cut(text, max) {
|
|
3656
|
+
return text.length > max ? `${text.slice(0, max)}…` : text;
|
|
3657
|
+
}
|
|
3658
|
+
hide() {
|
|
3659
|
+
this.count++;
|
|
3660
|
+
return REDACTED;
|
|
3661
|
+
}
|
|
3662
|
+
static placeholder(value) {
|
|
3663
|
+
return /^(?:\$|<|\{|\[redacted|process\.env|os\.environ|xxx|\*{3})/i.test(value) || /^(?:true|false|null|undefined|none|string|number)$/i.test(value);
|
|
3664
|
+
}
|
|
3665
|
+
}
|
|
3666
|
+
|
|
3667
|
+
// ../../ta-packages/ta-gateway/esm/node/model-proxy.js
|
|
3668
|
+
import { createHash as createHash5, timingSafeEqual } from "node:crypto";
|
|
3669
|
+
import { existsSync as existsSync3, readFileSync as readFileSync7 } from "node:fs";
|
|
3670
|
+
import { join as join6 } from "node:path";
|
|
3671
|
+
|
|
3672
|
+
class ModelConfigError extends Error {
|
|
3673
|
+
}
|
|
3674
|
+
var MAX_BODY = 32 * 1024 * 1024;
|
|
3675
|
+
var MAX_READ = 8 * 1024 * 1024;
|
|
3676
|
+
var DROPPED_REQUEST = new Set([
|
|
3677
|
+
"host",
|
|
3678
|
+
"authorization",
|
|
3679
|
+
"x-api-key",
|
|
3680
|
+
"x-goog-api-key",
|
|
3681
|
+
"api-key",
|
|
3682
|
+
"cookie",
|
|
3683
|
+
"connection",
|
|
3684
|
+
"content-length",
|
|
3685
|
+
"accept-encoding",
|
|
3686
|
+
"transfer-encoding",
|
|
3687
|
+
"keep-alive",
|
|
3688
|
+
"upgrade",
|
|
3689
|
+
"proxy-authorization",
|
|
3690
|
+
"x-forwarded-for",
|
|
3691
|
+
"x-forwarded-proto",
|
|
3692
|
+
"x-forwarded-host"
|
|
3693
|
+
]);
|
|
3694
|
+
var DROPPED_RESPONSE = new Set(["content-encoding", "content-length", "transfer-encoding", "connection", "keep-alive", "set-cookie"]);
|
|
3695
|
+
var ModelProxyUtil;
|
|
3696
|
+
(function(ModelProxyUtil2) {
|
|
3697
|
+
ModelProxyUtil2.PATH = "/api/models";
|
|
3698
|
+
const ID = /^[a-z0-9][a-z0-9._-]{0,63}$/i;
|
|
3699
|
+
function read(dir) {
|
|
3700
|
+
var _a;
|
|
3701
|
+
const file = join6(dir, "models.json");
|
|
3702
|
+
if (!existsSync3(file)) {
|
|
3703
|
+
return {};
|
|
3704
|
+
}
|
|
3705
|
+
let config;
|
|
3706
|
+
try {
|
|
3707
|
+
config = JSON.parse(readFileSync7(file, "utf8"));
|
|
3708
|
+
} catch (error2) {
|
|
3709
|
+
throw new ModelConfigError(`${file} is not JSON: ${error2.message}`);
|
|
3710
|
+
}
|
|
3711
|
+
for (const [id, provider] of Object.entries((_a = config.providers) !== null && _a !== undefined ? _a : {})) {
|
|
3712
|
+
if (!ID.test(id)) {
|
|
3713
|
+
throw new ModelConfigError(`"${id}" cannot name a provider of models: letters, digits, . _ and -.`);
|
|
3714
|
+
}
|
|
3715
|
+
if (typeof (provider === null || provider === undefined ? undefined : provider.baseUrl) !== "string" || !/^https?:\/\//.test(provider.baseUrl) || typeof provider.api !== "string" || !Array.isArray(provider.models)) {
|
|
3716
|
+
throw new ModelConfigError(`The provider of models "${id}" needs baseUrl (http or https), api and models, as pi's models.json.`);
|
|
3717
|
+
}
|
|
3718
|
+
}
|
|
3719
|
+
return config;
|
|
3720
|
+
}
|
|
3721
|
+
ModelProxyUtil2.read = read;
|
|
3722
|
+
function key(value, env = process.env) {
|
|
3723
|
+
if (!value) {
|
|
3724
|
+
return;
|
|
3725
|
+
}
|
|
3726
|
+
const name = value.startsWith("$") ? value.slice(1) : value;
|
|
3727
|
+
if (/^[A-Z][A-Z0-9_]*$/.test(name) && env[name]) {
|
|
3728
|
+
return env[name];
|
|
3729
|
+
}
|
|
3730
|
+
return value.startsWith("$") ? undefined : value;
|
|
3731
|
+
}
|
|
3732
|
+
ModelProxyUtil2.key = key;
|
|
3733
|
+
function catalog(config) {
|
|
3734
|
+
var _a;
|
|
3735
|
+
return {
|
|
3736
|
+
providers: Object.entries((_a = config.providers) !== null && _a !== undefined ? _a : {}).map(([id, provider]) => ({
|
|
3737
|
+
id,
|
|
3738
|
+
name: provider.name,
|
|
3739
|
+
api: provider.api,
|
|
3740
|
+
models: provider.models.map((model) => ({ ...model }))
|
|
3741
|
+
}))
|
|
3742
|
+
};
|
|
3743
|
+
}
|
|
3744
|
+
ModelProxyUtil2.catalog = catalog;
|
|
3745
|
+
function presented(request2) {
|
|
3746
|
+
var _a, _b;
|
|
3747
|
+
const bearer = request2.headers.authorization;
|
|
3748
|
+
if (bearer === null || bearer === undefined ? undefined : bearer.startsWith("Bearer ")) {
|
|
3749
|
+
return bearer.slice("Bearer ".length).trim();
|
|
3750
|
+
}
|
|
3751
|
+
for (const name of ["x-api-key", "x-goog-api-key", "api-key"]) {
|
|
3752
|
+
const value = request2.headers[name];
|
|
3753
|
+
if (typeof value === "string" && value) {
|
|
3754
|
+
return value.trim();
|
|
3755
|
+
}
|
|
3756
|
+
}
|
|
3757
|
+
return (_b = new URL((_a = request2.url) !== null && _a !== undefined ? _a : "/", "http://gateway").searchParams.get("key")) !== null && _b !== undefined ? _b : undefined;
|
|
3758
|
+
}
|
|
3759
|
+
ModelProxyUtil2.presented = presented;
|
|
3760
|
+
function authHeaders(api, key2) {
|
|
3761
|
+
if (!key2) {
|
|
3762
|
+
return {};
|
|
3763
|
+
}
|
|
3764
|
+
if (api.startsWith("anthropic")) {
|
|
3765
|
+
return { "x-api-key": key2 };
|
|
3766
|
+
}
|
|
3767
|
+
if (api.startsWith("google")) {
|
|
3768
|
+
return { "x-goog-api-key": key2 };
|
|
3769
|
+
}
|
|
3770
|
+
if (api.includes("azure")) {
|
|
3771
|
+
return { "api-key": key2 };
|
|
3772
|
+
}
|
|
3773
|
+
return { authorization: `Bearer ${key2}` };
|
|
3774
|
+
}
|
|
3775
|
+
ModelProxyUtil2.authHeaders = authHeaders;
|
|
3776
|
+
function target(baseUrl, rest, query) {
|
|
3777
|
+
const params = new URLSearchParams(query);
|
|
3778
|
+
params.delete("key");
|
|
3779
|
+
const search = params.toString();
|
|
3780
|
+
return `${baseUrl.replace(/\/+$/, "")}${rest}${search ? `?${search}` : ""}`;
|
|
3781
|
+
}
|
|
3782
|
+
ModelProxyUtil2.target = target;
|
|
3783
|
+
function number(value) {
|
|
3784
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
|
|
3785
|
+
}
|
|
3786
|
+
function usageOf(value) {
|
|
3787
|
+
var _a, _b, _c, _d;
|
|
3788
|
+
var _e, _f, _g;
|
|
3789
|
+
if (!value || typeof value !== "object") {
|
|
3790
|
+
return;
|
|
3791
|
+
}
|
|
3792
|
+
const object = value;
|
|
3793
|
+
const usage = (_g = (_f = (_e = object.usage) !== null && _e !== undefined ? _e : (_a = object.message) === null || _a === undefined ? undefined : _a.usage) !== null && _f !== undefined ? _f : (_b = object.response) === null || _b === undefined ? undefined : _b.usage) !== null && _g !== undefined ? _g : object.usageMetadata;
|
|
3794
|
+
if (!usage || typeof usage !== "object") {
|
|
3795
|
+
return;
|
|
3796
|
+
}
|
|
3797
|
+
if ("promptTokenCount" in usage || "candidatesTokenCount" in usage) {
|
|
3798
|
+
const cached = number(usage.cachedContentTokenCount);
|
|
3799
|
+
return {
|
|
3800
|
+
input: Math.max(0, number(usage.promptTokenCount) - cached),
|
|
3801
|
+
output: number(usage.candidatesTokenCount) + number(usage.thoughtsTokenCount),
|
|
3802
|
+
cacheRead: cached,
|
|
3803
|
+
cacheWrite: 0
|
|
3804
|
+
};
|
|
3805
|
+
}
|
|
3806
|
+
if ("prompt_tokens" in usage || "completion_tokens" in usage) {
|
|
3807
|
+
const cached = number((_c = usage.prompt_tokens_details) === null || _c === undefined ? undefined : _c.cached_tokens);
|
|
3808
|
+
return { input: Math.max(0, number(usage.prompt_tokens) - cached), output: number(usage.completion_tokens), cacheRead: cached, cacheWrite: 0 };
|
|
3809
|
+
}
|
|
3810
|
+
if ("input_tokens" in usage || "output_tokens" in usage) {
|
|
3811
|
+
const cached = number((_d = usage.input_tokens_details) === null || _d === undefined ? undefined : _d.cached_tokens);
|
|
3812
|
+
return {
|
|
3813
|
+
input: Math.max(0, number(usage.input_tokens) - cached),
|
|
3814
|
+
output: number(usage.output_tokens),
|
|
3815
|
+
cacheRead: cached + number(usage.cache_read_input_tokens),
|
|
3816
|
+
cacheWrite: number(usage.cache_creation_input_tokens)
|
|
3817
|
+
};
|
|
3818
|
+
}
|
|
3819
|
+
return;
|
|
3820
|
+
}
|
|
3821
|
+
ModelProxyUtil2.usageOf = usageOf;
|
|
3822
|
+
function merge(into, next) {
|
|
3823
|
+
if (!next) {
|
|
3824
|
+
return into;
|
|
3825
|
+
}
|
|
3826
|
+
if (!into) {
|
|
3827
|
+
return { ...next };
|
|
3828
|
+
}
|
|
3829
|
+
return {
|
|
3830
|
+
input: Math.max(into.input, next.input),
|
|
3831
|
+
output: Math.max(into.output, next.output),
|
|
3832
|
+
cacheRead: Math.max(into.cacheRead, next.cacheRead),
|
|
3833
|
+
cacheWrite: Math.max(into.cacheWrite, next.cacheWrite)
|
|
3834
|
+
};
|
|
3835
|
+
}
|
|
3836
|
+
ModelProxyUtil2.merge = merge;
|
|
3837
|
+
function total(usage) {
|
|
3838
|
+
return usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
|
|
3839
|
+
}
|
|
3840
|
+
ModelProxyUtil2.total = total;
|
|
3841
|
+
function cost(usage, price) {
|
|
3842
|
+
var _a, _b, _c, _d;
|
|
3843
|
+
if (!price) {
|
|
3844
|
+
return 0;
|
|
3845
|
+
}
|
|
3846
|
+
return (usage.input * ((_a = price.input) !== null && _a !== undefined ? _a : 0) + usage.output * ((_b = price.output) !== null && _b !== undefined ? _b : 0) + usage.cacheRead * ((_c = price.cacheRead) !== null && _c !== undefined ? _c : 0) + usage.cacheWrite * ((_d = price.cacheWrite) !== null && _d !== undefined ? _d : 0)) / 1e6;
|
|
3847
|
+
}
|
|
3848
|
+
ModelProxyUtil2.cost = cost;
|
|
3849
|
+
function metered(api, body) {
|
|
3850
|
+
if (api !== "openai-completions") {
|
|
3851
|
+
return body;
|
|
3852
|
+
}
|
|
3853
|
+
let parsed;
|
|
3854
|
+
try {
|
|
3855
|
+
parsed = JSON.parse(body.toString("utf8"));
|
|
3856
|
+
} catch (_a) {
|
|
3857
|
+
return body;
|
|
3858
|
+
}
|
|
3859
|
+
if (!parsed || typeof parsed !== "object" || parsed.stream !== true) {
|
|
3860
|
+
return body;
|
|
3861
|
+
}
|
|
3862
|
+
const options = parsed.stream_options && typeof parsed.stream_options === "object" ? parsed.stream_options : {};
|
|
3863
|
+
return Buffer.from(JSON.stringify({ ...parsed, stream_options: { ...options, include_usage: true } }));
|
|
3864
|
+
}
|
|
3865
|
+
ModelProxyUtil2.metered = metered;
|
|
3866
|
+
function modelOf(body, rest) {
|
|
3867
|
+
var _a;
|
|
3868
|
+
const named = (_a = /\/models\/([^/:]+)/.exec(rest)) === null || _a === undefined ? undefined : _a[1];
|
|
3869
|
+
if (named) {
|
|
3870
|
+
return decodeURIComponent(named);
|
|
3871
|
+
}
|
|
3872
|
+
try {
|
|
3873
|
+
const model = JSON.parse(body.toString("utf8")).model;
|
|
3874
|
+
return typeof model === "string" ? model : undefined;
|
|
3875
|
+
} catch (_b) {
|
|
3876
|
+
return;
|
|
3877
|
+
}
|
|
3878
|
+
}
|
|
3879
|
+
ModelProxyUtil2.modelOf = modelOf;
|
|
3880
|
+
function error(response, status, message, type = "insufficient_quota") {
|
|
3881
|
+
if (response.headersSent) {
|
|
3882
|
+
response.destroy();
|
|
3883
|
+
return;
|
|
3884
|
+
}
|
|
3885
|
+
response.writeHead(status, { "Content-Type": "application/json", "Cache-Control": "no-store" });
|
|
3886
|
+
response.end(JSON.stringify({ type: "error", error: { type, code: type, message } }));
|
|
3887
|
+
}
|
|
3888
|
+
ModelProxyUtil2.error = error;
|
|
3889
|
+
function sameKey(given, known) {
|
|
3890
|
+
return timingSafeEqual(createHash5("sha256").update(given).digest(), createHash5("sha256").update(known).digest());
|
|
3891
|
+
}
|
|
3892
|
+
ModelProxyUtil2.sameKey = sameKey;
|
|
3893
|
+
})(ModelProxyUtil || (ModelProxyUtil = {}));
|
|
3894
|
+
|
|
3895
|
+
class ModelProxy {
|
|
3896
|
+
constructor(config, hooks, env = process.env) {
|
|
3897
|
+
this.config = config;
|
|
3898
|
+
this.hooks = hooks;
|
|
3899
|
+
this.env = env;
|
|
3900
|
+
}
|
|
3901
|
+
get enabled() {
|
|
3902
|
+
var _a;
|
|
3903
|
+
return Object.keys((_a = this.config.providers) !== null && _a !== undefined ? _a : {}).length > 0;
|
|
3904
|
+
}
|
|
3905
|
+
provider(id) {
|
|
3906
|
+
var _a;
|
|
3907
|
+
return new Map(Object.entries((_a = this.config.providers) !== null && _a !== undefined ? _a : {})).get(id);
|
|
3908
|
+
}
|
|
3909
|
+
async handle(request2, response) {
|
|
3910
|
+
var _a, _b;
|
|
3911
|
+
var _c, _d, _e, _f;
|
|
3912
|
+
response.setHeader("Cache-Control", "no-store");
|
|
3913
|
+
const url = new URL((_c = request2.url) !== null && _c !== undefined ? _c : "/", "http://gateway");
|
|
3914
|
+
const given = ModelProxyUtil.presented(request2);
|
|
3915
|
+
const tenant = given ? this.hooks.tenantOfKey(given) : undefined;
|
|
3916
|
+
if (!tenant) {
|
|
3917
|
+
ModelProxyUtil.error(response, 401, "This model key is not one of the gateway.", "authentication_error");
|
|
3918
|
+
return;
|
|
3919
|
+
}
|
|
3920
|
+
const rest = url.pathname.slice(ModelProxyUtil.PATH.length);
|
|
3921
|
+
if (rest === "" || rest === "/") {
|
|
3922
|
+
if (request2.method !== "GET") {
|
|
3923
|
+
ModelProxyUtil.error(response, 405, "Read the catalog with GET.", "invalid_request_error");
|
|
3924
|
+
return;
|
|
3925
|
+
}
|
|
3926
|
+
response.writeHead(200, { "Content-Type": "application/json" });
|
|
3927
|
+
response.end(JSON.stringify(ModelProxyUtil.catalog(this.config)));
|
|
3928
|
+
return;
|
|
3929
|
+
}
|
|
3930
|
+
const id = rest.slice(1).split("/")[0];
|
|
3931
|
+
const provider = this.provider(id);
|
|
3932
|
+
if (!provider) {
|
|
3933
|
+
ModelProxyUtil.error(response, 404, `The gateway has no provider of models ${id}.`, "not_found_error");
|
|
3934
|
+
return;
|
|
3935
|
+
}
|
|
3936
|
+
const refused = this.hooks.refuse(tenant.id);
|
|
3937
|
+
if (refused) {
|
|
3938
|
+
ModelProxyUtil.error(response, 429, refused);
|
|
3939
|
+
return;
|
|
3940
|
+
}
|
|
3941
|
+
const received = await this.body(request2);
|
|
3942
|
+
if (!received) {
|
|
3943
|
+
ModelProxyUtil.error(response, 413, "The request is too large.", "invalid_request_error");
|
|
3944
|
+
return;
|
|
3945
|
+
}
|
|
3946
|
+
const body = ModelProxyUtil.metered(provider.api, received);
|
|
3947
|
+
const path = rest.slice(1 + id.length);
|
|
3948
|
+
const model = ModelProxyUtil.modelOf(body, path);
|
|
3949
|
+
const headers = {};
|
|
3950
|
+
for (const [name, value] of Object.entries(request2.headers)) {
|
|
3951
|
+
if (typeof value === "string" && !DROPPED_REQUEST.has(name)) {
|
|
3952
|
+
headers[name] = value;
|
|
3953
|
+
}
|
|
3954
|
+
}
|
|
3955
|
+
Object.assign(headers, (_d = provider.headers) !== null && _d !== undefined ? _d : {}, ModelProxyUtil.authHeaders(provider.api, ModelProxyUtil.key(provider.apiKey, this.env)));
|
|
3956
|
+
const abort = new AbortController;
|
|
3957
|
+
response.once("close", () => abort.abort());
|
|
3958
|
+
let upstream;
|
|
3959
|
+
try {
|
|
3960
|
+
upstream = await fetch(ModelProxyUtil.target(provider.baseUrl, path, url.searchParams), {
|
|
3961
|
+
method: request2.method,
|
|
3962
|
+
headers,
|
|
3963
|
+
body: request2.method === "GET" || request2.method === "HEAD" ? undefined : new Uint8Array(body),
|
|
3964
|
+
signal: abort.signal
|
|
3965
|
+
});
|
|
3966
|
+
} catch (error) {
|
|
3967
|
+
ModelProxyUtil.error(response, 502, `The provider of models ${id} is not reachable: ${error.message}`, "api_error");
|
|
3968
|
+
return;
|
|
3969
|
+
}
|
|
3970
|
+
const out = {};
|
|
3971
|
+
upstream.headers.forEach((value, name) => {
|
|
3972
|
+
if (!DROPPED_RESPONSE.has(name)) {
|
|
3973
|
+
out[name] = value;
|
|
3974
|
+
}
|
|
3975
|
+
});
|
|
3976
|
+
response.writeHead(upstream.status, out);
|
|
3977
|
+
const stream = ((_e = upstream.headers.get("content-type")) !== null && _e !== undefined ? _e : "").includes("event-stream");
|
|
3978
|
+
let usage;
|
|
3979
|
+
let text = "";
|
|
3980
|
+
const decoder = new TextDecoder;
|
|
3981
|
+
const reader = (_a = upstream.body) === null || _a === undefined ? undefined : _a.getReader();
|
|
3982
|
+
try {
|
|
3983
|
+
while (reader) {
|
|
3984
|
+
const { done, value } = await reader.read();
|
|
3985
|
+
if (done) {
|
|
3986
|
+
break;
|
|
3987
|
+
}
|
|
3988
|
+
if (!response.write(value) && !await this.drained(response)) {
|
|
3989
|
+
break;
|
|
3990
|
+
}
|
|
3991
|
+
const chunk = decoder.decode(value, { stream: true });
|
|
3992
|
+
if (stream) {
|
|
3993
|
+
text += chunk;
|
|
3994
|
+
const lines = text.split(`
|
|
3995
|
+
`);
|
|
3996
|
+
text = (_f = lines.pop()) !== null && _f !== undefined ? _f : "";
|
|
3997
|
+
for (const line of lines) {
|
|
3998
|
+
usage = ModelProxyUtil.merge(usage, this.event(line));
|
|
3999
|
+
}
|
|
4000
|
+
} else if (text.length < MAX_READ) {
|
|
4001
|
+
text += chunk;
|
|
4002
|
+
}
|
|
4003
|
+
}
|
|
4004
|
+
if (stream) {
|
|
4005
|
+
usage = ModelProxyUtil.merge(usage, this.event(text));
|
|
4006
|
+
} else if (upstream.ok) {
|
|
4007
|
+
try {
|
|
4008
|
+
usage = ModelProxyUtil.usageOf(JSON.parse(text));
|
|
4009
|
+
} catch (_g) {}
|
|
4010
|
+
}
|
|
4011
|
+
response.end();
|
|
4012
|
+
} catch (_h) {
|
|
4013
|
+
response.destroy();
|
|
4014
|
+
} finally {
|
|
4015
|
+
if (usage && ModelProxyUtil.total(usage) > 0) {
|
|
4016
|
+
const price = (_b = provider.models.find((each) => each.id === model)) === null || _b === undefined ? undefined : _b.cost;
|
|
4017
|
+
this.hooks.record(tenant.id, usage, ModelProxyUtil.cost(usage, price));
|
|
4018
|
+
}
|
|
4019
|
+
}
|
|
4020
|
+
}
|
|
4021
|
+
drained(response) {
|
|
4022
|
+
if (response.destroyed) {
|
|
4023
|
+
return Promise.resolve(false);
|
|
4024
|
+
}
|
|
4025
|
+
return new Promise((resolve) => {
|
|
4026
|
+
const done = (drained) => {
|
|
4027
|
+
response.off("drain", onDrain);
|
|
4028
|
+
response.off("close", onClose);
|
|
4029
|
+
resolve(drained);
|
|
4030
|
+
};
|
|
4031
|
+
const onDrain = () => done(true);
|
|
4032
|
+
const onClose = () => done(false);
|
|
4033
|
+
response.on("drain", onDrain);
|
|
4034
|
+
response.on("close", onClose);
|
|
4035
|
+
});
|
|
4036
|
+
}
|
|
4037
|
+
event(line) {
|
|
4038
|
+
const trimmed = line.trim();
|
|
4039
|
+
if (!trimmed.startsWith("data:")) {
|
|
4040
|
+
return;
|
|
4041
|
+
}
|
|
4042
|
+
try {
|
|
4043
|
+
return ModelProxyUtil.usageOf(JSON.parse(trimmed.slice(5)));
|
|
4044
|
+
} catch (_a) {
|
|
4045
|
+
return;
|
|
4046
|
+
}
|
|
4047
|
+
}
|
|
4048
|
+
async body(request2) {
|
|
4049
|
+
const chunks = [];
|
|
4050
|
+
let size = 0;
|
|
4051
|
+
for await (const chunk of request2) {
|
|
4052
|
+
size += chunk.length;
|
|
4053
|
+
if (size > MAX_BODY) {
|
|
4054
|
+
return;
|
|
4055
|
+
}
|
|
4056
|
+
chunks.push(chunk);
|
|
4057
|
+
}
|
|
4058
|
+
return Buffer.concat(chunks);
|
|
4059
|
+
}
|
|
4060
|
+
}
|
|
4061
|
+
|
|
4062
|
+
// ../../ta-packages/ta-gateway/esm/node/gateway-service.js
|
|
4063
|
+
class GatewayCallError extends Error {
|
|
4064
|
+
constructor(message, code, hint) {
|
|
4065
|
+
super(message);
|
|
4066
|
+
this.code = code;
|
|
4067
|
+
this.hint = hint;
|
|
4068
|
+
}
|
|
4069
|
+
}
|
|
4070
|
+
var DAY2 = 24 * 3600000;
|
|
4071
|
+
var GatewayServiceUtil;
|
|
4072
|
+
(function(GatewayServiceUtil2) {
|
|
4073
|
+
GatewayServiceUtil2.DEFAULT_LOG_LINES = 200;
|
|
4074
|
+
GatewayServiceUtil2.MAX_LOG_LINES = 2000;
|
|
4075
|
+
GatewayServiceUtil2.RULES = {
|
|
4076
|
+
TaGatewayServer: {
|
|
4077
|
+
getGateway: "anyone",
|
|
4078
|
+
setEnvironment: "anyone",
|
|
4079
|
+
restartEnvironment: "anyone",
|
|
4080
|
+
listTenants: "admin",
|
|
4081
|
+
getConsole: "admin",
|
|
4082
|
+
getTenantAgents: "admin",
|
|
4083
|
+
getAgentActivity: "admin",
|
|
4084
|
+
getTenantLogs: "admin",
|
|
4085
|
+
getUsageTrend: "admin",
|
|
4086
|
+
getPolicy: "admin",
|
|
4087
|
+
getAudit: "admin",
|
|
4088
|
+
startTenant: "admin",
|
|
4089
|
+
stopTenant: "admin",
|
|
4090
|
+
restartTenant: "admin",
|
|
4091
|
+
createSignInLink: "admin",
|
|
4092
|
+
setTenantLimits: "admin",
|
|
4093
|
+
grantMinutes: "admin",
|
|
4094
|
+
setSuspended: "admin",
|
|
4095
|
+
setDefaultLimits: "owner",
|
|
4096
|
+
setRole: "owner",
|
|
4097
|
+
createAccessToken: "anyone"
|
|
4098
|
+
},
|
|
4099
|
+
TaAccessServer: {
|
|
4100
|
+
getAccess: "anyone",
|
|
4101
|
+
signOut: "anyone",
|
|
4102
|
+
bindUrl: "admin",
|
|
4103
|
+
allow: "admin",
|
|
4104
|
+
dismiss: "admin",
|
|
4105
|
+
disallow: "admin"
|
|
4106
|
+
}
|
|
4107
|
+
};
|
|
4108
|
+
GatewayServiceUtil2.AUDITED = new Set([
|
|
4109
|
+
"startTenant",
|
|
4110
|
+
"stopTenant",
|
|
4111
|
+
"restartTenant",
|
|
4112
|
+
"getTenantLogs",
|
|
4113
|
+
"getAgentActivity",
|
|
4114
|
+
"createSignInLink",
|
|
4115
|
+
"setTenantLimits",
|
|
4116
|
+
"grantMinutes",
|
|
4117
|
+
"setSuspended",
|
|
4118
|
+
"setDefaultLimits",
|
|
4119
|
+
"setRole",
|
|
4120
|
+
"allow",
|
|
4121
|
+
"dismiss",
|
|
4122
|
+
"disallow"
|
|
4123
|
+
]);
|
|
4124
|
+
const BY_SERVICE = new Map(Object.entries(GatewayServiceUtil2.RULES).map(([service, rules]) => [service, new Map(Object.entries(rules))]));
|
|
4125
|
+
function need(service, name) {
|
|
4126
|
+
var _a;
|
|
4127
|
+
return (_a = BY_SERVICE.get(service)) === null || _a === undefined ? undefined : _a.get(name);
|
|
4128
|
+
}
|
|
4129
|
+
GatewayServiceUtil2.need = need;
|
|
4130
|
+
function allowed(role, need2) {
|
|
4131
|
+
return need2 === "anyone" || GatewayPolicyUtil.rank(role) >= GatewayPolicyUtil.rank(need2);
|
|
4132
|
+
}
|
|
4133
|
+
GatewayServiceUtil2.allowed = allowed;
|
|
4134
|
+
function lines(value) {
|
|
4135
|
+
const count = Math.floor(Number(value !== null && value !== undefined ? value : GatewayServiceUtil2.DEFAULT_LOG_LINES));
|
|
4136
|
+
return Number.isFinite(count) && count > 0 ? Math.min(count, GatewayServiceUtil2.MAX_LOG_LINES) : GatewayServiceUtil2.DEFAULT_LOG_LINES;
|
|
4137
|
+
}
|
|
4138
|
+
GatewayServiceUtil2.lines = lines;
|
|
4139
|
+
function days(value) {
|
|
4140
|
+
const count = Math.floor(Number(value !== null && value !== undefined ? value : 7));
|
|
4141
|
+
return Number.isFinite(count) ? Math.min(31, Math.max(1, count)) : 7;
|
|
4142
|
+
}
|
|
4143
|
+
GatewayServiceUtil2.days = days;
|
|
4144
|
+
GatewayServiceUtil2.SERVICES = Object.keys(GatewayServiceUtil2.RULES);
|
|
4145
|
+
function owns(method) {
|
|
4146
|
+
return typeof method === "string" && GatewayServiceUtil2.SERVICES.includes(method.split("/")[0]);
|
|
4147
|
+
}
|
|
4148
|
+
GatewayServiceUtil2.owns = owns;
|
|
4149
|
+
function mayOwn(text) {
|
|
4150
|
+
return GatewayServiceUtil2.SERVICES.some((service) => text.includes(`"${service}/`));
|
|
4151
|
+
}
|
|
4152
|
+
GatewayServiceUtil2.mayOwn = mayOwn;
|
|
4153
|
+
GatewayServiceUtil2.PROMPTS = ["TaServer/prompt", "TaServer/steer", "TaServer/followUp"];
|
|
4154
|
+
function mayPrompt(text) {
|
|
4155
|
+
return GatewayServiceUtil2.PROMPTS.some((method) => text.includes(`"${method}"`));
|
|
4156
|
+
}
|
|
4157
|
+
GatewayServiceUtil2.mayPrompt = mayPrompt;
|
|
4158
|
+
function error(id, message, code, hint) {
|
|
4159
|
+
return JSON.stringify({ jsonrpc: "2.0", id, error: { code: TA_RPC_ERROR_CODE, message, data: { kind: "application", data: { code, hint } } } });
|
|
4160
|
+
}
|
|
4161
|
+
GatewayServiceUtil2.error = error;
|
|
4162
|
+
function result(id, result2) {
|
|
4163
|
+
return JSON.stringify({ jsonrpc: "2.0", id, result: result2 !== null && result2 !== undefined ? result2 : null });
|
|
4164
|
+
}
|
|
4165
|
+
GatewayServiceUtil2.result = result;
|
|
4166
|
+
function invalid(message, hint) {
|
|
4167
|
+
return new GatewayCallError(message, TaErrorCode.INVALID_ARGUMENT, hint);
|
|
4168
|
+
}
|
|
4169
|
+
function forbidden(message, hint) {
|
|
4170
|
+
return new GatewayCallError(message, TaErrorCode.FORBIDDEN, hint);
|
|
4171
|
+
}
|
|
4172
|
+
GatewayServiceUtil2.forbidden = forbidden;
|
|
4173
|
+
function choice(value, own, providers) {
|
|
4174
|
+
var _a;
|
|
4175
|
+
const given = value !== null && value !== undefined ? value : {};
|
|
4176
|
+
if (typeof given.provider === "string") {
|
|
4177
|
+
if (!providers.includes(given.provider)) {
|
|
4178
|
+
throw invalid(`The gateway has no provider ${given.provider} for you.`, "Pick one of the options of getGateway.");
|
|
4179
|
+
}
|
|
4180
|
+
return { provider: given.provider };
|
|
4181
|
+
}
|
|
4182
|
+
if (given.type === "ta") {
|
|
4183
|
+
if (!own) {
|
|
4184
|
+
throw invalid("This gateway runs your Ta in its own sandboxes only.", "Pick one of the options of getGateway.");
|
|
4185
|
+
}
|
|
4186
|
+
let url;
|
|
4187
|
+
try {
|
|
4188
|
+
url = TaSandboxUtil.url(String((_a = given.url) !== null && _a !== undefined ? _a : ""));
|
|
4189
|
+
} catch (error2) {
|
|
4190
|
+
throw invalid(error2.message, "Write the address its Ta service is reached at, such as https://ta.example.com.");
|
|
4191
|
+
}
|
|
4192
|
+
if (typeof given.token !== "string" || !given.token.trim()) {
|
|
4193
|
+
throw invalid("A Ta service of your own is reached with its access token.", "Copy it from ~/.ta/agent/serve.json there, or the link ta serve printed.");
|
|
4194
|
+
}
|
|
4195
|
+
return { type: "ta", url, token: given.token.trim(), name: typeof given.name === "string" && given.name.trim() ? given.name.trim() : undefined };
|
|
4196
|
+
}
|
|
4197
|
+
throw invalid("No environment was named.", 'Pass { provider } or { type: "ta", url, token }.');
|
|
4198
|
+
}
|
|
4199
|
+
GatewayServiceUtil2.choice = choice;
|
|
4200
|
+
function detail(method, params) {
|
|
4201
|
+
const [, second] = params;
|
|
4202
|
+
switch (method) {
|
|
4203
|
+
case "setTenantLimits":
|
|
4204
|
+
case "setDefaultLimits":
|
|
4205
|
+
return JSON.stringify(method === "setDefaultLimits" ? params[0] : second);
|
|
4206
|
+
case "grantMinutes":
|
|
4207
|
+
case "getTenantLogs":
|
|
4208
|
+
return String(second !== null && second !== undefined ? second : "");
|
|
4209
|
+
case "setSuspended":
|
|
4210
|
+
return second ? "suspended" : "let go";
|
|
4211
|
+
case "setRole":
|
|
4212
|
+
return String(second);
|
|
4213
|
+
default:
|
|
4214
|
+
return;
|
|
4215
|
+
}
|
|
4216
|
+
}
|
|
4217
|
+
GatewayServiceUtil2.detail = detail;
|
|
4218
|
+
})(GatewayServiceUtil || (GatewayServiceUtil = {}));
|
|
4219
|
+
|
|
4220
|
+
class GatewayService {
|
|
4221
|
+
constructor(parts) {
|
|
4222
|
+
this.models = parts.models;
|
|
4223
|
+
this.flow = parts.flow;
|
|
4224
|
+
this.tenants = parts.tenants;
|
|
4225
|
+
this.environments = parts.environments;
|
|
4226
|
+
this.policy = parts.policy;
|
|
4227
|
+
this.quotas = parts.quotas;
|
|
4228
|
+
this.usage = parts.usage;
|
|
4229
|
+
this.audit = parts.audit;
|
|
4230
|
+
this.hooks = parts.hooks;
|
|
4231
|
+
this.meta = parts.meta;
|
|
4232
|
+
}
|
|
4233
|
+
async call(caller, method, params) {
|
|
4234
|
+
const [service, name] = method.split("/");
|
|
4235
|
+
const need = GatewayServiceUtil.need(service, name);
|
|
4236
|
+
if (!need) {
|
|
4237
|
+
throw new GatewayCallError(`${method} is not a method of the gateway.`, TaErrorCode.INVALID_ARGUMENT, "Call a method of TaGatewayServer or TaAccessServer.");
|
|
4238
|
+
}
|
|
4239
|
+
if (!GatewayServiceUtil.allowed(caller.role, need)) {
|
|
4240
|
+
throw need === "owner" ? GatewayServiceUtil.forbidden("Only owners of the gateway may do that.", "Ask one of those of TA_GATEWAY_ADMINS.") : GatewayServiceUtil.forbidden("Only administrators of the gateway may do that.", "Ask one of its administrators.");
|
|
4241
|
+
}
|
|
4242
|
+
const result = await this.handle(caller, service, name, params);
|
|
4243
|
+
if (GatewayServiceUtil.AUDITED.has(name)) {
|
|
4244
|
+
this.record(caller, name, params);
|
|
4245
|
+
}
|
|
4246
|
+
return result;
|
|
4247
|
+
}
|
|
4248
|
+
handle(caller, service, name, params) {
|
|
4249
|
+
const [first, second] = params;
|
|
4250
|
+
const handlers = service === "TaGatewayServer" ? {
|
|
4251
|
+
getGateway: () => this.info(caller),
|
|
4252
|
+
setEnvironment: () => this.setEnvironment(caller, first),
|
|
4253
|
+
restartEnvironment: () => this.restart(caller),
|
|
4254
|
+
listTenants: () => this.tenants.list().map((tenant) => this.tenantInfo(tenant)),
|
|
4255
|
+
getConsole: () => this.console(caller),
|
|
4256
|
+
getTenantAgents: () => this.environments.agents(this.tenant(first)),
|
|
4257
|
+
getAgentActivity: () => this.activity(first, second),
|
|
4258
|
+
getTenantLogs: () => this.logs(first, second),
|
|
4259
|
+
getUsageTrend: () => this.trend(first, second),
|
|
4260
|
+
getPolicy: () => this.policyView(),
|
|
4261
|
+
getAudit: () => this.audit.list(Math.min(500, Math.max(1, Math.floor(Number(first !== null && first !== undefined ? first : 100)) || 100))),
|
|
4262
|
+
startTenant: () => this.startTenant(caller, first),
|
|
4263
|
+
stopTenant: () => this.stopTenant(caller, first),
|
|
4264
|
+
restartTenant: () => this.restartTenant(caller, first),
|
|
4265
|
+
createSignInLink: () => this.signInLink(first, second),
|
|
4266
|
+
setTenantLimits: () => this.setTenantLimits(caller, first, second),
|
|
4267
|
+
grantMinutes: () => this.grantMinutes(caller, first, second),
|
|
4268
|
+
setSuspended: () => this.setSuspended(caller, first, second),
|
|
4269
|
+
setDefaultLimits: () => this.setDefaultLimits(first),
|
|
4270
|
+
setRole: () => this.setRole(caller, first, second),
|
|
4271
|
+
createAccessToken: () => this.accessToken(caller, first)
|
|
4272
|
+
} : {
|
|
4273
|
+
getAccess: () => this.access(caller),
|
|
4274
|
+
bindUrl: () => this.flow.bindUrl(String(first)),
|
|
4275
|
+
allow: () => this.allow(first, second),
|
|
4276
|
+
dismiss: () => this.flow.store.dismiss(String(first)),
|
|
4277
|
+
disallow: () => this.disallow(caller, String(first)),
|
|
4278
|
+
signOut: () => this.signOut(caller, String(first))
|
|
4279
|
+
};
|
|
4280
|
+
return Promise.resolve().then(handlers[name]);
|
|
4281
|
+
}
|
|
4282
|
+
record(caller, action2, params) {
|
|
4283
|
+
var _a;
|
|
4284
|
+
var _b;
|
|
4285
|
+
const [first] = params;
|
|
4286
|
+
const target = typeof first === "string" ? (_b = (_a = this.tenants.get(first)) === null || _a === undefined ? undefined : _a.account) !== null && _b !== undefined ? _b : /^[a-z][a-z0-9-]*:\S+$/.test(first) ? first : undefined : undefined;
|
|
4287
|
+
try {
|
|
4288
|
+
this.audit.add({ actor: caller.account, action: action2, target, detail: GatewayServiceUtil.detail(action2, params) });
|
|
4289
|
+
} catch (error) {
|
|
4290
|
+
console.warn(`ta gateway: the audit could not be written: ${error.message}`);
|
|
4291
|
+
}
|
|
4292
|
+
}
|
|
4293
|
+
info(caller) {
|
|
4294
|
+
var _a;
|
|
4295
|
+
const tenant = (_a = this.tenants.get(caller.tenant.id)) !== null && _a !== undefined ? _a : caller.tenant;
|
|
4296
|
+
this.usage.refresh(tenant.id, this.environments.usageReader(tenant)).catch(() => {
|
|
4297
|
+
return;
|
|
4298
|
+
});
|
|
4299
|
+
const limits = this.quotas.limits(tenant);
|
|
4300
|
+
return {
|
|
4301
|
+
account: { id: tenant.account, name: tenant.name, avatar: tenant.avatar },
|
|
4302
|
+
tenant: tenant.id,
|
|
4303
|
+
environment: this.environments.info(tenant),
|
|
4304
|
+
options: this.environments.choices().filter((option) => !limits.providers || limits.providers.includes(option.id)),
|
|
4305
|
+
own: this.environments.settings.own && limits.own,
|
|
4306
|
+
admin: caller.admin,
|
|
4307
|
+
role: caller.role,
|
|
4308
|
+
quota: this.quotas.state(tenant)
|
|
4309
|
+
};
|
|
4310
|
+
}
|
|
4311
|
+
async setEnvironment(caller, value) {
|
|
4312
|
+
var _a;
|
|
4313
|
+
const tenant = (_a = this.tenants.get(caller.tenant.id)) !== null && _a !== undefined ? _a : caller.tenant;
|
|
4314
|
+
const limits = this.quotas.limits(tenant);
|
|
4315
|
+
const choice = GatewayServiceUtil.choice(value, this.environments.settings.own && limits.own, this.environments.providers.map((provider) => provider.id).filter((id) => !limits.providers || limits.providers.includes(id)));
|
|
4316
|
+
const next = await this.environments.change(tenant, choice);
|
|
4317
|
+
caller.tenant = next;
|
|
4318
|
+
this.hooks.reset(next.id);
|
|
4319
|
+
return this.info(caller);
|
|
4320
|
+
}
|
|
4321
|
+
async restart(caller) {
|
|
4322
|
+
await this.environments.stop(caller.tenant.id).catch(() => {
|
|
4323
|
+
return;
|
|
4324
|
+
});
|
|
4325
|
+
this.hooks.reset(caller.tenant.id);
|
|
4326
|
+
return this.info(caller);
|
|
4327
|
+
}
|
|
4328
|
+
tenantInfo(tenant) {
|
|
4329
|
+
return {
|
|
4330
|
+
id: tenant.id,
|
|
4331
|
+
account: tenant.account,
|
|
4332
|
+
name: tenant.name,
|
|
4333
|
+
avatar: tenant.avatar,
|
|
4334
|
+
environment: this.environments.info(tenant),
|
|
4335
|
+
connections: this.environments.connections(tenant.id),
|
|
4336
|
+
createdAt: tenant.createdAt,
|
|
4337
|
+
lastActiveAt: tenant.lastActiveAt,
|
|
4338
|
+
role: this.quotas.role(tenant),
|
|
4339
|
+
quota: this.quotas.state(tenant),
|
|
4340
|
+
limits: tenant.limits,
|
|
4341
|
+
suspended: tenant.suspended || undefined
|
|
4342
|
+
};
|
|
4343
|
+
}
|
|
4344
|
+
async console(caller) {
|
|
4345
|
+
var _a, _b;
|
|
4346
|
+
var _c, _d, _e;
|
|
4347
|
+
const tenants = this.tenants.list();
|
|
4348
|
+
const [usages, agents] = await Promise.all([
|
|
4349
|
+
Promise.all(tenants.map((tenant) => this.environments.usage(tenant).catch(() => {
|
|
4350
|
+
return;
|
|
4351
|
+
}))),
|
|
4352
|
+
Promise.all(tenants.map((tenant) => this.environments.agentSummary(tenant).catch(() => {
|
|
4353
|
+
return;
|
|
4354
|
+
})))
|
|
4355
|
+
]);
|
|
4356
|
+
const counts = new Map;
|
|
4357
|
+
for (const tenant of tenants) {
|
|
4358
|
+
const { provider } = this.environments.info(tenant);
|
|
4359
|
+
counts.set(provider, ((_c = counts.get(provider)) !== null && _c !== undefined ? _c : 0) + 1);
|
|
4360
|
+
}
|
|
4361
|
+
const fallback = (_d = this.environments.settings.default) !== null && _d !== undefined ? _d : (_a = this.environments.providers[0]) === null || _a === undefined ? undefined : _a.id;
|
|
4362
|
+
return {
|
|
4363
|
+
version: this.meta.version,
|
|
4364
|
+
startedAt: this.meta.startedAt,
|
|
4365
|
+
publicUrl: this.meta.publicUrl,
|
|
4366
|
+
providers: this.environments.providers.map((provider) => {
|
|
4367
|
+
var _a2;
|
|
4368
|
+
return {
|
|
4369
|
+
id: provider.id,
|
|
4370
|
+
name: provider.name,
|
|
4371
|
+
type: provider.type,
|
|
4372
|
+
default: provider.id === fallback,
|
|
4373
|
+
tenants: (_a2 = counts.get(provider.id)) !== null && _a2 !== undefined ? _a2 : 0,
|
|
4374
|
+
logs: typeof provider.logs === "function"
|
|
4375
|
+
};
|
|
4376
|
+
}),
|
|
4377
|
+
own: this.environments.settings.own,
|
|
4378
|
+
idleMinutes: this.environments.settings.idleMinutes,
|
|
4379
|
+
tenants: tenants.map((tenant, index) => ({ ...this.tenantInfo(tenant), usage: usages[index], agents: agents[index] })).sort((a, b) => {
|
|
4380
|
+
var _a2, _b2;
|
|
4381
|
+
return ((_a2 = b.lastActiveAt) !== null && _a2 !== undefined ? _a2 : b.createdAt) - ((_b2 = a.lastActiveAt) !== null && _b2 !== undefined ? _b2 : a.createdAt);
|
|
4382
|
+
}),
|
|
4383
|
+
requests: this.flow.store.info(caller.token).requests.length,
|
|
4384
|
+
role: caller.role,
|
|
4385
|
+
policy: this.policyView(),
|
|
4386
|
+
models: ModelProxyUtil.catalog((_e = (_b = this.models) === null || _b === undefined ? undefined : _b.config) !== null && _e !== undefined ? _e : {}).providers.map((provider) => ({
|
|
4387
|
+
id: provider.id,
|
|
4388
|
+
name: provider.name,
|
|
4389
|
+
api: provider.api,
|
|
4390
|
+
models: provider.models.length
|
|
4391
|
+
}))
|
|
4392
|
+
};
|
|
4393
|
+
}
|
|
4394
|
+
policyView() {
|
|
4395
|
+
return this.policy.view(this.environments.settings.idleMinutes);
|
|
4396
|
+
}
|
|
4397
|
+
tenant(id) {
|
|
4398
|
+
const tenant = typeof id === "string" ? this.tenants.get(id) : undefined;
|
|
4399
|
+
if (!tenant) {
|
|
4400
|
+
throw new GatewayCallError(`The gateway has no tenant ${id}.`, TaErrorCode.INVALID_ARGUMENT, "Pick one of the tenants of getConsole.");
|
|
4401
|
+
}
|
|
4402
|
+
return tenant;
|
|
4403
|
+
}
|
|
4404
|
+
manage(caller, id) {
|
|
4405
|
+
const tenant = this.tenant(id);
|
|
4406
|
+
if (caller.role !== "owner" && (this.quotas.role(tenant) !== "member" || tenant.account === caller.account)) {
|
|
4407
|
+
throw GatewayServiceUtil.forbidden("Admins manage members only, not themselves, other admins or owners.", "Ask an owner of the gateway (TA_GATEWAY_ADMINS).");
|
|
4408
|
+
}
|
|
4409
|
+
return tenant;
|
|
4410
|
+
}
|
|
4411
|
+
async startTenant(caller, id) {
|
|
4412
|
+
await this.environments.ensure(this.manage(caller, id));
|
|
4413
|
+
}
|
|
4414
|
+
async stopTenant(caller, id) {
|
|
4415
|
+
const tenant = this.manage(caller, id);
|
|
4416
|
+
await this.environments.stop(tenant.id);
|
|
4417
|
+
this.hooks.reset(tenant.id);
|
|
4418
|
+
}
|
|
4419
|
+
async restartTenant(caller, id) {
|
|
4420
|
+
const tenant = this.manage(caller, id);
|
|
4421
|
+
await this.environments.stop(tenant.id).catch(() => {
|
|
4422
|
+
return;
|
|
4423
|
+
});
|
|
4424
|
+
this.hooks.reset(tenant.id);
|
|
4425
|
+
await this.environments.ensure(tenant);
|
|
4426
|
+
}
|
|
4427
|
+
async logs(id, lines) {
|
|
4428
|
+
return new Redactor().text(await this.environments.logs(this.tenant(id), GatewayServiceUtil.lines(lines)));
|
|
4429
|
+
}
|
|
4430
|
+
activity(id, sessionId) {
|
|
4431
|
+
return this.environments.activity(this.tenant(id), String(sessionId));
|
|
4432
|
+
}
|
|
4433
|
+
async trend(daysValue, id) {
|
|
4434
|
+
const now = Date.now();
|
|
4435
|
+
const days = GatewayServiceUtil.days(daysValue);
|
|
4436
|
+
const tenants = id === undefined || id === null ? this.tenants.list() : [this.tenant(id)];
|
|
4437
|
+
const fresh = await Promise.all(tenants.map((tenant) => this.usage.refresh(tenant.id, this.environments.usageReader(tenant), now, 0).catch(() => false)));
|
|
4438
|
+
const byTenant = new Map;
|
|
4439
|
+
const stale = [];
|
|
4440
|
+
tenants.forEach((tenant, index) => {
|
|
4441
|
+
const kept = this.usage.get(tenant.id);
|
|
4442
|
+
if (kept) {
|
|
4443
|
+
byTenant.set(tenant.id, kept.buckets);
|
|
4444
|
+
if (!fresh[index]) {
|
|
4445
|
+
stale.push(tenant.id);
|
|
4446
|
+
}
|
|
4447
|
+
}
|
|
4448
|
+
});
|
|
4449
|
+
const since = Math.floor((now - days * DAY2) / 3600000) * 3600000;
|
|
4450
|
+
return GatewayUsageUtil.trend(byTenant, since, stale, now);
|
|
4451
|
+
}
|
|
4452
|
+
limits(value, nullable) {
|
|
4453
|
+
try {
|
|
4454
|
+
return GatewayPolicyUtil.limits(value, this.environments.providers.map((provider) => provider.id), nullable);
|
|
4455
|
+
} catch (error) {
|
|
4456
|
+
if (error instanceof PolicyError) {
|
|
4457
|
+
throw new GatewayCallError(error.message, TaErrorCode.INVALID_ARGUMENT, "Send the limits again, written well.");
|
|
4458
|
+
}
|
|
4459
|
+
throw error;
|
|
4460
|
+
}
|
|
4461
|
+
}
|
|
4462
|
+
setTenantLimits(caller, id, value) {
|
|
4463
|
+
const tenant = this.manage(caller, id);
|
|
4464
|
+
this.tenants.update(tenant.id, { limits: value === null ? undefined : this.limits(value, true) });
|
|
4465
|
+
}
|
|
4466
|
+
grantMinutes(caller, id, minutes) {
|
|
4467
|
+
const tenant = this.manage(caller, id);
|
|
4468
|
+
if (typeof minutes !== "number" || !Number.isInteger(minutes) || Math.abs(minutes) > 1e6) {
|
|
4469
|
+
throw new GatewayCallError("Minutes are a whole number.", TaErrorCode.INVALID_ARGUMENT, "Give, say, 60; a negative number takes some back.");
|
|
4470
|
+
}
|
|
4471
|
+
this.quotas.grant(tenant, minutes);
|
|
4472
|
+
}
|
|
4473
|
+
async setSuspended(caller, id, suspended) {
|
|
4474
|
+
const tenant = this.manage(caller, id);
|
|
4475
|
+
this.tenants.update(tenant.id, { suspended: suspended === true || undefined });
|
|
4476
|
+
if (suspended === true) {
|
|
4477
|
+
await this.environments.stop(tenant.id).catch(() => {
|
|
4478
|
+
return;
|
|
4479
|
+
});
|
|
4480
|
+
this.hooks.reset(tenant.id);
|
|
4481
|
+
}
|
|
4482
|
+
}
|
|
4483
|
+
setDefaultLimits(value) {
|
|
4484
|
+
this.policy.setDefaults(this.limits(value, false));
|
|
4485
|
+
return this.policyView();
|
|
4486
|
+
}
|
|
4487
|
+
setRole(caller, account, role) {
|
|
4488
|
+
const id = typeof account === "string" ? account.trim() : "";
|
|
4489
|
+
if (role !== "admin" && role !== "member") {
|
|
4490
|
+
throw new GatewayCallError("A role is admin or member.", TaErrorCode.INVALID_ARGUMENT, "Owners are those of TA_GATEWAY_ADMINS.");
|
|
4491
|
+
}
|
|
4492
|
+
if (this.policy.role(id) === "owner") {
|
|
4493
|
+
throw GatewayServiceUtil.forbidden("An owner stays one: they are of TA_GATEWAY_ADMINS.", "Change TA_GATEWAY_ADMINS where the gateway runs.");
|
|
4494
|
+
}
|
|
4495
|
+
if (id === caller.account || !this.tenants.list().some((tenant) => tenant.account === id)) {
|
|
4496
|
+
throw new GatewayCallError(`${id} has not signed in to the gateway.`, TaErrorCode.INVALID_ARGUMENT, "Make an account that signed in an admin.");
|
|
4497
|
+
}
|
|
4498
|
+
this.policy.setRole(id, role);
|
|
4499
|
+
return this.policyView();
|
|
4500
|
+
}
|
|
4501
|
+
accessToken(caller, label) {
|
|
4502
|
+
var _a;
|
|
4503
|
+
const tenant = (_a = this.tenants.get(caller.tenant.id)) !== null && _a !== undefined ? _a : caller.tenant;
|
|
4504
|
+
const device = typeof label === "string" && label.trim() ? label.trim().slice(0, 60) : "Another Ta";
|
|
4505
|
+
const token = this.flow.store.signIn({ id: caller.account, name: tenant.name, avatar: tenant.avatar }, device);
|
|
4506
|
+
console.log(`ta gateway: ${caller.account} made a token for ${device}`);
|
|
4507
|
+
return { token, url: this.meta.publicUrl };
|
|
4508
|
+
}
|
|
4509
|
+
access(caller) {
|
|
4510
|
+
const providers = this.flow.providers.map(({ id, name }) => ({ id, name }));
|
|
4511
|
+
const info = this.flow.store.info(caller.token);
|
|
4512
|
+
if (caller.admin) {
|
|
4513
|
+
return { providers, canManage: true, ...info };
|
|
4514
|
+
}
|
|
4515
|
+
return { providers, canManage: false, accounts: [], requests: [], signIns: info.signIns.filter((signIn) => signIn.account === caller.account) };
|
|
4516
|
+
}
|
|
4517
|
+
allow(account, name) {
|
|
4518
|
+
if (typeof account !== "string" || !/^[a-z][a-z0-9-]*:\S+$/.test(account.trim())) {
|
|
4519
|
+
throw new GatewayCallError(`${account} is not an account.`, TaErrorCode.INVALID_ARGUMENT, "Write it <provider>:<login or address>, such as github:octocat.");
|
|
4520
|
+
}
|
|
4521
|
+
this.flow.store.allow(account.trim(), typeof name === "string" ? name : undefined);
|
|
2860
4522
|
}
|
|
2861
|
-
async disallow(account) {
|
|
2862
|
-
|
|
4523
|
+
async disallow(caller, account) {
|
|
4524
|
+
const normalized = AccessUtil.normalize(account);
|
|
4525
|
+
const role = this.policy.role(normalized);
|
|
4526
|
+
if (role === "owner" || role === "admin" && caller.role !== "owner") {
|
|
4527
|
+
throw GatewayServiceUtil.forbidden(role === "owner" ? "Owners are let in where the gateway runs (TA_GATEWAY_ADMINS)." : "Only owners remove an admin.", "Ask an owner of the gateway.");
|
|
4528
|
+
}
|
|
4529
|
+
if (role === "admin") {
|
|
4530
|
+
this.policy.setRole(normalized, "member");
|
|
4531
|
+
}
|
|
4532
|
+
this.flow.store.disallow(normalized);
|
|
2863
4533
|
this.hooks.cutOff();
|
|
2864
|
-
for (const tenant of this.tenants.list().filter((one) => !this.flow.store.allows(one.account))) {
|
|
4534
|
+
for (const tenant of this.tenants.list().filter((one) => !this.flow.store.allows(one.account) && !this.policy.lets(one.account))) {
|
|
2865
4535
|
const unpaired = await this.environments.revoke(tenant).catch((error) => {
|
|
2866
4536
|
console.warn(`ta gateway: revoking ${tenant.account} failed: ${error.message}`);
|
|
2867
4537
|
return 0;
|
|
@@ -2872,33 +4542,42 @@ class GatewayService {
|
|
|
2872
4542
|
async signOut(caller, id) {
|
|
2873
4543
|
const own = this.flow.store.info(caller.token).signIns.find((signIn) => signIn.id === id);
|
|
2874
4544
|
if (!caller.admin && (own === null || own === undefined ? undefined : own.account) !== caller.account) {
|
|
2875
|
-
throw
|
|
4545
|
+
throw GatewayServiceUtil.forbidden("That sign-in is not yours.", "Sign out of the browsers of your own account.");
|
|
2876
4546
|
}
|
|
2877
4547
|
this.flow.store.signOut(id);
|
|
2878
4548
|
this.hooks.cutOff();
|
|
2879
4549
|
}
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
4550
|
+
signInLink(account, name) {
|
|
4551
|
+
const id = typeof account === "string" ? account.trim() : "";
|
|
4552
|
+
if (!/^[a-z][a-z0-9-]*:\S+$/.test(id)) {
|
|
4553
|
+
throw new GatewayCallError(`${account} is not an account.`, TaErrorCode.INVALID_ARGUMENT, "Write it <kind>:<name>, such as github:octocat or user:alice.");
|
|
2883
4554
|
}
|
|
2884
|
-
|
|
4555
|
+
if (this.tenants.list().some((tenant) => tenant.account === id) || this.policy.role(id) !== "member") {
|
|
4556
|
+
throw GatewayServiceUtil.forbidden(`${id} has been here already: a link would sign whoever opens it in as them.`, "Invite accounts that are new; one that has been here signs in its own way.");
|
|
4557
|
+
}
|
|
4558
|
+
return this.meta.links.create(id, typeof name === "string" && name.trim() ? name.trim() : undefined);
|
|
2885
4559
|
}
|
|
2886
4560
|
}
|
|
2887
4561
|
|
|
2888
4562
|
// ../../ta-packages/ta-gateway/esm/node/gateway-socket.js
|
|
2889
4563
|
var MAX_QUEUED = 256;
|
|
2890
|
-
var
|
|
4564
|
+
var MAX_QUEUED_BYTES2 = 16 * 1024 * 1024;
|
|
4565
|
+
var FLOOD_MESSAGES = 2000;
|
|
4566
|
+
var FLOOD_WINDOW_MS = 1e4;
|
|
2891
4567
|
|
|
2892
4568
|
class GatewaySocket {
|
|
2893
|
-
constructor(browser, caller, environments, service) {
|
|
4569
|
+
constructor(browser, caller, environments, service, quotas) {
|
|
2894
4570
|
this.browser = browser;
|
|
2895
4571
|
this.caller = caller;
|
|
2896
4572
|
this.environments = environments;
|
|
2897
4573
|
this.service = service;
|
|
4574
|
+
this.quotas = quotas;
|
|
2898
4575
|
this.queued = [];
|
|
2899
4576
|
this.queuedBytes = 0;
|
|
2900
4577
|
this.closed = false;
|
|
2901
4578
|
this.answering = 0;
|
|
4579
|
+
this.windowStart = Date.now();
|
|
4580
|
+
this.windowCount = 0;
|
|
2902
4581
|
this.release = environments.connect(caller.tenant.id);
|
|
2903
4582
|
browser.onMessage((text) => this.fromBrowser(text));
|
|
2904
4583
|
browser.onClose(() => this.dispose());
|
|
@@ -2947,6 +4626,18 @@ class GatewaySocket {
|
|
|
2947
4626
|
};
|
|
2948
4627
|
}
|
|
2949
4628
|
fromBrowser(text) {
|
|
4629
|
+
const now = Date.now();
|
|
4630
|
+
if (now - this.windowStart > FLOOD_WINDOW_MS) {
|
|
4631
|
+
this.windowStart = now;
|
|
4632
|
+
this.windowCount = 0;
|
|
4633
|
+
}
|
|
4634
|
+
if (++this.windowCount > FLOOD_MESSAGES) {
|
|
4635
|
+
this.close(1008, "Too many messages.");
|
|
4636
|
+
return;
|
|
4637
|
+
}
|
|
4638
|
+
if (this.failure === undefined && GatewayServiceUtil.mayPrompt(text) && !this.admitPrompt(text)) {
|
|
4639
|
+
return;
|
|
4640
|
+
}
|
|
2950
4641
|
if (this.failure === undefined && !GatewayServiceUtil.mayOwn(text)) {
|
|
2951
4642
|
this.forward(text);
|
|
2952
4643
|
return;
|
|
@@ -2970,11 +4661,30 @@ class GatewaySocket {
|
|
|
2970
4661
|
}
|
|
2971
4662
|
this.forward(text);
|
|
2972
4663
|
}
|
|
4664
|
+
admitPrompt(text) {
|
|
4665
|
+
let message;
|
|
4666
|
+
try {
|
|
4667
|
+
message = JSON.parse(text);
|
|
4668
|
+
} catch (_a) {
|
|
4669
|
+
return true;
|
|
4670
|
+
}
|
|
4671
|
+
if (typeof message.method !== "string" || !GatewayServiceUtil.PROMPTS.includes(message.method) || !this.quotas) {
|
|
4672
|
+
return true;
|
|
4673
|
+
}
|
|
4674
|
+
if (this.quotas.takePrompt(this.caller.tenant)) {
|
|
4675
|
+
return true;
|
|
4676
|
+
}
|
|
4677
|
+
const seconds = Math.ceil(this.quotas.retryAfter(this.caller.tenant) / 1000);
|
|
4678
|
+
if (message.id !== undefined) {
|
|
4679
|
+
this.browser.send(GatewayServiceUtil.error(message.id, `You sent as many messages this minute as your account may (${this.quotas.limits(this.caller.tenant).promptsPerMinute}).`, TaErrorCode.RATE_LIMITED, `Send it again in ${seconds} s.`));
|
|
4680
|
+
}
|
|
4681
|
+
return false;
|
|
4682
|
+
}
|
|
2973
4683
|
forward(text) {
|
|
2974
4684
|
var _a;
|
|
2975
4685
|
if (((_a = this.inner) === null || _a === undefined ? undefined : _a.readyState) === WebSocket.OPEN) {
|
|
2976
4686
|
this.inner.send(text);
|
|
2977
|
-
} else if (this.queued.length < MAX_QUEUED && this.queuedBytes + text.length <=
|
|
4687
|
+
} else if (this.queued.length < MAX_QUEUED && this.queuedBytes + text.length <= MAX_QUEUED_BYTES2) {
|
|
2978
4688
|
this.queued.push(text);
|
|
2979
4689
|
this.queuedBytes += text.length;
|
|
2980
4690
|
} else {
|
|
@@ -3018,10 +4728,34 @@ class GatewaySocket {
|
|
|
3018
4728
|
|
|
3019
4729
|
// ../../ta-packages/ta-gateway/esm/node/local-sandbox.js
|
|
3020
4730
|
import { spawn as spawn2 } from "node:child_process";
|
|
3021
|
-
import { mkdirSync as
|
|
4731
|
+
import { mkdirSync as mkdirSync4 } from "node:fs";
|
|
3022
4732
|
import { createServer } from "node:net";
|
|
3023
|
-
import { join as
|
|
4733
|
+
import { join as join7 } from "node:path";
|
|
3024
4734
|
var STOP_TIMEOUT_MS = 1e4;
|
|
4735
|
+
var KEPT_LINES = 2000;
|
|
4736
|
+
|
|
4737
|
+
class LineTail {
|
|
4738
|
+
constructor(max = KEPT_LINES) {
|
|
4739
|
+
this.max = max;
|
|
4740
|
+
this.lines = [];
|
|
4741
|
+
this.partial = "";
|
|
4742
|
+
}
|
|
4743
|
+
write(chunk) {
|
|
4744
|
+
var _a;
|
|
4745
|
+
const parts = (this.partial + chunk).split(`
|
|
4746
|
+
`);
|
|
4747
|
+
this.partial = (_a = parts.pop()) !== null && _a !== undefined ? _a : "";
|
|
4748
|
+
this.lines.push(...parts);
|
|
4749
|
+
if (this.lines.length > this.max) {
|
|
4750
|
+
this.lines.splice(0, this.lines.length - this.max);
|
|
4751
|
+
}
|
|
4752
|
+
}
|
|
4753
|
+
last(count) {
|
|
4754
|
+
const lines = this.partial ? [...this.lines, this.partial] : this.lines;
|
|
4755
|
+
return lines.slice(-count).join(`
|
|
4756
|
+
`);
|
|
4757
|
+
}
|
|
4758
|
+
}
|
|
3025
4759
|
|
|
3026
4760
|
class LocalSandbox {
|
|
3027
4761
|
constructor(id, config, context) {
|
|
@@ -3029,29 +4763,36 @@ class LocalSandbox {
|
|
|
3029
4763
|
this.context = context;
|
|
3030
4764
|
this.type = "local";
|
|
3031
4765
|
this.running = new Map;
|
|
4766
|
+
this.output = new Map;
|
|
3032
4767
|
this.name = SandboxProviders.string(config, "name", "Local");
|
|
4768
|
+
this.gatewayUrl = SandboxProviders.string(config, "gatewayUrl");
|
|
3033
4769
|
}
|
|
3034
4770
|
async start(tenant) {
|
|
4771
|
+
var _a, _b;
|
|
3035
4772
|
const running = this.running.get(tenant.id);
|
|
3036
4773
|
if (running && running.child.exitCode === null) {
|
|
3037
4774
|
return { url: running.url, token: tenant.token };
|
|
3038
4775
|
}
|
|
3039
|
-
const root =
|
|
3040
|
-
const agentDir =
|
|
3041
|
-
const cwd =
|
|
3042
|
-
|
|
3043
|
-
|
|
4776
|
+
const root = join7(this.context.dir, "local", tenant.id);
|
|
4777
|
+
const agentDir = join7(root, "agent");
|
|
4778
|
+
const cwd = join7(root, "workspace");
|
|
4779
|
+
mkdirSync4(agentDir, { recursive: true });
|
|
4780
|
+
mkdirSync4(cwd, { recursive: true });
|
|
3044
4781
|
const port = await LocalSandbox.freePort();
|
|
3045
4782
|
const [command, ...args] = this.context.taCommand;
|
|
3046
4783
|
const child = spawn2(command, [...args, "serve", "--port", String(port)], {
|
|
3047
4784
|
cwd,
|
|
3048
|
-
env: { ...process.env, TA_CODING_AGENT_DIR: agentDir, TA_SERVE_TOKEN: tenant.token, CELL_SERVER__HOST: "127.0.0.1" },
|
|
3049
|
-
stdio: "ignore"
|
|
4785
|
+
env: { ...process.env, ...tenant.env, TA_CODING_AGENT_DIR: agentDir, TA_SERVE_TOKEN: tenant.token, CELL_SERVER__HOST: "127.0.0.1" },
|
|
4786
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
3050
4787
|
});
|
|
4788
|
+
const tail = new LineTail;
|
|
4789
|
+
this.output.set(tenant.id, tail);
|
|
4790
|
+
(_a = child.stdout) === null || _a === undefined || _a.setEncoding("utf8").on("data", (data) => tail.write(data));
|
|
4791
|
+
(_b = child.stderr) === null || _b === undefined || _b.setEncoding("utf8").on("data", (data) => tail.write(data));
|
|
3051
4792
|
const url = `http://127.0.0.1:${port}`;
|
|
3052
4793
|
const gone = () => {
|
|
3053
|
-
var
|
|
3054
|
-
if (((
|
|
4794
|
+
var _a2;
|
|
4795
|
+
if (((_a2 = this.running.get(tenant.id)) === null || _a2 === undefined ? undefined : _a2.child) === child) {
|
|
3055
4796
|
this.running.delete(tenant.id);
|
|
3056
4797
|
}
|
|
3057
4798
|
};
|
|
@@ -3080,6 +4821,11 @@ class LocalSandbox {
|
|
|
3080
4821
|
async state(tenant) {
|
|
3081
4822
|
return this.running.has(tenant.id) ? "running" : "stopped";
|
|
3082
4823
|
}
|
|
4824
|
+
async logs(tenant, lines) {
|
|
4825
|
+
var _a;
|
|
4826
|
+
var _b;
|
|
4827
|
+
return (_b = (_a = this.output.get(tenant.id)) === null || _a === undefined ? undefined : _a.last(lines)) !== null && _b !== undefined ? _b : "";
|
|
4828
|
+
}
|
|
3083
4829
|
async dispose() {
|
|
3084
4830
|
await Promise.all([...this.running.keys()].map((id) => this.stop({ id, account: "", token: "" })));
|
|
3085
4831
|
}
|
|
@@ -3096,94 +4842,15 @@ class LocalSandbox {
|
|
|
3096
4842
|
}
|
|
3097
4843
|
|
|
3098
4844
|
// ../../ta-packages/ta-gateway/esm/node/sign-in-links.js
|
|
3099
|
-
import { createHash as
|
|
3100
|
-
import { readFileSync as
|
|
3101
|
-
import { join as
|
|
3102
|
-
|
|
3103
|
-
// ../../ta-packages/ta-gateway/esm/node/tenant-store.js
|
|
3104
|
-
import { createHash as createHash4, randomBytes as randomBytes3 } from "node:crypto";
|
|
3105
|
-
import { chmodSync as chmodSync2, mkdirSync as mkdirSync3, readdirSync, readFileSync as readFileSync3, renameSync as renameSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
3106
|
-
import { join as join3 } from "node:path";
|
|
3107
|
-
function writePrivate(file, data) {
|
|
3108
|
-
const temporary = `${file}.${process.pid}.tmp`;
|
|
3109
|
-
writeFileSync2(temporary, JSON.stringify(data, undefined, 2), { mode: 384 });
|
|
3110
|
-
chmodSync2(temporary, 384);
|
|
3111
|
-
renameSync2(temporary, file);
|
|
3112
|
-
}
|
|
3113
|
-
var TenantUtil;
|
|
3114
|
-
(function(TenantUtil2) {
|
|
3115
|
-
function id(account) {
|
|
3116
|
-
return createHash4("sha256").update(account).digest("hex").slice(0, 16);
|
|
3117
|
-
}
|
|
3118
|
-
TenantUtil2.id = id;
|
|
3119
|
-
})(TenantUtil || (TenantUtil = {}));
|
|
3120
|
-
|
|
3121
|
-
class TenantStore {
|
|
3122
|
-
constructor(root) {
|
|
3123
|
-
this.cache = new Map;
|
|
3124
|
-
this.dir = join3(root, "tenants");
|
|
3125
|
-
mkdirSync3(this.dir, { recursive: true, mode: 448 });
|
|
3126
|
-
}
|
|
3127
|
-
get(id) {
|
|
3128
|
-
if (!/^[0-9a-f]{16}$/.test(id)) {
|
|
3129
|
-
return;
|
|
3130
|
-
}
|
|
3131
|
-
let tenant = this.cache.get(id);
|
|
3132
|
-
if (!tenant) {
|
|
3133
|
-
try {
|
|
3134
|
-
tenant = JSON.parse(readFileSync3(join3(this.dir, `${id}.json`), "utf8"));
|
|
3135
|
-
this.cache.set(id, tenant);
|
|
3136
|
-
} catch (_a) {
|
|
3137
|
-
return;
|
|
3138
|
-
}
|
|
3139
|
-
}
|
|
3140
|
-
return tenant;
|
|
3141
|
-
}
|
|
3142
|
-
ensure(account, now = Date.now()) {
|
|
3143
|
-
const id = TenantUtil.id(account.id);
|
|
3144
|
-
const known = this.get(id);
|
|
3145
|
-
if (known) {
|
|
3146
|
-
if (account.name && (known.name !== account.name || known.avatar !== account.avatar)) {
|
|
3147
|
-
return this.update(id, { name: account.name, avatar: account.avatar });
|
|
3148
|
-
}
|
|
3149
|
-
return known;
|
|
3150
|
-
}
|
|
3151
|
-
const tenant = {
|
|
3152
|
-
id,
|
|
3153
|
-
account: account.id,
|
|
3154
|
-
name: account.name,
|
|
3155
|
-
avatar: account.avatar,
|
|
3156
|
-
token: randomBytes3(32).toString("base64url"),
|
|
3157
|
-
createdAt: now
|
|
3158
|
-
};
|
|
3159
|
-
this.save(tenant);
|
|
3160
|
-
return tenant;
|
|
3161
|
-
}
|
|
3162
|
-
update(id, change) {
|
|
3163
|
-
const tenant = this.get(id);
|
|
3164
|
-
if (!tenant) {
|
|
3165
|
-
throw new Error(`No tenant ${id}`);
|
|
3166
|
-
}
|
|
3167
|
-
const next = { ...tenant, ...change };
|
|
3168
|
-
this.save(next);
|
|
3169
|
-
return next;
|
|
3170
|
-
}
|
|
3171
|
-
list() {
|
|
3172
|
-
return readdirSync(this.dir).filter((name) => /^[0-9a-f]{16}\.json$/.test(name)).map((name) => this.get(name.slice(0, -5))).filter((tenant) => tenant !== undefined);
|
|
3173
|
-
}
|
|
3174
|
-
save(tenant) {
|
|
3175
|
-
writePrivate(join3(this.dir, `${tenant.id}.json`), tenant);
|
|
3176
|
-
this.cache.set(tenant.id, tenant);
|
|
3177
|
-
}
|
|
3178
|
-
}
|
|
3179
|
-
|
|
3180
|
-
// ../../ta-packages/ta-gateway/esm/node/sign-in-links.js
|
|
4845
|
+
import { createHash as createHash6, randomBytes as randomBytes4 } from "node:crypto";
|
|
4846
|
+
import { readFileSync as readFileSync8 } from "node:fs";
|
|
4847
|
+
import { join as join8 } from "node:path";
|
|
3181
4848
|
var SIGN_IN_LINK_PATH = "/sign-in/link/";
|
|
3182
|
-
var hash = (secret) =>
|
|
4849
|
+
var hash = (secret) => createHash6("sha256").update(secret).digest("base64url");
|
|
3183
4850
|
|
|
3184
4851
|
class SignInLinks {
|
|
3185
4852
|
constructor(dir) {
|
|
3186
|
-
this.file =
|
|
4853
|
+
this.file = join8(dir, "links.json");
|
|
3187
4854
|
}
|
|
3188
4855
|
create(account, name, ttlMs = 24 * 3600 * 1000, now = Date.now()) {
|
|
3189
4856
|
if (!/^[a-z][a-z0-9-]*:\S+$/.test(account)) {
|
|
@@ -3205,7 +4872,7 @@ class SignInLinks {
|
|
|
3205
4872
|
}
|
|
3206
4873
|
read(now) {
|
|
3207
4874
|
try {
|
|
3208
|
-
const links = JSON.parse(
|
|
4875
|
+
const links = JSON.parse(readFileSync8(this.file, "utf8"));
|
|
3209
4876
|
return Array.isArray(links) ? links.filter((link) => link.expiresAt > now) : [];
|
|
3210
4877
|
} catch (_a) {
|
|
3211
4878
|
return [];
|
|
@@ -3214,6 +4881,7 @@ class SignInLinks {
|
|
|
3214
4881
|
}
|
|
3215
4882
|
|
|
3216
4883
|
// ../../ta-packages/ta-gateway/esm/node/gateway-server.js
|
|
4884
|
+
var TICK_MS = 30000;
|
|
3217
4885
|
var FILES_COOKIE = "ta_gateway";
|
|
3218
4886
|
var TYPES = {
|
|
3219
4887
|
".html": "text/html; charset=utf-8",
|
|
@@ -3237,6 +4905,28 @@ var SANDBOX_TYPES = {
|
|
|
3237
4905
|
};
|
|
3238
4906
|
var GatewayServerUtil;
|
|
3239
4907
|
(function(GatewayServerUtil2) {
|
|
4908
|
+
function reach(type, port, publicUrl) {
|
|
4909
|
+
if (type === "docker") {
|
|
4910
|
+
return `http://host.docker.internal:${port}`;
|
|
4911
|
+
}
|
|
4912
|
+
if (type === "local" || !publicUrl) {
|
|
4913
|
+
return `http://127.0.0.1:${port}`;
|
|
4914
|
+
}
|
|
4915
|
+
return publicUrl;
|
|
4916
|
+
}
|
|
4917
|
+
GatewayServerUtil2.reach = reach;
|
|
4918
|
+
GatewayServerUtil2.EXEC_PATH = "/api/exec";
|
|
4919
|
+
function blockedReason(block) {
|
|
4920
|
+
switch (block) {
|
|
4921
|
+
case "suspended":
|
|
4922
|
+
return "Your account is suspended by an administrator of the gateway.";
|
|
4923
|
+
case "minutes":
|
|
4924
|
+
return "Your free minutes of this month are used up.";
|
|
4925
|
+
default:
|
|
4926
|
+
return;
|
|
4927
|
+
}
|
|
4928
|
+
}
|
|
4929
|
+
GatewayServerUtil2.blockedReason = blockedReason;
|
|
3240
4930
|
function file(root, urlPath) {
|
|
3241
4931
|
let path;
|
|
3242
4932
|
try {
|
|
@@ -3244,11 +4934,11 @@ var GatewayServerUtil;
|
|
|
3244
4934
|
} catch (_a) {
|
|
3245
4935
|
path = "/";
|
|
3246
4936
|
}
|
|
3247
|
-
const absolute = normalize(
|
|
3248
|
-
if ((absolute === root || absolute.startsWith(root.endsWith(sep) ? root : root + sep)) &&
|
|
4937
|
+
const absolute = normalize(join9(root, path));
|
|
4938
|
+
if ((absolute === root || absolute.startsWith(root.endsWith(sep) ? root : root + sep)) && existsSync4(absolute) && statSync(absolute).isFile()) {
|
|
3249
4939
|
return absolute;
|
|
3250
4940
|
}
|
|
3251
|
-
return
|
|
4941
|
+
return join9(root, "index.html");
|
|
3252
4942
|
}
|
|
3253
4943
|
GatewayServerUtil2.file = file;
|
|
3254
4944
|
function filesTarget(requestUrl, endpoint) {
|
|
@@ -3265,20 +4955,22 @@ var GatewayServerUtil;
|
|
|
3265
4955
|
|
|
3266
4956
|
class GatewayServer {
|
|
3267
4957
|
static async create(options) {
|
|
3268
|
-
|
|
4958
|
+
const models = ModelProxyUtil.read(options.dir);
|
|
4959
|
+
mkdirSync5(options.dir, { recursive: true, mode: 448 });
|
|
3269
4960
|
const config = SandboxProviders.read(options.dir);
|
|
3270
4961
|
const context = { dir: options.dir, taCommand: options.taCommand };
|
|
3271
4962
|
const providers = await SandboxProviders.create(config, context, SANDBOX_TYPES);
|
|
3272
|
-
return new GatewayServer(options, providers, config);
|
|
4963
|
+
return new GatewayServer(options, providers, config, models);
|
|
3273
4964
|
}
|
|
3274
|
-
constructor(options, providers, config) {
|
|
4965
|
+
constructor(options, providers, config, models = {}) {
|
|
3275
4966
|
var _a, _b;
|
|
3276
4967
|
this.options = options;
|
|
3277
4968
|
this.sockets = new Set;
|
|
3278
4969
|
const env = (_a = options.env) !== null && _a !== undefined ? _a : process.env;
|
|
3279
|
-
|
|
3280
|
-
|
|
3281
|
-
|
|
4970
|
+
const owners = AccessUtil.parseList(env.TA_GATEWAY_ADMINS);
|
|
4971
|
+
this.policy = new GatewayPolicy(options.dir, owners);
|
|
4972
|
+
const allowed = [...AccessUtil.parseList(env.TA_ALLOWED_USERS), ...owners];
|
|
4973
|
+
this.flow = new SignInFlow(SignInProviders.fromEnv(env), new AccessStore(join9(options.dir, "access.json"), allowed), SignInProviders.publicUrl(env));
|
|
3282
4974
|
this.tenants = new TenantStore(options.dir);
|
|
3283
4975
|
this.links = new SignInLinks(options.dir);
|
|
3284
4976
|
this.environments = new Environments(providers, this.tenants, {
|
|
@@ -3286,25 +4978,73 @@ class GatewayServer {
|
|
|
3286
4978
|
own: config.ownEnvironments !== false,
|
|
3287
4979
|
idleMinutes: (_b = config.idleMinutes) !== null && _b !== undefined ? _b : 0
|
|
3288
4980
|
});
|
|
3289
|
-
this.
|
|
4981
|
+
this.usage = new GatewayUsage(options.dir);
|
|
4982
|
+
this.audit = new GatewayAudit(options.dir);
|
|
4983
|
+
this.quotas = new GatewayQuotas(this.policy, this.tenants, this.usage, () => this.environments.settings.own, () => this.models.enabled);
|
|
4984
|
+
this.environments.guard = (tenant) => GatewayServerUtil.blockedReason(this.quotas.blocked(tenant));
|
|
4985
|
+
this.models = new ModelProxy(models, {
|
|
4986
|
+
tenantOfKey: (key) => this.tenants.list().find((tenant) => tenant.modelKey && ModelProxyUtil.sameKey(key, tenant.modelKey)),
|
|
4987
|
+
refuse: (id) => {
|
|
4988
|
+
const tenant = this.tenants.get(id);
|
|
4989
|
+
return tenant ? this.quotas.refuseModels(tenant) : "No such account.";
|
|
4990
|
+
},
|
|
4991
|
+
record: (id, usage, cost) => this.quotas.addTokens(id, ModelProxyUtil.total(usage), cost)
|
|
4992
|
+
}, env);
|
|
4993
|
+
this.environments.envOf = (tenant, provider) => {
|
|
4994
|
+
var _a2, _b2, _c;
|
|
4995
|
+
if (!this.models.enabled) {
|
|
4996
|
+
return;
|
|
4997
|
+
}
|
|
4998
|
+
const url = (_a2 = provider.gatewayUrl) !== null && _a2 !== undefined ? _a2 : GatewayServerUtil.reach(provider.type, (_c = (_b2 = this.port) !== null && _b2 !== undefined ? _b2 : options.port) !== null && _c !== undefined ? _c : 4100, SandboxProviders.publicBase(env));
|
|
4999
|
+
return { TA_MODEL_PROXY_URL: url.replace(/\/+$/, ""), TA_MODEL_PROXY_KEY: this.tenants.modelKey(tenant.id) };
|
|
5000
|
+
};
|
|
5001
|
+
this.service = new GatewayService({
|
|
5002
|
+
flow: this.flow,
|
|
5003
|
+
tenants: this.tenants,
|
|
5004
|
+
environments: this.environments,
|
|
5005
|
+
policy: this.policy,
|
|
5006
|
+
quotas: this.quotas,
|
|
5007
|
+
usage: this.usage,
|
|
5008
|
+
audit: this.audit,
|
|
5009
|
+
hooks: { cutOff: () => this.cutOff(), reset: (id) => this.reset(id) },
|
|
5010
|
+
models: this.models,
|
|
5011
|
+
meta: { links: this.links, startedAt: Date.now(), version: options.version, publicUrl: SandboxProviders.publicBase(env) }
|
|
5012
|
+
});
|
|
3290
5013
|
}
|
|
3291
5014
|
isAdmin(account) {
|
|
3292
|
-
return this.
|
|
5015
|
+
return this.policy.role(account) !== "member";
|
|
5016
|
+
}
|
|
5017
|
+
async tick(now = Date.now()) {
|
|
5018
|
+
for (const tenant of this.quotas.tick(this.environments.runningIds(), now)) {
|
|
5019
|
+
console.log(`ta gateway: ${tenant.account} used its free minutes of the month: its sandbox stops`);
|
|
5020
|
+
await this.environments.stop(tenant.id).catch(() => {
|
|
5021
|
+
return;
|
|
5022
|
+
});
|
|
5023
|
+
this.reset(tenant.id);
|
|
5024
|
+
}
|
|
3293
5025
|
}
|
|
3294
5026
|
listen() {
|
|
5027
|
+
var _a, _b;
|
|
3295
5028
|
const server = createServer2((request2, response) => void this.handle(request2, response));
|
|
3296
5029
|
server.on("upgrade", (request2, socket, head) => this.upgrade(request2, socket, head));
|
|
3297
5030
|
this.server = server;
|
|
5031
|
+
this.ticker = setInterval(() => void this.tick().catch((error) => console.warn(`ta gateway: counting minutes failed: ${error.message}`)), TICK_MS);
|
|
5032
|
+
(_b = (_a = this.ticker).unref) === null || _b === undefined || _b.call(_a);
|
|
3298
5033
|
return new Promise((resolve, reject) => {
|
|
3299
|
-
var
|
|
5034
|
+
var _a2, _b2;
|
|
3300
5035
|
server.once("error", reject);
|
|
3301
|
-
server.listen((
|
|
5036
|
+
server.listen((_a2 = this.options.port) !== null && _a2 !== undefined ? _a2 : 4100, (_b2 = this.options.host) !== null && _b2 !== undefined ? _b2 : "127.0.0.1", () => {
|
|
3302
5037
|
const { address, port } = server.address();
|
|
5038
|
+
this.port = port;
|
|
3303
5039
|
resolve(`http://${address.includes(":") ? `[${address}]` : address}:${port}`);
|
|
3304
5040
|
});
|
|
3305
5041
|
});
|
|
3306
5042
|
}
|
|
3307
5043
|
async close() {
|
|
5044
|
+
clearInterval(this.ticker);
|
|
5045
|
+
await this.tick().catch(() => {
|
|
5046
|
+
return;
|
|
5047
|
+
});
|
|
3308
5048
|
for (const socket of this.sockets) {
|
|
3309
5049
|
socket.close(1001, "The gateway is stopping.");
|
|
3310
5050
|
}
|
|
@@ -3332,7 +5072,8 @@ class GatewayServer {
|
|
|
3332
5072
|
return;
|
|
3333
5073
|
}
|
|
3334
5074
|
const tenant = this.tenants.ensure({ id: signIn.account, name: signIn.name, avatar: signIn.avatar });
|
|
3335
|
-
|
|
5075
|
+
const role = this.policy.role(signIn.account);
|
|
5076
|
+
return { token, account: signIn.account, tenant, role, admin: role !== "member" };
|
|
3336
5077
|
}
|
|
3337
5078
|
async handle(request2, response) {
|
|
3338
5079
|
var _a;
|
|
@@ -3345,6 +5086,8 @@ class GatewayServer {
|
|
|
3345
5086
|
name: "ta gateway",
|
|
3346
5087
|
onSignedIn: (res, token, account, origin) => this.signedIn(res, token, account, origin)
|
|
3347
5088
|
});
|
|
5089
|
+
} else if (path === ModelProxyUtil.PATH || path.startsWith(`${ModelProxyUtil.PATH}/`)) {
|
|
5090
|
+
await this.models.handle(request2, response);
|
|
3348
5091
|
} else if (path.startsWith(`${TA_FILES_PATH}/`)) {
|
|
3349
5092
|
await this.files(request2, response);
|
|
3350
5093
|
} else if (path === "/api" || path.startsWith("/api/")) {
|
|
@@ -3449,7 +5192,7 @@ class GatewayServer {
|
|
|
3449
5192
|
upgrade(request2, socket, head) {
|
|
3450
5193
|
var _a, _b;
|
|
3451
5194
|
const url = new URL((_a = request2.url) !== null && _a !== undefined ? _a : "/", "http://gateway");
|
|
3452
|
-
if (url.pathname !== TA_WEBSOCKET_PATH) {
|
|
5195
|
+
if (url.pathname !== TA_WEBSOCKET_PATH && url.pathname !== GatewayServerUtil.EXEC_PATH) {
|
|
3453
5196
|
socket.destroy();
|
|
3454
5197
|
return;
|
|
3455
5198
|
}
|
|
@@ -3465,9 +5208,24 @@ class GatewayServer {
|
|
|
3465
5208
|
WebSocketConnection.refuse(socket, 401, "Not authorized.");
|
|
3466
5209
|
return;
|
|
3467
5210
|
}
|
|
5211
|
+
const most = this.quotas.limits(caller.tenant).connections;
|
|
5212
|
+
if (most !== undefined && this.environments.connections(caller.tenant.id) >= most) {
|
|
5213
|
+
WebSocketConnection.refuse(socket, 429, `At most ${most} pages of your account connect at once.`);
|
|
5214
|
+
return;
|
|
5215
|
+
}
|
|
3468
5216
|
this.tenants.update(caller.tenant.id, { lastActiveAt: Date.now() });
|
|
5217
|
+
if (url.pathname === GatewayServerUtil.EXEC_PATH) {
|
|
5218
|
+
const blocked = GatewayServerUtil.blockedReason(this.quotas.blocked(caller.tenant));
|
|
5219
|
+
if (blocked) {
|
|
5220
|
+
WebSocketConnection.refuse(socket, 403, blocked);
|
|
5221
|
+
return;
|
|
5222
|
+
}
|
|
5223
|
+
const exec = WebSocketConnection.open(socket, checked.key, head, { maxPayload: 64 * 1024 * 1024, heartbeat: 30000 });
|
|
5224
|
+
new GatewayExecProxy(exec, this.environments.ensure(caller.tenant), this.environments.connect(caller.tenant.id));
|
|
5225
|
+
return;
|
|
5226
|
+
}
|
|
3469
5227
|
const connection = WebSocketConnection.open(socket, checked.key, head, { maxPayload: 32 * 1024 * 1024, heartbeat: 30000, deflate: checked.deflate });
|
|
3470
|
-
const gatewaySocket = new GatewaySocket(connection, caller, this.environments, this.service);
|
|
5228
|
+
const gatewaySocket = new GatewaySocket(connection, caller, this.environments, this.service, this.quotas);
|
|
3471
5229
|
this.sockets.add(gatewaySocket);
|
|
3472
5230
|
connection.onClose(() => this.sockets.delete(gatewaySocket));
|
|
3473
5231
|
}
|
|
@@ -3515,7 +5273,7 @@ var GatewayCli;
|
|
|
3515
5273
|
process.stdout.write(HELP);
|
|
3516
5274
|
return;
|
|
3517
5275
|
}
|
|
3518
|
-
const dir = (_b = (_a = values.dir) !== null && _a !== undefined ? _a : process.env.TA_GATEWAY_DIR) !== null && _b !== undefined ? _b :
|
|
5276
|
+
const dir = (_b = (_a = values.dir) !== null && _a !== undefined ? _a : process.env.TA_GATEWAY_DIR) !== null && _b !== undefined ? _b : join10(homedir(), ".ta", "gateway");
|
|
3519
5277
|
if (positionals[0] === "link") {
|
|
3520
5278
|
link(dir, positionals[1], values.name);
|
|
3521
5279
|
return;
|
|
@@ -3532,7 +5290,8 @@ var GatewayCli;
|
|
|
3532
5290
|
host: values.host,
|
|
3533
5291
|
port: Number((_d = (_c = values.port) !== null && _c !== undefined ? _c : process.env.PORT) !== null && _d !== undefined ? _d : 4100),
|
|
3534
5292
|
publicDir: options.publicDir,
|
|
3535
|
-
taCommand: options.taCommand
|
|
5293
|
+
taCommand: options.taCommand,
|
|
5294
|
+
version: options.version
|
|
3536
5295
|
});
|
|
3537
5296
|
} catch (error) {
|
|
3538
5297
|
console.error(`ta gateway: ${error.message}`);
|