@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
79 lines (78 loc) • 4.08 kB
TypeScript
/**
* Host-loop delegation (N5): `registerAgentTool()` wraps an isolated agent
* (runIsolatedAgent) as a tool on the HOST NeuroLink instance, so the host's
* EXISTING generate() loop delegates — never a second router generate.
*
* Framework policy lives here:
* - per-turn delegation caps, counted per top-level generate() in the loop
* itself (AsyncLocalStorage turn scope);
* - depth limits via tool context (`agentDepth`) — at the limit the tool is
* withheld from the request entirely;
* - a process-wide concurrency pool with queue timeout, shared by every
* registered agent tool;
* - every refusal carries its recovery instruction in the error text.
*/
import type { NeuroLink } from "../neurolink.js";
import type { AgentToolRegistrationOptions, GenerateOptions, IsolatedAgentDefinition } from "../types/index.js";
/** Bound on the content text returned into the host loop per delegation. */
export declare const DELEGATION_RESULT_CONTENT_CHARS = 4000;
/**
* Enter a per-turn delegation scope for a top-level generate(). Returns null
* when a scope is already active (nested/internal generates share the
* top-level turn's counters) — the caller then proceeds without wrapping.
*/
export declare function beginDelegationTurn(host: NeuroLink, options: GenerateOptions | string | Record<string, unknown>): {
options: typeof options;
run: <T>(fn: () => Promise<T>) => Promise<T>;
} | null;
/**
* Run `fn` one delegation level deeper, sharing the current turn's counters.
* Used by AgentNetwork's standalone delegations: the network holds a pool
* slot around each agent execution, so any registered-agent-tool delegation
* the agent makes from inside must take the NESTED path (pool bypass, depth
* checks) — request-scoped via AsyncLocalStorage, never instance state.
*/
export declare function runWithNestedDelegationDepth<T>(fn: () => Promise<T>): Promise<T>;
/**
* The delegation depth a tool call is being made AT — execution context
* first (a worker running the host's registered closure arrives with its own
* `agentDepth`), then the per-turn ALS scope, then the host's own context.
*
* Exported so every delegation surface answers "how deep am I?" the same
* way; `executeDelegation` and the background form both call it.
*/
export declare function resolveDelegationDepth(host: NeuroLink, executionContext?: Record<string, unknown>): number;
/**
* Raise the process-wide delegation pool to at least `capacity` slots.
*
* Raises only — never lowers. The pool is shared by every delegation surface,
* so a per-agent "max concurrent" that could shrink it would be a throttle on
* everyone else's work. Queued waiters the raise just unblocked are granted
* immediately, so a raise never leaves capacity idle behind a full queue.
*/
export declare function raiseDelegationPoolCapacity(capacity: number): void;
/**
* Take a pool slot only if one is free right now. Returns the release
* function, or undefined when the pool is full — never queues.
*
* Background delegation needs the answer synchronously: `spawnDelegate`
* returns a handle BEFORE the worker starts, and the handle reports whether
* the worker is running or waiting for a slot. Asking after the fact would be
* a guess.
*/
export declare function tryAcquireDelegationSlot(): (() => void) | undefined;
/**
* Acquire a slot in the process-wide delegation pool, waiting up to
* `timeoutMs` in the queue. Resolves to a release function.
*/
export declare function acquireDelegationSlot(timeoutMs?: number): Promise<() => void>;
/**
* Register an isolated agent as a delegation tool on the host instance.
* The tool becomes available to every generate()/stream() the host runs —
* inside its existing loop, no second router generate.
*/
export declare function registerAgentTool(host: NeuroLink, definition: IsolatedAgentDefinition, options?: AgentToolRegistrationOptions): {
name: string;
};
/** Test-only: reset the process-wide pool. */
export declare function resetDelegationPoolForTests(capacity?: number): void;