defineRemoteAction<O, S> function

RemoteAction<O, S> defineRemoteAction<O, S>({
  1. required String url,
  2. Map<String, String>? defaultHeaders,
  3. Client? httpClient,
  4. required O fromResponse(
    1. dynamic jsonData
    ),
  5. S fromStreamChunk(
    1. dynamic jsonData
    )?,
})

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:

  • O: The type of the output data from a non-streaming flow invocation, or the type of the final response from a streaming flow.
  • S: 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: A function that converts the JSON-decoded response data (typically a Map<String, dynamic> or primitive type from jsonDecode) into the expected output type O. This is mandatory.
  • fromStreamChunk: An optional function that converts the JSON-decoded stream chunk data into the expected stream type S. This is required if you intend to use the .stream() method.

Returns a RemoteAction<O, S> instance.

Implementation

RemoteAction<O, S> defineRemoteAction<O, S>({
  required String url,
  Map<String, String>? defaultHeaders,
  http.Client? httpClient,
  required O Function(dynamic jsonData) fromResponse,
  S Function(dynamic jsonData)? fromStreamChunk,
}) {
  return RemoteAction<O, S>(
    url: url,
    defaultHeaders: defaultHeaders,
    httpClient: httpClient,
    fromResponse: fromResponse,
    fromStreamChunk: fromStreamChunk,
  );
}