338 lines
20 KiB
SQL
338 lines
20 KiB
SQL
-- =============================================================================
|
|
-- 医疗器械商务与研发智能参谋系统 - 专属记忆与多级上下文数据库初始化脚本
|
|
-- 运行环境:开发环境 (5433:5432) | 镜像:pgvector/pgvector:pg16
|
|
-- 涵盖范围:
|
|
-- 1. 单用户偏好记忆 (User Preferences & Habits)
|
|
-- 2. 多用户与项目级单一事实黑板 (Project Blackboards & Decision Audit)
|
|
-- 3. 会话与多轮交互日志 (Chat Sessions & Multi-Turn Messages)
|
|
-- 4. CAS 内容寻址文件存储与引用去重 (Storage Blobs & File References)
|
|
-- 5. 动态向量化记忆切片 (Context Memory Chunks with Vector 1024 & Full-Text)
|
|
-- =============================================================================
|
|
|
|
CREATE EXTENSION IF NOT EXISTS vector;
|
|
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 1. 用户与工作习惯偏好表 (User Profiles & Habits)
|
|
-- -----------------------------------------------------------------------------
|
|
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 'engineer', -- pm, director, regulatory, engineer, sales
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS user_preferences (
|
|
user_id BIGINT PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
|
|
communication_style TEXT NOT NULL DEFAULT 'concise_structured', -- concise_structured, detailed_analytical, executive_summary
|
|
focus_areas TEXT[] NOT NULL DEFAULT '{}', -- 例如: {"cost_budget", "reg_compliance", "timeline"}
|
|
output_format_pref TEXT NOT NULL DEFAULT 'standard_report', -- standard_report, bilingual_brief, ppt_bullet
|
|
custom_instructions TEXT NOT NULL DEFAULT '', -- 用户明确要求的定制化引导
|
|
memory_facts JSONB NOT NULL DEFAULT '{}'::jsonb, -- 异步蒸馏出的长期偏好画像事实
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 2. 项目与统一事实黑板表 (Projects & Blackboard SSOT)
|
|
-- -----------------------------------------------------------------------------
|
|
CREATE TABLE IF NOT EXISTS projects (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
code TEXT NOT NULL UNIQUE,
|
|
name TEXT NOT NULL,
|
|
customer_name TEXT NOT NULL DEFAULT '',
|
|
device_type TEXT NOT NULL DEFAULT '',
|
|
intended_use TEXT NOT NULL DEFAULT '',
|
|
target_market TEXT NOT NULL DEFAULT '',
|
|
current_stage TEXT NOT NULL DEFAULT 'V0.1_triage',
|
|
environment TEXT NOT NULL DEFAULT 'dev',
|
|
status TEXT NOT NULL DEFAULT 'active',
|
|
metadata_json JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS project_blackboards (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
project_id BIGINT NOT NULL UNIQUE REFERENCES projects(id) ON DELETE CASCADE,
|
|
facts JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
version INT NOT NULL DEFAULT 1,
|
|
last_updated_by TEXT NOT NULL DEFAULT 'system',
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 3. 多用户协同活动流与决策审计表 (Project Activities & Decisions)
|
|
-- -----------------------------------------------------------------------------
|
|
CREATE TABLE IF NOT EXISTS project_activities (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
project_id BIGINT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
user_id BIGINT REFERENCES users(id) ON DELETE SET NULL,
|
|
user_name TEXT NOT NULL DEFAULT '',
|
|
activity_type TEXT NOT NULL, -- chat, decision, operation, upload, download, stage_advance
|
|
title TEXT NOT NULL,
|
|
detail JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
weight NUMERIC(4,2) NOT NULL DEFAULT 1.00, -- 初始重要度权重 (决策为 1.0,闲聊为 0.2)
|
|
is_milestone BOOLEAN NOT NULL DEFAULT FALSE, -- 里程碑事件不衰减
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_project_activities_pid_time ON project_activities(project_id, created_at DESC);
|
|
CREATE INDEX IF NOT EXISTS idx_project_activities_uid ON project_activities(user_id);
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 4. 会话与多轮交互日志表 (Chat Sessions & Messages)
|
|
-- -----------------------------------------------------------------------------
|
|
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) ON DELETE SET NULL,
|
|
title TEXT NOT NULL DEFAULT '新对话',
|
|
current_stage TEXT NOT NULL DEFAULT '',
|
|
pending_exp JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
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_pid ON chat_sessions(project_id);
|
|
CREATE INDEX IF NOT EXISTS idx_chat_sessions_uid ON chat_sessions(user_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,
|
|
user_id BIGINT REFERENCES users(id) ON DELETE SET NULL,
|
|
role TEXT NOT NULL, -- user, assistant, system
|
|
content TEXT NOT NULL,
|
|
intent TEXT NOT NULL DEFAULT '',
|
|
branch TEXT NOT NULL DEFAULT '',
|
|
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
day_key DATE,
|
|
category TEXT NOT NULL DEFAULT 'uncategorized',
|
|
deleted_at TIMESTAMPTZ,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_chat_messages_sid_time ON chat_messages(session_id, created_at ASC);
|
|
CREATE INDEX IF NOT EXISTS idx_chat_messages_sid_day_id ON chat_messages(session_id, day_key DESC, id DESC);
|
|
CREATE INDEX IF NOT EXISTS idx_chat_messages_sid_category ON chat_messages(session_id, category, id DESC);
|
|
CREATE EXTENSION IF NOT EXISTS pg_trgm;
|
|
CREATE INDEX IF NOT EXISTS idx_chat_messages_content_trgm ON chat_messages USING GIN (content gin_trgm_ops);
|
|
|
|
-- 4.1 每日智能摘要表 (Daily Digests)
|
|
CREATE TABLE IF NOT EXISTS chat_daily_digests (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
session_id TEXT NOT NULL REFERENCES chat_sessions(session_id) ON DELETE CASCADE,
|
|
project_id BIGINT REFERENCES projects(id) ON DELETE SET NULL,
|
|
day_key DATE NOT NULL,
|
|
headline TEXT NOT NULL DEFAULT '',
|
|
key_points JSONB NOT NULL DEFAULT '[]'::jsonb,
|
|
deliverables JSONB NOT NULL DEFAULT '[]'::jsonb,
|
|
decisions JSONB NOT NULL DEFAULT '[]'::jsonb,
|
|
open_questions JSONB NOT NULL DEFAULT '[]'::jsonb,
|
|
stage_progress TEXT NOT NULL DEFAULT '',
|
|
first_message_id BIGINT,
|
|
last_message_id BIGINT,
|
|
message_count INT NOT NULL DEFAULT 0,
|
|
status TEXT NOT NULL DEFAULT 'pending',
|
|
source TEXT NOT NULL DEFAULT '',
|
|
model TEXT NOT NULL DEFAULT '',
|
|
token_usage INT NOT NULL DEFAULT 0,
|
|
digest_version INT NOT NULL DEFAULT 1,
|
|
generated_at TIMESTAMPTZ,
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
UNIQUE (session_id, day_key)
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_digests_sid_day ON chat_daily_digests(session_id, day_key DESC);
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 5. CAS 物理文件寻址与引用去重表 (Storage Blobs & File References)
|
|
-- -----------------------------------------------------------------------------
|
|
CREATE TABLE IF NOT EXISTS storage_blobs (
|
|
sha256 TEXT PRIMARY KEY, -- 文件 SHA-256 哈希值
|
|
size_bytes BIGINT NOT NULL, -- 物理字节数
|
|
mime_type TEXT NOT NULL DEFAULT 'application/octet-stream',
|
|
storage_path TEXT NOT NULL, -- 宿主机相对物理路径 (如 storage/blobs/ab/cd/xxx.bin)
|
|
ref_count INT NOT NULL DEFAULT 1, -- 被引用次数
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS file_references (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
project_id BIGINT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
user_id BIGINT REFERENCES users(id) ON DELETE SET NULL,
|
|
blob_sha256 TEXT NOT NULL REFERENCES storage_blobs(sha256) ON DELETE RESTRICT,
|
|
file_name TEXT NOT NULL, -- 用户原文件名
|
|
file_category TEXT NOT NULL DEFAULT 'material', -- material, standard, cad, quote, deliverable
|
|
summary TEXT NOT NULL DEFAULT '', -- 自动提取的文档摘要
|
|
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_file_references_proj ON file_references(project_id);
|
|
CREATE INDEX IF NOT EXISTS idx_file_references_blob ON file_references(blob_sha256);
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 6. 动态向量化记忆切片表 (Context Memory Chunks: User / Project / Agent)
|
|
-- -----------------------------------------------------------------------------
|
|
CREATE TABLE IF NOT EXISTS context_memory_chunks (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
domain TEXT NOT NULL, -- user_memory, project_doc, project_activity, chat_summary
|
|
project_id BIGINT REFERENCES projects(id) ON DELETE CASCADE,
|
|
user_id BIGINT REFERENCES users(id) ON DELETE SET NULL,
|
|
reference_id TEXT NOT NULL DEFAULT '', -- 关联的外键标识 (如 file_id, session_id, activity_id)
|
|
title TEXT NOT NULL DEFAULT '',
|
|
content TEXT NOT NULL,
|
|
chunk_hash TEXT NOT NULL DEFAULT '', -- 文本哈希防重复向量化
|
|
embedding vector(1024), -- bge-m3 1024 维向量
|
|
tsv tsvector, -- 中文/英文全文检索分词
|
|
importance_weight NUMERIC(4,2) NOT NULL DEFAULT 1.00,
|
|
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_mem_chunks_domain_proj ON context_memory_chunks(domain, project_id, created_at DESC);
|
|
CREATE INDEX IF NOT EXISTS idx_mem_chunks_user ON context_memory_chunks(user_id, domain);
|
|
-- chunk_hash 必须唯一:IndexApprovedDocument 使用 ON CONFLICT (chunk_hash)
|
|
CREATE UNIQUE INDEX IF NOT EXISTS uq_mem_chunks_hash ON context_memory_chunks(chunk_hash);
|
|
CREATE INDEX IF NOT EXISTS idx_mem_chunks_tsv ON context_memory_chunks USING gin (tsv);
|
|
CREATE INDEX IF NOT EXISTS idx_mem_chunks_vec ON context_memory_chunks USING hnsw (embedding vector_cosine_ops);
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 7. 受控文档台账与生命周期表 (Document Records)
|
|
-- -----------------------------------------------------------------------------
|
|
CREATE TABLE IF NOT EXISTS document_records (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
doc_id TEXT NOT NULL UNIQUE, -- 业务唯一标识,如 doc_proj1_feasibility_reg_v1
|
|
project_id BIGINT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
task_key TEXT NOT NULL, -- 任务标识: feasibility_reg, intake, cdo_quote
|
|
title TEXT NOT NULL, -- 交付物标题
|
|
stage TEXT NOT NULL DEFAULT 'V0.5_feasibility',
|
|
version INT NOT NULL DEFAULT 1,
|
|
current_state TEXT NOT NULL DEFAULT 'drafting', -- drafting, reviewing, approving, approved, rejected
|
|
is_locked BOOLEAN NOT NULL DEFAULT FALSE, -- 锁定状态 (reviewing/approving/approved 期间禁止直接修改)
|
|
is_template_only BOOLEAN NOT NULL DEFAULT TRUE, -- 是否仅为骨架模板 (模板状态即使查看也不算认领)
|
|
content_sha256 TEXT NOT NULL DEFAULT '', -- 当前版本正文哈希
|
|
work_dir TEXT NOT NULL DEFAULT '', -- 工作区相对路径
|
|
|
|
-- 三级责任人(各有且仅有一人)
|
|
author_id BIGINT REFERENCES users(id) ON DELETE SET NULL,
|
|
author_name TEXT NOT NULL DEFAULT '',
|
|
reviewer_id BIGINT REFERENCES users(id) ON DELETE SET NULL,
|
|
reviewer_name TEXT NOT NULL DEFAULT '',
|
|
approver_id BIGINT REFERENCES users(id) ON DELETE SET NULL,
|
|
approver_name TEXT NOT NULL DEFAULT '',
|
|
|
|
-- 认领状态 (Claiming State)
|
|
author_claimed BOOLEAN NOT NULL DEFAULT FALSE, -- 编制人是否已认领 (非模板状态被查看或编辑后为 true)
|
|
author_claimed_at TIMESTAMPTZ,
|
|
author_last_viewed_at TIMESTAMPTZ,
|
|
author_last_edited_at TIMESTAMPTZ,
|
|
reviewer_claimed BOOLEAN NOT NULL DEFAULT FALSE, -- 审核人是否已认领审核
|
|
reviewer_claimed_at TIMESTAMPTZ,
|
|
approver_claimed BOOLEAN NOT NULL DEFAULT FALSE, -- 批准人是否已认领批准
|
|
approver_claimed_at TIMESTAMPTZ,
|
|
|
|
-- 审核/批准最后意见与驳回原因
|
|
last_rejected_reason TEXT NOT NULL DEFAULT '', -- 驳回原因 (驳回时必填)
|
|
last_rejected_by_role TEXT NOT NULL DEFAULT '', -- reviewer, approver
|
|
last_rejected_by_name TEXT NOT NULL DEFAULT '',
|
|
last_rejected_at TIMESTAMPTZ,
|
|
approved_at TIMESTAMPTZ,
|
|
|
|
-- 可配置项 (Config JSONB)
|
|
config JSONB NOT NULL DEFAULT '{"reject_rollback_to": "drafting", "allow_approver_to_reviewer": true}'::jsonb,
|
|
|
|
-- 编辑租约 (运行时互斥,正交于 is_locked)
|
|
lease_holder_type TEXT NOT NULL DEFAULT '', -- squad | human | ''
|
|
lease_holder_id TEXT NOT NULL DEFAULT '', -- run_id / user_id
|
|
lease_work_dir TEXT NOT NULL DEFAULT '',
|
|
lease_expires_at TIMESTAMPTZ,
|
|
lease_heartbeat_at TIMESTAMPTZ,
|
|
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_doc_records_pid ON document_records(project_id);
|
|
CREATE INDEX IF NOT EXISTS idx_doc_records_task ON document_records(task_key);
|
|
CREATE INDEX IF NOT EXISTS idx_doc_records_state ON document_records(current_state);
|
|
CREATE INDEX IF NOT EXISTS idx_doc_records_author ON document_records(author_id);
|
|
CREATE INDEX IF NOT EXISTS idx_doc_records_reviewer ON document_records(reviewer_id);
|
|
CREATE INDEX IF NOT EXISTS idx_doc_records_approver ON document_records(approver_id);
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 8. 批注与修改意见表 (Document Comments & Annotations)
|
|
-- -----------------------------------------------------------------------------
|
|
CREATE TABLE IF NOT EXISTS document_comments (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
doc_id TEXT NOT NULL REFERENCES document_records(doc_id) ON DELETE CASCADE,
|
|
user_id BIGINT REFERENCES users(id) ON DELETE SET NULL,
|
|
user_name TEXT NOT NULL DEFAULT '',
|
|
user_role TEXT NOT NULL, -- author, reviewer, approver, advisor
|
|
target_section TEXT NOT NULL DEFAULT '', -- 批注针对的章节或锚点 (如 "## 结论")
|
|
comment_type TEXT NOT NULL DEFAULT 'comment', -- comment(普通批注), suggestion(修改建议), reject_reason(驳回理由)
|
|
content TEXT NOT NULL, -- 批注或意见正文
|
|
status TEXT NOT NULL DEFAULT 'open', -- open, resolved, accepted, rejected
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_doc_comments_doc ON document_comments(doc_id);
|
|
CREATE INDEX IF NOT EXISTS idx_doc_comments_user ON document_comments(user_id);
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 9. 智能体修改变更提案表 (Change Proposals for Claimed Documents)
|
|
-- -----------------------------------------------------------------------------
|
|
CREATE TABLE IF NOT EXISTS document_change_proposals (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
proposal_id TEXT NOT NULL UNIQUE, -- 变更提案唯一标识
|
|
doc_id TEXT NOT NULL REFERENCES document_records(doc_id) ON DELETE CASCADE,
|
|
project_id BIGINT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
triggered_by TEXT NOT NULL DEFAULT 'agent_squad', -- 触发来源 (agent_squad, stage_sync, user_request)
|
|
proposed_changes TEXT NOT NULL, -- 拟修改地方及差异摘要
|
|
impact_analysis TEXT NOT NULL, -- 影响范围与后果分析
|
|
risk_assessment TEXT NOT NULL, -- 潜在风险预警
|
|
status TEXT NOT NULL DEFAULT 'pending_author_approval', -- pending_author_approval, approved_by_author, rejected_by_author, applied
|
|
author_decision_reason TEXT NOT NULL DEFAULT '', -- 编制人拍板同意或拒绝修改的原因
|
|
decided_at TIMESTAMPTZ,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_change_proposals_doc ON document_change_proposals(doc_id);
|
|
CREATE INDEX IF NOT EXISTS idx_change_proposals_status ON document_change_proposals(status);
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 10. 文档治理全生命周期审计流表 (Document Audit Logs)
|
|
-- -----------------------------------------------------------------------------
|
|
CREATE TABLE IF NOT EXISTS document_audit_logs (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
doc_id TEXT NOT NULL REFERENCES document_records(doc_id) ON DELETE CASCADE,
|
|
action TEXT NOT NULL, -- view, edit, claim, submit_review, review_approve, review_reject, approver_approve, approver_reject, propose_change, decide_change
|
|
actor_id BIGINT,
|
|
actor_name TEXT NOT NULL DEFAULT '',
|
|
actor_role TEXT NOT NULL DEFAULT '', -- author, reviewer, approver, system
|
|
from_state TEXT NOT NULL DEFAULT '',
|
|
to_state TEXT NOT NULL DEFAULT '',
|
|
details JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_doc_audit_doc_time ON document_audit_logs(doc_id, created_at DESC);
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 11. 会话已读位点双轨表 (Chat Read Positions)
|
|
-- -----------------------------------------------------------------------------
|
|
CREATE TABLE IF NOT EXISTS chat_read_positions (
|
|
session_id VARCHAR(64) NOT NULL,
|
|
user_id BIGINT NOT NULL,
|
|
last_read_message_id BIGINT NOT NULL DEFAULT 0,
|
|
dismissed_unread_ids BIGINT[] NOT NULL DEFAULT '{}',
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (session_id, user_id)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_chat_read_positions_session_user
|
|
ON chat_read_positions (session_id, user_id);
|