@illuma-ai/agents 1.1.25 → 1.3.0
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/agents/AgentContext.cjs +20 -3
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/spawnPath.cjs +104 -0
- package/dist/cjs/common/spawnPath.cjs.map +1 -0
- package/dist/cjs/graphs/Graph.cjs +87 -31
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/HandoffRegistry.cjs +143 -0
- package/dist/cjs/graphs/HandoffRegistry.cjs.map +1 -0
- package/dist/cjs/graphs/MultiAgentGraph.cjs +587 -184
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/graphs/phases/flushLoop.cjs +214 -0
- package/dist/cjs/graphs/phases/flushLoop.cjs.map +1 -0
- package/dist/cjs/graphs/phases/memoryFlushPhase.cjs +102 -0
- package/dist/cjs/graphs/phases/memoryFlushPhase.cjs.map +1 -0
- package/dist/cjs/llm/bedrock/index.cjs +4 -3
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +115 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/memory/citations.cjs +69 -0
- package/dist/cjs/memory/citations.cjs.map +1 -0
- package/dist/cjs/memory/compositeBackend.cjs +60 -0
- package/dist/cjs/memory/compositeBackend.cjs.map +1 -0
- package/dist/cjs/memory/constants.cjs +232 -0
- package/dist/cjs/memory/constants.cjs.map +1 -0
- package/dist/cjs/memory/embeddings.cjs +151 -0
- package/dist/cjs/memory/embeddings.cjs.map +1 -0
- package/dist/cjs/memory/factory.cjs +95 -0
- package/dist/cjs/memory/factory.cjs.map +1 -0
- package/dist/cjs/memory/migrate.cjs +81 -0
- package/dist/cjs/memory/migrate.cjs.map +1 -0
- package/dist/cjs/memory/mmr.cjs +138 -0
- package/dist/cjs/memory/mmr.cjs.map +1 -0
- package/dist/cjs/memory/paths.cjs +217 -0
- package/dist/cjs/memory/paths.cjs.map +1 -0
- package/dist/cjs/memory/pgvectorStore.cjs +225 -0
- package/dist/cjs/memory/pgvectorStore.cjs.map +1 -0
- package/dist/cjs/memory/recallTracking.cjs +98 -0
- package/dist/cjs/memory/recallTracking.cjs.map +1 -0
- package/dist/cjs/memory/schema.sql +51 -0
- package/dist/cjs/memory/temporalDecay.cjs +118 -0
- package/dist/cjs/memory/temporalDecay.cjs.map +1 -0
- package/dist/cjs/nodes/ApprovalGateNode.cjs +1 -1
- package/dist/cjs/nodes/ApprovalGateNode.cjs.map +1 -1
- package/dist/cjs/prompts/memoryFlushPrompt.cjs +49 -0
- package/dist/cjs/prompts/memoryFlushPrompt.cjs.map +1 -0
- package/dist/cjs/run.cjs +16 -3
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/stream.cjs +4 -4
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/AskUser.cjs +6 -1
- package/dist/cjs/tools/AskUser.cjs.map +1 -1
- package/dist/cjs/tools/BrowserTools.cjs +1 -1
- package/dist/cjs/tools/BrowserTools.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +127 -10
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/approval/constants.cjs +2 -2
- package/dist/cjs/tools/approval/constants.cjs.map +1 -1
- package/dist/cjs/tools/memory/index.cjs +58 -0
- package/dist/cjs/tools/memory/index.cjs.map +1 -0
- package/dist/cjs/tools/memory/memoryAppendTool.cjs +69 -0
- package/dist/cjs/tools/memory/memoryAppendTool.cjs.map +1 -0
- package/dist/cjs/tools/memory/memoryGetTool.cjs +49 -0
- package/dist/cjs/tools/memory/memoryGetTool.cjs.map +1 -0
- package/dist/cjs/tools/memory/memorySearchTool.cjs +65 -0
- package/dist/cjs/tools/memory/memorySearchTool.cjs.map +1 -0
- package/dist/cjs/tools/memory/shared.cjs +106 -0
- package/dist/cjs/tools/memory/shared.cjs.map +1 -0
- package/dist/cjs/types/graph.cjs.map +1 -1
- package/dist/cjs/utils/childAgentContext.cjs +242 -0
- package/dist/cjs/utils/childAgentContext.cjs.map +1 -0
- package/dist/cjs/utils/events.cjs +36 -4
- package/dist/cjs/utils/events.cjs.map +1 -1
- package/dist/cjs/utils/finishReasons.cjs +44 -0
- package/dist/cjs/utils/finishReasons.cjs.map +1 -0
- package/dist/cjs/utils/llm.cjs.map +1 -1
- package/dist/cjs/utils/logging.cjs +34 -0
- package/dist/cjs/utils/logging.cjs.map +1 -0
- package/dist/cjs/utils/toolCallNormalization.cjs +250 -0
- package/dist/cjs/utils/toolCallNormalization.cjs.map +1 -0
- package/dist/esm/agents/AgentContext.mjs +20 -3
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/spawnPath.mjs +95 -0
- package/dist/esm/common/spawnPath.mjs.map +1 -0
- package/dist/esm/graphs/Graph.mjs +87 -31
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/HandoffRegistry.mjs +141 -0
- package/dist/esm/graphs/HandoffRegistry.mjs.map +1 -0
- package/dist/esm/graphs/MultiAgentGraph.mjs +587 -184
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/graphs/phases/flushLoop.mjs +209 -0
- package/dist/esm/graphs/phases/flushLoop.mjs.map +1 -0
- package/dist/esm/graphs/phases/memoryFlushPhase.mjs +99 -0
- package/dist/esm/graphs/phases/memoryFlushPhase.mjs.map +1 -0
- package/dist/esm/llm/bedrock/index.mjs +4 -3
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/main.mjs +21 -0
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/memory/citations.mjs +64 -0
- package/dist/esm/memory/citations.mjs.map +1 -0
- package/dist/esm/memory/compositeBackend.mjs +58 -0
- package/dist/esm/memory/compositeBackend.mjs.map +1 -0
- package/dist/esm/memory/constants.mjs +198 -0
- package/dist/esm/memory/constants.mjs.map +1 -0
- package/dist/esm/memory/embeddings.mjs +148 -0
- package/dist/esm/memory/embeddings.mjs.map +1 -0
- package/dist/esm/memory/factory.mjs +93 -0
- package/dist/esm/memory/factory.mjs.map +1 -0
- package/dist/esm/memory/migrate.mjs +78 -0
- package/dist/esm/memory/migrate.mjs.map +1 -0
- package/dist/esm/memory/mmr.mjs +130 -0
- package/dist/esm/memory/mmr.mjs.map +1 -0
- package/dist/esm/memory/paths.mjs +207 -0
- package/dist/esm/memory/paths.mjs.map +1 -0
- package/dist/esm/memory/pgvectorStore.mjs +223 -0
- package/dist/esm/memory/pgvectorStore.mjs.map +1 -0
- package/dist/esm/memory/recallTracking.mjs +94 -0
- package/dist/esm/memory/recallTracking.mjs.map +1 -0
- package/dist/esm/memory/schema.sql +51 -0
- package/dist/esm/memory/temporalDecay.mjs +110 -0
- package/dist/esm/memory/temporalDecay.mjs.map +1 -0
- package/dist/esm/nodes/ApprovalGateNode.mjs +1 -1
- package/dist/esm/nodes/ApprovalGateNode.mjs.map +1 -1
- package/dist/esm/prompts/memoryFlushPrompt.mjs +44 -0
- package/dist/esm/prompts/memoryFlushPrompt.mjs.map +1 -0
- package/dist/esm/run.mjs +16 -3
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/stream.mjs +4 -4
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/AskUser.mjs +6 -1
- package/dist/esm/tools/AskUser.mjs.map +1 -1
- package/dist/esm/tools/BrowserTools.mjs +1 -1
- package/dist/esm/tools/BrowserTools.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +128 -11
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/approval/constants.mjs +2 -2
- package/dist/esm/tools/approval/constants.mjs.map +1 -1
- package/dist/esm/tools/memory/index.mjs +46 -0
- package/dist/esm/tools/memory/index.mjs.map +1 -0
- package/dist/esm/tools/memory/memoryAppendTool.mjs +67 -0
- package/dist/esm/tools/memory/memoryAppendTool.mjs.map +1 -0
- package/dist/esm/tools/memory/memoryGetTool.mjs +47 -0
- package/dist/esm/tools/memory/memoryGetTool.mjs.map +1 -0
- package/dist/esm/tools/memory/memorySearchTool.mjs +63 -0
- package/dist/esm/tools/memory/memorySearchTool.mjs.map +1 -0
- package/dist/esm/tools/memory/shared.mjs +98 -0
- package/dist/esm/tools/memory/shared.mjs.map +1 -0
- package/dist/esm/types/graph.mjs.map +1 -1
- package/dist/esm/utils/childAgentContext.mjs +237 -0
- package/dist/esm/utils/childAgentContext.mjs.map +1 -0
- package/dist/esm/utils/events.mjs +36 -5
- package/dist/esm/utils/events.mjs.map +1 -1
- package/dist/esm/utils/finishReasons.mjs +41 -0
- package/dist/esm/utils/finishReasons.mjs.map +1 -0
- package/dist/esm/utils/llm.mjs.map +1 -1
- package/dist/esm/utils/logging.mjs +31 -0
- package/dist/esm/utils/logging.mjs.map +1 -0
- package/dist/esm/utils/toolCallNormalization.mjs +247 -0
- package/dist/esm/utils/toolCallNormalization.mjs.map +1 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/common/spawnPath.d.ts +59 -0
- package/dist/types/graphs/HandoffRegistry.d.ts +97 -0
- package/dist/types/graphs/MultiAgentGraph.d.ts +58 -18
- package/dist/types/graphs/index.d.ts +1 -0
- package/dist/types/graphs/phases/flushLoop.d.ts +106 -0
- package/dist/types/graphs/phases/memoryFlushPhase.d.ts +100 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/memory/__tests__/mockBackend.d.ts +40 -0
- package/dist/types/memory/citations.d.ts +39 -0
- package/dist/types/memory/compositeBackend.d.ts +30 -0
- package/dist/types/memory/constants.d.ts +121 -0
- package/dist/types/memory/embeddings.d.ts +15 -0
- package/dist/types/memory/factory.d.ts +23 -0
- package/dist/types/memory/index.d.ts +21 -0
- package/dist/types/memory/migrate.d.ts +14 -0
- package/dist/types/memory/mmr.d.ts +50 -0
- package/dist/types/memory/paths.d.ts +107 -0
- package/dist/types/memory/pgvectorStore.d.ts +56 -0
- package/dist/types/memory/recallTracking.d.ts +30 -0
- package/dist/types/memory/temporalDecay.d.ts +53 -0
- package/dist/types/memory/types.d.ts +182 -0
- package/dist/types/prompts/memoryFlushPrompt.d.ts +54 -0
- package/dist/types/run.d.ts +1 -0
- package/dist/types/tools/AskUser.d.ts +1 -1
- package/dist/types/tools/BrowserTools.d.ts +2 -2
- package/dist/types/tools/approval/constants.d.ts +2 -2
- package/dist/types/tools/memory/index.d.ts +39 -0
- package/dist/types/tools/memory/memoryAppendTool.d.ts +27 -0
- package/dist/types/tools/memory/memoryGetTool.d.ts +22 -0
- package/dist/types/tools/memory/memorySearchTool.d.ts +22 -0
- package/dist/types/tools/memory/shared.d.ts +106 -0
- package/dist/types/types/graph.d.ts +16 -3
- package/dist/types/utils/childAgentContext.d.ts +99 -0
- package/dist/types/utils/events.d.ts +21 -0
- package/dist/types/utils/finishReasons.d.ts +32 -0
- package/dist/types/utils/logging.d.ts +2 -0
- package/dist/types/utils/toolCallNormalization.d.ts +44 -0
- package/package.json +6 -4
- package/src/agents/AgentContext.ts +26 -3
- package/src/common/__tests__/enum.test.ts +4 -2
- package/src/common/__tests__/spawnPath.test.ts +110 -0
- package/src/common/index.ts +1 -0
- package/src/common/spawnPath.ts +101 -0
- package/src/graphs/Graph.ts +94 -43
- package/src/graphs/HandoffRegistry.ts +199 -0
- package/src/graphs/MultiAgentGraph.ts +694 -226
- package/src/graphs/__tests__/HandoffRegistry.test.ts +410 -0
- package/src/graphs/__tests__/multi-agent-delegate.test.ts +61 -16
- package/src/graphs/__tests__/multi-agent-edges.test.ts +4 -2
- package/src/graphs/__tests__/multi-agent-nested-subgraph.test.ts +221 -0
- package/src/graphs/__tests__/structured-output.integration.test.ts +212 -118
- package/src/graphs/contextManagement.e2e.test.ts +1 -1
- package/src/graphs/index.ts +1 -0
- package/src/graphs/phases/__tests__/flushLoop.test.ts +264 -0
- package/src/graphs/phases/__tests__/memoryFlushPhase.test.ts +37 -0
- package/src/graphs/phases/__tests__/runMemoryFlush.test.ts +150 -0
- package/src/graphs/phases/flushLoop.ts +303 -0
- package/src/graphs/phases/memoryFlushPhase.ts +209 -0
- package/src/index.ts +30 -1
- package/src/llm/bedrock/index.ts +4 -5
- package/src/memory/__tests__/citations.test.ts +61 -0
- package/src/memory/__tests__/compositeBackend.test.ts +79 -0
- package/src/memory/__tests__/isolation.test.ts +206 -0
- package/src/memory/__tests__/mmr.test.ts +148 -0
- package/src/memory/__tests__/mockBackend.ts +161 -0
- package/src/memory/__tests__/paths.test.ts +168 -0
- package/src/memory/__tests__/recallTracking.test.ts +96 -0
- package/src/memory/__tests__/temporalDecay.test.ts +151 -0
- package/src/memory/citations.ts +80 -0
- package/src/memory/compositeBackend.ts +99 -0
- package/src/memory/constants.ts +229 -0
- package/src/memory/embeddings.ts +188 -0
- package/src/memory/factory.ts +111 -0
- package/src/memory/index.ts +46 -0
- package/src/memory/migrate.ts +116 -0
- package/src/memory/mmr.ts +161 -0
- package/src/memory/paths.ts +258 -0
- package/src/memory/pgvectorStore.ts +324 -0
- package/src/memory/recallTracking.ts +127 -0
- package/src/memory/schema.sql +51 -0
- package/src/memory/temporalDecay.ts +134 -0
- package/src/memory/types.ts +185 -0
- package/src/nodes/ApprovalGateNode.ts +4 -10
- package/src/nodes/__tests__/ApprovalGateNode.test.ts +11 -20
- package/src/prompts/memoryFlushPrompt.ts +78 -0
- package/src/run.ts +17 -6
- package/src/scripts/test-bedrock-handoff-autonomous.ts +56 -20
- package/src/specs/agent-handoffs-bedrock.integration.test.ts +8 -5
- package/src/specs/agent-handoffs.test.ts +8 -2
- package/src/stream.ts +4 -6
- package/src/tools/AskUser.ts +7 -2
- package/src/tools/BrowserTools.ts +3 -5
- package/src/tools/ToolNode.ts +150 -13
- package/src/tools/__tests__/ToolApproval.test.ts +22 -9
- package/src/tools/approval/__tests__/constants.test.ts +4 -4
- package/src/tools/approval/constants.ts +2 -2
- package/src/tools/memory/__tests__/memoryTools.test.ts +205 -0
- package/src/tools/memory/index.ts +96 -0
- package/src/tools/memory/memoryAppendTool.ts +101 -0
- package/src/tools/memory/memoryGetTool.ts +53 -0
- package/src/tools/memory/memorySearchTool.ts +80 -0
- package/src/tools/memory/shared.ts +169 -0
- package/src/tools/search/search.test.ts +6 -1
- package/src/types/graph.ts +16 -3
- package/src/utils/__tests__/childAgentContext.test.ts +217 -0
- package/src/utils/__tests__/finishReasons.test.ts +55 -0
- package/src/utils/__tests__/toolCallNormalization.test.ts +181 -0
- package/src/utils/childAgentContext.ts +259 -0
- package/src/utils/events.ts +37 -4
- package/src/utils/finishReasons.ts +40 -0
- package/src/utils/llm.ts +0 -1
- package/src/utils/logging.ts +45 -8
- package/src/utils/toolCallNormalization.ts +271 -0
package/dist/cjs/stream.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.cjs","sources":["../../src/stream.ts"],"sourcesContent":["// src/stream.ts\nimport type { ChatOpenAIReasoningSummary } from '@langchain/openai';\nimport type { AIMessageChunk } from '@langchain/core/messages';\nimport type { ToolCall } from '@langchain/core/messages/tool';\nimport type { AgentContext } from '@/agents/AgentContext';\nimport type { StandardGraph } from '@/graphs';\nimport type * as t from '@/types';\nimport {\n ToolCallTypes,\n ContentTypes,\n GraphEvents,\n StepTypes,\n Providers,\n} from '@/common';\nimport {\n handleServerToolResult,\n handleToolCallChunks,\n handleToolCalls,\n} from '@/tools/handlers';\nimport { getMessageId } from '@/messages';\n\n/**\n * Parses content to extract thinking sections enclosed in <think> tags using string operations\n * @param content The content to parse\n * @returns An object with separated text and thinking content\n */\nfunction parseThinkingContent(content: string): {\n text: string;\n thinking: string;\n} {\n // If no think tags, return the original content as text\n if (!content.includes('<think>')) {\n return { text: content, thinking: '' };\n }\n\n let textResult = '';\n const thinkingResult: string[] = [];\n let position = 0;\n\n while (position < content.length) {\n const thinkStart = content.indexOf('<think>', position);\n\n if (thinkStart === -1) {\n // No more think tags, add the rest and break\n textResult += content.slice(position);\n break;\n }\n\n // Add text before the think tag\n textResult += content.slice(position, thinkStart);\n\n const thinkEnd = content.indexOf('</think>', thinkStart);\n if (thinkEnd === -1) {\n // Malformed input, no closing tag\n textResult += content.slice(thinkStart);\n break;\n }\n\n // Add the thinking content\n const thinkContent = content.slice(thinkStart + 7, thinkEnd);\n thinkingResult.push(thinkContent);\n\n // Move position to after the think tag\n position = thinkEnd + 8; // 8 is the length of '</think>'\n }\n\n return {\n text: textResult.trim(),\n thinking: thinkingResult.join('\\n').trim(),\n };\n}\n\nfunction getNonEmptyValue(possibleValues: string[]): string | undefined {\n for (const value of possibleValues) {\n if (value && value.trim() !== '') {\n return value;\n }\n }\n return undefined;\n}\n\nexport function getChunkContent({\n chunk,\n provider,\n reasoningKey,\n}: {\n chunk?: Partial<AIMessageChunk>;\n provider?: Providers;\n reasoningKey: 'reasoning_content' | 'reasoning';\n}): string | t.MessageContentComplex[] | undefined {\n if (\n (provider === Providers.OPENAI || provider === Providers.AZURE) &&\n (\n chunk?.additional_kwargs?.reasoning as\n | Partial<ChatOpenAIReasoningSummary>\n | undefined\n )?.summary?.[0]?.text != null &&\n ((\n chunk?.additional_kwargs?.reasoning as\n | Partial<ChatOpenAIReasoningSummary>\n | undefined\n )?.summary?.[0]?.text?.length ?? 0) > 0\n ) {\n return (\n chunk?.additional_kwargs?.reasoning as\n | Partial<ChatOpenAIReasoningSummary>\n | undefined\n )?.summary?.[0]?.text;\n }\n /**\n * For OpenRouter, reasoning is stored in additional_kwargs.reasoning (not reasoning_content).\n * NOTE: We intentionally do NOT extract text from reasoning_details here.\n * The reasoning_details array contains the FULL accumulated reasoning text (set only on final chunk),\n * but individual reasoning tokens are already streamed via additional_kwargs.reasoning.\n * Extracting from reasoning_details would cause duplication.\n * The reasoning_details is only used for:\n * 1. Detecting reasoning mode in handleReasoning()\n * 2. Final message storage (for thought signatures)\n */\n if (provider === Providers.OPENROUTER) {\n // Content presence signals end of reasoning phase - prefer content over reasoning\n // This handles transitional chunks that may have both reasoning and content\n if (typeof chunk?.content === 'string' && chunk.content !== '') {\n return chunk.content;\n }\n const reasoning = chunk?.additional_kwargs?.reasoning as string | undefined;\n if (reasoning != null && reasoning !== '') {\n return reasoning;\n }\n return chunk?.content;\n }\n return (\n ((chunk?.additional_kwargs?.[reasoningKey] as string | undefined) ?? '') ||\n chunk?.content\n );\n}\n\nexport class ChatModelStreamHandler implements t.EventHandler {\n async handle(\n event: string,\n data: t.StreamEventData,\n metadata?: Record<string, unknown>,\n graph?: StandardGraph\n ): Promise<void> {\n if (!graph) {\n throw new Error('Graph not found');\n }\n if (!graph.config) {\n throw new Error('Config not found in graph');\n }\n if (!data.chunk) {\n console.warn(`No chunk found in ${event} event`);\n return;\n }\n\n const agentContext = graph.getAgentContext(metadata);\n\n const chunk = data.chunk as Partial<AIMessageChunk>;\n const content = getChunkContent({\n chunk,\n reasoningKey: agentContext.reasoningKey,\n provider: agentContext.provider,\n });\n const skipHandling = await handleServerToolResult({\n graph,\n content,\n metadata,\n agentContext,\n });\n if (skipHandling) {\n return;\n }\n this.handleReasoning(chunk, agentContext);\n let hasToolCalls = false;\n\n if (\n chunk.tool_calls &&\n chunk.tool_calls.length > 0 &&\n chunk.tool_calls.every(\n (tc) =>\n tc.id != null &&\n tc.id !== '' &&\n (tc as Partial<ToolCall>).name != null &&\n tc.name !== ''\n )\n ) {\n hasToolCalls = true;\n await handleToolCalls(chunk.tool_calls, metadata, graph);\n }\n\n const hasToolCallChunks =\n (chunk.tool_call_chunks && chunk.tool_call_chunks.length > 0) ?? false;\n const isEmptyContent =\n typeof content === 'undefined' ||\n !content.length ||\n (typeof content === 'string' && !content);\n\n /** Set a preliminary message ID if found in empty chunk */\n const isEmptyChunk = isEmptyContent && !hasToolCallChunks;\n if (\n isEmptyChunk &&\n (chunk.id ?? '') !== '' &&\n !graph.prelimMessageIdsByStepKey.has(chunk.id ?? '')\n ) {\n const stepKey = graph.getStepKey(metadata);\n graph.prelimMessageIdsByStepKey.set(stepKey, chunk.id ?? '');\n } else if (isEmptyChunk) {\n return;\n }\n\n const stepKey = graph.getStepKey(metadata);\n\n if (\n hasToolCallChunks &&\n chunk.tool_call_chunks &&\n chunk.tool_call_chunks.length &&\n typeof chunk.tool_call_chunks[0]?.index === 'number'\n ) {\n await handleToolCallChunks({\n graph,\n stepKey,\n toolCallChunks: chunk.tool_call_chunks,\n metadata,\n });\n }\n\n if (isEmptyContent) {\n return;\n }\n\n const message_id = getMessageId(stepKey, graph) ?? '';\n if (message_id) {\n await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n },\n metadata\n );\n }\n\n const stepId = graph.getStepIdByKey(stepKey);\n const runStep = graph.getRunStep(stepId);\n if (!runStep) {\n console.warn(`\\n\n==============================================================\n\n\nRun step for ${stepId} does not exist, cannot dispatch delta event.\n\nevent: ${event}\nstepId: ${stepId}\nstepKey: ${stepKey}\nmessage_id: ${message_id}\nhasToolCalls: ${hasToolCalls}\nhasToolCallChunks: ${hasToolCallChunks}\n\n==============================================================\n\\n`);\n return;\n }\n\n /* Note: tool call chunks may have non-empty content that matches the current tool chunk generation */\n if (typeof content === 'string' && runStep.type === StepTypes.TOOL_CALLS) {\n return;\n } else if (\n hasToolCallChunks &&\n (chunk.tool_call_chunks?.some((tc) => tc.args === content) ?? false)\n ) {\n return;\n } else if (typeof content === 'string') {\n if (agentContext.currentTokenType === ContentTypes.TEXT) {\n await graph.dispatchMessageDelta(stepId, {\n content: [\n {\n type: ContentTypes.TEXT,\n text: content,\n },\n ],\n });\n } else if (agentContext.currentTokenType === 'think_and_text') {\n const { text, thinking } = parseThinkingContent(content);\n if (thinking) {\n await graph.dispatchReasoningDelta(stepId, {\n content: [\n {\n type: ContentTypes.THINK,\n think: thinking,\n },\n ],\n });\n }\n if (text) {\n agentContext.currentTokenType = ContentTypes.TEXT;\n agentContext.tokenTypeSwitch = 'content';\n const newStepKey = graph.getStepKey(metadata);\n const message_id = getMessageId(newStepKey, graph) ?? '';\n await graph.dispatchRunStep(\n newStepKey,\n {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n },\n metadata\n );\n\n const newStepId = graph.getStepIdByKey(newStepKey);\n await graph.dispatchMessageDelta(newStepId, {\n content: [\n {\n type: ContentTypes.TEXT,\n text: text,\n },\n ],\n });\n }\n } else {\n await graph.dispatchReasoningDelta(stepId, {\n content: [\n {\n type: ContentTypes.THINK,\n think: content,\n },\n ],\n });\n }\n } else if (\n content.every((c) => c.type?.startsWith(ContentTypes.TEXT) ?? false)\n ) {\n await graph.dispatchMessageDelta(stepId, {\n content,\n });\n } else if (\n content.every(\n (c) =>\n (c.type?.startsWith(ContentTypes.THINKING) ?? false) ||\n (c.type?.startsWith(ContentTypes.REASONING) ?? false) ||\n (c.type?.startsWith(ContentTypes.REASONING_CONTENT) ?? false) ||\n c.type === 'redacted_thinking'\n )\n ) {\n await graph.dispatchReasoningDelta(stepId, {\n content: content.map((c) => ({\n type: ContentTypes.THINK,\n think:\n (c as t.ThinkingContentText).thinking ??\n (c as Partial<t.GoogleReasoningContentText>).reasoning ??\n (c as Partial<t.BedrockReasoningContentText>).reasoningText?.text ??\n '',\n })),\n });\n }\n }\n handleReasoning(\n chunk: Partial<AIMessageChunk>,\n agentContext: AgentContext\n ): void {\n let reasoning_content = chunk.additional_kwargs?.[\n agentContext.reasoningKey\n ] as string | Partial<ChatOpenAIReasoningSummary> | undefined;\n if (\n Array.isArray(chunk.content) &&\n (chunk.content[0]?.type === ContentTypes.THINKING ||\n chunk.content[0]?.type === ContentTypes.REASONING ||\n chunk.content[0]?.type === ContentTypes.REASONING_CONTENT ||\n chunk.content[0]?.type === 'redacted_thinking')\n ) {\n reasoning_content = 'valid';\n } else if (\n (agentContext.provider === Providers.OPENAI ||\n agentContext.provider === Providers.AZURE) &&\n reasoning_content != null &&\n typeof reasoning_content !== 'string' &&\n reasoning_content.summary?.[0]?.text != null &&\n reasoning_content.summary[0].text\n ) {\n reasoning_content = 'valid';\n } else if (\n agentContext.provider === Providers.OPENROUTER &&\n // Only set reasoning as valid if content is NOT present (content signals end of reasoning)\n (chunk.content == null || chunk.content === '') &&\n // Check for reasoning_details (final chunk) OR reasoning string (intermediate chunks)\n ((chunk.additional_kwargs?.reasoning_details != null &&\n Array.isArray(chunk.additional_kwargs.reasoning_details) &&\n chunk.additional_kwargs.reasoning_details.length > 0) ||\n (typeof chunk.additional_kwargs?.reasoning === 'string' &&\n chunk.additional_kwargs.reasoning !== ''))\n ) {\n reasoning_content = 'valid';\n }\n if (\n reasoning_content != null &&\n reasoning_content !== '' &&\n (chunk.content == null ||\n chunk.content === '' ||\n reasoning_content === 'valid')\n ) {\n agentContext.currentTokenType = ContentTypes.THINK;\n agentContext.tokenTypeSwitch = 'reasoning';\n return;\n } else if (\n agentContext.tokenTypeSwitch === 'reasoning' &&\n agentContext.currentTokenType !== ContentTypes.TEXT &&\n ((chunk.content != null && chunk.content !== '') ||\n (chunk.tool_calls?.length ?? 0) > 0 ||\n (chunk.tool_call_chunks?.length ?? 0) > 0)\n ) {\n agentContext.currentTokenType = ContentTypes.TEXT;\n agentContext.tokenTypeSwitch = 'content';\n agentContext.reasoningTransitionCount++;\n } else if (\n chunk.content != null &&\n typeof chunk.content === 'string' &&\n chunk.content.includes('<think>') &&\n chunk.content.includes('</think>')\n ) {\n agentContext.currentTokenType = 'think_and_text';\n agentContext.tokenTypeSwitch = 'content';\n } else if (\n chunk.content != null &&\n typeof chunk.content === 'string' &&\n chunk.content.includes('<think>')\n ) {\n agentContext.currentTokenType = ContentTypes.THINK;\n agentContext.tokenTypeSwitch = 'content';\n } else if (\n agentContext.lastToken != null &&\n agentContext.lastToken.includes('</think>')\n ) {\n agentContext.currentTokenType = ContentTypes.TEXT;\n agentContext.tokenTypeSwitch = 'content';\n }\n if (typeof chunk.content !== 'string') {\n return;\n }\n agentContext.lastToken = chunk.content;\n }\n}\n\nexport function createContentAggregator(): t.ContentAggregatorResult {\n const contentParts: Array<t.MessageContentComplex | undefined> = [];\n const stepMap = new Map<string, t.RunStep>();\n const toolCallIdMap = new Map<string, string>();\n // Track step IDs for structured output tool calls that should be skipped\n const skippedStructuredOutputSteps = new Set<string>();\n // Track agentId and groupId for each content index (applied to content parts)\n const contentMetaMap = new Map<\n number,\n { agentId?: string; groupId?: number }\n >();\n\n const updateContent = (\n index: number,\n contentPart?: t.MessageContentComplex,\n finalUpdate = false\n ): void => {\n if (!contentPart) {\n console.warn(\"No content part found in 'updateContent'\");\n return;\n }\n const partType = contentPart.type ?? '';\n if (!partType) {\n console.warn('No content type found in content part');\n return;\n }\n\n if (!contentParts[index] && partType !== ContentTypes.TOOL_CALL) {\n contentParts[index] = { type: partType };\n }\n\n if (!partType.startsWith(contentParts[index]?.type ?? '')) {\n console.warn('Content type mismatch');\n return;\n }\n\n if (\n partType.startsWith(ContentTypes.TEXT) &&\n ContentTypes.TEXT in contentPart &&\n typeof contentPart.text === 'string'\n ) {\n // TODO: update this!!\n const currentContent = contentParts[index] as t.MessageDeltaUpdate;\n const update: t.MessageDeltaUpdate = {\n type: ContentTypes.TEXT,\n text: (currentContent.text || '') + contentPart.text,\n };\n\n if (contentPart.tool_call_ids) {\n update.tool_call_ids = contentPart.tool_call_ids;\n }\n contentParts[index] = update;\n } else if (\n partType.startsWith(ContentTypes.THINK) &&\n ContentTypes.THINK in contentPart &&\n typeof contentPart.think === 'string'\n ) {\n const currentContent = contentParts[index] as t.ReasoningDeltaUpdate;\n const update: t.ReasoningDeltaUpdate = {\n type: ContentTypes.THINK,\n think: (currentContent.think || '') + contentPart.think,\n };\n contentParts[index] = update;\n } else if (\n partType.startsWith(ContentTypes.AGENT_UPDATE) &&\n ContentTypes.AGENT_UPDATE in contentPart &&\n contentPart.agent_update != null\n ) {\n const update: t.AgentUpdate = {\n type: ContentTypes.AGENT_UPDATE,\n agent_update: contentPart.agent_update,\n };\n\n contentParts[index] = update;\n } else if (\n partType === ContentTypes.IMAGE_URL &&\n 'image_url' in contentPart\n ) {\n const currentContent = contentParts[index] as {\n type: 'image_url';\n image_url: string;\n };\n contentParts[index] = {\n ...currentContent,\n };\n } else if (\n partType === ContentTypes.TOOL_CALL &&\n 'tool_call' in contentPart\n ) {\n const incomingName = contentPart.tool_call.name;\n const incomingId = contentPart.tool_call.id;\n const toolCallArgs = (contentPart.tool_call as t.ToolCallPart).args;\n\n // When we receive a tool call with a name, it's the complete tool call\n // Consolidate with any previously accumulated args from chunks\n const hasValidName = incomingName != null && incomingName !== '';\n\n const existingContent = contentParts[index] as\n | (Omit<t.ToolCallContent, 'tool_call'> & {\n tool_call?: t.ToolCallPart;\n })\n | undefined;\n\n // Allow processing if:\n // 1. Incoming has a valid name (initial tool call setup), OR\n // 2. It's a final update (ON_RUN_STEP_COMPLETED), OR\n // 3. Existing content already has a name (delta streaming — grows args progressively)\n const existingHasName =\n existingContent?.tool_call?.name != null &&\n existingContent.tool_call.name !== '';\n if (!hasValidName && !finalUpdate && !existingHasName) {\n return;\n }\n\n /** Concatenate string args for progressive streaming; replace when object (complete).\n * When incoming delta has a valid name, treat as a complete replacement —\n * this handles the Bedrock case where handleToolCalls sends complete args\n * as a delta to an existing step that already has partial args from chunks. */\n const existingArgs = existingContent?.tool_call?.args;\n const existingIsEmptyObj =\n typeof existingArgs === 'object' &&\n Object.keys(existingArgs as Record<string, unknown>).length === 0;\n const existingIsString =\n typeof existingArgs === 'string' || existingIsEmptyObj;\n let args =\n finalUpdate ||\n hasValidName ||\n (!existingIsString && typeof existingArgs === 'object') ||\n typeof toolCallArgs === 'object'\n ? contentPart.tool_call.args\n : ((existingIsEmptyObj ? '' : existingArgs) ?? '') +\n (toolCallArgs ?? '');\n if (\n finalUpdate &&\n args == null &&\n existingContent?.tool_call?.args != null\n ) {\n args = existingContent.tool_call.args;\n }\n\n const id =\n getNonEmptyValue([incomingId, existingContent?.tool_call?.id]) ?? '';\n const name =\n getNonEmptyValue([incomingName, existingContent?.tool_call?.name]) ??\n '';\n\n const newToolCall: ToolCall & t.PartMetadata = {\n id,\n name,\n args,\n type: ToolCallTypes.TOOL_CALL,\n };\n\n if (finalUpdate) {\n newToolCall.progress = 1;\n newToolCall.output = contentPart.tool_call.output;\n }\n\n contentParts[index] = {\n type: ContentTypes.TOOL_CALL,\n tool_call: newToolCall,\n };\n }\n\n // Apply agentId (for MultiAgentGraph) and groupId (for parallel execution) to content parts\n // - agentId present → MultiAgentGraph (show agent labels)\n // - groupId present → parallel execution (render columns)\n const meta = contentMetaMap.get(index);\n if (meta?.agentId != null) {\n (contentParts[index] as t.MessageContentComplex).agentId = meta.agentId;\n }\n if (meta?.groupId != null) {\n (contentParts[index] as t.MessageContentComplex).groupId = meta.groupId;\n }\n };\n\n const aggregateContent = ({\n event,\n data,\n }: {\n event: GraphEvents;\n data:\n | t.RunStep\n | t.AgentUpdate\n | t.MessageDeltaEvent\n | t.RunStepDeltaEvent\n | { result: t.ToolEndEvent }\n | {\n structuredResponse: Record<string, unknown>;\n schema: Record<string, unknown>;\n };\n }): void => {\n // Handle structured output - add the JSON response as a text content part\n if (event === GraphEvents.ON_STRUCTURED_OUTPUT) {\n const structuredData = data as {\n structuredResponse: Record<string, unknown>;\n schema: Record<string, unknown>;\n };\n const contentPart: t.MessageContentComplex = {\n type: ContentTypes.TEXT,\n text: JSON.stringify(structuredData.structuredResponse, null, 2),\n };\n // Use the next available index\n const nextIndex = contentParts.length;\n updateContent(nextIndex, contentPart);\n return;\n }\n\n if (event === GraphEvents.ON_RUN_STEP) {\n const runStep = data as t.RunStep;\n stepMap.set(runStep.id, runStep);\n\n // Track agentId (MultiAgentGraph) and groupId (parallel execution) separately\n // - agentId: present for all MultiAgentGraph runs (enables agent labels in UI)\n // - groupId: present only for parallel execution (enables column rendering)\n const hasAgentId = runStep.agentId != null && runStep.agentId !== '';\n const hasGroupId = runStep.groupId != null;\n if (hasAgentId || hasGroupId) {\n const existingMeta = contentMetaMap.get(runStep.index) ?? {};\n if (hasAgentId) {\n existingMeta.agentId = runStep.agentId;\n }\n if (hasGroupId) {\n existingMeta.groupId = runStep.groupId;\n }\n contentMetaMap.set(runStep.index, existingMeta);\n }\n\n // Store tool call IDs if present\n if (\n runStep.stepDetails.type === StepTypes.TOOL_CALLS &&\n runStep.stepDetails.tool_calls\n ) {\n // Check if ALL tool calls are structured output tool calls\n const toolCalls = runStep.stepDetails.tool_calls as ToolCall[];\n const allAreStructuredOutput =\n toolCalls.length > 0 &&\n toolCalls.every((toolCall) => {\n const toolName = toolCall.name.toLowerCase();\n return (\n toolName === 'response' ||\n toolName === 'structuredresponse' ||\n toolName === 'structured_response'\n );\n });\n\n // If all tool calls are structured output, skip this entire step\n if (allAreStructuredOutput) {\n skippedStructuredOutputSteps.add(runStep.id);\n return;\n }\n\n // When tool_calls is empty (Bedrock sends name/args only at model end),\n // initialize a placeholder so contentParts[index] is not null.\n // ON_RUN_STEP_COMPLETED or ON_RUN_STEP_DELTA will fill in the real data.\n if (toolCalls.length === 0) {\n contentParts[runStep.index] = {\n type: ContentTypes.TOOL_CALL,\n tool_call: {\n args: '',\n name: '',\n id: '',\n type: ToolCallTypes.TOOL_CALL,\n },\n };\n }\n\n toolCalls.forEach((toolCall) => {\n // Skip structured output tool calls - these are synthetic tool calls\n // used by withStructuredOutput() to extract JSON, not real tools\n const toolName = toolCall.name.toLowerCase();\n if (\n toolName === 'response' ||\n toolName === 'structuredresponse' ||\n toolName === 'structured_response'\n ) {\n return;\n }\n\n const toolCallId = toolCall.id ?? '';\n if ('id' in toolCall && toolCallId) {\n toolCallIdMap.set(runStep.id, toolCallId);\n }\n const contentPart: t.MessageContentComplex = {\n type: ContentTypes.TOOL_CALL,\n tool_call: {\n args: toolCall.args,\n name: toolCall.name,\n id: toolCallId,\n },\n };\n\n updateContent(runStep.index, contentPart);\n });\n }\n } else if (event === GraphEvents.ON_MESSAGE_DELTA) {\n const messageDelta = data as t.MessageDeltaEvent;\n const runStep = stepMap.get(messageDelta.id);\n if (!runStep) {\n console.warn('No run step or runId found for message delta event');\n return;\n }\n\n if (messageDelta.delta.content) {\n const contentPart = Array.isArray(messageDelta.delta.content)\n ? messageDelta.delta.content[0]\n : messageDelta.delta.content;\n\n updateContent(runStep.index, contentPart);\n }\n } else if (\n event === GraphEvents.ON_AGENT_UPDATE &&\n (data as t.AgentUpdate | undefined)?.agent_update\n ) {\n const contentPart = data as t.AgentUpdate | undefined;\n if (!contentPart) {\n return;\n }\n updateContent(contentPart.agent_update.index, contentPart);\n } else if (event === GraphEvents.ON_REASONING_DELTA) {\n const reasoningDelta = data as t.ReasoningDeltaEvent;\n const runStep = stepMap.get(reasoningDelta.id);\n if (!runStep) {\n console.warn('No run step or runId found for reasoning delta event');\n return;\n }\n\n if (reasoningDelta.delta.content) {\n const contentPart = Array.isArray(reasoningDelta.delta.content)\n ? reasoningDelta.delta.content[0]\n : reasoningDelta.delta.content;\n\n updateContent(runStep.index, contentPart);\n }\n } else if (event === GraphEvents.ON_RUN_STEP_DELTA) {\n const runStepDelta = data as t.RunStepDeltaEvent;\n\n // Skip deltas for structured output tool calls\n if (skippedStructuredOutputSteps.has(runStepDelta.id)) {\n return;\n }\n\n const runStep = stepMap.get(runStepDelta.id);\n if (!runStep) {\n console.warn('No run step or runId found for run step delta event');\n return;\n }\n\n if (\n runStepDelta.delta.type === StepTypes.TOOL_CALLS &&\n runStepDelta.delta.tool_calls\n ) {\n runStepDelta.delta.tool_calls.forEach((toolCallDelta) => {\n /** Use toolCallIdMap first; fall back to the delta's own id\n * (Bedrock sends complete tool call data as a delta via handleToolCalls,\n * which includes the id, but the map was never populated because\n * the initial on_run_step had tool_calls: []) */\n const toolCallId =\n toolCallIdMap.get(runStepDelta.id) ??\n (toolCallDelta.id != null && toolCallDelta.id !== ''\n ? toolCallDelta.id\n : undefined);\n\n // Store the mapping for future lookups (e.g. on_run_step_completed)\n if (\n toolCallId != null &&\n toolCallId !== '' &&\n !toolCallIdMap.has(runStepDelta.id)\n ) {\n toolCallIdMap.set(runStepDelta.id, toolCallId);\n }\n\n const contentPart: t.MessageContentComplex = {\n type: ContentTypes.TOOL_CALL,\n tool_call: {\n args: toolCallDelta.args ?? '',\n name: toolCallDelta.name,\n id: toolCallId,\n },\n };\n\n updateContent(runStep.index, contentPart);\n });\n }\n } else if (event === GraphEvents.ON_RUN_STEP_COMPLETED) {\n const { result } = data as unknown as { result: t.ToolEndEvent };\n\n const { id: stepId } = result;\n\n // Skip completion events for structured output tool calls\n if (skippedStructuredOutputSteps.has(stepId)) {\n return;\n }\n\n const runStep = stepMap.get(stepId);\n if (!runStep) {\n console.warn(\n 'No run step or runId found for completed tool call event'\n );\n return;\n }\n\n const contentPart: t.MessageContentComplex = {\n type: ContentTypes.TOOL_CALL,\n tool_call: result.tool_call,\n };\n\n updateContent(runStep.index, contentPart, true);\n }\n };\n\n return { contentParts, aggregateContent, stepMap };\n}\n"],"names":["Providers","handleServerToolResult","handleToolCalls","handleToolCallChunks","getMessageId","StepTypes","ContentTypes","ToolCallTypes","GraphEvents"],"mappings":";;;;;;;;;;AAqBA;;;;AAIG;AACH,SAAS,oBAAoB,CAAC,OAAe,EAAA;;IAK3C,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE;QAChC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE;IACxC;IAEA,IAAI,UAAU,GAAG,EAAE;IACnB,MAAM,cAAc,GAAa,EAAE;IACnC,IAAI,QAAQ,GAAG,CAAC;AAEhB,IAAA,OAAO,QAAQ,GAAG,OAAO,CAAC,MAAM,EAAE;QAChC,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,QAAQ,CAAC;AAEvD,QAAA,IAAI,UAAU,KAAK,EAAE,EAAE;;AAErB,YAAA,UAAU,IAAI,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC;YACrC;QACF;;QAGA,UAAU,IAAI,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,UAAU,CAAC;QAEjD,MAAM,QAAQ,GAAG,OAAO,CAAC,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AAEhE,YAAA,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,MAAM;;AAG7B,YAAA,IAAI,4BAA4B,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE;gBAC5C;YACF;YAEA,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC;YACnC,IAAI,CAAC,OAAO,EAAE;AACZ,gBAAA,OAAO,CAAC,IAAI,CACV,0DAA0D,CAC3D;gBACD;YACF;AAEA,YAAA,MAAM,WAAW,GAA4B;gBAC3C,IAAI,EAAEF,kBAAY,CAAC,SAAS;gBAC5B,SAAS,EAAE,MAAM,CAAC,SAAS;aAC5B;YAED,aAAa,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,CAAC;QACjD;AACF,IAAA,CAAC;AAED,IAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,OAAO,EAAE;AACpD;;;;;;"}
|
|
1
|
+
{"version":3,"file":"stream.cjs","sources":["../../src/stream.ts"],"sourcesContent":["// src/stream.ts\nimport type { ChatOpenAIReasoningSummary } from '@langchain/openai';\nimport type { AIMessageChunk } from '@langchain/core/messages';\nimport type { ToolCall } from '@langchain/core/messages/tool';\nimport type { AgentContext } from '@/agents/AgentContext';\nimport type { StandardGraph } from '@/graphs';\nimport type * as t from '@/types';\nimport {\n ToolCallTypes,\n ContentTypes,\n GraphEvents,\n StepTypes,\n Providers,\n} from '@/common';\nimport {\n handleServerToolResult,\n handleToolCallChunks,\n handleToolCalls,\n} from '@/tools/handlers';\nimport { getMessageId } from '@/messages';\n\n/**\n * Parses content to extract thinking sections enclosed in <think> tags using string operations\n * @param content The content to parse\n * @returns An object with separated text and thinking content\n */\nfunction parseThinkingContent(content: string): {\n text: string;\n thinking: string;\n} {\n // If no think tags, return the original content as text\n if (!content.includes('<think>')) {\n return { text: content, thinking: '' };\n }\n\n let textResult = '';\n const thinkingResult: string[] = [];\n let position = 0;\n\n while (position < content.length) {\n const thinkStart = content.indexOf('<think>', position);\n\n if (thinkStart === -1) {\n // No more think tags, add the rest and break\n textResult += content.slice(position);\n break;\n }\n\n // Add text before the think tag\n textResult += content.slice(position, thinkStart);\n\n const thinkEnd = content.indexOf('</think>', thinkStart);\n if (thinkEnd === -1) {\n // Malformed input, no closing tag\n textResult += content.slice(thinkStart);\n break;\n }\n\n // Add the thinking content\n const thinkContent = content.slice(thinkStart + 7, thinkEnd);\n thinkingResult.push(thinkContent);\n\n // Move position to after the think tag\n position = thinkEnd + 8; // 8 is the length of '</think>'\n }\n\n return {\n text: textResult.trim(),\n thinking: thinkingResult.join('\\n').trim(),\n };\n}\n\nfunction getNonEmptyValue(possibleValues: string[]): string | undefined {\n for (const value of possibleValues) {\n if (value && value.trim() !== '') {\n return value;\n }\n }\n return undefined;\n}\n\nexport function getChunkContent({\n chunk,\n provider,\n reasoningKey,\n}: {\n chunk?: Partial<AIMessageChunk>;\n provider?: Providers;\n reasoningKey: 'reasoning_content' | 'reasoning';\n}): string | t.MessageContentComplex[] | undefined {\n if (\n (provider === Providers.OPENAI || provider === Providers.AZURE) &&\n (\n chunk?.additional_kwargs?.reasoning as\n | Partial<ChatOpenAIReasoningSummary>\n | undefined\n )?.summary?.[0]?.text != null &&\n ((\n chunk?.additional_kwargs?.reasoning as\n | Partial<ChatOpenAIReasoningSummary>\n | undefined\n )?.summary?.[0]?.text?.length ?? 0) > 0\n ) {\n return (\n chunk?.additional_kwargs?.reasoning as\n | Partial<ChatOpenAIReasoningSummary>\n | undefined\n )?.summary?.[0]?.text;\n }\n /**\n * For OpenRouter, reasoning is stored in additional_kwargs.reasoning (not reasoning_content).\n * NOTE: We intentionally do NOT extract text from reasoning_details here.\n * The reasoning_details array contains the FULL accumulated reasoning text (set only on final chunk),\n * but individual reasoning tokens are already streamed via additional_kwargs.reasoning.\n * Extracting from reasoning_details would cause duplication.\n * The reasoning_details is only used for:\n * 1. Detecting reasoning mode in handleReasoning()\n * 2. Final message storage (for thought signatures)\n */\n if (provider === Providers.OPENROUTER) {\n // Content presence signals end of reasoning phase - prefer content over reasoning\n // This handles transitional chunks that may have both reasoning and content\n if (typeof chunk?.content === 'string' && chunk.content !== '') {\n return chunk.content;\n }\n const reasoning = chunk?.additional_kwargs?.reasoning as string | undefined;\n if (reasoning != null && reasoning !== '') {\n return reasoning;\n }\n return chunk?.content;\n }\n return (\n ((chunk?.additional_kwargs?.[reasoningKey] as string | undefined) ?? '') ||\n chunk?.content\n );\n}\n\nexport class ChatModelStreamHandler implements t.EventHandler {\n async handle(\n event: string,\n data: t.StreamEventData,\n metadata?: Record<string, unknown>,\n graph?: StandardGraph\n ): Promise<void> {\n if (!graph) {\n throw new Error('Graph not found');\n }\n if (!graph.config) {\n throw new Error('Config not found in graph');\n }\n if (!data.chunk) {\n console.warn(`No chunk found in ${event} event`);\n return;\n }\n\n const agentContext = graph.getAgentContext(metadata);\n\n const chunk = data.chunk as Partial<AIMessageChunk>;\n const content = getChunkContent({\n chunk,\n reasoningKey: agentContext.reasoningKey,\n provider: agentContext.provider,\n });\n const skipHandling = await handleServerToolResult({\n graph,\n content,\n metadata,\n agentContext,\n });\n if (skipHandling) {\n return;\n }\n this.handleReasoning(chunk, agentContext);\n let hasToolCalls = false;\n\n if (\n chunk.tool_calls &&\n chunk.tool_calls.length > 0 &&\n chunk.tool_calls.every(\n (tc) =>\n tc.id != null &&\n tc.id !== '' &&\n (tc as Partial<ToolCall>).name != null &&\n tc.name !== ''\n )\n ) {\n hasToolCalls = true;\n await handleToolCalls(chunk.tool_calls, metadata, graph);\n }\n\n const hasToolCallChunks =\n (chunk.tool_call_chunks && chunk.tool_call_chunks.length > 0) ?? false;\n const isEmptyContent =\n typeof content === 'undefined' ||\n !content.length ||\n (typeof content === 'string' && !content);\n\n /** Set a preliminary message ID if found in empty chunk */\n const isEmptyChunk = isEmptyContent && !hasToolCallChunks;\n if (\n isEmptyChunk &&\n (chunk.id ?? '') !== '' &&\n !graph.prelimMessageIdsByStepKey.has(chunk.id ?? '')\n ) {\n const stepKey = graph.getStepKey(metadata);\n graph.prelimMessageIdsByStepKey.set(stepKey, chunk.id ?? '');\n } else if (isEmptyChunk) {\n return;\n }\n\n const stepKey = graph.getStepKey(metadata);\n\n if (\n hasToolCallChunks &&\n chunk.tool_call_chunks &&\n chunk.tool_call_chunks.length &&\n typeof chunk.tool_call_chunks[0]?.index === 'number'\n ) {\n await handleToolCallChunks({\n graph,\n stepKey,\n toolCallChunks: chunk.tool_call_chunks,\n metadata,\n });\n }\n\n if (isEmptyContent) {\n return;\n }\n\n const message_id = getMessageId(stepKey, graph) ?? '';\n if (message_id) {\n await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n },\n metadata\n );\n }\n\n const stepId = graph.getStepIdByKey(stepKey);\n const runStep = graph.getRunStep(stepId);\n if (!runStep) {\n console.warn(`\\n\n==============================================================\n\n\nRun step for ${stepId} does not exist, cannot dispatch delta event.\n\nevent: ${event}\nstepId: ${stepId}\nstepKey: ${stepKey}\nmessage_id: ${message_id}\nhasToolCalls: ${hasToolCalls}\nhasToolCallChunks: ${hasToolCallChunks}\n\n==============================================================\n\\n`);\n return;\n }\n\n /* Note: tool call chunks may have non-empty content that matches the current tool chunk generation */\n if (typeof content === 'string' && runStep.type === StepTypes.TOOL_CALLS) {\n return;\n } else if (\n hasToolCallChunks &&\n (chunk.tool_call_chunks?.some((tc) => tc.args === content) ?? false)\n ) {\n return;\n } else if (typeof content === 'string') {\n if (agentContext.currentTokenType === ContentTypes.TEXT) {\n await graph.dispatchMessageDelta(stepId, {\n content: [\n {\n type: ContentTypes.TEXT,\n text: content,\n },\n ],\n });\n } else if (agentContext.currentTokenType === 'think_and_text') {\n const { text, thinking } = parseThinkingContent(content);\n if (thinking) {\n await graph.dispatchReasoningDelta(stepId, {\n content: [\n {\n type: ContentTypes.THINK,\n think: thinking,\n },\n ],\n });\n }\n if (text) {\n agentContext.currentTokenType = ContentTypes.TEXT;\n agentContext.tokenTypeSwitch = 'content';\n const newStepKey = graph.getStepKey(metadata);\n const message_id = getMessageId(newStepKey, graph) ?? '';\n await graph.dispatchRunStep(\n newStepKey,\n {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n },\n metadata\n );\n\n const newStepId = graph.getStepIdByKey(newStepKey);\n await graph.dispatchMessageDelta(newStepId, {\n content: [\n {\n type: ContentTypes.TEXT,\n text: text,\n },\n ],\n });\n }\n } else {\n await graph.dispatchReasoningDelta(stepId, {\n content: [\n {\n type: ContentTypes.THINK,\n think: content,\n },\n ],\n });\n }\n } else if (\n content.every((c) => c.type?.startsWith(ContentTypes.TEXT) ?? false)\n ) {\n await graph.dispatchMessageDelta(stepId, {\n content,\n });\n } else if (\n content.every(\n (c) =>\n (c.type?.startsWith(ContentTypes.THINKING) ?? false) ||\n (c.type?.startsWith(ContentTypes.REASONING) ?? false) ||\n (c.type?.startsWith(ContentTypes.REASONING_CONTENT) ?? false) ||\n c.type === 'redacted_thinking'\n )\n ) {\n await graph.dispatchReasoningDelta(stepId, {\n content: content.map((c) => ({\n type: ContentTypes.THINK,\n think:\n (c as t.ThinkingContentText).thinking ??\n (c as Partial<t.GoogleReasoningContentText>).reasoning ??\n (c as Partial<t.BedrockReasoningContentText>).reasoningText?.text ??\n '',\n })),\n });\n }\n }\n handleReasoning(\n chunk: Partial<AIMessageChunk>,\n agentContext: AgentContext\n ): void {\n let reasoning_content = chunk.additional_kwargs?.[\n agentContext.reasoningKey\n ] as string | Partial<ChatOpenAIReasoningSummary> | undefined;\n if (\n Array.isArray(chunk.content) &&\n (chunk.content[0]?.type === ContentTypes.THINKING ||\n chunk.content[0]?.type === ContentTypes.REASONING ||\n chunk.content[0]?.type === ContentTypes.REASONING_CONTENT ||\n chunk.content[0]?.type === 'redacted_thinking')\n ) {\n reasoning_content = 'valid';\n } else if (\n (agentContext.provider === Providers.OPENAI ||\n agentContext.provider === Providers.AZURE) &&\n reasoning_content != null &&\n typeof reasoning_content !== 'string' &&\n reasoning_content.summary?.[0]?.text != null &&\n reasoning_content.summary[0].text\n ) {\n reasoning_content = 'valid';\n } else if (\n agentContext.provider === Providers.OPENROUTER &&\n // Only set reasoning as valid if content is NOT present (content signals end of reasoning)\n (chunk.content == null || chunk.content === '') &&\n // Check for reasoning_details (final chunk) OR reasoning string (intermediate chunks)\n ((chunk.additional_kwargs?.reasoning_details != null &&\n Array.isArray(chunk.additional_kwargs.reasoning_details) &&\n chunk.additional_kwargs.reasoning_details.length > 0) ||\n (typeof chunk.additional_kwargs?.reasoning === 'string' &&\n chunk.additional_kwargs.reasoning !== ''))\n ) {\n reasoning_content = 'valid';\n }\n if (\n reasoning_content != null &&\n reasoning_content !== '' &&\n (chunk.content == null ||\n chunk.content === '' ||\n reasoning_content === 'valid')\n ) {\n agentContext.currentTokenType = ContentTypes.THINK;\n agentContext.tokenTypeSwitch = 'reasoning';\n return;\n } else if (\n agentContext.tokenTypeSwitch === 'reasoning' &&\n agentContext.currentTokenType !== ContentTypes.TEXT &&\n ((chunk.content != null && chunk.content !== '') ||\n (chunk.tool_calls?.length ?? 0) > 0 ||\n (chunk.tool_call_chunks?.length ?? 0) > 0)\n ) {\n agentContext.currentTokenType = ContentTypes.TEXT;\n agentContext.tokenTypeSwitch = 'content';\n agentContext.reasoningTransitionCount++;\n } else if (\n chunk.content != null &&\n typeof chunk.content === 'string' &&\n chunk.content.includes('<think>') &&\n chunk.content.includes('</think>')\n ) {\n agentContext.currentTokenType = 'think_and_text';\n agentContext.tokenTypeSwitch = 'content';\n } else if (\n chunk.content != null &&\n typeof chunk.content === 'string' &&\n chunk.content.includes('<think>')\n ) {\n agentContext.currentTokenType = ContentTypes.THINK;\n agentContext.tokenTypeSwitch = 'content';\n } else if (\n agentContext.lastToken != null &&\n agentContext.lastToken.includes('</think>')\n ) {\n agentContext.currentTokenType = ContentTypes.TEXT;\n agentContext.tokenTypeSwitch = 'content';\n }\n if (typeof chunk.content !== 'string') {\n return;\n }\n agentContext.lastToken = chunk.content;\n }\n}\n\nexport function createContentAggregator(): t.ContentAggregatorResult {\n const contentParts: Array<t.MessageContentComplex | undefined> = [];\n const stepMap = new Map<string, t.RunStep>();\n const toolCallIdMap = new Map<string, string>();\n // Track step IDs for structured output tool calls that should be skipped\n const skippedStructuredOutputSteps = new Set<string>();\n // Track agentId and groupId for each content index (applied to content parts)\n const contentMetaMap = new Map<\n number,\n { agentId?: string; groupId?: number }\n >();\n\n const updateContent = (\n index: number,\n contentPart?: t.MessageContentComplex,\n finalUpdate = false\n ): void => {\n if (!contentPart) {\n console.warn(\"No content part found in 'updateContent'\");\n return;\n }\n const partType = contentPart.type ?? '';\n if (!partType) {\n console.warn('No content type found in content part');\n return;\n }\n\n if (!contentParts[index] && partType !== ContentTypes.TOOL_CALL) {\n contentParts[index] = { type: partType };\n }\n\n if (!partType.startsWith(contentParts[index]?.type ?? '')) {\n console.warn('Content type mismatch');\n return;\n }\n\n if (\n partType.startsWith(ContentTypes.TEXT) &&\n ContentTypes.TEXT in contentPart &&\n typeof contentPart.text === 'string'\n ) {\n // TODO: update this!!\n const currentContent = contentParts[index] as t.MessageDeltaUpdate;\n const update: t.MessageDeltaUpdate = {\n type: ContentTypes.TEXT,\n text: (currentContent.text || '') + contentPart.text,\n };\n\n if (contentPart.tool_call_ids) {\n update.tool_call_ids = contentPart.tool_call_ids;\n }\n contentParts[index] = update;\n } else if (\n partType.startsWith(ContentTypes.THINK) &&\n ContentTypes.THINK in contentPart &&\n typeof contentPart.think === 'string'\n ) {\n const currentContent = contentParts[index] as t.ReasoningDeltaUpdate;\n const update: t.ReasoningDeltaUpdate = {\n type: ContentTypes.THINK,\n think: (currentContent.think || '') + contentPart.think,\n };\n contentParts[index] = update;\n } else if (\n partType.startsWith(ContentTypes.AGENT_UPDATE) &&\n ContentTypes.AGENT_UPDATE in contentPart &&\n contentPart.agent_update != null\n ) {\n const update: t.AgentUpdate = {\n type: ContentTypes.AGENT_UPDATE,\n agent_update: contentPart.agent_update,\n };\n\n contentParts[index] = update;\n } else if (\n partType === ContentTypes.IMAGE_URL &&\n 'image_url' in contentPart\n ) {\n const currentContent = contentParts[index] as {\n type: 'image_url';\n image_url: string;\n };\n contentParts[index] = {\n ...currentContent,\n };\n } else if (\n partType === ContentTypes.TOOL_CALL &&\n 'tool_call' in contentPart\n ) {\n const incomingName = contentPart.tool_call.name;\n const incomingId = contentPart.tool_call.id;\n const toolCallArgs = (contentPart.tool_call as t.ToolCallPart).args;\n\n // When we receive a tool call with a name, it's the complete tool call\n // Consolidate with any previously accumulated args from chunks\n const hasValidName = incomingName != null && incomingName !== '';\n\n const existingContent = contentParts[index] as\n | (Omit<t.ToolCallContent, 'tool_call'> & {\n tool_call?: t.ToolCallPart;\n })\n | undefined;\n\n // Allow processing if:\n // 1. Incoming has a valid name (initial tool call setup), OR\n // 2. It's a final update (ON_RUN_STEP_COMPLETED), OR\n // 3. Existing content already has a name (delta streaming — grows args progressively)\n const existingHasName =\n existingContent?.tool_call?.name != null &&\n existingContent.tool_call.name !== '';\n if (!hasValidName && !finalUpdate && !existingHasName) {\n return;\n }\n\n /** Concatenate string args for progressive streaming; replace when object (complete).\n * When incoming delta has a valid name, treat as a complete replacement —\n * this handles the Bedrock case where handleToolCalls sends complete args\n * as a delta to an existing step that already has partial args from chunks. */\n const existingArgs = existingContent?.tool_call?.args;\n const existingIsEmptyObj =\n typeof existingArgs === 'object' &&\n Object.keys(existingArgs as Record<string, unknown>).length === 0;\n const existingIsString =\n typeof existingArgs === 'string' || existingIsEmptyObj;\n let args =\n finalUpdate ||\n hasValidName ||\n (!existingIsString && typeof existingArgs === 'object') ||\n typeof toolCallArgs === 'object'\n ? contentPart.tool_call.args\n : ((existingIsEmptyObj ? '' : existingArgs) ?? '') +\n (toolCallArgs ?? '');\n if (\n finalUpdate &&\n args == null &&\n existingContent?.tool_call?.args != null\n ) {\n args = existingContent.tool_call.args;\n }\n\n const id =\n getNonEmptyValue([incomingId, existingContent?.tool_call?.id]) ?? '';\n const name =\n getNonEmptyValue([incomingName, existingContent?.tool_call?.name]) ??\n '';\n\n const newToolCall: ToolCall & t.PartMetadata = {\n id,\n name,\n args,\n type: ToolCallTypes.TOOL_CALL,\n };\n\n if (finalUpdate) {\n newToolCall.progress = 1;\n newToolCall.output = contentPart.tool_call.output;\n }\n\n contentParts[index] = {\n type: ContentTypes.TOOL_CALL,\n tool_call: newToolCall,\n };\n }\n\n // Apply agentId (for MultiAgentGraph) and groupId (for parallel execution) to content parts\n // - agentId present → MultiAgentGraph (show agent labels)\n // - groupId present → parallel execution (render columns)\n const meta = contentMetaMap.get(index);\n if (meta?.agentId != null) {\n (contentParts[index] as t.MessageContentComplex).agentId = meta.agentId;\n }\n if (meta?.groupId != null) {\n (contentParts[index] as t.MessageContentComplex).groupId = meta.groupId;\n }\n };\n\n const aggregateContent = ({\n event,\n data,\n }: {\n event: GraphEvents;\n data:\n | t.RunStep\n | t.AgentUpdate\n | t.MessageDeltaEvent\n | t.RunStepDeltaEvent\n | { result: t.ToolEndEvent }\n | {\n structuredResponse: Record<string, unknown>;\n schema: Record<string, unknown>;\n };\n }): void => {\n // Handle structured output - add the JSON response as a text content part\n if (event === GraphEvents.ON_STRUCTURED_OUTPUT) {\n const structuredData = data as {\n structuredResponse: Record<string, unknown>;\n schema: Record<string, unknown>;\n };\n const contentPart: t.MessageContentComplex = {\n type: ContentTypes.TEXT,\n text: JSON.stringify(structuredData.structuredResponse, null, 2),\n };\n // Use the next available index\n const nextIndex = contentParts.length;\n updateContent(nextIndex, contentPart);\n return;\n }\n\n if (event === GraphEvents.ON_RUN_STEP) {\n const runStep = data as t.RunStep;\n stepMap.set(runStep.id, runStep);\n\n // Track agentId (MultiAgentGraph) and groupId (parallel execution) separately\n // - agentId: present for all MultiAgentGraph runs (enables agent labels in UI)\n // - groupId: present only for parallel execution (enables column rendering)\n const hasAgentId = runStep.agentId != null && runStep.agentId !== '';\n const hasGroupId = runStep.groupId != null;\n if (hasAgentId || hasGroupId) {\n const existingMeta = contentMetaMap.get(runStep.index) ?? {};\n if (hasAgentId) {\n existingMeta.agentId = runStep.agentId;\n }\n if (hasGroupId) {\n existingMeta.groupId = runStep.groupId;\n }\n contentMetaMap.set(runStep.index, existingMeta);\n }\n\n // Store tool call IDs if present\n if (\n runStep.stepDetails.type === StepTypes.TOOL_CALLS &&\n runStep.stepDetails.tool_calls\n ) {\n // Check if ALL tool calls are structured output tool calls\n const toolCalls = runStep.stepDetails.tool_calls as ToolCall[];\n const allAreStructuredOutput =\n toolCalls.length > 0 &&\n toolCalls.every((toolCall) => {\n const toolName = toolCall.name.toLowerCase();\n return (\n toolName === 'response' ||\n toolName === 'structuredresponse' ||\n toolName === 'structured_response'\n );\n });\n\n // If all tool calls are structured output, skip this entire step\n if (allAreStructuredOutput) {\n skippedStructuredOutputSteps.add(runStep.id);\n return;\n }\n\n // When tool_calls is empty (Bedrock sends name/args only at model end),\n // initialize a placeholder so contentParts[index] is not null.\n // ON_RUN_STEP_COMPLETED or ON_RUN_STEP_DELTA will fill in the real data.\n if (toolCalls.length === 0) {\n contentParts[runStep.index] = {\n type: ContentTypes.TOOL_CALL,\n tool_call: {\n args: '',\n name: '',\n id: '',\n type: ToolCallTypes.TOOL_CALL,\n },\n };\n }\n\n toolCalls.forEach((toolCall) => {\n // Skip structured output tool calls - these are synthetic tool calls\n // used by withStructuredOutput() to extract JSON, not real tools\n const toolName = toolCall.name.toLowerCase();\n if (\n toolName === 'response' ||\n toolName === 'structuredresponse' ||\n toolName === 'structured_response'\n ) {\n return;\n }\n\n const toolCallId = toolCall.id ?? '';\n if ('id' in toolCall && toolCallId) {\n toolCallIdMap.set(runStep.id, toolCallId);\n }\n const contentPart: t.MessageContentComplex = {\n type: ContentTypes.TOOL_CALL,\n tool_call: {\n args: toolCall.args,\n name: toolCall.name,\n id: toolCallId,\n },\n };\n\n updateContent(runStep.index, contentPart);\n });\n }\n } else if (event === GraphEvents.ON_MESSAGE_DELTA) {\n const messageDelta = data as t.MessageDeltaEvent;\n const runStep = stepMap.get(messageDelta.id);\n if (!runStep) {\n // Expected in handoff subgraphs — reasoning/message deltas can arrive before ON_RUN_STEP\n return;\n }\n\n if (messageDelta.delta.content) {\n const contentPart = Array.isArray(messageDelta.delta.content)\n ? messageDelta.delta.content[0]\n : messageDelta.delta.content;\n\n updateContent(runStep.index, contentPart);\n }\n } else if (\n event === GraphEvents.ON_AGENT_UPDATE &&\n (data as t.AgentUpdate | undefined)?.agent_update\n ) {\n const contentPart = data as t.AgentUpdate | undefined;\n if (!contentPart) {\n return;\n }\n updateContent(contentPart.agent_update.index, contentPart);\n } else if (event === GraphEvents.ON_REASONING_DELTA) {\n const reasoningDelta = data as t.ReasoningDeltaEvent;\n const runStep = stepMap.get(reasoningDelta.id);\n if (!runStep) {\n // Expected in handoff subgraphs — reasoning deltas arrive before ON_RUN_STEP\n return;\n }\n\n if (reasoningDelta.delta.content) {\n const contentPart = Array.isArray(reasoningDelta.delta.content)\n ? reasoningDelta.delta.content[0]\n : reasoningDelta.delta.content;\n\n updateContent(runStep.index, contentPart);\n }\n } else if (event === GraphEvents.ON_RUN_STEP_DELTA) {\n const runStepDelta = data as t.RunStepDeltaEvent;\n\n // Skip deltas for structured output tool calls\n if (skippedStructuredOutputSteps.has(runStepDelta.id)) {\n return;\n }\n\n const runStep = stepMap.get(runStepDelta.id);\n if (!runStep) {\n // Expected in handoff subgraphs — step deltas can arrive before ON_RUN_STEP\n return;\n }\n\n if (\n runStepDelta.delta.type === StepTypes.TOOL_CALLS &&\n runStepDelta.delta.tool_calls\n ) {\n runStepDelta.delta.tool_calls.forEach((toolCallDelta) => {\n /** Use toolCallIdMap first; fall back to the delta's own id\n * (Bedrock sends complete tool call data as a delta via handleToolCalls,\n * which includes the id, but the map was never populated because\n * the initial on_run_step had tool_calls: []) */\n const toolCallId =\n toolCallIdMap.get(runStepDelta.id) ??\n (toolCallDelta.id != null && toolCallDelta.id !== ''\n ? toolCallDelta.id\n : undefined);\n\n // Store the mapping for future lookups (e.g. on_run_step_completed)\n if (\n toolCallId != null &&\n toolCallId !== '' &&\n !toolCallIdMap.has(runStepDelta.id)\n ) {\n toolCallIdMap.set(runStepDelta.id, toolCallId);\n }\n\n const contentPart: t.MessageContentComplex = {\n type: ContentTypes.TOOL_CALL,\n tool_call: {\n args: toolCallDelta.args ?? '',\n name: toolCallDelta.name,\n id: toolCallId,\n },\n };\n\n updateContent(runStep.index, contentPart);\n });\n }\n } else if (event === GraphEvents.ON_RUN_STEP_COMPLETED) {\n const { result } = data as unknown as { result: t.ToolEndEvent };\n\n const { id: stepId } = result;\n\n // Skip completion events for structured output tool calls\n if (skippedStructuredOutputSteps.has(stepId)) {\n return;\n }\n\n const runStep = stepMap.get(stepId);\n if (!runStep) {\n // Expected in handoff subgraphs — completion can arrive for untracked steps\n return;\n }\n\n const contentPart: t.MessageContentComplex = {\n type: ContentTypes.TOOL_CALL,\n tool_call: result.tool_call,\n };\n\n updateContent(runStep.index, contentPart, true);\n }\n };\n\n return { contentParts, aggregateContent, stepMap };\n}\n"],"names":["Providers","handleServerToolResult","handleToolCalls","handleToolCallChunks","getMessageId","StepTypes","ContentTypes","ToolCallTypes","GraphEvents"],"mappings":";;;;;;;;;;AAqBA;;;;AAIG;AACH,SAAS,oBAAoB,CAAC,OAAe,EAAA;;IAK3C,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE;QAChC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE;IACxC;IAEA,IAAI,UAAU,GAAG,EAAE;IACnB,MAAM,cAAc,GAAa,EAAE;IACnC,IAAI,QAAQ,GAAG,CAAC;AAEhB,IAAA,OAAO,QAAQ,GAAG,OAAO,CAAC,MAAM,EAAE;QAChC,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,QAAQ,CAAC;AAEvD,QAAA,IAAI,UAAU,KAAK,EAAE,EAAE;;AAErB,YAAA,UAAU,IAAI,OAAO,CAAC,KA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YAAA,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,MAAM;;AAG7B,YAAA,IAAI,4BAA4B,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE;gBAC5C;YACF;YAEA,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC;YACnC,IAAI,CAAC,OAAO,EAAE;;gBAEZ;YACF;AAEA,YAAA,MAAM,WAAW,GAA4B;gBAC3C,IAAI,EAAEF,kBAAY,CAAC,SAAS;gBAC5B,SAAS,EAAE,MAAM,CAAC,SAAS;aAC5B;YAED,aAAa,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,CAAC;QACjD;AACF,IAAA,CAAC;AAED,IAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,OAAO,EAAE;AACpD;;;;;;"}
|
|
@@ -8,7 +8,12 @@ require('./approval/constants.cjs');
|
|
|
8
8
|
const AskUserToolName = _enum.Constants.ASK_USER;
|
|
9
9
|
const AskUserDescription = 'Ask the user clarification questions with structured options before taking action. ' +
|
|
10
10
|
'For a single key question, use question+options. For multiple related questions, use steps[] (max 3 steps). ' +
|
|
11
|
-
'Each step can be single-select or multi-select. After receiving answers, proceed immediately.'
|
|
11
|
+
'Each step can be single-select or multi-select. After receiving answers, proceed immediately. ' +
|
|
12
|
+
'BEFORE calling this tool, ALWAYS emit one short sentence of plain-text output (one line, <= 20 words) ' +
|
|
13
|
+
'explaining why you need more information — e.g. "I need a few details to help with that.". ' +
|
|
14
|
+
'NEVER include an option like "I\'ll give it myself", "Other", "Something else", "Custom", "None of the above", ' +
|
|
15
|
+
'or any equivalent free-text escape hatch — the UI already provides a dedicated "Something else" button ' +
|
|
16
|
+
'for free-text responses. Only list concrete, mutually-exclusive choices.';
|
|
12
17
|
const AskUserOptionSchema = zod.z.object({
|
|
13
18
|
label: zod.z.string().describe('Short display label'),
|
|
14
19
|
value: zod.z.string().describe('Machine-readable value returned when selected'),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AskUser.cjs","sources":["../../../src/tools/AskUser.ts"],"sourcesContent":["import { tool } from '@langchain/core/tools';\nimport { z } from 'zod';\nimport { Constants } from '@/common';\n\nexport const AskUserToolName = Constants.ASK_USER;\n\nexport const AskUserDescription =\n 'Ask the user clarification questions with structured options before taking action. ' +\n 'For a single key question, use question+options. For multiple related questions, use steps[] (max 3 steps). ' +\n 'Each step can be single-select or multi-select. After receiving answers, proceed immediately.';\n\nconst AskUserOptionSchema = z.object({\n label: z.string().describe('Short display label'),\n value: z.string().describe('Machine-readable value returned when selected'),\n description: z.string().optional().describe('Optional longer description'),\n});\n\nexport const AskUserStepSchema = z.object({\n question: z.string().describe('The question to ask the user for this step'),\n options: z\n .array(AskUserOptionSchema)\n .min(2)\n .max(6)\n .describe('Structured options for this step (2-6 choices)'),\n type: z\n .enum(['single', 'multi'])\n .default('single')\n .describe('Selection type: single-select or multi-select'),\n});\n\nexport type AskUserStep = z.infer<typeof AskUserStepSchema>;\n\nconst AskUserSchema = z\n .object({\n question: z\n .string()\n .optional()\n .describe('The question to ask the user. Be specific and clear.'),\n options: z\n .array(AskUserOptionSchema)\n .min(2)\n .max(6)\n .optional()\n .describe('Structured options for the user (2-6 choices)'),\n context: z.string().optional().describe('Why you are asking this question'),\n steps: z\n .array(AskUserStepSchema)\n .min(1)\n .max(5)\n .optional()\n .describe('Multi-step wizard: array of question steps (1-5 steps)'),\n })\n // passthrough() preserves extra fields like _selectedOption injected by the\n // approval flow's modifiedArgs — without this, Zod strips unknown keys.\n .passthrough()\n .refine(\n (data) => {\n // Must have either steps[] OR question+options (legacy format)\n const hasSteps = Array.isArray(data.steps) && data.steps.length > 0;\n const hasLegacy =\n typeof data.question === 'string' && Array.isArray(data.options);\n return hasSteps || hasLegacy;\n },\n {\n message: 'Either steps[] or question+options must be provided',\n }\n );\n\n/**\n * Represents a single step selection from the multi-step wizard UI.\n * Exported for use by both ranger web client and browser extension.\n */\nexport type StepSelection = {\n values: string[];\n customInput?: string;\n};\n\n/** Field names injected into modifiedArgs by the approval flow */\nexport const HITL_FIELDS = {\n SELECTIONS: '_selections',\n SELECTED_OPTION: '_selectedOption',\n CUSTOM_INPUT: '_customInput',\n} as const;\n\n/**\n * Creates an ask_user tool that lets the LLM ask clarification questions mid-conversation.\n *\n * How it works:\n * 1. LLM calls ask_user(question, options) OR ask_user(steps)\n * 2. ToolNode.requiresApproval() → always true (per-tool rule in askUserRules.js)\n * 3. ToolNode.requestApproval() promotes options/question/steps to top-level fields on the event\n * 4. Frontend HumanInputPopover renders option cards + freeform text input\n * 5. User selects option(s) OR types custom input →\n * backend resolves with modifiedArgs containing _selections, _selectedOption, or _customInput\n * 6. Tool executes, reads the user's choice, returns result to LLM\n */\n// eslint-disable-next-line @typescript-eslint/explicit-function-return-type\nexport function createAskUserTool() {\n return tool(\n async (args) => {\n const extArgs = args as Record<string, unknown>;\n const selections = extArgs[HITL_FIELDS.SELECTIONS] as\n | StepSelection[]\n | undefined;\n const customInput = extArgs[HITL_FIELDS.CUSTOM_INPUT] as\n | string\n | undefined;\n const selectedOption = extArgs[HITL_FIELDS.SELECTED_OPTION] as\n | string\n | undefined;\n\n // Multi-step wizard flow: _selections is an array of { values, customInput? } per step\n if (\n selections &&\n Array.isArray(selections) &&\n Array.isArray(args.steps)\n ) {\n const lines: string[] = [];\n for (let i = 0; i < args.steps.length; i++) {\n const step = args.steps[i];\n const sel = selections[i];\n if (!sel || (sel.values.length === 0 && !sel.customInput)) {\n lines.push(`${i + 1}. ${step.question} → (skipped)`);\n continue;\n }\n if (sel.customInput) {\n lines.push(`${i + 1}. ${step.question} → \"${sel.customInput}\"`);\n continue;\n }\n // Resolve value(s) to labels\n const labels = sel.values.map((v) => {\n const opt = step.options.find((o) => o.value === v);\n return opt?.label || v;\n });\n lines.push(`${i + 1}. ${step.question} → ${labels.join(', ')}`);\n }\n return `User responses:\\n${lines.join('\\n')}`;\n }\n\n // Legacy: custom freeform input takes priority — user explicitly chose \"none of these\"\n if (customInput) {\n return `User provided custom response: \"${customInput}\"`;\n }\n\n if (selectedOption) {\n const option = args.options?.find((o) => o.value === selectedOption);\n return `User selected: \"${option?.label || selectedOption}\" (value: ${selectedOption})`;\n }\n\n // Fallback — should not happen in interactive mode since approval always fires first\n return 'No user response received.';\n },\n {\n name: AskUserToolName,\n description: AskUserDescription,\n schema: AskUserSchema,\n }\n );\n}\n"],"names":["Constants","z","tool"],"mappings":";;;;;;;AAIO,MAAM,eAAe,GAAGA,eAAS,CAAC;AAElC,MAAM,kBAAkB,GAC7B,qFAAqF;IACrF,8GAA8G;AAC9G,IAAA;AAEF,MAAM,mBAAmB,GAAGC,KAAC,CAAC,MAAM,CAAC;IACnC,KAAK,EAAEA,KAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,qBAAqB,CAAC;IACjD,KAAK,EAAEA,KAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,+CAA+C,CAAC;AAC3E,IAAA,WAAW,EAAEA,KAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,6BAA6B,CAAC;AAC3E,CAAA,CAAC;AAEK,MAAM,iBAAiB,GAAGA,KAAC,CAAC,MAAM,CAAC;IACxC,QAAQ,EAAEA,KAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,4CAA4C,CAAC;AAC3E,IAAA,OAAO,EAAEA;SACN,KAAK,CAAC,mBAAmB;SACzB,GAAG,CAAC,CAAC;SACL,GAAG,CAAC,CAAC;SACL,QAAQ,CAAC,gDAAgD,CAAC;AAC7D,IAAA,IAAI,EAAEA;AACH,SAAA,IAAI,CAAC,CAAC,QAAQ,EAAE,OAAO,CAAC;SACxB,OAAO,CAAC,QAAQ;SAChB,QAAQ,CAAC,+CAA+C,CAAC;AAC7D,CAAA;AAID,MAAM,aAAa,GAAGA;AACnB,KAAA,MAAM,CAAC;AACN,IAAA,QAAQ,EAAEA;AACP,SAAA,MAAM;AACN,SAAA,Q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,IAAI,CAAC,CAAA,EAAG,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC,QAAQ,CAAA,GAAA,EAAM,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA,CAAE,CAAC;YACjE;YACA,OAAO,CAAA,iBAAA,EAAoB,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;QAC/C;;QAGA,IAAI,WAAW,EAAE;YACf,OAAO,CAAA,gCAAA,EAAmC,WAAW,CAAA,CAAA,CAAG;QAC1D;QAEA,IAAI,cAAc,EAAE;AAClB,YAAA,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,KAAK,cAAc,CAAC;YACpE,OAAO,CAAA,gBAAA,EAAmB,MAAM,EAAE,KAAK,IAAI,cAAc,CAAA,UAAA,EAAa,cAAc,CAAA,CAAA,CAAG;QACzF;;AAGA,QAAA,OAAO,4BAA4B;AACrC,IAAA,CAAC,EACD;AACE,QAAA,IAAI,EAAE,eAAe;AACrB,QAAA,WAAW,EAAE,kBAAkB;AAC/B,QAAA,MAAM,EAAE,aAAa;AACtB,KAAA,CACF;AACH;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"AskUser.cjs","sources":["../../../src/tools/AskUser.ts"],"sourcesContent":["import { tool } from '@langchain/core/tools';\nimport { z } from 'zod';\nimport { Constants } from '@/common';\n\nexport const AskUserToolName = Constants.ASK_USER;\n\nexport const AskUserDescription =\n 'Ask the user clarification questions with structured options before taking action. ' +\n 'For a single key question, use question+options. For multiple related questions, use steps[] (max 3 steps). ' +\n 'Each step can be single-select or multi-select. After receiving answers, proceed immediately. ' +\n 'BEFORE calling this tool, ALWAYS emit one short sentence of plain-text output (one line, <= 20 words) ' +\n 'explaining why you need more information — e.g. \"I need a few details to help with that.\". ' +\n 'NEVER include an option like \"I\\'ll give it myself\", \"Other\", \"Something else\", \"Custom\", \"None of the above\", ' +\n 'or any equivalent free-text escape hatch — the UI already provides a dedicated \"Something else\" button ' +\n 'for free-text responses. Only list concrete, mutually-exclusive choices.';\n\nconst AskUserOptionSchema = z.object({\n label: z.string().describe('Short display label'),\n value: z.string().describe('Machine-readable value returned when selected'),\n description: z.string().optional().describe('Optional longer description'),\n});\n\nexport const AskUserStepSchema = z.object({\n question: z.string().describe('The question to ask the user for this step'),\n options: z\n .array(AskUserOptionSchema)\n .min(2)\n .max(6)\n .describe('Structured options for this step (2-6 choices)'),\n type: z\n .enum(['single', 'multi'])\n .default('single')\n .describe('Selection type: single-select or multi-select'),\n});\n\nexport type AskUserStep = z.infer<typeof AskUserStepSchema>;\n\nconst AskUserSchema = z\n .object({\n question: z\n .string()\n .optional()\n .describe('The question to ask the user. Be specific and clear.'),\n options: z\n .array(AskUserOptionSchema)\n .min(2)\n .max(6)\n .optional()\n .describe('Structured options for the user (2-6 choices)'),\n context: z.string().optional().describe('Why you are asking this question'),\n steps: z\n .array(AskUserStepSchema)\n .min(1)\n .max(5)\n .optional()\n .describe('Multi-step wizard: array of question steps (1-5 steps)'),\n })\n // passthrough() preserves extra fields like _selectedOption injected by the\n // approval flow's modifiedArgs — without this, Zod strips unknown keys.\n .passthrough()\n .refine(\n (data) => {\n // Must have either steps[] OR question+options (legacy format)\n const hasSteps = Array.isArray(data.steps) && data.steps.length > 0;\n const hasLegacy =\n typeof data.question === 'string' && Array.isArray(data.options);\n return hasSteps || hasLegacy;\n },\n {\n message: 'Either steps[] or question+options must be provided',\n }\n );\n\n/**\n * Represents a single step selection from the multi-step wizard UI.\n * Exported for use by both the host web client and browser extension.\n */\nexport type StepSelection = {\n values: string[];\n customInput?: string;\n};\n\n/** Field names injected into modifiedArgs by the approval flow */\nexport const HITL_FIELDS = {\n SELECTIONS: '_selections',\n SELECTED_OPTION: '_selectedOption',\n CUSTOM_INPUT: '_customInput',\n} as const;\n\n/**\n * Creates an ask_user tool that lets the LLM ask clarification questions mid-conversation.\n *\n * How it works:\n * 1. LLM calls ask_user(question, options) OR ask_user(steps)\n * 2. ToolNode.requiresApproval() → always true (per-tool rule in askUserRules.js)\n * 3. ToolNode.requestApproval() promotes options/question/steps to top-level fields on the event\n * 4. Frontend HumanInputPopover renders option cards + freeform text input\n * 5. User selects option(s) OR types custom input →\n * backend resolves with modifiedArgs containing _selections, _selectedOption, or _customInput\n * 6. Tool executes, reads the user's choice, returns result to LLM\n */\n// eslint-disable-next-line @typescript-eslint/explicit-function-return-type\nexport function createAskUserTool() {\n return tool(\n async (args) => {\n const extArgs = args as Record<string, unknown>;\n const selections = extArgs[HITL_FIELDS.SELECTIONS] as\n | StepSelection[]\n | undefined;\n const customInput = extArgs[HITL_FIELDS.CUSTOM_INPUT] as\n | string\n | undefined;\n const selectedOption = extArgs[HITL_FIELDS.SELECTED_OPTION] as\n | string\n | undefined;\n\n // Multi-step wizard flow: _selections is an array of { values, customInput? } per step\n if (\n selections &&\n Array.isArray(selections) &&\n Array.isArray(args.steps)\n ) {\n const lines: string[] = [];\n for (let i = 0; i < args.steps.length; i++) {\n const step = args.steps[i];\n const sel = selections[i];\n if (!sel || (sel.values.length === 0 && !sel.customInput)) {\n lines.push(`${i + 1}. ${step.question} → (skipped)`);\n continue;\n }\n if (sel.customInput) {\n lines.push(`${i + 1}. ${step.question} → \"${sel.customInput}\"`);\n continue;\n }\n // Resolve value(s) to labels\n const labels = sel.values.map((v) => {\n const opt = step.options.find((o) => o.value === v);\n return opt?.label || v;\n });\n lines.push(`${i + 1}. ${step.question} → ${labels.join(', ')}`);\n }\n return `User responses:\\n${lines.join('\\n')}`;\n }\n\n // Legacy: custom freeform input takes priority — user explicitly chose \"none of these\"\n if (customInput) {\n return `User provided custom response: \"${customInput}\"`;\n }\n\n if (selectedOption) {\n const option = args.options?.find((o) => o.value === selectedOption);\n return `User selected: \"${option?.label || selectedOption}\" (value: ${selectedOption})`;\n }\n\n // Fallback — should not happen in interactive mode since approval always fires first\n return 'No user response received.';\n },\n {\n name: AskUserToolName,\n description: AskUserDescription,\n schema: AskUser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A,iBAAA,EAAoB,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;QAC/C;;QAGA,IAAI,WAAW,EAAE;YACf,OAAO,CAAA,gCAAA,EAAmC,WAAW,CAAA,CAAA,CAAG;QAC1D;QAEA,IAAI,cAAc,EAAE;AAClB,YAAA,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,KAAK,cAAc,CAAC;YACpE,OAAO,CAAA,gBAAA,EAAmB,MAAM,EAAE,KAAK,IAAI,cAAc,CAAA,UAAA,EAAa,cAAc,CAAA,CAAA,CAAG;QACzF;;AAGA,QAAA,OAAO,4BAA4B;AACrC,IAAA,CAAC,EACD;AACE,QAAA,IAAI,EAAE,eAAe;AACrB,QAAA,WAAW,EAAE,kBAAkB;AAC/B,QAAA,MAAM,EAAE,aAAa;AACtB,KAAA,CACF;AACH;;;;;;;;"}
|
|
@@ -4,7 +4,7 @@ var zod = require('zod');
|
|
|
4
4
|
var tools = require('@langchain/core/tools');
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Browser tool names - keep in sync with
|
|
7
|
+
* Browser tool names - keep in sync with the browser extension
|
|
8
8
|
* These tools execute locally in the browser extension, NOT on the server
|
|
9
9
|
*/
|
|
10
10
|
const EBrowserTools = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BrowserTools.cjs","sources":["../../../src/tools/BrowserTools.ts"],"sourcesContent":["import { z } from 'zod';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport type * as _t from '@/types';\n\n/**\n * Browser tool names - keep in sync with ranger-browser extension\n * These tools execute locally in the browser extension, NOT on the server\n */\nexport const EBrowserTools = {\n CLICK: 'browser_click',\n TYPE: 'browser_type',\n NAVIGATE: 'browser_navigate',\n SCROLL: 'browser_scroll',\n EXTRACT: 'browser_extract',\n HOVER: 'browser_hover',\n WAIT: 'browser_wait',\n BACK: 'browser_back',\n SCREENSHOT: 'browser_screenshot',\n GET_PAGE_STATE: 'browser_get_page_state',\n KEYPRESS: 'browser_keypress',\n SWITCH_TAB: 'browser_switch_tab',\n} as const;\n\nexport type BrowserToolName =\n (typeof EBrowserTools)[keyof typeof EBrowserTools];\n\n/**\n * Callback function type for waiting on browser action results\n * This allows the server (Ranger) to provide a callback that waits for the extension\n * to POST results back to the server before returning to the LLM.\n *\n * @param action - The browser action (click, type, navigate, etc.)\n * @param args - Arguments for the action\n * @param toolCallId - Unique ID for this tool call (from config.toolCall.id)\n * @returns Promise that resolves with the actual browser result (page state, etc.)\n */\nexport type BrowserToolCallback = (\n action: string,\n args: Record<string, unknown>,\n toolCallId: string\n) => Promise<BrowserActionResult>;\n\n/**\n * Result returned from browser action execution\n */\nexport interface BrowserActionResult {\n success: boolean;\n url?: string;\n title?: string;\n elementList?: string; // Text-based element list\n error?: string;\n screenshot?: string; // Base64 screenshot (if requested)\n}\n\n/**\n * Check if browser capability is available based on request headers or context\n * The browser extension sets these headers when connected:\n * - X-Illuma-Browser-Extension: true\n * - X-Illuma-Browser-Capable: true\n */\nexport function hasBrowserCapability(req?: {\n headers?: Record<string, string | string[] | undefined>;\n}): boolean {\n if (!req?.headers) {\n return false;\n }\n\n const browserExtension = req.headers['x-illuma-browser-extension'];\n const browserCapable = req.headers['x-illuma-browser-capable'];\n\n return browserExtension === 'true' || browserCapable === 'true';\n}\n\n// Tool schemas\nconst BrowserClickSchema = z.object({\n index: z\n .number()\n .describe(\n 'The [index] of the element to click. Use fieldLabel to identify the correct element. For form fields, target <input> or <textarea> elements, NOT parent <div> containers.'\n ),\n text: z\n .string()\n .optional()\n .describe(\n 'The visible text of the element being clicked (e.g., \"Send\", \"Delete\", \"Buy Now\"). Always include this for buttons and links.'\n ),\n label: z\n .string()\n .optional()\n .describe(\n 'The fieldLabel or ariaLabel of the element, if available.'\n ),\n});\n\nconst BrowserTypeSchema = z.object({\n index: z\n .number()\n .describe(\n 'The [index] of the INPUT element to type into. Target <input> or <textarea> elements. Check fieldLabel to identify the correct field.'\n ),\n text: z.string().describe('The text to type into the element'),\n pressEnter: z\n .boolean()\n .optional()\n .describe(\n 'Whether to press Enter after typing (useful for search forms and submitting)'\n ),\n});\n\nconst BrowserNavigateSchema = z.object({\n url: z\n .string()\n .describe('The full URL to navigate to (must include https://)'),\n});\n\nconst BrowserScrollSchema = z.object({\n direction: z\n .enum(['up', 'down', 'left', 'right'])\n .describe('Direction to scroll'),\n amount: z\n .number()\n .optional()\n .describe('Pixels to scroll (default: one viewport height)'),\n});\n\nconst BrowserExtractSchema = z.object({\n query: z\n .string()\n .optional()\n .describe('Optional: specific content to extract from the page'),\n});\n\nconst BrowserHoverSchema = z.object({\n index: z.number().describe('The index number of the element to hover over'),\n});\n\nconst BrowserWaitSchema = z.object({\n duration: z\n .number()\n .optional()\n .describe('Milliseconds to wait (default: 1000)'),\n});\n\nconst BrowserBackSchema = z.object({});\n\nconst BrowserScreenshotSchema = z.object({});\n\nconst BrowserGetPageStateSchema = z.object({});\n\nconst BrowserKeypressSchema = z.object({\n keys: z\n .string()\n .describe(\n 'Keyboard keys to press. Use \"+\" to combine modifiers (e.g., \"Control+Enter\", \"Control+a\", \"Escape\", \"Tab\", \"Enter\"). Common shortcuts: Control+Enter (submit forms/send), Escape (close dialogs), Tab (next field).'\n ),\n});\n\nconst BrowserSwitchTabSchema = z.object({\n tabId: z\n .number()\n .describe(\n 'The tab ID to switch to. Use the tab IDs shown in the tabs list from page state.'\n ),\n});\n\n/**\n * Browser tool response interface\n * This is what the extension returns after executing the action\n */\nexport interface BrowserToolResponse {\n requiresBrowserExecution: true;\n action: string;\n args: Record<string, unknown>;\n toolCallId?: string; // Added to help extension correlate with callback\n}\n\n/**\n * Options for creating browser tools\n */\nexport interface CreateBrowserToolsOptions {\n /**\n * Optional callback that waits for browser action results.\n * When provided, tools will await this callback to get actual results from the extension.\n * When not provided, tools return markers immediately (for non-server contexts).\n */\n waitForResult?: BrowserToolCallback;\n}\n\n/**\n * Format browser action result for LLM consumption\n */\nfunction formatResultForLLM(\n result: BrowserActionResult,\n action: string\n): string {\n const parts: string[] = [];\n\n // Include error info but DON'T discard page state — the extension often sends\n // success=false with valid page state (URL, elementList) that the LLM needs\n if (!result.success && result.error) {\n parts.push(`**Note:** Action \"${action}\" reported: ${result.error}`);\n }\n\n if (result.url != null && result.url !== '') {\n parts.push(`**Current URL:** ${result.url}`);\n }\n if (result.title != null && result.title !== '') {\n parts.push(`**Page Title:** ${result.title}`);\n }\n if (result.elementList != null && result.elementList !== '') {\n parts.push(\n `\\n**Interactive Elements** (for typing: target <input> elements with fieldLabel, NOT parent <div> containers):\\n${result.elementList}`\n );\n }\n if (result.screenshot != null && result.screenshot !== '') {\n parts.push('\\n[Screenshot captured and displayed to user]');\n }\n\n if (parts.length === 0) {\n if (!result.success) {\n return `Browser action \"${action}\" failed: ${result.error || 'Unknown error'}`;\n }\n return `Browser action \"${action}\" completed successfully.`;\n }\n\n return parts.join('\\n');\n}\n\n/**\n * Create browser tools with optional callback for waiting on results\n *\n * When waitForResult callback is provided:\n * 1. Tool returns marker that triggers extension\n * 2. Tool then awaits callback to get actual results\n * 3. Returns real page state to LLM\n *\n * When no callback:\n * 1. Tool returns marker only (for non-server contexts)\n *\n * NOTE: These tools use TEXT-BASED element lists, NOT screenshots\n * Screenshots would be 100K+ tokens each - element lists are ~100 tokens\n */\nexport function createBrowserTools(\n options?: CreateBrowserToolsOptions\n): DynamicStructuredTool[] {\n const { waitForResult } = options || {};\n const tools: DynamicStructuredTool[] = [];\n\n /**\n * Helper to create tool function that optionally waits for results\n * The toolCallId is extracted from the RunnableConfig passed by LangChain\n */\n const createToolFunction = (action: string) => {\n return async (\n args: Record<string, unknown>,\n config?: { toolCall?: { id?: string } }\n ): Promise<string> => {\n const toolCallId =\n config?.toolCall?.id ??\n `tool_${Date.now()}_${Math.random().toString(36).slice(2)}`;\n\n // Create marker for extension\n const marker: BrowserToolResponse = {\n requiresBrowserExecution: true,\n action,\n args,\n toolCallId,\n };\n\n // If no callback, return marker immediately (extension handles via SSE interception)\n if (!waitForResult) {\n return JSON.stringify(marker);\n }\n\n // With callback: wait for actual results from extension\n // The marker is still returned initially via SSE, but we wait for the callback\n try {\n const result = await waitForResult(action, args, toolCallId);\n return formatResultForLLM(result, action);\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n return `Browser action \"${action}\" failed: ${errorMessage}`;\n }\n };\n };\n\n // browser_click\n tools.push(\n tool(createToolFunction('click'), {\n name: EBrowserTools.CLICK,\n description:\n 'Click element by [index]. ALWAYS include the element text and label in your call for safety review. Use fieldLabel attribute to identify correct element. For form fields, target <input> elements NOT parent <div> containers.',\n schema: BrowserClickSchema,\n })\n );\n\n // browser_type\n tools.push(\n tool(createToolFunction('type'), {\n name: EBrowserTools.TYPE,\n description:\n 'Type text into <input> element by [index]. CRITICAL: Always target <input> or <textarea> tags (NOT parent <div> containers). Use fieldLabel to identify correct field (e.g., fieldLabel=\"To recipients\" for To field).',\n schema: BrowserTypeSchema,\n })\n );\n\n // browser_navigate\n tools.push(\n tool(createToolFunction('navigate'), {\n name: EBrowserTools.NAVIGATE,\n description:\n 'Navigate to URL (include https://). Returns new page element list.',\n schema: BrowserNavigateSchema,\n })\n );\n\n // browser_scroll\n tools.push(\n tool(createToolFunction('scroll'), {\n name: EBrowserTools.SCROLL,\n description:\n 'Scroll page (up/down/left/right). Returns updated element list.',\n schema: BrowserScrollSchema,\n })\n );\n\n // browser_extract\n tools.push(\n tool(createToolFunction('extract'), {\n name: EBrowserTools.EXTRACT,\n description:\n 'Extract page content. Returns URL, title, and element list.',\n schema: BrowserExtractSchema,\n })\n );\n\n // browser_hover\n tools.push(\n tool(createToolFunction('hover'), {\n name: EBrowserTools.HOVER,\n description:\n 'Hover element by [index] to reveal menus/tooltips. Returns updated element list.',\n schema: BrowserHoverSchema,\n })\n );\n\n // browser_wait\n tools.push(\n tool(createToolFunction('wait'), {\n name: EBrowserTools.WAIT,\n description:\n 'Wait for async content to load. Returns updated element list.',\n schema: BrowserWaitSchema,\n })\n );\n\n // browser_back\n tools.push(\n tool(createToolFunction('back'), {\n name: EBrowserTools.BACK,\n description:\n 'Go back in browser history. Returns previous page element list.',\n schema: BrowserBackSchema,\n })\n );\n\n // browser_screenshot\n tools.push(\n tool(createToolFunction('screenshot'), {\n name: EBrowserTools.SCREENSHOT,\n description:\n 'Capture screenshot. Displayed to user. Use get_page_state for automation.',\n schema: BrowserScreenshotSchema,\n })\n );\n\n // browser_get_page_state\n tools.push(\n tool(createToolFunction('get_page_state'), {\n name: EBrowserTools.GET_PAGE_STATE,\n description:\n 'Get page URL, title, and interactive elements with [index] for actions. Start here.',\n schema: BrowserGetPageStateSchema,\n })\n );\n\n // browser_keypress - for keyboard shortcuts\n tools.push(\n tool(createToolFunction('keypress'), {\n name: EBrowserTools.KEYPRESS,\n description:\n 'Send keyboard shortcut or key press. Use for: Control+Enter (send email/submit), Escape (close dialog/cancel), Tab (next field), Enter (confirm). The keys are sent to the currently focused element.',\n schema: BrowserKeypressSchema,\n })\n );\n\n // browser_switch_tab - for switching between tabs\n tools.push(\n tool(createToolFunction('switch_tab'), {\n name: EBrowserTools.SWITCH_TAB,\n description:\n 'Switch to a different browser tab by its ID. Tab IDs are shown in the page state. Use this to work with existing open tabs (e.g., use existing Gmail tab instead of opening a new one).',\n schema: BrowserSwitchTabSchema,\n })\n );\n\n return tools;\n}\n"],"names":["z","tools","tool"],"mappings":";;;;;AAIA;;;AAGG;AACI,MAAM,aAAa,GAAG;AAC3B,IAAA,KAAK,EAAE,eAAe;AACtB,IAAA,IAAI,EAAE,cAAc;AACpB,IAAA,QAAQ,EAAE,kBAAkB;AAC5B,IAAA,MAAM,EAAE,gBAAgB;AACxB,IAAA,OAAO,EAAE,iBAAiB;AAC1B,IAAA,KAAK,EAAE,eAAe;AACtB,IAAA,IAAI,EAAE,cAAc;AACpB,IAAA,IAAI,EAAE,cAAc;AACpB,IAAA,UAAU,EAAE,oBAAoB;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["../../../src/tools/BrowserTools.ts"],"sourcesContent":["import { z } from 'zod';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport type * as _t from '@/types';\n\n/**\n * Browser tool names - keep in sync with the browser extension\n * These tools execute locally in the browser extension, NOT on the server\n */\nexport const EBrowserTools = {\n CLICK: 'browser_click',\n TYPE: 'browser_type',\n NAVIGATE: 'browser_navigate',\n SCROLL: 'browser_scroll',\n EXTRACT: 'browser_extract',\n HOVER: 'browser_hover',\n WAIT: 'browser_wait',\n BACK: 'browser_back',\n SCREENSHOT: 'browser_screenshot',\n GET_PAGE_STATE: 'browser_get_page_state',\n KEYPRESS: 'browser_keypress',\n SWITCH_TAB: 'browser_switch_tab',\n} as const;\n\nexport type BrowserToolName =\n (typeof EBrowserTools)[keyof typeof EBrowserTools];\n\n/**\n * Callback function type for waiting on browser action results\n * This allows the host server to provide a callback that waits for the extension\n * to POST results back to the server before returning to the LLM.\n *\n * @param action - The browser action (click, type, navigate, etc.)\n * @param args - Arguments for the action\n * @param toolCallId - Unique ID for this tool call (from config.toolCall.id)\n * @returns Promise that resolves with the actual browser result (page state, etc.)\n */\nexport type BrowserToolCallback = (\n action: string,\n args: Record<string, unknown>,\n toolCallId: string\n) => Promise<BrowserActionResult>;\n\n/**\n * Result returned from browser action execution\n */\nexport interface BrowserActionResult {\n success: boolean;\n url?: string;\n title?: string;\n elementList?: string; // Text-based element list\n error?: string;\n screenshot?: string; // Base64 screenshot (if requested)\n}\n\n/**\n * Check if browser capability is available based on request headers or context\n * The browser extension sets these headers when connected:\n * - X-Illuma-Browser-Extension: true\n * - X-Illuma-Browser-Capable: true\n */\nexport function hasBrowserCapability(req?: {\n headers?: Record<string, string | string[] | undefined>;\n}): boolean {\n if (!req?.headers) {\n return false;\n }\n\n const browserExtension = req.headers['x-illuma-browser-extension'];\n const browserCapable = req.headers['x-illuma-browser-capable'];\n\n return browserExtension === 'true' || browserCapable === 'true';\n}\n\n// Tool schemas\nconst BrowserClickSchema = z.object({\n index: z\n .number()\n .describe(\n 'The [index] of the element to click. Use fieldLabel to identify the correct element. For form fields, target <input> or <textarea> elements, NOT parent <div> containers.'\n ),\n text: z\n .string()\n .optional()\n .describe(\n 'The visible text of the element being clicked (e.g., \"Send\", \"Delete\", \"Buy Now\"). Always include this for buttons and links.'\n ),\n label: z\n .string()\n .optional()\n .describe('The fieldLabel or ariaLabel of the element, if available.'),\n});\n\nconst BrowserTypeSchema = z.object({\n index: z\n .number()\n .describe(\n 'The [index] of the INPUT element to type into. Target <input> or <textarea> elements. Check fieldLabel to identify the correct field.'\n ),\n text: z.string().describe('The text to type into the element'),\n pressEnter: z\n .boolean()\n .optional()\n .describe(\n 'Whether to press Enter after typing (useful for search forms and submitting)'\n ),\n});\n\nconst BrowserNavigateSchema = z.object({\n url: z\n .string()\n .describe('The full URL to navigate to (must include https://)'),\n});\n\nconst BrowserScrollSchema = z.object({\n direction: z\n .enum(['up', 'down', 'left', 'right'])\n .describe('Direction to scroll'),\n amount: z\n .number()\n .optional()\n .describe('Pixels to scroll (default: one viewport height)'),\n});\n\nconst BrowserExtractSchema = z.object({\n query: z\n .string()\n .optional()\n .describe('Optional: specific content to extract from the page'),\n});\n\nconst BrowserHoverSchema = z.object({\n index: z.number().describe('The index number of the element to hover over'),\n});\n\nconst BrowserWaitSchema = z.object({\n duration: z\n .number()\n .optional()\n .describe('Milliseconds to wait (default: 1000)'),\n});\n\nconst BrowserBackSchema = z.object({});\n\nconst BrowserScreenshotSchema = z.object({});\n\nconst BrowserGetPageStateSchema = z.object({});\n\nconst BrowserKeypressSchema = z.object({\n keys: z\n .string()\n .describe(\n 'Keyboard keys to press. Use \"+\" to combine modifiers (e.g., \"Control+Enter\", \"Control+a\", \"Escape\", \"Tab\", \"Enter\"). Common shortcuts: Control+Enter (submit forms/send), Escape (close dialogs), Tab (next field).'\n ),\n});\n\nconst BrowserSwitchTabSchema = z.object({\n tabId: z\n .number()\n .describe(\n 'The tab ID to switch to. Use the tab IDs shown in the tabs list from page state.'\n ),\n});\n\n/**\n * Browser tool response interface\n * This is what the extension returns after executing the action\n */\nexport interface BrowserToolResponse {\n requiresBrowserExecution: true;\n action: string;\n args: Record<string, unknown>;\n toolCallId?: string; // Added to help extension correlate with callback\n}\n\n/**\n * Options for creating browser tools\n */\nexport interface CreateBrowserToolsOptions {\n /**\n * Optional callback that waits for browser action results.\n * When provided, tools will await this callback to get actual results from the extension.\n * When not provided, tools return markers immediately (for non-server contexts).\n */\n waitForResult?: BrowserToolCallback;\n}\n\n/**\n * Format browser action result for LLM consumption\n */\nfunction formatResultForLLM(\n result: BrowserActionResult,\n action: string\n): string {\n const parts: string[] = [];\n\n // Include error info but DON'T discard page state — the extension often sends\n // success=false with valid page state (URL, elementList) that the LLM needs\n if (!result.success && result.error) {\n parts.push(`**Note:** Action \"${action}\" reported: ${result.error}`);\n }\n\n if (result.url != null && result.url !== '') {\n parts.push(`**Current URL:** ${result.url}`);\n }\n if (result.title != null && result.title !== '') {\n parts.push(`**Page Title:** ${result.title}`);\n }\n if (result.elementList != null && result.elementList !== '') {\n parts.push(\n `\\n**Interactive Elements** (for typing: target <input> elements with fieldLabel, NOT parent <div> containers):\\n${result.elementList}`\n );\n }\n if (result.screenshot != null && result.screenshot !== '') {\n parts.push('\\n[Screenshot captured and displayed to user]');\n }\n\n if (parts.length === 0) {\n if (!result.success) {\n return `Browser action \"${action}\" failed: ${result.error || 'Unknown error'}`;\n }\n return `Browser action \"${action}\" completed successfully.`;\n }\n\n return parts.join('\\n');\n}\n\n/**\n * Create browser tools with optional callback for waiting on results\n *\n * When waitForResult callback is provided:\n * 1. Tool returns marker that triggers extension\n * 2. Tool then awaits callback to get actual results\n * 3. Returns real page state to LLM\n *\n * When no callback:\n * 1. Tool returns marker only (for non-server contexts)\n *\n * NOTE: These tools use TEXT-BASED element lists, NOT screenshots\n * Screenshots would be 100K+ tokens each - element lists are ~100 tokens\n */\nexport function createBrowserTools(\n options?: CreateBrowserToolsOptions\n): DynamicStructuredTool[] {\n const { waitForResult } = options || {};\n const tools: DynamicStructuredTool[] = [];\n\n /**\n * Helper to create tool function that optionally waits for results\n * The toolCallId is extracted from the RunnableConfig passed by LangChain\n */\n const createToolFunction = (action: string) => {\n return async (\n args: Record<string, unknown>,\n config?: { toolCall?: { id?: string } }\n ): Promise<string> => {\n const toolCallId =\n config?.toolCall?.id ??\n `tool_${Date.now()}_${Math.random().toString(36).slice(2)}`;\n\n // Create marker for extension\n const marker: BrowserToolResponse = {\n requiresBrowserExecution: true,\n action,\n args,\n toolCallId,\n };\n\n // If no callback, return marker immediately (extension handles via SSE interception)\n if (!waitForResult) {\n return JSON.stringify(marker);\n }\n\n // With callback: wait for actual results from extension\n // The marker is still returned initially via SSE, but we wait for the callback\n try {\n const result = await waitForResult(action, args, toolCallId);\n return formatResultForLLM(result, action);\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n return `Browser action \"${action}\" failed: ${errorMessage}`;\n }\n };\n };\n\n // browser_click\n tools.push(\n tool(createToolFunction('click'), {\n name: EBrowserTools.CLICK,\n description:\n 'Click element by [index]. ALWAYS include the element text and label in your call for safety review. Use fieldLabel attribute to identify correct element. For form fields, target <input> elements NOT parent <div> containers.',\n schema: BrowserClickSchema,\n })\n );\n\n // browser_type\n tools.push(\n tool(createToolFunction('type'), {\n name: EBrowserTools.TYPE,\n description:\n 'Type text into <input> element by [index]. CRITICAL: Always target <input> or <textarea> tags (NOT parent <div> containers). Use fieldLabel to identify correct field (e.g., fieldLabel=\"To recipients\" for To field).',\n schema: BrowserTypeSchema,\n })\n );\n\n // browser_navigate\n tools.push(\n tool(createToolFunction('navigate'), {\n name: EBrowserTools.NAVIGATE,\n description:\n 'Navigate to URL (include https://). Returns new page element list.',\n schema: BrowserNavigateSchema,\n })\n );\n\n // browser_scroll\n tools.push(\n tool(createToolFunction('scroll'), {\n name: EBrowserTools.SCROLL,\n description:\n 'Scroll page (up/down/left/right). Returns updated element list.',\n schema: BrowserScrollSchema,\n })\n );\n\n // browser_extract\n tools.push(\n tool(createToolFunction('extract'), {\n name: EBrowserTools.EXTRACT,\n description:\n 'Extract page content. Returns URL, title, and element list.',\n schema: BrowserExtractSchema,\n })\n );\n\n // browser_hover\n tools.push(\n tool(createToolFunction('hover'), {\n name: EBrowserTools.HOVER,\n description:\n 'Hover element by [index] to reveal menus/tooltips. Returns updated element list.',\n schema: BrowserHoverSchema,\n })\n );\n\n // browser_wait\n tools.push(\n tool(createToolFunction('wait'), {\n name: EBrowserTools.WAIT,\n description:\n 'Wait for async content to load. Returns updated element list.',\n schema: BrowserWaitSchema,\n })\n );\n\n // browser_back\n tools.push(\n tool(createToolFunction('back'), {\n name: EBrowserTools.BACK,\n description:\n 'Go back in browser history. Returns previous page element list.',\n schema: BrowserBackSchema,\n })\n );\n\n // browser_screenshot\n tools.push(\n tool(createToolFunction('screenshot'), {\n name: EBrowserTools.SCREENSHOT,\n description:\n 'Capture screenshot. Displayed to user. Use get_page_state for automation.',\n schema: BrowserScreenshotSchema,\n })\n );\n\n // browser_get_page_state\n tools.push(\n tool(createToolFunction('get_page_state'), {\n name: EBrowserTools.GET_PAGE_STATE,\n description:\n 'Get page URL, title, and interactive elements with [index] for actions. Start here.',\n schema: BrowserGetPageStateSchema,\n })\n );\n\n // browser_keypress - for keyboard shortcuts\n tools.push(\n tool(createToolFunction('keypress'), {\n name: EBrowserTools.KEYPRESS,\n description:\n 'Send keyboard shortcut or key press. Use for: Control+Enter (send email/submit), Escape (close dialog/cancel), Tab (next field), Enter (confirm). The keys are sent to the currently focused element.',\n schema: BrowserKeypressSchema,\n })\n );\n\n // browser_switch_tab - for switching between tabs\n tools.push(\n tool(createToolFunction('switch_tab'), {\n name: EBrowserTools.SWITCH_TAB,\n description:\n 'Switch to a different browser tab by its ID. Tab IDs are shown in the page state. Use this to work with existing open tabs (e.g., use existing Gmail tab instead of opening a new one).',\n schema: BrowserSwitchTabSchema,\n })\n );\n\n return tools;\n}\n"],"names":["z","tools","tool"],"mappings":";;;;;A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EAAE,qBAAqB;AAC9B,KAAA,CAAC,CACH;;IAGDD,OAAK,CAAC,IAAI,CACRC,UAAI,CAAC,kBAAkB,CAAC,YAAY,CAAC,EAAE;QACrC,IAAI,EAAE,aAAa,CAAC,UAAU;AAC9B,QAAA,WAAW,EACT,yLAAyL;AAC3L,QAAA,MAAM,EAAE,sBAAsB;AAC/B,KAAA,CAAC,CACH;AAED,IAAA,OAAOD,OAAK;AACd;;;;;;"}
|
|
@@ -139,7 +139,8 @@ class ToolNode extends run.RunnableCallable {
|
|
|
139
139
|
// Resolve the effective execution context for this agent.
|
|
140
140
|
// Per-agent overrides take precedence — allows handoff agents to bypass HITL
|
|
141
141
|
// while the primary agent retains interactive approval.
|
|
142
|
-
const effectiveContext = (this.agentId && agentExecutionContextOverrides?.[this.agentId]) ??
|
|
142
|
+
const effectiveContext = (this.agentId && agentExecutionContextOverrides?.[this.agentId]) ??
|
|
143
|
+
executionContext;
|
|
143
144
|
// Scheduled executions bypass all approval checks — no user is present
|
|
144
145
|
if (effectiveContext === constants.ExecutionContext.SCHEDULED) {
|
|
145
146
|
return false;
|
|
@@ -151,9 +152,19 @@ class ToolNode extends run.RunnableCallable {
|
|
|
151
152
|
if (effectiveContext === constants.ExecutionContext.HANDOFF) {
|
|
152
153
|
return false;
|
|
153
154
|
}
|
|
154
|
-
// Graph-managed tools (handoff/transfer
|
|
155
|
-
// these are internal routing mechanisms, not user-facing tool executions
|
|
156
|
-
|
|
155
|
+
// Graph-managed routing tools (handoff/transfer) bypass HITL approval —
|
|
156
|
+
// these are internal routing mechanisms, not user-facing tool executions.
|
|
157
|
+
//
|
|
158
|
+
// NOTE: `directToolNames` is used for two purposes — (1) marking tools that
|
|
159
|
+
// are loaded as full instances and don't need on-demand ON_TOOL_EXECUTE loading,
|
|
160
|
+
// and (2) bypassing HITL. In event-driven mode ALL built-in tools (including
|
|
161
|
+
// `ask_user`) end up in directToolNames for reason (1), so we cannot use
|
|
162
|
+
// `directToolNames.has(toolName)` as the HITL-bypass test — it would let
|
|
163
|
+
// `ask_user` execute without ever firing interrupt(), defeating the whole tool.
|
|
164
|
+
// Instead, gate the bypass on the actual routing-tool name prefix.
|
|
165
|
+
if (this.directToolNames?.has(toolName) &&
|
|
166
|
+
(toolName.startsWith(_enum.Constants.LC_TRANSFER_TO_) ||
|
|
167
|
+
toolName.startsWith(_enum.Constants.LC_HANDOFF_TO_))) {
|
|
157
168
|
return false;
|
|
158
169
|
}
|
|
159
170
|
// Determine the effective policy for this tool
|
|
@@ -185,7 +196,7 @@ class ToolNode extends run.RunnableCallable {
|
|
|
185
196
|
* @param config - The runnable config for event dispatch
|
|
186
197
|
* @returns The approval response from the human
|
|
187
198
|
*/
|
|
188
|
-
requestApproval(call, config) {
|
|
199
|
+
async requestApproval(call, config) {
|
|
189
200
|
const approvalRequest = {
|
|
190
201
|
type: 'tool_approval_required',
|
|
191
202
|
toolCallId: call.id ?? '',
|
|
@@ -194,9 +205,13 @@ class ToolNode extends run.RunnableCallable {
|
|
|
194
205
|
agentId: this.agentId,
|
|
195
206
|
description: `Tool "${call.name}" wants to execute with the provided arguments.`,
|
|
196
207
|
};
|
|
197
|
-
//
|
|
198
|
-
//
|
|
199
|
-
|
|
208
|
+
// MUST await — interrupt() throws GraphInterrupt synchronously which unwinds
|
|
209
|
+
// the call stack. Any un-awaited dispatch Promise is abandoned before the
|
|
210
|
+
// host's ON_TOOL_APPROVAL_REQUIRED handler runs, so the MongoDB row never
|
|
211
|
+
// gets written and the subsequent approve-tool POST 404s with
|
|
212
|
+
// "No pending approval found". Awaiting guarantees the handler has persisted
|
|
213
|
+
// the request before we suspend the graph.
|
|
214
|
+
await events.safeDispatchCustomEvent(_enum.GraphEvents.ON_TOOL_APPROVAL_REQUIRED, approvalRequest, config);
|
|
200
215
|
// interrupt() throws GraphInterrupt on first call (checkpoints state),
|
|
201
216
|
// returns the resume value on re-execution after Command({ resume }).
|
|
202
217
|
const response = langgraph.interrupt(approvalRequest);
|
|
@@ -294,7 +309,7 @@ class ToolNode extends run.RunnableCallable {
|
|
|
294
309
|
// Resumes when host sends Command({ resume: ToolApprovalResponse }).
|
|
295
310
|
// ========================================================================
|
|
296
311
|
if (this.requiresApproval(call.name, call.args)) {
|
|
297
|
-
const approvalResponse = this.requestApproval(call, config);
|
|
312
|
+
const approvalResponse = await this.requestApproval(call, config);
|
|
298
313
|
if (!approvalResponse.approved) {
|
|
299
314
|
// Human denied the tool call - return a denial message
|
|
300
315
|
return new messages.ToolMessage({
|
|
@@ -659,7 +674,7 @@ class ToolNode extends run.RunnableCallable {
|
|
|
659
674
|
const denialMessages = [];
|
|
660
675
|
for (const call of toolCalls) {
|
|
661
676
|
if (this.requiresApproval(call.name, call.args)) {
|
|
662
|
-
const approvalResponse = this.requestApproval(call, config);
|
|
677
|
+
const approvalResponse = await this.requestApproval(call, config);
|
|
663
678
|
if (!approvalResponse.approved) {
|
|
664
679
|
denialMessages.push(new messages.ToolMessage({
|
|
665
680
|
status: 'error',
|
|
@@ -913,6 +928,108 @@ class ToolNode extends run.RunnableCallable {
|
|
|
913
928
|
combinedOutputs.push(Array.isArray(input) ? [output] : { messages: [output] });
|
|
914
929
|
}
|
|
915
930
|
}
|
|
931
|
+
/**
|
|
932
|
+
* Dedupe handoffs targeting the same destination.
|
|
933
|
+
*
|
|
934
|
+
* Parent LLMs sometimes emit multiple parallel transfer_to_<agent> tool
|
|
935
|
+
* calls for the same child agent in one model tick (e.g. "send email 1"
|
|
936
|
+
* and "send email 2" both routed to the Productivity Assistant). LangGraph
|
|
937
|
+
* would otherwise create two parallel Sends into the same subgraph, which
|
|
938
|
+
* produces duplicate handoff entries in the UI and leaves extra
|
|
939
|
+
* tool_call_ids without a clean result when the single child run finishes.
|
|
940
|
+
*
|
|
941
|
+
* Merge same-destination handoffs into one Command whose update.messages
|
|
942
|
+
* carries every original ToolMessage — the primary one has its
|
|
943
|
+
* Instructions field rebuilt from the combined set, and the rest are kept
|
|
944
|
+
* verbatim so the parent message history has a ToolMessage for every
|
|
945
|
+
* tool_call_id (LangChain requires every AI tool_call to be resolved).
|
|
946
|
+
* The child's _extractTransferContext filters all transfer ToolMessages
|
|
947
|
+
* out of the child's view, so it only ever sees the merged instructions.
|
|
948
|
+
*/
|
|
949
|
+
if (handoffCommands.length > 1) {
|
|
950
|
+
const byDestination = new Map();
|
|
951
|
+
for (const cmd of handoffCommands) {
|
|
952
|
+
const goto = cmd.goto;
|
|
953
|
+
const dest = typeof goto === 'string' ? goto : goto[0];
|
|
954
|
+
const arr = byDestination.get(dest) ?? [];
|
|
955
|
+
arr.push(cmd);
|
|
956
|
+
byDestination.set(dest, arr);
|
|
957
|
+
}
|
|
958
|
+
const hasDuplicates = Array.from(byDestination.values()).some((arr) => arr.length > 1);
|
|
959
|
+
if (hasDuplicates) {
|
|
960
|
+
const TRANSFER_INSTRUCTIONS_PATTERN = /(?:Instructions?|Context):\s*(.+)/is;
|
|
961
|
+
const mergedHandoffs = [];
|
|
962
|
+
for (const [dest, cmds] of byDestination) {
|
|
963
|
+
if (cmds.length === 1) {
|
|
964
|
+
mergedHandoffs.push(cmds[0]);
|
|
965
|
+
continue;
|
|
966
|
+
}
|
|
967
|
+
const allToolMessages = [];
|
|
968
|
+
const allOtherMessages = [];
|
|
969
|
+
for (const cmd of cmds) {
|
|
970
|
+
const upd = cmd.update;
|
|
971
|
+
for (const m of upd?.messages ?? []) {
|
|
972
|
+
if (m.getType() === 'tool') {
|
|
973
|
+
allToolMessages.push(m);
|
|
974
|
+
}
|
|
975
|
+
else {
|
|
976
|
+
allOtherMessages.push(m);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
if (allToolMessages.length === 0) {
|
|
981
|
+
mergedHandoffs.push(cmds[0]);
|
|
982
|
+
continue;
|
|
983
|
+
}
|
|
984
|
+
const primary = allToolMessages[0];
|
|
985
|
+
const primaryContent = typeof primary.content === 'string'
|
|
986
|
+
? primary.content
|
|
987
|
+
: JSON.stringify(primary.content);
|
|
988
|
+
const mergedInstructions = [];
|
|
989
|
+
for (const tm of allToolMessages) {
|
|
990
|
+
const c = typeof tm.content === 'string'
|
|
991
|
+
? tm.content
|
|
992
|
+
: JSON.stringify(tm.content);
|
|
993
|
+
const match = c.match(TRANSFER_INSTRUCTIONS_PATTERN);
|
|
994
|
+
if (match?.[1]) {
|
|
995
|
+
const instr = match[1].trim();
|
|
996
|
+
if (instr && !mergedInstructions.includes(instr)) {
|
|
997
|
+
mergedInstructions.push(instr);
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
let mergedPrimaryContent = primaryContent;
|
|
1002
|
+
if (mergedInstructions.length > 1) {
|
|
1003
|
+
const header = primaryContent
|
|
1004
|
+
.replace(TRANSFER_INSTRUCTIONS_PATTERN, '')
|
|
1005
|
+
.trimEnd();
|
|
1006
|
+
const combinedInstr = mergedInstructions
|
|
1007
|
+
.map((s, i) => `${i + 1}. ${s}`)
|
|
1008
|
+
.join('\n');
|
|
1009
|
+
mergedPrimaryContent =
|
|
1010
|
+
`${header}\nInstructions: ${combinedInstr}`.trim();
|
|
1011
|
+
}
|
|
1012
|
+
const mergedPrimary = new messages.ToolMessage({
|
|
1013
|
+
content: mergedPrimaryContent,
|
|
1014
|
+
tool_call_id: primary.tool_call_id,
|
|
1015
|
+
name: primary.name,
|
|
1016
|
+
additional_kwargs: { ...primary.additional_kwargs },
|
|
1017
|
+
});
|
|
1018
|
+
const mergedMessages = [
|
|
1019
|
+
mergedPrimary,
|
|
1020
|
+
...allToolMessages.slice(1),
|
|
1021
|
+
...allOtherMessages,
|
|
1022
|
+
];
|
|
1023
|
+
mergedHandoffs.push(new langgraph.Command({
|
|
1024
|
+
graph: langgraph.Command.PARENT,
|
|
1025
|
+
goto: dest,
|
|
1026
|
+
update: { messages: mergedMessages },
|
|
1027
|
+
}));
|
|
1028
|
+
}
|
|
1029
|
+
handoffCommands.length = 0;
|
|
1030
|
+
handoffCommands.push(...mergedHandoffs);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
916
1033
|
/**
|
|
917
1034
|
* Handle handoff commands - convert to Send objects for parallel execution
|
|
918
1035
|
* when multiple handoffs are requested
|