@terreno/ai 0.6.0 → 0.7.1

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.
Files changed (63) hide show
  1. package/dist/index.d.ts +7 -0
  2. package/dist/index.js +24 -1
  3. package/dist/langfuseApp.d.ts +9 -0
  4. package/dist/langfuseApp.js +96 -0
  5. package/dist/langfuseCache.d.ts +16 -0
  6. package/dist/langfuseCache.js +104 -0
  7. package/dist/langfuseClient.d.ts +6 -0
  8. package/dist/langfuseClient.js +82 -0
  9. package/dist/langfusePrompts.d.ts +12 -0
  10. package/dist/langfusePrompts.js +215 -0
  11. package/dist/langfuseRoutesEvaluations.d.ts +3 -0
  12. package/dist/langfuseRoutesEvaluations.js +79 -0
  13. package/dist/langfuseRoutesMiddleware.d.ts +2 -0
  14. package/dist/langfuseRoutesMiddleware.js +13 -0
  15. package/dist/langfuseRoutesPlayground.d.ts +2 -0
  16. package/dist/langfuseRoutesPlayground.js +106 -0
  17. package/dist/langfuseRoutesPrompts.d.ts +2 -0
  18. package/dist/langfuseRoutesPrompts.js +129 -0
  19. package/dist/langfuseRoutesTraces.d.ts +2 -0
  20. package/dist/langfuseRoutesTraces.js +82 -0
  21. package/dist/langfuseTracing.d.ts +4 -0
  22. package/dist/langfuseTracing.js +77 -0
  23. package/dist/langfuseTypes.d.ts +96 -0
  24. package/dist/langfuseTypes.js +2 -0
  25. package/dist/langfuseVercelAi.d.ts +12 -0
  26. package/dist/langfuseVercelAi.js +98 -0
  27. package/dist/routes/gpt.js +110 -79
  28. package/dist/service/aiService.js +3 -0
  29. package/dist/types/index.d.ts +3 -0
  30. package/package.json +16 -3
  31. package/src/index.ts +24 -0
  32. package/src/langfuseApp.ts +57 -0
  33. package/src/langfuseCache.test.ts +83 -0
  34. package/src/langfuseCache.ts +58 -0
  35. package/src/langfuseClient.ts +32 -0
  36. package/src/langfusePrompts.test.ts +143 -0
  37. package/src/langfusePrompts.ts +150 -0
  38. package/src/langfuseRoutes.test.ts +143 -0
  39. package/src/langfuseRoutesEvaluations.ts +48 -0
  40. package/src/langfuseRoutesMiddleware.ts +12 -0
  41. package/src/langfuseRoutesPlayground.ts +50 -0
  42. package/src/langfuseRoutesPrompts.ts +86 -0
  43. package/src/langfuseRoutesTraces.ts +40 -0
  44. package/src/langfuseTracing.ts +33 -0
  45. package/src/langfuseTypes.ts +103 -0
  46. package/src/langfuseVercelAi.ts +72 -0
  47. package/src/routes/gpt.ts +30 -0
  48. package/src/routes/routes.test.ts +9 -1
  49. package/src/service/aiService.ts +3 -0
  50. package/src/types/index.ts +3 -0
  51. package/tsconfig.json +1 -1
  52. package/dist/models/aiRequest.test.d.ts +0 -1
  53. package/dist/models/aiRequest.test.js +0 -189
  54. package/dist/models/fileAttachment.test.d.ts +0 -1
  55. package/dist/models/fileAttachment.test.js +0 -217
  56. package/dist/models/gptHistory.test.d.ts +0 -1
  57. package/dist/models/gptHistory.test.js +0 -318
  58. package/dist/routes/routes.test.d.ts +0 -1
  59. package/dist/routes/routes.test.js +0 -303
  60. package/dist/service/aiService.test.d.ts +0 -1
  61. package/dist/service/aiService.test.js +0 -367
  62. package/dist/tests/bunSetup.d.ts +0 -1
  63. package/dist/tests/bunSetup.js +0 -227
@@ -0,0 +1,79 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ 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({
65
+ comment: comment !== null && comment !== void 0 ? comment : null,
66
+ dataType: dataType,
67
+ name: name,
68
+ traceId: traceId,
69
+ value: value,
70
+ });
71
+ return [4 /*yield*/, client.flushAsync()];
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
+ }
76
+ });
77
+ }); }));
78
+ };
79
+ exports.addEvaluationRoutes = addEvaluationRoutes;
@@ -0,0 +1,2 @@
1
+ import type { NextFunction, Request, Response } from "express";
2
+ export declare const requireAdmin: (req: Request, _res: Response, next: NextFunction) => void;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
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);
8
+ if (!isAdmin) {
9
+ throw new api_1.APIError({ disableExternalErrorTracking: true, status: 403, title: "Forbidden" });
10
+ }
11
+ next();
12
+ };
13
+ exports.requireAdmin = requireAdmin;
@@ -0,0 +1,2 @@
1
+ import type express from "express";
2
+ export declare const addPlaygroundRoutes: (router: express.Application, basePath: string) => void;
@@ -0,0 +1,106 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
64
+ 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
+ }
98
+ });
99
+ }); }));
100
+ };
101
+ 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);
106
+ };
@@ -0,0 +1,2 @@
1
+ import type express from "express";
2
+ export declare const addPromptRoutes: (router: express.Application, basePath: string) => void;
@@ -0,0 +1,129 @@
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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ 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);
24
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.addPromptRoutes = void 0;
51
+ var api_1 = require("@terreno/api");
52
+ var langfuseClient_1 = require("./langfuseClient");
53
+ var langfusePrompts_1 = require("./langfusePrompts");
54
+ var langfuseRoutesMiddleware_1 = require("./langfuseRoutesMiddleware");
55
+ var addPromptRoutes = function (router, basePath) {
56
+ router.get("".concat(basePath, "/prompts"), [(0, api_1.authenticateMiddleware)(), langfuseRoutesMiddleware_1.requireAdmin], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
57
+ var client, page, limit, result;
58
+ return __generator(this, function (_a) {
59
+ switch (_a.label) {
60
+ case 0:
61
+ client = (0, langfuseClient_1.getLangfuseClient)();
62
+ page = Number(req.query.page) || 1;
63
+ limit = Number(req.query.limit) || 20;
64
+ return [4 /*yield*/, client.api.promptsList({ limit: limit, page: page })];
65
+ case 1:
66
+ result = _a.sent();
67
+ return [2 /*return*/, res.json({
68
+ data: result.data,
69
+ meta: result.meta,
70
+ })];
71
+ }
72
+ });
73
+ }); }));
74
+ router.get("".concat(basePath, "/prompts/:name"), [(0, api_1.authenticateMiddleware)(), langfuseRoutesMiddleware_1.requireAdmin], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
75
+ var client, name, version, label, prompt;
76
+ return __generator(this, function (_a) {
77
+ switch (_a.label) {
78
+ case 0:
79
+ client = (0, langfuseClient_1.getLangfuseClient)();
80
+ name = req.params.name;
81
+ version = req.query.version ? Number(req.query.version) : undefined;
82
+ label = req.query.label;
83
+ return [4 /*yield*/, client.getPrompt(name, version, __assign({ cacheTtlSeconds: 0 }, (label ? { label: label } : {})))];
84
+ case 1:
85
+ prompt = _a.sent();
86
+ return [2 /*return*/, res.json({
87
+ config: prompt.config,
88
+ labels: prompt.labels,
89
+ name: prompt.name,
90
+ prompt: prompt.prompt,
91
+ tags: prompt.tags,
92
+ type: prompt.type,
93
+ version: prompt.version,
94
+ })];
95
+ }
96
+ });
97
+ }); }));
98
+ router.post("".concat(basePath, "/prompts"), [(0, api_1.authenticateMiddleware)(), langfuseRoutesMiddleware_1.requireAdmin], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
99
+ var _a, name, type, prompt, labels, tags, config, result;
100
+ return __generator(this, function (_b) {
101
+ switch (_b.label) {
102
+ case 0:
103
+ _a = req.body, name = _a.name, type = _a.type, prompt = _a.prompt, labels = _a.labels, tags = _a.tags, config = _a.config;
104
+ if (!name || !type || prompt === undefined) {
105
+ throw new api_1.APIError({
106
+ detail: "name, type, and prompt are required",
107
+ status: 400,
108
+ title: "Invalid request",
109
+ });
110
+ }
111
+ return [4 /*yield*/, (0, langfusePrompts_1.createPrompt)({ config: config, labels: labels, name: name, prompt: prompt, tags: tags, type: type })];
112
+ case 1:
113
+ result = _b.sent();
114
+ return [2 /*return*/, res.status(201).json(result)];
115
+ }
116
+ });
117
+ }); }));
118
+ router.delete("".concat(basePath, "/prompts/:name/cache"), [(0, api_1.authenticateMiddleware)(), langfuseRoutesMiddleware_1.requireAdmin], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
119
+ return __generator(this, function (_a) {
120
+ switch (_a.label) {
121
+ case 0: return [4 /*yield*/, (0, langfusePrompts_1.invalidatePromptCache)(req.params.name)];
122
+ case 1:
123
+ _a.sent();
124
+ return [2 /*return*/, res.json({ invalidated: true, name: req.params.name })];
125
+ }
126
+ });
127
+ }); }));
128
+ };
129
+ exports.addPromptRoutes = addPromptRoutes;
@@ -0,0 +1,2 @@
1
+ import type express from "express";
2
+ export declare const addTraceRoutes: (router: express.Application, basePath: string) => void;
@@ -0,0 +1,82 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.addTraceRoutes = void 0;
40
+ var api_1 = require("@terreno/api");
41
+ var langfuseClient_1 = require("./langfuseClient");
42
+ var langfuseRoutesMiddleware_1 = require("./langfuseRoutesMiddleware");
43
+ var addTraceRoutes = function (router, basePath) {
44
+ router.get("".concat(basePath, "/traces"), [(0, api_1.authenticateMiddleware)(), langfuseRoutesMiddleware_1.requireAdmin], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
45
+ var client, page, limit, userId, fromTimestamp, toTimestamp, result;
46
+ return __generator(this, function (_a) {
47
+ switch (_a.label) {
48
+ case 0:
49
+ client = (0, langfuseClient_1.getLangfuseClient)();
50
+ page = Number(req.query.page) || 1;
51
+ limit = Number(req.query.limit) || 20;
52
+ userId = req.query.userId;
53
+ fromTimestamp = req.query.from ? new Date(req.query.from) : undefined;
54
+ toTimestamp = req.query.to ? new Date(req.query.to) : undefined;
55
+ return [4 /*yield*/, client.fetchTraces({
56
+ fromTimestamp: fromTimestamp,
57
+ limit: limit,
58
+ page: page,
59
+ toTimestamp: toTimestamp,
60
+ userId: userId,
61
+ })];
62
+ case 1:
63
+ result = _a.sent();
64
+ return [2 /*return*/, res.json({ data: result.data, meta: result.meta })];
65
+ }
66
+ });
67
+ }); }));
68
+ router.get("".concat(basePath, "/traces/:traceId"), [(0, api_1.authenticateMiddleware)(), langfuseRoutesMiddleware_1.requireAdmin], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
69
+ var client, result;
70
+ return __generator(this, function (_a) {
71
+ switch (_a.label) {
72
+ case 0:
73
+ client = (0, langfuseClient_1.getLangfuseClient)();
74
+ return [4 /*yield*/, client.fetchTrace(req.params.traceId)];
75
+ case 1:
76
+ result = _a.sent();
77
+ return [2 /*return*/, res.json(result.data)];
78
+ }
79
+ });
80
+ }); }));
81
+ };
82
+ exports.addTraceRoutes = addTraceRoutes;
@@ -0,0 +1,4 @@
1
+ import { NodeSDK } from "@opentelemetry/sdk-node";
2
+ import type { LangfuseAppOptions } from "./langfuseTypes";
3
+ export declare const initTracing: (options: LangfuseAppOptions) => NodeSDK;
4
+ export declare const shutdownTracing: () => Promise<void>;
@@ -0,0 +1,77 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.shutdownTracing = exports.initTracing = void 0;
40
+ var otel_1 = require("@langfuse/otel");
41
+ var sdk_node_1 = require("@opentelemetry/sdk-node");
42
+ var api_1 = require("@terreno/api");
43
+ var sdkInstance = null;
44
+ var initTracing = function (options) {
45
+ var _a, _b;
46
+ var sdk = new sdk_node_1.NodeSDK({
47
+ serviceName: (_a = options.serviceName) !== null && _a !== void 0 ? _a : "terreno-app",
48
+ spanProcessors: [
49
+ new otel_1.LangfuseSpanProcessor({
50
+ baseUrl: (_b = options.baseUrl) !== null && _b !== void 0 ? _b : "https://cloud.langfuse.com",
51
+ publicKey: options.publicKey,
52
+ secretKey: options.secretKey,
53
+ }),
54
+ ],
55
+ });
56
+ sdk.start();
57
+ sdkInstance = sdk;
58
+ api_1.logger.info("Langfuse OpenTelemetry tracing initialized");
59
+ return sdk;
60
+ };
61
+ exports.initTracing = initTracing;
62
+ var shutdownTracing = function () { return __awaiter(void 0, void 0, void 0, function () {
63
+ return __generator(this, function (_a) {
64
+ switch (_a.label) {
65
+ case 0:
66
+ if (!sdkInstance) return [3 /*break*/, 2];
67
+ return [4 /*yield*/, sdkInstance.shutdown()];
68
+ case 1:
69
+ _a.sent();
70
+ sdkInstance = null;
71
+ api_1.logger.info("Langfuse OpenTelemetry tracing shut down");
72
+ _a.label = 2;
73
+ case 2: return [2 /*return*/];
74
+ }
75
+ });
76
+ }); };
77
+ exports.shutdownTracing = shutdownTracing;
@@ -0,0 +1,96 @@
1
+ export interface ScoringFunction {
2
+ name: string;
3
+ description: string;
4
+ scoreType: "numeric" | "categorical" | "boolean";
5
+ categories?: string[];
6
+ range?: {
7
+ min: number;
8
+ max: number;
9
+ };
10
+ }
11
+ export interface LangfuseAppOptions {
12
+ secretKey: string;
13
+ publicKey: string;
14
+ /** Organization slug in Langfuse (default: "flourish-health") */
15
+ organization?: string;
16
+ /** Project name/slug in Langfuse (default: "terreno") */
17
+ project?: string;
18
+ /** Langfuse project ID (UUID) for trace URLs; optional, keys usually imply project */
19
+ projectId?: string;
20
+ baseUrl?: string;
21
+ adminPath?: string;
22
+ enableAdminUI?: boolean;
23
+ enableTracing?: boolean;
24
+ serviceName?: string;
25
+ cache?: {
26
+ promptTtlSeconds?: number;
27
+ traceTtlSeconds?: number;
28
+ };
29
+ evaluation?: {
30
+ enabled?: boolean;
31
+ scoringFunctions?: ScoringFunction[];
32
+ };
33
+ }
34
+ export interface ChatMessage {
35
+ role: string;
36
+ content: string;
37
+ }
38
+ export interface LangfuseCachedPrompt {
39
+ name: string;
40
+ version: number;
41
+ type: "text" | "chat";
42
+ prompt: string | ChatMessage[];
43
+ config: Record<string, unknown>;
44
+ labels: string[];
45
+ tags: string[];
46
+ }
47
+ export interface TelemetrySettings {
48
+ isEnabled?: boolean;
49
+ functionId?: string;
50
+ metadata?: Record<string, string | number | boolean>;
51
+ }
52
+ export interface PreparePromptResult {
53
+ prompt?: string;
54
+ messages?: ChatMessage[];
55
+ config: Record<string, unknown>;
56
+ telemetry: TelemetrySettings;
57
+ }
58
+ export interface GetPromptOptions {
59
+ label?: string;
60
+ variables?: Record<string, string>;
61
+ userId?: string;
62
+ }
63
+ export interface PromptListItem {
64
+ name: string;
65
+ version: number;
66
+ type: "text" | "chat";
67
+ labels: string[];
68
+ tags: string[];
69
+ createdAt: string;
70
+ }
71
+ export interface TraceListItem {
72
+ id: string;
73
+ name: string;
74
+ userId?: string;
75
+ sessionId?: string;
76
+ timestamp: string;
77
+ input?: unknown;
78
+ output?: unknown;
79
+ metadata?: Record<string, unknown>;
80
+ }
81
+ export interface PaginatedResult<T> {
82
+ data: T[];
83
+ meta: {
84
+ page: number;
85
+ limit: number;
86
+ total: number;
87
+ totalPages: number;
88
+ };
89
+ }
90
+ export interface ScoreSubmission {
91
+ traceId: string;
92
+ name: string;
93
+ value: number | string;
94
+ comment?: string;
95
+ dataType?: "NUMERIC" | "CATEGORICAL" | "BOOLEAN";
96
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,12 @@
1
+ import type { LangfuseAppOptions, PreparePromptResult } from "./langfuseTypes";
2
+ export declare const preparePromptForAI: (params: {
3
+ promptName: string;
4
+ variables?: Record<string, string>;
5
+ userId?: string;
6
+ label?: string;
7
+ }, appOptions?: Pick<LangfuseAppOptions, "cache">) => Promise<PreparePromptResult>;
8
+ export declare const createTelemetryConfig: (params: {
9
+ functionId: string;
10
+ userId?: string;
11
+ metadata?: Record<string, string | number | boolean>;
12
+ }) => PreparePromptResult["telemetry"];