-
Notifications
You must be signed in to change notification settings - Fork 487
Feat: added context scope stream to enable streaming event from nested agent-as-tool invocations #697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rainbow-f1zz
wants to merge
7
commits into
openai:main
Choose a base branch
from
rainbow-f1zz:feat/context-scope-stream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,193
−22
Open
Feat: added context scope stream to enable streaming event from nested agent-as-tool invocations #697
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ba665ca
feat: add streamAgentTools parameter to enable streaming for agent-as…
rainbow-fizz f4dee98
added agentName to raw event to identify publishers
rainbow-fizz 6536043
removed redundant space to underscore replacement
rainbow-fizz 6962e08
fixed a typo: Schame -> Schema
rainbow-fizz 0ff296e
added unit tests
rainbow-fizz 82686b9
removed redundant debug log
rainbow-fizz 34d6cce
fixed a bug where cancel is not handled in context-scope stream
rainbow-fizz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { Agent, Runner, tool } from '@openai/agents'; | ||
| import { z } from 'zod'; | ||
|
|
||
| const getWeatherTool = tool({ | ||
| name: 'get_weather', | ||
| description: 'Get the weather for a given city', | ||
| parameters: z.object({ | ||
| city: z.string(), | ||
| }), | ||
| execute: async (input) => { | ||
| return `The weather in ${input.city} is sunny`; | ||
| }, | ||
| }); | ||
|
|
||
| const weatherAgent = new Agent({ | ||
| name: 'Weather Agent', | ||
| tools: [getWeatherTool], | ||
| }); | ||
|
|
||
| const getLocalNewsTool = tool({ | ||
| name: 'get_local_news', | ||
| description: 'Get the local news for today', | ||
| parameters: z.object({ | ||
| city: z.string(), | ||
| }), | ||
| execute: async (input) => { | ||
| return `Big news in ${input.city} today: famous local cat won Guinness World Record for loudest purr!`; | ||
| }, | ||
| }); | ||
|
|
||
| const newsAgent = new Agent({ | ||
| name: 'News Agent', | ||
| instructions: 'You are a news agent that can tell the news for a given city.', | ||
| tools: [getLocalNewsTool], | ||
| }); | ||
|
|
||
| const personalAgent = new Agent({ | ||
| name: 'Personal Agent', | ||
| instructions: | ||
| 'You are a personal agent that prepares a user for the day. You can use the news agent to get the news for the day, and the weather agent to get the weather for the day.', | ||
| tools: [ | ||
| newsAgent.asTool({ | ||
| toolName: 'news_agent', | ||
| toolDescription: 'Get the local news for today', | ||
| }), | ||
| weatherAgent.asTool({ | ||
| toolName: 'weather_agent', | ||
| toolDescription: 'Get the weather for today', | ||
| }), | ||
| ], | ||
| }); | ||
|
|
||
| const runner = new Runner({ | ||
| model: 'gpt-4.1-mini', | ||
| tracingDisabled: true, | ||
| }); | ||
|
|
||
| async function main() { | ||
| const streamedRunResult = await runner.run( | ||
| personalAgent, | ||
| "What's up in Beijing today?", | ||
| { | ||
| stream: true, | ||
| // enable streaming of agent as tool events in the context scope stream | ||
| streamAgentTools: true, | ||
| }, | ||
| ); | ||
|
|
||
| for await (const event of streamedRunResult) { | ||
| console.log(JSON.stringify(event)); | ||
| } | ||
| } | ||
|
|
||
| main().catch((error) => { | ||
| console.error(error); | ||
| process.exit(1); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
streamAgentToolsis enabled, the async iterator returned fromStreamedRunResultis the context-scope stream created here, but itscancelhandler is a no-op and never marks the result as cancelled. If a caller stops consuming the stream (e.g., breaks out offor await), the context stream is cancelled but#cancelledstays false, so_addItemcontinues to enqueue into a cancelled controller and the run loop keeps executing nested agents instead of short-circuiting. This regresses the previous cancellation semantics (line 264) and can throw once the controller is closed or waste work for abandoned streams. The cancel handler should set the same cancellation flag/cleanup as the primary stream.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed