Skip to content

An AI-powered search engine with a generative UI

feat: add isNewChat flag to optimize new chat creation

Yoshiki Miura638741b2b767
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index 66a73034..dd86bb4f 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -19,7 +19,7 @@ export async function POST(req: Request) {
try {
const body = await req.json()
- const { message, chatId, trigger, messageId } = body
+ const { message, chatId, trigger, messageId, isNewChat } = body
// Handle different triggers
if (trigger === 'regenerate-assistant-message') {
@@ -87,7 +87,8 @@ export async function POST(req: Request) {
userId: userId, // userId is guaranteed to be non-null after authentication check above
trigger,
messageId,
- abortSignal
+ abortSignal,
+ isNewChat
})
} catch (error) {
console.error('API route error:', error)
diff --git a/components/chat.tsx b/components/chat.tsx
index f3220374..1118b5bc 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -100,7 +100,8 @@ export function Chat({
trigger: 'submit-user-message',
chatId: id,
message: messages[messages.length - 1],
- messageId
+ messageId,
+ isNewChat: messages.length === 1
}
}
}
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index 38532884..abb5a7f8 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -33,7 +33,7 @@ const DEFAULT_CHAT_TITLE = 'Untitled'
export async function createChatStreamResponse(
config: BaseStreamConfig
): Promise<Response> {
- const { message, model, chatId, userId, trigger, messageId, abortSignal } =
+ const { message, model, chatId, userId, trigger, messageId, abortSignal, isNewChat } =
config
const modelId = `${model.providerId}:${model.id}`
@@ -86,7 +86,8 @@ export async function createChatStreamResponse(
trigger,
initialChat,
abortSignal,
- parentTraceId // Add parent trace ID to context
+ parentTraceId, // Add parent trace ID to context
+ isNewChat
}
// Create the stream
diff --git a/lib/streaming/helpers/prepare-messages.ts b/lib/streaming/helpers/prepare-messages.ts
index c513b203..3d4ce46e 100644
--- a/lib/streaming/helpers/prepare-messages.ts
+++ b/lib/streaming/helpers/prepare-messages.ts
@@ -16,7 +16,7 @@ export async function prepareMessages(
context: StreamContext,
message: UIMessage | null
): Promise<UIMessage[]> {
- const { chatId, userId, trigger, messageId, initialChat } = context
+ const { chatId, userId, trigger, messageId, initialChat, isNewChat } = context
if (trigger === 'regenerate-assistant-message' && messageId) {
// Handle regeneration - use initialChat if available to avoid DB call
@@ -79,6 +79,15 @@ export async function prepareMessages(
id: message.id || generateId()
}
+ // Optimize for new chats: create chat and save message together
+ if (isNewChat) {
+ // Use createChatWithFirstMessage for atomic operation
+ const { createChatWithFirstMessage } = await import('@/lib/actions/chat')
+ await createChatWithFirstMessage(chatId, messageWithId, userId, DEFAULT_CHAT_TITLE)
+ return [messageWithId]
+ }
+
+ // For existing chats
if (!initialChat) {
await createChat(chatId, DEFAULT_CHAT_TITLE, userId)
}
diff --git a/lib/streaming/helpers/types.ts b/lib/streaming/helpers/types.ts
index 2a2cbdd3..2a55dbac 100644
--- a/lib/streaming/helpers/types.ts
+++ b/lib/streaming/helpers/types.ts
@@ -10,4 +10,5 @@ export interface StreamContext {
initialChat: (Chat & { messages: UIMessage[] }) | null
abortSignal?: AbortSignal
parentTraceId?: string
+ isNewChat?: boolean
}
diff --git a/lib/streaming/types.ts b/lib/streaming/types.ts
index 47894df4..28820964 100644
--- a/lib/streaming/types.ts
+++ b/lib/streaming/types.ts
@@ -10,4 +10,5 @@ export interface BaseStreamConfig {
trigger?: 'submit-user-message' | 'regenerate-assistant-message'
messageId?: string
abortSignal?: AbortSignal
+ isNewChat?: boolean
}