feat(go): added SessionRunner.RunBatched()#5018
feat(go): added SessionRunner.RunBatched()#5018apascal07 wants to merge 1 commit intoap/go-session-flowfrom
SessionRunner.RunBatched()#5018Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces the SessionFlow API, an experimental feature for managing stateful, multi-turn conversational flows with automatic snapshot persistence and streaming support. It includes new Go types for session management, a SessionRunner for turn-based interaction, and prompt-backed flow definitions. My feedback highlights potential performance concerns regarding the use of JSON marshaling for deep-copying snapshots and session states, as well as a recommendation to replace the magic number in the RunBatched buffer size with a named constant.
I am having trouble creating individual review comments. Click here to see my feedback.
go/ai/exp/session.go (115-128)
The copySnapshot function uses JSON marshaling and unmarshaling for deep copying. While this is a convenient way to achieve deep copies, it can be inefficient for very large SessionSnapshot objects or if this operation is performed frequently. Consider if a more performant deep copy mechanism is necessary, depending on the expected size and frequency of state snapshots.
go/ai/exp/session.go (249-258)
Similar to copySnapshot, copyStateLocked uses JSON marshaling and unmarshaling for deep copying the session state. This could introduce performance overhead if the session state (SessionState[State]) becomes very large or if state copying is a frequent operation. Evaluate the potential performance impact based on typical session state sizes.
go/ai/exp/session_flow.go (117)
The buffer size of 128 for the buf channel in RunBatched is a magic number. It would be clearer and more maintainable to define this as a named constant, possibly configurable, to explain its purpose and allow for easier adjustments in the future.
Adds
SessionRunner.RunBatched()for combining queued inputs into a single turn when a client sends multiple messages in rapid succession, saving model round-trips. Also replacesEndTurn bool+SnapshotID stringonSessionFlowStreamChunkwith a structuredTurnEndtype that consolidates both signals into one chunk and reports how many inputs were combined.Stacked on #4462.
Examples
Custom session flows
Replace
sess.Runwithsess.RunBatched. Same callback signature; combining is transparent:Prompt-backed session flows
Opt in via
WithBatchedInputs():Client-side usage
No client changes required.
TurnEnd.InputCounttells the client how many sends were consumed:API Reference
New
Changed
SessionFlowStreamChunkfieldsEndTurn boolandSnapshotID stringreplaced byTurnEnd *TurnEnd:Why ToolRestart inputs are not special-cased
RunBatchedcombines all queued inputs unconditionally because mixed-type queuing cannot occur in correct client code:If a buggy client queues mixed types,
handleResumeOptionfails with a clear precondition error.