story-kit: merge 86_story_show_live_activity_status_instead_of_static_thinking_indicator_in_chat

This commit is contained in:
Dave
2026-02-23 18:38:15 +00:00
parent da8ded460e
commit af1625a132
5 changed files with 54 additions and 4 deletions

View File

@@ -75,6 +75,10 @@ enum WsResponse {
tool_name: String,
tool_input: serde_json::Value,
},
/// The agent started assembling a tool call; shows live status in the UI.
ToolActivity {
tool_name: String,
},
}
impl From<WatcherEvent> for WsResponse {
@@ -168,6 +172,7 @@ pub async fn ws_handler(ws: WebSocket, ctx: Data<&Arc<AppContext>>) -> impl poem
Ok(WsRequest::Chat { messages, config }) => {
let tx_updates = tx.clone();
let tx_tokens = tx.clone();
let tx_activity = tx.clone();
let ctx_clone = ctx.clone();
let perm_tx = perm_req_tx.clone();
@@ -188,6 +193,11 @@ pub async fn ws_handler(ws: WebSocket, ctx: Data<&Arc<AppContext>>) -> impl poem
content: token.to_string(),
});
},
move |tool_name: &str| {
let _ = tx_activity.send(WsResponse::ToolActivity {
tool_name: tool_name.to_string(),
});
},
Some(perm_tx),
);
tokio::pin!(chat_fut);

View File

@@ -176,13 +176,15 @@ pub fn set_anthropic_api_key(store: &dyn StoreOps, api_key: String) -> Result<()
set_anthropic_api_key_impl(store, &api_key)
}
pub async fn chat<F, U>(
#[allow(clippy::too_many_arguments)]
pub async fn chat<F, U, A>(
messages: Vec<Message>,
config: ProviderConfig,
state: &SessionState,
store: &dyn StoreOps,
mut on_update: F,
mut on_token: U,
mut on_activity: A,
permission_tx: Option<
tokio::sync::mpsc::UnboundedSender<
crate::llm::providers::claude_code::PermissionReqMsg,
@@ -192,6 +194,7 @@ pub async fn chat<F, U>(
where
F: FnMut(&[Message]) + Send,
U: FnMut(&str) + Send,
A: FnMut(&str) + Send,
{
use crate::llm::providers::anthropic::AnthropicProvider;
use crate::llm::providers::ollama::OllamaProvider;
@@ -322,6 +325,7 @@ where
tools,
&mut cancel_rx,
|token| on_token(token),
|tool_name| on_activity(tool_name),
)
.await
.map_err(|e| format!("Anthropic Error: {e}"))?

View File

@@ -156,16 +156,18 @@ impl AnthropicProvider {
.join("\n\n")
}
pub async fn chat_stream<F>(
pub async fn chat_stream<F, A>(
&self,
model: &str,
messages: &[Message],
tools: &[ToolDefinition],
cancel_rx: &mut Receiver<bool>,
mut on_token: F,
mut on_activity: A,
) -> Result<CompletionResponse, String>
where
F: FnMut(&str),
A: FnMut(&str),
{
let anthropic_messages = Self::convert_messages(messages);
let anthropic_tools = Self::convert_tools(tools);
@@ -257,6 +259,7 @@ impl AnthropicProvider {
{
let id = content_block["id"].as_str().unwrap_or("").to_string();
let name = content_block["name"].as_str().unwrap_or("").to_string();
on_activity(&name);
current_tool_use = Some((id, name, String::new()));
}
}