@nhtio/adk 1.20260609.0 → 1.20260609.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/CHANGELOG.md +54 -9
  2. package/batteries/tools/_shared/index.d.ts +121 -0
  3. package/batteries/tools/_shared.cjs +157 -0
  4. package/batteries/tools/_shared.cjs.map +1 -0
  5. package/batteries/tools/_shared.mjs +149 -0
  6. package/batteries/tools/_shared.mjs.map +1 -0
  7. package/batteries/tools/index.d.ts +2 -0
  8. package/batteries/tools/scrapper/exceptions.d.ts +21 -0
  9. package/batteries/tools/scrapper/index.d.ts +172 -0
  10. package/batteries/tools/scrapper/shared.d.ts +139 -0
  11. package/batteries/tools/scrapper.cjs +8 -0
  12. package/batteries/tools/scrapper.mjs +2 -0
  13. package/batteries/tools/searxng/index.d.ts +47 -20
  14. package/batteries/tools/searxng.cjs +2 -1
  15. package/batteries/tools/searxng.mjs +2 -2
  16. package/batteries/tools/web_retrieval/index.d.ts +186 -0
  17. package/batteries/tools/web_retrieval.cjs +206 -0
  18. package/batteries/tools/web_retrieval.cjs.map +1 -0
  19. package/batteries/tools/web_retrieval.mjs +201 -0
  20. package/batteries/tools/web_retrieval.mjs.map +1 -0
  21. package/batteries/tools.cjs +13 -1
  22. package/batteries/tools.mjs +4 -2
  23. package/batteries.cjs +13 -1
  24. package/batteries.mjs +4 -2
  25. package/common.d.ts +1 -1
  26. package/eslint/rules.cjs +1 -1
  27. package/eslint/rules.mjs +1 -1
  28. package/eslint.cjs +2 -2
  29. package/eslint.mjs +2 -2
  30. package/index.cjs +2 -2
  31. package/index.mjs +2 -2
  32. package/mcp/adk-docs-corpus.json +1 -1
  33. package/package.json +210 -195
  34. package/scrapper-BHM1mCde.mjs +432 -0
  35. package/scrapper-BHM1mCde.mjs.map +1 -0
  36. package/scrapper-BeweWurk.js +462 -0
  37. package/scrapper-BeweWurk.js.map +1 -0
  38. package/{searxng-CyA-nEu5.mjs → searxng-BJFulNcK.mjs} +74 -84
  39. package/searxng-BJFulNcK.mjs.map +1 -0
  40. package/{searxng-Bkrwhwhw.js → searxng-B_D--V5q.js} +80 -84
  41. package/searxng-B_D--V5q.js.map +1 -0
  42. package/skills/adk-assembly/SKILL.md +2 -2
  43. package/searxng-Bkrwhwhw.js.map +0 -1
  44. package/searxng-CyA-nEu5.mjs.map +0 -1
package/CHANGELOG.md CHANGED
@@ -71,13 +71,55 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
71
71
 
72
72
  ### Added
73
73
 
74
+ - **Scrapper web-extraction tool battery (`@nhtio/adk/batteries/tools/scrapper`).** Tools for any
75
+ [Scrapper](https://github.com/amerkurev/scrapper) instance — a headless-browser service that gives
76
+ an agent browser-grade page reading (JS-rendered pages a plain fetch can't see) as a **stateless**
77
+ HTTP call: fresh incognito context per request, no stored session/cookies/credentials. Two verbs,
78
+ each with an async factory (accepts a dynamic-import `artifact` resolver) and a sync variant:
79
+ `createScrapperArticleTool`/`…Sync` (`/api/article`) and `createScrapperLinksTool`/`…Sync`
80
+ (`/api/links`). Like the SearXNG battery these are factories (not constants) and must not be
81
+ bulk-registered via `Object.values(batteries)`.
82
+ - **Per-parameter disposition** — for every modeled knob the factory chooses: `fixed` (pinned;
83
+ sent always, removed from the model schema), `defaults` (model-overridable), or open
84
+ (model-settable). `url` is always required; `fixedQuery` is a raw kebab passthrough for
85
+ un-modeled params, keeping the battery generic across instances/versions.
86
+ - **Two distinct header channels** — `config.headers` (static or sync/async resolver) authenticates
87
+ to the Scrapper *instance*; the `extra_http_headers` *param* (`'K:v;K2:v2'`) is what the scraper's
88
+ browser sends to the *target site*.
89
+ - Same SearXNG-style two-level output (`resultFormat` normalized/raw/either), `artifact` resolver,
90
+ and input/output middleware pipelines (`shortCircuit`, fresh runner per call). Errors degrade to
91
+ `Error:` strings (parses Scrapper's `{detail:[{msg}]}`; missing `url` → HTTP 422); bad config →
92
+ `E_INVALID_SCRAPPER_CONFIG`. Documented as a featured-battery page with TSDoc `@warning`s for the
93
+ `scroll_down`-needs-`sleep` and instance-relative-URI gotchas. Cross-env unit spec (stubbed
94
+ `fetch`, disposition, resolver, all-three-artifact round-trips) + env-gated live integration spec
95
+ (`TEST_SCRAPPER_URL` / `TEST_SCRAPPER_HEADERS`).
96
+
97
+ - **Web-retrieval RAG glue (`@nhtio/adk/batteries/tools/web_retrieval`).** The shared seam from
98
+ search/scrape results to turn `Retrievable`s, used by both the Scrapper and SearXNG batteries.
99
+ Pure converters — `searxngResultsToRetrievables`, `scrapperArticleToRetrievable`,
100
+ `scrapperLinksToRetrievables` — return plain `RawRetrievable[]` (zero core-class instantiation;
101
+ core referenced as `import type` only). `storeRetrievables(ctx, raws, { retrievable })` constructs
102
+ and stores records via a **resolver-injected** `Retrievable` constructor (ctor / sync / async /
103
+ dynamic-import), so the module never value-imports core. Long page text becomes a reader-backed
104
+ `SpooledArtifact` via a caller `spool` hook (the converter recommends an open
105
+ `ArtifactConstructorResolver` for the content — markdown/json/text — so a consumer's own subclass
106
+ works unchanged; no chunker). Web content defaults to `trustTier: 'third-party-public'` (a
107
+ constant, not URL inference — CONTRIBUTING DD#12).
108
+
109
+ - **Shared tool-battery helpers (`@nhtio/adk/batteries/tools/_shared`).** Internal building blocks
110
+ for the configured-HTTP tool batteries: `resolveArtifact`/`resolveArtifactSync` (resolver → sync
111
+ `() => Ctor`), the onion middleware-pipeline runners (fresh runner per call, short-circuit +
112
+ non-terminal detection), header resolution, and the `ArtifactResolver`/`SyncArtifactResolver`
113
+ types. SearXNG and Scrapper both build on it instead of carrying copies.
114
+
74
115
  - **SearXNG search tool battery (`@nhtio/adk/batteries/tools/searxng`).** A web-search tool for any
75
- [SearXNG](https://docs.searxng.org/dev/search_api.html) instance, exposed via a **factory** —
76
- `createSearxngSearchTool(config)` rather than a ready-made constant. It is the first
77
- factory-style tool battery: a search tool has to know *which* instance to query and is usually
78
- behind custom authentication, so it needs per-deployment config that cannot be baked in at module
79
- load. Because it exports a factory (not a `Tool`), it must not be bulk-registered via
80
- `Object.values(batteries)` — call the factory first, then register the returned tool.
116
+ [SearXNG](https://docs.searxng.org/dev/search_api.html) instance, exposed via **factories** —
117
+ async `createSearxngSearchTool(config)` and sync `createSearxngSearchToolSync(config)` rather
118
+ than a ready-made constant. It is the first factory-style tool battery: a search tool has to know
119
+ *which* instance to query and is usually behind custom authentication, so it needs per-deployment
120
+ config that cannot be baked in at module load. Because it exports factories (not a `Tool`), they
121
+ must not be bulk-registered via `Object.values(batteries)` — call a factory first, then register
122
+ the returned tool.
81
123
  - **Custom-header auth** — `config.headers` accepts a static `Record<string,string>` or a
82
124
  sync/async resolver (`() => headers | Promise<headers>`); the resolver runs on every search, so
83
125
  refreshable bearer tokens work. Caller headers override the default `Accept`/`User-Agent`.
@@ -92,9 +134,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
92
134
  hit); output stages filter/re-rank `ctx.results`, mutate `ctx.raw`, or set `ctx.output` verbatim
93
135
  (e.g. rendered markdown). A `ctx.stash` Map carries across both; a fresh runner is minted per
94
136
  invocation (middleware runners are single-use).
95
- - **Configurable spool artifact** — `config.artifactConstructor` (default `() => SpooledJsonArtifact`)
96
- is passed straight through to the `Tool`; set `() => SpooledMarkdownArtifact` or
97
- `() => SpooledArtifact` to match a custom `outputPipeline` render.
137
+ - **Configurable spool artifact (resolver)** — `config.artifact` (default `() => SpooledJsonArtifact`)
138
+ is an open `ArtifactConstructorResolver`: a ctor, a sync resolver, or via the async factory —
139
+ an async/dynamic-import resolver (`() => import('@nhtio/adk/spooled_artifact').then(m => m.SpooledMarkdownArtifact)`),
140
+ so a consumer's own `SpooledArtifact` subclass works with no battery change. The async factory
141
+ resolves it before building the `Tool` (whose `artifactConstructor` must be sync); the sync
142
+ factory accepts only the sync subset.
98
143
  - **Graceful failures** — a disabled-JSON instance (SearXNG disables JSON by default → HTTP 403),
99
144
  network errors, timeouts, and thrown pipeline stages all return `Error:` strings the model can
100
145
  react to; only malformed args throw (`E_INVALID_TOOL_ARGS`). Invalid config throws the
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Cross-battery helpers shared by the configured HTTP tool batteries (SearXNG, Scrapper, …).
3
+ *
4
+ * @module @nhtio/adk/batteries/tools/_shared
5
+ *
6
+ * @remarks
7
+ * These are internal building blocks for the *factory-style* tool batteries — the ones that talk
8
+ * to a configured HTTP instance behind custom auth and expose input/output middleware pipelines.
9
+ * Rather than each battery carry its own copy, the common machinery lives here:
10
+ *
11
+ * - {@link resolveArtifact} / {@link resolveArtifactSync} — turn an {@link ArtifactResolver}
12
+ * (a constructor, a sync resolver, or an async / dynamic-import resolver) into the **sync**
13
+ * `() => SpooledArtifactConstructor` that `Tool.artifactConstructor` requires. Mirrors the vector
14
+ * battery's `resolveClientCtor`.
15
+ * - {@link resolveHeaders} — collapse a static header object or a (sync/async) resolver into a
16
+ * plain header record for one request (refreshable-auth friendly).
17
+ * - {@link runInputPipeline} / {@link runOutputPipeline} — the onion middleware runners (fresh
18
+ * runner per call, short-circuit + non-terminal detection), generic over the context type.
19
+ *
20
+ * This module imports harness primitives only through their specific subpath barrels
21
+ * (`@nhtio/adk/spooled_artifact`, `@nhtio/adk/forge`, `@nhtio/adk/guards`) per the batteries
22
+ * barrel-only rule.
23
+ */
24
+ import { Middleware } from '@nhtio/middleware';
25
+ import type { NextFn } from '@nhtio/middleware';
26
+ import type { SpooledArtifactConstructor } from "../../../forge";
27
+ /** A static set of request headers (used for custom instance authentication). */
28
+ export type ToolHeaders = Record<string, string>;
29
+ /**
30
+ * A resolver returning request headers, sync or async. Use this form when the auth token is
31
+ * refreshable — the resolver runs on every request, so a fresh token can be minted per call.
32
+ */
33
+ export type ToolHeadersResolver = () => ToolHeaders | Promise<ToolHeaders>;
34
+ /**
35
+ * Resolve the configured headers (a static object or a sync/async resolver) for a single request.
36
+ *
37
+ * @param headers - The static header record, the resolver, or `undefined`.
38
+ * @returns A fresh, owned copy of the resolved headers (`{}` when none supplied).
39
+ */
40
+ export declare const resolveHeaders: (headers: ToolHeaders | ToolHeadersResolver | undefined) => Promise<ToolHeaders>;
41
+ /** Convenience alias for the spooled-artifact constructor a tool wraps its output in. */
42
+ export type SpooledArtifactCtor = SpooledArtifactConstructor;
43
+ /**
44
+ * The artifact configuration accepted by a factory: a constructor, a sync resolver, or an async /
45
+ * dynamic-import resolver (which may yield a module namespace whose `default` is the constructor).
46
+ *
47
+ * @remarks
48
+ * Mirrors the vector battery's `client` resolver and `Tool.artifactConstructor`'s indirection. The
49
+ * async form lets a consumer `() => import('@nhtio/adk/spooled_artifact').then(m => m.SpooledMarkdownArtifact)`
50
+ * so the artifact class never enters their static module graph.
51
+ */
52
+ export type ArtifactResolver = SpooledArtifactCtor | (() => SpooledArtifactCtor | {
53
+ default: SpooledArtifactCtor;
54
+ }) | (() => Promise<SpooledArtifactCtor | {
55
+ default: SpooledArtifactCtor;
56
+ }>);
57
+ /** The sync subset of {@link ArtifactResolver} — a constructor or a sync resolver (no Promise). */
58
+ export type SyncArtifactResolver = SpooledArtifactCtor | (() => SpooledArtifactCtor | {
59
+ default: SpooledArtifactCtor;
60
+ });
61
+ /**
62
+ * Resolve an {@link ArtifactResolver} to the **sync** `() => SpooledArtifactCtor` that
63
+ * `Tool.artifactConstructor` requires (the wrap-site and the construction-time validator both
64
+ * invoke it synchronously, so an async resolver cannot be passed straight through).
65
+ *
66
+ * @remarks
67
+ * A bare constructor is itself a function, so it is distinguished from a resolver via
68
+ * `SpooledArtifact.isSpooledArtifactConstructor` (the same duck-typed guard the core validator
69
+ * uses) rather than by arity. Async because a dynamic-import resolver must be awaited here.
70
+ *
71
+ * @param resolver - The artifact configuration. When `undefined`, callers should fall back to
72
+ * their own default (this function rejects `undefined` so the default lives with the caller).
73
+ * @param onInvalid - Throws a battery-scoped error; receives a human-readable reason.
74
+ * @returns A sync `() => SpooledArtifactCtor` suitable for `Tool.artifactConstructor`.
75
+ */
76
+ export declare const resolveArtifact: (resolver: ArtifactResolver, onInvalid: (reason: string) => never) => Promise<() => SpooledArtifactCtor>;
77
+ /**
78
+ * Synchronous {@link resolveArtifact}: accepts only the {@link SyncArtifactResolver} subset and
79
+ * throws (via `onInvalid`) on an async resolver — a runtime guard for JS callers who bypass the
80
+ * compile-time narrowing.
81
+ *
82
+ * @param resolver - A constructor or a sync resolver.
83
+ * @param onInvalid - Throws a battery-scoped error; receives a human-readable reason.
84
+ * @returns A sync `() => SpooledArtifactCtor` suitable for `Tool.artifactConstructor`.
85
+ */
86
+ export declare const resolveArtifactSync: (resolver: SyncArtifactResolver, onInvalid: (reason: string) => never) => (() => SpooledArtifactCtor);
87
+ /** `true` when `value` is the short-circuit sentinel produced by {@link makeShortCircuit}. */
88
+ export declare const isShortCircuit: (value: unknown) => value is {
89
+ result: string;
90
+ };
91
+ /**
92
+ * Build a `shortCircuit(result)` function for an input-pipeline context. Calling it throws the
93
+ * internal sentinel, which {@link runInputPipeline} catches and converts into the verbatim result
94
+ * (skipping the HTTP request entirely — e.g. a cache hit).
95
+ *
96
+ * @returns A function that, when called with a result string, throws the short-circuit sentinel.
97
+ */
98
+ export declare const makeShortCircuit: () => ((result: string) => never);
99
+ /** A generic onion middleware stage over a mutable context `C`. */
100
+ export type MiddlewareFn<C> = (ctx: C, next: NextFn) => void | Promise<void>;
101
+ /**
102
+ * Run an input pipeline over `ctx`. Returns the short-circuit string when a stage short-circuited,
103
+ * or `undefined` when the pipeline reached its terminal handler. A non-terminal pipeline (a stage
104
+ * that neither called `next()` nor short-circuited) throws — the caller converts it to an
105
+ * `Error:` string.
106
+ *
107
+ * @param mw - The `Middleware` instance holding the stages (a fresh `.runner()` is minted here).
108
+ * @param ctx - The mutable input context handed to each stage.
109
+ * @param label - Battery name, used in the non-terminal error message.
110
+ * @returns The short-circuit result string, or `undefined` if the pipeline ran to completion.
111
+ */
112
+ export declare const runInputPipeline: <C>(mw: Middleware<MiddlewareFn<C>>, ctx: C, label: string) => Promise<string | undefined>;
113
+ /**
114
+ * Run an output pipeline over `ctx`; rethrow any stage error to the caller's try/catch. A
115
+ * non-terminal pipeline (no `next()`) throws.
116
+ *
117
+ * @param mw - The `Middleware` instance holding the stages (a fresh `.runner()` is minted here).
118
+ * @param ctx - The mutable output context handed to each stage.
119
+ * @param label - Battery name, used in the non-terminal error message.
120
+ */
121
+ export declare const runOutputPipeline: <C>(mw: Middleware<MiddlewareFn<C>>, ctx: C, label: string) => Promise<void>;
@@ -0,0 +1,157 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ require("../../chunk-Ble4zEEl.js");
3
+ const require_tool_registry = require("../../tool_registry-CKJPze3j.js");
4
+ const require_spooled_artifact = require("../../spooled_artifact-DX8LLyUX.js");
5
+ require("../../guards.cjs");
6
+ require("../../spooled_artifact.cjs");
7
+ //#region src/batteries/tools/_shared/index.ts
8
+ /**
9
+ * Resolve the configured headers (a static object or a sync/async resolver) for a single request.
10
+ *
11
+ * @param headers - The static header record, the resolver, or `undefined`.
12
+ * @returns A fresh, owned copy of the resolved headers (`{}` when none supplied).
13
+ */
14
+ var resolveHeaders = async (headers) => {
15
+ if (typeof headers === "function") return { ...await headers() };
16
+ return { ...headers ?? {} };
17
+ };
18
+ /** Unwrap a resolved value that may be a module namespace whose `default` is the constructor. */
19
+ var unwrapDefault = (value) => {
20
+ if (require_tool_registry.isObject(value) && "default" in value) {
21
+ const def = value.default;
22
+ if (require_spooled_artifact.SpooledArtifact.isSpooledArtifactConstructor(def)) return def;
23
+ }
24
+ return value;
25
+ };
26
+ /**
27
+ * Resolve an {@link ArtifactResolver} to the **sync** `() => SpooledArtifactCtor` that
28
+ * `Tool.artifactConstructor` requires (the wrap-site and the construction-time validator both
29
+ * invoke it synchronously, so an async resolver cannot be passed straight through).
30
+ *
31
+ * @remarks
32
+ * A bare constructor is itself a function, so it is distinguished from a resolver via
33
+ * `SpooledArtifact.isSpooledArtifactConstructor` (the same duck-typed guard the core validator
34
+ * uses) rather than by arity. Async because a dynamic-import resolver must be awaited here.
35
+ *
36
+ * @param resolver - The artifact configuration. When `undefined`, callers should fall back to
37
+ * their own default (this function rejects `undefined` so the default lives with the caller).
38
+ * @param onInvalid - Throws a battery-scoped error; receives a human-readable reason.
39
+ * @returns A sync `() => SpooledArtifactCtor` suitable for `Tool.artifactConstructor`.
40
+ */
41
+ var resolveArtifact = async (resolver, onInvalid) => {
42
+ if (require_spooled_artifact.SpooledArtifact.isSpooledArtifactConstructor(resolver)) {
43
+ const ctor = resolver;
44
+ return () => ctor;
45
+ }
46
+ if (typeof resolver !== "function") onInvalid("artifact must be a SpooledArtifact constructor or a resolver returning one");
47
+ let resolved;
48
+ try {
49
+ resolved = await resolver();
50
+ } catch (err) {
51
+ onInvalid(`artifact resolver threw: ${require_tool_registry.isError(err) ? err.message : String(err)}`);
52
+ }
53
+ resolved = unwrapDefault(resolved);
54
+ if (!require_spooled_artifact.SpooledArtifact.isSpooledArtifactConstructor(resolved)) onInvalid("artifact resolver did not resolve to a SpooledArtifact constructor");
55
+ const ctor = resolved;
56
+ return () => ctor;
57
+ };
58
+ /**
59
+ * Synchronous {@link resolveArtifact}: accepts only the {@link SyncArtifactResolver} subset and
60
+ * throws (via `onInvalid`) on an async resolver — a runtime guard for JS callers who bypass the
61
+ * compile-time narrowing.
62
+ *
63
+ * @param resolver - A constructor or a sync resolver.
64
+ * @param onInvalid - Throws a battery-scoped error; receives a human-readable reason.
65
+ * @returns A sync `() => SpooledArtifactCtor` suitable for `Tool.artifactConstructor`.
66
+ */
67
+ var resolveArtifactSync = (resolver, onInvalid) => {
68
+ if (require_spooled_artifact.SpooledArtifact.isSpooledArtifactConstructor(resolver)) {
69
+ const ctor = resolver;
70
+ return () => ctor;
71
+ }
72
+ if (typeof resolver !== "function") onInvalid("artifact must be a SpooledArtifact constructor or a resolver returning one");
73
+ let resolved;
74
+ try {
75
+ resolved = resolver();
76
+ } catch (err) {
77
+ onInvalid(`artifact resolver threw: ${require_tool_registry.isError(err) ? err.message : String(err)}`);
78
+ }
79
+ if (require_tool_registry.isInstanceOf(resolved, "Promise", Promise)) onInvalid("artifact resolver is async; use the async factory variant for dynamic-import resolvers");
80
+ resolved = unwrapDefault(resolved);
81
+ if (!require_spooled_artifact.SpooledArtifact.isSpooledArtifactConstructor(resolved)) onInvalid("artifact resolver did not resolve to a SpooledArtifact constructor");
82
+ const ctor = resolved;
83
+ return () => ctor;
84
+ };
85
+ /** Internal sentinel a short-circuiting input stage throws to unwind the pipeline immediately. */
86
+ var SHORT_CIRCUIT = Symbol("adk.tools.shortCircuit");
87
+ /** `true` when `value` is the short-circuit sentinel produced by {@link makeShortCircuit}. */
88
+ var isShortCircuit = (value) => require_tool_registry.isObject(value) && value[SHORT_CIRCUIT] === true;
89
+ /**
90
+ * Build a `shortCircuit(result)` function for an input-pipeline context. Calling it throws the
91
+ * internal sentinel, which {@link runInputPipeline} catches and converts into the verbatim result
92
+ * (skipping the HTTP request entirely — e.g. a cache hit).
93
+ *
94
+ * @returns A function that, when called with a result string, throws the short-circuit sentinel.
95
+ */
96
+ var makeShortCircuit = () => {
97
+ return (result) => {
98
+ throw {
99
+ [SHORT_CIRCUIT]: true,
100
+ result
101
+ };
102
+ };
103
+ };
104
+ /**
105
+ * Run an input pipeline over `ctx`. Returns the short-circuit string when a stage short-circuited,
106
+ * or `undefined` when the pipeline reached its terminal handler. A non-terminal pipeline (a stage
107
+ * that neither called `next()` nor short-circuited) throws — the caller converts it to an
108
+ * `Error:` string.
109
+ *
110
+ * @param mw - The `Middleware` instance holding the stages (a fresh `.runner()` is minted here).
111
+ * @param ctx - The mutable input context handed to each stage.
112
+ * @param label - Battery name, used in the non-terminal error message.
113
+ * @returns The short-circuit result string, or `undefined` if the pipeline ran to completion.
114
+ */
115
+ var runInputPipeline = async (mw, ctx, label) => {
116
+ let reached = false;
117
+ let caught;
118
+ await mw.runner().errorHandler(async (error) => {
119
+ caught = error;
120
+ }).finalHandler(async () => {
121
+ reached = true;
122
+ }).run((fn, next) => Promise.resolve(fn(ctx, next)));
123
+ if (caught !== void 0) {
124
+ if (isShortCircuit(caught)) return caught.result;
125
+ throw caught;
126
+ }
127
+ if (!reached) throw new Error(`${label} input pipeline did not call next() and did not short-circuit.`);
128
+ };
129
+ /**
130
+ * Run an output pipeline over `ctx`; rethrow any stage error to the caller's try/catch. A
131
+ * non-terminal pipeline (no `next()`) throws.
132
+ *
133
+ * @param mw - The `Middleware` instance holding the stages (a fresh `.runner()` is minted here).
134
+ * @param ctx - The mutable output context handed to each stage.
135
+ * @param label - Battery name, used in the non-terminal error message.
136
+ */
137
+ var runOutputPipeline = async (mw, ctx, label) => {
138
+ let reached = false;
139
+ let caught;
140
+ await mw.runner().errorHandler(async (error) => {
141
+ caught = error;
142
+ }).finalHandler(async () => {
143
+ reached = true;
144
+ }).run((fn, next) => Promise.resolve(fn(ctx, next)));
145
+ if (caught !== void 0) throw caught;
146
+ if (!reached) throw new Error(`${label} output pipeline did not call next().`);
147
+ };
148
+ //#endregion
149
+ exports.isShortCircuit = isShortCircuit;
150
+ exports.makeShortCircuit = makeShortCircuit;
151
+ exports.resolveArtifact = resolveArtifact;
152
+ exports.resolveArtifactSync = resolveArtifactSync;
153
+ exports.resolveHeaders = resolveHeaders;
154
+ exports.runInputPipeline = runInputPipeline;
155
+ exports.runOutputPipeline = runOutputPipeline;
156
+
157
+ //# sourceMappingURL=_shared.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_shared.cjs","names":[],"sources":["../../../src/batteries/tools/_shared/index.ts"],"sourcesContent":["/**\n * Cross-battery helpers shared by the configured HTTP tool batteries (SearXNG, Scrapper, …).\n *\n * @module @nhtio/adk/batteries/tools/_shared\n *\n * @remarks\n * These are internal building blocks for the *factory-style* tool batteries — the ones that talk\n * to a configured HTTP instance behind custom auth and expose input/output middleware pipelines.\n * Rather than each battery carry its own copy, the common machinery lives here:\n *\n * - {@link resolveArtifact} / {@link resolveArtifactSync} — turn an {@link ArtifactResolver}\n * (a constructor, a sync resolver, or an async / dynamic-import resolver) into the **sync**\n * `() => SpooledArtifactConstructor` that `Tool.artifactConstructor` requires. Mirrors the vector\n * battery's `resolveClientCtor`.\n * - {@link resolveHeaders} — collapse a static header object or a (sync/async) resolver into a\n * plain header record for one request (refreshable-auth friendly).\n * - {@link runInputPipeline} / {@link runOutputPipeline} — the onion middleware runners (fresh\n * runner per call, short-circuit + non-terminal detection), generic over the context type.\n *\n * This module imports harness primitives only through their specific subpath barrels\n * (`@nhtio/adk/spooled_artifact`, `@nhtio/adk/forge`, `@nhtio/adk/guards`) per the batteries\n * barrel-only rule.\n */\n\nimport { Middleware } from '@nhtio/middleware'\nimport { SpooledArtifact } from '@nhtio/adk/spooled_artifact'\nimport { isError, isObject, isInstanceOf } from '@nhtio/adk/guards'\nimport type { NextFn } from '@nhtio/middleware'\nimport type { SpooledArtifactConstructor } from '@nhtio/adk/forge'\n\n// ── Header resolution ────────────────────────────────────────────────────────\n\n/** A static set of request headers (used for custom instance authentication). */\nexport type ToolHeaders = Record<string, string>\n\n/**\n * A resolver returning request headers, sync or async. Use this form when the auth token is\n * refreshable — the resolver runs on every request, so a fresh token can be minted per call.\n */\nexport type ToolHeadersResolver = () => ToolHeaders | Promise<ToolHeaders>\n\n/**\n * Resolve the configured headers (a static object or a sync/async resolver) for a single request.\n *\n * @param headers - The static header record, the resolver, or `undefined`.\n * @returns A fresh, owned copy of the resolved headers (`{}` when none supplied).\n */\nexport const resolveHeaders = async (\n headers: ToolHeaders | ToolHeadersResolver | undefined\n): Promise<ToolHeaders> => {\n if (typeof headers === 'function') return { ...(await headers()) }\n return { ...(headers ?? {}) }\n}\n\n// ── Artifact resolver ────────────────────────────────────────────────────────\n\n/** Convenience alias for the spooled-artifact constructor a tool wraps its output in. */\nexport type SpooledArtifactCtor = SpooledArtifactConstructor\n\n/**\n * The artifact configuration accepted by a factory: a constructor, a sync resolver, or an async /\n * dynamic-import resolver (which may yield a module namespace whose `default` is the constructor).\n *\n * @remarks\n * Mirrors the vector battery's `client` resolver and `Tool.artifactConstructor`'s indirection. The\n * async form lets a consumer `() => import('@nhtio/adk/spooled_artifact').then(m => m.SpooledMarkdownArtifact)`\n * so the artifact class never enters their static module graph.\n */\nexport type ArtifactResolver =\n | SpooledArtifactCtor\n | (() => SpooledArtifactCtor | { default: SpooledArtifactCtor })\n | (() => Promise<SpooledArtifactCtor | { default: SpooledArtifactCtor }>)\n\n/** The sync subset of {@link ArtifactResolver} — a constructor or a sync resolver (no Promise). */\nexport type SyncArtifactResolver =\n | SpooledArtifactCtor\n | (() => SpooledArtifactCtor | { default: SpooledArtifactCtor })\n\n/** Unwrap a resolved value that may be a module namespace whose `default` is the constructor. */\nconst unwrapDefault = (value: unknown): unknown => {\n if (isObject(value) && 'default' in value) {\n const def = (value as { default?: unknown }).default\n if (SpooledArtifact.isSpooledArtifactConstructor(def)) return def\n }\n return value\n}\n\n/**\n * Resolve an {@link ArtifactResolver} to the **sync** `() => SpooledArtifactCtor` that\n * `Tool.artifactConstructor` requires (the wrap-site and the construction-time validator both\n * invoke it synchronously, so an async resolver cannot be passed straight through).\n *\n * @remarks\n * A bare constructor is itself a function, so it is distinguished from a resolver via\n * `SpooledArtifact.isSpooledArtifactConstructor` (the same duck-typed guard the core validator\n * uses) rather than by arity. Async because a dynamic-import resolver must be awaited here.\n *\n * @param resolver - The artifact configuration. When `undefined`, callers should fall back to\n * their own default (this function rejects `undefined` so the default lives with the caller).\n * @param onInvalid - Throws a battery-scoped error; receives a human-readable reason.\n * @returns A sync `() => SpooledArtifactCtor` suitable for `Tool.artifactConstructor`.\n */\nexport const resolveArtifact = async (\n resolver: ArtifactResolver,\n onInvalid: (reason: string) => never\n): Promise<() => SpooledArtifactCtor> => {\n // A constructor: hand back a thunk that returns it.\n if (SpooledArtifact.isSpooledArtifactConstructor(resolver)) {\n const ctor = resolver\n return () => ctor\n }\n // Otherwise it must be a resolver function.\n if (typeof resolver !== 'function') {\n onInvalid('artifact must be a SpooledArtifact constructor or a resolver returning one')\n }\n let resolved: unknown\n try {\n resolved = await (resolver as () => unknown)()\n } catch (err) {\n onInvalid(`artifact resolver threw: ${isError(err) ? err.message : String(err)}`)\n }\n resolved = unwrapDefault(resolved)\n if (!SpooledArtifact.isSpooledArtifactConstructor(resolved)) {\n onInvalid('artifact resolver did not resolve to a SpooledArtifact constructor')\n }\n const ctor = resolved as SpooledArtifactCtor\n return () => ctor\n}\n\n/**\n * Synchronous {@link resolveArtifact}: accepts only the {@link SyncArtifactResolver} subset and\n * throws (via `onInvalid`) on an async resolver — a runtime guard for JS callers who bypass the\n * compile-time narrowing.\n *\n * @param resolver - A constructor or a sync resolver.\n * @param onInvalid - Throws a battery-scoped error; receives a human-readable reason.\n * @returns A sync `() => SpooledArtifactCtor` suitable for `Tool.artifactConstructor`.\n */\nexport const resolveArtifactSync = (\n resolver: SyncArtifactResolver,\n onInvalid: (reason: string) => never\n): (() => SpooledArtifactCtor) => {\n if (SpooledArtifact.isSpooledArtifactConstructor(resolver)) {\n const ctor = resolver\n return () => ctor\n }\n if (typeof resolver !== 'function') {\n onInvalid('artifact must be a SpooledArtifact constructor or a resolver returning one')\n }\n let resolved: unknown\n try {\n resolved = (resolver as () => unknown)()\n } catch (err) {\n onInvalid(`artifact resolver threw: ${isError(err) ? err.message : String(err)}`)\n }\n if (isInstanceOf(resolved, 'Promise', Promise)) {\n onInvalid(\n 'artifact resolver is async; use the async factory variant for dynamic-import resolvers'\n )\n }\n resolved = unwrapDefault(resolved)\n if (!SpooledArtifact.isSpooledArtifactConstructor(resolved)) {\n onInvalid('artifact resolver did not resolve to a SpooledArtifact constructor')\n }\n const ctor = resolved as SpooledArtifactCtor\n return () => ctor\n}\n\n// ── Middleware pipeline runners ──────────────────────────────────────────────\n\n/** Internal sentinel a short-circuiting input stage throws to unwind the pipeline immediately. */\nconst SHORT_CIRCUIT = Symbol('adk.tools.shortCircuit')\n\ninterface ShortCircuitSignal {\n [SHORT_CIRCUIT]: true\n result: string\n}\n\n/** `true` when `value` is the short-circuit sentinel produced by {@link makeShortCircuit}. */\nexport const isShortCircuit = (value: unknown): value is { result: string } =>\n isObject(value) && (value as Record<symbol, unknown>)[SHORT_CIRCUIT] === true\n\n/**\n * Build a `shortCircuit(result)` function for an input-pipeline context. Calling it throws the\n * internal sentinel, which {@link runInputPipeline} catches and converts into the verbatim result\n * (skipping the HTTP request entirely — e.g. a cache hit).\n *\n * @returns A function that, when called with a result string, throws the short-circuit sentinel.\n */\nexport const makeShortCircuit = (): ((result: string) => never) => {\n return (result: string): never => {\n const signal: ShortCircuitSignal = { [SHORT_CIRCUIT]: true, result }\n throw signal\n }\n}\n\n/** A generic onion middleware stage over a mutable context `C`. */\nexport type MiddlewareFn<C> = (ctx: C, next: NextFn) => void | Promise<void>\n\n/**\n * Run an input pipeline over `ctx`. Returns the short-circuit string when a stage short-circuited,\n * or `undefined` when the pipeline reached its terminal handler. A non-terminal pipeline (a stage\n * that neither called `next()` nor short-circuited) throws — the caller converts it to an\n * `Error:` string.\n *\n * @param mw - The `Middleware` instance holding the stages (a fresh `.runner()` is minted here).\n * @param ctx - The mutable input context handed to each stage.\n * @param label - Battery name, used in the non-terminal error message.\n * @returns The short-circuit result string, or `undefined` if the pipeline ran to completion.\n */\nexport const runInputPipeline = async <C>(\n mw: Middleware<MiddlewareFn<C>>,\n ctx: C,\n label: string\n): Promise<string | undefined> => {\n let reached = false\n let caught: unknown\n await mw\n .runner()\n .errorHandler(async (error: unknown) => {\n caught = error\n })\n .finalHandler(async () => {\n reached = true\n })\n .run((fn, next) => Promise.resolve(fn(ctx, next)))\n\n if (caught !== undefined) {\n if (isShortCircuit(caught)) return caught.result\n throw caught\n }\n if (!reached) {\n throw new Error(`${label} input pipeline did not call next() and did not short-circuit.`)\n }\n return undefined\n}\n\n/**\n * Run an output pipeline over `ctx`; rethrow any stage error to the caller's try/catch. A\n * non-terminal pipeline (no `next()`) throws.\n *\n * @param mw - The `Middleware` instance holding the stages (a fresh `.runner()` is minted here).\n * @param ctx - The mutable output context handed to each stage.\n * @param label - Battery name, used in the non-terminal error message.\n */\nexport const runOutputPipeline = async <C>(\n mw: Middleware<MiddlewareFn<C>>,\n ctx: C,\n label: string\n): Promise<void> => {\n let reached = false\n let caught: unknown\n await mw\n .runner()\n .errorHandler(async (error: unknown) => {\n caught = error\n })\n .finalHandler(async () => {\n reached = true\n })\n .run((fn, next) => Promise.resolve(fn(ctx, next)))\n\n if (caught !== undefined) throw caught\n if (!reached) throw new Error(`${label} output pipeline did not call next().`)\n}\n"],"mappings":";;;;;;;;;;;;;AA+CA,IAAa,iBAAiB,OAC5B,YACyB;CACzB,IAAI,OAAO,YAAY,YAAY,OAAO,EAAE,GAAI,MAAM,QAAQ,EAAG;CACjE,OAAO,EAAE,GAAI,WAAW,CAAC,EAAG;AAC9B;;AA2BA,IAAM,iBAAiB,UAA4B;CACjD,IAAI,sBAAA,SAAS,KAAK,KAAK,aAAa,OAAO;EACzC,MAAM,MAAO,MAAgC;EAC7C,IAAI,yBAAA,gBAAgB,6BAA6B,GAAG,GAAG,OAAO;CAChE;CACA,OAAO;AACT;;;;;;;;;;;;;;;;AAiBA,IAAa,kBAAkB,OAC7B,UACA,cACuC;CAEvC,IAAI,yBAAA,gBAAgB,6BAA6B,QAAQ,GAAG;EAC1D,MAAM,OAAO;EACb,aAAa;CACf;CAEA,IAAI,OAAO,aAAa,YACtB,UAAU,4EAA4E;CAExF,IAAI;CACJ,IAAI;EACF,WAAW,MAAO,SAA2B;CAC/C,SAAS,KAAK;EACZ,UAAU,4BAA4B,sBAAA,QAAQ,GAAG,IAAI,IAAI,UAAU,OAAO,GAAG,GAAG;CAClF;CACA,WAAW,cAAc,QAAQ;CACjC,IAAI,CAAC,yBAAA,gBAAgB,6BAA6B,QAAQ,GACxD,UAAU,oEAAoE;CAEhF,MAAM,OAAO;CACb,aAAa;AACf;;;;;;;;;;AAWA,IAAa,uBACX,UACA,cACgC;CAChC,IAAI,yBAAA,gBAAgB,6BAA6B,QAAQ,GAAG;EAC1D,MAAM,OAAO;EACb,aAAa;CACf;CACA,IAAI,OAAO,aAAa,YACtB,UAAU,4EAA4E;CAExF,IAAI;CACJ,IAAI;EACF,WAAY,SAA2B;CACzC,SAAS,KAAK;EACZ,UAAU,4BAA4B,sBAAA,QAAQ,GAAG,IAAI,IAAI,UAAU,OAAO,GAAG,GAAG;CAClF;CACA,IAAI,sBAAA,aAAa,UAAU,WAAW,OAAO,GAC3C,UACE,wFACF;CAEF,WAAW,cAAc,QAAQ;CACjC,IAAI,CAAC,yBAAA,gBAAgB,6BAA6B,QAAQ,GACxD,UAAU,oEAAoE;CAEhF,MAAM,OAAO;CACb,aAAa;AACf;;AAKA,IAAM,gBAAgB,OAAO,wBAAwB;;AAQrD,IAAa,kBAAkB,UAC7B,sBAAA,SAAS,KAAK,KAAM,MAAkC,mBAAmB;;;;;;;;AAS3E,IAAa,yBAAsD;CACjE,QAAQ,WAA0B;EAEhC,MAAM;IADgC,gBAAgB;GAAM;EACtD;CACR;AACF;;;;;;;;;;;;AAgBA,IAAa,mBAAmB,OAC9B,IACA,KACA,UACgC;CAChC,IAAI,UAAU;CACd,IAAI;CACJ,MAAM,GACH,OAAO,EACP,aAAa,OAAO,UAAmB;EACtC,SAAS;CACX,CAAC,EACA,aAAa,YAAY;EACxB,UAAU;CACZ,CAAC,EACA,KAAK,IAAI,SAAS,QAAQ,QAAQ,GAAG,KAAK,IAAI,CAAC,CAAC;CAEnD,IAAI,WAAW,KAAA,GAAW;EACxB,IAAI,eAAe,MAAM,GAAG,OAAO,OAAO;EAC1C,MAAM;CACR;CACA,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,GAAG,MAAM,+DAA+D;AAG5F;;;;;;;;;AAUA,IAAa,oBAAoB,OAC/B,IACA,KACA,UACkB;CAClB,IAAI,UAAU;CACd,IAAI;CACJ,MAAM,GACH,OAAO,EACP,aAAa,OAAO,UAAmB;EACtC,SAAS;CACX,CAAC,EACA,aAAa,YAAY;EACxB,UAAU;CACZ,CAAC,EACA,KAAK,IAAI,SAAS,QAAQ,QAAQ,GAAG,KAAK,IAAI,CAAC,CAAC;CAEnD,IAAI,WAAW,KAAA,GAAW,MAAM;CAChC,IAAI,CAAC,SAAS,MAAM,IAAI,MAAM,GAAG,MAAM,sCAAsC;AAC/E"}
@@ -0,0 +1,149 @@
1
+ import { c as isObject, o as isError, s as isInstanceOf } from "../../tool_registry-791Vrjtf.mjs";
2
+ import { t as SpooledArtifact } from "../../spooled_artifact-7eePq7JA.mjs";
3
+ import "../../guards.mjs";
4
+ import "../../spooled_artifact.mjs";
5
+ //#region src/batteries/tools/_shared/index.ts
6
+ /**
7
+ * Resolve the configured headers (a static object or a sync/async resolver) for a single request.
8
+ *
9
+ * @param headers - The static header record, the resolver, or `undefined`.
10
+ * @returns A fresh, owned copy of the resolved headers (`{}` when none supplied).
11
+ */
12
+ var resolveHeaders = async (headers) => {
13
+ if (typeof headers === "function") return { ...await headers() };
14
+ return { ...headers ?? {} };
15
+ };
16
+ /** Unwrap a resolved value that may be a module namespace whose `default` is the constructor. */
17
+ var unwrapDefault = (value) => {
18
+ if (isObject(value) && "default" in value) {
19
+ const def = value.default;
20
+ if (SpooledArtifact.isSpooledArtifactConstructor(def)) return def;
21
+ }
22
+ return value;
23
+ };
24
+ /**
25
+ * Resolve an {@link ArtifactResolver} to the **sync** `() => SpooledArtifactCtor` that
26
+ * `Tool.artifactConstructor` requires (the wrap-site and the construction-time validator both
27
+ * invoke it synchronously, so an async resolver cannot be passed straight through).
28
+ *
29
+ * @remarks
30
+ * A bare constructor is itself a function, so it is distinguished from a resolver via
31
+ * `SpooledArtifact.isSpooledArtifactConstructor` (the same duck-typed guard the core validator
32
+ * uses) rather than by arity. Async because a dynamic-import resolver must be awaited here.
33
+ *
34
+ * @param resolver - The artifact configuration. When `undefined`, callers should fall back to
35
+ * their own default (this function rejects `undefined` so the default lives with the caller).
36
+ * @param onInvalid - Throws a battery-scoped error; receives a human-readable reason.
37
+ * @returns A sync `() => SpooledArtifactCtor` suitable for `Tool.artifactConstructor`.
38
+ */
39
+ var resolveArtifact = async (resolver, onInvalid) => {
40
+ if (SpooledArtifact.isSpooledArtifactConstructor(resolver)) {
41
+ const ctor = resolver;
42
+ return () => ctor;
43
+ }
44
+ if (typeof resolver !== "function") onInvalid("artifact must be a SpooledArtifact constructor or a resolver returning one");
45
+ let resolved;
46
+ try {
47
+ resolved = await resolver();
48
+ } catch (err) {
49
+ onInvalid(`artifact resolver threw: ${isError(err) ? err.message : String(err)}`);
50
+ }
51
+ resolved = unwrapDefault(resolved);
52
+ if (!SpooledArtifact.isSpooledArtifactConstructor(resolved)) onInvalid("artifact resolver did not resolve to a SpooledArtifact constructor");
53
+ const ctor = resolved;
54
+ return () => ctor;
55
+ };
56
+ /**
57
+ * Synchronous {@link resolveArtifact}: accepts only the {@link SyncArtifactResolver} subset and
58
+ * throws (via `onInvalid`) on an async resolver — a runtime guard for JS callers who bypass the
59
+ * compile-time narrowing.
60
+ *
61
+ * @param resolver - A constructor or a sync resolver.
62
+ * @param onInvalid - Throws a battery-scoped error; receives a human-readable reason.
63
+ * @returns A sync `() => SpooledArtifactCtor` suitable for `Tool.artifactConstructor`.
64
+ */
65
+ var resolveArtifactSync = (resolver, onInvalid) => {
66
+ if (SpooledArtifact.isSpooledArtifactConstructor(resolver)) {
67
+ const ctor = resolver;
68
+ return () => ctor;
69
+ }
70
+ if (typeof resolver !== "function") onInvalid("artifact must be a SpooledArtifact constructor or a resolver returning one");
71
+ let resolved;
72
+ try {
73
+ resolved = resolver();
74
+ } catch (err) {
75
+ onInvalid(`artifact resolver threw: ${isError(err) ? err.message : String(err)}`);
76
+ }
77
+ if (isInstanceOf(resolved, "Promise", Promise)) onInvalid("artifact resolver is async; use the async factory variant for dynamic-import resolvers");
78
+ resolved = unwrapDefault(resolved);
79
+ if (!SpooledArtifact.isSpooledArtifactConstructor(resolved)) onInvalid("artifact resolver did not resolve to a SpooledArtifact constructor");
80
+ const ctor = resolved;
81
+ return () => ctor;
82
+ };
83
+ /** Internal sentinel a short-circuiting input stage throws to unwind the pipeline immediately. */
84
+ var SHORT_CIRCUIT = Symbol("adk.tools.shortCircuit");
85
+ /** `true` when `value` is the short-circuit sentinel produced by {@link makeShortCircuit}. */
86
+ var isShortCircuit = (value) => isObject(value) && value[SHORT_CIRCUIT] === true;
87
+ /**
88
+ * Build a `shortCircuit(result)` function for an input-pipeline context. Calling it throws the
89
+ * internal sentinel, which {@link runInputPipeline} catches and converts into the verbatim result
90
+ * (skipping the HTTP request entirely — e.g. a cache hit).
91
+ *
92
+ * @returns A function that, when called with a result string, throws the short-circuit sentinel.
93
+ */
94
+ var makeShortCircuit = () => {
95
+ return (result) => {
96
+ throw {
97
+ [SHORT_CIRCUIT]: true,
98
+ result
99
+ };
100
+ };
101
+ };
102
+ /**
103
+ * Run an input pipeline over `ctx`. Returns the short-circuit string when a stage short-circuited,
104
+ * or `undefined` when the pipeline reached its terminal handler. A non-terminal pipeline (a stage
105
+ * that neither called `next()` nor short-circuited) throws — the caller converts it to an
106
+ * `Error:` string.
107
+ *
108
+ * @param mw - The `Middleware` instance holding the stages (a fresh `.runner()` is minted here).
109
+ * @param ctx - The mutable input context handed to each stage.
110
+ * @param label - Battery name, used in the non-terminal error message.
111
+ * @returns The short-circuit result string, or `undefined` if the pipeline ran to completion.
112
+ */
113
+ var runInputPipeline = async (mw, ctx, label) => {
114
+ let reached = false;
115
+ let caught;
116
+ await mw.runner().errorHandler(async (error) => {
117
+ caught = error;
118
+ }).finalHandler(async () => {
119
+ reached = true;
120
+ }).run((fn, next) => Promise.resolve(fn(ctx, next)));
121
+ if (caught !== void 0) {
122
+ if (isShortCircuit(caught)) return caught.result;
123
+ throw caught;
124
+ }
125
+ if (!reached) throw new Error(`${label} input pipeline did not call next() and did not short-circuit.`);
126
+ };
127
+ /**
128
+ * Run an output pipeline over `ctx`; rethrow any stage error to the caller's try/catch. A
129
+ * non-terminal pipeline (no `next()`) throws.
130
+ *
131
+ * @param mw - The `Middleware` instance holding the stages (a fresh `.runner()` is minted here).
132
+ * @param ctx - The mutable output context handed to each stage.
133
+ * @param label - Battery name, used in the non-terminal error message.
134
+ */
135
+ var runOutputPipeline = async (mw, ctx, label) => {
136
+ let reached = false;
137
+ let caught;
138
+ await mw.runner().errorHandler(async (error) => {
139
+ caught = error;
140
+ }).finalHandler(async () => {
141
+ reached = true;
142
+ }).run((fn, next) => Promise.resolve(fn(ctx, next)));
143
+ if (caught !== void 0) throw caught;
144
+ if (!reached) throw new Error(`${label} output pipeline did not call next().`);
145
+ };
146
+ //#endregion
147
+ export { isShortCircuit, makeShortCircuit, resolveArtifact, resolveArtifactSync, resolveHeaders, runInputPipeline, runOutputPipeline };
148
+
149
+ //# sourceMappingURL=_shared.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_shared.mjs","names":[],"sources":["../../../src/batteries/tools/_shared/index.ts"],"sourcesContent":["/**\n * Cross-battery helpers shared by the configured HTTP tool batteries (SearXNG, Scrapper, …).\n *\n * @module @nhtio/adk/batteries/tools/_shared\n *\n * @remarks\n * These are internal building blocks for the *factory-style* tool batteries — the ones that talk\n * to a configured HTTP instance behind custom auth and expose input/output middleware pipelines.\n * Rather than each battery carry its own copy, the common machinery lives here:\n *\n * - {@link resolveArtifact} / {@link resolveArtifactSync} — turn an {@link ArtifactResolver}\n * (a constructor, a sync resolver, or an async / dynamic-import resolver) into the **sync**\n * `() => SpooledArtifactConstructor` that `Tool.artifactConstructor` requires. Mirrors the vector\n * battery's `resolveClientCtor`.\n * - {@link resolveHeaders} — collapse a static header object or a (sync/async) resolver into a\n * plain header record for one request (refreshable-auth friendly).\n * - {@link runInputPipeline} / {@link runOutputPipeline} — the onion middleware runners (fresh\n * runner per call, short-circuit + non-terminal detection), generic over the context type.\n *\n * This module imports harness primitives only through their specific subpath barrels\n * (`@nhtio/adk/spooled_artifact`, `@nhtio/adk/forge`, `@nhtio/adk/guards`) per the batteries\n * barrel-only rule.\n */\n\nimport { Middleware } from '@nhtio/middleware'\nimport { SpooledArtifact } from '@nhtio/adk/spooled_artifact'\nimport { isError, isObject, isInstanceOf } from '@nhtio/adk/guards'\nimport type { NextFn } from '@nhtio/middleware'\nimport type { SpooledArtifactConstructor } from '@nhtio/adk/forge'\n\n// ── Header resolution ────────────────────────────────────────────────────────\n\n/** A static set of request headers (used for custom instance authentication). */\nexport type ToolHeaders = Record<string, string>\n\n/**\n * A resolver returning request headers, sync or async. Use this form when the auth token is\n * refreshable — the resolver runs on every request, so a fresh token can be minted per call.\n */\nexport type ToolHeadersResolver = () => ToolHeaders | Promise<ToolHeaders>\n\n/**\n * Resolve the configured headers (a static object or a sync/async resolver) for a single request.\n *\n * @param headers - The static header record, the resolver, or `undefined`.\n * @returns A fresh, owned copy of the resolved headers (`{}` when none supplied).\n */\nexport const resolveHeaders = async (\n headers: ToolHeaders | ToolHeadersResolver | undefined\n): Promise<ToolHeaders> => {\n if (typeof headers === 'function') return { ...(await headers()) }\n return { ...(headers ?? {}) }\n}\n\n// ── Artifact resolver ────────────────────────────────────────────────────────\n\n/** Convenience alias for the spooled-artifact constructor a tool wraps its output in. */\nexport type SpooledArtifactCtor = SpooledArtifactConstructor\n\n/**\n * The artifact configuration accepted by a factory: a constructor, a sync resolver, or an async /\n * dynamic-import resolver (which may yield a module namespace whose `default` is the constructor).\n *\n * @remarks\n * Mirrors the vector battery's `client` resolver and `Tool.artifactConstructor`'s indirection. The\n * async form lets a consumer `() => import('@nhtio/adk/spooled_artifact').then(m => m.SpooledMarkdownArtifact)`\n * so the artifact class never enters their static module graph.\n */\nexport type ArtifactResolver =\n | SpooledArtifactCtor\n | (() => SpooledArtifactCtor | { default: SpooledArtifactCtor })\n | (() => Promise<SpooledArtifactCtor | { default: SpooledArtifactCtor }>)\n\n/** The sync subset of {@link ArtifactResolver} — a constructor or a sync resolver (no Promise). */\nexport type SyncArtifactResolver =\n | SpooledArtifactCtor\n | (() => SpooledArtifactCtor | { default: SpooledArtifactCtor })\n\n/** Unwrap a resolved value that may be a module namespace whose `default` is the constructor. */\nconst unwrapDefault = (value: unknown): unknown => {\n if (isObject(value) && 'default' in value) {\n const def = (value as { default?: unknown }).default\n if (SpooledArtifact.isSpooledArtifactConstructor(def)) return def\n }\n return value\n}\n\n/**\n * Resolve an {@link ArtifactResolver} to the **sync** `() => SpooledArtifactCtor` that\n * `Tool.artifactConstructor` requires (the wrap-site and the construction-time validator both\n * invoke it synchronously, so an async resolver cannot be passed straight through).\n *\n * @remarks\n * A bare constructor is itself a function, so it is distinguished from a resolver via\n * `SpooledArtifact.isSpooledArtifactConstructor` (the same duck-typed guard the core validator\n * uses) rather than by arity. Async because a dynamic-import resolver must be awaited here.\n *\n * @param resolver - The artifact configuration. When `undefined`, callers should fall back to\n * their own default (this function rejects `undefined` so the default lives with the caller).\n * @param onInvalid - Throws a battery-scoped error; receives a human-readable reason.\n * @returns A sync `() => SpooledArtifactCtor` suitable for `Tool.artifactConstructor`.\n */\nexport const resolveArtifact = async (\n resolver: ArtifactResolver,\n onInvalid: (reason: string) => never\n): Promise<() => SpooledArtifactCtor> => {\n // A constructor: hand back a thunk that returns it.\n if (SpooledArtifact.isSpooledArtifactConstructor(resolver)) {\n const ctor = resolver\n return () => ctor\n }\n // Otherwise it must be a resolver function.\n if (typeof resolver !== 'function') {\n onInvalid('artifact must be a SpooledArtifact constructor or a resolver returning one')\n }\n let resolved: unknown\n try {\n resolved = await (resolver as () => unknown)()\n } catch (err) {\n onInvalid(`artifact resolver threw: ${isError(err) ? err.message : String(err)}`)\n }\n resolved = unwrapDefault(resolved)\n if (!SpooledArtifact.isSpooledArtifactConstructor(resolved)) {\n onInvalid('artifact resolver did not resolve to a SpooledArtifact constructor')\n }\n const ctor = resolved as SpooledArtifactCtor\n return () => ctor\n}\n\n/**\n * Synchronous {@link resolveArtifact}: accepts only the {@link SyncArtifactResolver} subset and\n * throws (via `onInvalid`) on an async resolver — a runtime guard for JS callers who bypass the\n * compile-time narrowing.\n *\n * @param resolver - A constructor or a sync resolver.\n * @param onInvalid - Throws a battery-scoped error; receives a human-readable reason.\n * @returns A sync `() => SpooledArtifactCtor` suitable for `Tool.artifactConstructor`.\n */\nexport const resolveArtifactSync = (\n resolver: SyncArtifactResolver,\n onInvalid: (reason: string) => never\n): (() => SpooledArtifactCtor) => {\n if (SpooledArtifact.isSpooledArtifactConstructor(resolver)) {\n const ctor = resolver\n return () => ctor\n }\n if (typeof resolver !== 'function') {\n onInvalid('artifact must be a SpooledArtifact constructor or a resolver returning one')\n }\n let resolved: unknown\n try {\n resolved = (resolver as () => unknown)()\n } catch (err) {\n onInvalid(`artifact resolver threw: ${isError(err) ? err.message : String(err)}`)\n }\n if (isInstanceOf(resolved, 'Promise', Promise)) {\n onInvalid(\n 'artifact resolver is async; use the async factory variant for dynamic-import resolvers'\n )\n }\n resolved = unwrapDefault(resolved)\n if (!SpooledArtifact.isSpooledArtifactConstructor(resolved)) {\n onInvalid('artifact resolver did not resolve to a SpooledArtifact constructor')\n }\n const ctor = resolved as SpooledArtifactCtor\n return () => ctor\n}\n\n// ── Middleware pipeline runners ──────────────────────────────────────────────\n\n/** Internal sentinel a short-circuiting input stage throws to unwind the pipeline immediately. */\nconst SHORT_CIRCUIT = Symbol('adk.tools.shortCircuit')\n\ninterface ShortCircuitSignal {\n [SHORT_CIRCUIT]: true\n result: string\n}\n\n/** `true` when `value` is the short-circuit sentinel produced by {@link makeShortCircuit}. */\nexport const isShortCircuit = (value: unknown): value is { result: string } =>\n isObject(value) && (value as Record<symbol, unknown>)[SHORT_CIRCUIT] === true\n\n/**\n * Build a `shortCircuit(result)` function for an input-pipeline context. Calling it throws the\n * internal sentinel, which {@link runInputPipeline} catches and converts into the verbatim result\n * (skipping the HTTP request entirely — e.g. a cache hit).\n *\n * @returns A function that, when called with a result string, throws the short-circuit sentinel.\n */\nexport const makeShortCircuit = (): ((result: string) => never) => {\n return (result: string): never => {\n const signal: ShortCircuitSignal = { [SHORT_CIRCUIT]: true, result }\n throw signal\n }\n}\n\n/** A generic onion middleware stage over a mutable context `C`. */\nexport type MiddlewareFn<C> = (ctx: C, next: NextFn) => void | Promise<void>\n\n/**\n * Run an input pipeline over `ctx`. Returns the short-circuit string when a stage short-circuited,\n * or `undefined` when the pipeline reached its terminal handler. A non-terminal pipeline (a stage\n * that neither called `next()` nor short-circuited) throws — the caller converts it to an\n * `Error:` string.\n *\n * @param mw - The `Middleware` instance holding the stages (a fresh `.runner()` is minted here).\n * @param ctx - The mutable input context handed to each stage.\n * @param label - Battery name, used in the non-terminal error message.\n * @returns The short-circuit result string, or `undefined` if the pipeline ran to completion.\n */\nexport const runInputPipeline = async <C>(\n mw: Middleware<MiddlewareFn<C>>,\n ctx: C,\n label: string\n): Promise<string | undefined> => {\n let reached = false\n let caught: unknown\n await mw\n .runner()\n .errorHandler(async (error: unknown) => {\n caught = error\n })\n .finalHandler(async () => {\n reached = true\n })\n .run((fn, next) => Promise.resolve(fn(ctx, next)))\n\n if (caught !== undefined) {\n if (isShortCircuit(caught)) return caught.result\n throw caught\n }\n if (!reached) {\n throw new Error(`${label} input pipeline did not call next() and did not short-circuit.`)\n }\n return undefined\n}\n\n/**\n * Run an output pipeline over `ctx`; rethrow any stage error to the caller's try/catch. A\n * non-terminal pipeline (no `next()`) throws.\n *\n * @param mw - The `Middleware` instance holding the stages (a fresh `.runner()` is minted here).\n * @param ctx - The mutable output context handed to each stage.\n * @param label - Battery name, used in the non-terminal error message.\n */\nexport const runOutputPipeline = async <C>(\n mw: Middleware<MiddlewareFn<C>>,\n ctx: C,\n label: string\n): Promise<void> => {\n let reached = false\n let caught: unknown\n await mw\n .runner()\n .errorHandler(async (error: unknown) => {\n caught = error\n })\n .finalHandler(async () => {\n reached = true\n })\n .run((fn, next) => Promise.resolve(fn(ctx, next)))\n\n if (caught !== undefined) throw caught\n if (!reached) throw new Error(`${label} output pipeline did not call next().`)\n}\n"],"mappings":";;;;;;;;;;;AA+CA,IAAa,iBAAiB,OAC5B,YACyB;CACzB,IAAI,OAAO,YAAY,YAAY,OAAO,EAAE,GAAI,MAAM,QAAQ,EAAG;CACjE,OAAO,EAAE,GAAI,WAAW,CAAC,EAAG;AAC9B;;AA2BA,IAAM,iBAAiB,UAA4B;CACjD,IAAI,SAAS,KAAK,KAAK,aAAa,OAAO;EACzC,MAAM,MAAO,MAAgC;EAC7C,IAAI,gBAAgB,6BAA6B,GAAG,GAAG,OAAO;CAChE;CACA,OAAO;AACT;;;;;;;;;;;;;;;;AAiBA,IAAa,kBAAkB,OAC7B,UACA,cACuC;CAEvC,IAAI,gBAAgB,6BAA6B,QAAQ,GAAG;EAC1D,MAAM,OAAO;EACb,aAAa;CACf;CAEA,IAAI,OAAO,aAAa,YACtB,UAAU,4EAA4E;CAExF,IAAI;CACJ,IAAI;EACF,WAAW,MAAO,SAA2B;CAC/C,SAAS,KAAK;EACZ,UAAU,4BAA4B,QAAQ,GAAG,IAAI,IAAI,UAAU,OAAO,GAAG,GAAG;CAClF;CACA,WAAW,cAAc,QAAQ;CACjC,IAAI,CAAC,gBAAgB,6BAA6B,QAAQ,GACxD,UAAU,oEAAoE;CAEhF,MAAM,OAAO;CACb,aAAa;AACf;;;;;;;;;;AAWA,IAAa,uBACX,UACA,cACgC;CAChC,IAAI,gBAAgB,6BAA6B,QAAQ,GAAG;EAC1D,MAAM,OAAO;EACb,aAAa;CACf;CACA,IAAI,OAAO,aAAa,YACtB,UAAU,4EAA4E;CAExF,IAAI;CACJ,IAAI;EACF,WAAY,SAA2B;CACzC,SAAS,KAAK;EACZ,UAAU,4BAA4B,QAAQ,GAAG,IAAI,IAAI,UAAU,OAAO,GAAG,GAAG;CAClF;CACA,IAAI,aAAa,UAAU,WAAW,OAAO,GAC3C,UACE,wFACF;CAEF,WAAW,cAAc,QAAQ;CACjC,IAAI,CAAC,gBAAgB,6BAA6B,QAAQ,GACxD,UAAU,oEAAoE;CAEhF,MAAM,OAAO;CACb,aAAa;AACf;;AAKA,IAAM,gBAAgB,OAAO,wBAAwB;;AAQrD,IAAa,kBAAkB,UAC7B,SAAS,KAAK,KAAM,MAAkC,mBAAmB;;;;;;;;AAS3E,IAAa,yBAAsD;CACjE,QAAQ,WAA0B;EAEhC,MAAM;IADgC,gBAAgB;GAAM;EACtD;CACR;AACF;;;;;;;;;;;;AAgBA,IAAa,mBAAmB,OAC9B,IACA,KACA,UACgC;CAChC,IAAI,UAAU;CACd,IAAI;CACJ,MAAM,GACH,OAAO,EACP,aAAa,OAAO,UAAmB;EACtC,SAAS;CACX,CAAC,EACA,aAAa,YAAY;EACxB,UAAU;CACZ,CAAC,EACA,KAAK,IAAI,SAAS,QAAQ,QAAQ,GAAG,KAAK,IAAI,CAAC,CAAC;CAEnD,IAAI,WAAW,KAAA,GAAW;EACxB,IAAI,eAAe,MAAM,GAAG,OAAO,OAAO;EAC1C,MAAM;CACR;CACA,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,GAAG,MAAM,+DAA+D;AAG5F;;;;;;;;;AAUA,IAAa,oBAAoB,OAC/B,IACA,KACA,UACkB;CAClB,IAAI,UAAU;CACd,IAAI;CACJ,MAAM,GACH,OAAO,EACP,aAAa,OAAO,UAAmB;EACtC,SAAS;CACX,CAAC,EACA,aAAa,YAAY;EACxB,UAAU;CACZ,CAAC,EACA,KAAK,IAAI,SAAS,QAAQ,QAAQ,GAAG,KAAK,IAAI,CAAC,CAAC;CAEnD,IAAI,WAAW,KAAA,GAAW,MAAM;CAChC,IAAI,CAAC,SAAS,MAAM,IAAI,MAAM,GAAG,MAAM,sCAAsC;AAC/E"}
@@ -22,6 +22,7 @@ export * from "./math/index";
22
22
  export * from "./memory/index";
23
23
  export * from "./parsing/index";
24
24
  export * from "./retrievables/index";
25
+ export * from "./scrapper/index";
25
26
  export * from "./searxng/index";
26
27
  export * from "./standing_instructions/index";
27
28
  export * from "./statistics/index";
@@ -29,5 +30,6 @@ export * from "./string_processing/index";
29
30
  export * from "./structured_data/index";
30
31
  export * from "./text_analysis/index";
31
32
  export * from "./text_comparison/index";
33
+ export * from "./web_retrieval/index";
32
34
  export * from "./time/index";
33
35
  export * from "./unit_conversion/index";
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Battery-scoped exception constructors for the Scrapper web-extraction tool.
3
+ *
4
+ * @remarks
5
+ * Battery-scoped exception classes owned by the Scrapper tool battery (not the ADK core). Minted
6
+ * via `createException` from `@nhtio/adk/factories` and re-exported from the battery's barrel
7
+ * (`@nhtio/adk/batteries/tools/scrapper`). This file intentionally carries **no** `@module` tag:
8
+ * it is an internal sibling relative-imported by `index.ts`, so it does not mint its own
9
+ * `…/scrapper/exceptions` entrypoint (whose `exceptions` leaf basename would collide with sibling
10
+ * batteries under the `vite-plugin-dts` rolled-up `.d.ts` rule).
11
+ */
12
+ /**
13
+ * Thrown when a Scrapper factory receives invalid configuration.
14
+ *
15
+ * @remarks
16
+ * Fatal: config bugs (missing/unparseable `instanceUrl`, a bad `artifact` resolver, or an async
17
+ * resolver passed to a `*Sync` factory) fail loud at factory-call time rather than at first scrape.
18
+ */
19
+ export declare const E_INVALID_SCRAPPER_CONFIG: import("../../../factories").CreatedException<[
20
+ string
21
+ ]>;