import {z} from 'zod' export type GoogleCloudAiplatformV1AudioTranscriptionWordInfo = {endOffset?: unknown | undefined, startOffset?: unknown | undefined, word?: string | undefined}; export type GoogleCloudAiplatformV1AudioTranscription = {speakerLabel?: string | undefined, text?: string | undefined, words?: Array | undefined}; export type GoogleCloudAiplatformV1CodeExecutionResult = {id?: string | undefined, outcome?: ('OUTCOME_UNSPECIFIED' | 'OUTCOME_OK' | 'OUTCOME_FAILED' | 'OUTCOME_DEADLINE_EXCEEDED') | undefined, output?: string | undefined}; export type GoogleCloudAiplatformV1ExecutableCode = {code?: string | undefined, id?: string | undefined, language?: ('LANGUAGE_UNSPECIFIED' | 'PYTHON') | undefined}; export type GoogleCloudAiplatformV1FileData = {displayName?: string | undefined, fileUri?: string | undefined, mimeType?: string | undefined}; export type GoogleCloudAiplatformV1PartialArg = {boolValue?: boolean | undefined, jsonPath?: string | undefined, nullValue?: 'NULL_VALUE' | undefined, numberValue?: number | undefined, stringValue?: string | undefined, willContinue?: boolean | undefined}; export type GoogleCloudAiplatformV1FunctionCall = {args?: {[key: string]: unknown} | undefined, id?: string | undefined, name?: string | undefined, partialArgs?: Array | undefined, willContinue?: boolean | undefined}; export type GoogleCloudAiplatformV1FunctionResponseFileData = {displayName?: string | undefined, fileUri?: string | undefined, mimeType?: string | undefined}; export type GoogleCloudAiplatformV1FunctionResponseBlob = {data?: string | undefined, displayName?: string | undefined, mimeType?: string | undefined}; export type GoogleCloudAiplatformV1FunctionResponsePart = {fileData?: GoogleCloudAiplatformV1FunctionResponseFileData | undefined, inlineData?: GoogleCloudAiplatformV1FunctionResponseBlob | undefined}; export type GoogleCloudAiplatformV1FunctionResponse = {id?: string | undefined, name?: string | undefined, parts?: Array | undefined, response?: {[key: string]: unknown} | undefined, scheduling?: ('SCHEDULING_UNSPECIFIED' | 'SILENT' | 'WHEN_IDLE' | 'INTERRUPT') | undefined}; export type GoogleCloudAiplatformV1Blob = {data?: string | undefined, displayName?: string | undefined, mimeType?: string | undefined}; export type GoogleCloudAiplatformV1PartMediaResolution = {level?: ('MEDIA_RESOLUTION_UNSPECIFIED' | 'MEDIA_RESOLUTION_LOW' | 'MEDIA_RESOLUTION_MEDIUM' | 'MEDIA_RESOLUTION_HIGH' | 'MEDIA_RESOLUTION_ULTRA_HIGH') | undefined}; export type GoogleCloudAiplatformV1SpeechMetadata = {speaker?: string | undefined, style?: string | undefined}; export type GoogleCloudAiplatformV1VideoMetadata = {endOffset?: unknown | undefined, fps?: number | undefined, startOffset?: unknown | undefined}; export type GoogleCloudAiplatformV1Part = {audioTranscription?: GoogleCloudAiplatformV1AudioTranscription | undefined, codeExecutionResult?: GoogleCloudAiplatformV1CodeExecutionResult | undefined, executableCode?: GoogleCloudAiplatformV1ExecutableCode | undefined, fileData?: GoogleCloudAiplatformV1FileData | undefined, functionCall?: GoogleCloudAiplatformV1FunctionCall | undefined, functionResponse?: GoogleCloudAiplatformV1FunctionResponse | undefined, inlineData?: GoogleCloudAiplatformV1Blob | undefined, mediaResolution?: GoogleCloudAiplatformV1PartMediaResolution | undefined, speechMetadata?: GoogleCloudAiplatformV1SpeechMetadata | undefined, text?: string | undefined, thought?: boolean | undefined, thoughtSignature?: string | undefined, videoMetadata?: GoogleCloudAiplatformV1VideoMetadata | undefined}; export type GoogleCloudAiplatformV1Content = {parts?: Array | undefined, role?: string | undefined}; export type GoogleCloudAiplatformV1ComputeTokensRequest = {contents?: Array | undefined, instances?: Array | undefined, model?: string | undefined}; export type CreateApiV1EndpointsEndpointsIdComputeTokensArgs = {endpointsId: string, body: GoogleCloudAiplatformV1ComputeTokensRequest}; export type CreateApiV1EndpointsEndpointsIdComputeTokensOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, Oauth2c?: string | undefined, fetch?: typeof fetch | undefined}; export const googleCloudAiplatformV1TokensInfo = z.object({role: z.string().optional(), tokenIds: z.array(z.string()).optional(), tokens: z.array(z.string()).optional()}); export const googleCloudAiplatformV1ComputeTokensResponse = z.object({tokensInfo: z.array(googleCloudAiplatformV1TokensInfo).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 }; /** * Return a list of tokens based on the input text. * * POST /v1/endpoints/{endpointsId}:computeTokens */ export const createApiV1EndpointsEndpointsIdComputeTokens = async (args: CreateApiV1EndpointsEndpointsIdComputeTokensArgs, options?: CreateApiV1EndpointsEndpointsIdComputeTokensOptions) => { const {baseUrl = "https://aiplatform.googleapis.com", fetch: fetchFn = fetch, Oauth2, Oauth2c} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) if (Oauth2c !== undefined) headers.set('authorization', `Bearer ${Oauth2c}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/endpoints/${encodeURIComponent(String(args.endpointsId))}:computeTokens${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(googleCloudAiplatformV1ComputeTokensResponse, await res.json()) };