@terreno/ai 0.7.1 → 0.8.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/langfuseClient.d.ts +3 -3
- package/dist/langfuseClient.js +7 -14
- package/dist/langfusePrompts.js +37 -60
- package/dist/langfuseRoutesEvaluations.js +3 -3
- package/dist/langfuseRoutesPrompts.js +2 -2
- package/dist/langfuseRoutesTraces.js +5 -5
- package/package.json +5 -6
- package/src/langfuseClient.ts +6 -8
- package/src/langfusePrompts.test.ts +25 -36
- package/src/langfusePrompts.ts +18 -47
- package/src/langfuseRoutes.test.ts +20 -5
- package/src/langfuseRoutesEvaluations.ts +3 -3
- package/src/langfuseRoutesPrompts.ts +3 -2
- package/src/langfuseRoutesTraces.ts +5 -5
package/dist/langfuseClient.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LangfuseClient } from "@langfuse/client";
|
|
2
2
|
import type { LangfuseAppOptions } from "./langfuseTypes";
|
|
3
|
-
export declare const initLangfuseClient: (options: LangfuseAppOptions) =>
|
|
4
|
-
export declare const getLangfuseClient: () =>
|
|
3
|
+
export declare const initLangfuseClient: (options: LangfuseAppOptions) => LangfuseClient;
|
|
4
|
+
export declare const getLangfuseClient: () => LangfuseClient;
|
|
5
5
|
export declare const isLangfuseInitialized: () => boolean;
|
|
6
6
|
export declare const shutdownLangfuseClient: () => Promise<void>;
|
package/dist/langfuseClient.js
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -48,11 +37,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
48
37
|
};
|
|
49
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
39
|
exports.shutdownLangfuseClient = exports.isLangfuseInitialized = exports.getLangfuseClient = exports.initLangfuseClient = void 0;
|
|
51
|
-
var
|
|
40
|
+
var client_1 = require("@langfuse/client");
|
|
52
41
|
var langfuseInstance = null;
|
|
53
42
|
var initLangfuseClient = function (options) {
|
|
54
43
|
var _a;
|
|
55
|
-
langfuseInstance = new
|
|
44
|
+
langfuseInstance = new client_1.LangfuseClient({
|
|
45
|
+
baseUrl: (_a = options.baseUrl) !== null && _a !== void 0 ? _a : "https://cloud.langfuse.com",
|
|
46
|
+
publicKey: options.publicKey,
|
|
47
|
+
secretKey: options.secretKey,
|
|
48
|
+
});
|
|
56
49
|
return langfuseInstance;
|
|
57
50
|
};
|
|
58
51
|
exports.initLangfuseClient = initLangfuseClient;
|
|
@@ -70,7 +63,7 @@ var shutdownLangfuseClient = function () { return __awaiter(void 0, void 0, void
|
|
|
70
63
|
switch (_a.label) {
|
|
71
64
|
case 0:
|
|
72
65
|
if (!langfuseInstance) return [3 /*break*/, 2];
|
|
73
|
-
return [4 /*yield*/, langfuseInstance.
|
|
66
|
+
return [4 /*yield*/, langfuseInstance.shutdown()];
|
|
74
67
|
case 1:
|
|
75
68
|
_a.sent();
|
|
76
69
|
langfuseInstance = null;
|
package/dist/langfusePrompts.js
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -81,55 +70,48 @@ var DEFAULT_LABEL = "production";
|
|
|
81
70
|
var buildPromptCacheKey = function (name, label) {
|
|
82
71
|
return "prompt:".concat(name, ":").concat(label !== null && label !== void 0 ? label : DEFAULT_LABEL);
|
|
83
72
|
};
|
|
84
|
-
var toLatestVersion = function (data) {
|
|
85
|
-
var _a, _b, _c;
|
|
86
|
-
var base = {
|
|
87
|
-
config: ((_a = data.config) !== null && _a !== void 0 ? _a : {}),
|
|
88
|
-
labels: (_b = data.labels) !== null && _b !== void 0 ? _b : [],
|
|
89
|
-
name: data.name,
|
|
90
|
-
tags: (_c = data.tags) !== null && _c !== void 0 ? _c : [],
|
|
91
|
-
version: data.version,
|
|
92
|
-
};
|
|
93
|
-
if (data.type === "chat") {
|
|
94
|
-
return __assign(__assign({}, base), { prompt: data.prompt, type: "chat" });
|
|
95
|
-
}
|
|
96
|
-
return __assign(__assign({}, base), { prompt: data.prompt, type: "text" });
|
|
97
|
-
};
|
|
98
73
|
var getPrompt = function (name_1) {
|
|
99
74
|
var args_1 = [];
|
|
100
75
|
for (var _i = 1; _i < arguments.length; _i++) {
|
|
101
76
|
args_1[_i - 1] = arguments[_i];
|
|
102
77
|
}
|
|
103
78
|
return __awaiter(void 0, __spreadArray([name_1], __read(args_1), false), void 0, function (name, options, appOptions) {
|
|
104
|
-
var ttl, cacheKey, cached, client,
|
|
105
|
-
var _a, _b, _c, _d, _e, _f;
|
|
79
|
+
var ttl, cacheKey, cached, client, prompt, cachedPrompt;
|
|
80
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
106
81
|
if (options === void 0) { options = {}; }
|
|
107
|
-
return __generator(this, function (
|
|
108
|
-
switch (
|
|
82
|
+
return __generator(this, function (_j) {
|
|
83
|
+
switch (_j.label) {
|
|
109
84
|
case 0:
|
|
110
85
|
ttl = (_b = (_a = appOptions === null || appOptions === void 0 ? void 0 : appOptions.cache) === null || _a === void 0 ? void 0 : _a.promptTtlSeconds) !== null && _b !== void 0 ? _b : DEFAULT_PROMPT_TTL;
|
|
111
86
|
cacheKey = buildPromptCacheKey(name, options.label);
|
|
112
87
|
return [4 /*yield*/, (0, langfuseCache_1.getCached)(cacheKey)];
|
|
113
88
|
case 1:
|
|
114
|
-
cached =
|
|
89
|
+
cached = _j.sent();
|
|
115
90
|
if (cached) {
|
|
116
91
|
api_1.logger.debug("Langfuse prompt cache hit: \"".concat(name, "\" v").concat(cached.version, " (label: ").concat((_c = options.label) !== null && _c !== void 0 ? _c : DEFAULT_LABEL, ")"));
|
|
117
92
|
return [2 /*return*/, cached];
|
|
118
93
|
}
|
|
119
94
|
api_1.logger.info("Langfuse prompt cache miss: fetching \"".concat(name, "\" from API (label: ").concat((_d = options.label) !== null && _d !== void 0 ? _d : DEFAULT_LABEL, ")"));
|
|
120
95
|
client = (0, langfuseClient_1.getLangfuseClient)();
|
|
121
|
-
return [4 /*yield*/, client.
|
|
96
|
+
return [4 /*yield*/, client.prompt.get(name, {
|
|
97
|
+
cacheTtlSeconds: 0,
|
|
98
|
+
label: (_e = options.label) !== null && _e !== void 0 ? _e : DEFAULT_LABEL,
|
|
99
|
+
})];
|
|
122
100
|
case 2:
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
101
|
+
prompt = _j.sent();
|
|
102
|
+
cachedPrompt = {
|
|
103
|
+
config: ((_f = prompt.config) !== null && _f !== void 0 ? _f : {}),
|
|
104
|
+
labels: (_g = prompt.labels) !== null && _g !== void 0 ? _g : [],
|
|
105
|
+
name: prompt.name,
|
|
106
|
+
prompt: prompt.prompt,
|
|
107
|
+
tags: (_h = prompt.tags) !== null && _h !== void 0 ? _h : [],
|
|
108
|
+
type: prompt.type,
|
|
109
|
+
version: prompt.version,
|
|
110
|
+
};
|
|
129
111
|
api_1.logger.info("Langfuse prompt fetched: \"".concat(name, "\" v").concat(cachedPrompt.version, " (ttl: ").concat(ttl, "s)"));
|
|
130
112
|
return [4 /*yield*/, (0, langfuseCache_1.setCached)(cacheKey, cachedPrompt, ttl)];
|
|
131
113
|
case 3:
|
|
132
|
-
|
|
114
|
+
_j.sent();
|
|
133
115
|
return [2 /*return*/, cachedPrompt];
|
|
134
116
|
}
|
|
135
117
|
});
|
|
@@ -154,23 +136,21 @@ var compilePrompt = function (cached, variables) {
|
|
|
154
136
|
};
|
|
155
137
|
exports.compilePrompt = compilePrompt;
|
|
156
138
|
var createPrompt = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
157
|
-
var client,
|
|
139
|
+
var client, chatMessages;
|
|
158
140
|
var _a, _b, _c, _d;
|
|
159
141
|
return __generator(this, function (_e) {
|
|
160
142
|
switch (_e.label) {
|
|
161
143
|
case 0:
|
|
162
144
|
client = (0, langfuseClient_1.getLangfuseClient)();
|
|
163
145
|
if (!(params.type === "text")) return [3 /*break*/, 2];
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
};
|
|
173
|
-
return [4 /*yield*/, client.createPrompt(body)];
|
|
146
|
+
return [4 /*yield*/, client.prompt.create({
|
|
147
|
+
config: params.config,
|
|
148
|
+
labels: (_a = params.labels) !== null && _a !== void 0 ? _a : [DEFAULT_LABEL],
|
|
149
|
+
name: params.name,
|
|
150
|
+
prompt: params.prompt,
|
|
151
|
+
tags: (_b = params.tags) !== null && _b !== void 0 ? _b : [],
|
|
152
|
+
type: "text",
|
|
153
|
+
})];
|
|
174
154
|
case 1:
|
|
175
155
|
_e.sent();
|
|
176
156
|
return [3 /*break*/, 4];
|
|
@@ -178,18 +158,15 @@ var createPrompt = function (params) { return __awaiter(void 0, void 0, void 0,
|
|
|
178
158
|
chatMessages = params.prompt.map(function (msg) { return ({
|
|
179
159
|
content: msg.content,
|
|
180
160
|
role: msg.role,
|
|
181
|
-
type: "chatmessage",
|
|
182
161
|
}); });
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
};
|
|
192
|
-
return [4 /*yield*/, client.createPrompt(body)];
|
|
162
|
+
return [4 /*yield*/, client.prompt.create({
|
|
163
|
+
config: params.config,
|
|
164
|
+
labels: (_c = params.labels) !== null && _c !== void 0 ? _c : [DEFAULT_LABEL],
|
|
165
|
+
name: params.name,
|
|
166
|
+
prompt: chatMessages,
|
|
167
|
+
tags: (_d = params.tags) !== null && _d !== void 0 ? _d : [],
|
|
168
|
+
type: "chat",
|
|
169
|
+
})];
|
|
193
170
|
case 3:
|
|
194
171
|
_e.sent();
|
|
195
172
|
_e.label = 4;
|
|
@@ -61,14 +61,14 @@ var addEvaluationRoutes = function (router, basePath, scoringFunctions) {
|
|
|
61
61
|
});
|
|
62
62
|
}
|
|
63
63
|
client = (0, langfuseClient_1.getLangfuseClient)();
|
|
64
|
-
client.score({
|
|
65
|
-
comment: comment !== null && comment !== void 0 ? comment :
|
|
64
|
+
client.score.create({
|
|
65
|
+
comment: comment !== null && comment !== void 0 ? comment : undefined,
|
|
66
66
|
dataType: dataType,
|
|
67
67
|
name: name,
|
|
68
68
|
traceId: traceId,
|
|
69
69
|
value: value,
|
|
70
70
|
});
|
|
71
|
-
return [4 /*yield*/, client.
|
|
71
|
+
return [4 /*yield*/, client.flush()];
|
|
72
72
|
case 1:
|
|
73
73
|
_b.sent();
|
|
74
74
|
return [2 /*return*/, res.status(201).json({ comment: comment, dataType: dataType, name: name, traceId: traceId, value: value })];
|
|
@@ -61,7 +61,7 @@ var addPromptRoutes = function (router, basePath) {
|
|
|
61
61
|
client = (0, langfuseClient_1.getLangfuseClient)();
|
|
62
62
|
page = Number(req.query.page) || 1;
|
|
63
63
|
limit = Number(req.query.limit) || 20;
|
|
64
|
-
return [4 /*yield*/, client.api.
|
|
64
|
+
return [4 /*yield*/, client.api.prompts.list({ limit: limit, page: page })];
|
|
65
65
|
case 1:
|
|
66
66
|
result = _a.sent();
|
|
67
67
|
return [2 /*return*/, res.json({
|
|
@@ -80,7 +80,7 @@ var addPromptRoutes = function (router, basePath) {
|
|
|
80
80
|
name = req.params.name;
|
|
81
81
|
version = req.query.version ? Number(req.query.version) : undefined;
|
|
82
82
|
label = req.query.label;
|
|
83
|
-
return [4 /*yield*/, client.
|
|
83
|
+
return [4 /*yield*/, client.prompt.get(name, __assign(__assign({ cacheTtlSeconds: 0 }, (version !== undefined ? { version: version } : {})), (label ? { label: label } : {})))];
|
|
84
84
|
case 1:
|
|
85
85
|
prompt = _a.sent();
|
|
86
86
|
return [2 /*return*/, res.json({
|
|
@@ -50,9 +50,9 @@ var addTraceRoutes = function (router, basePath) {
|
|
|
50
50
|
page = Number(req.query.page) || 1;
|
|
51
51
|
limit = Number(req.query.limit) || 20;
|
|
52
52
|
userId = req.query.userId;
|
|
53
|
-
fromTimestamp = req.query.from ?
|
|
54
|
-
toTimestamp = req.query.to ?
|
|
55
|
-
return [4 /*yield*/, client.
|
|
53
|
+
fromTimestamp = req.query.from ? req.query.from : undefined;
|
|
54
|
+
toTimestamp = req.query.to ? req.query.to : undefined;
|
|
55
|
+
return [4 /*yield*/, client.api.trace.list({
|
|
56
56
|
fromTimestamp: fromTimestamp,
|
|
57
57
|
limit: limit,
|
|
58
58
|
page: page,
|
|
@@ -71,10 +71,10 @@ var addTraceRoutes = function (router, basePath) {
|
|
|
71
71
|
switch (_a.label) {
|
|
72
72
|
case 0:
|
|
73
73
|
client = (0, langfuseClient_1.getLangfuseClient)();
|
|
74
|
-
return [4 /*yield*/, client.
|
|
74
|
+
return [4 /*yield*/, client.api.trace.get(req.params.traceId)];
|
|
75
75
|
case 1:
|
|
76
76
|
result = _a.sent();
|
|
77
|
-
return [2 /*return*/, res.json(result
|
|
77
|
+
return [2 /*return*/, res.json(result)];
|
|
78
78
|
}
|
|
79
79
|
});
|
|
80
80
|
}); }));
|
package/package.json
CHANGED
|
@@ -6,13 +6,12 @@
|
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@ai-sdk/mcp": "^1.0.21",
|
|
8
8
|
"@google-cloud/storage": "^7.15.0",
|
|
9
|
-
"@langfuse/
|
|
9
|
+
"@langfuse/client": "^5.0.1",
|
|
10
|
+
"@langfuse/otel": "^5.0.1",
|
|
10
11
|
"@opentelemetry/api": "^1.9.0",
|
|
11
12
|
"@opentelemetry/sdk-node": "^0.213.0",
|
|
12
|
-
"@terreno/api": "0.
|
|
13
|
+
"@terreno/api": "0.8.0",
|
|
13
14
|
"ai": "^6.0.97",
|
|
14
|
-
"langfuse": "^3.38.6",
|
|
15
|
-
"langfuse-core": "^3.38.6",
|
|
16
15
|
"multer": "^1.4.5-lts.1"
|
|
17
16
|
},
|
|
18
17
|
"description": "AI service layer for Terreno apps - GPT chat, request logging, and admin tools.",
|
|
@@ -46,7 +45,7 @@
|
|
|
46
45
|
"main": "dist/index.js",
|
|
47
46
|
"name": "@terreno/ai",
|
|
48
47
|
"peerDependencies": {
|
|
49
|
-
"@terreno/api": "0.
|
|
48
|
+
"@terreno/api": "0.8.0",
|
|
50
49
|
"mongoose": "^8.0.0"
|
|
51
50
|
},
|
|
52
51
|
"repository": {
|
|
@@ -64,5 +63,5 @@
|
|
|
64
63
|
"test:ci": "bun test --preload ./src/tests/bunSetup.ts"
|
|
65
64
|
},
|
|
66
65
|
"types": "dist/index.d.ts",
|
|
67
|
-
"version": "0.
|
|
66
|
+
"version": "0.8.0"
|
|
68
67
|
}
|
package/src/langfuseClient.ts
CHANGED
|
@@ -1,21 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {LangfuseClient} from "@langfuse/client";
|
|
2
2
|
|
|
3
3
|
import type {LangfuseAppOptions} from "./langfuseTypes";
|
|
4
4
|
|
|
5
|
-
let langfuseInstance:
|
|
5
|
+
let langfuseInstance: LangfuseClient | null = null;
|
|
6
6
|
|
|
7
|
-
export const initLangfuseClient = (options: LangfuseAppOptions):
|
|
8
|
-
langfuseInstance = new
|
|
7
|
+
export const initLangfuseClient = (options: LangfuseAppOptions): LangfuseClient => {
|
|
8
|
+
langfuseInstance = new LangfuseClient({
|
|
9
9
|
baseUrl: options.baseUrl ?? "https://cloud.langfuse.com",
|
|
10
|
-
flushAt: 1,
|
|
11
10
|
publicKey: options.publicKey,
|
|
12
11
|
secretKey: options.secretKey,
|
|
13
|
-
...(options.projectId ? {_projectId: options.projectId} : {}),
|
|
14
12
|
});
|
|
15
13
|
return langfuseInstance;
|
|
16
14
|
};
|
|
17
15
|
|
|
18
|
-
export const getLangfuseClient = ():
|
|
16
|
+
export const getLangfuseClient = (): LangfuseClient => {
|
|
19
17
|
if (!langfuseInstance) {
|
|
20
18
|
throw new Error("Langfuse client not initialized. Call initLangfuseClient first.");
|
|
21
19
|
}
|
|
@@ -26,7 +24,7 @@ export const isLangfuseInitialized = (): boolean => langfuseInstance != null;
|
|
|
26
24
|
|
|
27
25
|
export const shutdownLangfuseClient = async (): Promise<void> => {
|
|
28
26
|
if (langfuseInstance) {
|
|
29
|
-
await langfuseInstance.
|
|
27
|
+
await langfuseInstance.shutdown();
|
|
30
28
|
langfuseInstance = null;
|
|
31
29
|
}
|
|
32
30
|
};
|
|
@@ -3,21 +3,18 @@ import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
|
3
3
|
import {LangfuseCache} from "./langfuseCache";
|
|
4
4
|
import type {LangfuseCachedPrompt} from "./langfuseTypes";
|
|
5
5
|
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
version: 1,
|
|
15
|
-
},
|
|
16
|
-
fetchResult: "success" as const,
|
|
6
|
+
const mockPromptGet = mock(async (_name: string, _options?: Record<string, unknown>) => ({
|
|
7
|
+
config: {},
|
|
8
|
+
labels: ["production"],
|
|
9
|
+
name: "test-prompt",
|
|
10
|
+
prompt: "Hello world",
|
|
11
|
+
tags: [],
|
|
12
|
+
type: "text" as const,
|
|
13
|
+
version: 1,
|
|
17
14
|
}));
|
|
18
15
|
|
|
19
16
|
mock.module("./langfuseClient", () => ({
|
|
20
|
-
getLangfuseClient: () => ({
|
|
17
|
+
getLangfuseClient: () => ({prompt: {get: mockPromptGet}}),
|
|
21
18
|
}));
|
|
22
19
|
|
|
23
20
|
const {compilePrompt, getPrompt} = await import("./langfusePrompts");
|
|
@@ -90,21 +87,16 @@ describe("compilePrompt", () => {
|
|
|
90
87
|
describe("getPrompt", () => {
|
|
91
88
|
beforeEach(async () => {
|
|
92
89
|
await LangfuseCache.deleteMany({});
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
version: 1,
|
|
104
|
-
},
|
|
105
|
-
fetchResult: "success" as const,
|
|
106
|
-
})
|
|
107
|
-
);
|
|
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
|
+
}));
|
|
108
100
|
});
|
|
109
101
|
|
|
110
102
|
afterEach(async () => {
|
|
@@ -115,13 +107,13 @@ describe("getPrompt", () => {
|
|
|
115
107
|
const result = await getPrompt("test-prompt");
|
|
116
108
|
expect(result.name).toBe("test-prompt");
|
|
117
109
|
expect(result.type).toBe("text");
|
|
118
|
-
expect(
|
|
110
|
+
expect(mockPromptGet).toHaveBeenCalledTimes(1);
|
|
119
111
|
});
|
|
120
112
|
|
|
121
113
|
it("returns cached result on second call", async () => {
|
|
122
114
|
await getPrompt("test-prompt");
|
|
123
115
|
await getPrompt("test-prompt");
|
|
124
|
-
expect(
|
|
116
|
+
expect(mockPromptGet).toHaveBeenCalledTimes(1);
|
|
125
117
|
});
|
|
126
118
|
|
|
127
119
|
it("writes fetched prompt to cache", async () => {
|
|
@@ -131,13 +123,10 @@ describe("getPrompt", () => {
|
|
|
131
123
|
});
|
|
132
124
|
|
|
133
125
|
it("throws when Langfuse fetch fails", async () => {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}));
|
|
126
|
+
mockPromptGet.mockImplementation(async () => {
|
|
127
|
+
throw new Error("Prompt not found");
|
|
128
|
+
});
|
|
138
129
|
|
|
139
|
-
await expect(getPrompt("missing-prompt")).rejects.toThrow(
|
|
140
|
-
'Failed to fetch prompt "missing-prompt"'
|
|
141
|
-
);
|
|
130
|
+
await expect(getPrompt("missing-prompt")).rejects.toThrow("Prompt not found");
|
|
142
131
|
});
|
|
143
132
|
});
|
package/src/langfusePrompts.ts
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
import {logger} from "@terreno/api";
|
|
2
|
-
import type {
|
|
3
|
-
CreateChatPromptBody,
|
|
4
|
-
CreateTextPromptBody,
|
|
5
|
-
GetLangfusePromptSuccessData,
|
|
6
|
-
} from "langfuse-core";
|
|
7
2
|
|
|
8
3
|
import {getCached, invalidateCache, setCached} from "./langfuseCache";
|
|
9
4
|
import {getLangfuseClient} from "./langfuseClient";
|
|
@@ -22,30 +17,6 @@ const buildPromptCacheKey = (name: string, label?: string): string => {
|
|
|
22
17
|
return `prompt:${name}:${label ?? DEFAULT_LABEL}`;
|
|
23
18
|
};
|
|
24
19
|
|
|
25
|
-
const toLatestVersion = (data: GetLangfusePromptSuccessData): LangfuseCachedPrompt => {
|
|
26
|
-
const base = {
|
|
27
|
-
config: (data.config ?? {}) as Record<string, unknown>,
|
|
28
|
-
labels: data.labels ?? [],
|
|
29
|
-
name: data.name,
|
|
30
|
-
tags: data.tags ?? [],
|
|
31
|
-
version: data.version,
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
if (data.type === "chat") {
|
|
35
|
-
return {
|
|
36
|
-
...base,
|
|
37
|
-
prompt: data.prompt as ChatMessage[],
|
|
38
|
-
type: "chat",
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
...base,
|
|
44
|
-
prompt: data.prompt as string,
|
|
45
|
-
type: "text",
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
|
|
49
20
|
export const getPrompt = async (
|
|
50
21
|
name: string,
|
|
51
22
|
options: GetPromptOptions = {},
|
|
@@ -66,16 +37,21 @@ export const getPrompt = async (
|
|
|
66
37
|
`Langfuse prompt cache miss: fetching "${name}" from API (label: ${options.label ?? DEFAULT_LABEL})`
|
|
67
38
|
);
|
|
68
39
|
const client = getLangfuseClient();
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
40
|
+
const prompt = await client.prompt.get(name, {
|
|
41
|
+
cacheTtlSeconds: 0,
|
|
42
|
+
label: options.label ?? DEFAULT_LABEL,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const cachedPrompt: LangfuseCachedPrompt = {
|
|
46
|
+
config: (prompt.config ?? {}) as Record<string, unknown>,
|
|
47
|
+
labels: prompt.labels ?? [],
|
|
48
|
+
name: prompt.name,
|
|
49
|
+
prompt: prompt.prompt as string | ChatMessage[],
|
|
50
|
+
tags: prompt.tags ?? [],
|
|
51
|
+
type: prompt.type as "text" | "chat",
|
|
52
|
+
version: prompt.version,
|
|
53
|
+
};
|
|
77
54
|
|
|
78
|
-
const cachedPrompt = toLatestVersion(result.data);
|
|
79
55
|
logger.info(`Langfuse prompt fetched: "${name}" v${cachedPrompt.version} (ttl: ${ttl}s)`);
|
|
80
56
|
await setCached(cacheKey, cachedPrompt, ttl);
|
|
81
57
|
return cachedPrompt;
|
|
@@ -112,32 +88,27 @@ export const createPrompt = async (params: {
|
|
|
112
88
|
const client = getLangfuseClient();
|
|
113
89
|
|
|
114
90
|
if (params.type === "text") {
|
|
115
|
-
|
|
91
|
+
await client.prompt.create({
|
|
116
92
|
config: params.config,
|
|
117
|
-
isActive: true,
|
|
118
93
|
labels: params.labels ?? [DEFAULT_LABEL],
|
|
119
94
|
name: params.name,
|
|
120
95
|
prompt: params.prompt as string,
|
|
121
96
|
tags: params.tags ?? [],
|
|
122
97
|
type: "text",
|
|
123
|
-
};
|
|
124
|
-
await client.createPrompt(body);
|
|
98
|
+
});
|
|
125
99
|
} else {
|
|
126
100
|
const chatMessages = (params.prompt as ChatMessage[]).map((msg) => ({
|
|
127
101
|
content: msg.content,
|
|
128
102
|
role: msg.role,
|
|
129
|
-
type: "chatmessage" as const,
|
|
130
103
|
}));
|
|
131
|
-
|
|
104
|
+
await client.prompt.create({
|
|
132
105
|
config: params.config,
|
|
133
|
-
isActive: true,
|
|
134
106
|
labels: params.labels ?? [DEFAULT_LABEL],
|
|
135
107
|
name: params.name,
|
|
136
108
|
prompt: chatMessages,
|
|
137
109
|
tags: params.tags ?? [],
|
|
138
110
|
type: "chat",
|
|
139
|
-
};
|
|
140
|
-
await client.createPrompt(body);
|
|
111
|
+
});
|
|
141
112
|
}
|
|
142
113
|
|
|
143
114
|
await invalidatePromptCache(params.name);
|
|
@@ -7,12 +7,27 @@ import supertest from "supertest";
|
|
|
7
7
|
mock.module("./langfuseClient", () => ({
|
|
8
8
|
getLangfuseClient: () => ({
|
|
9
9
|
api: {
|
|
10
|
-
|
|
10
|
+
prompts: {
|
|
11
|
+
list: async () => ({data: [], meta: {limit: 20, page: 1, total: 0, totalPages: 0}}),
|
|
12
|
+
},
|
|
13
|
+
trace: {
|
|
14
|
+
get: async () => null,
|
|
15
|
+
list: async () => ({data: [], meta: {limit: 20, page: 1, total: 0, totalPages: 0}}),
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
flush: async () => {},
|
|
19
|
+
prompt: {
|
|
20
|
+
get: async () => ({
|
|
21
|
+
config: {},
|
|
22
|
+
labels: [],
|
|
23
|
+
name: "test",
|
|
24
|
+
prompt: "",
|
|
25
|
+
tags: [],
|
|
26
|
+
type: "text",
|
|
27
|
+
version: 1,
|
|
28
|
+
}),
|
|
11
29
|
},
|
|
12
|
-
|
|
13
|
-
fetchTraces: async () => ({data: [], meta: {limit: 20, page: 1, total: 0, totalPages: 0}}),
|
|
14
|
-
flushAsync: async () => {},
|
|
15
|
-
score: () => {},
|
|
30
|
+
score: {create: () => {}},
|
|
16
31
|
}),
|
|
17
32
|
}));
|
|
18
33
|
|
|
@@ -33,15 +33,15 @@ export const addEvaluationRoutes = (
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
const client = getLangfuseClient();
|
|
36
|
-
client.score({
|
|
37
|
-
comment: comment ??
|
|
36
|
+
client.score.create({
|
|
37
|
+
comment: comment ?? undefined,
|
|
38
38
|
dataType: dataType as "NUMERIC" | "CATEGORICAL" | "BOOLEAN" | undefined,
|
|
39
39
|
name,
|
|
40
40
|
traceId,
|
|
41
41
|
value,
|
|
42
42
|
});
|
|
43
43
|
|
|
44
|
-
await client.
|
|
44
|
+
await client.flush();
|
|
45
45
|
return res.status(201).json({comment, dataType, name, traceId, value});
|
|
46
46
|
})
|
|
47
47
|
);
|
|
@@ -15,7 +15,7 @@ export const addPromptRoutes = (router: express.Application, basePath: string):
|
|
|
15
15
|
const page = Number(req.query.page) || 1;
|
|
16
16
|
const limit = Number(req.query.limit) || 20;
|
|
17
17
|
|
|
18
|
-
const result = await client.api.
|
|
18
|
+
const result = await client.api.prompts.list({limit, page});
|
|
19
19
|
return res.json({
|
|
20
20
|
data: result.data,
|
|
21
21
|
meta: result.meta,
|
|
@@ -32,8 +32,9 @@ export const addPromptRoutes = (router: express.Application, basePath: string):
|
|
|
32
32
|
const version = req.query.version ? Number(req.query.version) : undefined;
|
|
33
33
|
const label = req.query.label as string | undefined;
|
|
34
34
|
|
|
35
|
-
const prompt = await client.
|
|
35
|
+
const prompt = await client.prompt.get(name, {
|
|
36
36
|
cacheTtlSeconds: 0,
|
|
37
|
+
...(version !== undefined ? {version} : {}),
|
|
37
38
|
...(label ? {label} : {}),
|
|
38
39
|
});
|
|
39
40
|
|
|
@@ -13,10 +13,10 @@ export const addTraceRoutes = (router: express.Application, basePath: string): v
|
|
|
13
13
|
const page = Number(req.query.page) || 1;
|
|
14
14
|
const limit = Number(req.query.limit) || 20;
|
|
15
15
|
const userId = req.query.userId as string | undefined;
|
|
16
|
-
const fromTimestamp = req.query.from ?
|
|
17
|
-
const toTimestamp = req.query.to ?
|
|
16
|
+
const fromTimestamp = req.query.from ? (req.query.from as string) : undefined;
|
|
17
|
+
const toTimestamp = req.query.to ? (req.query.to as string) : undefined;
|
|
18
18
|
|
|
19
|
-
const result = await client.
|
|
19
|
+
const result = await client.api.trace.list({
|
|
20
20
|
fromTimestamp,
|
|
21
21
|
limit,
|
|
22
22
|
page,
|
|
@@ -33,8 +33,8 @@ export const addTraceRoutes = (router: express.Application, basePath: string): v
|
|
|
33
33
|
[authenticateMiddleware(), requireAdmin],
|
|
34
34
|
asyncHandler(async (req, res) => {
|
|
35
35
|
const client = getLangfuseClient();
|
|
36
|
-
const result = await client.
|
|
37
|
-
return res.json(result
|
|
36
|
+
const result = await client.api.trace.get(req.params.traceId);
|
|
37
|
+
return res.json(result);
|
|
38
38
|
})
|
|
39
39
|
);
|
|
40
40
|
};
|