Skip to content

Commit ea7c4da

Browse files
committed
Fix #366 Add conversations API support
1 parent 6f1677c commit ea7c4da

File tree

14 files changed

+76
-20
lines changed

14 files changed

+76
-20
lines changed

.changeset/thin-plants-hear.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@openai/agents-openai': patch
3+
'@openai/agents-core': patch
4+
'@openai/agents': patch
5+
---
6+
7+
#366 Add conversations API support

examples/basic/conversations.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Agent, run } from '@openai/agents';
2+
import OpenAI from 'openai';
3+
4+
async function main() {
5+
const client = new OpenAI();
6+
7+
console.log('### New conversation:\n');
8+
const newConvo = await client.conversations.create({});
9+
console.log(`New conversation: ${JSON.stringify(newConvo, null, 2)}`);
10+
const conversationId = newConvo.id;
11+
12+
const agent = new Agent({
13+
name: 'Assistant',
14+
instructions: 'You are a helpful assistant. be VERY concise.',
15+
});
16+
17+
// Set the conversation ID for the runs
18+
console.log('\n### Agent runs:\n');
19+
const runOptions = { conversationId };
20+
let result = await run(
21+
agent,
22+
'What is the largest country in South America?',
23+
runOptions,
24+
);
25+
console.log(`First run: ${result.finalOutput}`); // e.g., Brazil
26+
result = await run(agent, 'What is the capital of that country?', runOptions);
27+
console.log(`Second run: ${result.finalOutput}`); // e.g., Brasilia
28+
29+
console.log('\n### Conversation items:\n');
30+
const convo = await client.conversations.items.list(conversationId);
31+
for await (const page of convo.iterPages()) {
32+
for (const item of page.getPaginatedItems()) {
33+
// desc order
34+
console.log(JSON.stringify(item, null, 2));
35+
}
36+
}
37+
}
38+
if (require.main === module) {
39+
main().catch(console.error);
40+
}

examples/basic/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"start:tool-use-behavior": "tsx tool-use-behavior.ts",
2727
"start:tools": "tsx tools.ts",
2828
"start:reasoning": "tsx reasoning.ts",
29-
"start:local-file": "tsx local-file.ts"
29+
"start:local-file": "tsx local-file.ts",
30+
"start:conversations": "tsx conversations.ts"
3031
}
3132
}

examples/docs/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"@openai/agents-core": "workspace:*",
88
"@openai/agents-extensions": "workspace:*",
99
"@openai/agents-realtime": "workspace:*",
10-
"openai": "^5.12.2",
1110
"server-only": "^0.0.1",
1211
"zod": "^3.25.40"
1312
},

packages/agents-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"@modelcontextprotocol/sdk": "^1.17.2"
7171
},
7272
"dependencies": {
73-
"openai": "^5.12.2",
73+
"openai": "^5.15",
7474
"debug": "^4.4.0"
7575
},
7676
"peerDependencies": {

packages/agents-core/src/metadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const METADATA = {
66
"version": "0.0.17",
77
"versions": {
88
"@openai/agents-core": "0.0.17",
9-
"openai": "^5.12.2"
9+
"openai": "^5.15"
1010
}
1111
};
1212

packages/agents-core/src/model.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ export type ModelRequest = {
177177
*/
178178
previousResponseId?: string;
179179

180+
/**
181+
* The ID of stored conversation to use for the model.
182+
*
183+
* see https://platform.openai.com/docs/guides/conversation-state?api-mode=responses#openai-apis-for-conversation-state
184+
* see https://platform.openai.com/docs/api-reference/conversations/create
185+
*/
186+
conversationId?: string;
187+
180188
/**
181189
* The model settings to use for the model.
182190
*/

packages/agents-core/src/run.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ type SharedRunOptions<TContext = undefined> = {
136136
maxTurns?: number;
137137
signal?: AbortSignal;
138138
previousResponseId?: string;
139+
conversationId?: string;
139140
};
140141

141142
export type StreamRunOptions<TContext = undefined> =
@@ -393,6 +394,7 @@ export class Runner extends RunHooks<any, AgentOutputType<unknown>> {
393394
prompt: await state._currentAgent.getPrompt(state._context),
394395
input: turnInput,
395396
previousResponseId: options.previousResponseId,
397+
conversationId: options.conversationId,
396398
modelSettings,
397399
tools: serializedTools,
398400
outputType: convertAgentOutputTypeToSerializable(
@@ -757,6 +759,7 @@ export class Runner extends RunHooks<any, AgentOutputType<unknown>> {
757759
prompt: await currentAgent.getPrompt(result.state._context),
758760
input: turnInput,
759761
previousResponseId: options.previousResponseId,
762+
conversationId: options.conversationId,
760763
modelSettings,
761764
tools: serializedTools,
762765
handoffs: serializedHandoffs,

packages/agents-openai/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"dependencies": {
1818
"@openai/agents-core": "workspace:*",
1919
"debug": "^4.4.0",
20-
"openai": "^5.12.2"
20+
"openai": "^5.15"
2121
},
2222
"scripts": {
2323
"prebuild": "tsx ../../scripts/embedMeta.ts",

packages/agents-openai/src/metadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const METADATA = {
77
"versions": {
88
"@openai/agents-openai": "0.0.17",
99
"@openai/agents-core": "workspace:*",
10-
"openai": "^5.12.2"
10+
"openai": "^5.15"
1111
}
1212
};
1313

0 commit comments

Comments
 (0)