use super::TopicHook;
use crabllm_core::Provider;
use schemars::JsonSchema;
use serde::Deserialize;
use wcore::ToolDispatch;
#[derive(Deserialize, JsonSchema)]
pub struct SwitchTopic {
pub title: String,
#[serde(default)]
pub description: Option<String>,
}
impl<P: Provider + 'static> TopicHook<P> {
pub(super) async fn handle_switch_topic(&self, call: ToolDispatch) -> Result<String, String> {
let input: SwitchTopic =
serde_json::from_str(&call.args).map_err(|e| format!("invalid arguments: {e}"))?;
let shared = self
.runtime
.get()
.ok_or_else(|| "switch_topic: runtime not initialized".to_owned())?;
let rt = shared.read().await.clone();
let outcome = rt
.switch_topic(
&call.agent,
&call.sender,
&input.title,
input.description.as_deref(),
)
.await
.map_err(|e| e.to_string())?;
Ok(if outcome.resumed {
format!("resumed topic: {}", input.title)
} else {
format!("created topic: {}", input.title)
})
}
}