({ statusCode: 404, statusMessage: 'Chat not found' }) } if (!chat.title) { cons...
🚨 错误信息
import { convertToModelMessages, createUIMessageStream, createUIMessageStreamResponse, generateText, smoothStream, stepCountIs, streamText } from 'ai'
import { z } from 'zod'
import { db, schema } from 'hub:db'
import { and, eq } from 'drizzle-orm'
import type { UIMessage } from 'ai'
defineRouteMeta({
openAPI: {
description: 'Chat with AI.',
tags: ['ai']
}
})
export default defineEventHandler(async (event) => {
const session = await getUserSession(event)
const { id } = await getValidatedRouterParams(event, z.object({
id: z.string()
}).parse)
const { model, messages } = await readValidatedBody(event, z.object({
model: z.string(),
messages: z.array(z.custom<UIMessage>())
}).parse)
const chat = await db.query.chats.findFirst({
where: () => and(
eq(schema.chats.id, id as string),
eq(schema.chats.userId, session.user?.id || session.id)
),
with: {
messages: true
}
})
if (!chat) {
throw createError({ statusCode: 404, statusMessage: 'Chat not found' })
}
if (!chat.title) {
const { text: title } = await generateText({
model: 'openai/gpt-4o-mini',
system: `You are a title generator for a chat:
- Generate a short title based on the first user's message
- The title should be less than 30 characters long
- The title should be a summary of the user's message
- Do not use quotes (' or ") or colons (:) or any other punctuation
- Do not use markdown, just plain text`,
prompt: JSON.stringify(messages[0])
})
await db.update(schema.chats).set({ title }).where(eq(schema.chats.id, id as string))
}
const lastMessage = messages[messages.length - 1]
if (lastMessage?.role === 'user' && messages.length > 1) {
await db.insert(schema.messages).values({
chatId: id as string,
role: 'user',
parts: lastMessage.parts
})
}
const stream = createUIMessageStream({
execute: async ({ writer }) => {
const result = streamText({
model,
system: `You are a knowledgeable and helpful AI assistant. ${session.user?.username ? `The user's name is ${session.user.username}.` : ''} Your goal is to provide clear, accurate, and well-structured responses.
**FORMATTING RULES (CRITICAL):**
- ABSOLUTELY NO MARKDOWN HEADINGS: Never use #, ##, ###, ####, #####, or ######
- NO underline-style headings with === or ---
- Use **bold text** for emphasis and section labels instead
- Examples:
* Instead of "## Usage", write "**Usage:**" or just "Here's how to use it:"
* Instead of "# Complete Guide", write "**Complete Guide**" or start directly with content
- Start all responses with content, never with a heading
**RESPONSE QUALITY:**
- Be concise yet comprehensive
- Use examples when helpful
- Break down complex topics into digestible parts
- Maintain a friendly, professional tone`,
messages: await convertToModelMessages(messages),
providerOptions: {
openai: {
reasoningEffort: 'low',
reasoningSummary: 'detailed'
},
google: {
thinkingConfig: {
includeThoughts: true,
thinkingBudget: 2048
}
}
},
stopWhen: stepCountIs(5),
experimental_transform: smoothStream({ chunking: 'word' }),
tools: {
weather: weatherTool,
chart: chartTool
}
})
if (!chat.title) {
writer.write({
type: 'data-chat-title',
data: { message: 'Generating title...' },
transient: true
})
}
writer.merge(result.toUIMessageStream({
sendReasoning: true
}))
},
onFinish: async ({ messages }) => {
await db.insert(schema.messages).values(messages.map(message => ({
chatId: chat.id,
role: message.role as 'user' | 'assistant',
parts: message.parts
})))
}
})
return createUIMessageStreamResponse({
stream
})
})
🤖 AI解决方案
代码糕手分析报告
根据您提供的代码,我没有看到具体的报错信息。您只提供了代码片段,但没有提供运行时产生的错误消息。
目前无法进行分析,因为:
缺少实际的报错信息(错误堆栈、错误消息、错误类型)
缺少运行环境信息
缺少触发错误的上下文
请提供具体的报错信息,包括:
为了获得准确的分析,建议:
复制完整错误:从控制台或日志中复制整个错误消息
提供上下文:说明在什么操作下出现了错误
️ 标注版本信息:相关库或框架的版本号
错误翻译
根据您提供的代码,我没有看到具体的报错信息。您只提供了代码片段,但没有提供运行时产生的错误消息。
原因分析
目前无法进行分析,因为:
解决方案
请提供具体的报错信息,包括:
Error: [错误消息]
at [堆栈跟踪]
或者控制台输出的完整错误信息。
️ 预防措施
为了获得准确的分析,建议:
---
请提供您的报错信息: