fix openai tool calls, also plan bug where plan is empty.

This commit is contained in:
Jochen
2026-03-23 20:49:58 +11:00
parent 98ca094be7
commit c343dfa2f0
2 changed files with 51 additions and 13 deletions

View File

@@ -815,7 +815,7 @@ pub async fn execute_plan_read<W: UiWriter>(
} }
None => { None => {
ctx.ui_writer.print_plan_compact(None, None, false); ctx.ui_writer.print_plan_compact(None, None, false);
Ok(String::new()) Ok("No plan exists yet. Use plan_write to create one.".to_string())
} }
} }
} }

View File

@@ -415,19 +415,57 @@ impl LLMProvider for OpenAIProvider {
} }
fn convert_messages(messages: &[Message]) -> Vec<serde_json::Value> { fn convert_messages(messages: &[Message]) -> Vec<serde_json::Value> {
messages let mut result = Vec::new();
.iter() for msg in messages {
.map(|msg| { // Tool result messages: OpenAI expects role "tool" with tool_call_id
json!({ if let Some(ref tool_call_id) = msg.tool_result_id {
"role": match msg.role { result.push(json!({
MessageRole::System => "system", "role": "tool",
MessageRole::User => "user", "tool_call_id": tool_call_id,
MessageRole::Assistant => "assistant",
},
"content": msg.content, "content": msg.content,
}) }));
}) continue;
.collect() }
let role = match msg.role {
MessageRole::System => "system",
MessageRole::User => "user",
MessageRole::Assistant => "assistant",
};
// Assistant messages with tool calls
if !msg.tool_calls.is_empty() {
let tool_calls: Vec<serde_json::Value> = msg.tool_calls.iter().map(|tc| {
json!({
"id": tc.id,
"type": "function",
"function": {
"name": tc.name,
"arguments": tc.input.to_string(),
}
})
}).collect();
let mut m = json!({
"role": role,
"tool_calls": tool_calls,
});
// Include content only if non-empty (OpenAI allows null/absent content
// on assistant messages that have tool_calls)
if !msg.content.is_empty() {
m["content"] = json!(msg.content);
}
result.push(m);
continue;
}
// Regular messages
result.push(json!({
"role": role,
"content": msg.content,
}));
}
result
} }
fn convert_tools(tools: &[Tool]) -> Vec<serde_json::Value> { fn convert_tools(tools: &[Tool]) -> Vec<serde_json::Value> {