use std::collections::HashMap;
use std::ops::Deref;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError, Weak};
use futures_util::future::BoxFuture;
use futures_util::stream::BoxStream;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};
use super::chat::{agent_stream, serialize_messages, socket_auth};
use super::format::{AntFormat, AntFormatOrFormat};
use super::persona::Identity;
use crate::authentification::CredentialsOrApiKey;
use crate::drive::{self, FileMeta, from_access_code};
use crate::error::{Error, Result};
use crate::groups::Group;
use crate::http::{client, encode, ok, websocket};
use crate::parrot::agent::Memory;
use crate::parrot::generative::{Content, StreamEvent, Tool};
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};
use crate::termite::versioning::{FileOrFiles, Repository};
pub use super::graph::{
FileNode, GraphEdge, GraphNode, GraphNodeWire, LinkedInNode, StartNode, Stimuli, TextNode,
WebsiteNode, XNode,
};
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,
}
}
fn scoped(
request: reqwest::RequestBuilder,
timeline_id: &str,
group: Option<&Group>,
access_code: Option<&str>,
) -> reqwest::RequestBuilder {
match access_code.filter(|code| !code.is_empty()) {
Some(access_code) => request.header("Access-Code", access_code),
None => {
let request = request.header("Timeline", timeline_id);
match group {
Some(group) => request.header("Group", &group.id),
None => request,
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TimelineEntry {
pub name: String,
pub id: String,
}
#[derive(Debug, Clone, Default)]
pub struct GroupOptions {
pub group: Option<Group>,
}
pub async fn list_timelines(
authorization: &CredentialsOrApiKey,
opts: GroupOptions,
) -> Result<Vec<TimelineEntry>> {
let response = authorized(
client().get(format!("{}/timelines", super::get_url())),
authorization,
opts.group.as_ref(),
)
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
#[derive(Debug, Clone, Default)]
pub struct CreateTimelineOptions {
pub group: Option<Group>,
pub audience: Option<Vec<String>>,
pub repository: Option<Repository>,
}
pub async fn create_timeline(
authorization: &CredentialsOrApiKey,
name: &str,
opts: CreateTimelineOptions,
) -> Result<TimelineEntry> {
let mut body = Map::new();
body.insert("name".to_string(), Value::from(name));
if let Some(audience) = opts.audience.filter(|audience| !audience.is_empty()) {
body.insert("audience".to_string(), json!(audience));
}
if let Some(repository) = &opts.repository {
body.insert("repository".to_string(), Value::from(repository.id));
}
let response = authorized(
client().post(format!("{}/timelines", super::get_url())),
authorization,
opts.group.as_ref(),
)
.json(&body)
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
pub async fn delete_timeline(
authorization: &CredentialsOrApiKey,
id: &str,
opts: GroupOptions,
) -> Result<()> {
let response = authorized(
client().delete(format!("{}/timeline", super::get_url())),
authorization,
opts.group.as_ref(),
)
.header("Timeline", id)
.send()
.await?;
ok(response).await?;
Ok(())
}
pub async fn rename_timeline(
authorization: &CredentialsOrApiKey,
id: &str,
name: &str,
opts: GroupOptions,
) -> Result<()> {
let response = authorized(
client().post(format!("{}/timeline/rename", super::get_url())),
authorization,
opts.group.as_ref(),
)
.header("Timeline", id)
.json(&json!({ "name": name }))
.send()
.await?;
ok(response).await?;
Ok(())
}
#[derive(Debug, Clone, Default)]
pub struct AccessOptions {
pub group: Option<Group>,
pub access_code: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct TrustGraph {
#[serde(default)]
pub edges: Vec<TrustEdge>,
}
pub async fn get_trust(
authorization: &CredentialsOrApiKey,
timeline_id: &str,
opts: AccessOptions,
) -> Result<TrustGraph> {
let request = authorized(
client().get(format!("{}/timeline/trust", super::get_url())),
authorization,
None,
);
let response = scoped(
request,
timeline_id,
opts.group.as_ref(),
opts.access_code.as_deref(),
)
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TrustEdge {
pub from: String,
pub to: String,
pub weight: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub updated_at: Option<f64>,
}
fn trust_edge(json: &Value) -> Option<TrustEdge> {
let edge: TrustEdge = serde_json::from_value(json.clone()).ok()?;
(!edge.from.is_empty() && !edge.to.is_empty() && edge.weight > 0.0).then_some(edge)
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FileAccessCodeMeta {
pub code: String,
#[serde(default)]
pub name: String,
#[serde(default)]
pub content_type: String,
}
#[derive(Clone)]
pub struct Poll {
pub id: String,
pub goal: String,
pub query: Vec<AntFormatOrFormat>,
pub results: Value,
}
impl Poll {
pub fn new(id: &str, goal: &str, query: Vec<AntFormatOrFormat>) -> Poll {
Poll {
id: id.to_string(),
goal: goal.to_string(),
query,
results: Value::Null,
}
}
pub fn from_json(json: &Value) -> Poll {
let read = |format: &Value| {
format
.get("type")
.is_some()
.then(|| AntFormat::from_json(format).ok())
.flatten()
};
let query = match &json["query"] {
Value::Array(formats) => formats.iter().filter_map(read).collect(),
format @ Value::Object(_) => read(format).into_iter().collect(),
_ => Vec::new(),
};
Poll {
id: match &json["id"] {
Value::String(id) => id.clone(),
Value::Null => String::new(),
id => id.to_string(),
},
goal: json["goal"].as_str().unwrap_or_default().to_string(),
query,
results: json["results"].clone(),
}
}
pub fn to_json(&self) -> Value {
json!({
"id": self.id,
"goal": self.goal,
"query": self.query.iter().map(AntFormatOrFormat::to_json).collect::<Vec<_>>(),
"results": self.results,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum RunStatus {
Queued,
Running,
Done,
Aborted,
Failed,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgentNote {
pub agent_id: String,
pub text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub to: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TrajectoryNode {
pub id: String,
pub kind: String,
pub title: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Thread {
pub post: String,
#[serde(default)]
pub comments: Vec<AgentNote>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Trajectory {
pub id: String,
#[serde(default)]
pub nodes: Vec<TrajectoryNode>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reflections: Option<Vec<AgentNote>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub thread: Option<Thread>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub forwards: Option<Vec<AgentNote>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum StimulusPhase {
Browse,
Reflect,
Discuss,
Forward,
Answer,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AgentSession {
pub reference: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FeedItem {
pub id: String,
pub agent_id: String,
pub text: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StimulusProgress {
pub stimulus_label: String,
pub phase: StimulusPhase,
pub done: f64,
pub total: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sessions: Option<HashMap<String, AgentSession>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub feed: Option<Vec<FeedItem>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RunProgress {
#[serde(default)]
pub active_node_ids: Vec<String>,
#[serde(default)]
pub active_edge_ids: Vec<String>,
#[serde(default)]
pub cluster_hits: HashMap<String, f64>,
#[serde(default)]
pub cluster_required: HashMap<String, f64>,
#[serde(default)]
pub by_stimulus: HashMap<String, StimulusProgress>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Run {
pub id: String,
pub poll_id: String,
pub iteration: f64,
pub status: RunStatus,
pub trajectories: Vec<Trajectory>,
pub polls: Option<Value>,
pub progress: Option<RunProgress>,
pub agent_ids: Vec<String>,
pub reach: Option<HashMap<String, Vec<String>>>,
pub started_at: f64,
pub finished_at: Option<f64>,
pub error: Option<String>,
}
fn normalize_run_status(raw: Option<&str>) -> RunStatus {
match raw.unwrap_or_default().to_uppercase().as_str() {
"QUEUED" => RunStatus::Queued,
"RUNNING" | "PENDING" | "ABORTING" => RunStatus::Running,
"DONE" => RunStatus::Done,
"ABORTED" => RunStatus::Aborted,
"FAILED" => RunStatus::Failed,
_ => RunStatus::Queued,
}
}
impl Run {
pub fn new(wire: &Value) -> Run {
let progress = if wire["progress"].is_null() {
&wire["polls"]["progress"]
} else {
&wire["progress"]
};
Run {
id: wire["id"].as_str().unwrap_or_default().to_string(),
poll_id: wire["pollId"].as_str().unwrap_or_default().to_string(),
iteration: wire["iteration"].as_f64().unwrap_or_default(),
status: normalize_run_status(wire["status"].as_str()),
trajectories: serde_json::from_value(wire["trajectories"].clone()).unwrap_or_default(),
polls: (!wire["polls"].is_null()).then(|| wire["polls"].clone()),
progress: serde_json::from_value(progress.clone()).ok(),
agent_ids: serde_json::from_value(wire["agentIds"].clone()).unwrap_or_default(),
reach: serde_json::from_value(wire["reach"].clone()).ok(),
started_at: wire["startedAt"].as_f64().unwrap_or_default(),
finished_at: wire["finishedAt"].as_f64(),
error: wire["error"].as_str().map(str::to_string),
}
}
}
pub fn run_is_active(status: Option<&str>) -> bool {
matches!(
normalize_run_status(status),
RunStatus::Queued | RunStatus::Running
)
}
#[derive(Clone)]
pub struct TimelineChat {
pub room: ChatRoom,
pub me: Presence,
pub chat_id: String,
}
#[derive(Debug, Clone, Default)]
pub struct AgentGenerateOptions {
pub tools: Option<Vec<Tool>>,
}
#[derive(Clone)]
pub struct Agent {
room: Room,
parent: Weak<TimelineState>,
pub id: String,
pub persona: Identity,
pub repository: Option<u64>,
}
impl Agent {
pub fn parent(&self) -> Option<TimelineRoom> {
self.parent.upgrade().map(|state| TimelineRoom {
room: self.room.clone(),
state,
})
}
pub fn from_json(json: &Value, parent: &TimelineRoom) -> Option<Agent> {
Some(Agent {
room: parent.room.clone(),
parent: Arc::downgrade(&parent.state),
id: match &json["id"] {
Value::String(id) => id.clone(),
id => id.to_string(),
},
persona: serde_json::from_value(json["persona"].clone()).ok()?,
repository: json["repository"]
.as_f64()
.filter(|repository| *repository > 0.0)
.map(|repository| repository as u64),
})
}
pub fn set_repository(&self, repository: Option<&Repository>) {
self.room.post(&json!({
"event": "set-agent-repository",
"agentId": self.id,
"repository": repository.map(|repository| repository.id),
}));
}
pub async fn get_chat(&self, hooks: Option<Arc<dyn RoomHooks>>) -> Result<TimelineChat> {
let parent = self
.parent()
.ok_or_else(|| Error::Message("Room not found".to_string()))?;
parent.open_chat(&self.id, hooks).await
}
fn timeline_id(&self) -> String {
self.parent()
.map(|room| room.timeline_id())
.unwrap_or_default()
}
fn access(&self) -> AccessOptions {
match self.room.authorization() {
Authorization::AccessCode { access_code, .. } => AccessOptions {
group: None,
access_code: Some(access_code.clone()),
},
Authorization::Server { group, .. } => AccessOptions {
group: group
.as_ref()
.map(|group| Group::from_json(&json!({ "id": group }))),
access_code: None,
},
}
}
pub async fn observe(&self, content: &str) -> Result<Memory> {
observe_agent(
self.room.get_authorization(),
&self.timeline_id(),
&self.id,
content,
self.access(),
)
.await
}
pub async fn generate(
&self,
messages: &[Content],
opts: AgentGenerateOptions,
) -> Result<BoxStream<'static, StreamEvent>> {
let access = self.access();
generate_agent(
self.room.get_authorization(),
&self.timeline_id(),
&self.id,
messages,
GenerateOptions {
group: access.group,
access_code: access.access_code,
tools: opts.tools,
},
)
.await
}
pub fn to_json(&self) -> Value {
json!({ "id": self.id, "persona": self.persona })
}
}
type RepositoryWatcher = Arc<dyn Fn(Option<u64>) + Send + Sync>;
struct Timeline {
timeline_id: String,
name: String,
agents: HashMap<String, Agent>,
polls: HashMap<String, Poll>,
runs: HashMap<String, Run>,
nodes: HashMap<String, GraphNode>,
edges: HashMap<String, GraphEdge>,
file_access_codes: HashMap<String, FileAccessCodeMeta>,
trust_edges: HashMap<String, TrustEdge>,
status: String,
removed: bool,
repository: Option<u64>,
owner: String,
}
impl Default for Timeline {
fn default() -> Self {
Timeline {
timeline_id: String::new(),
name: String::new(),
agents: HashMap::new(),
polls: HashMap::new(),
runs: HashMap::new(),
nodes: HashMap::new(),
edges: HashMap::new(),
file_access_codes: HashMap::new(),
trust_edges: HashMap::new(),
status: "IDLE".to_string(),
removed: false,
repository: None,
owner: String::new(),
}
}
}
struct TimelineState {
timeline: Mutex<Timeline>,
watchers: Mutex<Vec<(u64, RepositoryWatcher)>>,
next_watcher: AtomicU64,
}
fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
mutex.lock().unwrap_or_else(PoisonError::into_inner)
}
#[derive(Debug, Clone, Default)]
pub struct UploadFileOptions {
pub name: String,
pub content_type: Option<String>,
pub x: f64,
pub y: f64,
pub node_id: Option<String>,
pub kind: Option<Stimuli>,
}
#[derive(Debug, Clone)]
pub struct UploadedFile {
pub node: GraphNode,
pub access_code: FileAccessCodeMeta,
}
#[derive(Default)]
pub struct ConnectOptions {
pub group: Option<Group>,
pub hooks: Option<Arc<dyn Hooks>>,
pub server: Option<String>,
}
#[derive(Clone)]
pub struct TimelineRoom {
room: Room,
state: Arc<TimelineState>,
}
impl Deref for TimelineRoom {
type Target = Room;
fn deref(&self) -> &Room {
&self.room
}
}
fn pick<'a>(packet: &'a Value, key: &str) -> &'a Value {
if !packet[key].is_null() {
&packet[key]
} else if !packet["data"][key].is_null() {
&packet["data"][key]
} else {
&packet["data"]
}
}
fn id_of(packet: &Value) -> Option<String> {
let id = if packet["id"].is_null() {
&packet["data"]["id"]
} else {
&packet["id"]
};
match id {
Value::String(id) if !id.is_empty() => Some(id.clone()),
Value::Number(id) => Some(id.to_string()),
_ => None,
}
}
impl TimelineRoom {
fn timeline(&self) -> MutexGuard<'_, Timeline> {
lock(&self.state.timeline)
}
pub fn timeline_id(&self) -> String {
self.timeline().timeline_id.clone()
}
pub fn name(&self) -> String {
self.timeline().name.clone()
}
pub fn agents(&self) -> HashMap<String, Agent> {
self.timeline().agents.clone()
}
pub fn polls(&self) -> HashMap<String, Poll> {
self.timeline().polls.clone()
}
pub fn runs(&self) -> HashMap<String, Run> {
self.timeline().runs.clone()
}
pub fn nodes(&self) -> HashMap<String, GraphNode> {
self.timeline().nodes.clone()
}
pub fn edges(&self) -> HashMap<String, GraphEdge> {
self.timeline().edges.clone()
}
pub fn file_access_codes(&self) -> HashMap<String, FileAccessCodeMeta> {
self.timeline().file_access_codes.clone()
}
pub fn trust_edges(&self) -> HashMap<String, TrustEdge> {
self.timeline().trust_edges.clone()
}
pub fn status(&self) -> String {
self.timeline().status.clone()
}
pub fn removed(&self) -> bool {
self.timeline().removed
}
pub fn repository(&self) -> Option<u64> {
self.timeline().repository
}
pub fn owner(&self) -> String {
self.timeline().owner.clone()
}
pub fn group(&self) -> Option<Group> {
let owner = self.owner();
owner
.strip_prefix("group:")
.filter(|id| !id.is_empty() && id.bytes().all(|byte| byte.is_ascii_digit()))
.map(|id| Group::from_json(&json!({ "id": id })))
}
fn access(&self) -> (Option<Group>, Option<String>) {
match self.authorization() {
Authorization::AccessCode { access_code, .. } => (None, Some(access_code.clone())),
Authorization::Server { group, .. } => (
group
.as_ref()
.map(|group| Group::from_json(&json!({ "id": group }))),
None,
),
}
}
fn scoped(&self, request: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
let (group, access_code) = self.access();
scoped(
authorized(request, self.get_authorization(), None),
&self.timeline_id(),
group.as_ref(),
access_code.as_deref(),
)
}
pub async fn set_repository(&self, repository: Option<&Repository>) -> Result<()> {
self.post(&json!({ "event": "set-repository", "repository": repository.map(|repository| repository.id) }));
Ok(())
}
pub fn watch_repository(
&self,
listener: impl Fn(Option<u64>) + Send + Sync + 'static,
) -> Box<dyn FnOnce() + Send + Sync> {
let id = self.state.next_watcher.fetch_add(1, Ordering::Relaxed);
lock(&self.state.watchers).push((id, Arc::new(listener)));
let state = Arc::downgrade(&self.state);
Box::new(move || {
if let Some(state) = state.upgrade() {
lock(&state.watchers).retain(|(other, _)| *other != id);
}
})
}
fn apply_repository(&self, raw: &Value) {
let repository = raw
.as_f64()
.filter(|repository| *repository > 0.0)
.map(|repository| repository as u64);
{
let mut timeline = self.timeline();
if timeline.repository == repository {
return;
}
timeline.repository = repository;
}
let watchers: Vec<RepositoryWatcher> = lock(&self.state.watchers)
.iter()
.map(|(_, listener)| listener.clone())
.collect();
for listener in watchers {
listener(repository);
}
}
pub async fn run_trust(&self, run_id: &str) -> Result<Vec<TrustEdge>> {
let response = self
.scoped(client().get(format!(
"{}/timeline/run?runId={}",
super::get_url(),
encode(run_id)
)))
.send()
.await?;
let run: Value = ok(response).await?.json().await?;
Ok(run["overlay"]
.as_array()
.into_iter()
.flatten()
.filter_map(trust_edge)
.collect())
}
pub fn hydrate(&self, state: &Value) {
{
let mut timeline = self.timeline();
if let Some(id) = state["id"].as_str() {
timeline.timeline_id = id.to_string();
}
if let Some(name) = state["name"].as_str() {
timeline.name = name.to_string();
}
if let Some(owner) = state["owner"].as_str() {
timeline.owner = owner.to_string();
}
}
self.apply_repository(&state["repository"]);
let agents: HashMap<String, Agent> = state["agents"]
.as_array()
.into_iter()
.flatten()
.filter_map(|agent| Agent::from_json(agent, self))
.map(|agent| (agent.id.clone(), agent))
.collect();
let mut timeline = self.timeline();
timeline.status = state["status"].as_str().unwrap_or("IDLE").to_string();
timeline.agents = agents;
timeline.polls = state["polls"]
.as_array()
.into_iter()
.flatten()
.map(Poll::from_json)
.map(|poll| (poll.id.clone(), poll))
.collect();
timeline.runs = state["runs"]
.as_array()
.into_iter()
.flatten()
.map(Run::new)
.map(|run| (run.id.clone(), run))
.collect();
timeline.nodes = state["graph"]["nodes"]
.as_array()
.into_iter()
.flatten()
.filter_map(|node| serde_json::from_value::<GraphNodeWire>(node.clone()).ok())
.filter(|node| !node.id.is_empty())
.map(|node| (node.id.clone(), GraphNode::from_json(&node, None)))
.collect();
timeline.edges = state["graph"]["edges"]
.as_array()
.into_iter()
.flatten()
.filter(|edge| edge["id"].as_str().is_some_and(|id| !id.is_empty()))
.map(GraphEdge::from_json)
.map(|edge| (edge.id.clone(), edge))
.collect();
timeline.trust_edges = state["trustEdges"]
.as_array()
.into_iter()
.flatten()
.filter_map(trust_edge)
.map(|edge| (format!("{}->{}", edge.from, edge.to), edge))
.collect();
timeline.file_access_codes = state["fileAccessCodes"]
.as_object()
.into_iter()
.flatten()
.filter_map(|(node_id, meta)| {
let meta: FileAccessCodeMeta = serde_json::from_value(meta.clone()).ok()?;
(!meta.code.is_empty()).then(|| (node_id.clone(), meta))
})
.collect();
let node_ids: Vec<String> = timeline.file_access_codes.keys().cloned().collect();
let authorization = self.get_authorization().clone();
for node_id in node_ids {
attach_drive_file(&mut timeline, &authorization, &node_id);
}
}
pub fn handle_event(&self, packet: &Value) -> Option<BoxFuture<'static, Result<()>>> {
let event = packet["event"].as_str().unwrap_or_default();
match event {
"upsert-agent" => {
if let Some(agent) = Agent::from_json(pick(packet, "agent"), self) {
self.timeline().agents.insert(agent.id.clone(), agent);
}
}
"remove-agent" => {
if let Some(id) = id_of(packet) {
self.timeline().agents.remove(&id);
}
}
"upsert-poll" => {
let poll = pick(packet, "poll");
if !poll["id"].is_null() {
let poll = Poll::from_json(poll);
self.timeline().polls.insert(poll.id.clone(), poll);
}
}
"remove-poll" => {
if let Some(id) = id_of(packet) {
self.timeline().polls.remove(&id);
}
}
"upsert-run" | "run-status" => {
let run = pick(packet, "run");
if run["id"].as_str().is_some_and(|id| !id.is_empty()) {
let run = Run::new(run);
self.timeline().runs.insert(run.id.clone(), run);
}
}
"remove-run" => {
if let Some(id) = id_of(packet) {
self.timeline().runs.remove(&id);
}
}
"status" => {
let status = if packet["status"].is_null() {
&packet["data"]["status"]
} else {
&packet["status"]
};
if let Some(status) = status.as_str() {
self.timeline().status = status.to_string();
}
}
"update-timeline" => {
let timeline = if packet["timeline"].is_null() {
&packet["data"]["timeline"]
} else {
&packet["timeline"]
};
if let Some(name) = timeline["name"].as_str().filter(|name| !name.is_empty()) {
self.timeline().name = name.to_string();
}
if timeline.get("repository").is_some() {
self.apply_repository(&timeline["repository"]);
}
}
"timeline-removed" => self.timeline().removed = true,
"upsert-graph-node" => return self.upsert_graph_node_frame(packet),
"remove-graph-node" => {
if let Some(id) = id_of(packet) {
let mut timeline = self.timeline();
timeline.nodes.remove(&id);
timeline.file_access_codes.remove(&id);
timeline
.edges
.retain(|_, edge| edge.from_node_id != id && edge.to_node_id != id);
}
}
"upsert-graph-edge" => {
let edge = pick(packet, "edge");
if edge["id"].as_str().is_some_and(|id| !id.is_empty()) {
let edge = GraphEdge::from_json(edge);
self.timeline().edges.insert(edge.id.clone(), edge);
}
}
"remove-graph-edge" => {
if let Some(id) = id_of(packet) {
self.timeline().edges.remove(&id);
}
}
"upsert-trust-edge" => {
if let Some(edge) = trust_edge(pick(packet, "edge")) {
self.timeline()
.trust_edges
.insert(format!("{}->{}", edge.from, edge.to), edge);
}
}
"remove-trust-edge" => {
let field = |key: &str| {
if packet[key].is_null() {
packet["data"][key].as_str()
} else {
packet[key].as_str()
}
};
if let (Some(from), Some(to)) = (field("from"), field("to"))
&& !from.is_empty()
&& !to.is_empty()
{
self.timeline().trust_edges.remove(&format!("{from}->{to}"));
}
}
_ => {}
}
None
}
fn upsert_graph_node_frame(&self, packet: &Value) -> Option<BoxFuture<'static, Result<()>>> {
let node = pick(packet, "node");
let access_code = if packet["accessCode"].is_null() {
&packet["data"]["accessCode"]
} else {
&packet["accessCode"]
};
let wire: Option<GraphNodeWire> = serde_json::from_value(node.clone())
.ok()
.filter(|node: &GraphNodeWire| !node.id.is_empty());
let wire = wire?;
let authorization = self.get_authorization().clone();
let mut timeline = self.timeline();
let incoming = merge_previous(
timeline.nodes.get(&wire.id),
GraphNode::from_json(&wire, None),
);
timeline.nodes.insert(wire.id.clone(), incoming);
if let Ok(meta) = serde_json::from_value::<FileAccessCodeMeta>(access_code.clone())
&& !meta.code.is_empty()
{
timeline.file_access_codes.insert(wire.id.clone(), meta);
attach_drive_file(&mut timeline, &authorization, &wire.id);
return None;
}
let needs_file = match timeline.nodes.get(&wire.id) {
Some(GraphNode::File(_)) => true,
Some(GraphNode::X(node)) => node.wire_path.is_some() || !node.media.is_empty(),
Some(GraphNode::LinkedIn(node)) => node.wire_path.is_some() || !node.media.is_empty(),
_ => false,
};
if needs_file && !timeline.file_access_codes.contains_key(&wire.id) {
drop(timeline);
let room = self.clone();
let id = wire.id;
return Some(Box::pin(async move {
room.remint_file_access_code(&id).await?;
Ok(())
}));
}
None
}
pub fn upsert_graph_node(&self, node: GraphNode) -> GraphNode {
let next = {
let mut timeline = self.timeline();
let next = merge_file(timeline.nodes.get(node.id()), node);
timeline.nodes.insert(next.id().to_string(), next.clone());
next
};
self.post(&json!({ "event": "upsert-graph-node", "node": next.to_json() }));
next
}
pub fn remove_graph_node(&self, id: &str) {
if id == "start" {
return;
}
{
let mut timeline = self.timeline();
timeline.nodes.remove(id);
timeline.file_access_codes.remove(id);
timeline
.edges
.retain(|_, edge| edge.from_node_id != id && edge.to_node_id != id);
}
self.post(&json!({ "event": "remove-graph-node", "id": id }));
}
pub fn upsert_graph_edge(&self, edge: GraphEdge) -> GraphEdge {
self.timeline().edges.insert(edge.id.clone(), edge.clone());
self.post(&json!({ "event": "upsert-graph-edge", "edge": edge.to_json() }));
edge
}
pub fn remove_graph_edge(&self, id: &str) {
self.timeline().edges.remove(id);
self.post(&json!({ "event": "remove-graph-edge", "id": id }));
}
pub async fn upload_file_node(
&self,
file: Vec<u8>,
opts: UploadFileOptions,
) -> Result<UploadedFile> {
let content_type = opts
.content_type
.filter(|content_type| !content_type.is_empty())
.unwrap_or_else(|| "application/octet-stream".to_string());
let mut request = self
.scoped(client().post(format!("{}/timeline/graph/file", super::get_url())))
.header("Content-Type", content_type)
.header("Filename", &opts.name)
.header("X", opts.x.to_string())
.header("Y", opts.y.to_string());
if let Some(node_id) = opts.node_id.filter(|id| !id.is_empty()) {
request = request.header("Node-Id", node_id);
}
if let Some(kind) = opts.kind {
request = request.header(
"Kind",
serde_json::to_value(kind)?.as_str().unwrap_or_default(),
);
}
let json: Value = ok(request.body(file).send().await?).await?.json().await?;
let access_code: FileAccessCodeMeta = serde_json::from_value(json["accessCode"].clone())?;
let wire: GraphNodeWire = serde_json::from_value(json["node"].clone())?;
let handle = from_access_code(
self.get_authorization().clone(),
&access_code.code,
FileMeta {
name: access_code.name.clone(),
content_type: access_code.content_type.clone(),
..FileMeta::default()
},
);
let node = GraphNode::from_json(&wire, Some(FileOrFiles::File(handle)));
{
let mut timeline = self.timeline();
timeline.nodes.insert(node.id().to_string(), node.clone());
timeline
.file_access_codes
.insert(node.id().to_string(), access_code.clone());
}
Ok(UploadedFile { node, access_code })
}
pub async fn remint_file_access_code(&self, node_id: &str) -> Result<FileAccessCodeMeta> {
let response = self
.scoped(client().post(format!(
"{}/timeline/graph/file/access-code",
super::get_url()
)))
.json(&json!({ "nodeId": node_id }))
.send()
.await?;
let meta: FileAccessCodeMeta = ok(response).await?.json().await?;
let authorization = self.get_authorization().clone();
let mut timeline = self.timeline();
timeline
.file_access_codes
.insert(node_id.to_string(), meta.clone());
attach_drive_file(&mut timeline, &authorization, node_id);
Ok(meta)
}
pub async fn file_for_node(&self, node_id: &str) -> Result<drive::File> {
let meta = self.open_graph_file(node_id).await?;
Ok(from_access_code(
self.get_authorization().clone(),
&meta.code,
FileMeta {
name: meta.name,
content_type: meta.content_type,
..FileMeta::default()
},
))
}
pub async fn open_graph_file(&self, node_id: &str) -> Result<FileAccessCodeMeta> {
let known = self
.timeline()
.file_access_codes
.get(node_id)
.filter(|meta| !meta.code.is_empty())
.cloned();
match known {
Some(meta) => Ok(meta),
None => self.remint_file_access_code(node_id).await,
}
}
pub fn upsert_agent(&self, identity: Identity) -> Agent {
let agent = {
let mut timeline = self.timeline();
let preferred = identity.id.trim().to_string();
let id = if !preferred.is_empty() && !timeline.agents.contains_key(&preferred) {
preferred
} else {
let mut n = 0;
while timeline.agents.contains_key(&n.to_string()) {
n += 1;
}
n.to_string()
};
let agent = Agent {
room: self.room.clone(),
parent: Arc::downgrade(&self.state),
id,
persona: identity,
repository: None,
};
timeline.agents.insert(agent.id.clone(), agent.clone());
agent
};
self.post(&json!({ "event": "upsert-agent", "agent": agent.to_json() }));
agent
}
pub fn remove_agent(&self, id: &str) {
self.timeline().agents.remove(id);
self.post(&json!({ "event": "remove-agent", "id": id }));
}
pub fn upsert_poll(&self, goal: &str, query: Vec<AntFormatOrFormat>) -> Poll {
let poll = {
let mut timeline = self.timeline();
let mut id = 0;
while timeline.polls.contains_key(&id.to_string()) {
id += 1;
}
let poll = Poll::new(&id.to_string(), goal, query);
timeline.polls.insert(poll.id.clone(), poll.clone());
poll
};
self.post(&json!({ "event": "upsert-poll", "poll": poll.to_json() }));
poll
}
pub fn remove_poll(&self, id: &str) {
self.timeline().polls.remove(id);
self.post(&json!({ "event": "remove-poll", "id": id }));
}
pub async fn start_run(&self, goal: &str, query: &[AntFormatOrFormat]) -> Result<String> {
let response = self
.scoped(client().post(format!("{}/timeline/run", super::get_url())))
.json(&json!({
"goal": goal,
"query": query.iter().map(AntFormatOrFormat::to_json).collect::<Vec<_>>(),
}))
.send()
.await?;
let json: Value = ok(response).await?.json().await?;
Ok(match &json["runId"] {
Value::String(id) => id.clone(),
Value::Null => String::new(),
id => id.to_string(),
})
}
pub async fn abort_run(&self, run_id: &str) -> Result<()> {
let response = self
.scoped(client().post(format!("{}/timeline/run/abort", super::get_url())))
.json(&json!({ "runId": run_id }))
.send()
.await?;
ok(response).await?;
Ok(())
}
pub async fn get_chat(&self, hooks: Option<Arc<dyn RoomHooks>>) -> Result<TimelineChat> {
self.open_chat("", hooks).await
}
pub async fn open_chat(
&self,
agent_id: &str,
hooks: Option<Arc<dyn RoomHooks>>,
) -> Result<TimelineChat> {
let response = self
.scoped(client().post(format!("{}/timeline/chat/ensure", super::get_url())))
.json(&json!({ "agentId": agent_id }))
.send()
.await?;
let json: Value = ok(response).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(self.get_authorization(), &target, hooks).await?;
Ok(TimelineChat { room, me, chat_id })
}
pub async fn connect(
authorization: &CredentialsOrApiKey,
target_id: &str,
opts: ConnectOptions,
) -> Result<(TimelineRoom, Presence)> {
TimelineRoom::open(
Authorization::Server {
authorization: authorization.clone(),
server: opts.server.unwrap_or_else(|| target_id.to_string()),
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<(TimelineRoom, Presence)> {
TimelineRoom::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<(TimelineRoom, Presence)> {
let state = Arc::new(TimelineState {
timeline: Mutex::new(Timeline::default()),
watchers: Mutex::new(Vec::new()),
next_watcher: AtomicU64::new(0),
});
let me = connect_server(
auth,
&format!("{}/timeline", super::get_url()),
Arc::new(TimelineHooks {
state: state.clone(),
user: hooks.unwrap_or_else(|| Arc::new(())),
}),
)
.await?;
Ok((
TimelineRoom {
room: me.room().clone(),
state,
},
me,
))
}
}
fn merge_file(previous: Option<&GraphNode>, next: GraphNode) -> GraphNode {
match (previous, next) {
(Some(GraphNode::File(previous)), GraphNode::File(mut next)) => {
if next.file.is_none() {
next.file = previous.file.clone();
}
if next.wire_path.as_deref().is_none_or(str::is_empty) {
next.wire_path = previous.wire_path.clone();
}
GraphNode::File(next)
}
(_, next) => next,
}
}
fn merge_previous(previous: Option<&GraphNode>, next: GraphNode) -> GraphNode {
let previous_media = match previous {
Some(GraphNode::X(node)) => Some(&node.media),
Some(GraphNode::LinkedIn(node)) => Some(&node.media),
_ => None,
};
match (previous_media, merge_file(previous, next)) {
(Some(media), GraphNode::X(mut node)) if node.media.is_empty() && !media.is_empty() => {
node.media = media.clone();
GraphNode::X(node)
}
(Some(media), GraphNode::LinkedIn(mut node))
if node.media.is_empty() && !media.is_empty() =>
{
node.media = media.clone();
GraphNode::LinkedIn(node)
}
(_, next) => next,
}
}
fn attach_drive_file(timeline: &mut Timeline, authorization: &CredentialsOrApiKey, node_id: &str) {
let Some(meta) = timeline
.file_access_codes
.get(node_id)
.filter(|meta| !meta.code.is_empty())
.cloned()
else {
return;
};
let Some(node) = timeline.nodes.get_mut(node_id) else {
return;
};
let file = from_access_code(
authorization.clone(),
&meta.code,
FileMeta {
name: meta.name,
content_type: meta.content_type,
..FileMeta::default()
},
);
match node {
GraphNode::File(node) => node.file = Some(file),
GraphNode::X(node) => match node.media.first_mut() {
Some(first) => *first = file,
None => node.media = vec![file],
},
GraphNode::LinkedIn(node) => match node.media.first_mut() {
Some(first) => *first = file,
None => node.media = vec![file],
},
_ => {}
}
}
struct TimelineHooks {
state: Arc<TimelineState>,
user: Arc<dyn Hooks>,
}
impl TimelineHooks {
fn room(&self, room: &Room) -> TimelineRoom {
TimelineRoom {
room: room.clone(),
state: self.state.clone(),
}
}
}
impl Hooks for TimelineHooks {
fn create_room(&self, room: &Room, state: &Value) {
lock(&self.state.timeline).timeline_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 {
if let Some(pending) = self.room(room).handle_event(packet) {
self.user.on_event(room, packet);
let user = self.user.clone();
let room = room.clone();
let packet = packet.clone();
tokio::spawn(async move {
let _ = pending.await;
user.on_event(&room, &packet);
});
return true;
}
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);
}
}
pub async fn observe_agent(
authorization: &CredentialsOrApiKey,
timeline_id: &str,
agent_id: &str,
content: &str,
opts: AccessOptions,
) -> Result<Memory> {
let request = authorized(
client().post(format!("{}/timeline/agent/observe", super::get_url())),
authorization,
None,
);
let response = scoped(
request,
timeline_id,
opts.group.as_ref(),
opts.access_code.as_deref(),
)
.json(&json!({ "agentId": agent_id, "content": content }))
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
#[derive(Debug, Clone, Default)]
pub struct GenerateOptions {
pub group: Option<Group>,
pub access_code: Option<String>,
pub tools: Option<Vec<Tool>>,
}
pub async fn generate_agent(
authorization: &CredentialsOrApiKey,
timeline_id: &str,
agent_id: &str,
messages: &[Content],
opts: GenerateOptions,
) -> Result<BoxStream<'static, StreamEvent>> {
let auth = socket_auth(
authorization,
("Timeline", timeline_id),
opts.group.as_ref(),
opts.access_code.as_deref(),
);
let url = format!(
"{}/timeline/agent/generate?authorization={auth}",
websocket(&super::get_url())
);
let tools = opts.tools.unwrap_or_default();
let messages = serialize_messages(authorization, messages).await?;
let functions: Vec<&crate::parrot::generative::Function> =
tools.iter().map(|tool| &tool.function).collect();
let generate = json!({ "event": "generate", "agentId": agent_id, "messages": messages, "tools": functions });
agent_stream(url, generate, tools).await
}
#[cfg(test)]
mod tests {
use serde_json::json;
use tokio::sync::mpsc;
use super::*;
use crate::authentification::ApiKey;
use crate::drive::DriveAuthorization;
fn timeline_room() -> TimelineRoom {
let (ws, _outbox) = mpsc::unbounded_channel();
TimelineRoom {
room: Room::new(
"http://localhost/timeline".to_string(),
ws,
Authorization::Server {
authorization: CredentialsOrApiKey::ApiKey(ApiKey::default()),
server: "t1".to_string(),
group: Some("4".to_string()),
},
"t1".to_string(),
),
state: Arc::new(TimelineState {
timeline: Mutex::new(Timeline::default()),
watchers: Mutex::new(Vec::new()),
next_watcher: AtomicU64::new(0),
}),
}
}
#[test]
fn runs_and_polls_read_the_wire() {
let run = Run::new(&json!({
"id": "r1", "pollId": "0", "status": "aborting", "agentIds": ["0"],
"polls": { "progress": { "activeNodeIds": ["n1"], "byStimulus": { "n1": { "stimulusLabel": "Post", "phase": "browse", "done": 1, "total": 3 } } } },
"trajectories": [{ "id": "t", "nodes": [{ "id": "n1", "kind": "x", "title": "Post" }] }],
"startedAt": 5
}));
assert_eq!(run.status, RunStatus::Running);
assert_eq!(
run.progress.unwrap().by_stimulus["n1"].phase,
StimulusPhase::Browse
);
assert_eq!(run.trajectories[0].nodes[0].title, "Post");
assert!(run_is_active(Some("pending")));
assert!(!run_is_active(Some("done")));
let poll = Poll::from_json(&json!({
"id": "0", "goal": "Would they share it?",
"query": [{ "type": "linkedin", "answer": "Share" }, "not a format"]
}));
assert_eq!(poll.query.len(), 1);
let json = poll.clone().to_json();
assert_eq!(json["query"][0]["type"], "linkedin");
assert_eq!(json["query"][0]["answer"], "Share");
}
#[test]
fn timeline_rooms_follow_graph_frames_and_attach_files() {
let room = timeline_room();
room.hydrate(&json!({
"id": "t1", "name": "Launch", "owner": "group:4", "status": "RUNNING",
"graph": {
"nodes": [
{ "id": "start", "kind": "start", "x": 0, "y": 0, "label": "Start" },
{ "id": "f1", "kind": "file", "x": 1, "y": 1, "label": "deck.pdf", "filePath": "ant/deck.pdf" }
],
"edges": [{ "id": "e1", "fromNodeId": "start", "toNodeId": "f1" }]
},
"fileAccessCodes": { "f1": { "code": "abc", "name": "deck.pdf", "contentType": "application/pdf" } },
"trustEdges": [{ "from": "0", "to": "1", "weight": 0.4 }, { "from": "1", "to": "0", "weight": 0 }]
}));
assert_eq!(room.status(), "RUNNING");
assert_eq!(room.trust_edges().len(), 1);
let GraphNode::File(file) = &room.nodes()["f1"] else {
panic!("expected a file node");
};
assert!(matches!(
&file.file.as_ref().unwrap().authorization,
DriveAuthorization::AccessCode { access_code, .. } if access_code == "abc"
));
assert!(room
.handle_event(&json!({ "event": "upsert-graph-node", "node": { "id": "f1", "kind": "file", "x": 5, "y": 5, "label": "deck.pdf" } }))
.is_none());
let GraphNode::File(file) = &room.nodes()["f1"] else {
panic!("expected a file node");
};
assert_eq!(file.x, 5.0);
assert_eq!(file.wire_path.as_deref(), Some("ant/deck.pdf"));
assert!(file.file.is_some());
assert!(room
.handle_event(&json!({ "event": "upsert-graph-node", "node": { "id": "f2", "kind": "file", "x": 0, "y": 0, "label": "new.png" } }))
.is_some());
room.handle_event(&json!({ "event": "remove-graph-node", "data": { "id": "f1" } }));
assert!(room.edges().is_empty());
room.handle_event(&json!({ "event": "timeline-removed" }));
assert!(room.removed());
}
}