@terreno/ai 0.13.3 → 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.
@@ -5,36 +5,45 @@ import type {GptHistoryDocument, GptHistoryModel} from "../types";
5
5
 
6
6
  const contentPartSchema = new mongoose.Schema(
7
7
  {
8
- filename: {type: String},
9
- mimeType: {type: String},
10
- text: {type: String},
11
- type: {enum: ["text", "image", "file"], required: true, type: String},
12
- url: {type: String},
8
+ filename: {description: "Original filename of the attached file", type: String},
9
+ mimeType: {description: "MIME type of the content part", type: String},
10
+ text: {description: "Text content of this part", type: String},
11
+ type: {
12
+ description: "The kind of content this part represents",
13
+ enum: ["text", "image", "file"],
14
+ required: true,
15
+ type: String,
16
+ },
17
+ url: {description: "URL pointing to the content resource", type: String},
13
18
  },
14
- {_id: false}
19
+ {_id: false, strict: "throw"}
15
20
  );
16
21
 
17
22
  const gptHistoryPromptSchema = new mongoose.Schema(
18
23
  {
19
- args: {type: mongoose.Schema.Types.Mixed},
20
- content: {type: [contentPartSchema]},
21
- model: {type: String},
24
+ args: {description: "Arguments passed to a tool call", type: mongoose.Schema.Types.Mixed},
25
+ content: {description: "Multipart content attached to this prompt", type: [contentPartSchema]},
26
+ model: {description: "AI model identifier used for this prompt", type: String},
22
27
  rating: {
23
28
  description: "User feedback rating for this prompt",
24
29
  enum: ["up", "down"],
25
30
  type: String,
26
31
  },
27
- result: {type: mongoose.Schema.Types.Mixed},
28
- text: {required: true, type: String},
29
- toolCallId: {type: String},
30
- toolName: {type: String},
32
+ result: {description: "Result returned from a tool call", type: mongoose.Schema.Types.Mixed},
33
+ text: {description: "Text content of the prompt or response", required: true, type: String},
34
+ toolCallId: {
35
+ description: "Identifier linking a tool result to its originating call",
36
+ type: String,
37
+ },
38
+ toolName: {description: "Name of the tool that was invoked", type: String},
31
39
  type: {
40
+ description: "Role of this message in the conversation",
32
41
  enum: ["user", "assistant", "system", "tool-call", "tool-result"],
33
42
  required: true,
34
43
  type: String,
35
44
  },
36
45
  },
37
- {_id: false}
46
+ {_id: false, strict: "throw"}
38
47
  );
39
48
 
40
49
  const gptHistorySchema = new mongoose.Schema<GptHistoryDocument, GptHistoryModel>(
@@ -45,9 +54,19 @@ const gptHistorySchema = new mongoose.Schema<GptHistoryDocument, GptHistoryModel
45
54
  ref: "Project",
46
55
  type: mongoose.Schema.Types.ObjectId,
47
56
  },
48
- prompts: {default: [], type: [gptHistoryPromptSchema]},
49
- title: {type: String},
50
- userId: {index: true, ref: "User", required: true, type: mongoose.Schema.Types.ObjectId},
57
+ prompts: {
58
+ default: [],
59
+ description: "Ordered list of messages in this conversation",
60
+ type: [gptHistoryPromptSchema],
61
+ },
62
+ title: {description: "Auto-generated title from the first assistant response", type: String},
63
+ userId: {
64
+ description: "The user who owns this conversation history",
65
+ index: true,
66
+ ref: "User",
67
+ required: true,
68
+ type: mongoose.Schema.Types.ObjectId,
69
+ },
51
70
  },
52
71
  {strict: "throw", toJSON: {virtuals: true}, toObject: {virtuals: true}}
53
72
  );
@@ -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>(
@@ -1,11 +1,12 @@
1
- import {asyncHandler, authenticateMiddleware, createOpenApiBuilder} from "@terreno/api";
1
+ import {APIError, asyncHandler, authenticateMiddleware, createOpenApiBuilder} from "@terreno/api";
2
2
  import type express from "express";
3
+ import {DateTime} from "luxon";
3
4
 
4
5
  import {AIRequest} from "../models/aiRequest";
5
6
  import type {AiRequestsExplorerRouteOptions} from "../types";
6
7
 
7
8
  export const addAiRequestsExplorerRoutes = (
8
- router: any,
9
+ router: express.Router,
9
10
  options?: AiRequestsExplorerRouteOptions
10
11
  ): void => {
11
12
  router.get(
@@ -31,7 +32,7 @@ export const addAiRequestsExplorerRoutes = (
31
32
  .build(),
32
33
  ],
33
34
  asyncHandler(async (req: express.Request, res: express.Response) => {
34
- const user = (req as any).user;
35
+ const user = req.user as {admin?: boolean} | undefined;
35
36
  if (!user?.admin) {
36
37
  return res.status(403).json({error: "Admin access required"});
37
38
  }
@@ -43,7 +44,9 @@ export const addAiRequestsExplorerRoutes = (
43
44
  );
44
45
  const skip = (page - 1) * limit;
45
46
 
46
- const match: Record<string, any> = {deleted: {$ne: true}};
47
+ const match: Record<string, unknown> & {created?: {$gte?: Date; $lte?: Date}} = {
48
+ deleted: {$ne: true},
49
+ };
47
50
 
48
51
  if (req.query.requestType) {
49
52
  match.requestType = req.query.requestType;
@@ -54,10 +57,21 @@ export const addAiRequestsExplorerRoutes = (
54
57
  if (req.query.startDate || req.query.endDate) {
55
58
  match.created = {};
56
59
  if (req.query.startDate) {
57
- match.created.$gte = new Date(req.query.startDate as string);
60
+ const startDt = DateTime.fromISO(req.query.startDate as string, {zone: "utc"});
61
+ if (!startDt.isValid) {
62
+ throw new APIError({
63
+ status: 400,
64
+ title: "Invalid startDate format (expected ISO 8601)",
65
+ });
66
+ }
67
+ match.created.$gte = startDt.toJSDate();
58
68
  }
59
69
  if (req.query.endDate) {
60
- match.created.$lte = new Date(req.query.endDate as string);
70
+ const endDt = DateTime.fromISO(req.query.endDate as string, {zone: "utc"});
71
+ if (!endDt.isValid) {
72
+ throw new APIError({status: 400, title: "Invalid endDate format (expected ISO 8601)"});
73
+ }
74
+ match.created.$lte = endDt.toJSDate();
61
75
  }
62
76
  }
63
77
 
@@ -21,7 +21,7 @@ const ALLOWED_MIME_TYPES = new Set([
21
21
  const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
22
22
 
23
23
  export const addFileRoutes = (
24
- router: any,
24
+ router: express.Router,
25
25
  options: FileRouteOptions & {fileStorageService: FileStorageService}
26
26
  ): void => {
27
27
  const {fileStorageService, maxFileSize = DEFAULT_MAX_FILE_SIZE} = options;
@@ -56,8 +56,9 @@ export const addFileRoutes = (
56
56
  .build(),
57
57
  ],
58
58
  asyncHandler(async (req: express.Request, res: express.Response) => {
59
- const file = (req as any).file as Express.Multer.File | undefined;
60
- const userId = (req as any).user?._id as mongoose.Types.ObjectId;
59
+ const file = (req as express.Request & {file?: Express.Multer.File}).file;
60
+ const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)
61
+ ?._id as mongoose.Types.ObjectId;
61
62
 
62
63
  if (!file) {
63
64
  throw new APIError({status: 400, title: "No file provided"});
@@ -87,7 +88,7 @@ export const addFileRoutes = (
87
88
  asyncHandler(async (req: express.Request, res: express.Response) => {
88
89
  const gcsKey = req.params.gcsKey as string;
89
90
 
90
- const attachment = await FileAttachment.findOne({deleted: false, gcsKey});
91
+ const attachment = await FileAttachment.findOneOrNone({deleted: false, gcsKey});
91
92
  if (!attachment) {
92
93
  throw new APIError({status: 404, title: "File not found"});
93
94
  }
@@ -110,9 +111,10 @@ export const addFileRoutes = (
110
111
  ],
111
112
  asyncHandler(async (req: express.Request, res: express.Response) => {
112
113
  const gcsKey = req.params.gcsKey as string;
113
- const userId = (req as any).user?._id as mongoose.Types.ObjectId;
114
+ const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)
115
+ ?._id as mongoose.Types.ObjectId;
114
116
 
115
- const attachment = await FileAttachment.findOne({deleted: false, gcsKey});
117
+ const attachment = await FileAttachment.findOneOrNone({deleted: false, gcsKey});
116
118
  if (!attachment) {
117
119
  throw new APIError({status: 404, title: "File not found"});
118
120
  }
package/src/routes/gpt.ts CHANGED
@@ -16,7 +16,12 @@ import {GptHistory} from "../models/gptHistory";
16
16
  import {Project} from "../models/project";
17
17
  import {AIService} from "../service/aiService";
18
18
  import {TITLE_GENERATION_PROMPT} from "../service/prompts";
19
- import type {GptHistoryPrompt, GptRouteOptions, MessageContentPart} from "../types";
19
+ import type {
20
+ GptHistoryDocument,
21
+ GptHistoryPrompt,
22
+ GptRouteOptions,
23
+ MessageContentPart,
24
+ } from "../types";
20
25
 
21
26
  const DEMO_RESPONSE =
22
27
  "This is demo mode. To use AI features, paste your Gemini API key in Settings.";
@@ -90,7 +95,7 @@ const generateTitle = async (
90
95
  }
91
96
  };
92
97
 
93
- export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
98
+ export const addGptRoutes = (router: express.Router, options: GptRouteOptions): void => {
94
99
  const {mcpService, tools: routeTools, createRequestTools, toolChoice, maxSteps} = options;
95
100
 
96
101
  router.post(
@@ -135,7 +140,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
135
140
  model: requestModel,
136
141
  projectId,
137
142
  } = req.body;
138
- const userId = (req as any).user?._id as mongoose.Types.ObjectId | undefined;
143
+ const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
139
144
 
140
145
  if (!prompt || typeof prompt !== "string") {
141
146
  throw new APIError({status: 400, title: "prompt is required"});
@@ -158,7 +163,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
158
163
  }
159
164
 
160
165
  // Load or create history
161
- let history;
166
+ let history: GptHistoryDocument | null;
162
167
  if (historyId) {
163
168
  history = await GptHistory.findById(historyId);
164
169
  if (!history) {
@@ -228,7 +233,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
228
233
  if (attachments && Array.isArray(attachments)) {
229
234
  logger.debug("Processing attachments", {
230
235
  count: attachments.length,
231
- types: attachments.map((a: any) => ({
236
+ types: attachments.map((a: {mimeType?: string; type?: string; url?: string}) => ({
232
237
  mimeType: a.mimeType,
233
238
  type: a.type,
234
239
  urlLength: a.url?.length ?? 0,
@@ -311,7 +316,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
311
316
  const result = streamText({
312
317
  experimental_telemetry: telemetry,
313
318
  messages,
314
- model: (aiService as any).model,
319
+ model: aiService.model,
315
320
  providerOptions: !supportsTools
316
321
  ? {
317
322
  google: {responseModalities: ["TEXT", "IMAGE"]},
@@ -320,7 +325,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
320
325
  : undefined,
321
326
  stopWhen: allTools ? stepCountIs(maxSteps ?? 5) : stepCountIs(1),
322
327
  system: effectiveSystemPrompt ?? undefined,
323
- temperature: (aiService as any).defaultTemperature,
328
+ temperature: aiService.defaultTemperature,
324
329
  toolChoice: allTools ? (toolChoice ?? "auto") : undefined,
325
330
  tools: allTools,
326
331
  });
@@ -332,7 +337,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
332
337
 
333
338
  for await (const part of result.fullStream as AsyncIterable<{
334
339
  type: string;
335
- [key: string]: any;
340
+ [key: string]: unknown;
336
341
  }>) {
337
342
  partCount++;
338
343
  if (partCount <= 5 || part.type === "error" || part.type === "file") {
@@ -407,9 +412,9 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
407
412
  // Persist tool call in history
408
413
  history.prompts.push({
409
414
  args: part.input as Record<string, unknown>,
410
- text: `Tool call: ${part.toolName}`,
411
- toolCallId: part.toolCallId,
412
- toolName: part.toolName,
415
+ text: `Tool call: ${part.toolName as string}`,
416
+ toolCallId: part.toolCallId as string,
417
+ toolName: part.toolName as string,
413
418
  type: "tool-call",
414
419
  });
415
420
  } else if (part.type === "tool-result") {
@@ -447,9 +452,9 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
447
452
  // Persist tool result in history (without the large file data)
448
453
  history.prompts.push({
449
454
  result: cleanResult as unknown,
450
- text: `Tool result: ${part.toolName}`,
451
- toolCallId: part.toolCallId,
452
- toolName: part.toolName,
455
+ text: `Tool result: ${part.toolName as string}`,
456
+ toolCallId: part.toolCallId as string,
457
+ toolName: part.toolName as string,
453
458
  type: "tool-result",
454
459
  });
455
460
  }
@@ -615,7 +620,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
615
620
  asyncHandler(async (req: express.Request, res: express.Response) => {
616
621
  const {id} = req.params;
617
622
  const {promptIndex, rating} = req.body;
618
- const userId = (req as any).user?._id as mongoose.Types.ObjectId | undefined;
623
+ const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
619
624
 
620
625
  if (typeof promptIndex !== "number" || promptIndex < 0) {
621
626
  throw new APIError({status: 400, title: "promptIndex must be a non-negative number"});
@@ -662,7 +667,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
662
667
  ],
663
668
  asyncHandler(async (req: express.Request, res: express.Response) => {
664
669
  const {text} = req.body;
665
- const userId = (req as any).user?._id as mongoose.Types.ObjectId | undefined;
670
+ const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
666
671
 
667
672
  if (!text || typeof text !== "string") {
668
673
  throw new APIError({status: 400, title: "text is required"});
@@ -1,9 +1,10 @@
1
1
  import {type ModelRouterOptions, modelRouter, Permissions} from "@terreno/api";
2
+ import type express from "express";
2
3
  import {GptHistory} from "../models/gptHistory";
3
4
  import type {GptHistoryDocument, GptHistoryRouteOptions} from "../types";
4
5
 
5
6
  export const addGptHistoryRoutes = (
6
- router: any,
7
+ router: express.Router,
7
8
  options?: Partial<ModelRouterOptions<GptHistoryDocument>> & GptHistoryRouteOptions
8
9
  ): void => {
9
10
  router.use(
package/src/routes/mcp.ts CHANGED
@@ -3,7 +3,7 @@ import type express from "express";
3
3
 
4
4
  import type {McpRouteOptions} from "../types";
5
5
 
6
- export const addMcpRoutes = (router: any, options: McpRouteOptions): void => {
6
+ export const addMcpRoutes = (router: express.Router, options: McpRouteOptions): void => {
7
7
  const {mcpService} = options;
8
8
 
9
9
  router.get(
@@ -20,7 +20,7 @@ export const addMcpRoutes = (router: any, options: McpRouteOptions): void => {
20
20
  .build(),
21
21
  ],
22
22
  asyncHandler(async (req: express.Request, res: express.Response) => {
23
- const user = (req as any).user;
23
+ const user = req.user as {admin?: boolean} | undefined;
24
24
  if (!user?.admin) {
25
25
  throw new APIError({status: 403, title: "Admin access required"});
26
26
  }
@@ -42,11 +42,10 @@ export const addMcpRoutes = (router: any, options: McpRouteOptions): void => {
42
42
  ],
43
43
  asyncHandler(async (_req: express.Request, res: express.Response) => {
44
44
  const tools = await mcpService.getTools();
45
- const toolList = Object.entries(tools).map(([name, tool]) => ({
46
- description: (tool as any).description,
47
- name,
48
- parameters: (tool as any).parameters,
49
- }));
45
+ const toolList = Object.entries(tools).map(([name, tool]) => {
46
+ const t = tool as {description?: string; parameters?: unknown};
47
+ return {description: t.description, name, parameters: t.parameters};
48
+ });
50
49
  return res.json({data: toolList});
51
50
  })
52
51
  );
@@ -63,7 +62,7 @@ export const addMcpRoutes = (router: any, options: McpRouteOptions): void => {
63
62
  .build(),
64
63
  ],
65
64
  asyncHandler(async (req: express.Request, res: express.Response) => {
66
- const user = (req as any).user;
65
+ const user = req.user as {admin?: boolean} | undefined;
67
66
  if (!user?.admin) {
68
67
  throw new APIError({status: 403, title: "Admin access required"});
69
68
  }
@@ -13,7 +13,7 @@ import {Project} from "../models/project";
13
13
  import type {ProjectDocument} from "../types";
14
14
 
15
15
  export const addProjectRoutes = (
16
- router: any,
16
+ router: express.Router,
17
17
  options?: {openApiOptions?: Record<string, unknown>}
18
18
  ): void => {
19
19
  // Add memory to a project (registered before modelRouter so it isn't shadowed)
@@ -916,6 +916,54 @@ describe("AI Routes", () => {
916
916
  await Project.deleteMany({});
917
917
  });
918
918
 
919
+ it("streams text-delta events through the SSE path with a minimal mock model", async () => {
920
+ // Covers the text-delta SSE branch by using a model that emits only
921
+ // text-delta + finish (no step-start/step-end events).
922
+ const noStepModel = {
923
+ doGenerate: mock(async () => ({
924
+ content: [{text: "ok", type: "text" as const}],
925
+ finishReason: "stop" as const,
926
+ usage: {inputTokens: 1, outputTokens: 1},
927
+ })),
928
+ doStream: mock(async () => ({
929
+ stream: new ReadableStream({
930
+ start(controller) {
931
+ controller.enqueue({delta: "buffered tail", id: "t1", type: "text-delta" as const});
932
+ controller.enqueue({
933
+ finishReason: "stop" as const,
934
+ type: "finish" as const,
935
+ usage: {inputTokens: 1, outputTokens: 1},
936
+ });
937
+ controller.close();
938
+ },
939
+ }),
940
+ })),
941
+ modelId: "no-step-model",
942
+ provider: "mock-provider",
943
+ specificationVersion: "v2" as const,
944
+ supportedUrls: {},
945
+ };
946
+ const customApp = setupServer({
947
+ addRoutes: (router, options) => {
948
+ addGptRoutes(router, {
949
+ aiService: new AIService({model: noStepModel as unknown as LanguageModel}),
950
+ openApiOptions: options,
951
+ });
952
+ },
953
+ skipListen: true,
954
+ userModel: UserModel,
955
+ });
956
+ const agent = await authAsUser(customApp, "notAdmin");
957
+ const res = await agent
958
+ .post("/gpt/prompt")
959
+ .send({prompt: "Hi"})
960
+ .buffer(true)
961
+ .parse(sseCollect);
962
+ expect(res.status).toBe(200);
963
+ const body = (res as SseResponse).body;
964
+ expect(body).toContain("buffered tail");
965
+ });
966
+
919
967
  it("uses per-request tools via createRequestTools", async () => {
920
968
  const createRequestTools = mock(() => ({
921
969
  localLookup: {description: "Per-request tool"} as unknown as Tool,
@@ -940,6 +988,61 @@ describe("AI Routes", () => {
940
988
  expect(res.status).toBe(200);
941
989
  expect(createRequestTools).toHaveBeenCalled();
942
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
+ });
943
1046
  });
944
1047
 
945
1048
  describe("GPT history rating route", () => {
@@ -1101,7 +1204,6 @@ describe("AI Routes", () => {
1101
1204
 
1102
1205
  it("handles AIRequest.logRequest failure gracefully", async () => {
1103
1206
  const originalLogRequest = AIRequest.logRequest;
1104
- // biome-ignore lint/suspicious/noExplicitAny: Override static method for test mock.
1105
1207
  AIRequest.logRequest = mock(async () => {
1106
1208
  throw new Error("database write failed");
1107
1209
  }) as unknown as typeof AIRequest.logRequest;
@@ -1417,6 +1519,77 @@ describe("AI Routes", () => {
1417
1519
  expect(res.body.total).toBe(1);
1418
1520
  });
1419
1521
 
1522
+ it("returns 400 for invalid startDate format", async () => {
1523
+ const agent = await authAsUser(app, "admin");
1524
+ const res = await agent.get("/aiRequestsExplorer?startDate=not-a-date");
1525
+
1526
+ expect(res.status).toBe(400);
1527
+ expect(res.body.title).toContain("Invalid startDate");
1528
+ });
1529
+
1530
+ it("returns 400 for invalid endDate format", async () => {
1531
+ const agent = await authAsUser(app, "admin");
1532
+ const res = await agent.get("/aiRequestsExplorer?endDate=not-a-date");
1533
+
1534
+ expect(res.status).toBe(400);
1535
+ expect(res.body.title).toContain("Invalid endDate");
1536
+ });
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
+
1420
1593
  it("filters by startDate and endDate range", async () => {
1421
1594
  const a = await AIRequest.create({
1422
1595
  aiModel: "gpt-4",
@@ -148,7 +148,7 @@ describe("AIService", () => {
148
148
 
149
149
  it("should translate text with explicit sourceLanguage", async () => {
150
150
  const model = createMockModel("Bonjour le monde");
151
- const service = new AIService({model: model as any});
151
+ const service = new AIService({model: model as unknown as LanguageModel});
152
152
 
153
153
  const result = await service.translateText({
154
154
  sourceLanguage: "English",
@@ -162,7 +162,7 @@ describe("AIService", () => {
162
162
  describe("modelId getter", () => {
163
163
  it("should return the model id from the underlying model", () => {
164
164
  const model = createMockModel();
165
- const service = new AIService({model: model as any});
165
+ const service = new AIService({model: model as unknown as LanguageModel});
166
166
 
167
167
  expect(service.modelId).toBe("mock-model");
168
168
  });
@@ -179,7 +179,7 @@ describe("AIService", () => {
179
179
  specificationVersion: "v2" as const,
180
180
  supportedUrls: {},
181
181
  };
182
- const service = new AIService({model: model as any});
182
+ const service = new AIService({model: model as unknown as LanguageModel});
183
183
 
184
184
  expect(service.modelId).toBe("unknown");
185
185
  });
@@ -191,7 +191,7 @@ describe("AIService", () => {
191
191
  model.doStream = mock(async () => {
192
192
  throw new Error("Stream API error");
193
193
  });
194
- const service = new AIService({model: model as any});
194
+ const service = new AIService({model: model as unknown as LanguageModel});
195
195
 
196
196
  const run = async () => {
197
197
  const chunks: string[] = [];
@@ -211,7 +211,7 @@ describe("AIService", () => {
211
211
  describe("buildMessages file content", () => {
212
212
  it("should convert multi-modal user prompts with file content", () => {
213
213
  const model = createMockModel();
214
- const service = new AIService({model: model as any});
214
+ const service = new AIService({model: model as unknown as LanguageModel});
215
215
 
216
216
  const messages = service.buildMessages([
217
217
  {
@@ -230,7 +230,9 @@ describe("AIService", () => {
230
230
  ]);
231
231
 
232
232
  expect(messages.length).toBe(1);
233
- const content = (messages[0] as any).content;
233
+ const content = (
234
+ messages[0] as {content: Array<{type: string; filename?: string; mediaType?: string}>}
235
+ ).content;
234
236
  expect(content.length).toBe(2);
235
237
  expect(content[1].type).toBe("file");
236
238
  expect(content[1].filename).toBe("report.pdf");
@@ -241,7 +243,7 @@ describe("AIService", () => {
241
243
  describe("generateChatStream", () => {
242
244
  it("should stream chat responses and log the request", async () => {
243
245
  const model = createMockModel();
244
- const service = new AIService({model: model as any});
246
+ const service = new AIService({model: model as unknown as LanguageModel});
245
247
  const userId = new mongoose.Types.ObjectId();
246
248
 
247
249
  const chunks: string[] = [];
@@ -266,7 +268,7 @@ describe("AIService", () => {
266
268
  model.doStream = mock(async () => {
267
269
  throw new Error("Chat stream failed");
268
270
  });
269
- const service = new AIService({model: model as any});
271
+ const service = new AIService({model: model as unknown as LanguageModel});
270
272
 
271
273
  const run = async () => {
272
274
  const chunks: string[] = [];
@@ -286,7 +288,7 @@ describe("AIService", () => {
286
288
 
287
289
  it("should accept systemPrompt override", async () => {
288
290
  const model = createMockModel();
289
- const service = new AIService({model: model as any});
291
+ const service = new AIService({model: model as unknown as LanguageModel});
290
292
 
291
293
  const chunks: string[] = [];
292
294
  for await (const chunk of service.generateChatStream({