Skip to content

Commit b349e7e

Browse files
committed
Fix #366 Add conversations API support
1 parent dba4bb6 commit b349e7e

File tree

7 files changed

+396
-610
lines changed

7 files changed

+396
-610
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
}

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/src/openaiResponsesModel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ export class OpenAIResponsesModel implements Model {
853853
include,
854854
tools,
855855
previous_response_id: request.previousResponseId,
856+
conversation: request.conversationId,
856857
prompt,
857858
temperature: request.modelSettings.temperature,
858859
top_p: request.modelSettings.topP,

0 commit comments

Comments
 (0)