Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: auto title generation #12

Merged
merged 2 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions app/(chat)/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
ApiSendMessageResponse,
ApiSendMessageStreamedResponse,
ApiGetAllConversationsResponse,
ApiRenameConversationResponse,
} from '@/app/(chat)/types';
import { extractErrorMessageOrDefault } from '@/lib/utils';

Expand Down Expand Up @@ -98,14 +99,13 @@ export const getConversationMessages = async (
* Create a conversation
* @param accessToken
* @param projectId
* @param conversationName
* @param conversationId
* @returns result containing the created conversation
*/
export const createConversation = async (
accessToken: string,
projectId: string,
conversationId: string,
conversationName: string,
): Promise<Result<ApiCreateConversationResponse, string>> => {
try {
const conversationResponse = await fetch(
Expand All @@ -117,7 +117,10 @@ export const createConversation = async (
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: conversationName,
/**
* This will be updated immediately via title generation API
*/
name: 'Default Title',
project_id: projectId,
conversation_id: conversationId,
}),
Expand Down Expand Up @@ -236,6 +239,7 @@ export const sendMessageStreamed = async (
/**
* Get all conversations
* @param accessToken
* @param projectId
* @returns result containing all conversations of current user
*/
export const getAllConversations = async (
Expand Down Expand Up @@ -266,3 +270,47 @@ export const getAllConversations = async (
return Err(extractErrorMessageOrDefault(error));
}
};

/**
* Rename a conversation title based on a message
* @param accessToken
* @param projectId
* @param conversationId
* @param message
* @returns result containing updated title
*/
export const renameConversation = async (
accessToken: string,
projectId: string,
conversationId: string,
message: string,
): Promise<Result<ApiRenameConversationResponse, string>> => {
try {
const renameResponse = await fetch(
`${patternCoreEndpoint}/playground/conversation/${projectId}/${conversationId}/title-generation`,
{
method: 'POST',
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
message,
}),
},
);

if (renameResponse.ok) {
const result: ApiRenameConversationResponse = (
await renameResponse.json()
).data;

return Ok(result);
}
return Err(
`Renaming conversation failed with error code ${renameResponse.status}`,
);
} catch (error) {
return Err(extractErrorMessageOrDefault(error));
}
};
1 change: 1 addition & 0 deletions app/(chat)/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function POST(request: Request) {
session.accessToken,
session.projectId,
id,
userMessage.content,
);

if (conversationResult.isErr()) {
Expand Down
19 changes: 17 additions & 2 deletions app/(chat)/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@ import type { Chat } from '@/lib/db/schema';

import {
createConversation,
getAllConversations,
renameConversation,
getConversation,
getAllConversations,
} from './adapter';
import type { Conversation } from './types';

/**
* Checks if the conversation exists and returns it, otherwise creates it
* @param accessToken
* @param projectId
* @param conversationId
* @param initialMessage
* @returns result containing the existing or created conversation
*/
export const getOrCreateConversation = async (
accessToken: string,
projectId: string,
conversationId: string,
initialMessage: string,
): Promise<Result<Conversation, string>> => {
const conversationResult = await getConversation(
accessToken,
Expand All @@ -35,13 +40,23 @@ export const getOrCreateConversation = async (
accessToken,
projectId,
conversationId,
'Default Title',
);
if (createConversationResult.isErr()) {
return Err(createConversationResult.error);
}

conversation = createConversationResult.value;

/**
* We don't care if the renaming process is successful. It may fail and the
* conversation will keep the default title
*/
await renameConversation(
accessToken,
projectId,
conversationId,
initialMessage,
);
}

return Ok(conversation);
Expand Down
3 changes: 3 additions & 0 deletions app/(chat)/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ export type ApiCreateConversationResponse = Conversation;
export type ApiSendMessageResponse = string;
export type ApiSendMessageStreamedResponse = ReadableStream;
export type ApiGetAllConversationsResponse = Conversation[];
export interface ApiRenameConversationResponse {
title: string;
}