import {z} from 'zod' export type GeminiPart = {text?: string | undefined, thought?: boolean | undefined, thoughtSignature?: string | undefined, inlineData?: ({mimeType?: string | undefined, data?: string | undefined, displayName?: string | undefined}) | undefined, fileData?: ({mimeType?: string | undefined, fileUri?: string | undefined, displayName?: string | undefined}) | undefined, functionCall?: ({id?: string | undefined, name?: string | undefined, args?: Record | undefined}) | undefined, functionResponse?: ({id?: string | undefined, name?: string | undefined, response?: Record | undefined, willContinue?: boolean | undefined, scheduling?: string | undefined}) | undefined, executableCode?: ({language?: string | undefined, code?: string | undefined}) | undefined, codeExecutionResult?: ({outcome?: ('OUTCOME_UNSPECIFIED' | 'OUTCOME_OK' | 'OUTCOME_FAILED' | 'OUTCOME_DEADLINE_EXCEEDED') | undefined, output?: string | undefined}) | undefined, videoMetadata?: ({fps?: number | undefined, startOffset?: string | undefined, endOffset?: string | undefined}) | undefined}; export type GeminiContent = {role?: ('user' | 'model') | undefined, parts?: Array | undefined}; export type GeminiSchema = {type?: ('TYPE_UNSPECIFIED' | 'STRING' | 'NUMBER' | 'INTEGER' | 'BOOLEAN' | 'ARRAY' | 'OBJECT' | 'NULL') | undefined, format?: string | undefined, title?: string | undefined, description?: string | undefined, nullable?: boolean | undefined, enum?: Array | undefined, properties?: {[key: string]: GeminiSchema} | undefined, required?: Array | undefined, items?: GeminiSchema | undefined, minItems?: number | undefined, maxItems?: number | undefined, minLength?: number | undefined, maxLength?: number | undefined, minimum?: number | undefined, maximum?: number | undefined, pattern?: string | undefined, default: unknown, example: unknown, anyOf?: Array | undefined, propertyOrdering?: Array | undefined, minProperties?: number | undefined, maxProperties?: number | undefined}; export type GeminiEmbeddingRequest = {model?: string | undefined, content?: GeminiContent | undefined, taskType?: string | undefined, title?: string | undefined, outputDimensionality?: number | undefined}; export type GeminiGenerationRequest = {model?: string | undefined, contents?: Array | undefined, systemInstruction?: GeminiContent | undefined, generationConfig?: ({temperature?: number | undefined, topP?: number | undefined, topK?: number | undefined, candidateCount?: number | undefined, maxOutputTokens?: number | undefined, stopSequences?: Array | undefined, responseMimeType?: string | undefined, responseSchema?: GeminiSchema | undefined, responseJsonSchema?: Record | undefined, responseModalities?: (Array<'MODALITY_UNSPECIFIED' | 'TEXT' | 'IMAGE' | 'AUDIO'>) | undefined, speechConfig?: ({voiceConfig?: ({prebuiltVoiceConfig?: ({voiceName?: string | undefined}) | undefined}) | undefined, multiSpeakerVoiceConfig?: ({speakerVoiceConfigs?: (Array<{speaker?: string | undefined, voiceConfig?: ({prebuiltVoiceConfig?: ({voiceName?: string | undefined}) | undefined}) | undefined}>) | undefined}) | undefined, languageCode?: string | undefined}) | undefined, thinkingConfig?: ({includeThoughts?: boolean | undefined, thinkingBudget?: number | undefined, thinkingLevel?: ('THINKING_LEVEL_UNSPECIFIED' | 'LOW' | 'HIGH') | undefined}) | undefined, frequencyPenalty?: number | undefined, presencePenalty?: number | undefined, seed?: number | undefined, logprobs?: number | undefined, responseLogprobs?: boolean | undefined, audioTimestamp?: boolean | undefined, mediaResolution?: string | undefined, routingConfig?: ({autoMode?: ({modelRoutingPreference?: string | undefined}) | undefined, manualMode?: ({modelName?: string | undefined}) | undefined}) | undefined, modelSelectionConfig?: ({featureSelectionPreference?: string | undefined}) | undefined, enableAffectiveDialog?: boolean | undefined}) | undefined, safetySettings?: (Array<{category?: string | undefined, threshold?: string | undefined, method?: string | undefined}>) | undefined, tools?: (Array<{functionDeclarations?: (Array<{name?: string | undefined, description?: string | undefined, parameters?: GeminiSchema | undefined, parametersJsonSchema?: Record | undefined, response?: GeminiSchema | undefined, responseJsonSchema?: Record | undefined, behavior?: ('UNSPECIFIED' | 'BLOCKING' | 'NON_BLOCKING') | undefined}>) | undefined, googleSearch?: ({timeRangeFilter?: ({startTime?: string | undefined, endTime?: string | undefined}) | undefined, excludeDomains?: Array | undefined}) | undefined, googleSearchRetrieval?: ({dynamicRetrievalConfig?: ({mode?: string | undefined, dynamicThreshold?: number | undefined}) | undefined}) | undefined, retrieval?: ({disableAttribution?: boolean | undefined, externalApi?: ({endpoint?: string | undefined, apiSpec?: ('API_SPEC_UNSPECIFIED' | 'SIMPLE_SEARCH' | 'ELASTIC_SEARCH') | undefined, authConfig?: ({authType?: ('AUTH_TYPE_UNSPECIFIED' | 'NO_AUTH' | 'API_KEY_AUTH' | 'HTTP_BASIC_AUTH' | 'GOOGLE_SERVICE_ACCOUNT_AUTH' | 'OAUTH' | 'OIDC_AUTH') | undefined, apiKeyConfig?: ({apiKeyString?: string | undefined}) | undefined, googleServiceAccountConfig?: ({serviceAccount?: string | undefined}) | undefined, httpBasicAuthConfig?: ({credentialSecret?: string | undefined}) | undefined, oauthConfig?: ({accessToken?: string | undefined, serviceAccount?: string | undefined}) | undefined, oidcConfig?: ({idToken?: string | undefined, serviceAccount?: string | undefined}) | undefined}) | undefined, elasticSearchParams?: ({index?: string | undefined, numHits?: number | undefined, searchTemplate?: string | undefined}) | undefined}) | undefined, vertexAiSearch?: ({datastore?: string | undefined, engine?: string | undefined, filter?: string | undefined, maxResults?: number | undefined, dataStoreSpecs?: (Array<{dataStore?: string | undefined, filter?: string | undefined}>) | undefined}) | undefined, vertexRagStore?: ({ragCorpora?: Array | undefined, ragResources?: (Array<{ragCorpus?: string | undefined, ragFileIds?: Array | undefined}>) | undefined, ragRetrievalConfig?: ({topK?: number | undefined, filter?: ({metadataFilter?: string | undefined, vectorDistanceThreshold?: number | undefined, vectorSimilarityThreshold?: number | undefined}) | undefined, hybridSearch?: ({alpha?: number | undefined}) | undefined, ranking?: ({llmRanker?: ({modelName?: string | undefined}) | undefined, rankService?: ({modelName?: string | undefined}) | undefined}) | undefined}) | undefined, similarityTopK?: number | undefined, storeContext?: boolean | undefined, vectorDistanceThreshold?: number | undefined}) | undefined}) | undefined, codeExecution?: Record | undefined, enterpriseWebSearch?: ({excludeDomains?: Array | undefined}) | undefined, googleMaps?: ({authConfig?: ({authType?: ('AUTH_TYPE_UNSPECIFIED' | 'NO_AUTH' | 'API_KEY_AUTH' | 'HTTP_BASIC_AUTH' | 'GOOGLE_SERVICE_ACCOUNT_AUTH' | 'OAUTH' | 'OIDC_AUTH') | undefined, apiKeyConfig?: ({apiKeyString?: string | undefined}) | undefined, googleServiceAccountConfig?: ({serviceAccount?: string | undefined}) | undefined, httpBasicAuthConfig?: ({credentialSecret?: string | undefined}) | undefined, oauthConfig?: ({accessToken?: string | undefined, serviceAccount?: string | undefined}) | undefined, oidcConfig?: ({idToken?: string | undefined, serviceAccount?: string | undefined}) | undefined}) | undefined}) | undefined, urlContext?: Record | undefined, computerUse?: ({environment?: ('ENVIRONMENT_UNSPECIFIED' | 'ENVIRONMENT_BROWSER') | undefined}) | undefined}>) | undefined, toolConfig?: ({functionCallingConfig?: ({mode?: ('MODE_UNSPECIFIED' | 'AUTO' | 'ANY' | 'NONE' | 'VALIDATED') | undefined, allowedFunctionNames?: Array | undefined}) | undefined, retrievalConfig?: ({latLng?: ({latitude?: number | undefined, longitude?: number | undefined}) | undefined, languageCode?: string | undefined}) | undefined}) | undefined, cachedContent?: string | undefined, labels?: {[key: string]: string} | undefined, requests?: Array | undefined, fallbacks?: Array | undefined}; export type CreateApiGenaiV1betaModelsModelPredictArgs = {model: string, body: GeminiGenerationRequest}; export type CreateApiGenaiV1betaModelsModelPredictOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const geminiPart = z.object({text: z.string().optional(), thought: z.boolean().optional(), thoughtSignature: z.string().optional(), inlineData: z.object({mimeType: z.string().optional(), data: z.string().optional(), displayName: z.string().optional()}).optional(), fileData: z.object({mimeType: z.string().optional(), fileUri: z.string().optional(), displayName: z.string().optional()}).optional(), functionCall: z.object({id: z.string().optional(), name: z.string().optional(), args: z.object({}).optional()}).optional(), functionResponse: z.object({id: z.string().optional(), name: z.string().optional(), response: z.object({}).optional(), willContinue: z.boolean().optional(), scheduling: z.string().optional()}).optional(), executableCode: z.object({language: z.string().optional(), code: z.string().optional()}).optional(), codeExecutionResult: z.object({outcome: z.enum(["OUTCOME_UNSPECIFIED", "OUTCOME_OK", "OUTCOME_FAILED", "OUTCOME_DEADLINE_EXCEEDED"]).optional(), output: z.string().optional()}).optional(), videoMetadata: z.object({fps: z.number().optional(), startOffset: z.string().optional(), endOffset: z.string().optional()}).optional()}); export const geminiContent = z.object({role: z.enum(["user", "model"]).optional(), parts: z.array(geminiPart).optional()}); export const geminiGenerationResponse = z.object({candidates: z.array(z.object({content: geminiContent.optional(), finishReason: z.enum(["FINISH_REASON_UNSPECIFIED", "STOP", "MAX_TOKENS", "SAFETY", "RECITATION", "LANGUAGE", "OTHER", "BLOCKLIST", "PROHIBITED_CONTENT", "SPII", "MALFORMED_FUNCTION_CALL", "IMAGE_SAFETY", "UNEXPECTED_TOOL_CALL"]).optional(), finishMessage: z.string().optional(), tokenCount: z.number().int().optional(), safetyRatings: z.array(z.object({category: z.string().optional(), probability: z.string().optional(), probabilityScore: z.number().optional(), severity: z.string().optional(), severityScore: z.number().optional(), blocked: z.boolean().optional(), overwrittenThreshold: z.string().optional()})).optional(), citationMetadata: z.object({}).optional(), index: z.number().int().optional(), groundingMetadata: z.object({}).optional(), urlContextMetadata: z.object({urlMetadata: z.array(z.object({retrievedUrl: z.string().optional(), urlRetrievalStatus: z.string().optional()})).optional()}).optional(), avgLogprobs: z.number().optional(), logprobsResult: z.object({chosenCandidates: z.array(z.object({token: z.string().optional(), tokenId: z.number().int().optional(), logProbability: z.number().optional()})).optional(), topCandidates: z.array(z.object({candidates: z.array(z.object({token: z.string().optional(), tokenId: z.number().int().optional(), logProbability: z.number().optional()})).optional()})).optional()}).optional()})).optional(), promptFeedback: z.object({blockReason: z.string().optional(), blockReasonMessage: z.string().optional(), safetyRatings: z.array(z.object({category: z.string().optional(), probability: z.string().optional(), probabilityScore: z.number().optional(), severity: z.string().optional(), severityScore: z.number().optional(), blocked: z.boolean().optional(), overwrittenThreshold: z.string().optional()})).optional()}).optional(), usageMetadata: z.object({promptTokenCount: z.number().int().optional(), candidatesTokenCount: z.number().int().optional(), totalTokenCount: z.number().int().optional(), cachedContentTokenCount: z.number().int().optional(), thoughtsTokenCount: z.number().int().optional(), toolUsePromptTokenCount: z.number().int().optional(), trafficType: z.string().optional(), cacheTokensDetails: z.array(z.object({modality: z.string().optional(), tokenCount: z.number().int().optional()})).optional(), candidatesTokensDetails: z.array(z.object({modality: z.string().optional(), tokenCount: z.number().int().optional()})).optional(), promptTokensDetails: z.array(z.object({modality: z.string().optional(), tokenCount: z.number().int().optional()})).optional(), toolUsePromptTokensDetails: z.array(z.object({modality: z.string().optional(), tokenCount: z.number().int().optional()})).optional()}).optional(), modelVersion: z.string().optional(), responseId: z.string().optional(), createTime: 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 }; /** * Generate image (Gemini format) * * POST /genai/v1beta/models/{model}:predict * * Also accepts auth: BasicAuth, VirtualKeyAuth, ApiKeyAuth */ export const createApiGenaiV1betaModelsModelPredict = async (args: CreateApiGenaiV1betaModelsModelPredictArgs, options?: CreateApiGenaiV1betaModelsModelPredictOptions) => { const {baseUrl = "http://localhost:8080", 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(/\/+$/, '')}/genai/v1beta/models/${encodeURIComponent(String(args.model))}:predict${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(geminiGenerationResponse, await res.json()) };