import {z} from 'zod' export type FilePurpose = 'batch' | 'fine-tune' | 'history'; export type GetApiV1FilesArgs = {limit?: number | undefined, purpose?: (FilePurpose | null) | undefined, ai_project_id?: (string | null) | undefined}; export type GetApiV1FilesOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const openAIFile = z.object({id: z.string(), bytes: z.number().int(), created_at: z.number().int(), filename: z.string(), object: z.string().optional(), purpose: z.string(), status: z.string().nullable().optional(), status_details: z.string().nullable().optional()}); export const listFilesResponse = z.object({data: z.array(openAIFile), object: z.string().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 user files * * GET /v1/files */ export const getApiV1Files = async (args: GetApiV1FilesArgs, options?: GetApiV1FilesOptions) => { const {baseUrl = "https://api.tokenfactory.nebius.com", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.purpose != null) query.append("purpose", String(args.purpose)) if (args.ai_project_id != null) query.append("ai_project_id", String(args.ai_project_id)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) 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(listFilesResponse, await res.json()) };