@celljs/ta 4.15.1 → 4.15.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/dist/cli.mjs +2 -1
  2. package/dist/gateway.mjs +1320 -178
  3. package/dist/pi/CHANGELOG.md +8 -0
  4. package/dist/pi/package.json +1 -1
  5. package/dist/pi/packages/ta-3d/package.json +1 -1
  6. package/dist/pi/packages/ta-agents/package.json +1 -1
  7. package/dist/pi/packages/ta-bots/host/index.mjs +2 -0
  8. package/dist/pi/packages/ta-bots/package.json +1 -1
  9. package/dist/pi/packages/ta-changes/package.json +1 -1
  10. package/dist/pi/packages/ta-channels/host/index.mjs +3 -0
  11. package/dist/pi/packages/ta-channels/package.json +1 -1
  12. package/dist/pi/packages/ta-computer-use/package.json +1 -1
  13. package/dist/pi/packages/ta-cron/package.json +1 -1
  14. package/dist/pi/packages/ta-extensions/package.json +1 -1
  15. package/dist/pi/packages/ta-extensions/web/index.js +2 -0
  16. package/dist/pi/packages/ta-games/package.json +1 -1
  17. package/dist/pi/packages/ta-glance/package.json +1 -1
  18. package/dist/pi/packages/ta-initiative/package.json +1 -1
  19. package/dist/pi/packages/ta-launcher/package.json +1 -1
  20. package/dist/pi/packages/ta-manual/package.json +1 -1
  21. package/dist/pi/packages/ta-moments/extension/index.mjs +2 -0
  22. package/dist/pi/packages/ta-moments/host/index.mjs +2 -0
  23. package/dist/pi/packages/ta-moments/package.json +1 -1
  24. package/dist/pi/packages/ta-moments/web/index.js +2 -0
  25. package/dist/pi/packages/ta-nearby/package.json +1 -1
  26. package/dist/pi/packages/ta-notify/package.json +1 -1
  27. package/dist/pi/packages/ta-plot/package.json +1 -1
  28. package/dist/pi/packages/ta-recall/package.json +1 -1
  29. package/dist/pi/packages/ta-rooms/package.json +1 -1
  30. package/dist/pi/packages/ta-share/package.json +1 -1
  31. package/dist/pi/packages/ta-sync/package.json +1 -1
  32. package/dist/pi/packages/ta-sync-cloud/package.json +1 -1
  33. package/dist/pi/packages/ta-team/package.json +1 -1
  34. package/dist/pi/packages/ta-workspace/package.json +1 -1
  35. package/dist/pi/packages/ta-workspace/web/index.js +2 -0
  36. package/dist/public/assets/{abnfDiagram-S7VZAUFO-CD8CaArq.js → abnfDiagram-S7VZAUFO-Dd51Bqbx.js} +1 -1
  37. package/dist/public/assets/{architecture-WOLXFQ4H-f2a9bodL.js → architecture-WOLXFQ4H-BbG3_x7_.js} +1 -1
  38. package/dist/public/assets/{architectureDiagram-YGSQEFNW-BCWH9CwO.js → architectureDiagram-YGSQEFNW-DbJO4t8I.js} +1 -1
  39. package/dist/public/assets/{blockDiagram-APKKGZFW-fUamQMeJ.js → blockDiagram-APKKGZFW-BwtrHF1f.js} +1 -1
  40. package/dist/public/assets/{browser-CrMa174j.js → browser-CTbeNBsA.js} +1 -1
  41. package/dist/public/assets/{c4Diagram-KJ2TOQU5-CkLtnwp1.js → c4Diagram-KJ2TOQU5-CfBe5A0H.js} +1 -1
  42. package/dist/public/assets/{chunk-6MUBBAK2-cwcGPuAH.js → chunk-6MUBBAK2-C7R0mhrg.js} +1 -1
  43. package/dist/public/assets/{chunk-AXFCVDMN-DNkmkYfC.js → chunk-AXFCVDMN-VQgQruZT.js} +1 -1
  44. package/dist/public/assets/{chunk-BG4553SI-C8qbbr2Z.js → chunk-BG4553SI-CkUi_vD5.js} +1 -1
  45. package/dist/public/assets/{chunk-DQASFODM-C-PJ2CrJ.js → chunk-DQASFODM-BAQq7ktk.js} +1 -1
  46. package/dist/public/assets/{chunk-HFQBQIA3-BYcgxNEL.js → chunk-HFQBQIA3-B1oF8lB7.js} +1 -1
  47. package/dist/public/assets/{chunk-KQLB2F66-BTDNzrRl.js → chunk-KQLB2F66-DE5amcgT.js} +1 -1
  48. package/dist/public/assets/{chunk-KS47KZWR-1sGg1pKP.js → chunk-KS47KZWR-ev9S_csp.js} +1 -1
  49. package/dist/public/assets/{chunk-LEVMRMFU-7lAsFwHT.js → chunk-LEVMRMFU-ziqnRvTG.js} +1 -1
  50. package/dist/public/assets/{chunk-M57EVK23-C8K-ltig.js → chunk-M57EVK23-DaqPdEKA.js} +1 -1
  51. package/dist/public/assets/{chunk-OY52VR5R-CBe1QL_V.js → chunk-OY52VR5R-DFOG8j6z.js} +1 -1
  52. package/dist/public/assets/{chunk-QB3YPZ4I-COhYyyE_.js → chunk-QB3YPZ4I-eg35JLLN.js} +1 -1
  53. package/dist/public/assets/{chunk-R3KSYZAL-CREAgaSS.js → chunk-R3KSYZAL-J73nie-R.js} +1 -1
  54. package/dist/public/assets/{chunk-STOXU5M4-CTl0P8_C.js → chunk-STOXU5M4-B8EvDZvB.js} +1 -1
  55. package/dist/public/assets/{chunk-TU62E6Q5-B_GG7baF.js → chunk-TU62E6Q5-DyCM007V.js} +1 -1
  56. package/dist/public/assets/{chunk-X4PVCEUW-CDrSpVOj.js → chunk-X4PVCEUW-BBGeCNmA.js} +1 -1
  57. package/dist/public/assets/{chunk-XXDRQBXY-sKeiDEnC.js → chunk-XXDRQBXY-CAtJ2Wph.js} +1 -1
  58. package/dist/public/assets/{classDiagram-v2-L25IHNXF-Cqo1O5FQ.js → classDiagram-v2-L25IHNXF-DhQM6yHy.js} +1 -1
  59. package/dist/public/assets/{cose-bilkent-JH36ORCC-CTSKeRBe.js → cose-bilkent-JH36ORCC-AqIcHabu.js} +1 -1
  60. package/dist/public/assets/cynefin-EF2NZ3EQ-yFytgtiy.js +1 -0
  61. package/dist/public/assets/{cynefinDiagram-L36ESFBY-67xEPyP9.js → cynefinDiagram-L36ESFBY-7SyXzhRd.js} +1 -1
  62. package/dist/public/assets/{d3-selection-BUiX-3qh.js → d3-selection-DsahLieG.js} +1 -1
  63. package/dist/public/assets/{dagre-SPEB5RXH-DDw-u3Kg.js → dagre-SPEB5RXH-DalaGtMS.js} +1 -1
  64. package/dist/public/assets/{diagram-3B7GL73J-BBFInaCs.js → diagram-3B7GL73J-BcY-qB9G.js} +1 -1
  65. package/dist/public/assets/{diagram-DNEMPZML-ZdnFKM7-.js → diagram-DNEMPZML-NV7fbe0h.js} +1 -1
  66. package/dist/public/assets/{diagram-L3MUGCDN-CPb04hOv.js → diagram-L3MUGCDN-BPx4GHoj.js} +1 -1
  67. package/dist/public/assets/{diagram-LA4NIPWU-P9wHFGDS.js → diagram-LA4NIPWU-BeUxqmw1.js} +1 -1
  68. package/dist/public/assets/{diagram-MHCOC4E2-Ce_Cr-t6.js → diagram-MHCOC4E2-Dlg1S5na.js} +1 -1
  69. package/dist/public/assets/{diagram-XPFHEONP-CfS34OHW.js → diagram-XPFHEONP-DQhRoMkK.js} +1 -1
  70. package/dist/public/assets/{dist-6pZqZEaM.js → dist-CQU8zJh_.js} +1 -1
  71. package/dist/public/assets/{ebnfDiagram-UZSYDL2S-BkHabCn2.js → ebnfDiagram-UZSYDL2S-gVEs3Um2.js} +1 -1
  72. package/dist/public/assets/{editor.api-CO4Q0NKb.js → editor.api-SQ1snuat.js} +1 -1
  73. package/dist/public/assets/{elk-XV6XMXQE-JpreOVeR.js → elk-XV6XMXQE-CHtlrPwu.js} +1 -1
  74. package/dist/public/assets/{erDiagram-EYBAPDEH-DDFJwzHx.js → erDiagram-EYBAPDEH-B0xok726.js} +1 -1
  75. package/dist/public/assets/{eventmodeling-K75KTNOO-DsnkF6Zc.js → eventmodeling-K75KTNOO-CFwphA0e.js} +1 -1
  76. package/dist/public/assets/flowDiagram-MALGAFMO-Cwye_DsX.js +1 -0
  77. package/dist/public/assets/{freemarker2-Cm4NK652.js → freemarker2-DJKO-X2B.js} +1 -1
  78. package/dist/public/assets/{ganttDiagram-23REXITY-C7eedoYb.js → ganttDiagram-23REXITY-B5wVGN1A.js} +1 -1
  79. package/dist/public/assets/gitGraph-VSP46ZUC-BYzb--zb.js +1 -0
  80. package/dist/public/assets/{gitGraphDiagram-STJD7YCW-Cuh3ih-z.js → gitGraphDiagram-STJD7YCW-DEjNU1R_.js} +1 -1
  81. package/dist/public/assets/{handlebars-BnE51DUi.js → handlebars-ChVN7JPd.js} +1 -1
  82. package/dist/public/assets/{html-7koekDfa.js → html-CQ0znKSa.js} +1 -1
  83. package/dist/public/assets/{index-CQG1XddL.js → index-CLlacdXR.js} +2 -2
  84. package/dist/public/assets/info-OHQRW6UA-na0qX3JP.js +1 -0
  85. package/dist/public/assets/{infoDiagram-KBXO4MBG-BY3iq7I8.js → infoDiagram-KBXO4MBG-CJtrS72M.js} +1 -1
  86. package/dist/public/assets/{ishikawaDiagram-M35NCXHK-COjlz24y.js → ishikawaDiagram-M35NCXHK-qYI1QBsK.js} +1 -1
  87. package/dist/public/assets/{javascript-BUZgUhdn.js → javascript-B-DHbkyI.js} +1 -1
  88. package/dist/public/assets/{journeyDiagram-OPLOXDXO-DReJE04Q.js → journeyDiagram-OPLOXDXO-BOQ2y2ZZ.js} +1 -1
  89. package/dist/public/assets/{jsonMode-qPeVDBDp.js → jsonMode-XuGC5N_z.js} +1 -1
  90. package/dist/public/assets/{kanban-definition-BZKZHSQY-BB0_ePvg.js → kanban-definition-BZKZHSQY-BJ64tKdP.js} +1 -1
  91. package/dist/public/assets/{liquid-CNKL9dLX.js → liquid-B0Ujylzc.js} +1 -1
  92. package/dist/public/assets/{mdx-D1_UzrBc.js → mdx-B19-V510.js} +1 -1
  93. package/dist/public/assets/{mermaid-parser.core-DiE3z_tV.js → mermaid-parser.core-Scb01iBq.js} +3 -3
  94. package/dist/public/assets/{mermaid.core-CBCRrLdl.js → mermaid.core-BMuvs7us.js} +4 -4
  95. package/dist/public/assets/{mindmap-definition-I2QAS2VK-D-Y5fKl1.js → mindmap-definition-I2QAS2VK-BYfOS3XN.js} +1 -1
  96. package/dist/public/assets/module-B3SKdB70.js +1 -0
  97. package/dist/public/assets/module-DOrYVZ7B.js +2615 -0
  98. package/dist/public/assets/{monaco--czRbHhk.js → monaco-Bbe2os7m.js} +3 -3
  99. package/dist/public/assets/packet-JDAUHWVQ-jhCd70vR.js +1 -0
  100. package/dist/public/assets/{pegDiagram-FD4XPPHP-1jx5jc73.js → pegDiagram-FD4XPPHP-D-Vlq9Gm.js} +1 -1
  101. package/dist/public/assets/pie-XZMESJXO-DV0NJFsI.js +1 -0
  102. package/dist/public/assets/{pieDiagram-J4AKIIQI-DGv_WCCu.js → pieDiagram-J4AKIIQI-_-94-aMD.js} +1 -1
  103. package/dist/public/assets/{python-DNZu5LjV.js → python-CWnMIOYq.js} +1 -1
  104. package/dist/public/assets/{quadrantDiagram-XTYBLID2-BgY0lf8V.js → quadrantDiagram-XTYBLID2-BhFUbUYz.js} +1 -1
  105. package/dist/public/assets/radar-ABXABTNO-Co-Hlf34.js +1 -0
  106. package/dist/public/assets/railroad-I3PHUGI6-DKMPEjDD.js +1 -0
  107. package/dist/public/assets/{railroad-abnf-LNEFI6M7-tZ7rJxwM.js → railroad-abnf-LNEFI6M7-yUa9GGCQ.js} +1 -1
  108. package/dist/public/assets/{railroad-ebnf-DWYD2UWJ-JO6n_1TK.js → railroad-ebnf-DWYD2UWJ-xY4YiL8M.js} +1 -1
  109. package/dist/public/assets/{railroad-peg-JR7BVNR7-Bf0PG2YJ.js → railroad-peg-JR7BVNR7-BYFYCUAQ.js} +1 -1
  110. package/dist/public/assets/{railroadDiagram-MRQ6NCDL-BMjog84X.js → railroadDiagram-MRQ6NCDL-C9FVhj0k.js} +1 -1
  111. package/dist/public/assets/{razor-DU-x8qSP.js → razor-bDtpN6EJ.js} +1 -1
  112. package/dist/public/assets/{requirementDiagram-Y7LWUY3S-CktbbWjr.js → requirementDiagram-Y7LWUY3S-DLmAg-6C.js} +1 -1
  113. package/dist/public/assets/{sankeyDiagram-WGJFYUXA-CQonOre7.js → sankeyDiagram-WGJFYUXA-Bl5IpDk4.js} +1 -1
  114. package/dist/public/assets/{sequenceDiagram-5NDRBG5M-CAe8-JvA.js → sequenceDiagram-5NDRBG5M-Z-N0OiT_.js} +1 -1
  115. package/dist/public/assets/{stateDiagram-v2-FJ4FJNEA-BrkqOuRm.js → stateDiagram-v2-FJ4FJNEA-ctKQz8Mo.js} +1 -1
  116. package/dist/public/assets/{swimlanes-UMQ2AFYF-sLpgHBKo.js → swimlanes-UMQ2AFYF-CWLKnALg.js} +1 -1
  117. package/dist/public/assets/swimlanesDiagram-OMPLPG4O-CU76xJSu.js +8 -0
  118. package/dist/public/assets/{timeline-definition-SEUHKTQE-CHE0x-WN.js → timeline-definition-SEUHKTQE-DP8Ph_d6.js} +1 -1
  119. package/dist/public/assets/treeView-D4JQ5SDB-JMMnu_MW.js +1 -0
  120. package/dist/public/assets/treemap-SAJKECNS-CPlAD3X1.js +1 -0
  121. package/dist/public/assets/{typescript-BrNT2oWW.js → typescript-CGpNlLbn.js} +1 -1
  122. package/dist/public/assets/{usecaseDiagram-AFA6YXN4-DwkFRI5R.js → usecaseDiagram-AFA6YXN4-DvFjNj3J.js} +1 -1
  123. package/dist/public/assets/{vennDiagram-NNMJR3RB-BquQVmJq.js → vennDiagram-NNMJR3RB-3m2F2H-I.js} +1 -1
  124. package/dist/public/assets/wardley-7MLQ67FV-Dhc8wpLp.js +1 -0
  125. package/dist/public/assets/{wardleyDiagram-S7YFUSEH-sNYFUfMe.js → wardleyDiagram-S7YFUSEH-CsMntu16.js} +1 -1
  126. package/dist/public/assets/{xml-BY65QKv7.js → xml-ByiLoakX.js} +1 -1
  127. package/dist/public/assets/{xychartDiagram-GGNKY7IX-CnWEYuid.js → xychartDiagram-GGNKY7IX-C80rxvM0.js} +1 -1
  128. package/dist/public/assets/{yaml-BMovUwzO.js → yaml-Cz2Af5Rj.js} +1 -1
  129. package/dist/public/index.html +1 -1
  130. package/dist/serve/index.mjs +1083 -744
  131. package/package.json +39 -39
  132. package/dist/public/assets/cynefin-EF2NZ3EQ-CP12ndt7.js +0 -1
  133. package/dist/public/assets/flowDiagram-MALGAFMO-Djxeacjw.js +0 -1
  134. package/dist/public/assets/gitGraph-VSP46ZUC-qlWN7ijl.js +0 -1
  135. package/dist/public/assets/info-OHQRW6UA-jN69V28z.js +0 -1
  136. package/dist/public/assets/module-BEbKICB1.js +0 -2475
  137. package/dist/public/assets/module-Egyx6Otv.js +0 -1
  138. package/dist/public/assets/packet-JDAUHWVQ-Cl8QuXQJ.js +0 -1
  139. package/dist/public/assets/pie-XZMESJXO-kVGJsGZe.js +0 -1
  140. package/dist/public/assets/radar-ABXABTNO-Dw71L-n8.js +0 -1
  141. package/dist/public/assets/railroad-I3PHUGI6-DkaUvI4_.js +0 -1
  142. package/dist/public/assets/swimlanesDiagram-OMPLPG4O-amBdr5Wc.js +0 -8
  143. package/dist/public/assets/treeView-D4JQ5SDB-DKLyFqZk.js +0 -1
  144. package/dist/public/assets/treemap-SAJKECNS-CRG6ogj8.js +0 -1
  145. 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 join6 } from "node:path";
3
+ import { join as join9 } 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 existsSync2, mkdirSync as mkdirSync4, statSync } from "node:fs";
7
+ import { createReadStream, existsSync as existsSync3, 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 join5, normalize, sep } from "node:path";
10
+ import { extname, join as join8, normalize, sep } from "node:path";
11
11
  import { pipeline } from "node:stream";
12
12
 
13
13
  // ../../packages/rpc/esm/node/websocket/websocket-connection.js
@@ -1003,6 +1003,7 @@ var TaErrorCode = {
1003
1003
  CANCELLED: "CELL_TA_CANCELLED",
1004
1004
  NOT_SIGNED_IN: "CELL_TA_NOT_SIGNED_IN",
1005
1005
  ENVIRONMENT_FAILED: "CELL_TA_ENVIRONMENT_FAILED",
1006
+ RATE_LIMITED: "CELL_TA_RATE_LIMITED",
1006
1007
  FORBIDDEN: "CELL_TA_FORBIDDEN",
1007
1008
  LIMIT: "CELL_TA_LIMIT",
1008
1009
  CONNECTION_LOST: "CELL_TA_CONNECTION_LOST"
@@ -1449,6 +1450,8 @@ var TaActionUtil;
1449
1450
  }
1450
1451
  TaActionUtil2.inside = inside;
1451
1452
  })(TaActionUtil || (TaActionUtil = {}));
1453
+ // ../../ta-packages/ta-protocol/esm/common/usage-protocol.js
1454
+ var TaUsageServer = Symbol("TaUsageServer");
1452
1455
  // ../../ta-packages/ta-protocol/esm/common/workspace-protocol.js
1453
1456
  var TA_FILES_PATH = "/api/files";
1454
1457
  var TaWorkspaceServer = Symbol("TaWorkspaceServer");
@@ -2173,8 +2176,19 @@ class CommandSandbox {
2173
2176
  if (!start) {
2174
2177
  throw new Error(`The provider "${id}" of the type command needs "start", a command that prints the address of the Ta service.`);
2175
2178
  }
2176
- this.commands = { start, stop: SandboxProviders.string(config, "stop"), state: SandboxProviders.string(config, "state") };
2179
+ this.commands = {
2180
+ start,
2181
+ stop: SandboxProviders.string(config, "stop"),
2182
+ state: SandboxProviders.string(config, "state"),
2183
+ logs: SandboxProviders.string(config, "logs")
2184
+ };
2177
2185
  this.env = SandboxProviders.env(config);
2186
+ if (!this.commands.logs) {
2187
+ this.logs = undefined;
2188
+ }
2189
+ }
2190
+ logs(tenant, lines) {
2191
+ return this.run(this.commands.logs, tenant, { TA_LOG_LINES: String(lines) });
2178
2192
  }
2179
2193
  async start(tenant) {
2180
2194
  const stdout = await this.run(this.commands.start, tenant);
@@ -2196,10 +2210,10 @@ class CommandSandbox {
2196
2210
  const word = (await this.run(this.commands.state, tenant)).trim().split(/\s+/).pop();
2197
2211
  return word === "running" || word === "stopped" ? word : "missing";
2198
2212
  }
2199
- run(command, tenant) {
2213
+ run(command, tenant, extra = {}) {
2200
2214
  return new Promise((resolve, reject) => {
2201
2215
  const child = spawn("sh", ["-c", command], {
2202
- env: { ...process.env, ...this.env, TA_TENANT_ID: tenant.id, TA_TENANT_ACCOUNT: tenant.account, TA_SERVE_TOKEN: tenant.token },
2216
+ env: { ...process.env, ...this.env, TA_TENANT_ID: tenant.id, TA_TENANT_ACCOUNT: tenant.account, TA_SERVE_TOKEN: tenant.token, ...extra },
2203
2217
  stdio: ["ignore", "pipe", "pipe"]
2204
2218
  });
2205
2219
  let stdout = "";
@@ -2245,7 +2259,16 @@ class DockerEngine {
2245
2259
  throw new DockerError(`${host} is not a Docker host: write unix://<socket> or tcp://<host>:<port>.`, 0);
2246
2260
  }
2247
2261
  }
2248
- call(method, path, body, timeoutMs = 120000) {
2262
+ async call(method, path, body, timeoutMs = 120000) {
2263
+ const answer = await this.raw(method, path, body, timeoutMs);
2264
+ const text = answer.body.toString("utf8");
2265
+ let parsed = text;
2266
+ try {
2267
+ parsed = text ? JSON.parse(text) : undefined;
2268
+ } catch (_a) {}
2269
+ return { status: answer.status, body: parsed };
2270
+ }
2271
+ raw(method, path, body, timeoutMs = 120000) {
2249
2272
  return new Promise((resolve, reject) => {
2250
2273
  const payload = body === undefined ? undefined : JSON.stringify(body);
2251
2274
  const req = request({
@@ -2259,17 +2282,17 @@ class DockerEngine {
2259
2282
  res.on("data", (chunk) => chunks.push(chunk));
2260
2283
  res.on("end", () => {
2261
2284
  var _a, _b;
2262
- const text = Buffer.concat(chunks).toString("utf8");
2263
- let parsed = text;
2264
- try {
2265
- parsed = text ? JSON.parse(text) : undefined;
2266
- } catch (_c) {}
2285
+ const buffer = Buffer.concat(chunks);
2267
2286
  const status = (_a = res.statusCode) !== null && _a !== undefined ? _a : 0;
2268
2287
  if (status >= 400) {
2269
- const message = (_b = parsed === null || parsed === undefined ? undefined : parsed.message) !== null && _b !== undefined ? _b : text;
2288
+ const text = buffer.toString("utf8");
2289
+ let message = text;
2290
+ try {
2291
+ message = (_b = JSON.parse(text).message) !== null && _b !== undefined ? _b : text;
2292
+ } catch (_c) {}
2270
2293
  reject(new DockerError(`Docker: ${method} ${path.split("?")[0]} answered ${status}: ${message}`, status));
2271
2294
  } else {
2272
- resolve({ status, body: parsed });
2295
+ resolve({ status, body: buffer });
2273
2296
  }
2274
2297
  });
2275
2298
  });
@@ -2303,6 +2326,28 @@ class DockerEngine {
2303
2326
  }
2304
2327
  }
2305
2328
  }
2329
+ async stats(name) {
2330
+ try {
2331
+ const { body } = await this.call("GET", `/containers/${encodeURIComponent(name)}/stats?stream=false`, undefined, 15000);
2332
+ return DockerEngineUtil.usage(body);
2333
+ } catch (error) {
2334
+ if (error instanceof DockerError && (error.status === 404 || error.status === 409)) {
2335
+ return;
2336
+ }
2337
+ throw error;
2338
+ }
2339
+ }
2340
+ async logs(name, lines) {
2341
+ try {
2342
+ const { body } = await this.raw("GET", `/containers/${encodeURIComponent(name)}/logs?stdout=1&stderr=1&tail=${lines}`, undefined, 15000);
2343
+ return DockerEngineUtil.demux(body);
2344
+ } catch (error) {
2345
+ if (error instanceof DockerError && error.status === 404) {
2346
+ return "";
2347
+ }
2348
+ throw error;
2349
+ }
2350
+ }
2306
2351
  async ensureNetwork(name, isolated) {
2307
2352
  try {
2308
2353
  await this.call("GET", `/networks/${encodeURIComponent(name)}`);
@@ -2330,6 +2375,36 @@ class DockerEngine {
2330
2375
  }
2331
2376
  var DockerEngineUtil;
2332
2377
  (function(DockerEngineUtil2) {
2378
+ function usage(stats) {
2379
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
2380
+ var _r, _s, _t, _u, _v, _w;
2381
+ 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)) {
2382
+ return;
2383
+ }
2384
+ 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);
2385
+ 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);
2386
+ const cpus = ((_j = stats.cpu_stats) === null || _j === undefined ? undefined : _j.online_cpus) || 1;
2387
+ const cpu = systemDelta > 0 && cpuDelta >= 0 ? Math.round(cpuDelta / systemDelta * cpus * 1000) / 10 : undefined;
2388
+ const memory = (_k = stats.memory_stats) === null || _k === undefined ? undefined : _k.usage;
2389
+ 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;
2390
+ return { cpu, memory: memory === undefined ? undefined : Math.max(0, memory - cache), memoryLimit: (_q = stats.memory_stats) === null || _q === undefined ? undefined : _q.limit };
2391
+ }
2392
+ DockerEngineUtil2.usage = usage;
2393
+ function demux(buffer) {
2394
+ const parts = [];
2395
+ let offset = 0;
2396
+ while (offset + 8 <= buffer.length) {
2397
+ const stream = buffer[offset];
2398
+ const size = buffer.readUInt32BE(offset + 4);
2399
+ if (stream > 2 || buffer[offset + 1] !== 0 || buffer[offset + 2] !== 0 || buffer[offset + 3] !== 0 || offset + 8 + size > buffer.length) {
2400
+ return buffer.toString("utf8");
2401
+ }
2402
+ parts.push(buffer.subarray(offset + 8, offset + 8 + size));
2403
+ offset += 8 + size;
2404
+ }
2405
+ return offset === buffer.length ? Buffer.concat(parts).toString("utf8") : buffer.toString("utf8");
2406
+ }
2407
+ DockerEngineUtil2.demux = demux;
2333
2408
  function splitImage(image) {
2334
2409
  const slash = image.lastIndexOf("/");
2335
2410
  const colon = image.lastIndexOf(":");
@@ -2422,6 +2497,12 @@ class DockerSandbox {
2422
2497
  const container = await this.engine.inspect(this.containerName(tenant));
2423
2498
  return !container ? "missing" : container.State.Running ? "running" : "stopped";
2424
2499
  }
2500
+ usage(tenant) {
2501
+ return this.engine.stats(this.containerName(tenant));
2502
+ }
2503
+ logs(tenant, lines) {
2504
+ return this.engine.logs(this.containerName(tenant), lines);
2505
+ }
2425
2506
  spec(tenant) {
2426
2507
  const name = this.containerName(tenant);
2427
2508
  return {
@@ -2512,34 +2593,122 @@ var EnvironmentsUtil;
2512
2593
  throw new EnvironmentError(`Nothing answered at ${url} within ${Math.round(timeoutMs / 1000)} seconds${last instanceof Error ? `: ${last.message}` : ""}.`);
2513
2594
  }
2514
2595
  EnvironmentsUtil2.waitReady = waitReady;
2515
- function call(endpoint, method, params = [], timeoutMs = 1e4) {
2596
+ async function call(endpoint, method, params = [], timeoutMs = 1e4) {
2597
+ const [result] = await calls(endpoint, [[method, params]], timeoutMs);
2598
+ return result;
2599
+ }
2600
+ EnvironmentsUtil2.call = call;
2601
+ function calls(endpoint, list, timeoutMs = 1e4) {
2516
2602
  return new Promise((resolve, reject) => {
2517
2603
  const url = new URL(`${endpoint.url.replace(/^http/, "ws")}${TA_WEBSOCKET_PATH}`);
2518
2604
  url.searchParams.set("token", endpoint.token);
2519
2605
  const socket = new WebSocket(url);
2520
- const timer = setTimeout(() => finish(new EnvironmentError(`${method} took too long`)), timeoutMs);
2521
- const finish = (error, result) => {
2606
+ const results = new Array(list.length);
2607
+ let left = list.length;
2608
+ let done = false;
2609
+ const timer = setTimeout(() => {
2610
+ var _a;
2611
+ var _b;
2612
+ return finish(new EnvironmentError(`${(_b = (_a = list[0]) === null || _a === undefined ? undefined : _a[0]) !== null && _b !== undefined ? _b : "A call"} took too long`));
2613
+ }, timeoutMs);
2614
+ const finish = (error) => {
2615
+ if (done) {
2616
+ return;
2617
+ }
2618
+ done = true;
2522
2619
  clearTimeout(timer);
2523
2620
  socket.close();
2524
2621
  if (error) {
2525
2622
  reject(error);
2526
2623
  } else {
2527
- resolve(result);
2624
+ resolve(results);
2625
+ }
2626
+ };
2627
+ if (left === 0) {
2628
+ queueMicrotask(() => finish());
2629
+ }
2630
+ socket.onopen = () => {
2631
+ for (const [index, [method, params]] of list.entries()) {
2632
+ socket.send(JSON.stringify({ jsonrpc: "2.0", id: index + 1, method, params }));
2528
2633
  }
2529
2634
  };
2530
- socket.onopen = () => socket.send(JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }));
2531
2635
  socket.onerror = () => finish(new EnvironmentError(`Cannot reach ${endpoint.url}`));
2532
2636
  socket.onmessage = (message) => {
2637
+ var _a;
2533
2638
  const answer = JSON.parse(String(message.data));
2534
- if (answer.id === 1) {
2535
- finish(answer.error ? new EnvironmentError(answer.error.message) : undefined, answer.result);
2536
- } else if (answer.method !== undefined && answer.id !== undefined) {
2639
+ if (answer.method !== undefined && answer.id !== undefined) {
2537
2640
  socket.send(JSON.stringify({ jsonrpc: "2.0", id: answer.id, result: null }));
2641
+ return;
2642
+ }
2643
+ const index = typeof answer.id === "number" ? answer.id - 1 : -1;
2644
+ if (index < 0 || index >= list.length || results[index] !== undefined) {
2645
+ return;
2646
+ }
2647
+ if (list.length === 1 && answer.error) {
2648
+ finish(new EnvironmentError(answer.error.message));
2649
+ return;
2650
+ }
2651
+ results[index] = answer.error ? new EnvironmentError(answer.error.message) : (_a = answer.result) !== null && _a !== undefined ? _a : null;
2652
+ if (--left === 0) {
2653
+ finish();
2538
2654
  }
2539
2655
  };
2540
2656
  });
2541
2657
  }
2542
- EnvironmentsUtil2.call = call;
2658
+ EnvironmentsUtil2.calls = calls;
2659
+ function status(session) {
2660
+ return session.waiting ? "waiting" : session.isStreaming ? "working" : session.asking ? "asking" : "idle";
2661
+ }
2662
+ EnvironmentsUtil2.status = status;
2663
+ function summary(sessions) {
2664
+ const statuses = sessions.map(status);
2665
+ return {
2666
+ sessions: sessions.length,
2667
+ working: statuses.filter((each) => each === "working").length,
2668
+ waiting: statuses.filter((each) => each === "waiting" || each === "asking").length
2669
+ };
2670
+ }
2671
+ EnvironmentsUtil2.summary = summary;
2672
+ function stats(value) {
2673
+ var _a;
2674
+ var _b, _c, _d, _e, _f, _g, _h, _j;
2675
+ const given = value;
2676
+ if (!given || typeof given !== "object" || !given.tokens || typeof given.cost !== "number") {
2677
+ return;
2678
+ }
2679
+ const percent = (_a = given.contextUsage) === null || _a === undefined ? undefined : _a.percent;
2680
+ return {
2681
+ userMessages: (_b = given.userMessages) !== null && _b !== undefined ? _b : 0,
2682
+ assistantMessages: (_c = given.assistantMessages) !== null && _c !== undefined ? _c : 0,
2683
+ toolCalls: (_d = given.toolCalls) !== null && _d !== undefined ? _d : 0,
2684
+ tokens: {
2685
+ input: (_e = given.tokens.input) !== null && _e !== undefined ? _e : 0,
2686
+ output: (_f = given.tokens.output) !== null && _f !== undefined ? _f : 0,
2687
+ cacheRead: (_g = given.tokens.cacheRead) !== null && _g !== undefined ? _g : 0,
2688
+ cacheWrite: (_h = given.tokens.cacheWrite) !== null && _h !== undefined ? _h : 0,
2689
+ total: (_j = given.tokens.total) !== null && _j !== undefined ? _j : 0
2690
+ },
2691
+ cost: given.cost,
2692
+ context: typeof percent === "number" ? Math.round(percent) : undefined
2693
+ };
2694
+ }
2695
+ EnvironmentsUtil2.stats = stats;
2696
+ function agentSession(session, stats2) {
2697
+ return {
2698
+ sessionId: session.sessionId,
2699
+ name: session.sessionName || undefined,
2700
+ cwd: session.cwd,
2701
+ model: session.model,
2702
+ status: status(session),
2703
+ messageCount: session.messageCount,
2704
+ attached: session.attached,
2705
+ createdAt: session.createdAt,
2706
+ updatedAt: session.updatedAt,
2707
+ group: session.group,
2708
+ stats: stats2
2709
+ };
2710
+ }
2711
+ EnvironmentsUtil2.agentSession = agentSession;
2543
2712
  function sandboxTenant(tenant) {
2544
2713
  return { id: tenant.id, account: tenant.account, token: tenant.token };
2545
2714
  }
@@ -2591,12 +2760,97 @@ class Environments {
2591
2760
  url: own && provider instanceof TaSandbox ? provider.url : undefined
2592
2761
  };
2593
2762
  }
2763
+ async usage(tenant) {
2764
+ var _a;
2765
+ if (((_a = this.running.get(tenant.id)) === null || _a === undefined ? undefined : _a.status) !== "running") {
2766
+ return;
2767
+ }
2768
+ const { provider, own } = this.provider(tenant);
2769
+ if (own || !provider.usage) {
2770
+ return;
2771
+ }
2772
+ return provider.usage(EnvironmentsUtil.sandboxTenant(tenant)).catch(() => {
2773
+ return;
2774
+ });
2775
+ }
2776
+ async logs(tenant, lines) {
2777
+ const { provider, own } = this.provider(tenant);
2778
+ if (own || !provider.logs) {
2779
+ throw new EnvironmentError(`The sandboxes of ${own ? "a Ta service of the user" : provider.name} keep no logs for the gateway.`);
2780
+ }
2781
+ return provider.logs(EnvironmentsUtil.sandboxTenant(tenant), lines);
2782
+ }
2783
+ watched(tenant) {
2784
+ const running = this.running.get(tenant.id);
2785
+ return (running === null || running === undefined ? undefined : running.status) === "running" && running.endpoint && !this.provider(tenant).own ? running.endpoint : undefined;
2786
+ }
2787
+ runningIds() {
2788
+ return [...this.running.entries()].filter(([id, running]) => {
2789
+ const tenant = running.status === "running" ? this.tenants.get(id) : undefined;
2790
+ return tenant !== undefined && !this.provider(tenant).own;
2791
+ }).map(([id]) => id);
2792
+ }
2793
+ usageReader(tenant) {
2794
+ const endpoint = this.watched(tenant);
2795
+ return endpoint && ((since) => EnvironmentsUtil.call(endpoint, "TaUsageServer/getUsage", [since], 20000));
2796
+ }
2797
+ async activity(tenant, sessionId) {
2798
+ const endpoint = this.watched(tenant);
2799
+ if (!endpoint) {
2800
+ throw new EnvironmentError("Its sandbox does not run: start it to see what its agents did.");
2801
+ }
2802
+ return EnvironmentsUtil.call(endpoint, "TaUsageServer/getActivity", [sessionId, 50]);
2803
+ }
2804
+ async agentSummary(tenant, timeoutMs = 3000) {
2805
+ const endpoint = this.watched(tenant);
2806
+ if (!endpoint) {
2807
+ return;
2808
+ }
2809
+ const sessions = await EnvironmentsUtil.call(endpoint, "TaServer/listSessions", [], timeoutMs).catch(() => {
2810
+ return;
2811
+ });
2812
+ return sessions && EnvironmentsUtil.summary(sessions);
2813
+ }
2814
+ async agents(tenant, now = Date.now()) {
2815
+ const endpoint = this.watched(tenant);
2816
+ if (!endpoint) {
2817
+ 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.");
2818
+ }
2819
+ const [sessions] = await EnvironmentsUtil.calls(endpoint, [["TaServer/listSessions", []]]);
2820
+ const stats = await EnvironmentsUtil.calls(endpoint, sessions.map((session) => ["TaServer/getSessionStats", [session.sessionId]])).catch(() => sessions.map(() => {
2821
+ return;
2822
+ }));
2823
+ const list = sessions.map((session, index) => EnvironmentsUtil.agentSession(session, EnvironmentsUtil.stats(stats[index]))).sort((a, b) => b.updatedAt - a.updatedAt);
2824
+ return {
2825
+ sessions: list,
2826
+ totals: {
2827
+ ...EnvironmentsUtil.summary(sessions),
2828
+ toolCalls: list.reduce((sum, each) => {
2829
+ var _a;
2830
+ var _b;
2831
+ return sum + ((_b = (_a = each.stats) === null || _a === undefined ? undefined : _a.toolCalls) !== null && _b !== undefined ? _b : 0);
2832
+ }, 0),
2833
+ tokens: list.reduce((sum, each) => {
2834
+ var _a;
2835
+ var _b;
2836
+ return sum + ((_b = (_a = each.stats) === null || _a === undefined ? undefined : _a.tokens.total) !== null && _b !== undefined ? _b : 0);
2837
+ }, 0),
2838
+ cost: list.reduce((sum, each) => {
2839
+ var _a;
2840
+ var _b;
2841
+ return sum + ((_b = (_a = each.stats) === null || _a === undefined ? undefined : _a.cost) !== null && _b !== undefined ? _b : 0);
2842
+ }, 0)
2843
+ },
2844
+ readAt: now
2845
+ };
2846
+ }
2594
2847
  connections(tenantId) {
2595
2848
  var _a;
2596
2849
  var _b;
2597
2850
  return (_b = (_a = this.running.get(tenantId)) === null || _a === undefined ? undefined : _a.connections) !== null && _b !== undefined ? _b : 0;
2598
2851
  }
2599
2852
  ensure(tenant) {
2853
+ var _a;
2600
2854
  const running = this.state(tenant.id);
2601
2855
  if (running.status === "running" && running.endpoint) {
2602
2856
  return Promise.resolve(running.endpoint);
@@ -2604,13 +2858,18 @@ class Environments {
2604
2858
  if (running.starting) {
2605
2859
  return running.starting;
2606
2860
  }
2861
+ const blocked = (_a = this.guard) === null || _a === undefined ? undefined : _a.call(this, tenant);
2862
+ if (blocked) {
2863
+ Object.assign(running, { status: "failed", error: blocked });
2864
+ return Promise.reject(new EnvironmentError(blocked));
2865
+ }
2607
2866
  running.status = "starting";
2608
2867
  running.error = undefined;
2609
2868
  const starting = (async () => {
2610
- var _a;
2869
+ var _a2;
2611
2870
  const { provider } = this.provider(tenant);
2612
2871
  const endpoint = await provider.start(EnvironmentsUtil.sandboxTenant(tenant));
2613
- await EnvironmentsUtil.waitReady(endpoint.url, (_a = this.settings.readyTimeoutMs) !== null && _a !== undefined ? _a : provider.readyTimeoutMs);
2872
+ await EnvironmentsUtil.waitReady(endpoint.url, (_a2 = this.settings.readyTimeoutMs) !== null && _a2 !== undefined ? _a2 : provider.readyTimeoutMs);
2614
2873
  return endpoint;
2615
2874
  })();
2616
2875
  running.starting = starting;
@@ -2710,6 +2969,539 @@ class Environments {
2710
2969
  }
2711
2970
  }
2712
2971
 
2972
+ // ../../ta-packages/ta-gateway/esm/node/gateway-audit.js
2973
+ import { appendFileSync, chmodSync as chmodSync2, existsSync as existsSync2, readFileSync as readFileSync3 } from "node:fs";
2974
+ import { join as join2 } from "node:path";
2975
+ var TAIL_BYTES = 2 * 1024 * 1024;
2976
+
2977
+ class GatewayAudit {
2978
+ constructor(dir) {
2979
+ this.file = join2(dir, "audit.jsonl");
2980
+ }
2981
+ add(entry, now = Date.now()) {
2982
+ const fresh = !existsSync2(this.file);
2983
+ appendFileSync(this.file, `${JSON.stringify({ at: now, ...entry })}
2984
+ `, { mode: 384 });
2985
+ if (fresh) {
2986
+ chmodSync2(this.file, 384);
2987
+ }
2988
+ }
2989
+ list(limit) {
2990
+ let text;
2991
+ try {
2992
+ text = readFileSync3(this.file, "utf8");
2993
+ } catch (_a) {
2994
+ return [];
2995
+ }
2996
+ if (text.length > TAIL_BYTES) {
2997
+ text = text.slice(text.indexOf(`
2998
+ `, text.length - TAIL_BYTES) + 1);
2999
+ }
3000
+ const entries = [];
3001
+ const lines = text.split(`
3002
+ `);
3003
+ for (let index = lines.length - 1;index >= 0 && entries.length < limit; index--) {
3004
+ try {
3005
+ if (lines[index]) {
3006
+ entries.push(JSON.parse(lines[index]));
3007
+ }
3008
+ } catch (_b) {}
3009
+ }
3010
+ return entries;
3011
+ }
3012
+ }
3013
+
3014
+ // ../../ta-packages/ta-gateway/esm/node/gateway-policy.js
3015
+ import { readFileSync as readFileSync5 } from "node:fs";
3016
+ import { join as join4 } from "node:path";
3017
+
3018
+ // ../../ta-packages/ta-gateway/esm/node/tenant-store.js
3019
+ import { createHash as createHash4, randomBytes as randomBytes3 } from "node:crypto";
3020
+ import { chmodSync as chmodSync3, mkdirSync as mkdirSync2, readdirSync, readFileSync as readFileSync4, renameSync as renameSync2, writeFileSync as writeFileSync2 } from "node:fs";
3021
+ import { join as join3 } from "node:path";
3022
+ function writePrivate(file, data) {
3023
+ const temporary = `${file}.${process.pid}.tmp`;
3024
+ writeFileSync2(temporary, JSON.stringify(data, undefined, 2), { mode: 384 });
3025
+ chmodSync3(temporary, 384);
3026
+ renameSync2(temporary, file);
3027
+ }
3028
+ var TenantUtil;
3029
+ (function(TenantUtil2) {
3030
+ function id(account) {
3031
+ return createHash4("sha256").update(account).digest("hex").slice(0, 16);
3032
+ }
3033
+ TenantUtil2.id = id;
3034
+ })(TenantUtil || (TenantUtil = {}));
3035
+
3036
+ class TenantStore {
3037
+ constructor(root) {
3038
+ this.cache = new Map;
3039
+ this.dir = join3(root, "tenants");
3040
+ mkdirSync2(this.dir, { recursive: true, mode: 448 });
3041
+ }
3042
+ get(id) {
3043
+ if (!/^[0-9a-f]{16}$/.test(id)) {
3044
+ return;
3045
+ }
3046
+ let tenant = this.cache.get(id);
3047
+ if (!tenant) {
3048
+ try {
3049
+ tenant = JSON.parse(readFileSync4(join3(this.dir, `${id}.json`), "utf8"));
3050
+ this.cache.set(id, tenant);
3051
+ } catch (_a) {
3052
+ return;
3053
+ }
3054
+ }
3055
+ return tenant;
3056
+ }
3057
+ ensure(account, now = Date.now()) {
3058
+ const id = TenantUtil.id(account.id);
3059
+ const known = this.get(id);
3060
+ if (known) {
3061
+ if (account.name && (known.name !== account.name || known.avatar !== account.avatar)) {
3062
+ return this.update(id, { name: account.name, avatar: account.avatar });
3063
+ }
3064
+ return known;
3065
+ }
3066
+ const tenant = {
3067
+ id,
3068
+ account: account.id,
3069
+ name: account.name,
3070
+ avatar: account.avatar,
3071
+ token: randomBytes3(32).toString("base64url"),
3072
+ createdAt: now
3073
+ };
3074
+ this.save(tenant);
3075
+ return tenant;
3076
+ }
3077
+ update(id, change) {
3078
+ const tenant = this.get(id);
3079
+ if (!tenant) {
3080
+ throw new Error(`No tenant ${id}`);
3081
+ }
3082
+ const next = { ...tenant, ...change };
3083
+ this.save(next);
3084
+ return next;
3085
+ }
3086
+ list() {
3087
+ 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);
3088
+ }
3089
+ save(tenant) {
3090
+ writePrivate(join3(this.dir, `${tenant.id}.json`), tenant);
3091
+ this.cache.set(tenant.id, tenant);
3092
+ }
3093
+ }
3094
+
3095
+ // ../../ta-packages/ta-gateway/esm/node/gateway-policy.js
3096
+ class PolicyError extends Error {
3097
+ }
3098
+ var GatewayPolicyUtil;
3099
+ (function(GatewayPolicyUtil2) {
3100
+ const NUMBERS = ["freeMinutes", "promptsPerMinute", "connections"];
3101
+ const RANK = { member: 0, admin: 1, owner: 2 };
3102
+ function rank(role) {
3103
+ return RANK[role];
3104
+ }
3105
+ GatewayPolicyUtil2.rank = rank;
3106
+ function limits(value, providers, nullable) {
3107
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
3108
+ throw new PolicyError('Limits are an object, such as { "freeMinutes": 600 }.');
3109
+ }
3110
+ const given = value;
3111
+ const out = {};
3112
+ for (const key of Object.keys(given)) {
3113
+ if (![...NUMBERS, "providers", "own"].includes(key)) {
3114
+ throw new PolicyError(`"${key}" is no limit: those there are are ${[...NUMBERS, "providers", "own"].join(", ")}.`);
3115
+ }
3116
+ }
3117
+ for (const key of NUMBERS) {
3118
+ const item = given[key];
3119
+ if (item === undefined) {
3120
+ continue;
3121
+ }
3122
+ if (item === null) {
3123
+ if (nullable) {
3124
+ out[key] = null;
3125
+ }
3126
+ continue;
3127
+ }
3128
+ if (typeof item !== "number" || !Number.isInteger(item) || item < 0 || item > 1e7) {
3129
+ throw new PolicyError(`"${key}" is a whole number from 0, or null for none.`);
3130
+ }
3131
+ out[key] = item;
3132
+ }
3133
+ if (given.providers !== undefined) {
3134
+ if (given.providers === null) {
3135
+ if (nullable) {
3136
+ out.providers = null;
3137
+ }
3138
+ } else if (!Array.isArray(given.providers) || given.providers.some((id) => typeof id !== "string" || !providers.includes(id))) {
3139
+ throw new PolicyError(`"providers" lists providers of the gateway: ${providers.join(", ")}.`);
3140
+ } else {
3141
+ out.providers = [...new Set(given.providers)];
3142
+ }
3143
+ }
3144
+ if (given.own !== undefined) {
3145
+ if (given.own === null) {
3146
+ if (nullable) {
3147
+ out.own = null;
3148
+ }
3149
+ } else if (typeof given.own !== "boolean") {
3150
+ throw new PolicyError('"own" is true or false.');
3151
+ } else {
3152
+ out.own = given.own;
3153
+ }
3154
+ }
3155
+ return out;
3156
+ }
3157
+ GatewayPolicyUtil2.limits = limits;
3158
+ function resolve(defaults, own, ownAllowed) {
3159
+ const pick = (key) => own && own[key] !== undefined ? own[key] : defaults[key];
3160
+ const number = (key) => {
3161
+ const value = pick(key);
3162
+ return typeof value === "number" ? value : undefined;
3163
+ };
3164
+ const providers = pick("providers");
3165
+ const own_ = pick("own");
3166
+ return {
3167
+ freeMinutes: number("freeMinutes"),
3168
+ promptsPerMinute: number("promptsPerMinute"),
3169
+ connections: number("connections"),
3170
+ providers: Array.isArray(providers) ? providers : undefined,
3171
+ own: ownAllowed && own_ !== false
3172
+ };
3173
+ }
3174
+ GatewayPolicyUtil2.resolve = resolve;
3175
+ })(GatewayPolicyUtil || (GatewayPolicyUtil = {}));
3176
+
3177
+ class GatewayPolicy {
3178
+ constructor(dir, owners) {
3179
+ this.owners = owners;
3180
+ this.file = join4(dir, "policy.json");
3181
+ this.stored = this.read();
3182
+ }
3183
+ role(account) {
3184
+ var _a;
3185
+ if (this.owners.some((pattern) => AccessUtil.matches(pattern, account))) {
3186
+ return "owner";
3187
+ }
3188
+ return ((_a = this.stored.admins) === null || _a === undefined ? undefined : _a.includes(account)) ? "admin" : "member";
3189
+ }
3190
+ lets(account) {
3191
+ return this.role(account) !== "member";
3192
+ }
3193
+ get defaults() {
3194
+ var _a;
3195
+ return (_a = this.stored.defaults) !== null && _a !== undefined ? _a : {};
3196
+ }
3197
+ setDefaults(limits) {
3198
+ this.stored = { ...this.stored, defaults: limits };
3199
+ writePrivate(this.file, this.stored);
3200
+ }
3201
+ setRole(account, role) {
3202
+ var _a;
3203
+ const admins = ((_a = this.stored.admins) !== null && _a !== undefined ? _a : []).filter((other) => other !== account);
3204
+ this.stored = { ...this.stored, admins: role === "admin" ? [...admins, account] : admins };
3205
+ writePrivate(this.file, this.stored);
3206
+ }
3207
+ view(idleMinutes) {
3208
+ var _a;
3209
+ return {
3210
+ defaults: this.defaults,
3211
+ admins: [
3212
+ ...this.owners.map((account) => ({ account, role: "owner" })),
3213
+ ...((_a = this.stored.admins) !== null && _a !== undefined ? _a : []).filter((account) => this.role(account) === "admin").map((account) => ({ account, role: "admin" }))
3214
+ ],
3215
+ idleMinutes
3216
+ };
3217
+ }
3218
+ read() {
3219
+ try {
3220
+ const value = JSON.parse(readFileSync5(this.file, "utf8"));
3221
+ return value && typeof value === "object" ? value : {};
3222
+ } catch (_a) {
3223
+ return {};
3224
+ }
3225
+ }
3226
+ }
3227
+
3228
+ // ../../ta-packages/ta-gateway/esm/node/gateway-usage.js
3229
+ import { mkdirSync as mkdirSync3, readFileSync as readFileSync6 } from "node:fs";
3230
+ import { join as join5 } from "node:path";
3231
+ var HOUR = 3600000;
3232
+ var DAY = 24 * HOUR;
3233
+ var KEPT_DAYS = 32;
3234
+ var FRESH_MS = 60000;
3235
+ var GatewayUsageUtil;
3236
+ (function(GatewayUsageUtil2) {
3237
+ GatewayUsageUtil2.KEEP_MS = KEPT_DAYS * DAY;
3238
+ function month(now) {
3239
+ const date = new Date(now);
3240
+ const start = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1);
3241
+ const next = Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + 1, 1);
3242
+ return { key: new Date(start).toISOString().slice(0, 7), start, next };
3243
+ }
3244
+ GatewayUsageUtil2.month = month;
3245
+ function tokens(bucket) {
3246
+ return bucket.input + bucket.output + bucket.cacheRead + bucket.cacheWrite;
3247
+ }
3248
+ GatewayUsageUtil2.tokens = tokens;
3249
+ function trend(byTenant, since, stale, now) {
3250
+ var _a, _b;
3251
+ const hours = new Map;
3252
+ const models = new Map;
3253
+ const tenants = [];
3254
+ for (const [tenantId, buckets] of byTenant) {
3255
+ const share = { tenantId, tokens: 0, cost: 0, turns: 0 };
3256
+ for (const bucket of buckets) {
3257
+ if (bucket.at + HOUR <= since) {
3258
+ continue;
3259
+ }
3260
+ const count = tokens(bucket);
3261
+ const hour = (_a = hours.get(bucket.at)) !== null && _a !== undefined ? _a : { at: bucket.at, input: 0, output: 0, tokens: 0, cost: 0, turns: 0 };
3262
+ hour.input += bucket.input;
3263
+ hour.output += bucket.output;
3264
+ hour.tokens += count;
3265
+ hour.cost += bucket.cost;
3266
+ hour.turns += bucket.turns;
3267
+ hours.set(bucket.at, hour);
3268
+ const model = (_b = models.get(bucket.model)) !== null && _b !== undefined ? _b : { model: bucket.model, tokens: 0, cost: 0, turns: 0 };
3269
+ model.tokens += count;
3270
+ model.cost += bucket.cost;
3271
+ model.turns += bucket.turns;
3272
+ models.set(bucket.model, model);
3273
+ share.tokens += count;
3274
+ share.cost += bucket.cost;
3275
+ share.turns += bucket.turns;
3276
+ }
3277
+ if (share.turns > 0) {
3278
+ tenants.push(share);
3279
+ }
3280
+ }
3281
+ return {
3282
+ since,
3283
+ hours: [...hours.values()].sort((a, b) => a.at - b.at),
3284
+ byModel: [...models.values()].sort((a, b) => b.cost - a.cost || b.tokens - a.tokens),
3285
+ byTenant: tenants.sort((a, b) => b.cost - a.cost || b.tokens - a.tokens),
3286
+ stale,
3287
+ readAt: now
3288
+ };
3289
+ }
3290
+ GatewayUsageUtil2.trend = trend;
3291
+ })(GatewayUsageUtil || (GatewayUsageUtil = {}));
3292
+
3293
+ class GatewayUsage {
3294
+ constructor(root) {
3295
+ this.kept = new Map;
3296
+ this.reading = new Map;
3297
+ this.dir = join5(root, "usage");
3298
+ mkdirSync3(this.dir, { recursive: true, mode: 448 });
3299
+ }
3300
+ get(tenantId) {
3301
+ let kept = this.kept.get(tenantId);
3302
+ if (!kept && /^[0-9a-f]{16}$/.test(tenantId)) {
3303
+ try {
3304
+ kept = JSON.parse(readFileSync6(join5(this.dir, `${tenantId}.json`), "utf8"));
3305
+ this.kept.set(tenantId, kept);
3306
+ } catch (_a) {
3307
+ return;
3308
+ }
3309
+ }
3310
+ return kept;
3311
+ }
3312
+ async refresh(tenantId, read, now = Date.now(), maxAge = FRESH_MS) {
3313
+ const kept = this.get(tenantId);
3314
+ if (!read) {
3315
+ return false;
3316
+ }
3317
+ if (kept && now - kept.readAt < maxAge) {
3318
+ return true;
3319
+ }
3320
+ let reading = this.reading.get(tenantId);
3321
+ if (!reading) {
3322
+ reading = read(now - GatewayUsageUtil.KEEP_MS).then((buckets) => {
3323
+ const next = { readAt: now, buckets };
3324
+ this.kept.set(tenantId, next);
3325
+ writePrivate(join5(this.dir, `${tenantId}.json`), next);
3326
+ });
3327
+ this.reading.set(tenantId, reading);
3328
+ reading.finally(() => this.reading.delete(tenantId)).catch(() => {
3329
+ return;
3330
+ });
3331
+ }
3332
+ try {
3333
+ await reading;
3334
+ return true;
3335
+ } catch (_a) {
3336
+ return false;
3337
+ }
3338
+ }
3339
+ month(tenantId, now = Date.now()) {
3340
+ var _a;
3341
+ var _b;
3342
+ const { start } = GatewayUsageUtil.month(now);
3343
+ let tokens = 0;
3344
+ let cost = 0;
3345
+ for (const bucket of (_b = (_a = this.get(tenantId)) === null || _a === undefined ? undefined : _a.buckets) !== null && _b !== undefined ? _b : []) {
3346
+ if (bucket.at >= start) {
3347
+ tokens += GatewayUsageUtil.tokens(bucket);
3348
+ cost += bucket.cost;
3349
+ }
3350
+ }
3351
+ return { tokens, cost };
3352
+ }
3353
+ }
3354
+
3355
+ // ../../ta-packages/ta-gateway/esm/node/gateway-quotas.js
3356
+ var MINUTE = 60000;
3357
+ var MAX_TICK_MS = 2 * MINUTE;
3358
+
3359
+ class GatewayQuotas {
3360
+ constructor(policy, tenants, usage, ownAllowed) {
3361
+ this.policy = policy;
3362
+ this.tenants = tenants;
3363
+ this.usage = usage;
3364
+ this.ownAllowed = ownAllowed;
3365
+ this.lastTick = Date.now();
3366
+ this.prompts = new Map;
3367
+ }
3368
+ role(tenant) {
3369
+ return this.policy.role(tenant.account);
3370
+ }
3371
+ limits(tenant) {
3372
+ if (this.role(tenant) === "owner") {
3373
+ return { own: this.ownAllowed() };
3374
+ }
3375
+ return GatewayPolicyUtil.resolve(this.policy.defaults, tenant.limits, this.ownAllowed());
3376
+ }
3377
+ meter(tenant, now = Date.now()) {
3378
+ var _a;
3379
+ const { key } = GatewayUsageUtil.month(now);
3380
+ return ((_a = tenant.meter) === null || _a === undefined ? undefined : _a.month) === key ? tenant.meter : { month: key, runMs: 0, bonusMinutes: 0 };
3381
+ }
3382
+ blocked(tenant, now = Date.now()) {
3383
+ if (tenant.suspended && this.role(tenant) !== "owner") {
3384
+ return "suspended";
3385
+ }
3386
+ const { freeMinutes } = this.limits(tenant);
3387
+ const meter = this.meter(tenant, now);
3388
+ return freeMinutes !== undefined && meter.runMs >= (freeMinutes + meter.bonusMinutes) * MINUTE ? "minutes" : undefined;
3389
+ }
3390
+ state(tenant, now = Date.now()) {
3391
+ const { key, next } = GatewayUsageUtil.month(now);
3392
+ const meter = this.meter(tenant, now);
3393
+ const limits = this.limits(tenant);
3394
+ const month = this.usage.month(tenant.id, now);
3395
+ return {
3396
+ month: key,
3397
+ resetsAt: next,
3398
+ minutes: { used: Math.floor(meter.runMs / MINUTE), limit: limits.freeMinutes, bonus: meter.bonusMinutes },
3399
+ tokens: { used: month.tokens, cost: month.cost },
3400
+ promptsPerMinute: limits.promptsPerMinute,
3401
+ connections: limits.connections,
3402
+ blocked: this.blocked(tenant, now)
3403
+ };
3404
+ }
3405
+ takePrompt(tenant, now = Date.now()) {
3406
+ var _a;
3407
+ const limit = this.limits(tenant).promptsPerMinute;
3408
+ if (limit === undefined) {
3409
+ return true;
3410
+ }
3411
+ const times = ((_a = this.prompts.get(tenant.id)) !== null && _a !== undefined ? _a : []).filter((time) => now - time < MINUTE);
3412
+ if (times.length >= limit) {
3413
+ this.prompts.set(tenant.id, times);
3414
+ return false;
3415
+ }
3416
+ times.push(now);
3417
+ this.prompts.set(tenant.id, times);
3418
+ return true;
3419
+ }
3420
+ retryAfter(tenant, now = Date.now()) {
3421
+ var _a;
3422
+ const oldest = (_a = this.prompts.get(tenant.id)) === null || _a === undefined ? undefined : _a[0];
3423
+ return oldest === undefined ? 0 : Math.max(0, MINUTE - (now - oldest));
3424
+ }
3425
+ tick(running, now = Date.now()) {
3426
+ const elapsed = Math.min(Math.max(0, now - this.lastTick), MAX_TICK_MS);
3427
+ this.lastTick = now;
3428
+ const blocked = [];
3429
+ for (const id of running) {
3430
+ const tenant = this.tenants.get(id);
3431
+ if (!tenant) {
3432
+ continue;
3433
+ }
3434
+ const meter = this.meter(tenant, now);
3435
+ const next = this.tenants.update(id, { meter: { ...meter, runMs: meter.runMs + elapsed } });
3436
+ if (this.blocked(next, now)) {
3437
+ blocked.push(next);
3438
+ }
3439
+ }
3440
+ return blocked;
3441
+ }
3442
+ grant(tenant, minutes, now = Date.now()) {
3443
+ const meter = this.meter(tenant, now);
3444
+ return this.tenants.update(tenant.id, { meter: { ...meter, bonusMinutes: Math.max(0, meter.bonusMinutes + minutes) } });
3445
+ }
3446
+ }
3447
+
3448
+ // ../../ta-packages/ta-protocol/esm/common/redactor.js
3449
+ var REDACTED = "[redacted]";
3450
+ var WHOLE = [
3451
+ /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 ]*PRIVATE KEY-----/g,
3452
+ /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{20,}/g,
3453
+ /\bgh[pousr]_[A-Za-z0-9]{30,}\b/g,
3454
+ /\bgithub_pat_[A-Za-z0-9_]{40,}\b/g,
3455
+ /\bglpat-[A-Za-z0-9_-]{20,}\b/g,
3456
+ /\bxox[abprs]-[A-Za-z0-9-]{10,}\b/g,
3457
+ /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g,
3458
+ /\bAIza[0-9A-Za-z_-]{35}\b/g,
3459
+ /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/g
3460
+ ];
3461
+ var NAMED = [
3462
+ /(\bBearer\s+)[A-Za-z0-9._~+/-]{16,}=*/gi,
3463
+ /(\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,
3464
+ /(:\/\/[^\s:/@]+:)([^\s@/]{3,})(@)/g
3465
+ ];
3466
+
3467
+ class Redactor {
3468
+ constructor() {
3469
+ this.count = 0;
3470
+ }
3471
+ text(text) {
3472
+ let out = text;
3473
+ for (const pattern of WHOLE) {
3474
+ out = out.replace(pattern, () => this.hide());
3475
+ }
3476
+ out = out.replace(NAMED[0], (_match, before) => before + this.hide());
3477
+ out = out.replace(NAMED[1], (match, before, value) => Redactor.placeholder(value) ? match : before + this.hide());
3478
+ out = out.replace(NAMED[2], (_match, before, _value, after) => before + this.hide() + after);
3479
+ return out;
3480
+ }
3481
+ value(value, max) {
3482
+ if (typeof value === "string") {
3483
+ return Redactor.cut(this.text(value), max);
3484
+ }
3485
+ if (Array.isArray(value)) {
3486
+ return value.map((item) => this.value(item, max));
3487
+ }
3488
+ if (value && typeof value === "object") {
3489
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, this.value(item, max)]));
3490
+ }
3491
+ return value;
3492
+ }
3493
+ static cut(text, max) {
3494
+ return text.length > max ? `${text.slice(0, max)}…` : text;
3495
+ }
3496
+ hide() {
3497
+ this.count++;
3498
+ return REDACTED;
3499
+ }
3500
+ static placeholder(value) {
3501
+ return /^(?:\$|<|\{|\[redacted|process\.env|os\.environ|xxx|\*{3})/i.test(value) || /^(?:true|false|null|undefined|none|string|number)$/i.test(value);
3502
+ }
3503
+ }
3504
+
2713
3505
  // ../../ta-packages/ta-gateway/esm/node/gateway-service.js
2714
3506
  class GatewayCallError extends Error {
2715
3507
  constructor(message, code, hint) {
@@ -2718,9 +3510,80 @@ class GatewayCallError extends Error {
2718
3510
  this.hint = hint;
2719
3511
  }
2720
3512
  }
3513
+ var DAY2 = 24 * 3600000;
2721
3514
  var GatewayServiceUtil;
2722
3515
  (function(GatewayServiceUtil2) {
2723
- GatewayServiceUtil2.SERVICES = ["TaGatewayServer", "TaAccessServer"];
3516
+ GatewayServiceUtil2.DEFAULT_LOG_LINES = 200;
3517
+ GatewayServiceUtil2.MAX_LOG_LINES = 2000;
3518
+ GatewayServiceUtil2.RULES = {
3519
+ TaGatewayServer: {
3520
+ getGateway: "anyone",
3521
+ setEnvironment: "anyone",
3522
+ restartEnvironment: "anyone",
3523
+ listTenants: "admin",
3524
+ getConsole: "admin",
3525
+ getTenantAgents: "admin",
3526
+ getAgentActivity: "admin",
3527
+ getTenantLogs: "admin",
3528
+ getUsageTrend: "admin",
3529
+ getPolicy: "admin",
3530
+ getAudit: "admin",
3531
+ startTenant: "admin",
3532
+ stopTenant: "admin",
3533
+ restartTenant: "admin",
3534
+ createSignInLink: "admin",
3535
+ setTenantLimits: "admin",
3536
+ grantMinutes: "admin",
3537
+ setSuspended: "admin",
3538
+ setDefaultLimits: "owner",
3539
+ setRole: "owner"
3540
+ },
3541
+ TaAccessServer: {
3542
+ getAccess: "anyone",
3543
+ signOut: "anyone",
3544
+ bindUrl: "admin",
3545
+ allow: "admin",
3546
+ dismiss: "admin",
3547
+ disallow: "admin"
3548
+ }
3549
+ };
3550
+ GatewayServiceUtil2.AUDITED = new Set([
3551
+ "startTenant",
3552
+ "stopTenant",
3553
+ "restartTenant",
3554
+ "getTenantLogs",
3555
+ "getAgentActivity",
3556
+ "createSignInLink",
3557
+ "setTenantLimits",
3558
+ "grantMinutes",
3559
+ "setSuspended",
3560
+ "setDefaultLimits",
3561
+ "setRole",
3562
+ "allow",
3563
+ "dismiss",
3564
+ "disallow"
3565
+ ]);
3566
+ const BY_SERVICE = new Map(Object.entries(GatewayServiceUtil2.RULES).map(([service, rules]) => [service, new Map(Object.entries(rules))]));
3567
+ function need(service, name) {
3568
+ var _a;
3569
+ return (_a = BY_SERVICE.get(service)) === null || _a === undefined ? undefined : _a.get(name);
3570
+ }
3571
+ GatewayServiceUtil2.need = need;
3572
+ function allowed(role, need2) {
3573
+ return need2 === "anyone" || GatewayPolicyUtil.rank(role) >= GatewayPolicyUtil.rank(need2);
3574
+ }
3575
+ GatewayServiceUtil2.allowed = allowed;
3576
+ function lines(value) {
3577
+ const count = Math.floor(Number(value !== null && value !== undefined ? value : GatewayServiceUtil2.DEFAULT_LOG_LINES));
3578
+ return Number.isFinite(count) && count > 0 ? Math.min(count, GatewayServiceUtil2.MAX_LOG_LINES) : GatewayServiceUtil2.DEFAULT_LOG_LINES;
3579
+ }
3580
+ GatewayServiceUtil2.lines = lines;
3581
+ function days(value) {
3582
+ const count = Math.floor(Number(value !== null && value !== undefined ? value : 7));
3583
+ return Number.isFinite(count) ? Math.min(31, Math.max(1, count)) : 7;
3584
+ }
3585
+ GatewayServiceUtil2.days = days;
3586
+ GatewayServiceUtil2.SERVICES = Object.keys(GatewayServiceUtil2.RULES);
2724
3587
  function owns(method) {
2725
3588
  return typeof method === "string" && GatewayServiceUtil2.SERVICES.includes(method.split("/")[0]);
2726
3589
  }
@@ -2729,6 +3592,11 @@ var GatewayServiceUtil;
2729
3592
  return GatewayServiceUtil2.SERVICES.some((service) => text.includes(`"${service}/`));
2730
3593
  }
2731
3594
  GatewayServiceUtil2.mayOwn = mayOwn;
3595
+ GatewayServiceUtil2.PROMPTS = ["TaServer/prompt", "TaServer/steer", "TaServer/followUp"];
3596
+ function mayPrompt(text) {
3597
+ return GatewayServiceUtil2.PROMPTS.some((method) => text.includes(`"${method}"`));
3598
+ }
3599
+ GatewayServiceUtil2.mayPrompt = mayPrompt;
2732
3600
  function error(id, message, code, hint) {
2733
3601
  return JSON.stringify({ jsonrpc: "2.0", id, error: { code: TA_RPC_ERROR_CODE, message, data: { kind: "application", data: { code, hint } } } });
2734
3602
  }
@@ -2740,18 +3608,22 @@ var GatewayServiceUtil;
2740
3608
  function invalid(message, hint) {
2741
3609
  return new GatewayCallError(message, TaErrorCode.INVALID_ARGUMENT, hint);
2742
3610
  }
3611
+ function forbidden(message, hint) {
3612
+ return new GatewayCallError(message, TaErrorCode.FORBIDDEN, hint);
3613
+ }
3614
+ GatewayServiceUtil2.forbidden = forbidden;
2743
3615
  function choice(value, own, providers) {
2744
3616
  var _a;
2745
3617
  const given = value !== null && value !== undefined ? value : {};
2746
3618
  if (typeof given.provider === "string") {
2747
3619
  if (!providers.includes(given.provider)) {
2748
- throw invalid(`The gateway has no provider ${given.provider}.`, "Pick one of the options of getGateway.");
3620
+ throw invalid(`The gateway has no provider ${given.provider} for you.`, "Pick one of the options of getGateway.");
2749
3621
  }
2750
3622
  return { provider: given.provider };
2751
3623
  }
2752
3624
  if (given.type === "ta") {
2753
3625
  if (!own) {
2754
- throw invalid("This gateway runs Ta in its own sandboxes only.", "Pick one of the options of getGateway.");
3626
+ throw invalid("This gateway runs your Ta in its own sandboxes only.", "Pick one of the options of getGateway.");
2755
3627
  }
2756
3628
  let url;
2757
3629
  try {
@@ -2767,56 +3639,120 @@ var GatewayServiceUtil;
2767
3639
  throw invalid("No environment was named.", 'Pass { provider } or { type: "ta", url, token }.');
2768
3640
  }
2769
3641
  GatewayServiceUtil2.choice = choice;
3642
+ function detail(method, params) {
3643
+ const [, second] = params;
3644
+ switch (method) {
3645
+ case "setTenantLimits":
3646
+ case "setDefaultLimits":
3647
+ return JSON.stringify(method === "setDefaultLimits" ? params[0] : second);
3648
+ case "grantMinutes":
3649
+ case "getTenantLogs":
3650
+ return String(second !== null && second !== undefined ? second : "");
3651
+ case "setSuspended":
3652
+ return second ? "suspended" : "let go";
3653
+ case "setRole":
3654
+ return String(second);
3655
+ default:
3656
+ return;
3657
+ }
3658
+ }
3659
+ GatewayServiceUtil2.detail = detail;
2770
3660
  })(GatewayServiceUtil || (GatewayServiceUtil = {}));
2771
3661
 
2772
3662
  class GatewayService {
2773
- constructor(flow, tenants, environments, hooks) {
2774
- this.flow = flow;
2775
- this.tenants = tenants;
2776
- this.environments = environments;
2777
- this.hooks = hooks;
3663
+ constructor(parts) {
3664
+ this.flow = parts.flow;
3665
+ this.tenants = parts.tenants;
3666
+ this.environments = parts.environments;
3667
+ this.policy = parts.policy;
3668
+ this.quotas = parts.quotas;
3669
+ this.usage = parts.usage;
3670
+ this.audit = parts.audit;
3671
+ this.hooks = parts.hooks;
3672
+ this.meta = parts.meta;
2778
3673
  }
2779
3674
  async call(caller, method, params) {
2780
3675
  const [service, name] = method.split("/");
3676
+ const need = GatewayServiceUtil.need(service, name);
3677
+ if (!need) {
3678
+ throw new GatewayCallError(`${method} is not a method of the gateway.`, TaErrorCode.INVALID_ARGUMENT, "Call a method of TaGatewayServer or TaAccessServer.");
3679
+ }
3680
+ if (!GatewayServiceUtil.allowed(caller.role, need)) {
3681
+ 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.");
3682
+ }
3683
+ const result = await this.handle(caller, service, name, params);
3684
+ if (GatewayServiceUtil.AUDITED.has(name)) {
3685
+ this.record(caller, name, params);
3686
+ }
3687
+ return result;
3688
+ }
3689
+ handle(caller, service, name, params) {
3690
+ const [first, second] = params;
2781
3691
  const handlers = service === "TaGatewayServer" ? {
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)
3692
+ getGateway: () => this.info(caller),
3693
+ setEnvironment: () => this.setEnvironment(caller, first),
3694
+ restartEnvironment: () => this.restart(caller),
3695
+ listTenants: () => this.tenants.list().map((tenant) => this.tenantInfo(tenant)),
3696
+ getConsole: () => this.console(caller),
3697
+ getTenantAgents: () => this.environments.agents(this.tenant(first)),
3698
+ getAgentActivity: () => this.activity(first, second),
3699
+ getTenantLogs: () => this.logs(first, second),
3700
+ getUsageTrend: () => this.trend(first, second),
3701
+ getPolicy: () => this.policyView(),
3702
+ getAudit: () => this.audit.list(Math.min(500, Math.max(1, Math.floor(Number(first !== null && first !== undefined ? first : 100)) || 100))),
3703
+ startTenant: () => this.startTenant(caller, first),
3704
+ stopTenant: () => this.stopTenant(caller, first),
3705
+ restartTenant: () => this.restartTenant(caller, first),
3706
+ createSignInLink: () => this.signInLink(first, second),
3707
+ setTenantLimits: () => this.setTenantLimits(caller, first, second),
3708
+ grantMinutes: () => this.grantMinutes(caller, first, second),
3709
+ setSuspended: () => this.setSuspended(caller, first, second),
3710
+ setDefaultLimits: () => this.setDefaultLimits(first),
3711
+ setRole: () => this.setRole(caller, first, second)
2787
3712
  } : {
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))
3713
+ getAccess: () => this.access(caller),
3714
+ bindUrl: () => this.flow.bindUrl(String(first)),
3715
+ allow: () => this.allow(first, second),
3716
+ dismiss: () => this.flow.store.dismiss(String(first)),
3717
+ disallow: () => this.disallow(caller, String(first)),
3718
+ signOut: () => this.signOut(caller, String(first))
2794
3719
  };
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.");
3720
+ return Promise.resolve().then(handlers[name]);
3721
+ }
3722
+ record(caller, action2, params) {
3723
+ var _a;
3724
+ var _b;
3725
+ const [first] = params;
3726
+ 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;
3727
+ try {
3728
+ this.audit.add({ actor: caller.account, action: action2, target, detail: GatewayServiceUtil.detail(action2, params) });
3729
+ } catch (error) {
3730
+ console.warn(`ta gateway: the audit could not be written: ${error.message}`);
2798
3731
  }
2799
- return handler(...params);
2800
3732
  }
2801
3733
  info(caller) {
2802
3734
  var _a;
2803
3735
  const tenant = (_a = this.tenants.get(caller.tenant.id)) !== null && _a !== undefined ? _a : caller.tenant;
3736
+ this.usage.refresh(tenant.id, this.environments.usageReader(tenant)).catch(() => {
3737
+ return;
3738
+ });
3739
+ const limits = this.quotas.limits(tenant);
2804
3740
  return {
2805
3741
  account: { id: tenant.account, name: tenant.name, avatar: tenant.avatar },
2806
3742
  tenant: tenant.id,
2807
3743
  environment: this.environments.info(tenant),
2808
- options: this.environments.choices(),
2809
- own: this.ownAllowed(),
2810
- admin: caller.admin
3744
+ options: this.environments.choices().filter((option) => !limits.providers || limits.providers.includes(option.id)),
3745
+ own: this.environments.settings.own && limits.own,
3746
+ admin: caller.admin,
3747
+ role: caller.role,
3748
+ quota: this.quotas.state(tenant)
2811
3749
  };
2812
3750
  }
2813
- ownAllowed() {
2814
- return this.environments.settings.own;
2815
- }
2816
3751
  async setEnvironment(caller, value) {
2817
3752
  var _a;
2818
- const choice = GatewayServiceUtil.choice(value, this.ownAllowed(), this.environments.providers.map((provider) => provider.id));
2819
3753
  const tenant = (_a = this.tenants.get(caller.tenant.id)) !== null && _a !== undefined ? _a : caller.tenant;
3754
+ const limits = this.quotas.limits(tenant);
3755
+ 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)));
2820
3756
  const next = await this.environments.change(tenant, choice);
2821
3757
  caller.tenant = next;
2822
3758
  this.hooks.reset(next.id);
@@ -2829,20 +3765,172 @@ class GatewayService {
2829
3765
  this.hooks.reset(caller.tenant.id);
2830
3766
  return this.info(caller);
2831
3767
  }
2832
- async listTenants(caller) {
2833
- return this.admin(caller, () => this.tenants.list().map((tenant) => ({
3768
+ tenantInfo(tenant) {
3769
+ return {
2834
3770
  id: tenant.id,
2835
3771
  account: tenant.account,
2836
3772
  name: tenant.name,
3773
+ avatar: tenant.avatar,
2837
3774
  environment: this.environments.info(tenant),
2838
3775
  connections: this.environments.connections(tenant.id),
2839
3776
  createdAt: tenant.createdAt,
2840
- lastActiveAt: tenant.lastActiveAt
2841
- })));
3777
+ lastActiveAt: tenant.lastActiveAt,
3778
+ role: this.quotas.role(tenant),
3779
+ quota: this.quotas.state(tenant),
3780
+ limits: tenant.limits,
3781
+ suspended: tenant.suspended || undefined
3782
+ };
3783
+ }
3784
+ async console(caller) {
3785
+ var _a;
3786
+ var _b, _c;
3787
+ const tenants = this.tenants.list();
3788
+ const [usages, agents] = await Promise.all([
3789
+ Promise.all(tenants.map((tenant) => this.environments.usage(tenant).catch(() => {
3790
+ return;
3791
+ }))),
3792
+ Promise.all(tenants.map((tenant) => this.environments.agentSummary(tenant).catch(() => {
3793
+ return;
3794
+ })))
3795
+ ]);
3796
+ const counts = new Map;
3797
+ for (const tenant of tenants) {
3798
+ const { provider } = this.environments.info(tenant);
3799
+ counts.set(provider, ((_b = counts.get(provider)) !== null && _b !== undefined ? _b : 0) + 1);
3800
+ }
3801
+ const fallback = (_c = this.environments.settings.default) !== null && _c !== undefined ? _c : (_a = this.environments.providers[0]) === null || _a === undefined ? undefined : _a.id;
3802
+ return {
3803
+ version: this.meta.version,
3804
+ startedAt: this.meta.startedAt,
3805
+ publicUrl: this.meta.publicUrl,
3806
+ providers: this.environments.providers.map((provider) => {
3807
+ var _a2;
3808
+ return {
3809
+ id: provider.id,
3810
+ name: provider.name,
3811
+ type: provider.type,
3812
+ default: provider.id === fallback,
3813
+ tenants: (_a2 = counts.get(provider.id)) !== null && _a2 !== undefined ? _a2 : 0,
3814
+ logs: typeof provider.logs === "function"
3815
+ };
3816
+ }),
3817
+ own: this.environments.settings.own,
3818
+ idleMinutes: this.environments.settings.idleMinutes,
3819
+ tenants: tenants.map((tenant, index) => ({ ...this.tenantInfo(tenant), usage: usages[index], agents: agents[index] })).sort((a, b) => {
3820
+ var _a2, _b2;
3821
+ return ((_a2 = b.lastActiveAt) !== null && _a2 !== undefined ? _a2 : b.createdAt) - ((_b2 = a.lastActiveAt) !== null && _b2 !== undefined ? _b2 : a.createdAt);
3822
+ }),
3823
+ requests: this.flow.store.info(caller.token).requests.length,
3824
+ role: caller.role,
3825
+ policy: this.policyView()
3826
+ };
3827
+ }
3828
+ policyView() {
3829
+ return this.policy.view(this.environments.settings.idleMinutes);
3830
+ }
3831
+ tenant(id) {
3832
+ const tenant = typeof id === "string" ? this.tenants.get(id) : undefined;
3833
+ if (!tenant) {
3834
+ throw new GatewayCallError(`The gateway has no tenant ${id}.`, TaErrorCode.INVALID_ARGUMENT, "Pick one of the tenants of getConsole.");
3835
+ }
3836
+ return tenant;
3837
+ }
3838
+ manage(caller, id) {
3839
+ const tenant = this.tenant(id);
3840
+ if (caller.role !== "owner" && (this.quotas.role(tenant) !== "member" || tenant.account === caller.account)) {
3841
+ throw GatewayServiceUtil.forbidden("Admins manage members only, not themselves, other admins or owners.", "Ask an owner of the gateway (TA_GATEWAY_ADMINS).");
3842
+ }
3843
+ return tenant;
3844
+ }
3845
+ async startTenant(caller, id) {
3846
+ await this.environments.ensure(this.manage(caller, id));
2842
3847
  }
2843
3848
  async stopTenant(caller, id) {
2844
- await this.admin(caller, () => this.environments.stop(String(id)));
2845
- this.hooks.reset(String(id));
3849
+ const tenant = this.manage(caller, id);
3850
+ await this.environments.stop(tenant.id);
3851
+ this.hooks.reset(tenant.id);
3852
+ }
3853
+ async restartTenant(caller, id) {
3854
+ const tenant = this.manage(caller, id);
3855
+ await this.environments.stop(tenant.id).catch(() => {
3856
+ return;
3857
+ });
3858
+ this.hooks.reset(tenant.id);
3859
+ await this.environments.ensure(tenant);
3860
+ }
3861
+ async logs(id, lines) {
3862
+ return new Redactor().text(await this.environments.logs(this.tenant(id), GatewayServiceUtil.lines(lines)));
3863
+ }
3864
+ activity(id, sessionId) {
3865
+ return this.environments.activity(this.tenant(id), String(sessionId));
3866
+ }
3867
+ async trend(daysValue, id) {
3868
+ const now = Date.now();
3869
+ const days = GatewayServiceUtil.days(daysValue);
3870
+ const tenants = id === undefined || id === null ? this.tenants.list() : [this.tenant(id)];
3871
+ const fresh = await Promise.all(tenants.map((tenant) => this.usage.refresh(tenant.id, this.environments.usageReader(tenant), now, 0).catch(() => false)));
3872
+ const byTenant = new Map;
3873
+ const stale = [];
3874
+ tenants.forEach((tenant, index) => {
3875
+ const kept = this.usage.get(tenant.id);
3876
+ if (kept) {
3877
+ byTenant.set(tenant.id, kept.buckets);
3878
+ if (!fresh[index]) {
3879
+ stale.push(tenant.id);
3880
+ }
3881
+ }
3882
+ });
3883
+ const since = Math.floor((now - days * DAY2) / 3600000) * 3600000;
3884
+ return GatewayUsageUtil.trend(byTenant, since, stale, now);
3885
+ }
3886
+ limits(value, nullable) {
3887
+ try {
3888
+ return GatewayPolicyUtil.limits(value, this.environments.providers.map((provider) => provider.id), nullable);
3889
+ } catch (error) {
3890
+ if (error instanceof PolicyError) {
3891
+ throw new GatewayCallError(error.message, TaErrorCode.INVALID_ARGUMENT, "Send the limits again, written well.");
3892
+ }
3893
+ throw error;
3894
+ }
3895
+ }
3896
+ setTenantLimits(caller, id, value) {
3897
+ const tenant = this.manage(caller, id);
3898
+ this.tenants.update(tenant.id, { limits: value === null ? undefined : this.limits(value, true) });
3899
+ }
3900
+ grantMinutes(caller, id, minutes) {
3901
+ const tenant = this.manage(caller, id);
3902
+ if (typeof minutes !== "number" || !Number.isInteger(minutes) || Math.abs(minutes) > 1e6) {
3903
+ throw new GatewayCallError("Minutes are a whole number.", TaErrorCode.INVALID_ARGUMENT, "Give, say, 60; a negative number takes some back.");
3904
+ }
3905
+ this.quotas.grant(tenant, minutes);
3906
+ }
3907
+ async setSuspended(caller, id, suspended) {
3908
+ const tenant = this.manage(caller, id);
3909
+ this.tenants.update(tenant.id, { suspended: suspended === true || undefined });
3910
+ if (suspended === true) {
3911
+ await this.environments.stop(tenant.id).catch(() => {
3912
+ return;
3913
+ });
3914
+ this.hooks.reset(tenant.id);
3915
+ }
3916
+ }
3917
+ setDefaultLimits(value) {
3918
+ this.policy.setDefaults(this.limits(value, false));
3919
+ return this.policyView();
3920
+ }
3921
+ setRole(caller, account, role) {
3922
+ const id = typeof account === "string" ? account.trim() : "";
3923
+ if (role !== "admin" && role !== "member") {
3924
+ throw new GatewayCallError("A role is admin or member.", TaErrorCode.INVALID_ARGUMENT, "Owners are those of TA_GATEWAY_ADMINS.");
3925
+ }
3926
+ if (this.policy.role(id) === "owner") {
3927
+ throw GatewayServiceUtil.forbidden("An owner stays one: they are of TA_GATEWAY_ADMINS.", "Change TA_GATEWAY_ADMINS where the gateway runs.");
3928
+ }
3929
+ if (id === caller.account || !this.tenants.list().some((tenant) => tenant.account === id)) {
3930
+ throw new GatewayCallError(`${id} has not signed in to the gateway.`, TaErrorCode.INVALID_ARGUMENT, "Make an account that signed in an admin.");
3931
+ }
3932
+ this.policy.setRole(id, role);
3933
+ return this.policyView();
2846
3934
  }
2847
3935
  access(caller) {
2848
3936
  const providers = this.flow.providers.map(({ id, name }) => ({ id, name }));
@@ -2858,10 +3946,18 @@ class GatewayService {
2858
3946
  }
2859
3947
  this.flow.store.allow(account.trim(), typeof name === "string" ? name : undefined);
2860
3948
  }
2861
- async disallow(account) {
2862
- this.flow.store.disallow(AccessUtil.normalize(account));
3949
+ async disallow(caller, account) {
3950
+ const normalized = AccessUtil.normalize(account);
3951
+ const role = this.policy.role(normalized);
3952
+ if (role === "owner" || role === "admin" && caller.role !== "owner") {
3953
+ 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.");
3954
+ }
3955
+ if (role === "admin") {
3956
+ this.policy.setRole(normalized, "member");
3957
+ }
3958
+ this.flow.store.disallow(normalized);
2863
3959
  this.hooks.cutOff();
2864
- for (const tenant of this.tenants.list().filter((one) => !this.flow.store.allows(one.account))) {
3960
+ for (const tenant of this.tenants.list().filter((one) => !this.flow.store.allows(one.account) && !this.policy.lets(one.account))) {
2865
3961
  const unpaired = await this.environments.revoke(tenant).catch((error) => {
2866
3962
  console.warn(`ta gateway: revoking ${tenant.account} failed: ${error.message}`);
2867
3963
  return 0;
@@ -2872,33 +3968,42 @@ class GatewayService {
2872
3968
  async signOut(caller, id) {
2873
3969
  const own = this.flow.store.info(caller.token).signIns.find((signIn) => signIn.id === id);
2874
3970
  if (!caller.admin && (own === null || own === undefined ? undefined : own.account) !== caller.account) {
2875
- throw new GatewayCallError("That sign-in is not yours.", TaErrorCode.INVALID_ARGUMENT, "Sign out of the browsers of your own account.");
3971
+ throw GatewayServiceUtil.forbidden("That sign-in is not yours.", "Sign out of the browsers of your own account.");
2876
3972
  }
2877
3973
  this.flow.store.signOut(id);
2878
3974
  this.hooks.cutOff();
2879
3975
  }
2880
- async admin(caller, action2) {
2881
- if (!caller.admin) {
2882
- throw new GatewayCallError("Only administrators of the gateway may do that.", TaErrorCode.INVALID_ARGUMENT, "Ask one of those of TA_GATEWAY_ADMINS.");
3976
+ signInLink(account, name) {
3977
+ const id = typeof account === "string" ? account.trim() : "";
3978
+ if (!/^[a-z][a-z0-9-]*:\S+$/.test(id)) {
3979
+ throw new GatewayCallError(`${account} is not an account.`, TaErrorCode.INVALID_ARGUMENT, "Write it <kind>:<name>, such as github:octocat or user:alice.");
3980
+ }
3981
+ if (this.tenants.list().some((tenant) => tenant.account === id) || this.policy.role(id) !== "member") {
3982
+ 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.");
2883
3983
  }
2884
- return action2();
3984
+ return this.meta.links.create(id, typeof name === "string" && name.trim() ? name.trim() : undefined);
2885
3985
  }
2886
3986
  }
2887
3987
 
2888
3988
  // ../../ta-packages/ta-gateway/esm/node/gateway-socket.js
2889
3989
  var MAX_QUEUED = 256;
2890
3990
  var MAX_QUEUED_BYTES = 16 * 1024 * 1024;
3991
+ var FLOOD_MESSAGES = 2000;
3992
+ var FLOOD_WINDOW_MS = 1e4;
2891
3993
 
2892
3994
  class GatewaySocket {
2893
- constructor(browser, caller, environments, service) {
3995
+ constructor(browser, caller, environments, service, quotas) {
2894
3996
  this.browser = browser;
2895
3997
  this.caller = caller;
2896
3998
  this.environments = environments;
2897
3999
  this.service = service;
4000
+ this.quotas = quotas;
2898
4001
  this.queued = [];
2899
4002
  this.queuedBytes = 0;
2900
4003
  this.closed = false;
2901
4004
  this.answering = 0;
4005
+ this.windowStart = Date.now();
4006
+ this.windowCount = 0;
2902
4007
  this.release = environments.connect(caller.tenant.id);
2903
4008
  browser.onMessage((text) => this.fromBrowser(text));
2904
4009
  browser.onClose(() => this.dispose());
@@ -2947,6 +4052,18 @@ class GatewaySocket {
2947
4052
  };
2948
4053
  }
2949
4054
  fromBrowser(text) {
4055
+ const now = Date.now();
4056
+ if (now - this.windowStart > FLOOD_WINDOW_MS) {
4057
+ this.windowStart = now;
4058
+ this.windowCount = 0;
4059
+ }
4060
+ if (++this.windowCount > FLOOD_MESSAGES) {
4061
+ this.close(1008, "Too many messages.");
4062
+ return;
4063
+ }
4064
+ if (this.failure === undefined && GatewayServiceUtil.mayPrompt(text) && !this.admitPrompt(text)) {
4065
+ return;
4066
+ }
2950
4067
  if (this.failure === undefined && !GatewayServiceUtil.mayOwn(text)) {
2951
4068
  this.forward(text);
2952
4069
  return;
@@ -2970,6 +4087,25 @@ class GatewaySocket {
2970
4087
  }
2971
4088
  this.forward(text);
2972
4089
  }
4090
+ admitPrompt(text) {
4091
+ let message;
4092
+ try {
4093
+ message = JSON.parse(text);
4094
+ } catch (_a) {
4095
+ return true;
4096
+ }
4097
+ if (typeof message.method !== "string" || !GatewayServiceUtil.PROMPTS.includes(message.method) || !this.quotas) {
4098
+ return true;
4099
+ }
4100
+ if (this.quotas.takePrompt(this.caller.tenant)) {
4101
+ return true;
4102
+ }
4103
+ const seconds = Math.ceil(this.quotas.retryAfter(this.caller.tenant) / 1000);
4104
+ if (message.id !== undefined) {
4105
+ 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.`));
4106
+ }
4107
+ return false;
4108
+ }
2973
4109
  forward(text) {
2974
4110
  var _a;
2975
4111
  if (((_a = this.inner) === null || _a === undefined ? undefined : _a.readyState) === WebSocket.OPEN) {
@@ -3018,10 +4154,34 @@ class GatewaySocket {
3018
4154
 
3019
4155
  // ../../ta-packages/ta-gateway/esm/node/local-sandbox.js
3020
4156
  import { spawn as spawn2 } from "node:child_process";
3021
- import { mkdirSync as mkdirSync2 } from "node:fs";
4157
+ import { mkdirSync as mkdirSync4 } from "node:fs";
3022
4158
  import { createServer } from "node:net";
3023
- import { join as join2 } from "node:path";
4159
+ import { join as join6 } from "node:path";
3024
4160
  var STOP_TIMEOUT_MS = 1e4;
4161
+ var KEPT_LINES = 2000;
4162
+
4163
+ class LineTail {
4164
+ constructor(max = KEPT_LINES) {
4165
+ this.max = max;
4166
+ this.lines = [];
4167
+ this.partial = "";
4168
+ }
4169
+ write(chunk) {
4170
+ var _a;
4171
+ const parts = (this.partial + chunk).split(`
4172
+ `);
4173
+ this.partial = (_a = parts.pop()) !== null && _a !== undefined ? _a : "";
4174
+ this.lines.push(...parts);
4175
+ if (this.lines.length > this.max) {
4176
+ this.lines.splice(0, this.lines.length - this.max);
4177
+ }
4178
+ }
4179
+ last(count) {
4180
+ const lines = this.partial ? [...this.lines, this.partial] : this.lines;
4181
+ return lines.slice(-count).join(`
4182
+ `);
4183
+ }
4184
+ }
3025
4185
 
3026
4186
  class LocalSandbox {
3027
4187
  constructor(id, config, context) {
@@ -3029,29 +4189,35 @@ class LocalSandbox {
3029
4189
  this.context = context;
3030
4190
  this.type = "local";
3031
4191
  this.running = new Map;
4192
+ this.output = new Map;
3032
4193
  this.name = SandboxProviders.string(config, "name", "Local");
3033
4194
  }
3034
4195
  async start(tenant) {
4196
+ var _a, _b;
3035
4197
  const running = this.running.get(tenant.id);
3036
4198
  if (running && running.child.exitCode === null) {
3037
4199
  return { url: running.url, token: tenant.token };
3038
4200
  }
3039
- const root = join2(this.context.dir, "local", tenant.id);
3040
- const agentDir = join2(root, "agent");
3041
- const cwd = join2(root, "workspace");
3042
- mkdirSync2(agentDir, { recursive: true });
3043
- mkdirSync2(cwd, { recursive: true });
4201
+ const root = join6(this.context.dir, "local", tenant.id);
4202
+ const agentDir = join6(root, "agent");
4203
+ const cwd = join6(root, "workspace");
4204
+ mkdirSync4(agentDir, { recursive: true });
4205
+ mkdirSync4(cwd, { recursive: true });
3044
4206
  const port = await LocalSandbox.freePort();
3045
4207
  const [command, ...args] = this.context.taCommand;
3046
4208
  const child = spawn2(command, [...args, "serve", "--port", String(port)], {
3047
4209
  cwd,
3048
4210
  env: { ...process.env, TA_CODING_AGENT_DIR: agentDir, TA_SERVE_TOKEN: tenant.token, CELL_SERVER__HOST: "127.0.0.1" },
3049
- stdio: "ignore"
4211
+ stdio: ["ignore", "pipe", "pipe"]
3050
4212
  });
4213
+ const tail = new LineTail;
4214
+ this.output.set(tenant.id, tail);
4215
+ (_a = child.stdout) === null || _a === undefined || _a.setEncoding("utf8").on("data", (data) => tail.write(data));
4216
+ (_b = child.stderr) === null || _b === undefined || _b.setEncoding("utf8").on("data", (data) => tail.write(data));
3051
4217
  const url = `http://127.0.0.1:${port}`;
3052
4218
  const gone = () => {
3053
- var _a;
3054
- if (((_a = this.running.get(tenant.id)) === null || _a === undefined ? undefined : _a.child) === child) {
4219
+ var _a2;
4220
+ if (((_a2 = this.running.get(tenant.id)) === null || _a2 === undefined ? undefined : _a2.child) === child) {
3055
4221
  this.running.delete(tenant.id);
3056
4222
  }
3057
4223
  };
@@ -3080,6 +4246,11 @@ class LocalSandbox {
3080
4246
  async state(tenant) {
3081
4247
  return this.running.has(tenant.id) ? "running" : "stopped";
3082
4248
  }
4249
+ async logs(tenant, lines) {
4250
+ var _a;
4251
+ var _b;
4252
+ return (_b = (_a = this.output.get(tenant.id)) === null || _a === undefined ? undefined : _a.last(lines)) !== null && _b !== undefined ? _b : "";
4253
+ }
3083
4254
  async dispose() {
3084
4255
  await Promise.all([...this.running.keys()].map((id) => this.stop({ id, account: "", token: "" })));
3085
4256
  }
@@ -3097,93 +4268,14 @@ class LocalSandbox {
3097
4268
 
3098
4269
  // ../../ta-packages/ta-gateway/esm/node/sign-in-links.js
3099
4270
  import { createHash as createHash5, randomBytes as randomBytes4 } from "node:crypto";
3100
- import { readFileSync as readFileSync4 } from "node:fs";
3101
- import { join as join4 } from "node:path";
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
4271
+ import { readFileSync as readFileSync7 } from "node:fs";
4272
+ import { join as join7 } from "node:path";
3181
4273
  var SIGN_IN_LINK_PATH = "/sign-in/link/";
3182
4274
  var hash = (secret) => createHash5("sha256").update(secret).digest("base64url");
3183
4275
 
3184
4276
  class SignInLinks {
3185
4277
  constructor(dir) {
3186
- this.file = join4(dir, "links.json");
4278
+ this.file = join7(dir, "links.json");
3187
4279
  }
3188
4280
  create(account, name, ttlMs = 24 * 3600 * 1000, now = Date.now()) {
3189
4281
  if (!/^[a-z][a-z0-9-]*:\S+$/.test(account)) {
@@ -3205,7 +4297,7 @@ class SignInLinks {
3205
4297
  }
3206
4298
  read(now) {
3207
4299
  try {
3208
- const links = JSON.parse(readFileSync4(this.file, "utf8"));
4300
+ const links = JSON.parse(readFileSync7(this.file, "utf8"));
3209
4301
  return Array.isArray(links) ? links.filter((link) => link.expiresAt > now) : [];
3210
4302
  } catch (_a) {
3211
4303
  return [];
@@ -3214,6 +4306,7 @@ class SignInLinks {
3214
4306
  }
3215
4307
 
3216
4308
  // ../../ta-packages/ta-gateway/esm/node/gateway-server.js
4309
+ var TICK_MS = 30000;
3217
4310
  var FILES_COOKIE = "ta_gateway";
3218
4311
  var TYPES = {
3219
4312
  ".html": "text/html; charset=utf-8",
@@ -3237,6 +4330,17 @@ var SANDBOX_TYPES = {
3237
4330
  };
3238
4331
  var GatewayServerUtil;
3239
4332
  (function(GatewayServerUtil2) {
4333
+ function blockedReason(block) {
4334
+ switch (block) {
4335
+ case "suspended":
4336
+ return "Your account is suspended by an administrator of the gateway.";
4337
+ case "minutes":
4338
+ return "Your free minutes of this month are used up.";
4339
+ default:
4340
+ return;
4341
+ }
4342
+ }
4343
+ GatewayServerUtil2.blockedReason = blockedReason;
3240
4344
  function file(root, urlPath) {
3241
4345
  let path;
3242
4346
  try {
@@ -3244,11 +4348,11 @@ var GatewayServerUtil;
3244
4348
  } catch (_a) {
3245
4349
  path = "/";
3246
4350
  }
3247
- const absolute = normalize(join5(root, path));
3248
- if ((absolute === root || absolute.startsWith(root.endsWith(sep) ? root : root + sep)) && existsSync2(absolute) && statSync(absolute).isFile()) {
4351
+ const absolute = normalize(join8(root, path));
4352
+ if ((absolute === root || absolute.startsWith(root.endsWith(sep) ? root : root + sep)) && existsSync3(absolute) && statSync(absolute).isFile()) {
3249
4353
  return absolute;
3250
4354
  }
3251
- return join5(root, "index.html");
4355
+ return join8(root, "index.html");
3252
4356
  }
3253
4357
  GatewayServerUtil2.file = file;
3254
4358
  function filesTarget(requestUrl, endpoint) {
@@ -3265,7 +4369,7 @@ var GatewayServerUtil;
3265
4369
 
3266
4370
  class GatewayServer {
3267
4371
  static async create(options) {
3268
- mkdirSync4(options.dir, { recursive: true, mode: 448 });
4372
+ mkdirSync5(options.dir, { recursive: true, mode: 448 });
3269
4373
  const config = SandboxProviders.read(options.dir);
3270
4374
  const context = { dir: options.dir, taCommand: options.taCommand };
3271
4375
  const providers = await SandboxProviders.create(config, context, SANDBOX_TYPES);
@@ -3276,9 +4380,10 @@ class GatewayServer {
3276
4380
  this.options = options;
3277
4381
  this.sockets = new Set;
3278
4382
  const env = (_a = options.env) !== null && _a !== undefined ? _a : process.env;
3279
- this.admins = AccessUtil.parseList(env.TA_GATEWAY_ADMINS);
3280
- const allowed = [...AccessUtil.parseList(env.TA_ALLOWED_USERS), ...this.admins];
3281
- this.flow = new SignInFlow(SignInProviders.fromEnv(env), new AccessStore(join5(options.dir, "access.json"), allowed), SignInProviders.publicUrl(env));
4383
+ const owners = AccessUtil.parseList(env.TA_GATEWAY_ADMINS);
4384
+ this.policy = new GatewayPolicy(options.dir, owners);
4385
+ const allowed = [...AccessUtil.parseList(env.TA_ALLOWED_USERS), ...owners];
4386
+ this.flow = new SignInFlow(SignInProviders.fromEnv(env), new AccessStore(join8(options.dir, "access.json"), allowed), SignInProviders.publicUrl(env));
3282
4387
  this.tenants = new TenantStore(options.dir);
3283
4388
  this.links = new SignInLinks(options.dir);
3284
4389
  this.environments = new Environments(providers, this.tenants, {
@@ -3286,25 +4391,55 @@ class GatewayServer {
3286
4391
  own: config.ownEnvironments !== false,
3287
4392
  idleMinutes: (_b = config.idleMinutes) !== null && _b !== undefined ? _b : 0
3288
4393
  });
3289
- this.service = new GatewayService(this.flow, this.tenants, this.environments, { cutOff: () => this.cutOff(), reset: (id) => this.reset(id) });
4394
+ this.usage = new GatewayUsage(options.dir);
4395
+ this.audit = new GatewayAudit(options.dir);
4396
+ this.quotas = new GatewayQuotas(this.policy, this.tenants, this.usage, () => this.environments.settings.own);
4397
+ this.environments.guard = (tenant) => GatewayServerUtil.blockedReason(this.quotas.blocked(tenant));
4398
+ this.service = new GatewayService({
4399
+ flow: this.flow,
4400
+ tenants: this.tenants,
4401
+ environments: this.environments,
4402
+ policy: this.policy,
4403
+ quotas: this.quotas,
4404
+ usage: this.usage,
4405
+ audit: this.audit,
4406
+ hooks: { cutOff: () => this.cutOff(), reset: (id) => this.reset(id) },
4407
+ meta: { links: this.links, startedAt: Date.now(), version: options.version, publicUrl: SandboxProviders.publicBase(env) }
4408
+ });
3290
4409
  }
3291
4410
  isAdmin(account) {
3292
- return this.admins.some((pattern) => AccessUtil.matches(pattern, account));
4411
+ return this.policy.role(account) !== "member";
4412
+ }
4413
+ async tick(now = Date.now()) {
4414
+ for (const tenant of this.quotas.tick(this.environments.runningIds(), now)) {
4415
+ console.log(`ta gateway: ${tenant.account} used its free minutes of the month: its sandbox stops`);
4416
+ await this.environments.stop(tenant.id).catch(() => {
4417
+ return;
4418
+ });
4419
+ this.reset(tenant.id);
4420
+ }
3293
4421
  }
3294
4422
  listen() {
4423
+ var _a, _b;
3295
4424
  const server = createServer2((request2, response) => void this.handle(request2, response));
3296
4425
  server.on("upgrade", (request2, socket, head) => this.upgrade(request2, socket, head));
3297
4426
  this.server = server;
4427
+ this.ticker = setInterval(() => void this.tick().catch((error) => console.warn(`ta gateway: counting minutes failed: ${error.message}`)), TICK_MS);
4428
+ (_b = (_a = this.ticker).unref) === null || _b === undefined || _b.call(_a);
3298
4429
  return new Promise((resolve, reject) => {
3299
- var _a, _b;
4430
+ var _a2, _b2;
3300
4431
  server.once("error", reject);
3301
- server.listen((_a = this.options.port) !== null && _a !== undefined ? _a : 4100, (_b = this.options.host) !== null && _b !== undefined ? _b : "127.0.0.1", () => {
4432
+ server.listen((_a2 = this.options.port) !== null && _a2 !== undefined ? _a2 : 4100, (_b2 = this.options.host) !== null && _b2 !== undefined ? _b2 : "127.0.0.1", () => {
3302
4433
  const { address, port } = server.address();
3303
4434
  resolve(`http://${address.includes(":") ? `[${address}]` : address}:${port}`);
3304
4435
  });
3305
4436
  });
3306
4437
  }
3307
4438
  async close() {
4439
+ clearInterval(this.ticker);
4440
+ await this.tick().catch(() => {
4441
+ return;
4442
+ });
3308
4443
  for (const socket of this.sockets) {
3309
4444
  socket.close(1001, "The gateway is stopping.");
3310
4445
  }
@@ -3332,7 +4467,8 @@ class GatewayServer {
3332
4467
  return;
3333
4468
  }
3334
4469
  const tenant = this.tenants.ensure({ id: signIn.account, name: signIn.name, avatar: signIn.avatar });
3335
- return { token, account: signIn.account, tenant, admin: this.isAdmin(signIn.account) };
4470
+ const role = this.policy.role(signIn.account);
4471
+ return { token, account: signIn.account, tenant, role, admin: role !== "member" };
3336
4472
  }
3337
4473
  async handle(request2, response) {
3338
4474
  var _a;
@@ -3465,9 +4601,14 @@ class GatewayServer {
3465
4601
  WebSocketConnection.refuse(socket, 401, "Not authorized.");
3466
4602
  return;
3467
4603
  }
4604
+ const most = this.quotas.limits(caller.tenant).connections;
4605
+ if (most !== undefined && this.environments.connections(caller.tenant.id) >= most) {
4606
+ WebSocketConnection.refuse(socket, 429, `At most ${most} pages of your account connect at once.`);
4607
+ return;
4608
+ }
3468
4609
  this.tenants.update(caller.tenant.id, { lastActiveAt: Date.now() });
3469
4610
  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);
4611
+ const gatewaySocket = new GatewaySocket(connection, caller, this.environments, this.service, this.quotas);
3471
4612
  this.sockets.add(gatewaySocket);
3472
4613
  connection.onClose(() => this.sockets.delete(gatewaySocket));
3473
4614
  }
@@ -3515,7 +4656,7 @@ var GatewayCli;
3515
4656
  process.stdout.write(HELP);
3516
4657
  return;
3517
4658
  }
3518
- const dir = (_b = (_a = values.dir) !== null && _a !== undefined ? _a : process.env.TA_GATEWAY_DIR) !== null && _b !== undefined ? _b : join6(homedir(), ".ta", "gateway");
4659
+ const dir = (_b = (_a = values.dir) !== null && _a !== undefined ? _a : process.env.TA_GATEWAY_DIR) !== null && _b !== undefined ? _b : join9(homedir(), ".ta", "gateway");
3519
4660
  if (positionals[0] === "link") {
3520
4661
  link(dir, positionals[1], values.name);
3521
4662
  return;
@@ -3532,7 +4673,8 @@ var GatewayCli;
3532
4673
  host: values.host,
3533
4674
  port: Number((_d = (_c = values.port) !== null && _c !== undefined ? _c : process.env.PORT) !== null && _d !== undefined ? _d : 4100),
3534
4675
  publicDir: options.publicDir,
3535
- taCommand: options.taCommand
4676
+ taCommand: options.taCommand,
4677
+ version: options.version
3536
4678
  });
3537
4679
  } catch (error) {
3538
4680
  console.error(`ta gateway: ${error.message}`);