DO $$
BEGIN
    IF to_regclass('agent_model_invocations') IS NULL THEN
        CREATE VIEW agent_model_invocations AS
        SELECT
            id,
            session,
            conversation,
            message,
            model_version,
            request_id,
            status,
            prompt,
            response,
            error,
            started_at,
            finished_at,
            latency_ms
        FROM agent_model_invocation;
    END IF;
END $$;
