Merge remote-tracking branch 'origin/rcc/thinking' into dev/rust
# Conflicts: # rust/crates/commands/src/lib.rs # rust/crates/rusty-claude-cli/src/main.rs
This commit is contained in:
@@ -17,6 +17,8 @@ pub struct ApiRequest {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum AssistantEvent {
|
||||
TextDelta(String),
|
||||
ThinkingDelta(String),
|
||||
ThinkingSignature(String),
|
||||
ToolUse {
|
||||
id: String,
|
||||
name: String,
|
||||
@@ -247,15 +249,26 @@ fn build_assistant_message(
|
||||
events: Vec<AssistantEvent>,
|
||||
) -> Result<(ConversationMessage, Option<TokenUsage>), RuntimeError> {
|
||||
let mut text = String::new();
|
||||
let mut thinking = String::new();
|
||||
let mut thinking_signature: Option<String> = None;
|
||||
let mut blocks = Vec::new();
|
||||
let mut finished = false;
|
||||
let mut usage = None;
|
||||
|
||||
for event in events {
|
||||
match event {
|
||||
AssistantEvent::TextDelta(delta) => text.push_str(&delta),
|
||||
AssistantEvent::TextDelta(delta) => {
|
||||
flush_thinking_block(&mut thinking, &mut thinking_signature, &mut blocks);
|
||||
text.push_str(&delta);
|
||||
}
|
||||
AssistantEvent::ThinkingDelta(delta) => {
|
||||
flush_text_block(&mut text, &mut blocks);
|
||||
thinking.push_str(&delta);
|
||||
}
|
||||
AssistantEvent::ThinkingSignature(signature) => thinking_signature = Some(signature),
|
||||
AssistantEvent::ToolUse { id, name, input } => {
|
||||
flush_text_block(&mut text, &mut blocks);
|
||||
flush_thinking_block(&mut thinking, &mut thinking_signature, &mut blocks);
|
||||
blocks.push(ContentBlock::ToolUse { id, name, input });
|
||||
}
|
||||
AssistantEvent::Usage(value) => usage = Some(value),
|
||||
@@ -266,6 +279,7 @@ fn build_assistant_message(
|
||||
}
|
||||
|
||||
flush_text_block(&mut text, &mut blocks);
|
||||
flush_thinking_block(&mut thinking, &mut thinking_signature, &mut blocks);
|
||||
|
||||
if !finished {
|
||||
return Err(RuntimeError::new(
|
||||
@@ -290,6 +304,19 @@ fn flush_text_block(text: &mut String, blocks: &mut Vec<ContentBlock>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn flush_thinking_block(
|
||||
thinking: &mut String,
|
||||
signature: &mut Option<String>,
|
||||
blocks: &mut Vec<ContentBlock>,
|
||||
) {
|
||||
if !thinking.is_empty() || signature.is_some() {
|
||||
blocks.push(ContentBlock::Thinking {
|
||||
text: std::mem::take(thinking),
|
||||
signature: signature.take(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
type ToolHandler = Box<dyn FnMut(&str) -> Result<String, ToolError>>;
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -325,8 +352,8 @@ impl ToolExecutor for StaticToolExecutor {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
ApiClient, ApiRequest, AssistantEvent, ConversationRuntime, RuntimeError,
|
||||
StaticToolExecutor,
|
||||
build_assistant_message, ApiClient, ApiRequest, AssistantEvent, ConversationRuntime,
|
||||
RuntimeError, StaticToolExecutor,
|
||||
};
|
||||
use crate::compact::CompactionConfig;
|
||||
use crate::permissions::{
|
||||
@@ -503,6 +530,29 @@ mod tests {
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thinking_blocks_are_preserved_separately_from_text() {
|
||||
let (message, usage) = build_assistant_message(vec![
|
||||
AssistantEvent::ThinkingDelta("first ".to_string()),
|
||||
AssistantEvent::ThinkingDelta("second".to_string()),
|
||||
AssistantEvent::ThinkingSignature("sig-1".to_string()),
|
||||
AssistantEvent::TextDelta("final".to_string()),
|
||||
AssistantEvent::MessageStop,
|
||||
])
|
||||
.expect("assistant message should build");
|
||||
|
||||
assert_eq!(usage, None);
|
||||
assert!(matches!(
|
||||
&message.blocks[0],
|
||||
ContentBlock::Thinking { text, signature }
|
||||
if text == "first second" && signature.as_deref() == Some("sig-1")
|
||||
));
|
||||
assert!(matches!(
|
||||
&message.blocks[1],
|
||||
ContentBlock::Text { text } if text == "final"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reconstructs_usage_tracker_from_restored_session() {
|
||||
struct SimpleApi;
|
||||
|
||||
Reference in New Issue
Block a user