import type { Command } from "commander"; import type { SingleResult } from "../../output/index.js"; import { scheduleSchema } from "./schema.js"; import { connectScheduleClient, parseScheduleCreateInput, toScheduleCommandError, toScheduleRow, type ScheduleCommandOptions, type ScheduleRow, } from "./shared.js"; export interface ScheduleCreateOptions extends ScheduleCommandOptions { every?: string; cron?: string; name?: string; target?: string; provider?: string; maxRuns?: string; expiresIn?: string; } export async function runCreateCommand( prompt: string, options: ScheduleCreateOptions, _command: Command, ): Promise> { const input = parseScheduleCreateInput({ prompt, every: options.every, cron: options.cron, name: options.name, target: options.target, provider: options.provider, maxRuns: options.maxRuns, expiresIn: options.expiresIn, }); const { client } = await connectScheduleClient(options.host); try { const payload = await client.scheduleCreate(input); if (payload.error || !payload.schedule) { throw new Error(payload.error ?? "Schedule creation failed"); } return { type: "single", data: toScheduleRow(payload.schedule), schema: scheduleSchema, }; } catch (error) { throw toScheduleCommandError("SCHEDULE_CREATE_FAILED", "create schedule", error); } finally { await client.close().catch(() => {}); } }