feat: file/image upload + separated framework root (sessions/data/uploads)

- POST /api/uploads: base64 -> shared ~/.strata-ctl/uploads store (uuid+ext),
  content-sniffed kind (image/text/binary), text token-counted via the model
  tokenizer; metadata persisted in a new uploads table.
- Chat attachments routed by kind: images -> Vision bridge (multipart image_url),
  small text (<=2000 tokens) inlined, large text/binary -> path + shell_exec
  reminder; binaries never inlined. Attachment context persisted with the turn.
- GUI: attach button + file picker, removable chips, attachments sent with the
  turn and shown on the user bubble; cleared on session switch.
- Framework root ~/.strata-ctl separated by type: sessions/<name>/, data/ (SQLite,
  moved from ~/.config/strata-ctl), uploads/. Legacy DB copied once on first run.
- count_tokens seam on Formatter (real tokenizer override) + ChatService.
- i18n: upload errors + attachment markers (backend) and attach.* (GUI), it+en.
- 121 tests + 2 ignored; clippy/fmt clean; validated live (upload routing, real
  tokenizer count, image->Vision answered "Rosso", new layout).
This commit is contained in:
adm committed 2026-11-25 16:25:55 +01:00
1 parent 498e3dbc1b
commit 65f8111ba2
19 files changed
+672 -47

No files matched your search

+73
View File
@@ -322,6 +322,10 @@ async fn first_interaction_creates_personal_folder() {
.folder
.expect("personal folder created at first interaction");
assert!(std::path::Path::new(&folder).is_dir());
assert!(
folder.contains("/sessions/"),
"folder under sessions/: {folder}"
);
assert!(s.name_locked);
let _ = std::fs::remove_dir_all(&folder);
}
@@ -446,3 +450,72 @@ async fn settings_are_copied_on_create_and_isolated() {
Some(0.5)
);
}
#[tokio::test]
async fn upload_routes_by_kind_and_attaches_to_chat() {
use base64::Engine;
let (app, store) = build_app().await;
let b64 = |bytes: &[u8]| base64::engine::general_purpose::STANDARD.encode(bytes);
// Small text file -> kind "text", tokens measured, bytes on disk under uploads/.
let (st, text) = req(
&app,
Method::POST,
"/api/uploads",
Some(serde_json::json!({"name": "nota.txt", "data": b64(b"contenuto di prova")})),
)
.await;
assert_eq!(st, StatusCode::OK, "{text}");
let meta: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(meta["kind"], "text");
assert!(meta["tokens"].as_u64().unwrap() > 0);
let path = meta["path"].as_str().unwrap().to_string();
assert!(std::path::Path::new(&path).is_file());
assert!(path.contains("/uploads/"), "shared upload dir: {path}");
let text_id = meta["id"].as_str().unwrap().to_string();
// Binary (NUL / invalid UTF-8) -> kind "binary", never inlined.
let (st, text) = req(
&app,
Method::POST,
"/api/uploads",
Some(serde_json::json!({"name": "dat.bin", "data": b64(&[0u8, 1, 2, 255])})),
)
.await;
assert_eq!(st, StatusCode::OK, "{text}");
let bin: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(bin["kind"], "binary");
let bin_id = bin["id"].as_str().unwrap().to_string();
// Image (PNG magic) -> kind "image" (routed through Vision, not inline text).
let png = [0x89u8, b'P', b'N', b'G', b'\r', b'\n', 0x1a, b'\n', 0, 0];
let (st, text) = req(
&app,
Method::POST,
"/api/uploads",
Some(serde_json::json!({"name": "foto.png", "data": b64(&png)})),
)
.await;
assert_eq!(st, StatusCode::OK, "{text}");
let img: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(img["kind"], "image");
// Chat attaching text + binary: the persisted user turn carries the inline
// text and the binary path reminder — never the binary bytes themselves.
let id = create_session(&app).await;
let (st, _) = req(
&app,
Method::POST,
&format!("/api/sessions/{id}/chat"),
Some(serde_json::json!({"content": "vedi allegati", "attachments": [text_id, bin_id]})),
)
.await;
assert_eq!(st, StatusCode::OK);
let msgs = store.messages(&id).unwrap();
let user = &msgs[0].content;
assert!(user.contains("contenuto di prova"), "inline text: {user}");
assert!(user.contains("dat.bin"), "binary path reminder: {user}");
assert!(user.contains("/uploads/"), "path present: {user}");
let _ = std::fs::remove_file(&path);
}