feat: 统一自动化任务调度配置并增强聊天流恢复

This commit is contained in:
qzl
2026-03-24 18:19:33 +08:00
parent 23359c2d01
commit 389f5248fc
30 changed files with 1144 additions and 888 deletions
@@ -92,15 +92,95 @@ class MessageContextConfigModel {
}
}
class ScheduleRunAtModel {
final int hour;
final int minute;
ScheduleRunAtModel({required this.hour, required this.minute});
factory ScheduleRunAtModel.fromJson(Map<String, dynamic>? json) {
if (json == null) {
return ScheduleRunAtModel(hour: 8, minute: 0);
}
return ScheduleRunAtModel(
hour: _parseInt(json['hour'], field: 'hour', fallback: 8),
minute: _parseInt(json['minute'], field: 'minute', fallback: 0),
);
}
Map<String, dynamic> toJson() => {'hour': hour, 'minute': minute};
ScheduleRunAtModel copyWith({int? hour, int? minute}) {
return ScheduleRunAtModel(
hour: hour ?? this.hour,
minute: minute ?? this.minute,
);
}
}
class ScheduleConfigModel {
final String type;
final ScheduleRunAtModel runAt;
final List<int>? weekdays;
ScheduleConfigModel({required this.type, required this.runAt, this.weekdays});
factory ScheduleConfigModel.fromJson(Map<String, dynamic>? json) {
if (json == null) {
return ScheduleConfigModel(
type: 'daily',
runAt: ScheduleRunAtModel(hour: 8, minute: 0),
);
}
final type = _parseString(json['type'], field: 'type', fallback: 'daily');
final dynamic weekdaysRaw = json['weekdays'];
List<int>? weekdays;
if (weekdaysRaw is List) {
weekdays = weekdaysRaw
.map((item) => _parseInt(item, field: 'weekdays', fallback: 1))
.toList();
}
return ScheduleConfigModel(
type: type,
runAt: ScheduleRunAtModel.fromJson(
json['run_at'] as Map<String, dynamic>?,
),
weekdays: type.toLowerCase() == 'weekly' ? weekdays ?? [1] : null,
);
}
Map<String, dynamic> toJson() {
final map = <String, dynamic>{'type': type, 'run_at': runAt.toJson()};
if (weekdays != null) {
map['weekdays'] = weekdays;
}
return map;
}
ScheduleConfigModel copyWith({
String? type,
ScheduleRunAtModel? runAt,
List<int>? weekdays,
}) {
return ScheduleConfigModel(
type: type ?? this.type,
runAt: runAt ?? this.runAt,
weekdays: weekdays ?? this.weekdays,
);
}
}
class AutomationJobConfigModel {
final String inputTemplate;
final List<String> enabledTools;
final MessageContextConfigModel context;
final ScheduleConfigModel schedule;
AutomationJobConfigModel({
required this.inputTemplate,
required this.enabledTools,
required this.context,
required this.schedule,
});
factory AutomationJobConfigModel.fromJson(Map<String, dynamic>? json) {
@@ -109,6 +189,7 @@ class AutomationJobConfigModel {
inputTemplate: '',
enabledTools: const [],
context: MessageContextConfigModel.fromJson(null),
schedule: ScheduleConfigModel.fromJson(null),
);
}
return AutomationJobConfigModel(
@@ -126,6 +207,11 @@ class AutomationJobConfigModel {
json['context'] as Map<String, dynamic>?,
)
: MessageContextConfigModel.fromJson(null),
schedule: json['schedule'] != null
? ScheduleConfigModel.fromJson(
json['schedule'] as Map<String, dynamic>?,
)
: ScheduleConfigModel.fromJson(null),
);
}
@@ -133,17 +219,20 @@ class AutomationJobConfigModel {
'input_template': inputTemplate,
'enabled_tools': enabledTools,
'context': context.toJson(),
'schedule': schedule.toJson(),
};
AutomationJobConfigModel copyWith({
String? inputTemplate,
List<String>? enabledTools,
MessageContextConfigModel? context,
ScheduleConfigModel? schedule,
}) {
return AutomationJobConfigModel(
inputTemplate: inputTemplate ?? this.inputTemplate,
enabledTools: enabledTools ?? this.enabledTools,
context: context ?? this.context,
schedule: schedule ?? this.schedule,
);
}
}
@@ -153,8 +242,6 @@ class AutomationJobModel {
final String ownerId;
final String? bootstrapKey;
final String title;
final String scheduleType;
final String runAt;
final String timezone;
final String status;
final bool isSystem;
@@ -169,8 +256,6 @@ class AutomationJobModel {
required this.ownerId,
this.bootstrapKey,
required this.title,
required this.scheduleType,
required this.runAt,
required this.timezone,
required this.status,
required this.isSystem,
@@ -193,16 +278,6 @@ class AutomationJobModel {
fallback: '',
),
title: _parseString(json['title'], field: 'title', fallback: ''),
scheduleType: _parseString(
json['schedule_type'],
field: 'schedule_type',
fallback: 'daily',
),
runAt: _parseString(
json['run_at'],
field: 'run_at',
fallback: '08:00:00',
),
timezone: _parseString(
json['timezone'],
field: 'timezone',
@@ -263,8 +338,6 @@ class AutomationJobModel {
'owner_id': ownerId,
'bootstrap_key': bootstrapKey,
'title': title,
'schedule_type': scheduleType,
'run_at': runAt,
'timezone': timezone,
'status': status,
'is_system': isSystem,
@@ -280,8 +353,6 @@ class AutomationJobModel {
String? ownerId,
String? bootstrapKey,
String? title,
String? scheduleType,
String? runAt,
String? timezone,
String? status,
bool? isSystem,
@@ -296,8 +367,6 @@ class AutomationJobModel {
ownerId: ownerId ?? this.ownerId,
bootstrapKey: bootstrapKey ?? this.bootstrapKey,
title: title ?? this.title,
scheduleType: scheduleType ?? this.scheduleType,
runAt: runAt ?? this.runAt,
timezone: timezone ?? this.timezone,
status: status ?? this.status,
isSystem: isSystem ?? this.isSystem,
@@ -311,9 +380,9 @@ class AutomationJobModel {
bool get isActive => status.toLowerCase() == 'active';
bool get isDaily => scheduleType.toLowerCase() == 'daily';
bool get isDaily => config.schedule.type.toLowerCase() == 'daily';
bool get isWeekly => scheduleType.toLowerCase() == 'weekly';
bool get isWeekly => config.schedule.type.toLowerCase() == 'weekly';
}
class AutomationJobListResponse {
@@ -342,16 +411,12 @@ class AutomationJobListResponse {
class AutomationJobCreateRequest {
final String title;
final String scheduleType;
final String runAt;
final String timezone;
final String status;
final AutomationJobConfigModel config;
AutomationJobCreateRequest({
required this.title,
required this.scheduleType,
required this.runAt,
required this.timezone,
required this.status,
required this.config,
@@ -359,8 +424,6 @@ class AutomationJobCreateRequest {
Map<String, dynamic> toJson() => {
'title': title,
'schedule_type': scheduleType,
'run_at': runAt,
'timezone': timezone,
'status': status,
'config': config.toJson(),
@@ -371,11 +434,13 @@ class AutomationJobConfigPatchModel {
final String? inputTemplate;
final List<String>? enabledTools;
final MessageContextConfigModel? context;
final ScheduleConfigModel? schedule;
AutomationJobConfigPatchModel({
this.inputTemplate,
this.enabledTools,
this.context,
this.schedule,
});
Map<String, dynamic> toJson() {
@@ -383,22 +448,19 @@ class AutomationJobConfigPatchModel {
if (inputTemplate != null) map['input_template'] = inputTemplate;
if (enabledTools != null) map['enabled_tools'] = enabledTools;
if (context != null) map['context'] = context!.toJson();
if (schedule != null) map['schedule'] = schedule!.toJson();
return map;
}
}
class AutomationJobUpdateRequest {
final String? title;
final String? scheduleType;
final String? runAt;
final String? timezone;
final String? status;
final AutomationJobConfigPatchModel? config;
AutomationJobUpdateRequest({
this.title,
this.scheduleType,
this.runAt,
this.timezone,
this.status,
this.config,
@@ -407,8 +469,6 @@ class AutomationJobUpdateRequest {
Map<String, dynamic> toJson() {
final map = <String, dynamic>{};
if (title != null) map['title'] = title;
if (scheduleType != null) map['schedule_type'] = scheduleType;
if (runAt != null) map['run_at'] = runAt;
if (timezone != null) map['timezone'] = timezone;
if (status != null) map['status'] = status;
if (config != null) map['config'] = config!.toJson();