Add Web Push, guided start screen, SVG icons, chunked uploads, rename
- Web Push (VAPID + RFC 8291, pure Rust): permission/question/finished notifications while no app window is visible; settings with test button - Guided new-session flow: task, craft target (website/app/game/old software/fresh), clean room or deep, description, model, folder (new, existing or cloned git repository); originals upload to original/ - Craft phase bar (craft_phase tool) for study/specify/build/check/ready - Chunked uploads (8 MB) up to server.max_upload_mb (default 500 MB) - All UI glyphs and emojis replaced by themeable SVG icons - Rename dialog with manual name or model-generated suggestion Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
013695722a
commit
a0d0e707f2
48 files changed
+2724
-228
No files matched your search
+83
-2
@@ -72,6 +72,7 @@ pub fn router(app: AppState) -> Router {
|
||||
.route("/sessions", get(list_sessions).post(create_session))
|
||||
.route("/sessions/search", get(search_sessions))
|
||||
.route("/sessions/{id}/fork", post(fork_session))
|
||||
.route("/sessions/{id}/title", post(suggest_title))
|
||||
.route("/sessions/{id}/export", get(export_session))
|
||||
.route("/sessions/{id}", get(get_session).patch(update_session).delete(delete_session))
|
||||
.route("/sessions/{id}/prompt", post(prompt))
|
||||
@@ -120,6 +121,9 @@ pub fn router(app: AppState) -> Router {
|
||||
.route("/images/config", get(images_config))
|
||||
.route("/images/{id}", delete(delete_image))
|
||||
.route("/images/{id}/file", get(image_file))
|
||||
.route("/push/key", get(push_key))
|
||||
.route("/push/subscribe", post(push_subscribe).delete(push_unsubscribe))
|
||||
.route("/push/test", post(push_test))
|
||||
.route("/ws", get(ws))
|
||||
.layer(middleware::from_fn_with_state(app.clone(), auth::require_user))
|
||||
// Prompts may carry images (base64); the default limit is 2 MB.
|
||||
@@ -163,12 +167,13 @@ async fn get_config(Extension(user): Extension<User>, State(app): State<AppState
|
||||
let default_model = if cfg.server.default_model.is_empty() { app.registry().first_model() } else { Some(cfg.server.default_model.clone()) };
|
||||
if !user.is_admin() {
|
||||
// Users only need defaults, not the configuration.
|
||||
return Ok(Json(json!({"default_model": default_model, "currency": cfg.server.currency})));
|
||||
return Ok(Json(json!({"default_model": default_model, "small_model": cfg.server.small_model, "currency": cfg.server.currency})));
|
||||
}
|
||||
Ok(Json(json!({
|
||||
"config": config::redacted_json(&cfg),
|
||||
"workspace": app.workspace(),
|
||||
"default_model": default_model,
|
||||
"small_model": cfg.server.small_model,
|
||||
"currency": cfg.server.currency,
|
||||
})))
|
||||
}
|
||||
@@ -320,6 +325,7 @@ struct CreateSession {
|
||||
model: Option<String>,
|
||||
agent: Option<String>,
|
||||
permission_mode: Option<String>,
|
||||
clean_room: Option<bool>,
|
||||
}
|
||||
|
||||
/// Resolve a directory and check it lies inside one of the user's roots.
|
||||
@@ -364,7 +370,8 @@ async fn create_session(Extension(user): Extension<User>, State(app): State<AppS
|
||||
updated_at: now,
|
||||
todos: Value::Null,
|
||||
last_usage: None,
|
||||
clean_room: None,
|
||||
clean_room: body.clean_room,
|
||||
phase: None,
|
||||
};
|
||||
let s = app.save_session(s)?;
|
||||
Ok(Json(json!(s)))
|
||||
@@ -389,6 +396,19 @@ async fn search_sessions(Extension(user): Extension<User>, State(app): State<App
|
||||
Ok(Json(json!(hits)))
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
struct TitleBody {
|
||||
model: Option<String>,
|
||||
}
|
||||
|
||||
/// Generate a title suggestion with a chosen model (not saved).
|
||||
async fn suggest_title(Extension(user): Extension<User>, State(app): State<AppState>, Path(id): Path<String>, body: Option<Json<TitleBody>>) -> ApiResult {
|
||||
writable(&app, &user, &id)?;
|
||||
let model = body.and_then(|b| b.0.model);
|
||||
let title = crate::runner::suggest_title(app.clone(), &id, model).await.map_err(|e| ApiError(StatusCode::BAD_GATEWAY, format!("{e:#}")))?;
|
||||
Ok(Json(json!({"title": title})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
struct ForkBody {
|
||||
/// Last message to keep (inclusive); all messages when missing.
|
||||
@@ -414,6 +434,7 @@ async fn fork_session(Extension(user): Extension<User>, State(app): State<AppSta
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
last_usage: None,
|
||||
phase: None,
|
||||
..src.clone()
|
||||
};
|
||||
let fork = app.save_session(fork)?;
|
||||
@@ -988,6 +1009,50 @@ async fn stats(Extension(user): Extension<User>, State(app): State<AppState>, Qu
|
||||
|
||||
// --- websocket --------------------------------------------------------------
|
||||
|
||||
// --- web push -------------------------------------------------------------------
|
||||
|
||||
async fn push_key(State(app): State<AppState>) -> ApiResult {
|
||||
Ok(Json(json!({"key": app.push.public_key()})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct PushKeys {
|
||||
p256dh: String,
|
||||
auth: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct PushSubscription {
|
||||
endpoint: String,
|
||||
keys: PushKeys,
|
||||
}
|
||||
|
||||
async fn push_subscribe(Extension(user): Extension<User>, State(app): State<AppState>, Json(sub): Json<PushSubscription>) -> ApiResult {
|
||||
if !sub.endpoint.starts_with("https://") {
|
||||
return Err(ApiError(StatusCode::BAD_REQUEST, "invalid push endpoint".into()));
|
||||
}
|
||||
app.store.push_add(&user.id, &sub.endpoint, &sub.keys.p256dh, &sub.keys.auth)?;
|
||||
Ok(Json(json!({"ok": true})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct PushEndpoint {
|
||||
endpoint: String,
|
||||
}
|
||||
|
||||
async fn push_unsubscribe(Extension(user): Extension<User>, State(app): State<AppState>, Json(b): Json<PushEndpoint>) -> ApiResult {
|
||||
// Only the owner's own subscription.
|
||||
if app.store.push_subscriptions(&user.id)?.iter().any(|s| s.endpoint == b.endpoint) {
|
||||
app.store.push_remove(&b.endpoint)?;
|
||||
}
|
||||
Ok(Json(json!({"ok": true})))
|
||||
}
|
||||
|
||||
async fn push_test(Extension(user): Extension<User>, State(app): State<AppState>) -> ApiResult {
|
||||
let sent = crate::push::send_to_user(&app, &user.id, &json!({"kind": "test", "title": "CodeCraft", "body": "Notifications work.", "tag": "test", "url": "/"})).await;
|
||||
Ok(Json(json!({"sent": sent})))
|
||||
}
|
||||
|
||||
async fn ws(Extension(user): Extension<User>, State(app): State<AppState>, upgrade: WebSocketUpgrade) -> Response {
|
||||
upgrade.on_upgrade(move |socket| ws_loop(app, user, socket))
|
||||
}
|
||||
@@ -1002,6 +1067,7 @@ async fn ws_loop(app: AppState, user: User, mut socket: WebSocket) {
|
||||
let mut ping = tokio::time::interval(std::time::Duration::from_secs(25));
|
||||
// Re-check the login regularly so disabled users lose their live connection.
|
||||
let mut recheck = tokio::time::interval(std::time::Duration::from_secs(60));
|
||||
let mut visible = false;
|
||||
loop {
|
||||
tokio::select! {
|
||||
ev = rx.recv() => match ev {
|
||||
@@ -1021,6 +1087,18 @@ async fn ws_loop(app: AppState, user: User, mut socket: WebSocket) {
|
||||
},
|
||||
msg = socket.recv() => match msg {
|
||||
Some(Ok(WsMessage::Close(_))) | None | Some(Err(_)) => break,
|
||||
Some(Ok(WsMessage::Text(t))) => {
|
||||
// The client reports whether its window is visible (push only when none is).
|
||||
if let Ok(v) = serde_json::from_str::<Value>(&t) {
|
||||
if v["type"] == "visibility" {
|
||||
let now = v["visible"].as_bool().unwrap_or(false);
|
||||
if now != visible {
|
||||
app.push.set_visible(&user.id, if now { 1 } else { -1 });
|
||||
visible = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ = ping.tick() => {
|
||||
@@ -1035,6 +1113,9 @@ async fn ws_loop(app: AppState, user: User, mut socket: WebSocket) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if visible {
|
||||
app.push.set_visible(&user.id, -1);
|
||||
}
|
||||
}
|
||||
|
||||
async fn send_event(socket: &mut WebSocket, ev: &Event) -> Result<(), axum::Error> {
|
||||
|
||||
@@ -71,6 +71,8 @@ pub struct App {
|
||||
pub shells: crate::shells::Shells,
|
||||
/// Language servers (diagnostics after edits).
|
||||
pub lsp: crate::lsp::Lsp,
|
||||
/// Web Push (VAPID key, window visibility per user).
|
||||
pub push: crate::push::Push,
|
||||
}
|
||||
|
||||
/// What a user may do with a session.
|
||||
@@ -108,6 +110,7 @@ impl App {
|
||||
tool_install: Mutex::default(),
|
||||
shells: Mutex::default(),
|
||||
lsp: Default::default(),
|
||||
push: crate::push::Push::new(&data),
|
||||
});
|
||||
crate::auth::bootstrap(&app)?;
|
||||
app.clone().start_mcp();
|
||||
|
||||
@@ -246,6 +246,8 @@ pub struct ServerConfig {
|
||||
pub budget_warning: f64,
|
||||
/// "warn" (default) or "block": refuse new model requests once a budget is used up.
|
||||
pub budget_action: String,
|
||||
/// Largest file upload in MB (uploads are sent in chunks, so the reverse proxy limit doesn't apply).
|
||||
pub max_upload_mb: u64,
|
||||
}
|
||||
|
||||
impl Default for ServerConfig {
|
||||
@@ -263,6 +265,7 @@ impl Default for ServerConfig {
|
||||
monthly_budget: 0.0,
|
||||
budget_warning: 0.8,
|
||||
budget_action: "warn".into(),
|
||||
max_upload_mb: 500,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+22
-3
@@ -373,16 +373,35 @@ pub async fn remove(Extension(user): Extension<User>, State(app): State<AppState
|
||||
pub struct UploadQuery {
|
||||
/// Target file path (absolute).
|
||||
path: String,
|
||||
/// Chunked upload: byte offset of this chunk (0 = first chunk, creates the file).
|
||||
offset: Option<u64>,
|
||||
}
|
||||
|
||||
/// Upload one file as the raw request body.
|
||||
/// Upload a file as the raw request body – whole, or in chunks with `offset`
|
||||
/// (large files: each chunk stays below the reverse proxy's body limit).
|
||||
pub async fn upload(Extension(user): Extension<User>, State(app): State<AppState>, Query(q): Query<UploadQuery>, body: Bytes) -> ApiResult {
|
||||
let p = checked_path(&app, &user, &q.path, false)?;
|
||||
if p.is_dir() {
|
||||
return Err(bad("is a directory"));
|
||||
}
|
||||
tokio::fs::write(&p, &body).await?;
|
||||
Ok(Json(json!({"ok": true, "path": p, "size": body.len()})))
|
||||
let max = app.config().server.max_upload_mb.saturating_mul(1024 * 1024);
|
||||
let offset = q.offset.unwrap_or(0);
|
||||
if offset + body.len() as u64 > max {
|
||||
return Err(ApiError(StatusCode::PAYLOAD_TOO_LARGE, format!("file is larger than {} MB", max / 1024 / 1024)));
|
||||
}
|
||||
if offset == 0 {
|
||||
tokio::fs::write(&p, &body).await?;
|
||||
} else {
|
||||
use tokio::io::AsyncWriteExt;
|
||||
let current = tokio::fs::metadata(&p).await.map(|m| m.len()).unwrap_or(0);
|
||||
if current != offset {
|
||||
return Err(ApiError(StatusCode::CONFLICT, format!("chunk offset {offset} does not match the uploaded size {current}")));
|
||||
}
|
||||
let mut f = tokio::fs::OpenOptions::new().append(true).open(&p).await?;
|
||||
f.write_all(&body).await?;
|
||||
f.flush().await?;
|
||||
}
|
||||
Ok(Json(json!({"ok": true, "path": p, "size": offset + body.len() as u64})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
||||
@@ -16,6 +16,7 @@ mod mentions;
|
||||
mod mcp;
|
||||
mod permission;
|
||||
mod prompts;
|
||||
mod push;
|
||||
mod provider;
|
||||
mod reverse;
|
||||
mod runner;
|
||||
@@ -47,6 +48,7 @@ async fn main() -> Result<()> {
|
||||
let bind = cfg.server.bind.clone();
|
||||
let app = app::App::new(path, cfg).await?;
|
||||
lsp::spawn_reaper(app.clone());
|
||||
push::spawn_listener(app.clone());
|
||||
let router = api::router(app);
|
||||
let listener = tokio::net::TcpListener::bind(&bind).await?;
|
||||
tracing::info!("listening on http://{bind}");
|
||||
|
||||
@@ -50,7 +50,7 @@ const BUILD_PROMPT: &str = "Mode: BUILD. You have full access to read, modify an
|
||||
const PLAN_PROMPT: &str = r#"Mode: PLAN. You are in read-only planning mode: you must not modify files, run state-changing commands or make commits.
|
||||
Investigate the codebase, ask clarifying questions with ask_user where requirements are ambiguous, and finish with a concrete, step-by-step implementation plan (files to change, approach, risks, verification). The user switches to build mode to execute it."#;
|
||||
|
||||
const CRAFT_PROMPT: &str = r#"Mode: CRAFT. You recreate the look and behaviour of a target (website, app or game) as a NEW, original project the user owns, working in four phases. Keep the user informed of the current phase with todo_write (one item per phase).
|
||||
const CRAFT_PROMPT: &str = r#"Mode: CRAFT. You recreate the look and behaviour of a target (website, app or game) as a NEW, original project the user owns, working in phases. Report each phase with craft_phase when you enter it (study, specify in deep mode, build, check, ready) – the user sees it as a progress bar; use todo_write for the steps inside a phase.
|
||||
|
||||
1. STUDY – Inspect the target. For websites use browser_snapshot on each relevant page (it captures every configured screen width plus visible text). Note layout, colours, typography, spacing, components, navigation and behaviour. For apps/games use screenshots the user provides and their description. Summarise your findings briefly.
|
||||
2. BUILD – Create the new project in the working directory, in the stack the user asked for (otherwise a sensible default: plain HTML/CSS/JS for sites). Write clean, original code and original placeholder content/assets; do not copy proprietary text, logos, images or code.
|
||||
|
||||
@@ -0,0 +1,303 @@
|
||||
//! Web Push notifications (VAPID, RFC 8030/8291/8292) without external services.
|
||||
//!
|
||||
//! The server sends a push to a user's subscribed devices when an agent asks
|
||||
//! for permission, asks a question, or finishes a longer run – but only while
|
||||
//! none of the user's app windows is visible.
|
||||
|
||||
use crate::app::App;
|
||||
use crate::types::Event;
|
||||
use aes_gcm::aead::{Aead, KeyInit};
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64;
|
||||
use hkdf::Hkdf;
|
||||
use p256::ecdsa::signature::Signer;
|
||||
use p256::elliptic_curve::sec1::ToSec1Point;
|
||||
use p256::{PublicKey, SecretKey};
|
||||
use serde_json::{Value, json};
|
||||
use sha2::Sha256;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
/// Runs shorter than this don't trigger a "finished" notification.
|
||||
const MIN_RUN_FOR_DONE: Duration = Duration::from_secs(20);
|
||||
|
||||
pub struct Push {
|
||||
key_path: PathBuf,
|
||||
key: OnceLock<SecretKey>,
|
||||
/// user id -> number of visible app windows (reported over the WebSocket)
|
||||
visible: Mutex<HashMap<String, usize>>,
|
||||
/// session id -> start of the current run
|
||||
runs: Mutex<HashMap<String, Instant>>,
|
||||
}
|
||||
|
||||
fn random_bytes<const N: usize>() -> [u8; N] {
|
||||
rand::random()
|
||||
}
|
||||
|
||||
impl Push {
|
||||
pub fn new(data_dir: &std::path::Path) -> Self {
|
||||
Self { key_path: data_dir.join("vapid.key"), key: OnceLock::new(), visible: Mutex::default(), runs: Mutex::default() }
|
||||
}
|
||||
|
||||
/// The VAPID key pair, created on first use and kept on the data volume.
|
||||
fn key(&self) -> &SecretKey {
|
||||
self.key.get_or_init(|| {
|
||||
if let Ok(bytes) = std::fs::read(&self.key_path) {
|
||||
if let Ok(k) = SecretKey::from_slice(&bytes) {
|
||||
return k;
|
||||
}
|
||||
}
|
||||
let k = loop {
|
||||
if let Ok(k) = SecretKey::from_slice(&random_bytes::<32>()) {
|
||||
break k;
|
||||
}
|
||||
};
|
||||
let _ = std::fs::write(&self.key_path, k.to_bytes());
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let _ = std::fs::set_permissions(&self.key_path, std::fs::Permissions::from_mode(0o600));
|
||||
}
|
||||
k
|
||||
})
|
||||
}
|
||||
|
||||
/// Public key for PushManager.subscribe (base64url, uncompressed point).
|
||||
pub fn public_key(&self) -> String {
|
||||
B64.encode(self.key().public_key().as_affine().to_uncompressed_point())
|
||||
}
|
||||
|
||||
pub fn set_visible(&self, user_id: &str, delta: isize) {
|
||||
let mut v = self.visible.lock().unwrap();
|
||||
let n = v.entry(user_id.to_string()).or_default();
|
||||
*n = (*n as isize + delta).max(0) as usize;
|
||||
}
|
||||
|
||||
fn is_visible(&self, user_id: &str) -> bool {
|
||||
self.visible.lock().unwrap().get(user_id).is_some_and(|n| *n > 0)
|
||||
}
|
||||
}
|
||||
|
||||
/// RFC 8291 "aes128gcm" encryption of one record.
|
||||
fn encrypt(ua_public: &[u8], auth: &[u8], plaintext: &[u8]) -> Result<Vec<u8>> {
|
||||
let as_secret = loop {
|
||||
if let Ok(k) = SecretKey::from_slice(&random_bytes::<32>()) {
|
||||
break k;
|
||||
}
|
||||
};
|
||||
encrypt_with(ua_public, auth, plaintext, &as_secret, random_bytes::<16>())
|
||||
}
|
||||
|
||||
fn encrypt_with(ua_public: &[u8], auth: &[u8], plaintext: &[u8], as_secret: &SecretKey, salt: [u8; 16]) -> Result<Vec<u8>> {
|
||||
let ua = PublicKey::from_sec1_bytes(ua_public).map_err(|_| anyhow!("invalid p256dh key"))?;
|
||||
let as_public = as_secret.public_key().as_affine().to_uncompressed_point();
|
||||
let shared = p256::ecdh::diffie_hellman(as_secret.to_nonzero_scalar(), ua.as_affine());
|
||||
|
||||
// IKM from the ECDH secret and the subscription's auth secret.
|
||||
let mut key_info = b"WebPush: info\0".to_vec();
|
||||
key_info.extend_from_slice(ua_public);
|
||||
key_info.extend_from_slice(&as_public);
|
||||
let mut ikm = [0u8; 32];
|
||||
Hkdf::<Sha256>::new(Some(auth), shared.raw_secret_bytes()).expand(&key_info, &mut ikm).map_err(|_| anyhow!("hkdf"))?;
|
||||
|
||||
let prk = Hkdf::<Sha256>::new(Some(&salt), &ikm);
|
||||
let mut cek = [0u8; 16];
|
||||
let mut nonce = [0u8; 12];
|
||||
prk.expand(b"Content-Encoding: aes128gcm\0", &mut cek).map_err(|_| anyhow!("hkdf"))?;
|
||||
prk.expand(b"Content-Encoding: nonce\0", &mut nonce).map_err(|_| anyhow!("hkdf"))?;
|
||||
|
||||
let mut record = plaintext.to_vec();
|
||||
record.push(2); // padding delimiter: last record
|
||||
let cipher = aes_gcm::Aes128Gcm::new_from_slice(&cek).map_err(|_| anyhow!("aes key"))?;
|
||||
let ct = cipher.encrypt(&nonce.into(), record.as_slice()).map_err(|_| anyhow!("encryption failed"))?;
|
||||
|
||||
let mut out = Vec::with_capacity(16 + 4 + 1 + 65 + ct.len());
|
||||
out.extend_from_slice(&salt);
|
||||
out.extend_from_slice(&4096u32.to_be_bytes());
|
||||
out.push(as_public.len() as u8);
|
||||
out.extend_from_slice(&as_public);
|
||||
out.extend_from_slice(&ct);
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// VAPID authorization header (RFC 8292) for an endpoint.
|
||||
fn vapid_header(key: &SecretKey, endpoint: &str, subject: &str) -> Result<String> {
|
||||
let url = reqwest::Url::parse(endpoint)?;
|
||||
let aud = url.origin().ascii_serialization();
|
||||
let exp = chrono::Utc::now().timestamp() + 12 * 3600;
|
||||
let header = B64.encode(br#"{"typ":"JWT","alg":"ES256"}"#);
|
||||
let claims = B64.encode(serde_json::to_vec(&json!({"aud": aud, "exp": exp, "sub": subject}))?);
|
||||
let signing_input = format!("{header}.{claims}");
|
||||
let sk = p256::ecdsa::SigningKey::from(key);
|
||||
let sig: p256::ecdsa::Signature = sk.sign(signing_input.as_bytes());
|
||||
let jwt = format!("{signing_input}.{}", B64.encode(sig.to_bytes()));
|
||||
let k = B64.encode(key.public_key().as_affine().to_uncompressed_point());
|
||||
Ok(format!("vapid t={jwt}, k={k}"))
|
||||
}
|
||||
|
||||
/// Send a notification to all devices of a user.
|
||||
pub async fn send_to_user(app: &App, user_id: &str, payload: &Value) -> usize {
|
||||
let subs = app.store.push_subscriptions(user_id).unwrap_or_default();
|
||||
let mut sent = 0;
|
||||
for sub in subs {
|
||||
match send_one(app, &sub.endpoint, &sub.p256dh, &sub.auth, payload).await {
|
||||
Ok(()) => sent += 1,
|
||||
Err(e) => {
|
||||
let gone = e.to_string().contains("410") || e.to_string().contains("404");
|
||||
tracing::debug!("push to {}: {e:#}", sub.endpoint);
|
||||
if gone {
|
||||
let _ = app.store.push_remove(&sub.endpoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sent
|
||||
}
|
||||
|
||||
async fn send_one(app: &App, endpoint: &str, p256dh: &str, auth: &str, payload: &Value) -> Result<()> {
|
||||
let ua = B64.decode(p256dh.trim_end_matches('=')).context("p256dh")?;
|
||||
let auth = B64.decode(auth.trim_end_matches('=')).context("auth")?;
|
||||
let body = encrypt(&ua, &auth, &serde_json::to_vec(payload)?)?;
|
||||
let cfg = app.config();
|
||||
let subject = if cfg.auth.public_url.starts_with("https://") { cfg.auth.public_url.clone() } else { "mailto:admin@localhost".to_string() };
|
||||
let resp = crate::provider::http_client()
|
||||
.post(endpoint)
|
||||
.header("Authorization", vapid_header(app.push.key(), endpoint, &subject)?)
|
||||
.header("Content-Encoding", "aes128gcm")
|
||||
.header("Content-Type", "application/octet-stream")
|
||||
.header("TTL", "86400")
|
||||
.header("Urgency", "high")
|
||||
.timeout(Duration::from_secs(20))
|
||||
.body(body)
|
||||
.send()
|
||||
.await?;
|
||||
if !resp.status().is_success() {
|
||||
bail!("HTTP {}: {}", resp.status().as_u16(), resp.text().await.unwrap_or_default().chars().take(200).collect::<String>());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Owner and title of the top-level session.
|
||||
fn session_info(app: &App, session_id: &str) -> Option<(String, String, String)> {
|
||||
let root = app.root_session_id(session_id);
|
||||
let s = app.store.get_session(&root).ok().flatten()?;
|
||||
let title = if s.title.is_empty() { "CodeCraft".to_string() } else { s.title.clone() };
|
||||
Some((s.owner_id?, title, root))
|
||||
}
|
||||
|
||||
/// Background task: turn agent events into notifications.
|
||||
pub fn spawn_listener(app: Arc<App>) {
|
||||
tokio::spawn(async move {
|
||||
let mut rx = app.subscribe();
|
||||
loop {
|
||||
let ev = match rx.recv().await {
|
||||
Ok(ev) => ev,
|
||||
Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
|
||||
Err(_) => break,
|
||||
};
|
||||
let (session_id, kind, body) = match &ev {
|
||||
Event::PermissionRequest { session_id, title, tool, .. } => (session_id.clone(), "permission", format!("{tool}: {title}")),
|
||||
Event::QuestionRequest { session_id, questions, .. } => {
|
||||
let q = questions.first().map(|q| q.question.clone()).unwrap_or_default();
|
||||
(session_id.clone(), "question", q)
|
||||
}
|
||||
Event::SessionStatus { session_id, busy: true } => {
|
||||
app.push.runs.lock().unwrap().insert(session_id.clone(), Instant::now());
|
||||
continue;
|
||||
}
|
||||
Event::SessionStatus { session_id, busy: false } => {
|
||||
let started = app.push.runs.lock().unwrap().remove(session_id);
|
||||
if !started.is_some_and(|t| t.elapsed() >= MIN_RUN_FOR_DONE) {
|
||||
continue;
|
||||
}
|
||||
// Subagents report through their parent.
|
||||
if app.store.get_session(session_id).ok().flatten().is_none_or(|s| s.parent_id.is_some()) {
|
||||
continue;
|
||||
}
|
||||
(session_id.clone(), "done", String::new())
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
let Some((owner, title, root)) = session_info(&app, &session_id) else { continue };
|
||||
if app.push.is_visible(&owner) {
|
||||
continue;
|
||||
}
|
||||
let payload = json!({
|
||||
"kind": kind,
|
||||
"title": title,
|
||||
"body": body.chars().take(180).collect::<String>(),
|
||||
"tag": format!("{root}-{kind}"),
|
||||
"url": format!("/#/s/{root}"),
|
||||
});
|
||||
let app2 = app.clone();
|
||||
tokio::spawn(async move {
|
||||
send_to_user(&app2, &owner, &payload).await;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Decrypt with the user agent's keys (RFC 8291) to check the encryption.
|
||||
#[test]
|
||||
fn roundtrip() {
|
||||
let ua_secret = SecretKey::from_slice(&[7u8; 32]).unwrap();
|
||||
let ua_public = ua_secret.public_key().as_affine().to_uncompressed_point();
|
||||
let auth = [9u8; 16];
|
||||
let msg = br#"{"title":"hi"}"#;
|
||||
let body = encrypt(&ua_public, &auth, msg).unwrap();
|
||||
|
||||
let salt = &body[..16];
|
||||
let idlen = body[20] as usize;
|
||||
let as_public = &body[21..21 + idlen];
|
||||
let ct = &body[21 + idlen..];
|
||||
let as_pk = PublicKey::from_sec1_bytes(as_public).unwrap();
|
||||
let shared = p256::ecdh::diffie_hellman(ua_secret.to_nonzero_scalar(), as_pk.as_affine());
|
||||
let mut info = b"WebPush: info\0".to_vec();
|
||||
info.extend_from_slice(&ua_public);
|
||||
info.extend_from_slice(as_public);
|
||||
let mut ikm = [0u8; 32];
|
||||
Hkdf::<Sha256>::new(Some(&auth), shared.raw_secret_bytes()).expand(&info, &mut ikm).unwrap();
|
||||
let prk = Hkdf::<Sha256>::new(Some(salt), &ikm);
|
||||
let (mut cek, mut nonce) = ([0u8; 16], [0u8; 12]);
|
||||
prk.expand(b"Content-Encoding: aes128gcm\0", &mut cek).unwrap();
|
||||
prk.expand(b"Content-Encoding: nonce\0", &mut nonce).unwrap();
|
||||
let pt = aes_gcm::Aes128Gcm::new_from_slice(&cek).unwrap().decrypt(&nonce.into(), ct).unwrap();
|
||||
assert_eq!(&pt[..pt.len() - 1], msg);
|
||||
assert_eq!(pt[pt.len() - 1], 2);
|
||||
}
|
||||
|
||||
/// Example from RFC 8291, section 5.
|
||||
#[test]
|
||||
fn rfc8291_vector() {
|
||||
let d = |s: &str| B64.decode(s).unwrap();
|
||||
let as_secret = SecretKey::from_slice(&d("yfWPiYE-n46HLnH0KqZOF1fJJU3MYrct3AELtAQ-oRw")).unwrap();
|
||||
let ua_public = d("BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4");
|
||||
let auth = d("BTBZMqHH6r4Tts7J_aSIgg");
|
||||
let salt: [u8; 16] = d("DGv6ra1nlYgDCS1FRnbzlw").try_into().unwrap();
|
||||
let out = encrypt_with(&ua_public, &auth, b"When I grow up, I want to be a watermelon", &as_secret, salt).unwrap();
|
||||
assert_eq!(
|
||||
B64.encode(out),
|
||||
"DGv6ra1nlYgDCS1FRnbzlwAAEABBBP4z9KsN6nGRTbVYI_c7VJSPQTBtkgcy27mlmlMoZIIgDll6e3vCYLocInmYWAmS6TlzAC8wEqKK6PBru3jl7A_yl95bQpu6cVPTpK4Mqgkf1CXztLVBSt2Ks3oZwbuwXPXLWyouBWLVWGNWQexSgSxsj_Qulcy4a-fN"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vapid_jwt_verifies() {
|
||||
use p256::ecdsa::signature::Verifier;
|
||||
let key = SecretKey::from_slice(&[3u8; 32]).unwrap();
|
||||
let h = vapid_header(&key, "https://fcm.googleapis.com/fcm/send/abc", "mailto:a@b.c").unwrap();
|
||||
let jwt = h.strip_prefix("vapid t=").unwrap().split(", k=").next().unwrap();
|
||||
let (input, sig) = jwt.rsplit_once('.').unwrap();
|
||||
let sig = p256::ecdsa::Signature::from_slice(&B64.decode(sig).unwrap()).unwrap();
|
||||
p256::ecdsa::VerifyingKey::from(key.public_key()).verify(input.as_bytes(), &sig).unwrap();
|
||||
let claims: Value = serde_json::from_slice(&B64.decode(input.split('.').nth(1).unwrap()).unwrap()).unwrap();
|
||||
assert_eq!(claims["aud"], "https://fcm.googleapis.com");
|
||||
}
|
||||
}
|
||||
+42
-2
@@ -220,7 +220,7 @@ pub fn available_tools(app: &App, agent: &Agent, owner: Option<&crate::store::Us
|
||||
all.into_iter()
|
||||
.filter(|t| !agent.read_only || t.read_only())
|
||||
// Craft tools only where they are useful.
|
||||
.filter(|t| agent.name == "craft" || !matches!(t.name(), "browser_snapshot" | "image_compare" | "binary_info" | "decompile") || agent.tools.is_some())
|
||||
.filter(|t| agent.name == "craft" || !matches!(t.name(), "browser_snapshot" | "image_compare" | "binary_info" | "decompile" | "craft_phase") || agent.tools.is_some())
|
||||
.filter(|t| match &agent.tools {
|
||||
None => true,
|
||||
Some(list) => {
|
||||
@@ -640,6 +640,7 @@ fn create_child(app: &Arc<App>, ctx: &ToolCtx, agent_name: &str, description: &s
|
||||
todos: Value::Null,
|
||||
last_usage: None,
|
||||
clean_room: parent.clean_room,
|
||||
phase: None,
|
||||
};
|
||||
let child = app.save_session(child)?;
|
||||
let msg = Message::new(Role::User, vec![Part::Text { text: prompt }]);
|
||||
@@ -929,6 +930,45 @@ fn maybe_generate_title(app: Arc<App>, session_id: &str, text: &str) {
|
||||
|
||||
const COMPACT_PROMPT: &str = "Summarize this coding session so far so that the work can continue seamlessly from the summary alone. Include: the user's goals and explicit requests (verbatim where important), decisions made, files created/changed (with paths) and their current state, commands/tests run and results, open problems, and the exact next steps. Be thorough but concise; use headings and bullet points.";
|
||||
|
||||
/// Suggest a title for a session from its conversation, with the given model
|
||||
/// (or the small model). The title is returned, not saved.
|
||||
pub async fn suggest_title(app: Arc<App>, session_id: &str, model_spec: Option<String>) -> Result<String> {
|
||||
let session = app.session(session_id)?;
|
||||
let spec = model_spec.filter(|m| !m.is_empty()).unwrap_or_else(|| small_model(&app, &session));
|
||||
let (provider, mut model) = app.registry().resolve(&spec)?;
|
||||
model.effort = Some("low".into());
|
||||
let history = active_history(app.store.messages(session_id)?);
|
||||
let mut transcript = transcript_of(&history);
|
||||
if transcript.len() > 12_000 {
|
||||
// Beginning (the goal) and end (where it went).
|
||||
let head = clip(&transcript, 6_000);
|
||||
let mut start = transcript.len() - 5_000;
|
||||
while !transcript.is_char_boundary(start) {
|
||||
start += 1;
|
||||
}
|
||||
transcript = format!("{head}\n…\n{}", &transcript[start..]);
|
||||
}
|
||||
if transcript.trim().is_empty() {
|
||||
bail!("the session has no messages yet");
|
||||
}
|
||||
let req = ChatRequest {
|
||||
system: "Write a short, specific title (max 6 words) for the coding session below. Use the language of the user's messages. Reply with the title only, no quotes, no punctuation at the end.".into(),
|
||||
messages: vec![Message::new(Role::User, vec![Part::Text { text: format!("<session>\n{transcript}\n</session>") }])],
|
||||
tools: vec![],
|
||||
max_tokens: 2000,
|
||||
reasoning: false,
|
||||
model: model.clone(),
|
||||
};
|
||||
let (text, usage) = complete_text(&app, &spec, provider, req, CancellationToken::new()).await?;
|
||||
record_side_usage(&app, &session, &spec, &model, &usage, "title");
|
||||
let text = strip_think(&text);
|
||||
let title = text.lines().find(|l| !l.trim().is_empty()).unwrap_or("").trim().trim_matches(['"', '\'', '*', '#', ' ']).to_string();
|
||||
if title.is_empty() {
|
||||
bail!("the model returned no title");
|
||||
}
|
||||
Ok(title.chars().take(80).collect())
|
||||
}
|
||||
|
||||
/// Remove a leading `<think>…</think>` block some models put into the text.
|
||||
fn strip_think(text: &str) -> String {
|
||||
let t = text.trim_start();
|
||||
@@ -953,7 +993,7 @@ fn clip(s: &str, max: usize) -> String {
|
||||
}
|
||||
|
||||
/// The history as readable text for summarizing (tool output shortened).
|
||||
fn transcript_of(history: &[Message]) -> String {
|
||||
pub fn transcript_of(history: &[Message]) -> String {
|
||||
let mut out = String::new();
|
||||
for m in history {
|
||||
let role = if matches!(m.role, Role::User) { "USER" } else { "ASSISTANT" };
|
||||
|
||||
@@ -97,6 +97,13 @@ impl Store {
|
||||
created_at INTEGER NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS queue_session ON queue(session_id, created_at);
|
||||
CREATE TABLE IF NOT EXISTS push_subs (
|
||||
endpoint TEXT PRIMARY KEY,
|
||||
user_id TEXT NOT NULL,
|
||||
p256dh TEXT NOT NULL,
|
||||
auth TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS audit (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
ts INTEGER NOT NULL,
|
||||
@@ -874,6 +881,30 @@ impl Store {
|
||||
// --- audit log & backup ----------------------------------------------------------
|
||||
|
||||
impl Store {
|
||||
// --- web push ---------------------------------------------------------
|
||||
|
||||
pub fn push_add(&self, user_id: &str, endpoint: &str, p256dh: &str, auth: &str) -> Result<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO push_subs (endpoint, user_id, p256dh, auth, created_at) VALUES (?1, ?2, ?3, ?4, ?5)
|
||||
ON CONFLICT(endpoint) DO UPDATE SET user_id = ?2, p256dh = ?3, auth = ?4",
|
||||
rusqlite::params![endpoint, user_id, p256dh, auth, now_ms()],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn push_remove(&self, endpoint: &str) -> Result<()> {
|
||||
self.conn.lock().unwrap().execute("DELETE FROM push_subs WHERE endpoint = ?1", [endpoint])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn push_subscriptions(&self, user_id: &str) -> Result<Vec<PushSub>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let mut stmt = conn.prepare("SELECT endpoint, p256dh, auth FROM push_subs WHERE user_id = ?1")?;
|
||||
let rows = stmt.query_map([user_id], |r| Ok(PushSub { endpoint: r.get(0)?, p256dh: r.get(1)?, auth: r.get(2)? }))?;
|
||||
Ok(rows.collect::<std::result::Result<_, _>>()?)
|
||||
}
|
||||
|
||||
pub fn audit(&self, user_id: Option<&str>, username: &str, action: &str, detail: &str, ip: &str) {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let _ = conn.execute(
|
||||
@@ -946,3 +977,9 @@ impl Store {
|
||||
Ok(data.and_then(|d| serde_json::from_str::<Value>(&d).ok()).and_then(|v| v["created_at"].as_i64()))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PushSub {
|
||||
pub endpoint: String,
|
||||
pub p256dh: String,
|
||||
pub auth: String,
|
||||
}
|
||||
@@ -55,6 +55,48 @@ impl Tool for TodoWrite {
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub const CRAFT_PHASES: &[&str] = &["study", "specify", "build", "check", "ready"];
|
||||
|
||||
/// Craft workflow: report the current phase (shown as a progress bar).
|
||||
pub struct CraftPhase;
|
||||
|
||||
#[async_trait]
|
||||
impl Tool for CraftPhase {
|
||||
fn name(&self) -> &str {
|
||||
"craft_phase"
|
||||
}
|
||||
fn description(&self) -> String {
|
||||
"Report the craft phase you are entering (study, specify, build, check, ready). The user sees it \
|
||||
as a progress bar. Call it at the start of every phase, also when you go back to an earlier one."
|
||||
.into()
|
||||
}
|
||||
fn schema(&self) -> Value {
|
||||
json!({"type": "object", "properties": {
|
||||
"phase": {"type": "string", "enum": CRAFT_PHASES},
|
||||
"note": {"type": "string", "description": "One short sentence: what happens in this phase"}
|
||||
}, "required": ["phase"]})
|
||||
}
|
||||
fn read_only(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn title(&self, input: &Value) -> String {
|
||||
format!("Phase: {}", input["phase"].as_str().unwrap_or("?"))
|
||||
}
|
||||
async fn run(&self, input: Value, ctx: &ToolCtx) -> Result<ToolOutput> {
|
||||
let phase = arg_str(&input, "phase")?;
|
||||
if !CRAFT_PHASES.contains(&phase) {
|
||||
bail!("unknown phase '{phase}' – use one of {}", CRAFT_PHASES.join(", "));
|
||||
}
|
||||
let root = ctx.app.root_session_id(&ctx.session_id);
|
||||
let mut s = ctx.app.session(&root)?;
|
||||
s.phase = Some(phase.to_string());
|
||||
ctx.app.save_session(s)?;
|
||||
Ok(ToolOutput::text(format!("Phase: {phase}")).with_meta(json!({"phase": phase, "note": input["note"]})))
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub struct AskUser;
|
||||
|
||||
#[async_trait]
|
||||
|
||||
@@ -159,6 +159,7 @@ pub fn builtin_tools() -> Vec<Arc<dyn Tool>> {
|
||||
Arc::new(crate::shells::KillShell),
|
||||
Arc::new(web::WebFetch),
|
||||
Arc::new(web::WebSearch),
|
||||
Arc::new(agent::CraftPhase),
|
||||
Arc::new(crate::lsp::Diagnostics),
|
||||
Arc::new(agent::TodoWrite),
|
||||
Arc::new(agent::AskUser),
|
||||
|
||||
@@ -127,6 +127,9 @@ pub struct Session {
|
||||
/// Craft mode: clean room (true) or deep (false); None = server default.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub clean_room: Option<bool>,
|
||||
/// Craft workflow phase reported by the agent (study, specify, build, check, ready).
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub phase: Option<String>,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
|
||||
Reference in new issue
Block a user