shapewall 0.0.0-stage → 0.2.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 (304) hide show
  1. package/CHANGELOG.md +139 -0
  2. package/Dockerfile.agent +45 -0
  3. package/LICENSE +202 -0
  4. package/NOTICE +12 -0
  5. package/README.md +651 -2
  6. package/THIRD_PARTY.md +165 -0
  7. package/bin/shapewall.js +29 -0
  8. package/dist/agent/package-lock.json +8384 -0
  9. package/dist/client/assets/BoardChat-D5SFd8HE.js +28 -0
  10. package/dist/client/assets/BuzzDialog-HWXBy52W.js +1 -0
  11. package/dist/client/assets/ChecksPanel-DTdImWoD.js +1 -0
  12. package/dist/client/assets/History-CEv6b-av.js +1 -0
  13. package/dist/client/assets/LocalSettings-C4vriEVQ.js +1 -0
  14. package/dist/client/assets/Management-DCfhcEUY.js +1 -0
  15. package/dist/client/assets/QuickConnect-DQGgUsl1.js +1 -0
  16. package/dist/client/assets/ShareDialog-C_plIto0.js +2 -0
  17. package/dist/client/assets/app-update-dialog-2hPz_N9G.js +1 -0
  18. package/dist/client/assets/circle-x-BZMPi8Zf.js +1 -0
  19. package/dist/client/assets/index-ByQRKVit.css +2 -0
  20. package/dist/client/assets/index-DweQ8Qtu.js +29 -0
  21. package/dist/client/assets/refresh-cw-CRow0Nbq.js +1 -0
  22. package/dist/client/assets/runtime-Bft6x_6E.js +4 -0
  23. package/dist/client/assets/square-A8EnIYLR.js +1 -0
  24. package/dist/client/assets/tts-dist-DYlBQmwG.js +18 -0
  25. package/dist/client/assets/tts-en-g2p-BPUtBFGG.js +1 -0
  26. package/dist/client/assets/tts-ort-wasm-simd-threaded-qh98TCA_.wasm +0 -0
  27. package/dist/client/assets/tts-ort.wasm.bundle.min-rEkcs_ow.js +12 -0
  28. package/dist/client/assets/tts-rolldown-runtime-D8NudN84.js +1 -0
  29. package/dist/client/assets/tts-spacy-en-tokenizer-6SNMl--A.json +1 -0
  30. package/dist/client/assets/tts-tts.worker-Bzm6aWbv.js +1 -0
  31. package/dist/client/assets/tts-us_gold-DcjeNMH1.json +92589 -0
  32. package/dist/client/assets/tts-us_silver-C964mEB5.json +93363 -0
  33. package/dist/client/assets/ui-CLbws0rx.js +41 -0
  34. package/dist/client/build-info.json +1 -0
  35. package/dist/client/favicon.svg +7 -0
  36. package/dist/client/icons/apple-touch-icon.png +0 -0
  37. package/dist/client/icons/icon-192.png +0 -0
  38. package/dist/client/icons/icon-512.png +0 -0
  39. package/dist/client/icons/maskable-512.png +0 -0
  40. package/dist/client/index.html +25 -0
  41. package/dist/client/manifest.webmanifest +15 -0
  42. package/dist/client/sw.js +53 -0
  43. package/dist/client/theme-boot.js +27 -0
  44. package/dist/console/assets/LocalAccountsApp-BQCcmj3x.js +1 -0
  45. package/dist/console/assets/LocalConsole-Bb2FUDs7.js +1 -0
  46. package/dist/console/assets/app-update-dialog-DIeYepLZ.js +1 -0
  47. package/dist/console/assets/console-C7uJLLOn.js +51 -0
  48. package/dist/console/assets/console-R8IBY3Am.css +2 -0
  49. package/dist/console/build-info.json +1 -0
  50. package/dist/console/console.html +21 -0
  51. package/dist/console/favicon.svg +7 -0
  52. package/dist/console/icons/apple-touch-icon.png +0 -0
  53. package/dist/console/icons/icon-192.png +0 -0
  54. package/dist/console/icons/icon-512.png +0 -0
  55. package/dist/console/icons/maskable-512.png +0 -0
  56. package/dist/console/manifest.webmanifest +15 -0
  57. package/dist/console/theme-boot.js +27 -0
  58. package/docs/agent-hardening.md +342 -0
  59. package/docs/agent-recipes.md +187 -0
  60. package/docs/agent-workflow.md +320 -0
  61. package/docs/agents.md +894 -0
  62. package/docs/architecture.md +242 -0
  63. package/docs/buzz.md +140 -0
  64. package/docs/capture-warnings.md +57 -0
  65. package/docs/config.md +489 -0
  66. package/docs/deployment.md +289 -0
  67. package/docs/design-system.md +162 -0
  68. package/docs/github.md +107 -0
  69. package/docs/hosted.md +764 -0
  70. package/docs/images/board-overview.png +0 -0
  71. package/docs/images/journeys.png +0 -0
  72. package/docs/images/live-comment.png +0 -0
  73. package/docs/images/phone-comment.png +0 -0
  74. package/docs/images/phone-overview.png +0 -0
  75. package/docs/images/variant.png +0 -0
  76. package/docs/interaction-audits.md +40 -0
  77. package/docs/journeys.md +137 -0
  78. package/docs/local-accounts.md +80 -0
  79. package/docs/notifications.md +237 -0
  80. package/docs/page-checks.md +69 -0
  81. package/docs/performance.md +174 -0
  82. package/docs/starter-agent.md +325 -0
  83. package/docs/updates.md +87 -0
  84. package/examples/audits/interaction.audit.json +17 -0
  85. package/package.json +132 -4
  86. package/src/accounts/avatar.js +121 -0
  87. package/src/accounts/local.js +464 -0
  88. package/src/agent/acp/command.js +204 -0
  89. package/src/agent/acp/detect.js +75 -0
  90. package/src/agent/acp/sandbox.js +160 -0
  91. package/src/agent/agent-health.js +75 -0
  92. package/src/agent/backends/acp.js +1297 -0
  93. package/src/agent/backends/index.js +61 -0
  94. package/src/agent/backends/openai.js +308 -0
  95. package/src/agent/composer.js +221 -0
  96. package/src/agent/config.js +338 -0
  97. package/src/agent/context.js +191 -0
  98. package/src/agent/control.js +122 -0
  99. package/src/agent/errors.js +17 -0
  100. package/src/agent/feedback-loop.js +137 -0
  101. package/src/agent/manage.js +94 -0
  102. package/src/agent/next-steps.js +51 -0
  103. package/src/agent/owner-mcp.js +38 -0
  104. package/src/agent/plain-text.js +52 -0
  105. package/src/agent/ratings-core.js +213 -0
  106. package/src/agent/ratings.js +100 -0
  107. package/src/agent/reply-text.js +67 -0
  108. package/src/agent/routes.js +1065 -0
  109. package/src/agent/run.js +326 -0
  110. package/src/agent/settings-routes.js +704 -0
  111. package/src/agent/showable.js +23 -0
  112. package/src/agent/starter/approvals.js +181 -0
  113. package/src/agent/starter/build-image.js +90 -0
  114. package/src/agent/starter/cli.js +163 -0
  115. package/src/agent/starter/disk.js +53 -0
  116. package/src/agent/starter/doctor.js +180 -0
  117. package/src/agent/starter/egress-proxy.js +351 -0
  118. package/src/agent/starter/gate.js +90 -0
  119. package/src/agent/starter/loop.js +114 -0
  120. package/src/agent/starter/model-relay.js +289 -0
  121. package/src/agent/starter/pause.js +14 -0
  122. package/src/agent/starter/probe.js +272 -0
  123. package/src/agent/starter/prompt.js +41 -0
  124. package/src/agent/starter/push-guard.js +146 -0
  125. package/src/agent/starter/rpc.js +164 -0
  126. package/src/agent/starter/runtime.js +287 -0
  127. package/src/agent/starter/sandbox-main.js +121 -0
  128. package/src/agent/starter/settings.js +290 -0
  129. package/src/agent/starter/source.js +69 -0
  130. package/src/agent/starter/supervisor.js +755 -0
  131. package/src/agent/starter/sweep.js +46 -0
  132. package/src/agent/starter/tools.js +524 -0
  133. package/src/agent/starter/watch.js +84 -0
  134. package/src/agent/starter/worktree.js +409 -0
  135. package/src/agent/text-diff.js +210 -0
  136. package/src/agent/thread-history.js +223 -0
  137. package/src/agent/threads.js +399 -0
  138. package/src/agent/tools.js +200 -0
  139. package/src/agent/turn-changes.js +424 -0
  140. package/src/agent/undo-state.js +26 -0
  141. package/src/agent/user-settings.js +308 -0
  142. package/src/auth/record.js +375 -0
  143. package/src/broken-links.js +164 -0
  144. package/src/buzz/client.js +75 -0
  145. package/src/buzz/config.js +151 -0
  146. package/src/buzz/environment.js +13 -0
  147. package/src/buzz/gateway.js +192 -0
  148. package/src/buzz/routes.js +61 -0
  149. package/src/buzz/signer-child.js +205 -0
  150. package/src/buzz/store.js +89 -0
  151. package/src/buzz/transport.js +60 -0
  152. package/src/buzz/verify.js +140 -0
  153. package/src/capture/actions.js +159 -0
  154. package/src/capture/capture.js +776 -0
  155. package/src/capture/dom-names.js +189 -0
  156. package/src/capture/element-map.js +509 -0
  157. package/src/capture/live.js +827 -0
  158. package/src/capture/media-scheme.js +317 -0
  159. package/src/capture/network-summary.js +86 -0
  160. package/src/capture/pacing.js +34 -0
  161. package/src/capture/requests.js +167 -0
  162. package/src/capture/sanitize.js +752 -0
  163. package/src/checks/accessibility.js +134 -0
  164. package/src/checks/budgets.js +58 -0
  165. package/src/checks/cli.js +55 -0
  166. package/src/checks/comparison.js +113 -0
  167. package/src/checks/environments.js +32 -0
  168. package/src/checks/flow-config.js +82 -0
  169. package/src/checks/flow-origin.js +53 -0
  170. package/src/checks/flows.js +398 -0
  171. package/src/checks/lighthouse-auth.js +145 -0
  172. package/src/checks/lighthouse-findings.js +130 -0
  173. package/src/checks/lighthouse.js +395 -0
  174. package/src/checks/robots.js +85 -0
  175. package/src/checks/routes.js +52 -0
  176. package/src/checks/run-control.js +259 -0
  177. package/src/checks/runner.js +724 -0
  178. package/src/checks/schema.js +133 -0
  179. package/src/checks/seo-analysis.js +443 -0
  180. package/src/checks/seo-probe.js +190 -0
  181. package/src/checks/seo-snapshot.js +99 -0
  182. package/src/checks/store.js +214 -0
  183. package/src/checks/transport.js +47 -0
  184. package/src/cli/args.js +319 -0
  185. package/src/cli/capture.js +79 -0
  186. package/src/cli/commands.js +92 -0
  187. package/src/cli/comments.js +84 -0
  188. package/src/cli/help.js +243 -0
  189. package/src/cli/init.js +214 -0
  190. package/src/cli/misc.js +64 -0
  191. package/src/cli/notify.js +87 -0
  192. package/src/cli/open.js +22 -0
  193. package/src/cli/serve.js +274 -0
  194. package/src/cli/variant.js +82 -0
  195. package/src/cli/watch.js +70 -0
  196. package/src/cli.js +130 -0
  197. package/src/comment-fields.js +76 -0
  198. package/src/comments-cli.js +208 -0
  199. package/src/config-reload.js +100 -0
  200. package/src/config.js +1061 -0
  201. package/src/devices-cli.js +21 -0
  202. package/src/diff.js +95 -0
  203. package/src/discover.js +284 -0
  204. package/src/doctor.js +295 -0
  205. package/src/event-log.js +268 -0
  206. package/src/events-cli.js +102 -0
  207. package/src/events.js +113 -0
  208. package/src/export.js +119 -0
  209. package/src/feedback.js +246 -0
  210. package/src/github.js +551 -0
  211. package/src/hosted/LICENSE.md +105 -0
  212. package/src/hosted/access-cli.js +78 -0
  213. package/src/hosted/admin.js +682 -0
  214. package/src/hosted/agent-approvals.js +163 -0
  215. package/src/hosted/agent-policy.js +189 -0
  216. package/src/hosted/app.js +510 -0
  217. package/src/hosted/auth-routes.js +540 -0
  218. package/src/hosted/auth.js +536 -0
  219. package/src/hosted/backup.js +483 -0
  220. package/src/hosted/board-context.js +35 -0
  221. package/src/hosted/board-routes.js +486 -0
  222. package/src/hosted/board-view.js +94 -0
  223. package/src/hosted/clean-worker.js +24 -0
  224. package/src/hosted/cleaner.js +89 -0
  225. package/src/hosted/comments.js +355 -0
  226. package/src/hosted/console-routes.js +401 -0
  227. package/src/hosted/cookies.js +18 -0
  228. package/src/hosted/crypto.js +80 -0
  229. package/src/hosted/db.js +496 -0
  230. package/src/hosted/device-routes.js +175 -0
  231. package/src/hosted/device.js +233 -0
  232. package/src/hosted/events.js +63 -0
  233. package/src/hosted/guards.js +172 -0
  234. package/src/hosted/guest-view.js +65 -0
  235. package/src/hosted/http.js +39 -0
  236. package/src/hosted/import.js +300 -0
  237. package/src/hosted/mail.js +73 -0
  238. package/src/hosted/mcp-http.js +71 -0
  239. package/src/hosted/membership.js +114 -0
  240. package/src/hosted/operator.js +478 -0
  241. package/src/hosted/ops-cli.js +345 -0
  242. package/src/hosted/pages.js +146 -0
  243. package/src/hosted/permissions.js +113 -0
  244. package/src/hosted/presence.js +372 -0
  245. package/src/hosted/projects.js +124 -0
  246. package/src/hosted/public.js +109 -0
  247. package/src/hosted/push.js +622 -0
  248. package/src/hosted/quotas.js +115 -0
  249. package/src/hosted/ratelimit.js +96 -0
  250. package/src/hosted/rebind.js +316 -0
  251. package/src/hosted/retention.js +128 -0
  252. package/src/hosted/server.js +260 -0
  253. package/src/hosted/setup-link.js +64 -0
  254. package/src/hosted/setup.js +323 -0
  255. package/src/hosted/shares.js +444 -0
  256. package/src/hosted/tailscale.js +204 -0
  257. package/src/hosted/tenancy.js +301 -0
  258. package/src/hosted/token-api.js +455 -0
  259. package/src/hosted/token-events.js +60 -0
  260. package/src/hosted/tokens.js +57 -0
  261. package/src/hosted/turnstile.js +49 -0
  262. package/src/hosted/usercontent.js +154 -0
  263. package/src/hosted/zip.js +155 -0
  264. package/src/invocation.js +58 -0
  265. package/src/journey-paths.js +282 -0
  266. package/src/journeys.js +189 -0
  267. package/src/links.js +106 -0
  268. package/src/local-projects.js +182 -0
  269. package/src/lock.js +193 -0
  270. package/src/mcp.js +417 -0
  271. package/src/mentions.js +151 -0
  272. package/src/net.js +15 -0
  273. package/src/notify/config.js +275 -0
  274. package/src/notify/index.js +301 -0
  275. package/src/notify/lease.js +73 -0
  276. package/src/notify/messages.js +212 -0
  277. package/src/notify/run.js +174 -0
  278. package/src/pixeldiff.js +49 -0
  279. package/src/png.js +143 -0
  280. package/src/reactions.js +111 -0
  281. package/src/remote/cli.js +133 -0
  282. package/src/remote/credentials.js +62 -0
  283. package/src/remote/ids.js +12 -0
  284. package/src/remote/login.js +155 -0
  285. package/src/remote/remote-mcp.js +237 -0
  286. package/src/remote/remote-watch.js +291 -0
  287. package/src/remote/remote.js +313 -0
  288. package/src/remote/share-state.js +78 -0
  289. package/src/remote/share.js +369 -0
  290. package/src/remote/terminal.js +13 -0
  291. package/src/serve-state.js +64 -0
  292. package/src/server/app.js +824 -0
  293. package/src/server/devices.js +186 -0
  294. package/src/server/local-console.js +28 -0
  295. package/src/server/local-user.js +59 -0
  296. package/src/server/pairing-routes.js +159 -0
  297. package/src/server/share-routes.js +89 -0
  298. package/src/server/tailscale.js +97 -0
  299. package/src/sitemap.js +431 -0
  300. package/src/store.js +983 -0
  301. package/src/tokens.js +11 -0
  302. package/src/update/routes.js +245 -0
  303. package/src/watch.js +403 -0
  304. package/src/which.js +58 -0
@@ -0,0 +1,114 @@
1
+ // The starter agent's turn loop, inside the container: ask the model, run
2
+ // the tools it calls, repeat until it answers without a tool call or a
3
+ // budget runs out.
4
+ //
5
+ // Budgets per comment, checked before every model call:
6
+ // - turns: model calls;
7
+ // - tokens: as the endpoint reports them (stream_options.include_usage),
8
+ // or estimated at 4 characters a token when it does not; each answer's
9
+ // max_tokens is capped at what is left;
10
+ // - wall time: a deadline, also enforced from the host (it stops the
11
+ // container), so a model or a command that hangs cannot outlast it.
12
+
13
+ export const TOOL_CALLS_PER_TURN_MAX = 8
14
+ const DEFAULT_ANSWER_TOKENS = 4096
15
+ const TEXT_MAX = 20000
16
+
17
+ const ARG_SHOWN_MAX = 160
18
+
19
+ // A tool call's arguments for the run log: names and short values (long
20
+ // text such as a file's content is cut, with its length).
21
+ export function argsSummary(args) {
22
+ if (!args || typeof args !== 'object' || Array.isArray(args)) return null
23
+ const out = {}
24
+ for (const [key, value] of Object.entries(args).slice(0, 12)) {
25
+ if (typeof value === 'string')
26
+ out[key] = value.length > ARG_SHOWN_MAX ? `${value.slice(0, ARG_SHOWN_MAX)}... (${value.length} characters)` : value
27
+ else if (['number', 'boolean'].includes(typeof value) || value === null) out[key] = value
28
+ else out[key] = '[...]'
29
+ }
30
+ return out
31
+ }
32
+
33
+ const estimate = (value) => Math.ceil((typeof value === 'string' ? value.length : JSON.stringify(value).length) / 4)
34
+
35
+ // runLoop({ backend, settings, system, task, tools, budget, deadline, emit, signal })
36
+ // -> { stop: 'answered' | 'turns' | 'tokens' | 'time' | 'stopped' | 'error', text, usage, error? }
37
+ export async function runLoop({
38
+ backend,
39
+ settings,
40
+ system,
41
+ task,
42
+ tools,
43
+ budget,
44
+ deadline,
45
+ emit = () => {},
46
+ signal = null,
47
+ now = () => Date.now(),
48
+ }) {
49
+ const messages = [{ role: 'user', content: [{ type: 'text', text: task }] }]
50
+ const usage = { turns: 0, tokens: 0, estimated: false }
51
+ let text = ''
52
+ const over = () => {
53
+ if (signal?.aborted) return 'stopped'
54
+ if (now() >= deadline) return 'time'
55
+ if (usage.turns >= budget.turns) return 'turns'
56
+ if (usage.tokens >= budget.tokens) return 'tokens'
57
+ return null
58
+ }
59
+ for (;;) {
60
+ const stop = over()
61
+ if (stop) return { stop, text: text.trim(), usage }
62
+ usage.turns += 1
63
+ emit({ type: 'turn', turn: usage.turns })
64
+ // What is left of the token budget caps this answer.
65
+ settings.maxTokens = Math.max(1, Math.min(settings.answerTokens || DEFAULT_ANSWER_TOKENS, budget.tokens - usage.tokens))
66
+ let turnText = ''
67
+ let reported = null
68
+ const calls = []
69
+ try {
70
+ for await (const event of backend.stream({ system, messages, tools: tools.specs, signal })) {
71
+ if (signal?.aborted) break
72
+ if (event.type === 'text') {
73
+ turnText += event.text
74
+ emit({ type: 'text', text: event.text })
75
+ } else if (event.type === 'usage') {
76
+ reported = (reported || 0) + event.total
77
+ } else if (event.type === 'tool_call') {
78
+ calls.push(event)
79
+ }
80
+ }
81
+ } catch (error) {
82
+ if (signal?.aborted) return { stop: 'stopped', text: text.trim(), usage }
83
+ return {
84
+ stop: 'error',
85
+ text: text.trim(),
86
+ usage,
87
+ error: { code: error.code || 'agent_error', message: String(error.message).slice(0, 300) },
88
+ }
89
+ }
90
+ if (reported === null) {
91
+ usage.estimated = true
92
+ usage.tokens += estimate(system) + estimate(messages) + estimate(turnText) + estimate(calls)
93
+ } else usage.tokens += reported
94
+ emit({ type: 'usage', tokens: usage.tokens, estimated: usage.estimated })
95
+ if (signal?.aborted) return { stop: 'stopped', text: text.trim(), usage }
96
+ if (turnText) text = `${text}${text ? '\n\n' : ''}${turnText}`.slice(-TEXT_MAX)
97
+ if (!calls.length) return { stop: 'answered', text: text.trim(), usage }
98
+ const taken = calls.slice(0, TOOL_CALLS_PER_TURN_MAX).map((call, index) => ({ ...call, id: call.id || `call_${usage.turns}_${index}` }))
99
+ messages.push({ role: 'assistant', content: [{ type: 'text', text: turnText }], toolCalls: taken })
100
+ for (const call of taken) {
101
+ const id = call.id
102
+ emit({ type: 'tool', id, name: call.name, state: 'running', args: argsSummary(call.args) })
103
+ const result = await tools.run(call)
104
+ emit({ type: 'tool', id, name: call.name, label: result.label, state: result.ok ? 'done' : 'error', detail: result.detail })
105
+ messages.push({ role: 'tool', toolCallId: id, content: [{ type: 'text', text: result.text }] })
106
+ }
107
+ if (calls.length > taken.length) {
108
+ messages.push({
109
+ role: 'user',
110
+ content: [{ type: 'text', text: `[Shapewall] Only the first ${TOOL_CALLS_PER_TURN_MAX} tool calls of a turn run.` }],
111
+ })
112
+ }
113
+ }
114
+ }
@@ -0,0 +1,289 @@
1
+ // The host side of the agent's model calls (rpc.js method "model").
2
+ //
3
+ // The agent in the container never holds the API key and never talks to the
4
+ // model endpoint itself. It sends the chat completions body here; the host
5
+ // sends it to the configured endpoint only, with the key, and streams the
6
+ // answer back. That makes the host the authority on the budget:
7
+ //
8
+ // - turns: one per model call; a call past the budget is refused;
9
+ // - tokens: counted from the usage the endpoint reports in the stream (or
10
+ // estimated at 4 characters a token when it does not), and each call's
11
+ // max_tokens is capped at what is left; a call past the budget is refused;
12
+ // - the model name, the endpoint and the key are the host's: the request's
13
+ // own `model` is replaced, and only messages, tools, stream_options and
14
+ // max_tokens are taken from it.
15
+ //
16
+ // Anything the endpoint answers is passed back with the key removed.
17
+
18
+ import { agentKey } from '../config.js'
19
+ import { redact } from '../backends/openai.js'
20
+ import { pause } from './pause.js'
21
+
22
+ // Time to the response headers of one model call. Until the model first
23
+ // answers in a run it may still be loading (a local model on a cold start
24
+ // takes minutes), so the first wait is longer. settings.modelTimeoutMs
25
+ // (starterAgent.modelTimeoutSeconds, --model-timeout) sets both.
26
+ export const FIRST_CALL_TIMEOUT_MS = 180000
27
+ export const CALL_TIMEOUT_MS = 60000
28
+ // A timeout or a connection failure is tried once more, after this pause
29
+ // (none after a timeout: the wait was long enough).
30
+ const RETRY_PAUSE_MS = 1000
31
+ const ERROR_BODY_MAX = 4000
32
+ const DEFAULT_ANSWER_TOKENS = 4096
33
+ const MESSAGES_MAX = 400
34
+
35
+ const isObject = (value) => Boolean(value) && typeof value === 'object' && !Array.isArray(value)
36
+
37
+ // The usage reported in server-sent event lines (or one JSON body).
38
+ function usageFrom(text) {
39
+ let total = null
40
+ for (const line of text.split('\n')) {
41
+ const data = line.startsWith('data:') ? line.slice(5).trim() : null
42
+ if (!data || data === '[DONE]' || !data.includes('usage')) continue
43
+ try {
44
+ const usage = JSON.parse(data).usage
45
+ // The last report wins: some servers repeat a running total in every chunk.
46
+ if (usage) total = Number(usage.total_tokens) || (Number(usage.prompt_tokens) || 0) + (Number(usage.completion_tokens) || 0)
47
+ } catch {
48
+ /* not JSON */
49
+ }
50
+ }
51
+ if (total === null && text.trim().startsWith('{')) {
52
+ try {
53
+ const usage = JSON.parse(text).usage
54
+ if (usage) total = Number(usage.total_tokens) || 0
55
+ } catch {
56
+ /* not JSON */
57
+ }
58
+ }
59
+ return total
60
+ }
61
+
62
+ // A message as it goes to the model: only the fields chat completions use,
63
+ // and in content only text and inline (data:) images. A remote image URL
64
+ // would have the provider fetch it: a way out of the sandbox.
65
+ const ROLES = new Set(['system', 'user', 'assistant', 'tool'])
66
+ function cleanMessage(message) {
67
+ if (!isObject(message) || !ROLES.has(message.role)) return { role: 'user', content: '' }
68
+ const out = { role: message.role }
69
+ if (typeof message.content === 'string' || message.content === null) out.content = message.content
70
+ else if (Array.isArray(message.content)) {
71
+ out.content = message.content
72
+ .filter((part) => isObject(part))
73
+ .flatMap((part) => {
74
+ if (part.type === 'text' && typeof part.text === 'string') return [{ type: 'text', text: part.text }]
75
+ const url = typeof part.image_url === 'string' ? part.image_url : part.image_url?.url
76
+ if (part.type === 'image_url' && typeof url === 'string' && /^data:image\/(png|jpeg|webp|gif);base64,/i.test(url)) {
77
+ return [{ type: 'image_url', image_url: { url } }]
78
+ }
79
+ return [{ type: 'text', text: '[Shapewall: a part that is not text or an inline image was removed]' }]
80
+ })
81
+ } else out.content = ''
82
+ // Earlier calls of the agent's own function tools, and only those fields.
83
+ if (message.role === 'assistant' && Array.isArray(message.tool_calls)) {
84
+ const calls = message.tool_calls.flatMap((call) =>
85
+ isObject(call) && (call.type === undefined || call.type === 'function') && isObject(call.function)
86
+ ? [
87
+ {
88
+ ...(typeof call.id === 'string' ? { id: call.id.slice(0, 200) } : {}),
89
+ type: 'function',
90
+ function: {
91
+ name: String(call.function.name || '').slice(0, 64),
92
+ arguments: typeof call.function.arguments === 'string' ? call.function.arguments : '{}',
93
+ },
94
+ },
95
+ ]
96
+ : [],
97
+ )
98
+ if (calls.length) out.tool_calls = calls
99
+ }
100
+ if (message.role === 'tool' && typeof message.tool_call_id === 'string') out.tool_call_id = message.tool_call_id
101
+ if (typeof message.name === 'string') out.name = message.name.slice(0, 64)
102
+ return out
103
+ }
104
+
105
+ // A tool as it goes to the model: { type: 'function', function: { name,
106
+ // description, parameters } } and nothing else, or null.
107
+ function cleanTool(tool) {
108
+ if (!isObject(tool) || tool.type !== 'function' || !isObject(tool.function)) return null
109
+ const { name, description, parameters } = tool.function
110
+ if (typeof name !== 'string' || !/^[A-Za-z0-9_-]{1,64}$/.test(name)) return null
111
+ return {
112
+ type: 'function',
113
+ function: {
114
+ name,
115
+ ...(typeof description === 'string' ? { description: description.slice(0, 4000) } : {}),
116
+ ...(isObject(parameters) ? { parameters } : {}),
117
+ },
118
+ }
119
+ }
120
+
121
+ const seconds = (ms) => (ms >= 1000 ? `${Math.round(ms / 1000)} s` : `${ms} ms`)
122
+
123
+ // createModelRelay({ settings, env, budget, send, fetchImpl, deadline }) ->
124
+ // { handle(callMessage), cancel(id), stopAll(), usage, stop }
125
+ // deadline: the run's end (ms since the epoch); no retry and no wait past it.
126
+ export function createModelRelay({
127
+ settings,
128
+ env = process.env,
129
+ budget,
130
+ send,
131
+ fetchImpl = globalThis.fetch,
132
+ log = () => {},
133
+ deadline = null,
134
+ now = () => Date.now(),
135
+ retryPauseMs = RETRY_PAUSE_MS,
136
+ }) {
137
+ const usage = { turns: 0, tokens: 0, estimated: false }
138
+ const running = new Map()
139
+ const state = { stop: null, answered: false }
140
+ const timeoutMs = () => settings.modelTimeoutMs || (state.answered ? CALL_TIMEOUT_MS : FIRST_CALL_TIMEOUT_MS)
141
+ const timeLeft = () => (deadline === null ? Infinity : deadline - now())
142
+
143
+ // One request to the endpoint, at most twice: a timeout or a connection
144
+ // failure (no answer at all) is tried once more, within the same turn.
145
+ // -> { response } | { error: 'cancelled' | 'could not reach it: ...' }
146
+ async function connect(url, init, call, key) {
147
+ let reason = null
148
+ for (let attempt = 1; attempt <= 2; attempt += 1) {
149
+ const limit = Math.min(timeoutMs(), timeLeft())
150
+ if (limit <= 0) break
151
+ const attemptController = new AbortController()
152
+ const onCancel = () => attemptController.abort('cancelled')
153
+ call.signal.addEventListener('abort', onCancel, { once: true })
154
+ const timer = setTimeout(() => attemptController.abort('timeout'), limit)
155
+ try {
156
+ // On success the cancel link stays: cancelling the call stops reading its body.
157
+ const response = await fetchImpl(url, { ...init, signal: attemptController.signal })
158
+ state.answered = true
159
+ return { response }
160
+ } catch (error) {
161
+ call.signal.removeEventListener('abort', onCancel)
162
+ if (call.signal.aborted) return { error: 'cancelled' }
163
+ reason = attemptController.signal.aborted ? `timed out after ${seconds(limit)}` : redact(error.cause?.code || error.message, key)
164
+ } finally {
165
+ clearTimeout(timer)
166
+ }
167
+ if (attempt === 1) {
168
+ log(`no answer (${reason}); trying once more`)
169
+ await pause(reason.startsWith('timed out') ? 0 : Math.min(retryPauseMs, Math.max(0, timeLeft())), call.signal)
170
+ if (call.signal.aborted) return { error: 'cancelled' }
171
+ }
172
+ }
173
+ log(`no answer (${reason || 'the time budget ran out'})`)
174
+ return { error: `could not reach it: ${reason || 'the time budget ran out'}` }
175
+ }
176
+
177
+ const refuse = (id, status, message) => {
178
+ send({ t: 'head', id, status, contentType: 'application/json' })
179
+ send({ t: 'chunk', id, data: JSON.stringify({ error: { message } }) })
180
+ send({ t: 'end', id })
181
+ }
182
+
183
+ async function handle(message) {
184
+ const id = message.id
185
+ const body = message.params?.body
186
+ if (!isObject(body) || !Array.isArray(body.messages) || body.messages.length > MESSAGES_MAX) {
187
+ return refuse(id, 400, 'Shapewall: a chat completions body with messages is needed')
188
+ }
189
+ if (usage.turns >= budget.turns) {
190
+ state.stop = 'turns'
191
+ return refuse(id, 429, 'Shapewall: the turn budget for this comment is used up')
192
+ }
193
+ if (usage.tokens >= budget.tokens) {
194
+ state.stop = 'tokens'
195
+ return refuse(id, 429, 'Shapewall: the token budget for this comment is used up')
196
+ }
197
+ // One call at a time: concurrent calls would each see the whole budget left.
198
+ if (running.size) return refuse(id, 429, 'Shapewall: one model call at a time')
199
+ const messages = body.messages.map(cleanMessage)
200
+ // Function tools only (the agent's own, run in the sandbox): a provider-side
201
+ // tool (web_search, browser, code_interpreter, ...) would act outside it.
202
+ const functions = Array.isArray(body.tools) ? body.tools.map(cleanTool).filter(Boolean) : []
203
+ const tools = functions.length ? functions : null
204
+ // The prompt's size is reserved up front, with the answer's max_tokens.
205
+ const prompt = Math.ceil(JSON.stringify([messages, tools]).length / 4)
206
+ const left = budget.tokens - usage.tokens - prompt
207
+ if (left < 1) {
208
+ state.stop = 'tokens'
209
+ return refuse(id, 429, 'Shapewall: the token budget for this comment is used up')
210
+ }
211
+ usage.turns += 1
212
+ const asked =
213
+ Number.isInteger(body.max_tokens) && body.max_tokens > 0 ? body.max_tokens : settings.model.maxTokens || DEFAULT_ANSWER_TOKENS
214
+ const out = { model: settings.model.model, messages, stream: true, max_tokens: Math.min(asked, left) }
215
+ if (tools) out.tools = tools
216
+ if (body.stream_options?.include_usage === true) out.stream_options = { include_usage: true }
217
+ if (settings.model.temperature !== null && settings.model.temperature !== undefined) out.temperature = settings.model.temperature
218
+ const key = settings.model.keyVar ? agentKey({ keyVar: settings.model.keyVar }, env) : null
219
+ const headers = { 'Content-Type': 'application/json', Accept: 'text/event-stream' }
220
+ if (key) headers.Authorization = `Bearer ${key}`
221
+ const requestBody = JSON.stringify(out)
222
+ const controller = new AbortController()
223
+ if (running.size) return refuse(id, 429, 'Shapewall: one model call at a time')
224
+ running.set(id, controller)
225
+ const connected = await connect(
226
+ `${settings.model.baseUrl}/chat/completions`,
227
+ { method: 'POST', headers, body: requestBody, redirect: 'error' },
228
+ controller,
229
+ key,
230
+ )
231
+ if (!connected.response) {
232
+ running.delete(id)
233
+ // Read once as it is: the container passes the host's words on unwrapped.
234
+ send({ t: 'end', id, error: connected.error })
235
+ return
236
+ }
237
+ const response = connected.response
238
+ let seen = ''
239
+ // The end goes out last, after the call is off the books: the agent may
240
+ // send its next call the moment it reads it.
241
+ let end = { t: 'end', id }
242
+ try {
243
+ const contentType = String(response.headers.get('content-type') || 'application/json')
244
+ if (!response.ok) {
245
+ const text = redact((await response.text().catch(() => '')).slice(0, ERROR_BODY_MAX), key)
246
+ send({ t: 'head', id, status: response.status, contentType })
247
+ send({ t: 'chunk', id, data: text })
248
+ log(`model answered ${response.status}`)
249
+ return
250
+ }
251
+ send({ t: 'head', id, status: response.status, contentType })
252
+ const decoder = new TextDecoder()
253
+ for await (const chunk of response.body) {
254
+ const text = decoder.decode(chunk, { stream: true })
255
+ // Kept for the usage count: the tail is enough for the last event.
256
+ seen = (seen + text).slice(-200000)
257
+ send({ t: 'chunk', id, data: redact(text, key) })
258
+ }
259
+ } catch (error) {
260
+ end = { t: 'end', id, error: controller.signal.aborted ? 'cancelled' : `the answer broke off (${redact(error.message, key)})` }
261
+ } finally {
262
+ running.delete(id)
263
+ const reported = usageFrom(seen)
264
+ if (reported === null) {
265
+ usage.estimated = true
266
+ usage.tokens += Math.ceil((requestBody.length + seen.length) / 4)
267
+ } else usage.tokens += reported
268
+ send(end)
269
+ }
270
+ }
271
+
272
+ return {
273
+ usage,
274
+ get stop() {
275
+ return state.stop
276
+ },
277
+ // The wait for the next call's headers (the first call's is longer).
278
+ get timeoutMs() {
279
+ return timeoutMs()
280
+ },
281
+ handle,
282
+ cancel(id) {
283
+ running.get(id)?.abort('cancelled')
284
+ },
285
+ stopAll() {
286
+ for (const controller of running.values()) controller.abort('cancelled')
287
+ },
288
+ }
289
+ }
@@ -0,0 +1,14 @@
1
+ // Waits ms, or less when signal aborts; never rejects. Built on
2
+ // timers/promises so the abort listener goes away with the timer (one
3
+ // added per wait in a long poll loop would pile up on the signal).
4
+
5
+ import { setTimeout as wait } from 'node:timers/promises'
6
+
7
+ export async function pause(ms, signal = null) {
8
+ if (signal?.aborted) return
9
+ try {
10
+ await wait(ms, undefined, signal ? { signal } : {})
11
+ } catch (error) {
12
+ if (error?.name !== 'AbortError') throw error
13
+ }
14
+ }