import {z} from 'zod' export type CreateApiV1betaModelsModelGenerateContentArgs = {model: string, body: {contents: Array<{role?: ('user' | 'model') | undefined, parts?: (Array<{text?: string | undefined, inline_data?: ({mime_type?: ('image/jpeg' | 'image/png' | 'image/webp') | undefined, data?: string | undefined}) | undefined}>) | undefined}>, generationConfig?: ({responseModalities?: (Array<'TEXT' | 'IMAGE'>) | undefined, imageConfig?: ({aspectRatio?: string | undefined, imageSize?: ('512px' | '1K' | '2K' | '4K') | undefined}) | undefined, thinkingConfig?: ({thinkingLevel?: ('minimal' | 'high') | undefined, includeThoughts?: boolean | undefined}) | undefined}) | undefined, tools?: (Array<{google_search?: Record | undefined}>) | undefined}}; export type CreateApiV1betaModelsModelGenerateContentOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1betaModelsModelGenerateContentResponse = z.object({candidates: z.array(z.object({content: z.object({role: z.string().optional(), parts: z.array(z.object({text: z.string().optional(), inlineData: z.object({mimeType: z.string().optional(), data: z.string().optional()}).optional()})).optional()}).optional(), finishReason: z.enum(["STOP", "MAX_TOKENS", "SAFETY", "RECITATION"]).optional(), index: z.number().int().optional(), safetyRatings: z.array(z.object({category: z.string().optional(), probability: z.string().optional()})).optional()})).optional(), usageMetadata: z.object({promptTokenCount: z.number().int().optional(), candidatesTokenCount: z.number().int().optional(), totalTokenCount: z.number().int().optional(), thoughtsTokenCount: z.number().int().optional()}).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 }; /** * Gemini Image Generation * * POST /v1beta/models/{model}:generateContent */ export const createApiV1betaModelsModelGenerateContent = async (args: CreateApiV1betaModelsModelGenerateContentArgs, options?: CreateApiV1betaModelsModelGenerateContentOptions) => { const {baseUrl = "https://api.deerapi.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1beta/models/${encodeURIComponent(String(args.model))}:generateContent${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1betaModelsModelGenerateContentResponse, await res.json()) };