@terreno/ai 0.9.2 → 0.10.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/bunfig.unit.toml +2 -0
- 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/langfuseCache.test.ts +46 -40
- package/src/langfusePrompts.test.ts +176 -66
- package/src/langfuseRoutesMiddleware.test.ts +57 -0
- package/src/routes/gpt.ts +25 -8
- package/src/routes/projects.ts +1 -1
- package/src/tests/bunSetup.ts +16 -10
- package/src/types/index.ts +2 -0
package/bunfig.unit.toml
ADDED
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.
|
|
13
|
+
"@terreno/api": "0.10.0",
|
|
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.
|
|
51
|
+
"@terreno/api": "0.10.0",
|
|
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.
|
|
69
|
+
"version": "0.10.0"
|
|
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,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {afterEach,
|
|
1
|
+
import {afterEach, describe, expect, it, mock} from "bun:test";
|
|
2
2
|
|
|
3
3
|
import {getCached, invalidateCache, LangfuseCache, setCached} from "./langfuseCache";
|
|
4
4
|
import type {LangfuseCachedPrompt} from "./langfuseTypes";
|
|
@@ -13,71 +13,77 @@ const samplePrompt: LangfuseCachedPrompt = {
|
|
|
13
13
|
version: 1,
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
});
|
|
16
|
+
const originalFindOne = LangfuseCache.findOne;
|
|
17
|
+
const originalFindOneAndUpdate = LangfuseCache.findOneAndUpdate;
|
|
18
|
+
const originalDeleteMany = LangfuseCache.deleteMany;
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
describe("cache", () => {
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
LangfuseCache.findOne = originalFindOne;
|
|
23
|
+
LangfuseCache.findOneAndUpdate = originalFindOneAndUpdate;
|
|
24
|
+
LangfuseCache.deleteMany = originalDeleteMany;
|
|
23
25
|
});
|
|
24
26
|
|
|
25
27
|
describe("getCached", () => {
|
|
26
28
|
it("returns null when no entry exists", async () => {
|
|
29
|
+
const lean = mock(async () => null);
|
|
30
|
+
const findOne = mock(() => ({lean}));
|
|
31
|
+
LangfuseCache.findOne = findOne as unknown as typeof LangfuseCache.findOne;
|
|
32
|
+
|
|
27
33
|
const result = await getCached("prompt:missing:production");
|
|
28
|
-
expect(result).toBeNull();
|
|
29
|
-
});
|
|
30
34
|
|
|
31
|
-
it("returns null for expired entries", async () => {
|
|
32
|
-
await setCached("prompt:test:production", samplePrompt, -1);
|
|
33
|
-
const result = await getCached("prompt:test:production");
|
|
34
35
|
expect(result).toBeNull();
|
|
36
|
+
expect(findOne).toHaveBeenCalledTimes(1);
|
|
37
|
+
const [query] = (findOne as {mock: {calls: unknown[][]}}).mock.calls[0];
|
|
38
|
+
expect((query as {key: string}).key).toBe("prompt:missing:production");
|
|
39
|
+
expect((query as {expiresAt: {$gt: Date}}).expiresAt.$gt).toBeInstanceOf(Date);
|
|
35
40
|
});
|
|
36
41
|
|
|
37
|
-
it("returns
|
|
38
|
-
|
|
42
|
+
it("returns cached value when entry exists", async () => {
|
|
43
|
+
const lean = mock(async () => ({value: samplePrompt}));
|
|
44
|
+
const findOne = mock(() => ({lean}));
|
|
45
|
+
LangfuseCache.findOne = findOne as unknown as typeof LangfuseCache.findOne;
|
|
46
|
+
|
|
39
47
|
const result = await getCached("prompt:test:production");
|
|
40
|
-
|
|
41
|
-
expect(result
|
|
42
|
-
expect(
|
|
48
|
+
|
|
49
|
+
expect(result).toEqual(samplePrompt);
|
|
50
|
+
expect(lean).toHaveBeenCalledTimes(1);
|
|
43
51
|
});
|
|
44
52
|
});
|
|
45
53
|
|
|
46
54
|
describe("setCached", () => {
|
|
47
|
-
it("
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
});
|
|
55
|
+
it("upserts cache entries with computed expiry", async () => {
|
|
56
|
+
const findOneAndUpdate = mock(async () => ({}));
|
|
57
|
+
LangfuseCache.findOneAndUpdate =
|
|
58
|
+
findOneAndUpdate as unknown as typeof LangfuseCache.findOneAndUpdate;
|
|
52
59
|
|
|
53
|
-
|
|
60
|
+
const beforeCall = Date.now();
|
|
54
61
|
await setCached("prompt:test:production", samplePrompt, 60);
|
|
55
|
-
const
|
|
56
|
-
await setCached("prompt:test:production", updated, 60);
|
|
62
|
+
const afterCall = Date.now();
|
|
57
63
|
|
|
58
|
-
|
|
59
|
-
|
|
64
|
+
expect(findOneAndUpdate).toHaveBeenCalledTimes(1);
|
|
65
|
+
const [filter, update, options] = (findOneAndUpdate as {mock: {calls: unknown[][]}}).mock
|
|
66
|
+
.calls[0];
|
|
67
|
+
expect(filter).toEqual({key: "prompt:test:production"});
|
|
68
|
+
expect((update as {key: string}).key).toBe("prompt:test:production");
|
|
69
|
+
expect((update as {value: LangfuseCachedPrompt}).value).toEqual(samplePrompt);
|
|
70
|
+
expect(options).toEqual({upsert: true});
|
|
60
71
|
|
|
61
|
-
const
|
|
62
|
-
expect(
|
|
72
|
+
const expiresAt = (update as {expiresAt: Date}).expiresAt.getTime();
|
|
73
|
+
expect(expiresAt).toBeGreaterThanOrEqual(beforeCall + 59000);
|
|
74
|
+
expect(expiresAt).toBeLessThanOrEqual(afterCall + 61000);
|
|
63
75
|
});
|
|
64
76
|
});
|
|
65
77
|
|
|
66
78
|
describe("invalidateCache", () => {
|
|
67
|
-
it("
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
await setCached("prompt:other-prompt:production", {...samplePrompt, name: "other"}, 60);
|
|
79
|
+
it("passes regex key pattern to deleteMany", async () => {
|
|
80
|
+
const deleteMany = mock(async () => ({}));
|
|
81
|
+
LangfuseCache.deleteMany = deleteMany as unknown as typeof LangfuseCache.deleteMany;
|
|
71
82
|
|
|
72
83
|
await invalidateCache("prompt:test-prompt:");
|
|
73
84
|
|
|
74
|
-
|
|
75
|
-
expect(
|
|
76
|
-
expect(remaining[0].key).toBe("prompt:other-prompt:production");
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it("does not throw when no entries match", async () => {
|
|
80
|
-
await expect(invalidateCache("prompt:nonexistent:")).resolves.toBeUndefined();
|
|
85
|
+
expect(deleteMany).toHaveBeenCalledTimes(1);
|
|
86
|
+
expect(deleteMany).toHaveBeenCalledWith({key: {$regex: "prompt:test-prompt:"}});
|
|
81
87
|
});
|
|
82
88
|
});
|
|
83
89
|
});
|
|
@@ -1,8 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
2
|
|
|
3
|
-
import {LangfuseCache} from "./langfuseCache";
|
|
4
3
|
import type {LangfuseCachedPrompt} from "./langfuseTypes";
|
|
5
4
|
|
|
5
|
+
const cacheValues = new Map<string, LangfuseCachedPrompt>();
|
|
6
|
+
|
|
7
|
+
const mockLoggerDebug = mock(() => {});
|
|
8
|
+
const mockLoggerInfo = mock(() => {});
|
|
9
|
+
const mockGetCached = mock(async (key: string) => {
|
|
10
|
+
return cacheValues.get(key) ?? null;
|
|
11
|
+
});
|
|
12
|
+
const mockSetCached = mock(async (key: string, value: LangfuseCachedPrompt) => {
|
|
13
|
+
cacheValues.set(key, value);
|
|
14
|
+
});
|
|
15
|
+
const mockInvalidateCache = mock(async (prefix: string) => {
|
|
16
|
+
const keys = [...cacheValues.keys()];
|
|
17
|
+
for (const key of keys) {
|
|
18
|
+
if (key.startsWith(prefix)) {
|
|
19
|
+
cacheValues.delete(key);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
const mockPromptCreate = mock(async (_params: Record<string, unknown>) => {});
|
|
6
24
|
const mockPromptGet = mock(async (_name: string, _options?: Record<string, unknown>) => ({
|
|
7
25
|
config: {},
|
|
8
26
|
labels: ["production"],
|
|
@@ -13,11 +31,26 @@ const mockPromptGet = mock(async (_name: string, _options?: Record<string, unkno
|
|
|
13
31
|
version: 1,
|
|
14
32
|
}));
|
|
15
33
|
|
|
34
|
+
mock.module("@terreno/api", () => ({
|
|
35
|
+
logger: {
|
|
36
|
+
debug: mockLoggerDebug,
|
|
37
|
+
info: mockLoggerInfo,
|
|
38
|
+
},
|
|
39
|
+
}));
|
|
40
|
+
|
|
41
|
+
mock.module("./langfuseCache", () => ({
|
|
42
|
+
getCached: mockGetCached,
|
|
43
|
+
invalidateCache: mockInvalidateCache,
|
|
44
|
+
setCached: mockSetCached,
|
|
45
|
+
}));
|
|
46
|
+
|
|
16
47
|
mock.module("./langfuseClient", () => ({
|
|
17
|
-
getLangfuseClient: () => ({prompt: {get: mockPromptGet}}),
|
|
48
|
+
getLangfuseClient: () => ({prompt: {create: mockPromptCreate, get: mockPromptGet}}),
|
|
18
49
|
}));
|
|
19
50
|
|
|
20
|
-
const {compilePrompt, getPrompt} = await import(
|
|
51
|
+
const {compilePrompt, createPrompt, getPrompt, invalidatePromptCache} = await import(
|
|
52
|
+
"./langfusePrompts"
|
|
53
|
+
);
|
|
21
54
|
|
|
22
55
|
const textPrompt: LangfuseCachedPrompt = {
|
|
23
56
|
config: {},
|
|
@@ -42,84 +75,92 @@ const chatPrompt: LangfuseCachedPrompt = {
|
|
|
42
75
|
version: 1,
|
|
43
76
|
};
|
|
44
77
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
78
|
+
beforeEach(() => {
|
|
79
|
+
cacheValues.clear();
|
|
80
|
+
mockLoggerDebug.mockClear();
|
|
81
|
+
mockLoggerInfo.mockClear();
|
|
82
|
+
mockGetCached.mockClear();
|
|
83
|
+
mockSetCached.mockClear();
|
|
84
|
+
mockInvalidateCache.mockClear();
|
|
85
|
+
mockPromptCreate.mockClear();
|
|
86
|
+
mockPromptGet.mockClear();
|
|
87
|
+
mockPromptGet.mockImplementation(async () => ({
|
|
88
|
+
config: {},
|
|
89
|
+
labels: ["production"],
|
|
90
|
+
name: "test-prompt",
|
|
91
|
+
prompt: "Hello world",
|
|
92
|
+
tags: [],
|
|
93
|
+
type: "text" as const,
|
|
94
|
+
version: 1,
|
|
95
|
+
}));
|
|
96
|
+
});
|
|
51
97
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
98
|
+
describe("compilePrompt", () => {
|
|
99
|
+
it("replaces variables in text prompts", () => {
|
|
100
|
+
const result = compilePrompt(textPrompt, {age: "30", name: "Alice"});
|
|
101
|
+
expect(result).toBe("Hello Alice, you are 30 years old.");
|
|
102
|
+
});
|
|
56
103
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
prompt: "Hello world.",
|
|
61
|
-
};
|
|
62
|
-
const result = compilePrompt(noVarPrompt);
|
|
63
|
-
expect(result).toBe("Hello world.");
|
|
64
|
-
});
|
|
104
|
+
it("keeps missing text variables unreplaced", () => {
|
|
105
|
+
const result = compilePrompt(textPrompt, {name: "Alice"});
|
|
106
|
+
expect(result).toBe("Hello Alice, you are {{age}} years old.");
|
|
65
107
|
});
|
|
66
108
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
expect(messages[0].content).toBe("You are a helpful assistant for Acme.");
|
|
73
|
-
expect(messages[1].content).toBe("My name is Bob.");
|
|
74
|
-
});
|
|
109
|
+
it("replaces variables in chat prompts", () => {
|
|
110
|
+
const result = compilePrompt(chatPrompt, {company: "Acme", name: "Bob"}) as Array<{
|
|
111
|
+
content: string;
|
|
112
|
+
role: string;
|
|
113
|
+
}>;
|
|
75
114
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
role: string;
|
|
79
|
-
content: string;
|
|
80
|
-
}>;
|
|
81
|
-
expect(result[0].role).toBe("system");
|
|
82
|
-
expect(result[1].role).toBe("user");
|
|
83
|
-
});
|
|
115
|
+
expect(result[0]).toEqual({content: "You are a helpful assistant for Acme.", role: "system"});
|
|
116
|
+
expect(result[1]).toEqual({content: "My name is Bob.", role: "user"});
|
|
84
117
|
});
|
|
85
118
|
});
|
|
86
119
|
|
|
87
120
|
describe("getPrompt", () => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
mockPromptGet.mockClear();
|
|
91
|
-
mockPromptGet.mockImplementation(async (_name: string, _options?: Record<string, unknown>) => ({
|
|
92
|
-
config: {},
|
|
93
|
-
labels: ["production"],
|
|
94
|
-
name: "test-prompt",
|
|
95
|
-
prompt: "Hello world",
|
|
96
|
-
tags: [],
|
|
97
|
-
type: "text" as const,
|
|
98
|
-
version: 1,
|
|
99
|
-
}));
|
|
100
|
-
});
|
|
121
|
+
it("returns cached prompts without calling Langfuse", async () => {
|
|
122
|
+
cacheValues.set("prompt:test-prompt:production", textPrompt);
|
|
101
123
|
|
|
102
|
-
|
|
103
|
-
|
|
124
|
+
const result = await getPrompt("test-prompt");
|
|
125
|
+
|
|
126
|
+
expect(result).toEqual(textPrompt);
|
|
127
|
+
expect(mockPromptGet).toHaveBeenCalledTimes(0);
|
|
128
|
+
expect(mockLoggerDebug).toHaveBeenCalledWith(
|
|
129
|
+
'Langfuse prompt cache hit: "test-prompt" v1 (label: production)'
|
|
130
|
+
);
|
|
104
131
|
});
|
|
105
132
|
|
|
106
|
-
it("fetches
|
|
133
|
+
it("fetches and caches prompts with default ttl/label", async () => {
|
|
107
134
|
const result = await getPrompt("test-prompt");
|
|
108
|
-
expect(result.name).toBe("test-prompt");
|
|
109
|
-
expect(result.type).toBe("text");
|
|
110
|
-
expect(mockPromptGet).toHaveBeenCalledTimes(1);
|
|
111
|
-
});
|
|
112
135
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
136
|
+
expect(mockPromptGet).toHaveBeenCalledWith("test-prompt", {
|
|
137
|
+
cacheTtlSeconds: 0,
|
|
138
|
+
label: "production",
|
|
139
|
+
});
|
|
140
|
+
expect(mockSetCached).toHaveBeenCalledWith("prompt:test-prompt:production", result, 60);
|
|
141
|
+
expect(cacheValues.get("prompt:test-prompt:production")).toEqual(result);
|
|
117
142
|
});
|
|
118
143
|
|
|
119
|
-
it("
|
|
120
|
-
await getPrompt("test-prompt");
|
|
121
|
-
|
|
122
|
-
expect(
|
|
144
|
+
it("fetches and caches prompts with custom ttl/label", async () => {
|
|
145
|
+
await getPrompt("test-prompt", {label: "staging"}, {cache: {promptTtlSeconds: 120}});
|
|
146
|
+
|
|
147
|
+
expect(mockPromptGet).toHaveBeenCalledWith("test-prompt", {
|
|
148
|
+
cacheTtlSeconds: 0,
|
|
149
|
+
label: "staging",
|
|
150
|
+
});
|
|
151
|
+
expect(mockSetCached).toHaveBeenCalledWith(
|
|
152
|
+
"prompt:test-prompt:staging",
|
|
153
|
+
{
|
|
154
|
+
config: {},
|
|
155
|
+
labels: ["production"],
|
|
156
|
+
name: "test-prompt",
|
|
157
|
+
prompt: "Hello world",
|
|
158
|
+
tags: [],
|
|
159
|
+
type: "text",
|
|
160
|
+
version: 1,
|
|
161
|
+
},
|
|
162
|
+
120
|
|
163
|
+
);
|
|
123
164
|
});
|
|
124
165
|
|
|
125
166
|
it("throws when Langfuse fetch fails", async () => {
|
|
@@ -130,3 +171,72 @@ describe("getPrompt", () => {
|
|
|
130
171
|
await expect(getPrompt("missing-prompt")).rejects.toThrow("Prompt not found");
|
|
131
172
|
});
|
|
132
173
|
});
|
|
174
|
+
|
|
175
|
+
describe("createPrompt", () => {
|
|
176
|
+
it("creates text prompts with default labels/tags", async () => {
|
|
177
|
+
const result = await createPrompt({
|
|
178
|
+
name: "created-prompt",
|
|
179
|
+
prompt: "Text prompt body",
|
|
180
|
+
type: "text",
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
expect(mockPromptCreate).toHaveBeenCalledWith({
|
|
184
|
+
config: undefined,
|
|
185
|
+
labels: ["production"],
|
|
186
|
+
name: "created-prompt",
|
|
187
|
+
prompt: "Text prompt body",
|
|
188
|
+
tags: [],
|
|
189
|
+
type: "text",
|
|
190
|
+
});
|
|
191
|
+
expect(mockInvalidateCache).toHaveBeenCalledWith("prompt:created-prompt:");
|
|
192
|
+
expect(mockPromptGet).toHaveBeenCalledWith("created-prompt", {
|
|
193
|
+
cacheTtlSeconds: 0,
|
|
194
|
+
label: "production",
|
|
195
|
+
});
|
|
196
|
+
expect(result.name).toBe("test-prompt");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("creates chat prompts preserving role and content", async () => {
|
|
200
|
+
await createPrompt({
|
|
201
|
+
config: {temperature: 0.3},
|
|
202
|
+
labels: ["staging"],
|
|
203
|
+
name: "chat-prompt",
|
|
204
|
+
prompt: [
|
|
205
|
+
{content: "System says hello", role: "system"},
|
|
206
|
+
{content: "User asks question", role: "user"},
|
|
207
|
+
],
|
|
208
|
+
tags: ["support"],
|
|
209
|
+
type: "chat",
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
expect(mockPromptCreate).toHaveBeenCalledWith({
|
|
213
|
+
config: {temperature: 0.3},
|
|
214
|
+
labels: ["staging"],
|
|
215
|
+
name: "chat-prompt",
|
|
216
|
+
prompt: [
|
|
217
|
+
{content: "System says hello", role: "system"},
|
|
218
|
+
{content: "User asks question", role: "user"},
|
|
219
|
+
],
|
|
220
|
+
tags: ["support"],
|
|
221
|
+
type: "chat",
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe("invalidatePromptCache", () => {
|
|
227
|
+
it("invalidates cache entries for a prompt prefix", async () => {
|
|
228
|
+
cacheValues.set("prompt:target-prompt:production", textPrompt);
|
|
229
|
+
cacheValues.set("prompt:target-prompt:staging", textPrompt);
|
|
230
|
+
cacheValues.set("prompt:other-prompt:production", textPrompt);
|
|
231
|
+
|
|
232
|
+
await invalidatePromptCache("target-prompt");
|
|
233
|
+
|
|
234
|
+
expect(mockInvalidateCache).toHaveBeenCalledWith("prompt:target-prompt:");
|
|
235
|
+
expect(cacheValues.has("prompt:target-prompt:production")).toBe(false);
|
|
236
|
+
expect(cacheValues.has("prompt:target-prompt:staging")).toBe(false);
|
|
237
|
+
expect(cacheValues.has("prompt:other-prompt:production")).toBe(true);
|
|
238
|
+
expect(mockLoggerInfo).toHaveBeenCalledWith(
|
|
239
|
+
"Langfuse prompt cache invalidated for: target-prompt"
|
|
240
|
+
);
|
|
241
|
+
});
|
|
242
|
+
});
|
|
@@ -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/tests/bunSetup.ts
CHANGED
|
@@ -4,19 +4,25 @@ import {setupEnvironment, winstonLogger} from "@terreno/api";
|
|
|
4
4
|
import mongoose from "mongoose";
|
|
5
5
|
import winston from "winston";
|
|
6
6
|
|
|
7
|
+
const shouldConnectToTestDb = process.env.BUN_TEST_DISABLE_DB !== "true";
|
|
8
|
+
|
|
7
9
|
// Connect to MongoDB once for all tests
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
});
|
|
10
|
+
if (shouldConnectToTestDb) {
|
|
11
|
+
beforeAll(async () => {
|
|
12
|
+
await mongoose
|
|
13
|
+
.connect("mongodb://127.0.0.1/terreno-ai-test?&connectTimeoutMS=360000")
|
|
14
|
+
.catch((err) => {
|
|
15
|
+
console.error("Failed to connect to MongoDB:", err);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
15
19
|
|
|
16
20
|
// Close MongoDB connection after all tests
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
if (shouldConnectToTestDb) {
|
|
22
|
+
afterAll(async () => {
|
|
23
|
+
await mongoose.connection.close();
|
|
24
|
+
});
|
|
25
|
+
}
|
|
20
26
|
|
|
21
27
|
let logs: string[] = [];
|
|
22
28
|
|
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. */
|