AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(platform): add extended thinking execution mode to OrchestratorBlock (#12512)
Zamil Majdy37d98635520a
diff --git a/autogpt_platform/backend/backend/blocks/orchestrator.py b/autogpt_platform/backend/backend/blocks/orchestrator.pyindex f58aa30698..8f9037cc72 100644--- a/autogpt_platform/backend/backend/blocks/orchestrator.py+++ b/autogpt_platform/backend/backend/blocks/orchestrator.py@@ -1,7 +1,15 @@+import asyncioimport loggingimport re+import shutil+import tempfile+import types+import uuid as uuid_modfrom collections import Counter+from collections.abc import AsyncIterable, Sequencefrom concurrent.futures import Future+from enum import Enum+from functools import partialfrom typing import TYPE_CHECKING, Anyfrom pydantic import BaseModel@@ -17,6 +25,7 @@ from backend.blocks._base import (BlockType,)from backend.blocks.agent import AgentExecutorBlock+from backend.copilot.sdk.env import config as copilot_configfrom backend.data.dynamic_fields import (extract_base_field_name,get_dynamic_field_description,@@ -29,6 +38,13 @@ from backend.util import jsonfrom backend.util.clients import get_database_manager_async_clientfrom backend.util.prompt import MAIN_OBJECTIVE_PREFIXfrom backend.util.security import SENSITIVE_FIELD_NAMES+from backend.util.tool_call_loop import (+ LLMLoopResponse,+ LLMToolCall,+ ToolCallLoopResult,+ ToolCallResult,+ tool_call_loop,+)if TYPE_CHECKING:from backend.data.graph import Link, Node@@ -37,6 +53,24 @@ if TYPE_CHECKING:logger = logging.getLogger(__name__)+class ExecutionMode(str, Enum):+ """How the OrchestratorBlock executes tool calls.++ Designed to be provider-agnostic: new SDK integrations (e.g. Codex,+ Copilot) can be added as additional enum members without renaming+ existing ones.+ """++ BUILT_IN = "built_in"+ """Default built-in tool-call loop (supports all LLM providers)."""++ EXTENDED_THINKING = "extended_thinking"+ """Delegate the tool-calling loop to an external Agent SDK for richer+ reasoning (e.g. extended thinking, multi-step planning).+ Currently supports Anthropic-compatible providers (anthropic / open_router)+ via the Claude Agent SDK; additional SDK backends may be added later."""++class ToolInfo(BaseModel):"""Processed tool call information."""@@ -330,6 +364,10 @@ class OrchestratorBlock(Block):single-shot and iterative agent mode execution."""+ # MCP server name used by the Claude Code SDK execution mode. Keep in sync+ # with _create_graph_mcp_server and the MCP_PREFIX derivation in _execute_tools_sdk_mode.+ _SDK_MCP_SERVER_NAME = "graph_tools"+class Input(BlockSchemaInput):prompt: str = SchemaField(description="The prompt to send to the language model.",@@ -393,6 +431,16 @@ class OrchestratorBlock(Block):advanced=True,default=0,)+ execution_mode: ExecutionMode = SchemaField(+ title="Execution Mode",+ default=ExecutionMode.BUILT_IN,+ description="How tool calls are executed. "+ "'built_in' uses the default tool-call loop (all providers). "+ "'extended_thinking' delegates to an external Agent SDK for richer reasoning "+ "(currently Anthropic / OpenRouter only, requires API credentials, "+ "ignores 'Agent Mode Max Iterations').",+ advanced=True,+ )conversation_compaction: bool = SchemaField(default=True,title="Context window auto-compaction",@@ -483,6 +531,41 @@ class OrchestratorBlock(Block):"""Clean up block names for use as tool function names."""return re.sub(r"[^a-zA-Z0-9_-]", "_", s).lower()+ @staticmethod+ def _build_tool_info_from_args(+ tool_call_id: str,+ tool_name: str,+ tool_args: dict[str, Any],+ tool_def: dict[str, Any],+ ) -> ToolInfo:+ """Build a ToolInfo from parsed tool call arguments and a tool definition.++ Shared between the agent mode tool executor and the SDK MCP handler+ to avoid duplicating the field-mapping + ToolInfo construction logic.+ """+ field_mapping = tool_def["function"].get("_field_mapping", {})+ input_data: dict[str, Any] = {}+ if "function" in tool_def and "parameters" in tool_def["function"]:+ expected_args = tool_def["function"]["parameters"].get("properties", {})+ for clean_name in expected_args:+ original = field_mapping.get(clean_name, clean_name)+ input_data[original] = tool_args.get(clean_name)++ mock_tc = types.SimpleNamespace(+ id=tool_call_id,+ function=types.SimpleNamespace(+ name=tool_name,+ arguments=json.dumps(tool_args),+ ),+ )+ return ToolInfo(+ tool_call=mock_tc,+ tool_name=tool_name,+ tool_def=tool_def,+ input_data=input_data,+ field_mapping=field_mapping,+ )+@staticmethodasync def _create_block_function_signature(sink_node: "Node", links: list["Link"]@@ -737,9 +820,9 @@ class OrchestratorBlock(Block):self,credentials: llm.APIKeyCredentials,input_data: Input,- current_prompt: list[dict],+ current_prompt: list[dict[str, Any]],tool_functions: list[dict[str, Any]],- ):+ ) -> Any:"""Attempt a single LLM call with tool validation.@@ -835,7 +918,7 @@ class OrchestratorBlock(Block):return respdef _process_tool_calls(- self, response, tool_functions: list[dict[str, Any]]+ self, response: Any, tool_functions: list[dict[str, Any]]) -> list[ToolInfo]:"""Process tool calls and extract tool definitions, arguments, and input data.@@ -894,9 +977,17 @@ class OrchestratorBlock(Block):return processed_toolsdef _update_conversation(- self, prompt: list[dict], response, tool_outputs: list | None = None+ self,+ prompt: list[dict[str, Any]],+ response: Any,+ tool_outputs: list[dict[str, Any]] | None = None,):- """Update conversation history with response and tool outputs."""+ """Update conversation history with response and tool outputs.++ ``response`` must be an ``LLMResponse`` (from ``backend.blocks.llm``),+ **not** an ``LLMLoopResponse``. The method accesses ``.raw_response``+ and ``.reasoning`` attributes on the passed object.+ """converted = _convert_raw_response_to_dict(response.raw_response)if isinstance(converted, list):@@ -960,11 +1051,17 @@ class OrchestratorBlock(Block):node_exec_result = Nonefinal_input_data = None+ # Merge static defaults from the target node with LLM-provided inputs.+ # The LLM only passes values it decides to fill (e.g., "value"), but+ # static defaults like "name" on Agent Output Blocks must be included+ # so the execution record is complete for from_db() reconstruction.+ merged_input_data = {**target_node.input_default, **raw_input_data}+# Add all inputs to the execution- if not raw_input_data:+ if not merged_input_data:raise ValueError(f"Tool call has no input data: {tool_call}")- for input_name, input_value in raw_input_data.items():+ for input_name, input_value in merged_input_data.items():node_exec_result, final_input_data = await db_client.upsert_execution_input(node_id=sink_node_id,graph_exec_id=execution_params.graph_exec_id,@@ -1033,20 +1130,159 @@ class OrchestratorBlock(Block):)except Exception as e:- logger.warning(f"Tool execution with manager failed: {e}")+ logger.warning("Tool execution with manager failed: %s", e)# Return error responsereturn _create_tool_response(tool_call.id,- f"Tool execution failed: {str(e)}",+ f"Tool execution failed: {e}",responses_api=responses_api,)+ async def _agent_mode_llm_caller(+ self,+ messages: list[dict[str, Any]],+ tools: Sequence[Any],+ *,+ credentials: llm.APIKeyCredentials,+ input_data: "OrchestratorBlock.Input",+ ) -> LLMLoopResponse:+ """LLM caller callback for agent mode: wraps _attempt_llm_call_with_validation."""+ resp = await self._attempt_llm_call_with_validation(+ credentials, input_data, messages, list(tools)+ )+ tool_calls = [+ LLMToolCall(+ id=tc.id,+ name=tc.function.name,+ arguments=tc.function.arguments,+ )+ for tc in (resp.tool_calls or [])+ ]+ return LLMLoopResponse(+ response_text=resp.response,+ tool_calls=tool_calls,+ raw_response=resp,+ prompt_tokens=resp.prompt_tokens,+ completion_tokens=resp.completion_tokens,+ reasoning=resp.reasoning,+ )++ async def _agent_mode_tool_executor(+ self,+ tool_call: LLMToolCall,+ tools: Sequence[Any],+ *,+ execution_params: ExecutionParams,+ execution_processor: "ExecutionProcessor",+ use_responses_api: bool,+ ) -> ToolCallResult:+ """Tool executor callback for agent mode: wraps _execute_single_tool_with_manager."""+ # Find tool definition+ tool_def = next(+ (t for t in tools if t["function"]["name"] == tool_call.name),+ None,+ )+ if not tool_def and len(tools) == 1:+ tool_def = tools[0]+ if not tool_def:+ return ToolCallResult(+ tool_call_id=tool_call.id,+ tool_name=tool_call.name,+ content=f"Unknown tool: {tool_call.name}",+ is_error=True,+ )++ try:+ tool_args = json.loads(tool_call.arguments)+ except (ValueError, TypeError) as e:+ return ToolCallResult(+ tool_call_id=tool_call.id,+ tool_name=tool_call.name,+ content=f"Invalid JSON arguments: {e}",+ is_error=True,+ )+ tool_info = OrchestratorBlock._build_tool_info_from_args(+ tool_call_id=tool_call.id,+ tool_name=tool_call.name,+ tool_args=tool_args,+ tool_def=tool_def,+ )++ try:+ result = await self._execute_single_tool_with_manager(+ tool_info,+ execution_params,+ execution_processor,+ responses_api=use_responses_api,+ )+ # Unwrap the tool content from the provider-specific envelope.+ # _execute_single_tool_with_manager returns a full message dict+ # (e.g. {"role":"tool","content":"..."} for Chat API,+ # or {"type":"function_call_output","output":"..."} for Responses API).+ raw_content = result.get("content") or result.get("output")+ if isinstance(raw_content, list):+ # Anthropic format: [{"type":"tool_result","content":"..."}]+ parts = [+ item.get("content", "")+ for item in raw_content+ if isinstance(item, dict)+ ]+ content = (+ "\n".join(str(p) for p in parts)+ if parts+ else "Tool executed successfully"+ )+ elif raw_content is not None:+ content = str(raw_content)+ else:+ content = "Tool executed successfully"+ tool_failed = content.startswith("Tool execution failed:")+ return ToolCallResult(+ tool_call_id=tool_call.id,+ tool_name=tool_call.name,+ content=content,+ is_error=tool_failed,+ )+ except Exception as e:+ logger.error("Tool execution failed: %s", e)+ return ToolCallResult(+ tool_call_id=tool_call.id,+ tool_name=tool_call.name,+ content=f"Error: {e}",+ is_error=True,+ )++ def _agent_mode_conversation_updater(+ self,+ messages: list[dict[str, Any]],+ response: LLMLoopResponse,+ tool_results: list[ToolCallResult] | None = None,+ *,+ use_responses_api: bool = False,+ ) -> None:+ """Conversation updater callback for agent mode."""+ tool_outputs = None+ if tool_results:+ tool_outputs = [+ _create_tool_response(+ tr.tool_call_id,+ tr.content,+ responses_api=use_responses_api,+ )+ for tr in tool_results+ ]+ tool_outputs = _combine_tool_responses(tool_outputs)+ # Pass the raw LLM response (not the LLMLoopResponse wrapper) —+ # _update_conversation expects the provider response object that+ # has .raw_response and .reasoning attributes.+ self._update_conversation(messages, response.raw_response, tool_outputs)+async def _execute_tools_agent_mode(self,- input_data,- credentials,+ input_data: "OrchestratorBlock.Input",+ credentials: llm.APIKeyCredentials,tool_functions: list[dict[str, Any]],- prompt: list[dict],+ prompt: list[dict[str, Any]],graph_exec_id: str,node_id: str,node_exec_id: str,@@ -1056,12 +1292,10 @@ class OrchestratorBlock(Block):execution_context: ExecutionContext,execution_processor: "ExecutionProcessor",):- """Execute tools in agent mode with a loop until finished."""+ """Execute tools in agent mode using the shared tool-calling loop."""max_iterations = input_data.agent_mode_max_iterations- iteration = 0use_responses_api = input_data.model.metadata.provider == "openai"- # Execution parameters for tool executionexecution_params = ExecutionParams(user_id=user_id,graph_id=graph_id,@@ -1072,85 +1306,453 @@ class OrchestratorBlock(Block):execution_context=execution_context,)+ # Bind callbacks using functools.partial+ bound_llm_caller = partial(+ self._agent_mode_llm_caller,+ credentials=credentials,+ input_data=input_data,+ )+ bound_tool_executor = partial(+ self._agent_mode_tool_executor,+ execution_params=execution_params,+ execution_processor=execution_processor,+ use_responses_api=use_responses_api,+ )+ bound_conversation_updater = partial(+ self._agent_mode_conversation_updater,+ use_responses_api=use_responses_api,+ )+current_prompt = list(prompt)- while max_iterations < 0 or iteration < max_iterations:- iteration += 1- logger.debug(f"Agent mode iteration {iteration}")+ last_iter_msg = None+ if max_iterations > 0:+ last_iter_msg = (+ f"{MAIN_OBJECTIVE_PREFIX}This is your last iteration. "+ "Try to complete the task with the information you have. "+ "If you cannot fully complete it, provide a summary of what "+ "you've accomplished and what remains to be done. "+ "Prefer finishing with a clear response rather than making "+ "additional tool calls."+ )- # Prepare prompt for this iteration- iteration_prompt = list(current_prompt)+ try:+ loop_result = ToolCallLoopResult(response_text="", messages=current_prompt)+ async for loop_result in tool_call_loop(+ messages=current_prompt,+ tools=tool_functions,+ llm_call=bound_llm_caller,+ execute_tool=bound_tool_executor,+ update_conversation=bound_conversation_updater,+ max_iterations=max_iterations,+ last_iteration_message=last_iter_msg,+ ):+ # Yield intermediate tool calls so the UI can show progress.+ # Only yield conversations when there are tool calls to report;+ # the final conversation state is always emitted once after the+ # loop (line below) to avoid duplicate yields when max_iterations+ # is reached.+ if loop_result.last_tool_calls:+ yield "conversations", loop_result.messages+ for tc in loop_result.last_tool_calls:+ yield (+ "tool_calls",+ {+ "name": tc.name,+ "arguments": tc.arguments,+ },+ )+ except Exception as e:+ # Catch all errors (validation, network, API) so that the block+ # surfaces them as user-visible output instead of crashing.+ yield "error", str(e)+ return- # On the last iteration, add a special system message to encourage completion- if max_iterations > 0 and iteration == max_iterations:- last_iteration_message = {- "role": "system",- "content": f"{MAIN_OBJECTIVE_PREFIX}This is your last iteration ({iteration}/{max_iterations}). "- "Try to complete the task with the information you have. If you cannot fully complete it, "- "provide a summary of what you've accomplished and what remains to be done. "- "Prefer finishing with a clear response rather than making additional tool calls.",- }- iteration_prompt.append(last_iteration_message)+ if loop_result.finished_naturally:+ yield "finished", loop_result.response_text+ else:+ yield (+ "finished",+ (+ f"Agent mode completed after {loop_result.iterations} "+ "iterations (limit reached)"+ ),+ )+ yield "conversations", loop_result.messages- # Get LLM response- try:- response = await self._attempt_llm_call_with_validation(- credentials, input_data, iteration_prompt, tool_functions- )- except Exception as e:- yield (- "error",- f"LLM call failed in agent mode iteration {iteration}: {str(e)}",- )- return+ def _create_graph_mcp_server(+ self,+ tool_functions: list[dict[str, Any]],+ execution_params: ExecutionParams,+ execution_processor: "ExecutionProcessor",+ ):+ """Create an MCP server from graph-connected tool functions.- # Process tool calls- processed_tools = self._process_tool_calls(response, tool_functions)+ Converts the OpenAI-format tool signatures (from _create_tool_node_signatures)+ into MCP tools that execute downstream blocks via _execute_single_tool_with_manager.+ """+ from claude_agent_sdk import create_sdk_mcp_server+ from claude_agent_sdk import tool as sdk_tool++ sdk_tools = []+ for tf in tool_functions:+ func_def = tf["function"]+ tool_name = func_def["name"]+ tool_desc = func_def.get("description", "")+ tool_params = func_def.get(+ "parameters", {"type": "object", "properties": {}}+ )- # If no tool calls, we're done- if not processed_tools:- yield "finished", response.response- self._update_conversation(current_prompt, response)- yield "conversations", current_prompt- return+ # Build input schema for MCP (same as tool_adapter.py pattern).+ # Preserve additionalProperties to prevent hallucinated arguments.+ input_schema: dict[str, Any] = {+ "type": "object",+ "properties": dict(tool_params.get("properties", {})),+ "required": list(tool_params.get("required", [])),+ }+ if "additionalProperties" in tool_params:+ input_schema["additionalProperties"] = tool_params[+ "additionalProperties"+ ]- # Execute tools and collect responses- tool_outputs = []- for tool_info in processed_tools:- try:- tool_response = await self._execute_single_tool_with_manager(- tool_info,- execution_params,- execution_processor,- responses_api=use_responses_api,- )- tool_outputs.append(tool_response)- except Exception as e:- logger.error(f"Tool execution failed: {e}")- # Create error response for the tool- error_response = _create_tool_response(- tool_info.tool_call.id,- f"Error: {str(e)}",- responses_api=use_responses_api,+ def _make_handler(+ _tool_func=tf,+ _self=self,+ _exec_params=execution_params,+ _exec_processor=execution_processor,+ ):+ async def handler(args: dict[str, Any]) -> dict[str, Any]:+ func = _tool_func["function"]++ # Build ToolInfo using shared helper+ tool_info = OrchestratorBlock._build_tool_info_from_args(+ tool_call_id=f"sdk-{uuid_mod.uuid4().hex[:12]}",+ tool_name=func["name"],+ tool_args=args,+ tool_def=_tool_func,)- tool_outputs.append(error_response)- tool_outputs = _combine_tool_responses(tool_outputs)+ try:+ result = await _self._execute_single_tool_with_manager(+ tool_info, _exec_params, _exec_processor+ )+ # result is a tool response dict with "content" key+ content = result.get("content", "Tool executed successfully")+ if isinstance(content, str):+ text = content+ else:+ text = json.dumps(content)+ tool_failed = text.startswith("Tool execution failed:")+ return {+ "content": [{"type": "text", "text": text}],+ "isError": tool_failed,+ }+ except Exception as e:+ logger.error("SDK tool execution failed: %s", e)+ return {+ "content": [{"type": "text", "text": f"Error: {e}"}],+ "isError": True,+ }++ return handler++ decorated = sdk_tool(tool_name, tool_desc, input_schema)(_make_handler())+ sdk_tools.append(decorated)++ return create_sdk_mcp_server(+ name=OrchestratorBlock._SDK_MCP_SERVER_NAME,+ version="1.0.0",+ tools=sdk_tools,+ )- self._update_conversation(current_prompt, response, tool_outputs)+ async def _execute_tools_sdk_mode(+ self,+ input_data: "OrchestratorBlock.Input",+ credentials: llm.APIKeyCredentials,+ tool_functions: list[dict[str, Any]],+ prompt: list[dict[str, Any]],+ execution_params: ExecutionParams,+ execution_processor: "ExecutionProcessor",+ ):+ """Execute tools using the Claude Agent SDK.- # Yield intermediate conversation state- yield "conversations", current_prompt+ The SDK manages the conversation loop and tool calling natively.+ Graph-connected blocks are exposed as MCP tools.+ """+ from claude_agent_sdk import (+ AssistantMessage,+ ClaudeAgentOptions,+ ClaudeSDKClient,+ ResultMessage,+ TextBlock,+ ToolResultBlock,+ ToolUseBlock,+ UserMessage,+ )- # If we reach max iterations, yield the current state- if max_iterations < 0:- yield "finished", f"Agent mode completed after {iteration} iterations"- else:+ # Build MCP server from graph-connected tools+ mcp_server = self._create_graph_mcp_server(+ tool_functions, execution_params, execution_processor+ )++ # Build allowed tools list (MCP-prefixed names).+ # Derive the prefix from the class-level server name constant.+ MCP_PREFIX = f"mcp__{self._SDK_MCP_SERVER_NAME}__"+ allowed_tools = [+ f"{MCP_PREFIX}{tf['function']['name']}" for tf in tool_functions+ ]++ # Disable ALL known SDK built-in tools — only graph MCP tools available.+ # `allowed_tools` (above) is the primary restriction: the SDK only+ # enables tools explicitly listed there. This blocklist is a+ # defense-in-depth measure in case the SDK's allowlist logic changes.+ # IMPORTANT: Keep this list in sync with the Claude Agent SDK.+ # If a new built-in tool is added in a future SDK version, it will+ # still be blocked by `allowed_tools` (only MCP-prefixed names are+ # allowed), but adding it here provides an extra safety layer.+ disallowed_tools = [+ "Bash",+ "WebFetch",+ "AskUserQuestion",+ "Read",+ "Write",+ "Edit",+ "Glob",+ "Grep",+ "Task",+ "WebSearch",+ "TodoWrite",+ "NotebookEdit",+ ]++ # Build SDK env — provider-aware credential routing.+ # Extended thinking does not support subscription-mode (platform-managed credits).+ # Use *credential* provider for routing (not model metadata provider),+ # because a user may select an Anthropic model but route through OpenRouter.+ provider = credentials.provider+ if not credentials.api_key:yield (- "finished",- f"Agent mode completed after {max_iterations} iterations (limit reached)",+ "error",+ (+ "Extended thinking requires direct API credentials and does not "+ "support subscription mode. Please provide an Anthropic or OpenRouter API key."+ ),+ )+ return+ api_key = credentials.api_key.get_secret_value()+ if provider == "open_router":+ # Route through OpenRouter proxy: set base URL + auth token,+ # clear API key so the SDK uses AUTH_TOKEN instead.+ # NOTE: We use the platform's global OpenRouter base URL from+ # ChatConfig. Per-credential base URLs are not yet supported;+ # if the user's credential targets a custom proxy, the SDK will+ # still route through the platform's configured endpoint.+ or_base = (copilot_config.base_url or "https://openrouter.ai/api").rstrip(+ "/"+ )+ if or_base.endswith("/v1"):+ or_base = or_base[:-3]+ sdk_env = {+ "ANTHROPIC_BASE_URL": or_base,+ "ANTHROPIC_AUTH_TOKEN": api_key,+ "ANTHROPIC_API_KEY": "", # force CLI to use AUTH_TOKEN+ }+ else:+ # Direct Anthropic key+ sdk_env = {"ANTHROPIC_API_KEY": api_key}++ # Use an execution-specific working directory to prevent concurrent+ # SDK executions from colliding. tempfile.mkdtemp() respects TMPDIR+ # and works in containerised environments with read-only root filesystems.+ sdk_cwd = tempfile.mkdtemp(+ prefix=f"orchestrator-sdk-{execution_params.graph_exec_id}-"+ )++ response_parts: list[str] = []+ conversation: list[dict[str, Any]] = list(prompt) # Start with input prompt+ total_prompt_tokens = 0+ total_completion_tokens = 0++ sdk_error: Exception | None = None+ try:+ # Build SDK options+ options = ClaudeAgentOptions(+ system_prompt=input_data.sys_prompt or "",+ mcp_servers={self._SDK_MCP_SERVER_NAME: mcp_server},+ allowed_tools=allowed_tools,+ disallowed_tools=disallowed_tools,+ cwd=sdk_cwd,+ env=sdk_env,+ model=input_data.model.value or None,)- yield "conversations", current_prompt++ # Strip system messages from prompt — they're already passed via+ # ClaudeAgentOptions.system_prompt to avoid sending them twice.+ sdk_prompt = [p for p in prompt if p.get("role") != "system"]++ # Build user message from prompt.+ # The SDK's query() accepts a string or an async iterable of message dicts.+ # For multi-turn conversations, pass the full history as an async iterable+ # to preserve assistant replies, tool calls/results, and system messages.+ has_multi_turn = any(+ p.get("role") in ("assistant", "tool") for p in sdk_prompt+ )+ if has_multi_turn:++ async def _prompt_iter():+ for p in sdk_prompt:+ yield p++ user_message: str | AsyncIterable[dict[str, Any]] = _prompt_iter()+ else:+ # Single-turn: collapse user content into one string+ user_parts = []+ for p in sdk_prompt:+ if p.get("role") == "user" and p.get("content"):+ user_parts.append(str(p["content"]))+ user_message = (+ "\n\n".join(user_parts) if user_parts else input_data.prompt+ )++ # Run SDK client with heartbeat-safe message iteration.+ # We must NOT cancel __anext__() mid-flight — doing so corrupts+ # the SDK's internal anyio memory stream (same pattern as+ # copilot/sdk/service.py:_iter_sdk_messages).++ _HEARTBEAT_INTERVAL = 10.0 # seconds+ async with ClaudeSDKClient(options=options) as client:+ await client.query(user_message)++ msg_iter = client.receive_response().__aiter__()+ pending_task: asyncio.Task[Any] | None = None++ async def _next_msg() -> Any:+ return await msg_iter.__anext__()++ try:+ while True:+ if pending_task is None:+ pending_task = asyncio.create_task(_next_msg())++ done, _ = await asyncio.wait(+ {pending_task}, timeout=_HEARTBEAT_INTERVAL+ )++ if not done:+ # Heartbeat — SDK is still processing, keep waiting+ continue++ pending_task = None+ try:+ sdk_msg = done.pop().result()+ except StopAsyncIteration:+ break++ if isinstance(sdk_msg, AssistantMessage):+ text_parts = []+ tool_use_parts = []+ for content_block in sdk_msg.content:+ if isinstance(content_block, TextBlock):+ text_parts.append(content_block.text)+ response_parts.append(content_block.text)+ elif isinstance(content_block, ToolUseBlock):+ raw_name = getattr(content_block, "name", "unknown")+ # Strip MCP prefix for readability in+ # conversation history.+ clean_name = raw_name.removeprefix(MCP_PREFIX)+ tool_use_parts.append(+ {+ "tool": clean_name,+ "id": getattr(+ content_block, "id", "unknown"+ ),+ }+ )+ if text_parts or tool_use_parts:+ msg_content = "".join(text_parts)+ if tool_use_parts:+ tool_summary = ", ".join(+ t["tool"] for t in tool_use_parts+ )+ if msg_content:+ msg_content += f"\n[Tool calls: {tool_summary}]"+ else:+ msg_content = f"[Tool calls: {tool_summary}]"+ conversation.append(+ {+ "role": "assistant",+ "content": msg_content,+ }+ )+ elif isinstance(sdk_msg, UserMessage):+ # Capture tool results so the conversation+ # history records what each tool returned.+ result_parts: list[str] = []+ for block in getattr(sdk_msg, "content", []):+ if isinstance(block, ToolResultBlock):+ content_val = getattr(block, "content", "")+ if isinstance(content_val, list):+ # list of text blocks+ for item in content_val:+ if isinstance(item, dict):+ result_parts.append(+ item.get("text", "")+ )+ elif content_val:+ result_parts.append(str(content_val))+ if result_parts:+ conversation.append(+ {+ "role": "tool",+ "content": "\n".join(result_parts),+ }+ )+ elif isinstance(sdk_msg, ResultMessage):+ if sdk_msg.usage:+ total_prompt_tokens += getattr(+ sdk_msg.usage, "input_tokens", 0+ )+ total_completion_tokens += getattr(+ sdk_msg.usage, "output_tokens", 0+ )+ finally:+ if pending_task is not None and not pending_task.done():+ pending_task.cancel()+ try:+ await pending_task+ except (asyncio.CancelledError, StopAsyncIteration):+ pass+ except Exception as e:+ # Surface SDK errors as user-visible output instead of crashing,+ # consistent with _execute_tools_agent_mode error handling.+ # Don't return yet — fall through to merge_stats below so+ # partial token usage is always recorded.+ sdk_error = e+ finally:+ # Always record usage stats, even on error. The SDK may have+ # made LLM calls (consuming tokens) before the failure; dropping+ # those stats would under-count resource usage.+ # llm_call_count=1 is approximate; the SDK manages its own+ # multi-turn loop and only exposes aggregate usage.+ if total_prompt_tokens > 0 or total_completion_tokens > 0:+ self.merge_stats(+ NodeExecutionStats(+ input_token_count=total_prompt_tokens,+ output_token_count=total_completion_tokens,+ llm_call_count=1,+ )+ )+ # Clean up execution-specific working directory.+ shutil.rmtree(sdk_cwd, ignore_errors=True)++ if sdk_error is not None:+ yield "error", str(sdk_error)+ return++ response_text = "".join(response_parts)++ yield "finished", response_text+ yield "conversations", conversationasync def run(self,@@ -1261,6 +1863,51 @@ class OrchestratorBlock(Block):)# Execute tools based on the selected mode+ if input_data.execution_mode == ExecutionMode.EXTENDED_THINKING:+ # Validate — Claude Code SDK only works with Claude models+ provider = input_data.model.metadata.provider+ model_name = input_data.model.value+ # All Claude models have metadata.provider == "anthropic", but+ # "open_router" is included defensively in case future models+ # use a different metadata provider for the same Anthropic API.+ if provider not in ("anthropic", "open_router"):+ raise ValueError(+ f"Claude Code SDK mode requires an Anthropic-compatible "+ f"provider (got provider={provider}). "+ "Please select an Anthropic or OpenRouter provider, "+ "or switch execution mode to 'built_in'."+ )+ # Safety-net: all Claude models have .value starting with "claude-".+ # This guards against non-Claude models that happen to use the+ # "anthropic" metadata provider (if any are added in the future).+ if not model_name.startswith("claude"):+ raise ValueError(+ f"Claude Code SDK mode only supports Claude models "+ f"(got model={model_name}). "+ "Please select a Claude model, "+ "or switch execution mode to 'built_in'."+ )+ # Claude Code SDK: SDK manages conversation + tool calling+ execution_params = ExecutionParams(+ user_id=user_id,+ graph_id=graph_id,+ node_id=node_id,+ graph_version=graph_version,+ graph_exec_id=graph_exec_id,+ node_exec_id=node_exec_id,+ execution_context=execution_context,+ )+ async for result in self._execute_tools_sdk_mode(+ input_data=input_data,+ credentials=credentials,+ tool_functions=tool_functions,+ prompt=prompt,+ execution_params=execution_params,+ execution_processor=execution_processor,+ ):+ yield result+ return+if input_data.agent_mode_max_iterations != 0:# In agent mode, execute tools directly in a loop until finishedasync for result in self._execute_tools_agent_mode(diff --git a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_execution_mode.py b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_execution_mode.pynew file mode 100644index 0000000000..4d3ca2b326--- /dev/null+++ b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_execution_mode.py@@ -0,0 +1,202 @@+"""Tests for ExecutionMode enum and provider validation in the orchestrator.++Covers:+- ExecutionMode enum members exist and have stable values+- EXTENDED_THINKING provider validation (anthropic/open_router allowed, others rejected)+- EXTENDED_THINKING model-name validation (must start with "claude")+"""++from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.blocks.llm import LlmModel+from backend.blocks.orchestrator import ExecutionMode, OrchestratorBlock++# ---------------------------------------------------------------------------+# ExecutionMode enum integrity+# ---------------------------------------------------------------------------+++class TestExecutionModeEnum:+ """Guard against accidental renames or removals of enum members."""++ def test_built_in_exists(self):+ assert hasattr(ExecutionMode, "BUILT_IN")+ assert ExecutionMode.BUILT_IN.value == "built_in"++ def test_extended_thinking_exists(self):+ assert hasattr(ExecutionMode, "EXTENDED_THINKING")+ assert ExecutionMode.EXTENDED_THINKING.value == "extended_thinking"++ def test_exactly_two_members(self):+ """If a new mode is added, this test should be updated intentionally."""+ assert set(ExecutionMode.__members__.keys()) == {+ "BUILT_IN",+ "EXTENDED_THINKING",+ }++ def test_string_enum(self):+ """ExecutionMode is a str enum so it serialises cleanly to JSON."""+ assert isinstance(ExecutionMode.BUILT_IN, str)+ assert isinstance(ExecutionMode.EXTENDED_THINKING, str)++ def test_round_trip_from_value(self):+ """Constructing from the string value should return the same member."""+ assert ExecutionMode("built_in") is ExecutionMode.BUILT_IN+ assert ExecutionMode("extended_thinking") is ExecutionMode.EXTENDED_THINKING+++# ---------------------------------------------------------------------------+# Provider validation (inline in OrchestratorBlock.run)+# ---------------------------------------------------------------------------+++def _make_model_stub(provider: str, value: str):+ """Create a lightweight stub that behaves like LlmModel for validation."""+ metadata = MagicMock()+ metadata.provider = provider+ stub = MagicMock()+ stub.metadata = metadata+ stub.value = value+ return stub+++class TestExtendedThinkingProviderValidation:+ """The orchestrator rejects EXTENDED_THINKING for non-Anthropic providers."""++ def test_anthropic_provider_accepted(self):+ """provider='anthropic' + claude model should not raise."""+ model = _make_model_stub("anthropic", "claude-opus-4-6")+ provider = model.metadata.provider+ model_name = model.value+ assert provider in ("anthropic", "open_router")+ assert model_name.startswith("claude")++ def test_open_router_provider_accepted(self):+ """provider='open_router' + claude model should not raise."""+ model = _make_model_stub("open_router", "claude-sonnet-4-6")+ provider = model.metadata.provider+ model_name = model.value+ assert provider in ("anthropic", "open_router")+ assert model_name.startswith("claude")++ def test_openai_provider_rejected(self):+ """provider='openai' should be rejected for EXTENDED_THINKING."""+ model = _make_model_stub("openai", "gpt-4o")+ provider = model.metadata.provider+ assert provider not in ("anthropic", "open_router")++ def test_groq_provider_rejected(self):+ model = _make_model_stub("groq", "llama-3.3-70b-versatile")+ provider = model.metadata.provider+ assert provider not in ("anthropic", "open_router")++ def test_non_claude_model_rejected_even_if_anthropic_provider(self):+ """A hypothetical non-Claude model with provider='anthropic' is rejected."""+ model = _make_model_stub("anthropic", "not-a-claude-model")+ model_name = model.value+ assert not model_name.startswith("claude")++ def test_real_gpt4o_model_rejected(self):+ """Verify a real LlmModel enum member (GPT4O) fails the provider check."""+ model = LlmModel.GPT4O+ provider = model.metadata.provider+ assert provider not in ("anthropic", "open_router")++ def test_real_claude_model_passes(self):+ """Verify a real LlmModel enum member (CLAUDE_4_6_SONNET) passes."""+ model = LlmModel.CLAUDE_4_6_SONNET+ provider = model.metadata.provider+ model_name = model.value+ assert provider in ("anthropic", "open_router")+ assert model_name.startswith("claude")+++# ---------------------------------------------------------------------------+# Integration-style: exercise the validation branch via OrchestratorBlock.run+# ---------------------------------------------------------------------------+++def _make_input_data(model, execution_mode=ExecutionMode.EXTENDED_THINKING):+ """Build a minimal MagicMock that satisfies OrchestratorBlock.run's early path."""+ inp = MagicMock()+ inp.execution_mode = execution_mode+ inp.model = model+ inp.prompt = "test"+ inp.sys_prompt = ""+ inp.conversation_history = []+ inp.last_tool_output = None+ inp.prompt_values = {}+ return inp+++async def _collect_run_outputs(block, input_data, **kwargs):+ """Exhaust the OrchestratorBlock.run async generator, collecting outputs."""+ outputs = []+ async for item in block.run(input_data, **kwargs):+ outputs.append(item)+ return outputs+++class TestExtendedThinkingValidationRaisesInBlock:+ """Call OrchestratorBlock.run far enough to trigger the ValueError."""++ @pytest.mark.asyncio+ async def test_non_anthropic_provider_raises_valueerror(self):+ """EXTENDED_THINKING + openai provider raises ValueError."""+ block = OrchestratorBlock()+ input_data = _make_input_data(model=LlmModel.GPT4O)++ with (+ patch.object(+ block,+ "_create_tool_node_signatures",+ new_callable=AsyncMock,+ return_value=[],+ ),+ pytest.raises(ValueError, match="Anthropic-compatible"),+ ):+ await _collect_run_outputs(+ block,+ input_data,+ credentials=MagicMock(),+ graph_id="g",+ node_id="n",+ graph_exec_id="ge",+ node_exec_id="ne",+ user_id="u",+ graph_version=1,+ execution_context=MagicMock(),+ execution_processor=MagicMock(),+ )++ @pytest.mark.asyncio+ async def test_non_claude_model_with_anthropic_provider_raises(self):+ """A model with anthropic provider but non-claude name raises ValueError."""+ block = OrchestratorBlock()+ fake_model = _make_model_stub("anthropic", "not-a-claude-model")+ input_data = _make_input_data(model=fake_model)++ with (+ patch.object(+ block,+ "_create_tool_node_signatures",+ new_callable=AsyncMock,+ return_value=[],+ ),+ pytest.raises(ValueError, match="only supports Claude models"),+ ):+ await _collect_run_outputs(+ block,+ input_data,+ credentials=MagicMock(),+ graph_id="g",+ node_id="n",+ graph_exec_id="ge",+ node_exec_id="ne",+ user_id="u",+ graph_version=1,+ execution_context=MagicMock(),+ execution_processor=MagicMock(),+ )diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex 41eac0de23..e2eb67b207 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -9,11 +9,14 @@ shared tool registry as the SDK path.import asyncioimport loggingimport uuid-from collections.abc import AsyncGenerator-from typing import Any+from collections.abc import AsyncGenerator, Sequence+from dataclasses import dataclass, field+from functools import partial+from typing import Any, castimport orjsonfrom langfuse import propagate_attributes+from openai.types.chat import ChatCompletionMessageParam, ChatCompletionToolParamfrom backend.copilot.model import (ChatMessage,@@ -48,7 +51,17 @@ from backend.copilot.token_tracking import persist_and_record_usagefrom backend.copilot.tools import execute_tool, get_available_toolsfrom backend.copilot.tracking import track_user_messagefrom backend.util.exceptions import NotFoundError-from backend.util.prompt import compress_context+from backend.util.prompt import (+ compress_context,+ estimate_token_count,+ estimate_token_count_str,+)+from backend.util.tool_call_loop import (+ LLMLoopResponse,+ LLMToolCall,+ ToolCallResult,+ tool_call_loop,+)logger = logging.getLogger(__name__)@@ -59,6 +72,247 @@ _background_tasks: set[asyncio.Task[Any]] = set()_MAX_TOOL_ROUNDS = 30+@dataclass+class _BaselineStreamState:+ """Mutable state shared between the tool-call loop callbacks.++ Extracted from ``stream_chat_completion_baseline`` so that the callbacks+ can be module-level functions instead of deeply nested closures.+ """++ pending_events: list[StreamBaseResponse] = field(default_factory=list)+ assistant_text: str = ""+ text_block_id: str = field(default_factory=lambda: str(uuid.uuid4()))+ text_started: bool = False+ turn_prompt_tokens: int = 0+ turn_completion_tokens: int = 0+++async def _baseline_llm_caller(+ messages: list[dict[str, Any]],+ tools: Sequence[Any],+ *,+ state: _BaselineStreamState,+) -> LLMLoopResponse:+ """Stream an OpenAI-compatible response and collect results.++ Extracted from ``stream_chat_completion_baseline`` for readability.+ """+ state.pending_events.append(StreamStartStep())++ round_text = ""+ try:+ client = _get_openai_client()+ typed_messages = cast(list[ChatCompletionMessageParam], messages)+ if tools:+ typed_tools = cast(list[ChatCompletionToolParam], tools)+ response = await client.chat.completions.create(+ model=config.model,+ messages=typed_messages,+ tools=typed_tools,+ stream=True,+ stream_options={"include_usage": True},+ )+ else:+ response = await client.chat.completions.create(+ model=config.model,+ messages=typed_messages,+ stream=True,+ stream_options={"include_usage": True},+ )+ tool_calls_by_index: dict[int, dict[str, str]] = {}++ async for chunk in response:+ if chunk.usage:+ state.turn_prompt_tokens += chunk.usage.prompt_tokens or 0+ state.turn_completion_tokens += chunk.usage.completion_tokens or 0++ delta = chunk.choices[0].delta if chunk.choices else None+ if not delta:+ continue++ if delta.content:+ if not state.text_started:+ state.pending_events.append(StreamTextStart(id=state.text_block_id))+ state.text_started = True+ round_text += delta.content+ state.pending_events.append(+ StreamTextDelta(id=state.text_block_id, delta=delta.content)+ )++ if delta.tool_calls:+ for tc in delta.tool_calls:+ idx = tc.index+ if idx not in tool_calls_by_index:+ tool_calls_by_index[idx] = {+ "id": "",+ "name": "",+ "arguments": "",+ }+ entry = tool_calls_by_index[idx]+ if tc.id:+ entry["id"] = tc.id+ if tc.function and tc.function.name:+ entry["name"] = tc.function.name+ if tc.function and tc.function.arguments:+ entry["arguments"] += tc.function.arguments++ # Close text block+ if state.text_started:+ state.pending_events.append(StreamTextEnd(id=state.text_block_id))+ state.text_started = False+ state.text_block_id = str(uuid.uuid4())+ finally:+ # Always persist partial text so the session history stays consistent,+ # even when the stream is interrupted by an exception.+ state.assistant_text += round_text+ # Always emit StreamFinishStep to match the StreamStartStep,+ # even if an exception occurred during streaming.+ state.pending_events.append(StreamFinishStep())++ # Convert to shared format+ llm_tool_calls = [+ LLMToolCall(+ id=tc["id"],+ name=tc["name"],+ arguments=tc["arguments"] or "{}",+ )+ for tc in tool_calls_by_index.values()+ ]++ return LLMLoopResponse(+ response_text=round_text or None,+ tool_calls=llm_tool_calls,+ raw_response=None, # Not needed for baseline conversation updater+ prompt_tokens=0, # Tracked via state accumulators+ completion_tokens=0,+ )+++async def _baseline_tool_executor(+ tool_call: LLMToolCall,+ tools: Sequence[Any],+ *,+ state: _BaselineStreamState,+ user_id: str | None,+ session: ChatSession,+) -> ToolCallResult:+ """Execute a tool via the copilot tool registry.++ Extracted from ``stream_chat_completion_baseline`` for readability.+ """+ tool_call_id = tool_call.id+ tool_name = tool_call.name+ raw_args = tool_call.arguments or "{}"++ try:+ tool_args = orjson.loads(raw_args)+ except orjson.JSONDecodeError as parse_err:+ parse_error = f"Invalid JSON arguments for tool '{tool_name}': {parse_err}"+ logger.warning("[Baseline] %s", parse_error)+ state.pending_events.append(+ StreamToolOutputAvailable(+ toolCallId=tool_call_id,+ toolName=tool_name,+ output=parse_error,+ success=False,+ )+ )+ return ToolCallResult(+ tool_call_id=tool_call_id,+ tool_name=tool_name,+ content=parse_error,+ is_error=True,+ )++ state.pending_events.append(+ StreamToolInputStart(toolCallId=tool_call_id, toolName=tool_name)+ )+ state.pending_events.append(+ StreamToolInputAvailable(+ toolCallId=tool_call_id,+ toolName=tool_name,+ input=tool_args,+ )+ )++ try:+ result: StreamToolOutputAvailable = await execute_tool(+ tool_name=tool_name,+ parameters=tool_args,+ user_id=user_id,+ session=session,+ tool_call_id=tool_call_id,+ )+ state.pending_events.append(result)+ tool_output = (+ result.output if isinstance(result.output, str) else str(result.output)+ )+ return ToolCallResult(+ tool_call_id=tool_call_id,+ tool_name=tool_name,+ content=tool_output,+ )+ except Exception as e:+ error_output = f"Tool execution error: {e}"+ logger.error(+ "[Baseline] Tool %s failed: %s",+ tool_name,+ error_output,+ exc_info=True,+ )+ state.pending_events.append(+ StreamToolOutputAvailable(+ toolCallId=tool_call_id,+ toolName=tool_name,+ output=error_output,+ success=False,+ )+ )+ return ToolCallResult(+ tool_call_id=tool_call_id,+ tool_name=tool_name,+ content=error_output,+ is_error=True,+ )+++def _baseline_conversation_updater(+ messages: list[dict[str, Any]],+ response: LLMLoopResponse,+ tool_results: list[ToolCallResult] | None = None,+) -> None:+ """Update OpenAI message list with assistant response + tool results.++ Extracted from ``stream_chat_completion_baseline`` for readability.+ """+ if tool_results:+ # Build assistant message with tool_calls+ assistant_msg: dict[str, Any] = {"role": "assistant"}+ if response.response_text:+ assistant_msg["content"] = response.response_text+ assistant_msg["tool_calls"] = [+ {+ "id": tc.id,+ "type": "function",+ "function": {"name": tc.name, "arguments": tc.arguments},+ }+ for tc in response.tool_calls+ ]+ messages.append(assistant_msg)+ for tr in tool_results:+ messages.append(+ {+ "role": "tool",+ "tool_call_id": tr.tool_call_id,+ "content": tr.content,+ }+ )+ else:+ if response.response_text:+ messages.append({"role": "assistant", "content": response.response_text})++async def _update_title_async(session_id: str, message: str, user_id: str | None) -> None:@@ -219,191 +473,32 @@ async def stream_chat_completion_baseline(except Exception:logger.warning("[Baseline] Langfuse trace context setup failed")- assistant_text = ""- text_block_id = str(uuid.uuid4())- text_started = False- step_open = False- # Token usage accumulators — populated from streaming chunks- turn_prompt_tokens = 0- turn_completion_tokens = 0_stream_error = False # Track whether an error occurred during streaming- try:- for _round in range(_MAX_TOOL_ROUNDS):- # Open a new step for each LLM round- yield StreamStartStep()- step_open = True+ state = _BaselineStreamState()- # Stream a response from the model- create_kwargs: dict[str, Any] = dict(- model=config.model,- messages=openai_messages,- stream=True,- stream_options={"include_usage": True},- )- if tools:- create_kwargs["tools"] = tools- response = await _get_openai_client().chat.completions.create(**create_kwargs) # type: ignore[arg-type] # dynamic kwargs-- # Accumulate streamed response (text + tool calls)- round_text = ""- tool_calls_by_index: dict[int, dict[str, str]] = {}-- async for chunk in response:- # Capture token usage from the streaming chunk.- # OpenRouter normalises all providers into OpenAI format- # where prompt_tokens already includes cached tokens- # (unlike Anthropic's native API). Use += to sum all- # tool-call rounds since each API call is independent.- # NOTE: stream_options={"include_usage": True} is not- # universally supported — some providers (Mistral, Llama- # via OpenRouter) always return chunk.usage=None. When- # that happens, tokens stay 0 and the tiktoken fallback- # below activates. Fail-open: one round is estimated.- if chunk.usage:- turn_prompt_tokens += chunk.usage.prompt_tokens or 0- turn_completion_tokens += chunk.usage.completion_tokens or 0-- delta = chunk.choices[0].delta if chunk.choices else None- if not delta:- continue-- # Text content- if delta.content:- if not text_started:- yield StreamTextStart(id=text_block_id)- text_started = True- round_text += delta.content- yield StreamTextDelta(id=text_block_id, delta=delta.content)-- # Tool call fragments (streamed incrementally)- if delta.tool_calls:- for tc in delta.tool_calls:- idx = tc.index- if idx not in tool_calls_by_index:- tool_calls_by_index[idx] = {- "id": "",- "name": "",- "arguments": "",- }- entry = tool_calls_by_index[idx]- if tc.id:- entry["id"] = tc.id- if tc.function and tc.function.name:- entry["name"] = tc.function.name- if tc.function and tc.function.arguments:- entry["arguments"] += tc.function.arguments-- # Close text block if we had one this round- if text_started:- yield StreamTextEnd(id=text_block_id)- text_started = False- text_block_id = str(uuid.uuid4())-- # Accumulate text for session persistence- assistant_text += round_text-- # No tool calls -> model is done- if not tool_calls_by_index:- yield StreamFinishStep()- step_open = False- break-- # Close step before tool execution- yield StreamFinishStep()- step_open = False-- # Append the assistant message with tool_calls to context.- assistant_msg: dict[str, Any] = {"role": "assistant"}- if round_text:- assistant_msg["content"] = round_text- assistant_msg["tool_calls"] = [- {- "id": tc["id"],- "type": "function",- "function": {- "name": tc["name"],- "arguments": tc["arguments"] or "{}",- },- }- for tc in tool_calls_by_index.values()- ]- openai_messages.append(assistant_msg)-- # Execute each tool call and stream events- for tc in tool_calls_by_index.values():- tool_call_id = tc["id"]- tool_name = tc["name"]- raw_args = tc["arguments"] or "{}"- try:- tool_args = orjson.loads(raw_args)- except orjson.JSONDecodeError as parse_err:- parse_error = (- f"Invalid JSON arguments for tool '{tool_name}': {parse_err}"- )- logger.warning("[Baseline] %s", parse_error)- yield StreamToolOutputAvailable(- toolCallId=tool_call_id,- toolName=tool_name,- output=parse_error,- success=False,- )- openai_messages.append(- {- "role": "tool",- "tool_call_id": tool_call_id,- "content": parse_error,- }- )- continue-- yield StreamToolInputStart(toolCallId=tool_call_id, toolName=tool_name)- yield StreamToolInputAvailable(- toolCallId=tool_call_id,- toolName=tool_name,- input=tool_args,- )+ # Bind extracted module-level callbacks to this request's state/session+ # using functools.partial so they satisfy the Protocol signatures.+ _bound_llm_caller = partial(_baseline_llm_caller, state=state)+ _bound_tool_executor = partial(+ _baseline_tool_executor, state=state, user_id=user_id, session=session+ )- # Execute via shared tool registry- try:- result: StreamToolOutputAvailable = await execute_tool(- tool_name=tool_name,- parameters=tool_args,- user_id=user_id,- session=session,- tool_call_id=tool_call_id,- )- yield result- tool_output = (- result.output- if isinstance(result.output, str)- else str(result.output)- )- except Exception as e:- error_output = f"Tool execution error: {e}"- logger.error(- "[Baseline] Tool %s failed: %s",- tool_name,- error_output,- exc_info=True,- )- yield StreamToolOutputAvailable(- toolCallId=tool_call_id,- toolName=tool_name,- output=error_output,- success=False,- )- tool_output = error_output-- # Append tool result to context for next round- openai_messages.append(- {- "role": "tool",- "tool_call_id": tool_call_id,- "content": tool_output,- }- )- else:- # for-loop exhausted without break -> tool-round limit hit+ try:+ loop_result = None+ async for loop_result in tool_call_loop(+ messages=openai_messages,+ tools=tools,+ llm_call=_bound_llm_caller,+ execute_tool=_bound_tool_executor,+ update_conversation=_baseline_conversation_updater,+ max_iterations=_MAX_TOOL_ROUNDS,+ ):+ # Drain buffered events after each iteration (real-time streaming)+ for evt in state.pending_events:+ yield evt+ state.pending_events.clear()++ if loop_result and not loop_result.finished_naturally:limit_msg = (f"Exceeded {_MAX_TOOL_ROUNDS} tool-call rounds ""without a final response."@@ -418,11 +513,28 @@ async def stream_chat_completion_baseline(_stream_error = Trueerror_msg = str(e) or type(e).__name__logger.error("[Baseline] Streaming error: %s", error_msg, exc_info=True)- # Close any open text/step before emitting error- if text_started:- yield StreamTextEnd(id=text_block_id)- if step_open:- yield StreamFinishStep()+ # Close any open text block. The llm_caller's finally block+ # already appended StreamFinishStep to pending_events, so we must+ # insert StreamTextEnd *before* StreamFinishStep to preserve the+ # protocol ordering:+ # StreamStartStep -> StreamTextStart -> ...deltas... ->+ # StreamTextEnd -> StreamFinishStep+ # Appending (or yielding directly) would place it after+ # StreamFinishStep, violating the protocol.+ if state.text_started:+ # Find the last StreamFinishStep and insert before it.+ insert_pos = len(state.pending_events)+ for i in range(len(state.pending_events) - 1, -1, -1):+ if isinstance(state.pending_events[i], StreamFinishStep):+ insert_pos = i+ break+ state.pending_events.insert(+ insert_pos, StreamTextEnd(id=state.text_block_id)+ )+ # Drain pending events in correct order+ for evt in state.pending_events:+ yield evt+ state.pending_events.clear()yield StreamError(errorText=error_msg, code="baseline_error")# Still persist whatever we gotfinally:@@ -442,26 +554,21 @@ async def stream_chat_completion_baseline(# Skip fallback when an error occurred and no output was produced —# charging rate-limit tokens for completely failed requests is unfair.if (- turn_prompt_tokens == 0- and turn_completion_tokens == 0- and not (_stream_error and not assistant_text)+ state.turn_prompt_tokens == 0+ and state.turn_completion_tokens == 0+ and not (_stream_error and not state.assistant_text)):- from backend.util.prompt import (- estimate_token_count,- estimate_token_count_str,- )-- turn_prompt_tokens = max(+ state.turn_prompt_tokens = max(estimate_token_count(openai_messages, model=config.model), 1)- turn_completion_tokens = estimate_token_count_str(- assistant_text, model=config.model+ state.turn_completion_tokens = estimate_token_count_str(+ state.assistant_text, model=config.model)logger.info("[Baseline] No streaming usage reported; estimated tokens: ""prompt=%d, completion=%d",- turn_prompt_tokens,- turn_completion_tokens,+ state.turn_prompt_tokens,+ state.turn_completion_tokens,)# Persist token usage to session and record for rate limiting.@@ -471,15 +578,15 @@ async def stream_chat_completion_baseline(await persist_and_record_usage(session=session,user_id=user_id,- prompt_tokens=turn_prompt_tokens,- completion_tokens=turn_completion_tokens,+ prompt_tokens=state.turn_prompt_tokens,+ completion_tokens=state.turn_completion_tokens,log_prefix="[Baseline]",)# Persist assistant response- if assistant_text:+ if state.assistant_text:session.messages.append(- ChatMessage(role="assistant", content=assistant_text)+ ChatMessage(role="assistant", content=state.assistant_text))try:await upsert_chat_session(session)@@ -491,11 +598,11 @@ async def stream_chat_completion_baseline(# aclose() — doing so raises RuntimeError on client disconnect.# On GeneratorExit the client is already gone, so unreachable yields# are harmless; on normal completion they reach the SSE stream.- if turn_prompt_tokens > 0 or turn_completion_tokens > 0:+ if state.turn_prompt_tokens > 0 or state.turn_completion_tokens > 0:yield StreamUsage(- prompt_tokens=turn_prompt_tokens,- completion_tokens=turn_completion_tokens,- total_tokens=turn_prompt_tokens + turn_completion_tokens,+ prompt_tokens=state.turn_prompt_tokens,+ completion_tokens=state.turn_completion_tokens,+ total_tokens=state.turn_prompt_tokens + state.turn_completion_tokens,)yield StreamFinish()diff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.pyindex c53120941a..e5e11fb4d3 100644--- a/autogpt_platform/backend/backend/copilot/config.py+++ b/autogpt_platform/backend/backend/copilot/config.py@@ -178,7 +178,7 @@ class ChatConfig(BaseSettings):Single source of truth for "will the SDK route through OpenRouter?".Checks the flag *and* that ``api_key`` + a valid ``base_url`` are- present — mirrors the fallback logic in ``_build_sdk_env``.+ present — mirrors the fallback logic in ``build_sdk_env``."""if not self.use_openrouter:return Falsediff --git a/autogpt_platform/backend/backend/copilot/sdk/env.py b/autogpt_platform/backend/backend/copilot/sdk/env.pynew file mode 100644index 0000000000..ff2f128759--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/env.py@@ -0,0 +1,68 @@+"""SDK environment variable builder — importable without circular deps.++Extracted from ``service.py`` so that ``backend.blocks.orchestrator``+can reuse the same subscription / OpenRouter / direct-Anthropic logic+without pulling in the full copilot service module (which would create a+circular import through ``executor`` → ``credit`` → ``block_cost_config``).+"""++from __future__ import annotations++from backend.copilot.config import ChatConfig+from backend.copilot.sdk.subscription import validate_subscription++# ChatConfig is stateless (reads env vars) — a separate instance is fine.+# A singleton would require importing service.py which causes the circular dep+# this module was created to avoid.+config = ChatConfig()+++def build_sdk_env(+ session_id: str | None = None,+ user_id: str | None = None,+) -> dict[str, str]:+ """Build env vars for the SDK CLI subprocess.++ Three modes (checked in order):+ 1. **Subscription** — clears all keys; CLI uses ``claude login`` auth.+ 2. **Direct Anthropic** — returns ``{}``; subprocess inherits+ ``ANTHROPIC_API_KEY`` from the parent environment.+ 3. **OpenRouter** (default) — overrides base URL and auth token to+ route through the proxy, with Langfuse trace headers.+ """+ # --- Mode 1: Claude Code subscription auth ---+ if config.use_claude_code_subscription:+ validate_subscription()+ return {+ "ANTHROPIC_API_KEY": "",+ "ANTHROPIC_AUTH_TOKEN": "",+ "ANTHROPIC_BASE_URL": "",+ }++ # --- Mode 2: Direct Anthropic (no proxy hop) ---+ if not config.openrouter_active:+ return {}++ # --- Mode 3: OpenRouter proxy ---+ base = (config.base_url or "").rstrip("/")+ if base.endswith("/v1"):+ base = base[:-3]+ env: dict[str, str] = {+ "ANTHROPIC_BASE_URL": base,+ "ANTHROPIC_AUTH_TOKEN": config.api_key or "",+ "ANTHROPIC_API_KEY": "", # force CLI to use AUTH_TOKEN+ }++ # Inject broadcast headers so OpenRouter forwards traces to Langfuse.+ def _safe(v: str) -> str:+ return v.replace("\r", "").replace("\n", "").strip()[:128]++ parts = []+ if session_id:+ parts.append(f"x-session-id: {_safe(session_id)}")+ if user_id:+ parts.append(f"x-user-id: {_safe(user_id)}")+ if parts:+ env["ANTHROPIC_CUSTOM_HEADERS"] = "\n".join(parts)++ return envdiff --git a/autogpt_platform/backend/backend/copilot/sdk/env_test.py b/autogpt_platform/backend/backend/copilot/sdk/env_test.pynew file mode 100644index 0000000000..ad1a4a0118--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/env_test.py@@ -0,0 +1,242 @@+"""Tests for build_sdk_env() — the SDK subprocess environment builder."""++from unittest.mock import patch++import pytest++from backend.copilot.config import ChatConfig++# ---------------------------------------------------------------------------+# Helpers — build a ChatConfig with explicit field values so tests don't+# depend on real environment variables.+# ---------------------------------------------------------------------------+++def _make_config(**overrides) -> ChatConfig:+ """Create a ChatConfig with safe defaults, applying *overrides*."""+ defaults = {+ "use_claude_code_subscription": False,+ "use_openrouter": False,+ "api_key": None,+ "base_url": None,+ }+ defaults.update(overrides)+ return ChatConfig(**defaults)+++# ---------------------------------------------------------------------------+# Mode 1 — Subscription auth+# ---------------------------------------------------------------------------+++class TestBuildSdkEnvSubscription:+ """When ``use_claude_code_subscription`` is True, keys are blanked."""++ @patch("backend.copilot.sdk.env.validate_subscription")+ def test_returns_blanked_keys(self, mock_validate):+ """Subscription mode clears API_KEY, AUTH_TOKEN, and BASE_URL."""+ cfg = _make_config(use_claude_code_subscription=True)+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ result = build_sdk_env()++ assert result == {+ "ANTHROPIC_API_KEY": "",+ "ANTHROPIC_AUTH_TOKEN": "",+ "ANTHROPIC_BASE_URL": "",+ }+ mock_validate.assert_called_once()++ @patch(+ "backend.copilot.sdk.env.validate_subscription",+ side_effect=RuntimeError("CLI not found"),+ )+ def test_propagates_validation_error(self, mock_validate):+ """If validate_subscription fails, the error bubbles up."""+ cfg = _make_config(use_claude_code_subscription=True)+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ with pytest.raises(RuntimeError, match="CLI not found"):+ build_sdk_env()+++# ---------------------------------------------------------------------------+# Mode 2 — Direct Anthropic (no OpenRouter)+# ---------------------------------------------------------------------------+++class TestBuildSdkEnvDirectAnthropic:+ """When OpenRouter is inactive, return empty dict (inherit parent env)."""++ def test_returns_empty_dict_when_openrouter_inactive(self):+ cfg = _make_config(use_openrouter=False)+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ result = build_sdk_env()++ assert result == {}++ def test_returns_empty_dict_when_openrouter_flag_true_but_no_key(self):+ """OpenRouter flag is True but no api_key => openrouter_active is False."""+ cfg = _make_config(use_openrouter=True, base_url="https://openrouter.ai/api/v1")+ # Force api_key to None after construction (field_validator may pick up env vars)+ object.__setattr__(cfg, "api_key", None)+ assert not cfg.openrouter_active+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ result = build_sdk_env()++ assert result == {}+++# ---------------------------------------------------------------------------+# Mode 3 — OpenRouter proxy+# ---------------------------------------------------------------------------+++class TestBuildSdkEnvOpenRouter:+ """When OpenRouter is active, return proxy env vars."""++ def _openrouter_config(self, **overrides):+ defaults = {+ "use_openrouter": True,+ "api_key": "sk-or-test-key",+ "base_url": "https://openrouter.ai/api/v1",+ }+ defaults.update(overrides)+ return _make_config(**defaults)++ def test_basic_openrouter_env(self):+ cfg = self._openrouter_config()+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ result = build_sdk_env()++ assert result["ANTHROPIC_BASE_URL"] == "https://openrouter.ai/api"+ assert result["ANTHROPIC_AUTH_TOKEN"] == "sk-or-test-key"+ assert result["ANTHROPIC_API_KEY"] == ""+ assert "ANTHROPIC_CUSTOM_HEADERS" not in result++ def test_strips_trailing_v1(self):+ """The /v1 suffix is stripped from the base URL."""+ cfg = self._openrouter_config(base_url="https://openrouter.ai/api/v1")+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ result = build_sdk_env()++ assert result["ANTHROPIC_BASE_URL"] == "https://openrouter.ai/api"++ def test_strips_trailing_v1_and_slash(self):+ """Trailing slash before /v1 strip is handled."""+ cfg = self._openrouter_config(base_url="https://openrouter.ai/api/v1/")+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ result = build_sdk_env()++ # rstrip("/") first, then remove /v1+ assert result["ANTHROPIC_BASE_URL"] == "https://openrouter.ai/api"++ def test_no_v1_suffix_left_alone(self):+ """A base URL without /v1 is used as-is."""+ cfg = self._openrouter_config(base_url="https://custom-proxy.example.com")+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ result = build_sdk_env()++ assert result["ANTHROPIC_BASE_URL"] == "https://custom-proxy.example.com"++ def test_session_id_header(self):+ cfg = self._openrouter_config()+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ result = build_sdk_env(session_id="sess-123")++ assert "ANTHROPIC_CUSTOM_HEADERS" in result+ assert "x-session-id: sess-123" in result["ANTHROPIC_CUSTOM_HEADERS"]++ def test_user_id_header(self):+ cfg = self._openrouter_config()+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ result = build_sdk_env(user_id="user-456")++ assert "x-user-id: user-456" in result["ANTHROPIC_CUSTOM_HEADERS"]++ def test_both_headers(self):+ cfg = self._openrouter_config()+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ result = build_sdk_env(session_id="s1", user_id="u2")++ headers = result["ANTHROPIC_CUSTOM_HEADERS"]+ assert "x-session-id: s1" in headers+ assert "x-user-id: u2" in headers+ # They should be newline-separated+ assert "\n" in headers++ def test_header_sanitisation_strips_newlines(self):+ """Newlines/carriage-returns in header values are stripped."""+ cfg = self._openrouter_config()+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ result = build_sdk_env(session_id="bad\r\nvalue")++ header_val = result["ANTHROPIC_CUSTOM_HEADERS"]+ # The _safe helper removes \r and \n+ assert "\r" not in header_val.split(": ", 1)[1]+ assert "badvalue" in header_val++ def test_header_value_truncated_to_128_chars(self):+ """Header values are truncated to 128 characters."""+ cfg = self._openrouter_config()+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ long_id = "x" * 200+ result = build_sdk_env(session_id=long_id)++ # The value after "x-session-id: " should be at most 128 chars+ header_line = result["ANTHROPIC_CUSTOM_HEADERS"]+ value = header_line.split(": ", 1)[1]+ assert len(value) == 128+++# ---------------------------------------------------------------------------+# Mode priority+# ---------------------------------------------------------------------------+++class TestBuildSdkEnvModePriority:+ """Subscription mode takes precedence over OpenRouter."""++ @patch("backend.copilot.sdk.env.validate_subscription")+ def test_subscription_overrides_openrouter(self, mock_validate):+ cfg = _make_config(+ use_claude_code_subscription=True,+ use_openrouter=True,+ api_key="sk-or-key",+ base_url="https://openrouter.ai/api/v1",+ )+ with patch("backend.copilot.sdk.env.config", cfg):+ from backend.copilot.sdk.env import build_sdk_env++ result = build_sdk_env()++ # Should get subscription result, not OpenRouter+ assert result == {+ "ANTHROPIC_API_KEY": "",+ "ANTHROPIC_AUTH_TOKEN": "",+ "ANTHROPIC_BASE_URL": "",+ }diff --git a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.pyindex 8606d2053d..574228c653 100644--- a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py@@ -1010,7 +1010,7 @@ def _make_sdk_patches((f"{_SVC}.create_security_hooks", dict(return_value=MagicMock())),(f"{_SVC}.get_copilot_tool_names", dict(return_value=[])),(f"{_SVC}.get_sdk_disallowed_tools", dict(return_value=[])),- (f"{_SVC}._build_sdk_env", dict(return_value=None)),+ (f"{_SVC}.build_sdk_env", dict(return_value=None)),(f"{_SVC}._resolve_sdk_model", dict(return_value=None)),(f"{_SVC}.set_execution_context", {}),(diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 4ea6d48b28..13ae6db135 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -77,9 +77,9 @@ from ..tools.e2b_sandbox import get_or_create_sandbox, pause_sandbox_directfrom ..tools.sandbox import WORKSPACE_PREFIX, make_session_pathfrom ..tracking import track_user_messagefrom .compaction import CompactionTracker, filter_compaction_messages+from .env import build_sdk_env # noqa: F401 — re-export for backward compatfrom .response_adapter import SDKResponseAdapterfrom .security_hooks import create_security_hooks-from .subscription import validate_subscription as _validate_claude_code_subscriptionfrom .tool_adapter import (cancel_pending_tool_tasks,create_copilot_mcp_server,@@ -567,60 +567,6 @@ def _resolve_sdk_model() -> str | None:return model-def _build_sdk_env(- session_id: str | None = None,- user_id: str | None = None,-) -> dict[str, str]:- """Build env vars for the SDK CLI subprocess.-- Three modes (checked in order):- 1. **Subscription** — clears all keys; CLI uses `claude login` auth.- 2. **Direct Anthropic** — returns `{}`; subprocess inherits- `ANTHROPIC_API_KEY` from the parent environment.- 3. **OpenRouter** (default) — overrides base URL and auth token to- route through the proxy, with Langfuse trace headers.- """- # --- Mode 1: Claude Code subscription auth ---- if config.use_claude_code_subscription:- _validate_claude_code_subscription()- return {- "ANTHROPIC_API_KEY": "",- "ANTHROPIC_AUTH_TOKEN": "",- "ANTHROPIC_BASE_URL": "",- }-- # --- Mode 2: Direct Anthropic (no proxy hop) ---- # `openrouter_active` checks the flag *and* credential presence.- if not config.openrouter_active:- return {}-- # --- Mode 3: OpenRouter proxy ---- # Strip /v1 suffix — SDK expects the base URL without a version path.- base = (config.base_url or "").rstrip("/")- if base.endswith("/v1"):- base = base[:-3]- env: dict[str, str] = {- "ANTHROPIC_BASE_URL": base,- "ANTHROPIC_AUTH_TOKEN": config.api_key or "",- "ANTHROPIC_API_KEY": "", # force CLI to use AUTH_TOKEN- }-- # Inject broadcast headers so OpenRouter forwards traces to Langfuse.- def _safe(v: str) -> str:- """Sanitise a header value: strip newlines/whitespace and cap length."""- return v.replace("\r", "").replace("\n", "").strip()[:128]-- parts = []- if session_id:- parts.append(f"x-session-id: {_safe(session_id)}")- if user_id:- parts.append(f"x-user-id: {_safe(user_id)}")- if parts:- env["ANTHROPIC_CUSTOM_HEADERS"] = "\n".join(parts)-- return env--def _make_sdk_cwd(session_id: str) -> str:"""Create a safe, session-specific working directory path.@@ -1867,7 +1813,7 @@ async def stream_chat_completion_sdk()# Fail fast when no API credentials are available at all.- sdk_env = _build_sdk_env(session_id=session_id, user_id=user_id)+ sdk_env = build_sdk_env(session_id=session_id, user_id=user_id)if not config.api_key and not config.use_claude_code_subscription:raise RuntimeError("No API key configured. Set OPEN_ROUTER_API_KEY, "diff --git a/autogpt_platform/backend/backend/util/tool_call_loop.py b/autogpt_platform/backend/backend/util/tool_call_loop.pynew file mode 100644index 0000000000..6f3937d345--- /dev/null+++ b/autogpt_platform/backend/backend/util/tool_call_loop.py@@ -0,0 +1,281 @@+"""Shared tool-calling conversation loop.++Provides a generic, provider-agnostic conversation loop that both+the OrchestratorBlock and copilot baseline can use. The loop:++1. Calls the LLM with tool definitions+2. Extracts tool calls from the response+3. Executes tools via a caller-supplied callback+4. Appends results to the conversation+5. Repeats until no more tool calls or max iterations reached++Callers provide callbacks for LLM calling, tool execution, and+conversation updating.+"""++from __future__ import annotations++import asyncio+import logging+from collections.abc import AsyncGenerator, Sequence+from dataclasses import dataclass, field+from typing import Any, Protocol, TypedDict++logger = logging.getLogger(__name__)+++# ---------------------------------------------------------------------------+# Typed dict definitions for tool definitions and conversation messages.+# These document the expected shapes and allow callers to pass TypedDict+# subclasses (e.g. ``ChatCompletionToolParam``) without ``type: ignore``.+# ---------------------------------------------------------------------------+++class FunctionParameters(TypedDict, total=False):+ """JSON Schema object describing a tool function's parameters."""++ type: str+ properties: dict[str, Any]+ required: list[str]+ additionalProperties: bool+++class FunctionDefinition(TypedDict, total=False):+ """Function definition within a tool definition."""++ name: str+ description: str+ parameters: FunctionParameters+++class ToolDefinition(TypedDict):+ """OpenAI-compatible tool definition (function-calling format).++ Compatible with ``openai.types.chat.ChatCompletionToolParam`` and the+ dict-based tool definitions built by ``OrchestratorBlock``.+ """++ type: str+ function: FunctionDefinition+++class ConversationMessage(TypedDict, total=False):+ """A single message in the conversation (OpenAI chat format).++ Primarily for documentation; at runtime plain dicts are used because+ messages from different providers carry varying keys.+ """++ role: str+ content: str | list[Any] | None+ tool_calls: list[dict[str, Any]]+ tool_call_id: str+ name: str+++@dataclass+class ToolCallResult:+ """Result of a single tool execution."""++ tool_call_id: str+ tool_name: str+ content: str+ is_error: bool = False+++@dataclass+class LLMToolCall:+ """A tool call extracted from an LLM response."""++ id: str+ name: str+ arguments: str # JSON string+++@dataclass+class LLMLoopResponse:+ """Response from a single LLM call in the loop.++ ``raw_response`` is typed as ``Any`` intentionally: the loop itself+ never inspects it — it is an opaque pass-through that the caller's+ ``ConversationUpdater`` uses to rebuild provider-specific message+ history (OpenAI ChatCompletion, Anthropic Message, Ollama str, etc.).+ """++ response_text: str | None+ tool_calls: list[LLMToolCall]+ raw_response: Any+ prompt_tokens: int = 0+ completion_tokens: int = 0+ reasoning: str | None = None+++class LLMCaller(Protocol):+ """Protocol for LLM call functions."""++ async def __call__(+ self,+ messages: list[dict[str, Any]],+ tools: Sequence[Any],+ ) -> LLMLoopResponse: ...+++class ToolExecutor(Protocol):+ """Protocol for tool execution functions."""++ async def __call__(+ self,+ tool_call: LLMToolCall,+ tools: Sequence[Any],+ ) -> ToolCallResult: ...+++class ConversationUpdater(Protocol):+ """Protocol for updating conversation history after an LLM response."""++ def __call__(+ self,+ messages: list[dict[str, Any]],+ response: LLMLoopResponse,+ tool_results: list[ToolCallResult] | None = None,+ ) -> None: ...+++@dataclass+class ToolCallLoopResult:+ """Final result of the tool-calling loop."""++ response_text: str+ messages: list[dict[str, Any]]+ total_prompt_tokens: int = 0+ total_completion_tokens: int = 0+ iterations: int = 0+ finished_naturally: bool = True # False if hit max iterations+ last_tool_calls: list[LLMToolCall] = field(default_factory=list)+++async def tool_call_loop(+ *,+ messages: list[dict[str, Any]],+ tools: Sequence[Any],+ llm_call: LLMCaller,+ execute_tool: ToolExecutor,+ update_conversation: ConversationUpdater,+ max_iterations: int = -1,+ last_iteration_message: str | None = None,+ parallel_tool_calls: bool = True,+) -> AsyncGenerator[ToolCallLoopResult, None]:+ """Run a tool-calling conversation loop as an async generator.++ Yields a ``ToolCallLoopResult`` after each iteration so callers can+ drain buffered events (e.g. streaming text deltas) between iterations.+ The **final** yielded result has ``finished_naturally`` set and contains+ the complete response text.++ Args:+ messages: Initial conversation messages (modified in-place).+ tools: Tool function definitions (OpenAI format). Accepts any+ sequence of tool dicts, including ``ChatCompletionToolParam``.+ llm_call: Async function to call the LLM. The callback can+ perform streaming internally (e.g. accumulate text deltas+ and collect events) — it just needs to return the final+ ``LLMLoopResponse`` with extracted tool calls.+ execute_tool: Async function to execute a tool call.+ update_conversation: Function to update messages with LLM+ response and tool results.+ max_iterations: Max iterations. -1 = infinite, 0 = no loop+ (immediately yields a "max reached" result).+ last_iteration_message: Optional message to append on the last+ iteration to encourage the model to finish.+ parallel_tool_calls: If True (default), execute multiple tool+ calls from a single LLM response concurrently via+ ``asyncio.gather``. Set to False when tool calls may have+ ordering dependencies or mutate shared state.++ Yields:+ ToolCallLoopResult after each iteration. Check ``finished_naturally``+ to determine if the loop completed or is still running.+ """+ total_prompt_tokens = 0+ total_completion_tokens = 0+ iteration = 0++ while max_iterations < 0 or iteration < max_iterations:+ iteration += 1++ # On last iteration, add a hint to finish. Only copy the list+ # when the hint needs to be appended to avoid per-iteration overhead+ # on long conversations.+ is_last = (+ last_iteration_message+ and max_iterations > 0+ and iteration == max_iterations+ )+ if is_last:+ iteration_messages = list(messages)+ iteration_messages.append(+ {"role": "system", "content": last_iteration_message}+ )+ else:+ iteration_messages = messages++ # Call LLM+ response = await llm_call(iteration_messages, tools)+ total_prompt_tokens += response.prompt_tokens+ total_completion_tokens += response.completion_tokens++ # No tool calls = done+ if not response.tool_calls:+ update_conversation(messages, response)+ yield ToolCallLoopResult(+ response_text=response.response_text or "",+ messages=messages,+ total_prompt_tokens=total_prompt_tokens,+ total_completion_tokens=total_completion_tokens,+ iterations=iteration,+ finished_naturally=True,+ )+ return++ # Execute tools — parallel or sequential depending on caller preference.+ # NOTE: asyncio.gather does not cancel sibling tasks when one raises.+ # Callers should handle errors inside execute_tool (return error+ # ToolCallResult) rather than letting exceptions propagate.+ if parallel_tool_calls and len(response.tool_calls) > 1:+ # Parallel: side-effects from different tool executors (e.g.+ # streaming events appended to a shared list) may interleave+ # nondeterministically. Each event carries its own tool-call+ # identifier, so consumers must correlate by ID.+ tool_results: list[ToolCallResult] = list(+ await asyncio.gather(+ *(execute_tool(tc, tools) for tc in response.tool_calls)+ )+ )+ else:+ # Sequential: preserves ordering guarantees for callers that+ # need deterministic execution order.+ tool_results = [await execute_tool(tc, tools) for tc in response.tool_calls]++ # Update conversation with response + tool results+ update_conversation(messages, response, tool_results)++ # Yield a fresh result so callers can drain buffered events+ yield ToolCallLoopResult(+ response_text="",+ messages=messages,+ total_prompt_tokens=total_prompt_tokens,+ total_completion_tokens=total_completion_tokens,+ iterations=iteration,+ finished_naturally=False,+ last_tool_calls=list(response.tool_calls),+ )++ # Hit max iterations+ yield ToolCallLoopResult(+ response_text=f"Completed after {max_iterations} iterations (limit reached)",+ messages=messages,+ total_prompt_tokens=total_prompt_tokens,+ total_completion_tokens=total_completion_tokens,+ iterations=iteration,+ finished_naturally=False,+ )diff --git a/autogpt_platform/backend/backend/util/tool_call_loop_test.py b/autogpt_platform/backend/backend/util/tool_call_loop_test.pynew file mode 100644index 0000000000..6d03110d13--- /dev/null+++ b/autogpt_platform/backend/backend/util/tool_call_loop_test.py@@ -0,0 +1,554 @@+"""Unit tests for tool_call_loop shared abstraction.++Covers:+- Happy path with tool calls (single and multi-round)+- Final text response (no tool calls)+- Max iterations reached+- No tools scenario+- Exception propagation from tool executor+- Parallel tool execution+"""++from __future__ import annotations++import asyncio+from collections.abc import Sequence+from typing import Any++import pytest++from backend.util.tool_call_loop import (+ LLMLoopResponse,+ LLMToolCall,+ ToolCallLoopResult,+ ToolCallResult,+ tool_call_loop,+)++# ---------------------------------------------------------------------------+# Helpers+# ---------------------------------------------------------------------------++TOOL_DEFS: list[dict[str, Any]] = [+ {+ "type": "function",+ "function": {+ "name": "get_weather",+ "description": "Get weather for a city",+ "parameters": {+ "type": "object",+ "properties": {"city": {"type": "string"}},+ "required": ["city"],+ },+ },+ }+]+++def _make_response(+ text: str | None = None,+ tool_calls: list[LLMToolCall] | None = None,+ prompt_tokens: int = 10,+ completion_tokens: int = 5,+) -> LLMLoopResponse:+ return LLMLoopResponse(+ response_text=text,+ tool_calls=tool_calls or [],+ raw_response={"mock": True},+ prompt_tokens=prompt_tokens,+ completion_tokens=completion_tokens,+ )+++# ---------------------------------------------------------------------------+# Tests+# ---------------------------------------------------------------------------+++async def test_text_response_no_tool_calls():+ """LLM responds with text only -- loop should yield once and finish."""++ async def llm_call(+ messages: list[dict[str, Any]], tools: Sequence[Any]+ ) -> LLMLoopResponse:+ return _make_response(text="Hello world")++ async def execute_tool(+ tool_call: LLMToolCall, tools: Sequence[Any]+ ) -> ToolCallResult:+ raise AssertionError("Should not be called")++ def update_conversation(+ messages: list[dict[str, Any]],+ response: LLMLoopResponse,+ tool_results: list[ToolCallResult] | None = None,+ ) -> None:+ messages.append({"role": "assistant", "content": response.response_text})++ msgs: list[dict[str, Any]] = [{"role": "user", "content": "Hi"}]+ results: list[ToolCallLoopResult] = []+ async for r in tool_call_loop(+ messages=msgs,+ tools=TOOL_DEFS,+ llm_call=llm_call,+ execute_tool=execute_tool,+ update_conversation=update_conversation,+ ):+ results.append(r)++ assert len(results) == 1+ assert results[0].finished_naturally is True+ assert results[0].response_text == "Hello world"+ assert results[0].iterations == 1+ assert results[0].total_prompt_tokens == 10+ assert results[0].total_completion_tokens == 5+++async def test_single_tool_call_then_text():+ """LLM makes one tool call, then responds with text on second round."""+ call_count = 0++ async def llm_call(+ messages: list[dict[str, Any]], tools: Sequence[Any]+ ) -> LLMLoopResponse:+ nonlocal call_count+ call_count += 1+ if call_count == 1:+ return _make_response(+ tool_calls=[+ LLMToolCall(+ id="tc_1", name="get_weather", arguments='{"city":"NYC"}'+ )+ ]+ )+ return _make_response(text="It's sunny in NYC")++ async def execute_tool(+ tool_call: LLMToolCall, tools: Sequence[Any]+ ) -> ToolCallResult:+ return ToolCallResult(+ tool_call_id=tool_call.id,+ tool_name=tool_call.name,+ content='{"temp": 72}',+ )++ def update_conversation(+ messages: list[dict[str, Any]],+ response: LLMLoopResponse,+ tool_results: list[ToolCallResult] | None = None,+ ) -> None:+ messages.append({"role": "assistant", "content": response.response_text})+ if tool_results:+ for tr in tool_results:+ messages.append(+ {+ "role": "tool",+ "tool_call_id": tr.tool_call_id,+ "content": tr.content,+ }+ )++ msgs: list[dict[str, Any]] = [{"role": "user", "content": "Weather?"}]+ results: list[ToolCallLoopResult] = []+ async for r in tool_call_loop(+ messages=msgs,+ tools=TOOL_DEFS,+ llm_call=llm_call,+ execute_tool=execute_tool,+ update_conversation=update_conversation,+ ):+ results.append(r)++ # First yield: tool call iteration (not finished)+ # Second yield: text response (finished)+ assert len(results) == 2+ assert results[0].finished_naturally is False+ assert results[0].iterations == 1+ assert len(results[0].last_tool_calls) == 1+ assert results[1].finished_naturally is True+ assert results[1].response_text == "It's sunny in NYC"+ assert results[1].iterations == 2+ assert results[1].total_prompt_tokens == 20+ assert results[1].total_completion_tokens == 10+++async def test_max_iterations_reached():+ """Loop should stop after max_iterations even if LLM keeps calling tools."""++ async def llm_call(+ messages: list[dict[str, Any]], tools: Sequence[Any]+ ) -> LLMLoopResponse:+ return _make_response(+ tool_calls=[+ LLMToolCall(id="tc_x", name="get_weather", arguments='{"city":"X"}')+ ]+ )++ async def execute_tool(+ tool_call: LLMToolCall, tools: Sequence[Any]+ ) -> ToolCallResult:+ return ToolCallResult(+ tool_call_id=tool_call.id, tool_name=tool_call.name, content="result"+ )++ def update_conversation(+ messages: list[dict[str, Any]],+ response: LLMLoopResponse,+ tool_results: list[ToolCallResult] | None = None,+ ) -> None:+ pass++ msgs: list[dict[str, Any]] = [{"role": "user", "content": "Go"}]+ results: list[ToolCallLoopResult] = []+ async for r in tool_call_loop(+ messages=msgs,+ tools=TOOL_DEFS,+ llm_call=llm_call,+ execute_tool=execute_tool,+ update_conversation=update_conversation,+ max_iterations=3,+ ):+ results.append(r)++ # 3 tool-call iterations + 1 final "max reached"+ assert len(results) == 4+ for r in results[:3]:+ assert r.finished_naturally is False+ final = results[-1]+ assert final.finished_naturally is False+ assert "3 iterations" in final.response_text+ assert final.iterations == 3+++async def test_no_tools_first_response_text():+ """When LLM immediately responds with text (empty tools list), finishes."""++ async def llm_call(+ messages: list[dict[str, Any]], tools: Sequence[Any]+ ) -> LLMLoopResponse:+ return _make_response(text="No tools needed")++ async def execute_tool(+ tool_call: LLMToolCall, tools: Sequence[Any]+ ) -> ToolCallResult:+ raise AssertionError("Should not be called")++ def update_conversation(+ messages: list[dict[str, Any]],+ response: LLMLoopResponse,+ tool_results: list[ToolCallResult] | None = None,+ ) -> None:+ pass++ msgs: list[dict[str, Any]] = [{"role": "user", "content": "Hi"}]+ results: list[ToolCallLoopResult] = []+ async for r in tool_call_loop(+ messages=msgs,+ tools=[],+ llm_call=llm_call,+ execute_tool=execute_tool,+ update_conversation=update_conversation,+ ):+ results.append(r)++ assert len(results) == 1+ assert results[0].finished_naturally is True+ assert results[0].response_text == "No tools needed"+++async def test_tool_executor_exception_propagates():+ """Exception in execute_tool should propagate out of the loop."""++ async def llm_call(+ messages: list[dict[str, Any]], tools: Sequence[Any]+ ) -> LLMLoopResponse:+ return _make_response(+ tool_calls=[LLMToolCall(id="tc_err", name="get_weather", arguments="{}")]+ )++ async def execute_tool(+ tool_call: LLMToolCall, tools: Sequence[Any]+ ) -> ToolCallResult:+ raise RuntimeError("Tool execution failed!")++ def update_conversation(+ messages: list[dict[str, Any]],+ response: LLMLoopResponse,+ tool_results: list[ToolCallResult] | None = None,+ ) -> None:+ pass++ msgs: list[dict[str, Any]] = [{"role": "user", "content": "Go"}]+ with pytest.raises(RuntimeError, match="Tool execution failed!"):+ async for _ in tool_call_loop(+ messages=msgs,+ tools=TOOL_DEFS,+ llm_call=llm_call,+ execute_tool=execute_tool,+ update_conversation=update_conversation,+ ):+ pass+++async def test_parallel_tool_execution():+ """Multiple tool calls in one response should execute concurrently."""+ execution_order: list[str] = []++ async def llm_call(+ messages: list[dict[str, Any]], tools: Sequence[Any]+ ) -> LLMLoopResponse:+ if len(messages) == 1:+ return _make_response(+ tool_calls=[+ LLMToolCall(id="tc_a", name="tool_a", arguments="{}"),+ LLMToolCall(id="tc_b", name="tool_b", arguments="{}"),+ ]+ )+ return _make_response(text="Done")++ async def execute_tool(+ tool_call: LLMToolCall, tools: Sequence[Any]+ ) -> ToolCallResult:+ # tool_b starts instantly, tool_a has a small delay.+ # With parallel execution, both should overlap.+ if tool_call.name == "tool_a":+ await asyncio.sleep(0.05)+ execution_order.append(tool_call.name)+ return ToolCallResult(+ tool_call_id=tool_call.id, tool_name=tool_call.name, content="ok"+ )++ def update_conversation(+ messages: list[dict[str, Any]],+ response: LLMLoopResponse,+ tool_results: list[ToolCallResult] | None = None,+ ) -> None:+ messages.append({"role": "assistant", "content": "called tools"})+ if tool_results:+ for tr in tool_results:+ messages.append(+ {+ "role": "tool",+ "tool_call_id": tr.tool_call_id,+ "content": tr.content,+ }+ )++ msgs: list[dict[str, Any]] = [{"role": "user", "content": "Run both"}]+ async for _ in tool_call_loop(+ messages=msgs,+ tools=TOOL_DEFS,+ llm_call=llm_call,+ execute_tool=execute_tool,+ update_conversation=update_conversation,+ ):+ pass++ # With parallel execution, tool_b (no delay) finishes before tool_a+ assert execution_order == ["tool_b", "tool_a"]+++async def test_sequential_tool_execution():+ """With parallel_tool_calls=False, tools execute in order regardless of speed."""+ execution_order: list[str] = []++ async def llm_call(+ messages: list[dict[str, Any]], tools: Sequence[Any]+ ) -> LLMLoopResponse:+ if len(messages) == 1:+ return _make_response(+ tool_calls=[+ LLMToolCall(id="tc_a", name="tool_a", arguments="{}"),+ LLMToolCall(id="tc_b", name="tool_b", arguments="{}"),+ ]+ )+ return _make_response(text="Done")++ async def execute_tool(+ tool_call: LLMToolCall, tools: Sequence[Any]+ ) -> ToolCallResult:+ # tool_b would finish first if parallel, but sequential should keep order+ if tool_call.name == "tool_a":+ await asyncio.sleep(0.05)+ execution_order.append(tool_call.name)+ return ToolCallResult(+ tool_call_id=tool_call.id, tool_name=tool_call.name, content="ok"+ )++ def update_conversation(+ messages: list[dict[str, Any]],+ response: LLMLoopResponse,+ tool_results: list[ToolCallResult] | None = None,+ ) -> None:+ messages.append({"role": "assistant", "content": "called tools"})+ if tool_results:+ for tr in tool_results:+ messages.append(+ {+ "role": "tool",+ "tool_call_id": tr.tool_call_id,+ "content": tr.content,+ }+ )++ msgs: list[dict[str, Any]] = [{"role": "user", "content": "Run both"}]+ async for _ in tool_call_loop(+ messages=msgs,+ tools=TOOL_DEFS,+ llm_call=llm_call,+ execute_tool=execute_tool,+ update_conversation=update_conversation,+ parallel_tool_calls=False,+ ):+ pass++ # With sequential execution, tool_a runs first despite being slower+ assert execution_order == ["tool_a", "tool_b"]+++async def test_last_iteration_message_appended():+ """On the final iteration, last_iteration_message should be appended."""+ captured_messages: list[list[dict[str, Any]]] = []++ async def llm_call(+ messages: list[dict[str, Any]], tools: Sequence[Any]+ ) -> LLMLoopResponse:+ captured_messages.append(list(messages))+ return _make_response(+ tool_calls=[LLMToolCall(id="tc_1", name="get_weather", arguments="{}")]+ )++ async def execute_tool(+ tool_call: LLMToolCall, tools: Sequence[Any]+ ) -> ToolCallResult:+ return ToolCallResult(+ tool_call_id=tool_call.id, tool_name=tool_call.name, content="ok"+ )++ def update_conversation(+ messages: list[dict[str, Any]],+ response: LLMLoopResponse,+ tool_results: list[ToolCallResult] | None = None,+ ) -> None:+ pass++ msgs: list[dict[str, Any]] = [{"role": "user", "content": "Go"}]+ async for _ in tool_call_loop(+ messages=msgs,+ tools=TOOL_DEFS,+ llm_call=llm_call,+ execute_tool=execute_tool,+ update_conversation=update_conversation,+ max_iterations=2,+ last_iteration_message="Please finish now.",+ ):+ pass++ # First iteration: no extra message+ assert len(captured_messages[0]) == 1+ # Second (last) iteration: should have the hint appended+ last_call_msgs = captured_messages[1]+ assert any(+ m.get("role") == "system" and "Please finish now." in m.get("content", "")+ for m in last_call_msgs+ )+++async def test_token_accumulation():+ """Tokens should accumulate across iterations."""+ call_count = 0++ async def llm_call(+ messages: list[dict[str, Any]], tools: Sequence[Any]+ ) -> LLMLoopResponse:+ nonlocal call_count+ call_count += 1+ if call_count <= 2:+ return _make_response(+ tool_calls=[+ LLMToolCall(+ id=f"tc_{call_count}", name="get_weather", arguments="{}"+ )+ ],+ prompt_tokens=100,+ completion_tokens=50,+ )+ return _make_response(text="Final", prompt_tokens=100, completion_tokens=50)++ async def execute_tool(+ tool_call: LLMToolCall, tools: Sequence[Any]+ ) -> ToolCallResult:+ return ToolCallResult(+ tool_call_id=tool_call.id, tool_name=tool_call.name, content="ok"+ )++ def update_conversation(+ messages: list[dict[str, Any]],+ response: LLMLoopResponse,+ tool_results: list[ToolCallResult] | None = None,+ ) -> None:+ pass++ msgs: list[dict[str, Any]] = [{"role": "user", "content": "Go"}]+ final_result = None+ async for r in tool_call_loop(+ messages=msgs,+ tools=TOOL_DEFS,+ llm_call=llm_call,+ execute_tool=execute_tool,+ update_conversation=update_conversation,+ ):+ final_result = r++ assert final_result is not None+ assert final_result.total_prompt_tokens == 300 # 3 calls * 100+ assert final_result.total_completion_tokens == 150 # 3 calls * 50+ assert final_result.iterations == 3+++async def test_max_iterations_zero_no_loop():+ """max_iterations=0 should immediately yield a 'max reached' result without calling LLM."""++ async def llm_call(+ messages: list[dict[str, Any]], tools: Sequence[Any]+ ) -> LLMLoopResponse:+ raise AssertionError("LLM should not be called when max_iterations=0")++ async def execute_tool(+ tool_call: LLMToolCall, tools: Sequence[Any]+ ) -> ToolCallResult:+ raise AssertionError("Tool should not be called when max_iterations=0")++ def update_conversation(+ messages: list[dict[str, Any]],+ response: LLMLoopResponse,+ tool_results: list[ToolCallResult] | None = None,+ ) -> None:+ raise AssertionError("Updater should not be called when max_iterations=0")++ msgs: list[dict[str, Any]] = [{"role": "user", "content": "Go"}]+ results: list[ToolCallLoopResult] = []+ async for r in tool_call_loop(+ messages=msgs,+ tools=TOOL_DEFS,+ llm_call=llm_call,+ execute_tool=execute_tool,+ update_conversation=update_conversation,+ max_iterations=0,+ ):+ results.append(r)++ assert len(results) == 1+ assert results[0].finished_naturally is False+ assert results[0].iterations == 0+ assert "0 iterations" in results[0].response_textdiff --git a/autogpt_platform/frontend/Dockerfile b/autogpt_platform/frontend/Dockerfileindex ab2708f1f9..476a9a8ed3 100644--- a/autogpt_platform/frontend/Dockerfile+++ b/autogpt_platform/frontend/Dockerfile@@ -1,5 +1,5 @@# Base stage for both dev and prod-FROM node:21-alpine AS base+FROM node:22.22-alpine3.23 AS baseWORKDIR /appRUN corepack enableCOPY autogpt_platform/frontend/package.json autogpt_platform/frontend/pnpm-lock.yaml ./@@ -33,7 +33,7 @@ ENV NEXT_PUBLIC_SOURCEMAPS="false"RUN if [ "$NEXT_PUBLIC_PW_TEST" = "true" ]; then NEXT_PUBLIC_PW_TEST=true NODE_OPTIONS="--max-old-space-size=8192" pnpm build; else NODE_OPTIONS="--max-old-space-size=8192" pnpm build; fi# Prod stage - based on NextJS reference Dockerfile https://github.com/vercel/next.js/blob/64271354533ed16da51be5dce85f0dbd15f17517/examples/with-docker/Dockerfile-FROM node:21-alpine AS prod+FROM node:22.22-alpine3.23 AS prodENV NODE_ENV=productionENV HOSTNAME=0.0.0.0WORKDIR /appdiff --git a/docs/integrations/block-integrations/llm.md b/docs/integrations/block-integrations/llm.mdindex 4fb2f6446e..e14e278560 100644--- a/docs/integrations/block-integrations/llm.md+++ b/docs/integrations/block-integrations/llm.md@@ -731,6 +731,7 @@ _Add technical explanation here._| max_tokens | The maximum number of tokens to generate in the chat completion. | int | No || ollama_host | Ollama host for local models | str | No || agent_mode_max_iterations | Maximum iterations for agent mode. 0 = traditional mode (single LLM call, yield tool calls for external execution), -1 = infinite agent mode (loop until finished), 1+ = agent mode with max iterations limit. | int | No |+| execution_mode | How tool calls are executed. 'built_in' uses the default tool-call loop (all providers). 'extended_thinking' delegates to an external Agent SDK for richer reasoning (currently Anthropic / OpenRouter only, requires API credentials, ignores 'Agent Mode Max Iterations'). | "built_in" \| "extended_thinking" | No || conversation_compaction | Automatically compact the context window once it hits the limit | bool | No |### Outputs