Skip to content

Commit

Permalink
listSessions
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalef committed Aug 29, 2023
1 parent 128292b commit 38f4bb3
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/memory_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,67 @@ export default class MemoryManager {
return new Session(responseData);
}

/**
* Asynchronously retrieve a list of paginated sessions.
*
* @param {number} [limit] - Limit the number of results returned.
* @param {number} [cursor] - Cursor for pagination.
* @returns {Promise<Array<Session>>} A list of all sessions paginated.
* @throws {APIError} If the API response format is unexpected.
*/
async listSessions(
limit?: number,
cursor?: number
): Promise<Array<Session>> {
const params = new URLSearchParams();
if (limit !== undefined) params.append("limit", limit.toString());
if (cursor !== undefined) params.append("cursor", cursor.toString());

const response = await handleRequest(
fetch(`${this.client.getFullUrl("/sessions")}?${params.toString()}`, {
headers: this.client.headers,
}),
`Failed to get sessions`
);

const responseData = await response.json();

return responseData.map((session: any) => new Session(session));
}

/**
* Retrieve all sessions, handling pagination automatically.
* Yields a generator of lists of sessions.
*
* @param {number} [chunkSize=100] - The number of sessions to retrieve at a time.
* @returns {AsyncGenerator<Array<Session>, void, unknown>}
* The next chunk of sessions from the server.
* @throws {APIError} If the API response format is unexpected.
* @throws {ConnectionError} If the connection to the server fails.
*/
async *listSessionsChunked(
chunkSize: number = 100
): AsyncGenerator<Array<Session>, void, unknown> {
let cursor: number | undefined;

while (true) {
// eslint-disable-next-line no-await-in-loop
const sessions = await this.listSessions(chunkSize, cursor);

if (sessions.length === 0) {
// We've reached the last page
break;
}

yield sessions;

if (cursor === undefined) {
cursor = 0;
}
cursor += chunkSize;
}
}

/**
* Retrieves memory for a specific session.
* @param {string} sessionID - The ID of the session to retrieve memory for.
Expand Down
105 changes: 105 additions & 0 deletions src/tests/memory_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,111 @@ describe("ZepClient", () => {
});
});

// Test Suite for listSessions()
describe("listSessions", () => {
// Test for retrieving sessions
it("should retrieve sessions", async () => {
const responseData = [
{
uuid: "uuid1",
created_at: "2022-01-01T00:00:00Z",
updated_at: "2022-01-01T00:00:00Z",
session_id: "session1",
metadata: {},
},
{
uuid: "uuid2",
created_at: "2022-01-01T00:00:00Z",
updated_at: "2022-01-01T00:00:00Z",
session_id: "session2",
metadata: {},
},
];

fetchMock.mockResponseOnce(JSON.stringify(responseData));

const sessions = await client.memory.listSessions();

expect(sessions).toEqual(responseData.map((session) => new Session(session)));
});

// Test for retrieving sessions with limit
it("should retrieve sessions with limit", async () => {
const responseData = [
{
uuid: "uuid1",
created_at: "2022-01-01T00:00:00Z",
updated_at: "2022-01-01T00:00:00Z",
session_id: "session1",
metadata: {},
},
];

fetchMock.mockResponseOnce(JSON.stringify(responseData));

const sessions = await client.memory.listSessions(1);

expect(sessions).toEqual(responseData.map((session) => new Session(session)));
});
});

// Test Suite for listSessionsChunked()
describe("listSessionsChunked", () => {
// Test for retrieving all sessions in chunks
it("should retrieve all sessions in chunks", async () => {
const expectedSessionsData = [
[
{
uuid: "uuid1",
created_at: "2022-01-01T00:00:00Z",
updated_at: "2022-01-01T00:00:00Z",
session_id: "session1",
metadata: {},
},
{
uuid: "uuid2",
created_at: "2022-01-01T00:00:00Z",
updated_at: "2022-01-01T00:00:00Z",
session_id: "session2",
metadata: {},
},
],
[
{
uuid: "uuid3",
created_at: "2022-01-01T00:00:00Z",
updated_at: "2022-01-01T00:00:00Z",
session_id: "session3",
metadata: {},
},
{
uuid: "uuid4",
created_at: "2022-01-01T00:00:00Z",
updated_at: "2022-01-01T00:00:00Z",
session_id: "session4",
metadata: {},
},
],
];

fetchMock.mockResponses(
JSON.stringify(expectedSessionsData[0]),
JSON.stringify(expectedSessionsData[1]),
JSON.stringify([]) // empty response to indicate end of list
);

const sessionsChunked = [];
for await (const sessions of client.memory.listSessionsChunked(2)) {
sessionsChunked.push(sessions.map((session) => session.toDict()));
}

expect(sessionsChunked).toEqual(expectedSessionsData);
});
});




// Test Suite for getMemory()
describe("getMemory", () => {
// Test for retrieving memory for a session
Expand Down

0 comments on commit 38f4bb3

Please sign in to comment.