CREATE TABLE IF NOT EXISTS ai_tool_call (
    tool_call_id VARCHAR(255) NOT NULL,
    session UUID NOT NULL,
    tool_name VARCHAR(255) NOT NULL,
    caller UUID NOT NULL,
    arguments JSONB NOT NULL,
    result JSONB NOT NULL,
    status VARCHAR(255) NOT NULL,
    execution_time_ms BIGINT,
    error_message TEXT,
    error_stack TEXT,
    retry_count INTEGER NOT NULL DEFAULT 0,
    created_at TIMESTAMPTZ NOT NULL,
    completed_at TIMESTAMPTZ,
    updated_at TIMESTAMPTZ NOT NULL,
    PRIMARY KEY (tool_call_id, session)
);

CREATE INDEX idx_ai_tool_call_session ON ai_tool_call (session);
CREATE INDEX idx_ai_tool_call_status ON ai_tool_call (status);
