@terreno/ai 57.7.0 → 57.9.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.
@@ -3,6 +3,7 @@ import {TerrenoApp} from "@terreno/api";
3
3
  import {jsonSchema, type LanguageModel, type Tool, tool} from "ai";
4
4
  import {assert} from "chai";
5
5
  import type express from "express";
6
+ import {DateTime} from "luxon";
6
7
  import mongoose from "mongoose";
7
8
  import type supertest from "supertest";
8
9
 
@@ -10,6 +11,7 @@ import {AIRequest} from "../models/aiRequest";
10
11
  import {GptHistory} from "../models/gptHistory";
11
12
  import {Project} from "../models/project";
12
13
  import {AIService} from "../service/aiService";
14
+ import type {FileStorageService} from "../service/fileStorage";
13
15
  import type {MCPService} from "../service/mcpService";
14
16
  import {authAsUser, ensureTestUsers, UserModel} from "../tests/helpers";
15
17
  import {addAiRequestsExplorerRoutes} from "./aiRequestsExplorer";
@@ -95,7 +97,37 @@ const createImageModel = () => ({
95
97
  },
96
98
  }),
97
99
  })),
98
- modelId: "gemini-2.5-flash-image",
100
+ modelId: "gemini-3-pro-image",
101
+ provider: "mock-provider",
102
+ specificationVersion: "v2" as const,
103
+ supportedUrls: {},
104
+ });
105
+
106
+ // Mock model that emits one generated image and no text
107
+ const createImageOnlyModel = () => ({
108
+ doGenerate: mock(async () => ({
109
+ content: [],
110
+ finishReason: "stop" as const,
111
+ usage: {inputTokens: 2, outputTokens: 2},
112
+ })),
113
+ doStream: mock(async () => ({
114
+ stream: new ReadableStream({
115
+ start(controller) {
116
+ controller.enqueue({
117
+ data: "aGVsbG8=",
118
+ mediaType: "image/png",
119
+ type: "file" as const,
120
+ });
121
+ controller.enqueue({
122
+ finishReason: "stop" as const,
123
+ type: "finish" as const,
124
+ usage: {inputTokens: 2, outputTokens: 2},
125
+ });
126
+ controller.close();
127
+ },
128
+ }),
129
+ })),
130
+ modelId: "gemini-3-pro-image",
99
131
  provider: "mock-provider",
100
132
  specificationVersion: "v2" as const,
101
133
  supportedUrls: {},
@@ -315,11 +347,11 @@ describe("AI Routes", () => {
315
347
  const agent = await authAsUser(customApp, "notAdmin");
316
348
  const res = await agent
317
349
  .post("/gpt/prompt")
318
- .send({model: "gemini-2.5-pro", prompt: "Hi"})
350
+ .send({model: "gemini-3.1-pro-preview", prompt: "Hi"})
319
351
  .buffer(true)
320
352
  .parse(sseCollect);
321
353
  expect(res.status).toBe(200);
322
- expect(createServerModelFn).toHaveBeenCalledWith("gemini-2.5-pro");
354
+ expect(createServerModelFn).toHaveBeenCalledWith("gemini-3.1-pro-preview");
323
355
  });
324
356
 
325
357
  it("falls back to default aiService when createServerModelFn returns null", async () => {
@@ -482,6 +514,34 @@ describe("AI Routes", () => {
482
514
  );
483
515
  });
484
516
 
517
+ it("emits and saves a generated image once, with no placeholder text", async () => {
518
+ const imageService = new AIService({
519
+ model: createImageOnlyModel() as unknown as LanguageModel,
520
+ });
521
+ const imgApp = new TerrenoApp({
522
+ configureApp: (router, options) => {
523
+ addGptRoutes(router, {aiService: imageService, openApiOptions: options});
524
+ },
525
+ skipListen: true,
526
+ userModel: UserModel,
527
+ }).build();
528
+ const agent = await authAsUser(imgApp, "notAdmin");
529
+ const res = await agent
530
+ .post("/gpt/prompt")
531
+ .send({prompt: "Make an image"})
532
+ .buffer(true)
533
+ .parse(sseCollect);
534
+ expect(res.status).toBe(200);
535
+ const body = (res as SseResponse).body;
536
+ expect(body.match(/"image":\{/g)?.length).toBe(1);
537
+
538
+ const history = await GptHistory.findExactlyOne({"prompts.text": "Make an image"});
539
+ const assistant = history.prompts.find((p) => p.type === "assistant");
540
+ expect(assistant?.text).toBe("");
541
+ expect(assistant?.content).toHaveLength(1);
542
+ expect(assistant?.content?.[0]?.url).toBe("data:image/png;base64,aGVsbG8=");
543
+ });
544
+
485
545
  it("writes an SSE error event when the model stream throws", async () => {
486
546
  const errService = new AIService({
487
547
  model: createErrorModel() as unknown as LanguageModel,
@@ -1184,6 +1244,455 @@ describe("AI Routes", () => {
1184
1244
  });
1185
1245
  });
1186
1246
 
1247
+ describe("GPT Prompt durable attachments and resumable streams", () => {
1248
+ const getNotAdminId = async (): Promise<mongoose.Types.ObjectId> => {
1249
+ const notAdmin = (await UserModel.findOne({
1250
+ email: "notAdmin@example.com",
1251
+ })) as mongoose.Document & {_id: mongoose.Types.ObjectId};
1252
+ return notAdmin._id;
1253
+ };
1254
+
1255
+ const createFakeStorage = () => {
1256
+ const upload = mock(
1257
+ async ({filename, mimeType}: {buffer: Buffer; filename: string; mimeType: string}) => ({
1258
+ filename,
1259
+ gcsKey: `uploads/test/${filename}`,
1260
+ mimeType,
1261
+ size: 5,
1262
+ url: `https://storage.googleapis.com/bucket/uploads/test/${filename}`,
1263
+ })
1264
+ );
1265
+ const getSignedUrl = mock(async (gcsKey: string) => `https://signed.example.com/${gcsKey}`);
1266
+ return {
1267
+ getSignedUrl,
1268
+ service: {getSignedUrl, upload} as unknown as FileStorageService,
1269
+ upload,
1270
+ };
1271
+ };
1272
+
1273
+ const textOnlyStream = (capture?: (options: StreamTextOptions) => void) => {
1274
+ streamTextOverride = (streamOptions) => {
1275
+ capture?.(streamOptions);
1276
+ return {
1277
+ files: Promise.resolve([]),
1278
+ fullStream: (async function* () {
1279
+ yield {type: "start-step"};
1280
+ yield {text: "Reply", type: "text-delta"};
1281
+ yield {type: "finish-step"};
1282
+ })(),
1283
+ } as unknown as StreamTextResult;
1284
+ };
1285
+ };
1286
+
1287
+ const buildApp = (extra: Partial<Parameters<typeof addGptRoutes>[1]> = {}) =>
1288
+ new TerrenoApp({
1289
+ configureApp: (router, options) => {
1290
+ addGptRoutes(router, {aiService, openApiOptions: options, ...extra});
1291
+ },
1292
+ skipListen: true,
1293
+ userModel: UserModel,
1294
+ }).build();
1295
+
1296
+ it("rejects attachments when file uploads are disabled", async () => {
1297
+ const gated = buildApp({fileUploadsEnabled: async () => false});
1298
+ const agent = await authAsUser(gated, "notAdmin");
1299
+ const res = await agent.post("/gpt/prompt").send({
1300
+ attachments: [{mimeType: "image/png", type: "image", url: "data:image/png;base64,aGk="}],
1301
+ prompt: "What is this?",
1302
+ });
1303
+ expect(res.status).toBe(403);
1304
+ expect(res.body.title).toBe("File uploads are disabled");
1305
+ expect(await GptHistory.countDocuments({})).toBe(0);
1306
+ });
1307
+
1308
+ it("rejects client-only attachment URLs before streaming", async () => {
1309
+ const agent = await authAsUser(app, "notAdmin");
1310
+ const res = await agent.post("/gpt/prompt").send({
1311
+ attachments: [{mimeType: "image/png", type: "image", url: "blob:http://localhost/abc"}],
1312
+ prompt: "What is this?",
1313
+ });
1314
+ expect(res.status).toBeGreaterThanOrEqual(400);
1315
+ expect(res.body.title).toContain("Attachment URL must be an http(s) or data: URL");
1316
+ expect(await GptHistory.countDocuments({})).toBe(0);
1317
+ });
1318
+
1319
+ it("rejects file: attachment URLs", async () => {
1320
+ const agent = await authAsUser(app, "notAdmin");
1321
+ const res = await agent.post("/gpt/prompt").send({
1322
+ attachments: [
1323
+ {filename: "a.pdf", mimeType: "application/pdf", type: "file", url: "file:///a.pdf"},
1324
+ ],
1325
+ prompt: "Read this",
1326
+ });
1327
+ expect(res.status).toBeGreaterThanOrEqual(400);
1328
+ expect(res.body.title).toContain("Attachment URL");
1329
+ });
1330
+
1331
+ it("uploads data: attachments and stores a durable reference", async () => {
1332
+ const storage = createFakeStorage();
1333
+ let modelMessages: unknown;
1334
+ textOnlyStream((streamOptions) => {
1335
+ modelMessages = streamOptions.messages;
1336
+ });
1337
+ const agent = await authAsUser(buildApp({fileStorageService: storage.service}), "notAdmin");
1338
+ const dataUrl = "data:image/png;base64,aGVsbG8=";
1339
+ const res = await agent
1340
+ .post("/gpt/prompt")
1341
+ .send({
1342
+ attachments: [{filename: "pic.png", mimeType: "image/png", type: "image", url: dataUrl}],
1343
+ prompt: "Describe",
1344
+ })
1345
+ .buffer(true)
1346
+ .parse(sseCollect);
1347
+ expect(res.status).toBe(200);
1348
+
1349
+ expect(storage.upload).toHaveBeenCalledTimes(1);
1350
+ const uploadArgs = storage.upload.mock.calls[0][0];
1351
+ expect(uploadArgs.buffer.toString()).toBe("hello");
1352
+ expect(uploadArgs.mimeType).toBe("image/png");
1353
+
1354
+ const history = await GptHistory.findExactlyOne({"prompts.text": "Describe"});
1355
+ const imagePart = history.prompts[0].content?.find((p) => p.type === "image");
1356
+ expect(imagePart?.gcsKey).toBe("uploads/test/pic.png");
1357
+ expect(imagePart?.url).toBe("https://storage.googleapis.com/bucket/uploads/test/pic.png");
1358
+ // The model still receives the original data for this turn
1359
+ expect(JSON.stringify(modelMessages)).toContain("aGVsbG8=");
1360
+ });
1361
+
1362
+ it("decodes non-base64 data: attachments and names unnamed uploads", async () => {
1363
+ const storage = createFakeStorage();
1364
+ textOnlyStream();
1365
+ const agent = await authAsUser(buildApp({fileStorageService: storage.service}), "notAdmin");
1366
+ await agent
1367
+ .post("/gpt/prompt")
1368
+ .send({
1369
+ attachments: [{mimeType: "text/plain", type: "file", url: "data:text/plain,hi%20there"}],
1370
+ prompt: "Read",
1371
+ })
1372
+ .buffer(true)
1373
+ .parse(sseCollect);
1374
+ const uploadArgs = storage.upload.mock.calls[0][0];
1375
+ expect(uploadArgs.buffer.toString()).toBe("hi there");
1376
+ expect(uploadArgs.filename).toMatch(/^attachment-\d+\.plain$/);
1377
+ });
1378
+
1379
+ it("keeps data: attachments inline when no storage is configured", async () => {
1380
+ textOnlyStream();
1381
+ const agent = await authAsUser(app, "notAdmin");
1382
+ await agent
1383
+ .post("/gpt/prompt")
1384
+ .send({
1385
+ attachments: [
1386
+ {mimeType: "image/png", type: "image", url: "data:image/png;base64,aGVsbG8="},
1387
+ ],
1388
+ prompt: "Inline",
1389
+ })
1390
+ .buffer(true)
1391
+ .parse(sseCollect);
1392
+ const history = await GptHistory.findExactlyOne({"prompts.text": "Inline"});
1393
+ const imagePart = history.prompts[0].content?.find((p) => p.type === "image");
1394
+ expect(imagePart?.url).toBe("data:image/png;base64,aGVsbG8=");
1395
+ expect(imagePart?.gcsKey).toBeUndefined();
1396
+ });
1397
+
1398
+ it("returns 502 when the attachment upload fails", async () => {
1399
+ const failingStorage = {
1400
+ getSignedUrl: mock(async () => ""),
1401
+ upload: mock(async () => {
1402
+ throw new Error("bucket unavailable");
1403
+ }),
1404
+ } as unknown as FileStorageService;
1405
+ const agent = await authAsUser(buildApp({fileStorageService: failingStorage}), "notAdmin");
1406
+ const res = await agent.post("/gpt/prompt").send({
1407
+ attachments: [{mimeType: "image/png", type: "image", url: "data:image/png;base64,aGk="}],
1408
+ prompt: "Fail",
1409
+ });
1410
+ expect(res.status).toBeGreaterThanOrEqual(400);
1411
+ expect(res.body.title).toBe("Attachment upload failed");
1412
+ });
1413
+
1414
+ it("signs stored attachment URLs for earlier turns", async () => {
1415
+ const storage = createFakeStorage();
1416
+ let modelMessages: unknown;
1417
+ textOnlyStream((streamOptions) => {
1418
+ modelMessages = streamOptions.messages;
1419
+ });
1420
+ const history = await GptHistory.create({
1421
+ prompts: [
1422
+ {
1423
+ content: [
1424
+ {text: "Look", type: "text"},
1425
+ {
1426
+ gcsKey: "uploads/test/old.png",
1427
+ mimeType: "image/png",
1428
+ type: "image",
1429
+ url: "https://storage.googleapis.com/bucket/uploads/test/old.png",
1430
+ },
1431
+ ],
1432
+ text: "Look",
1433
+ type: "user",
1434
+ },
1435
+ {status: "complete", text: "Nice", type: "assistant"},
1436
+ ],
1437
+ userId: await getNotAdminId(),
1438
+ });
1439
+ const agent = await authAsUser(buildApp({fileStorageService: storage.service}), "notAdmin");
1440
+ await agent
1441
+ .post("/gpt/prompt")
1442
+ .send({historyId: history._id.toString(), prompt: "And now?"})
1443
+ .buffer(true)
1444
+ .parse(sseCollect);
1445
+ expect(storage.getSignedUrl).toHaveBeenCalledWith("uploads/test/old.png");
1446
+ expect(JSON.stringify(modelMessages)).toContain("signed.example.com/uploads/test/old.png");
1447
+ });
1448
+
1449
+ it("emits a started event and saves the completed reply with its streamId", async () => {
1450
+ textOnlyStream();
1451
+ const agent = await authAsUser(app, "notAdmin");
1452
+ const res = await agent
1453
+ .post("/gpt/prompt")
1454
+ .send({prompt: "Hello"})
1455
+ .buffer(true)
1456
+ .parse(sseCollect);
1457
+ const body = (res as SseResponse).body;
1458
+ const firstEvent = JSON.parse(body.split("\n\n")[0].slice(6));
1459
+ expect(firstEvent.started).toBe(true);
1460
+ expect(typeof firstEvent.historyId).toBe("string");
1461
+ expect(typeof firstEvent.streamId).toBe("string");
1462
+
1463
+ const history = await GptHistory.findExactlyOne({_id: firstEvent.historyId});
1464
+ const assistant = history.prompts.filter((p) => p.type === "assistant");
1465
+ expect(assistant).toHaveLength(1);
1466
+ expect(assistant[0].status).toBe("complete");
1467
+ expect(assistant[0].streamId).toBe(firstEvent.streamId);
1468
+ expect(assistant[0].text).toBe("Reply");
1469
+ });
1470
+
1471
+ it("persists partial assistant text while the reply streams", async () => {
1472
+ let persistedDuringStream: {status?: string; text?: string} | undefined;
1473
+ streamTextOverride = () =>
1474
+ ({
1475
+ files: Promise.resolve([]),
1476
+ fullStream: (async function* () {
1477
+ yield {type: "start-step"};
1478
+ yield {text: "partial ", type: "text-delta"};
1479
+ const deadline = DateTime.now().plus({seconds: 3});
1480
+ while (DateTime.now() < deadline) {
1481
+ const doc = await GptHistory.findOne({"prompts.text": "Stream me"});
1482
+ const reply = doc?.prompts.find((p) => p.type === "assistant");
1483
+ if (reply?.text) {
1484
+ persistedDuringStream = {status: reply.status, text: reply.text};
1485
+ break;
1486
+ }
1487
+ await new Promise((resolve) => setTimeout(resolve, 10));
1488
+ }
1489
+ yield {text: "done", type: "text-delta"};
1490
+ yield {type: "finish-step"};
1491
+ })(),
1492
+ }) as unknown as StreamTextResult;
1493
+ const agent = await authAsUser(buildApp({streamPersistIntervalMs: 10}), "notAdmin");
1494
+ await agent.post("/gpt/prompt").send({prompt: "Stream me"}).buffer(true).parse(sseCollect);
1495
+ expect(persistedDuringStream).toEqual({status: "streaming", text: "partial "});
1496
+ const history = await GptHistory.findExactlyOne({"prompts.text": "Stream me"});
1497
+ expect(history.prompts.find((p) => p.type === "assistant")?.text).toBe("partial done");
1498
+ });
1499
+
1500
+ it("keeps partial text marked as error when the stream fails", async () => {
1501
+ streamTextOverride = () =>
1502
+ ({
1503
+ files: Promise.resolve([]),
1504
+ fullStream: (async function* () {
1505
+ yield {text: "half an ans", type: "text-delta"};
1506
+ throw new Error("provider dropped");
1507
+ })(),
1508
+ }) as unknown as StreamTextResult;
1509
+ const agent = await authAsUser(app, "notAdmin");
1510
+ await agent.post("/gpt/prompt").send({prompt: "Break"}).buffer(true).parse(sseCollect);
1511
+ const history = await GptHistory.findExactlyOne({"prompts.text": "Break"});
1512
+ const assistant = history.prompts.find((p) => p.type === "assistant");
1513
+ expect(assistant?.status).toBe("error");
1514
+ expect(assistant?.text).toBe("half an ans");
1515
+ });
1516
+
1517
+ it("drops the placeholder when the stream fails before any output", async () => {
1518
+ streamTextOverride = () =>
1519
+ ({
1520
+ files: Promise.resolve([]),
1521
+ fullStream: (async function* () {
1522
+ yield {type: "start-step"};
1523
+ throw new Error("provider dropped");
1524
+ })(),
1525
+ }) as unknown as StreamTextResult;
1526
+ const agent = await authAsUser(app, "notAdmin");
1527
+ await agent.post("/gpt/prompt").send({prompt: "Nothing"}).buffer(true).parse(sseCollect);
1528
+ const history = await GptHistory.findExactlyOne({"prompts.text": "Nothing"});
1529
+ expect(history.prompts.map((p) => p.type)).toEqual(["user"]);
1530
+ });
1531
+
1532
+ it("resumes an in-flight reply and finishes when it completes", async () => {
1533
+ const history = await GptHistory.create({
1534
+ prompts: [
1535
+ {text: "Q", type: "user"},
1536
+ {status: "streaming", streamId: "s1", text: "Hello", type: "assistant"},
1537
+ ],
1538
+ userId: await getNotAdminId(),
1539
+ });
1540
+ const agent = await authAsUser(buildApp({streamResumePollIntervalMs: 10}), "notAdmin");
1541
+ setTimeout(() => {
1542
+ void GptHistory.updateOne(
1543
+ {_id: history._id},
1544
+ {
1545
+ $set: {
1546
+ "prompts.1.content": [{mimeType: "image/png", type: "image", url: "https://x/i.png"}],
1547
+ "prompts.1.status": "complete",
1548
+ "prompts.1.text": "Hello world",
1549
+ title: "Greeting",
1550
+ },
1551
+ }
1552
+ ).exec();
1553
+ }, 60);
1554
+ const res = await agent
1555
+ .get(`/gpt/histories/${history._id}/stream`)
1556
+ .buffer(true)
1557
+ .parse(sseCollect);
1558
+ expect(res.status).toBe(200);
1559
+ const events = (res as SseResponse).body
1560
+ .split("\n\n")
1561
+ .filter(Boolean)
1562
+ .map((e) => JSON.parse(e.slice(6)));
1563
+ expect(events[0]).toEqual({historyId: history._id.toString(), resumed: true, streamId: "s1"});
1564
+ expect(events.filter((e) => e.text).map((e) => e.text)).toEqual(["Hello", " world"]);
1565
+ expect(events).toContainEqual({image: {mimeType: "image/png", url: "https://x/i.png"}});
1566
+ expect(events[events.length - 1]).toEqual({
1567
+ done: true,
1568
+ historyId: history._id.toString(),
1569
+ title: "Greeting",
1570
+ });
1571
+ });
1572
+
1573
+ it("skips text the client already shows via offset", async () => {
1574
+ const history = await GptHistory.create({
1575
+ prompts: [
1576
+ {text: "Q", type: "user"},
1577
+ {status: "complete", streamId: "s2", text: "Hello world", type: "assistant"},
1578
+ ],
1579
+ userId: await getNotAdminId(),
1580
+ });
1581
+ const agent = await authAsUser(app, "notAdmin");
1582
+ const res = await agent
1583
+ .get(`/gpt/histories/${history._id}/stream?streamId=s2&offset=5`)
1584
+ .buffer(true)
1585
+ .parse(sseCollect);
1586
+ const body = (res as SseResponse).body;
1587
+ assert.include(body, '"replace":true,"text":"Hello world"');
1588
+ });
1589
+
1590
+ it("replaces a stale client placeholder on the first resume poll", async () => {
1591
+ const history = await GptHistory.create({
1592
+ prompts: [
1593
+ {text: "Q", type: "user"},
1594
+ {status: "complete", streamId: "s2", text: "Answer", type: "assistant"},
1595
+ ],
1596
+ userId: await getNotAdminId(),
1597
+ });
1598
+ const agent = await authAsUser(app, "notAdmin");
1599
+ const res = await agent
1600
+ .get(`/gpt/histories/${history._id}/stream?streamId=s2&offset=12`)
1601
+ .buffer(true)
1602
+ .parse(sseCollect);
1603
+
1604
+ assert.include((res as SseResponse).body, '"replace":true,"text":"Answer"');
1605
+ });
1606
+
1607
+ it("sends a replace event when persisted text is rewritten", async () => {
1608
+ const history = await GptHistory.create({
1609
+ prompts: [
1610
+ {text: "Q", type: "user"},
1611
+ {status: "streaming", streamId: "s3", text: "thinking...", type: "assistant"},
1612
+ ],
1613
+ userId: await getNotAdminId(),
1614
+ });
1615
+ const agent = await authAsUser(buildApp({streamResumePollIntervalMs: 10}), "notAdmin");
1616
+ setTimeout(() => {
1617
+ void GptHistory.updateOne(
1618
+ {_id: history._id},
1619
+ {$set: {"prompts.1.status": "complete", "prompts.1.text": "Answer"}}
1620
+ ).exec();
1621
+ }, 60);
1622
+ const res = await agent
1623
+ .get(`/gpt/histories/${history._id}/stream`)
1624
+ .buffer(true)
1625
+ .parse(sseCollect);
1626
+ expect((res as SseResponse).body).toContain('"replace":true,"text":"Answer"');
1627
+ });
1628
+
1629
+ it("finishes immediately when nothing is streaming", async () => {
1630
+ const history = await GptHistory.create({
1631
+ prompts: [{text: "Q", type: "user"}],
1632
+ userId: await getNotAdminId(),
1633
+ });
1634
+ const agent = await authAsUser(app, "notAdmin");
1635
+ const res = await agent
1636
+ .get(`/gpt/histories/${history._id}/stream`)
1637
+ .buffer(true)
1638
+ .parse(sseCollect);
1639
+ const body = (res as SseResponse).body;
1640
+ expect(body).toContain('"resumed":true');
1641
+ expect(body).toContain('"done":true');
1642
+ });
1643
+
1644
+ it("marks a stale streaming reply as interrupted", async () => {
1645
+ const history = await GptHistory.create({
1646
+ prompts: [
1647
+ {text: "Q", type: "user"},
1648
+ {status: "streaming", streamId: "s4", text: "Par", type: "assistant"},
1649
+ ],
1650
+ userId: await getNotAdminId(),
1651
+ });
1652
+ await new Promise((resolve) => setTimeout(resolve, 20));
1653
+ const agent = await authAsUser(buildApp({streamStaleAfterMs: 5}), "notAdmin");
1654
+ const res = await agent
1655
+ .get(`/gpt/histories/${history._id}/stream`)
1656
+ .buffer(true)
1657
+ .parse(sseCollect);
1658
+ const body = (res as SseResponse).body;
1659
+ expect(body).toContain("interrupted");
1660
+ expect(body).toContain('"done":true');
1661
+ const updated = await GptHistory.findExactlyOne({_id: history._id});
1662
+ expect(updated.prompts[1].status).toBe("error");
1663
+ });
1664
+
1665
+ it("reports an interrupted reply that already ended in error", async () => {
1666
+ const history = await GptHistory.create({
1667
+ prompts: [
1668
+ {text: "Q", type: "user"},
1669
+ {status: "error", streamId: "s5", text: "Par", type: "assistant"},
1670
+ ],
1671
+ userId: await getNotAdminId(),
1672
+ });
1673
+ const agent = await authAsUser(app, "notAdmin");
1674
+ const res = await agent
1675
+ .get(`/gpt/histories/${history._id}/stream?streamId=s5`)
1676
+ .buffer(true)
1677
+ .parse(sseCollect);
1678
+ expect((res as SseResponse).body).toContain("interrupted");
1679
+ });
1680
+
1681
+ it("rejects resume for other users and missing histories", async () => {
1682
+ const history = await GptHistory.create({
1683
+ prompts: [{text: "Q", type: "user"}],
1684
+ userId: await getNotAdminId(),
1685
+ });
1686
+ const admin = await authAsUser(app, "admin");
1687
+ const forbidden = await admin.get(`/gpt/histories/${history._id}/stream`);
1688
+ expect(forbidden.status).toBe(403);
1689
+ const missing = await admin.get(
1690
+ `/gpt/histories/${new mongoose.Types.ObjectId().toString()}/stream`
1691
+ );
1692
+ expect(missing.status).toBe(404);
1693
+ });
1694
+ });
1695
+
1187
1696
  describe("GPT Prompt error handling", () => {
1188
1697
  it("sends an outer SSE error when error handling itself fails after streaming starts", async () => {
1189
1698
  streamTextOverride = () =>
@@ -524,6 +524,45 @@ describe("AIService", () => {
524
524
  expect(messages[2]).toEqual({content: "How are you?", role: "user"});
525
525
  });
526
526
 
527
+ it("should give image-only assistant prompts stand-in text", () => {
528
+ const model = createMockModel();
529
+ const service = new AIService({model: model as unknown as LanguageModel});
530
+
531
+ const messages = service.buildMessages([
532
+ {text: "Make an image", type: "user"},
533
+ {
534
+ content: [
535
+ {mimeType: "image/png", type: "image" as const, url: "data:image/png;base64,aGVsbG8="},
536
+ ],
537
+ text: "",
538
+ type: "assistant",
539
+ },
540
+ ]);
541
+
542
+ expect(messages[1]).toEqual({content: "[Generated image]", role: "assistant"});
543
+ });
544
+
545
+ it("skips streaming placeholders and empty failed replies", () => {
546
+ const model = createMockModel();
547
+ const service = new AIService({model: model as unknown as LanguageModel});
548
+
549
+ const messages = service.buildMessages([
550
+ {text: "First", type: "user"},
551
+ {status: "error", text: "", type: "assistant"},
552
+ {text: "Second", type: "user"},
553
+ {status: "error", text: "Partial answer", type: "assistant"},
554
+ {text: "Third", type: "user"},
555
+ {status: "streaming", text: "Half", type: "assistant"},
556
+ ]);
557
+
558
+ expect(messages).toEqual([
559
+ {content: "First", role: "user"},
560
+ {content: "Second", role: "user"},
561
+ {content: "Partial answer", role: "assistant"},
562
+ {content: "Third", role: "user"},
563
+ ]);
564
+ });
565
+
527
566
  it("should convert multi-modal user prompts with image content", () => {
528
567
  const model = createMockModel();
529
568
  const service = new AIService({model: model as unknown as LanguageModel});
@@ -512,6 +512,13 @@ export class AIService {
512
512
  if (prompt.type === "tool-call" || prompt.type === "tool-result") {
513
513
  continue;
514
514
  }
515
+ // A reply still streaming (or one that failed before any output) is not a real turn yet
516
+ if (
517
+ prompt.status === "streaming" ||
518
+ (prompt.status === "error" && !prompt.text && !prompt.content?.length)
519
+ ) {
520
+ continue;
521
+ }
515
522
 
516
523
  const role = prompt.type as "user" | "assistant" | "system";
517
524
 
@@ -548,6 +555,9 @@ export class AIService {
548
555
  }
549
556
  }
550
557
  messages.push({content: parts, role: "user"} as ModelMessage);
558
+ } else if (!prompt.text && prompt.content?.length) {
559
+ // Providers reject empty assistant turns; stand in for an image-only response
560
+ messages.push({content: "[Generated image]", role});
551
561
  } else {
552
562
  messages.push({content: prompt.text, role});
553
563
  }
@@ -0,0 +1,23 @@
1
+ import {APIError} from "@terreno/api";
2
+ import type express from "express";
3
+
4
+ /** Static switch, or a per-request check (for example a feature flag). */
5
+ export type FileUploadsEnabled = boolean | ((req: express.Request) => boolean | Promise<boolean>);
6
+
7
+ /**
8
+ * Reject the request when file uploads are turned off.
9
+ * Missing or `true` leaves uploads enabled.
10
+ */
11
+ export const assertFileUploadsEnabled = async (
12
+ req: express.Request,
13
+ fileUploadsEnabled: FileUploadsEnabled | undefined
14
+ ): Promise<void> => {
15
+ if (fileUploadsEnabled === undefined || fileUploadsEnabled === true) {
16
+ return;
17
+ }
18
+ const allowed =
19
+ typeof fileUploadsEnabled === "function" ? await fileUploadsEnabled(req) : fileUploadsEnabled;
20
+ if (!allowed) {
21
+ throw new APIError({status: 403, title: "File uploads are disabled"});
22
+ }
23
+ };