Skip to content

armgit / kwant

Public Imported from GitHub
0

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.

Hand-back: the only other bot takes it only when it fits

Lin (kwant bot)26798104d984
diff --git a/src/router/team.rs b/src/router/team.rs
index da6b6f2c..3a74cda6 100644
--- a/src/router/team.rs
+++ b/src/router/team.rs
@@ -726,14 +726,27 @@ pub fn pick_member_hinted<'a>(
last_speaker: Option<&str>,
hints: &Hints,
) -> Option<Pick<'a>> {
- let only = match members {
- [] => return None,
- [one] => Some(one),
- _ => None,
- };
- if let Some(k) = only {
+ if let [k] = members {
return Some(Pick { kwant: k, basis: "only", reason: format!("{} is the only bot here that can answer", k.name), terms: vec![], fit: 1.0, choices: vec![] });
}
+ pick_by_words(prompt, lead_kwant_id, members, laya, lead_word, last_speaker, hints)
+}
+
+/// [`pick_member_hinted`] without its shortcut for a lone bot: how well the message fits even the
+/// only one, as when a handed-back message has one other bot to go to.
+#[allow(clippy::too_many_arguments)]
+pub fn pick_by_words<'a>(
+ prompt: &str,
+ lead_kwant_id: &str,
+ members: &'a [Kwant],
+ laya: Option<LayaGuess<'a>>,
+ lead_word: &str,
+ last_speaker: Option<&str>,
+ hints: &Hints,
+) -> Option<Pick<'a>> {
+ if members.is_empty() {
+ return None;
+ }
let words = tokens(prompt);
let lead = lead_of(lead_kwant_id, members);
let to_lead = |matched: Vec<&'a Kwant>, terms: Vec<String>| {
@@ -1844,6 +1857,21 @@ mod fit_tests {
assert_eq!(p.kwant.id, "k-lead", "{p:?}");
}
+ /// A lone bot gets every message, but [`pick_by_words`] still says whether the message is its
+ /// work: a handed-back message goes on to the only other bot just then.
+ #[test]
+ fn a_lone_bot_is_still_scored_on_the_words() {
+ let script = [bot("k-script", "Scriptwriter", "TikTok Scriptwriter", "Scripts and hooks for book videos.")];
+ let baby = "Is it safe for my baby to crawl on hardwood floors?";
+ let p = pick_member_hinted(baby, "k-script", &script, None, "channel lead", None, &Hints::default()).unwrap();
+ assert_eq!((p.basis, p.fit), ("only", 1.0));
+ let p = pick_by_words(baby, "k-script", &script, None, "channel lead", None, &Hints::default()).unwrap();
+ assert!(p.fit < ROUTE_AT, "{p:?}");
+ let p = pick_by_words("Write a TikTok hook about the lantern parade", "k-script", &script, None, "channel lead", None, &Hints::default()).unwrap();
+ assert!(p.fit >= ROUTE_AT, "{p:?}");
+ assert!(pick_by_words(baby, "k-script", &[], None, "channel lead", None, &Hints::default()).is_none());
+ }
+
/// An entry of a bot's avoids list keeps role and word matching off it; naming it still works.
#[test]
fn avoids_keep_a_bot_out_unless_it_is_named() {
diff --git a/src/server/rerouting.rs b/src/server/rerouting.rs
index 84e1036a..f6313df3 100644
--- a/src/server/rerouting.rs
+++ b/src/server/rerouting.rs
@@ -387,7 +387,11 @@ async fn next_after_handback(
if !members.is_empty() {
let hints = hints_for(state, space, members).await;
let lead = if channel.lead_kwant_id == from.id { members[0].id.clone() } else { channel.lead_kwant_id.clone() };
- let route = state.router.route_channel_query_hinted(text, &channel.name, &lead, members, None, &hints).await;
+ let mut route = state.router.route_channel_query_hinted(text, &channel.name, &lead, members, None, &hints).await;
+ if route.basis == "only" {
+ // The router gives the only bot left everything (fit 1), even what isn't its work
+ route.fit = crate::router::team::pick_by_words(text, &lead, members, None, "channel lead", None, &hints).map_or(0.0, |p| p.fit);
+ }
if route.fit >= ROUTE_AT {
if let Some(k) = members.iter().find(|k| k.id == route.kwant_id) {
return Some((k.clone(), route.fit));
diff --git a/tests/concierge_test.rs b/tests/concierge_test.rs
index 72795aaf..f02805f8 100644
--- a/tests/concierge_test.rs
+++ b/tests/concierge_test.rs
@@ -396,6 +396,32 @@ async fn a_hand_back_passes_on_the_message_it_answers_not_the_newest() {
assert!(err && text.contains("already handed back"), "{text}");
}
+/// The only other bot here takes a handed-back message only when it fits it; else the Concierge
+/// does. Live on 0.6.4, Atlas handed back a baby question it got for "crawl", and the
+/// Scriptwriter took it and answered it as not its area either.
+#[tokio::test]
+async fn a_hand_back_skips_the_only_other_bot_when_it_doesnt_fit() {
+ let (app, state, _tmp) = setup().await;
+ let script = ok(&app, "POST", "/api/kwants", Some(specialist("Scriptwriter", "TikTok Scriptwriter", "Scripts and hooks for book videos."))).await["id"].as_str().unwrap().to_string();
+ let atlas = ok(&app, "POST", "/api/kwants", Some(specialist("Atlas", "Technical SEO", "Crawl errors and sitemaps."))).await["id"].as_str().unwrap().to_string();
+ only(&app, &state, &[&script, &atlas]).await;
+ let general = state.db.general_channel(&SpaceId::default_space()).await.unwrap().expect("#general").id;
+ let (to, basis) = routed_to(&app, &state, &general, "Is it safe for my baby to crawl on hardwood floors?").await;
+ assert_eq!((to.as_str(), basis.as_deref()), (atlas.as_str(), Some("keywords")));
+ let asked = last_user_message(&state, &general).await;
+ assert!(concierges(&state).await.is_empty());
+
+ let token = answering_token(&state, &atlas, &general, Some(&asked)).await;
+ let mut events = state.event_sender.subscribe();
+ let (text, err) = hand_back(&app, &token, "Baby safety isn't technical SEO").await;
+ assert!(!err, "{text}");
+ let made = concierges(&state).await;
+ assert_eq!(made.len(), 1, "the Concierge, made now: {text}");
+ let routing = next_reply_routing(&state, &mut events, &general).await;
+ assert_eq!((routing["basis"].as_str(), routing["kwant_id"].as_str()), (Some("handback"), Some(made[0].as_str())), "{routing}");
+ assert_eq!(routing["message_id"], asked.as_str());
+}
+
/// A turn the router picked from the message's words is told the person didn't choose the bot and
/// that it may hand the message back. An @mention, the Concierge and the hand-back turn aren't (QA
/// on 0.6.4: without being told, the live model handed back 0 of 3 off-topic messages).