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.

Shared CSV and text files get their preview again (31n) (#927)

Oleksandr Ivanchenko2760ce931f41
diff --git a/src/db/smart_results.rs b/src/db/smart_results.rs
index b34cc3d7..62295120 100644
--- a/src/db/smart_results.rs
+++ b/src/db/smart_results.rs
@@ -401,13 +401,19 @@ mod tests {
.unwrap()
}
- /// What `share_file` does: the card is saved first, the attachment linked after.
- async fn card(&self, sender: &str, direct: &str, attachments: &[&crate::db::attachments::Attachment]) -> Message {
+ /// What `share_file` does: the card is saved first, the attachment linked after. Like a real
+ /// share, the message's sender is the bot's display name and its id is in `meta.kwant_id`.
+ async fn card(&self, kwant: &str, direct: &str, attachments: &[&crate::db::attachments::Attachment]) -> Message {
+ let cards: Vec<Value> = attachments.iter().map(|a| a.card()).collect();
+ self.card_as("Pip", direct, json!({"kind": "file", "attachments": cards, "kwant_id": kwant, "kwant_name": "Pip"})).await
+ }
+
+ /// A file card saved with this sender and exactly this meta.
+ async fn card_as(&self, sender: &str, direct: &str, meta: Value) -> Message {
let mut msg = message("kwant", "Shared a file");
msg.sender_id = sender.into();
msg.direct_kwant_id = Some(direct.into());
- let cards: Vec<Value> = attachments.iter().map(|a| a.card()).collect();
- self.db.insert_message_with(&msg, &with_meta(json!({"kind": "file", "attachments": cards}))).await.unwrap()
+ self.db.insert_message_with(&msg, &with_meta(meta)).await.unwrap()
}
}
@@ -545,6 +551,30 @@ mod tests {
assert_eq!(f.card("k1", "k1", &[&tied]).await.results, None);
}
+ /// 31n: `share_file` saves the card with the bot's display name as sender and the bot's id in
+ /// `meta.kwant_id`; the files block checks that id, never the sender.
+ #[tokio::test]
+ async fn a_share_is_matched_by_the_bot_id_in_its_meta_not_its_sender() {
+ let f = Fixture::new().await;
+ let csv = f.shared("data.csv", "text/csv", b"a,b\n1,2\n", "k1", Some("k1")).await;
+ let cards = json!([csv.card()]);
+ // A real share: display name as sender, the bot's id in the meta
+ let real = f.card_as("Pip", "k1", json!({"kind": "file", "attachments": cards.clone(), "kwant_id": "k1", "kwant_name": "Pip"})).await;
+ let block = &real.results.expect("a real share gets its files block")["blocks"][0];
+ assert_eq!(block["items"][0]["preview"]["kind"], "table");
+ // Another bot's id in the meta
+ let other = f.card_as("Pip", "k1", json!({"kind": "file", "attachments": cards.clone(), "kwant_id": "k2"})).await;
+ assert_eq!(other.results, None, "meta names another bot");
+ // No bot id in the meta, even when the sender is the bot's id
+ let bare = f.card_as("k1", "k1", json!({"kind": "file", "attachments": cards.clone()})).await;
+ assert_eq!(bare.results, None, "no meta kwant_id");
+ let not_text = f.card_as("k1", "k1", json!({"kind": "file", "attachments": cards.clone(), "kwant_id": 1})).await;
+ assert_eq!(not_text.results, None, "a meta kwant_id that isn't text");
+ // A sender naming the bot doesn't stand in for another bot's id in the meta
+ let spoof = f.card_as("k1", "k1", json!({"kind": "file", "attachments": cards.clone(), "kwant_id": "k2"})).await;
+ assert_eq!(spoof.results, None, "sender k1, meta k2");
+ }
+
#[tokio::test]
async fn forged_ids_and_oversized_cards_get_no_block() {
let f = Fixture::new().await;
diff --git a/src/server/smart_results/files.rs b/src/server/smart_results/files.rs
index b53cbf10..d62316d4 100644
--- a/src/server/smart_results/files.rs
+++ b/src/server/smart_results/files.rs
@@ -72,12 +72,15 @@ pub async fn candidate(conn: &mut SqliteConnection, input: &NewMessage, extra: &
}))
}
-/// Shared by this message's sender, into this message's own chat and thread, and not yet tied to
-/// a message.
+/// Shared by the bot this card names, into this message's own chat and thread, and not yet tied to
+/// a message. The bot is the server-written `meta.kwant_id` (`share_file` saves the card with the
+/// bot's display name as sender, so the sender never matches an id); no id there, no block.
fn belongs(row: &Attachment, input: &NewMessage, extra: &MessageExtra) -> bool {
+ let shared_by = extra.meta.as_ref().and_then(|m| m.get("kwant_id")).and_then(|v| v.as_str());
row.source == "agent"
&& row.message_id.is_none()
- && row.kwant_id.as_deref() == Some(input.sender_id.as_str())
+ && shared_by.is_some()
+ && row.kwant_id.as_deref() == shared_by
&& row.channel_id == input.channel_id
&& row.direct_kwant_id == input.direct_kwant_id
&& row.thread_id == extra.thread_id
diff --git a/tests/attachments_test.rs b/tests/attachments_test.rs
index cea1f266..9ae0dadb 100644
--- a/tests/attachments_test.rs
+++ b/tests/attachments_test.rs
@@ -316,6 +316,13 @@ async fn agents_share_files_as_download_cards_from_inside_their_workspace_only()
assert_eq!(meta["kind"], "file");
assert_eq!(meta["attachments"][0]["mime"], "text/csv");
let aid = meta["attachments"][0]["id"].as_str().unwrap();
+ // 31n: the card is saved with the bot's name as sender; it still gets the CSV's table preview
+ let mut shown = [card.clone()];
+ env.state.db.attach_results(&mut shown).await;
+ let block = shown[0].results.clone().expect("a shared CSV gets a files block")["blocks"][0].clone();
+ assert_eq!(block["type"], "files", "{block}");
+ assert_eq!(block["items"][0]["attachment_id"], aid);
+ assert_eq!(block["items"][0]["preview"]["kind"], "table", "{block}");
let (s, h, body) = fetch(&env, &format!("/api/attachments/{aid}/content"), &[]).await;
assert_eq!(s, StatusCode::OK);
assert_eq!(body, b"a,b\n1,2\n");