@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,615 @@
|
|
|
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
|
+
const require_tool = require("./tool-COSeH8I6.js");
|
|
6
|
+
const require_dispatch_runner = require("./dispatch_runner-CsoH0nld.js");
|
|
7
|
+
let _nhtio_validation = require("@nhtio/validation");
|
|
8
|
+
let luxon = require("luxon");
|
|
9
|
+
let _nhtio_middleware = require("@nhtio/middleware");
|
|
10
|
+
let _nhtio_tiny_typed_emitter = require("@nhtio/tiny-typed-emitter");
|
|
11
|
+
//#region src/lib/classes/turn_gate.ts
|
|
12
|
+
/**
|
|
13
|
+
* Validator schema used to validate a {@link RawTurnGate} before constructing a {@link TurnGate}.
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* - `schema` and `abortSignal` are validated as opaque passthrough values.
|
|
17
|
+
* - `timeout` must be a positive integer when provided.
|
|
18
|
+
*/
|
|
19
|
+
var rawTurnGateSchema = _nhtio_validation.validator.object({
|
|
20
|
+
id: _nhtio_validation.validator.string().required(),
|
|
21
|
+
turnId: _nhtio_validation.validator.string().required(),
|
|
22
|
+
reason: _nhtio_validation.validator.string().required(),
|
|
23
|
+
payload: _nhtio_validation.validator.any().required(),
|
|
24
|
+
schema: _nhtio_validation.validator.any().custom((value, helpers) => {
|
|
25
|
+
if (value === void 0 || value === null) return void 0;
|
|
26
|
+
if (typeof value.validate === "function") return value;
|
|
27
|
+
return helpers.error("any.invalid");
|
|
28
|
+
}).optional(),
|
|
29
|
+
timeout: _nhtio_validation.validator.number().integer().min(1).optional(),
|
|
30
|
+
abortSignal: _nhtio_validation.validator.any().custom((value, helpers) => {
|
|
31
|
+
if (value === void 0 || value === null) return void 0;
|
|
32
|
+
if (typeof AbortSignal !== "undefined" && value instanceof AbortSignal) return value;
|
|
33
|
+
if (require_tool_registry.isObject(value) && typeof value.aborted === "boolean" && typeof value.addEventListener === "function") return value;
|
|
34
|
+
return helpers.error("any.invalid");
|
|
35
|
+
}).optional(),
|
|
36
|
+
createdAt: _nhtio_validation.validator.datetime().required()
|
|
37
|
+
});
|
|
38
|
+
/**
|
|
39
|
+
* A cooperative suspension gate that blocks a turn's middleware pipeline until resolved, rejected,
|
|
40
|
+
* aborted, or timed out.
|
|
41
|
+
*
|
|
42
|
+
* @typeParam T - The expected type of the resolution value.
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* Created exclusively via `ctx.waitFor()` — middleware never constructs a gate directly.
|
|
46
|
+
* The gate emits `turnGateOpen` on the runner's observability bus at creation time and
|
|
47
|
+
* `turnGateClosed` when it settles.
|
|
48
|
+
*
|
|
49
|
+
* Resolution is validated against an optional schema before the internal promise is settled.
|
|
50
|
+
* A validation failure throws {@link @nhtio/adk!E_INVALID_TURN_GATE_RESOLUTION} **synchronously in the
|
|
51
|
+
* caller's context** — the promise is NOT settled and the gate remains open.
|
|
52
|
+
*/
|
|
53
|
+
var TurnGate = class TurnGate {
|
|
54
|
+
/**
|
|
55
|
+
* Validator schema that accepts a {@link RawTurnGate} object.
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* Reusable fragment for any schema that needs to validate or nest a gate entry.
|
|
59
|
+
*/
|
|
60
|
+
static schema = rawTurnGateSchema;
|
|
61
|
+
/**
|
|
62
|
+
* Returns `true` if `value` is a {@link TurnGate} instance.
|
|
63
|
+
*
|
|
64
|
+
* @remarks
|
|
65
|
+
* Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety.
|
|
66
|
+
*
|
|
67
|
+
* @param value - The value to test.
|
|
68
|
+
* @returns `true` when `value` is a {@link TurnGate} instance.
|
|
69
|
+
*/
|
|
70
|
+
static isTurnGate(value) {
|
|
71
|
+
return require_tool_registry.isInstanceOf(value, "TurnGate", TurnGate);
|
|
72
|
+
}
|
|
73
|
+
#id;
|
|
74
|
+
#turnId;
|
|
75
|
+
#reason;
|
|
76
|
+
#payload;
|
|
77
|
+
#createdAt;
|
|
78
|
+
#settled;
|
|
79
|
+
#schema;
|
|
80
|
+
#controller;
|
|
81
|
+
#resolve;
|
|
82
|
+
#reject;
|
|
83
|
+
#promise;
|
|
84
|
+
/**
|
|
85
|
+
* @param raw - The raw gate input validated against `rawTurnGateSchema`.
|
|
86
|
+
* @throws {@link @nhtio/adk!E_INVALID_INITIAL_TURN_GATE_VALUE} when `raw` does not satisfy the schema.
|
|
87
|
+
*/
|
|
88
|
+
constructor(raw) {
|
|
89
|
+
let resolved;
|
|
90
|
+
try {
|
|
91
|
+
resolved = require_exceptions.validateOrThrow(rawTurnGateSchema, raw, true);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
throw new require_runtime.E_INVALID_INITIAL_TURN_GATE_VALUE({ cause: require_tool_registry.isError(err) ? err : void 0 });
|
|
94
|
+
}
|
|
95
|
+
this.#id = resolved.id;
|
|
96
|
+
this.#turnId = resolved.turnId;
|
|
97
|
+
this.#reason = resolved.reason;
|
|
98
|
+
this.#payload = resolved.payload;
|
|
99
|
+
this.#createdAt = resolved.createdAt;
|
|
100
|
+
this.#settled = false;
|
|
101
|
+
this.#schema = resolved.schema;
|
|
102
|
+
this.#controller = new AbortController();
|
|
103
|
+
this.#promise = new Promise((resolve, reject) => {
|
|
104
|
+
this.#resolve = resolve;
|
|
105
|
+
this.#reject = reject;
|
|
106
|
+
});
|
|
107
|
+
const onAbort = () => {
|
|
108
|
+
if (!this.#settled) {
|
|
109
|
+
this.#settled = true;
|
|
110
|
+
this.#reject(new require_runtime.E_TURN_GATE_ABORTED());
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
this.#controller.signal.addEventListener("abort", onAbort, { once: true });
|
|
114
|
+
if (resolved.abortSignal) if (resolved.abortSignal.aborted) queueMicrotask(() => onAbort());
|
|
115
|
+
else {
|
|
116
|
+
resolved.abortSignal.addEventListener("abort", onAbort, { once: true });
|
|
117
|
+
this.#promise.then(() => resolved.abortSignal.removeEventListener("abort", onAbort), () => resolved.abortSignal.removeEventListener("abort", onAbort));
|
|
118
|
+
}
|
|
119
|
+
if (resolved.timeout !== void 0) {
|
|
120
|
+
const timer = setTimeout(() => {
|
|
121
|
+
if (!this.#settled) {
|
|
122
|
+
this.#settled = true;
|
|
123
|
+
this.#reject(new require_runtime.E_TURN_GATE_TIMEOUT());
|
|
124
|
+
}
|
|
125
|
+
}, resolved.timeout);
|
|
126
|
+
this.#promise.then(() => clearTimeout(timer), () => clearTimeout(timer));
|
|
127
|
+
}
|
|
128
|
+
Object.defineProperties(this, {
|
|
129
|
+
id: {
|
|
130
|
+
get: () => this.#id,
|
|
131
|
+
enumerable: true,
|
|
132
|
+
configurable: false
|
|
133
|
+
},
|
|
134
|
+
turnId: {
|
|
135
|
+
get: () => this.#turnId,
|
|
136
|
+
enumerable: true,
|
|
137
|
+
configurable: false
|
|
138
|
+
},
|
|
139
|
+
reason: {
|
|
140
|
+
get: () => this.#reason,
|
|
141
|
+
enumerable: true,
|
|
142
|
+
configurable: false
|
|
143
|
+
},
|
|
144
|
+
payload: {
|
|
145
|
+
get: () => this.#payload,
|
|
146
|
+
enumerable: true,
|
|
147
|
+
configurable: false
|
|
148
|
+
},
|
|
149
|
+
createdAt: {
|
|
150
|
+
get: () => this.#createdAt,
|
|
151
|
+
enumerable: true,
|
|
152
|
+
configurable: false
|
|
153
|
+
},
|
|
154
|
+
isSettled: {
|
|
155
|
+
get: () => this.#settled,
|
|
156
|
+
enumerable: true,
|
|
157
|
+
configurable: false
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Resolves the gate with `value`, unblocking the awaiting middleware.
|
|
163
|
+
*
|
|
164
|
+
* @remarks
|
|
165
|
+
* If a schema was provided at construction, `value` is validated synchronously before the
|
|
166
|
+
* promise is settled. A validation failure throws {@link @nhtio/adk!E_INVALID_TURN_GATE_RESOLUTION}
|
|
167
|
+
* in the caller's context — the promise is NOT settled and the gate remains open.
|
|
168
|
+
*
|
|
169
|
+
* No-ops if the gate is already settled.
|
|
170
|
+
*
|
|
171
|
+
* @param value - The resolution value. Must satisfy the gate's schema when one was provided.
|
|
172
|
+
* @throws {@link @nhtio/adk!E_INVALID_TURN_GATE_RESOLUTION} when `value` fails schema validation.
|
|
173
|
+
*/
|
|
174
|
+
resolve(value) {
|
|
175
|
+
if (this.#settled) return;
|
|
176
|
+
if (this.#schema !== void 0) try {
|
|
177
|
+
value = require_exceptions.validateOrThrow(this.#schema, value, true);
|
|
178
|
+
} catch (err) {
|
|
179
|
+
throw new require_runtime.E_INVALID_TURN_GATE_RESOLUTION({ cause: require_tool_registry.isError(err) ? err : void 0 });
|
|
180
|
+
}
|
|
181
|
+
this.#settled = true;
|
|
182
|
+
this.#resolve(value);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Rejects the gate with `error`, unblocking the awaiting middleware with a rejection.
|
|
186
|
+
*
|
|
187
|
+
* @remarks
|
|
188
|
+
* No-ops if the gate is already settled.
|
|
189
|
+
*
|
|
190
|
+
* @param error - The rejection reason.
|
|
191
|
+
*/
|
|
192
|
+
reject(error) {
|
|
193
|
+
if (this.#settled) return;
|
|
194
|
+
this.#settled = true;
|
|
195
|
+
this.#reject(error);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Aborts the gate by firing the internal `AbortController`, which rejects the promise with
|
|
199
|
+
* {@link @nhtio/adk!E_TURN_GATE_ABORTED}.
|
|
200
|
+
*
|
|
201
|
+
* @remarks
|
|
202
|
+
* No-ops if the gate is already settled. Distinct from the turn-level abort signal — this
|
|
203
|
+
* allows callers to cancel a specific gate without aborting the whole turn.
|
|
204
|
+
*/
|
|
205
|
+
abort() {
|
|
206
|
+
if (this.#settled) return;
|
|
207
|
+
this.#controller.abort();
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Returns the internal promise. Called by `ctx.waitFor()` to block the middleware pipeline.
|
|
211
|
+
*
|
|
212
|
+
* @internal
|
|
213
|
+
*/
|
|
214
|
+
_promise() {
|
|
215
|
+
return this.#promise;
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
//#endregion
|
|
219
|
+
//#region src/lib/contracts/turn_runner_config.ts
|
|
220
|
+
/**
|
|
221
|
+
* Validator schema used to validate a {@link TurnRunnerConfig} at {@link @nhtio/adk!TurnRunner} construction time.
|
|
222
|
+
*
|
|
223
|
+
* @remarks
|
|
224
|
+
* Validates that all callbacks are functions of the correct arity, and that `tools` — when
|
|
225
|
+
* provided — is an array of valid {@link @nhtio/adk!Tool} instances. Defaults `tools` to `[]`.
|
|
226
|
+
*
|
|
227
|
+
* Throws {@link @nhtio/adk!E_INVALID_TURN_RUNNER_CONFIG} (via the {@link @nhtio/adk!TurnRunner} constructor) when
|
|
228
|
+
* validation fails.
|
|
229
|
+
*/
|
|
230
|
+
var turnRunnerConfigSchema = _nhtio_validation.validator.object({
|
|
231
|
+
executorCallback: _nhtio_validation.validator.function().required(),
|
|
232
|
+
fetchMemoriesCallback: _nhtio_validation.validator.function().arity(1).required(),
|
|
233
|
+
fetchMessagesCallback: _nhtio_validation.validator.function().arity(1).required(),
|
|
234
|
+
fetchThoughtsCallback: _nhtio_validation.validator.function().arity(1).required(),
|
|
235
|
+
fetchToolCallsCallback: _nhtio_validation.validator.function().arity(1).required(),
|
|
236
|
+
fetchToolsCallback: _nhtio_validation.validator.function().arity(1).required(),
|
|
237
|
+
refreshStandingInstructionsCallback: _nhtio_validation.validator.function().arity(1).required(),
|
|
238
|
+
storeStandingInstructionCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
239
|
+
mutateStandingInstructionCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
240
|
+
deleteStandingInstructionCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
241
|
+
storeMemoryCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
242
|
+
mutateMemoryCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
243
|
+
deleteMemoryCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
244
|
+
fetchRetrievablesCallback: _nhtio_validation.validator.function().arity(1).required(),
|
|
245
|
+
storeRetrievableCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
246
|
+
mutateRetrievableCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
247
|
+
deleteRetrievableCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
248
|
+
storeMessageCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
249
|
+
mutateMessageCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
250
|
+
deleteMessageCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
251
|
+
storeThoughtCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
252
|
+
mutateThoughtCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
253
|
+
deleteThoughtCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
254
|
+
storeToolCallCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
255
|
+
mutateToolCallCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
256
|
+
deleteToolCallCallback: _nhtio_validation.validator.function().arity(2).required(),
|
|
257
|
+
tools: _nhtio_validation.validator.array().items(_nhtio_validation.validator.any().custom((value, helpers) => {
|
|
258
|
+
if (require_tool.Tool.isTool(value)) return value;
|
|
259
|
+
return helpers.error("any.invalid");
|
|
260
|
+
})).default([]),
|
|
261
|
+
turnInputPipeline: _nhtio_validation.validator.array().items(_nhtio_validation.validator.function()).default([]),
|
|
262
|
+
turnOutputPipeline: _nhtio_validation.validator.array().items(_nhtio_validation.validator.function()).default([]),
|
|
263
|
+
dispatchInputPipeline: _nhtio_validation.validator.array().items(_nhtio_validation.validator.function()).default([]),
|
|
264
|
+
dispatchOutputPipeline: _nhtio_validation.validator.array().items(_nhtio_validation.validator.function()).default([])
|
|
265
|
+
});
|
|
266
|
+
//#endregion
|
|
267
|
+
//#region src/lib/turn_runner.ts
|
|
268
|
+
/**
|
|
269
|
+
* Executes a single agent turn through paired input and output middleware pipelines.
|
|
270
|
+
*
|
|
271
|
+
* @remarks
|
|
272
|
+
* Construction validates `config` eagerly and throws {@link @nhtio/adk!E_INVALID_TURN_RUNNER_CONFIG} if it
|
|
273
|
+
* does not satisfy the schema — fail-fast so misconfiguration surfaces before any turn runs.
|
|
274
|
+
*
|
|
275
|
+
* Each call to {@link TurnRunner.run} threads a {@link @nhtio/adk!TurnContext} through the input pipeline,
|
|
276
|
+
* invokes the model, then threads the result through the output pipeline. Middleware on each side
|
|
277
|
+
* can read and mutate the context for pre- and post-processing (e.g. message normalisation, tool
|
|
278
|
+
* call dispatch, response filtering).
|
|
279
|
+
*
|
|
280
|
+
* **Two event buses:**
|
|
281
|
+
* - Functional bus (`on` / `off` / `once`): `message`, `thought`, `toolCall` — pipeline-affecting
|
|
282
|
+
* events that middleware raises throughout turn execution.
|
|
283
|
+
* - Observability bus (`observe` / `unobserve` / `observeOnce`): `turnStart`, `turnEnd`,
|
|
284
|
+
* `turnGateOpen`, `turnGateClosed`, `error` — instrumentation-only events that monitor execution
|
|
285
|
+
* without participating in it.
|
|
286
|
+
*
|
|
287
|
+
* Streaming content is surfaced via `message` and `thought` events; tool call lifecycle via
|
|
288
|
+
* `toolCall`; non-fatal pipeline errors via the observability `error` event; gate lifecycle via
|
|
289
|
+
* `turnGateOpen` and `turnGateClosed` — all throughout execution.
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* ```ts
|
|
293
|
+
* const runner = new TurnRunner({
|
|
294
|
+
* fetchMemoriesCallback: async (ctx) => memoryStore.query(ctx),
|
|
295
|
+
* fetchMessagesCallback: async (ctx) => messageStore.history(ctx),
|
|
296
|
+
* fetchThoughtsCallback: async (ctx) => thoughtStore.history(ctx),
|
|
297
|
+
* fetchToolCallsCallback: async (ctx) => toolCallStore.history(ctx),
|
|
298
|
+
* })
|
|
299
|
+
* // Functional bus — pipeline events
|
|
300
|
+
* runner.on('message', (chunk) => process.stdout.write(chunk.aDelta))
|
|
301
|
+
* // Observability bus — instrumentation
|
|
302
|
+
* runner.observe('error', (err) => console.error(err.toString()))
|
|
303
|
+
* runner.observe('turnStart', ({ turnId }) => console.log('turn started', turnId))
|
|
304
|
+
* runner.observe('turnGateOpen', (gate) => {
|
|
305
|
+
* if (gate.reason === 'tool_approval') {
|
|
306
|
+
* gate.resolve(true) // approve immediately for this example
|
|
307
|
+
* }
|
|
308
|
+
* })
|
|
309
|
+
* await runner.run({
|
|
310
|
+
* turnAbortController: new AbortController(),
|
|
311
|
+
* systemPrompt: 'You are a helpful assistant.',
|
|
312
|
+
* standingInstructions: [],
|
|
313
|
+
* })
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
var TurnRunner = class TurnRunner {
|
|
317
|
+
/**
|
|
318
|
+
* Returns `true` if `value` is a {@link TurnRunner} instance.
|
|
319
|
+
*
|
|
320
|
+
* @remarks
|
|
321
|
+
* Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety.
|
|
322
|
+
*
|
|
323
|
+
* @param value - The value to test.
|
|
324
|
+
* @returns `true` when `value` is a {@link TurnRunner} instance.
|
|
325
|
+
*/
|
|
326
|
+
static isTurnRunner(value) {
|
|
327
|
+
return require_tool_registry.isInstanceOf(value, "TurnRunner", TurnRunner);
|
|
328
|
+
}
|
|
329
|
+
#config;
|
|
330
|
+
#inputRunner;
|
|
331
|
+
#outputRunner;
|
|
332
|
+
#functionalEmitter;
|
|
333
|
+
#observabilityEmitter;
|
|
334
|
+
/**
|
|
335
|
+
* @param config - Construction-time configuration validated against {@link turnRunnerConfigSchema}.
|
|
336
|
+
* @throws {@link @nhtio/adk!E_INVALID_TURN_RUNNER_CONFIG} when `config` does not satisfy the schema.
|
|
337
|
+
*/
|
|
338
|
+
constructor(config) {
|
|
339
|
+
if (!require_exceptions.passesSchema(turnRunnerConfigSchema, config)) throw new require_runtime.E_INVALID_TURN_RUNNER_CONFIG();
|
|
340
|
+
this.#config = require_exceptions.validateOrThrow(turnRunnerConfigSchema, config, true);
|
|
341
|
+
const turnInputPipeline = new _nhtio_middleware.Middleware();
|
|
342
|
+
const turnOutputPipeline = new _nhtio_middleware.Middleware();
|
|
343
|
+
const wrap = (fn) => (ctx, next) => {
|
|
344
|
+
if (ctx.aborted) return next();
|
|
345
|
+
return fn(ctx, next);
|
|
346
|
+
};
|
|
347
|
+
for (const fn of this.#config.turnInputPipeline) turnInputPipeline.add(wrap(fn));
|
|
348
|
+
for (const fn of this.#config.turnOutputPipeline) turnOutputPipeline.add(wrap(fn));
|
|
349
|
+
this.#inputRunner = turnInputPipeline.runner();
|
|
350
|
+
this.#outputRunner = turnOutputPipeline.runner();
|
|
351
|
+
this.#functionalEmitter = new _nhtio_tiny_typed_emitter.TypedEventEmitter();
|
|
352
|
+
this.#observabilityEmitter = new _nhtio_tiny_typed_emitter.TypedEventEmitter();
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Removes a previously registered functional listener for `event`.
|
|
356
|
+
*
|
|
357
|
+
* @param event - The event to stop listening to.
|
|
358
|
+
* @param listener - The listener function to remove.
|
|
359
|
+
* @returns `this` for chaining.
|
|
360
|
+
*/
|
|
361
|
+
off(event, listener) {
|
|
362
|
+
this.#functionalEmitter.off(event, listener);
|
|
363
|
+
return this;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Registers a persistent functional listener for `event`.
|
|
367
|
+
*
|
|
368
|
+
* @param event - The event to listen to.
|
|
369
|
+
* @param listener - The function to call on each emission.
|
|
370
|
+
* @returns `this` for chaining.
|
|
371
|
+
*/
|
|
372
|
+
on(event, listener) {
|
|
373
|
+
this.#functionalEmitter.on(event, listener);
|
|
374
|
+
return this;
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Registers a one-time functional listener for `event` that is automatically removed after the
|
|
378
|
+
* first emission.
|
|
379
|
+
*
|
|
380
|
+
* @param event - The event to listen to.
|
|
381
|
+
* @param listener - The function to call on the next emission.
|
|
382
|
+
* @returns `this` for chaining.
|
|
383
|
+
*/
|
|
384
|
+
once(event, listener) {
|
|
385
|
+
this.#functionalEmitter.once(event, listener);
|
|
386
|
+
return this;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Removes a previously registered observability listener for `event`.
|
|
390
|
+
*
|
|
391
|
+
* @param event - The event to stop observing.
|
|
392
|
+
* @param listener - The listener function to remove.
|
|
393
|
+
* @returns `this` for chaining.
|
|
394
|
+
*/
|
|
395
|
+
unobserve(event, listener) {
|
|
396
|
+
this.#observabilityEmitter.off(event, listener);
|
|
397
|
+
return this;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Registers a persistent observability listener for `event`.
|
|
401
|
+
*
|
|
402
|
+
* @remarks
|
|
403
|
+
* Use the observability bus (`observe` / `unobserve` / `observeOnce`) for instrumentation:
|
|
404
|
+
* turn lifecycle, gate lifecycle, and non-fatal errors. Use the functional bus (`on` / `off` /
|
|
405
|
+
* `once`) for pipeline-affecting events: `message`, `thought`, `toolCall`.
|
|
406
|
+
*
|
|
407
|
+
* @param event - The event to observe.
|
|
408
|
+
* @param listener - The function to call on each emission.
|
|
409
|
+
* @returns `this` for chaining.
|
|
410
|
+
*/
|
|
411
|
+
observe(event, listener) {
|
|
412
|
+
this.#observabilityEmitter.on(event, listener);
|
|
413
|
+
return this;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Registers a one-time observability listener for `event` that is automatically removed after
|
|
417
|
+
* the first emission.
|
|
418
|
+
*
|
|
419
|
+
* @param event - The event to observe once.
|
|
420
|
+
* @param listener - The function to call on the next emission.
|
|
421
|
+
* @returns `this` for chaining.
|
|
422
|
+
*/
|
|
423
|
+
observeOnce(event, listener) {
|
|
424
|
+
this.#observabilityEmitter.once(event, listener);
|
|
425
|
+
return this;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Executes a single agent turn against the provided raw context.
|
|
429
|
+
*
|
|
430
|
+
* @remarks
|
|
431
|
+
* Returns `Promise<void>` intentionally — all meaningful output surfaces via events, not return
|
|
432
|
+
* values. Register listeners before calling `run`: observability events (`turnStart`, `turnEnd`)
|
|
433
|
+
* bracket execution; functional events (`message`, `thought`, `toolCall`) fire throughout;
|
|
434
|
+
* observability `error` carries non-fatal pipeline failures; `turnGateOpen` and `turnGateClosed`
|
|
435
|
+
* fire when middleware suspends via `ctx.waitFor()`. Awaiting this method only tells you the
|
|
436
|
+
* pipeline has finished, not what it produced.
|
|
437
|
+
*
|
|
438
|
+
* Constructs a validated {@link @nhtio/adk!TurnContext} from `context` (throwing
|
|
439
|
+
* {@link @nhtio/adk!E_INVALID_TURN_CONTEXT} on failure), then runs the input middleware pipeline.
|
|
440
|
+
* Abort signals are silently swallowed.
|
|
441
|
+
*
|
|
442
|
+
* @param context - Raw input validated and wrapped into a {@link @nhtio/adk!TurnContext} before execution.
|
|
443
|
+
* @throws {@link @nhtio/adk!E_INVALID_TURN_CONTEXT} when `context` does not satisfy the schema.
|
|
444
|
+
*/
|
|
445
|
+
async run(context) {
|
|
446
|
+
const abortController = context.turnAbortController ?? new AbortController();
|
|
447
|
+
let turnContextId;
|
|
448
|
+
const openGate = (raw) => {
|
|
449
|
+
const gate = new TurnGate({
|
|
450
|
+
...raw,
|
|
451
|
+
turnId: turnContextId,
|
|
452
|
+
abortSignal: abortController.signal
|
|
453
|
+
});
|
|
454
|
+
this.#observabilityEmitter.emit("turnGateOpen", gate);
|
|
455
|
+
const promise = gate._promise();
|
|
456
|
+
promise.then(() => {
|
|
457
|
+
this.#observabilityEmitter.emit("turnGateClosed", {
|
|
458
|
+
gateId: gate.id,
|
|
459
|
+
turnId: gate.turnId,
|
|
460
|
+
result: "resolved",
|
|
461
|
+
settledAt: luxon.DateTime.now()
|
|
462
|
+
});
|
|
463
|
+
}, (err) => {
|
|
464
|
+
let result = "rejected";
|
|
465
|
+
if (require_tool_registry.isInstanceOf(err, "E_TURN_GATE_ABORTED")) result = "aborted";
|
|
466
|
+
else if (require_tool_registry.isInstanceOf(err, "E_TURN_GATE_TIMEOUT")) result = "timeout";
|
|
467
|
+
this.#observabilityEmitter.emit("turnGateClosed", {
|
|
468
|
+
gateId: gate.id,
|
|
469
|
+
turnId: gate.turnId,
|
|
470
|
+
result,
|
|
471
|
+
settledAt: luxon.DateTime.now()
|
|
472
|
+
});
|
|
473
|
+
});
|
|
474
|
+
return promise;
|
|
475
|
+
};
|
|
476
|
+
const tools = new require_tool_registry.ToolRegistry(this.#config.tools);
|
|
477
|
+
const turnContext = new require_dispatch_runner.TurnContext({
|
|
478
|
+
...context,
|
|
479
|
+
turnAbortController: abortController
|
|
480
|
+
}, {
|
|
481
|
+
fetchMemories: this.#config.fetchMemoriesCallback,
|
|
482
|
+
fetchMessages: this.#config.fetchMessagesCallback,
|
|
483
|
+
fetchThoughts: this.#config.fetchThoughtsCallback,
|
|
484
|
+
fetchToolCalls: this.#config.fetchToolCallsCallback,
|
|
485
|
+
fetchTools: this.#config.fetchToolsCallback,
|
|
486
|
+
refreshStandingInstructions: this.#config.refreshStandingInstructionsCallback,
|
|
487
|
+
storeStandingInstruction: this.#config.storeStandingInstructionCallback,
|
|
488
|
+
mutateStandingInstruction: this.#config.mutateStandingInstructionCallback,
|
|
489
|
+
deleteStandingInstruction: this.#config.deleteStandingInstructionCallback,
|
|
490
|
+
storeMemory: this.#config.storeMemoryCallback,
|
|
491
|
+
mutateMemory: this.#config.mutateMemoryCallback,
|
|
492
|
+
deleteMemory: this.#config.deleteMemoryCallback,
|
|
493
|
+
fetchRetrievables: this.#config.fetchRetrievablesCallback,
|
|
494
|
+
storeRetrievable: this.#config.storeRetrievableCallback,
|
|
495
|
+
mutateRetrievable: this.#config.mutateRetrievableCallback,
|
|
496
|
+
deleteRetrievable: this.#config.deleteRetrievableCallback,
|
|
497
|
+
storeMessage: this.#config.storeMessageCallback,
|
|
498
|
+
mutateMessage: this.#config.mutateMessageCallback,
|
|
499
|
+
deleteMessage: this.#config.deleteMessageCallback,
|
|
500
|
+
storeThought: this.#config.storeThoughtCallback,
|
|
501
|
+
mutateThought: this.#config.mutateThoughtCallback,
|
|
502
|
+
deleteThought: this.#config.deleteThoughtCallback,
|
|
503
|
+
storeToolCall: this.#config.storeToolCallCallback,
|
|
504
|
+
mutateToolCall: this.#config.mutateToolCallCallback,
|
|
505
|
+
deleteToolCall: this.#config.deleteToolCallCallback,
|
|
506
|
+
emitMessage: (content) => this.#functionalEmitter.emit("message", content),
|
|
507
|
+
emitThought: (content) => this.#functionalEmitter.emit("thought", content),
|
|
508
|
+
emitToolCall: (content) => this.#functionalEmitter.emit("toolCall", content),
|
|
509
|
+
emitToolExecutionStart: (event) => this.#observabilityEmitter.emit("toolExecutionStart", event),
|
|
510
|
+
emitToolExecutionEnd: (event) => this.#observabilityEmitter.emit("toolExecutionEnd", event),
|
|
511
|
+
openGate,
|
|
512
|
+
tools
|
|
513
|
+
});
|
|
514
|
+
turnContextId = turnContext.id;
|
|
515
|
+
const startedAt = luxon.DateTime.now();
|
|
516
|
+
this.#observabilityEmitter.emit("turnStart", {
|
|
517
|
+
turnId: turnContext.id,
|
|
518
|
+
startedAt
|
|
519
|
+
});
|
|
520
|
+
const emitTurnEnd = () => {
|
|
521
|
+
const endedAt = luxon.DateTime.now();
|
|
522
|
+
this.#observabilityEmitter.emit("turnEnd", {
|
|
523
|
+
turnId: turnContext.id,
|
|
524
|
+
startedAt,
|
|
525
|
+
endedAt,
|
|
526
|
+
durationMs: endedAt.diff(startedAt).milliseconds
|
|
527
|
+
});
|
|
528
|
+
};
|
|
529
|
+
let inputFailed = false;
|
|
530
|
+
let inputReached = false;
|
|
531
|
+
await this.#inputRunner.errorHandler(async (error) => {
|
|
532
|
+
if (!require_tool_registry.isError(error) || !require_tool_registry.isInstanceOf(error, "AbortError")) {
|
|
533
|
+
inputFailed = true;
|
|
534
|
+
const err = new require_runtime.E_INPUT_PIPELINE_ERROR({ cause: require_tool_registry.isError(error) ? error : void 0 });
|
|
535
|
+
this.#observabilityEmitter.emit("error", err);
|
|
536
|
+
}
|
|
537
|
+
}).finalHandler(async () => {
|
|
538
|
+
inputReached = true;
|
|
539
|
+
}).run((fn, next) => Promise.resolve(fn(turnContext, next)));
|
|
540
|
+
if (!inputReached && !inputFailed && !turnContext.aborted) {
|
|
541
|
+
inputFailed = true;
|
|
542
|
+
const err = new require_runtime.E_PIPELINE_SHORT_CIRCUITED(["turn-input"]);
|
|
543
|
+
this.#observabilityEmitter.emit("error", err);
|
|
544
|
+
}
|
|
545
|
+
if (inputFailed || turnContext.aborted) {
|
|
546
|
+
emitTurnEnd();
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
let dispatchFailed = false;
|
|
550
|
+
try {
|
|
551
|
+
await require_dispatch_runner.DispatchRunner.dispatch({
|
|
552
|
+
source: turnContext,
|
|
553
|
+
executor: this.#config.executorCallback,
|
|
554
|
+
turnInputPipeline: this.#config.dispatchInputPipeline,
|
|
555
|
+
turnOutputPipeline: this.#config.dispatchOutputPipeline,
|
|
556
|
+
observers: {
|
|
557
|
+
dispatchStart: [(e) => {
|
|
558
|
+
this.#observabilityEmitter.emit("dispatchStart", e);
|
|
559
|
+
}],
|
|
560
|
+
dispatchEnd: [(e) => {
|
|
561
|
+
this.#observabilityEmitter.emit("dispatchEnd", e);
|
|
562
|
+
}],
|
|
563
|
+
iterationStart: [(e) => {
|
|
564
|
+
this.#observabilityEmitter.emit("iterationStart", e);
|
|
565
|
+
}],
|
|
566
|
+
iterationEnd: [(e) => {
|
|
567
|
+
this.#observabilityEmitter.emit("iterationEnd", e);
|
|
568
|
+
}],
|
|
569
|
+
log: [(e) => {
|
|
570
|
+
this.#observabilityEmitter.emit("log", e);
|
|
571
|
+
}]
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
} catch (err) {
|
|
575
|
+
dispatchFailed = true;
|
|
576
|
+
const wrapped = require_tool_registry.isInstanceOf(err, "BaseException") ? err : err;
|
|
577
|
+
this.#observabilityEmitter.emit("error", wrapped);
|
|
578
|
+
}
|
|
579
|
+
if (dispatchFailed || turnContext.aborted) {
|
|
580
|
+
emitTurnEnd();
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
let outputFailed = false;
|
|
584
|
+
let outputReached = false;
|
|
585
|
+
await this.#outputRunner.errorHandler(async (error) => {
|
|
586
|
+
if (!require_tool_registry.isError(error) || !require_tool_registry.isInstanceOf(error, "AbortError")) {
|
|
587
|
+
outputFailed = true;
|
|
588
|
+
const err = new require_runtime.E_OUTPUT_PIPELINE_ERROR({ cause: require_tool_registry.isError(error) ? error : void 0 });
|
|
589
|
+
this.#observabilityEmitter.emit("error", err);
|
|
590
|
+
}
|
|
591
|
+
}).finalHandler(async () => {
|
|
592
|
+
outputReached = true;
|
|
593
|
+
}).run((fn, next) => Promise.resolve(fn(turnContext, next)));
|
|
594
|
+
if (!outputReached && !outputFailed && !turnContext.aborted) {
|
|
595
|
+
const err = new require_runtime.E_PIPELINE_SHORT_CIRCUITED(["turn-output"]);
|
|
596
|
+
this.#observabilityEmitter.emit("error", err);
|
|
597
|
+
}
|
|
598
|
+
emitTurnEnd();
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
//#endregion
|
|
602
|
+
Object.defineProperty(exports, "TurnGate", {
|
|
603
|
+
enumerable: true,
|
|
604
|
+
get: function() {
|
|
605
|
+
return TurnGate;
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
Object.defineProperty(exports, "TurnRunner", {
|
|
609
|
+
enumerable: true,
|
|
610
|
+
get: function() {
|
|
611
|
+
return TurnRunner;
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
//# sourceMappingURL=turn_runner-CMm2BHdX.js.map
|