@terreno/ai 57.20.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.
- package/AGENTS.md +20 -18
- package/CHANGELOG.md +93 -0
- package/biome.jsonc +3 -2
- package/dist/aiApp.d.ts +5 -2
- package/dist/aiApp.js +3 -2
- package/dist/harness/agentLoop.js +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +6 -2
- package/dist/migrations/gptHistoryStringIds.d.ts +41 -0
- package/dist/migrations/gptHistoryStringIds.js +94 -0
- package/dist/models/aiDataset.js +2 -2
- package/dist/models/gptHistory.js +11 -0
- package/dist/observability/local/experimentRunner.d.ts +2 -5
- package/dist/routes/files.d.ts +7 -1
- package/dist/routes/files.js +62 -13
- package/dist/routes/gptActions.js +0 -7
- package/dist/routes/gptHistories.js +8 -2
- package/dist/service/aiDatasets.js +2 -1
- package/dist/service/chatTurn.js +4 -0
- package/dist/service/vertex.d.ts +5 -2
- package/dist/types/harness.d.ts +5 -2
- package/dist/types/index.d.ts +5 -2
- package/e2e/smoke.ts +227 -0
- package/package.json +19 -16
- package/src/aiApp.test.ts +43 -18
- package/src/aiApp.ts +6 -3
- package/src/harness/agentLoop.test.ts +1 -1
- package/src/harness/agentLoop.ts +2 -1
- package/src/harness/agentRecovery.test.ts +31 -28
- package/src/harness/approvals.test.ts +11 -9
- package/src/harness/events.test.ts +10 -6
- package/src/harness/extensions.test.ts +7 -6
- package/src/harness/ownership.test.ts +20 -9
- package/src/harness/retries.test.ts +6 -3
- package/src/harness/routes/conversations.test.ts +2 -2
- package/src/harness/routes/tasks.test.ts +2 -2
- package/src/harness/runners/jobsRunner.test.ts +1 -1
- package/src/harness/subagent.test.ts +3 -2
- package/src/harness/waits.test.ts +6 -4
- package/src/index.test.ts +3 -3
- package/src/index.ts +7 -1
- package/src/isolated/gptStream.isolated.ts +35 -30
- package/src/isolated/langfuseClient.isolated.ts +9 -3
- package/src/isolated/lazyProjectModel.isolated.ts +2 -2
- package/src/langfuseApp.test.ts +4 -20
- package/src/langfuseRoutes.test.ts +20 -15
- package/src/langfuseTracing.test.ts +6 -2
- package/src/langfuseVercelAi.test.ts +12 -9
- package/src/migrations/gptHistoryStringIds.test.ts +231 -0
- package/src/migrations/gptHistoryStringIds.ts +150 -0
- package/src/models/aiDataset.test.ts +1 -1
- package/src/models/aiDataset.ts +2 -2
- package/src/models/gptHistory.test.ts +22 -11
- package/src/models/gptHistory.ts +19 -1
- package/src/observability/evaluate.test.ts +12 -10
- package/src/observability/local/experimentRunner.test.ts +9 -12
- package/src/observability/local/experimentRunner.ts +2 -2
- package/src/observability/local/promptStore.test.ts +4 -3
- package/src/observability/routes/datasets.test.ts +2 -2
- package/src/observability/routes/evaluators.test.ts +2 -2
- package/src/observability/routes/experiments.test.ts +2 -2
- package/src/observability/routes/prompts.test.ts +3 -3
- package/src/observability/routes/review.test.ts +2 -2
- package/src/observability/routes/status.test.ts +4 -4
- package/src/observability/routes/testMultiStage.test.ts +6 -6
- package/src/observability/routes/traces.test.ts +2 -2
- package/src/routes/files.test.ts +126 -3
- package/src/routes/files.ts +90 -15
- package/src/routes/gpt.test.ts +23 -16
- package/src/routes/gptActions.test.ts +15 -15
- package/src/routes/gptActions.ts +1 -4
- package/src/routes/gptApprovals.test.ts +1 -1
- package/src/routes/gptDatasets.test.ts +1 -1
- package/src/routes/gptFiles.test.ts +2 -2
- package/src/routes/gptHistories.test.ts +17 -11
- package/src/routes/gptHistories.ts +59 -55
- package/src/routes/gptHistoriesSync.test.ts +141 -0
- package/src/routes/mcp.test.ts +3 -3
- package/src/routes/projects.test.ts +5 -3
- package/src/routes/routes.test.ts +64 -47
- package/src/service/aiDatasets.ts +2 -1
- package/src/service/aiService.test.ts +1 -1
- package/src/service/asks.test.ts +14 -6
- package/src/service/chatTurn.ts +4 -0
- package/src/service/fileStorage.test.ts +5 -2
- package/src/service/getMCPTools.test.ts +9 -5
- package/src/service/mcpService.test.ts +6 -3
- package/src/service/vertex.test.ts +1 -1
- package/src/service/vertex.ts +7 -4
- package/src/tests/bunSetup.ts +1 -1
- package/src/tests/chatHarness.ts +4 -4
- package/src/tests/fakeJudge.ts +8 -0
- package/src/tests/fakeLangfusePrompt.ts +10 -0
- package/src/tests/helpers.ts +26 -4
- package/src/types/harness.ts +6 -2
- package/src/types/index.ts +5 -2
- package/tsconfig.typecheck.json +18 -0
|
@@ -3,7 +3,7 @@ import {TerrenoApp} from "@terreno/api";
|
|
|
3
3
|
import {assert} from "chai";
|
|
4
4
|
import type express from "express";
|
|
5
5
|
|
|
6
|
-
import {authAsUser, ensureTestUsers,
|
|
6
|
+
import {authAsUser, ensureTestUsers, terrenoUserModel} from "../../tests/helpers";
|
|
7
7
|
import {createLocalObservabilityPlugin} from "../local/localPlugin";
|
|
8
8
|
import {registerObsSpan} from "../local/models/obsSpan";
|
|
9
9
|
import {registerObsTrace} from "../local/models/obsTrace";
|
|
@@ -86,7 +86,7 @@ describe("observability test-multi-stage route", () => {
|
|
|
86
86
|
await registerObsSpan().deleteMany({});
|
|
87
87
|
const plugin = createLocalObservabilityPlugin();
|
|
88
88
|
fakeAiService = createFakeAiService();
|
|
89
|
-
app = new TerrenoApp({skipListen: true, userModel:
|
|
89
|
+
app = new TerrenoApp({skipListen: true, userModel: terrenoUserModel})
|
|
90
90
|
.register(new ObservabilityApp({aiService: fakeAiService, plugins: [plugin]}))
|
|
91
91
|
.build();
|
|
92
92
|
});
|
|
@@ -125,7 +125,7 @@ describe("observability test-multi-stage route", () => {
|
|
|
125
125
|
|
|
126
126
|
it("returns 503 when no server or request AI service is available", async () => {
|
|
127
127
|
const plugin = createLocalObservabilityPlugin();
|
|
128
|
-
const noAiApp = new TerrenoApp({skipListen: true, userModel:
|
|
128
|
+
const noAiApp = new TerrenoApp({skipListen: true, userModel: terrenoUserModel})
|
|
129
129
|
.register(new ObservabilityApp({plugins: [plugin]}))
|
|
130
130
|
.build();
|
|
131
131
|
const agent = await authAsUser(noAiApp, "admin");
|
|
@@ -141,7 +141,7 @@ describe("observability test-multi-stage route", () => {
|
|
|
141
141
|
return apiKey === "request-key" ? createFakeAiService() : undefined;
|
|
142
142
|
});
|
|
143
143
|
const plugin = createLocalObservabilityPlugin();
|
|
144
|
-
const requestKeyApp = new TerrenoApp({skipListen: true, userModel:
|
|
144
|
+
const requestKeyApp = new TerrenoApp({skipListen: true, userModel: terrenoUserModel})
|
|
145
145
|
.register(new ObservabilityApp({plugins: [plugin], requestAiServiceFactory}))
|
|
146
146
|
.build();
|
|
147
147
|
const agent = await authAsUser(requestKeyApp, "admin");
|
|
@@ -159,7 +159,7 @@ describe("observability test-multi-stage route", () => {
|
|
|
159
159
|
it("exports an error trace and rethrows when a child LLM stage fails", async () => {
|
|
160
160
|
const failingService = createFakeAiService({failOn: OBS_TEST_MULTI_STAGE_CALL_2_SCHEMA_NAME});
|
|
161
161
|
const plugin = createLocalObservabilityPlugin();
|
|
162
|
-
const failingApp = new TerrenoApp({skipListen: true, userModel:
|
|
162
|
+
const failingApp = new TerrenoApp({skipListen: true, userModel: terrenoUserModel})
|
|
163
163
|
.register(new ObservabilityApp({aiService: failingService, plugins: [plugin]}))
|
|
164
164
|
.build();
|
|
165
165
|
const agent = await authAsUser(failingApp, "admin");
|
|
@@ -186,7 +186,7 @@ describe("observability test-multi-stage route", () => {
|
|
|
186
186
|
it("exports the completed child stages when final synthesis fails", async () => {
|
|
187
187
|
const failingService = createFakeAiService({failOn: OBS_TEST_MULTI_STAGE_FINAL_SCHEMA_NAME});
|
|
188
188
|
const plugin = createLocalObservabilityPlugin();
|
|
189
|
-
const failingApp = new TerrenoApp({skipListen: true, userModel:
|
|
189
|
+
const failingApp = new TerrenoApp({skipListen: true, userModel: terrenoUserModel})
|
|
190
190
|
.register(new ObservabilityApp({aiService: failingService, plugins: [plugin]}))
|
|
191
191
|
.build();
|
|
192
192
|
const agent = await authAsUser(failingApp, "admin");
|
|
@@ -3,7 +3,7 @@ import {TerrenoApp} from "@terreno/api";
|
|
|
3
3
|
import type express from "express";
|
|
4
4
|
import {DateTime} from "luxon";
|
|
5
5
|
|
|
6
|
-
import {authAsUser, ensureTestUsers,
|
|
6
|
+
import {authAsUser, ensureTestUsers, terrenoUserModel} from "../../tests/helpers";
|
|
7
7
|
import {createLocalObservabilityPlugin} from "../local/localPlugin";
|
|
8
8
|
import {registerObsScore} from "../local/models/obsScore";
|
|
9
9
|
import {registerObsSpan} from "../local/models/obsSpan";
|
|
@@ -32,7 +32,7 @@ describe("observability trace routes", () => {
|
|
|
32
32
|
plugin.traceSink && "store" in plugin.traceSink
|
|
33
33
|
? (plugin.traceSink as {store: LocalTraceStore}).store
|
|
34
34
|
: new LocalTraceStore();
|
|
35
|
-
app = new TerrenoApp({skipListen: true, userModel:
|
|
35
|
+
app = new TerrenoApp({skipListen: true, userModel: terrenoUserModel})
|
|
36
36
|
.register(new ObservabilityApp({plugins: [plugin]}))
|
|
37
37
|
.build();
|
|
38
38
|
});
|
package/src/routes/files.test.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import {beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
2
|
import {TerrenoApp} from "@terreno/api";
|
|
3
|
+
// The side-effect-free entry: `@terreno/api/testing` registers api's own test `User` model,
|
|
4
|
+
// which collides with this package's `User` when the whole suite shares one process.
|
|
5
|
+
import {expectDocumentedResponse, type OpenApiResponseSpec} from "@terreno/api/testing/openApi";
|
|
3
6
|
import type express from "express";
|
|
4
7
|
import type mongoose from "mongoose";
|
|
5
8
|
import supertest from "supertest";
|
|
6
9
|
|
|
7
10
|
import {FileAttachment} from "../models/fileAttachment";
|
|
8
11
|
import type {FileStorageService, UploadFileParams, UploadFileResult} from "../service/fileStorage";
|
|
9
|
-
import {authAsUser, ensureTestUsers, UserModel} from "../tests/helpers";
|
|
12
|
+
import {authAsUser, ensureTestUsers, terrenoUserModel, UserModel} from "../tests/helpers";
|
|
10
13
|
import {addFileRoutes} from "./files";
|
|
11
14
|
|
|
12
15
|
type MockFileStorageService = Pick<FileStorageService, "upload" | "delete" | "getSignedUrl">;
|
|
@@ -45,11 +48,12 @@ describe("File Routes", () => {
|
|
|
45
48
|
configureApp: (router, options) => {
|
|
46
49
|
addFileRoutes(router, {
|
|
47
50
|
fileStorageService: fileStorageService as FileStorageService,
|
|
51
|
+
gcsBucket: "test-bucket",
|
|
48
52
|
openApiOptions: options,
|
|
49
53
|
});
|
|
50
54
|
},
|
|
51
55
|
skipListen: true,
|
|
52
|
-
userModel:
|
|
56
|
+
userModel: terrenoUserModel,
|
|
53
57
|
}).build();
|
|
54
58
|
});
|
|
55
59
|
|
|
@@ -76,11 +80,12 @@ describe("File Routes", () => {
|
|
|
76
80
|
addFileRoutes(router, {
|
|
77
81
|
fileStorageService: fileStorageService as FileStorageService,
|
|
78
82
|
fileUploadsEnabled: async () => false,
|
|
83
|
+
gcsBucket: "test-bucket",
|
|
79
84
|
openApiOptions: options,
|
|
80
85
|
});
|
|
81
86
|
},
|
|
82
87
|
skipListen: true,
|
|
83
|
-
userModel:
|
|
88
|
+
userModel: terrenoUserModel,
|
|
84
89
|
}).build();
|
|
85
90
|
const agent = await authAsUser(gated, "notAdmin");
|
|
86
91
|
const res = await agent
|
|
@@ -219,4 +224,122 @@ describe("File Routes", () => {
|
|
|
219
224
|
expect(fileStorageService.delete as ReturnType<typeof mock>).not.toHaveBeenCalled();
|
|
220
225
|
});
|
|
221
226
|
});
|
|
227
|
+
describe("OpenAPI", () => {
|
|
228
|
+
it("documents POST /files/upload as multipart/form-data with the data envelope", async () => {
|
|
229
|
+
const res = await supertest(app).get("/openapi.json").expect(200);
|
|
230
|
+
const operation = res.body.paths["/files/upload"]?.post;
|
|
231
|
+
expect(operation?.tags).toEqual(["files"]);
|
|
232
|
+
expect(Object.keys(operation.requestBody.content)).toEqual(["multipart/form-data"]);
|
|
233
|
+
const requestSchema = operation.requestBody.content["multipart/form-data"].schema;
|
|
234
|
+
expect(requestSchema.required).toEqual(["file"]);
|
|
235
|
+
expect(requestSchema.properties.file.format).toBe("binary");
|
|
236
|
+
expect(requestSchema.properties.file.type).toBe("string");
|
|
237
|
+
const responseSchema = operation.responses["200"].content["application/json"].schema;
|
|
238
|
+
expect(responseSchema.required).toEqual(["data"]);
|
|
239
|
+
expect(responseSchema.properties.data.required).toEqual([
|
|
240
|
+
"filename",
|
|
241
|
+
"gcsKey",
|
|
242
|
+
"id",
|
|
243
|
+
"mimeType",
|
|
244
|
+
"size",
|
|
245
|
+
"url",
|
|
246
|
+
]);
|
|
247
|
+
|
|
248
|
+
// The generator keeps Express 5's `*gcsKey` wildcard as written, so find the key route.
|
|
249
|
+
const keyPath = Object.keys(res.body.paths).find(
|
|
250
|
+
(path) => path.startsWith("/files/") && path !== "/files/upload"
|
|
251
|
+
);
|
|
252
|
+
const keyRoute = res.body.paths[keyPath ?? ""];
|
|
253
|
+
interface SchemaNode {
|
|
254
|
+
properties: Record<string, SchemaNode & {type?: string}>;
|
|
255
|
+
}
|
|
256
|
+
const dataOf = (operation: {
|
|
257
|
+
responses: Record<string, {content: Record<string, {schema: SchemaNode}>}>;
|
|
258
|
+
}): SchemaNode =>
|
|
259
|
+
operation.responses["200"].content["application/json"].schema.properties.data;
|
|
260
|
+
expect(dataOf(keyRoute.get).properties.url.type).toBe("string");
|
|
261
|
+
expect(dataOf(keyRoute.delete).properties.success.type).toBe("boolean");
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("documents every field the upload returns", async () => {
|
|
265
|
+
const docs = await supertest(app).get("/openapi.json").expect(200);
|
|
266
|
+
const documented = Object.keys(
|
|
267
|
+
docs.body.paths["/files/upload"].post.responses["200"].content["application/json"].schema
|
|
268
|
+
.properties.data.properties
|
|
269
|
+
);
|
|
270
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
271
|
+
const res = await agent
|
|
272
|
+
.post("/files/upload")
|
|
273
|
+
.attach("file", Buffer.from("hello"), {contentType: "text/plain", filename: "hi.txt"})
|
|
274
|
+
.expect(200);
|
|
275
|
+
expect(documented).toEqual(expect.arrayContaining(Object.keys(res.body.data)));
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it("returns upload, read, and delete bodies that match the documented schemas", async () => {
|
|
279
|
+
const spec = (await supertest(app).get("/openapi.json").expect(200))
|
|
280
|
+
.body as OpenApiResponseSpec;
|
|
281
|
+
const keyPath = Object.keys(spec.paths ?? {}).find(
|
|
282
|
+
(path) => path.startsWith("/files/") && path !== "/files/upload"
|
|
283
|
+
);
|
|
284
|
+
expect(keyPath).toBeDefined();
|
|
285
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
286
|
+
|
|
287
|
+
const uploaded = await agent
|
|
288
|
+
.post("/files/upload")
|
|
289
|
+
.attach("file", Buffer.from("hello"), {contentType: "text/plain", filename: "hi.txt"})
|
|
290
|
+
.expect(200);
|
|
291
|
+
expectDocumentedResponse({method: "post", path: "/files/upload", response: uploaded, spec});
|
|
292
|
+
|
|
293
|
+
const gcsKey = `uploads/${userId.toString()}/1727550000000-documented.txt`;
|
|
294
|
+
await FileAttachment.create({
|
|
295
|
+
filename: "documented.txt",
|
|
296
|
+
gcsKey,
|
|
297
|
+
mimeType: "text/plain",
|
|
298
|
+
size: 5,
|
|
299
|
+
url: "https://example.com/documented.txt",
|
|
300
|
+
userId,
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
const read = await agent.get(`/files/${gcsKey}`).expect(200);
|
|
304
|
+
expectDocumentedResponse({method: "get", path: keyPath ?? "", response: read, spec});
|
|
305
|
+
|
|
306
|
+
const deleted = await agent.delete(`/files/${gcsKey}`).expect(200);
|
|
307
|
+
expectDocumentedResponse({method: "delete", path: keyPath ?? "", response: deleted, spec});
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
describe("runtime-resolved storage", () => {
|
|
312
|
+
it("answers 503 while storage is unconfigured and uploads once it resolves", async () => {
|
|
313
|
+
let current: FileStorageService | undefined;
|
|
314
|
+
const runtimeApp = new TerrenoApp({
|
|
315
|
+
configureApp: (router, options) => {
|
|
316
|
+
addFileRoutes(router, {
|
|
317
|
+
fileStorageService: () => current,
|
|
318
|
+
gcsBucket: "",
|
|
319
|
+
openApiOptions: options,
|
|
320
|
+
});
|
|
321
|
+
},
|
|
322
|
+
skipListen: true,
|
|
323
|
+
userModel: terrenoUserModel,
|
|
324
|
+
}).build();
|
|
325
|
+
const agent = await authAsUser(runtimeApp, "notAdmin");
|
|
326
|
+
|
|
327
|
+
const docs = await supertest(runtimeApp).get("/openapi.json").expect(200);
|
|
328
|
+
expect(docs.body.paths["/files/upload"]?.post).toBeDefined();
|
|
329
|
+
|
|
330
|
+
const unconfigured = await agent
|
|
331
|
+
.post("/files/upload")
|
|
332
|
+
.attach("file", Buffer.from("hello"), {contentType: "text/plain", filename: "hi.txt"});
|
|
333
|
+
expect(unconfigured.status).toBe(503);
|
|
334
|
+
expect(unconfigured.body.title).toBe("File storage is not configured");
|
|
335
|
+
expect(fileStorageService.upload as ReturnType<typeof mock>).not.toHaveBeenCalled();
|
|
336
|
+
|
|
337
|
+
current = fileStorageService as FileStorageService;
|
|
338
|
+
const configured = await agent
|
|
339
|
+
.post("/files/upload")
|
|
340
|
+
.attach("file", Buffer.from("hello"), {contentType: "text/plain", filename: "hi.txt"});
|
|
341
|
+
expect(configured.status).toBe(200);
|
|
342
|
+
expect(fileStorageService.upload as ReturnType<typeof mock>).toHaveBeenCalledTimes(1);
|
|
343
|
+
});
|
|
344
|
+
});
|
|
222
345
|
});
|
package/src/routes/files.ts
CHANGED
|
@@ -30,11 +30,40 @@ const gcsKeyParam = (req: express.Request): string => {
|
|
|
30
30
|
const requestUserId = (req: express.Request): mongoose.Types.ObjectId =>
|
|
31
31
|
(req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id as mongoose.Types.ObjectId;
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* A storage service, or a function that returns the current one. Pass a function when storage
|
|
35
|
+
* can be configured or cleared at runtime: the routes stay mounted (and documented in OpenAPI)
|
|
36
|
+
* and answer 503 while it returns `undefined`.
|
|
37
|
+
*/
|
|
38
|
+
export type FileStorageServiceSource = FileStorageService | (() => FileStorageService | undefined);
|
|
39
|
+
|
|
40
|
+
const resolveFileStorageService = (source: FileStorageServiceSource): FileStorageService => {
|
|
41
|
+
const service = typeof source === "function" ? source() : source;
|
|
42
|
+
if (!service) {
|
|
43
|
+
throw new APIError({status: 503, title: "File storage is not configured"});
|
|
44
|
+
}
|
|
45
|
+
return service;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const uploadResultSchema = {
|
|
49
|
+
filename: {type: "string"},
|
|
50
|
+
gcsKey: {
|
|
51
|
+
description: "Storage key; GET or DELETE /files/<gcsKey> reads or removes the file",
|
|
52
|
+
type: "string",
|
|
53
|
+
},
|
|
54
|
+
id: {description: "The FileAttachment id", type: "string"},
|
|
55
|
+
mimeType: {type: "string"},
|
|
56
|
+
size: {description: "Size in bytes", type: "number"},
|
|
57
|
+
url: {type: "string"},
|
|
58
|
+
};
|
|
59
|
+
|
|
33
60
|
export const addFileRoutes = (
|
|
34
61
|
router: express.Router,
|
|
35
|
-
options: FileRouteOptions & {fileStorageService:
|
|
62
|
+
options: FileRouteOptions & {fileStorageService: FileStorageServiceSource}
|
|
36
63
|
): void => {
|
|
37
|
-
const {
|
|
64
|
+
const {maxFileSize = DEFAULT_MAX_FILE_SIZE} = options;
|
|
65
|
+
const fileStorageService = (): FileStorageService =>
|
|
66
|
+
resolveFileStorageService(options.fileStorageService);
|
|
38
67
|
|
|
39
68
|
const upload = multer({
|
|
40
69
|
fileFilter: (_req, file, cb) => {
|
|
@@ -52,22 +81,46 @@ export const addFileRoutes = (
|
|
|
52
81
|
"/files/upload",
|
|
53
82
|
[
|
|
54
83
|
authenticateMiddleware(),
|
|
84
|
+
// Fail before multer buffers the upload when no storage is configured.
|
|
85
|
+
(_req: express.Request, _res: express.Response, next: express.NextFunction): void => {
|
|
86
|
+
try {
|
|
87
|
+
fileStorageService();
|
|
88
|
+
next();
|
|
89
|
+
} catch (error) {
|
|
90
|
+
next(error);
|
|
91
|
+
}
|
|
92
|
+
},
|
|
55
93
|
upload.single("file"),
|
|
56
94
|
createOpenApiBuilder(options.openApiOptions ?? {})
|
|
57
95
|
.withTags(["files"])
|
|
58
96
|
.withSummary("Upload a file")
|
|
59
|
-
.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
97
|
+
.withDescription(
|
|
98
|
+
`Send the file as multipart/form-data in the \`file\` field. Allowed types: ${[
|
|
99
|
+
...ALLOWED_MIME_TYPES,
|
|
100
|
+
].join(", ")}. Maximum size: ${maxFileSize} bytes.`
|
|
101
|
+
)
|
|
102
|
+
.withRequestBody(
|
|
103
|
+
{file: {format: "binary", required: true, type: "string"}},
|
|
104
|
+
{mediaType: "multipart/form-data"}
|
|
105
|
+
)
|
|
106
|
+
.withResponse(
|
|
107
|
+
200,
|
|
108
|
+
{
|
|
109
|
+
data: {
|
|
110
|
+
properties: uploadResultSchema,
|
|
111
|
+
required: Object.keys(uploadResultSchema),
|
|
112
|
+
type: "object",
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
{required: ["data"]}
|
|
116
|
+
)
|
|
117
|
+
// Multer puts the file on req.file, not req.body, so JSON body validation must stay off.
|
|
118
|
+
.withValidation({enabled: false})
|
|
67
119
|
.build(),
|
|
68
120
|
],
|
|
69
121
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
70
122
|
await assertFileUploadsEnabled(req, options.fileUploadsEnabled);
|
|
123
|
+
const storage = fileStorageService();
|
|
71
124
|
const file = (req as express.Request & {file?: Express.Multer.File}).file;
|
|
72
125
|
const userId = requestUserId(req);
|
|
73
126
|
|
|
@@ -75,7 +128,7 @@ export const addFileRoutes = (
|
|
|
75
128
|
throw new APIError({status: 400, title: "No file provided"});
|
|
76
129
|
}
|
|
77
130
|
|
|
78
|
-
const result = await
|
|
131
|
+
const result = await storage.upload({
|
|
79
132
|
buffer: file.buffer,
|
|
80
133
|
filename: file.originalname,
|
|
81
134
|
mimeType: file.mimetype,
|
|
@@ -94,10 +147,21 @@ export const addFileRoutes = (
|
|
|
94
147
|
.withTags(["files"])
|
|
95
148
|
.withSummary("Get file URL")
|
|
96
149
|
.withPathParameter("gcsKey", {type: "string"})
|
|
97
|
-
.withResponse(
|
|
150
|
+
.withResponse(
|
|
151
|
+
200,
|
|
152
|
+
{
|
|
153
|
+
data: {
|
|
154
|
+
properties: {url: {description: "Signed URL for the file", type: "string"}},
|
|
155
|
+
required: ["url"],
|
|
156
|
+
type: "object",
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
{required: ["data"]}
|
|
160
|
+
)
|
|
98
161
|
.build(),
|
|
99
162
|
],
|
|
100
163
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
164
|
+
const storage = fileStorageService();
|
|
101
165
|
const gcsKey = gcsKeyParam(req);
|
|
102
166
|
|
|
103
167
|
// Another user's file is "not found", so keys cannot be probed.
|
|
@@ -110,7 +174,7 @@ export const addFileRoutes = (
|
|
|
110
174
|
throw new APIError({status: 404, title: "File not found"});
|
|
111
175
|
}
|
|
112
176
|
|
|
113
|
-
const url = await
|
|
177
|
+
const url = await storage.getSignedUrl(gcsKey);
|
|
114
178
|
return res.json({data: {url}});
|
|
115
179
|
})
|
|
116
180
|
);
|
|
@@ -123,10 +187,21 @@ export const addFileRoutes = (
|
|
|
123
187
|
.withTags(["files"])
|
|
124
188
|
.withSummary("Delete a file")
|
|
125
189
|
.withPathParameter("gcsKey", {type: "string"})
|
|
126
|
-
.withResponse(
|
|
190
|
+
.withResponse(
|
|
191
|
+
200,
|
|
192
|
+
{
|
|
193
|
+
data: {
|
|
194
|
+
properties: {success: {type: "boolean"}},
|
|
195
|
+
required: ["success"],
|
|
196
|
+
type: "object",
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
{required: ["data"]}
|
|
200
|
+
)
|
|
127
201
|
.build(),
|
|
128
202
|
],
|
|
129
203
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
204
|
+
const storage = fileStorageService();
|
|
130
205
|
const gcsKey = gcsKeyParam(req);
|
|
131
206
|
const userId = requestUserId(req);
|
|
132
207
|
|
|
@@ -139,7 +214,7 @@ export const addFileRoutes = (
|
|
|
139
214
|
throw new APIError({status: 403, title: "Not authorized to delete this file"});
|
|
140
215
|
}
|
|
141
216
|
|
|
142
|
-
await
|
|
217
|
+
await storage.delete(gcsKey);
|
|
143
218
|
return res.json({data: {success: true}});
|
|
144
219
|
})
|
|
145
220
|
);
|
package/src/routes/gpt.test.ts
CHANGED
|
@@ -51,7 +51,13 @@ import {
|
|
|
51
51
|
toolNamesOf,
|
|
52
52
|
USER_PROMPT,
|
|
53
53
|
} from "../tests/chatHarness";
|
|
54
|
-
import {
|
|
54
|
+
import {
|
|
55
|
+
authAsUser,
|
|
56
|
+
ensureTestUsers,
|
|
57
|
+
historyRouterOptions,
|
|
58
|
+
terrenoUserModel,
|
|
59
|
+
UserModel,
|
|
60
|
+
} from "../tests/helpers";
|
|
55
61
|
import type {GptHistoryDocument} from "../types";
|
|
56
62
|
import {addGptRoutes} from "./gpt";
|
|
57
63
|
import {addGptHistoryRoutes} from "./gptHistories";
|
|
@@ -68,7 +74,7 @@ describe("/gpt/prompt asks", () => {
|
|
|
68
74
|
|
|
69
75
|
afterEach(async () => {
|
|
70
76
|
await AIRequest.deleteMany({});
|
|
71
|
-
await GptHistory.deleteMany({});
|
|
77
|
+
await GptHistory.collection.deleteMany({});
|
|
72
78
|
});
|
|
73
79
|
|
|
74
80
|
describe("pausing on an ask", () => {
|
|
@@ -2840,7 +2846,7 @@ describe("/gpt/prompt asks", () => {
|
|
|
2840
2846
|
asks: true,
|
|
2841
2847
|
};
|
|
2842
2848
|
addGptHistoryRoutes(router, {
|
|
2843
|
-
...options,
|
|
2849
|
+
...historyRouterOptions(options),
|
|
2844
2850
|
chat,
|
|
2845
2851
|
responseHandler: async (value) => {
|
|
2846
2852
|
const history = (value as GptHistoryDocument).toObject();
|
|
@@ -2850,7 +2856,7 @@ describe("/gpt/prompt asks", () => {
|
|
|
2850
2856
|
addGptRoutes(router, chat);
|
|
2851
2857
|
},
|
|
2852
2858
|
skipListen: true,
|
|
2853
|
-
userModel:
|
|
2859
|
+
userModel: terrenoUserModel,
|
|
2854
2860
|
}).build();
|
|
2855
2861
|
const agent = await authAsUser(app, "notAdmin");
|
|
2856
2862
|
const historyId = await pauseOnPlanAsk(agent);
|
|
@@ -2894,7 +2900,7 @@ describe("/gpt/prompt asks", () => {
|
|
|
2894
2900
|
const app = new TerrenoApp({
|
|
2895
2901
|
configureApp: (router, options) => {
|
|
2896
2902
|
addGptHistoryRoutes(router, {
|
|
2897
|
-
...options,
|
|
2903
|
+
...historyRouterOptions(options),
|
|
2898
2904
|
openApiOverwrite: {
|
|
2899
2905
|
create: {summary: "Start a conversation"},
|
|
2900
2906
|
update: {requestBody: {description: "The fields to change"}},
|
|
@@ -2902,7 +2908,7 @@ describe("/gpt/prompt asks", () => {
|
|
|
2902
2908
|
});
|
|
2903
2909
|
},
|
|
2904
2910
|
skipListen: true,
|
|
2905
|
-
userModel:
|
|
2911
|
+
userModel: terrenoUserModel,
|
|
2906
2912
|
}).build();
|
|
2907
2913
|
const agent = await authAsUser(app, "notAdmin");
|
|
2908
2914
|
|
|
@@ -2920,12 +2926,12 @@ describe("/gpt/prompt asks", () => {
|
|
|
2920
2926
|
const app = new TerrenoApp({
|
|
2921
2927
|
configureApp: (router, options) => {
|
|
2922
2928
|
addGptHistoryRoutes(router, {
|
|
2923
|
-
...options,
|
|
2929
|
+
...historyRouterOptions(options),
|
|
2924
2930
|
preUpdate: (body) => ({...body, title: `${body.title} (edited)`}) as GptHistoryDocument,
|
|
2925
2931
|
});
|
|
2926
2932
|
},
|
|
2927
2933
|
skipListen: true,
|
|
2928
|
-
userModel:
|
|
2934
|
+
userModel: terrenoUserModel,
|
|
2929
2935
|
}).build();
|
|
2930
2936
|
const agent = await authAsUser(app, "notAdmin");
|
|
2931
2937
|
const created = await agent
|
|
@@ -2980,13 +2986,14 @@ const STEPPER_HOST_ACTIONS = {
|
|
|
2980
2986
|
scaleStepper: {handles: "stepper" as const},
|
|
2981
2987
|
};
|
|
2982
2988
|
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2989
|
+
interface BlocksEvent {
|
|
2990
|
+
errors: {code: string; path: string}[];
|
|
2991
|
+
ok: boolean;
|
|
2992
|
+
warnings?: unknown[];
|
|
2993
|
+
}
|
|
2994
|
+
|
|
2995
|
+
const blocksEventOf = (events: Record<string, unknown>[]): BlocksEvent =>
|
|
2996
|
+
events.find((event) => "blocks" in event)?.blocks as BlocksEvent;
|
|
2990
2997
|
|
|
2991
2998
|
describe("/gpt/prompt uiBlocks", () => {
|
|
2992
2999
|
beforeAll(async () => {
|
|
@@ -2995,7 +3002,7 @@ describe("/gpt/prompt uiBlocks", () => {
|
|
|
2995
3002
|
|
|
2996
3003
|
afterEach(async () => {
|
|
2997
3004
|
await AIRequest.deleteMany({});
|
|
2998
|
-
await GptHistory.deleteMany({});
|
|
3005
|
+
await GptHistory.collection.deleteMany({});
|
|
2999
3006
|
});
|
|
3000
3007
|
|
|
3001
3008
|
it("leaves the prompt and the event list unchanged when uiBlocks is off", async () => {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import {afterEach, describe, expect, it} from "bun:test";
|
|
2
2
|
import {z} from "@terreno/api";
|
|
3
|
-
import type mongoose from "mongoose";
|
|
4
3
|
|
|
5
4
|
import {AIRequest} from "../models/aiRequest";
|
|
6
5
|
import {GptHistory} from "../models/gptHistory";
|
|
@@ -30,7 +29,7 @@ const validBlocks = (): Record<string, unknown> => {
|
|
|
30
29
|
describe("POST /gpt/actions", () => {
|
|
31
30
|
afterEach(async () => {
|
|
32
31
|
await AIRequest.deleteMany({});
|
|
33
|
-
await GptHistory.deleteMany({});
|
|
32
|
+
await GptHistory.collection.deleteMany({});
|
|
34
33
|
});
|
|
35
34
|
|
|
36
35
|
it("keeps the default handler cap at 10 seconds", () => {
|
|
@@ -47,7 +46,7 @@ describe("POST /gpt/actions", () => {
|
|
|
47
46
|
|
|
48
47
|
it("rejects an unknown name, a bad payload, and a non-owner", async () => {
|
|
49
48
|
const created = await ensureTestUsers();
|
|
50
|
-
const userId =
|
|
49
|
+
const userId = created[1]._id;
|
|
51
50
|
const history = await GptHistory.create({
|
|
52
51
|
prompts: [{text: "Hello", type: "user"}],
|
|
53
52
|
userId,
|
|
@@ -89,7 +88,7 @@ describe("POST /gpt/actions", () => {
|
|
|
89
88
|
|
|
90
89
|
it("returns the handler document and logs ui_action", async () => {
|
|
91
90
|
const created = await ensureTestUsers();
|
|
92
|
-
const userId =
|
|
91
|
+
const userId = created[1]._id;
|
|
93
92
|
const history = await GptHistory.create({
|
|
94
93
|
prompts: [{text: "Hello", type: "user"}],
|
|
95
94
|
userId,
|
|
@@ -135,7 +134,7 @@ describe("POST /gpt/actions", () => {
|
|
|
135
134
|
|
|
136
135
|
it("returns 504 and logs the timeout", async () => {
|
|
137
136
|
const created = await ensureTestUsers();
|
|
138
|
-
const userId =
|
|
137
|
+
const userId = created[1]._id;
|
|
139
138
|
const history = await GptHistory.create({
|
|
140
139
|
prompts: [{text: "Hello", type: "user"}],
|
|
141
140
|
userId,
|
|
@@ -166,7 +165,7 @@ describe("POST /gpt/actions", () => {
|
|
|
166
165
|
|
|
167
166
|
it("returns 500 when the handler's document is invalid", async () => {
|
|
168
167
|
const created = await ensureTestUsers();
|
|
169
|
-
const userId =
|
|
168
|
+
const userId = created[1]._id;
|
|
170
169
|
const history = await GptHistory.create({
|
|
171
170
|
prompts: [{text: "Hello", type: "user"}],
|
|
172
171
|
userId,
|
|
@@ -199,7 +198,7 @@ describe("POST /gpt/actions", () => {
|
|
|
199
198
|
expect(res.body.meta.fields["blocks[0].color"]).toEqual(expect.stringContaining("UNKNOWN_KEY"));
|
|
200
199
|
});
|
|
201
200
|
|
|
202
|
-
it("rejects a missing body
|
|
201
|
+
it("rejects a missing body and a history that does not exist, by any string id", async () => {
|
|
203
202
|
await ensureTestUsers();
|
|
204
203
|
const app = buildApp({
|
|
205
204
|
model,
|
|
@@ -216,9 +215,10 @@ describe("POST /gpt/actions", () => {
|
|
|
216
215
|
messageId: "m1",
|
|
217
216
|
name: "export_csv",
|
|
218
217
|
};
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
expect(
|
|
218
|
+
// History ids are Strings (UUIDs or migrated ObjectId hex), so any string is looked up.
|
|
219
|
+
const unknownId = await agent.post("/gpt/actions").send(body);
|
|
220
|
+
expect(unknownId.status).toBe(404);
|
|
221
|
+
expect(unknownId.body.title).toBe("History not found");
|
|
222
222
|
|
|
223
223
|
const missing = await agent.post("/gpt/actions").send({
|
|
224
224
|
...body,
|
|
@@ -230,7 +230,7 @@ describe("POST /gpt/actions", () => {
|
|
|
230
230
|
|
|
231
231
|
it("rejects a replace value the client does not understand", async () => {
|
|
232
232
|
const created = await ensureTestUsers();
|
|
233
|
-
const userId =
|
|
233
|
+
const userId = created[1]._id;
|
|
234
234
|
const history = await GptHistory.create({
|
|
235
235
|
prompts: [{text: "Hello", type: "user"}],
|
|
236
236
|
userId,
|
|
@@ -284,14 +284,14 @@ blocks:
|
|
|
284
284
|
describe("POST /gpt/actions with scaleStepperHostAction", () => {
|
|
285
285
|
afterEach(async () => {
|
|
286
286
|
await AIRequest.deleteMany({});
|
|
287
|
-
await GptHistory.deleteMany({});
|
|
287
|
+
await GptHistory.collection.deleteMany({});
|
|
288
288
|
});
|
|
289
289
|
|
|
290
290
|
const setup = async (
|
|
291
291
|
assistantPrompts: string[]
|
|
292
292
|
): Promise<{agent: Awaited<ReturnType<typeof authAsUser>>; historyId: string}> => {
|
|
293
293
|
const created = await ensureTestUsers();
|
|
294
|
-
const userId =
|
|
294
|
+
const userId = created[1]._id;
|
|
295
295
|
const history = await GptHistory.create({
|
|
296
296
|
prompts: [
|
|
297
297
|
{text: "Plan a roast", type: "user"},
|
|
@@ -396,14 +396,14 @@ blocks:
|
|
|
396
396
|
describe("POST /gpt/actions with toggleChecklistHostAction", () => {
|
|
397
397
|
afterEach(async () => {
|
|
398
398
|
await AIRequest.deleteMany({});
|
|
399
|
-
await GptHistory.deleteMany({});
|
|
399
|
+
await GptHistory.collection.deleteMany({});
|
|
400
400
|
});
|
|
401
401
|
|
|
402
402
|
const setup = async (
|
|
403
403
|
assistantPrompts: string[]
|
|
404
404
|
): Promise<{agent: Awaited<ReturnType<typeof authAsUser>>; historyId: string}> => {
|
|
405
405
|
const created = await ensureTestUsers();
|
|
406
|
-
const userId =
|
|
406
|
+
const userId = created[1]._id;
|
|
407
407
|
const history = await GptHistory.create({
|
|
408
408
|
prompts: [
|
|
409
409
|
{text: "Plan a roast", type: "user"},
|
package/src/routes/gptActions.ts
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
import {type BlocksDocument, validateBlocks} from "@terreno/blocks";
|
|
9
9
|
import type express from "express";
|
|
10
10
|
import {DateTime} from "luxon";
|
|
11
|
-
import mongoose from "mongoose";
|
|
11
|
+
import type mongoose from "mongoose";
|
|
12
12
|
|
|
13
13
|
import {AIRequest} from "../models/aiRequest";
|
|
14
14
|
import {GptHistory} from "../models/gptHistory";
|
|
@@ -158,9 +158,6 @@ export const addGptActionRoutes = (router: express.Router, options: GptRouteOpti
|
|
|
158
158
|
});
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
if (!mongoose.Types.ObjectId.isValid(historyId)) {
|
|
162
|
-
throw new APIError({status: 400, title: "historyId is not valid"});
|
|
163
|
-
}
|
|
164
161
|
const userId = requestUserId(req);
|
|
165
162
|
const history = await GptHistory.findOneOrNone({_id: historyId, deleted: false});
|
|
166
163
|
if (!history) {
|
|
@@ -39,7 +39,7 @@ describe("GET /gpt/datasets/:id", () => {
|
|
|
39
39
|
it("buckets by week, downsamples with LTTB, and paginates tables", async () => {
|
|
40
40
|
await ensureTestUsers();
|
|
41
41
|
const created = await ensureTestUsers();
|
|
42
|
-
const userId =
|
|
42
|
+
const userId = created[1]._id;
|
|
43
43
|
const rows = hourlyRows(12_480);
|
|
44
44
|
const registered = await registerAiDataset({
|
|
45
45
|
columns,
|