mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
feat(desktop): add agent teams (#132)
This commit is contained in:
@@ -10,6 +10,7 @@ mod media;
|
||||
mod messages;
|
||||
mod personas;
|
||||
mod profile;
|
||||
mod teams;
|
||||
mod tokens;
|
||||
|
||||
pub use agent_discovery::*;
|
||||
@@ -24,4 +25,5 @@ pub use media::*;
|
||||
pub use messages::*;
|
||||
pub use personas::*;
|
||||
pub use profile::*;
|
||||
pub use teams::*;
|
||||
pub use tokens::*;
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
use tauri::{AppHandle, State};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
app_state::AppState,
|
||||
managed_agents::{load_teams, save_teams, CreateTeamRequest, TeamRecord, UpdateTeamRequest},
|
||||
util::now_iso,
|
||||
};
|
||||
|
||||
fn trim_required(value: &str, label: &str) -> Result<String, String> {
|
||||
let trimmed = value.trim();
|
||||
if trimmed.is_empty() {
|
||||
return Err(format!("{label} is required"));
|
||||
}
|
||||
Ok(trimmed.to_string())
|
||||
}
|
||||
|
||||
fn trim_optional(value: Option<String>) -> Option<String> {
|
||||
value.and_then(|candidate| {
|
||||
let trimmed = candidate.trim();
|
||||
(!trimmed.is_empty()).then(|| trimmed.to_string())
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn list_teams(app: AppHandle, state: State<'_, AppState>) -> Result<Vec<TeamRecord>, String> {
|
||||
let _store_guard = state
|
||||
.managed_agents_store_lock
|
||||
.lock()
|
||||
.map_err(|error| error.to_string())?;
|
||||
load_teams(&app)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn create_team(
|
||||
input: CreateTeamRequest,
|
||||
app: AppHandle,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<TeamRecord, String> {
|
||||
let name = trim_required(&input.name, "Team name")?;
|
||||
let description = trim_optional(input.description);
|
||||
let now = now_iso();
|
||||
|
||||
let _store_guard = state
|
||||
.managed_agents_store_lock
|
||||
.lock()
|
||||
.map_err(|error| error.to_string())?;
|
||||
let mut teams = load_teams(&app)?;
|
||||
let team = TeamRecord {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
name,
|
||||
description,
|
||||
persona_ids: input.persona_ids,
|
||||
created_at: now.clone(),
|
||||
updated_at: now,
|
||||
};
|
||||
teams.push(team.clone());
|
||||
save_teams(&app, &teams)?;
|
||||
Ok(team)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn update_team(
|
||||
input: UpdateTeamRequest,
|
||||
app: AppHandle,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<TeamRecord, String> {
|
||||
let name = trim_required(&input.name, "Team name")?;
|
||||
let description = trim_optional(input.description);
|
||||
|
||||
let _store_guard = state
|
||||
.managed_agents_store_lock
|
||||
.lock()
|
||||
.map_err(|error| error.to_string())?;
|
||||
let mut teams = load_teams(&app)?;
|
||||
let team = teams
|
||||
.iter_mut()
|
||||
.find(|record| record.id == input.id)
|
||||
.ok_or_else(|| format!("team {} not found", input.id))?;
|
||||
|
||||
team.name = name;
|
||||
team.description = description;
|
||||
team.persona_ids = input.persona_ids;
|
||||
team.updated_at = now_iso();
|
||||
|
||||
let updated = team.clone();
|
||||
save_teams(&app, &teams)?;
|
||||
Ok(updated)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn delete_team(
|
||||
id: String,
|
||||
app: AppHandle,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<(), String> {
|
||||
let _store_guard = state
|
||||
.managed_agents_store_lock
|
||||
.lock()
|
||||
.map_err(|error| error.to_string())?;
|
||||
let mut teams = load_teams(&app)?;
|
||||
let original_len = teams.len();
|
||||
teams.retain(|record| record.id != id);
|
||||
if teams.len() == original_len {
|
||||
return Err(format!("team {id} not found"));
|
||||
}
|
||||
save_teams(&app, &teams)
|
||||
}
|
||||
@@ -174,6 +174,10 @@ pub fn run() {
|
||||
create_persona,
|
||||
update_persona,
|
||||
delete_persona,
|
||||
list_teams,
|
||||
create_team,
|
||||
update_team,
|
||||
delete_team,
|
||||
])
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while building tauri application");
|
||||
|
||||
@@ -2,10 +2,12 @@ mod discovery;
|
||||
mod personas;
|
||||
mod runtime;
|
||||
mod storage;
|
||||
mod teams;
|
||||
mod types;
|
||||
|
||||
pub use discovery::*;
|
||||
pub use personas::*;
|
||||
pub use runtime::*;
|
||||
pub use storage::*;
|
||||
pub use teams::*;
|
||||
pub use types::*;
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
use tauri::AppHandle;
|
||||
|
||||
use crate::managed_agents::{managed_agents_base_dir, TeamRecord};
|
||||
|
||||
fn teams_store_path(app: &AppHandle) -> Result<PathBuf, String> {
|
||||
Ok(managed_agents_base_dir(app)?.join("teams.json"))
|
||||
}
|
||||
|
||||
fn sort_teams(records: &mut [TeamRecord]) {
|
||||
records.sort_by(|left, right| {
|
||||
left.name
|
||||
.to_lowercase()
|
||||
.cmp(&right.name.to_lowercase())
|
||||
.then_with(|| left.id.cmp(&right.id))
|
||||
});
|
||||
}
|
||||
|
||||
pub fn load_teams(app: &AppHandle) -> Result<Vec<TeamRecord>, String> {
|
||||
let path = teams_store_path(app)?;
|
||||
if !path.exists() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
let content =
|
||||
fs::read_to_string(&path).map_err(|error| format!("failed to read teams store: {error}"))?;
|
||||
let mut records: Vec<TeamRecord> = serde_json::from_str(&content)
|
||||
.map_err(|error| format!("failed to parse teams store: {error}"))?;
|
||||
sort_teams(&mut records);
|
||||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn save_teams(app: &AppHandle, records: &[TeamRecord]) -> Result<(), String> {
|
||||
let mut sorted = records.to_vec();
|
||||
sort_teams(&mut sorted);
|
||||
|
||||
let path = teams_store_path(app)?;
|
||||
let payload = serde_json::to_vec_pretty(&sorted)
|
||||
.map_err(|error| format!("failed to serialize teams store: {error}"))?;
|
||||
fs::write(&path, payload).map_err(|error| format!("failed to write teams store: {error}"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::sort_teams;
|
||||
use crate::managed_agents::TeamRecord;
|
||||
|
||||
fn team(id: &str, name: &str) -> TeamRecord {
|
||||
TeamRecord {
|
||||
id: id.to_string(),
|
||||
name: name.to_string(),
|
||||
description: None,
|
||||
persona_ids: Vec::new(),
|
||||
created_at: "2026-03-20T00:00:00Z".to_string(),
|
||||
updated_at: "2026-03-20T00:00:00Z".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sort_teams_alphabetical_case_insensitive() {
|
||||
let mut teams = vec![
|
||||
team("3", "Zulu"),
|
||||
team("1", "alpha"),
|
||||
team("2", "Bravo"),
|
||||
];
|
||||
sort_teams(&mut teams);
|
||||
|
||||
let names: Vec<&str> = teams.iter().map(|t| t.name.as_str()).collect();
|
||||
assert_eq!(names, vec!["alpha", "Bravo", "Zulu"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sort_teams_breaks_ties_by_id() {
|
||||
let mut teams = vec![
|
||||
team("b", "same"),
|
||||
team("a", "same"),
|
||||
];
|
||||
sort_teams(&mut teams);
|
||||
|
||||
let ids: Vec<&str> = teams.iter().map(|t| t.id.as_str()).collect();
|
||||
assert_eq!(ids, vec!["a", "b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sort_teams_empty_is_noop() {
|
||||
let mut teams: Vec<TeamRecord> = Vec::new();
|
||||
sort_teams(&mut teams);
|
||||
assert!(teams.is_empty());
|
||||
}
|
||||
}
|
||||
@@ -238,6 +238,35 @@ pub struct AgentModelInfo {
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TeamRecord {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub persona_ids: Vec<String>,
|
||||
pub created_at: String,
|
||||
pub updated_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CreateTeamRequest {
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
#[serde(default)]
|
||||
pub persona_ids: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct UpdateTeamRequest {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
#[serde(default)]
|
||||
pub persona_ids: Vec<String>,
|
||||
}
|
||||
|
||||
pub const DEFAULT_ACP_COMMAND: &str = "sprout-acp";
|
||||
pub const DEFAULT_AGENT_COMMAND: &str = "goose";
|
||||
pub const DEFAULT_MCP_COMMAND: &str = "sprout-mcp-server";
|
||||
|
||||
Reference in New Issue
Block a user