@terreno/ai 57.8.0 → 57.9.0

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/.cursorrules CHANGED
@@ -49,7 +49,7 @@ import {AIService} from "@terreno/ai";
49
49
  import {google} from "@ai-sdk/google";
50
50
 
51
51
  const aiService = new AIService({
52
- model: google("gemini-2.5-flash"),
52
+ model: google("gemini-3.8-flash"),
53
53
  defaultTemperature: 1.0, // optional
54
54
  });
55
55
  ```
@@ -165,7 +165,7 @@ Consuming apps wire up routes in their `setupServer` call:
165
165
  import {AIService, addGptRoutes, addGptHistoryRoutes, addAiRequestsExplorerRoutes} from "@terreno/ai";
166
166
  import {google} from "@ai-sdk/google";
167
167
 
168
- const aiService = new AIService({model: google("gemini-2.5-flash")});
168
+ const aiService = new AIService({model: google("gemini-3.8-flash")});
169
169
 
170
170
  setupServer({
171
171
  addRoutes: (router, options) => {
package/README.md CHANGED
@@ -20,7 +20,7 @@ import {AiApp, AIService} from "@terreno/ai";
20
20
  import {google} from "@ai-sdk/google";
21
21
  import {User} from "./models/user";
22
22
 
23
- const aiService = new AIService({model: google("gemini-2.5-flash")});
23
+ const aiService = new AIService({model: google("gemini-3.8-flash")});
24
24
 
25
25
  new TerrenoApp({userModel: User})
26
26
  .register(new AiApp({aiService}))
package/dist/aiApp.d.ts CHANGED
@@ -17,6 +17,11 @@ export interface AiAppOptions {
17
17
  fileStorageService?: FileStorageService;
18
18
  /** GCS bucket name for file uploads. Required alongside fileStorageService. */
19
19
  gcsBucket?: string;
20
+ /**
21
+ * When `false` or the function returns `false`, file uploads and chat attachments are rejected.
22
+ * Omit or pass `true` to leave uploads enabled whenever storage is configured.
23
+ */
24
+ fileUploadsEnabled?: import("./service/fileUploadsGate").FileUploadsEnabled;
20
25
  /** Maximum number of tool-calling steps per chat request. Defaults to 5 when tools are present. */
21
26
  maxSteps?: number;
22
27
  /** MCP service for connecting to external tool servers. */
@@ -25,7 +30,7 @@ export interface AiAppOptions {
25
30
  openApiOptions?: Record<string, unknown>;
26
31
  /** Tool choice strategy for chat requests. Defaults to "auto" when tools are present. */
27
32
  toolChoice?: "auto" | "none" | "required";
28
- /** Cheap model ID used for generating conversation titles (e.g. "gemini-2.0-flash-lite"). Falls back to the main model if not set. */
33
+ /** Cheap model ID used for generating conversation titles (e.g. "gemini-3.5-flash-lite"). Falls back to the main model if not set. */
29
34
  titleModelId?: string;
30
35
  /** Tool definitions available to the AI model during chat. */
31
36
  tools?: Record<string, Tool>;
@@ -43,7 +48,7 @@ export interface AiAppOptions {
43
48
  * import {AiApp, AIService} from "@terreno/ai";
44
49
  * import {google} from "@ai-sdk/google";
45
50
  *
46
- * const aiService = new AIService({model: google("gemini-2.5-flash")});
51
+ * const aiService = new AIService({model: google("gemini-3.8-flash")});
47
52
  * new AiApp({aiService, tools: myTools}).register(app);
48
53
  * ```
49
54
  *
@@ -51,7 +56,7 @@ export interface AiAppOptions {
51
56
  * ```typescript
52
57
  * // Demo mode with per-request key support (no server-side API key needed)
53
58
  * new AiApp({
54
- * createModelFn: (key) => google("gemini-2.5-flash", {apiKey: key}),
59
+ * createModelFn: (key) => google("gemini-3.8-flash", {apiKey: key}),
55
60
  * demoMode: true,
56
61
  * }).register(app);
57
62
  * ```
package/dist/aiApp.js CHANGED
@@ -20,7 +20,7 @@ const projects_1 = require("./routes/projects");
20
20
  * import {AiApp, AIService} from "@terreno/ai";
21
21
  * import {google} from "@ai-sdk/google";
22
22
  *
23
- * const aiService = new AIService({model: google("gemini-2.5-flash")});
23
+ * const aiService = new AIService({model: google("gemini-3.8-flash")});
24
24
  * new AiApp({aiService, tools: myTools}).register(app);
25
25
  * ```
26
26
  *
@@ -28,7 +28,7 @@ const projects_1 = require("./routes/projects");
28
28
  * ```typescript
29
29
  * // Demo mode with per-request key support (no server-side API key needed)
30
30
  * new AiApp({
31
- * createModelFn: (key) => google("gemini-2.5-flash", {apiKey: key}),
31
+ * createModelFn: (key) => google("gemini-3.8-flash", {apiKey: key}),
32
32
  * demoMode: true,
33
33
  * }).register(app);
34
34
  * ```
@@ -40,13 +40,16 @@ class AiApp {
40
40
  }
41
41
  register(app) {
42
42
  const router = app;
43
- const { aiService, createModelFn, createServerModelFn, demoMode, fileStorageService, gcsBucket, maxSteps, mcpService, openApiOptions, titleModelId, toolChoice, tools, } = this.options;
43
+ const { aiService, createModelFn, createServerModelFn, demoMode, fileStorageService, fileUploadsEnabled, gcsBucket, maxSteps, mcpService, openApiOptions, titleModelId, toolChoice, tools, } = this.options;
44
44
  (0, gptHistories_1.addGptHistoryRoutes)(router, { openApiOptions });
45
45
  (0, gpt_1.addGptRoutes)(router, {
46
46
  aiService,
47
47
  createModelFn,
48
48
  createServerModelFn,
49
49
  demoMode,
50
+ // Attachments are only uploaded when the file routes are mounted too
51
+ fileStorageService: fileStorageService && gcsBucket ? fileStorageService : undefined,
52
+ fileUploadsEnabled,
50
53
  maxSteps,
51
54
  mcpService,
52
55
  openApiOptions,
@@ -59,6 +62,7 @@ class AiApp {
59
62
  if (fileStorageService && gcsBucket) {
60
63
  (0, files_1.addFileRoutes)(router, {
61
64
  fileStorageService,
65
+ fileUploadsEnabled,
62
66
  gcsBucket,
63
67
  openApiOptions,
64
68
  });
@@ -8,6 +8,10 @@ const api_1 = require("@terreno/api");
8
8
  const mongoose_1 = __importDefault(require("mongoose"));
9
9
  const contentPartSchema = new mongoose_1.default.Schema({
10
10
  filename: { description: "Original filename of the attached file", type: String },
11
+ gcsKey: {
12
+ description: "Durable storage key for an attachment uploaded through FileStorageService",
13
+ type: String,
14
+ },
11
15
  mimeType: { description: "MIME type of the content part", type: String },
12
16
  text: { description: "Text content of this part", type: String },
13
17
  type: {
@@ -28,12 +32,21 @@ const gptHistoryPromptSchema = new mongoose_1.default.Schema({
28
32
  type: String,
29
33
  },
30
34
  result: { description: "Result returned from a tool call", type: mongoose_1.default.Schema.Types.Mixed },
35
+ status: {
36
+ description: "Lifecycle of an assistant reply: streaming while partial text is persisted, then complete or error",
37
+ enum: ["streaming", "complete", "error"],
38
+ type: String,
39
+ },
40
+ streamId: {
41
+ description: "Identifier of the /gpt/prompt reply, used to resume an in-flight stream",
42
+ type: String,
43
+ },
31
44
  text: {
32
45
  default: "",
33
46
  description: "Text content of the prompt or response",
34
- // Image-only responses carry their payload in content, not text
47
+ // Image-only responses carry their payload in content, and streaming replies start empty
35
48
  required: function () {
36
- return !this.content?.length;
49
+ return !this.content?.length && !this.status;
37
50
  },
38
51
  type: String,
39
52
  },
@@ -7,6 +7,7 @@ exports.addFileRoutes = void 0;
7
7
  const api_1 = require("@terreno/api");
8
8
  const multer_1 = __importDefault(require("multer"));
9
9
  const fileAttachment_1 = require("../models/fileAttachment");
10
+ const fileUploadsGate_1 = require("../service/fileUploadsGate");
10
11
  const ALLOWED_MIME_TYPES = new Set([
11
12
  "image/jpeg",
12
13
  "image/png",
@@ -47,6 +48,7 @@ const addFileRoutes = (router, options) => {
47
48
  })
48
49
  .build(),
49
50
  ], (0, api_1.asyncHandler)(async (req, res) => {
51
+ await (0, fileUploadsGate_1.assertFileUploadsEnabled)(req, options.fileUploadsEnabled);
50
52
  const file = req.file;
51
53
  const userId = req.user
52
54
  ?._id;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.addGptRoutes = void 0;
4
+ const node_crypto_1 = require("node:crypto");
4
5
  const api_1 = require("@terreno/api");
5
6
  const ai_1 = require("ai");
6
7
  const luxon_1 = require("luxon");
@@ -9,7 +10,134 @@ const aiRequest_1 = require("../models/aiRequest");
9
10
  const gptHistory_1 = require("../models/gptHistory");
10
11
  const project_1 = require("../models/project");
11
12
  const aiService_1 = require("../service/aiService");
13
+ const fileUploadsGate_1 = require("../service/fileUploadsGate");
12
14
  const prompts_1 = require("../service/prompts");
15
+ const DEFAULT_STREAM_PERSIST_INTERVAL_MS = 1000;
16
+ const DEFAULT_STREAM_RESUME_POLL_INTERVAL_MS = 500;
17
+ const DEFAULT_STREAM_STALE_AFTER_MS = 60_000;
18
+ // Bump `updated` even without new text so long tool calls do not look stale to resumers
19
+ const STREAM_HEARTBEAT_MS = 10_000;
20
+ const DURABLE_URL_PATTERN = /^(https?:|data:)/i;
21
+ const DATA_URL_PATTERN = /^data:([^;,]*)((?:;[^;,]*)*?)(;base64)?,(.*)$/s;
22
+ const sseEvent = (payload) => `data: ${JSON.stringify(payload)}\n\n`;
23
+ const sleep = (ms) => new Promise((resolve) => {
24
+ setTimeout(resolve, ms);
25
+ });
26
+ /**
27
+ * Reject attachment URLs that only exist on the client (blob:, file:, content:, ph:, ...).
28
+ * They cannot be read by the model provider or reloaded from history later.
29
+ */
30
+ const validateAttachments = (attachments) => {
31
+ if (attachments === undefined || attachments === null) {
32
+ return [];
33
+ }
34
+ if (!Array.isArray(attachments)) {
35
+ throw new api_1.APIError({ status: 400, title: "attachments must be an array" });
36
+ }
37
+ for (const attachment of attachments) {
38
+ if (attachment?.type !== "image" && attachment?.type !== "file") {
39
+ continue;
40
+ }
41
+ if (typeof attachment.url !== "string" || !DURABLE_URL_PATTERN.test(attachment.url)) {
42
+ const scheme = typeof attachment.url === "string" ? (attachment.url.split(":")[0] ?? "") : "missing";
43
+ throw new api_1.APIError({
44
+ detail: `Attachment "${attachment.filename ?? attachment.type}" uses a "${scheme}" URL. ` +
45
+ "Upload the file first (POST /files/upload) or send it as a data: URL.",
46
+ status: 400,
47
+ title: "Attachment URL must be an http(s) or data: URL",
48
+ });
49
+ }
50
+ }
51
+ return attachments;
52
+ };
53
+ const decodeDataUrl = (url) => {
54
+ const match = DATA_URL_PATTERN.exec(url);
55
+ if (!match) {
56
+ return undefined;
57
+ }
58
+ const [, mimeType, , base64Flag, payload] = match;
59
+ const buffer = base64Flag
60
+ ? Buffer.from(payload, "base64")
61
+ : Buffer.from(decodeURIComponent(payload), "utf8");
62
+ return { buffer, mimeType: mimeType || undefined };
63
+ };
64
+ /** Build model and history content parts, uploading data: attachments when storage is configured. */
65
+ const buildAttachmentParts = async ({ attachments, fileStorageService, userId, }) => {
66
+ const modelParts = [];
67
+ const storedParts = [];
68
+ for (const attachment of attachments) {
69
+ if (attachment.type !== "image" && attachment.type !== "file") {
70
+ continue;
71
+ }
72
+ const url = attachment.url;
73
+ const modelPart = attachment.type === "image"
74
+ ? { mimeType: attachment.mimeType, type: "image", url }
75
+ : {
76
+ filename: attachment.filename,
77
+ mimeType: attachment.mimeType,
78
+ type: "file",
79
+ url,
80
+ };
81
+ modelParts.push(modelPart);
82
+ const decoded = url.startsWith("data:") ? decodeDataUrl(url) : undefined;
83
+ if (!fileStorageService || !userId || !decoded) {
84
+ storedParts.push(modelPart);
85
+ continue;
86
+ }
87
+ const mimeType = attachment.mimeType ?? decoded.mimeType ?? "application/octet-stream";
88
+ const extension = mimeType.split("/")[1]?.split("+")[0] ?? "bin";
89
+ try {
90
+ const uploaded = await fileStorageService.upload({
91
+ buffer: decoded.buffer,
92
+ filename: attachment.filename ?? `attachment-${luxon_1.DateTime.now().toMillis()}.${extension}`,
93
+ mimeType,
94
+ userId,
95
+ });
96
+ storedParts.push({ ...modelPart, gcsKey: uploaded.gcsKey, url: uploaded.url });
97
+ }
98
+ catch (error) {
99
+ throw new api_1.APIError({
100
+ cause: error,
101
+ detail: error instanceof Error ? error.message : String(error),
102
+ status: 502,
103
+ title: "Attachment upload failed",
104
+ });
105
+ }
106
+ }
107
+ return { modelParts, storedParts };
108
+ };
109
+ const toPlainPrompt = (prompt) => {
110
+ const maybeSubdoc = prompt;
111
+ return typeof maybeSubdoc.toObject === "function" ? maybeSubdoc.toObject() : prompt;
112
+ };
113
+ /** Swap stored attachment references for short-lived signed URLs the model provider can fetch. */
114
+ const resolveStoredAttachmentUrls = async (prompts, fileStorageService) => {
115
+ if (!fileStorageService) {
116
+ return prompts;
117
+ }
118
+ return Promise.all(prompts.map(async (prompt) => {
119
+ if (!prompt.content?.some((part) => part.type !== "text" && part.gcsKey)) {
120
+ return prompt;
121
+ }
122
+ const plain = toPlainPrompt(prompt);
123
+ const content = await Promise.all((plain.content ?? []).map(async (part) => {
124
+ if (part.type === "text" || !part.gcsKey) {
125
+ return part;
126
+ }
127
+ try {
128
+ return { ...part, url: await fileStorageService.getSignedUrl(part.gcsKey) };
129
+ }
130
+ catch (error) {
131
+ api_1.logger.warn("Could not sign stored attachment URL", {
132
+ error: error instanceof Error ? error.message : String(error),
133
+ gcsKey: part.gcsKey,
134
+ });
135
+ return part;
136
+ }
137
+ }));
138
+ return { ...plain, content };
139
+ }));
140
+ };
13
141
  const isGeneratedImageFile = (value) => typeof value === "object" &&
14
142
  value !== null &&
15
143
  "base64" in value &&
@@ -77,7 +205,10 @@ const generateTitle = async (prompt, response, aiService, options, perRequestApi
77
205
  }
78
206
  };
79
207
  const addGptRoutes = (router, options) => {
80
- const { mcpService, tools: routeTools, createRequestTools, toolChoice, maxSteps } = options;
208
+ const { mcpService, tools: routeTools, createRequestTools, toolChoice, maxSteps, fileStorageService, } = options;
209
+ const persistIntervalMs = options.streamPersistIntervalMs ?? DEFAULT_STREAM_PERSIST_INTERVAL_MS;
210
+ const resumePollIntervalMs = options.streamResumePollIntervalMs ?? DEFAULT_STREAM_RESUME_POLL_INTERVAL_MS;
211
+ const staleAfterMs = options.streamStaleAfterMs ?? DEFAULT_STREAM_STALE_AFTER_MS;
81
212
  router.post("/gpt/prompt", [
82
213
  (0, api_1.authenticateMiddleware)(),
83
214
  (0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
@@ -115,6 +246,10 @@ const addGptRoutes = (router, options) => {
115
246
  if (!prompt || typeof prompt !== "string") {
116
247
  throw new api_1.APIError({ status: 400, title: "prompt is required" });
117
248
  }
249
+ if (Array.isArray(attachments) && attachments.length > 0) {
250
+ await (0, fileUploadsGate_1.assertFileUploadsEnabled)(req, options.fileUploadsEnabled);
251
+ }
252
+ const validAttachments = validateAttachments(attachments);
118
253
  // Resolve AI service (per-request key takes priority, then configured service)
119
254
  const hasPerRequestKey = !!req.headers["x-ai-api-key"];
120
255
  const hasCreateModelFn = !!options.createModelFn;
@@ -192,34 +327,20 @@ const addGptRoutes = (router, options) => {
192
327
  }
193
328
  }
194
329
  // Build content parts from attachments
195
- const contentParts = [{ text: prompt, type: "text" }];
196
- if (attachments && Array.isArray(attachments)) {
197
- api_1.logger.debug("Processing attachments", {
198
- count: attachments.length,
199
- types: attachments.map((a) => ({
200
- mimeType: a.mimeType,
201
- type: a.type,
202
- urlLength: a.url?.length ?? 0,
203
- })),
204
- });
205
- for (const attachment of attachments) {
206
- if (attachment.type === "image") {
207
- contentParts.push({
208
- mimeType: attachment.mimeType,
209
- type: "image",
210
- url: attachment.url,
211
- });
212
- }
213
- else if (attachment.type === "file") {
214
- contentParts.push({
215
- filename: attachment.filename,
216
- mimeType: attachment.mimeType,
217
- type: "file",
218
- url: attachment.url,
219
- });
220
- }
221
- }
222
- }
330
+ api_1.logger.debug("Processing attachments", {
331
+ count: validAttachments.length,
332
+ types: validAttachments.map((a) => ({
333
+ mimeType: a.mimeType,
334
+ type: a.type,
335
+ urlLength: a.url?.length ?? 0,
336
+ })),
337
+ });
338
+ const { modelParts, storedParts } = await buildAttachmentParts({
339
+ attachments: validAttachments,
340
+ fileStorageService,
341
+ userId,
342
+ });
343
+ const contentParts = [{ text: prompt, type: "text" }, ...storedParts];
223
344
  // Add user prompt to history
224
345
  const hasAttachments = contentParts.length > 1;
225
346
  const userPrompt = {
@@ -233,9 +354,14 @@ const addGptRoutes = (router, options) => {
233
354
  attachmentCount: contentParts.length - 1,
234
355
  historyLength: history.prompts.length,
235
356
  });
236
- const messages = aiService.buildMessages(history.prompts);
357
+ // Earlier turns reference stored attachments; this turn sends the original URLs
358
+ const earlierPrompts = await resolveStoredAttachmentUrls(history.prompts.slice(0, -1), fileStorageService);
359
+ const modelUserPrompt = hasAttachments
360
+ ? { ...userPrompt, content: [{ text: prompt, type: "text" }, ...modelParts] }
361
+ : userPrompt;
362
+ const messages = aiService.buildMessages([...earlierPrompts, modelUserPrompt]);
237
363
  api_1.logger.debug("Messages built", { messageCount: messages.length });
238
- // Some models (e.g. gemini-2.5-flash-image) don't support tool calling
364
+ // Some models (e.g. gemini-3-pro-image) don't support tool calling
239
365
  const modelId = aiService.modelId;
240
366
  const supportsTools = !modelId?.includes("image");
241
367
  // Merge tools from route config, per-request tools, and MCP service
@@ -253,12 +379,65 @@ const addGptRoutes = (router, options) => {
253
379
  }
254
380
  }
255
381
  }
382
+ // Persist the user turn and a streaming placeholder so a reload can resume the reply
383
+ const streamId = (0, node_crypto_1.randomUUID)();
384
+ history.prompts.push({
385
+ model: aiService.modelId,
386
+ status: "streaming",
387
+ streamId,
388
+ text: "",
389
+ type: "assistant",
390
+ });
391
+ await history.save();
392
+ const savedHistory = history;
393
+ const placeholderIndex = history.prompts.length - 1;
256
394
  // Stream response via SSE
257
395
  res.setHeader("Content-Type", "text/event-stream");
258
396
  res.setHeader("Cache-Control", "no-cache");
259
397
  res.setHeader("Connection", "keep-alive");
260
398
  sseStarted = true;
399
+ res.write(sseEvent({ historyId: history._id.toString(), started: true, streamId }));
261
400
  let fullResponse = "";
401
+ // Buffer text per step so we can discard reasoning text when a tool call follows
402
+ let stepTextBuffer = "";
403
+ let stepHasToolCall = false;
404
+ const currentPartialText = () => fullResponse + (stepHasToolCall ? "" : stepTextBuffer);
405
+ let lastPersistedText = "";
406
+ let lastPersistedAt = luxon_1.DateTime.now();
407
+ let persistQueue = Promise.resolve();
408
+ const persistTimer = setInterval(() => {
409
+ const text = currentPartialText();
410
+ const isHeartbeatDue = luxon_1.DateTime.now().diff(lastPersistedAt).toMillis() >= STREAM_HEARTBEAT_MS;
411
+ if (text === lastPersistedText && !isHeartbeatDue) {
412
+ return;
413
+ }
414
+ lastPersistedText = text;
415
+ lastPersistedAt = luxon_1.DateTime.now();
416
+ persistQueue = persistQueue
417
+ .then(async () => {
418
+ await gptHistory_1.GptHistory.updateOne({ _id: savedHistory._id, [`prompts.${placeholderIndex}.streamId`]: streamId }, {
419
+ $set: {
420
+ [`prompts.${placeholderIndex}.text`]: text,
421
+ updated: luxon_1.DateTime.now().toJSDate(),
422
+ },
423
+ });
424
+ })
425
+ .catch((persistErr) => {
426
+ api_1.logger.warn("Failed to persist partial GPT response", {
427
+ error: persistErr instanceof Error ? persistErr.message : String(persistErr),
428
+ });
429
+ });
430
+ }, persistIntervalMs);
431
+ const stopPersisting = async () => {
432
+ clearInterval(persistTimer);
433
+ await persistQueue;
434
+ };
435
+ const removePlaceholder = () => {
436
+ const index = savedHistory.prompts.findIndex((p) => p.streamId === streamId);
437
+ if (index >= 0) {
438
+ savedHistory.prompts.splice(index, 1);
439
+ }
440
+ };
262
441
  const generatedImages = [];
263
442
  // Stream file parts and result.files report the same images; emit each data URL once.
264
443
  const sendGeneratedImage = (file) => {
@@ -298,9 +477,6 @@ const addGptRoutes = (router, options) => {
298
477
  tools: allTools,
299
478
  });
300
479
  let partCount = 0;
301
- // Buffer text per step so we can discard reasoning text when a tool call follows
302
- let stepTextBuffer = "";
303
- let stepHasToolCall = false;
304
480
  for await (const part of result.fullStream) {
305
481
  partCount++;
306
482
  if (partCount <= 5 || part.type === "error" || part.type === "file") {
@@ -435,7 +611,9 @@ const addGptRoutes = (router, options) => {
435
611
  error: fileErr instanceof Error ? fileErr.message : String(fileErr),
436
612
  });
437
613
  }
438
- // Save assistant response to history
614
+ // Replace the streaming placeholder with the final reply, after any tool turns
615
+ await stopPersisting();
616
+ removePlaceholder();
439
617
  if (fullResponse || generatedImages.length > 0) {
440
618
  const contentParts = generatedImages.map((img) => ({
441
619
  mimeType: img.mimeType,
@@ -444,12 +622,15 @@ const addGptRoutes = (router, options) => {
444
622
  }));
445
623
  const assistantPrompt = {
446
624
  model: aiService.modelId,
625
+ status: "complete",
626
+ streamId,
447
627
  text: fullResponse,
448
628
  type: "assistant",
449
629
  ...(contentParts.length > 0 ? { content: contentParts } : {}),
450
630
  };
451
631
  history.prompts.push(assistantPrompt);
452
632
  }
633
+ history.markModified("prompts");
453
634
  await history.save();
454
635
  try {
455
636
  await aiRequest_1.AIRequest.logRequest({
@@ -490,6 +671,28 @@ const addGptRoutes = (router, options) => {
490
671
  api_1.logger.error("Error in GPT stream", {
491
672
  error: error instanceof Error ? error.message : String(error),
492
673
  });
674
+ // Keep any partial reply so the conversation shows what streamed before the failure
675
+ try {
676
+ await stopPersisting();
677
+ const partialText = currentPartialText();
678
+ const placeholder = savedHistory.prompts.find((p) => p.streamId === streamId);
679
+ if (placeholder?.status === "streaming") {
680
+ if (partialText) {
681
+ placeholder.text = partialText;
682
+ placeholder.status = "error";
683
+ }
684
+ else {
685
+ removePlaceholder();
686
+ }
687
+ savedHistory.markModified("prompts");
688
+ await savedHistory.save();
689
+ }
690
+ }
691
+ catch (saveErr) {
692
+ api_1.logger.warn("Failed to save interrupted GPT response", {
693
+ error: saveErr instanceof Error ? saveErr.message : String(saveErr),
694
+ });
695
+ }
493
696
  try {
494
697
  await aiRequest_1.AIRequest.logRequest({
495
698
  aiModel: modelId ?? "unknown",
@@ -530,6 +733,104 @@ const addGptRoutes = (router, options) => {
530
733
  }
531
734
  }
532
735
  });
736
+ // Re-attach to a reply that /gpt/prompt is still streaming (e.g. after a reload or remount).
737
+ // Polls the persisted partial output, so it works across server instances.
738
+ router.get("/gpt/histories/:id/stream", [
739
+ (0, api_1.authenticateMiddleware)(),
740
+ (0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
741
+ .withTags(["gpt"])
742
+ .withSummary("Resume an in-flight GPT reply as SSE")
743
+ .withPathParameter("id", { type: "string" })
744
+ .withQueryParameter("streamId", { type: "string" }, { description: "Reply to follow. Defaults to the latest streaming reply." })
745
+ .withQueryParameter("offset", { type: "number" }, { description: "Characters of the reply the client already shows. Defaults to 0." })
746
+ .withResponse(200, { data: { type: "string" } })
747
+ .build(),
748
+ ], (0, api_1.asyncHandler)(async (req, res) => {
749
+ const { id } = req.params;
750
+ const userId = req.user?._id;
751
+ const requestedStreamId = typeof req.query.streamId === "string" ? req.query.streamId : undefined;
752
+ const parsedOffset = Number(req.query.offset ?? 0);
753
+ const offset = Number.isFinite(parsedOffset) && parsedOffset > 0 ? parsedOffset : 0;
754
+ const history = await gptHistory_1.GptHistory.findById(id);
755
+ if (!history) {
756
+ throw new api_1.APIError({ status: 404, title: "History not found" });
757
+ }
758
+ if (history.userId.toString() !== userId?.toString()) {
759
+ throw new api_1.APIError({ status: 403, title: "Not authorized to access this history" });
760
+ }
761
+ const reply = requestedStreamId
762
+ ? history.prompts.find((p) => p.streamId === requestedStreamId)
763
+ : history.prompts.findLast((p) => p.status === "streaming");
764
+ const streamId = reply?.streamId;
765
+ res.setHeader("Content-Type", "text/event-stream");
766
+ res.setHeader("Cache-Control", "no-cache");
767
+ res.setHeader("Connection", "keep-alive");
768
+ res.write(sseEvent({ historyId: id, resumed: true, ...(streamId ? { streamId } : {}) }));
769
+ let isClosed = false;
770
+ req.on("close", () => {
771
+ isClosed = true;
772
+ });
773
+ let isInitialPoll = true;
774
+ let sentText;
775
+ let sentLength = offset;
776
+ try {
777
+ while (!isClosed) {
778
+ const current = streamId ? await gptHistory_1.GptHistory.findById(id) : history;
779
+ if (!current) {
780
+ res.write(sseEvent({ error: "History not found" }));
781
+ break;
782
+ }
783
+ const currentReply = streamId
784
+ ? current.prompts.find((p) => p.streamId === streamId)
785
+ : undefined;
786
+ const text = currentReply?.text ?? "";
787
+ if (isInitialPoll && offset > 0) {
788
+ // The client can have loaded a stale placeholder before reconnecting.
789
+ res.write(sseEvent({ replace: true, text }));
790
+ }
791
+ else if (sentText !== undefined && !text.startsWith(sentText)) {
792
+ // Text from a step that turned into a tool call was discarded; resend the reply
793
+ res.write(sseEvent({ replace: true, text }));
794
+ }
795
+ else if (text.length > sentLength) {
796
+ res.write(sseEvent({ text: text.slice(sentLength) }));
797
+ }
798
+ isInitialPoll = false;
799
+ sentText = text;
800
+ sentLength = text.length;
801
+ if (currentReply?.status === "streaming") {
802
+ const sinceUpdateMs = luxon_1.DateTime.now()
803
+ .diff(luxon_1.DateTime.fromJSDate(current.updated))
804
+ .toMillis();
805
+ if (sinceUpdateMs > staleAfterMs) {
806
+ await gptHistory_1.GptHistory.updateOne({ _id: current._id, prompts: { $elemMatch: { status: "streaming", streamId } } }, { $set: { "prompts.$.status": "error" } });
807
+ res.write(sseEvent({ error: "The reply was interrupted before it finished" }));
808
+ res.write(sseEvent({ done: true, historyId: id }));
809
+ break;
810
+ }
811
+ await sleep(resumePollIntervalMs);
812
+ continue;
813
+ }
814
+ for (const part of currentReply?.content ?? []) {
815
+ if (part.type === "image") {
816
+ res.write(sseEvent({ image: { mimeType: part.mimeType, url: part.url } }));
817
+ }
818
+ }
819
+ if (currentReply?.status === "error") {
820
+ res.write(sseEvent({ error: "The reply was interrupted before it finished" }));
821
+ }
822
+ res.write(sseEvent({ done: true, historyId: id, ...(current.title ? { title: current.title } : {}) }));
823
+ break;
824
+ }
825
+ }
826
+ catch (error) {
827
+ api_1.logger.error("Error resuming GPT stream", {
828
+ error: error instanceof Error ? error.message : String(error),
829
+ });
830
+ res.write(sseEvent({ error: error instanceof Error ? error.message : "Unknown error" }));
831
+ }
832
+ res.end();
833
+ }));
533
834
  router.patch("/gpt/histories/:id/rating", [
534
835
  (0, api_1.authenticateMiddleware)(),
535
836
  (0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
@@ -393,6 +393,11 @@ class AIService {
393
393
  if (prompt.type === "tool-call" || prompt.type === "tool-result") {
394
394
  continue;
395
395
  }
396
+ // A reply still streaming (or one that failed before any output) is not a real turn yet
397
+ if (prompt.status === "streaming" ||
398
+ (prompt.status === "error" && !prompt.text && !prompt.content?.length)) {
399
+ continue;
400
+ }
396
401
  const role = prompt.type;
397
402
  if (prompt.content && prompt.content.length > 0 && role === "user") {
398
403
  const parts = [];
@@ -0,0 +1,8 @@
1
+ import type express from "express";
2
+ /** Static switch, or a per-request check (for example a feature flag). */
3
+ export type FileUploadsEnabled = boolean | ((req: express.Request) => boolean | Promise<boolean>);
4
+ /**
5
+ * Reject the request when file uploads are turned off.
6
+ * Missing or `true` leaves uploads enabled.
7
+ */
8
+ export declare const assertFileUploadsEnabled: (req: express.Request, fileUploadsEnabled: FileUploadsEnabled | undefined) => Promise<void>;