Files
server-core/scripts/migrate_agent_gate.sql
T

64 lines
3.6 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- ============================================================
-- 智能体互通(agent_gate)数据库迁移
-- 适用:server-core 生产库(本机与生产共用同一 MySQL)
-- 执行方式(本机):
-- mysql -h47.108.226.213 -P8091 -uopc -pjjjsgysyujkwjwgb opc < migrate_agent_gate.sql
-- 幂等:全部语句可重复执行(先检查列/表是否已存在)
-- ============================================================
-- 1) agents 表新增:外部调用开关 + 调用确认模式
SET @col_exists := (
SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'opc' AND TABLE_NAME = 'agents' AND COLUMN_NAME = 'external_callable'
);
SET @sql := IF(@col_exists = 0,
'ALTER TABLE agents ADD COLUMN external_callable TINYINT(1) NOT NULL DEFAULT 1 COMMENT ''是否允许被企业内其他账号/智能体调用''',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @col_exists := (
SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'opc' AND TABLE_NAME = 'agents' AND COLUMN_NAME = 'call_mode'
);
SET @sql := IF(@col_exists = 0,
'ALTER TABLE agents ADD COLUMN call_mode VARCHAR(16) NOT NULL DEFAULT ''auto'' COMMENT ''auto=自动执行 / confirm=需归属用户确认''',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- 2) 智能体临时授权表(L3 成员开放调用)
CREATE TABLE IF NOT EXISTS agent_access_grants (
id VARCHAR(64) NOT NULL COMMENT 'grant_xxx',
grantor_user_id VARCHAR(36) NOT NULL COMMENT '授权人(agent 归属用户)',
target_agent_addr VARCHAR(128) NOT NULL COMMENT '目标 agent 地址 {user_id}.{agent_id}',
grantee_user_id VARCHAR(36) NOT NULL COMMENT '被授权人(可调用方)',
scope TEXT NOT NULL COMMENT 'prompt 前缀白名单(每行一条),空=全部',
max_calls INT NOT NULL DEFAULT 0 COMMENT '0=不限次数',
used_calls INT NOT NULL DEFAULT 0,
expires_at VARCHAR(32) NOT NULL DEFAULT '' COMMENT 'ISO 时间(UTC);空=永久',
status VARCHAR(16) NOT NULL DEFAULT 'active' COMMENT 'active|revoked',
created_at VARCHAR(32) NOT NULL DEFAULT '',
updated_at VARCHAR(32) NOT NULL DEFAULT '',
PRIMARY KEY (id),
KEY idx_agent_grant_grantee (grantee_user_id, status),
KEY idx_agent_grant_target (target_agent_addr, status),
KEY idx_agent_grant_grantor (grantor_user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='智能体临时开放调用授权(L3';
-- 3) im-service 智能体任务登记表(im_agent_tasks,幂等兜底;im-service 启动时也会 create_all
CREATE TABLE IF NOT EXISTS im_agent_tasks (
task_id VARCHAR(48) NOT NULL COMMENT 'agt_xxx',
addr VARCHAR(128) NOT NULL DEFAULT '' COMMENT '{user_id}.{agent_id}',
conv_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT 'agent_reply 回写会话',
caller_user_id VARCHAR(36) NOT NULL DEFAULT '',
caller_name VARCHAR(64) NOT NULL DEFAULT '',
prompt TEXT,
mode VARCHAR(16) NOT NULL DEFAULT 'auto' COMMENT 'auto|confirm',
status VARCHAR(16) NOT NULL DEFAULT 'pending' COMMENT 'pending|running|ok|error|offline|cancelled',
reply_content TEXT,
created_at VARCHAR(32) NOT NULL DEFAULT '',
updated_at VARCHAR(32) NOT NULL DEFAULT '',
PRIMARY KEY (task_id),
KEY idx_im_agent_task_conv (conv_id),
KEY idx_im_agent_task_addr (addr, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='智能体互通任务登记(im-service)';