@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,4 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* History grouping for context reductions: split structured message arrays into atomic groups.
|
|
3
|
+
*
|
|
4
|
+
* A group is the smallest unit a trim may remove, so "fixed" counts entries while "user-turn"
|
|
5
|
+
* keeps each user message together with the assistant replies that follow it. Tool protocols are
|
|
6
|
+
* rejected because they need their own grouping contract.
|
|
7
|
+
*
|
|
8
|
+
* Main exports: `historyGrouping`, `historyGroupingCatalog`, `historyGroups` and
|
|
9
|
+
* `validateRecentHistory`.
|
|
10
|
+
*/
|
|
1
11
|
import type { ContextHistoryGrouping, ContextRecentHistory } from '../../structs/support/context-processing.js';
|
|
12
|
+
/**
|
|
13
|
+
* Validate a grouping declaration and return it unchanged.
|
|
14
|
+
*
|
|
15
|
+
* @param options Grouping to validate.
|
|
16
|
+
* @param options.grouping Grouping kind plus its kind-specific settings.
|
|
17
|
+
* @returns The same grouping object supplied by the caller.
|
|
18
|
+
* @throws {ValidationError} When the kind is unknown or its settings are invalid.
|
|
19
|
+
*/
|
|
2
20
|
export declare function historyGrouping(options: {
|
|
3
21
|
grouping: ContextHistoryGrouping;
|
|
4
22
|
}): ContextHistoryGrouping;
|
|
@@ -6,6 +24,12 @@ declare function validateFixed(grouping: ContextHistoryGrouping): void;
|
|
|
6
24
|
declare function fixedGroups(entries: readonly unknown[], grouping: ContextHistoryGrouping): unknown[][];
|
|
7
25
|
declare function validateUserTurns(): void;
|
|
8
26
|
declare function userTurns(entries: readonly unknown[]): unknown[][];
|
|
27
|
+
/**
|
|
28
|
+
* Grouping kinds with their validators and splitters.
|
|
29
|
+
*
|
|
30
|
+
* Validation and splitting come from one declaration so a policy cannot pass validation with
|
|
31
|
+
* rules that the splitter does not apply.
|
|
32
|
+
*/
|
|
9
33
|
export declare const historyGroupingCatalog: {
|
|
10
34
|
fixed: {
|
|
11
35
|
validate: typeof validateFixed;
|
|
@@ -16,9 +40,28 @@ export declare const historyGroupingCatalog: {
|
|
|
16
40
|
split: typeof userTurns;
|
|
17
41
|
};
|
|
18
42
|
};
|
|
43
|
+
/**
|
|
44
|
+
* Split the entries of one structured field into removable groups.
|
|
45
|
+
*
|
|
46
|
+
* @param value Fragment value expected to be an object holding an array at `field`.
|
|
47
|
+
* @param options Field name and grouping rule to apply.
|
|
48
|
+
* @returns Grouped entries in source order; an empty array yields no groups.
|
|
49
|
+
* @throws {ValidationError} When the value is a string, the field is not an array, the grouping
|
|
50
|
+
* is invalid, or the entries do not satisfy the grouping contract.
|
|
51
|
+
*/
|
|
19
52
|
export declare function historyGroups(value: string | Record<string, unknown>, options: {
|
|
20
53
|
field: string;
|
|
21
54
|
grouping: ContextHistoryGrouping;
|
|
22
55
|
}): unknown[][];
|
|
56
|
+
/**
|
|
57
|
+
* Validate a recent-history retention declaration.
|
|
58
|
+
*
|
|
59
|
+
* `keepGroups` may be zero (drop everything eligible); negative or fractional counts are rejected
|
|
60
|
+
* because groups are indivisible.
|
|
61
|
+
*
|
|
62
|
+
* @param options Field, retained group count and grouping rule.
|
|
63
|
+
* @throws {ValidationError} When the field is empty, `keepGroups` is invalid, or the grouping is
|
|
64
|
+
* not recognised.
|
|
65
|
+
*/
|
|
23
66
|
export declare function validateRecentHistory(options: ContextRecentHistory): void;
|
|
24
67
|
export {};
|
|
@@ -2,6 +2,14 @@ import { ValidationError } from '../../structs/errors.js';
|
|
|
2
2
|
import { breakWhile } from '../../utils/rules.js';
|
|
3
3
|
const invalid = (message) => new ValidationError([{ code: 'CONTEXT_POLICY', message }]);
|
|
4
4
|
const positive = (value) => Number.isSafeInteger(value) && value > 0;
|
|
5
|
+
/**
|
|
6
|
+
* Validate a grouping declaration and return it unchanged.
|
|
7
|
+
*
|
|
8
|
+
* @param options Grouping to validate.
|
|
9
|
+
* @param options.grouping Grouping kind plus its kind-specific settings.
|
|
10
|
+
* @returns The same grouping object supplied by the caller.
|
|
11
|
+
* @throws {ValidationError} When the kind is unknown or its settings are invalid.
|
|
12
|
+
*/
|
|
5
13
|
export function historyGrouping(options) {
|
|
6
14
|
const grouping = options.grouping;
|
|
7
15
|
breakWhile(!grouping || !Object.hasOwn(historyGroupingCatalog, grouping.kind), invalid('Unknown history grouping'));
|
|
@@ -24,7 +32,9 @@ function userTurns(entries) {
|
|
|
24
32
|
const groups = [];
|
|
25
33
|
for (const entry of entries) {
|
|
26
34
|
const message = entry;
|
|
27
|
-
breakWhile(!message ||
|
|
35
|
+
breakWhile(!message ||
|
|
36
|
+
!['user', 'assistant'].includes(message.role) ||
|
|
37
|
+
typeof message.content !== 'string', invalid('User-turn history requires user/assistant text messages; tool protocols need their own grouping contract'));
|
|
28
38
|
if (message.role === 'user')
|
|
29
39
|
groups.push([]);
|
|
30
40
|
breakWhile(!groups.length, invalid('User-turn history cannot start with an orphan assistant message'));
|
|
@@ -32,15 +42,40 @@ function userTurns(entries) {
|
|
|
32
42
|
}
|
|
33
43
|
return groups;
|
|
34
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Grouping kinds with their validators and splitters.
|
|
47
|
+
*
|
|
48
|
+
* Validation and splitting come from one declaration so a policy cannot pass validation with
|
|
49
|
+
* rules that the splitter does not apply.
|
|
50
|
+
*/
|
|
35
51
|
export const historyGroupingCatalog = {
|
|
36
52
|
fixed: { validate: validateFixed, split: fixedGroups },
|
|
37
53
|
'user-turn': { validate: validateUserTurns, split: userTurns },
|
|
38
54
|
};
|
|
55
|
+
/**
|
|
56
|
+
* Split the entries of one structured field into removable groups.
|
|
57
|
+
*
|
|
58
|
+
* @param value Fragment value expected to be an object holding an array at `field`.
|
|
59
|
+
* @param options Field name and grouping rule to apply.
|
|
60
|
+
* @returns Grouped entries in source order; an empty array yields no groups.
|
|
61
|
+
* @throws {ValidationError} When the value is a string, the field is not an array, the grouping
|
|
62
|
+
* is invalid, or the entries do not satisfy the grouping contract.
|
|
63
|
+
*/
|
|
39
64
|
export function historyGroups(value, options) {
|
|
40
65
|
breakWhile(typeof value === 'string' || !Array.isArray(value[options.field]), invalid('History requires a structured array field'));
|
|
41
66
|
const grouping = historyGrouping(options);
|
|
42
67
|
return historyGroupingCatalog[grouping.kind].split(value[options.field], grouping);
|
|
43
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Validate a recent-history retention declaration.
|
|
71
|
+
*
|
|
72
|
+
* `keepGroups` may be zero (drop everything eligible); negative or fractional counts are rejected
|
|
73
|
+
* because groups are indivisible.
|
|
74
|
+
*
|
|
75
|
+
* @param options Field, retained group count and grouping rule.
|
|
76
|
+
* @throws {ValidationError} When the field is empty, `keepGroups` is invalid, or the grouping is
|
|
77
|
+
* not recognised.
|
|
78
|
+
*/
|
|
44
79
|
export function validateRecentHistory(options) {
|
|
45
80
|
breakWhile(!options.field || !Number.isSafeInteger(options.keepGroups) || options.keepGroups < 0, invalid('Invalid recent-history retention'));
|
|
46
81
|
historyGrouping(options);
|
|
@@ -1,12 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context support: app-side assembly, reduction and selection helpers for Core context plans.
|
|
3
|
+
*
|
|
4
|
+
* Core keeps the trust, budget, assembly and structural-audit guarantees. This module supplies
|
|
5
|
+
* recommended builders (assembly, compressor, selector, strategy) and node factories that plug
|
|
6
|
+
* into an application workflow; it does not replace Core's processing contract.
|
|
7
|
+
*
|
|
8
|
+
* Main exports: `defineContextAssembly`, `defineContextFragments`, `createContextCompressor`,
|
|
9
|
+
* `createContextSelector`, `createContextStrategy`, the context node factories and the
|
|
10
|
+
* registered policy/reduction/selection catalogs.
|
|
11
|
+
*/
|
|
1
12
|
export * from './assembly.js';
|
|
2
13
|
export * from './fragments.js';
|
|
3
14
|
export type * from '../../structs/support/context.js';
|
|
4
15
|
export * from './compression.js';
|
|
5
16
|
export type * from '../../structs/support/context-processing.js';
|
|
6
17
|
export * from './selection.js';
|
|
7
|
-
export { processContext, validateContextPolicy, contextPolicyCatalog, contextReductionCatalog } from './processing.js';
|
|
18
|
+
export { processContext, validateContextPolicy, contextPolicyCatalog, contextReductionCatalog, } from './processing.js';
|
|
8
19
|
export * from './selection-policy.js';
|
|
9
20
|
export type { ContextPlan } from '../../structs/context/processing.js';
|
|
10
21
|
export * from './nodes.js';
|
|
11
22
|
export { createContextStrategy, contextHistoryStrategies } from './strategies.js';
|
|
12
|
-
export type { ContextStrategy, ContextStrategyOptions, ContextStrategySettings } from './strategies.js';
|
|
23
|
+
export type { ContextStrategy, ContextStrategyOptions, ContextStrategySettings, } from './strategies.js';
|
|
@@ -1,8 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context support: app-side assembly, reduction and selection helpers for Core context plans.
|
|
3
|
+
*
|
|
4
|
+
* Core keeps the trust, budget, assembly and structural-audit guarantees. This module supplies
|
|
5
|
+
* recommended builders (assembly, compressor, selector, strategy) and node factories that plug
|
|
6
|
+
* into an application workflow; it does not replace Core's processing contract.
|
|
7
|
+
*
|
|
8
|
+
* Main exports: `defineContextAssembly`, `defineContextFragments`, `createContextCompressor`,
|
|
9
|
+
* `createContextSelector`, `createContextStrategy`, the context node factories and the
|
|
10
|
+
* registered policy/reduction/selection catalogs.
|
|
11
|
+
*/
|
|
1
12
|
export * from './assembly.js';
|
|
2
13
|
export * from './fragments.js';
|
|
3
14
|
export * from './compression.js';
|
|
4
15
|
export * from './selection.js';
|
|
5
|
-
export { processContext, validateContextPolicy, contextPolicyCatalog, contextReductionCatalog } from './processing.js';
|
|
16
|
+
export { processContext, validateContextPolicy, contextPolicyCatalog, contextReductionCatalog, } from './processing.js';
|
|
6
17
|
export * from './selection-policy.js';
|
|
7
18
|
export * from './nodes.js';
|
|
8
19
|
export { createContextStrategy, contextHistoryStrategies } from './strategies.js';
|
|
@@ -3,43 +3,105 @@ import type { ContextPlan } from '../../structs/context/processing.js';
|
|
|
3
3
|
import type { BuiltContext } from '../../structs/context/fragments.js';
|
|
4
4
|
import type { ModelPrepareRequest, PreparedModelCall, ModelTransportDefinition } from '../../structs/model/runtime.js';
|
|
5
5
|
import type { ContextCompressor, ContextSelector, ContextPolicy, ContextProcessingReport } from '../../structs/support/context-processing.js';
|
|
6
|
+
/** Outcome of one context node: the built input, its prepared model call and the change report. */
|
|
6
7
|
export interface ContextNodeResult {
|
|
8
|
+
/** Context built from the processed fragments, sized against the full input limit. */
|
|
7
9
|
readonly input: BuiltContext;
|
|
10
|
+
/** Model call prepared for this stage; carries measurement and remaining call budget. */
|
|
8
11
|
readonly prepared: PreparedModelCall;
|
|
12
|
+
/** What processing changed: characters, token usage, selection decisions and reductions. */
|
|
9
13
|
readonly report: ContextProcessingReport;
|
|
10
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Definition accepted by `createContextProcessingNode`.
|
|
17
|
+
*
|
|
18
|
+
* It is a normal Core `WorkflowNode` except that the node body is declared here: `plan` builds the
|
|
19
|
+
* context, `update` decides the committed state, and policy/handlers/timeout are validated when the
|
|
20
|
+
* node is created.
|
|
21
|
+
*/
|
|
11
22
|
export type ContextProcessingNodeDefinition<State, Result, App> = Omit<WorkflowNode<State, Result, App>, 'run' | 'report'> & {
|
|
23
|
+
/** Builds the context plan; must be synchronous so budget failures surface before any I/O. */
|
|
12
24
|
readonly plan: (state: Readonly<State>, context: WorkflowContext<App>) => ContextPlan;
|
|
25
|
+
/** Model preparation request, either static or derived from the current node state. */
|
|
13
26
|
readonly request?: (ModelPrepareRequest<string | ModelTransportDefinition> & {
|
|
14
27
|
modelId?: string;
|
|
15
28
|
}) | ((state: Readonly<State>, context: WorkflowContext<App>) => ModelPrepareRequest<string | ModelTransportDefinition> & {
|
|
16
29
|
modelId?: string;
|
|
17
30
|
});
|
|
31
|
+
/** Maps the node result to the state fields to commit; return only changed fields. */
|
|
18
32
|
readonly update: (result: ContextNodeResult, state: Readonly<State>) => Partial<State>;
|
|
33
|
+
/** Optional prebuilt model call, used instead of preparing `request`. */
|
|
19
34
|
readonly prepared?: (state: Readonly<State>, context: WorkflowContext<App>) => PreparedModelCall;
|
|
35
|
+
/** Processing policy; omitted means `validate` (no reduction, audit checks only). */
|
|
20
36
|
readonly policy?: ContextPolicy;
|
|
37
|
+
/** Compressors referenced by compression steps; validated against the policy at creation. */
|
|
21
38
|
readonly compressors?: readonly ContextCompressor[];
|
|
39
|
+
/** Selectors referenced by model-driven selection; validated against the policy at creation. */
|
|
22
40
|
readonly selectors?: readonly ContextSelector[];
|
|
41
|
+
/** Per-node timeout in milliseconds; defaults to the Core call timeout. */
|
|
23
42
|
readonly timeoutMs?: number;
|
|
24
43
|
};
|
|
44
|
+
/** Result of a selection or reduction stage, before the node's `update` commits chosen fields. */
|
|
25
45
|
export interface ContextStageResult {
|
|
46
|
+
/** Plan describing current fragments at the full (unreduced) input limit. */
|
|
26
47
|
readonly plan: ContextPlan;
|
|
48
|
+
/** Model call prepared for this stage. */
|
|
27
49
|
readonly prepared: PreparedModelCall;
|
|
50
|
+
/** Processing report accumulated by this and preceding steps. */
|
|
28
51
|
readonly report: ContextProcessingReport;
|
|
29
52
|
}
|
|
53
|
+
/** Selection stage result: the chosen step IDs plus the unchanged stage snapshot. */
|
|
30
54
|
export interface ContextSelectionResult extends ContextStageResult {
|
|
55
|
+
/** Registered reduction step IDs chosen for execution; selection itself executes nothing. */
|
|
31
56
|
readonly stepIds: readonly string[];
|
|
32
57
|
}
|
|
58
|
+
/** Definition of a selection node: like a processing node, but `update` receives chosen step IDs. */
|
|
33
59
|
export type ContextSelectionNodeDefinition<State, Result, App> = Omit<ContextProcessingNodeDefinition<State, Result, App>, 'update'> & {
|
|
60
|
+
/** Receives the chosen step IDs so the graph can route to reduction nodes. */
|
|
34
61
|
readonly update: (result: ContextSelectionResult, state: Readonly<State>) => Partial<State>;
|
|
35
62
|
};
|
|
63
|
+
/**
|
|
64
|
+
* Definition of a reduction node: one registered step is applied per node run.
|
|
65
|
+
*
|
|
66
|
+
* `stepId` may be fixed or derived from state, which lets a graph route over several steps.
|
|
67
|
+
*/
|
|
36
68
|
export type ContextReductionNodeDefinition<State, Result, App> = Omit<ContextProcessingNodeDefinition<State, Result, App>, 'update'> & {
|
|
69
|
+
/** ID of the reduction step to run, fixed or derived from state. */
|
|
37
70
|
readonly stepId: string | ((state: Readonly<State>) => string);
|
|
71
|
+
/** Receives the stage result after one reduction has been applied. */
|
|
38
72
|
readonly update: (result: ContextStageResult, state: Readonly<State>) => Partial<State>;
|
|
39
73
|
};
|
|
40
|
-
/**
|
|
74
|
+
/**
|
|
75
|
+
* Create one node that runs the complete context policy and returns the built context.
|
|
76
|
+
*
|
|
77
|
+
* Use this when the graph does not need to route between selection and reduction phases.
|
|
78
|
+
*
|
|
79
|
+
* @param definition Node definition including plan, model request/prepared call, policy and update.
|
|
80
|
+
* @returns A `WorkflowNode` returning the processed context, prepared call and report.
|
|
81
|
+
* @throws {Error} When the policy, its handlers or the timeout are invalid, or the plan is missing
|
|
82
|
+
* enough input capacity.
|
|
83
|
+
*/
|
|
41
84
|
export declare function createContextProcessingNode<State, Result, App = undefined>(definition: ContextProcessingNodeDefinition<State, Result, App>): WorkflowNode<State, Result, App>;
|
|
42
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* Create an ordinary node that selects reduction steps without executing them.
|
|
87
|
+
*
|
|
88
|
+
* The node reports the registered step IDs so the graph can route to reduction nodes itself.
|
|
89
|
+
*
|
|
90
|
+
* @param definition Node definition with a `reduce` policy and a selection-aware `update`.
|
|
91
|
+
* @returns A `WorkflowNode` returning the chosen step IDs plus the current stage result.
|
|
92
|
+
* @throws {Error} When the policy is not a reduction policy, a reduction target is protected, or
|
|
93
|
+
* selection exceeds the remaining call budget.
|
|
94
|
+
*/
|
|
43
95
|
export declare function createContextSelectionNode<State, Result, App = undefined>(definition: ContextSelectionNodeDefinition<State, Result, App>): WorkflowNode<State, Result, App>;
|
|
44
|
-
/**
|
|
96
|
+
/**
|
|
97
|
+
* Create a node that applies exactly one registered reduction step.
|
|
98
|
+
*
|
|
99
|
+
* Partial plans keep the full budget until final assembly, so intermediate nodes do not shrink the
|
|
100
|
+
* allowance reserved for the reply.
|
|
101
|
+
*
|
|
102
|
+
* @param definition Node definition with a fixed or state-derived `stepId` and update function.
|
|
103
|
+
* @returns A `WorkflowNode` returning the stage result after the reduction.
|
|
104
|
+
* @throws {Error} When the step ID is not registered, the target is protected, or the reduction
|
|
105
|
+
* fails or increases the fragment size.
|
|
106
|
+
*/
|
|
45
107
|
export declare function createContextReductionNode<State, Result, App = undefined>(definition: ContextReductionNodeDefinition<State, Result, App>): WorkflowNode<State, Result, App>;
|
|
@@ -1,17 +1,41 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Context workflow nodes: wire context planning, processing, selection and reduction into Core.
|
|
3
|
+
*
|
|
4
|
+
* Each factory returns a standard `WorkflowNode` that prepares the model call, converts the plan
|
|
5
|
+
* into a Core processing state and then applies one stage. Applications may use the single
|
|
6
|
+
* complete node or the explicit selection/reduction pair in their own graph. The nodes add no new
|
|
7
|
+
* scheduling contract; Core still owns budgets, cancellation and commit.
|
|
8
|
+
*
|
|
9
|
+
* Main exports: `createContextProcessingNode`, `createContextSelectionNode`,
|
|
10
|
+
* `createContextReductionNode` and their result/definition types.
|
|
11
|
+
*/
|
|
12
|
+
import { beginContextProcessing, finishContextProcessing, chooseSteps, applyReduction, needsReduction, watermarks, validateReductionTargets, contextPolicyCatalog, validateContextPolicy, } from './processing.js';
|
|
2
13
|
import { modelRunnerDefaults } from '../../model/defaults.js';
|
|
3
14
|
import { synchronous } from '../../utils/synchronous.js';
|
|
4
15
|
import { combineSignals } from '../../utils/cancellation.js';
|
|
5
16
|
import { breakWhile } from '../../utils/rules.js';
|
|
6
17
|
function history(source) {
|
|
7
18
|
const grouping = source.grouping;
|
|
8
|
-
return {
|
|
9
|
-
|
|
19
|
+
return {
|
|
20
|
+
field: source.field,
|
|
21
|
+
keepGroups: source.keepGroups,
|
|
22
|
+
grouping: grouping.kind === 'fixed'
|
|
23
|
+
? { kind: grouping.kind, size: grouping.size }
|
|
24
|
+
: { kind: grouping.kind },
|
|
25
|
+
};
|
|
10
26
|
}
|
|
11
27
|
function reduction(source) {
|
|
12
28
|
const { id, kind, fragmentId, targetChars } = source;
|
|
13
|
-
return kind === 'trim'
|
|
14
|
-
|
|
29
|
+
return kind === 'trim'
|
|
30
|
+
? { id, kind, fragmentId, targetChars, ...history(source) }
|
|
31
|
+
: {
|
|
32
|
+
id,
|
|
33
|
+
kind,
|
|
34
|
+
fragmentId,
|
|
35
|
+
targetChars,
|
|
36
|
+
handlerId: source.handlerId,
|
|
37
|
+
preserve: source.preserve && history(source.preserve),
|
|
38
|
+
};
|
|
15
39
|
}
|
|
16
40
|
function policySnapshot(source) {
|
|
17
41
|
if (source.mode === 'audit')
|
|
@@ -19,74 +43,168 @@ function policySnapshot(source) {
|
|
|
19
43
|
if (source.mode !== 'reduce')
|
|
20
44
|
return { mode: source.mode };
|
|
21
45
|
const selection = source.selection;
|
|
22
|
-
return {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
46
|
+
return {
|
|
47
|
+
mode: source.mode,
|
|
48
|
+
triggerChars: source.triggerChars,
|
|
49
|
+
targetInputChars: source.targetInputChars,
|
|
50
|
+
triggerTokens: source.triggerTokens,
|
|
51
|
+
targetInputTokens: source.targetInputTokens,
|
|
52
|
+
steps: source.steps.map(reduction),
|
|
53
|
+
selection: !selection
|
|
54
|
+
? undefined
|
|
55
|
+
: selection.mode === 'fixed'
|
|
56
|
+
? { mode: selection.mode, kind: selection.kind }
|
|
57
|
+
: selection.mode === 'auto'
|
|
58
|
+
? {
|
|
59
|
+
mode: selection.mode,
|
|
60
|
+
reserveCalls: selection.reserveCalls,
|
|
61
|
+
maxTrimLossRatio: selection.maxTrimLossRatio,
|
|
62
|
+
}
|
|
63
|
+
: selection.mode === 'model'
|
|
64
|
+
? {
|
|
65
|
+
mode: selection.mode,
|
|
66
|
+
reserveCalls: selection.reserveCalls,
|
|
67
|
+
handlerId: selection.handlerId,
|
|
68
|
+
}
|
|
69
|
+
: { mode: selection.mode },
|
|
70
|
+
};
|
|
27
71
|
}
|
|
28
72
|
function stageResult(work, prepared, fullLimit, steps) {
|
|
29
|
-
const mutable = steps.map(step => step.fragmentId);
|
|
30
|
-
return {
|
|
31
|
-
|
|
32
|
-
|
|
73
|
+
const mutable = steps.map((step) => step.fragmentId);
|
|
74
|
+
return {
|
|
75
|
+
plan: {
|
|
76
|
+
additionalMessages: work.plan.additionalMessages,
|
|
77
|
+
fragments: work.fragments,
|
|
78
|
+
maxInputChars: fullLimit,
|
|
79
|
+
originalFragments: work.plan.originalFragments ?? work.before,
|
|
80
|
+
// Several steps may target one fragment; the plan lists each mutable ID only once.
|
|
81
|
+
mutableFragmentIds: [...new Set(mutable)],
|
|
82
|
+
},
|
|
83
|
+
prepared,
|
|
84
|
+
report: work.report,
|
|
85
|
+
};
|
|
33
86
|
}
|
|
34
87
|
/** Common node wiring only; each specialized creator supplies its actual algorithm. */
|
|
35
88
|
function contextNode(definition, execute) {
|
|
36
89
|
const plan = definition.plan.bind(definition), update = definition.update.bind(definition);
|
|
37
90
|
const selectPrepared = definition.prepared?.bind(definition);
|
|
38
91
|
const requestSource = definition.request;
|
|
39
|
-
const request = typeof requestSource === 'function'
|
|
40
|
-
|
|
41
|
-
|
|
92
|
+
const request = typeof requestSource === 'function'
|
|
93
|
+
? requestSource.bind(definition)
|
|
94
|
+
: {
|
|
95
|
+
stageId: requestSource?.stageId,
|
|
96
|
+
modelId: requestSource?.modelId,
|
|
97
|
+
transport: requestSource?.transport,
|
|
98
|
+
maxOutputTokens: requestSource?.maxOutputTokens,
|
|
99
|
+
};
|
|
42
100
|
const policy = policySnapshot(definition.policy ?? { mode: 'validate' });
|
|
43
|
-
const compressors = (definition.compressors ?? []).map(handler => {
|
|
101
|
+
const compressors = (definition.compressors ?? []).map((handler) => {
|
|
44
102
|
const { id, maxCalls, compress } = handler;
|
|
45
|
-
return {
|
|
103
|
+
return {
|
|
104
|
+
id,
|
|
105
|
+
maxCalls,
|
|
106
|
+
compress: typeof compress === 'function' ? compress.bind(handler) : compress,
|
|
107
|
+
};
|
|
46
108
|
});
|
|
47
|
-
const selectors = (definition.selectors ?? []).map(handler => {
|
|
109
|
+
const selectors = (definition.selectors ?? []).map((handler) => {
|
|
48
110
|
const { id, maxCalls, select } = handler;
|
|
49
111
|
return { id, maxCalls, select: typeof select === 'function' ? select.bind(handler) : select };
|
|
50
112
|
});
|
|
51
113
|
validateContextPolicy(policy, compressors, selectors);
|
|
52
114
|
const timeoutMs = definition.timeoutMs ?? modelRunnerDefaults.callTimeoutMs;
|
|
53
115
|
breakWhile(!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 2147483647, 'Invalid context node timeout');
|
|
54
|
-
return {
|
|
55
|
-
|
|
56
|
-
|
|
116
|
+
return {
|
|
117
|
+
id: definition.id,
|
|
118
|
+
description: definition.description,
|
|
119
|
+
name: definition.name,
|
|
120
|
+
startsRound: definition.startsRound,
|
|
121
|
+
requires: definition.requires,
|
|
122
|
+
provides: definition.provides,
|
|
123
|
+
onErrorNodeId: definition.onErrorNodeId,
|
|
124
|
+
onErrorRouteId: definition.onErrorRouteId,
|
|
125
|
+
routes: definition.routes,
|
|
57
126
|
async run(state, context) {
|
|
127
|
+
// The node timeout is an additional bound; parent cancellation still wins.
|
|
58
128
|
const signal = combineSignals(context.signal, AbortSignal.timeout(timeoutMs));
|
|
59
|
-
const prepared = selectPrepared
|
|
129
|
+
const prepared = selectPrepared
|
|
130
|
+
? selectPrepared(state, context)
|
|
131
|
+
: context.model.prepare(typeof request === 'function' ? request(state, context) : request);
|
|
60
132
|
const input = synchronous(plan(state, context), 'Context plans must be synchronous');
|
|
133
|
+
// An empty measurement reports the allowance left after transport, instruction and schema
|
|
134
|
+
// overhead, so processing must fit inside whichever limit is tighter.
|
|
61
135
|
const overhead = prepared.measure([]);
|
|
62
136
|
const fullLimit = Math.min(input.maxInputChars, overhead.maxInputChars);
|
|
63
137
|
const bodyLimit = fullLimit;
|
|
64
138
|
breakWhile(bodyLimit < 1, 'No input capacity for context');
|
|
65
|
-
const work = beginContextProcessing({
|
|
66
|
-
|
|
67
|
-
|
|
139
|
+
const work = beginContextProcessing({
|
|
140
|
+
fragments: input.fragments,
|
|
141
|
+
additionalMessages: input.additionalMessages,
|
|
142
|
+
originalFragments: input.originalFragments,
|
|
143
|
+
mutableFragmentIds: input.mutableFragmentIds,
|
|
144
|
+
maxInputChars: bodyLimit,
|
|
145
|
+
}, policy, {
|
|
146
|
+
stageId: prepared.stageId,
|
|
147
|
+
signal,
|
|
148
|
+
diagnostics: context.diagnostics,
|
|
149
|
+
compressors,
|
|
150
|
+
selectors,
|
|
151
|
+
// Read lazily so handlers see the live budget rather than the value at preparation.
|
|
68
152
|
remainingCalls: () => prepared.remainingCalls,
|
|
69
|
-
callModel: options => context.model.invoke({
|
|
70
|
-
|
|
71
|
-
|
|
153
|
+
callModel: (options) => context.model.invoke({
|
|
154
|
+
...options,
|
|
155
|
+
modelId: options.modelId ?? prepared.modelId,
|
|
156
|
+
signal,
|
|
157
|
+
}),
|
|
158
|
+
...(overhead.counterId
|
|
159
|
+
? {
|
|
160
|
+
tokenBudget: {
|
|
161
|
+
counterId: overhead.counterId,
|
|
162
|
+
inputLimit: overhead.inputTokenLimit,
|
|
163
|
+
measure: (messages) => prepared.measure(messages).inputTokens,
|
|
164
|
+
},
|
|
165
|
+
}
|
|
166
|
+
: {}),
|
|
72
167
|
});
|
|
73
168
|
context.report({ payload: { phase: 'processing', fragmentCount: input.fragments.length } });
|
|
74
169
|
const result = await execute(work, prepared, fullLimit, state, context);
|
|
170
|
+
// Never report or commit a result produced after cancellation.
|
|
75
171
|
signal.throwIfAborted();
|
|
76
172
|
context.report({ payload: { phase: 'processed', report: work.report } });
|
|
77
173
|
return synchronous(update(result, state), 'Context node updates must be synchronous');
|
|
78
174
|
},
|
|
79
175
|
};
|
|
80
176
|
}
|
|
81
|
-
/**
|
|
177
|
+
/**
|
|
178
|
+
* Create one node that runs the complete context policy and returns the built context.
|
|
179
|
+
*
|
|
180
|
+
* Use this when the graph does not need to route between selection and reduction phases.
|
|
181
|
+
*
|
|
182
|
+
* @param definition Node definition including plan, model request/prepared call, policy and update.
|
|
183
|
+
* @returns A `WorkflowNode` returning the processed context, prepared call and report.
|
|
184
|
+
* @throws {Error} When the policy, its handlers or the timeout are invalid, or the plan is missing
|
|
185
|
+
* enough input capacity.
|
|
186
|
+
*/
|
|
82
187
|
export function createContextProcessingNode(definition) {
|
|
83
188
|
return contextNode(definition, async (work, prepared, fullLimit) => {
|
|
84
189
|
await contextPolicyCatalog[work.policy.mode].run(work);
|
|
85
190
|
const result = finishContextProcessing(work);
|
|
86
|
-
return {
|
|
191
|
+
return {
|
|
192
|
+
input: { ...result.context, maxInputChars: fullLimit },
|
|
193
|
+
prepared,
|
|
194
|
+
report: result.report,
|
|
195
|
+
};
|
|
87
196
|
});
|
|
88
197
|
}
|
|
89
|
-
/**
|
|
198
|
+
/**
|
|
199
|
+
* Create an ordinary node that selects reduction steps without executing them.
|
|
200
|
+
*
|
|
201
|
+
* The node reports the registered step IDs so the graph can route to reduction nodes itself.
|
|
202
|
+
*
|
|
203
|
+
* @param definition Node definition with a `reduce` policy and a selection-aware `update`.
|
|
204
|
+
* @returns A `WorkflowNode` returning the chosen step IDs plus the current stage result.
|
|
205
|
+
* @throws {Error} When the policy is not a reduction policy, a reduction target is protected, or
|
|
206
|
+
* selection exceeds the remaining call budget.
|
|
207
|
+
*/
|
|
90
208
|
export function createContextSelectionNode(definition) {
|
|
91
209
|
return contextNode(definition, async (work, prepared, fullLimit) => {
|
|
92
210
|
validateReductionTargets(work);
|
|
@@ -95,11 +213,22 @@ export function createContextSelectionNode(definition) {
|
|
|
95
213
|
work.report.selection = policy.selection?.mode ?? 'sequence';
|
|
96
214
|
work.report.decisions = [];
|
|
97
215
|
const stepIds = needsReduction(work, watermarks(work).trigger)
|
|
98
|
-
? await chooseSteps(work, policy.steps, policy.selection ?? { mode: 'sequence' })
|
|
216
|
+
? await chooseSteps(work, policy.steps, policy.selection ?? { mode: 'sequence' })
|
|
217
|
+
: [];
|
|
99
218
|
return { ...stageResult(work, prepared, fullLimit, policy.steps), stepIds };
|
|
100
219
|
});
|
|
101
220
|
}
|
|
102
|
-
/**
|
|
221
|
+
/**
|
|
222
|
+
* Create a node that applies exactly one registered reduction step.
|
|
223
|
+
*
|
|
224
|
+
* Partial plans keep the full budget until final assembly, so intermediate nodes do not shrink the
|
|
225
|
+
* allowance reserved for the reply.
|
|
226
|
+
*
|
|
227
|
+
* @param definition Node definition with a fixed or state-derived `stepId` and update function.
|
|
228
|
+
* @returns A `WorkflowNode` returning the stage result after the reduction.
|
|
229
|
+
* @throws {Error} When the step ID is not registered, the target is protected, or the reduction
|
|
230
|
+
* fails or increases the fragment size.
|
|
231
|
+
*/
|
|
103
232
|
export function createContextReductionNode(definition) {
|
|
104
233
|
const source = definition.stepId;
|
|
105
234
|
const stepId = typeof source === 'function' ? source.bind(definition) : () => source;
|
|
@@ -107,7 +236,7 @@ export function createContextReductionNode(definition) {
|
|
|
107
236
|
validateReductionTargets(work);
|
|
108
237
|
const policy = work.policy;
|
|
109
238
|
const id = synchronous(stepId(state), 'Context operation IDs must be synchronous');
|
|
110
|
-
const step = policy.steps.find(item => item.id === id);
|
|
239
|
+
const step = policy.steps.find((item) => item.id === id);
|
|
111
240
|
breakWhile(!step, 'Context operation is not registered');
|
|
112
241
|
await applyReduction(work, step, policy.selection ?? { mode: 'sequence' });
|
|
113
242
|
return stageResult(work, prepared, fullLimit, policy.steps);
|