WIP push-to-talk Letta chat frontend
1use core::fmt;
2
3use serde::{Deserialize, Serialize};
4use serde_string_enum::{DeserializeLabeledStringEnum, SerializeLabeledStringEnum};
5use strum::{Display, EnumString};
6use ts_rs::TS;
7
8#[derive(Debug)]
9pub enum LettaError {
10 ConfigurationError(String),
11 ApiKeyError(keyring::Error),
12 RequestError(reqwest::Error),
13 EventSourceError(reqwest_eventsource::Error),
14}
15
16impl fmt::Display for LettaError {
17 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18 match self {
19 LettaError::ConfigurationError(msg) => write!(f, "Configuration error: {}", msg),
20 LettaError::ApiKeyError(err) => write!(f, "API key error: {}", err),
21 LettaError::RequestError(err) => write!(f, "Letta request error: {}", err),
22 LettaError::EventSourceError(err) => write!(f, "Letta event source error: {}", err),
23 }
24 }
25}
26
27impl From<keyring::Error> for LettaError {
28 fn from(err: keyring::Error) -> Self {
29 LettaError::ApiKeyError(err)
30 }
31}
32
33impl From<reqwest::Error> for LettaError {
34 fn from(err: reqwest::Error) -> Self {
35 LettaError::RequestError(err)
36 }
37}
38
39impl From<reqwest_eventsource::Error> for LettaError {
40 fn from(err: reqwest_eventsource::Error) -> Self {
41 LettaError::EventSourceError(err)
42 }
43}
44
45#[derive(Serialize, Deserialize, Clone, Display, EnumString)]
46#[strum(prefix = "letta")]
47pub enum LettaConfigKey {
48 BaseUrl,
49 AgentId,
50}
51
52#[derive(Serialize, Deserialize, TS, Debug)]
53#[ts(export)]
54pub struct LettaAgentInfo {
55 id: String,
56 name: String,
57}
58
59#[derive(Serialize, Deserialize, TS, Debug, Clone)]
60#[serde(tag = "type", rename_all = "lowercase")]
61pub enum LettaMessageContent {
62 Text { text: String },
63 Image { source: String },
64}
65
66#[derive(TS, Debug, SerializeLabeledStringEnum, DeserializeLabeledStringEnum, Clone, Copy)]
67pub enum LettaReasoningSource {
68 #[string = "reasoner_model"]
69 ReasonerModel,
70
71 #[string = "non_reasoner_model"]
72 NonReasonerModel,
73}
74
75#[derive(SerializeLabeledStringEnum, DeserializeLabeledStringEnum, TS, Debug, Clone, Copy)]
76pub enum LettaHiddenReasoningState {
77 #[string = "redacted"]
78 Redacted,
79
80 #[string = "omitted"]
81 Omitted,
82}
83
84#[derive(Serialize, Deserialize, TS, Debug, Clone)]
85#[serde(untagged)]
86pub enum LettaToolCall {
87 Call {
88 name: String,
89 arguments: String,
90 tool_call_id: String,
91 },
92 Delta {
93 name: Option<String>,
94 arguments: Option<String>,
95 tool_call_id: Option<String>,
96 },
97}
98
99#[derive(SerializeLabeledStringEnum, DeserializeLabeledStringEnum, TS, Debug, Clone, Copy)]
100pub enum LettaToolReturnStatus {
101 #[string = "success"]
102 Success,
103
104 #[string = "error"]
105 Error,
106}
107
108#[derive(Serialize, Deserialize, TS, Debug, Clone)]
109#[serde(tag = "message_type", rename_all = "snake_case")]
110#[ts(export)]
111pub enum LettaCompletionMessage {
112 SystemMessage {
113 id: String,
114 date: String,
115 content: String,
116 name: Option<String>,
117 otid: Option<String>,
118 sender_id: Option<String>,
119 step_id: Option<String>,
120 is_err: Option<bool>,
121 seq_id: Option<i64>,
122 run_id: Option<String>,
123 },
124 UserMessage {
125 id: String,
126 date: String,
127 content: Vec<LettaMessageContent>,
128 name: Option<String>,
129 otid: Option<String>,
130 sender_id: Option<String>,
131 step_id: Option<String>,
132 is_err: Option<bool>,
133 seq_id: Option<i64>,
134 run_id: Option<String>,
135 },
136 ReasoningMessage {
137 id: String,
138 date: String,
139 reasoning: String,
140 name: Option<String>,
141 otid: Option<String>,
142 sender_id: Option<String>,
143 step_id: Option<String>,
144 is_err: Option<bool>,
145 seq_id: Option<i64>,
146 run_id: Option<String>,
147 source: Option<LettaReasoningSource>,
148 signature: Option<String>,
149 },
150 HiddenReasoningMessage {
151 id: String,
152 date: String,
153 state: LettaHiddenReasoningState,
154 name: Option<String>,
155 otid: Option<String>,
156 sender_id: Option<String>,
157 step_id: Option<String>,
158 is_err: Option<bool>,
159 seq_id: Option<i64>,
160 run_id: Option<String>,
161 hidden_reasoning: Option<String>,
162 },
163 ToolCallMessage {
164 id: String,
165 date: String,
166 tool_call: LettaToolCall,
167 name: Option<String>,
168 otid: Option<String>,
169 sender_id: Option<String>,
170 step_id: Option<String>,
171 is_err: Option<bool>,
172 seq_id: Option<i64>,
173 run_id: Option<String>,
174 },
175 ToolReturnMessage {
176 id: String,
177 date: String,
178 tool_return: String,
179 status: LettaToolReturnStatus,
180 tool_call_id: String,
181 name: Option<String>,
182 otid: Option<String>,
183 sender_id: Option<String>,
184 step_id: Option<String>,
185 is_err: Option<bool>,
186 seq_id: Option<i64>,
187 run_id: Option<String>,
188 stdout: Option<Vec<String>>,
189 stderr: Option<Vec<String>>,
190 },
191 AssistantMessage {
192 id: String,
193 date: String,
194 content: Vec<LettaMessageContent>,
195 name: Option<String>,
196 otid: Option<String>,
197 sender_id: Option<String>,
198 step_id: Option<String>,
199 is_err: Option<bool>,
200 seq_id: Option<i64>,
201 run_id: Option<String>,
202 },
203 ApprovalRequestMessage {
204 id: String,
205 date: String,
206 tool_call: LettaToolCall,
207 name: Option<String>,
208 otid: Option<String>,
209 sender_id: Option<String>,
210 step_id: Option<String>,
211 is_err: Option<bool>,
212 seq_id: Option<i64>,
213 run_id: Option<String>,
214 },
215 ApprovalResponseMessage {
216 id: String,
217 date: String,
218 approve: bool,
219 approval_request_id: String,
220 name: Option<String>,
221 otid: Option<String>,
222 sender_id: Option<String>,
223 step_id: Option<String>,
224 is_err: Option<bool>,
225 seq_id: Option<i64>,
226 run_id: Option<String>,
227 reason: Option<String>,
228 },
229 StopReason {
230 // Not documented, only observed value so far is "end_turn"
231 stop_reason: String,
232 },
233 UsageStatistics {
234 completion_tokens: i64,
235 prompt_tokens: i64,
236 step_count: i64,
237 // Observed but undocumented, inner type unclear
238 // steps_messages: Option<Vec<String>>,
239
240 // Observed but undocumented
241 // run_ids: Option<Vec<String>>,
242 },
243}