import {z} from 'zod' export type ModelProvider = 'openai' | 'azure' | 'anthropic' | 'bedrock' | 'cohere' | 'vertex' | 'vllm' | 'mistral' | 'ollama' | 'groq' | 'sgl' | 'parasail' | 'perplexity' | 'replicate' | 'cerebras' | 'deepseek' | 'gemini' | 'openrouter' | 'elevenlabs' | 'huggingface' | 'nebius' | 'xai' | 'runway' | 'fireworks'; export type GetApiV1FilesArgs = {'x-model-provider': ModelProvider, purpose?: ('batch' | 'assistants' | 'fine-tune' | 'vision' | 'batch_output' | 'user_data' | 'responses' | 'evals') | undefined, limit?: number | undefined, after?: string | undefined, order?: ('asc' | 'desc') | undefined}; export type GetApiV1FilesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const modelProvider = z.enum(["openai", "azure", "anthropic", "bedrock", "cohere", "vertex", "vllm", "mistral", "ollama", "groq", "sgl", "parasail", "perplexity", "replicate", "cerebras", "deepseek", "gemini", "openrouter", "elevenlabs", "huggingface", "nebius", "xai", "runway", "fireworks"]); export const bifrostCacheDebug = z.object({cache_hit: z.boolean().optional(), cache_id: z.string().optional(), hit_type: z.string().optional(), requested_provider: z.string().optional(), requested_model: z.string().optional(), provider_used: z.string().optional(), model_used: z.string().optional(), input_tokens: z.number().int().optional(), threshold: z.number().optional(), similarity: z.number().optional()}); export const bifrostResponseExtraFields = z.object({request_type: z.string().optional(), provider: modelProvider.optional(), model_requested: z.string().optional(), model_deployment: z.string().optional(), latency: z.number().int().optional(), chunk_index: z.number().int().optional(), raw_request: z.object({}).optional(), raw_response: z.object({}).optional(), cache_debug: bifrostCacheDebug.optional()}); export const fileListResponse = z.object({object: z.string().optional(), data: z.array(z.object({id: z.string().optional(), object: z.string().optional(), bytes: z.number().int().optional(), created_at: z.number().int().optional(), filename: z.string().optional(), purpose: z.enum(["batch", "assistants", "fine-tune", "vision", "batch_output", "user_data", "responses", "evals"]).optional(), status: z.enum(["uploaded", "processed", "processing", "error", "deleted"]).optional(), status_details: z.string().optional(), expires_at: z.number().int().optional()})).optional(), has_more: z.boolean().optional(), after: z.string().optional(), extra_fields: bifrostResponseExtraFields.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 files * * GET /v1/files * * Also accepts auth: BasicAuth, VirtualKeyAuth, ApiKeyAuth */ export const getApiV1Files = async (args: GetApiV1FilesArgs, options?: GetApiV1FilesOptions) => { const {baseUrl = "http://localhost:8080", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-model-provider'] != null) query.append("x-model-provider", String(args['x-model-provider'])) 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 (args.order != null) query.append("order", String(args.order)) 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(fileListResponse, await res.json()) };