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 GeminiEmbeddingRequest = {model?: string | undefined, content?: GeminiContent | undefined, taskType?: string | undefined, title?: string | undefined, outputDimensionality?: number | undefined}; export type CreateApiGenaiV1betaModelsModelEmbedContentArgs = {model: string, body: GeminiEmbeddingRequest}; export type CreateApiGenaiV1betaModelsModelEmbedContentOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const geminiEmbeddingResponse = z.object({embeddings: z.array(z.object({values: z.array(z.number()).optional(), statistics: z.object({tokenCount: z.number().int().optional()}).optional()})).optional(), metadata: z.object({billableCharacterCount: 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 }; /** * Embed content (Gemini format) * * POST /genai/v1beta/models/{model}:embedContent * * Also accepts auth: BasicAuth, VirtualKeyAuth, ApiKeyAuth */ export const createApiGenaiV1betaModelsModelEmbedContent = async (args: CreateApiGenaiV1betaModelsModelEmbedContentArgs, options?: CreateApiGenaiV1betaModelsModelEmbedContentOptions) => { 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))}:embedContent${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(geminiEmbeddingResponse, await res.json()) };