import type { PromptFragment } from "@b4run/core"; import { type BaseMessage, ToolMessage } from "@langchain/core/messages"; import { type AgentMiddleware } from "langchain"; import { type ModelRetryPolicy } from "./model-call-retry.js"; import { type ResolvedSummarizationConfig } from "./summarization/index.js"; export interface B4AgentMiddlewareOptions { /** Resolved provider, used to enrich standardized server-tool blocks. */ readonly provider?: string; readonly systemPrompt: string; readonly promptFragments: readonly PromptFragment[]; /** The route's own state fields, which prompt fragments may read. */ readonly stateFieldNames: readonly string[]; readonly summarization?: ResolvedSummarizationConfig; /** Tools that end the run on a successful result. */ readonly returnDirectToolNames: ReadonlySet; /** The route's `retry`, resolved; defaults to 3 attempts and a 1s base delay. */ readonly retry?: ModelRetryPolicy; } /** * The middleware B4 installs on every `createAgent` route. * * Two middlewares, split by what they may write. `createAgent` hands a * middleware's hooks only the state fields that middleware declares, and a * middleware NODE (a `beforeModel` hook) writes every declared field back * when it returns an update, which re-applies a non-idempotent reducer such * as `append`. So the model/tool middleware, which has no node and never * writes state, declares the route's fields to read them; the loop-entry * middleware declares only replace-semantics fields. * * The loop-entry middleware exists only when the route summarizes or has a * `returnDirect` tool: each `beforeModel` hook is its own graph node and costs * a superstep per loop against `recursionLimit`, so a route without either * keeps the two-node loop it had under `createReactAgent`. */ export declare function createB4AgentMiddleware(options: B4AgentMiddlewareOptions): AgentMiddleware[]; /** * The history with an error result after every tool call that never got one, * or the same array when there is none. * * By the time the model is called, every tool call of a finished turn has a * result: the tools node runs them all first, and a call parked on an * approval interrupt runs on resume before the model is called again. So a * call still unanswered here belongs to a run that was cut off between the * model turn and its tools (`recursionLimit`, an abort, a crash), and the * checkpoint keeps it. Providers refuse that history (OpenAI: "An assistant * message with 'tool_calls' must be followed by tool messages"), which would * fail every later turn on the thread. Only the model's view is repaired; the * checkpoint is left as it is. */ export declare function answerDanglingToolCalls(messages: BaseMessage[]): BaseMessage[]; /** * The error result the model reads when a tool fails, in the exact form * LangGraph's prebuilt `ToolNode` produced. Without this, `createAgent` treats * a tool error that escapes `wrapToolCall` as fatal, and the error messages it * builds itself carry no `status`, so nothing downstream (the returnDirect * check here, the adapter's root-tool-error projection) could tell a failed * call from a successful one. */ export declare function toolErrorMessage(error: unknown, name: string, id: string | undefined): ToolMessage; /** * Whether the tool results the model is about to read include a successful * result from a `returnDirect` tool. A failed call goes back to the model so * it can correct the call and retry; the first success ends the run. Only the * trailing tool results count: they are this turn's, and an earlier turn's * result already had its chance to end the run. */ export declare function endsOnReturnDirect(messages: readonly BaseMessage[], returnDirectToolNames: ReadonlySet): boolean; //# sourceMappingURL=agent-middleware.d.ts.map