import {z} from 'zod' export type GetApiV3AgentsAgentKeySchedulesArgs = {agent_key: string}; export type GetApiV3AgentsAgentKeySchedulesOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const publicSchedulePayload = z.object({input: z.unknown(), memory_entity_id: z.string().optional(), metadata: z.record(z.string(), z.string()).optional(), variables: z.record(z.string(), z.unknown()).optional()}); export const publicSchedule = z.object({_id: z.string(), agent_key: z.string(), agent_tag: z.string().optional(), created: z.string(), created_by_id: z.string(), display_name: z.string().optional(), expression: z.string(), generation: z.number().int(), is_active: z.boolean(), last_triggered_at: z.string().optional(), payload: publicSchedulePayload, trigger_count: z.number().int(), type: z.enum(["cron", "once", "interval"]), updated: z.string(), updated_by_id: z.string().optional()}); export const getApiV3AgentsAgentKeySchedulesResponse = z.object({schedules: z.array(publicSchedule).nullable()}); 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 }; /** * List schedules * * GET /v3/agents/{agent_key}/schedules */ export const getApiV3AgentsAgentKeySchedules = async (args: GetApiV3AgentsAgentKeySchedulesArgs, options?: GetApiV3AgentsAgentKeySchedulesOptions) => { const {baseUrl = "https://my.orq.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/agents/${encodeURIComponent(String(args.agent_key))}/schedules${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV3AgentsAgentKeySchedulesResponse, await res.json()) };