@terreno/ai 0.18.0 → 0.20.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/aiService.d.ts +17 -2
- package/dist/service/aiService.js +326 -8
- package/dist/service/index.d.ts +5 -1
- package/dist/service/index.js +8 -1
- package/dist/service/parseAiJson.d.ts +33 -0
- package/dist/service/parseAiJson.js +180 -0
- package/dist/service/prompts.d.ts +2 -0
- package/dist/service/prompts.js +4 -1
- package/dist/types/index.d.ts +36 -1
- package/dist/types/index.js +3 -0
- package/package.json +3 -3
- package/src/service/aiService.test.ts +142 -1
- package/src/service/aiService.ts +320 -2
- package/src/service/index.ts +5 -0
- package/src/service/parseAiJson.test.ts +76 -0
- package/src/service/parseAiJson.ts +214 -0
- package/src/service/prompts.ts +5 -0
- package/src/service/vertex.test.ts +69 -0
- package/src/types/index.ts +41 -0
|
@@ -188,6 +188,15 @@ describe("vertex helpers", () => {
|
|
|
188
188
|
});
|
|
189
189
|
});
|
|
190
190
|
|
|
191
|
+
describe("createVertexProvider without vertexFactory", () => {
|
|
192
|
+
it("returns undefined when @ai-sdk/google-vertex is not installed (no factory)", () => {
|
|
193
|
+
// When no vertexFactory is passed, loadVertexModule is called.
|
|
194
|
+
// Since @ai-sdk/google-vertex is likely not installed in the test env, it returns undefined.
|
|
195
|
+
const provider = createVertexProvider({project: "demo-project"});
|
|
196
|
+
expect(provider).toBeUndefined();
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
191
200
|
describe("listEnabledVertexModels", () => {
|
|
192
201
|
it("paginates and normalizes publisher model names", async () => {
|
|
193
202
|
const fetchImpl = mock(async (input: string | URL) => {
|
|
@@ -256,5 +265,65 @@ describe("vertex helpers", () => {
|
|
|
256
265
|
});
|
|
257
266
|
expect(models).toBeUndefined();
|
|
258
267
|
});
|
|
268
|
+
|
|
269
|
+
it("returns undefined when project is not provided", async () => {
|
|
270
|
+
const models = await listEnabledVertexModels({
|
|
271
|
+
project: "",
|
|
272
|
+
});
|
|
273
|
+
expect(models).toBeUndefined();
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it("returns undefined when default access token acquisition fails (no credentials)", async () => {
|
|
277
|
+
// Mock google-auth-library to simulate credential failure deterministically,
|
|
278
|
+
// regardless of whether ADC is configured in the environment.
|
|
279
|
+
mock.module("google-auth-library", () => ({
|
|
280
|
+
GoogleAuth: class {
|
|
281
|
+
async getClient(): Promise<never> {
|
|
282
|
+
throw new Error("No credentials configured");
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
}));
|
|
286
|
+
try {
|
|
287
|
+
const fetchImpl = mock(
|
|
288
|
+
async () => ({json: async () => ({}), ok: true}) as unknown as Response
|
|
289
|
+
);
|
|
290
|
+
const models = await listEnabledVertexModels({
|
|
291
|
+
fetchImpl: fetchImpl as unknown as typeof fetch,
|
|
292
|
+
project: "demo-project",
|
|
293
|
+
});
|
|
294
|
+
expect(models).toBeUndefined();
|
|
295
|
+
// fetchImpl should not have been called since token acquisition should fail first
|
|
296
|
+
expect(fetchImpl).not.toHaveBeenCalled();
|
|
297
|
+
} finally {
|
|
298
|
+
mock.module("google-auth-library", () => ({}));
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it("returns undefined when no fetch implementation is available", async () => {
|
|
303
|
+
const originalFetch = globalThis.fetch;
|
|
304
|
+
// Temporarily remove globalThis.fetch to test the fallback
|
|
305
|
+
(globalThis as {fetch?: typeof fetch}).fetch = undefined as unknown as typeof fetch;
|
|
306
|
+
try {
|
|
307
|
+
const models = await listEnabledVertexModels({
|
|
308
|
+
getAccessToken: async () => "fake-token",
|
|
309
|
+
project: "demo-project",
|
|
310
|
+
});
|
|
311
|
+
expect(models).toBeUndefined();
|
|
312
|
+
} finally {
|
|
313
|
+
globalThis.fetch = originalFetch;
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("returns undefined when fetch throws a network error", async () => {
|
|
318
|
+
const fetchImpl = mock(async () => {
|
|
319
|
+
throw new Error("Network failure");
|
|
320
|
+
});
|
|
321
|
+
const models = await listEnabledVertexModels({
|
|
322
|
+
fetchImpl: fetchImpl as unknown as typeof fetch,
|
|
323
|
+
getAccessToken: async () => "fake-token",
|
|
324
|
+
project: "demo-project",
|
|
325
|
+
});
|
|
326
|
+
expect(models).toBeUndefined();
|
|
327
|
+
});
|
|
259
328
|
});
|
|
260
329
|
});
|
package/src/types/index.ts
CHANGED
|
@@ -8,6 +8,9 @@ import type mongoose from "mongoose";
|
|
|
8
8
|
|
|
9
9
|
export const DEFAULT_AI_REQUEST_TYPES = [
|
|
10
10
|
"general",
|
|
11
|
+
"json_array",
|
|
12
|
+
"json_object",
|
|
13
|
+
"json_value",
|
|
11
14
|
"remix",
|
|
12
15
|
"summarization",
|
|
13
16
|
"translation",
|
|
@@ -205,6 +208,44 @@ export interface TranslateOptions {
|
|
|
205
208
|
userId?: mongoose.Types.ObjectId;
|
|
206
209
|
}
|
|
207
210
|
|
|
211
|
+
export interface GenerateJsonValueOptions {
|
|
212
|
+
maxOutputTokens?: number;
|
|
213
|
+
/** Optional name passed to the provider for structured-output guidance. */
|
|
214
|
+
outputName?: string;
|
|
215
|
+
/** Optional description passed to the provider for structured-output guidance. */
|
|
216
|
+
outputDescription?: string;
|
|
217
|
+
prompt: string;
|
|
218
|
+
systemPrompt?: string;
|
|
219
|
+
temperature?: number;
|
|
220
|
+
userId?: mongoose.Types.ObjectId;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export interface GenerateJsonObjectOptions<OBJECT> {
|
|
224
|
+
maxOutputTokens?: number;
|
|
225
|
+
prompt: string;
|
|
226
|
+
/** Zod schema, `jsonSchema(...)`, or other `FlexibleSchema` accepted by the AI SDK. */
|
|
227
|
+
schema: import("ai").FlexibleSchema<OBJECT>;
|
|
228
|
+
schemaDescription?: string;
|
|
229
|
+
schemaName?: string;
|
|
230
|
+
systemPrompt?: string;
|
|
231
|
+
temperature?: number;
|
|
232
|
+
userId?: mongoose.Types.ObjectId;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export interface GenerateJsonArrayOptions<ELEMENT> {
|
|
236
|
+
/** Schema for each array element. */
|
|
237
|
+
element: import("ai").FlexibleSchema<ELEMENT>;
|
|
238
|
+
/** Optional description for the array output (provider guidance). */
|
|
239
|
+
outputDescription?: string;
|
|
240
|
+
/** Optional name for the array output (provider guidance). */
|
|
241
|
+
outputName?: string;
|
|
242
|
+
maxOutputTokens?: number;
|
|
243
|
+
prompt: string;
|
|
244
|
+
systemPrompt?: string;
|
|
245
|
+
temperature?: number;
|
|
246
|
+
userId?: mongoose.Types.ObjectId;
|
|
247
|
+
}
|
|
248
|
+
|
|
208
249
|
// ============================================================
|
|
209
250
|
// Route Option Types
|
|
210
251
|
// ============================================================
|