Files
pipelines/app/lib/features/chat/domain/chat_automations.dart
T

52 lines
1.5 KiB
Dart

import '../../../core/api_gateway.dart';
/// The native provider's per-chat scheduled-work report. Missing fields remain
/// unknown; in particular, an absent count is never interpreted as zero.
class ChatAutomationStatus {
const ChatAutomationStatus({
this.supported = false,
this.count,
this.reason,
this.observedAt,
});
factory ChatAutomationStatus.fromReply(
JsonMap reply, {
required String expectedSessionId,
}) {
final value = reply['chat_automations'];
if (value is! Map) return const ChatAutomationStatus();
final map = value;
final reportedSession = '${map['session_id'] ?? ''}'.trim();
if (reportedSession.isEmpty || reportedSession != expectedSessionId) {
return const ChatAutomationStatus();
}
final count = map['active_count'];
final observed = map['observed_at_ms'];
final validCount = count is int && count >= 0 ? count : null;
DateTime? observedAt;
if (observed is int && observed > 0) {
try {
observedAt = DateTime.fromMillisecondsSinceEpoch(observed);
} on ArgumentError {
observedAt = null;
}
}
return ChatAutomationStatus(
supported: map['status'] == 'supported',
count: map['status'] == 'supported' ? validCount : null,
reason: '${map['reason'] ?? ''}'.trim().isEmpty
? null
: '${map['reason']}'.trim(),
observedAt: observedAt,
);
}
final bool supported;
final int? count;
final String? reason;
final DateTime? observedAt;
bool get unavailable => !supported;
}