feat(voice): scope http_context to AgentSession when no job context#5608
Open
feat(voice): scope http_context to AgentSession when no job context#5608
Conversation
Outside a job process (tests, scripts, ad-hoc usage) there is no process-level http session bound to the event loop, so plugins calling utils.http_context.http_session() would raise. AgentSession.start now sets up the context (via _new_session_ctx) when get_job_context() is None, and aclose tears it down — including on a failed start so the factory isn't leaked. Also makes inference STT SpeechStream fetch the session lazily at ws_connect time instead of caching it at construction, so it picks up the AgentSession's session correctly and doesn't hold a stale reference.
When two AgentSessions run in the same task (e.g. nested), the inner start() previously overwrote the outer's contextvar factory because the ownership check only looked at job_ctx and the inner's own flag. Add an _is_http_session_ctx_set helper and consult it before claiming ownership, so the inner session reuses the outer's session and doesn't close it on aclose.
| aiohttp.WSMsgType.CLOSE, | ||
| aiohttp.WSMsgType.CLOSING, | ||
| ): | ||
| if closing_ws or self._session.closed: |
Member
There was a problem hiding this comment.
I believe we have the same pattern in other files as well.
Contributor
Author
There was a problem hiding this comment.
added this back with a local http_session var
Member
There was a problem hiding this comment.
Should we fix all "or self._session.closed:" usage?
Capture the http session once at the start of _run and pass it into _connect_ws. Restore the http_session.closed check in recv_task so a normal shutdown via AgentSession.aclose (which closes the underlying ClientSession) doesn't get reported as an unexpected gateway close.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When AgentSession runs outside a job process (tests, scripts, ad-hoc usage) there is no process-level http session bound to the event loop, so
utils.http_context.http_session()raises and STT/TTS that rely on it can't connect. AgentSession now creates the http session ctx instart()(whenget_job_context()is None and no outer ctx is already set) and tears it down inaclose(), so the same plugins that work inside a job process also work standalone with an AgentSession.