@terreno/ai 0.16.1 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/service/gemini.d.ts +30 -0
- package/dist/service/gemini.js +154 -0
- package/dist/service/index.d.ts +4 -0
- package/dist/service/index.js +13 -1
- package/dist/service/vertex.d.ts +115 -0
- package/dist/service/vertex.js +373 -0
- package/package.json +4 -3
- package/src/service/gemini.test.ts +96 -0
- package/src/service/gemini.ts +113 -0
- package/src/service/index.ts +19 -0
- package/src/service/vertex.test.ts +260 -0
- package/src/service/vertex.ts +339 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import type {ImageModel, LanguageModel} from "ai";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
assertVertexModelsEnabled,
|
|
6
|
+
createVertexProvider,
|
|
7
|
+
isVertexModelAllowed,
|
|
8
|
+
listEnabledVertexModels,
|
|
9
|
+
normalizeVertexModelId,
|
|
10
|
+
type VertexLanguageModelProvider,
|
|
11
|
+
verifyVertexModelsEnabled,
|
|
12
|
+
} from "./vertex";
|
|
13
|
+
|
|
14
|
+
const makeLanguageModel = (modelId: string): LanguageModel =>
|
|
15
|
+
({modelId, provider: "vertex"}) as unknown as LanguageModel;
|
|
16
|
+
|
|
17
|
+
const makeImageModel = (modelId: string): ImageModel =>
|
|
18
|
+
({modelId, provider: "vertex"}) as unknown as ImageModel;
|
|
19
|
+
|
|
20
|
+
const makeVertexFactory = (): ((opts: {
|
|
21
|
+
location: string;
|
|
22
|
+
project: string;
|
|
23
|
+
}) => VertexLanguageModelProvider) => {
|
|
24
|
+
return () => {
|
|
25
|
+
const provider = ((modelId: string) =>
|
|
26
|
+
makeLanguageModel(modelId)) as VertexLanguageModelProvider;
|
|
27
|
+
provider.image = (modelId: string) => makeImageModel(modelId);
|
|
28
|
+
return provider;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
describe("vertex helpers", () => {
|
|
33
|
+
const originalProject = process.env.GOOGLE_VERTEX_PROJECT;
|
|
34
|
+
const originalLocation = process.env.GOOGLE_VERTEX_LOCATION;
|
|
35
|
+
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
delete process.env.GOOGLE_VERTEX_PROJECT;
|
|
38
|
+
delete process.env.GOOGLE_VERTEX_LOCATION;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterEach(() => {
|
|
42
|
+
if (originalProject === undefined) {
|
|
43
|
+
delete process.env.GOOGLE_VERTEX_PROJECT;
|
|
44
|
+
} else {
|
|
45
|
+
process.env.GOOGLE_VERTEX_PROJECT = originalProject;
|
|
46
|
+
}
|
|
47
|
+
if (originalLocation === undefined) {
|
|
48
|
+
delete process.env.GOOGLE_VERTEX_LOCATION;
|
|
49
|
+
} else {
|
|
50
|
+
process.env.GOOGLE_VERTEX_LOCATION = originalLocation;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("normalizeVertexModelId", () => {
|
|
55
|
+
it("strips publisher resource prefixes", () => {
|
|
56
|
+
expect(normalizeVertexModelId("publishers/google/models/gemini-2.5-flash")).toBe(
|
|
57
|
+
"gemini-2.5-flash"
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("strips version suffixes", () => {
|
|
62
|
+
expect(normalizeVertexModelId("gemini-2.5-pro@001")).toBe("gemini-2.5-pro");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("returns bare ids unchanged", () => {
|
|
66
|
+
expect(normalizeVertexModelId("gemini-2.5-flash")).toBe("gemini-2.5-flash");
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe("isVertexModelAllowed", () => {
|
|
71
|
+
it("allows all models when no allow-list is set", () => {
|
|
72
|
+
expect(isVertexModelAllowed("any-model")).toBe(true);
|
|
73
|
+
expect(isVertexModelAllowed("any-model", [])).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("restricts to listed models when an allow-list is provided", () => {
|
|
77
|
+
expect(isVertexModelAllowed("gemini-2.5-flash", ["gemini-2.5-flash"])).toBe(true);
|
|
78
|
+
expect(isVertexModelAllowed("gemini-2.5-pro", ["gemini-2.5-flash"])).toBe(false);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("normalizes both the model id and allow-list entries before comparing", () => {
|
|
82
|
+
expect(isVertexModelAllowed("gemini-2.5-flash", ["gemini-2.5-flash@001"])).toBe(true);
|
|
83
|
+
expect(
|
|
84
|
+
isVertexModelAllowed("publishers/google/models/gemini-2.5-flash", ["gemini-2.5-flash"])
|
|
85
|
+
).toBe(true);
|
|
86
|
+
expect(isVertexModelAllowed("gemini-2.5-flash@001", ["gemini-2.5-pro"])).toBe(false);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe("createVertexProvider", () => {
|
|
91
|
+
it("returns undefined when no project is configured", () => {
|
|
92
|
+
const provider = createVertexProvider({vertexFactory: makeVertexFactory()});
|
|
93
|
+
expect(provider).toBeUndefined();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("permits all models by default", () => {
|
|
97
|
+
const provider = createVertexProvider({
|
|
98
|
+
project: "demo-project",
|
|
99
|
+
vertexFactory: makeVertexFactory(),
|
|
100
|
+
});
|
|
101
|
+
expect(provider).toBeDefined();
|
|
102
|
+
expect(provider?.allowedModels).toBeUndefined();
|
|
103
|
+
expect(provider?.isModelAllowed("gemini-anything")).toBe(true);
|
|
104
|
+
expect(provider?.languageModel("gemini-anything")).toBeDefined();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("defaults location from env then us-central1", () => {
|
|
108
|
+
const withDefault = createVertexProvider({
|
|
109
|
+
project: "demo-project",
|
|
110
|
+
vertexFactory: makeVertexFactory(),
|
|
111
|
+
});
|
|
112
|
+
expect(withDefault?.location).toBe("us-central1");
|
|
113
|
+
|
|
114
|
+
const withExplicit = createVertexProvider({
|
|
115
|
+
location: "europe-west1",
|
|
116
|
+
project: "demo-project",
|
|
117
|
+
vertexFactory: makeVertexFactory(),
|
|
118
|
+
});
|
|
119
|
+
expect(withExplicit?.location).toBe("europe-west1");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("enforces the allow-list when provided", () => {
|
|
123
|
+
const provider = createVertexProvider({
|
|
124
|
+
allowedModels: ["gemini-2.5-flash"],
|
|
125
|
+
project: "demo-project",
|
|
126
|
+
vertexFactory: makeVertexFactory(),
|
|
127
|
+
});
|
|
128
|
+
expect(provider?.isModelAllowed("gemini-2.5-flash")).toBe(true);
|
|
129
|
+
expect(provider?.isModelAllowed("gemini-2.5-pro")).toBe(false);
|
|
130
|
+
expect(provider?.languageModel("gemini-2.5-flash")).toBeDefined();
|
|
131
|
+
expect(() => provider?.languageModel("gemini-2.5-pro")).toThrow();
|
|
132
|
+
expect(() => provider?.imageModel("imagen-4.0-fast-generate-001")).toThrow();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("resolves versioned/path model ids against the allow-list", () => {
|
|
136
|
+
const provider = createVertexProvider({
|
|
137
|
+
allowedModels: ["gemini-2.5-flash@001"],
|
|
138
|
+
project: "demo-project",
|
|
139
|
+
vertexFactory: makeVertexFactory(),
|
|
140
|
+
});
|
|
141
|
+
expect(provider?.isModelAllowed("gemini-2.5-flash")).toBe(true);
|
|
142
|
+
expect(provider?.languageModel("gemini-2.5-flash")).toBeDefined();
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe("verifyVertexModelsEnabled", () => {
|
|
147
|
+
it("marks models available/unavailable based on the listing", async () => {
|
|
148
|
+
const result = await verifyVertexModelsEnabled({
|
|
149
|
+
listModelsFn: mock(async () => ["gemini-2.5-flash", "gemini-2.0-flash-lite"]),
|
|
150
|
+
models: ["gemini-2.5-flash", "gemini-2.5-pro"],
|
|
151
|
+
project: "demo-project",
|
|
152
|
+
});
|
|
153
|
+
expect(result.checked).toBe(true);
|
|
154
|
+
expect(result.available).toEqual(["gemini-2.5-flash"]);
|
|
155
|
+
expect(result.unavailable).toEqual(["gemini-2.5-pro"]);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("returns checked=false when the listing is unavailable", async () => {
|
|
159
|
+
const result = await verifyVertexModelsEnabled({
|
|
160
|
+
listModelsFn: mock(async () => undefined),
|
|
161
|
+
models: ["gemini-2.5-flash"],
|
|
162
|
+
project: "demo-project",
|
|
163
|
+
});
|
|
164
|
+
expect(result.checked).toBe(false);
|
|
165
|
+
expect(result.available).toEqual(["gemini-2.5-flash"]);
|
|
166
|
+
expect(result.unavailable).toEqual([]);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe("assertVertexModelsEnabled", () => {
|
|
171
|
+
it("throws when a requested model is not enabled", async () => {
|
|
172
|
+
await expect(
|
|
173
|
+
assertVertexModelsEnabled({
|
|
174
|
+
listModelsFn: mock(async () => ["gemini-2.5-flash"]),
|
|
175
|
+
models: ["gemini-2.5-pro"],
|
|
176
|
+
project: "demo-project",
|
|
177
|
+
})
|
|
178
|
+
).rejects.toThrow();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("does not throw when verification is inconclusive", async () => {
|
|
182
|
+
const result = await assertVertexModelsEnabled({
|
|
183
|
+
listModelsFn: mock(async () => undefined),
|
|
184
|
+
models: ["gemini-2.5-pro"],
|
|
185
|
+
project: "demo-project",
|
|
186
|
+
});
|
|
187
|
+
expect(result.checked).toBe(false);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
describe("listEnabledVertexModels", () => {
|
|
192
|
+
it("paginates and normalizes publisher model names", async () => {
|
|
193
|
+
const fetchImpl = mock(async (input: string | URL) => {
|
|
194
|
+
const url = new URL(input.toString());
|
|
195
|
+
const isSecondPage = url.searchParams.get("pageToken") === "page-2";
|
|
196
|
+
const body = isSecondPage
|
|
197
|
+
? {publisherModels: [{name: "publishers/google/models/gemini-2.5-pro"}]}
|
|
198
|
+
: {
|
|
199
|
+
nextPageToken: "page-2",
|
|
200
|
+
publisherModels: [{name: "publishers/google/models/gemini-2.5-flash"}],
|
|
201
|
+
};
|
|
202
|
+
return {
|
|
203
|
+
json: async () => body,
|
|
204
|
+
ok: true,
|
|
205
|
+
status: 200,
|
|
206
|
+
} as unknown as Response;
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const models = await listEnabledVertexModels({
|
|
210
|
+
fetchImpl: fetchImpl as unknown as typeof fetch,
|
|
211
|
+
getAccessToken: async () => "fake-token",
|
|
212
|
+
location: "us-central1",
|
|
213
|
+
project: "demo-project",
|
|
214
|
+
});
|
|
215
|
+
expect(models).toEqual(["gemini-2.5-flash", "gemini-2.5-pro"]);
|
|
216
|
+
expect(fetchImpl).toHaveBeenCalledTimes(2);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("returns undefined when no access token is available", async () => {
|
|
220
|
+
const models = await listEnabledVertexModels({
|
|
221
|
+
fetchImpl: mock(async () => ({}) as unknown as Response) as unknown as typeof fetch,
|
|
222
|
+
getAccessToken: async () => undefined,
|
|
223
|
+
project: "demo-project",
|
|
224
|
+
});
|
|
225
|
+
expect(models).toBeUndefined();
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("returns undefined when more pages remain than the max page budget", async () => {
|
|
229
|
+
const fetchImpl = mock(async () => {
|
|
230
|
+
return {
|
|
231
|
+
json: async () => ({
|
|
232
|
+
nextPageToken: "always-more",
|
|
233
|
+
publisherModels: [{name: "publishers/google/models/gemini-2.5-flash"}],
|
|
234
|
+
}),
|
|
235
|
+
ok: true,
|
|
236
|
+
status: 200,
|
|
237
|
+
} as unknown as Response;
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
const models = await listEnabledVertexModels({
|
|
241
|
+
fetchImpl: fetchImpl as unknown as typeof fetch,
|
|
242
|
+
getAccessToken: async () => "fake-token",
|
|
243
|
+
project: "demo-project",
|
|
244
|
+
});
|
|
245
|
+
expect(models).toBeUndefined();
|
|
246
|
+
expect(fetchImpl).toHaveBeenCalledTimes(10);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("returns undefined when the listing request fails", async () => {
|
|
250
|
+
const models = await listEnabledVertexModels({
|
|
251
|
+
fetchImpl: mock(
|
|
252
|
+
async () => ({json: async () => ({}), ok: false, status: 403}) as unknown as Response
|
|
253
|
+
) as unknown as typeof fetch,
|
|
254
|
+
getAccessToken: async () => "fake-token",
|
|
255
|
+
project: "demo-project",
|
|
256
|
+
});
|
|
257
|
+
expect(models).toBeUndefined();
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
});
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import {APIError, logger} from "@terreno/api";
|
|
2
|
+
import type {ImageModel, LanguageModel} from "ai";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Helper for Google's Vertex AI provider, now part of the "Gemini Enterprise Agent Platform"
|
|
6
|
+
* (formerly Vertex AI), renamed at Google Cloud Next 2026. The `@ai-sdk/google-vertex` provider
|
|
7
|
+
* and the underlying `*-aiplatform.googleapis.com` REST endpoints are unchanged by the rename;
|
|
8
|
+
* this module centralizes provider creation, model allow-listing, and enabled-model verification
|
|
9
|
+
* against the Gemini Enterprise Agent Platform (Vertex AI) APIs.
|
|
10
|
+
*
|
|
11
|
+
* Behavior:
|
|
12
|
+
* - By default ALL Vertex models are permitted (no allow-list).
|
|
13
|
+
* - Consumers may pass `allowedModels` to restrict which models can be resolved when initializing.
|
|
14
|
+
* - `verifyVertexModelsEnabled` / `assertVertexModelsEnabled` confirm that the requested models are
|
|
15
|
+
* actually enabled/available for the project using the Google publisher-models listing API.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** Default Vertex / Gemini Enterprise Agent Platform region. */
|
|
19
|
+
export const DEFAULT_VERTEX_LOCATION = "us-central1";
|
|
20
|
+
|
|
21
|
+
const CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
|
|
22
|
+
const MAX_MODEL_LIST_PAGES = 10;
|
|
23
|
+
const MODEL_LIST_PAGE_SIZE = "200";
|
|
24
|
+
|
|
25
|
+
/** Provider returned by `@ai-sdk/google-vertex` `createVertex`. */
|
|
26
|
+
export interface VertexLanguageModelProvider {
|
|
27
|
+
(modelId: string): LanguageModel;
|
|
28
|
+
image: (modelId: string) => ImageModel;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface VertexModule {
|
|
32
|
+
createVertex: (opts: {location: string; project: string}) => VertexLanguageModelProvider;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface CreateVertexProviderOptions {
|
|
36
|
+
/**
|
|
37
|
+
* Optional allow-list of model ids consumers may use. When omitted or empty (the default), ALL
|
|
38
|
+
* Vertex models are permitted. When provided, only listed models may be resolved.
|
|
39
|
+
*/
|
|
40
|
+
allowedModels?: string[];
|
|
41
|
+
/** Vertex location/region. Falls back to GOOGLE_VERTEX_LOCATION, then `us-central1`. */
|
|
42
|
+
location?: string;
|
|
43
|
+
/** GCP project id. Falls back to the GOOGLE_VERTEX_PROJECT env var. */
|
|
44
|
+
project?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Injectable factory for the `@ai-sdk/google-vertex` provider, primarily for testing. When
|
|
47
|
+
* omitted, the provider is loaded dynamically from `@ai-sdk/google-vertex`.
|
|
48
|
+
*/
|
|
49
|
+
vertexFactory?: (opts: {location: string; project: string}) => VertexLanguageModelProvider;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface TerrenoVertexProvider {
|
|
53
|
+
/** Configured allow-list (`undefined` means all models are allowed). */
|
|
54
|
+
allowedModels?: string[];
|
|
55
|
+
/** Resolve an image model, enforcing the allow-list. Throws `APIError(400)` when disallowed. */
|
|
56
|
+
imageModel: (modelId: string) => ImageModel;
|
|
57
|
+
/** Returns true when the model id is permitted by the configured allow-list. */
|
|
58
|
+
isModelAllowed: (modelId: string) => boolean;
|
|
59
|
+
/** Resolve a language model, enforcing the allow-list. Throws `APIError(400)` when disallowed. */
|
|
60
|
+
languageModel: (modelId: string) => LanguageModel;
|
|
61
|
+
/** Configured Vertex location. */
|
|
62
|
+
location: string;
|
|
63
|
+
/** Configured GCP project id. */
|
|
64
|
+
project: string;
|
|
65
|
+
/** Underlying `@ai-sdk/google-vertex` provider. */
|
|
66
|
+
raw: VertexLanguageModelProvider;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface ListEnabledVertexModelsOptions {
|
|
70
|
+
/** Injectable fetch implementation (defaults to the global `fetch`). */
|
|
71
|
+
fetchImpl?: typeof fetch;
|
|
72
|
+
/** Injectable access-token getter (defaults to Application Default Credentials). */
|
|
73
|
+
getAccessToken?: () => Promise<string | undefined>;
|
|
74
|
+
/** Vertex location/region. Falls back to GOOGLE_VERTEX_LOCATION, then `us-central1`. */
|
|
75
|
+
location?: string;
|
|
76
|
+
/** GCP project id. */
|
|
77
|
+
project: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface VertexModelAvailability {
|
|
81
|
+
/** Requested models confirmed available/enabled via the Google API. */
|
|
82
|
+
available: string[];
|
|
83
|
+
/** Whether the Google API listing was successfully retrieved (false means it was skipped). */
|
|
84
|
+
checked: boolean;
|
|
85
|
+
/** Requested models that were NOT found in the Google API listing. */
|
|
86
|
+
unavailable: string[];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface VerifyVertexModelsOptions extends ListEnabledVertexModelsOptions {
|
|
90
|
+
/** Injectable model-listing function, primarily for testing. */
|
|
91
|
+
listModelsFn?: (options: ListEnabledVertexModelsOptions) => Promise<string[] | undefined>;
|
|
92
|
+
/** Models to verify are enabled/available for the project. */
|
|
93
|
+
models: string[];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Normalize a publisher model resource name (e.g. "publishers/google/models/gemini-2.5-flash" or
|
|
98
|
+
* "gemini-2.5-flash@001") down to its bare model id (e.g. "gemini-2.5-flash").
|
|
99
|
+
*/
|
|
100
|
+
export const normalizeVertexModelId = (modelName: string): string => {
|
|
101
|
+
const lastSegment = modelName.trim().split("/").pop() ?? modelName.trim();
|
|
102
|
+
return lastSegment.split("@")[0] ?? lastSegment;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Returns true when the model id is permitted. An empty/undefined allow-list permits all models.
|
|
107
|
+
* Both the requested model id and allow-list entries are normalized (publisher paths and `@version`
|
|
108
|
+
* suffixes stripped) so matching stays consistent with `verifyVertexModelsEnabled`.
|
|
109
|
+
*/
|
|
110
|
+
export const isVertexModelAllowed = (modelId: string, allowedModels?: string[]): boolean => {
|
|
111
|
+
if (!allowedModels || allowedModels.length === 0) {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
const normalized = normalizeVertexModelId(modelId);
|
|
115
|
+
return allowedModels.some((allowed) => normalizeVertexModelId(allowed) === normalized);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const loadVertexModule = (): VertexModule | undefined => {
|
|
119
|
+
try {
|
|
120
|
+
return require("@ai-sdk/google-vertex") as VertexModule;
|
|
121
|
+
} catch {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const getDefaultAccessToken = async (): Promise<string | undefined> => {
|
|
127
|
+
try {
|
|
128
|
+
// google-auth-library is a transitive dependency of @ai-sdk/google-vertex.
|
|
129
|
+
const {GoogleAuth} = require("google-auth-library") as {
|
|
130
|
+
GoogleAuth: new (opts: {
|
|
131
|
+
scopes: string[];
|
|
132
|
+
}) => {
|
|
133
|
+
getClient: () => Promise<{getAccessToken: () => Promise<{token?: string | null}>}>;
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
const auth = new GoogleAuth({scopes: [CLOUD_PLATFORM_SCOPE]});
|
|
137
|
+
const client = await auth.getClient();
|
|
138
|
+
const result = await client.getAccessToken();
|
|
139
|
+
return result?.token ?? undefined;
|
|
140
|
+
} catch (error) {
|
|
141
|
+
logger.warn(
|
|
142
|
+
`Unable to acquire Google access token for Vertex model verification: ${
|
|
143
|
+
(error as Error).message
|
|
144
|
+
}`
|
|
145
|
+
);
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const resolveLocation = (location?: string): string =>
|
|
151
|
+
location ?? process.env.GOOGLE_VERTEX_LOCATION ?? DEFAULT_VERTEX_LOCATION;
|
|
152
|
+
|
|
153
|
+
const aiPlatformHost = (location: string): string =>
|
|
154
|
+
location === "global" ? "aiplatform.googleapis.com" : `${location}-aiplatform.googleapis.com`;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Create a Vertex AI (Gemini Enterprise Agent Platform) provider that enforces an optional model
|
|
158
|
+
* allow-list. Returns `undefined` when no project is configured or `@ai-sdk/google-vertex` is not
|
|
159
|
+
* installed, so callers can fall back to another provider or demo mode.
|
|
160
|
+
*/
|
|
161
|
+
export const createVertexProvider = (
|
|
162
|
+
options: CreateVertexProviderOptions = {}
|
|
163
|
+
): TerrenoVertexProvider | undefined => {
|
|
164
|
+
const project = options.project ?? process.env.GOOGLE_VERTEX_PROJECT;
|
|
165
|
+
if (!project) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const location = resolveLocation(options.location);
|
|
170
|
+
const allowedModels =
|
|
171
|
+
options.allowedModels && options.allowedModels.length > 0 ? options.allowedModels : undefined;
|
|
172
|
+
|
|
173
|
+
const factory = options.vertexFactory ?? loadVertexModule()?.createVertex;
|
|
174
|
+
if (!factory) {
|
|
175
|
+
logger.warn(
|
|
176
|
+
"Vertex AI (Gemini Enterprise Agent Platform) provider requested but @ai-sdk/google-vertex is not installed."
|
|
177
|
+
);
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const raw = factory({location, project});
|
|
182
|
+
|
|
183
|
+
const assertAllowed = (modelId: string): void => {
|
|
184
|
+
if (isVertexModelAllowed(modelId, allowedModels)) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
throw new APIError({
|
|
188
|
+
detail: `Model "${modelId}" is not in the configured Vertex model allow-list (${(
|
|
189
|
+
allowedModels ?? []
|
|
190
|
+
).join(", ")}).`,
|
|
191
|
+
status: 400,
|
|
192
|
+
title: "Model not permitted",
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
allowedModels,
|
|
198
|
+
imageModel: (modelId: string): ImageModel => {
|
|
199
|
+
assertAllowed(modelId);
|
|
200
|
+
return raw.image(modelId);
|
|
201
|
+
},
|
|
202
|
+
isModelAllowed: (modelId: string): boolean => isVertexModelAllowed(modelId, allowedModels),
|
|
203
|
+
languageModel: (modelId: string): LanguageModel => {
|
|
204
|
+
assertAllowed(modelId);
|
|
205
|
+
return raw(modelId);
|
|
206
|
+
},
|
|
207
|
+
location,
|
|
208
|
+
project,
|
|
209
|
+
raw,
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* List the Google publisher models available to a project via the Gemini Enterprise Agent Platform
|
|
215
|
+
* (Vertex AI) `publishers/google/models` endpoint. Returns the normalized model ids, or `undefined`
|
|
216
|
+
* when the listing could not be retrieved (missing credentials, network error, etc.).
|
|
217
|
+
*/
|
|
218
|
+
export const listEnabledVertexModels = async (
|
|
219
|
+
options: ListEnabledVertexModelsOptions
|
|
220
|
+
): Promise<string[] | undefined> => {
|
|
221
|
+
const {project} = options;
|
|
222
|
+
if (!project) {
|
|
223
|
+
return undefined;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
227
|
+
if (!fetchImpl) {
|
|
228
|
+
logger.warn("No fetch implementation available for Vertex model listing.");
|
|
229
|
+
return undefined;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const getToken = options.getAccessToken ?? getDefaultAccessToken;
|
|
233
|
+
const token = await getToken();
|
|
234
|
+
if (!token) {
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const location = resolveLocation(options.location);
|
|
239
|
+
const baseUrl = `https://${aiPlatformHost(location)}/v1beta1/publishers/google/models`;
|
|
240
|
+
const models: string[] = [];
|
|
241
|
+
let pageToken: string | undefined;
|
|
242
|
+
let pages = 0;
|
|
243
|
+
|
|
244
|
+
try {
|
|
245
|
+
do {
|
|
246
|
+
const url = new URL(baseUrl);
|
|
247
|
+
url.searchParams.set("pageSize", MODEL_LIST_PAGE_SIZE);
|
|
248
|
+
if (pageToken) {
|
|
249
|
+
url.searchParams.set("pageToken", pageToken);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const response = await fetchImpl(url.toString(), {
|
|
253
|
+
headers: {Authorization: `Bearer ${token}`},
|
|
254
|
+
});
|
|
255
|
+
if (!response.ok) {
|
|
256
|
+
logger.warn(`Vertex model listing failed with status ${response.status}.`);
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const body = (await response.json()) as {
|
|
261
|
+
nextPageToken?: string;
|
|
262
|
+
publisherModels?: Array<{name?: string}>;
|
|
263
|
+
};
|
|
264
|
+
for (const model of body.publisherModels ?? []) {
|
|
265
|
+
if (model.name) {
|
|
266
|
+
models.push(normalizeVertexModelId(model.name));
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
pageToken = body.nextPageToken;
|
|
270
|
+
pages += 1;
|
|
271
|
+
} while (pageToken && pages < MAX_MODEL_LIST_PAGES);
|
|
272
|
+
} catch (error) {
|
|
273
|
+
logger.warn(`Vertex model listing errored: ${(error as Error).message}`);
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// More pages remained than we were willing to fetch. Returning the partial list would let
|
|
278
|
+
// verification falsely mark enabled models as unavailable, so treat it as inconclusive instead.
|
|
279
|
+
if (pageToken) {
|
|
280
|
+
logger.warn(
|
|
281
|
+
`Vertex model listing exceeded ${MAX_MODEL_LIST_PAGES} pages; treating verification as inconclusive.`
|
|
282
|
+
);
|
|
283
|
+
return undefined;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return models;
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Verify that the requested models are enabled/available for the project using the Google APIs.
|
|
291
|
+
* When the listing cannot be retrieved (e.g. no credentials), `checked` is false and verification
|
|
292
|
+
* is treated as inconclusive rather than failing.
|
|
293
|
+
*/
|
|
294
|
+
export const verifyVertexModelsEnabled = async (
|
|
295
|
+
options: VerifyVertexModelsOptions
|
|
296
|
+
): Promise<VertexModelAvailability> => {
|
|
297
|
+
const {listModelsFn, models, ...listOptions} = options;
|
|
298
|
+
const requested = [...new Set(models.map((model) => normalizeVertexModelId(model)))];
|
|
299
|
+
|
|
300
|
+
const list = listModelsFn ?? listEnabledVertexModels;
|
|
301
|
+
const enabled = await list(listOptions);
|
|
302
|
+
|
|
303
|
+
if (!enabled) {
|
|
304
|
+
return {available: requested, checked: false, unavailable: []};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const enabledSet = new Set(enabled.map((model) => normalizeVertexModelId(model)));
|
|
308
|
+
const available: string[] = [];
|
|
309
|
+
const unavailable: string[] = [];
|
|
310
|
+
for (const model of requested) {
|
|
311
|
+
if (enabledSet.has(model)) {
|
|
312
|
+
available.push(model);
|
|
313
|
+
} else {
|
|
314
|
+
unavailable.push(model);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return {available, checked: true, unavailable};
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Like `verifyVertexModelsEnabled`, but throws an `APIError` when the listing was retrieved and one
|
|
322
|
+
* or more requested models are not enabled/available. Inconclusive checks (no credentials/network)
|
|
323
|
+
* do not throw so local development and offline environments keep working.
|
|
324
|
+
*/
|
|
325
|
+
export const assertVertexModelsEnabled = async (
|
|
326
|
+
options: VerifyVertexModelsOptions
|
|
327
|
+
): Promise<VertexModelAvailability> => {
|
|
328
|
+
const result = await verifyVertexModelsEnabled(options);
|
|
329
|
+
if (result.checked && result.unavailable.length > 0) {
|
|
330
|
+
throw new APIError({
|
|
331
|
+
detail: `The following models are not enabled/available on the Gemini Enterprise Agent Platform (Vertex AI) for this project: ${result.unavailable.join(
|
|
332
|
+
", "
|
|
333
|
+
)}.`,
|
|
334
|
+
status: 400,
|
|
335
|
+
title: "Vertex models unavailable",
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
return result;
|
|
339
|
+
};
|