@terreno/ai 57.21.0 → 58.0.0-beta.2
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/AGENTS.md +20 -18
- package/CHANGELOG.md +89 -0
- package/biome.jsonc +2 -2
- package/dist/aiApp.d.ts +5 -2
- package/dist/aiApp.js +3 -2
- package/dist/index.d.ts +1 -1
- package/dist/models/gptHistory.js +2 -0
- package/dist/routes/files.d.ts +7 -1
- package/dist/routes/files.js +62 -13
- package/dist/routes/gptHistories.js +8 -2
- package/dist/service/chatTurn.js +5 -0
- package/dist/service/vertex.d.ts +5 -2
- package/dist/types/index.d.ts +4 -0
- package/package.json +13 -12
- package/src/aiApp.test.ts +21 -1
- package/src/aiApp.ts +6 -3
- package/src/index.ts +1 -1
- package/src/isolated/gptStream.isolated.ts +2 -2
- package/src/models/gptHistory.test.ts +2 -2
- package/src/models/gptHistory.ts +9 -1
- package/src/routes/files.test.ts +121 -0
- package/src/routes/files.ts +90 -15
- package/src/routes/gpt.test.ts +14 -2
- package/src/routes/gptActions.test.ts +3 -3
- package/src/routes/gptApprovals.test.ts +1 -1
- package/src/routes/gptFiles.test.ts +1 -1
- package/src/routes/gptHistories.test.ts +2 -2
- package/src/routes/gptHistories.ts +59 -55
- package/src/routes/gptHistoriesSync.test.ts +230 -0
- package/src/routes/routes.test.ts +6 -5
- package/src/service/chatTurn.ts +5 -0
- package/src/service/vertex.test.ts +1 -1
- package/src/service/vertex.ts +7 -4
- package/src/types/index.ts +4 -0
|
@@ -0,0 +1,230 @@
|
|
|
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 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
|
+
} from "../tests/helpers";
|
|
15
|
+
import {addGptHistoryRoutes} from "./gptHistories";
|
|
16
|
+
|
|
17
|
+
const OTHER_USER = {
|
|
18
|
+
admin: false,
|
|
19
|
+
email: "sync-other@example.com",
|
|
20
|
+
name: "Other",
|
|
21
|
+
password: "password",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const OBJECT_ID_PATTERN = /^[0-9a-f]{24}$/;
|
|
25
|
+
|
|
26
|
+
interface SnapshotEntity {
|
|
27
|
+
data: Record<string, unknown>;
|
|
28
|
+
deleted: boolean;
|
|
29
|
+
id: string;
|
|
30
|
+
seq: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const ownerStream = (userId: string): string => `gpt/histories|owner:${userId}`;
|
|
34
|
+
|
|
35
|
+
const snapshotPath = (userId: string): string =>
|
|
36
|
+
`/sync/snapshot?stream=${encodeURIComponent(ownerStream(userId))}`;
|
|
37
|
+
|
|
38
|
+
const idOf = (user: AiTestUserDoc): string =>
|
|
39
|
+
String((user as AiTestUserDoc & {_id: mongoose.Types.ObjectId})._id);
|
|
40
|
+
|
|
41
|
+
const buildSyncApp = (): express.Application =>
|
|
42
|
+
new TerrenoApp({
|
|
43
|
+
configureApp: (router, options) => {
|
|
44
|
+
addGptHistoryRoutes(router, historyRouterOptions(options));
|
|
45
|
+
},
|
|
46
|
+
skipListen: true,
|
|
47
|
+
userModel: terrenoUserModel,
|
|
48
|
+
})
|
|
49
|
+
.register(new SyncApp())
|
|
50
|
+
.build();
|
|
51
|
+
|
|
52
|
+
describe("/gpt/histories over sync", () => {
|
|
53
|
+
let notAdminId: string;
|
|
54
|
+
let otherId: string;
|
|
55
|
+
|
|
56
|
+
beforeAll(async () => {
|
|
57
|
+
const [, notAdmin] = await ensureTestUsers();
|
|
58
|
+
const [other] = await ensureTestUsers([OTHER_USER]);
|
|
59
|
+
notAdminId = idOf(notAdmin);
|
|
60
|
+
otherId = idOf(other);
|
|
61
|
+
await GptHistory.collection.deleteMany({});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
afterEach(async () => {
|
|
65
|
+
await GptHistory.collection.deleteMany({});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("gives new histories a server-made ObjectId", async () => {
|
|
69
|
+
const agent = await authAsUser(buildSyncApp(), "notAdmin");
|
|
70
|
+
|
|
71
|
+
const res = await agent.post("/gpt/histories").send({title: "Fresh"}).expect(201);
|
|
72
|
+
|
|
73
|
+
expect(res.body.data._id).toMatch(OBJECT_ID_PATTERN);
|
|
74
|
+
const history = await GptHistory.findById(res.body.data._id);
|
|
75
|
+
expect(history?.title).toBe("Fresh");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("returns only the owner's histories in the snapshot of their stream", async () => {
|
|
79
|
+
const app = buildSyncApp();
|
|
80
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
81
|
+
const otherAgent = await authAsUserWithCredentials(app, OTHER_USER);
|
|
82
|
+
const mine = await agent.post("/gpt/histories").send({title: "Mine"}).expect(201);
|
|
83
|
+
await otherAgent.post("/gpt/histories").send({title: "Theirs"}).expect(201);
|
|
84
|
+
await GptHistory.create({prompts: [], title: "Also theirs", userId: otherId});
|
|
85
|
+
|
|
86
|
+
const streams = await agent.get("/sync/streams").expect(200);
|
|
87
|
+
const res = await agent.get(snapshotPath(notAdminId)).expect(200);
|
|
88
|
+
|
|
89
|
+
expect(JSON.stringify(streams.body)).toContain(ownerStream(notAdminId));
|
|
90
|
+
const entities = res.body.entities as SnapshotEntity[];
|
|
91
|
+
expect(entities.map((entity) => entity.id)).toEqual([mine.body.data._id]);
|
|
92
|
+
expect(entities[0].data.title).toBe("Mine");
|
|
93
|
+
expect(entities[0].deleted).toBe(false);
|
|
94
|
+
expect(entities[0].seq).toBeGreaterThan(0);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("refuses another user's stream", async () => {
|
|
98
|
+
const agent = await authAsUser(buildSyncApp(), "notAdmin");
|
|
99
|
+
|
|
100
|
+
await agent.get(snapshotPath(otherId)).expect(403);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("leaves the paused turn's server-only fields out of synced pending asks", async () => {
|
|
104
|
+
const agent = await authAsUser(buildSyncApp(), "notAdmin");
|
|
105
|
+
const history = await GptHistory.create({
|
|
106
|
+
pendingAsk: {
|
|
107
|
+
created: new Date("2026-01-05T09:00:00.000Z"),
|
|
108
|
+
input: {question: "Continue?"},
|
|
109
|
+
kind: "confirm",
|
|
110
|
+
promptIndex: 1,
|
|
111
|
+
responseMessages: [{content: "secret", role: "assistant"}],
|
|
112
|
+
simple: {buttons: [], question: "Continue?"},
|
|
113
|
+
toolCallId: "call_confirm",
|
|
114
|
+
},
|
|
115
|
+
prompts: [{text: "Hello", type: "user"}],
|
|
116
|
+
userId: notAdminId,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const res = await agent.get(snapshotPath(notAdminId)).expect(200);
|
|
120
|
+
|
|
121
|
+
const entity = (res.body.entities as SnapshotEntity[]).find(
|
|
122
|
+
(item) => item.id === String(history._id)
|
|
123
|
+
);
|
|
124
|
+
const pendingAsk = entity?.data.pendingAsk as Record<string, unknown>;
|
|
125
|
+
expect(pendingAsk.toolCallId).toBe("call_confirm");
|
|
126
|
+
expect(pendingAsk).not.toHaveProperty("promptIndex");
|
|
127
|
+
expect(pendingAsk).not.toHaveProperty("responseMessages");
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("tombstones a deleted history in the owner's snapshot", async () => {
|
|
131
|
+
const agent = await authAsUser(buildSyncApp(), "notAdmin");
|
|
132
|
+
const created = await agent.post("/gpt/histories").send({title: "Gone"}).expect(201);
|
|
133
|
+
|
|
134
|
+
await agent.delete(`/gpt/histories/${created.body.data._id}`).expect(204);
|
|
135
|
+
const res = await agent.get(snapshotPath(notAdminId)).expect(200);
|
|
136
|
+
|
|
137
|
+
const entity = (res.body.entities as SnapshotEntity[]).find(
|
|
138
|
+
(item) => item.id === created.body.data._id
|
|
139
|
+
);
|
|
140
|
+
expect(entity?.deleted).toBe(true);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("creates a history through sync with a client-minted ObjectId and edits it by that id", async () => {
|
|
144
|
+
const agent = await authAsUser(buildSyncApp(), "notAdmin");
|
|
145
|
+
const id = new mongoose.Types.ObjectId().toHexString();
|
|
146
|
+
|
|
147
|
+
const created = await agent
|
|
148
|
+
.post("/sync/mutate")
|
|
149
|
+
.send({
|
|
150
|
+
collection: "gpt/histories",
|
|
151
|
+
data: {prompts: [], title: "Offline"},
|
|
152
|
+
id,
|
|
153
|
+
mutationId: `create-${id}`,
|
|
154
|
+
operation: "create",
|
|
155
|
+
})
|
|
156
|
+
.expect(200);
|
|
157
|
+
expect(created.body.ack.id).toBe(id);
|
|
158
|
+
const stored = await GptHistory.findById(id);
|
|
159
|
+
expect(stored?._id).toBeInstanceOf(mongoose.Types.ObjectId);
|
|
160
|
+
expect(String(stored?.userId)).toBe(notAdminId);
|
|
161
|
+
|
|
162
|
+
const renamed = await agent
|
|
163
|
+
.post("/sync/mutate")
|
|
164
|
+
.send({
|
|
165
|
+
baseVersion: created.body.ack.seq,
|
|
166
|
+
collection: "gpt/histories",
|
|
167
|
+
data: {title: "Renamed"},
|
|
168
|
+
id,
|
|
169
|
+
mutationId: `rename-${id}`,
|
|
170
|
+
operation: "update",
|
|
171
|
+
})
|
|
172
|
+
.expect(200);
|
|
173
|
+
expect((await GptHistory.findById(id))?.title).toBe("Renamed");
|
|
174
|
+
|
|
175
|
+
await agent
|
|
176
|
+
.post("/sync/mutate")
|
|
177
|
+
.send({
|
|
178
|
+
collection: "gpt/histories",
|
|
179
|
+
id,
|
|
180
|
+
mutationId: `delete-${id}`,
|
|
181
|
+
operation: "delete",
|
|
182
|
+
})
|
|
183
|
+
.expect(200);
|
|
184
|
+
const res = await agent.get(snapshotPath(notAdminId)).expect(200);
|
|
185
|
+
const entity = (res.body.entities as SnapshotEntity[]).find((item) => item.id === id);
|
|
186
|
+
expect(entity?.deleted).toBe(true);
|
|
187
|
+
expect(entity?.seq).toBeGreaterThan(renamed.body.ack.seq);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("nacks a UUID history id as invalid_id without writing anything", async () => {
|
|
191
|
+
const agent = await authAsUser(buildSyncApp(), "notAdmin");
|
|
192
|
+
|
|
193
|
+
const res = await agent
|
|
194
|
+
.post("/sync/mutate")
|
|
195
|
+
.send({
|
|
196
|
+
collection: "gpt/histories",
|
|
197
|
+
data: {prompts: [], title: "UUID"},
|
|
198
|
+
id: crypto.randomUUID(),
|
|
199
|
+
mutationId: "create-uuid-history",
|
|
200
|
+
operation: "create",
|
|
201
|
+
})
|
|
202
|
+
.expect(422);
|
|
203
|
+
|
|
204
|
+
expect(res.body.nack.code).toBe("validation");
|
|
205
|
+
expect(res.body.nack.reason).toBe("invalid_id");
|
|
206
|
+
expect(await GptHistory.collection.countDocuments({})).toBe(0);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("nacks id_collision when a client-minted id is another user's history", async () => {
|
|
210
|
+
const agent = await authAsUser(buildSyncApp(), "notAdmin");
|
|
211
|
+
const theirs = await GptHistory.create({prompts: [], title: "Theirs", userId: otherId});
|
|
212
|
+
|
|
213
|
+
const res = await agent
|
|
214
|
+
.post("/sync/mutate")
|
|
215
|
+
.send({
|
|
216
|
+
collection: "gpt/histories",
|
|
217
|
+
data: {prompts: [], title: "Mine"},
|
|
218
|
+
id: String(theirs._id),
|
|
219
|
+
mutationId: "create-colliding-history",
|
|
220
|
+
operation: "create",
|
|
221
|
+
})
|
|
222
|
+
.expect(422);
|
|
223
|
+
|
|
224
|
+
expect(res.body.nack.reason).toBe("id_collision");
|
|
225
|
+
expect(res.body.nack.message).toBe("Entity id already exists");
|
|
226
|
+
const stored = await GptHistory.findById(theirs._id);
|
|
227
|
+
expect(stored?.title).toBe("Theirs");
|
|
228
|
+
expect(String(stored?.userId)).toBe(otherId);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
@@ -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
|
|
1714
|
-
|
|
1715
|
-
|
|
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);
|
package/src/service/chatTurn.ts
CHANGED
|
@@ -415,6 +415,11 @@ const loadHistory = async ({
|
|
|
415
415
|
if (!historyId) {
|
|
416
416
|
return new GptHistory({prompts: [], userId, ...(projectId ? {projectId} : {})});
|
|
417
417
|
}
|
|
418
|
+
// The id arrives as the ObjectId's hex string. An object here would be read as a query
|
|
419
|
+
// operator, and any other string would surface as a CastError 500.
|
|
420
|
+
if (typeof historyId !== "string" || !/^[0-9a-fA-F]{24}$/.test(historyId)) {
|
|
421
|
+
throw new APIError({status: 400, title: "historyId is not valid"});
|
|
422
|
+
}
|
|
418
423
|
const history = await GptHistory.findById(historyId);
|
|
419
424
|
if (!history) {
|
|
420
425
|
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
|
|
package/src/service/vertex.ts
CHANGED
|
@@ -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
|
-
|
|
28
|
-
|
|
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,
|
package/src/types/index.ts
CHANGED
|
@@ -154,6 +154,10 @@ 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
|
+
/**
|
|
158
|
+
* Synced through @terreno/syncdb with the default ObjectId `_id`; sync clients that create
|
|
159
|
+
* histories configure `idFormats: {"gpt/histories": "objectId"}`.
|
|
160
|
+
*/
|
|
157
161
|
export interface GptHistoryDocument extends mongoose.Document<mongoose.Types.ObjectId> {
|
|
158
162
|
created: Date;
|
|
159
163
|
deleted: boolean;
|