@nhtio/adk 0.1.0-master-e04b0eba → 0.1.0-master-bd43a4ea
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/batteries/embeddings/index.d.ts +25 -0
- package/batteries/embeddings/openai/adapter.cjs +185 -0
- package/batteries/embeddings/openai/adapter.cjs.map +1 -0
- package/batteries/embeddings/openai/adapter.d.ts +74 -0
- package/batteries/embeddings/openai/adapter.mjs +183 -0
- package/batteries/embeddings/openai/adapter.mjs.map +1 -0
- package/batteries/embeddings/openai/exceptions.cjs +48 -0
- package/batteries/embeddings/openai/exceptions.cjs.map +1 -0
- package/batteries/embeddings/openai/exceptions.d.ts +45 -0
- package/batteries/embeddings/openai/exceptions.mjs +43 -0
- package/batteries/embeddings/openai/exceptions.mjs.map +1 -0
- package/batteries/embeddings/openai/helpers.cjs +25 -0
- package/batteries/embeddings/openai/helpers.cjs.map +1 -0
- package/batteries/embeddings/openai/helpers.d.ts +25 -0
- package/batteries/embeddings/openai/helpers.mjs +23 -0
- package/batteries/embeddings/openai/helpers.mjs.map +1 -0
- package/batteries/embeddings/openai/index.d.ts +17 -0
- package/batteries/embeddings/openai/types.cjs +2 -0
- package/batteries/embeddings/openai/types.d.ts +123 -0
- package/batteries/embeddings/openai/types.mjs +0 -0
- package/batteries/embeddings/openai/validation.cjs +56 -0
- package/batteries/embeddings/openai/validation.cjs.map +1 -0
- package/batteries/embeddings/openai/validation.d.ts +24 -0
- package/batteries/embeddings/openai/validation.mjs +53 -0
- package/batteries/embeddings/openai/validation.mjs.map +1 -0
- package/batteries/embeddings/openai.cjs +14 -0
- package/batteries/embeddings/openai.mjs +5 -0
- package/batteries/embeddings/webllm/adapter.cjs +147 -0
- package/batteries/embeddings/webllm/adapter.cjs.map +1 -0
- package/batteries/embeddings/webllm/adapter.d.ts +74 -0
- package/batteries/embeddings/webllm/adapter.mjs +145 -0
- package/batteries/embeddings/webllm/adapter.mjs.map +1 -0
- package/batteries/embeddings/webllm/exceptions.cjs +31 -0
- package/batteries/embeddings/webllm/exceptions.cjs.map +1 -0
- package/batteries/embeddings/webllm/exceptions.d.ts +25 -0
- package/batteries/embeddings/webllm/exceptions.mjs +28 -0
- package/batteries/embeddings/webllm/exceptions.mjs.map +1 -0
- package/batteries/embeddings/webllm/index.d.ts +15 -0
- package/batteries/embeddings/webllm/types.cjs +2 -0
- package/batteries/embeddings/webllm/types.d.ts +52 -0
- package/batteries/embeddings/webllm/types.mjs +0 -0
- package/batteries/embeddings/webllm/validation.cjs +43 -0
- package/batteries/embeddings/webllm/validation.cjs.map +1 -0
- package/batteries/embeddings/webllm/validation.d.ts +25 -0
- package/batteries/embeddings/webllm/validation.mjs +40 -0
- package/batteries/embeddings/webllm/validation.mjs.map +1 -0
- package/batteries/embeddings/webllm.cjs +10 -0
- package/batteries/embeddings/webllm.mjs +4 -0
- package/batteries/embeddings.cjs +15 -0
- package/batteries/embeddings.mjs +6 -0
- package/batteries/index.d.ts +1 -0
- package/batteries/llm/openai_chat_completions/adapter.cjs +14 -73
- package/batteries/llm/openai_chat_completions/adapter.cjs.map +1 -1
- package/batteries/llm/openai_chat_completions/adapter.mjs +8 -67
- package/batteries/llm/openai_chat_completions/adapter.mjs.map +1 -1
- package/batteries/llm/openai_chat_completions/helpers.cjs +2 -2
- package/batteries/llm/openai_chat_completions/helpers.mjs +2 -2
- package/batteries/llm/openai_chat_completions/validation.cjs +1 -1
- package/batteries/llm/openai_chat_completions/validation.mjs +1 -1
- package/batteries/llm/webllm_chat_completions/adapter.cjs +7 -6
- package/batteries/llm/webllm_chat_completions/adapter.cjs.map +1 -1
- package/batteries/llm/webllm_chat_completions/adapter.mjs +7 -6
- package/batteries/llm/webllm_chat_completions/adapter.mjs.map +1 -1
- package/batteries/llm/webllm_chat_completions/validation.cjs +1 -1
- package/batteries/llm/webllm_chat_completions/validation.mjs +1 -1
- package/batteries/storage/flydrive.cjs +1 -1
- package/batteries/storage/flydrive.mjs +1 -1
- package/batteries/storage/in_memory.cjs +1 -1
- package/batteries/storage/in_memory.mjs +1 -1
- package/batteries/storage/opfs.cjs +1 -1
- package/batteries/storage/opfs.mjs +1 -1
- package/batteries/tools/color.cjs +2 -2
- package/batteries/tools/color.mjs +2 -2
- package/batteries/tools/comparison.cjs +3 -3
- package/batteries/tools/comparison.mjs +3 -3
- package/batteries/tools/data_structure.cjs +3 -3
- package/batteries/tools/data_structure.mjs +3 -3
- package/batteries/tools/datetime_extended.cjs +2 -2
- package/batteries/tools/datetime_extended.mjs +2 -2
- package/batteries/tools/datetime_math.cjs +2 -2
- package/batteries/tools/datetime_math.mjs +2 -2
- package/batteries/tools/encoding.cjs +3 -3
- package/batteries/tools/encoding.mjs +3 -3
- package/batteries/tools/formatting.cjs +3 -3
- package/batteries/tools/formatting.mjs +3 -3
- package/batteries/tools/geo_basics.cjs +2 -2
- package/batteries/tools/geo_basics.mjs +2 -2
- package/batteries/tools/math.cjs +3 -3
- package/batteries/tools/math.mjs +3 -3
- package/batteries/tools/memory.cjs +5 -5
- package/batteries/tools/memory.mjs +5 -5
- package/batteries/tools/parsing.cjs +4 -4
- package/batteries/tools/parsing.mjs +4 -4
- package/batteries/tools/retrievables.cjs +4 -4
- package/batteries/tools/retrievables.mjs +4 -4
- package/batteries/tools/standing_instructions.cjs +4 -4
- package/batteries/tools/standing_instructions.mjs +4 -4
- package/batteries/tools/statistics.cjs +4 -4
- package/batteries/tools/statistics.mjs +4 -4
- package/batteries/tools/string_processing.cjs +3 -3
- package/batteries/tools/string_processing.mjs +3 -3
- package/batteries/tools/structured_data.cjs +3 -3
- package/batteries/tools/structured_data.mjs +3 -3
- package/batteries/tools/text_analysis.cjs +3 -3
- package/batteries/tools/text_analysis.mjs +3 -3
- package/batteries/tools/text_comparison.cjs +2 -2
- package/batteries/tools/text_comparison.mjs +2 -2
- package/batteries/tools/time.cjs +2 -2
- package/batteries/tools/time.mjs +2 -2
- package/batteries/tools/unit_conversion.cjs +2 -2
- package/batteries/tools/unit_conversion.mjs +2 -2
- package/batteries.cjs +13 -0
- package/batteries.mjs +8 -3
- package/{common-DuKWGTVd.js → common-D_e5zYsG.js} +8 -8
- package/{common-DuKWGTVd.js.map → common-D_e5zYsG.js.map} +1 -1
- package/{common-ClCHam5-.mjs → common-lMrnzoyn.mjs} +8 -8
- package/{common-ClCHam5-.mjs.map → common-lMrnzoyn.mjs.map} +1 -1
- package/common.cjs +7 -7
- package/common.mjs +7 -7
- package/{dispatch_runner-uNtS-XSP.mjs → dispatch_runner-CDF3X0nv.mjs} +3 -3
- package/{dispatch_runner-uNtS-XSP.mjs.map → dispatch_runner-CDF3X0nv.mjs.map} +1 -1
- package/{dispatch_runner-CEFHXRJZ.js → dispatch_runner-CpuyATj1.js} +3 -3
- package/{dispatch_runner-CEFHXRJZ.js.map → dispatch_runner-CpuyATj1.js.map} +1 -1
- package/dispatch_runner.cjs +1 -1
- package/dispatch_runner.mjs +1 -1
- package/exceptions.cjs +1 -1
- package/exceptions.mjs +1 -1
- package/forge.cjs +4 -4
- package/forge.mjs +4 -4
- package/guards.cjs +8 -8
- package/guards.mjs +8 -8
- package/index.cjs +12 -12
- package/index.mjs +12 -12
- package/lib/exceptions/runtime.d.ts +5 -0
- package/lib/utils/retry.cjs +107 -0
- package/lib/utils/retry.cjs.map +1 -0
- package/lib/utils/retry.d.ts +63 -0
- package/lib/utils/retry.mjs +102 -0
- package/lib/utils/retry.mjs.map +1 -0
- package/mcp/adk-docs-corpus.json +1 -1
- package/package.json +138 -73
- package/{runtime-DyD9oQjH.js → runtime-MFFcJrRv.js} +6 -2
- package/runtime-MFFcJrRv.js.map +1 -0
- package/{runtime-CDIZwCT0.mjs → runtime-j92CNi5z.mjs} +6 -2
- package/runtime-j92CNi5z.mjs.map +1 -0
- package/skills/adk-assembly/SKILL.md +2 -2
- package/{spooled_artifact-CHvDDYGA.js → spooled_artifact-B8gIIn9h.js} +4 -4
- package/{spooled_artifact-CHvDDYGA.js.map → spooled_artifact-B8gIIn9h.js.map} +1 -1
- package/{spooled_artifact-D-JrpY4W.mjs → spooled_artifact-CWoKUDEm.mjs} +4 -4
- package/{spooled_artifact-D-JrpY4W.mjs.map → spooled_artifact-CWoKUDEm.mjs.map} +1 -1
- package/spooled_artifact.cjs +2 -2
- package/spooled_artifact.mjs +2 -2
- package/{spooled_markdown_artifact-B4eWOfCX.mjs → spooled_markdown_artifact-CNle4jXN.mjs} +3 -3
- package/{spooled_markdown_artifact-B4eWOfCX.mjs.map → spooled_markdown_artifact-CNle4jXN.mjs.map} +1 -1
- package/{spooled_markdown_artifact-BYfPqFvk.js → spooled_markdown_artifact-DQX0RCdI.js} +3 -3
- package/{spooled_markdown_artifact-BYfPqFvk.js.map → spooled_markdown_artifact-DQX0RCdI.js.map} +1 -1
- package/{thought-DBNsR6l8.js → thought-BD6AkkOr.js} +4 -4
- package/{thought-DBNsR6l8.js.map → thought-BD6AkkOr.js.map} +1 -1
- package/{thought-D9IS11b5.mjs → thought-B_P8LiB6.mjs} +4 -4
- package/{thought-D9IS11b5.mjs.map → thought-B_P8LiB6.mjs.map} +1 -1
- package/{tool-CsYuHhiS.mjs → tool-CRZSUcdP.mjs} +3 -3
- package/{tool-CsYuHhiS.mjs.map → tool-CRZSUcdP.mjs.map} +1 -1
- package/{tool-DIHzOZiV.js → tool-CX9vNHAw.js} +3 -3
- package/{tool-DIHzOZiV.js.map → tool-CX9vNHAw.js.map} +1 -1
- package/{tool_call-CkOVOhg0.js → tool_call--7ti-frB.js} +4 -4
- package/{tool_call-CkOVOhg0.js.map → tool_call--7ti-frB.js.map} +1 -1
- package/{tool_call-Bs_Q5LOG.mjs → tool_call-BUeMuCc6.mjs} +4 -4
- package/{tool_call-Bs_Q5LOG.mjs.map → tool_call-BUeMuCc6.mjs.map} +1 -1
- package/{tool_registry-CX3ofUh9.mjs → tool_registry-BGHg6KTq.mjs} +2 -2
- package/{tool_registry-CX3ofUh9.mjs.map → tool_registry-BGHg6KTq.mjs.map} +1 -1
- package/{tool_registry-CKk5ooxm.js → tool_registry-CtCQ4Xoz.js} +2 -2
- package/{tool_registry-CKk5ooxm.js.map → tool_registry-CtCQ4Xoz.js.map} +1 -1
- package/{turn_runner-D0qGIrRI.js → turn_runner-BJTtAORU.js} +7 -6
- package/turn_runner-BJTtAORU.js.map +1 -0
- package/{turn_runner-C1-mup84.mjs → turn_runner-C02LZHjt.mjs} +8 -7
- package/turn_runner-C02LZHjt.mjs.map +1 -0
- package/turn_runner.cjs +1 -1
- package/turn_runner.mjs +1 -1
- package/runtime-CDIZwCT0.mjs.map +0 -1
- package/runtime-DyD9oQjH.js.map +0 -1
- package/turn_runner-C1-mup84.mjs.map +0 -1
- package/turn_runner-D0qGIrRI.js.map +0 -1
package/{spooled_markdown_artifact-B4eWOfCX.mjs.map → spooled_markdown_artifact-CNle4jXN.mjs.map}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spooled_markdown_artifact-B4eWOfCX.mjs","names":["#format","#resolveFormat","#resolveRecords","#parsed","#resolveIndex","#index","#frontmatter"],"sources":["../src/lib/classes/spooled_json_artifact.ts","../src/lib/classes/spooled_markdown_artifact.ts"],"sourcesContent":["import { default as JSON5 } from 'json5'\nimport { JSONPath } from 'jsonpath-plus'\nimport { validator } from '@nhtio/validation'\nimport { ArtifactTool } from './artifact_tool'\nimport { ToolRegistry } from './tool_registry'\nimport { isInstanceOf, isObject } from '../utils/guards'\nimport { SpooledArtifact, defaultSerialise } from './spooled_artifact'\nimport type { SpoolReader } from '../contracts/spool_reader'\nimport type { ToolMethodDescriptor } from './spooled_artifact'\nimport type { DispatchContext } from '../contracts/dispatch_context'\n\n/**\n * The set of JSON-derived formats that {@link SpooledJsonArtifact} can handle.\n *\n * @remarks\n * - `json` — a single JSON value spanning the entire artifact (strict RFC 8259).\n * - `json5` — a single JSON5 value spanning the entire artifact; permits comments, trailing\n * commas, unquoted keys, and other relaxed syntax via the `json5` package.\n * - `jsonl` — newline-delimited JSON; each non-empty line is an independent JSON value.\n * - `ndjson` — alias for `jsonl`; both names are accepted and behave identically.\n */\nexport type JsonArtifactFormat = 'json' | 'json5' | 'jsonl' | 'ndjson'\n\n/**\n * Detects the {@link JsonArtifactFormat} of a raw string.\n *\n * @remarks\n * Detection strategy (in order):\n * 1. If the content parses as strict JSON → `json`.\n * 2. If every non-empty line parses as strict JSON → `jsonl`.\n * 3. If the content parses as JSON5 → `json5`.\n * 4. Otherwise throws.\n *\n * Strict JSON is tried before JSON5 so that well-formed JSON files are not unnecessarily\n * classified as JSON5.\n *\n * @param content - The full artifact text to inspect.\n * @returns The inferred format.\n * @throws `Error` when the content cannot be classified as any supported JSON format.\n */\nfunction inferFormat(content: string): JsonArtifactFormat {\n const trimmed = content.trim()\n\n // 1. Try strict JSON\n try {\n JSON.parse(trimmed)\n return 'json'\n } catch {\n // fall through\n }\n\n // 2. Try JSONL (every non-empty line is valid JSON)\n const nonEmptyLines = trimmed.split('\\n').filter((l) => l.trim().length > 0)\n if (\n nonEmptyLines.length > 0 &&\n nonEmptyLines.every((l) => {\n try {\n JSON.parse(l)\n return true\n } catch {\n return false\n }\n })\n ) {\n return 'jsonl'\n }\n\n // 3. Try JSON5\n try {\n JSON5.parse(trimmed)\n return 'json5'\n } catch {\n // fall through\n }\n\n throw new Error('Unable to infer JSON format: content is not valid JSON, JSONL, NDJSON, or JSON5')\n}\n\n/**\n * A {@link @nhtio/adk!SpooledArtifact} specialisation that adds JSON-aware read operations.\n *\n * @typeParam T - The expected shape of each parsed record. Defaults to `unknown`.\n *\n * @remarks\n * Construct with an optional `format` hint. When omitted the format is auto-detected on first\n * access by reading the full artifact and running {@link inferFormat}. Once detected (or\n * provided), the format is cached for the lifetime of the instance.\n *\n * All JSON methods are async, consistent with {@link @nhtio/adk!SpooledArtifact}.\n *\n * Path-based methods (`json_get`, `json_filter`, `json_pluck`) use\n * [JSONPath-Plus](https://github.com/JSONPath-Plus/JSONPath) expressions. Full JSONPath syntax\n * is supported, including recursive descent (`..`), filter expressions (`[?(@.age > 18)]`),\n * and union selectors.\n */\nexport class SpooledJsonArtifact<T = unknown> extends SpooledArtifact {\n #format: JsonArtifactFormat | undefined\n #parsed: T[] | undefined\n\n /**\n * @param reader - The backing store to read from.\n * @param format - Optional format hint. When omitted, the format is inferred on first access.\n */\n constructor(reader: SpoolReader, format?: JsonArtifactFormat) {\n super(reader)\n this.#format = format\n }\n\n /**\n * Returns `true` if `value` is a {@link SpooledJsonArtifact} instance.\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. Matches the pattern used by every\n * other class guard in the ADK; safe against the dual-module-copy case where two distinct\n * `SpooledJsonArtifact` classes coexist in the same realm.\n *\n * @param value - The value to test.\n * @returns `true` when `value` is a {@link SpooledJsonArtifact} instance.\n */\n public static isSpooledJsonArtifact(value: unknown): value is SpooledJsonArtifact {\n return isInstanceOf(value, 'SpooledJsonArtifact', SpooledJsonArtifact)\n }\n\n /**\n * The JSON-specific artifact-query descriptors this class adds on top of the base set.\n *\n * @remarks\n * Lists `artifact_json_type`, `artifact_json_keys`, `artifact_json_length`,\n * `artifact_json_get`, `artifact_json_filter`, `artifact_json_slice`, `artifact_json_pluck`.\n * The base seven descriptors (`artifact_head`, etc.) are NOT included here — they are\n * forged separately by {@link SpooledJsonArtifact.forgeTools}, which calls\n * `SpooledArtifact.forgeTools(ctx)` to produce the base-narrowed tools and then registers\n * its own JSON tools on the result. Downstream consumers building custom subclasses\n * should follow the same pattern: own only your own descriptors; override `forgeTools` to\n * compose with the base output.\n */\n public static toolMethods: ReadonlyArray<ToolMethodDescriptor> = Object.freeze([\n {\n name: 'artifact_json_type',\n method: 'json_type',\n description:\n 'Return the JSON format (json | json5 | jsonl | ndjson) of a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_json_keys',\n method: 'json_keys',\n description: 'Return the top-level keys of a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_json_length',\n method: 'json_length',\n description:\n 'Return the record count of a JSON artifact produced earlier in this turn (1 for json/json5; line count for jsonl/ndjson).',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_json_get',\n method: 'json_get',\n description:\n 'Evaluate a JSONPath expression against a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({\n path: validator.string().required().description(\"JSONPath expression, e.g. '$.user.name'.\"),\n }),\n },\n {\n name: 'artifact_json_filter',\n method: 'json_filter',\n description:\n 'Return records of a JSON artifact (produced earlier in this turn) matched by a JSONPath filter.',\n argsSchema: validator.object({\n path: validator\n .string()\n .required()\n .description(\"JSONPath filter expression, e.g. '$[?(@.age>18)]'.\"),\n }),\n },\n {\n name: 'artifact_json_slice',\n method: 'json_slice',\n description:\n 'Return a slice of records by index range from a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({\n start: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start index (inclusive).'),\n end: validator.number().integer().min(0).optional().description('End index (exclusive).'),\n }),\n },\n {\n name: 'artifact_json_pluck',\n method: 'json_pluck',\n description:\n 'Return all values matched by a JSONPath expression across every record of a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({\n path: validator.string().required().description(\"JSONPath expression, e.g. '$..name'.\"),\n }),\n },\n ])\n\n /**\n * Forges base-class tools plus JSON-specific tools narrowed to {@link SpooledJsonArtifact}.\n *\n * @remarks\n * Standard subclass extension pattern: call `SpooledArtifact.forgeTools(ctx)` to produce\n * the base seven `artifact_*` tools narrowed to any `SpooledArtifact` in the turn, then\n * register one `ArtifactTool` per JSON-specific descriptor narrowed to JSON artifacts.\n * Downstream consumers building their own subclasses should follow the same shape.\n */\n public static override forgeTools(ctx: DispatchContext): ToolRegistry {\n const registry = SpooledArtifact.forgeTools(ctx)\n const requires = SpooledJsonArtifact\n const compatibleIds = [...ctx.turnToolCalls]\n .filter((tc) => !tc.fromArtifactTool && isInstanceOf(tc.results, requires.name, requires))\n .map((tc) => tc.id)\n if (compatibleIds.length === 0) return registry\n\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 = (\n descriptor.argsSchema ?? validator.object<Record<string, never>>({})\n ).append({\n callId: callIdSchema,\n })\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 (\n descriptor.method === 'json_get' ||\n descriptor.method === 'json_filter' ||\n descriptor.method === 'json_pluck'\n ) {\n methodArgs.push(args.path as string)\n } else if (descriptor.method === 'json_slice') {\n methodArgs.push(args.start as number | undefined, args.end as number | undefined)\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 const serialise = descriptor.serialise ?? defaultSerialise\n return serialise(result)\n },\n })\n registry.register(tool)\n }\n return registry\n }\n\n /**\n * Resolves and caches the detected or provided format.\n */\n async #resolveFormat(): Promise<JsonArtifactFormat> {\n if (this.#format !== undefined) {\n return this.#format\n }\n const lines = await this.cat()\n this.#format = inferFormat(lines.join('\\n'))\n return this.#format\n }\n\n /**\n * Parses and caches all records from the artifact.\n *\n * @remarks\n * For `json`/`json5` format: returns a single-element array containing the parsed root value.\n * For `jsonl`/`ndjson` format: returns one element per non-empty line.\n */\n async #resolveRecords(): Promise<T[]> {\n if (this.#parsed !== undefined) {\n return this.#parsed\n }\n const format = await this.#resolveFormat()\n const lines = await this.cat()\n if (format === 'json') {\n this.#parsed = [JSON.parse(lines.join('\\n')) as T]\n } else if (format === 'json5') {\n this.#parsed = [JSON5.parse(lines.join('\\n')) as T]\n } else {\n // jsonl / ndjson\n this.#parsed = lines.filter((l) => l.trim().length > 0).map((l) => JSON.parse(l) as T)\n }\n return this.#parsed\n }\n\n /**\n * Returns the detected or provided format for this artifact.\n *\n * @returns One of `'json'`, `'json5'`, `'jsonl'`, or `'ndjson'`.\n */\n async json_type(): Promise<JsonArtifactFormat> {\n return this.#resolveFormat()\n }\n\n /**\n * Returns the top-level keys of the parsed content.\n *\n * @remarks\n * - For `json`/`json5`: returns the keys of the root object, or `undefined` when the root is\n * not a plain object (e.g. an array or scalar).\n * - For `jsonl`/`ndjson`: returns the union of keys across all records that are plain objects.\n * Duplicate keys are deduplicated.\n *\n * @returns Array of key strings, or `undefined` when no object keys are present.\n */\n async json_keys(): Promise<string[] | undefined> {\n const records = await this.#resolveRecords()\n const format = await this.#resolveFormat()\n if (format === 'json' || format === 'json5') {\n const root = records[0]\n if (isObject(root)) {\n return Object.keys(root as object)\n }\n return undefined\n }\n const keySet = new Set<string>()\n for (const record of records) {\n if (isObject(record)) {\n for (const key of Object.keys(record as object)) {\n keySet.add(key)\n }\n }\n }\n return keySet.size > 0 ? Array.from(keySet) : undefined\n }\n\n /**\n * Returns the total number of records in the artifact.\n *\n * @remarks\n * - For `json`/`json5`: always `1` (the entire artifact is a single value).\n * - For `jsonl`/`ndjson`: the number of non-empty lines.\n *\n * @returns The record count.\n */\n async json_length(): Promise<number> {\n const records = await this.#resolveRecords()\n return records.length\n }\n\n /**\n * Evaluates a JSONPath expression against the parsed content.\n *\n * @remarks\n * Uses [JSONPath-Plus](https://github.com/JSONPath-Plus/JSONPath). Full JSONPath syntax is\n * supported: recursive descent (`$..*`), filter expressions (`$[?(@.age > 18)]`), union\n * selectors, and more.\n *\n * - For `json`/`json5`: evaluates the expression against the root value.\n * - For `jsonl`/`ndjson`: evaluates the expression against each record and returns a flat\n * array of all matches across all records.\n *\n * @param path - A JSONPath expression (e.g. `'$.user.address.city'`, `'$..name'`).\n * @returns Array of matched values. Empty array when no matches are found.\n */\n async json_get(path: string): Promise<unknown[]> {\n const records = await this.#resolveRecords()\n const format = await this.#resolveFormat()\n if (format === 'json' || format === 'json5') {\n return JSONPath({ path, json: records[0] as object })\n }\n return records.flatMap((r) => JSONPath({ path, json: r as object }))\n }\n\n /**\n * Returns a slice of the parsed records by index range.\n *\n * @remarks\n * For `json`/`json5`: always returns `[root]` — the artifact is a single record so slicing is\n * not meaningful. For `jsonl`/`ndjson`: behaves like `Array.prototype.slice`.\n *\n * @param start - Start index (inclusive). Defaults to `0`.\n * @param end - End index (exclusive). Defaults to the record count.\n * @returns Array of sliced records.\n */\n async json_slice(start?: number, end?: number): Promise<T[]> {\n const records = await this.#resolveRecords()\n const format = await this.#resolveFormat()\n if (format === 'json' || format === 'json5') {\n return records\n }\n return records.slice(start, end)\n }\n\n /**\n * Returns records matched by a JSONPath filter expression.\n *\n * @remarks\n * Evaluates `path` against each record and returns those for which the expression produces at\n * least one match. For `json`/`json5`, evaluates against the root value and returns it in an\n * array if matched.\n *\n * @param path - A JSONPath expression (e.g. `'$[?(@.status === \"active\")]'`).\n * @returns Array of matching records.\n */\n async json_filter(path: string): Promise<T[]> {\n const records = await this.#resolveRecords()\n return records.filter((r) => {\n const matches = JSONPath({ path, json: r as object })\n return Array.isArray(matches) && matches.length > 0\n })\n }\n\n /**\n * Returns all values matched by a JSONPath expression across every record.\n *\n * @remarks\n * Convenience over {@link SpooledJsonArtifact.json_get} with an identical signature — use\n * whichever name better communicates intent at the call site. `json_pluck` reads well for\n * extracting a single field column; `json_get` reads well for structured queries.\n *\n * @param path - A JSONPath expression (e.g. `'$..name'`).\n * @returns Array of matched values.\n */\n async json_pluck(path: string): Promise<unknown[]> {\n return this.json_get(path)\n }\n}\n","import { remark } from 'remark'\nimport { visit } from 'unist-util-visit'\nimport { load as yamlLoad } from 'js-yaml'\nimport { validator } from '@nhtio/validation'\nimport { isInstanceOf } from '../utils/guards'\nimport { ArtifactTool } from './artifact_tool'\nimport { ToolRegistry } from './tool_registry'\nimport { default as remarkGfm } from 'remark-gfm'\nimport { toString as mdastToString } from 'mdast-util-to-string'\nimport { default as remarkFrontmatter } from 'remark-frontmatter'\nimport { SpooledArtifact, defaultSerialise } from './spooled_artifact'\nimport type { Root, Link, Image } from 'mdast'\nimport type { SpoolReader } from '../contracts/spool_reader'\nimport type { ToolMethodDescriptor } from './spooled_artifact'\nimport type { DispatchContext } from '../contracts/dispatch_context'\n\n/**\n * A single heading entry in the document's structural index.\n *\n * @remarks\n * `startLine` is the 0-based line of the heading itself. `endLine` is the 0-based index of the\n * last line belonging to this section (inclusive) — the line immediately before the next heading\n * of equal or lesser depth, or the last line of the document.\n */\nexport interface MarkdownHeadingEntry {\n /** ATX heading depth: 1 (`#`) through 6 (`######`). */\n depth: 1 | 2 | 3 | 4 | 5 | 6\n /** The heading text with the leading `#` prefix stripped and trimmed. */\n text: string\n /** 0-based line index of the heading line itself. */\n startLine: number\n /** 0-based line index of the last line in this section (inclusive). */\n endLine: number\n}\n\n/**\n * A single fenced code block entry in the document's structural index.\n */\nexport interface MarkdownCodeEntry {\n /** The language identifier immediately after the opening fence, or `null` when absent. */\n lang: string | null\n /** 0-based line index of the opening fence line. */\n startLine: number\n /** 0-based line index of the closing fence line. */\n endLine: number\n}\n\ninterface MarkdownIndex {\n headings: MarkdownHeadingEntry[]\n codeBlocks: MarkdownCodeEntry[]\n}\n\n/**\n * A section of a markdown document as returned by {@link SpooledMarkdownArtifact.md_sections}.\n *\n * @remarks\n * Contains only line-range metadata — no content is fetched until the caller explicitly\n * requests it via `cat(bodyStartLine, bodyEndLine + 1)`.\n */\nexport interface MarkdownSection {\n depth: 1 | 2 | 3 | 4 | 5 | 6\n /** The heading text. */\n heading: string\n /** 0-based line of the heading itself. */\n headingLine: number\n /** 0-based line of the first body line (heading line + 1). */\n bodyStartLine: number\n /** 0-based line of the last body line (inclusive). */\n bodyEndLine: number\n}\n\n/**\n * Returns a configured remark processor with frontmatter and GFM support.\n */\nfunction processor() {\n return remark().use(remarkFrontmatter).use(remarkGfm)\n}\n\n/**\n * Parses a heading line (e.g. `## My Heading`) and returns `{ depth, text }`, or `null` when\n * the line is not an ATX heading.\n */\nfunction parseHeadingLine(line: string): { depth: 1 | 2 | 3 | 4 | 5 | 6; text: string } | null {\n const match = /^(#{1,6})\\s+(.*)$/.exec(line)\n if (!match) return null\n const depth = match[1].length as 1 | 2 | 3 | 4 | 5 | 6\n return { depth, text: match[2].trim() }\n}\n\n/**\n * Returns the length of a fence marker at the start of `line` (3+), or `0` if the line is not\n * a fence opener/closer. Handles both backtick (` ``` `) and tilde (`~~~`) fences.\n */\nfunction fenceLength(line: string): number {\n const trimmed = line.trimStart()\n const match = /^(`{3,}|~{3,})/.exec(trimmed)\n return match ? match[1].length : 0\n}\n\n/**\n * Extracts the language identifier from a fence opener line (e.g. ` ```ts ` → `'ts'`), or\n * `null` when none is present.\n */\nfunction fenceLang(line: string): string | null {\n const trimmed = line.trimStart()\n const match = /^(?:`{3,}|~{3,})(\\S+)/.exec(trimmed)\n return match ? match[1] : null\n}\n\n/**\n * A {@link @nhtio/adk!SpooledArtifact} specialisation that adds markdown-aware structural queries.\n *\n * @remarks\n * Designed for large markdown documents where loading the full content into memory is\n * impractical. The structural index (heading positions, code block positions) is built by a\n * single line-by-line scan of the {@link @nhtio/adk!SpoolReader} without retaining any content. Only the\n * tiny metadata index and the parsed frontmatter object are cached.\n *\n * Content retrieval is always bounded — use `cat(start, end)` or the `startLine`/`endLine`\n * parameters on inline methods to fetch only the lines you need.\n *\n * Inline methods (`md_links`, `md_images`, `md_text`, `md_ast`) accept optional line-range\n * arguments. Without a range they read the full document — documented trade-off, caller\n * responsibility to bound the range for large documents.\n *\n * The processor always applies `remark-gfm` (tables, task lists, strikethrough, autolinks)\n * in addition to standard CommonMark and YAML frontmatter.\n */\nexport class SpooledMarkdownArtifact extends SpooledArtifact {\n #index: MarkdownIndex | undefined\n #frontmatter: Record<string, unknown> | null | undefined\n\n /**\n * @param reader - The backing store to read from.\n */\n constructor(reader: SpoolReader) {\n super(reader)\n }\n\n /**\n * Returns `true` if `value` is a {@link SpooledMarkdownArtifact} instance.\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. Matches the pattern used by every\n * other class guard in the ADK; safe against the dual-module-copy case where two distinct\n * `SpooledMarkdownArtifact` classes coexist in the same realm.\n */\n public static isSpooledMarkdownArtifact(value: unknown): value is SpooledMarkdownArtifact {\n return isInstanceOf(value, 'SpooledMarkdownArtifact', SpooledMarkdownArtifact)\n }\n\n /**\n * The markdown-specific artifact-query descriptors this class adds on top of the base set.\n *\n * @remarks\n * Lists `artifact_md_frontmatter`, `artifact_md_headings`, `artifact_md_code_blocks`,\n * `artifact_md_sections`, `artifact_md_links`, `artifact_md_images`, `artifact_md_text`,\n * `artifact_md_ast`. The base seven descriptors (`artifact_head`, etc.) are NOT included\n * here — they are forged separately by {@link SpooledMarkdownArtifact.forgeTools}, which\n * calls `SpooledArtifact.forgeTools(ctx)` to produce the base-narrowed tools and then\n * registers its own markdown tools on the result. Downstream consumers building custom\n * subclasses should follow the same pattern: own only your own descriptors; override\n * `forgeTools` to compose with the base output.\n */\n public static toolMethods: ReadonlyArray<ToolMethodDescriptor> = Object.freeze([\n {\n name: 'artifact_md_frontmatter',\n method: 'md_frontmatter',\n description:\n 'Return parsed YAML frontmatter (or undefined) from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_md_headings',\n method: 'md_headings',\n description:\n 'Return all headings, optionally filtered by depth, from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n depth: validator\n .number()\n .integer()\n .min(1)\n .max(6)\n .optional()\n .description('ATX heading depth (1-6).'),\n }),\n },\n {\n name: 'artifact_md_code_blocks',\n method: 'md_code_blocks',\n description:\n 'Return all fenced code block entries, optionally filtered by language, from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n lang: validator\n .string()\n .optional()\n .description('Language identifier. Pass empty string to match blocks with no lang.'),\n }),\n },\n {\n name: 'artifact_md_sections',\n method: 'md_sections',\n description:\n 'Return document sections (line-range metadata only) from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n depth: validator\n .number()\n .integer()\n .min(1)\n .max(6)\n .optional()\n .description('ATX heading depth (1-6).'),\n }),\n },\n {\n name: 'artifact_md_links',\n method: 'md_links',\n description:\n 'Return all inline and reference links within the given line range from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n {\n name: 'artifact_md_images',\n method: 'md_images',\n description:\n 'Return all images within the given line range from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n {\n name: 'artifact_md_text',\n method: 'md_text',\n description:\n 'Return plain text with markup stripped, for the given line range, from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n {\n name: 'artifact_md_ast',\n method: 'md_ast',\n description:\n 'Return the full MDAST Root for the specified line range from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n ])\n\n /**\n * Forges base-class tools plus markdown-specific tools narrowed to\n * {@link SpooledMarkdownArtifact}.\n *\n * @remarks\n * Standard subclass extension pattern: call `SpooledArtifact.forgeTools(ctx)` to produce\n * the base seven `artifact_*` tools narrowed to any `SpooledArtifact` in the turn, then\n * register one `ArtifactTool` per markdown-specific descriptor narrowed to markdown\n * artifacts. Downstream consumers building their own subclasses should follow the same\n * shape.\n */\n public static override forgeTools(ctx: DispatchContext): ToolRegistry {\n const registry = SpooledArtifact.forgeTools(ctx)\n const requires = SpooledMarkdownArtifact\n const compatibleIds = [...ctx.turnToolCalls]\n .filter((tc) => !tc.fromArtifactTool && isInstanceOf(tc.results, requires.name, requires))\n .map((tc) => tc.id)\n if (compatibleIds.length === 0) return registry\n\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 = (\n descriptor.argsSchema ?? validator.object<Record<string, never>>({})\n ).append({\n callId: callIdSchema,\n })\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 === 'md_headings' || descriptor.method === 'md_sections') {\n methodArgs.push(args.depth as number | undefined)\n } else if (descriptor.method === 'md_code_blocks') {\n methodArgs.push(args.lang as string | undefined)\n } else if (\n descriptor.method === 'md_links' ||\n descriptor.method === 'md_images' ||\n descriptor.method === 'md_text' ||\n descriptor.method === 'md_ast'\n ) {\n methodArgs.push(\n args.startLine as number | undefined,\n args.endLine as number | undefined\n )\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 const serialise = descriptor.serialise ?? defaultSerialise\n return serialise(result)\n },\n })\n registry.register(tool)\n }\n return registry\n }\n\n /**\n * Builds the structural index in a single top-to-bottom pass, retaining only metadata.\n */\n async #resolveIndex(): Promise<MarkdownIndex> {\n if (this.#index !== undefined) return this.#index\n\n const count = await this.lineCount()\n const headingsRaw: Array<{ depth: 1 | 2 | 3 | 4 | 5 | 6; text: string; startLine: number }> = []\n const codeBlocks: MarkdownCodeEntry[] = []\n\n let inFrontmatter = false\n let frontmatterClosed = false\n let inCodeBlock = false\n let openFenceLen = 0\n let openFenceStartLine = 0\n let openFenceLang: string | null = null\n\n for (let i = 0; i < count; i++) {\n const rawLine = await this.line(i)\n const l = rawLine ?? ''\n\n // Handle frontmatter block at the top of the document\n if (i === 0 && l.trim() === '---') {\n inFrontmatter = true\n continue\n }\n if (inFrontmatter) {\n if (l.trim() === '---') {\n inFrontmatter = false\n frontmatterClosed = true\n }\n continue\n }\n if (!frontmatterClosed && i === 0) {\n // No frontmatter — treat as normal document from the start\n }\n\n // Handle fenced code blocks\n const fLen = fenceLength(l)\n if (!inCodeBlock && fLen >= 3) {\n inCodeBlock = true\n openFenceLen = fLen\n openFenceStartLine = i\n openFenceLang = fenceLang(l)\n continue\n }\n if (inCodeBlock) {\n // A closing fence must use the same fence character (` or ~) and be at least as long.\n if (fLen >= openFenceLen) {\n const openLine = (await this.line(openFenceStartLine)) ?? ''\n const openChar = openLine.trimStart()[0]\n const closeChar = l.trimStart()[0]\n if (closeChar === openChar) {\n codeBlocks.push({ lang: openFenceLang, startLine: openFenceStartLine, endLine: i })\n inCodeBlock = false\n openFenceLen = 0\n }\n }\n continue\n }\n\n // Detect ATX headings (not inside code blocks, not in frontmatter)\n const heading = parseHeadingLine(l)\n if (heading) {\n headingsRaw.push({ ...heading, startLine: i })\n }\n }\n\n // Unclosed code block — record it anyway\n if (inCodeBlock) {\n codeBlocks.push({ lang: openFenceLang, startLine: openFenceStartLine, endLine: count - 1 })\n }\n\n // Post-process heading endLine values\n const headings: MarkdownHeadingEntry[] = headingsRaw.map((h, idx) => {\n const nextBoundary = headingsRaw.slice(idx + 1).find((n) => n.depth <= h.depth)\n const endLine = nextBoundary ? nextBoundary.startLine - 1 : count - 1\n return { ...h, endLine }\n })\n\n this.#index = { headings, codeBlocks }\n return this.#index\n }\n\n // ── Frontmatter ───────────────────────────────────────────────────────────\n\n /**\n * Returns the parsed YAML frontmatter, or `undefined` when no frontmatter block is present.\n *\n * @remarks\n * Short-circuits after reading the frontmatter block — never reads the document body. Caches\n * the result so subsequent calls are free. The result is `undefined` (not an empty object)\n * when no frontmatter is found, distinguishing \"no frontmatter\" from \"empty frontmatter\".\n */\n async md_frontmatter(): Promise<Record<string, unknown> | undefined> {\n if (this.#frontmatter !== undefined) return this.#frontmatter ?? undefined\n\n const firstLine = await this.line(0)\n if (firstLine?.trim() !== '---') {\n this.#frontmatter = null\n return undefined\n }\n\n const maxScan = Math.min(await this.lineCount(), 200)\n const yamlLines: string[] = []\n let closed = false\n for (let i = 1; i < maxScan; i++) {\n const l = await this.line(i)\n if (l?.trim() === '---') {\n closed = true\n break\n }\n yamlLines.push(l ?? '')\n }\n\n if (!closed) {\n this.#frontmatter = null\n return undefined\n }\n\n this.#frontmatter = yamlLoad(yamlLines.join('\\n')) as Record<string, unknown>\n return this.#frontmatter\n }\n\n // ── Structural index queries ──────────────────────────────────────────────\n\n /**\n * Returns all headings in document order, optionally filtered by depth.\n *\n * @remarks\n * Uses the cached structural index — no content is fetched from the {@link @nhtio/adk!SpoolReader}.\n *\n * @param depth - When provided, only headings at this ATX depth (1–6) are returned.\n */\n async md_headings(depth?: 1 | 2 | 3 | 4 | 5 | 6): Promise<MarkdownHeadingEntry[]> {\n const index = await this.#resolveIndex()\n if (depth === undefined) return index.headings.slice()\n return index.headings.filter((h) => h.depth === depth)\n }\n\n /**\n * Returns all fenced code block entries, optionally filtered by language identifier.\n *\n * @remarks\n * Returns line-range metadata only — no content is fetched. Use `cat(entry.startLine + 1,\n * entry.endLine)` to retrieve the code body (excluding fence lines).\n *\n * @param lang - When provided, only blocks with this exact lang identifier are returned.\n * Pass an empty string to match blocks with no lang identifier.\n */\n async md_code_blocks(lang?: string): Promise<MarkdownCodeEntry[]> {\n const index = await this.#resolveIndex()\n if (lang === undefined) return index.codeBlocks.slice()\n const target = lang === '' ? null : lang\n return index.codeBlocks.filter((b) => b.lang === target)\n }\n\n /**\n * Returns document sections derived from the structural index.\n *\n * @remarks\n * Returns only line-range metadata — body content is never fetched. To retrieve the body of a\n * section, call `cat(section.bodyStartLine, section.bodyEndLine + 1)`.\n *\n * When `depth` is provided, only sections introduced by a heading at that depth are returned;\n * deeper headings become part of the body.\n *\n * @param depth - When provided, only sections at this ATX depth (1–6) are returned.\n */\n async md_sections(depth?: 1 | 2 | 3 | 4 | 5 | 6): Promise<MarkdownSection[]> {\n const index = await this.#resolveIndex()\n const headings =\n depth !== undefined ? index.headings.filter((h) => h.depth === depth) : index.headings\n return headings.map((h) => ({\n depth: h.depth,\n heading: h.text,\n headingLine: h.startLine,\n bodyStartLine: h.startLine + 1,\n bodyEndLine: h.endLine,\n }))\n }\n\n // ── Inline queries (bounded) ──────────────────────────────────────────────\n\n /**\n * Returns the full MDAST Root for the specified line range.\n *\n * @remarks\n * Without a range, reads the full document — for large documents, use\n * {@link SpooledMarkdownArtifact.md_sections} to locate sections and pass\n * bounded line ranges here.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_ast(startLine?: number, endLine?: number): Promise<Root> {\n const lines = await this.cat(startLine, endLine)\n return processor().parse(lines.join('\\n')) as Root\n }\n\n /**\n * Returns all inline and reference links in the specified line range.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_links(\n startLine?: number,\n endLine?: number\n ): Promise<Array<{ text: string; url: string; title?: string }>> {\n const lines = await this.cat(startLine, endLine)\n const ast = processor().parse(lines.join('\\n'))\n const results: Array<{ text: string; url: string; title?: string }> = []\n visit(ast, 'link', (node: Link) => {\n results.push({\n text: mdastToString(node),\n url: node.url,\n title: node.title ?? undefined,\n })\n })\n return results\n }\n\n /**\n * Returns all images in the specified line range.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_images(\n startLine?: number,\n endLine?: number\n ): Promise<Array<{ alt: string; url: string; title?: string }>> {\n const lines = await this.cat(startLine, endLine)\n const ast = processor().parse(lines.join('\\n'))\n const results: Array<{ alt: string; url: string; title?: string }> = []\n visit(ast, 'image', (node: Image) => {\n results.push({\n alt: node.alt ?? '',\n url: node.url,\n title: node.title ?? undefined,\n })\n })\n return results\n }\n\n /**\n * Returns all document text with markup stripped, for the specified line range.\n *\n * @remarks\n * Uses `mdast-util-to-string` to extract plain text from the AST — code, link text, and\n * alt text are included; markdown syntax is removed.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_text(startLine?: number, endLine?: number): Promise<string> {\n const lines = await this.cat(startLine, endLine)\n const ast = processor().parse(lines.join('\\n'))\n return mdastToString(ast)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCA,SAAS,YAAY,SAAqC;CACxD,MAAM,UAAU,QAAQ,KAAK;CAG7B,IAAI;EACF,KAAK,MAAM,OAAO;EAClB,OAAO;CACT,QAAQ,CAER;CAGA,MAAM,gBAAgB,QAAQ,MAAM,IAAI,EAAE,QA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,WAAoB,SAAmC;EACnE,MAAM,QAAQ,MAAM,KAAK,IAAI,WAAW,OAAO;EAE/C,OAAO,SADK,UAAU,EAAE,MAAM,MAAM,KAAK,IAAI,CACxB,CAAG;CAC1B;AACF"}
|
|
1
|
+
{"version":3,"file":"spooled_markdown_artifact-CNle4jXN.mjs","names":["#format","#resolveFormat","#resolveRecords","#parsed","#resolveIndex","#index","#frontmatter"],"sources":["../src/lib/classes/spooled_json_artifact.ts","../src/lib/classes/spooled_markdown_artifact.ts"],"sourcesContent":["import { default as JSON5 } from 'json5'\nimport { JSONPath } from 'jsonpath-plus'\nimport { validator } from '@nhtio/validation'\nimport { ArtifactTool } from './artifact_tool'\nimport { ToolRegistry } from './tool_registry'\nimport { isInstanceOf, isObject } from '../utils/guards'\nimport { SpooledArtifact, defaultSerialise } from './spooled_artifact'\nimport type { SpoolReader } from '../contracts/spool_reader'\nimport type { ToolMethodDescriptor } from './spooled_artifact'\nimport type { DispatchContext } from '../contracts/dispatch_context'\n\n/**\n * The set of JSON-derived formats that {@link SpooledJsonArtifact} can handle.\n *\n * @remarks\n * - `json` — a single JSON value spanning the entire artifact (strict RFC 8259).\n * - `json5` — a single JSON5 value spanning the entire artifact; permits comments, trailing\n * commas, unquoted keys, and other relaxed syntax via the `json5` package.\n * - `jsonl` — newline-delimited JSON; each non-empty line is an independent JSON value.\n * - `ndjson` — alias for `jsonl`; both names are accepted and behave identically.\n */\nexport type JsonArtifactFormat = 'json' | 'json5' | 'jsonl' | 'ndjson'\n\n/**\n * Detects the {@link JsonArtifactFormat} of a raw string.\n *\n * @remarks\n * Detection strategy (in order):\n * 1. If the content parses as strict JSON → `json`.\n * 2. If every non-empty line parses as strict JSON → `jsonl`.\n * 3. If the content parses as JSON5 → `json5`.\n * 4. Otherwise throws.\n *\n * Strict JSON is tried before JSON5 so that well-formed JSON files are not unnecessarily\n * classified as JSON5.\n *\n * @param content - The full artifact text to inspect.\n * @returns The inferred format.\n * @throws `Error` when the content cannot be classified as any supported JSON format.\n */\nfunction inferFormat(content: string): JsonArtifactFormat {\n const trimmed = content.trim()\n\n // 1. Try strict JSON\n try {\n JSON.parse(trimmed)\n return 'json'\n } catch {\n // fall through\n }\n\n // 2. Try JSONL (every non-empty line is valid JSON)\n const nonEmptyLines = trimmed.split('\\n').filter((l) => l.trim().length > 0)\n if (\n nonEmptyLines.length > 0 &&\n nonEmptyLines.every((l) => {\n try {\n JSON.parse(l)\n return true\n } catch {\n return false\n }\n })\n ) {\n return 'jsonl'\n }\n\n // 3. Try JSON5\n try {\n JSON5.parse(trimmed)\n return 'json5'\n } catch {\n // fall through\n }\n\n throw new Error('Unable to infer JSON format: content is not valid JSON, JSONL, NDJSON, or JSON5')\n}\n\n/**\n * A {@link @nhtio/adk!SpooledArtifact} specialisation that adds JSON-aware read operations.\n *\n * @typeParam T - The expected shape of each parsed record. Defaults to `unknown`.\n *\n * @remarks\n * Construct with an optional `format` hint. When omitted the format is auto-detected on first\n * access by reading the full artifact and running {@link inferFormat}. Once detected (or\n * provided), the format is cached for the lifetime of the instance.\n *\n * All JSON methods are async, consistent with {@link @nhtio/adk!SpooledArtifact}.\n *\n * Path-based methods (`json_get`, `json_filter`, `json_pluck`) use\n * [JSONPath-Plus](https://github.com/JSONPath-Plus/JSONPath) expressions. Full JSONPath syntax\n * is supported, including recursive descent (`..`), filter expressions (`[?(@.age > 18)]`),\n * and union selectors.\n */\nexport class SpooledJsonArtifact<T = unknown> extends SpooledArtifact {\n #format: JsonArtifactFormat | undefined\n #parsed: T[] | undefined\n\n /**\n * @param reader - The backing store to read from.\n * @param format - Optional format hint. When omitted, the format is inferred on first access.\n */\n constructor(reader: SpoolReader, format?: JsonArtifactFormat) {\n super(reader)\n this.#format = format\n }\n\n /**\n * Returns `true` if `value` is a {@link SpooledJsonArtifact} instance.\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. Matches the pattern used by every\n * other class guard in the ADK; safe against the dual-module-copy case where two distinct\n * `SpooledJsonArtifact` classes coexist in the same realm.\n *\n * @param value - The value to test.\n * @returns `true` when `value` is a {@link SpooledJsonArtifact} instance.\n */\n public static isSpooledJsonArtifact(value: unknown): value is SpooledJsonArtifact {\n return isInstanceOf(value, 'SpooledJsonArtifact', SpooledJsonArtifact)\n }\n\n /**\n * The JSON-specific artifact-query descriptors this class adds on top of the base set.\n *\n * @remarks\n * Lists `artifact_json_type`, `artifact_json_keys`, `artifact_json_length`,\n * `artifact_json_get`, `artifact_json_filter`, `artifact_json_slice`, `artifact_json_pluck`.\n * The base seven descriptors (`artifact_head`, etc.) are NOT included here — they are\n * forged separately by {@link SpooledJsonArtifact.forgeTools}, which calls\n * `SpooledArtifact.forgeTools(ctx)` to produce the base-narrowed tools and then registers\n * its own JSON tools on the result. Downstream consumers building custom subclasses\n * should follow the same pattern: own only your own descriptors; override `forgeTools` to\n * compose with the base output.\n */\n public static toolMethods: ReadonlyArray<ToolMethodDescriptor> = Object.freeze([\n {\n name: 'artifact_json_type',\n method: 'json_type',\n description:\n 'Return the JSON format (json | json5 | jsonl | ndjson) of a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_json_keys',\n method: 'json_keys',\n description: 'Return the top-level keys of a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_json_length',\n method: 'json_length',\n description:\n 'Return the record count of a JSON artifact produced earlier in this turn (1 for json/json5; line count for jsonl/ndjson).',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_json_get',\n method: 'json_get',\n description:\n 'Evaluate a JSONPath expression against a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({\n path: validator.string().required().description(\"JSONPath expression, e.g. '$.user.name'.\"),\n }),\n },\n {\n name: 'artifact_json_filter',\n method: 'json_filter',\n description:\n 'Return records of a JSON artifact (produced earlier in this turn) matched by a JSONPath filter.',\n argsSchema: validator.object({\n path: validator\n .string()\n .required()\n .description(\"JSONPath filter expression, e.g. '$[?(@.age>18)]'.\"),\n }),\n },\n {\n name: 'artifact_json_slice',\n method: 'json_slice',\n description:\n 'Return a slice of records by index range from a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({\n start: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start index (inclusive).'),\n end: validator.number().integer().min(0).optional().description('End index (exclusive).'),\n }),\n },\n {\n name: 'artifact_json_pluck',\n method: 'json_pluck',\n description:\n 'Return all values matched by a JSONPath expression across every record of a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({\n path: validator.string().required().description(\"JSONPath expression, e.g. '$..name'.\"),\n }),\n },\n ])\n\n /**\n * Forges base-class tools plus JSON-specific tools narrowed to {@link SpooledJsonArtifact}.\n *\n * @remarks\n * Standard subclass extension pattern: call `SpooledArtifact.forgeTools(ctx)` to produce\n * the base seven `artifact_*` tools narrowed to any `SpooledArtifact` in the turn, then\n * register one `ArtifactTool` per JSON-specific descriptor narrowed to JSON artifacts.\n * Downstream consumers building their own subclasses should follow the same shape.\n */\n public static override forgeTools(ctx: DispatchContext): ToolRegistry {\n const registry = SpooledArtifact.forgeTools(ctx)\n const requires = SpooledJsonArtifact\n const compatibleIds = [...ctx.turnToolCalls]\n .filter((tc) => !tc.fromArtifactTool && isInstanceOf(tc.results, requires.name, requires))\n .map((tc) => tc.id)\n if (compatibleIds.length === 0) return registry\n\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 = (\n descriptor.argsSchema ?? validator.object<Record<string, never>>({})\n ).append({\n callId: callIdSchema,\n })\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 (\n descriptor.method === 'json_get' ||\n descriptor.method === 'json_filter' ||\n descriptor.method === 'json_pluck'\n ) {\n methodArgs.push(args.path as string)\n } else if (descriptor.method === 'json_slice') {\n methodArgs.push(args.start as number | undefined, args.end as number | undefined)\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 const serialise = descriptor.serialise ?? defaultSerialise\n return serialise(result)\n },\n })\n registry.register(tool)\n }\n return registry\n }\n\n /**\n * Resolves and caches the detected or provided format.\n */\n async #resolveFormat(): Promise<JsonArtifactFormat> {\n if (this.#format !== undefined) {\n return this.#format\n }\n const lines = await this.cat()\n this.#format = inferFormat(lines.join('\\n'))\n return this.#format\n }\n\n /**\n * Parses and caches all records from the artifact.\n *\n * @remarks\n * For `json`/`json5` format: returns a single-element array containing the parsed root value.\n * For `jsonl`/`ndjson` format: returns one element per non-empty line.\n */\n async #resolveRecords(): Promise<T[]> {\n if (this.#parsed !== undefined) {\n return this.#parsed\n }\n const format = await this.#resolveFormat()\n const lines = await this.cat()\n if (format === 'json') {\n this.#parsed = [JSON.parse(lines.join('\\n')) as T]\n } else if (format === 'json5') {\n this.#parsed = [JSON5.parse(lines.join('\\n')) as T]\n } else {\n // jsonl / ndjson\n this.#parsed = lines.filter((l) => l.trim().length > 0).map((l) => JSON.parse(l) as T)\n }\n return this.#parsed\n }\n\n /**\n * Returns the detected or provided format for this artifact.\n *\n * @returns One of `'json'`, `'json5'`, `'jsonl'`, or `'ndjson'`.\n */\n async json_type(): Promise<JsonArtifactFormat> {\n return this.#resolveFormat()\n }\n\n /**\n * Returns the top-level keys of the parsed content.\n *\n * @remarks\n * - For `json`/`json5`: returns the keys of the root object, or `undefined` when the root is\n * not a plain object (e.g. an array or scalar).\n * - For `jsonl`/`ndjson`: returns the union of keys across all records that are plain objects.\n * Duplicate keys are deduplicated.\n *\n * @returns Array of key strings, or `undefined` when no object keys are present.\n */\n async json_keys(): Promise<string[] | undefined> {\n const records = await this.#resolveRecords()\n const format = await this.#resolveFormat()\n if (format === 'json' || format === 'json5') {\n const root = records[0]\n if (isObject(root)) {\n return Object.keys(root as object)\n }\n return undefined\n }\n const keySet = new Set<string>()\n for (const record of records) {\n if (isObject(record)) {\n for (const key of Object.keys(record as object)) {\n keySet.add(key)\n }\n }\n }\n return keySet.size > 0 ? Array.from(keySet) : undefined\n }\n\n /**\n * Returns the total number of records in the artifact.\n *\n * @remarks\n * - For `json`/`json5`: always `1` (the entire artifact is a single value).\n * - For `jsonl`/`ndjson`: the number of non-empty lines.\n *\n * @returns The record count.\n */\n async json_length(): Promise<number> {\n const records = await this.#resolveRecords()\n return records.length\n }\n\n /**\n * Evaluates a JSONPath expression against the parsed content.\n *\n * @remarks\n * Uses [JSONPath-Plus](https://github.com/JSONPath-Plus/JSONPath). Full JSONPath syntax is\n * supported: recursive descent (`$..*`), filter expressions (`$[?(@.age > 18)]`), union\n * selectors, and more.\n *\n * - For `json`/`json5`: evaluates the expression against the root value.\n * - For `jsonl`/`ndjson`: evaluates the expression against each record and returns a flat\n * array of all matches across all records.\n *\n * @param path - A JSONPath expression (e.g. `'$.user.address.city'`, `'$..name'`).\n * @returns Array of matched values. Empty array when no matches are found.\n */\n async json_get(path: string): Promise<unknown[]> {\n const records = await this.#resolveRecords()\n const format = await this.#resolveFormat()\n if (format === 'json' || format === 'json5') {\n return JSONPath({ path, json: records[0] as object })\n }\n return records.flatMap((r) => JSONPath({ path, json: r as object }))\n }\n\n /**\n * Returns a slice of the parsed records by index range.\n *\n * @remarks\n * For `json`/`json5`: always returns `[root]` — the artifact is a single record so slicing is\n * not meaningful. For `jsonl`/`ndjson`: behaves like `Array.prototype.slice`.\n *\n * @param start - Start index (inclusive). Defaults to `0`.\n * @param end - End index (exclusive). Defaults to the record count.\n * @returns Array of sliced records.\n */\n async json_slice(start?: number, end?: number): Promise<T[]> {\n const records = await this.#resolveRecords()\n const format = await this.#resolveFormat()\n if (format === 'json' || format === 'json5') {\n return records\n }\n return records.slice(start, end)\n }\n\n /**\n * Returns records matched by a JSONPath filter expression.\n *\n * @remarks\n * Evaluates `path` against each record and returns those for which the expression produces at\n * least one match. For `json`/`json5`, evaluates against the root value and returns it in an\n * array if matched.\n *\n * @param path - A JSONPath expression (e.g. `'$[?(@.status === \"active\")]'`).\n * @returns Array of matching records.\n */\n async json_filter(path: string): Promise<T[]> {\n const records = await this.#resolveRecords()\n return records.filter((r) => {\n const matches = JSONPath({ path, json: r as object })\n return Array.isArray(matches) && matches.length > 0\n })\n }\n\n /**\n * Returns all values matched by a JSONPath expression across every record.\n *\n * @remarks\n * Convenience over {@link SpooledJsonArtifact.json_get} with an identical signature — use\n * whichever name better communicates intent at the call site. `json_pluck` reads well for\n * extracting a single field column; `json_get` reads well for structured queries.\n *\n * @param path - A JSONPath expression (e.g. `'$..name'`).\n * @returns Array of matched values.\n */\n async json_pluck(path: string): Promise<unknown[]> {\n return this.json_get(path)\n }\n}\n","import { remark } from 'remark'\nimport { visit } from 'unist-util-visit'\nimport { load as yamlLoad } from 'js-yaml'\nimport { validator } from '@nhtio/validation'\nimport { isInstanceOf } from '../utils/guards'\nimport { ArtifactTool } from './artifact_tool'\nimport { ToolRegistry } from './tool_registry'\nimport { default as remarkGfm } from 'remark-gfm'\nimport { toString as mdastToString } from 'mdast-util-to-string'\nimport { default as remarkFrontmatter } from 'remark-frontmatter'\nimport { SpooledArtifact, defaultSerialise } from './spooled_artifact'\nimport type { Root, Link, Image } from 'mdast'\nimport type { SpoolReader } from '../contracts/spool_reader'\nimport type { ToolMethodDescriptor } from './spooled_artifact'\nimport type { DispatchContext } from '../contracts/dispatch_context'\n\n/**\n * A single heading entry in the document's structural index.\n *\n * @remarks\n * `startLine` is the 0-based line of the heading itself. `endLine` is the 0-based index of the\n * last line belonging to this section (inclusive) — the line immediately before the next heading\n * of equal or lesser depth, or the last line of the document.\n */\nexport interface MarkdownHeadingEntry {\n /** ATX heading depth: 1 (`#`) through 6 (`######`). */\n depth: 1 | 2 | 3 | 4 | 5 | 6\n /** The heading text with the leading `#` prefix stripped and trimmed. */\n text: string\n /** 0-based line index of the heading line itself. */\n startLine: number\n /** 0-based line index of the last line in this section (inclusive). */\n endLine: number\n}\n\n/**\n * A single fenced code block entry in the document's structural index.\n */\nexport interface MarkdownCodeEntry {\n /** The language identifier immediately after the opening fence, or `null` when absent. */\n lang: string | null\n /** 0-based line index of the opening fence line. */\n startLine: number\n /** 0-based line index of the closing fence line. */\n endLine: number\n}\n\ninterface MarkdownIndex {\n headings: MarkdownHeadingEntry[]\n codeBlocks: MarkdownCodeEntry[]\n}\n\n/**\n * A section of a markdown document as returned by {@link SpooledMarkdownArtifact.md_sections}.\n *\n * @remarks\n * Contains only line-range metadata — no content is fetched until the caller explicitly\n * requests it via `cat(bodyStartLine, bodyEndLine + 1)`.\n */\nexport interface MarkdownSection {\n depth: 1 | 2 | 3 | 4 | 5 | 6\n /** The heading text. */\n heading: string\n /** 0-based line of the heading itself. */\n headingLine: number\n /** 0-based line of the first body line (heading line + 1). */\n bodyStartLine: number\n /** 0-based line of the last body line (inclusive). */\n bodyEndLine: number\n}\n\n/**\n * Returns a configured remark processor with frontmatter and GFM support.\n */\nfunction processor() {\n return remark().use(remarkFrontmatter).use(remarkGfm)\n}\n\n/**\n * Parses a heading line (e.g. `## My Heading`) and returns `{ depth, text }`, or `null` when\n * the line is not an ATX heading.\n */\nfunction parseHeadingLine(line: string): { depth: 1 | 2 | 3 | 4 | 5 | 6; text: string } | null {\n const match = /^(#{1,6})\\s+(.*)$/.exec(line)\n if (!match) return null\n const depth = match[1].length as 1 | 2 | 3 | 4 | 5 | 6\n return { depth, text: match[2].trim() }\n}\n\n/**\n * Returns the length of a fence marker at the start of `line` (3+), or `0` if the line is not\n * a fence opener/closer. Handles both backtick (` ``` `) and tilde (`~~~`) fences.\n */\nfunction fenceLength(line: string): number {\n const trimmed = line.trimStart()\n const match = /^(`{3,}|~{3,})/.exec(trimmed)\n return match ? match[1].length : 0\n}\n\n/**\n * Extracts the language identifier from a fence opener line (e.g. ` ```ts ` → `'ts'`), or\n * `null` when none is present.\n */\nfunction fenceLang(line: string): string | null {\n const trimmed = line.trimStart()\n const match = /^(?:`{3,}|~{3,})(\\S+)/.exec(trimmed)\n return match ? match[1] : null\n}\n\n/**\n * A {@link @nhtio/adk!SpooledArtifact} specialisation that adds markdown-aware structural queries.\n *\n * @remarks\n * Designed for large markdown documents where loading the full content into memory is\n * impractical. The structural index (heading positions, code block positions) is built by a\n * single line-by-line scan of the {@link @nhtio/adk!SpoolReader} without retaining any content. Only the\n * tiny metadata index and the parsed frontmatter object are cached.\n *\n * Content retrieval is always bounded — use `cat(start, end)` or the `startLine`/`endLine`\n * parameters on inline methods to fetch only the lines you need.\n *\n * Inline methods (`md_links`, `md_images`, `md_text`, `md_ast`) accept optional line-range\n * arguments. Without a range they read the full document — documented trade-off, caller\n * responsibility to bound the range for large documents.\n *\n * The processor always applies `remark-gfm` (tables, task lists, strikethrough, autolinks)\n * in addition to standard CommonMark and YAML frontmatter.\n */\nexport class SpooledMarkdownArtifact extends SpooledArtifact {\n #index: MarkdownIndex | undefined\n #frontmatter: Record<string, unknown> | null | undefined\n\n /**\n * @param reader - The backing store to read from.\n */\n constructor(reader: SpoolReader) {\n super(reader)\n }\n\n /**\n * Returns `true` if `value` is a {@link SpooledMarkdownArtifact} instance.\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. Matches the pattern used by every\n * other class guard in the ADK; safe against the dual-module-copy case where two distinct\n * `SpooledMarkdownArtifact` classes coexist in the same realm.\n */\n public static isSpooledMarkdownArtifact(value: unknown): value is SpooledMarkdownArtifact {\n return isInstanceOf(value, 'SpooledMarkdownArtifact', SpooledMarkdownArtifact)\n }\n\n /**\n * The markdown-specific artifact-query descriptors this class adds on top of the base set.\n *\n * @remarks\n * Lists `artifact_md_frontmatter`, `artifact_md_headings`, `artifact_md_code_blocks`,\n * `artifact_md_sections`, `artifact_md_links`, `artifact_md_images`, `artifact_md_text`,\n * `artifact_md_ast`. The base seven descriptors (`artifact_head`, etc.) are NOT included\n * here — they are forged separately by {@link SpooledMarkdownArtifact.forgeTools}, which\n * calls `SpooledArtifact.forgeTools(ctx)` to produce the base-narrowed tools and then\n * registers its own markdown tools on the result. Downstream consumers building custom\n * subclasses should follow the same pattern: own only your own descriptors; override\n * `forgeTools` to compose with the base output.\n */\n public static toolMethods: ReadonlyArray<ToolMethodDescriptor> = Object.freeze([\n {\n name: 'artifact_md_frontmatter',\n method: 'md_frontmatter',\n description:\n 'Return parsed YAML frontmatter (or undefined) from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_md_headings',\n method: 'md_headings',\n description:\n 'Return all headings, optionally filtered by depth, from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n depth: validator\n .number()\n .integer()\n .min(1)\n .max(6)\n .optional()\n .description('ATX heading depth (1-6).'),\n }),\n },\n {\n name: 'artifact_md_code_blocks',\n method: 'md_code_blocks',\n description:\n 'Return all fenced code block entries, optionally filtered by language, from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n lang: validator\n .string()\n .optional()\n .description('Language identifier. Pass empty string to match blocks with no lang.'),\n }),\n },\n {\n name: 'artifact_md_sections',\n method: 'md_sections',\n description:\n 'Return document sections (line-range metadata only) from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n depth: validator\n .number()\n .integer()\n .min(1)\n .max(6)\n .optional()\n .description('ATX heading depth (1-6).'),\n }),\n },\n {\n name: 'artifact_md_links',\n method: 'md_links',\n description:\n 'Return all inline and reference links within the given line range from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n {\n name: 'artifact_md_images',\n method: 'md_images',\n description:\n 'Return all images within the given line range from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n {\n name: 'artifact_md_text',\n method: 'md_text',\n description:\n 'Return plain text with markup stripped, for the given line range, from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n {\n name: 'artifact_md_ast',\n method: 'md_ast',\n description:\n 'Return the full MDAST Root for the specified line range from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n ])\n\n /**\n * Forges base-class tools plus markdown-specific tools narrowed to\n * {@link SpooledMarkdownArtifact}.\n *\n * @remarks\n * Standard subclass extension pattern: call `SpooledArtifact.forgeTools(ctx)` to produce\n * the base seven `artifact_*` tools narrowed to any `SpooledArtifact` in the turn, then\n * register one `ArtifactTool` per markdown-specific descriptor narrowed to markdown\n * artifacts. Downstream consumers building their own subclasses should follow the same\n * shape.\n */\n public static override forgeTools(ctx: DispatchContext): ToolRegistry {\n const registry = SpooledArtifact.forgeTools(ctx)\n const requires = SpooledMarkdownArtifact\n const compatibleIds = [...ctx.turnToolCalls]\n .filter((tc) => !tc.fromArtifactTool && isInstanceOf(tc.results, requires.name, requires))\n .map((tc) => tc.id)\n if (compatibleIds.length === 0) return registry\n\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 = (\n descriptor.argsSchema ?? validator.object<Record<string, never>>({})\n ).append({\n callId: callIdSchema,\n })\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 === 'md_headings' || descriptor.method === 'md_sections') {\n methodArgs.push(args.depth as number | undefined)\n } else if (descriptor.method === 'md_code_blocks') {\n methodArgs.push(args.lang as string | undefined)\n } else if (\n descriptor.method === 'md_links' ||\n descriptor.method === 'md_images' ||\n descriptor.method === 'md_text' ||\n descriptor.method === 'md_ast'\n ) {\n methodArgs.push(\n args.startLine as number | undefined,\n args.endLine as number | undefined\n )\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 const serialise = descriptor.serialise ?? defaultSerialise\n return serialise(result)\n },\n })\n registry.register(tool)\n }\n return registry\n }\n\n /**\n * Builds the structural index in a single top-to-bottom pass, retaining only metadata.\n */\n async #resolveIndex(): Promise<MarkdownIndex> {\n if (this.#index !== undefined) return this.#index\n\n const count = await this.lineCount()\n const headingsRaw: Array<{ depth: 1 | 2 | 3 | 4 | 5 | 6; text: string; startLine: number }> = []\n const codeBlocks: MarkdownCodeEntry[] = []\n\n let inFrontmatter = false\n let frontmatterClosed = false\n let inCodeBlock = false\n let openFenceLen = 0\n let openFenceStartLine = 0\n let openFenceLang: string | null = null\n\n for (let i = 0; i < count; i++) {\n const rawLine = await this.line(i)\n const l = rawLine ?? ''\n\n // Handle frontmatter block at the top of the document\n if (i === 0 && l.trim() === '---') {\n inFrontmatter = true\n continue\n }\n if (inFrontmatter) {\n if (l.trim() === '---') {\n inFrontmatter = false\n frontmatterClosed = true\n }\n continue\n }\n if (!frontmatterClosed && i === 0) {\n // No frontmatter — treat as normal document from the start\n }\n\n // Handle fenced code blocks\n const fLen = fenceLength(l)\n if (!inCodeBlock && fLen >= 3) {\n inCodeBlock = true\n openFenceLen = fLen\n openFenceStartLine = i\n openFenceLang = fenceLang(l)\n continue\n }\n if (inCodeBlock) {\n // A closing fence must use the same fence character (` or ~) and be at least as long.\n if (fLen >= openFenceLen) {\n const openLine = (await this.line(openFenceStartLine)) ?? ''\n const openChar = openLine.trimStart()[0]\n const closeChar = l.trimStart()[0]\n if (closeChar === openChar) {\n codeBlocks.push({ lang: openFenceLang, startLine: openFenceStartLine, endLine: i })\n inCodeBlock = false\n openFenceLen = 0\n }\n }\n continue\n }\n\n // Detect ATX headings (not inside code blocks, not in frontmatter)\n const heading = parseHeadingLine(l)\n if (heading) {\n headingsRaw.push({ ...heading, startLine: i })\n }\n }\n\n // Unclosed code block — record it anyway\n if (inCodeBlock) {\n codeBlocks.push({ lang: openFenceLang, startLine: openFenceStartLine, endLine: count - 1 })\n }\n\n // Post-process heading endLine values\n const headings: MarkdownHeadingEntry[] = headingsRaw.map((h, idx) => {\n const nextBoundary = headingsRaw.slice(idx + 1).find((n) => n.depth <= h.depth)\n const endLine = nextBoundary ? nextBoundary.startLine - 1 : count - 1\n return { ...h, endLine }\n })\n\n this.#index = { headings, codeBlocks }\n return this.#index\n }\n\n // ── Frontmatter ───────────────────────────────────────────────────────────\n\n /**\n * Returns the parsed YAML frontmatter, or `undefined` when no frontmatter block is present.\n *\n * @remarks\n * Short-circuits after reading the frontmatter block — never reads the document body. Caches\n * the result so subsequent calls are free. The result is `undefined` (not an empty object)\n * when no frontmatter is found, distinguishing \"no frontmatter\" from \"empty frontmatter\".\n */\n async md_frontmatter(): Promise<Record<string, unknown> | undefined> {\n if (this.#frontmatter !== undefined) return this.#frontmatter ?? undefined\n\n const firstLine = await this.line(0)\n if (firstLine?.trim() !== '---') {\n this.#frontmatter = null\n return undefined\n }\n\n const maxScan = Math.min(await this.lineCount(), 200)\n const yamlLines: string[] = []\n let closed = false\n for (let i = 1; i < maxScan; i++) {\n const l = await this.line(i)\n if (l?.trim() === '---') {\n closed = true\n break\n }\n yamlLines.push(l ?? '')\n }\n\n if (!closed) {\n this.#frontmatter = null\n return undefined\n }\n\n this.#frontmatter = yamlLoad(yamlLines.join('\\n')) as Record<string, unknown>\n return this.#frontmatter\n }\n\n // ── Structural index queries ──────────────────────────────────────────────\n\n /**\n * Returns all headings in document order, optionally filtered by depth.\n *\n * @remarks\n * Uses the cached structural index — no content is fetched from the {@link @nhtio/adk!SpoolReader}.\n *\n * @param depth - When provided, only headings at this ATX depth (1–6) are returned.\n */\n async md_headings(depth?: 1 | 2 | 3 | 4 | 5 | 6): Promise<MarkdownHeadingEntry[]> {\n const index = await this.#resolveIndex()\n if (depth === undefined) return index.headings.slice()\n return index.headings.filter((h) => h.depth === depth)\n }\n\n /**\n * Returns all fenced code block entries, optionally filtered by language identifier.\n *\n * @remarks\n * Returns line-range metadata only — no content is fetched. Use `cat(entry.startLine + 1,\n * entry.endLine)` to retrieve the code body (excluding fence lines).\n *\n * @param lang - When provided, only blocks with this exact lang identifier are returned.\n * Pass an empty string to match blocks with no lang identifier.\n */\n async md_code_blocks(lang?: string): Promise<MarkdownCodeEntry[]> {\n const index = await this.#resolveIndex()\n if (lang === undefined) return index.codeBlocks.slice()\n const target = lang === '' ? null : lang\n return index.codeBlocks.filter((b) => b.lang === target)\n }\n\n /**\n * Returns document sections derived from the structural index.\n *\n * @remarks\n * Returns only line-range metadata — body content is never fetched. To retrieve the body of a\n * section, call `cat(section.bodyStartLine, section.bodyEndLine + 1)`.\n *\n * When `depth` is provided, only sections introduced by a heading at that depth are returned;\n * deeper headings become part of the body.\n *\n * @param depth - When provided, only sections at this ATX depth (1–6) are returned.\n */\n async md_sections(depth?: 1 | 2 | 3 | 4 | 5 | 6): Promise<MarkdownSection[]> {\n const index = await this.#resolveIndex()\n const headings =\n depth !== undefined ? index.headings.filter((h) => h.depth === depth) : index.headings\n return headings.map((h) => ({\n depth: h.depth,\n heading: h.text,\n headingLine: h.startLine,\n bodyStartLine: h.startLine + 1,\n bodyEndLine: h.endLine,\n }))\n }\n\n // ── Inline queries (bounded) ──────────────────────────────────────────────\n\n /**\n * Returns the full MDAST Root for the specified line range.\n *\n * @remarks\n * Without a range, reads the full document — for large documents, use\n * {@link SpooledMarkdownArtifact.md_sections} to locate sections and pass\n * bounded line ranges here.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_ast(startLine?: number, endLine?: number): Promise<Root> {\n const lines = await this.cat(startLine, endLine)\n return processor().parse(lines.join('\\n')) as Root\n }\n\n /**\n * Returns all inline and reference links in the specified line range.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_links(\n startLine?: number,\n endLine?: number\n ): Promise<Array<{ text: string; url: string; title?: string }>> {\n const lines = await this.cat(startLine, endLine)\n const ast = processor().parse(lines.join('\\n'))\n const results: Array<{ text: string; url: string; title?: string }> = []\n visit(ast, 'link', (node: Link) => {\n results.push({\n text: mdastToString(node),\n url: node.url,\n title: node.title ?? undefined,\n })\n })\n return results\n }\n\n /**\n * Returns all images in the specified line range.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_images(\n startLine?: number,\n endLine?: number\n ): Promise<Array<{ alt: string; url: string; title?: string }>> {\n const lines = await this.cat(startLine, endLine)\n const ast = processor().parse(lines.join('\\n'))\n const results: Array<{ alt: string; url: string; title?: string }> = []\n visit(ast, 'image', (node: Image) => {\n results.push({\n alt: node.alt ?? '',\n url: node.url,\n title: node.title ?? undefined,\n })\n })\n return results\n }\n\n /**\n * Returns all document text with markup stripped, for the specified line range.\n *\n * @remarks\n * Uses `mdast-util-to-string` to extract plain text from the AST — code, link text, and\n * alt text are included; markdown syntax is removed.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_text(startLine?: number, endLine?: number): Promise<string> {\n const lines = await this.cat(startLine, endLine)\n const ast = processor().parse(lines.join('\\n'))\n return mdastToString(ast)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCA,SAAS,YAAY,SAAqC;CACxD,MAAM,UAAU,QAAQ,KAAK;CAG7B,IAAI;EACF,KAAK,MAAM,OAAO;EAClB,OAAO;CACT,QAAQ,CAER;CAGA,MAAM,gBAAgB,QAAQ,MAAM,IAAI,EAAE,QAAQ,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC;CAC3E,IACE,cAAc,SAAS,KACvB,cAAc,OAAO,MAAM;EACzB,IAAI;GACF,KAAK,MAAM,CAAC;GACZ,OAAO;EACT,QAAQ;GACN,OAAO;EACT;CACF,CAAC,GAED,OAAO;CAIT,IAAI;EACF,MAAM,MAAM,OAAO;EACnB,OAAO;CACT,QAAQ,CAER;CAEA,MAAM,IAAI,MAAM,iFAAiF;AACnG;;;;;;;;;;;;;;;;;;AAmBA,IAAa,sBAAb,MAAa,4BAAyC,gBAAgB;CACpE;CACA;;;;;CAMA,YAAY,QAAqB,QAA6B;EAC5D,MAAM,MAAM;EAC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,SAAiC;EAChE,MAAM,QAAQ,MAAM,KAAK,IAAI,WAAW,OAAO;EAC/C,OAAO,UAAU,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;CAC3C;;;;;;;CAQA,MAAM,SACJ,WACA,SAC+D;EAC/D,MAAM,QAAQ,MAAM,KAAK,IAAI,WAAW,OAAO;EAC/C,MAAM,MAAM,UAAU,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;EAC9C,MAAM,UAAgE,CAAC;EACvE,MAAM,KAAK,SAAS,SAAe;GACjC,QAAQ,KAAK;IACX,MAAM,SAAc,IAAI;IACxB,KAAK,KAAK;IACV,OAAO,KAAK,SAAS,KAAA;GACvB,CAAC;EACH,CAAC;EACD,OAAO;CACT;;;;;;;CAQA,MAAM,UACJ,WACA,SAC8D;EAC9D,MAAM,QAAQ,MAAM,KAAK,IAAI,WAAW,OAAO;EAC/C,MAAM,MAAM,UAAU,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;EAC9C,MAAM,UAA+D,CAAC;EACtE,MAAM,KAAK,UAAU,SAAgB;GACnC,QAAQ,KAAK;IACX,KAAK,KAAK,OAAO;IACjB,KAAK,KAAK;IACV,OAAO,KAAK,SAAS,KAAA;GACvB,CAAC;EACH,CAAC;EACD,OAAO;CACT;;;;;;;;;;;CAYA,MAAM,QAAQ,WAAoB,SAAmC;EACnE,MAAM,QAAQ,MAAM,KAAK,IAAI,WAAW,OAAO;EAE/C,OAAO,SADK,UAAU,EAAE,MAAM,MAAM,KAAK,IAAI,CACxB,CAAG;CAC1B;AACF"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const require_chunk = require("./chunk-Ble4zEEl.js");
|
|
2
|
-
const require_tool_registry = require("./tool_registry-
|
|
3
|
-
const require_spooled_artifact = require("./spooled_artifact-
|
|
2
|
+
const require_tool_registry = require("./tool_registry-CtCQ4Xoz.js");
|
|
3
|
+
const require_spooled_artifact = require("./spooled_artifact-B8gIIn9h.js");
|
|
4
4
|
let _nhtio_validation = require("@nhtio/validation");
|
|
5
5
|
let json5 = require("json5");
|
|
6
6
|
json5 = require_chunk.__toESM(json5);
|
|
@@ -783,4 +783,4 @@ Object.defineProperty(exports, "SpooledMarkdownArtifact", {
|
|
|
783
783
|
}
|
|
784
784
|
});
|
|
785
785
|
|
|
786
|
-
//# sourceMappingURL=spooled_markdown_artifact-
|
|
786
|
+
//# sourceMappingURL=spooled_markdown_artifact-DQX0RCdI.js.map
|
package/{spooled_markdown_artifact-BYfPqFvk.js.map → spooled_markdown_artifact-DQX0RCdI.js.map}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spooled_markdown_artifact-BYfPqFvk.js","names":["#format","#resolveFormat","#resolveRecords","#parsed","#resolveIndex","#index","#frontmatter"],"sources":["../src/lib/classes/spooled_json_artifact.ts","../src/lib/classes/spooled_markdown_artifact.ts"],"sourcesContent":["import { default as JSON5 } from 'json5'\nimport { JSONPath } from 'jsonpath-plus'\nimport { validator } from '@nhtio/validation'\nimport { ArtifactTool } from './artifact_tool'\nimport { ToolRegistry } from './tool_registry'\nimport { isInstanceOf, isObject } from '../utils/guards'\nimport { SpooledArtifact, defaultSerialise } from './spooled_artifact'\nimport type { SpoolReader } from '../contracts/spool_reader'\nimport type { ToolMethodDescriptor } from './spooled_artifact'\nimport type { DispatchContext } from '../contracts/dispatch_context'\n\n/**\n * The set of JSON-derived formats that {@link SpooledJsonArtifact} can handle.\n *\n * @remarks\n * - `json` — a single JSON value spanning the entire artifact (strict RFC 8259).\n * - `json5` — a single JSON5 value spanning the entire artifact; permits comments, trailing\n * commas, unquoted keys, and other relaxed syntax via the `json5` package.\n * - `jsonl` — newline-delimited JSON; each non-empty line is an independent JSON value.\n * - `ndjson` — alias for `jsonl`; both names are accepted and behave identically.\n */\nexport type JsonArtifactFormat = 'json' | 'json5' | 'jsonl' | 'ndjson'\n\n/**\n * Detects the {@link JsonArtifactFormat} of a raw string.\n *\n * @remarks\n * Detection strategy (in order):\n * 1. If the content parses as strict JSON → `json`.\n * 2. If every non-empty line parses as strict JSON → `jsonl`.\n * 3. If the content parses as JSON5 → `json5`.\n * 4. Otherwise throws.\n *\n * Strict JSON is tried before JSON5 so that well-formed JSON files are not unnecessarily\n * classified as JSON5.\n *\n * @param content - The full artifact text to inspect.\n * @returns The inferred format.\n * @throws `Error` when the content cannot be classified as any supported JSON format.\n */\nfunction inferFormat(content: string): JsonArtifactFormat {\n const trimmed = content.trim()\n\n // 1. Try strict JSON\n try {\n JSON.parse(trimmed)\n return 'json'\n } catch {\n // fall through\n }\n\n // 2. Try JSONL (every non-empty line is valid JSON)\n const nonEmptyLines = trimmed.split('\\n').filter((l) => l.trim().length > 0)\n if (\n nonEmptyLines.length > 0 &&\n nonEmptyLines.every((l) => {\n try {\n JSON.parse(l)\n return true\n } catch {\n return false\n }\n })\n ) {\n return 'jsonl'\n }\n\n // 3. Try JSON5\n try {\n JSON5.parse(trimmed)\n return 'json5'\n } catch {\n // fall through\n }\n\n throw new Error('Unable to infer JSON format: content is not valid JSON, JSONL, NDJSON, or JSON5')\n}\n\n/**\n * A {@link @nhtio/adk!SpooledArtifact} specialisation that adds JSON-aware read operations.\n *\n * @typeParam T - The expected shape of each parsed record. Defaults to `unknown`.\n *\n * @remarks\n * Construct with an optional `format` hint. When omitted the format is auto-detected on first\n * access by reading the full artifact and running {@link inferFormat}. Once detected (or\n * provided), the format is cached for the lifetime of the instance.\n *\n * All JSON methods are async, consistent with {@link @nhtio/adk!SpooledArtifact}.\n *\n * Path-based methods (`json_get`, `json_filter`, `json_pluck`) use\n * [JSONPath-Plus](https://github.com/JSONPath-Plus/JSONPath) expressions. Full JSONPath syntax\n * is supported, including recursive descent (`..`), filter expressions (`[?(@.age > 18)]`),\n * and union selectors.\n */\nexport class SpooledJsonArtifact<T = unknown> extends SpooledArtifact {\n #format: JsonArtifactFormat | undefined\n #parsed: T[] | undefined\n\n /**\n * @param reader - The backing store to read from.\n * @param format - Optional format hint. When omitted, the format is inferred on first access.\n */\n constructor(reader: SpoolReader, format?: JsonArtifactFormat) {\n super(reader)\n this.#format = format\n }\n\n /**\n * Returns `true` if `value` is a {@link SpooledJsonArtifact} instance.\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. Matches the pattern used by every\n * other class guard in the ADK; safe against the dual-module-copy case where two distinct\n * `SpooledJsonArtifact` classes coexist in the same realm.\n *\n * @param value - The value to test.\n * @returns `true` when `value` is a {@link SpooledJsonArtifact} instance.\n */\n public static isSpooledJsonArtifact(value: unknown): value is SpooledJsonArtifact {\n return isInstanceOf(value, 'SpooledJsonArtifact', SpooledJsonArtifact)\n }\n\n /**\n * The JSON-specific artifact-query descriptors this class adds on top of the base set.\n *\n * @remarks\n * Lists `artifact_json_type`, `artifact_json_keys`, `artifact_json_length`,\n * `artifact_json_get`, `artifact_json_filter`, `artifact_json_slice`, `artifact_json_pluck`.\n * The base seven descriptors (`artifact_head`, etc.) are NOT included here — they are\n * forged separately by {@link SpooledJsonArtifact.forgeTools}, which calls\n * `SpooledArtifact.forgeTools(ctx)` to produce the base-narrowed tools and then registers\n * its own JSON tools on the result. Downstream consumers building custom subclasses\n * should follow the same pattern: own only your own descriptors; override `forgeTools` to\n * compose with the base output.\n */\n public static toolMethods: ReadonlyArray<ToolMethodDescriptor> = Object.freeze([\n {\n name: 'artifact_json_type',\n method: 'json_type',\n description:\n 'Return the JSON format (json | json5 | jsonl | ndjson) of a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_json_keys',\n method: 'json_keys',\n description: 'Return the top-level keys of a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_json_length',\n method: 'json_length',\n description:\n 'Return the record count of a JSON artifact produced earlier in this turn (1 for json/json5; line count for jsonl/ndjson).',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_json_get',\n method: 'json_get',\n description:\n 'Evaluate a JSONPath expression against a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({\n path: validator.string().required().description(\"JSONPath expression, e.g. '$.user.name'.\"),\n }),\n },\n {\n name: 'artifact_json_filter',\n method: 'json_filter',\n description:\n 'Return records of a JSON artifact (produced earlier in this turn) matched by a JSONPath filter.',\n argsSchema: validator.object({\n path: validator\n .string()\n .required()\n .description(\"JSONPath filter expression, e.g. '$[?(@.age>18)]'.\"),\n }),\n },\n {\n name: 'artifact_json_slice',\n method: 'json_slice',\n description:\n 'Return a slice of records by index range from a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({\n start: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start index (inclusive).'),\n end: validator.number().integer().min(0).optional().description('End index (exclusive).'),\n }),\n },\n {\n name: 'artifact_json_pluck',\n method: 'json_pluck',\n description:\n 'Return all values matched by a JSONPath expression across every record of a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({\n path: validator.string().required().description(\"JSONPath expression, e.g. '$..name'.\"),\n }),\n },\n ])\n\n /**\n * Forges base-class tools plus JSON-specific tools narrowed to {@link SpooledJsonArtifact}.\n *\n * @remarks\n * Standard subclass extension pattern: call `SpooledArtifact.forgeTools(ctx)` to produce\n * the base seven `artifact_*` tools narrowed to any `SpooledArtifact` in the turn, then\n * register one `ArtifactTool` per JSON-specific descriptor narrowed to JSON artifacts.\n * Downstream consumers building their own subclasses should follow the same shape.\n */\n public static override forgeTools(ctx: DispatchContext): ToolRegistry {\n const registry = SpooledArtifact.forgeTools(ctx)\n const requires = SpooledJsonArtifact\n const compatibleIds = [...ctx.turnToolCalls]\n .filter((tc) => !tc.fromArtifactTool && isInstanceOf(tc.results, requires.name, requires))\n .map((tc) => tc.id)\n if (compatibleIds.length === 0) return registry\n\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 = (\n descriptor.argsSchema ?? validator.object<Record<string, never>>({})\n ).append({\n callId: callIdSchema,\n })\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 (\n descriptor.method === 'json_get' ||\n descriptor.method === 'json_filter' ||\n descriptor.method === 'json_pluck'\n ) {\n methodArgs.push(args.path as string)\n } else if (descriptor.method === 'json_slice') {\n methodArgs.push(args.start as number | undefined, args.end as number | undefined)\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 const serialise = descriptor.serialise ?? defaultSerialise\n return serialise(result)\n },\n })\n registry.register(tool)\n }\n return registry\n }\n\n /**\n * Resolves and caches the detected or provided format.\n */\n async #resolveFormat(): Promise<JsonArtifactFormat> {\n if (this.#format !== undefined) {\n return this.#format\n }\n const lines = await this.cat()\n this.#format = inferFormat(lines.join('\\n'))\n return this.#format\n }\n\n /**\n * Parses and caches all records from the artifact.\n *\n * @remarks\n * For `json`/`json5` format: returns a single-element array containing the parsed root value.\n * For `jsonl`/`ndjson` format: returns one element per non-empty line.\n */\n async #resolveRecords(): Promise<T[]> {\n if (this.#parsed !== undefined) {\n return this.#parsed\n }\n const format = await this.#resolveFormat()\n const lines = await this.cat()\n if (format === 'json') {\n this.#parsed = [JSON.parse(lines.join('\\n')) as T]\n } else if (format === 'json5') {\n this.#parsed = [JSON5.parse(lines.join('\\n')) as T]\n } else {\n // jsonl / ndjson\n this.#parsed = lines.filter((l) => l.trim().length > 0).map((l) => JSON.parse(l) as T)\n }\n return this.#parsed\n }\n\n /**\n * Returns the detected or provided format for this artifact.\n *\n * @returns One of `'json'`, `'json5'`, `'jsonl'`, or `'ndjson'`.\n */\n async json_type(): Promise<JsonArtifactFormat> {\n return this.#resolveFormat()\n }\n\n /**\n * Returns the top-level keys of the parsed content.\n *\n * @remarks\n * - For `json`/`json5`: returns the keys of the root object, or `undefined` when the root is\n * not a plain object (e.g. an array or scalar).\n * - For `jsonl`/`ndjson`: returns the union of keys across all records that are plain objects.\n * Duplicate keys are deduplicated.\n *\n * @returns Array of key strings, or `undefined` when no object keys are present.\n */\n async json_keys(): Promise<string[] | undefined> {\n const records = await this.#resolveRecords()\n const format = await this.#resolveFormat()\n if (format === 'json' || format === 'json5') {\n const root = records[0]\n if (isObject(root)) {\n return Object.keys(root as object)\n }\n return undefined\n }\n const keySet = new Set<string>()\n for (const record of records) {\n if (isObject(record)) {\n for (const key of Object.keys(record as object)) {\n keySet.add(key)\n }\n }\n }\n return keySet.size > 0 ? Array.from(keySet) : undefined\n }\n\n /**\n * Returns the total number of records in the artifact.\n *\n * @remarks\n * - For `json`/`json5`: always `1` (the entire artifact is a single value).\n * - For `jsonl`/`ndjson`: the number of non-empty lines.\n *\n * @returns The record count.\n */\n async json_length(): Promise<number> {\n const records = await this.#resolveRecords()\n return records.length\n }\n\n /**\n * Evaluates a JSONPath expression against the parsed content.\n *\n * @remarks\n * Uses [JSONPath-Plus](https://github.com/JSONPath-Plus/JSONPath). Full JSONPath syntax is\n * supported: recursive descent (`$..*`), filter expressions (`$[?(@.age > 18)]`), union\n * selectors, and more.\n *\n * - For `json`/`json5`: evaluates the expression against the root value.\n * - For `jsonl`/`ndjson`: evaluates the expression against each record and returns a flat\n * array of all matches across all records.\n *\n * @param path - A JSONPath expression (e.g. `'$.user.address.city'`, `'$..name'`).\n * @returns Array of matched values. Empty array when no matches are found.\n */\n async json_get(path: string): Promise<unknown[]> {\n const records = await this.#resolveRecords()\n const format = await this.#resolveFormat()\n if (format === 'json' || format === 'json5') {\n return JSONPath({ path, json: records[0] as object })\n }\n return records.flatMap((r) => JSONPath({ path, json: r as object }))\n }\n\n /**\n * Returns a slice of the parsed records by index range.\n *\n * @remarks\n * For `json`/`json5`: always returns `[root]` — the artifact is a single record so slicing is\n * not meaningful. For `jsonl`/`ndjson`: behaves like `Array.prototype.slice`.\n *\n * @param start - Start index (inclusive). Defaults to `0`.\n * @param end - End index (exclusive). Defaults to the record count.\n * @returns Array of sliced records.\n */\n async json_slice(start?: number, end?: number): Promise<T[]> {\n const records = await this.#resolveRecords()\n const format = await this.#resolveFormat()\n if (format === 'json' || format === 'json5') {\n return records\n }\n return records.slice(start, end)\n }\n\n /**\n * Returns records matched by a JSONPath filter expression.\n *\n * @remarks\n * Evaluates `path` against each record and returns those for which the expression produces at\n * least one match. For `json`/`json5`, evaluates against the root value and returns it in an\n * array if matched.\n *\n * @param path - A JSONPath expression (e.g. `'$[?(@.status === \"active\")]'`).\n * @returns Array of matching records.\n */\n async json_filter(path: string): Promise<T[]> {\n const records = await this.#resolveRecords()\n return records.filter((r) => {\n const matches = JSONPath({ path, json: r as object })\n return Array.isArray(matches) && matches.length > 0\n })\n }\n\n /**\n * Returns all values matched by a JSONPath expression across every record.\n *\n * @remarks\n * Convenience over {@link SpooledJsonArtifact.json_get} with an identical signature — use\n * whichever name better communicates intent at the call site. `json_pluck` reads well for\n * extracting a single field column; `json_get` reads well for structured queries.\n *\n * @param path - A JSONPath expression (e.g. `'$..name'`).\n * @returns Array of matched values.\n */\n async json_pluck(path: string): Promise<unknown[]> {\n return this.json_get(path)\n }\n}\n","import { remark } from 'remark'\nimport { visit } from 'unist-util-visit'\nimport { load as yamlLoad } from 'js-yaml'\nimport { validator } from '@nhtio/validation'\nimport { isInstanceOf } from '../utils/guards'\nimport { ArtifactTool } from './artifact_tool'\nimport { ToolRegistry } from './tool_registry'\nimport { default as remarkGfm } from 'remark-gfm'\nimport { toString as mdastToString } from 'mdast-util-to-string'\nimport { default as remarkFrontmatter } from 'remark-frontmatter'\nimport { SpooledArtifact, defaultSerialise } from './spooled_artifact'\nimport type { Root, Link, Image } from 'mdast'\nimport type { SpoolReader } from '../contracts/spool_reader'\nimport type { ToolMethodDescriptor } from './spooled_artifact'\nimport type { DispatchContext } from '../contracts/dispatch_context'\n\n/**\n * A single heading entry in the document's structural index.\n *\n * @remarks\n * `startLine` is the 0-based line of the heading itself. `endLine` is the 0-based index of the\n * last line belonging to this section (inclusive) — the line immediately before the next heading\n * of equal or lesser depth, or the last line of the document.\n */\nexport interface MarkdownHeadingEntry {\n /** ATX heading depth: 1 (`#`) through 6 (`######`). */\n depth: 1 | 2 | 3 | 4 | 5 | 6\n /** The heading text with the leading `#` prefix stripped and trimmed. */\n text: string\n /** 0-based line index of the heading line itself. */\n startLine: number\n /** 0-based line index of the last line in this section (inclusive). */\n endLine: number\n}\n\n/**\n * A single fenced code block entry in the document's structural index.\n */\nexport interface MarkdownCodeEntry {\n /** The language identifier immediately after the opening fence, or `null` when absent. */\n lang: string | null\n /** 0-based line index of the opening fence line. */\n startLine: number\n /** 0-based line index of the closing fence line. */\n endLine: number\n}\n\ninterface MarkdownIndex {\n headings: MarkdownHeadingEntry[]\n codeBlocks: MarkdownCodeEntry[]\n}\n\n/**\n * A section of a markdown document as returned by {@link SpooledMarkdownArtifact.md_sections}.\n *\n * @remarks\n * Contains only line-range metadata — no content is fetched until the caller explicitly\n * requests it via `cat(bodyStartLine, bodyEndLine + 1)`.\n */\nexport interface MarkdownSection {\n depth: 1 | 2 | 3 | 4 | 5 | 6\n /** The heading text. */\n heading: string\n /** 0-based line of the heading itself. */\n headingLine: number\n /** 0-based line of the first body line (heading line + 1). */\n bodyStartLine: number\n /** 0-based line of the last body line (inclusive). */\n bodyEndLine: number\n}\n\n/**\n * Returns a configured remark processor with frontmatter and GFM support.\n */\nfunction processor() {\n return remark().use(remarkFrontmatter).use(remarkGfm)\n}\n\n/**\n * Parses a heading line (e.g. `## My Heading`) and returns `{ depth, text }`, or `null` when\n * the line is not an ATX heading.\n */\nfunction parseHeadingLine(line: string): { depth: 1 | 2 | 3 | 4 | 5 | 6; text: string } | null {\n const match = /^(#{1,6})\\s+(.*)$/.exec(line)\n if (!match) return null\n const depth = match[1].length as 1 | 2 | 3 | 4 | 5 | 6\n return { depth, text: match[2].trim() }\n}\n\n/**\n * Returns the length of a fence marker at the start of `line` (3+), or `0` if the line is not\n * a fence opener/closer. Handles both backtick (` ``` `) and tilde (`~~~`) fences.\n */\nfunction fenceLength(line: string): number {\n const trimmed = line.trimStart()\n const match = /^(`{3,}|~{3,})/.exec(trimmed)\n return match ? match[1].length : 0\n}\n\n/**\n * Extracts the language identifier from a fence opener line (e.g. ` ```ts ` → `'ts'`), or\n * `null` when none is present.\n */\nfunction fenceLang(line: string): string | null {\n const trimmed = line.trimStart()\n const match = /^(?:`{3,}|~{3,})(\\S+)/.exec(trimmed)\n return match ? match[1] : null\n}\n\n/**\n * A {@link @nhtio/adk!SpooledArtifact} specialisation that adds markdown-aware structural queries.\n *\n * @remarks\n * Designed for large markdown documents where loading the full content into memory is\n * impractical. The structural index (heading positions, code block positions) is built by a\n * single line-by-line scan of the {@link @nhtio/adk!SpoolReader} without retaining any content. Only the\n * tiny metadata index and the parsed frontmatter object are cached.\n *\n * Content retrieval is always bounded — use `cat(start, end)` or the `startLine`/`endLine`\n * parameters on inline methods to fetch only the lines you need.\n *\n * Inline methods (`md_links`, `md_images`, `md_text`, `md_ast`) accept optional line-range\n * arguments. Without a range they read the full document — documented trade-off, caller\n * responsibility to bound the range for large documents.\n *\n * The processor always applies `remark-gfm` (tables, task lists, strikethrough, autolinks)\n * in addition to standard CommonMark and YAML frontmatter.\n */\nexport class SpooledMarkdownArtifact extends SpooledArtifact {\n #index: MarkdownIndex | undefined\n #frontmatter: Record<string, unknown> | null | undefined\n\n /**\n * @param reader - The backing store to read from.\n */\n constructor(reader: SpoolReader) {\n super(reader)\n }\n\n /**\n * Returns `true` if `value` is a {@link SpooledMarkdownArtifact} instance.\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. Matches the pattern used by every\n * other class guard in the ADK; safe against the dual-module-copy case where two distinct\n * `SpooledMarkdownArtifact` classes coexist in the same realm.\n */\n public static isSpooledMarkdownArtifact(value: unknown): value is SpooledMarkdownArtifact {\n return isInstanceOf(value, 'SpooledMarkdownArtifact', SpooledMarkdownArtifact)\n }\n\n /**\n * The markdown-specific artifact-query descriptors this class adds on top of the base set.\n *\n * @remarks\n * Lists `artifact_md_frontmatter`, `artifact_md_headings`, `artifact_md_code_blocks`,\n * `artifact_md_sections`, `artifact_md_links`, `artifact_md_images`, `artifact_md_text`,\n * `artifact_md_ast`. The base seven descriptors (`artifact_head`, etc.) are NOT included\n * here — they are forged separately by {@link SpooledMarkdownArtifact.forgeTools}, which\n * calls `SpooledArtifact.forgeTools(ctx)` to produce the base-narrowed tools and then\n * registers its own markdown tools on the result. Downstream consumers building custom\n * subclasses should follow the same pattern: own only your own descriptors; override\n * `forgeTools` to compose with the base output.\n */\n public static toolMethods: ReadonlyArray<ToolMethodDescriptor> = Object.freeze([\n {\n name: 'artifact_md_frontmatter',\n method: 'md_frontmatter',\n description:\n 'Return parsed YAML frontmatter (or undefined) from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_md_headings',\n method: 'md_headings',\n description:\n 'Return all headings, optionally filtered by depth, from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n depth: validator\n .number()\n .integer()\n .min(1)\n .max(6)\n .optional()\n .description('ATX heading depth (1-6).'),\n }),\n },\n {\n name: 'artifact_md_code_blocks',\n method: 'md_code_blocks',\n description:\n 'Return all fenced code block entries, optionally filtered by language, from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n lang: validator\n .string()\n .optional()\n .description('Language identifier. Pass empty string to match blocks with no lang.'),\n }),\n },\n {\n name: 'artifact_md_sections',\n method: 'md_sections',\n description:\n 'Return document sections (line-range metadata only) from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n depth: validator\n .number()\n .integer()\n .min(1)\n .max(6)\n .optional()\n .description('ATX heading depth (1-6).'),\n }),\n },\n {\n name: 'artifact_md_links',\n method: 'md_links',\n description:\n 'Return all inline and reference links within the given line range from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n {\n name: 'artifact_md_images',\n method: 'md_images',\n description:\n 'Return all images within the given line range from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n {\n name: 'artifact_md_text',\n method: 'md_text',\n description:\n 'Return plain text with markup stripped, for the given line range, from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n {\n name: 'artifact_md_ast',\n method: 'md_ast',\n description:\n 'Return the full MDAST Root for the specified line range from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n ])\n\n /**\n * Forges base-class tools plus markdown-specific tools narrowed to\n * {@link SpooledMarkdownArtifact}.\n *\n * @remarks\n * Standard subclass extension pattern: call `SpooledArtifact.forgeTools(ctx)` to produce\n * the base seven `artifact_*` tools narrowed to any `SpooledArtifact` in the turn, then\n * register one `ArtifactTool` per markdown-specific descriptor narrowed to markdown\n * artifacts. Downstream consumers building their own subclasses should follow the same\n * shape.\n */\n public static override forgeTools(ctx: DispatchContext): ToolRegistry {\n const registry = SpooledArtifact.forgeTools(ctx)\n const requires = SpooledMarkdownArtifact\n const compatibleIds = [...ctx.turnToolCalls]\n .filter((tc) => !tc.fromArtifactTool && isInstanceOf(tc.results, requires.name, requires))\n .map((tc) => tc.id)\n if (compatibleIds.length === 0) return registry\n\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 = (\n descriptor.argsSchema ?? validator.object<Record<string, never>>({})\n ).append({\n callId: callIdSchema,\n })\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 === 'md_headings' || descriptor.method === 'md_sections') {\n methodArgs.push(args.depth as number | undefined)\n } else if (descriptor.method === 'md_code_blocks') {\n methodArgs.push(args.lang as string | undefined)\n } else if (\n descriptor.method === 'md_links' ||\n descriptor.method === 'md_images' ||\n descriptor.method === 'md_text' ||\n descriptor.method === 'md_ast'\n ) {\n methodArgs.push(\n args.startLine as number | undefined,\n args.endLine as number | undefined\n )\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 const serialise = descriptor.serialise ?? defaultSerialise\n return serialise(result)\n },\n })\n registry.register(tool)\n }\n return registry\n }\n\n /**\n * Builds the structural index in a single top-to-bottom pass, retaining only metadata.\n */\n async #resolveIndex(): Promise<MarkdownIndex> {\n if (this.#index !== undefined) return this.#index\n\n const count = await this.lineCount()\n const headingsRaw: Array<{ depth: 1 | 2 | 3 | 4 | 5 | 6; text: string; startLine: number }> = []\n const codeBlocks: MarkdownCodeEntry[] = []\n\n let inFrontmatter = false\n let frontmatterClosed = false\n let inCodeBlock = false\n let openFenceLen = 0\n let openFenceStartLine = 0\n let openFenceLang: string | null = null\n\n for (let i = 0; i < count; i++) {\n const rawLine = await this.line(i)\n const l = rawLine ?? ''\n\n // Handle frontmatter block at the top of the document\n if (i === 0 && l.trim() === '---') {\n inFrontmatter = true\n continue\n }\n if (inFrontmatter) {\n if (l.trim() === '---') {\n inFrontmatter = false\n frontmatterClosed = true\n }\n continue\n }\n if (!frontmatterClosed && i === 0) {\n // No frontmatter — treat as normal document from the start\n }\n\n // Handle fenced code blocks\n const fLen = fenceLength(l)\n if (!inCodeBlock && fLen >= 3) {\n inCodeBlock = true\n openFenceLen = fLen\n openFenceStartLine = i\n openFenceLang = fenceLang(l)\n continue\n }\n if (inCodeBlock) {\n // A closing fence must use the same fence character (` or ~) and be at least as long.\n if (fLen >= openFenceLen) {\n const openLine = (await this.line(openFenceStartLine)) ?? ''\n const openChar = openLine.trimStart()[0]\n const closeChar = l.trimStart()[0]\n if (closeChar === openChar) {\n codeBlocks.push({ lang: openFenceLang, startLine: openFenceStartLine, endLine: i })\n inCodeBlock = false\n openFenceLen = 0\n }\n }\n continue\n }\n\n // Detect ATX headings (not inside code blocks, not in frontmatter)\n const heading = parseHeadingLine(l)\n if (heading) {\n headingsRaw.push({ ...heading, startLine: i })\n }\n }\n\n // Unclosed code block — record it anyway\n if (inCodeBlock) {\n codeBlocks.push({ lang: openFenceLang, startLine: openFenceStartLine, endLine: count - 1 })\n }\n\n // Post-process heading endLine values\n const headings: MarkdownHeadingEntry[] = headingsRaw.map((h, idx) => {\n const nextBoundary = headingsRaw.slice(idx + 1).find((n) => n.depth <= h.depth)\n const endLine = nextBoundary ? nextBoundary.startLine - 1 : count - 1\n return { ...h, endLine }\n })\n\n this.#index = { headings, codeBlocks }\n return this.#index\n }\n\n // ── Frontmatter ───────────────────────────────────────────────────────────\n\n /**\n * Returns the parsed YAML frontmatter, or `undefined` when no frontmatter block is present.\n *\n * @remarks\n * Short-circuits after reading the frontmatter block — never reads the document body. Caches\n * the result so subsequent calls are free. The result is `undefined` (not an empty object)\n * when no frontmatter is found, distinguishing \"no frontmatter\" from \"empty frontmatter\".\n */\n async md_frontmatter(): Promise<Record<string, unknown> | undefined> {\n if (this.#frontmatter !== undefined) return this.#frontmatter ?? undefined\n\n const firstLine = await this.line(0)\n if (firstLine?.trim() !== '---') {\n this.#frontmatter = null\n return undefined\n }\n\n const maxScan = Math.min(await this.lineCount(), 200)\n const yamlLines: string[] = []\n let closed = false\n for (let i = 1; i < maxScan; i++) {\n const l = await this.line(i)\n if (l?.trim() === '---') {\n closed = true\n break\n }\n yamlLines.push(l ?? '')\n }\n\n if (!closed) {\n this.#frontmatter = null\n return undefined\n }\n\n this.#frontmatter = yamlLoad(yamlLines.join('\\n')) as Record<string, unknown>\n return this.#frontmatter\n }\n\n // ── Structural index queries ──────────────────────────────────────────────\n\n /**\n * Returns all headings in document order, optionally filtered by depth.\n *\n * @remarks\n * Uses the cached structural index — no content is fetched from the {@link @nhtio/adk!SpoolReader}.\n *\n * @param depth - When provided, only headings at this ATX depth (1–6) are returned.\n */\n async md_headings(depth?: 1 | 2 | 3 | 4 | 5 | 6): Promise<MarkdownHeadingEntry[]> {\n const index = await this.#resolveIndex()\n if (depth === undefined) return index.headings.slice()\n return index.headings.filter((h) => h.depth === depth)\n }\n\n /**\n * Returns all fenced code block entries, optionally filtered by language identifier.\n *\n * @remarks\n * Returns line-range metadata only — no content is fetched. Use `cat(entry.startLine + 1,\n * entry.endLine)` to retrieve the code body (excluding fence lines).\n *\n * @param lang - When provided, only blocks with this exact lang identifier are returned.\n * Pass an empty string to match blocks with no lang identifier.\n */\n async md_code_blocks(lang?: string): Promise<MarkdownCodeEntry[]> {\n const index = await this.#resolveIndex()\n if (lang === undefined) return index.codeBlocks.slice()\n const target = lang === '' ? null : lang\n return index.codeBlocks.filter((b) => b.lang === target)\n }\n\n /**\n * Returns document sections derived from the structural index.\n *\n * @remarks\n * Returns only line-range metadata — body content is never fetched. To retrieve the body of a\n * section, call `cat(section.bodyStartLine, section.bodyEndLine + 1)`.\n *\n * When `depth` is provided, only sections introduced by a heading at that depth are returned;\n * deeper headings become part of the body.\n *\n * @param depth - When provided, only sections at this ATX depth (1–6) are returned.\n */\n async md_sections(depth?: 1 | 2 | 3 | 4 | 5 | 6): Promise<MarkdownSection[]> {\n const index = await this.#resolveIndex()\n const headings =\n depth !== undefined ? index.headings.filter((h) => h.depth === depth) : index.headings\n return headings.map((h) => ({\n depth: h.depth,\n heading: h.text,\n headingLine: h.startLine,\n bodyStartLine: h.startLine + 1,\n bodyEndLine: h.endLine,\n }))\n }\n\n // ── Inline queries (bounded) ──────────────────────────────────────────────\n\n /**\n * Returns the full MDAST Root for the specified line range.\n *\n * @remarks\n * Without a range, reads the full document — for large documents, use\n * {@link SpooledMarkdownArtifact.md_sections} to locate sections and pass\n * bounded line ranges here.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_ast(startLine?: number, endLine?: number): Promise<Root> {\n const lines = await this.cat(startLine, endLine)\n return processor().parse(lines.join('\\n')) as Root\n }\n\n /**\n * Returns all inline and reference links in the specified line range.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_links(\n startLine?: number,\n endLine?: number\n ): Promise<Array<{ text: string; url: string; title?: string }>> {\n const lines = await this.cat(startLine, endLine)\n const ast = processor().parse(lines.join('\\n'))\n const results: Array<{ text: string; url: string; title?: string }> = []\n visit(ast, 'link', (node: Link) => {\n results.push({\n text: mdastToString(node),\n url: node.url,\n title: node.title ?? undefined,\n })\n })\n return results\n }\n\n /**\n * Returns all images in the specified line range.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_images(\n startLine?: number,\n endLine?: number\n ): Promise<Array<{ alt: string; url: string; title?: string }>> {\n const lines = await this.cat(startLine, endLine)\n const ast = processor().parse(lines.join('\\n'))\n const results: Array<{ alt: string; url: string; title?: string }> = []\n visit(ast, 'image', (node: Image) => {\n results.push({\n alt: node.alt ?? '',\n url: node.url,\n title: node.title ?? undefined,\n })\n })\n return results\n }\n\n /**\n * Returns all document text with markup stripped, for the specified line range.\n *\n * @remarks\n * Uses `mdast-util-to-string` to extract plain text from the AST — code, link text, and\n * alt text are included; markdown syntax is removed.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_text(startLine?: number, endLine?: number): Promise<string> {\n const lines = await this.cat(startLine, endLine)\n const ast = processor().parse(lines.join('\\n'))\n return mdastToString(ast)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCA,SAAS,YAAY,SAAqC;CACxD,MAAM,UAAU,QAAQ,KAAK;CAG7B,IAAI;EACF,KAAK,MAAM,OAAO;EAClB,OAAO;CACT,QAAQ,CAER;CAGA,MAAM,gBAAgB,QAAQ,MAAM,IAAI,EAAE,QAAQ,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC;CAC3E,IACE,cAAc,SAAS,KACvB,cAAc,OAAO,MAAM;EACzB,IAAI;GACF,KAAK,MAAM,CAAC;GACZ,OAAO;EACT,QAAQ;GACN,OAAO;EACT;CACF,CAAC,GAED,OAAO;CA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,MAAM,KAAK,IAAI,WAAW,OAAO;EAC/C,MAAM,MAAM,UAAU,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;EAC9C,MAAM,UAA+D,CAAC;EACtE,CAAA,GAAA,iBAAA,OAAM,KAAK,UAAU,SAAgB;GACnC,QAAQ,KAAK;IACX,KAAK,KAAK,OAAO;IACjB,KAAK,KAAK;IACV,OAAO,KAAK,SAAS,KAAA;GACvB,CAAC;EACH,CAAC;EACD,OAAO;CACT;;;;;;;;;;;CAYA,MAAM,QAAQ,WAAoB,SAAmC;EACnE,MAAM,QAAQ,MAAM,KAAK,IAAI,WAAW,OAAO;EAE/C,QAAA,GAAA,qBAAA,UADY,UAAU,EAAE,MAAM,MAAM,KAAK,IAAI,CACxB,CAAG;CAC1B;AACF"}
|
|
1
|
+
{"version":3,"file":"spooled_markdown_artifact-DQX0RCdI.js","names":["#format","#resolveFormat","#resolveRecords","#parsed","#resolveIndex","#index","#frontmatter"],"sources":["../src/lib/classes/spooled_json_artifact.ts","../src/lib/classes/spooled_markdown_artifact.ts"],"sourcesContent":["import { default as JSON5 } from 'json5'\nimport { JSONPath } from 'jsonpath-plus'\nimport { validator } from '@nhtio/validation'\nimport { ArtifactTool } from './artifact_tool'\nimport { ToolRegistry } from './tool_registry'\nimport { isInstanceOf, isObject } from '../utils/guards'\nimport { SpooledArtifact, defaultSerialise } from './spooled_artifact'\nimport type { SpoolReader } from '../contracts/spool_reader'\nimport type { ToolMethodDescriptor } from './spooled_artifact'\nimport type { DispatchContext } from '../contracts/dispatch_context'\n\n/**\n * The set of JSON-derived formats that {@link SpooledJsonArtifact} can handle.\n *\n * @remarks\n * - `json` — a single JSON value spanning the entire artifact (strict RFC 8259).\n * - `json5` — a single JSON5 value spanning the entire artifact; permits comments, trailing\n * commas, unquoted keys, and other relaxed syntax via the `json5` package.\n * - `jsonl` — newline-delimited JSON; each non-empty line is an independent JSON value.\n * - `ndjson` — alias for `jsonl`; both names are accepted and behave identically.\n */\nexport type JsonArtifactFormat = 'json' | 'json5' | 'jsonl' | 'ndjson'\n\n/**\n * Detects the {@link JsonArtifactFormat} of a raw string.\n *\n * @remarks\n * Detection strategy (in order):\n * 1. If the content parses as strict JSON → `json`.\n * 2. If every non-empty line parses as strict JSON → `jsonl`.\n * 3. If the content parses as JSON5 → `json5`.\n * 4. Otherwise throws.\n *\n * Strict JSON is tried before JSON5 so that well-formed JSON files are not unnecessarily\n * classified as JSON5.\n *\n * @param content - The full artifact text to inspect.\n * @returns The inferred format.\n * @throws `Error` when the content cannot be classified as any supported JSON format.\n */\nfunction inferFormat(content: string): JsonArtifactFormat {\n const trimmed = content.trim()\n\n // 1. Try strict JSON\n try {\n JSON.parse(trimmed)\n return 'json'\n } catch {\n // fall through\n }\n\n // 2. Try JSONL (every non-empty line is valid JSON)\n const nonEmptyLines = trimmed.split('\\n').filter((l) => l.trim().length > 0)\n if (\n nonEmptyLines.length > 0 &&\n nonEmptyLines.every((l) => {\n try {\n JSON.parse(l)\n return true\n } catch {\n return false\n }\n })\n ) {\n return 'jsonl'\n }\n\n // 3. Try JSON5\n try {\n JSON5.parse(trimmed)\n return 'json5'\n } catch {\n // fall through\n }\n\n throw new Error('Unable to infer JSON format: content is not valid JSON, JSONL, NDJSON, or JSON5')\n}\n\n/**\n * A {@link @nhtio/adk!SpooledArtifact} specialisation that adds JSON-aware read operations.\n *\n * @typeParam T - The expected shape of each parsed record. Defaults to `unknown`.\n *\n * @remarks\n * Construct with an optional `format` hint. When omitted the format is auto-detected on first\n * access by reading the full artifact and running {@link inferFormat}. Once detected (or\n * provided), the format is cached for the lifetime of the instance.\n *\n * All JSON methods are async, consistent with {@link @nhtio/adk!SpooledArtifact}.\n *\n * Path-based methods (`json_get`, `json_filter`, `json_pluck`) use\n * [JSONPath-Plus](https://github.com/JSONPath-Plus/JSONPath) expressions. Full JSONPath syntax\n * is supported, including recursive descent (`..`), filter expressions (`[?(@.age > 18)]`),\n * and union selectors.\n */\nexport class SpooledJsonArtifact<T = unknown> extends SpooledArtifact {\n #format: JsonArtifactFormat | undefined\n #parsed: T[] | undefined\n\n /**\n * @param reader - The backing store to read from.\n * @param format - Optional format hint. When omitted, the format is inferred on first access.\n */\n constructor(reader: SpoolReader, format?: JsonArtifactFormat) {\n super(reader)\n this.#format = format\n }\n\n /**\n * Returns `true` if `value` is a {@link SpooledJsonArtifact} instance.\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. Matches the pattern used by every\n * other class guard in the ADK; safe against the dual-module-copy case where two distinct\n * `SpooledJsonArtifact` classes coexist in the same realm.\n *\n * @param value - The value to test.\n * @returns `true` when `value` is a {@link SpooledJsonArtifact} instance.\n */\n public static isSpooledJsonArtifact(value: unknown): value is SpooledJsonArtifact {\n return isInstanceOf(value, 'SpooledJsonArtifact', SpooledJsonArtifact)\n }\n\n /**\n * The JSON-specific artifact-query descriptors this class adds on top of the base set.\n *\n * @remarks\n * Lists `artifact_json_type`, `artifact_json_keys`, `artifact_json_length`,\n * `artifact_json_get`, `artifact_json_filter`, `artifact_json_slice`, `artifact_json_pluck`.\n * The base seven descriptors (`artifact_head`, etc.) are NOT included here — they are\n * forged separately by {@link SpooledJsonArtifact.forgeTools}, which calls\n * `SpooledArtifact.forgeTools(ctx)` to produce the base-narrowed tools and then registers\n * its own JSON tools on the result. Downstream consumers building custom subclasses\n * should follow the same pattern: own only your own descriptors; override `forgeTools` to\n * compose with the base output.\n */\n public static toolMethods: ReadonlyArray<ToolMethodDescriptor> = Object.freeze([\n {\n name: 'artifact_json_type',\n method: 'json_type',\n description:\n 'Return the JSON format (json | json5 | jsonl | ndjson) of a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_json_keys',\n method: 'json_keys',\n description: 'Return the top-level keys of a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_json_length',\n method: 'json_length',\n description:\n 'Return the record count of a JSON artifact produced earlier in this turn (1 for json/json5; line count for jsonl/ndjson).',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_json_get',\n method: 'json_get',\n description:\n 'Evaluate a JSONPath expression against a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({\n path: validator.string().required().description(\"JSONPath expression, e.g. '$.user.name'.\"),\n }),\n },\n {\n name: 'artifact_json_filter',\n method: 'json_filter',\n description:\n 'Return records of a JSON artifact (produced earlier in this turn) matched by a JSONPath filter.',\n argsSchema: validator.object({\n path: validator\n .string()\n .required()\n .description(\"JSONPath filter expression, e.g. '$[?(@.age>18)]'.\"),\n }),\n },\n {\n name: 'artifact_json_slice',\n method: 'json_slice',\n description:\n 'Return a slice of records by index range from a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({\n start: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start index (inclusive).'),\n end: validator.number().integer().min(0).optional().description('End index (exclusive).'),\n }),\n },\n {\n name: 'artifact_json_pluck',\n method: 'json_pluck',\n description:\n 'Return all values matched by a JSONPath expression across every record of a JSON artifact produced earlier in this turn.',\n argsSchema: validator.object({\n path: validator.string().required().description(\"JSONPath expression, e.g. '$..name'.\"),\n }),\n },\n ])\n\n /**\n * Forges base-class tools plus JSON-specific tools narrowed to {@link SpooledJsonArtifact}.\n *\n * @remarks\n * Standard subclass extension pattern: call `SpooledArtifact.forgeTools(ctx)` to produce\n * the base seven `artifact_*` tools narrowed to any `SpooledArtifact` in the turn, then\n * register one `ArtifactTool` per JSON-specific descriptor narrowed to JSON artifacts.\n * Downstream consumers building their own subclasses should follow the same shape.\n */\n public static override forgeTools(ctx: DispatchContext): ToolRegistry {\n const registry = SpooledArtifact.forgeTools(ctx)\n const requires = SpooledJsonArtifact\n const compatibleIds = [...ctx.turnToolCalls]\n .filter((tc) => !tc.fromArtifactTool && isInstanceOf(tc.results, requires.name, requires))\n .map((tc) => tc.id)\n if (compatibleIds.length === 0) return registry\n\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 = (\n descriptor.argsSchema ?? validator.object<Record<string, never>>({})\n ).append({\n callId: callIdSchema,\n })\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 (\n descriptor.method === 'json_get' ||\n descriptor.method === 'json_filter' ||\n descriptor.method === 'json_pluck'\n ) {\n methodArgs.push(args.path as string)\n } else if (descriptor.method === 'json_slice') {\n methodArgs.push(args.start as number | undefined, args.end as number | undefined)\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 const serialise = descriptor.serialise ?? defaultSerialise\n return serialise(result)\n },\n })\n registry.register(tool)\n }\n return registry\n }\n\n /**\n * Resolves and caches the detected or provided format.\n */\n async #resolveFormat(): Promise<JsonArtifactFormat> {\n if (this.#format !== undefined) {\n return this.#format\n }\n const lines = await this.cat()\n this.#format = inferFormat(lines.join('\\n'))\n return this.#format\n }\n\n /**\n * Parses and caches all records from the artifact.\n *\n * @remarks\n * For `json`/`json5` format: returns a single-element array containing the parsed root value.\n * For `jsonl`/`ndjson` format: returns one element per non-empty line.\n */\n async #resolveRecords(): Promise<T[]> {\n if (this.#parsed !== undefined) {\n return this.#parsed\n }\n const format = await this.#resolveFormat()\n const lines = await this.cat()\n if (format === 'json') {\n this.#parsed = [JSON.parse(lines.join('\\n')) as T]\n } else if (format === 'json5') {\n this.#parsed = [JSON5.parse(lines.join('\\n')) as T]\n } else {\n // jsonl / ndjson\n this.#parsed = lines.filter((l) => l.trim().length > 0).map((l) => JSON.parse(l) as T)\n }\n return this.#parsed\n }\n\n /**\n * Returns the detected or provided format for this artifact.\n *\n * @returns One of `'json'`, `'json5'`, `'jsonl'`, or `'ndjson'`.\n */\n async json_type(): Promise<JsonArtifactFormat> {\n return this.#resolveFormat()\n }\n\n /**\n * Returns the top-level keys of the parsed content.\n *\n * @remarks\n * - For `json`/`json5`: returns the keys of the root object, or `undefined` when the root is\n * not a plain object (e.g. an array or scalar).\n * - For `jsonl`/`ndjson`: returns the union of keys across all records that are plain objects.\n * Duplicate keys are deduplicated.\n *\n * @returns Array of key strings, or `undefined` when no object keys are present.\n */\n async json_keys(): Promise<string[] | undefined> {\n const records = await this.#resolveRecords()\n const format = await this.#resolveFormat()\n if (format === 'json' || format === 'json5') {\n const root = records[0]\n if (isObject(root)) {\n return Object.keys(root as object)\n }\n return undefined\n }\n const keySet = new Set<string>()\n for (const record of records) {\n if (isObject(record)) {\n for (const key of Object.keys(record as object)) {\n keySet.add(key)\n }\n }\n }\n return keySet.size > 0 ? Array.from(keySet) : undefined\n }\n\n /**\n * Returns the total number of records in the artifact.\n *\n * @remarks\n * - For `json`/`json5`: always `1` (the entire artifact is a single value).\n * - For `jsonl`/`ndjson`: the number of non-empty lines.\n *\n * @returns The record count.\n */\n async json_length(): Promise<number> {\n const records = await this.#resolveRecords()\n return records.length\n }\n\n /**\n * Evaluates a JSONPath expression against the parsed content.\n *\n * @remarks\n * Uses [JSONPath-Plus](https://github.com/JSONPath-Plus/JSONPath). Full JSONPath syntax is\n * supported: recursive descent (`$..*`), filter expressions (`$[?(@.age > 18)]`), union\n * selectors, and more.\n *\n * - For `json`/`json5`: evaluates the expression against the root value.\n * - For `jsonl`/`ndjson`: evaluates the expression against each record and returns a flat\n * array of all matches across all records.\n *\n * @param path - A JSONPath expression (e.g. `'$.user.address.city'`, `'$..name'`).\n * @returns Array of matched values. Empty array when no matches are found.\n */\n async json_get(path: string): Promise<unknown[]> {\n const records = await this.#resolveRecords()\n const format = await this.#resolveFormat()\n if (format === 'json' || format === 'json5') {\n return JSONPath({ path, json: records[0] as object })\n }\n return records.flatMap((r) => JSONPath({ path, json: r as object }))\n }\n\n /**\n * Returns a slice of the parsed records by index range.\n *\n * @remarks\n * For `json`/`json5`: always returns `[root]` — the artifact is a single record so slicing is\n * not meaningful. For `jsonl`/`ndjson`: behaves like `Array.prototype.slice`.\n *\n * @param start - Start index (inclusive). Defaults to `0`.\n * @param end - End index (exclusive). Defaults to the record count.\n * @returns Array of sliced records.\n */\n async json_slice(start?: number, end?: number): Promise<T[]> {\n const records = await this.#resolveRecords()\n const format = await this.#resolveFormat()\n if (format === 'json' || format === 'json5') {\n return records\n }\n return records.slice(start, end)\n }\n\n /**\n * Returns records matched by a JSONPath filter expression.\n *\n * @remarks\n * Evaluates `path` against each record and returns those for which the expression produces at\n * least one match. For `json`/`json5`, evaluates against the root value and returns it in an\n * array if matched.\n *\n * @param path - A JSONPath expression (e.g. `'$[?(@.status === \"active\")]'`).\n * @returns Array of matching records.\n */\n async json_filter(path: string): Promise<T[]> {\n const records = await this.#resolveRecords()\n return records.filter((r) => {\n const matches = JSONPath({ path, json: r as object })\n return Array.isArray(matches) && matches.length > 0\n })\n }\n\n /**\n * Returns all values matched by a JSONPath expression across every record.\n *\n * @remarks\n * Convenience over {@link SpooledJsonArtifact.json_get} with an identical signature — use\n * whichever name better communicates intent at the call site. `json_pluck` reads well for\n * extracting a single field column; `json_get` reads well for structured queries.\n *\n * @param path - A JSONPath expression (e.g. `'$..name'`).\n * @returns Array of matched values.\n */\n async json_pluck(path: string): Promise<unknown[]> {\n return this.json_get(path)\n }\n}\n","import { remark } from 'remark'\nimport { visit } from 'unist-util-visit'\nimport { load as yamlLoad } from 'js-yaml'\nimport { validator } from '@nhtio/validation'\nimport { isInstanceOf } from '../utils/guards'\nimport { ArtifactTool } from './artifact_tool'\nimport { ToolRegistry } from './tool_registry'\nimport { default as remarkGfm } from 'remark-gfm'\nimport { toString as mdastToString } from 'mdast-util-to-string'\nimport { default as remarkFrontmatter } from 'remark-frontmatter'\nimport { SpooledArtifact, defaultSerialise } from './spooled_artifact'\nimport type { Root, Link, Image } from 'mdast'\nimport type { SpoolReader } from '../contracts/spool_reader'\nimport type { ToolMethodDescriptor } from './spooled_artifact'\nimport type { DispatchContext } from '../contracts/dispatch_context'\n\n/**\n * A single heading entry in the document's structural index.\n *\n * @remarks\n * `startLine` is the 0-based line of the heading itself. `endLine` is the 0-based index of the\n * last line belonging to this section (inclusive) — the line immediately before the next heading\n * of equal or lesser depth, or the last line of the document.\n */\nexport interface MarkdownHeadingEntry {\n /** ATX heading depth: 1 (`#`) through 6 (`######`). */\n depth: 1 | 2 | 3 | 4 | 5 | 6\n /** The heading text with the leading `#` prefix stripped and trimmed. */\n text: string\n /** 0-based line index of the heading line itself. */\n startLine: number\n /** 0-based line index of the last line in this section (inclusive). */\n endLine: number\n}\n\n/**\n * A single fenced code block entry in the document's structural index.\n */\nexport interface MarkdownCodeEntry {\n /** The language identifier immediately after the opening fence, or `null` when absent. */\n lang: string | null\n /** 0-based line index of the opening fence line. */\n startLine: number\n /** 0-based line index of the closing fence line. */\n endLine: number\n}\n\ninterface MarkdownIndex {\n headings: MarkdownHeadingEntry[]\n codeBlocks: MarkdownCodeEntry[]\n}\n\n/**\n * A section of a markdown document as returned by {@link SpooledMarkdownArtifact.md_sections}.\n *\n * @remarks\n * Contains only line-range metadata — no content is fetched until the caller explicitly\n * requests it via `cat(bodyStartLine, bodyEndLine + 1)`.\n */\nexport interface MarkdownSection {\n depth: 1 | 2 | 3 | 4 | 5 | 6\n /** The heading text. */\n heading: string\n /** 0-based line of the heading itself. */\n headingLine: number\n /** 0-based line of the first body line (heading line + 1). */\n bodyStartLine: number\n /** 0-based line of the last body line (inclusive). */\n bodyEndLine: number\n}\n\n/**\n * Returns a configured remark processor with frontmatter and GFM support.\n */\nfunction processor() {\n return remark().use(remarkFrontmatter).use(remarkGfm)\n}\n\n/**\n * Parses a heading line (e.g. `## My Heading`) and returns `{ depth, text }`, or `null` when\n * the line is not an ATX heading.\n */\nfunction parseHeadingLine(line: string): { depth: 1 | 2 | 3 | 4 | 5 | 6; text: string } | null {\n const match = /^(#{1,6})\\s+(.*)$/.exec(line)\n if (!match) return null\n const depth = match[1].length as 1 | 2 | 3 | 4 | 5 | 6\n return { depth, text: match[2].trim() }\n}\n\n/**\n * Returns the length of a fence marker at the start of `line` (3+), or `0` if the line is not\n * a fence opener/closer. Handles both backtick (` ``` `) and tilde (`~~~`) fences.\n */\nfunction fenceLength(line: string): number {\n const trimmed = line.trimStart()\n const match = /^(`{3,}|~{3,})/.exec(trimmed)\n return match ? match[1].length : 0\n}\n\n/**\n * Extracts the language identifier from a fence opener line (e.g. ` ```ts ` → `'ts'`), or\n * `null` when none is present.\n */\nfunction fenceLang(line: string): string | null {\n const trimmed = line.trimStart()\n const match = /^(?:`{3,}|~{3,})(\\S+)/.exec(trimmed)\n return match ? match[1] : null\n}\n\n/**\n * A {@link @nhtio/adk!SpooledArtifact} specialisation that adds markdown-aware structural queries.\n *\n * @remarks\n * Designed for large markdown documents where loading the full content into memory is\n * impractical. The structural index (heading positions, code block positions) is built by a\n * single line-by-line scan of the {@link @nhtio/adk!SpoolReader} without retaining any content. Only the\n * tiny metadata index and the parsed frontmatter object are cached.\n *\n * Content retrieval is always bounded — use `cat(start, end)` or the `startLine`/`endLine`\n * parameters on inline methods to fetch only the lines you need.\n *\n * Inline methods (`md_links`, `md_images`, `md_text`, `md_ast`) accept optional line-range\n * arguments. Without a range they read the full document — documented trade-off, caller\n * responsibility to bound the range for large documents.\n *\n * The processor always applies `remark-gfm` (tables, task lists, strikethrough, autolinks)\n * in addition to standard CommonMark and YAML frontmatter.\n */\nexport class SpooledMarkdownArtifact extends SpooledArtifact {\n #index: MarkdownIndex | undefined\n #frontmatter: Record<string, unknown> | null | undefined\n\n /**\n * @param reader - The backing store to read from.\n */\n constructor(reader: SpoolReader) {\n super(reader)\n }\n\n /**\n * Returns `true` if `value` is a {@link SpooledMarkdownArtifact} instance.\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. Matches the pattern used by every\n * other class guard in the ADK; safe against the dual-module-copy case where two distinct\n * `SpooledMarkdownArtifact` classes coexist in the same realm.\n */\n public static isSpooledMarkdownArtifact(value: unknown): value is SpooledMarkdownArtifact {\n return isInstanceOf(value, 'SpooledMarkdownArtifact', SpooledMarkdownArtifact)\n }\n\n /**\n * The markdown-specific artifact-query descriptors this class adds on top of the base set.\n *\n * @remarks\n * Lists `artifact_md_frontmatter`, `artifact_md_headings`, `artifact_md_code_blocks`,\n * `artifact_md_sections`, `artifact_md_links`, `artifact_md_images`, `artifact_md_text`,\n * `artifact_md_ast`. The base seven descriptors (`artifact_head`, etc.) are NOT included\n * here — they are forged separately by {@link SpooledMarkdownArtifact.forgeTools}, which\n * calls `SpooledArtifact.forgeTools(ctx)` to produce the base-narrowed tools and then\n * registers its own markdown tools on the result. Downstream consumers building custom\n * subclasses should follow the same pattern: own only your own descriptors; override\n * `forgeTools` to compose with the base output.\n */\n public static toolMethods: ReadonlyArray<ToolMethodDescriptor> = Object.freeze([\n {\n name: 'artifact_md_frontmatter',\n method: 'md_frontmatter',\n description:\n 'Return parsed YAML frontmatter (or undefined) from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({}),\n },\n {\n name: 'artifact_md_headings',\n method: 'md_headings',\n description:\n 'Return all headings, optionally filtered by depth, from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n depth: validator\n .number()\n .integer()\n .min(1)\n .max(6)\n .optional()\n .description('ATX heading depth (1-6).'),\n }),\n },\n {\n name: 'artifact_md_code_blocks',\n method: 'md_code_blocks',\n description:\n 'Return all fenced code block entries, optionally filtered by language, from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n lang: validator\n .string()\n .optional()\n .description('Language identifier. Pass empty string to match blocks with no lang.'),\n }),\n },\n {\n name: 'artifact_md_sections',\n method: 'md_sections',\n description:\n 'Return document sections (line-range metadata only) from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n depth: validator\n .number()\n .integer()\n .min(1)\n .max(6)\n .optional()\n .description('ATX heading depth (1-6).'),\n }),\n },\n {\n name: 'artifact_md_links',\n method: 'md_links',\n description:\n 'Return all inline and reference links within the given line range from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n {\n name: 'artifact_md_images',\n method: 'md_images',\n description:\n 'Return all images within the given line range from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n {\n name: 'artifact_md_text',\n method: 'md_text',\n description:\n 'Return plain text with markup stripped, for the given line range, from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n {\n name: 'artifact_md_ast',\n method: 'md_ast',\n description:\n 'Return the full MDAST Root for the specified line range from a markdown artifact produced earlier in this turn.',\n argsSchema: validator.object({\n startLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('Start line (inclusive).'),\n endLine: validator\n .number()\n .integer()\n .min(0)\n .optional()\n .description('End line (exclusive).'),\n }),\n },\n ])\n\n /**\n * Forges base-class tools plus markdown-specific tools narrowed to\n * {@link SpooledMarkdownArtifact}.\n *\n * @remarks\n * Standard subclass extension pattern: call `SpooledArtifact.forgeTools(ctx)` to produce\n * the base seven `artifact_*` tools narrowed to any `SpooledArtifact` in the turn, then\n * register one `ArtifactTool` per markdown-specific descriptor narrowed to markdown\n * artifacts. Downstream consumers building their own subclasses should follow the same\n * shape.\n */\n public static override forgeTools(ctx: DispatchContext): ToolRegistry {\n const registry = SpooledArtifact.forgeTools(ctx)\n const requires = SpooledMarkdownArtifact\n const compatibleIds = [...ctx.turnToolCalls]\n .filter((tc) => !tc.fromArtifactTool && isInstanceOf(tc.results, requires.name, requires))\n .map((tc) => tc.id)\n if (compatibleIds.length === 0) return registry\n\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 = (\n descriptor.argsSchema ?? validator.object<Record<string, never>>({})\n ).append({\n callId: callIdSchema,\n })\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 === 'md_headings' || descriptor.method === 'md_sections') {\n methodArgs.push(args.depth as number | undefined)\n } else if (descriptor.method === 'md_code_blocks') {\n methodArgs.push(args.lang as string | undefined)\n } else if (\n descriptor.method === 'md_links' ||\n descriptor.method === 'md_images' ||\n descriptor.method === 'md_text' ||\n descriptor.method === 'md_ast'\n ) {\n methodArgs.push(\n args.startLine as number | undefined,\n args.endLine as number | undefined\n )\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 const serialise = descriptor.serialise ?? defaultSerialise\n return serialise(result)\n },\n })\n registry.register(tool)\n }\n return registry\n }\n\n /**\n * Builds the structural index in a single top-to-bottom pass, retaining only metadata.\n */\n async #resolveIndex(): Promise<MarkdownIndex> {\n if (this.#index !== undefined) return this.#index\n\n const count = await this.lineCount()\n const headingsRaw: Array<{ depth: 1 | 2 | 3 | 4 | 5 | 6; text: string; startLine: number }> = []\n const codeBlocks: MarkdownCodeEntry[] = []\n\n let inFrontmatter = false\n let frontmatterClosed = false\n let inCodeBlock = false\n let openFenceLen = 0\n let openFenceStartLine = 0\n let openFenceLang: string | null = null\n\n for (let i = 0; i < count; i++) {\n const rawLine = await this.line(i)\n const l = rawLine ?? ''\n\n // Handle frontmatter block at the top of the document\n if (i === 0 && l.trim() === '---') {\n inFrontmatter = true\n continue\n }\n if (inFrontmatter) {\n if (l.trim() === '---') {\n inFrontmatter = false\n frontmatterClosed = true\n }\n continue\n }\n if (!frontmatterClosed && i === 0) {\n // No frontmatter — treat as normal document from the start\n }\n\n // Handle fenced code blocks\n const fLen = fenceLength(l)\n if (!inCodeBlock && fLen >= 3) {\n inCodeBlock = true\n openFenceLen = fLen\n openFenceStartLine = i\n openFenceLang = fenceLang(l)\n continue\n }\n if (inCodeBlock) {\n // A closing fence must use the same fence character (` or ~) and be at least as long.\n if (fLen >= openFenceLen) {\n const openLine = (await this.line(openFenceStartLine)) ?? ''\n const openChar = openLine.trimStart()[0]\n const closeChar = l.trimStart()[0]\n if (closeChar === openChar) {\n codeBlocks.push({ lang: openFenceLang, startLine: openFenceStartLine, endLine: i })\n inCodeBlock = false\n openFenceLen = 0\n }\n }\n continue\n }\n\n // Detect ATX headings (not inside code blocks, not in frontmatter)\n const heading = parseHeadingLine(l)\n if (heading) {\n headingsRaw.push({ ...heading, startLine: i })\n }\n }\n\n // Unclosed code block — record it anyway\n if (inCodeBlock) {\n codeBlocks.push({ lang: openFenceLang, startLine: openFenceStartLine, endLine: count - 1 })\n }\n\n // Post-process heading endLine values\n const headings: MarkdownHeadingEntry[] = headingsRaw.map((h, idx) => {\n const nextBoundary = headingsRaw.slice(idx + 1).find((n) => n.depth <= h.depth)\n const endLine = nextBoundary ? nextBoundary.startLine - 1 : count - 1\n return { ...h, endLine }\n })\n\n this.#index = { headings, codeBlocks }\n return this.#index\n }\n\n // ── Frontmatter ───────────────────────────────────────────────────────────\n\n /**\n * Returns the parsed YAML frontmatter, or `undefined` when no frontmatter block is present.\n *\n * @remarks\n * Short-circuits after reading the frontmatter block — never reads the document body. Caches\n * the result so subsequent calls are free. The result is `undefined` (not an empty object)\n * when no frontmatter is found, distinguishing \"no frontmatter\" from \"empty frontmatter\".\n */\n async md_frontmatter(): Promise<Record<string, unknown> | undefined> {\n if (this.#frontmatter !== undefined) return this.#frontmatter ?? undefined\n\n const firstLine = await this.line(0)\n if (firstLine?.trim() !== '---') {\n this.#frontmatter = null\n return undefined\n }\n\n const maxScan = Math.min(await this.lineCount(), 200)\n const yamlLines: string[] = []\n let closed = false\n for (let i = 1; i < maxScan; i++) {\n const l = await this.line(i)\n if (l?.trim() === '---') {\n closed = true\n break\n }\n yamlLines.push(l ?? '')\n }\n\n if (!closed) {\n this.#frontmatter = null\n return undefined\n }\n\n this.#frontmatter = yamlLoad(yamlLines.join('\\n')) as Record<string, unknown>\n return this.#frontmatter\n }\n\n // ── Structural index queries ──────────────────────────────────────────────\n\n /**\n * Returns all headings in document order, optionally filtered by depth.\n *\n * @remarks\n * Uses the cached structural index — no content is fetched from the {@link @nhtio/adk!SpoolReader}.\n *\n * @param depth - When provided, only headings at this ATX depth (1–6) are returned.\n */\n async md_headings(depth?: 1 | 2 | 3 | 4 | 5 | 6): Promise<MarkdownHeadingEntry[]> {\n const index = await this.#resolveIndex()\n if (depth === undefined) return index.headings.slice()\n return index.headings.filter((h) => h.depth === depth)\n }\n\n /**\n * Returns all fenced code block entries, optionally filtered by language identifier.\n *\n * @remarks\n * Returns line-range metadata only — no content is fetched. Use `cat(entry.startLine + 1,\n * entry.endLine)` to retrieve the code body (excluding fence lines).\n *\n * @param lang - When provided, only blocks with this exact lang identifier are returned.\n * Pass an empty string to match blocks with no lang identifier.\n */\n async md_code_blocks(lang?: string): Promise<MarkdownCodeEntry[]> {\n const index = await this.#resolveIndex()\n if (lang === undefined) return index.codeBlocks.slice()\n const target = lang === '' ? null : lang\n return index.codeBlocks.filter((b) => b.lang === target)\n }\n\n /**\n * Returns document sections derived from the structural index.\n *\n * @remarks\n * Returns only line-range metadata — body content is never fetched. To retrieve the body of a\n * section, call `cat(section.bodyStartLine, section.bodyEndLine + 1)`.\n *\n * When `depth` is provided, only sections introduced by a heading at that depth are returned;\n * deeper headings become part of the body.\n *\n * @param depth - When provided, only sections at this ATX depth (1–6) are returned.\n */\n async md_sections(depth?: 1 | 2 | 3 | 4 | 5 | 6): Promise<MarkdownSection[]> {\n const index = await this.#resolveIndex()\n const headings =\n depth !== undefined ? index.headings.filter((h) => h.depth === depth) : index.headings\n return headings.map((h) => ({\n depth: h.depth,\n heading: h.text,\n headingLine: h.startLine,\n bodyStartLine: h.startLine + 1,\n bodyEndLine: h.endLine,\n }))\n }\n\n // ── Inline queries (bounded) ──────────────────────────────────────────────\n\n /**\n * Returns the full MDAST Root for the specified line range.\n *\n * @remarks\n * Without a range, reads the full document — for large documents, use\n * {@link SpooledMarkdownArtifact.md_sections} to locate sections and pass\n * bounded line ranges here.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_ast(startLine?: number, endLine?: number): Promise<Root> {\n const lines = await this.cat(startLine, endLine)\n return processor().parse(lines.join('\\n')) as Root\n }\n\n /**\n * Returns all inline and reference links in the specified line range.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_links(\n startLine?: number,\n endLine?: number\n ): Promise<Array<{ text: string; url: string; title?: string }>> {\n const lines = await this.cat(startLine, endLine)\n const ast = processor().parse(lines.join('\\n'))\n const results: Array<{ text: string; url: string; title?: string }> = []\n visit(ast, 'link', (node: Link) => {\n results.push({\n text: mdastToString(node),\n url: node.url,\n title: node.title ?? undefined,\n })\n })\n return results\n }\n\n /**\n * Returns all images in the specified line range.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_images(\n startLine?: number,\n endLine?: number\n ): Promise<Array<{ alt: string; url: string; title?: string }>> {\n const lines = await this.cat(startLine, endLine)\n const ast = processor().parse(lines.join('\\n'))\n const results: Array<{ alt: string; url: string; title?: string }> = []\n visit(ast, 'image', (node: Image) => {\n results.push({\n alt: node.alt ?? '',\n url: node.url,\n title: node.title ?? undefined,\n })\n })\n return results\n }\n\n /**\n * Returns all document text with markup stripped, for the specified line range.\n *\n * @remarks\n * Uses `mdast-util-to-string` to extract plain text from the AST — code, link text, and\n * alt text are included; markdown syntax is removed.\n *\n * @param startLine - 0-based start line (inclusive). Defaults to `0`.\n * @param endLine - 0-based end line (exclusive). Defaults to `lineCount()`.\n */\n async md_text(startLine?: number, endLine?: number): Promise<string> {\n const lines = await this.cat(startLine, endLine)\n const ast = processor().parse(lines.join('\\n'))\n return mdastToString(ast)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCA,SAAS,YAAY,SAAqC;CACxD,MAAM,UAAU,QAAQ,KAAK;CAG7B,IAAI;EACF,KAAK,MAAM,OAAO;EAClB,OAAO;CACT,QAAQ,CAER;CAGA,MAAM,gBAAgB,QAAQ,MAAM,IAAI,EAAE,QAAQ,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC;CAC3E,IACE,cAAc,SAAS,KACvB,c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O,KAAK,SAAS,KAAA;GACvB,CAAC;EACH,CAAC;EACD,OAAO;CACT;;;;;;;CAQA,MAAM,UACJ,WACA,SAC8D;EAC9D,MAAM,QAAQ,MAAM,KAAK,IAAI,WAAW,OAAO;EAC/C,MAAM,MAAM,UAAU,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;EAC9C,MAAM,UAA+D,CAAC;EACtE,CAAA,GAAA,iBAAA,OAAM,KAAK,UAAU,SAAgB;GACnC,QAAQ,KAAK;IACX,KAAK,KAAK,OAAO;IACjB,KAAK,KAAK;IACV,OAAO,KAAK,SAAS,KAAA;GACvB,CAAC;EACH,CAAC;EACD,OAAO;CACT;;;;;;;;;;;CAYA,MAAM,QAAQ,WAAoB,SAAmC;EACnE,MAAM,QAAQ,MAAM,KAAK,IAAI,WAAW,OAAO;EAE/C,QAAA,GAAA,qBAAA,UADY,UAAU,EAAE,MAAM,MAAM,KAAK,IAAI,CACxB,CAAG;CAC1B;AACF"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
require("./chunk-Ble4zEEl.js");
|
|
2
2
|
const require_exceptions = require("./exceptions-CitH5wZI.js");
|
|
3
|
-
const require_tool_registry = require("./tool_registry-
|
|
4
|
-
const require_runtime = require("./runtime-
|
|
5
|
-
const require_tool_call = require("./tool_call-
|
|
3
|
+
const require_tool_registry = require("./tool_registry-CtCQ4Xoz.js");
|
|
4
|
+
const require_runtime = require("./runtime-MFFcJrRv.js");
|
|
5
|
+
const require_tool_call = require("./tool_call--7ti-frB.js");
|
|
6
6
|
let _nhtio_validation = require("@nhtio/validation");
|
|
7
7
|
//#region src/lib/classes/identity.ts
|
|
8
8
|
/**
|
|
@@ -491,4 +491,4 @@ Object.defineProperty(exports, "Thought", {
|
|
|
491
491
|
}
|
|
492
492
|
});
|
|
493
493
|
|
|
494
|
-
//# sourceMappingURL=thought-
|
|
494
|
+
//# sourceMappingURL=thought-BD6AkkOr.js.map
|