From d366de3c865378c88e73fa62b049108ee51c09aa Mon Sep 17 00:00:00 2001 From: Mateo Ortegon <60627969+blacksheep-git@users.noreply.github.com> Date: Fri, 31 Oct 2025 20:09:46 -0400 Subject: [PATCH] =?UTF-8?q?Fix:=20generate=20title=20from=20user=20message?= =?UTF-8?q?=20=E2=80=94=20use=20only=20text=20parts=20(#1279)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- app/(chat)/actions.ts | 10 ++++------ lib/ai/prompts.ts | 6 ++++++ lib/utils.ts | 4 ++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/(chat)/actions.ts b/app/(chat)/actions.ts index 4e8a3ee..ea9e662 100644 --- a/app/(chat)/actions.ts +++ b/app/(chat)/actions.ts @@ -4,11 +4,13 @@ import { generateText, type UIMessage } from "ai"; import { cookies } from "next/headers"; import type { VisibilityType } from "@/components/visibility-selector"; import { myProvider } from "@/lib/ai/providers"; +import { titlePrompt } from "@/lib/ai/prompts"; import { deleteMessagesByChatIdAfterTimestamp, getMessageById, updateChatVisiblityById, } from "@/lib/db/queries"; +import { getTextFromMessage } from "@/lib/utils"; export async function saveChatModelAsCookie(model: string) { const cookieStore = await cookies(); @@ -22,12 +24,8 @@ export async function generateTitleFromUserMessage({ }) { const { text: title } = await generateText({ model: myProvider.languageModel("title-model"), - system: `\n - - you will generate a short title based on the first message a user begins a conversation with - - ensure it is not more than 80 characters long - - the title should be a summary of the user's message - - do not use quotes or colons`, - prompt: JSON.stringify(message), + system: titlePrompt, + prompt: getTextFromMessage(message), }); return title; diff --git a/lib/ai/prompts.ts b/lib/ai/prompts.ts index 43bb158..80b87e6 100644 --- a/lib/ai/prompts.ts +++ b/lib/ai/prompts.ts @@ -112,3 +112,9 @@ export const updateDocumentPrompt = ( ${currentContent}`; }; + +export const titlePrompt = `\n + - you will generate a short title based on the first message a user begins a conversation with + - ensure it is not more than 80 characters long + - the title should be a summary of the user's message + - do not use quotes or colons` diff --git a/lib/utils.ts b/lib/utils.ts index cb7846f..d94b492 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -108,9 +108,9 @@ export function convertToUIMessages(messages: DBMessage[]): ChatMessage[] { })); } -export function getTextFromMessage(message: ChatMessage): string { +export function getTextFromMessage(message: ChatMessage | UIMessage): string { return message.parts .filter((part) => part.type === 'text') - .map((part) => part.text) + .map((part) => (part as { type: 'text'; text: string}).text) .join(''); }