fix: 优化AI解析和添加调试日志

- 简化SYSTEM_PROMPT,使AI更容易理解并返回正确的JSON格式
- 添加详细日志帮助调试AI解析问题
- 改进错误处理

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
ddshi 2026-02-28 11:17:56 +08:00
parent 1ff0693901
commit d862203140

View File

@ -11,22 +11,25 @@ const SYSTEM_PROMPT = `你是一个帮助用户创建事件(纪念日或提醒
${new Date().toISOString()} ${new Date().toISOString()}
JSON格式返回 JSON格式
{ {"type": "anniversary", "title": "事件标题", "content": "详细内容", "date": "2026-02-13T09:00:00Z", "timezone": "Asia/Shanghai", "is_lunar": false, "repeat_type": "yearly", "priority": "none", "reminder_times": []}
"parsed": {
"type": "anniversary 或 reminder",
"title": "事件标题(简洁准确,去除""、""等前缀)", - type: "anniversary"() "reminder"()
"content": "详细内容(仅提醒类型,如会议主题、待办事项等)", - title: 简洁的事件标题"提醒""帮我"
"date": "2026-02-13T15:00:00Z", - content: 仅提醒类型需要
"timezone": "Asia/Shanghai", - date: ISO 8601
"is_lunar": true false, - timezone: "Asia/Shanghai"
"repeat_type": "daily, weekly, monthly, yearly 或 none", - is_lunar: 是否农历日期
"priority": "none, red, green 或 yellow仅提醒red=重要紧急green=绿色yellow=黄色)", - repeat_type: "daily"(), "weekly"(), "monthly"(), "yearly"(), "none"()
"reminder_times": ["提前提醒时间点数组,如 2026-02-12T09:00:00Z"] - priority: "none", "red", "green", "yellow"
}, - reminder_times: 提前提醒时间数组
"response": "友好的确认消息"
}
- "提醒我明天上午9点开会" title = "开会"
- "提醒我春节回家" title = "春节回家"
- "帮我记一下妈妈的生日" title = "妈妈的生日"
## ##
@ -200,14 +203,19 @@ async function callDeepSeek(message: string): Promise<{
const data = await response.json(); const data = await response.json();
const content = data.choices[0]?.message?.content || ''; const content = data.choices[0]?.message?.content || '';
// Log the raw response for debugging
console.log('[AI] Raw response:', content);
// Extract JSON from response // Extract JSON from response
const jsonMatch = content.match(/\{[\s\S]*\}/); const jsonMatch = content.match(/\{[\s\S]*\}/);
if (jsonMatch) { if (jsonMatch) {
try { try {
const rawParsed = JSON.parse(jsonMatch[0]); const rawParsed = JSON.parse(jsonMatch[0]);
console.log('[AI] Parsed JSON:', rawParsed);
const parsed = rawParsed.parsed || rawParsed; const parsed = rawParsed.parsed || rawParsed;
// Validate parsed data with Zod schema // Validate parsed data with Zod schema
const validated = parsedEventSchema.parse(parsed); const validated = parsedEventSchema.parse(parsed);
console.log('[AI] Validated:', validated);
// 构建友好的用户消息 // 构建友好的用户消息
const typeText = validated.type === 'anniversary' ? '纪念日' : '提醒'; const typeText = validated.type === 'anniversary' ? '纪念日' : '提醒';
const dateObj = new Date(validated.date); const dateObj = new Date(validated.date);
@ -223,11 +231,13 @@ async function callDeepSeek(message: string): Promise<{
response: `已为你创建${typeText}${validated.title}」,日期:${dateText}${repeatText}`, response: `已为你创建${typeText}${validated.title}」,日期:${dateText}${repeatText}`,
}; };
} catch (e) { } catch (e) {
console.error('[AI] Parse error:', e);
// Schema validation failed, use mock response // Schema validation failed, use mock response
return mockParseResponse(message); return mockParseResponse(message);
} }
} }
console.warn('[AI] No JSON found in response, using mock');
return mockParseResponse(message); return mockParseResponse(message);
} catch (error) { } catch (error) {
console.error('DeepSeek API error:', error); console.error('DeepSeek API error:', error);