설계서대로 구현. flutter_gemma 실제 통합은 OQ-1 (모델 URL+SHA) 확정 후.
v1은 LlmService 추상 + ModelLifecycle (다운로드/SHA/purge) + Riverpod
providers + 다이얼로그 + Settings 화면까지. main.dart 가 MockLlmService 를
override 해 모든 경로가 graceful (suggest 결과는 빈 리스트).
추가:
- lib/data/ai/{llm_service,gemma_llm_service,model_lifecycle}.dart
- lib/domain/ai/{frame_candidate,few_shot_builder,parse_response,suggest_frame}.dart
- lib/state/ai_providers.dart (aiSettings + modelAvailability + frameSuggestions)
- lib/ui/screens/settings_screen.dart (opt-in 토글 + 모델 상태 표시)
- lib/ui/widgets/frame_suggestion_dialog.dart (후보 3개 카드 + 다시 시도)
- HabitCreateScreen: "AI 제안" 버튼 (opt-in + ready 일 때만 노출)
- MetaDao.remove(key) 추가 (purge 용)
테스트 31개 신규 추가 (총 62개 통과):
- test/domain/ai/{suggest_frame, few_shot_builder, parse_response}_test.dart
- test/data/ai/model_lifecycle_test.dart (download/SHA/purge/availability)
flutter analyze 0 issue, flutter build apk --debug 통과.
Refs #215
84 lines
2.4 KiB
Dart
84 lines
2.4 KiB
Dart
import '../../data/ai/llm_service.dart';
|
|
import '../frame/validate_frame_level.dart';
|
|
import '../models/frame_pattern.dart';
|
|
import 'few_shot_builder.dart';
|
|
import 'frame_candidate.dart';
|
|
import 'parse_response.dart';
|
|
|
|
/// JSON schema (function-calling parameters) for the model output.
|
|
const Map<String, dynamic> kFrameCandidatesSchema = {
|
|
'type': 'object',
|
|
'properties': {
|
|
'candidates': {
|
|
'type': 'array',
|
|
'minItems': 1,
|
|
'maxItems': 3,
|
|
'items': {
|
|
'type': 'object',
|
|
'properties': {
|
|
'level': {'type': 'string', 'enum': ['L2', 'L3']},
|
|
'framed_text': {'type': 'string', 'maxLength': 120},
|
|
'confidence': {'type': 'number', 'minimum': 0, 'maximum': 1},
|
|
'source_pattern_id': {'type': 'string'},
|
|
},
|
|
'required': ['level', 'framed_text'],
|
|
},
|
|
},
|
|
},
|
|
'required': ['candidates'],
|
|
};
|
|
|
|
const Duration _defaultTimeout = Duration(seconds: 10);
|
|
const int _maxRawTextLength = 200;
|
|
|
|
/// Main domain function (#215 §A). Pure-ish: depends only on injected
|
|
/// [llm] and [framePatterns]. Never throws — failure returns an empty list
|
|
/// so callers (UI) can decide messaging (graceful degradation, AC-9).
|
|
Future<List<FrameCandidate>> suggestFrame(
|
|
SuggestFrameInput input, {
|
|
required LlmService llm,
|
|
required List<FramePatternModel> framePatterns,
|
|
Duration timeout = _defaultTimeout,
|
|
}) async {
|
|
final raw = input.rawText.trim();
|
|
if (raw.isEmpty || raw.length > _maxRawTextLength) {
|
|
return const [];
|
|
}
|
|
|
|
final prompt = buildFewShotPrompt(input, framePatterns);
|
|
|
|
Map<String, dynamic> json;
|
|
try {
|
|
json = await llm
|
|
.generateStructured(prompt, kFrameCandidatesSchema)
|
|
.timeout(timeout);
|
|
} catch (_) {
|
|
// Timeout / StateError / FormatException / anything else: graceful.
|
|
return const [];
|
|
}
|
|
|
|
List<FrameCandidate> candidates;
|
|
try {
|
|
candidates = parseFrameCandidates(json);
|
|
} on FormatException {
|
|
return const [];
|
|
}
|
|
|
|
// Drop L0/L1 + avoidance-violating via validateFrameLevel.
|
|
final validated = <FrameCandidate>[];
|
|
for (final c in candidates) {
|
|
final result = validateFrameLevel(
|
|
FrameInput(
|
|
level: c.level,
|
|
framedText: c.framedText,
|
|
originalText: input.rawText,
|
|
),
|
|
knownPatterns: framePatterns,
|
|
);
|
|
if (result.status == FrameValidationStatus.reject) continue;
|
|
validated.add(c);
|
|
if (validated.length >= 3) break;
|
|
}
|
|
return validated;
|
|
}
|