Skip to content

Commit c803fa9

Browse files
authored
feat: enhance response handling with reasoning support (#178)
* feat: enhance response handling with reasoning support * Updated ConversationId struct to derive Copy trait for improved usability. * Introduced reasoning token tracking in ResponseStreamContext to accumulate reasoning tokens during response processing. * Added new methods in ResponseServiceImpl for emitting reasoning events and processing reasoning tags in text deltas. * Enhanced Usage model to include reasoning tokens in usage calculations. * Implemented tests for reasoning tag processing and token estimation to ensure functionality and correctness. * refactor: few fixes and clippy * Removed unnecessary cloning of conversation_id in service methods for improved performance and clarity. * Updated related function calls to directly use conversation_id, enhancing code readability and efficiency. * refactor: streamline code formatting and improve readability * Removed unnecessary line breaks and simplified function calls in `service.rs` and `service_helpers.rs` for better clarity. * Enhanced the formatting of method parameters and function calls across multiple files to maintain consistency and improve code readability. * Updated tests to reflect changes in function call formatting, ensuring all tests remain functional and clear.
1 parent 7c8b897 commit c803fa9

5 files changed

Lines changed: 535 additions & 34 deletions

File tree

crates/services/src/conversations/models.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use uuid::Uuid;
44

55
use crate::{conversations::errors, workspace::WorkspaceId};
66

7-
#[derive(Debug, Clone, Serialize, Deserialize)]
7+
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
88
pub struct ConversationId(pub Uuid);
99

1010
impl std::str::FromStr for ConversationId {

crates/services/src/conversations/service.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ impl ports::ConversationServiceTrait for ConversationServiceImpl {
297297
// Verify conversation exists
298298
let conversation = self
299299
.conv_repo
300-
.get_by_id(conversation_id.clone(), workspace_id.clone())
300+
.get_by_id(conversation_id, workspace_id.clone())
301301
.await
302302
.map_err(|e| {
303303
errors::ConversationError::InternalError(format!(
@@ -367,12 +367,7 @@ impl ports::ConversationServiceTrait for ConversationServiceImpl {
367367
for item in items {
368368
let created_item = self
369369
.response_items_repo
370-
.create(
371-
response_id.clone(),
372-
api_key_id,
373-
Some(conversation_id.clone()),
374-
item,
375-
)
370+
.create(response_id.clone(), api_key_id, Some(conversation_id), item)
376371
.await
377372
.map_err(|e| {
378373
errors::ConversationError::InternalError(format!(

crates/services/src/responses/models.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,4 +732,20 @@ impl Usage {
732732
total_tokens: input_tokens + output_tokens,
733733
}
734734
}
735+
736+
pub fn new_with_reasoning(
737+
input_tokens: i32,
738+
output_tokens: i32,
739+
reasoning_tokens: i32,
740+
) -> Self {
741+
Self {
742+
input_tokens,
743+
input_tokens_details: Some(InputTokensDetails { cached_tokens: 0 }),
744+
output_tokens,
745+
output_tokens_details: Some(OutputTokensDetails {
746+
reasoning_tokens: reasoning_tokens as i64,
747+
}),
748+
total_tokens: input_tokens + output_tokens,
749+
}
750+
}
735751
}

0 commit comments

Comments
 (0)