Open History to see the last commit
1/**
2 * Agent Platform discovery document → the publisher-model methods
3 * (issue #203). The v1 document describes the whole platform. Training
4 * jobs, deployed endpoints, and partner rawPredict stay out of this spec.
5 */
6import { discoveryToOpenApi } from './gemini.ts'
7import type { DiscoveryDoc } from './gemini.ts'
8import type { OpenApiDocument } from './types.ts'
9
10export const VERTEX_DISCOVERY_URL =
11 'https://aiplatform.googleapis.com/$discovery/rest?version=v1'
12
13const VERBS = new Set([
14 'generateContent',
15 'streamGenerateContent',
16 'countTokens',
17 'embedContent',
18 'predict',
19 'predictLongRunning',
20 'fetchPredictOperation',
21])
22
23interface DiscoveryMethod {
24 id?: string
25 path?: string
26 flatPath?: string
27 [key: string]: unknown
28}
29
30interface DiscoveryResource {
31 methods?: Record<string, DiscoveryMethod>
32 resources?: Record<string, DiscoveryResource>
33}
34
35function keepMethod(method: DiscoveryMethod): boolean {
36 const id = method.id
37 if (!id?.includes('.publishers.models.')) return false
38 const verb = id.slice(id.lastIndexOf('.') + 1)
39 if (!VERBS.has(verb)) return false
40 // The short `/v1/publishers/...` aliases repeat the same verbs.
41 const path = method.flatPath ?? method.path ?? ''
42 return path.includes('projects/')
43}
44
45function pruneResources(
46 resources: Record<string, DiscoveryResource> | undefined,
47): Record<string, DiscoveryResource> | undefined {
48 if (!resources) return undefined
49 const out: Record<string, DiscoveryResource> = {}
50 for (const [name, resource] of Object.entries(resources)) {
51 const methods = Object.fromEntries(
52 Object.entries(resource.methods ?? {}).filter(([, method]) =>
53 keepMethod(method),
54 ),
55 )
56 const nested = pruneResources(resource.resources)
57 if (Object.keys(methods).length === 0 && !nested) continue
58 out[name] = {
59 ...(Object.keys(methods).length > 0 ? { methods } : {}),
60 ...(nested ? { resources: nested } : {}),
61 }
62 }
63 return Object.keys(out).length > 0 ? out : undefined
64}
65
66function keptMethods(
67 resources: Record<string, DiscoveryResource> | undefined,
68): Array<DiscoveryMethod> {
69 if (!resources) return []
70 const out: Array<DiscoveryMethod> = []
71 for (const resource of Object.values(resources)) {
72 out.push(...Object.values(resource.methods ?? {}))
73 out.push(...keptMethods(resource.resources))
74 }
75 return out
76}
77
78/** Schemas reachable from the kept methods. A dangling `$ref` throws. */
79function schemaClosure(
80 schemas: NonNullable<DiscoveryDoc['schemas']>,
81 roots: Array<unknown>,
82): NonNullable<DiscoveryDoc['schemas']> {
83 const want = new Set<string>()
84 const seen = new Set<unknown>()
85 const walk = (node: unknown): void => {
86 if (node === null || typeof node !== 'object') return
87 if (seen.has(node)) return
88 seen.add(node)
89 if (Array.isArray(node)) {
90 for (const item of node) walk(item)
91 return
92 }
93 const record = node as Record<string, unknown>
94 const ref = record.$ref
95 if (typeof ref === 'string' && !want.has(ref)) {
96 want.add(ref)
97 const next = schemas[ref]
98 if (!next) throw new Error(`vertex discovery: missing schema ${ref}`)
99 walk(next)
100 }
101 for (const value of Object.values(record)) walk(value)
102 }
103 for (const root of roots) walk(root)
104 return Object.fromEntries(
105 [...want].flatMap((name) => {
106 const schema = schemas[name]
107 return schema ? [[name, schema]] : []
108 }),
109 )
110}
111
112/**
113 * Discovery doc reduced to publisher-model generation methods and the
114 * schemas those methods reference.
115 */
116export function publisherModelDiscovery(doc: DiscoveryDoc): DiscoveryDoc {
117 const resources = pruneResources(
118 doc.resources as Record<string, DiscoveryResource> | undefined,
119 )
120 const methods = keptMethods(resources)
121 if (methods.length === 0) {
122 throw new Error('vertex discovery: no publisher model methods')
123 }
124 return {
125 ...doc,
126 resources: resources as DiscoveryDoc['resources'],
127 schemas: schemaClosure(doc.schemas ?? {}, methods),
128 }
129}
130
131export function vertexOpenApi(doc: DiscoveryDoc): OpenApiDocument {
132 return discoveryToOpenApi(publisherModelDiscovery(doc))
133}