Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

add xspace-agent/packages/core/src/db/migrations/0001_initial.sql

overstepping140afa08c39c
diff --git a/xspace-agent/packages/core/src/db/migrations/0001_initial.sql b/xspace-agent/packages/core/src/db/migrations/0001_initial.sql
new file mode 100644
index 000000000..077eae0f5
--- /dev/null
+++ b/xspace-agent/packages/core/src/db/migrations/0001_initial.sql
@@ -0,0 +1,124 @@
+-- Migration: 0001_initial
+-- Description: Create all foundation tables for xspace-agent platform
+-- Created: 2026-03-23
+
+-- Organizations (tenants)
+CREATE TABLE IF NOT EXISTS organizations (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ name TEXT NOT NULL,
+ slug TEXT UNIQUE NOT NULL,
+ plan TEXT NOT NULL DEFAULT 'free',
+ stripe_customer_id TEXT,
+ stripe_subscription_id TEXT,
+ settings JSONB DEFAULT '{}',
+ created_at TIMESTAMPTZ DEFAULT NOW(),
+ updated_at TIMESTAMPTZ DEFAULT NOW()
+);
+
+-- Users
+CREATE TABLE IF NOT EXISTS users (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ org_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
+ email TEXT UNIQUE NOT NULL,
+ name TEXT,
+ role TEXT NOT NULL DEFAULT 'member',
+ password_hash TEXT,
+ sso_provider TEXT,
+ sso_subject TEXT,
+ last_login_at TIMESTAMPTZ,
+ created_at TIMESTAMPTZ DEFAULT NOW()
+);
+
+-- API Keys
+CREATE TABLE IF NOT EXISTS api_keys (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ org_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
+ key_hash TEXT NOT NULL,
+ key_prefix TEXT NOT NULL,
+ name TEXT NOT NULL,
+ scopes TEXT[] DEFAULT '{}',
+ rate_limit INTEGER DEFAULT 1000,
+ last_used_at TIMESTAMPTZ,
+ expires_at TIMESTAMPTZ,
+ created_at TIMESTAMPTZ DEFAULT NOW()
+);
+
+-- Agents
+CREATE TABLE IF NOT EXISTS agents (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ org_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
+ name TEXT NOT NULL,
+ config JSONB NOT NULL,
+ status TEXT DEFAULT 'idle',
+ version INTEGER DEFAULT 1,
+ created_at TIMESTAMPTZ DEFAULT NOW(),
+ updated_at TIMESTAMPTZ DEFAULT NOW()
+);
+
+-- Agent Sessions
+CREATE TABLE IF NOT EXISTS agent_sessions (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ agent_id UUID REFERENCES agents(id) ON DELETE CASCADE,
+ org_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
+ space_url TEXT,
+ platform TEXT NOT NULL DEFAULT 'x-spaces',
+ status TEXT NOT NULL DEFAULT 'active',
+ started_at TIMESTAMPTZ DEFAULT NOW(),
+ ended_at TIMESTAMPTZ,
+ duration_seconds INTEGER,
+ metadata JSONB DEFAULT '{}'
+);
+
+-- Conversations
+CREATE TABLE IF NOT EXISTS conversations (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ session_id UUID REFERENCES agent_sessions(id) ON DELETE CASCADE,
+ org_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
+ messages JSONB DEFAULT '[]',
+ summary TEXT,
+ sentiment_avg REAL,
+ created_at TIMESTAMPTZ DEFAULT NOW()
+);
+
+-- Usage Records (for billing)
+CREATE TABLE IF NOT EXISTS usage_records (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ org_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
+ session_id UUID REFERENCES agent_sessions(id),
+ metric TEXT NOT NULL,
+ quantity BIGINT NOT NULL,
+ unit_cost_cents BIGINT,
+ provider TEXT,
+ recorded_at TIMESTAMPTZ DEFAULT NOW()
+);
+
+-- Audit Logs
+CREATE TABLE IF NOT EXISTS audit_logs (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ org_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
+ user_id UUID REFERENCES users(id),
+ action TEXT NOT NULL,
+ resource_type TEXT NOT NULL,
+ resource_id UUID,
+ details JSONB DEFAULT '{}',
+ ip_address INET,
+ created_at TIMESTAMPTZ DEFAULT NOW()
+);
+
+-- Migrations tracking table
+CREATE TABLE IF NOT EXISTS _migrations (
+ id SERIAL PRIMARY KEY,
+ name TEXT UNIQUE NOT NULL,
+ applied_at TIMESTAMPTZ DEFAULT NOW()
+);
+
+-- Indexes
+CREATE INDEX IF NOT EXISTS idx_usage_records_org_metric ON usage_records(org_id, metric, recorded_at);
+CREATE INDEX IF NOT EXISTS idx_agent_sessions_org ON agent_sessions(org_id, started_at);
+CREATE INDEX IF NOT EXISTS idx_audit_logs_org ON audit_logs(org_id, created_at);
+CREATE INDEX IF NOT EXISTS idx_api_keys_hash ON api_keys(key_hash);
+CREATE UNIQUE INDEX IF NOT EXISTS idx_users_email ON users(email);
+CREATE UNIQUE INDEX IF NOT EXISTS idx_organizations_slug ON organizations(slug);
+CREATE INDEX IF NOT EXISTS idx_organizations_stripe ON organizations(stripe_customer_id);
+
+