import type { PluginApi, PluginRuntimeContext } from '../../src/plugin-sdk.js'; import { handleCronCommand } from './src/commands.js'; import { Scheduler } from './src/scheduler.js'; import { migrateCronTasks } from './src/store.js'; let scheduler: Scheduler | null = null; let schedulerStop: Promise | null = null; export function getCronScheduler(): Scheduler | null { return scheduler; } const api: PluginApi = { async activate(ctx: PluginRuntimeContext) { await migrateCronTasks(); await scheduler?.stop(); await schedulerStop; const instance = new Scheduler(ctx); scheduler = instance; instance.start(); return () => { if (scheduler === instance) scheduler = null; let tracked: Promise; tracked = instance.stop().finally(() => { if (schedulerStop === tracked) schedulerStop = null; }); schedulerStop = tracked; }; }, async *runCommand(commandName, args, ctx) { if (!ctx.runtimeContext || ctx.runtimeContext.owner !== 'local-owner' || !['tui', 'desktop', 'cli'].includes(ctx.runtimeContext.surface) || ctx.runtimeContext.profile !== 'main' || ctx.runtimeContext.conversationMode === 'group') { yield { type: 'error', error: '后台或未授权宿主不能管理计划任务' }; yield { type: 'turn_done' }; return; } const text = await handleCronCommand(commandName, args, scheduler, ctx.runtimeContext ? { cwd: ctx.runtimeContext.cwd, sessionId: ctx.runtimeContext.sessionId } : undefined); yield { type: 'text', delta: text }; yield { type: 'turn_done' }; }, }; export default api;