Some clippy

This commit is contained in:
jif-oai
2025-10-31 13:37:11 +01:00
parent cfe7fb825c
commit e770917d9b
8 changed files with 104 additions and 94 deletions
+16 -18
View File
@@ -304,11 +304,10 @@ impl ChatCompletionsApiClient {
"content": content_value,
});
if let Some(reasoning) = reasoning_by_anchor_index.get(&idx) {
message
.as_object_mut()
.expect("message")
.insert("reasoning".to_string(), json!({"text": reasoning}));
if let Some(reasoning) = reasoning_by_anchor_index.get(&idx)
&& let Some(obj) = message.as_object_mut()
{
obj.insert("reasoning".to_string(), json!({"text": reasoning}));
}
messages.push(message);
@@ -641,19 +640,18 @@ async fn process_chat_sse<S>(
if let Some(finish) = choice.get("finish_reason").and_then(|f| f.as_str())
&& finish == "tool_calls"
&& let Some(name) = function_call_state.name.take() {
let call_id =
function_call_state.call_id.take().unwrap_or_default();
let arguments = std::mem::take(&mut function_call_state.arguments);
let item = ResponseItem::FunctionCall {
id: None,
name,
arguments,
call_id,
};
let _ =
tx_event.send(Ok(ResponseEvent::OutputItemDone(item))).await;
}
&& let Some(name) = function_call_state.name.take()
{
let call_id = function_call_state.call_id.take().unwrap_or_default();
let arguments = std::mem::take(&mut function_call_state.arguments);
let item = ResponseItem::FunctionCall {
id: None,
name,
arguments,
call_id,
};
let _ = tx_event.send(Ok(ResponseEvent::OutputItemDone(item))).await;
}
}
}
}
+4 -3
View File
@@ -121,9 +121,10 @@ impl ModelProviderInfo {
let mut builder = client.post(url);
if let Some(context) = effective_auth.as_ref()
&& let Some(token) = context.bearer_token.as_ref() {
builder = builder.bearer_auth(token);
}
&& let Some(token) = context.bearer_token.as_ref()
{
builder = builder.bearer_auth(token);
}
Ok(self.apply_http_headers(builder))
}
+38 -35
View File
@@ -76,10 +76,10 @@ impl ApiClient for ResponsesApiClient {
if self.config.provider.is_azure_responses_endpoint()
&& let Some(input_value) = payload_json.get_mut("input")
&& let Some(array) = input_value.as_array_mut()
{
attach_item_ids_array(array, &prompt.input);
}
&& let Some(array) = input_value.as_array_mut()
{
attach_item_ids_array(array, &prompt.input);
}
let max_attempts = self.config.provider.request_max_retries();
for attempt in 0..=max_attempts {
@@ -122,22 +122,22 @@ impl ResponsesApiClient {
.unwrap_or_else(|| self.config.conversation_id.to_string()),
});
if let Some(reasoning) = prompt.reasoning.as_ref() {
payload
.as_object_mut()
.expect("payload object")
.insert("reasoning".to_string(), serde_json::to_value(reasoning)?);
if let Some(reasoning) = prompt.reasoning.as_ref()
&& let Some(obj) = payload.as_object_mut()
{
obj.insert("reasoning".to_string(), serde_json::to_value(reasoning)?);
}
if let Some(text) = prompt.text_controls.as_ref() {
payload
.as_object_mut()
.expect("payload object")
.insert("text".to_string(), serde_json::to_value(text)?);
if let Some(text) = prompt.text_controls.as_ref()
&& let Some(obj) = payload.as_object_mut()
{
obj.insert("text".to_string(), serde_json::to_value(text)?);
}
if let Some(prev) = prompt.previous_response_id.as_ref() {
payload.as_object_mut().expect("payload object").insert(
if let Some(prev) = prompt.previous_response_id.as_ref()
&& let Some(obj) = payload.as_object_mut()
{
obj.insert(
"previous_response_id".to_string(),
Value::String(prev.clone()),
);
@@ -148,10 +148,12 @@ impl ResponsesApiClient {
} else {
Vec::new()
};
payload.as_object_mut().expect("payload object").insert(
"include".to_string(),
Value::Array(include.into_iter().map(Value::String).collect()),
);
if let Some(obj) = payload.as_object_mut() {
obj.insert(
"include".to_string(),
Value::Array(include.into_iter().map(Value::String).collect()),
);
}
Ok(payload)
}
@@ -261,14 +263,14 @@ impl ResponsesApiClient {
if status == StatusCode::UNAUTHORIZED
&& let Some(provider) = self.config.auth_provider.as_ref()
&& let Some(ctx) = auth.as_ref()
&& ctx.mode == AuthMode::ChatGPT
{
provider
.refresh_token()
.await
.map_err(|err| StreamAttemptError::Fatal(Error::Auth(err)))?;
}
&& let Some(ctx) = auth.as_ref()
&& ctx.mode == AuthMode::ChatGPT
{
provider
.refresh_token()
.await
.map_err(|err| StreamAttemptError::Fatal(Error::Auth(err)))?;
}
if !(status == StatusCode::TOO_MANY_REQUESTS
|| status == StatusCode::UNAUTHORIZED
@@ -671,10 +673,11 @@ fn backoff(attempt: u64) -> Duration {
Duration::from_millis(base * 100)
}
fn rate_limit_regex() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
fn rate_limit_regex() -> Option<&'static Regex> {
static RE: OnceLock<Option<Regex>> = OnceLock::new();
RE.get_or_init(|| Regex::new(r"Please try again in (\d+(?:\.\d+)?)(s|ms)").unwrap())
RE.get_or_init(|| Regex::new(r"Please try again in (\d+(?:\.\d+)?)(s|ms)").ok())
.as_ref()
}
fn try_parse_retry_after(err: &ErrorResponse) -> Option<Duration> {
@@ -682,8 +685,8 @@ fn try_parse_retry_after(err: &ErrorResponse) -> Option<Duration> {
return None;
}
let re = rate_limit_regex();
if let Some(message) = &err.message
if let Some(re) = rate_limit_regex()
&& let Some(message) = &err.message
&& let Some(captures) = re.captures(message)
{
let seconds = captures.get(1);
@@ -721,8 +724,8 @@ pub async fn stream_from_fixture(
let mut content = String::new();
for line in lines {
let line = line
.map_err(|e| Error::Other(format!("failed to read fixture {display_path}: {e}")))?;
let line =
line.map_err(|e| Error::Other(format!("failed to read fixture {display_path}: {e}")))?;
content.push_str(&line);
content.push_str("\n\n");
}
+10 -12
View File
@@ -182,18 +182,16 @@ impl ModelClient {
let mut prompt = payload.prompt.clone();
self.populate_prompt(&mut prompt);
if self.provider.wire_api == WireApi::Responses
&& let Some(path) = &*CODEX_RS_SSE_FIXTURE {
warn!(path, "Streaming from fixture");
let stream = stream_from_fixture(
path,
self.provider.clone(),
self.otel_event_manager.clone(),
)
.await
.map_err(map_api_error)?
.boxed();
return Ok(wrap_stream(stream));
}
&& let Some(path) = &*CODEX_RS_SSE_FIXTURE
{
warn!(path, "Streaming from fixture");
let stream =
stream_from_fixture(path, self.provider.clone(), self.otel_event_manager.clone())
.await
.map_err(map_api_error)?
.boxed();
return Ok(wrap_stream(stream));
}
let backend = self
.backend
+5 -3
View File
@@ -108,7 +108,7 @@ impl SessionState {
// Populate prompt fields that depend only on session state.
let (tools_json, has_freeform_apply_patch) =
tools_metadata_for_prompt(&tool_specs).expect("tool specs serialization");
tools_metadata_for_prompt(&tool_specs).unwrap_or_default();
format_prompt_items(&mut prompt.input, has_freeform_apply_patch);
let apply_patch_present = tool_specs.iter().any(|spec| spec.name() == "apply_patch");
@@ -141,8 +141,10 @@ pub(crate) fn build_prompt_from_items(
prompt_items: Vec<ResponseItem>,
chain_state: Option<&ResponsesApiChainState>,
) -> (Prompt, bool) {
let mut prompt = Prompt::default();
prompt.store_response = chain_state.is_some();
let mut prompt = Prompt {
store_response: chain_state.is_some(),
..Prompt::default()
};
if let Some(state) = chain_state {
if let Some(last_message_id) = state.last_message_id.as_ref() {
@@ -97,8 +97,10 @@ async fn run_request(input: Vec<ResponseItem>) -> Value {
codex_protocol::protocol::SessionSource::Exec,
);
let mut prompt = Prompt::default();
prompt.input = input;
let prompt = Prompt {
input,
..Prompt::default()
};
let mut stream = match client.stream_for_test(prompt).await {
Ok(s) => s,
+9 -7
View File
@@ -97,14 +97,16 @@ async fn run_stream_with_bytes(sse_body: &[u8]) -> Vec<ResponseEvent> {
codex_protocol::protocol::SessionSource::Exec,
);
let mut prompt = Prompt::default();
prompt.input = vec![ResponseItem::Message {
id: None,
role: "user".to_string(),
content: vec![ContentItem::InputText {
text: "hello".to_string(),
let prompt = Prompt {
input: vec![ResponseItem::Message {
id: None,
role: "user".to_string(),
content: vec![ContentItem::InputText {
text: "hello".to_string(),
}],
}],
}];
..Prompt::default()
};
let mut stream = match client.stream_for_test(prompt).await {
Ok(s) => s,
+18 -14
View File
@@ -82,14 +82,16 @@ async fn responses_stream_includes_subagent_header_on_review() {
SessionSource::SubAgent(codex_protocol::protocol::SubAgentSource::Review),
);
let mut prompt = Prompt::default();
prompt.input = vec![ResponseItem::Message {
id: None,
role: "user".into(),
content: vec![ContentItem::InputText {
text: "hello".into(),
let prompt = Prompt {
input: vec![ResponseItem::Message {
id: None,
role: "user".into(),
content: vec![ContentItem::InputText {
text: "hello".into(),
}],
}],
}];
..Prompt::default()
};
let mut stream = client.stream_for_test(prompt).await.expect("stream failed");
while let Some(event) = stream.next().await {
@@ -172,14 +174,16 @@ async fn responses_stream_includes_subagent_header_on_other() {
)),
);
let mut prompt = Prompt::default();
prompt.input = vec![ResponseItem::Message {
id: None,
role: "user".into(),
content: vec![ContentItem::InputText {
text: "hello".into(),
let prompt = Prompt {
input: vec![ResponseItem::Message {
id: None,
role: "user".into(),
content: vec![ContentItem::InputText {
text: "hello".into(),
}],
}],
}];
..Prompt::default()
};
let mut stream = client.stream_for_test(prompt).await.expect("stream failed");
while let Some(event) = stream.next().await {