@terreno/ai 0.14.0 → 0.14.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/dist/models/project.js +1 -1
- package/package.json +3 -3
- package/src/models/project.ts +1 -1
- package/src/routes/routes.test.ts +110 -0
- package/src/service/aiService.test.ts +11 -10
package/dist/models/project.js
CHANGED
|
@@ -15,7 +15,7 @@ var memorySchema = new mongoose_1.default.Schema({
|
|
|
15
15
|
type: String,
|
|
16
16
|
},
|
|
17
17
|
text: { description: "The memory content", required: true, type: String },
|
|
18
|
-
}, { timestamps: { createdAt: "created", updatedAt: false } });
|
|
18
|
+
}, { strict: "throw", timestamps: { createdAt: "created", updatedAt: false } });
|
|
19
19
|
var projectSchema = new mongoose_1.default.Schema({
|
|
20
20
|
memories: {
|
|
21
21
|
default: [],
|
package/package.json
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@types/multer": "^1.4.12",
|
|
24
24
|
"@types/passport-local-mongoose": "^6.1.5",
|
|
25
25
|
"@types/supertest": "^6.0.2",
|
|
26
|
-
"@terreno/api": "0.14.
|
|
26
|
+
"@terreno/api": "0.14.1",
|
|
27
27
|
"mongoose": "8.23.0",
|
|
28
28
|
"passport-local-mongoose": "^9.0.1",
|
|
29
29
|
"supertest": "^7.0.0",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"access": "public"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"@terreno/api": "0.14.
|
|
67
|
+
"@terreno/api": "0.14.1",
|
|
68
68
|
"mongoose": "^8.0.0"
|
|
69
69
|
},
|
|
70
70
|
"repository": {
|
|
@@ -83,5 +83,5 @@
|
|
|
83
83
|
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
84
84
|
},
|
|
85
85
|
"types": "dist/index.d.ts",
|
|
86
|
-
"version": "0.14.
|
|
86
|
+
"version": "0.14.1"
|
|
87
87
|
}
|
package/src/models/project.ts
CHANGED
|
@@ -14,7 +14,7 @@ const memorySchema = new mongoose.Schema(
|
|
|
14
14
|
},
|
|
15
15
|
text: {description: "The memory content", required: true, type: String},
|
|
16
16
|
},
|
|
17
|
-
{timestamps: {createdAt: "created", updatedAt: false}}
|
|
17
|
+
{strict: "throw", timestamps: {createdAt: "created", updatedAt: false}}
|
|
18
18
|
);
|
|
19
19
|
|
|
20
20
|
const projectSchema = new mongoose.Schema<ProjectDocument, ProjectModel>(
|
|
@@ -988,6 +988,61 @@ describe("AI Routes", () => {
|
|
|
988
988
|
expect(res.status).toBe(200);
|
|
989
989
|
expect(createRequestTools).toHaveBeenCalled();
|
|
990
990
|
});
|
|
991
|
+
|
|
992
|
+
it("streams with a model that emits a non-image file part (exercises file type guard)", async () => {
|
|
993
|
+
const nonImageFileModel = {
|
|
994
|
+
doGenerate: mock(async () => ({
|
|
995
|
+
content: [{text: "done", type: "text" as const}],
|
|
996
|
+
finishReason: "stop" as const,
|
|
997
|
+
usage: {inputTokens: 2, outputTokens: 2},
|
|
998
|
+
})),
|
|
999
|
+
doStream: mock(async () => ({
|
|
1000
|
+
stream: new ReadableStream({
|
|
1001
|
+
start(controller) {
|
|
1002
|
+
controller.enqueue({id: "t1", type: "text-start" as const});
|
|
1003
|
+
controller.enqueue({delta: "here is a file", id: "t1", type: "text-delta" as const});
|
|
1004
|
+
controller.enqueue({id: "t1", type: "text-end" as const});
|
|
1005
|
+
controller.enqueue({
|
|
1006
|
+
data: "UERGLWNvbnRlbnQ=",
|
|
1007
|
+
mediaType: "application/pdf",
|
|
1008
|
+
type: "file" as const,
|
|
1009
|
+
});
|
|
1010
|
+
controller.enqueue({
|
|
1011
|
+
finishReason: "stop" as const,
|
|
1012
|
+
type: "finish" as const,
|
|
1013
|
+
usage: {inputTokens: 2, outputTokens: 2},
|
|
1014
|
+
});
|
|
1015
|
+
controller.close();
|
|
1016
|
+
},
|
|
1017
|
+
}),
|
|
1018
|
+
})),
|
|
1019
|
+
modelId: "file-model",
|
|
1020
|
+
provider: "mock-provider",
|
|
1021
|
+
specificationVersion: "v2" as const,
|
|
1022
|
+
supportedUrls: {},
|
|
1023
|
+
};
|
|
1024
|
+
const fileApp = setupServer({
|
|
1025
|
+
addRoutes: (router, options) => {
|
|
1026
|
+
addGptRoutes(router, {
|
|
1027
|
+
aiService: new AIService({model: nonImageFileModel as unknown as LanguageModel}),
|
|
1028
|
+
openApiOptions: options,
|
|
1029
|
+
});
|
|
1030
|
+
},
|
|
1031
|
+
skipListen: true,
|
|
1032
|
+
userModel: UserModel,
|
|
1033
|
+
});
|
|
1034
|
+
const agent = await authAsUser(fileApp, "notAdmin");
|
|
1035
|
+
const res = await agent
|
|
1036
|
+
.post("/gpt/prompt")
|
|
1037
|
+
.send({prompt: "Give me a PDF"})
|
|
1038
|
+
.buffer(true)
|
|
1039
|
+
.parse(sseCollect);
|
|
1040
|
+
expect(res.status).toBe(200);
|
|
1041
|
+
const body = (res as SseResponse).body;
|
|
1042
|
+
// Non-image files should not produce an image SSE event
|
|
1043
|
+
expect(body).not.toContain('"image"');
|
|
1044
|
+
expect(body).toContain("done");
|
|
1045
|
+
});
|
|
991
1046
|
});
|
|
992
1047
|
|
|
993
1048
|
describe("GPT history rating route", () => {
|
|
@@ -1480,6 +1535,61 @@ describe("AI Routes", () => {
|
|
|
1480
1535
|
expect(res.body.title).toContain("Invalid endDate");
|
|
1481
1536
|
});
|
|
1482
1537
|
|
|
1538
|
+
it("streams with a model that emits a non-image file part (exercises file type guard)", async () => {
|
|
1539
|
+
const nonImageFileModel = {
|
|
1540
|
+
doGenerate: mock(async () => ({
|
|
1541
|
+
content: [{text: "done", type: "text" as const}],
|
|
1542
|
+
finishReason: "stop" as const,
|
|
1543
|
+
usage: {inputTokens: 2, outputTokens: 2},
|
|
1544
|
+
})),
|
|
1545
|
+
doStream: mock(async () => ({
|
|
1546
|
+
stream: new ReadableStream({
|
|
1547
|
+
start(controller) {
|
|
1548
|
+
controller.enqueue({id: "t1", type: "text-start" as const});
|
|
1549
|
+
controller.enqueue({delta: "here is a file", id: "t1", type: "text-delta" as const});
|
|
1550
|
+
controller.enqueue({id: "t1", type: "text-end" as const});
|
|
1551
|
+
controller.enqueue({
|
|
1552
|
+
data: "UERGLWNvbnRlbnQ=",
|
|
1553
|
+
mediaType: "application/pdf",
|
|
1554
|
+
type: "file" as const,
|
|
1555
|
+
});
|
|
1556
|
+
controller.enqueue({
|
|
1557
|
+
finishReason: "stop" as const,
|
|
1558
|
+
type: "finish" as const,
|
|
1559
|
+
usage: {inputTokens: 2, outputTokens: 2},
|
|
1560
|
+
});
|
|
1561
|
+
controller.close();
|
|
1562
|
+
},
|
|
1563
|
+
}),
|
|
1564
|
+
})),
|
|
1565
|
+
modelId: "file-model",
|
|
1566
|
+
provider: "mock-provider",
|
|
1567
|
+
specificationVersion: "v2" as const,
|
|
1568
|
+
supportedUrls: {},
|
|
1569
|
+
};
|
|
1570
|
+
const fileApp = setupServer({
|
|
1571
|
+
addRoutes: (router, options) => {
|
|
1572
|
+
addGptRoutes(router, {
|
|
1573
|
+
aiService: new AIService({model: nonImageFileModel as unknown as LanguageModel}),
|
|
1574
|
+
openApiOptions: options,
|
|
1575
|
+
});
|
|
1576
|
+
},
|
|
1577
|
+
skipListen: true,
|
|
1578
|
+
userModel: UserModel,
|
|
1579
|
+
});
|
|
1580
|
+
const agent = await authAsUser(fileApp, "notAdmin");
|
|
1581
|
+
const res = await agent
|
|
1582
|
+
.post("/gpt/prompt")
|
|
1583
|
+
.send({prompt: "Give me a PDF"})
|
|
1584
|
+
.buffer(true)
|
|
1585
|
+
.parse(sseCollect);
|
|
1586
|
+
expect(res.status).toBe(200);
|
|
1587
|
+
const body = (res as SseResponse).body;
|
|
1588
|
+
// Non-image files should not produce an image SSE event
|
|
1589
|
+
expect(body).not.toContain('"image"');
|
|
1590
|
+
expect(body).toContain("done");
|
|
1591
|
+
});
|
|
1592
|
+
|
|
1483
1593
|
it("filters by startDate and endDate range", async () => {
|
|
1484
1594
|
const a = await AIRequest.create({
|
|
1485
1595
|
aiModel: "gpt-4",
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
2
1
|
import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
3
2
|
import type {LanguageModel} from "ai";
|
|
4
3
|
import mongoose from "mongoose";
|
|
@@ -149,7 +148,7 @@ describe("AIService", () => {
|
|
|
149
148
|
|
|
150
149
|
it("should translate text with explicit sourceLanguage", async () => {
|
|
151
150
|
const model = createMockModel("Bonjour le monde");
|
|
152
|
-
const service = new AIService({model: model as
|
|
151
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
153
152
|
|
|
154
153
|
const result = await service.translateText({
|
|
155
154
|
sourceLanguage: "English",
|
|
@@ -163,7 +162,7 @@ describe("AIService", () => {
|
|
|
163
162
|
describe("modelId getter", () => {
|
|
164
163
|
it("should return the model id from the underlying model", () => {
|
|
165
164
|
const model = createMockModel();
|
|
166
|
-
const service = new AIService({model: model as
|
|
165
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
167
166
|
|
|
168
167
|
expect(service.modelId).toBe("mock-model");
|
|
169
168
|
});
|
|
@@ -180,7 +179,7 @@ describe("AIService", () => {
|
|
|
180
179
|
specificationVersion: "v2" as const,
|
|
181
180
|
supportedUrls: {},
|
|
182
181
|
};
|
|
183
|
-
const service = new AIService({model: model as
|
|
182
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
184
183
|
|
|
185
184
|
expect(service.modelId).toBe("unknown");
|
|
186
185
|
});
|
|
@@ -192,7 +191,7 @@ describe("AIService", () => {
|
|
|
192
191
|
model.doStream = mock(async () => {
|
|
193
192
|
throw new Error("Stream API error");
|
|
194
193
|
});
|
|
195
|
-
const service = new AIService({model: model as
|
|
194
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
196
195
|
|
|
197
196
|
const run = async () => {
|
|
198
197
|
const chunks: string[] = [];
|
|
@@ -212,7 +211,7 @@ describe("AIService", () => {
|
|
|
212
211
|
describe("buildMessages file content", () => {
|
|
213
212
|
it("should convert multi-modal user prompts with file content", () => {
|
|
214
213
|
const model = createMockModel();
|
|
215
|
-
const service = new AIService({model: model as
|
|
214
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
216
215
|
|
|
217
216
|
const messages = service.buildMessages([
|
|
218
217
|
{
|
|
@@ -231,7 +230,9 @@ describe("AIService", () => {
|
|
|
231
230
|
]);
|
|
232
231
|
|
|
233
232
|
expect(messages.length).toBe(1);
|
|
234
|
-
const content = (
|
|
233
|
+
const content = (
|
|
234
|
+
messages[0] as {content: Array<{type: string; filename?: string; mediaType?: string}>}
|
|
235
|
+
).content;
|
|
235
236
|
expect(content.length).toBe(2);
|
|
236
237
|
expect(content[1].type).toBe("file");
|
|
237
238
|
expect(content[1].filename).toBe("report.pdf");
|
|
@@ -242,7 +243,7 @@ describe("AIService", () => {
|
|
|
242
243
|
describe("generateChatStream", () => {
|
|
243
244
|
it("should stream chat responses and log the request", async () => {
|
|
244
245
|
const model = createMockModel();
|
|
245
|
-
const service = new AIService({model: model as
|
|
246
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
246
247
|
const userId = new mongoose.Types.ObjectId();
|
|
247
248
|
|
|
248
249
|
const chunks: string[] = [];
|
|
@@ -267,7 +268,7 @@ describe("AIService", () => {
|
|
|
267
268
|
model.doStream = mock(async () => {
|
|
268
269
|
throw new Error("Chat stream failed");
|
|
269
270
|
});
|
|
270
|
-
const service = new AIService({model: model as
|
|
271
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
271
272
|
|
|
272
273
|
const run = async () => {
|
|
273
274
|
const chunks: string[] = [];
|
|
@@ -287,7 +288,7 @@ describe("AIService", () => {
|
|
|
287
288
|
|
|
288
289
|
it("should accept systemPrompt override", async () => {
|
|
289
290
|
const model = createMockModel();
|
|
290
|
-
const service = new AIService({model: model as
|
|
291
|
+
const service = new AIService({model: model as unknown as LanguageModel});
|
|
291
292
|
|
|
292
293
|
const chunks: string[] = [];
|
|
293
294
|
for await (const chunk of service.generateChatStream({
|