Skip to content

Commit 64e4e2f

Browse files
committed
fixup(mockllm): support tool use
1 parent a91104e commit 64e4e2f

File tree

2 files changed

+65
-39
lines changed

2 files changed

+65
-39
lines changed

crates/chat-cli/src/api_client/mod.rs

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -640,47 +640,43 @@ impl ApiClient {
640640
self.set_mock_llm(move |mut ctx| {
641641
let response_groups = response_groups.clone();
642642
async move {
643-
// Determine which response group to use based on user message count
644-
// Each user message gets the next response group in sequence
645-
let response_index = ctx.count_user_messages().saturating_sub(1); // 0-indexed
646-
647-
// Send the corresponding response group
648-
if response_index < response_groups.len() {
649-
for event in &response_groups[response_index] {
650-
match event {
651-
ChatResponseStream::AssistantResponseEvent { content } => {
652-
ctx.respond(content).await?;
653-
},
654-
ChatResponseStream::ToolUseEvent { tool_use_id, name, input, stop } => {
655-
// For tool events, we need to create proper tool use structure
656-
if stop.unwrap_or(false) {
657-
// This is a complete tool use - parse the arguments
658-
let args = input.as_ref().map(|s| {
659-
serde_json::from_str(s).unwrap_or(serde_json::Value::String(s.clone()))
660-
}).unwrap_or(serde_json::Value::Null);
661-
662-
let _tool_use = crate::cli::chat::AssistantToolUse {
663-
id: tool_use_id.clone(),
664-
name: name.clone(),
665-
orig_name: name.clone(),
666-
args: args.clone(),
667-
orig_args: args,
668-
};
669-
670-
// For now, just send text response about tool use since we don't have direct tool event API
671-
// TODO: Implement proper tool event support in MockLLMContext if needed
672-
ctx.respond(format!("[Tool: {} with args: {}]", name, tool_use_id)).await?;
673-
}
674-
},
675-
_ => {}, // Ignore other event types
643+
// Determine which response group to use based on user message count
644+
// Each user message gets the next response group in sequence
645+
let response_index = ctx.count_user_messages().saturating_sub(1); // 0-indexed
646+
647+
tracing::debug!(
648+
actor="MockLLM",
649+
user_message=ctx.current_user_message(),
650+
count=ctx.count_user_messages(),
651+
response_index,
652+
response_groups=?response_groups,
653+
);
654+
655+
// Send the corresponding response group
656+
if response_index < response_groups.len() {
657+
for event in &response_groups[response_index] {
658+
match event {
659+
ChatResponseStream::AssistantResponseEvent { content } => {
660+
ctx.respond(content).await?;
661+
},
662+
ChatResponseStream::ToolUseEvent {
663+
tool_use_id,
664+
name,
665+
input,
666+
stop,
667+
} => {
668+
ctx.respond_tool_use(tool_use_id.clone(), name.clone(), input.clone(), stop.clone())
669+
.await?;
670+
},
671+
_ => {}, // Ignore other event types
672+
}
676673
}
674+
} else {
675+
// No more predefined responses, send a fallback
676+
ctx.respond("I don't have a response configured for this message.").await?;
677677
}
678-
} else {
679-
// No more predefined responses, send a fallback
680-
ctx.respond("I don't have a response configured for this message.").await?;
681-
}
682-
683-
Ok(())
678+
679+
Ok(())
684680
}
685681
});
686682
}

crates/chat-cli/src/mock_llm.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,42 @@ impl MockLLMContext {
3838
.map_err(|_| eyre::eyre!("Response channel closed"))
3939
}
4040

41+
async fn send_tool_use(
42+
tx: &mut mpsc::Sender<Result<ChatResponseStream, RecvError>>,
43+
tool_use_id: String,
44+
name: String,
45+
input: Option<String>,
46+
stop: Option<bool>,
47+
) -> eyre::Result<()> {
48+
tx
49+
.send(Ok(ChatResponseStream::ToolUseEvent {
50+
tool_use_id,
51+
name,
52+
input,
53+
stop,
54+
}))
55+
.await
56+
.map_err(|_| eyre::eyre!("Response channel closed"))
57+
}
58+
4159
/// Respond with text to the user.
4260
#[allow(dead_code)]
4361
pub async fn respond(&mut self, text: impl ToString) -> eyre::Result<()> {
4462
Self::send_text(&mut self.tx, text).await
4563
}
4664

65+
/// Respond with a tool use
66+
#[allow(dead_code)]
67+
pub async fn respond_tool_use(
68+
&mut self,
69+
tool_use_id: String,
70+
name: String,
71+
input: Option<String>,
72+
stop: Option<bool>,
73+
) -> eyre::Result<()> {
74+
Self::send_tool_use(&mut self.tx, tool_use_id, name, input, stop).await
75+
}
76+
4777
/// Count the number of user messages in the conversation (including the current one).
4878
/// Useful for determining which predefined response to use in sequence.
4979
#[allow(dead_code)]

0 commit comments

Comments
 (0)