@terreno/ai 0.13.0 → 0.13.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "@types/multer": "^1.4.12",
23
23
  "@types/passport-local-mongoose": "^6.1.5",
24
24
  "@types/supertest": "^6.0.2",
25
- "@terreno/api": "0.13.0",
25
+ "@terreno/api": "0.13.3",
26
26
  "mongoose": "8.23.0",
27
27
  "passport-local-mongoose": "^9.0.1",
28
28
  "supertest": "^7.0.0",
@@ -63,7 +63,7 @@
63
63
  "access": "public"
64
64
  },
65
65
  "peerDependencies": {
66
- "@terreno/api": "0.13.0",
66
+ "@terreno/api": "0.13.3",
67
67
  "mongoose": "^8.0.0"
68
68
  },
69
69
  "repository": {
@@ -82,5 +82,5 @@
82
82
  "test:coverage": "bun run ../scripts/check-coverage.ts"
83
83
  },
84
84
  "types": "dist/index.d.ts",
85
- "version": "0.13.0"
85
+ "version": "0.13.3"
86
86
  }
@@ -1,4 +1,4 @@
1
- import {afterEach, describe, expect, it, mock} from "bun:test";
1
+ import {afterEach, describe, expect, it} from "bun:test";
2
2
 
3
3
  import {
4
4
  getLangfuseClient,
@@ -7,23 +7,7 @@ import {
7
7
  shutdownLangfuseClient,
8
8
  } from "./langfuseClient";
9
9
 
10
- mock.module("@langfuse/client", () => {
11
- return {
12
- LangfuseClient: class MockLangfuseClient {
13
- baseUrl: string;
14
- publicKey: string;
15
- secretKey: string;
16
-
17
- constructor(opts: {baseUrl: string; publicKey: string; secretKey: string}) {
18
- this.baseUrl = opts.baseUrl;
19
- this.publicKey = opts.publicKey;
20
- this.secretKey = opts.secretKey;
21
- }
22
-
23
- shutdown = mock(async () => {});
24
- },
25
- };
26
- });
10
+ // @langfuse/client is mocked globally in tests/bunSetup.ts.
27
11
 
28
12
  describe("langfuseClient", () => {
29
13
  afterEach(async () => {
@@ -1,6 +1,8 @@
1
1
  import {beforeEach, describe, expect, it, mock} from "bun:test";
2
2
 
3
3
  import {LangfuseCache} from "./langfuseCache";
4
+ import {getLangfuseClient, initLangfuseClient} from "./langfuseClient";
5
+ import {compilePrompt, createPrompt, getPrompt, invalidatePromptCache} from "./langfusePrompts";
4
6
  import type {LangfuseCachedPrompt} from "./langfuseTypes";
5
7
 
6
8
  const mockPromptCreate = mock(async (_params: Record<string, unknown>) => {});
@@ -14,16 +16,6 @@ const mockPromptGet = mock(async (_name: string, _options?: Record<string, unkno
14
16
  version: 1,
15
17
  }));
16
18
 
17
- const realLangfuseClient = await import("./langfuseClient");
18
- mock.module("./langfuseClient", () => ({
19
- ...realLangfuseClient,
20
- getLangfuseClient: () => ({prompt: {create: mockPromptCreate, get: mockPromptGet}}),
21
- }));
22
-
23
- const {compilePrompt, createPrompt, getPrompt, invalidatePromptCache} = await import(
24
- "./langfusePrompts"
25
- );
26
-
27
19
  const textPrompt: LangfuseCachedPrompt = {
28
20
  config: {},
29
21
  labels: ["production"],
@@ -60,6 +52,15 @@ beforeEach(async () => {
60
52
  type: "text" as const,
61
53
  version: 1,
62
54
  }));
55
+ // Init the (fake) langfuse client and wire our local mocks onto its prompt
56
+ // methods so the real `langfusePrompts` code exercises customizable
57
+ // responses without hitting the network.
58
+ initLangfuseClient({publicKey: "pk-test", secretKey: "sk-test"});
59
+ const client = getLangfuseClient() as unknown as {
60
+ prompt: {create: typeof mockPromptCreate; get: typeof mockPromptGet};
61
+ };
62
+ client.prompt.get = mockPromptGet;
63
+ client.prompt.create = mockPromptCreate;
63
64
  });
64
65
 
65
66
  describe("compilePrompt", () => {
@@ -4,6 +4,12 @@ import mongoose from "mongoose";
4
4
  import passportLocalMongoose from "passport-local-mongoose";
5
5
  import supertest from "supertest";
6
6
 
7
+ import {getLangfuseClient, initLangfuseClient} from "./langfuseClient";
8
+ import {addEvaluationRoutes} from "./langfuseRoutesEvaluations";
9
+ import {addPlaygroundRoutes} from "./langfuseRoutesPlayground";
10
+ import {addPromptRoutes} from "./langfuseRoutesPrompts";
11
+ import {addTraceRoutes} from "./langfuseRoutesTraces";
12
+
7
13
  const scoreCreate = mock(() => {});
8
14
  const promptCreate = mock(async (_params: Record<string, unknown>) => ({}));
9
15
  const promptGet = mock(async (name: string) => ({
@@ -26,25 +32,6 @@ const traceList = mock(async () => ({
26
32
  const traceGet = mock(async (traceId: string) => ({id: traceId, name: "Trace"}));
27
33
  const flush = mock(async () => {});
28
34
 
29
- const realLangfuseClient = await import("./langfuseClient");
30
- mock.module("./langfuseClient", () => ({
31
- ...realLangfuseClient,
32
- getLangfuseClient: () => ({
33
- api: {
34
- prompts: {list: promptsList},
35
- trace: {get: traceGet, list: traceList},
36
- },
37
- flush,
38
- prompt: {create: promptCreate, get: promptGet},
39
- score: {create: scoreCreate},
40
- }),
41
- }));
42
-
43
- const {addPromptRoutes} = await import("./langfuseRoutesPrompts");
44
- const {addTraceRoutes} = await import("./langfuseRoutesTraces");
45
- const {addPlaygroundRoutes} = await import("./langfuseRoutesPlayground");
46
- const {addEvaluationRoutes} = await import("./langfuseRoutesEvaluations");
47
-
48
35
  const userSchema = new mongoose.Schema({
49
36
  admin: {default: false, type: Boolean},
50
37
  email: {index: true, type: String},
@@ -89,6 +76,27 @@ describe("Langfuse routes", () => {
89
76
  });
90
77
 
91
78
  beforeEach(() => {
79
+ // Init the (fake) langfuse client and wire our local mocks onto its
80
+ // methods so the route handlers exercise customizable responses without
81
+ // making real SDK calls.
82
+ initLangfuseClient({publicKey: "pk-test", secretKey: "sk-test"});
83
+ const client = getLangfuseClient() as unknown as {
84
+ api: {
85
+ prompts: {list: typeof promptsList};
86
+ trace: {get: typeof traceGet; list: typeof traceList};
87
+ };
88
+ flush: typeof flush;
89
+ prompt: {create: typeof promptCreate; get: typeof promptGet};
90
+ score: {create: typeof scoreCreate};
91
+ };
92
+ client.api.prompts.list = promptsList;
93
+ client.api.trace.get = traceGet;
94
+ client.api.trace.list = traceList;
95
+ client.flush = flush;
96
+ client.prompt.create = promptCreate;
97
+ client.prompt.get = promptGet;
98
+ client.score.create = scoreCreate;
99
+
92
100
  app = setupServer({
93
101
  addRoutes: (router) => {
94
102
  addPromptRoutes(router, "/admin/langfuse");
@@ -1,6 +1,8 @@
1
1
  import {beforeEach, describe, expect, it, mock} from "bun:test";
2
2
 
3
3
  import {LangfuseCache} from "./langfuseCache";
4
+ import {getLangfuseClient, initLangfuseClient} from "./langfuseClient";
5
+ import {createTelemetryConfig, preparePromptForAI} from "./langfuseVercelAi";
4
6
 
5
7
  const mockPromptGet = mock(async (_name: string, _options?: Record<string, unknown>) => ({
6
8
  config: {temperature: 0.5},
@@ -12,22 +14,16 @@ const mockPromptGet = mock(async (_name: string, _options?: Record<string, unkno
12
14
  version: 3,
13
15
  }));
14
16
 
15
- // Mock the langfuse SDK client so the real `getPrompt`/`compilePrompt` from
16
- // `./langfusePrompts` can be exercised without network access. We intentionally
17
- // do NOT mock `./langfusePrompts` directly because that would leak into other
18
- // test suites that share the module cache in the same bun process.
19
- const realLangfuseClient = await import("./langfuseClient");
20
- mock.module("./langfuseClient", () => ({
21
- ...realLangfuseClient,
22
- getLangfuseClient: () => ({prompt: {get: mockPromptGet}}),
23
- }));
24
-
25
- const {createTelemetryConfig, preparePromptForAI} = await import("./langfuseVercelAi");
26
-
27
17
  describe("langfuseVercelAi", () => {
28
18
  beforeEach(async () => {
29
19
  mockPromptGet.mockClear();
30
20
  await LangfuseCache.deleteMany({});
21
+ // Re-init the (fake) langfuse client and wire the prompt.get mock so the
22
+ // real `getPrompt`/`compilePrompt` from `./langfusePrompts` runs against
23
+ // a customizable response without network access.
24
+ initLangfuseClient({publicKey: "pk-test", secretKey: "sk-test"});
25
+ (getLangfuseClient() as unknown as {prompt: {get: typeof mockPromptGet}}).prompt.get =
26
+ mockPromptGet;
31
27
  });
32
28
 
33
29
  describe("preparePromptForAI", () => {
@@ -1119,6 +1119,112 @@ describe("AI Routes", () => {
1119
1119
  AIRequest.logRequest = originalLogRequest;
1120
1120
  }
1121
1121
  });
1122
+
1123
+ it("swallows preparePromptForAI failure when langfuse prompt loading throws", async () => {
1124
+ // Re-mock preparePromptForAI to throw, exercising the catch branch that
1125
+ // logs `Langfuse system prompt skipped: ...` and continues without it.
1126
+ mock.module("../langfuseVercelAi", () => ({
1127
+ ...realLangfuseVercelAi,
1128
+ createTelemetryConfig: () => ({functionId: "test", isEnabled: false}),
1129
+ preparePromptForAI: async () => {
1130
+ throw new Error("langfuse offline");
1131
+ },
1132
+ }));
1133
+ try {
1134
+ const customApp = setupServer({
1135
+ addRoutes: (router, options) => {
1136
+ addGptRoutes(router, {
1137
+ aiService,
1138
+ langfuseSystemPromptName: "broken-prompt",
1139
+ openApiOptions: options,
1140
+ });
1141
+ },
1142
+ skipListen: true,
1143
+ userModel: UserModel,
1144
+ });
1145
+ const agent = await authAsUser(customApp, "notAdmin");
1146
+ const res = await agent
1147
+ .post("/gpt/prompt")
1148
+ .send({prompt: "Hi"})
1149
+ .buffer(true)
1150
+ .parse(sseCollect);
1151
+ // Failure is silently caught; request still completes successfully.
1152
+ expect(res.status).toBe(200);
1153
+ const body = (res as SseResponse).body;
1154
+ expect(body).toContain("done");
1155
+ } finally {
1156
+ // Restore the original happy-path mock for subsequent tests.
1157
+ mock.module("../langfuseVercelAi", () => ({
1158
+ ...realLangfuseVercelAi,
1159
+ createTelemetryConfig: () => ({functionId: "test", isEnabled: false}),
1160
+ preparePromptForAI: async () => ({
1161
+ config: {},
1162
+ prompt: "test",
1163
+ telemetry: {isEnabled: false},
1164
+ }),
1165
+ }));
1166
+ }
1167
+ });
1168
+
1169
+ it("logs warning when AIRequest.logRequest throws inside the stream error catch", async () => {
1170
+ // Combine a stream that errors mid-iteration with a logRequest that also throws,
1171
+ // exercising the catch-within-catch that logs `Failed to log AIRequest error`.
1172
+ const streamIterationErrorModel = {
1173
+ doGenerate: mock(async () => ({
1174
+ content: [{text: "ok", type: "text" as const}],
1175
+ finishReason: "stop" as const,
1176
+ usage: {inputTokens: 1, outputTokens: 1},
1177
+ })),
1178
+ doStream: mock(async () => ({
1179
+ stream: new ReadableStream({
1180
+ start(controller) {
1181
+ controller.enqueue({id: "t1", type: "text-start" as const});
1182
+ controller.enqueue({
1183
+ delta: "partial",
1184
+ id: "t1",
1185
+ type: "text-delta" as const,
1186
+ });
1187
+ controller.enqueue({id: "t1", type: "text-end" as const});
1188
+ controller.error(new Error("stream iteration failure"));
1189
+ },
1190
+ }),
1191
+ })),
1192
+ modelId: "stream-iter-error",
1193
+ provider: "mock-provider",
1194
+ specificationVersion: "v2" as const,
1195
+ supportedUrls: {},
1196
+ };
1197
+ const errApp = setupServer({
1198
+ addRoutes: (router, options) => {
1199
+ addGptRoutes(router, {
1200
+ aiService: new AIService({
1201
+ model: streamIterationErrorModel as unknown as LanguageModel,
1202
+ }),
1203
+ openApiOptions: options,
1204
+ });
1205
+ },
1206
+ skipListen: true,
1207
+ userModel: UserModel,
1208
+ });
1209
+ const originalLogRequest = AIRequest.logRequest;
1210
+ AIRequest.logRequest = mock(async () => {
1211
+ throw new Error("log write failed");
1212
+ }) as unknown as typeof AIRequest.logRequest;
1213
+ try {
1214
+ const agent = await authAsUser(errApp, "notAdmin");
1215
+ const res = await agent
1216
+ .post("/gpt/prompt")
1217
+ .send({prompt: "Hi"})
1218
+ .buffer(true)
1219
+ .parse(sseCollect);
1220
+ // Failure path still flushes an SSE error event and ends the response.
1221
+ expect(res.status).toBe(200);
1222
+ const body = (res as SseResponse).body;
1223
+ expect(body).toContain("error");
1224
+ } finally {
1225
+ AIRequest.logRequest = originalLogRequest;
1226
+ }
1227
+ });
1122
1228
  });
1123
1229
 
1124
1230
  describe("GPT tools route", () => {
@@ -99,6 +99,65 @@ afterEach(() => {
99
99
  logs = [];
100
100
  });
101
101
 
102
+ // Mock @langfuse/client globally so the real `./langfuseClient` module runs
103
+ // without making network calls. Each `new LangfuseClient(...)` gets its own
104
+ // mock functions so tests can customize per-instance behavior (via
105
+ // `getLangfuseClient()` after `initLangfuseClient(...)`). Mocking the SDK
106
+ // here, rather than mocking `./langfuseClient` per-test-file, prevents the
107
+ // `mock.module()` global cache from leaking different shapes across files.
108
+ mock.module("@langfuse/client", () => {
109
+ return {
110
+ LangfuseClient: class FakeLangfuseClient {
111
+ baseUrl: string;
112
+ publicKey: string;
113
+ secretKey: string;
114
+ api = {
115
+ prompts: {
116
+ list: mock(async () => ({
117
+ data: [],
118
+ meta: {limit: 20, page: 1, total: 0, totalPages: 0},
119
+ })),
120
+ },
121
+ trace: {
122
+ get: mock(async (id: string) => ({id, name: "Trace"})),
123
+ list: mock(async () => ({
124
+ data: [],
125
+ meta: {limit: 20, page: 1, total: 0, totalPages: 0},
126
+ })),
127
+ },
128
+ };
129
+ prompt = {
130
+ create: mock(async (_params: Record<string, unknown>) => undefined),
131
+ get: mock(async (name: string) => ({
132
+ config: {},
133
+ labels: [],
134
+ name,
135
+ prompt: "",
136
+ tags: [],
137
+ type: "text" as const,
138
+ version: 1,
139
+ })),
140
+ };
141
+ score = {create: mock(() => {})};
142
+ flush = mock(async () => {});
143
+ shutdown = mock(async () => {});
144
+
145
+ constructor(opts: {baseUrl: string; publicKey: string; secretKey: string}) {
146
+ this.baseUrl = opts.baseUrl;
147
+ this.publicKey = opts.publicKey;
148
+ this.secretKey = opts.secretKey;
149
+ }
150
+ },
151
+ };
152
+ });
153
+
154
+ // Ensure no langfuse client instance leaks across tests in different files.
155
+ // The real langfuseInstance module variable is reset to null between tests.
156
+ beforeEach(async () => {
157
+ const {shutdownLangfuseClient} = await import("../langfuseClient");
158
+ await shutdownLangfuseClient();
159
+ });
160
+
102
161
  // Mock @sentry/bun module
103
162
  mock.module("@sentry/bun", () => {
104
163
  const mockFn = (): ReturnType<typeof mock> => mock(() => {});