Skip to content
3 min read · 520 words

@nhtio/adk/batteries/tools/_shared

Cross-battery helpers shared by the configured HTTP tool batteries (SearXNG, Scrapper, …).

Remarks

These are internal building blocks for the factory-style tool batteries — the ones that talk to a configured HTTP instance behind custom auth and expose input/output middleware pipelines. Rather than each battery carry its own copy, the common machinery lives here:

  • resolveArtifact / resolveArtifactSync — turn an ArtifactResolver (a constructor, a sync resolver, or an async / dynamic-import resolver) into the sync() => SpooledArtifactConstructor that Tool.artifactConstructor requires. Mirrors the vector battery's resolveClientCtor.
  • resolveHeaders — collapse a static header object or a (sync/async) resolver into a plain header record for one request (refreshable-auth friendly).
  • runInputPipeline / runOutputPipeline — the onion middleware runners (fresh runner per call, short-circuit + non-terminal detection), generic over the context type.

This module imports harness primitives only through their specific subpath barrels (@nhtio/adk/spooled_artifact, @nhtio/adk/forge, @nhtio/adk/guards) per the batteries barrel-only rule.

Type Aliases

Type AliasDescription
ArtifactResolverThe artifact configuration accepted by a factory: a constructor, a sync resolver, or an async / dynamic-import resolver (which may yield a module namespace whose default is the constructor).
MiddlewareFnA generic onion middleware stage over a mutable context C.
SpooledArtifactCtorConvenience alias for the spooled-artifact constructor a tool wraps its output in.
SyncArtifactResolverThe sync subset of ArtifactResolver — a constructor or a sync resolver (no Promise).
ToolHeadersA static set of request headers (used for custom instance authentication).
ToolHeadersResolverA resolver returning request headers, sync or async. Use this form when the auth token is refreshable — the resolver runs on every request, so a fresh token can be minted per call.

Functions

FunctionDescription
isShortCircuittrue when value is the short-circuit sentinel produced by makeShortCircuit.
makeShortCircuitBuild a shortCircuit(result) function for an input-pipeline context. Calling it throws the internal sentinel, which runInputPipeline catches and converts into the verbatim result (skipping the HTTP request entirely — e.g. a cache hit).
resolveArtifactResolve an ArtifactResolver to the sync () => SpooledArtifactCtor that Tool.artifactConstructor requires (the wrap-site and the construction-time validator both invoke it synchronously, so an async resolver cannot be passed straight through).
resolveArtifactSyncSynchronous resolveArtifact: accepts only the SyncArtifactResolver subset and throws (via onInvalid) on an async resolver — a runtime guard for JS callers who bypass the compile-time narrowing.
resolveHeadersResolve the configured headers (a static object or a sync/async resolver) for a single request.
runInputPipelineRun an input pipeline over ctx. Returns the short-circuit string when a stage short-circuited, or undefined when the pipeline reached its terminal handler. A non-terminal pipeline (a stage that neither called next() nor short-circuited) throws — the caller converts it to an Error: string.
runOutputPipelineRun an output pipeline over ctx; rethrow any stage error to the caller's try/catch. A non-terminal pipeline (no next()) throws.