import { OnModuleInit, OnModuleDestroy } from '@nestjs/common'; import { DatabaseService } from '../database/database.service.js'; import { AgentsService } from '../agents/agents.service.js'; import { ConfigService } from '../config/config.service.js'; import { UsageService } from '../usage/usage.service.js'; export type ScheduleType = 'once' | 'cron'; export interface RepeatRule { type: 'daily' | 'weekdays' | 'weekends' | 'weekly' | 'monthly'; time: string; days?: number[]; dayOfMonth?: number; } export interface ScheduledTask { id: string; workspace: string; message: string; scheduleType: ScheduleType; runAt?: number; cronExpr?: string; repeatRuleJson?: string; enabled: boolean; lastRunAt?: number; createdAt: number; updatedAt: number; } export type TaskExecutionStatus = 'success' | 'failed'; export interface TaskExecution { id: string; taskId: string; ranAt: number; status: TaskExecutionStatus; sessionId?: string; userMessage: string; assistantContent?: string; errorMessage?: string; createdAt: number; } /** 将 repeatRule 转为 cron 表达式 */ export declare function repeatRuleToCron(rule: RepeatRule): string; export declare class TasksService implements OnModuleInit, OnModuleDestroy { private readonly db; private readonly agentsService; private readonly configService; private readonly usageService; private tickTimer; private readonly TICK_MS; /** 正在执行中的 task id,避免同一任务在两次 tick 中并发执行 */ private runningTaskIds; constructor(db: DatabaseService, agentsService: AgentsService, configService: ConfigService, usageService: UsageService); onModuleInit(): void; onModuleDestroy(): void; private rowToTask; list(): ScheduledTask[]; get(id: string): ScheduledTask | null; private rowToExecution; addExecution(params: { taskId: string; ranAt: number; status: TaskExecutionStatus; sessionId?: string; userMessage: string; assistantContent?: string; errorMessage?: string; }): TaskExecution; listExecutions(taskId: string): TaskExecution[]; getExecution(id: string): TaskExecution | null; /** 删除指定任务的全部执行记录,返回删除条数 */ deleteExecutionsByTaskId(taskId: string): number; create(params: { workspace: string; message: string; scheduleType: ScheduleType; runAt?: number; cronExpr?: string; repeatRule?: RepeatRule; }): ScheduledTask; update(id: string, updates: Partial>): ScheduledTask | null; delete(id: string): boolean; /** 获取当前应执行的任务 */ getDueTasks(at: Date): ScheduledTask[]; /** 仅更新 last_run_at,不关单次任务。用于「开始执行」时立即占位,避免下一分钟 tick 重复触发同一任务。 */ markRunStarted(id: string): void; markRan(id: string): void; /** 每分钟触发:拉取到点任务,创建会话、写用户消息、请求 Gateway 执行,写执行记录,再 markRan */ private tick; }