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.

Keep an API conversation's cached prompt while its notes change (item 5)

Lin (kwant bot)332ea4c64471
diff --git a/src/db/api_context.rs b/src/db/api_context.rs
new file mode 100644
index 00000000..7de9b861
--- /dev/null
+++ b/src/db/api_context.rs
@@ -0,0 +1,109 @@
+//! What an API-key bot's conversation was last sent as its cached prompt prefix, so the next turn
+//! can send the same bytes again (see `crate::drivers::api`).
+
+use super::{Database, DbError};
+use chrono::{DateTime, Utc};
+use sqlx::FromRow;
+
+/// One bot's prefix in one conversation (`context_id` is a channel, direct chat or thread).
+#[derive(Debug, Clone, PartialEq, Eq, FromRow)]
+pub struct ApiContextRow {
+ pub kwant_id: String,
+ pub context_id: String,
+ /// The bot's persona, identity and soul when the prefix was written.
+ pub profile: String,
+ /// The team block, teammates and browser rules.
+ pub team: String,
+ /// Pinned instructions and memory.
+ pub notes: String,
+ /// When a turn last sent this prefix; the provider's cache lapses some minutes after it.
+ pub used_at: DateTime<Utc>,
+}
+
+impl Database {
+ pub(super) async fn migrate_api_context(&self) -> Result<(), DbError> {
+ sqlx::query(
+ "CREATE TABLE IF NOT EXISTS api_contexts (
+ kwant_id TEXT NOT NULL,
+ context_id TEXT NOT NULL,
+ profile TEXT NOT NULL,
+ team TEXT NOT NULL,
+ notes TEXT NOT NULL,
+ used_at TIMESTAMP NOT NULL,
+ PRIMARY KEY (kwant_id, context_id)
+ )",
+ )
+ .execute(&self.pool)
+ .await?;
+ Ok(())
+ }
+
+ pub async fn api_context(&self, kwant_id: &str, context_id: &str) -> Result<Option<ApiContextRow>, DbError> {
+ Ok(sqlx::query_as(
+ "SELECT kwant_id, context_id, profile, team, notes, used_at FROM api_contexts WHERE kwant_id = ? AND context_id = ?",
+ )
+ .bind(kwant_id)
+ .bind(context_id)
+ .fetch_optional(&self.pool)
+ .await?)
+ }
+
+ /// Writes a new prefix, replacing the old one.
+ pub async fn save_api_context(&self, row: &ApiContextRow) -> Result<(), DbError> {
+ sqlx::query(
+ "INSERT INTO api_contexts (kwant_id, context_id, profile, team, notes, used_at) VALUES (?, ?, ?, ?, ?, ?)
+ ON CONFLICT (kwant_id, context_id) DO UPDATE SET
+ profile = excluded.profile, team = excluded.team, notes = excluded.notes, used_at = excluded.used_at",
+ )
+ .bind(&row.kwant_id)
+ .bind(&row.context_id)
+ .bind(&row.profile)
+ .bind(&row.team)
+ .bind(&row.notes)
+ .bind(row.used_at)
+ .execute(&self.pool)
+ .await?;
+ Ok(())
+ }
+
+ /// Records that a turn sent the saved prefix again.
+ pub async fn touch_api_context(&self, kwant_id: &str, context_id: &str, at: DateTime<Utc>) -> Result<(), DbError> {
+ sqlx::query("UPDATE api_contexts SET used_at = ? WHERE kwant_id = ? AND context_id = ?")
+ .bind(at)
+ .bind(kwant_id)
+ .bind(context_id)
+ .execute(&self.pool)
+ .await?;
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[tokio::test]
+ async fn a_saved_prefix_is_replaced_and_touched() {
+ let tmp = tempfile::tempdir().unwrap();
+ let db = Database::init(tmp.path()).await.unwrap();
+ assert_eq!(db.api_context("k", "c").await.unwrap(), None);
+ let t0 = Utc::now() - chrono::Duration::minutes(10);
+ let mut row = ApiContextRow {
+ kwant_id: "k".into(),
+ context_id: "c".into(),
+ profile: "p".into(),
+ team: "t".into(),
+ notes: "n".into(),
+ used_at: t0,
+ };
+ db.save_api_context(&row).await.unwrap();
+ row.notes = "n2".into();
+ db.save_api_context(&row).await.unwrap();
+ let t1 = Utc::now();
+ db.touch_api_context("k", "c", t1).await.unwrap();
+ let got = db.api_context("k", "c").await.unwrap().unwrap();
+ assert_eq!(got.notes, "n2");
+ assert_eq!(got.used_at.timestamp_millis(), t1.timestamp_millis());
+ assert_eq!(db.api_context("k", "other").await.unwrap(), None);
+ }
+}
diff --git a/src/db/bot_actions.rs b/src/db/bot_actions.rs
index f9768836..7a4470ea 100644
--- a/src/db/bot_actions.rs
+++ b/src/db/bot_actions.rs
@@ -323,7 +323,7 @@ impl Database {
}
/// Deletes a bot the person chose to remove, with its direct chat, threads, memories,
- /// skills, routines, questions, approvals, suggestions, proposals and memberships, in one
+ /// skills, routines, questions, approvals, suggestions, proposals, cached API prompts and memberships, in one
/// transaction. Channels and teams it led get another lead (`new_lead`, which must be in
/// the same space). What it wrote in shared channels stays, as the room's history; so does
/// the space's log of bot actions.
@@ -347,6 +347,7 @@ impl Database {
"DELETE FROM kwant_memories WHERE kwant_id = ?1",
"DELETE FROM kwant_mcp_disabled WHERE kwant_id = ?1",
"DELETE FROM kwant_mcp_servers WHERE kwant_id = ?1",
+ "DELETE FROM api_contexts WHERE kwant_id = ?1",
] {
sqlx::query(sql).bind(id).execute(&mut *tx).await?;
}
diff --git a/src/db/mod.rs b/src/db/mod.rs
index 016a10be..c3368336 100644
--- a/src/db/mod.rs
+++ b/src/db/mod.rs
@@ -17,6 +17,7 @@ pub mod bot_actions;
pub mod hires;
pub mod imports;
pub mod approval_modes;
+pub mod api_context;
/// What a space's #general says about itself.
pub const GENERAL_DESCRIPTION: &str = "Everyone in this space. Laya picks who answers.";
@@ -140,6 +141,7 @@ impl Database {
db.migrate_zombie().await?;
db.migrate_parallel().await?;
db.migrate_documents().await?;
+ db.migrate_api_context().await?;
if spaces {
let migration = db.migrate_spaces(data_dir, fresh).await?;
db.require_spaces_schema(&migration).await?;
diff --git a/src/drivers/api.rs b/src/drivers/api.rs
index f14745b0..1e96af4f 100644
--- a/src/drivers/api.rs
+++ b/src/drivers/api.rs
@@ -1,5 +1,6 @@
use super::{events::RuntimeEvent, Driver, DriverError, TurnInput, TurnOutput};
use async_trait::async_trait;
+use chrono::{DateTime, Utc};
use reqwest::Client;
use crate::mcp::session::{Caller, McpToolSession};
use serde_json::{json, Value};
@@ -127,6 +128,87 @@ fn volatile(role: &str, content: &str) -> Value {
m
}
+/// How long the Anthropic API keeps a cached prefix after its last use (`ephemeral`'s default).
+const CACHE_TTL_SECS: i64 = 5 * 60;
+
+/// The cached part of an Anthropic system prompt, split by what changes it.
+#[derive(Debug, Clone, PartialEq, Eq)]
+struct Prefix {
+ /// Persona, identity and soul: changes when the bot's profile is edited.
+ profile: String,
+ /// Team block, teammates and browser rules: changes with the roster.
+ team: String,
+ /// Pinned instructions and memory: changes when a note is added or edited.
+ notes: String,
+}
+
+/// The prefix to send this turn, a note of what changed since it was written, and whether it is
+/// new. While the provider's cache is warm a conversation keeps the prefix it was first sent, so a
+/// memory, roster or profile edit doesn't rewrite every cached token; the edit rides along after
+/// the breakpoints instead. With no saved prefix, or a cache that has lapsed, `now` is the prefix.
+fn keep_prefix(saved: Option<(&Prefix, DateTime<Utc>)>, now: &Prefix, at: DateTime<Utc>) -> (Prefix, Option<String>, bool) {
+ match saved {
+ Some((old, used_at)) if (at - used_at).num_seconds() < CACHE_TTL_SECS => (old.clone(), changes_note(old, now), false),
+ _ => (now.clone(), None, true),
+ }
+}
+
+/// What changed between the cached prefix and today's, for the end of the request.
+fn changes_note(old: &Prefix, now: &Prefix) -> Option<String> {
+ let mut parts = Vec::new();
+ if old.profile != now.profile {
+ parts.push(format!("(Your profile now reads:){}", now.profile));
+ }
+ if old.team != now.team {
+ parts.push(format!("(Your team and teammates now:){}", now.team));
+ }
+ if old.notes != now.notes {
+ parts.push(if now.notes.is_empty() {
+ "(Your pinned instructions and notes are now empty.)".to_string()
+ } else {
+ format!("(Your pinned instructions and notes now read:)\n{}", now.notes)
+ });
+ }
+ (!parts.is_empty()).then(|| {
+ format!(
+ "[Updated by kwnt since earlier in this chat]\nkwnt, not the person, sends this: parts of your instructions changed during \
+ this conversation. Where these differ from the ones at the start, follow these.\n\n{}",
+ parts.join("\n\n")
+ )
+ })
+}
+
+/// [`keep_prefix`] for this turn's conversation, read from and saved to the turn's store.
+async fn conversation_prefix(input: &TurnInput, now: Prefix) -> (Prefix, Option<String>) {
+ let Some(db) = input.store.as_ref().filter(|_| !input.thread_id.is_empty()) else {
+ return (now, None);
+ };
+ let (kwant_id, context_id) = (&input.kwant.id, &input.thread_id);
+ let at = Utc::now();
+ let saved = db.api_context(kwant_id, context_id).await.ok().flatten().map(|r| {
+ let used_at = r.used_at;
+ (Prefix { profile: r.profile, team: r.team, notes: r.notes }, used_at)
+ });
+ let (prefix, changes, fresh) = keep_prefix(saved.as_ref().map(|(p, t)| (p, *t)), &now, at);
+ let stored = if fresh {
+ db.save_api_context(&crate::db::api_context::ApiContextRow {
+ kwant_id: kwant_id.clone(),
+ context_id: context_id.clone(),
+ profile: prefix.profile.clone(),
+ team: prefix.team.clone(),
+ notes: prefix.notes.clone(),
+ used_at: at,
+ })
+ .await
+ } else {
+ db.touch_api_context(kwant_id, context_id, at).await
+ };
+ if let Err(e) = stored {
+ tracing::warn!(error = %e, "could not save the API prompt prefix");
+ }
+ (prefix, changes)
+}
+
/// Turns the OpenAI-style message list into an Anthropic Messages body: system prompts move to
/// the top-level `system`, same-role neighbours share one message and the first message is the
/// user's.
@@ -718,37 +800,53 @@ impl Driver for ApiDriver {
};
let anthropic = is_anthropic(&url);
let parts = input.system_prompt_parts(browser);
- let mut messages = if anthropic {
- // Cached part first; what changes per turn or per minute after the breakpoint
- vec![
- json!({"role": "system", "content": format!("{}{}{}", parts.head, parts.team, parts.tail)}),
- volatile("system", input.continuity.trim()),
- ]
- } else {
- vec![json!({"role": "system", "content": input.format_system_prompt_with(browser)})]
- };
- for msg in &input.history {
- let (role, content) = match msg.sender_role.as_str() {
- "kwant" if msg.sender_id == input.kwant.name => ("assistant", msg.text.clone()),
- // Teammates' replies are context from other speakers, not this kwant's own turns
- "kwant" => ("user", format!("[{}]: {}", msg.sender_id, msg.text)),
- "system" => ("system", msg.text.clone()),
- _ => ("user", msg.text.clone()),
+ let messages = if anthropic {
+ let now = Prefix {
+ profile: parts.head.clone(),
+ team: format!("{}{}", parts.team, parts.tail),
+ notes: input.continuity.trim().to_string(),
};
- messages.push(if anthropic && role == "system" {
- volatile(role, &content)
- } else {
- json!({"role": role, "content": content})
- });
- }
- if anthropic {
+ let (prefix, changes) = conversation_prefix(&input, now).await;
+ // Cached part first; what changes per turn or per minute after the breakpoints
+ let mut messages = vec![
+ json!({"role": "system", "content": format!("{}{}", prefix.profile, prefix.team)}),
+ json!({"role": "system", "content": prefix.notes}),
+ ];
+ for msg in &input.history {
+ let (role, content) = match msg.sender_role.as_str() {
+ "kwant" if msg.sender_id == input.kwant.name => ("assistant", msg.text.clone()),
+ "kwant" => ("user", format!("[{}]: {}", msg.sender_id, msg.text)),
+ // kwnt's own notices stay where they happened, so a new one doesn't
+ // change the cached prefix before it
+ "system" => ("user", format!("[kwnt notice]: {}", msg.text)),
+ _ => ("user", msg.text.clone()),
+ };
+ messages.push(json!({"role": role, "content": content}));
+ }
// The prompt and date go after the breakpoint on the last history message: next
// turn the prompt is history, and the date would otherwise change the prefix
+ if let Some(changes) = changes {
+ messages.push(volatile("user", &changes));
+ }
+ messages.push(volatile("user", input.skills.trim()));
messages.push(volatile("user", &input.prompt));
messages.push(volatile("user", parts.date.trim()));
+ messages
} else {
+ let mut messages = vec![json!({"role": "system", "content": input.format_system_prompt_with(browser)})];
+ for msg in &input.history {
+ let (role, content) = match msg.sender_role.as_str() {
+ "kwant" if msg.sender_id == input.kwant.name => ("assistant", msg.text.clone()),
+ // Teammates' replies are context from other speakers, not this kwant's own turns
+ "kwant" => ("user", format!("[{}]: {}", msg.sender_id, msg.text)),
+ "system" => ("system", msg.text.clone()),
+ _ => ("user", msg.text.clone()),
+ };
+ messages.push(json!({"role": role, "content": content}));
+ }
messages.push(json!({"role": "user", "content": input.prompt}));
- }
+ messages
+ };
let effort = input.effort_level.as_deref().unwrap_or("medium");
let max_tokens = match effort {
@@ -1114,6 +1212,8 @@ mod tests {
fn turn(prompt: &str, history: Vec<crate::db::models::Message>) -> TurnInput {
TurnInput {
continuity: "[Pinned]\nnotes for this turn".into(),
+ skills: String::new(),
+ store: None,
browser_api_url: None,
thread_id: "t1".into(),
kwant: crate::db::models::Kwant {
@@ -1146,7 +1246,8 @@ mod tests {
}
/// The point of caching: turn 2 starts with exactly the blocks turn 1 cached, and nothing that
- /// changes per minute (the date) or per turn (notes, the prompt) sits before a breakpoint.
+ /// changes per minute (the date) or per turn (skills, the prompt) sits before a breakpoint.
+ /// A note edited between the turns keeps the cached prefix and is sent after it.
#[tokio::test]
async fn a_second_turn_reuses_the_first_turns_cached_prefix() {
let reply = json!({"content": [{"type": "text", "text": "Done."}], "usage": {"input_tokens": 5, "output_tokens": 2}});
@@ -1154,9 +1255,14 @@ mod tests {
let driver = ApiDriver::new(Some("sk-ant-test-key".into()), Some(url));
let (tx, mut rx) = mpsc::channel(64);
tokio::spawn(async move { while rx.recv().await.is_some() {} });
+ let tmp = tempfile::tempdir().unwrap();
+ let db = crate::db::Database::init(tmp.path()).await.unwrap();
let first = message("m1", "user", "Oleks", "add caching");
- driver.stream_turn(turn("add caching", vec![first.clone()]), tx.clone()).await.unwrap();
+ let mut one = turn("add caching", vec![first.clone()]);
+ one.store = Some(db.clone());
+ one.skills = "[Skills]\nskill picked for turn 1".into();
+ driver.stream_turn(one, tx.clone()).await.unwrap();
// A teammate's reply lands right after the cached message, in the same (user) role
let history = vec![
first,
@@ -1165,7 +1271,9 @@ mod tests {
message("m4", "user", "Oleks", "and the docs"),
];
let mut second = turn("and the docs", history);
+ second.store = Some(db);
second.continuity = "[Pinned]\ndifferent notes now".into();
+ second.skills = "[Skills]\nanother skill for turn 2".into();
driver.stream_turn(second, tx).await.unwrap();
let bodies: Vec<Value> = seen.lock().unwrap().iter().map(|(_, b)| b.clone()).collect();
@@ -1173,14 +1281,22 @@ mod tests {
for body in &bodies {
assert_eq!(breakpoints(body), 2);
let blocks = rendered(body);
- let date = blocks.iter().position(|b| b.1.contains("[Current date]")).unwrap();
- assert!(date > blocks.iter().rposition(|b| b.2).unwrap(), "the date is after every breakpoint");
+ let last_breakpoint = blocks.iter().rposition(|b| b.2).unwrap();
+ for changing in ["[Current date]", "[Skills]"] {
+ let at = blocks.iter().position(|b| b.1.contains(changing)).unwrap();
+ assert!(at > last_breakpoint, "{changing} is after every breakpoint");
+ }
}
- // The system breakpoint: the same stable block both turns, without the date or the notes
- assert!(one[0].2 && two[0].2);
- assert_eq!(one[0].1, two[0].1);
+ // The system breakpoint: the same blocks both turns, ending with the notes turn 1 had
+ assert_eq!(bodies[0]["system"], bodies[1]["system"]);
assert!(!one[0].1.contains("[Current date]") && !one[0].1.contains("[Pinned]"));
- assert!(one[1].1.contains("[Pinned]") && !one[1].2, "notes come after the system breakpoint");
+ assert_eq!((one[1].1.as_str(), one[1].2), ("[Pinned]\nnotes for this turn", true));
+ // The edit comes after the message breakpoint, in a note saying it is from kwnt
+ let last_breakpoint = two.iter().rposition(|b| b.2).unwrap();
+ let note = two.iter().position(|b| b.1.starts_with("[Updated by kwnt")).unwrap();
+ assert!(note > last_breakpoint);
+ assert!(two[note].1.contains("different notes now") && !two[note].1.contains("(Your profile"));
+ assert!(!one.iter().any(|b| b.1.starts_with("[Updated by kwnt")));
// The message breakpoint of turn 1 ends a prefix that turn 2 repeats unchanged
let msgs = |r: &[(String, String, bool)]| r.iter().filter(|b| b.0 != "system").cloned().collect::<Vec<_>>();
let (msgs_one, msgs_two) = (msgs(&one), msgs(&two));
@@ -1191,7 +1307,56 @@ mod tests {
// Turn 2 marks its own last history block for turn 3, and sends the prompt after it
let end_two = msgs_two.iter().position(|b| b.2).unwrap();
assert_eq!(msgs_two[end_two].1, "and the docs");
- assert_eq!(msgs_two[end_two + 1].1, "and the docs");
+ assert!(msgs_two[end_two + 1..].iter().any(|b| b.1 == "and the docs"));
+ }
+
+ fn prefix(notes: &str) -> Prefix {
+ Prefix { profile: "\n\nYou are Lin.".into(), team: "\n\n[Team] Ada, Pixel".into(), notes: notes.into() }
+ }
+
+ #[test]
+ fn a_warm_prefix_is_kept_and_a_lapsed_one_is_rewritten() {
+ let at = Utc::now();
+ let old = prefix("likes tea");
+ let mut now = prefix("likes coffee");
+ now.team = "\n\n[Team] Ada, Pixel, Rigel".into();
+
+ let (kept, note, fresh) = keep_prefix(Some((&old, at - chrono::Duration::seconds(60))), &now, at);
+ assert_eq!((kept, fresh), (old.clone(), false));
+ let note = note.unwrap();
+ assert!(note.contains("Rigel") && note.contains("likes coffee"));
+ assert!(!note.contains("(Your profile"), "only what changed is repeated");
+
+ let (kept, note, _) = keep_prefix(Some((&old, at - chrono::Duration::seconds(60))), &old, at);
+ assert_eq!((kept, note), (old.clone(), None), "nothing changed, nothing to add");
+
+ let lapsed = at - chrono::Duration::seconds(CACHE_TTL_SECS + 1);
+ assert_eq!(keep_prefix(Some((&old, lapsed)), &now, at), (now.clone(), None, true));
+ assert_eq!(keep_prefix(None, &now, at), (now.clone(), None, true));
+
+ let (_, note, _) = keep_prefix(Some((&old, at)), &prefix(""), at);
+ assert!(note.unwrap().contains("are now empty"));
+ }
+
+ #[tokio::test]
+ async fn kwnt_notices_stay_where_they_happened() {
+ let reply = json!({"content": [{"type": "text", "text": "ok"}], "usage": {"input_tokens": 5, "output_tokens": 2}});
+ let (url, seen) = mock(200, reply).await;
+ let driver = ApiDriver::new(Some("sk-ant-test-key".into()), Some(url));
+ let (tx, mut rx) = mpsc::channel(64);
+ tokio::spawn(async move { while rx.recv().await.is_some() {} });
+ let history = vec![
+ message("m1", "user", "Oleks", "hi"),
+ message("m2", "system", "kwnt", "Ada wasn't asked"),
+ message("m3", "user", "Oleks", "go on"),
+ ];
+ driver.stream_turn(turn("go on", history), tx).await.unwrap();
+ let body = seen.lock().unwrap()[0].1.clone();
+ let blocks = rendered(&body);
+ assert!(!body["system"].to_string().contains("wasn't asked"));
+ let at = blocks.iter().position(|b| b.1 == "[kwnt notice]: Ada wasn't asked").unwrap();
+ assert_eq!(blocks[at - 1].1, "hi");
+ assert_eq!(blocks[at + 1].1, "go on");
}
#[tokio::test]
diff --git a/src/drivers/cli.rs b/src/drivers/cli.rs
index 7a09a1f5..8307dbb1 100644
--- a/src/drivers/cli.rs
+++ b/src/drivers/cli.rs
@@ -1159,6 +1159,8 @@ mod tests {
};
let input = TurnInput {
continuity: String::new(),
+ skills: String::new(),
+ store: None,
browser_api_url: None,
thread_id: "t1".into(),
kwant,
@@ -1230,6 +1232,8 @@ mod tests {
};
let input = TurnInput {
continuity: String::new(),
+ skills: String::new(),
+ store: None,
browser_api_url: None,
thread_id: "t1".into(),
kwant,
diff --git a/src/drivers/mod.rs b/src/drivers/mod.rs
index b943cb8d..71cddbc9 100644
--- a/src/drivers/mod.rs
+++ b/src/drivers/mod.rs
@@ -62,6 +62,14 @@ pub struct TurnInput {
/// (see `crate::server::continuity`). Empty when there are none.
#[serde(default)]
pub continuity: String,
+ /// This turn's "[Skills]" block, picked from the prompt (see `crate::server::skills`), so it
+ /// can change every turn. Prompts show it right after `continuity`.
+ #[serde(default)]
+ pub skills: String,
+ /// Where an API turn keeps per-conversation state between turns (its cached prompt prefix);
+ /// `None` runs without it, as tests and one-off calls do.
+ #[serde(skip)]
+ pub store: Option<crate::db::Database>,
/// MCP servers, approver and records for this turn; `None` when the kwant has none.
#[serde(skip)]
pub mcp: Option<crate::mcp::McpTurnContext>,
@@ -140,12 +148,12 @@ impl TurnInput {
pub fn format_system_prompt_with(&self, browser: BrowserAccess) -> String {
let p = self.system_prompt_parts(browser);
- format!("{}{}{}{}{}", p.head, p.date, p.team, self.continuity, p.tail)
+ format!("{}{}{}{}{}{}", p.head, p.date, p.team, self.continuity, self.skills, p.tail)
}
/// The system prompt split by how often each part changes, for callers that cache it: `head`,
- /// `team` and `tail` stay the same between turns, `date` changes every minute and
- /// `self.continuity` can change every turn. `format_system_prompt_with` joins them in order.
+ /// `team` and `tail` stay the same between turns, `date` changes every minute,
+ /// `self.continuity` when the bot's notes change and `self.skills` can change every turn. `format_system_prompt_with` joins them in order.
pub fn system_prompt_parts(&self, browser: BrowserAccess) -> SystemPromptParts {
let mut base = format!(
"{}\n\nYou are {}, {}.",
@@ -489,6 +497,8 @@ mod tests {
sandbox_id: None,
teammates: vec![],
continuity: String::new(),
+ skills: String::new(),
+ store: None,
mcp: None,
}
}
diff --git a/src/server/routes.rs b/src/server/routes.rs
index 19167168..68e98f32 100644
--- a/src/server/routes.rs
+++ b/src/server/routes.rs
@@ -1611,8 +1611,8 @@ async fn run_kwant_turn_inner(
channel_id.as_deref(),
with_tools,
)
- .await
- + &super::skills::prompt_block(state, &target_kwant.id, &prompt, with_tools).await;
+ .await;
+ let skills = super::skills::prompt_block(state, &target_kwant.id, &prompt, with_tools).await;
let turn_input = TurnInput {
browser_api_url: Some(format!(
"http://127.0.0.1:{}/api/sandbox/{}",
@@ -1636,6 +1636,8 @@ async fn run_kwant_turn_inner(
.collect(),
mcp: mcp_ctx.clone(),
continuity,
+ skills,
+ store: Some(state.db.clone()),
};
let had_state = state.db.list_profile_states().await.unwrap_or_default();
diff --git a/tests/browser_tools_test.rs b/tests/browser_tools_test.rs
index 912b7f4c..3538021f 100644
--- a/tests/browser_tools_test.rs
+++ b/tests/browser_tools_test.rs
@@ -157,6 +157,8 @@ async fn claude_turn_gets_browser_tools_that_drive_its_own_panel() {
sandbox_id: Some(bot.id.clone()),
teammates: vec![],
continuity: String::new(),
+ skills: String::new(),
+ store: None,
mcp: Some(ctx.clone()),
};
let prompt = input.format_system_prompt();
diff --git a/tests/driver_test.rs b/tests/driver_test.rs
index 0e01ae0d..9a1ac2cf 100644
--- a/tests/driver_test.rs
+++ b/tests/driver_test.rs
@@ -83,6 +83,8 @@ async fn test_mock_driver_streaming() {
let input = TurnInput {
continuity: String::new(),
+ skills: String::new(),
+ store: None,
browser_api_url: None,
thread_id: "thread-1".into(),
kwant: dummy_kwant,
@@ -145,6 +147,8 @@ fn test_turn_input_format_system_prompt_with_and_without_team_context() {
// 1. Without team context
let input_solo = TurnInput {
continuity: String::new(),
+ skills: String::new(),
+ store: None,
browser_api_url: None,
thread_id: "thread-1".into(),
kwant: kwant.clone(),
@@ -190,6 +194,8 @@ fn test_turn_input_format_system_prompt_with_and_without_team_context() {
// 2. With team context
let input_team = TurnInput {
continuity: String::new(),
+ skills: String::new(),
+ store: None,
browser_api_url: None,
thread_id: "thread-team".into(),
kwant: kwant.clone(),
@@ -220,6 +226,8 @@ fn test_turn_input_format_system_prompt_with_and_without_team_context() {
// 3. With team context including workspace and repo URL
let input_workspace = TurnInput {
continuity: String::new(),
+ skills: String::new(),
+ store: None,
browser_api_url: None,
thread_id: "thread-workspace".into(),
kwant: kwant.clone(),
diff --git a/tests/grok_driver_test.rs b/tests/grok_driver_test.rs
index 3e854a71..2460cd64 100644
--- a/tests/grok_driver_test.rs
+++ b/tests/grok_driver_test.rs
@@ -33,6 +33,8 @@ const AUTH: &str = r#"{"xai::api_key":{"key":"xai-FAKE","auth_mode":"api_key"}}"
fn turn(mcp: Option<McpTurnContext>) -> TurnInput {
TurnInput {
continuity: String::new(),
+ skills: String::new(),
+ store: None,
browser_api_url: None,
thread_id: "t1".into(),
kwant: Kwant {
diff --git a/tests/mcp_api_loop_test.rs b/tests/mcp_api_loop_test.rs
index 5e800cc5..62886b91 100644
--- a/tests/mcp_api_loop_test.rs
+++ b/tests/mcp_api_loop_test.rs
@@ -79,6 +79,8 @@ fn ctx(approver: Arc<dyn ToolApprover>, timeout_secs: i64, max: usize) -> McpTur
fn input(mcp: Option<McpTurnContext>) -> TurnInput {
TurnInput {
continuity: String::new(),
+ skills: String::new(),
+ store: None,
browser_api_url: None,
thread_id: "t1".into(),
kwant: Kwant {
diff --git a/tests/mcp_runtime_test.rs b/tests/mcp_runtime_test.rs
index 669e41f4..835da173 100644
--- a/tests/mcp_runtime_test.rs
+++ b/tests/mcp_runtime_test.rs
@@ -268,6 +268,8 @@ async fn permission_endpoint_answers_claude_prompts_over_http() {
fn turn(mcp: Option<McpTurnContext>) -> TurnInput {
TurnInput {
continuity: String::new(),
+ skills: String::new(),
+ store: None,
browser_api_url: None,
thread_id: "t1".into(),
kwant: Kwant {