Skip to content

Commit

Permalink
fix: limit chat title length to 255
Browse files Browse the repository at this point in the history
  • Loading branch information
Mini256 committed Jun 7, 2024
1 parent f966fc3 commit 3443367
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/app/api/v1/chats/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,20 @@ export const POST = defineHandler({
return CHAT_CAN_NOT_ASSIGN_SESSION_ID_ERROR;
}

// TODO: using AI generated title.
let title = body.name ?? DEFAULT_CHAT_TITLE;
if (title.length > 255) {
title = title.substring(0, 255);
}

return await createChat({
engine: engine.engine,
engine_id: engine.id,
engine_name: engine.name,
engine_options: JSON.stringify(engine.engine_options),
created_at: new Date(),
created_by: userId,
title: title,
// TODO: using AI generated title.
title: limitTitleLength(body.name ?? DEFAULT_CHAT_TITLE),
});
}

const lastUserMessage = messages.findLast(m => m.role === 'user')?.content ?? '';

// For Ask Widget.
let chat: Chat | undefined;
let sessionId = body.sessionId;
Expand All @@ -83,7 +80,7 @@ export const POST = defineHandler({
engine_options: JSON.stringify(engine.engine_options),
created_at: new Date(),
created_by: userId,
title: body.name ?? body.messages.findLast(message => message.role === 'user')?.content ?? DEFAULT_CHAT_TITLE,
title: limitTitleLength(body.name ?? lastUserMessage ?? DEFAULT_CHAT_TITLE),
});
sessionId = chat.url_key;
} else {
Expand All @@ -106,7 +103,6 @@ export const POST = defineHandler({
await chatService.deleteHistoryFromMessage(chat, body.messageId);
}

const lastUserMessage = messages.findLast(m => m.role === 'user')?.content ?? '';
const chatResult = await chatService.chat(sessionId, userId, lastUserMessage, body.regenerate ?? false, body.stream as any);

if (body.stream) {
Expand All @@ -116,6 +112,10 @@ export const POST = defineHandler({
}
});

function limitTitleLength(title: string, limit: number = 255): string {
return title.length > limit ? title.substring(0, limit) : title;
}

export const GET = defineHandler({
auth: 'anonymous',
searchParams: z.object({
Expand Down

0 comments on commit 3443367

Please sign in to comment.