@terreno/ai 57.8.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,7 @@ const createImageModel = () => ({
95
97
  },
96
98
  }),
97
99
  })),
98
- modelId: "gemini-2.5-flash-image",
100
+ modelId: "gemini-3-pro-image",
99
101
  provider: "mock-provider",
100
102
  specificationVersion: "v2" as const,
101
103
  supportedUrls: {},
@@ -345,11 +347,11 @@ describe("AI Routes", () => {
345
347
  const agent = await authAsUser(customApp, "notAdmin");
346
348
  const res = await agent
347
349
  .post("/gpt/prompt")
348
- .send({model: "gemini-2.5-pro", prompt: "Hi"})
350
+ .send({model: "gemini-3.1-pro-preview", prompt: "Hi"})
349
351
  .buffer(true)
350
352
  .parse(sseCollect);
351
353
  expect(res.status).toBe(200);
352
- expect(createServerModelFn).toHaveBeenCalledWith("gemini-2.5-pro");
354
+ expect(createServerModelFn).toHaveBeenCalledWith("gemini-3.1-pro-preview");
353
355
  });
354
356
 
355
357
  it("falls back to default aiService when createServerModelFn returns null", async () => {
@@ -1242,6 +1244,455 @@ describe("AI Routes", () => {
1242
1244
  });
1243
1245
  });
1244
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
+
1245
1696
  describe("GPT Prompt error handling", () => {
1246
1697
  it("sends an outer SSE error when error handling itself fails after streaming starts", async () => {
1247
1698
  streamTextOverride = () =>
@@ -542,6 +542,27 @@ describe("AIService", () => {
542
542
  expect(messages[1]).toEqual({content: "[Generated image]", role: "assistant"});
543
543
  });
544
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
+
545
566
  it("should convert multi-modal user prompts with image content", () => {
546
567
  const model = createMockModel();
547
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
 
@@ -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
+ };
@@ -12,11 +12,11 @@ const jsonResponse = (body: unknown, ok = true, status = 200): Response =>
12
12
  describe("gemini model listing helpers", () => {
13
13
  describe("normalizeGeminiModelId", () => {
14
14
  it("strips the models/ prefix", () => {
15
- expect(normalizeGeminiModelId("models/gemini-2.5-flash")).toBe("gemini-2.5-flash");
15
+ expect(normalizeGeminiModelId("models/gemini-3.8-flash")).toBe("gemini-3.8-flash");
16
16
  });
17
17
 
18
18
  it("leaves a bare id unchanged", () => {
19
- expect(normalizeGeminiModelId("gemini-2.5-pro")).toBe("gemini-2.5-pro");
19
+ expect(normalizeGeminiModelId("gemini-3.1-pro-preview")).toBe("gemini-3.1-pro-preview");
20
20
  });
21
21
  });
22
22
 
@@ -30,29 +30,32 @@ describe("gemini model listing helpers", () => {
30
30
  const fetchImpl = mock(async () =>
31
31
  jsonResponse({
32
32
  models: [
33
- {name: "models/gemini-2.5-pro", supportedGenerationMethods: ["generateContent"]},
34
- {name: "models/gemini-2.5-flash", supportedGenerationMethods: ["generateContent"]},
33
+ {
34
+ name: "models/gemini-3.1-pro-preview",
35
+ supportedGenerationMethods: ["generateContent"],
36
+ },
37
+ {name: "models/gemini-3.8-flash", supportedGenerationMethods: ["generateContent"]},
35
38
  {name: "models/text-embedding-004", supportedGenerationMethods: ["embedContent"]},
36
39
  ],
37
40
  })
38
41
  ) as unknown as typeof fetch;
39
42
 
40
43
  const result = await listGeminiApiModels({apiKey: "key", fetchImpl});
41
- expect(result).toEqual(["gemini-2.5-pro", "gemini-2.5-flash"]);
44
+ expect(result).toEqual(["gemini-3.1-pro-preview", "gemini-3.8-flash"]);
42
45
  });
43
46
 
44
47
  it("returns every model when chatOnly is false", async () => {
45
48
  const fetchImpl = mock(async () =>
46
49
  jsonResponse({
47
50
  models: [
48
- {name: "models/gemini-2.5-flash", supportedGenerationMethods: ["generateContent"]},
51
+ {name: "models/gemini-3.8-flash", supportedGenerationMethods: ["generateContent"]},
49
52
  {name: "models/text-embedding-004", supportedGenerationMethods: ["embedContent"]},
50
53
  ],
51
54
  })
52
55
  ) as unknown as typeof fetch;
53
56
 
54
57
  const result = await listGeminiApiModels({apiKey: "key", chatOnly: false, fetchImpl});
55
- expect(result).toEqual(["gemini-2.5-flash", "text-embedding-004"]);
58
+ expect(result).toEqual(["gemini-3.8-flash", "text-embedding-004"]);
56
59
  });
57
60
 
58
61
  it("follows pagination via nextPageToken", async () => {
@@ -62,20 +65,23 @@ describe("gemini model listing helpers", () => {
62
65
  if (call === 1) {
63
66
  return jsonResponse({
64
67
  models: [
65
- {name: "models/gemini-2.5-pro", supportedGenerationMethods: ["generateContent"]},
68
+ {
69
+ name: "models/gemini-3.1-pro-preview",
70
+ supportedGenerationMethods: ["generateContent"],
71
+ },
66
72
  ],
67
73
  nextPageToken: "page2",
68
74
  });
69
75
  }
70
76
  return jsonResponse({
71
77
  models: [
72
- {name: "models/gemini-2.5-flash", supportedGenerationMethods: ["generateContent"]},
78
+ {name: "models/gemini-3.8-flash", supportedGenerationMethods: ["generateContent"]},
73
79
  ],
74
80
  });
75
81
  }) as unknown as typeof fetch;
76
82
 
77
83
  const result = await listGeminiApiModels({apiKey: "key", fetchImpl});
78
- expect(result).toEqual(["gemini-2.5-pro", "gemini-2.5-flash"]);
84
+ expect(result).toEqual(["gemini-3.1-pro-preview", "gemini-3.8-flash"]);
79
85
  expect(call).toBe(2);
80
86
  });
81
87
 
@@ -4,7 +4,7 @@ import {logger} from "@terreno/api";
4
4
  * Helper for listing models available to the Gemini Developer API (the API-key based
5
5
  * `generativelanguage.googleapis.com` service, distinct from Vertex / the Gemini Enterprise Agent
6
6
  * Platform). Used to drive model pickers from the live set of models Google actually exposes for a
7
- * given API key, so retired models (e.g. an old `gemini-2.0-flash`) never appear.
7
+ * given API key, so retired models (e.g. an old `gemini-2.5-flash`) never appear.
8
8
  */
9
9
 
10
10
  /** Default Gemini Developer API base URL. */
@@ -33,14 +33,14 @@ interface GeminiApiModel {
33
33
  supportedGenerationMethods?: string[];
34
34
  }
35
35
 
36
- /** Strip the "models/" resource prefix from a Gemini model name (e.g. "models/gemini-2.5-flash"). */
36
+ /** Strip the "models/" resource prefix from a Gemini model name (e.g. "models/gemini-3.8-flash"). */
37
37
  export const normalizeGeminiModelId = (name: string): string => {
38
38
  return name.trim().replace(/^models\//, "");
39
39
  };
40
40
 
41
41
  /**
42
42
  * List the models available to a Gemini Developer API key via the `models` REST endpoint. Returns
43
- * normalized model ids (e.g. "gemini-2.5-flash"). By default only chat-capable models (those
43
+ * normalized model ids (e.g. "gemini-3.8-flash"). By default only chat-capable models (those
44
44
  * supporting `generateContent`) are returned. Returns `undefined` when the listing could not be
45
45
  * retrieved (missing key, network error, non-200 response, etc.).
46
46
  */