@terreno/ai 57.20.0 → 58.0.0-beta.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 (97) hide show
  1. package/AGENTS.md +20 -18
  2. package/CHANGELOG.md +93 -0
  3. package/biome.jsonc +3 -2
  4. package/dist/aiApp.d.ts +5 -2
  5. package/dist/aiApp.js +3 -2
  6. package/dist/harness/agentLoop.js +1 -0
  7. package/dist/index.d.ts +2 -1
  8. package/dist/index.js +6 -2
  9. package/dist/migrations/gptHistoryStringIds.d.ts +41 -0
  10. package/dist/migrations/gptHistoryStringIds.js +94 -0
  11. package/dist/models/aiDataset.js +2 -2
  12. package/dist/models/gptHistory.js +11 -0
  13. package/dist/observability/local/experimentRunner.d.ts +2 -5
  14. package/dist/routes/files.d.ts +7 -1
  15. package/dist/routes/files.js +62 -13
  16. package/dist/routes/gptActions.js +0 -7
  17. package/dist/routes/gptHistories.js +8 -2
  18. package/dist/service/aiDatasets.js +2 -1
  19. package/dist/service/chatTurn.js +4 -0
  20. package/dist/service/vertex.d.ts +5 -2
  21. package/dist/types/harness.d.ts +5 -2
  22. package/dist/types/index.d.ts +5 -2
  23. package/e2e/smoke.ts +227 -0
  24. package/package.json +19 -16
  25. package/src/aiApp.test.ts +43 -18
  26. package/src/aiApp.ts +6 -3
  27. package/src/harness/agentLoop.test.ts +1 -1
  28. package/src/harness/agentLoop.ts +2 -1
  29. package/src/harness/agentRecovery.test.ts +31 -28
  30. package/src/harness/approvals.test.ts +11 -9
  31. package/src/harness/events.test.ts +10 -6
  32. package/src/harness/extensions.test.ts +7 -6
  33. package/src/harness/ownership.test.ts +20 -9
  34. package/src/harness/retries.test.ts +6 -3
  35. package/src/harness/routes/conversations.test.ts +2 -2
  36. package/src/harness/routes/tasks.test.ts +2 -2
  37. package/src/harness/runners/jobsRunner.test.ts +1 -1
  38. package/src/harness/subagent.test.ts +3 -2
  39. package/src/harness/waits.test.ts +6 -4
  40. package/src/index.test.ts +3 -3
  41. package/src/index.ts +7 -1
  42. package/src/isolated/gptStream.isolated.ts +35 -30
  43. package/src/isolated/langfuseClient.isolated.ts +9 -3
  44. package/src/isolated/lazyProjectModel.isolated.ts +2 -2
  45. package/src/langfuseApp.test.ts +4 -20
  46. package/src/langfuseRoutes.test.ts +20 -15
  47. package/src/langfuseTracing.test.ts +6 -2
  48. package/src/langfuseVercelAi.test.ts +12 -9
  49. package/src/migrations/gptHistoryStringIds.test.ts +231 -0
  50. package/src/migrations/gptHistoryStringIds.ts +150 -0
  51. package/src/models/aiDataset.test.ts +1 -1
  52. package/src/models/aiDataset.ts +2 -2
  53. package/src/models/gptHistory.test.ts +22 -11
  54. package/src/models/gptHistory.ts +19 -1
  55. package/src/observability/evaluate.test.ts +12 -10
  56. package/src/observability/local/experimentRunner.test.ts +9 -12
  57. package/src/observability/local/experimentRunner.ts +2 -2
  58. package/src/observability/local/promptStore.test.ts +4 -3
  59. package/src/observability/routes/datasets.test.ts +2 -2
  60. package/src/observability/routes/evaluators.test.ts +2 -2
  61. package/src/observability/routes/experiments.test.ts +2 -2
  62. package/src/observability/routes/prompts.test.ts +3 -3
  63. package/src/observability/routes/review.test.ts +2 -2
  64. package/src/observability/routes/status.test.ts +4 -4
  65. package/src/observability/routes/testMultiStage.test.ts +6 -6
  66. package/src/observability/routes/traces.test.ts +2 -2
  67. package/src/routes/files.test.ts +126 -3
  68. package/src/routes/files.ts +90 -15
  69. package/src/routes/gpt.test.ts +23 -16
  70. package/src/routes/gptActions.test.ts +15 -15
  71. package/src/routes/gptActions.ts +1 -4
  72. package/src/routes/gptApprovals.test.ts +1 -1
  73. package/src/routes/gptDatasets.test.ts +1 -1
  74. package/src/routes/gptFiles.test.ts +2 -2
  75. package/src/routes/gptHistories.test.ts +17 -11
  76. package/src/routes/gptHistories.ts +59 -55
  77. package/src/routes/gptHistoriesSync.test.ts +141 -0
  78. package/src/routes/mcp.test.ts +3 -3
  79. package/src/routes/projects.test.ts +5 -3
  80. package/src/routes/routes.test.ts +64 -47
  81. package/src/service/aiDatasets.ts +2 -1
  82. package/src/service/aiService.test.ts +1 -1
  83. package/src/service/asks.test.ts +14 -6
  84. package/src/service/chatTurn.ts +4 -0
  85. package/src/service/fileStorage.test.ts +5 -2
  86. package/src/service/getMCPTools.test.ts +9 -5
  87. package/src/service/mcpService.test.ts +6 -3
  88. package/src/service/vertex.test.ts +1 -1
  89. package/src/service/vertex.ts +7 -4
  90. package/src/tests/bunSetup.ts +1 -1
  91. package/src/tests/chatHarness.ts +4 -4
  92. package/src/tests/fakeJudge.ts +8 -0
  93. package/src/tests/fakeLangfusePrompt.ts +10 -0
  94. package/src/tests/helpers.ts +26 -4
  95. package/src/types/harness.ts +6 -2
  96. package/src/types/index.ts +5 -2
  97. package/tsconfig.typecheck.json +18 -0
@@ -17,7 +17,14 @@ import type {ObservabilityPlugin} from "../observability/types";
17
17
  import {AIService} from "../service/aiService";
18
18
  import type {FileStorageService} from "../service/fileStorage";
19
19
  import type {MCPService} from "../service/mcpService";
20
- import {authAsUser, ensureTestUsers, UserModel} from "../tests/helpers";
20
+ import {
21
+ authAsUser,
22
+ ensureTestUsers,
23
+ historyRouterOptions,
24
+ terrenoUserModel,
25
+ UserModel,
26
+ } from "../tests/helpers";
27
+ import type {FileContentPart, ImageContentPart} from "../types/index";
21
28
  import {addAiRequestsExplorerRoutes} from "./aiRequestsExplorer";
22
29
  import {addGptHistoryRoutes} from "./gptHistories";
23
30
 
@@ -25,14 +32,15 @@ type SseResponse = supertest.Response & {body: string};
25
32
 
26
33
  // Mock langfuseVercelAi to avoid transitive langfuse SDK import in tests.
27
34
  // Spread the real module so later-loaded test files still see the real exports.
28
- const realLangfuseVercelAi = await import("../langfuseVercelAi");
35
+ // The package compiles as CommonJS, so modules load with require instead of top-level await.
36
+ const realLangfuseVercelAi: typeof import("../langfuseVercelAi.js") = require("../langfuseVercelAi");
29
37
  mock.module("../langfuseVercelAi", () => ({
30
38
  ...realLangfuseVercelAi,
31
39
  createTelemetryConfig: () => ({functionId: "test", isEnabled: false}),
32
40
  preparePromptForAI: async () => ({config: {}, prompt: "test", telemetry: {isEnabled: false}}),
33
41
  }));
34
42
 
35
- const realAi = await import("ai");
43
+ const realAi: typeof import("ai") = require("ai");
36
44
  type StreamTextOptions = Parameters<typeof realAi.streamText>[0];
37
45
  type StreamTextResult = ReturnType<typeof realAi.streamText>;
38
46
  const realStreamText = realAi.streamText;
@@ -43,7 +51,7 @@ mock.module("ai", () => ({
43
51
  streamTextOverride ? streamTextOverride(options) : realStreamText(options),
44
52
  }));
45
53
 
46
- const {addGptRoutes} = await import("./gpt");
54
+ const {addGptRoutes}: typeof import("./gpt.js") = require("./gpt");
47
55
 
48
56
  // Create mock AI model (LanguageModelV2)
49
57
  const createMockModel = (modelId = "mock-model") => ({
@@ -184,7 +192,7 @@ describe("AI Routes", () => {
184
192
  beforeAll(async () => {
185
193
  await ensureTestUsers();
186
194
  await AIRequest.deleteMany({});
187
- await GptHistory.deleteMany({});
195
+ await GptHistory.collection.deleteMany({});
188
196
  });
189
197
 
190
198
  beforeEach(() => {
@@ -193,12 +201,12 @@ describe("AI Routes", () => {
193
201
 
194
202
  app = new TerrenoApp({
195
203
  configureApp: (router, options) => {
196
- addGptHistoryRoutes(router, options);
204
+ addGptHistoryRoutes(router, historyRouterOptions(options));
197
205
  addGptRoutes(router, {aiService, openApiOptions: options});
198
206
  addAiRequestsExplorerRoutes(router, {openApiOptions: options});
199
207
  },
200
208
  skipListen: true,
201
- userModel: UserModel,
209
+ userModel: terrenoUserModel,
202
210
  }).build();
203
211
  });
204
212
 
@@ -207,7 +215,7 @@ describe("AI Routes", () => {
207
215
  resetObservabilityApp();
208
216
  await AIRequest.deleteMany({});
209
217
  await FileAttachment.deleteMany({});
210
- await GptHistory.deleteMany({});
218
+ await GptHistory.collection.deleteMany({});
211
219
  });
212
220
 
213
221
  describe("GPT History routes", () => {
@@ -262,7 +270,7 @@ describe("AI Routes", () => {
262
270
  addGptRoutes(router, {openApiOptions: options});
263
271
  },
264
272
  skipListen: true,
265
- userModel: UserModel,
273
+ userModel: terrenoUserModel,
266
274
  }).build();
267
275
  const agent = await authAsUser(demoApp, "notAdmin");
268
276
  const res = await agent.post("/gpt/remix").send({text: "Hello"});
@@ -397,7 +405,7 @@ describe("AI Routes", () => {
397
405
  addGptRoutes(router, {openApiOptions: options});
398
406
  },
399
407
  skipListen: true,
400
- userModel: UserModel,
408
+ userModel: terrenoUserModel,
401
409
  }).build();
402
410
  const agent = await authAsUser(demoApp, "notAdmin");
403
411
  const res = await agent
@@ -419,7 +427,7 @@ describe("AI Routes", () => {
419
427
  addGptRoutes(router, {createModelFn, openApiOptions: options});
420
428
  },
421
429
  skipListen: true,
422
- userModel: UserModel,
430
+ userModel: terrenoUserModel,
423
431
  }).build();
424
432
  const agent = await authAsUser(customApp, "notAdmin");
425
433
  const res = await agent
@@ -441,7 +449,7 @@ describe("AI Routes", () => {
441
449
  addGptRoutes(router, {createServerModelFn, openApiOptions: options});
442
450
  },
443
451
  skipListen: true,
444
- userModel: UserModel,
452
+ userModel: terrenoUserModel,
445
453
  }).build();
446
454
  const agent = await authAsUser(customApp, "notAdmin");
447
455
  const res = await agent
@@ -454,13 +462,16 @@ describe("AI Routes", () => {
454
462
  });
455
463
 
456
464
  it("falls back to default aiService when createServerModelFn returns null", async () => {
457
- const createServerModelFn = mock((_modelId?: string) => null);
465
+ // A JavaScript host may return null even though the option's type says undefined.
466
+ const createServerModelFn = mock(
467
+ (_modelId?: string) => null as unknown as LanguageModel | undefined
468
+ );
458
469
  const customApp = new TerrenoApp({
459
470
  configureApp: (router, options) => {
460
471
  addGptRoutes(router, {aiService, createServerModelFn, openApiOptions: options});
461
472
  },
462
473
  skipListen: true,
463
- userModel: UserModel,
474
+ userModel: terrenoUserModel,
464
475
  }).build();
465
476
  const agent = await authAsUser(customApp, "notAdmin");
466
477
  const res = await agent
@@ -576,7 +587,7 @@ describe("AI Routes", () => {
576
587
  addGptRoutes(router, {aiService: noFinishService, openApiOptions: options});
577
588
  },
578
589
  skipListen: true,
579
- userModel: UserModel,
590
+ userModel: terrenoUserModel,
580
591
  }).build();
581
592
  const agent = await authAsUser(noFinishApp, "notAdmin");
582
593
  const res = await agent
@@ -598,7 +609,7 @@ describe("AI Routes", () => {
598
609
  addGptRoutes(router, {aiService: imageService, openApiOptions: options});
599
610
  },
600
611
  skipListen: true,
601
- userModel: UserModel,
612
+ userModel: terrenoUserModel,
602
613
  }).build();
603
614
  const agent = await authAsUser(imgApp, "notAdmin");
604
615
  const res = await agent
@@ -622,7 +633,7 @@ describe("AI Routes", () => {
622
633
  addGptRoutes(router, {aiService: imageService, openApiOptions: options});
623
634
  },
624
635
  skipListen: true,
625
- userModel: UserModel,
636
+ userModel: terrenoUserModel,
626
637
  }).build();
627
638
  const agent = await authAsUser(imgApp, "notAdmin");
628
639
  const res = await agent
@@ -638,7 +649,9 @@ describe("AI Routes", () => {
638
649
  const assistant = history.prompts.find((p) => p.type === "assistant");
639
650
  expect(assistant?.text).toBe("");
640
651
  expect(assistant?.content).toHaveLength(1);
641
- expect(assistant?.content?.[0]?.url).toBe("data:image/png;base64,aGVsbG8=");
652
+ expect((assistant?.content?.[0] as ImageContentPart | undefined)?.url).toBe(
653
+ "data:image/png;base64,aGVsbG8="
654
+ );
642
655
  });
643
656
 
644
657
  it("writes an SSE error event when the model stream throws", async () => {
@@ -650,7 +663,7 @@ describe("AI Routes", () => {
650
663
  addGptRoutes(router, {aiService: errService, openApiOptions: options});
651
664
  },
652
665
  skipListen: true,
653
- userModel: UserModel,
666
+ userModel: terrenoUserModel,
654
667
  }).build();
655
668
  const agent = await authAsUser(errApp, "notAdmin");
656
669
  const res = await agent
@@ -674,7 +687,7 @@ describe("AI Routes", () => {
674
687
  addGptRoutes(router, {aiService, mcpService, openApiOptions: options});
675
688
  },
676
689
  skipListen: true,
677
- userModel: UserModel,
690
+ userModel: terrenoUserModel,
678
691
  }).build();
679
692
  const agent = await authAsUser(customApp, "notAdmin");
680
693
  const res = await agent
@@ -697,7 +710,7 @@ describe("AI Routes", () => {
697
710
  addGptRoutes(router, {aiService, mcpService, openApiOptions: options});
698
711
  },
699
712
  skipListen: true,
700
- userModel: UserModel,
713
+ userModel: terrenoUserModel,
701
714
  }).build();
702
715
  const agent = await authAsUser(customApp, "notAdmin");
703
716
  const res = await agent
@@ -727,7 +740,7 @@ describe("AI Routes", () => {
727
740
  });
728
741
  },
729
742
  skipListen: true,
730
- userModel: UserModel,
743
+ userModel: terrenoUserModel,
731
744
  }).build();
732
745
  const agent = await authAsUser(customApp, "notAdmin");
733
746
  const res = await agent
@@ -752,7 +765,7 @@ describe("AI Routes", () => {
752
765
  });
753
766
  },
754
767
  skipListen: true,
755
- userModel: UserModel,
768
+ userModel: terrenoUserModel,
756
769
  }).build();
757
770
  const agent = await authAsUser(customApp, "notAdmin");
758
771
  const res = await agent
@@ -785,7 +798,7 @@ describe("AI Routes", () => {
785
798
  });
786
799
  },
787
800
  skipListen: true,
788
- userModel: UserModel,
801
+ userModel: terrenoUserModel,
789
802
  }).build();
790
803
  const agent = await authAsUser(imgApp, "notAdmin");
791
804
  const res = await agent
@@ -806,7 +819,7 @@ describe("AI Routes", () => {
806
819
  });
807
820
  },
808
821
  skipListen: true,
809
- userModel: UserModel,
822
+ userModel: terrenoUserModel,
810
823
  }).build();
811
824
  const agent = await authAsUser(customApp, "notAdmin");
812
825
  const res = await agent
@@ -912,7 +925,7 @@ describe("AI Routes", () => {
912
925
  });
913
926
  },
914
927
  skipListen: true,
915
- userModel: UserModel,
928
+ userModel: terrenoUserModel,
916
929
  }).build();
917
930
  const agent = await authAsUser(toolApp, "notAdmin");
918
931
  const res = await agent
@@ -1018,7 +1031,7 @@ describe("AI Routes", () => {
1018
1031
  });
1019
1032
  },
1020
1033
  skipListen: true,
1021
- userModel: UserModel,
1034
+ userModel: terrenoUserModel,
1022
1035
  }).build();
1023
1036
  const agent = await authAsUser(fileToolApp, "notAdmin");
1024
1037
  const res = await agent
@@ -1074,7 +1087,7 @@ describe("AI Routes", () => {
1074
1087
  });
1075
1088
  },
1076
1089
  skipListen: true,
1077
- userModel: UserModel,
1090
+ userModel: terrenoUserModel,
1078
1091
  }).build();
1079
1092
  const agent = await authAsUser(errApp, "notAdmin");
1080
1093
  const res = await agent
@@ -1127,7 +1140,7 @@ describe("AI Routes", () => {
1127
1140
  });
1128
1141
  },
1129
1142
  skipListen: true,
1130
- userModel: UserModel,
1143
+ userModel: terrenoUserModel,
1131
1144
  }).build();
1132
1145
  const agent = await authAsUser(customApp, "notAdmin");
1133
1146
  const res = await agent
@@ -1199,7 +1212,7 @@ describe("AI Routes", () => {
1199
1212
  });
1200
1213
  },
1201
1214
  skipListen: true,
1202
- userModel: UserModel,
1215
+ userModel: terrenoUserModel,
1203
1216
  }).build();
1204
1217
  const agent = await authAsUser(customApp, "notAdmin");
1205
1218
  const res = await agent
@@ -1225,7 +1238,7 @@ describe("AI Routes", () => {
1225
1238
  });
1226
1239
  },
1227
1240
  skipListen: true,
1228
- userModel: UserModel,
1241
+ userModel: terrenoUserModel,
1229
1242
  }).build();
1230
1243
  const agent = await authAsUser(customApp, "notAdmin");
1231
1244
  const res = await agent
@@ -1277,7 +1290,7 @@ describe("AI Routes", () => {
1277
1290
  });
1278
1291
  },
1279
1292
  skipListen: true,
1280
- userModel: UserModel,
1293
+ userModel: terrenoUserModel,
1281
1294
  }).build();
1282
1295
  const agent = await authAsUser(fileApp, "notAdmin");
1283
1296
  const res = await agent
@@ -1446,7 +1459,7 @@ describe("AI Routes", () => {
1446
1459
  addGptRoutes(router, {aiService, openApiOptions: options, ...extra});
1447
1460
  },
1448
1461
  skipListen: true,
1449
- userModel: UserModel,
1462
+ userModel: terrenoUserModel,
1450
1463
  }).build();
1451
1464
 
1452
1465
  it("rejects attachments when file uploads are disabled", async () => {
@@ -1684,7 +1697,10 @@ describe("AI Routes", () => {
1684
1697
  .patch(`/gpt/histories/${historyId}`)
1685
1698
  .send({prompts: stored.body.data.prompts});
1686
1699
  expect(roundTripped.status).toBe(200);
1687
- expect((await GptHistory.findById(historyId))?.prompts[0].content?.[1].gcsKey).toBe(ownKey);
1700
+ const filePart = (await GptHistory.findById(historyId))?.prompts[0].content?.[1] as
1701
+ | FileContentPart
1702
+ | ImageContentPart;
1703
+ expect(filePart.gcsKey).toBe(ownKey);
1688
1704
 
1689
1705
  const patched = await agent.patch(`/gpt/histories/${historyId}`).send({
1690
1706
  prompts: [{content: [{text: "f", type: "text"}, plantedPart], text: "f", type: "user"}],
@@ -1694,9 +1710,10 @@ describe("AI Routes", () => {
1694
1710
  .patch(`/gpt/histories/${historyId}`)
1695
1711
  .send({"prompts.0.content.1.gcsKey": plantedKey});
1696
1712
  expect(dotted.status).toBe(200);
1697
- const prompt = await agent
1698
- .post(`/gpt/histories/${historyId}/prompts`)
1699
- .send({prompts: {content: [plantedPart], text: "g", type: "user"}});
1713
+ const latest = await agent.get(`/gpt/histories/${historyId}`);
1714
+ const prompt = await agent.patch(`/gpt/histories/${historyId}`).send({
1715
+ prompts: [...latest.body.data.prompts, {content: [plantedPart], text: "g", type: "user"}],
1716
+ });
1700
1717
  expect(prompt.status).toBe(200);
1701
1718
 
1702
1719
  const history = await GptHistory.findById(historyId);
@@ -1999,7 +2016,7 @@ describe("AI Routes", () => {
1999
2016
  });
2000
2017
  },
2001
2018
  skipListen: true,
2002
- userModel: UserModel,
2019
+ userModel: terrenoUserModel,
2003
2020
  }).build();
2004
2021
  const agent = await authAsUser(errApp, "notAdmin");
2005
2022
 
@@ -2047,7 +2064,7 @@ describe("AI Routes", () => {
2047
2064
  addGptRoutes(router, {aiService, openApiOptions: options});
2048
2065
  },
2049
2066
  skipListen: true,
2050
- userModel: UserModel,
2067
+ userModel: terrenoUserModel,
2051
2068
  }).build();
2052
2069
  const agent = await authAsUser(errApp, "notAdmin");
2053
2070
 
@@ -2097,7 +2114,7 @@ describe("AI Routes", () => {
2097
2114
  });
2098
2115
  },
2099
2116
  skipListen: true,
2100
- userModel: UserModel,
2117
+ userModel: terrenoUserModel,
2101
2118
  }).build();
2102
2119
  const agent = await authAsUser(errApp, "notAdmin");
2103
2120
  const res = await agent
@@ -2150,7 +2167,7 @@ describe("AI Routes", () => {
2150
2167
  });
2151
2168
  },
2152
2169
  skipListen: true,
2153
- userModel: UserModel,
2170
+ userModel: terrenoUserModel,
2154
2171
  }).build();
2155
2172
  const agent = await authAsUser(customApp, "notAdmin");
2156
2173
  const res = await agent
@@ -2214,7 +2231,7 @@ describe("AI Routes", () => {
2214
2231
  });
2215
2232
  },
2216
2233
  skipListen: true,
2217
- userModel: UserModel,
2234
+ userModel: terrenoUserModel,
2218
2235
  }).build();
2219
2236
  const originalLogRequest = AIRequest.logRequest;
2220
2237
  AIRequest.logRequest = mock(async () => {
@@ -2248,7 +2265,7 @@ describe("AI Routes", () => {
2248
2265
  });
2249
2266
  },
2250
2267
  skipListen: true,
2251
- userModel: UserModel,
2268
+ userModel: terrenoUserModel,
2252
2269
  }).build();
2253
2270
  const agent = await authAsUser(customApp, "notAdmin");
2254
2271
  const res = await agent.get("/gpt/tools");
@@ -2265,7 +2282,7 @@ describe("AI Routes", () => {
2265
2282
  addGptRoutes(router, {aiService, createRequestTools, openApiOptions: options});
2266
2283
  },
2267
2284
  skipListen: true,
2268
- userModel: UserModel,
2285
+ userModel: terrenoUserModel,
2269
2286
  }).build();
2270
2287
  const agent = await authAsUser(customApp, "notAdmin");
2271
2288
  const res = await agent.get("/gpt/tools");
@@ -2282,7 +2299,7 @@ describe("AI Routes", () => {
2282
2299
  addGptRoutes(router, {aiService, mcpService, openApiOptions: options});
2283
2300
  },
2284
2301
  skipListen: true,
2285
- userModel: UserModel,
2302
+ userModel: terrenoUserModel,
2286
2303
  }).build();
2287
2304
  const agent = await authAsUser(customApp, "notAdmin");
2288
2305
  const res = await agent.get("/gpt/tools");
@@ -2301,7 +2318,7 @@ describe("AI Routes", () => {
2301
2318
  addGptRoutes(router, {aiService, mcpService, openApiOptions: options});
2302
2319
  },
2303
2320
  skipListen: true,
2304
- userModel: UserModel,
2321
+ userModel: terrenoUserModel,
2305
2322
  }).build();
2306
2323
  const agent = await authAsUser(customApp, "notAdmin");
2307
2324
  const res = await agent.get("/gpt/tools");
@@ -2556,7 +2573,7 @@ describe("AI Routes", () => {
2556
2573
  });
2557
2574
  },
2558
2575
  skipListen: true,
2559
- userModel: UserModel,
2576
+ userModel: terrenoUserModel,
2560
2577
  }).build();
2561
2578
  const agent = await authAsUser(fileApp, "notAdmin");
2562
2579
  const res = await agent
@@ -133,7 +133,8 @@ export const registerAiDataset = async ({
133
133
  assertRows(columns, rows);
134
134
  const created = await AIDataset.create({
135
135
  columns,
136
- historyId,
136
+ // GptHistory ids are Strings; an ObjectId from an older caller is stored as its hex string.
137
+ historyId: String(historyId),
137
138
  rowCount: rows.length,
138
139
  rows,
139
140
  userId,
@@ -13,7 +13,7 @@ import {AIService, TemperaturePresets} from "./aiService";
13
13
  // Create a mock LanguageModelV2
14
14
  const createMockModel = (responseText = "Mock response") => {
15
15
  return {
16
- doGenerate: mock(async () => ({
16
+ doGenerate: mock(async (_options: unknown) => ({
17
17
  content: [{text: responseText, type: "text" as const}],
18
18
  finishReason: "stop" as const,
19
19
  usage: {inputTokens: 5, outputTokens: 10},
@@ -1,6 +1,14 @@
1
1
  import {describe, expect, it} from "bun:test";
2
2
  import {ASK_LIMITS, type AskKind, type AskResponse, askPromptSection} from "@terreno/blocks";
3
- import {asSchema, jsonSchema, type ModelMessage, type Tool, tool} from "ai";
3
+ import {
4
+ asSchema,
5
+ type JSONValue,
6
+ jsonSchema,
7
+ type ModelMessage,
8
+ type Tool,
9
+ type ToolResultPart,
10
+ tool,
11
+ } from "ai";
4
12
 
5
13
  import {
6
14
  approvalAskInput,
@@ -24,7 +32,7 @@ const PLAN_ASK_INPUT = {
24
32
  {id: "team", label: "Team"},
25
33
  ],
26
34
  prompt: "Which plan should I set up?",
27
- select: "one",
35
+ select: "one" as const,
28
36
  };
29
37
 
30
38
  const TEAM_ANSWER = {action: "accept" as const, content: {selected: ["team"]}};
@@ -57,8 +65,8 @@ const lookupResult = {
57
65
  type: "tool-result" as const,
58
66
  };
59
67
 
60
- const answerResult = (toolCallId: string, value: unknown) => ({
61
- output: {type: "json", value},
68
+ const answerResult = (toolCallId: string, value: unknown): ToolResultPart => ({
69
+ output: {type: "json", value: value as JSONValue},
62
70
  toolCallId,
63
71
  toolName: "ask_choice",
64
72
  type: "tool-result",
@@ -76,14 +84,14 @@ describe("asks", () => {
76
84
  expected: ["confirm"],
77
85
  label: '{kinds: ["confirm"]}',
78
86
  },
79
- {asks: {kinds: []}, expected: [], label: "{kinds: []}"},
87
+ {asks: {kinds: [] as AskKind[]}, expected: [], label: "{kinds: []}"},
80
88
  {
81
89
  asks: {kinds: ["choice", "choice"] as AskKind[]},
82
90
  expected: ["choice"],
83
91
  label: '{kinds: ["choice", "choice"]}',
84
92
  },
85
93
  ])("resolves $label", ({asks, expected}) => {
86
- expect(resolveAskKinds(asks)).toEqual(expected);
94
+ expect(resolveAskKinds(asks)).toEqual([...expected]);
87
95
  });
88
96
 
89
97
  it("throws on an unknown kind and names the known kinds", () => {
@@ -415,6 +415,10 @@ const loadHistory = async ({
415
415
  if (!historyId) {
416
416
  return new GptHistory({prompts: [], userId, ...(projectId ? {projectId} : {})});
417
417
  }
418
+ // History ids are Strings (synced collection); an object here would be read as a query operator.
419
+ if (typeof historyId !== "string") {
420
+ throw new APIError({status: 400, title: "historyId must be a string"});
421
+ }
418
422
  const history = await GptHistory.findById(historyId);
419
423
  if (!history) {
420
424
  throw new APIError({status: 404, title: "History not found"});
@@ -1,4 +1,4 @@
1
- import {afterEach, beforeEach, describe, expect, it} from "bun:test";
1
+ import {afterEach, beforeAll, beforeEach, describe, expect, it} from "bun:test";
2
2
  import {registerGcsMock} from "@terreno/test";
3
3
  import mongoose from "mongoose";
4
4
 
@@ -7,7 +7,10 @@ import {FileAttachment} from "../models/fileAttachment";
7
7
  // In-memory @google-cloud/storage so the service doesn't try to authenticate
8
8
  const gcs = registerGcsMock();
9
9
 
10
- const {FileStorageService} = await import("./fileStorage");
10
+ let FileStorageService: typeof import("./fileStorage.js").FileStorageService;
11
+ beforeAll(async () => {
12
+ ({FileStorageService} = await import("./fileStorage.js"));
13
+ });
11
14
 
12
15
  describe("FileStorageService", () => {
13
16
  beforeEach(async () => {
@@ -7,12 +7,16 @@ import {
7
7
  registerMCPTool,
8
8
  type User,
9
9
  } from "@terreno/api";
10
+ import type {ToolExecutionOptions} from "ai";
10
11
  import {assert} from "chai";
11
12
  import mongoose, {Schema} from "mongoose";
12
13
  import {z} from "zod";
13
14
 
14
15
  import {getMCPTools} from "./getMCPTools";
15
16
 
17
+ /** `execute` options as the AI SDK passes them; getMCPTools ignores them. */
18
+ const callOptions: ToolExecutionOptions = {messages: [], toolCallId: "call-1"};
19
+
16
20
  interface NoteFields {
17
21
  _id: mongoose.Types.ObjectId;
18
22
  title: string;
@@ -67,7 +71,7 @@ describe("getMCPTools", () => {
67
71
 
68
72
  expect(Object.keys(tools).sort()).toEqual(["notes_create", "notes_list"]);
69
73
 
70
- const created = await tools.notes_create.execute?.({title: "From AI tools"});
74
+ const created = await tools.notes_create.execute?.({title: "From AI tools"}, callOptions);
71
75
  expect(created).toEqual(
72
76
  expect.objectContaining({
73
77
  data: expect.objectContaining({title: "From AI tools"}),
@@ -88,7 +92,7 @@ describe("getMCPTools", () => {
88
92
  const tools = getMCPTools();
89
93
  assert.deepEqual(Object.keys(tools), ["echo_json"]);
90
94
  assert.equal(tools.echo_json.description, "Echoes structured json back to the caller");
91
- assert.deepEqual(await tools.echo_json.execute?.({value: "hi"}), {echoed: "hi"});
95
+ assert.deepEqual(await tools.echo_json.execute?.({value: "hi"}, callOptions), {echoed: "hi"});
92
96
  });
93
97
 
94
98
  it("returns joined plain text when the tool response is not json", async () => {
@@ -105,7 +109,7 @@ describe("getMCPTools", () => {
105
109
  });
106
110
 
107
111
  const tools = getMCPTools();
108
- assert.equal(await tools.prose.execute?.({}), "first line\nsecond line");
112
+ assert.equal(await tools.prose.execute?.({}, callOptions), "first line\nsecond line");
109
113
  });
110
114
 
111
115
  it("passes the authenticated user through to custom tool handlers", async () => {
@@ -121,8 +125,8 @@ describe("getMCPTools", () => {
121
125
  });
122
126
 
123
127
  const user = asUser();
124
- await getMCPTools(user).whoami.execute?.({});
125
- await getMCPTools().whoami.execute?.({});
128
+ await getMCPTools(user).whoami.execute?.({}, callOptions);
129
+ await getMCPTools().whoami.execute?.({}, callOptions);
126
130
  assert.deepEqual(seenUserIds, [user.id, undefined]);
127
131
  });
128
132
  });
@@ -1,15 +1,18 @@
1
- import {beforeEach, describe, expect, it, mock} from "bun:test";
1
+ import {beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
2
2
 
3
3
  const clientImpl = {
4
4
  close: mock(async () => {}),
5
5
  tools: mock(async () => ({search: {description: "Search"}})),
6
6
  };
7
- const createMCPClientMock = mock(async () => clientImpl);
7
+ const createMCPClientMock = mock(async (_opts: Record<string, unknown>) => clientImpl);
8
8
  mock.module("@ai-sdk/mcp", () => ({
9
9
  createMCPClient: (opts: Record<string, unknown>) => createMCPClientMock(opts),
10
10
  }));
11
11
 
12
- const {MCPService} = await import("./mcpService");
12
+ let MCPService: typeof import("./mcpService.js").MCPService;
13
+ beforeAll(async () => {
14
+ ({MCPService} = await import("./mcpService.js"));
15
+ });
13
16
 
14
17
  const makeConfig = (overrides?: Partial<Record<string, unknown>>) => ({
15
18
  name: "test-server",
@@ -417,10 +417,10 @@ describe("vertex helpers", () => {
417
417
  describe("createVertexProvider", () => {
418
418
  it("returns undefined and logs a warning when factory is not available and module is not installed", () => {
419
419
  const provider = createVertexProvider({
420
+ loadModule: () => undefined,
420
421
  project: "demo-project",
421
422
  vertexFactory: undefined,
422
423
  });
423
- // loadVertexModule returns undefined because @ai-sdk/google-vertex is not installed in test env
424
424
  expect(provider).toBeUndefined();
425
425
  });
426
426
 
@@ -1,3 +1,4 @@
1
+ import type {ImageModelV4, LanguageModelV4} from "@ai-sdk/provider";
1
2
  import {APIError, logger} from "@terreno/api";
2
3
  import type {ImageModel, LanguageModel} from "ai";
3
4
 
@@ -24,8 +25,10 @@ const MODEL_LIST_PAGE_SIZE = "200";
24
25
 
25
26
  /** Provider returned by `@ai-sdk/google-vertex` `createVertex`. */
26
27
  export interface VertexLanguageModelProvider {
27
- (modelId: string): LanguageModel;
28
- image: (modelId: string) => ImageModel;
28
+ /** `@ai-sdk/google-vertex` v4+ returns `LanguageModelV4`; older `ai` majors still type `LanguageModel` as v2/v3 only. */
29
+ (modelId: string): LanguageModel | LanguageModelV4;
30
+ /** `@ai-sdk/google-vertex` v4+ returns `ImageModelV4`; older `ai` majors still type `ImageModel` as v2/v3 only. */
31
+ image: (modelId: string) => ImageModel | ImageModelV4;
29
32
  }
30
33
 
31
34
  interface VertexModule {
@@ -229,9 +232,9 @@ export const createVertexProvider = (
229
232
 
230
233
  return {
231
234
  allowedModels,
232
- imageModel: (modelId: string): ImageModel => raw.image(assertAllowed(modelId)),
235
+ imageModel: (modelId: string): ImageModel => raw.image(assertAllowed(modelId)) as ImageModel,
233
236
  isModelAllowed: (modelId: string): boolean => isVertexModelAllowed(modelId, allowedModels),
234
- languageModel: (modelId: string): LanguageModel => raw(assertAllowed(modelId)),
237
+ languageModel: (modelId: string): LanguageModel => raw(assertAllowed(modelId)) as LanguageModel,
235
238
  location,
236
239
  project,
237
240
  raw,
@@ -7,7 +7,7 @@ registerBackendPreload({
7
7
  connectMongoInBeforeAll: true,
8
8
  mongo: {baseDatabaseName: "terreno-ai-test", useReplSet: true},
9
9
  onBeforeEach: async () => {
10
- const {shutdownLangfuseClient} = await import("../langfuseClient");
10
+ const {shutdownLangfuseClient} = await import("../langfuseClient.js");
11
11
  await shutdownLangfuseClient();
12
12
  },
13
13
  testEnv: {
@@ -11,7 +11,7 @@ import {addGptRoutes} from "../routes/gpt";
11
11
  import {addGptHistoryRoutes} from "../routes/gptHistories";
12
12
  import {AIService} from "../service/aiService";
13
13
  import type {GptHistoryDocument, GptRouteOptions} from "../types";
14
- import {type authAsUser, UserModel} from "./helpers";
14
+ import {type authAsUser, historyRouterOptions, terrenoUserModel} from "./helpers";
15
15
 
16
16
  type ModelStreamPart = {type: string; [key: string]: unknown};
17
17
 
@@ -232,7 +232,7 @@ export const createScriptedModel = ({
232
232
  const remaining = [...steps];
233
233
  let callCount = 0;
234
234
  return {
235
- doGenerate: mock(async () => ({
235
+ doGenerate: mock(async (_options: ModelCallOptions) => ({
236
236
  content: [{text: "Workspace setup", type: "text" as const}],
237
237
  finishReason: "stop" as const,
238
238
  usage: USAGE,
@@ -348,11 +348,11 @@ export const buildApp = ({
348
348
  openApiOptions: options,
349
349
  ...routeOptions,
350
350
  };
351
- addGptHistoryRoutes(router, {...options, chat});
351
+ addGptHistoryRoutes(router, {...historyRouterOptions(options), chat});
352
352
  addGptRoutes(router, chat);
353
353
  },
354
354
  skipListen: true,
355
- userModel: UserModel,
355
+ userModel: terrenoUserModel,
356
356
  }).build();
357
357
 
358
358
  export interface ServedApp {