diff --git a/common/types/chat_saved_object_attributes.ts b/common/types/chat_saved_object_attributes.ts index acfc8d71..118ca75f 100644 --- a/common/types/chat_saved_object_attributes.ts +++ b/common/types/chat_saved_object_attributes.ts @@ -18,7 +18,7 @@ export interface Interaction { export interface ISession { title: string; - version: number; + version?: number; createdTimeMs: number; updatedTimeMs: number; messages: IMessage[]; diff --git a/public/hooks/use_sessions.ts b/public/hooks/use_sessions.ts index 1f0789bd..8c863683 100644 --- a/public/hooks/use_sessions.ts +++ b/public/hooks/use_sessions.ts @@ -42,9 +42,9 @@ export const usePatchSession = () => { dispatch({ type: 'request', abortController }); return core.services.http .put(`${ASSISTANT_API.SESSION}/${sessionId}`, { - query: { + body: JSON.stringify({ title, - }, + }), signal: abortController.signal, }) .then((payload) => dispatch({ type: 'success', payload })) diff --git a/server/routes/chat_routes.ts b/server/routes/chat_routes.ts index c234a42d..2d557236 100644 --- a/server/routes/chat_routes.ts +++ b/server/routes/chat_routes.ts @@ -99,7 +99,7 @@ const updateSessionRoute = { params: schema.object({ sessionId: schema.string(), }), - query: schema.object({ + body: schema.object({ title: schema.string(), }), }, @@ -225,7 +225,7 @@ export function registerChatRoutes(router: IRouter, routeOptions: RoutesOptions) try { const getResponse = await storageService.updateSession( request.params.sessionId, - request.query.title + request.body.title ); return response.ok({ body: getResponse }); } catch (error) { diff --git a/server/services/storage/agent_framework_storage_service.ts b/server/services/storage/agent_framework_storage_service.ts index 06e7777a..827170e7 100644 --- a/server/services/storage/agent_framework_storage_service.ts +++ b/server/services/storage/agent_framework_storage_service.ts @@ -4,6 +4,7 @@ */ import { ApiResponse } from '@opensearch-project/opensearch/.'; +import { TransportRequestPromise } from '@opensearch-project/opensearch/lib/Transport'; import { AgentFrameworkTrace } from '../../../common/utils/llm_chat/traces'; import { OpenSearchClient } from '../../../../../src/core/server'; import { @@ -29,37 +30,38 @@ export class AgentFrameworkStorageService implements StorageService { private readonly messageParsers: MessageParser[] = [] ) {} async getSession(sessionId: string): Promise { - const session = (await this.client.transport.request({ - method: 'GET', - path: `/_plugins/_ml/memory/conversation/${sessionId}/_list`, - })) as ApiResponse<{ - interactions: Interaction[]; - }>; + const [interactionsResp, conversation] = await Promise.all([ + this.client.transport.request({ + method: 'GET', + path: `/_plugins/_ml/memory/conversation/${sessionId}/_list`, + }) as TransportRequestPromise< + ApiResponse<{ + interactions: Interaction[]; + }> + >, + this.client.transport.request({ + method: 'GET', + path: `/_plugins/_ml/memory/conversation/${sessionId}`, + }) as TransportRequestPromise< + ApiResponse<{ + conversation_id: string; + create_time: string; + updated_time: string; + name: string; + }> + >, + ]); const messageParserRunner = new MessageParserRunner(this.messageParsers); - const finalInteractions: Interaction[] = [...session.body.interactions]; + const finalInteractions: Interaction[] = [...interactionsResp.body.interactions]; - /** - * Sort interactions according to create_time - */ - finalInteractions.sort((interactionA, interactionB) => { - const { create_time: createTimeA } = interactionA; - const { create_time: createTimeB } = interactionB; - const createTimeMSA = +new Date(createTimeA); - const createTimeMSB = +new Date(createTimeB); - if (isNaN(createTimeMSA) || isNaN(createTimeMSB)) { - return 0; - } - return createTimeMSA - createTimeMSB; - }); let finalMessages: IMessage[] = []; for (const interaction of finalInteractions) { finalMessages = [...finalMessages, ...(await messageParserRunner.run(interaction))]; } return { - title: 'test', - version: 1, - createdTimeMs: Date.now(), - updatedTimeMs: Date.now(), + title: conversation.body.name, + createdTimeMs: +new Date(conversation.body.create_time), + updatedTimeMs: +new Date(conversation.body.updated_time), messages: finalMessages, interactions: finalInteractions, };