@terreno/ai 0.10.0 → 0.11.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.
@@ -1,5 +1,6 @@
1
1
  import {afterEach, beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
2
2
  import {createdUpdatedPlugin, setupServer} from "@terreno/api";
3
+ import type {LanguageModel, Tool} from "ai";
3
4
  import type express from "express";
4
5
  import mongoose from "mongoose";
5
6
  import passportLocalMongoose from "passport-local-mongoose";
@@ -7,12 +8,20 @@ import supertest from "supertest";
7
8
 
8
9
  import {AIRequest} from "../models/aiRequest";
9
10
  import {GptHistory} from "../models/gptHistory";
11
+ import {Project} from "../models/project";
10
12
  import {AIService} from "../service/aiService";
13
+ import type {MCPService} from "../service/mcpService";
11
14
  import {addAiRequestsExplorerRoutes} from "./aiRequestsExplorer";
12
15
  import {addGptHistoryRoutes} from "./gptHistories";
13
16
 
14
- // Mock langfuseVercelAi to avoid transitive langfuse SDK import in tests
17
+ type PasswordedUser = {setPassword: (password: string) => Promise<void>};
18
+ type SseResponse = supertest.Response & {body: string};
19
+
20
+ // Mock langfuseVercelAi to avoid transitive langfuse SDK import in tests.
21
+ // Spread the real module so later-loaded test files still see the real exports.
22
+ const realLangfuseVercelAi = await import("../langfuseVercelAi");
15
23
  mock.module("../langfuseVercelAi", () => ({
24
+ ...realLangfuseVercelAi,
16
25
  createTelemetryConfig: () => ({functionId: "test", isEnabled: false}),
17
26
  preparePromptForAI: async () => ({config: {}, prompt: "test", telemetry: {isEnabled: false}}),
18
27
  }));
@@ -25,15 +34,19 @@ const userSchema = new mongoose.Schema({
25
34
  email: {index: true, type: String},
26
35
  name: String,
27
36
  });
28
- userSchema.plugin(passportLocalMongoose as any, {
29
- usernameField: "email",
30
- });
37
+ userSchema.plugin(
38
+ passportLocalMongoose as unknown as (
39
+ schema: mongoose.Schema,
40
+ options: {usernameField: string}
41
+ ) => void,
42
+ {usernameField: "email"}
43
+ );
31
44
  userSchema.plugin(createdUpdatedPlugin);
32
45
 
33
46
  const UserModel = mongoose.models.User || mongoose.model("User", userSchema);
34
47
 
35
48
  // Create mock AI model (LanguageModelV2)
36
- const createMockModel = () => ({
49
+ const createMockModel = (modelId = "mock-model") => ({
37
50
  doGenerate: mock(async () => ({
38
51
  content: [{text: "AI response", type: "text" as const}],
39
52
  finishReason: "stop" as const,
@@ -55,14 +68,63 @@ const createMockModel = () => ({
55
68
  },
56
69
  }),
57
70
  })),
58
- modelId: "mock-model",
71
+ modelId,
59
72
  provider: "mock-provider",
60
73
  specificationVersion: "v2" as const,
61
74
  supportedUrls: {},
62
75
  });
63
76
 
64
- let app: any;
65
- let aiService: any;
77
+ // Create mock model that emits a file (image) event
78
+ const createImageModel = () => ({
79
+ doGenerate: mock(async () => ({
80
+ content: [{text: "done", type: "text" as const}],
81
+ finishReason: "stop" as const,
82
+ usage: {inputTokens: 2, outputTokens: 2},
83
+ })),
84
+ doStream: mock(async () => ({
85
+ stream: new ReadableStream({
86
+ start(controller) {
87
+ controller.enqueue({id: "t1", type: "text-start" as const});
88
+ controller.enqueue({delta: "here is an image", id: "t1", type: "text-delta" as const});
89
+ controller.enqueue({id: "t1", type: "text-end" as const});
90
+ controller.enqueue({
91
+ data: "aGVsbG8=",
92
+ mediaType: "image/png",
93
+ type: "file" as const,
94
+ });
95
+ controller.enqueue({
96
+ finishReason: "stop" as const,
97
+ type: "finish" as const,
98
+ usage: {inputTokens: 2, outputTokens: 2},
99
+ });
100
+ controller.close();
101
+ },
102
+ }),
103
+ })),
104
+ modelId: "gemini-2.5-flash-image",
105
+ provider: "mock-provider",
106
+ specificationVersion: "v2" as const,
107
+ supportedUrls: {},
108
+ });
109
+
110
+ // Mock model that throws during doStream to exercise the inner catch
111
+ const createErrorModel = () => ({
112
+ doGenerate: mock(async () => ({
113
+ content: [],
114
+ finishReason: "error" as const,
115
+ usage: {inputTokens: 0, outputTokens: 0},
116
+ })),
117
+ doStream: mock(async () => {
118
+ throw new Error("stream failure");
119
+ }),
120
+ modelId: "error-model",
121
+ provider: "mock-provider",
122
+ specificationVersion: "v2" as const,
123
+ supportedUrls: {},
124
+ });
125
+
126
+ let app: express.Application;
127
+ let aiService: AIService;
66
128
 
67
129
  const authAsUser = async (appInstance: express.Application, type: "admin" | "notAdmin") => {
68
130
  const email = type === "admin" ? "admin@example.com" : "notAdmin@example.com";
@@ -81,17 +143,17 @@ describe("AI Routes", () => {
81
143
  await GptHistory.deleteMany({});
82
144
 
83
145
  const admin = await UserModel.create({admin: true, email: "admin@example.com", name: "Admin"});
84
- await (admin as any).setPassword("securePassword");
146
+ await (admin as unknown as PasswordedUser).setPassword("securePassword");
85
147
  await admin.save();
86
148
 
87
149
  const user = await UserModel.create({email: "notAdmin@example.com", name: "User"});
88
- await (user as any).setPassword("password");
150
+ await (user as unknown as PasswordedUser).setPassword("password");
89
151
  await user.save();
90
152
  });
91
153
 
92
154
  beforeEach(() => {
93
155
  const mockModel = createMockModel();
94
- aiService = new AIService({model: mockModel as any});
156
+ aiService = new AIService({model: mockModel as unknown as LanguageModel});
95
157
 
96
158
  app = setupServer({
97
159
  addRoutes: (router, options) => {
@@ -100,7 +162,7 @@ describe("AI Routes", () => {
100
162
  addAiRequestsExplorerRoutes(router, {openApiOptions: options});
101
163
  },
102
164
  skipListen: true,
103
- userModel: UserModel as any,
165
+ userModel: UserModel,
104
166
  });
105
167
  });
106
168
 
@@ -145,6 +207,814 @@ describe("AI Routes", () => {
145
207
 
146
208
  expect(res.status).toBe(400);
147
209
  });
210
+
211
+ it("returns demo response when no aiService configured", async () => {
212
+ const demoApp = setupServer({
213
+ addRoutes: (router, options) => {
214
+ addGptRoutes(router, {openApiOptions: options});
215
+ },
216
+ skipListen: true,
217
+ userModel: UserModel,
218
+ });
219
+ const agent = await authAsUser(demoApp, "notAdmin");
220
+ const res = await agent.post("/gpt/remix").send({text: "Hello"});
221
+ expect(res.status).toBe(200);
222
+ expect(res.body.data).toContain("demo mode");
223
+ });
224
+ });
225
+
226
+ type SseStream = {on: (event: string, handler: (arg: Buffer) => void) => void};
227
+ const sseCollect = (r: SseStream, cb: (err: Error | null, data: string) => void) => {
228
+ let data = "";
229
+ r.on("data", (chunk: Buffer) => {
230
+ data += chunk.toString();
231
+ });
232
+ r.on("end", () => cb(null, data));
233
+ };
234
+
235
+ describe("GPT Prompt route", () => {
236
+ it("requires a prompt", async () => {
237
+ const agent = await authAsUser(app, "notAdmin");
238
+ const res = await agent.post("/gpt/prompt").send({});
239
+ // APIError.instanceof check in pre-stream catch results in 500 from the dist-compiled class,
240
+ // but the body should contain the detail from the thrown APIError.
241
+ expect(res.status).toBeGreaterThanOrEqual(400);
242
+ expect(res.body.detail || res.body.title).toContain("prompt");
243
+ });
244
+
245
+ it("streams a response and saves history", async () => {
246
+ const agent = await authAsUser(app, "notAdmin");
247
+ const res = await agent
248
+ .post("/gpt/prompt")
249
+ .send({prompt: "Hi"})
250
+ .buffer(true)
251
+ .parse(sseCollect);
252
+
253
+ expect(res.status).toBe(200);
254
+ const body = (res as SseResponse).body;
255
+ expect(body).toContain("data:");
256
+ expect(body).toContain("done");
257
+
258
+ const histories = await GptHistory.find({});
259
+ expect(histories.length).toBeGreaterThanOrEqual(1);
260
+ });
261
+
262
+ it("sends demo response when no ai service configured", async () => {
263
+ const demoApp = setupServer({
264
+ addRoutes: (router, options) => {
265
+ addGptRoutes(router, {openApiOptions: options});
266
+ },
267
+ skipListen: true,
268
+ userModel: UserModel,
269
+ });
270
+ const agent = await authAsUser(demoApp, "notAdmin");
271
+ const res = await agent
272
+ .post("/gpt/prompt")
273
+ .send({prompt: "Hi"})
274
+ .buffer(true)
275
+ .parse(sseCollect);
276
+ expect(res.status).toBe(200);
277
+ const body = (res as SseResponse).body;
278
+ expect(body).toContain("demo mode");
279
+ });
280
+
281
+ it("uses per-request api key via x-ai-api-key header", async () => {
282
+ const createModelFn = mock((_apiKey: string, _modelId?: string) => {
283
+ return createMockModel() as unknown as LanguageModel;
284
+ });
285
+ const customApp = setupServer({
286
+ addRoutes: (router, options) => {
287
+ addGptRoutes(router, {createModelFn, openApiOptions: options});
288
+ },
289
+ skipListen: true,
290
+ userModel: UserModel,
291
+ });
292
+ const agent = await authAsUser(customApp, "notAdmin");
293
+ const res = await agent
294
+ .post("/gpt/prompt")
295
+ .set("x-ai-api-key", "test-key")
296
+ .send({prompt: "Hi"})
297
+ .buffer(true)
298
+ .parse(sseCollect);
299
+ expect(res.status).toBe(200);
300
+ expect(createModelFn).toHaveBeenCalled();
301
+ });
302
+
303
+ it("uses createServerModelFn when model requested", async () => {
304
+ const createServerModelFn = mock(
305
+ (_modelId?: string) => createMockModel() as unknown as LanguageModel
306
+ );
307
+ const customApp = setupServer({
308
+ addRoutes: (router, options) => {
309
+ addGptRoutes(router, {createServerModelFn, openApiOptions: options});
310
+ },
311
+ skipListen: true,
312
+ userModel: UserModel,
313
+ });
314
+ const agent = await authAsUser(customApp, "notAdmin");
315
+ const res = await agent
316
+ .post("/gpt/prompt")
317
+ .send({model: "gemini-2.5-pro", prompt: "Hi"})
318
+ .buffer(true)
319
+ .parse(sseCollect);
320
+ expect(res.status).toBe(200);
321
+ expect(createServerModelFn).toHaveBeenCalledWith("gemini-2.5-pro");
322
+ });
323
+
324
+ it("rejects history from another user", async () => {
325
+ const otherUserId = new mongoose.Types.ObjectId();
326
+ const other = await GptHistory.create({
327
+ prompts: [{text: "Hello", type: "user"}],
328
+ userId: otherUserId,
329
+ });
330
+
331
+ const agent = await authAsUser(app, "notAdmin");
332
+ const res = await agent
333
+ .post("/gpt/prompt")
334
+ .send({historyId: other._id.toString(), prompt: "Hi"});
335
+ // APIError instanceof check returns false for dist-compiled classes, yielding 500
336
+ expect(res.status).toBeGreaterThanOrEqual(400);
337
+ expect(JSON.stringify(res.body)).toContain("authorized");
338
+ });
339
+
340
+ it("returns an error body for unknown historyId", async () => {
341
+ const agent = await authAsUser(app, "notAdmin");
342
+ const fakeId = new mongoose.Types.ObjectId().toString();
343
+ const res = await agent.post("/gpt/prompt").send({historyId: fakeId, prompt: "Hi"});
344
+ expect(res.status).toBeGreaterThanOrEqual(400);
345
+ expect(JSON.stringify(res.body)).toContain("not found".toLowerCase());
346
+ });
347
+
348
+ it("continues an existing history", async () => {
349
+ const notAdmin = (await UserModel.findOne({
350
+ email: "notAdmin@example.com",
351
+ })) as mongoose.Document & {_id: mongoose.Types.ObjectId};
352
+ const history = await GptHistory.create({
353
+ prompts: [{text: "Hello", type: "user"}],
354
+ userId: notAdmin._id,
355
+ });
356
+
357
+ const agent = await authAsUser(app, "notAdmin");
358
+ const res = await agent
359
+ .post("/gpt/prompt")
360
+ .send({historyId: history._id.toString(), prompt: "Hi again"})
361
+ .buffer(true)
362
+ .parse(sseCollect);
363
+ expect(res.status).toBe(200);
364
+ const updated = await GptHistory.findById(history._id);
365
+ expect(updated?.prompts.length).toBeGreaterThanOrEqual(3);
366
+ });
367
+
368
+ it("integrates project context when projectId is provided", async () => {
369
+ const notAdmin = (await UserModel.findOne({
370
+ email: "notAdmin@example.com",
371
+ })) as mongoose.Document & {_id: mongoose.Types.ObjectId};
372
+ const project = await Project.create({
373
+ memories: [{source: "user", text: "Likes brief answers"}],
374
+ name: "Test Project",
375
+ systemContext: "Respond concisely",
376
+ userId: notAdmin._id,
377
+ });
378
+
379
+ const agent = await authAsUser(app, "notAdmin");
380
+ const res = await agent
381
+ .post("/gpt/prompt")
382
+ .send({projectId: project._id.toString(), prompt: "Hi"})
383
+ .buffer(true)
384
+ .parse(sseCollect);
385
+ expect(res.status).toBe(200);
386
+ await Project.deleteMany({});
387
+ });
388
+
389
+ it("handles attachments in the prompt", async () => {
390
+ const agent = await authAsUser(app, "notAdmin");
391
+ const res = await agent
392
+ .post("/gpt/prompt")
393
+ .send({
394
+ attachments: [
395
+ {mimeType: "image/png", type: "image", url: "https://example.com/img.png"},
396
+ {
397
+ filename: "doc.pdf",
398
+ mimeType: "application/pdf",
399
+ type: "file",
400
+ url: "https://example.com/doc.pdf",
401
+ },
402
+ ],
403
+ prompt: "What is this?",
404
+ })
405
+ .buffer(true)
406
+ .parse(sseCollect);
407
+ expect(res.status).toBe(200);
408
+ });
409
+
410
+ it("streams inline image events from the model", async () => {
411
+ const imageService = new AIService({
412
+ model: createImageModel() as unknown as LanguageModel,
413
+ });
414
+ const imgApp = setupServer({
415
+ addRoutes: (router, options) => {
416
+ addGptRoutes(router, {aiService: imageService, openApiOptions: options});
417
+ },
418
+ skipListen: true,
419
+ userModel: UserModel,
420
+ });
421
+ const agent = await authAsUser(imgApp, "notAdmin");
422
+ const res = await agent
423
+ .post("/gpt/prompt")
424
+ .send({prompt: "Make an image"})
425
+ .buffer(true)
426
+ .parse(sseCollect);
427
+ expect(res.status).toBe(200);
428
+ const body = (res as SseResponse).body;
429
+ expect(body).toContain("image");
430
+ });
431
+
432
+ it("writes an SSE error event when the model stream throws", async () => {
433
+ const errService = new AIService({
434
+ model: createErrorModel() as unknown as LanguageModel,
435
+ });
436
+ const errApp = setupServer({
437
+ addRoutes: (router, options) => {
438
+ addGptRoutes(router, {aiService: errService, openApiOptions: options});
439
+ },
440
+ skipListen: true,
441
+ userModel: UserModel,
442
+ });
443
+ const agent = await authAsUser(errApp, "notAdmin");
444
+ const res = await agent
445
+ .post("/gpt/prompt")
446
+ .send({prompt: "Hi"})
447
+ .buffer(true)
448
+ .parse(sseCollect);
449
+ expect(res.status).toBe(200);
450
+ const body = (res as SseResponse).body;
451
+ expect(body).toContain("error");
452
+ });
453
+
454
+ it("merges MCP tools and tolerates MCP getTools failures", async () => {
455
+ const mcpService = {
456
+ getTools: mock(async () => {
457
+ throw new Error("mcp unavailable");
458
+ }),
459
+ } as unknown as MCPService;
460
+ const customApp = setupServer({
461
+ addRoutes: (router, options) => {
462
+ addGptRoutes(router, {aiService, mcpService, openApiOptions: options});
463
+ },
464
+ skipListen: true,
465
+ userModel: UserModel,
466
+ });
467
+ const agent = await authAsUser(customApp, "notAdmin");
468
+ const res = await agent
469
+ .post("/gpt/prompt")
470
+ .send({prompt: "Hi"})
471
+ .buffer(true)
472
+ .parse(sseCollect);
473
+ expect(res.status).toBe(200);
474
+ expect(mcpService.getTools).toHaveBeenCalled();
475
+ });
476
+
477
+ it("merges MCP tools on success", async () => {
478
+ const mcpService = {
479
+ getTools: mock(async () => ({
480
+ search: {description: "web search"},
481
+ })),
482
+ } as unknown as MCPService;
483
+ const customApp = setupServer({
484
+ addRoutes: (router, options) => {
485
+ addGptRoutes(router, {aiService, mcpService, openApiOptions: options});
486
+ },
487
+ skipListen: true,
488
+ userModel: UserModel,
489
+ });
490
+ const agent = await authAsUser(customApp, "notAdmin");
491
+ const res = await agent
492
+ .post("/gpt/prompt")
493
+ .send({prompt: "Hi"})
494
+ .buffer(true)
495
+ .parse(sseCollect);
496
+ expect(res.status).toBe(200);
497
+ });
498
+
499
+ it("generates titles via a dedicated title model factory", async () => {
500
+ const mainFn = mock(() => createMockModel("main") as unknown as LanguageModel);
501
+ const titleFn = mock(() => createMockModel("title-model") as unknown as LanguageModel);
502
+ // createServerModelFn routes by id so we can differentiate main vs. title model.
503
+ const createServerModelFn = mock((modelId?: string) => {
504
+ if (modelId === "title-model") {
505
+ return titleFn();
506
+ }
507
+ return mainFn();
508
+ });
509
+ const customApp = setupServer({
510
+ addRoutes: (router, options) => {
511
+ addGptRoutes(router, {
512
+ createServerModelFn,
513
+ openApiOptions: options,
514
+ titleModelId: "title-model",
515
+ });
516
+ },
517
+ skipListen: true,
518
+ userModel: UserModel,
519
+ });
520
+ const agent = await authAsUser(customApp, "notAdmin");
521
+ const res = await agent
522
+ .post("/gpt/prompt")
523
+ .send({model: "main", prompt: "Hello"})
524
+ .buffer(true)
525
+ .parse(sseCollect);
526
+ expect(res.status).toBe(200);
527
+ expect(createServerModelFn).toHaveBeenCalledWith("title-model");
528
+ });
529
+
530
+ it("generates titles via per-request api key title model", async () => {
531
+ const createModelFn = mock((_apiKey: string, _modelId?: string) => {
532
+ return createMockModel() as unknown as LanguageModel;
533
+ });
534
+ const customApp = setupServer({
535
+ addRoutes: (router, options) => {
536
+ addGptRoutes(router, {
537
+ createModelFn,
538
+ openApiOptions: options,
539
+ titleModelId: "title-small",
540
+ });
541
+ },
542
+ skipListen: true,
543
+ userModel: UserModel,
544
+ });
545
+ const agent = await authAsUser(customApp, "notAdmin");
546
+ const res = await agent
547
+ .post("/gpt/prompt")
548
+ .set("x-ai-api-key", "user-key")
549
+ .send({prompt: "Hello"})
550
+ .buffer(true)
551
+ .parse(sseCollect);
552
+ expect(res.status).toBe(200);
553
+ // First call is the main model, subsequent call should be the title model.
554
+ expect(createModelFn).toHaveBeenCalled();
555
+ const titleCalls = (
556
+ createModelFn as unknown as {mock: {calls: Array<[string, string?]>}}
557
+ ).mock.calls.filter((args) => args[1] === "title-small");
558
+ expect(titleCalls.length).toBeGreaterThanOrEqual(1);
559
+ });
560
+
561
+ it("applies image provider options for image-capable models", async () => {
562
+ const imageService = new AIService({
563
+ model: createImageModel() as unknown as LanguageModel,
564
+ });
565
+ const imgApp = setupServer({
566
+ addRoutes: (router, options) => {
567
+ addGptRoutes(router, {
568
+ aiService: imageService,
569
+ openApiOptions: options,
570
+ tools: {
571
+ dummy: {description: "ignored for image models"} as unknown as Tool,
572
+ },
573
+ });
574
+ },
575
+ skipListen: true,
576
+ userModel: UserModel,
577
+ });
578
+ const agent = await authAsUser(imgApp, "notAdmin");
579
+ const res = await agent
580
+ .post("/gpt/prompt")
581
+ .send({prompt: "Draw a cat"})
582
+ .buffer(true)
583
+ .parse(sseCollect);
584
+ expect(res.status).toBe(200);
585
+ });
586
+
587
+ it("uses the langfuse system prompt when configured", async () => {
588
+ const customApp = setupServer({
589
+ addRoutes: (router, options) => {
590
+ addGptRoutes(router, {
591
+ aiService,
592
+ langfuseSystemPromptName: "missing-prompt-name",
593
+ openApiOptions: options,
594
+ });
595
+ },
596
+ skipListen: true,
597
+ userModel: UserModel,
598
+ });
599
+ const agent = await authAsUser(customApp, "notAdmin");
600
+ const res = await agent
601
+ .post("/gpt/prompt")
602
+ .send({prompt: "Hi"})
603
+ .buffer(true)
604
+ .parse(sseCollect);
605
+ expect(res.status).toBe(200);
606
+ });
607
+
608
+ it("forwards model tool-call and tool-result stream events via SSE", async () => {
609
+ const toolModel = {
610
+ doGenerate: mock(async () => ({
611
+ content: [{text: "ok", type: "text" as const}],
612
+ finishReason: "stop" as const,
613
+ usage: {inputTokens: 1, outputTokens: 1},
614
+ })),
615
+ doStream: mock(async () => ({
616
+ stream: new ReadableStream({
617
+ start(controller) {
618
+ controller.enqueue({id: "t1", type: "text-start" as const});
619
+ controller.enqueue({
620
+ delta: "Let me search.",
621
+ id: "t1",
622
+ type: "text-delta" as const,
623
+ });
624
+ controller.enqueue({id: "t1", type: "text-end" as const});
625
+ controller.enqueue({
626
+ input: {q: "hello"},
627
+ providerExecuted: true,
628
+ toolCallId: "tc1",
629
+ toolName: "search",
630
+ type: "tool-call" as const,
631
+ });
632
+ controller.enqueue({
633
+ output: {
634
+ fileData: "data:application/pdf;base64,AAAA",
635
+ filename: "result.pdf",
636
+ mimeType: "application/pdf",
637
+ results: ["item1"],
638
+ },
639
+ providerExecuted: true,
640
+ toolCallId: "tc1",
641
+ toolName: "search",
642
+ type: "tool-result" as const,
643
+ });
644
+ controller.enqueue({
645
+ finishReason: "stop" as const,
646
+ type: "finish" as const,
647
+ usage: {inputTokens: 3, outputTokens: 3},
648
+ });
649
+ controller.close();
650
+ },
651
+ }),
652
+ })),
653
+ modelId: "mock-tools",
654
+ provider: "mock-provider",
655
+ specificationVersion: "v2" as const,
656
+ supportedUrls: {},
657
+ };
658
+ const toolApp = setupServer({
659
+ addRoutes: (router, options) => {
660
+ addGptRoutes(router, {
661
+ aiService: new AIService({model: toolModel as unknown as LanguageModel}),
662
+ openApiOptions: options,
663
+ tools: {
664
+ search: {description: "Web search"} as unknown as Tool,
665
+ },
666
+ });
667
+ },
668
+ skipListen: true,
669
+ userModel: UserModel,
670
+ });
671
+ const agent = await authAsUser(toolApp, "notAdmin");
672
+ const res = await agent
673
+ .post("/gpt/prompt")
674
+ .send({prompt: "search"})
675
+ .buffer(true)
676
+ .parse(sseCollect);
677
+ expect(res.status).toBe(200);
678
+ const body = (res as SseResponse).body;
679
+ expect(body).toContain("toolCall");
680
+ expect(body).toContain("toolResult");
681
+ });
682
+
683
+ it("forwards mid-stream error parts as SSE error events", async () => {
684
+ const errEventModel = {
685
+ doGenerate: mock(async () => ({
686
+ content: [{text: "ok", type: "text" as const}],
687
+ finishReason: "stop" as const,
688
+ usage: {inputTokens: 1, outputTokens: 1},
689
+ })),
690
+ doStream: mock(async () => ({
691
+ stream: new ReadableStream({
692
+ start(controller) {
693
+ controller.enqueue({id: "t1", type: "text-start" as const});
694
+ controller.enqueue({delta: "partial", id: "t1", type: "text-delta" as const});
695
+ controller.enqueue({id: "t1", type: "text-end" as const});
696
+ controller.enqueue({
697
+ error: new Error("mid-stream failure"),
698
+ type: "error" as const,
699
+ });
700
+ controller.enqueue({
701
+ finishReason: "stop" as const,
702
+ type: "finish" as const,
703
+ usage: {inputTokens: 1, outputTokens: 1},
704
+ });
705
+ controller.close();
706
+ },
707
+ }),
708
+ })),
709
+ modelId: "mock-error-event",
710
+ provider: "mock-provider",
711
+ specificationVersion: "v2" as const,
712
+ supportedUrls: {},
713
+ };
714
+ const errApp = setupServer({
715
+ addRoutes: (router, options) => {
716
+ addGptRoutes(router, {
717
+ aiService: new AIService({
718
+ model: errEventModel as unknown as LanguageModel,
719
+ }),
720
+ openApiOptions: options,
721
+ });
722
+ },
723
+ skipListen: true,
724
+ userModel: UserModel,
725
+ });
726
+ const agent = await authAsUser(errApp, "notAdmin");
727
+ const res = await agent
728
+ .post("/gpt/prompt")
729
+ .send({prompt: "Hi"})
730
+ .buffer(true)
731
+ .parse(sseCollect);
732
+ expect(res.status).toBe(200);
733
+ const body = (res as SseResponse).body;
734
+ expect(body).toContain("mid-stream failure");
735
+ });
736
+
737
+ it("swallows title generation errors without failing the stream", async () => {
738
+ const throwingTitleModel = {
739
+ doGenerate: mock(async () => {
740
+ throw new Error("title generate failed");
741
+ }),
742
+ doStream: mock(async () => ({
743
+ stream: new ReadableStream({
744
+ start(controller) {
745
+ controller.enqueue({id: "t1", type: "text-start" as const});
746
+ controller.enqueue({delta: "ok", id: "t1", type: "text-delta" as const});
747
+ controller.enqueue({id: "t1", type: "text-end" as const});
748
+ controller.enqueue({
749
+ finishReason: "stop" as const,
750
+ type: "finish" as const,
751
+ usage: {inputTokens: 1, outputTokens: 1},
752
+ });
753
+ controller.close();
754
+ },
755
+ }),
756
+ })),
757
+ modelId: "title-throw",
758
+ provider: "mock-provider",
759
+ specificationVersion: "v2" as const,
760
+ supportedUrls: {},
761
+ };
762
+ const createServerModelFn = mock((id?: string) => {
763
+ if (id === "title-throw-model") {
764
+ return throwingTitleModel as unknown as LanguageModel;
765
+ }
766
+ return createMockModel() as unknown as LanguageModel;
767
+ });
768
+ const customApp = setupServer({
769
+ addRoutes: (router, options) => {
770
+ addGptRoutes(router, {
771
+ createServerModelFn,
772
+ openApiOptions: options,
773
+ titleModelId: "title-throw-model",
774
+ });
775
+ },
776
+ skipListen: true,
777
+ userModel: UserModel,
778
+ });
779
+ const agent = await authAsUser(customApp, "notAdmin");
780
+ const res = await agent
781
+ .post("/gpt/prompt")
782
+ .send({model: "main-model", prompt: "Give me a title"})
783
+ .buffer(true)
784
+ .parse(sseCollect);
785
+ expect(res.status).toBe(200);
786
+ expect(createServerModelFn).toHaveBeenCalledWith("title-throw-model");
787
+ });
788
+
789
+ it("associates a projectId with an existing history when provided later", async () => {
790
+ const notAdmin = (await UserModel.findOne({
791
+ email: "notAdmin@example.com",
792
+ })) as mongoose.Document & {_id: mongoose.Types.ObjectId};
793
+ const project = await Project.create({name: "Late Project", userId: notAdmin._id});
794
+ const history = await GptHistory.create({
795
+ prompts: [{text: "Hi", type: "user"}],
796
+ userId: notAdmin._id,
797
+ });
798
+ const agent = await authAsUser(app, "notAdmin");
799
+ const res = await agent
800
+ .post("/gpt/prompt")
801
+ .send({
802
+ historyId: history._id.toString(),
803
+ projectId: project._id.toString(),
804
+ prompt: "Follow up",
805
+ })
806
+ .buffer(true)
807
+ .parse(sseCollect);
808
+ expect(res.status).toBe(200);
809
+ const updated = await GptHistory.findById(history._id);
810
+ expect(updated?.projectId?.toString()).toBe(project._id.toString());
811
+ await Project.deleteMany({});
812
+ });
813
+
814
+ it("uses per-request tools via createRequestTools", async () => {
815
+ const createRequestTools = mock(() => ({
816
+ localLookup: {description: "Per-request tool"} as unknown as Tool,
817
+ }));
818
+ const customApp = setupServer({
819
+ addRoutes: (router, options) => {
820
+ addGptRoutes(router, {
821
+ aiService,
822
+ createRequestTools,
823
+ openApiOptions: options,
824
+ });
825
+ },
826
+ skipListen: true,
827
+ userModel: UserModel,
828
+ });
829
+ const agent = await authAsUser(customApp, "notAdmin");
830
+ const res = await agent
831
+ .post("/gpt/prompt")
832
+ .send({prompt: "Hi"})
833
+ .buffer(true)
834
+ .parse(sseCollect);
835
+ expect(res.status).toBe(200);
836
+ expect(createRequestTools).toHaveBeenCalled();
837
+ });
838
+ });
839
+
840
+ describe("GPT history rating route", () => {
841
+ it("rejects non-numeric promptIndex", async () => {
842
+ const notAdmin = (await UserModel.findOne({
843
+ email: "notAdmin@example.com",
844
+ })) as mongoose.Document & {_id: mongoose.Types.ObjectId};
845
+ const h = await GptHistory.create({
846
+ prompts: [{text: "hi", type: "user"}],
847
+ userId: notAdmin._id,
848
+ });
849
+ const agent = await authAsUser(app, "notAdmin");
850
+ const res = await agent
851
+ .patch(`/gpt/histories/${h._id.toString()}/rating`)
852
+ .send({promptIndex: "bad", rating: "up"});
853
+ expect(res.status).toBe(400);
854
+ });
855
+
856
+ it("rejects invalid rating value", async () => {
857
+ const notAdmin = (await UserModel.findOne({
858
+ email: "notAdmin@example.com",
859
+ })) as mongoose.Document & {_id: mongoose.Types.ObjectId};
860
+ const h = await GptHistory.create({
861
+ prompts: [{text: "hi", type: "user"}],
862
+ userId: notAdmin._id,
863
+ });
864
+ const agent = await authAsUser(app, "notAdmin");
865
+ const res = await agent
866
+ .patch(`/gpt/histories/${h._id.toString()}/rating`)
867
+ .send({promptIndex: 0, rating: "maybe"});
868
+ expect(res.status).toBe(400);
869
+ });
870
+
871
+ it("returns 404 for missing history", async () => {
872
+ const agent = await authAsUser(app, "notAdmin");
873
+ const id = new mongoose.Types.ObjectId().toString();
874
+ const res = await agent
875
+ .patch(`/gpt/histories/${id}/rating`)
876
+ .send({promptIndex: 0, rating: "up"});
877
+ expect(res.status).toBe(404);
878
+ });
879
+
880
+ it("returns 403 when not owner", async () => {
881
+ const otherId = new mongoose.Types.ObjectId();
882
+ const h = await GptHistory.create({
883
+ prompts: [{text: "hi", type: "user"}],
884
+ userId: otherId,
885
+ });
886
+ const agent = await authAsUser(app, "notAdmin");
887
+ const res = await agent
888
+ .patch(`/gpt/histories/${h._id.toString()}/rating`)
889
+ .send({promptIndex: 0, rating: "up"});
890
+ expect(res.status).toBe(403);
891
+ });
892
+
893
+ it("rejects out-of-range promptIndex", async () => {
894
+ const notAdmin = (await UserModel.findOne({
895
+ email: "notAdmin@example.com",
896
+ })) as mongoose.Document & {_id: mongoose.Types.ObjectId};
897
+ const h = await GptHistory.create({
898
+ prompts: [{text: "hi", type: "user"}],
899
+ userId: notAdmin._id,
900
+ });
901
+ const agent = await authAsUser(app, "notAdmin");
902
+ const res = await agent
903
+ .patch(`/gpt/histories/${h._id.toString()}/rating`)
904
+ .send({promptIndex: 99, rating: "up"});
905
+ expect(res.status).toBe(400);
906
+ });
907
+
908
+ it("sets rating on a prompt", async () => {
909
+ const notAdmin = (await UserModel.findOne({
910
+ email: "notAdmin@example.com",
911
+ })) as mongoose.Document & {_id: mongoose.Types.ObjectId};
912
+ const h = await GptHistory.create({
913
+ prompts: [
914
+ {text: "hi", type: "user"},
915
+ {text: "hello!", type: "assistant"},
916
+ ],
917
+ userId: notAdmin._id,
918
+ });
919
+ const agent = await authAsUser(app, "notAdmin");
920
+ const res = await agent
921
+ .patch(`/gpt/histories/${h._id.toString()}/rating`)
922
+ .send({promptIndex: 1, rating: "up"});
923
+ expect(res.status).toBe(200);
924
+ expect(res.body.data.rating).toBe("up");
925
+ });
926
+
927
+ it("clears rating when null is passed", async () => {
928
+ const notAdmin = (await UserModel.findOne({
929
+ email: "notAdmin@example.com",
930
+ })) as mongoose.Document & {_id: mongoose.Types.ObjectId};
931
+ const h = await GptHistory.create({
932
+ prompts: [
933
+ {text: "hi", type: "user"},
934
+ {rating: "up", text: "hello!", type: "assistant"},
935
+ ],
936
+ userId: notAdmin._id,
937
+ });
938
+ const agent = await authAsUser(app, "notAdmin");
939
+ const res = await agent
940
+ .patch(`/gpt/histories/${h._id.toString()}/rating`)
941
+ .send({promptIndex: 1, rating: null});
942
+ expect(res.status).toBe(200);
943
+ expect(res.body.data.rating).toBeNull();
944
+ });
945
+ });
946
+
947
+ describe("GPT tools route", () => {
948
+ it("lists built-in tools from route config", async () => {
949
+ const customApp = setupServer({
950
+ addRoutes: (router, options) => {
951
+ addGptRoutes(router, {
952
+ aiService,
953
+ openApiOptions: options,
954
+ tools: {echo: {description: "Echo tool"} as unknown as Tool},
955
+ });
956
+ },
957
+ skipListen: true,
958
+ userModel: UserModel,
959
+ });
960
+ const agent = await authAsUser(customApp, "notAdmin");
961
+ const res = await agent.get("/gpt/tools");
962
+ expect(res.status).toBe(200);
963
+ expect(res.body.data).toEqual([expect.objectContaining({name: "echo", source: "builtin"})]);
964
+ });
965
+
966
+ it("includes per-request tools when createRequestTools is set", async () => {
967
+ const createRequestTools = mock(() => ({
968
+ now: {description: "Current time"} as unknown as Tool,
969
+ }));
970
+ const customApp = setupServer({
971
+ addRoutes: (router, options) => {
972
+ addGptRoutes(router, {aiService, createRequestTools, openApiOptions: options});
973
+ },
974
+ skipListen: true,
975
+ userModel: UserModel,
976
+ });
977
+ const agent = await authAsUser(customApp, "notAdmin");
978
+ const res = await agent.get("/gpt/tools");
979
+ expect(res.status).toBe(200);
980
+ expect(res.body.data).toEqual([expect.objectContaining({name: "now"})]);
981
+ });
982
+
983
+ it("includes MCP tools when mcpService is set", async () => {
984
+ const mcpService = {
985
+ getTools: mock(async () => ({search: {description: "web search"}})),
986
+ } as unknown as MCPService;
987
+ const customApp = setupServer({
988
+ addRoutes: (router, options) => {
989
+ addGptRoutes(router, {aiService, mcpService, openApiOptions: options});
990
+ },
991
+ skipListen: true,
992
+ userModel: UserModel,
993
+ });
994
+ const agent = await authAsUser(customApp, "notAdmin");
995
+ const res = await agent.get("/gpt/tools");
996
+ expect(res.status).toBe(200);
997
+ expect(res.body.data).toEqual([expect.objectContaining({name: "search", source: "mcp"})]);
998
+ });
999
+
1000
+ it("tolerates mcpService.getTools errors", async () => {
1001
+ const mcpService = {
1002
+ getTools: mock(async () => {
1003
+ throw new Error("boom");
1004
+ }),
1005
+ } as unknown as MCPService;
1006
+ const customApp = setupServer({
1007
+ addRoutes: (router, options) => {
1008
+ addGptRoutes(router, {aiService, mcpService, openApiOptions: options});
1009
+ },
1010
+ skipListen: true,
1011
+ userModel: UserModel,
1012
+ });
1013
+ const agent = await authAsUser(customApp, "notAdmin");
1014
+ const res = await agent.get("/gpt/tools");
1015
+ expect(res.status).toBe(200);
1016
+ expect(res.body.data).toEqual([]);
1017
+ });
148
1018
  });
149
1019
 
150
1020
  describe("AI Requests Explorer route", () => {
@@ -172,5 +1042,122 @@ describe("AI Routes", () => {
172
1042
  expect(res.body.total).toBeGreaterThanOrEqual(1);
173
1043
  expect(res.body.page).toBe(1);
174
1044
  });
1045
+
1046
+ it("filters by requestType query parameter", async () => {
1047
+ await AIRequest.create({
1048
+ aiModel: "gpt-4",
1049
+ prompt: "remix prompt",
1050
+ requestType: "remix",
1051
+ response: "remix response",
1052
+ });
1053
+ await AIRequest.create({
1054
+ aiModel: "gpt-4",
1055
+ prompt: "general prompt",
1056
+ requestType: "general",
1057
+ response: "general response",
1058
+ });
1059
+
1060
+ const agent = await authAsUser(app, "admin");
1061
+ const res = await agent.get("/aiRequestsExplorer?requestType=remix");
1062
+
1063
+ expect(res.status).toBe(200);
1064
+ expect(res.body.total).toBe(1);
1065
+ expect(res.body.data[0].requestType).toBe("remix");
1066
+ });
1067
+
1068
+ it("filters by model query parameter", async () => {
1069
+ await AIRequest.create({
1070
+ aiModel: "gpt-4",
1071
+ prompt: "p1",
1072
+ requestType: "general",
1073
+ response: "r1",
1074
+ });
1075
+ await AIRequest.create({
1076
+ aiModel: "claude-3",
1077
+ prompt: "p2",
1078
+ requestType: "general",
1079
+ response: "r2",
1080
+ });
1081
+
1082
+ const agent = await authAsUser(app, "admin");
1083
+ const res = await agent.get("/aiRequestsExplorer?model=claude-3");
1084
+
1085
+ expect(res.status).toBe(200);
1086
+ expect(res.body.total).toBe(1);
1087
+ expect(res.body.data[0].aiModel).toBe("claude-3");
1088
+ });
1089
+
1090
+ it("filters by startDate query parameter", async () => {
1091
+ const old = await AIRequest.create({
1092
+ aiModel: "gpt-4",
1093
+ prompt: "old",
1094
+ requestType: "general",
1095
+ response: "r",
1096
+ });
1097
+ await AIRequest.updateOne({_id: old._id}, {created: new Date("2020-01-01T00:00:00Z")});
1098
+
1099
+ await AIRequest.create({
1100
+ aiModel: "gpt-4",
1101
+ prompt: "new",
1102
+ requestType: "general",
1103
+ response: "r",
1104
+ });
1105
+
1106
+ const agent = await authAsUser(app, "admin");
1107
+ const res = await agent.get("/aiRequestsExplorer?startDate=2024-01-01T00:00:00Z");
1108
+
1109
+ expect(res.status).toBe(200);
1110
+ // Only the recently-created record should be returned
1111
+ expect(res.body.total).toBe(1);
1112
+ });
1113
+
1114
+ it("filters by endDate query parameter", async () => {
1115
+ const old = await AIRequest.create({
1116
+ aiModel: "gpt-4",
1117
+ prompt: "old",
1118
+ requestType: "general",
1119
+ response: "r",
1120
+ });
1121
+ await AIRequest.updateOne({_id: old._id}, {created: new Date("2020-01-01T00:00:00Z")});
1122
+
1123
+ await AIRequest.create({
1124
+ aiModel: "gpt-4",
1125
+ prompt: "new",
1126
+ requestType: "general",
1127
+ response: "r",
1128
+ });
1129
+
1130
+ const agent = await authAsUser(app, "admin");
1131
+ const res = await agent.get("/aiRequestsExplorer?endDate=2021-01-01T00:00:00Z");
1132
+
1133
+ expect(res.status).toBe(200);
1134
+ expect(res.body.total).toBe(1);
1135
+ });
1136
+
1137
+ it("filters by startDate and endDate range", async () => {
1138
+ const a = await AIRequest.create({
1139
+ aiModel: "gpt-4",
1140
+ prompt: "a",
1141
+ requestType: "general",
1142
+ response: "r",
1143
+ });
1144
+ await AIRequest.updateOne({_id: a._id}, {created: new Date("2022-06-01T00:00:00Z")});
1145
+
1146
+ const b = await AIRequest.create({
1147
+ aiModel: "gpt-4",
1148
+ prompt: "b",
1149
+ requestType: "general",
1150
+ response: "r",
1151
+ });
1152
+ await AIRequest.updateOne({_id: b._id}, {created: new Date("2020-01-01T00:00:00Z")});
1153
+
1154
+ const agent = await authAsUser(app, "admin");
1155
+ const res = await agent.get(
1156
+ "/aiRequestsExplorer?startDate=2022-01-01T00:00:00Z&endDate=2023-01-01T00:00:00Z"
1157
+ );
1158
+
1159
+ expect(res.status).toBe(200);
1160
+ expect(res.body.total).toBe(1);
1161
+ });
175
1162
  });
176
1163
  });