feat: add SessionInfo abstraction for external session ID tracking #2037
+45
−26
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
In some cases we need to save the session id or to restore an existing session id and to continue from it, in some cases we need to move a session across process boundary (for example debug or human in the loop scenarios).
The transport previously owned session state directly via
self.session_id, making it impossible for consumers to observe or persist session IDs externally (e.g. persisting to a debug state endpoint for playground/agent resume scenarios).This PR introduces a
SessionInfobase class with asyncget_session_id/set_session_idmethods that the transport delegates all session ID storage to. Consumers can subclassSessionInfoto add side-effects like HTTP persistence, and pass it via the newsession_infoparameter onStreamableHTTPTransportandstreamable_http_client.Key changes
SessionInfoclass — base class with async get/set methods; default implementation stores in a plain attribute (backwards compatible)_session_info— replaces directself.session_idusage_prepare_headersand_maybe_extract_session_id_from_responseare now async — to support subclasses that need I/O (e.g. loading from a remote store)session_idfield fromRequestContext— headers are built from_prepare_headers, not from the contextstreamable_http_clientaccepts optionalsession_infoparameter — passed through to the transport constructorget_session_idis now async — delegates toSessionInfo.get_session_id()Backwards compatibility
session_infois provided, a plainSessionInfo()is created internallyRequestContext.session_idfield was not used by any handler (headers come from_prepare_headers)Example usage
Track the session ID externally:
Resume an existing session (skip initialize):