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.

18.7 KB
Raw Blame
//! Bringing bots in from other apps and taking them out again.
//!
//! - `POST /api/import/preview?space=&filename=`: a file as the raw body, or JSON `{ "text": … }`
//!   for a pasted reply. Parses and checks it; writes nothing and keeps nothing.
//! - `POST /api/import/apply?space=`: `{ "bundle": …, "skip": [refs] }`, the bundle preview
//!   returned. Checks it again and creates it all in one transaction.
//! - `GET /api/imports?space=`, `GET /api/imports/:id?space=`, `POST /api/imports/:id/undo?space=`.
//! - `GET /api/kwants/:id/export`, `GET /api/export?space=`: `kwant.bundle/1`, keys scrubbed.
//! - `GET /api/import/prompts`: the export prompt to send a bot in Grok, ChatGPT, Claude, ….
//! - `GET /api/import/local`, `POST /api/import/local/preview?space=`: bots and skills other agent
//!   apps keep in this computer's home folder (the app on this computer only; see `import::local`).
//! - `POST /api/import/github/preview?space=`: `{ "url": … }`, a public repository on github.com,
//!   fetched from codeload only (see `import::github`).

use super::spaces::Scope;
use super::AppState;
use crate::db::imports::{ImportRecord, UndoError};
use crate::db::spaces::SpaceId;
use crate::import::{self, parsers, Bundle, Checked, ImportError, BUNDLE_FORMAT, MAX_ZIP_BYTES};
use axum::{
    body::Body,
    extract::{ConnectInfo, Path, Query, State},
    http::{header, HeaderMap, StatusCode},
    response::{IntoResponse, Response},
    Json,
};
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::HashSet;
use std::net::SocketAddr;

/// An apply body: a bundle with up to 50 bots of 2 × 16k characters, plus skills.
const MAX_APPLY_BYTES: usize = 8 * 1024 * 1024;

fn error(status: StatusCode, code: &str, message: impl Into<String>) -> Response {
    (status, Json(json!({ "error": message.into(), "code": code }))).into_response()
}

fn import_error(e: ImportError) -> Response {
    match e {
        ImportError::TooLarge(m) => error(StatusCode::PAYLOAD_TOO_LARGE, "too_large", m),
        ImportError::Unsupported(m) => error(StatusCode::UNSUPPORTED_MEDIA_TYPE, "unsupported", m),
        ImportError::Invalid(m) => error(StatusCode::UNPROCESSABLE_ENTITY, "invalid", m),
    }
}

fn internal(e: impl std::fmt::Display) -> Response {
    tracing::warn!(error = %e, "import failed");
    error(StatusCode::INTERNAL_SERVER_ERROR, "internal", "database error")
}

/// The body, read up to `limit` bytes. Only kept in memory for this request.
#[allow(clippy::result_large_err)]
async fn read_body(headers: &HeaderMap, body: Body, limit: usize, what: &str) -> Result<Vec<u8>, Response> {
    let too_large = || error(StatusCode::PAYLOAD_TOO_LARGE, "too_large", format!("{what} can be at most {} MB.", limit / (1024 * 1024)));
    let declared = headers.get(header::CONTENT_LENGTH).and_then(|v| v.to_str().ok()).and_then(|v| v.parse::<usize>().ok());
    if declared.is_some_and(|n| n > limit) {
        return Err(too_large());
    }
    let mut out = Vec::with_capacity(declared.unwrap_or(0).min(limit));
    let mut stream = body.into_data_stream();
    while let Some(chunk) = stream.next().await {
        let chunk = chunk.map_err(|_| error(StatusCode::BAD_REQUEST, "invalid", "The upload was interrupted."))?;
        if out.len() + chunk.len() > limit {
            return Err(too_large());
        }
        out.extend_from_slice(&chunk);
    }
    Ok(out)
}

#[allow(clippy::result_large_err)]
async fn names_in(state: &AppState, space: &SpaceId) -> Result<Vec<String>, Response> {
    state.db.list_kwants(space).await.map(|ks| ks.into_iter().map(|k| k.name).collect()).map_err(internal)
}

#[derive(Deserialize)]
pub struct PreviewQuery {
    filename: Option<String>,
}

#[derive(Deserialize)]
struct Pasted {
    text: String,
}

#[derive(Serialize)]
pub struct Preview {
    #[serde(flatten)]
    checked: Checked,
    /// True when nothing in `problems` stops the import.
    can_apply: bool,
}

/// `POST /api/import/preview`. A JSON body `{ "text": … }` (Content-Type `application/json`)
/// is pasted text; anything else is a file, named by `?filename=`.
pub async fn preview(State(state): State<AppState>, Scope(space): Scope, Query(q): Query<PreviewQuery>, headers: HeaderMap, body: Body) -> Response {
    let bytes = match read_body(&headers, body, MAX_ZIP_BYTES, "A file").await {
        Ok(b) => b,
        Err(r) => return r,
    };
    if bytes.is_empty() {
        return error(StatusCode::BAD_REQUEST, "empty", "Nothing was sent.");
    }
    let is_json = headers.get(header::CONTENT_TYPE).and_then(|v| v.to_str().ok()).is_some_and(|c| c.starts_with("application/json"));
    let parsed = match (is_json, serde_json::from_slice::<Pasted>(&bytes)) {
        (true, Ok(p)) => parsers::parse_pasted(&p.text),
        _ => parsers::parse_file(&bytes, q.filename.as_deref()),
    };
    drop(bytes);
    let bundle = match parsed {
        Ok(b) => b,
        Err(e) => return import_error(e),
    };
    match checked_preview(&state, &space, bundle).await {
        Ok(p) => Json(p).into_response(),
        Err(r) => r,
    }
}

/// The checks every preview runs against the space's names.
#[allow(clippy::result_large_err)]
async fn checked_preview(state: &AppState, space: &SpaceId, bundle: Bundle) -> Result<Preview, Response> {
    let names = names_in(state, space).await?;
    let checked = import::normalize(bundle, &names);
    let can_apply = checked.problems.is_empty() && !(checked.bundle.bots.is_empty() && checked.bundle.skills.is_empty());
    Ok(Preview { checked, can_apply })
}

const NOT_LOCAL: &str = "Only the kwant app on this computer can look for bots here.";

fn is_local(headers: &HeaderMap, peer: &Option<ConnectInfo<SocketAddr>>) -> bool {
    super::auth::is_local(headers, peer.as_ref().map(|c| c.0.ip()))
}

/// `GET /api/import/local`: what other agent apps keep on this computer, names and counts only.
/// Anywhere but the app on this computer, `available: false` and nothing listed.
pub async fn local_list(peer: Option<ConnectInfo<SocketAddr>>, headers: HeaderMap) -> Response {
    if !is_local(&headers, &peer) {
        return Json(json!({ "available": false, "reason": NOT_LOCAL, "sources": [] })).into_response();
    }
    let Some((home, codex)) = import::local::home() else {
        return Json(json!({ "available": false, "reason": "kwant couldn't find your home folder.", "sources": [] })).into_response();
    };
    let found = tokio::task::spawn_blocking(move || import::local::scan(&home, codex.as_deref())).await.unwrap_or_default();
    Json(json!({ "available": true, "sources": found })).into_response()
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct LocalPick {
    sources: Vec<String>,
}

/// `POST /api/import/local/preview`: `{ "sources": [ids] }` from the list, read into one preview.
pub async fn local_preview(State(state): State<AppState>, Scope(space): Scope, peer: Option<ConnectInfo<SocketAddr>>, headers: HeaderMap, body: Body) -> Response {
    if !is_local(&headers, &peer) {
        return error(StatusCode::FORBIDDEN, "local_only", NOT_LOCAL);
    }
    let bytes = match read_body(&headers, body, 64 * 1024, "A list of sources").await {
        Ok(b) => b,
        Err(r) => return r,
    };
    let bad = |m: &str| error(StatusCode::BAD_REQUEST, "bad_request", m);
    let Ok(pick) = serde_json::from_slice::<LocalPick>(&bytes) else { return bad("Send {\"sources\": [ids]} from the list.") };
    if pick.sources.is_empty() || pick.sources.len() > import::local::MAX_PICKED {
        return bad("Tick between 1 and 10 sources.");
    }
    if pick.sources.iter().collect::<HashSet<_>>().len() != pick.sources.len() {
        return bad("A source is ticked twice.");
    }
    let Some((home, codex)) = import::local::home() else { return error(StatusCode::NOT_FOUND, "not_found", "kwant couldn't find your home folder.") };
    let read = tokio::task::spawn_blocking(move || import::local::read(&home, codex.as_deref(), &pick.sources)).await;
    let bundle = match read {
        Ok(Ok(b)) => b,
        Ok(Err(import::local::ReadError::NotFound(id))) => return error(StatusCode::NOT_FOUND, "not_found", format!("{id} isn't on this computer any more. Refresh the list.")),
        Ok(Err(import::local::ReadError::Import(e))) => return import_error(e),
        Err(e) => return internal(e),
    };
    match checked_preview(&state, &space, bundle).await {
        Ok(p) => Json(p).into_response(),
        Err(r) => r,
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct GithubLink {
    url: String,
}

#[derive(Serialize)]
struct GithubPreview {
    #[serde(flatten)]
    preview: Preview,
    github: import::github::GithubInfo,
}

/// `POST /api/import/github/preview`: `{ "url": … }`, a public repository on github.com.
pub async fn github_preview(State(state): State<AppState>, Scope(space): Scope, headers: HeaderMap, body: Body) -> Response {
    let bytes = match read_body(&headers, body, 16 * 1024, "A link").await {
        Ok(b) => b,
        Err(r) => return r,
    };
    let Ok(link) = serde_json::from_slice::<GithubLink>(&bytes) else {
        return error(StatusCode::BAD_REQUEST, "bad_request", "Send {\"url\": \"https://github.com/…\"}.");
    };
    use import::github::{self, GithubError};
    let (bundle, info) = match github::preview(github::CODELOAD, &link.url).await {
        Ok(r) => r,
        Err(GithubError::NotGithub(m)) => return error(StatusCode::UNPROCESSABLE_ENTITY, "not_github", m),
        Err(GithubError::Redirect) => {
            return error(StatusCode::UNPROCESSABLE_ENTITY, "redirect", "GitHub answered with a redirect, which kwant doesn't follow. Check the link (a renamed repository needs its new name).")
        }
        Err(GithubError::NotFound) => return error(StatusCode::NOT_FOUND, "not_found", "There's no public repository or branch at this link. Private repositories can't be imported from a link."),
        Err(GithubError::FetchFailed(m)) => return error(StatusCode::BAD_GATEWAY, "fetch_failed", m),
        Err(GithubError::Import(e)) => return import_error(e),
    };
    let mut preview = match checked_preview(&state, &space, bundle).await {
        Ok(p) => p,
        Err(r) => return r,
    };
    if info.licence.is_none() {
        preview.checked.warnings.push(import::Notice { bot: None, message: github::NO_LICENCE.into() });
    }
    Json(GithubPreview { preview, github: info }).into_response()
}

#[derive(Deserialize)]
struct ApplyRequest {
    bundle: Bundle,
    #[serde(default)]
    skip: Vec<String>,
}

#[derive(Serialize)]
pub struct Applied {
    import: ImportRecord,
    warnings: Vec<import::Notice>,
    dropped: Vec<import::Notice>,
    renames: Vec<import::Rename>,
}

/// `POST /api/import/apply`. Bots named in `skip` (by `ref`) are left out, and teams lose them.
pub async fn apply(State(state): State<AppState>, Scope(space): Scope, headers: HeaderMap, body: Body) -> Response {
    let bytes = match read_body(&headers, body, MAX_APPLY_BYTES, "An import").await {
        Ok(b) => b,
        Err(r) => return r,
    };
    let req: ApplyRequest = match serde_json::from_slice(&bytes) {
        Ok(r) => r,
        Err(e) => return error(StatusCode::UNPROCESSABLE_ENTITY, "invalid", format!("This isn't an import from preview: {e}")),
    };
    let mut bundle = req.bundle;
    if bundle.format != BUNDLE_FORMAT {
        return error(StatusCode::UNPROCESSABLE_ENTITY, "invalid", format!("Send the bundle preview returned (format {BUNDLE_FORMAT})."));
    }
    let skip: HashSet<&str> = req.skip.iter().map(String::as_str).collect();
    bundle.bots.retain(|b| !skip.contains(b.ref_.as_str()));
    for t in &mut bundle.teams {
        t.members.retain(|m| !skip.contains(m.as_str()));
        if t.lead.as_deref().is_some_and(|l| skip.contains(l)) {
            t.lead = None;
        }
    }
    bundle.teams.retain(|t| !t.members.is_empty());
    let names = match names_in(&state, &space).await {
        Ok(n) => n,
        Err(r) => return r,
    };
    let checked = import::normalize(bundle, &names);
    if !checked.problems.is_empty() {
        return (StatusCode::BAD_REQUEST, Json(json!({
            "error": checked.problems.iter().map(|p| p.message.as_str()).collect::<Vec<_>>().join(" "),
            "code": "problems",
            "problems": checked.problems,
        })))
            .into_response();
    }
    if checked.bundle.bots.is_empty() && checked.bundle.skills.is_empty() {
        return error(StatusCode::BAD_REQUEST, "empty", "There's nothing left to import.");
    }
    match state.db.apply_import(&space, &checked.bundle).await {
        Ok(import) => {
            tracing::info!(import_id = %import.id, space = %space, bots = import.created.kwants.len(), "import applied");
            (
                StatusCode::CREATED,
                Json(Applied { import, warnings: checked.warnings, dropped: checked.dropped, renames: checked.renames }),
            )
                .into_response()
        }
        Err(crate::db::DbError::Conflict(m)) => error(StatusCode::CONFLICT, "name_taken", m),
        Err(e) => internal(e),
    }
}

/// The import, if it is in `space` (404 otherwise, as for any row in another space).
#[allow(clippy::result_large_err)]
async fn import_in(state: &AppState, space: &SpaceId, id: &str) -> Result<ImportRecord, Response> {
    match state.db.get_import(id).await {
        Ok(Some(r)) if r.space_id == space.as_str() => Ok(r),
        Ok(_) => Err(error(StatusCode::NOT_FOUND, "not_found", "No such import in this space.")),
        Err(e) => Err(internal(e)),
    }
}

/// `GET /api/imports`: the space's imports, newest first.
pub async fn list(State(state): State<AppState>, Scope(space): Scope) -> Response {
    match state.db.list_imports(&space).await {
        Ok(list) => Json(list).into_response(),
        Err(e) => internal(e),
    }
}

/// `GET /api/imports/:id`.
pub async fn get(State(state): State<AppState>, Scope(space): Scope, Path(id): Path<String>) -> Response {
    match import_in(&state, &space, &id).await {
        Ok(r) => Json(r).into_response(),
        Err(r) => r,
    }
}

#[derive(Deserialize, Default)]
struct UndoRequest {
    #[serde(default)]
    force: bool,
}

/// `POST /api/imports/:id/undo`, optional body `{ "force": true }` to remove bots edited since.
pub async fn undo(State(state): State<AppState>, Scope(space): Scope, Path(id): Path<String>, headers: HeaderMap, body: Body) -> Response {
    if let Err(r) = import_in(&state, &space, &id).await {
        return r;
    }
    let bytes = match read_body(&headers, body, 4096, "An undo").await {
        Ok(b) => b,
        Err(r) => return r,
    };
    let req: UndoRequest = if bytes.iter().all(u8::is_ascii_whitespace) || bytes.trim_ascii() == b"null" {
        UndoRequest::default()
    } else {
        match serde_json::from_slice(&bytes) {
            Ok(r) => r,
            Err(_) => return error(StatusCode::UNPROCESSABLE_ENTITY, "invalid", "Send {\"force\": true} or nothing."),
        }
    };
    match state.db.undo_import(&id, req.force).await {
        Ok(r) => {
            let c = &r.created;
            let gone: Vec<&str> =
                c.kwants.iter().map(|k| k.id.as_str()).chain(c.teams.iter().chain(&c.channels).map(String::as_str)).collect();
            super::passwords::forget_deleted_owners(&state, &gone).await;
            tracing::info!(import_id = %id, space = %space, "import undone");
            Json(r).into_response()
        }
        Err(UndoError::NotFound) => error(StatusCode::NOT_FOUND, "not_found", "No such import in this space."),
        Err(UndoError::AlreadyUndone) => error(StatusCode::CONFLICT, "already_undone", "This import was already undone."),
        Err(UndoError::Edited(names)) => (
            StatusCode::CONFLICT,
            Json(json!({
                "error": format!("{} changed since the import. Undo anyway to remove them with your changes.", names.join(", ")),
                "code": "edited",
                "edited": names,
            })),
        )
            .into_response(),
        Err(UndoError::InUse(what)) => (
            StatusCode::CONFLICT,
            Json(json!({
                "error": format!("This import is in use, so it can't be undone: {}. Delete those bots one by one instead.", what.join("; ")),
                "code": "in_use",
                "in_use": what,
            })),
        )
            .into_response(),
        Err(e @ UndoError::Db(_)) => {
            // Plain words for the person; the SQLite message stays in the log
            tracing::warn!(import_id = %id, error = ?e, "undoing an import failed");
            error(StatusCode::INTERNAL_SERVER_ERROR, "undo_failed", crate::db::bot_actions::undo_failure_message("this import", &e))
        }
    }
}

/// A download file name from a bot or space name: ASCII letters, digits, `-` and `_`, other
/// runs as one `-`; `fallback` when nothing is left.
pub fn file_stem(name: &str, fallback: &str) -> String {
    let mut out = String::new();
    for c in name.chars() {
        if c.is_ascii_alphanumeric() || c == '_' {
            out.push(c);
        } else if !out.ends_with('-') {
            out.push('-');
        }
    }
    let out = out.trim_matches('-');
    if out.is_empty() { fallback.to_string() } else { out.chars().take(80).collect() }
}

fn download(bundle: &Bundle, file: &str) -> Response {
    (
        [(header::CONTENT_DISPOSITION, format!("attachment; filename=\"{file}.kwant.json\""))],
        Json(bundle),
    )
        .into_response()
}

/// `GET /api/kwants/:id/export` (the row guard checks `?space=`).
pub async fn export_kwant(State(state): State<AppState>, Path(id): Path<String>) -> Response {
    match state.db.export_kwant(&id).await {
        Ok(Some(b)) => {
            let name = b.bots.first().map(|k| k.name.clone()).unwrap_or_default();
            download(&b, &file_stem(&name, &id))
        }
        Ok(None) => error(StatusCode::NOT_FOUND, "not_found", "bot not found"),
        Err(e) => internal(e),
    }
}

/// `GET /api/export?space=`: every bot and team in the space.
pub async fn export_space(State(state): State<AppState>, Scope(space): Scope) -> Response {
    let name = match state.db.get_space(&space).await {
        Ok(s) => s.map(|s| s.name).unwrap_or_default(),
        Err(e) => return internal(e),
    };
    match state.db.export_space(&space).await {
        Ok(b) => download(&b, &file_stem(&name, space.as_str())),
        Err(e) => internal(e),
    }
}

/// `GET /api/import/prompts`.
pub async fn prompts() -> Response {
    Json(import::prompts::export_prompts()).into_response()
}

#[cfg(test)]
mod tests {
    use super::file_stem;

    #[test]
    fn file_stems() {
        assert_eq!(file_stem("Default", "spc_x"), "Default");
        assert_eq!(file_stem("  My  Team / 2 ", "spc_x"), "My-Team-2");
        assert_eq!(file_stem("Café", "spc_x"), "Caf");
        assert_eq!(file_stem("★★", "spc_x"), "spc_x");
        assert_eq!(file_stem("", "spc_x"), "spc_x");
    }
}