stream method

Stream<SseEvent> stream({
  1. required TuulOptions tuulOptions,
})

Initiates a streaming request and returns a Stream of SseEvent.

This is the preferred method for functional code or using Flutter's StreamBuilder. The stream yields SseEvent objects as they arrive from the server.

Implementation

Stream<SseEvent> stream({required TuulOptions tuulOptions}) async* {
  tuulOptions.validate();

  final rawStream = client.sseStream(
    "/generate",
    method: 'POST',
    headers: _headers(),
    body: tuulOptions.toJson(),
  );

  await for (final chunk in rawStream) {
    try {
      final decoded = jsonDecode(chunk);

      if (decoded is Map<String, dynamic>) {
        yield SseEvent.fromJson(decoded);
      }
    } catch (_) {
      // Ignore non-JSON chunks or comments
    }
  }
}