@nhtio/adk 0.1.0-master-f0aa531d
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/LICENSE.md +9 -0
- package/README.md +3 -0
- package/batteries/index.d.ts +28 -0
- package/batteries/llm/index.d.ts +11 -0
- package/batteries/llm/openai_chat_completions/adapter.cjs +916 -0
- package/batteries/llm/openai_chat_completions/adapter.cjs.map +1 -0
- package/batteries/llm/openai_chat_completions/adapter.d.ts +101 -0
- package/batteries/llm/openai_chat_completions/adapter.mjs +914 -0
- package/batteries/llm/openai_chat_completions/adapter.mjs.map +1 -0
- package/batteries/llm/openai_chat_completions/exceptions.cjs +89 -0
- package/batteries/llm/openai_chat_completions/exceptions.cjs.map +1 -0
- package/batteries/llm/openai_chat_completions/exceptions.d.ts +97 -0
- package/batteries/llm/openai_chat_completions/exceptions.mjs +81 -0
- package/batteries/llm/openai_chat_completions/exceptions.mjs.map +1 -0
- package/batteries/llm/openai_chat_completions/helpers.cjs +819 -0
- package/batteries/llm/openai_chat_completions/helpers.cjs.map +1 -0
- package/batteries/llm/openai_chat_completions/helpers.d.ts +233 -0
- package/batteries/llm/openai_chat_completions/helpers.mjs +783 -0
- package/batteries/llm/openai_chat_completions/helpers.mjs.map +1 -0
- package/batteries/llm/openai_chat_completions/index.d.ts +27 -0
- package/batteries/llm/openai_chat_completions/types.cjs +1 -0
- package/batteries/llm/openai_chat_completions/types.d.ts +524 -0
- package/batteries/llm/openai_chat_completions/types.mjs +0 -0
- package/batteries/llm/openai_chat_completions/validation.cjs +190 -0
- package/batteries/llm/openai_chat_completions/validation.cjs.map +1 -0
- package/batteries/llm/openai_chat_completions/validation.d.ts +31 -0
- package/batteries/llm/openai_chat_completions/validation.mjs +187 -0
- package/batteries/llm/openai_chat_completions/validation.mjs.map +1 -0
- package/batteries/llm/openai_chat_completions.cjs +51 -0
- package/batteries/llm/openai_chat_completions.mjs +5 -0
- package/batteries/llm/webllm_chat_completions/adapter.cjs +658 -0
- package/batteries/llm/webllm_chat_completions/adapter.cjs.map +1 -0
- package/batteries/llm/webllm_chat_completions/adapter.d.ts +103 -0
- package/batteries/llm/webllm_chat_completions/adapter.mjs +656 -0
- package/batteries/llm/webllm_chat_completions/adapter.mjs.map +1 -0
- package/batteries/llm/webllm_chat_completions/exceptions.cjs +70 -0
- package/batteries/llm/webllm_chat_completions/exceptions.cjs.map +1 -0
- package/batteries/llm/webllm_chat_completions/exceptions.d.ts +74 -0
- package/batteries/llm/webllm_chat_completions/exceptions.mjs +65 -0
- package/batteries/llm/webllm_chat_completions/exceptions.mjs.map +1 -0
- package/batteries/llm/webllm_chat_completions/helpers.cjs +38 -0
- package/batteries/llm/webllm_chat_completions/helpers.d.ts +6 -0
- package/batteries/llm/webllm_chat_completions/helpers.mjs +2 -0
- package/batteries/llm/webllm_chat_completions/index.d.ts +25 -0
- package/batteries/llm/webllm_chat_completions/types.d.ts +31 -0
- package/batteries/llm/webllm_chat_completions/validation.cjs +115 -0
- package/batteries/llm/webllm_chat_completions/validation.cjs.map +1 -0
- package/batteries/llm/webllm_chat_completions/validation.d.ts +8 -0
- package/batteries/llm/webllm_chat_completions/validation.mjs +112 -0
- package/batteries/llm/webllm_chat_completions/validation.mjs.map +1 -0
- package/batteries/llm/webllm_chat_completions.cjs +50 -0
- package/batteries/llm/webllm_chat_completions.mjs +6 -0
- package/batteries/llm.cjs +63 -0
- package/batteries/llm.mjs +10 -0
- package/batteries/storage/flydrive/index.d.ts +167 -0
- package/batteries/storage/flydrive.cjs +249 -0
- package/batteries/storage/flydrive.cjs.map +1 -0
- package/batteries/storage/flydrive.mjs +249 -0
- package/batteries/storage/flydrive.mjs.map +1 -0
- package/batteries/storage/in_memory/index.d.ts +106 -0
- package/batteries/storage/in_memory.cjs +121 -0
- package/batteries/storage/in_memory.cjs.map +1 -0
- package/batteries/storage/in_memory.mjs +119 -0
- package/batteries/storage/in_memory.mjs.map +1 -0
- package/batteries/storage/index.d.ts +18 -0
- package/batteries/storage/opfs/index.d.ts +299 -0
- package/batteries/storage/opfs.cjs +368 -0
- package/batteries/storage/opfs.cjs.map +1 -0
- package/batteries/storage/opfs.mjs +366 -0
- package/batteries/storage/opfs.mjs.map +1 -0
- package/batteries/storage.cjs +4 -0
- package/batteries/storage.mjs +2 -0
- package/batteries/tools/color/index.d.ts +37 -0
- package/batteries/tools/color.cjs +659 -0
- package/batteries/tools/color.cjs.map +1 -0
- package/batteries/tools/color.mjs +655 -0
- package/batteries/tools/color.mjs.map +1 -0
- package/batteries/tools/comparison/index.d.ts +29 -0
- package/batteries/tools/comparison.cjs +171 -0
- package/batteries/tools/comparison.cjs.map +1 -0
- package/batteries/tools/comparison.mjs +168 -0
- package/batteries/tools/comparison.mjs.map +1 -0
- package/batteries/tools/data_structure/index.d.ts +30 -0
- package/batteries/tools/data_structure.cjs +270 -0
- package/batteries/tools/data_structure.cjs.map +1 -0
- package/batteries/tools/data_structure.mjs +267 -0
- package/batteries/tools/data_structure.mjs.map +1 -0
- package/batteries/tools/datetime_extended/index.d.ts +51 -0
- package/batteries/tools/datetime_extended.cjs +309 -0
- package/batteries/tools/datetime_extended.cjs.map +1 -0
- package/batteries/tools/datetime_extended.mjs +302 -0
- package/batteries/tools/datetime_extended.mjs.map +1 -0
- package/batteries/tools/datetime_math/index.d.ts +36 -0
- package/batteries/tools/datetime_math.cjs +175 -0
- package/batteries/tools/datetime_math.cjs.map +1 -0
- package/batteries/tools/datetime_math.mjs +171 -0
- package/batteries/tools/datetime_math.mjs.map +1 -0
- package/batteries/tools/encoding/index.d.ts +36 -0
- package/batteries/tools/encoding.cjs +156 -0
- package/batteries/tools/encoding.cjs.map +1 -0
- package/batteries/tools/encoding.mjs +152 -0
- package/batteries/tools/encoding.mjs.map +1 -0
- package/batteries/tools/formatting/index.d.ts +28 -0
- package/batteries/tools/formatting.cjs +120 -0
- package/batteries/tools/formatting.cjs.map +1 -0
- package/batteries/tools/formatting.mjs +117 -0
- package/batteries/tools/formatting.mjs.map +1 -0
- package/batteries/tools/geo_basics/index.d.ts +33 -0
- package/batteries/tools/geo_basics.cjs +136 -0
- package/batteries/tools/geo_basics.cjs.map +1 -0
- package/batteries/tools/geo_basics.mjs +132 -0
- package/batteries/tools/geo_basics.mjs.map +1 -0
- package/batteries/tools/index.d.ts +32 -0
- package/batteries/tools/math/index.d.ts +37 -0
- package/batteries/tools/math.cjs +136 -0
- package/batteries/tools/math.cjs.map +1 -0
- package/batteries/tools/math.mjs +133 -0
- package/batteries/tools/math.mjs.map +1 -0
- package/batteries/tools/memory/index.d.ts +73 -0
- package/batteries/tools/memory.cjs +193 -0
- package/batteries/tools/memory.cjs.map +1 -0
- package/batteries/tools/memory.mjs +187 -0
- package/batteries/tools/memory.mjs.map +1 -0
- package/batteries/tools/parsing/index.d.ts +47 -0
- package/batteries/tools/parsing.cjs +191 -0
- package/batteries/tools/parsing.cjs.map +1 -0
- package/batteries/tools/parsing.mjs +185 -0
- package/batteries/tools/parsing.mjs.map +1 -0
- package/batteries/tools/retrievables/index.d.ts +81 -0
- package/batteries/tools/retrievables.cjs +215 -0
- package/batteries/tools/retrievables.cjs.map +1 -0
- package/batteries/tools/retrievables.mjs +209 -0
- package/batteries/tools/retrievables.mjs.map +1 -0
- package/batteries/tools/standing_instructions/index.d.ts +64 -0
- package/batteries/tools/standing_instructions.cjs +126 -0
- package/batteries/tools/standing_instructions.cjs.map +1 -0
- package/batteries/tools/standing_instructions.mjs +121 -0
- package/batteries/tools/standing_instructions.mjs.map +1 -0
- package/batteries/tools/statistics/index.d.ts +46 -0
- package/batteries/tools/statistics.cjs +253 -0
- package/batteries/tools/statistics.cjs.map +1 -0
- package/batteries/tools/statistics.mjs +248 -0
- package/batteries/tools/statistics.mjs.map +1 -0
- package/batteries/tools/string_processing/index.d.ts +29 -0
- package/batteries/tools/string_processing.cjs +154 -0
- package/batteries/tools/string_processing.cjs.map +1 -0
- package/batteries/tools/string_processing.mjs +151 -0
- package/batteries/tools/string_processing.mjs.map +1 -0
- package/batteries/tools/structured_data/index.d.ts +34 -0
- package/batteries/tools/structured_data.cjs +189 -0
- package/batteries/tools/structured_data.cjs.map +1 -0
- package/batteries/tools/structured_data.mjs +185 -0
- package/batteries/tools/structured_data.mjs.map +1 -0
- package/batteries/tools/text_analysis/index.d.ts +31 -0
- package/batteries/tools/text_analysis.cjs +120 -0
- package/batteries/tools/text_analysis.cjs.map +1 -0
- package/batteries/tools/text_analysis.mjs +117 -0
- package/batteries/tools/text_analysis.mjs.map +1 -0
- package/batteries/tools/text_comparison/index.d.ts +28 -0
- package/batteries/tools/text_comparison.cjs +96 -0
- package/batteries/tools/text_comparison.cjs.map +1 -0
- package/batteries/tools/text_comparison.mjs +93 -0
- package/batteries/tools/text_comparison.mjs.map +1 -0
- package/batteries/tools/time/index.d.ts +27 -0
- package/batteries/tools/time.cjs +63 -0
- package/batteries/tools/time.cjs.map +1 -0
- package/batteries/tools/time.mjs +60 -0
- package/batteries/tools/time.mjs.map +1 -0
- package/batteries/tools/unit_conversion/index.d.ts +19 -0
- package/batteries/tools/unit_conversion.cjs +452 -0
- package/batteries/tools/unit_conversion.cjs.map +1 -0
- package/batteries/tools/unit_conversion.mjs +450 -0
- package/batteries/tools/unit_conversion.mjs.map +1 -0
- package/batteries/tools.cjs +80 -0
- package/batteries/tools.mjs +21 -0
- package/batteries.cjs +142 -0
- package/batteries.mjs +30 -0
- package/chunk-KmRHZBOW.js +35 -0
- package/common-DeZaonK1.mjs +208 -0
- package/common-DeZaonK1.mjs.map +1 -0
- package/common-Od8edUXU.js +232 -0
- package/common-Od8edUXU.js.map +1 -0
- package/common.cjs +31 -0
- package/common.d.ts +108 -0
- package/common.mjs +8 -0
- package/dispatch_runner-9j6bXHL3.mjs +1609 -0
- package/dispatch_runner-9j6bXHL3.mjs.map +1 -0
- package/dispatch_runner-CsoH0nld.js +1627 -0
- package/dispatch_runner-CsoH0nld.js.map +1 -0
- package/dispatch_runner.cjs +3 -0
- package/dispatch_runner.d.ts +17 -0
- package/dispatch_runner.mjs +2 -0
- package/exceptions-D5YrO9Vm.js +280 -0
- package/exceptions-D5YrO9Vm.js.map +1 -0
- package/exceptions-NrzIHw_R.mjs +244 -0
- package/exceptions-NrzIHw_R.mjs.map +1 -0
- package/exceptions.cjs +33 -0
- package/exceptions.d.ts +52 -0
- package/exceptions.mjs +3 -0
- package/factories.cjs +4 -0
- package/factories.d.ts +39 -0
- package/factories.mjs +2 -0
- package/forge.cjs +9 -0
- package/forge.d.ts +49 -0
- package/forge.mjs +5 -0
- package/guards.cjs +96 -0
- package/guards.cjs.map +1 -0
- package/guards.d.ts +83 -0
- package/guards.mjs +72 -0
- package/guards.mjs.map +1 -0
- package/index.cjs +107 -0
- package/index.cjs.map +1 -0
- package/index.d.ts +18 -0
- package/index.mjs +31 -0
- package/index.mjs.map +1 -0
- package/lib/classes/artifact_tool.d.ts +129 -0
- package/lib/classes/base_exception.d.ts +83 -0
- package/lib/classes/identity.d.ts +71 -0
- package/lib/classes/media.d.ts +326 -0
- package/lib/classes/memory.d.ts +72 -0
- package/lib/classes/message.d.ts +137 -0
- package/lib/classes/registry.d.ts +79 -0
- package/lib/classes/retrievable.d.ts +100 -0
- package/lib/classes/spooled_artifact.d.ts +296 -0
- package/lib/classes/spooled_json_artifact.d.ts +158 -0
- package/lib/classes/spooled_markdown_artifact.d.ts +202 -0
- package/lib/classes/thought.d.ts +142 -0
- package/lib/classes/tokenizable.d.ts +124 -0
- package/lib/classes/tool.d.ts +228 -0
- package/lib/classes/tool_call.d.ts +190 -0
- package/lib/classes/tool_registry.d.ts +159 -0
- package/lib/classes/turn_gate.d.ts +109 -0
- package/lib/contracts/dispatch_context.d.ts +345 -0
- package/lib/contracts/media_reader.d.ts +60 -0
- package/lib/contracts/spool_reader.d.ts +80 -0
- package/lib/contracts/spooled_artifact_constructor.d.ts +38 -0
- package/lib/contracts/turn_runner_config.d.ts +101 -0
- package/lib/contracts/turn_runner_context.d.ts +267 -0
- package/lib/dispatch_runner.d.ts +98 -0
- package/lib/exceptions/runtime.d.ts +370 -0
- package/lib/helpers/media_readers.d.ts +39 -0
- package/lib/turn_runner.d.ts +144 -0
- package/lib/types/dispatch_context.d.ts +233 -0
- package/lib/types/dispatch_runner.d.ts +387 -0
- package/lib/types/turn_runner.d.ts +322 -0
- package/lib/utils/canonical_json.d.ts +18 -0
- package/lib/utils/exceptions.d.ts +78 -0
- package/lib/utils/guards.d.ts +32 -0
- package/lib/utils/validation.d.ts +77 -0
- package/package.json +334 -0
- package/runtime-BJVkrGQe.js +519 -0
- package/runtime-BJVkrGQe.js.map +1 -0
- package/runtime-CrEPIFgr.mjs +346 -0
- package/runtime-CrEPIFgr.mjs.map +1 -0
- package/skills/adk-assembly/SKILL.md +109 -0
- package/skills/adk-assembly/references/assembly-contract.md +66 -0
- package/skills/adk-assembly/references/executors-tools-pipelines-events.md +113 -0
- package/skills/adk-assembly/references/first-integration.md +93 -0
- package/skills/adk-assembly/references/storage-and-context.md +102 -0
- package/spooled_artifact-C5ZtGxuJ.mjs +544 -0
- package/spooled_artifact-C5ZtGxuJ.mjs.map +1 -0
- package/spooled_artifact-Cm9Te22K.js +568 -0
- package/spooled_artifact-Cm9Te22K.js.map +1 -0
- package/spooled_artifact.cjs +7 -0
- package/spooled_artifact.d.ts +40 -0
- package/spooled_artifact.mjs +3 -0
- package/spooled_markdown_artifact-BpUJol0W.mjs +771 -0
- package/spooled_markdown_artifact-BpUJol0W.mjs.map +1 -0
- package/spooled_markdown_artifact-RRB113sy.js +786 -0
- package/spooled_markdown_artifact-RRB113sy.js.map +1 -0
- package/thought-CDb457b4.mjs +470 -0
- package/thought-CDb457b4.mjs.map +1 -0
- package/thought-DuN2PgdO.js +494 -0
- package/thought-DuN2PgdO.js.map +1 -0
- package/tool-COSeH8I6.js +302 -0
- package/tool-COSeH8I6.js.map +1 -0
- package/tool-D2WB1EA1.mjs +296 -0
- package/tool-D2WB1EA1.mjs.map +1 -0
- package/tool_call-BKyyxGaZ.mjs +578 -0
- package/tool_call-BKyyxGaZ.mjs.map +1 -0
- package/tool_call-DFgzcVcU.js +608 -0
- package/tool_call-DFgzcVcU.js.map +1 -0
- package/tool_registry-Dkfprsck.js +641 -0
- package/tool_registry-Dkfprsck.js.map +1 -0
- package/tool_registry-DqLOyGyG.mjs +592 -0
- package/tool_registry-DqLOyGyG.mjs.map +1 -0
- package/turn_runner-CMm2BHdX.js +615 -0
- package/turn_runner-CMm2BHdX.js.map +1 -0
- package/turn_runner-y7eyEcJH.mjs +603 -0
- package/turn_runner-y7eyEcJH.mjs.map +1 -0
- package/turn_runner.cjs +3 -0
- package/turn_runner.d.ts +21 -0
- package/turn_runner.mjs +2 -0
- package/types.cjs +1 -0
- package/types.d.ts +56 -0
- package/types.mjs +0 -0
- package/vite-env.d.ts +23 -0
|
@@ -0,0 +1,1627 @@
|
|
|
1
|
+
require("./chunk-KmRHZBOW.js");
|
|
2
|
+
const require_exceptions = require("./exceptions-D5YrO9Vm.js");
|
|
3
|
+
const require_tool_registry = require("./tool_registry-Dkfprsck.js");
|
|
4
|
+
const require_runtime = require("./runtime-BJVkrGQe.js");
|
|
5
|
+
let _nhtio_validation = require("@nhtio/validation");
|
|
6
|
+
let uuid = require("uuid");
|
|
7
|
+
let luxon = require("luxon");
|
|
8
|
+
let js_sha256 = require("js-sha256");
|
|
9
|
+
let _nhtio_hooks = require("@nhtio/hooks");
|
|
10
|
+
let _nhtio_middleware = require("@nhtio/middleware");
|
|
11
|
+
//#region src/lib/contracts/turn_runner_context.ts
|
|
12
|
+
/**
|
|
13
|
+
* Validator schema used to validate a {@link RawTurnContext} before constructing a {@link TurnContext}.
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* Validates all four fields of {@link RawTurnContext}:
|
|
17
|
+
* - `turnAbortController` — required `AbortController` instance.
|
|
18
|
+
* - `stash` — optional string-keyed object; defaults to `{}`.
|
|
19
|
+
* - `systemPrompt` — required string or {@link @nhtio/adk!Tokenizable}, via {@link @nhtio/adk!Tokenizable.schema}.
|
|
20
|
+
* - `standingInstructions` — optional array of strings or {@link @nhtio/adk!Tokenizable} instances, each
|
|
21
|
+
* validated via {@link @nhtio/adk!Tokenizable.schema}; defaults to `[]`.
|
|
22
|
+
*
|
|
23
|
+
* Throws a `ValidationException` (via {@link validateOrThrow}) when validation fails.
|
|
24
|
+
*/
|
|
25
|
+
var turnContextSchema = _nhtio_validation.validator.object({
|
|
26
|
+
turnAbortController: _nhtio_validation.validator.alternatives(_nhtio_validation.validator.object().instance(AbortController), _nhtio_validation.validator.function().instance(AbortController)).required(),
|
|
27
|
+
stash: _nhtio_validation.validator.object().pattern(_nhtio_validation.validator.string(), _nhtio_validation.validator.any()).default({}),
|
|
28
|
+
systemPrompt: require_tool_registry.Tokenizable.schema.required(),
|
|
29
|
+
standingInstructions: _nhtio_validation.validator.array().items(require_tool_registry.Tokenizable.schema).default([])
|
|
30
|
+
}).required();
|
|
31
|
+
/**
|
|
32
|
+
* The validated, strongly-typed context object threaded through every middleware step in a
|
|
33
|
+
* single agent turn.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* Constructed from a {@link RawTurnContext} by {@link @nhtio/adk!TurnRunner.run}. Middleware functions
|
|
37
|
+
* receive this object and use it to read and share state across pipeline steps.
|
|
38
|
+
*/
|
|
39
|
+
var TurnContext = class TurnContext {
|
|
40
|
+
/**
|
|
41
|
+
* Returns `true` if `value` is a {@link TurnContext} instance.
|
|
42
|
+
*
|
|
43
|
+
* @remarks
|
|
44
|
+
* Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety. The ADK does not export the
|
|
45
|
+
* `TurnContext` class itself as a constructable value — use this guard plus the
|
|
46
|
+
* {@link TurnContext} type for runtime detection and TypeScript narrowing.
|
|
47
|
+
*
|
|
48
|
+
* @param value - The value to test.
|
|
49
|
+
* @returns `true` when `value` is a {@link TurnContext} instance.
|
|
50
|
+
*/
|
|
51
|
+
static isTurnContext(value) {
|
|
52
|
+
return require_tool_registry.isInstanceOf(value, "TurnContext", TurnContext);
|
|
53
|
+
}
|
|
54
|
+
#id;
|
|
55
|
+
#turnAbortController;
|
|
56
|
+
#stash;
|
|
57
|
+
#systemPrompt;
|
|
58
|
+
#standingInstructions;
|
|
59
|
+
#turnMemories;
|
|
60
|
+
#turnRetrievables;
|
|
61
|
+
#turnMessages;
|
|
62
|
+
#turnThoughts;
|
|
63
|
+
#turnToolCalls;
|
|
64
|
+
/**
|
|
65
|
+
* @param raw - The raw context input validated against {@link turnContextSchema}.
|
|
66
|
+
* @param injected - Runtime callbacks supplied by `TurnRunner`; bound as instance methods so
|
|
67
|
+
* middleware can call fetch and emit methods directly on the context.
|
|
68
|
+
* @throws {@link @nhtio/adk!E_INVALID_TURN_CONTEXT} when `raw` does not satisfy the schema.
|
|
69
|
+
*
|
|
70
|
+
* @internal
|
|
71
|
+
*/
|
|
72
|
+
constructor(raw, injected) {
|
|
73
|
+
try {
|
|
74
|
+
raw = require_exceptions.validateOrThrow(turnContextSchema, raw, true);
|
|
75
|
+
} catch (err) {
|
|
76
|
+
throw new require_runtime.E_INVALID_TURN_CONTEXT({ cause: require_tool_registry.isError(err) ? err : void 0 });
|
|
77
|
+
}
|
|
78
|
+
this.#id = (0, uuid.v6)();
|
|
79
|
+
this.#turnAbortController = raw.turnAbortController;
|
|
80
|
+
this.#stash = new require_tool_registry.Registry(raw.stash);
|
|
81
|
+
this.#systemPrompt = require_tool_registry.Tokenizable.isTokenizable(raw.systemPrompt) ? raw.systemPrompt : new require_tool_registry.Tokenizable(raw.systemPrompt);
|
|
82
|
+
this.#standingInstructions = new Set((raw.standingInstructions || []).map((instr) => require_tool_registry.Tokenizable.isTokenizable(instr) ? instr : new require_tool_registry.Tokenizable(instr)));
|
|
83
|
+
this.#turnMemories = /* @__PURE__ */ new Set();
|
|
84
|
+
this.#turnRetrievables = /* @__PURE__ */ new Set();
|
|
85
|
+
this.#turnMessages = /* @__PURE__ */ new Set();
|
|
86
|
+
this.#turnThoughts = /* @__PURE__ */ new Set();
|
|
87
|
+
this.#turnToolCalls = /* @__PURE__ */ new Set();
|
|
88
|
+
Object.defineProperties(this, {
|
|
89
|
+
id: {
|
|
90
|
+
get: () => this.#id,
|
|
91
|
+
enumerable: true,
|
|
92
|
+
configurable: false
|
|
93
|
+
},
|
|
94
|
+
aborted: {
|
|
95
|
+
get: () => Boolean(this.#turnAbortController.signal.aborted),
|
|
96
|
+
enumerable: true,
|
|
97
|
+
configurable: false
|
|
98
|
+
},
|
|
99
|
+
abortSignal: {
|
|
100
|
+
get: () => this.#turnAbortController.signal,
|
|
101
|
+
enumerable: true,
|
|
102
|
+
configurable: false
|
|
103
|
+
},
|
|
104
|
+
abort: {
|
|
105
|
+
value: (reason) => this.#turnAbortController.abort(reason),
|
|
106
|
+
enumerable: true,
|
|
107
|
+
configurable: false,
|
|
108
|
+
writable: false
|
|
109
|
+
},
|
|
110
|
+
stash: {
|
|
111
|
+
get: () => this.#stash,
|
|
112
|
+
enumerable: true,
|
|
113
|
+
configurable: false
|
|
114
|
+
},
|
|
115
|
+
systemPrompt: {
|
|
116
|
+
get: () => this.#systemPrompt,
|
|
117
|
+
enumerable: true,
|
|
118
|
+
configurable: false
|
|
119
|
+
},
|
|
120
|
+
standingInstructions: {
|
|
121
|
+
get: () => this.#standingInstructions,
|
|
122
|
+
enumerable: true,
|
|
123
|
+
configurable: false
|
|
124
|
+
},
|
|
125
|
+
turnMemories: {
|
|
126
|
+
get: () => this.#turnMemories,
|
|
127
|
+
enumerable: true,
|
|
128
|
+
configurable: false
|
|
129
|
+
},
|
|
130
|
+
turnRetrievables: {
|
|
131
|
+
get: () => this.#turnRetrievables,
|
|
132
|
+
enumerable: true,
|
|
133
|
+
configurable: false
|
|
134
|
+
},
|
|
135
|
+
turnMessages: {
|
|
136
|
+
get: () => this.#turnMessages,
|
|
137
|
+
enumerable: true,
|
|
138
|
+
configurable: false
|
|
139
|
+
},
|
|
140
|
+
turnThoughts: {
|
|
141
|
+
get: () => this.#turnThoughts,
|
|
142
|
+
enumerable: true,
|
|
143
|
+
configurable: false
|
|
144
|
+
},
|
|
145
|
+
turnToolCalls: {
|
|
146
|
+
get: () => this.#turnToolCalls,
|
|
147
|
+
enumerable: true,
|
|
148
|
+
configurable: false
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
Object.defineProperties(this, {
|
|
152
|
+
fetchMemories: {
|
|
153
|
+
value: () => injected.fetchMemories(this),
|
|
154
|
+
enumerable: true,
|
|
155
|
+
configurable: false,
|
|
156
|
+
writable: false
|
|
157
|
+
},
|
|
158
|
+
fetchMessages: {
|
|
159
|
+
value: () => injected.fetchMessages(this),
|
|
160
|
+
enumerable: true,
|
|
161
|
+
configurable: false,
|
|
162
|
+
writable: false
|
|
163
|
+
},
|
|
164
|
+
fetchThoughts: {
|
|
165
|
+
value: () => injected.fetchThoughts(this),
|
|
166
|
+
enumerable: true,
|
|
167
|
+
configurable: false,
|
|
168
|
+
writable: false
|
|
169
|
+
},
|
|
170
|
+
fetchToolCalls: {
|
|
171
|
+
value: () => injected.fetchToolCalls(this),
|
|
172
|
+
enumerable: true,
|
|
173
|
+
configurable: false,
|
|
174
|
+
writable: false
|
|
175
|
+
},
|
|
176
|
+
fetchTools: {
|
|
177
|
+
value: () => injected.fetchTools(this),
|
|
178
|
+
enumerable: true,
|
|
179
|
+
configurable: false,
|
|
180
|
+
writable: false
|
|
181
|
+
},
|
|
182
|
+
refreshStandingInstructions: {
|
|
183
|
+
value: () => injected.refreshStandingInstructions(this),
|
|
184
|
+
enumerable: true,
|
|
185
|
+
configurable: false,
|
|
186
|
+
writable: false
|
|
187
|
+
},
|
|
188
|
+
storeStandingInstruction: {
|
|
189
|
+
value: (v) => injected.storeStandingInstruction(this, v),
|
|
190
|
+
enumerable: true,
|
|
191
|
+
configurable: false,
|
|
192
|
+
writable: false
|
|
193
|
+
},
|
|
194
|
+
mutateStandingInstruction: {
|
|
195
|
+
value: (v) => injected.mutateStandingInstruction(this, v),
|
|
196
|
+
enumerable: true,
|
|
197
|
+
configurable: false,
|
|
198
|
+
writable: false
|
|
199
|
+
},
|
|
200
|
+
deleteStandingInstruction: {
|
|
201
|
+
value: (v) => injected.deleteStandingInstruction(this, v),
|
|
202
|
+
enumerable: true,
|
|
203
|
+
configurable: false,
|
|
204
|
+
writable: false
|
|
205
|
+
},
|
|
206
|
+
storeMemory: {
|
|
207
|
+
value: (v) => injected.storeMemory(this, v),
|
|
208
|
+
enumerable: true,
|
|
209
|
+
configurable: false,
|
|
210
|
+
writable: false
|
|
211
|
+
},
|
|
212
|
+
mutateMemory: {
|
|
213
|
+
value: (v) => injected.mutateMemory(this, v),
|
|
214
|
+
enumerable: true,
|
|
215
|
+
configurable: false,
|
|
216
|
+
writable: false
|
|
217
|
+
},
|
|
218
|
+
deleteMemory: {
|
|
219
|
+
value: (id) => injected.deleteMemory(this, id),
|
|
220
|
+
enumerable: true,
|
|
221
|
+
configurable: false,
|
|
222
|
+
writable: false
|
|
223
|
+
},
|
|
224
|
+
fetchRetrievables: {
|
|
225
|
+
value: () => injected.fetchRetrievables(this),
|
|
226
|
+
enumerable: true,
|
|
227
|
+
configurable: false,
|
|
228
|
+
writable: false
|
|
229
|
+
},
|
|
230
|
+
storeRetrievable: {
|
|
231
|
+
value: (v) => injected.storeRetrievable(this, v),
|
|
232
|
+
enumerable: true,
|
|
233
|
+
configurable: false,
|
|
234
|
+
writable: false
|
|
235
|
+
},
|
|
236
|
+
mutateRetrievable: {
|
|
237
|
+
value: (v) => injected.mutateRetrievable(this, v),
|
|
238
|
+
enumerable: true,
|
|
239
|
+
configurable: false,
|
|
240
|
+
writable: false
|
|
241
|
+
},
|
|
242
|
+
deleteRetrievable: {
|
|
243
|
+
value: (id) => injected.deleteRetrievable(this, id),
|
|
244
|
+
enumerable: true,
|
|
245
|
+
configurable: false,
|
|
246
|
+
writable: false
|
|
247
|
+
},
|
|
248
|
+
storeMessage: {
|
|
249
|
+
value: (v) => injected.storeMessage(this, v),
|
|
250
|
+
enumerable: true,
|
|
251
|
+
configurable: false,
|
|
252
|
+
writable: false
|
|
253
|
+
},
|
|
254
|
+
mutateMessage: {
|
|
255
|
+
value: (v) => injected.mutateMessage(this, v),
|
|
256
|
+
enumerable: true,
|
|
257
|
+
configurable: false,
|
|
258
|
+
writable: false
|
|
259
|
+
},
|
|
260
|
+
deleteMessage: {
|
|
261
|
+
value: (id) => injected.deleteMessage(this, id),
|
|
262
|
+
enumerable: true,
|
|
263
|
+
configurable: false,
|
|
264
|
+
writable: false
|
|
265
|
+
},
|
|
266
|
+
storeThought: {
|
|
267
|
+
value: (v) => injected.storeThought(this, v),
|
|
268
|
+
enumerable: true,
|
|
269
|
+
configurable: false,
|
|
270
|
+
writable: false
|
|
271
|
+
},
|
|
272
|
+
mutateThought: {
|
|
273
|
+
value: (v) => injected.mutateThought(this, v),
|
|
274
|
+
enumerable: true,
|
|
275
|
+
configurable: false,
|
|
276
|
+
writable: false
|
|
277
|
+
},
|
|
278
|
+
deleteThought: {
|
|
279
|
+
value: (id) => injected.deleteThought(this, id),
|
|
280
|
+
enumerable: true,
|
|
281
|
+
configurable: false,
|
|
282
|
+
writable: false
|
|
283
|
+
},
|
|
284
|
+
storeToolCall: {
|
|
285
|
+
value: (v) => injected.storeToolCall(this, v),
|
|
286
|
+
enumerable: true,
|
|
287
|
+
configurable: false,
|
|
288
|
+
writable: false
|
|
289
|
+
},
|
|
290
|
+
mutateToolCall: {
|
|
291
|
+
value: (v) => injected.mutateToolCall(this, v),
|
|
292
|
+
enumerable: true,
|
|
293
|
+
configurable: false,
|
|
294
|
+
writable: false
|
|
295
|
+
},
|
|
296
|
+
deleteToolCall: {
|
|
297
|
+
value: (id) => injected.deleteToolCall(this, id),
|
|
298
|
+
enumerable: true,
|
|
299
|
+
configurable: false,
|
|
300
|
+
writable: false
|
|
301
|
+
},
|
|
302
|
+
emitMessage: {
|
|
303
|
+
value: injected.emitMessage,
|
|
304
|
+
enumerable: true,
|
|
305
|
+
configurable: false,
|
|
306
|
+
writable: false
|
|
307
|
+
},
|
|
308
|
+
emitThought: {
|
|
309
|
+
value: injected.emitThought,
|
|
310
|
+
enumerable: true,
|
|
311
|
+
configurable: false,
|
|
312
|
+
writable: false
|
|
313
|
+
},
|
|
314
|
+
emitToolCall: {
|
|
315
|
+
value: injected.emitToolCall,
|
|
316
|
+
enumerable: true,
|
|
317
|
+
configurable: false,
|
|
318
|
+
writable: false
|
|
319
|
+
},
|
|
320
|
+
emitToolExecutionStart: {
|
|
321
|
+
value: injected.emitToolExecutionStart,
|
|
322
|
+
enumerable: true,
|
|
323
|
+
configurable: false,
|
|
324
|
+
writable: false
|
|
325
|
+
},
|
|
326
|
+
emitToolExecutionEnd: {
|
|
327
|
+
value: injected.emitToolExecutionEnd,
|
|
328
|
+
enumerable: true,
|
|
329
|
+
configurable: false,
|
|
330
|
+
writable: false
|
|
331
|
+
},
|
|
332
|
+
waitFor: {
|
|
333
|
+
value: injected.openGate,
|
|
334
|
+
enumerable: true,
|
|
335
|
+
configurable: false,
|
|
336
|
+
writable: false
|
|
337
|
+
},
|
|
338
|
+
tools: {
|
|
339
|
+
get: () => injected.tools,
|
|
340
|
+
enumerable: true,
|
|
341
|
+
configurable: false
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
//#endregion
|
|
347
|
+
//#region src/lib/contracts/dispatch_context.ts
|
|
348
|
+
var rawDispatchContextSchema = _nhtio_validation.validator.object({
|
|
349
|
+
turnAbortController: _nhtio_validation.validator.alternatives(_nhtio_validation.validator.object().instance(AbortController), _nhtio_validation.validator.function().instance(AbortController)).optional(),
|
|
350
|
+
stash: _nhtio_validation.validator.object().pattern(_nhtio_validation.validator.string(), _nhtio_validation.validator.any()).default({}),
|
|
351
|
+
systemPrompt: require_tool_registry.Tokenizable.schema.required(),
|
|
352
|
+
standingInstructions: _nhtio_validation.validator.array().items(require_tool_registry.Tokenizable.schema).default([]),
|
|
353
|
+
memories: _nhtio_validation.validator.array().default([]),
|
|
354
|
+
retrievables: _nhtio_validation.validator.array().default([]),
|
|
355
|
+
messages: _nhtio_validation.validator.array().default([]),
|
|
356
|
+
thoughts: _nhtio_validation.validator.array().default([]),
|
|
357
|
+
toolCalls: _nhtio_validation.validator.array().default([]),
|
|
358
|
+
tools: _nhtio_validation.validator.array().default([]),
|
|
359
|
+
fetchMemories: _nhtio_validation.validator.function().required(),
|
|
360
|
+
fetchRetrievables: _nhtio_validation.validator.function().required(),
|
|
361
|
+
fetchMessages: _nhtio_validation.validator.function().required(),
|
|
362
|
+
fetchThoughts: _nhtio_validation.validator.function().required(),
|
|
363
|
+
fetchToolCalls: _nhtio_validation.validator.function().required(),
|
|
364
|
+
fetchTools: _nhtio_validation.validator.function().required(),
|
|
365
|
+
refreshStandingInstructions: _nhtio_validation.validator.function().required(),
|
|
366
|
+
storeStandingInstruction: _nhtio_validation.validator.function().required(),
|
|
367
|
+
mutateStandingInstruction: _nhtio_validation.validator.function().required(),
|
|
368
|
+
deleteStandingInstruction: _nhtio_validation.validator.function().required(),
|
|
369
|
+
storeMemory: _nhtio_validation.validator.function().required(),
|
|
370
|
+
mutateMemory: _nhtio_validation.validator.function().required(),
|
|
371
|
+
deleteMemory: _nhtio_validation.validator.function().required(),
|
|
372
|
+
storeRetrievable: _nhtio_validation.validator.function().required(),
|
|
373
|
+
mutateRetrievable: _nhtio_validation.validator.function().required(),
|
|
374
|
+
deleteRetrievable: _nhtio_validation.validator.function().required(),
|
|
375
|
+
storeMessage: _nhtio_validation.validator.function().required(),
|
|
376
|
+
mutateMessage: _nhtio_validation.validator.function().required(),
|
|
377
|
+
deleteMessage: _nhtio_validation.validator.function().required(),
|
|
378
|
+
storeThought: _nhtio_validation.validator.function().required(),
|
|
379
|
+
mutateThought: _nhtio_validation.validator.function().required(),
|
|
380
|
+
deleteThought: _nhtio_validation.validator.function().required(),
|
|
381
|
+
storeToolCall: _nhtio_validation.validator.function().required(),
|
|
382
|
+
mutateToolCall: _nhtio_validation.validator.function().required(),
|
|
383
|
+
deleteToolCall: _nhtio_validation.validator.function().required(),
|
|
384
|
+
hooks: _nhtio_validation.validator.object().optional(),
|
|
385
|
+
waitFor: _nhtio_validation.validator.function().optional()
|
|
386
|
+
});
|
|
387
|
+
/**
|
|
388
|
+
* Context object for a single LLM execution call.
|
|
389
|
+
*
|
|
390
|
+
* @remarks
|
|
391
|
+
* Mirrors the surface of {@link @nhtio/adk!TurnContext} but is path-agnostic — it knows nothing about a
|
|
392
|
+
* parent context. Mutations apply to local Sets immediately, call persistence callbacks
|
|
393
|
+
* immediately, and fire the corresponding mutation hook (`storedMemory`, `mutatedMemory`,
|
|
394
|
+
* `deletedMemory`, etc.) in both standalone and derived dispatches.
|
|
395
|
+
*
|
|
396
|
+
* The {@link @nhtio/adk!DispatchRunner} is the only thing that creates a context with a parent
|
|
397
|
+
* relationship: when dispatched with a `source: TurnContext`, the runner subscribes to the
|
|
398
|
+
* mutation hooks, queues deltas internally, and flushes them to the parent's Sets at the end of
|
|
399
|
+
* each iteration. The context itself remains unaware of the parent.
|
|
400
|
+
*
|
|
401
|
+
* Middleware/executor signals termination via {@link DispatchContext.ack} (clean completion)
|
|
402
|
+
* or {@link DispatchContext.nack} (failure). Both set an internal flag the runner reads at
|
|
403
|
+
* end-of-iteration to decide whether to loop or exit. {@link DispatchContext.isSignalled},
|
|
404
|
+
* {@link DispatchContext.isAcked}, and {@link DispatchContext.nackError} are publicly
|
|
405
|
+
* readable getters so middleware can inspect signal state and bail early.
|
|
406
|
+
*/
|
|
407
|
+
var DispatchContext = class DispatchContext {
|
|
408
|
+
#id;
|
|
409
|
+
#dispatchId;
|
|
410
|
+
#iteration;
|
|
411
|
+
#turnAbortController;
|
|
412
|
+
#stash;
|
|
413
|
+
#systemPrompt;
|
|
414
|
+
#standingInstructions;
|
|
415
|
+
#turnMemories;
|
|
416
|
+
#turnRetrievables;
|
|
417
|
+
#turnMessages;
|
|
418
|
+
#turnThoughts;
|
|
419
|
+
#turnToolCalls;
|
|
420
|
+
#tools;
|
|
421
|
+
#hooks;
|
|
422
|
+
#ackHandlers;
|
|
423
|
+
#fetchMemories;
|
|
424
|
+
#fetchRetrievables;
|
|
425
|
+
#fetchMessages;
|
|
426
|
+
#fetchThoughts;
|
|
427
|
+
#fetchToolCalls;
|
|
428
|
+
#fetchTools;
|
|
429
|
+
#refreshStandingInstructions;
|
|
430
|
+
#storeStandingInstruction;
|
|
431
|
+
#mutateStandingInstruction;
|
|
432
|
+
#deleteStandingInstruction;
|
|
433
|
+
#storeMemory;
|
|
434
|
+
#mutateMemory;
|
|
435
|
+
#deleteMemory;
|
|
436
|
+
#storeRetrievable;
|
|
437
|
+
#mutateRetrievable;
|
|
438
|
+
#deleteRetrievable;
|
|
439
|
+
#storeMessage;
|
|
440
|
+
#mutateMessage;
|
|
441
|
+
#deleteMessage;
|
|
442
|
+
#storeThought;
|
|
443
|
+
#mutateThought;
|
|
444
|
+
#deleteThought;
|
|
445
|
+
#storeToolCall;
|
|
446
|
+
#mutateToolCall;
|
|
447
|
+
#deleteToolCall;
|
|
448
|
+
#waitFor;
|
|
449
|
+
#toolCallChecksums;
|
|
450
|
+
#signalled;
|
|
451
|
+
#nackError;
|
|
452
|
+
/**
|
|
453
|
+
* @param raw - Raw input validated against the schema.
|
|
454
|
+
* @throws {@link @nhtio/adk!E_INVALID_LLM_EXECUTION_CONTEXT} when `raw` does not satisfy the schema.
|
|
455
|
+
*/
|
|
456
|
+
constructor(raw) {
|
|
457
|
+
let resolved;
|
|
458
|
+
try {
|
|
459
|
+
resolved = require_exceptions.validateOrThrow(rawDispatchContextSchema, raw, true);
|
|
460
|
+
} catch (err) {
|
|
461
|
+
throw new require_runtime.E_INVALID_LLM_EXECUTION_CONTEXT({ cause: require_tool_registry.isError(err) ? err : void 0 });
|
|
462
|
+
}
|
|
463
|
+
this.#id = (0, uuid.v6)();
|
|
464
|
+
this.#dispatchId = "";
|
|
465
|
+
this.#iteration = 0;
|
|
466
|
+
this.#turnAbortController = resolved.turnAbortController ?? new AbortController();
|
|
467
|
+
this.#stash = new require_tool_registry.Registry(resolved.stash);
|
|
468
|
+
this.#systemPrompt = require_tool_registry.Tokenizable.isTokenizable(resolved.systemPrompt) ? resolved.systemPrompt : new require_tool_registry.Tokenizable(resolved.systemPrompt);
|
|
469
|
+
this.#standingInstructions = new Set(resolved.standingInstructions.map((i) => require_tool_registry.Tokenizable.isTokenizable(i) ? i : new require_tool_registry.Tokenizable(i)));
|
|
470
|
+
this.#turnMemories = new Set(resolved.memories);
|
|
471
|
+
this.#turnRetrievables = new Set(resolved.retrievables);
|
|
472
|
+
this.#turnMessages = new Set(resolved.messages);
|
|
473
|
+
this.#turnThoughts = new Set(resolved.thoughts);
|
|
474
|
+
this.#turnToolCalls = new Set(resolved.toolCalls);
|
|
475
|
+
this.#tools = new require_tool_registry.ToolRegistry(resolved.tools);
|
|
476
|
+
this.#toolCallChecksums = /* @__PURE__ */ new Map();
|
|
477
|
+
for (const tc of resolved.toolCalls) {
|
|
478
|
+
const prev = this.#toolCallChecksums.get(tc.checksum) ?? 0;
|
|
479
|
+
this.#toolCallChecksums.set(tc.checksum, prev + 1);
|
|
480
|
+
}
|
|
481
|
+
this.#fetchMemories = resolved.fetchMemories;
|
|
482
|
+
this.#fetchRetrievables = resolved.fetchRetrievables;
|
|
483
|
+
this.#fetchMessages = resolved.fetchMessages;
|
|
484
|
+
this.#fetchThoughts = resolved.fetchThoughts;
|
|
485
|
+
this.#fetchToolCalls = resolved.fetchToolCalls;
|
|
486
|
+
this.#fetchTools = resolved.fetchTools;
|
|
487
|
+
this.#refreshStandingInstructions = resolved.refreshStandingInstructions;
|
|
488
|
+
this.#storeStandingInstruction = resolved.storeStandingInstruction;
|
|
489
|
+
this.#mutateStandingInstruction = resolved.mutateStandingInstruction;
|
|
490
|
+
this.#deleteStandingInstruction = resolved.deleteStandingInstruction;
|
|
491
|
+
this.#storeMemory = resolved.storeMemory;
|
|
492
|
+
this.#mutateMemory = resolved.mutateMemory;
|
|
493
|
+
this.#deleteMemory = resolved.deleteMemory;
|
|
494
|
+
this.#storeRetrievable = resolved.storeRetrievable;
|
|
495
|
+
this.#mutateRetrievable = resolved.mutateRetrievable;
|
|
496
|
+
this.#deleteRetrievable = resolved.deleteRetrievable;
|
|
497
|
+
this.#storeMessage = resolved.storeMessage;
|
|
498
|
+
this.#mutateMessage = resolved.mutateMessage;
|
|
499
|
+
this.#deleteMessage = resolved.deleteMessage;
|
|
500
|
+
this.#storeThought = resolved.storeThought;
|
|
501
|
+
this.#mutateThought = resolved.mutateThought;
|
|
502
|
+
this.#deleteThought = resolved.deleteThought;
|
|
503
|
+
this.#storeToolCall = resolved.storeToolCall;
|
|
504
|
+
this.#mutateToolCall = resolved.mutateToolCall;
|
|
505
|
+
this.#deleteToolCall = resolved.deleteToolCall;
|
|
506
|
+
this.#waitFor = resolved.waitFor;
|
|
507
|
+
this.#signalled = void 0;
|
|
508
|
+
this.#nackError = void 0;
|
|
509
|
+
this.#hooks = new _nhtio_hooks.Hooks();
|
|
510
|
+
this.#ackHandlers = /* @__PURE__ */ new Set();
|
|
511
|
+
if (resolved.hooks) {
|
|
512
|
+
const regs = resolved.hooks;
|
|
513
|
+
for (const key of Object.keys(regs)) {
|
|
514
|
+
const entry = regs[key];
|
|
515
|
+
if (!entry) continue;
|
|
516
|
+
const handlers = Array.isArray(entry) ? entry : [entry];
|
|
517
|
+
for (const h of handlers) this.#hooks.add(key, h);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
Object.defineProperties(this, {
|
|
521
|
+
id: {
|
|
522
|
+
get: () => this.#id,
|
|
523
|
+
enumerable: true,
|
|
524
|
+
configurable: false
|
|
525
|
+
},
|
|
526
|
+
dispatchId: {
|
|
527
|
+
get: () => this.#dispatchId,
|
|
528
|
+
enumerable: true,
|
|
529
|
+
configurable: false
|
|
530
|
+
},
|
|
531
|
+
iteration: {
|
|
532
|
+
get: () => this.#iteration,
|
|
533
|
+
enumerable: true,
|
|
534
|
+
configurable: false
|
|
535
|
+
},
|
|
536
|
+
aborted: {
|
|
537
|
+
get: () => Boolean(this.#turnAbortController.signal.aborted),
|
|
538
|
+
enumerable: true,
|
|
539
|
+
configurable: false
|
|
540
|
+
},
|
|
541
|
+
abortSignal: {
|
|
542
|
+
get: () => this.#turnAbortController.signal,
|
|
543
|
+
enumerable: true,
|
|
544
|
+
configurable: false
|
|
545
|
+
},
|
|
546
|
+
abort: {
|
|
547
|
+
value: (reason) => this.#turnAbortController.abort(reason),
|
|
548
|
+
enumerable: true,
|
|
549
|
+
configurable: false,
|
|
550
|
+
writable: false
|
|
551
|
+
},
|
|
552
|
+
isSignalled: {
|
|
553
|
+
get: () => this.#signalled !== void 0,
|
|
554
|
+
enumerable: true,
|
|
555
|
+
configurable: false
|
|
556
|
+
},
|
|
557
|
+
isAcked: {
|
|
558
|
+
get: () => this.#signalled === "ack",
|
|
559
|
+
enumerable: true,
|
|
560
|
+
configurable: false
|
|
561
|
+
},
|
|
562
|
+
nackError: {
|
|
563
|
+
get: () => this.#nackError,
|
|
564
|
+
enumerable: true,
|
|
565
|
+
configurable: false
|
|
566
|
+
},
|
|
567
|
+
stash: {
|
|
568
|
+
get: () => this.#stash,
|
|
569
|
+
enumerable: true,
|
|
570
|
+
configurable: false
|
|
571
|
+
},
|
|
572
|
+
systemPrompt: {
|
|
573
|
+
get: () => this.#systemPrompt,
|
|
574
|
+
enumerable: true,
|
|
575
|
+
configurable: false
|
|
576
|
+
},
|
|
577
|
+
standingInstructions: {
|
|
578
|
+
get: () => this.#standingInstructions,
|
|
579
|
+
enumerable: true,
|
|
580
|
+
configurable: false
|
|
581
|
+
},
|
|
582
|
+
turnMemories: {
|
|
583
|
+
get: () => this.#turnMemories,
|
|
584
|
+
enumerable: true,
|
|
585
|
+
configurable: false
|
|
586
|
+
},
|
|
587
|
+
turnRetrievables: {
|
|
588
|
+
get: () => this.#turnRetrievables,
|
|
589
|
+
enumerable: true,
|
|
590
|
+
configurable: false
|
|
591
|
+
},
|
|
592
|
+
turnMessages: {
|
|
593
|
+
get: () => this.#turnMessages,
|
|
594
|
+
enumerable: true,
|
|
595
|
+
configurable: false
|
|
596
|
+
},
|
|
597
|
+
turnThoughts: {
|
|
598
|
+
get: () => this.#turnThoughts,
|
|
599
|
+
enumerable: true,
|
|
600
|
+
configurable: false
|
|
601
|
+
},
|
|
602
|
+
turnToolCalls: {
|
|
603
|
+
get: () => this.#turnToolCalls,
|
|
604
|
+
enumerable: true,
|
|
605
|
+
configurable: false
|
|
606
|
+
},
|
|
607
|
+
tools: {
|
|
608
|
+
get: () => this.#tools,
|
|
609
|
+
enumerable: true,
|
|
610
|
+
configurable: false
|
|
611
|
+
},
|
|
612
|
+
fetchMemories: {
|
|
613
|
+
value: () => this.#fetchMemories(this),
|
|
614
|
+
enumerable: true,
|
|
615
|
+
configurable: false,
|
|
616
|
+
writable: false
|
|
617
|
+
},
|
|
618
|
+
fetchRetrievables: {
|
|
619
|
+
value: () => this.#fetchRetrievables(this),
|
|
620
|
+
enumerable: true,
|
|
621
|
+
configurable: false,
|
|
622
|
+
writable: false
|
|
623
|
+
},
|
|
624
|
+
fetchMessages: {
|
|
625
|
+
value: () => this.#fetchMessages(this),
|
|
626
|
+
enumerable: true,
|
|
627
|
+
configurable: false,
|
|
628
|
+
writable: false
|
|
629
|
+
},
|
|
630
|
+
fetchThoughts: {
|
|
631
|
+
value: () => this.#fetchThoughts(this),
|
|
632
|
+
enumerable: true,
|
|
633
|
+
configurable: false,
|
|
634
|
+
writable: false
|
|
635
|
+
},
|
|
636
|
+
fetchToolCalls: {
|
|
637
|
+
value: () => this.#fetchToolCalls(this),
|
|
638
|
+
enumerable: true,
|
|
639
|
+
configurable: false,
|
|
640
|
+
writable: false
|
|
641
|
+
},
|
|
642
|
+
fetchTools: {
|
|
643
|
+
value: () => this.#fetchTools(this),
|
|
644
|
+
enumerable: true,
|
|
645
|
+
configurable: false,
|
|
646
|
+
writable: false
|
|
647
|
+
},
|
|
648
|
+
refreshStandingInstructions: {
|
|
649
|
+
value: () => this.#refreshStandingInstructions(this),
|
|
650
|
+
enumerable: true,
|
|
651
|
+
configurable: false,
|
|
652
|
+
writable: false
|
|
653
|
+
},
|
|
654
|
+
storeStandingInstruction: {
|
|
655
|
+
value: (v) => this.#doStoreStandingInstruction(v),
|
|
656
|
+
enumerable: true,
|
|
657
|
+
configurable: false,
|
|
658
|
+
writable: false
|
|
659
|
+
},
|
|
660
|
+
mutateStandingInstruction: {
|
|
661
|
+
value: (v) => this.#doMutateStandingInstruction(v),
|
|
662
|
+
enumerable: true,
|
|
663
|
+
configurable: false,
|
|
664
|
+
writable: false
|
|
665
|
+
},
|
|
666
|
+
deleteStandingInstruction: {
|
|
667
|
+
value: (v) => this.#doDeleteStandingInstruction(v),
|
|
668
|
+
enumerable: true,
|
|
669
|
+
configurable: false,
|
|
670
|
+
writable: false
|
|
671
|
+
},
|
|
672
|
+
storeMemory: {
|
|
673
|
+
value: (v) => this.#doStoreMemory(v),
|
|
674
|
+
enumerable: true,
|
|
675
|
+
configurable: false,
|
|
676
|
+
writable: false
|
|
677
|
+
},
|
|
678
|
+
mutateMemory: {
|
|
679
|
+
value: (v) => this.#doMutateMemory(v),
|
|
680
|
+
enumerable: true,
|
|
681
|
+
configurable: false,
|
|
682
|
+
writable: false
|
|
683
|
+
},
|
|
684
|
+
deleteMemory: {
|
|
685
|
+
value: (id) => this.#doDeleteMemory(id),
|
|
686
|
+
enumerable: true,
|
|
687
|
+
configurable: false,
|
|
688
|
+
writable: false
|
|
689
|
+
},
|
|
690
|
+
storeRetrievable: {
|
|
691
|
+
value: (v) => this.#doStoreRetrievable(v),
|
|
692
|
+
enumerable: true,
|
|
693
|
+
configurable: false,
|
|
694
|
+
writable: false
|
|
695
|
+
},
|
|
696
|
+
mutateRetrievable: {
|
|
697
|
+
value: (v) => this.#doMutateRetrievable(v),
|
|
698
|
+
enumerable: true,
|
|
699
|
+
configurable: false,
|
|
700
|
+
writable: false
|
|
701
|
+
},
|
|
702
|
+
deleteRetrievable: {
|
|
703
|
+
value: (id) => this.#doDeleteRetrievable(id),
|
|
704
|
+
enumerable: true,
|
|
705
|
+
configurable: false,
|
|
706
|
+
writable: false
|
|
707
|
+
},
|
|
708
|
+
storeMessage: {
|
|
709
|
+
value: (v) => this.#doStoreMessage(v),
|
|
710
|
+
enumerable: true,
|
|
711
|
+
configurable: false,
|
|
712
|
+
writable: false
|
|
713
|
+
},
|
|
714
|
+
mutateMessage: {
|
|
715
|
+
value: (v) => this.#doMutateMessage(v),
|
|
716
|
+
enumerable: true,
|
|
717
|
+
configurable: false,
|
|
718
|
+
writable: false
|
|
719
|
+
},
|
|
720
|
+
deleteMessage: {
|
|
721
|
+
value: (id) => this.#doDeleteMessage(id),
|
|
722
|
+
enumerable: true,
|
|
723
|
+
configurable: false,
|
|
724
|
+
writable: false
|
|
725
|
+
},
|
|
726
|
+
storeThought: {
|
|
727
|
+
value: (v) => this.#doStoreThought(v),
|
|
728
|
+
enumerable: true,
|
|
729
|
+
configurable: false,
|
|
730
|
+
writable: false
|
|
731
|
+
},
|
|
732
|
+
mutateThought: {
|
|
733
|
+
value: (v) => this.#doMutateThought(v),
|
|
734
|
+
enumerable: true,
|
|
735
|
+
configurable: false,
|
|
736
|
+
writable: false
|
|
737
|
+
},
|
|
738
|
+
deleteThought: {
|
|
739
|
+
value: (id) => this.#doDeleteThought(id),
|
|
740
|
+
enumerable: true,
|
|
741
|
+
configurable: false,
|
|
742
|
+
writable: false
|
|
743
|
+
},
|
|
744
|
+
storeToolCall: {
|
|
745
|
+
value: (v) => this.#doStoreToolCall(v),
|
|
746
|
+
enumerable: true,
|
|
747
|
+
configurable: false,
|
|
748
|
+
writable: false
|
|
749
|
+
},
|
|
750
|
+
mutateToolCall: {
|
|
751
|
+
value: (v) => this.#doMutateToolCall(v),
|
|
752
|
+
enumerable: true,
|
|
753
|
+
configurable: false,
|
|
754
|
+
writable: false
|
|
755
|
+
},
|
|
756
|
+
deleteToolCall: {
|
|
757
|
+
value: (id) => this.#doDeleteToolCall(id),
|
|
758
|
+
enumerable: true,
|
|
759
|
+
configurable: false,
|
|
760
|
+
writable: false
|
|
761
|
+
},
|
|
762
|
+
emitMessage: {
|
|
763
|
+
value: (content) => {
|
|
764
|
+
this.#hooks.runner("message").run(content);
|
|
765
|
+
},
|
|
766
|
+
enumerable: true,
|
|
767
|
+
configurable: false,
|
|
768
|
+
writable: false
|
|
769
|
+
},
|
|
770
|
+
emitThought: {
|
|
771
|
+
value: (content) => {
|
|
772
|
+
this.#hooks.runner("thought").run(content);
|
|
773
|
+
},
|
|
774
|
+
enumerable: true,
|
|
775
|
+
configurable: false,
|
|
776
|
+
writable: false
|
|
777
|
+
},
|
|
778
|
+
emitToolCall: {
|
|
779
|
+
value: (content) => {
|
|
780
|
+
this.#hooks.runner("toolCall").run(content);
|
|
781
|
+
},
|
|
782
|
+
enumerable: true,
|
|
783
|
+
configurable: false,
|
|
784
|
+
writable: false
|
|
785
|
+
},
|
|
786
|
+
emitToolExecutionStart: {
|
|
787
|
+
value: (event) => {
|
|
788
|
+
this.#hooks.runner("toolExecutionStart").run(event);
|
|
789
|
+
},
|
|
790
|
+
enumerable: true,
|
|
791
|
+
configurable: false,
|
|
792
|
+
writable: false
|
|
793
|
+
},
|
|
794
|
+
emitToolExecutionEnd: {
|
|
795
|
+
value: (event) => {
|
|
796
|
+
this.#hooks.runner("toolExecutionEnd").run(event);
|
|
797
|
+
},
|
|
798
|
+
enumerable: true,
|
|
799
|
+
configurable: false,
|
|
800
|
+
writable: false
|
|
801
|
+
},
|
|
802
|
+
waitFor: {
|
|
803
|
+
value: (gateRaw) => {
|
|
804
|
+
if (!this.#waitFor) return Promise.reject(new require_runtime.E_LLM_EXECUTION_GATE_NOT_SUPPORTED());
|
|
805
|
+
return this.#waitFor(gateRaw);
|
|
806
|
+
},
|
|
807
|
+
enumerable: true,
|
|
808
|
+
configurable: false,
|
|
809
|
+
writable: false
|
|
810
|
+
}
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
async #doStoreStandingInstruction(v) {
|
|
814
|
+
const t = require_tool_registry.Tokenizable.isTokenizable(v) ? v : new require_tool_registry.Tokenizable(v);
|
|
815
|
+
this.#standingInstructions.add(t);
|
|
816
|
+
await this.#storeStandingInstruction(this, v);
|
|
817
|
+
this.#hooks.runner("storedStandingInstruction").run(t);
|
|
818
|
+
}
|
|
819
|
+
async #doMutateStandingInstruction(v) {
|
|
820
|
+
const t = require_tool_registry.Tokenizable.isTokenizable(v) ? v : new require_tool_registry.Tokenizable(v);
|
|
821
|
+
this.#standingInstructions.add(t);
|
|
822
|
+
await this.#mutateStandingInstruction(this, v);
|
|
823
|
+
this.#hooks.runner("mutatedStandingInstruction").run(t);
|
|
824
|
+
}
|
|
825
|
+
async #doDeleteStandingInstruction(v) {
|
|
826
|
+
const t = require_tool_registry.Tokenizable.isTokenizable(v) ? v : new require_tool_registry.Tokenizable(v);
|
|
827
|
+
this.#standingInstructions.delete(t);
|
|
828
|
+
await this.#deleteStandingInstruction(this, v);
|
|
829
|
+
this.#hooks.runner("deletedStandingInstruction").run(t);
|
|
830
|
+
}
|
|
831
|
+
async #doStoreMemory(v) {
|
|
832
|
+
this.#turnMemories.add(v);
|
|
833
|
+
await this.#storeMemory(this, v);
|
|
834
|
+
this.#hooks.runner("storedMemory").run(v);
|
|
835
|
+
}
|
|
836
|
+
async #doMutateMemory(v) {
|
|
837
|
+
this.#turnMemories.add(v);
|
|
838
|
+
await this.#mutateMemory(this, v);
|
|
839
|
+
this.#hooks.runner("mutatedMemory").run(v);
|
|
840
|
+
}
|
|
841
|
+
async #doDeleteMemory(id) {
|
|
842
|
+
for (const m of this.#turnMemories) if (m.id === id) {
|
|
843
|
+
this.#turnMemories.delete(m);
|
|
844
|
+
break;
|
|
845
|
+
}
|
|
846
|
+
await this.#deleteMemory(this, id);
|
|
847
|
+
this.#hooks.runner("deletedMemory").run(id);
|
|
848
|
+
}
|
|
849
|
+
async #doStoreRetrievable(v) {
|
|
850
|
+
this.#turnRetrievables.add(v);
|
|
851
|
+
await this.#storeRetrievable(this, v);
|
|
852
|
+
this.#hooks.runner("storedRetrievable").run(v);
|
|
853
|
+
}
|
|
854
|
+
async #doMutateRetrievable(v) {
|
|
855
|
+
this.#turnRetrievables.add(v);
|
|
856
|
+
await this.#mutateRetrievable(this, v);
|
|
857
|
+
this.#hooks.runner("mutatedRetrievable").run(v);
|
|
858
|
+
}
|
|
859
|
+
async #doDeleteRetrievable(id) {
|
|
860
|
+
for (const r of this.#turnRetrievables) if (r.id === id) {
|
|
861
|
+
this.#turnRetrievables.delete(r);
|
|
862
|
+
break;
|
|
863
|
+
}
|
|
864
|
+
await this.#deleteRetrievable(this, id);
|
|
865
|
+
this.#hooks.runner("deletedRetrievable").run(id);
|
|
866
|
+
}
|
|
867
|
+
async #doStoreMessage(v) {
|
|
868
|
+
this.#turnMessages.add(v);
|
|
869
|
+
await this.#storeMessage(this, v);
|
|
870
|
+
this.#hooks.runner("storedMessage").run(v);
|
|
871
|
+
}
|
|
872
|
+
async #doMutateMessage(v) {
|
|
873
|
+
this.#turnMessages.add(v);
|
|
874
|
+
await this.#mutateMessage(this, v);
|
|
875
|
+
this.#hooks.runner("mutatedMessage").run(v);
|
|
876
|
+
}
|
|
877
|
+
async #doDeleteMessage(id) {
|
|
878
|
+
for (const m of this.#turnMessages) if (m.id === id) {
|
|
879
|
+
this.#turnMessages.delete(m);
|
|
880
|
+
break;
|
|
881
|
+
}
|
|
882
|
+
await this.#deleteMessage(this, id);
|
|
883
|
+
this.#hooks.runner("deletedMessage").run(id);
|
|
884
|
+
}
|
|
885
|
+
async #doStoreThought(v) {
|
|
886
|
+
this.#turnThoughts.add(v);
|
|
887
|
+
await this.#storeThought(this, v);
|
|
888
|
+
this.#hooks.runner("storedThought").run(v);
|
|
889
|
+
}
|
|
890
|
+
async #doMutateThought(v) {
|
|
891
|
+
this.#turnThoughts.add(v);
|
|
892
|
+
await this.#mutateThought(this, v);
|
|
893
|
+
this.#hooks.runner("mutatedThought").run(v);
|
|
894
|
+
}
|
|
895
|
+
async #doDeleteThought(id) {
|
|
896
|
+
for (const t of this.#turnThoughts) if (t.id === id) {
|
|
897
|
+
this.#turnThoughts.delete(t);
|
|
898
|
+
break;
|
|
899
|
+
}
|
|
900
|
+
await this.#deleteThought(this, id);
|
|
901
|
+
this.#hooks.runner("deletedThought").run(id);
|
|
902
|
+
}
|
|
903
|
+
async #doStoreToolCall(v) {
|
|
904
|
+
this.#turnToolCalls.add(v);
|
|
905
|
+
const prev = this.#toolCallChecksums.get(v.checksum) ?? 0;
|
|
906
|
+
this.#toolCallChecksums.set(v.checksum, prev + 1);
|
|
907
|
+
await this.#storeToolCall(this, v);
|
|
908
|
+
this.#hooks.runner("storedToolCall").run(v);
|
|
909
|
+
}
|
|
910
|
+
async #doMutateToolCall(v) {
|
|
911
|
+
this.#turnToolCalls.add(v);
|
|
912
|
+
await this.#mutateToolCall(this, v);
|
|
913
|
+
this.#hooks.runner("mutatedToolCall").run(v);
|
|
914
|
+
}
|
|
915
|
+
async #doDeleteToolCall(id) {
|
|
916
|
+
for (const tc of this.#turnToolCalls) if (tc.id === id) {
|
|
917
|
+
this.#turnToolCalls.delete(tc);
|
|
918
|
+
const count = this.#toolCallChecksums.get(tc.checksum) ?? 1;
|
|
919
|
+
if (count <= 1) this.#toolCallChecksums.delete(tc.checksum);
|
|
920
|
+
else this.#toolCallChecksums.set(tc.checksum, count - 1);
|
|
921
|
+
break;
|
|
922
|
+
}
|
|
923
|
+
await this.#deleteToolCall(this, id);
|
|
924
|
+
this.#hooks.runner("deletedToolCall").run(id);
|
|
925
|
+
}
|
|
926
|
+
/** @internal Set by {@link @nhtio/adk!DispatchRunner} after construction. */
|
|
927
|
+
_setDispatchId(id) {
|
|
928
|
+
this.#dispatchId = id;
|
|
929
|
+
}
|
|
930
|
+
/** @internal Updated by {@link @nhtio/adk!DispatchRunner} on each iteration. */
|
|
931
|
+
_setIteration(n) {
|
|
932
|
+
this.#iteration = n;
|
|
933
|
+
}
|
|
934
|
+
/** @internal Accessor used by {@link @nhtio/adk!DispatchRunner} to register forwarding handlers. */
|
|
935
|
+
_getHooks() {
|
|
936
|
+
return this.#hooks;
|
|
937
|
+
}
|
|
938
|
+
/**
|
|
939
|
+
* Returns how many times a tool call with the given checksum has been stored in this execution.
|
|
940
|
+
*
|
|
941
|
+
* @remarks
|
|
942
|
+
* Checksums are computed over `tool + args` (see {@link @nhtio/adk!ToolCall.checksum}). This count lets
|
|
943
|
+
* the executor detect repeat invocations of the same call without scanning the full Set.
|
|
944
|
+
* Returns `0` when the checksum has not been seen.
|
|
945
|
+
*
|
|
946
|
+
* @param checksum - The `ToolCall.checksum` value to look up.
|
|
947
|
+
*/
|
|
948
|
+
toolCallCount(checksum) {
|
|
949
|
+
return this.#toolCallChecksums.get(checksum) ?? 0;
|
|
950
|
+
}
|
|
951
|
+
/**
|
|
952
|
+
* Signals successful completion of this execution.
|
|
953
|
+
*
|
|
954
|
+
* @remarks
|
|
955
|
+
* Sets the context's internal signal flag. The {@link @nhtio/adk!DispatchRunner} reads the flag at the
|
|
956
|
+
* end of each iteration to decide whether to loop or exit. Calling `ack()` does NOT abort the
|
|
957
|
+
* current iteration — the current pipeline and flush complete first.
|
|
958
|
+
*
|
|
959
|
+
* @throws {@link @nhtio/adk!E_LLM_EXECUTION_ALREADY_SIGNALLED} when the context has already been signalled
|
|
960
|
+
* (whether via `ack()` or `nack()`).
|
|
961
|
+
*/
|
|
962
|
+
ack() {
|
|
963
|
+
if (this.#signalled !== void 0) throw new require_runtime.E_LLM_EXECUTION_ALREADY_SIGNALLED();
|
|
964
|
+
this.#signalled = "ack";
|
|
965
|
+
for (const handler of this.#ackHandlers) try {
|
|
966
|
+
handler();
|
|
967
|
+
} catch {}
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* Registers a handler to run when this context completes successfully via {@link ack}.
|
|
971
|
+
*
|
|
972
|
+
* @remarks
|
|
973
|
+
* The handler does NOT fire on {@link nack} — failed executor runs should leave any
|
|
974
|
+
* ack-tied subscriptions alone so the consumer can inspect what was registered when
|
|
975
|
+
* debugging the failure. Returns an unsubscribe function; subscriptions are short-lived
|
|
976
|
+
* and die with the context regardless.
|
|
977
|
+
*
|
|
978
|
+
* The canonical consumer is `ToolRegistry.bindContext(ctx)`, which uses this hook to drop
|
|
979
|
+
* ephemeral tools (notably forged artifact-query tools from `SpooledArtifact.forgeTools(ctx)`)
|
|
980
|
+
* at ctx-completion. Consumers may also register custom handlers here for any per-executor
|
|
981
|
+
* cleanup.
|
|
982
|
+
*
|
|
983
|
+
* @param handler - Callback invoked when `ack()` is called.
|
|
984
|
+
* @returns An unsubscribe function that removes the handler.
|
|
985
|
+
*
|
|
986
|
+
* @see {@link @nhtio/adk!ToolRegistry.bindContext}
|
|
987
|
+
* @see {@link @nhtio/adk!SpooledArtifact.forgeTools}
|
|
988
|
+
*/
|
|
989
|
+
onAck(handler) {
|
|
990
|
+
this.#ackHandlers.add(handler);
|
|
991
|
+
return () => {
|
|
992
|
+
this.#ackHandlers.delete(handler);
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Signals failed completion of this execution, optionally with an error.
|
|
997
|
+
*
|
|
998
|
+
* @remarks
|
|
999
|
+
* Sets the context's internal signal flag and stores the error. The {@link @nhtio/adk!DispatchRunner}
|
|
1000
|
+
* reads the flag at the end of each iteration and surfaces the error via the `dispatchEnd`
|
|
1001
|
+
* observability payload and as the rejection reason of `dispatch()`. Calling `nack()` does NOT
|
|
1002
|
+
* abort the current iteration — the current pipeline and flush complete first.
|
|
1003
|
+
*
|
|
1004
|
+
* @param error - Optional error describing the failure. If omitted, a generic Error is used.
|
|
1005
|
+
* @throws {@link @nhtio/adk!E_LLM_EXECUTION_ALREADY_SIGNALLED} when the context has already been signalled.
|
|
1006
|
+
*/
|
|
1007
|
+
nack(error) {
|
|
1008
|
+
if (this.#signalled !== void 0) throw new require_runtime.E_LLM_EXECUTION_ALREADY_SIGNALLED();
|
|
1009
|
+
this.#signalled = "nack";
|
|
1010
|
+
this.#nackError = error ?? /* @__PURE__ */ new Error("LLM execution was nacked without an explicit error.");
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Returns `true` if `value` is a {@link DispatchContext} instance.
|
|
1014
|
+
*
|
|
1015
|
+
* @remarks
|
|
1016
|
+
* Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety. The ADK does not export the
|
|
1017
|
+
* `DispatchContext` class itself as a constructable value — use this guard plus the
|
|
1018
|
+
* {@link DispatchContext} type for runtime detection and TypeScript narrowing.
|
|
1019
|
+
*
|
|
1020
|
+
* @param value - The value to test.
|
|
1021
|
+
* @returns `true` when `value` is a {@link DispatchContext} instance.
|
|
1022
|
+
*/
|
|
1023
|
+
static isDispatchContext(value) {
|
|
1024
|
+
return require_tool_registry.isInstanceOf(value, "DispatchContext", DispatchContext);
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
//#endregion
|
|
1028
|
+
//#region src/lib/dispatch_runner.ts
|
|
1029
|
+
var dispatchInputSchema = _nhtio_validation.validator.object({
|
|
1030
|
+
source: _nhtio_validation.validator.any().custom((value, helpers) => {
|
|
1031
|
+
if (value === void 0) return value;
|
|
1032
|
+
if (require_tool_registry.isInstanceOf(value, "TurnContext", TurnContext)) return value;
|
|
1033
|
+
return helpers.error("any.invalid");
|
|
1034
|
+
}).optional(),
|
|
1035
|
+
raw: _nhtio_validation.validator.object().unknown(true).optional(),
|
|
1036
|
+
executor: _nhtio_validation.validator.function().required(),
|
|
1037
|
+
turnInputPipeline: _nhtio_validation.validator.array().items(_nhtio_validation.validator.function()).default([]),
|
|
1038
|
+
turnOutputPipeline: _nhtio_validation.validator.array().items(_nhtio_validation.validator.function()).default([]),
|
|
1039
|
+
hooks: _nhtio_validation.validator.object().optional(),
|
|
1040
|
+
observers: _nhtio_validation.validator.object().optional()
|
|
1041
|
+
});
|
|
1042
|
+
/**
|
|
1043
|
+
* Orchestrates a single LLM execution dispatch — input pipeline → executor → output pipeline —
|
|
1044
|
+
* looped until middleware/executor signals completion via {@link @nhtio/adk!DispatchContext.ack} /
|
|
1045
|
+
* {@link @nhtio/adk!DispatchContext.nack} or the abort signal fires.
|
|
1046
|
+
*
|
|
1047
|
+
* @remarks
|
|
1048
|
+
* `DispatchRunner` has a private constructor and is invoked via the static `dispatch()`
|
|
1049
|
+
* method. Each dispatch creates a fresh single-use runner that is garbage-collected after the
|
|
1050
|
+
* call completes — matching the `@nhtio/hooks` GC rationale already baked into the context.
|
|
1051
|
+
*
|
|
1052
|
+
* The runner owns the relationship between an {@link @nhtio/adk!DispatchContext} and its parent
|
|
1053
|
+
* {@link @nhtio/adk!TurnContext} (when given a source). It subscribes to the context's mutation hooks,
|
|
1054
|
+
* queues `ContextDelta` entries, and flushes them to the parent's Sets at the end of every
|
|
1055
|
+
* iteration. Emits propagate from the context's hooks → the runner's hooks → (optionally) the
|
|
1056
|
+
* parent `TurnContext`'s emit methods → the `TurnRunner`'s buses.
|
|
1057
|
+
*
|
|
1058
|
+
* Two hook buses, mirroring `TurnRunner`'s pattern:
|
|
1059
|
+
*
|
|
1060
|
+
* - **Functional** (`hooks`): `message`, `thought`, `toolCall` — pipeline-affecting events
|
|
1061
|
+
* - **Observability** (`observers`): `iterationStart`, `iterationEnd`, `dispatchStart`,
|
|
1062
|
+
* `dispatchEnd`, `error`, `toolExecutionStart`, `toolExecutionEnd` — instrumentation only
|
|
1063
|
+
*
|
|
1064
|
+
* The runner has no `maxIterations`, no `maxToolCallChecksumRepeats`. Implementers use the
|
|
1065
|
+
* primitives — `ctx.iteration`, `ctx.toolCallCount(checksum)`, `ctx.ack()`, `ctx.nack()`,
|
|
1066
|
+
* `ctx.abortSignal` — to build any termination bounds they need in their own middleware.
|
|
1067
|
+
*/
|
|
1068
|
+
/**
|
|
1069
|
+
* Module-private token gating direct construction of {@link DispatchRunner}. Callers must use
|
|
1070
|
+
* {@link DispatchRunner.dispatch}; the symbol is not exported, so external code cannot satisfy
|
|
1071
|
+
* the guard at runtime.
|
|
1072
|
+
*/
|
|
1073
|
+
var CONSTRUCT_TOKEN = Symbol("DispatchRunner.construct");
|
|
1074
|
+
var DispatchRunner = class DispatchRunner {
|
|
1075
|
+
#functionalHooks;
|
|
1076
|
+
#observabilityHooks;
|
|
1077
|
+
#inputPipeline;
|
|
1078
|
+
#outputPipeline;
|
|
1079
|
+
#sourceCtx;
|
|
1080
|
+
#deltaQueue;
|
|
1081
|
+
constructor(token, sourceCtx, turnInputPipeline, turnOutputPipeline, hooks, observers) {
|
|
1082
|
+
if (token !== CONSTRUCT_TOKEN) throw new require_runtime.E_INVALID_LLM_DISPATCH_INPUT();
|
|
1083
|
+
this.#sourceCtx = sourceCtx;
|
|
1084
|
+
this.#deltaQueue = [];
|
|
1085
|
+
this.#functionalHooks = new _nhtio_hooks.Hooks();
|
|
1086
|
+
this.#observabilityHooks = new _nhtio_hooks.Hooks();
|
|
1087
|
+
const inputPipeline = new _nhtio_middleware.Middleware();
|
|
1088
|
+
const outputPipeline = new _nhtio_middleware.Middleware();
|
|
1089
|
+
const wrap = (fn) => (ctx, next) => {
|
|
1090
|
+
if (ctx.aborted) return next();
|
|
1091
|
+
return fn(ctx, next);
|
|
1092
|
+
};
|
|
1093
|
+
for (const fn of turnInputPipeline) inputPipeline.add(wrap(fn));
|
|
1094
|
+
for (const fn of turnOutputPipeline) outputPipeline.add(wrap(fn));
|
|
1095
|
+
this.#inputPipeline = inputPipeline;
|
|
1096
|
+
this.#outputPipeline = outputPipeline;
|
|
1097
|
+
if (hooks) for (const key of Object.keys(hooks)) {
|
|
1098
|
+
const entry = hooks[key];
|
|
1099
|
+
if (!entry) continue;
|
|
1100
|
+
const handlers = Array.isArray(entry) ? entry : [entry];
|
|
1101
|
+
for (const h of handlers) this.#functionalHooks.add(key, h);
|
|
1102
|
+
}
|
|
1103
|
+
if (observers) for (const key of Object.keys(observers)) {
|
|
1104
|
+
const entry = observers[key];
|
|
1105
|
+
if (!entry) continue;
|
|
1106
|
+
const handlers = Array.isArray(entry) ? entry : [entry];
|
|
1107
|
+
for (const h of handlers) this.#observabilityHooks.add(key, h);
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
/**
|
|
1111
|
+
* Returns `true` if `value` is a {@link DispatchRunner} instance.
|
|
1112
|
+
*
|
|
1113
|
+
* @remarks
|
|
1114
|
+
* Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety.
|
|
1115
|
+
*
|
|
1116
|
+
* @param value - The value to test.
|
|
1117
|
+
* @returns `true` when `value` is a {@link DispatchRunner} instance.
|
|
1118
|
+
*/
|
|
1119
|
+
static isDispatchRunner(value) {
|
|
1120
|
+
return require_tool_registry.isInstanceOf(value, "DispatchRunner", DispatchRunner);
|
|
1121
|
+
}
|
|
1122
|
+
/**
|
|
1123
|
+
* Dispatches a single LLM execution.
|
|
1124
|
+
*
|
|
1125
|
+
* @remarks
|
|
1126
|
+
* Constructs an {@link @nhtio/adk!DispatchContext} (derived from `source` or from `raw`), runs the
|
|
1127
|
+
* iteration loop, and resolves when middleware/executor signals completion via `ctx.ack()` or
|
|
1128
|
+
* the abort signal fires. Rejects with the nack error when middleware/executor calls
|
|
1129
|
+
* `ctx.nack(err)`. Pipeline and executor errors are wrapped, surfaced on the observability
|
|
1130
|
+
* `error` hook, and re-thrown.
|
|
1131
|
+
*
|
|
1132
|
+
* @param input - The dispatch input. Provide either `source` (derived path) or `raw` (standalone).
|
|
1133
|
+
* @throws {@link @nhtio/adk!E_INVALID_LLM_DISPATCH_INPUT} when the input does not satisfy validation, or
|
|
1134
|
+
* when neither `source` nor `raw` is provided, or when both are provided.
|
|
1135
|
+
*/
|
|
1136
|
+
static async dispatch(input) {
|
|
1137
|
+
let resolved;
|
|
1138
|
+
try {
|
|
1139
|
+
resolved = require_exceptions.validateOrThrow(dispatchInputSchema, input, true);
|
|
1140
|
+
} catch (err) {
|
|
1141
|
+
throw new require_runtime.E_INVALID_LLM_DISPATCH_INPUT({ cause: require_tool_registry.isError(err) ? err : void 0 });
|
|
1142
|
+
}
|
|
1143
|
+
if (!resolved.source && !resolved.raw) throw new require_runtime.E_INVALID_LLM_DISPATCH_INPUT();
|
|
1144
|
+
if (resolved.source && resolved.raw) throw new require_runtime.E_INVALID_LLM_DISPATCH_INPUT();
|
|
1145
|
+
const runner = new DispatchRunner(CONSTRUCT_TOKEN, resolved.source, resolved.turnInputPipeline, resolved.turnOutputPipeline, resolved.hooks, resolved.observers);
|
|
1146
|
+
const llmCtx = runner.#buildContext(resolved.source, resolved.raw);
|
|
1147
|
+
runner.#wireContextHooks(llmCtx);
|
|
1148
|
+
await runner.#runDispatch(llmCtx, resolved.executor);
|
|
1149
|
+
}
|
|
1150
|
+
#buildContext(source, raw) {
|
|
1151
|
+
if (source) {
|
|
1152
|
+
const ac = new AbortController();
|
|
1153
|
+
source.abortSignal.addEventListener("abort", () => ac.abort(), { once: true });
|
|
1154
|
+
return new DispatchContext({
|
|
1155
|
+
turnAbortController: ac,
|
|
1156
|
+
stash: source.stash.all(),
|
|
1157
|
+
systemPrompt: source.systemPrompt,
|
|
1158
|
+
standingInstructions: [...source.standingInstructions],
|
|
1159
|
+
memories: [...source.turnMemories],
|
|
1160
|
+
retrievables: [...source.turnRetrievables],
|
|
1161
|
+
messages: [...source.turnMessages],
|
|
1162
|
+
thoughts: [...source.turnThoughts],
|
|
1163
|
+
toolCalls: [...source.turnToolCalls],
|
|
1164
|
+
tools: source.tools.all(),
|
|
1165
|
+
fetchMemories: () => source.fetchMemories(),
|
|
1166
|
+
fetchRetrievables: () => source.fetchRetrievables(),
|
|
1167
|
+
fetchMessages: () => source.fetchMessages(),
|
|
1168
|
+
fetchThoughts: () => source.fetchThoughts(),
|
|
1169
|
+
fetchToolCalls: () => source.fetchToolCalls(),
|
|
1170
|
+
fetchTools: () => source.fetchTools(),
|
|
1171
|
+
refreshStandingInstructions: () => source.refreshStandingInstructions(),
|
|
1172
|
+
storeStandingInstruction: (_c, v) => source.storeStandingInstruction(v),
|
|
1173
|
+
mutateStandingInstruction: (_c, v) => source.mutateStandingInstruction(v),
|
|
1174
|
+
deleteStandingInstruction: (_c, v) => source.deleteStandingInstruction(v),
|
|
1175
|
+
storeMemory: (_c, v) => source.storeMemory(v),
|
|
1176
|
+
mutateMemory: (_c, v) => source.mutateMemory(v),
|
|
1177
|
+
deleteMemory: (_c, id) => source.deleteMemory(id),
|
|
1178
|
+
storeRetrievable: (_c, v) => source.storeRetrievable(v),
|
|
1179
|
+
mutateRetrievable: (_c, v) => source.mutateRetrievable(v),
|
|
1180
|
+
deleteRetrievable: (_c, id) => source.deleteRetrievable(id),
|
|
1181
|
+
storeMessage: (_c, v) => source.storeMessage(v),
|
|
1182
|
+
mutateMessage: (_c, v) => source.mutateMessage(v),
|
|
1183
|
+
deleteMessage: (_c, id) => source.deleteMessage(id),
|
|
1184
|
+
storeThought: (_c, v) => source.storeThought(v),
|
|
1185
|
+
mutateThought: (_c, v) => source.mutateThought(v),
|
|
1186
|
+
deleteThought: (_c, id) => source.deleteThought(id),
|
|
1187
|
+
storeToolCall: (_c, v) => source.storeToolCall(v),
|
|
1188
|
+
mutateToolCall: (_c, v) => source.mutateToolCall(v),
|
|
1189
|
+
deleteToolCall: (_c, id) => source.deleteToolCall(id),
|
|
1190
|
+
waitFor: source.waitFor
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
1193
|
+
return new DispatchContext(raw);
|
|
1194
|
+
}
|
|
1195
|
+
#wireContextHooks(llmCtx) {
|
|
1196
|
+
const ctxHooks = llmCtx._getHooks();
|
|
1197
|
+
ctxHooks.add("message", (c) => {
|
|
1198
|
+
this.#functionalHooks.runner("message").run(c);
|
|
1199
|
+
if (this.#sourceCtx) this.#sourceCtx.emitMessage(c);
|
|
1200
|
+
});
|
|
1201
|
+
ctxHooks.add("thought", (c) => {
|
|
1202
|
+
this.#functionalHooks.runner("thought").run(c);
|
|
1203
|
+
if (this.#sourceCtx) this.#sourceCtx.emitThought(c);
|
|
1204
|
+
});
|
|
1205
|
+
ctxHooks.add("toolCall", (c) => {
|
|
1206
|
+
this.#functionalHooks.runner("toolCall").run(c);
|
|
1207
|
+
if (this.#sourceCtx) this.#sourceCtx.emitToolCall(c);
|
|
1208
|
+
});
|
|
1209
|
+
ctxHooks.add("toolExecutionStart", (e) => {
|
|
1210
|
+
this.#observabilityHooks.runner("toolExecutionStart").run(e);
|
|
1211
|
+
if (this.#sourceCtx) this.#sourceCtx.emitToolExecutionStart(e);
|
|
1212
|
+
});
|
|
1213
|
+
ctxHooks.add("toolExecutionEnd", (e) => {
|
|
1214
|
+
this.#observabilityHooks.runner("toolExecutionEnd").run(e);
|
|
1215
|
+
if (this.#sourceCtx) this.#sourceCtx.emitToolExecutionEnd(e);
|
|
1216
|
+
});
|
|
1217
|
+
ctxHooks.add("storedStandingInstruction", (v) => {
|
|
1218
|
+
this.#deltaQueue.push({
|
|
1219
|
+
op: "store",
|
|
1220
|
+
type: "standingInstruction",
|
|
1221
|
+
value: v
|
|
1222
|
+
});
|
|
1223
|
+
});
|
|
1224
|
+
ctxHooks.add("mutatedStandingInstruction", (v) => {
|
|
1225
|
+
this.#deltaQueue.push({
|
|
1226
|
+
op: "mutate",
|
|
1227
|
+
type: "standingInstruction",
|
|
1228
|
+
value: v
|
|
1229
|
+
});
|
|
1230
|
+
});
|
|
1231
|
+
ctxHooks.add("deletedStandingInstruction", (v) => {
|
|
1232
|
+
this.#deltaQueue.push({
|
|
1233
|
+
op: "delete",
|
|
1234
|
+
type: "standingInstruction",
|
|
1235
|
+
value: v
|
|
1236
|
+
});
|
|
1237
|
+
});
|
|
1238
|
+
ctxHooks.add("storedMemory", (v) => {
|
|
1239
|
+
this.#deltaQueue.push({
|
|
1240
|
+
op: "store",
|
|
1241
|
+
type: "memory",
|
|
1242
|
+
value: v
|
|
1243
|
+
});
|
|
1244
|
+
});
|
|
1245
|
+
ctxHooks.add("mutatedMemory", (v) => {
|
|
1246
|
+
this.#deltaQueue.push({
|
|
1247
|
+
op: "mutate",
|
|
1248
|
+
type: "memory",
|
|
1249
|
+
value: v
|
|
1250
|
+
});
|
|
1251
|
+
});
|
|
1252
|
+
ctxHooks.add("deletedMemory", (id) => {
|
|
1253
|
+
this.#deltaQueue.push({
|
|
1254
|
+
op: "delete",
|
|
1255
|
+
type: "memory",
|
|
1256
|
+
value: id
|
|
1257
|
+
});
|
|
1258
|
+
});
|
|
1259
|
+
ctxHooks.add("storedRetrievable", (v) => {
|
|
1260
|
+
this.#deltaQueue.push({
|
|
1261
|
+
op: "store",
|
|
1262
|
+
type: "retrievable",
|
|
1263
|
+
value: v
|
|
1264
|
+
});
|
|
1265
|
+
});
|
|
1266
|
+
ctxHooks.add("mutatedRetrievable", (v) => {
|
|
1267
|
+
this.#deltaQueue.push({
|
|
1268
|
+
op: "mutate",
|
|
1269
|
+
type: "retrievable",
|
|
1270
|
+
value: v
|
|
1271
|
+
});
|
|
1272
|
+
});
|
|
1273
|
+
ctxHooks.add("deletedRetrievable", (id) => {
|
|
1274
|
+
this.#deltaQueue.push({
|
|
1275
|
+
op: "delete",
|
|
1276
|
+
type: "retrievable",
|
|
1277
|
+
value: id
|
|
1278
|
+
});
|
|
1279
|
+
});
|
|
1280
|
+
ctxHooks.add("storedMessage", (v) => {
|
|
1281
|
+
this.#deltaQueue.push({
|
|
1282
|
+
op: "store",
|
|
1283
|
+
type: "message",
|
|
1284
|
+
value: v
|
|
1285
|
+
});
|
|
1286
|
+
});
|
|
1287
|
+
ctxHooks.add("mutatedMessage", (v) => {
|
|
1288
|
+
this.#deltaQueue.push({
|
|
1289
|
+
op: "mutate",
|
|
1290
|
+
type: "message",
|
|
1291
|
+
value: v
|
|
1292
|
+
});
|
|
1293
|
+
});
|
|
1294
|
+
ctxHooks.add("deletedMessage", (id) => {
|
|
1295
|
+
this.#deltaQueue.push({
|
|
1296
|
+
op: "delete",
|
|
1297
|
+
type: "message",
|
|
1298
|
+
value: id
|
|
1299
|
+
});
|
|
1300
|
+
});
|
|
1301
|
+
ctxHooks.add("storedThought", (v) => {
|
|
1302
|
+
this.#deltaQueue.push({
|
|
1303
|
+
op: "store",
|
|
1304
|
+
type: "thought",
|
|
1305
|
+
value: v
|
|
1306
|
+
});
|
|
1307
|
+
});
|
|
1308
|
+
ctxHooks.add("mutatedThought", (v) => {
|
|
1309
|
+
this.#deltaQueue.push({
|
|
1310
|
+
op: "mutate",
|
|
1311
|
+
type: "thought",
|
|
1312
|
+
value: v
|
|
1313
|
+
});
|
|
1314
|
+
});
|
|
1315
|
+
ctxHooks.add("deletedThought", (id) => {
|
|
1316
|
+
this.#deltaQueue.push({
|
|
1317
|
+
op: "delete",
|
|
1318
|
+
type: "thought",
|
|
1319
|
+
value: id
|
|
1320
|
+
});
|
|
1321
|
+
});
|
|
1322
|
+
ctxHooks.add("storedToolCall", (v) => {
|
|
1323
|
+
this.#deltaQueue.push({
|
|
1324
|
+
op: "store",
|
|
1325
|
+
type: "toolCall",
|
|
1326
|
+
value: v
|
|
1327
|
+
});
|
|
1328
|
+
});
|
|
1329
|
+
ctxHooks.add("mutatedToolCall", (v) => {
|
|
1330
|
+
this.#deltaQueue.push({
|
|
1331
|
+
op: "mutate",
|
|
1332
|
+
type: "toolCall",
|
|
1333
|
+
value: v
|
|
1334
|
+
});
|
|
1335
|
+
});
|
|
1336
|
+
ctxHooks.add("deletedToolCall", (id) => {
|
|
1337
|
+
this.#deltaQueue.push({
|
|
1338
|
+
op: "delete",
|
|
1339
|
+
type: "toolCall",
|
|
1340
|
+
value: id
|
|
1341
|
+
});
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* Constructs an {@link @nhtio/adk!DispatchExecutorHelpers} instance bound to a single dispatch.
|
|
1346
|
+
*
|
|
1347
|
+
* @remarks
|
|
1348
|
+
* Per-id stream state lives on closure-captured `Map`s, so it persists across iterations of
|
|
1349
|
+
* the dispatch but cannot leak between dispatches (the runner itself is single-use and
|
|
1350
|
+
* garbage-collected after `dispatch()` returns).
|
|
1351
|
+
*/
|
|
1352
|
+
#buildHelpers(ctx) {
|
|
1353
|
+
const observabilityHooks = this.#observabilityHooks;
|
|
1354
|
+
const makeLogEmitter = (level) => (entry) => {
|
|
1355
|
+
const event = {
|
|
1356
|
+
dispatchId: ctx.dispatchId,
|
|
1357
|
+
iteration: ctx.iteration,
|
|
1358
|
+
emittedAt: luxon.DateTime.now(),
|
|
1359
|
+
level,
|
|
1360
|
+
kind: entry.kind,
|
|
1361
|
+
message: entry.message,
|
|
1362
|
+
...entry.payload !== void 0 ? { payload: entry.payload } : {}
|
|
1363
|
+
};
|
|
1364
|
+
observabilityHooks.runner("log").run(event);
|
|
1365
|
+
};
|
|
1366
|
+
const log = {
|
|
1367
|
+
trace: makeLogEmitter("trace"),
|
|
1368
|
+
debug: makeLogEmitter("debug"),
|
|
1369
|
+
info: makeLogEmitter("info"),
|
|
1370
|
+
warn: makeLogEmitter("warn"),
|
|
1371
|
+
error: makeLogEmitter("error")
|
|
1372
|
+
};
|
|
1373
|
+
const messageStreams = /* @__PURE__ */ new Map();
|
|
1374
|
+
const thoughtStreams = /* @__PURE__ */ new Map();
|
|
1375
|
+
const toolCallStreams = /* @__PURE__ */ new Map();
|
|
1376
|
+
const buildStream = (store, id, deltaText, isComplete) => {
|
|
1377
|
+
let entry = store.get(id);
|
|
1378
|
+
const now = luxon.DateTime.now();
|
|
1379
|
+
if (!entry) {
|
|
1380
|
+
entry = {
|
|
1381
|
+
full: "",
|
|
1382
|
+
createdAt: now,
|
|
1383
|
+
isComplete: false
|
|
1384
|
+
};
|
|
1385
|
+
store.set(id, entry);
|
|
1386
|
+
}
|
|
1387
|
+
if (entry.isComplete) throw new Error(`stream "${id}" is already complete; further chunks are not accepted`);
|
|
1388
|
+
entry.full = entry.full + deltaText;
|
|
1389
|
+
entry.isComplete = isComplete;
|
|
1390
|
+
return {
|
|
1391
|
+
id,
|
|
1392
|
+
createdAt: entry.createdAt,
|
|
1393
|
+
updatedAt: now,
|
|
1394
|
+
full: entry.full,
|
|
1395
|
+
aDelta: deltaText,
|
|
1396
|
+
isComplete,
|
|
1397
|
+
...isComplete ? { completedAt: now } : {}
|
|
1398
|
+
};
|
|
1399
|
+
};
|
|
1400
|
+
return {
|
|
1401
|
+
reportMessage: (id, deltaText, opts) => {
|
|
1402
|
+
const isComplete = opts?.isComplete ?? false;
|
|
1403
|
+
ctx.emitMessage(buildStream(messageStreams, id, deltaText, isComplete));
|
|
1404
|
+
},
|
|
1405
|
+
reportThought: (id, deltaText, opts) => {
|
|
1406
|
+
const isComplete = opts?.isComplete ?? false;
|
|
1407
|
+
ctx.emitThought(buildStream(thoughtStreams, id, deltaText, isComplete));
|
|
1408
|
+
},
|
|
1409
|
+
reportToolCall: (id, partial) => {
|
|
1410
|
+
let entry = toolCallStreams.get(id);
|
|
1411
|
+
const now = luxon.DateTime.now();
|
|
1412
|
+
if (!entry) {
|
|
1413
|
+
entry = {
|
|
1414
|
+
isError: false,
|
|
1415
|
+
isComplete: false,
|
|
1416
|
+
createdAt: now
|
|
1417
|
+
};
|
|
1418
|
+
toolCallStreams.set(id, entry);
|
|
1419
|
+
}
|
|
1420
|
+
if (entry.isComplete) throw new Error(`tool call "${id}" is already complete; further updates are not accepted`);
|
|
1421
|
+
if (partial.tool !== void 0) entry.tool = partial.tool;
|
|
1422
|
+
if (partial.args !== void 0) entry.args = partial.args;
|
|
1423
|
+
if (partial.results !== void 0) entry.results = partial.results;
|
|
1424
|
+
if (partial.isError !== void 0) entry.isError = partial.isError;
|
|
1425
|
+
if (partial.isComplete !== void 0) entry.isComplete = partial.isComplete;
|
|
1426
|
+
if (entry.tool !== void 0 && entry.args !== void 0) entry.checksum = (0, js_sha256.sha256)(require_tool_registry.canonicalStringify({
|
|
1427
|
+
tool: entry.tool,
|
|
1428
|
+
args: entry.args
|
|
1429
|
+
}));
|
|
1430
|
+
const content = {
|
|
1431
|
+
id,
|
|
1432
|
+
tool: entry.tool ?? "",
|
|
1433
|
+
args: entry.args,
|
|
1434
|
+
checksum: entry.checksum ?? "",
|
|
1435
|
+
createdAt: entry.createdAt,
|
|
1436
|
+
updatedAt: now,
|
|
1437
|
+
isComplete: entry.isComplete,
|
|
1438
|
+
isError: entry.isError,
|
|
1439
|
+
...entry.results !== void 0 ? { results: entry.results } : {},
|
|
1440
|
+
...entry.isComplete ? { completedAt: now } : {}
|
|
1441
|
+
};
|
|
1442
|
+
ctx.emitToolCall(content);
|
|
1443
|
+
},
|
|
1444
|
+
log
|
|
1445
|
+
};
|
|
1446
|
+
}
|
|
1447
|
+
async #runDispatch(llmCtx, executor) {
|
|
1448
|
+
const dispatchId = (0, uuid.v6)();
|
|
1449
|
+
llmCtx._setDispatchId(dispatchId);
|
|
1450
|
+
const dispatchStartedAt = luxon.DateTime.now();
|
|
1451
|
+
this.#observabilityHooks.runner("dispatchStart").run({
|
|
1452
|
+
dispatchId,
|
|
1453
|
+
startedAt: dispatchStartedAt
|
|
1454
|
+
});
|
|
1455
|
+
const helpers = this.#buildHelpers(llmCtx);
|
|
1456
|
+
let iteration = 0;
|
|
1457
|
+
let dispatchError;
|
|
1458
|
+
try {
|
|
1459
|
+
while (!llmCtx.aborted && !llmCtx.isSignalled) {
|
|
1460
|
+
llmCtx._setIteration(iteration);
|
|
1461
|
+
const iterationStartedAt = luxon.DateTime.now();
|
|
1462
|
+
this.#observabilityHooks.runner("iterationStart").run({
|
|
1463
|
+
dispatchId,
|
|
1464
|
+
iteration,
|
|
1465
|
+
startedAt: iterationStartedAt
|
|
1466
|
+
});
|
|
1467
|
+
await this.#runPipeline(this.#inputPipeline.runner(), llmCtx, "input");
|
|
1468
|
+
if (llmCtx.aborted || llmCtx.isSignalled) {
|
|
1469
|
+
if (llmCtx.isAcked && !llmCtx.aborted) await this.#flush();
|
|
1470
|
+
else this.#deltaQueue.length = 0;
|
|
1471
|
+
break;
|
|
1472
|
+
}
|
|
1473
|
+
try {
|
|
1474
|
+
await executor(llmCtx, helpers);
|
|
1475
|
+
} catch (err) {
|
|
1476
|
+
if (this.#isAbortError(err)) {
|
|
1477
|
+
this.#deltaQueue.length = 0;
|
|
1478
|
+
break;
|
|
1479
|
+
}
|
|
1480
|
+
const wrapped = new require_runtime.E_LLM_EXECUTION_EXECUTOR_ERROR({ cause: require_tool_registry.isError(err) ? err : void 0 });
|
|
1481
|
+
this.#observabilityHooks.runner("error").run(wrapped);
|
|
1482
|
+
this.#deltaQueue.length = 0;
|
|
1483
|
+
throw wrapped;
|
|
1484
|
+
}
|
|
1485
|
+
if (llmCtx.aborted || llmCtx.isSignalled) {
|
|
1486
|
+
if (llmCtx.isAcked && !llmCtx.aborted) await this.#flush();
|
|
1487
|
+
else this.#deltaQueue.length = 0;
|
|
1488
|
+
break;
|
|
1489
|
+
}
|
|
1490
|
+
await this.#runPipeline(this.#outputPipeline.runner(), llmCtx, "output");
|
|
1491
|
+
await this.#flush();
|
|
1492
|
+
const iterationEndedAt = luxon.DateTime.now();
|
|
1493
|
+
this.#observabilityHooks.runner("iterationEnd").run({
|
|
1494
|
+
dispatchId,
|
|
1495
|
+
iteration,
|
|
1496
|
+
startedAt: iterationStartedAt,
|
|
1497
|
+
endedAt: iterationEndedAt,
|
|
1498
|
+
durationMs: iterationEndedAt.diff(iterationStartedAt).milliseconds
|
|
1499
|
+
});
|
|
1500
|
+
iteration++;
|
|
1501
|
+
}
|
|
1502
|
+
} catch (err) {
|
|
1503
|
+
dispatchError = require_tool_registry.isError(err) ? err : new Error(String(err));
|
|
1504
|
+
}
|
|
1505
|
+
const status = dispatchError ? "nack" : llmCtx.nackError ? "nack" : llmCtx.isAcked ? "ack" : "aborted";
|
|
1506
|
+
const finalError = dispatchError ?? llmCtx.nackError;
|
|
1507
|
+
if (!dispatchError && llmCtx.nackError) {
|
|
1508
|
+
const nackErr = llmCtx.nackError;
|
|
1509
|
+
const wrapped = require_tool_registry.isInstanceOf(nackErr, "BaseException") ? nackErr : new require_runtime.E_LLM_EXECUTION_EXECUTOR_ERROR({ cause: require_tool_registry.isError(nackErr) ? nackErr : void 0 });
|
|
1510
|
+
this.#observabilityHooks.runner("error").run(wrapped);
|
|
1511
|
+
}
|
|
1512
|
+
const dispatchEndedAt = luxon.DateTime.now();
|
|
1513
|
+
this.#observabilityHooks.runner("dispatchEnd").run({
|
|
1514
|
+
dispatchId,
|
|
1515
|
+
status,
|
|
1516
|
+
error: finalError,
|
|
1517
|
+
iterations: iteration,
|
|
1518
|
+
startedAt: dispatchStartedAt,
|
|
1519
|
+
endedAt: dispatchEndedAt,
|
|
1520
|
+
durationMs: dispatchEndedAt.diff(dispatchStartedAt).milliseconds
|
|
1521
|
+
});
|
|
1522
|
+
if (finalError) throw finalError;
|
|
1523
|
+
}
|
|
1524
|
+
async #runPipeline(pipeline, llmCtx, label) {
|
|
1525
|
+
let pipelineError;
|
|
1526
|
+
let reached = false;
|
|
1527
|
+
await pipeline.errorHandler(async (error) => {
|
|
1528
|
+
if (this.#isAbortError(error)) return;
|
|
1529
|
+
pipelineError = new require_runtime.E_DISPATCH_PIPELINE_ERROR({ cause: require_tool_registry.isError(error) ? error : void 0 });
|
|
1530
|
+
}).finalHandler(async () => {
|
|
1531
|
+
reached = true;
|
|
1532
|
+
}).run((fn, next) => Promise.resolve(fn(llmCtx, next)));
|
|
1533
|
+
if (pipelineError) {
|
|
1534
|
+
this.#observabilityHooks.runner("error").run(pipelineError);
|
|
1535
|
+
throw pipelineError;
|
|
1536
|
+
}
|
|
1537
|
+
if (!reached && !llmCtx.aborted && !llmCtx.isSignalled) {
|
|
1538
|
+
const shortCircuitError = new require_runtime.E_PIPELINE_SHORT_CIRCUITED([`dispatch-${label}`]);
|
|
1539
|
+
this.#observabilityHooks.runner("error").run(shortCircuitError);
|
|
1540
|
+
throw shortCircuitError;
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
#isAbortError(err) {
|
|
1544
|
+
return require_tool_registry.isError(err) && require_tool_registry.isInstanceOf(err, "AbortError");
|
|
1545
|
+
}
|
|
1546
|
+
async #flush() {
|
|
1547
|
+
if (!this.#sourceCtx) {
|
|
1548
|
+
this.#deltaQueue.length = 0;
|
|
1549
|
+
return;
|
|
1550
|
+
}
|
|
1551
|
+
while (this.#deltaQueue.length > 0) {
|
|
1552
|
+
const delta = this.#deltaQueue.shift();
|
|
1553
|
+
this.#applyDeltaToParent(delta);
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
#applyDeltaToParent(delta) {
|
|
1557
|
+
const ctx = this.#sourceCtx;
|
|
1558
|
+
const { op, type, value } = delta;
|
|
1559
|
+
if (type === "standingInstruction") {
|
|
1560
|
+
const t = value;
|
|
1561
|
+
if (op === "store" || op === "mutate") ctx.standingInstructions.add(t);
|
|
1562
|
+
else ctx.standingInstructions.delete(t);
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
if (type === "memory") {
|
|
1566
|
+
if (op === "store" || op === "mutate") ctx.turnMemories.add(value);
|
|
1567
|
+
else for (const m of ctx.turnMemories) if (m.id === value) {
|
|
1568
|
+
ctx.turnMemories.delete(m);
|
|
1569
|
+
break;
|
|
1570
|
+
}
|
|
1571
|
+
return;
|
|
1572
|
+
}
|
|
1573
|
+
if (type === "retrievable") {
|
|
1574
|
+
if (op === "store" || op === "mutate") ctx.turnRetrievables.add(value);
|
|
1575
|
+
else for (const r of ctx.turnRetrievables) if (r.id === value) {
|
|
1576
|
+
ctx.turnRetrievables.delete(r);
|
|
1577
|
+
break;
|
|
1578
|
+
}
|
|
1579
|
+
return;
|
|
1580
|
+
}
|
|
1581
|
+
if (type === "message") {
|
|
1582
|
+
if (op === "store" || op === "mutate") ctx.turnMessages.add(value);
|
|
1583
|
+
else for (const m of ctx.turnMessages) if (m.id === value) {
|
|
1584
|
+
ctx.turnMessages.delete(m);
|
|
1585
|
+
break;
|
|
1586
|
+
}
|
|
1587
|
+
return;
|
|
1588
|
+
}
|
|
1589
|
+
if (type === "thought") {
|
|
1590
|
+
if (op === "store" || op === "mutate") ctx.turnThoughts.add(value);
|
|
1591
|
+
else for (const t of ctx.turnThoughts) if (t.id === value) {
|
|
1592
|
+
ctx.turnThoughts.delete(t);
|
|
1593
|
+
break;
|
|
1594
|
+
}
|
|
1595
|
+
return;
|
|
1596
|
+
}
|
|
1597
|
+
if (type === "toolCall") {
|
|
1598
|
+
if (op === "store" || op === "mutate") ctx.turnToolCalls.add(value);
|
|
1599
|
+
else for (const tc of ctx.turnToolCalls) if (tc.id === value) {
|
|
1600
|
+
ctx.turnToolCalls.delete(tc);
|
|
1601
|
+
break;
|
|
1602
|
+
}
|
|
1603
|
+
return;
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
};
|
|
1607
|
+
//#endregion
|
|
1608
|
+
Object.defineProperty(exports, "DispatchContext", {
|
|
1609
|
+
enumerable: true,
|
|
1610
|
+
get: function() {
|
|
1611
|
+
return DispatchContext;
|
|
1612
|
+
}
|
|
1613
|
+
});
|
|
1614
|
+
Object.defineProperty(exports, "DispatchRunner", {
|
|
1615
|
+
enumerable: true,
|
|
1616
|
+
get: function() {
|
|
1617
|
+
return DispatchRunner;
|
|
1618
|
+
}
|
|
1619
|
+
});
|
|
1620
|
+
Object.defineProperty(exports, "TurnContext", {
|
|
1621
|
+
enumerable: true,
|
|
1622
|
+
get: function() {
|
|
1623
|
+
return TurnContext;
|
|
1624
|
+
}
|
|
1625
|
+
});
|
|
1626
|
+
|
|
1627
|
+
//# sourceMappingURL=dispatch_runner-CsoH0nld.js.map
|