Skip to main content

fauna_api/marmot/
workspace.rs

1use std::collections::HashMap;
2use std::ops::Deref;
3use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
4
5use serde::{Deserialize, Serialize};
6use serde_json::{Map, Value, json};
7
8use super::scraper::Scraper;
9use crate::authentification::CredentialsOrApiKey;
10use crate::error::{Error, Result};
11use crate::groups::Group;
12use crate::http::{client, ok};
13use crate::parrot::room::{ChatRoom, RoomHooks, connect_room};
14use crate::parrot::share::ChatTarget;
15use crate::shared::server::{Authorization, Hooks, Presence, Room, connect as connect_server};
16
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18#[serde(rename_all = "camelCase")]
19pub struct WorkspaceFile {
20    pub name: String,
21    pub content_type: String,
22    pub size: u64,
23    pub key: String,
24}
25
26#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
27#[serde(rename_all = "camelCase")]
28pub struct WorkspaceDatasetMeta {
29    pub id: String,
30    pub name: String,
31    #[serde(default)]
32    pub schema: Value,
33    #[serde(default)]
34    pub termination: Vec<Value>,
35    #[serde(default)]
36    pub content_types: Vec<String>,
37    #[serde(default)]
38    pub proxy: String,
39    #[serde(default)]
40    pub document_modes: Vec<String>,
41    #[serde(default)]
42    pub created_at: i64,
43}
44
45#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
46#[serde(rename_all = "camelCase")]
47pub struct WorkspaceSummary {
48    pub id: String,
49    pub name: String,
50    pub description: String,
51    pub created_at: i64,
52    pub datasets: u64,
53    pub files: u64,
54}
55
56/// The `opts` argument of the workspace calls: `group` acts on that group's workspaces.
57#[derive(Debug, Clone, Default)]
58pub struct WorkspaceOptions {
59    pub group: Option<Group>,
60}
61
62async fn post(
63    authorization: &CredentialsOrApiKey,
64    route: &str,
65    body: Option<Value>,
66    group: Option<&Group>,
67) -> Result<reqwest::Response> {
68    let mut request = client()
69        .post(format!("{}{route}", super::get_url()))
70        .header("Content-Type", "application/json")
71        .header(
72            "Authorization",
73            match authorization {
74                CredentialsOrApiKey::Credentials(credentials) => {
75                    format!("Token {}", credentials.token)
76                }
77                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
78            },
79        );
80    if let Some(group) = group {
81        request = request.header("Group", &group.id);
82    }
83    if let Some(body) = body {
84        request = request.json(&body);
85    }
86    ok(request.send().await?).await
87}
88
89pub async fn list_workspaces(
90    authorization: &CredentialsOrApiKey,
91    opts: WorkspaceOptions,
92) -> Result<Vec<WorkspaceSummary>> {
93    let response = post(
94        authorization,
95        "/marmot/workspace/list",
96        None,
97        opts.group.as_ref(),
98    )
99    .await?;
100    Ok(response.json().await?)
101}
102
103/// `description` defaults to empty.
104pub async fn create_workspace(
105    authorization: &CredentialsOrApiKey,
106    name: &str,
107    description: Option<&str>,
108    opts: WorkspaceOptions,
109) -> Result<String> {
110    let response = post(
111        authorization,
112        "/marmot/workspace/create",
113        Some(json!({ "name": name, "description": description.unwrap_or_default() })),
114        opts.group.as_ref(),
115    )
116    .await?;
117    Ok(response.text().await?)
118}
119
120/// `Partial<{ name, description }>` in the TypeScript SDK.
121#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
122pub struct PartialWorkspace {
123    #[serde(default, skip_serializing_if = "Option::is_none")]
124    pub name: Option<String>,
125    #[serde(default, skip_serializing_if = "Option::is_none")]
126    pub description: Option<String>,
127}
128
129pub async fn update_workspace(
130    authorization: &CredentialsOrApiKey,
131    id: &str,
132    update: &PartialWorkspace,
133    opts: WorkspaceOptions,
134) -> Result<()> {
135    let mut body = Map::new();
136    body.insert("id".to_string(), Value::from(id));
137    if let Value::Object(fields) = serde_json::to_value(update)? {
138        body.extend(fields);
139    }
140    post(
141        authorization,
142        "/marmot/workspace/update",
143        Some(Value::Object(body)),
144        opts.group.as_ref(),
145    )
146    .await?;
147    Ok(())
148}
149
150pub async fn remove_workspace(
151    authorization: &CredentialsOrApiKey,
152    id: &str,
153    opts: WorkspaceOptions,
154) -> Result<()> {
155    post(
156        authorization,
157        "/marmot/workspace/remove",
158        Some(json!({ "id": id })),
159        opts.group.as_ref(),
160    )
161    .await?;
162    Ok(())
163}
164
165struct Workspace {
166    workspace_id: String,
167    name: String,
168    description: String,
169    files: HashMap<String, WorkspaceFile>,
170    datasets: HashMap<String, WorkspaceDatasetMeta>,
171    scrapers: HashMap<String, Scraper>,
172    repository_id: Option<u64>,
173    agent_text: String,
174}
175
176/// `{ room, self, chatId }` in the TypeScript SDK, with `self` as `me`.
177#[derive(Clone)]
178pub struct WorkspaceChat {
179    pub room: ChatRoom,
180    pub me: Presence,
181    pub chat_id: String,
182}
183
184/// The `opts` argument of `WorkspaceRoom::connect`. `server` names the room when
185/// it isn't the workspace's id.
186#[derive(Default)]
187pub struct ConnectOptions {
188    pub group: Option<Group>,
189    pub hooks: Option<Arc<dyn Hooks>>,
190    pub server: Option<String>,
191}
192
193#[derive(Clone)]
194pub struct WorkspaceRoom {
195    room: Room,
196    state: Arc<Mutex<Workspace>>,
197}
198
199impl Deref for WorkspaceRoom {
200    type Target = Room;
201    fn deref(&self) -> &Room {
202        &self.room
203    }
204}
205
206fn field<'a>(packet: &'a Value, key: &str) -> &'a Value {
207    if packet[key].is_null() {
208        &packet["data"][key]
209    } else {
210        &packet[key]
211    }
212}
213
214fn number_or_none(value: &Value) -> Option<u64> {
215    match value {
216        Value::Null | Value::Bool(false) => None,
217        Value::Number(number) if number.as_f64() == Some(0.0) => None,
218        Value::String(text) if text.is_empty() => None,
219        value => Some(crate::marmot::browser::js_number(Some(value)) as u64),
220    }
221}
222
223impl WorkspaceRoom {
224    fn workspace(&self) -> MutexGuard<'_, Workspace> {
225        self.state.lock().unwrap_or_else(PoisonError::into_inner)
226    }
227
228    pub fn workspace_id(&self) -> String {
229        self.workspace().workspace_id.clone()
230    }
231
232    pub fn name(&self) -> String {
233        self.workspace().name.clone()
234    }
235
236    pub fn description(&self) -> String {
237        self.workspace().description.clone()
238    }
239
240    pub fn files(&self) -> HashMap<String, WorkspaceFile> {
241        self.workspace().files.clone()
242    }
243
244    pub fn datasets(&self) -> HashMap<String, WorkspaceDatasetMeta> {
245        self.workspace().datasets.clone()
246    }
247
248    /// Kept datasets, by dataset id.
249    pub fn scrapers(&self) -> HashMap<String, Scraper> {
250        self.workspace().scrapers.clone()
251    }
252
253    /// The Termite repository whose machine runs the workspace's scrapers, once there is one.
254    pub fn repository_id(&self) -> Option<u64> {
255        self.workspace().repository_id
256    }
257
258    /// The model the workspace's scraper agent thinks with.
259    pub fn agent_text(&self) -> String {
260        self.workspace().agent_text.clone()
261    }
262
263    pub fn hydrate(&self, state: &Value) {
264        let mut workspace = self.workspace();
265        if let Some(id) = state["id"].as_str() {
266            workspace.workspace_id = id.to_string();
267        }
268        if let Some(name) = state["name"].as_str() {
269            workspace.name = name.to_string();
270        }
271        if let Some(description) = state["description"].as_str() {
272            workspace.description = description.to_string();
273        }
274        workspace.files = state["files"]
275            .as_array()
276            .into_iter()
277            .flatten()
278            .filter(|file| file["key"].as_str().is_some_and(|key| !key.is_empty()))
279            .filter_map(|file| serde_json::from_value::<WorkspaceFile>(file.clone()).ok())
280            .map(|file| (file.key.clone(), file))
281            .collect();
282        workspace.datasets = state["datasets"]
283            .as_array()
284            .into_iter()
285            .flatten()
286            .filter_map(|dataset| {
287                serde_json::from_value::<WorkspaceDatasetMeta>(dataset.clone()).ok()
288            })
289            .filter(|dataset| !dataset.id.is_empty())
290            .map(|dataset| (dataset.id.clone(), dataset))
291            .collect();
292        workspace.scrapers = state["scrapers"]
293            .as_array()
294            .into_iter()
295            .flatten()
296            .filter(|scraper| !scraper["datasetId"].is_null() && scraper["datasetId"] != "")
297            .map(Scraper::from_json)
298            .map(|scraper| (scraper.dataset_id.clone(), scraper))
299            .collect();
300        workspace.repository_id = number_or_none(&state["repositoryId"]);
301        workspace.agent_text = state["agentText"].as_str().unwrap_or_default().to_string();
302    }
303
304    pub fn handle_event(&self, packet: &Value) {
305        let mut workspace = self.workspace();
306        match packet["event"].as_str().unwrap_or_default() {
307            "upsert-file" => {
308                if let Ok(file) =
309                    serde_json::from_value::<WorkspaceFile>(field(packet, "file").clone())
310                    && !file.key.is_empty()
311                {
312                    workspace.files.insert(file.key.clone(), file);
313                }
314            }
315            "remove-file" => {
316                if let Some(key) = field(packet, "key").as_str().filter(|key| !key.is_empty()) {
317                    workspace.files.remove(key);
318                }
319            }
320            "upsert-dataset" => {
321                if let Ok(dataset) =
322                    serde_json::from_value::<WorkspaceDatasetMeta>(field(packet, "dataset").clone())
323                    && !dataset.id.is_empty()
324                {
325                    workspace.datasets.insert(dataset.id.clone(), dataset);
326                }
327            }
328            "remove-dataset" => {
329                if let Some(id) = field(packet, "id").as_str().filter(|id| !id.is_empty()) {
330                    workspace.datasets.remove(id);
331                }
332            }
333            "upsert-scraper" => {
334                let scraper = field(packet, "scraper");
335                if !scraper["datasetId"].is_null() && scraper["datasetId"] != "" {
336                    let scraper = Scraper::from_json(scraper);
337                    workspace
338                        .scrapers
339                        .insert(scraper.dataset_id.clone(), scraper);
340                }
341            }
342            "remove-scraper" => {
343                let dataset_id = field(packet, "datasetId");
344                if !dataset_id.is_null() && dataset_id != "" {
345                    let dataset_id = crate::marmot::browser::js_string(Some(dataset_id), "");
346                    workspace.scrapers.remove(&dataset_id);
347                }
348            }
349            "update-workspace" => {
350                let update = field(packet, "workspace");
351                if let Some(name) = update["name"].as_str().filter(|name| !name.is_empty()) {
352                    workspace.name = name.to_string();
353                }
354                if let Some(description) = update.get("description") {
355                    workspace.description = description.as_str().unwrap_or_default().to_string();
356                }
357                if let Some(repository_id) = update.get("repositoryId") {
358                    workspace.repository_id = number_or_none(repository_id);
359                }
360                if let Some(agent_text) = update.get("agentText") {
361                    workspace.agent_text = agent_text.as_str().unwrap_or_default().to_string();
362                }
363            }
364            _ => {}
365        }
366    }
367
368    pub async fn get_chat(&self, hooks: Option<Arc<dyn RoomHooks>>) -> Result<WorkspaceChat> {
369        let authorization = self.get_authorization();
370        let request = client()
371            .post(format!("{}/marmot/workspace/chat/ensure", super::get_url()))
372            .header("Content-Type", "application/json")
373            .header(
374                "Authorization",
375                match authorization {
376                    CredentialsOrApiKey::Credentials(credentials) => {
377                        format!("Token {}", credentials.token)
378                    }
379                    CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
380                },
381            );
382        let request = match self.authorization() {
383            Authorization::AccessCode { access_code, .. } => {
384                request.header("Access-Code", access_code)
385            }
386            Authorization::Server { group, .. } => {
387                let request = request.header("Server", self.workspace_id());
388                match group {
389                    Some(group) => request.header("Group", group),
390                    None => request,
391                }
392            }
393        };
394        let json: Value = ok(request.send().await?).await?.json().await?;
395        let chat_id = json["chatId"].as_str().unwrap_or_default().to_string();
396        let code = json["code"].as_str().unwrap_or_default().to_string();
397        if chat_id.is_empty() || code.is_empty() {
398            return Err(Error::Message("Invalid ensure response.".to_string()));
399        }
400
401        let target = match self.authorization() {
402            Authorization::AccessCode { .. } => ChatTarget::Code { code },
403            Authorization::Server { .. } => ChatTarget::Reference {
404                reference: json["reference"]
405                    .as_str()
406                    .filter(|reference| !reference.is_empty())
407                    .map(str::to_string)
408                    .unwrap_or_else(|| format!("parrot:chat:{chat_id}")),
409            },
410        };
411        let (room, me) = connect_room(authorization, &target, hooks).await?;
412        Ok(WorkspaceChat { room, me, chat_id })
413    }
414
415    pub async fn connect(
416        authorization: &CredentialsOrApiKey,
417        target_id: &str,
418        opts: ConnectOptions,
419    ) -> Result<(WorkspaceRoom, Presence)> {
420        let server = opts.server.unwrap_or_else(|| target_id.to_string());
421        WorkspaceRoom::open(
422            Authorization::Server {
423                authorization: authorization.clone(),
424                server,
425                group: opts.group.as_ref().map(|group| group.id.clone()),
426            },
427            opts.hooks,
428        )
429        .await
430    }
431
432    pub async fn join(
433        authorization: &CredentialsOrApiKey,
434        access_code: &str,
435        hooks: Option<Arc<dyn Hooks>>,
436    ) -> Result<(WorkspaceRoom, Presence)> {
437        WorkspaceRoom::open(
438            Authorization::AccessCode {
439                authorization: authorization.clone(),
440                access_code: access_code.to_string(),
441            },
442            hooks,
443        )
444        .await
445    }
446
447    async fn open(
448        auth: Authorization,
449        hooks: Option<Arc<dyn Hooks>>,
450    ) -> Result<(WorkspaceRoom, Presence)> {
451        let state = Arc::new(Mutex::new(Workspace {
452            workspace_id: String::new(),
453            name: String::new(),
454            description: String::new(),
455            files: HashMap::new(),
456            datasets: HashMap::new(),
457            scrapers: HashMap::new(),
458            repository_id: None,
459            agent_text: String::new(),
460        }));
461        let me = connect_server(
462            auth,
463            &format!("{}/marmot/workspace", super::get_url()),
464            Arc::new(WorkspaceHooks {
465                state: state.clone(),
466                user: hooks.unwrap_or_else(|| Arc::new(())),
467            }),
468        )
469        .await?;
470        let room = WorkspaceRoom {
471            room: me.room().clone(),
472            state,
473        };
474        Ok((room, me))
475    }
476}
477
478struct WorkspaceHooks {
479    state: Arc<Mutex<Workspace>>,
480    user: Arc<dyn Hooks>,
481}
482
483impl WorkspaceHooks {
484    fn room(&self, room: &Room) -> WorkspaceRoom {
485        WorkspaceRoom {
486            room: room.clone(),
487            state: self.state.clone(),
488        }
489    }
490}
491
492impl Hooks for WorkspaceHooks {
493    fn create_room(&self, room: &Room, state: &Value) {
494        self.state
495            .lock()
496            .unwrap_or_else(PoisonError::into_inner)
497            .workspace_id = room.id().to_string();
498        self.user.create_room(room, state);
499    }
500
501    fn create_presence(&self, room: &Room, presence: &Presence) {
502        self.user.create_presence(room, presence);
503    }
504
505    fn remove_presence(&self, room: &Room, presence: &Presence) {
506        self.user.remove_presence(room, presence);
507    }
508
509    fn on_connection(&self, room: &Room, state: &Value) {
510        self.room(room).hydrate(state);
511        self.user.on_connection(room, state);
512    }
513
514    fn on_event(&self, room: &Room, packet: &Value) -> bool {
515        self.room(room).handle_event(packet);
516        self.user.on_event(room, packet);
517        true
518    }
519
520    fn on_presence(&self, room: &Room) {
521        self.user.on_presence(room);
522    }
523
524    fn on_closed(&self, room: &Room, reason: Option<&str>) {
525        self.user.on_closed(room, reason);
526    }
527}
528
529#[cfg(test)]
530mod tests {
531    use serde_json::json;
532    use tokio::sync::mpsc;
533
534    use super::*;
535    use crate::authentification::ApiKey;
536
537    #[test]
538    fn workspace_rooms_follow_their_snapshot_and_events() {
539        let (ws, _outbox) = mpsc::unbounded_channel();
540        let room = WorkspaceRoom {
541            room: Room::new(
542                "http://localhost/marmot/workspace".to_string(),
543                ws,
544                Authorization::Server {
545                    authorization: CredentialsOrApiKey::ApiKey(ApiKey::default()),
546                    server: "w1".to_string(),
547                    group: None,
548                },
549                "w1".to_string(),
550            ),
551            state: Arc::new(Mutex::new(Workspace {
552                workspace_id: "w1".to_string(),
553                name: String::new(),
554                description: String::new(),
555                files: HashMap::new(),
556                datasets: HashMap::new(),
557                scrapers: HashMap::new(),
558                repository_id: None,
559                agent_text: String::new(),
560            })),
561        };
562        room.hydrate(&json!({
563            "name": "Prices",
564            "files": [{ "name": "a.csv", "contentType": "text/csv", "size": 3, "key": "k1" }, { "name": "x" }],
565            "datasets": [{ "id": "d1", "name": "Shoes", "schema": {}, "termination": [], "contentTypes": [], "proxy": "datacenter", "documentModes": [], "createdAt": 1 }],
566            "scrapers": [{ "datasetId": "d1", "workspaceId": "w1", "status": "active" }],
567            "repositoryId": "7",
568            "agentText": "gpt-5.4"
569        }));
570        assert_eq!(room.name(), "Prices");
571        assert_eq!(room.files().len(), 1);
572        assert_eq!(room.datasets()["d1"].name, "Shoes");
573        assert_eq!(room.repository_id(), Some(7));
574
575        room.handle_event(&json!({ "event": "remove-file", "data": { "key": "k1" } }));
576        room.handle_event(&json!({ "event": "remove-scraper", "datasetId": "d1" }));
577        room.handle_event(&json!({ "event": "update-workspace", "data": { "workspace": { "name": "Deals", "repositoryId": null } } }));
578        assert!(room.files().is_empty());
579        assert!(room.scrapers().is_empty());
580        assert_eq!(room.name(), "Deals");
581        assert_eq!(room.repository_id(), None);
582    }
583}