generate cfhat title

This commit is contained in:
dal 2025-08-06 22:56:27 -06:00
parent 1d545a009c
commit 5370b8cbf6
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
1 changed files with 22 additions and 16 deletions

View File

@ -13,12 +13,13 @@ const llmOutputSchema = z.object({
const generateChatTitleInstructions = ` const generateChatTitleInstructions = `
I am a chat title generator that is responsible for generating a title for the chat. I am a chat title generator that is responsible for generating a title for the chat.
The title should be 3-8 words, capturing the main topic or intent of the conversation. With an emphasis on the user's question and most recent converstaion topic. The title should be 3-8 words, capturing the main topic or intent of the conversation.
With an emphasis on the user's question and most recent converstaion topic.
`; `;
export interface GenerateChatTitleParams { export interface GenerateChatTitleParams {
prompt: string; prompt: string;
conversationHistory?: CoreMessage[]; conversationHistory?: ModelMessage[];
chatId?: string; chatId?: string;
messageId?: string; messageId?: string;
} }
@ -35,15 +36,25 @@ export async function generateChatTitle({
}: GenerateChatTitleParams): Promise<GenerateChatTitleResult> { }: GenerateChatTitleParams): Promise<GenerateChatTitleResult> {
try { try {
// Prepare messages for the LLM // Prepare messages for the LLM
let messages: CoreMessage[]; const messages: ModelMessage[] = [];
// Add system message
messages.push({
role: 'system',
content: generateChatTitleInstructions,
});
// Add conversation history if available
if (conversationHistory && conversationHistory.length > 0) { if (conversationHistory && conversationHistory.length > 0) {
// Use conversation history as context + append new user message messages.push(...conversationHistory);
messages = appendToConversation(conversationHistory, prompt);
} else {
// Otherwise, use just the prompt
messages = standardizeMessages(prompt);
} }
// Add the current user prompt
messages.push({
role: 'user',
content: prompt,
});
let title: { title: string }; let title: { title: string };
try { try {
@ -52,13 +63,7 @@ export async function generateChatTitle({
const { object } = await generateObject({ const { object } = await generateObject({
model: Haiku35, model: Haiku35,
schema: llmOutputSchema, schema: llmOutputSchema,
messages: [ messages,
{
role: 'system',
content: generateChatTitleInstructions,
},
...messages,
],
}); });
return object; return object;
@ -68,7 +73,8 @@ export async function generateChatTitle({
} }
); );
title = await tracedChatTitle(); const result = await tracedChatTitle();
title = { title: result.title ?? 'New Analysis' };
} catch (llmError) { } catch (llmError) {
// Handle LLM generation errors specifically // Handle LLM generation errors specifically
console.warn('[GenerateChatTitle] LLM failed to generate valid response:', { console.warn('[GenerateChatTitle] LLM failed to generate valid response:', {