2026-11-26 00:36:27 +08:00
|
|
|
// Scheduled tasks: prompts the server sends on a schedule, into a new session
|
|
|
|
|
// or an existing one. The wire shape follows the web client
|
|
|
|
|
// (`/api/projects/:projectId/scheduled-tasks`), which reads the same tasks.
|
2026-11-26 09:04:25 +08:00
|
|
|
import '../l10n.dart';
|
|
|
|
|
|
2026-11-26 00:36:27 +08:00
|
|
|
class ScheduledTask {
|
|
|
|
|
final String id;
|
|
|
|
|
final String name;
|
|
|
|
|
final bool enabled;
|
|
|
|
|
|
|
|
|
|
/// 'daily' | 'weekly' | 'once' | 'cron'.
|
|
|
|
|
final String kind;
|
|
|
|
|
|
|
|
|
|
/// Daily and weekly: the `HH:mm` times it runs at.
|
|
|
|
|
final List<String> times;
|
|
|
|
|
|
|
|
|
|
/// Weekly: 0 is Sunday, as the server counts.
|
|
|
|
|
final List<int> weekdays;
|
|
|
|
|
|
|
|
|
|
/// Once: `YYYY-MM-DD` and `HH:mm`.
|
|
|
|
|
final String date;
|
|
|
|
|
final String time;
|
|
|
|
|
|
|
|
|
|
/// Cron: the expression.
|
|
|
|
|
final String cron;
|
|
|
|
|
|
|
|
|
|
/// IANA name; empty means the server's own zone.
|
|
|
|
|
final String timezone;
|
|
|
|
|
|
|
|
|
|
/// What a run does: the prompt, and whether the session's defaults are used
|
|
|
|
|
/// instead of a fixed model and agent.
|
|
|
|
|
final String prompt;
|
|
|
|
|
final bool useDefaults;
|
|
|
|
|
final String providerId;
|
|
|
|
|
final String modelId;
|
|
|
|
|
final String variant;
|
|
|
|
|
final String agent;
|
|
|
|
|
|
|
|
|
|
/// Sends into this session instead of a new one; empty means a new session.
|
|
|
|
|
final String targetSessionId;
|
|
|
|
|
|
|
|
|
|
final String lastStatus;
|
|
|
|
|
final String lastError;
|
|
|
|
|
final int lastRunAt;
|
|
|
|
|
final int nextRunAt;
|
|
|
|
|
final int lastDurationMs;
|
|
|
|
|
final String lastSessionId;
|
|
|
|
|
|
|
|
|
|
const ScheduledTask({
|
|
|
|
|
this.id = '',
|
|
|
|
|
this.name = '',
|
|
|
|
|
this.enabled = true,
|
|
|
|
|
this.kind = 'daily',
|
|
|
|
|
this.times = const [],
|
|
|
|
|
this.weekdays = const [],
|
|
|
|
|
this.date = '',
|
|
|
|
|
this.time = '',
|
|
|
|
|
this.cron = '',
|
|
|
|
|
this.timezone = '',
|
|
|
|
|
this.prompt = '',
|
|
|
|
|
this.useDefaults = true,
|
|
|
|
|
this.providerId = '',
|
|
|
|
|
this.modelId = '',
|
|
|
|
|
this.variant = '',
|
|
|
|
|
this.agent = '',
|
|
|
|
|
this.targetSessionId = '',
|
|
|
|
|
this.lastStatus = '',
|
|
|
|
|
this.lastError = '',
|
|
|
|
|
this.lastRunAt = 0,
|
|
|
|
|
this.nextRunAt = 0,
|
|
|
|
|
this.lastDurationMs = 0,
|
|
|
|
|
this.lastSessionId = '',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
factory ScheduledTask.fromJson(Map<String, dynamic> j) {
|
|
|
|
|
final schedule = j['schedule'] as Map<String, dynamic>? ?? const {};
|
|
|
|
|
final execution = j['execution'] as Map<String, dynamic>? ?? const {};
|
|
|
|
|
final state = j['state'] as Map<String, dynamic>? ?? const {};
|
|
|
|
|
String text(Object? value) => value is String ? value : '';
|
|
|
|
|
int number(Object? value) => (value is num) ? value.toInt() : 0;
|
|
|
|
|
return ScheduledTask(
|
|
|
|
|
id: text(j['id']),
|
|
|
|
|
name: text(j['name']),
|
|
|
|
|
enabled: j['enabled'] != false,
|
|
|
|
|
kind: text(schedule['kind']),
|
|
|
|
|
times: [
|
|
|
|
|
for (final value in schedule['times'] as List? ?? const [])
|
|
|
|
|
if (value is String) value,
|
|
|
|
|
],
|
|
|
|
|
weekdays: [
|
|
|
|
|
for (final value in schedule['weekdays'] as List? ?? const [])
|
|
|
|
|
if (value is num) value.toInt(),
|
|
|
|
|
],
|
|
|
|
|
date: text(schedule['date']),
|
|
|
|
|
time: text(schedule['time']),
|
|
|
|
|
cron: text(schedule['cron']),
|
|
|
|
|
timezone: text(schedule['timezone']),
|
|
|
|
|
prompt: text(execution['prompt']),
|
|
|
|
|
// The web client treats a task without a fixed model as using the
|
|
|
|
|
// session's defaults, whatever the flag says.
|
|
|
|
|
useDefaults: execution['useDefaults'] == true || text(execution['modelID']).isEmpty,
|
|
|
|
|
providerId: text(execution['providerID']),
|
|
|
|
|
modelId: text(execution['modelID']),
|
|
|
|
|
variant: text(execution['variant']),
|
|
|
|
|
agent: text(execution['agent']),
|
|
|
|
|
targetSessionId: text(j['targetSessionId']),
|
|
|
|
|
lastStatus: text(state['lastStatus']),
|
|
|
|
|
lastError: text(state['lastError']),
|
|
|
|
|
lastRunAt: number(state['lastRunAt']),
|
|
|
|
|
nextRunAt: number(state['nextRunAt']),
|
|
|
|
|
lastDurationMs: number(state['lastDurationMs']),
|
|
|
|
|
lastSessionId: text(state['lastSessionId']),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// What the server is sent on save. State is the server's own; a task keeps
|
|
|
|
|
/// it unless it is deliberately replaced.
|
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
|
|
|
if (id.isNotEmpty) 'id': id,
|
|
|
|
|
'name': name,
|
|
|
|
|
'enabled': enabled,
|
|
|
|
|
'schedule': {
|
|
|
|
|
'kind': kind,
|
|
|
|
|
if (kind == 'daily' || kind == 'weekly') 'times': times,
|
|
|
|
|
if (kind == 'weekly') 'weekdays': weekdays,
|
|
|
|
|
if (kind == 'once') ...{'date': date, 'time': time},
|
|
|
|
|
if (kind == 'cron') 'cron': cron,
|
|
|
|
|
if (timezone.isNotEmpty) 'timezone': timezone,
|
|
|
|
|
},
|
|
|
|
|
'execution': {
|
|
|
|
|
'prompt': prompt,
|
|
|
|
|
'useDefaults': useDefaults,
|
|
|
|
|
if (!useDefaults && providerId.isNotEmpty) 'providerID': providerId,
|
|
|
|
|
if (!useDefaults && modelId.isNotEmpty) 'modelID': modelId,
|
|
|
|
|
if (!useDefaults && variant.isNotEmpty) 'variant': variant,
|
|
|
|
|
if (!useDefaults && agent.isNotEmpty) 'agent': agent,
|
|
|
|
|
},
|
|
|
|
|
if (targetSessionId.isNotEmpty) 'targetSessionId': targetSessionId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ScheduledTask copyWith({bool? enabled}) => ScheduledTask(
|
|
|
|
|
id: id,
|
|
|
|
|
name: name,
|
|
|
|
|
enabled: enabled ?? this.enabled,
|
|
|
|
|
kind: kind,
|
|
|
|
|
times: times,
|
|
|
|
|
weekdays: weekdays,
|
|
|
|
|
date: date,
|
|
|
|
|
time: time,
|
|
|
|
|
cron: cron,
|
|
|
|
|
timezone: timezone,
|
|
|
|
|
prompt: prompt,
|
|
|
|
|
useDefaults: useDefaults,
|
|
|
|
|
providerId: providerId,
|
|
|
|
|
modelId: modelId,
|
|
|
|
|
variant: variant,
|
|
|
|
|
agent: agent,
|
|
|
|
|
targetSessionId: targetSessionId,
|
|
|
|
|
lastStatus: lastStatus,
|
|
|
|
|
lastError: lastError,
|
|
|
|
|
lastRunAt: lastRunAt,
|
|
|
|
|
nextRunAt: nextRunAt,
|
|
|
|
|
lastDurationMs: lastDurationMs,
|
|
|
|
|
lastSessionId: lastSessionId,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/// The model line: a fixed choice, or that the run uses the session's.
|
|
|
|
|
String get modelLabel {
|
2026-11-26 11:48:16 +08:00
|
|
|
if (useDefaults || modelId.isEmpty) return tr('Use default model and agent');
|
2026-11-26 00:36:27 +08:00
|
|
|
final model = [providerId, modelId].where((s) => s.isNotEmpty).join('/');
|
|
|
|
|
return agent.isEmpty ? model : '$model · $agent';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-26 11:48:16 +08:00
|
|
|
const scheduledWeekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
2026-11-26 00:36:27 +08:00
|
|
|
|
|
|
|
|
const scheduledTaskKindLabels = {
|
2026-11-26 11:48:16 +08:00
|
|
|
'daily': 'Daily',
|
|
|
|
|
'weekly': 'Weekly',
|
|
|
|
|
'once': 'Once',
|
2026-11-26 00:36:27 +08:00
|
|
|
'cron': 'Cron',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// '每天 09:00', '每周 周一、周三 09:00', '一次性 2026-12-01 09:00', …
|
|
|
|
|
String scheduledScheduleText(ScheduledTask task) {
|
2026-11-26 11:48:16 +08:00
|
|
|
final separator = tr(', ');
|
2026-11-26 09:04:25 +08:00
|
|
|
final times = task.times.isEmpty ? task.time : task.times.join(separator);
|
2026-11-26 00:36:27 +08:00
|
|
|
switch (task.kind) {
|
|
|
|
|
case 'daily':
|
2026-11-26 11:48:16 +08:00
|
|
|
return trArgs('Daily {0}', [times.isEmpty ? '--:--' : times]);
|
2026-11-26 00:36:27 +08:00
|
|
|
case 'weekly':
|
|
|
|
|
final days = [
|
|
|
|
|
for (final day in task.weekdays)
|
2026-11-26 09:04:25 +08:00
|
|
|
if (day >= 0 && day < scheduledWeekdays.length)
|
|
|
|
|
tr(scheduledWeekdays[day]),
|
|
|
|
|
].join(separator);
|
2026-11-26 11:48:16 +08:00
|
|
|
return trArgs('Weekly {0} {1}', [
|
|
|
|
|
days.isEmpty ? tr('no days') : days,
|
2026-11-26 09:04:25 +08:00
|
|
|
times.isEmpty ? '--:--' : times,
|
|
|
|
|
]);
|
2026-11-26 00:36:27 +08:00
|
|
|
case 'once':
|
2026-11-26 11:48:16 +08:00
|
|
|
return trArgs('Once {0} {1}', [
|
|
|
|
|
task.date.isEmpty ? tr('no date') : task.date,
|
2026-11-26 09:04:25 +08:00
|
|
|
task.time.isEmpty ? '--:--' : task.time,
|
|
|
|
|
]);
|
2026-11-26 00:36:27 +08:00
|
|
|
case 'cron':
|
2026-11-26 11:48:16 +08:00
|
|
|
return trArgs('Cron: {0}', [task.cron]);
|
2026-11-26 00:36:27 +08:00
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String scheduledStatusLabel(String status) => switch (status) {
|
2026-11-26 11:48:16 +08:00
|
|
|
'success' || 'sent' => tr('Success'),
|
|
|
|
|
'error' || 'failed' => tr('Error'),
|
|
|
|
|
'running' => tr('Running'),
|
|
|
|
|
'queued' => tr('Queued'),
|
|
|
|
|
'skipped' => tr('Skipped'),
|
|
|
|
|
'cancelled' => tr('Cancelled'),
|
|
|
|
|
_ => tr('Idle'),
|
2026-11-26 00:36:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// How far away a moment is, in the words the web client uses.
|
|
|
|
|
String scheduledRelativeTime(int milliseconds) {
|
|
|
|
|
if (milliseconds <= 0) return '';
|
|
|
|
|
final diff = milliseconds - DateTime.now().millisecondsSinceEpoch;
|
|
|
|
|
final abs = diff.abs();
|
|
|
|
|
const minute = 60 * 1000;
|
|
|
|
|
const hour = 60 * minute;
|
|
|
|
|
const day = 24 * hour;
|
|
|
|
|
final future = diff >= 0;
|
2026-11-26 11:48:16 +08:00
|
|
|
if (abs < minute) return future ? tr('In less than a minute') : tr('Just now');
|
2026-11-26 00:36:27 +08:00
|
|
|
if (abs < hour) {
|
|
|
|
|
final m = (abs / minute).round();
|
2026-11-26 09:04:25 +08:00
|
|
|
return future
|
2026-11-26 11:48:16 +08:00
|
|
|
? trArgs('In {0} min', [m])
|
|
|
|
|
: trArgs('{0} min ago', [m]);
|
2026-11-26 00:36:27 +08:00
|
|
|
}
|
|
|
|
|
if (abs < day) {
|
|
|
|
|
final h = abs ~/ hour;
|
|
|
|
|
final m = ((abs % hour) / minute).round();
|
2026-11-26 09:04:25 +08:00
|
|
|
final body =
|
2026-11-26 11:48:16 +08:00
|
|
|
m > 0 ? trArgs('{0} hr {1} min', [h, m]) : trArgs('{0} hr', [h]);
|
|
|
|
|
return future ? trArgs('In {0}', [body]) : trArgs('{0} ago', [body]);
|
2026-11-26 00:36:27 +08:00
|
|
|
}
|
|
|
|
|
final d = abs ~/ day;
|
|
|
|
|
final h = ((abs % day) / hour).round();
|
2026-11-26 11:48:16 +08:00
|
|
|
final body = h > 0 ? trArgs('{0} days {1} hr', [d, h]) : trArgs('{0} days', [d]);
|
|
|
|
|
return future ? trArgs('In {0}', [body]) : trArgs('{0} ago', [body]);
|
2026-11-26 00:36:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String scheduledClock(int milliseconds) {
|
|
|
|
|
if (milliseconds <= 0) return '';
|
|
|
|
|
final at = DateTime.fromMillisecondsSinceEpoch(milliseconds);
|
|
|
|
|
String two(int n) => n.toString().padLeft(2, '0');
|
|
|
|
|
return '${two(at.hour)}:${two(at.minute)}';
|
|
|
|
|
}
|