@librechat/agents 3.4.4 → 3.4.6
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/common/enum.cjs +2 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +268 -14
- package/dist/cjs/graphs/Graph.cjs.map +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/langfuseTraceShaping.cjs +9 -6
- package/dist/cjs/langfuseTraceShaping.cjs.map +1 -1
- package/dist/cjs/main.cjs +6 -0
- package/dist/cjs/run.cjs +147 -14
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/session/AgentSession.cjs +93 -31
- package/dist/cjs/session/AgentSession.cjs.map +1 -1
- package/dist/cjs/session/handlers.cjs +10 -0
- package/dist/cjs/session/handlers.cjs.map +1 -1
- package/dist/cjs/stream.cjs +17 -3
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +1 -0
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +4 -2
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +1 -0
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +28 -0
- 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/handlers.cjs +3 -0
- package/dist/cjs/utils/handlers.cjs.map +1 -1
- package/dist/esm/common/enum.mjs +2 -0
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +268 -14
- package/dist/esm/graphs/Graph.mjs.map +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/langfuseTraceShaping.mjs +9 -6
- package/dist/esm/langfuseTraceShaping.mjs.map +1 -1
- package/dist/esm/main.mjs +3 -1
- package/dist/esm/run.mjs +147 -14
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/session/AgentSession.mjs +93 -31
- package/dist/esm/session/AgentSession.mjs.map +1 -1
- package/dist/esm/session/handlers.mjs +10 -0
- package/dist/esm/session/handlers.mjs.map +1 -1
- package/dist/esm/stream.mjs +17 -3
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +1 -0
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +4 -2
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +1 -0
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +28 -0
- 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/handlers.mjs +3 -0
- package/dist/esm/utils/handlers.mjs.map +1 -1
- package/dist/types/common/enum.d.ts +2 -0
- package/dist/types/graphs/Graph.d.ts +87 -0
- 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/langfuseTraceShaping.d.ts +4 -3
- package/dist/types/run.d.ts +9 -1
- package/dist/types/session/types.d.ts +1 -1
- package/dist/types/summarization/node.d.ts +6 -0
- package/dist/types/types/graph.d.ts +3 -3
- package/dist/types/types/hitl.d.ts +31 -2
- package/dist/types/types/stream.d.ts +57 -0
- package/dist/types/utils/handlers.d.ts +1 -0
- package/package.json +2 -1
- package/src/aggregator.test.ts +127 -0
- package/src/common/enum.ts +2 -0
- package/src/graphs/Graph.ts +365 -7
- package/src/graphs/__tests__/Graph.closeRunStep.test.ts +385 -0
- package/src/hitl/askUserQuestions.ts +126 -0
- package/src/hitl/askUserQuestionsInterrupt.ts +115 -0
- package/src/hitl/index.ts +6 -0
- package/src/langfuseTraceShaping.ts +14 -7
- package/src/run.ts +219 -41
- package/src/session/AgentSession.ts +105 -13
- package/src/session/handlers.ts +14 -2
- package/src/session/types.ts +1 -0
- package/src/specs/activity-label-observability.live.test.ts +262 -0
- package/src/specs/activity-label-observability.test.ts +167 -0
- package/src/specs/ask-user-questions.live.test.ts +185 -0
- package/src/specs/ask-user-questions.test.ts +293 -0
- package/src/specs/langfuse-trace-shaping.test.ts +32 -0
- package/src/specs/run-step-timestamps.test.ts +414 -0
- package/src/stream.ts +29 -6
- package/src/summarization/node.ts +11 -0
- package/src/tools/ToolNode.ts +2 -0
- package/src/tools/__tests__/SubagentExecutor.test.ts +52 -10
- package/src/tools/__tests__/handlers.test.ts +2 -0
- package/src/tools/handlers.ts +1 -0
- package/src/tools/subagent/SubagentExecutor.ts +46 -4
- package/src/types/graph.ts +9 -6
- package/src/types/hitl.ts +36 -2
- package/src/types/stream.ts +65 -12
- package/src/utils/handlers.ts +13 -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
|
+
export { isAskUserQuestionInterrupt };
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=hitl.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hitl.mjs","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"}
|
|
@@ -36,6 +36,9 @@ function createHandlers(callbacks) {
|
|
|
36
36
|
});
|
|
37
37
|
callbacks?.onRunStepDelta?.(event, data);
|
|
38
38
|
} },
|
|
39
|
+
["on_run_step_closed"]: { handle: (event, data) => {
|
|
40
|
+
callbacks?.onRunStepClosed?.(event, data);
|
|
41
|
+
} },
|
|
39
42
|
["on_message_delta"]: { handle: (event, data) => {
|
|
40
43
|
aggregateContent({
|
|
41
44
|
event,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handlers.mjs","names":[],"sources":["../../../src/utils/handlers.ts"],"sourcesContent":["/**\n * Multi-Agent Handler Utilities\n *\n * Provides a simple helper to create handlers with content aggregation for multi-agent scripts.\n *\n * Usage:\n * ```typescript\n * const { contentParts, aggregateContent, handlers } = createHandlers();\n *\n * // With callbacks\n * const { contentParts, aggregateContent, handlers } = createHandlers({\n * onRunStep: (event, data) => console.log('Step:', data),\n * onRunStepCompleted: (event, data) => console.log('Completed:', data)\n * });\n * ```\n */\n\nimport type * as t from '@/types';\nimport { ChatModelStreamHandler, createContentAggregator } from '@/stream';\nimport { ToolEndHandler, ModelEndHandler } from '@/events';\nimport { GraphEvents } from '@/common';\n\ninterface HandlerCallbacks {\n onRunStep?: (event: GraphEvents.ON_RUN_STEP, data: t.StreamEventData) => void;\n onRunStepCompleted?: (\n event: GraphEvents.ON_RUN_STEP_COMPLETED,\n data: t.StreamEventData\n ) => void;\n onRunStepDelta?: (\n event: GraphEvents.ON_RUN_STEP_DELTA,\n data: t.StreamEventData\n ) => void;\n onMessageDelta?: (\n event: GraphEvents.ON_MESSAGE_DELTA,\n data: t.StreamEventData\n ) => void;\n}\n\n/**\n * Creates handlers with content aggregation for multi-agent scripts\n */\nexport function createHandlers(callbacks?: HandlerCallbacks): {\n contentParts: Array<t.MessageContentComplex | undefined>;\n aggregateContent: ReturnType<\n typeof createContentAggregator\n >['aggregateContent'];\n handlers: Record<string, t.EventHandler>;\n} {\n // Set up content aggregator\n const { contentParts, aggregateContent } = createContentAggregator();\n\n // Create the handlers object\n const handlers = {\n [GraphEvents.TOOL_END]: new ToolEndHandler(),\n [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),\n [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),\n\n [GraphEvents.ON_RUN_STEP]: {\n handle: (\n event: GraphEvents.ON_RUN_STEP,\n data: t.StreamEventData\n ): void => {\n aggregateContent({ event, data: data as t.RunStep });\n callbacks?.onRunStep?.(event, data);\n },\n },\n\n [GraphEvents.ON_RUN_STEP_COMPLETED]: {\n handle: (\n event: GraphEvents.ON_RUN_STEP_COMPLETED,\n data: t.StreamEventData\n ): void => {\n aggregateContent({\n event,\n data: data as unknown as { result: t.ToolEndEvent },\n });\n callbacks?.onRunStepCompleted?.(event, data);\n },\n },\n\n [GraphEvents.ON_RUN_STEP_DELTA]: {\n handle: (\n event: GraphEvents.ON_RUN_STEP_DELTA,\n data: t.StreamEventData\n ): void => {\n aggregateContent({ event, data: data as t.RunStepDeltaEvent });\n callbacks?.onRunStepDelta?.(event, data);\n },\n },\n\n [GraphEvents.ON_MESSAGE_DELTA]: {\n handle: (\n event: GraphEvents.ON_MESSAGE_DELTA,\n data: t.StreamEventData\n ): void => {\n aggregateContent({ event, data: data as t.MessageDeltaEvent });\n callbacks?.onMessageDelta?.(event, data);\n },\n },\n\n [GraphEvents.ON_SUMMARIZE_DELTA]: {\n handle: (event: string, data: t.StreamEventData): void => {\n aggregateContent({\n event: event as GraphEvents,\n data: data as t.SummarizeDeltaData,\n });\n },\n },\n\n [GraphEvents.ON_SUMMARIZE_COMPLETE]: {\n handle: (event: string, data: t.StreamEventData): void => {\n aggregateContent({\n event: event as GraphEvents,\n data: data as t.SummarizeCompleteEvent,\n });\n },\n },\n };\n\n return {\n contentParts,\n aggregateContent,\n handlers,\n };\n}\n"],"mappings":";;;;;;;;
|
|
1
|
+
{"version":3,"file":"handlers.mjs","names":[],"sources":["../../../src/utils/handlers.ts"],"sourcesContent":["/**\n * Multi-Agent Handler Utilities\n *\n * Provides a simple helper to create handlers with content aggregation for multi-agent scripts.\n *\n * Usage:\n * ```typescript\n * const { contentParts, aggregateContent, handlers } = createHandlers();\n *\n * // With callbacks\n * const { contentParts, aggregateContent, handlers } = createHandlers({\n * onRunStep: (event, data) => console.log('Step:', data),\n * onRunStepCompleted: (event, data) => console.log('Completed:', data)\n * });\n * ```\n */\n\nimport type * as t from '@/types';\nimport { ChatModelStreamHandler, createContentAggregator } from '@/stream';\nimport { ToolEndHandler, ModelEndHandler } from '@/events';\nimport { GraphEvents } from '@/common';\n\ninterface HandlerCallbacks {\n onRunStep?: (event: GraphEvents.ON_RUN_STEP, data: t.StreamEventData) => void;\n onRunStepCompleted?: (\n event: GraphEvents.ON_RUN_STEP_COMPLETED,\n data: t.StreamEventData\n ) => void;\n onRunStepDelta?: (\n event: GraphEvents.ON_RUN_STEP_DELTA,\n data: t.StreamEventData\n ) => void;\n onRunStepClosed?: (\n event: GraphEvents.ON_RUN_STEP_CLOSED,\n data: t.RunStepClosedEvent\n ) => void;\n onMessageDelta?: (\n event: GraphEvents.ON_MESSAGE_DELTA,\n data: t.StreamEventData\n ) => void;\n}\n\n/**\n * Creates handlers with content aggregation for multi-agent scripts\n */\nexport function createHandlers(callbacks?: HandlerCallbacks): {\n contentParts: Array<t.MessageContentComplex | undefined>;\n aggregateContent: ReturnType<\n typeof createContentAggregator\n >['aggregateContent'];\n handlers: Record<string, t.EventHandler>;\n} {\n // Set up content aggregator\n const { contentParts, aggregateContent } = createContentAggregator();\n\n // Create the handlers object\n const handlers = {\n [GraphEvents.TOOL_END]: new ToolEndHandler(),\n [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),\n [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),\n\n [GraphEvents.ON_RUN_STEP]: {\n handle: (\n event: GraphEvents.ON_RUN_STEP,\n data: t.StreamEventData\n ): void => {\n aggregateContent({ event, data: data as t.RunStep });\n callbacks?.onRunStep?.(event, data);\n },\n },\n\n [GraphEvents.ON_RUN_STEP_COMPLETED]: {\n handle: (\n event: GraphEvents.ON_RUN_STEP_COMPLETED,\n data: t.StreamEventData\n ): void => {\n aggregateContent({\n event,\n data: data as unknown as { result: t.ToolEndEvent },\n });\n callbacks?.onRunStepCompleted?.(event, data);\n },\n },\n\n [GraphEvents.ON_RUN_STEP_DELTA]: {\n handle: (\n event: GraphEvents.ON_RUN_STEP_DELTA,\n data: t.StreamEventData\n ): void => {\n aggregateContent({ event, data: data as t.RunStepDeltaEvent });\n callbacks?.onRunStepDelta?.(event, data);\n },\n },\n\n [GraphEvents.ON_RUN_STEP_CLOSED]: {\n handle: (\n event: GraphEvents.ON_RUN_STEP_CLOSED,\n data: t.StreamEventData\n ): void => {\n callbacks?.onRunStepClosed?.(event, data as t.RunStepClosedEvent);\n },\n },\n\n [GraphEvents.ON_MESSAGE_DELTA]: {\n handle: (\n event: GraphEvents.ON_MESSAGE_DELTA,\n data: t.StreamEventData\n ): void => {\n aggregateContent({ event, data: data as t.MessageDeltaEvent });\n callbacks?.onMessageDelta?.(event, data);\n },\n },\n\n [GraphEvents.ON_SUMMARIZE_DELTA]: {\n handle: (event: string, data: t.StreamEventData): void => {\n aggregateContent({\n event: event as GraphEvents,\n data: data as t.SummarizeDeltaData,\n });\n },\n },\n\n [GraphEvents.ON_SUMMARIZE_COMPLETE]: {\n handle: (event: string, data: t.StreamEventData): void => {\n aggregateContent({\n event: event as GraphEvents,\n data: data as t.SummarizeCompleteEvent,\n });\n },\n },\n };\n\n return {\n contentParts,\n aggregateContent,\n handlers,\n };\n}\n"],"mappings":";;;;;;;;AA6CA,SAAgB,eAAe,WAM7B;CAEA,MAAM,EAAE,cAAc,qBAAqB,wBAAwB;CA+EnE,OAAO;EACL;EACA;EACA,UAAA;oBA9EwB,IAAI,eAAe;0BACb,IAAI,gBAAgB;6BACjB,IAAI,uBAAuB;oBAEjC,EACzB,SACE,OACA,SACS;IACT,iBAAiB;KAAE;KAAa;IAAkB,CAAC;IACnD,WAAW,YAAY,OAAO,IAAI;GACpC,EACF;8BAEqC,EACnC,SACE,OACA,SACS;IACT,iBAAiB;KACf;KACM;IACR,CAAC;IACD,WAAW,qBAAqB,OAAO,IAAI;GAC7C,EACF;0BAEiC,EAC/B,SACE,OACA,SACS;IACT,iBAAiB;KAAE;KAAa;IAA4B,CAAC;IAC7D,WAAW,iBAAiB,OAAO,IAAI;GACzC,EACF;2BAEkC,EAChC,SACE,OACA,SACS;IACT,WAAW,kBAAkB,OAAO,IAA4B;GAClE,EACF;yBAEgC,EAC9B,SACE,OACA,SACS;IACT,iBAAiB;KAAE;KAAa;IAA4B,CAAC;IAC7D,WAAW,iBAAiB,OAAO,IAAI;GACzC,EACF;2BAEkC,EAChC,SAAS,OAAe,SAAkC;IACxD,iBAAiB;KACR;KACD;IACR,CAAC;GACH,EACF;8BAEqC,EACnC,SAAS,OAAe,SAAkC;IACxD,iBAAiB;KACR;KACD;IACR,CAAC;GACH,EACF;EAMO;CACT;AACF"}
|
|
@@ -13,6 +13,8 @@ export declare enum GraphEvents {
|
|
|
13
13
|
ON_RUN_STEP_DELTA = "on_run_step_delta",
|
|
14
14
|
/** [Custom] Completed event for run steps (tool calls) */
|
|
15
15
|
ON_RUN_STEP_COMPLETED = "on_run_step_completed",
|
|
16
|
+
/** [Custom] Terminal signal for a run step: closed with status + timestamps */
|
|
17
|
+
ON_RUN_STEP_CLOSED = "on_run_step_closed",
|
|
16
18
|
/** [Custom] Delta events for messages */
|
|
17
19
|
ON_MESSAGE_DELTA = "on_message_delta",
|
|
18
20
|
/** [Custom] Reasoning Delta events for messages */
|
|
@@ -45,6 +45,17 @@ export declare abstract class Graph<T extends t.BaseGraphState = t.BaseGraphStat
|
|
|
45
45
|
stepKeyIds: Map<string, string[]>;
|
|
46
46
|
contentIndexMap: Map<string, number>;
|
|
47
47
|
toolCallStepIds: Map<string, string>;
|
|
48
|
+
/** Step ID -> tool call IDs whose completions have not yet arrived. */
|
|
49
|
+
pendingToolCallsByStep: Map<string, Set<string>>;
|
|
50
|
+
/**
|
|
51
|
+
* Step ID -> latest producer completion time seen for that step. Parallel
|
|
52
|
+
* calls sharing a step can settle out of producer order when their host
|
|
53
|
+
* handlers differ in latency, so the call that happens to drain the set is
|
|
54
|
+
* not necessarily the one that finished last.
|
|
55
|
+
*/
|
|
56
|
+
latestCompletionByStep: Map<string, number>;
|
|
57
|
+
/** Agent key ('' for single-agent) -> currently open MESSAGE_CREATION step ID. */
|
|
58
|
+
openMessageStepByAgent: Map<string, string>;
|
|
48
59
|
/**
|
|
49
60
|
* Step IDs dispatched through the handler registry during this run.
|
|
50
61
|
* Event echo suppression is tracked separately so repeated deltas for
|
|
@@ -153,6 +164,16 @@ export declare abstract class Graph<T extends t.BaseGraphState = t.BaseGraphStat
|
|
|
153
164
|
clearHeavyState(): void;
|
|
154
165
|
getEagerEventToolUsageCount(agentId?: string): Map<string, number>;
|
|
155
166
|
protected clearEagerEventToolUsageCounts(): void;
|
|
167
|
+
/**
|
|
168
|
+
* Tracks a tool call whose completion must arrive before its step can be
|
|
169
|
+
* considered finished. Registered wherever `toolCallStepIds` gains entries,
|
|
170
|
+
* except cross-process subagent resume restoration, where pending state
|
|
171
|
+
* cannot be faithfully rebuilt and closes fall back to completions/sweep.
|
|
172
|
+
*/
|
|
173
|
+
registerPendingToolCall(toolCallId: string, stepId: string): void;
|
|
174
|
+
/** Lazily creates a step's pending-completions set; callers registering a
|
|
175
|
+
* batch hoist this lookup out of their per-call loop. */
|
|
176
|
+
protected getPendingToolCallSet(stepId: string): Set<string>;
|
|
156
177
|
markHandlerDispatchedEvent(eventName: string, stepId: string): () => void;
|
|
157
178
|
hasHandlerDispatchedEvent(eventName: string, stepId: string): boolean;
|
|
158
179
|
/**
|
|
@@ -415,6 +436,72 @@ export declare class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode>
|
|
|
415
436
|
releasePreemptSeal(): void;
|
|
416
437
|
getPreemptStats(): t.PreemptStats;
|
|
417
438
|
getRunStep(stepId: string): t.RunStep | undefined;
|
|
439
|
+
/**
|
|
440
|
+
* Derives the same lane key `dispatchRunStep` stamps as `runStep.agentId`.
|
|
441
|
+
* The multi-agent check gates the lookup because `getAgentContext` signals
|
|
442
|
+
* a miss by throwing: single-agent graphs key every step under `''`, so
|
|
443
|
+
* resolving the context could only ever produce a thrown-and-discarded
|
|
444
|
+
* Error on a per-model-call path.
|
|
445
|
+
*/
|
|
446
|
+
protected getStepAgentKey(metadata?: Record<string, unknown>): string;
|
|
447
|
+
/**
|
|
448
|
+
* O(1) reverse lookup: both dispatch funnels key the open-message map by
|
|
449
|
+
* `runStep.agentId ?? ''`, so the entry is addressable without scanning.
|
|
450
|
+
*/
|
|
451
|
+
private untrackRunStep;
|
|
452
|
+
/**
|
|
453
|
+
* Shared step accounting for both dispatch funnels: a successor step in
|
|
454
|
+
* the same agent lane marks the previous message step as finished — its
|
|
455
|
+
* CLOSED event must precede the successor's ON_RUN_STEP so hosts observe
|
|
456
|
+
* a consistent open-step timeline — then the step is registered in the
|
|
457
|
+
* content maps and tracked as the lane's open message step when
|
|
458
|
+
* applicable.
|
|
459
|
+
*/
|
|
460
|
+
protected trackDispatchedRunStep(runStep: t.RunStep, metadata?: Record<string, unknown>,
|
|
461
|
+
/**
|
|
462
|
+
* Summarization steps are typed MESSAGE_CREATION but own an explicit
|
|
463
|
+
* completion, and their model call emits `CHAT_MODEL_END` well before the
|
|
464
|
+
* summary is assembled. Tracking one as the lane's open step would let
|
|
465
|
+
* model-end publish an authoritative `completed` closure early — the
|
|
466
|
+
* measured duration would exclude the remaining work and a later
|
|
467
|
+
* post-model failure could no longer change the status. They close
|
|
468
|
+
* through `recordStepCompletion` instead.
|
|
469
|
+
*/
|
|
470
|
+
trackAsOpenMessageStep?: boolean): Promise<void>;
|
|
471
|
+
/**
|
|
472
|
+
* Closes a run step: stamps its terminal status + timestamp on the stored
|
|
473
|
+
* `RunStep` and emits `ON_RUN_STEP_CLOSED`. First close wins — later calls
|
|
474
|
+
* are no-ops — except a `restamp` close, which lets a `completed`
|
|
475
|
+
* TOOL_CALLS step refresh `completed_at` when a late-registered parallel
|
|
476
|
+
* tool call finishes after the step already closed (the eager-execution
|
|
477
|
+
* race). `cancelled`/`failed` are immutable once stamped.
|
|
478
|
+
*/
|
|
479
|
+
closeRunStep(stepId: string, status: Exclude<t.RunStepStatus, 'in_progress'>, options?: t.RunStepCloseOptions): Promise<boolean>;
|
|
480
|
+
/**
|
|
481
|
+
* Observes one `ON_RUN_STEP_COMPLETED` for a step and closes the step when
|
|
482
|
+
* no registered tool calls remain pending. Steps without pending tracking
|
|
483
|
+
* (summaries, cross-process resume) close on their first completion; the
|
|
484
|
+
* terminal-status guard in `closeRunStep` absorbs duplicate echoes.
|
|
485
|
+
*/
|
|
486
|
+
recordStepCompletion(stepId: string, options?: t.RecordStepCompletionOptions): Promise<void>;
|
|
487
|
+
/**
|
|
488
|
+
* Closes the tracked open MESSAGE_CREATION step for the event's agent lane.
|
|
489
|
+
* Fires on every model end, so the empty-map check short-circuits ahead of
|
|
490
|
+
* resolving the lane key — a turn whose message step already closed through
|
|
491
|
+
* successor-close does no work here.
|
|
492
|
+
*/
|
|
493
|
+
closeOpenMessageStep(metadata?: Record<string, unknown>,
|
|
494
|
+
/** Model-end time captured before host handlers ran, so a slow usage sink
|
|
495
|
+
* cannot inflate the step's measured duration. */
|
|
496
|
+
at?: number): Promise<void>;
|
|
497
|
+
/**
|
|
498
|
+
* End-of-run sweep: closes every step that never reached a terminal
|
|
499
|
+
* status. Dual-dispatches like any other close — the custom-event channel
|
|
500
|
+
* is usually already torn down here and `safeDispatchCustomEvent` reports
|
|
501
|
+
* that quietly, but callback-only subscribers still receive the terminal
|
|
502
|
+
* signal whenever it is alive.
|
|
503
|
+
*/
|
|
504
|
+
closeUnfinishedRunSteps(status: Exclude<t.RunStepStatus, 'in_progress'>, at?: number): Promise<void>;
|
|
418
505
|
getAgentContext(metadata: Record<string, unknown> | undefined): AgentContext;
|
|
419
506
|
getStepBaseKey(metadata: Record<string, unknown> | undefined): string;
|
|
420
507
|
getStepKey(metadata: Record<string, unknown> | undefined): string;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AskUserQuestionsRequest, AskUserQuestionsResolution } from '@/types/hitl';
|
|
2
|
+
/**
|
|
3
|
+
* Suspend once to collect answers to several related questions. The first
|
|
4
|
+
* question is also included in the legacy `question` field so existing hosts
|
|
5
|
+
* can render a useful fallback during a staged rollout.
|
|
6
|
+
*
|
|
7
|
+
* Question ids must be non-empty and unique within the batch. The helper
|
|
8
|
+
* accepts at most four questions so hosts can render the interaction as one
|
|
9
|
+
* focused decision surface rather than an unbounded form.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const { answers } = askUserQuestions({
|
|
14
|
+
* questions: [
|
|
15
|
+
* { id: 'environment', question: 'Which environment?' },
|
|
16
|
+
* { id: 'region', question: 'Which region?' },
|
|
17
|
+
* ],
|
|
18
|
+
* });
|
|
19
|
+
* return `Deploy to ${answers.environment} in ${answers.region}`;
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function askUserQuestions(request: AskUserQuestionsRequest, options?: {
|
|
23
|
+
toolCallId?: string;
|
|
24
|
+
}): AskUserQuestionsResolution;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AskUserQuestionRequest, AskUserQuestionsInterruptPayload } from '@/types/hitl';
|
|
2
|
+
/** Maximum questions supported by one batched clarification interaction. */
|
|
3
|
+
export declare const MAX_ASK_USER_QUESTIONS = 4;
|
|
4
|
+
/** Safe identifier format for answer-map keys in a batched question. */
|
|
5
|
+
export declare const ASK_USER_QUESTION_ID_PATTERN: RegExp;
|
|
6
|
+
export declare function isAskUserQuestionRequest(value: unknown): value is AskUserQuestionRequest;
|
|
7
|
+
/**
|
|
8
|
+
* Type guard for the batched form of an `ask_user_question` interrupt. Hosts
|
|
9
|
+
* use this to select the multi-question UI and `AskUserQuestionsResolution`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isAskUserQuestionsInterrupt(payload: unknown): payload is AskUserQuestionsInterruptPayload;
|
|
@@ -4,3 +4,5 @@
|
|
|
4
4
|
* `askUserQuestion()`) live here.
|
|
5
5
|
*/
|
|
6
6
|
export { askUserQuestion } from './askUserQuestion';
|
|
7
|
+
export { askUserQuestions } from './askUserQuestions';
|
|
8
|
+
export { ASK_USER_QUESTION_ID_PATTERN, isAskUserQuestionsInterrupt, MAX_ASK_USER_QUESTIONS, } from './askUserQuestionsInterrupt';
|
|
@@ -20,8 +20,9 @@ export declare function shouldDropLangfuseSpan(spanName: string): boolean;
|
|
|
20
20
|
* - Agent nodes become `agent` observations, while tool-dispatch nodes become
|
|
21
21
|
* stable `chain` observations whose input is scoped to the pending calls.
|
|
22
22
|
* Individual child calls remain `tool` observations (items 3 & 4).
|
|
23
|
-
* - Agent trace roots become `agent` observations
|
|
24
|
-
* `chain` observations. Root and trace input/output are
|
|
25
|
-
* question and assistant response when chat messages are
|
|
23
|
+
* - Agent trace roots become `agent` observations, while title and activity
|
|
24
|
+
* summary roots become `chain` observations. Root and trace input/output are
|
|
25
|
+
* reduced to the user question and assistant response when chat messages are
|
|
26
|
+
* available (item 2).
|
|
26
27
|
*/
|
|
27
28
|
export declare function shapeLangfuseSpan(span: ReadableSpan): void;
|
package/dist/types/run.d.ts
CHANGED
|
@@ -138,6 +138,14 @@ export declare class Run<_T extends t.BaseGraphState> {
|
|
|
138
138
|
private createCustomEventCallback;
|
|
139
139
|
private shouldClearHookSession;
|
|
140
140
|
private isAwaitingResume;
|
|
141
|
+
/**
|
|
142
|
+
* Terminal status for steps still open at end-of-run: `cancelled` for
|
|
143
|
+
* intentional stops (caller abort, hook halt), `failed` for unexpected
|
|
144
|
+
* stream errors, `completed` for a natural finish. Reads `_haltedReason`
|
|
145
|
+
* behind a method boundary on purpose — it is assigned inside the
|
|
146
|
+
* `consumeStream` closure, which control-flow narrowing cannot see.
|
|
147
|
+
*/
|
|
148
|
+
private resolveSweepStatus;
|
|
141
149
|
private getStreamLangfuseConfig;
|
|
142
150
|
private getStreamToolOutputTracingLangfuseConfig;
|
|
143
151
|
processStream(inputs: t.IState | Command, callerConfig: t.RunStreamConfig, streamOptions?: t.EventStreamOptions): Promise<MessageContentComplex[] | undefined>;
|
|
@@ -247,7 +255,7 @@ export declare class Run<_T extends t.BaseGraphState> {
|
|
|
247
255
|
}>;
|
|
248
256
|
/**
|
|
249
257
|
* Generates one parent summary for two or more logical activities. The
|
|
250
|
-
* summary model is traced as a dedicated activity-phase
|
|
258
|
+
* summary model is traced as a dedicated activity-phase chain root in the
|
|
251
259
|
* conversation session, with the model callback recorded as its generation
|
|
252
260
|
* child. No session id means no phase trace, avoiding orphan observations.
|
|
253
261
|
*/
|
|
@@ -150,7 +150,7 @@ export interface AgentSessionRunOptions {
|
|
|
150
150
|
streamOptions?: t.EventStreamOptions;
|
|
151
151
|
}
|
|
152
152
|
export interface AgentSessionStreamEvent {
|
|
153
|
-
type: 'run.started' | 'message.delta' | 'reasoning.delta' | 'tool.started' | 'tool.delta' | 'tool.completed' | 'usage.updated' | 'run.completed' | 'run.failed' | 'run.interrupted' | 'run.halted';
|
|
153
|
+
type: 'run.started' | 'message.delta' | 'reasoning.delta' | 'tool.started' | 'tool.delta' | 'tool.completed' | 'step.finished' | 'usage.updated' | 'run.completed' | 'run.failed' | 'run.interrupted' | 'run.halted';
|
|
154
154
|
sequence: number;
|
|
155
155
|
runId: string;
|
|
156
156
|
threadId: string;
|
|
@@ -21,6 +21,12 @@ interface CreateSummarizeNodeParams {
|
|
|
21
21
|
hookRegistry?: HookRegistry;
|
|
22
22
|
dispatchRunStep: (runStep: t.RunStep, config?: RunnableConfig) => Promise<void>;
|
|
23
23
|
dispatchRunStepCompleted: (stepId: string, result: t.StepCompleted, config?: RunnableConfig) => Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Terminal close for a summary step that ends without a completion —
|
|
26
|
+
* an errored or empty summary would otherwise stay `in_progress` until
|
|
27
|
+
* the run-end sweep reported it as `completed`.
|
|
28
|
+
*/
|
|
29
|
+
closeRunStep?: (stepId: string, status: Exclude<t.RunStepStatus, 'in_progress'>, config?: RunnableConfig) => Promise<void>;
|
|
24
30
|
/** The run's shared breaker signal, composed into every summarization
|
|
25
31
|
* model attempt so a sibling branch tripping a stream limit also
|
|
26
32
|
* cancels in-flight summaries. */
|
|
@@ -5,8 +5,8 @@ import type { RunnableConfig, Runnable } from '@langchain/core/runnables';
|
|
|
5
5
|
import type { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
6
6
|
import type { GoogleAIToolType } from '@langchain/google-common';
|
|
7
7
|
import type { SummarizationNodeInput, SummarizeCompleteEvent, SummarizationConfig, SummarizeStartEvent, SummarizeDeltaEvent } from '@/types/summarize';
|
|
8
|
+
import type { RunStep, RunStepDeltaEvent, RunStepClosedEvent, MessageDeltaEvent, ReasoningDeltaEvent } from '@/types/stream';
|
|
8
9
|
import type { ToolMap, ToolSessionMap, ToolEndEvent, GenericTool, LCTool, ToolExecuteBatchRequest } from '@/types/tools';
|
|
9
|
-
import type { RunStep, RunStepDeltaEvent, MessageDeltaEvent, ReasoningDeltaEvent } from '@/types/stream';
|
|
10
10
|
import type { TokenCounter, StreamLimits, StreamPreemption, TokenBudgetBreakdown } from '@/types/run';
|
|
11
11
|
import type { Providers, Callback, GraphNodeKeys } from '@/common';
|
|
12
12
|
import type { StandardGraph, MultiAgentGraph } from '@/graphs';
|
|
@@ -73,7 +73,7 @@ export interface ContextUsageEvent {
|
|
|
73
73
|
calibrationRatio?: number;
|
|
74
74
|
}
|
|
75
75
|
export interface EventHandler {
|
|
76
|
-
handle(event: string, data: StreamEventData | ModelEndData | RunStep | RunStepDeltaEvent | MessageDeltaEvent | ReasoningDeltaEvent | SummarizeStartEvent | SummarizeDeltaEvent | SummarizeCompleteEvent | SubagentUpdateEvent | AgentLogEvent | ContextUsageEvent | ToolExecuteBatchRequest | {
|
|
76
|
+
handle(event: string, data: StreamEventData | ModelEndData | RunStep | RunStepDeltaEvent | RunStepClosedEvent | MessageDeltaEvent | ReasoningDeltaEvent | SummarizeStartEvent | SummarizeDeltaEvent | SummarizeCompleteEvent | SubagentUpdateEvent | AgentLogEvent | ContextUsageEvent | ToolExecuteBatchRequest | {
|
|
77
77
|
result: ToolEndEvent;
|
|
78
78
|
}, metadata?: Record<string, unknown>, graph?: StandardGraph | MultiAgentGraph): void | Promise<void>;
|
|
79
79
|
}
|
|
@@ -442,7 +442,7 @@ export type ExecutableSubagentConfig = ResolvedSubagentConfig | LazySingleAgentS
|
|
|
442
442
|
/** Graph-aware config accepted by the executor. Graph configs stay eager. */
|
|
443
443
|
export type ExecutableSubagentConfigEntry = ExecutableSubagentConfig | GraphSubagentConfig;
|
|
444
444
|
/** Lifecycle phase carried on {@link SubagentUpdateEvent}. */
|
|
445
|
-
export type SubagentUpdatePhase = 'start' | 'run_step' | 'run_step_delta' | 'run_step_completed' | 'message_delta' | 'reasoning_delta' | 'stop' | 'error';
|
|
445
|
+
export type SubagentUpdatePhase = 'start' | 'run_step' | 'run_step_delta' | 'run_step_completed' | 'run_step_closed' | 'message_delta' | 'reasoning_delta' | 'stop' | 'error';
|
|
446
446
|
export interface SubagentAncestryEntry {
|
|
447
447
|
readonly subagentRunId: string;
|
|
448
448
|
readonly subagentType: string;
|
|
@@ -157,15 +157,35 @@ export interface AskUserQuestionRequest {
|
|
|
157
157
|
*/
|
|
158
158
|
multiSelect?: boolean;
|
|
159
159
|
}
|
|
160
|
+
/** One independently answerable question in a batched question request. */
|
|
161
|
+
export interface AskUserQuestionBatchItem extends AskUserQuestionRequest {
|
|
162
|
+
/** Batch-unique identifier (`[A-Za-z][A-Za-z0-9_-]{0,63}`). */
|
|
163
|
+
id: string;
|
|
164
|
+
/** Optional short heading rendered above the question. */
|
|
165
|
+
header?: string;
|
|
166
|
+
}
|
|
167
|
+
/** Input shape for one tool call that asks one to four questions together. */
|
|
168
|
+
export interface AskUserQuestionsRequest {
|
|
169
|
+
questions: AskUserQuestionBatchItem[];
|
|
170
|
+
}
|
|
160
171
|
/**
|
|
161
172
|
* Structured payload the SDK passes to `interrupt()` when an agent (or
|
|
162
173
|
* a custom node) needs to ask the user a clarifying question. Mirrors
|
|
163
|
-
* Claude Code's `AskUserQuestion` semantic. Resume value
|
|
164
|
-
* `AskUserQuestionResolution
|
|
174
|
+
* Claude Code's `AskUserQuestion` semantic. Resume value is
|
|
175
|
+
* `AskUserQuestionResolution` for a single question, or
|
|
176
|
+
* `AskUserQuestionsResolution` when `questions` is present.
|
|
165
177
|
*/
|
|
166
178
|
export interface AskUserQuestionInterruptPayload {
|
|
167
179
|
type: 'ask_user_question';
|
|
180
|
+
/**
|
|
181
|
+
* Single-question request, or the first question as a compatibility
|
|
182
|
+
* fallback when `questions` contains a batch. This lets existing hosts show
|
|
183
|
+
* a useful preview during a staged rollout, but they must support `questions`
|
|
184
|
+
* and `AskUserQuestionsResolution` before enabling a batched tool schema.
|
|
185
|
+
*/
|
|
168
186
|
question: AskUserQuestionRequest;
|
|
187
|
+
/** One to four questions collected by one `ask_user_question` tool call. */
|
|
188
|
+
questions?: AskUserQuestionsRequest['questions'];
|
|
169
189
|
/**
|
|
170
190
|
* The `tool_call_id` of the ask-tool call that raised this interrupt,
|
|
171
191
|
* when the tool body supplied it (see `askUserQuestion`'s `options`).
|
|
@@ -175,6 +195,10 @@ export interface AskUserQuestionInterruptPayload {
|
|
|
175
195
|
*/
|
|
176
196
|
tool_call_id?: string;
|
|
177
197
|
}
|
|
198
|
+
/** Batch-specialized ask payload for hosts that render several questions. */
|
|
199
|
+
export interface AskUserQuestionsInterruptPayload extends AskUserQuestionInterruptPayload {
|
|
200
|
+
questions: AskUserQuestionsRequest['questions'];
|
|
201
|
+
}
|
|
178
202
|
/**
|
|
179
203
|
* Discriminated union of every interrupt payload the SDK raises. New
|
|
180
204
|
* variants can be added without breaking existing handlers as long as
|
|
@@ -194,6 +218,11 @@ export interface AskUserQuestionResolution {
|
|
|
194
218
|
*/
|
|
195
219
|
answer: string;
|
|
196
220
|
}
|
|
221
|
+
/** Resume value for a batched `ask_user_question` interrupt. */
|
|
222
|
+
export interface AskUserQuestionsResolution {
|
|
223
|
+
/** Human answers keyed by each `AskUserQuestionBatchItem.id`. */
|
|
224
|
+
answers: Record<string, string>;
|
|
225
|
+
}
|
|
197
226
|
/**
|
|
198
227
|
* Type guard narrowing an arbitrary value to a `ToolApprovalInterruptPayload`.
|
|
199
228
|
* Accepts `unknown` (not just `HumanInterruptPayload`) because hosts can
|
|
@@ -2,6 +2,7 @@ import type { MessageContentImageUrl, MessageContentText, ToolMessage, BaseMessa
|
|
|
2
2
|
import type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';
|
|
3
3
|
import type { LLMResult, Generation } from '@langchain/core/outputs';
|
|
4
4
|
import type { Command } from '@langchain/langgraph';
|
|
5
|
+
import type Anthropic from '@anthropic-ai/sdk';
|
|
5
6
|
import type { AnthropicContentBlock } from '@/llm/anthropic/types';
|
|
6
7
|
import type { AssistantTextPhase } from '@/types/assistantPhase';
|
|
7
8
|
import type { SummarizeCompleteEvent } from '@/types/summarize';
|
|
@@ -34,8 +35,25 @@ start, stream and end are associated with slightly different data payload.
|
|
|
34
35
|
|
|
35
36
|
Please see the documentation for EventData for more details. */
|
|
36
37
|
export type EventName = string;
|
|
38
|
+
export type RunStepStatus = 'in_progress' | 'completed' | 'cancelled' | 'failed';
|
|
37
39
|
export type RunStep = {
|
|
38
40
|
type: StepTypes;
|
|
41
|
+
/** Epoch ms when the step was dispatched. */
|
|
42
|
+
created_at?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Lifecycle status; terminal values are stamped when the step closes.
|
|
45
|
+
* Invariant (enforced by `closeRunStep`, not the type, to stay wire-compatible
|
|
46
|
+
* with the OpenAI Assistants shape): a terminal status sets exactly one
|
|
47
|
+
* matching `*_at` field; first close wins and `cancelled`/`failed` are
|
|
48
|
+
* immutable once stamped.
|
|
49
|
+
*/
|
|
50
|
+
status?: RunStepStatus;
|
|
51
|
+
/** Epoch ms when the step closed with status `completed`. */
|
|
52
|
+
completed_at?: number;
|
|
53
|
+
/** Epoch ms when the step closed with status `cancelled` (abort/halt). */
|
|
54
|
+
cancelled_at?: number;
|
|
55
|
+
/** Epoch ms when the step closed with status `failed`. */
|
|
56
|
+
failed_at?: number;
|
|
39
57
|
id: string;
|
|
40
58
|
runId?: string;
|
|
41
59
|
agentId?: string;
|
|
@@ -71,6 +89,41 @@ export interface RunStepDeltaEvent {
|
|
|
71
89
|
*/
|
|
72
90
|
delta: ToolCallDelta;
|
|
73
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Terminal signal for a run step, emitted exactly once per step when it
|
|
94
|
+
* finishes (`completed`), is aborted/halted (`cancelled`), or the run errors
|
|
95
|
+
* (`failed`). The `id` is top-level so callback echoes dedupe like other
|
|
96
|
+
* step-scoped events.
|
|
97
|
+
*/
|
|
98
|
+
export interface RunStepClosedEvent {
|
|
99
|
+
id: string;
|
|
100
|
+
index: number;
|
|
101
|
+
type: StepTypes;
|
|
102
|
+
status: Exclude<RunStepStatus, 'in_progress'>;
|
|
103
|
+
/** Epoch ms when the step was dispatched, when known. */
|
|
104
|
+
created_at?: number;
|
|
105
|
+
/** Epoch ms when the step reached its terminal status. */
|
|
106
|
+
closed_at: number;
|
|
107
|
+
runId?: string;
|
|
108
|
+
agentId?: string;
|
|
109
|
+
groupId?: number;
|
|
110
|
+
stepIndex?: number;
|
|
111
|
+
}
|
|
112
|
+
export type RecordStepCompletionOptions = {
|
|
113
|
+
/** The completing tool call, when the step tracks pending completions. */
|
|
114
|
+
toolCallId?: string;
|
|
115
|
+
metadata?: Record<string, unknown>;
|
|
116
|
+
/**
|
|
117
|
+
* Producer-stamped completion time (epoch ms). Carried through so a slow
|
|
118
|
+
* host completion handler cannot inflate the recorded step duration.
|
|
119
|
+
*/
|
|
120
|
+
at?: number;
|
|
121
|
+
};
|
|
122
|
+
export type RunStepCloseOptions = {
|
|
123
|
+
/** Epoch ms for the terminal stamp; defaults to `Date.now()` at close time. */
|
|
124
|
+
at?: number;
|
|
125
|
+
metadata?: Record<string, unknown>;
|
|
126
|
+
};
|
|
74
127
|
export type StepDetails = MessageCreationDetails | ToolCallsDetails;
|
|
75
128
|
export type SummaryCompleted = {
|
|
76
129
|
type: 'summary';
|
|
@@ -132,6 +185,8 @@ export type ToolCompleteEvent = ToolCallCompleted & {
|
|
|
132
185
|
/** The content index of the tool call */
|
|
133
186
|
index: number;
|
|
134
187
|
type: 'tool_call';
|
|
188
|
+
/** Epoch ms when this tool call's completion was dispatched. */
|
|
189
|
+
completed_at?: number;
|
|
135
190
|
};
|
|
136
191
|
export type ToolCallsDetails = {
|
|
137
192
|
type: StepTypes.TOOL_CALLS;
|
|
@@ -224,6 +279,8 @@ export type MessageDeltaUpdate = {
|
|
|
224
279
|
type: ContentTypes.TEXT;
|
|
225
280
|
text: string;
|
|
226
281
|
tool_call_ids?: string[];
|
|
282
|
+
/** Provider-supplied source citations, accumulated across deltas. */
|
|
283
|
+
citations?: Anthropic.TextCitation[];
|
|
227
284
|
};
|
|
228
285
|
export type ReasoningDeltaUpdate = {
|
|
229
286
|
type: ContentTypes.THINK;
|
|
@@ -21,6 +21,7 @@ interface HandlerCallbacks {
|
|
|
21
21
|
onRunStep?: (event: GraphEvents.ON_RUN_STEP, data: t.StreamEventData) => void;
|
|
22
22
|
onRunStepCompleted?: (event: GraphEvents.ON_RUN_STEP_COMPLETED, data: t.StreamEventData) => void;
|
|
23
23
|
onRunStepDelta?: (event: GraphEvents.ON_RUN_STEP_DELTA, data: t.StreamEventData) => void;
|
|
24
|
+
onRunStepClosed?: (event: GraphEvents.ON_RUN_STEP_CLOSED, data: t.RunStepClosedEvent) => void;
|
|
24
25
|
onMessageDelta?: (event: GraphEvents.ON_MESSAGE_DELTA, data: t.StreamEventData) => void;
|
|
25
26
|
}
|
|
26
27
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@librechat/agents",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.6",
|
|
4
4
|
"reova": {
|
|
5
5
|
"enabled": true,
|
|
6
6
|
"endpoint": "https://telemetry.reo.dev/data"
|
|
@@ -199,6 +199,7 @@
|
|
|
199
199
|
"supervised": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/supervised.ts --provider anthropic --name Jo --location \"New York, NY\"",
|
|
200
200
|
"test": "NODE_OPTIONS='--experimental-vm-modules' jest",
|
|
201
201
|
"test:live:handoffs": "RUN_HANDOFF_LIVE_TESTS=1 NODE_OPTIONS='--experimental-vm-modules' jest src/specs/agent-handoffs.live.test.ts --runInBand",
|
|
202
|
+
"test:live:ask-user-questions": "RUN_ASK_USER_QUESTIONS_LIVE_TESTS=1 NODE_OPTIONS='--experimental-vm-modules' jest src/specs/ask-user-questions.live.test.ts --runInBand",
|
|
202
203
|
"test:memory": "NODE_OPTIONS='--expose-gc' npx jest src/specs/title.memory-leak.test.ts",
|
|
203
204
|
"test:all": "npm test -- --testPathIgnorePatterns=title.memory-leak.test.ts && npm run test:memory",
|
|
204
205
|
"reinstall": "npm run clean && npm ci && rm -rf ./dist && npm run build",
|