From 374a21c7feeb1a08a143f4b86ff9f7df4d2d4164 Mon Sep 17 00:00:00 2001 From: "ilblackdragon@gmail.com" Date: Sun, 22 Mar 2026 00:50:57 -0700 Subject: [PATCH] fix(bridge): match existing LLM request format to prevent 400 errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The LLM bridge was missing several defaults that the existing Reasoning.respond_with_tools() sets: - tool_choice: "auto" when tools are present (required by some providers) - max_tokens: 4096 (default) - temperature: 0.7 (default) - When no tools (force_text): use plain complete() instead of complete_with_tools() with empty tools array — matches existing no-tools fallback path Co-Authored-By: Claude Opus 4.6 (1M context) --- src/bridge/llm_adapter.rs | 43 ++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/src/bridge/llm_adapter.rs b/src/bridge/llm_adapter.rs index b190f0af..8b17aedd 100644 --- a/src/bridge/llm_adapter.rs +++ b/src/bridge/llm_adapter.rs @@ -58,17 +58,40 @@ impl LlmBackend for LlmBridgeAdapter { actions.iter().map(action_def_to_tool_def).collect() }; - // Build request - let mut request = ToolCompletionRequest::new(chat_messages, tools); - if let Some(max_tokens) = config.max_tokens { - request = request.with_max_tokens(max_tokens); - } - if let Some(temp) = config.temperature { - request = request.with_temperature(temp); - } - if config.force_text { - request = request.with_tool_choice("none"); + // Build request — match the existing Reasoning.respond_with_tools() defaults + let max_tokens = config.max_tokens.unwrap_or(4096); + let temperature = config.temperature.unwrap_or(0.7); + + if tools.is_empty() { + // No tools: use plain completion (matches existing no-tools path) + let mut request = crate::llm::CompletionRequest::new(chat_messages) + .with_max_tokens(max_tokens) + .with_temperature(temperature); + request.metadata = config.metadata.clone(); + + let response = provider + .complete(request) + .await + .map_err(|e| EngineError::Llm { + reason: e.to_string(), + })?; + + return Ok(LlmOutput { + response: LlmResponse::Text(response.content), + usage: TokenUsage { + input_tokens: u64::from(response.input_tokens), + output_tokens: u64::from(response.output_tokens), + cache_read_tokens: u64::from(response.cache_read_input_tokens), + cache_write_tokens: u64::from(response.cache_creation_input_tokens), + }, + }); } + + // With tools: use tool completion (matches existing tools path) + let mut request = ToolCompletionRequest::new(chat_messages, tools) + .with_max_tokens(max_tokens) + .with_temperature(temperature) + .with_tool_choice("auto"); request.metadata = config.metadata.clone(); // Call provider