@terreno/ai 0.12.2 → 0.13.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.
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "@langfuse/client": "^5.0.1",
10
10
  "@langfuse/otel": "^5.0.1",
11
11
  "@opentelemetry/api": "^1.9.0",
12
- "@opentelemetry/sdk-node": "^0.213.0",
12
+ "@opentelemetry/sdk-node": "0.213.0",
13
13
  "ai": "^6.0.97",
14
14
  "multer": "^1.4.5-lts.1"
15
15
  },
@@ -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.12.2",
25
+ "@terreno/api": "0.13.1",
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.12.2",
66
+ "@terreno/api": "0.13.1",
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.12.2"
85
+ "version": "0.13.1"
86
86
  }
@@ -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", () => {