From d86220314064f65a4c767ff8f00058dd067cae88 Mon Sep 17 00:00:00 2001 From: ddshi <8811906+ddshi@user.noreply.gitee.com> Date: Sat, 28 Feb 2026 11:17:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96AI=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E5=92=8C=E6=B7=BB=E5=8A=A0=E8=B0=83=E8=AF=95=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 简化SYSTEM_PROMPT,使AI更容易理解并返回正确的JSON格式 - 添加详细日志帮助调试AI解析问题 - 改进错误处理 Co-Authored-By: Claude Opus 4.6 --- src/routes/ai.ts | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/routes/ai.ts b/src/routes/ai.ts index 563f54b..34d8317 100644 --- a/src/routes/ai.ts +++ b/src/routes/ai.ts @@ -11,22 +11,25 @@ const SYSTEM_PROMPT = `你是一个帮助用户创建事件(纪念日或提醒 当前时间:${new Date().toISOString()} -任务:从自然语言中解析用户输入,并严格按照以下JSON格式返回: +任务:从自然语言中解析用户输入,并直接返回以下JSON格式(不要有其他文字解释): -{ - "parsed": { - "type": "anniversary 或 reminder", - "title": "事件标题(简洁准确,去除"提醒"、"帮我"等前缀)", - "content": "详细内容(仅提醒类型,如会议主题、待办事项等)", - "date": "2026-02-13T15:00:00Z", - "timezone": "Asia/Shanghai", - "is_lunar": true 或 false, - "repeat_type": "daily, weekly, monthly, yearly 或 none", - "priority": "none, red, green 或 yellow(仅提醒,red=重要紧急,green=绿色,yellow=黄色)", - "reminder_times": ["提前提醒时间点数组,如 2026-02-12T09:00:00Z"](仅提醒) - }, - "response": "友好的确认消息" -} +{"type": "anniversary", "title": "事件标题", "content": "详细内容", "date": "2026-02-13T09:00:00Z", "timezone": "Asia/Shanghai", "is_lunar": false, "repeat_type": "yearly", "priority": "none", "reminder_times": []} + +重要字段说明: +- type: "anniversary"(纪念日) 或 "reminder"(提醒) +- title: 简洁的事件标题,去除"提醒"、"帮我"等前缀 +- content: 仅提醒类型需要,事件的详细内容 +- date: ISO 8601 格式的日期时间 +- timezone: "Asia/Shanghai" +- is_lunar: 是否农历日期 +- repeat_type: "daily"(每天), "weekly"(每周), "monthly"(每月), "yearly"(每年), "none"(不重复) +- priority: "none", "red", "green", "yellow" +- reminder_times: 提前提醒时间数组 + +标题提取示例: +- "提醒我明天上午9点开会" → title = "开会" +- "提醒我春节回家" → title = "春节回家" +- "帮我记一下妈妈的生日" → title = "妈妈的生日" ## 意图识别规则(非常重要): @@ -200,14 +203,19 @@ async function callDeepSeek(message: string): Promise<{ const data = await response.json(); const content = data.choices[0]?.message?.content || ''; + // Log the raw response for debugging + console.log('[AI] Raw response:', content); + // Extract JSON from response const jsonMatch = content.match(/\{[\s\S]*\}/); if (jsonMatch) { try { const rawParsed = JSON.parse(jsonMatch[0]); + console.log('[AI] Parsed JSON:', rawParsed); const parsed = rawParsed.parsed || rawParsed; // Validate parsed data with Zod schema const validated = parsedEventSchema.parse(parsed); + console.log('[AI] Validated:', validated); // 构建友好的用户消息 const typeText = validated.type === 'anniversary' ? '纪念日' : '提醒'; const dateObj = new Date(validated.date); @@ -223,11 +231,13 @@ async function callDeepSeek(message: string): Promise<{ response: `已为你创建${typeText}「${validated.title}」,日期:${dateText}${repeatText}`, }; } catch (e) { + console.error('[AI] Parse error:', e); // Schema validation failed, use mock response return mockParseResponse(message); } } + console.warn('[AI] No JSON found in response, using mock'); return mockParseResponse(message); } catch (error) { console.error('DeepSeek API error:', error);