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.

6.6 KB
Raw Blame
//! Files in chats: what the user attached to a message and what an agent shared. The bytes live
//! content-addressed under `<data dir>/attachments/<sha[0..2]>/<sha>`; a row names one use of them.

use super::{Database, DbError};
use chrono::{DateTime, Utc};
use serde::Serialize;
use serde_json::{json, Value};
use sqlx::FromRow;
use uuid::Uuid;

#[derive(Debug, Clone, Serialize, FromRow, PartialEq)]
pub struct Attachment {
    pub id: String,
    pub sha256: String,
    /// Display name (sanitized: no path, no control characters).
    pub name: String,
    /// Sniffed from the bytes, never taken from the client.
    pub mime: String,
    pub size: i64,
    /// "user" (uploaded) or "agent" (shared by `kwant_id`).
    pub source: String,
    pub kwant_id: Option<String>,
    /// The chat it belongs to (channel, thread or direct-chat kwant id); null until a user
    /// upload is sent with a message.
    pub context_id: Option<String>,
    pub channel_id: Option<String>,
    pub direct_kwant_id: Option<String>,
    pub thread_id: Option<String>,
    pub message_id: Option<String>,
    pub created_at: DateTime<Utc>,
    /// G3 Point at it: the part of a page the person picked in Watch live (JSON, see
    /// `server::point::Pick`), stored with its crop. Null for every other file.
    #[sqlx(default)]
    pub point: Option<String>,
}

impl Attachment {
    /// What a message's `meta.attachments` holds: enough for a card, nothing else. (A picked
    /// part of a page gets its `point` from `server::attachments::cards`.)
    pub fn card(&self) -> Value {
        json!({"id": self.id, "name": self.name, "mime": self.mime, "size": self.size})
    }
}

/// A new row: the stored blob and who/where it came from.
#[derive(Debug, Clone, Default)]
pub struct NewAttachment {
    pub sha256: String,
    pub name: String,
    pub mime: String,
    pub size: i64,
    pub source: String,
    pub kwant_id: Option<String>,
    pub context_id: Option<String>,
    pub channel_id: Option<String>,
    pub direct_kwant_id: Option<String>,
    pub thread_id: Option<String>,
    /// See [`Attachment::point`].
    pub point: Option<String>,
}

impl Database {
    pub(super) async fn migrate_attachments(&self) -> Result<(), DbError> {
        for stmt in [
            "CREATE TABLE IF NOT EXISTS attachments (
                id TEXT PRIMARY KEY,
                sha256 TEXT NOT NULL,
                name TEXT NOT NULL,
                mime TEXT NOT NULL,
                size INTEGER NOT NULL,
                source TEXT NOT NULL CHECK (source IN ('user', 'agent')),
                kwant_id TEXT,
                context_id TEXT,
                channel_id TEXT,
                direct_kwant_id TEXT,
                thread_id TEXT,
                message_id TEXT,
                created_at TIMESTAMP NOT NULL
            )",
            "CREATE INDEX IF NOT EXISTS attachments_by_context ON attachments (context_id, created_at)",
        ] {
            sqlx::query(stmt).execute(&self.pool).await?;
        }
        // G3 (0.9.0): a picked part of a page rides on its crop's row
        let has_point: Option<(String,)> = sqlx::query_as(
            "SELECT name FROM pragma_table_info('attachments') WHERE name = 'point'",
        )
        .fetch_optional(&self.pool)
        .await?;
        if has_point.is_none() {
            sqlx::query("ALTER TABLE attachments ADD COLUMN point TEXT")
                .execute(&self.pool)
                .await?;
        }
        Ok(())
    }

    pub async fn insert_attachment(&self, a: &NewAttachment) -> Result<Attachment, DbError> {
        let id = Uuid::new_v4().to_string();
        sqlx::query(
            "INSERT INTO attachments (id, sha256, name, mime, size, source, kwant_id, context_id, channel_id, direct_kwant_id, thread_id, created_at, point)
             VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        )
        .bind(&id)
        .bind(&a.sha256)
        .bind(&a.name)
        .bind(&a.mime)
        .bind(a.size)
        .bind(&a.source)
        .bind(&a.kwant_id)
        .bind(&a.context_id)
        .bind(&a.channel_id)
        .bind(&a.direct_kwant_id)
        .bind(&a.thread_id)
        .bind(Utc::now())
        .bind(&a.point)
        .execute(&self.pool)
        .await?;
        Ok(self.get_attachment(&id).await?.expect("just inserted"))
    }

    pub async fn get_attachment(&self, id: &str) -> Result<Option<Attachment>, DbError> {
        Ok(sqlx::query_as::<_, Attachment>("SELECT * FROM attachments WHERE id = ?")
            .bind(id)
            .fetch_optional(&self.pool)
            .await?)
    }

    /// A chat's files, newest first.
    pub async fn list_attachments_in(&self, context_id: &str, limit: i64) -> Result<Vec<Attachment>, DbError> {
        Ok(sqlx::query_as::<_, Attachment>(
            "SELECT * FROM attachments WHERE context_id = ? ORDER BY created_at DESC LIMIT ?",
        )
        .bind(context_id)
        .bind(limit)
        .fetch_all(&self.pool)
        .await?)
    }

    /// The newest file `kwant_id` shared into `context_id` since `since` with these exact bytes
    /// (`sha256`), already posted as a message: a second share of it is skipped.
    pub async fn recent_agent_share(
        &self,
        context_id: &str,
        kwant_id: &str,
        sha256: &str,
        since: DateTime<Utc>,
    ) -> Result<Option<Attachment>, DbError> {
        Ok(sqlx::query_as::<_, Attachment>(
            "SELECT * FROM attachments WHERE context_id = ? AND source = 'agent' AND kwant_id = ? AND sha256 = ?
             AND message_id IS NOT NULL AND created_at >= ? ORDER BY created_at DESC LIMIT 1",
        )
        .bind(context_id)
        .bind(kwant_id)
        .bind(sha256)
        .bind(since)
        .fetch_optional(&self.pool)
        .await?)
    }

    /// Ties attachments to the message they were sent or shared with, and to its chat. Only rows
    /// not yet tied to a message change; returns how many did.
    pub async fn link_attachments(
        &self,
        ids: &[String],
        message_id: &str,
        context_id: &str,
        channel_id: Option<&str>,
        direct_kwant_id: Option<&str>,
        thread_id: Option<&str>,
    ) -> Result<u64, DbError> {
        let mut n = 0;
        for id in ids {
            n += sqlx::query(
                "UPDATE attachments SET message_id = ?, context_id = ?, channel_id = ?, direct_kwant_id = ?, thread_id = ?
                 WHERE id = ? AND message_id IS NULL",
            )
            .bind(message_id)
            .bind(context_id)
            .bind(channel_id)
            .bind(direct_kwant_id)
            .bind(thread_id)
            .bind(id)
            .execute(&self.pool)
            .await?
            .rows_affected();
        }
        Ok(n)
    }
}