Skip to content

Commit

Permalink
feat: bypass response from backend server (#178) (#180)
Browse files Browse the repository at this point in the history
(cherry picked from commit 3991de2)

Signed-off-by: SuZhou-Joe <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 10767ad commit ab55500
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 88 deletions.
18 changes: 14 additions & 4 deletions server/services/storage/agent_framework_storage_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,13 @@ describe('AgentFrameworkStorageService', () => {
mockedTransport.mockImplementationOnce(async (params) => {
return Promise.reject({ meta: { body: 'error' } });
});
expect(agentFrameworkService.getTraces('foo')).rejects.toMatchInlineSnapshot(
`[Error: get traces failed, reason:"error"]`
);
expect(agentFrameworkService.getTraces('foo')).rejects.toMatchInlineSnapshot(`
Object {
"meta": Object {
"body": "error",
},
}
`);
});

it('should encode id when calls getTraces with non-standard params in request payload', async () => {
Expand Down Expand Up @@ -408,7 +412,13 @@ describe('AgentFrameworkStorageService', () => {
bar: 'foo',
},
})
).rejects.toMatchInlineSnapshot(`[Error: update interaction failed, reason:"error"]`);
).rejects.toMatchInlineSnapshot(`
Object {
"meta": Object {
"body": "error",
},
}
`);
});

it('should encode id when calls updateInteraction with non-standard params in request payload', async () => {
Expand Down
152 changes: 68 additions & 84 deletions server/services/storage/agent_framework_storage_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,109 +146,93 @@ export class AgentFrameworkStorageService implements StorageService {
}

async deleteConversation(conversationId: string): Promise<ConversationOptResponse> {
try {
const response = await this.client.transport.request({
method: 'DELETE',
path: `${ML_COMMONS_BASE_API}/memory/${encodeURIComponent(conversationId)}`,
});
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 conversation failed, reason:' + JSON.stringify(error.meta?.body));
const response = await this.client.transport.request({
method: 'DELETE',
path: `${ML_COMMONS_BASE_API}/memory/${encodeURIComponent(conversationId)}`,
});
if (response.statusCode === 200) {
return {
success: true,
};
} else {
return {
success: false,
statusCode: response.statusCode,
message: JSON.stringify(response.body),
};
}
}

async updateConversation(
conversationId: string,
title: string
): Promise<ConversationOptResponse> {
try {
const response = await this.client.transport.request({
method: 'PUT',
path: `${ML_COMMONS_BASE_API}/memory/${encodeURIComponent(conversationId)}`,
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 conversation failed, reason:' + JSON.stringify(error.meta?.body));
const response = await this.client.transport.request({
method: 'PUT',
path: `${ML_COMMONS_BASE_API}/memory/${encodeURIComponent(conversationId)}`,
body: {
name: title,
},
});
if (response.statusCode === 200) {
return {
success: true,
};
} else {
return {
success: false,
statusCode: response.statusCode,
message: JSON.stringify(response.body),
};
}
}

async getTraces(interactionId: string): Promise<AgentFrameworkTrace[]> {
try {
const response = (await this.client.transport.request({
method: 'GET',
path: `${ML_COMMONS_BASE_API}/memory/message/${encodeURIComponent(interactionId)}/traces`,
})) as ApiResponse<{
traces: Array<{
message_id: string;
create_time: string;
input: string;
response: string;
origin: string;
trace_number: number;
}>;
const response = (await this.client.transport.request({
method: 'GET',
path: `${ML_COMMONS_BASE_API}/memory/message/${encodeURIComponent(interactionId)}/traces`,
})) as ApiResponse<{
traces: Array<{
message_id: string;
create_time: string;
input: string;
response: string;
origin: string;
trace_number: number;
}>;
}>;

return response.body.traces.map((item) => ({
interactionId: item.message_id,
input: item.input,
output: item.response,
createTime: item.create_time,
origin: item.origin,
traceNumber: item.trace_number,
}));
} catch (error) {
throw new Error('get traces failed, reason:' + JSON.stringify(error.meta?.body));
}
return response.body.traces.map((item) => ({
interactionId: item.message_id,
input: item.input,
output: item.response,
createTime: item.create_time,
origin: item.origin,
traceNumber: item.trace_number,
}));
}

async updateInteraction(
interactionId: string,
additionalInfo: Record<string, Record<string, boolean | string>>
): Promise<ConversationOptResponse> {
try {
const response = await this.client.transport.request({
method: 'PUT',
path: `${ML_COMMONS_BASE_API}/memory/message/${encodeURIComponent(interactionId)}`,
body: {
additional_info: additionalInfo,
},
});
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 interaction failed, reason:' + JSON.stringify(error.meta?.body));
const response = await this.client.transport.request({
method: 'PUT',
path: `${ML_COMMONS_BASE_API}/memory/message/${encodeURIComponent(interactionId)}`,
body: {
additional_info: additionalInfo,
},
});
if (response.statusCode === 200) {
return {
success: true,
};
} else {
return {
success: false,
statusCode: response.statusCode,
message: JSON.stringify(response.body),
};
}
}

Expand Down

0 comments on commit ab55500

Please sign in to comment.