Skip to content

Commit b7a4baf

Browse files
committed
Refactor build_message_response to accept Usage struct, removing duplicate cache-token assignments in both clients
1 parent d987028 commit b7a4baf

3 files changed

Lines changed: 26 additions & 23 deletions

File tree

src/api/anthropic.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,17 +339,18 @@ impl AnthropicClient {
339339
}
340340
}
341341

342-
let mut response = utils::build_message_response(
342+
Ok(utils::build_message_response(
343343
message_id,
344344
model_name,
345345
content_blocks,
346346
stop_reason,
347-
input_tokens,
348-
output_tokens,
349-
);
350-
response.usage.cache_read_input_tokens = cache_read_input_tokens;
351-
response.usage.cache_creation_input_tokens = cache_creation_input_tokens;
352-
Ok(response)
347+
Usage {
348+
input_tokens,
349+
output_tokens,
350+
cache_read_input_tokens,
351+
cache_creation_input_tokens,
352+
},
353+
))
353354
}
354355
}
355356

src/api/openai.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,18 @@ impl OpenAIClient {
209209
_ => None,
210210
};
211211

212-
let mut response = utils::build_message_response(
212+
Ok(utils::build_message_response(
213213
response_parsed.id,
214214
response_parsed.model,
215215
content_blocks,
216216
stop_reason,
217-
usage.input_tokens.unwrap_or(0),
218-
usage.output_tokens.unwrap_or(0),
219-
);
220-
response.usage.cache_read_input_tokens = cache_read;
221-
Ok(response)
217+
Usage {
218+
input_tokens: usage.input_tokens.unwrap_or(0),
219+
output_tokens: usage.output_tokens.unwrap_or(0),
220+
cache_read_input_tokens: cache_read,
221+
cache_creation_input_tokens: None,
222+
},
223+
))
222224
}
223225
}
224226

src/api/utils.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ pub fn build_message_response(
7575
model: String,
7676
content: Vec<ContentBlock>,
7777
stop_reason: Option<String>,
78-
input_tokens: u32,
79-
output_tokens: u32,
78+
usage: Usage,
8079
) -> CreateMessageResponse {
8180
CreateMessageResponse {
8281
_id: id,
@@ -85,12 +84,7 @@ pub fn build_message_response(
8584
content,
8685
_model: model,
8786
stop_reason,
88-
usage: Usage {
89-
input_tokens,
90-
output_tokens,
91-
cache_read_input_tokens: None,
92-
cache_creation_input_tokens: None,
93-
},
87+
usage,
9488
}
9589
}
9690

@@ -601,8 +595,12 @@ mod tests {
601595
"test-model".into(),
602596
vec![],
603597
Some("max_tokens".into()),
604-
100,
605-
50,
598+
Usage {
599+
input_tokens: 100,
600+
output_tokens: 50,
601+
cache_read_input_tokens: Some(40),
602+
cache_creation_input_tokens: Some(10),
603+
},
606604
);
607605
assert_eq!(r._id, "id-42");
608606
assert_eq!(r._model, "test-model");
@@ -611,6 +609,8 @@ mod tests {
611609
assert_eq!(r.stop_reason.as_deref(), Some("max_tokens"));
612610
assert_eq!(r.usage.input_tokens, 100);
613611
assert_eq!(r.usage.output_tokens, 50);
612+
assert_eq!(r.usage.cache_read_input_tokens, Some(40));
613+
assert_eq!(r.usage.cache_creation_input_tokens, Some(10));
614614
assert!(r.content.is_empty());
615615
}
616616

0 commit comments

Comments
 (0)