Package com.google.genkit.ai.agent
Class SessionRunner<S>
java.lang.Object
com.google.genkit.ai.agent.SessionRunner<S>
- Type Parameters:
S- the type of the custom session state
Drives ONE turn of an agent session: validates the input, appends the user message, runs the turn
body, and persists a snapshot on success (
SnapshotStatus.COMPLETED) or on failure (SnapshotStatus.FAILED). Failure snapshots are swallowed — runTurn(com.google.genkit.ai.agent.AgentInput, com.google.genkit.ai.agent.TurnBody<S>) never rethrows
exceptions from the turn body.
Client-managed mode (store == null): no persistence; the runner tracks the last
snapshot in-memory only and lastSnapshotId() always returns "".
-
Constructor Summary
ConstructorsConstructorDescriptionSessionRunner(Session<S> session, SessionStore<S> store, SessionStoreOptions opts) Constructs a new SessionRunner.SessionRunner(Session<S> session, SessionStore<S> store, SessionStoreOptions opts, String seedSnapshotId) Constructs a new SessionRunner seeded with the id of the snapshot the session was resumed from. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddArtifacts(Artifact... a) Adds artifacts to the session.voidaddMessages(Message... m) Appends messages to the session.Returns a copy of the current artifacts list.Returns a deep copy of the current custom state.Returns a copy of the current messages list.getState()Returns a deep copy of the current session state.Returns the session state as of the last successful turn, ornullif no successful turn has completed.Returns the last persisted (or in-memory) snapshot, ornullif no turn has completed.Returns the ID of the last snapshot, ornullif no turn has completed.Returns the error from the last failed turn, ornullif the last turn succeeded.Returns the finish reason of the last completed turn.voidrunTurn(AgentInput input, TurnBody<S> turnBody) Runs ONE turn: Validatesinput.message: role must be null/USER; rejects tool-request and tool-response parts → throwsGenkitExceptionwithINVALID_ARGUMENT(API misuse; not graceful).session()Returns the underlying session.Returns the session ID.intReturns the number of turns that have completed (including failed turns).voidupdateCustom(UnaryOperator<S> fn) Appliesfnto the current custom state and stores the result.
-
Constructor Details
-
SessionRunner
Constructs a new SessionRunner.- Parameters:
session- the in-memory session to drivestore- the session store, ornullfor client-managed mode (no server persistence)opts- options forwarded to store operations
-
SessionRunner
public SessionRunner(Session<S> session, SessionStore<S> store, SessionStoreOptions opts, String seedSnapshotId) Constructs a new SessionRunner seeded with the id of the snapshot the session was resumed from.Seeding
lastSnapshotIdmakes the first post-resume turn chain itsparentIdto the resumed snapshot (theparentId = lastSnapshotIdlogic inmaybeSnapshot(com.google.genkit.ai.agent.SnapshotStatus, com.google.genkit.ai.agent.AgentFinishReason, com.google.genkit.ai.agent.RuntimeError, java.lang.String, java.lang.String)then threads correctly across invocations). Passnullfor a fresh session.- Parameters:
session- the in-memory session to drivestore- the session store, ornullfor client-managed mode (no server persistence)opts- options forwarded to store operationsseedSnapshotId- the id of the snapshot this session was resumed from, ornull
-
-
Method Details
-
sessionId
Returns the session ID.- Returns:
- the session ID (never null)
-
getState
Returns a deep copy of the current session state.- Returns:
- the current session state
-
getMessages
Returns a copy of the current messages list.- Returns:
- the current messages
-
addMessages
Appends messages to the session.- Parameters:
m- messages to add
-
getCustom
Returns a deep copy of the current custom state.- Returns:
- the custom state
-
updateCustom
Appliesfnto the current custom state and stores the result.- Parameters:
fn- the update function
-
getArtifacts
Returns a copy of the current artifacts list.- Returns:
- the artifacts
-
addArtifacts
Adds artifacts to the session.- Parameters:
a- artifacts to add
-
session
Returns the underlying session.- Returns:
- the session
-
turnIndex
public int turnIndex()Returns the number of turns that have completed (including failed turns).- Returns:
- the turn index (0 before any turn has run)
-
lastSnapshot
Returns the last persisted (or in-memory) snapshot, ornullif no turn has completed.- Returns:
- the last snapshot
-
lastSnapshotId
Returns the ID of the last snapshot, ornullif no turn has completed. Returns""(empty string) in client-managed mode.- Returns:
- the last snapshot ID
-
lastGoodState
Returns the session state as of the last successful turn, ornullif no successful turn has completed.- Returns:
- the last good state
-
lastTurnFinishReason
Returns the finish reason of the last completed turn.- Returns:
- the finish reason, or
nullif no turn has run
-
lastTurnError
Returns the error from the last failed turn, ornullif the last turn succeeded.- Returns:
- the last turn error
-
runTurn
Runs ONE turn:- Validates
input.message: role must be null/USER; rejects tool-request and tool-response parts → throwsGenkitExceptionwithINVALID_ARGUMENT(API misuse; not graceful). - Reserves a turn snapshot ID (UUID if store != null, else "").
- Appends
input.messageto the session if non-null. - Runs
turnBody.run(input, turnCtx). - On success: persists a
COMPLETEDsnapshot; recordslastGoodState; increments turn index. - On exception: records
lastTurnError; persists aFAILEDsnapshot (orABORTEDforInterruptedException); increments turn index; does NOT rethrow.
- Parameters:
input- the agent input for this turnturnBody- the turn body to execute- Throws:
GenkitException- (INVALID_ARGUMENT) if the input message is invalid — this IS propagated (it is API misuse, not a graceful turn failure)
- Validates
-