diff --git a/app/(chat)/adapter.ts b/app/(chat)/adapter.ts index 43582cb..da5b1ba 100644 --- a/app/(chat)/adapter.ts +++ b/app/(chat)/adapter.ts @@ -7,6 +7,7 @@ import type { ApiSendMessageResponse, ApiSendMessageStreamedResponse, ApiGetAllConversationsResponse, + ApiRenameConversationResponse, } from '@/app/(chat)/types'; import { extractErrorMessageOrDefault } from '@/lib/utils'; @@ -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> => { try { const conversationResponse = await fetch( @@ -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, }), @@ -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 ( @@ -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> => { + 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)); + } +}; diff --git a/app/(chat)/api/chat/route.ts b/app/(chat)/api/chat/route.ts index 458a562..e9679c6 100644 --- a/app/(chat)/api/chat/route.ts +++ b/app/(chat)/api/chat/route.ts @@ -39,6 +39,7 @@ export async function POST(request: Request) { session.accessToken, session.projectId, id, + userMessage.content, ); if (conversationResult.isErr()) { diff --git a/app/(chat)/service.ts b/app/(chat)/service.ts index e8757e5..959890a 100644 --- a/app/(chat)/service.ts +++ b/app/(chat)/service.ts @@ -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> => { const conversationResult = await getConversation( accessToken, @@ -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); diff --git a/app/(chat)/types.ts b/app/(chat)/types.ts index e6b93a1..e0eab5f 100644 --- a/app/(chat)/types.ts +++ b/app/(chat)/types.ts @@ -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; +}