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

Integrate Memeory APIs of agent framework #15

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Changes from 1 commit
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
117 changes: 111 additions & 6 deletions server/services/storage/agent_framework_storage_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import { StorageService } from './storage_service';
import { MessageParser } from '../../types';
import { MessageParserRunner } from '../../utils/message_parser_runner';

export interface SessionOptResponse {
success: boolean;
statusCode?: number | null;
message?: string;
}

export class AgentFrameworkStorageService implements StorageService {
constructor(
private readonly client: OpenSearchClient,
Expand Down Expand Up @@ -59,20 +65,119 @@ export class AgentFrameworkStorageService implements StorageService {
}

async getSessions(query: GetSessionsSchema): Promise<ISessionFindResponse> {
throw new Error('Method not implemented.');
let sortField = '';
if (query.sortField === 'updatedTimeMs') {
sortField = 'create_time';
}
let searchFields: string[] = [];
if (query.search && query.searchFields) {
if (typeof query.searchFields === 'string') {
searchFields = [...searchFields, query.searchFields.replace('title', 'name')];
} else {
searchFields = query.searchFields.map((item) => item.replace('title', 'name'));
}
}

const requestParams = {
from: (query.page - 1) * query.perPage,
size: query.perPage,
...(searchFields.length > 0 && {
query: {
multi_match: {
query: query.search,
fields: searchFields,
},
},
}),
...(searchFields.length === 0 && {
query: {
match_all: {},
},
}),
...(sortField && query.sortOrder && { sort: [{ [sortField]: query.sortOrder }] }),
};

const sessions = await this.client.transport.request({
method: 'GET',
path: `/_plugins/_ml/memory/conversation/_search`,
body: requestParams,
});

return {
objects: sessions.body.hits.hits
.filter(
(hit: {
_source: { name: string; create_time: string };
}): hit is RequiredKey<typeof hit, '_source'> =>
hit._source !== null && hit._source !== undefined
)
.map((item: { _id: string; _source: { name: string; create_time: string } }) => ({
id: item._id,
title: item._source.name,
version: 1,
createdTimeMs: Date.parse(item._source.create_time),
updatedTimeMs: Date.parse(item._source.create_time),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any update time in the source data? I think we should use update time here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backend API doesn't return update_time by now, it hasn't been implemented, I will update the code once it's ready.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add some todo about it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have done that, thanks!

messages: [] as IMessage[],
})),
total:
typeof sessions.body.hits.total === 'number'
? sessions.body.hits.total
: sessions.body.hits.total.value,
};
}

async saveMessages(
title: string,
sessionId: string | undefined,
messages: IMessage[]
): Promise<{ sessionId: string; messages: IMessage[] }> {
throw new Error('Method not implemented.');
throw new Error('Method is no need');
}
deleteSession(sessionId: string): Promise<{}> {
throw new Error('Method not implemented.');

async deleteSession(sessionId: string): Promise<SessionOptResponse> {
try {
const response = await this.client.transport.request({
method: 'DELETE',
path: `/_plugins/_ml/memory/conversation/${sessionId}/_delete`,
});
if (response.statusCode === 200) {
return {
success: true,
};
} else {
return {
success: false,
statusCode: response.statusCode,
message: JSON.stringify(response.body),
};
}
} catch (error) {
throw new Error('delete converstaion failed, reason:' + JSON.stringify(error.meta?.body));
}
}
updateSession(sessionId: string, title: string): Promise<{}> {
throw new Error('Method not implemented.');

async updateSession(sessionId: string, title: string): Promise<SessionOptResponse> {
try {
const response = await this.client.transport.request({
method: 'PUT',
path: `/_plugins/_ml/memory/conversation/${sessionId}/_update`,
body: {
name: title,
},
});
if (response.statusCode === 200) {
return {
success: true,
};
} else {
return {
success: false,
statusCode: response.statusCode,
message: JSON.stringify(response.body),
};
}
} catch (error) {
throw new Error('update converstaion failed, reason:' + JSON.stringify(error.meta?.body));
}
}
}
Loading