adressing reviews

This commit is contained in:
Ahmed Ibrahim
2025-07-14 15:05:49 -07:00
parent 4aff7927fc
commit dd879fedb1
5 changed files with 100 additions and 91 deletions
+86 -77
View File
@@ -475,8 +475,10 @@ mod tests {
use crate::models::LocalShellAction;
use crate::models::LocalShellExecAction;
use crate::models::LocalShellStatus;
use crate::openai_tools::create_tools_json_for_chat_completions_api;
use futures::StreamExt;
use futures::stream;
use serde_json::json;
/// Helper constructing a minimal assistant text chunk.
fn text_chunk(txt: &str) -> ResponseEvent {
@@ -500,23 +502,23 @@ mod tests {
let stream = stream::iter(events).aggregate();
let collected: Vec<_> = stream.map(Result::unwrap).collect().await;
assert_eq!(collected.len(), 2, "only final message and Completed");
let expected = vec![
ResponseEvent::OutputItemDone(ResponseItem::Message {
role: "assistant".into(),
content: vec![ContentItem::OutputText {
text: "Hello, world".into(),
}],
}),
ResponseEvent::Completed {
response_id: "r1".into(),
token_usage: None,
},
];
match &collected[0] {
ResponseEvent::OutputItemDone(ResponseItem::Message { content, .. }) => {
let text = match &content[0] {
ContentItem::OutputText { text } => text,
_ => panic!("unexpected content item"),
};
assert_eq!(text, "Hello, world");
}
other => panic!("unexpected first event: {other:?}"),
}
assert!(matches!(
collected[1],
ResponseEvent::Completed { response_id: ref id, token_usage: None } if id == "r1"
));
assert_eq!(
collected, expected,
"aggregated assistant message + Completed"
);
}
#[tokio::test]
@@ -540,36 +542,28 @@ mod tests {
let stream = stream::iter(events).aggregate();
let collected: Vec<_> = stream.map(Result::unwrap).collect().await;
assert_eq!(collected.len(), 3);
let expected = vec![
ResponseEvent::OutputItemDone(func_call.clone()),
ResponseEvent::OutputItemDone(ResponseItem::Message {
role: "assistant".into(),
content: vec![ContentItem::OutputText {
text: "foobar".into(),
}],
}),
ResponseEvent::Completed {
response_id: "r2".into(),
token_usage: None,
},
];
// First event should be the function call forwarded directly.
assert!(matches!(
collected[0],
ResponseEvent::OutputItemDone(ResponseItem::FunctionCall { .. })
));
// Second is the combined assistant message.
match &collected[1] {
ResponseEvent::OutputItemDone(ResponseItem::Message { content, .. }) => {
let text = match &content[0] {
ContentItem::OutputText { text } => text,
_ => panic!("unexpected content item"),
};
assert_eq!(text, "foobar");
}
other => panic!("unexpected second event: {other:?}"),
}
// Final Completed event.
assert!(matches!(
collected[2],
ResponseEvent::Completed { response_id: ref id, token_usage: None } if id == "r2"
));
assert_eq!(
collected, expected,
"non-text items forwarded intact; text merged"
);
}
#[tokio::test]
async fn formats_tool_calls_in_chat_payload() {
use serde_json::Value;
use std::sync::Arc;
use std::sync::Mutex;
use wiremock::Mock;
@@ -580,10 +574,10 @@ mod tests {
use wiremock::matchers::method;
use wiremock::matchers::path;
struct CaptureResponder(Arc<Mutex<Option<Value>>>);
struct CaptureResponder(Arc<Mutex<Option<serde_json::Value>>>);
impl Respond for CaptureResponder {
fn respond(&self, req: &Request) -> ResponseTemplate {
let v: Value = serde_json::from_slice(&req.body).unwrap();
let v: serde_json::Value = serde_json::from_slice(&req.body).unwrap();
*self.0.lock().unwrap() = Some(v);
ResponseTemplate::new(200)
.insert_header("content-type", "text/event-stream")
@@ -605,10 +599,7 @@ data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp\",\"output\":
.mount(&server)
.await;
unsafe {
std::env::set_var("OPENAI_REQUEST_MAX_RETRIES", "0");
}
// Build provider pointing at mock server; no need to mutate global env vars.
let provider = ModelProviderInfo {
name: "openai".into(),
base_url: format!("{}/v1", server.uri()),
@@ -656,37 +647,55 @@ data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp\",\"output\":
.unwrap();
let body = captured.lock().unwrap().take().unwrap();
let messages = body.get("messages").unwrap().as_array().unwrap();
// function_call
let call = messages[2].get("tool_calls").unwrap().as_array().unwrap()[0].clone();
assert_eq!(call.get("id").unwrap().as_str().unwrap(), "call123");
assert_eq!(call.get("type").unwrap().as_str().unwrap(), "function");
let func = call.get("function").unwrap();
assert_eq!(func.get("name").unwrap().as_str().unwrap(), "shell");
assert_eq!(func.get("arguments").unwrap().as_str().unwrap(), "[]");
// Build the expected payload exactly as stream_chat_completions() should.
let full_instructions = prompt.get_full_instructions("model");
let expected_messages = vec![
json!({"role":"system","content":full_instructions}),
json!({"role":"user","content":"hi"}),
json!({
"role":"assistant",
"content":null,
"tool_calls":[{
"id":"call123",
"type":"function",
"function":{
"name":"shell",
"arguments":"[]"
}
}]
}),
json!({
"role":"tool",
"tool_call_id":"call123",
"content":"ok"
}),
json!({
"role":"assistant",
"content":null,
"tool_calls":[{
"id":"ls1",
"type":"local_shell_call",
"status":"completed",
"action":{
"type":"exec",
"command":["echo","hi"],
"timeout_ms":1,
"working_directory":null,
"env":null,
"user":null
}
}]
}),
];
let tools_json = create_tools_json_for_chat_completions_api(&prompt, "model").unwrap();
let expected_body = json!({
"model":"model",
"messages": expected_messages,
"stream": true,
"tools": tools_json,
});
// function_call_output
assert_eq!(messages[3].get("role").unwrap().as_str().unwrap(), "tool");
assert_eq!(
messages[3].get("tool_call_id").unwrap().as_str().unwrap(),
"call123"
);
assert_eq!(messages[3].get("content").unwrap().as_str().unwrap(), "ok");
// local_shell_call
let shell_call = messages[4].get("tool_calls").unwrap().as_array().unwrap()[0].clone();
assert_eq!(
shell_call.get("type").unwrap().as_str().unwrap(),
"local_shell_call"
);
assert_eq!(shell_call.get("id").unwrap().as_str().unwrap(), "ls1");
let action = shell_call.get("action").unwrap();
assert_eq!(
action.get("command").unwrap().as_array().unwrap()[0]
.as_str()
.unwrap(),
"echo"
);
assert_eq!(body, expected_body, "chat payload encoded incorrectly");
}
}
+4 -4
View File
@@ -315,7 +315,7 @@ where
// duplicated `output` array embedded in the `response.completed`
// payload. That produced two concrete issues:
// 1. No real‑time streaming – the user only saw output after the
// entire turn had finished, which broke the “typing” UX and
// entire turn had finished, which broke the "typing" UX and
// made long‑running turns look stalled.
// 2. Duplicate `function_call_output` items – both the
// individual *and* the completed array were forwarded, which
@@ -393,13 +393,13 @@ async fn stream_from_fixture(path: impl AsRef<Path>) -> Result<ResponseStream> {
Ok(ResponseStream { rx_event })
}
#[cfg(test)]
#[cfg(test)]
mod tests {
#![allow(clippy::expect_used, clippy::unwrap_used)]
use super::*;
use crate::models::{LocalShellAction, LocalShellStatus};
use crate::models::LocalShellAction;
use crate::models::LocalShellStatus;
use serde_json::json;
use tokio::sync::mpsc;
use tokio_test::io::Builder as IoBuilder;
@@ -702,4 +702,4 @@ data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp\",\"output\":
);
}
}
}
}
+1 -1
View File
@@ -49,7 +49,7 @@ impl Prompt {
}
}
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq)]
pub enum ResponseEvent {
Created,
OutputItemDone(ResponseItem),
+8 -8
View File
@@ -8,7 +8,7 @@ use serde::ser::Serializer;
use crate::protocol::InputItem;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ResponseInputItem {
Message {
@@ -25,7 +25,7 @@ pub enum ResponseInputItem {
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentItem {
InputText { text: String },
@@ -33,7 +33,7 @@ pub enum ContentItem {
OutputText { text: String },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ResponseItem {
Message {
@@ -99,7 +99,7 @@ impl From<ResponseInputItem> for ResponseItem {
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum LocalShellStatus {
Completed,
@@ -107,13 +107,13 @@ pub enum LocalShellStatus {
Incomplete,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum LocalShellAction {
Exec(LocalShellExecAction),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct LocalShellExecAction {
pub command: Vec<String>,
pub timeout_ms: Option<u64>,
@@ -122,7 +122,7 @@ pub struct LocalShellExecAction {
pub user: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ReasoningItemReasoningSummary {
SummaryText { text: String },
@@ -177,7 +177,7 @@ pub struct ShellToolCallParams {
pub timeout_ms: Option<u64>,
}
#[derive(Deserialize, Debug, Clone)]
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct FunctionCallOutputPayload {
pub content: String,
#[allow(dead_code)]
+1 -1
View File
@@ -326,7 +326,7 @@ pub struct TaskCompleteEvent {
pub last_agent_message: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq)]
pub struct TokenUsage {
pub input_tokens: u64,
pub cached_input_tokens: Option<u64>,