infinity_daemon/session/
display.rs1use infinity_agent_core::ThreadId;
2use infinity_agent_core::message::InfinityMessage;
3use infinity_agent_core::system::AgentEvent;
4use infinity_protocol::{DaemonMessage, ThreadRef, TokenUsage};
5use infinity_provider_protocol::message::{AssistantContent, ToolResultContent, UserContent};
6
7pub(crate) fn agent_event_to_daemon(thread_id: &ThreadId<str>, evt: &AgentEvent) -> DaemonMessage {
8 let tid = Some(ThreadRef::local(thread_id.to_owned()));
9 match evt {
10 AgentEvent::CompletionStarted => DaemonMessage::StartOutput { thread_id: tid },
11 AgentEvent::TextChunk { text } => DaemonMessage::TextChunk {
12 thread_id: tid,
13 chunk: text.clone(),
14 },
15 AgentEvent::ToolCall {
16 name,
17 args,
18 display_as,
19 } => DaemonMessage::ToolCall {
20 name: name.clone(),
21 args: args.to_string(),
22 thread_id: tid,
23 display_as: display_as.clone(),
24 },
25 AgentEvent::ToolResult { segments } => DaemonMessage::ToolResult {
26 segments: segments.clone(),
27 thread_id: tid,
28 },
29 AgentEvent::Info { text } => DaemonMessage::Info {
30 thread_id: tid,
31 text: text.clone(),
32 },
33 AgentEvent::CompletionFinished { usage } => {
34 let token_usage = usage.map(|u| TokenUsage {
35 input_tokens: Some(u.input_tokens),
36 output_tokens: Some(u.output_tokens),
37 total_tokens: Some(u.total_tokens),
38 });
39 DaemonMessage::ResponseDone {
40 thread_id: tid,
41 token_usage,
42 }
43 }
44 AgentEvent::UserInput { text } => DaemonMessage::UserInputEcho {
45 thread_id: tid,
46 text: text.clone(),
47 },
48 AgentEvent::SubscriptionEvent { name, text } => DaemonMessage::SubscriptionEvent {
49 name: name.clone(),
50 text: text.clone(),
51 thread_id: tid,
52 },
53 AgentEvent::OAuthRequired { auth_url } => DaemonMessage::OAuthRequired {
54 thread_id: tid,
55 auth_url: auth_url.clone(),
56 },
57 AgentEvent::ThinkingStarted => DaemonMessage::ThinkingStart { thread_id: tid },
58 AgentEvent::ThinkingEnded => DaemonMessage::ThinkingEnd { thread_id: tid },
59 AgentEvent::ThinkingChunk { text } => DaemonMessage::ThinkingChunk {
60 thread_id: tid,
61 chunk: text.clone(),
62 },
63 AgentEvent::UserChoiceRequired { choice } => DaemonMessage::UserChoiceRequired {
64 thread_id: tid,
65 id: choice.id.clone(),
66 prompt: choice.prompt.clone(),
67 choices: choice.choices.clone(),
68 default: choice.default,
69 },
70 AgentEvent::UserChoiceDismissed { choice_id } => DaemonMessage::UserChoiceComplete {
71 choice_id: choice_id.clone(),
72 },
73 AgentEvent::CompactionApplied => DaemonMessage::CompactionApplied { thread_id: tid },
74 }
75}
76
77pub(crate) fn history_message_to_daemon(
78 msg: &InfinityMessage,
79 tid: &ThreadId<str>,
80 history: &[InfinityMessage],
81) -> Option<DaemonMessage> {
82 let thread_id = Some(ThreadRef::local(tid.to_owned()));
83 match msg {
84 InfinityMessage::SubscriptionEvent {
85 result,
86 tool_call_id,
87 child_thread_id,
88 ..
89 } => {
90 let text = if let Some(ToolResultContent::Text(t)) = result.content.first() {
91 t.text.clone()
92 } else {
93 String::new()
94 };
95 let name = if let Some(child_id) = child_thread_id {
96 format!("Report from child thread {}", child_id)
97 } else {
98 history
99 .iter()
100 .find_map(|m| {
101 if let InfinityMessage::ToolCall { call, .. } = m
102 && call.id == tool_call_id.as_str()
103 {
104 Some(format!(
105 "{}({})",
106 call.function.name, call.function.arguments
107 ))
108 } else {
109 None
110 }
111 })
112 .unwrap_or_else(|| tool_call_id.as_str().to_owned())
113 };
114 Some(DaemonMessage::SubscriptionEvent {
115 name,
116 text,
117 thread_id,
118 })
119 }
120 InfinityMessage::ToolCall { call, display_as } => Some(DaemonMessage::ToolCall {
121 name: call.function.name.clone(),
122 args: call.function.arguments.to_string(),
123 thread_id,
124 display_as: display_as.clone(),
125 }),
126 InfinityMessage::ToolResult {
127 result,
128 display_segments,
129 } => {
130 if let Some(ToolResultContent::Text(t)) = result.content.first() {
131 Some(DaemonMessage::ToolResult {
134 segments: rap_protocol::build_display_segments(
135 display_segments.as_deref(),
136 &t.text,
137 ),
138 thread_id,
139 })
140 } else {
141 None
142 }
143 }
144 InfinityMessage::User { content } => {
145 if let UserContent::Text(text) = content {
146 let display_text = text
150 .text
151 .strip_prefix("<interrupt>")
152 .unwrap_or(&text.text)
153 .to_owned();
154 Some(DaemonMessage::UserInputEcho {
155 thread_id,
156 text: display_text,
157 })
158 } else {
159 None
160 }
161 }
162 InfinityMessage::Assistant { content } => {
163 if let AssistantContent::Text(text) = content {
164 Some(DaemonMessage::TextChunk {
165 thread_id,
166 chunk: text.text.clone(),
167 })
168 } else {
169 None
170 }
171 }
172 }
173}