import { BaseMessage } from '@langchain/core/messages'; import { l as AgentHistoryEntry, j as AgentHandlerContext, M as MessageContent, k as AgentHandlers, n as AgentMessage, p as AgentMessageContext, D as ToolApprovalDecision, b as AgentActionContext, R as ReplyHandle, A as Agent } from '../agent.types-DFLG1_vb.js'; import { LanguageModelLike } from '@langchain/core/language_models/base'; import { RunnableConfig } from '@langchain/core/runnables'; import { StructuredToolInterface } from '@langchain/core/tools'; import { AgentMiddleware } from 'langchain'; import { A as Awaitable } from '../util.types-DaFfsxgy.js'; import 'chat'; interface ToolResultPayload { toolCallId: string; toolName?: string; output: unknown; } /** * Convert conversation context into LangChain `BaseMessage[]` for `createAgent().invoke(...)`. * * Pass `ctx` to prepend `ctx.notification` as an `AIMessage`. Pass `ctx.history` to skip that * injection (e.g. after `isFromWorkflow`). History already includes the current inbound message. * * Returning a `LangChainAgentConfig` hydrates unreachable attachment URLs for you. * If you invoke the agent yourself, wrap this result in `hydrateUnreachableAttachmentUrls`. * * @param system - Optional system prompt prepended as a `SystemMessage`. Omit it when the * agent already receives a prompt (e.g. via `createAgent({ prompt })`) to avoid duplication. * @param freshResults - Tool outputs the adapter executed this turn (approve-resume). Cycles * with a fresh result are replayed as resolved call+result pairs. */ declare function toLangChainMessages(history: AgentHistoryEntry[], system?: string, freshResults?: Map): BaseMessage[]; declare function toLangChainMessages(ctx: Pick, system?: string, freshResults?: Map): BaseMessage[]; /** * Replace attachment URLs a hosted model cannot fetch with inline base64. * HTTPS public URLs are left as-is so production signed S3 links stay URL-based. * Over-budget downloads are omitted so one large file cannot abort the turn. * * Novu calls this automatically when you return a `LangChainAgentConfig`. * Call it yourself before `createAgent().invoke(...)` when you map history. */ declare function hydrateUnreachableAttachmentUrls(messages: BaseMessage[]): Promise; /** A tool call surfaced to `needsApproval` before it executes. */ interface LangChainToolCall { id?: string; name: string; args: Record; } /** * Declarative agent config the adapter runs on your behalf. * * Returning this from `onMessage` lets Novu own the tool-approval loop: gated * tools pause the turn with an approval card and resume statelessly from * `ctx.history` — no LangGraph checkpointer required. */ interface LangChainAgentConfig { /** * Chat model instance or `"provider:model"` identifier passed to `createAgent`. * On Next.js, model strings require LangChain packages in `serverExternalPackages` * (scaffolded by `novu connect --runtime langchain`). */ model: string | LanguageModelLike; /** Tools available to the agent. */ tools?: StructuredToolInterface[]; /** System prompt for the agent (forwarded to `createAgent` as `prompt`). */ system?: string; /** Return `true` to gate a tool call behind a Novu approval card before it runs. */ needsApproval?: (toolCall: LangChainToolCall) => boolean; /** Extra LangChain middleware, appended after Novu's approval middleware. */ middleware?: AgentMiddleware[]; /** * Run config forwarded as the second argument to `agent.invoke(...)` (e.g. * `signal`, `configurable`, `context`, `recursionLimit`, `callbacks`). Use this * to control the LangGraph run for a turn. */ invokeConfig?: RunnableConfig; /** * Optional post-run reply formatter. Receives the model's final text and returns * the content to deliver — a string or a {@link MessageContent} card. Return * `void`/`undefined` to deliver the final text unchanged. Runs only when the * model produced a non-empty final text. */ formatReply?: (finalText: string) => Awaitable; } /** * Result of a LangChain agent/graph you invoked yourself (e.g. `await agent.invoke(...)`). * Delivered as-is — tool approval is not managed by Novu on this path. */ interface LangChainInvokeResult { messages: BaseMessage[]; } /** Anything a `@novu/framework/langchain` handler may return for automatic delivery. */ type LangChainResult = LangChainAgentConfig | LangChainInvokeResult | BaseMessage; /** * Handlers for `@novu/framework/langchain` agents. * * Extends {@link AgentHandlers}: same events and config (`toolApproval`, etc.), * but `onMessage` and `onToolApproval` may return a {@link LangChainResult} for * automatic delivery. */ type LangChainAgentHandlers = Omit & { onMessage: (message: AgentMessage, ctx: AgentMessageContext) => Awaitable; /** * Optional. Auto-resumes `onMessage` after approve/deny unless you return a * `LangChainResult` to drive the resume yourself. */ onToolApproval?: (decision: ToolApprovalDecision, ctx: AgentActionContext) => Awaitable; onError?: AgentHandlers['onError']; }; type LangChainMessageHandler = LangChainAgentHandlers['onMessage']; /** * Wrap LangChain handlers as a Novu {@link Agent}. * * Return a {@link import('./types').LangChainAgentConfig} from `onMessage` to let Novu run the * agent and own the tool-approval loop; gated tools pause with an approval card and resume * statelessly from `ctx.history`. You may also return already-produced messages, a card, or plain * text. Approvals auto-resume `onMessage` unless a custom `onToolApproval` handles them. */ declare function agent(id: string, handlers: LangChainMessageHandler | LangChainAgentHandlers): Agent; /** * Thrown from the approval middleware when a gated tool is about to run. It propagates out * of `agent.invoke()` so the adapter can post a Novu approval card and end the turn instead * of executing the tool. The approval decision is later replayed from `ctx.history`. */ declare class NovuToolApprovalRequired extends Error { readonly toolCallId: string; readonly toolName: string; readonly input?: Record; constructor(toolCall: { toolCallId: string; toolName: string; input?: Record; }); } export { type LangChainAgentConfig, type LangChainAgentHandlers, type LangChainInvokeResult, type LangChainResult, type LangChainToolCall, NovuToolApprovalRequired, agent, hydrateUnreachableAttachmentUrls, toLangChainMessages };