import {z} from 'zod' export type GetApiV1FilesArgs = {purpose?: ('batch' | 'batch_output' | 'batch_error') | undefined, limit?: string | undefined, after?: string | undefined}; export type GetApiV1FilesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const batchFile = z.object({id: z.string(), object: z.literal("file"), bytes: z.number(), created_at: z.number(), expires_at: z.number().nullable(), filename: z.string(), purpose: z.enum(["batch", "batch_output", "batch_error"]), status: z.literal("processed"), status_details: z.unknown()}); export const getApiV1FilesResponse = z.object({object: z.literal("list"), data: z.array(batchFile), first_id: z.string().nullable(), last_id: z.string().nullable(), has_more: z.boolean()}); 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 files * * GET /v1/files */ export const getApiV1Files = async (args: GetApiV1FilesArgs, options?: GetApiV1FilesOptions) => { const {baseUrl = "https://api.llmgateway.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.purpose != null) query.append("purpose", String(args.purpose)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.after != null) query.append("after", String(args.after)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/files${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(getApiV1FilesResponse, await res.json()) };