[IJAI-1134] feat(agent,protocol): support suspend-based paginated session/list - #123
Open
forketyfork wants to merge 1 commit into
Open
Conversation
Issue: session/list is served through setPaginatedRequestHandler, whose SequenceToPaginatedResponseAdapter needs a non-suspending Sequence. An agent whose session source is a database can only satisfy that by materializing the entire list on the first request, and the adapter's cursors live in server memory, are single-use, and are evicted after a minute of inactivity — costly for a slow-scrolling or reconnecting client. Solution: add setSuspendPaginatedRequestHandler, a sibling of setPaginatedRequestHandler that takes a suspend (request) -> Pair<List, cursor> page fetcher instead of a Sequence, with no iterator or cursor state kept by the SDK — the cursor is opaque and entirely owned by whatever the caller encodes into it, so it can be as durable as a database keyset. Wire this into session/list itself via a new AgentSupport.listSessionsPage hook: when an agent overrides it, Agent serves session/list from it directly; otherwise it falls back to the existing listSessions()/Sequence path unchanged, so existing agents keep working exactly as before.
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.
Issue
https://youtrack.jetbrains.com/issue/IJAI-1134
session/list(and any otherAcpPaginatedRequest/AcpPaginatedResponsemethod) is served throughsetPaginatedRequestHandler, backed bySequenceToPaginatedResponseAdapter. That adapter needs a non-suspendingSequence, so an agent whose session source is a database can only satisfy the contract by materializing the entire result set inside the one suspend call it's given, before pagination even starts. The adapter also keeps the resulting iterator in server-process memory behind a cursor that is single-use and evicted after a minute of inactivity — a real cost for a client that scrolls slowly or reconnects, since its cursor is simply gone.In our cloud-based implementation, we need a
session/listthat walks a keyset straight from the DB, one page per request, with a cursor that stays valid indefinitely and survives a reconnect.Solution
setSuspendPaginatedRequestHandler(Protocol.extensions.kt): a sibling ofsetPaginatedRequestHandlerthat takespageFactory: suspend (request) -> Pair<List<TItem>, String?>instead of aSequence. No iterator or cursor state is kept by the SDK — the cursor is opaque and entirely owned by whateverpageFactoryencodes into it, so it can be as durable as a database keyset, and a client that pauses or reconnects loses nothing.AgentSupport.listSessionsPage(AgentSupport.kt): a new optional hook alongsidelistSessions, defaulting tonull("not overridden").Agentnow tries this first forsession/list; when it returns non-null, the response is served directly from it via the new suspend-based path. When it returnsnull(the default),session/listfalls back tolistSessions()/Sequenceexactly as before — existing agents that only overridelistSessions()see no behavior change.Context
listSessionsPage(e.g. request-object vs. individual params,Pairvs. a smallPagetype) based on review.session/listend-to-end throughAgentbefore this PR (onlySequenceToPaginatedResponseAdapterwas unit-tested directly);AgentSessionListTestadds that coverage for both the new and the fallback path.