1use infinity_provider_protocol::message::{
2 AssistantContent, Message, ToolCall, ToolResult, UserContent,
3};
4use rap_protocol::ThreadId;
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Deserialize, Serialize, Clone)]
8#[serde(untagged)]
9pub enum InputMessageContent {
10 OAuth(OAuthRequired),
11 UserChoice(UserChoiceRequired),
12 User(UserContent),
13}
14
15#[derive(Debug, Deserialize, Serialize, Clone)]
16pub struct OAuthRequired {
17 #[serde(rename = "type")]
18 pub content_type: String,
19 pub id: String,
20 pub call_id: Option<String>,
21 pub auth_url: String,
22}
23
24#[derive(Debug, Deserialize, Serialize, Clone)]
25pub struct UserChoiceRequired {
26 #[serde(rename = "type")]
27 pub content_type: String,
28 pub id: String,
29 pub call_id: Option<String>,
30 pub prompt: String,
31 pub choices: Vec<String>,
32 pub default: usize,
33 pub response_url: String,
34}
35
36#[derive(Debug, Deserialize, Serialize, Clone)]
40#[serde(untagged)]
41pub enum SyntheticKind {
42 Tagged(TaggedSyntheticKind),
43 SubscriptionEvent(String),
45}
46
47#[derive(Debug, Deserialize, Serialize, Clone)]
48#[serde(tag = "type")]
49pub enum TaggedSyntheticKind {
50 #[serde(rename = "subscription_event")]
51 SubscriptionEvent {
52 tool_call_id: String,
53 #[serde(default)]
54 associative: bool,
55 #[serde(default)]
58 r#final: bool,
59 },
60 #[serde(rename = "thread_report")]
61 ThreadReport {
62 tool_call_id: String,
63 child_thread_id: ThreadId,
64 },
65 #[serde(rename = "parent_message")]
66 ParentMessage { tool_call_id: String },
67 #[serde(rename = "compaction")]
68 Compaction,
69 #[serde(rename = "compaction_complete")]
70 CompactionComplete,
71}
72
73impl SyntheticKind {
74 pub fn tool_call_id(&self) -> &str {
75 match self {
76 SyntheticKind::Tagged(TaggedSyntheticKind::SubscriptionEvent {
77 tool_call_id, ..
78 }) => tool_call_id,
79 SyntheticKind::Tagged(TaggedSyntheticKind::ThreadReport { tool_call_id, .. }) => {
80 tool_call_id
81 }
82 SyntheticKind::Tagged(TaggedSyntheticKind::ParentMessage { tool_call_id }) => {
83 tool_call_id
84 }
85 SyntheticKind::Tagged(TaggedSyntheticKind::Compaction) => "",
86 SyntheticKind::Tagged(TaggedSyntheticKind::CompactionComplete) => "",
87 SyntheticKind::SubscriptionEvent(id) => id,
88 }
89 }
90
91 pub fn is_thread_report(&self) -> bool {
92 matches!(
93 self,
94 SyntheticKind::Tagged(TaggedSyntheticKind::ThreadReport { .. })
95 )
96 }
97
98 pub fn is_parent_message(&self) -> bool {
99 matches!(
100 self,
101 SyntheticKind::Tagged(TaggedSyntheticKind::ParentMessage { .. })
102 )
103 }
104
105 pub fn is_compaction(&self) -> bool {
106 matches!(self, SyntheticKind::Tagged(TaggedSyntheticKind::Compaction))
107 }
108
109 pub fn is_compaction_complete(&self) -> bool {
110 matches!(
111 self,
112 SyntheticKind::Tagged(TaggedSyntheticKind::CompactionComplete)
113 )
114 }
115
116 pub fn is_associative(&self) -> bool {
119 matches!(
120 self,
121 SyntheticKind::Tagged(TaggedSyntheticKind::SubscriptionEvent {
122 associative: true,
123 ..
124 })
125 )
126 }
127
128 pub fn is_final(&self) -> bool {
131 matches!(
132 self,
133 SyntheticKind::Tagged(TaggedSyntheticKind::SubscriptionEvent { r#final: true, .. })
134 )
135 }
136}
137
138#[derive(Debug, Deserialize, Serialize, Clone)]
139pub struct InputMessage {
140 pub content: InputMessageContent,
141 pub group_id: ThreadId,
142 #[serde(default)]
143 pub metadata: Option<serde_json::Value>,
144 #[serde(default)]
145 pub synthetic: Option<SyntheticKind>,
146 #[serde(default)]
150 pub display_as: Option<Vec<rap_protocol::DisplaySegment>>,
151 #[serde(default)]
155 pub subscription: bool,
156}
157
158impl InputMessage {
159 pub fn user_text(thread_id: impl Into<ThreadId>, text: impl Into<String>) -> Self {
161 Self {
162 content: InputMessageContent::User(UserContent::text(text.into())),
163 group_id: thread_id.into(),
164 metadata: None,
165 synthetic: None,
166 display_as: None,
167 subscription: false,
168 }
169 }
170}
171
172#[derive(Debug, Clone, Serialize, Deserialize)]
180#[serde(tag = "kind")]
181pub enum InfinityMessage {
182 #[serde(rename = "user")]
184 User { content: UserContent },
185 #[serde(rename = "assistant")]
187 Assistant { content: AssistantContent },
188 #[serde(rename = "tool_call")]
190 ToolCall {
191 call: ToolCall,
192 #[serde(default, skip_serializing_if = "Option::is_none")]
193 display_as: Option<String>,
194 },
195 #[serde(rename = "tool_result")]
197 ToolResult {
198 result: ToolResult,
199 #[serde(default, skip_serializing_if = "Option::is_none")]
200 display_segments: Option<Vec<rap_protocol::DisplaySegment>>,
201 },
202 #[serde(rename = "subscription_event")]
210 SubscriptionEvent {
211 result: Box<ToolResult>,
212 tool_call_id: String,
214 #[serde(default, skip_serializing_if = "Option::is_none")]
216 child_thread_id: Option<ThreadId>,
217 #[serde(default, skip_serializing_if = "Option::is_none")]
222 invocation: Option<Box<ToolCall>>,
223 },
224}
225
226impl InfinityMessage {
227 pub fn into_messages(self) -> Vec<Message> {
231 match self {
232 Self::User { content } => vec![Message::User {
233 content: vec![content],
234 }],
235 Self::Assistant { content } => vec![Message::Assistant {
236 content: vec![content],
237 }],
238 Self::ToolCall { call, .. } => vec![Message::Assistant {
239 content: vec![AssistantContent::ToolCall(call)],
240 }],
241 Self::ToolResult { result, .. } => vec![Message::User {
242 content: vec![UserContent::ToolResult(result)],
243 }],
244 Self::SubscriptionEvent {
245 result, invocation, ..
246 } => {
247 let mut msgs = Vec::new();
248 if let Some(call) = invocation {
249 msgs.push(Message::Assistant {
250 content: vec![AssistantContent::ToolCall(*call)],
251 });
252 }
253 msgs.push(Message::User {
254 content: vec![UserContent::ToolResult(*result)],
255 });
256 msgs
257 }
258 }
259 }
260
261 pub fn tool_result(&self) -> Option<&ToolResult> {
264 match self {
265 Self::ToolResult { result, .. } => Some(result),
266 Self::SubscriptionEvent { result, .. } => Some(result),
267 _ => None,
268 }
269 }
270
271 pub fn from_message(msg: Message) -> Self {
274 match msg {
275 Message::User { content } => {
276 let first = content.into_iter().next().expect("bug: empty content");
277 match first {
278 UserContent::ToolResult(result) => Self::ToolResult {
279 result,
280 display_segments: None,
281 },
282 other => Self::User { content: other },
283 }
284 }
285 Message::Assistant { content } => {
286 let first = content.into_iter().next().expect("bug: empty content");
287 match first {
288 AssistantContent::ToolCall(call) => Self::ToolCall {
289 call,
290 display_as: None,
291 },
292 other => Self::Assistant { content: other },
293 }
294 }
295 }
296 }
297}