Neue Cloud Function diagnosePlant: Foto + optionale Art an Claude, deutsches JSON-Ergebnis (Befund/Ursache/Behandlung/Vorbeugung). App: PlantDiagnosisService + Untersuchen-Sektion im Pflanzen-Detail mit Ergebnis-Bottom-Sheet und KI-Disclaimer. askClaude max_tokens 1024→2048 (adaptives Denken zählt mit ins Limit). Deploy steht aus. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
58 lines
1.9 KiB
Dart
58 lines
1.9 KiB
Dart
import 'dart:convert';
|
||
import 'dart:typed_data';
|
||
|
||
import 'package:cloud_functions/cloud_functions.dart';
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
|
||
/// Ergebnis der KI-Krankheits-Diagnose (Cloud Function diagnosePlant).
|
||
class DiagnosisResult {
|
||
const DiagnosisResult({
|
||
required this.healthy,
|
||
required this.summary,
|
||
required this.details,
|
||
required this.treatment,
|
||
required this.prevention,
|
||
});
|
||
|
||
final bool healthy;
|
||
final String summary;
|
||
final String details;
|
||
final String treatment;
|
||
final String prevention;
|
||
}
|
||
|
||
class PlantDiagnosisService {
|
||
PlantDiagnosisService(this._functionsGetter);
|
||
|
||
// Lazy, damit Widget-Tests ohne initialisiertes Firebase auskommen —
|
||
// FirebaseFunctions.instanceFor wirft sonst schon beim Erzeugen.
|
||
final FirebaseFunctions Function() _functionsGetter;
|
||
|
||
/// Schickt das Foto an die Cloud Function (Claude) – dauert einige Sekunden.
|
||
/// [speciesHint] ist die bekannte Art der Pflanze und hilft der Diagnose.
|
||
Future<DiagnosisResult> diagnose(
|
||
Uint8List imageBytes, {
|
||
String? speciesHint,
|
||
}) async {
|
||
final callable = _functionsGetter().httpsCallable('diagnosePlant');
|
||
final response = await callable.call<Map<String, dynamic>>({
|
||
'imageBase64': base64Encode(imageBytes),
|
||
if (speciesHint != null && speciesHint.trim().isNotEmpty)
|
||
'speciesHint': speciesHint.trim(),
|
||
});
|
||
final data = response.data;
|
||
return DiagnosisResult(
|
||
healthy: data['healthy'] as bool? ?? false,
|
||
summary: data['summary'] as String? ?? '',
|
||
details: data['details'] as String? ?? '',
|
||
treatment: data['treatment'] as String? ?? '',
|
||
prevention: data['prevention'] as String? ?? '',
|
||
);
|
||
}
|
||
}
|
||
|
||
final plantDiagnosisServiceProvider = Provider<PlantDiagnosisService>((ref) {
|
||
return PlantDiagnosisService(
|
||
() => FirebaseFunctions.instanceFor(region: 'europe-west3'),
|
||
);
|
||
});
|