"""Shared capabilities for planning, querying, repair and evidence composition. No KPI calculation or browser-success assertion belongs in this module. """ CAPABILITY_INSTRUCTIONS = ( '【平台能力与职责】正式质量指标由quality_data服务端计算和核对,模型不得自行重算或补造。' '平台根据已核实的quality_data结果及任务的visualPreference/visualType自动准备并展示图表。' '模型负责理解任务、选择查询与事实、解释和组织文字,不需要输出图片、HTML、SVG或绘图代码,' '不需要寻找绘图工具、探测图表服务或让用户另行生成图表。' '不要调用原生display_chart:它读取原生SQL查询结果,与quality_data来源不兼容;本平台图表不经过该工具。' '用户要求受支持的图表时,取得相应数据并保留绘图意图即可完成模型职责;' '不能因文字JSON通道不直接包含图片就声称无法生成图表,不能把已安排的图表写成后续建议。' '模型无法观察浏览器渲染是否成功,不得声称已看见图表或替前端宣布渲染成功。' '同轮复合目标可通过queryPlan拆成最多8个有界节点;root目标、filters和期间继续作为权威用户意图,' 'selector只说明对象来源,不得把子对象筛选回写root filters。' ) def capability_context(task=None): task = task if isinstance(task, dict) else {} plan = task.get('queryPlan') if isinstance(task.get('queryPlan'), list) else [] node_summaries = [] for node in plan[:8]: if not isinstance(node, dict): continue node_summaries.append({key: node[key] for key in ('nodeId', 'purpose', 'fields', 'stages', 'visualType', 'visualPreference', 'comparisonRequired', 'dimension', 'limit', 'sort', 'optional', 'selector') if key in node}) return { 'metrics': {'owner': 'quality_data_server', 'modelMayRecalculate': False}, 'charts': { 'owner': 'platform', 'source': 'verified_quality_data', 'supportedTypes': ['bar', 'column', 'line', 'area', 'heatmap', 'donut', 'pareto', 'waterfall'], 'preference': task.get('visualPreference', 'auto'), 'requestedType': task.get('visualType'), 'drawingToolRequired': False, 'modelCanObserveBrowserRendering': False, }, 'queryPlan': {'maxNodes': 8, 'maxScopedReads': 40, 'nodeSummary': node_summaries, 'periodOwner': 'rootTask', 'scopeOwner': 'protocolServer', 'source': 'verified_node_scope_protocol'}, } def apply_platform_tools(payload): """Do not advertise a native SQL-result renderer for quality API evidence.""" tools = payload.get('tools') if not isinstance(tools, list): return payload kept = [tool for tool in tools if not (isinstance(tool, dict) and isinstance(tool.get('function'), dict) and tool['function'].get('name') == 'display_chart')] if len(kept) == len(tools): return payload if kept: payload['tools'] = kept else: payload.pop('tools', None) choice = payload.get('tool_choice') if ((isinstance(choice, dict) and (choice.get('function') or {}).get('name') == 'display_chart') or not kept and choice in ('auto', 'required')): payload.pop('tool_choice', None) return payload def primary_query_required(task): """A source-defect TOP does not need unrelated parent KPI prefetching.""" return not (task.get('kind') == 'analysis' and set(task.get('dimensions', [])) == {'defect'} and not task.get('comparisonRequired') and not task.get('comparisonPeriod') and not set(task.get('fields', [])) - {'scrapWeight'})