infinity_agent_core/tools/
cancel_subscription.rs1use async_trait::async_trait;
2use infinity_provider_protocol::message::{Text, ToolResult, ToolResultContent};
3use tracing;
4
5use super::{Tool, ToolContext};
6use crate::traits::{InputSender, StateStore};
7use rap_client::http::HttpClient;
8use rap_client::notifier::RapNotifier;
9
10pub struct CancelSubscriptionTool<S: StateStore, H: HttpClient> {
20 pub state_store: S,
21 pub rap_notifier: Option<RapNotifier<H>>,
22}
23
24#[async_trait]
25impl<M: InputSender + 'static, S: StateStore + 'static, H: HttpClient + 'static> Tool<M>
26 for CancelSubscriptionTool<S, H>
27{
28 fn name(&self) -> &str {
29 "cancel_subscription"
30 }
31
32 fn description(&self) -> &str {
33 "Cancel an active subscription by its tool call ID. This sends a cancellation notification to the tool server and removes the subscription from tracking. You can only cancel subscriptions that were created by the current thread."
34 }
35
36 fn parameters(&self) -> serde_json::Value {
37 serde_json::json!({
38 "type": "object",
39 "properties": {
40 "tool_call_id": {
41 "type": "string",
42 "description": "The tool call ID of the subscription to cancel. This is the ID of the original tool call that started the subscription."
43 }
44 },
45 "required": ["tool_call_id"]
46 })
47 }
48
49 async fn execute(
50 &self,
51 _args: serde_json::Value,
52 _id: rap_protocol::ToolCallId,
53 _call_id: Option<rap_protocol::ProviderCallId>,
54 _context: &ToolContext<M>,
55 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
56 Ok(())
58 }
59
60 fn supports_sync(&self) -> bool {
61 true
62 }
63
64 async fn execute_synchronous(
65 &self,
66 args: &serde_json::Value,
67 id: &rap_protocol::ToolCallId<str>,
68 call_id: Option<&rap_protocol::ProviderCallId<str>>,
69 context: &ToolContext<M>,
70 ) -> Option<ToolResult> {
71 let Some(tool_call_id) = args
72 .get("tool_call_id")
73 .and_then(|v| v.as_str())
74 .map(rap_protocol::ToolCallId::from_ref)
75 else {
76 return Some(error_result(id, call_id, "Error: tool_call_id is required"));
77 };
78
79 let Ok(active) = self
82 .state_store
83 .get_active_subscriptions(&context.group_id)
84 .await
85 else {
86 return Some(error_result(
87 id,
88 call_id,
89 "Error: failed to read thread subscriptions",
90 ));
91 };
92
93 if !active.iter().any(|s| s == tool_call_id) {
94 return Some(error_result(
95 id,
96 call_id,
97 &format!(
98 "Error: no active subscription found for tool call ID '{}'",
99 tool_call_id
100 ),
101 ));
102 }
103
104 if let Some(ref notifier) = self.rap_notifier {
106 tracing::info!(
107 "Sending cancellation notification for subscription {}",
108 tool_call_id
109 );
110 notifier
111 .notify_tool_cancelled(&context.group_id, tool_call_id)
112 .await;
113 }
114
115 if let Err(e) = self
117 .state_store
118 .remove_active_subscription(&context.group_id, tool_call_id)
119 .await
120 {
121 tracing::warn!("Failed to persist subscription removal: {}", e);
122 }
123
124 tracing::info!(
125 "Cancelled subscription {} in thread {}",
126 tool_call_id,
127 context.group_id
128 );
129
130 Some(ToolResult {
131 id: id.as_str().to_owned(),
132 call_id: call_id.map(|c| c.as_str().to_owned()),
133 content: vec![ToolResultContent::Text(Text {
134 text: format!("Subscription '{}' cancelled successfully.", tool_call_id),
135 })],
136 })
137 }
138}
139
140fn error_result(
141 id: &rap_protocol::ToolCallId<str>,
142 call_id: Option<&rap_protocol::ProviderCallId<str>>,
143 text: &str,
144) -> ToolResult {
145 ToolResult {
146 id: id.as_str().to_owned(),
147 call_id: call_id.map(|c| c.as_str().to_owned()),
148 content: vec![ToolResultContent::Text(Text {
149 text: text.to_owned(),
150 })],
151 }
152}