import {z} from 'zod' export type GetApiScheduledTasksArgs = {offset?: number | undefined, limit?: number | undefined, name_filter?: string | undefined, status_filter?: string | undefined, collection_id?: string | undefined, has_collection?: boolean | undefined}; export type GetApiScheduledTasksOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduledTask = z.object({id: z.string().optional(), user_id: z.string().optional(), username: z.string().optional(), name: z.string().optional(), description: z.string().optional(), schedule_type: z.enum(["once", "recurring", "interval"]).optional(), schedule_expression: z.string().optional(), task_type: z.enum(["chat_completion", "agent_task"]).optional(), task_config: z.record(z.string(), z.unknown()).optional(), status: z.enum(["active", "paused", "completed", "expired"]).optional(), execution_status: z.string().optional(), last_execution_at: z.string().nullable().optional(), next_execution_at: z.string().nullable().optional(), execution_count: z.number().int().optional(), max_executions: z.number().int().nullable().optional(), expires_at: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), is_owner: z.boolean().optional(), notification_email: z.string().optional()}); export const scheduledTaskList = z.object({items: z.array(scheduledTask).optional(), total: z.number().int().optional()}); 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 scheduled tasks for the current user. * * GET /scheduled_tasks */ export const getApiScheduledTasks = async (args: GetApiScheduledTasksArgs, options?: GetApiScheduledTasksOptions) => { const {baseUrl = "https://h2ogpte.genai.h2o.ai/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.name_filter != null) query.append("name_filter", String(args.name_filter)) if (args.status_filter != null) query.append("status_filter", String(args.status_filter)) if (args.collection_id != null) query.append("collection_id", String(args.collection_id)) if (args.has_collection != null) query.append("has_collection", String(args.has_collection)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/scheduled_tasks${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(scheduledTaskList, await res.json()) };