Compare commits

..

2 Commits

Author SHA1 Message Date
Ponshu Developer b3d07f8794 chore: update download page to v1.0.22 2026-04-09 19:12:25 +09:00
Ponshu Developer 0d741c4cb0 fix: add retry + fallback for Gemini 503 UNAVAILABLE errors
- Retry up to 3 times with exponential backoff on 503/UNAVAILABLE
- Fall back to gemini-2.0-flash on final attempt
- Replace raw JSON error with user-friendly Japanese message

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-09 19:04:40 +09:00
2 changed files with 64 additions and 32 deletions

View File

@ -261,14 +261,9 @@ $extractedText
throw Exception('Gemini API Key is missing. Please set GEMINI_API_KEY.'); throw Exception('Gemini API Key is missing. Please set GEMINI_API_KEY.');
} }
final model = GenerativeModel( // : 503/UNAVAILABLE
model: 'gemini-2.5-flash', // FIXED MODEL NAME - DO NOT CHANGE without explicit user approval (confirmed working on 2026-01-17) const primaryModel = 'gemini-2.5-flash'; // FIXED - confirmed 2026-01-17
apiKey: apiKey, const fallbackModel = 'gemini-2.0-flash'; // 503
generationConfig: GenerationConfig(
responseMimeType: 'application/json',
temperature: 0.2, // 0.40.2 (2026-02-09)
),
);
// Prepare Prompt // Prepare Prompt
final promptText = customPrompt ?? ''' final promptText = customPrompt ?? '''
@ -296,37 +291,74 @@ $extractedText
null null
'''; ''';
// Prepare Content // Prepare Content parts ()
final contentParts = <Part>[TextPart(promptText)]; final contentParts = <Part>[TextPart(promptText)];
for (var path in imagePaths) { for (var path in imagePaths) {
//
final bytes = await File(path).readAsBytes(); final bytes = await File(path).readAsBytes();
contentParts.add(DataPart('image/jpeg', bytes)); contentParts.add(DataPart('image/jpeg', bytes));
} }
try { // 503 :
final response = await model.generateContent([Content.multi(contentParts)]); const maxRetries = 3;
final modelsToTry = [primaryModel, primaryModel, primaryModel, fallbackModel];
final jsonString = response.text; for (int attempt = 0; attempt <= maxRetries; attempt++) {
if (jsonString == null) throw Exception('Empty response from Gemini'); final modelName = modelsToTry[attempt];
final isLastAttempt = attempt == maxRetries;
final jsonMap = jsonDecode(jsonString); try {
final result = SakeAnalysisResult.fromJson(jsonMap); if (attempt > 0) {
final waitSec = attempt == maxRetries ? 2 : (attempt * 3);
debugPrint('Retry $attempt/$maxRetries (model: $modelName, wait: ${waitSec}s)...');
await Future.delayed(Duration(seconds: waitSec));
}
// 3. final model = GenerativeModel(
if (imagePaths.isNotEmpty) { model: modelName,
final imageHash = await AnalysisCacheService.computeCombinedHash(imagePaths); apiKey: apiKey,
await AnalysisCacheService.saveCache(imageHash, result); generationConfig: GenerationConfig(
// 4. v1.0.15: responseMimeType: 'application/json',
await AnalysisCacheService.registerBrandIndex(result.name, imageHash); temperature: 0.2,
),
);
final response = await model.generateContent([Content.multi(contentParts)]);
final jsonString = response.text;
if (jsonString == null) throw Exception('Empty response from Gemini');
final jsonMap = jsonDecode(jsonString);
final result = SakeAnalysisResult.fromJson(jsonMap);
// 3.
if (imagePaths.isNotEmpty) {
final imageHash = await AnalysisCacheService.computeCombinedHash(imagePaths);
await AnalysisCacheService.saveCache(imageHash, result);
// 4. v1.0.15:
await AnalysisCacheService.registerBrandIndex(result.name, imageHash);
}
if (attempt > 0) debugPrint('Succeeded on attempt $attempt (model: $modelName)');
return result;
} catch (e) {
final errStr = e.toString();
final is503 = errStr.contains('503') || errStr.contains('UNAVAILABLE') || errStr.contains('high demand');
debugPrint('Direct API Error (attempt $attempt, model: $modelName): $e');
if (isLastAttempt || !is503) {
// or 503
if (is503) {
throw Exception('AIサーバーが混雑しています。しばらく待ってから再試行してください。');
}
throw Exception('AI解析エラー(Direct): $e');
}
// 503
} }
return result;
} catch (e) {
debugPrint('Direct API Error: $e');
throw Exception('AI解析エラー(Direct): $e');
} }
//
throw Exception('AI解析に失敗しました。再試行してください。');
} }
} }

View File

@ -1,18 +1,18 @@
{ {
"date": "2026-04-06", "date": "2026-04-06",
"name": "Ponshu Room 1.0.21 (2026-04-06)", "name": "Ponshu Room 1.0.22 (2026-04-09)",
"version": "v1.0.21", "version": "v1.0.22",
"apks": { "apks": {
"eiji": { "eiji": {
"lite": { "lite": {
"url": "https://posimai-lab.tail72e846.ts.net/mai/ponshu-room-lite/releases/download/v1.0.21/ponshu_room_consumer_eiji.apk", "url": "https://posimai-lab.tail72e846.ts.net/mai/ponshu-room-lite/releases/download/v1.0.22/ponshu_room_consumer_eiji.apk",
"size_mb": 89, "size_mb": 89,
"filename": "ponshu_room_consumer_eiji.apk" "filename": "ponshu_room_consumer_eiji.apk"
} }
}, },
"maita": { "maita": {
"lite": { "lite": {
"url": "https://posimai-lab.tail72e846.ts.net/mai/ponshu-room-lite/releases/download/v1.0.21/ponshu_room_consumer_maita.apk", "url": "https://posimai-lab.tail72e846.ts.net/mai/ponshu-room-lite/releases/download/v1.0.22/ponshu_room_consumer_maita.apk",
"size_mb": 89, "size_mb": 89,
"filename": "ponshu_room_consumer_maita.apk" "filename": "ponshu_room_consumer_maita.apk"
} }