@desplega.ai/agent-swarm 1.131.1 → 1.134.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/{actions-jgcy477k.js → actions-7kast243.js} +20 -18
- package/dist/{anthropic-messages-rjmd2e7n.js → anthropic-messages-706yad38.js} +33 -17
- package/dist/app-0g84t8wf.js +28 -0
- package/dist/{artifact-jysj3wrg.js → artifact-dje5ke4h.js} +37 -131
- package/dist/{assistant-f2vkk1hj.js → assistant-ac4htcky.js} +25 -22
- package/dist/{azure-openai-responses-c0yjmbfd.js → azure-openai-responses-be59fbsz.js} +13 -13
- package/dist/{boot-reembed-q7mp8hcr.js → boot-reembed-b1rcqn85.js} +13 -11
- package/dist/{boot-reembed-xmw8wcx6.js → boot-reembed-kw2541c4.js} +14 -12
- package/dist/{boot-scrub-logs-dsqrv79n.js → boot-scrub-logs-ja29t92g.js} +11 -9
- package/dist/{claude-adapter-q6x7yspx.js → claude-adapter-5cg1fv8s.js} +33 -29
- package/dist/{claude-managed-adapter-wsg37bt8.js → claude-managed-adapter-9z9sdytx.js} +9 -9
- package/dist/{claude-managed-setup-52z4mq9w.js → claude-managed-setup-3zqp51cr.js} +5 -5
- package/dist/{claude-1ex232fc.js → claude-wqn3dtca.js} +1 -1
- package/dist/{cli-4wgngmwv.js → cli-00w4hbam.js} +1 -1
- package/dist/{cli-er3a4bb0.js → cli-15429qdw.js} +6 -1
- package/dist/{cli-e5dabkx0.js → cli-1cwhyvta.js} +4 -4
- package/dist/{cli-zsspvvm2.js → cli-1ybr6vw2.js} +237 -237
- package/dist/{cli-xpxde7rm.js → cli-2wv4kj0e.js} +60 -31
- package/dist/cli-34w2584r.js +530 -0
- package/dist/{cli-se2p6rkb.js → cli-38taxznr.js} +6 -6
- package/dist/{cli-9sbhdpvb.js → cli-3cgfyfzm.js} +14 -14
- package/dist/{cli-5n6e5yfn.js → cli-3zmdzfjf.js} +79 -74
- package/dist/{cli-3ty0t082.js → cli-43x36mty.js} +197 -8
- package/dist/{cli-ycrnynpw.js → cli-480nehnh.js} +29 -22
- package/dist/{cli-0258203w.js → cli-492e7ap5.js} +140 -20
- package/dist/{cli-3fj60etf.js → cli-497b685t.js} +3 -3
- package/dist/{cli-pxfxkx5m.js → cli-4bap00k0.js} +1 -1
- package/dist/{cli-w2wxd56v.js → cli-4bc8vmhm.js} +848 -770
- package/dist/{cli-aca5pz9f.js → cli-589599p9.js} +1 -1
- package/dist/{cli-t930g0xh.js → cli-5tnr64pm.js} +1 -1
- package/dist/{cli-jxg3s9ey.js → cli-65d93ves.js} +7 -7
- package/dist/{cli-6dpytdr2.js → cli-6x363v1z.js} +2 -2
- package/dist/{cli-dxak0fqd.js → cli-787ph852.js} +11 -11
- package/dist/{cli-793jvbsh.js → cli-7bv8nckk.js} +16 -11
- package/dist/{cli-zyxs93y9.js → cli-7jcc9y1r.js} +3 -3
- package/dist/{cli-f1am9szv.js → cli-87t2gstf.js} +1 -1
- package/dist/{cli-9aryf9tv.js → cli-95m51ztz.js} +1 -1
- package/dist/{cli-4w4fmq9x.js → cli-9w49enmh.js} +2 -2
- package/dist/{cli-v69vk5h0.js → cli-9y5b02vr.js} +13 -13
- package/dist/{cli-x23me2mg.js → cli-a1rrkrb4.js} +1 -1
- package/dist/{cli-psrqtw2m.js → cli-a3pkfpqs.js} +2 -2
- package/dist/{cli-h0rgxd24.js → cli-a4ajmfvp.js} +6 -6
- package/dist/{cli-ygrbmjja.js → cli-aw8bb6sf.js} +6 -6
- package/dist/{cli-g74vpjp2.js → cli-brm3et47.js} +55 -22
- package/dist/{cli-czkae1fp.js → cli-c5ddbdfj.js} +9 -9
- package/dist/{cli-114fvtmj.js → cli-c79a9zrc.js} +1 -1
- package/dist/{cli-49n9f49x.js → cli-dx3dz0ms.js} +3 -3
- package/dist/{cli-6dp3ydxa.js → cli-e07exmsb.js} +18 -14
- package/dist/cli-et1bbwqf.js +13 -0
- package/dist/{cli-yazx6hap.js → cli-f3s9dry0.js} +14 -14
- package/dist/{cli-t8d349cy.js → cli-f4wj0s3m.js} +3 -3
- package/dist/{cli-d6n0thxw.js → cli-f64gdfxc.js} +1 -1
- package/dist/{cli-pwfsqc2x.js → cli-fyd58xgb.js} +55 -55
- package/dist/{cli-7dm2zqy2.js → cli-g0j14czv.js} +2 -2
- package/dist/{cli-b0p7rfnd.js → cli-g2p9bd1z.js} +14 -14
- package/dist/{cli-z905tvmm.js → cli-g814jbst.js} +2 -2
- package/dist/{cli-5dsbcjjw.js → cli-ghexzd2d.js} +970 -970
- package/dist/{cli-jvv0g8bh.js → cli-gpt9kf01.js} +15 -15
- package/dist/{cli-rgrfzg8z.js → cli-grt6qv91.js} +380 -380
- package/dist/{cli-wz9115x0.js → cli-gtafvdy0.js} +8 -8
- package/dist/{cli-q5dzzak0.js → cli-gy07xk4s.js} +2 -2
- package/dist/{cli-sy7jk3tx.js → cli-h0djenzm.js} +2 -2
- package/dist/{cli-nrkkknat.js → cli-h37gbjbf.js} +2 -281
- package/dist/{cli-jevgrb4w.js → cli-h39xxnq6.js} +1200 -491
- package/dist/{cli-gz9sbyk3.js → cli-jepmgn7r.js} +1 -1
- package/dist/{cli-ccf9vy7q.js → cli-jmvk6et8.js} +7 -7
- package/dist/{cli-2rk0cx7s.js → cli-jxj92gkt.js} +51 -41
- package/dist/{cli-bqw0mt1n.js → cli-k46t4664.js} +18 -3
- package/dist/{cli-50vbt00w.js → cli-m07313tr.js} +1153 -696
- package/dist/{cli-4j2mrzag.js → cli-mkq1rk7b.js} +1 -1
- package/dist/{cli-1z7048dy.js → cli-mnm7w46e.js} +1 -1
- package/dist/{cli-kd1ck4rx.js → cli-mp9sg4cg.js} +14 -8
- package/dist/{cli-jbcva0xj.js → cli-mrammd8c.js} +60 -35
- package/dist/cli-nanaxrz6.js +27 -0
- package/dist/{cli-p9swy5t3.js → cli-pq2ee5ja.js} +8 -6
- package/dist/{cli-2wadx7p2.js → cli-pt43pdp1.js} +2 -2
- package/dist/{cli-7sgrxg9a.js → cli-q6d0vvey.js} +4 -4
- package/dist/{cli-k9qkf1hw.js → cli-qf9kpsve.js} +9 -9
- package/dist/cli-qh4sqfws.js +15 -0
- package/dist/{cli-e15jhafb.js → cli-qntkxysp.js} +14 -14
- package/dist/{cli-sajgpkqg.js → cli-qs9hhsvb.js} +1723 -325
- package/dist/{cli-z2zcxes1.js → cli-qt5bt3vk.js} +1 -1
- package/dist/{cli-wx82tw1k.js → cli-qtekrty1.js} +3 -3
- package/dist/{cli-g1re8r05.js → cli-rh6f3s9n.js} +3 -3
- package/dist/{cli-n0krnc6d.js → cli-rzhcrw2p.js} +2 -2
- package/dist/{cli-gecy3z2s.js → cli-snw62ze5.js} +4 -4
- package/dist/{cli-n6h8qt8n.js → cli-snz3p4yf.js} +15 -10
- package/dist/{cli-r6kqqbj0.js → cli-tkf8n6cw.js} +11 -11
- package/dist/{cli-4rkfg1wx.js → cli-tkrr24mz.js} +14 -9
- package/dist/{cli-9dr5qzc0.js → cli-tv9f1kyb.js} +54 -43
- package/dist/{cli-gm9cbhbq.js → cli-vtz6yj3v.js} +28 -9
- package/dist/{cli-2f07m0xr.js → cli-wf86d5t7.js} +1 -1
- package/dist/{cli-7zd05xb4.js → cli-whrvct19.js} +45 -45
- package/dist/{cli-ap1czx8f.js → cli-wrrea8xm.js} +3 -3
- package/dist/{cli-57khth04.js → cli-xmyqv00z.js} +120 -4
- package/dist/{cli-cwy5hqbq.js → cli-yjmk7tzc.js} +1 -1
- package/dist/{cli-6pcz9h15.js → cli-zykjrwcx.js} +180 -394
- package/dist/cli.js +45 -45
- package/dist/{codex-adapter-htjkn22b.js → codex-adapter-hsq6mz3m.js} +21 -19
- package/dist/{codex-hook-hq54t9ft.js → codex-hook-8c500gq8.js} +8 -7
- package/dist/{codex-login-z6yhbkmk.js → codex-login-x7603481.js} +3 -3
- package/dist/{codex-session-runner-wjfc7smz.js → codex-session-runner-c1x3x143.js} +15 -13
- package/dist/{commands-xhrfwqsk.js → commands-rwtddtst.js} +11 -9
- package/dist/{db-anvwb22f.js → db-j2b355vz.js} +458 -444
- package/dist/{devin-adapter-v1sarqtw.js → devin-adapter-8qmhqpy3.js} +3 -3
- package/dist/{devtools-2y1r7yqk.js → devtools-j2rnj8w7.js} +4 -4
- package/dist/{e2b-jdkwemv2.js → e2b-znpx78eg.js} +16 -16
- package/dist/{es5-vjgtkgp6.js → es5-fmqrjzy3.js} +2 -2
- package/dist/{event-bus-axn823e9.js → event-bus-n8sb35pw.js} +4 -4
- package/dist/{getMachineId-bsd-6tc9wxdj.js → getMachineId-bsd-wpzbhkm6.js} +4 -4
- package/dist/{getMachineId-darwin-sjt9sgnc.js → getMachineId-darwin-45zabjy0.js} +4 -4
- package/dist/{getMachineId-linux-v5n18y9p.js → getMachineId-linux-warnp5pd.js} +3 -3
- package/dist/{getMachineId-unsupported-yrjyqw3d.js → getMachineId-unsupported-5spent2a.js} +3 -3
- package/dist/{getMachineId-win-w3r8063c.js → getMachineId-win-v4bvcw7a.js} +4 -4
- package/dist/{google-generative-ai-hysrd8qx.js → google-generative-ai-b0wzss55.js} +17 -14
- package/dist/{google-vertex-p5arrga0.js → google-vertex-e8q4dpya.js} +18 -15
- package/dist/{handlers-b9kbc621.js → handlers-hchgrxkh.js} +32 -29
- package/dist/{hook-xkvn6c5h.js → hook-p49egqc0.js} +66 -52
- package/dist/hook-spb00w76.js +34 -0
- package/dist/{http-9xh6ta4r.js → http-nh14kkms.js} +663 -181
- package/dist/index-1qthq7vc.js +6 -0
- package/dist/{index-0g68f0p6.js → index-20xqmn3v.js} +3 -3
- package/dist/{index-vdkr5z2w.js → index-3e03k6e4.js} +114 -114
- package/dist/{index-ywds4812.js → index-4cbyhq2g.js} +43 -41
- package/dist/index-67pzqfrb.js +36 -0
- package/dist/{index-bccrkdkn.js → index-7m8yxkw5.js} +16 -16
- package/dist/{index-eaxyqcmj.js → index-dvsk64sm.js} +163 -83
- package/dist/{index-dqkpnmqm.js → index-e19s2m8k.js} +18 -18
- package/dist/{index-j30k5zxn.js → index-emvxcy98.js} +16 -16
- package/dist/{index-7ndw6ecy.js → index-gqnmr60d.js} +9 -9
- package/dist/{index-9znhar3x.js → index-j02xvjtd.js} +184 -185
- package/dist/{index-1s2442yw.js → index-ns5kksc1.js} +7 -7
- package/dist/{index-dpy2hth8.js → index-pdxc8kfh.js} +71 -71
- package/dist/{index-z1m7pw0v.js → index-ptcqm8bc.js} +107 -107
- package/dist/{index-070v4eyb.js → index-pyp7rexm.js} +5 -6
- package/dist/{index-86znjz1p.js → index-shne41sd.js} +13 -13
- package/dist/{index-2qn2qndv.js → index-t36q2v4t.js} +183 -183
- package/dist/index-tcnvv8w6.js +7 -0
- package/dist/index-w91qbfv1.js +7 -0
- package/dist/{index-fzc0mwvj.js → index-yqweff5n.js} +32 -32
- package/dist/index-yrfphye1.js +39 -0
- package/dist/{keepalive-vc7s2aa6.js → keepalive-9k51nqhc.js} +21 -15
- package/dist/lead-2xq3dxbz.js +88 -0
- package/dist/maintenance-0c0g6nym.js +28 -0
- package/dist/mistral-conversations-2njwdwhj.js +717 -0
- package/dist/{multipart-parser-qaktc7p8.js → multipart-parser-6gftesw0.js} +2 -2
- package/dist/{oauth-refresh-sweep-yecgjpk4.js → oauth-refresh-sweep-273a64r2.js} +14 -12
- package/dist/{onboard-xj0f73f9.js → onboard-tsv9wwhz.js} +12 -12
- package/dist/{openai-codex-responses-5g27pd6f.js → openai-codex-responses-kt84eq86.js} +26 -26
- package/dist/{openai-completions-3vkmfz2a.js → openai-completions-mrypq7qp.js} +17 -16
- package/dist/{openai-responses-dg4t7j7a.js → openai-responses-qhrk21ez.js} +20 -17
- package/dist/{opencode-adapter-1gm38aba.js → opencode-adapter-rar9676j.js} +13 -11
- package/dist/{openrouter-images-g3gm8931.js → openrouter-images-684fysx9.js} +5 -5
- package/dist/{otel-impl-nxg0n0b3.js → otel-impl-wp1mfzd2.js} +752 -537
- package/dist/{photon_rs-ky7dj5qw.js → photon_rs-8dwt2pzy.js} +581 -581
- package/dist/{pi-messages-tqj8q6b9.js → pi-messages-txp9f97r.js} +7 -7
- package/dist/{pi-mono-adapter-4g5kby31.js → pi-mono-adapter-yz47kk8e.js} +1910 -1180
- package/dist/pricing-refresh-jw1yw2fn.js +22 -0
- package/dist/rbac-roles-dg964c8p.js +32 -0
- package/dist/{rbac-roles-1hcxdtg7.js → rbac-roles-p53cg5cv.js} +19 -17
- package/dist/{registry-02cy9z5z.js → registry-mh12y17v.js} +4 -4
- package/dist/render-v2-56vn2rk9.js +36 -0
- package/dist/seed-pricing-52xx6z63.js +19 -0
- package/dist/{setup-46xtgq3c.js → setup-e8gp79sy.js} +7 -7
- package/dist/token-util-zy908s1a.js +6 -0
- package/dist/{token-fxkn2zf2.js → token-xg5711x5.js} +4 -4
- package/dist/worker-s17f1g46.js +88 -0
- package/dist/{x-6mg4kfpe.js → x-xkfh0w0v.js} +5 -5
- package/openapi.json +566 -22
- package/package.json +6 -6
- package/src/be/budget-refusal-notify.ts +14 -7
- package/src/be/db-queries/task-artifacts.ts +182 -0
- package/src/be/db.ts +834 -171
- package/src/be/events.ts +5 -0
- package/src/be/migrations/131_session_logs_created_at_index.sql +2 -0
- package/src/be/migrations/132_multi_runtime_instances.sql +44 -0
- package/src/be/migrations/133_workflow_run_trigger_type.sql +12 -0
- package/src/be/migrations/135_backfill_task_pull_request_attachments.sql +228 -0
- package/src/be/migrations/136_task_requester_provenance.sql +37 -0
- package/src/be/migrations/137_memory_retrieval_composite_index.sql +1 -0
- package/src/be/migrations/runner.ts +17 -11
- package/src/be/multi-runtime.ts +430 -0
- package/src/be/scripts/extract-schema.ts +3 -1
- package/src/be/scripts/typecheck.ts +6 -2
- package/src/be/seed-prompt-templates.ts +25 -2
- package/src/be/seed-skills/index.ts +7 -0
- package/src/be/steering.ts +1 -1
- package/src/be/swarm-config-guard.ts +18 -0
- package/src/commands/credential-wait.ts +54 -0
- package/src/commands/onboard/steps/core-credentials.tsx +1 -1
- package/src/commands/onboard/steps/custom-templates.tsx +1 -1
- package/src/commands/onboard/steps/harness-credentials.tsx +1 -1
- package/src/commands/onboard/steps/health-check.tsx +1 -1
- package/src/commands/onboard/steps/start.tsx +1 -1
- package/src/commands/profile-sync-audit.ts +56 -0
- package/src/commands/profile-sync.ts +287 -15
- package/src/commands/provider-credentials.ts +46 -19
- package/src/commands/runner.ts +199 -33
- package/src/commands/templates.ts +22 -0
- package/src/github/handlers.ts +55 -9
- package/src/heartbeat/heartbeat.ts +109 -17
- package/src/hooks/hook.ts +49 -33
- package/src/http/active-sessions.ts +11 -1
- package/src/http/agents.ts +261 -30
- package/src/http/all-routes.ts +1 -0
- package/src/http/config.ts +13 -2
- package/src/http/core.ts +102 -4
- package/src/http/db-query-bounded.ts +313 -0
- package/src/http/db-query-shared.ts +249 -0
- package/src/http/db-query.ts +54 -42
- package/src/http/events.ts +2 -0
- package/src/http/index.ts +23 -2
- package/src/http/mcp-bridge.ts +37 -3
- package/src/http/mcp.ts +12 -0
- package/src/http/memory.ts +13 -2
- package/src/http/openapi.ts +1 -0
- package/src/http/poll.ts +70 -27
- package/src/http/prompt-templates.ts +36 -13
- package/src/http/route-def.ts +43 -9
- package/src/http/schedules.ts +2 -2
- package/src/http/scripts.ts +10 -1
- package/src/http/session-data.ts +44 -0
- package/src/http/tasks.ts +2 -2
- package/src/http/users.ts +74 -52
- package/src/http/webhooks.ts +5 -2
- package/src/http/workflows.ts +1 -4
- package/src/integrations/kapso/inbound.ts +1 -1
- package/src/jira/outbound.ts +39 -8
- package/src/jira/webhook-lifecycle.ts +13 -2
- package/src/linear/outbound.ts +43 -10
- package/src/oauth/keepalive.ts +13 -2
- package/src/prompts/base-prompt.ts +1 -1
- package/src/prompts/default-drift.ts +21 -0
- package/src/prompts/session-templates.ts +56 -7
- package/src/providers/claude-adapter.ts +6 -0
- package/src/providers/codex-adapter.ts +6 -0
- package/src/providers/opencode-adapter.ts +6 -0
- package/src/providers/pi-mono-adapter.ts +8 -0
- package/src/providers/pricing-sources.md +24 -0
- package/src/queue-stall-alarm.ts +216 -0
- package/src/rbac/legacy-policy.ts +3 -0
- package/src/rbac/permissions.ts +12 -0
- package/src/scheduler/scheduler.ts +7 -0
- package/src/script-workflows/executor.ts +2 -1
- package/src/scripts-runtime/executors/native.ts +2 -1
- package/src/scripts-runtime/executors/types.ts +5 -0
- package/src/scripts-runtime/loader.ts +5 -0
- package/src/scripts-runtime/sdk-allowlist.ts +3 -0
- package/src/scripts-runtime/swarm-config.ts +8 -0
- package/src/scripts-runtime/swarm-sdk.ts +7 -0
- package/src/scripts-runtime/types/stdlib.d.ts +4 -0
- package/src/scripts-runtime/types/swarm-sdk.d.ts +4 -0
- package/src/server.ts +6 -0
- package/src/slack/channel-join.ts +22 -3
- package/src/slack/channel-lifecycle.ts +114 -0
- package/src/tests/budget-claim-gate.test.ts +20 -0
- package/src/tests/claude-adapter.test.ts +32 -0
- package/src/tests/codex-adapter.test.ts +30 -0
- package/src/tests/create-task-validation.test.ts +128 -0
- package/src/tests/credential-wait.test.ts +67 -1
- package/src/tests/db-query.test.ts +711 -2
- package/src/tests/heartbeat-checklist.test.ts +28 -0
- package/src/tests/http-api-integration.test.ts +81 -0
- package/src/tests/http-users.test.ts +166 -1
- package/src/tests/identity-field-budget.test.ts +176 -0
- package/src/tests/mcp-tools-user.test.ts +34 -0
- package/src/tests/mcp-user-route.test.ts +17 -0
- package/src/tests/metrics-http.test.ts +44 -0
- package/src/tests/migration-failure.test.ts +86 -0
- package/src/tests/multi-runtime-registration.test.ts +2810 -0
- package/src/tests/openapi-response-contract.test.ts +24 -0
- package/src/tests/openapi-route-headers.test.ts +83 -0
- package/src/tests/opencode-adapter.test.ts +40 -0
- package/src/tests/outbound-listener-rejection.test.ts +138 -0
- package/src/tests/pi-mono-adapter.test.ts +98 -0
- package/src/tests/preload.ts +2 -0
- package/src/tests/profile-sync-warning.test.ts +277 -0
- package/src/tests/profile-sync.test.ts +232 -6
- package/src/tests/prompt-template-resolver.test.ts +177 -6
- package/src/tests/prompt-template-session.test.ts +39 -3
- package/src/tests/queue-stall-alarm.test.ts +226 -0
- package/src/tests/rbac-charact-slack.test.ts +29 -0
- package/src/tests/rbac-engine.test.ts +3 -0
- package/src/tests/requester-comms.test.ts +32 -0
- package/src/tests/runner-requester-profile.test.ts +38 -0
- package/src/tests/schedule-target-type.test.ts +38 -0
- package/src/tests/script-connections.test.ts +13 -1
- package/src/tests/scripts-runtime-identity.test.ts +375 -0
- package/src/tests/scripts-runtime.test.ts +4 -3
- package/src/tests/sdk-allowlist.test.ts +47 -0
- package/src/tests/send-task-requested-by.test.ts +9 -0
- package/src/tests/session-costs.test.ts +506 -1
- package/src/tests/shutdown-error-scrubbing.test.ts +92 -0
- package/src/tests/slack-channel-lifecycle.test.ts +241 -0
- package/src/tests/slack-render-v2.test.ts +43 -0
- package/src/tests/steering-core.test.ts +28 -0
- package/src/tests/swarm-config-reserved-keys.test.ts +22 -0
- package/src/tests/task-artifacts.test.ts +386 -0
- package/src/tests/task-attachments-schema.test.ts +17 -1
- package/src/tests/task-completion-idempotency.test.ts +57 -0
- package/src/tests/task-pull-request-backfill-migration.test.ts +321 -0
- package/src/tests/tool-annotations.test.ts +4 -0
- package/src/tests/update-profile-api.test.ts +67 -39
- package/src/tests/update-profile-auth.test.ts +48 -4
- package/src/tests/user-attribution-migration.test.ts +54 -0
- package/src/tests/user-identity.test.ts +72 -0
- package/src/tests/workflow-async-v2.test.ts +39 -1
- package/src/tests/workflow-http-v2.test.ts +3 -2
- package/src/tests/workflow-mcp-trigger-schema.test.ts +52 -3
- package/src/tests/workflow-retry-v2.test.ts +45 -1
- package/src/tools/db-query.ts +18 -8
- package/src/tools/get-task-details.ts +24 -3
- package/src/tools/manage-user.ts +36 -3
- package/src/tools/poll-task.ts +93 -14
- package/src/tools/script-common.ts +5 -0
- package/src/tools/send-task.ts +1 -4
- package/src/tools/slack-archive-channel.ts +70 -0
- package/src/tools/slack-create-channel.ts +74 -0
- package/src/tools/slack-invite-to-channel.ts +78 -0
- package/src/tools/store-progress.ts +13 -2
- package/src/tools/swarm-config/delete-config.ts +9 -0
- package/src/tools/swarm-config/set-config.ts +3 -2
- package/src/tools/task-action.ts +30 -0
- package/src/tools/task-tool-ctx.ts +8 -1
- package/src/tools/tool-config.ts +4 -1
- package/src/tools/update-profile.ts +16 -21
- package/src/tools/utils.ts +5 -0
- package/src/tools/workflows/trigger-workflow.ts +7 -1
- package/src/types.ts +165 -1
- package/src/utils/github-pull-request.ts +76 -0
- package/src/utils/identity-field-budget.ts +73 -0
- package/src/utils/multi-runtime.ts +20 -0
- package/src/utils/requester-comms.ts +24 -0
- package/src/workflows/engine.ts +22 -2
- package/src/workflows/resume.ts +19 -11
- package/src/workflows/retry-poller.ts +1 -0
- package/src/workflows/triggers.ts +4 -0
- package/templates/skills/db-query-guidance/config.json +14 -0
- package/templates/skills/db-query-guidance/content.md +57 -0
- package/dist/app-5da78bd4.js +0 -26
- package/dist/cli-3te4jdkm.js +0 -225
- package/dist/cli-bmze986d.js +0 -7
- package/dist/cli-kh3ba2zf.js +0 -607
- package/dist/hook-cgk4wxmx.js +0 -31
- package/dist/index-25wjsdv2.js +0 -34
- package/dist/index-76at2ymp.js +0 -7
- package/dist/index-hase6zbe.js +0 -6
- package/dist/index-vg4snay4.js +0 -36
- package/dist/index-vy8edt27.js +0 -7
- package/dist/lead-r2yqfmx7.js +0 -85
- package/dist/maintenance-mw70k468.js +0 -26
- package/dist/mistral-conversations-81fwav0j.js +0 -30578
- package/dist/otel-nqa12wf3.js +0 -3149
- package/dist/pricing-refresh-2cn6effr.js +0 -20
- package/dist/rbac-roles-vynday5g.js +0 -30
- package/dist/render-v2-tc0bh8kk.js +0 -34
- package/dist/seed-pricing-8kceb43f.js +0 -17
- package/dist/token-util-pqyc8kng.js +0 -6
- package/dist/worker-qrbz6hmv.js +0 -85
|
@@ -0,0 +1,717 @@
|
|
|
1
|
+
import {
|
|
2
|
+
shortHash
|
|
3
|
+
} from "./cli-5m9j2spm.js";
|
|
4
|
+
import {
|
|
5
|
+
parseStreamingJson
|
|
6
|
+
} from "./cli-7jcc9y1r.js";
|
|
7
|
+
import {
|
|
8
|
+
buildBaseOptions,
|
|
9
|
+
getJsonSchemaToolParameters,
|
|
10
|
+
resolveJsonSchemaStrictSampling,
|
|
11
|
+
transformMessages
|
|
12
|
+
} from "./cli-xmyqv00z.js";
|
|
13
|
+
import {
|
|
14
|
+
sanitizeSurrogates
|
|
15
|
+
} from "./cli-y2apkfe2.js";
|
|
16
|
+
import {
|
|
17
|
+
headersToRecord
|
|
18
|
+
} from "./cli-vapyk8sw.js";
|
|
19
|
+
import {
|
|
20
|
+
calculateCost,
|
|
21
|
+
clampThinkingLevel
|
|
22
|
+
} from "./cli-akdvwc9v.js";
|
|
23
|
+
import {
|
|
24
|
+
AssistantMessageEventStream
|
|
25
|
+
} from "./cli-8wrympd7.js";
|
|
26
|
+
import"./cli-pq2ee5ja.js";
|
|
27
|
+
|
|
28
|
+
// node_modules/@earendil-works/pi-ai/dist/api/mistral-conversations.js
|
|
29
|
+
var MISTRAL_TOOL_CALL_ID_LENGTH = 9;
|
|
30
|
+
var MAX_MISTRAL_ERROR_BODY_CHARS = 4000;
|
|
31
|
+
var stream = (model, context, options) => {
|
|
32
|
+
const stream2 = new AssistantMessageEventStream;
|
|
33
|
+
(async () => {
|
|
34
|
+
const output = createOutput(model);
|
|
35
|
+
try {
|
|
36
|
+
const apiKey = options?.apiKey;
|
|
37
|
+
if (!apiKey) {
|
|
38
|
+
throw new Error(`No API key for provider: ${model.provider}`);
|
|
39
|
+
}
|
|
40
|
+
const normalizeMistralToolCallId = createMistralToolCallIdNormalizer();
|
|
41
|
+
const transformedMessages = transformMessages(context.messages, model, (id) => normalizeMistralToolCallId(id));
|
|
42
|
+
let payload = buildChatPayload(model, context, transformedMessages, options);
|
|
43
|
+
const nextPayload = await options?.onPayload?.(payload, model);
|
|
44
|
+
if (nextPayload !== undefined) {
|
|
45
|
+
payload = nextPayload;
|
|
46
|
+
}
|
|
47
|
+
const mistralStream = await requestMistralStream(model, payload, apiKey, options);
|
|
48
|
+
stream2.push({ type: "start", partial: output });
|
|
49
|
+
await consumeChatStream(model, output, stream2, mistralStream);
|
|
50
|
+
if (options?.signal?.aborted) {
|
|
51
|
+
throw new Error("Request was aborted");
|
|
52
|
+
}
|
|
53
|
+
if (output.stopReason === "pending") {
|
|
54
|
+
throw new Error("Mistral stream ended without a finish reason");
|
|
55
|
+
}
|
|
56
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") {
|
|
57
|
+
throw new Error(output.errorMessage || "An unknown error occurred");
|
|
58
|
+
}
|
|
59
|
+
stream2.push({ type: "done", reason: output.stopReason, message: output });
|
|
60
|
+
stream2.end();
|
|
61
|
+
} catch (error) {
|
|
62
|
+
for (const block of output.content) {
|
|
63
|
+
delete block.partialArgs;
|
|
64
|
+
}
|
|
65
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
66
|
+
output.errorMessage = formatMistralError(error);
|
|
67
|
+
stream2.push({ type: "error", reason: output.stopReason, error: output });
|
|
68
|
+
stream2.end();
|
|
69
|
+
}
|
|
70
|
+
})();
|
|
71
|
+
return stream2;
|
|
72
|
+
};
|
|
73
|
+
var streamSimple = (model, context, options) => {
|
|
74
|
+
const apiKey = options?.apiKey;
|
|
75
|
+
if (!apiKey) {
|
|
76
|
+
throw new Error(`No API key for provider: ${model.provider}`);
|
|
77
|
+
}
|
|
78
|
+
const base = buildBaseOptions(model, context, options, apiKey);
|
|
79
|
+
const clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : undefined;
|
|
80
|
+
const reasoning = clampedReasoning === "off" ? undefined : clampedReasoning;
|
|
81
|
+
const shouldUseReasoning = model.reasoning && reasoning !== undefined;
|
|
82
|
+
return stream(model, context, {
|
|
83
|
+
...base,
|
|
84
|
+
promptMode: shouldUseReasoning && usesPromptModeReasoning(model) ? "reasoning" : undefined,
|
|
85
|
+
reasoningEffort: shouldUseReasoning && usesReasoningEffort(model) ? mapReasoningEffort(model, reasoning) : undefined
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
function createOutput(model) {
|
|
89
|
+
return {
|
|
90
|
+
role: "assistant",
|
|
91
|
+
content: [],
|
|
92
|
+
api: model.api,
|
|
93
|
+
provider: model.provider,
|
|
94
|
+
model: model.id,
|
|
95
|
+
usage: {
|
|
96
|
+
input: 0,
|
|
97
|
+
output: 0,
|
|
98
|
+
cacheRead: 0,
|
|
99
|
+
cacheWrite: 0,
|
|
100
|
+
totalTokens: 0,
|
|
101
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
|
|
102
|
+
},
|
|
103
|
+
stopReason: "pending",
|
|
104
|
+
timestamp: Date.now()
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function createMistralToolCallIdNormalizer() {
|
|
108
|
+
const idMap = new Map;
|
|
109
|
+
const reverseMap = new Map;
|
|
110
|
+
return (id) => {
|
|
111
|
+
const existing = idMap.get(id);
|
|
112
|
+
if (existing)
|
|
113
|
+
return existing;
|
|
114
|
+
let attempt = 0;
|
|
115
|
+
while (true) {
|
|
116
|
+
const candidate = deriveMistralToolCallId(id, attempt);
|
|
117
|
+
const owner = reverseMap.get(candidate);
|
|
118
|
+
if (!owner || owner === id) {
|
|
119
|
+
idMap.set(id, candidate);
|
|
120
|
+
reverseMap.set(candidate, id);
|
|
121
|
+
return candidate;
|
|
122
|
+
}
|
|
123
|
+
attempt++;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function deriveMistralToolCallId(id, attempt) {
|
|
128
|
+
const normalized = id.replace(/[^a-zA-Z0-9]/g, "");
|
|
129
|
+
if (attempt === 0 && normalized.length === MISTRAL_TOOL_CALL_ID_LENGTH)
|
|
130
|
+
return normalized;
|
|
131
|
+
const seedBase = normalized || id;
|
|
132
|
+
const seed = attempt === 0 ? seedBase : `${seedBase}:${attempt}`;
|
|
133
|
+
return shortHash(seed).replace(/[^a-zA-Z0-9]/g, "").slice(0, MISTRAL_TOOL_CALL_ID_LENGTH);
|
|
134
|
+
}
|
|
135
|
+
function formatMistralError(error) {
|
|
136
|
+
if (error instanceof Error) {
|
|
137
|
+
const httpError = error;
|
|
138
|
+
const statusCode = typeof httpError.statusCode === "number" ? httpError.statusCode : undefined;
|
|
139
|
+
const bodyText = typeof httpError.body === "string" ? httpError.body.trim() : undefined;
|
|
140
|
+
if (statusCode !== undefined && bodyText) {
|
|
141
|
+
return `Mistral API error (${statusCode}): ${truncateErrorText(bodyText, MAX_MISTRAL_ERROR_BODY_CHARS)}`;
|
|
142
|
+
}
|
|
143
|
+
if (statusCode !== undefined)
|
|
144
|
+
return `Mistral API error (${statusCode}): ${error.message}`;
|
|
145
|
+
return error.message;
|
|
146
|
+
}
|
|
147
|
+
return safeJsonStringify(error);
|
|
148
|
+
}
|
|
149
|
+
function truncateErrorText(text, maxChars) {
|
|
150
|
+
if (text.length <= maxChars)
|
|
151
|
+
return text;
|
|
152
|
+
return `${text.slice(0, maxChars)}... [truncated ${text.length - maxChars} chars]`;
|
|
153
|
+
}
|
|
154
|
+
function safeJsonStringify(value) {
|
|
155
|
+
try {
|
|
156
|
+
const serialized = JSON.stringify(value);
|
|
157
|
+
return serialized === undefined ? String(value) : serialized;
|
|
158
|
+
} catch {
|
|
159
|
+
return String(value);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
async function requestMistralStream(model, payload, apiKey, options) {
|
|
163
|
+
const baseUrl = new URL(model.baseUrl);
|
|
164
|
+
baseUrl.pathname = `${baseUrl.pathname.replace(/\/+$/u, "")}/`;
|
|
165
|
+
const url = new URL("v1/chat/completions", baseUrl);
|
|
166
|
+
const headers = buildMistralHeaders(model, apiKey, options);
|
|
167
|
+
const timeoutSignal = AbortSignal.timeout(options?.timeoutMs ?? 60000);
|
|
168
|
+
const signal = options?.signal ? AbortSignal.any([options.signal, timeoutSignal]) : timeoutSignal;
|
|
169
|
+
const response = await (options?.fetch ?? globalThis.fetch)(url, {
|
|
170
|
+
method: "POST",
|
|
171
|
+
headers,
|
|
172
|
+
body: JSON.stringify(toMistralWirePayload(payload)),
|
|
173
|
+
signal
|
|
174
|
+
});
|
|
175
|
+
await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
|
|
176
|
+
if (!response.ok) {
|
|
177
|
+
const body = await response.text();
|
|
178
|
+
throw new MistralHttpError(response.status, body, response.statusText);
|
|
179
|
+
}
|
|
180
|
+
if (!response.body) {
|
|
181
|
+
throw new Error("Mistral response has no body");
|
|
182
|
+
}
|
|
183
|
+
return readMistralEvents(response.body, signal);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
class MistralHttpError extends Error {
|
|
187
|
+
statusCode;
|
|
188
|
+
body;
|
|
189
|
+
constructor(statusCode, body, statusText) {
|
|
190
|
+
super(statusText || `Request failed with status ${statusCode}`);
|
|
191
|
+
this.name = "MistralHttpError";
|
|
192
|
+
this.statusCode = statusCode;
|
|
193
|
+
this.body = body;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function buildMistralHeaders(model, apiKey, options) {
|
|
197
|
+
const headers = new Headers({
|
|
198
|
+
accept: "text/event-stream",
|
|
199
|
+
authorization: `Bearer ${apiKey}`,
|
|
200
|
+
"content-type": "application/json"
|
|
201
|
+
});
|
|
202
|
+
applyMistralHeaderOverrides(headers, model.headers);
|
|
203
|
+
applyMistralHeaderOverrides(headers, options?.headers);
|
|
204
|
+
const hasExplicitAffinity = hasMistralHeaderOverride(model.headers, "x-affinity") || hasMistralHeaderOverride(options?.headers, "x-affinity");
|
|
205
|
+
if (shouldUsePromptCaching(options) && !hasExplicitAffinity) {
|
|
206
|
+
headers.set("x-affinity", options.sessionId);
|
|
207
|
+
}
|
|
208
|
+
return headers;
|
|
209
|
+
}
|
|
210
|
+
function applyMistralHeaderOverrides(headers, overrides) {
|
|
211
|
+
if (!overrides)
|
|
212
|
+
return;
|
|
213
|
+
for (const [name, value] of Object.entries(overrides)) {
|
|
214
|
+
if (value === null)
|
|
215
|
+
headers.delete(name);
|
|
216
|
+
else
|
|
217
|
+
headers.set(name, value);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
function hasMistralHeaderOverride(overrides, target) {
|
|
221
|
+
return !!overrides && Object.keys(overrides).some((name) => name.toLowerCase() === target);
|
|
222
|
+
}
|
|
223
|
+
function toMistralWirePayload(payload) {
|
|
224
|
+
const wirePayload = { ...payload };
|
|
225
|
+
for (const [source, target] of [
|
|
226
|
+
["topP", "top_p"],
|
|
227
|
+
["maxTokens", "max_tokens"],
|
|
228
|
+
["randomSeed", "random_seed"],
|
|
229
|
+
["responseFormat", "response_format"],
|
|
230
|
+
["toolChoice", "tool_choice"],
|
|
231
|
+
["presencePenalty", "presence_penalty"],
|
|
232
|
+
["frequencyPenalty", "frequency_penalty"],
|
|
233
|
+
["parallelToolCalls", "parallel_tool_calls"],
|
|
234
|
+
["reasoningEffort", "reasoning_effort"],
|
|
235
|
+
["promptMode", "prompt_mode"],
|
|
236
|
+
["promptCacheKey", "prompt_cache_key"],
|
|
237
|
+
["safePrompt", "safe_prompt"]
|
|
238
|
+
]) {
|
|
239
|
+
remapMistralProperty(wirePayload, source, target);
|
|
240
|
+
}
|
|
241
|
+
wirePayload.messages = payload.messages.map((message) => toMistralWireMessage(message));
|
|
242
|
+
const responseFormat = wirePayload.response_format;
|
|
243
|
+
if (isMistralRecord(responseFormat)) {
|
|
244
|
+
const wireResponseFormat = { ...responseFormat };
|
|
245
|
+
remapMistralProperty(wireResponseFormat, "jsonSchema", "json_schema");
|
|
246
|
+
const jsonSchema = wireResponseFormat.json_schema;
|
|
247
|
+
if (isMistralRecord(jsonSchema)) {
|
|
248
|
+
const wireJsonSchema = { ...jsonSchema };
|
|
249
|
+
remapMistralProperty(wireJsonSchema, "schemaDefinition", "schema");
|
|
250
|
+
wireResponseFormat.json_schema = wireJsonSchema;
|
|
251
|
+
}
|
|
252
|
+
wirePayload.response_format = wireResponseFormat;
|
|
253
|
+
}
|
|
254
|
+
return wirePayload;
|
|
255
|
+
}
|
|
256
|
+
function toMistralWireMessage(message) {
|
|
257
|
+
const wireMessage = { ...message };
|
|
258
|
+
remapMistralProperty(wireMessage, "toolCalls", "tool_calls");
|
|
259
|
+
remapMistralProperty(wireMessage, "toolCallId", "tool_call_id");
|
|
260
|
+
if (Array.isArray(message.content)) {
|
|
261
|
+
wireMessage.content = message.content.map((chunk) => toMistralWireContentChunk(chunk));
|
|
262
|
+
}
|
|
263
|
+
return wireMessage;
|
|
264
|
+
}
|
|
265
|
+
function toMistralWireContentChunk(chunk) {
|
|
266
|
+
const wireChunk = { ...chunk };
|
|
267
|
+
for (const [source, target] of [
|
|
268
|
+
["imageUrl", "image_url"],
|
|
269
|
+
["documentUrl", "document_url"],
|
|
270
|
+
["documentName", "document_name"],
|
|
271
|
+
["fileId", "file_id"],
|
|
272
|
+
["referenceIds", "reference_ids"],
|
|
273
|
+
["inputAudio", "input_audio"]
|
|
274
|
+
]) {
|
|
275
|
+
remapMistralProperty(wireChunk, source, target);
|
|
276
|
+
}
|
|
277
|
+
return wireChunk;
|
|
278
|
+
}
|
|
279
|
+
function remapMistralProperty(record, source, target) {
|
|
280
|
+
if (!(source in record))
|
|
281
|
+
return;
|
|
282
|
+
record[target] = record[source];
|
|
283
|
+
delete record[source];
|
|
284
|
+
}
|
|
285
|
+
function isMistralRecord(value) {
|
|
286
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
287
|
+
}
|
|
288
|
+
var MISTRAL_STREAM_DONE = Symbol("mistral-stream-done");
|
|
289
|
+
async function* readMistralEvents(body, signal) {
|
|
290
|
+
const reader = body.getReader();
|
|
291
|
+
const decoder = new TextDecoder;
|
|
292
|
+
let buffer = "";
|
|
293
|
+
const onAbort = () => {
|
|
294
|
+
reader.cancel().catch(() => {});
|
|
295
|
+
};
|
|
296
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
297
|
+
try {
|
|
298
|
+
while (true) {
|
|
299
|
+
if (signal.aborted)
|
|
300
|
+
throw signal.reason;
|
|
301
|
+
const { done, value } = await reader.read();
|
|
302
|
+
if (signal.aborted)
|
|
303
|
+
throw signal.reason;
|
|
304
|
+
buffer += done ? decoder.decode() : decoder.decode(value, { stream: true });
|
|
305
|
+
let boundary = findMistralEventBoundary(buffer);
|
|
306
|
+
while (boundary) {
|
|
307
|
+
const event = parseMistralEvent(buffer.slice(0, boundary.index));
|
|
308
|
+
buffer = buffer.slice(boundary.index + boundary.length);
|
|
309
|
+
if (event === MISTRAL_STREAM_DONE)
|
|
310
|
+
return;
|
|
311
|
+
if (event)
|
|
312
|
+
yield event;
|
|
313
|
+
boundary = findMistralEventBoundary(buffer);
|
|
314
|
+
}
|
|
315
|
+
if (done)
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
if (buffer.trim()) {
|
|
319
|
+
const event = parseMistralEvent(buffer);
|
|
320
|
+
if (event !== MISTRAL_STREAM_DONE && event)
|
|
321
|
+
yield event;
|
|
322
|
+
}
|
|
323
|
+
} finally {
|
|
324
|
+
signal.removeEventListener("abort", onAbort);
|
|
325
|
+
try {
|
|
326
|
+
await reader.cancel();
|
|
327
|
+
} catch {}
|
|
328
|
+
try {
|
|
329
|
+
reader.releaseLock();
|
|
330
|
+
} catch {}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
function findMistralEventBoundary(buffer) {
|
|
334
|
+
const match = /\r\n\r\n|\r\n\r|\r\n\n|\r\r\n|\n\r\n|\r\r|\n\r|\n\n/u.exec(buffer);
|
|
335
|
+
return match?.index === undefined ? undefined : { index: match.index, length: match[0].length };
|
|
336
|
+
}
|
|
337
|
+
function parseMistralEvent(raw) {
|
|
338
|
+
const data = raw.split(/\r\n|\r|\n/u).filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trimStart()).join(`
|
|
339
|
+
`).trim();
|
|
340
|
+
if (!data)
|
|
341
|
+
return;
|
|
342
|
+
if (data === "[DONE]")
|
|
343
|
+
return MISTRAL_STREAM_DONE;
|
|
344
|
+
const parsed = JSON.parse(data);
|
|
345
|
+
if (!isMistralRecord(parsed) || !Array.isArray(parsed.choices)) {
|
|
346
|
+
throw new Error("Invalid Mistral streaming event");
|
|
347
|
+
}
|
|
348
|
+
return { data: parsed };
|
|
349
|
+
}
|
|
350
|
+
function buildChatPayload(model, context, messages, options) {
|
|
351
|
+
const payload = {
|
|
352
|
+
model: model.id,
|
|
353
|
+
stream: true,
|
|
354
|
+
messages: toChatMessages(messages, model.input.includes("image"))
|
|
355
|
+
};
|
|
356
|
+
if (context.tools?.length)
|
|
357
|
+
payload.tools = toFunctionTools(context.tools);
|
|
358
|
+
if (options?.temperature !== undefined)
|
|
359
|
+
payload.temperature = options.temperature;
|
|
360
|
+
if (options?.maxTokens !== undefined)
|
|
361
|
+
payload.maxTokens = options.maxTokens;
|
|
362
|
+
if (options?.toolChoice)
|
|
363
|
+
payload.toolChoice = mapToolChoice(options.toolChoice);
|
|
364
|
+
if (options?.promptMode)
|
|
365
|
+
payload.promptMode = options.promptMode;
|
|
366
|
+
if (options?.reasoningEffort)
|
|
367
|
+
payload.reasoningEffort = options.reasoningEffort;
|
|
368
|
+
if (shouldUsePromptCaching(options))
|
|
369
|
+
payload.promptCacheKey = options.sessionId;
|
|
370
|
+
if (context.systemPrompt) {
|
|
371
|
+
payload.messages.unshift({
|
|
372
|
+
role: "system",
|
|
373
|
+
content: sanitizeSurrogates(context.systemPrompt)
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
return payload;
|
|
377
|
+
}
|
|
378
|
+
function shouldUsePromptCaching(options) {
|
|
379
|
+
return options?.cacheRetention !== "none" && !!options?.sessionId;
|
|
380
|
+
}
|
|
381
|
+
function getMistralCachedPromptTokens(usage, promptTokens) {
|
|
382
|
+
const rawUsage = usage;
|
|
383
|
+
const rawCachedTokens = rawUsage.promptTokensDetails?.cachedTokens ?? rawUsage.prompt_tokens_details?.cached_tokens ?? rawUsage.promptTokenDetails?.cachedTokens ?? rawUsage.prompt_token_details?.cached_tokens ?? rawUsage.numCachedTokens ?? rawUsage.num_cached_tokens ?? 0;
|
|
384
|
+
const cachedTokens = typeof rawCachedTokens === "number" && Number.isFinite(rawCachedTokens) ? rawCachedTokens : 0;
|
|
385
|
+
return Math.min(promptTokens, Math.max(0, cachedTokens));
|
|
386
|
+
}
|
|
387
|
+
async function consumeChatStream(model, output, stream2, mistralStream) {
|
|
388
|
+
let currentBlock = null;
|
|
389
|
+
const blocks = output.content;
|
|
390
|
+
const blockIndex = () => blocks.length - 1;
|
|
391
|
+
const toolBlocksByKey = new Map;
|
|
392
|
+
const finishCurrentBlock = (block) => {
|
|
393
|
+
if (!block)
|
|
394
|
+
return;
|
|
395
|
+
if (block.type === "text") {
|
|
396
|
+
stream2.push({
|
|
397
|
+
type: "text_end",
|
|
398
|
+
contentIndex: blockIndex(),
|
|
399
|
+
content: block.text,
|
|
400
|
+
partial: output
|
|
401
|
+
});
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
if (block.type === "thinking") {
|
|
405
|
+
stream2.push({
|
|
406
|
+
type: "thinking_end",
|
|
407
|
+
contentIndex: blockIndex(),
|
|
408
|
+
content: block.thinking,
|
|
409
|
+
partial: output
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
for await (const event of mistralStream) {
|
|
414
|
+
const chunk = event.data;
|
|
415
|
+
output.responseId ||= chunk.id;
|
|
416
|
+
if (chunk.usage) {
|
|
417
|
+
const promptTokens = chunk.usage.prompt_tokens || 0;
|
|
418
|
+
const cachedPromptTokens = getMistralCachedPromptTokens(chunk.usage, promptTokens);
|
|
419
|
+
output.usage.input = Math.max(0, promptTokens - cachedPromptTokens);
|
|
420
|
+
output.usage.output = chunk.usage.completion_tokens || 0;
|
|
421
|
+
output.usage.cacheRead = cachedPromptTokens;
|
|
422
|
+
output.usage.cacheWrite = 0;
|
|
423
|
+
output.usage.totalTokens = chunk.usage.total_tokens || output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
424
|
+
calculateCost(model, output.usage);
|
|
425
|
+
}
|
|
426
|
+
const choice = chunk.choices[0];
|
|
427
|
+
if (!choice)
|
|
428
|
+
continue;
|
|
429
|
+
if (choice.finish_reason) {
|
|
430
|
+
output.rawStopReason = choice.finish_reason;
|
|
431
|
+
const stopReasonResult = mapChatStopReason(choice.finish_reason);
|
|
432
|
+
output.stopReason = stopReasonResult.stopReason;
|
|
433
|
+
if (stopReasonResult.errorMessage) {
|
|
434
|
+
output.errorMessage = stopReasonResult.errorMessage;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
const delta = choice.delta;
|
|
438
|
+
if (delta.content !== null && delta.content !== undefined) {
|
|
439
|
+
const contentItems = typeof delta.content === "string" ? [delta.content] : delta.content;
|
|
440
|
+
for (const item of contentItems) {
|
|
441
|
+
if (typeof item === "string") {
|
|
442
|
+
const textDelta = sanitizeSurrogates(item);
|
|
443
|
+
if (!currentBlock || currentBlock.type !== "text") {
|
|
444
|
+
finishCurrentBlock(currentBlock);
|
|
445
|
+
currentBlock = { type: "text", text: "" };
|
|
446
|
+
output.content.push(currentBlock);
|
|
447
|
+
stream2.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
|
|
448
|
+
}
|
|
449
|
+
currentBlock.text += textDelta;
|
|
450
|
+
stream2.push({
|
|
451
|
+
type: "text_delta",
|
|
452
|
+
contentIndex: blockIndex(),
|
|
453
|
+
delta: textDelta,
|
|
454
|
+
partial: output
|
|
455
|
+
});
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
if (item.type === "thinking") {
|
|
459
|
+
const deltaText = (item.thinking ?? []).map((part) => part.text ?? "").filter((text) => text.length > 0).join("");
|
|
460
|
+
const thinkingDelta = sanitizeSurrogates(deltaText);
|
|
461
|
+
if (!thinkingDelta)
|
|
462
|
+
continue;
|
|
463
|
+
if (!currentBlock || currentBlock.type !== "thinking") {
|
|
464
|
+
finishCurrentBlock(currentBlock);
|
|
465
|
+
currentBlock = { type: "thinking", thinking: "" };
|
|
466
|
+
output.content.push(currentBlock);
|
|
467
|
+
stream2.push({ type: "thinking_start", contentIndex: blockIndex(), partial: output });
|
|
468
|
+
}
|
|
469
|
+
currentBlock.thinking += thinkingDelta;
|
|
470
|
+
stream2.push({
|
|
471
|
+
type: "thinking_delta",
|
|
472
|
+
contentIndex: blockIndex(),
|
|
473
|
+
delta: thinkingDelta,
|
|
474
|
+
partial: output
|
|
475
|
+
});
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
if (item.type === "text") {
|
|
479
|
+
const textDelta = sanitizeSurrogates(item.text ?? "");
|
|
480
|
+
if (!currentBlock || currentBlock.type !== "text") {
|
|
481
|
+
finishCurrentBlock(currentBlock);
|
|
482
|
+
currentBlock = { type: "text", text: "" };
|
|
483
|
+
output.content.push(currentBlock);
|
|
484
|
+
stream2.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
|
|
485
|
+
}
|
|
486
|
+
currentBlock.text += textDelta;
|
|
487
|
+
stream2.push({
|
|
488
|
+
type: "text_delta",
|
|
489
|
+
contentIndex: blockIndex(),
|
|
490
|
+
delta: textDelta,
|
|
491
|
+
partial: output
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
const toolCalls = delta.tool_calls || [];
|
|
497
|
+
for (const toolCall of toolCalls) {
|
|
498
|
+
if (currentBlock) {
|
|
499
|
+
finishCurrentBlock(currentBlock);
|
|
500
|
+
currentBlock = null;
|
|
501
|
+
}
|
|
502
|
+
const callId = toolCall.id && toolCall.id !== "null" ? toolCall.id : deriveMistralToolCallId(`toolcall:${toolCall.index ?? 0}`, 0);
|
|
503
|
+
const key = `${callId}:${toolCall.index || 0}`;
|
|
504
|
+
const existingIndex = toolBlocksByKey.get(key);
|
|
505
|
+
let block;
|
|
506
|
+
if (existingIndex !== undefined) {
|
|
507
|
+
const existing = output.content[existingIndex];
|
|
508
|
+
if (existing?.type === "toolCall") {
|
|
509
|
+
block = existing;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
if (!block) {
|
|
513
|
+
block = {
|
|
514
|
+
type: "toolCall",
|
|
515
|
+
id: callId,
|
|
516
|
+
name: toolCall.function.name,
|
|
517
|
+
arguments: {},
|
|
518
|
+
partialArgs: ""
|
|
519
|
+
};
|
|
520
|
+
output.content.push(block);
|
|
521
|
+
toolBlocksByKey.set(key, output.content.length - 1);
|
|
522
|
+
stream2.push({ type: "toolcall_start", contentIndex: output.content.length - 1, partial: output });
|
|
523
|
+
}
|
|
524
|
+
const argsDelta = typeof toolCall.function.arguments === "string" ? toolCall.function.arguments : JSON.stringify(toolCall.function.arguments || {});
|
|
525
|
+
block.partialArgs = (block.partialArgs || "") + argsDelta;
|
|
526
|
+
block.arguments = parseStreamingJson(block.partialArgs);
|
|
527
|
+
stream2.push({
|
|
528
|
+
type: "toolcall_delta",
|
|
529
|
+
contentIndex: toolBlocksByKey.get(key),
|
|
530
|
+
delta: argsDelta,
|
|
531
|
+
partial: output
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
finishCurrentBlock(currentBlock);
|
|
536
|
+
for (const index of toolBlocksByKey.values()) {
|
|
537
|
+
const block = output.content[index];
|
|
538
|
+
if (block.type !== "toolCall")
|
|
539
|
+
continue;
|
|
540
|
+
const toolBlock = block;
|
|
541
|
+
toolBlock.arguments = parseStreamingJson(toolBlock.partialArgs);
|
|
542
|
+
delete toolBlock.partialArgs;
|
|
543
|
+
stream2.push({
|
|
544
|
+
type: "toolcall_end",
|
|
545
|
+
contentIndex: index,
|
|
546
|
+
toolCall: toolBlock,
|
|
547
|
+
partial: output
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
function toFunctionTools(tools) {
|
|
552
|
+
return tools.map((tool) => {
|
|
553
|
+
const strict = resolveJsonSchemaStrictSampling(tool, true);
|
|
554
|
+
return {
|
|
555
|
+
type: "function",
|
|
556
|
+
function: {
|
|
557
|
+
name: tool.name,
|
|
558
|
+
description: tool.description,
|
|
559
|
+
parameters: stripSymbolKeys(getJsonSchemaToolParameters(tool, strict)),
|
|
560
|
+
strict: strict ?? false
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
function stripSymbolKeys(value) {
|
|
566
|
+
if (Array.isArray(value)) {
|
|
567
|
+
return value.map((item) => stripSymbolKeys(item));
|
|
568
|
+
}
|
|
569
|
+
if (value && typeof value === "object") {
|
|
570
|
+
const result = {};
|
|
571
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
572
|
+
result[key] = stripSymbolKeys(entry);
|
|
573
|
+
}
|
|
574
|
+
return result;
|
|
575
|
+
}
|
|
576
|
+
return value;
|
|
577
|
+
}
|
|
578
|
+
function toChatMessages(messages, supportsImages) {
|
|
579
|
+
const result = [];
|
|
580
|
+
for (const msg of messages) {
|
|
581
|
+
if (msg.role === "user") {
|
|
582
|
+
if (typeof msg.content === "string") {
|
|
583
|
+
result.push({ role: "user", content: sanitizeSurrogates(msg.content) });
|
|
584
|
+
continue;
|
|
585
|
+
}
|
|
586
|
+
const hadImages = msg.content.some((item) => item.type === "image");
|
|
587
|
+
const content = msg.content.filter((item) => item.type === "text" || supportsImages).map((item) => {
|
|
588
|
+
if (item.type === "text")
|
|
589
|
+
return { type: "text", text: sanitizeSurrogates(item.text) };
|
|
590
|
+
return { type: "image_url", imageUrl: `data:${item.mimeType};base64,${item.data}` };
|
|
591
|
+
});
|
|
592
|
+
if (content.length > 0) {
|
|
593
|
+
result.push({ role: "user", content });
|
|
594
|
+
continue;
|
|
595
|
+
}
|
|
596
|
+
if (hadImages && !supportsImages) {
|
|
597
|
+
result.push({ role: "user", content: "(image omitted: model does not support images)" });
|
|
598
|
+
}
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
if (msg.role === "assistant") {
|
|
602
|
+
const contentParts = [];
|
|
603
|
+
const toolCalls = [];
|
|
604
|
+
for (const block of msg.content) {
|
|
605
|
+
if (block.type === "text") {
|
|
606
|
+
if (block.text.trim().length > 0) {
|
|
607
|
+
contentParts.push({ type: "text", text: sanitizeSurrogates(block.text) });
|
|
608
|
+
}
|
|
609
|
+
continue;
|
|
610
|
+
}
|
|
611
|
+
if (block.type === "thinking") {
|
|
612
|
+
if (block.thinking.trim().length > 0) {
|
|
613
|
+
contentParts.push({
|
|
614
|
+
type: "thinking",
|
|
615
|
+
thinking: [{ type: "text", text: sanitizeSurrogates(block.thinking) }]
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
continue;
|
|
619
|
+
}
|
|
620
|
+
toolCalls.push({
|
|
621
|
+
id: block.id,
|
|
622
|
+
type: "function",
|
|
623
|
+
function: { name: block.name, arguments: JSON.stringify(block.arguments || {}) },
|
|
624
|
+
index: 0
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
const assistantMessage = { role: "assistant", prefix: false };
|
|
628
|
+
if (contentParts.length > 0)
|
|
629
|
+
assistantMessage.content = contentParts;
|
|
630
|
+
if (toolCalls.length > 0)
|
|
631
|
+
assistantMessage.toolCalls = toolCalls;
|
|
632
|
+
if (contentParts.length > 0 || toolCalls.length > 0)
|
|
633
|
+
result.push(assistantMessage);
|
|
634
|
+
continue;
|
|
635
|
+
}
|
|
636
|
+
const toolContent = [];
|
|
637
|
+
const textResult = msg.content.filter((part) => part.type === "text").map((part) => part.type === "text" ? sanitizeSurrogates(part.text) : "").join(`
|
|
638
|
+
`);
|
|
639
|
+
const hasImages = msg.content.some((part) => part.type === "image");
|
|
640
|
+
const toolText = buildToolResultText(textResult, hasImages, supportsImages, msg.isError);
|
|
641
|
+
toolContent.push({ type: "text", text: toolText });
|
|
642
|
+
for (const part of msg.content) {
|
|
643
|
+
if (!supportsImages)
|
|
644
|
+
continue;
|
|
645
|
+
if (part.type !== "image")
|
|
646
|
+
continue;
|
|
647
|
+
toolContent.push({
|
|
648
|
+
type: "image_url",
|
|
649
|
+
imageUrl: `data:${part.mimeType};base64,${part.data}`
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
result.push({
|
|
653
|
+
role: "tool",
|
|
654
|
+
toolCallId: msg.toolCallId,
|
|
655
|
+
name: msg.toolName,
|
|
656
|
+
content: toolContent
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
return result;
|
|
660
|
+
}
|
|
661
|
+
function buildToolResultText(text, hasImages, supportsImages, isError) {
|
|
662
|
+
const trimmed = text.trim();
|
|
663
|
+
const errorPrefix = isError ? "[tool error] " : "";
|
|
664
|
+
if (trimmed.length > 0) {
|
|
665
|
+
const imageSuffix = hasImages && !supportsImages ? `
|
|
666
|
+
[tool image omitted: model does not support images]` : "";
|
|
667
|
+
return `${errorPrefix}${trimmed}${imageSuffix}`;
|
|
668
|
+
}
|
|
669
|
+
if (hasImages) {
|
|
670
|
+
if (supportsImages) {
|
|
671
|
+
return isError ? "[tool error] (see attached image)" : "(see attached image)";
|
|
672
|
+
}
|
|
673
|
+
return isError ? "[tool error] (image omitted: model does not support images)" : "(image omitted: model does not support images)";
|
|
674
|
+
}
|
|
675
|
+
return isError ? "[tool error] (no tool output)" : "(no tool output)";
|
|
676
|
+
}
|
|
677
|
+
function usesReasoningEffort(model) {
|
|
678
|
+
return model.id === "mistral-small-2603" || model.id === "mistral-small-latest" || model.id === "mistral-medium-3.5";
|
|
679
|
+
}
|
|
680
|
+
function usesPromptModeReasoning(model) {
|
|
681
|
+
return model.reasoning && !usesReasoningEffort(model);
|
|
682
|
+
}
|
|
683
|
+
function mapReasoningEffort(model, level) {
|
|
684
|
+
return model.thinkingLevelMap?.[level] ?? "high";
|
|
685
|
+
}
|
|
686
|
+
function mapToolChoice(choice) {
|
|
687
|
+
if (!choice)
|
|
688
|
+
return;
|
|
689
|
+
if (choice === "auto" || choice === "none" || choice === "any" || choice === "required") {
|
|
690
|
+
return choice;
|
|
691
|
+
}
|
|
692
|
+
return {
|
|
693
|
+
type: "function",
|
|
694
|
+
function: { name: choice.function.name }
|
|
695
|
+
};
|
|
696
|
+
}
|
|
697
|
+
function mapChatStopReason(reason) {
|
|
698
|
+
if (reason === null)
|
|
699
|
+
return { stopReason: "stop" };
|
|
700
|
+
switch (reason) {
|
|
701
|
+
case "stop":
|
|
702
|
+
return { stopReason: "stop" };
|
|
703
|
+
case "length":
|
|
704
|
+
case "model_length":
|
|
705
|
+
return { stopReason: "length" };
|
|
706
|
+
case "tool_calls":
|
|
707
|
+
return { stopReason: "toolUse" };
|
|
708
|
+
case "error":
|
|
709
|
+
return { stopReason: "error", errorMessage: "Provider stopped with: error" };
|
|
710
|
+
default:
|
|
711
|
+
return { stopReason: "error", errorMessage: `Provider stopped with: ${reason}` };
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
export {
|
|
715
|
+
stream,
|
|
716
|
+
streamSimple
|
|
717
|
+
};
|