@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,544 @@
|
|
|
1
|
+
import { a as validateOrThrow, i as passesSchema, n as ValidationException } from "./exceptions-NrzIHw_R.mjs";
|
|
2
|
+
import { a as Tokenizable, i as TokenEncoding, s as isInstanceOf, t as ToolRegistry } from "./tool_registry-DqLOyGyG.mjs";
|
|
3
|
+
import { C as E_NOT_A_SPOOL_READER, d as E_INVALID_INITIAL_TOOL_VALUE } from "./runtime-CrEPIFgr.mjs";
|
|
4
|
+
import { t as Tool } from "./tool-D2WB1EA1.mjs";
|
|
5
|
+
import { validator } from "@nhtio/validation";
|
|
6
|
+
//#region src/lib/classes/artifact_tool.ts
|
|
7
|
+
/**
|
|
8
|
+
* Validator schema for a {@link RawArtifactTool}.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* Mirrors the base tool schema but explicitly forbids `artifactConstructor` — the entire
|
|
12
|
+
* point of `ArtifactTool` is to opt out of `SpooledArtifact` wrapping.
|
|
13
|
+
*/
|
|
14
|
+
var rawArtifactToolSchema = validator.object({
|
|
15
|
+
name: validator.string().required(),
|
|
16
|
+
description: validator.string().required(),
|
|
17
|
+
inputSchema: validator.any().custom((value, helpers) => {
|
|
18
|
+
if (validator.isSchema(value) && value.type === "object") return value;
|
|
19
|
+
return helpers.error("any.invalid");
|
|
20
|
+
}).required(),
|
|
21
|
+
handler: validator.function().required(),
|
|
22
|
+
meta: validator.object().pattern(validator.string(), validator.any()).default({}),
|
|
23
|
+
ephemeral: validator.boolean().default(false),
|
|
24
|
+
trusted: validator.boolean().default(false),
|
|
25
|
+
onCollision: validator.string().valid("throw", "replace", "keep").default("throw"),
|
|
26
|
+
artifactConstructor: validator.any().forbidden()
|
|
27
|
+
});
|
|
28
|
+
/**
|
|
29
|
+
* A {@link @nhtio/adk!Tool} subclass whose handler return value is wrapped directly in a
|
|
30
|
+
* {@link @nhtio/adk!Tokenizable} (not a {@link @nhtio/adk!SpooledArtifact}) when it
|
|
31
|
+
* lands on `ToolCall.results`.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* `ArtifactTool` is the canonical producer for **forged artifact-query tools** — the tools
|
|
35
|
+
* `SpooledArtifact.forgeTools(ctx)` emits so the model can `head`, `tail`, `grep`, `json_get`,
|
|
36
|
+
* `md_headings` (etc.) an artifact that is already in `ctx.turnToolCalls`.
|
|
37
|
+
*
|
|
38
|
+
* The difference from {@link @nhtio/adk!Tool} is structural, not stylistic:
|
|
39
|
+
*
|
|
40
|
+
* - A normal `Tool`'s handler returns bytes the ADK wraps in a fresh `SpooledArtifact`.
|
|
41
|
+
* The artifact lands in `ToolCall.results`, joins `ctx.turnToolCalls`, and is itself a
|
|
42
|
+
* first-class queryable artifact in the turn.
|
|
43
|
+
* - An `ArtifactTool`'s handler returns a string that is **already the model-visible answer**
|
|
44
|
+
* to a query against an existing artifact. The ADK wraps it in a `Tokenizable` rather
|
|
45
|
+
* than a `SpooledArtifact`; nothing new is queryable on its own. Subsequent
|
|
46
|
+
* `forgeTools(ctx)` calls exclude `ToolCall`s produced by an `ArtifactTool` from the
|
|
47
|
+
* `callId` enum (via the `ToolCall.fromArtifactTool` marker) — this is the structural fix
|
|
48
|
+
* that breaks the otherwise-recursive grep-on-the-grep-result loop.
|
|
49
|
+
*
|
|
50
|
+
* Consumers who want to build their own artifact-query tools (e.g. for a domain-specific
|
|
51
|
+
* spooled subclass not shipped by the ADK) should extend or instantiate this class.
|
|
52
|
+
*/
|
|
53
|
+
var ArtifactTool = class ArtifactTool extends Tool {
|
|
54
|
+
/**
|
|
55
|
+
* Validator schema that accepts a {@link RawArtifactTool} object.
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* Differs from {@link @nhtio/adk!Tool.schema} by forbidding `artifactConstructor` — wrapping is
|
|
59
|
+
* exactly the thing this class opts out of. Typed identically to {@link @nhtio/adk!Tool.schema} so the
|
|
60
|
+
* subclass relationship `class ArtifactTool extends Tool` remains structurally sound; the
|
|
61
|
+
* runtime validation rules still differ as declared by `rawArtifactToolSchema`.
|
|
62
|
+
*/
|
|
63
|
+
static schema = rawArtifactToolSchema;
|
|
64
|
+
/**
|
|
65
|
+
* Returns `true` if `value` is an {@link ArtifactTool} instance.
|
|
66
|
+
*
|
|
67
|
+
* @remarks
|
|
68
|
+
* Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety — `instanceof` would fail for instances
|
|
69
|
+
* created in a different module copy or VM context.
|
|
70
|
+
*
|
|
71
|
+
* @param value - The value to test.
|
|
72
|
+
* @returns `true` when `value` is an {@link ArtifactTool} instance.
|
|
73
|
+
*/
|
|
74
|
+
static isArtifactTool(value) {
|
|
75
|
+
return isInstanceOf(value, "ArtifactTool", ArtifactTool);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* @param raw - Raw tool input validated against {@link ArtifactTool.schema}.
|
|
79
|
+
*
|
|
80
|
+
* @throws {@link @nhtio/adk!E_INVALID_INITIAL_TOOL_VALUE} when `raw` does not satisfy
|
|
81
|
+
* {@link ArtifactTool.schema} (most commonly, when `artifactConstructor` is supplied — it is
|
|
82
|
+
* explicitly forbidden on this class) or when the base {@link @nhtio/adk!Tool} constructor rejects the
|
|
83
|
+
* input for any reason.
|
|
84
|
+
*/
|
|
85
|
+
constructor(raw) {
|
|
86
|
+
try {
|
|
87
|
+
validateOrThrow(rawArtifactToolSchema, raw, true);
|
|
88
|
+
} catch (err) {
|
|
89
|
+
if (isInstanceOf(err, "ValidationException", ValidationException)) throw new E_INVALID_INITIAL_TOOL_VALUE({ cause: err });
|
|
90
|
+
throw err;
|
|
91
|
+
}
|
|
92
|
+
super(raw);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/lib/contracts/spool_reader.ts
|
|
97
|
+
/**
|
|
98
|
+
* Validator schema used to validate a {@link SpoolReader} value.
|
|
99
|
+
*
|
|
100
|
+
* @remarks
|
|
101
|
+
* Because `SpoolReader` is a structural interface with no associated constructor, validation is
|
|
102
|
+
* duck-typed: the value must be an object, class instance, or function with `line`, `byteLength`,
|
|
103
|
+
* and `lineCount` present as callable properties. Arity is not enforced — implementations may add
|
|
104
|
+
* optional parameters beyond the contract.
|
|
105
|
+
*/
|
|
106
|
+
var spoolReaderSchema = validator.any().custom((value, helpers) => {
|
|
107
|
+
if (value !== null && value !== void 0 && typeof value.line === "function" && typeof value.byteLength === "function" && typeof value.lineCount === "function" && typeof value.readAll === "function") return value;
|
|
108
|
+
return helpers.error("any.invalid");
|
|
109
|
+
});
|
|
110
|
+
/**
|
|
111
|
+
* Returns `true` if `value` implements the {@link SpoolReader} interface.
|
|
112
|
+
*
|
|
113
|
+
* @remarks
|
|
114
|
+
* Duck-typed: checks that `value` is non-null with `line`, `byteLength`, `lineCount`, and
|
|
115
|
+
* `readAll` as callable functions. Does not use `instanceof` — there is no `SpoolReader`
|
|
116
|
+
* constructor.
|
|
117
|
+
*
|
|
118
|
+
* @param value - The value to test.
|
|
119
|
+
* @returns `true` when `value` conforms to the {@link SpoolReader} interface.
|
|
120
|
+
*/
|
|
121
|
+
var implementsSpoolReader = (value) => {
|
|
122
|
+
return passesSchema(spoolReaderSchema, value);
|
|
123
|
+
};
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/lib/classes/spooled_artifact.ts
|
|
126
|
+
var noArgsSchema = validator.object({});
|
|
127
|
+
/**
|
|
128
|
+
* Default serialiser for {@link @nhtio/adk!ArtifactTool} handler return values when a descriptor does not
|
|
129
|
+
* provide its own. Exported for reuse by subclass `forgeTools` overrides.
|
|
130
|
+
*
|
|
131
|
+
* @param result - The artifact-method return value.
|
|
132
|
+
* @returns A string suitable for inclusion in an LLM tool-call response.
|
|
133
|
+
*/
|
|
134
|
+
var defaultSerialise = (result) => {
|
|
135
|
+
if (result === void 0) return "(undefined)";
|
|
136
|
+
if (result === null) return "null";
|
|
137
|
+
if (typeof result === "string") return result;
|
|
138
|
+
if (Array.isArray(result) && result.every((r) => typeof r === "string")) {
|
|
139
|
+
if (result.length === 0) return "(empty list)";
|
|
140
|
+
return result.join("\n");
|
|
141
|
+
}
|
|
142
|
+
if (typeof result === "number") return String(result);
|
|
143
|
+
return JSON.stringify(result, null, 2);
|
|
144
|
+
};
|
|
145
|
+
var baseToolMethods = Object.freeze([
|
|
146
|
+
{
|
|
147
|
+
name: "artifact_head",
|
|
148
|
+
method: "head",
|
|
149
|
+
description: "Return the first n lines of a spooled artifact produced earlier in this turn. Takes a callId selecting which artifact to inspect.",
|
|
150
|
+
argsSchema: validator.object({ n: validator.number().integer().min(1).default(10).description("Number of lines to return.") })
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
name: "artifact_tail",
|
|
154
|
+
method: "tail",
|
|
155
|
+
description: "Return the last n lines of a spooled artifact produced earlier in this turn. Takes a callId selecting which artifact to inspect.",
|
|
156
|
+
argsSchema: validator.object({ n: validator.number().integer().min(1).default(10).description("Number of lines to return.") })
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
name: "artifact_grep",
|
|
160
|
+
method: "grep",
|
|
161
|
+
description: "Return all lines matching a regular expression pattern from a spooled artifact produced earlier in this turn.",
|
|
162
|
+
argsSchema: validator.object({
|
|
163
|
+
pattern: validator.string().required().description("Regular expression pattern, applied via JavaScript RegExp."),
|
|
164
|
+
flags: validator.string().pattern(/^[imsu]*$/).optional().description("Optional RegExp flags. Allowed: 'i' (case-insensitive), 'm' (multiline), 's' (dotAll), 'u' (unicode). 'g' and 'y' are disallowed because per-line matching is stateless.")
|
|
165
|
+
})
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
name: "artifact_cat",
|
|
169
|
+
method: "cat",
|
|
170
|
+
description: "Return lines from a spooled artifact produced earlier in this turn, optionally bounded to a range.",
|
|
171
|
+
argsSchema: validator.object({
|
|
172
|
+
start: validator.number().integer().min(0).optional().description("Start line (inclusive)."),
|
|
173
|
+
end: validator.number().integer().min(0).optional().description("End line (exclusive).")
|
|
174
|
+
})
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
name: "artifact_byte_length",
|
|
178
|
+
method: "byteLength",
|
|
179
|
+
description: "Return the total byte length of a spooled artifact produced earlier in this turn.",
|
|
180
|
+
argsSchema: noArgsSchema
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
name: "artifact_line_count",
|
|
184
|
+
method: "lineCount",
|
|
185
|
+
description: "Return the total line count of a spooled artifact produced earlier in this turn.",
|
|
186
|
+
argsSchema: noArgsSchema
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
name: "artifact_estimate_tokens",
|
|
190
|
+
method: "estimateTokens",
|
|
191
|
+
description: "Estimate the total token count of a spooled artifact produced earlier in this turn under a named encoding.",
|
|
192
|
+
argsSchema: validator.object({ encoding: validator.string().valid(...TokenEncoding).required().description("Token encoding identifier.") })
|
|
193
|
+
}
|
|
194
|
+
]);
|
|
195
|
+
/**
|
|
196
|
+
* A lazy, line-oriented view over an arbitrary backing store.
|
|
197
|
+
*
|
|
198
|
+
* @remarks
|
|
199
|
+
* All I/O methods are async to remain compatible with both in-memory and streaming
|
|
200
|
+
* {@link @nhtio/adk!SpoolReader} implementations. Token estimation delegates to
|
|
201
|
+
* {@link @nhtio/adk!Tokenizable.estimateTokens} — the same backends used elsewhere in the ADK.
|
|
202
|
+
*
|
|
203
|
+
* The class is read-only by design: mutation of the underlying data is the responsibility of the
|
|
204
|
+
* producer that created the {@link @nhtio/adk!SpoolReader}, not the consumer reading from this artifact.
|
|
205
|
+
*/
|
|
206
|
+
var SpooledArtifact = class SpooledArtifact {
|
|
207
|
+
/**
|
|
208
|
+
* The set of artifact-query methods this class surfaces via {@link SpooledArtifact.forgeTools}.
|
|
209
|
+
*
|
|
210
|
+
* @remarks
|
|
211
|
+
* The base set covers the generic line-oriented operations every artifact supports:
|
|
212
|
+
* `artifact_head`, `artifact_tail`, `artifact_grep`, `artifact_cat`, `artifact_byte_length`,
|
|
213
|
+
* `artifact_line_count`, `artifact_estimate_tokens`. Each `toolMethods` array lists **only**
|
|
214
|
+
* its own class's descriptors — subclasses do not concatenate inherited descriptors. The
|
|
215
|
+
* subclass instead overrides {@link SpooledArtifact.forgeTools} to merge the base registry
|
|
216
|
+
* (produced by `SpooledArtifact.forgeTools(ctx)`) with its own — see
|
|
217
|
+
* {@link @nhtio/adk!SpooledJsonArtifact.forgeTools} and {@link @nhtio/adk!SpooledMarkdownArtifact.forgeTools} for
|
|
218
|
+
* the canonical shape and the pattern downstream consumers should follow when building
|
|
219
|
+
* their own `SpooledArtifact` subclasses.
|
|
220
|
+
*
|
|
221
|
+
* Tool names are absolute (not subclass-prefixed). Forged tools carry
|
|
222
|
+
* `Tool.onCollision = 'replace'` so merging multiple subclasses' `forgeTools()` outputs is
|
|
223
|
+
* silent — every same-named tool dispatches the same method on whatever artifact the
|
|
224
|
+
* `callId` resolves to, so the overlap is behaviourally interchangeable.
|
|
225
|
+
*
|
|
226
|
+
* Frozen at module load.
|
|
227
|
+
*/
|
|
228
|
+
static toolMethods = baseToolMethods;
|
|
229
|
+
#reader;
|
|
230
|
+
/**
|
|
231
|
+
* @param reader - The backing store to read from.
|
|
232
|
+
* @throws {@link @nhtio/adk!E_NOT_A_SPOOL_READER} when `reader` does not implement {@link @nhtio/adk!SpoolReader}.
|
|
233
|
+
*/
|
|
234
|
+
constructor(reader) {
|
|
235
|
+
if (!implementsSpoolReader(reader)) throw new E_NOT_A_SPOOL_READER();
|
|
236
|
+
this.#reader = reader;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Returns the line at the given 0-based index, or `undefined` when out of range.
|
|
240
|
+
*
|
|
241
|
+
* @remarks
|
|
242
|
+
* Protected so subclasses can scan the backing store line-by-line without allocating
|
|
243
|
+
* intermediate arrays. Delegates directly to the {@link @nhtio/adk!SpoolReader}.
|
|
244
|
+
*
|
|
245
|
+
* @param index - 0-based line index.
|
|
246
|
+
* @returns The raw line string, or `undefined` when out of range.
|
|
247
|
+
*/
|
|
248
|
+
async line(index) {
|
|
249
|
+
return this.#reader.line(index);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Returns `true` if `value` is a {@link SpooledArtifact} instance (including any subclass).
|
|
253
|
+
*
|
|
254
|
+
* @remarks
|
|
255
|
+
* Uses the cross-realm-safe {@link @nhtio/adk!isInstanceOf} guard: `instanceof` first, then
|
|
256
|
+
* `Symbol.hasInstance`, then a `constructor.name` fallback. Subclass instances (e.g.
|
|
257
|
+
* {@link @nhtio/adk!SpooledJsonArtifact}) satisfy this guard because `instanceof` walks the prototype
|
|
258
|
+
* chain. The fallbacks handle the dual-module-copy case where two distinct `SpooledArtifact`
|
|
259
|
+
* classes coexist in the same realm (e.g. one bundled into a downstream library, one in the
|
|
260
|
+
* consumer's `node_modules`).
|
|
261
|
+
*
|
|
262
|
+
* @param value - The value to test.
|
|
263
|
+
* @returns `true` when `value` is a {@link SpooledArtifact} instance.
|
|
264
|
+
*/
|
|
265
|
+
static isSpooledArtifact(value) {
|
|
266
|
+
return isInstanceOf(value, "SpooledArtifact", SpooledArtifact);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Returns `true` if `value` is a constructor function whose prototype chain includes
|
|
270
|
+
* {@link SpooledArtifact} (including `SpooledArtifact` itself).
|
|
271
|
+
*
|
|
272
|
+
* @remarks
|
|
273
|
+
* Used by {@link @nhtio/adk!Tool} to validate the optional `artifactConstructor` field. Performs an
|
|
274
|
+
* `instanceof`-based check on the prototype chain; falls back to a duck-type test that looks
|
|
275
|
+
* for the canonical SpooledArtifact instance methods on `value.prototype` for cross-realm
|
|
276
|
+
* safety (constructors passed from a different module copy or VM context).
|
|
277
|
+
*
|
|
278
|
+
* @param value - The value to test.
|
|
279
|
+
* @returns `true` when `value` is a constructor for `SpooledArtifact` or a subclass.
|
|
280
|
+
*/
|
|
281
|
+
static isSpooledArtifactConstructor(value) {
|
|
282
|
+
if (typeof value !== "function") return false;
|
|
283
|
+
if (value === SpooledArtifact) return true;
|
|
284
|
+
const proto = value.prototype;
|
|
285
|
+
if (proto === void 0 || proto === null) return false;
|
|
286
|
+
if (isInstanceOf(proto, "SpooledArtifact", SpooledArtifact)) return true;
|
|
287
|
+
return [
|
|
288
|
+
"head",
|
|
289
|
+
"tail",
|
|
290
|
+
"grep",
|
|
291
|
+
"cat",
|
|
292
|
+
"byteLength",
|
|
293
|
+
"lineCount",
|
|
294
|
+
"estimateTokens"
|
|
295
|
+
].every((m) => typeof proto[m] === "function");
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Returns the first `n` lines of the artifact.
|
|
299
|
+
*
|
|
300
|
+
* @remarks
|
|
301
|
+
* If the artifact contains fewer than `n` lines, all available lines are returned. Matches the
|
|
302
|
+
* behaviour of POSIX `head -n`.
|
|
303
|
+
*
|
|
304
|
+
* @param n - Number of lines to return. Defaults to 10.
|
|
305
|
+
* @returns Array of line strings, without trailing newlines.
|
|
306
|
+
*/
|
|
307
|
+
async head(n = 10) {
|
|
308
|
+
const count = await this.#reader.lineCount();
|
|
309
|
+
const limit = Math.min(n, count);
|
|
310
|
+
const lines = [];
|
|
311
|
+
for (let i = 0; i < limit; i++) {
|
|
312
|
+
const line = await this.#reader.line(i);
|
|
313
|
+
if (line !== void 0) lines.push(line);
|
|
314
|
+
}
|
|
315
|
+
return lines;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Returns the last `n` lines of the artifact.
|
|
319
|
+
*
|
|
320
|
+
* @remarks
|
|
321
|
+
* If the artifact contains fewer than `n` lines, all available lines are returned. Matches the
|
|
322
|
+
* behaviour of POSIX `tail -n`.
|
|
323
|
+
*
|
|
324
|
+
* @param n - Number of lines to return. Defaults to 10.
|
|
325
|
+
* @returns Array of line strings, without trailing newlines.
|
|
326
|
+
*/
|
|
327
|
+
async tail(n = 10) {
|
|
328
|
+
const count = await this.#reader.lineCount();
|
|
329
|
+
const start = Math.max(0, count - n);
|
|
330
|
+
const lines = [];
|
|
331
|
+
for (let i = start; i < count; i++) {
|
|
332
|
+
const line = await this.#reader.line(i);
|
|
333
|
+
if (line !== void 0) lines.push(line);
|
|
334
|
+
}
|
|
335
|
+
return lines;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Returns all lines that match `pattern`.
|
|
339
|
+
*
|
|
340
|
+
* @remarks
|
|
341
|
+
* Behaves like POSIX `grep`: each line is tested against the pattern and included in the result
|
|
342
|
+
* when it matches. The pattern is applied as a JavaScript `RegExp`; flags (e.g. case-
|
|
343
|
+
* insensitivity) should be encoded in the expression itself.
|
|
344
|
+
*
|
|
345
|
+
* Stateful flags (`g`, `y`) on the supplied `RegExp` would normally cause `pattern.test()` to
|
|
346
|
+
* advance `lastIndex` across calls, producing skipped matches and order-dependent results. To
|
|
347
|
+
* keep the per-line semantics stateless, `grep` resets `pattern.lastIndex` to `0` before each
|
|
348
|
+
* line test. The forged `artifact_grep` tool also rejects `g` and `y` flags up-front at schema
|
|
349
|
+
* validation time.
|
|
350
|
+
*
|
|
351
|
+
* @param pattern - The regular expression to test each line against.
|
|
352
|
+
* @returns Array of matching line strings, in order.
|
|
353
|
+
*/
|
|
354
|
+
async grep(pattern) {
|
|
355
|
+
const count = await this.#reader.lineCount();
|
|
356
|
+
const matches = [];
|
|
357
|
+
for (let i = 0; i < count; i++) {
|
|
358
|
+
const line = await this.#reader.line(i);
|
|
359
|
+
if (line !== void 0) {
|
|
360
|
+
pattern.lastIndex = 0;
|
|
361
|
+
if (pattern.test(line)) matches.push(line);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return matches;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Returns lines from the artifact, optionally bounded to a range.
|
|
368
|
+
*
|
|
369
|
+
* @remarks
|
|
370
|
+
* Without arguments, returns all lines — equivalent to POSIX `cat`. With `start` and/or `end`,
|
|
371
|
+
* behaves like `Array.prototype.slice`: `start` defaults to `0`, `end` defaults to the total
|
|
372
|
+
* line count, and only lines in `[start, end)` are fetched from the backing store. For large
|
|
373
|
+
* artifacts, prefer a bounded range or {@link SpooledArtifact.head} / {@link SpooledArtifact.tail}.
|
|
374
|
+
*
|
|
375
|
+
* @param start - 0-based start line index (inclusive). Defaults to `0`.
|
|
376
|
+
* @param end - 0-based end line index (exclusive). Defaults to `lineCount()`.
|
|
377
|
+
* @returns Array of line strings in the requested range.
|
|
378
|
+
*/
|
|
379
|
+
async cat(start, end) {
|
|
380
|
+
const count = await this.#reader.lineCount();
|
|
381
|
+
const from = Math.max(0, start ?? 0);
|
|
382
|
+
const to = Math.min(count, end ?? count);
|
|
383
|
+
const lines = [];
|
|
384
|
+
for (let i = from; i < to; i++) {
|
|
385
|
+
const line = await this.#reader.line(i);
|
|
386
|
+
if (line !== void 0) lines.push(line);
|
|
387
|
+
}
|
|
388
|
+
return lines;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Returns the total byte length of the underlying data.
|
|
392
|
+
*
|
|
393
|
+
* @returns The byte length as reported by the {@link @nhtio/adk!SpoolReader}.
|
|
394
|
+
*/
|
|
395
|
+
async byteLength() {
|
|
396
|
+
return this.#reader.byteLength();
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Returns the total number of lines in the artifact.
|
|
400
|
+
*
|
|
401
|
+
* @returns The line count as reported by the {@link @nhtio/adk!SpoolReader}.
|
|
402
|
+
*/
|
|
403
|
+
async lineCount() {
|
|
404
|
+
return this.#reader.lineCount();
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Estimates the total token count of the artifact under `encoding`.
|
|
408
|
+
*
|
|
409
|
+
* @remarks
|
|
410
|
+
* Reads the full byte-faithful content via {@link SpooledArtifact.asString} (which delegates to
|
|
411
|
+
* {@link @nhtio/adk!SpoolReader.readAll}) and delegates to {@link @nhtio/adk!Tokenizable.estimateTokens}. The estimate
|
|
412
|
+
* therefore reflects the actual source bytes — including trailing newlines and non-`\n` line
|
|
413
|
+
* terminators that the line-based {@link SpooledArtifact.cat} view would otherwise discard or
|
|
414
|
+
* misrepresent.
|
|
415
|
+
*
|
|
416
|
+
* @param encoding - The encoding identifier to use for counting.
|
|
417
|
+
* @returns The estimated number of tokens.
|
|
418
|
+
*/
|
|
419
|
+
async estimateTokens(encoding) {
|
|
420
|
+
const content = await this.#reader.readAll();
|
|
421
|
+
return Tokenizable.estimateTokens(content, encoding);
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Returns the full artifact body as a single byte-faithful string.
|
|
425
|
+
*
|
|
426
|
+
* @remarks
|
|
427
|
+
* Round-trip faithful to whatever bytes the {@link @nhtio/adk!SpoolReader} was constructed over —
|
|
428
|
+
* preserves trailing newlines and non-`\n` line terminators that {@link SpooledArtifact.cat}
|
|
429
|
+
* discards via its line-based view. This is the canonical primitive for "inline the artifact
|
|
430
|
+
* content directly into a message" use cases.
|
|
431
|
+
*
|
|
432
|
+
* `asString()` and the static `forgeTools(ctx)` factory on each subclass are independent
|
|
433
|
+
* alternatives — a consumer chooses per turn whether to inline the body in a message
|
|
434
|
+
* (`await tc.results.asString()`) or hand the model query tools
|
|
435
|
+
* (`SpooledArtifact.forgeTools(ctx)`). Neither calls the other; either works with neither.
|
|
436
|
+
*
|
|
437
|
+
* @returns The full content as a single string.
|
|
438
|
+
*/
|
|
439
|
+
async asString() {
|
|
440
|
+
return this.#reader.readAll();
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Forges a fresh {@link @nhtio/adk!ToolRegistry} of ephemeral {@link @nhtio/adk!ArtifactTool} instances that let the
|
|
444
|
+
* LLM query artifacts already present in `ctx.turnToolCalls`.
|
|
445
|
+
*
|
|
446
|
+
* @remarks
|
|
447
|
+
* Standard subclass extension pattern — each class owns only its own `toolMethods` and its
|
|
448
|
+
* own `forgeTools`. The base `SpooledArtifact.forgeTools(ctx)` narrows the `callId` enum to
|
|
449
|
+
* any `tc.results instanceof SpooledArtifact` (so subclass instances are included — that's
|
|
450
|
+
* the whole point of inheritance) and dispatches the seven base methods (`head`, `tail`,
|
|
451
|
+
* `grep`, `cat`, `byteLength`, `lineCount`, `estimateTokens`) on the resolved artifact.
|
|
452
|
+
* Subclasses override `forgeTools` to call this static first and then register their own
|
|
453
|
+
* tools on the returned registry — see {@link @nhtio/adk!SpooledJsonArtifact.forgeTools} and
|
|
454
|
+
* {@link @nhtio/adk!SpooledMarkdownArtifact.forgeTools} for the canonical shape. There is no
|
|
455
|
+
* `requiresSubclass` field, no helper indirection, and no `this`-based class narrowing —
|
|
456
|
+
* just plain `instanceof ThisClass` at each subclass's own filter site.
|
|
457
|
+
*
|
|
458
|
+
* For each descriptor in this class's `toolMethods`, the factory:
|
|
459
|
+
*
|
|
460
|
+
* 1. Walks `ctx.turnToolCalls` to find `ToolCall`s whose `results instanceof SpooledArtifact`.
|
|
461
|
+
* `ToolCall`s flagged `fromArtifactTool === true` are excluded — they carry a
|
|
462
|
+
* {@link @nhtio/adk!Tokenizable}, not a `SpooledArtifact`, and including them would let the model
|
|
463
|
+
* `artifact_grep` on a previous `artifact_grep` result (an infinite-recursion hazard with
|
|
464
|
+
* no semantic value).
|
|
465
|
+
* 2. Returns an empty registry if no compatible callIds are found — no point shipping tools
|
|
466
|
+
* whose `callId` enum is empty.
|
|
467
|
+
* 3. Otherwise mints an {@link @nhtio/adk!ArtifactTool} with `ephemeral: true` and `onCollision: 'replace'`
|
|
468
|
+
* so multiple `Subclass.forgeTools(ctx)` outputs merge silently. The tool's `inputSchema`
|
|
469
|
+
* includes a required `callId` field with `.valid(...compatibleIds)`, plus the descriptor's
|
|
470
|
+
* own `argsSchema` fields.
|
|
471
|
+
*
|
|
472
|
+
* The handler resolves the artifact via `[...ctx.turnToolCalls].find(t => t.id === callId)`,
|
|
473
|
+
* dispatches the descriptor's method, and serialises the return value (string → as-is;
|
|
474
|
+
* string[] → newline-join; number → `String(n)`; otherwise `JSON.stringify(value, null, 2)`;
|
|
475
|
+
* `descriptor.serialise` overrides the defaults). `grep` is special-cased: the handler
|
|
476
|
+
* constructs `new RegExp(pattern, flags ?? '')` before invoking the artifact's `grep` method.
|
|
477
|
+
*
|
|
478
|
+
* The returned registry must be merged into the consumer's main registry and the main
|
|
479
|
+
* registry must be bound to `ctx` via {@link @nhtio/adk!ToolRegistry.bindContext}:
|
|
480
|
+
*
|
|
481
|
+
* ```ts
|
|
482
|
+
* const executor: DispatchExecutorFn = async (ctx) => {
|
|
483
|
+
* const forged = SpooledArtifact.forgeTools(ctx)
|
|
484
|
+
* const merged = ToolRegistry.merge([main, forged])
|
|
485
|
+
* main.bindContext(ctx)
|
|
486
|
+
* const result = await llm.invoke({ tools: merged.all(), ... })
|
|
487
|
+
* ctx.ack() // ← ephemeral cleanup fires here
|
|
488
|
+
* }
|
|
489
|
+
* ```
|
|
490
|
+
*
|
|
491
|
+
* @warning You **must** call `registry.bindContext(ctx)` on the registry hosting these tools,
|
|
492
|
+
* or ephemeral cleanup will not run and the `callId` enum in subsequent executor calls will
|
|
493
|
+
* be stale (excluding new tool calls produced in the meantime).
|
|
494
|
+
*
|
|
495
|
+
* @param ctx - The execution context whose `turnToolCalls` snapshot defines the `callId` enum.
|
|
496
|
+
* @returns A fresh `ToolRegistry`. Empty when `turnToolCalls` contains no compatible artifacts.
|
|
497
|
+
*
|
|
498
|
+
* @see {@link @nhtio/adk!ToolRegistry.bindContext}
|
|
499
|
+
* @see {@link @nhtio/adk!ToolRegistry.merge}
|
|
500
|
+
* @see {@link @nhtio/adk!DispatchContext.onAck}
|
|
501
|
+
*/
|
|
502
|
+
static forgeTools(ctx) {
|
|
503
|
+
const requires = SpooledArtifact;
|
|
504
|
+
const compatibleIds = [...ctx.turnToolCalls].filter((tc) => !tc.fromArtifactTool && isInstanceOf(tc.results, requires.name, requires)).map((tc) => tc.id);
|
|
505
|
+
if (compatibleIds.length === 0) return new ToolRegistry([]);
|
|
506
|
+
const tools = [];
|
|
507
|
+
for (const descriptor of this.toolMethods) {
|
|
508
|
+
const callIdSchema = validator.string().valid(...compatibleIds).required().description("ToolCall id of the artifact to query.");
|
|
509
|
+
const argsSchema = (descriptor.argsSchema ?? noArgsSchema).append({ callId: callIdSchema });
|
|
510
|
+
const serialise = descriptor.serialise ?? defaultSerialise;
|
|
511
|
+
const tool = new ArtifactTool({
|
|
512
|
+
name: descriptor.name,
|
|
513
|
+
description: descriptor.description,
|
|
514
|
+
inputSchema: argsSchema,
|
|
515
|
+
ephemeral: true,
|
|
516
|
+
onCollision: "replace",
|
|
517
|
+
handler: async (rawArgs, ctxInner) => {
|
|
518
|
+
const args = rawArgs;
|
|
519
|
+
const tc = [...ctxInner.turnToolCalls].find((t) => t.id === args.callId);
|
|
520
|
+
if (!tc) return `Error: no tool call with id ${args.callId} in this turn`;
|
|
521
|
+
const artifact = tc.results;
|
|
522
|
+
if (!isInstanceOf(artifact, requires.name, requires)) return `Error: tool call ${args.callId} results are not a ${requires.name} instance`;
|
|
523
|
+
const methodArgs = [];
|
|
524
|
+
if (descriptor.method === "grep") {
|
|
525
|
+
const pattern = args.pattern;
|
|
526
|
+
const flags = args.flags ?? "";
|
|
527
|
+
methodArgs.push(new RegExp(pattern, flags));
|
|
528
|
+
} else if (descriptor.method === "head" || descriptor.method === "tail") methodArgs.push(args.n ?? 10);
|
|
529
|
+
else if (descriptor.method === "cat") methodArgs.push(args.start, args.end);
|
|
530
|
+
else if (descriptor.method === "estimateTokens") methodArgs.push(args.encoding);
|
|
531
|
+
const fn = artifact[descriptor.method];
|
|
532
|
+
if (typeof fn !== "function") return `Error: artifact has no method ${descriptor.method}`;
|
|
533
|
+
return serialise(await Promise.resolve(fn.apply(artifact, methodArgs)));
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
tools.push(tool);
|
|
537
|
+
}
|
|
538
|
+
return new ToolRegistry(tools);
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
//#endregion
|
|
542
|
+
export { ArtifactTool as i, defaultSerialise as n, implementsSpoolReader as r, SpooledArtifact as t };
|
|
543
|
+
|
|
544
|
+
//# sourceMappingURL=spooled_artifact-C5ZtGxuJ.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spooled_artifact-C5ZtGxuJ.mjs","names":["#reader"],"sources":["../src/lib/classes/artifact_tool.ts","../src/lib/contracts/spool_reader.ts","../src/lib/classes/spooled_artifact.ts"],"sourcesContent":["import { Tool } from './tool'\nimport { validator } from '@nhtio/validation'\nimport { isInstanceOf } from '../utils/guards'\nimport { E_INVALID_INITIAL_TOOL_VALUE } from '../exceptions/runtime'\nimport { validateOrThrow, ValidationException } from '../utils/validation'\nimport type { Registry } from './registry'\nimport type { Schema } from '@nhtio/validation'\nimport type { Tokenizable } from './tokenizable'\nimport type { DispatchContext } from '../contracts/dispatch_context'\n\n/**\n * The execution function for an {@link ArtifactTool}.\n *\n * @remarks\n * Identical to the base tool handler except the return type is narrowed to\n * `string | Tokenizable | Promise<string | Tokenizable>`. Forged artifact-query tools emit\n * model-visible strings — the ADK wraps a bare-string return into a {@link @nhtio/adk!Tokenizable}\n * at the result-wrapping site so downstream code can rely on\n * `ToolCall.results instanceof Tokenizable` for every `ArtifactTool` invocation.\n */\nexport type ArtifactToolHandler = (\n args: unknown,\n ctx: DispatchContext,\n meta: Registry\n) => string | Tokenizable | Promise<string | Tokenizable>\n\n/**\n * Plain input object supplied to {@link ArtifactTool} at construction time.\n *\n * @remarks\n * Mirrors the base `RawTool` except `artifactConstructor` is forbidden — an `ArtifactTool`\n * emits a {@link @nhtio/adk!Tokenizable} directly into `ToolCall.results` and explicitly opts out of\n * `SpooledArtifact` wrapping. The forbidden field is enforced by {@link ArtifactTool.schema}\n * at construction time.\n */\nexport interface RawArtifactTool {\n /** Unique identifier used in LLM tool definitions. Recommend lowercase snake_case. */\n name: string\n /** Human-readable description passed to the model to explain what the tool does. */\n description: string\n /** @nhtio/validation schema for the tool's input arguments. */\n inputSchema: Schema\n /** Execution function. Returns a string or {@link @nhtio/adk!Tokenizable}; the ADK wraps a bare string into a `Tokenizable` at the result-wrapping site. */\n handler: ArtifactToolHandler\n /** Optional arbitrary metadata for this tool. Defaults to `{}`. */\n meta?: Record<string, unknown>\n /**\n * When `true`, marks this tool as owned by a specific {@link @nhtio/adk!DispatchContext}.\n *\n * @remarks\n * `ArtifactTool` instances produced by `SpooledArtifact.forgeTools(ctx)` set this to `true`\n * so that `ToolRegistry.pruneEphemeral()` drops them at ctx-completion.\n *\n * @defaultValue `false`\n */\n ephemeral?: boolean\n /**\n * When `true`, declares that this tool's output should be treated as **trusted developer/user\n * intent** rather than as untrusted third-party text when surfaced to the model.\n *\n * @remarks\n * Forged artifact-query tools default to `false` because their results are derived from\n * spooled artifact bodies — which may themselves be untrusted upstream tool output. The\n * trust signal does not promote handle-query results above the trust tier of the underlying\n * artifact.\n *\n * @defaultValue `false`\n */\n trusted?: boolean\n /**\n * Self-declared merge collision policy honoured by `ToolRegistry.merge`.\n *\n * @remarks\n * Forged artifact-query tools set this to `'replace'` so that merging multiple\n * `Subclass.forgeTools(ctx)` outputs (whose base-method tools overlap by name) resolves\n * silently — the descriptors, snapshot, and handler behaviour are interchangeable across\n * subclasses, so replacement is a behavioural no-op.\n *\n * @defaultValue `'throw'`\n */\n onCollision?: 'throw' | 'replace' | 'keep'\n}\n\n/**\n * Validator schema for a {@link RawArtifactTool}.\n *\n * @remarks\n * Mirrors the base tool schema but explicitly forbids `artifactConstructor` — the entire\n * point of `ArtifactTool` is to opt out of `SpooledArtifact` wrapping.\n */\nconst rawArtifactToolSchema = validator.object<RawArtifactTool & { artifactConstructor?: never }>({\n name: validator.string().required(),\n description: validator.string().required(),\n inputSchema: validator\n .any()\n .custom((value, helpers) => {\n if (validator.isSchema(value) && (value as any).type === 'object') return value\n return helpers.error('any.invalid')\n })\n .required(),\n handler: validator.function().required(),\n meta: validator.object().pattern(validator.string(), validator.any()).default({}),\n ephemeral: validator.boolean().default(false),\n trusted: validator.boolean().default(false),\n onCollision: validator.string().valid('throw', 'replace', 'keep').default('throw'),\n artifactConstructor: validator.any().forbidden(),\n})\n\n/**\n * A {@link @nhtio/adk!Tool} subclass whose handler return value is wrapped directly in a\n * {@link @nhtio/adk!Tokenizable} (not a {@link @nhtio/adk!SpooledArtifact}) when it\n * lands on `ToolCall.results`.\n *\n * @remarks\n * `ArtifactTool` is the canonical producer for **forged artifact-query tools** — the tools\n * `SpooledArtifact.forgeTools(ctx)` emits so the model can `head`, `tail`, `grep`, `json_get`,\n * `md_headings` (etc.) an artifact that is already in `ctx.turnToolCalls`.\n *\n * The difference from {@link @nhtio/adk!Tool} is structural, not stylistic:\n *\n * - A normal `Tool`'s handler returns bytes the ADK wraps in a fresh `SpooledArtifact`.\n * The artifact lands in `ToolCall.results`, joins `ctx.turnToolCalls`, and is itself a\n * first-class queryable artifact in the turn.\n * - An `ArtifactTool`'s handler returns a string that is **already the model-visible answer**\n * to a query against an existing artifact. The ADK wraps it in a `Tokenizable` rather\n * than a `SpooledArtifact`; nothing new is queryable on its own. Subsequent\n * `forgeTools(ctx)` calls exclude `ToolCall`s produced by an `ArtifactTool` from the\n * `callId` enum (via the `ToolCall.fromArtifactTool` marker) — this is the structural fix\n * that breaks the otherwise-recursive grep-on-the-grep-result loop.\n *\n * Consumers who want to build their own artifact-query tools (e.g. for a domain-specific\n * spooled subclass not shipped by the ADK) should extend or instantiate this class.\n */\nexport class ArtifactTool extends Tool {\n /**\n * Validator schema that accepts a {@link RawArtifactTool} object.\n *\n * @remarks\n * Differs from {@link @nhtio/adk!Tool.schema} by forbidding `artifactConstructor` — wrapping is\n * exactly the thing this class opts out of. Typed identically to {@link @nhtio/adk!Tool.schema} so the\n * subclass relationship `class ArtifactTool extends Tool` remains structurally sound; the\n * runtime validation rules still differ as declared by `rawArtifactToolSchema`.\n */\n public static schema = rawArtifactToolSchema as unknown as typeof Tool.schema\n\n /**\n * Returns `true` if `value` is an {@link ArtifactTool} instance.\n *\n * @remarks\n * Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety — `instanceof` would fail for instances\n * created in a different module copy or VM context.\n *\n * @param value - The value to test.\n * @returns `true` when `value` is an {@link ArtifactTool} instance.\n */\n public static isArtifactTool(value: unknown): value is ArtifactTool {\n return isInstanceOf(value, 'ArtifactTool', ArtifactTool)\n }\n\n /**\n * @param raw - Raw tool input validated against {@link ArtifactTool.schema}.\n *\n * @throws {@link @nhtio/adk!E_INVALID_INITIAL_TOOL_VALUE} when `raw` does not satisfy\n * {@link ArtifactTool.schema} (most commonly, when `artifactConstructor` is supplied — it is\n * explicitly forbidden on this class) or when the base {@link @nhtio/adk!Tool} constructor rejects the\n * input for any reason.\n */\n constructor(raw: RawArtifactTool) {\n // Enforce the forbidden `artifactConstructor` field up-front so the error reports against\n // ArtifactTool's contract, not the base Tool's. The base Tool constructor re-validates\n // against its own schema and stores the resolved fields.\n try {\n validateOrThrow(rawArtifactToolSchema, raw, true)\n } catch (err) {\n if (isInstanceOf(err, 'ValidationException', ValidationException)) {\n throw new E_INVALID_INITIAL_TOOL_VALUE({ cause: err })\n }\n throw err\n }\n super(raw as never)\n }\n}\n","import { validator } from '@nhtio/validation'\nimport { passesSchema } from '../utils/validation'\n\n/**\n * Backing store contract for a {@link @nhtio/adk!SpooledArtifact}.\n *\n * @remarks\n * Implementations may read from memory, a file handle, a network stream, or any other byte\n * source. The interface is intentionally minimal — the artifact layer handles all higher-level\n * operations (`head`, `tail`, `grep`, etc.) by composing calls to these three primitives.\n *\n * Line indexing is 0-based. Implementations must return `undefined` from {@link SpoolReader.line}\n * when the index is out of range rather than throwing.\n *\n * All three methods may be synchronous or asynchronous to accommodate both in-memory and I/O-\n * backed implementations without forcing unnecessary promise overhead on simple cases.\n */\nexport interface SpoolReader {\n /**\n * Returns the line at the given 0-based index, or `undefined` when out of range.\n *\n * @param index - 0-based line index.\n * @returns The raw line string (without trailing newline), or `undefined`.\n */\n line(index: number): string | undefined | Promise<string | undefined>\n\n /**\n * Returns the total number of bytes in the underlying data.\n *\n * @remarks\n * Used for reporting and token-estimation purposes. Byte length is distinct from character\n * length for multi-byte encodings.\n *\n * @returns The byte length of the underlying data.\n */\n byteLength(): number | Promise<number>\n\n /**\n * Returns the total number of lines in the underlying data.\n *\n * @remarks\n * Required so consumers know when to stop iterating; the line count must remain stable for the\n * lifetime of the reader.\n *\n * @returns The total line count.\n */\n lineCount(): number | Promise<number>\n\n /**\n * Returns the full underlying content as a single decoded string, byte-faithful to the source.\n *\n * @remarks\n * Unlike {@link SpoolReader.line}, this method preserves trailing newlines and any non-`\\n`\n * line terminators (e.g. `\\r\\n`) present in the original bytes. It is the primitive that\n * powers `SpooledArtifact.asString()` — the round-trip-faithful alternative to assembling\n * the artifact body from per-line reads.\n *\n * Implementations should make this O(n) in the size of the underlying data and may cache the\n * result if the read source is durable. Streaming implementations may choose not to cache.\n *\n * @returns The full underlying content as a single string.\n */\n readAll(): string | Promise<string>\n}\n\n/**\n * Validator schema used to validate a {@link SpoolReader} value.\n *\n * @remarks\n * Because `SpoolReader` is a structural interface with no associated constructor, validation is\n * duck-typed: the value must be an object, class instance, or function with `line`, `byteLength`,\n * and `lineCount` present as callable properties. Arity is not enforced — implementations may add\n * optional parameters beyond the contract.\n */\nexport const spoolReaderSchema = validator.any().custom((value, helpers) => {\n if (\n value !== null &&\n value !== undefined &&\n typeof (value as any).line === 'function' &&\n typeof (value as any).byteLength === 'function' &&\n typeof (value as any).lineCount === 'function' &&\n typeof (value as any).readAll === 'function'\n ) {\n return value as SpoolReader\n }\n return helpers.error('any.invalid')\n})\n\n/**\n * Returns `true` if `value` implements the {@link SpoolReader} interface.\n *\n * @remarks\n * Duck-typed: checks that `value` is non-null with `line`, `byteLength`, `lineCount`, and\n * `readAll` as callable functions. Does not use `instanceof` — there is no `SpoolReader`\n * constructor.\n *\n * @param value - The value to test.\n * @returns `true` when `value` conforms to the {@link SpoolReader} interface.\n */\nexport const implementsSpoolReader = (value: unknown): value is SpoolReader => {\n return passesSchema(spoolReaderSchema, value)\n}\n","import { validator } from '@nhtio/validation'\nimport { isInstanceOf } from '../utils/guards'\nimport { ArtifactTool } from './artifact_tool'\nimport { ToolRegistry } from './tool_registry'\nimport { Tokenizable, TokenEncoding } from './tokenizable'\nimport { E_NOT_A_SPOOL_READER } from '../exceptions/runtime'\nimport { implementsSpoolReader } from '../contracts/spool_reader'\nimport type { ObjectSchema } from '@nhtio/validation'\nimport type { SpoolReader } from '../contracts/spool_reader'\nimport type { DispatchContext } from '../contracts/dispatch_context'\n\n/**\n * Constructor signature for {@link SpooledArtifact} and any subclass.\n *\n * @remarks\n * Used by {@link @nhtio/adk!Tool} to declare the artifact subclass the consumer should use when wrapping\n * the handler's serialised output. The variadic rest parameter accommodates subclass-specific\n * constructor arguments (e.g. `SpooledJsonArtifact(reader, format?)`).\n *\n * @typeParam A - The {@link SpooledArtifact} subtype the constructor produces.\n */\nexport type SpooledArtifactConstructor<A extends SpooledArtifact = SpooledArtifact> = new (\n reader: SpoolReader,\n ...rest: any[]\n) => A\n\n/**\n * Metadata table entry for one of the artifact's existing query methods, used by\n * {@link SpooledArtifact.forgeTools} to surface that method as an {@link @nhtio/adk!ArtifactTool}.\n *\n * @remarks\n * This is a metadata shape, not a general method → tool pipeline. `forgeTools` knows how to\n * marshal arguments for a fixed, closed set of method names (the base seven on\n * {@link SpooledArtifact} and the JSON/Markdown methods on the bundled subclasses); a\n * descriptor is the place to attach a tool name, description, args schema, and optional\n * serializer to one of those methods. Adding a descriptor for a method whose name is not\n * in that closed set will produce a tool whose handler invokes the method with no arguments.\n *\n * For new methods that require custom argument marshalling, branching, multi-step logic,\n * cross-artifact joins, or any other behaviour beyond \"call this existing method,\" override\n * {@link SpooledArtifact.forgeTools} and mint the {@link @nhtio/adk!ArtifactTool} directly — do not try\n * to express it through a descriptor.\n *\n * Zero-arg methods are the exception: a descriptor with no `argsSchema` (or one that adds\n * only `callId`-adjacent fields you don't consume) works for any method that takes no\n * arguments, regardless of name.\n */\nexport interface ToolMethodDescriptor {\n /** Absolute tool name as exposed to the LLM (e.g. `'artifact_head'`). */\n name: string\n /** Method to invoke on the resolved artifact instance (e.g. `'head'`, `'json_get'`). */\n method: string\n /** Human-readable description passed to the model. Should mention \"in this turn\" so the model understands the artifact's lifecycle scope. */\n description: string\n /** Schema for the method's own args, NOT including `callId`. `forgeTools()` injects `callId`. */\n argsSchema?: ObjectSchema\n /** Optional formatter for non-string return values. Defaults: string → as-is; string[] → newline-join; number → `String(n)`; otherwise `JSON.stringify(value, null, 2)`. */\n serialise?: (result: unknown) => string\n}\n\nconst noArgsSchema = validator.object<Record<string, never>>({})\n\n/**\n * Default serialiser for {@link @nhtio/adk!ArtifactTool} handler return values when a descriptor does not\n * provide its own. Exported for reuse by subclass `forgeTools` overrides.\n *\n * @param result - The artifact-method return value.\n * @returns A string suitable for inclusion in an LLM tool-call response.\n */\nexport const defaultSerialise = (result: unknown): string => {\n if (result === undefined) return '(undefined)'\n if (result === null) return 'null'\n if (typeof result === 'string') return result\n if (Array.isArray(result) && result.every((r) => typeof r === 'string')) {\n if (result.length === 0) return '(empty list)'\n return (result as string[]).join('\\n')\n }\n if (typeof result === 'number') return String(result)\n return JSON.stringify(result, null, 2)\n}\n\nconst baseToolMethods: ReadonlyArray<ToolMethodDescriptor> = Object.freeze([\n {\n name: 'artifact_head',\n method: 'head',\n description:\n 'Return the first n lines of a spooled artifact produced earlier in this turn. Takes a callId selecting which artifact to inspect.',\n argsSchema: validator.object({\n n: validator.number().integer().min(1).default(10).description('Number of lines to return.'),\n }),\n },\n {\n name: 'artifact_tail',\n method: 'tail',\n description:\n 'Return the last n lines of a spooled artifact produced earlier in this turn. Takes a callId selecting which artifact to inspect.',\n argsSchema: validator.object({\n n: validator.number().integer().min(1).default(10).description('Number of lines to return.'),\n }),\n },\n {\n name: 'artifact_grep',\n method: 'grep',\n description:\n 'Return all lines matching a regular expression pattern from a spooled artifact produced earlier in this turn.',\n argsSchema: validator.object({\n pattern: validator\n .string()\n .required()\n .description('Regular expression pattern, applied via JavaScript RegExp.'),\n flags: validator\n .string()\n .pattern(/^[imsu]*$/)\n .optional()\n .description(\n \"Optional RegExp flags. Allowed: 'i' (case-insensitive), 'm' (multiline), 's' (dotAll), 'u' (unicode). 'g' and 'y' are disallowed because per-line matching is stateless.\"\n ),\n }),\n },\n {\n name: 'artifact_cat',\n method: 'cat',\n description:\n 'Return lines from a spooled artifact produced earlier in this turn, optionally bounded to a range.',\n argsSchema: validator.object({\n start: validator.number().integer().min(0).optional().description('Start line (inclusive).'),\n end: validator.number().integer().min(0).optional().description('End line (exclusive).'),\n }),\n },\n {\n name: 'artifact_byte_length',\n method: 'byteLength',\n description:\n 'Return the total byte length of a spooled artifact produced earlier in this turn.',\n argsSchema: noArgsSchema,\n },\n {\n name: 'artifact_line_count',\n method: 'lineCount',\n description: 'Return the total line count of a spooled artifact produced earlier in this turn.',\n argsSchema: noArgsSchema,\n },\n {\n name: 'artifact_estimate_tokens',\n method: 'estimateTokens',\n description:\n 'Estimate the total token count of a spooled artifact produced earlier in this turn under a named encoding.',\n argsSchema: validator.object({\n encoding: validator\n .string()\n .valid(...TokenEncoding)\n .required()\n .description('Token encoding identifier.'),\n }),\n },\n])\n\n/**\n * A lazy, line-oriented view over an arbitrary backing store.\n *\n * @remarks\n * All I/O methods are async to remain compatible with both in-memory and streaming\n * {@link @nhtio/adk!SpoolReader} implementations. Token estimation delegates to\n * {@link @nhtio/adk!Tokenizable.estimateTokens} — the same backends used elsewhere in the ADK.\n *\n * The class is read-only by design: mutation of the underlying data is the responsibility of the\n * producer that created the {@link @nhtio/adk!SpoolReader}, not the consumer reading from this artifact.\n */\nexport class SpooledArtifact {\n /**\n * The set of artifact-query methods this class surfaces via {@link SpooledArtifact.forgeTools}.\n *\n * @remarks\n * The base set covers the generic line-oriented operations every artifact supports:\n * `artifact_head`, `artifact_tail`, `artifact_grep`, `artifact_cat`, `artifact_byte_length`,\n * `artifact_line_count`, `artifact_estimate_tokens`. Each `toolMethods` array lists **only**\n * its own class's descriptors — subclasses do not concatenate inherited descriptors. The\n * subclass instead overrides {@link SpooledArtifact.forgeTools} to merge the base registry\n * (produced by `SpooledArtifact.forgeTools(ctx)`) with its own — see\n * {@link @nhtio/adk!SpooledJsonArtifact.forgeTools} and {@link @nhtio/adk!SpooledMarkdownArtifact.forgeTools} for\n * the canonical shape and the pattern downstream consumers should follow when building\n * their own `SpooledArtifact` subclasses.\n *\n * Tool names are absolute (not subclass-prefixed). Forged tools carry\n * `Tool.onCollision = 'replace'` so merging multiple subclasses' `forgeTools()` outputs is\n * silent — every same-named tool dispatches the same method on whatever artifact the\n * `callId` resolves to, so the overlap is behaviourally interchangeable.\n *\n * Frozen at module load.\n */\n public static toolMethods: ReadonlyArray<ToolMethodDescriptor> = baseToolMethods\n\n #reader: SpoolReader\n\n /**\n * @param reader - The backing store to read from.\n * @throws {@link @nhtio/adk!E_NOT_A_SPOOL_READER} when `reader` does not implement {@link @nhtio/adk!SpoolReader}.\n */\n constructor(reader: SpoolReader) {\n if (!implementsSpoolReader(reader)) {\n throw new E_NOT_A_SPOOL_READER()\n }\n this.#reader = reader\n }\n\n /**\n * Returns the line at the given 0-based index, or `undefined` when out of range.\n *\n * @remarks\n * Protected so subclasses can scan the backing store line-by-line without allocating\n * intermediate arrays. Delegates directly to the {@link @nhtio/adk!SpoolReader}.\n *\n * @param index - 0-based line index.\n * @returns The raw line string, or `undefined` when out of range.\n */\n protected async line(index: number): Promise<string | undefined> {\n return this.#reader.line(index)\n }\n\n /**\n * Returns `true` if `value` is a {@link SpooledArtifact} instance (including any subclass).\n *\n * @remarks\n * Uses the cross-realm-safe {@link @nhtio/adk!isInstanceOf} guard: `instanceof` first, then\n * `Symbol.hasInstance`, then a `constructor.name` fallback. Subclass instances (e.g.\n * {@link @nhtio/adk!SpooledJsonArtifact}) satisfy this guard because `instanceof` walks the prototype\n * chain. The fallbacks handle the dual-module-copy case where two distinct `SpooledArtifact`\n * classes coexist in the same realm (e.g. one bundled into a downstream library, one in the\n * consumer's `node_modules`).\n *\n * @param value - The value to test.\n * @returns `true` when `value` is a {@link SpooledArtifact} instance.\n */\n public static isSpooledArtifact(value: unknown): value is SpooledArtifact {\n return isInstanceOf(value, 'SpooledArtifact', SpooledArtifact)\n }\n\n /**\n * Returns `true` if `value` is a constructor function whose prototype chain includes\n * {@link SpooledArtifact} (including `SpooledArtifact` itself).\n *\n * @remarks\n * Used by {@link @nhtio/adk!Tool} to validate the optional `artifactConstructor` field. Performs an\n * `instanceof`-based check on the prototype chain; falls back to a duck-type test that looks\n * for the canonical SpooledArtifact instance methods on `value.prototype` for cross-realm\n * safety (constructors passed from a different module copy or VM context).\n *\n * @param value - The value to test.\n * @returns `true` when `value` is a constructor for `SpooledArtifact` or a subclass.\n */\n public static isSpooledArtifactConstructor(\n value: unknown\n ): value is SpooledArtifactConstructor<SpooledArtifact> {\n if (typeof value !== 'function') return false\n if (value === SpooledArtifact) return true\n const proto = (value as { prototype?: unknown }).prototype\n if (proto === undefined || proto === null) return false\n if (isInstanceOf(proto, 'SpooledArtifact', SpooledArtifact)) return true\n // Cross-realm duck-type fallback: prototype carries the canonical SpooledArtifact methods\n const methods = ['head', 'tail', 'grep', 'cat', 'byteLength', 'lineCount', 'estimateTokens']\n return methods.every((m) => typeof (proto as Record<string, unknown>)[m] === 'function')\n }\n\n /**\n * Returns the first `n` lines of the artifact.\n *\n * @remarks\n * If the artifact contains fewer than `n` lines, all available lines are returned. Matches the\n * behaviour of POSIX `head -n`.\n *\n * @param n - Number of lines to return. Defaults to 10.\n * @returns Array of line strings, without trailing newlines.\n */\n async head(n: number = 10): Promise<string[]> {\n const count = await this.#reader.lineCount()\n const limit = Math.min(n, count)\n const lines: string[] = []\n for (let i = 0; i < limit; i++) {\n const line = await this.#reader.line(i)\n if (line !== undefined) {\n lines.push(line)\n }\n }\n return lines\n }\n\n /**\n * Returns the last `n` lines of the artifact.\n *\n * @remarks\n * If the artifact contains fewer than `n` lines, all available lines are returned. Matches the\n * behaviour of POSIX `tail -n`.\n *\n * @param n - Number of lines to return. Defaults to 10.\n * @returns Array of line strings, without trailing newlines.\n */\n async tail(n: number = 10): Promise<string[]> {\n const count = await this.#reader.lineCount()\n const start = Math.max(0, count - n)\n const lines: string[] = []\n for (let i = start; i < count; i++) {\n const line = await this.#reader.line(i)\n if (line !== undefined) {\n lines.push(line)\n }\n }\n return lines\n }\n\n /**\n * Returns all lines that match `pattern`.\n *\n * @remarks\n * Behaves like POSIX `grep`: each line is tested against the pattern and included in the result\n * when it matches. The pattern is applied as a JavaScript `RegExp`; flags (e.g. case-\n * insensitivity) should be encoded in the expression itself.\n *\n * Stateful flags (`g`, `y`) on the supplied `RegExp` would normally cause `pattern.test()` to\n * advance `lastIndex` across calls, producing skipped matches and order-dependent results. To\n * keep the per-line semantics stateless, `grep` resets `pattern.lastIndex` to `0` before each\n * line test. The forged `artifact_grep` tool also rejects `g` and `y` flags up-front at schema\n * validation time.\n *\n * @param pattern - The regular expression to test each line against.\n * @returns Array of matching line strings, in order.\n */\n async grep(pattern: RegExp): Promise<string[]> {\n const count = await this.#reader.lineCount()\n const matches: string[] = []\n for (let i = 0; i < count; i++) {\n const line = await this.#reader.line(i)\n if (line !== undefined) {\n pattern.lastIndex = 0\n if (pattern.test(line)) {\n matches.push(line)\n }\n }\n }\n return matches\n }\n\n /**\n * Returns lines from the artifact, optionally bounded to a range.\n *\n * @remarks\n * Without arguments, returns all lines — equivalent to POSIX `cat`. With `start` and/or `end`,\n * behaves like `Array.prototype.slice`: `start` defaults to `0`, `end` defaults to the total\n * line count, and only lines in `[start, end)` are fetched from the backing store. For large\n * artifacts, prefer a bounded range or {@link SpooledArtifact.head} / {@link SpooledArtifact.tail}.\n *\n * @param start - 0-based start line index (inclusive). Defaults to `0`.\n * @param end - 0-based end line index (exclusive). Defaults to `lineCount()`.\n * @returns Array of line strings in the requested range.\n */\n async cat(start?: number, end?: number): Promise<string[]> {\n const count = await this.#reader.lineCount()\n const from = Math.max(0, start ?? 0)\n const to = Math.min(count, end ?? count)\n const lines: string[] = []\n for (let i = from; i < to; i++) {\n const line = await this.#reader.line(i)\n if (line !== undefined) {\n lines.push(line)\n }\n }\n return lines\n }\n\n /**\n * Returns the total byte length of the underlying data.\n *\n * @returns The byte length as reported by the {@link @nhtio/adk!SpoolReader}.\n */\n async byteLength(): Promise<number> {\n return this.#reader.byteLength()\n }\n\n /**\n * Returns the total number of lines in the artifact.\n *\n * @returns The line count as reported by the {@link @nhtio/adk!SpoolReader}.\n */\n async lineCount(): Promise<number> {\n return this.#reader.lineCount()\n }\n\n /**\n * Estimates the total token count of the artifact under `encoding`.\n *\n * @remarks\n * Reads the full byte-faithful content via {@link SpooledArtifact.asString} (which delegates to\n * {@link @nhtio/adk!SpoolReader.readAll}) and delegates to {@link @nhtio/adk!Tokenizable.estimateTokens}. The estimate\n * therefore reflects the actual source bytes — including trailing newlines and non-`\\n` line\n * terminators that the line-based {@link SpooledArtifact.cat} view would otherwise discard or\n * misrepresent.\n *\n * @param encoding - The encoding identifier to use for counting.\n * @returns The estimated number of tokens.\n */\n async estimateTokens(encoding: TokenEncoding): Promise<number> {\n const content = await this.#reader.readAll()\n return Tokenizable.estimateTokens(content, encoding)\n }\n\n /**\n * Returns the full artifact body as a single byte-faithful string.\n *\n * @remarks\n * Round-trip faithful to whatever bytes the {@link @nhtio/adk!SpoolReader} was constructed over —\n * preserves trailing newlines and non-`\\n` line terminators that {@link SpooledArtifact.cat}\n * discards via its line-based view. This is the canonical primitive for \"inline the artifact\n * content directly into a message\" use cases.\n *\n * `asString()` and the static `forgeTools(ctx)` factory on each subclass are independent\n * alternatives — a consumer chooses per turn whether to inline the body in a message\n * (`await tc.results.asString()`) or hand the model query tools\n * (`SpooledArtifact.forgeTools(ctx)`). Neither calls the other; either works with neither.\n *\n * @returns The full content as a single string.\n */\n async asString(): Promise<string> {\n return this.#reader.readAll()\n }\n\n /**\n * Forges a fresh {@link @nhtio/adk!ToolRegistry} of ephemeral {@link @nhtio/adk!ArtifactTool} instances that let the\n * LLM query artifacts already present in `ctx.turnToolCalls`.\n *\n * @remarks\n * Standard subclass extension pattern — each class owns only its own `toolMethods` and its\n * own `forgeTools`. The base `SpooledArtifact.forgeTools(ctx)` narrows the `callId` enum to\n * any `tc.results instanceof SpooledArtifact` (so subclass instances are included — that's\n * the whole point of inheritance) and dispatches the seven base methods (`head`, `tail`,\n * `grep`, `cat`, `byteLength`, `lineCount`, `estimateTokens`) on the resolved artifact.\n * Subclasses override `forgeTools` to call this static first and then register their own\n * tools on the returned registry — see {@link @nhtio/adk!SpooledJsonArtifact.forgeTools} and\n * {@link @nhtio/adk!SpooledMarkdownArtifact.forgeTools} for the canonical shape. There is no\n * `requiresSubclass` field, no helper indirection, and no `this`-based class narrowing —\n * just plain `instanceof ThisClass` at each subclass's own filter site.\n *\n * For each descriptor in this class's `toolMethods`, the factory:\n *\n * 1. Walks `ctx.turnToolCalls` to find `ToolCall`s whose `results instanceof SpooledArtifact`.\n * `ToolCall`s flagged `fromArtifactTool === true` are excluded — they carry a\n * {@link @nhtio/adk!Tokenizable}, not a `SpooledArtifact`, and including them would let the model\n * `artifact_grep` on a previous `artifact_grep` result (an infinite-recursion hazard with\n * no semantic value).\n * 2. Returns an empty registry if no compatible callIds are found — no point shipping tools\n * whose `callId` enum is empty.\n * 3. Otherwise mints an {@link @nhtio/adk!ArtifactTool} with `ephemeral: true` and `onCollision: 'replace'`\n * so multiple `Subclass.forgeTools(ctx)` outputs merge silently. The tool's `inputSchema`\n * includes a required `callId` field with `.valid(...compatibleIds)`, plus the descriptor's\n * own `argsSchema` fields.\n *\n * The handler resolves the artifact via `[...ctx.turnToolCalls].find(t => t.id === callId)`,\n * dispatches the descriptor's method, and serialises the return value (string → as-is;\n * string[] → newline-join; number → `String(n)`; otherwise `JSON.stringify(value, null, 2)`;\n * `descriptor.serialise` overrides the defaults). `grep` is special-cased: the handler\n * constructs `new RegExp(pattern, flags ?? '')` before invoking the artifact's `grep` method.\n *\n * The returned registry must be merged into the consumer's main registry and the main\n * registry must be bound to `ctx` via {@link @nhtio/adk!ToolRegistry.bindContext}:\n *\n * ```ts\n * const executor: DispatchExecutorFn = async (ctx) => {\n * const forged = SpooledArtifact.forgeTools(ctx)\n * const merged = ToolRegistry.merge([main, forged])\n * main.bindContext(ctx)\n * const result = await llm.invoke({ tools: merged.all(), ... })\n * ctx.ack() // ← ephemeral cleanup fires here\n * }\n * ```\n *\n * @warning You **must** call `registry.bindContext(ctx)` on the registry hosting these tools,\n * or ephemeral cleanup will not run and the `callId` enum in subsequent executor calls will\n * be stale (excluding new tool calls produced in the meantime).\n *\n * @param ctx - The execution context whose `turnToolCalls` snapshot defines the `callId` enum.\n * @returns A fresh `ToolRegistry`. Empty when `turnToolCalls` contains no compatible artifacts.\n *\n * @see {@link @nhtio/adk!ToolRegistry.bindContext}\n * @see {@link @nhtio/adk!ToolRegistry.merge}\n * @see {@link @nhtio/adk!DispatchContext.onAck}\n */\n public static forgeTools(ctx: DispatchContext): ToolRegistry {\n const requires: SpooledArtifactConstructor = SpooledArtifact\n const calls = [...ctx.turnToolCalls]\n const compatibleIds = calls\n .filter((tc) => !tc.fromArtifactTool && isInstanceOf(tc.results, requires.name, requires))\n .map((tc) => tc.id)\n if (compatibleIds.length === 0) return new ToolRegistry([])\n\n const tools: ArtifactTool[] = []\n for (const descriptor of this.toolMethods) {\n const callIdSchema = validator\n .string()\n .valid(...compatibleIds)\n .required()\n .description('ToolCall id of the artifact to query.')\n\n const argsSchema = (descriptor.argsSchema ?? noArgsSchema).append({\n callId: callIdSchema,\n })\n\n const serialise = descriptor.serialise ?? defaultSerialise\n\n const tool = new ArtifactTool({\n name: descriptor.name,\n description: descriptor.description,\n inputSchema: argsSchema,\n ephemeral: true,\n onCollision: 'replace',\n handler: async (rawArgs, ctxInner) => {\n const args = rawArgs as Record<string, unknown> & { callId: string }\n const tc = [...ctxInner.turnToolCalls].find((t) => t.id === args.callId)\n if (!tc) {\n return `Error: no tool call with id ${args.callId} in this turn`\n }\n const artifact = tc.results\n if (!isInstanceOf(artifact, requires.name, requires)) {\n return `Error: tool call ${args.callId} results are not a ${requires.name} instance`\n }\n const methodArgs: unknown[] = []\n if (descriptor.method === 'grep') {\n const pattern = args.pattern as string\n const flags = (args.flags as string | undefined) ?? ''\n methodArgs.push(new RegExp(pattern, flags))\n } else if (descriptor.method === 'head' || descriptor.method === 'tail') {\n methodArgs.push((args.n as number | undefined) ?? 10)\n } else if (descriptor.method === 'cat') {\n methodArgs.push(args.start as number | undefined, args.end as number | undefined)\n } else if (descriptor.method === 'estimateTokens') {\n methodArgs.push(args.encoding as TokenEncoding)\n }\n const fn = (artifact as unknown as Record<string, (...a: unknown[]) => unknown>)[\n descriptor.method\n ]\n if (typeof fn !== 'function') {\n return `Error: artifact has no method ${descriptor.method}`\n }\n const result = await Promise.resolve(fn.apply(artifact, methodArgs))\n return serialise(result)\n },\n })\n tools.push(tool)\n }\n return new ToolRegistry(tools)\n }\n}\n"],"mappings":";;;;;;;;;;;;;AA0FA,IAAM,wBAAwB,UAAU,OAA0D;CAChG,MAAM,UAAU,OAAO,EAAE,SAAS;CAClC,aAAa,UAAU,OAAO,EAAE,SAAS;CACzC,aAAa,UACV,IAAI,EACJ,QAAQ,OAAO,YAAY;EAC1B,IAAI,UAAU,SAAS,KAAK,KAAM,MAAc,SAAS,UAAU,OAAO;EAC1E,OAAO,QAAQ,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