CREATE TABLE `tb_agent_artifact` (
    `id` BIGINT NOT NULL COMMENT '内部主键',
    `artifact_id` VARCHAR(64) NOT NULL COMMENT '对外稳定产物ID',
    `tenant_id` BIGINT NOT NULL COMMENT '租户ID',
    `agent_id` BIGINT NOT NULL COMMENT 'Agent ID',
    `owner_user_id` BIGINT NOT NULL COMMENT '创建及下载归属用户',
    `chat_mode` VARCHAR(16) NOT NULL COMMENT 'DRAFT或FORMAL',
    `chat_session_id` BIGINT NULL COMMENT '正式聊天会话ID',
    `runtime_session_id` VARCHAR(128) NOT NULL COMMENT 'Runtime会话标识',
    `request_id` VARCHAR(128) NOT NULL COMMENT '运行请求标识',
    `round_id` BIGINT NULL COMMENT '正式聊天轮次ID',
    `variant_index` INT NULL COMMENT '正式聊天答案版本序号',
    `tool_call_id` VARCHAR(128) NOT NULL COMMENT 'Artifact工具调用ID',
    `file_name` VARCHAR(255) NOT NULL COMMENT '安全展示文件名',
    `mime_type` VARCHAR(128) NOT NULL COMMENT '服务端识别MIME',
    `size_bytes` BIGINT NOT NULL COMMENT '实际字节数',
    `sha256` CHAR(64) NULL COMMENT '内容SHA-256',
    `storage_platform` VARCHAR(64) NOT NULL COMMENT '内部存储平台',
    `object_key` VARCHAR(1024) NOT NULL COMMENT '内部对象定位',
    `storage_etag` VARCHAR(255) NULL COMMENT '对象ETag',
    `status` VARCHAR(32) NOT NULL COMMENT '产物状态',
    `expires_at` DATETIME NULL COMMENT '草稿产物过期时间',
    `retry_count` INT NOT NULL DEFAULT 0 COMMENT '清理重试次数',
    `next_retry_at` DATETIME NULL COMMENT '下次补偿时间',
    `last_error_code` VARCHAR(64) 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 NOT NULL DEFAULT 0 COMMENT '逻辑删除标记',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_agent_artifact_tenant_public_id` (`tenant_id`, `artifact_id`),
    KEY `idx_agent_artifact_session_status` (`tenant_id`, `chat_session_id`, `status`, `id`),
    KEY `idx_agent_artifact_draft_owner` (`tenant_id`, `owner_user_id`, `runtime_session_id`, `status`, `id`),
    KEY `idx_agent_artifact_cleanup` (`status`, `expires_at`, `next_retry_at`, `id`),
    KEY `idx_agent_artifact_retry` (`status`, `next_retry_at`, `id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Agent产物状态账本';
