Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

google-vertex.ts

71 lines2.2 KB

1/**
2 * Gemini Enterprise Agent Platform, formerly Vertex AI (issue #203).
3 * The id stays `google-vertex` so stored rows and URLs keep their place.
4 * Models and prices come from Google's public docs. Schemas come from
5 * the public discovery document, limited to publisher model methods.
6 * Partner models stay on their own providers. No service account.
7 */
8import type { Activity } from '#/db/schema.ts'
9​
10import { bearerConnect } from '../connect.ts'
11import type { DiscoveryDoc } from '../gemini.ts'
12import { fetchText, sha256Text } from '../types.ts'
13import type {
14 ListModelsResult,
15 ProviderConfig,
16 ProviderSecrets,
17 SpecFetchResult,
18} from '../types.ts'
19import {
20 VERTEX_LOCATIONS_URL,
21 vertexEndpoint,
22 vertexModelList,
23} from '../vertex-catalog.ts'
24import { VERTEX_DISCOVERY_URL, vertexOpenApi } from '../vertex-spec.ts'
25​
26const VERB_ACTIVITY: Record<string, Activity> = {
27 generateContent: 'chat',
28 streamGenerateContent: 'chat',
29 countTokens: 'chat',
30 embedContent: 'embeddings',
31 predict: 'image',
32 predictLongRunning: 'video',
33 fetchPredictOperation: 'video',
35​
36async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
37 const text = await fetchText(VERTEX_DISCOVERY_URL)
38 const spec = vertexOpenApi(JSON.parse(text) as DiscoveryDoc)
39 return {
40 specs: [spec],
41 sources: [{ url: VERTEX_DISCOVERY_URL, hash: await sha256Text(text) }],
42 outputStrategy: 'post-200',
43 }
45​
46async function listModels(
47 _env: ProviderSecrets,
48 kv?: KVNamespace,
49): Promise<ListModelsResult> {
50 return { models: await vertexModelList(kv) }
52​
53export const provider: ProviderConfig = {
54 id: 'google-vertex',
55 displayName: 'Gemini Enterprise Agent Platform',
56 specSourceUrl: VERTEX_DISCOVERY_URL,
57 modelsEndpoint: VERTEX_LOCATIONS_URL,
58 defaultDerivation: 'upstream-spec',
59 connect: bearerConnect(
60 'https://aiplatform.googleapis.com',
61 'Global endpoint. A regional call uses https://LOCATION-aiplatform.googleapis.com with the same path.',
62 ),
63 fetchSpec,
64 listModels,
65 classify: (path): Activity | null => {
66 const verb = /:([A-Za-z]+)$/.exec(path)?.[1]
67 if (!verb) return null
68 return VERB_ACTIVITY[verb] ?? null
69 },
70 generationEndpointId: ({ rawId }) => vertexEndpoint(rawId),