From 203297056daa44f69f3c07b8e698941ffc958f47 Mon Sep 17 00:00:00 2001 From: WeiCHEN Date: Mon, 7 Sep 2026 15:59:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(db):=20=E5=A2=9E=E5=8A=A0=E5=85=B1?= =?UTF-8?q?=E4=BA=AB=E7=9F=A5=E8=AF=86=E5=BA=93=E4=B8=8E=E9=9A=94=E7=A6=BB?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=95=B0=E6=8D=AE=E5=BA=93=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- deploy/postgres/kb_init.sql | 34 ++++++++++++++++ deploy/postgres/system_init.sql | 72 +++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 deploy/postgres/kb_init.sql create mode 100644 deploy/postgres/system_init.sql diff --git a/deploy/postgres/kb_init.sql b/deploy/postgres/kb_init.sql new file mode 100644 index 00000000..5b523bab --- /dev/null +++ b/deploy/postgres/kb_init.sql @@ -0,0 +1,34 @@ +-- 共享知识库 (Shared Knowledge Base) 初始化脚本 +-- pgvector 扩展及基础条目库 +CREATE EXTENSION IF NOT EXISTS vector; +CREATE EXTENSION IF NOT EXISTS pg_trgm; + +CREATE TABLE IF NOT EXISTS kb_documents ( + id BIGSERIAL PRIMARY KEY, + title TEXT NOT NULL, + doc_no TEXT NOT NULL DEFAULT '', + category TEXT NOT NULL DEFAULT '', + content TEXT NOT NULL, + embedding vector(1024), + metadata JSONB NOT NULL DEFAULT '{}'::jsonb, + is_active BOOLEAN NOT NULL DEFAULT true, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE INDEX IF NOT EXISTS idx_kb_doc_title ON kb_documents USING gin (title gin_trgm_ops); +CREATE INDEX IF NOT EXISTS idx_kb_doc_no ON kb_documents(doc_no); +CREATE INDEX IF NOT EXISTS idx_kb_category ON kb_documents(category); + +-- 共享法规/知识切片表 +CREATE TABLE IF NOT EXISTS kb_chunks ( + id BIGSERIAL PRIMARY KEY, + document_id BIGINT REFERENCES kb_documents(id) ON DELETE CASCADE, + chunk_index INT NOT NULL DEFAULT 0, + chunk_text TEXT NOT NULL, + embedding vector(1024), + tokens_count INT NOT NULL DEFAULT 0, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE INDEX IF NOT EXISTS idx_kb_chunk_doc ON kb_chunks(document_id); diff --git a/deploy/postgres/system_init.sql b/deploy/postgres/system_init.sql new file mode 100644 index 00000000..3dd3da48 --- /dev/null +++ b/deploy/postgres/system_init.sql @@ -0,0 +1,72 @@ +-- 各环境专属系统数据库 (System App DB) 初始化脚本 +-- 注意:对话记录、系统用户、执行任务状态等在各环境之间严格物理/逻辑隔离 + +CREATE TABLE IF NOT EXISTS schema_migrations ( + version INTEGER PRIMARY KEY, + applied_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +-- 系统租户/项目 +CREATE TABLE IF NOT EXISTS projects ( + id BIGSERIAL PRIMARY KEY, + name TEXT NOT NULL, + code TEXT NOT NULL UNIQUE, + environment TEXT NOT NULL DEFAULT 'dev', + status TEXT NOT NULL DEFAULT 'active', + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +-- 用户表 (隔离) +CREATE TABLE IF NOT EXISTS users ( + id BIGSERIAL PRIMARY KEY, + account_id TEXT NOT NULL UNIQUE, + name TEXT NOT NULL DEFAULT '', + role TEXT NOT NULL DEFAULT 'user', + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +-- 对话会话表 (隔离:各环境不得共享) +CREATE TABLE IF NOT EXISTS chat_sessions ( + id BIGSERIAL PRIMARY KEY, + session_id TEXT NOT NULL UNIQUE, + project_id BIGINT REFERENCES projects(id) ON DELETE CASCADE, + user_id BIGINT REFERENCES users(id), + title TEXT NOT NULL DEFAULT '新对话', + model TEXT NOT NULL DEFAULT '', + status TEXT NOT NULL DEFAULT 'active', + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE INDEX IF NOT EXISTS idx_chat_sessions_uid ON chat_sessions(user_id); +CREATE INDEX IF NOT EXISTS idx_chat_sessions_pid ON chat_sessions(project_id); + +-- 对话消息与历史流 (隔离:各环境不得共享) +CREATE TABLE IF NOT EXISTS chat_messages ( + id BIGSERIAL PRIMARY KEY, + session_id TEXT NOT NULL REFERENCES chat_sessions(session_id) ON DELETE CASCADE, + role TEXT NOT NULL, -- user / assistant / system / tool + content TEXT NOT NULL, + tool_calls JSONB, + tokens INT NOT NULL DEFAULT 0, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE INDEX IF NOT EXISTS idx_chat_messages_sid ON chat_messages(session_id); + +-- 智能体异步执行任务与审计日志 (隔离) +CREATE TABLE IF NOT EXISTS agent_tasks ( + id BIGSERIAL PRIMARY KEY, + task_id TEXT NOT NULL UNIQUE, + session_id TEXT REFERENCES chat_sessions(session_id), + status TEXT NOT NULL DEFAULT 'pending', -- pending, running, completed, failed + plan_steps JSONB NOT NULL DEFAULT '[]'::jsonb, + result_summary TEXT NOT NULL DEFAULT '', + error_message TEXT NOT NULL DEFAULT '', + started_at TIMESTAMPTZ, + finished_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE INDEX IF NOT EXISTS idx_agent_tasks_status ON agent_tasks(status);