""" backend/app/cli/faq_commands.py 用途:FAQ 相关 Flask CLI 命令注册。 依赖:HotFaqAggregator、Flask click。 逻辑:create_app 时 init_app 注册 faq-aggregate-hot 命令。 """ import click from flask.cli import with_appcontext from app.services.hot_faq_aggregator import HotFaqAggregator @click.command('faq-aggregate-hot') @click.option('--agent-id', type=int, default=None, help='仅聚合指定 Agent') @click.option('--dry-run', is_flag=True, help='只统计不写 PostgreSQL') @with_appcontext def faq_aggregate_hot(agent_id: int | None, dry_run: bool): """ 离线聚合 ChatLog 热点 FAQ 候选并写入 faq_hot_candidates 表。 核心逻辑:委托 HotFaqAggregator.aggregate_hot_faq_candidates。 """ result = HotFaqAggregator.aggregate_hot_faq_candidates(agent_id=agent_id, dry_run=dry_run) if not result.get('enabled'): click.echo(result.get('message', 'FAQ hot aggregation disabled')) return click.echo( f"scanned={result['scanned_logs']} groups={result['group_count']} " f"promoted={result['promoted_count']} skipped={result['skipped_count']} dry_run={dry_run}" ) for item in result.get('candidates', [])[:20]: click.echo( f" [{item.get('agent_name')}] {item.get('canonical_query')} " f"asks={item.get('ask_count_7d')} users={item.get('user_count_7d')} " f"dominance={item.get('answer_dominance')}" ) if len(result.get('candidates', [])) > 20: click.echo(f" ... and {len(result['candidates']) - 20} more") def init_app(app): """向 Flask app 注册 FAQ CLI 命令。""" app.cli.add_command(faq_aggregate_hot)