generate method
Generates a complete answer.
Throws an AgenticException subclass on failure — never a transport
exception, and never a provider-specific type.
Implementation
@override
Future<ChatResponse> generate(
ChatRequest request, {
AgenticContext? context,
}) async {
_requests.add(request);
if (latency > Duration.zero) {
await (context?.clock ?? const SystemClock()).delay(latency);
}
context?.throwIfCancelled();
final turn = _nextTurn();
if (turn.error case final error?) throw error;
if (turn.response case final response?) {
return response.copyWith(metadata: request.metadata);
}
// A chunk-scripted turn asked for by a non-streaming caller: assemble it,
// so the same script serves both modes.
final builder = ChatResponseBuilder(modelId: info.id)
..addAll(turn.chunks ?? const <ChatChunk>[]);
return builder.build();
}