Skip to main content

fauna_api/parrot/
chat.rs

1use std::sync::Arc;
2
3use chrono::SecondsFormat;
4use futures_util::StreamExt;
5use serde::{Deserialize, Serialize};
6use serde_json::{Map, Value, json};
7
8use super::generative::{Content, File as FilePart, Part, file_ref};
9use super::room::{ChatRoom, RoomHooks, connect_room};
10use super::share::{ChatTarget, OpenRoomOptions, open_room};
11use crate::authentification::CredentialsOrApiKey;
12use crate::billing::PageQuery;
13use crate::drive::{self, DriveAuthorization, FileDates};
14use crate::error::Result;
15use crate::groups::Group;
16use crate::http::{client, ok};
17use crate::shared::server::Presence;
18use crate::termite::versioning::Repository;
19
20/// The `options` argument of `open_chat`.
21#[derive(Default)]
22pub struct OpenChatOptions {
23    pub chat_id: Option<String>,
24    pub messages: Option<Vec<Content>>,
25    pub share: Option<bool>,
26    pub hooks: Option<Arc<dyn RoomHooks>>,
27}
28
29/// What `open_chat` resolves to: `{ room, self, reference, code? }` in the
30/// TypeScript SDK, with `self` as `me`.
31#[derive(Clone)]
32pub struct OpenedChat {
33    pub room: ChatRoom,
34    pub me: Presence,
35    pub reference: String,
36    pub code: Option<String>,
37}
38
39/// Open a chat as a live room: host it server-side, seed the newest window from
40/// durable, then connect. Pass a durable `chat_id` to bind the room
41/// (`parrot:chat:{chatId}`, persisted + billed to the chat's balance-holder);
42/// omit it for an unbound ephemeral room. `share: true` also mints an access code
43/// others can redeem. Every chat, solo or group, opens through this single
44/// always-host path. Returns the connected `ChatRoom`, the caller's `Presence`,
45/// the room `reference`, and the access `code` when shared.
46pub async fn open_chat(
47    authorization: &CredentialsOrApiKey,
48    options: OpenChatOptions,
49) -> Result<OpenedChat> {
50    let opened = open_room(
51        authorization,
52        OpenRoomOptions {
53            chat_id: options.chat_id,
54            messages: options.messages,
55            share: options.share,
56            write: None,
57        },
58    )
59    .await?;
60    let target = match &opened.code {
61        Some(code) if !code.is_empty() => ChatTarget::Code { code: code.clone() },
62        _ => ChatTarget::Reference {
63            reference: opened.reference.clone(),
64        },
65    };
66    let (room, me) = connect_room(authorization, &target, options.hooks).await?;
67    Ok(OpenedChat {
68        room,
69        me,
70        reference: opened.reference,
71        code: opened.code,
72    })
73}
74
75/// The `options` argument of `create_chat`.
76#[derive(Debug, Clone, Default)]
77pub struct CreateChatOptions {
78    /// People only for good: sends never start a model reply.
79    pub human: Option<bool>,
80    /// Instructs every model reply in the chat.
81    pub system_prompt: Option<String>,
82    /// Only you can delete it or move it to another group; it still goes away
83    /// with its group.
84    pub locked: Option<bool>,
85    /// The Termite repository Parrot works in during replies: yours for a
86    /// personal chat, the group's for a group chat.
87    pub repository: Option<Repository>,
88}
89
90/// Create a chat, owned by the caller or by `group`. `human` makes it a people
91/// only chat for good: sends never start a model reply. `system_prompt`
92/// instructs every model reply in the chat. `locked` lets only you delete it or
93/// move it to another group. `repository` is a Termite repository Parrot works
94/// in during replies. `name` defaults to `New Chat`.
95pub async fn create_chat(
96    authorization: &CredentialsOrApiKey,
97    name: Option<&str>,
98    group: Option<&Group>,
99    options: CreateChatOptions,
100) -> Result<String> {
101    let mut request = client()
102        .post(format!("{}/chat/create", super::get_url()))
103        .header(
104            "Authorization",
105            match authorization {
106                CredentialsOrApiKey::Credentials(credentials) => {
107                    format!("Token {}", credentials.token)
108                }
109                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
110            },
111        );
112    if let Some(group) = group {
113        request = request.header("Group", &group.id);
114    }
115    let response = request
116        .json(&json!({
117            "name": name.unwrap_or("New Chat"),
118            "human": options.human == Some(true),
119            "systemPrompt": options.system_prompt,
120            "locked": options.locked == Some(true),
121            "repository": options.repository.as_ref().map(|repository| repository.id),
122        }))
123        .send()
124        .await?;
125    let json: serde_json::Value = ok(response).await?.json().await?;
126    Ok(json["id"].as_str().unwrap_or_default().to_string())
127}
128
129pub async fn delete_chat(
130    authorization: &CredentialsOrApiKey,
131    id: &str,
132    group: Option<&Group>,
133) -> Result<()> {
134    let mut request = client()
135        .post(format!("{}/chat/delete", super::get_url()))
136        .header(
137            "Authorization",
138            match authorization {
139                CredentialsOrApiKey::Credentials(credentials) => {
140                    format!("Token {}", credentials.token)
141                }
142                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
143            },
144        );
145    if let Some(group) = group {
146        request = request.header("Group", &group.id);
147    }
148    let response = request.json(&json!({ "id": id })).send().await?;
149    ok(response).await?;
150    Ok(())
151}
152
153#[derive(Debug, Clone, Serialize)]
154#[serde(rename_all = "camelCase")]
155pub struct ChatSummary {
156    pub id: String,
157    pub name: String,
158    pub principal_id: String,
159    /// People only: sends never start a model reply.
160    pub human: bool,
161    /// Only whoever made it can delete it or move it.
162    pub locked: bool,
163    /// The repository Parrot works in, by id.
164    pub repository: Option<u64>,
165    /// Milliseconds since the epoch.
166    pub created: i64,
167    /// Milliseconds since the epoch.
168    pub updated: i64,
169    #[serde(skip_serializing)]
170    pub last: Option<Content>,
171    /// Messages the caller hasn't read. A person and each of their API keys keep
172    /// their own place; one that never marked the chat sees only its newest 20.
173    pub unread: u32,
174}
175
176impl ChatSummary {
177    pub fn from_json(json: &Value) -> ChatSummary {
178        let text = |value: &Value| match value {
179            Value::String(text) => text.clone(),
180            Value::Null => String::new(),
181            value => value.to_string(),
182        };
183        ChatSummary {
184            id: text(&json["id"]),
185            name: text(&json["name"]),
186            principal_id: text(&json["principalId"]),
187            human: json["human"] == true,
188            locked: json["locked"] == true,
189            repository: json["repository"].as_u64().filter(|id| *id > 0),
190            created: json["created"].as_f64().unwrap_or_default() as i64,
191            updated: json["updated"].as_f64().unwrap_or_default() as i64,
192            last: json["last"]
193                .is_object()
194                .then(|| Content::from_json(&json["last"], None)),
195            unread: json["unread"].as_u64().unwrap_or_default() as u32,
196        }
197    }
198}
199
200impl<'de> Deserialize<'de> for ChatSummary {
201    fn deserialize<D: serde::Deserializer<'de>>(
202        deserializer: D,
203    ) -> std::result::Result<Self, D::Error> {
204        Ok(ChatSummary::from_json(&Value::deserialize(deserializer)?))
205    }
206}
207
208pub async fn list_chats(
209    authorization: &CredentialsOrApiKey,
210    group: Option<&Group>,
211) -> Result<Vec<ChatSummary>> {
212    let mut request = client()
213        .get(format!("{}/chat/all", super::get_url()))
214        .header(
215            "Authorization",
216            match authorization {
217                CredentialsOrApiKey::Credentials(credentials) => {
218                    format!("Token {}", credentials.token)
219                }
220                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
221            },
222        );
223    if let Some(group) = group {
224        request = request.header("Group", &group.id);
225    }
226    let response = request.send().await?;
227    let rows: Vec<Value> = ok(response).await?.json().await?;
228    Ok(rows.iter().map(ChatSummary::from_json).collect())
229}
230
231pub async fn rename_chat(
232    authorization: &CredentialsOrApiKey,
233    id: &str,
234    name: &str,
235    group: Option<&Group>,
236) -> Result<()> {
237    let mut request = client()
238        .post(format!("{}/chat/rename", super::get_url()))
239        .header(
240            "Authorization",
241            match authorization {
242                CredentialsOrApiKey::Credentials(credentials) => {
243                    format!("Token {}", credentials.token)
244                }
245                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
246            },
247        );
248    if let Some(group) = group {
249        request = request.header("Group", &group.id);
250    }
251    let response = request
252        .json(&json!({ "id": id, "name": name }))
253        .send()
254        .await?;
255    ok(response).await?;
256    Ok(())
257}
258
259/// Mark a chat read for the caller, up to `position` (the next unread one) or
260/// all of it. Signed in with an API key, the key keeps its own place, apart from
261/// its person. Sending a message reads the chat for its sender. Returns what's
262/// left unread.
263pub async fn mark_read(
264    authorization: &CredentialsOrApiKey,
265    id: &str,
266    position: Option<u32>,
267    group: Option<&Group>,
268) -> Result<u32> {
269    let mut request = client()
270        .post(format!("{}/chat/mark-read", super::get_url()))
271        .header(
272            "Authorization",
273            match authorization {
274                CredentialsOrApiKey::Credentials(credentials) => {
275                    format!("Token {}", credentials.token)
276                }
277                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
278            },
279        );
280    if let Some(group) = group {
281        request = request.header("Group", &group.id);
282    }
283    let response = request
284        .json(&json!({ "id": id, "position": position }))
285        .send()
286        .await?;
287    let json: Value = ok(response).await?.json().await?;
288    Ok(json["unread"].as_u64().unwrap_or_default() as u32)
289}
290
291/// The rest of a draft reply in a chat, from its newest messages and how you write to the
292/// same people in its owner's other chats. Empty when the draft needs nothing more. It streams
293/// as the model writes it: `on_text` gets the rest so far each time more arrives.
294pub async fn autocomplete(
295    authorization: &CredentialsOrApiKey,
296    id: &str,
297    text: &str,
298    group: Option<&Group>,
299    on_text: Option<impl Fn(&str)>,
300) -> Result<String> {
301    let mut request = client()
302        .post(format!("{}/chat/autocomplete", super::get_url()))
303        .header(
304            "Authorization",
305            match authorization {
306                CredentialsOrApiKey::Credentials(credentials) => {
307                    format!("Token {}", credentials.token)
308                }
309                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
310            },
311        );
312    if let Some(group) = group {
313        request = request.header("Group", &group.id);
314    }
315    let response = request
316        .json(&json!({ "id": id, "text": text }))
317        .send()
318        .await?;
319    let response = ok(response).await?;
320
321    let mut pending = Vec::new();
322    let mut rest = String::new();
323    let mut body = response.bytes_stream();
324    while let Some(chunk) = body.next().await {
325        pending.extend_from_slice(&chunk?);
326        while let Some(end) = pending.windows(2).position(|pair| pair == b"\n\n") {
327            let event: Vec<u8> = pending.drain(..end + 2).collect();
328            for line in String::from_utf8_lossy(&event[..end]).lines() {
329                if let Some(data) = line.strip_prefix("data: ") {
330                    rest.push_str(&serde_json::from_str::<String>(data)?);
331                }
332            }
333            if let Some(on_text) = &on_text {
334                on_text(&rest);
335            }
336        }
337    }
338    Ok(rest)
339}
340
341fn hydrate_file_part(
342    authorization: &CredentialsOrApiKey,
343    part: &Value,
344    group: Option<&Group>,
345) -> Part {
346    let name = part["name"].as_str().unwrap_or("file");
347    let content_type = part["contentType"]
348        .as_str()
349        .unwrap_or("application/octet-stream");
350    let mut meta = part.clone();
351    meta["name"] = Value::from(name);
352    meta["contentType"] = Value::from(content_type);
353    if let Some(access_code) = part["accessCode"].as_str().filter(|code| !code.is_empty()) {
354        return Part::File(FilePart {
355            file: drive::File::from_json(
356                &meta,
357                DriveAuthorization::AccessCode {
358                    authorization: authorization.clone(),
359                    access_code: access_code.to_string(),
360                },
361            ),
362        });
363    }
364    if let Some(path) = part["path"].as_str().filter(|path| !path.is_empty()) {
365        return Part::File(FilePart {
366            file: drive::File::from_json(
367                &meta,
368                DriveAuthorization::Path {
369                    authorization: authorization.clone(),
370                    path: path.to_string(),
371                    group: part["group"]
372                        .as_str()
373                        .map(str::to_string)
374                        .or_else(|| group.map(|group| group.id.clone())),
375                },
376            ),
377        });
378    }
379    Part::File(FilePart {
380        file: drive::from_bytes(
381            authorization.clone(),
382            name,
383            content_type,
384            Vec::new(),
385            FileDates::default(),
386        ),
387    })
388}
389
390/// One page of a chat, newest first.
391pub async fn read_chat(
392    authorization: &CredentialsOrApiKey,
393    id: &str,
394    group: Option<&Group>,
395    query: PageQuery,
396) -> Result<Vec<Content>> {
397    let mut request = client()
398        .post(format!("{}/chat/read", super::get_url()))
399        .header(
400            "Authorization",
401            match authorization {
402                CredentialsOrApiKey::Credentials(credentials) => {
403                    format!("Token {}", credentials.token)
404                }
405                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
406            },
407        );
408    if let Some(group) = group {
409        request = request.header("Group", &group.id);
410    }
411    let response = request
412        .json(&json!({ "id": id, "cursor": query.cursor, "max": query.max }))
413        .send()
414        .await?;
415    let json: Value = ok(response).await?.json().await?;
416
417    let file = |part: &Value| hydrate_file_part(authorization, part, group);
418    let messages = json
419        .as_array()
420        .into_iter()
421        .flatten()
422        .map(|message| Content::from_json(message, Some(&file)))
423        .collect();
424    Ok(messages)
425}
426
427pub async fn write_chat(
428    authorization: &CredentialsOrApiKey,
429    id: &str,
430    messages: &[Content],
431    group: Option<&Group>,
432) -> Result<()> {
433    let mut serialized_messages = Vec::with_capacity(messages.len());
434    for message in messages {
435        let mut serialized_parts = Vec::with_capacity(message.parts.len());
436
437        for part in &message.parts {
438            match part {
439                Part::File(part) if !part.file.online() => {
440                    let file = &part.file;
441                    let dest = format!("tmp/{}-{}", uuid::Uuid::new_v4(), file.name());
442                    let bytes = file.clone().download(false, None).await?;
443                    let mut ready = drive::from_bytes(
444                        authorization.clone(),
445                        &dest,
446                        &file.content_type,
447                        bytes,
448                        FileDates::default(),
449                    );
450                    ready.upload(None).await?;
451                    serialized_parts.push(file_ref(&ready));
452                }
453                part => serialized_parts.push(part.wire()),
454            }
455        }
456
457        let mut wire = Map::new();
458        wire.insert("parts".to_string(), Value::Array(serialized_parts));
459        wire.insert("role".to_string(), serde_json::to_value(message.role)?);
460        if let Some(author) = &message.author {
461            wire.insert("author".to_string(), serde_json::to_value(author)?);
462        }
463        if let Some(model) = &message.model {
464            wire.insert("model".to_string(), Value::from(model.as_str()));
465        }
466        wire.insert(
467            "createdAt".to_string(),
468            Value::from(
469                message
470                    .created_at
471                    .to_rfc3339_opts(SecondsFormat::Millis, true),
472            ),
473        );
474        serialized_messages.push(Value::Object(wire));
475    }
476
477    let mut request = client()
478        .post(format!("{}/chat/write", super::get_url()))
479        .header(
480            "Authorization",
481            match authorization {
482                CredentialsOrApiKey::Credentials(credentials) => {
483                    format!("Token {}", credentials.token)
484                }
485                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
486            },
487        );
488    if let Some(group) = group {
489        request = request.header("Group", &group.id);
490    }
491    let response = request
492        .json(&json!({ "id": id, "messages": serialized_messages }))
493        .send()
494        .await?;
495    ok(response).await?;
496    Ok(())
497}