-- Agent 会话与消息表。

CREATE TABLE IF NOT EXISTS agent_session (
    id BIGSERIAL PRIMARY KEY,
    session_code VARCHAR(100) NOT NULL,
    agent_id BIGINT NOT NULL,
    workflow_run_id BIGINT,
    title VARCHAR(200),
    status VARCHAR(50) NOT NULL DEFAULT 'ACTIVE',
    metadata_json JSONB NOT NULL DEFAULT '{}'::jsonb,
    version INTEGER NOT NULL DEFAULT 1,
    create_time TIMESTAMP,
    update_time TIMESTAMP,
    remark VARCHAR(500) DEFAULT '',
    create_by VARCHAR(64),
    update_by VARCHAR(64),
    CONSTRAINT uk_agent_session_code UNIQUE (session_code),
    CONSTRAINT fk_agent_session_agent_id FOREIGN KEY (agent_id) REFERENCES agent_definition (id),
    CONSTRAINT fk_agent_session_run_id FOREIGN KEY (workflow_run_id) REFERENCES workflow_run (id)
);

CREATE TABLE IF NOT EXISTS agent_message (
    id BIGSERIAL PRIMARY KEY,
    session_id BIGINT NOT NULL,
    role VARCHAR(50) NOT NULL,
    content TEXT NOT NULL,
    citation_json JSONB NOT NULL DEFAULT '[]'::jsonb,
    token_count INTEGER,
    version INTEGER NOT NULL DEFAULT 1,
    create_time TIMESTAMP,
    update_time TIMESTAMP,
    remark VARCHAR(500) DEFAULT '',
    create_by VARCHAR(64),
    update_by VARCHAR(64),
    CONSTRAINT fk_agent_message_session_id FOREIGN KEY (session_id) REFERENCES agent_session (id)
);

CREATE INDEX IF NOT EXISTS idx_agent_session_agent_id ON agent_session (agent_id);
CREATE INDEX IF NOT EXISTS idx_agent_session_status ON agent_session (status);
CREATE INDEX IF NOT EXISTS idx_agent_message_session_id ON agent_message (session_id);
CREATE INDEX IF NOT EXISTS idx_agent_message_role ON agent_message (role);

COMMENT ON TABLE agent_session IS 'Agent会话表';
COMMENT ON COLUMN agent_session.id IS 'ID';
COMMENT ON COLUMN agent_session.session_code IS '会话编码';
COMMENT ON COLUMN agent_session.agent_id IS 'Agent ID';
COMMENT ON COLUMN agent_session.workflow_run_id IS '来源Workflow运行ID';
COMMENT ON COLUMN agent_session.title IS '会话标题';
COMMENT ON COLUMN agent_session.status IS '会话状态';
COMMENT ON COLUMN agent_session.metadata_json IS '会话元数据JSON';
COMMENT ON COLUMN agent_session.version IS '版本';
COMMENT ON COLUMN agent_session.create_time IS '创建时间';
COMMENT ON COLUMN agent_session.update_time IS '更新时间';
COMMENT ON COLUMN agent_session.remark IS '备注';
COMMENT ON COLUMN agent_session.create_by IS '创建者';
COMMENT ON COLUMN agent_session.update_by IS '更新者';
COMMENT ON TABLE agent_message IS 'Agent消息表';
COMMENT ON COLUMN agent_message.id IS 'ID';
COMMENT ON COLUMN agent_message.session_id IS '会话ID';
COMMENT ON COLUMN agent_message.role IS '消息角色';
COMMENT ON COLUMN agent_message.content IS '消息内容';
COMMENT ON COLUMN agent_message.citation_json IS '引用信息JSON';
COMMENT ON COLUMN agent_message.token_count IS 'Token数量';
COMMENT ON COLUMN agent_message.version IS '版本';
COMMENT ON COLUMN agent_message.create_time IS '创建时间';
COMMENT ON COLUMN agent_message.update_time IS '更新时间';
COMMENT ON COLUMN agent_message.remark IS '备注';
COMMENT ON COLUMN agent_message.create_by IS '创建者';
COMMENT ON COLUMN agent_message.update_by IS '更新者';
