Skip to content

Commit

Permalink
feat: retrieve conversation metadata when loading conversation
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Nov 30, 2023
1 parent a5c2025 commit 5ca502e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion common/types/chat_saved_object_attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface Interaction {

export interface ISession {
title: string;
version: number;
version?: number;
createdTimeMs: number;
updatedTimeMs: number;
messages: IMessage[];
Expand Down
4 changes: 2 additions & 2 deletions public/hooks/use_sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand Down
4 changes: 2 additions & 2 deletions server/routes/chat_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const updateSessionRoute = {
params: schema.object({
sessionId: schema.string(),
}),
query: schema.object({
body: schema.object({
title: schema.string(),
}),
},
Expand Down Expand Up @@ -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) {
Expand Down
50 changes: 26 additions & 24 deletions server/services/storage/agent_framework_storage_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -29,37 +30,38 @@ export class AgentFrameworkStorageService implements StorageService {
private readonly messageParsers: MessageParser[] = []
) {}
async getSession(sessionId: string): Promise<ISession> {
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,
};
Expand Down

0 comments on commit 5ca502e

Please sign in to comment.