fix: clean up cron job edit payloads (#364)

x找我下,跟你聊聊
This commit is contained in:
Zhicheng Han
2026-05-01 02:12:53 +02:00
committed by GitHub
parent 571687459f
commit 7f01fdf56e
6 changed files with 330 additions and 38 deletions
+89 -3
View File
@@ -1,5 +1,33 @@
import { request } from '../client'
export interface JobScheduleInterval {
kind: 'interval'
minutes: number
display: string
}
export interface JobScheduleCron {
kind: 'cron'
expr: string
display: string
}
export interface JobScheduleOnce {
kind: 'once'
run_at: string
display: string
}
type UnknownJobSchedule = {
kind: string
display?: string
expr?: string
minutes?: number
run_at?: string
}
export type JobSchedule = string | JobScheduleInterval | JobScheduleCron | JobScheduleOnce | UnknownJobSchedule
export interface Job {
job_id: string
id: string
@@ -12,7 +40,7 @@ export interface Job {
provider: string | null
base_url: string | null
script: string | null
schedule: string | { kind: string; expr: string; display: string }
schedule: JobSchedule
schedule_display: string
repeat: string | { times: number | null; completed: number }
enabled: boolean
@@ -45,21 +73,79 @@ export interface CreateJobRequest {
export interface UpdateJobRequest {
name?: string
schedule?: string | { kind: string; expr: string; display: string }
schedule?: string
prompt?: string
deliver?: string
skills?: string[]
skill?: string
repeat?: number
repeat?: number | null
enabled?: boolean
model?: string
provider?: string
}
export interface JobFormValues {
name: string
schedule: string
prompt: string
deliver: string
repeat_times: number | null
}
function unwrap(res: { job: Job }): Job {
return res.job
}
function isScheduleObject(schedule: JobSchedule | null | undefined): schedule is Exclude<JobSchedule, string> {
return typeof schedule === 'object' && schedule !== null
}
export function scheduleToEditableInput(schedule: JobSchedule | null | undefined, fallback = ''): string {
if (typeof schedule === 'string') return schedule
if (!isScheduleObject(schedule)) return fallback
if (schedule.kind === 'cron') return schedule.expr || schedule.display || fallback
if (schedule.kind === 'once') return schedule.run_at || schedule.display || fallback
if (schedule.kind === 'interval') {
return schedule.display || (typeof schedule.minutes === 'number' ? `every ${schedule.minutes}m` : fallback)
}
const unknownSchedule = schedule as UnknownJobSchedule
return unknownSchedule.expr || unknownSchedule.run_at || unknownSchedule.display || fallback
}
export function scheduleToDisplayText(schedule: JobSchedule | null | undefined, fallback = '—'): string {
if (typeof schedule === 'string') return schedule
if (!isScheduleObject(schedule)) return fallback
if (schedule.kind === 'cron') return schedule.expr || schedule.display || fallback
if (schedule.kind === 'interval') return schedule.display || scheduleToEditableInput(schedule, fallback)
if (schedule.kind === 'once') return schedule.display || scheduleToEditableInput(schedule, fallback)
const unknownSchedule = schedule as UnknownJobSchedule
return unknownSchedule.display || unknownSchedule.expr || unknownSchedule.run_at || fallback
}
export function jobRepeatToEditValue(repeat: Job['repeat']): number | null {
if (repeat && typeof repeat === 'object') return repeat.times ?? null
return null
}
export function buildJobUpdateRequest(original: Job, form: JobFormValues): UpdateJobRequest {
const payload: UpdateJobRequest = {}
const originalSchedule = scheduleToEditableInput(original.schedule, original.schedule_display || '')
const originalRepeat = jobRepeatToEditValue(original.repeat)
const originalDeliver = original.deliver || 'origin'
if (form.name !== original.name) payload.name = form.name
if (form.schedule !== originalSchedule) payload.schedule = form.schedule
if (form.prompt !== (original.prompt || '')) payload.prompt = form.prompt
if (form.deliver !== originalDeliver) payload.deliver = form.deliver
if (form.repeat_times !== originalRepeat) payload.repeat = form.repeat_times
return payload
}
export async function listJobs(): Promise<Job[]> {
const res = await request<{ jobs: Job[] }>('/api/hermes/jobs?include_disabled=true')
return res.jobs