-- 智能体审计日志表
-- 记录所有工具调用的详细信息：工具名称、执行状态、耗时、输出预览
--
-- status: OK (成功) / FAIL (失败) / SESSION_STOP (会话终止)

CREATE TABLE IF NOT EXISTS agent_audit_log (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id TEXT NOT NULL,
    step INTEGER NOT NULL DEFAULT 0,
    tool_name TEXT,
    status TEXT NOT NULL CHECK(status IN ('OK', 'FAIL', 'SESSION_STOP')),
    elapsed_ms INTEGER NOT NULL DEFAULT 0,
    output_preview TEXT,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (session_id) REFERENCES agent_sessions(session_id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_audit_log_session ON agent_audit_log(session_id);
CREATE INDEX IF NOT EXISTS idx_audit_log_created ON agent_audit_log(created_at);
