import {z} from 'zod' export type GetApiAnthropicV1FilesArgs = {'x-model-provider'?: string | undefined, limit?: number | undefined, after_id?: string | undefined}; export type GetApiAnthropicV1FilesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAnthropicV1FilesResponse = z.object({data: z.array(z.object({id: z.string().optional(), type: z.string().optional(), filename: z.string().optional(), mime_type: z.string().optional(), size_bytes: z.number().int().optional(), created_at: z.string().optional(), downloadable: z.boolean().optional()})).optional(), has_more: z.boolean().optional(), first_id: z.string().optional(), last_id: 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 files (Anthropic format) * * GET /anthropic/v1/files * * Also accepts auth: BasicAuth, VirtualKeyAuth, ApiKeyAuth */ export const getApiAnthropicV1Files = async (args: GetApiAnthropicV1FilesArgs, options?: GetApiAnthropicV1FilesOptions) => { 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) headers.set("x-model-provider", String(args['x-model-provider'])) if (args.limit != null) query.append("limit", String(args.limit)) if (args.after_id != null) query.append("after_id", String(args.after_id)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/anthropic/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(getApiAnthropicV1FilesResponse, await res.json()) };