Teams (deploy ladder rung 1): schema + provisioning + API
A team = a baseline topology staffed with real claws. Migration 0010 (teams + team_members node→claw bindings) + cm-db repo/teams.rs. runtime_provision.rs turns a claw into a live runtime agent claw_<id> via the synced gateway config API (#7468): create agent + bind model_provider (mapped from chosen model) + risk_profile=toolfree + clawmates_door bundle — atomic, immediately drivable. routes/teams.rs: POST /api/teams (create claws + provision + build(kind,roles) + bind node.attrs["agent"]=claw_<id> + persist), GET /api/teams[/{id}], POST /api/teams/{id}/run (enqueue a durable run of the team graph — reuses the topology worker + SSE). v1 persona = topology role via the prompt builder; the claw's system_prompt stays its chat identity. Spike confirmed: runtime agent provisioning works; IDENTITY.md persona works for API models (Gemini/Groq), masked by CLI models (Claude/Kimi Code). 16 cm-api tests + provision unit tests pass, clippy clean. Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
3402a3b56d
commit
8123a27bcf
@@ -0,0 +1,234 @@
|
||||
//! Team endpoints — deploy a baseline topology staffed with real claws, then run
|
||||
//! it on the durable topology runner. The first rung of the deploy ladder.
|
||||
|
||||
use axum::extract::{Path, State};
|
||||
use axum::http::StatusCode;
|
||||
use axum::Json;
|
||||
use cm_domain::{AccessPolicy, Agent, AgentId, AgentStatus};
|
||||
use cm_topology::{build, TopologyKind};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use time::format_description::well_known::Rfc3339;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::runtime_provision::{claw_alias, RuntimeProvisioner};
|
||||
use crate::{ApiError, AppState, Authed};
|
||||
|
||||
/// One staffed role in the team (becomes a claw + a topology node).
|
||||
#[derive(Deserialize)]
|
||||
pub struct TeamMemberInput {
|
||||
pub role: String,
|
||||
pub name: String,
|
||||
/// Model selector: claude | glm | glm-5.2 | kimi | gemini | groq.
|
||||
#[serde(default)]
|
||||
pub model: String,
|
||||
#[serde(default)]
|
||||
pub system_prompt: String,
|
||||
#[serde(default)]
|
||||
pub accent: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreateTeamRequest {
|
||||
pub name: String,
|
||||
/// TopologyKind (snake_case), e.g. "hierarchical", "pipeline".
|
||||
pub kind: String,
|
||||
pub members: Vec<TeamMemberInput>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct TeamCreated {
|
||||
pub team_id: String,
|
||||
}
|
||||
|
||||
fn parse_kind(s: &str) -> Result<TopologyKind, ApiError> {
|
||||
serde_json::from_value(Value::String(s.to_string())).map_err(|_| ApiError::BadRequest)
|
||||
}
|
||||
|
||||
/// `POST /api/teams` — create a team: for each member create a claw + provision a
|
||||
/// runtime agent, build the baseline topology, bind node→claw, persist.
|
||||
pub async fn create_team(
|
||||
State(state): State<AppState>,
|
||||
Authed(user): Authed,
|
||||
Json(body): Json<CreateTeamRequest>,
|
||||
) -> Result<(StatusCode, Json<TeamCreated>), ApiError> {
|
||||
if body.members.is_empty() {
|
||||
return Err(ApiError::BadRequest);
|
||||
}
|
||||
let kind = parse_kind(&body.kind)?;
|
||||
let provisioner = RuntimeProvisioner::from_env().ok_or(ApiError::Internal)?;
|
||||
|
||||
// 1. Create each claw (DB row) + provision it as a live runtime agent.
|
||||
let mut claw_ids: Vec<Uuid> = Vec::with_capacity(body.members.len());
|
||||
for m in &body.members {
|
||||
let agent = Agent {
|
||||
id: AgentId::new(),
|
||||
workspace_id: user.workspace_id,
|
||||
name: m.name.clone(),
|
||||
job_title: m.role.clone(),
|
||||
system_prompt: m.system_prompt.clone(),
|
||||
avatar: String::new(),
|
||||
accent: m.accent.clone(),
|
||||
wallpaper: String::new(),
|
||||
managed_by: user.user_id,
|
||||
status: AgentStatus::Online,
|
||||
};
|
||||
cm_db::repo::agents::insert(&state.pool, &agent, &AccessPolicy::default()).await?;
|
||||
let claw_id = agent.id.as_uuid();
|
||||
// Provisioning failure rolls the team back at the runtime layer is best-
|
||||
// effort; the claw row stays (visible in the roster) so nothing is lost.
|
||||
provisioner
|
||||
.provision_claw(claw_id, &m.model)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
eprintln!("teams: provision claw {claw_id} failed: {e}");
|
||||
ApiError::Internal
|
||||
})?;
|
||||
claw_ids.push(claw_id);
|
||||
}
|
||||
|
||||
// 2. Build the baseline topology and bind each node to its claw's runtime alias.
|
||||
let roles: Vec<&str> = body.members.iter().map(|m| m.role.as_str()).collect();
|
||||
let mut graph = build(kind, &roles).map_err(|_| ApiError::BadRequest)?;
|
||||
for (i, node) in graph.nodes.iter_mut().enumerate() {
|
||||
if let Some(cid) = claw_ids.get(i) {
|
||||
node.attrs.insert("agent".into(), claw_alias(*cid));
|
||||
node.attrs.insert("claw_id".into(), cid.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Persist team + node→claw bindings.
|
||||
let team_id = Uuid::now_v7();
|
||||
let graph_json = serde_json::to_value(&graph).map_err(|_| ApiError::Internal)?;
|
||||
cm_db::repo::teams::insert_team(
|
||||
&state.pool,
|
||||
team_id,
|
||||
user.workspace_id,
|
||||
&body.name,
|
||||
kind.as_str(),
|
||||
&graph_json,
|
||||
)
|
||||
.await?;
|
||||
for (i, node) in graph.nodes.iter().enumerate() {
|
||||
if let Some(cid) = claw_ids.get(i) {
|
||||
cm_db::repo::teams::add_member(&state.pool, team_id, &node.id, *cid, &node.role).await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok((
|
||||
StatusCode::CREATED,
|
||||
Json(TeamCreated {
|
||||
team_id: team_id.to_string(),
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct TeamSummaryOut {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub kind: String,
|
||||
pub status: String,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
/// `GET /api/teams` — recent teams for the workspace.
|
||||
pub async fn list_teams(
|
||||
State(state): State<AppState>,
|
||||
Authed(user): Authed,
|
||||
) -> Result<Json<Vec<TeamSummaryOut>>, ApiError> {
|
||||
let rows = cm_db::repo::teams::list_for_workspace(&state.pool, user.workspace_id, 50).await?;
|
||||
Ok(Json(
|
||||
rows.into_iter()
|
||||
.map(|t| TeamSummaryOut {
|
||||
id: t.id.to_string(),
|
||||
name: t.name,
|
||||
kind: t.kind,
|
||||
status: t.status,
|
||||
created_at: t.created_at.format(&Rfc3339).unwrap_or_default(),
|
||||
})
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct TeamMemberOut {
|
||||
pub node_id: String,
|
||||
pub claw_id: String,
|
||||
pub role: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct TeamDetail {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub kind: String,
|
||||
pub status: String,
|
||||
pub created_at: String,
|
||||
pub graph: Value,
|
||||
pub members: Vec<TeamMemberOut>,
|
||||
}
|
||||
|
||||
/// `GET /api/teams/{id}` — a team's graph + node→claw bindings.
|
||||
pub async fn get_team(
|
||||
State(state): State<AppState>,
|
||||
Authed(user): Authed,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<TeamDetail>, ApiError> {
|
||||
let team = cm_db::repo::teams::get_team(&state.pool, id, user.workspace_id).await?;
|
||||
let members = cm_db::repo::teams::members_for_team(&state.pool, id).await?;
|
||||
Ok(Json(TeamDetail {
|
||||
id: team.id.to_string(),
|
||||
name: team.name,
|
||||
kind: team.kind,
|
||||
status: team.status,
|
||||
created_at: team.created_at.format(&Rfc3339).unwrap_or_default(),
|
||||
graph: team.graph,
|
||||
members: members
|
||||
.into_iter()
|
||||
.map(|m| TeamMemberOut {
|
||||
node_id: m.node_id,
|
||||
claw_id: m.claw_id.to_string(),
|
||||
role: m.role,
|
||||
})
|
||||
.collect(),
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct RunTeamRequest {
|
||||
pub task: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct RunAccepted {
|
||||
pub run_id: String,
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
/// `POST /api/teams/{id}/run` — enqueue a durable run of the team's topology
|
||||
/// (drives the bound claws). Poll/stream via `/api/topology-runs/{run_id}`.
|
||||
pub async fn run_team(
|
||||
State(state): State<AppState>,
|
||||
Authed(user): Authed,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(body): Json<RunTeamRequest>,
|
||||
) -> Result<(StatusCode, Json<RunAccepted>), ApiError> {
|
||||
let team = cm_db::repo::teams::get_team(&state.pool, id, user.workspace_id).await?;
|
||||
let run_id = Uuid::now_v7();
|
||||
cm_db::repo::topology_runs::enqueue_run(
|
||||
&state.pool,
|
||||
run_id,
|
||||
user.workspace_id,
|
||||
&body.task,
|
||||
&team.graph,
|
||||
)
|
||||
.await?;
|
||||
Ok((
|
||||
StatusCode::ACCEPTED,
|
||||
Json(RunAccepted {
|
||||
run_id: run_id.to_string(),
|
||||
status: "queued".into(),
|
||||
}),
|
||||
))
|
||||
}
|
||||
Reference in New Issue
Block a user