89 lines
2.8 KiB
Dart
89 lines
2.8 KiB
Dart
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<String, Object?> 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<ProgressCandidate> candidates;
|
||
final String? answer;
|
||
final ProgressCandidate? request;
|
||
final List<String> activeTasks, completedTasks;
|
||
factory ProgressAnswer.fromJson(Map<String, Object?> j) {
|
||
final p = j['progress'] as Map<String, Object?>?;
|
||
return ProgressAnswer(
|
||
requiresSelection: j['requiresSelection']! as bool,
|
||
candidates: ((j['candidates'] as List?) ?? const [])
|
||
.map(
|
||
(e) =>
|
||
ProgressCandidate.fromJson(Map<String, Object?>.from(e as Map)),
|
||
)
|
||
.toList(),
|
||
answer: j['answer'] as String?,
|
||
request: j['request'] == null
|
||
? null
|
||
: ProgressCandidate.fromJson(
|
||
Map<String, Object?>.from(j['request']! as Map),
|
||
),
|
||
activeTasks: ((p?['activeTaskNames'] as List?) ?? const [])
|
||
.cast<String>(),
|
||
completedTasks: ((p?['completedTaskNames'] as List?) ?? const [])
|
||
.cast<String>(),
|
||
processEnded: p?['processEnded'] as bool? ?? false,
|
||
);
|
||
}
|
||
}
|
||
|
||
class LeaveProgressRepository {
|
||
LeaveProgressRepository({required this.client, required this.baseUrl});
|
||
final http.Client client;
|
||
final String baseUrl;
|
||
Future<ProgressAnswer> 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<String, Object?>,
|
||
);
|
||
}
|
||
}
|