use std::collections::{HashMap, HashSet};
use std::ops::Deref;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::time::Duration;
use chrono::{DateTime, Utc};
use futures_util::future::BoxFuture;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};
use tokio::sync::oneshot;
use super::versioning::Repository;
use crate::authentification::CredentialsOrApiKey;
use crate::billing::PageQuery;
use crate::error::{Error, Result};
use crate::groups::Group;
use crate::marmot::browser::{
BrowserRoomHooks, BrowserTool, PageRoom, js_number, js_string,
parrot_tools as browser_parrot_tools, present, tool,
};
use crate::marmot::web::{BootstrapMeta, HtmlPointer};
use crate::parrot::generative::{Function, Tool, Usage};
use crate::shared::schema::Schema;
use crate::shared::server::{Authorization, Hooks, Presence, Room, connect as connect_server};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WindowApp {
Web,
Files,
File,
Terminal,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum WindowTool {
Windows,
Focus {
window: f64,
},
Place {
window: f64,
x: f64,
y: f64,
},
Resize {
window: f64,
width: f64,
height: f64,
},
Close {
window: f64,
},
Reload {
window: f64,
},
Open {
app: WindowApp,
#[serde(default, skip_serializing_if = "Option::is_none")]
url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
command: Option<String>,
},
Key {
keys: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
window: Option<f64>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum GrepOutput {
Files,
Lines,
Count,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
tag = "kind",
rename_all = "lowercase",
rename_all_fields = "camelCase"
)]
pub enum FileTool {
List {
#[serde(default, skip_serializing_if = "Option::is_none")]
path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pattern: Option<String>,
},
Grep {
pattern: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
glob: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
ignore_case: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
context: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
output: Option<GrepOutput>,
#[serde(default, skip_serializing_if = "Option::is_none")]
max: Option<f64>,
},
Read {
path: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
line: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
before: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
after: Option<f64>,
},
Write {
path: String,
text: String,
},
Replace {
path: String,
old: String,
new: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
all: Option<bool>,
},
Insert {
path: String,
after: f64,
text: String,
},
Remove {
path: String,
start: f64,
end: f64,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ProcessSort {
Cpu,
Memory,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum ProcessSignal {
Term,
Kill,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum ProcessTool {
Processes {
#[serde(default, skip_serializing_if = "Option::is_none")]
sort: Option<ProcessSort>,
#[serde(default, skip_serializing_if = "Option::is_none")]
limit: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
filter: Option<String>,
},
Kill {
pid: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
signal: Option<ProcessSignal>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum InternetMode {
Private,
Public,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum InternetTool {
Internet { mode: InternetMode },
}
pub type DesktopTool = BrowserTool;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
#[allow(clippy::large_enum_variant)]
pub enum MachineTool {
Desktop(DesktopTool),
Window(WindowTool),
File(FileTool),
Process(ProcessTool),
Internet(InternetTool),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WindowKind {
App,
Web,
Files,
Preview,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Window {
pub id: u64,
pub kind: WindowKind,
pub app: String,
pub title: String,
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
pub focused: bool,
}
fn truthy(value: &Value) -> bool {
match value {
Value::Null => false,
Value::Bool(value) => *value,
Value::Number(number) => number
.as_f64()
.is_some_and(|number| number != 0.0 && !number.is_nan()),
Value::String(text) => !text.is_empty(),
_ => true,
}
}
impl Window {
pub fn from_json(json: &Value) -> Window {
let kind = match json["kind"].as_str() {
Some("web") => WindowKind::Web,
Some("files") => WindowKind::Files,
Some("preview") => WindowKind::Preview,
_ => WindowKind::App,
};
let kind_name = match kind {
WindowKind::App => "app",
WindowKind::Web => "web",
WindowKind::Files => "files",
WindowKind::Preview => "preview",
};
Window {
id: js_number(json.get("id")) as u64,
kind,
app: js_string(json.get("app"), kind_name),
title: js_string(json.get("title"), ""),
x: json["x"].as_f64().unwrap_or_default(),
y: json["y"].as_f64().unwrap_or_default(),
width: json["width"].as_f64().unwrap_or_default(),
height: json["height"].as_f64().unwrap_or_default(),
focused: truthy(&json["focused"]),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum FilesView {
List,
Cozy,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FilesState {
pub path: String,
pub view: FilesView,
}
impl FilesState {
pub fn from_json(json: &Value) -> FilesState {
FilesState {
path: js_string(json.get("path"), ""),
view: if json["view"] == "cozy" {
FilesView::Cozy
} else {
FilesView::List
},
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum MachineStatus {
Booting,
Running,
Stopping,
Stopped,
Failed,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Viewport {
pub width: f64,
pub height: f64,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct ViewportBounds {
pub min: Viewport,
pub max: Viewport,
}
pub const VIEWPORT: ViewportBounds = ViewportBounds {
min: Viewport {
width: 640.0,
height: 400.0,
},
max: Viewport {
width: 2560.0,
height: 1600.0,
},
};
pub fn clamp_viewport(size: Viewport) -> Viewport {
Viewport {
width: size
.width
.clamp(VIEWPORT.min.width, VIEWPORT.max.width)
.round(),
height: size
.height
.clamp(VIEWPORT.min.height, VIEWPORT.max.height)
.round(),
}
}
pub trait MachineRoomHooks: BrowserRoomHooks {
fn on_window(&self, _room: &MachineRoom, _window: &Window) {}
fn on_window_closed(&self, _room: &MachineRoom, _id: u64) {}
fn on_files(&self, _room: &MachineRoom, _window: u64, _files: &FilesState) {}
fn on_status(&self, _room: &MachineRoom) {}
fn on_private(&self, _room: &MachineRoom) {}
fn on_agent_step(&self, _room: &MachineRoom, _step: &AgentStep) {}
}
impl MachineRoomHooks for () {}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgentStep {
pub agent: String,
pub step: u64,
pub call: u64,
pub tool: String,
#[serde(default)]
pub args: Map<String, Value>,
#[serde(default)]
pub result: String,
#[serde(default)]
pub model_ms: f64,
#[serde(default)]
pub tool_ms: f64,
#[serde(default)]
pub usage: Usage,
}
const DESKTOP_TOOLS: [&str; 5] = ["move", "click", "drag", "scroll", "type"];
const CALL_MS: u64 = 60_000;
const EXEC_MS: u64 = 120_000;
const STOP_MS: u64 = 10 * 60_000;
pub type MachineToolInvoker =
Arc<dyn Fn(MachineTool) -> BoxFuture<'static, Result<String>> + Send + Sync>;
pub type ExecInvoker =
Arc<dyn Fn(String, Option<f64>) -> BoxFuture<'static, Result<String>> + Send + Sync>;
fn optional_text(p: &Value, key: &str) -> Option<String> {
present(p.get(key)).map(|value| js_string(Some(value), ""))
}
fn optional_number(p: &Value, key: &str) -> Option<f64> {
present(p.get(key)).map(|value| js_number(Some(value)))
}
pub fn parrot_tools(invoke: MachineToolInvoker, exec: ExecInvoker) -> Vec<Tool> {
let desktop = invoke.clone();
let mut tools = browser_parrot_tools(
&DESKTOP_TOOLS
.iter()
.map(|kind| kind.to_string())
.collect::<HashSet<_>>(),
Arc::new(move |tool| desktop(MachineTool::Desktop(tool))),
);
let mut add = |name: &str,
description: &str,
properties: Value,
required: &[&str],
build: fn(&Value) -> MachineTool| {
let mut parameters = json!({ "type": "OBJECT", "properties": properties });
if !required.is_empty() {
parameters["required"] = json!(required);
}
let invoke = invoke.clone();
tools.push(tool(name, description, parameters, move |params: Value| {
let params = if params.is_null() { json!({}) } else { params };
invoke(build(¶ms))
}));
};
add(
"windows",
"List every open desktop window with its id, kind, app, title, position, size, and focus.",
json!({}),
&[],
|_| MachineTool::Window(WindowTool::Windows),
);
add(
"focus",
"Bring a window to the front and give it keyboard focus.",
json!({
"window": { "type": "NUMBER", "description": "Window id" },
}),
&["window"],
|p| {
MachineTool::Window(WindowTool::Focus {
window: js_number(p.get("window")),
})
},
);
add(
"place",
"Move a window so its top left corner is at desktop coordinates.",
json!({
"window": { "type": "NUMBER", "description": "Window id" },
"x": { "type": "NUMBER", "description": "Desktop X pixel" },
"y": { "type": "NUMBER", "description": "Desktop Y pixel" },
}),
&["window", "x", "y"],
|p| {
MachineTool::Window(WindowTool::Place {
window: js_number(p.get("window")),
x: js_number(p.get("x")),
y: js_number(p.get("y")),
})
},
);
add(
"resize",
"Resize a window. Apps redraw at the new size.",
json!({
"window": { "type": "NUMBER", "description": "Window id" },
"width": { "type": "NUMBER", "description": "Width in pixels" },
"height": { "type": "NUMBER", "description": "Height in pixels" },
}),
&["window", "width", "height"],
|p| {
MachineTool::Window(WindowTool::Resize {
window: js_number(p.get("window")),
width: js_number(p.get("width")),
height: js_number(p.get("height")),
})
},
);
add(
"close",
"Close a window.",
json!({
"window": { "type": "NUMBER", "description": "Window id" },
}),
&["window"],
|p| {
MachineTool::Window(WindowTool::Close {
window: js_number(p.get("window")),
})
},
);
add(
"reload",
"Reload a Web window after its page's files change, so it shows them. Previews follow their file on their own, and reload too.",
json!({
"window": { "type": "NUMBER", "description": "Window id" },
}),
&["window"],
|p| {
MachineTool::Window(WindowTool::Reload {
window: js_number(p.get("window")),
})
},
);
add(
"open",
"Open a window on the desktop: web opens a Web window at url (http or https; serve local pages over HTTP first, since file:// pages don't load); files opens a Files window at a folder; file previews a file (images, PDFs, video, audio, text); terminal opens a terminal, running command in view; when it ends the window stays open at a prompt, so people can read the result and you can type more. A preview follows its file as it changes.",
json!({
"app": { "type": "STRING", "enum": ["web", "files", "file", "terminal"], "description": "web, files, file or terminal" },
"url": { "type": "STRING", "description": "http or https address for a web window" },
"path": { "type": "STRING", "description": "Folder for files, or the file to preview, relative to /repository" },
"command": { "type": "STRING", "description": "Command for a terminal to run in view" },
}),
&["app"],
|p| {
MachineTool::Window(WindowTool::Open {
app: match p.get("app").and_then(Value::as_str) {
Some("files") => WindowApp::Files,
Some("file") => WindowApp::File,
Some("terminal") => WindowApp::Terminal,
_ => WindowApp::Web,
},
url: optional_text(p, "url"),
path: optional_text(p, "path"),
command: optional_text(p, "command"),
})
},
);
add(
"key",
"Press a key chord, like ctrl and s together. Focuses the window first when given. ctrl r or f5 on a Web window reloads it.",
json!({
"keys": { "type": "ARRAY", "items": { "type": "STRING" }, "description": "Keys held together, e.g. [\"ctrl\", \"s\"]" },
"window": { "type": "NUMBER", "description": "Window id to focus first" },
}),
&["keys"],
|p| {
MachineTool::Window(WindowTool::Key {
keys: p
.get("keys")
.and_then(Value::as_array)
.map(|keys| keys.iter().map(|key| js_string(Some(key), "")).collect())
.unwrap_or_default(),
window: optional_number(p, "window"),
})
},
);
add(
"list",
"List files under a folder of /repository. Folders end in a slash. With pattern (like **/*.ts) it matches recursively.",
json!({
"path": { "type": "STRING", "description": "Folder relative to /repository. Defaults to the root." },
"pattern": { "type": "STRING", "description": "Glob to match recursively" },
}),
&[],
|p| {
MachineTool::File(FileTool::List {
path: optional_text(p, "path"),
pattern: optional_text(p, "pattern"),
})
},
);
add(
"grep",
"Search file contents in /repository with ripgrep. Output is matching files by default, or lines with context, or counts.",
json!({
"pattern": { "type": "STRING", "description": "Regular expression" },
"path": { "type": "STRING", "description": "Folder or file to search" },
"glob": { "type": "STRING", "description": "Only files matching this glob" },
"ignoreCase": { "type": "BOOLEAN", "description": "Case insensitive" },
"context": { "type": "NUMBER", "description": "Lines of context around each match" },
"output": { "type": "STRING", "enum": ["files", "lines", "count"], "description": "files, lines, or count" },
"max": { "type": "NUMBER", "description": "Result cap, default 50" },
}),
&["pattern"],
|p| {
MachineTool::File(FileTool::Grep {
pattern: js_string(p.get("pattern"), ""),
path: optional_text(p, "path"),
glob: optional_text(p, "glob"),
ignore_case: present(p.get("ignoreCase")).map(truthy),
context: optional_number(p, "context"),
output: present(p.get("output"))
.and_then(|output| serde_json::from_value(output.clone()).ok()),
max: optional_number(p, "max"),
})
},
);
add(
"read",
"Read numbered lines of a file: from line minus before to line plus after. Defaults to the first 100 lines.",
json!({
"path": { "type": "STRING", "description": "File relative to /repository" },
"line": { "type": "NUMBER", "description": "Line to center on, default 1" },
"before": { "type": "NUMBER", "description": "Lines before, default 0" },
"after": { "type": "NUMBER", "description": "Lines after, default 100" },
}),
&["path"],
|p| {
MachineTool::File(FileTool::Read {
path: js_string(p.get("path"), ""),
line: optional_number(p, "line"),
before: optional_number(p, "before"),
after: optional_number(p, "after"),
})
},
);
add(
"write",
"Create or overwrite a file, creating missing folders.",
json!({
"path": { "type": "STRING", "description": "File relative to /repository" },
"text": { "type": "STRING", "description": "Full file contents" },
}),
&["path", "text"],
|p| {
MachineTool::File(FileTool::Write {
path: js_string(p.get("path"), ""),
text: js_string(p.get("text"), ""),
})
},
);
add(
"replace",
"Replace an exact string in a file. old must match exactly once unless all is true. Prefer this for edits.",
json!({
"path": { "type": "STRING", "description": "File relative to /repository" },
"old": { "type": "STRING", "description": "Exact text to find" },
"new": { "type": "STRING", "description": "Replacement text" },
"all": { "type": "BOOLEAN", "description": "Replace every match" },
}),
&["path", "old", "new"],
|p| {
MachineTool::File(FileTool::Replace {
path: js_string(p.get("path"), ""),
old: js_string(p.get("old"), ""),
new: js_string(p.get("new"), ""),
all: present(p.get("all")).map(truthy),
})
},
);
add(
"insert",
"Insert text after a line number. 0 inserts at the top. Read the file first.",
json!({
"path": { "type": "STRING", "description": "File relative to /repository" },
"after": { "type": "NUMBER", "description": "Line to insert after" },
"text": { "type": "STRING", "description": "Text to insert" },
}),
&["path", "after", "text"],
|p| {
MachineTool::File(FileTool::Insert {
path: js_string(p.get("path"), ""),
after: js_number(p.get("after")),
text: js_string(p.get("text"), ""),
})
},
);
add(
"remove",
"Delete lines start through end, inclusive. Read the file first.",
json!({
"path": { "type": "STRING", "description": "File relative to /repository" },
"start": { "type": "NUMBER", "description": "First line" },
"end": { "type": "NUMBER", "description": "Last line" },
}),
&["path", "start", "end"],
|p| {
MachineTool::File(FileTool::Remove {
path: js_string(p.get("path"), ""),
start: js_number(p.get("start")),
end: js_number(p.get("end")),
})
},
);
add(
"processes",
"List running processes with their CPU right now, memory, running time, command and listening ports, plus the machine's CPU load, memory and free disk. Default: the 25 busiest by CPU.",
json!({
"sort": { "type": "STRING", "enum": ["cpu", "memory"], "description": "cpu or memory, default cpu" },
"limit": { "type": "NUMBER", "description": "How many to list, default 25" },
"filter": { "type": "STRING", "description": "Only commands containing this text" },
}),
&[],
|p| {
MachineTool::Process(ProcessTool::Processes {
sort: match p.get("sort").and_then(Value::as_str) {
Some("memory") => Some(ProcessSort::Memory),
Some("cpu") => Some(ProcessSort::Cpu),
_ => None,
},
limit: optional_number(p, "limit"),
filter: optional_text(p, "filter"),
})
},
);
add(
"kill",
"Stop one of your processes. TERM asks it to exit, KILL forces it. The machine's own processes, including the desktop's browser, can't be stopped.",
json!({
"pid": { "type": "NUMBER", "description": "Process id from processes" },
"signal": { "type": "STRING", "enum": ["TERM", "KILL"], "description": "TERM (default) or KILL" },
}),
&["pid"],
|p| {
MachineTool::Process(ProcessTool::Kill {
pid: js_number(p.get("pid")),
signal: match p.get("signal").and_then(Value::as_str) {
Some("KILL") => Some(ProcessSignal::Kill),
Some("TERM") => Some(ProcessSignal::Term),
_ => None,
},
})
},
);
add(
"internet",
"Choose how the machine reaches the internet. private sends every connection out from a residential address, billed per GB; public uses the machine's own address. While private reconnects the internet is paused and connections are refused; switch to public if the work can't wait.",
json!({
"mode": { "type": "STRING", "enum": ["private", "public"], "description": "private or public" },
}),
&["mode"],
|p| {
MachineTool::Internet(InternetTool::Internet {
mode: if p.get("mode").and_then(Value::as_str) == Some("public") {
InternetMode::Public
} else {
InternetMode::Private
},
})
},
);
tools.push(tool(
"exec",
"Run a Bash command as the agent user in /repository. Each call is a new shell: cd, exported variables and activated environments don't carry over, and there's no input or terminal. Output comes back when the command exits, and nothing shows on screen; to let people watch a command run, open a terminal with it instead. Start servers and GUI apps in the background with & (or nohup ... &) so they keep running; a timeout stops everything the command started. GUI apps open as desktop windows.",
json!({
"type": "OBJECT",
"properties": {
"command": { "type": "STRING", "description": "Bash command" },
"timeoutMs": { "type": "NUMBER", "description": "Timeout in ms, default 120000; pass more for installs and builds" },
},
"required": ["command"],
}),
move |params: Value| exec(js_string(params.get("command"), ""), optional_number(¶ms, "timeoutMs")),
));
tools
}
pub const DESKTOP_DECLINED: &str =
"They chose not to use a computer for this. Do it without one if you can.";
const DESKTOP_CAPTURE: &str = "Everything it captures is attached to the chat for people to see, and comes back to you too. Set video to record the whole job as a captioned video, for when someone asks to see it done. Slower and costlier than the other tools; use it only when the screen matters.";
pub fn desktop_tool(repository: Option<&Repository>) -> Function {
let mut properties = json!({
"goal": { "type": "STRING", "description": "What the desktop agent should do, in full, with what to capture" },
"video": { "type": "BOOLEAN", "description": "Record the whole job as a captioned video" },
});
if repository.is_some() {
return Function {
name: "use_desktop".to_string(),
description: Some(format!(
"Do something on the repository's desktop that needs a screen, like using an app on the machine or working across its windows. A desktop agent does it and reports back. For a website alone, use_browser is quicker. {DESKTOP_CAPTURE}"
)),
parameters: serde_json::from_value::<Schema>(
json!({ "type": "OBJECT", "properties": properties, "required": ["goal"] }),
)
.expect("tool schemas are valid"),
default: None,
example: None,
behavior: None,
};
}
properties["name"] = json!({ "type": "STRING", "description": "What to call the repository this makes: two to four words about the task, like Invoice spreadsheet cleanup" });
Function {
name: "use_desktop".to_string(),
description: Some(format!(
"Do something on a computer that needs a screen, like running an app or working across windows. This chat has no computer yet, so the person is asked first to make a repository for it: a Linux machine with a desktop. If they agree, a desktop agent does it and reports back; if they don't, do that request without a computer. For a website alone, use use_browser, which needs no computer. {DESKTOP_CAPTURE}"
)),
parameters: serde_json::from_value::<Schema>(
json!({ "type": "OBJECT", "properties": properties, "required": ["goal", "name"] }),
)
.expect("tool schemas are valid"),
default: None,
example: None,
behavior: None,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Pricing {
pub machine_per_hour: f64,
pub storage_per_gb_month: f64,
}
pub async fn pricing() -> Result<Pricing> {
let response = crate::http::client()
.get(format!("{}/termite/pricing", super::get_url()))
.send()
.await?;
let json: Value = crate::http::ok(response).await?.json().await?;
Ok(Pricing {
machine_per_hour: js_number(json.get("machinePerHour")),
storage_per_gb_month: js_number(json.get("storagePerGbMonth")),
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WakeReason {
Called,
Crashed,
Skipped,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Wake {
pub id: String,
pub reason: WakeReason,
pub prompt: String,
pub started: DateTime<Utc>,
pub finished: Option<DateTime<Utc>>,
pub ok: bool,
pub answer: String,
}
impl Wake {
pub fn from_json(json: &Value) -> Wake {
Wake {
id: js_string(json.get("id"), "undefined"),
reason: match json["reason"].as_str() {
Some("crashed") => WakeReason::Crashed,
Some("skipped") => WakeReason::Skipped,
_ => WakeReason::Called,
},
prompt: js_string(json.get("prompt"), ""),
started: crate::tickets::parse_date(&json["started"]).unwrap_or_default(),
finished: if truthy(&json["finished"]) {
crate::tickets::parse_date(&json["finished"])
} else {
None
},
ok: truthy(&json["ok"]),
answer: js_string(json.get("answer"), ""),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct GroupOptions {
pub group: Option<Group>,
}
fn authorized(
request: reqwest::RequestBuilder,
authorization: &CredentialsOrApiKey,
group: Option<&Group>,
) -> reqwest::RequestBuilder {
let request = request.header(
"Authorization",
match authorization {
CredentialsOrApiKey::Credentials(credentials) => format!("Token {}", credentials.token),
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
},
);
match group {
Some(group) => request.header("Group", &group.id),
None => request,
}
}
pub async fn wakes(
authorization: &CredentialsOrApiKey,
repository: u64,
query: PageQuery,
opts: GroupOptions,
) -> Result<Vec<Wake>> {
let response = authorized(
crate::http::client().get(format!(
"{}/termite/repositories/{repository}/wakes?cursor={}&max={}",
super::get_url(),
query.cursor,
query.max
)),
authorization,
opts.group.as_ref(),
)
.send()
.await?;
let json: Value = crate::http::ok(response).await?.json().await?;
Ok(json
.as_array()
.into_iter()
.flatten()
.map(Wake::from_json)
.collect())
}
struct Machine {
windows: HashMap<u64, Window>,
files: HashMap<u64, FilesState>,
status: MachineStatus,
error: String,
name: String,
private: bool,
private_reason: String,
}
struct MachineState {
machine: u64,
repository: u64,
state: Mutex<Machine>,
hooks: Mutex<Arc<dyn MachineRoomHooks>>,
calls: Mutex<HashMap<String, oneshot::Sender<Result<Value>>>>,
}
fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
mutex.lock().unwrap_or_else(PoisonError::into_inner)
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExecResult {
pub code: i64,
pub stdout: String,
pub stderr: String,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct ExecOptions {
pub timeout_ms: Option<u64>,
}
#[derive(Default)]
pub struct StartOptions {
pub group: Option<Group>,
pub hooks: Option<Arc<dyn MachineRoomHooks>>,
pub private: Option<bool>,
}
#[derive(Default)]
pub struct MachineOptions {
pub group: Option<Group>,
pub hooks: Option<Arc<dyn MachineRoomHooks>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MachineTarget {
Id { id: u64 },
Reference { reference: String },
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PrivateState {
pub on: bool,
pub reason: String,
}
#[derive(Clone)]
pub struct MachineRoom {
page: PageRoom,
inner: Arc<MachineState>,
}
impl Deref for MachineRoom {
type Target = PageRoom;
fn deref(&self) -> &PageRoom {
&self.page
}
}
impl MachineRoom {
fn create(room: &Room, hooks: Arc<dyn MachineRoomHooks>, state: &Value) -> MachineRoom {
let page_hooks: Arc<dyn BrowserRoomHooks> = hooks.clone();
let page = PageRoom::create(
room,
page_hooks,
state,
Some(DESKTOP_TOOLS.iter().map(|kind| kind.to_string()).collect()),
);
let machine = MachineRoom {
page,
inner: Arc::new(MachineState {
machine: js_number(Some(&state["machine"]["id"])) as u64,
repository: js_number(Some(&state["machine"]["repository"])) as u64,
state: Mutex::new(Machine {
windows: HashMap::new(),
files: HashMap::new(),
status: MachineStatus::Booting,
error: String::new(),
name: String::new(),
private: false,
private_reason: String::new(),
}),
hooks: Mutex::new(hooks),
calls: Mutex::new(HashMap::new()),
}),
};
machine.hydrate(state);
machine
}
fn machine_state(&self) -> MutexGuard<'_, Machine> {
lock(&self.inner.state)
}
fn machine_hooks(&self) -> Arc<dyn MachineRoomHooks> {
lock(&self.inner.hooks).clone()
}
pub fn machine(&self) -> u64 {
self.inner.machine
}
pub fn repository(&self) -> u64 {
self.inner.repository
}
pub fn windows(&self) -> HashMap<u64, Window> {
self.machine_state().windows.clone()
}
pub fn files(&self) -> HashMap<u64, FilesState> {
self.machine_state().files.clone()
}
pub fn status(&self) -> MachineStatus {
self.machine_state().status
}
pub fn error(&self) -> String {
self.machine_state().error.clone()
}
pub fn name(&self) -> String {
self.machine_state().name.clone()
}
pub fn private(&self) -> bool {
self.machine_state().private
}
pub fn private_reason(&self) -> String {
self.machine_state().private_reason.clone()
}
pub fn hydrate(&self, state: &Value) {
self.page.apply_control(&state["control"]);
let mut machine = self.machine_state();
if let Ok(status) = serde_json::from_value(state["machine"]["status"].clone()) {
machine.status = status;
}
if state["machine"].is_object() {
machine.error = js_string(state["machine"].get("error"), "");
machine.name = js_string(state["machine"].get("name"), "");
}
if let Some(windows) = state["windows"].as_array() {
machine.windows = windows
.iter()
.map(Window::from_json)
.map(|window| (window.id, window))
.collect();
}
if let Some(files) = state["files"].as_array() {
machine.files = files
.iter()
.map(|row| {
(
js_number(row.get("window")) as u64,
FilesState::from_json(row),
)
})
.collect();
}
if truthy(&state["private"]) {
machine.private = truthy(&state["private"]["on"]);
machine.private_reason = js_string(state["private"].get("reason"), "");
}
}
pub fn watch(&self, hooks: Arc<dyn MachineRoomHooks>) {
*lock(&self.inner.hooks) = hooks.clone();
self.page.watch(hooks);
}
pub async fn invoke_tool(&self, tool: &MachineTool) -> Result<String> {
if let MachineTool::Desktop(tool) = tool
&& DESKTOP_TOOLS.contains(&tool.kind())
{
return self.page.invoke_tool(tool).await;
}
let result = self
.call(json!({ "kind": "tool", "tool": tool }), CALL_MS)
.await?;
Ok(match result {
Value::Null => String::new(),
Value::String(result) => result,
result => result.to_string(),
})
}
pub async fn exec(&self, command: &str, opts: ExecOptions) -> Result<ExecResult> {
let timeout_ms = opts.timeout_ms.unwrap_or(EXEC_MS);
let result = self
.call(
json!({ "kind": "exec", "command": command, "timeoutMs": timeout_ms }),
timeout_ms + 15_000,
)
.await?;
Ok(ExecResult {
code: match result.get("code") {
None | Some(Value::Null) => -1,
code => js_number(code) as i64,
},
stdout: js_string(result.get("stdout"), ""),
stderr: js_string(result.get("stderr"), ""),
})
}
pub async fn set_viewport(&self, width: f64, height: f64) -> Result<()> {
let size = clamp_viewport(Viewport { width, height });
self.call(
json!({ "kind": "viewport", "width": size.width, "height": size.height }),
CALL_MS,
)
.await?;
Ok(())
}
pub async fn open(&self, path: &str) -> Result<()> {
self.call(json!({ "kind": "open", "path": path }), CALL_MS)
.await?;
Ok(())
}
pub async fn set_files(&self, window: u64, files: &FilesState) -> Result<()> {
self.call(
json!({ "kind": "files", "window": window, "path": files.path, "view": files.view }),
CALL_MS,
)
.await?;
Ok(())
}
pub async fn stop(&self) -> Result<()> {
match self.call(json!({ "kind": "stop" }), STOP_MS).await {
Ok(_) => Ok(()),
Err(_) if self.status() == MachineStatus::Stopped => Ok(()),
Err(error) => Err(error),
}
}
async fn call(&self, call: Value, timeout_ms: u64) -> Result<Value> {
let id = uuid::Uuid::new_v4().to_string();
let (resolve, result) = oneshot::channel();
lock(&self.inner.calls).insert(id.clone(), resolve);
self.post(&json!({ "event": "machine-call", "data": { "id": id, "call": call } }));
match tokio::time::timeout(Duration::from_millis(timeout_ms), result).await {
Ok(Ok(result)) => result,
Ok(Err(_)) => Err(Error::Message("Machine closed.".to_string())),
Err(_) => {
lock(&self.inner.calls).remove(&id);
Err(Error::Message("Machine call timed out.".to_string()))
}
}
}
pub fn parrot_tools(&self) -> Vec<Tool> {
let invoking = self.clone();
let executing = self.clone();
parrot_tools(
Arc::new(move |tool| {
let room = invoking.clone();
Box::pin(async move { room.invoke_tool(&tool).await })
}),
Arc::new(move |command, timeout_ms| {
let room = executing.clone();
Box::pin(async move {
let result = room
.exec(
&command,
ExecOptions {
timeout_ms: timeout_ms.map(|ms| ms as u64),
},
)
.await?;
Ok(serde_json::to_string(&result)?)
})
}),
)
}
pub fn handle_event(&self, packet: &Value) -> bool {
let data = &packet["data"];
let hooks = self.machine_hooks();
match packet["event"].as_str().unwrap_or_default() {
"machine-status" => {
if let Ok(status) = serde_json::from_value(data["status"].clone()) {
self.machine_state().status = status;
}
hooks.on_status(self);
true
}
"machine-window" => {
if !truthy(&data["window"]) {
return true;
}
let window = Window::from_json(&data["window"]);
self.machine_state()
.windows
.insert(window.id, window.clone());
hooks.on_window(self, &window);
true
}
"machine-window-closed" => {
let id = js_number(data.get("id")) as u64;
{
let mut machine = self.machine_state();
machine.windows.remove(&id);
machine.files.remove(&id);
}
hooks.on_window_closed(self, id);
true
}
"machine-files" => {
let window = js_number(data.get("window")) as u64;
let files = FilesState::from_json(data);
self.machine_state().files.insert(window, files.clone());
hooks.on_files(self, window, &files);
true
}
"machine-private" => {
{
let mut machine = self.machine_state();
machine.private = truthy(&data["on"]);
machine.private_reason = js_string(data.get("reason"), "");
}
hooks.on_private(self);
true
}
"machine-agent-step" => {
if let Ok(step) = serde_json::from_value::<AgentStep>(data.clone()) {
hooks.on_agent_step(self, &step);
}
true
}
"machine-result" => {
let Some(id) = data["id"].as_str() else {
return true;
};
let Some(pending) = lock(&self.inner.calls).remove(id) else {
return true;
};
let _ = pending.send(if truthy(&data["error"]) {
Err(Error::Message(js_string(data.get("error"), "")))
} else {
Ok(data["result"].clone())
});
true
}
"machine-closed" => {
{
let mut machine = self.machine_state();
if let Ok(status) = serde_json::from_value(data["status"].clone()) {
machine.status = status;
}
machine.error = js_string(data.get("error"), "");
}
hooks.on_status(self);
for (_, pending) in lock(&self.inner.calls).drain() {
let _ = pending.send(Err(Error::Message("Machine closed.".to_string())));
}
self.page
.handle_event(&json!({ "event": "browser-closed", "data": {} }))
}
_ => self.page.handle_event(packet),
}
}
pub async fn start(
authorization: &CredentialsOrApiKey,
repository: u64,
opts: StartOptions,
) -> Result<(MachineRoom, Presence)> {
let body = match opts.private {
Some(private) => json!({ "private": private }),
None => json!({}),
};
let response = authorized(
crate::http::client().post(format!(
"{}/termite/repositories/{repository}/machine",
super::get_url()
)),
authorization,
opts.group.as_ref(),
)
.json(&body)
.send()
.await?;
let json: Value = crate::http::ok(response).await?.json().await?;
MachineRoom::connect(
authorization,
&MachineTarget::Id {
id: js_number(json.get("id")) as u64,
},
MachineOptions {
group: opts.group,
hooks: opts.hooks,
},
)
.await
}
pub async fn shutdown(
authorization: &CredentialsOrApiKey,
repository: u64,
opts: GroupOptions,
) -> Result<()> {
let response = authorized(
crate::http::client().post(format!(
"{}/termite/repositories/{repository}/machine/stop",
super::get_url()
)),
authorization,
opts.group.as_ref(),
)
.json(&json!({}))
.send()
.await?;
crate::http::ok(response).await?;
Ok(())
}
pub async fn set_private(
authorization: &CredentialsOrApiKey,
repository: u64,
on: bool,
opts: GroupOptions,
) -> Result<PrivateState> {
let response = authorized(
crate::http::client().post(format!(
"{}/termite/repositories/{repository}/machine/private",
super::get_url()
)),
authorization,
opts.group.as_ref(),
)
.json(&json!({ "on": on }))
.send()
.await?;
let json: Value = crate::http::ok(response).await?.json().await?;
Ok(PrivateState {
on: truthy(&json["on"]),
reason: js_string(json.get("reason"), ""),
})
}
pub async fn connect(
authorization: &CredentialsOrApiKey,
target: &MachineTarget,
opts: MachineOptions,
) -> Result<(MachineRoom, Presence)> {
MachineRoom::open_room(
Authorization::Server {
authorization: authorization.clone(),
server: match target {
MachineTarget::Id { id } => format!("termite:machine:{id}"),
MachineTarget::Reference { reference } => reference.clone(),
},
group: opts.group.as_ref().map(|group| group.id.clone()),
},
opts.hooks.unwrap_or_else(|| Arc::new(())),
)
.await
}
pub async fn join(
authorization: &CredentialsOrApiKey,
access_code: &str,
opts: MachineOptions,
) -> Result<(MachineRoom, Presence)> {
MachineRoom::open_room(
Authorization::AccessCode {
authorization: authorization.clone(),
access_code: access_code.to_string(),
},
opts.hooks.unwrap_or_else(|| Arc::new(())),
)
.await
}
async fn open_room(
auth: Authorization,
hooks: Arc<dyn MachineRoomHooks>,
) -> Result<(MachineRoom, Presence)> {
let machine_hooks = Arc::new(MachineHooks {
room: Mutex::new(None),
hooks,
});
let me = connect_server(
auth,
&format!("{}/termite/machine/live", super::get_url()),
machine_hooks.clone(),
)
.await?;
let room = machine_hooks
.room()
.ok_or_else(|| Error::Message("Room not found".to_string()))?;
room.emit_presence();
Ok((room, me))
}
}
struct MachineHooks {
room: Mutex<Option<MachineRoom>>,
hooks: Arc<dyn MachineRoomHooks>,
}
impl MachineHooks {
fn room(&self) -> Option<MachineRoom> {
lock(&self.room).clone()
}
}
impl Hooks for MachineHooks {
fn create_room(&self, room: &Room, state: &Value) {
*lock(&self.room) = Some(MachineRoom::create(room, self.hooks.clone(), state));
}
fn on_presence(&self, _room: &Room) {
if let Some(room) = self.room() {
room.emit_presence();
}
}
fn on_connection(&self, _room: &Room, state: &Value) {
let Some(room) = self.room() else {
return;
};
if let Ok(bootstrap) = serde_json::from_value::<BootstrapMeta>(state["bootstrap"].clone()) {
room.web().ingest_bootstrap(bootstrap);
}
if let Ok(cursor) = serde_json::from_value::<HtmlPointer>(state["cursor"].clone()) {
room.page.set_cursor(cursor.clone());
room.machine_hooks().on_cursor(&room.page, &cursor);
}
room.hydrate(state);
}
fn on_event(&self, _room: &Room, packet: &Value) -> bool {
self.room().is_some_and(|room| room.handle_event(packet))
}
fn on_closed(&self, _room: &Room, _reason: Option<&str>) {
if let Some(room) = self.room() {
room.emit_closed();
}
}
}