[Developer] #218 Real Gemma 4 E2B integration via flutter_gemma 0.16.5
Implements the OQ-1 follow-up to #215 v0.2.0: replace the placeholder GemmaLlmService stub with a real flutter_gemma 0.16.5 backend driving Gemma 4 E2B (litert-community/gemma-4-E2B-it-litert-lm, 2.41GB). Highlights: - GemmaLlmService.load → FlutterGemma.initialize + installModel.fromFile + getActiveModel; idempotent + FileSystemException on missing file. - generateStructured uses Gemma 4 native function calling via createChat(tools: [Tool(...)], toolChoice: required). Stream parsed by collectFunctionCall — first FCR wins, ParallelFCR first-call wins, TextResponse/ThinkingResponse skipped, errors sanitized to prevent prompt leakage. - main.dart wires _LazyLlmService adapter that resolves to GemmaLlmService when ModelLifecycle reports ready, MockLlmService otherwise. - ai_providers.dart pins real model URL + SHA-256 (181938...39a63c). - F2 hardening: ModelLifecycle.purge wraps each delete + meta remove in try/catch so a single OS-level flake cannot block opt-out. - Android: INTERNET / FOREGROUND_SERVICE / POST_NOTIFICATIONS permissions + R8 proguard-rules.pro keeping MediaPipe / LiteRT / TFLite / protobuf JNI entry points (release builds otherwise crash on first inference). Design-First: fn-gemma_llm_service.md updated to v2 — §C (_appendSchemaInstruction) deprecated after reading flutter_gemma 0.16.5 source (Gemma 4 SDK injects tool declarations via template; prompt-side append would double-wrap). Tests: - 10 new unit tests for collectFunctionCall covering all 8 fn-spec cases + 2 ParallelFunctionCallResponse paths. - All 81 existing tests still pass. - flutter analyze: 0 issues. Refs #218 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import 'data/ai/gemma_llm_service.dart';
|
||||
import 'data/ai/llm_service.dart';
|
||||
import 'data/ai/model_lifecycle.dart';
|
||||
import 'data/db/daos/meta_dao.dart';
|
||||
import 'state/ai_providers.dart';
|
||||
import 'state/providers.dart';
|
||||
import 'ui/screens/habit_list_screen.dart';
|
||||
@@ -12,15 +15,62 @@ Future<void> main() async {
|
||||
runApp(ProviderScope(
|
||||
overrides: [
|
||||
appDatabaseProvider.overrideWithValue(db),
|
||||
// OQ-1 pending: production-ready GemmaLlmService is wired here once
|
||||
// model URL + SHA are pinned and flutter_gemma is added. Until then,
|
||||
// MockLlmService keeps the app graceful (suggestFrame returns []).
|
||||
llmServiceProvider.overrideWithValue(MockLlmService()),
|
||||
// #218: real GemmaLlmService when model file is on disk + verified,
|
||||
// MockLlmService otherwise. The provider is read lazily by the frame
|
||||
// suggestion flow, so the resolution is dynamic per call.
|
||||
llmServiceProvider.overrideWith((ref) {
|
||||
return _LazyLlmService(
|
||||
lifecycle: ref.watch(modelLifecycleProvider),
|
||||
meta: ref.watch(metaDaoProvider),
|
||||
);
|
||||
}),
|
||||
],
|
||||
child: const LifeHelperApp(),
|
||||
));
|
||||
}
|
||||
|
||||
/// Adapter that lazily resolves between [GemmaLlmService] (when the
|
||||
/// model file exists + meta is intact) and [MockLlmService] (fallback,
|
||||
/// graceful empty candidates). Keeps the rest of the app unaware of
|
||||
/// the difference — `suggestFrame` only sees [LlmService].
|
||||
class _LazyLlmService implements LlmService {
|
||||
_LazyLlmService({required this.lifecycle, required this.meta});
|
||||
final ModelLifecycle lifecycle;
|
||||
final MetaDao meta;
|
||||
LlmService? _delegate;
|
||||
|
||||
Future<LlmService> _resolve() async {
|
||||
if (_delegate != null) return _delegate!;
|
||||
final avail = await lifecycle.checkAvailability();
|
||||
final path = await meta.find(AiMetaKeys.modelPath);
|
||||
if (avail == ModelAvailability.ready && path != null) {
|
||||
_delegate = GemmaLlmService(modelPath: path);
|
||||
} else {
|
||||
_delegate = MockLlmService();
|
||||
}
|
||||
return _delegate!;
|
||||
}
|
||||
|
||||
@override
|
||||
bool get isLoaded => _delegate?.isLoaded ?? false;
|
||||
|
||||
@override
|
||||
Future<void> load() async => (await _resolve()).load();
|
||||
|
||||
@override
|
||||
Future<void> unload() async {
|
||||
final d = _delegate;
|
||||
if (d != null) await d.unload();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Map<String, dynamic>> generateStructured(
|
||||
String prompt,
|
||||
Map<String, dynamic> schema,
|
||||
) async =>
|
||||
(await _resolve()).generateStructured(prompt, schema);
|
||||
}
|
||||
|
||||
class LifeHelperApp extends StatelessWidget {
|
||||
const LifeHelperApp({super.key});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user