@terreno/ai 58.0.3 → 58.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -20,6 +20,19 @@ Upgrade notes for consumer action live in [`mcp-server/src/docs/upgrades/`](mcp-
20
20
 
21
21
  Unreleased changes live in [`changelog/unreleased/`](changelog/unreleased/). Add one Markdown file per feature (see that directory's README) instead of editing this section.
22
22
 
23
+ ## [58.0.5] - 2026-11-14
24
+
25
+ ### Fixed
26
+
27
+ - GPT chat no longer shows an image twice for Gemini image models such as `gemini-3-pro-image`. The draft images Gemini returns while it reasons (`thought: true`) are no longer streamed or saved; the last draft is kept only when there is no final image.
28
+ - `@terreno/ai` works with Gemini and Vertex again on `ai` 6. 58.0.0 moved the Google providers to the `ai` 7 line (`@ai-sdk/google-vertex` 5, `@ai-sdk/provider` 4, `@ai-sdk/mcp` 2), whose models `ai` 6 rejects with `Unsupported model version v4`. The `@ai-sdk/google-vertex` peer is back to `^4.0.199`, the `@ai-sdk/provider` peer is removed, and `@ai-sdk/mcp` is back to `^1.0.104`. Apps should install `@ai-sdk/google-vertex` 4.x and `@ai-sdk/google` 3.x.
29
+
30
+ ## [58.0.4] - 2026-11-11
31
+
32
+ ### Fixed
33
+
34
+ - The admin consent form list loads again. `ConsentFormList` sorts by `-created` (and by its Title, Type, Version, Active, and Order columns), but `ConsentApp`'s `/consent-forms` router did not declare those as `sortFields`, so every list request returned 400 `sort-field-not-allowed`. Both `/consent-forms` and `/consent-responses` now allow sorting by the fields their admin metadata marks sortable; other fields are still rejected.
35
+
23
36
  ## [58.0.3] - 2026-11-11
24
37
 
25
38
  Upgrade note: [`mcp-server/src/docs/upgrades/58.0.3.md`](mcp-server/src/docs/upgrades/58.0.3.md).
@@ -32,6 +32,17 @@ const isGeneratedImageFile = (value) => typeof value === "object" &&
32
32
  typeof value.mediaType === "string" &&
33
33
  value.mediaType.startsWith("image/");
34
34
  const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
35
+ /**
36
+ * True when a provider marks a generated file as part of the model's thinking. Gemini image models
37
+ * (e.g. gemini-3-pro-image) draw draft images while they reason and return them tagged
38
+ * `thought: true`, under the `google` or `vertex` metadata key. The final image is usually the same
39
+ * picture with different bytes, so showing drafts makes one image appear twice.
40
+ *
41
+ * @param providerMetadata The `providerMetadata` of a stream `file` part or step content part.
42
+ * @returns Whether any provider namespace flags the file as a thought.
43
+ */
44
+ const isThoughtFile = (providerMetadata) => isRecord(providerMetadata) &&
45
+ Object.values(providerMetadata).some((namespace) => isRecord(namespace) && namespace.thought === true);
35
46
  /** The tool name an ask's rows store: the host tool for an approval ask, else `ask_<kind>`. */
36
47
  const askRowToolName = (ask) => ask.toolName ?? (0, asks_1.askToolName)(ask.kind);
37
48
  /** The fields that mark an ask as an approval, or none for an ask the model made. */
@@ -872,7 +883,9 @@ const consumeStream = async ({ approvals, askKinds, deniedReasons, holdAssistant
872
883
  const { askCalls, generatedImages } = record;
873
884
  const callOrder = new Map();
874
885
  const askOrder = new Map();
875
- // Stream file parts and result.files report the same images; emit each data URL once.
886
+ // Draft (thought) images are held back; the last one is sent only if no final image arrives.
887
+ let lastThoughtImage;
888
+ // Stream file parts and step content report the same images; emit each data URL once.
876
889
  const sendGeneratedImage = (file) => {
877
890
  const url = `data:${file.mediaType};base64,${file.base64}`;
878
891
  if (generatedImages.some((img) => img.url === url)) {
@@ -958,8 +971,14 @@ const consumeStream = async ({ approvals, askKinds, deniedReasons, holdAssistant
958
971
  }
959
972
  if (part.type === "file") {
960
973
  if (isGeneratedImageFile(part.file)) {
961
- sendGeneratedImage(part.file);
962
- api_1.logger.debug("Sent inline image from stream");
974
+ if (isThoughtFile(part.providerMetadata)) {
975
+ lastThoughtImage = part.file;
976
+ api_1.logger.debug("Held back draft image from stream");
977
+ }
978
+ else {
979
+ sendGeneratedImage(part.file);
980
+ api_1.logger.debug("Sent inline image from stream");
981
+ }
963
982
  }
964
983
  continue;
965
984
  }
@@ -1082,17 +1101,28 @@ const consumeStream = async ({ approvals, askKinds, deniedReasons, holdAssistant
1082
1101
  }
1083
1102
  api_1.logger.debug("Stream completed", { fullResponseLength: record.fullResponse.length, partCount });
1084
1103
  askCalls.sort((a, b) => (askOrder.get(a.toolCallId) ?? askOrder.size) - (askOrder.get(b.toolCallId) ?? askOrder.size));
1085
- // Check for generated images (e.g. from gemini-2.5-flash-image)
1104
+ // Pick up generated images the stream did not report (e.g. from gemini-2.5-flash-image). Read
1105
+ // step content rather than `result.files`: only step content keeps the provider's thought flag.
1086
1106
  try {
1087
- const files = await result.files;
1088
- if (files && files.length > 0) {
1089
- for (const file of files) {
1090
- if (isGeneratedImageFile(file)) {
1091
- sendGeneratedImage(file);
1107
+ const steps = await result.steps;
1108
+ for (const step of steps ?? []) {
1109
+ for (const content of step.content) {
1110
+ if (content.type !== "file" || !isGeneratedImageFile(content.file)) {
1111
+ continue;
1112
+ }
1113
+ if (isThoughtFile(content.providerMetadata)) {
1114
+ lastThoughtImage = content.file;
1115
+ }
1116
+ else {
1117
+ sendGeneratedImage(content.file);
1092
1118
  }
1093
1119
  }
1094
- api_1.logger.debug("Sent generated images", { count: generatedImages.length });
1095
1120
  }
1121
+ // A reply that only produced drafts still shows its last one rather than nothing.
1122
+ if (generatedImages.length === 0 && lastThoughtImage) {
1123
+ sendGeneratedImage(lastThoughtImage);
1124
+ }
1125
+ api_1.logger.debug("Sent generated images", { count: generatedImages.length });
1096
1126
  }
1097
1127
  catch (fileErr) {
1098
1128
  api_1.logger.debug("No files in response", {
@@ -1,4 +1,3 @@
1
- import type { ImageModelV4, LanguageModelV4 } from "@ai-sdk/provider";
2
1
  import type { ImageModel, LanguageModel } from "ai";
3
2
  /**
4
3
  * Helper for Google's Vertex AI provider, now part of the "Gemini Enterprise Agent Platform"
@@ -17,10 +16,8 @@ import type { ImageModel, LanguageModel } from "ai";
17
16
  export declare const DEFAULT_VERTEX_LOCATION = "global";
18
17
  /** Provider returned by `@ai-sdk/google-vertex` `createVertex`. */
19
18
  export interface VertexLanguageModelProvider {
20
- /** `@ai-sdk/google-vertex` v4+ returns `LanguageModelV4`; older `ai` majors still type `LanguageModel` as v2/v3 only. */
21
- (modelId: string): LanguageModel | LanguageModelV4;
22
- /** `@ai-sdk/google-vertex` v4+ returns `ImageModelV4`; older `ai` majors still type `ImageModel` as v2/v3 only. */
23
- image: (modelId: string) => ImageModel | ImageModelV4;
19
+ (modelId: string): LanguageModel;
20
+ image: (modelId: string) => ImageModel;
24
21
  }
25
22
  interface VertexModule {
26
23
  createVertex: (opts: {
package/package.json CHANGED
@@ -4,9 +4,9 @@
4
4
  "url": "https://github.com/TerrenoLabs/terreno/issues"
5
5
  },
6
6
  "dependencies": {
7
- "@ai-sdk/mcp": "^2.0.57",
7
+ "@ai-sdk/mcp": "^1.0.104",
8
8
  "@google-cloud/storage": "^7.15.0",
9
- "@terreno/blocks": "58.0.3",
9
+ "@terreno/blocks": "58.0.5",
10
10
  "ai": "^6.0.97",
11
11
  "ajv": "8.20.0",
12
12
  "csv-parse": "^7.0.3",
@@ -17,11 +17,12 @@
17
17
  },
18
18
  "description": "Provider-agnostic AI service with streaming chat, request logging, and admin tools — part of Terreno, the batteries-included TypeScript framework for universal apps.",
19
19
  "devDependencies": {
20
+ "@ai-sdk/google-vertex": "^4.0.199",
20
21
  "@biomejs/biome": "2.5.13",
21
- "@terreno/admin-backend": "58.0.3",
22
- "@terreno/api": "58.0.3",
23
- "@terreno/jobs": "58.0.3",
24
- "@terreno/test": "58.0.3",
22
+ "@terreno/admin-backend": "58.0.5",
23
+ "@terreno/api": "58.0.5",
24
+ "@terreno/jobs": "58.0.5",
25
+ "@terreno/test": "58.0.5",
25
26
  "@types/bun": "^1.4.0",
26
27
  "@types/express": "^5.0.6",
27
28
  "@types/luxon": "^3.7.1",
@@ -119,10 +120,9 @@
119
120
  "main": "dist/index.js",
120
121
  "name": "@terreno/ai",
121
122
  "peerDependencies": {
122
- "@ai-sdk/google-vertex": "^5.0.92",
123
- "@ai-sdk/provider": "^4.0.26",
124
- "@terreno/api": "58.0.3",
125
- "@terreno/jobs": "58.0.3",
123
+ "@ai-sdk/google-vertex": "^4.0.199",
124
+ "@terreno/api": "58.0.5",
125
+ "@terreno/jobs": "58.0.5",
126
126
  "mongoose": "^8.0.0 || ^9.0.0"
127
127
  },
128
128
  "peerDependenciesMeta": {
@@ -155,5 +155,5 @@
155
155
  "typecheck": "tsc -p tsconfig.typecheck.json"
156
156
  },
157
157
  "types": "dist/index.d.ts",
158
- "version": "58.0.3"
158
+ "version": "58.0.5"
159
159
  }
@@ -1,4 +1,5 @@
1
1
  import {afterEach, beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
2
+ import {createVertex} from "@ai-sdk/google-vertex";
2
3
  import {TerrenoApp} from "@terreno/api";
3
4
  import {jsonSchema, type LanguageModel, type Tool, tool} from "ai";
4
5
  import {assert} from "chai";
@@ -746,6 +747,117 @@ describe("AI Routes", () => {
746
747
  );
747
748
  });
748
749
 
750
+ // These run the real Vertex provider against a canned Gemini response, so they also fail if
751
+ // the installed @ai-sdk/google-vertex speaks a model spec this `ai` major cannot run.
752
+ describe("Gemini image model through the Vertex provider", () => {
753
+ const DRAFT = "RFJBRlQ=";
754
+ const FINAL = "RklOQUw=";
755
+
756
+ const inlineImage = (data: string, isThought = false): Record<string, unknown> => ({
757
+ inlineData: {data, mimeType: "image/png"},
758
+ ...(isThought ? {thought: true} : {}),
759
+ });
760
+
761
+ /** A Vertex model whose fetch replays the given response chunks as Gemini SSE. */
762
+ const cannedGeminiModel = (chunks: Record<string, unknown>[][]): LanguageModel => {
763
+ const body = chunks
764
+ .map((parts, index) => {
765
+ const isLast = index === chunks.length - 1;
766
+ const chunk = {
767
+ candidates: [
768
+ {content: {parts, role: "model"}, ...(isLast ? {finishReason: "STOP"} : {})},
769
+ ],
770
+ ...(isLast
771
+ ? {
772
+ usageMetadata: {
773
+ candidatesTokenCount: 1,
774
+ promptTokenCount: 1,
775
+ totalTokenCount: 2,
776
+ },
777
+ }
778
+ : {}),
779
+ };
780
+ return `data: ${JSON.stringify(chunk)}\n\n`;
781
+ })
782
+ .join("");
783
+ const vertex = createVertex({
784
+ apiKey: "test-key",
785
+ fetch: (async (): Promise<Response> =>
786
+ new Response(body, {
787
+ headers: {"content-type": "text/event-stream"},
788
+ })) as unknown as typeof fetch,
789
+ });
790
+ return vertex("gemini-3-pro-image");
791
+ };
792
+
793
+ const promptForImages = async (
794
+ model: LanguageModel,
795
+ prompt: string
796
+ ): Promise<{savedUrls: string[]; streamedUrls: string[]}> => {
797
+ const imgApp = new TerrenoApp({
798
+ configureApp: (router, options) => {
799
+ addGptRoutes(router, {aiService: new AIService({model}), openApiOptions: options});
800
+ },
801
+ skipListen: true,
802
+ userModel: terrenoUserModel,
803
+ }).build();
804
+ const agent = await authAsUser(imgApp, "notAdmin");
805
+ const res = await agent.post("/gpt/prompt").send({prompt}).buffer(true).parse(sseCollect);
806
+ expect(res.status).toBe(200);
807
+ const events = (res as SseResponse).body
808
+ .split("\n")
809
+ .filter((line) => line.startsWith("data: "))
810
+ .map((line) => JSON.parse(line.slice("data: ".length)) as Record<string, unknown>);
811
+ expect(events.filter((event) => event.error)).toEqual([]);
812
+ const streamedUrls = events
813
+ .map((event) => (event.image as {url?: string} | undefined)?.url)
814
+ .filter((url): url is string => typeof url === "string");
815
+ const history = await GptHistory.findExactlyOne({"prompts.text": prompt});
816
+ const assistant = history.prompts.find((p) => p.type === "assistant");
817
+ const savedUrls = (assistant?.content ?? [])
818
+ .filter((part): part is ImageContentPart => part.type === "image")
819
+ .map((part) => part.url);
820
+ return {savedUrls, streamedUrls};
821
+ };
822
+
823
+ it("streams and saves the final image", async () => {
824
+ const model = cannedGeminiModel([[{text: "Here you go"}], [inlineImage(FINAL)]]);
825
+ const {savedUrls, streamedUrls} = await promptForImages(model, "vertex final only");
826
+ expect(streamedUrls).toEqual([`data:image/png;base64,${FINAL}`]);
827
+ expect(savedUrls).toEqual([`data:image/png;base64,${FINAL}`]);
828
+ });
829
+
830
+ it("drops a draft image that differs from the final image", async () => {
831
+ const model = cannedGeminiModel([
832
+ [inlineImage(DRAFT, true), {text: "Here you go"}],
833
+ [inlineImage(FINAL)],
834
+ ]);
835
+ const {savedUrls, streamedUrls} = await promptForImages(model, "vertex draft differs");
836
+ expect(streamedUrls).toEqual([`data:image/png;base64,${FINAL}`]);
837
+ expect(savedUrls).toEqual([`data:image/png;base64,${FINAL}`]);
838
+ });
839
+
840
+ it("keeps one image when the draft matches the final image", async () => {
841
+ const model = cannedGeminiModel([
842
+ [inlineImage(FINAL, true), {text: "Here you go"}],
843
+ [inlineImage(FINAL)],
844
+ ]);
845
+ const {savedUrls, streamedUrls} = await promptForImages(model, "vertex draft matches");
846
+ expect(streamedUrls).toEqual([`data:image/png;base64,${FINAL}`]);
847
+ expect(savedUrls).toEqual([`data:image/png;base64,${FINAL}`]);
848
+ });
849
+
850
+ it("keeps the last draft when the model returns no final image", async () => {
851
+ const model = cannedGeminiModel([
852
+ [inlineImage("RklSU1Q=", true)],
853
+ [inlineImage(DRAFT, true), {text: "Here you go"}],
854
+ ]);
855
+ const {savedUrls, streamedUrls} = await promptForImages(model, "vertex draft only");
856
+ expect(streamedUrls).toEqual([`data:image/png;base64,${DRAFT}`]);
857
+ expect(savedUrls).toEqual([`data:image/png;base64,${DRAFT}`]);
858
+ });
859
+ });
860
+
749
861
  it("writes an SSE error event when the model stream throws", async () => {
750
862
  const errService = new AIService({
751
863
  model: createErrorModel() as unknown as LanguageModel,
@@ -256,6 +256,21 @@ const isGeneratedImageFile = (value: unknown): value is GeneratedImageFile =>
256
256
  const isRecord = (value: unknown): value is Record<string, unknown> =>
257
257
  typeof value === "object" && value !== null && !Array.isArray(value);
258
258
 
259
+ /**
260
+ * True when a provider marks a generated file as part of the model's thinking. Gemini image models
261
+ * (e.g. gemini-3-pro-image) draw draft images while they reason and return them tagged
262
+ * `thought: true`, under the `google` or `vertex` metadata key. The final image is usually the same
263
+ * picture with different bytes, so showing drafts makes one image appear twice.
264
+ *
265
+ * @param providerMetadata The `providerMetadata` of a stream `file` part or step content part.
266
+ * @returns Whether any provider namespace flags the file as a thought.
267
+ */
268
+ const isThoughtFile = (providerMetadata: unknown): boolean =>
269
+ isRecord(providerMetadata) &&
270
+ Object.values(providerMetadata).some(
271
+ (namespace) => isRecord(namespace) && namespace.thought === true
272
+ );
273
+
259
274
  /** The tool name an ask's rows store: the host tool for an approval ask, else `ask_<kind>`. */
260
275
  const askRowToolName = (ask: {kind: AskKind; toolName?: string}): string =>
261
276
  ask.toolName ?? askToolName(ask.kind);
@@ -1415,7 +1430,9 @@ const consumeStream = async ({
1415
1430
  const {askCalls, generatedImages} = record;
1416
1431
  const callOrder = new Map<string, number>();
1417
1432
  const askOrder = new Map<string, number>();
1418
- // Stream file parts and result.files report the same images; emit each data URL once.
1433
+ // Draft (thought) images are held back; the last one is sent only if no final image arrives.
1434
+ let lastThoughtImage: GeneratedImageFile | undefined;
1435
+ // Stream file parts and step content report the same images; emit each data URL once.
1419
1436
  const sendGeneratedImage = (file: GeneratedImageFile): void => {
1420
1437
  const url = `data:${file.mediaType};base64,${file.base64}`;
1421
1438
  if (generatedImages.some((img) => img.url === url)) {
@@ -1507,8 +1524,13 @@ const consumeStream = async ({
1507
1524
  }
1508
1525
  if (part.type === "file") {
1509
1526
  if (isGeneratedImageFile(part.file)) {
1510
- sendGeneratedImage(part.file);
1511
- logger.debug("Sent inline image from stream");
1527
+ if (isThoughtFile(part.providerMetadata)) {
1528
+ lastThoughtImage = part.file;
1529
+ logger.debug("Held back draft image from stream");
1530
+ } else {
1531
+ sendGeneratedImage(part.file);
1532
+ logger.debug("Sent inline image from stream");
1533
+ }
1512
1534
  }
1513
1535
  continue;
1514
1536
  }
@@ -1634,17 +1656,27 @@ const consumeStream = async ({
1634
1656
  (askOrder.get(a.toolCallId) ?? askOrder.size) - (askOrder.get(b.toolCallId) ?? askOrder.size)
1635
1657
  );
1636
1658
 
1637
- // Check for generated images (e.g. from gemini-2.5-flash-image)
1659
+ // Pick up generated images the stream did not report (e.g. from gemini-2.5-flash-image). Read
1660
+ // step content rather than `result.files`: only step content keeps the provider's thought flag.
1638
1661
  try {
1639
- const files = await result.files;
1640
- if (files && files.length > 0) {
1641
- for (const file of files) {
1642
- if (isGeneratedImageFile(file)) {
1643
- sendGeneratedImage(file);
1662
+ const steps = await result.steps;
1663
+ for (const step of steps ?? []) {
1664
+ for (const content of step.content as StreamPart[]) {
1665
+ if (content.type !== "file" || !isGeneratedImageFile(content.file)) {
1666
+ continue;
1667
+ }
1668
+ if (isThoughtFile(content.providerMetadata)) {
1669
+ lastThoughtImage = content.file;
1670
+ } else {
1671
+ sendGeneratedImage(content.file);
1644
1672
  }
1645
1673
  }
1646
- logger.debug("Sent generated images", {count: generatedImages.length});
1647
1674
  }
1675
+ // A reply that only produced drafts still shows its last one rather than nothing.
1676
+ if (generatedImages.length === 0 && lastThoughtImage) {
1677
+ sendGeneratedImage(lastThoughtImage);
1678
+ }
1679
+ logger.debug("Sent generated images", {count: generatedImages.length});
1648
1680
  } catch (fileErr) {
1649
1681
  logger.debug("No files in response", {
1650
1682
  error: fileErr instanceof Error ? fileErr.message : String(fileErr),
@@ -1,4 +1,3 @@
1
- import type {ImageModelV4, LanguageModelV4} from "@ai-sdk/provider";
2
1
  import {APIError, logger} from "@terreno/api";
3
2
  import type {ImageModel, LanguageModel} from "ai";
4
3
 
@@ -25,10 +24,8 @@ const MODEL_LIST_PAGE_SIZE = "200";
25
24
 
26
25
  /** Provider returned by `@ai-sdk/google-vertex` `createVertex`. */
27
26
  export interface VertexLanguageModelProvider {
28
- /** `@ai-sdk/google-vertex` v4+ returns `LanguageModelV4`; older `ai` majors still type `LanguageModel` as v2/v3 only. */
29
- (modelId: string): LanguageModel | LanguageModelV4;
30
- /** `@ai-sdk/google-vertex` v4+ returns `ImageModelV4`; older `ai` majors still type `ImageModel` as v2/v3 only. */
31
- image: (modelId: string) => ImageModel | ImageModelV4;
27
+ (modelId: string): LanguageModel;
28
+ image: (modelId: string) => ImageModel;
32
29
  }
33
30
 
34
31
  interface VertexModule {
@@ -232,9 +229,9 @@ export const createVertexProvider = (
232
229
 
233
230
  return {
234
231
  allowedModels,
235
- imageModel: (modelId: string): ImageModel => raw.image(assertAllowed(modelId)) as ImageModel,
232
+ imageModel: (modelId: string): ImageModel => raw.image(assertAllowed(modelId)),
236
233
  isModelAllowed: (modelId: string): boolean => isVertexModelAllowed(modelId, allowedModels),
237
- languageModel: (modelId: string): LanguageModel => raw(assertAllowed(modelId)) as LanguageModel,
234
+ languageModel: (modelId: string): LanguageModel => raw(assertAllowed(modelId)),
238
235
  location,
239
236
  project,
240
237
  raw,