Class ActionContext

java.lang.Object
com.google.genkit.core.ActionContext

public class ActionContext extends Object
ActionContext provides context for action execution including tracing and flow information. It is passed to all action executions and carries request-scoped state.
  • Constructor Details

    • ActionContext

      public ActionContext(SpanContext spanContext, String flowName, String spanPath, Registry registry, String sessionId, String threadName, Map<String,Object> context)
      Creates a new ActionContext.
      Parameters:
      spanContext - the tracing span context, may be null
      flowName - the name of the enclosing flow, may be null
      spanPath - the current span path for tracing
      registry - the Genkit registry
      sessionId - the session ID for multi-turn conversations
      threadName - the thread name for grouping related requests
      context - the request-scoped user context (e.g. {"auth": {...}}), may be null
    • ActionContext

      public ActionContext(SpanContext spanContext, String flowName, String spanPath, Registry registry, String sessionId, String threadName, Map<String,Object> context, Object resumed, Object originalInput)
      Creates a new ActionContext including resume-awareness fields.
      Parameters:
      spanContext - the tracing span context, may be null
      flowName - the name of the enclosing flow, may be null
      spanPath - the current span path for tracing
      registry - the Genkit registry
      sessionId - the session ID for multi-turn conversations
      threadName - the thread name for grouping related requests
      context - the request-scoped user context (e.g. {"auth": {...}}), may be null
      resumed - the resume metadata attached when this action is being re-invoked after an interrupt/restart (mirrors JS ToolRunOptions.resumed / Go ToolContext.Resumed); null on a normal (non-resumed) invocation
      originalInput - the tool request's original input (before any restart-replaced input); mirrors Go ToolContext.OriginalInput; null when not resuming
    • ActionContext

      public ActionContext(SpanContext spanContext, String flowName, String spanPath, Registry registry, String sessionId, String threadName)
      Creates a new ActionContext.
      Parameters:
      spanContext - the tracing span context, may be null
      flowName - the name of the enclosing flow, may be null
      spanPath - the current span path for tracing
      registry - the Genkit registry
      sessionId - the session ID for multi-turn conversations
      threadName - the thread name for grouping related requests
    • ActionContext

      public ActionContext(SpanContext spanContext, String flowName, String spanPath, Registry registry)
      Creates a new ActionContext.
      Parameters:
      spanContext - the tracing span context, may be null
      flowName - the name of the enclosing flow, may be null
      spanPath - the current span path for tracing
      registry - the Genkit registry
    • ActionContext

      public ActionContext(SpanContext spanContext, String flowName, Registry registry)
      Creates a new ActionContext.
      Parameters:
      spanContext - the tracing span context, may be null
      flowName - the name of the enclosing flow, may be null
      registry - the Genkit registry
    • ActionContext

      public ActionContext(Registry registry)
      Creates a new ActionContext with default values.
      Parameters:
      registry - the Genkit registry
  • Method Details

    • getSpanContext

      public SpanContext getSpanContext()
      Returns the tracing span context.
      Returns:
      the span context, or null if tracing is not active
    • getFlowName

      public String getFlowName()
      Returns the name of the enclosing flow, if any.
      Returns:
      the flow name, or null if not in a flow context
    • getSpanPath

      public String getSpanPath()
      Returns the current span path for tracing.
      Returns:
      the span path, or null if not in a traced context
    • getRegistry

      public Registry getRegistry()
      Returns the Genkit registry.
      Returns:
      the registry
    • getSessionId

      public String getSessionId()
      Returns the session ID for multi-turn conversations.
      Returns:
      the session ID, or null if not set
    • getThreadName

      public String getThreadName()
      Returns the thread name for grouping related requests.
      Returns:
      the thread name, or null if not set
    • getContext

      public Map<String,Object> getContext()
      Returns the request-scoped user context.

      This is the context object injected by callers (such as the Dev UI "Execution context" panel or the reflection/serving layers), e.g. {"auth": {"user": "alice"}}. It is threaded through the run so tools and flows can read it via getContext().

      Returns:
      the user context map, or null if not set
    • getResumed

      public Object getResumed()
      Returns the resume metadata attached when this action is being re-invoked after an interrupt/restart, or null on a normal invocation.

      Mirrors JS ToolRunOptions.resumed and Go ToolContext.Resumed: a restart-aware tool can inspect this to distinguish a fresh call from a resumed one and read the client-supplied approval/confirmation payload.

      Returns:
      the resumed metadata value, or null if this is not a resumed invocation
    • isResumed

      public boolean isResumed()
      Returns true if this action is being re-invoked after an interrupt/restart (i.e. getResumed() is non-null).
      Returns:
      whether this is a resumed invocation
    • getOriginalInput

      public Object getOriginalInput()
      Returns the tool request's original input (before any restart-replaced input), mirroring Go ToolContext.OriginalInput. null when not resuming or when the original input was not preserved.
      Returns:
      the original input, or null
    • withFlowName

      public ActionContext withFlowName(String flowName)
      Creates a new ActionContext with a different flow name.
      Parameters:
      flowName - the new flow name
      Returns:
      a new ActionContext with the updated flow name
    • withSpanContext

      public ActionContext withSpanContext(SpanContext spanContext)
      Creates a new ActionContext with a different span context.
      Parameters:
      spanContext - the new span context
      Returns:
      a new ActionContext with the updated span context
    • withSpanPath

      public ActionContext withSpanPath(String spanPath)
      Creates a new ActionContext with a different span path.
      Parameters:
      spanPath - the new span path
      Returns:
      a new ActionContext with the updated span path
    • withSessionId

      public ActionContext withSessionId(String sessionId)
      Creates a new ActionContext with a session ID.
      Parameters:
      sessionId - the session ID
      Returns:
      a new ActionContext with the session ID
    • withThreadName

      public ActionContext withThreadName(String threadName)
      Creates a new ActionContext with a thread name.
      Parameters:
      threadName - the thread name
      Returns:
      a new ActionContext with the thread name
    • withContext

      public ActionContext withContext(Map<String,Object> context)
      Creates a new ActionContext with the given request-scoped user context.
      Parameters:
      context - the user context map (e.g. {"auth": {...}}), may be null
      Returns:
      a new ActionContext with the updated user context
    • withResumed

      public ActionContext withResumed(Object resumed, Object originalInput)
      Creates a new ActionContext carrying resume-awareness for a restarted tool call.
      Parameters:
      resumed - the resume metadata value (from the restart part's metadata.resumed); may be null
      originalInput - the tool request's original input; may be null
      Returns:
      a new ActionContext with the resume-awareness fields set
    • builder

      public static ActionContext.Builder builder()
      Creates a builder for ActionContext.
      Returns:
      a new builder