@terreno/ai 57.21.0 → 58.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/AGENTS.md +20 -18
  2. package/CHANGELOG.md +43 -0
  3. package/biome.jsonc +2 -2
  4. package/dist/aiApp.d.ts +5 -2
  5. package/dist/aiApp.js +3 -2
  6. package/dist/index.d.ts +2 -1
  7. package/dist/index.js +6 -2
  8. package/dist/migrations/gptHistoryStringIds.d.ts +41 -0
  9. package/dist/migrations/gptHistoryStringIds.js +94 -0
  10. package/dist/models/aiDataset.js +2 -2
  11. package/dist/models/gptHistory.js +11 -0
  12. package/dist/routes/files.d.ts +7 -1
  13. package/dist/routes/files.js +62 -13
  14. package/dist/routes/gptActions.js +0 -7
  15. package/dist/routes/gptHistories.js +8 -2
  16. package/dist/service/aiDatasets.js +2 -1
  17. package/dist/service/chatTurn.js +4 -0
  18. package/dist/service/vertex.d.ts +5 -2
  19. package/dist/types/index.d.ts +5 -2
  20. package/package.json +13 -12
  21. package/src/aiApp.test.ts +21 -1
  22. package/src/aiApp.ts +6 -3
  23. package/src/index.ts +7 -1
  24. package/src/isolated/gptStream.isolated.ts +2 -2
  25. package/src/migrations/gptHistoryStringIds.test.ts +231 -0
  26. package/src/migrations/gptHistoryStringIds.ts +150 -0
  27. package/src/models/aiDataset.test.ts +1 -1
  28. package/src/models/aiDataset.ts +2 -2
  29. package/src/models/gptHistory.test.ts +12 -5
  30. package/src/models/gptHistory.ts +19 -1
  31. package/src/routes/files.test.ts +121 -0
  32. package/src/routes/files.ts +90 -15
  33. package/src/routes/gpt.test.ts +2 -2
  34. package/src/routes/gptActions.test.ts +8 -7
  35. package/src/routes/gptActions.ts +1 -4
  36. package/src/routes/gptApprovals.test.ts +1 -1
  37. package/src/routes/gptFiles.test.ts +1 -1
  38. package/src/routes/gptHistories.test.ts +2 -2
  39. package/src/routes/gptHistories.ts +59 -55
  40. package/src/routes/gptHistoriesSync.test.ts +141 -0
  41. package/src/routes/routes.test.ts +6 -5
  42. package/src/service/aiDatasets.ts +2 -1
  43. package/src/service/chatTurn.ts +4 -0
  44. package/src/service/vertex.test.ts +1 -1
  45. package/src/service/vertex.ts +7 -4
  46. package/src/types/index.ts +5 -2
@@ -0,0 +1,141 @@
1
+ import {afterEach, beforeAll, describe, expect, it} from "bun:test";
2
+ import {SyncApp, TerrenoApp} from "@terreno/api";
3
+ import type express from "express";
4
+ import type mongoose from "mongoose";
5
+
6
+ import {GptHistory} from "../models/gptHistory";
7
+ import {
8
+ type AiTestUserDoc,
9
+ authAsUser,
10
+ authAsUserWithCredentials,
11
+ ensureTestUsers,
12
+ historyRouterOptions,
13
+ terrenoUserModel,
14
+ UserModel,
15
+ } from "../tests/helpers";
16
+ import {addGptHistoryRoutes} from "./gptHistories";
17
+
18
+ const OTHER_USER = {
19
+ admin: false,
20
+ email: "sync-other@example.com",
21
+ name: "Other",
22
+ password: "password",
23
+ };
24
+
25
+ const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
26
+
27
+ interface SnapshotEntity {
28
+ data: Record<string, unknown>;
29
+ deleted: boolean;
30
+ id: string;
31
+ seq: number;
32
+ }
33
+
34
+ const ownerStream = (userId: string): string => `gpt/histories|owner:${userId}`;
35
+
36
+ const snapshotPath = (userId: string): string =>
37
+ `/sync/snapshot?stream=${encodeURIComponent(ownerStream(userId))}`;
38
+
39
+ const idOf = (user: AiTestUserDoc): string =>
40
+ String((user as AiTestUserDoc & {_id: mongoose.Types.ObjectId})._id);
41
+
42
+ const buildSyncApp = (): express.Application =>
43
+ new TerrenoApp({
44
+ configureApp: (router, options) => {
45
+ addGptHistoryRoutes(router, historyRouterOptions(options));
46
+ },
47
+ skipListen: true,
48
+ userModel: terrenoUserModel,
49
+ })
50
+ .register(new SyncApp())
51
+ .build();
52
+
53
+ describe("/gpt/histories over sync", () => {
54
+ let notAdminId: string;
55
+ let otherId: string;
56
+
57
+ beforeAll(async () => {
58
+ const [, notAdmin] = await ensureTestUsers();
59
+ const [other] = await ensureTestUsers([OTHER_USER]);
60
+ notAdminId = idOf(notAdmin);
61
+ otherId = idOf(other);
62
+ await GptHistory.collection.deleteMany({});
63
+ });
64
+
65
+ afterEach(async () => {
66
+ await GptHistory.collection.deleteMany({});
67
+ });
68
+
69
+ it("gives new histories a UUID String id", async () => {
70
+ const agent = await authAsUser(buildSyncApp(), "notAdmin");
71
+
72
+ const res = await agent.post("/gpt/histories").send({title: "Fresh"}).expect(201);
73
+
74
+ expect(res.body.data._id).toMatch(UUID_PATTERN);
75
+ const history = await GptHistory.findById(res.body.data._id);
76
+ expect(history?.title).toBe("Fresh");
77
+ });
78
+
79
+ it("returns only the owner's histories in the snapshot of their stream", async () => {
80
+ const app = buildSyncApp();
81
+ const agent = await authAsUser(app, "notAdmin");
82
+ const otherAgent = await authAsUserWithCredentials(app, OTHER_USER);
83
+ const mine = await agent.post("/gpt/histories").send({title: "Mine"}).expect(201);
84
+ await otherAgent.post("/gpt/histories").send({title: "Theirs"}).expect(201);
85
+ await GptHistory.create({prompts: [], title: "Also theirs", userId: otherId});
86
+
87
+ const streams = await agent.get("/sync/streams").expect(200);
88
+ const res = await agent.get(snapshotPath(notAdminId)).expect(200);
89
+
90
+ expect(JSON.stringify(streams.body)).toContain(ownerStream(notAdminId));
91
+ const entities = res.body.entities as SnapshotEntity[];
92
+ expect(entities.map((entity) => entity.id)).toEqual([mine.body.data._id]);
93
+ expect(entities[0].data.title).toBe("Mine");
94
+ expect(entities[0].deleted).toBe(false);
95
+ expect(entities[0].seq).toBeGreaterThan(0);
96
+ });
97
+
98
+ it("refuses another user's stream", async () => {
99
+ const agent = await authAsUser(buildSyncApp(), "notAdmin");
100
+
101
+ await agent.get(snapshotPath(otherId)).expect(403);
102
+ });
103
+
104
+ it("leaves the paused turn's server-only fields out of synced pending asks", async () => {
105
+ const agent = await authAsUser(buildSyncApp(), "notAdmin");
106
+ const history = await GptHistory.create({
107
+ pendingAsk: {
108
+ created: new Date("2026-01-05T09:00:00.000Z"),
109
+ input: {question: "Continue?"},
110
+ kind: "confirm",
111
+ promptIndex: 1,
112
+ responseMessages: [{content: "secret", role: "assistant"}],
113
+ simple: {buttons: [], question: "Continue?"},
114
+ toolCallId: "call_confirm",
115
+ },
116
+ prompts: [{text: "Hello", type: "user"}],
117
+ userId: notAdminId,
118
+ });
119
+
120
+ const res = await agent.get(snapshotPath(notAdminId)).expect(200);
121
+
122
+ const entity = (res.body.entities as SnapshotEntity[]).find((item) => item.id === history._id);
123
+ const pendingAsk = entity?.data.pendingAsk as Record<string, unknown>;
124
+ expect(pendingAsk.toolCallId).toBe("call_confirm");
125
+ expect(pendingAsk).not.toHaveProperty("promptIndex");
126
+ expect(pendingAsk).not.toHaveProperty("responseMessages");
127
+ });
128
+
129
+ it("tombstones a deleted history in the owner's snapshot", async () => {
130
+ const agent = await authAsUser(buildSyncApp(), "notAdmin");
131
+ const created = await agent.post("/gpt/histories").send({title: "Gone"}).expect(201);
132
+
133
+ await agent.delete(`/gpt/histories/${created.body.data._id}`).expect(204);
134
+ const res = await agent.get(snapshotPath(notAdminId)).expect(200);
135
+
136
+ const entity = (res.body.entities as SnapshotEntity[]).find(
137
+ (item) => item.id === created.body.data._id
138
+ );
139
+ expect(entity?.deleted).toBe(true);
140
+ });
141
+ });
@@ -192,7 +192,7 @@ describe("AI Routes", () => {
192
192
  beforeAll(async () => {
193
193
  await ensureTestUsers();
194
194
  await AIRequest.deleteMany({});
195
- await GptHistory.deleteMany({});
195
+ await GptHistory.collection.deleteMany({});
196
196
  });
197
197
 
198
198
  beforeEach(() => {
@@ -215,7 +215,7 @@ describe("AI Routes", () => {
215
215
  resetObservabilityApp();
216
216
  await AIRequest.deleteMany({});
217
217
  await FileAttachment.deleteMany({});
218
- await GptHistory.deleteMany({});
218
+ await GptHistory.collection.deleteMany({});
219
219
  });
220
220
 
221
221
  describe("GPT History routes", () => {
@@ -1710,9 +1710,10 @@ describe("AI Routes", () => {
1710
1710
  .patch(`/gpt/histories/${historyId}`)
1711
1711
  .send({"prompts.0.content.1.gcsKey": plantedKey});
1712
1712
  expect(dotted.status).toBe(200);
1713
- const prompt = await agent
1714
- .post(`/gpt/histories/${historyId}/prompts`)
1715
- .send({prompts: {content: [plantedPart], text: "g", type: "user"}});
1713
+ const latest = await agent.get(`/gpt/histories/${historyId}`);
1714
+ const prompt = await agent.patch(`/gpt/histories/${historyId}`).send({
1715
+ prompts: [...latest.body.data.prompts, {content: [plantedPart], text: "g", type: "user"}],
1716
+ });
1716
1717
  expect(prompt.status).toBe(200);
1717
1718
 
1718
1719
  const history = await GptHistory.findById(historyId);
@@ -133,7 +133,8 @@ export const registerAiDataset = async ({
133
133
  assertRows(columns, rows);
134
134
  const created = await AIDataset.create({
135
135
  columns,
136
- historyId,
136
+ // GptHistory ids are Strings; an ObjectId from an older caller is stored as its hex string.
137
+ historyId: String(historyId),
137
138
  rowCount: rows.length,
138
139
  rows,
139
140
  userId,
@@ -415,6 +415,10 @@ const loadHistory = async ({
415
415
  if (!historyId) {
416
416
  return new GptHistory({prompts: [], userId, ...(projectId ? {projectId} : {})});
417
417
  }
418
+ // History ids are Strings (synced collection); an object here would be read as a query operator.
419
+ if (typeof historyId !== "string") {
420
+ throw new APIError({status: 400, title: "historyId must be a string"});
421
+ }
418
422
  const history = await GptHistory.findById(historyId);
419
423
  if (!history) {
420
424
  throw new APIError({status: 404, title: "History not found"});
@@ -417,10 +417,10 @@ describe("vertex helpers", () => {
417
417
  describe("createVertexProvider", () => {
418
418
  it("returns undefined and logs a warning when factory is not available and module is not installed", () => {
419
419
  const provider = createVertexProvider({
420
+ loadModule: () => undefined,
420
421
  project: "demo-project",
421
422
  vertexFactory: undefined,
422
423
  });
423
- // loadVertexModule returns undefined because @ai-sdk/google-vertex is not installed in test env
424
424
  expect(provider).toBeUndefined();
425
425
  });
426
426
 
@@ -1,3 +1,4 @@
1
+ import type {ImageModelV4, LanguageModelV4} from "@ai-sdk/provider";
1
2
  import {APIError, logger} from "@terreno/api";
2
3
  import type {ImageModel, LanguageModel} from "ai";
3
4
 
@@ -24,8 +25,10 @@ const MODEL_LIST_PAGE_SIZE = "200";
24
25
 
25
26
  /** Provider returned by `@ai-sdk/google-vertex` `createVertex`. */
26
27
  export interface VertexLanguageModelProvider {
27
- (modelId: string): LanguageModel;
28
- image: (modelId: string) => ImageModel;
28
+ /** `@ai-sdk/google-vertex` v4+ returns `LanguageModelV4`; older `ai` majors still type `LanguageModel` as v2/v3 only. */
29
+ (modelId: string): LanguageModel | LanguageModelV4;
30
+ /** `@ai-sdk/google-vertex` v4+ returns `ImageModelV4`; older `ai` majors still type `ImageModel` as v2/v3 only. */
31
+ image: (modelId: string) => ImageModel | ImageModelV4;
29
32
  }
30
33
 
31
34
  interface VertexModule {
@@ -229,9 +232,9 @@ export const createVertexProvider = (
229
232
 
230
233
  return {
231
234
  allowedModels,
232
- imageModel: (modelId: string): ImageModel => raw.image(assertAllowed(modelId)),
235
+ imageModel: (modelId: string): ImageModel => raw.image(assertAllowed(modelId)) as ImageModel,
233
236
  isModelAllowed: (modelId: string): boolean => isVertexModelAllowed(modelId, allowedModels),
234
- languageModel: (modelId: string): LanguageModel => raw(assertAllowed(modelId)),
237
+ languageModel: (modelId: string): LanguageModel => raw(assertAllowed(modelId)) as LanguageModel,
235
238
  location,
236
239
  project,
237
240
  raw,
@@ -154,7 +154,9 @@ interface GptHistoryPendingAskState {
154
154
  /** The ask a history is waiting on. At most one per history. */
155
155
  export type GptHistoryPendingAsk = Ask & GptHistoryPendingAskState;
156
156
 
157
- export interface GptHistoryDocument extends mongoose.Document<mongoose.Types.ObjectId> {
157
+ /** Synced through @terreno/syncdb, so `_id` is a String (UUID for new rows, ObjectId hex for migrated rows). */
158
+ export interface GptHistoryDocument extends mongoose.Document<string> {
159
+ _id: string;
158
160
  created: Date;
159
161
  deleted: boolean;
160
162
  pendingAsk?: GptHistoryPendingAsk;
@@ -515,7 +517,8 @@ export interface AIDatasetDocument extends mongoose.Document<mongoose.Types.Obje
515
517
  created: Date;
516
518
  deleted: boolean;
517
519
  expiresAt?: Date;
518
- historyId: mongoose.Types.ObjectId;
520
+ /** The GptHistory String `_id`. */
521
+ historyId: string;
519
522
  rowCount: number;
520
523
  rows: AIDatasetCell[][];
521
524
  updated: Date;