@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
|
@@ -1,20 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI-compatible HTTP provider: streaming chat completions over SSE.
|
|
3
|
+
*
|
|
4
|
+
* The provider owns the request/response lifecycle and error classification; the application
|
|
5
|
+
* supplies endpoint, credentials, optional request-body extensions and user-visible error mapping.
|
|
6
|
+
* Configuration is snapshotted so getters cannot change between validation and I/O, and every
|
|
7
|
+
* failure is recorded with the same code that is returned to the caller.
|
|
8
|
+
*
|
|
9
|
+
* Main exports: `CompatibleModelProvider` and the provider configuration/error types.
|
|
10
|
+
*/
|
|
1
11
|
import { getErrorInfo, recordErrorContext } from '../../errors.js';
|
|
2
12
|
import { modelRunnerDefaults } from '../../model/defaults.js';
|
|
3
13
|
import { snapshotModelOptions } from '../../model/options.js';
|
|
4
14
|
import { diagnosticError } from '../../diagnostics/index.js';
|
|
5
15
|
import { ChatProvider } from '../../structs/model/chat-provider.js';
|
|
6
|
-
import { chatCompletionFunctionOptions, chatCompletionParts, chatCompletionMessages } from './chat-completions.js';
|
|
16
|
+
import { chatCompletionFunctionOptions, chatCompletionParts, chatCompletionMessages, } from './chat-completions.js';
|
|
17
|
+
import { defaultCompletionPolicy } from './completion/default.js';
|
|
7
18
|
import { readSse } from '../transport/sse.js';
|
|
8
19
|
import { providerObservation } from './observer.js';
|
|
9
20
|
import { synchronous } from '../../utils/synchronous.js';
|
|
10
21
|
import { awaitWithSignal } from '../../utils/cancellation.js';
|
|
22
|
+
/**
|
|
23
|
+
* HTTP status to provider error code.
|
|
24
|
+
*
|
|
25
|
+
* Unlisted statuses fall back to `MODEL_UPSTREAM`; 408, 429 and 5xx are additionally marked
|
|
26
|
+
* retryable when the error context is recorded.
|
|
27
|
+
*/
|
|
11
28
|
const httpFailures = Object.freeze({
|
|
12
|
-
400: 'MODEL_CONFIG',
|
|
13
|
-
|
|
29
|
+
400: 'MODEL_CONFIG',
|
|
30
|
+
401: 'MODEL_AUTH',
|
|
31
|
+
402: 'MODEL_BALANCE',
|
|
32
|
+
403: 'MODEL_AUTH',
|
|
33
|
+
404: 'MODEL_CONFIG',
|
|
34
|
+
429: 'MODEL_BUSY',
|
|
14
35
|
});
|
|
15
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Request fields the adapter composes itself.
|
|
38
|
+
*
|
|
39
|
+
* Allowing `requestBody` to set them would let application configuration replace the model,
|
|
40
|
+
* prompt, streaming mode or output budget that Core already validated.
|
|
41
|
+
*/
|
|
42
|
+
const reservedRequestFields = [
|
|
43
|
+
'model',
|
|
44
|
+
'messages',
|
|
45
|
+
'stream',
|
|
46
|
+
'max_tokens',
|
|
47
|
+
'max_completion_tokens',
|
|
48
|
+
'toJSON',
|
|
49
|
+
];
|
|
16
50
|
function assertRequestExtensions(config) {
|
|
17
|
-
if (config.maxOutputTokens !== undefined &&
|
|
51
|
+
if (config.maxOutputTokens !== undefined &&
|
|
52
|
+
(!Number.isSafeInteger(config.maxOutputTokens) || config.maxOutputTokens < 1)) {
|
|
18
53
|
throw new TypeError('Provider maxOutputTokens must be a positive safe integer');
|
|
19
54
|
}
|
|
20
55
|
for (const key of reservedRequestFields) {
|
|
@@ -67,12 +102,18 @@ function snapshotConfiguration(config, signal) {
|
|
|
67
102
|
generated = { enable_thinking: level !== 'off' };
|
|
68
103
|
else if (format === 'thinking-budget') {
|
|
69
104
|
const configured = read(reasoning, 'budgets');
|
|
70
|
-
const budgets = {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
105
|
+
const budgets = {
|
|
106
|
+
low: configured ? (read(configured, 'low') ?? 1024) : 1024,
|
|
107
|
+
medium: configured ? (read(configured, 'medium') ?? 4096) : 4096,
|
|
108
|
+
high: configured ? (read(configured, 'high') ?? 8192) : 8192,
|
|
109
|
+
};
|
|
110
|
+
if (Object.values(budgets).some((value) => !Number.isSafeInteger(value) || value < 1))
|
|
74
111
|
throw new TypeError('Thinking budgets must be positive safe integers');
|
|
75
|
-
generated = {
|
|
112
|
+
generated = {
|
|
113
|
+
thinking: level === 'off'
|
|
114
|
+
? { type: 'disabled' }
|
|
115
|
+
: { type: 'enabled', budget_tokens: budgets[level] },
|
|
116
|
+
};
|
|
76
117
|
}
|
|
77
118
|
else
|
|
78
119
|
throw new TypeError('Unknown provider reasoning format');
|
|
@@ -91,10 +132,22 @@ function snapshotConfiguration(config, signal) {
|
|
|
91
132
|
throw error;
|
|
92
133
|
}
|
|
93
134
|
}
|
|
94
|
-
/**
|
|
135
|
+
/**
|
|
136
|
+
* HTTP/SSE lifecycle shared by hosts. Apps supply config and user-visible error mapping only.
|
|
137
|
+
*
|
|
138
|
+
* A static configuration is validated and frozen in the constructor. A configuration function is
|
|
139
|
+
* called once per request instead, so credentials and models may rotate between calls.
|
|
140
|
+
*/
|
|
95
141
|
export class CompatibleModelProvider extends ChatProvider {
|
|
96
142
|
configuration;
|
|
97
143
|
ports;
|
|
144
|
+
/**
|
|
145
|
+
* Create a provider.
|
|
146
|
+
*
|
|
147
|
+
* @param options Static or per-request configuration, optional ports and observation/error hooks.
|
|
148
|
+
* @throws {TypeError} When a static configuration is invalid (for example `maxTokens`,
|
|
149
|
+
* reserved request-body fields, unsupported reasoning format or invalid budgets).
|
|
150
|
+
*/
|
|
98
151
|
constructor(options) {
|
|
99
152
|
super();
|
|
100
153
|
const { config: configuration, ports } = options;
|
|
@@ -102,6 +155,7 @@ export class CompatibleModelProvider extends ChatProvider {
|
|
|
102
155
|
request: ports?.request?.bind(ports),
|
|
103
156
|
ensureJsonInstruction: ports?.ensureJsonInstruction,
|
|
104
157
|
allowEmptyJson: ports?.allowEmptyJson,
|
|
158
|
+
completionPolicy: ports?.completionPolicy,
|
|
105
159
|
observer: options.observe?.provider,
|
|
106
160
|
error: options.errors?.failure?.bind(options.errors),
|
|
107
161
|
};
|
|
@@ -111,11 +165,33 @@ export class CompatibleModelProvider extends ChatProvider {
|
|
|
111
165
|
this.configuration = snapshotConfiguration(configuration);
|
|
112
166
|
}
|
|
113
167
|
failure(code, status, reason) {
|
|
114
|
-
const error = this.ports.error?.({
|
|
115
|
-
|
|
116
|
-
|
|
168
|
+
const error = this.ports.error?.({
|
|
169
|
+
code,
|
|
170
|
+
...(status !== undefined ? { status } : {}),
|
|
171
|
+
...(reason ? { reason } : {}),
|
|
172
|
+
}) ?? Object.assign(new Error(code), { code, status, reason });
|
|
173
|
+
recordErrorContext(error, {
|
|
174
|
+
codes: [code],
|
|
175
|
+
// Transient HTTP conditions are retryable; protocol/format failures are not.
|
|
176
|
+
...(status === undefined
|
|
177
|
+
? {}
|
|
178
|
+
: { isRetryable: status === 408 || status === 429 || status >= 500 }),
|
|
179
|
+
});
|
|
117
180
|
return error;
|
|
118
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Stream one chat-completions response as Core model parts.
|
|
184
|
+
*
|
|
185
|
+
* The method validates the effective request and yields parts as they arrive. Whether the
|
|
186
|
+
* finished stream is acceptable is decided by the configured completion policy, which defaults
|
|
187
|
+
* to the legacy rules. A stream that ends without the end-of-stream marker is incomplete, not a
|
|
188
|
+
* partial success.
|
|
189
|
+
*
|
|
190
|
+
* @param messages Prompt messages in order, including provider tool-thread messages.
|
|
191
|
+
* @param context Provider context with signal, call options and diagnostics/observer ports.
|
|
192
|
+
* @yields Text chunks and function-call deltas as they arrive.
|
|
193
|
+
* @throws The classified provider error (see `MODEL_*` codes) or the signal's abort reason.
|
|
194
|
+
*/
|
|
119
195
|
async *stream(messages, context) {
|
|
120
196
|
const { signal } = context;
|
|
121
197
|
signal.throwIfAborted();
|
|
@@ -128,11 +204,14 @@ export class CompatibleModelProvider extends ChatProvider {
|
|
|
128
204
|
throw new TypeError('Use maxOutputTokens');
|
|
129
205
|
options = snapshotModelOptions(configured);
|
|
130
206
|
signal.throwIfAborted();
|
|
131
|
-
if (options.maxOutputTokens !== undefined &&
|
|
207
|
+
if (options.maxOutputTokens !== undefined &&
|
|
208
|
+
(!Number.isSafeInteger(options.maxOutputTokens) || options.maxOutputTokens < 1)) {
|
|
132
209
|
throw new TypeError('maxOutputTokens must be a positive safe integer');
|
|
133
210
|
}
|
|
134
|
-
config =
|
|
135
|
-
|
|
211
|
+
config =
|
|
212
|
+
typeof this.configuration === 'function'
|
|
213
|
+
? snapshotConfiguration(this.configuration(), signal)
|
|
214
|
+
: this.configuration;
|
|
136
215
|
signal.throwIfAborted();
|
|
137
216
|
}
|
|
138
217
|
catch (error) {
|
|
@@ -141,9 +220,14 @@ export class CompatibleModelProvider extends ChatProvider {
|
|
|
141
220
|
}
|
|
142
221
|
const maxOutputTokens = Math.min(options.maxOutputTokens ?? config.maxOutputTokens ?? modelRunnerDefaults.maxOutputTokens, config.maxOutputTokens ?? Infinity);
|
|
143
222
|
const thinking = config.requestBody?.thinking;
|
|
144
|
-
if (thinking !== null &&
|
|
223
|
+
if (thinking !== null &&
|
|
224
|
+
typeof thinking === 'object' &&
|
|
225
|
+
Object.hasOwn(thinking, 'budget_tokens')) {
|
|
145
226
|
const budget = thinking.budget_tokens;
|
|
146
|
-
if (typeof budget !== 'number' ||
|
|
227
|
+
if (typeof budget !== 'number' ||
|
|
228
|
+
!Number.isSafeInteger(budget) ||
|
|
229
|
+
budget < 1 ||
|
|
230
|
+
budget >= maxOutputTokens) {
|
|
147
231
|
throw new TypeError('Thinking budget_tokens must be positive and smaller than the effective output token limit');
|
|
148
232
|
}
|
|
149
233
|
}
|
|
@@ -153,61 +237,99 @@ export class CompatibleModelProvider extends ChatProvider {
|
|
|
153
237
|
try {
|
|
154
238
|
if (!config.apiKey)
|
|
155
239
|
throw this.failure('MODEL_NOT_CONFIGURED');
|
|
156
|
-
|
|
157
|
-
|
|
240
|
+
// Some providers require the word "json" in the prompt before accepting a JSON response
|
|
241
|
+
// format; only add the instruction when the prompt does not already mention it.
|
|
242
|
+
const needsJsonInstruction = this.ports.ensureJsonInstruction &&
|
|
243
|
+
!options.functions &&
|
|
244
|
+
options.json &&
|
|
245
|
+
!messages.some((message) => ['system', 'user'].includes(message.role) && /json/i.test(message.content));
|
|
158
246
|
const requestMessages = needsJsonInstruction
|
|
159
|
-
? [{ role: 'system', content: '只输出合法 JSON 对象,遵循给定字段和示例。' }, ...messages]
|
|
247
|
+
? [{ role: 'system', content: '只输出合法 JSON 对象,遵循给定字段和示例。' }, ...messages]
|
|
248
|
+
: messages;
|
|
160
249
|
const request = this.ports.request ?? fetch;
|
|
250
|
+
// Build the body once: the completion policy must judge the same fields the upstream reads.
|
|
251
|
+
const body = {
|
|
252
|
+
model: config.model,
|
|
253
|
+
messages: chatCompletionMessages(requestMessages),
|
|
254
|
+
stream: true,
|
|
255
|
+
max_tokens: maxOutputTokens,
|
|
256
|
+
...((options.temperature ?? config.temperature) !== undefined
|
|
257
|
+
? { temperature: options.temperature ?? config.temperature }
|
|
258
|
+
: {}),
|
|
259
|
+
...(!options.functions && options.json ? { response_format: { type: 'json_object' } } : {}),
|
|
260
|
+
...chatCompletionFunctionOptions(options),
|
|
261
|
+
...config.requestBody,
|
|
262
|
+
...(options.includeUsage ? { stream_options: { include_usage: true } } : {}),
|
|
263
|
+
};
|
|
264
|
+
const policy = this.ports.completionPolicy ?? defaultCompletionPolicy;
|
|
265
|
+
const session = policy.begin({
|
|
266
|
+
functions: !!options.functions,
|
|
267
|
+
toolChoice: body.tool_choice,
|
|
268
|
+
allowEmptyText: !options.functions && !!options.json && !!this.ports.allowEmptyJson,
|
|
269
|
+
}, (code) => this.failure(code));
|
|
161
270
|
const observedRequest = (url, init) => {
|
|
162
271
|
observation.emit('provider.request', () => {
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
272
|
+
const observed = JSON.parse(String(init?.body));
|
|
273
|
+
observed.messages = observed.messages.map((message) => ({
|
|
274
|
+
...message,
|
|
275
|
+
content: Array.isArray(message.content)
|
|
276
|
+
? message.content.map((part) => part.type === 'image_url'
|
|
277
|
+
? { ...part, image_url: { url: '[image omitted]' } }
|
|
278
|
+
: part)
|
|
279
|
+
: message.content,
|
|
280
|
+
}));
|
|
281
|
+
return { url: String(url), body: observed };
|
|
167
282
|
});
|
|
168
283
|
signal.throwIfAborted();
|
|
169
284
|
return request(url, init);
|
|
170
285
|
};
|
|
171
286
|
signal.throwIfAborted();
|
|
172
287
|
const pendingResponse = Promise.resolve(observedRequest(`${config.baseUrl.replace(/\/+$/, '')}/chat/completions`, {
|
|
173
|
-
method: 'POST',
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
})).then(response => activeResponse(response, signal));
|
|
288
|
+
method: 'POST',
|
|
289
|
+
redirect: 'error',
|
|
290
|
+
signal,
|
|
291
|
+
headers: {
|
|
292
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
293
|
+
'Content-Type': 'application/json',
|
|
294
|
+
Accept: 'text/event-stream',
|
|
295
|
+
},
|
|
296
|
+
body: JSON.stringify(body),
|
|
297
|
+
})).then((response) => activeResponse(response, signal));
|
|
183
298
|
const response = await awaitWithSignal(pendingResponse, signal);
|
|
184
|
-
observation.emit('provider.response', () => ({
|
|
299
|
+
observation.emit('provider.response', () => ({
|
|
300
|
+
status: response.status,
|
|
301
|
+
requestId: response.headers.get('x-request-id'),
|
|
302
|
+
contentType: response.headers.get('content-type'),
|
|
303
|
+
}));
|
|
185
304
|
activeResponse(response, signal);
|
|
186
305
|
if (!response.ok) {
|
|
187
306
|
if (observation.enabled('provider.error-body')) {
|
|
188
307
|
try {
|
|
189
308
|
const capture = await captureErrorBody(response);
|
|
190
|
-
observation.emit('provider.error-body', () => ({
|
|
309
|
+
observation.emit('provider.error-body', () => ({
|
|
310
|
+
status: response.status,
|
|
311
|
+
...capture,
|
|
312
|
+
}));
|
|
313
|
+
}
|
|
314
|
+
catch {
|
|
315
|
+
/* Error capture cannot replace the HTTP failure. */
|
|
191
316
|
}
|
|
192
|
-
catch { /* Error capture cannot replace the HTTP failure. */ }
|
|
193
317
|
}
|
|
194
318
|
else
|
|
195
319
|
discardResponse(response);
|
|
196
320
|
throw this.failure(httpFailures[response.status] ?? 'MODEL_UPSTREAM', response.status);
|
|
197
321
|
}
|
|
198
|
-
if (!response.body ||
|
|
322
|
+
if (!response.body ||
|
|
323
|
+
!response.headers.get('content-type')?.toLowerCase().startsWith('text/event-stream')) {
|
|
199
324
|
discardResponse(response);
|
|
200
325
|
throw this.failure('MODEL_FORMAT');
|
|
201
326
|
}
|
|
202
327
|
let finished = false;
|
|
203
|
-
let hasFunction = false;
|
|
204
|
-
let hasText = false;
|
|
205
328
|
for await (const event of readSse(response.body, { signal, waitForCancel: false })) {
|
|
206
329
|
observation.emit('provider.frame', () => ({ data: event.data }));
|
|
207
330
|
signal.throwIfAborted();
|
|
208
331
|
if (event.data === '[DONE]') {
|
|
209
|
-
|
|
210
|
-
throw this.failure('MODEL_INCOMPLETE');
|
|
332
|
+
session.end();
|
|
211
333
|
complete = true;
|
|
212
334
|
return;
|
|
213
335
|
}
|
|
@@ -223,11 +345,17 @@ export class CompatibleModelProvider extends ChatProvider {
|
|
|
223
345
|
if (chunk.error)
|
|
224
346
|
throw this.failure('MODEL_UPSTREAM');
|
|
225
347
|
const usage = chunk.usage;
|
|
226
|
-
if (usage &&
|
|
227
|
-
|
|
348
|
+
if (usage &&
|
|
349
|
+
[usage.prompt_tokens, usage.completion_tokens, usage.total_tokens].every((value) => Number.isInteger(value) && value >= 0))
|
|
350
|
+
options.onUsage?.({
|
|
351
|
+
promptTokens: usage.prompt_tokens,
|
|
352
|
+
completionTokens: usage.completion_tokens,
|
|
353
|
+
totalTokens: usage.total_tokens,
|
|
354
|
+
});
|
|
228
355
|
signal.throwIfAborted();
|
|
229
356
|
if (chunk.choices !== undefined && !Array.isArray(chunk.choices))
|
|
230
357
|
throw this.failure('MODEL_FORMAT');
|
|
358
|
+
// One request produces one choice; extra choices are ignored rather than merged.
|
|
231
359
|
const choice = chunk.choices?.find((item) => item?.index === 0);
|
|
232
360
|
if (!choice)
|
|
233
361
|
continue;
|
|
@@ -236,20 +364,13 @@ export class CompatibleModelProvider extends ChatProvider {
|
|
|
236
364
|
const parts = chatCompletionParts(choice.delta);
|
|
237
365
|
for (const part of parts) {
|
|
238
366
|
signal.throwIfAborted();
|
|
239
|
-
|
|
240
|
-
hasText = true;
|
|
241
|
-
else {
|
|
242
|
-
if (!options.functions)
|
|
243
|
-
throw this.failure('MODEL_PROTOCOL');
|
|
244
|
-
hasFunction = true;
|
|
245
|
-
}
|
|
367
|
+
session.part(part);
|
|
246
368
|
yield part;
|
|
247
369
|
}
|
|
248
370
|
if (choice.finish_reason != null) {
|
|
249
371
|
if (finished)
|
|
250
372
|
throw this.failure('MODEL_PROTOCOL');
|
|
251
|
-
|
|
252
|
-
throw this.failure(choice.finish_reason === 'length' ? 'MODEL_OUTPUT_LIMIT' : 'MODEL_INCOMPLETE');
|
|
373
|
+
session.finish(choice.finish_reason);
|
|
253
374
|
finished = true;
|
|
254
375
|
}
|
|
255
376
|
}
|
|
@@ -257,11 +378,14 @@ export class CompatibleModelProvider extends ChatProvider {
|
|
|
257
378
|
}
|
|
258
379
|
catch (error) {
|
|
259
380
|
failure = { error };
|
|
260
|
-
if (!signal.aborted && getErrorInfo(error).codes.every(code => code === 'UNKNOWN_ERROR'))
|
|
381
|
+
if (!signal.aborted && getErrorInfo(error).codes.every((code) => code === 'UNKNOWN_ERROR'))
|
|
261
382
|
recordErrorContext(error, { codes: ['MODEL_UPSTREAM'] });
|
|
262
383
|
}
|
|
263
384
|
finally {
|
|
264
|
-
observation.emit('provider.end', () => ({
|
|
385
|
+
observation.emit('provider.end', () => ({
|
|
386
|
+
complete,
|
|
387
|
+
...(failure ? { error: diagnosticError(failure.error) } : {}),
|
|
388
|
+
}));
|
|
265
389
|
}
|
|
266
390
|
throw failure.error;
|
|
267
391
|
}
|
|
@@ -271,7 +395,9 @@ function discardResponse(response) {
|
|
|
271
395
|
try {
|
|
272
396
|
void Promise.resolve(response.body?.cancel()).catch(() => { });
|
|
273
397
|
}
|
|
274
|
-
catch {
|
|
398
|
+
catch {
|
|
399
|
+
/* Preserve the classified failure. */
|
|
400
|
+
}
|
|
275
401
|
}
|
|
276
402
|
function activeResponse(response, signal) {
|
|
277
403
|
if (signal.aborted) {
|
|
@@ -290,7 +416,9 @@ async function captureErrorBody(response) {
|
|
|
290
416
|
let bytes = 0;
|
|
291
417
|
let truncated = true;
|
|
292
418
|
let timer;
|
|
293
|
-
const deadline = new Promise(resolve => {
|
|
419
|
+
const deadline = new Promise((resolve) => {
|
|
420
|
+
timer = setTimeout(() => resolve(undefined), 250);
|
|
421
|
+
});
|
|
294
422
|
try {
|
|
295
423
|
while (bytes < 65536) {
|
|
296
424
|
const item = await Promise.race([reader.read(), deadline]);
|
|
@@ -314,6 +442,8 @@ async function captureErrorBody(response) {
|
|
|
314
442
|
try {
|
|
315
443
|
reader.releaseLock();
|
|
316
444
|
}
|
|
317
|
-
catch {
|
|
445
|
+
catch {
|
|
446
|
+
/* A pending read is cancelled independently. */
|
|
447
|
+
}
|
|
318
448
|
}
|
|
319
449
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool-call delta aggregation.
|
|
3
|
+
*
|
|
4
|
+
* One algorithm is shared by completion policies and direct callers: fragments are keyed by their
|
|
5
|
+
* provider index, IDs may not conflict, and a complete call requires both an ID and a name. A
|
|
6
|
+
* complete call never hides another started but incomplete one.
|
|
7
|
+
*
|
|
8
|
+
* Exports: `AggregatedToolCall`, `ToolCallCollector`, `ToolCallFailure` and
|
|
9
|
+
* `createToolCallCollector`.
|
|
10
|
+
*/
|
|
11
|
+
import type { ModelPart } from '../../../structs/model/messages.js';
|
|
12
|
+
/** One completed native tool call in provider index order. */
|
|
13
|
+
export interface AggregatedToolCall {
|
|
14
|
+
readonly index: number;
|
|
15
|
+
readonly id: string;
|
|
16
|
+
readonly name: string;
|
|
17
|
+
readonly arguments: string;
|
|
18
|
+
}
|
|
19
|
+
/** Report a protocol violation and stop; callers decide how the failure is classified. */
|
|
20
|
+
export type ToolCallFailure = (message: string) => never;
|
|
21
|
+
/** Aggregation entry point for streamed function-call deltas. */
|
|
22
|
+
export interface ToolCallCollector {
|
|
23
|
+
/** Feed one part; text parts are ignored and cannot satisfy or break a call. */
|
|
24
|
+
push(part: ModelPart): void;
|
|
25
|
+
/** Calls started so far, including incomplete ones, in index order. */
|
|
26
|
+
readonly calls: readonly AggregatedToolCall[];
|
|
27
|
+
/**
|
|
28
|
+
* Finalize the calls.
|
|
29
|
+
*
|
|
30
|
+
* @throws When any started call is still missing its ID or name.
|
|
31
|
+
*/
|
|
32
|
+
complete(): readonly AggregatedToolCall[];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Create a collector for one response.
|
|
36
|
+
*
|
|
37
|
+
* @param fail - Protocol failure factory; defaults to the Core model protocol error.
|
|
38
|
+
* @returns A collector that keeps its own state and never shares it with another response.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createToolCallCollector(fail?: ToolCallFailure): ToolCallCollector;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { modelProtocolError } from '../../../utils/model-protocol.js';
|
|
2
|
+
/**
|
|
3
|
+
* Create a collector for one response.
|
|
4
|
+
*
|
|
5
|
+
* @param fail - Protocol failure factory; defaults to the Core model protocol error.
|
|
6
|
+
* @returns A collector that keeps its own state and never shares it with another response.
|
|
7
|
+
*/
|
|
8
|
+
export function createToolCallCollector(fail = modelProtocolError) {
|
|
9
|
+
const pending = new Map();
|
|
10
|
+
const ids = new Set();
|
|
11
|
+
const calls = () => [...pending.entries()]
|
|
12
|
+
.sort(([left], [right]) => left - right)
|
|
13
|
+
.map(([index, call]) => ({ index, ...call }));
|
|
14
|
+
return {
|
|
15
|
+
push(part) {
|
|
16
|
+
if (typeof part === 'string')
|
|
17
|
+
return;
|
|
18
|
+
if (!part || typeof part !== 'object' || Array.isArray(part))
|
|
19
|
+
fail('Expected a native model call');
|
|
20
|
+
const { type, index, id, name, arguments: args } = part;
|
|
21
|
+
if (type !== 'function-call-delta')
|
|
22
|
+
fail('Expected a native model call');
|
|
23
|
+
if (!Number.isInteger(index) || index < 0)
|
|
24
|
+
fail('Invalid model call index');
|
|
25
|
+
if ((id !== undefined && (typeof id !== 'string' || !id)) ||
|
|
26
|
+
(name !== undefined && typeof name !== 'string') ||
|
|
27
|
+
(args !== undefined && typeof args !== 'string'))
|
|
28
|
+
fail('Invalid model call fields');
|
|
29
|
+
let call = pending.get(index);
|
|
30
|
+
if (!call) {
|
|
31
|
+
call = { id: '', name: '', arguments: '' };
|
|
32
|
+
pending.set(index, call);
|
|
33
|
+
}
|
|
34
|
+
if (id !== undefined) {
|
|
35
|
+
if (call.id && call.id !== id)
|
|
36
|
+
fail('Conflicting model call IDs');
|
|
37
|
+
if (!call.id) {
|
|
38
|
+
if (ids.has(id))
|
|
39
|
+
fail('Duplicate model call ID');
|
|
40
|
+
ids.add(id);
|
|
41
|
+
call.id = id;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
call.name += name ?? '';
|
|
45
|
+
call.arguments += args ?? '';
|
|
46
|
+
},
|
|
47
|
+
get calls() {
|
|
48
|
+
return calls();
|
|
49
|
+
},
|
|
50
|
+
complete() {
|
|
51
|
+
const completed = calls();
|
|
52
|
+
for (const call of completed)
|
|
53
|
+
if (!call.id || !call.name)
|
|
54
|
+
fail('Incomplete model call');
|
|
55
|
+
return completed;
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default completion policy: the behavior the provider had before policies were configurable.
|
|
3
|
+
*
|
|
4
|
+
* Strictness is driven by whether the call declared functions, not by `tool_choice`: that one fact
|
|
5
|
+
* decides both whether function parts are acceptable and which `finish_reason` is expected.
|
|
6
|
+
*
|
|
7
|
+
* Exports: `defaultCompletionPolicy`.
|
|
8
|
+
*/
|
|
9
|
+
import type { CompletionPolicy } from '../../../structs/support/model/completion.js';
|
|
10
|
+
export declare const defaultCompletionPolicy: CompletionPolicy;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const policy = {
|
|
2
|
+
id: 'default',
|
|
3
|
+
begin(facts, fail) {
|
|
4
|
+
let finished = false;
|
|
5
|
+
let hasText = false;
|
|
6
|
+
let hasCall = false;
|
|
7
|
+
return {
|
|
8
|
+
part(part) {
|
|
9
|
+
if (typeof part === 'string') {
|
|
10
|
+
hasText = true;
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (!facts.functions)
|
|
14
|
+
throw fail('MODEL_PROTOCOL');
|
|
15
|
+
hasCall = true;
|
|
16
|
+
},
|
|
17
|
+
finish(reason) {
|
|
18
|
+
if (reason !== (facts.functions ? 'tool_calls' : 'stop'))
|
|
19
|
+
throw fail(reason === 'length' ? 'MODEL_OUTPUT_LIMIT' : 'MODEL_INCOMPLETE');
|
|
20
|
+
finished = true;
|
|
21
|
+
},
|
|
22
|
+
end() {
|
|
23
|
+
if (!finished || (facts.functions ? !hasCall : !hasText && !facts.allowEmptyText))
|
|
24
|
+
throw fail('MODEL_INCOMPLETE');
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
export const defaultCompletionPolicy = Object.freeze(policy);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { createToolCallCollector } from './calls.js';
|
|
2
|
+
export type { AggregatedToolCall, ToolCallCollector, ToolCallFailure } from './calls.js';
|
|
3
|
+
export { defaultCompletionPolicy } from './default.js';
|
|
4
|
+
export { sessionCompletionPolicy } from './session.js';
|
|
5
|
+
export type * from '../../../structs/support/model/completion.js';
|
|
6
|
+
/** Preset policies selected by an explicit port; the default is used when none is given. */
|
|
7
|
+
export declare const completionPolicyCatalog: Readonly<{
|
|
8
|
+
default: import("./index.js").CompletionPolicy;
|
|
9
|
+
session: import("./index.js").CompletionPolicy;
|
|
10
|
+
}>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Completion policy catalog: policies a compatible provider may be configured with.
|
|
3
|
+
*
|
|
4
|
+
* Registration is explicit and frozen. The module refuses to load when a key and the policy's own
|
|
5
|
+
* identifier disagree, so a lookup can never return a policy under a false name.
|
|
6
|
+
*
|
|
7
|
+
* Main exports: `completionPolicyCatalog`, the two preset re-exports, the shared call collector
|
|
8
|
+
* and their contract types.
|
|
9
|
+
*/
|
|
10
|
+
import { breakWhile } from '../../../utils/rules.js';
|
|
11
|
+
import { defaultCompletionPolicy } from './default.js';
|
|
12
|
+
import { sessionCompletionPolicy } from './session.js';
|
|
13
|
+
export { createToolCallCollector } from './calls.js';
|
|
14
|
+
export { defaultCompletionPolicy } from './default.js';
|
|
15
|
+
export { sessionCompletionPolicy } from './session.js';
|
|
16
|
+
/** Preset policies selected by an explicit port; the default is used when none is given. */
|
|
17
|
+
export const completionPolicyCatalog = Object.freeze({
|
|
18
|
+
default: defaultCompletionPolicy,
|
|
19
|
+
session: sessionCompletionPolicy,
|
|
20
|
+
});
|
|
21
|
+
for (const [id, policy] of Object.entries(completionPolicyCatalog))
|
|
22
|
+
breakWhile(id !== policy.id, 'Completion policy ID does not match its registered policy');
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standard session completion policy: one policy covering the documented `tool_choice` modes.
|
|
3
|
+
*
|
|
4
|
+
* Selection is explicit; consumers that do not opt in keep the default policy. Rules follow the
|
|
5
|
+
* effective `tool_choice` of the request body, so a host override is judged as the upstream saw it.
|
|
6
|
+
* An omitted field keeps the pre-policy rules instead of being read as `required`.
|
|
7
|
+
*
|
|
8
|
+
* Exports: `sessionCompletionPolicy`.
|
|
9
|
+
*/
|
|
10
|
+
import type { CompletionPolicy } from '../../../structs/support/model/completion.js';
|
|
11
|
+
export declare const sessionCompletionPolicy: CompletionPolicy;
|