Class BidiActionImpl<I,O,S,Init>

java.lang.Object
com.google.genkit.core.BidiActionImpl<I,O,S,Init>
Type Parameters:
I - per-turn input type
O - final output type
S - stream chunk type
Init - session-init type
All Implemented Interfaces:
Action<I,O,S>, BidiAction<I,O,S,Init>, Registerable

public final class BidiActionImpl<I,O,S,Init> extends Object implements BidiAction<I,O,S,Init>
Concrete implementation of BidiAction built from a name, type parameters, optional metadata, and a BidiAction.BidiHandler.

Key invariants:

  • Method Details

    • getName

      public String getName()
      Description copied from interface: Action
      Returns the name of the action.
      Specified by:
      getName in interface Action<I,O,S>
      Returns:
      the action name
    • getType

      public ActionType getType()
      Description copied from interface: Action
      Returns the type of the action.
      Specified by:
      getType in interface Action<I,O,S>
      Returns:
      the action type
    • getDesc

      public ActionDesc getDesc()
      Description copied from interface: Action
      Returns the descriptor of the action containing metadata, schemas, etc.
      Specified by:
      getDesc in interface Action<I,O,S>
      Returns:
      the action descriptor
    • getInputSchema

      public Map<String,Object> getInputSchema()
      Description copied from interface: Action
      Returns the JSON schema for the action's input type.
      Specified by:
      getInputSchema in interface Action<I,O,S>
      Returns:
      the input schema as a map, or null if not defined
    • getOutputSchema

      public Map<String,Object> getOutputSchema()
      Description copied from interface: Action
      Returns the JSON schema for the action's output type.
      Specified by:
      getOutputSchema in interface Action<I,O,S>
      Returns:
      the output schema as a map, or null if not defined
    • getMetadata

      public Map<String,Object> getMetadata()
      Description copied from interface: Action
      Returns additional metadata for the action.
      Specified by:
      getMetadata in interface Action<I,O,S>
      Returns:
      the metadata map
    • run

      public O run(ActionContext ctx, I input) throws GenkitException
      Unary adaptation: wraps input in a BufferedInputSource (with null init) and delegates to the bidi handler.
      Specified by:
      run in interface Action<I,O,S>
      Parameters:
      ctx - the action context
      input - the input to the action
      Returns:
      the output of the action
      Throws:
      GenkitException - if the action fails
    • run

      public O run(ActionContext ctx, I input, Consumer<S> streamCallback) throws GenkitException
      Unary adaptation with streaming: wraps input in a BufferedInputSource (with null init) and delegates to the bidi handler.
      Specified by:
      run in interface Action<I,O,S>
      Parameters:
      ctx - the action context
      input - the input to the action
      streamCallback - callback for receiving streaming chunks, may be null
      Returns:
      the output of the action
      Throws:
      GenkitException - if the action fails
    • runJson

      public com.fasterxml.jackson.databind.JsonNode runJson(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode input, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) throws GenkitException
      Description copied from interface: Action
      Runs the action with JSON input and returns JSON output.
      Specified by:
      runJson in interface Action<I,O,S>
      Parameters:
      ctx - the action context
      input - the JSON input
      streamCallback - callback for receiving streaming JSON chunks, may be null
      Returns:
      the JSON output
      Throws:
      GenkitException - if the action fails
    • runJsonWithTelemetry

      public ActionRunResult<com.fasterxml.jackson.databind.JsonNode> runJsonWithTelemetry(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode input, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) throws GenkitException
      Description copied from interface: Action
      Runs the action with JSON input and returns the result with telemetry information.
      Specified by:
      runJsonWithTelemetry in interface Action<I,O,S>
      Parameters:
      ctx - the action context
      input - the JSON input
      streamCallback - callback for receiving streaming JSON chunks, may be null
      Returns:
      the action result including telemetry data
      Throws:
      GenkitException - if the action fails
    • runBidi

      public O runBidi(ActionContext ctx, Init init, InputSource<I> inputs, Consumer<S> streamCallback) throws GenkitException
      Description copied from interface: BidiAction
      Runs the action in bidirectional-streaming mode.
      Specified by:
      runBidi in interface BidiAction<I,O,S,Init>
      Parameters:
      ctx - the action context
      init - the one-time session-init value; may be null
      inputs - the stream of per-turn inputs
      streamCallback - callback invoked for each emitted chunk; may be null
      Returns:
      the final output
      Throws:
      GenkitException - if execution fails
    • runBidiJson

      public com.fasterxml.jackson.databind.JsonNode runBidiJson(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode init, InputSource<com.fasterxml.jackson.databind.JsonNode> inputs, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) throws GenkitException
      Description copied from interface: BidiAction
      Runs the action in bidirectional-streaming mode with JSON-typed arguments.

      Deserializes init and each element from inputs before handing them to the typed handler, and serializes all chunks and the final result back to JsonNode.

      Specified by:
      runBidiJson in interface BidiAction<I,O,S,Init>
      Parameters:
      ctx - the action context
      init - the one-time session-init as a JsonNode; may be null
      inputs - the stream of per-turn inputs as JsonNode values
      streamCallback - callback invoked for each emitted chunk serialized to JsonNode; may be null
      Returns:
      the final output serialized to JsonNode
      Throws:
      GenkitException - if execution fails
    • runBidiJsonWithTelemetry

      public ActionRunResult<com.fasterxml.jackson.databind.JsonNode> runBidiJsonWithTelemetry(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode init, InputSource<com.fasterxml.jackson.databind.JsonNode> inputs, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) throws GenkitException
      Description copied from interface: BidiAction
      Runs the action in bidirectional-streaming mode with JSON-typed arguments and telemetry.

      Behaves like BidiAction.runBidiJson(com.google.genkit.core.ActionContext, com.fasterxml.jackson.databind.JsonNode, com.google.genkit.core.InputSource<com.fasterxml.jackson.databind.JsonNode>, java.util.function.Consumer<com.fasterxml.jackson.databind.JsonNode>) (deserializing init and each element from inputs, serializing all chunks and the final result back to JsonNode) but additionally creates a new tracing span and captures the resulting trace/span IDs, returning them in an ActionRunResult. Unlike the unary Action.runJsonWithTelemetry(com.google.genkit.core.ActionContext, com.fasterxml.jackson.databind.JsonNode, java.util.function.Consumer<com.fasterxml.jackson.databind.JsonNode>), this threads the real init and the full stream of inputs through to the handler.

      Specified by:
      runBidiJsonWithTelemetry in interface BidiAction<I,O,S,Init>
      Parameters:
      ctx - the action context
      init - the one-time session-init as a JsonNode; may be null
      inputs - the stream of per-turn inputs as JsonNode values
      streamCallback - callback invoked for each emitted chunk serialized to JsonNode; may be null
      Returns:
      the final output serialized to JsonNode together with the captured trace/span IDs
      Throws:
      GenkitException - if execution fails
    • register

      public void register(Registry registry)
      Description copied from interface: Registerable
      Registers this primitive with the given registry.
      Specified by:
      register in interface Registerable
      Parameters:
      registry - the registry to register with
    • builder

      public static <I, O, S, Init> BidiActionImpl.Builder<I,O,S,Init> builder()
      Creates a new builder for BidiActionImpl.
      Type Parameters:
      I - per-turn input type
      O - final output type
      S - stream chunk type
      Init - session-init type
      Returns:
      a new builder