@parcha/agentrun-serve 0.1.0-beta.32 → 0.1.0-beta.34
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/CHANGELOG.md +270 -0
- package/README.md +343 -246
- package/dist/adapters.d.ts +0 -7
- package/dist/adapters.js +11 -22
- package/dist/agent-rows.d.ts +98 -0
- package/dist/agent-rows.js +1 -0
- package/dist/agents-store.d.ts +6 -2
- package/dist/agents-store.js +60 -25
- package/dist/agents.d.ts +9 -125
- package/dist/agents.js +15 -305
- package/dist/answer-json.d.ts +23 -0
- package/dist/answer-json.js +58 -0
- package/dist/api-only.d.ts +14 -0
- package/dist/api-only.js +33 -0
- package/dist/archil-routes.d.ts +1 -1
- package/dist/bash-read-only.d.ts +22 -0
- package/dist/bash-read-only.js +701 -0
- package/dist/box-egress-vm.d.ts +0 -8
- package/dist/box-egress-vm.js +0 -10
- package/dist/box-egress.d.ts +0 -4
- package/dist/box-egress.js +1 -16
- package/dist/box-paths.d.ts +25 -2
- package/dist/box-paths.js +79 -11
- package/dist/box-relay.d.ts +0 -11
- package/dist/box-relay.js +0 -20
- package/dist/box-settings.d.ts +26 -0
- package/dist/box-settings.js +63 -0
- package/dist/boxes.d.ts +21 -1
- package/dist/boxes.js +27 -20
- package/dist/browser-commands.d.ts +5 -2
- package/dist/browser-commands.js +4 -4
- package/dist/browser-computer.d.ts +5 -0
- package/dist/browser-computer.js +21 -0
- package/dist/browser-cost.d.ts +9 -6
- package/dist/browser-cost.js +94 -51
- package/dist/browser-live.d.ts +7 -20
- package/dist/browser-live.js +15 -93
- package/dist/browser-person.d.ts +18 -53
- package/dist/browser-person.js +94 -279
- package/dist/budget.d.ts +0 -4
- package/dist/budget.js +0 -7
- package/dist/cap.d.ts +8 -12
- package/dist/cap.js +12 -27
- package/dist/cli.js +61 -20
- package/dist/closed-pipes.d.ts +2 -0
- package/dist/closed-pipes.js +13 -0
- package/dist/computer-commands.d.ts +2 -0
- package/dist/computer-commands.js +2 -0
- package/dist/computer-keys.js +5 -2
- package/dist/computer-setting.d.ts +20 -4
- package/dist/computer-setting.js +25 -5
- package/dist/computers.d.ts +31 -3
- package/dist/computers.js +70 -11
- package/dist/confine.d.ts +120 -0
- package/dist/confine.js +436 -0
- package/dist/console/assets/index-RPHGlXmM.js +540 -0
- package/dist/console/assets/index-opTN5Naq.css +1 -0
- package/dist/console/index.html +2 -2
- package/dist/daytona-api.d.ts +0 -11
- package/dist/daytona-api.js +1 -82
- package/dist/decision-rows.d.ts +1 -1
- package/dist/decision-rows.js +1 -1
- package/dist/docker-daemon.d.ts +31 -0
- package/dist/docker-daemon.js +56 -0
- package/dist/docker-host-addresses.d.ts +37 -0
- package/dist/docker-host-addresses.js +110 -0
- package/dist/effort-guidance.js +4 -3
- package/dist/environment.d.ts +0 -3
- package/dist/environment.js +3 -5
- package/dist/errors.d.ts +0 -2
- package/dist/errors.js +6 -9
- package/dist/filed-answer.d.ts +32 -0
- package/dist/filed-answer.js +160 -0
- package/dist/files.d.ts +1 -11
- package/dist/files.js +2 -21
- package/dist/github-connection.d.ts +2 -2
- package/dist/github-connection.js +1 -1
- package/dist/graph-view.d.ts +3 -5
- package/dist/graph-view.js +4 -14
- package/dist/handed-values.d.ts +9 -0
- package/dist/handed-values.js +26 -0
- package/dist/harness-install.d.ts +22 -5
- package/dist/harness-install.js +98 -20
- package/dist/harness-opencode/adapter.d.ts +7 -0
- package/dist/harness-opencode/adapter.js +23 -6
- package/dist/harness-opencode/index.d.ts +1 -1
- package/dist/harness-opencode/index.js +1 -1
- package/dist/harness-opencode/verify.d.ts +17 -0
- package/dist/harness-opencode/verify.js +30 -10
- package/dist/harness-table.js +6 -1
- package/dist/harness.d.ts +4 -3
- package/dist/harness.js +16 -1
- package/dist/heard.js +1 -1
- package/dist/held-fd.d.ts +5 -0
- package/dist/held-fd.js +14 -0
- package/dist/host-calls.d.ts +20 -0
- package/dist/host-calls.js +12 -0
- package/dist/host-tools.d.ts +4 -21
- package/dist/host-tools.js +4 -61
- package/dist/host.d.ts +2 -4
- package/dist/host.js +2 -7
- package/dist/import-summary.d.ts +1 -5
- package/dist/import-summary.js +0 -21
- package/dist/import.d.ts +6 -6
- package/dist/import.js +62 -24
- package/dist/key-refusal.d.ts +5 -1
- package/dist/key-refusal.js +19 -4
- package/dist/learn/check.d.ts +6 -2
- package/dist/learn/check.js +83 -29
- package/dist/learn/folder.d.ts +5 -1
- package/dist/learn/folder.js +10 -3
- package/dist/learn/grade.d.ts +14 -0
- package/dist/learn/grade.js +68 -12
- package/dist/learn/index.d.ts +4 -4
- package/dist/learn/index.js +4 -4
- package/dist/learn/reflect.d.ts +31 -2
- package/dist/learn/reflect.js +79 -4
- package/dist/learn/relevance.d.ts +24 -0
- package/dist/learn/relevance.js +91 -0
- package/dist/learn/secrets.d.ts +8 -1
- package/dist/learn/secrets.js +95 -15
- package/dist/learn/skills.d.ts +112 -14
- package/dist/learn/skills.js +556 -52
- package/dist/learned-workflow.d.ts +37 -5
- package/dist/learned-workflow.js +106 -12
- package/dist/learning-routes.d.ts +16 -49
- package/dist/learning-routes.js +46 -36
- package/dist/learning-run.d.ts +6 -250
- package/dist/learning-run.js +11 -665
- package/dist/learning-words.d.ts +12 -5
- package/dist/learning-words.js +18 -9
- package/dist/learning.d.ts +93 -77
- package/dist/learning.js +176 -304
- package/dist/live/stream.js +1 -1
- package/dist/live/translate.js +13 -3
- package/dist/local-models.d.ts +11 -5
- package/dist/local-models.js +13 -8
- package/dist/mcp-gateway.d.ts +11 -52
- package/dist/mcp-gateway.js +44 -148
- package/dist/mcp-oauth.d.ts +10 -14
- package/dist/mcp-oauth.js +14 -32
- package/dist/mcp-probe.d.ts +0 -7
- package/dist/mcp-probe.js +4 -10
- package/dist/mcp-servers-store.d.ts +6 -6
- package/dist/mcp-servers-store.js +13 -15
- package/dist/mcp-store.js +3 -4
- package/dist/mcp.js +4 -3
- package/dist/model.d.ts +6 -7
- package/dist/model.js +43 -27
- package/dist/monid.d.ts +0 -2
- package/dist/monid.js +3 -6
- package/dist/openapi.d.ts +8 -1
- package/dist/openapi.js +124 -50
- package/dist/page.d.ts +1 -3
- package/dist/page.js +9 -19
- package/dist/paid-calls.d.ts +21 -27
- package/dist/paid-calls.js +28 -62
- package/dist/pause.d.ts +0 -2
- package/dist/pause.js +1 -8
- package/dist/place.d.ts +0 -5
- package/dist/place.js +0 -10
- package/dist/print.d.ts +2 -1
- package/dist/print.js +4 -2
- package/dist/propose.js +83 -39
- package/dist/push-handler.d.ts +1 -1
- package/dist/reflect-range.d.ts +25 -0
- package/dist/reflect-range.js +30 -0
- package/dist/refusal-words.d.ts +5 -1
- package/dist/refusal-words.js +5 -1
- package/dist/rows.d.ts +1 -1
- package/dist/run-cut.d.ts +0 -6
- package/dist/run-cut.js +0 -6
- package/dist/run-db.d.ts +2 -13
- package/dist/run-db.js +2 -41
- package/dist/run-launch.d.ts +19 -2
- package/dist/run-launch.js +13 -5
- package/dist/run-learning-served.d.ts +46 -2
- package/dist/run-learning-served.js +26 -7
- package/dist/run-reads-over-store.d.ts +1 -1
- package/dist/run-reads-over-store.js +15 -6
- package/dist/run-secrets.d.ts +33 -0
- package/dist/run-secrets.js +53 -0
- package/dist/run-timeline-served.d.ts +5 -1
- package/dist/run-timeline-served.js +19 -5
- package/dist/run-view.d.ts +12 -17
- package/dist/run-view.js +29 -28
- package/dist/runnable.d.ts +18 -10
- package/dist/runnable.js +21 -10
- package/dist/runner/adapter-loop.d.ts +14 -3
- package/dist/runner/adapter-loop.js +464 -62
- package/dist/runner/adapter-run-spec.d.ts +29 -8
- package/dist/runner/adapter-run-spec.js +96 -37
- package/dist/runner/adapters/index.js +11 -1
- package/dist/runner/adapters/pi.d.ts +15 -1
- package/dist/runner/adapters/pi.js +19 -5
- package/dist/runner/beside.d.ts +30 -3
- package/dist/runner/beside.js +54 -12
- package/dist/runner/claim-secrets.d.ts +10 -1
- package/dist/runner/claim-secrets.js +49 -15
- package/dist/runner/cli-run-spec.d.ts +2 -0
- package/dist/runner/cli-run-spec.js +60 -17
- package/dist/runner/commands.js +1 -1
- package/dist/runner/computer-journal.d.ts +15 -2
- package/dist/runner/computer-journal.js +41 -5
- package/dist/runner/conformance/harness.js +118 -8
- package/dist/runner/conformance/subject.d.ts +4 -1
- package/dist/runner/dispatch.d.ts +8 -1
- package/dist/runner/dispatch.js +81 -7
- package/dist/runner/end-of-run.d.ts +8 -2
- package/dist/runner/end-of-run.js +10 -4
- package/dist/runner/github-app.js +1 -1
- package/dist/runner/harness-loops.d.ts +9 -1
- package/dist/runner/harness-loops.js +15 -2
- package/dist/runner/journal-kinds.d.ts +30 -6
- package/dist/runner/journal-kinds.js +20 -5
- package/dist/runner/journal-reader.d.ts +1 -1
- package/dist/runner/journal-reader.js +2 -2
- package/dist/runner/learning-ask.d.ts +4 -0
- package/dist/runner/learning-ask.js +15 -1
- package/dist/runner/learning-end.d.ts +18 -3
- package/dist/runner/learning-end.js +113 -11
- package/dist/runner/learning-loop.d.ts +5 -0
- package/dist/runner/learning-loop.js +26 -6
- package/dist/runner/live-ticker.d.ts +1 -1
- package/dist/runner/live-ticker.js +28 -4
- package/dist/runner/loop-contract.d.ts +3 -2
- package/dist/runner/main.d.ts +64 -10
- package/dist/runner/main.js +165 -27
- package/dist/runner/mcp-claim.d.ts +1 -1
- package/dist/runner/mcp-claim.js +2 -3
- package/dist/runner/mcp-run.d.ts +2 -0
- package/dist/runner/mcp-run.js +15 -5
- package/dist/runner/memory-claim.d.ts +9 -0
- package/dist/runner/memory-claim.js +25 -0
- package/dist/runner/metrics.d.ts +33 -1
- package/dist/runner/metrics.js +85 -1
- package/dist/runner/model-route.d.ts +13 -0
- package/dist/runner/model-route.js +154 -11
- package/dist/runner/redact.d.ts +8 -2
- package/dist/runner/redact.js +11 -2
- package/dist/runner/reflect-hook.d.ts +12 -1
- package/dist/runner/reflect-hook.js +33 -3
- package/dist/runner/runner.d.ts +20 -0
- package/dist/runner/runner.js +56 -10
- package/dist/runner/store-port.d.ts +1 -1
- package/dist/runner/store-secrets.d.ts +3 -3
- package/dist/runner/store-secrets.js +8 -12
- package/dist/runner/text-tools.d.ts +44 -0
- package/dist/runner/text-tools.js +237 -0
- package/dist/runner/workflow-loop.d.ts +6 -0
- package/dist/runner/workflow-loop.js +6 -4
- package/dist/runner/workflow-spec.d.ts +18 -0
- package/dist/runner/workflow-spec.js +68 -26
- package/dist/runs-over-store.d.ts +8 -3
- package/dist/runs-over-store.js +76 -19
- package/dist/seal-key.d.ts +54 -15
- package/dist/seal-key.js +232 -82
- package/dist/sealed-settings-store.d.ts +19 -4
- package/dist/sealed-settings-store.js +9 -9
- package/dist/sealing.d.ts +4 -0
- package/dist/sealing.js +20 -0
- package/dist/secret-cut.d.ts +1 -1
- package/dist/secret-cut.js +1 -1
- package/dist/secret-name.d.ts +9 -0
- package/dist/secret-name.js +15 -0
- package/dist/secret-writes.d.ts +0 -1
- package/dist/secret-writes.js +0 -1
- package/dist/secrets.d.ts +2 -10
- package/dist/secrets.js +21 -17
- package/dist/server-fs.d.ts +4 -0
- package/dist/server-fs.js +8 -2
- package/dist/server-lock.d.ts +0 -24
- package/dist/server-lock.js +1 -80
- package/dist/server-secrets-store.d.ts +4 -4
- package/dist/server-secrets-store.js +10 -32
- package/dist/server-secrets.d.ts +4 -9
- package/dist/server-secrets.js +12 -33
- package/dist/server.d.ts +26 -44
- package/dist/server.js +229 -142
- package/dist/sign-ins-store.d.ts +34 -36
- package/dist/sign-ins-store.js +27 -38
- package/dist/sign-ins.d.ts +2 -119
- package/dist/sign-ins.js +5 -311
- package/dist/standard-webhooks.d.ts +1 -1
- package/dist/standard-webhooks.js +1 -1
- package/dist/stats.d.ts +3 -2
- package/dist/stats.js +1 -1
- package/dist/store/agent-body.d.ts +8 -2
- package/dist/store/agent-body.js +16 -4
- package/dist/store/index.d.ts +3 -3
- package/dist/store/index.js +3 -3
- package/dist/store/open.d.ts +18 -5
- package/dist/store/open.js +23 -3
- package/dist/store/postgres/connection.d.ts +18 -2
- package/dist/store/postgres/connection.js +156 -32
- package/dist/store/postgres/migrate.d.ts +4 -0
- package/dist/store/postgres/migrate.js +27 -3
- package/dist/store/postgres/migrations/0003_commands_one_answer.check.sql +19 -0
- package/dist/store/postgres/migrations/0003_commands_one_answer.sql +13 -0
- package/dist/store/postgres/store.d.ts +9 -2
- package/dist/store/postgres/store.js +9 -0
- package/dist/store/schema.js +1 -1
- package/dist/store/seal.js +2 -2
- package/dist/store/sqlite/connection.js +49 -5
- package/dist/store/sqlite/run-patch.d.ts +3 -0
- package/dist/store/sqlite/run-patch.js +3 -0
- package/dist/store/sqlite/store.js +64 -20
- package/dist/store/store.d.ts +38 -6
- package/dist/store/store.js +12 -2
- package/dist/store-reads/cap.d.ts +1 -1
- package/dist/store-reads/cap.js +22 -4
- package/dist/store-reads/journal.d.ts +25 -6
- package/dist/store-reads/journal.js +173 -21
- package/dist/store-reads/learning.d.ts +26 -8
- package/dist/store-reads/learning.js +12 -4
- package/dist/store-reads/workflow.d.ts +1 -1
- package/dist/store-reads/workflow.js +1 -1
- package/dist/store-view.d.ts +5 -1
- package/dist/store-view.js +91 -23
- package/dist/template-eval.d.ts +60 -4
- package/dist/template-eval.js +120 -54
- package/dist/templates.d.ts +24 -10
- package/dist/templates.js +69 -38
- package/dist/timeline/group.d.ts +33 -0
- package/dist/timeline/group.js +77 -0
- package/dist/timeline/index.d.ts +7 -0
- package/dist/timeline/index.js +7 -0
- package/dist/timeline/labels.d.ts +79 -0
- package/dist/timeline/labels.js +150 -0
- package/dist/timeline/origin.d.ts +56 -0
- package/dist/timeline/origin.js +92 -0
- package/dist/timeline/render-text.d.ts +11 -0
- package/dist/timeline/render-text.js +62 -0
- package/dist/timeline/timeline.d.ts +274 -0
- package/dist/timeline/timeline.js +1308 -0
- package/dist/timeline/util.d.ts +17 -0
- package/dist/timeline/util.js +49 -0
- package/dist/tool-gate.d.ts +41 -216
- package/dist/tool-gate.js +79 -540
- package/dist/tool-keys-store.d.ts +11 -12
- package/dist/tool-keys-store.js +24 -31
- package/dist/tool-keys.d.ts +9 -104
- package/dist/tool-keys.js +15 -271
- package/dist/tool-plane.d.ts +14 -42
- package/dist/tool-plane.js +23 -81
- package/dist/tool-settings-store.d.ts +1 -2
- package/dist/tool-settings-store.js +5 -8
- package/dist/tools.d.ts +31 -19
- package/dist/tools.js +101 -108
- package/dist/trigger-adapter.d.ts +2 -2
- package/dist/trigger-schedule.js +2 -1
- package/dist/trigger-secrets-shown.js +5 -1
- package/dist/trigger-targets.d.ts +13 -1
- package/dist/trigger-targets.js +31 -1
- package/dist/triggers.d.ts +2 -2
- package/dist/triggers.js +3 -3
- package/dist/turn-end.d.ts +1 -1
- package/dist/turn-end.js +1 -1
- package/dist/turns.d.ts +0 -6
- package/dist/turns.js +0 -19
- package/dist/uploads.js +1 -1
- package/dist/usage.d.ts +0 -2
- package/dist/usage.js +0 -2
- package/dist/validate.d.ts +2 -0
- package/dist/validate.js +23 -14
- package/dist/webhooks.d.ts +102 -76
- package/dist/webhooks.js +266 -233
- package/dist/workflow-run.d.ts +114 -0
- package/dist/workflow-run.js +144 -0
- package/dist/workspace/beneath.d.ts +19 -0
- package/dist/workspace/beneath.js +106 -0
- package/dist/workspace/computer-deps.d.ts +52 -5
- package/dist/workspace/computer-deps.js +154 -19
- package/dist/workspace/computer-for-claim.d.ts +19 -14
- package/dist/workspace/computer-for-claim.js +77 -16
- package/dist/workspace/computer-hold.d.ts +6 -1
- package/dist/workspace/computer-hold.js +6 -1
- package/dist/workspace/computer-janitor.d.ts +47 -0
- package/dist/workspace/computer-janitor.js +182 -0
- package/dist/workspace/computer-providers.d.ts +6 -2
- package/dist/workspace/computer-providers.js +1 -1
- package/dist/workspace/grep-worker.d.ts +5 -0
- package/dist/workspace/grep-worker.js +9 -6
- package/dist/workspace/index.d.ts +4 -3
- package/dist/workspace/index.js +4 -4
- package/dist/workspace/local.d.ts +11 -2
- package/dist/workspace/local.js +161 -43
- package/dist/workspace/resolve-computer.d.ts +50 -10
- package/dist/workspace/resolve-computer.js +46 -11
- package/dist/workspace/run-tools.d.ts +13 -31
- package/dist/workspace/run-tools.js +10 -27
- package/dist/workspace-paths.d.ts +0 -17
- package/dist/workspace-paths.js +1 -41
- package/dist/workspace.d.ts +0 -17
- package/dist/workspace.js +3 -99
- package/launcher/agentrun-serve +6 -33
- package/package.json +14 -14
- package/templates/README.md +9 -3
- package/templates/inbound-lead/agent.md +1 -1
- package/templates/kyb/agent.md +1 -1
- package/templates/market-monitor/agent.md +2 -2
- package/templates/market-monitor/skills/monitor/SKILL.md +6 -4
- package/dist/app.d.ts +0 -5
- package/dist/app.js +0 -514
- package/dist/bound-files.d.ts +0 -4
- package/dist/bound-files.js +0 -164
- package/dist/bounds.d.ts +0 -26
- package/dist/bounds.js +0 -137
- package/dist/coding-tools.d.ts +0 -47
- package/dist/coding-tools.js +0 -189
- package/dist/computer-adb.d.ts +0 -12
- package/dist/computer-adb.js +0 -14
- package/dist/console/assets/index-35-7aeIZ.js +0 -519
- package/dist/console/assets/index-Cejbov-Y.css +0 -1
- package/dist/content-filter.d.ts +0 -7
- package/dist/content-filter.js +0 -23
- package/dist/disk.d.ts +0 -33
- package/dist/disk.js +0 -85
- package/dist/door-line.d.ts +0 -23
- package/dist/door-line.js +0 -83
- package/dist/grade.d.ts +0 -63
- package/dist/grade.js +0 -150
- package/dist/harness-app.d.ts +0 -69
- package/dist/harness-app.js +0 -1162
- package/dist/mcp-servers.d.ts +0 -269
- package/dist/mcp-servers.js +0 -456
- package/dist/packs.d.ts +0 -71
- package/dist/packs.js +0 -180
- package/dist/request-start.d.ts +0 -27
- package/dist/request-start.js +0 -381
- package/dist/run-box.d.ts +0 -205
- package/dist/run-box.js +0 -661
- package/dist/run-holder.d.ts +0 -52
- package/dist/run-holder.js +0 -139
- package/dist/runs.d.ts +0 -1200
- package/dist/runs.js +0 -4103
- package/dist/sealed-settings.d.ts +0 -60
- package/dist/sealed-settings.js +0 -178
- package/dist/served-only.d.ts +0 -5
- package/dist/served-only.js +0 -12
- package/dist/sessions.d.ts +0 -6
- package/dist/sessions.js +0 -109
- package/dist/workspace/computer-serves.d.ts +0 -12
- package/dist/workspace/computer-serves.js +0 -15
package/dist/packs.js
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import { EFFORTS, THINKING_LEVELS } from "./model.js";
|
|
2
|
-
import { isObject } from "./validate.js";
|
|
3
|
-
/** A pack's name. */
|
|
4
|
-
export const PACK_NAME = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
|
5
|
-
/** An output type: a key of a pack's `outputs`. */
|
|
6
|
-
export const OUTPUT_TYPE = /^[a-z0-9][a-z0-9_-]{0,63}$/;
|
|
7
|
-
/** A skill name a pack names: the name rule Agent Skills and the Claude adapter use. */
|
|
8
|
-
export const PACK_SKILL = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
|
9
|
-
/** The most skills one list of a pack names. */
|
|
10
|
-
export const MAX_PACK_SKILLS = 32;
|
|
11
|
-
/** The most output types one pack names. */
|
|
12
|
-
export const MAX_PACK_OUTPUTS = 32;
|
|
13
|
-
/** The longest output file: the same cap serve applies to a skill path. */
|
|
14
|
-
export const MAX_OUTPUT_FILE = 512;
|
|
15
|
-
const PACK_FIELDS = ["version", "name", "effort", "outputs", "default_output"];
|
|
16
|
-
const EFFORT_FIELDS = ["skills", "thinking"];
|
|
17
|
-
const OUTPUT_FIELDS = ["skills", "file", "effort"];
|
|
18
|
-
const OVERRIDE_FIELDS = ["skills"];
|
|
19
|
-
const issue = (field, reason, message, fix) => ({ field, reason, message, ...(fix ? { fix } : {}) });
|
|
20
|
-
/** `key` under `at`; at the top of a pack with no `at`, the key alone. */
|
|
21
|
-
const under = (at, key) => (at ? `${at}.${key}` : key);
|
|
22
|
-
/** `value`'s own property `key`, never one it inherits (an output type may be named `constructor`). */
|
|
23
|
-
const own = (value, key) => (Object.hasOwn(value, key) ? value[key] : undefined);
|
|
24
|
-
/** Each key of `value` not in `fields` refused. */
|
|
25
|
-
function onlyFields(value, fields, at, out) {
|
|
26
|
-
for (const key of Object.keys(value)) {
|
|
27
|
-
if (!fields.includes(key))
|
|
28
|
-
out.push(issue(under(at, key), "not_allowed", `${at || "the pack"} keeps no field ${key}: it keeps ${fields.join(", ")}`, `Remove ${under(at, key)}.`));
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
/** A list of skill names: each one PACK_SKILL takes, none twice, at most MAX_PACK_SKILLS. Over the limit, the first
|
|
32
|
-
* MAX_PACK_SKILLS are still checked and the rest are not. */
|
|
33
|
-
function skillList(value, at, out) {
|
|
34
|
-
if (value === undefined)
|
|
35
|
-
return void out.push(issue(at, "missing", `${at} is required`));
|
|
36
|
-
if (!Array.isArray(value))
|
|
37
|
-
return void out.push(issue(at, "wrong_type", `${at} is not a list of skill names`));
|
|
38
|
-
if (value.length > MAX_PACK_SKILLS)
|
|
39
|
-
out.push(issue(at, "too_many", `${at} names ${value.length} skills, more than ${MAX_PACK_SKILLS}`));
|
|
40
|
-
const seen = new Set();
|
|
41
|
-
value.slice(0, MAX_PACK_SKILLS).forEach((name, i) => {
|
|
42
|
-
const field = `${at}[${i}]`;
|
|
43
|
-
if (typeof name !== "string")
|
|
44
|
-
out.push(issue(field, "wrong_type", `${field} is not a skill name`));
|
|
45
|
-
else if (!PACK_SKILL.test(name))
|
|
46
|
-
out.push(issue(field, "bad_format", `${field} is not a skill name: ${JSON.stringify(name)} must match ${PACK_SKILL.source}`));
|
|
47
|
-
else if (seen.has(name))
|
|
48
|
-
out.push(issue(field, "duplicate", `${field} names ${name} again`, `Remove ${field}.`));
|
|
49
|
-
else
|
|
50
|
-
seen.add(name);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
/** An object with `skills` and only `fields`, or undefined after its issue. */
|
|
54
|
-
function skillsRow(value, fields, at, out) {
|
|
55
|
-
if (value === undefined)
|
|
56
|
-
return void out.push(issue(at, "missing", `${at} is required`));
|
|
57
|
-
if (!isObject(value))
|
|
58
|
-
return void out.push(issue(at, "wrong_type", `${at} is not an object`));
|
|
59
|
-
onlyFields(value, fields, at, out);
|
|
60
|
-
skillList(value.skills, under(at, "skills"), out);
|
|
61
|
-
return value;
|
|
62
|
-
}
|
|
63
|
-
/** An object keyed by effort, each row checked by `row`; `every` asks for all three. */
|
|
64
|
-
function byEffort(value, at, every, out, row) {
|
|
65
|
-
if (value === undefined)
|
|
66
|
-
return void out.push(issue(at, "missing", `${at} is required`));
|
|
67
|
-
if (!isObject(value))
|
|
68
|
-
return void out.push(issue(at, "wrong_type", `${at} is not an object of efforts`));
|
|
69
|
-
for (const key of Object.keys(value)) {
|
|
70
|
-
if (!EFFORTS.includes(key))
|
|
71
|
-
out.push(issue(under(at, key), "not_allowed", `${key} is not an effort: an effort is one of ${EFFORTS.join(", ")}`, `Remove ${under(at, key)}.`));
|
|
72
|
-
}
|
|
73
|
-
for (const effort of EFFORTS)
|
|
74
|
-
if (every || Object.hasOwn(value, effort))
|
|
75
|
-
row(own(value, effort), under(at, effort));
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* A file a run writes in its workspace, as the run's file route reads it back: relative, `/` between segments, no
|
|
79
|
-
* segment empty or starting with `.` (`.`, `..` and dot-named entries are the run's own), no backslash or NUL.
|
|
80
|
-
*/
|
|
81
|
-
function fileIssue(file, at, out) {
|
|
82
|
-
if (typeof file !== "string")
|
|
83
|
-
out.push(issue(at, "wrong_type", `${at} is not a path`));
|
|
84
|
-
else if (file.length > MAX_OUTPUT_FILE)
|
|
85
|
-
out.push(issue(at, "too_long", `${at} is longer than ${MAX_OUTPUT_FILE} characters`));
|
|
86
|
-
else if (file.includes("\\") || file.includes("\0") || file.split("/").some((part) => part === "" || part.startsWith("."))) {
|
|
87
|
-
out.push(issue(at, "bad_format", `${at} is not a plain relative path in the run's workspace: no leading /, no empty segment, none starting with ., no backslash or NUL`));
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Every issue with `value` as a version 1 pack, none if it is one. Fields are paths into the pack
|
|
92
|
-
* (`effort.low.skills[0]`), under `at` when given (`profile.effort.low.skills[0]`); the pack itself is `at`. A skill
|
|
93
|
-
* list or `outputs` over its limit is `too_many`, and its items up to the limit are checked as any others; items past
|
|
94
|
-
* the limit are not checked, so an oversized pack makes a bounded number of issues.
|
|
95
|
-
*/
|
|
96
|
-
export function packIssues(value, { at = "" } = {}) {
|
|
97
|
-
if (!isObject(value))
|
|
98
|
-
return [issue(at, value === undefined ? "missing" : "wrong_type", `${at || "the pack"} is not an object`)];
|
|
99
|
-
const out = [];
|
|
100
|
-
onlyFields(value, PACK_FIELDS, at, out);
|
|
101
|
-
const version = under(at, "version");
|
|
102
|
-
if (value.version === undefined)
|
|
103
|
-
out.push(issue(version, "missing", `${version} is required`));
|
|
104
|
-
else if (typeof value.version !== "number")
|
|
105
|
-
out.push(issue(version, "wrong_type", `${version} is not a number`));
|
|
106
|
-
else if (value.version !== 1)
|
|
107
|
-
out.push(issue(version, "not_an_option", `${version} is 1: this server reads version 1 packs`));
|
|
108
|
-
const name = under(at, "name");
|
|
109
|
-
if (value.name === undefined)
|
|
110
|
-
out.push(issue(name, "missing", `${name} is required`));
|
|
111
|
-
else if (typeof value.name !== "string")
|
|
112
|
-
out.push(issue(name, "wrong_type", `${name} is not a string`));
|
|
113
|
-
else if (!PACK_NAME.test(value.name))
|
|
114
|
-
out.push(issue(name, "bad_format", `${name} must match ${PACK_NAME.source}`));
|
|
115
|
-
byEffort(value.effort, under(at, "effort"), true, out, (row, field) => {
|
|
116
|
-
const checked = skillsRow(row, EFFORT_FIELDS, field, out);
|
|
117
|
-
if (checked?.thinking !== undefined && !THINKING_LEVELS.includes(checked.thinking)) {
|
|
118
|
-
out.push(issue(under(field, "thinking"), "not_an_option", `${under(field, "thinking")} is one of ${THINKING_LEVELS.join(", ")}: thinking is never off`));
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
const outputs = under(at, "outputs");
|
|
122
|
-
if (value.outputs === undefined)
|
|
123
|
-
out.push(issue(outputs, "missing", `${outputs} is required`));
|
|
124
|
-
else if (!isObject(value.outputs))
|
|
125
|
-
out.push(issue(outputs, "wrong_type", `${outputs} is not an object of output types`));
|
|
126
|
-
else {
|
|
127
|
-
const entries = Object.entries(value.outputs);
|
|
128
|
-
if (entries.length > MAX_PACK_OUTPUTS)
|
|
129
|
-
out.push(issue(outputs, "too_many", `${outputs} names ${entries.length} output types, more than ${MAX_PACK_OUTPUTS}`));
|
|
130
|
-
for (const [type, output] of entries.slice(0, MAX_PACK_OUTPUTS)) {
|
|
131
|
-
const field = under(outputs, type);
|
|
132
|
-
if (!OUTPUT_TYPE.test(type))
|
|
133
|
-
out.push(issue(field, "bad_format", `the output type ${JSON.stringify(type)} must match ${OUTPUT_TYPE.source}`));
|
|
134
|
-
const checked = skillsRow(output, OUTPUT_FIELDS, field, out);
|
|
135
|
-
if (checked?.file !== undefined)
|
|
136
|
-
fileIssue(checked.file, under(field, "file"), out);
|
|
137
|
-
if (checked?.effort !== undefined)
|
|
138
|
-
byEffort(checked.effort, under(field, "effort"), false, out, (row, place) => void skillsRow(row, OVERRIDE_FIELDS, place, out));
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
const fallback = under(at, "default_output");
|
|
142
|
-
if (value.default_output !== undefined) {
|
|
143
|
-
if (typeof value.default_output !== "string")
|
|
144
|
-
out.push(issue(fallback, "wrong_type", `${fallback} is not an output type`));
|
|
145
|
-
else if (isObject(value.outputs) && !Object.hasOwn(value.outputs, value.default_output)) {
|
|
146
|
-
const types = Object.keys(value.outputs);
|
|
147
|
-
out.push(issue(fallback, "not_an_option", `${fallback} is one of the pack's output types: ${types.length ? types.join(", ") : "it names none"}`, `Name a key of ${outputs}, or remove ${fallback}.`));
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
return out;
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* `pack` (one packIssues finds nothing wrong with) read for a run at `effort` asked for `output`: the effort row's
|
|
154
|
-
* skills, then the output's writer skills (its skills at that effort if it names some there, else its own), each skill
|
|
155
|
-
* once at its first place. With no output asked, the pack's default_output; with neither, no output and no writer.
|
|
156
|
-
* `thinking` is there only when the effort row sets one, so the caller keeps its own level otherwise. An effort that is
|
|
157
|
-
* not one, or an output type the pack does not name, is an issue on `effort` or `output`.
|
|
158
|
-
*/
|
|
159
|
-
export function resolvePack(pack, { effort = "medium", output } = {}) {
|
|
160
|
-
const issues = [];
|
|
161
|
-
if (!EFFORTS.includes(effort))
|
|
162
|
-
issues.push(issue("effort", "not_an_option", `effort is one of ${EFFORTS.join(", ")}`));
|
|
163
|
-
if (output !== undefined && (typeof output !== "string" || !Object.hasOwn(pack.outputs, output))) {
|
|
164
|
-
const types = Object.keys(pack.outputs);
|
|
165
|
-
const options = types.length ? `the pack ${pack.name} names ${types.join(", ")}` : `the pack ${pack.name} names no output type`;
|
|
166
|
-
issues.push(issue("output", typeof output === "string" ? "not_an_option" : "wrong_type", `output is an output type of the pack: ${options}`));
|
|
167
|
-
}
|
|
168
|
-
if (issues.length)
|
|
169
|
-
return { issues };
|
|
170
|
-
const row = pack.effort[effort];
|
|
171
|
-
const type = output ?? pack.default_output;
|
|
172
|
-
const writer = type === undefined ? undefined : pack.outputs[type];
|
|
173
|
-
const writers = writer ? (writer.effort?.[effort]?.skills ?? writer.skills) : [];
|
|
174
|
-
return {
|
|
175
|
-
effort,
|
|
176
|
-
...(row.thinking !== undefined ? { thinking: row.thinking } : {}),
|
|
177
|
-
skills: [...new Set([...row.skills, ...writers])],
|
|
178
|
-
...(type !== undefined && writer ? { output: { type, ...(writer.file !== undefined ? { file: writer.file } : {}) } } : {}),
|
|
179
|
-
};
|
|
180
|
-
}
|
package/dist/request-start.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { Models } from "@earendil-works/pi-ai";
|
|
2
|
-
import { type DurableRun } from "@parcha/pi-durable-disk";
|
|
3
|
-
import { type RefusalFigures } from "./cap.js";
|
|
4
|
-
/** Whether the run in `root` (its directory) was cancelled: the mark serve's cancel writes there first. */
|
|
5
|
-
export declare const cancelledIn: (root: string) => () => boolean;
|
|
6
|
-
/** How long a mark that carries a hold (a lost or failed request's end, an unreadable cost's) may take to reach the store
|
|
7
|
-
* before the request fails loudly (END_NOT_WRITTEN): 15 s. A lost mark is waited for before the retry is sent, so a
|
|
8
|
-
* commit queued behind a large entry on a slow disk (seconds) past 1 s let the retry leave, and its answer reach pi,
|
|
9
|
-
* with no mark in the store: a crash then read the request at the answer's own usage. Such a mark is written only on a
|
|
10
|
-
* path already lost or failed, so the wait costs latency there and nowhere else; 15 s holds such a commit with room to
|
|
11
|
-
* spare, and still fails a store that has stopped within a turn's patience, never a turn held without bound. */
|
|
12
|
-
export declare const END_MARK_WAIT_MS = 15000;
|
|
13
|
-
/** `models` with each generation request marked before it leaves, once `open` hands over the run (its Harness). Once
|
|
14
|
-
* `cancelled` says the run was cancelled, no request is sent: each is refused here, before it is marked, or after its
|
|
15
|
-
* mark when the cancel came while the mark waited, and then its start is withdrawn (an end mark, `unsent`). */
|
|
16
|
-
export declare function markRequests<M extends Models>(models: M, cancelled?: () => boolean, { holdMarkWaitMs }?: {
|
|
17
|
-
holdMarkWaitMs?: number;
|
|
18
|
-
}): Promise<{
|
|
19
|
-
models: M;
|
|
20
|
-
open(run: DurableRun): void;
|
|
21
|
-
refused(figures?: RefusalFigures): Promise<void>;
|
|
22
|
-
wrapUp(data: {
|
|
23
|
-
capUsd: number;
|
|
24
|
-
leftUsd: number;
|
|
25
|
-
words: string;
|
|
26
|
-
}): void;
|
|
27
|
-
}>;
|
package/dist/request-start.js
DELETED
|
@@ -1,381 +0,0 @@
|
|
|
1
|
-
// A pi run's model request is marked on the run's root before it leaves (REQUEST_STARTED), and again if its stream
|
|
2
|
-
// ends aborted (REQUEST_ENDED). pi writes a request's entry when it answers or fails, and when it is aborted only if
|
|
3
|
-
// content had streamed, so one cut before that (a stop, a crash) would leave nothing to count; with the marks, the
|
|
4
|
-
// timeline counts it and reads the run's cost unknown, while a request still in flight reads as the sum so far.
|
|
5
|
-
//
|
|
6
|
-
// pi's compaction summary is a paid request too (`completeSimple`), and pi books its usage in its own usage document,
|
|
7
|
-
// never as an entry the run's timeline sums. So it is marked the same way (`request: "compaction"`), and its answer's
|
|
8
|
-
// cost is written as REQUEST_COST: the run's total, and /usage with it, include it, and one cut before its answer reads
|
|
9
|
-
// unknown. Both are wrapped here: `streamSimple` for the generation's requests, `completeSimple` for the summary. A
|
|
10
|
-
// tool's own request on the run's models (web_search's, hosted-search.ts) is a side request the same way, under its own
|
|
11
|
-
// label (sideRequests), and its cost is its tokens' plus the fee its tool names.
|
|
12
|
-
//
|
|
13
|
-
// Under a cap (cap.ts), each request comes with the worst case the cap held for it, and its start mark carries that
|
|
14
|
-
// figure (`heldUsd`), as does the end mark of a summary that failed: the cap counts a request a crash cut at it, in a
|
|
15
|
-
// later process too. The end mark of one stopped before its end carries what the cap counts it at given what it streamed
|
|
16
|
-
// (cap.ts stoppedFigure: what it streamed and an allowance, never more than its worst case). The timeline does not read it, so the run's shown cost
|
|
17
|
-
// is unknown as before. A request the cap refused gets a start mark of its own (`capRefused`), written before pi writes
|
|
18
|
-
// its error entry, so that entry answers its own mark and never stands for an earlier request a stop or a crash cut.
|
|
19
|
-
import { randomUUID } from "node:crypto";
|
|
20
|
-
import { existsSync } from "node:fs";
|
|
21
|
-
import { join } from "node:path";
|
|
22
|
-
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
23
|
-
import { ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
|
|
24
|
-
import { CAP_WRAPUP, REQUEST_COST, REQUEST_ENDED, REQUEST_REFUSED, REQUEST_STARTED } from "@parcha/pi-durable-agent/timeline";
|
|
25
|
-
import { BARRIER, syncNow } from "@parcha/pi-durable-disk";
|
|
26
|
-
import { anyAttemptLost, ATTEMPTS, attemptLost, BOUND_FIELD, BOUND_USD, CAP_REFUSED_FIELD, END_NOT_WRITTEN, CLAMP, HELD_FIELD, HELD_USD, MARK_NOT_WRITTEN, markResponseBegan, piEnded, refusalFields, sideRequests, STOPPED_USD } from "./cap.js";
|
|
27
|
-
import { RUN_CANCELLED } from "./files.js";
|
|
28
|
-
import { noStoreToSync } from "./paid-calls.js";
|
|
29
|
-
import { isConnectCode, isGatewayTimeout } from "@parcha/agentrun-kernel";
|
|
30
|
-
/** Whether the run in `root` (its directory) was cancelled: the mark serve's cancel writes there first. */
|
|
31
|
-
export const cancelledIn = (root) => () => existsSync(join(root, RUN_CANCELLED));
|
|
32
|
-
/** How long a mark that carries a hold (a lost or failed request's end, an unreadable cost's) may take to reach the store
|
|
33
|
-
* before the request fails loudly (END_NOT_WRITTEN): 15 s. A lost mark is waited for before the retry is sent, so a
|
|
34
|
-
* commit queued behind a large entry on a slow disk (seconds) past 1 s let the retry leave, and its answer reach pi,
|
|
35
|
-
* with no mark in the store: a crash then read the request at the answer's own usage. Such a mark is written only on a
|
|
36
|
-
* path already lost or failed, so the wait costs latency there and nowhere else; 15 s holds such a commit with room to
|
|
37
|
-
* spare, and still fails a store that has stopped within a turn's patience, never a turn held without bound. */
|
|
38
|
-
export const END_MARK_WAIT_MS = 15_000;
|
|
39
|
-
/** How long a stop's mark is waited for before pi has the stopped result: a stop's mark needs no fatal wait (without it
|
|
40
|
-
* the request reads as cut, at its hold, never less), so a slow store costs a stop at most this. */
|
|
41
|
-
const STOP_MARK_WAIT_MS = 1_000;
|
|
42
|
-
/** `models` with each generation request marked before it leaves, once `open` hands over the run (its Harness). Once
|
|
43
|
-
* `cancelled` says the run was cancelled, no request is sent: each is refused here, before it is marked, or after its
|
|
44
|
-
* mark when the cancel came while the mark waited, and then its start is withdrawn (an end mark, `unsent`). */
|
|
45
|
-
export async function markRequests(models, cancelled = () => false, { holdMarkWaitMs = END_MARK_WAIT_MS } = {}) {
|
|
46
|
-
const { createAssistantMessageEventStream } = await import("@earendil-works/pi-ai");
|
|
47
|
-
let open;
|
|
48
|
-
// A request a resumed turn sends before the app's open waits for it: the mark needs the run's store.
|
|
49
|
-
const opened = new Promise((resolve) => (open = resolve));
|
|
50
|
-
const mark = async (kind, data = {}) => {
|
|
51
|
-
const run = await opened;
|
|
52
|
-
await run.harness.commit(async (tx) => void (await tx.appendEntry(ROOT_CONVERSATION_ID, { kind, data })), BACKGROUND_CONTEXT);
|
|
53
|
-
};
|
|
54
|
-
/** A request's start mark, then the run journal's barrier (pi-durable-disk barriers.ts, modelRequestStarted): the mark,
|
|
55
|
-
* with the hold and bound a crash is read at, is on disk before the request is sent. A failure fails the request
|
|
56
|
-
* unsent (MARK_NOT_WRITTEN): with no store to sync, no mark is written (paid-calls.ts noStoreToSync); a sync that fails
|
|
57
|
-
* after the mark is answered by `unsent`, its end mark (best effort), so the store never holds a start that did not
|
|
58
|
-
* leave. */
|
|
59
|
-
const markStarted = async (data, unsent) => {
|
|
60
|
-
const run = await opened;
|
|
61
|
-
if (noStoreToSync(run.store))
|
|
62
|
-
throw new Error("the run's store is not open, so its start mark cannot be made durable");
|
|
63
|
-
await mark(REQUEST_STARTED, data);
|
|
64
|
-
try {
|
|
65
|
-
await syncNow(run.store, BARRIER.modelRequestStarted);
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
await mark(REQUEST_ENDED, unsent).catch(() => undefined);
|
|
69
|
-
throw new Error(`written, but it could not be made durable: ${reason(error)}`);
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
const failed = (model, why) => ({
|
|
73
|
-
role: "assistant", content: [], api: model.api, provider: model.provider, model: model.id, stopReason: "error", timestamp: Date.now(),
|
|
74
|
-
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
|
|
75
|
-
errorMessage: why,
|
|
76
|
-
});
|
|
77
|
-
const reason = (error) => String(error?.message ?? error).slice(0, 200);
|
|
78
|
-
/** Whether `work` (a mark's write) is in the store within `holdMarkWaitMs` (END_MARK_WAIT_MS, a test's own when it
|
|
79
|
-
* gives one): undefined when it is, else why not (its refusal, or the wait run out). */
|
|
80
|
-
const landed = (work) => new Promise((resolve) => {
|
|
81
|
-
const timer = setTimeout(() => resolve(new Error(`the store had not taken it in ${holdMarkWaitMs} ms`)), holdMarkWaitMs);
|
|
82
|
-
work.then(() => (clearTimeout(timer), resolve(undefined)), (error) => (clearTimeout(timer), resolve(error ?? new Error("refused"))));
|
|
83
|
-
});
|
|
84
|
-
/** Until `work` settles, or `ms` pass. */
|
|
85
|
-
const within = (work, ms) => new Promise((resolve) => {
|
|
86
|
-
const timer = setTimeout(resolve, ms);
|
|
87
|
-
void work.finally(() => (clearTimeout(timer), resolve()));
|
|
88
|
-
});
|
|
89
|
-
/** What the run's cap held for a request (its options' HELD_USD), as an end mark's field; none without a cap. */
|
|
90
|
-
const heldOf = (options) => {
|
|
91
|
-
const held = options?.[HELD_USD];
|
|
92
|
-
return typeof held === "number" && Number.isFinite(held) ? { [HELD_FIELD]: held } : {};
|
|
93
|
-
};
|
|
94
|
-
/** Whether a request the cap watched is lost (piEnded, from the attempts its cap recorded): an error before any event
|
|
95
|
-
* that left, or an answer after an attempt that was lost. It can have been billed, so it counts at its hold. Only a
|
|
96
|
-
* request the cap watched (a hold, and its attempts, or null on an API it cannot watch): another is read as before. */
|
|
97
|
-
const lostOf = (options, message) => {
|
|
98
|
-
const record = options;
|
|
99
|
-
if (typeof record?.[HELD_USD] !== "number" || !record || !(ATTEMPTS in record))
|
|
100
|
-
return false;
|
|
101
|
-
return piEnded(message, false, record[ATTEMPTS] ?? undefined).end === "lost";
|
|
102
|
-
};
|
|
103
|
-
/** `options` whose fetch writes the request's end mark at its hold (`lost`), once, the moment an attempt is known lost:
|
|
104
|
-
* a rejection once it could have left (any code but a connect one, and not the run's own stop) or a gateway's timeout
|
|
105
|
-
* (504, 524), as that attempt's fetch ends; an answer pi-ai then tries again (a 2xx that was not the last), before the
|
|
106
|
-
* next attempt is sent, since only the retry says so. Whatever follows (a retry's answer, a stop while pi-ai waits to
|
|
107
|
-
* retry, an error), the provider can have billed the lost attempt, and the mark is in the store before pi's entry for
|
|
108
|
-
* the request can be, so a later process reads the hold this one books, with no window between them. `options` as
|
|
109
|
-
* given on a request the cap does not watch. */
|
|
110
|
-
const watchingLost = (options, markLost) => {
|
|
111
|
-
const record = options;
|
|
112
|
-
const attempts = record && ATTEMPTS in record ? record[ATTEMPTS] : null;
|
|
113
|
-
if (!attempts || typeof record?.[HELD_USD] !== "number" || typeof record.fetch !== "function")
|
|
114
|
-
return options;
|
|
115
|
-
const inner = record.fetch;
|
|
116
|
-
let marked = false;
|
|
117
|
-
const markOnce = async () => {
|
|
118
|
-
if (marked)
|
|
119
|
-
return;
|
|
120
|
-
marked = true;
|
|
121
|
-
await markLost().catch(() => undefined);
|
|
122
|
-
};
|
|
123
|
-
/** Whether the attempt the fetch just recorded is lost as it ends, whatever comes after it. */
|
|
124
|
-
const lostNow = () => {
|
|
125
|
-
const last = attempts.at(-1);
|
|
126
|
-
return last !== undefined && ("code" in last ? !last.aborted && !isConnectCode(last.code) : isGatewayTimeout(last.status));
|
|
127
|
-
};
|
|
128
|
-
return { ...record, fetch: (async (input, init) => {
|
|
129
|
-
// An answer pi-ai tries again: lost, as the retry shows.
|
|
130
|
-
if (attempts.some((attempt) => attemptLost(attempt, false)))
|
|
131
|
-
await markOnce();
|
|
132
|
-
let response;
|
|
133
|
-
try {
|
|
134
|
-
response = await inner(input, init);
|
|
135
|
-
}
|
|
136
|
-
catch (error) {
|
|
137
|
-
if (lostNow())
|
|
138
|
-
await markOnce();
|
|
139
|
-
throw error;
|
|
140
|
-
}
|
|
141
|
-
if (lostNow())
|
|
142
|
-
await markOnce();
|
|
143
|
-
return response;
|
|
144
|
-
}) };
|
|
145
|
-
};
|
|
146
|
-
/** What the run's cap counts a request stopped before its end at, given the answer it had streamed (its options'
|
|
147
|
-
* STOPPED_USD), as an end mark's field; its worst case when the cap gave no such figure, or when an attempt before the
|
|
148
|
-
* stop was lost (anyAttemptLost: it can have been billed, and the cap counts it at its hold in this process too); none
|
|
149
|
-
* without a cap. The one figure every stop's end mark carries, a generation's and a summary's. */
|
|
150
|
-
const stoppedOf = (options, message) => {
|
|
151
|
-
const attempts = options?.[ATTEMPTS];
|
|
152
|
-
if (anyAttemptLost(attempts ?? undefined))
|
|
153
|
-
return heldOf(options);
|
|
154
|
-
const stopped = options?.[STOPPED_USD];
|
|
155
|
-
const figure = typeof stopped === "function" ? stopped(message) : undefined;
|
|
156
|
-
return typeof figure === "number" && Number.isFinite(figure) ? { [HELD_FIELD]: figure } : heldOf(options);
|
|
157
|
-
};
|
|
158
|
-
/** A request's bound (BOUND_FIELD, when above 0: the run's cost serves it while the request is in flight), as its
|
|
159
|
-
* start mark keeps it. */
|
|
160
|
-
const boundOf = (options) => {
|
|
161
|
-
const bound = options?.[BOUND_USD];
|
|
162
|
-
return typeof bound === "number" && Number.isFinite(bound) && bound > 0 ? { [BOUND_FIELD]: bound } : {};
|
|
163
|
-
};
|
|
164
|
-
/** A start mark's figures: what the cap held it at, its bound, and what the cap limited its output to when it did
|
|
165
|
-
* (CLAMP), so a reply cut short there says so on the run's timeline. */
|
|
166
|
-
const startOf = (options) => {
|
|
167
|
-
const clamp = options?.[CLAMP];
|
|
168
|
-
const kept = clamp && typeof clamp.tokens === "number" && typeof clamp.leftUsd === "number" && typeof clamp.capUsd === "number" ? { clamp: { tokens: clamp.tokens, leftUsd: clamp.leftUsd, capUsd: clamp.capUsd } } : {};
|
|
169
|
-
return { ...heldOf(options), ...boundOf(options), ...kept };
|
|
170
|
-
};
|
|
171
|
-
/** A cancelled run's request, never sent. It waits for the cancel to reach the run (the request's abort), and then ends
|
|
172
|
-
* as aborted, as a request the cancel cut does: the turn, or a workflow's node, stops, never failing on the refusal.
|
|
173
|
-
* serve's cancel aborts a live run's turn and then stops its process, so the wait is short. Its bound, a minute, is a
|
|
174
|
-
* backstop for a process the cancel never reaches (an orphan of a server that died in between): it then ends as an
|
|
175
|
-
* error, and `markRefusal` lets the timeline skip pi's entry for it. */
|
|
176
|
-
const refuse = async (model, what, signal, markRefusal) => {
|
|
177
|
-
if (signal && !signal.aborted) {
|
|
178
|
-
await new Promise((resolve) => {
|
|
179
|
-
const timer = setTimeout(resolve, 60_000);
|
|
180
|
-
signal.addEventListener("abort", () => (clearTimeout(timer), resolve()), { once: true });
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
if (signal?.aborted)
|
|
184
|
-
return { ...failed(model, ""), stopReason: "aborted", errorMessage: undefined };
|
|
185
|
-
if (markRefusal)
|
|
186
|
-
await mark(REQUEST_REFUSED).catch(() => undefined);
|
|
187
|
-
return failed(model, `the run was cancelled: the ${what} was not sent`);
|
|
188
|
-
};
|
|
189
|
-
const streamSimple = (model, context, options) => {
|
|
190
|
-
const outer = createAssistantMessageEventStream();
|
|
191
|
-
const refused = async () => {
|
|
192
|
-
const message = await refuse(model, "model request", options?.signal, true);
|
|
193
|
-
outer.push({ type: "error", reason: message.stopReason === "aborted" ? "aborted" : "error", error: message });
|
|
194
|
-
outer.end(message);
|
|
195
|
-
};
|
|
196
|
-
void (async () => {
|
|
197
|
-
if (cancelled())
|
|
198
|
-
return refused();
|
|
199
|
-
try {
|
|
200
|
-
await markStarted(startOf(options), { unsent: true });
|
|
201
|
-
}
|
|
202
|
-
catch (error) {
|
|
203
|
-
const message = failed(model, `${MARK_NOT_WRITTEN} (${reason(error)})`);
|
|
204
|
-
outer.push({ type: "error", reason: "error", error: message });
|
|
205
|
-
outer.end(message);
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
if (cancelled()) {
|
|
209
|
-
await mark(REQUEST_ENDED, { unsent: true }).catch(() => undefined);
|
|
210
|
-
return refused();
|
|
211
|
-
}
|
|
212
|
-
let result;
|
|
213
|
-
// Whether the provider's response had begun (an event came in) by the time the request ended.
|
|
214
|
-
let began = false;
|
|
215
|
-
// The stream's error event, passed on once the request's end is marked (below): the outer stream's result, which
|
|
216
|
-
// pi writes as the entry and the cap counts, settles on that event.
|
|
217
|
-
let failure;
|
|
218
|
-
// Its done event, held the same way: the stream's result settles on it too, so pi has an answer only once its mark
|
|
219
|
-
// that carries a hold (a lost or unpriced request's) is in the store, and its entry always comes after that mark.
|
|
220
|
-
let done;
|
|
221
|
-
// Marked lost before pi-ai tries again (watchingLost): its end mark is in the store before its answer can be.
|
|
222
|
-
let lostMarked = false;
|
|
223
|
-
// Its write refused: the request's end is not recorded, and the request fails saying so (END_NOT_WRITTEN).
|
|
224
|
-
let lostRefused;
|
|
225
|
-
const sent = watchingLost(options, async () => {
|
|
226
|
-
lostMarked = true;
|
|
227
|
-
lostRefused = await landed(mark(REQUEST_ENDED, { lost: true, ...heldOf(options) }));
|
|
228
|
-
});
|
|
229
|
-
try {
|
|
230
|
-
const inner = models.streamSimple(model, context, sent);
|
|
231
|
-
for await (const event of inner) {
|
|
232
|
-
if (event.type === "error") {
|
|
233
|
-
failure = event;
|
|
234
|
-
continue;
|
|
235
|
-
}
|
|
236
|
-
began = true;
|
|
237
|
-
if (event.type === "done") {
|
|
238
|
-
done = event;
|
|
239
|
-
continue;
|
|
240
|
-
}
|
|
241
|
-
outer.push(event);
|
|
242
|
-
}
|
|
243
|
-
result = await inner.result();
|
|
244
|
-
}
|
|
245
|
-
catch (error) {
|
|
246
|
-
// A stream that throws instead of ending on an error event still ends the request, as an error, never a hang.
|
|
247
|
-
result = failed(model, `the model request failed: ${reason(error)}`);
|
|
248
|
-
failure = { type: "error", reason: "error", error: result };
|
|
249
|
-
}
|
|
250
|
-
// An answer or a failure gets pi's own entry; an abort may get none, so its end is marked, with the provider's id
|
|
251
|
-
// for the request when it named one before the cut (the request joins its record by it). A store that cannot
|
|
252
|
-
// take the mark leaves the request unended, which the timeline also reads as cut, at an unknown cost: the
|
|
253
|
-
// conservative reading, so the failure is swallowed here.
|
|
254
|
-
let ending;
|
|
255
|
-
// Whether `ending` carries the hold a later process must read (an error's or a lost request's): it is in the store
|
|
256
|
-
// before pi has the result, however long the store takes, and a refused one fails the request loudly. A stop's
|
|
257
|
-
// mark keeps its bound: without it the request stays open, read at its hold, which is never less.
|
|
258
|
-
let holding = false;
|
|
259
|
-
// An answer whose cost cannot be read counts at its hold (the booking rule), in this process and in a later one: its
|
|
260
|
-
// end mark carries the hold (`unpriced`), in the store before pi has the answer, as a lost request's does.
|
|
261
|
-
const total = result.usage?.cost?.total;
|
|
262
|
-
const unpriced = result.stopReason !== "error" && result.stopReason !== "aborted" && !(typeof total === "number" && Number.isFinite(total)) && Object.keys(heldOf(options)).length > 0;
|
|
263
|
-
// A request marked lost before its retry was sent has its end in the store already, at its hold: nothing more.
|
|
264
|
-
if (lostMarked) {
|
|
265
|
-
if (result.stopReason === "error" && failure?.type === "error")
|
|
266
|
-
markResponseBegan(failure.error);
|
|
267
|
-
}
|
|
268
|
-
else if (result.stopReason === "aborted") {
|
|
269
|
-
// Reached only with no lost attempt: watchingLost sets `lostMarked` synchronously, before it awaits the mark, the
|
|
270
|
-
// moment an attempt is lost, and that mark lands before pi-ai's retry sleep, so a stop in the sleep finds it set.
|
|
271
|
-
const responseId = result.responseId;
|
|
272
|
-
ending = mark(REQUEST_ENDED, { ...(typeof responseId === "string" ? { responseId } : {}), ...stoppedOf(options, result) });
|
|
273
|
-
}
|
|
274
|
-
else if (result.stopReason === "error" && (began || lostOf(options, result))) {
|
|
275
|
-
// An error after its response began (an overload part way, a reset): its usage is what had streamed, not what the
|
|
276
|
-
// provider can have billed. Its message is recorded (markResponseBegan), which the cap counts at its hold in this
|
|
277
|
-
// process, and its end is marked with the hold (`error`), which the spend read back counts. One before any event
|
|
278
|
-
// (a 429, a 5xx) is refused unbilled, and counts at its own usage.
|
|
279
|
-
markResponseBegan(result);
|
|
280
|
-
if (failure?.type === "error")
|
|
281
|
-
markResponseBegan(failure.error);
|
|
282
|
-
ending = mark(REQUEST_ENDED, { error: true, ...heldOf(options) });
|
|
283
|
-
holding = true;
|
|
284
|
-
}
|
|
285
|
-
else if (unpriced) {
|
|
286
|
-
ending = mark(REQUEST_ENDED, { unpriced: true, ...heldOf(options) });
|
|
287
|
-
holding = true;
|
|
288
|
-
}
|
|
289
|
-
// The end mark goes in before pi has the result, so the store holds it before pi's entry; a store that has not taken
|
|
290
|
-
// it in END_MARK_WAIT_MS does not hold the turn. One that carries a hold and is not in the store by then (refused, or
|
|
291
|
-
// still being written) fails the request loudly (END_NOT_WRITTEN): pi's entry for it then leaves its start open, read
|
|
292
|
-
// at its hold (spentIn), so no store's pace stands between a crash and a request read at less than it can have cost.
|
|
293
|
-
// A stop's mark needs none of that: without it the request reads as cut, at its hold, which is never less.
|
|
294
|
-
let unrecorded = lostRefused;
|
|
295
|
-
if (ending && holding)
|
|
296
|
-
unrecorded = await landed(ending);
|
|
297
|
-
else if (ending)
|
|
298
|
-
await within(ending.catch(() => undefined), STOP_MARK_WAIT_MS);
|
|
299
|
-
if (unrecorded !== undefined && result.stopReason !== "aborted") {
|
|
300
|
-
result = failed(model, `${END_NOT_WRITTEN} (${reason(unrecorded)})`);
|
|
301
|
-
failure = { type: "error", reason: "error", error: result };
|
|
302
|
-
}
|
|
303
|
-
// A request whose end could not be recorded fails loudly (failure, above) in place of its answer.
|
|
304
|
-
if (failure)
|
|
305
|
-
outer.push(failure);
|
|
306
|
-
else if (done)
|
|
307
|
-
outer.push(done);
|
|
308
|
-
outer.end(result);
|
|
309
|
-
})();
|
|
310
|
-
return outer;
|
|
311
|
-
};
|
|
312
|
-
const completeSimple = async (model, context, options) => {
|
|
313
|
-
const side = sideRequests.get(context);
|
|
314
|
-
// Its label, and an id of its own: such requests run inside tool calls, which pi runs side by side, so its marks name
|
|
315
|
-
// it (bounds.ts closes its bound by it).
|
|
316
|
-
const COMPACTION = { request: side?.label ?? "compaction", requestId: randomUUID() };
|
|
317
|
-
const what = side ? `${side.label} request` : "summary request";
|
|
318
|
-
// A cancelled run sends no summary either; pi writes no entry for a failed summary, so nothing needs marking.
|
|
319
|
-
if (cancelled())
|
|
320
|
-
return refuse(model, what, options?.signal, false);
|
|
321
|
-
try {
|
|
322
|
-
await markStarted({ ...COMPACTION, ...heldOf(options), ...boundOf(options) }, { ...COMPACTION, unsent: true });
|
|
323
|
-
}
|
|
324
|
-
catch (error) {
|
|
325
|
-
return failed(model, `${MARK_NOT_WRITTEN} (${reason(error)})`);
|
|
326
|
-
}
|
|
327
|
-
if (cancelled()) {
|
|
328
|
-
await mark(REQUEST_ENDED, { ...COMPACTION, unsent: true }).catch(() => undefined);
|
|
329
|
-
return refuse(model, what, options?.signal, false);
|
|
330
|
-
}
|
|
331
|
-
let result;
|
|
332
|
-
try {
|
|
333
|
-
result = await models.completeSimple(model, context, options);
|
|
334
|
-
}
|
|
335
|
-
catch (error) {
|
|
336
|
-
await mark(REQUEST_ENDED, { ...COMPACTION, ...heldOf(options) }).catch(() => undefined);
|
|
337
|
-
throw error;
|
|
338
|
-
}
|
|
339
|
-
// Its cost closes it; an abort, as for a generation's request, closes it unpriced. A store that cannot take either
|
|
340
|
-
// leaves it open, which reads as cut, at an unknown cost.
|
|
341
|
-
if (result.stopReason === "aborted")
|
|
342
|
-
await mark(REQUEST_ENDED, { ...COMPACTION, ...stoppedOf(options, result) }).catch(() => undefined);
|
|
343
|
-
else {
|
|
344
|
-
const total = result.usage?.cost?.total;
|
|
345
|
-
// A side request's fee is its cost too, on the answer as well: the cap counts the answer's total (cap.ts).
|
|
346
|
-
const usd = typeof total === "number" ? total + (side?.feeUsd() ?? 0) : null;
|
|
347
|
-
if (side && usd !== null)
|
|
348
|
-
result.usage.cost.total = usd;
|
|
349
|
-
// Its tokens, in pi's shape, when pi counted any: zeros are no count.
|
|
350
|
-
const u = result.usage;
|
|
351
|
-
const usage = u && u.input + u.output + u.cacheRead + u.cacheWrite > 0 ? { input: u.input, output: u.output, cacheRead: u.cacheRead, cacheWrite: u.cacheWrite } : undefined;
|
|
352
|
-
// One that ended on an error after its response began (it holds content or a token count; a summary has no events
|
|
353
|
-
// to read) counts at its hold: it says so, and carries it. One before any of that counts at its own usage.
|
|
354
|
-
const began = result.stopReason === "error" && (usage !== undefined || result.content.length > 0);
|
|
355
|
-
if (began)
|
|
356
|
-
markResponseBegan(result);
|
|
357
|
-
// Failed before any response, it counts at its hold too unless it provably never left (piEnded): the same figure the
|
|
358
|
-
// cap books in this process, which a later process reads from this mark.
|
|
359
|
-
// An answer whose cost cannot be read counts at its hold too (the booking rule), which its mark carries.
|
|
360
|
-
const failure = began || lostOf(options, result) ? { failed: true, ...heldOf(options) } : result.stopReason !== "error" && !(typeof usd === "number" && Number.isFinite(usd)) && Object.keys(heldOf(options)).length > 0 ? { unpriced: true, ...heldOf(options) } : {};
|
|
361
|
-
await mark(REQUEST_COST, usage ? { ...COMPACTION, usd, usage: usage, ...failure } : { ...COMPACTION, usd, ...failure }).catch(() => undefined);
|
|
362
|
-
}
|
|
363
|
-
return result;
|
|
364
|
-
};
|
|
365
|
-
const proxy = new Proxy(models, {
|
|
366
|
-
get: (target, key) => {
|
|
367
|
-
if (key === "streamSimple")
|
|
368
|
-
return streamSimple;
|
|
369
|
-
if (key === "completeSimple")
|
|
370
|
-
return completeSimple;
|
|
371
|
-
const value = Reflect.get(target, key, target);
|
|
372
|
-
return typeof value === "function" ? value.bind(target) : value;
|
|
373
|
-
},
|
|
374
|
-
});
|
|
375
|
-
// The start mark of a generation request the cap refused (capModels), which never left: pi's error entry answers it. A
|
|
376
|
-
// refusal for room keeps its figures on it (what the cap counted as taken, and the least the request needed).
|
|
377
|
-
const refused = (figures) => mark(REQUEST_STARTED, { [CAP_REFUSED_FIELD]: true, ...refusalFields(figures) });
|
|
378
|
-
// The wrap-up's row (cap.ts capModels' `wrapUp`): what the run's model was told near its cap, on the root.
|
|
379
|
-
const wrapUp = (data) => void mark(CAP_WRAPUP, data).catch(() => undefined);
|
|
380
|
-
return { models: proxy, open, refused, wrapUp };
|
|
381
|
-
}
|