schedule.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // api/schedule.ts:定时任务(增删改查 / 启停 / 立即执行 / 执行记录)。
  2. //
  3. // 响应形状均已对真实响应核实(2026-10):
  4. // GET /schedules → { tasks, total }
  5. // GET /schedules/{id} → { task }
  6. // GET /schedules/{id}/runs → { runs, task_id, total },runs[] 元素为
  7. // { id, task_id, status, started_at, finished_at, duration_ms, input, output, error }
  8. // 命令类接口(增改删 / 启停 / 立即执行)的响应体没有调用方读取,声明为 unknown。
  9. import { defHttp } from '/@/utils/http/axios/index';
  10. import type { ScheduleTask } from '/@/views/ventAI/manageAssistent/types/index';
  11. enum Api {
  12. schedules = '/ventAI/api/schedules',
  13. }
  14. export interface ScheduleListResponse {
  15. tasks: ScheduleTask[];
  16. total: number;
  17. }
  18. /**
  19. * 一次执行记录(GET /schedules/{id}/runs 响应的 runs[] 元素)。
  20. * 仍在执行中的记录只会填到 started_at,后续字段按可选处理(面板渲染也一律先判空)。
  21. */
  22. export interface ScheduleRun {
  23. id: number;
  24. task_id: number;
  25. /** running / success / failed / skipped(见 SchedulePanel 的 runStatusLabel) */
  26. status: string;
  27. started_at: string;
  28. finished_at?: string;
  29. duration_ms?: number;
  30. input?: string;
  31. output?: string;
  32. error?: string;
  33. }
  34. export interface ScheduleRunsResponse {
  35. runs: ScheduleRun[];
  36. task_id: number;
  37. total: number;
  38. }
  39. /** 单条定时任务详情(GET /schedules/{id}):外层是 { task } 而不是裸对象,已对真实响应核实 */
  40. export interface ScheduleDetailResponse {
  41. task: ScheduleTask;
  42. }
  43. /**
  44. * 定时任务接口
  45. */
  46. export const createSchedule = (data: {
  47. name: string;
  48. schedule?: { freq: string; hour?: number; minute?: number; day_of_week?: number; day_of_month?: number };
  49. cron_expr?: string;
  50. prompt: string;
  51. notify_webhook?: string;
  52. }): Promise<unknown> => defHttp.post<unknown>({ url: Api.schedules, data }, { isTransformResponse: false });
  53. export const getScheduleList = (include_disabled = true): Promise<ScheduleListResponse> =>
  54. defHttp.get<ScheduleListResponse>({ url: Api.schedules, params: { include_disabled } }, { joinParamsToUrl: true, isTransformResponse: false });
  55. export const getScheduleDetail = (task_id: number): Promise<ScheduleDetailResponse> =>
  56. defHttp.get<ScheduleDetailResponse>({ url: `${Api.schedules}/${task_id}` }, { isTransformResponse: false });
  57. export const updateSchedule = (
  58. task_id: number,
  59. data: { name?: string; schedule?: Record<string, unknown>; cron_expr?: string; prompt?: string; notify_webhook?: string }
  60. ): Promise<unknown> => defHttp.put<unknown>({ url: `${Api.schedules}/${task_id}`, data }, { isTransformResponse: false });
  61. export const pauseSchedule = (task_id: number): Promise<unknown> =>
  62. defHttp.post<unknown>({ url: `${Api.schedules}/${task_id}/pause` }, { isTransformResponse: false });
  63. export const resumeSchedule = (task_id: number): Promise<unknown> =>
  64. defHttp.post<unknown>({ url: `${Api.schedules}/${task_id}/resume` }, { isTransformResponse: false });
  65. export const deleteSchedule = (id: number): Promise<unknown> =>
  66. defHttp.delete<unknown>({ url: `${Api.schedules}/${id}` }, { isTransformResponse: false });
  67. export const getScheduleRuns = (task_id: number, limit = 50): Promise<ScheduleRunsResponse> =>
  68. defHttp.get<ScheduleRunsResponse>({ url: `${Api.schedules}/${task_id}/runs`, params: { limit } }, { joinParamsToUrl: true, isTransformResponse: false });
  69. export const runScheduleNow = (task_id: number): Promise<unknown> =>
  70. defHttp.post<unknown>({ url: `${Api.schedules}/${task_id}/run` }, { isTransformResponse: false });