use std::collections::HashMap;
use std::ops::Deref;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};
use super::scraper::Scraper;
use crate::authentification::CredentialsOrApiKey;
use crate::error::{Error, Result};
use crate::groups::Group;
use crate::http::{client, ok};
use crate::parrot::room::{ChatRoom, RoomHooks, connect_room};
use crate::parrot::share::ChatTarget;
use crate::shared::server::{Authorization, Hooks, Presence, Room, connect as connect_server};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WorkspaceFile {
pub name: String,
pub content_type: String,
pub size: u64,
pub key: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WorkspaceDatasetMeta {
pub id: String,
pub name: String,
#[serde(default)]
pub schema: Value,
#[serde(default)]
pub termination: Vec<Value>,
#[serde(default)]
pub content_types: Vec<String>,
#[serde(default)]
pub proxy: String,
#[serde(default)]
pub document_modes: Vec<String>,
#[serde(default)]
pub created_at: i64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WorkspaceSummary {
pub id: String,
pub name: String,
pub description: String,
pub created_at: i64,
pub datasets: u64,
pub files: u64,
}
#[derive(Debug, Clone, Default)]
pub struct WorkspaceOptions {
pub group: Option<Group>,
}
async fn post(
authorization: &CredentialsOrApiKey,
route: &str,
body: Option<Value>,
group: Option<&Group>,
) -> Result<reqwest::Response> {
let mut request = client()
.post(format!("{}{route}", super::get_url()))
.header("Content-Type", "application/json")
.header(
"Authorization",
match authorization {
CredentialsOrApiKey::Credentials(credentials) => {
format!("Token {}", credentials.token)
}
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
},
);
if let Some(group) = group {
request = request.header("Group", &group.id);
}
if let Some(body) = body {
request = request.json(&body);
}
ok(request.send().await?).await
}
pub async fn list_workspaces(
authorization: &CredentialsOrApiKey,
opts: WorkspaceOptions,
) -> Result<Vec<WorkspaceSummary>> {
let response = post(
authorization,
"/marmot/workspace/list",
None,
opts.group.as_ref(),
)
.await?;
Ok(response.json().await?)
}
pub async fn create_workspace(
authorization: &CredentialsOrApiKey,
name: &str,
description: Option<&str>,
opts: WorkspaceOptions,
) -> Result<String> {
let response = post(
authorization,
"/marmot/workspace/create",
Some(json!({ "name": name, "description": description.unwrap_or_default() })),
opts.group.as_ref(),
)
.await?;
Ok(response.text().await?)
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct PartialWorkspace {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
pub async fn update_workspace(
authorization: &CredentialsOrApiKey,
id: &str,
update: &PartialWorkspace,
opts: WorkspaceOptions,
) -> Result<()> {
let mut body = Map::new();
body.insert("id".to_string(), Value::from(id));
if let Value::Object(fields) = serde_json::to_value(update)? {
body.extend(fields);
}
post(
authorization,
"/marmot/workspace/update",
Some(Value::Object(body)),
opts.group.as_ref(),
)
.await?;
Ok(())
}
pub async fn remove_workspace(
authorization: &CredentialsOrApiKey,
id: &str,
opts: WorkspaceOptions,
) -> Result<()> {
post(
authorization,
"/marmot/workspace/remove",
Some(json!({ "id": id })),
opts.group.as_ref(),
)
.await?;
Ok(())
}
struct Workspace {
workspace_id: String,
name: String,
description: String,
files: HashMap<String, WorkspaceFile>,
datasets: HashMap<String, WorkspaceDatasetMeta>,
scrapers: HashMap<String, Scraper>,
repository_id: Option<u64>,
agent_text: String,
}
#[derive(Clone)]
pub struct WorkspaceChat {
pub room: ChatRoom,
pub me: Presence,
pub chat_id: String,
}
#[derive(Default)]
pub struct ConnectOptions {
pub group: Option<Group>,
pub hooks: Option<Arc<dyn Hooks>>,
pub server: Option<String>,
}
#[derive(Clone)]
pub struct WorkspaceRoom {
room: Room,
state: Arc<Mutex<Workspace>>,
}
impl Deref for WorkspaceRoom {
type Target = Room;
fn deref(&self) -> &Room {
&self.room
}
}
fn field<'a>(packet: &'a Value, key: &str) -> &'a Value {
if packet[key].is_null() {
&packet["data"][key]
} else {
&packet[key]
}
}
fn number_or_none(value: &Value) -> Option<u64> {
match value {
Value::Null | Value::Bool(false) => None,
Value::Number(number) if number.as_f64() == Some(0.0) => None,
Value::String(text) if text.is_empty() => None,
value => Some(crate::marmot::browser::js_number(Some(value)) as u64),
}
}
impl WorkspaceRoom {
fn workspace(&self) -> MutexGuard<'_, Workspace> {
self.state.lock().unwrap_or_else(PoisonError::into_inner)
}
pub fn workspace_id(&self) -> String {
self.workspace().workspace_id.clone()
}
pub fn name(&self) -> String {
self.workspace().name.clone()
}
pub fn description(&self) -> String {
self.workspace().description.clone()
}
pub fn files(&self) -> HashMap<String, WorkspaceFile> {
self.workspace().files.clone()
}
pub fn datasets(&self) -> HashMap<String, WorkspaceDatasetMeta> {
self.workspace().datasets.clone()
}
pub fn scrapers(&self) -> HashMap<String, Scraper> {
self.workspace().scrapers.clone()
}
pub fn repository_id(&self) -> Option<u64> {
self.workspace().repository_id
}
pub fn agent_text(&self) -> String {
self.workspace().agent_text.clone()
}
pub fn hydrate(&self, state: &Value) {
let mut workspace = self.workspace();
if let Some(id) = state["id"].as_str() {
workspace.workspace_id = id.to_string();
}
if let Some(name) = state["name"].as_str() {
workspace.name = name.to_string();
}
if let Some(description) = state["description"].as_str() {
workspace.description = description.to_string();
}
workspace.files = state["files"]
.as_array()
.into_iter()
.flatten()
.filter(|file| file["key"].as_str().is_some_and(|key| !key.is_empty()))
.filter_map(|file| serde_json::from_value::<WorkspaceFile>(file.clone()).ok())
.map(|file| (file.key.clone(), file))
.collect();
workspace.datasets = state["datasets"]
.as_array()
.into_iter()
.flatten()
.filter_map(|dataset| {
serde_json::from_value::<WorkspaceDatasetMeta>(dataset.clone()).ok()
})
.filter(|dataset| !dataset.id.is_empty())
.map(|dataset| (dataset.id.clone(), dataset))
.collect();
workspace.scrapers = state["scrapers"]
.as_array()
.into_iter()
.flatten()
.filter(|scraper| !scraper["datasetId"].is_null() && scraper["datasetId"] != "")
.map(Scraper::from_json)
.map(|scraper| (scraper.dataset_id.clone(), scraper))
.collect();
workspace.repository_id = number_or_none(&state["repositoryId"]);
workspace.agent_text = state["agentText"].as_str().unwrap_or_default().to_string();
}
pub fn handle_event(&self, packet: &Value) {
let mut workspace = self.workspace();
match packet["event"].as_str().unwrap_or_default() {
"upsert-file" => {
if let Ok(file) =
serde_json::from_value::<WorkspaceFile>(field(packet, "file").clone())
&& !file.key.is_empty()
{
workspace.files.insert(file.key.clone(), file);
}
}
"remove-file" => {
if let Some(key) = field(packet, "key").as_str().filter(|key| !key.is_empty()) {
workspace.files.remove(key);
}
}
"upsert-dataset" => {
if let Ok(dataset) =
serde_json::from_value::<WorkspaceDatasetMeta>(field(packet, "dataset").clone())
&& !dataset.id.is_empty()
{
workspace.datasets.insert(dataset.id.clone(), dataset);
}
}
"remove-dataset" => {
if let Some(id) = field(packet, "id").as_str().filter(|id| !id.is_empty()) {
workspace.datasets.remove(id);
}
}
"upsert-scraper" => {
let scraper = field(packet, "scraper");
if !scraper["datasetId"].is_null() && scraper["datasetId"] != "" {
let scraper = Scraper::from_json(scraper);
workspace
.scrapers
.insert(scraper.dataset_id.clone(), scraper);
}
}
"remove-scraper" => {
let dataset_id = field(packet, "datasetId");
if !dataset_id.is_null() && dataset_id != "" {
let dataset_id = crate::marmot::browser::js_string(Some(dataset_id), "");
workspace.scrapers.remove(&dataset_id);
}
}
"update-workspace" => {
let update = field(packet, "workspace");
if let Some(name) = update["name"].as_str().filter(|name| !name.is_empty()) {
workspace.name = name.to_string();
}
if let Some(description) = update.get("description") {
workspace.description = description.as_str().unwrap_or_default().to_string();
}
if let Some(repository_id) = update.get("repositoryId") {
workspace.repository_id = number_or_none(repository_id);
}
if let Some(agent_text) = update.get("agentText") {
workspace.agent_text = agent_text.as_str().unwrap_or_default().to_string();
}
}
_ => {}
}
}
pub async fn get_chat(&self, hooks: Option<Arc<dyn RoomHooks>>) -> Result<WorkspaceChat> {
let authorization = self.get_authorization();
let request = client()
.post(format!("{}/marmot/workspace/chat/ensure", super::get_url()))
.header("Content-Type", "application/json")
.header(
"Authorization",
match authorization {
CredentialsOrApiKey::Credentials(credentials) => {
format!("Token {}", credentials.token)
}
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
},
);
let request = match self.authorization() {
Authorization::AccessCode { access_code, .. } => {
request.header("Access-Code", access_code)
}
Authorization::Server { group, .. } => {
let request = request.header("Server", self.workspace_id());
match group {
Some(group) => request.header("Group", group),
None => request,
}
}
};
let json: Value = ok(request.send().await?).await?.json().await?;
let chat_id = json["chatId"].as_str().unwrap_or_default().to_string();
let code = json["code"].as_str().unwrap_or_default().to_string();
if chat_id.is_empty() || code.is_empty() {
return Err(Error::Message("Invalid ensure response.".to_string()));
}
let target = match self.authorization() {
Authorization::AccessCode { .. } => ChatTarget::Code { code },
Authorization::Server { .. } => ChatTarget::Reference {
reference: json["reference"]
.as_str()
.filter(|reference| !reference.is_empty())
.map(str::to_string)
.unwrap_or_else(|| format!("parrot:chat:{chat_id}")),
},
};
let (room, me) = connect_room(authorization, &target, hooks).await?;
Ok(WorkspaceChat { room, me, chat_id })
}
pub async fn connect(
authorization: &CredentialsOrApiKey,
target_id: &str,
opts: ConnectOptions,
) -> Result<(WorkspaceRoom, Presence)> {
let server = opts.server.unwrap_or_else(|| target_id.to_string());
WorkspaceRoom::open(
Authorization::Server {
authorization: authorization.clone(),
server,
group: opts.group.as_ref().map(|group| group.id.clone()),
},
opts.hooks,
)
.await
}
pub async fn join(
authorization: &CredentialsOrApiKey,
access_code: &str,
hooks: Option<Arc<dyn Hooks>>,
) -> Result<(WorkspaceRoom, Presence)> {
WorkspaceRoom::open(
Authorization::AccessCode {
authorization: authorization.clone(),
access_code: access_code.to_string(),
},
hooks,
)
.await
}
async fn open(
auth: Authorization,
hooks: Option<Arc<dyn Hooks>>,
) -> Result<(WorkspaceRoom, Presence)> {
let state = Arc::new(Mutex::new(Workspace {
workspace_id: String::new(),
name: String::new(),
description: String::new(),
files: HashMap::new(),
datasets: HashMap::new(),
scrapers: HashMap::new(),
repository_id: None,
agent_text: String::new(),
}));
let me = connect_server(
auth,
&format!("{}/marmot/workspace", super::get_url()),
Arc::new(WorkspaceHooks {
state: state.clone(),
user: hooks.unwrap_or_else(|| Arc::new(())),
}),
)
.await?;
let room = WorkspaceRoom {
room: me.room().clone(),
state,
};
Ok((room, me))
}
}
struct WorkspaceHooks {
state: Arc<Mutex<Workspace>>,
user: Arc<dyn Hooks>,
}
impl WorkspaceHooks {
fn room(&self, room: &Room) -> WorkspaceRoom {
WorkspaceRoom {
room: room.clone(),
state: self.state.clone(),
}
}
}
impl Hooks for WorkspaceHooks {
fn create_room(&self, room: &Room, state: &Value) {
self.state
.lock()
.unwrap_or_else(PoisonError::into_inner)
.workspace_id = room.id().to_string();
self.user.create_room(room, state);
}
fn create_presence(&self, room: &Room, presence: &Presence) {
self.user.create_presence(room, presence);
}
fn remove_presence(&self, room: &Room, presence: &Presence) {
self.user.remove_presence(room, presence);
}
fn on_connection(&self, room: &Room, state: &Value) {
self.room(room).hydrate(state);
self.user.on_connection(room, state);
}
fn on_event(&self, room: &Room, packet: &Value) -> bool {
self.room(room).handle_event(packet);
self.user.on_event(room, packet);
true
}
fn on_presence(&self, room: &Room) {
self.user.on_presence(room);
}
fn on_closed(&self, room: &Room, reason: Option<&str>) {
self.user.on_closed(room, reason);
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use tokio::sync::mpsc;
use super::*;
use crate::authentification::ApiKey;
#[test]
fn workspace_rooms_follow_their_snapshot_and_events() {
let (ws, _outbox) = mpsc::unbounded_channel();
let room = WorkspaceRoom {
room: Room::new(
"http://localhost/marmot/workspace".to_string(),
ws,
Authorization::Server {
authorization: CredentialsOrApiKey::ApiKey(ApiKey::default()),
server: "w1".to_string(),
group: None,
},
"w1".to_string(),
),
state: Arc::new(Mutex::new(Workspace {
workspace_id: "w1".to_string(),
name: String::new(),
description: String::new(),
files: HashMap::new(),
datasets: HashMap::new(),
scrapers: HashMap::new(),
repository_id: None,
agent_text: String::new(),
})),
};
room.hydrate(&json!({
"name": "Prices",
"files": [{ "name": "a.csv", "contentType": "text/csv", "size": 3, "key": "k1" }, { "name": "x" }],
"datasets": [{ "id": "d1", "name": "Shoes", "schema": {}, "termination": [], "contentTypes": [], "proxy": "datacenter", "documentModes": [], "createdAt": 1 }],
"scrapers": [{ "datasetId": "d1", "workspaceId": "w1", "status": "active" }],
"repositoryId": "7",
"agentText": "gpt-5.4"
}));
assert_eq!(room.name(), "Prices");
assert_eq!(room.files().len(), 1);
assert_eq!(room.datasets()["d1"].name, "Shoes");
assert_eq!(room.repository_id(), Some(7));
room.handle_event(&json!({ "event": "remove-file", "data": { "key": "k1" } }));
room.handle_event(&json!({ "event": "remove-scraper", "datasetId": "d1" }));
room.handle_event(&json!({ "event": "update-workspace", "data": { "workspace": { "name": "Deals", "repositoryId": null } } }));
assert!(room.files().is_empty());
assert!(room.scrapers().is_empty());
assert_eq!(room.name(), "Deals");
assert_eq!(room.repository_id(), None);
}
}