QA 1차 (커밋 6ab4c0d 검증) 에서 3건 AC 미충족 → 03-Developer 반려.
본 커밋은 그 3건을 해결한다.
AC6 — _AiSuggestButton 가시성 분기 분리:
- optIn=false → 숨김 유지
- optIn=true && !ready → 노출 + disabled + Tooltip("AI 도움을 먼저 켜주세요")
- optIn=true && ready → enabled
AC5 보완: 후보의 c.level 직접 전달 (이전 "나는" 휴리스틱 제거).
FrameSuggestionDialog.show 반환 타입 String? → FrameCandidate?.
AC4 — L2:2 + L3:1 분포 강제:
- few_shot prompt 에 "정확히 L2 2개 + L3 1개" 명시
- suggestFrame 결과를 _shapeDistribution(l2Quota=2, l3Quota=1) 로 후처리
- 부족분은 패딩 X (graceful: UI 가 더 적은 카드만 표시)
AC2 — 다운로드 진행률 + 일시정지/재개 UI:
- ModelDownloadController (StateNotifier<DownloadProgress?>)
· start() / pause() / resume() / cancel()
· pause() 는 subscription 만 cancel, .tmp + meta_kv 유지 → resume 시 Range header 로 이어받음
- AiSettingsController.setOptIn(true) → controller.start() 자동 호출
- SettingsScreen 에 _DownloadProgressTile 추가
· LinearProgressIndicator + bytes/total + 일시정지/재개/다시 시도 토글
회귀 테스트 9건 신규:
- test/ui/ai_suggest_button_visibility_test.dart (4): AC6 4상태 (hidden / disabled+tooltip × missing/downloading / enabled)
- test/state/model_download_controller_test.dart (3): opt-in→start, pause→paused, cancel→idle
- test/domain/ai/suggest_frame_test.dart (+3): AC4 분포 케이스 3개 (기존 take(3) 테스트 대체)
검증:
- flutter analyze → No issues found
- flutter test → 71 tests pass (62 → 71, +9 신규)
- flutter build apk --debug → 성공 (8.8s)
OQ-1 (모델 URL+SHA) 미해결 유지. MockLlmService 기본 주입 + placeholder URL 다운로드는 여전히 실패하지만, UI/스트림 wiring 은 모두 검증됨.
Refs #215
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
108 lines
3.2 KiB
Dart
108 lines
3.2 KiB
Dart
import '../../data/ai/llm_service.dart';
|
|
import '../frame/validate_frame_level.dart';
|
|
import '../models/frame_pattern.dart';
|
|
import '../models/habit.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);
|
|
}
|
|
// AC4: Enforce L2:2 + L3:1 distribution. Take L2 first (up to 2), then L3
|
|
// (up to 1). If a slot is short, leave it short rather than pad with the
|
|
// other level — graceful: caller sees fewer cards but never a wrong shape.
|
|
return _shapeDistribution(validated, l2Quota: 2, l3Quota: 1);
|
|
}
|
|
|
|
List<FrameCandidate> _shapeDistribution(
|
|
List<FrameCandidate> all, {
|
|
required int l2Quota,
|
|
required int l3Quota,
|
|
}) {
|
|
final out = <FrameCandidate>[];
|
|
var l2Taken = 0;
|
|
var l3Taken = 0;
|
|
for (final c in all) {
|
|
if (c.level == FrameLevel.l2 && l2Taken < l2Quota) {
|
|
out.add(c);
|
|
l2Taken += 1;
|
|
} else if (c.level == FrameLevel.l3 && l3Taken < l3Quota) {
|
|
out.add(c);
|
|
l3Taken += 1;
|
|
}
|
|
if (l2Taken >= l2Quota && l3Taken >= l3Quota) break;
|
|
}
|
|
return out;
|
|
}
|