import {z} from 'zod' export type ScheduleCreateRequest = {agent?: ('devin' | 'data_analyst') | undefined, bypass_approval?: boolean | undefined, create_as_user_id?: (string | null) | undefined, frequency?: (string | null) | undefined, interval_count?: number | undefined, name: string, notify_on?: ('always' | 'failure' | 'never') | undefined, platform?: (string | null) | undefined, playbook_id?: (string | null) | undefined, prompt: string, schedule_type?: ('recurring' | 'one_time') | undefined, scheduled_at?: (string | null) | undefined, slack_channel_id?: (string | null) | undefined, slack_team_id?: (string | null) | undefined, tags?: (Array | null) | undefined, target_devin_id?: (string | null) | undefined}; export type CreateApiV3OrganizationsOrgIdSchedulesArgs = {org_id: string, body: ScheduleCreateRequest}; export type CreateApiV3OrganizationsOrgIdSchedulesOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const playbookInfo = z.object({playbook_id: z.string(), title: z.string().nullable()}); export const scheduleResponse = z.object({agent: z.enum(["devin", "data_analyst"]), bypass_approval: z.boolean().optional(), consecutive_failures: z.number().int(), created_at: z.string(), created_by: z.string().nullable(), enabled: z.boolean(), frequency: z.string().nullable(), interval_count: z.number().int().optional(), last_edited_by: z.string().nullable().optional(), last_error_at: z.string().nullable(), last_error_message: z.string().nullable(), last_executed_at: z.string().nullable(), name: z.string(), notify_on: z.enum(["always", "failure", "never"]), org_id: z.string(), platform: z.string().nullable().optional(), playbook: playbookInfo.nullable(), prompt: z.string(), schedule_type: z.enum(["recurring", "one_time"]).optional(), scheduled_at: z.string().nullable().optional(), scheduled_session_id: z.string(), slack_channel_id: z.string().nullable().optional(), slack_team_id: z.string().nullable().optional(), tags: z.array(z.string()).nullable().optional(), target_devin_id: z.string().nullable().optional(), updated_at: z.string()}); 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 /v3/organizations/{org_id}/schedules */ export const createApiV3OrganizationsOrgIdSchedules = async (args: CreateApiV3OrganizationsOrgIdSchedulesArgs, options: CreateApiV3OrganizationsOrgIdSchedulesOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/organizations/${encodeURIComponent(String(args.org_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()) };