diff --git a/src/providers/openai.rs b/src/providers/openai.rs index ecb6300f..240ea6b1 100644 --- a/src/providers/openai.rs +++ b/src/providers/openai.rs @@ -731,6 +731,8 @@ impl super::Provider for OpenAIProvider { let mut es = es; let mut content_buffer = String::new(); let mut has_tool_calls = false; + let mut tool_index_map = std::collections::HashMap::::new(); + let mut next_tool_index = 0u32; while let Some(event) = es.next().await { match event { @@ -765,8 +767,15 @@ impl super::Provider for OpenAIProvider { let call_id = item.get("call_id").and_then(|v| v.as_str()); let name = item.get("name").and_then(|v| v.as_str()); + let out_idx = chunk.get("output_index").and_then(|v| v.as_u64()).unwrap_or(0) as u32; + let tc_idx = *tool_index_map.entry(out_idx).or_insert_with(|| { + let i = next_tool_index; + next_tool_index += 1; + i + }); + tool_calls = Some(vec![crate::models::ToolCallDelta { - index: chunk.get("output_index").and_then(|v| v.as_u64()).unwrap_or(0) as u32, + index: tc_idx, id: call_id.map(|s| s.to_string()), call_type: Some("function".to_string()), function: Some(crate::models::FunctionCallDelta { @@ -780,8 +789,16 @@ impl super::Provider for OpenAIProvider { "response.function_call_arguments.delta" => { if let Some(delta) = chunk.get("delta").and_then(|v| v.as_str()) { has_tool_calls = true; + + let out_idx = chunk.get("output_index").and_then(|v| v.as_u64()).unwrap_or(0) as u32; + let tc_idx = *tool_index_map.entry(out_idx).or_insert_with(|| { + let i = next_tool_index; + next_tool_index += 1; + i + }); + tool_calls = Some(vec![crate::models::ToolCallDelta { - index: chunk.get("output_index").and_then(|v| v.as_u64()).unwrap_or(0) as u32, + index: tc_idx, id: None, call_type: None, function: Some(crate::models::FunctionCallDelta {