@terreno/ai 57.3.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.
@@ -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
- var api_1 = require("@terreno/api");
66
- var langfuseCache_1 = require("./langfuseCache");
67
- var langfuseClient_1 = require("./langfuseClient");
68
- var DEFAULT_PROMPT_TTL = 60;
69
- var DEFAULT_LABEL = "production";
70
- var buildPromptCacheKey = function (name, label) {
71
- return "prompt:".concat(name, ":").concat(label !== null && label !== void 0 ? label : DEFAULT_LABEL);
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
- var getPrompt = function (name_1) {
74
- var args_1 = [];
75
- for (var _i = 1; _i < arguments.length; _i++) {
76
- args_1[_i - 1] = arguments[_i];
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
- return __awaiter(void 0, __spreadArray([name_1], __read(args_1), false), void 0, function (name, options, appOptions) {
79
- var ttl, cacheKey, cached, client, prompt, cachedPrompt;
80
- var _a, _b, _c, _d, _e, _f, _g, _h;
81
- if (options === void 0) { options = {}; }
82
- return __generator(this, function (_j) {
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
- var compilePrompt = function (cached, variables) {
122
- if (variables === void 0) { variables = {}; }
123
- var replace = function (template) {
124
- return template.replace(/\{\{(\w+)\}\}/g, function (_match, key) {
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(function (msg) { return ({
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
- var createPrompt = function (params) { return __awaiter(void 0, void 0, void 0, function () {
139
- var client, chatMessages;
140
- var _a, _b, _c, _d;
141
- return __generator(this, function (_e) {
142
- switch (_e.label) {
143
- case 0:
144
- client = (0, langfuseClient_1.getLangfuseClient)();
145
- if (!(params.type === "text")) return [3 /*break*/, 2];
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
- })];
154
- case 1:
155
- _e.sent();
156
- return [3 /*break*/, 4];
157
- case 2:
158
- chatMessages = params.prompt.map(function (msg) { return ({
159
- content: msg.content,
160
- role: msg.role,
161
- }); });
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
- })];
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
- var invalidatePromptCache = function (name) { return __awaiter(void 0, void 0, void 0, function () {
182
- return __generator(this, function (_a) {
183
- switch (_a.label) {
184
- case 0: return [4 /*yield*/, (0, langfuseCache_1.invalidateCache)("prompt:".concat(name, ":"))];
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
- var api_1 = require("@terreno/api");
41
- var langfuseClient_1 = require("./langfuseClient");
42
- var langfuseRoutesMiddleware_1 = require("./langfuseRoutesMiddleware");
43
- var addEvaluationRoutes = function (router, basePath, scoringFunctions) {
44
- if (scoringFunctions === void 0) { scoringFunctions = []; }
45
- router.get("".concat(basePath, "/evaluations/config"), [(0, api_1.authenticateMiddleware)(), langfuseRoutesMiddleware_1.requireAdmin], (0, api_1.asyncHandler)(function (_req, res) { return __awaiter(void 0, void 0, void 0, function () {
46
- return __generator(this, function (_a) {
47
- return [2 /*return*/, res.json({ scoringFunctions: scoringFunctions })];
48
- });
49
- }); }));
50
- router.post("".concat(basePath, "/evaluations"), [(0, api_1.authenticateMiddleware)(), langfuseRoutesMiddleware_1.requireAdmin], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
51
- var _a, traceId, name, value, comment, dataType, client;
52
- return __generator(this, function (_b) {
53
- switch (_b.label) {
54
- case 0:
55
- _a = req.body, traceId = _a.traceId, name = _a.name, value = _a.value, comment = _a.comment, dataType = _a.dataType;
56
- if (!traceId || !name || value === undefined) {
57
- throw new api_1.APIError({
58
- detail: "traceId, name, and value are required",
59
- status: 400,
60
- title: "Invalid request",
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
- var api_1 = require("@terreno/api");
5
- var requireAdmin = function (req, _res, next) {
6
- var user = req.user;
7
- var isAdmin = typeof (user === null || user === void 0 ? void 0 : user.isAdmin) === "function" ? user.isAdmin() : api_1.Permissions.IsAdmin("read", user);
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
- var api_1 = require("@terreno/api");
66
- var langfusePrompts_1 = require("./langfusePrompts");
67
- var langfuseRoutesMiddleware_1 = require("./langfuseRoutesMiddleware");
68
- var addPlaygroundRoutes = function (router, basePath) {
69
- router.post("".concat(basePath, "/playground"), [(0, api_1.authenticateMiddleware)(), langfuseRoutesMiddleware_1.requireAdmin], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
70
- var _a, promptName, variables, label, cached, compiled;
71
- return __generator(this, function (_b) {
72
- switch (_b.label) {
73
- case 0:
74
- _a = req.body, promptName = _a.promptName, variables = _a.variables, label = _a.label;
75
- if (!promptName) {
76
- throw new api_1.APIError({
77
- detail: "promptName is required",
78
- status: 400,
79
- title: "Invalid request",
80
- });
81
- }
82
- return [4 /*yield*/, (0, langfusePrompts_1.getPrompt)(promptName, { label: label, variables: variables })];
83
- case 1:
84
- cached = _b.sent();
85
- compiled = (0, langfusePrompts_1.compilePrompt)(cached, variables !== null && variables !== void 0 ? variables : {});
86
- return [2 /*return*/, res.json({
87
- compiled: compiled,
88
- config: cached.config,
89
- labels: cached.labels,
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
- var extractVariables = function (template) {
103
- var _a;
104
- var matches = (_a = template.match(/\{\{(\w+)\}\}/g)) !== null && _a !== void 0 ? _a : [];
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
  };