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.9 KB
Raw Blame
CREATE TABLE IF NOT EXISTS kwants (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    title TEXT NOT NULL,
    persona_prompt TEXT NOT NULL,
    soul TEXT NOT NULL DEFAULT '',
    avatar_sprite TEXT NOT NULL,
    driver_type TEXT NOT NULL DEFAULT 'api',
    preferred_model TEXT NOT NULL DEFAULT 'auto',
    temperature REAL NOT NULL DEFAULT 0.7,
    max_tokens INTEGER NOT NULL DEFAULT 4096,
    level INTEGER NOT NULL DEFAULT 1,
    turns_completed INTEGER NOT NULL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS channels (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    description TEXT,
    lead_kwant_id TEXT NOT NULL,
    member_kwants TEXT NOT NULL,
    routing_mode TEXT NOT NULL DEFAULT 'auto',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(lead_kwant_id) REFERENCES kwants(id)
);

CREATE TABLE IF NOT EXISTS messages (
    id TEXT PRIMARY KEY,
    channel_id TEXT,
    direct_kwant_id TEXT,
    sender_role TEXT NOT NULL,
    sender_id TEXT NOT NULL,
    text TEXT NOT NULL,
    reasoning_content TEXT,
    tool_calls TEXT,
    tokens_used INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS approvals (
    id TEXT PRIMARY KEY,
    turn_id TEXT NOT NULL,
    kwant_id TEXT NOT NULL,
    tool_name TEXT NOT NULL,
    parameters TEXT NOT NULL,
    status TEXT NOT NULL DEFAULT 'pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS settings (
    key TEXT PRIMARY KEY,
    value TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS teams (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL UNIQUE,
    description TEXT,
    icon TEXT NOT NULL DEFAULT 'users',
    lead_kwant_id TEXT NOT NULL,
    member_kwants TEXT NOT NULL,
    workspace_path TEXT,
    repo_url TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(lead_kwant_id) REFERENCES kwants(id)
);


CREATE TABLE IF NOT EXISTS mcp_servers (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL UNIQUE,
    transport TEXT NOT NULL,
    command TEXT,
    args TEXT NOT NULL DEFAULT '[]',
    url TEXT,
    env TEXT NOT NULL DEFAULT '{}',
    headers TEXT NOT NULL DEFAULT '{}',
    enabled INTEGER NOT NULL DEFAULT 1,
    timeout_secs INTEGER NOT NULL DEFAULT 30,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS kwant_mcp_servers (
    kwant_id TEXT NOT NULL,
    server_id TEXT NOT NULL,
    PRIMARY KEY (kwant_id, server_id),
    FOREIGN KEY(kwant_id) REFERENCES kwants(id) ON DELETE CASCADE,
    FOREIGN KEY(server_id) REFERENCES mcp_servers(id) ON DELETE CASCADE
);

-- Per-kwant opt-outs. Every enabled server is on for every kwant unless a row here turns it off.
-- kwant_mcp_servers (the old opt-in list) is no longer read, but kept so a rollback finds it intact
CREATE TABLE IF NOT EXISTS kwant_mcp_disabled (
    kwant_id TEXT NOT NULL,
    server_id TEXT NOT NULL,
    PRIMARY KEY (kwant_id, server_id),
    FOREIGN KEY(kwant_id) REFERENCES kwants(id) ON DELETE CASCADE,
    FOREIGN KEY(server_id) REFERENCES mcp_servers(id) ON DELETE CASCADE
);

-- OAuth client registrations (RFC 7591), one per server, redirect URI and authorization server
CREATE TABLE IF NOT EXISTS mcp_oauth_clients (
    server_id TEXT NOT NULL,
    redirect_uri TEXT NOT NULL,
    issuer TEXT NOT NULL,
    client_id TEXT NOT NULL,
    client_secret TEXT,
    auth_method TEXT NOT NULL DEFAULT 'none',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (server_id, redirect_uri, issuer),
    FOREIGN KEY(server_id) REFERENCES mcp_servers(id) ON DELETE CASCADE
);

-- OAuth tokens for http MCP servers, plus what a refresh needs. Secrets: never serialized
CREATE TABLE IF NOT EXISTS mcp_oauth_tokens (
    server_id TEXT PRIMARY KEY,
    access_token TEXT NOT NULL,
    refresh_token TEXT,
    expires_at TIMESTAMP,
    token_endpoint TEXT NOT NULL,
    client_id TEXT NOT NULL,
    client_secret TEXT,
    auth_method TEXT NOT NULL DEFAULT 'none',
    resource TEXT NOT NULL,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(server_id) REFERENCES mcp_servers(id) ON DELETE CASCADE
);

-- LLM accounts per engine (claude, codex, grok). kind: system = the CLI's own login or the
-- Settings key (how kwnt always worked), cli_home = a separate CLI config dir, api_key = a stored key
CREATE TABLE IF NOT EXISTS llm_profiles (
    id TEXT PRIMARY KEY,
    engine TEXT NOT NULL CHECK (engine IN ('claude', 'codex', 'grok')),
    name TEXT NOT NULL,
    kind TEXT NOT NULL CHECK (kind IN ('system', 'cli_home', 'api_key')),
    home_dir TEXT UNIQUE,
    priority INTEGER NOT NULL DEFAULT 100,
    enabled INTEGER NOT NULL DEFAULT 1,
    is_default INTEGER NOT NULL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE (engine, name COLLATE NOCASE)
);

CREATE UNIQUE INDEX IF NOT EXISTS llm_profiles_one_default ON llm_profiles (engine) WHERE is_default = 1;

-- Write-only API keys. Never serialized, never logged
CREATE TABLE IF NOT EXISTS llm_profile_secrets (
    profile_id TEXT PRIMARY KEY,
    api_key TEXT NOT NULL,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(profile_id) REFERENCES llm_profiles(id) ON DELETE CASCADE
);

-- Failover state: a quota-limited profile is skipped until limited_until. last_error is a class, never provider text
CREATE TABLE IF NOT EXISTS llm_profile_state (
    profile_id TEXT PRIMARY KEY,
    limited_until TIMESTAMP,
    last_error TEXT,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(profile_id) REFERENCES llm_profiles(id) ON DELETE CASCADE
);

-- Agent memory: short facts a kwant carries into every turn. source: 'user' (added in the UI) or 'agent' (its own tools)
CREATE TABLE IF NOT EXISTS kwant_memories (
    id TEXT PRIMARY KEY,
    kwant_id TEXT NOT NULL,
    text TEXT NOT NULL,
    source TEXT NOT NULL DEFAULT 'user' CHECK (source IN ('user', 'agent')),
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS kwant_memories_by_kwant ON kwant_memories (kwant_id, created_at);

-- Scheduled agent runs. schedule is JSON (see crate::routines::Schedule). channel_id NULL = the kwant direct chat
CREATE TABLE IF NOT EXISTS routines (
    id TEXT PRIMARY KEY,
    kwant_id TEXT NOT NULL,
    channel_id TEXT,
    name TEXT NOT NULL,
    instructions TEXT NOT NULL,
    schedule TEXT NOT NULL,
    enabled INTEGER NOT NULL DEFAULT 1,
    next_run_at TIMESTAMP,
    last_run_at TIMESTAMP,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS routine_runs (
    id TEXT PRIMARY KEY,
    routine_id TEXT NOT NULL,
    trigger TEXT NOT NULL CHECK (trigger IN ('schedule', 'manual')),
    started_at TIMESTAMP NOT NULL,
    ended_at TIMESTAMP,
    status TEXT NOT NULL CHECK (status IN ('running', 'ok', 'error', 'skipped', 'needs_you', 'incomplete')),
    error TEXT,
    message_id TEXT
);

CREATE INDEX IF NOT EXISTS routine_runs_by_routine ON routine_runs (routine_id, started_at)