Class AgentSessionContext

java.lang.Object
com.google.genkit.ai.agent.AgentSessionContext

public final class AgentSessionContext extends Object
AgentSessionContext binds a Session to the current thread so that prompts, middleware, and tools can access the active session without passing it through every call frame.

A ThreadLocal is used for binding; the context is always cleared after run(com.google.genkit.ai.agent.Session<?>, java.lang.Runnable) or call(com.google.genkit.ai.agent.Session<?>, java.util.function.Supplier<T>) returns (even if the body throws).

  • Method Details

    • run

      public static void run(Session<?> session, Runnable body)
      Executes body with session bound to the current thread context. Clears the binding when the body returns or throws.
      Parameters:
      session - the session to bind (must not be null)
      body - the runnable to execute
    • call

      public static <T> T call(Session<?> session, Supplier<T> body)
      Executes body with session bound to the current thread context and returns the result. Clears the binding when the body returns or throws.
      Type Parameters:
      T - the return type
      Parameters:
      session - the session to bind (must not be null)
      body - the supplier to execute
      Returns:
      the value returned by body
    • current

      public static Session<?> current()
      Returns the Session currently bound to this thread, or null if none.
      Returns:
      the current session, or null
    • currentArtifactStore

      public static ArtifactStore currentArtifactStore()
      Returns the current session viewed as an ArtifactStore, or null if no session is bound to this thread.
      Returns:
      the current session as an ArtifactStore, or null