@terreno/ai 0.16.1 → 0.18.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,373 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
39
|
+
var t = {};
|
|
40
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
41
|
+
t[p] = s[p];
|
|
42
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
43
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
44
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
45
|
+
t[p[i]] = s[p[i]];
|
|
46
|
+
}
|
|
47
|
+
return t;
|
|
48
|
+
};
|
|
49
|
+
var __values = (this && this.__values) || function(o) {
|
|
50
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
51
|
+
if (m) return m.call(o);
|
|
52
|
+
if (o && typeof o.length === "number") return {
|
|
53
|
+
next: function () {
|
|
54
|
+
if (o && i >= o.length) o = void 0;
|
|
55
|
+
return { value: o && o[i++], done: !o };
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
59
|
+
};
|
|
60
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
61
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
62
|
+
if (!m) return o;
|
|
63
|
+
var i = m.call(o), r, ar = [], e;
|
|
64
|
+
try {
|
|
65
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
66
|
+
}
|
|
67
|
+
catch (error) { e = { error: error }; }
|
|
68
|
+
finally {
|
|
69
|
+
try {
|
|
70
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
71
|
+
}
|
|
72
|
+
finally { if (e) throw e.error; }
|
|
73
|
+
}
|
|
74
|
+
return ar;
|
|
75
|
+
};
|
|
76
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
77
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
78
|
+
if (ar || !(i in from)) {
|
|
79
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
80
|
+
ar[i] = from[i];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
84
|
+
};
|
|
85
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
86
|
+
exports.assertVertexModelsEnabled = exports.verifyVertexModelsEnabled = exports.listEnabledVertexModels = exports.createVertexProvider = exports.isVertexModelAllowed = exports.normalizeVertexModelId = exports.DEFAULT_VERTEX_LOCATION = void 0;
|
|
87
|
+
var api_1 = require("@terreno/api");
|
|
88
|
+
/**
|
|
89
|
+
* Helper for Google's Vertex AI provider, now part of the "Gemini Enterprise Agent Platform"
|
|
90
|
+
* (formerly Vertex AI), renamed at Google Cloud Next 2026. The `@ai-sdk/google-vertex` provider
|
|
91
|
+
* and the underlying `*-aiplatform.googleapis.com` REST endpoints are unchanged by the rename;
|
|
92
|
+
* this module centralizes provider creation, model allow-listing, and enabled-model verification
|
|
93
|
+
* against the Gemini Enterprise Agent Platform (Vertex AI) APIs.
|
|
94
|
+
*
|
|
95
|
+
* Behavior:
|
|
96
|
+
* - By default ALL Vertex models are permitted (no allow-list).
|
|
97
|
+
* - Consumers may pass `allowedModels` to restrict which models can be resolved when initializing.
|
|
98
|
+
* - `verifyVertexModelsEnabled` / `assertVertexModelsEnabled` confirm that the requested models are
|
|
99
|
+
* actually enabled/available for the project using the Google publisher-models listing API.
|
|
100
|
+
*/
|
|
101
|
+
/** Default Vertex / Gemini Enterprise Agent Platform region. */
|
|
102
|
+
exports.DEFAULT_VERTEX_LOCATION = "us-central1";
|
|
103
|
+
var CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
|
|
104
|
+
var MAX_MODEL_LIST_PAGES = 10;
|
|
105
|
+
var MODEL_LIST_PAGE_SIZE = "200";
|
|
106
|
+
/**
|
|
107
|
+
* Normalize a publisher model resource name (e.g. "publishers/google/models/gemini-2.5-flash" or
|
|
108
|
+
* "gemini-2.5-flash@001") down to its bare model id (e.g. "gemini-2.5-flash").
|
|
109
|
+
*/
|
|
110
|
+
var normalizeVertexModelId = function (modelName) {
|
|
111
|
+
var _a, _b;
|
|
112
|
+
var lastSegment = (_a = modelName.trim().split("/").pop()) !== null && _a !== void 0 ? _a : modelName.trim();
|
|
113
|
+
return (_b = lastSegment.split("@")[0]) !== null && _b !== void 0 ? _b : lastSegment;
|
|
114
|
+
};
|
|
115
|
+
exports.normalizeVertexModelId = normalizeVertexModelId;
|
|
116
|
+
/**
|
|
117
|
+
* Returns true when the model id is permitted. An empty/undefined allow-list permits all models.
|
|
118
|
+
* Both the requested model id and allow-list entries are normalized (publisher paths and `@version`
|
|
119
|
+
* suffixes stripped) so matching stays consistent with `verifyVertexModelsEnabled`.
|
|
120
|
+
*/
|
|
121
|
+
var isVertexModelAllowed = function (modelId, allowedModels) {
|
|
122
|
+
if (!allowedModels || allowedModels.length === 0) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
var normalized = (0, exports.normalizeVertexModelId)(modelId);
|
|
126
|
+
return allowedModels.some(function (allowed) { return (0, exports.normalizeVertexModelId)(allowed) === normalized; });
|
|
127
|
+
};
|
|
128
|
+
exports.isVertexModelAllowed = isVertexModelAllowed;
|
|
129
|
+
var loadVertexModule = function () {
|
|
130
|
+
try {
|
|
131
|
+
return require("@ai-sdk/google-vertex");
|
|
132
|
+
}
|
|
133
|
+
catch (_a) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
var getDefaultAccessToken = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
138
|
+
var GoogleAuth, auth, client, result, error_1;
|
|
139
|
+
var _a;
|
|
140
|
+
return __generator(this, function (_b) {
|
|
141
|
+
switch (_b.label) {
|
|
142
|
+
case 0:
|
|
143
|
+
_b.trys.push([0, 3, , 4]);
|
|
144
|
+
GoogleAuth = require("google-auth-library").GoogleAuth;
|
|
145
|
+
auth = new GoogleAuth({ scopes: [CLOUD_PLATFORM_SCOPE] });
|
|
146
|
+
return [4 /*yield*/, auth.getClient()];
|
|
147
|
+
case 1:
|
|
148
|
+
client = _b.sent();
|
|
149
|
+
return [4 /*yield*/, client.getAccessToken()];
|
|
150
|
+
case 2:
|
|
151
|
+
result = _b.sent();
|
|
152
|
+
return [2 /*return*/, (_a = result === null || result === void 0 ? void 0 : result.token) !== null && _a !== void 0 ? _a : undefined];
|
|
153
|
+
case 3:
|
|
154
|
+
error_1 = _b.sent();
|
|
155
|
+
api_1.logger.warn("Unable to acquire Google access token for Vertex model verification: ".concat(error_1.message));
|
|
156
|
+
return [2 /*return*/, undefined];
|
|
157
|
+
case 4: return [2 /*return*/];
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}); };
|
|
161
|
+
var resolveLocation = function (location) { var _a; return (_a = location !== null && location !== void 0 ? location : process.env.GOOGLE_VERTEX_LOCATION) !== null && _a !== void 0 ? _a : exports.DEFAULT_VERTEX_LOCATION; };
|
|
162
|
+
var aiPlatformHost = function (location) {
|
|
163
|
+
return location === "global" ? "aiplatform.googleapis.com" : "".concat(location, "-aiplatform.googleapis.com");
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Create a Vertex AI (Gemini Enterprise Agent Platform) provider that enforces an optional model
|
|
167
|
+
* allow-list. Returns `undefined` when no project is configured or `@ai-sdk/google-vertex` is not
|
|
168
|
+
* installed, so callers can fall back to another provider or demo mode.
|
|
169
|
+
*/
|
|
170
|
+
var createVertexProvider = function (options) {
|
|
171
|
+
var _a, _b, _c;
|
|
172
|
+
if (options === void 0) { options = {}; }
|
|
173
|
+
var project = (_a = options.project) !== null && _a !== void 0 ? _a : process.env.GOOGLE_VERTEX_PROJECT;
|
|
174
|
+
if (!project) {
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
var location = resolveLocation(options.location);
|
|
178
|
+
var allowedModels = options.allowedModels && options.allowedModels.length > 0 ? options.allowedModels : undefined;
|
|
179
|
+
var factory = (_b = options.vertexFactory) !== null && _b !== void 0 ? _b : (_c = loadVertexModule()) === null || _c === void 0 ? void 0 : _c.createVertex;
|
|
180
|
+
if (!factory) {
|
|
181
|
+
api_1.logger.warn("Vertex AI (Gemini Enterprise Agent Platform) provider requested but @ai-sdk/google-vertex is not installed.");
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
var raw = factory({ location: location, project: project });
|
|
185
|
+
var assertAllowed = function (modelId) {
|
|
186
|
+
if ((0, exports.isVertexModelAllowed)(modelId, allowedModels)) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
throw new api_1.APIError({
|
|
190
|
+
detail: "Model \"".concat(modelId, "\" is not in the configured Vertex model allow-list (").concat((allowedModels !== null && allowedModels !== void 0 ? allowedModels : []).join(", "), ")."),
|
|
191
|
+
status: 400,
|
|
192
|
+
title: "Model not permitted",
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
return {
|
|
196
|
+
allowedModels: allowedModels,
|
|
197
|
+
imageModel: function (modelId) {
|
|
198
|
+
assertAllowed(modelId);
|
|
199
|
+
return raw.image(modelId);
|
|
200
|
+
},
|
|
201
|
+
isModelAllowed: function (modelId) { return (0, exports.isVertexModelAllowed)(modelId, allowedModels); },
|
|
202
|
+
languageModel: function (modelId) {
|
|
203
|
+
assertAllowed(modelId);
|
|
204
|
+
return raw(modelId);
|
|
205
|
+
},
|
|
206
|
+
location: location,
|
|
207
|
+
project: project,
|
|
208
|
+
raw: raw,
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
exports.createVertexProvider = createVertexProvider;
|
|
212
|
+
/**
|
|
213
|
+
* List the Google publisher models available to a project via the Gemini Enterprise Agent Platform
|
|
214
|
+
* (Vertex AI) `publishers/google/models` endpoint. Returns the normalized model ids, or `undefined`
|
|
215
|
+
* when the listing could not be retrieved (missing credentials, network error, etc.).
|
|
216
|
+
*/
|
|
217
|
+
var listEnabledVertexModels = function (options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
218
|
+
var project, fetchImpl, getToken, token, location, baseUrl, models, pageToken, pages, url, response, body, _a, _b, model, error_2;
|
|
219
|
+
var e_1, _c;
|
|
220
|
+
var _d, _e, _f;
|
|
221
|
+
return __generator(this, function (_g) {
|
|
222
|
+
switch (_g.label) {
|
|
223
|
+
case 0:
|
|
224
|
+
project = options.project;
|
|
225
|
+
if (!project) {
|
|
226
|
+
return [2 /*return*/, undefined];
|
|
227
|
+
}
|
|
228
|
+
fetchImpl = (_d = options.fetchImpl) !== null && _d !== void 0 ? _d : globalThis.fetch;
|
|
229
|
+
if (!fetchImpl) {
|
|
230
|
+
api_1.logger.warn("No fetch implementation available for Vertex model listing.");
|
|
231
|
+
return [2 /*return*/, undefined];
|
|
232
|
+
}
|
|
233
|
+
getToken = (_e = options.getAccessToken) !== null && _e !== void 0 ? _e : getDefaultAccessToken;
|
|
234
|
+
return [4 /*yield*/, getToken()];
|
|
235
|
+
case 1:
|
|
236
|
+
token = _g.sent();
|
|
237
|
+
if (!token) {
|
|
238
|
+
return [2 /*return*/, undefined];
|
|
239
|
+
}
|
|
240
|
+
location = resolveLocation(options.location);
|
|
241
|
+
baseUrl = "https://".concat(aiPlatformHost(location), "/v1beta1/publishers/google/models");
|
|
242
|
+
models = [];
|
|
243
|
+
pages = 0;
|
|
244
|
+
_g.label = 2;
|
|
245
|
+
case 2:
|
|
246
|
+
_g.trys.push([2, 8, , 9]);
|
|
247
|
+
_g.label = 3;
|
|
248
|
+
case 3:
|
|
249
|
+
url = new URL(baseUrl);
|
|
250
|
+
url.searchParams.set("pageSize", MODEL_LIST_PAGE_SIZE);
|
|
251
|
+
if (pageToken) {
|
|
252
|
+
url.searchParams.set("pageToken", pageToken);
|
|
253
|
+
}
|
|
254
|
+
return [4 /*yield*/, fetchImpl(url.toString(), {
|
|
255
|
+
headers: { Authorization: "Bearer ".concat(token) },
|
|
256
|
+
})];
|
|
257
|
+
case 4:
|
|
258
|
+
response = _g.sent();
|
|
259
|
+
if (!response.ok) {
|
|
260
|
+
api_1.logger.warn("Vertex model listing failed with status ".concat(response.status, "."));
|
|
261
|
+
return [2 /*return*/, undefined];
|
|
262
|
+
}
|
|
263
|
+
return [4 /*yield*/, response.json()];
|
|
264
|
+
case 5:
|
|
265
|
+
body = (_g.sent());
|
|
266
|
+
try {
|
|
267
|
+
for (_a = (e_1 = void 0, __values((_f = body.publisherModels) !== null && _f !== void 0 ? _f : [])), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
268
|
+
model = _b.value;
|
|
269
|
+
if (model.name) {
|
|
270
|
+
models.push((0, exports.normalizeVertexModelId)(model.name));
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
275
|
+
finally {
|
|
276
|
+
try {
|
|
277
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
278
|
+
}
|
|
279
|
+
finally { if (e_1) throw e_1.error; }
|
|
280
|
+
}
|
|
281
|
+
pageToken = body.nextPageToken;
|
|
282
|
+
pages += 1;
|
|
283
|
+
_g.label = 6;
|
|
284
|
+
case 6:
|
|
285
|
+
if (pageToken && pages < MAX_MODEL_LIST_PAGES) return [3 /*break*/, 3];
|
|
286
|
+
_g.label = 7;
|
|
287
|
+
case 7: return [3 /*break*/, 9];
|
|
288
|
+
case 8:
|
|
289
|
+
error_2 = _g.sent();
|
|
290
|
+
api_1.logger.warn("Vertex model listing errored: ".concat(error_2.message));
|
|
291
|
+
return [2 /*return*/, undefined];
|
|
292
|
+
case 9:
|
|
293
|
+
// More pages remained than we were willing to fetch. Returning the partial list would let
|
|
294
|
+
// verification falsely mark enabled models as unavailable, so treat it as inconclusive instead.
|
|
295
|
+
if (pageToken) {
|
|
296
|
+
api_1.logger.warn("Vertex model listing exceeded ".concat(MAX_MODEL_LIST_PAGES, " pages; treating verification as inconclusive."));
|
|
297
|
+
return [2 /*return*/, undefined];
|
|
298
|
+
}
|
|
299
|
+
return [2 /*return*/, models];
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}); };
|
|
303
|
+
exports.listEnabledVertexModels = listEnabledVertexModels;
|
|
304
|
+
/**
|
|
305
|
+
* Verify that the requested models are enabled/available for the project using the Google APIs.
|
|
306
|
+
* When the listing cannot be retrieved (e.g. no credentials), `checked` is false and verification
|
|
307
|
+
* is treated as inconclusive rather than failing.
|
|
308
|
+
*/
|
|
309
|
+
var verifyVertexModelsEnabled = function (options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
310
|
+
var listModelsFn, models, listOptions, requested, list, enabled, enabledSet, available, unavailable, requested_1, requested_1_1, model;
|
|
311
|
+
var e_2, _a;
|
|
312
|
+
return __generator(this, function (_b) {
|
|
313
|
+
switch (_b.label) {
|
|
314
|
+
case 0:
|
|
315
|
+
listModelsFn = options.listModelsFn, models = options.models, listOptions = __rest(options, ["listModelsFn", "models"]);
|
|
316
|
+
requested = __spreadArray([], __read(new Set(models.map(function (model) { return (0, exports.normalizeVertexModelId)(model); }))), false);
|
|
317
|
+
list = listModelsFn !== null && listModelsFn !== void 0 ? listModelsFn : exports.listEnabledVertexModels;
|
|
318
|
+
return [4 /*yield*/, list(listOptions)];
|
|
319
|
+
case 1:
|
|
320
|
+
enabled = _b.sent();
|
|
321
|
+
if (!enabled) {
|
|
322
|
+
return [2 /*return*/, { available: requested, checked: false, unavailable: [] }];
|
|
323
|
+
}
|
|
324
|
+
enabledSet = new Set(enabled.map(function (model) { return (0, exports.normalizeVertexModelId)(model); }));
|
|
325
|
+
available = [];
|
|
326
|
+
unavailable = [];
|
|
327
|
+
try {
|
|
328
|
+
for (requested_1 = __values(requested), requested_1_1 = requested_1.next(); !requested_1_1.done; requested_1_1 = requested_1.next()) {
|
|
329
|
+
model = requested_1_1.value;
|
|
330
|
+
if (enabledSet.has(model)) {
|
|
331
|
+
available.push(model);
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
unavailable.push(model);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
339
|
+
finally {
|
|
340
|
+
try {
|
|
341
|
+
if (requested_1_1 && !requested_1_1.done && (_a = requested_1.return)) _a.call(requested_1);
|
|
342
|
+
}
|
|
343
|
+
finally { if (e_2) throw e_2.error; }
|
|
344
|
+
}
|
|
345
|
+
return [2 /*return*/, { available: available, checked: true, unavailable: unavailable }];
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
}); };
|
|
349
|
+
exports.verifyVertexModelsEnabled = verifyVertexModelsEnabled;
|
|
350
|
+
/**
|
|
351
|
+
* Like `verifyVertexModelsEnabled`, but throws an `APIError` when the listing was retrieved and one
|
|
352
|
+
* or more requested models are not enabled/available. Inconclusive checks (no credentials/network)
|
|
353
|
+
* do not throw so local development and offline environments keep working.
|
|
354
|
+
*/
|
|
355
|
+
var assertVertexModelsEnabled = function (options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
356
|
+
var result;
|
|
357
|
+
return __generator(this, function (_a) {
|
|
358
|
+
switch (_a.label) {
|
|
359
|
+
case 0: return [4 /*yield*/, (0, exports.verifyVertexModelsEnabled)(options)];
|
|
360
|
+
case 1:
|
|
361
|
+
result = _a.sent();
|
|
362
|
+
if (result.checked && result.unavailable.length > 0) {
|
|
363
|
+
throw new api_1.APIError({
|
|
364
|
+
detail: "The following models are not enabled/available on the Gemini Enterprise Agent Platform (Vertex AI) for this project: ".concat(result.unavailable.join(", "), "."),
|
|
365
|
+
status: 400,
|
|
366
|
+
title: "Vertex models unavailable",
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
return [2 /*return*/, result];
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
}); };
|
|
373
|
+
exports.assertVertexModelsEnabled = assertVertexModelsEnabled;
|
package/package.json
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"@opentelemetry/api": "^1.9.0",
|
|
12
12
|
"@opentelemetry/sdk-node": "0.213.0",
|
|
13
13
|
"ai": "^6.0.97",
|
|
14
|
+
"google-auth-library": "^10.5.0",
|
|
14
15
|
"luxon": "^3.7.2",
|
|
15
16
|
"multer": "^1.4.5-lts.1"
|
|
16
17
|
},
|
|
@@ -23,7 +24,7 @@
|
|
|
23
24
|
"@types/multer": "^1.4.12",
|
|
24
25
|
"@types/passport-local-mongoose": "^6.1.5",
|
|
25
26
|
"@types/supertest": "^6.0.2",
|
|
26
|
-
"@terreno/api": "0.
|
|
27
|
+
"@terreno/api": "0.18.0",
|
|
27
28
|
"mongoose": "8.23.0",
|
|
28
29
|
"passport-local-mongoose": "^9.0.1",
|
|
29
30
|
"supertest": "^7.0.0",
|
|
@@ -64,7 +65,7 @@
|
|
|
64
65
|
"access": "public"
|
|
65
66
|
},
|
|
66
67
|
"peerDependencies": {
|
|
67
|
-
"@terreno/api": "0.
|
|
68
|
+
"@terreno/api": "0.18.0",
|
|
68
69
|
"mongoose": "^8.0.0"
|
|
69
70
|
},
|
|
70
71
|
"repository": {
|
|
@@ -83,5 +84,5 @@
|
|
|
83
84
|
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
84
85
|
},
|
|
85
86
|
"types": "dist/index.d.ts",
|
|
86
|
-
"version": "0.
|
|
87
|
+
"version": "0.18.0"
|
|
87
88
|
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {listGeminiApiModels, normalizeGeminiModelId} from "./gemini";
|
|
4
|
+
|
|
5
|
+
const jsonResponse = (body: unknown, ok = true, status = 200): Response =>
|
|
6
|
+
({
|
|
7
|
+
json: async () => body,
|
|
8
|
+
ok,
|
|
9
|
+
status,
|
|
10
|
+
}) as unknown as Response;
|
|
11
|
+
|
|
12
|
+
describe("gemini model listing helpers", () => {
|
|
13
|
+
describe("normalizeGeminiModelId", () => {
|
|
14
|
+
it("strips the models/ prefix", () => {
|
|
15
|
+
expect(normalizeGeminiModelId("models/gemini-2.5-flash")).toBe("gemini-2.5-flash");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("leaves a bare id unchanged", () => {
|
|
19
|
+
expect(normalizeGeminiModelId("gemini-2.5-pro")).toBe("gemini-2.5-pro");
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe("listGeminiApiModels", () => {
|
|
24
|
+
it("returns undefined when no api key is provided", async () => {
|
|
25
|
+
const result = await listGeminiApiModels({apiKey: ""});
|
|
26
|
+
expect(result).toBeUndefined();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("returns normalized chat model ids and filters out non-generateContent models", async () => {
|
|
30
|
+
const fetchImpl = mock(async () =>
|
|
31
|
+
jsonResponse({
|
|
32
|
+
models: [
|
|
33
|
+
{name: "models/gemini-2.5-pro", supportedGenerationMethods: ["generateContent"]},
|
|
34
|
+
{name: "models/gemini-2.5-flash", supportedGenerationMethods: ["generateContent"]},
|
|
35
|
+
{name: "models/text-embedding-004", supportedGenerationMethods: ["embedContent"]},
|
|
36
|
+
],
|
|
37
|
+
})
|
|
38
|
+
) as unknown as typeof fetch;
|
|
39
|
+
|
|
40
|
+
const result = await listGeminiApiModels({apiKey: "key", fetchImpl});
|
|
41
|
+
expect(result).toEqual(["gemini-2.5-pro", "gemini-2.5-flash"]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("returns every model when chatOnly is false", async () => {
|
|
45
|
+
const fetchImpl = mock(async () =>
|
|
46
|
+
jsonResponse({
|
|
47
|
+
models: [
|
|
48
|
+
{name: "models/gemini-2.5-flash", supportedGenerationMethods: ["generateContent"]},
|
|
49
|
+
{name: "models/text-embedding-004", supportedGenerationMethods: ["embedContent"]},
|
|
50
|
+
],
|
|
51
|
+
})
|
|
52
|
+
) as unknown as typeof fetch;
|
|
53
|
+
|
|
54
|
+
const result = await listGeminiApiModels({apiKey: "key", chatOnly: false, fetchImpl});
|
|
55
|
+
expect(result).toEqual(["gemini-2.5-flash", "text-embedding-004"]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("follows pagination via nextPageToken", async () => {
|
|
59
|
+
let call = 0;
|
|
60
|
+
const fetchImpl = mock(async () => {
|
|
61
|
+
call += 1;
|
|
62
|
+
if (call === 1) {
|
|
63
|
+
return jsonResponse({
|
|
64
|
+
models: [
|
|
65
|
+
{name: "models/gemini-2.5-pro", supportedGenerationMethods: ["generateContent"]},
|
|
66
|
+
],
|
|
67
|
+
nextPageToken: "page2",
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return jsonResponse({
|
|
71
|
+
models: [
|
|
72
|
+
{name: "models/gemini-2.5-flash", supportedGenerationMethods: ["generateContent"]},
|
|
73
|
+
],
|
|
74
|
+
});
|
|
75
|
+
}) as unknown as typeof fetch;
|
|
76
|
+
|
|
77
|
+
const result = await listGeminiApiModels({apiKey: "key", fetchImpl});
|
|
78
|
+
expect(result).toEqual(["gemini-2.5-pro", "gemini-2.5-flash"]);
|
|
79
|
+
expect(call).toBe(2);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("returns undefined when the request fails", async () => {
|
|
83
|
+
const fetchImpl = mock(async () => jsonResponse({}, false, 503)) as unknown as typeof fetch;
|
|
84
|
+
const result = await listGeminiApiModels({apiKey: "key", fetchImpl});
|
|
85
|
+
expect(result).toBeUndefined();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("returns undefined when fetch throws", async () => {
|
|
89
|
+
const fetchImpl = mock(async () => {
|
|
90
|
+
throw new Error("network down");
|
|
91
|
+
}) as unknown as typeof fetch;
|
|
92
|
+
const result = await listGeminiApiModels({apiKey: "key", fetchImpl});
|
|
93
|
+
expect(result).toBeUndefined();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {logger} from "@terreno/api";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Helper for listing models available to the Gemini Developer API (the API-key based
|
|
5
|
+
* `generativelanguage.googleapis.com` service, distinct from Vertex / the Gemini Enterprise Agent
|
|
6
|
+
* Platform). Used to drive model pickers from the live set of models Google actually exposes for a
|
|
7
|
+
* given API key, so retired models (e.g. an old `gemini-2.0-flash`) never appear.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Default Gemini Developer API base URL. */
|
|
11
|
+
export const GEMINI_API_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
12
|
+
|
|
13
|
+
const MAX_MODEL_LIST_PAGES = 10;
|
|
14
|
+
const MODEL_LIST_PAGE_SIZE = "200";
|
|
15
|
+
const GENERATE_CONTENT_METHOD = "generateContent";
|
|
16
|
+
|
|
17
|
+
export interface ListGeminiApiModelsOptions {
|
|
18
|
+
/** Gemini Developer API key. */
|
|
19
|
+
apiKey: string;
|
|
20
|
+
/** Override the API base URL (primarily for testing). Defaults to `GEMINI_API_BASE_URL`. */
|
|
21
|
+
baseUrl?: string;
|
|
22
|
+
/**
|
|
23
|
+
* When true (the default), only models that support `generateContent` (i.e. chat-capable models)
|
|
24
|
+
* are returned. Set to false to return every listed model id.
|
|
25
|
+
*/
|
|
26
|
+
chatOnly?: boolean;
|
|
27
|
+
/** Injectable fetch implementation (defaults to the global `fetch`). */
|
|
28
|
+
fetchImpl?: typeof fetch;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface GeminiApiModel {
|
|
32
|
+
name?: string;
|
|
33
|
+
supportedGenerationMethods?: string[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Strip the "models/" resource prefix from a Gemini model name (e.g. "models/gemini-2.5-flash"). */
|
|
37
|
+
export const normalizeGeminiModelId = (name: string): string => {
|
|
38
|
+
return name.trim().replace(/^models\//, "");
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* List the models available to a Gemini Developer API key via the `models` REST endpoint. Returns
|
|
43
|
+
* normalized model ids (e.g. "gemini-2.5-flash"). By default only chat-capable models (those
|
|
44
|
+
* supporting `generateContent`) are returned. Returns `undefined` when the listing could not be
|
|
45
|
+
* retrieved (missing key, network error, non-200 response, etc.).
|
|
46
|
+
*/
|
|
47
|
+
export const listGeminiApiModels = async (
|
|
48
|
+
options: ListGeminiApiModelsOptions
|
|
49
|
+
): Promise<string[] | undefined> => {
|
|
50
|
+
const {apiKey} = options;
|
|
51
|
+
if (!apiKey) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
56
|
+
if (!fetchImpl) {
|
|
57
|
+
logger.warn("No fetch implementation available for Gemini model listing.");
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const chatOnly = options.chatOnly ?? true;
|
|
62
|
+
const baseUrl = options.baseUrl ?? GEMINI_API_BASE_URL;
|
|
63
|
+
const models: string[] = [];
|
|
64
|
+
let pageToken: string | undefined;
|
|
65
|
+
let pages = 0;
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
do {
|
|
69
|
+
const url = new URL(`${baseUrl}/models`);
|
|
70
|
+
url.searchParams.set("key", apiKey);
|
|
71
|
+
url.searchParams.set("pageSize", MODEL_LIST_PAGE_SIZE);
|
|
72
|
+
if (pageToken) {
|
|
73
|
+
url.searchParams.set("pageToken", pageToken);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const response = await fetchImpl(url.toString());
|
|
77
|
+
if (!response.ok) {
|
|
78
|
+
logger.warn(`Gemini model listing failed with status ${response.status}.`);
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const body = (await response.json()) as {
|
|
83
|
+
models?: GeminiApiModel[];
|
|
84
|
+
nextPageToken?: string;
|
|
85
|
+
};
|
|
86
|
+
for (const model of body.models ?? []) {
|
|
87
|
+
if (!model.name) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (
|
|
91
|
+
chatOnly &&
|
|
92
|
+
!(model.supportedGenerationMethods ?? []).includes(GENERATE_CONTENT_METHOD)
|
|
93
|
+
) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
models.push(normalizeGeminiModelId(model.name));
|
|
97
|
+
}
|
|
98
|
+
pageToken = body.nextPageToken;
|
|
99
|
+
pages += 1;
|
|
100
|
+
} while (pageToken && pages < MAX_MODEL_LIST_PAGES);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
logger.warn(`Gemini model listing errored: ${(error as Error).message}`);
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (pageToken) {
|
|
107
|
+
logger.warn(
|
|
108
|
+
`Gemini model listing exceeded ${MAX_MODEL_LIST_PAGES} pages; returning partial list.`
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return models;
|
|
113
|
+
};
|
package/src/service/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export {AIService, TemperaturePresets} from "./aiService";
|
|
2
2
|
export {FileStorageService} from "./fileStorage";
|
|
3
|
+
export type {ListGeminiApiModelsOptions} from "./gemini";
|
|
4
|
+
export {GEMINI_API_BASE_URL, listGeminiApiModels, normalizeGeminiModelId} from "./gemini";
|
|
3
5
|
export {MCPService} from "./mcpService";
|
|
4
6
|
export {
|
|
5
7
|
CONTENT_SUMMARY_PROMPT,
|
|
@@ -8,4 +10,21 @@ export {
|
|
|
8
10
|
TITLE_GENERATION_PROMPT,
|
|
9
11
|
TRANSLATION_PROMPT,
|
|
10
12
|
} from "./prompts";
|
|
13
|
+
export type {
|
|
14
|
+
CreateVertexProviderOptions,
|
|
15
|
+
ListEnabledVertexModelsOptions,
|
|
16
|
+
TerrenoVertexProvider,
|
|
17
|
+
VerifyVertexModelsOptions,
|
|
18
|
+
VertexLanguageModelProvider,
|
|
19
|
+
VertexModelAvailability,
|
|
20
|
+
} from "./vertex";
|
|
21
|
+
export {
|
|
22
|
+
assertVertexModelsEnabled,
|
|
23
|
+
createVertexProvider,
|
|
24
|
+
DEFAULT_VERTEX_LOCATION,
|
|
25
|
+
isVertexModelAllowed,
|
|
26
|
+
listEnabledVertexModels,
|
|
27
|
+
normalizeVertexModelId,
|
|
28
|
+
verifyVertexModelsEnabled,
|
|
29
|
+
} from "./vertex";
|
|
11
30
|
export type {WebSearchProvider, WebSearchResult} from "./webSearchTool";
|