ai_orchestration.sql 55 KB

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