Compare commits
4 Commits
rust
...
d0be16d8e3
| Author | SHA1 | Date | |
|---|---|---|---|
| d0be16d8e3 | |||
| 83e0ad0240 | |||
| 275ce34d05 | |||
| cb5b921550 |
@@ -36,6 +36,57 @@ impl OpenAIProvider {
|
|||||||
pricing: app_config.pricing.openai.clone(),
|
pricing: app_config.pricing.openai.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// GPT-5.4 models sometimes emit parallel tool calls as a JSON block starting with
|
||||||
|
/// '{"tool_uses":' inside a text message instead of discrete function_call items.
|
||||||
|
/// This method attempts to extract and parse such tool calls.
|
||||||
|
pub fn parse_tool_uses_json(text: &str) -> Vec<crate::models::ToolCall> {
|
||||||
|
let mut calls = Vec::new();
|
||||||
|
if let Some(start) = text.find("{\"tool_uses\":") {
|
||||||
|
// Find the end of the JSON block by matching braces
|
||||||
|
let sub = &text[start..];
|
||||||
|
let mut brace_count = 0;
|
||||||
|
let mut end_idx = 0;
|
||||||
|
let mut found = false;
|
||||||
|
|
||||||
|
for (i, c) in sub.char_indices() {
|
||||||
|
if c == '{' { brace_count += 1; }
|
||||||
|
else if c == '}' {
|
||||||
|
brace_count -= 1;
|
||||||
|
if brace_count == 0 {
|
||||||
|
end_idx = i + 1;
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if found {
|
||||||
|
let json_str = &sub[..end_idx];
|
||||||
|
if let Ok(val) = serde_json::from_str::<serde_json::Value>(json_str) {
|
||||||
|
if let Some(uses) = val.get("tool_uses").and_then(|u| u.as_array()) {
|
||||||
|
for (idx, u) in uses.iter().enumerate() {
|
||||||
|
let name = u.get("recipient_name")
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.unwrap_or("unknown")
|
||||||
|
// Strip "functions." prefix if present
|
||||||
|
.replace("functions.", "");
|
||||||
|
let arguments = u.get("parameters")
|
||||||
|
.map(|v| v.to_string())
|
||||||
|
.unwrap_or_else(|| "{}".to_string());
|
||||||
|
|
||||||
|
calls.push(crate::models::ToolCall {
|
||||||
|
id: format!("call_tu_{}_{}", uuid::Uuid::new_v4().to_string()[..8].to_string(), idx),
|
||||||
|
call_type: "function".to_string(),
|
||||||
|
function: crate::models::FunctionCall { name, arguments },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
calls
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
@@ -112,10 +163,57 @@ impl super::Provider for OpenAIProvider {
|
|||||||
let messages_json = helpers::messages_to_openai_json(&request.messages).await?;
|
let messages_json = helpers::messages_to_openai_json(&request.messages).await?;
|
||||||
let mut input_parts = Vec::new();
|
let mut input_parts = Vec::new();
|
||||||
for m in &messages_json {
|
for m in &messages_json {
|
||||||
let mut role = m["role"].as_str().unwrap_or("user").to_string();
|
let role = m["role"].as_str().unwrap_or("user");
|
||||||
// Newer models (gpt-5, o1) prefer "developer" over "system"
|
|
||||||
if role == "system" {
|
if role == "tool" {
|
||||||
role = "developer".to_string();
|
input_parts.push(serde_json::json!({
|
||||||
|
"type": "function_call_output",
|
||||||
|
"call_id": m.get("tool_call_id").and_then(|v| v.as_str()).unwrap_or(""),
|
||||||
|
"output": m.get("content").and_then(|v| v.as_str()).unwrap_or("")
|
||||||
|
}));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if role == "assistant" && m.get("tool_calls").is_some() {
|
||||||
|
// Push message part if it exists
|
||||||
|
let content_val = m.get("content").cloned().unwrap_or(serde_json::json!(""));
|
||||||
|
if !content_val.is_null() && (content_val.is_array() && !content_val.as_array().unwrap().is_empty() || content_val.is_string() && !content_val.as_str().unwrap().is_empty()) {
|
||||||
|
let mut content = content_val.clone();
|
||||||
|
if let Some(text) = content.as_str() {
|
||||||
|
content = serde_json::json!([{ "type": "output_text", "text": text }]);
|
||||||
|
} else if let Some(arr) = content.as_array_mut() {
|
||||||
|
for part in arr {
|
||||||
|
if let Some(obj) = part.as_object_mut() {
|
||||||
|
if obj.get("type").and_then(|v| v.as_str()) == Some("text") {
|
||||||
|
obj.insert("type".to_string(), serde_json::json!("output_text"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
input_parts.push(serde_json::json!({
|
||||||
|
"type": "message",
|
||||||
|
"role": "assistant",
|
||||||
|
"content": content
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push tool calls as separate items
|
||||||
|
if let Some(tcs) = m.get("tool_calls").and_then(|v| v.as_array()) {
|
||||||
|
for tc in tcs {
|
||||||
|
input_parts.push(serde_json::json!({
|
||||||
|
"type": "function_call",
|
||||||
|
"call_id": tc["id"],
|
||||||
|
"name": tc["function"]["name"],
|
||||||
|
"arguments": tc["function"]["arguments"]
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut mapped_role = role.to_string();
|
||||||
|
if mapped_role == "system" {
|
||||||
|
mapped_role = "developer".to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut content = m.get("content").cloned().unwrap_or(serde_json::json!([]));
|
let mut content = m.get("content").cloned().unwrap_or(serde_json::json!([]));
|
||||||
@@ -127,12 +225,11 @@ impl super::Provider for OpenAIProvider {
|
|||||||
if let Some(t) = part_obj.get("type").and_then(|v| v.as_str()) {
|
if let Some(t) = part_obj.get("type").and_then(|v| v.as_str()) {
|
||||||
match t {
|
match t {
|
||||||
"text" => {
|
"text" => {
|
||||||
let new_type = if role == "assistant" { "output_text" } else { "input_text" };
|
let new_type = if mapped_role == "assistant" { "output_text" } else { "input_text" };
|
||||||
part_obj.insert("type".to_string(), serde_json::json!(new_type));
|
part_obj.insert("type".to_string(), serde_json::json!(new_type));
|
||||||
}
|
}
|
||||||
"image_url" => {
|
"image_url" => {
|
||||||
// Assistant typically doesn't have image_url in history this way, but for safety:
|
let new_type = if mapped_role == "assistant" { "output_image" } else { "input_image" };
|
||||||
let new_type = if role == "assistant" { "output_image" } else { "input_image" };
|
|
||||||
part_obj.insert("type".to_string(), serde_json::json!(new_type));
|
part_obj.insert("type".to_string(), serde_json::json!(new_type));
|
||||||
if let Some(img_url) = part_obj.remove("image_url") {
|
if let Some(img_url) = part_obj.remove("image_url") {
|
||||||
part_obj.insert("image".to_string(), img_url);
|
part_obj.insert("image".to_string(), img_url);
|
||||||
@@ -144,14 +241,20 @@ impl super::Provider for OpenAIProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if let Some(text) = content.as_str() {
|
} else if let Some(text) = content.as_str() {
|
||||||
let new_type = if role == "assistant" { "output_text" } else { "input_text" };
|
// If it's just a string, send it as a string instead of an array of objects
|
||||||
content = serde_json::json!([{ "type": new_type, "text": text }]);
|
// as it's safer for standard conversational messages.
|
||||||
|
content = serde_json::json!(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
input_parts.push(serde_json::json!({
|
let mut msg_item = serde_json::json!({
|
||||||
"role": role,
|
"type": "message",
|
||||||
|
"role": mapped_role,
|
||||||
"content": content
|
"content": content
|
||||||
}));
|
});
|
||||||
|
if let Some(name) = m.get("name") {
|
||||||
|
msg_item["name"] = name.clone();
|
||||||
|
}
|
||||||
|
input_parts.push(msg_item);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut body = serde_json::json!({
|
let mut body = serde_json::json!({
|
||||||
@@ -174,7 +277,33 @@ impl super::Provider for OpenAIProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(tools) = &request.tools {
|
if let Some(tools) = &request.tools {
|
||||||
body["tools"] = serde_json::json!(tools);
|
let flattened: Vec<serde_json::Value> = tools.iter().map(|t| {
|
||||||
|
let mut obj = serde_json::json!({
|
||||||
|
"type": t.tool_type,
|
||||||
|
"name": t.function.name,
|
||||||
|
});
|
||||||
|
if let Some(desc) = &t.function.description {
|
||||||
|
obj["description"] = serde_json::json!(desc);
|
||||||
|
}
|
||||||
|
if let Some(params) = &t.function.parameters {
|
||||||
|
obj["parameters"] = params.clone();
|
||||||
|
}
|
||||||
|
obj
|
||||||
|
}).collect();
|
||||||
|
body["tools"] = serde_json::json!(flattened);
|
||||||
|
}
|
||||||
|
if let Some(tool_choice) = &request.tool_choice {
|
||||||
|
match tool_choice {
|
||||||
|
crate::models::ToolChoice::Mode(mode) => {
|
||||||
|
body["tool_choice"] = serde_json::json!(mode);
|
||||||
|
}
|
||||||
|
crate::models::ToolChoice::Specific(specific) => {
|
||||||
|
body["tool_choice"] = serde_json::json!({
|
||||||
|
"type": specific.choice_type,
|
||||||
|
"name": specific.function.name,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let resp = self
|
let resp = self
|
||||||
@@ -200,18 +329,43 @@ impl super::Provider for OpenAIProvider {
|
|||||||
|
|
||||||
// Normalize Responses API output into ProviderResponse
|
// Normalize Responses API output into ProviderResponse
|
||||||
let mut content_text = String::new();
|
let mut content_text = String::new();
|
||||||
|
let mut tool_calls = Vec::new();
|
||||||
if let Some(output) = resp_json.get("output").and_then(|o| o.as_array()) {
|
if let Some(output) = resp_json.get("output").and_then(|o| o.as_array()) {
|
||||||
for out in output {
|
for out in output {
|
||||||
|
let item_type = out.get("type").and_then(|v| v.as_str()).unwrap_or("");
|
||||||
|
match item_type {
|
||||||
|
"message" => {
|
||||||
if let Some(contents) = out.get("content").and_then(|c| c.as_array()) {
|
if let Some(contents) = out.get("content").and_then(|c| c.as_array()) {
|
||||||
for item in contents {
|
for item in contents {
|
||||||
if let Some(text) = item.get("text").and_then(|t| t.as_str()) {
|
if let Some(text) = item.get("text").and_then(|t| t.as_str()) {
|
||||||
if !content_text.is_empty() { content_text.push_str("\n"); }
|
if !content_text.is_empty() { content_text.push_str("\n"); }
|
||||||
content_text.push_str(text);
|
content_text.push_str(text);
|
||||||
} else if let Some(parts) = item.get("parts").and_then(|p| p.as_array()) {
|
}
|
||||||
for p in parts {
|
}
|
||||||
if let Some(t) = p.as_str() {
|
}
|
||||||
|
}
|
||||||
|
"function_call" => {
|
||||||
|
let id = out.get("call_id")
|
||||||
|
.or_else(|| out.get("item_id"))
|
||||||
|
.or_else(|| out.get("id"))
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.unwrap_or("")
|
||||||
|
.to_string();
|
||||||
|
let name = out.get("name").and_then(|v| v.as_str()).unwrap_or("").to_string();
|
||||||
|
let arguments = out.get("arguments").and_then(|v| v.as_str()).unwrap_or("").to_string();
|
||||||
|
tool_calls.push(crate::models::ToolCall {
|
||||||
|
id,
|
||||||
|
call_type: "function".to_string(),
|
||||||
|
function: crate::models::FunctionCall { name, arguments },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
// Fallback for older/nested structure
|
||||||
|
if let Some(contents) = out.get("content").and_then(|c| c.as_array()) {
|
||||||
|
for item in contents {
|
||||||
|
if let Some(text) = item.get("text").and_then(|t| t.as_str()) {
|
||||||
if !content_text.is_empty() { content_text.push_str("\n"); }
|
if !content_text.is_empty() { content_text.push_str("\n"); }
|
||||||
content_text.push_str(t);
|
content_text.push_str(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -241,10 +395,20 @@ impl super::Provider for OpenAIProvider {
|
|||||||
let completion_tokens = resp_json.get("usage").and_then(|u| u.get("completion_tokens")).and_then(|v| v.as_u64()).unwrap_or(0) as u32;
|
let completion_tokens = resp_json.get("usage").and_then(|u| u.get("completion_tokens")).and_then(|v| v.as_u64()).unwrap_or(0) as u32;
|
||||||
let total_tokens = resp_json.get("usage").and_then(|u| u.get("total_tokens")).and_then(|v| v.as_u64()).unwrap_or(0) as u32;
|
let total_tokens = resp_json.get("usage").and_then(|u| u.get("total_tokens")).and_then(|v| v.as_u64()).unwrap_or(0) as u32;
|
||||||
|
|
||||||
|
// GPT-5.4 parallel tool calls might be embedded in content_text as a JSON block
|
||||||
|
let embedded_calls = Self::parse_tool_uses_json(&content_text);
|
||||||
|
if !embedded_calls.is_empty() {
|
||||||
|
// Strip the JSON part from content_text to keep it clean
|
||||||
|
if let Some(start) = content_text.find("{\"tool_uses\":") {
|
||||||
|
content_text = content_text[..start].to_string();
|
||||||
|
}
|
||||||
|
tool_calls.extend(embedded_calls);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(ProviderResponse {
|
Ok(ProviderResponse {
|
||||||
content: content_text,
|
content: content_text,
|
||||||
reasoning_content: None,
|
reasoning_content: None,
|
||||||
tool_calls: None,
|
tool_calls: if tool_calls.is_empty() { None } else { Some(tool_calls) },
|
||||||
prompt_tokens,
|
prompt_tokens,
|
||||||
completion_tokens,
|
completion_tokens,
|
||||||
reasoning_tokens: 0,
|
reasoning_tokens: 0,
|
||||||
@@ -379,10 +543,57 @@ impl super::Provider for OpenAIProvider {
|
|||||||
let messages_json = helpers::messages_to_openai_json(&request.messages).await?;
|
let messages_json = helpers::messages_to_openai_json(&request.messages).await?;
|
||||||
let mut input_parts = Vec::new();
|
let mut input_parts = Vec::new();
|
||||||
for m in &messages_json {
|
for m in &messages_json {
|
||||||
let mut role = m["role"].as_str().unwrap_or("user").to_string();
|
let role = m["role"].as_str().unwrap_or("user");
|
||||||
// Newer models (gpt-5, o1) prefer "developer" over "system"
|
|
||||||
if role == "system" {
|
if role == "tool" {
|
||||||
role = "developer".to_string();
|
input_parts.push(serde_json::json!({
|
||||||
|
"type": "function_call_output",
|
||||||
|
"call_id": m.get("tool_call_id").and_then(|v| v.as_str()).unwrap_or(""),
|
||||||
|
"output": m.get("content").and_then(|v| v.as_str()).unwrap_or("")
|
||||||
|
}));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if role == "assistant" && m.get("tool_calls").is_some() {
|
||||||
|
// Push message part if it exists
|
||||||
|
let content_val = m.get("content").cloned().unwrap_or(serde_json::json!(""));
|
||||||
|
if !content_val.is_null() && (content_val.is_array() && !content_val.as_array().unwrap().is_empty() || content_val.is_string() && !content_val.as_str().unwrap().is_empty()) {
|
||||||
|
let mut content = content_val.clone();
|
||||||
|
if let Some(text) = content.as_str() {
|
||||||
|
content = serde_json::json!([{ "type": "output_text", "text": text }]);
|
||||||
|
} else if let Some(arr) = content.as_array_mut() {
|
||||||
|
for part in arr {
|
||||||
|
if let Some(obj) = part.as_object_mut() {
|
||||||
|
if obj.get("type").and_then(|v| v.as_str()) == Some("text") {
|
||||||
|
obj.insert("type".to_string(), serde_json::json!("output_text"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
input_parts.push(serde_json::json!({
|
||||||
|
"type": "message",
|
||||||
|
"role": "assistant",
|
||||||
|
"content": content
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push tool calls as separate items
|
||||||
|
if let Some(tcs) = m.get("tool_calls").and_then(|v| v.as_array()) {
|
||||||
|
for tc in tcs {
|
||||||
|
input_parts.push(serde_json::json!({
|
||||||
|
"type": "function_call",
|
||||||
|
"call_id": tc["id"],
|
||||||
|
"name": tc["function"]["name"],
|
||||||
|
"arguments": tc["function"]["arguments"]
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut mapped_role = role.to_string();
|
||||||
|
if mapped_role == "system" {
|
||||||
|
mapped_role = "developer".to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut content = m.get("content").cloned().unwrap_or(serde_json::json!([]));
|
let mut content = m.get("content").cloned().unwrap_or(serde_json::json!([]));
|
||||||
@@ -394,12 +605,11 @@ impl super::Provider for OpenAIProvider {
|
|||||||
if let Some(t) = part_obj.get("type").and_then(|v| v.as_str()) {
|
if let Some(t) = part_obj.get("type").and_then(|v| v.as_str()) {
|
||||||
match t {
|
match t {
|
||||||
"text" => {
|
"text" => {
|
||||||
let new_type = if role == "assistant" { "output_text" } else { "input_text" };
|
let new_type = if mapped_role == "assistant" { "output_text" } else { "input_text" };
|
||||||
part_obj.insert("type".to_string(), serde_json::json!(new_type));
|
part_obj.insert("type".to_string(), serde_json::json!(new_type));
|
||||||
}
|
}
|
||||||
"image_url" => {
|
"image_url" => {
|
||||||
// Assistant typically doesn't have image_url in history this way, but for safety:
|
let new_type = if mapped_role == "assistant" { "output_image" } else { "input_image" };
|
||||||
let new_type = if role == "assistant" { "output_image" } else { "input_image" };
|
|
||||||
part_obj.insert("type".to_string(), serde_json::json!(new_type));
|
part_obj.insert("type".to_string(), serde_json::json!(new_type));
|
||||||
if let Some(img_url) = part_obj.remove("image_url") {
|
if let Some(img_url) = part_obj.remove("image_url") {
|
||||||
part_obj.insert("image".to_string(), img_url);
|
part_obj.insert("image".to_string(), img_url);
|
||||||
@@ -411,14 +621,20 @@ impl super::Provider for OpenAIProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if let Some(text) = content.as_str() {
|
} else if let Some(text) = content.as_str() {
|
||||||
let new_type = if role == "assistant" { "output_text" } else { "input_text" };
|
// If it's just a string, send it as a string instead of an array of objects
|
||||||
content = serde_json::json!([{ "type": new_type, "text": text }]);
|
// as it's safer for standard conversational messages.
|
||||||
|
content = serde_json::json!(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
input_parts.push(serde_json::json!({
|
let mut msg_item = serde_json::json!({
|
||||||
"role": role,
|
"type": "message",
|
||||||
|
"role": mapped_role,
|
||||||
"content": content
|
"content": content
|
||||||
}));
|
});
|
||||||
|
if let Some(name) = m.get("name") {
|
||||||
|
msg_item["name"] = name.clone();
|
||||||
|
}
|
||||||
|
input_parts.push(msg_item);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut body = serde_json::json!({
|
let mut body = serde_json::json!({
|
||||||
@@ -441,6 +657,36 @@ impl super::Provider for OpenAIProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(tools) = &request.tools {
|
||||||
|
let flattened: Vec<serde_json::Value> = tools.iter().map(|t| {
|
||||||
|
let mut obj = serde_json::json!({
|
||||||
|
"type": t.tool_type,
|
||||||
|
"name": t.function.name,
|
||||||
|
});
|
||||||
|
if let Some(desc) = &t.function.description {
|
||||||
|
obj["description"] = serde_json::json!(desc);
|
||||||
|
}
|
||||||
|
if let Some(params) = &t.function.parameters {
|
||||||
|
obj["parameters"] = params.clone();
|
||||||
|
}
|
||||||
|
obj
|
||||||
|
}).collect();
|
||||||
|
body["tools"] = serde_json::json!(flattened);
|
||||||
|
}
|
||||||
|
if let Some(tool_choice) = &request.tool_choice {
|
||||||
|
match tool_choice {
|
||||||
|
crate::models::ToolChoice::Mode(mode) => {
|
||||||
|
body["tool_choice"] = serde_json::json!(mode);
|
||||||
|
}
|
||||||
|
crate::models::ToolChoice::Specific(specific) => {
|
||||||
|
body["tool_choice"] = serde_json::json!({
|
||||||
|
"type": specific.choice_type,
|
||||||
|
"name": specific.function.name,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let url = format!("{}/responses", self.config.base_url);
|
let url = format!("{}/responses", self.config.base_url);
|
||||||
let api_key = self.api_key.clone();
|
let api_key = self.api_key.clone();
|
||||||
let model = request.model.clone();
|
let model = request.model.clone();
|
||||||
@@ -475,6 +721,7 @@ impl super::Provider for OpenAIProvider {
|
|||||||
// Responses API specific parsing for streaming
|
// Responses API specific parsing for streaming
|
||||||
let mut content = String::new();
|
let mut content = String::new();
|
||||||
let mut finish_reason = None;
|
let mut finish_reason = None;
|
||||||
|
let mut tool_calls = None;
|
||||||
|
|
||||||
let event_type = chunk.get("type").and_then(|v| v.as_str()).unwrap_or("");
|
let event_type = chunk.get("type").and_then(|v| v.as_str()).unwrap_or("");
|
||||||
|
|
||||||
@@ -484,15 +731,35 @@ impl super::Provider for OpenAIProvider {
|
|||||||
content.push_str(delta);
|
content.push_str(delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"response.output_text.done" => {
|
"response.item.delta" => {
|
||||||
if let Some(text) = chunk.get("text").and_then(|v| v.as_str()) {
|
if let Some(delta) = chunk.get("delta") {
|
||||||
// Some implementations send the full text at the end
|
let t = delta.get("type").and_then(|v| v.as_str()).unwrap_or("");
|
||||||
// We usually prefer deltas, but if we haven't seen them, this is the fallback.
|
if t == "function_call" {
|
||||||
// However, if we're already yielding deltas, we might not want this.
|
let call_id = delta.get("call_id")
|
||||||
// For now, let's just use it as a signal that we're done.
|
.or_else(|| chunk.get("item_id"))
|
||||||
finish_reason = Some("stop".to_string());
|
.and_then(|v| v.as_str());
|
||||||
|
let name = delta.get("name").and_then(|v| v.as_str());
|
||||||
|
let arguments = delta.get("arguments").and_then(|v| v.as_str());
|
||||||
|
|
||||||
|
tool_calls = Some(vec![crate::models::ToolCallDelta {
|
||||||
|
index: chunk.get("output_index").and_then(|v| v.as_u64()).unwrap_or(0) as u32,
|
||||||
|
id: call_id.map(|s| s.to_string()),
|
||||||
|
call_type: Some("function".to_string()),
|
||||||
|
function: Some(crate::models::FunctionCallDelta {
|
||||||
|
name: name.map(|s| s.to_string()),
|
||||||
|
arguments: arguments.map(|s| s.to_string()),
|
||||||
|
}),
|
||||||
|
}]);
|
||||||
|
} else if t == "message" {
|
||||||
|
if let Some(text) = delta.get("text").and_then(|v| v.as_str()) {
|
||||||
|
content.push_str(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"response.output_text.done" | "response.item.done" => {
|
||||||
|
finish_reason = Some("stop".to_string());
|
||||||
|
}
|
||||||
"response.done" => {
|
"response.done" => {
|
||||||
finish_reason = Some("stop".to_string());
|
finish_reason = Some("stop".to_string());
|
||||||
}
|
}
|
||||||
@@ -514,12 +781,41 @@ impl super::Provider for OpenAIProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !content.is_empty() || finish_reason.is_some() {
|
// GPT-5.4 parallel tool calls might be embedded in content as a JSON block
|
||||||
|
let embedded_calls = Self::parse_tool_uses_json(&content);
|
||||||
|
|
||||||
|
if !embedded_calls.is_empty() {
|
||||||
|
// Strip the JSON part from content to keep it clean
|
||||||
|
if let Some(start) = content.find("{\"tool_uses\":") {
|
||||||
|
content = content[..start].to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert ToolCall to ToolCallDelta for streaming
|
||||||
|
let deltas: Vec<crate::models::ToolCallDelta> = embedded_calls.into_iter().enumerate().map(|(idx, tc)| {
|
||||||
|
crate::models::ToolCallDelta {
|
||||||
|
index: idx as u32,
|
||||||
|
id: Some(tc.id),
|
||||||
|
call_type: Some("function".to_string()),
|
||||||
|
function: Some(crate::models::FunctionCallDelta {
|
||||||
|
name: Some(tc.function.name),
|
||||||
|
arguments: Some(tc.function.arguments),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
if let Some(ref mut existing) = tool_calls {
|
||||||
|
existing.extend(deltas);
|
||||||
|
} else {
|
||||||
|
tool_calls = Some(deltas);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !content.is_empty() || finish_reason.is_some() || tool_calls.is_some() {
|
||||||
yield ProviderStreamChunk {
|
yield ProviderStreamChunk {
|
||||||
content,
|
content,
|
||||||
reasoning_content: None,
|
reasoning_content: None,
|
||||||
finish_reason,
|
finish_reason,
|
||||||
tool_calls: None,
|
tool_calls,
|
||||||
model: model.clone(),
|
model: model.clone(),
|
||||||
usage: None,
|
usage: None,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user