diff --git a/src/app/api/chat/route.ts b/src/app/api/chat/route.ts index 224588c..001714d 100644 --- a/src/app/api/chat/route.ts +++ b/src/app/api/chat/route.ts @@ -92,7 +92,15 @@ const DEFAULT_SYSTEM_PROMPT = `你是一个专业、友好的 AI 助手。请遵 ## 特别注意 - 如果问题不明确,先确认理解是否正确 - 如果有多种方案,说明各自的优缺点 -- 承认不确定的地方,不要编造信息`; +- 承认不确定的地方,不要编造信息 + +## 图表绘制规范(重要) +当使用 code_execution 工具绘制图表时,请遵循以下规则: +1. **只生成一段代码**:一个请求只调用一次 code_execution 工具绘制图表,不要生成多段重复的绘图代码 +2. **代码完整性**:确保一段代码中包含所有需要展示的图表内容 +3. **使用默认样式**:系统已默认配置 seaborn-whitegrid 风格,无需在代码中再次设置 plt.style.use() +4. **中文支持**:系统已配置中文字体(Noto Sans SC),可以直接使用中文标签和标题 +5. **不要重复绘图**:即使是展示不同数据,也应该在一张图表中使用子图(subplot)展示,而不是分开多次绘制`; // POST /api/chat - 发送消息并获取 AI 回复 export async function POST(request: Request) { @@ -393,6 +401,26 @@ export async function POST(request: Request) { // 执行工具 const result = await executeTool(tc.name, tc.input); + // 检查是否需要浏览器端 Pyodide 执行 + if (result.requiresPyodide) { + // 发送 Pyodide 执行请求事件 + controller.enqueue(encoder.encode(`data: ${JSON.stringify({ + type: 'pyodide_execution_required', + id: tc.id, + name: tc.name, + code: result.code, + language: result.language, + })}\n\n`)); + + // 将占位工具结果发送给 AI(稍后会被前端执行结果替换) + toolResults.push({ + type: 'tool_result', + tool_use_id: tc.id, + content: '代码正在浏览器端执行中,请稍候...', + }); + continue; + } + // 发送工具执行结果事件(使用简短版本) controller.enqueue(encoder.encode(`data: ${JSON.stringify({ type: 'tool_execution_result', @@ -400,6 +428,7 @@ export async function POST(request: Request) { name: tc.name, success: result.success, result: result.displayResult, + images: result.images, })}\n\n`)); // 将简短的工具结果显示给用户