@terreno/ai 0.3.2 → 0.4.2
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 +5 -0
- package/dist/aiApp.d.ts +2 -0
- package/dist/aiApp.js +4 -1
- package/dist/models/aiRequest.js +68 -9
- package/dist/models/aiRequest.test.js +7 -5
- package/dist/models/gptHistory.js +6 -8
- package/dist/models/gptHistory.test.js +4 -22
- package/dist/models/index.d.ts +1 -0
- package/dist/models/index.js +3 -1
- package/dist/models/project.d.ts +2 -0
- package/dist/models/project.js +47 -0
- package/dist/routes/files.js +2 -2
- package/dist/routes/gpt.js +368 -128
- package/dist/routes/gptHistories.js +1 -1
- package/dist/routes/index.d.ts +1 -0
- package/dist/routes/index.js +3 -1
- package/dist/routes/projects.d.ts +3 -0
- package/dist/routes/projects.js +147 -0
- package/dist/service/aiService.js +21 -5
- package/dist/service/fileStorage.d.ts +3 -1
- package/dist/service/fileStorage.js +2 -2
- package/dist/service/index.d.ts +2 -1
- package/dist/service/index.js +2 -1
- package/dist/service/prompts.d.ts +1 -0
- package/dist/service/prompts.js +3 -1
- package/dist/service/webSearchTool.d.ts +39 -0
- package/dist/service/webSearchTool.js +2 -0
- package/dist/types/index.d.ts +39 -2
- package/dist/types/index.js +7 -2
- package/package.json +5 -5
- package/src/aiApp.ts +6 -0
- package/src/models/aiRequest.test.ts +8 -8
- package/src/models/aiRequest.ts +68 -10
- package/src/models/gptHistory.test.ts +4 -16
- package/src/models/gptHistory.ts +6 -9
- package/src/models/index.ts +1 -0
- package/src/models/project.ts +54 -0
- package/src/routes/files.ts +4 -4
- package/src/routes/gpt.ts +493 -295
- package/src/routes/gptHistories.ts +1 -1
- package/src/routes/index.ts +1 -0
- package/src/routes/mcp.ts +1 -1
- package/src/routes/projects.ts +115 -0
- package/src/service/aiService.ts +22 -6
- package/src/service/fileStorage.ts +3 -3
- package/src/service/index.ts +2 -0
- package/src/service/prompts.ts +4 -0
- package/src/service/webSearchTool.ts +40 -0
- package/src/types/index.ts +54 -2
package/src/routes/gpt.ts
CHANGED
|
@@ -12,7 +12,9 @@ import type mongoose from "mongoose";
|
|
|
12
12
|
|
|
13
13
|
import {AIRequest} from "../models/aiRequest";
|
|
14
14
|
import {GptHistory} from "../models/gptHistory";
|
|
15
|
+
import {Project} from "../models/project";
|
|
15
16
|
import {AIService} from "../service/aiService";
|
|
17
|
+
import {TITLE_GENERATION_PROMPT} from "../service/prompts";
|
|
16
18
|
import type {GptHistoryPrompt, GptRouteOptions, MessageContentPart} from "../types";
|
|
17
19
|
|
|
18
20
|
const DEMO_RESPONSE =
|
|
@@ -46,6 +48,33 @@ const resolveAiService = (
|
|
|
46
48
|
return options.aiService;
|
|
47
49
|
};
|
|
48
50
|
|
|
51
|
+
/** Generate a short title for a conversation using a cheap model call. */
|
|
52
|
+
const generateTitle = async (
|
|
53
|
+
prompt: string,
|
|
54
|
+
response: string,
|
|
55
|
+
aiService: AIService,
|
|
56
|
+
options: GptRouteOptions,
|
|
57
|
+
perRequestApiKey?: string
|
|
58
|
+
): Promise<string | undefined> => {
|
|
59
|
+
try {
|
|
60
|
+
let titleService = aiService;
|
|
61
|
+
if (options.titleModelId && options.createModelFn && perRequestApiKey) {
|
|
62
|
+
titleService = new AIService({
|
|
63
|
+
model: options.createModelFn(perRequestApiKey, options.titleModelId),
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
const conversationSnippet = `User: ${prompt}\nAssistant: ${response.substring(0, 500)}`;
|
|
67
|
+
const title = await titleService.generateText({
|
|
68
|
+
prompt: conversationSnippet,
|
|
69
|
+
systemPrompt: TITLE_GENERATION_PROMPT,
|
|
70
|
+
temperature: 0.3,
|
|
71
|
+
});
|
|
72
|
+
return title.trim().replace(/^["']|["']$/g, "");
|
|
73
|
+
} catch {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
49
78
|
export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
50
79
|
const {mcpService, tools: routeTools, createRequestTools, toolChoice, maxSteps} = options;
|
|
51
80
|
|
|
@@ -71,353 +100,454 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
71
100
|
},
|
|
72
101
|
historyId: {type: "string"},
|
|
73
102
|
model: {type: "string"},
|
|
103
|
+
projectId: {type: "string"},
|
|
74
104
|
prompt: {type: "string"},
|
|
75
105
|
systemPrompt: {type: "string"},
|
|
76
106
|
})
|
|
77
107
|
.withResponse(200, {data: {type: "string"}})
|
|
78
108
|
.build(),
|
|
79
109
|
],
|
|
80
|
-
asyncHandler
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
110
|
+
// Use a raw handler instead of asyncHandler so we can control error responses
|
|
111
|
+
// for both pre-stream (JSON) and mid-stream (SSE) errors.
|
|
112
|
+
async (req: express.Request, res: express.Response) => {
|
|
113
|
+
let sseStarted = false;
|
|
114
|
+
try {
|
|
115
|
+
const {
|
|
116
|
+
prompt,
|
|
117
|
+
historyId,
|
|
118
|
+
systemPrompt,
|
|
119
|
+
attachments,
|
|
120
|
+
model: requestModel,
|
|
121
|
+
projectId,
|
|
122
|
+
} = req.body;
|
|
123
|
+
const userId = (req as any).user?._id as mongoose.Types.ObjectId | undefined;
|
|
124
|
+
|
|
125
|
+
if (!prompt || typeof prompt !== "string") {
|
|
126
|
+
throw new APIError({status: 400, title: "prompt is required"});
|
|
127
|
+
}
|
|
97
128
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
129
|
+
// Resolve AI service (per-request key takes priority, then configured service)
|
|
130
|
+
const hasPerRequestKey = !!req.headers["x-ai-api-key"];
|
|
131
|
+
const hasCreateModelFn = !!options.createModelFn;
|
|
132
|
+
const hasConfiguredService = !!options.aiService;
|
|
133
|
+
logger.debug("Resolving AI service", {
|
|
134
|
+
hasConfiguredService,
|
|
135
|
+
hasCreateModelFn,
|
|
136
|
+
hasPerRequestKey,
|
|
137
|
+
});
|
|
103
138
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (!history) {
|
|
109
|
-
throw new APIError({status: 404, title: "History not found"});
|
|
110
|
-
}
|
|
111
|
-
if (history.userId.toString() !== userId?.toString()) {
|
|
112
|
-
throw new APIError({status: 403, title: "Not authorized to access this history"});
|
|
139
|
+
const aiService = resolveAiService(req, options, requestModel);
|
|
140
|
+
if (!aiService) {
|
|
141
|
+
logger.debug("No AI service available, sending demo response");
|
|
142
|
+
return sendDemoResponse(res);
|
|
113
143
|
}
|
|
114
|
-
} else {
|
|
115
|
-
history = new GptHistory({prompts: [], userId});
|
|
116
|
-
}
|
|
117
144
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
});
|
|
128
|
-
} else if (attachment.type === "file") {
|
|
129
|
-
contentParts.push({
|
|
130
|
-
filename: attachment.filename,
|
|
131
|
-
mimeType: attachment.mimeType,
|
|
132
|
-
type: "file",
|
|
133
|
-
url: attachment.url,
|
|
134
|
-
});
|
|
145
|
+
// Load or create history
|
|
146
|
+
let history;
|
|
147
|
+
if (historyId) {
|
|
148
|
+
history = await GptHistory.findById(historyId);
|
|
149
|
+
if (!history) {
|
|
150
|
+
throw new APIError({status: 404, title: "History not found"});
|
|
151
|
+
}
|
|
152
|
+
if (history.userId.toString() !== userId?.toString()) {
|
|
153
|
+
throw new APIError({status: 403, title: "Not authorized to access this history"});
|
|
135
154
|
}
|
|
155
|
+
} else {
|
|
156
|
+
history = new GptHistory({prompts: [], userId, ...(projectId ? {projectId} : {})});
|
|
136
157
|
}
|
|
137
|
-
}
|
|
138
158
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
// Build messages from history using AIService helper
|
|
149
|
-
const messages = aiService.buildMessages(history.prompts);
|
|
150
|
-
|
|
151
|
-
// Some models (e.g. gemini-2.5-flash-image) don't support tool calling
|
|
152
|
-
const modelId = aiService.modelId;
|
|
153
|
-
const supportsTools = !modelId?.includes("image");
|
|
154
|
-
|
|
155
|
-
// Merge tools from route config, per-request tools, and MCP service
|
|
156
|
-
let allTools: Record<string, Tool> | undefined;
|
|
157
|
-
const requestTools = createRequestTools ? createRequestTools(req) : undefined;
|
|
158
|
-
if (supportsTools && (routeTools || requestTools || mcpService)) {
|
|
159
|
-
allTools = {...(routeTools ?? {}), ...(requestTools ?? {})};
|
|
160
|
-
if (mcpService) {
|
|
159
|
+
// If history doesn't have a projectId yet but one was provided, associate it
|
|
160
|
+
if (projectId && !history.projectId) {
|
|
161
|
+
history.projectId = projectId;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Load project context if a projectId is provided (or inherited from history)
|
|
165
|
+
const effectiveProjectId = projectId ?? history.projectId;
|
|
166
|
+
let effectiveSystemPrompt = systemPrompt;
|
|
167
|
+
if (effectiveProjectId) {
|
|
161
168
|
try {
|
|
162
|
-
const
|
|
163
|
-
|
|
169
|
+
const project = await Project.findById(effectiveProjectId);
|
|
170
|
+
if (project && project.userId.toString() === userId?.toString()) {
|
|
171
|
+
const parts: string[] = [];
|
|
172
|
+
if (project.systemContext) {
|
|
173
|
+
parts.push(project.systemContext);
|
|
174
|
+
}
|
|
175
|
+
if (project.memories.length > 0) {
|
|
176
|
+
parts.push(
|
|
177
|
+
`## Relevant Memories\n${project.memories.map((m) => `- ${m.text}`).join("\n")}`
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
if (parts.length > 0) {
|
|
181
|
+
const projectContext = parts.join("\n\n");
|
|
182
|
+
effectiveSystemPrompt = effectiveSystemPrompt
|
|
183
|
+
? `${projectContext}\n\n${effectiveSystemPrompt}`
|
|
184
|
+
: projectContext;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
164
187
|
} catch {
|
|
165
|
-
//
|
|
188
|
+
// Project loading failure should not block the request
|
|
166
189
|
}
|
|
167
190
|
}
|
|
168
|
-
}
|
|
169
191
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
192
|
+
// Build content parts from attachments
|
|
193
|
+
const contentParts: MessageContentPart[] = [{text: prompt, type: "text"}];
|
|
194
|
+
if (attachments && Array.isArray(attachments)) {
|
|
195
|
+
logger.debug("Processing attachments", {
|
|
196
|
+
count: attachments.length,
|
|
197
|
+
types: attachments.map((a: any) => ({
|
|
198
|
+
mimeType: a.mimeType,
|
|
199
|
+
type: a.type,
|
|
200
|
+
urlLength: a.url?.length ?? 0,
|
|
201
|
+
})),
|
|
202
|
+
});
|
|
203
|
+
for (const attachment of attachments) {
|
|
204
|
+
if (attachment.type === "image") {
|
|
205
|
+
contentParts.push({
|
|
206
|
+
mimeType: attachment.mimeType,
|
|
207
|
+
type: "image",
|
|
208
|
+
url: attachment.url,
|
|
209
|
+
});
|
|
210
|
+
} else if (attachment.type === "file") {
|
|
211
|
+
contentParts.push({
|
|
212
|
+
filename: attachment.filename,
|
|
213
|
+
mimeType: attachment.mimeType,
|
|
214
|
+
type: "file",
|
|
215
|
+
url: attachment.url,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
174
220
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
temperature: (aiService as any).defaultTemperature,
|
|
189
|
-
toolChoice: allTools ? (toolChoice ?? "auto") : undefined,
|
|
190
|
-
tools: allTools,
|
|
221
|
+
// Add user prompt to history
|
|
222
|
+
const hasAttachments = contentParts.length > 1;
|
|
223
|
+
const userPrompt: GptHistoryPrompt = {
|
|
224
|
+
text: prompt,
|
|
225
|
+
type: "user",
|
|
226
|
+
...(hasAttachments ? {content: contentParts} : {}),
|
|
227
|
+
};
|
|
228
|
+
history.prompts.push(userPrompt);
|
|
229
|
+
|
|
230
|
+
// Build messages from history using AIService helper
|
|
231
|
+
logger.debug("Building messages", {
|
|
232
|
+
attachmentCount: contentParts.length - 1,
|
|
233
|
+
historyLength: history.prompts.length,
|
|
191
234
|
});
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
235
|
+
const messages = aiService.buildMessages(history.prompts);
|
|
236
|
+
logger.debug("Messages built", {messageCount: messages.length});
|
|
237
|
+
|
|
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
|
+
|
|
242
|
+
// Merge tools from route config, per-request tools, and MCP service
|
|
243
|
+
let allTools: Record<string, Tool> | undefined;
|
|
244
|
+
const requestTools = createRequestTools ? createRequestTools(req) : undefined;
|
|
245
|
+
if (supportsTools && (routeTools || requestTools || mcpService)) {
|
|
246
|
+
allTools = {...(routeTools ?? {}), ...(requestTools ?? {})};
|
|
247
|
+
if (mcpService) {
|
|
248
|
+
try {
|
|
249
|
+
const mcpTools = await mcpService.getTools();
|
|
250
|
+
Object.assign(allTools, mcpTools);
|
|
251
|
+
} catch {
|
|
252
|
+
// MCP tool discovery failure should not block the request
|
|
253
|
+
}
|
|
210
254
|
}
|
|
255
|
+
}
|
|
211
256
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
257
|
+
// Stream response via SSE
|
|
258
|
+
res.setHeader("Content-Type", "text/event-stream");
|
|
259
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
260
|
+
res.setHeader("Connection", "keep-alive");
|
|
261
|
+
sseStarted = true;
|
|
262
|
+
|
|
263
|
+
let fullResponse = "";
|
|
264
|
+
const generatedImages: Array<{mimeType: string; url: string}> = [];
|
|
265
|
+
const startTime = Date.now();
|
|
266
|
+
try {
|
|
267
|
+
logger.debug("Starting streamText", {model: modelId, supportsTools});
|
|
268
|
+
const result = streamText({
|
|
269
|
+
messages,
|
|
270
|
+
model: (aiService as any).model,
|
|
271
|
+
providerOptions: !supportsTools
|
|
272
|
+
? {google: {responseModalities: ["TEXT", "IMAGE"]}}
|
|
273
|
+
: undefined,
|
|
274
|
+
stopWhen: allTools ? stepCountIs(maxSteps ?? 5) : stepCountIs(1),
|
|
275
|
+
system: effectiveSystemPrompt ?? undefined,
|
|
276
|
+
temperature: (aiService as any).defaultTemperature,
|
|
277
|
+
toolChoice: allTools ? (toolChoice ?? "auto") : undefined,
|
|
278
|
+
tools: allTools,
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
let partCount = 0;
|
|
282
|
+
// Buffer text per step so we can discard reasoning text when a tool call follows
|
|
283
|
+
let stepTextBuffer = "";
|
|
284
|
+
let stepHasToolCall = false;
|
|
285
|
+
|
|
286
|
+
for await (const part of result.fullStream as AsyncIterable<{
|
|
287
|
+
type: string;
|
|
288
|
+
[key: string]: any;
|
|
289
|
+
}>) {
|
|
290
|
+
partCount++;
|
|
291
|
+
if (partCount <= 5 || part.type === "error" || part.type === "file") {
|
|
292
|
+
logger.debug("Stream part", {
|
|
293
|
+
partCount,
|
|
294
|
+
type: part.type,
|
|
295
|
+
...(part.type === "file" ? {mediaType: part.mediaType} : {}),
|
|
296
|
+
...(part.type === "error" ? {error: String(part.error ?? part)} : {}),
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Track step boundaries to discard reasoning text from tool-call steps
|
|
301
|
+
if (part.type === "start-step") {
|
|
302
|
+
stepTextBuffer = "";
|
|
303
|
+
stepHasToolCall = false;
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
if (part.type === "finish-step") {
|
|
307
|
+
// Only emit buffered text if no tool call happened in this step
|
|
308
|
+
if (!stepHasToolCall && stepTextBuffer) {
|
|
309
|
+
// Strip model reasoning that leaks as JSON action blobs
|
|
310
|
+
const cleaned = stepTextBuffer
|
|
311
|
+
.replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "")
|
|
312
|
+
.trim();
|
|
313
|
+
if (cleaned) {
|
|
314
|
+
fullResponse += cleaned;
|
|
315
|
+
res.write(`data: ${JSON.stringify({text: cleaned})}\n\n`);
|
|
316
|
+
}
|
|
228
317
|
}
|
|
318
|
+
stepTextBuffer = "";
|
|
319
|
+
stepHasToolCall = false;
|
|
320
|
+
continue;
|
|
229
321
|
}
|
|
230
|
-
stepTextBuffer = "";
|
|
231
|
-
stepHasToolCall = false;
|
|
232
|
-
continue;
|
|
233
|
-
}
|
|
234
322
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
244
|
-
if (part.type === "file") {
|
|
245
|
-
const mediaType = part.mediaType as string | undefined;
|
|
246
|
-
if (mediaType?.startsWith("image/")) {
|
|
247
|
-
const dataUrl = `data:${mediaType};base64,${part.base64}`;
|
|
248
|
-
generatedImages.push({mimeType: mediaType, url: dataUrl});
|
|
249
|
-
res.write(
|
|
250
|
-
`data: ${JSON.stringify({image: {mimeType: mediaType, url: dataUrl}})}\n\n`
|
|
251
|
-
);
|
|
252
|
-
logger.debug("Sent inline image from stream");
|
|
323
|
+
if (part.type === "error") {
|
|
324
|
+
const errMsg =
|
|
325
|
+
part.error instanceof Error
|
|
326
|
+
? part.error.message
|
|
327
|
+
: String(part.error ?? "Unknown stream error");
|
|
328
|
+
logger.error("AI stream error part", {error: errMsg});
|
|
329
|
+
res.write(`data: ${JSON.stringify({error: errMsg})}\n\n`);
|
|
330
|
+
continue;
|
|
253
331
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
332
|
+
if (part.type === "file") {
|
|
333
|
+
const mediaType = part.mediaType as string | undefined;
|
|
334
|
+
if (mediaType?.startsWith("image/")) {
|
|
335
|
+
const dataUrl = `data:${mediaType};base64,${part.base64}`;
|
|
336
|
+
generatedImages.push({mimeType: mediaType, url: dataUrl});
|
|
337
|
+
res.write(
|
|
338
|
+
`data: ${JSON.stringify({image: {mimeType: mediaType, url: dataUrl}})}\n\n`
|
|
339
|
+
);
|
|
340
|
+
logger.debug("Sent inline image from stream");
|
|
341
|
+
}
|
|
342
|
+
continue;
|
|
260
343
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
344
|
+
if (part.type === "text-delta") {
|
|
345
|
+
const textChunk = (part.text ?? "") as string;
|
|
346
|
+
if (textChunk) {
|
|
347
|
+
stepTextBuffer += textChunk;
|
|
348
|
+
}
|
|
349
|
+
} else if (part.type === "tool-call") {
|
|
350
|
+
stepHasToolCall = true;
|
|
351
|
+
res.write(
|
|
352
|
+
`data: ${JSON.stringify({
|
|
353
|
+
toolCall: {
|
|
354
|
+
args: part.input,
|
|
355
|
+
toolCallId: part.toolCallId,
|
|
356
|
+
toolName: part.toolName,
|
|
357
|
+
},
|
|
358
|
+
})}\n\n`
|
|
359
|
+
);
|
|
360
|
+
// Persist tool call in history
|
|
361
|
+
history.prompts.push({
|
|
362
|
+
args: part.input as Record<string, unknown>,
|
|
363
|
+
text: `Tool call: ${part.toolName}`,
|
|
364
|
+
toolCallId: part.toolCallId,
|
|
365
|
+
toolName: part.toolName,
|
|
366
|
+
type: "tool-call",
|
|
367
|
+
});
|
|
368
|
+
} else if (part.type === "tool-result") {
|
|
369
|
+
const toolResult = part.output as Record<string, unknown> | undefined;
|
|
370
|
+
|
|
371
|
+
// If the tool result contains file data, send it as a separate file SSE event
|
|
372
|
+
if (toolResult?.fileData && typeof toolResult.fileData === "string") {
|
|
373
|
+
res.write(
|
|
374
|
+
`data: ${JSON.stringify({
|
|
375
|
+
file: {
|
|
376
|
+
filename: toolResult.filename ?? "document",
|
|
377
|
+
mimeType: toolResult.mimeType ?? "application/octet-stream",
|
|
378
|
+
url: toolResult.fileData,
|
|
379
|
+
},
|
|
380
|
+
})}\n\n`
|
|
381
|
+
);
|
|
382
|
+
logger.debug("Sent generated file from tool result", {
|
|
383
|
+
filename: toolResult.filename,
|
|
384
|
+
mimeType: toolResult.mimeType,
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Strip fileData from result before sending/storing to avoid bloating the SSE and DB
|
|
389
|
+
const {fileData: _fileData, ...cleanResult} = toolResult ?? {};
|
|
282
390
|
|
|
283
|
-
// If the tool result contains file data, send it as a separate file SSE event
|
|
284
|
-
if (toolResult?.fileData && typeof toolResult.fileData === "string") {
|
|
285
391
|
res.write(
|
|
286
392
|
`data: ${JSON.stringify({
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
393
|
+
toolResult: {
|
|
394
|
+
result: cleanResult,
|
|
395
|
+
toolCallId: part.toolCallId,
|
|
396
|
+
toolName: part.toolName,
|
|
291
397
|
},
|
|
292
398
|
})}\n\n`
|
|
293
399
|
);
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
400
|
+
// Persist tool result in history (without the large file data)
|
|
401
|
+
history.prompts.push({
|
|
402
|
+
result: cleanResult as unknown,
|
|
403
|
+
text: `Tool result: ${part.toolName}`,
|
|
404
|
+
toolCallId: part.toolCallId,
|
|
405
|
+
toolName: part.toolName,
|
|
406
|
+
type: "tool-result",
|
|
297
407
|
});
|
|
298
408
|
}
|
|
299
|
-
|
|
300
|
-
// Strip fileData from result before sending/storing to avoid bloating the SSE and DB
|
|
301
|
-
const {fileData: _fileData, ...cleanResult} = toolResult ?? {};
|
|
302
|
-
|
|
303
|
-
res.write(
|
|
304
|
-
`data: ${JSON.stringify({
|
|
305
|
-
toolResult: {
|
|
306
|
-
result: cleanResult,
|
|
307
|
-
toolCallId: part.toolCallId,
|
|
308
|
-
toolName: part.toolName,
|
|
309
|
-
},
|
|
310
|
-
})}\n\n`
|
|
311
|
-
);
|
|
312
|
-
// Persist tool result in history (without the large file data)
|
|
313
|
-
history.prompts.push({
|
|
314
|
-
result: cleanResult as unknown,
|
|
315
|
-
text: `Tool result: ${part.toolName}`,
|
|
316
|
-
toolCallId: part.toolCallId,
|
|
317
|
-
toolName: part.toolName,
|
|
318
|
-
type: "tool-result",
|
|
319
|
-
});
|
|
320
409
|
}
|
|
321
|
-
}
|
|
322
410
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
411
|
+
// Flush any remaining buffered text from the last step
|
|
412
|
+
if (!stepHasToolCall && stepTextBuffer) {
|
|
413
|
+
const cleaned = stepTextBuffer.replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "").trim();
|
|
414
|
+
if (cleaned) {
|
|
415
|
+
fullResponse += cleaned;
|
|
416
|
+
res.write(`data: ${JSON.stringify({text: cleaned})}\n\n`);
|
|
417
|
+
}
|
|
329
418
|
}
|
|
330
|
-
}
|
|
331
419
|
|
|
332
|
-
|
|
420
|
+
logger.debug("Stream completed", {fullResponseLength: fullResponse.length, partCount});
|
|
333
421
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
422
|
+
// Check for generated images (e.g. from gemini-2.5-flash-image)
|
|
423
|
+
try {
|
|
424
|
+
const files = await result.files;
|
|
425
|
+
if (files && files.length > 0) {
|
|
426
|
+
for (const file of files) {
|
|
427
|
+
if (file.mediaType.startsWith("image/")) {
|
|
428
|
+
const dataUrl = `data:${file.mediaType};base64,${file.base64}`;
|
|
429
|
+
generatedImages.push({mimeType: file.mediaType, url: dataUrl});
|
|
430
|
+
res.write(
|
|
431
|
+
`data: ${JSON.stringify({
|
|
432
|
+
image: {mimeType: file.mediaType, url: dataUrl},
|
|
433
|
+
})}\n\n`
|
|
434
|
+
);
|
|
435
|
+
}
|
|
347
436
|
}
|
|
437
|
+
logger.debug("Sent generated images", {count: files.length});
|
|
348
438
|
}
|
|
349
|
-
|
|
439
|
+
} catch (fileErr) {
|
|
440
|
+
logger.debug("No files in response", {
|
|
441
|
+
error: fileErr instanceof Error ? fileErr.message : String(fileErr),
|
|
442
|
+
});
|
|
350
443
|
}
|
|
351
|
-
} catch (fileErr) {
|
|
352
|
-
logger.debug("No files in response", {
|
|
353
|
-
error: fileErr instanceof Error ? fileErr.message : String(fileErr),
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
444
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
445
|
+
// Save assistant response to history
|
|
446
|
+
if (fullResponse || generatedImages.length > 0) {
|
|
447
|
+
const contentParts: MessageContentPart[] = generatedImages.map((img) => ({
|
|
448
|
+
mimeType: img.mimeType,
|
|
449
|
+
type: "image" as const,
|
|
450
|
+
url: img.url,
|
|
451
|
+
}));
|
|
452
|
+
const assistantPrompt: GptHistoryPrompt = {
|
|
453
|
+
model: aiService.modelId,
|
|
454
|
+
text: fullResponse || "(image)",
|
|
455
|
+
type: "assistant",
|
|
456
|
+
...(contentParts.length > 0 ? {content: contentParts} : {}),
|
|
457
|
+
};
|
|
458
|
+
history.prompts.push(assistantPrompt);
|
|
459
|
+
}
|
|
460
|
+
await history.save();
|
|
373
461
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
462
|
+
try {
|
|
463
|
+
await AIRequest.logRequest({
|
|
464
|
+
aiModel: modelId ?? "unknown",
|
|
465
|
+
prompt,
|
|
466
|
+
requestType: "general",
|
|
467
|
+
response: fullResponse,
|
|
468
|
+
responseTime: Date.now() - startTime,
|
|
469
|
+
userId: userId ?? undefined,
|
|
470
|
+
});
|
|
471
|
+
} catch (logErr) {
|
|
472
|
+
logger.warn("Failed to log AIRequest", {
|
|
473
|
+
error: logErr instanceof Error ? logErr.message : String(logErr),
|
|
474
|
+
});
|
|
475
|
+
}
|
|
388
476
|
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
477
|
+
// Generate a title for new conversations using a cheap model call
|
|
478
|
+
if (!history.title && fullResponse) {
|
|
479
|
+
const perRequestApiKey = req.headers["x-ai-api-key"] as string | undefined;
|
|
480
|
+
const title = await generateTitle(
|
|
481
|
+
prompt,
|
|
482
|
+
fullResponse,
|
|
483
|
+
aiService,
|
|
484
|
+
options,
|
|
485
|
+
perRequestApiKey
|
|
486
|
+
);
|
|
487
|
+
if (title) {
|
|
488
|
+
history.title = title;
|
|
489
|
+
await history.save();
|
|
490
|
+
}
|
|
491
|
+
}
|
|
399
492
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
493
|
+
logger.debug("Sending done event", {
|
|
494
|
+
fullResponseLength: fullResponse.length,
|
|
495
|
+
historyId: history._id.toString(),
|
|
496
|
+
});
|
|
497
|
+
res.write(
|
|
498
|
+
`data: ${JSON.stringify({
|
|
499
|
+
done: true,
|
|
500
|
+
historyId: history._id.toString(),
|
|
501
|
+
...(history.title ? {title: history.title} : {}),
|
|
502
|
+
})}\n\n`
|
|
503
|
+
);
|
|
504
|
+
res.end();
|
|
505
|
+
} catch (error) {
|
|
506
|
+
logger.error("Error in GPT stream", {
|
|
403
507
|
error: error instanceof Error ? error.message : String(error),
|
|
404
|
-
prompt,
|
|
405
|
-
requestType: "general",
|
|
406
|
-
responseTime: Date.now() - startTime,
|
|
407
|
-
userId: userId ?? undefined,
|
|
408
508
|
});
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
509
|
+
|
|
510
|
+
try {
|
|
511
|
+
await AIRequest.logRequest({
|
|
512
|
+
aiModel: modelId ?? "unknown",
|
|
513
|
+
error: error instanceof Error ? error.message : String(error),
|
|
514
|
+
prompt,
|
|
515
|
+
requestType: "general",
|
|
516
|
+
responseTime: Date.now() - startTime,
|
|
517
|
+
userId: userId ?? undefined,
|
|
518
|
+
});
|
|
519
|
+
} catch (logErr) {
|
|
520
|
+
logger.warn("Failed to log AIRequest error", {
|
|
521
|
+
error: logErr instanceof Error ? logErr.message : String(logErr),
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
res.write(
|
|
526
|
+
`data: ${JSON.stringify({error: error instanceof Error ? error.message : "Unknown error"})}\n\n`
|
|
527
|
+
);
|
|
528
|
+
res.end();
|
|
529
|
+
}
|
|
530
|
+
} catch (outerError) {
|
|
531
|
+
// Catch-all for errors thrown before or after SSE streaming
|
|
532
|
+
const message = outerError instanceof Error ? outerError.message : String(outerError);
|
|
533
|
+
const status = outerError instanceof APIError ? outerError.status : 500;
|
|
534
|
+
const stack = outerError instanceof Error ? outerError.stack : undefined;
|
|
535
|
+
logger.error("GPT prompt handler error", {error: message, stack, status});
|
|
536
|
+
|
|
537
|
+
if (sseStarted) {
|
|
538
|
+
// Already sent SSE headers — send error as SSE event
|
|
539
|
+
res.write(`data: ${JSON.stringify({error: message})}\n\n`);
|
|
540
|
+
res.end();
|
|
541
|
+
} else {
|
|
542
|
+
// Haven't started SSE — send a normal JSON error response
|
|
543
|
+
res.status(status).json({
|
|
544
|
+
detail: message,
|
|
545
|
+
status,
|
|
546
|
+
title: outerError instanceof APIError ? outerError.title : "Internal server error",
|
|
412
547
|
});
|
|
413
548
|
}
|
|
414
|
-
|
|
415
|
-
res.write(
|
|
416
|
-
`data: ${JSON.stringify({error: error instanceof Error ? error.message : "Unknown error"})}\n\n`
|
|
417
|
-
);
|
|
418
|
-
res.end();
|
|
419
549
|
}
|
|
420
|
-
}
|
|
550
|
+
}
|
|
421
551
|
);
|
|
422
552
|
|
|
423
553
|
router.patch(
|
|
@@ -500,4 +630,72 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
500
630
|
return res.json({data: result});
|
|
501
631
|
})
|
|
502
632
|
);
|
|
633
|
+
|
|
634
|
+
router.get(
|
|
635
|
+
"/gpt/tools",
|
|
636
|
+
[
|
|
637
|
+
authenticateMiddleware(),
|
|
638
|
+
createOpenApiBuilder(options.openApiOptions ?? {})
|
|
639
|
+
.withTags(["gpt"])
|
|
640
|
+
.withSummary("List available AI tools")
|
|
641
|
+
.withResponse(200, {
|
|
642
|
+
data: {
|
|
643
|
+
items: {
|
|
644
|
+
properties: {
|
|
645
|
+
description: {type: "string"},
|
|
646
|
+
name: {type: "string"},
|
|
647
|
+
source: {type: "string"},
|
|
648
|
+
},
|
|
649
|
+
type: "object",
|
|
650
|
+
},
|
|
651
|
+
type: "array",
|
|
652
|
+
},
|
|
653
|
+
})
|
|
654
|
+
.build(),
|
|
655
|
+
],
|
|
656
|
+
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
657
|
+
const tools: Array<{name: string; description: string; source: string}> = [];
|
|
658
|
+
|
|
659
|
+
// Static tools from route config
|
|
660
|
+
if (routeTools) {
|
|
661
|
+
for (const [name, t] of Object.entries(routeTools)) {
|
|
662
|
+
tools.push({
|
|
663
|
+
description: (t as {description?: string}).description ?? "",
|
|
664
|
+
name,
|
|
665
|
+
source: "builtin",
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// Per-request tools
|
|
671
|
+
if (createRequestTools) {
|
|
672
|
+
const requestTools = createRequestTools(req);
|
|
673
|
+
for (const [name, t] of Object.entries(requestTools)) {
|
|
674
|
+
tools.push({
|
|
675
|
+
description: (t as {description?: string}).description ?? "",
|
|
676
|
+
name,
|
|
677
|
+
source: "builtin",
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// MCP tools
|
|
683
|
+
if (mcpService) {
|
|
684
|
+
try {
|
|
685
|
+
const mcpTools = await mcpService.getTools();
|
|
686
|
+
for (const [name, t] of Object.entries(mcpTools)) {
|
|
687
|
+
tools.push({
|
|
688
|
+
description: (t as {description?: string}).description ?? "",
|
|
689
|
+
name,
|
|
690
|
+
source: "mcp",
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
} catch {
|
|
694
|
+
// MCP tool discovery failure should not break the request
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
return res.json({data: tools});
|
|
699
|
+
})
|
|
700
|
+
);
|
|
503
701
|
};
|