@terreno/ai 57.4.0 → 57.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/aiAdminApp.js +9 -10
- package/dist/aiApp.js +31 -31
- package/dist/langfuseApp.js +27 -76
- package/dist/langfuseCache.js +20 -79
- package/dist/langfuseClient.js +13 -58
- package/dist/langfusePrompts.js +70 -173
- package/dist/langfuseRoutesEvaluations.js +26 -73
- package/dist/langfuseRoutesMiddleware.js +4 -4
- package/dist/langfuseRoutesPlayground.js +29 -99
- package/dist/langfuseRoutesPrompts.js +48 -121
- package/dist/langfuseRoutesTraces.js +24 -76
- package/dist/langfuseTracing.js +15 -60
- package/dist/langfuseVercelAi.js +38 -87
- package/dist/models/aiRequest.js +19 -72
- package/dist/models/fileAttachment.js +3 -3
- package/dist/models/gptHistory.js +5 -5
- package/dist/models/project.js +4 -4
- package/dist/routes/aiRequestsExplorer.js +89 -150
- package/dist/routes/files.js +50 -115
- package/dist/routes/gpt.js +457 -694
- package/dist/routes/gptHistories.js +17 -19
- package/dist/routes/mcp.js +30 -102
- package/dist/routes/projects.js +53 -116
- package/dist/service/aiService.js +393 -706
- package/dist/service/fileStorage.js +42 -134
- package/dist/service/gemini.js +55 -130
- package/dist/service/getMCPTools.js +17 -86
- package/dist/service/mcpService.js +87 -258
- package/dist/service/parseAiJson.js +32 -32
- package/dist/service/vertex.js +129 -283
- package/package.json +7 -7
- package/tsconfig.json +6 -21
package/dist/routes/gpt.js
CHANGED
|
@@ -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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
-
|
|
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(
|
|
121
|
-
res.write(
|
|
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
|
-
|
|
132
|
-
|
|
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
|
-
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
|
|
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
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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)(
|
|
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
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
if (
|
|
260
|
-
|
|
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(
|
|
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
|
-
?
|
|
167
|
+
? `${projectContext}\n\n${effectiveSystemPrompt}`
|
|
283
168
|
: projectContext;
|
|
284
169
|
}
|
|
285
170
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
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
|
-
?
|
|
186
|
+
? `${langfusePrompt}\n\n${effectiveSystemPrompt}`
|
|
305
187
|
: langfusePrompt;
|
|
306
188
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
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
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
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
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
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",
|
|
435
|
-
|
|
436
|
-
:
|
|
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
|
-
|
|
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
|
-
|
|
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(
|
|
322
|
+
res.write(`data: ${JSON.stringify({ text: cleaned })}\n\n`);
|
|
453
323
|
}
|
|
454
324
|
}
|
|
455
325
|
stepTextBuffer = "";
|
|
456
326
|
stepHasToolCall = false;
|
|
457
|
-
|
|
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(
|
|
332
|
+
: String(part.error ?? "Unknown stream error");
|
|
463
333
|
api_1.logger.error("AI stream error part", { error: errMsg });
|
|
464
|
-
res.write(
|
|
465
|
-
|
|
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 =
|
|
339
|
+
const dataUrl = `data:${part.file.mediaType};base64,${part.file.base64}`;
|
|
470
340
|
generatedImages.push({ mimeType: part.file.mediaType, url: dataUrl });
|
|
471
|
-
res.write(
|
|
341
|
+
res.write(`data: ${JSON.stringify({
|
|
472
342
|
image: { mimeType: part.file.mediaType, url: dataUrl },
|
|
473
|
-
})
|
|
343
|
+
})}\n\n`);
|
|
474
344
|
api_1.logger.debug("Sent inline image from stream");
|
|
475
345
|
}
|
|
476
|
-
|
|
346
|
+
continue;
|
|
477
347
|
}
|
|
478
348
|
if (part.type === "text-delta") {
|
|
479
|
-
textChunk = (
|
|
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(
|
|
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
|
-
})
|
|
362
|
+
})}\n\n`);
|
|
493
363
|
// Persist tool call in history
|
|
494
|
-
|
|
364
|
+
history.prompts.push({
|
|
495
365
|
args: part.input,
|
|
496
|
-
text:
|
|
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 (
|
|
506
|
-
res.write(
|
|
375
|
+
if (toolResult?.fileData && typeof toolResult.fileData === "string") {
|
|
376
|
+
res.write(`data: ${JSON.stringify({
|
|
507
377
|
file: {
|
|
508
|
-
filename:
|
|
509
|
-
mimeType:
|
|
378
|
+
filename: toolResult.filename ?? "document",
|
|
379
|
+
mimeType: toolResult.mimeType ?? "application/octet-stream",
|
|
510
380
|
url: toolResult.fileData,
|
|
511
381
|
},
|
|
512
|
-
})
|
|
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
|
-
|
|
519
|
-
|
|
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
|
-
})
|
|
396
|
+
})}\n\n`);
|
|
526
397
|
// Persist tool result in history (without the large file data)
|
|
527
|
-
|
|
398
|
+
history.prompts.push({
|
|
528
399
|
result: cleanResult,
|
|
529
|
-
text:
|
|
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
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
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
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
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
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
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
|
-
|
|
595
|
-
|
|
596
|
-
fileErr_1 = _u.sent();
|
|
431
|
+
}
|
|
432
|
+
catch (fileErr) {
|
|
597
433
|
api_1.logger.debug("No files in response", {
|
|
598
|
-
error:
|
|
434
|
+
error: fileErr instanceof Error ? fileErr.message : String(fileErr),
|
|
599
435
|
});
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
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
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
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
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
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
|
-
|
|
661
|
-
|
|
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:
|
|
504
|
+
error: logErr instanceof Error ? logErr.message : String(logErr),
|
|
678
505
|
});
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
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)(
|
|
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)(
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
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)(
|
|
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)(
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
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)(
|
|
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)(
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
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;
|