@jangts/agent-core 0.1.1 → 0.2.1
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 +25 -0
- package/LICENSE +55 -0
- package/NOTICE +6 -0
- package/README.md +8 -0
- package/development/docs/README.en.md +70 -0
- package/development/docs/README.md +1 -1
- package/development/docs/acceptance.en.md +34 -0
- package/development/docs/agent-development.en.md +42 -0
- package/development/docs/api/README.en.md +62 -0
- package/development/docs/api/advanced-agents.en.md +135 -0
- package/development/docs/api/context-processing.en.md +110 -0
- package/development/docs/api/core.en.md +274 -0
- package/development/docs/api/examples.en.md +118 -0
- package/development/docs/api/kits.en.md +183 -0
- package/development/docs/api/knowledge-answering.en.md +166 -0
- package/development/docs/api/mock.en.md +186 -0
- package/development/docs/api/observability.en.md +166 -0
- package/development/docs/api/reference.en.md +2235 -0
- package/development/docs/api/reference.md +1907 -1874
- package/development/docs/api/support.en.md +252 -0
- package/development/docs/api/support.md +27 -0
- package/development/docs/api/types.en.md +181 -0
- package/development/docs/api/workflow.en.md +188 -0
- package/development/docs/basic-usage.en.md +78 -0
- package/development/docs/create-app.en.md +76 -0
- package/development/docs/examples/offline-agent.en.md +273 -0
- package/development/docs/examples.en.md +36 -0
- package/development/docs/flows/knowledge-maintenance.en.md +38 -0
- package/development/docs/getting-started.en.md +71 -0
- package/development/docs/knowledge.en.md +89 -0
- package/development/docs/maintenance.en.md +62 -0
- package/development/docs/materials.en.md +38 -0
- package/development/docs/mechanisms.en.md +227 -0
- package/development/docs/memory-and-learning.en.md +42 -0
- package/development/docs/operations.en.md +51 -0
- package/development/docs/records.en.md +63 -0
- package/development/docs/registration.en.md +63 -0
- package/development/skills/agent-core-app/scripts/create-app.mjs +286 -70
- package/development/skills/agent-core-app/scripts/lib/knowledge.mjs +77 -16
- package/development/skills/agent-core-app/scripts/lib/source.mjs +30 -2
- package/development/skills/agent-core-app/scripts/plan.mjs +46 -12
- package/development/skills/agent-core-app/scripts/prepare-source.mjs +61 -10
- package/development/skills/agent-core-app/scripts/resources.mjs +62 -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 +17 -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 +33 -0
- package/dist/cjs/support/model/completion/index.js +28 -0
- package/dist/cjs/support/model/completion/session.js +107 -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.d.ts +6 -0
- package/dist/esm/diagnostics/catalog.js +17 -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 +39 -0
- package/dist/esm/structs/kits/search.d.ts +24 -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 +34 -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 +54 -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 +65 -3
- package/dist/esm/support/context/nodes.js +165 -36
- package/dist/esm/support/context/processing.d.ts +152 -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 +34 -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 +11 -0
- package/dist/esm/support/model/completion/default.js +30 -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 +16 -0
- package/dist/esm/support/model/completion/session.js +104 -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 +26 -2
- package/dist/esm/support/model/preview-rules.js +66 -16
- package/dist/esm/support/model/reply-preview.d.ts +22 -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 +28 -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 +7 -3
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.executeInvocation = exports.ToolRegistry = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Core tool registry: the mechanism that turns registered tools into invoked tools.
|
|
6
|
+
*
|
|
7
|
+
* `ToolRegistry` owns identity, registration-time validation, cancellation, diagnostics,
|
|
8
|
+
* output parsing and success/failure bookkeeping. It deliberately knows nothing about
|
|
9
|
+
* visibility, authorization policy or domain metadata: those are App registrations, and
|
|
10
|
+
* `authorize` is called by the typed bridge in `tool-runtime.js`.
|
|
11
|
+
*
|
|
12
|
+
* `invoke` requires a synchronous tool; `invokeAsync` awaits thenables. Both reject an
|
|
13
|
+
* unknown tool and report through the ports bound at construction.
|
|
14
|
+
*
|
|
15
|
+
* Exports: `ToolRegistry`, `executeInvocation`, and the tool/invocation contracts.
|
|
16
|
+
*/
|
|
4
17
|
const events_js_1 = require("../execution/events.js");
|
|
5
18
|
const invocation_js_1 = require("../utils/invocation.js");
|
|
6
19
|
const synchronous_js_1 = require("../utils/synchronous.js");
|
|
@@ -8,14 +21,28 @@ const cancellation_js_1 = require("../utils/cancellation.js");
|
|
|
8
21
|
const index_js_1 = require("../diagnostics/index.js");
|
|
9
22
|
const registration_js_1 = require("../utils/registration.js");
|
|
10
23
|
const reporting_js_1 = require("../execution/reporting.js");
|
|
11
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* Registry mechanism only: visibility, authorization policy, schemas and semantic metadata
|
|
26
|
+
* are host registrations. A registry is mutable until sealed and keeps its own call log.
|
|
27
|
+
*/
|
|
12
28
|
class ToolRegistry {
|
|
29
|
+
/** Insertion order is the declaration order and is preserved by every enumeration below. */
|
|
13
30
|
tools = new Map();
|
|
31
|
+
/** Sealing is one-way: a running stage must not add tools to the registry it executes. */
|
|
14
32
|
sealed = false;
|
|
33
|
+
/** In-memory execution facts for this registry's lifetime, one entry per completed call. */
|
|
15
34
|
executionEvents = [];
|
|
16
|
-
|
|
35
|
+
/** Frozen snapshot of the recorded execution facts. */
|
|
36
|
+
get events() {
|
|
37
|
+
return Object.freeze([...this.executionEvents]);
|
|
38
|
+
}
|
|
17
39
|
diagnostics;
|
|
18
40
|
ports;
|
|
41
|
+
/**
|
|
42
|
+
* @param options.execution Request-lifetime signal plus the diagnostics/observer source the
|
|
43
|
+
* registry starts from.
|
|
44
|
+
* @param options.errors Factories for the unknown-tool error and for failure codes.
|
|
45
|
+
*/
|
|
19
46
|
constructor({ execution, errors } = {}) {
|
|
20
47
|
this.ports = {
|
|
21
48
|
signal: execution?.signal,
|
|
@@ -25,42 +52,137 @@ class ToolRegistry {
|
|
|
25
52
|
};
|
|
26
53
|
this.diagnostics = this.ports.diagnostics;
|
|
27
54
|
}
|
|
28
|
-
|
|
55
|
+
/**
|
|
56
|
+
* Replace the diagnostics scope used by subsequent calls.
|
|
57
|
+
*
|
|
58
|
+
* Calls already in flight keep the scope they started with.
|
|
59
|
+
*
|
|
60
|
+
* @param diagnostics New diagnostics context, or undefined to stop emitting events.
|
|
61
|
+
*/
|
|
62
|
+
setDiagnostics(diagnostics) {
|
|
63
|
+
this.diagnostics = diagnostics;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Register one executable tool.
|
|
67
|
+
*
|
|
68
|
+
* The stored entry is frozen and its parsers are bound to the tool, so later changes on the
|
|
69
|
+
* caller's object cannot alter validation or execution.
|
|
70
|
+
*
|
|
71
|
+
* @param tool Executable tool; `id` and `kind` must be non-empty, and the parse and execute
|
|
72
|
+
* handlers must all be functions.
|
|
73
|
+
* @returns This registry, for chaining.
|
|
74
|
+
* @throws Error when the registry is sealed or the id is already registered.
|
|
75
|
+
* @throws TypeError when the tool shape is invalid.
|
|
76
|
+
*/
|
|
29
77
|
register(tool) {
|
|
30
78
|
if (this.sealed)
|
|
31
79
|
throw new Error('Tool registry is sealed');
|
|
32
80
|
const { id, kind, description, parseInput, parseOutput, execute, debugSteps } = tool;
|
|
33
|
-
if (!id?.trim() ||
|
|
81
|
+
if (!id?.trim() ||
|
|
82
|
+
!kind?.trim() ||
|
|
83
|
+
![parseInput, parseOutput, execute].every((value) => typeof value === 'function'))
|
|
34
84
|
throw new TypeError('Invalid executable tool');
|
|
35
85
|
if (this.tools.has(id))
|
|
36
86
|
throw new Error(`Duplicate registered tool: ${id}`);
|
|
37
87
|
const input = parseInput.bind(tool), output = parseOutput.bind(tool);
|
|
38
|
-
this.tools.set(id, Object.freeze({
|
|
39
|
-
|
|
40
|
-
|
|
88
|
+
this.tools.set(id, Object.freeze({
|
|
89
|
+
...(0, registration_js_1.registrationData)(tool, [
|
|
90
|
+
'id',
|
|
91
|
+
'kind',
|
|
92
|
+
'description',
|
|
93
|
+
'parseInput',
|
|
94
|
+
'parseOutput',
|
|
95
|
+
'execute',
|
|
96
|
+
'debugSteps',
|
|
97
|
+
]),
|
|
98
|
+
id,
|
|
99
|
+
kind,
|
|
100
|
+
description,
|
|
101
|
+
debugSteps: debugSteps?.bind(tool),
|
|
102
|
+
parseInput: input,
|
|
103
|
+
parseOutput: output,
|
|
104
|
+
execute: execute.bind(tool),
|
|
105
|
+
}));
|
|
41
106
|
return this;
|
|
42
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Invoke a registered tool synchronously.
|
|
110
|
+
*
|
|
111
|
+
* @param request Single request object carrying `toolId`, `input`, and optional signal and
|
|
112
|
+
* diagnostics. Exactly one argument is accepted; the arity check is runtime-enforced
|
|
113
|
+
* because callers may bypass the type system.
|
|
114
|
+
* @returns The tool result, with frozen invocation metadata.
|
|
115
|
+
* @throws TypeError when the request shape is wrong, the diagnostics belong to another run,
|
|
116
|
+
* or the tool returns a promise.
|
|
117
|
+
* @throws Error when the tool is unknown or the tool itself fails; the abort reason
|
|
118
|
+
* surfaces when the signal is cancelled.
|
|
119
|
+
*/
|
|
43
120
|
invoke(request) {
|
|
44
121
|
const call = (0, invocation_js_1.toolInvocation)(request, arguments.length);
|
|
45
122
|
return this.execute(call.toolId, call.input, false, call.diagnostics, call.signal);
|
|
46
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Invoke a registered tool and await its result.
|
|
126
|
+
*
|
|
127
|
+
* @param request Single request object, identical to `invoke`.
|
|
128
|
+
* @returns The tool result once the tool settles.
|
|
129
|
+
* @throws TypeError or Error under the same conditions as `invoke`; rejections from an
|
|
130
|
+
* asynchronous tool are propagated unchanged.
|
|
131
|
+
*/
|
|
47
132
|
async invokeAsync(request) {
|
|
48
133
|
const call = (0, invocation_js_1.toolInvocation)(request, arguments.length);
|
|
49
|
-
return await this.execute(call.toolId, call.input, true, call.diagnostics, call.signal);
|
|
134
|
+
return (await this.execute(call.toolId, call.input, true, call.diagnostics, call.signal));
|
|
50
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* Run one call: validate, parse input, execute, parse output, then record the outcome.
|
|
138
|
+
*
|
|
139
|
+
* @param name Registered tool id.
|
|
140
|
+
* @param input Untrusted caller input, parsed before the tool sees it.
|
|
141
|
+
* @param asynchronous Whether promises returned by the tool are awaited.
|
|
142
|
+
* @param parentDiagnostics Scope the call is recorded under; defaults to the registry's.
|
|
143
|
+
* @param requestSignal Per-call signal, combined with the request-lifetime signal.
|
|
144
|
+
* @returns The frozen result on success; every failure path throws.
|
|
145
|
+
*/
|
|
51
146
|
execute(name, input, asynchronous, parentDiagnostics = this.diagnostics, requestSignal) {
|
|
52
|
-
|
|
147
|
+
// A tool call stays inside the run that invoked it: mixing two diagnostic trees would
|
|
148
|
+
// produce records with two parents and break run-scoped correlation.
|
|
149
|
+
if (this.diagnostics?.identity?.runId &&
|
|
150
|
+
parentDiagnostics?.identity?.runId &&
|
|
151
|
+
this.diagnostics.identity.runId !== parentDiagnostics.identity.runId)
|
|
53
152
|
throw new TypeError('Tool diagnostics must belong to the same run');
|
|
54
153
|
const tool = this.tools.get(name);
|
|
55
154
|
if (!tool)
|
|
56
155
|
throw this.ports.unavailable?.(name) ?? new Error(`Tool is not registered: ${name}`);
|
|
156
|
+
// Either signal may cancel the call; combining them keeps both lifetimes effective.
|
|
57
157
|
const signal = (0, cancellation_js_1.combineSignals)(this.ports.signal, requestSignal);
|
|
58
158
|
const invocationId = (0, invocation_js_1.invocationId)();
|
|
59
|
-
const diagnostics = parentDiagnostics?.child(invocationId, {
|
|
159
|
+
const diagnostics = parentDiagnostics?.child(invocationId, {
|
|
160
|
+
invocationId,
|
|
161
|
+
nodeRole: 'invocation',
|
|
162
|
+
definitionKind: 'tool',
|
|
163
|
+
definitionId: name,
|
|
164
|
+
nodeId: name,
|
|
165
|
+
nodeName: name,
|
|
166
|
+
nodeKind: 'tool',
|
|
167
|
+
// Cap the persisted description so an App-supplied string cannot inflate every record.
|
|
168
|
+
nodeDescription: tool.description?.slice(0, 1024),
|
|
169
|
+
// Tool calls are not stage-scoped; explicit undefined clears inherited span fields.
|
|
170
|
+
stageId: undefined,
|
|
171
|
+
definitionRevision: undefined,
|
|
172
|
+
});
|
|
60
173
|
const reports = new reporting_js_1.NodeReports(diagnostics, signal, 'tool.result');
|
|
61
|
-
const reporting = {
|
|
62
|
-
|
|
174
|
+
const reporting = {
|
|
175
|
+
report: reports.report.bind(reports),
|
|
176
|
+
createSubFlowLogger: reports.createSubFlowLogger.bind(reports),
|
|
177
|
+
};
|
|
178
|
+
diagnostics?.emit('tool.call', () => ({
|
|
179
|
+
toolId: name,
|
|
180
|
+
invocationId,
|
|
181
|
+
...(diagnostics?.enabled('tool.call', 'content') ? { input } : {}),
|
|
182
|
+
}));
|
|
63
183
|
const started = Date.now();
|
|
184
|
+
// Track the phase for failure reports, so input or output parsing errors can be told
|
|
185
|
+
// apart from failures raised by the tool itself.
|
|
64
186
|
let phase = 'input';
|
|
65
187
|
const output = (value) => {
|
|
66
188
|
signal.throwIfAborted();
|
|
@@ -74,28 +196,82 @@ class ToolRegistry {
|
|
|
74
196
|
let debug = reports.debug();
|
|
75
197
|
if (tool.debugSteps && diagnostics?.enabled('tool.result', 'content')) {
|
|
76
198
|
try {
|
|
77
|
-
debug = reports.debug({
|
|
199
|
+
debug = reports.debug({
|
|
200
|
+
subs: (0, synchronous_js_1.synchronous)(tool.debugSteps(result, reporting), 'Tool debug reports must be synchronous'),
|
|
201
|
+
});
|
|
78
202
|
}
|
|
79
203
|
catch (error) {
|
|
80
|
-
|
|
204
|
+
// Debug material is optional evidence: it must not turn a successful tool call into
|
|
205
|
+
// a failure. Defer the error until a consumer actually asks for the sub-steps.
|
|
206
|
+
debug = {
|
|
207
|
+
usedModel: reports.debug().usedModel,
|
|
208
|
+
subSteps: () => {
|
|
209
|
+
throw error;
|
|
210
|
+
},
|
|
211
|
+
};
|
|
81
212
|
}
|
|
82
213
|
}
|
|
83
214
|
signal.throwIfAborted();
|
|
84
215
|
reports.close();
|
|
85
|
-
diagnostics?.emit('tool.result', () => ({
|
|
86
|
-
|
|
87
|
-
|
|
216
|
+
diagnostics?.emit('tool.result', () => ({
|
|
217
|
+
toolId: name,
|
|
218
|
+
invocationId,
|
|
219
|
+
status: 'success',
|
|
220
|
+
...(diagnostics?.enabled('tool.result', 'content') ? { value: result } : {}),
|
|
221
|
+
durationMs: Date.now() - started,
|
|
222
|
+
}), debug);
|
|
223
|
+
// Kept locally as well as emitted, so a run summary does not require a log consumer.
|
|
224
|
+
this.executionEvents.push(Object.freeze({
|
|
225
|
+
...diagnostics?.identity,
|
|
226
|
+
toolId: name,
|
|
227
|
+
invocationId,
|
|
228
|
+
startedAtMs: started,
|
|
229
|
+
kind: tool.kind,
|
|
230
|
+
durationMs: Date.now() - started,
|
|
231
|
+
status: 'success',
|
|
232
|
+
}));
|
|
233
|
+
return (0, invocation_js_1.invocationResult)(result, {
|
|
234
|
+
...diagnostics?.identity,
|
|
235
|
+
invocationId,
|
|
236
|
+
toolId: name,
|
|
237
|
+
startedAtMs: started,
|
|
238
|
+
durationMs: Date.now() - started,
|
|
239
|
+
status: 'success',
|
|
240
|
+
});
|
|
88
241
|
};
|
|
89
242
|
const fail = (error) => {
|
|
90
243
|
reports.close();
|
|
91
|
-
|
|
92
|
-
diagnostics?.emit('
|
|
244
|
+
// Rule-level record carries the failing phase, which the tool result does not.
|
|
245
|
+
diagnostics?.emit('rule.result', () => ({
|
|
246
|
+
tags: [name, phase],
|
|
247
|
+
error: (0, index_js_1.diagnosticError)(error),
|
|
248
|
+
}));
|
|
249
|
+
diagnostics?.emit('tool.result', () => ({
|
|
250
|
+
toolId: name,
|
|
251
|
+
invocationId,
|
|
252
|
+
status: signal.aborted ? 'cancelled' : 'error',
|
|
253
|
+
...(diagnostics?.enabled('tool.result', 'content')
|
|
254
|
+
? { error: (0, index_js_1.diagnosticError)(error) }
|
|
255
|
+
: {}),
|
|
256
|
+
durationMs: Date.now() - started,
|
|
257
|
+
}), reports.debug());
|
|
93
258
|
let errorCode = signal.aborted ? 'CANCELLED' : 'TOOL_ERROR';
|
|
94
259
|
try {
|
|
95
260
|
errorCode = this.ports.errorCode?.(error) ?? errorCode;
|
|
96
261
|
}
|
|
97
|
-
catch {
|
|
98
|
-
|
|
262
|
+
catch {
|
|
263
|
+
/* Error decoration cannot replace the original failure. */
|
|
264
|
+
}
|
|
265
|
+
this.executionEvents.push(Object.freeze({
|
|
266
|
+
...diagnostics?.identity,
|
|
267
|
+
toolId: name,
|
|
268
|
+
invocationId,
|
|
269
|
+
startedAtMs: started,
|
|
270
|
+
kind: tool.kind,
|
|
271
|
+
durationMs: Date.now() - started,
|
|
272
|
+
status: signal.aborted ? 'cancelled' : 'error',
|
|
273
|
+
...(errorCode ? { errorCode } : {}),
|
|
274
|
+
}));
|
|
99
275
|
throw error;
|
|
100
276
|
};
|
|
101
277
|
try {
|
|
@@ -103,8 +279,16 @@ class ToolRegistry {
|
|
|
103
279
|
const parsed = (0, synchronous_js_1.synchronous)(tool.parseInput(input), 'Tool input parsers must be synchronous');
|
|
104
280
|
signal.throwIfAborted();
|
|
105
281
|
phase = 'execute';
|
|
106
|
-
const value = tool.execute(parsed, {
|
|
282
|
+
const value = tool.execute(parsed, {
|
|
283
|
+
mode: asynchronous ? 'async' : 'sync',
|
|
284
|
+
invocationId,
|
|
285
|
+
signal,
|
|
286
|
+
diagnostics,
|
|
287
|
+
...reporting,
|
|
288
|
+
});
|
|
107
289
|
if (asynchronous)
|
|
290
|
+
// Async completions are routed to `fail` explicitly, so bookkeeping runs exactly once
|
|
291
|
+
// whether the tool settles immediately or later.
|
|
108
292
|
return (0, cancellation_js_1.awaitWithSignal)(value, signal).then(output).then(succeed).catch(fail);
|
|
109
293
|
return succeed(output((0, synchronous_js_1.synchronous)(value, 'Synchronous tools must not return a Promise')));
|
|
110
294
|
}
|
|
@@ -112,20 +296,71 @@ class ToolRegistry {
|
|
|
112
296
|
return fail(error);
|
|
113
297
|
}
|
|
114
298
|
}
|
|
299
|
+
/**
|
|
300
|
+
* Register several tools.
|
|
301
|
+
*
|
|
302
|
+
* @param tools Executable tools applied in iteration order.
|
|
303
|
+
* @returns This registry, for chaining.
|
|
304
|
+
* @throws Error or TypeError under the same conditions as `register`.
|
|
305
|
+
*/
|
|
115
306
|
registerAll(tools) {
|
|
116
307
|
for (const tool of tools)
|
|
117
308
|
this.register(tool);
|
|
118
309
|
return this;
|
|
119
310
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
311
|
+
/**
|
|
312
|
+
* @param name Tool id.
|
|
313
|
+
* @returns The registered tool, or undefined when absent.
|
|
314
|
+
*/
|
|
315
|
+
get(name) {
|
|
316
|
+
return this.tools.get(name);
|
|
317
|
+
}
|
|
318
|
+
/** Frozen snapshot of the registered tools, in declaration order. */
|
|
319
|
+
get definitions() {
|
|
320
|
+
return Object.freeze([...this.tools.values()]);
|
|
321
|
+
}
|
|
322
|
+
/** Registered tool ids in declaration order. */
|
|
323
|
+
ids() {
|
|
324
|
+
return Object.freeze([...this.tools.keys()]);
|
|
325
|
+
}
|
|
326
|
+
/** Live iterator over `[id, tool]` pairs; it reflects only entries added before sealing. */
|
|
327
|
+
entries() {
|
|
328
|
+
return this.tools.entries();
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* @param id Tool id.
|
|
332
|
+
* @returns The registered tool.
|
|
333
|
+
* @throws Error when the tool is unknown; the App-supplied `unavailable` factory can
|
|
334
|
+
* replace the default message.
|
|
335
|
+
*/
|
|
336
|
+
require(id) {
|
|
337
|
+
const tool = this.get(id);
|
|
338
|
+
if (!tool)
|
|
339
|
+
throw this.ports.unavailable?.(id) ?? new Error(`Tool is not registered: ${id}`);
|
|
340
|
+
return tool;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* @param name Tool id.
|
|
344
|
+
* @returns Whether the id is registered in this registry.
|
|
345
|
+
*/
|
|
346
|
+
has(name) {
|
|
347
|
+
return this.tools.has(name);
|
|
348
|
+
}
|
|
349
|
+
/** Live iterator over the registered tools. */
|
|
350
|
+
values() {
|
|
351
|
+
return this.tools.values();
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Close the registry to further registration.
|
|
355
|
+
*
|
|
356
|
+
* Sealing is idempotent and does not affect existing tools.
|
|
357
|
+
*
|
|
358
|
+
* @returns This registry.
|
|
359
|
+
*/
|
|
360
|
+
seal() {
|
|
361
|
+
this.sealed = true;
|
|
362
|
+
return this;
|
|
363
|
+
}
|
|
129
364
|
}
|
|
130
365
|
exports.ToolRegistry = ToolRegistry;
|
|
131
366
|
var invocation_js_2 = require("./invocation.js");
|
|
@@ -2,11 +2,33 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.commonPrefixLength = void 0;
|
|
4
4
|
exports.assembleFragments = assembleFragments;
|
|
5
|
+
/**
|
|
6
|
+
* Assemble validated fragments into the messages sent to the model.
|
|
7
|
+
*
|
|
8
|
+
* The order is fixed: system text first, then one user message carrying structured fields as
|
|
9
|
+
* JSON. Fragment metadata stays program-side in `fragmentUsage` and never becomes an
|
|
10
|
+
* instruction. The finished message list is measured against the overall budget, so a caller
|
|
11
|
+
* sees the same character count a transport will deliver.
|
|
12
|
+
*
|
|
13
|
+
* Exports: `assembleFragments`, plus a shared text helper and the fragment types.
|
|
14
|
+
*/
|
|
5
15
|
const fragments_js_1 = require("./fragments.js");
|
|
6
16
|
const errors_js_1 = require("../structs/errors.js");
|
|
7
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* Build the model input from fragments.
|
|
19
|
+
*
|
|
20
|
+
* Metadata remains program-side: data never becomes a system instruction.
|
|
21
|
+
*
|
|
22
|
+
* @param fragments Fragments in carriage order; ids must be unique across the list.
|
|
23
|
+
* @param maxInputChars Total character budget for the resulting messages.
|
|
24
|
+
* @returns Messages, per-fragment usage and the measured input size.
|
|
25
|
+
* @throws Error when a fragment is invalid or duplicated, a user fragment carries a string
|
|
26
|
+
* value, or two user fragments declare the same field.
|
|
27
|
+
* @throws ValidationError when a fragment or the assembled input exceeds its budget.
|
|
28
|
+
*/
|
|
8
29
|
function assembleFragments(fragments, maxInputChars) {
|
|
9
30
|
const ids = new Set();
|
|
31
|
+
// One merged object lets duplicate field names be rejected rather than silently overwritten.
|
|
10
32
|
const fields = {};
|
|
11
33
|
const system = [];
|
|
12
34
|
const usage = [];
|
|
@@ -20,15 +42,34 @@ function assembleFragments(fragments, maxInputChars) {
|
|
|
20
42
|
for (const [key, value] of Object.entries(fragment.value)) {
|
|
21
43
|
if (Object.hasOwn(fields, key))
|
|
22
44
|
throw new Error(`Duplicate context field: ${key}`);
|
|
45
|
+
// defineProperty keeps names such as `__proto__` as own data instead of letting an
|
|
46
|
+
// assignment reach the object prototype.
|
|
23
47
|
Object.defineProperty(fields, key, { value, enumerable: true });
|
|
24
48
|
}
|
|
25
49
|
}
|
|
26
|
-
usage.push({
|
|
27
|
-
|
|
50
|
+
usage.push({
|
|
51
|
+
id: fragment.id,
|
|
52
|
+
sourceId: fragment.sourceId,
|
|
53
|
+
trust: fragment.trust,
|
|
54
|
+
lifetime: fragment.lifetime,
|
|
55
|
+
chars: serialized.length,
|
|
56
|
+
maxChars: fragment.maxChars,
|
|
57
|
+
});
|
|
28
58
|
}
|
|
29
|
-
|
|
59
|
+
// Blank-line separation keeps independently declared instructions readable and attributable
|
|
60
|
+
// while still arriving as one system message.
|
|
61
|
+
const messages = [
|
|
62
|
+
{ role: 'system', content: system.join('\n\n') },
|
|
63
|
+
{ role: 'user', content: JSON.stringify(fields) },
|
|
64
|
+
];
|
|
65
|
+
// Measured on the exact strings above, so the count matches what the transport will send.
|
|
30
66
|
const inputChars = messages.reduce((sum, message) => sum + message.content.length, 0);
|
|
31
|
-
(0, fragments_js_1.validateContextBudget)(inputChars, maxInputChars, () => new errors_js_1.ValidationError([
|
|
67
|
+
(0, fragments_js_1.validateContextBudget)(inputChars, maxInputChars, () => new errors_js_1.ValidationError([
|
|
68
|
+
{
|
|
69
|
+
code: 'CONTEXT_BUDGET',
|
|
70
|
+
message: `上下文超过${maxInputChars}字符;不截断原问题、约束与证据。`,
|
|
71
|
+
},
|
|
72
|
+
]));
|
|
32
73
|
return { messages, fragmentUsage: usage, inputChars, maxInputChars };
|
|
33
74
|
}
|
|
34
75
|
var text_js_1 = require("../utils/text.js");
|
|
@@ -3,20 +3,45 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.auditContextChange = auditContextChange;
|
|
4
4
|
const errors_js_1 = require("../structs/errors.js");
|
|
5
5
|
const rules_js_1 = require("../utils/rules.js");
|
|
6
|
+
/** Single code for every violation, so consumers can classify audit failures uniformly. */
|
|
6
7
|
const fail = (message) => new errors_js_1.ValidationError([{ code: 'CONTEXT_POLICY', message }]);
|
|
7
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Compare trusted originals with the fragments about to be assembled.
|
|
10
|
+
*
|
|
11
|
+
* @param original Fragments captured before any App-side processing.
|
|
12
|
+
* @param current Fragments that will actually be assembled; same length as `original`.
|
|
13
|
+
* @param mutable Ids explicitly allowed to change value. Everything else is protected.
|
|
14
|
+
* @returns Nothing; a clean audit has no side effect.
|
|
15
|
+
* @throws ValidationError when the fragment list, its metadata, the value type or the field
|
|
16
|
+
* set changed, or when a protected fragment was rewritten.
|
|
17
|
+
*/
|
|
8
18
|
function auditContextChange(original, current, mutable) {
|
|
19
|
+
// Fragments may be rewritten in place but not added or removed: assembly and audit both
|
|
20
|
+
// assume the same positions describe the same fragment.
|
|
9
21
|
(0, rules_js_1.breakWhile)(original.length !== current.length, fail('Context fragments cannot be added or removed'));
|
|
10
22
|
for (const [index, before] of original.entries()) {
|
|
11
23
|
const after = current[index];
|
|
24
|
+
// Metadata and value are compared separately: only the value may be rewritten, and only
|
|
25
|
+
// for ids the plan explicitly allows.
|
|
12
26
|
const { value: _before, ...beforeMetadata } = before;
|
|
13
27
|
const { value: _after, ...afterMetadata } = after;
|
|
14
|
-
(0, rules_js_1.breakWhile)(Object.keys(beforeMetadata).length !== Object.keys(afterMetadata).length
|
|
15
|
-
|
|
28
|
+
(0, rules_js_1.breakWhile)(Object.keys(beforeMetadata).length !== Object.keys(afterMetadata).length ||
|
|
29
|
+
Object.entries(beforeMetadata).some(([key, value]) => afterMetadata[key] !== value), fail('Context identity, trust and budgets must be preserved'));
|
|
30
|
+
// A type change would alter how the fragment is serialized, so it is rejected before any
|
|
31
|
+
// value comparison is attempted.
|
|
16
32
|
(0, rules_js_1.breakWhile)(typeof before.value !== typeof after.value, fail('Context value type must be preserved'));
|
|
33
|
+
// String comparison captures both values and their key order; a reordered rewrite counts
|
|
34
|
+
// as a change, which is the conservative direction for an audit.
|
|
17
35
|
const changed = JSON.stringify(before.value) !== JSON.stringify(after.value);
|
|
18
|
-
(0, rules_js_1.breakWhile)(changed &&
|
|
19
|
-
|
|
20
|
-
|
|
36
|
+
(0, rules_js_1.breakWhile)(changed &&
|
|
37
|
+
(!mutable.has(before.id) || before.role === 'system' || before.trust === 'instruction'), fail(`Protected context changed: ${before.id}`));
|
|
38
|
+
if (changed &&
|
|
39
|
+
before.value !== null &&
|
|
40
|
+
after.value !== null &&
|
|
41
|
+
typeof before.value === 'object' &&
|
|
42
|
+
typeof after.value === 'object')
|
|
43
|
+
// Only the field set is compared here; field order is not meaningful for context data.
|
|
44
|
+
(0, rules_js_1.breakWhile)(JSON.stringify(Object.keys(before.value).sort()) !==
|
|
45
|
+
JSON.stringify(Object.keys(after.value).sort()), fail('Context fields must be preserved'));
|
|
21
46
|
}
|
|
22
47
|
}
|
|
@@ -5,30 +5,73 @@ exports.validateContextFragment = validateContextFragment;
|
|
|
5
5
|
exports.validateContextBudget = validateContextBudget;
|
|
6
6
|
const errors_js_1 = require("../structs/errors.js");
|
|
7
7
|
const rules_js_1 = require("../utils/rules.js");
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Validate one fragment and return the text it contributes to the model input.
|
|
10
|
+
*
|
|
11
|
+
* @param fragment Declaration to check.
|
|
12
|
+
* @param ids Optional accumulator of ids already seen. Callers validating a batch pass the
|
|
13
|
+
* same set for every fragment so duplicates are detected across the whole input.
|
|
14
|
+
* @param checkBudget Whether this fragment's own `maxChars` is enforced. Disable only when the
|
|
15
|
+
* same serialized text has already been checked against the identical limit.
|
|
16
|
+
* @returns The exact text the fragment contributes: the value when it is a string, otherwise
|
|
17
|
+
* the JSON form used for structured fields.
|
|
18
|
+
* @throws Error when identity, trust, lifetime, role, the `maxChars` declaration or the value
|
|
19
|
+
* shape is invalid, or when untrusted content is declared as a system instruction.
|
|
20
|
+
* @throws ValidationError when the serialized text exceeds `maxChars`; text is never truncated.
|
|
21
|
+
*/
|
|
9
22
|
function validateContextFragment(fragment, ids = new Set(), checkBudget = true) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
23
|
+
// Ids are stable keys in usage records and protected-context errors, so they start with a
|
|
24
|
+
// lowercase letter and admit only characters that are safe in both positions.
|
|
25
|
+
if (!/^[a-z][a-zA-Z0-9:._-]*$/.test(fragment.id) ||
|
|
26
|
+
ids.has(fragment.id) ||
|
|
27
|
+
!fragment.sourceId ||
|
|
28
|
+
!['instruction', 'server', 'user', 'tool'].includes(fragment.trust) ||
|
|
29
|
+
!['configuration', 'turn', 'step'].includes(fragment.lifetime) ||
|
|
30
|
+
!['system', 'user'].includes(fragment.role) ||
|
|
31
|
+
!Number.isSafeInteger(fragment.maxChars) ||
|
|
32
|
+
fragment.maxChars < 1)
|
|
14
33
|
throw new Error('Invalid context fragment');
|
|
34
|
+
// Consumed before the later checks: a failed fragment must not leave its id reusable by a
|
|
35
|
+
// retry through the same accumulator.
|
|
15
36
|
ids.add(fragment.id);
|
|
16
|
-
if (fragment.role === 'system' &&
|
|
37
|
+
if (fragment.role === 'system' &&
|
|
38
|
+
(fragment.trust === 'user' || fragment.trust === 'tool' || typeof fragment.value !== 'string'))
|
|
17
39
|
throw new Error('Untrusted context cannot become a system instruction');
|
|
18
|
-
if (typeof fragment.value !== 'string' &&
|
|
40
|
+
if (typeof fragment.value !== 'string' &&
|
|
41
|
+
(!fragment.value || typeof fragment.value !== 'object' || Array.isArray(fragment.value)))
|
|
19
42
|
throw new Error('Invalid context value');
|
|
43
|
+
// Structured values are measured as the JSON text that will be sent, so the declared budget
|
|
44
|
+
// describes the payload rather than the in-memory object.
|
|
20
45
|
const serialized = typeof fragment.value === 'string' ? fragment.value : JSON.stringify(fragment.value);
|
|
21
46
|
if (checkBudget && serialized.length > fragment.maxChars)
|
|
22
|
-
throw new errors_js_1.ValidationError([
|
|
47
|
+
throw new errors_js_1.ValidationError([
|
|
48
|
+
{
|
|
49
|
+
code: 'CONTEXT_BUDGET',
|
|
50
|
+
message: `上下文片段 ${fragment.id} 超过预算;不截断原问题、约束与证据。`,
|
|
51
|
+
},
|
|
52
|
+
]);
|
|
23
53
|
return serialized;
|
|
24
54
|
}
|
|
25
|
-
/**
|
|
55
|
+
/**
|
|
56
|
+
* Enforce the character budget of the whole model input.
|
|
57
|
+
*
|
|
58
|
+
* @param inputChars Characters that will be sent; must be a non-negative safe integer.
|
|
59
|
+
* @param maxInputChars Budget limit. `Infinity` is accepted as an explicit "no limit", which
|
|
60
|
+
* is why it is allowed in place of a safe integer.
|
|
61
|
+
* @param exceeded Optional factory for the over-budget error. Support may pass one to keep its
|
|
62
|
+
* own message; Core otherwise raises a `CONTEXT_BUDGET` ValidationError.
|
|
63
|
+
* @returns Nothing.
|
|
64
|
+
* @throws TypeError when either count is not a non-negative integer.
|
|
65
|
+
* @throws Error when the budget is exceeded: the `exceeded` result, or a ValidationError.
|
|
66
|
+
*/
|
|
26
67
|
function validateContextBudget(inputChars, maxInputChars, exceeded) {
|
|
27
68
|
(0, rules_js_1.breakWhile)(!Number.isSafeInteger(inputChars) || inputChars < 0, () => new TypeError('Invalid context input size'));
|
|
28
69
|
(0, rules_js_1.breakWhile)(maxInputChars !== Infinity && (!Number.isSafeInteger(maxInputChars) || maxInputChars < 0), () => new TypeError('Invalid context budget'));
|
|
29
70
|
if (inputChars > maxInputChars)
|
|
30
|
-
throw exceeded?.(maxInputChars)
|
|
31
|
-
|
|
71
|
+
throw (exceeded?.(maxInputChars) ??
|
|
72
|
+
new errors_js_1.ValidationError([
|
|
73
|
+
{ code: 'CONTEXT_BUDGET', message: `Model input exceeds ${maxInputChars} characters` },
|
|
74
|
+
]));
|
|
32
75
|
}
|
|
33
76
|
var text_js_1 = require("../utils/text.js");
|
|
34
77
|
Object.defineProperty(exports, "commonPrefixLength", { enumerable: true, get: function () { return text_js_1.commonPrefixLength; } });
|
|
@@ -15,6 +15,18 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
exports.assembleContextPlan = exports.assembleFragments = void 0;
|
|
18
|
+
/**
|
|
19
|
+
* Context surface: fragment trust, per-fragment budgets, message assembly, structural audit,
|
|
20
|
+
* the request-local journal and plan assembly.
|
|
21
|
+
*
|
|
22
|
+
* Core owns the guarantees here. A fragment declares its trust, lifetime and character
|
|
23
|
+
* budget in one place; assembly validates those declarations and refuses to truncate;
|
|
24
|
+
* auditing rejects structural change to protected fragments. Selection, ordering, compression
|
|
25
|
+
* and model choice remain App-side nodes that run before this boundary.
|
|
26
|
+
*
|
|
27
|
+
* Exports: `assembleFragments`, `assembleContextPlan`, `ContextJournal`, the fragment helpers,
|
|
28
|
+
* and the evidence Kit helpers consumed alongside assembled context.
|
|
29
|
+
*/
|
|
18
30
|
__exportStar(require("./fragments.js"), exports);
|
|
19
31
|
var assembly_js_1 = require("./assembly.js");
|
|
20
32
|
Object.defineProperty(exports, "assembleFragments", { enumerable: true, get: function () { return assembly_js_1.assembleFragments; } });
|