Open History to see the last commit
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 { beginGoogleDocsPoll, googleDocsFailures } from '../google-docs.ts'
13import { fetchText, sha256Text } from '../types.ts'
14import type {
15 ListModelsResult,
16 ProviderConfig,
17 ProviderSecrets,
18 SpecFetchResult,
19} from '../types.ts'
20import {
21 VERTEX_LOCATIONS_URL,
22 vertexEndpoint,
23 vertexModelList,
24} from '../vertex-catalog.ts'
25import { VERTEX_DISCOVERY_URL, vertexOpenApi } from '../vertex-spec.ts'
26
27const VERB_ACTIVITY: Record<string, Activity> = {
28 generateContent: 'chat',
29 streamGenerateContent: 'chat',
30 countTokens: 'chat',
31 embedContent: 'embeddings',
32 predict: 'image',
33 predictLongRunning: 'video',
34 fetchPredictOperation: 'video',
35}
36
37async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
38 const text = await fetchText(VERTEX_DISCOVERY_URL)
39 const spec = vertexOpenApi(JSON.parse(text) as DiscoveryDoc)
40 return {
41 specs: [spec],
42 sources: [{ url: VERTEX_DISCOVERY_URL, hash: await sha256Text(text) }],
43 outputStrategy: 'post-200',
44 }
45}
46
47async function listModels(
48 _env: ProviderSecrets,
49 kv?: KVNamespace,
50): Promise<ListModelsResult> {
51 beginGoogleDocsPoll()
52 return {
53 models: await vertexModelList(kv),
54 docsFailures: googleDocsFailures(),
55 }
56}
57
58export const provider: ProviderConfig = {
59 id: 'google-vertex',
60 displayName: 'Gemini Enterprise Agent Platform',
61 specSourceUrl: VERTEX_DISCOVERY_URL,
62 modelsEndpoint: VERTEX_LOCATIONS_URL,
63 defaultDerivation: 'upstream-spec',
64 connect: bearerConnect(
65 'https://aiplatform.googleapis.com',
66 'Global endpoint. A regional call uses https://LOCATION-aiplatform.googleapis.com with the same path.',
67 ),
68 fetchSpec,
69 listModels,
70 classify: (path): Activity | null => {
71 const verb = /:([A-Za-z]+)$/.exec(path)?.[1]
72 if (!verb) return null
73 return VERB_ACTIVITY[verb] ?? null
74 },
75 generationEndpointId: ({ rawId }) => vertexEndpoint(rawId),
76}