@terreno/ai 0.5.0 → 0.7.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.
Files changed (64) 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 +17 -4
  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/projects.ts +6 -4
  49. package/src/routes/routes.test.ts +9 -1
  50. package/src/service/aiService.ts +3 -0
  51. package/src/types/index.ts +3 -0
  52. package/tsconfig.json +1 -1
  53. package/dist/models/aiRequest.test.d.ts +0 -1
  54. package/dist/models/aiRequest.test.js +0 -189
  55. package/dist/models/fileAttachment.test.d.ts +0 -1
  56. package/dist/models/fileAttachment.test.js +0 -217
  57. package/dist/models/gptHistory.test.d.ts +0 -1
  58. package/dist/models/gptHistory.test.js +0 -318
  59. package/dist/routes/routes.test.d.ts +0 -1
  60. package/dist/routes/routes.test.js +0 -303
  61. package/dist/service/aiService.test.d.ts +0 -1
  62. package/dist/service/aiService.test.js +0 -367
  63. package/dist/tests/bunSetup.d.ts +0 -1
  64. package/dist/tests/bunSetup.js +0 -227
package/dist/index.d.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  export type { AiAppOptions } from "./aiApp";
2
2
  export { AiApp } from "./aiApp";
3
+ export { LangfuseApp } from "./langfuseApp";
4
+ export { getCached, invalidateCache, LangfuseCache, setCached } from "./langfuseCache";
5
+ export { getLangfuseClient, initLangfuseClient, isLangfuseInitialized, shutdownLangfuseClient, } from "./langfuseClient";
6
+ export { compilePrompt, createPrompt, getPrompt, invalidatePromptCache } from "./langfusePrompts";
7
+ export { initTracing, shutdownTracing } from "./langfuseTracing";
8
+ export type { ChatMessage, GetPromptOptions, LangfuseAppOptions, LangfuseCachedPrompt, PaginatedResult, PreparePromptResult, PromptListItem, ScoreSubmission, ScoringFunction, TelemetrySettings, TraceListItem, } from "./langfuseTypes";
9
+ export { createTelemetryConfig, preparePromptForAI } from "./langfuseVercelAi";
3
10
  export * from "./models";
4
11
  export * from "./routes";
5
12
  export * from "./service";
package/dist/index.js CHANGED
@@ -14,9 +14,32 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.AiApp = void 0;
17
+ exports.preparePromptForAI = exports.createTelemetryConfig = exports.shutdownTracing = exports.initTracing = exports.invalidatePromptCache = exports.getPrompt = exports.createPrompt = exports.compilePrompt = exports.shutdownLangfuseClient = exports.isLangfuseInitialized = exports.initLangfuseClient = exports.getLangfuseClient = exports.setCached = exports.LangfuseCache = exports.invalidateCache = exports.getCached = exports.LangfuseApp = exports.AiApp = void 0;
18
18
  var aiApp_1 = require("./aiApp");
19
19
  Object.defineProperty(exports, "AiApp", { enumerable: true, get: function () { return aiApp_1.AiApp; } });
20
+ var langfuseApp_1 = require("./langfuseApp");
21
+ Object.defineProperty(exports, "LangfuseApp", { enumerable: true, get: function () { return langfuseApp_1.LangfuseApp; } });
22
+ var langfuseCache_1 = require("./langfuseCache");
23
+ Object.defineProperty(exports, "getCached", { enumerable: true, get: function () { return langfuseCache_1.getCached; } });
24
+ Object.defineProperty(exports, "invalidateCache", { enumerable: true, get: function () { return langfuseCache_1.invalidateCache; } });
25
+ Object.defineProperty(exports, "LangfuseCache", { enumerable: true, get: function () { return langfuseCache_1.LangfuseCache; } });
26
+ Object.defineProperty(exports, "setCached", { enumerable: true, get: function () { return langfuseCache_1.setCached; } });
27
+ var langfuseClient_1 = require("./langfuseClient");
28
+ Object.defineProperty(exports, "getLangfuseClient", { enumerable: true, get: function () { return langfuseClient_1.getLangfuseClient; } });
29
+ Object.defineProperty(exports, "initLangfuseClient", { enumerable: true, get: function () { return langfuseClient_1.initLangfuseClient; } });
30
+ Object.defineProperty(exports, "isLangfuseInitialized", { enumerable: true, get: function () { return langfuseClient_1.isLangfuseInitialized; } });
31
+ Object.defineProperty(exports, "shutdownLangfuseClient", { enumerable: true, get: function () { return langfuseClient_1.shutdownLangfuseClient; } });
32
+ var langfusePrompts_1 = require("./langfusePrompts");
33
+ Object.defineProperty(exports, "compilePrompt", { enumerable: true, get: function () { return langfusePrompts_1.compilePrompt; } });
34
+ Object.defineProperty(exports, "createPrompt", { enumerable: true, get: function () { return langfusePrompts_1.createPrompt; } });
35
+ Object.defineProperty(exports, "getPrompt", { enumerable: true, get: function () { return langfusePrompts_1.getPrompt; } });
36
+ Object.defineProperty(exports, "invalidatePromptCache", { enumerable: true, get: function () { return langfusePrompts_1.invalidatePromptCache; } });
37
+ var langfuseTracing_1 = require("./langfuseTracing");
38
+ Object.defineProperty(exports, "initTracing", { enumerable: true, get: function () { return langfuseTracing_1.initTracing; } });
39
+ Object.defineProperty(exports, "shutdownTracing", { enumerable: true, get: function () { return langfuseTracing_1.shutdownTracing; } });
40
+ var langfuseVercelAi_1 = require("./langfuseVercelAi");
41
+ Object.defineProperty(exports, "createTelemetryConfig", { enumerable: true, get: function () { return langfuseVercelAi_1.createTelemetryConfig; } });
42
+ Object.defineProperty(exports, "preparePromptForAI", { enumerable: true, get: function () { return langfuseVercelAi_1.preparePromptForAI; } });
20
43
  __exportStar(require("./models"), exports);
21
44
  __exportStar(require("./routes"), exports);
22
45
  __exportStar(require("./service"), exports);
@@ -0,0 +1,9 @@
1
+ import type { TerrenoPlugin } from "@terreno/api";
2
+ import type express from "express";
3
+ import type { LangfuseAppOptions } from "./langfuseTypes";
4
+ export declare class LangfuseApp implements TerrenoPlugin {
5
+ private options;
6
+ constructor(options: LangfuseAppOptions);
7
+ register(app: express.Application): void;
8
+ shutdown(): Promise<void>;
9
+ }
@@ -0,0 +1,96 @@
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.LangfuseApp = void 0;
40
+ var api_1 = require("@terreno/api");
41
+ var langfuseClient_1 = require("./langfuseClient");
42
+ var langfuseRoutesEvaluations_1 = require("./langfuseRoutesEvaluations");
43
+ var langfuseRoutesPlayground_1 = require("./langfuseRoutesPlayground");
44
+ var langfuseRoutesPrompts_1 = require("./langfuseRoutesPrompts");
45
+ var langfuseRoutesTraces_1 = require("./langfuseRoutesTraces");
46
+ var langfuseTracing_1 = require("./langfuseTracing");
47
+ var LangfuseApp = /** @class */ (function () {
48
+ function LangfuseApp(options) {
49
+ this.options = options;
50
+ }
51
+ LangfuseApp.prototype.register = function (app) {
52
+ var _this = this;
53
+ var _a, _b, _c, _d, _e;
54
+ var organization = (_a = this.options.organization) !== null && _a !== void 0 ? _a : "flourish-health";
55
+ var project = (_b = this.options.project) !== null && _b !== void 0 ? _b : "terreno";
56
+ (0, langfuseClient_1.initLangfuseClient)(this.options);
57
+ if (this.options.enableTracing !== false) {
58
+ try {
59
+ (0, langfuseTracing_1.initTracing)(this.options);
60
+ }
61
+ catch (err) {
62
+ api_1.logger.warn("Langfuse tracing initialization failed: ".concat(err));
63
+ }
64
+ }
65
+ if (this.options.enableAdminUI !== false) {
66
+ var adminPath = (_c = this.options.adminPath) !== null && _c !== void 0 ? _c : "/admin/langfuse";
67
+ (0, langfuseRoutesPrompts_1.addPromptRoutes)(app, adminPath);
68
+ (0, langfuseRoutesTraces_1.addTraceRoutes)(app, adminPath);
69
+ (0, langfuseRoutesPlayground_1.addPlaygroundRoutes)(app, adminPath);
70
+ if ((_d = this.options.evaluation) === null || _d === void 0 ? void 0 : _d.enabled) {
71
+ (0, langfuseRoutesEvaluations_1.addEvaluationRoutes)(app, adminPath, (_e = this.options.evaluation.scoringFunctions) !== null && _e !== void 0 ? _e : []);
72
+ }
73
+ api_1.logger.info("Langfuse admin routes mounted at ".concat(adminPath, " (org: ").concat(organization, ", project: ").concat(project, ")"));
74
+ }
75
+ process.once("SIGTERM", function () {
76
+ void _this.shutdown();
77
+ });
78
+ };
79
+ LangfuseApp.prototype.shutdown = function () {
80
+ return __awaiter(this, void 0, void 0, function () {
81
+ return __generator(this, function (_a) {
82
+ switch (_a.label) {
83
+ case 0: return [4 /*yield*/, (0, langfuseClient_1.shutdownLangfuseClient)()];
84
+ case 1:
85
+ _a.sent();
86
+ return [4 /*yield*/, (0, langfuseTracing_1.shutdownTracing)()];
87
+ case 2:
88
+ _a.sent();
89
+ return [2 /*return*/];
90
+ }
91
+ });
92
+ });
93
+ };
94
+ return LangfuseApp;
95
+ }());
96
+ exports.LangfuseApp = LangfuseApp;
@@ -0,0 +1,16 @@
1
+ import mongoose from "mongoose";
2
+ import type { LangfuseCachedPrompt } from "./langfuseTypes";
3
+ interface LangfuseCacheDocument extends mongoose.Document {
4
+ key: string;
5
+ value: LangfuseCachedPrompt;
6
+ expiresAt: Date;
7
+ }
8
+ export declare const LangfuseCache: mongoose.Model<LangfuseCacheDocument, {}, {}, {}, mongoose.Document<unknown, {}, LangfuseCacheDocument, {}, {}> & LangfuseCacheDocument & Required<{
9
+ _id: mongoose.Types.ObjectId;
10
+ }> & {
11
+ __v: number;
12
+ }, any>;
13
+ export declare const getCached: (key: string) => Promise<LangfuseCachedPrompt | null>;
14
+ export declare const setCached: (key: string, value: LangfuseCachedPrompt, ttlSeconds: number) => Promise<void>;
15
+ export declare const invalidateCache: (keyPattern: string) => Promise<void>;
16
+ export {};
@@ -0,0 +1,104 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.invalidateCache = exports.setCached = exports.getCached = exports.LangfuseCache = void 0;
43
+ var mongoose_1 = __importDefault(require("mongoose"));
44
+ var langfuseCacheSchema = new mongoose_1.default.Schema({
45
+ expiresAt: {
46
+ description: "When this cache entry expires",
47
+ required: true,
48
+ type: Date,
49
+ },
50
+ key: {
51
+ description: "Cache key (e.g. prompt:chat-assistant:production)",
52
+ required: true,
53
+ type: String,
54
+ unique: true,
55
+ },
56
+ value: {
57
+ description: "Cached Langfuse object",
58
+ required: true,
59
+ type: mongoose_1.default.Schema.Types.Mixed,
60
+ },
61
+ }, { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
62
+ langfuseCacheSchema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 });
63
+ exports.LangfuseCache = mongoose_1.default.models.LangfuseCache ||
64
+ mongoose_1.default.model("LangfuseCache", langfuseCacheSchema);
65
+ var getCached = function (key) { return __awaiter(void 0, void 0, void 0, function () {
66
+ var entry;
67
+ return __generator(this, function (_a) {
68
+ switch (_a.label) {
69
+ case 0: return [4 /*yield*/, exports.LangfuseCache.findOne({ expiresAt: { $gt: new Date() }, key: key }).lean()];
70
+ case 1:
71
+ entry = _a.sent();
72
+ if (!entry) {
73
+ return [2 /*return*/, null];
74
+ }
75
+ return [2 /*return*/, entry.value];
76
+ }
77
+ });
78
+ }); };
79
+ exports.getCached = getCached;
80
+ var setCached = function (key, value, ttlSeconds) { return __awaiter(void 0, void 0, void 0, function () {
81
+ var expiresAt;
82
+ return __generator(this, function (_a) {
83
+ switch (_a.label) {
84
+ case 0:
85
+ expiresAt = new Date(Date.now() + ttlSeconds * 1000);
86
+ return [4 /*yield*/, exports.LangfuseCache.findOneAndUpdate({ key: key }, { expiresAt: expiresAt, key: key, value: value }, { upsert: true })];
87
+ case 1:
88
+ _a.sent();
89
+ return [2 /*return*/];
90
+ }
91
+ });
92
+ }); };
93
+ exports.setCached = setCached;
94
+ var invalidateCache = function (keyPattern) { return __awaiter(void 0, void 0, void 0, function () {
95
+ return __generator(this, function (_a) {
96
+ switch (_a.label) {
97
+ case 0: return [4 /*yield*/, exports.LangfuseCache.deleteMany({ key: { $regex: keyPattern } })];
98
+ case 1:
99
+ _a.sent();
100
+ return [2 /*return*/];
101
+ }
102
+ });
103
+ }); };
104
+ exports.invalidateCache = invalidateCache;
@@ -0,0 +1,6 @@
1
+ import { Langfuse } from "langfuse";
2
+ import type { LangfuseAppOptions } from "./langfuseTypes";
3
+ export declare const initLangfuseClient: (options: LangfuseAppOptions) => Langfuse;
4
+ export declare const getLangfuseClient: () => Langfuse;
5
+ export declare const isLangfuseInitialized: () => boolean;
6
+ export declare const shutdownLangfuseClient: () => Promise<void>;
@@ -0,0 +1,82 @@
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.shutdownLangfuseClient = exports.isLangfuseInitialized = exports.getLangfuseClient = exports.initLangfuseClient = void 0;
51
+ var langfuse_1 = require("langfuse");
52
+ var langfuseInstance = null;
53
+ var initLangfuseClient = function (options) {
54
+ var _a;
55
+ langfuseInstance = new langfuse_1.Langfuse(__assign({ baseUrl: (_a = options.baseUrl) !== null && _a !== void 0 ? _a : "https://cloud.langfuse.com", flushAt: 1, publicKey: options.publicKey, secretKey: options.secretKey }, (options.projectId ? { _projectId: options.projectId } : {})));
56
+ return langfuseInstance;
57
+ };
58
+ exports.initLangfuseClient = initLangfuseClient;
59
+ var getLangfuseClient = function () {
60
+ if (!langfuseInstance) {
61
+ throw new Error("Langfuse client not initialized. Call initLangfuseClient first.");
62
+ }
63
+ return langfuseInstance;
64
+ };
65
+ exports.getLangfuseClient = getLangfuseClient;
66
+ var isLangfuseInitialized = function () { return langfuseInstance != null; };
67
+ exports.isLangfuseInitialized = isLangfuseInitialized;
68
+ var shutdownLangfuseClient = function () { return __awaiter(void 0, void 0, void 0, function () {
69
+ return __generator(this, function (_a) {
70
+ switch (_a.label) {
71
+ case 0:
72
+ if (!langfuseInstance) return [3 /*break*/, 2];
73
+ return [4 /*yield*/, langfuseInstance.flushAsync()];
74
+ case 1:
75
+ _a.sent();
76
+ langfuseInstance = null;
77
+ _a.label = 2;
78
+ case 2: return [2 /*return*/];
79
+ }
80
+ });
81
+ }); };
82
+ exports.shutdownLangfuseClient = shutdownLangfuseClient;
@@ -0,0 +1,12 @@
1
+ import type { ChatMessage, GetPromptOptions, LangfuseAppOptions, LangfuseCachedPrompt } from "./langfuseTypes";
2
+ export declare const getPrompt: (name: string, options?: GetPromptOptions, appOptions?: Pick<LangfuseAppOptions, "cache">) => Promise<LangfuseCachedPrompt>;
3
+ export declare const compilePrompt: (cached: LangfuseCachedPrompt, variables?: Record<string, string>) => string | ChatMessage[];
4
+ export declare const createPrompt: (params: {
5
+ name: string;
6
+ type: "text" | "chat";
7
+ prompt: string | ChatMessage[];
8
+ labels?: string[];
9
+ tags?: string[];
10
+ config?: Record<string, unknown>;
11
+ }) => Promise<LangfuseCachedPrompt>;
12
+ export declare const invalidatePromptCache: (name: string) => Promise<void>;
@@ -0,0 +1,215 @@
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
+ var __read = (this && this.__read) || function (o, n) {
50
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
51
+ if (!m) return o;
52
+ var i = m.call(o), r, ar = [], e;
53
+ try {
54
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
+ }
56
+ catch (error) { e = { error: error }; }
57
+ finally {
58
+ try {
59
+ if (r && !r.done && (m = i["return"])) m.call(i);
60
+ }
61
+ finally { if (e) throw e.error; }
62
+ }
63
+ return ar;
64
+ };
65
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
66
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
67
+ if (ar || !(i in from)) {
68
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
69
+ ar[i] = from[i];
70
+ }
71
+ }
72
+ return to.concat(ar || Array.prototype.slice.call(from));
73
+ };
74
+ Object.defineProperty(exports, "__esModule", { value: true });
75
+ exports.invalidatePromptCache = exports.createPrompt = exports.compilePrompt = exports.getPrompt = void 0;
76
+ var api_1 = require("@terreno/api");
77
+ var langfuseCache_1 = require("./langfuseCache");
78
+ var langfuseClient_1 = require("./langfuseClient");
79
+ var DEFAULT_PROMPT_TTL = 60;
80
+ var DEFAULT_LABEL = "production";
81
+ var buildPromptCacheKey = function (name, label) {
82
+ return "prompt:".concat(name, ":").concat(label !== null && label !== void 0 ? label : DEFAULT_LABEL);
83
+ };
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
+ var getPrompt = function (name_1) {
99
+ var args_1 = [];
100
+ for (var _i = 1; _i < arguments.length; _i++) {
101
+ args_1[_i - 1] = arguments[_i];
102
+ }
103
+ return __awaiter(void 0, __spreadArray([name_1], __read(args_1), false), void 0, function (name, options, appOptions) {
104
+ var ttl, cacheKey, cached, client, result, cachedPrompt;
105
+ var _a, _b, _c, _d, _e, _f;
106
+ if (options === void 0) { options = {}; }
107
+ return __generator(this, function (_g) {
108
+ switch (_g.label) {
109
+ case 0:
110
+ 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
+ cacheKey = buildPromptCacheKey(name, options.label);
112
+ return [4 /*yield*/, (0, langfuseCache_1.getCached)(cacheKey)];
113
+ case 1:
114
+ cached = _g.sent();
115
+ if (cached) {
116
+ 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
+ return [2 /*return*/, cached];
118
+ }
119
+ 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
+ client = (0, langfuseClient_1.getLangfuseClient)();
121
+ return [4 /*yield*/, client.getPromptStateless(name, undefined, options.label)];
122
+ case 2:
123
+ result = _g.sent();
124
+ if (result.fetchResult !== "success") {
125
+ api_1.logger.warn("Langfuse prompt fetch failed: \"".concat(name, "\" \u2014 ").concat((_e = result.data.message) !== null && _e !== void 0 ? _e : "unknown error"));
126
+ throw new Error("Failed to fetch prompt \"".concat(name, "\": ").concat((_f = result.data.message) !== null && _f !== void 0 ? _f : "unknown error"));
127
+ }
128
+ cachedPrompt = toLatestVersion(result.data);
129
+ api_1.logger.info("Langfuse prompt fetched: \"".concat(name, "\" v").concat(cachedPrompt.version, " (ttl: ").concat(ttl, "s)"));
130
+ return [4 /*yield*/, (0, langfuseCache_1.setCached)(cacheKey, cachedPrompt, ttl)];
131
+ case 3:
132
+ _g.sent();
133
+ return [2 /*return*/, cachedPrompt];
134
+ }
135
+ });
136
+ });
137
+ };
138
+ exports.getPrompt = getPrompt;
139
+ var compilePrompt = function (cached, variables) {
140
+ if (variables === void 0) { variables = {}; }
141
+ var replace = function (template) {
142
+ return template.replace(/\{\{(\w+)\}\}/g, function (_match, key) {
143
+ var _a;
144
+ return (_a = variables[key]) !== null && _a !== void 0 ? _a : "{{".concat(key, "}}");
145
+ });
146
+ };
147
+ if (cached.type === "text") {
148
+ return replace(cached.prompt);
149
+ }
150
+ return cached.prompt.map(function (msg) { return ({
151
+ content: replace(msg.content),
152
+ role: msg.role,
153
+ }); });
154
+ };
155
+ exports.compilePrompt = compilePrompt;
156
+ var createPrompt = function (params) { return __awaiter(void 0, void 0, void 0, function () {
157
+ var client, body, chatMessages, body;
158
+ var _a, _b, _c, _d;
159
+ return __generator(this, function (_e) {
160
+ switch (_e.label) {
161
+ case 0:
162
+ client = (0, langfuseClient_1.getLangfuseClient)();
163
+ if (!(params.type === "text")) return [3 /*break*/, 2];
164
+ body = {
165
+ config: params.config,
166
+ isActive: true,
167
+ labels: (_a = params.labels) !== null && _a !== void 0 ? _a : [DEFAULT_LABEL],
168
+ name: params.name,
169
+ prompt: params.prompt,
170
+ tags: (_b = params.tags) !== null && _b !== void 0 ? _b : [],
171
+ type: "text",
172
+ };
173
+ return [4 /*yield*/, client.createPrompt(body)];
174
+ case 1:
175
+ _e.sent();
176
+ return [3 /*break*/, 4];
177
+ case 2:
178
+ chatMessages = params.prompt.map(function (msg) { return ({
179
+ content: msg.content,
180
+ role: msg.role,
181
+ type: "chatmessage",
182
+ }); });
183
+ body = {
184
+ config: params.config,
185
+ isActive: true,
186
+ labels: (_c = params.labels) !== null && _c !== void 0 ? _c : [DEFAULT_LABEL],
187
+ name: params.name,
188
+ prompt: chatMessages,
189
+ tags: (_d = params.tags) !== null && _d !== void 0 ? _d : [],
190
+ type: "chat",
191
+ };
192
+ return [4 /*yield*/, client.createPrompt(body)];
193
+ case 3:
194
+ _e.sent();
195
+ _e.label = 4;
196
+ case 4: return [4 /*yield*/, (0, exports.invalidatePromptCache)(params.name)];
197
+ case 5:
198
+ _e.sent();
199
+ return [2 /*return*/, (0, exports.getPrompt)(params.name, { label: DEFAULT_LABEL })];
200
+ }
201
+ });
202
+ }); };
203
+ exports.createPrompt = createPrompt;
204
+ var invalidatePromptCache = function (name) { return __awaiter(void 0, void 0, void 0, function () {
205
+ return __generator(this, function (_a) {
206
+ switch (_a.label) {
207
+ case 0: return [4 /*yield*/, (0, langfuseCache_1.invalidateCache)("prompt:".concat(name, ":"))];
208
+ case 1:
209
+ _a.sent();
210
+ api_1.logger.info("Langfuse prompt cache invalidated for: ".concat(name));
211
+ return [2 /*return*/];
212
+ }
213
+ });
214
+ }); };
215
+ exports.invalidatePromptCache = invalidatePromptCache;
@@ -0,0 +1,3 @@
1
+ import type express from "express";
2
+ import type { ScoringFunction } from "./langfuseTypes";
3
+ export declare const addEvaluationRoutes: (router: express.Application, basePath: string, scoringFunctions?: ScoringFunction[]) => void;