Skip to content

Commit c8c67f1

Browse files
committed
add memory store
1 parent e8c0bde commit c8c67f1

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

packages/agents-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export type {
167167
} from './types';
168168
export { Usage } from './usage';
169169
export type { Session, SessionInputCallback } from './memory/session';
170+
export { MemorySession } from './memory/memorySession';
170171

171172
/**
172173
* Exporting the whole protocol as an object here. This contains both the types
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { randomUUID } from '@openai/agents-core/_shims';
2+
3+
import type { AgentInputItem } from '../types';
4+
import type { Session } from './session';
5+
6+
export type MemorySessionOptions = {
7+
sessionId?: string;
8+
initialItems?: AgentInputItem[];
9+
};
10+
11+
/**
12+
* Simple in-memory session store intended for demos or tests. Not recommended for production use.
13+
*/
14+
export class MemorySession implements Session {
15+
private readonly sessionId: string;
16+
17+
private items: AgentInputItem[];
18+
19+
constructor(options: MemorySessionOptions = {}) {
20+
this.sessionId = options.sessionId ?? randomUUID();
21+
this.items = options.initialItems ? [...options.initialItems] : [];
22+
}
23+
24+
async getSessionId(): Promise<string> {
25+
return this.sessionId;
26+
}
27+
28+
async getItems(limit?: number): Promise<AgentInputItem[]> {
29+
if (limit === undefined) {
30+
return [...this.items];
31+
}
32+
if (limit <= 0) {
33+
return [];
34+
}
35+
const start = Math.max(this.items.length - limit, 0);
36+
return this.items.slice(start);
37+
}
38+
39+
async addItems(items: AgentInputItem[]): Promise<void> {
40+
if (items.length === 0) {
41+
return;
42+
}
43+
this.items = [...this.items, ...items];
44+
}
45+
46+
async popItem(): Promise<AgentInputItem | undefined> {
47+
if (this.items.length === 0) {
48+
return undefined;
49+
}
50+
const item = this.items[this.items.length - 1];
51+
this.items = this.items.slice(0, -1);
52+
return item;
53+
}
54+
55+
async clearSession(): Promise<void> {
56+
this.items = [];
57+
}
58+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, expect, test } from 'vitest';
2+
3+
import { MemorySession } from '../src/memory/memorySession';
4+
import type { AgentInputItem } from '../src/types';
5+
6+
const createUserMessage = (text: string): AgentInputItem => ({
7+
role: 'user',
8+
content: [
9+
{
10+
type: 'input_text',
11+
text,
12+
},
13+
],
14+
});
15+
16+
describe('MemorySession', () => {
17+
test('stores and retrieves items in memory', async () => {
18+
const initialItems = [createUserMessage('hello')];
19+
const session = new MemorySession({
20+
sessionId: 'session-1',
21+
initialItems,
22+
});
23+
24+
expect(await session.getSessionId()).toBe('session-1');
25+
expect(await session.getItems()).toEqual(initialItems);
26+
27+
const newItems = [createUserMessage('one'), createUserMessage('two')];
28+
await session.addItems(newItems);
29+
expect(await session.getItems()).toEqual([...initialItems, ...newItems]);
30+
31+
expect(await session.getItems(2)).toEqual(newItems);
32+
33+
expect(await session.popItem()).toEqual(newItems[1]);
34+
expect(await session.getItems()).toEqual([...initialItems, newItems[0]]);
35+
36+
await session.clearSession();
37+
expect(await session.getItems()).toEqual([]);
38+
expect(await session.getItems(3)).toEqual([]);
39+
expect(await session.popItem()).toBeUndefined();
40+
});
41+
});

0 commit comments

Comments
 (0)