@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
package/dist/routes/files.js
CHANGED
|
@@ -25,8 +25,27 @@ const gcsKeyParam = (req) => {
|
|
|
25
25
|
return Array.isArray(param) ? param.join("/") : param;
|
|
26
26
|
};
|
|
27
27
|
const requestUserId = (req) => req.user?._id;
|
|
28
|
+
const resolveFileStorageService = (source) => {
|
|
29
|
+
const service = typeof source === "function" ? source() : source;
|
|
30
|
+
if (!service) {
|
|
31
|
+
throw new api_1.APIError({ status: 503, title: "File storage is not configured" });
|
|
32
|
+
}
|
|
33
|
+
return service;
|
|
34
|
+
};
|
|
35
|
+
const uploadResultSchema = {
|
|
36
|
+
filename: { type: "string" },
|
|
37
|
+
gcsKey: {
|
|
38
|
+
description: "Storage key; GET or DELETE /files/<gcsKey> reads or removes the file",
|
|
39
|
+
type: "string",
|
|
40
|
+
},
|
|
41
|
+
id: { description: "The FileAttachment id", type: "string" },
|
|
42
|
+
mimeType: { type: "string" },
|
|
43
|
+
size: { description: "Size in bytes", type: "number" },
|
|
44
|
+
url: { type: "string" },
|
|
45
|
+
};
|
|
28
46
|
const addFileRoutes = (router, options) => {
|
|
29
|
-
const {
|
|
47
|
+
const { maxFileSize = DEFAULT_MAX_FILE_SIZE } = options;
|
|
48
|
+
const fileStorageService = () => resolveFileStorageService(options.fileStorageService);
|
|
30
49
|
const upload = (0, multer_1.default)({
|
|
31
50
|
fileFilter: (_req, file, cb) => {
|
|
32
51
|
if (ALLOWED_MIME_TYPES.has(file.mimetype)) {
|
|
@@ -41,27 +60,43 @@ const addFileRoutes = (router, options) => {
|
|
|
41
60
|
});
|
|
42
61
|
router.post("/files/upload", [
|
|
43
62
|
(0, api_1.authenticateMiddleware)(),
|
|
63
|
+
// Fail before multer buffers the upload when no storage is configured.
|
|
64
|
+
(_req, _res, next) => {
|
|
65
|
+
try {
|
|
66
|
+
fileStorageService();
|
|
67
|
+
next();
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
next(error);
|
|
71
|
+
}
|
|
72
|
+
},
|
|
44
73
|
upload.single("file"),
|
|
45
74
|
(0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
|
|
46
75
|
.withTags(["files"])
|
|
47
76
|
.withSummary("Upload a file")
|
|
77
|
+
.withDescription(`Send the file as multipart/form-data in the \`file\` field. Allowed types: ${[
|
|
78
|
+
...ALLOWED_MIME_TYPES,
|
|
79
|
+
].join(", ")}. Maximum size: ${maxFileSize} bytes.`)
|
|
80
|
+
.withRequestBody({ file: { format: "binary", required: true, type: "string" } }, { mediaType: "multipart/form-data" })
|
|
48
81
|
.withResponse(200, {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
82
|
+
data: {
|
|
83
|
+
properties: uploadResultSchema,
|
|
84
|
+
required: Object.keys(uploadResultSchema),
|
|
85
|
+
type: "object",
|
|
86
|
+
},
|
|
87
|
+
}, { required: ["data"] })
|
|
88
|
+
// Multer puts the file on req.file, not req.body, so JSON body validation must stay off.
|
|
89
|
+
.withValidation({ enabled: false })
|
|
56
90
|
.build(),
|
|
57
91
|
], (0, api_1.asyncHandler)(async (req, res) => {
|
|
58
92
|
await (0, fileUploadsGate_1.assertFileUploadsEnabled)(req, options.fileUploadsEnabled);
|
|
93
|
+
const storage = fileStorageService();
|
|
59
94
|
const file = req.file;
|
|
60
95
|
const userId = requestUserId(req);
|
|
61
96
|
if (!file) {
|
|
62
97
|
throw new api_1.APIError({ status: 400, title: "No file provided" });
|
|
63
98
|
}
|
|
64
|
-
const result = await
|
|
99
|
+
const result = await storage.upload({
|
|
65
100
|
buffer: file.buffer,
|
|
66
101
|
filename: file.originalname,
|
|
67
102
|
mimeType: file.mimetype,
|
|
@@ -75,9 +110,16 @@ const addFileRoutes = (router, options) => {
|
|
|
75
110
|
.withTags(["files"])
|
|
76
111
|
.withSummary("Get file URL")
|
|
77
112
|
.withPathParameter("gcsKey", { type: "string" })
|
|
78
|
-
.withResponse(200, {
|
|
113
|
+
.withResponse(200, {
|
|
114
|
+
data: {
|
|
115
|
+
properties: { url: { description: "Signed URL for the file", type: "string" } },
|
|
116
|
+
required: ["url"],
|
|
117
|
+
type: "object",
|
|
118
|
+
},
|
|
119
|
+
}, { required: ["data"] })
|
|
79
120
|
.build(),
|
|
80
121
|
], (0, api_1.asyncHandler)(async (req, res) => {
|
|
122
|
+
const storage = fileStorageService();
|
|
81
123
|
const gcsKey = gcsKeyParam(req);
|
|
82
124
|
// Another user's file is "not found", so keys cannot be probed.
|
|
83
125
|
const attachment = await fileAttachment_1.FileAttachment.findOneOrNone({
|
|
@@ -88,7 +130,7 @@ const addFileRoutes = (router, options) => {
|
|
|
88
130
|
if (!attachment) {
|
|
89
131
|
throw new api_1.APIError({ status: 404, title: "File not found" });
|
|
90
132
|
}
|
|
91
|
-
const url = await
|
|
133
|
+
const url = await storage.getSignedUrl(gcsKey);
|
|
92
134
|
return res.json({ data: { url } });
|
|
93
135
|
}));
|
|
94
136
|
router.delete("/files/*gcsKey", [
|
|
@@ -97,9 +139,16 @@ const addFileRoutes = (router, options) => {
|
|
|
97
139
|
.withTags(["files"])
|
|
98
140
|
.withSummary("Delete a file")
|
|
99
141
|
.withPathParameter("gcsKey", { type: "string" })
|
|
100
|
-
.withResponse(200, {
|
|
142
|
+
.withResponse(200, {
|
|
143
|
+
data: {
|
|
144
|
+
properties: { success: { type: "boolean" } },
|
|
145
|
+
required: ["success"],
|
|
146
|
+
type: "object",
|
|
147
|
+
},
|
|
148
|
+
}, { required: ["data"] })
|
|
101
149
|
.build(),
|
|
102
150
|
], (0, api_1.asyncHandler)(async (req, res) => {
|
|
151
|
+
const storage = fileStorageService();
|
|
103
152
|
const gcsKey = gcsKeyParam(req);
|
|
104
153
|
const userId = requestUserId(req);
|
|
105
154
|
const attachment = await fileAttachment_1.FileAttachment.findOneOrNone({ deleted: false, gcsKey });
|
|
@@ -109,7 +158,7 @@ const addFileRoutes = (router, options) => {
|
|
|
109
158
|
if (attachment.userId.toString() !== userId.toString()) {
|
|
110
159
|
throw new api_1.APIError({ status: 403, title: "Not authorized to delete this file" });
|
|
111
160
|
}
|
|
112
|
-
await
|
|
161
|
+
await storage.delete(gcsKey);
|
|
113
162
|
return res.json({ data: { success: true } });
|
|
114
163
|
}));
|
|
115
164
|
};
|
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.addGptActionRoutes = exports.HOST_ACTION_TIMEOUT_MS = void 0;
|
|
7
4
|
const api_1 = require("@terreno/api");
|
|
8
5
|
const blocks_1 = require("@terreno/blocks");
|
|
9
6
|
const luxon_1 = require("luxon");
|
|
10
|
-
const mongoose_1 = __importDefault(require("mongoose"));
|
|
11
7
|
const aiRequest_1 = require("../models/aiRequest");
|
|
12
8
|
const gptHistory_1 = require("../models/gptHistory");
|
|
13
9
|
/**
|
|
@@ -124,9 +120,6 @@ const addGptActionRoutes = (router, options) => {
|
|
|
124
120
|
title: "historyId, messageId, blockId, elementId, and name are required",
|
|
125
121
|
});
|
|
126
122
|
}
|
|
127
|
-
if (!mongoose_1.default.Types.ObjectId.isValid(historyId)) {
|
|
128
|
-
throw new api_1.APIError({ status: 400, title: "historyId is not valid" });
|
|
129
|
-
}
|
|
130
123
|
const userId = requestUserId(req);
|
|
131
124
|
const history = await gptHistory_1.GptHistory.findOneOrNone({ _id: historyId, deleted: false });
|
|
132
125
|
if (!history) {
|
|
@@ -7,6 +7,8 @@ const luxon_1 = require("luxon");
|
|
|
7
7
|
const gptHistory_1 = require("../models/gptHistory");
|
|
8
8
|
const asks_1 = require("../service/asks");
|
|
9
9
|
const chatTurn_1 = require("../service/chatTurn");
|
|
10
|
+
/** Route path of the history CRUD; the sync collection tag is this path without the slash. */
|
|
11
|
+
const GPT_HISTORIES_PATH = "/gpt/histories";
|
|
10
12
|
/**
|
|
11
13
|
* Only the chat turn writes `pendingAsk`: it holds messages replayed verbatim to the model, so a
|
|
12
14
|
* client must not be able to plant or edit one, including through dotted paths.
|
|
@@ -237,7 +239,9 @@ const addGptHistoryRoutes = (router, options) => {
|
|
|
237
239
|
if (headlessChat) {
|
|
238
240
|
(0, asks_1.assertNoReservedToolNames)(headlessChat.tools);
|
|
239
241
|
}
|
|
240
|
-
|
|
242
|
+
// The three-argument form registers the collection, so SyncApp serves it to @terreno/syncdb
|
|
243
|
+
// clients as the `gpt/histories` collection on the caller's owner stream.
|
|
244
|
+
const registration = (0, api_1.modelRouter)(GPT_HISTORIES_PATH, gptHistory_1.GptHistory, {
|
|
241
245
|
...openApiOptions,
|
|
242
246
|
...routerOptions,
|
|
243
247
|
...(headlessChat ? headlessActions({ chat: headlessChat, routerOptions }) : {}),
|
|
@@ -278,7 +282,9 @@ const addGptHistoryRoutes = (router, options) => {
|
|
|
278
282
|
: withClientPendingAsk(serialized));
|
|
279
283
|
},
|
|
280
284
|
sort: "-updated",
|
|
285
|
+
sync: { scope: { field: "userId", type: "owner" } },
|
|
281
286
|
validation: withCallerUserId(routerOptions.validation),
|
|
282
|
-
})
|
|
287
|
+
});
|
|
288
|
+
router.use(registration.path, registration.router);
|
|
283
289
|
};
|
|
284
290
|
exports.addGptHistoryRoutes = addGptHistoryRoutes;
|
|
@@ -91,7 +91,8 @@ const registerAiDataset = async ({ columns, historyId, rows, userId, }) => {
|
|
|
91
91
|
assertRows(columns, rows);
|
|
92
92
|
const created = await aiDataset_1.AIDataset.create({
|
|
93
93
|
columns,
|
|
94
|
-
|
|
94
|
+
// GptHistory ids are Strings; an ObjectId from an older caller is stored as its hex string.
|
|
95
|
+
historyId: String(historyId),
|
|
95
96
|
rowCount: rows.length,
|
|
96
97
|
rows,
|
|
97
98
|
userId,
|
package/dist/service/chatTurn.js
CHANGED
|
@@ -155,6 +155,10 @@ const loadHistory = async ({ historyId, projectId, userId, }) => {
|
|
|
155
155
|
if (!historyId) {
|
|
156
156
|
return new gptHistory_1.GptHistory({ prompts: [], userId, ...(projectId ? { projectId } : {}) });
|
|
157
157
|
}
|
|
158
|
+
// History ids are Strings (synced collection); an object here would be read as a query operator.
|
|
159
|
+
if (typeof historyId !== "string") {
|
|
160
|
+
throw new api_1.APIError({ status: 400, title: "historyId must be a string" });
|
|
161
|
+
}
|
|
158
162
|
const history = await gptHistory_1.GptHistory.findById(historyId);
|
|
159
163
|
if (!history) {
|
|
160
164
|
throw new api_1.APIError({ status: 404, title: "History not found" });
|
package/dist/service/vertex.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ImageModelV4, LanguageModelV4 } from "@ai-sdk/provider";
|
|
1
2
|
import type { ImageModel, LanguageModel } from "ai";
|
|
2
3
|
/**
|
|
3
4
|
* Helper for Google's Vertex AI provider, now part of the "Gemini Enterprise Agent Platform"
|
|
@@ -16,8 +17,10 @@ import type { ImageModel, LanguageModel } from "ai";
|
|
|
16
17
|
export declare const DEFAULT_VERTEX_LOCATION = "global";
|
|
17
18
|
/** Provider returned by `@ai-sdk/google-vertex` `createVertex`. */
|
|
18
19
|
export interface VertexLanguageModelProvider {
|
|
19
|
-
|
|
20
|
-
|
|
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;
|
|
21
24
|
}
|
|
22
25
|
interface VertexModule {
|
|
23
26
|
createVertex: (opts: {
|
package/dist/types/harness.d.ts
CHANGED
|
@@ -536,9 +536,12 @@ export interface HarnessToolDefinition<Args = unknown, Result = unknown> extends
|
|
|
536
536
|
}
|
|
537
537
|
/**
|
|
538
538
|
* Any tool, whatever its argument and result types. `execute` arguments are contravariant, so
|
|
539
|
-
* `never` accepts every tool in heterogeneous lists without an explicit `any`.
|
|
539
|
+
* `never` accepts every tool in heterogeneous lists without an explicit `any`. A Zod schema's
|
|
540
|
+
* output is covariant, so `parameters` widens to `unknown` instead.
|
|
540
541
|
*/
|
|
541
|
-
export
|
|
542
|
+
export interface AnyHarnessToolDefinition extends Omit<HarnessToolDefinition<never, unknown>, "parameters"> {
|
|
543
|
+
parameters: z.ZodType<unknown>;
|
|
544
|
+
}
|
|
542
545
|
export interface HarnessAgentDefinitionInput {
|
|
543
546
|
/** Tried in order once the primary model's retryable failures exhaust `modelRetry`. */
|
|
544
547
|
fallbackModels?: HarnessModelRef[];
|
package/dist/types/index.d.ts
CHANGED
|
@@ -113,7 +113,9 @@ interface GptHistoryPendingAskState {
|
|
|
113
113
|
}
|
|
114
114
|
/** The ask a history is waiting on. At most one per history. */
|
|
115
115
|
export type GptHistoryPendingAsk = Ask & GptHistoryPendingAskState;
|
|
116
|
-
|
|
116
|
+
/** Synced through @terreno/syncdb, so `_id` is a String (UUID for new rows, ObjectId hex for migrated rows). */
|
|
117
|
+
export interface GptHistoryDocument extends mongoose.Document<string> {
|
|
118
|
+
_id: string;
|
|
117
119
|
created: Date;
|
|
118
120
|
deleted: boolean;
|
|
119
121
|
pendingAsk?: GptHistoryPendingAsk;
|
|
@@ -435,7 +437,8 @@ export interface AIDatasetDocument extends mongoose.Document<mongoose.Types.Obje
|
|
|
435
437
|
created: Date;
|
|
436
438
|
deleted: boolean;
|
|
437
439
|
expiresAt?: Date;
|
|
438
|
-
|
|
440
|
+
/** The GptHistory String `_id`. */
|
|
441
|
+
historyId: string;
|
|
439
442
|
rowCount: number;
|
|
440
443
|
rows: AIDatasetCell[][];
|
|
441
444
|
updated: Date;
|
package/e2e/smoke.ts
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Real-process smoke for @terreno/ai. Run `bun e2e/smoke.ts` after `bun run compile`.
|
|
3
|
+
*
|
|
4
|
+
* The smoke re-runs this file with `serve` as a child process: a minimal TerrenoApp from the
|
|
5
|
+
* compiled `@terreno/api` with the compiled `AiApp` registered, on a free port, against
|
|
6
|
+
* `smokeMongoUri()`. The child's `AIService` wraps a deterministic fake provider
|
|
7
|
+
* (`MockLanguageModelV3` from `ai/test`) and runs with every provider key unset. The parent then
|
|
8
|
+
* checks over real HTTP that a signed-in user's chat request streams back the fake provider's
|
|
9
|
+
* text. Exits non-zero on the first failed check.
|
|
10
|
+
*/
|
|
11
|
+
import {
|
|
12
|
+
freePort,
|
|
13
|
+
type SpawnedProcess,
|
|
14
|
+
smokeMongoUri,
|
|
15
|
+
spawnUntilReady,
|
|
16
|
+
stopSmokeMongo,
|
|
17
|
+
} from "@terreno/test";
|
|
18
|
+
import type express from "express";
|
|
19
|
+
import {DateTime} from "luxon";
|
|
20
|
+
import type {Schema} from "mongoose";
|
|
21
|
+
|
|
22
|
+
const SERVE_ARG = "serve";
|
|
23
|
+
const FAKE_REPLY = "Hello from the ai smoke's fake provider.";
|
|
24
|
+
const FAKE_TITLE = "Smoke chat";
|
|
25
|
+
|
|
26
|
+
/** Provider credentials the smoke unsets in the child, so no real provider can answer. */
|
|
27
|
+
const PROVIDER_ENV_KEYS = [
|
|
28
|
+
"ANTHROPIC_API_KEY",
|
|
29
|
+
"GEMINI_API_KEY",
|
|
30
|
+
"GOOGLE_APPLICATION_CREDENTIALS",
|
|
31
|
+
"GOOGLE_GENERATIVE_AI_API_KEY",
|
|
32
|
+
"GOOGLE_VERTEX_PROJECT",
|
|
33
|
+
"LANGFUSE_PUBLIC_KEY",
|
|
34
|
+
"LANGFUSE_SECRET_KEY",
|
|
35
|
+
"OPENAI_API_KEY",
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
interface ChatEvent {
|
|
39
|
+
done?: boolean;
|
|
40
|
+
error?: string;
|
|
41
|
+
historyId?: string;
|
|
42
|
+
text?: string;
|
|
43
|
+
title?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const ZERO_USAGE = {
|
|
47
|
+
inputTokens: {cacheRead: 0, cacheWrite: 0, noCache: 0, total: 0},
|
|
48
|
+
outputTokens: {reasoning: 0, text: 0, total: 0},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Child: the compiled API with auth, the compiled AiApp on a fake model, and a health plugin.
|
|
53
|
+
* The packages are imported here, not at the top, so the parent process never loads them.
|
|
54
|
+
*/
|
|
55
|
+
const serve = async (): Promise<void> => {
|
|
56
|
+
const {createdUpdatedPlugin, logger, TerrenoApp} = await import("@terreno/api");
|
|
57
|
+
const {AIService, AiApp} = await import("@terreno/ai");
|
|
58
|
+
const {MockLanguageModelV3} = await import("ai/test");
|
|
59
|
+
const {default: mongoose} = await import("mongoose");
|
|
60
|
+
const {default: passportLocalMongoose} = await import("passport-local-mongoose");
|
|
61
|
+
|
|
62
|
+
const userSchema = new mongoose.Schema({
|
|
63
|
+
admin: {default: false, description: "Whether the user is an admin", type: Boolean},
|
|
64
|
+
email: {description: "Sign-in email", type: String},
|
|
65
|
+
name: {description: "Display name", type: String},
|
|
66
|
+
});
|
|
67
|
+
userSchema.plugin(passportLocalMongoose as unknown as (schema: Schema, options: object) => void, {
|
|
68
|
+
usernameCaseInsensitive: true,
|
|
69
|
+
usernameField: "email",
|
|
70
|
+
});
|
|
71
|
+
userSchema.plugin(createdUpdatedPlugin);
|
|
72
|
+
const User = mongoose.model("User", userSchema);
|
|
73
|
+
|
|
74
|
+
// Streams the chat reply and answers the title request, the two calls one chat turn makes.
|
|
75
|
+
const model = new MockLanguageModelV3({
|
|
76
|
+
doGenerate: async () => ({
|
|
77
|
+
content: [{text: FAKE_TITLE, type: "text"}],
|
|
78
|
+
finishReason: {raw: "stop", unified: "stop"},
|
|
79
|
+
usage: ZERO_USAGE,
|
|
80
|
+
warnings: [],
|
|
81
|
+
}),
|
|
82
|
+
doStream: async () => ({
|
|
83
|
+
stream: new ReadableStream({
|
|
84
|
+
start(controller): void {
|
|
85
|
+
controller.enqueue({type: "stream-start", warnings: []});
|
|
86
|
+
controller.enqueue({id: "text-1", type: "text-start"});
|
|
87
|
+
controller.enqueue({delta: FAKE_REPLY, id: "text-1", type: "text-delta"});
|
|
88
|
+
controller.enqueue({id: "text-1", type: "text-end"});
|
|
89
|
+
controller.enqueue({
|
|
90
|
+
finishReason: {raw: "stop", unified: "stop"},
|
|
91
|
+
type: "finish",
|
|
92
|
+
usage: ZERO_USAGE,
|
|
93
|
+
});
|
|
94
|
+
controller.close();
|
|
95
|
+
},
|
|
96
|
+
}),
|
|
97
|
+
}),
|
|
98
|
+
modelId: "ai-smoke-fake-model",
|
|
99
|
+
provider: "ai-smoke-fake-provider",
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const healthPlugin = {
|
|
103
|
+
register: (app: express.Application): void => {
|
|
104
|
+
app.get("/health", (_req, res) => {
|
|
105
|
+
res.json({healthy: mongoose.connection.readyState === 1});
|
|
106
|
+
});
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
await mongoose.connect(process.env.MONGO_URI as string);
|
|
111
|
+
const app = new TerrenoApp({
|
|
112
|
+
loggingOptions: {logRequests: false},
|
|
113
|
+
userModel: User as unknown as ConstructorParameters<typeof TerrenoApp>[0]["userModel"],
|
|
114
|
+
})
|
|
115
|
+
.register(new AiApp({aiService: new AIService({model})}))
|
|
116
|
+
.register(healthPlugin);
|
|
117
|
+
app.start();
|
|
118
|
+
await app.whenReady();
|
|
119
|
+
logger.info(`[aiSmoke] serving on port ${process.env.PORT}`);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const expectStatus = async (res: Response, status: number, step: string): Promise<string> => {
|
|
123
|
+
const body = await res.text();
|
|
124
|
+
if (res.status !== status) {
|
|
125
|
+
throw new Error(`${step}: expected ${status}, got ${res.status}: ${body}`);
|
|
126
|
+
}
|
|
127
|
+
return body;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/** Splits a finished `text/event-stream` body into its JSON `data:` events. */
|
|
131
|
+
const parseEvents = (body: string): ChatEvent[] =>
|
|
132
|
+
body
|
|
133
|
+
.split("\n\n")
|
|
134
|
+
.map((chunk) => chunk.trim())
|
|
135
|
+
.filter((chunk) => chunk.startsWith("data: "))
|
|
136
|
+
.map((chunk) => JSON.parse(chunk.slice("data: ".length)) as ChatEvent);
|
|
137
|
+
|
|
138
|
+
/** Parent: boots the child and sends one chat request over HTTP. */
|
|
139
|
+
const run = async (): Promise<void> => {
|
|
140
|
+
const startedAt = DateTime.now();
|
|
141
|
+
const port = await freePort();
|
|
142
|
+
const baseUrl = `http://127.0.0.1:${port}`;
|
|
143
|
+
let server: SpawnedProcess | undefined;
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
server = await spawnUntilReady({
|
|
147
|
+
cmd: [process.execPath, __filename, SERVE_ARG],
|
|
148
|
+
env: {
|
|
149
|
+
...Object.fromEntries(PROVIDER_ENV_KEYS.map((key) => [key, undefined])),
|
|
150
|
+
MONGO_URI: await smokeMongoUri({databaseName: "aiSmoke"}),
|
|
151
|
+
PORT: String(port),
|
|
152
|
+
REFRESH_TOKEN_SECRET: "ai-smoke-refresh-secret",
|
|
153
|
+
SESSION_SECRET: "ai-smoke-session-secret",
|
|
154
|
+
TOKEN_ISSUER: "ai-smoke",
|
|
155
|
+
TOKEN_SECRET: "ai-smoke-token-secret",
|
|
156
|
+
},
|
|
157
|
+
timeoutMs: 30000,
|
|
158
|
+
url: `${baseUrl}/health`,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const signup = JSON.parse(
|
|
162
|
+
await expectStatus(
|
|
163
|
+
await fetch(`${baseUrl}/auth/signup`, {
|
|
164
|
+
body: JSON.stringify({
|
|
165
|
+
email: `ai-smoke-${startedAt.toMillis()}@example.com`,
|
|
166
|
+
password: "ai-smoke-password-1",
|
|
167
|
+
}),
|
|
168
|
+
headers: {"Content-Type": "application/json"},
|
|
169
|
+
method: "POST",
|
|
170
|
+
}),
|
|
171
|
+
200,
|
|
172
|
+
"POST /auth/signup"
|
|
173
|
+
)
|
|
174
|
+
) as {data: {token: string}};
|
|
175
|
+
console.info("✅ POST /auth/signup signs in a user");
|
|
176
|
+
|
|
177
|
+
const chat = await fetch(`${baseUrl}/gpt/prompt`, {
|
|
178
|
+
body: JSON.stringify({prompt: "Say hello."}),
|
|
179
|
+
headers: {
|
|
180
|
+
Authorization: `Bearer ${signup.data.token}`,
|
|
181
|
+
"Content-Type": "application/json",
|
|
182
|
+
},
|
|
183
|
+
method: "POST",
|
|
184
|
+
});
|
|
185
|
+
const events = parseEvents(await expectStatus(chat, 200, "POST /gpt/prompt"));
|
|
186
|
+
const failed = events.find((event) => event.error);
|
|
187
|
+
if (failed) {
|
|
188
|
+
throw new Error(`POST /gpt/prompt: the stream reported an error: ${failed.error}`);
|
|
189
|
+
}
|
|
190
|
+
const reply = events.map((event) => event.text ?? "").join("");
|
|
191
|
+
if (reply !== FAKE_REPLY) {
|
|
192
|
+
throw new Error(
|
|
193
|
+
`POST /gpt/prompt: expected the fake provider's text, got ${JSON.stringify(events)}`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
const done = events.find((event) => event.done);
|
|
197
|
+
if (!done?.historyId || done.title !== FAKE_TITLE) {
|
|
198
|
+
throw new Error(
|
|
199
|
+
`POST /gpt/prompt: expected a done event with a history and the fake title, got ${JSON.stringify(done)}`
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
console.info("✅ POST /gpt/prompt streams the fake provider's reply");
|
|
203
|
+
} catch (error: unknown) {
|
|
204
|
+
if (server) {
|
|
205
|
+
console.error(`--- server output ---\n${server.output()}`);
|
|
206
|
+
}
|
|
207
|
+
throw error;
|
|
208
|
+
} finally {
|
|
209
|
+
await server?.stop();
|
|
210
|
+
await stopSmokeMongo();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
console.info(`ai smoke passed in ${DateTime.now().diff(startedAt).toMillis()} ms`);
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const main = process.argv[2] === SERVE_ARG ? serve : run;
|
|
217
|
+
main().then(
|
|
218
|
+
() => {
|
|
219
|
+
if (main === run) {
|
|
220
|
+
process.exit(0);
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
(error: unknown) => {
|
|
224
|
+
console.error(`❌ ${error instanceof Error ? error.message : String(error)}`);
|
|
225
|
+
process.exit(1);
|
|
226
|
+
}
|
|
227
|
+
);
|
package/package.json
CHANGED
|
@@ -4,17 +4,17 @@
|
|
|
4
4
|
"url": "https://github.com/TerrenoLabs/terreno/issues"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@ai-sdk/mcp": "^
|
|
7
|
+
"@ai-sdk/mcp": "^2.0.57",
|
|
8
8
|
"@google-cloud/storage": "^7.15.0",
|
|
9
9
|
"@langfuse/client": "^5.0.1",
|
|
10
10
|
"@langfuse/otel": "^5.0.1",
|
|
11
11
|
"@opentelemetry/api": "^1.9.0",
|
|
12
|
-
"@opentelemetry/sdk-node": "0.
|
|
13
|
-
"@terreno/blocks": "
|
|
12
|
+
"@opentelemetry/sdk-node": "0.222.0",
|
|
13
|
+
"@terreno/blocks": "58.0.0-beta.1",
|
|
14
14
|
"ai": "^6.0.97",
|
|
15
15
|
"ajv": "8.20.0",
|
|
16
16
|
"csv-parse": "^6.1.0",
|
|
17
|
-
"google-auth-library": "^
|
|
17
|
+
"google-auth-library": "^11.1.0",
|
|
18
18
|
"luxon": "^3.7.2",
|
|
19
19
|
"multer": "2.2.0",
|
|
20
20
|
"sanitize-html": "^2.17.0"
|
|
@@ -22,10 +22,10 @@
|
|
|
22
22
|
"description": "Provider-agnostic AI service with streaming chat, request logging, and admin tools — part of Terreno, the batteries-included TypeScript framework for universal apps.",
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@biomejs/biome": "^2.3.6",
|
|
25
|
-
"@terreno/admin-backend": "
|
|
26
|
-
"@terreno/api": "
|
|
27
|
-
"@terreno/jobs": "
|
|
28
|
-
"@terreno/test": "
|
|
25
|
+
"@terreno/admin-backend": "58.0.0-beta.1",
|
|
26
|
+
"@terreno/api": "58.0.0-beta.1",
|
|
27
|
+
"@terreno/jobs": "58.0.0-beta.1",
|
|
28
|
+
"@terreno/test": "58.0.0-beta.1",
|
|
29
29
|
"@types/bun": "^1.4.0",
|
|
30
30
|
"@types/express": "^5.0.6",
|
|
31
31
|
"@types/luxon": "^3.7.1",
|
|
@@ -135,9 +135,10 @@
|
|
|
135
135
|
"main": "dist/index.js",
|
|
136
136
|
"name": "@terreno/ai",
|
|
137
137
|
"peerDependencies": {
|
|
138
|
-
"@ai-sdk/google-vertex": "^
|
|
139
|
-
"@
|
|
140
|
-
"@terreno/
|
|
138
|
+
"@ai-sdk/google-vertex": "^5.0.92",
|
|
139
|
+
"@ai-sdk/provider": "^4.0.26",
|
|
140
|
+
"@terreno/api": "58.0.0-beta.1",
|
|
141
|
+
"@terreno/jobs": "58.0.0-beta.1",
|
|
141
142
|
"mongoose": "^8.0.0 || ^9.0.0"
|
|
142
143
|
},
|
|
143
144
|
"peerDependenciesMeta": {
|
|
@@ -159,14 +160,16 @@
|
|
|
159
160
|
"compile": "bun tsc",
|
|
160
161
|
"compile:watch": "bun tsc -w",
|
|
161
162
|
"dev": "bun tsc -w",
|
|
162
|
-
"lint": "biome check ./src",
|
|
163
|
-
"lint:fix": "biome check --write ./src",
|
|
164
|
-
"lint:unsafefix": "biome check --fix --unsafe ./src",
|
|
163
|
+
"lint": "biome check ./src ./e2e",
|
|
164
|
+
"lint:fix": "biome check --write ./src ./e2e",
|
|
165
|
+
"lint:unsafefix": "biome check --fix --unsafe ./src ./e2e",
|
|
166
|
+
"smoke": "bun e2e/smoke.ts",
|
|
165
167
|
"test": "bun test --preload ./src/tests/bunSetup.ts && for f in ./src/isolated/*.isolated.ts; do bun test --preload ./src/tests/bunSetup.ts \"$f\" || exit 1; done",
|
|
166
168
|
"test:agent": "AGENT=1 bun test --only-failures --preload ./src/tests/bunSetup.ts && for f in ./src/isolated/*.isolated.ts; do AGENT=1 bun test --only-failures --preload ./src/tests/bunSetup.ts \"$f\" || exit 1; done",
|
|
167
169
|
"test:ci": "bun test --preload ./src/tests/bunSetup.ts && for f in ./src/isolated/*.isolated.ts; do bun test --preload ./src/tests/bunSetup.ts \"$f\" || exit 1; done",
|
|
168
|
-
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
170
|
+
"test:coverage": "bun run ../scripts/check-coverage.ts",
|
|
171
|
+
"typecheck": "tsc -p tsconfig.typecheck.json"
|
|
169
172
|
},
|
|
170
173
|
"types": "dist/index.d.ts",
|
|
171
|
-
"version": "
|
|
174
|
+
"version": "58.0.0-beta.1"
|
|
172
175
|
}
|