defineRemoteAction<Input, Output, Chunk, Init> function

RemoteAction<Input, Output, Chunk, Init> defineRemoteAction<Input, Output, Chunk, Init>({
  1. required String url,
  2. Map<String, String>? defaultHeaders,
  3. Client? httpClient,
  4. Output fromResponse(
    1. dynamic jsonData
    )?,
  5. Chunk fromStreamChunk(
    1. dynamic jsonData
    )?,
  6. SchemanticType<Input>? inputSchema,
  7. SchemanticType<Output>? outputSchema,
  8. SchemanticType<Chunk>? streamSchema,
})

Defines a remote Genkit action (flow) client.

This function returns a RemoteAction instance, which can be used to call or stream the specified Genkit flow. It simplifies the process of setting up a flow client by allowing direct specification of data conversion functions.

Type parameters:

  • Output: The type of the output data from a non-streaming flow invocation, or the type of the final response from a streaming flow.
  • Chunk: The type of the data chunks streamed from the flow.

Parameters:

  • url: The absolute URL of the Genkit flow.
  • defaultHeaders: Optional default HTTP headers to be sent with every request.
  • httpClient: Optional http.Client instance. If not provided, a new one will be created and managed by the RemoteAction. If provided, the caller is responsible for its lifecycle (e.g., closing it).
  • fromResponse: An optional function that converts the JSON-decoded response data (typically a Map<String, dynamic> or primitive type from jsonDecode) into the expected output type Output. If not provided, the result is a dynamic object from jsonDecode.
  • fromStreamChunk: An optional function that converts the JSON-decoded stream chunk data into the expected stream type Chunk. If not provided, chunks are dynamic objects from jsonDecode.

Returns a RemoteAction<Output, Chunk> instance.

Implementation

RemoteAction<Input, Output, Chunk, Init>
defineRemoteAction<Input, Output, Chunk, Init>({
  required String url,
  Map<String, String>? defaultHeaders,
  http.Client? httpClient,
  Output Function(dynamic jsonData)? fromResponse,
  Chunk Function(dynamic jsonData)? fromStreamChunk,
  SchemanticType<Input>? inputSchema,
  SchemanticType<Output>? outputSchema,
  SchemanticType<Chunk>? streamSchema,
}) {
  if (fromResponse != null && outputSchema != null) {
    throw ArgumentError(
      'Cannot provide both fromResponse and outputSchema. Please provide only one.',
    );
  }
  if (fromStreamChunk != null && streamSchema != null) {
    throw ArgumentError(
      'Cannot provide both fromStreamChunk and streamSchema. Please provide only one.',
    );
  }

  return RemoteAction<Input, Output, Chunk, Init>(
    url: url,
    defaultHeaders: defaultHeaders,
    httpClient: httpClient,
    fromResponse:
        fromResponse ??
        (outputSchema != null
            ? (d) => outputSchema.parse(d)
            : (d) => d as Output),
    fromStreamChunk:
        fromStreamChunk ??
        (streamSchema != null
            ? (d) => streamSchema.parse(d)
            : (d) => d as Chunk),
  );
}