@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
|
@@ -4,19 +4,43 @@ exports.chatCompletionFunctionOptions = chatCompletionFunctionOptions;
|
|
|
4
4
|
exports.chatCompletionParts = chatCompletionParts;
|
|
5
5
|
exports.chatCompletionMessages = chatCompletionMessages;
|
|
6
6
|
const errors_js_1 = require("../../structs/errors.js");
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Build the tool-selection request fields for a function-calling stage.
|
|
9
|
+
*
|
|
10
|
+
* The adapter expects exactly one function call per response, so tool use is required and parallel
|
|
11
|
+
* calls are disabled.
|
|
12
|
+
*
|
|
13
|
+
* @param options Model options; absent `functions` means the stage has no tools.
|
|
14
|
+
* @returns Tool fields to merge into the request body, or an empty object.
|
|
15
|
+
* @throws {TypeError} When `functions` is present but empty.
|
|
16
|
+
*/
|
|
8
17
|
function chatCompletionFunctionOptions(options) {
|
|
9
18
|
if (!options.functions)
|
|
10
19
|
return {};
|
|
11
20
|
if (!options.functions.length)
|
|
12
21
|
throw new TypeError('Empty model function catalog');
|
|
13
22
|
return {
|
|
14
|
-
tools: options.functions.map(fn => ({ type: 'function', function: fn })),
|
|
15
|
-
tool_choice: 'required',
|
|
23
|
+
tools: options.functions.map((fn) => ({ type: 'function', function: fn })),
|
|
24
|
+
tool_choice: 'required',
|
|
25
|
+
parallel_tool_calls: false,
|
|
16
26
|
};
|
|
17
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Convert one streamed `choice.delta` into Core model parts.
|
|
30
|
+
*
|
|
31
|
+
* Text is emitted only when non-empty; each tool-call fragment carries its index so Core can
|
|
32
|
+
* reassemble arguments that arrive across chunks.
|
|
33
|
+
*
|
|
34
|
+
* @param delta Raw `delta` value from a chat-completions chunk.
|
|
35
|
+
* @returns Text and function-call-delta parts; an unrelated shape yields an empty array.
|
|
36
|
+
* @throws {ValidationError} When `tool_calls` or one of its entries has an invalid shape.
|
|
37
|
+
*/
|
|
18
38
|
function chatCompletionParts(delta) {
|
|
19
|
-
const fail = () => {
|
|
39
|
+
const fail = () => {
|
|
40
|
+
throw new errors_js_1.ValidationError([
|
|
41
|
+
{ code: 'MODEL_PROTOCOL', message: 'Invalid function call stream' },
|
|
42
|
+
]);
|
|
43
|
+
};
|
|
20
44
|
if (!delta || typeof delta !== 'object' || Array.isArray(delta))
|
|
21
45
|
return [];
|
|
22
46
|
const value = delta;
|
|
@@ -30,16 +54,21 @@ function chatCompletionParts(delta) {
|
|
|
30
54
|
if (!item || typeof item !== 'object' || Array.isArray(item))
|
|
31
55
|
fail();
|
|
32
56
|
const call = item;
|
|
33
|
-
if (!Number.isInteger(call.index) ||
|
|
34
|
-
|
|
35
|
-
|
|
57
|
+
if (!Number.isInteger(call.index) ||
|
|
58
|
+
call.index < 0 ||
|
|
59
|
+
(call.id !== undefined && (typeof call.id !== 'string' || !call.id)) ||
|
|
60
|
+
(call.type !== undefined && call.type !== 'function'))
|
|
36
61
|
fail();
|
|
37
|
-
if (call.function !== undefined &&
|
|
62
|
+
if (call.function !== undefined &&
|
|
63
|
+
(!call.function || typeof call.function !== 'object' || Array.isArray(call.function)))
|
|
38
64
|
fail();
|
|
39
65
|
const fn = (call.function ?? {});
|
|
40
|
-
if ((fn.name !== undefined && typeof fn.name !== 'string') ||
|
|
66
|
+
if ((fn.name !== undefined && typeof fn.name !== 'string') ||
|
|
67
|
+
(fn.arguments !== undefined && typeof fn.arguments !== 'string'))
|
|
41
68
|
fail();
|
|
42
|
-
parts.push({
|
|
69
|
+
parts.push({
|
|
70
|
+
type: 'function-call-delta',
|
|
71
|
+
index: call.index,
|
|
43
72
|
...(call.id !== undefined ? { id: call.id } : {}),
|
|
44
73
|
...(fn.name !== undefined ? { name: fn.name } : {}),
|
|
45
74
|
...(fn.arguments !== undefined ? { arguments: fn.arguments } : {}),
|
|
@@ -48,21 +77,64 @@ function chatCompletionParts(delta) {
|
|
|
48
77
|
}
|
|
49
78
|
return parts;
|
|
50
79
|
}
|
|
51
|
-
/**
|
|
80
|
+
/**
|
|
81
|
+
* Map Core and provider messages to chat-completions message payloads.
|
|
82
|
+
*
|
|
83
|
+
* Only declared message fields are sent. Assistant tool declarations become nested
|
|
84
|
+
* `tool_calls`, and tool results become `tool_call_id`; both are rewritten explicitly rather than
|
|
85
|
+
* forwarded as-is. Messages with images become a content-block array; images are accepted only on
|
|
86
|
+
* user messages and only as `http(s)` or `data:image` URLs.
|
|
87
|
+
*
|
|
88
|
+
* @param messages Messages in prompt order, either Core messages or provider tool-thread messages.
|
|
89
|
+
* @returns Wire messages with `images` removed and content blocks where needed.
|
|
90
|
+
* @throws {TypeError} When a role and field do not belong together, or an image URL or detail level
|
|
91
|
+
* is invalid.
|
|
92
|
+
*/
|
|
52
93
|
function chatCompletionMessages(messages) {
|
|
53
|
-
return messages.map((
|
|
94
|
+
return messages.map((message) => {
|
|
95
|
+
const { role, content, images, toolCalls, toolCallId } = message;
|
|
96
|
+
if (!['system', 'user', 'assistant', 'tool'].includes(role) || typeof content !== 'string')
|
|
97
|
+
throw new TypeError('Invalid provider message role or content');
|
|
98
|
+
if (toolCallId !== undefined && role !== 'tool')
|
|
99
|
+
throw new TypeError('Tool results require the tool role');
|
|
100
|
+
if (role === 'tool' && (typeof toolCallId !== 'string' || !toolCallId))
|
|
101
|
+
throw new TypeError('Tool results require a tool call id');
|
|
102
|
+
if (toolCalls !== undefined && role !== 'assistant')
|
|
103
|
+
throw new TypeError('Tool calls require an assistant message');
|
|
104
|
+
const wire = { role, content };
|
|
105
|
+
if (toolCallId !== undefined)
|
|
106
|
+
wire.tool_call_id = toolCallId;
|
|
107
|
+
if (toolCalls !== undefined)
|
|
108
|
+
wire.tool_calls = toolCalls.map((call) => {
|
|
109
|
+
if (!call ||
|
|
110
|
+
typeof call.id !== 'string' ||
|
|
111
|
+
!call.id ||
|
|
112
|
+
typeof call.name !== 'string' ||
|
|
113
|
+
!call.name ||
|
|
114
|
+
typeof call.arguments !== 'string')
|
|
115
|
+
throw new TypeError('Invalid tool call');
|
|
116
|
+
return {
|
|
117
|
+
id: call.id,
|
|
118
|
+
type: 'function',
|
|
119
|
+
function: { name: call.name, arguments: call.arguments },
|
|
120
|
+
};
|
|
121
|
+
});
|
|
54
122
|
if (!images?.length)
|
|
55
|
-
return
|
|
56
|
-
if (
|
|
123
|
+
return wire;
|
|
124
|
+
if (role !== 'user')
|
|
57
125
|
throw new TypeError('Images require a user message');
|
|
58
126
|
for (const image of images) {
|
|
59
|
-
if (!image.url ||
|
|
60
|
-
|
|
127
|
+
if (!image.url ||
|
|
128
|
+
!/^(https?:\/\/|data:image\/)/i.test(image.url) ||
|
|
129
|
+
(image.detail !== undefined && !['auto', 'low', 'high', 'original'].includes(image.detail)))
|
|
61
130
|
throw new TypeError('Invalid model image');
|
|
62
131
|
}
|
|
63
|
-
return {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
132
|
+
return {
|
|
133
|
+
...wire,
|
|
134
|
+
content: [
|
|
135
|
+
{ type: 'text', text: content },
|
|
136
|
+
...images.map((image) => ({ type: 'image_url', image_url: image })),
|
|
137
|
+
],
|
|
138
|
+
};
|
|
67
139
|
});
|
|
68
140
|
}
|
|
@@ -4,17 +4,41 @@ exports.runChildren = runChildren;
|
|
|
4
4
|
const cancellation_js_1 = require("../../utils/cancellation.js");
|
|
5
5
|
const synchronous_js_1 = require("../../utils/synchronous.js");
|
|
6
6
|
const rules_js_1 = require("../../utils/rules.js");
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Run child tasks concurrently and return their values in declaration order.
|
|
9
|
+
*
|
|
10
|
+
* The first failure aborts the remaining group, but an already in-flight commit is still awaited
|
|
11
|
+
* because cancellation does not mean the write was rolled back.
|
|
12
|
+
*
|
|
13
|
+
* @param parent Parent runner supplying child creation, limits and cancellation.
|
|
14
|
+
* @param tasks Tasks to run; IDs must be unique and non-blank.
|
|
15
|
+
* @param options Concurrency, extra cancellation and accept/commit hooks.
|
|
16
|
+
* @returns Frozen values in declaration order.
|
|
17
|
+
* @throws {Error} When the concurrency, a task declaration or a duplicate ID is invalid.
|
|
18
|
+
* @throws The first task's error, or the combined signal's abort reason.
|
|
19
|
+
*/
|
|
8
20
|
async function runChildren(parent, tasks, options = {}) {
|
|
9
21
|
const concurrency = options.concurrency ?? parent.childLimits.maxConcurrentCalls;
|
|
10
|
-
(0, rules_js_1.breakWhile)(!Number.isSafeInteger(concurrency) ||
|
|
22
|
+
(0, rules_js_1.breakWhile)(!Number.isSafeInteger(concurrency) ||
|
|
23
|
+
concurrency < 1 ||
|
|
24
|
+
concurrency > parent.childLimits.maxConcurrentCalls, 'Child group concurrency exceeds the parent policy');
|
|
11
25
|
const accept = options.accept?.bind(options), commit = options.commit?.bind(options);
|
|
12
|
-
const definitions = tasks.map(task => {
|
|
26
|
+
const definitions = tasks.map((task) => {
|
|
13
27
|
const { id, maxCalls, reserveCalls, timeoutMs, transports, diagnostics, signal, run } = task;
|
|
14
28
|
(0, rules_js_1.breakWhile)(typeof id !== 'string' || !id.trim() || typeof run !== 'function', 'Invalid child task');
|
|
15
|
-
return {
|
|
29
|
+
return {
|
|
30
|
+
id,
|
|
31
|
+
maxCalls,
|
|
32
|
+
reserveCalls,
|
|
33
|
+
timeoutMs,
|
|
34
|
+
transports: transports === undefined ? undefined : [...transports],
|
|
35
|
+
diagnostics,
|
|
36
|
+
signal,
|
|
37
|
+
run: run.bind(task),
|
|
38
|
+
};
|
|
16
39
|
});
|
|
17
|
-
(0, rules_js_1.breakWhile)(new Set(definitions.map(task => task.id)).size !== definitions.length, 'Duplicate child task id');
|
|
40
|
+
(0, rules_js_1.breakWhile)(new Set(definitions.map((task) => task.id)).size !== definitions.length, 'Duplicate child task id');
|
|
41
|
+
// One group controller so the first failure cancels every sibling worker at once.
|
|
18
42
|
const cancellation = new AbortController();
|
|
19
43
|
const signal = (0, cancellation_js_1.combineSignals)(parent.signal, cancellation.signal, options.signal);
|
|
20
44
|
signal.throwIfAborted();
|
|
@@ -25,12 +49,16 @@ async function runChildren(parent, tasks, options = {}) {
|
|
|
25
49
|
const index = next++, definition = definitions[index];
|
|
26
50
|
try {
|
|
27
51
|
signal.throwIfAborted();
|
|
28
|
-
const child = parent.createChild({
|
|
52
|
+
const child = parent.createChild({
|
|
53
|
+
...definition,
|
|
54
|
+
signal: (0, cancellation_js_1.combineSignals)(signal, definition.signal),
|
|
55
|
+
});
|
|
29
56
|
const value = await (0, cancellation_js_1.awaitWithSignal)(definition.run(child, child.signal), child.signal);
|
|
30
57
|
child.signal.throwIfAborted();
|
|
31
58
|
if (accept)
|
|
32
59
|
await (0, cancellation_js_1.awaitAssertion)(() => accept(value, definition.id), child.signal, 'Child acceptance must return void');
|
|
33
60
|
child.signal.throwIfAborted();
|
|
61
|
+
// Index by declaration position so completion order never changes the result order.
|
|
34
62
|
values[index] = value;
|
|
35
63
|
}
|
|
36
64
|
catch (error) {
|
|
@@ -1,23 +1,58 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CompatibleModelProvider = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* OpenAI-compatible HTTP provider: streaming chat completions over SSE.
|
|
6
|
+
*
|
|
7
|
+
* The provider owns the request/response lifecycle and error classification; the application
|
|
8
|
+
* supplies endpoint, credentials, optional request-body extensions and user-visible error mapping.
|
|
9
|
+
* Configuration is snapshotted so getters cannot change between validation and I/O, and every
|
|
10
|
+
* failure is recorded with the same code that is returned to the caller.
|
|
11
|
+
*
|
|
12
|
+
* Main exports: `CompatibleModelProvider` and the provider configuration/error types.
|
|
13
|
+
*/
|
|
4
14
|
const errors_js_1 = require("../../errors.js");
|
|
5
15
|
const defaults_js_1 = require("../../model/defaults.js");
|
|
6
16
|
const options_js_1 = require("../../model/options.js");
|
|
7
17
|
const index_js_1 = require("../../diagnostics/index.js");
|
|
8
18
|
const chat_provider_js_1 = require("../../structs/model/chat-provider.js");
|
|
9
19
|
const chat_completions_js_1 = require("./chat-completions.js");
|
|
20
|
+
const default_js_1 = require("./completion/default.js");
|
|
10
21
|
const sse_js_1 = require("../transport/sse.js");
|
|
11
22
|
const observer_js_1 = require("./observer.js");
|
|
12
23
|
const synchronous_js_1 = require("../../utils/synchronous.js");
|
|
13
24
|
const cancellation_js_1 = require("../../utils/cancellation.js");
|
|
25
|
+
/**
|
|
26
|
+
* HTTP status to provider error code.
|
|
27
|
+
*
|
|
28
|
+
* Unlisted statuses fall back to `MODEL_UPSTREAM`; 408, 429 and 5xx are additionally marked
|
|
29
|
+
* retryable when the error context is recorded.
|
|
30
|
+
*/
|
|
14
31
|
const httpFailures = Object.freeze({
|
|
15
|
-
400: 'MODEL_CONFIG',
|
|
16
|
-
|
|
32
|
+
400: 'MODEL_CONFIG',
|
|
33
|
+
401: 'MODEL_AUTH',
|
|
34
|
+
402: 'MODEL_BALANCE',
|
|
35
|
+
403: 'MODEL_AUTH',
|
|
36
|
+
404: 'MODEL_CONFIG',
|
|
37
|
+
429: 'MODEL_BUSY',
|
|
17
38
|
});
|
|
18
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Request fields the adapter composes itself.
|
|
41
|
+
*
|
|
42
|
+
* Allowing `requestBody` to set them would let application configuration replace the model,
|
|
43
|
+
* prompt, streaming mode or output budget that Core already validated.
|
|
44
|
+
*/
|
|
45
|
+
const reservedRequestFields = [
|
|
46
|
+
'model',
|
|
47
|
+
'messages',
|
|
48
|
+
'stream',
|
|
49
|
+
'max_tokens',
|
|
50
|
+
'max_completion_tokens',
|
|
51
|
+
'toJSON',
|
|
52
|
+
];
|
|
19
53
|
function assertRequestExtensions(config) {
|
|
20
|
-
if (config.maxOutputTokens !== undefined &&
|
|
54
|
+
if (config.maxOutputTokens !== undefined &&
|
|
55
|
+
(!Number.isSafeInteger(config.maxOutputTokens) || config.maxOutputTokens < 1)) {
|
|
21
56
|
throw new TypeError('Provider maxOutputTokens must be a positive safe integer');
|
|
22
57
|
}
|
|
23
58
|
for (const key of reservedRequestFields) {
|
|
@@ -70,12 +105,18 @@ function snapshotConfiguration(config, signal) {
|
|
|
70
105
|
generated = { enable_thinking: level !== 'off' };
|
|
71
106
|
else if (format === 'thinking-budget') {
|
|
72
107
|
const configured = read(reasoning, 'budgets');
|
|
73
|
-
const budgets = {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
108
|
+
const budgets = {
|
|
109
|
+
low: configured ? (read(configured, 'low') ?? 1024) : 1024,
|
|
110
|
+
medium: configured ? (read(configured, 'medium') ?? 4096) : 4096,
|
|
111
|
+
high: configured ? (read(configured, 'high') ?? 8192) : 8192,
|
|
112
|
+
};
|
|
113
|
+
if (Object.values(budgets).some((value) => !Number.isSafeInteger(value) || value < 1))
|
|
77
114
|
throw new TypeError('Thinking budgets must be positive safe integers');
|
|
78
|
-
generated = {
|
|
115
|
+
generated = {
|
|
116
|
+
thinking: level === 'off'
|
|
117
|
+
? { type: 'disabled' }
|
|
118
|
+
: { type: 'enabled', budget_tokens: budgets[level] },
|
|
119
|
+
};
|
|
79
120
|
}
|
|
80
121
|
else
|
|
81
122
|
throw new TypeError('Unknown provider reasoning format');
|
|
@@ -94,10 +135,22 @@ function snapshotConfiguration(config, signal) {
|
|
|
94
135
|
throw error;
|
|
95
136
|
}
|
|
96
137
|
}
|
|
97
|
-
/**
|
|
138
|
+
/**
|
|
139
|
+
* HTTP/SSE lifecycle shared by hosts. Apps supply config and user-visible error mapping only.
|
|
140
|
+
*
|
|
141
|
+
* A static configuration is validated and frozen in the constructor. A configuration function is
|
|
142
|
+
* called once per request instead, so credentials and models may rotate between calls.
|
|
143
|
+
*/
|
|
98
144
|
class CompatibleModelProvider extends chat_provider_js_1.ChatProvider {
|
|
99
145
|
configuration;
|
|
100
146
|
ports;
|
|
147
|
+
/**
|
|
148
|
+
* Create a provider.
|
|
149
|
+
*
|
|
150
|
+
* @param options Static or per-request configuration, optional ports and observation/error hooks.
|
|
151
|
+
* @throws {TypeError} When a static configuration is invalid (for example `maxTokens`,
|
|
152
|
+
* reserved request-body fields, unsupported reasoning format or invalid budgets).
|
|
153
|
+
*/
|
|
101
154
|
constructor(options) {
|
|
102
155
|
super();
|
|
103
156
|
const { config: configuration, ports } = options;
|
|
@@ -105,6 +158,7 @@ class CompatibleModelProvider extends chat_provider_js_1.ChatProvider {
|
|
|
105
158
|
request: ports?.request?.bind(ports),
|
|
106
159
|
ensureJsonInstruction: ports?.ensureJsonInstruction,
|
|
107
160
|
allowEmptyJson: ports?.allowEmptyJson,
|
|
161
|
+
completionPolicy: ports?.completionPolicy,
|
|
108
162
|
observer: options.observe?.provider,
|
|
109
163
|
error: options.errors?.failure?.bind(options.errors),
|
|
110
164
|
};
|
|
@@ -114,11 +168,33 @@ class CompatibleModelProvider extends chat_provider_js_1.ChatProvider {
|
|
|
114
168
|
this.configuration = snapshotConfiguration(configuration);
|
|
115
169
|
}
|
|
116
170
|
failure(code, status, reason) {
|
|
117
|
-
const error = this.ports.error?.({
|
|
118
|
-
|
|
119
|
-
|
|
171
|
+
const error = this.ports.error?.({
|
|
172
|
+
code,
|
|
173
|
+
...(status !== undefined ? { status } : {}),
|
|
174
|
+
...(reason ? { reason } : {}),
|
|
175
|
+
}) ?? Object.assign(new Error(code), { code, status, reason });
|
|
176
|
+
(0, errors_js_1.recordErrorContext)(error, {
|
|
177
|
+
codes: [code],
|
|
178
|
+
// Transient HTTP conditions are retryable; protocol/format failures are not.
|
|
179
|
+
...(status === undefined
|
|
180
|
+
? {}
|
|
181
|
+
: { isRetryable: status === 408 || status === 429 || status >= 500 }),
|
|
182
|
+
});
|
|
120
183
|
return error;
|
|
121
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Stream one chat-completions response as Core model parts.
|
|
187
|
+
*
|
|
188
|
+
* The method validates the effective request and yields parts as they arrive. Whether the
|
|
189
|
+
* finished stream is acceptable is decided by the configured completion policy, which defaults
|
|
190
|
+
* to the legacy rules. A stream that ends without the end-of-stream marker is incomplete, not a
|
|
191
|
+
* partial success.
|
|
192
|
+
*
|
|
193
|
+
* @param messages Prompt messages in order, including provider tool-thread messages.
|
|
194
|
+
* @param context Provider context with signal, call options and diagnostics/observer ports.
|
|
195
|
+
* @yields Text chunks and function-call deltas as they arrive.
|
|
196
|
+
* @throws The classified provider error (see `MODEL_*` codes) or the signal's abort reason.
|
|
197
|
+
*/
|
|
122
198
|
async *stream(messages, context) {
|
|
123
199
|
const { signal } = context;
|
|
124
200
|
signal.throwIfAborted();
|
|
@@ -131,11 +207,14 @@ class CompatibleModelProvider extends chat_provider_js_1.ChatProvider {
|
|
|
131
207
|
throw new TypeError('Use maxOutputTokens');
|
|
132
208
|
options = (0, options_js_1.snapshotModelOptions)(configured);
|
|
133
209
|
signal.throwIfAborted();
|
|
134
|
-
if (options.maxOutputTokens !== undefined &&
|
|
210
|
+
if (options.maxOutputTokens !== undefined &&
|
|
211
|
+
(!Number.isSafeInteger(options.maxOutputTokens) || options.maxOutputTokens < 1)) {
|
|
135
212
|
throw new TypeError('maxOutputTokens must be a positive safe integer');
|
|
136
213
|
}
|
|
137
|
-
config =
|
|
138
|
-
|
|
214
|
+
config =
|
|
215
|
+
typeof this.configuration === 'function'
|
|
216
|
+
? snapshotConfiguration(this.configuration(), signal)
|
|
217
|
+
: this.configuration;
|
|
139
218
|
signal.throwIfAborted();
|
|
140
219
|
}
|
|
141
220
|
catch (error) {
|
|
@@ -144,9 +223,14 @@ class CompatibleModelProvider extends chat_provider_js_1.ChatProvider {
|
|
|
144
223
|
}
|
|
145
224
|
const maxOutputTokens = Math.min(options.maxOutputTokens ?? config.maxOutputTokens ?? defaults_js_1.modelRunnerDefaults.maxOutputTokens, config.maxOutputTokens ?? Infinity);
|
|
146
225
|
const thinking = config.requestBody?.thinking;
|
|
147
|
-
if (thinking !== null &&
|
|
226
|
+
if (thinking !== null &&
|
|
227
|
+
typeof thinking === 'object' &&
|
|
228
|
+
Object.hasOwn(thinking, 'budget_tokens')) {
|
|
148
229
|
const budget = thinking.budget_tokens;
|
|
149
|
-
if (typeof budget !== 'number' ||
|
|
230
|
+
if (typeof budget !== 'number' ||
|
|
231
|
+
!Number.isSafeInteger(budget) ||
|
|
232
|
+
budget < 1 ||
|
|
233
|
+
budget >= maxOutputTokens) {
|
|
150
234
|
throw new TypeError('Thinking budget_tokens must be positive and smaller than the effective output token limit');
|
|
151
235
|
}
|
|
152
236
|
}
|
|
@@ -156,61 +240,99 @@ class CompatibleModelProvider extends chat_provider_js_1.ChatProvider {
|
|
|
156
240
|
try {
|
|
157
241
|
if (!config.apiKey)
|
|
158
242
|
throw this.failure('MODEL_NOT_CONFIGURED');
|
|
159
|
-
|
|
160
|
-
|
|
243
|
+
// Some providers require the word "json" in the prompt before accepting a JSON response
|
|
244
|
+
// format; only add the instruction when the prompt does not already mention it.
|
|
245
|
+
const needsJsonInstruction = this.ports.ensureJsonInstruction &&
|
|
246
|
+
!options.functions &&
|
|
247
|
+
options.json &&
|
|
248
|
+
!messages.some((message) => ['system', 'user'].includes(message.role) && /json/i.test(message.content));
|
|
161
249
|
const requestMessages = needsJsonInstruction
|
|
162
|
-
? [{ role: 'system', content: '只输出合法 JSON 对象,遵循给定字段和示例。' }, ...messages]
|
|
250
|
+
? [{ role: 'system', content: '只输出合法 JSON 对象,遵循给定字段和示例。' }, ...messages]
|
|
251
|
+
: messages;
|
|
163
252
|
const request = this.ports.request ?? fetch;
|
|
253
|
+
// Build the body once: the completion policy must judge the same fields the upstream reads.
|
|
254
|
+
const body = {
|
|
255
|
+
model: config.model,
|
|
256
|
+
messages: (0, chat_completions_js_1.chatCompletionMessages)(requestMessages),
|
|
257
|
+
stream: true,
|
|
258
|
+
max_tokens: maxOutputTokens,
|
|
259
|
+
...((options.temperature ?? config.temperature) !== undefined
|
|
260
|
+
? { temperature: options.temperature ?? config.temperature }
|
|
261
|
+
: {}),
|
|
262
|
+
...(!options.functions && options.json ? { response_format: { type: 'json_object' } } : {}),
|
|
263
|
+
...(0, chat_completions_js_1.chatCompletionFunctionOptions)(options),
|
|
264
|
+
...config.requestBody,
|
|
265
|
+
...(options.includeUsage ? { stream_options: { include_usage: true } } : {}),
|
|
266
|
+
};
|
|
267
|
+
const policy = this.ports.completionPolicy ?? default_js_1.defaultCompletionPolicy;
|
|
268
|
+
const session = policy.begin({
|
|
269
|
+
functions: !!options.functions,
|
|
270
|
+
toolChoice: body.tool_choice,
|
|
271
|
+
allowEmptyText: !options.functions && !!options.json && !!this.ports.allowEmptyJson,
|
|
272
|
+
}, (code) => this.failure(code));
|
|
164
273
|
const observedRequest = (url, init) => {
|
|
165
274
|
observation.emit('provider.request', () => {
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
275
|
+
const observed = JSON.parse(String(init?.body));
|
|
276
|
+
observed.messages = observed.messages.map((message) => ({
|
|
277
|
+
...message,
|
|
278
|
+
content: Array.isArray(message.content)
|
|
279
|
+
? message.content.map((part) => part.type === 'image_url'
|
|
280
|
+
? { ...part, image_url: { url: '[image omitted]' } }
|
|
281
|
+
: part)
|
|
282
|
+
: message.content,
|
|
283
|
+
}));
|
|
284
|
+
return { url: String(url), body: observed };
|
|
170
285
|
});
|
|
171
286
|
signal.throwIfAborted();
|
|
172
287
|
return request(url, init);
|
|
173
288
|
};
|
|
174
289
|
signal.throwIfAborted();
|
|
175
290
|
const pendingResponse = Promise.resolve(observedRequest(`${config.baseUrl.replace(/\/+$/, '')}/chat/completions`, {
|
|
176
|
-
method: 'POST',
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
})).then(response => activeResponse(response, signal));
|
|
291
|
+
method: 'POST',
|
|
292
|
+
redirect: 'error',
|
|
293
|
+
signal,
|
|
294
|
+
headers: {
|
|
295
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
296
|
+
'Content-Type': 'application/json',
|
|
297
|
+
Accept: 'text/event-stream',
|
|
298
|
+
},
|
|
299
|
+
body: JSON.stringify(body),
|
|
300
|
+
})).then((response) => activeResponse(response, signal));
|
|
186
301
|
const response = await (0, cancellation_js_1.awaitWithSignal)(pendingResponse, signal);
|
|
187
|
-
observation.emit('provider.response', () => ({
|
|
302
|
+
observation.emit('provider.response', () => ({
|
|
303
|
+
status: response.status,
|
|
304
|
+
requestId: response.headers.get('x-request-id'),
|
|
305
|
+
contentType: response.headers.get('content-type'),
|
|
306
|
+
}));
|
|
188
307
|
activeResponse(response, signal);
|
|
189
308
|
if (!response.ok) {
|
|
190
309
|
if (observation.enabled('provider.error-body')) {
|
|
191
310
|
try {
|
|
192
311
|
const capture = await captureErrorBody(response);
|
|
193
|
-
observation.emit('provider.error-body', () => ({
|
|
312
|
+
observation.emit('provider.error-body', () => ({
|
|
313
|
+
status: response.status,
|
|
314
|
+
...capture,
|
|
315
|
+
}));
|
|
316
|
+
}
|
|
317
|
+
catch {
|
|
318
|
+
/* Error capture cannot replace the HTTP failure. */
|
|
194
319
|
}
|
|
195
|
-
catch { /* Error capture cannot replace the HTTP failure. */ }
|
|
196
320
|
}
|
|
197
321
|
else
|
|
198
322
|
discardResponse(response);
|
|
199
323
|
throw this.failure(httpFailures[response.status] ?? 'MODEL_UPSTREAM', response.status);
|
|
200
324
|
}
|
|
201
|
-
if (!response.body ||
|
|
325
|
+
if (!response.body ||
|
|
326
|
+
!response.headers.get('content-type')?.toLowerCase().startsWith('text/event-stream')) {
|
|
202
327
|
discardResponse(response);
|
|
203
328
|
throw this.failure('MODEL_FORMAT');
|
|
204
329
|
}
|
|
205
330
|
let finished = false;
|
|
206
|
-
let hasFunction = false;
|
|
207
|
-
let hasText = false;
|
|
208
331
|
for await (const event of (0, sse_js_1.readSse)(response.body, { signal, waitForCancel: false })) {
|
|
209
332
|
observation.emit('provider.frame', () => ({ data: event.data }));
|
|
210
333
|
signal.throwIfAborted();
|
|
211
334
|
if (event.data === '[DONE]') {
|
|
212
|
-
|
|
213
|
-
throw this.failure('MODEL_INCOMPLETE');
|
|
335
|
+
session.end();
|
|
214
336
|
complete = true;
|
|
215
337
|
return;
|
|
216
338
|
}
|
|
@@ -226,11 +348,17 @@ class CompatibleModelProvider extends chat_provider_js_1.ChatProvider {
|
|
|
226
348
|
if (chunk.error)
|
|
227
349
|
throw this.failure('MODEL_UPSTREAM');
|
|
228
350
|
const usage = chunk.usage;
|
|
229
|
-
if (usage &&
|
|
230
|
-
|
|
351
|
+
if (usage &&
|
|
352
|
+
[usage.prompt_tokens, usage.completion_tokens, usage.total_tokens].every((value) => Number.isInteger(value) && value >= 0))
|
|
353
|
+
options.onUsage?.({
|
|
354
|
+
promptTokens: usage.prompt_tokens,
|
|
355
|
+
completionTokens: usage.completion_tokens,
|
|
356
|
+
totalTokens: usage.total_tokens,
|
|
357
|
+
});
|
|
231
358
|
signal.throwIfAborted();
|
|
232
359
|
if (chunk.choices !== undefined && !Array.isArray(chunk.choices))
|
|
233
360
|
throw this.failure('MODEL_FORMAT');
|
|
361
|
+
// One request produces one choice; extra choices are ignored rather than merged.
|
|
234
362
|
const choice = chunk.choices?.find((item) => item?.index === 0);
|
|
235
363
|
if (!choice)
|
|
236
364
|
continue;
|
|
@@ -239,20 +367,13 @@ class CompatibleModelProvider extends chat_provider_js_1.ChatProvider {
|
|
|
239
367
|
const parts = (0, chat_completions_js_1.chatCompletionParts)(choice.delta);
|
|
240
368
|
for (const part of parts) {
|
|
241
369
|
signal.throwIfAborted();
|
|
242
|
-
|
|
243
|
-
hasText = true;
|
|
244
|
-
else {
|
|
245
|
-
if (!options.functions)
|
|
246
|
-
throw this.failure('MODEL_PROTOCOL');
|
|
247
|
-
hasFunction = true;
|
|
248
|
-
}
|
|
370
|
+
session.part(part);
|
|
249
371
|
yield part;
|
|
250
372
|
}
|
|
251
373
|
if (choice.finish_reason != null) {
|
|
252
374
|
if (finished)
|
|
253
375
|
throw this.failure('MODEL_PROTOCOL');
|
|
254
|
-
|
|
255
|
-
throw this.failure(choice.finish_reason === 'length' ? 'MODEL_OUTPUT_LIMIT' : 'MODEL_INCOMPLETE');
|
|
376
|
+
session.finish(choice.finish_reason);
|
|
256
377
|
finished = true;
|
|
257
378
|
}
|
|
258
379
|
}
|
|
@@ -260,11 +381,14 @@ class CompatibleModelProvider extends chat_provider_js_1.ChatProvider {
|
|
|
260
381
|
}
|
|
261
382
|
catch (error) {
|
|
262
383
|
failure = { error };
|
|
263
|
-
if (!signal.aborted && (0, errors_js_1.getErrorInfo)(error).codes.every(code => code === 'UNKNOWN_ERROR'))
|
|
384
|
+
if (!signal.aborted && (0, errors_js_1.getErrorInfo)(error).codes.every((code) => code === 'UNKNOWN_ERROR'))
|
|
264
385
|
(0, errors_js_1.recordErrorContext)(error, { codes: ['MODEL_UPSTREAM'] });
|
|
265
386
|
}
|
|
266
387
|
finally {
|
|
267
|
-
observation.emit('provider.end', () => ({
|
|
388
|
+
observation.emit('provider.end', () => ({
|
|
389
|
+
complete,
|
|
390
|
+
...(failure ? { error: (0, index_js_1.diagnosticError)(failure.error) } : {}),
|
|
391
|
+
}));
|
|
268
392
|
}
|
|
269
393
|
throw failure.error;
|
|
270
394
|
}
|
|
@@ -275,7 +399,9 @@ function discardResponse(response) {
|
|
|
275
399
|
try {
|
|
276
400
|
void Promise.resolve(response.body?.cancel()).catch(() => { });
|
|
277
401
|
}
|
|
278
|
-
catch {
|
|
402
|
+
catch {
|
|
403
|
+
/* Preserve the classified failure. */
|
|
404
|
+
}
|
|
279
405
|
}
|
|
280
406
|
function activeResponse(response, signal) {
|
|
281
407
|
if (signal.aborted) {
|
|
@@ -294,7 +420,9 @@ async function captureErrorBody(response) {
|
|
|
294
420
|
let bytes = 0;
|
|
295
421
|
let truncated = true;
|
|
296
422
|
let timer;
|
|
297
|
-
const deadline = new Promise(resolve => {
|
|
423
|
+
const deadline = new Promise((resolve) => {
|
|
424
|
+
timer = setTimeout(() => resolve(undefined), 250);
|
|
425
|
+
});
|
|
298
426
|
try {
|
|
299
427
|
while (bytes < 65536) {
|
|
300
428
|
const item = await Promise.race([reader.read(), deadline]);
|
|
@@ -318,6 +446,8 @@ async function captureErrorBody(response) {
|
|
|
318
446
|
try {
|
|
319
447
|
reader.releaseLock();
|
|
320
448
|
}
|
|
321
|
-
catch {
|
|
449
|
+
catch {
|
|
450
|
+
/* A pending read is cancelled independently. */
|
|
451
|
+
}
|
|
322
452
|
}
|
|
323
453
|
}
|