mirror of https://github.com/buster-so/buster.git
Refactor generateChatTitle function to improve title generation and database updates. Introduce helper functions for LLM title generation and database record updates, enhancing code clarity and maintainability.
This commit is contained in:
parent
5370b8cbf6
commit
a065612deb
|
@ -28,12 +28,13 @@ export interface GenerateChatTitleResult {
|
|||
title: string;
|
||||
}
|
||||
|
||||
export async function generateChatTitle({
|
||||
prompt,
|
||||
conversationHistory,
|
||||
chatId,
|
||||
messageId,
|
||||
}: GenerateChatTitleParams): Promise<GenerateChatTitleResult> {
|
||||
/**
|
||||
* Generates a title using the LLM with conversation context
|
||||
*/
|
||||
async function generateTitleWithLLM(
|
||||
prompt: string,
|
||||
conversationHistory?: ModelMessage[]
|
||||
): Promise<string> {
|
||||
try {
|
||||
// Prepare messages for the LLM
|
||||
const messages: ModelMessage[] = [];
|
||||
|
@ -55,9 +56,6 @@ export async function generateChatTitle({
|
|||
content: prompt,
|
||||
});
|
||||
|
||||
let title: { title: string };
|
||||
|
||||
try {
|
||||
const tracedChatTitle = wrapTraced(
|
||||
async () => {
|
||||
const { object } = await generateObject({
|
||||
|
@ -74,7 +72,7 @@ export async function generateChatTitle({
|
|||
);
|
||||
|
||||
const result = await tracedChatTitle();
|
||||
title = { title: result.title ?? 'New Analysis' };
|
||||
return result.title ?? 'New Analysis';
|
||||
} catch (llmError) {
|
||||
// Handle LLM generation errors specifically
|
||||
console.warn('[GenerateChatTitle] LLM failed to generate valid response:', {
|
||||
|
@ -83,16 +81,24 @@ export async function generateChatTitle({
|
|||
});
|
||||
|
||||
// Continue with fallback title instead of failing
|
||||
title = { title: 'New Analysis' };
|
||||
return 'New Analysis';
|
||||
}
|
||||
}
|
||||
|
||||
// Run database updates concurrently
|
||||
/**
|
||||
* Updates database records with the generated title
|
||||
*/
|
||||
async function updateDatabaseRecords(
|
||||
title: string,
|
||||
chatId?: string,
|
||||
messageId?: string
|
||||
): Promise<void> {
|
||||
const updatePromises: Promise<{ success: boolean }>[] = [];
|
||||
|
||||
if (chatId) {
|
||||
updatePromises.push(
|
||||
updateChat(chatId, {
|
||||
title: title.title,
|
||||
title,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -100,14 +106,28 @@ export async function generateChatTitle({
|
|||
if (messageId) {
|
||||
updatePromises.push(
|
||||
updateMessage(messageId, {
|
||||
title: title.title,
|
||||
title,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
await Promise.all(updatePromises);
|
||||
}
|
||||
|
||||
return { title: title.title };
|
||||
export async function generateChatTitle({
|
||||
prompt,
|
||||
conversationHistory,
|
||||
chatId,
|
||||
messageId,
|
||||
}: GenerateChatTitleParams): Promise<GenerateChatTitleResult> {
|
||||
try {
|
||||
// Generate title using LLM
|
||||
const title = await generateTitleWithLLM(prompt, conversationHistory);
|
||||
|
||||
// Update database records with the generated title
|
||||
await updateDatabaseRecords(title, chatId, messageId);
|
||||
|
||||
return { title };
|
||||
} catch (error) {
|
||||
// Handle AbortError gracefully
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
|
|
Loading…
Reference in New Issue