CREATE TABLE IF NOT EXISTS `tb_agent_session` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NULL COMMENT '租户ID',
    `agent_id` BIGINT NULL COMMENT 'Agent ID',
    `chat_session_id` BIGINT NULL COMMENT '聊天会话ID',
    `runtime_session_id` VARCHAR(128) NOT NULL COMMENT '运行时会话ID',
    `session_key` VARCHAR(255) NOT NULL COMMENT 'AgentScope session key',
    `state_json` JSON NULL COMMENT 'AgentScope session state envelope',
    `version` BIGINT NOT NULL DEFAULT 0 COMMENT '版本号',
    `cache_version` BIGINT NOT NULL DEFAULT 0 COMMENT '缓存版本号',
    `last_access_at` DATETIME NULL COMMENT '最后访问时间',
    `expires_at` DATETIME NULL COMMENT '过期时间',
    `created` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `created_by` BIGINT NULL COMMENT '创建人',
    `modified` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    `modified_by` BIGINT NULL COMMENT '修改人',
    `is_deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_agent_session_key` (`agent_id`, `session_key`),
    KEY `idx_agent_session_chat` (`chat_session_id`),
    KEY `idx_agent_session_runtime` (`runtime_session_id`),
    KEY `idx_agent_session_modified` (`modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AgentScope 会话状态';

CREATE TABLE IF NOT EXISTS `tb_agent_hitl_pending` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NULL COMMENT '租户ID',
    `agent_id` BIGINT NULL COMMENT 'Agent ID',
    `chat_session_id` BIGINT NULL COMMENT '聊天会话ID',
    `runtime_session_id` VARCHAR(128) NULL COMMENT '运行时会话ID',
    `request_id` VARCHAR(64) NULL COMMENT '运行请求ID',
    `resume_token` VARCHAR(128) NOT NULL COMMENT '恢复令牌',
    `tool_call_id` VARCHAR(128) NULL COMMENT '工具调用ID',
    `tool_name` VARCHAR(128) NULL COMMENT '工具名称',
    `tool_input_json` JSON NULL COMMENT '工具入参',
    `status` VARCHAR(32) NOT NULL DEFAULT 'PENDING' COMMENT '状态',
    `reject_reason` VARCHAR(1024) NULL COMMENT '拒绝原因',
    `expires_at` DATETIME NULL COMMENT '过期时间',
    `consumed_at` DATETIME NULL COMMENT '消费时间',
    `metadata_json` JSON NULL COMMENT '元数据',
    `created` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `created_by` BIGINT NULL COMMENT '创建人',
    `modified` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    `modified_by` BIGINT NULL COMMENT '修改人',
    `is_deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_agent_hitl_resume_token` (`resume_token`),
    KEY `idx_agent_hitl_session_status` (`chat_session_id`, `status`, `expires_at`),
    KEY `idx_agent_hitl_request` (`request_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Agent 工具审批挂起态';

CREATE TABLE IF NOT EXISTS `tb_agent_run_event` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NULL COMMENT '租户ID',
    `agent_id` BIGINT NULL COMMENT 'Agent ID',
    `chat_session_id` BIGINT NULL COMMENT '聊天会话ID',
    `round_id` BIGINT NULL COMMENT '聊天轮次ID',
    `round_no` INT NULL COMMENT '聊天轮次序号',
    `variant_index` INT NOT NULL DEFAULT 1 COMMENT '答案版本序号',
    `request_id` VARCHAR(64) NULL COMMENT '运行请求ID',
    `event_id` VARCHAR(64) NULL COMMENT '运行事件ID',
    `event_type` VARCHAR(64) NOT NULL COMMENT '运行事件类型',
    `event_phase` VARCHAR(64) NULL COMMENT '运行事件阶段',
    `tool_call_id` VARCHAR(128) NULL COMMENT '工具调用ID',
    `payload_json` JSON NULL COMMENT '事件载荷',
    `metadata_json` JSON NULL COMMENT '事件元数据',
    `created` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `created_by` BIGINT NULL COMMENT '创建人',
    PRIMARY KEY (`id`),
    KEY `idx_agent_event_session_round` (`chat_session_id`, `round_id`, `created`, `id`),
    KEY `idx_agent_event_request` (`request_id`, `created`, `id`),
    KEY `idx_agent_event_type` (`event_type`, `created`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Agent 运行事件摘要';
