@terreno/ai 57.7.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 +2 -2
- package/README.md +1 -1
- package/dist/aiApp.d.ts +8 -3
- package/dist/aiApp.js +7 -3
- package/dist/models/gptHistory.js +22 -1
- package/dist/routes/files.js +2 -0
- package/dist/routes/gpt.js +351 -49
- package/dist/service/aiService.js +9 -0
- package/dist/service/fileUploadsGate.d.ts +8 -0
- package/dist/service/fileUploadsGate.js +18 -0
- package/dist/service/gemini.d.ts +3 -3
- package/dist/service/gemini.js +3 -3
- package/dist/service/vertex.d.ts +6 -6
- package/dist/service/vertex.js +5 -5
- package/dist/types/index.d.ts +29 -1
- package/package.json +4 -4
- package/src/aiApp.ts +13 -3
- package/src/models/aiRequest.test.ts +2 -2
- package/src/models/gptHistory.test.ts +26 -0
- package/src/models/gptHistory.ts +23 -1
- package/src/routes/files.test.ts +21 -0
- package/src/routes/files.ts +2 -0
- package/src/routes/gpt.ts +436 -52
- package/src/routes/routes.test.ts +512 -3
- package/src/service/aiService.test.ts +39 -0
- package/src/service/aiService.ts +10 -0
- package/src/service/fileUploadsGate.ts +23 -0
- package/src/service/gemini.test.ts +16 -10
- package/src/service/gemini.ts +3 -3
- package/src/service/vertex.test.ts +49 -49
- package/src/service/vertex.ts +7 -8
- package/src/types/index.ts +30 -1
package/dist/routes/gpt.js
CHANGED
|
@@ -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
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
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
|
-
|
|
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-
|
|
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,13 +379,75 @@ 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 = [];
|
|
442
|
+
// Stream file parts and result.files report the same images; emit each data URL once.
|
|
443
|
+
const sendGeneratedImage = (file) => {
|
|
444
|
+
const url = `data:${file.mediaType};base64,${file.base64}`;
|
|
445
|
+
if (generatedImages.some((img) => img.url === url)) {
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
generatedImages.push({ mimeType: file.mediaType, url });
|
|
449
|
+
res.write(`data: ${JSON.stringify({ image: { mimeType: file.mediaType, url } })}\n\n`);
|
|
450
|
+
};
|
|
263
451
|
const startTime = luxon_1.DateTime.now().toMillis();
|
|
264
452
|
try {
|
|
265
453
|
api_1.logger.debug("Starting streamText", { model: modelId, supportsTools });
|
|
@@ -289,9 +477,6 @@ const addGptRoutes = (router, options) => {
|
|
|
289
477
|
tools: allTools,
|
|
290
478
|
});
|
|
291
479
|
let partCount = 0;
|
|
292
|
-
// Buffer text per step so we can discard reasoning text when a tool call follows
|
|
293
|
-
let stepTextBuffer = "";
|
|
294
|
-
let stepHasToolCall = false;
|
|
295
480
|
for await (const part of result.fullStream) {
|
|
296
481
|
partCount++;
|
|
297
482
|
if (partCount <= 5 || part.type === "error" || part.type === "file") {
|
|
@@ -336,11 +521,7 @@ const addGptRoutes = (router, options) => {
|
|
|
336
521
|
}
|
|
337
522
|
if (part.type === "file") {
|
|
338
523
|
if (isGeneratedImageFile(part.file)) {
|
|
339
|
-
|
|
340
|
-
generatedImages.push({ mimeType: part.file.mediaType, url: dataUrl });
|
|
341
|
-
res.write(`data: ${JSON.stringify({
|
|
342
|
-
image: { mimeType: part.file.mediaType, url: dataUrl },
|
|
343
|
-
})}\n\n`);
|
|
524
|
+
sendGeneratedImage(part.file);
|
|
344
525
|
api_1.logger.debug("Sent inline image from stream");
|
|
345
526
|
}
|
|
346
527
|
continue;
|
|
@@ -413,20 +594,16 @@ const addGptRoutes = (router, options) => {
|
|
|
413
594
|
}
|
|
414
595
|
}
|
|
415
596
|
api_1.logger.debug("Stream completed", { fullResponseLength: fullResponse.length, partCount });
|
|
416
|
-
//
|
|
597
|
+
// Catch generated images the stream did not report as file parts
|
|
417
598
|
try {
|
|
418
599
|
const files = await result.files;
|
|
419
600
|
if (files && files.length > 0) {
|
|
420
601
|
for (const file of files) {
|
|
421
|
-
if (file
|
|
422
|
-
|
|
423
|
-
generatedImages.push({ mimeType: file.mediaType, url: dataUrl });
|
|
424
|
-
res.write(`data: ${JSON.stringify({
|
|
425
|
-
image: { mimeType: file.mediaType, url: dataUrl },
|
|
426
|
-
})}\n\n`);
|
|
602
|
+
if (isGeneratedImageFile(file)) {
|
|
603
|
+
sendGeneratedImage(file);
|
|
427
604
|
}
|
|
428
605
|
}
|
|
429
|
-
api_1.logger.debug("Sent generated images", { count:
|
|
606
|
+
api_1.logger.debug("Sent generated images", { count: generatedImages.length });
|
|
430
607
|
}
|
|
431
608
|
}
|
|
432
609
|
catch (fileErr) {
|
|
@@ -434,7 +611,9 @@ const addGptRoutes = (router, options) => {
|
|
|
434
611
|
error: fileErr instanceof Error ? fileErr.message : String(fileErr),
|
|
435
612
|
});
|
|
436
613
|
}
|
|
437
|
-
//
|
|
614
|
+
// Replace the streaming placeholder with the final reply, after any tool turns
|
|
615
|
+
await stopPersisting();
|
|
616
|
+
removePlaceholder();
|
|
438
617
|
if (fullResponse || generatedImages.length > 0) {
|
|
439
618
|
const contentParts = generatedImages.map((img) => ({
|
|
440
619
|
mimeType: img.mimeType,
|
|
@@ -443,12 +622,15 @@ const addGptRoutes = (router, options) => {
|
|
|
443
622
|
}));
|
|
444
623
|
const assistantPrompt = {
|
|
445
624
|
model: aiService.modelId,
|
|
446
|
-
|
|
625
|
+
status: "complete",
|
|
626
|
+
streamId,
|
|
627
|
+
text: fullResponse,
|
|
447
628
|
type: "assistant",
|
|
448
629
|
...(contentParts.length > 0 ? { content: contentParts } : {}),
|
|
449
630
|
};
|
|
450
631
|
history.prompts.push(assistantPrompt);
|
|
451
632
|
}
|
|
633
|
+
history.markModified("prompts");
|
|
452
634
|
await history.save();
|
|
453
635
|
try {
|
|
454
636
|
await aiRequest_1.AIRequest.logRequest({
|
|
@@ -489,6 +671,28 @@ const addGptRoutes = (router, options) => {
|
|
|
489
671
|
api_1.logger.error("Error in GPT stream", {
|
|
490
672
|
error: error instanceof Error ? error.message : String(error),
|
|
491
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
|
+
}
|
|
492
696
|
try {
|
|
493
697
|
await aiRequest_1.AIRequest.logRequest({
|
|
494
698
|
aiModel: modelId ?? "unknown",
|
|
@@ -529,6 +733,104 @@ const addGptRoutes = (router, options) => {
|
|
|
529
733
|
}
|
|
530
734
|
}
|
|
531
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
|
+
}));
|
|
532
834
|
router.patch("/gpt/histories/:id/rating", [
|
|
533
835
|
(0, api_1.authenticateMiddleware)(),
|
|
534
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 = [];
|
|
@@ -423,6 +428,10 @@ class AIService {
|
|
|
423
428
|
}
|
|
424
429
|
messages.push({ content: parts, role: "user" });
|
|
425
430
|
}
|
|
431
|
+
else if (!prompt.text && prompt.content?.length) {
|
|
432
|
+
// Providers reject empty assistant turns; stand in for an image-only response
|
|
433
|
+
messages.push({ content: "[Generated image]", role });
|
|
434
|
+
}
|
|
426
435
|
else {
|
|
427
436
|
messages.push({ content: prompt.text, role });
|
|
428
437
|
}
|
|
@@ -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>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertFileUploadsEnabled = void 0;
|
|
4
|
+
const api_1 = require("@terreno/api");
|
|
5
|
+
/**
|
|
6
|
+
* Reject the request when file uploads are turned off.
|
|
7
|
+
* Missing or `true` leaves uploads enabled.
|
|
8
|
+
*/
|
|
9
|
+
const assertFileUploadsEnabled = async (req, fileUploadsEnabled) => {
|
|
10
|
+
if (fileUploadsEnabled === undefined || fileUploadsEnabled === true) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const allowed = typeof fileUploadsEnabled === "function" ? await fileUploadsEnabled(req) : fileUploadsEnabled;
|
|
14
|
+
if (!allowed) {
|
|
15
|
+
throw new api_1.APIError({ status: 403, title: "File uploads are disabled" });
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
exports.assertFileUploadsEnabled = assertFileUploadsEnabled;
|
package/dist/service/gemini.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Helper for listing models available to the Gemini Developer API (the API-key based
|
|
3
3
|
* `generativelanguage.googleapis.com` service, distinct from Vertex / the Gemini Enterprise Agent
|
|
4
4
|
* Platform). Used to drive model pickers from the live set of models Google actually exposes for a
|
|
5
|
-
* given API key, so retired models (e.g. an old `gemini-2.
|
|
5
|
+
* given API key, so retired models (e.g. an old `gemini-2.5-flash`) never appear.
|
|
6
6
|
*/
|
|
7
7
|
/** Default Gemini Developer API base URL. */
|
|
8
8
|
export declare const GEMINI_API_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
@@ -19,11 +19,11 @@ export interface ListGeminiApiModelsOptions {
|
|
|
19
19
|
/** Injectable fetch implementation (defaults to the global `fetch`). */
|
|
20
20
|
fetchImpl?: typeof fetch;
|
|
21
21
|
}
|
|
22
|
-
/** Strip the "models/" resource prefix from a Gemini model name (e.g. "models/gemini-
|
|
22
|
+
/** Strip the "models/" resource prefix from a Gemini model name (e.g. "models/gemini-3.8-flash"). */
|
|
23
23
|
export declare const normalizeGeminiModelId: (name: string) => string;
|
|
24
24
|
/**
|
|
25
25
|
* List the models available to a Gemini Developer API key via the `models` REST endpoint. Returns
|
|
26
|
-
* normalized model ids (e.g. "gemini-
|
|
26
|
+
* normalized model ids (e.g. "gemini-3.8-flash"). By default only chat-capable models (those
|
|
27
27
|
* supporting `generateContent`) are returned. Returns `undefined` when the listing could not be
|
|
28
28
|
* retrieved (missing key, network error, non-200 response, etc.).
|
|
29
29
|
*/
|
package/dist/service/gemini.js
CHANGED
|
@@ -6,21 +6,21 @@ const api_1 = require("@terreno/api");
|
|
|
6
6
|
* Helper for listing models available to the Gemini Developer API (the API-key based
|
|
7
7
|
* `generativelanguage.googleapis.com` service, distinct from Vertex / the Gemini Enterprise Agent
|
|
8
8
|
* Platform). Used to drive model pickers from the live set of models Google actually exposes for a
|
|
9
|
-
* given API key, so retired models (e.g. an old `gemini-2.
|
|
9
|
+
* given API key, so retired models (e.g. an old `gemini-2.5-flash`) never appear.
|
|
10
10
|
*/
|
|
11
11
|
/** Default Gemini Developer API base URL. */
|
|
12
12
|
exports.GEMINI_API_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
13
13
|
const MAX_MODEL_LIST_PAGES = 10;
|
|
14
14
|
const MODEL_LIST_PAGE_SIZE = "200";
|
|
15
15
|
const GENERATE_CONTENT_METHOD = "generateContent";
|
|
16
|
-
/** Strip the "models/" resource prefix from a Gemini model name (e.g. "models/gemini-
|
|
16
|
+
/** Strip the "models/" resource prefix from a Gemini model name (e.g. "models/gemini-3.8-flash"). */
|
|
17
17
|
const normalizeGeminiModelId = (name) => {
|
|
18
18
|
return name.trim().replace(/^models\//, "");
|
|
19
19
|
};
|
|
20
20
|
exports.normalizeGeminiModelId = normalizeGeminiModelId;
|
|
21
21
|
/**
|
|
22
22
|
* List the models available to a Gemini Developer API key via the `models` REST endpoint. Returns
|
|
23
|
-
* normalized model ids (e.g. "gemini-
|
|
23
|
+
* normalized model ids (e.g. "gemini-3.8-flash"). By default only chat-capable models (those
|
|
24
24
|
* supporting `generateContent`) are returned. Returns `undefined` when the listing could not be
|
|
25
25
|
* retrieved (missing key, network error, non-200 response, etc.).
|
|
26
26
|
*/
|
package/dist/service/vertex.d.ts
CHANGED
|
@@ -12,8 +12,8 @@ import type { ImageModel, LanguageModel } from "ai";
|
|
|
12
12
|
* - `verifyVertexModelsEnabled` / `assertVertexModelsEnabled` confirm that the requested models are
|
|
13
13
|
* actually enabled/available for the project using the Google publisher-models listing API.
|
|
14
14
|
*/
|
|
15
|
-
/** Default Vertex / Gemini Enterprise Agent Platform region. */
|
|
16
|
-
export declare const DEFAULT_VERTEX_LOCATION = "
|
|
15
|
+
/** Default Vertex / Gemini Enterprise Agent Platform region. Only global is supported for Gemini 3. */
|
|
16
|
+
export declare const DEFAULT_VERTEX_LOCATION = "global";
|
|
17
17
|
/** Provider returned by `@ai-sdk/google-vertex` `createVertex`. */
|
|
18
18
|
export interface VertexLanguageModelProvider {
|
|
19
19
|
(modelId: string): LanguageModel;
|
|
@@ -31,7 +31,7 @@ export interface CreateVertexProviderOptions {
|
|
|
31
31
|
* Vertex models are permitted. When provided, only listed models may be resolved.
|
|
32
32
|
*/
|
|
33
33
|
allowedModels?: string[];
|
|
34
|
-
/** Vertex location/region.
|
|
34
|
+
/** Vertex location/region. Forced to global endpoint for Gemini 3 support. */
|
|
35
35
|
location?: string;
|
|
36
36
|
/** GCP project id. Falls back to the GOOGLE_VERTEX_PROJECT env var. */
|
|
37
37
|
project?: string;
|
|
@@ -70,7 +70,7 @@ export interface ListEnabledVertexModelsOptions {
|
|
|
70
70
|
fetchImpl?: typeof fetch;
|
|
71
71
|
/** Injectable access-token getter (defaults to Application Default Credentials). */
|
|
72
72
|
getAccessToken?: () => Promise<string | undefined>;
|
|
73
|
-
/** Vertex location/region.
|
|
73
|
+
/** Vertex location/region. Forced to global endpoint for Gemini 3 support. */
|
|
74
74
|
location?: string;
|
|
75
75
|
/** GCP project id. */
|
|
76
76
|
project: string;
|
|
@@ -90,8 +90,8 @@ export interface VerifyVertexModelsOptions extends ListEnabledVertexModelsOption
|
|
|
90
90
|
models: string[];
|
|
91
91
|
}
|
|
92
92
|
/**
|
|
93
|
-
* Normalize a publisher model resource name (e.g. "publishers/google/models/gemini-
|
|
94
|
-
* "gemini-
|
|
93
|
+
* Normalize a publisher model resource name (e.g. "publishers/google/models/gemini-3.8-flash" or
|
|
94
|
+
* "gemini-3.8-flash@001") down to its bare model id (e.g. "gemini-3.8-flash").
|
|
95
95
|
*/
|
|
96
96
|
export declare const normalizeVertexModelId: (modelName: string) => string;
|
|
97
97
|
/**
|