@terreno/ai 57.4.0 → 57.6.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.
@@ -1,124 +1,30 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
24
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __asyncValues = (this && this.__asyncValues) || function (o) {
50
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
51
- var m = o[Symbol.asyncIterator], i;
52
- return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
53
- function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
54
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
55
- };
56
- var __rest = (this && this.__rest) || function (s, e) {
57
- var t = {};
58
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
59
- t[p] = s[p];
60
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
61
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
62
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
63
- t[p[i]] = s[p[i]];
64
- }
65
- return t;
66
- };
67
- var __values = (this && this.__values) || function(o) {
68
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
69
- if (m) return m.call(o);
70
- if (o && typeof o.length === "number") return {
71
- next: function () {
72
- if (o && i >= o.length) o = void 0;
73
- return { value: o && o[i++], done: !o };
74
- }
75
- };
76
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
77
- };
78
- var __read = (this && this.__read) || function (o, n) {
79
- var m = typeof Symbol === "function" && o[Symbol.iterator];
80
- if (!m) return o;
81
- var i = m.call(o), r, ar = [], e;
82
- try {
83
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
84
- }
85
- catch (error) { e = { error: error }; }
86
- finally {
87
- try {
88
- if (r && !r.done && (m = i["return"])) m.call(i);
89
- }
90
- finally { if (e) throw e.error; }
91
- }
92
- return ar;
93
- };
94
2
  Object.defineProperty(exports, "__esModule", { value: true });
95
3
  exports.addGptRoutes = void 0;
96
- var api_1 = require("@terreno/api");
97
- var ai_1 = require("ai");
98
- var luxon_1 = require("luxon");
99
- var langfuseVercelAi_1 = require("../langfuseVercelAi");
100
- var aiRequest_1 = require("../models/aiRequest");
101
- var gptHistory_1 = require("../models/gptHistory");
102
- var project_1 = require("../models/project");
103
- var aiService_1 = require("../service/aiService");
104
- var prompts_1 = require("../service/prompts");
105
- var isGeneratedImageFile = function (value) {
106
- return typeof value === "object" &&
107
- value !== null &&
108
- "base64" in value &&
109
- typeof value.base64 === "string" &&
110
- "mediaType" in value &&
111
- typeof value.mediaType === "string" &&
112
- value.mediaType.startsWith("image/");
113
- };
114
- var DEMO_RESPONSE = "This is demo mode. To use AI features, paste your Gemini API key in Settings.";
4
+ const api_1 = require("@terreno/api");
5
+ const ai_1 = require("ai");
6
+ const luxon_1 = require("luxon");
7
+ const langfuseVercelAi_1 = require("../langfuseVercelAi");
8
+ const aiRequest_1 = require("../models/aiRequest");
9
+ const gptHistory_1 = require("../models/gptHistory");
10
+ const project_1 = require("../models/project");
11
+ const aiService_1 = require("../service/aiService");
12
+ const prompts_1 = require("../service/prompts");
13
+ const isGeneratedImageFile = (value) => typeof value === "object" &&
14
+ value !== null &&
15
+ "base64" in value &&
16
+ typeof value.base64 === "string" &&
17
+ "mediaType" in value &&
18
+ typeof value.mediaType === "string" &&
19
+ value.mediaType.startsWith("image/");
20
+ const DEMO_RESPONSE = "This is demo mode. To use AI features, paste your Gemini API key in Settings.";
115
21
  /** Send a canned SSE demo response when no AI service is available. */
116
- var sendDemoResponse = function (res, historyId) {
22
+ const sendDemoResponse = (res, historyId) => {
117
23
  res.setHeader("Content-Type", "text/event-stream");
118
24
  res.setHeader("Cache-Control", "no-cache");
119
25
  res.setHeader("Connection", "keep-alive");
120
- res.write("data: ".concat(JSON.stringify({ text: DEMO_RESPONSE }), "\n\n"));
121
- res.write("data: ".concat(JSON.stringify(__assign({ done: true }, (historyId ? { historyId: historyId } : {}))), "\n\n"));
26
+ res.write(`data: ${JSON.stringify({ text: DEMO_RESPONSE })}\n\n`);
27
+ res.write(`data: ${JSON.stringify({ done: true, ...(historyId ? { historyId } : {}) })}\n\n`);
122
28
  res.end();
123
29
  };
124
30
  /**
@@ -128,13 +34,13 @@ var sendDemoResponse = function (res, historyId) {
128
34
  * 3. Pre-configured aiService from route options
129
35
  * 4. undefined (triggers demo mode response)
130
36
  */
131
- var resolveAiService = function (req, options, modelId) {
132
- var perRequestKey = req.headers["x-ai-api-key"];
37
+ const resolveAiService = (req, options, modelId) => {
38
+ const perRequestKey = req.headers["x-ai-api-key"];
133
39
  if (perRequestKey && options.createModelFn) {
134
40
  return new aiService_1.AIService({ model: options.createModelFn(perRequestKey, modelId) });
135
41
  }
136
42
  if (modelId && options.createServerModelFn) {
137
- var serverModel = options.createServerModelFn(modelId);
43
+ const serverModel = options.createServerModelFn(modelId);
138
44
  if (serverModel) {
139
45
  return new aiService_1.AIService({ model: serverModel });
140
46
  }
@@ -142,48 +48,39 @@ var resolveAiService = function (req, options, modelId) {
142
48
  return options.aiService;
143
49
  };
144
50
  /** Generate a short title for a conversation using a cheap model call. */
145
- var generateTitle = function (prompt, response, aiService, options, perRequestApiKey) { return __awaiter(void 0, void 0, void 0, function () {
146
- var titleService, titleModel, conversationSnippet, title, _a;
147
- return __generator(this, function (_b) {
148
- switch (_b.label) {
149
- case 0:
150
- _b.trys.push([0, 2, , 3]);
151
- titleService = aiService;
152
- if (options.titleModelId) {
153
- if (options.createModelFn && perRequestApiKey) {
154
- titleService = new aiService_1.AIService({
155
- model: options.createModelFn(perRequestApiKey, options.titleModelId),
156
- });
157
- }
158
- else if (options.createServerModelFn) {
159
- titleModel = options.createServerModelFn(options.titleModelId);
160
- if (titleModel) {
161
- titleService = new aiService_1.AIService({ model: titleModel });
162
- }
163
- }
51
+ const generateTitle = async (prompt, response, aiService, options, perRequestApiKey) => {
52
+ try {
53
+ let titleService = aiService;
54
+ if (options.titleModelId) {
55
+ if (options.createModelFn && perRequestApiKey) {
56
+ titleService = new aiService_1.AIService({
57
+ model: options.createModelFn(perRequestApiKey, options.titleModelId),
58
+ });
59
+ }
60
+ else if (options.createServerModelFn) {
61
+ const titleModel = options.createServerModelFn(options.titleModelId);
62
+ if (titleModel) {
63
+ titleService = new aiService_1.AIService({ model: titleModel });
164
64
  }
165
- conversationSnippet = "User: ".concat(prompt, "\nAssistant: ").concat(response.substring(0, 500));
166
- return [4 /*yield*/, titleService.generateText({
167
- prompt: conversationSnippet,
168
- systemPrompt: prompts_1.TITLE_GENERATION_PROMPT,
169
- temperature: 0.3,
170
- })];
171
- case 1:
172
- title = _b.sent();
173
- return [2 /*return*/, title.trim().replace(/^["']|["']$/g, "")];
174
- case 2:
175
- _a = _b.sent();
176
- return [2 /*return*/, undefined];
177
- case 3: return [2 /*return*/];
65
+ }
178
66
  }
179
- });
180
- }); };
181
- var addGptRoutes = function (router, options) {
182
- var _a, _b, _c, _d;
183
- var mcpService = options.mcpService, routeTools = options.tools, createRequestTools = options.createRequestTools, toolChoice = options.toolChoice, maxSteps = options.maxSteps;
67
+ const conversationSnippet = `User: ${prompt}\nAssistant: ${response.substring(0, 500)}`;
68
+ const title = await titleService.generateText({
69
+ prompt: conversationSnippet,
70
+ systemPrompt: prompts_1.TITLE_GENERATION_PROMPT,
71
+ temperature: 0.3,
72
+ });
73
+ return title.trim().replace(/^["']|["']$/g, "");
74
+ }
75
+ catch {
76
+ return undefined;
77
+ }
78
+ };
79
+ const addGptRoutes = (router, options) => {
80
+ const { mcpService, tools: routeTools, createRequestTools, toolChoice, maxSteps } = options;
184
81
  router.post("/gpt/prompt", [
185
82
  (0, api_1.authenticateMiddleware)(),
186
- (0, api_1.createOpenApiBuilder)((_a = options.openApiOptions) !== null && _a !== void 0 ? _a : {})
83
+ (0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
187
84
  .withTags(["gpt"])
188
85
  .withSummary("Stream a GPT chat response")
189
86
  .withRequestBody({
@@ -210,505 +107,431 @@ var addGptRoutes = function (router, options) {
210
107
  ],
211
108
  // Use a raw handler instead of asyncHandler so we can control error responses
212
109
  // for both pre-stream (JSON) and mid-stream (SSE) errors.
213
- function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
214
- var sseStarted, _a, prompt_1, historyId, systemPrompt, attachments, requestModel, projectId, userId, hasPerRequestKey, hasCreateModelFn, hasConfiguredService, aiService, history_1, effectiveProjectId, effectiveSystemPrompt, project, parts, projectContext, _b, langfuseResult, langfusePrompt, err_1, contentParts, attachments_1, attachments_1_1, attachment, hasAttachments, userPrompt, messages, modelId, supportsTools, allTools, requestTools, mcpTools, _c, fullResponse, generatedImages, startTime, telemetry, result, partCount, stepTextBuffer, stepHasToolCall, _d, _e, _f, part, cleaned, errMsg, dataUrl, textChunk, toolResult, _g, _fileData, cleanResult, e_1_1, cleaned, files, files_1, files_1_1, file, dataUrl, fileErr_1, contentParts_1, assistantPrompt, logErr_1, perRequestApiKey, title, error_1, logErr_2, outerError_1, message, status_1, stack;
215
- var e_2, _h, e_3, _j;
216
- var _k, e_1, _l, _m;
217
- var _o, _p, _q, _r, _s, _t;
218
- return __generator(this, function (_u) {
219
- switch (_u.label) {
220
- case 0:
221
- sseStarted = false;
222
- _u.label = 1;
223
- case 1:
224
- _u.trys.push([1, 48, , 49]);
225
- _a = req.body, prompt_1 = _a.prompt, historyId = _a.historyId, systemPrompt = _a.systemPrompt, attachments = _a.attachments, requestModel = _a.model, projectId = _a.projectId;
226
- userId = (_o = req.user) === null || _o === void 0 ? void 0 : _o._id;
227
- if (!prompt_1 || typeof prompt_1 !== "string") {
228
- throw new api_1.APIError({ status: 400, title: "prompt is required" });
229
- }
230
- hasPerRequestKey = !!req.headers["x-ai-api-key"];
231
- hasCreateModelFn = !!options.createModelFn;
232
- hasConfiguredService = !!options.aiService;
233
- api_1.logger.debug("Resolving AI service", {
234
- hasConfiguredService: hasConfiguredService,
235
- hasCreateModelFn: hasCreateModelFn,
236
- hasPerRequestKey: hasPerRequestKey,
237
- });
238
- aiService = resolveAiService(req, options, requestModel);
239
- if (!aiService) {
240
- api_1.logger.debug("No AI service available, sending demo response");
241
- return [2 /*return*/, sendDemoResponse(res)];
242
- }
243
- if (!historyId) return [3 /*break*/, 3];
244
- return [4 /*yield*/, gptHistory_1.GptHistory.findById(historyId)];
245
- case 2:
246
- history_1 = _u.sent();
247
- if (!history_1) {
248
- throw new api_1.APIError({ status: 404, title: "History not found" });
249
- }
250
- if (history_1.userId.toString() !== (userId === null || userId === void 0 ? void 0 : userId.toString())) {
251
- throw new api_1.APIError({ status: 403, title: "Not authorized to access this history" });
252
- }
253
- return [3 /*break*/, 4];
254
- case 3:
255
- history_1 = new gptHistory_1.GptHistory(__assign({ prompts: [], userId: userId }, (projectId ? { projectId: projectId } : {})));
256
- _u.label = 4;
257
- case 4:
258
- // If history doesn't have a projectId yet but one was provided, associate it
259
- if (projectId && !history_1.projectId) {
260
- history_1.projectId = projectId;
261
- }
262
- effectiveProjectId = projectId !== null && projectId !== void 0 ? projectId : history_1.projectId;
263
- effectiveSystemPrompt = systemPrompt;
264
- if (!effectiveProjectId) return [3 /*break*/, 8];
265
- _u.label = 5;
266
- case 5:
267
- _u.trys.push([5, 7, , 8]);
268
- return [4 /*yield*/, project_1.Project.findById(effectiveProjectId)];
269
- case 6:
270
- project = _u.sent();
271
- if (project && project.userId.toString() === (userId === null || userId === void 0 ? void 0 : userId.toString())) {
272
- parts = [];
110
+ async (req, res) => {
111
+ let sseStarted = false;
112
+ try {
113
+ const { prompt, historyId, systemPrompt, attachments, model: requestModel, projectId, } = req.body;
114
+ const userId = req.user?._id;
115
+ if (!prompt || typeof prompt !== "string") {
116
+ throw new api_1.APIError({ status: 400, title: "prompt is required" });
117
+ }
118
+ // Resolve AI service (per-request key takes priority, then configured service)
119
+ const hasPerRequestKey = !!req.headers["x-ai-api-key"];
120
+ const hasCreateModelFn = !!options.createModelFn;
121
+ const hasConfiguredService = !!options.aiService;
122
+ api_1.logger.debug("Resolving AI service", {
123
+ hasConfiguredService,
124
+ hasCreateModelFn,
125
+ hasPerRequestKey,
126
+ });
127
+ const aiService = resolveAiService(req, options, requestModel);
128
+ if (!aiService) {
129
+ api_1.logger.debug("No AI service available, sending demo response");
130
+ return sendDemoResponse(res);
131
+ }
132
+ // Load or create history
133
+ let history;
134
+ if (historyId) {
135
+ history = await gptHistory_1.GptHistory.findById(historyId);
136
+ if (!history) {
137
+ throw new api_1.APIError({ status: 404, title: "History not found" });
138
+ }
139
+ if (history.userId.toString() !== userId?.toString()) {
140
+ throw new api_1.APIError({ status: 403, title: "Not authorized to access this history" });
141
+ }
142
+ }
143
+ else {
144
+ history = new gptHistory_1.GptHistory({ prompts: [], userId, ...(projectId ? { projectId } : {}) });
145
+ }
146
+ // If history doesn't have a projectId yet but one was provided, associate it
147
+ if (projectId && !history.projectId) {
148
+ history.projectId = projectId;
149
+ }
150
+ // Load project context if a projectId is provided (or inherited from history)
151
+ const effectiveProjectId = projectId ?? history.projectId;
152
+ let effectiveSystemPrompt = systemPrompt;
153
+ if (effectiveProjectId) {
154
+ try {
155
+ const project = await project_1.Project.findById(effectiveProjectId);
156
+ if (project && project.userId.toString() === userId?.toString()) {
157
+ const parts = [];
273
158
  if (project.systemContext) {
274
159
  parts.push(project.systemContext);
275
160
  }
276
161
  if (project.memories.length > 0) {
277
- parts.push("## Relevant Memories\n".concat(project.memories.map(function (m) { return "- ".concat(m.text); }).join("\n")));
162
+ parts.push(`## Relevant Memories\n${project.memories.map((m) => `- ${m.text}`).join("\n")}`);
278
163
  }
279
164
  if (parts.length > 0) {
280
- projectContext = parts.join("\n\n");
165
+ const projectContext = parts.join("\n\n");
281
166
  effectiveSystemPrompt = effectiveSystemPrompt
282
- ? "".concat(projectContext, "\n\n").concat(effectiveSystemPrompt)
167
+ ? `${projectContext}\n\n${effectiveSystemPrompt}`
283
168
  : projectContext;
284
169
  }
285
170
  }
286
- return [3 /*break*/, 8];
287
- case 7:
288
- _b = _u.sent();
289
- return [3 /*break*/, 8];
290
- case 8:
291
- if (!options.langfuseSystemPromptName) return [3 /*break*/, 12];
292
- _u.label = 9;
293
- case 9:
294
- _u.trys.push([9, 11, , 12]);
295
- return [4 /*yield*/, (0, langfuseVercelAi_1.preparePromptForAI)({
296
- promptName: options.langfuseSystemPromptName,
297
- userId: userId === null || userId === void 0 ? void 0 : userId.toString(),
298
- })];
299
- case 10:
300
- langfuseResult = _u.sent();
301
- langfusePrompt = typeof langfuseResult.prompt === "string" ? langfuseResult.prompt : undefined;
171
+ }
172
+ catch {
173
+ // Project loading failure should not block the request
174
+ }
175
+ }
176
+ // Load system prompt from Langfuse if configured
177
+ if (options.langfuseSystemPromptName) {
178
+ try {
179
+ const langfuseResult = await (0, langfuseVercelAi_1.preparePromptForAI)({
180
+ promptName: options.langfuseSystemPromptName,
181
+ userId: userId?.toString(),
182
+ });
183
+ const langfusePrompt = typeof langfuseResult.prompt === "string" ? langfuseResult.prompt : undefined;
302
184
  if (langfusePrompt) {
303
185
  effectiveSystemPrompt = effectiveSystemPrompt
304
- ? "".concat(langfusePrompt, "\n\n").concat(effectiveSystemPrompt)
186
+ ? `${langfusePrompt}\n\n${effectiveSystemPrompt}`
305
187
  : langfusePrompt;
306
188
  }
307
- return [3 /*break*/, 12];
308
- case 11:
309
- err_1 = _u.sent();
310
- api_1.logger.debug("Langfuse system prompt skipped: ".concat(err_1.message));
311
- return [3 /*break*/, 12];
312
- case 12:
313
- contentParts = [{ text: prompt_1, type: "text" }];
314
- if (attachments && Array.isArray(attachments)) {
315
- api_1.logger.debug("Processing attachments", {
316
- count: attachments.length,
317
- types: attachments.map(function (a) {
318
- var _a, _b;
319
- return ({
320
- mimeType: a.mimeType,
321
- type: a.type,
322
- urlLength: (_b = (_a = a.url) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0,
323
- });
324
- }),
189
+ }
190
+ catch (err) {
191
+ api_1.logger.debug(`Langfuse system prompt skipped: ${err.message}`);
192
+ }
193
+ }
194
+ // 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,
325
211
  });
326
- try {
327
- for (attachments_1 = __values(attachments), attachments_1_1 = attachments_1.next(); !attachments_1_1.done; attachments_1_1 = attachments_1.next()) {
328
- attachment = attachments_1_1.value;
329
- if (attachment.type === "image") {
330
- contentParts.push({
331
- mimeType: attachment.mimeType,
332
- type: "image",
333
- url: attachment.url,
334
- });
335
- }
336
- else if (attachment.type === "file") {
337
- contentParts.push({
338
- filename: attachment.filename,
339
- mimeType: attachment.mimeType,
340
- type: "file",
341
- url: attachment.url,
342
- });
343
- }
344
- }
345
- }
346
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
347
- finally {
348
- try {
349
- if (attachments_1_1 && !attachments_1_1.done && (_h = attachments_1.return)) _h.call(attachments_1);
350
- }
351
- finally { if (e_2) throw e_2.error; }
352
- }
353
212
  }
354
- hasAttachments = contentParts.length > 1;
355
- userPrompt = __assign({ text: prompt_1, type: "user" }, (hasAttachments ? { content: contentParts } : {}));
356
- history_1.prompts.push(userPrompt);
357
- // Build messages from history using AIService helper
358
- api_1.logger.debug("Building messages", {
359
- attachmentCount: contentParts.length - 1,
360
- historyLength: history_1.prompts.length,
361
- });
362
- messages = aiService.buildMessages(history_1.prompts);
363
- api_1.logger.debug("Messages built", { messageCount: messages.length });
364
- modelId = aiService.modelId;
365
- supportsTools = !(modelId === null || modelId === void 0 ? void 0 : modelId.includes("image"));
366
- allTools = void 0;
367
- requestTools = createRequestTools ? createRequestTools(req) : undefined;
368
- if (!(supportsTools && (routeTools || requestTools || mcpService))) return [3 /*break*/, 16];
369
- allTools = __assign(__assign({}, (routeTools !== null && routeTools !== void 0 ? routeTools : {})), (requestTools !== null && requestTools !== void 0 ? requestTools : {}));
370
- if (!mcpService) return [3 /*break*/, 16];
371
- _u.label = 13;
372
- case 13:
373
- _u.trys.push([13, 15, , 16]);
374
- return [4 /*yield*/, mcpService.getTools()];
375
- case 14:
376
- mcpTools = _u.sent();
377
- Object.assign(allTools, mcpTools);
378
- return [3 /*break*/, 16];
379
- case 15:
380
- _c = _u.sent();
381
- return [3 /*break*/, 16];
382
- case 16:
383
- // Stream response via SSE
384
- res.setHeader("Content-Type", "text/event-stream");
385
- res.setHeader("Cache-Control", "no-cache");
386
- res.setHeader("Connection", "keep-alive");
387
- sseStarted = true;
388
- fullResponse = "";
389
- generatedImages = [];
390
- startTime = luxon_1.DateTime.now().toMillis();
391
- _u.label = 17;
392
- case 17:
393
- _u.trys.push([17, 42, , 47]);
394
- api_1.logger.debug("Starting streamText", { model: modelId, supportsTools: supportsTools });
395
- telemetry = (0, langfuseVercelAi_1.createTelemetryConfig)({
396
- functionId: "gpt-prompt",
397
- metadata: __assign({}, (options.langfuseSystemPromptName
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
+ }
223
+ // Add user prompt to history
224
+ const hasAttachments = contentParts.length > 1;
225
+ const userPrompt = {
226
+ text: prompt,
227
+ type: "user",
228
+ ...(hasAttachments ? { content: contentParts } : {}),
229
+ };
230
+ history.prompts.push(userPrompt);
231
+ // Build messages from history using AIService helper
232
+ api_1.logger.debug("Building messages", {
233
+ attachmentCount: contentParts.length - 1,
234
+ historyLength: history.prompts.length,
235
+ });
236
+ const messages = aiService.buildMessages(history.prompts);
237
+ api_1.logger.debug("Messages built", { messageCount: messages.length });
238
+ // Some models (e.g. gemini-2.5-flash-image) don't support tool calling
239
+ const modelId = aiService.modelId;
240
+ const supportsTools = !modelId?.includes("image");
241
+ // Merge tools from route config, per-request tools, and MCP service
242
+ let allTools;
243
+ const requestTools = createRequestTools ? createRequestTools(req) : undefined;
244
+ if (supportsTools && (routeTools || requestTools || mcpService)) {
245
+ allTools = { ...(routeTools ?? {}), ...(requestTools ?? {}) };
246
+ if (mcpService) {
247
+ try {
248
+ const mcpTools = await mcpService.getTools();
249
+ Object.assign(allTools, mcpTools);
250
+ }
251
+ catch {
252
+ // MCP tool discovery failure should not block the request
253
+ }
254
+ }
255
+ }
256
+ // Stream response via SSE
257
+ res.setHeader("Content-Type", "text/event-stream");
258
+ res.setHeader("Cache-Control", "no-cache");
259
+ res.setHeader("Connection", "keep-alive");
260
+ sseStarted = true;
261
+ let fullResponse = "";
262
+ const generatedImages = [];
263
+ const startTime = luxon_1.DateTime.now().toMillis();
264
+ try {
265
+ api_1.logger.debug("Starting streamText", { model: modelId, supportsTools });
266
+ const telemetry = (0, langfuseVercelAi_1.createTelemetryConfig)({
267
+ functionId: "gpt-prompt",
268
+ metadata: {
269
+ ...(options.langfuseSystemPromptName
398
270
  ? { langfusePromptName: options.langfuseSystemPromptName }
399
- : {})),
400
- userId: userId === null || userId === void 0 ? void 0 : userId.toString(),
401
- });
402
- result = (0, ai_1.streamText)({
403
- experimental_telemetry: telemetry,
404
- messages: messages,
405
- model: aiService.model,
406
- providerOptions: !supportsTools
407
- ? {
408
- google: { responseModalities: ["TEXT", "IMAGE"] },
409
- vertex: { responseModalities: ["TEXT", "IMAGE"] },
410
- }
411
- : undefined,
412
- stopWhen: allTools ? (0, ai_1.stepCountIs)(maxSteps !== null && maxSteps !== void 0 ? maxSteps : 5) : (0, ai_1.stepCountIs)(1),
413
- system: effectiveSystemPrompt !== null && effectiveSystemPrompt !== void 0 ? effectiveSystemPrompt : undefined,
414
- temperature: aiService.defaultTemperature,
415
- toolChoice: allTools ? (toolChoice !== null && toolChoice !== void 0 ? toolChoice : "auto") : undefined,
416
- tools: allTools,
417
- });
418
- partCount = 0;
419
- stepTextBuffer = "";
420
- stepHasToolCall = false;
421
- _u.label = 18;
422
- case 18:
423
- _u.trys.push([18, 23, 24, 29]);
424
- _d = true, _e = __asyncValues(result.fullStream);
425
- _u.label = 19;
426
- case 19: return [4 /*yield*/, _e.next()];
427
- case 20:
428
- if (!(_f = _u.sent(), _k = _f.done, !_k)) return [3 /*break*/, 22];
429
- _m = _f.value;
430
- _d = false;
431
- part = _m;
271
+ : {}),
272
+ },
273
+ userId: userId?.toString(),
274
+ });
275
+ const result = (0, ai_1.streamText)({
276
+ experimental_telemetry: telemetry,
277
+ messages,
278
+ model: aiService.model,
279
+ providerOptions: !supportsTools
280
+ ? {
281
+ google: { responseModalities: ["TEXT", "IMAGE"] },
282
+ vertex: { responseModalities: ["TEXT", "IMAGE"] },
283
+ }
284
+ : undefined,
285
+ stopWhen: allTools ? (0, ai_1.stepCountIs)(maxSteps ?? 5) : (0, ai_1.stepCountIs)(1),
286
+ system: effectiveSystemPrompt ?? undefined,
287
+ temperature: aiService.defaultTemperature,
288
+ toolChoice: allTools ? (toolChoice ?? "auto") : undefined,
289
+ tools: allTools,
290
+ });
291
+ 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
+ for await (const part of result.fullStream) {
432
296
  partCount++;
433
297
  if (partCount <= 5 || part.type === "error" || part.type === "file") {
434
- api_1.logger.debug("Stream part", __assign(__assign({ partCount: partCount, type: part.type }, (part.type === "file" && isGeneratedImageFile(part.file)
435
- ? { mediaType: part.file.mediaType }
436
- : {})), (part.type === "error" ? { error: String((_p = part.error) !== null && _p !== void 0 ? _p : part) } : {})));
298
+ api_1.logger.debug("Stream part", {
299
+ partCount,
300
+ type: part.type,
301
+ ...(part.type === "file" && isGeneratedImageFile(part.file)
302
+ ? { mediaType: part.file.mediaType }
303
+ : {}),
304
+ ...(part.type === "error" ? { error: String(part.error ?? part) } : {}),
305
+ });
437
306
  }
438
307
  // Track step boundaries to discard reasoning text from tool-call steps
439
308
  if (part.type === "start-step") {
440
309
  stepTextBuffer = "";
441
310
  stepHasToolCall = false;
442
- return [3 /*break*/, 21];
311
+ continue;
443
312
  }
444
313
  if (part.type === "finish-step") {
445
314
  // Only emit buffered text if no tool call happened in this step
446
315
  if (!stepHasToolCall && stepTextBuffer) {
447
- cleaned = stepTextBuffer
316
+ // Strip model reasoning that leaks as JSON action blobs
317
+ const cleaned = stepTextBuffer
448
318
  .replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "")
449
319
  .trim();
450
320
  if (cleaned) {
451
321
  fullResponse += cleaned;
452
- res.write("data: ".concat(JSON.stringify({ text: cleaned }), "\n\n"));
322
+ res.write(`data: ${JSON.stringify({ text: cleaned })}\n\n`);
453
323
  }
454
324
  }
455
325
  stepTextBuffer = "";
456
326
  stepHasToolCall = false;
457
- return [3 /*break*/, 21];
327
+ continue;
458
328
  }
459
329
  if (part.type === "error") {
460
- errMsg = part.error instanceof Error
330
+ const errMsg = part.error instanceof Error
461
331
  ? part.error.message
462
- : String((_q = part.error) !== null && _q !== void 0 ? _q : "Unknown stream error");
332
+ : String(part.error ?? "Unknown stream error");
463
333
  api_1.logger.error("AI stream error part", { error: errMsg });
464
- res.write("data: ".concat(JSON.stringify({ error: errMsg }), "\n\n"));
465
- return [3 /*break*/, 21];
334
+ res.write(`data: ${JSON.stringify({ error: errMsg })}\n\n`);
335
+ continue;
466
336
  }
467
337
  if (part.type === "file") {
468
338
  if (isGeneratedImageFile(part.file)) {
469
- dataUrl = "data:".concat(part.file.mediaType, ";base64,").concat(part.file.base64);
339
+ const dataUrl = `data:${part.file.mediaType};base64,${part.file.base64}`;
470
340
  generatedImages.push({ mimeType: part.file.mediaType, url: dataUrl });
471
- res.write("data: ".concat(JSON.stringify({
341
+ res.write(`data: ${JSON.stringify({
472
342
  image: { mimeType: part.file.mediaType, url: dataUrl },
473
- }), "\n\n"));
343
+ })}\n\n`);
474
344
  api_1.logger.debug("Sent inline image from stream");
475
345
  }
476
- return [3 /*break*/, 21];
346
+ continue;
477
347
  }
478
348
  if (part.type === "text-delta") {
479
- textChunk = ((_r = part.text) !== null && _r !== void 0 ? _r : "");
349
+ const textChunk = (part.text ?? "");
480
350
  if (textChunk) {
481
351
  stepTextBuffer += textChunk;
482
352
  }
483
353
  }
484
354
  else if (part.type === "tool-call") {
485
355
  stepHasToolCall = true;
486
- res.write("data: ".concat(JSON.stringify({
356
+ res.write(`data: ${JSON.stringify({
487
357
  toolCall: {
488
358
  args: part.input,
489
359
  toolCallId: part.toolCallId,
490
360
  toolName: part.toolName,
491
361
  },
492
- }), "\n\n"));
362
+ })}\n\n`);
493
363
  // Persist tool call in history
494
- history_1.prompts.push({
364
+ history.prompts.push({
495
365
  args: part.input,
496
- text: "Tool call: ".concat(part.toolName),
366
+ text: `Tool call: ${part.toolName}`,
497
367
  toolCallId: part.toolCallId,
498
368
  toolName: part.toolName,
499
369
  type: "tool-call",
500
370
  });
501
371
  }
502
372
  else if (part.type === "tool-result") {
503
- toolResult = part.output;
373
+ const toolResult = part.output;
504
374
  // If the tool result contains file data, send it as a separate file SSE event
505
- if ((toolResult === null || toolResult === void 0 ? void 0 : toolResult.fileData) && typeof toolResult.fileData === "string") {
506
- res.write("data: ".concat(JSON.stringify({
375
+ if (toolResult?.fileData && typeof toolResult.fileData === "string") {
376
+ res.write(`data: ${JSON.stringify({
507
377
  file: {
508
- filename: (_s = toolResult.filename) !== null && _s !== void 0 ? _s : "document",
509
- mimeType: (_t = toolResult.mimeType) !== null && _t !== void 0 ? _t : "application/octet-stream",
378
+ filename: toolResult.filename ?? "document",
379
+ mimeType: toolResult.mimeType ?? "application/octet-stream",
510
380
  url: toolResult.fileData,
511
381
  },
512
- }), "\n\n"));
382
+ })}\n\n`);
513
383
  api_1.logger.debug("Sent generated file from tool result", {
514
384
  filename: toolResult.filename,
515
385
  mimeType: toolResult.mimeType,
516
386
  });
517
387
  }
518
- _g = toolResult !== null && toolResult !== void 0 ? toolResult : {}, _fileData = _g.fileData, cleanResult = __rest(_g, ["fileData"]);
519
- res.write("data: ".concat(JSON.stringify({
388
+ // Strip fileData from result before sending/storing to avoid bloating the SSE and DB
389
+ const { fileData: _fileData, ...cleanResult } = toolResult ?? {};
390
+ res.write(`data: ${JSON.stringify({
520
391
  toolResult: {
521
392
  result: cleanResult,
522
393
  toolCallId: part.toolCallId,
523
394
  toolName: part.toolName,
524
395
  },
525
- }), "\n\n"));
396
+ })}\n\n`);
526
397
  // Persist tool result in history (without the large file data)
527
- history_1.prompts.push({
398
+ history.prompts.push({
528
399
  result: cleanResult,
529
- text: "Tool result: ".concat(part.toolName),
400
+ text: `Tool result: ${part.toolName}`,
530
401
  toolCallId: part.toolCallId,
531
402
  toolName: part.toolName,
532
403
  type: "tool-result",
533
404
  });
534
405
  }
535
- _u.label = 21;
536
- case 21:
537
- _d = true;
538
- return [3 /*break*/, 19];
539
- case 22: return [3 /*break*/, 29];
540
- case 23:
541
- e_1_1 = _u.sent();
542
- e_1 = { error: e_1_1 };
543
- return [3 /*break*/, 29];
544
- case 24:
545
- _u.trys.push([24, , 27, 28]);
546
- if (!(!_d && !_k && (_l = _e.return))) return [3 /*break*/, 26];
547
- return [4 /*yield*/, _l.call(_e)];
548
- case 25:
549
- _u.sent();
550
- _u.label = 26;
551
- case 26: return [3 /*break*/, 28];
552
- case 27:
553
- if (e_1) throw e_1.error;
554
- return [7 /*endfinally*/];
555
- case 28: return [7 /*endfinally*/];
556
- case 29:
557
- // Flush any remaining buffered text from the last step
558
- if (!stepHasToolCall && stepTextBuffer) {
559
- cleaned = stepTextBuffer.replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "").trim();
560
- if (cleaned) {
561
- fullResponse += cleaned;
562
- res.write("data: ".concat(JSON.stringify({ text: cleaned }), "\n\n"));
563
- }
406
+ }
407
+ // Flush any remaining buffered text from the last step
408
+ if (!stepHasToolCall && stepTextBuffer) {
409
+ const cleaned = stepTextBuffer.replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "").trim();
410
+ if (cleaned) {
411
+ fullResponse += cleaned;
412
+ res.write(`data: ${JSON.stringify({ text: cleaned })}\n\n`);
564
413
  }
565
- api_1.logger.debug("Stream completed", { fullResponseLength: fullResponse.length, partCount: partCount });
566
- _u.label = 30;
567
- case 30:
568
- _u.trys.push([30, 32, , 33]);
569
- return [4 /*yield*/, result.files];
570
- case 31:
571
- files = _u.sent();
414
+ }
415
+ api_1.logger.debug("Stream completed", { fullResponseLength: fullResponse.length, partCount });
416
+ // Check for generated images (e.g. from gemini-2.5-flash-image)
417
+ try {
418
+ const files = await result.files;
572
419
  if (files && files.length > 0) {
573
- try {
574
- for (files_1 = __values(files), files_1_1 = files_1.next(); !files_1_1.done; files_1_1 = files_1.next()) {
575
- file = files_1_1.value;
576
- if (file.mediaType.startsWith("image/")) {
577
- dataUrl = "data:".concat(file.mediaType, ";base64,").concat(file.base64);
578
- generatedImages.push({ mimeType: file.mediaType, url: dataUrl });
579
- res.write("data: ".concat(JSON.stringify({
580
- image: { mimeType: file.mediaType, url: dataUrl },
581
- }), "\n\n"));
582
- }
420
+ for (const file of files) {
421
+ if (file.mediaType.startsWith("image/")) {
422
+ const dataUrl = `data:${file.mediaType};base64,${file.base64}`;
423
+ generatedImages.push({ mimeType: file.mediaType, url: dataUrl });
424
+ res.write(`data: ${JSON.stringify({
425
+ image: { mimeType: file.mediaType, url: dataUrl },
426
+ })}\n\n`);
583
427
  }
584
428
  }
585
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
586
- finally {
587
- try {
588
- if (files_1_1 && !files_1_1.done && (_j = files_1.return)) _j.call(files_1);
589
- }
590
- finally { if (e_3) throw e_3.error; }
591
- }
592
429
  api_1.logger.debug("Sent generated images", { count: files.length });
593
430
  }
594
- return [3 /*break*/, 33];
595
- case 32:
596
- fileErr_1 = _u.sent();
431
+ }
432
+ catch (fileErr) {
597
433
  api_1.logger.debug("No files in response", {
598
- error: fileErr_1 instanceof Error ? fileErr_1.message : String(fileErr_1),
434
+ error: fileErr instanceof Error ? fileErr.message : String(fileErr),
599
435
  });
600
- return [3 /*break*/, 33];
601
- case 33:
602
- // Save assistant response to history
603
- if (fullResponse || generatedImages.length > 0) {
604
- contentParts_1 = generatedImages.map(function (img) { return ({
605
- mimeType: img.mimeType,
606
- type: "image",
607
- url: img.url,
608
- }); });
609
- assistantPrompt = __assign({ model: aiService.modelId, text: fullResponse || "(image)", type: "assistant" }, (contentParts_1.length > 0 ? { content: contentParts_1 } : {}));
610
- history_1.prompts.push(assistantPrompt);
611
- }
612
- return [4 /*yield*/, history_1.save()];
613
- case 34:
614
- _u.sent();
615
- _u.label = 35;
616
- case 35:
617
- _u.trys.push([35, 37, , 38]);
618
- return [4 /*yield*/, aiRequest_1.AIRequest.logRequest({
619
- aiModel: modelId !== null && modelId !== void 0 ? modelId : "unknown",
620
- prompt: prompt_1,
621
- requestType: "general",
622
- response: fullResponse,
623
- responseTime: luxon_1.DateTime.now().toMillis() - startTime,
624
- userId: userId !== null && userId !== void 0 ? userId : undefined,
625
- })];
626
- case 36:
627
- _u.sent();
628
- return [3 /*break*/, 38];
629
- case 37:
630
- logErr_1 = _u.sent();
631
- api_1.logger.warn("Failed to log AIRequest", {
632
- error: logErr_1 instanceof Error ? logErr_1.message : String(logErr_1),
436
+ }
437
+ // Save assistant response to history
438
+ if (fullResponse || generatedImages.length > 0) {
439
+ const contentParts = generatedImages.map((img) => ({
440
+ mimeType: img.mimeType,
441
+ type: "image",
442
+ url: img.url,
443
+ }));
444
+ const assistantPrompt = {
445
+ model: aiService.modelId,
446
+ text: fullResponse || "(image)",
447
+ type: "assistant",
448
+ ...(contentParts.length > 0 ? { content: contentParts } : {}),
449
+ };
450
+ history.prompts.push(assistantPrompt);
451
+ }
452
+ await history.save();
453
+ try {
454
+ await aiRequest_1.AIRequest.logRequest({
455
+ aiModel: modelId ?? "unknown",
456
+ prompt,
457
+ requestType: "general",
458
+ response: fullResponse,
459
+ responseTime: luxon_1.DateTime.now().toMillis() - startTime,
460
+ userId: userId ?? undefined,
633
461
  });
634
- return [3 /*break*/, 38];
635
- case 38:
636
- if (!(!history_1.title && fullResponse)) return [3 /*break*/, 41];
637
- perRequestApiKey = req.headers["x-ai-api-key"];
638
- return [4 /*yield*/, generateTitle(prompt_1, fullResponse, aiService, options, perRequestApiKey)];
639
- case 39:
640
- title = _u.sent();
641
- if (!title) return [3 /*break*/, 41];
642
- history_1.title = title;
643
- return [4 /*yield*/, history_1.save()];
644
- case 40:
645
- _u.sent();
646
- _u.label = 41;
647
- case 41:
648
- api_1.logger.debug("Sending done event", {
649
- fullResponseLength: fullResponse.length,
650
- historyId: history_1._id.toString(),
462
+ }
463
+ catch (logErr) {
464
+ api_1.logger.warn("Failed to log AIRequest", {
465
+ error: logErr instanceof Error ? logErr.message : String(logErr),
651
466
  });
652
- res.write("data: ".concat(JSON.stringify(__assign({ done: true, historyId: history_1._id.toString() }, (history_1.title ? { title: history_1.title } : {}))), "\n\n"));
653
- res.end();
654
- return [3 /*break*/, 47];
655
- case 42:
656
- error_1 = _u.sent();
657
- api_1.logger.error("Error in GPT stream", {
658
- error: error_1 instanceof Error ? error_1.message : String(error_1),
467
+ }
468
+ // Generate a title for new conversations using a cheap model call
469
+ if (!history.title && fullResponse) {
470
+ const perRequestApiKey = req.headers["x-ai-api-key"];
471
+ const title = await generateTitle(prompt, fullResponse, aiService, options, perRequestApiKey);
472
+ if (title) {
473
+ history.title = title;
474
+ await history.save();
475
+ }
476
+ }
477
+ api_1.logger.debug("Sending done event", {
478
+ fullResponseLength: fullResponse.length,
479
+ historyId: history._id.toString(),
480
+ });
481
+ res.write(`data: ${JSON.stringify({
482
+ done: true,
483
+ historyId: history._id.toString(),
484
+ ...(history.title ? { title: history.title } : {}),
485
+ })}\n\n`);
486
+ res.end();
487
+ }
488
+ catch (error) {
489
+ api_1.logger.error("Error in GPT stream", {
490
+ error: error instanceof Error ? error.message : String(error),
491
+ });
492
+ try {
493
+ await aiRequest_1.AIRequest.logRequest({
494
+ aiModel: modelId ?? "unknown",
495
+ error: error instanceof Error ? error.message : String(error),
496
+ prompt,
497
+ requestType: "general",
498
+ responseTime: luxon_1.DateTime.now().toMillis() - startTime,
499
+ userId: userId ?? undefined,
659
500
  });
660
- _u.label = 43;
661
- case 43:
662
- _u.trys.push([43, 45, , 46]);
663
- return [4 /*yield*/, aiRequest_1.AIRequest.logRequest({
664
- aiModel: modelId !== null && modelId !== void 0 ? modelId : "unknown",
665
- error: error_1 instanceof Error ? error_1.message : String(error_1),
666
- prompt: prompt_1,
667
- requestType: "general",
668
- responseTime: luxon_1.DateTime.now().toMillis() - startTime,
669
- userId: userId !== null && userId !== void 0 ? userId : undefined,
670
- })];
671
- case 44:
672
- _u.sent();
673
- return [3 /*break*/, 46];
674
- case 45:
675
- logErr_2 = _u.sent();
501
+ }
502
+ catch (logErr) {
676
503
  api_1.logger.warn("Failed to log AIRequest error", {
677
- error: logErr_2 instanceof Error ? logErr_2.message : String(logErr_2),
504
+ error: logErr instanceof Error ? logErr.message : String(logErr),
678
505
  });
679
- return [3 /*break*/, 46];
680
- case 46:
681
- res.write("data: ".concat(JSON.stringify({ error: error_1 instanceof Error ? error_1.message : "Unknown error" }), "\n\n"));
682
- res.end();
683
- return [3 /*break*/, 47];
684
- case 47: return [3 /*break*/, 49];
685
- case 48:
686
- outerError_1 = _u.sent();
687
- message = outerError_1 instanceof Error ? outerError_1.message : String(outerError_1);
688
- status_1 = outerError_1 instanceof api_1.APIError ? outerError_1.status : 500;
689
- stack = outerError_1 instanceof Error ? outerError_1.stack : undefined;
690
- api_1.logger.error("GPT prompt handler error", { error: message, stack: stack, status: status_1 });
691
- if (sseStarted) {
692
- // Already sent SSE headers — send error as SSE event
693
- res.write("data: ".concat(JSON.stringify({ error: message }), "\n\n"));
694
- res.end();
695
- }
696
- else {
697
- // Haven't started SSE — send a normal JSON error response
698
- res.status(status_1).json({
699
- detail: message,
700
- status: status_1,
701
- title: outerError_1 instanceof api_1.APIError ? outerError_1.title : "Internal server error",
702
- });
703
- }
704
- return [3 /*break*/, 49];
705
- case 49: return [2 /*return*/];
506
+ }
507
+ res.write(`data: ${JSON.stringify({ error: error instanceof Error ? error.message : "Unknown error" })}\n\n`);
508
+ res.end();
706
509
  }
707
- });
708
- }); });
510
+ }
511
+ catch (outerError) {
512
+ // Catch-all for errors thrown before or after SSE streaming
513
+ const message = outerError instanceof Error ? outerError.message : String(outerError);
514
+ const status = outerError instanceof api_1.APIError ? outerError.status : 500;
515
+ const stack = outerError instanceof Error ? outerError.stack : undefined;
516
+ api_1.logger.error("GPT prompt handler error", { error: message, stack, status });
517
+ if (sseStarted) {
518
+ // Already sent SSE headers — send error as SSE event
519
+ res.write(`data: ${JSON.stringify({ error: message })}\n\n`);
520
+ res.end();
521
+ }
522
+ else {
523
+ // Haven't started SSE — send a normal JSON error response
524
+ res.status(status).json({
525
+ detail: message,
526
+ status,
527
+ title: outerError instanceof api_1.APIError ? outerError.title : "Internal server error",
528
+ });
529
+ }
530
+ }
531
+ });
709
532
  router.patch("/gpt/histories/:id/rating", [
710
533
  (0, api_1.authenticateMiddleware)(),
711
- (0, api_1.createOpenApiBuilder)((_b = options.openApiOptions) !== null && _b !== void 0 ? _b : {})
534
+ (0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
712
535
  .withTags(["gpt"])
713
536
  .withSummary("Rate a prompt in a GPT history")
714
537
  .withPathParameter("id", { type: "string" })
@@ -718,50 +541,39 @@ var addGptRoutes = function (router, options) {
718
541
  })
719
542
  .withResponse(200, { data: { type: "object" } })
720
543
  .build(),
721
- ], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
722
- var id, _a, promptIndex, rating, userId, history;
723
- var _b, _c;
724
- return __generator(this, function (_d) {
725
- switch (_d.label) {
726
- case 0:
727
- id = req.params.id;
728
- _a = req.body, promptIndex = _a.promptIndex, rating = _a.rating;
729
- userId = (_b = req.user) === null || _b === void 0 ? void 0 : _b._id;
730
- if (typeof promptIndex !== "number" || promptIndex < 0) {
731
- throw new api_1.APIError({ status: 400, title: "promptIndex must be a non-negative number" });
732
- }
733
- if (rating !== null && rating !== "up" && rating !== "down") {
734
- throw new api_1.APIError({ status: 400, title: "rating must be 'up', 'down', or null" });
735
- }
736
- return [4 /*yield*/, gptHistory_1.GptHistory.findById(id)];
737
- case 1:
738
- history = _d.sent();
739
- if (!history) {
740
- throw new api_1.APIError({ status: 404, title: "History not found" });
741
- }
742
- if (history.userId.toString() !== (userId === null || userId === void 0 ? void 0 : userId.toString())) {
743
- throw new api_1.APIError({ status: 403, title: "Not authorized to access this history" });
744
- }
745
- if (promptIndex >= history.prompts.length) {
746
- throw new api_1.APIError({ status: 400, title: "promptIndex out of range" });
747
- }
748
- if (rating === null) {
749
- history.prompts[promptIndex].rating = undefined;
750
- }
751
- else {
752
- history.prompts[promptIndex].rating = rating;
753
- }
754
- history.markModified("prompts");
755
- return [4 /*yield*/, history.save()];
756
- case 2:
757
- _d.sent();
758
- return [2 /*return*/, res.json({ data: { promptIndex: promptIndex, rating: (_c = history.prompts[promptIndex].rating) !== null && _c !== void 0 ? _c : null } })];
759
- }
760
- });
761
- }); }));
544
+ ], (0, api_1.asyncHandler)(async (req, res) => {
545
+ const { id } = req.params;
546
+ const { promptIndex, rating } = req.body;
547
+ const userId = req.user?._id;
548
+ if (typeof promptIndex !== "number" || promptIndex < 0) {
549
+ throw new api_1.APIError({ status: 400, title: "promptIndex must be a non-negative number" });
550
+ }
551
+ if (rating !== null && rating !== "up" && rating !== "down") {
552
+ throw new api_1.APIError({ status: 400, title: "rating must be 'up', 'down', or null" });
553
+ }
554
+ const history = await gptHistory_1.GptHistory.findById(id);
555
+ if (!history) {
556
+ throw new api_1.APIError({ status: 404, title: "History not found" });
557
+ }
558
+ if (history.userId.toString() !== userId?.toString()) {
559
+ throw new api_1.APIError({ status: 403, title: "Not authorized to access this history" });
560
+ }
561
+ if (promptIndex >= history.prompts.length) {
562
+ throw new api_1.APIError({ status: 400, title: "promptIndex out of range" });
563
+ }
564
+ if (rating === null) {
565
+ history.prompts[promptIndex].rating = undefined;
566
+ }
567
+ else {
568
+ history.prompts[promptIndex].rating = rating;
569
+ }
570
+ history.markModified("prompts");
571
+ await history.save();
572
+ return res.json({ data: { promptIndex, rating: history.prompts[promptIndex].rating ?? null } });
573
+ }));
762
574
  router.post("/gpt/remix", [
763
575
  (0, api_1.authenticateMiddleware)(),
764
- (0, api_1.createOpenApiBuilder)((_c = options.openApiOptions) !== null && _c !== void 0 ? _c : {})
576
+ (0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
765
577
  .withTags(["gpt"])
766
578
  .withSummary("Remix text")
767
579
  .withRequestBody({
@@ -769,31 +581,22 @@ var addGptRoutes = function (router, options) {
769
581
  })
770
582
  .withResponse(200, { data: { type: "string" } })
771
583
  .build(),
772
- ], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
773
- var text, userId, aiService, result;
774
- var _a;
775
- return __generator(this, function (_b) {
776
- switch (_b.label) {
777
- case 0:
778
- text = req.body.text;
779
- userId = (_a = req.user) === null || _a === void 0 ? void 0 : _a._id;
780
- if (!text || typeof text !== "string") {
781
- throw new api_1.APIError({ status: 400, title: "text is required" });
782
- }
783
- aiService = resolveAiService(req, options);
784
- if (!aiService) {
785
- return [2 /*return*/, res.json({ data: DEMO_RESPONSE })];
786
- }
787
- return [4 /*yield*/, aiService.generateRemix({ text: text, userId: userId })];
788
- case 1:
789
- result = _b.sent();
790
- return [2 /*return*/, res.json({ data: result })];
791
- }
792
- });
793
- }); }));
584
+ ], (0, api_1.asyncHandler)(async (req, res) => {
585
+ const { text } = req.body;
586
+ const userId = req.user?._id;
587
+ if (!text || typeof text !== "string") {
588
+ throw new api_1.APIError({ status: 400, title: "text is required" });
589
+ }
590
+ const aiService = resolveAiService(req, options);
591
+ if (!aiService) {
592
+ return res.json({ data: DEMO_RESPONSE });
593
+ }
594
+ const result = await aiService.generateRemix({ text, userId });
595
+ return res.json({ data: result });
596
+ }));
794
597
  router.get("/gpt/tools", [
795
598
  (0, api_1.authenticateMiddleware)(),
796
- (0, api_1.createOpenApiBuilder)((_d = options.openApiOptions) !== null && _d !== void 0 ? _d : {})
599
+ (0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
797
600
  .withTags(["gpt"])
798
601
  .withSummary("List available AI tools")
799
602
  .withResponse(200, {
@@ -810,86 +613,46 @@ var addGptRoutes = function (router, options) {
810
613
  },
811
614
  })
812
615
  .build(),
813
- ], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
814
- var tools, _a, _b, _c, name_1, t, requestTools, _d, _e, _f, name_2, t, mcpTools, _g, _h, _j, name_3, t, _k;
815
- var e_4, _l, e_5, _m, e_6, _o;
816
- var _p, _q, _r;
817
- return __generator(this, function (_s) {
818
- switch (_s.label) {
819
- case 0:
820
- tools = [];
821
- // Static tools from route config
822
- if (routeTools) {
823
- try {
824
- for (_a = __values(Object.entries(routeTools)), _b = _a.next(); !_b.done; _b = _a.next()) {
825
- _c = __read(_b.value, 2), name_1 = _c[0], t = _c[1];
826
- tools.push({
827
- description: (_p = t.description) !== null && _p !== void 0 ? _p : "",
828
- name: name_1,
829
- source: "builtin",
830
- });
831
- }
832
- }
833
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
834
- finally {
835
- try {
836
- if (_b && !_b.done && (_l = _a.return)) _l.call(_a);
837
- }
838
- finally { if (e_4) throw e_4.error; }
839
- }
840
- }
841
- // Per-request tools
842
- if (createRequestTools) {
843
- requestTools = createRequestTools(req);
844
- try {
845
- for (_d = __values(Object.entries(requestTools)), _e = _d.next(); !_e.done; _e = _d.next()) {
846
- _f = __read(_e.value, 2), name_2 = _f[0], t = _f[1];
847
- tools.push({
848
- description: (_q = t.description) !== null && _q !== void 0 ? _q : "",
849
- name: name_2,
850
- source: "builtin",
851
- });
852
- }
853
- }
854
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
855
- finally {
856
- try {
857
- if (_e && !_e.done && (_m = _d.return)) _m.call(_d);
858
- }
859
- finally { if (e_5) throw e_5.error; }
860
- }
861
- }
862
- if (!mcpService) return [3 /*break*/, 4];
863
- _s.label = 1;
864
- case 1:
865
- _s.trys.push([1, 3, , 4]);
866
- return [4 /*yield*/, mcpService.getTools()];
867
- case 2:
868
- mcpTools = _s.sent();
869
- try {
870
- for (_g = __values(Object.entries(mcpTools)), _h = _g.next(); !_h.done; _h = _g.next()) {
871
- _j = __read(_h.value, 2), name_3 = _j[0], t = _j[1];
872
- tools.push({
873
- description: (_r = t.description) !== null && _r !== void 0 ? _r : "",
874
- name: name_3,
875
- source: "mcp",
876
- });
877
- }
878
- }
879
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
880
- finally {
881
- try {
882
- if (_h && !_h.done && (_o = _g.return)) _o.call(_g);
883
- }
884
- finally { if (e_6) throw e_6.error; }
885
- }
886
- return [3 /*break*/, 4];
887
- case 3:
888
- _k = _s.sent();
889
- return [3 /*break*/, 4];
890
- case 4: return [2 /*return*/, res.json({ data: tools })];
616
+ ], (0, api_1.asyncHandler)(async (req, res) => {
617
+ const tools = [];
618
+ // Static tools from route config
619
+ if (routeTools) {
620
+ for (const [name, t] of Object.entries(routeTools)) {
621
+ tools.push({
622
+ description: t.description ?? "",
623
+ name,
624
+ source: "builtin",
625
+ });
891
626
  }
892
- });
893
- }); }));
627
+ }
628
+ // Per-request tools
629
+ if (createRequestTools) {
630
+ const requestTools = createRequestTools(req);
631
+ for (const [name, t] of Object.entries(requestTools)) {
632
+ tools.push({
633
+ description: t.description ?? "",
634
+ name,
635
+ source: "builtin",
636
+ });
637
+ }
638
+ }
639
+ // MCP tools
640
+ if (mcpService) {
641
+ try {
642
+ const mcpTools = await mcpService.getTools();
643
+ for (const [name, t] of Object.entries(mcpTools)) {
644
+ tools.push({
645
+ description: t.description ?? "",
646
+ name,
647
+ source: "mcp",
648
+ });
649
+ }
650
+ }
651
+ catch {
652
+ // MCP tool discovery failure should not break the request
653
+ }
654
+ }
655
+ return res.json({ data: tools });
656
+ }));
894
657
  };
895
658
  exports.addGptRoutes = addGptRoutes;