ai_orchestration.sql 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  1. -- biz-buddy 智能体体系数据表(迁移自 postgres_ry_ai.sql)
  2. -- 去 RuoYi 列:create_dept/del_flag/tenant_id;保留 owner_dept_id/visible_scope 作为本项目可见范围治理列
  3. -- 幂等:create table / index if not exists,可重复执行
  4. -- dev 库可能存在同名旧表:先删后建(用户已确认)
  5. drop table if exists ai_model_config cascade;
  6. drop table if exists ai_knowledge_base cascade;
  7. drop table if exists ai_document cascade;
  8. drop table if exists ai_chunk cascade;
  9. drop table if exists ai_entry_agent cascade;
  10. drop table if exists ai_expert_agent cascade;
  11. drop table if exists ai_agent_tool cascade;
  12. drop table if exists ai_agent_tool_trust cascade;
  13. drop table if exists ai_agent_kb cascade;
  14. drop table if exists ai_session cascade;
  15. drop table if exists ai_session_resource cascade;
  16. drop table if exists ai_message cascade;
  17. drop table if exists ai_message_attachment cascade;
  18. drop table if exists ai_tool_call_log cascade;
  19. drop table if exists ai_route_run cascade;
  20. drop table if exists ai_route_expert_call cascade;
  21. drop table if exists ai_tool cascade;
  22. drop table if exists ai_mcp_client cascade;
  23. drop table if exists ai_tool_tag cascade;
  24. drop table if exists ai_tool_tag_rel cascade;
  25. drop table if exists ai_skill cascade;
  26. drop table if exists ai_skill_resource cascade;
  27. drop table if exists ai_agent_skill cascade;
  28. drop table if exists ai_usage_stat cascade;
  29. drop table if exists ai_permission_decision_log cascade;
  30. drop table if exists ai_trace cascade;
  31. drop table if exists ai_scenario_pack cascade;
  32. drop table if exists ai_scenario_pack_tool cascade;
  33. drop table if exists ai_scenario_pack_mcp_tool cascade;
  34. drop table if exists ai_scenario_pack_skill cascade;
  35. drop table if exists ai_sensitive_word cascade;
  36. drop table if exists ai_safety_event cascade;
  37. -- ============================================================
  38. -- 二、AI 业务表(33 张 ai_*)
  39. -- ============================================================
  40. -- 按业务域组织:模型与知识库 / 智能体与会话 / 工具与集成 / 技能 /
  41. -- 治理与观测 / 场景包。
  42. -- 约定:主键一律 bigint(业务侧雪花 ID,非自增);审计列 create_dept / create_by /
  43. -- create_time / update_by / update_time;业务主表用 del_flag char(1) default '0'
  44. -- 逻辑删除,关联与明细表物理删除、不设 del_flag;除 ai_skill_resource 对 ai_skill
  45. -- 的级联外键外,不使用数据库外键约束。
  46. -- ============================================================
  47. -- ============================================================
  48. -- 模型与知识库
  49. -- ============================================================
  50. create table if not exists ai_model_config (
  51. id bigint not null,
  52. config_name varchar(100) not null,
  53. provider varchar(50) not null,
  54. model_type varchar(20) not null,
  55. model_code varchar(100) not null,
  56. supports_vision char(1) default '1',
  57. base_url varchar(255) not null,
  58. api_key text,
  59. dimension integer,
  60. timeout_seconds integer default 60,
  61. max_retries integer default 1,
  62. status char(1) default '0',
  63. remark varchar(500),
  64. create_by bigint,
  65. create_time timestamp,
  66. update_by bigint,
  67. update_time timestamp,
  68. input_price numeric(12, 6),
  69. output_price numeric(12, 6),
  70. constraint pk_ai_model_config primary key (id)
  71. );
  72. comment on table ai_model_config is 'AI 模型配置表';
  73. comment on column ai_model_config.config_name is '配置名称';
  74. comment on column ai_model_config.provider is '提供商(openai/dashscope/deepseek/glm/ollama/local/custom;local=本地 ONNX 模型)';
  75. comment on column ai_model_config.model_type is '模型类型(chat 对话 / embedding 向量 / decision 决策 / rerank 重排)';
  76. comment on column ai_model_config.model_code is '模型标识(如 qwen-plus、text-embedding-v3)';
  77. comment on column ai_model_config.supports_vision is '是否支持视觉(图片输入;仅对话模型有意义):1是 0否';
  78. comment on column ai_model_config.base_url is '服务地址(OpenAI 兼容根地址;provider=local 时为本地模型目录)';
  79. comment on column ai_model_config.api_key is 'API 密钥(AES 密文)';
  80. comment on column ai_model_config.dimension is '向量维度(embedding 模型必填)';
  81. comment on column ai_model_config.timeout_seconds is '超时时间(秒)';
  82. comment on column ai_model_config.max_retries is '失败重试次数';
  83. comment on column ai_model_config.status is '状态(0正常 1停用)';
  84. comment on column ai_model_config.input_price is '输入单价(元 / 千 Token),为空按 0 计';
  85. comment on column ai_model_config.output_price is '输出单价(元 / 千 Token),为空按 0 计';
  86. create unique index if not exists uk_ai_model_config_name on ai_model_config (config_name);
  87. create table if not exists ai_knowledge_base (
  88. is_builtin char(1) default '0',
  89. owner_dept_id bigint,
  90. visible_scope char(1) default '0',
  91. id bigint not null,
  92. kb_name varchar(100) not null,
  93. kb_code varchar(100) not null,
  94. description varchar(500),
  95. embedding_model_id bigint not null,
  96. collection_name varchar(100) not null,
  97. dimension integer not null,
  98. chunk_size integer default 500,
  99. chunk_overlap integer default 50,
  100. retrieval_top_k integer default 5,
  101. rerank_enabled varchar(1) not null default '0',
  102. rerank_model_id bigint,
  103. retrieval_mode varchar(16) not null default 'vector',
  104. bm25_index_status varchar(16) not null default 'none',
  105. similarity_threshold numeric(4, 3) default 0.500,
  106. status char(1) default '0',
  107. remark varchar(500),
  108. create_by bigint,
  109. create_time timestamp,
  110. update_by bigint,
  111. update_time timestamp,
  112. constraint pk_ai_knowledge_base primary key (id)
  113. );
  114. comment on table ai_knowledge_base is 'AI 知识库表';
  115. comment on column ai_knowledge_base.owner_dept_id is '归属部门(生效范围=本部门时生效)';
  116. comment on column ai_knowledge_base.visible_scope is '生效范围(0公开 1本部门 2仅本人)';
  117. comment on column ai_knowledge_base.kb_code is '知识库编码(唯一,用于命名 Milvus 集合)';
  118. comment on column ai_knowledge_base.embedding_model_id is '向量模型(ai_model_config.id,model_type=embedding)';
  119. comment on column ai_knowledge_base.collection_name is 'Milvus 集合名称';
  120. comment on column ai_knowledge_base.dimension is '向量维度(与向量模型一致)';
  121. comment on column ai_knowledge_base.chunk_size is '切片长度(字符)';
  122. comment on column ai_knowledge_base.chunk_overlap is '切片重叠长度(字符)';
  123. comment on column ai_knowledge_base.retrieval_top_k is '默认召回条数';
  124. comment on column ai_knowledge_base.rerank_enabled is '是否启用检索重排(0否 1是)';
  125. comment on column ai_knowledge_base.rerank_model_id is '重排模型(ai_model_config.id,model_type=rerank)';
  126. comment on column ai_knowledge_base.retrieval_mode is '检索方式(vector 向量 / hybrid 向量+BM25)';
  127. comment on column ai_knowledge_base.bm25_index_status is 'BM25 索引状态(none/building/ready/failed)';
  128. comment on column ai_knowledge_base.similarity_threshold is '默认相似度阈值';
  129. comment on column ai_knowledge_base.status is '状态(0正常 1停用)';
  130. create unique index if not exists uk_ai_knowledge_base_code on ai_knowledge_base (kb_code);
  131. create table if not exists ai_document (
  132. id bigint not null,
  133. kb_id bigint not null,
  134. doc_name varchar(255) not null,
  135. file_suffix varchar(20),
  136. oss_id bigint,
  137. file_url varchar(500),
  138. file_size bigint,
  139. char_count integer default 0,
  140. chunk_count integer default 0,
  141. parse_status char(1) default '0',
  142. error_msg varchar(1000),
  143. remark varchar(500),
  144. create_by bigint,
  145. create_time timestamp,
  146. update_by bigint,
  147. update_time timestamp,
  148. content text,
  149. vector_status char(1) default '0',
  150. vector_error_msg varchar(1000),
  151. constraint pk_ai_document primary key (id)
  152. );
  153. comment on table ai_document is 'AI 知识库文档表';
  154. comment on column ai_document.doc_name is '文档名称';
  155. comment on column ai_document.file_suffix is '文件后缀(小写,不含点)';
  156. comment on column ai_document.oss_id is 'OSS 文件 ID(sys_oss.oss_id)';
  157. comment on column ai_document.file_url is '文件访问地址';
  158. comment on column ai_document.file_size is '文件大小(字节)';
  159. comment on column ai_document.char_count is '解析后字符数';
  160. comment on column ai_document.chunk_count is '切片数量';
  161. comment on column ai_document.parse_status is '解析状态(0待解析 1解析中 2已解析 3解析失败)';
  162. comment on column ai_document.error_msg is '解析失败原因';
  163. comment on column ai_document.content is '解析后的纯文本内容(用于在线预览)';
  164. comment on column ai_document.vector_status is '向量化状态(0待向量化 1向量化中 2已向量化 3向量化失败)';
  165. comment on column ai_document.vector_error_msg is '向量化失败原因';
  166. create index if not exists idx_ai_document_kb on ai_document (kb_id);
  167. create table if not exists ai_chunk (
  168. id bigint not null,
  169. kb_id bigint not null,
  170. doc_id bigint not null,
  171. chunk_index integer not null,
  172. content text,
  173. char_count integer default 0,
  174. vector_id varchar(100),
  175. heading_path varchar(500),
  176. page_no integer,
  177. char_start integer,
  178. char_end integer,
  179. create_by bigint,
  180. create_time timestamp,
  181. update_by bigint,
  182. update_time timestamp,
  183. constraint pk_ai_chunk primary key (id)
  184. );
  185. comment on table ai_chunk is 'AI 知识库文档切片表';
  186. comment on column ai_chunk.chunk_index is '切片序号(从 0 开始)';
  187. comment on column ai_chunk.content is '切片正文';
  188. comment on column ai_chunk.vector_id is 'Milvus 中的向量主键';
  189. comment on column ai_chunk.heading_path is '章节路径(各级标题用 > 连接,如 第2章 > 2.3 考勤)';
  190. comment on column ai_chunk.page_no is '切片起始页码(PDF 等分页文档)';
  191. comment on column ai_chunk.char_start is '切片在文档纯文本中的起始字符下标(含)';
  192. comment on column ai_chunk.char_end is '切片在文档纯文本中的结束字符下标(不含)';
  193. create index if not exists idx_ai_chunk_doc on ai_chunk (doc_id);
  194. create index if not exists idx_ai_chunk_kb on ai_chunk (kb_id);
  195. -- 存量库增量(幂等):为已存在的 ai_chunk 补充溯源元数据列;全新库由上方建表语句已含这些列,可重复执行。
  196. alter table ai_chunk add column if not exists heading_path varchar(500);
  197. alter table ai_chunk add column if not exists page_no integer;
  198. alter table ai_chunk add column if not exists char_start integer;
  199. alter table ai_chunk add column if not exists char_end integer;
  200. -- ============================================================
  201. -- 智能体与会话
  202. -- ============================================================
  203. create table if not exists ai_entry_agent (
  204. is_builtin char(1) default '0',
  205. owner_dept_id bigint,
  206. visible_scope char(1) default '0',
  207. id bigint not null,
  208. agent_name varchar(100) not null,
  209. agent_code varchar(100) not null,
  210. description varchar(500),
  211. sys_prompt text,
  212. model_id bigint not null,
  213. memory_model_id bigint,
  214. summary_model_id bigint,
  215. daily_token_quota bigint default 0,
  216. daily_request_quota integer default 0,
  217. monthly_token_quota bigint default 0,
  218. max_iters integer default 10,
  219. status char(1) default '0',
  220. remark varchar(500),
  221. singleton char(1) not null default '1',
  222. create_by bigint,
  223. create_time timestamp,
  224. update_by bigint,
  225. update_time timestamp,
  226. constraint pk_ai_entry_agent primary key (id),
  227. constraint ck_ai_entry_agent_singleton CHECK ((singleton = '1'::bpchar))
  228. );
  229. comment on table ai_entry_agent is 'AI 入口智能体表(全平台唯一,恒 1 行;REQ-AI-33 起由 ai_agent 拆出)';
  230. comment on column ai_entry_agent.owner_dept_id is '归属部门(可见范围=本部门时生效)';
  231. comment on column ai_entry_agent.visible_scope is '可见范围(0全员 1本部门 2仅创建人)';
  232. comment on column ai_entry_agent.agent_code is '智能体编码(唯一,作为 AgentScope 的 agent name)';
  233. comment on column ai_entry_agent.singleton is '单例行标记(恒 1,配合唯一索引强制「入口智能体全平台唯一」)';
  234. create unique index if not exists uk_ai_entry_agent_code on ai_entry_agent (agent_code);
  235. create unique index if not exists uk_ai_entry_agent_singleton on ai_entry_agent (singleton);
  236. create table if not exists ai_expert_agent (
  237. is_builtin char(1) default '0',
  238. owner_dept_id bigint,
  239. visible_scope char(1) default '0',
  240. id bigint not null,
  241. agent_name varchar(100) not null,
  242. agent_code varchar(100) not null,
  243. description varchar(500),
  244. sys_prompt text,
  245. model_id bigint not null,
  246. memory_model_id bigint,
  247. summary_model_id bigint,
  248. daily_token_quota bigint default 0,
  249. daily_request_quota integer default 0,
  250. monthly_token_quota bigint default 0,
  251. max_iters integer default 10,
  252. status char(1) default '0',
  253. remark varchar(500),
  254. expert_domain varchar(64),
  255. capability_desc varchar(500),
  256. output_contract text,
  257. expert_write_enabled char(1) not null default '1',
  258. auto_route char(1) not null default '1',
  259. memory_enabled char(1) not null default '1',
  260. create_by bigint,
  261. create_time timestamp,
  262. update_by bigint,
  263. update_time timestamp,
  264. constraint pk_ai_expert_agent primary key (id),
  265. constraint ck_ai_expert_agent_auto_route CHECK ((auto_route = ANY (ARRAY['0'::bpchar, '1'::bpchar])))
  266. );
  267. comment on table ai_expert_agent is 'AI 专家智能体表(可被入口智能体路由;REQ-AI-33 起由 ai_agent 拆出)';
  268. comment on column ai_expert_agent.owner_dept_id is '归属部门(可见范围=本部门时生效)';
  269. comment on column ai_expert_agent.visible_scope is '可见范围(0全员 1本部门 2仅创建人)';
  270. comment on column ai_expert_agent.agent_code is '智能体编码(唯一,作为 AgentScope 的 agent name 与路由专家 key)';
  271. comment on column ai_expert_agent.expert_domain is '专家领域标签(2026-10-02 起定义页已移除该输入项、不再参与换专家;仅存列)';
  272. comment on column ai_expert_agent.capability_desc is '能力描述(注入入口智能体的路由提示词;为空回落 description)';
  273. comment on column ai_expert_agent.output_contract is '默认输出契约(JSON Schema 文本,可空);任务级契约优先';
  274. comment on column ai_expert_agent.expert_write_enabled is '专家写工具开关(恒为 1:允许,按「允许」档生效、「需确认」档走 HITL 确认卡片)';
  275. comment on column ai_expert_agent.auto_route is '是否自动路由(1 进入口智能体的自动候选清单;0 只能被用户手动指定)';
  276. comment on column ai_expert_agent.memory_enabled is '是否启用长期记忆(1 开启 / 0 关闭:不注入也不沉淀长期记忆)';
  277. create index if not exists idx_ai_expert_agent_auto_route on ai_expert_agent (auto_route, status);
  278. create unique index if not exists uk_ai_expert_agent_code on ai_expert_agent (agent_code);
  279. create table if not exists ai_agent_tool (
  280. id bigint not null,
  281. agent_id bigint not null,
  282. tool_code varchar(100) not null,
  283. remark varchar(500),
  284. create_by bigint,
  285. create_time timestamp,
  286. update_by bigint,
  287. update_time timestamp,
  288. decision char(1) default '0',
  289. constraint pk_ai_agent_tool primary key (id)
  290. );
  291. comment on table ai_agent_tool is 'AI 智能体与业务工具关联表';
  292. comment on column ai_agent_tool.tool_code is '工具编码(由 AiToolCatalog 定义,P2 改为工具注册表驱动)';
  293. comment on column ai_agent_tool.decision is '工具授权决策(0允许 1需人工确认 2禁止)';
  294. create unique index if not exists uk_ai_agent_tool on ai_agent_tool (agent_id, tool_code);
  295. create table if not exists ai_agent_tool_trust (
  296. id bigint not null,
  297. agent_id bigint not null,
  298. tool_code varchar(100) not null,
  299. user_id bigint not null,
  300. decision varchar(1) not null,
  301. source varchar(16) not null,
  302. remark varchar(500),
  303. create_by bigint,
  304. create_time timestamp,
  305. update_by bigint,
  306. update_time timestamp,
  307. constraint pk_ai_agent_tool_trust primary key (id)
  308. );
  309. comment on table ai_agent_tool_trust is 'AI 智能体 × 工具 × 用户的信任决策表(「记住该工具」沉淀,撤销即删除)';
  310. comment on column ai_agent_tool_trust.user_id is '记住该工具的用户(信任只对该用户生效)';
  311. create unique index if not exists uk_ai_agent_tool_trust on ai_agent_tool_trust (agent_id, tool_code, user_id);
  312. create table if not exists ai_agent_kb (
  313. id bigint not null,
  314. agent_id bigint not null,
  315. kb_id bigint not null,
  316. remark varchar(500),
  317. create_by bigint,
  318. create_time timestamp,
  319. update_by bigint,
  320. update_time timestamp,
  321. constraint pk_ai_agent_kb primary key (id)
  322. );
  323. comment on table ai_agent_kb is 'AI 智能体与知识库关联表';
  324. comment on column ai_agent_kb.kb_id is '知识库主键(ai_knowledge_base.id)';
  325. create unique index if not exists uk_ai_agent_kb on ai_agent_kb (agent_id, kb_id);
  326. create table if not exists ai_session (
  327. case_id bigint,
  328. id bigint not null,
  329. session_title varchar(200),
  330. agent_id bigint not null,
  331. agent_code varchar(100) not null,
  332. user_id bigint not null,
  333. dept_id bigint,
  334. message_count integer default 0,
  335. status char(1) default '0',
  336. last_active_time timestamp,
  337. remark varchar(500),
  338. create_by bigint,
  339. create_time timestamp,
  340. update_by bigint,
  341. update_time timestamp,
  342. top_flag char(1) default '0',
  343. source varchar(20) default 'chat',
  344. scenario_pack_id bigint,
  345. scenario_pack_code varchar(64),
  346. pinned_expert_id bigint,
  347. plan_mode char(1) default '0',
  348. permission_mode varchar(16),
  349. constraint pk_ai_session primary key (id)
  350. );
  351. comment on table ai_session is 'AI 会话表(id 即 AgentScope 的 sessionId)';
  352. comment on column ai_session.case_id is '归属案件(案件级隔离:会话 / Agent 工作区 / 长期记忆 / 附件按案件隔离;case_id 为空表示存量全局会话,保留为 legacy,不参与案件列表)';
  353. comment on column ai_session.session_title is '会话标题(首条用户消息截取生成)';
  354. comment on column ai_session.agent_id is '所属智能体(ai_agent.id)';
  355. comment on column ai_session.agent_code is '智能体编码快照,智能体删除后仍可追溯';
  356. comment on column ai_session.user_id is '归属用户 ID';
  357. comment on column ai_session.dept_id is '归属部门 ID';
  358. comment on column ai_session.message_count is '消息条数';
  359. comment on column ai_session.status is '状态(0进行中 1已结束)';
  360. comment on column ai_session.last_active_time is '最后活跃时间';
  361. comment on column ai_session.top_flag is '置顶标志(0普通 1置顶;置顶会话在列表中优先排序)';
  362. comment on column ai_session.source is '会话来源:chat 手动对话';
  363. comment on column ai_session.scenario_pack_id is '所选场景包(ai_scenario_pack.id,可空;为空时该会话不接入任何 MCP 工具,也不叠加包内工具与技能)';
  364. comment on column ai_session.scenario_pack_code is '场景包编码快照(与 agent_code 同款,避免展示时回查)';
  365. comment on column ai_session.pinned_expert_id is '指定专家(ai_agent.id,可空):非空=本会话本轮强制路由到该专家(用户指定,action=pinned),空=走入口智能体正常决策;会话内可变更(取消指定即置空)';
  366. comment on column ai_session.plan_mode is '计划模式(0关闭 1开启):开启后每轮先进入只读计划阶段,产出 plans/PLAN.md 并经确认后执行;任务清单随计划能力默认启用';
  367. comment on column ai_session.permission_mode is '权限模式(confirm 变更前确认 / full 完全访问):会话级工具授权档位,由聊天输入框切换;为空时回退全局 permission-mode 配置(默认 DEFAULT=变更前确认)';
  368. create index if not exists idx_ai_session_user on ai_session (user_id, last_active_time DESC);
  369. create index if not exists idx_ai_session_case_user on ai_session (case_id, user_id, last_active_time DESC);
  370. create table if not exists ai_session_resource (
  371. id bigint not null,
  372. session_id bigint not null,
  373. resource_type varchar(20) not null,
  374. resource_id bigint not null,
  375. resource_key varchar(200) not null default '',
  376. remark varchar(500),
  377. create_by bigint,
  378. create_time timestamp,
  379. update_by bigint,
  380. update_time timestamp,
  381. constraint pk_ai_session_resource primary key (id)
  382. );
  383. comment on table ai_session_resource is 'AI 会话级资源集(REQ-AI-32):会话选中的场景包 / 业务工具 / 技能 / MCP 工具,对数剑与专家同时可见,会话内可增删、下一轮生效';
  384. comment on column ai_session_resource.session_id is '会话主键(ai_session.id)';
  385. comment on column ai_session_resource.resource_type is '资源类型(scenario 场景包 / tool 业务工具 / skill 技能 / mcp_tool MCP 工具)';
  386. comment on column ai_session_resource.resource_id is '资源主键(场景包→ai_scenario_pack.id;工具→ai_tool.id;技能→ai_skill.id;MCP 工具→ai_mcp_client.id)';
  387. comment on column ai_session_resource.resource_key is '稳定键(展示与去重):场景包编码 / 工具编码 / 技能名 / MCP 远端工具名';
  388. create index if not exists idx_ai_session_resource_session on ai_session_resource (session_id);
  389. create unique index if not exists uk_ai_session_resource on ai_session_resource (session_id, resource_type, resource_id, resource_key);
  390. create table if not exists ai_message (
  391. id bigint not null,
  392. session_id bigint not null,
  393. role varchar(20) not null,
  394. content text,
  395. reply_id varchar(64),
  396. streaming char(1) default '0',
  397. status char(1) default '0',
  398. error_msg varchar(1000),
  399. tool_call_count integer default 0,
  400. prompt_tokens integer,
  401. completion_tokens integer,
  402. total_tokens integer,
  403. duration_ms bigint,
  404. sources text,
  405. remark varchar(500),
  406. create_by bigint,
  407. create_time timestamp,
  408. update_by bigint,
  409. update_time timestamp,
  410. constraint pk_ai_message primary key (id)
  411. );
  412. comment on table ai_message is 'AI 会话消息表';
  413. comment on column ai_message.role is '角色(user 用户 / assistant 助手 / system 系统)';
  414. comment on column ai_message.content is '消息正文(助手为流式拼接后的完整答案)';
  415. comment on column ai_message.reply_id is 'AgentScope replyId,用于聚合流式块';
  416. comment on column ai_message.streaming is '流式状态(0已完成 1流式中)';
  417. comment on column ai_message.status is '状态(0正常 1失败)';
  418. comment on column ai_message.error_msg is '失败原因';
  419. comment on column ai_message.tool_call_count is '本轮工具调用次数';
  420. comment on column ai_message.prompt_tokens is '输入 Token 数';
  421. comment on column ai_message.completion_tokens is '输出 Token 数';
  422. comment on column ai_message.total_tokens is '总 Token 数';
  423. comment on column ai_message.duration_ms is '本轮耗时(毫秒)';
  424. comment on column ai_message.sources is '回答参考来源(JSON 字符串,知识库命中;可空)';
  425. create index if not exists idx_ai_message_session on ai_message (session_id, id);
  426. create index if not exists idx_ai_message_create_time on ai_message (create_time);
  427. -- 存量库增量(幂等):为已存在的 ai_message 补充「回答参考来源」列
  428. alter table ai_message add column if not exists sources text;
  429. create table if not exists ai_message_attachment (
  430. id bigint not null,
  431. session_id bigint not null,
  432. message_id bigint,
  433. file_type varchar(16) not null,
  434. file_name varchar(255) not null,
  435. file_suffix varchar(32),
  436. file_size bigint,
  437. mime_type varchar(64),
  438. oss_id bigint,
  439. parsed_content text,
  440. create_by bigint,
  441. create_time timestamp,
  442. update_by bigint,
  443. update_time timestamp,
  444. constraint pk_ai_message_attachment primary key (id)
  445. );
  446. comment on table ai_message_attachment is 'AI 会话消息附件表(聊天上传的图片/文档)';
  447. comment on column ai_message_attachment.session_id is '所属会话(ai_session.id)';
  448. comment on column ai_message_attachment.message_id is '所属用户消息(ai_message.id);为空表示尚未发送的暂存件';
  449. comment on column ai_message_attachment.file_type is '附件类型(image 图片 / document 文档)';
  450. comment on column ai_message_attachment.file_name is '原始文件名';
  451. comment on column ai_message_attachment.file_suffix is '文件后缀(小写,不含点)';
  452. comment on column ai_message_attachment.file_size is '文件大小(字节)';
  453. comment on column ai_message_attachment.mime_type is 'MIME 类型(图片为 image/*)';
  454. comment on column ai_message_attachment.oss_id is '对象存储主键(sys_oss.oss_id)';
  455. comment on column ai_message_attachment.parsed_content is '文档解析后的纯文本(图片为空)';
  456. create index if not exists idx_ai_message_attachment_session on ai_message_attachment (session_id);
  457. create index if not exists idx_ai_message_attachment_message on ai_message_attachment (message_id);
  458. -- 暂存件回收:按「未发送 + 创建时间」扫描(部分索引,避免把已发送附件也纳入索引)
  459. create index if not exists idx_ai_message_attachment_pending on ai_message_attachment (create_time) where message_id is null;
  460. create table if not exists ai_tool_call_log (
  461. id bigint not null,
  462. session_id bigint not null,
  463. message_id bigint,
  464. user_id bigint,
  465. tool_call_id varchar(64),
  466. tool_name varchar(100) not null,
  467. tool_input text,
  468. tool_output text,
  469. state varchar(20),
  470. duration_ms bigint,
  471. remark varchar(500),
  472. create_by bigint,
  473. create_time timestamp,
  474. update_by bigint,
  475. update_time timestamp,
  476. agent_id bigint,
  477. constraint pk_ai_tool_call_log primary key (id)
  478. );
  479. comment on table ai_tool_call_log is 'AI 工具调用明细表(物理追加)';
  480. comment on column ai_tool_call_log.session_id is '所属会话(ai_session.id)';
  481. comment on column ai_tool_call_log.message_id is '所属助手消息(ai_message.id)';
  482. comment on column ai_tool_call_log.user_id is '触发用户 ID';
  483. comment on column ai_tool_call_log.tool_call_id is 'AgentScope toolCallId';
  484. comment on column ai_tool_call_log.tool_name is '工具名称';
  485. comment on column ai_tool_call_log.tool_input is '入参 JSON(由 tool_call delta 增量拼接)';
  486. comment on column ai_tool_call_log.tool_output is '结果摘要(截断后)';
  487. comment on column ai_tool_call_log.state is '结果状态(SUCCESS / ERROR / INTERRUPTED / DENIED)';
  488. comment on column ai_tool_call_log.duration_ms is '工具执行耗时(毫秒)';
  489. comment on column ai_tool_call_log.agent_id is '发起该工具调用的智能体主键(入口轮=会话智能体,专家调用=专家,无人值守=对应智能体;历史行为空)';
  490. create index if not exists idx_ai_tool_call_agent on ai_tool_call_log (message_id, agent_id);
  491. create index if not exists idx_ai_tool_call_log_session on ai_tool_call_log (session_id, id);
  492. create index if not exists idx_ai_tool_call_log_create_time on ai_tool_call_log (create_time);
  493. create table if not exists ai_route_run (
  494. id bigint not null,
  495. session_id bigint not null,
  496. message_id bigint,
  497. entry_agent_id bigint not null,
  498. user_id bigint,
  499. action varchar(8),
  500. decision_json text,
  501. task_context text,
  502. expert_call_count integer default 0,
  503. status varchar(16) default 'running',
  504. degrade_reason varchar(500),
  505. duration_ms bigint,
  506. create_by bigint,
  507. create_time timestamp,
  508. update_by bigint,
  509. update_time timestamp,
  510. warning_text varchar(1000),
  511. constraint pk_ai_route_run primary key (id)
  512. );
  513. comment on table ai_route_run is 'AI 编排运行表(入口智能体的每一轮编排一行;自答时也落一行用于统计路由命中率)';
  514. comment on column ai_route_run.session_id is '用户会话主键(ai_session.id)';
  515. comment on column ai_route_run.message_id is '本轮的助手消息主键(ai_message.id)';
  516. comment on column ai_route_run.entry_agent_id is '入口智能体主键(本期恒为数剑)';
  517. comment on column ai_route_run.user_id is '提问用户';
  518. comment on column ai_route_run.action is '路由决策(self 自答 / route 路由)';
  519. comment on column ai_route_run.decision_json is '路由决策原文(含 reason / confidence,用于复盘决策质量)';
  520. comment on column ai_route_run.task_context is '下发给专家的任务片段(原样留档,用于事后核对「到底传了什么」)';
  521. comment on column ai_route_run.expert_call_count is '本次编排的专家调用次数(含重试与换专家)';
  522. comment on column ai_route_run.status is '编排状态:running / success / degraded(降级自答)/ aborted';
  523. comment on column ai_route_run.degrade_reason is '降级或中止原因';
  524. comment on column ai_route_run.duration_ms is '编排总耗时(毫秒)';
  525. comment on column ai_route_run.warning_text is '编排告警汇总(只告警不阻断):L3 脱敏命中项(按类型计数)+ 事实篡改抽查不一致项(专家关键数字未在最终回答中出现);无告警时为空';
  526. create index if not exists idx_ai_route_run_message on ai_route_run (message_id);
  527. create index if not exists idx_ai_route_run_session on ai_route_run (session_id, id);
  528. create index if not exists idx_ai_route_run_create_time on ai_route_run (create_time);
  529. create table if not exists ai_route_expert_call (
  530. id bigint not null,
  531. route_run_id bigint not null,
  532. expert_agent_id bigint not null,
  533. expert_session_id varchar(128),
  534. attempt_no integer default 1,
  535. call_kind varchar(8),
  536. contract_json text,
  537. raw_output text,
  538. parsed_output text,
  539. validation_error varchar(1000),
  540. status varchar(16),
  541. prompt_tokens bigint,
  542. completion_tokens bigint,
  543. duration_ms bigint,
  544. create_by bigint,
  545. create_time timestamp,
  546. constraint pk_ai_route_expert_call primary key (id)
  547. );
  548. comment on table ai_route_expert_call is 'AI 专家调用明细表(一次专家调用一行;追加型,物理删除)';
  549. comment on column ai_route_expert_call.route_run_id is '所属编排(ai_route_run.id)';
  550. comment on column ai_route_expert_call.expert_agent_id is '被调用的专家智能体主键(ai_agent.id)';
  551. comment on column ai_route_expert_call.expert_session_id is '专家独立会话标识(routeRunId#expertAgentId),与用户会话完全隔离';
  552. comment on column ai_route_expert_call.attempt_no is '第几次尝试(从 1 起)';
  553. comment on column ai_route_expert_call.call_kind is '调用类型:first 首次 / retry 同专家重试 / switch 换专家';
  554. comment on column ai_route_expert_call.contract_json is '本次下发的输出契约(第 3 次尝试记录放宽后的 partial 契约,便于核对当时按哪个松紧度判定)';
  555. comment on column ai_route_expert_call.raw_output is '专家原始输出原文(不改写、不裁剪,供前端来源卡片与事后追溯)';
  556. comment on column ai_route_expert_call.parsed_output is '通过契约校验后的结构化结果(JSON 文本)';
  557. comment on column ai_route_expert_call.validation_error is '结构校验失败原因(重试时回传给专家)';
  558. comment on column ai_route_expert_call.status is '调用状态:success / invalid / failed / timeout / pending_confirm / cancelled';
  559. comment on column ai_route_expert_call.prompt_tokens is '该次调用的输入 Token(计入编排总预算)';
  560. comment on column ai_route_expert_call.completion_tokens is '该次调用的输出 Token';
  561. comment on column ai_route_expert_call.duration_ms is '该次调用耗时(毫秒)';
  562. create index if not exists idx_ai_route_call_expert on ai_route_expert_call (expert_agent_id, create_time DESC);
  563. create index if not exists idx_ai_route_call_run on ai_route_expert_call (route_run_id, attempt_no);
  564. create index if not exists idx_ai_route_expert_call_create_time on ai_route_expert_call (create_time);
  565. -- ============================================================
  566. -- 工具与集成
  567. -- ============================================================
  568. create table if not exists ai_tool (
  569. id bigint not null,
  570. tool_code varchar(100) not null,
  571. tool_name varchar(100) not null,
  572. description varchar(500),
  573. tool_type char(1) default '0',
  574. params_schema text,
  575. impl_name varchar(100),
  576. status char(1) default '0',
  577. remark varchar(500),
  578. create_by bigint,
  579. create_time timestamp,
  580. update_by bigint,
  581. update_time timestamp,
  582. mcp_exposed char(1) default '0',
  583. sort_order integer not null default 0,
  584. constraint pk_ai_tool primary key (id)
  585. );
  586. comment on table ai_tool is '业务工具注册表(可被智能体调用的语义化工具)';
  587. comment on column ai_tool.sort_order is '排序值(升序,越小越靠前;默认 0)';
  588. comment on column ai_tool.tool_type is '工具类型(0只读 1写入;写入类需 P2 的 HITL 确认闭环)';
  589. comment on column ai_tool.params_schema is '参数定义(JSON Schema)';
  590. comment on column ai_tool.impl_name is '代码侧实现标识(AiToolFactory 中的 key)';
  591. comment on column ai_tool.status is '状态(0启用 1停用;停用后不再装配进智能体)';
  592. comment on column ai_tool.mcp_exposed is '是否对外暴露给 MCP 出口(0否 1是;仅只读工具可勾选)';
  593. create unique index if not exists uk_ai_tool_code on ai_tool (tool_code);
  594. create table if not exists ai_mcp_client (
  595. id bigint not null,
  596. server_name varchar(100) not null,
  597. transport varchar(20) not null,
  598. url varchar(500) not null,
  599. headers_json text,
  600. enable_tools varchar(2000),
  601. timeout_ms integer,
  602. init_timeout_ms integer,
  603. description varchar(500),
  604. status char(1) default '0',
  605. remark varchar(500),
  606. create_by bigint,
  607. create_time timestamp,
  608. update_by bigint,
  609. update_time timestamp,
  610. constraint pk_ai_mcp_client primary key (id)
  611. );
  612. comment on table ai_mcp_client is '外部 MCP Server 连接配置表(平台级,全部智能体可用)';
  613. comment on column ai_mcp_client.server_name is 'Server 名称(唯一,作为 ToolsConfig.mcpServers 的键)';
  614. comment on column ai_mcp_client.transport is '传输方式(sse / http;不放开 stdio)';
  615. comment on column ai_mcp_client.url is 'Server 地址(sse 或 streamable-http 端点)';
  616. comment on column ai_mcp_client.headers_json is '请求头 JSON(AES 密文,可承载 Authorization 等凭据,接口不回显明文)';
  617. comment on column ai_mcp_client.enable_tools is '工具白名单(逗号分隔;为空表示放行该 Server 全部工具)';
  618. comment on column ai_mcp_client.timeout_ms is '单次请求超时(毫秒),为空取框架默认';
  619. comment on column ai_mcp_client.init_timeout_ms is '初始化超时(毫秒),为空取框架默认';
  620. comment on column ai_mcp_client.status is '状态(0启用 1停用;停用后不再接入智能体装配)';
  621. create unique index if not exists uk_ai_mcp_client_name on ai_mcp_client (server_name);
  622. create table if not exists ai_tool_tag (
  623. id bigint not null,
  624. tag_name varchar(50) not null,
  625. remark varchar(500),
  626. create_by bigint,
  627. create_time timestamp,
  628. update_by bigint,
  629. update_time timestamp,
  630. constraint pk_ai_tool_tag primary key (id)
  631. );
  632. comment on table ai_tool_tag is '业务工具标签定义表';
  633. comment on column ai_tool_tag.tag_name is '标签名称(唯一,最长 50 字)';
  634. comment on column ai_tool_tag.remark is '备注(用途说明,便于管理大量标签)';
  635. create unique index if not exists uk_ai_tool_tag_name on ai_tool_tag (tag_name);
  636. create table if not exists ai_tool_tag_rel (
  637. id bigint not null,
  638. tool_id bigint not null,
  639. tag_id bigint not null,
  640. create_time timestamp,
  641. constraint pk_ai_tool_tag_rel primary key (id)
  642. );
  643. comment on table ai_tool_tag_rel is '业务工具与标签关联表(物理删除)';
  644. comment on column ai_tool_tag_rel.tool_id is '业务工具主键(ai_tool.id)';
  645. comment on column ai_tool_tag_rel.tag_id is '标签主键(ai_tool_tag.id)';
  646. create index if not exists idx_ai_tool_tag_rel_tag on ai_tool_tag_rel (tag_id);
  647. create unique index if not exists uk_ai_tool_tag_rel on ai_tool_tag_rel (tool_id, tag_id);
  648. -- ============================================================
  649. -- 技能
  650. -- ============================================================
  651. create table if not exists ai_skill (
  652. is_builtin char(1) default '0',
  653. owner_dept_id bigint,
  654. visible_scope char(1) default '0',
  655. id bigint not null,
  656. name varchar(255) not null,
  657. description text not null,
  658. skill_content text not null,
  659. source varchar(255) not null default 'bizbuddy',
  660. metadata_json text,
  661. created_at timestamp,
  662. updated_at timestamp,
  663. status char(1) default '0',
  664. remark varchar(500),
  665. create_by bigint,
  666. update_by bigint,
  667. constraint pk_ai_skill primary key (id)
  668. );
  669. comment on table ai_skill is 'AI 技能表(正文即 SKILL.md;列结构兼容 AgentScope PostgreSQL 技能仓库)';
  670. comment on column ai_skill.owner_dept_id is '归属部门(生效范围=本部门时生效)';
  671. comment on column ai_skill.visible_scope is '生效范围(0公开 1本部门 2仅本人)';
  672. comment on column ai_skill.name is '技能标识(唯一,即 AgentScope 的 skillId)';
  673. comment on column ai_skill.description is '技能用途说明(供模型判断是否加载)';
  674. comment on column ai_skill.skill_content is 'SKILL.md 全文(YAML frontmatter + Markdown 正文)';
  675. comment on column ai_skill.source is '技能来源(本项目维护的固定为 bizbuddy)';
  676. comment on column ai_skill.metadata_json is '技能元数据 JSON(框架读取用,可为空)';
  677. comment on column ai_skill.status is '状态(0启用 1停用)';
  678. create unique index if not exists uk_ai_skill_name on ai_skill (name);
  679. create table if not exists ai_skill_resource (
  680. id bigint not null,
  681. resource_path varchar(500) not null,
  682. resource_content text not null,
  683. created_at timestamp,
  684. updated_at timestamp,
  685. constraint pk_ai_skill_resource primary key (id, resource_path),
  686. constraint fk_ai_skill_resource_skill FOREIGN KEY (id) REFERENCES ai_skill(id) ON DELETE CASCADE
  687. );
  688. comment on table ai_skill_resource is 'AI 技能参考资料表(id 即所属技能主键)';
  689. comment on column ai_skill_resource.resource_path is '相对路径(如 references/faq.md)';
  690. comment on column ai_skill_resource.resource_content is '文件内容';
  691. create table if not exists ai_agent_skill (
  692. id bigint not null,
  693. agent_id bigint not null,
  694. skill_id bigint not null,
  695. remark varchar(500),
  696. create_by bigint,
  697. create_time timestamp,
  698. update_by bigint,
  699. update_time timestamp,
  700. constraint pk_ai_agent_skill primary key (id)
  701. );
  702. comment on table ai_agent_skill is 'AI 智能体与技能关联表';
  703. comment on column ai_agent_skill.skill_id is '技能主键(ai_skill.id)';
  704. create unique index if not exists uk_ai_agent_skill on ai_agent_skill (agent_id, skill_id);
  705. -- ============================================================
  706. -- 治理与观测
  707. -- ============================================================
  708. create table if not exists ai_usage_stat (
  709. id bigint not null,
  710. stat_date date not null,
  711. agent_id bigint not null,
  712. model_id bigint,
  713. user_id bigint not null,
  714. usage_type char(1) not null default '0',
  715. prompt_tokens bigint default 0,
  716. completion_tokens bigint default 0,
  717. total_tokens bigint default 0,
  718. request_count integer default 0,
  719. tool_call_count integer default 0,
  720. cost numeric(14, 6) default 0,
  721. create_time timestamp,
  722. update_time timestamp,
  723. constraint pk_ai_usage_stat primary key (id)
  724. );
  725. comment on table ai_usage_stat is 'AI 用量统计表(按日期 / 智能体 / 模型 / 用户 / 用量类型聚合)';
  726. comment on column ai_usage_stat.stat_date is '统计日期';
  727. comment on column ai_usage_stat.agent_id is '智能体主键;知识库入库等无智能体归属的平台调用固定为 0';
  728. comment on column ai_usage_stat.usage_type is '用量类型(0对话 1知识库入库)';
  729. comment on column ai_usage_stat.request_count is '请求(对话轮次)数;知识库入库固定为 0';
  730. comment on column ai_usage_stat.tool_call_count is '工具调用次数';
  731. comment on column ai_usage_stat.cost is '费用估算(元;按模型单价计算,未配置单价按 0 计)';
  732. create index if not exists idx_ai_usage_user on ai_usage_stat (user_id, stat_date DESC);
  733. create unique index if not exists uk_ai_usage on ai_usage_stat (stat_date, agent_id, model_id, user_id, usage_type);
  734. create table if not exists ai_permission_decision_log (
  735. id bigint not null,
  736. session_id bigint,
  737. message_id bigint,
  738. agent_id bigint,
  739. user_id bigint,
  740. tool_code varchar(100),
  741. tool_name varchar(100),
  742. decision char(1),
  743. decision_source varchar(32),
  744. matched_rule varchar(64),
  745. reason varchar(500),
  746. tool_input text,
  747. confirmed char(1),
  748. confirm_source varchar(16),
  749. duration_ms bigint,
  750. create_time timestamp,
  751. constraint pk_ai_permission_decision_log primary key (id)
  752. );
  753. comment on table ai_permission_decision_log is 'AI 工具权限决策日志(ALLOW / ASK / DENY 全量留痕)';
  754. comment on column ai_permission_decision_log.decision is '决策(0允许 1需确认 2禁止)';
  755. comment on column ai_permission_decision_log.decision_source is '决策来源(agent_tool 显式策略 / remember 记住规则 / global_mode 全局模式 / fallback 兜底)';
  756. comment on column ai_permission_decision_log.confirmed is '人工确认结果(0拒绝 1确认;非 ASK 决策为空)';
  757. comment on column ai_permission_decision_log.confirm_source is '确认作用范围(once 仅本次 / remember 记住该工具)';
  758. create index if not exists idx_ai_perm_log_session on ai_permission_decision_log (session_id, message_id);
  759. create index if not exists idx_ai_perm_log_tool on ai_permission_decision_log (tool_code, decision);
  760. create index if not exists idx_ai_perm_log_user on ai_permission_decision_log (user_id, create_time DESC);
  761. create index if not exists idx_ai_perm_log_create_time on ai_permission_decision_log (create_time);
  762. create table if not exists ai_trace (
  763. id bigint not null,
  764. session_id bigint not null,
  765. message_id bigint,
  766. agent_id bigint not null,
  767. source varchar(8) default 'chat',
  768. user_id bigint,
  769. model_id bigint,
  770. model_code varchar(200),
  771. model_provider varchar(64),
  772. summary_model_code varchar(200),
  773. memory_model_code varchar(200),
  774. context_json text,
  775. transitions_json text,
  776. status varchar(16) default 'running',
  777. duration_ms bigint,
  778. tool_call_count integer,
  779. prompt_tokens integer,
  780. completion_tokens integer,
  781. total_tokens integer,
  782. error_msg varchar(1000),
  783. create_time timestamp,
  784. update_time timestamp,
  785. parent_trace_id bigint,
  786. constraint pk_ai_trace primary key (id)
  787. );
  788. comment on table ai_trace is 'AI 链路追踪表(一次任务一条:执行期模型版本 + 上下文清单 + 状态迁移)';
  789. comment on column ai_trace.session_id is '会话主键(ai_session.id)';
  790. comment on column ai_trace.message_id is '助手消息主键(暂停阶段为待确认消息,恢复段更新同一条)';
  791. comment on column ai_trace.agent_id is '智能体主键(ai_agent.id)';
  792. comment on column ai_trace.source is '任务来源:chat 实时对话 / task 定时任务 / channel 渠道接入';
  793. comment on column ai_trace.user_id is '触发用户';
  794. comment on column ai_trace.model_id is '执行时使用的主对话模型主键(ai_model_config.id)';
  795. comment on column ai_trace.model_code is '执行时主对话模型标识(快照,事后改配置不改写历史)';
  796. comment on column ai_trace.model_provider is '执行时主对话模型提供商';
  797. comment on column ai_trace.summary_model_code is '执行时压缩摘要模型标识(未配置为空)';
  798. comment on column ai_trace.memory_model_code is '执行时记忆抽取与合并模型标识(未配置为空)';
  799. comment on column ai_trace.context_json is '上下文清单 JSON(工具白名单 / 知识库 / 技能 / 子智能体 / MCP / 各运行期开关)';
  800. comment on column ai_trace.transitions_json is '状态迁移 JSON 数组:[{at, state, detail}]';
  801. comment on column ai_trace.status is '链路状态:running / pending_confirm / success / failed / aborted';
  802. comment on column ai_trace.duration_ms is '总耗时(毫秒,含恢复段则累加)';
  803. comment on column ai_trace.tool_call_count is '工具调用次数';
  804. comment on column ai_trace.parent_trace_id is '父链路(编排链路为父、每次专家调用为子;null 表示顶层)';
  805. create index if not exists idx_ai_trace_agent on ai_trace (agent_id, create_time DESC);
  806. create index if not exists idx_ai_trace_message on ai_trace (message_id);
  807. create index if not exists idx_ai_trace_parent on ai_trace (parent_trace_id);
  808. create index if not exists idx_ai_trace_session on ai_trace (session_id);
  809. create index if not exists idx_ai_trace_create_time on ai_trace (create_time);
  810. -- ============================================================
  811. -- 场景包
  812. -- ============================================================
  813. create table if not exists ai_scenario_pack (
  814. is_builtin char(1) default '0',
  815. owner_dept_id bigint,
  816. visible_scope char(1) default '0',
  817. id bigint not null,
  818. pack_code varchar(64) not null,
  819. pack_name varchar(100) not null,
  820. description varchar(500),
  821. status char(1) not null default '0',
  822. remark varchar(500),
  823. create_by bigint,
  824. create_time timestamp,
  825. update_by bigint,
  826. update_time timestamp,
  827. constraint pk_ai_scenario_pack primary key (id)
  828. );
  829. comment on table ai_scenario_pack is 'AI 场景包定义表(按角色 / 部门打包业务工具 + MCP 工具 + 技能,对话时选用以增强能力)';
  830. comment on column ai_scenario_pack.owner_dept_id is '归属部门(生效范围=本部门时生效)';
  831. comment on column ai_scenario_pack.visible_scope is '生效范围(0公开 1本部门 2仅本人)';
  832. comment on column ai_scenario_pack.pack_code is '场景包编码(唯一,创建后不可修改)';
  833. comment on column ai_scenario_pack.pack_name is '场景包名称(如「财务场景包」「人力场景包」)';
  834. comment on column ai_scenario_pack.description is '用途说明(创建任务时供用户判断该选哪个包)';
  835. comment on column ai_scenario_pack.status is '状态(0 启用 1 停用)';
  836. create unique index if not exists uk_ai_scenario_pack_code on ai_scenario_pack (pack_code);
  837. create table if not exists ai_scenario_pack_tool (
  838. id bigint not null,
  839. pack_id bigint not null,
  840. tool_code varchar(64) not null,
  841. decision char(1) not null default '1',
  842. remark varchar(500),
  843. create_by bigint,
  844. create_time timestamp,
  845. update_by bigint,
  846. update_time timestamp,
  847. constraint pk_ai_scenario_pack_tool primary key (id)
  848. );
  849. comment on table ai_scenario_pack_tool is '场景包 × 业务工具(含授权决策,叠加到智能体既有绑定之上)';
  850. comment on column ai_scenario_pack_tool.tool_code is '业务工具编码(ai_tool.tool_code)';
  851. comment on column ai_scenario_pack_tool.decision is '授权决策(0 允许 / 1 需确认 / 2 禁止),同一工具以包内决策优先';
  852. create unique index if not exists uk_ai_scenario_pack_tool on ai_scenario_pack_tool (pack_id, tool_code);
  853. create table if not exists ai_scenario_pack_mcp_tool (
  854. id bigint not null,
  855. pack_id bigint not null,
  856. mcp_client_id bigint not null,
  857. tool_name varchar(200) not null,
  858. decision char(1) not null default '1',
  859. remark varchar(500),
  860. create_by bigint,
  861. create_time timestamp,
  862. update_by bigint,
  863. update_time timestamp,
  864. constraint pk_ai_scenario_pack_mcp_tool primary key (id)
  865. );
  866. comment on table ai_scenario_pack_mcp_tool is '场景包 × 外部 MCP 工具(仅装配包内勾选项;未选包的会话不接入 MCP)';
  867. comment on column ai_scenario_pack_mcp_tool.mcp_client_id is 'MCP 连接主键(ai_mcp_client.id)';
  868. comment on column ai_scenario_pack_mcp_tool.tool_name is '远端自声明的工具名(即 ai_mcp_client.enable_tools 的白名单口径;注册进工具箱时会被归一化重命名)';
  869. comment on column ai_scenario_pack_mcp_tool.decision is '授权决策(0 允许 / 1 需确认 / 2 禁止)';
  870. create unique index if not exists uk_ai_scenario_pack_mcp_tool on ai_scenario_pack_mcp_tool (pack_id, mcp_client_id, tool_name);
  871. create table if not exists ai_scenario_pack_skill (
  872. id bigint not null,
  873. pack_id bigint not null,
  874. skill_id bigint not null,
  875. remark varchar(500),
  876. create_by bigint,
  877. create_time timestamp,
  878. update_by bigint,
  879. update_time timestamp,
  880. constraint pk_ai_scenario_pack_skill primary key (id)
  881. );
  882. comment on table ai_scenario_pack_skill is '场景包 × 技能(叠加到智能体既有技能绑定之上,仍受技能生效范围过滤)';
  883. comment on column ai_scenario_pack_skill.skill_id is '技能主键(ai_skill.id)';
  884. create unique index if not exists uk_ai_scenario_pack_skill on ai_scenario_pack_skill (pack_id, skill_id);
  885. -- ============================================================
  886. -- 安全合规
  887. -- ============================================================
  888. create table if not exists ai_sensitive_word (
  889. id bigint not null,
  890. word varchar(200) not null,
  891. is_regex char(1) default '0',
  892. category varchar(50),
  893. enabled char(1) default '1',
  894. remark varchar(255),
  895. create_by bigint,
  896. create_time timestamp,
  897. update_by bigint,
  898. update_time timestamp,
  899. constraint pk_ai_sensitive_word primary key (id)
  900. );
  901. comment on table ai_sensitive_word is 'AI 敏感词库(支持普通词与正则,供输入 / 输出安全校验)';
  902. comment on column ai_sensitive_word.word is '敏感词或正则表达式';
  903. comment on column ai_sensitive_word.is_regex is '是否正则:0否(子串匹配) 1是';
  904. comment on column ai_sensitive_word.category is '分类(可空,供分组管理与统计)';
  905. comment on column ai_sensitive_word.enabled is '是否启用:1启用 0禁用';
  906. create table if not exists ai_safety_event (
  907. id bigint not null,
  908. scene varchar(20),
  909. action varchar(20),
  910. engine varchar(20),
  911. session_id bigint,
  912. message_id bigint,
  913. agent_id bigint,
  914. user_id bigint,
  915. hit_count integer,
  916. hits_json text,
  917. text_excerpt varchar(255),
  918. create_time timestamp,
  919. constraint pk_ai_safety_event primary key (id)
  920. );
  921. comment on table ai_safety_event is 'AI 安全事件留痕(敏感词 / PII 命中与处置,追加型明细)';
  922. comment on column ai_safety_event.scene is '场景(input 用户输入 / output 模型输出)';
  923. comment on column ai_safety_event.action is '处置动作(log 只记录 / mask 脱敏放行 / block 阻断)';
  924. comment on column ai_safety_event.engine is '命中引擎(word 敏感词 / pii 隐私信息)';
  925. comment on column ai_safety_event.hit_count is '命中项数量';
  926. comment on column ai_safety_event.hits_json is '命中项 JSON 数组(命中词 / PII 类型)';
  927. comment on column ai_safety_event.text_excerpt is '触发文本摘要(截断至 200 字符)';
  928. create index if not exists idx_ai_safety_event_create_time on ai_safety_event (create_time);
  929. create index if not exists idx_ai_safety_event_scene on ai_safety_event (scene);