@terreno/ai 0.9.2 → 0.9.3
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/aiApp.d.ts +3 -1
- package/dist/aiApp.js +2 -1
- package/dist/routes/gpt.js +27 -9
- package/dist/types/index.d.ts +2 -0
- package/package.json +6 -3
- package/src/aiApp.ts +5 -1
- package/src/langfuseRoutesMiddleware.test.ts +57 -0
- package/src/routes/gpt.ts +25 -8
- package/src/routes/projects.ts +1 -1
- package/src/types/index.ts +2 -0
package/dist/aiApp.d.ts
CHANGED
|
@@ -8,7 +8,9 @@ export interface AiAppOptions {
|
|
|
8
8
|
/** Pre-configured AIService instance. Optional when using per-request keys or demo mode. */
|
|
9
9
|
aiService?: AIService;
|
|
10
10
|
/** Factory function to create a LanguageModel from a per-request API key (sent via x-ai-api-key header). */
|
|
11
|
-
createModelFn?: (apiKey: string) => LanguageModel;
|
|
11
|
+
createModelFn?: (apiKey: string, modelId?: string) => LanguageModel;
|
|
12
|
+
/** Factory function to create a LanguageModel on the server side without a per-request key (e.g. Vertex AI with ADC). Returns undefined if no provider is configured. */
|
|
13
|
+
createServerModelFn?: (modelId?: string) => LanguageModel | undefined;
|
|
12
14
|
/** When true and no AI service is available, routes return canned demo responses instead of failing. */
|
|
13
15
|
demoMode?: boolean;
|
|
14
16
|
/** File storage service for handling file uploads to GCS. */
|
package/dist/aiApp.js
CHANGED
|
@@ -39,11 +39,12 @@ var AiApp = /** @class */ (function () {
|
|
|
39
39
|
}
|
|
40
40
|
AiApp.prototype.register = function (app) {
|
|
41
41
|
var router = app;
|
|
42
|
-
var _a = this.options, aiService = _a.aiService, createModelFn = _a.createModelFn, demoMode = _a.demoMode, fileStorageService = _a.fileStorageService, gcsBucket = _a.gcsBucket, maxSteps = _a.maxSteps, mcpService = _a.mcpService, openApiOptions = _a.openApiOptions, titleModelId = _a.titleModelId, toolChoice = _a.toolChoice, tools = _a.tools;
|
|
42
|
+
var _a = this.options, aiService = _a.aiService, createModelFn = _a.createModelFn, createServerModelFn = _a.createServerModelFn, demoMode = _a.demoMode, fileStorageService = _a.fileStorageService, gcsBucket = _a.gcsBucket, maxSteps = _a.maxSteps, mcpService = _a.mcpService, openApiOptions = _a.openApiOptions, titleModelId = _a.titleModelId, toolChoice = _a.toolChoice, tools = _a.tools;
|
|
43
43
|
(0, gptHistories_1.addGptHistoryRoutes)(router, { openApiOptions: openApiOptions });
|
|
44
44
|
(0, gpt_1.addGptRoutes)(router, {
|
|
45
45
|
aiService: aiService,
|
|
46
46
|
createModelFn: createModelFn,
|
|
47
|
+
createServerModelFn: createServerModelFn,
|
|
47
48
|
demoMode: demoMode,
|
|
48
49
|
maxSteps: maxSteps,
|
|
49
50
|
mcpService: mcpService,
|
package/dist/routes/gpt.js
CHANGED
|
@@ -113,29 +113,44 @@ var sendDemoResponse = function (res, historyId) {
|
|
|
113
113
|
};
|
|
114
114
|
/**
|
|
115
115
|
* Resolve the AIService for a request. Priority:
|
|
116
|
-
* 1. Per-request API key via `x-ai-api-key` header (creates a temporary AIService)
|
|
117
|
-
* 2.
|
|
118
|
-
* 3.
|
|
116
|
+
* 1. Per-request API key via `x-ai-api-key` header (creates a temporary AIService via createModelFn)
|
|
117
|
+
* 2. Specific model requested + server-side model factory (creates a temporary AIService via createServerModelFn)
|
|
118
|
+
* 3. Pre-configured aiService from route options
|
|
119
|
+
* 4. undefined (triggers demo mode response)
|
|
119
120
|
*/
|
|
120
121
|
var resolveAiService = function (req, options, modelId) {
|
|
121
122
|
var perRequestKey = req.headers["x-ai-api-key"];
|
|
122
123
|
if (perRequestKey && options.createModelFn) {
|
|
123
124
|
return new aiService_1.AIService({ model: options.createModelFn(perRequestKey, modelId) });
|
|
124
125
|
}
|
|
126
|
+
if (modelId && options.createServerModelFn) {
|
|
127
|
+
var serverModel = options.createServerModelFn(modelId);
|
|
128
|
+
if (serverModel) {
|
|
129
|
+
return new aiService_1.AIService({ model: serverModel });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
125
132
|
return options.aiService;
|
|
126
133
|
};
|
|
127
134
|
/** Generate a short title for a conversation using a cheap model call. */
|
|
128
135
|
var generateTitle = function (prompt, response, aiService, options, perRequestApiKey) { return __awaiter(void 0, void 0, void 0, function () {
|
|
129
|
-
var titleService, conversationSnippet, title, _a;
|
|
136
|
+
var titleService, titleModel, conversationSnippet, title, _a;
|
|
130
137
|
return __generator(this, function (_b) {
|
|
131
138
|
switch (_b.label) {
|
|
132
139
|
case 0:
|
|
133
140
|
_b.trys.push([0, 2, , 3]);
|
|
134
141
|
titleService = aiService;
|
|
135
|
-
if (options.titleModelId
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
142
|
+
if (options.titleModelId) {
|
|
143
|
+
if (options.createModelFn && perRequestApiKey) {
|
|
144
|
+
titleService = new aiService_1.AIService({
|
|
145
|
+
model: options.createModelFn(perRequestApiKey, options.titleModelId),
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
else if (options.createServerModelFn) {
|
|
149
|
+
titleModel = options.createServerModelFn(options.titleModelId);
|
|
150
|
+
if (titleModel) {
|
|
151
|
+
titleService = new aiService_1.AIService({ model: titleModel });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
139
154
|
}
|
|
140
155
|
conversationSnippet = "User: ".concat(prompt, "\nAssistant: ").concat(response.substring(0, 500));
|
|
141
156
|
return [4 /*yield*/, titleService.generateText({
|
|
@@ -379,7 +394,10 @@ var addGptRoutes = function (router, options) {
|
|
|
379
394
|
messages: messages,
|
|
380
395
|
model: aiService.model,
|
|
381
396
|
providerOptions: !supportsTools
|
|
382
|
-
? {
|
|
397
|
+
? {
|
|
398
|
+
google: { responseModalities: ["TEXT", "IMAGE"] },
|
|
399
|
+
vertex: { responseModalities: ["TEXT", "IMAGE"] },
|
|
400
|
+
}
|
|
383
401
|
: undefined,
|
|
384
402
|
stopWhen: allTools ? (0, ai_1.stepCountIs)(maxSteps !== null && maxSteps !== void 0 ? maxSteps : 5) : (0, ai_1.stepCountIs)(1),
|
|
385
403
|
system: effectiveSystemPrompt !== null && effectiveSystemPrompt !== void 0 ? effectiveSystemPrompt : undefined,
|
package/dist/types/index.d.ts
CHANGED
|
@@ -156,6 +156,8 @@ export interface GptRouteOptions {
|
|
|
156
156
|
aiService?: import("../service/aiService").AIService;
|
|
157
157
|
/** Factory to create a LanguageModel from a per-request API key (x-ai-api-key header). */
|
|
158
158
|
createModelFn?: (apiKey: string, modelId?: string) => import("ai").LanguageModel;
|
|
159
|
+
/** Factory to create a LanguageModel on the server side without a per-request key (e.g. Vertex AI with ADC). Used for model switching when no x-ai-api-key header is present. Returns undefined if no provider is configured (falls through to demo mode). */
|
|
160
|
+
createServerModelFn?: (modelId?: string) => import("ai").LanguageModel | undefined;
|
|
159
161
|
/** Factory to create per-request tools (e.g. tools that need the request's API key). Merged with static tools. */
|
|
160
162
|
createRequestTools?: (req: import("express").Request) => Record<string, import("ai").Tool>;
|
|
161
163
|
/** Return canned responses when no AI service is available. */
|
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"@langfuse/otel": "^5.0.1",
|
|
11
11
|
"@opentelemetry/api": "^1.9.0",
|
|
12
12
|
"@opentelemetry/sdk-node": "^0.213.0",
|
|
13
|
-
"@terreno/api": "0.9.
|
|
13
|
+
"@terreno/api": "0.9.3",
|
|
14
14
|
"ai": "^6.0.97",
|
|
15
15
|
"multer": "^1.4.5-lts.1"
|
|
16
16
|
},
|
|
@@ -44,8 +44,11 @@
|
|
|
44
44
|
"license": "Apache-2.0",
|
|
45
45
|
"main": "dist/index.js",
|
|
46
46
|
"name": "@terreno/ai",
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
47
50
|
"peerDependencies": {
|
|
48
|
-
"@terreno/api": "0.9.
|
|
51
|
+
"@terreno/api": "0.9.3",
|
|
49
52
|
"mongoose": "^8.0.0"
|
|
50
53
|
},
|
|
51
54
|
"repository": {
|
|
@@ -63,5 +66,5 @@
|
|
|
63
66
|
"test:ci": "bun test --preload ./src/tests/bunSetup.ts"
|
|
64
67
|
},
|
|
65
68
|
"types": "dist/index.d.ts",
|
|
66
|
-
"version": "0.9.
|
|
69
|
+
"version": "0.9.3"
|
|
67
70
|
}
|
package/src/aiApp.ts
CHANGED
|
@@ -16,7 +16,9 @@ export interface AiAppOptions {
|
|
|
16
16
|
/** Pre-configured AIService instance. Optional when using per-request keys or demo mode. */
|
|
17
17
|
aiService?: AIService;
|
|
18
18
|
/** Factory function to create a LanguageModel from a per-request API key (sent via x-ai-api-key header). */
|
|
19
|
-
createModelFn?: (apiKey: string) => LanguageModel;
|
|
19
|
+
createModelFn?: (apiKey: string, modelId?: string) => LanguageModel;
|
|
20
|
+
/** Factory function to create a LanguageModel on the server side without a per-request key (e.g. Vertex AI with ADC). Returns undefined if no provider is configured. */
|
|
21
|
+
createServerModelFn?: (modelId?: string) => LanguageModel | undefined;
|
|
20
22
|
/** When true and no AI service is available, routes return canned demo responses instead of failing. */
|
|
21
23
|
demoMode?: boolean;
|
|
22
24
|
/** File storage service for handling file uploads to GCS. */
|
|
@@ -75,6 +77,7 @@ export class AiApp implements TerrenoPlugin {
|
|
|
75
77
|
const {
|
|
76
78
|
aiService,
|
|
77
79
|
createModelFn,
|
|
80
|
+
createServerModelFn,
|
|
78
81
|
demoMode,
|
|
79
82
|
fileStorageService,
|
|
80
83
|
gcsBucket,
|
|
@@ -90,6 +93,7 @@ export class AiApp implements TerrenoPlugin {
|
|
|
90
93
|
addGptRoutes(router, {
|
|
91
94
|
aiService,
|
|
92
95
|
createModelFn,
|
|
96
|
+
createServerModelFn,
|
|
93
97
|
demoMode,
|
|
94
98
|
maxSteps,
|
|
95
99
|
mcpService,
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import type {NextFunction, Request, Response} from "express";
|
|
3
|
+
|
|
4
|
+
import {requireAdmin} from "./langfuseRoutesMiddleware";
|
|
5
|
+
|
|
6
|
+
const buildRequest = (user: unknown): Request => {
|
|
7
|
+
return {user} as Request;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const buildResponse = (): Response => {
|
|
11
|
+
return {} as Response;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
describe("requireAdmin", () => {
|
|
15
|
+
it("calls next when user.isAdmin returns true", () => {
|
|
16
|
+
const req = buildRequest({
|
|
17
|
+
admin: false,
|
|
18
|
+
isAdmin: () => true,
|
|
19
|
+
});
|
|
20
|
+
const next = mock(() => {}) as NextFunction;
|
|
21
|
+
|
|
22
|
+
requireAdmin(req, buildResponse(), next);
|
|
23
|
+
|
|
24
|
+
expect(next).toHaveBeenCalledTimes(1);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("uses Permissions.IsAdmin fallback when isAdmin is missing", () => {
|
|
28
|
+
const req = buildRequest({admin: true});
|
|
29
|
+
const next = mock(() => {}) as NextFunction;
|
|
30
|
+
|
|
31
|
+
requireAdmin(req, buildResponse(), next);
|
|
32
|
+
|
|
33
|
+
expect(next).toHaveBeenCalledTimes(1);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("throws APIError with 403 when user is not admin", () => {
|
|
37
|
+
const req = buildRequest({admin: false});
|
|
38
|
+
const next = mock(() => {}) as NextFunction;
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
requireAdmin(req, buildResponse(), next);
|
|
42
|
+
throw new Error("Expected requireAdmin to throw");
|
|
43
|
+
} catch (error) {
|
|
44
|
+
const apiError = error as {
|
|
45
|
+
disableExternalErrorTracking?: boolean;
|
|
46
|
+
name?: string;
|
|
47
|
+
status?: number;
|
|
48
|
+
title?: string;
|
|
49
|
+
};
|
|
50
|
+
expect(apiError.name).toBe("APIError");
|
|
51
|
+
expect(apiError.status).toBe(403);
|
|
52
|
+
expect(apiError.title).toBe("Forbidden");
|
|
53
|
+
expect(apiError.disableExternalErrorTracking).toBe(true);
|
|
54
|
+
expect(next).toHaveBeenCalledTimes(0);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
});
|
package/src/routes/gpt.ts
CHANGED
|
@@ -33,9 +33,10 @@ const sendDemoResponse = (res: express.Response, historyId?: string): void => {
|
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
35
|
* Resolve the AIService for a request. Priority:
|
|
36
|
-
* 1. Per-request API key via `x-ai-api-key` header (creates a temporary AIService)
|
|
37
|
-
* 2.
|
|
38
|
-
* 3.
|
|
36
|
+
* 1. Per-request API key via `x-ai-api-key` header (creates a temporary AIService via createModelFn)
|
|
37
|
+
* 2. Specific model requested + server-side model factory (creates a temporary AIService via createServerModelFn)
|
|
38
|
+
* 3. Pre-configured aiService from route options
|
|
39
|
+
* 4. undefined (triggers demo mode response)
|
|
39
40
|
*/
|
|
40
41
|
const resolveAiService = (
|
|
41
42
|
req: express.Request,
|
|
@@ -46,6 +47,12 @@ const resolveAiService = (
|
|
|
46
47
|
if (perRequestKey && options.createModelFn) {
|
|
47
48
|
return new AIService({model: options.createModelFn(perRequestKey, modelId)});
|
|
48
49
|
}
|
|
50
|
+
if (modelId && options.createServerModelFn) {
|
|
51
|
+
const serverModel = options.createServerModelFn(modelId);
|
|
52
|
+
if (serverModel) {
|
|
53
|
+
return new AIService({model: serverModel});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
49
56
|
return options.aiService;
|
|
50
57
|
};
|
|
51
58
|
|
|
@@ -59,10 +66,17 @@ const generateTitle = async (
|
|
|
59
66
|
): Promise<string | undefined> => {
|
|
60
67
|
try {
|
|
61
68
|
let titleService = aiService;
|
|
62
|
-
if (options.titleModelId
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
69
|
+
if (options.titleModelId) {
|
|
70
|
+
if (options.createModelFn && perRequestApiKey) {
|
|
71
|
+
titleService = new AIService({
|
|
72
|
+
model: options.createModelFn(perRequestApiKey, options.titleModelId),
|
|
73
|
+
});
|
|
74
|
+
} else if (options.createServerModelFn) {
|
|
75
|
+
const titleModel = options.createServerModelFn(options.titleModelId);
|
|
76
|
+
if (titleModel) {
|
|
77
|
+
titleService = new AIService({model: titleModel});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
66
80
|
}
|
|
67
81
|
const conversationSnippet = `User: ${prompt}\nAssistant: ${response.substring(0, 500)}`;
|
|
68
82
|
const title = await titleService.generateText({
|
|
@@ -299,7 +313,10 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
299
313
|
messages,
|
|
300
314
|
model: (aiService as any).model,
|
|
301
315
|
providerOptions: !supportsTools
|
|
302
|
-
? {
|
|
316
|
+
? {
|
|
317
|
+
google: {responseModalities: ["TEXT", "IMAGE"]},
|
|
318
|
+
vertex: {responseModalities: ["TEXT", "IMAGE"]},
|
|
319
|
+
}
|
|
303
320
|
: undefined,
|
|
304
321
|
stopWhen: allTools ? stepCountIs(maxSteps ?? 5) : stepCountIs(1),
|
|
305
322
|
system: effectiveSystemPrompt ?? undefined,
|
package/src/routes/projects.ts
CHANGED
|
@@ -27,7 +27,7 @@ export const addProjectRoutes = (
|
|
|
27
27
|
read: [Permissions.IsOwner],
|
|
28
28
|
update: [Permissions.IsOwner],
|
|
29
29
|
},
|
|
30
|
-
preCreate: (body
|
|
30
|
+
preCreate: (body, req: express.Request) =>
|
|
31
31
|
({
|
|
32
32
|
...body,
|
|
33
33
|
userId: (req as any).user?._id,
|
package/src/types/index.ts
CHANGED
|
@@ -212,6 +212,8 @@ export interface GptRouteOptions {
|
|
|
212
212
|
aiService?: import("../service/aiService").AIService;
|
|
213
213
|
/** Factory to create a LanguageModel from a per-request API key (x-ai-api-key header). */
|
|
214
214
|
createModelFn?: (apiKey: string, modelId?: string) => import("ai").LanguageModel;
|
|
215
|
+
/** Factory to create a LanguageModel on the server side without a per-request key (e.g. Vertex AI with ADC). Used for model switching when no x-ai-api-key header is present. Returns undefined if no provider is configured (falls through to demo mode). */
|
|
216
|
+
createServerModelFn?: (modelId?: string) => import("ai").LanguageModel | undefined;
|
|
215
217
|
/** Factory to create per-request tools (e.g. tools that need the request's API key). Merged with static tools. */
|
|
216
218
|
createRequestTools?: (req: import("express").Request) => Record<string, import("ai").Tool>;
|
|
217
219
|
/** Return canned responses when no AI service is available. */
|