generateStream<C> function

ActionStream<GenerateResponseChunk, GenerateResponse> generateStream<C>({
  1. required Model<C> model,
  2. String? prompt,
  3. List<Message>? messages,
  4. C? config,
  5. List<Tool>? tools,
  6. String? toolChoice,
  7. bool? returnToolRequests,
  8. int? maxTurns,
  9. JsonExtensionType? outputSchema,
  10. String? outputFormat,
  11. bool? outputConstrained,
  12. String? outputInstructions,
  13. bool? outputNoInstructions,
  14. String? outputContentType,
  15. Map<String, dynamic>? context,
})

Implementation

ActionStream<GenerateResponseChunk, GenerateResponse> generateStream<C>({
  required Model<C> model,
  String? prompt,
  List<Message>? messages,
  C? config,
  List<Tool>? tools,
  String? toolChoice,
  bool? returnToolRequests,
  int? maxTurns,
  JsonExtensionType? outputSchema,
  String? outputFormat,
  bool? outputConstrained,
  String? outputInstructions,
  bool? outputNoInstructions,
  String? outputContentType,
  Map<String, dynamic>? context,
}) {
  final streamController = StreamController<GenerateResponseChunk>();
  final actionStream = ActionStream<GenerateResponseChunk, GenerateResponse>(
    streamController.stream,
  );

  generate(
        prompt: prompt,
        messages: messages,
        model: model,
        config: config,
        tools: tools,
        toolChoice: toolChoice,
        returnToolRequests: returnToolRequests,
        maxTurns: maxTurns,
        outputSchema: outputSchema,
        outputFormat: outputFormat,
        outputConstrained: outputConstrained,
        outputInstructions: outputInstructions,
        outputNoInstructions: outputNoInstructions,
        outputContentType: outputContentType,
        context: context,
        onChunk: (chunk) {
          if (streamController.isClosed) return;
          streamController.add(chunk);
        },
      )
      .then((result) {
        actionStream.setResult(result);
        if (!streamController.isClosed) {
          streamController.close();
        }
      })
      .catchError((e, s) {
        actionStream.setError(e, s);
        if (!streamController.isClosed) {
          streamController.addError(e, s);
          streamController.close();
        }
      });

  return actionStream;
}