@librechat/agents 3.4.3 → 3.4.5
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.
- package/dist/cjs/graphs/Graph.cjs +27 -14
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +1 -1
- package/dist/cjs/hitl/askUserQuestions.cjs +66 -0
- package/dist/cjs/hitl/askUserQuestions.cjs.map +1 -0
- package/dist/cjs/hitl/askUserQuestionsInterrupt.cjs +46 -0
- package/dist/cjs/hitl/askUserQuestionsInterrupt.cjs.map +1 -0
- package/dist/cjs/hitl/index.cjs +2 -0
- package/dist/cjs/instrumentation.cjs +3 -3
- package/dist/cjs/langfuse.cjs +3 -3
- package/dist/cjs/langfuseRuntimeScope.cjs +1 -1
- package/dist/cjs/langfuseToolOutputTracing.cjs +2 -2
- package/dist/cjs/main.cjs +11 -1
- package/dist/cjs/messages/assistantPhase.cjs +59 -0
- package/dist/cjs/messages/assistantPhase.cjs.map +1 -0
- package/dist/cjs/messages/index.cjs +1 -0
- package/dist/cjs/prompts/activityLabel.cjs +76 -0
- package/dist/cjs/prompts/activityLabel.cjs.map +1 -1
- package/dist/cjs/run.cjs +200 -10
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/session/AgentSession.cjs +1 -1
- package/dist/cjs/stream.cjs +45 -8
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +3 -3
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +81 -6
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/types/hitl.cjs +13 -0
- package/dist/cjs/types/hitl.cjs.map +1 -0
- package/dist/cjs/utils/callbacks.cjs +8 -0
- package/dist/cjs/utils/callbacks.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +27 -14
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +1 -1
- package/dist/esm/hitl/askUserQuestions.mjs +66 -0
- package/dist/esm/hitl/askUserQuestions.mjs.map +1 -0
- package/dist/esm/hitl/askUserQuestionsInterrupt.mjs +43 -0
- package/dist/esm/hitl/askUserQuestionsInterrupt.mjs.map +1 -0
- package/dist/esm/hitl/index.mjs +2 -0
- package/dist/esm/instrumentation.mjs +3 -3
- package/dist/esm/langfuse.mjs +3 -3
- package/dist/esm/langfuseRuntimeScope.mjs +1 -1
- package/dist/esm/langfuseToolOutputTracing.mjs +2 -2
- package/dist/esm/main.mjs +5 -2
- package/dist/esm/messages/assistantPhase.mjs +57 -0
- package/dist/esm/messages/assistantPhase.mjs.map +1 -0
- package/dist/esm/messages/index.mjs +1 -0
- package/dist/esm/prompts/activityLabel.mjs +74 -1
- package/dist/esm/prompts/activityLabel.mjs.map +1 -1
- package/dist/esm/run.mjs +202 -12
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/session/AgentSession.mjs +1 -1
- package/dist/esm/stream.mjs +45 -8
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +3 -3
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +81 -6
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/types/hitl.mjs +13 -0
- package/dist/esm/types/hitl.mjs.map +1 -0
- package/dist/esm/utils/callbacks.mjs +8 -1
- package/dist/esm/utils/callbacks.mjs.map +1 -1
- package/dist/types/hitl/askUserQuestions.d.ts +24 -0
- package/dist/types/hitl/askUserQuestionsInterrupt.d.ts +11 -0
- package/dist/types/hitl/index.d.ts +2 -0
- package/dist/types/messages/assistantPhase.d.ts +22 -0
- package/dist/types/messages/index.d.ts +1 -0
- package/dist/types/prompts/activityLabel.d.ts +21 -1
- package/dist/types/run.d.ts +15 -2
- package/dist/types/types/activityLabel.d.ts +63 -0
- package/dist/types/types/assistantPhase.d.ts +6 -0
- package/dist/types/types/graph.d.ts +8 -1
- package/dist/types/types/hitl.d.ts +31 -2
- package/dist/types/types/index.d.ts +1 -0
- package/dist/types/types/stream.d.ts +11 -0
- package/dist/types/utils/callbacks.d.ts +1 -0
- package/package.json +2 -1
- package/src/graphs/Graph.ts +33 -9
- package/src/graphs/__tests__/Graph.reasoning.test.ts +57 -0
- package/src/hitl/askUserQuestions.ts +126 -0
- package/src/hitl/askUserQuestionsInterrupt.ts +115 -0
- package/src/hitl/index.ts +6 -0
- package/src/messages/assistantPhase.test.ts +75 -0
- package/src/messages/assistantPhase.ts +91 -0
- package/src/messages/index.ts +1 -0
- package/src/prompts/activityLabel.ts +177 -1
- package/src/run.ts +403 -21
- package/src/specs/activity-label-prompt.test.ts +123 -1
- package/src/specs/activity-phase-label.test.ts +306 -0
- package/src/specs/ask-user-questions.live.test.ts +185 -0
- package/src/specs/ask-user-questions.test.ts +293 -0
- package/src/stream.ts +69 -12
- package/src/tools/__tests__/SubagentExecutor.test.ts +436 -0
- package/src/tools/subagent/SubagentExecutor.ts +160 -8
- package/src/types/activityLabel.ts +65 -0
- package/src/types/assistantPhase.ts +6 -0
- package/src/types/graph.ts +8 -0
- package/src/types/hitl.ts +36 -2
- package/src/types/index.ts +1 -0
- package/src/types/stream.ts +9 -0
- package/src/utils/callbacks.ts +21 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/types/hitl.ts
|
|
2
|
+
/**
|
|
3
|
+
* Type guard narrowing an arbitrary value to an
|
|
4
|
+
* `AskUserQuestionInterruptPayload`. Same `unknown`-tolerant contract
|
|
5
|
+
* as `isToolApprovalInterrupt`.
|
|
6
|
+
*/
|
|
7
|
+
function isAskUserQuestionInterrupt(payload) {
|
|
8
|
+
return typeof payload === "object" && payload !== null && payload.type === "ask_user_question";
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
exports.isAskUserQuestionInterrupt = isAskUserQuestionInterrupt;
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=hitl.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hitl.cjs","names":[],"sources":["../../../src/types/hitl.ts"],"sourcesContent":["/**\n * First-class human-in-the-loop (HITL) types for `@librechat/agents`.\n * Surfaces the interrupt payload that `ToolNode` raises when a `PreToolUse`\n * hook returns `decision: 'ask'` and HITL is enabled on the run, plus the\n * resume-decision shape the host returns to continue or reject the tool.\n *\n * Mirrors the LangChain HITL middleware shape (action_requests /\n * review_configs) so hosts and clients can share rendering/UI semantics\n * across the langchain ecosystem.\n */\n\n/** Per-tool approval request emitted inside an interrupt payload. */\nexport interface ToolApprovalRequest {\n /** Stable id of the tool call (matches LangGraph `ToolCall.id`). */\n tool_call_id: string;\n /** Tool name being invoked. */\n name: string;\n /**\n * Arguments the tool is about to be invoked with — already resolved by\n * any `{{tool<i>turn<n>}}` references and any `updatedInput` returned\n * by the firing PreToolUse hook.\n */\n arguments: Record<string, unknown>;\n /**\n * Optional reason the hook supplied for asking (e.g., \"destructive\n * filesystem write\"). Hosts can render this verbatim.\n */\n description?: string;\n}\n\n/** Allowed host-side decisions for a `tool_approval` interrupt. */\nexport type ToolApprovalDecisionType =\n | 'approve'\n | 'reject'\n | 'edit'\n | 'respond';\n\n/** Per-action review configuration paired with each action_request. */\nexport interface ToolApprovalReviewConfig {\n /** Tool name (matches the `name` field on the corresponding action_request). */\n action_name: string;\n /**\n * Stable id of the tool call this review_config applies to (matches\n * the `tool_call_id` of the corresponding action_request). Lets a UI\n * map review_configs → action_requests directly when a batch\n * contains the same tool called more than once — by-position\n * mapping breaks down with duplicates.\n */\n tool_call_id: string;\n /** Decisions the host UI is allowed to surface for this action. */\n allowed_decisions: ToolApprovalDecisionType[];\n}\n\n/**\n * Resume value the host returns through `Run.resume(decisions)` after a\n * `tool_approval` interrupt. One entry per action_request, in the same\n * order. Hosts may also return a record keyed by `tool_call_id`; the SDK\n * handles either shape.\n *\n * Variants:\n * - `approve`: run the tool with its original (or hook-rewritten) args.\n * - `reject`: skip the tool, emit a blocked error `ToolMessage` with\n * `reason` surfaced to the model.\n * - `edit`: replace the tool's args with `updatedInput` (re-resolves\n * any `{{tool<i>turn<n>}}` placeholders) and run the tool.\n * - `respond`: skip the tool entirely and emit `responseText` as a\n * successful `ToolMessage`. Mirrors LangChain HITL middleware's\n * `respond` semantic — the human supplies the result the model sees,\n * bypassing tool execution. Useful when the user wants to short-circuit\n * a tool call with a hand-written answer (e.g., \"don't actually run\n * the search, just tell the model 'no relevant results'\").\n *\n * Note on hook semantics: `respond` does NOT fire the per-tool\n * `PostToolUse` hook (no real tool execution happened, so the\n * \"post-tool\" semantic doesn't apply). It DOES appear in the\n * `PostToolBatch` entry array with `status: 'success'` and the\n * user-supplied text as `toolOutput`, so batch-level audit /\n * convention hooks see the full set of outcomes.\n */\nexport type ToolApprovalDecision =\n | { type: 'approve' }\n | { type: 'reject'; reason?: string }\n | { type: 'edit'; updatedInput: Record<string, unknown> }\n | { type: 'respond'; responseText: string };\n\n/** Map form of resume decisions, keyed by tool call id. */\nexport type ToolApprovalDecisionMap = Record<string, ToolApprovalDecision>;\n\n/**\n * Categories of human-in-the-loop interrupts the SDK can raise. Hosts\n * narrow on `HumanInterruptPayload.type` to determine which payload\n * shape they're handling and which resume value to send back through\n * `Run.resume()`.\n *\n * Exported as a discrete type so downstream consumers (notably\n * LibreChat's wire types in `librechat-data-provider`) can mirror\n * the discriminator alongside their own host-side `PendingAction`\n * record without re-declaring the union themselves. Internal SDK\n * code narrows directly on the literal strings via the type guards\n * below; this type alias is primarily an integration-layer contract.\n */\nexport type HumanInterruptType = 'tool_approval' | 'ask_user_question';\n\n/** Identifies an interrupt that originated inside a checkpointed subagent. */\nexport interface SubagentInterruptScope {\n /** Child execution run id used by subagent update and usage events. */\n run_id: string;\n /** Child agent id that owns the interrupted tool call. */\n agent_id: string;\n /** Configured subagent type selected by the parent tool call. */\n subagent_type: string;\n /** Parent `subagent` tool call that launched this child. */\n parent_tool_call_id?: string;\n}\n\n/**\n * Structured payload the SDK passes to `interrupt()` when one or more\n * pending tool calls require host approval. All `ask`-decision tool calls\n * from a single ToolNode batch are bundled into one interrupt so the host\n * can render and resolve them together.\n *\n * Resume value: `ToolApprovalDecision[]` (in `action_requests` order) or\n * `ToolApprovalDecisionMap` (keyed by `tool_call_id`).\n */\nexport interface ToolApprovalInterruptPayload {\n type: 'tool_approval';\n action_requests: ToolApprovalRequest[];\n review_configs: ToolApprovalReviewConfig[];\n /** Hook-registry session whose policy raised this interrupt. */\n hook_session_id?: string;\n /** Present when the approval request was bridged from a child graph. */\n subagent?: SubagentInterruptScope;\n}\n\n/**\n * Pre-defined option the user can pick when answering an\n * `ask_user_question` interrupt. The selected option's `value` becomes\n * the resume value's `answer` field.\n */\nexport interface AskUserQuestionOption {\n /** Human-readable label rendered in the host UI. */\n label: string;\n /** Value returned via `AskUserQuestionResolution.answer` if picked. */\n value: string;\n}\n\n/** Question request emitted inside an `ask_user_question` interrupt. */\nexport interface AskUserQuestionRequest {\n /** The question to ask the human. */\n question: string;\n /** Optional context / description rendered alongside the question. */\n description?: string;\n /**\n * Optional pre-defined response options. When present, hosts can render\n * a picker; the user may still type a free-form answer when the host\n * UI allows it. Omit to require a free-form answer.\n */\n options?: AskUserQuestionOption[];\n /**\n * When `true`, the host UI may let the user pick several options; the\n * resulting `AskUserQuestionResolution.answer` is the selected option\n * values joined by `\", \"`. When omitted or `false`, hosts render a\n * single-select picker. Only meaningful alongside `options`.\n */\n multiSelect?: boolean;\n}\n\n/** One independently answerable question in a batched question request. */\nexport interface AskUserQuestionBatchItem extends AskUserQuestionRequest {\n /** Batch-unique identifier (`[A-Za-z][A-Za-z0-9_-]{0,63}`). */\n id: string;\n /** Optional short heading rendered above the question. */\n header?: string;\n}\n\n/** Input shape for one tool call that asks one to four questions together. */\nexport interface AskUserQuestionsRequest {\n questions: AskUserQuestionBatchItem[];\n}\n\n/**\n * Structured payload the SDK passes to `interrupt()` when an agent (or\n * a custom node) needs to ask the user a clarifying question. Mirrors\n * Claude Code's `AskUserQuestion` semantic. Resume value is\n * `AskUserQuestionResolution` for a single question, or\n * `AskUserQuestionsResolution` when `questions` is present.\n */\nexport interface AskUserQuestionInterruptPayload {\n type: 'ask_user_question';\n /**\n * Single-question request, or the first question as a compatibility\n * fallback when `questions` contains a batch. This lets existing hosts show\n * a useful preview during a staged rollout, but they must support `questions`\n * and `AskUserQuestionsResolution` before enabling a batched tool schema.\n */\n question: AskUserQuestionRequest;\n /** One to four questions collected by one `ask_user_question` tool call. */\n questions?: AskUserQuestionsRequest['questions'];\n /**\n * The `tool_call_id` of the ask-tool call that raised this interrupt,\n * when the tool body supplied it (see `askUserQuestion`'s `options`).\n * Lets hosts attribute the question — and later the answer — to the\n * exact tool-call content part instead of guessing by position, which\n * mislabels cards when a model emits several ask calls in one turn.\n */\n tool_call_id?: string;\n}\n\n/** Batch-specialized ask payload for hosts that render several questions. */\nexport interface AskUserQuestionsInterruptPayload\n extends AskUserQuestionInterruptPayload {\n questions: AskUserQuestionsRequest['questions'];\n}\n\n/**\n * Discriminated union of every interrupt payload the SDK raises. New\n * variants can be added without breaking existing handlers as long as\n * those handlers check `payload.type` before reading variant-specific\n * fields. Use the `isToolApprovalInterrupt` / `isAskUserQuestionInterrupt`\n * type guards for ergonomic narrowing.\n */\nexport type HumanInterruptPayload =\n | ToolApprovalInterruptPayload\n | AskUserQuestionInterruptPayload;\n\n/** Resume value the host returns for an `ask_user_question` interrupt. */\nexport interface AskUserQuestionResolution {\n /**\n * The human's answer. Free-form text, or — when `options` were\n * provided — one of the option `value`s (or, when the request set\n * `multiSelect`, several option `value`s joined by `\", \"`). Hosts may\n * also send any structured object their custom UI defines; see the\n * host docs for what your downstream consumer expects.\n */\n answer: string;\n}\n\n/** Resume value for a batched `ask_user_question` interrupt. */\nexport interface AskUserQuestionsResolution {\n /** Human answers keyed by each `AskUserQuestionBatchItem.id`. */\n answers: Record<string, string>;\n}\n\n/**\n * Type guard narrowing an arbitrary value to a `ToolApprovalInterruptPayload`.\n * Accepts `unknown` (not just `HumanInterruptPayload`) because hosts can\n * raise custom interrupt payloads from custom nodes — `getInterrupt()`\n * surfaces them as-is, and downstream code must validate the shape at\n * runtime before reading variant-specific fields.\n */\nexport function isToolApprovalInterrupt(\n payload: unknown\n): payload is ToolApprovalInterruptPayload {\n return (\n typeof payload === 'object' &&\n payload !== null &&\n (payload as { type?: unknown }).type === 'tool_approval'\n );\n}\n\n/**\n * Type guard narrowing an arbitrary value to an\n * `AskUserQuestionInterruptPayload`. Same `unknown`-tolerant contract\n * as `isToolApprovalInterrupt`.\n */\nexport function isAskUserQuestionInterrupt(\n payload: unknown\n): payload is AskUserQuestionInterruptPayload {\n return (\n typeof payload === 'object' &&\n payload !== null &&\n (payload as { type?: unknown }).type === 'ask_user_question'\n );\n}\n\n/**\n * Run-level configuration controlling HITL semantics. **HITL is OFF by\n * default** for now — the SDK ships the interrupt machinery, but the\n * default stays opt-in until host UIs (notably LibreChat) ship the\n * approval-rendering affordances needed to surface interrupts to end\n * users. Without that UI, an interrupt with no resolver looks like a\n * hung tool-call card. Hosts opt in explicitly with\n * `{ enabled: true }`. The intent is to flip this default to ON in a\n * future minor once the consumer ecosystem is ready to render\n * interrupts end-to-end.\n *\n * When enabled (`{ enabled: true }`):\n *\n * - `PreToolUse` hooks returning `decision: 'ask'` raise a real\n * LangGraph `interrupt()` instead of being treated as a synchronous\n * deny.\n * - `Run.create` installs a `MemorySaver` checkpointer fallback on the\n * run's compile options if the host did not provide one, since\n * LangGraph requires a checkpointer to suspend and resume.\n *\n * When disabled (the default — omitted, or `{ enabled: false }`):\n * `ask` decisions are fail-closed (blocked with an error\n * `ToolMessage`) and no checkpointer is implicitly attached. This\n * matches the pre-HITL behavior so existing hosts upgrading the SDK\n * see no change until they're ready to wire the resume UI.\n *\n * ## Scope: every tool the ToolNode runs\n *\n * The interrupt path is wired into both `dispatchToolEvents` (the\n * event-driven path) and `runDirectToolWithLifecycleHooks` (the\n * direct path used by `directToolNames` entries — graph-managed\n * handoff/subagent tools and every in-process `graphTool` instance).\n * `PreToolUse` hooks fire for every tool the ToolNode invokes, and\n * HITL approval gates every tool whose hook returns `'ask'` —\n * regardless of whether the tool is dispatched as an event or\n * invoked in-process. This convergence happened in two follow-up\n * commits to the original HITL surface (see `Graph.ts` —\n * `hookRegistry`/`humanInTheLoop` are passed in both\n * event-driven and legacy branches; and `ToolNode.runDirectToolWithLifecycleHooks`\n * — direct-path tools build their own single-tool `tool_approval`\n * payload and raise `interrupt()` the same way the event path does).\n *\n * Practical implications:\n * - Every host gets the full HITL surface across every tool the\n * model calls — event-dispatched, direct, mixed.\n * - `createToolPolicyHook` and `createWorkspacePolicyHook` apply\n * uniformly. A hook can be registered without knowing or caring\n * which path the tool will take.\n * - Direct tools that the host opted into via `directToolNames` no\n * longer bypass policy. If you need a tool to skip the hook\n * surface entirely, omit it from any registered matcher.\n *\n * ## Resume re-execution: every tool in the interrupted batch\n *\n * LangGraph rolls back to the start of the interrupted node on\n * resume. That means **every tool in the same batch as the one that\n * interrupted re-runs from the top on the resume pass**, not just\n * the interrupting tool, and not just the direct half (this used to\n * be framed as a direct-tool-specific concern; it is not — it\n * applies to event-dispatched siblings too). Practical contract:\n *\n * - The body of the interrupting tool itself runs **once** total\n * (the first pass interrupted *before* the body, the resume pass\n * ran the body after the host's decision was applied).\n * - The body of any sibling tool that already executed in the\n * same batch before the interrupting tool runs **twice** — once\n * on the first pass, once on the resume pass.\n * - `PreToolUse` hooks fire **once per pass per tool**. A hook\n * that always returns `'ask'` will loop forever on resume; real\n * hooks should be deterministic w.r.t. inputs and use the\n * `'ask' → host approves → resume → hook returns 'allow'`\n * pattern, where the second-pass `allow` reflects the host\n * having recorded the approval (e.g., a session-scoped approved-\n * paths set keyed by `runId`).\n *\n * Consequence: any tool with side effects MUST be idempotent if\n * there's any chance another tool in the same batch could trigger\n * an interrupt. This applies equally to direct tools (handoffs,\n * subagents) and to event tools.\n *\n * ### Guarding non-idempotent siblings via `interruptingToolNames`\n *\n * The \"must be idempotent\" rule above is unavoidable in the general\n * case, but the SDK can protect siblings against the one interrupt\n * shape it can predict: a tool whose *body* raises `interrupt()`\n * mid-execution — the `ask_user_question` shape, where the tool\n * suspends the run to collect a human answer. Declare such tools in\n * `RunConfig.interruptingToolNames`\n * ({@link ToolNodeOptions.interruptingToolNames}) and the ToolNode\n * schedules them, within each batch, **ahead of** their\n * non-interrupting direct siblings. When one interrupts, the batch\n * unwinds before any declared-safe sibling has run, so the sibling\n * executes exactly once (on resume) instead of twice. Empirically:\n *\n * - A **direct** sibling sharing the interrupter's in-process\n * `Promise.all` is the only shape that double-executes; declaring\n * the interrupter closes it.\n * - An **event-dispatched** sibling is already safe without any\n * config: the ToolNode awaits the whole direct group (where the\n * body interrupt unwinds) before it dispatches event tools, so a\n * dispatched sibling never runs on the first pass.\n *\n * This is a *scheduling* guard, not full resume idempotency: it only\n * covers tools that interrupt from their own body and only protects\n * siblings scheduled after them. It does not retroactively make a\n * `PreToolUse` `'ask'` gate on tool B stop tool A (already executed)\n * from re-running — unless B is itself declared interrupting, so it\n * runs first. Tools with side effects should still be written\n * idempotent as defense in depth.\n *\n * The guard only REORDERS the direct group — declaring a name does not\n * force it onto the direct path. The interrupting tool must already be a\n * real in-process graphTool (the only kind whose body can reach\n * `interrupt()`). A name that resolves to a schema-only event stub (an\n * inherited `toolDefinition` with no executable instance, e.g. in a\n * self-spawned child that scrubs `graphTools`) stays event-dispatched\n * and the ordering is a no-op for it.\n *\n * ## Note on idempotency\n *\n * Same root cause as the resume re-execution above: LangGraph\n * re-runs the interrupted node from the start on resume, which\n * fires `PreToolUse` hooks again. Hooks that produce side effects\n * (logging, external calls) will see at least two invocations per\n * paused turn — exactly two for the interrupting tool, possibly\n * more across siblings.\n */\nexport interface HumanInTheLoopConfig {\n /**\n * Master switch. Defaults to `false` — omit the field (or pass\n * `false`) to keep HITL off, or set `true` to opt in once the host\n * UI is ready to render and resolve `tool_approval` interrupts.\n */\n enabled?: boolean;\n}\n\n/**\n * Snapshot of an in-flight interrupt surfaced from `Run.processStream`\n * via `run.getInterrupt()`. Hosts persist this alongside their job\n * record so they can later call `Run.resume(decisions)` against a Run\n * compiled with the same `thread_id` / checkpointer.\n *\n * The `payload` type defaults to `HumanInterruptPayload` (the SDK's\n * built-in `tool_approval` / `ask_user_question` discriminated union)\n * for ergonomic narrowing in the common case. Hosts that raise custom\n * interrupt payloads from custom graph nodes can pass the type\n * parameter (`run.getInterrupt<MyCustom>()` or\n * `RunInterruptResult<MyCustom>`) — the SDK does not validate the\n * runtime shape, it just transports whatever the node passed to\n * `interrupt()`. Use the `isToolApprovalInterrupt` /\n * `isAskUserQuestionInterrupt` guards (which accept `unknown`) when\n * the source of the interrupt isn't statically known.\n */\nexport interface RunInterruptResult<TPayload = HumanInterruptPayload> {\n /** Stable id of the LangGraph interrupt (from `Interrupt.id`). */\n interruptId: string;\n /** `thread_id` the run was bound to — required to resume. */\n threadId?: string;\n /** LangGraph checkpoint id that contains the paused interrupt task. */\n checkpointId?: string;\n /** LangGraph checkpoint namespace for the paused interrupt task. */\n checkpointNs?: string;\n /** Structured payload describing what needs human input. */\n payload: TPayload;\n}\n"],"mappings":";;;;;;AAyQA,SAAgB,2BACd,SAC4C;CAC5C,OACE,OAAO,YAAY,YACnB,YAAY,QACX,QAA+B,SAAS;AAE7C"}
|
|
@@ -10,8 +10,16 @@ function findCallback(callbacks, predicate) {
|
|
|
10
10
|
if (callbacks == null) return;
|
|
11
11
|
return (Array.isArray(callbacks) ? callbacks : callbacks.handlers).find(predicate);
|
|
12
12
|
}
|
|
13
|
+
function filterCallbacks(callbacks, predicate) {
|
|
14
|
+
if (callbacks == null) return [];
|
|
15
|
+
if (Array.isArray(callbacks)) return callbacks.filter(predicate);
|
|
16
|
+
const filtered = callbacks.copy();
|
|
17
|
+
for (const handler of [...filtered.handlers]) if (!predicate(handler)) filtered.removeHandler(handler);
|
|
18
|
+
return filtered;
|
|
19
|
+
}
|
|
13
20
|
//#endregion
|
|
14
21
|
exports.appendCallbacks = appendCallbacks;
|
|
22
|
+
exports.filterCallbacks = filterCallbacks;
|
|
15
23
|
exports.findCallback = findCallback;
|
|
16
24
|
|
|
17
25
|
//# sourceMappingURL=callbacks.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"callbacks.cjs","names":["ensureHandler"],"sources":["../../../src/utils/callbacks.ts"],"sourcesContent":["import { ensureHandler } from '@langchain/core/callbacks/manager';\nimport type {\n BaseCallbackHandler,\n CallbackHandlerMethods,\n} from '@langchain/core/callbacks/base';\nimport type { Callbacks } from '@langchain/core/callbacks/manager';\n\nexport type CallbackEntry = BaseCallbackHandler | CallbackHandlerMethods;\n\nexport function appendCallbacks(\n callbacks: Callbacks | undefined,\n additions: readonly CallbackEntry[]\n): Callbacks {\n if (additions.length === 0) {\n return callbacks ?? [];\n }\n\n if (callbacks == null) {\n return [...additions];\n }\n\n if (Array.isArray(callbacks)) {\n return callbacks.concat(additions);\n }\n\n return callbacks.copy(additions.map(ensureHandler));\n}\n\nexport function findCallback(\n callbacks: Callbacks | undefined,\n predicate: (callback: CallbackEntry) => boolean\n): CallbackEntry | undefined {\n if (callbacks == null) {\n return undefined;\n }\n\n const handlers = Array.isArray(callbacks) ? callbacks : callbacks.handlers;\n return handlers.find(predicate);\n}\n"],"mappings":";;AASA,SAAgB,gBACd,WACA,WACW;CACX,IAAI,UAAU,WAAW,GACvB,OAAO,aAAa,CAAC;CAGvB,IAAI,aAAa,MACf,OAAO,CAAC,GAAG,SAAS;CAGtB,IAAI,MAAM,QAAQ,SAAS,GACzB,OAAO,UAAU,OAAO,SAAS;CAGnC,OAAO,UAAU,KAAK,UAAU,IAAIA,kCAAAA,aAAa,CAAC;AACpD;AAEA,SAAgB,aACd,WACA,WAC2B;CAC3B,IAAI,aAAa,MACf;CAIF,QADiB,MAAM,QAAQ,SAAS,IAAI,YAAY,UAAU,SAAA,CAClD,KAAK,SAAS;AAChC"}
|
|
1
|
+
{"version":3,"file":"callbacks.cjs","names":["ensureHandler"],"sources":["../../../src/utils/callbacks.ts"],"sourcesContent":["import { ensureHandler } from '@langchain/core/callbacks/manager';\nimport type {\n BaseCallbackHandler,\n CallbackHandlerMethods,\n} from '@langchain/core/callbacks/base';\nimport type { Callbacks } from '@langchain/core/callbacks/manager';\n\nexport type CallbackEntry = BaseCallbackHandler | CallbackHandlerMethods;\n\nexport function appendCallbacks(\n callbacks: Callbacks | undefined,\n additions: readonly CallbackEntry[]\n): Callbacks {\n if (additions.length === 0) {\n return callbacks ?? [];\n }\n\n if (callbacks == null) {\n return [...additions];\n }\n\n if (Array.isArray(callbacks)) {\n return callbacks.concat(additions);\n }\n\n return callbacks.copy(additions.map(ensureHandler));\n}\n\nexport function findCallback(\n callbacks: Callbacks | undefined,\n predicate: (callback: CallbackEntry) => boolean\n): CallbackEntry | undefined {\n if (callbacks == null) {\n return undefined;\n }\n\n const handlers = Array.isArray(callbacks) ? callbacks : callbacks.handlers;\n return handlers.find(predicate);\n}\n\nexport function filterCallbacks(\n callbacks: Callbacks | undefined,\n predicate: (callback: CallbackEntry) => boolean\n): Callbacks {\n if (callbacks == null) {\n return [];\n }\n\n if (Array.isArray(callbacks)) {\n return callbacks.filter(predicate);\n }\n\n const filtered = callbacks.copy();\n for (const handler of [...filtered.handlers]) {\n if (!predicate(handler)) {\n filtered.removeHandler(handler);\n }\n }\n return filtered;\n}\n"],"mappings":";;AASA,SAAgB,gBACd,WACA,WACW;CACX,IAAI,UAAU,WAAW,GACvB,OAAO,aAAa,CAAC;CAGvB,IAAI,aAAa,MACf,OAAO,CAAC,GAAG,SAAS;CAGtB,IAAI,MAAM,QAAQ,SAAS,GACzB,OAAO,UAAU,OAAO,SAAS;CAGnC,OAAO,UAAU,KAAK,UAAU,IAAIA,kCAAAA,aAAa,CAAC;AACpD;AAEA,SAAgB,aACd,WACA,WAC2B;CAC3B,IAAI,aAAa,MACf;CAIF,QADiB,MAAM,QAAQ,SAAS,IAAI,YAAY,UAAU,SAAA,CAClD,KAAK,SAAS;AAChC;AAEA,SAAgB,gBACd,WACA,WACW;CACX,IAAI,aAAa,MACf,OAAO,CAAC;CAGV,IAAI,MAAM,QAAQ,SAAS,GACzB,OAAO,UAAU,OAAO,SAAS;CAGnC,MAAM,WAAW,UAAU,KAAK;CAChC,KAAK,MAAM,WAAW,CAAC,GAAG,SAAS,QAAQ,GACzC,IAAI,CAAC,UAAU,OAAO,GACpB,SAAS,cAAc,OAAO;CAGlC,OAAO;AACT"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { SUBAGENT_REPLAY_CONTROLLER } from "../tools/subagent/SubagentReplay.mjs";
|
|
2
2
|
import { composeAbortSignals } from "../utils/misc.mjs";
|
|
3
3
|
import { hasToolOutputTracingConfig, resolveLangfuseConfig, resolveToolOutputTracingConfig } from "../langfuseConfig.mjs";
|
|
4
|
-
import { resolveLangfuseRuntimeScope, withLangfuseRuntimeScope } from "../langfuseRuntimeScope.mjs";
|
|
5
|
-
import { createLangfuseHandler, createLangfuseTraceMetadata, disposeLangfuseHandler, isLangfuseCallbackHandler } from "../langfuse.mjs";
|
|
6
4
|
import { PREEMPT_BOUNDARY_HOOK_TIMEOUT_MS } from "../common/constants.mjs";
|
|
7
5
|
import { GraphNodeKeys } from "../common/enum.mjs";
|
|
8
6
|
import "../common/index.mjs";
|
|
7
|
+
import { resolveLangfuseRuntimeScope, withLangfuseRuntimeScope } from "../langfuseRuntimeScope.mjs";
|
|
9
8
|
import { shouldTraceToolNodeForLangfuse } from "../langfuseToolOutputTracing.mjs";
|
|
9
|
+
import { createLangfuseHandler, createLangfuseTraceMetadata, disposeLangfuseHandler, isLangfuseCallbackHandler } from "../langfuse.mjs";
|
|
10
10
|
import { appendCallbacks, findCallback } from "../utils/callbacks.mjs";
|
|
11
11
|
import { applyGraphRuntimeConfig } from "./applyGraphRuntimeConfig.mjs";
|
|
12
12
|
import { calculateMaxToolResultChars } from "../utils/truncation.mjs";
|
|
@@ -28,11 +28,12 @@ import { coalesceAdjacentUserTurns, strictAlternationProviders } from "../messag
|
|
|
28
28
|
import { appendPredecessorHandoffCue, removePredecessorHandoffCue } from "../messages/handoffCue.mjs";
|
|
29
29
|
import { messagesStateReducer as messagesStateReducer$1 } from "../messages/reducer.mjs";
|
|
30
30
|
import { splitAtRecencyBoundary } from "../messages/recency.mjs";
|
|
31
|
+
import { getMessageCreationContentMetadata, splitAssistantTextContentByPhase } from "../messages/assistantPhase.mjs";
|
|
31
32
|
import "../messages/index.mjs";
|
|
32
33
|
import { joinKeys, resetIfNotEmpty } from "../utils/graph.mjs";
|
|
33
34
|
import { isAnthropicLike, isGoogleLike, isOpenAILike } from "../utils/llm.mjs";
|
|
34
|
-
import { handleToolCalls } from "../tools/handlers.mjs";
|
|
35
35
|
import { RUN_BREAKER_SCOPE_CONFIG_KEY, STREAM_LIMIT_EPOCH_KEY, StreamLimitExceededError, resolveStreamLimits, sweepStaleStreamLimitEntries } from "../llm/streamLimits.mjs";
|
|
36
|
+
import { handleToolCalls } from "../tools/handlers.mjs";
|
|
36
37
|
import { ToolOutputReferenceRegistry, annotateMessagesForLLM } from "../tools/toolOutputReferences.mjs";
|
|
37
38
|
import { HandlerRegistry } from "../events.mjs";
|
|
38
39
|
import { sleep } from "../utils/run.mjs";
|
|
@@ -62,8 +63,8 @@ import { createSchemaOnlyTools } from "../tools/schema.mjs";
|
|
|
62
63
|
import { AgentContext } from "../agents/AgentContext.mjs";
|
|
63
64
|
import { createFakeStreamingLLM } from "../llm/fake.mjs";
|
|
64
65
|
import { nanoid } from "nanoid";
|
|
65
|
-
import { AIMessageChunk, HumanMessage, ToolMessage } from "@langchain/core/messages";
|
|
66
66
|
import { Annotation, END, START, StateGraph } from "@langchain/langgraph";
|
|
67
|
+
import { AIMessageChunk, HumanMessage, ToolMessage } from "@langchain/core/messages";
|
|
67
68
|
import { tool } from "@langchain/core/tools";
|
|
68
69
|
import { v4 } from "uuid";
|
|
69
70
|
import { ContextOverflowError } from "@langchain/core/errors";
|
|
@@ -199,10 +200,13 @@ function minDefined(left, right) {
|
|
|
199
200
|
if (right == null) return left;
|
|
200
201
|
return Math.min(left, right);
|
|
201
202
|
}
|
|
202
|
-
async function dispatchMessageCreationStep({ graph, stepKey, messageId, metadata }) {
|
|
203
|
+
async function dispatchMessageCreationStep({ graph, stepKey, messageId, content, contentType, metadata }) {
|
|
203
204
|
await graph.dispatchRunStep(stepKey, {
|
|
204
205
|
type: "message_creation",
|
|
205
|
-
message_creation: {
|
|
206
|
+
message_creation: {
|
|
207
|
+
message_id: messageId,
|
|
208
|
+
...getMessageCreationContentMetadata(content, contentType)
|
|
209
|
+
}
|
|
206
210
|
}, metadata);
|
|
207
211
|
return graph.getStepIdByKey(stepKey);
|
|
208
212
|
}
|
|
@@ -215,19 +219,25 @@ async function dispatchTextMessageContent({ graph, stepKey, provider, content, m
|
|
|
215
219
|
graph,
|
|
216
220
|
stepKey,
|
|
217
221
|
messageId,
|
|
222
|
+
content: [contentPart],
|
|
218
223
|
metadata
|
|
219
224
|
});
|
|
220
225
|
await graph.dispatchMessageDelta(stepId, { content: [contentPart] }, metadata);
|
|
221
226
|
}
|
|
222
227
|
return true;
|
|
223
228
|
}
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
229
|
+
const contentGroups = Array.isArray(content) ? splitAssistantTextContentByPhase(content) : [content];
|
|
230
|
+
for (const contentGroup of contentGroups) {
|
|
231
|
+
const stepId = await dispatchMessageCreationStep({
|
|
232
|
+
graph,
|
|
233
|
+
stepKey,
|
|
234
|
+
messageId,
|
|
235
|
+
content: contentGroup,
|
|
236
|
+
contentType: "text",
|
|
237
|
+
metadata
|
|
238
|
+
});
|
|
239
|
+
await graph.dispatchMessageDelta(stepId, { content: contentGroup }, metadata);
|
|
240
|
+
}
|
|
231
241
|
return true;
|
|
232
242
|
}
|
|
233
243
|
async function dispatchReasoningContent({ graph, agentContext, reasoningContent, metadata }) {
|
|
@@ -246,7 +256,10 @@ async function dispatchReasoningContent({ graph, agentContext, reasoningContent,
|
|
|
246
256
|
}
|
|
247
257
|
await graph.dispatchRunStep(stepKey, {
|
|
248
258
|
type: "message_creation",
|
|
249
|
-
message_creation: {
|
|
259
|
+
message_creation: {
|
|
260
|
+
message_id: messageId,
|
|
261
|
+
content_type: "think"
|
|
262
|
+
}
|
|
250
263
|
}, metadata);
|
|
251
264
|
const stepId = graph.getStepIdByKey(stepKey);
|
|
252
265
|
await graph.dispatchReasoningDelta(stepId, { content: [{
|