"""Add Phase F fields to automation_agent_definitions. Adds temperature, max_tokens, max_steps, trace_mode, skill_ids, trigger_config, and ai_use_case_metadata to support the Phase F context-builder, SSE streaming, and AI-use-case features. Revision ID: 0122 Revises: 0121 """ from alembic import op import sqlalchemy as sa from sqlalchemy.dialects.postgresql import JSONB revision = "0122" down_revision = "0121" branch_labels = None depends_on = None def _table_exists(conn, table_name: str) -> bool: """True when the table exists (dual-path convergence, Gate B). On a fresh install the automation plugin SQL migration has not run yet when Alembic reaches this revision — skip instead of failing. The plugin-side convergence migration adds the same columns. """ row = conn.execute( sa.text("SELECT to_regclass(:tname) IS NOT NULL"), {"tname": f"public.{table_name}"}, ).scalar() return bool(row) def upgrade() -> None: conn = op.get_bind() if not _table_exists(conn, "automation_agent_definitions"): return op.add_column( "automation_agent_definitions", sa.Column("temperature", sa.Float, nullable=False, server_default="0.3"), ) op.add_column( "automation_agent_definitions", sa.Column("max_tokens", sa.Integer, nullable=False, server_default="1000"), ) op.add_column( "automation_agent_definitions", sa.Column("max_steps", sa.Integer, nullable=False, server_default="20"), ) op.add_column( "automation_agent_definitions", sa.Column( "trace_mode", sa.String(20), nullable=False, server_default="standard" ), ) op.add_column( "automation_agent_definitions", sa.Column("skill_ids", JSONB, nullable=False, server_default=sa.text("'[]'::jsonb")), ) op.add_column( "automation_agent_definitions", sa.Column("trigger_config", JSONB, nullable=False, server_default=sa.text("'{}'::jsonb")), ) op.add_column( "automation_agent_definitions", sa.Column( "ai_use_case_metadata", JSONB, nullable=False, server_default=sa.text("'{}'::jsonb"), ), ) def downgrade() -> None: op.drop_column("automation_agent_definitions", "ai_use_case_metadata") op.drop_column("automation_agent_definitions", "trigger_config") op.drop_column("automation_agent_definitions", "skill_ids") op.drop_column("automation_agent_definitions", "trace_mode") op.drop_column("automation_agent_definitions", "max_steps") op.drop_column("automation_agent_definitions", "max_tokens") op.drop_column("automation_agent_definitions", "temperature")