|
| 1 | +import { Agent, Runner } from '../../src/agents'; |
| 2 | + |
| 3 | +/** |
| 4 | + * This demonstrates usage of the `previous_response_id` parameter to continue a conversation. |
| 5 | + * The second run passes the previous response ID to the model, which allows it to continue the |
| 6 | + * conversation without re-sending the previous messages. |
| 7 | + * |
| 8 | + * Notes: |
| 9 | + * 1. This only applies to the OpenAI Responses API. Other models will ignore this parameter. |
| 10 | + * 2. Responses are only stored for 30 days as of this writing, so in production you should |
| 11 | + * store the response ID along with an expiration date; if the response is no longer valid, |
| 12 | + * you'll need to re-send the previous conversation history. |
| 13 | + */ |
| 14 | + |
| 15 | +async function main() { |
| 16 | + const agent = new Agent({ |
| 17 | + name: 'Assistant', |
| 18 | + instructions: 'You are a helpful assistant. be VERY concise.', |
| 19 | + }); |
| 20 | + |
| 21 | + const result = await Runner.run( |
| 22 | + agent, |
| 23 | + 'What is the largest country in South America?' |
| 24 | + ); |
| 25 | + console.log(result.finalOutput); |
| 26 | + // Brazil |
| 27 | + console.log('first message response_id:', result.lastResponseId); |
| 28 | + |
| 29 | + const followupResult = await Runner.run( |
| 30 | + agent, |
| 31 | + 'What is the capital of that country?', |
| 32 | + { previousResponseId: result.lastResponseId! } |
| 33 | + ); |
| 34 | + console.log(followupResult.finalOutput); |
| 35 | + // Brasilia |
| 36 | + console.log('second message response_id:', followupResult.lastResponseId); |
| 37 | +} |
| 38 | + |
| 39 | +async function mainStream() { |
| 40 | + const agent = new Agent({ |
| 41 | + name: 'Assistant', |
| 42 | + instructions: 'You are a helpful assistant. be VERY concise.', |
| 43 | + tools: [], |
| 44 | + }); |
| 45 | + |
| 46 | + const result = Runner.runStreamed( |
| 47 | + agent, |
| 48 | + 'What is the largest country in South America?' |
| 49 | + ); |
| 50 | + |
| 51 | + for await (const event of result.streamEvents()) { |
| 52 | + if ( |
| 53 | + event.type === 'raw_response_event' && |
| 54 | + event.data.type === 'response.output_text.delta' |
| 55 | + ) { |
| 56 | + process.stdout.write(event.data.delta); |
| 57 | + } |
| 58 | + } |
| 59 | + console.log('\n---'); |
| 60 | + |
| 61 | + console.log('first messageresponse_id:', result.lastResponseId); |
| 62 | + |
| 63 | + const followupResult = Runner.runStreamed( |
| 64 | + agent, |
| 65 | + 'What is the capital of that country?', |
| 66 | + { previousResponseId: result.lastResponseId! } |
| 67 | + ); |
| 68 | + |
| 69 | + for await (const event of followupResult.streamEvents()) { |
| 70 | + if ( |
| 71 | + event.type === 'raw_response_event' && |
| 72 | + event.data.type === 'response.output_text.delta' |
| 73 | + ) { |
| 74 | + process.stdout.write(event.data.delta); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + console.log('\n---'); |
| 79 | + console.log('second message response_id:', followupResult.lastResponseId); |
| 80 | +} |
| 81 | + |
| 82 | +// Get user input for stream mode |
| 83 | +const readline = require('readline').createInterface({ |
| 84 | + input: process.stdin, |
| 85 | + output: process.stdout, |
| 86 | +}); |
| 87 | + |
| 88 | +readline.question('Run in stream mode? (y/n): ', (answer: string) => { |
| 89 | + readline.close(); |
| 90 | + if (answer.toLowerCase() === 'y') { |
| 91 | + mainStream(); |
| 92 | + } else { |
| 93 | + main(); |
| 94 | + } |
| 95 | +}); |
0 commit comments