@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
@@ -0,0 +1,83 @@
1
+ import {afterEach, beforeEach, describe, expect, it} from "bun:test";
2
+
3
+ import {getCached, invalidateCache, LangfuseCache, setCached} from "./langfuseCache";
4
+ import type {LangfuseCachedPrompt} from "./langfuseTypes";
5
+
6
+ const samplePrompt: LangfuseCachedPrompt = {
7
+ config: {},
8
+ labels: ["production"],
9
+ name: "test-prompt",
10
+ prompt: "Hello world",
11
+ tags: [],
12
+ type: "text",
13
+ version: 1,
14
+ };
15
+
16
+ describe("cache", () => {
17
+ beforeEach(async () => {
18
+ await LangfuseCache.deleteMany({});
19
+ });
20
+
21
+ afterEach(async () => {
22
+ await LangfuseCache.deleteMany({});
23
+ });
24
+
25
+ describe("getCached", () => {
26
+ it("returns null when no entry exists", async () => {
27
+ const result = await getCached("prompt:missing:production");
28
+ expect(result).toBeNull();
29
+ });
30
+
31
+ it("returns null for expired entries", async () => {
32
+ await setCached("prompt:test:production", samplePrompt, -1);
33
+ const result = await getCached("prompt:test:production");
34
+ expect(result).toBeNull();
35
+ });
36
+
37
+ it("returns the value for valid non-expired entries", async () => {
38
+ await setCached("prompt:test:production", samplePrompt, 60);
39
+ const result = await getCached("prompt:test:production");
40
+ expect(result).not.toBeNull();
41
+ expect(result?.name).toBe("test-prompt");
42
+ expect(result?.type).toBe("text");
43
+ });
44
+ });
45
+
46
+ describe("setCached", () => {
47
+ it("stores a new entry", async () => {
48
+ await setCached("prompt:test:production", samplePrompt, 60);
49
+ const count = await LangfuseCache.countDocuments({key: "prompt:test:production"});
50
+ expect(count).toBe(1);
51
+ });
52
+
53
+ it("upserts when the same key is set twice", async () => {
54
+ await setCached("prompt:test:production", samplePrompt, 60);
55
+ const updated: LangfuseCachedPrompt = {...samplePrompt, version: 2};
56
+ await setCached("prompt:test:production", updated, 60);
57
+
58
+ const count = await LangfuseCache.countDocuments({key: "prompt:test:production"});
59
+ expect(count).toBe(1);
60
+
61
+ const result = await getCached("prompt:test:production");
62
+ expect(result?.version).toBe(2);
63
+ });
64
+ });
65
+
66
+ describe("invalidateCache", () => {
67
+ it("removes entries matching the pattern", async () => {
68
+ await setCached("prompt:test-prompt:production", samplePrompt, 60);
69
+ await setCached("prompt:test-prompt:staging", {...samplePrompt, version: 2}, 60);
70
+ await setCached("prompt:other-prompt:production", {...samplePrompt, name: "other"}, 60);
71
+
72
+ await invalidateCache("prompt:test-prompt:");
73
+
74
+ const remaining = await LangfuseCache.find({}).lean();
75
+ expect(remaining.length).toBe(1);
76
+ expect(remaining[0].key).toBe("prompt:other-prompt:production");
77
+ });
78
+
79
+ it("does not throw when no entries match", async () => {
80
+ await expect(invalidateCache("prompt:nonexistent:")).resolves.toBeUndefined();
81
+ });
82
+ });
83
+ });
@@ -0,0 +1,58 @@
1
+ import mongoose from "mongoose";
2
+
3
+ import type {LangfuseCachedPrompt} from "./langfuseTypes";
4
+
5
+ interface LangfuseCacheDocument extends mongoose.Document {
6
+ key: string;
7
+ value: LangfuseCachedPrompt;
8
+ expiresAt: Date;
9
+ }
10
+
11
+ const langfuseCacheSchema = new mongoose.Schema<LangfuseCacheDocument>(
12
+ {
13
+ expiresAt: {
14
+ description: "When this cache entry expires",
15
+ required: true,
16
+ type: Date,
17
+ },
18
+ key: {
19
+ description: "Cache key (e.g. prompt:chat-assistant:production)",
20
+ required: true,
21
+ type: String,
22
+ unique: true,
23
+ },
24
+ value: {
25
+ description: "Cached Langfuse object",
26
+ required: true,
27
+ type: mongoose.Schema.Types.Mixed,
28
+ },
29
+ },
30
+ {strict: "throw", toJSON: {virtuals: true}, toObject: {virtuals: true}}
31
+ );
32
+
33
+ langfuseCacheSchema.index({expiresAt: 1}, {expireAfterSeconds: 0});
34
+
35
+ export const LangfuseCache =
36
+ (mongoose.models.LangfuseCache as mongoose.Model<LangfuseCacheDocument>) ||
37
+ mongoose.model<LangfuseCacheDocument>("LangfuseCache", langfuseCacheSchema);
38
+
39
+ export const getCached = async (key: string): Promise<LangfuseCachedPrompt | null> => {
40
+ const entry = await LangfuseCache.findOne({expiresAt: {$gt: new Date()}, key}).lean();
41
+ if (!entry) {
42
+ return null;
43
+ }
44
+ return entry.value as LangfuseCachedPrompt;
45
+ };
46
+
47
+ export const setCached = async (
48
+ key: string,
49
+ value: LangfuseCachedPrompt,
50
+ ttlSeconds: number
51
+ ): Promise<void> => {
52
+ const expiresAt = new Date(Date.now() + ttlSeconds * 1000);
53
+ await LangfuseCache.findOneAndUpdate({key}, {expiresAt, key, value}, {upsert: true});
54
+ };
55
+
56
+ export const invalidateCache = async (keyPattern: string): Promise<void> => {
57
+ await LangfuseCache.deleteMany({key: {$regex: keyPattern}});
58
+ };
@@ -0,0 +1,32 @@
1
+ import {Langfuse} from "langfuse";
2
+
3
+ import type {LangfuseAppOptions} from "./langfuseTypes";
4
+
5
+ let langfuseInstance: Langfuse | null = null;
6
+
7
+ export const initLangfuseClient = (options: LangfuseAppOptions): Langfuse => {
8
+ langfuseInstance = new Langfuse({
9
+ baseUrl: options.baseUrl ?? "https://cloud.langfuse.com",
10
+ flushAt: 1,
11
+ publicKey: options.publicKey,
12
+ secretKey: options.secretKey,
13
+ ...(options.projectId ? {_projectId: options.projectId} : {}),
14
+ });
15
+ return langfuseInstance;
16
+ };
17
+
18
+ export const getLangfuseClient = (): Langfuse => {
19
+ if (!langfuseInstance) {
20
+ throw new Error("Langfuse client not initialized. Call initLangfuseClient first.");
21
+ }
22
+ return langfuseInstance;
23
+ };
24
+
25
+ export const isLangfuseInitialized = (): boolean => langfuseInstance != null;
26
+
27
+ export const shutdownLangfuseClient = async (): Promise<void> => {
28
+ if (langfuseInstance) {
29
+ await langfuseInstance.flushAsync();
30
+ langfuseInstance = null;
31
+ }
32
+ };
@@ -0,0 +1,143 @@
1
+ import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
2
+
3
+ import {LangfuseCache} from "./langfuseCache";
4
+ import type {LangfuseCachedPrompt} from "./langfuseTypes";
5
+
6
+ const mockGetPromptStateless = mock(async (_name: string, _version?: number, _label?: string) => ({
7
+ data: {
8
+ config: {},
9
+ labels: ["production"],
10
+ name: "test-prompt",
11
+ prompt: "Hello world",
12
+ tags: [],
13
+ type: "text" as const,
14
+ version: 1,
15
+ },
16
+ fetchResult: "success" as const,
17
+ }));
18
+
19
+ mock.module("./langfuseClient", () => ({
20
+ getLangfuseClient: () => ({getPromptStateless: mockGetPromptStateless}),
21
+ }));
22
+
23
+ const {compilePrompt, getPrompt} = await import("./langfusePrompts");
24
+
25
+ const textPrompt: LangfuseCachedPrompt = {
26
+ config: {},
27
+ labels: ["production"],
28
+ name: "test-prompt",
29
+ prompt: "Hello {{name}}, you are {{age}} years old.",
30
+ tags: [],
31
+ type: "text",
32
+ version: 1,
33
+ };
34
+
35
+ const chatPrompt: LangfuseCachedPrompt = {
36
+ config: {},
37
+ labels: ["production"],
38
+ name: "chat-prompt",
39
+ prompt: [
40
+ {content: "You are a helpful assistant for {{company}}.", role: "system"},
41
+ {content: "My name is {{name}}.", role: "user"},
42
+ ],
43
+ tags: [],
44
+ type: "chat",
45
+ version: 1,
46
+ };
47
+
48
+ describe("compilePrompt", () => {
49
+ describe("text prompts", () => {
50
+ it("replaces variables in a text prompt", () => {
51
+ const result = compilePrompt(textPrompt, {age: "30", name: "Alice"});
52
+ expect(result).toBe("Hello Alice, you are 30 years old.");
53
+ });
54
+
55
+ it("leaves unreplaced variables as-is when not provided", () => {
56
+ const result = compilePrompt(textPrompt, {name: "Alice"});
57
+ expect(result).toBe("Hello Alice, you are {{age}} years old.");
58
+ });
59
+
60
+ it("returns prompt unchanged when no variables provided", () => {
61
+ const noVarPrompt: LangfuseCachedPrompt = {
62
+ ...textPrompt,
63
+ prompt: "Hello world.",
64
+ };
65
+ const result = compilePrompt(noVarPrompt);
66
+ expect(result).toBe("Hello world.");
67
+ });
68
+ });
69
+
70
+ describe("chat prompts", () => {
71
+ it("replaces variables in all chat messages", () => {
72
+ const result = compilePrompt(chatPrompt, {company: "Acme", name: "Bob"});
73
+ expect(Array.isArray(result)).toBe(true);
74
+ const messages = result as Array<{role: string; content: string}>;
75
+ expect(messages[0].content).toBe("You are a helpful assistant for Acme.");
76
+ expect(messages[1].content).toBe("My name is Bob.");
77
+ });
78
+
79
+ it("preserves roles in chat messages", () => {
80
+ const result = compilePrompt(chatPrompt, {company: "X", name: "Y"}) as Array<{
81
+ role: string;
82
+ content: string;
83
+ }>;
84
+ expect(result[0].role).toBe("system");
85
+ expect(result[1].role).toBe("user");
86
+ });
87
+ });
88
+ });
89
+
90
+ describe("getPrompt", () => {
91
+ beforeEach(async () => {
92
+ await LangfuseCache.deleteMany({});
93
+ mockGetPromptStateless.mockClear();
94
+ mockGetPromptStateless.mockImplementation(
95
+ async (_name: string, _version?: number, _label?: string) => ({
96
+ data: {
97
+ config: {},
98
+ labels: ["production"],
99
+ name: "test-prompt",
100
+ prompt: "Hello world",
101
+ tags: [],
102
+ type: "text" as const,
103
+ version: 1,
104
+ },
105
+ fetchResult: "success" as const,
106
+ })
107
+ );
108
+ });
109
+
110
+ afterEach(async () => {
111
+ await LangfuseCache.deleteMany({});
112
+ });
113
+
114
+ it("fetches from Langfuse when cache is empty", async () => {
115
+ const result = await getPrompt("test-prompt");
116
+ expect(result.name).toBe("test-prompt");
117
+ expect(result.type).toBe("text");
118
+ expect(mockGetPromptStateless).toHaveBeenCalledTimes(1);
119
+ });
120
+
121
+ it("returns cached result on second call", async () => {
122
+ await getPrompt("test-prompt");
123
+ await getPrompt("test-prompt");
124
+ expect(mockGetPromptStateless).toHaveBeenCalledTimes(1);
125
+ });
126
+
127
+ it("writes fetched prompt to cache", async () => {
128
+ await getPrompt("test-prompt");
129
+ const countAfter = await LangfuseCache.countDocuments({key: "prompt:test-prompt:production"});
130
+ expect(countAfter).toBe(1);
131
+ });
132
+
133
+ it("throws when Langfuse fetch fails", async () => {
134
+ mockGetPromptStateless.mockImplementation(async () => ({
135
+ data: {message: "Prompt not found"},
136
+ fetchResult: "failure" as const,
137
+ }));
138
+
139
+ await expect(getPrompt("missing-prompt")).rejects.toThrow(
140
+ 'Failed to fetch prompt "missing-prompt"'
141
+ );
142
+ });
143
+ });
@@ -0,0 +1,150 @@
1
+ import {logger} from "@terreno/api";
2
+ import type {
3
+ CreateChatPromptBody,
4
+ CreateTextPromptBody,
5
+ GetLangfusePromptSuccessData,
6
+ } from "langfuse-core";
7
+
8
+ import {getCached, invalidateCache, setCached} from "./langfuseCache";
9
+ import {getLangfuseClient} from "./langfuseClient";
10
+ import type {
11
+ ChatMessage,
12
+ GetPromptOptions,
13
+ LangfuseAppOptions,
14
+ LangfuseCachedPrompt,
15
+ } from "./langfuseTypes";
16
+
17
+ const DEFAULT_PROMPT_TTL = 60;
18
+
19
+ const DEFAULT_LABEL = "production";
20
+
21
+ const buildPromptCacheKey = (name: string, label?: string): string => {
22
+ return `prompt:${name}:${label ?? DEFAULT_LABEL}`;
23
+ };
24
+
25
+ const toLatestVersion = (data: GetLangfusePromptSuccessData): LangfuseCachedPrompt => {
26
+ const base = {
27
+ config: (data.config ?? {}) as Record<string, unknown>,
28
+ labels: data.labels ?? [],
29
+ name: data.name,
30
+ tags: data.tags ?? [],
31
+ version: data.version,
32
+ };
33
+
34
+ if (data.type === "chat") {
35
+ return {
36
+ ...base,
37
+ prompt: data.prompt as ChatMessage[],
38
+ type: "chat",
39
+ };
40
+ }
41
+
42
+ return {
43
+ ...base,
44
+ prompt: data.prompt as string,
45
+ type: "text",
46
+ };
47
+ };
48
+
49
+ export const getPrompt = async (
50
+ name: string,
51
+ options: GetPromptOptions = {},
52
+ appOptions?: Pick<LangfuseAppOptions, "cache">
53
+ ): Promise<LangfuseCachedPrompt> => {
54
+ const ttl = appOptions?.cache?.promptTtlSeconds ?? DEFAULT_PROMPT_TTL;
55
+ const cacheKey = buildPromptCacheKey(name, options.label);
56
+
57
+ const cached = await getCached(cacheKey);
58
+ if (cached) {
59
+ logger.debug(
60
+ `Langfuse prompt cache hit: "${name}" v${cached.version} (label: ${options.label ?? DEFAULT_LABEL})`
61
+ );
62
+ return cached;
63
+ }
64
+
65
+ logger.info(
66
+ `Langfuse prompt cache miss: fetching "${name}" from API (label: ${options.label ?? DEFAULT_LABEL})`
67
+ );
68
+ const client = getLangfuseClient();
69
+ const result = await client.getPromptStateless(name, undefined, options.label);
70
+
71
+ if (result.fetchResult !== "success") {
72
+ logger.warn(
73
+ `Langfuse prompt fetch failed: "${name}" — ${result.data.message ?? "unknown error"}`
74
+ );
75
+ throw new Error(`Failed to fetch prompt "${name}": ${result.data.message ?? "unknown error"}`);
76
+ }
77
+
78
+ const cachedPrompt = toLatestVersion(result.data);
79
+ logger.info(`Langfuse prompt fetched: "${name}" v${cachedPrompt.version} (ttl: ${ttl}s)`);
80
+ await setCached(cacheKey, cachedPrompt, ttl);
81
+ return cachedPrompt;
82
+ };
83
+
84
+ export const compilePrompt = (
85
+ cached: LangfuseCachedPrompt,
86
+ variables: Record<string, string> = {}
87
+ ): string | ChatMessage[] => {
88
+ const replace = (template: string): string => {
89
+ return template.replace(/\{\{(\w+)\}\}/g, (_match, key: string) => {
90
+ return variables[key] ?? `{{${key}}}`;
91
+ });
92
+ };
93
+
94
+ if (cached.type === "text") {
95
+ return replace(cached.prompt as string);
96
+ }
97
+
98
+ return (cached.prompt as ChatMessage[]).map((msg) => ({
99
+ content: replace(msg.content),
100
+ role: msg.role,
101
+ }));
102
+ };
103
+
104
+ export const createPrompt = async (params: {
105
+ name: string;
106
+ type: "text" | "chat";
107
+ prompt: string | ChatMessage[];
108
+ labels?: string[];
109
+ tags?: string[];
110
+ config?: Record<string, unknown>;
111
+ }): Promise<LangfuseCachedPrompt> => {
112
+ const client = getLangfuseClient();
113
+
114
+ if (params.type === "text") {
115
+ const body: CreateTextPromptBody = {
116
+ config: params.config,
117
+ isActive: true,
118
+ labels: params.labels ?? [DEFAULT_LABEL],
119
+ name: params.name,
120
+ prompt: params.prompt as string,
121
+ tags: params.tags ?? [],
122
+ type: "text",
123
+ };
124
+ await client.createPrompt(body);
125
+ } else {
126
+ const chatMessages = (params.prompt as ChatMessage[]).map((msg) => ({
127
+ content: msg.content,
128
+ role: msg.role,
129
+ type: "chatmessage" as const,
130
+ }));
131
+ const body: CreateChatPromptBody = {
132
+ config: params.config,
133
+ isActive: true,
134
+ labels: params.labels ?? [DEFAULT_LABEL],
135
+ name: params.name,
136
+ prompt: chatMessages,
137
+ tags: params.tags ?? [],
138
+ type: "chat",
139
+ };
140
+ await client.createPrompt(body);
141
+ }
142
+
143
+ await invalidatePromptCache(params.name);
144
+ return getPrompt(params.name, {label: DEFAULT_LABEL});
145
+ };
146
+
147
+ export const invalidatePromptCache = async (name: string): Promise<void> => {
148
+ await invalidateCache(`prompt:${name}:`);
149
+ logger.info(`Langfuse prompt cache invalidated for: ${name}`);
150
+ };
@@ -0,0 +1,143 @@
1
+ import {beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
2
+ import {createdUpdatedPlugin, setupServer} from "@terreno/api";
3
+ import mongoose from "mongoose";
4
+ import passportLocalMongoose from "passport-local-mongoose";
5
+ import supertest from "supertest";
6
+
7
+ mock.module("./langfuseClient", () => ({
8
+ getLangfuseClient: () => ({
9
+ api: {
10
+ promptsList: async () => ({data: [], meta: {limit: 20, page: 1, total: 0, totalPages: 0}}),
11
+ },
12
+ fetchTrace: async () => ({data: null}),
13
+ fetchTraces: async () => ({data: [], meta: {limit: 20, page: 1, total: 0, totalPages: 0}}),
14
+ flushAsync: async () => {},
15
+ score: () => {},
16
+ }),
17
+ }));
18
+
19
+ const {addPromptRoutes} = await import("./langfuseRoutesPrompts");
20
+ const {addTraceRoutes} = await import("./langfuseRoutesTraces");
21
+ const {addPlaygroundRoutes} = await import("./langfuseRoutesPlayground");
22
+ const {addEvaluationRoutes} = await import("./langfuseRoutesEvaluations");
23
+
24
+ const userSchema = new mongoose.Schema({
25
+ admin: {default: false, type: Boolean},
26
+ email: {index: true, type: String},
27
+ name: String,
28
+ });
29
+ userSchema.plugin(passportLocalMongoose as Parameters<typeof userSchema.plugin>[0], {
30
+ usernameField: "email",
31
+ });
32
+ userSchema.plugin(createdUpdatedPlugin);
33
+
34
+ const UserModel = mongoose.models.User || mongoose.model("User", userSchema);
35
+
36
+ const authAsUser = async (
37
+ appInstance: Parameters<typeof supertest>[0],
38
+ type: "admin" | "notAdmin"
39
+ ) => {
40
+ const email = type === "admin" ? "lf-admin@example.com" : "lf-user@example.com";
41
+ const password = type === "admin" ? "adminPass123" : "userPass123";
42
+ const agent = supertest.agent(appInstance);
43
+ const res = await agent.post("/auth/login").send({email, password}).expect(200);
44
+ await agent.set("authorization", `Bearer ${res.body.data.token}`);
45
+ return agent;
46
+ };
47
+
48
+ let app: ReturnType<typeof setupServer>;
49
+
50
+ describe("Langfuse routes", () => {
51
+ beforeAll(async () => {
52
+ await UserModel.deleteMany({email: {$in: ["lf-admin@example.com", "lf-user@example.com"]}});
53
+
54
+ const admin = await UserModel.create({
55
+ admin: true,
56
+ email: "lf-admin@example.com",
57
+ name: "LF Admin",
58
+ });
59
+ await (admin as {setPassword: (p: string) => Promise<void>}).setPassword("adminPass123");
60
+ await admin.save();
61
+
62
+ const user = await UserModel.create({email: "lf-user@example.com", name: "LF User"});
63
+ await (user as {setPassword: (p: string) => Promise<void>}).setPassword("userPass123");
64
+ await user.save();
65
+ });
66
+
67
+ beforeEach(() => {
68
+ app = setupServer({
69
+ addRoutes: (router) => {
70
+ addPromptRoutes(router, "/admin/langfuse");
71
+ addTraceRoutes(router, "/admin/langfuse");
72
+ addPlaygroundRoutes(router, "/admin/langfuse");
73
+ addEvaluationRoutes(router, "/admin/langfuse", []);
74
+ },
75
+ skipListen: true,
76
+ userModel: UserModel as Parameters<typeof setupServer>[0]["userModel"],
77
+ });
78
+ });
79
+
80
+ describe("prompt routes", () => {
81
+ it("returns 403 for non-admin users", async () => {
82
+ const agent = await authAsUser(app, "notAdmin");
83
+ const res = await agent.get("/admin/langfuse/prompts");
84
+ expect(res.status).toBe(403);
85
+ });
86
+
87
+ it("returns non-403 for admin users", async () => {
88
+ const agent = await authAsUser(app, "admin");
89
+ const res = await agent.get("/admin/langfuse/prompts");
90
+ expect(res.status).not.toBe(403);
91
+ });
92
+ });
93
+
94
+ describe("trace routes", () => {
95
+ it("returns 403 for non-admin users", async () => {
96
+ const agent = await authAsUser(app, "notAdmin");
97
+ const res = await agent.get("/admin/langfuse/traces");
98
+ expect(res.status).toBe(403);
99
+ });
100
+
101
+ it("returns non-403 for admin users", async () => {
102
+ const agent = await authAsUser(app, "admin");
103
+ const res = await agent.get("/admin/langfuse/traces");
104
+ expect(res.status).not.toBe(403);
105
+ });
106
+ });
107
+
108
+ describe("playground routes", () => {
109
+ it("returns 403 for non-admin users", async () => {
110
+ const agent = await authAsUser(app, "notAdmin");
111
+ const res = await agent.post("/admin/langfuse/playground").send({promptName: "test"});
112
+ expect(res.status).toBe(403);
113
+ });
114
+
115
+ it("returns 400 for admin users when promptName is missing", async () => {
116
+ const agent = await authAsUser(app, "admin");
117
+ const res = await agent.post("/admin/langfuse/playground").send({});
118
+ expect(res.status).not.toBe(403);
119
+ });
120
+ });
121
+
122
+ describe("evaluation routes", () => {
123
+ it("returns 403 for non-admin users on config", async () => {
124
+ const agent = await authAsUser(app, "notAdmin");
125
+ const res = await agent.get("/admin/langfuse/evaluations/config");
126
+ expect(res.status).toBe(403);
127
+ });
128
+
129
+ it("returns non-403 for admin users on config", async () => {
130
+ const agent = await authAsUser(app, "admin");
131
+ const res = await agent.get("/admin/langfuse/evaluations/config");
132
+ expect(res.status).not.toBe(403);
133
+ });
134
+
135
+ it("returns 403 for non-admin users on submit", async () => {
136
+ const agent = await authAsUser(app, "notAdmin");
137
+ const res = await agent
138
+ .post("/admin/langfuse/evaluations")
139
+ .send({name: "score", traceId: "t1", value: 1});
140
+ expect(res.status).toBe(403);
141
+ });
142
+ });
143
+ });
@@ -0,0 +1,48 @@
1
+ import {APIError, asyncHandler, authenticateMiddleware} from "@terreno/api";
2
+ import type express from "express";
3
+
4
+ import {getLangfuseClient} from "./langfuseClient";
5
+ import {requireAdmin} from "./langfuseRoutesMiddleware";
6
+ import type {ScoreSubmission, ScoringFunction} from "./langfuseTypes";
7
+
8
+ export const addEvaluationRoutes = (
9
+ router: express.Application,
10
+ basePath: string,
11
+ scoringFunctions: ScoringFunction[] = []
12
+ ): void => {
13
+ router.get(
14
+ `${basePath}/evaluations/config`,
15
+ [authenticateMiddleware(), requireAdmin],
16
+ asyncHandler(async (_req, res) => {
17
+ return res.json({scoringFunctions});
18
+ })
19
+ );
20
+
21
+ router.post(
22
+ `${basePath}/evaluations`,
23
+ [authenticateMiddleware(), requireAdmin],
24
+ asyncHandler(async (req, res) => {
25
+ const {traceId, name, value, comment, dataType} = req.body as ScoreSubmission;
26
+
27
+ if (!traceId || !name || value === undefined) {
28
+ throw new APIError({
29
+ detail: "traceId, name, and value are required",
30
+ status: 400,
31
+ title: "Invalid request",
32
+ });
33
+ }
34
+
35
+ const client = getLangfuseClient();
36
+ client.score({
37
+ comment: comment ?? null,
38
+ dataType: dataType as "NUMERIC" | "CATEGORICAL" | "BOOLEAN" | undefined,
39
+ name,
40
+ traceId,
41
+ value,
42
+ });
43
+
44
+ await client.flushAsync();
45
+ return res.status(201).json({comment, dataType, name, traceId, value});
46
+ })
47
+ );
48
+ };
@@ -0,0 +1,12 @@
1
+ import {APIError, Permissions} from "@terreno/api";
2
+ import type {NextFunction, Request, Response} from "express";
3
+
4
+ export const requireAdmin = (req: Request, _res: Response, next: NextFunction): void => {
5
+ const user = req.user as Parameters<typeof Permissions.IsAdmin>[1] & {isAdmin?: () => boolean};
6
+ const isAdmin =
7
+ typeof user?.isAdmin === "function" ? user.isAdmin() : Permissions.IsAdmin("read", user);
8
+ if (!isAdmin) {
9
+ throw new APIError({disableExternalErrorTracking: true, status: 403, title: "Forbidden"});
10
+ }
11
+ next();
12
+ };