Files
life-helper/app/lib/state/ai_providers.dart
joungmin 9a9eb2abd5 [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>
2026-06-12 15:18:08 +09:00

194 lines
5.9 KiB
Dart

import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../data/ai/llm_service.dart';
import '../data/ai/model_lifecycle.dart';
import '../data/db/app_database.dart' as drift;
import '../domain/ai/frame_candidate.dart';
import '../domain/ai/suggest_frame.dart';
import '../domain/models/frame_pattern.dart';
import 'providers.dart';
/// Gemma 4 E2B instruction-tuned LiteRT-LM checkpoint (#218 OQ-1 resolved).
/// Hosted on HuggingFace `litert-community/gemma-4-E2B-it-litert-lm`.
/// File ≈ 2.41GB; SHA-256 pinned for integrity check.
///
/// Tests / placeholder builds may override `modelLifecycleProvider` with
/// fixture URLs. Production builds optionally inject a private mirror via
/// `--dart-define=GEMMA_MODEL_URL=...` (see main.dart).
const _kModelUrl =
'https://huggingface.co/litert-community/gemma-4-E2B-it-litert-lm/resolve/main/gemma-4-E2B-it.litertlm';
const _kModelSha256 =
'181938105e0eefd105961417e8da75903eacda102c4fce9ce90f50b97139a63c';
final modelLifecycleProvider = Provider<ModelLifecycle>((ref) {
return ModelLifecycle(
meta: ref.watch(metaDaoProvider),
config: ModelConfig(
url: Uri.parse(_kModelUrl),
expectedSha256: _kModelSha256,
),
);
});
/// Read-only opt-in state. Default OFF; persisted in `meta_kv`.
final aiSettingsProvider = FutureProvider<bool>((ref) async {
final meta = ref.watch(metaDaoProvider);
final v = await meta.find(AiMetaKeys.optIn);
return v == 'true';
});
/// Toggles opt-in. On opt-out, purges model file via [ModelLifecycle.purge].
/// On opt-in, kicks off `ModelDownloadController.start()` so AC2 (progress UI)
/// has a stream to subscribe to.
class AiSettingsController {
AiSettingsController(this.ref);
final Ref ref;
Future<int> setOptIn(bool value) async {
final meta = ref.read(metaDaoProvider);
if (value) {
await meta.put(AiMetaKeys.optIn, 'true');
ref.invalidate(aiSettingsProvider);
ref.invalidate(modelAvailabilityProvider);
// AC2: opt-in triggers download stream so Settings UI can render
// progress + pause/resume. Fire-and-forget; controller emits states.
ref.read(modelDownloadControllerProvider.notifier).start();
return 0;
}
// opt-out: cancel any in-flight download, then purge.
ref.read(modelDownloadControllerProvider.notifier).cancel();
final freed = await ref.read(modelLifecycleProvider).purge();
await meta.put(AiMetaKeys.optIn, 'false');
ref.invalidate(aiSettingsProvider);
ref.invalidate(modelAvailabilityProvider);
return freed;
}
}
final aiSettingsControllerProvider = Provider<AiSettingsController>((ref) {
return AiSettingsController(ref);
});
/// AC2: streams DownloadProgress + supports pause/resume/cancel.
/// State `null` means idle (no active subscription).
class ModelDownloadController extends StateNotifier<DownloadProgress?> {
ModelDownloadController(this.ref) : super(null);
final Ref ref;
StreamSubscription<DownloadProgress>? _sub;
void start() {
cancel();
final lc = ref.read(modelLifecycleProvider);
_sub = lc.download().listen(
(p) {
state = p;
if (p.state == DownloadState.completed) {
ref.invalidate(modelAvailabilityProvider);
}
},
onError: (Object e) {
state = DownloadProgress(
bytesReceived: state?.bytesReceived ?? 0,
totalBytes: state?.totalBytes ?? -1,
state: DownloadState.failed,
errorMessage: e.toString(),
);
},
onDone: () {
_sub = null;
},
);
}
/// Pauses by cancelling the subscription. .tmp file + meta_kv preserved so
/// `start()` resumes via HTTP Range header.
void pause() {
_sub?.cancel();
_sub = null;
final cur = state;
if (cur != null && cur.state != DownloadState.completed) {
state = DownloadProgress(
bytesReceived: cur.bytesReceived,
totalBytes: cur.totalBytes,
state: DownloadState.paused,
);
}
}
void resume() => start();
void cancel() {
_sub?.cancel();
_sub = null;
state = null;
}
@override
void dispose() {
_sub?.cancel();
super.dispose();
}
}
final modelDownloadControllerProvider =
StateNotifierProvider<ModelDownloadController, DownloadProgress?>(
(ref) => ModelDownloadController(ref),
);
final modelAvailabilityProvider =
FutureProvider<ModelAvailability>((ref) async {
final lc = ref.watch(modelLifecycleProvider);
return lc.checkAvailability();
});
/// Loads FramePatterns from DB and converts to domain models.
final framePatternsProvider = FutureProvider<List<FramePatternModel>>(
(ref) async {
final db = ref.watch(appDatabaseProvider);
final rows = await db.select(db.framePatterns).get();
return rows.map(_toDomain).toList(growable: false);
},
);
FramePatternModel _toDomain(drift.FramePattern r) => FramePatternModel(
id: r.id,
domain: r.domain,
avoidanceKeyword: r.avoidanceKeyword,
l0Example: r.l0Example,
l1SimpleReplace: r.l1SimpleReplace,
l2Suggestion: r.l2Suggestion,
l3Identity: r.l3Identity,
);
/// Singleton LLM service for the app. v1 starts unloaded; first
/// [suggestFrame] triggers `.load()` via the dialog. Override in tests with
/// `MockLlmService`.
final llmServiceProvider = Provider<LlmService>((ref) {
throw UnimplementedError(
'llmServiceProvider must be overridden (Mock in tests, '
'GemmaLlmService after OQ-1 in production).',
);
});
/// `family` param wraps a SuggestFrameInput. Loads model lazily before
/// calling suggestFrame.
final frameSuggestionsProvider = FutureProvider.autoDispose
.family<List<FrameCandidate>, SuggestFrameInput>((ref, input) async {
final llm = ref.watch(llmServiceProvider);
final patterns = await ref.watch(framePatternsProvider.future);
if (!llm.isLoaded) {
try {
await llm.load();
} catch (_) {
return const [];
}
}
return suggestFrame(
input,
llm: llm,
framePatterns: patterns,
);
});