use serde::{Deserialize, Serialize};
macro_rules! id_type {
($name:ident) => {
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
#[serde(transparent)]
pub struct $name(pub u32);
impl $name {
pub const fn new(value: u32) -> Self {
Self(value)
}
pub const fn raw(self) -> u32 {
self.0
}
}
};
}
id_type!(WorkspaceId);
id_type!(HarnessId);
impl HarnessId {
pub const ACTIVITY: Self = Self(u32::MAX);
pub const STATS: Self = Self(u32::MAX - 1);
pub fn is_screen(self) -> bool {
self == Self::ACTIVITY || self == Self::STATS
}
}
id_type!(SubagentId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessKind {
ClaudeCode,
OpenCode,
Pi,
Shell,
}
impl HarnessKind {
pub fn badge(self) -> &'static str {
match self {
HarnessKind::ClaudeCode => "CC",
HarnessKind::OpenCode => "OC",
HarnessKind::Pi => "PI",
HarnessKind::Shell => "SH",
}
}
pub fn label(self) -> &'static str {
match self {
HarnessKind::ClaudeCode => "Claude Code",
HarnessKind::OpenCode => "opencode",
HarnessKind::Pi => "pi",
HarnessKind::Shell => "shell",
}
}
pub fn default_command(self) -> &'static str {
match self {
HarnessKind::ClaudeCode => "claude",
HarnessKind::OpenCode => "opencode",
HarnessKind::Pi => "pi",
HarnessKind::Shell => "$SHELL",
}
}
pub const ALL: [HarnessKind; 4] = [
HarnessKind::ClaudeCode,
HarnessKind::OpenCode,
HarnessKind::Pi,
HarnessKind::Shell,
];
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessStatus {
Working,
Idle,
NeedsInput,
Error,
}
impl HarnessStatus {
pub fn label(self) -> &'static str {
match self {
HarnessStatus::Working => "working",
HarnessStatus::Idle => "idle",
HarnessStatus::NeedsInput => "needs input",
HarnessStatus::Error => "error",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Subagent {
pub id: SubagentId,
pub label: String,
pub detail: String,
pub elapsed: String,
pub working: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Harness {
pub id: HarnessId,
pub workspace: WorkspaceId,
pub name: String,
pub kind: HarnessKind,
pub cwd: String,
pub status: HarnessStatus,
#[serde(default)]
pub subagents: Vec<Subagent>,
#[serde(default)]
pub unread: u16,
#[serde(default)]
pub tag: String,
#[serde(default)]
pub title: String,
#[serde(default)]
pub pronouns: String,
#[serde(default)]
pub color: Option<usize>,
#[serde(default = "default_true")]
pub announced: bool,
#[serde(default = "default_true")]
pub started: bool,
}
fn default_true() -> bool {
true
}
impl Harness {
pub fn working_subagents(&self) -> usize {
self.subagents.iter().filter(|agent| agent.working).count()
}
pub fn has_live_subagents(&self) -> bool {
self.working_subagents() > 0
}
pub fn subagent_count(&self) -> usize {
self.subagents.len()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Workspace {
pub id: WorkspaceId,
pub name: String,
pub path: String,
pub accent: usize,
pub expanded: bool,
#[serde(default)]
pub home: bool,
#[serde(default)]
pub unloaded: bool,
pub harnesses: Vec<Harness>,
}
impl Workspace {
pub fn harness(&self, id: HarnessId) -> Option<&Harness> {
self.harnesses.iter().find(|harness| harness.id == id)
}
pub fn harness_mut(&mut self, id: HarnessId) -> Option<&mut Harness> {
self.harnesses.iter_mut().find(|harness| harness.id == id)
}
pub fn working(&self) -> usize {
self.harnesses
.iter()
.filter(|harness| harness.status == HarnessStatus::Working)
.count()
}
pub fn unread(&self) -> u16 {
self.harnesses.iter().map(|harness| harness.unread).sum()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Mail {
pub to: HarnessId,
pub from: String,
pub text: String,
#[serde(default)]
pub read: bool,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Activity {
pub time: String,
pub name: String,
pub message: String,
pub accent: usize,
#[serde(default)]
pub to: Option<(String, usize)>,
#[serde(default)]
pub harness: Option<HarnessId>,
#[serde(default)]
pub detail: Option<String>,
#[serde(default)]
pub link: Option<String>,
#[serde(skip)]
pub open: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Showcase {
pub id: u64,
pub from: HarnessId,
pub from_name: String,
pub title: String,
pub description: String,
pub time: String,
pub items: Vec<ShowItem>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShowItem {
pub path: std::path::PathBuf,
pub caption: String,
pub description: String,
}
pub const AGENT_COLORS: [usize; 6] = [0, 1, 3, 4, 5, 6];
pub fn agent_color(name: &str) -> usize {
let hash = name.bytes().fold(0xcbf29ce484222325u64, |h, b| {
(h ^ b as u64).wrapping_mul(0x100000001b3)
});
AGENT_COLORS[(hash >> 32) as usize % AGENT_COLORS.len()]
}
pub fn assign_agent_colors(workspaces: &mut [Workspace]) {
for harness in workspaces.iter_mut().flat_map(|w| &mut w.harnesses) {
harness.color = Some(agent_color(&harness.name));
}
}
#[cfg(test)]
mod color_tests {
use super::*;
#[test]
fn color_depends_only_on_the_name() {
assert_eq!(agent_color("Sable"), agent_color("Sable"));
assert!(AGENT_COLORS.contains(&agent_color("Quill")));
let names = ["Sable", "Quill", "Tamsin", "Okonkwo", "Wren", "Bartholomew"];
let colors: std::collections::HashSet<_> = names.iter().map(|n| agent_color(n)).collect();
assert!(colors.len() > 1);
}
}