@terreno/ai 57.4.0 → 57.6.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/README.md +2 -2
- package/dist/aiAdminApp.js +9 -10
- package/dist/aiApp.js +31 -31
- package/dist/langfuseApp.js +27 -76
- package/dist/langfuseCache.js +20 -79
- package/dist/langfuseClient.js +13 -58
- package/dist/langfusePrompts.js +70 -173
- package/dist/langfuseRoutesEvaluations.js +26 -73
- package/dist/langfuseRoutesMiddleware.js +4 -4
- package/dist/langfuseRoutesPlayground.js +29 -99
- package/dist/langfuseRoutesPrompts.js +48 -121
- package/dist/langfuseRoutesTraces.js +24 -76
- package/dist/langfuseTracing.js +15 -60
- package/dist/langfuseVercelAi.js +38 -87
- package/dist/models/aiRequest.js +19 -72
- package/dist/models/fileAttachment.js +3 -3
- package/dist/models/gptHistory.js +5 -5
- package/dist/models/project.js +4 -4
- package/dist/routes/aiRequestsExplorer.js +89 -150
- package/dist/routes/files.js +50 -115
- package/dist/routes/gpt.js +457 -694
- package/dist/routes/gptHistories.js +17 -19
- package/dist/routes/mcp.js +30 -102
- package/dist/routes/projects.js +53 -116
- package/dist/service/aiService.js +393 -706
- package/dist/service/fileStorage.js +42 -134
- package/dist/service/gemini.js +55 -130
- package/dist/service/getMCPTools.js +17 -86
- package/dist/service/mcpService.js +87 -258
- package/dist/service/parseAiJson.js +32 -32
- package/dist/service/vertex.js +129 -283
- package/package.json +7 -7
- package/tsconfig.json +6 -21
package/dist/langfusePrompts.js
CHANGED
|
@@ -1,192 +1,89 @@
|
|
|
1
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 __read = (this && this.__read) || function (o, n) {
|
|
39
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
40
|
-
if (!m) return o;
|
|
41
|
-
var i = m.call(o), r, ar = [], e;
|
|
42
|
-
try {
|
|
43
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
44
|
-
}
|
|
45
|
-
catch (error) { e = { error: error }; }
|
|
46
|
-
finally {
|
|
47
|
-
try {
|
|
48
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
49
|
-
}
|
|
50
|
-
finally { if (e) throw e.error; }
|
|
51
|
-
}
|
|
52
|
-
return ar;
|
|
53
|
-
};
|
|
54
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
55
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
56
|
-
if (ar || !(i in from)) {
|
|
57
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
58
|
-
ar[i] = from[i];
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
62
|
-
};
|
|
63
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
64
3
|
exports.invalidatePromptCache = exports.createPrompt = exports.compilePrompt = exports.getPrompt = void 0;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return
|
|
4
|
+
const api_1 = require("@terreno/api");
|
|
5
|
+
const langfuseCache_1 = require("./langfuseCache");
|
|
6
|
+
const langfuseClient_1 = require("./langfuseClient");
|
|
7
|
+
const DEFAULT_PROMPT_TTL = 60;
|
|
8
|
+
const DEFAULT_LABEL = "production";
|
|
9
|
+
const buildPromptCacheKey = (name, label) => {
|
|
10
|
+
return `prompt:${name}:${label ?? DEFAULT_LABEL}`;
|
|
72
11
|
};
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
12
|
+
const getPrompt = async (name, options = {}, appOptions) => {
|
|
13
|
+
const ttl = appOptions?.cache?.promptTtlSeconds ?? DEFAULT_PROMPT_TTL;
|
|
14
|
+
const cacheKey = buildPromptCacheKey(name, options.label);
|
|
15
|
+
const cached = await (0, langfuseCache_1.getCached)(cacheKey);
|
|
16
|
+
if (cached) {
|
|
17
|
+
api_1.logger.debug(`Langfuse prompt cache hit: "${name}" v${cached.version} (label: ${options.label ?? DEFAULT_LABEL})`);
|
|
18
|
+
return cached;
|
|
77
19
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
switch (_j.label) {
|
|
84
|
-
case 0:
|
|
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;
|
|
86
|
-
cacheKey = buildPromptCacheKey(name, options.label);
|
|
87
|
-
return [4 /*yield*/, (0, langfuseCache_1.getCached)(cacheKey)];
|
|
88
|
-
case 1:
|
|
89
|
-
cached = _j.sent();
|
|
90
|
-
if (cached) {
|
|
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, ")"));
|
|
92
|
-
return [2 /*return*/, cached];
|
|
93
|
-
}
|
|
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, ")"));
|
|
95
|
-
client = (0, langfuseClient_1.getLangfuseClient)();
|
|
96
|
-
return [4 /*yield*/, client.prompt.get(name, {
|
|
97
|
-
cacheTtlSeconds: 0,
|
|
98
|
-
label: (_e = options.label) !== null && _e !== void 0 ? _e : DEFAULT_LABEL,
|
|
99
|
-
})];
|
|
100
|
-
case 2:
|
|
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
|
-
};
|
|
111
|
-
api_1.logger.info("Langfuse prompt fetched: \"".concat(name, "\" v").concat(cachedPrompt.version, " (ttl: ").concat(ttl, "s)"));
|
|
112
|
-
return [4 /*yield*/, (0, langfuseCache_1.setCached)(cacheKey, cachedPrompt, ttl)];
|
|
113
|
-
case 3:
|
|
114
|
-
_j.sent();
|
|
115
|
-
return [2 /*return*/, cachedPrompt];
|
|
116
|
-
}
|
|
117
|
-
});
|
|
20
|
+
api_1.logger.info(`Langfuse prompt cache miss: fetching "${name}" from API (label: ${options.label ?? DEFAULT_LABEL})`);
|
|
21
|
+
const client = (0, langfuseClient_1.getLangfuseClient)();
|
|
22
|
+
const prompt = await client.prompt.get(name, {
|
|
23
|
+
cacheTtlSeconds: 0,
|
|
24
|
+
label: options.label ?? DEFAULT_LABEL,
|
|
118
25
|
});
|
|
26
|
+
const cachedPrompt = {
|
|
27
|
+
config: (prompt.config ?? {}),
|
|
28
|
+
labels: prompt.labels ?? [],
|
|
29
|
+
name: prompt.name,
|
|
30
|
+
prompt: prompt.prompt,
|
|
31
|
+
tags: prompt.tags ?? [],
|
|
32
|
+
type: prompt.type,
|
|
33
|
+
version: prompt.version,
|
|
34
|
+
};
|
|
35
|
+
api_1.logger.info(`Langfuse prompt fetched: "${name}" v${cachedPrompt.version} (ttl: ${ttl}s)`);
|
|
36
|
+
await (0, langfuseCache_1.setCached)(cacheKey, cachedPrompt, ttl);
|
|
37
|
+
return cachedPrompt;
|
|
119
38
|
};
|
|
120
39
|
exports.getPrompt = getPrompt;
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
var _a;
|
|
126
|
-
return (_a = variables[key]) !== null && _a !== void 0 ? _a : "{{".concat(key, "}}");
|
|
40
|
+
const compilePrompt = (cached, variables = {}) => {
|
|
41
|
+
const replace = (template) => {
|
|
42
|
+
return template.replace(/\{\{(\w+)\}\}/g, (_match, key) => {
|
|
43
|
+
return variables[key] ?? `{{${key}}}`;
|
|
127
44
|
});
|
|
128
45
|
};
|
|
129
46
|
if (cached.type === "text") {
|
|
130
47
|
return replace(cached.prompt);
|
|
131
48
|
}
|
|
132
|
-
return cached.prompt.map(
|
|
49
|
+
return cached.prompt.map((msg) => ({
|
|
133
50
|
content: replace(msg.content),
|
|
134
51
|
role: msg.role,
|
|
135
|
-
})
|
|
52
|
+
}));
|
|
136
53
|
};
|
|
137
54
|
exports.compilePrompt = compilePrompt;
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
tags: (_d = params.tags) !== null && _d !== void 0 ? _d : [],
|
|
168
|
-
type: "chat",
|
|
169
|
-
})];
|
|
170
|
-
case 3:
|
|
171
|
-
_e.sent();
|
|
172
|
-
_e.label = 4;
|
|
173
|
-
case 4: return [4 /*yield*/, (0, exports.invalidatePromptCache)(params.name)];
|
|
174
|
-
case 5:
|
|
175
|
-
_e.sent();
|
|
176
|
-
return [2 /*return*/, (0, exports.getPrompt)(params.name, { label: DEFAULT_LABEL })];
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
}); };
|
|
55
|
+
const createPrompt = async (params) => {
|
|
56
|
+
const client = (0, langfuseClient_1.getLangfuseClient)();
|
|
57
|
+
if (params.type === "text") {
|
|
58
|
+
await client.prompt.create({
|
|
59
|
+
config: params.config,
|
|
60
|
+
labels: params.labels ?? [DEFAULT_LABEL],
|
|
61
|
+
name: params.name,
|
|
62
|
+
prompt: params.prompt,
|
|
63
|
+
tags: params.tags ?? [],
|
|
64
|
+
type: "text",
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const chatMessages = params.prompt.map((msg) => ({
|
|
69
|
+
content: msg.content,
|
|
70
|
+
role: msg.role,
|
|
71
|
+
}));
|
|
72
|
+
await client.prompt.create({
|
|
73
|
+
config: params.config,
|
|
74
|
+
labels: params.labels ?? [DEFAULT_LABEL],
|
|
75
|
+
name: params.name,
|
|
76
|
+
prompt: chatMessages,
|
|
77
|
+
tags: params.tags ?? [],
|
|
78
|
+
type: "chat",
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
await (0, exports.invalidatePromptCache)(params.name);
|
|
82
|
+
return (0, exports.getPrompt)(params.name, { label: DEFAULT_LABEL });
|
|
83
|
+
};
|
|
180
84
|
exports.createPrompt = createPrompt;
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
case 1:
|
|
186
|
-
_a.sent();
|
|
187
|
-
api_1.logger.info("Langfuse prompt cache invalidated for: ".concat(name));
|
|
188
|
-
return [2 /*return*/];
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
}); };
|
|
85
|
+
const invalidatePromptCache = async (name) => {
|
|
86
|
+
await (0, langfuseCache_1.invalidateCache)(`prompt:${name}:`);
|
|
87
|
+
api_1.logger.info(`Langfuse prompt cache invalidated for: ${name}`);
|
|
88
|
+
};
|
|
192
89
|
exports.invalidatePromptCache = invalidatePromptCache;
|
|
@@ -1,79 +1,32 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.addEvaluationRoutes = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
client = (0, langfuseClient_1.getLangfuseClient)();
|
|
64
|
-
client.score.create({
|
|
65
|
-
comment: comment !== null && comment !== void 0 ? comment : undefined,
|
|
66
|
-
dataType: dataType,
|
|
67
|
-
name: name,
|
|
68
|
-
traceId: traceId,
|
|
69
|
-
value: value,
|
|
70
|
-
});
|
|
71
|
-
return [4 /*yield*/, client.flush()];
|
|
72
|
-
case 1:
|
|
73
|
-
_b.sent();
|
|
74
|
-
return [2 /*return*/, res.status(201).json({ comment: comment, dataType: dataType, name: name, traceId: traceId, value: value })];
|
|
75
|
-
}
|
|
4
|
+
const api_1 = require("@terreno/api");
|
|
5
|
+
const langfuseClient_1 = require("./langfuseClient");
|
|
6
|
+
const langfuseRoutesMiddleware_1 = require("./langfuseRoutesMiddleware");
|
|
7
|
+
const addEvaluationRoutes = (router, basePath, scoringFunctions = []) => {
|
|
8
|
+
router.get(`${basePath}/evaluations/config`, [(0, api_1.authenticateMiddleware)(), langfuseRoutesMiddleware_1.requireAdmin], (0, api_1.asyncHandler)(async (_req, res) => {
|
|
9
|
+
return res.json({ scoringFunctions });
|
|
10
|
+
}));
|
|
11
|
+
router.post(`${basePath}/evaluations`, [(0, api_1.authenticateMiddleware)(), langfuseRoutesMiddleware_1.requireAdmin], (0, api_1.asyncHandler)(async (req, res) => {
|
|
12
|
+
const { traceId, name, value, comment, dataType } = req.body;
|
|
13
|
+
if (!traceId || !name || value === undefined) {
|
|
14
|
+
throw new api_1.APIError({
|
|
15
|
+
detail: "traceId, name, and value are required",
|
|
16
|
+
status: 400,
|
|
17
|
+
title: "Invalid request",
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
const client = (0, langfuseClient_1.getLangfuseClient)();
|
|
21
|
+
client.score.create({
|
|
22
|
+
comment: comment ?? undefined,
|
|
23
|
+
dataType: dataType,
|
|
24
|
+
name,
|
|
25
|
+
traceId,
|
|
26
|
+
value,
|
|
76
27
|
});
|
|
77
|
-
|
|
28
|
+
await client.flush();
|
|
29
|
+
return res.status(201).json({ comment, dataType, name, traceId, value });
|
|
30
|
+
}));
|
|
78
31
|
};
|
|
79
32
|
exports.addEvaluationRoutes = addEvaluationRoutes;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.requireAdmin = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const api_1 = require("@terreno/api");
|
|
5
|
+
const requireAdmin = (req, _res, next) => {
|
|
6
|
+
const user = req.user;
|
|
7
|
+
const isAdmin = typeof user?.isAdmin === "function" ? user.isAdmin() : api_1.Permissions.IsAdmin("read", user);
|
|
8
8
|
if (!isAdmin) {
|
|
9
9
|
throw new api_1.APIError({ disableExternalErrorTracking: true, status: 403, title: "Forbidden" });
|
|
10
10
|
}
|
|
@@ -1,106 +1,36 @@
|
|
|
1
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 __read = (this && this.__read) || function (o, n) {
|
|
39
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
40
|
-
if (!m) return o;
|
|
41
|
-
var i = m.call(o), r, ar = [], e;
|
|
42
|
-
try {
|
|
43
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
44
|
-
}
|
|
45
|
-
catch (error) { e = { error: error }; }
|
|
46
|
-
finally {
|
|
47
|
-
try {
|
|
48
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
49
|
-
}
|
|
50
|
-
finally { if (e) throw e.error; }
|
|
51
|
-
}
|
|
52
|
-
return ar;
|
|
53
|
-
};
|
|
54
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
55
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
56
|
-
if (ar || !(i in from)) {
|
|
57
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
58
|
-
ar[i] = from[i];
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
62
|
-
};
|
|
63
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
64
3
|
exports.addPlaygroundRoutes = void 0;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
router.post(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
name: cached.name,
|
|
91
|
-
type: cached.type,
|
|
92
|
-
variables: extractVariables(cached.type === "text"
|
|
93
|
-
? cached.prompt
|
|
94
|
-
: cached.prompt.map(function (m) { return m.content; }).join(" ")),
|
|
95
|
-
version: cached.version,
|
|
96
|
-
})];
|
|
97
|
-
}
|
|
4
|
+
const api_1 = require("@terreno/api");
|
|
5
|
+
const langfusePrompts_1 = require("./langfusePrompts");
|
|
6
|
+
const langfuseRoutesMiddleware_1 = require("./langfuseRoutesMiddleware");
|
|
7
|
+
const addPlaygroundRoutes = (router, basePath) => {
|
|
8
|
+
router.post(`${basePath}/playground`, [(0, api_1.authenticateMiddleware)(), langfuseRoutesMiddleware_1.requireAdmin], (0, api_1.asyncHandler)(async (req, res) => {
|
|
9
|
+
const { promptName, variables, label } = req.body;
|
|
10
|
+
if (!promptName) {
|
|
11
|
+
throw new api_1.APIError({
|
|
12
|
+
detail: "promptName is required",
|
|
13
|
+
status: 400,
|
|
14
|
+
title: "Invalid request",
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
const cached = await (0, langfusePrompts_1.getPrompt)(promptName, { label, variables });
|
|
18
|
+
const compiled = (0, langfusePrompts_1.compilePrompt)(cached, variables ?? {});
|
|
19
|
+
return res.json({
|
|
20
|
+
compiled,
|
|
21
|
+
config: cached.config,
|
|
22
|
+
labels: cached.labels,
|
|
23
|
+
name: cached.name,
|
|
24
|
+
type: cached.type,
|
|
25
|
+
variables: extractVariables(cached.type === "text"
|
|
26
|
+
? cached.prompt
|
|
27
|
+
: cached.prompt.map((m) => m.content).join(" ")),
|
|
28
|
+
version: cached.version,
|
|
98
29
|
});
|
|
99
|
-
})
|
|
30
|
+
}));
|
|
100
31
|
};
|
|
101
32
|
exports.addPlaygroundRoutes = addPlaygroundRoutes;
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return __spreadArray([], __read(new Set(matches.map(function (m) { return m.slice(2, -2); }))), false);
|
|
33
|
+
const extractVariables = (template) => {
|
|
34
|
+
const matches = template.match(/\{\{(\w+)\}\}/g) ?? [];
|
|
35
|
+
return [...new Set(matches.map((m) => m.slice(2, -2)))];
|
|
106
36
|
};
|