sendMessage method
override
Send messages to the AI proxy. The proxy builds the system prompt
server-side — systemPrompt is ignored (kept for interface compatibility).
Implementation
@override
Future<String> sendMessage({
required String systemPrompt,
required List<Map<String, String>> messages,
int maxTokens = 8192,
}) async {
dev.log(
'Proxy request: provider=$providerName, mode=$mode, '
'messages=${messages.length}, maxTokens=$maxTokens',
name: 'ProxyAiDatasource',
);
try {
final credential = await credentialProvider();
if (credential == null || credential.isEmpty) {
throw AiApiException(
type: AiApiErrorType.auth,
message: '${backendProvider.displayName} credential not configured.',
);
}
final response = await _dio.post(
functionUrl,
options: Options(headers: headersBuilder()),
data: {
'provider': providerName,
'credential': credential,
'mode': mode,
if (topicId != null) 'topicId': topicId,
'messages': messages,
'isValidation': isValidation,
if (validationTarget != null) 'validationTarget': validationTarget,
'isCardGeneration': isCardGeneration,
'maxTokens': maxTokens,
'stream': false,
},
);
final data = response.data as Map<String, dynamic>;
// Log any validation warnings.
final validation = data['_validation'] as List<dynamic>?;
if (validation != null && validation.isNotEmpty) {
dev.log(
'Output validation flags: $validation',
name: 'ProxyAiDatasource',
);
}
return data['text'] as String;
} on DioException catch (e) {
throw _mapError(e);
}
}