goose_agent/
bang_command.rs1use anyhow::Result;
4use async_trait::async_trait;
5use goose_provider_types::conversation::message::Message;
6use goose_provider_types::conversation::{Conversation, EffectiveRole};
7use goose_provider_types::maybe_send::{MaybeSend, MaybeSync};
8use rmcp::model::CallToolRequestParams;
9
10use crate::operation::{
11 applied, last_effective_role, messages_since_kickoff, not_applicable, yielded, Emitter,
12 Operation, OperationResult,
13};
14
15fn bang_command(message: &str) -> Option<&str> {
16 message
17 .trim_start()
18 .strip_prefix('!')
19 .map(str::trim_start)
20 .filter(|command| !command.is_empty())
21}
22
23pub struct BangCommandOperation {
27 tool_name: String,
28 argument_name: String,
29}
30
31impl BangCommandOperation {
32 pub fn new(tool_name: impl Into<String>, argument_name: impl Into<String>) -> Self {
33 Self {
34 tool_name: tool_name.into(),
35 argument_name: argument_name.into(),
36 }
37 }
38}
39
40#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
41#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
42impl<S, E> Operation<S, E> for BangCommandOperation
43where
44 S: MaybeSend + MaybeSync + 'static,
45 E: From<Message> + MaybeSend + 'static,
46{
47 fn name(&self) -> &'static str {
48 "bang_command"
49 }
50
51 async fn run(
52 &self,
53 _session: &S,
54 conversation: &Conversation,
55 emit: &Emitter,
56 ) -> Result<OperationResult<E>> {
57 let messages = messages_since_kickoff(conversation)?;
58 let Some(kickoff) = messages.first() else {
59 return not_applicable();
60 };
61 let kickoff_text = kickoff.user_visible_content().as_concat_text();
62 let Some(command) = bang_command(&kickoff_text) else {
63 return not_applicable();
64 };
65
66 if messages.len() > 1 {
67 return if last_effective_role(messages)? == EffectiveRole::Tool {
68 yielded()
69 } else {
70 not_applicable()
71 };
72 }
73
74 let call = CallToolRequestParams::new(self.tool_name.clone()).with_arguments(
75 serde_json::Map::from_iter([(
76 self.argument_name.clone(),
77 serde_json::Value::String(command.to_string()),
78 )]),
79 );
80 let request = Message::assistant()
81 .with_tool_request(format!("bang_command_{}", uuid::Uuid::new_v4()), Ok(call));
82
83 applied([E::from(emit.message(request))])
84 }
85}