use std::path::PathBuf;
use serde::Serialize;
use tam_proto::{
AgentError, AgentInfo, ChatInfo, DeniedTool, MemberHealth, MemberInfo, MemberOutcome, Pending,
RunInfo, TaskInfo,
};
use crate::chat::Chat;
use crate::task::{Member, Task};
pub const SCHEMA_VERSION: u32 = 1;
#[derive(Debug, Serialize)]
pub struct ErrorInfo {
pub kind: &'static str,
pub message: String,
}
#[derive(Serialize)]
pub struct Failure {
pub schema_version: u32,
pub error: ErrorInfo,
}
#[derive(Serialize)]
pub struct Ps {
pub schema_version: u32,
pub tasks: Vec<PsTask>,
pub chats: Vec<PsChat>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Subject {
Task(String),
Chat(String),
}
#[derive(Serialize)]
pub struct PsTask {
pub name: String,
pub status: String,
pub dir: Option<PathBuf>,
pub agent: Option<String>,
pub context_percent: Option<u8>,
pub run_count: usize,
pub last_activity: Option<u64>,
pub created_at: u64,
pub members: Vec<PsMember>,
}
#[derive(Serialize)]
pub struct PsMember {
pub id: String,
pub name: String,
pub repo: String,
pub branch: String,
pub dir: PathBuf,
pub status: String,
pub agent: Option<String>,
pub health: Option<MemberHealth>,
pub label: String,
pub repo_root: Option<PathBuf>,
pub state: Option<String>,
pub pid: Option<u32>,
pub viewers: Option<usize>,
pub context_percent: Option<u8>,
pub session_id: Option<String>,
pub pending: Option<PendingView>,
pub turn_error: Option<TurnError>,
pub denied: Option<Denied>,
pub last_message: Option<String>,
pub last_run: Option<Run>,
}
#[derive(Serialize)]
pub struct PendingView {
pub tool: Option<String>,
pub summary: String,
}
#[derive(Serialize)]
pub struct TurnError {
pub kind: String,
pub message: Option<String>,
}
#[derive(Serialize)]
pub struct Denied {
pub tool: String,
pub summary: Option<String>,
pub reason: Option<String>,
}
#[derive(Serialize)]
pub struct Run {
pub provider: String,
pub session_id: Option<String>,
pub started_at: u64,
pub ended_at: Option<u64>,
pub exit_code: Option<i32>,
}
#[derive(Serialize)]
pub struct Receipt {
pub schema_version: u32,
#[serde(flatten)]
pub of: Subject,
pub members: Vec<Started>,
pub error: Option<ErrorInfo>,
}
#[derive(Serialize)]
pub struct Started {
pub id: String,
pub name: Option<String>,
pub label: String,
pub repo: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub branch: Option<String>,
pub dir: PathBuf,
pub agent: Option<String>,
pub resumed_session: Option<String>,
}
#[derive(Serialize)]
pub struct Outcomes {
pub schema_version: u32,
#[serde(flatten)]
pub of: Subject,
pub members: Vec<Outcome>,
pub error: Option<ErrorInfo>,
}
#[derive(Serialize)]
pub struct Outcome {
pub id: String,
pub name: Option<String>,
pub label: String,
pub state: String,
pub agent: Option<String>,
pub session_id: Option<String>,
pub exit_code: Option<i32>,
pub pending: Option<PendingView>,
pub turn_error: Option<TurnError>,
pub response: Option<String>,
pub response_at: Option<u64>,
}
impl Ps {
pub fn new(
tasks: Vec<(TaskInfo, Vec<RunInfo>)>,
chats: Vec<(ChatInfo, Vec<RunInfo>)>,
agents: &[AgentInfo],
) -> Self {
let mut tasks: Vec<(u8, PsTask)> = tasks
.into_iter()
.map(|(info, runs)| {
let created_at = info.created_at;
let task = Task::from_info(info, agents);
let entry = PsTask {
name: task.name.clone(),
status: task.status().to_string(),
dir: task.dir().map(|d| d.to_path_buf()),
agent: task.agent_info().map(|a| a.provider.clone()),
context_percent: task.agent_info().and_then(|a| a.context_percent),
run_count: task.run_count,
last_activity: task.last_activity,
created_at,
members: task
.members
.iter()
.map(|m| PsMember::new(&task, m, &runs))
.collect(),
};
(task.status().sort_priority(), entry)
})
.collect();
tasks.sort_by(|(a, x), (b, y)| (a, &x.name).cmp(&(b, &y.name)));
let mut chats: Vec<(Chat, Vec<RunInfo>)> = chats
.into_iter()
.map(|(info, runs)| (Chat::new(info, agents), runs))
.collect();
chats.sort_by_key(|(chat, _)| chat.status().sort_priority());
Self {
schema_version: SCHEMA_VERSION,
tasks: tasks.into_iter().map(|(_, t)| t).collect(),
chats: chats
.iter()
.map(|(chat, runs)| PsChat::new(chat, runs))
.collect(),
}
}
}
impl PsMember {
fn new(task: &Task, member: &Member, runs: &[RunInfo]) -> Self {
let info = &member.info;
let last_run = runs.iter().rev().find(|r| r.member == info.id);
let agent = member.agent_info.as_ref();
let detail = agent.map(|a| a.detail.as_ref());
Self {
id: info.id.clone(),
name: info.name.clone(),
repo: info.repo.clone(),
branch: info.branch.clone(),
dir: info.dir.clone(),
status: task.member_status(member).to_string(),
agent: agent.map(|a| a.provider.clone()),
health: info.health.clone(),
label: task.member_label(member),
repo_root: info.repo_root.clone(),
state: agent.map(|a| a.state.to_string()),
pid: agent.and_then(|a| a.pid),
viewers: agent.map(|a| a.viewers),
context_percent: agent.and_then(|a| a.context_percent),
session_id: detail
.and_then(|d| d.session_id.clone())
.or_else(|| last_run.and_then(|r| r.session_id.clone())),
pending: detail
.and_then(|d| d.pending.as_ref())
.map(PendingView::from),
turn_error: detail.and_then(|d| d.error.as_ref()).map(TurnError::from),
denied: detail.and_then(|d| d.denied.as_ref()).map(Denied::from),
last_message: detail.and_then(|d| d.last_message.clone()),
last_run: last_run.map(Run::from),
}
}
}
impl From<&Pending> for PendingView {
fn from(p: &Pending) -> Self {
Self {
tool: p.tool.clone(),
summary: p.summary.clone(),
}
}
}
impl From<&AgentError> for TurnError {
fn from(e: &AgentError) -> Self {
Self {
kind: e.kind.clone(),
message: e.message.clone(),
}
}
}
impl From<&DeniedTool> for Denied {
fn from(d: &DeniedTool) -> Self {
Self {
tool: d.tool.clone(),
summary: d.summary.clone(),
reason: d.reason.clone(),
}
}
}
impl From<&RunInfo> for Run {
fn from(r: &RunInfo) -> Self {
Self {
provider: r.provider.clone(),
session_id: r.session_id.clone(),
started_at: r.started_at,
ended_at: r.ended_at,
exit_code: r.exit_code,
}
}
}
impl Started {
pub fn new(task: &TaskInfo, member: &MemberInfo) -> Self {
Self {
id: member.id.clone(),
name: Some(member.name.clone()),
label: task.member_label(member),
repo: member.repo.clone(),
branch: Some(member.branch.clone()),
dir: member.dir.clone(),
agent: None,
resumed_session: None,
}
}
pub fn chat(chat: &ChatInfo) -> Self {
Self {
id: chat.id.clone(),
name: chat.name.clone(),
label: chat.label(),
repo: chat.repo.clone(),
branch: None,
dir: chat.dir.clone(),
agent: None,
resumed_session: None,
}
}
}
impl Outcomes {
pub fn new(task: &TaskInfo, outcomes: &[MemberOutcome], error: Option<ErrorInfo>) -> Self {
let name = |o: &MemberOutcome| {
task.member(&o.member)
.map(|m| m.name.clone())
.unwrap_or_else(|| o.label.clone())
};
Self {
schema_version: SCHEMA_VERSION,
of: Subject::Task(task.name.clone()),
members: outcomes
.iter()
.map(|o| Outcome::new(Some(name(o)), o))
.collect(),
error,
}
}
pub fn chat(chat: &ChatInfo, outcomes: &[MemberOutcome], error: Option<ErrorInfo>) -> Self {
Self {
schema_version: SCHEMA_VERSION,
of: Subject::Chat(chat.id.clone()),
members: outcomes
.iter()
.map(|o| Outcome::new(chat.name.clone(), o))
.collect(),
error,
}
}
}
impl Outcome {
fn new(name: Option<String>, o: &MemberOutcome) -> Self {
Self {
id: o.member.clone(),
name,
label: o.label.clone(),
state: o.state.to_string(),
agent: o.provider.clone(),
session_id: o.session_id.clone(),
exit_code: o.exit_code,
pending: o.pending.as_ref().map(PendingView::from),
turn_error: o.error.as_ref().map(TurnError::from),
response: o.response.as_ref().map(|r| r.text.clone()),
response_at: o.response.as_ref().map(|r| r.at),
}
}
}
#[derive(Serialize)]
pub struct Moved {
pub schema_version: u32,
pub task: String,
pub from: Vec<String>,
pub members: Vec<MovedMember>,
pub error: Option<ErrorInfo>,
}
#[derive(Serialize)]
pub struct MovedMember {
pub id: String,
pub task: String,
pub name: String,
pub label: String,
pub previous_label: Option<String>,
pub moved: bool,
pub repo: String,
pub branch: String,
pub dir: PathBuf,
pub agent: Option<String>,
pub notified: bool,
}
#[derive(Serialize)]
pub struct PsChat {
pub id: String,
pub name: Option<String>,
pub label: String,
pub title: Option<String>,
pub last_prompt: Option<String>,
pub repo: String,
pub dir: PathBuf,
pub status: String,
pub keep: bool,
pub closes_at: Option<u64>,
pub created_at: u64,
pub last_turn_at: Option<u64>,
pub run_count: usize,
pub agent: Option<String>,
pub state: Option<String>,
pub pid: Option<u32>,
pub viewers: Option<usize>,
pub context_percent: Option<u8>,
pub session_id: Option<String>,
pub pending: Option<PendingView>,
pub turn_error: Option<TurnError>,
pub denied: Option<Denied>,
pub last_message: Option<String>,
pub last_run: Option<Run>,
}
impl PsChat {
fn new(chat: &Chat, runs: &[RunInfo]) -> Self {
let info = &chat.info;
let agent = chat.agent.as_ref();
let detail = agent.map(|a| a.detail.as_ref());
Self {
id: info.id.clone(),
name: info.name.clone(),
label: info.label(),
title: info.title.clone(),
last_prompt: info.last_prompt.clone(),
repo: info.repo.clone(),
dir: info.dir.clone(),
status: chat.status().to_string(),
keep: info.keep,
closes_at: info.closes_at,
created_at: info.created_at,
last_turn_at: info.last_turn_at,
run_count: info.run_count,
agent: agent.map(|a| a.provider.clone()),
state: agent.map(|a| a.state.to_string()),
pid: agent.and_then(|a| a.pid),
viewers: agent.map(|a| a.viewers),
context_percent: agent.and_then(|a| a.context_percent),
session_id: detail
.and_then(|d| d.session_id.clone())
.or_else(|| info.session.clone()),
pending: detail
.and_then(|d| d.pending.as_ref())
.map(PendingView::from),
turn_error: detail.and_then(|d| d.error.as_ref()).map(TurnError::from),
denied: detail.and_then(|d| d.denied.as_ref()).map(Denied::from),
last_message: detail.and_then(|d| d.last_message.clone()),
last_run: runs.last().map(Run::from),
}
}
}
#[derive(Serialize)]
pub struct ChatDoc {
pub schema_version: u32,
pub chat: String,
pub name: Option<String>,
pub label: String,
pub repo: String,
pub dir: PathBuf,
pub keep: bool,
pub session_id: Option<String>,
pub created_at: u64,
pub last_turn_at: Option<u64>,
pub run_count: usize,
pub open: bool,
}
impl ChatDoc {
pub fn new(chat: &ChatInfo, open: bool) -> Self {
Self {
schema_version: SCHEMA_VERSION,
chat: chat.id.clone(),
name: chat.name.clone(),
label: chat.label(),
repo: chat.repo.clone(),
dir: chat.dir.clone(),
keep: chat.keep,
session_id: chat.session.clone(),
created_at: chat.created_at,
last_turn_at: chat.last_turn_at,
run_count: chat.run_count,
open,
}
}
}