import 'dart:convert'; import 'package:http/http.dart' as http; class ProgressCandidate { const ProgressCandidate({ required this.id, required this.type, required this.status, required this.startsAt, required this.endsAt, }); final String id, type, status; final DateTime startsAt, endsAt; factory ProgressCandidate.fromJson(Map j) => ProgressCandidate( id: j['id']! as String, type: j['type']! as String, status: j['status']! as String, startsAt: DateTime.parse(j['startsAt']! as String), endsAt: DateTime.parse(j['endsAt']! as String), ); } class ProgressAnswer { const ProgressAnswer({ required this.requiresSelection, required this.candidates, this.answer, this.request, this.activeTasks = const [], this.completedTasks = const [], this.processEnded = false, }); final bool requiresSelection, processEnded; final List candidates; final String? answer; final ProgressCandidate? request; final List activeTasks, completedTasks; factory ProgressAnswer.fromJson(Map j) { final p = j['progress'] as Map?; return ProgressAnswer( requiresSelection: j['requiresSelection']! as bool, candidates: ((j['candidates'] as List?) ?? const []) .map( (e) => ProgressCandidate.fromJson(Map.from(e as Map)), ) .toList(), answer: j['answer'] as String?, request: j['request'] == null ? null : ProgressCandidate.fromJson( Map.from(j['request']! as Map), ), activeTasks: ((p?['activeTaskNames'] as List?) ?? const []) .cast(), completedTasks: ((p?['completedTaskNames'] as List?) ?? const []) .cast(), processEnded: p?['processEnded'] as bool? ?? false, ); } } class LeaveProgressRepository { LeaveProgressRepository({required this.client, required this.baseUrl}); final http.Client client; final String baseUrl; Future ask(String text, {String? selectedRequestId}) async { final response = await client.post( Uri.parse('$baseUrl/ai/leave-progress-answers'), headers: const { 'Content-Type': 'application/json', 'Accept': 'application/json', }, body: jsonEncode({ 'text': text, 'timezone': 'Asia/Shanghai', 'selectedRequestId': ?selectedRequestId, }), ); if (response.statusCode < 200 || response.statusCode >= 300) { throw Exception('查询失败(${response.statusCode})'); } return ProgressAnswer.fromJson( jsonDecode(response.body) as Map, ); } }