Class ActionDef<I,O,S>

java.lang.Object
com.google.genkit.core.ActionDef<I,O,S>
Type Parameters:
I - The input type for the action
O - The output type for the action
S - The streaming chunk type (use Void for non-streaming actions)
All Implemented Interfaces:
Action<I,O,S>, Registerable

public class ActionDef<I,O,S> extends Object implements Action<I,O,S>
ActionDef is the default implementation of an Action. It provides a named, observable operation that can be executed and traced.
  • Constructor Details

    • ActionDef

      public ActionDef(String name, ActionType type, Map<String,Object> metadata, Map<String,Object> inputSchema, Class<I> inputClass, Class<O> outputClass, ActionDef.StreamingFunction<I,O,S> fn)
      Creates a new ActionDef.
      Parameters:
      name - the action name
      type - the action type
      metadata - additional metadata
      inputSchema - the input JSON schema
      inputClass - the input class
      outputClass - the output class
      fn - the action function
  • Method Details

    • create

      public static <I, O> ActionDef<I,O,Void> create(String name, ActionType type, Map<String,Object> metadata, Map<String,Object> inputSchema, Class<I> inputClass, Class<O> outputClass, ActionDef.ActionFunction<I,O> fn)
      Creates a non-streaming action.
      Type Parameters:
      I - input type
      O - output type
      Parameters:
      name - the action name
      type - the action type
      metadata - additional metadata
      inputSchema - the input JSON schema
      inputClass - the input class
      outputClass - the output class
      fn - the action function
      Returns:
      a new ActionDef
    • createStreaming

      public static <I, O, S> ActionDef<I,O,S> createStreaming(String name, ActionType type, Map<String,Object> metadata, Map<String,Object> inputSchema, Class<I> inputClass, Class<O> outputClass, ActionDef.StreamingFunction<I,O,S> fn)
      Creates a streaming action.
      Type Parameters:
      I - input type
      O - output type
      S - stream chunk type
      Parameters:
      name - the action name
      type - the action type
      metadata - additional metadata
      inputSchema - the input JSON schema
      inputClass - the input class
      outputClass - the output class
      fn - the streaming function
      Returns:
      a new ActionDef
    • 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
    • run

      public O run(ActionContext ctx, I input) throws GenkitException
      Description copied from interface: Action
      Runs the action with the given input.
      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
      Description copied from interface: Action
      Runs the action with the given input and streaming callback.
      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
    • 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
    • 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
    • getRegistry

      public Registry getRegistry()
      Returns the registry this action is registered with.
      Returns:
      the registry, or null if not registered