Merge branch 'dev' into hugo

This commit is contained in:
hugo committed 2026-06-17 22:29:48 +08:00
commit 97a5a1f106
35 files changed
+3749 -259

No files matched your search

@@ -0,0 +1,69 @@
"""extend quick_questions for FAQ cache
Revision ID: a1b2c3d4e5f7
Revises: f2a3b4c5d6e8
Create Date: 2026-06-16 10:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
revision = "a1b2c3d4e5f7"
down_revision = "f2a3b4c5d6e8"
branch_labels = None
depends_on = None
def upgrade():
bind = op.get_bind()
insp = sa.inspect(bind)
columns = {c["name"] for c in insp.get_columns("quick_questions")}
if "answer" not in columns:
op.add_column("quick_questions", sa.Column("answer", sa.Text(), nullable=True))
if "aliases" not in columns:
op.add_column("quick_questions", sa.Column("aliases", sa.JSON(), nullable=True))
if "cache_enabled" not in columns:
op.add_column(
"quick_questions",
sa.Column("cache_enabled", sa.Boolean(), nullable=False, server_default=sa.false()),
)
if "is_enabled" not in columns:
op.add_column(
"quick_questions",
sa.Column("is_enabled", sa.Boolean(), nullable=False, server_default=sa.true()),
)
if "hit_count" not in columns:
op.add_column(
"quick_questions",
sa.Column("hit_count", sa.Integer(), nullable=False, server_default="0"),
)
if "retriever_resources" not in columns:
op.add_column("quick_questions", sa.Column("retriever_resources", sa.JSON(), nullable=True))
if "expires_at" not in columns:
op.add_column("quick_questions", sa.Column("expires_at", sa.DateTime(), nullable=True))
if "updated_at" not in columns:
op.add_column(
"quick_questions",
sa.Column("updated_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
)
def downgrade():
bind = op.get_bind()
insp = sa.inspect(bind)
columns = {c["name"] for c in insp.get_columns("quick_questions")}
for col in (
"updated_at",
"expires_at",
"retriever_resources",
"hit_count",
"is_enabled",
"cache_enabled",
"aliases",
"answer",
):
if col in columns:
op.drop_column("quick_questions", col)
@@ -0,0 +1,63 @@
"""add faq_hot_candidates table
Revision ID: b2c3d4e5f6a8
Revises: a1b2c3d4e5f7
Create Date: 2026-06-16 14:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
revision = "b2c3d4e5f6a8"
down_revision = "a1b2c3d4e5f7"
branch_labels = None
depends_on = None
def upgrade():
bind = op.get_bind()
insp = sa.inspect(bind)
if "faq_hot_candidates" in insp.get_table_names():
return
op.create_table(
"faq_hot_candidates",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("agent_id", sa.Integer(), nullable=False),
sa.Column("q_norm", sa.String(length=255), nullable=False),
sa.Column("canonical_query", sa.Text(), nullable=False),
sa.Column("answer", sa.Text(), nullable=False),
sa.Column("source_chat_log_id", sa.Integer(), nullable=True),
sa.Column("ask_count_7d", sa.Integer(), nullable=False, server_default="0"),
sa.Column("user_count_7d", sa.Integer(), nullable=False, server_default="0"),
sa.Column("avg_tokens", sa.Integer(), nullable=False, server_default="0"),
sa.Column("avg_latency_ms", sa.Integer(), nullable=False, server_default="0"),
sa.Column("answer_dominance", sa.Float(), nullable=False, server_default="0"),
sa.Column("sample_queries", sa.JSON(), nullable=True),
sa.Column("retriever_resources", sa.JSON(), nullable=True),
sa.Column("last_asked_at", sa.DateTime(), nullable=True),
sa.Column("status", sa.String(length=20), nullable=False, server_default="candidate"),
sa.Column("promoted_quick_question_id", sa.Integer(), nullable=True),
sa.Column("deny_until", sa.DateTime(), nullable=True),
sa.Column("expires_at", sa.DateTime(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
sa.ForeignKeyConstraint(["agent_id"], ["agents.id"]),
sa.ForeignKeyConstraint(["source_chat_log_id"], ["chat_logs.id"]),
sa.ForeignKeyConstraint(["promoted_quick_question_id"], ["quick_questions.id"]),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("agent_id", "q_norm", name="uq_faq_hot_candidates_agent_qnorm"),
)
op.create_index("ix_faq_hot_candidates_agent_id", "faq_hot_candidates", ["agent_id"])
op.create_index("ix_faq_hot_candidates_status_expires", "faq_hot_candidates", ["status", "expires_at"])
def downgrade():
bind = op.get_bind()
insp = sa.inspect(bind)
if "faq_hot_candidates" not in insp.get_table_names():
return
op.drop_index("ix_faq_hot_candidates_status_expires", table_name="faq_hot_candidates")
op.drop_index("ix_faq_hot_candidates_agent_id", table_name="faq_hot_candidates")
op.drop_table("faq_hot_candidates")