import {z} from 'zod' export type CreateScheduleRequest = {cron_expression?: (string | null) | undefined, end_at?: (string | null) | undefined, execution_timeout_seconds?: (number | null) | undefined, interval_seconds?: (number | null) | undefined, name: string, paused?: boolean | undefined, start_at?: (string | null) | undefined, task_queue: string, workflow_name: string, workflow_params?: ({[key: string]: unknown} | null) | undefined}; export type CreateApiAgentsAgentIdSchedulesArgs = {agent_id: string, body: CreateScheduleRequest}; export type CreateApiAgentsAgentIdSchedulesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const scheduleActionInfo = z.object({task_queue: z.string(), workflow_id_prefix: z.string(), workflow_name: z.string(), workflow_params: z.array(z.unknown()).nullable().optional()}); export const scheduleSpecInfo = z.object({cron_expressions: z.array(z.string()).optional(), end_at: z.string().nullable().optional(), intervals_seconds: z.array(z.number().int()).optional(), start_at: z.string().nullable().optional()}); export const scheduleState = z.enum(["ACTIVE", "PAUSED"]); export const scheduleResponse = z.object({action: scheduleActionInfo, agent_id: z.string(), created_at: z.string().nullable().optional(), last_action_time: z.string().nullable().optional(), name: z.string(), next_action_times: z.array(z.string()).optional(), num_actions_missed: z.number().int().optional(), num_actions_taken: z.number().int().optional(), schedule_id: z.string(), spec: scheduleSpecInfo, state: scheduleState}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create Schedule * * POST /agents/{agent_id}/schedules */ export const createApiAgentsAgentIdSchedules = async (args: CreateApiAgentsAgentIdSchedulesArgs, options: CreateApiAgentsAgentIdSchedulesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/agents/${encodeURIComponent(String(args.agent_id))}/schedules${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(scheduleResponse, await res.json()) };