transientRetryStreamFunction function
StreamFunction
transientRetryStreamFunction(
- StreamFunction inner, {
- int maxAttempts = 3,
- Duration delay = const Duration(seconds: 5),
Wraps inner with the transient-network retry policy: up to
maxAttempts total attempts (1 = no retry), delay between them.
Implementation
StreamFunction transientRetryStreamFunction(
StreamFunction inner, {
int maxAttempts = 3,
Duration delay = const Duration(seconds: 5),
}) {
assert(maxAttempts >= 1, 'maxAttempts must be at least 1');
return (Model model, Context context, {CancelToken? cancelToken}) {
final out = AssistantMessageEventStream();
unawaited(
_drive(out, inner, model, context, cancelToken, maxAttempts, delay)
.catchError((Object error) {
// Defensive (providers never throw; a fake in tests might).
out.push(
ErrorEvent(
reason: StopReason.error,
error: AssistantMessage(
content: const [],
api: model.api,
provider: model.provider,
model: model.id,
usage: Usage.zero,
stopReason: StopReason.error,
errorMessage: '$error',
timestamp: DateTime.now(),
),
),
);
})
.whenComplete(out.end),
);
return out;
};
}