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