useDelegates method
ChatClientBuilder
useDelegates(
- ChatClientResponseHandler? getResponseFunc,
- ChatClientStreamingResponseHandler? getStreamingResponseFunc
Adds an anonymous delegating middleware based on one or both delegates.
If only getResponseFunc is supplied, it is used for streaming via
ChatResponse.toChatResponseUpdates.
If only getStreamingResponseFunc is supplied, it is used for
non-streaming via conversion to a ChatResponse.
Implementation
ChatClientBuilder useDelegates(
ChatClientResponseHandler? getResponseFunc,
ChatClientStreamingResponseHandler? getStreamingResponseFunc,
) {
if (getResponseFunc == null && getStreamingResponseFunc == null) {
throw ArgumentError(
'At least one of getResponseFunc or getStreamingResponseFunc must be non-null.',
);
}
final responseHandler = getResponseFunc ??
(messages, options, innerClient, cancellationToken) => _toChatResponse(
getStreamingResponseFunc!(
messages,
options,
innerClient,
cancellationToken,
),
);
final streamingHandler = getStreamingResponseFunc ??
(messages, options, innerClient, cancellationToken) async* {
final response = await getResponseFunc!(
messages,
options,
innerClient,
cancellationToken,
);
for (final update in response.toChatResponseUpdates()) {
yield update;
}
};
return useWithServices(
(innerClient, _) => AnonymousDelegatingChatClient(
innerClient,
responseHandler: responseHandler,
streamingResponseHandler: streamingHandler,
),
);
}