Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.
Rust ai-agentsai-assistantai-botautomationbotbots-teamchatbotclaude-duetgrokgrok-botgrok-bot-alternativeharnessllmmulti-agentopenai-dotsopenbotpersonal-agentrustself-hostedselfhosted-ai
Team channels: a lead's @mention hands work on as a delegation
Lin (kwant bot)dc9b0a6dcd01
diff --git a/src/server/routes.rs b/src/server/routes.rsindex 316a3a49..bf29ffc8 100644--- a/src/server/routes.rs+++ b/src/server/routes.rs@@ -819,6 +819,11 @@ pub(super) async fn run_routine_turn(/// Lets teammates that kwants @mentioned in `replies` answer in turn, so kwants can delegate to each other./// `already_spoken` kwants are not handed the turn again.async fn run_handoffs(scope: &TurnScope, already_spoken: &[String], replies: &[(Kwant, String)], user_prompt: &str) {+ if let [(from, text)] = replies {+ if is_team_channel_lead(scope, from).await {+ return delegate_mentions(scope, from, text, user_prompt).await;+ }+ }let mut spoken: Vec<String> = already_spoken.to_vec();let mut queue: std::collections::VecDeque<(Kwant, String, String)> = std::collections::VecDeque::new();let enqueue = |queue: &mut std::collections::VecDeque<(Kwant, String, String)>,@@ -885,6 +890,74 @@ async fn run_handoffs(scope: &TurnScope, already_spoken: &[String], replies: &[(let _ = events.send(RuntimeEvent::DiscussionCompleted { total_turns: done + 1, context_id: Some(context_id.clone()) });}+/// Whether `kwant` leads the team channel this turn is in (a channel turn, not a thread).+async fn is_team_channel_lead(scope: &TurnScope, kwant: &Kwant) -> bool {+ let (Some(cid), None, Some(_)) = (&scope.channel_id, &scope.thread_id, &scope.team_keys) else {+ return false;+ };+ match scope.state.db.get_channel(cid).await {+ Ok(Some(channel)) => channel.name.starts_with("team-") && channel.lead_kwant_id == kwant.id,+ _ => false,+ }+}++/// A team lead's @mentions in its team channel hand work on as delegations, not handoff turns:+/// each teammate gets the task in its own thread (Sent-to chip, queue, the same guards as the+/// delegate tool), and when the last one ends the lead is resumed once with every result to give+/// the user one answer. A refused handoff is said in the channel. Teammates the lead already+/// delegated to from this channel (with the tool) aren't sent it twice.+async fn delegate_mentions(scope: &TurnScope, lead: &Kwant, text: &str, user_prompt: &str) {+ let Some(cid) = scope.channel_id.clone() else { return };+ let names: Vec<&str> = scope.teammates.iter().map(|k| k.name.as_str()).collect();+ let open: Vec<String> = scope+ .state+ .db+ .list_delegations(Some(&lead.id), None, 50)+ .await+ .unwrap_or_default()+ .into_iter()+ .filter(|d| d.is_open() && d.origin_channel_id.as_deref() == Some(cid.as_str()))+ .map(|d| d.specialist_kwant_id)+ .collect();+ let mut targets: Vec<&Kwant> = Vec::new();+ for i in crate::router::handoff_targets(text, &names) {+ let k = &scope.teammates[i];+ if k.id != lead.id && !open.contains(&k.id) && !targets.iter().any(|t| t.id == k.id) {+ targets.push(k);+ }+ }+ let user = truncate_chars(user_prompt.trim(), 3000);+ let task = format!(+ "The user asked in the team channel:\n{user}\n\n{lead} wrote, mentioning you:\n{text}",+ lead = lead.name,+ text = truncate_chars(text.trim(), 4000),+ );+ let title = truncate_chars(user.lines().find(|l| !l.trim().is_empty()).unwrap_or("Task").trim(), 80).to_string();+ let allowed: Vec<String> = scope.teammates.iter().map(|k| k.id.clone()).collect();+ for specialist in targets.into_iter().take(MAX_HANDOFFS) {+ let req = super::delegation::StartRequest {+ lead: lead.clone(),+ specialist: specialist.clone(),+ task: task.clone(),+ title: Some(title.clone()),+ origin: super::delegation::Origin { channel_id: Some(cid.clone()), ..Default::default() },+ continue_lead: true,+ origin_depth: scope.delegation_depth,+ allowed: Some(allowed.clone()),+ };+ if let Err((_, why)) = super::delegation::start(&scope.state, req).await {+ tracing::info!(lead = %lead.id, specialist = %specialist.id, "mention handoff refused: {why}");+ let note = scope.message("system", "kwnt", format!("Couldn't hand this to {}: {why}", specialist.name));+ if let Some(saved) = scope.save(¬e, Some(serde_json::json!({"kind": "handoff_refused"}))).await {+ let _ = scope+ .state+ .event_sender+ .send(RuntimeEvent::MessageAdded { message: serde_json::to_value(&saved).unwrap_or_default() });+ }+ }+ }+}+/// Runs a moderated team discussion: the lead opens (or pauses to ask the user), teammates react to/// each other briefly or pass, @mentioned teammates join in, and the lead closes with one summary.async fn run_discussion(scope: &TurnScope, team: &Team, planned: Vec<Kwant>, user_prompt: &str) {diff --git a/tests/delegation_test.rs b/tests/delegation_test.rsindex f0660a15..56a37071 100644--- a/tests/delegation_test.rs+++ b/tests/delegation_test.rs@@ -22,8 +22,8 @@ use std::sync::OnceLock;use std::time::Duration;use tower::ServiceExt;-/// Replies "answer <tag>" for a prompt with "REPLYTAG:<tag>"; a "LONGTASK:<file>" prompt waits-/// (up to 60 s) for that file first. Every prompt is saved under `$KWNT_TEST_PROMPTS`.+/// Replies "answer <tag>" for a prompt with "REPLYTAG:<tag>", followed by "@<name>" for one with+/// "MENTION:<name>"; a "LONGTASK:<file>" prompt waits (up to 60 s) for that file first. Every prompt is saved under `$KWNT_TEST_PROMPTS`.const FAKE_CLAUDE: &str = r#"#!/bin/shcase "$1" inauth) echo '{"loggedIn":true}'; exit 0 ;;@@ -36,7 +36,8 @@ if [ -n "$release" ]; thenwhile [ ! -f "$release" ] && [ $i -lt 600 ]; do sleep 0.1; i=$((i+1)); donefitag=$(printf '%s\n' "$last" | sed -n 's/.*REPLYTAG:\([A-Za-z0-9_]*\).*/\1/p' | tail -n 1)-echo "{\"type\":\"result\",\"is_error\":false,\"num_turns\":1,\"result\":\"answer ${tag:-none}\"}"+mention=$(printf '%s\n' "$last" | sed -n 's/.*MENTION:\([A-Za-z0-9_]*\).*/\1/p' | head -n 1)+echo "{\"type\":\"result\",\"is_error\":false,\"num_turns\":1,\"result\":\"answer ${tag:-none}${mention:+ @$mention}\"}""#;fn prompts_dir() -> &'static PathBuf {@@ -773,3 +774,120 @@ async fn run_fan_in(env: &Env, third: String) {assert!(p.contains("Check each result"), "{p}");assert!(prompts_with("finished the task you delegated (“Fan part").is_empty(), "no per-part turns");}++async fn name(env: &Env, id: &str) -> String {+ let (_, kwants) = call(&env.app, "GET", "/api/kwants", None).await;+ kwants.as_array().unwrap().iter().find(|k| k["id"] == id).unwrap()["name"].as_str().unwrap().to_string()+}++/// A team led by `env.lead` with `env.specialist` in it; returns its channel's id.+async fn team_channel(env: &Env, team: &str) -> String {+ let (s, v) = call(+ &env.app,+ "POST",+ "/api/teams",+ Some(json!({"name": team, "lead_kwant_id": env.lead, "member_kwants": [env.lead, env.specialist]})),+ )+ .await;+ assert_eq!(s, StatusCode::OK, "{v}");+ let (_, channels) = call(&env.app, "GET", "/api/channels", None).await;+ channels+ .as_array()+ .unwrap()+ .iter()+ .find(|c| c["name"] == format!("team-{team}").as_str())+ .unwrap_or_else(|| panic!("no team-{team} channel: {channels}"))["id"]+ .as_str()+ .unwrap()+ .to_string()+}++async fn say_in(env: &Env, channel: &str, prompt: &str) {+ let (s, v) = call(&env.app, "POST", "/api/chat/send", Some(json!({"channel_id": channel, "prompt": prompt, "sender_id": "user"}))).await;+ assert_eq!(s, StatusCode::OK, "{v}");+}++async fn channel_messages(env: &Env, channel: &str) -> Vec<kwnt::db::models::Message> {+ env.state.db.list_messages(channel, 100).await.unwrap()+}++/// Gap 1: a team lead that hands work on with an @mention in its team channel delegates it. The+/// teammate works in its own thread (the delegation_started chip in the channel), its result is+/// posted back, and the lead is resumed once with it to give the user one final answer; the+/// teammate never takes a handoff turn in the channel itself.+#[tokio::test]+async fn a_leads_mention_in_its_team_channel_is_a_delegation() {+ let env = setup().await;+ let (lead, specialist) = (name(&env, &env.lead).await, name(&env, &env.specialist).await);+ let channel = team_channel(&env, "mention-squad").await;+ say_in(&env, &channel, &format!("@{lead} Ship it LEADMARK1\nREPLYTAG:shipped MENTION:{specialist}")).await;++ wait_for("the delegation from the mention", || async {+ env.state.db.list_delegations(Some(&env.lead), None, 10).await.unwrap().iter().any(|d| d.origin_channel_id.as_deref() == Some(channel.as_str()))+ })+ .await;+ let d = env.state.db.list_delegations(Some(&env.lead), None, 10).await.unwrap().into_iter().find(|d| d.origin_channel_id.as_deref() == Some(channel.as_str())).unwrap();+ assert_eq!(d.specialist_kwant_id, env.specialist);+ assert!(d.continue_lead);+ assert!(d.task.contains("LEADMARK1") && d.task.contains(&format!("@{specialist}")), "the task carries the request and the lead's words: {}", d.task);++ wait_for("the delegation to complete", || async { delegation_status(&env, &d.id).await == "completed" }).await;+ let lead_replies = || async {+ channel_messages(&env, &channel).await.into_iter().filter(|m| m.sender_role == "kwant" && m.sender_id == lead).count()+ };+ wait_for("the lead's final answer", || async { lead_replies().await == 2 }).await;+ // Settle: nothing else arrives+ tokio::time::sleep(Duration::from_millis(1500)).await;+ let msgs = channel_messages(&env, &channel).await;+ let kinds: Vec<Option<String>> = msgs.iter().map(meta_kind).collect();+ assert_eq!(kinds.iter().filter(|k| k.as_deref() == Some("delegation_started")).count(), 1, "one Sent-to chip");+ assert_eq!(kinds.iter().filter(|k| k.as_deref() == Some("delegation_result")).count(), 1, "one result");+ assert_eq!(lead_replies().await, 2, "the lead's handoff reply and one final answer");+ assert!(+ msgs.iter().filter(|m| m.sender_id == specialist).all(|m| meta_kind(m).as_deref() == Some("delegation_result")),+ "the teammate speaks in the channel only through its result"+ );+ let resumes = prompts_with("LEADMARK1").into_iter().filter(|p| p.contains("finished the task you delegated")).count();+ assert_eq!(resumes, 1, "the lead is resumed exactly once");+ assert!(prompts_with("LEADMARK1").iter().all(|p| !p.contains("handed this conversation to you")), "no direct handoff turn");+}++/// A teammate who isn't the lead still hands the turn on directly, as before; and a lead's+/// mention that the delegation guards refuse is said in the channel instead of running.+#[tokio::test]+async fn non_lead_mentions_hand_off_and_refused_mentions_are_reported() {+ let env = setup().await;+ let (lead, specialist) = (name(&env, &env.lead).await, name(&env, &env.specialist).await);+ let channel = team_channel(&env, "plain-squad").await;+ say_in(&env, &channel, &format!("@{specialist} Look at this PLAINMARK REPLYTAG:plain MENTION:{lead}")).await;+ wait_for("the lead's handoff turn", || async {+ prompts_with("PLAINMARK").iter().any(|p| p.contains("handed this conversation to you"))+ && channel_messages(&env, &channel).await.iter().any(|m| m.sender_id == lead)+ })+ .await;+ assert!(+ env.state.db.list_delegations(Some(&env.specialist), None, 10).await.unwrap().is_empty(),+ "a teammate's mention isn't a delegation"+ );++ // The lead already has the most unfinished delegations it may have: the mention is refused+ let release = env.release_file("refused-release");+ let mut held = Vec::new();+ for _ in 0..delegation::MAX_OPEN_PER_LEAD {+ held.push(delegate(&env, &format!("Hold LONGTASK:{} REPLYTAG:held", release.display())).await["id"].as_str().unwrap().to_string());+ }+ // Its own channel: the history above would carry the earlier MENTION into the lead's prompt+ let channel = team_channel(&env, "refused-squad").await;+ say_in(&env, &channel, &format!("@{lead} Again REFUSEDMARK REPLYTAG:again MENTION:{specialist}")).await;+ wait_for("the refusal note", || async {+ channel_messages(&env, &channel).await.iter().any(|m| meta_kind(m).as_deref() == Some("handoff_refused"))+ })+ .await;+ let note = channel_messages(&env, &channel).await.into_iter().find(|m| meta_kind(m).as_deref() == Some("handoff_refused")).unwrap();+ assert!(note.text.contains(&specialist) && note.text.contains("unfinished delegations"), "{}", note.text);+ assert!(prompts_with("REFUSEDMARK").iter().all(|p| !p.contains("handed this conversation to you")), "refused, not handed off directly");+ std::fs::write(&release, "").unwrap();+ for id in held {+ wait_for("a held task to finish", || async { delegation_status(&env, &id).await == "completed" }).await;+ }+}