@jangts/agent-core 0.1.1 → 0.2.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/CHANGELOG.md +18 -0
- package/development/docs/api/reference.md +1907 -1874
- package/development/docs/api/support.md +27 -0
- package/development/skills/agent-core-app/scripts/create-app.mjs +267 -70
- package/development/skills/agent-core-app/scripts/lib/knowledge.mjs +40 -15
- package/development/skills/agent-core-app/scripts/lib/source.mjs +30 -2
- package/development/skills/agent-core-app/scripts/plan.mjs +37 -12
- package/development/skills/agent-core-app/scripts/prepare-source.mjs +61 -10
- package/development/skills/agent-core-app/scripts/resources.mjs +45 -17
- package/development/skills/agent-core-app/scripts/validate-knowledge.mjs +18 -1
- package/development/skills/agent-core-app/templates/catalog.json +36 -22
- package/dist/cjs/capabilities/index.js +13 -0
- package/dist/cjs/capabilities/invocation.js +80 -10
- package/dist/cjs/capabilities/model.js +126 -13
- package/dist/cjs/capabilities/tool-runtime.js +64 -12
- package/dist/cjs/capabilities/tools.js +266 -31
- package/dist/cjs/context/assembly.js +46 -5
- package/dist/cjs/context/audit.js +31 -6
- package/dist/cjs/context/fragments.js +54 -11
- package/dist/cjs/context/index.js +12 -0
- package/dist/cjs/context/journal.js +84 -7
- package/dist/cjs/context/plan.js +30 -2
- package/dist/cjs/diagnostics/catalog.js +11 -3
- package/dist/cjs/diagnostics/index.js +149 -29
- package/dist/cjs/diagnostics/sub-flow.js +240 -40
- package/dist/cjs/errors.js +99 -17
- package/dist/cjs/execution/branch.js +20 -4
- package/dist/cjs/execution/engine.js +470 -56
- package/dist/cjs/execution/events.js +136 -18
- package/dist/cjs/execution/index.js +7 -0
- package/dist/cjs/execution/reporting.js +75 -6
- package/dist/cjs/execution/state.js +161 -22
- package/dist/cjs/execution/workflow.js +224 -32
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/kits/aggregation.js +45 -3
- package/dist/cjs/kits/answering/batch.js +23 -4
- package/dist/cjs/kits/answering/check-result.js +31 -8
- package/dist/cjs/kits/answering/checks.js +24 -3
- package/dist/cjs/kits/answering/flow.js +192 -36
- package/dist/cjs/kits/answering/index.js +19 -1
- package/dist/cjs/kits/answering/tasks.js +88 -13
- package/dist/cjs/kits/artifacts.js +36 -4
- package/dist/cjs/kits/candidates.js +47 -5
- package/dist/cjs/kits/delivery.js +40 -5
- package/dist/cjs/kits/durable.js +155 -31
- package/dist/cjs/kits/evidence.js +66 -15
- package/dist/cjs/kits/family.js +152 -22
- package/dist/cjs/kits/index.js +12 -1
- package/dist/cjs/kits/inference.js +104 -16
- package/dist/cjs/kits/intents/catalog.js +67 -16
- package/dist/cjs/kits/intents/index.js +11 -0
- package/dist/cjs/kits/intents/registry.js +188 -43
- package/dist/cjs/kits/intents/scopes.js +215 -46
- package/dist/cjs/kits/interaction/command.js +140 -36
- package/dist/cjs/kits/interaction/commands.js +86 -11
- package/dist/cjs/kits/interaction/handlers.js +27 -10
- package/dist/cjs/kits/interaction/index.js +10 -0
- package/dist/cjs/kits/interaction/transfer.js +80 -14
- package/dist/cjs/kits/knowledge/catalog.js +62 -11
- package/dist/cjs/kits/knowledge/index.js +16 -1
- package/dist/cjs/kits/knowledge/publication.js +99 -23
- package/dist/cjs/kits/knowledge/sections.js +33 -4
- package/dist/cjs/kits/normalization.js +51 -7
- package/dist/cjs/kits/policies.js +79 -15
- package/dist/cjs/kits/presentation/events.js +54 -10
- package/dist/cjs/kits/presentation/index.js +24 -1
- package/dist/cjs/kits/presentation/outputs.js +176 -38
- package/dist/cjs/kits/presentation/templates.js +33 -5
- package/dist/cjs/kits/presentation/variants.js +31 -2
- package/dist/cjs/kits/projection.js +63 -8
- package/dist/cjs/kits/rank-fusion.js +27 -4
- package/dist/cjs/kits/reflection-log-source.js +89 -16
- package/dist/cjs/kits/reflection.js +234 -60
- package/dist/cjs/kits/retrieval.js +98 -18
- package/dist/cjs/kits/search.js +180 -27
- package/dist/cjs/kits/text-skills.js +104 -19
- package/dist/cjs/kits/tools.js +415 -74
- package/dist/cjs/kits/vector-search.js +50 -16
- package/dist/cjs/mock/node.js +7 -1
- package/dist/cjs/mock/web.js +7 -1
- package/dist/cjs/mock.js +7 -1
- package/dist/cjs/model/binding.js +68 -17
- package/dist/cjs/model/call-budget.js +45 -4
- package/dist/cjs/model/decision-retry.js +19 -1
- package/dist/cjs/model/defaults.js +27 -4
- package/dist/cjs/model/index.js +7 -0
- package/dist/cjs/model/interactions/arguments.js +36 -1
- package/dist/cjs/model/interactions/json-object.js +48 -12
- package/dist/cjs/model/interactions/native-functions.js +52 -10
- package/dist/cjs/model/message-diagnostics.js +17 -4
- package/dist/cjs/model/options.js +9 -1
- package/dist/cjs/model/preparation.js +86 -12
- package/dist/cjs/model/runner.js +413 -79
- package/dist/cjs/model/selection-transport.js +48 -8
- package/dist/cjs/model/settings.js +67 -13
- package/dist/cjs/model/token-budget.js +69 -11
- package/dist/cjs/model/transports.js +59 -6
- package/dist/cjs/structs/common.js +11 -0
- package/dist/cjs/structs/debug.js +34 -5
- package/dist/cjs/structs/errors.js +26 -1
- package/dist/cjs/structs/index.js +11 -1
- package/dist/cjs/structs/kits/durable.js +7 -0
- package/dist/cjs/structs/kits/family.js +1 -0
- package/dist/cjs/structs/kits/interaction.js +8 -1
- package/dist/cjs/structs/kits/knowledge-publication.js +1 -0
- package/dist/cjs/structs/kits/reflection.js +18 -3
- package/dist/cjs/structs/model/chat-provider.js +6 -0
- package/dist/cjs/structs/support/model/completion.js +2 -0
- package/dist/cjs/structs/support/model/messages.js +2 -0
- package/dist/cjs/support/capabilities.js +43 -6
- package/dist/cjs/support/context/assembly.js +88 -16
- package/dist/cjs/support/context/compression.js +48 -6
- package/dist/cjs/support/context/fragments.js +6 -0
- package/dist/cjs/support/context/history.js +36 -1
- package/dist/cjs/support/context/index.js +11 -0
- package/dist/cjs/support/context/nodes.js +164 -35
- package/dist/cjs/support/context/processing.js +319 -61
- package/dist/cjs/support/context/selection-policy.js +98 -21
- package/dist/cjs/support/context/selection.js +61 -10
- package/dist/cjs/support/context/snapshot.js +29 -7
- package/dist/cjs/support/context/strategies.js +120 -28
- package/dist/cjs/support/index.js +9 -1
- package/dist/cjs/support/model/capabilities.js +20 -1
- package/dist/cjs/support/model/chat-completions.js +92 -20
- package/dist/cjs/support/model/children.js +34 -6
- package/dist/cjs/support/model/compatible-provider.js +189 -59
- package/dist/cjs/support/model/completion/calls.js +61 -0
- package/dist/cjs/support/model/completion/default.js +32 -0
- package/dist/cjs/support/model/completion/index.js +28 -0
- package/dist/cjs/support/model/completion/session.js +102 -0
- package/dist/cjs/support/model/index.js +12 -0
- package/dist/cjs/support/model/interactions/index.js +12 -1
- package/dist/cjs/support/model/interactions/json-object.js +6 -0
- package/dist/cjs/support/model/interactions/native-functions.js +6 -0
- package/dist/cjs/support/model/observer.js +32 -6
- package/dist/cjs/support/model/preview-rules.js +66 -16
- package/dist/cjs/support/model/reply-preview.js +77 -17
- package/dist/cjs/support/model/stage-transport.js +33 -7
- package/dist/cjs/support/model/strategies.js +75 -14
- package/dist/cjs/support/model/transports.js +53 -11
- package/dist/cjs/support/registration.js +49 -7
- package/dist/cjs/support/schema.js +46 -4
- package/dist/cjs/support/stream-events.js +60 -8
- package/dist/cjs/support/tool-runtime.js +6 -0
- package/dist/cjs/support/tools.js +79 -10
- package/dist/cjs/support/transport/index.js +7 -0
- package/dist/cjs/support/transport/json.js +28 -4
- package/dist/cjs/support/transport/sse.js +25 -2
- package/dist/cjs/utils/break-observer.js +20 -2
- package/dist/cjs/utils/cancellation.js +77 -9
- package/dist/cjs/utils/catalog.js +19 -4
- package/dist/cjs/utils/debug.js +14 -2
- package/dist/cjs/utils/index.js +8 -0
- package/dist/cjs/utils/interaction-definition.js +26 -6
- package/dist/cjs/utils/invocation.js +42 -4
- package/dist/cjs/utils/model-protocol.js +20 -1
- package/dist/cjs/utils/object.js +16 -0
- package/dist/cjs/utils/observer.js +22 -2
- package/dist/cjs/utils/protocol-registry.js +47 -4
- package/dist/cjs/utils/query-key.js +21 -2
- package/dist/cjs/utils/registration.js +18 -2
- package/dist/cjs/utils/rules.js +39 -11
- package/dist/cjs/utils/schema-adapter.js +25 -2
- package/dist/cjs/utils/synchronous.js +64 -7
- package/dist/cjs/utils/text.js +15 -1
- package/dist/esm/capabilities/index.d.ts +13 -0
- package/dist/esm/capabilities/index.js +13 -0
- package/dist/esm/capabilities/invocation.d.ts +16 -2
- package/dist/esm/capabilities/invocation.js +80 -10
- package/dist/esm/capabilities/model.d.ts +68 -1
- package/dist/esm/capabilities/model.js +126 -13
- package/dist/esm/capabilities/tool-runtime.d.ts +11 -0
- package/dist/esm/capabilities/tool-runtime.js +64 -12
- package/dist/esm/capabilities/tools.d.ts +96 -3
- package/dist/esm/capabilities/tools.js +267 -32
- package/dist/esm/context/assembly.d.ts +13 -2
- package/dist/esm/context/assembly.js +46 -5
- package/dist/esm/context/audit.d.ts +23 -1
- package/dist/esm/context/audit.js +31 -6
- package/dist/esm/context/fragments.d.ts +37 -3
- package/dist/esm/context/fragments.js +54 -11
- package/dist/esm/context/index.d.ts +12 -0
- package/dist/esm/context/index.js +12 -0
- package/dist/esm/context/journal.d.ts +32 -1
- package/dist/esm/context/journal.js +84 -7
- package/dist/esm/context/plan.d.ts +11 -1
- package/dist/esm/context/plan.js +30 -2
- package/dist/esm/diagnostics/catalog.js +11 -3
- package/dist/esm/diagnostics/index.d.ts +30 -2
- package/dist/esm/diagnostics/index.js +149 -29
- package/dist/esm/diagnostics/sub-flow.d.ts +41 -3
- package/dist/esm/diagnostics/sub-flow.js +240 -40
- package/dist/esm/errors.d.ts +22 -2
- package/dist/esm/errors.js +99 -17
- package/dist/esm/execution/branch.d.ts +19 -1
- package/dist/esm/execution/branch.js +20 -4
- package/dist/esm/execution/engine.d.ts +291 -5
- package/dist/esm/execution/engine.js +471 -57
- package/dist/esm/execution/events.d.ts +16 -2
- package/dist/esm/execution/events.js +136 -18
- package/dist/esm/execution/index.d.ts +7 -0
- package/dist/esm/execution/index.js +7 -0
- package/dist/esm/execution/reporting.d.ts +49 -1
- package/dist/esm/execution/reporting.js +75 -6
- package/dist/esm/execution/state.d.ts +126 -10
- package/dist/esm/execution/state.js +161 -22
- package/dist/esm/execution/workflow.d.ts +121 -3
- package/dist/esm/execution/workflow.js +224 -32
- package/dist/esm/index.d.ts +17 -1
- package/dist/esm/index.js +18 -0
- package/dist/esm/kits/aggregation.d.ts +34 -2
- package/dist/esm/kits/aggregation.js +45 -3
- package/dist/esm/kits/answering/batch.d.ts +17 -0
- package/dist/esm/kits/answering/batch.js +23 -4
- package/dist/esm/kits/answering/check-result.d.ts +18 -1
- package/dist/esm/kits/answering/check-result.js +31 -8
- package/dist/esm/kits/answering/checks.d.ts +11 -0
- package/dist/esm/kits/answering/checks.js +24 -3
- package/dist/esm/kits/answering/flow.d.ts +25 -2
- package/dist/esm/kits/answering/flow.js +192 -36
- package/dist/esm/kits/answering/index.d.ts +12 -0
- package/dist/esm/kits/answering/index.js +19 -1
- package/dist/esm/kits/answering/tasks.d.ts +32 -1
- package/dist/esm/kits/answering/tasks.js +88 -13
- package/dist/esm/kits/artifacts.d.ts +11 -0
- package/dist/esm/kits/artifacts.js +36 -4
- package/dist/esm/kits/candidates.d.ts +23 -1
- package/dist/esm/kits/candidates.js +47 -5
- package/dist/esm/kits/delivery.d.ts +34 -0
- package/dist/esm/kits/delivery.js +40 -5
- package/dist/esm/kits/durable.d.ts +19 -0
- package/dist/esm/kits/durable.js +155 -31
- package/dist/esm/kits/evidence.d.ts +28 -5
- package/dist/esm/kits/evidence.js +66 -15
- package/dist/esm/kits/family.d.ts +17 -1
- package/dist/esm/kits/family.js +152 -22
- package/dist/esm/kits/index.d.ts +12 -1
- package/dist/esm/kits/index.js +12 -1
- package/dist/esm/kits/inference.d.ts +64 -0
- package/dist/esm/kits/inference.js +104 -16
- package/dist/esm/kits/intents/catalog.d.ts +13 -1
- package/dist/esm/kits/intents/catalog.js +67 -16
- package/dist/esm/kits/intents/index.d.ts +11 -0
- package/dist/esm/kits/intents/index.js +11 -0
- package/dist/esm/kits/intents/registry.d.ts +61 -3
- package/dist/esm/kits/intents/registry.js +188 -43
- package/dist/esm/kits/intents/scopes.d.ts +73 -3
- package/dist/esm/kits/intents/scopes.js +215 -46
- package/dist/esm/kits/interaction/command.d.ts +38 -4
- package/dist/esm/kits/interaction/command.js +140 -36
- package/dist/esm/kits/interaction/commands.d.ts +66 -2
- package/dist/esm/kits/interaction/commands.js +86 -11
- package/dist/esm/kits/interaction/handlers.d.ts +19 -2
- package/dist/esm/kits/interaction/handlers.js +27 -10
- package/dist/esm/kits/interaction/index.d.ts +10 -0
- package/dist/esm/kits/interaction/index.js +10 -0
- package/dist/esm/kits/interaction/transfer.d.ts +39 -2
- package/dist/esm/kits/interaction/transfer.js +80 -14
- package/dist/esm/kits/knowledge/catalog.d.ts +13 -0
- package/dist/esm/kits/knowledge/catalog.js +62 -11
- package/dist/esm/kits/knowledge/index.d.ts +11 -0
- package/dist/esm/kits/knowledge/index.js +16 -1
- package/dist/esm/kits/knowledge/publication.d.ts +15 -1
- package/dist/esm/kits/knowledge/publication.js +99 -23
- package/dist/esm/kits/knowledge/sections.d.ts +31 -1
- package/dist/esm/kits/knowledge/sections.js +33 -4
- package/dist/esm/kits/normalization.d.ts +25 -2
- package/dist/esm/kits/normalization.js +51 -7
- package/dist/esm/kits/policies.d.ts +26 -3
- package/dist/esm/kits/policies.js +79 -15
- package/dist/esm/kits/presentation/events.d.ts +51 -4
- package/dist/esm/kits/presentation/events.js +54 -10
- package/dist/esm/kits/presentation/index.d.ts +12 -0
- package/dist/esm/kits/presentation/index.js +25 -2
- package/dist/esm/kits/presentation/outputs.d.ts +30 -1
- package/dist/esm/kits/presentation/outputs.js +176 -38
- package/dist/esm/kits/presentation/templates.d.ts +32 -3
- package/dist/esm/kits/presentation/templates.js +33 -5
- package/dist/esm/kits/presentation/variants.d.ts +29 -0
- package/dist/esm/kits/presentation/variants.js +31 -2
- package/dist/esm/kits/projection.d.ts +42 -5
- package/dist/esm/kits/projection.js +63 -8
- package/dist/esm/kits/rank-fusion.d.ts +21 -1
- package/dist/esm/kits/rank-fusion.js +28 -5
- package/dist/esm/kits/reflection-log-source.d.ts +28 -1
- package/dist/esm/kits/reflection-log-source.js +89 -16
- package/dist/esm/kits/reflection.d.ts +17 -1
- package/dist/esm/kits/reflection.js +235 -61
- package/dist/esm/kits/retrieval.d.ts +18 -2
- package/dist/esm/kits/retrieval.js +98 -18
- package/dist/esm/kits/search.d.ts +56 -3
- package/dist/esm/kits/search.js +180 -27
- package/dist/esm/kits/text-skills.d.ts +16 -2
- package/dist/esm/kits/text-skills.js +104 -19
- package/dist/esm/kits/tools.d.ts +122 -2
- package/dist/esm/kits/tools.js +416 -74
- package/dist/esm/kits/vector-search.d.ts +47 -1
- package/dist/esm/kits/vector-search.js +51 -17
- package/dist/esm/mock/node.d.ts +7 -0
- package/dist/esm/mock/node.js +7 -1
- package/dist/esm/mock/web.d.ts +7 -0
- package/dist/esm/mock/web.js +7 -1
- package/dist/esm/mock.d.ts +7 -0
- package/dist/esm/mock.js +7 -1
- package/dist/esm/model/binding.d.ts +32 -3
- package/dist/esm/model/binding.js +68 -17
- package/dist/esm/model/call-budget.d.ts +30 -2
- package/dist/esm/model/call-budget.js +45 -4
- package/dist/esm/model/decision-retry.d.ts +10 -1
- package/dist/esm/model/decision-retry.js +19 -1
- package/dist/esm/model/defaults.d.ts +13 -2
- package/dist/esm/model/defaults.js +27 -4
- package/dist/esm/model/index.d.ts +7 -0
- package/dist/esm/model/index.js +7 -0
- package/dist/esm/model/interactions/arguments.d.ts +27 -0
- package/dist/esm/model/interactions/arguments.js +36 -1
- package/dist/esm/model/interactions/json-object.d.ts +7 -1
- package/dist/esm/model/interactions/json-object.js +48 -12
- package/dist/esm/model/interactions/native-functions.d.ts +6 -0
- package/dist/esm/model/interactions/native-functions.js +52 -10
- package/dist/esm/model/message-diagnostics.d.ts +17 -1
- package/dist/esm/model/message-diagnostics.js +17 -4
- package/dist/esm/model/options.d.ts +18 -1
- package/dist/esm/model/options.js +9 -1
- package/dist/esm/model/preparation.d.ts +30 -2
- package/dist/esm/model/preparation.js +86 -12
- package/dist/esm/model/runner.d.ts +129 -5
- package/dist/esm/model/runner.js +414 -80
- package/dist/esm/model/selection-transport.d.ts +14 -1
- package/dist/esm/model/selection-transport.js +48 -8
- package/dist/esm/model/settings.d.ts +11 -0
- package/dist/esm/model/settings.js +67 -13
- package/dist/esm/model/token-budget.d.ts +59 -1
- package/dist/esm/model/token-budget.js +69 -11
- package/dist/esm/model/transports.d.ts +20 -2
- package/dist/esm/model/transports.js +59 -6
- package/dist/esm/structs/capabilities/invocation.d.ts +36 -0
- package/dist/esm/structs/capabilities/model.d.ts +27 -0
- package/dist/esm/structs/capabilities/tools.d.ts +38 -0
- package/dist/esm/structs/common.d.ts +62 -2
- package/dist/esm/structs/common.js +11 -0
- package/dist/esm/structs/context/fragments.d.ts +28 -0
- package/dist/esm/structs/context/journal.d.ts +28 -3
- package/dist/esm/structs/context/processing.d.ts +11 -0
- package/dist/esm/structs/debug.d.ts +58 -1
- package/dist/esm/structs/debug.js +34 -5
- package/dist/esm/structs/diagnostics.d.ts +119 -1
- package/dist/esm/structs/errors.d.ts +42 -1
- package/dist/esm/structs/errors.js +26 -1
- package/dist/esm/structs/execution/engine.d.ts +24 -1
- package/dist/esm/structs/execution/events.d.ts +12 -0
- package/dist/esm/structs/execution/workflow.d.ts +71 -0
- package/dist/esm/structs/index.d.ts +11 -1
- package/dist/esm/structs/index.js +11 -1
- package/dist/esm/structs/invocation.d.ts +30 -0
- package/dist/esm/structs/kits/aggregation.d.ts +12 -0
- package/dist/esm/structs/kits/answering/checks.d.ts +11 -0
- package/dist/esm/structs/kits/answering/flow.d.ts +35 -1
- package/dist/esm/structs/kits/answering/state.d.ts +21 -0
- package/dist/esm/structs/kits/answering/tasks.d.ts +21 -0
- package/dist/esm/structs/kits/artifacts.d.ts +14 -0
- package/dist/esm/structs/kits/candidates.d.ts +19 -0
- package/dist/esm/structs/kits/delivery.d.ts +22 -0
- package/dist/esm/structs/kits/durable.d.ts +56 -3
- package/dist/esm/structs/kits/durable.js +7 -0
- package/dist/esm/structs/kits/evidence.d.ts +23 -0
- package/dist/esm/structs/kits/family.d.ts +49 -1
- package/dist/esm/structs/kits/family.js +1 -0
- package/dist/esm/structs/kits/intents/catalog.d.ts +26 -0
- package/dist/esm/structs/kits/intents/registry.d.ts +34 -0
- package/dist/esm/structs/kits/intents/scopes.d.ts +36 -1
- package/dist/esm/structs/kits/interaction.d.ts +64 -0
- package/dist/esm/structs/kits/interaction.js +8 -1
- package/dist/esm/structs/kits/knowledge-publication.d.ts +43 -1
- package/dist/esm/structs/kits/knowledge-publication.js +1 -0
- package/dist/esm/structs/kits/knowledge.d.ts +39 -2
- package/dist/esm/structs/kits/normalization.d.ts +15 -0
- package/dist/esm/structs/kits/policies.d.ts +25 -1
- package/dist/esm/structs/kits/presentation/events.d.ts +28 -4
- package/dist/esm/structs/kits/presentation/outputs.d.ts +38 -1
- package/dist/esm/structs/kits/presentation/templates.d.ts +15 -0
- package/dist/esm/structs/kits/projection.d.ts +21 -0
- package/dist/esm/structs/kits/reflection.d.ts +61 -2
- package/dist/esm/structs/kits/reflection.js +18 -3
- package/dist/esm/structs/kits/retrieval.d.ts +38 -0
- package/dist/esm/structs/kits/search.d.ts +22 -0
- package/dist/esm/structs/kits/text-skills.d.ts +36 -0
- package/dist/esm/structs/kits/tools.d.ts +87 -0
- package/dist/esm/structs/model/chat-provider.d.ts +24 -1
- package/dist/esm/structs/model/chat-provider.js +6 -0
- package/dist/esm/structs/model/interactions.d.ts +38 -0
- package/dist/esm/structs/model/messages.d.ts +30 -0
- package/dist/esm/structs/model/runtime.d.ts +104 -6
- package/dist/esm/structs/support/capabilities.d.ts +19 -2
- package/dist/esm/structs/support/context-processing.d.ts +88 -9
- package/dist/esm/structs/support/context.d.ts +29 -1
- package/dist/esm/structs/support/model/completion.d.ts +50 -0
- package/dist/esm/structs/support/model/completion.js +1 -0
- package/dist/esm/structs/support/model/interactions.d.ts +13 -1
- package/dist/esm/structs/support/model/messages.d.ts +52 -0
- package/dist/esm/structs/support/model/messages.js +1 -0
- package/dist/esm/structs/support/model/provider-observer.d.ts +22 -1
- package/dist/esm/structs/support/model/provider.d.ts +71 -6
- package/dist/esm/structs/support/registration.d.ts +13 -0
- package/dist/esm/structs/support/schema.d.ts +14 -0
- package/dist/esm/structs/support/tools.d.ts +28 -0
- package/dist/esm/structs/support/transport/json.d.ts +16 -0
- package/dist/esm/structs/support/transport/sse.d.ts +15 -0
- package/dist/esm/structs/utils/rules.d.ts +19 -0
- package/dist/esm/support/capabilities.d.ts +25 -2
- package/dist/esm/support/capabilities.js +43 -6
- package/dist/esm/support/context/assembly.d.ts +35 -4
- package/dist/esm/support/context/assembly.js +88 -16
- package/dist/esm/support/context/compression.d.ts +17 -1
- package/dist/esm/support/context/compression.js +48 -6
- package/dist/esm/support/context/fragments.d.ts +6 -0
- package/dist/esm/support/context/fragments.js +6 -0
- package/dist/esm/support/context/history.d.ts +43 -0
- package/dist/esm/support/context/history.js +36 -1
- package/dist/esm/support/context/index.d.ts +13 -2
- package/dist/esm/support/context/index.js +12 -1
- package/dist/esm/support/context/nodes.d.ts +49 -3
- package/dist/esm/support/context/nodes.js +165 -36
- package/dist/esm/support/context/processing.d.ts +145 -1
- package/dist/esm/support/context/processing.js +320 -62
- package/dist/esm/support/context/selection-policy.d.ts +42 -2
- package/dist/esm/support/context/selection-policy.js +98 -21
- package/dist/esm/support/context/selection.d.ts +14 -1
- package/dist/esm/support/context/selection.js +61 -10
- package/dist/esm/support/context/snapshot.d.ts +23 -1
- package/dist/esm/support/context/snapshot.js +29 -7
- package/dist/esm/support/context/strategies.d.ts +35 -2
- package/dist/esm/support/context/strategies.js +120 -28
- package/dist/esm/support/index.d.ts +9 -1
- package/dist/esm/support/index.js +9 -1
- package/dist/esm/support/model/capabilities.d.ts +10 -0
- package/dist/esm/support/model/capabilities.js +20 -1
- package/dist/esm/support/model/chat-completions.d.ts +44 -15
- package/dist/esm/support/model/chat-completions.js +92 -20
- package/dist/esm/support/model/children.d.ts +32 -2
- package/dist/esm/support/model/children.js +34 -6
- package/dist/esm/support/model/compatible-provider.d.ts +29 -3
- package/dist/esm/support/model/compatible-provider.js +190 -60
- package/dist/esm/support/model/completion/calls.d.ts +40 -0
- package/dist/esm/support/model/completion/calls.js +58 -0
- package/dist/esm/support/model/completion/default.d.ts +10 -0
- package/dist/esm/support/model/completion/default.js +29 -0
- package/dist/esm/support/model/completion/index.d.ts +10 -0
- package/dist/esm/support/model/completion/index.js +22 -0
- package/dist/esm/support/model/completion/session.d.ts +11 -0
- package/dist/esm/support/model/completion/session.js +99 -0
- package/dist/esm/support/model/index.d.ts +14 -0
- package/dist/esm/support/model/index.js +12 -0
- package/dist/esm/support/model/interactions/index.d.ts +1 -1
- package/dist/esm/support/model/interactions/index.js +12 -1
- package/dist/esm/support/model/interactions/json-object.d.ts +6 -0
- package/dist/esm/support/model/interactions/json-object.js +6 -0
- package/dist/esm/support/model/interactions/native-functions.d.ts +6 -0
- package/dist/esm/support/model/interactions/native-functions.js +6 -0
- package/dist/esm/support/model/observer.d.ts +25 -2
- package/dist/esm/support/model/observer.js +32 -6
- package/dist/esm/support/model/preview-rules.d.ts +20 -2
- package/dist/esm/support/model/preview-rules.js +66 -16
- package/dist/esm/support/model/reply-preview.d.ts +15 -1
- package/dist/esm/support/model/reply-preview.js +77 -17
- package/dist/esm/support/model/stage-transport.d.ts +9 -1
- package/dist/esm/support/model/stage-transport.js +33 -7
- package/dist/esm/support/model/strategies.d.ts +24 -2
- package/dist/esm/support/model/strategies.js +75 -14
- package/dist/esm/support/model/transports.d.ts +17 -1
- package/dist/esm/support/model/transports.js +53 -11
- package/dist/esm/support/registration.d.ts +20 -1
- package/dist/esm/support/registration.js +49 -7
- package/dist/esm/support/schema.d.ts +31 -1
- package/dist/esm/support/schema.js +46 -4
- package/dist/esm/support/stream-events.d.ts +22 -1
- package/dist/esm/support/stream-events.js +60 -8
- package/dist/esm/support/tool-runtime.d.ts +6 -0
- package/dist/esm/support/tool-runtime.js +6 -0
- package/dist/esm/support/tools.d.ts +22 -0
- package/dist/esm/support/tools.js +79 -10
- package/dist/esm/support/transport/index.d.ts +7 -0
- package/dist/esm/support/transport/index.js +7 -0
- package/dist/esm/support/transport/json.d.ts +29 -2
- package/dist/esm/support/transport/json.js +28 -4
- package/dist/esm/support/transport/sse.d.ts +31 -1
- package/dist/esm/support/transport/sse.js +25 -2
- package/dist/esm/types.d.ts +9 -1
- package/dist/esm/utils/break-observer.d.ts +27 -2
- package/dist/esm/utils/break-observer.js +20 -2
- package/dist/esm/utils/cancellation.d.ts +56 -5
- package/dist/esm/utils/cancellation.js +77 -9
- package/dist/esm/utils/catalog.d.ts +20 -1
- package/dist/esm/utils/catalog.js +19 -4
- package/dist/esm/utils/debug.d.ts +20 -1
- package/dist/esm/utils/debug.js +14 -2
- package/dist/esm/utils/index.d.ts +8 -0
- package/dist/esm/utils/index.js +8 -0
- package/dist/esm/utils/interaction-definition.d.ts +18 -1
- package/dist/esm/utils/interaction-definition.js +26 -6
- package/dist/esm/utils/invocation.d.ts +36 -1
- package/dist/esm/utils/invocation.js +42 -4
- package/dist/esm/utils/model-protocol.d.ts +8 -0
- package/dist/esm/utils/model-protocol.js +20 -1
- package/dist/esm/utils/object.d.ts +16 -0
- package/dist/esm/utils/object.js +16 -0
- package/dist/esm/utils/observer.d.ts +19 -1
- package/dist/esm/utils/observer.js +22 -2
- package/dist/esm/utils/protocol-registry.d.ts +38 -1
- package/dist/esm/utils/protocol-registry.js +47 -4
- package/dist/esm/utils/query-key.d.ts +11 -1
- package/dist/esm/utils/query-key.js +21 -2
- package/dist/esm/utils/registration.d.ts +34 -2
- package/dist/esm/utils/registration.js +18 -2
- package/dist/esm/utils/rules.d.ts +81 -4
- package/dist/esm/utils/rules.js +39 -11
- package/dist/esm/utils/schema-adapter.d.ts +12 -1
- package/dist/esm/utils/schema-adapter.js +25 -2
- package/dist/esm/utils/synchronous.d.ts +60 -4
- package/dist/esm/utils/synchronous.js +64 -7
- package/dist/esm/utils/text.d.ts +15 -1
- package/dist/esm/utils/text.js +15 -1
- package/package.json +3 -2
package/dist/esm/model/runner.js
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model runner: the single path from prepared input to a validated result.
|
|
3
|
+
*
|
|
4
|
+
* This module is the model-facing protocol boundary. It owns, in strict order:
|
|
5
|
+
* cancellation, registration and budget checks, input measurement (characters, schema
|
|
6
|
+
* and tokens), budget charging, streaming through the transport's collector, then the
|
|
7
|
+
* journal entry, call trace and terminal event. Nothing here knows business prompts,
|
|
8
|
+
* stages or provider SDKs; those are registered by the App.
|
|
9
|
+
*
|
|
10
|
+
* Exports: `ModelRunner`, the re-exported `BudgetError`, and the runtime/config type
|
|
11
|
+
* re-exports at the bottom of the file.
|
|
12
|
+
*/
|
|
1
13
|
import { executionEvents } from '../execution/events.js';
|
|
2
14
|
import { ModelCallBudget, parentModelBudget } from './call-budget.js';
|
|
3
15
|
import { synchronous } from '../utils/synchronous.js';
|
|
@@ -5,7 +17,7 @@ import { awaitWithSignal, combineSignals, isTimeoutReason } from '../utils/cance
|
|
|
5
17
|
import { invocationId, invocationResult } from '../utils/invocation.js';
|
|
6
18
|
import { defaultTextTransport, modelRunnerDefaults } from './defaults.js';
|
|
7
19
|
import { breakWhile } from '../utils/rules.js';
|
|
8
|
-
import { compiledModelBinding, modelBindingId, snapshotModelBinding } from './binding.js';
|
|
20
|
+
import { compiledModelBinding, modelBindingId, snapshotModelBinding, } from './binding.js';
|
|
9
21
|
import { prepareModelTransport, preparedInputSize } from './preparation.js';
|
|
10
22
|
import { getErrorInfo, recordErrorContext } from '../errors.js';
|
|
11
23
|
import { modelInputTokenLimit, countModelInput, enforceModelInputTokens } from './token-budget.js';
|
|
@@ -18,19 +30,46 @@ import { ContextJournal } from '../context/journal.js';
|
|
|
18
30
|
import { commonPrefixLength } from '../utils/text.js';
|
|
19
31
|
import { modelProtocolError } from '../utils/model-protocol.js';
|
|
20
32
|
import { abortableStream } from '@jangts/streaming/abortable-stream';
|
|
33
|
+
// A context plan has no `messages` key yet; a built context carries already assembled ones.
|
|
21
34
|
const isContextPlan = (input) => !Array.isArray(input) && !('messages' in input);
|
|
22
35
|
const isMessageArray = (input) => Array.isArray(input);
|
|
36
|
+
/**
|
|
37
|
+
* Validate a requested completion size.
|
|
38
|
+
*
|
|
39
|
+
* @param tokens - Requested output tokens.
|
|
40
|
+
* @param stageId - Stage used for error context.
|
|
41
|
+
* @throws TypeError When `tokens` is not a positive safe integer.
|
|
42
|
+
*/
|
|
23
43
|
function validateOutputTokens(tokens, stageId) {
|
|
24
|
-
breakWhile(!Number.isSafeInteger(tokens) || tokens < 1, () => recordErrorContext(new TypeError('maxOutputTokens must be a positive safe integer'), {
|
|
44
|
+
breakWhile(!Number.isSafeInteger(tokens) || tokens < 1, () => recordErrorContext(new TypeError('maxOutputTokens must be a positive safe integer'), {
|
|
45
|
+
stageId,
|
|
46
|
+
codes: ['MODEL_CONFIG'],
|
|
47
|
+
isRetryable: false,
|
|
48
|
+
}));
|
|
25
49
|
}
|
|
26
50
|
export { BudgetError } from '../structs/errors.js';
|
|
27
|
-
/**
|
|
51
|
+
/**
|
|
52
|
+
* Execute registered model stages under one request's budgets and lifetime.
|
|
53
|
+
*
|
|
54
|
+
* Invariants: at most one call is in flight per runner; `callCount` and `journal` are
|
|
55
|
+
* local to the runner while `totalCallCount` includes descendants; every call is charged
|
|
56
|
+
* to the shared budget before I/O and every failure is annotated with its stage before it
|
|
57
|
+
* propagates. No business prompts, domain stages or provider SDKs are built into this
|
|
58
|
+
* runner.
|
|
59
|
+
*/
|
|
28
60
|
export class ModelRunner {
|
|
61
|
+
/** Record of parsed results for this runner; descendants keep their own journals. */
|
|
29
62
|
journal = new ContextJournal();
|
|
30
63
|
completedCallCount = 0;
|
|
31
64
|
activeCall;
|
|
32
|
-
|
|
33
|
-
get
|
|
65
|
+
/** Completed calls observed by this runner only. */
|
|
66
|
+
get callCount() {
|
|
67
|
+
return this.completedCallCount;
|
|
68
|
+
}
|
|
69
|
+
/** Detached copy of the most recent call trace, or undefined before the first call. */
|
|
70
|
+
get lastCall() {
|
|
71
|
+
return this.activeCall ? Object.freeze(structuredClone(this.activeCall)) : undefined;
|
|
72
|
+
}
|
|
34
73
|
priorInputs = new Map();
|
|
35
74
|
tokenBudget;
|
|
36
75
|
registeredTransports = new Map();
|
|
@@ -48,26 +87,60 @@ export class ModelRunner {
|
|
|
48
87
|
depth = 0;
|
|
49
88
|
inFlight = false;
|
|
50
89
|
requestGate;
|
|
51
|
-
|
|
90
|
+
/** Limits shared by every runner of one request; children only read them. */
|
|
91
|
+
get childLimits() {
|
|
92
|
+
return this.requestGate.limits;
|
|
93
|
+
}
|
|
52
94
|
/** Includes descendant attempts; local callCount and journal remain isolated. */
|
|
53
|
-
get totalCallCount() {
|
|
54
|
-
|
|
95
|
+
get totalCallCount() {
|
|
96
|
+
return this.budget.used;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Create a fresh runner for a nested stage.
|
|
100
|
+
*
|
|
101
|
+
* The child never inherits conversation, journal or sinks: only the provider, fixed
|
|
102
|
+
* limits, the parent budget and the child's own lifetime. All ancestors keep their
|
|
103
|
+
* budgets, and the child is charged through them, so nested work cannot overspend.
|
|
104
|
+
*
|
|
105
|
+
* @param options - Child id, call allowance, reserved calls, timeout and optional signals.
|
|
106
|
+
* @returns A runner sharing the request gate, with a budget derived from this one.
|
|
107
|
+
* @throws Error When the depth or child capacity is exhausted, a call is in flight,
|
|
108
|
+
* the settings are invalid, or the observer belongs to another run.
|
|
109
|
+
*/
|
|
55
110
|
createChild(options) {
|
|
56
111
|
breakWhile(this.depth >= this.requestGate.limits.maxDepth, 'Nested model children exceed the request depth limit');
|
|
57
112
|
breakWhile(this.inFlight, 'Cannot create a child while this model call is active');
|
|
58
113
|
breakWhile(this.requestGate.children >= this.requestGate.limits.maxChildren, 'Model child capacity exhausted');
|
|
59
|
-
breakWhile(typeof options.id !== 'string' ||
|
|
114
|
+
breakWhile(typeof options.id !== 'string' ||
|
|
115
|
+
!options.id.trim() ||
|
|
116
|
+
!Number.isSafeInteger(options.timeoutMs) ||
|
|
117
|
+
options.timeoutMs < 1 ||
|
|
118
|
+
options.timeoutMs > 2147483647, 'Invalid child execution settings');
|
|
60
119
|
const budget = this.budget.child(options.maxCalls, options.reserveCalls);
|
|
61
120
|
const diagnostics = options.diagnostics ?? this.ports.diagnostics;
|
|
62
|
-
breakWhile(!!this.ports.diagnostics?.identity?.runId &&
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
121
|
+
breakWhile(!!this.ports.diagnostics?.identity?.runId &&
|
|
122
|
+
!!diagnostics?.identity?.runId &&
|
|
123
|
+
this.ports.diagnostics.identity.runId !== diagnostics.identity.runId, 'Child observation belongs to another run');
|
|
124
|
+
const childOptions = {
|
|
125
|
+
provider: this.provider,
|
|
126
|
+
[modelBindingId]: this.modelId,
|
|
127
|
+
maxCalls: options.maxCalls,
|
|
128
|
+
callTimeoutMs: this.config.callTimeoutMs,
|
|
129
|
+
maxInputChars: this.maxInputChars,
|
|
130
|
+
maxOutputChars: this.config.maxOutputChars,
|
|
131
|
+
maxSchemaChars: this.ports.maxSchemaChars,
|
|
132
|
+
maxOutputTokens: this.maxOutputTokens,
|
|
133
|
+
tokenBudget: this.tokenBudget,
|
|
134
|
+
usage: this.config.usage,
|
|
135
|
+
transports: options.transports,
|
|
68
136
|
children: this.requestGate.limits,
|
|
69
|
-
execution: {
|
|
70
|
-
|
|
137
|
+
execution: {
|
|
138
|
+
signal: combineSignals(this.signal, AbortSignal.timeout(options.timeoutMs), options.signal),
|
|
139
|
+
diagnostics: diagnostics?.child(invocationId(), {
|
|
140
|
+
definitionKind: 'agent',
|
|
141
|
+
definitionId: options.id,
|
|
142
|
+
}),
|
|
143
|
+
},
|
|
71
144
|
};
|
|
72
145
|
const child = new ModelRunner(childOptions);
|
|
73
146
|
child.budget = budget;
|
|
@@ -76,9 +149,19 @@ export class ModelRunner {
|
|
|
76
149
|
child.requestGate = this.requestGate;
|
|
77
150
|
return child;
|
|
78
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Build a runner from App options, or adopt an already compiled binding.
|
|
154
|
+
*
|
|
155
|
+
* Compiling here rather than per call is what makes configuration immutable for the
|
|
156
|
+
* runner's lifetime. The text transport is always registered first, then App
|
|
157
|
+
* transports in declaration order.
|
|
158
|
+
*
|
|
159
|
+
* @param options - Runner options; may carry a compiled binding and an inherited budget.
|
|
160
|
+
* @throws TypeError When settings, child limits or a transport are invalid.
|
|
161
|
+
* @throws ValidationError When the token budget contract is incomplete.
|
|
162
|
+
*/
|
|
79
163
|
constructor(options) {
|
|
80
|
-
const resolved = options[compiledModelBinding]
|
|
81
|
-
?? snapshotModelBinding(options);
|
|
164
|
+
const resolved = options[compiledModelBinding] ?? snapshotModelBinding(options);
|
|
82
165
|
this.budget = new ModelCallBudget(resolved.config.maxCalls, options[parentModelBudget]);
|
|
83
166
|
this.requestGate = { active: 0, children: 0, limits: resolved.children };
|
|
84
167
|
this.provider = resolved.provider;
|
|
@@ -87,7 +170,11 @@ export class ModelRunner {
|
|
|
87
170
|
this.signal = options.execution?.signal ?? new AbortController().signal;
|
|
88
171
|
this.maxOutputTokens = resolved.maxOutputTokens;
|
|
89
172
|
this.maxInputChars = resolved.maxInputChars;
|
|
90
|
-
this.ports = Object.freeze({
|
|
173
|
+
this.ports = Object.freeze({
|
|
174
|
+
...resolved.ports,
|
|
175
|
+
diagnostics: executionEvents(options.execution) ??
|
|
176
|
+
(resolved.ports.onOutput ? createDiagnostics() : undefined),
|
|
177
|
+
});
|
|
91
178
|
this.execution = Object.freeze({ signal: this.signal, diagnostics: this.ports.diagnostics });
|
|
92
179
|
this.tokenBudget = resolved.tokenBudget;
|
|
93
180
|
this.transports.set(defaultTextTransport.id, defaultTextTransport);
|
|
@@ -96,7 +183,19 @@ export class ModelRunner {
|
|
|
96
183
|
this.transports.set(definition.id, definition);
|
|
97
184
|
}
|
|
98
185
|
}
|
|
99
|
-
|
|
186
|
+
/** Calls still available to this runner after the parent's reserve is applied. */
|
|
187
|
+
get remainingCalls() {
|
|
188
|
+
return this.budget.remaining;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Resolve the character limit for one input shape.
|
|
192
|
+
*
|
|
193
|
+
* A message array uses the runner limit; a built context or plan may declare a lower
|
|
194
|
+
* limit, and the stricter of the two wins so neither contract can be exceeded.
|
|
195
|
+
*
|
|
196
|
+
* @param input - Messages, built context or context plan.
|
|
197
|
+
* @returns The effective input character limit.
|
|
198
|
+
*/
|
|
100
199
|
inputLimit(input) {
|
|
101
200
|
if (isMessageArray(input))
|
|
102
201
|
return this.maxInputChars;
|
|
@@ -108,12 +207,32 @@ export class ModelRunner {
|
|
|
108
207
|
async emitOutput(event, signal) {
|
|
109
208
|
const sink = this.ports.onOutput;
|
|
110
209
|
signal.throwIfAborted();
|
|
111
|
-
await awaitWithSignal(Promise.resolve().then(() => {
|
|
210
|
+
await awaitWithSignal(Promise.resolve().then(() => {
|
|
211
|
+
signal.throwIfAborted();
|
|
212
|
+
return sink(event);
|
|
213
|
+
}), signal);
|
|
112
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* Publish and, when a sink is installed, deliver one lifecycle event.
|
|
217
|
+
*
|
|
218
|
+
* Delivery goes through the scope so observers see the event before the sink does;
|
|
219
|
+
* `consume` runs inside the delivery callback, after the abort check, so a cancelled
|
|
220
|
+
* call cannot mutate the collector.
|
|
221
|
+
*
|
|
222
|
+
* @param type - Lifecycle event name.
|
|
223
|
+
* @param payload - Lazily built payload; only evaluated when someone consumes it.
|
|
224
|
+
* @param signal - Signal that bounds publication and delivery.
|
|
225
|
+
* @param scope - Diagnostic scope owning the event and its consumers.
|
|
226
|
+
* @param consume - Optional collector update performed before delivery.
|
|
227
|
+
*/
|
|
113
228
|
async publishOutput(type, payload, signal, scope, consume) {
|
|
114
229
|
signal.throwIfAborted();
|
|
115
230
|
if (this.ports.onOutput)
|
|
116
|
-
await scope?.deliver(type, payload, event => {
|
|
231
|
+
await scope?.deliver(type, payload, (event) => {
|
|
232
|
+
signal.throwIfAborted();
|
|
233
|
+
consume?.();
|
|
234
|
+
return this.emitOutput(event, signal);
|
|
235
|
+
});
|
|
117
236
|
else {
|
|
118
237
|
scope?.emit(type, payload);
|
|
119
238
|
signal.throwIfAborted();
|
|
@@ -127,32 +246,69 @@ export class ModelRunner {
|
|
|
127
246
|
if (typeof stageId !== 'string' || !stageId.trim())
|
|
128
247
|
throw new TypeError('stageId must be a nonempty string');
|
|
129
248
|
validateOutputTokens(maxOutputTokens, stageId);
|
|
130
|
-
const transportId = request.transport === undefined
|
|
131
|
-
?
|
|
249
|
+
const transportId = request.transport === undefined
|
|
250
|
+
? defaultTextTransport.id
|
|
251
|
+
: typeof request.transport === 'string'
|
|
252
|
+
? request.transport
|
|
253
|
+
: this.registeredTransports.get(request.transport);
|
|
132
254
|
const transport = transportId === undefined ? undefined : this.transports.get(transportId);
|
|
133
255
|
if (!transport)
|
|
134
256
|
throw new TypeError('Prepare requires a registered model transport');
|
|
135
257
|
const prepared = prepareModelTransport(transport, stageId);
|
|
136
258
|
let state;
|
|
137
259
|
const runner = this;
|
|
138
|
-
const snapshot = Object.freeze({
|
|
139
|
-
|
|
140
|
-
|
|
260
|
+
const snapshot = Object.freeze({
|
|
261
|
+
...prepared,
|
|
262
|
+
stageId,
|
|
263
|
+
modelId: this.modelId,
|
|
264
|
+
transportId: transport.id,
|
|
265
|
+
maxOutputTokens,
|
|
266
|
+
get remainingCalls() {
|
|
267
|
+
return runner.remainingCalls;
|
|
268
|
+
},
|
|
269
|
+
/**
|
|
270
|
+
* Preflight measurement of one input shape.
|
|
271
|
+
*
|
|
272
|
+
* Measuring does not consume the prepared call and does not charge the budget; it
|
|
273
|
+
* only reports the sizes and whether all three budgets would accept the input.
|
|
274
|
+
*
|
|
275
|
+
* @param input - Prompt string, message array or built context.
|
|
276
|
+
* @returns Frozen character and token measurements with a `withinBudget` verdict.
|
|
277
|
+
* @throws TypeError When the input shape is invalid or the call is already used.
|
|
278
|
+
*/
|
|
141
279
|
measure(input) {
|
|
142
280
|
runner.signal.throwIfAborted();
|
|
143
281
|
if (state.used)
|
|
144
282
|
throw new TypeError('Prepared model call has already been used');
|
|
145
|
-
const base = typeof input === 'string'
|
|
283
|
+
const base = typeof input === 'string'
|
|
284
|
+
? [{ role: 'user', content: input }]
|
|
285
|
+
: isMessageArray(input)
|
|
286
|
+
? input
|
|
287
|
+
: input.messages;
|
|
146
288
|
const size = preparedInputSize(base, prepared);
|
|
147
289
|
const inputLimit = typeof input === 'string' ? runner.maxInputChars : runner.inputLimit(input);
|
|
148
|
-
const inputTokenLimit = runner.tokenBudget
|
|
149
|
-
|
|
290
|
+
const inputTokenLimit = runner.tokenBudget
|
|
291
|
+
? modelInputTokenLimit(runner.tokenBudget, maxOutputTokens)
|
|
292
|
+
: undefined;
|
|
293
|
+
const inputTokens = runner.tokenBudget
|
|
294
|
+
? countModelInput(runner.tokenBudget, size.messages, prepared.options)
|
|
295
|
+
: undefined;
|
|
150
296
|
const maxSchemaChars = runner.ports.maxSchemaChars;
|
|
151
|
-
return Object.freeze({
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
297
|
+
return Object.freeze({
|
|
298
|
+
messageChars: size.messageChars,
|
|
299
|
+
instructionChars: size.instructionChars,
|
|
300
|
+
schemaChars: size.schemaChars,
|
|
301
|
+
transportChars: size.transportChars,
|
|
302
|
+
inputChars: size.inputChars,
|
|
303
|
+
maxInputChars: inputLimit,
|
|
304
|
+
maxSchemaChars,
|
|
305
|
+
maxOutputTokens,
|
|
306
|
+
...(runner.tokenBudget
|
|
307
|
+
? { counterId: runner.tokenBudget.counterId, inputTokens, inputTokenLimit }
|
|
308
|
+
: {}),
|
|
309
|
+
withinBudget: size.messageChars <= inputLimit &&
|
|
310
|
+
size.transportChars <= maxSchemaChars &&
|
|
311
|
+
(inputTokens === undefined || inputTokens <= inputTokenLimit),
|
|
156
312
|
});
|
|
157
313
|
},
|
|
158
314
|
});
|
|
@@ -168,33 +324,81 @@ export class ModelRunner {
|
|
|
168
324
|
const prepared = state?.snapshot;
|
|
169
325
|
const stage = call.stageId ?? prepared?.stageId ?? modelRunnerDefaults.stage;
|
|
170
326
|
const tokens = call.maxOutputTokens ?? prepared?.maxOutputTokens ?? this.maxOutputTokens;
|
|
171
|
-
const invalid = (message) => recordErrorContext(new TypeError(message), {
|
|
327
|
+
const invalid = (message) => recordErrorContext(new TypeError(message), {
|
|
328
|
+
stageId: stage,
|
|
329
|
+
codes: ['MODEL_CONFIG'],
|
|
330
|
+
isRetryable: false,
|
|
331
|
+
});
|
|
172
332
|
breakWhile([
|
|
173
333
|
{ occur: 'stage' in call, error: () => invalid('Use stageId to select a model stage') },
|
|
174
|
-
{
|
|
175
|
-
|
|
176
|
-
|
|
334
|
+
{
|
|
335
|
+
occur: typeof stage !== 'string' || !stage.trim(),
|
|
336
|
+
error: () => invalid('stageId must be a nonempty string'),
|
|
337
|
+
},
|
|
338
|
+
{
|
|
339
|
+
occur: 'transportId' in call,
|
|
340
|
+
error: () => invalid('Use transport to select a registered model transport'),
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
occur: typeof call.transport === 'string' && !call.transport.trim(),
|
|
344
|
+
error: () => invalid('Transport ID must be a nonempty string'),
|
|
345
|
+
},
|
|
177
346
|
{ occur: 'prompt' in call, error: () => invalid('Use input to provide model input') },
|
|
178
347
|
{ occur: call.input == null, error: () => invalid('Model input is required') },
|
|
179
|
-
{
|
|
180
|
-
|
|
348
|
+
{
|
|
349
|
+
occur: call.input !== undefined &&
|
|
350
|
+
typeof call.input !== 'string' &&
|
|
351
|
+
call.instructions !== undefined,
|
|
352
|
+
error: () => invalid('instructions require string input; message inputs already declare their roles'),
|
|
353
|
+
},
|
|
354
|
+
{
|
|
355
|
+
occur: call.instructions !== undefined && typeof call.instructions !== 'string',
|
|
356
|
+
error: () => invalid('instructions must be text'),
|
|
357
|
+
},
|
|
181
358
|
]);
|
|
182
359
|
validateOutputTokens(tokens, stage);
|
|
183
360
|
const source = call.input;
|
|
184
|
-
const input = typeof source === 'string'
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
361
|
+
const input = typeof source === 'string'
|
|
362
|
+
? [
|
|
363
|
+
...(call.instructions === undefined
|
|
364
|
+
? []
|
|
365
|
+
: [{ role: 'system', content: call.instructions }]),
|
|
366
|
+
{ role: 'user', content: source },
|
|
367
|
+
]
|
|
368
|
+
: source;
|
|
188
369
|
const transport = call.transport;
|
|
189
|
-
const transportId = transport === undefined
|
|
190
|
-
|
|
370
|
+
const transportId = transport === undefined
|
|
371
|
+
? (prepared?.transportId ?? defaultTextTransport.id)
|
|
372
|
+
: typeof transport === 'string'
|
|
373
|
+
? transport
|
|
374
|
+
: this.registeredTransports.get(transport);
|
|
191
375
|
breakWhile(transportId === undefined, () => invalid('Typed transport must be the registered transport instance'));
|
|
192
|
-
if (prepared &&
|
|
376
|
+
if (prepared &&
|
|
377
|
+
(stage !== prepared.stageId ||
|
|
378
|
+
tokens !== prepared.maxOutputTokens ||
|
|
379
|
+
transportId !== prepared.transportId))
|
|
193
380
|
throw invalid('Prepared model request settings cannot be replaced');
|
|
194
381
|
if (state)
|
|
195
382
|
state.used = true;
|
|
196
383
|
return this.invokePrepared(stage, input, tokens, transportId, call.diagnostics, prepared, call.signal);
|
|
197
384
|
}
|
|
385
|
+
/**
|
|
386
|
+
* Acquire the per-runner call slot and execute one call.
|
|
387
|
+
*
|
|
388
|
+
* The concurrency guard is taken and released around `execute`; the error context is
|
|
389
|
+
* decorated with the stage before the failure leaves this method, so later layers do
|
|
390
|
+
* not need to re-derive it.
|
|
391
|
+
*
|
|
392
|
+
* @param stage - Stage id for diagnostics and error context.
|
|
393
|
+
* @param input - Messages, built context or context plan.
|
|
394
|
+
* @param tokens - Requested output tokens.
|
|
395
|
+
* @param transportId - Registered transport to execute.
|
|
396
|
+
* @param diagnostics - Observation scope for this call.
|
|
397
|
+
* @param prepared - Prepared call whose snapshot must be reused.
|
|
398
|
+
* @param requestSignal - Caller signal, combined with the runner signal.
|
|
399
|
+
* @returns The parsed result with invocation metadata.
|
|
400
|
+
* @throws Error When a call is already in flight or the request capacity is exhausted.
|
|
401
|
+
*/
|
|
198
402
|
async invokePrepared(stage, input, tokens, transportId, diagnostics = this.ports.diagnostics, prepared, requestSignal) {
|
|
199
403
|
const configuredRun = this.ports.diagnostics?.identity?.runId, selectedRun = diagnostics?.identity?.runId;
|
|
200
404
|
if (configuredRun && selectedRun && configuredRun !== selectedRun)
|
|
@@ -216,16 +420,59 @@ export class ModelRunner {
|
|
|
216
420
|
}
|
|
217
421
|
throw failure;
|
|
218
422
|
}
|
|
423
|
+
/**
|
|
424
|
+
* Execute the prepared call: measure, charge, stream, commit.
|
|
425
|
+
*
|
|
426
|
+
* Phase order is a correctness requirement, not an implementation detail. Every
|
|
427
|
+
* rejection path goes through `reject`, which annotates codes and emits
|
|
428
|
+
* `runtime.rejected`. Budget charging happens only after all input checks pass, so a
|
|
429
|
+
* rejected request never consumes a call. The terminal `model.end` event is published
|
|
430
|
+
* in `finally` with its own timeout, because the call signal is already aborted when a
|
|
431
|
+
* call is cancelled and terminal delivery must still retract previews.
|
|
432
|
+
*
|
|
433
|
+
* @param stage - Stage id for diagnostics.
|
|
434
|
+
* @param input - Messages, built context or context plan.
|
|
435
|
+
* @param tokens - Requested output tokens.
|
|
436
|
+
* @param transportId - Registered transport id.
|
|
437
|
+
* @param signal - Combined execution and request signal.
|
|
438
|
+
* @param parentDiagnostics - Parent observation scope, defaulting to the runner scope.
|
|
439
|
+
* @param prepared - Prepared call whose snapshot must be reused.
|
|
440
|
+
* @returns The parsed result with invocation metadata.
|
|
441
|
+
* @throws Error or ValidationError from any phase, decorated with stage context.
|
|
442
|
+
*/
|
|
219
443
|
async execute(stage, input, tokens, transportId, signal, parentDiagnostics = this.ports.diagnostics, prepared) {
|
|
220
444
|
const id = invocationId(), startedAtMs = Date.now();
|
|
221
445
|
this.activeCall = undefined;
|
|
222
|
-
const invocationDiagnostics = parentDiagnostics?.child(id, {
|
|
446
|
+
const invocationDiagnostics = parentDiagnostics?.child(id, {
|
|
447
|
+
invocationId: id,
|
|
448
|
+
modelId: this.modelId,
|
|
449
|
+
nodeRole: 'invocation',
|
|
450
|
+
definitionKind: 'model-transport',
|
|
451
|
+
definitionId: transportId,
|
|
452
|
+
nodeId: stage,
|
|
453
|
+
nodeName: stage,
|
|
454
|
+
nodeKind: 'model',
|
|
455
|
+
nodeDescription: 'Registered model invocation',
|
|
456
|
+
definitionRevision: undefined,
|
|
457
|
+
stageId: stage,
|
|
458
|
+
});
|
|
223
459
|
let diagnostics = invocationDiagnostics;
|
|
224
460
|
const reject = (error) => {
|
|
225
|
-
recordErrorContext(error, {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
461
|
+
recordErrorContext(error, {
|
|
462
|
+
stageId: stage,
|
|
463
|
+
...(signal.aborted
|
|
464
|
+
? {
|
|
465
|
+
codes: [isTimeoutReason(signal.reason) ? 'ROUND_TIMEOUT' : 'CANCELLED'],
|
|
466
|
+
isRetryable: false,
|
|
467
|
+
}
|
|
468
|
+
: error instanceof BudgetError
|
|
469
|
+
? { codes: ['BUDGET'], isRetryable: false }
|
|
470
|
+
: {}),
|
|
471
|
+
});
|
|
472
|
+
diagnostics?.emit('runtime.rejected', () => ({
|
|
473
|
+
stageId: stage,
|
|
474
|
+
error: diagnosticError(error),
|
|
475
|
+
}));
|
|
229
476
|
throw error;
|
|
230
477
|
};
|
|
231
478
|
const transport = this.transports.get(transportId);
|
|
@@ -286,11 +533,15 @@ export class ModelRunner {
|
|
|
286
533
|
const functionChars = size.transportChars;
|
|
287
534
|
// The binding snapshots and validates this fixed allowance at construction.
|
|
288
535
|
if (functionChars > this.ports.maxSchemaChars)
|
|
289
|
-
return reject(new ValidationError([
|
|
536
|
+
return reject(new ValidationError([
|
|
537
|
+
{ code: 'MODEL_SCHEMA_BUDGET', message: 'Model declarations exceed their schema budget' },
|
|
538
|
+
]));
|
|
290
539
|
const inputChars = size.inputChars;
|
|
291
540
|
try {
|
|
292
|
-
validateContextBudget(messageChars, this.inputLimit(input), limit => this.ports.inputBudgetError?.(limit)
|
|
293
|
-
|
|
541
|
+
validateContextBudget(messageChars, this.inputLimit(input), (limit) => this.ports.inputBudgetError?.(limit) ??
|
|
542
|
+
new ValidationError([
|
|
543
|
+
{ code: 'CONTEXT_BUDGET', message: `Model input exceeds ${limit} characters` },
|
|
544
|
+
]));
|
|
294
545
|
}
|
|
295
546
|
catch (error) {
|
|
296
547
|
return reject(error);
|
|
@@ -309,13 +560,32 @@ export class ModelRunner {
|
|
|
309
560
|
this.budget.take();
|
|
310
561
|
this.completedCallCount++;
|
|
311
562
|
const call = this.callCount;
|
|
312
|
-
const step = {
|
|
313
|
-
|
|
314
|
-
|
|
563
|
+
const step = {
|
|
564
|
+
...invocationDiagnostics?.identity,
|
|
565
|
+
modelId: this.modelId,
|
|
566
|
+
invocationId: id,
|
|
567
|
+
startedAtMs,
|
|
568
|
+
callSequence: call,
|
|
569
|
+
stageId: stage,
|
|
570
|
+
durationMs: 0,
|
|
571
|
+
inputChars,
|
|
572
|
+
outputChars: 0,
|
|
573
|
+
firstOutputMs: null,
|
|
574
|
+
outputDurationMs: 0,
|
|
575
|
+
status: 'error',
|
|
576
|
+
codes: [],
|
|
577
|
+
...(inputTokens === undefined ? {} : { inputTokens, inputTokenLimit: tokenLimit }),
|
|
578
|
+
};
|
|
315
579
|
if (!isMessageArray(input) && diagnostics?.enabled('model.prepare')) {
|
|
316
580
|
try {
|
|
317
|
-
const serialized = JSON.stringify({
|
|
318
|
-
|
|
581
|
+
const serialized = JSON.stringify({
|
|
582
|
+
messages: modelMessageDiagnostics(messages),
|
|
583
|
+
interaction: interactionOptions,
|
|
584
|
+
});
|
|
585
|
+
step.context = {
|
|
586
|
+
fragmentUsage: input.fragmentUsage,
|
|
587
|
+
stablePrefixChars: commonPrefixLength(serialized, this.priorInputs.get(stage) ?? ''),
|
|
588
|
+
};
|
|
319
589
|
this.priorInputs.set(stage, serialized);
|
|
320
590
|
}
|
|
321
591
|
catch {
|
|
@@ -340,7 +610,9 @@ export class ModelRunner {
|
|
|
340
610
|
if (call)
|
|
341
611
|
step.functionCall = call;
|
|
342
612
|
}
|
|
343
|
-
catch {
|
|
613
|
+
catch {
|
|
614
|
+
/* best-effort metadata */
|
|
615
|
+
}
|
|
344
616
|
};
|
|
345
617
|
const accountOutput = () => {
|
|
346
618
|
const outputChars = response.outputChars;
|
|
@@ -350,9 +622,18 @@ export class ModelRunner {
|
|
|
350
622
|
// largest observed size without requiring its internal buffer to grow.
|
|
351
623
|
step.outputChars = Math.max(step.outputChars, outputChars);
|
|
352
624
|
if (this.config.maxOutputChars !== undefined && outputChars > this.config.maxOutputChars)
|
|
353
|
-
throw this.ports.outputBudgetError?.() ??
|
|
625
|
+
throw (this.ports.outputBudgetError?.() ??
|
|
626
|
+
new ValidationError([
|
|
627
|
+
{ code: 'OUTPUT_LIMIT', message: 'Model output exceeds its character budget' },
|
|
628
|
+
]));
|
|
354
629
|
};
|
|
355
|
-
diagnostics?.emit('model.prepare', () => ({
|
|
630
|
+
diagnostics?.emit('model.prepare', () => ({
|
|
631
|
+
stageId: stage,
|
|
632
|
+
transportId,
|
|
633
|
+
messages: modelMessageDiagnostics(messages),
|
|
634
|
+
options: { ...interactionOptions, maxOutputTokens: tokens },
|
|
635
|
+
context: isMessageArray(input) ? undefined : input.fragmentUsage,
|
|
636
|
+
}));
|
|
356
637
|
try {
|
|
357
638
|
await this.publishOutput('model.start', () => ({ stageId: stage, callSequence: call, transportId }), callSignal, diagnostics);
|
|
358
639
|
callSignal.throwIfAborted();
|
|
@@ -361,10 +642,18 @@ export class ModelRunner {
|
|
|
361
642
|
throw new TypeError('Invalid model response collector');
|
|
362
643
|
accountOutput();
|
|
363
644
|
callSignal.throwIfAborted();
|
|
364
|
-
for await (const part of abortableStream(this.provider.stream(messages, {
|
|
645
|
+
for await (const part of abortableStream(this.provider.stream(messages, {
|
|
646
|
+
signal: callSignal,
|
|
647
|
+
diagnostics,
|
|
648
|
+
options: {
|
|
365
649
|
...interactionOptions,
|
|
366
|
-
maxOutputTokens: tokens,
|
|
367
|
-
|
|
650
|
+
maxOutputTokens: tokens,
|
|
651
|
+
includeUsage: this.config.usage,
|
|
652
|
+
onUsage: (usage) => {
|
|
653
|
+
step.usage = usage;
|
|
654
|
+
},
|
|
655
|
+
},
|
|
656
|
+
}), callSignal)) {
|
|
368
657
|
const partIndex = receivedParts;
|
|
369
658
|
receivedParts++;
|
|
370
659
|
callSignal.throwIfAborted();
|
|
@@ -382,29 +671,67 @@ export class ModelRunner {
|
|
|
382
671
|
captureCallMetadata();
|
|
383
672
|
await this.publishOutput('model.parsed', () => ({ stageId: stage, callSequence: call, value }), callSignal, diagnostics);
|
|
384
673
|
callSignal.throwIfAborted();
|
|
385
|
-
this.journal.model({
|
|
674
|
+
this.journal.model({
|
|
675
|
+
stageId: stage,
|
|
676
|
+
value,
|
|
677
|
+
invocation: {
|
|
678
|
+
...invocationDiagnostics?.identity,
|
|
679
|
+
invocationId: id,
|
|
680
|
+
startedAtMs,
|
|
681
|
+
durationMs: Date.now() - startedAtMs,
|
|
682
|
+
},
|
|
683
|
+
});
|
|
386
684
|
step.status = 'success';
|
|
387
|
-
return invocationResult(value, {
|
|
388
|
-
|
|
389
|
-
|
|
685
|
+
return invocationResult(value, {
|
|
686
|
+
...invocationDiagnostics?.identity,
|
|
687
|
+
invocationId: id,
|
|
688
|
+
modelId: this.modelId,
|
|
689
|
+
stageId: stage,
|
|
690
|
+
startedAtMs,
|
|
691
|
+
durationMs: Date.now() - startedAtMs,
|
|
692
|
+
callSequence: call,
|
|
693
|
+
status: 'success',
|
|
694
|
+
...(step.usage ? { usage: Object.freeze({ ...step.usage }) } : {}),
|
|
695
|
+
});
|
|
390
696
|
}
|
|
391
697
|
catch (error) {
|
|
392
698
|
callError = { error };
|
|
393
699
|
step.status = signal.aborted || callDeadline.aborted ? 'cancelled' : 'error';
|
|
394
700
|
try {
|
|
395
701
|
const info = getErrorInfo(error);
|
|
396
|
-
const codes = error instanceof ValidationError
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
702
|
+
const codes = error instanceof ValidationError
|
|
703
|
+
? error.issues.map((item) => item.code)
|
|
704
|
+
: [
|
|
705
|
+
signal.aborted
|
|
706
|
+
? isTimeoutReason(signal.reason)
|
|
707
|
+
? 'ROUND_TIMEOUT'
|
|
708
|
+
: 'CANCELLED'
|
|
709
|
+
: callDeadline.aborted
|
|
710
|
+
? 'CALL_TIMEOUT'
|
|
711
|
+
: info.code === 'UNKNOWN_ERROR'
|
|
712
|
+
? 'UPSTREAM_ERROR'
|
|
713
|
+
: info.code,
|
|
714
|
+
];
|
|
715
|
+
recordErrorContext(error, {
|
|
716
|
+
stageId: stage,
|
|
717
|
+
codes,
|
|
718
|
+
...(signal.aborted
|
|
719
|
+
? { isRetryable: false }
|
|
720
|
+
: callDeadline.aborted
|
|
721
|
+
? { isRetryable: true }
|
|
722
|
+
: {}),
|
|
723
|
+
});
|
|
401
724
|
step.codes = [...getErrorInfo(error).codes];
|
|
402
725
|
}
|
|
403
726
|
catch {
|
|
404
727
|
// Even instanceof or an Error subclass's fields can throw. Decoration
|
|
405
728
|
// must not replace the original value or skip terminal delivery.
|
|
406
729
|
const cancelled = step.status === 'cancelled', codes = [cancelled ? 'CANCELLED' : 'UPSTREAM_ERROR'];
|
|
407
|
-
recordErrorContext(error, {
|
|
730
|
+
recordErrorContext(error, {
|
|
731
|
+
stageId: stage,
|
|
732
|
+
codes,
|
|
733
|
+
...(cancelled ? { isRetryable: false } : {}),
|
|
734
|
+
});
|
|
408
735
|
step.codes = codes;
|
|
409
736
|
}
|
|
410
737
|
}
|
|
@@ -412,13 +739,20 @@ export class ModelRunner {
|
|
|
412
739
|
captureCallMetadata();
|
|
413
740
|
step.durationMs = Date.now() - startedAtMs;
|
|
414
741
|
step.outputDurationMs = step.firstOutputMs == null ? 0 : step.durationMs - step.firstOutputMs;
|
|
415
|
-
const end = () => ({
|
|
742
|
+
const end = () => ({
|
|
743
|
+
stageId: stage,
|
|
744
|
+
state: { ...step },
|
|
745
|
+
receivedParts,
|
|
746
|
+
...(callError ? { error: diagnosticError(callError.error) } : {}),
|
|
747
|
+
});
|
|
416
748
|
// Terminal delivery also retracts cancelled previews. It needs its own
|
|
417
749
|
// bounded lifetime: the call signal is already aborted on cancellation.
|
|
418
750
|
try {
|
|
419
751
|
await this.publishOutput('model.end', end, AbortSignal.timeout(250), diagnostics);
|
|
420
752
|
}
|
|
421
|
-
catch {
|
|
753
|
+
catch {
|
|
754
|
+
/* Terminal notification cannot replace a committed result or its original failure. */
|
|
755
|
+
}
|
|
422
756
|
}
|
|
423
757
|
throw callError.error;
|
|
424
758
|
}
|