@terreno/ai 0.3.2 → 0.5.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.
Files changed (49) hide show
  1. package/README.md +5 -0
  2. package/dist/aiApp.d.ts +2 -0
  3. package/dist/aiApp.js +4 -1
  4. package/dist/models/aiRequest.js +68 -9
  5. package/dist/models/aiRequest.test.js +7 -5
  6. package/dist/models/gptHistory.js +6 -8
  7. package/dist/models/gptHistory.test.js +4 -22
  8. package/dist/models/index.d.ts +1 -0
  9. package/dist/models/index.js +3 -1
  10. package/dist/models/project.d.ts +2 -0
  11. package/dist/models/project.js +47 -0
  12. package/dist/routes/files.js +2 -2
  13. package/dist/routes/gpt.js +368 -128
  14. package/dist/routes/gptHistories.js +1 -1
  15. package/dist/routes/index.d.ts +1 -0
  16. package/dist/routes/index.js +3 -1
  17. package/dist/routes/projects.d.ts +3 -0
  18. package/dist/routes/projects.js +147 -0
  19. package/dist/service/aiService.js +21 -5
  20. package/dist/service/fileStorage.d.ts +3 -1
  21. package/dist/service/fileStorage.js +2 -2
  22. package/dist/service/index.d.ts +2 -1
  23. package/dist/service/index.js +2 -1
  24. package/dist/service/prompts.d.ts +1 -0
  25. package/dist/service/prompts.js +3 -1
  26. package/dist/service/webSearchTool.d.ts +39 -0
  27. package/dist/service/webSearchTool.js +2 -0
  28. package/dist/types/index.d.ts +39 -2
  29. package/dist/types/index.js +7 -2
  30. package/package.json +5 -5
  31. package/src/aiApp.ts +6 -0
  32. package/src/models/aiRequest.test.ts +8 -8
  33. package/src/models/aiRequest.ts +68 -10
  34. package/src/models/gptHistory.test.ts +4 -16
  35. package/src/models/gptHistory.ts +6 -9
  36. package/src/models/index.ts +1 -0
  37. package/src/models/project.ts +54 -0
  38. package/src/routes/files.ts +4 -4
  39. package/src/routes/gpt.ts +493 -295
  40. package/src/routes/gptHistories.ts +1 -1
  41. package/src/routes/index.ts +1 -0
  42. package/src/routes/mcp.ts +1 -1
  43. package/src/routes/projects.ts +115 -0
  44. package/src/service/aiService.ts +22 -6
  45. package/src/service/fileStorage.ts +3 -3
  46. package/src/service/index.ts +2 -0
  47. package/src/service/prompts.ts +4 -0
  48. package/src/service/webSearchTool.ts +40 -0
  49. 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(async (req: express.Request, res: express.Response) => {
81
- const {prompt, historyId, systemPrompt, attachments, model: requestModel} = req.body;
82
- const userId = (req as any).user?._id as mongoose.Types.ObjectId | undefined;
83
-
84
- if (!prompt || typeof prompt !== "string") {
85
- throw new APIError({status: 400, title: "prompt is required"});
86
- }
87
-
88
- // Resolve AI service (per-request key takes priority, then configured service)
89
- const hasPerRequestKey = !!req.headers["x-ai-api-key"];
90
- const hasCreateModelFn = !!options.createModelFn;
91
- const hasConfiguredService = !!options.aiService;
92
- logger.debug("Resolving AI service", {
93
- hasConfiguredService,
94
- hasCreateModelFn,
95
- hasPerRequestKey,
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
- const aiService = resolveAiService(req, options, requestModel);
99
- if (!aiService) {
100
- logger.debug("No AI service available, sending demo response");
101
- return sendDemoResponse(res);
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
- // Load or create history
105
- let history;
106
- if (historyId) {
107
- history = await GptHistory.findById(historyId);
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
- // Build content parts from attachments
119
- const contentParts: MessageContentPart[] = [{text: prompt, type: "text"}];
120
- if (attachments && Array.isArray(attachments)) {
121
- for (const attachment of attachments) {
122
- if (attachment.type === "image") {
123
- contentParts.push({
124
- mimeType: attachment.mimeType,
125
- type: "image",
126
- url: attachment.url,
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
- // Add user prompt to history
140
- const hasAttachments = contentParts.length > 1;
141
- const userPrompt: GptHistoryPrompt = {
142
- text: prompt,
143
- type: "user",
144
- ...(hasAttachments ? {content: contentParts} : {}),
145
- };
146
- history.prompts.push(userPrompt);
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 mcpTools = await mcpService.getTools();
163
- Object.assign(allTools, mcpTools);
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
- // MCP tool discovery failure should not block the request
188
+ // Project loading failure should not block the request
166
189
  }
167
190
  }
168
- }
169
191
 
170
- // Stream response via SSE
171
- res.setHeader("Content-Type", "text/event-stream");
172
- res.setHeader("Cache-Control", "no-cache");
173
- res.setHeader("Connection", "keep-alive");
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
- let fullResponse = "";
176
- const generatedImages: Array<{mimeType: string; url: string}> = [];
177
- const startTime = Date.now();
178
- try {
179
- logger.debug("Starting streamText", {model: modelId, supportsTools});
180
- const result = streamText({
181
- messages,
182
- model: (aiService as any).model,
183
- providerOptions: !supportsTools
184
- ? {google: {responseModalities: ["TEXT", "IMAGE"]}}
185
- : undefined,
186
- stopWhen: allTools ? stepCountIs(maxSteps ?? 5) : stepCountIs(1),
187
- system: systemPrompt ?? undefined,
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
- let partCount = 0;
194
- // Buffer text per step so we can discard reasoning text when a tool call follows
195
- let stepTextBuffer = "";
196
- let stepHasToolCall = false;
197
-
198
- for await (const part of result.fullStream as AsyncIterable<{
199
- type: string;
200
- [key: string]: any;
201
- }>) {
202
- partCount++;
203
- if (partCount <= 5 || part.type === "error" || part.type === "file") {
204
- logger.debug("Stream part", {
205
- partCount,
206
- type: part.type,
207
- ...(part.type === "file" ? {mediaType: part.mediaType} : {}),
208
- ...(part.type === "error" ? {error: String(part.error ?? part)} : {}),
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
- // Track step boundaries to discard reasoning text from tool-call steps
213
- if (part.type === "start-step") {
214
- stepTextBuffer = "";
215
- stepHasToolCall = false;
216
- continue;
217
- }
218
- if (part.type === "finish-step") {
219
- // Only emit buffered text if no tool call happened in this step
220
- if (!stepHasToolCall && stepTextBuffer) {
221
- // Strip model reasoning that leaks as JSON action blobs
222
- const cleaned = stepTextBuffer
223
- .replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "")
224
- .trim();
225
- if (cleaned) {
226
- fullResponse += cleaned;
227
- res.write(`data: ${JSON.stringify({text: cleaned})}\n\n`);
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
- if (part.type === "error") {
236
- const errMsg =
237
- part.error instanceof Error
238
- ? part.error.message
239
- : String(part.error ?? "Unknown stream error");
240
- logger.error("AI stream error part", {error: errMsg});
241
- res.write(`data: ${JSON.stringify({error: errMsg})}\n\n`);
242
- continue;
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
- continue;
255
- }
256
- if (part.type === "text-delta") {
257
- const textChunk = (part.text ?? "") as string;
258
- if (textChunk) {
259
- stepTextBuffer += textChunk;
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
- } else if (part.type === "tool-call") {
262
- stepHasToolCall = true;
263
- res.write(
264
- `data: ${JSON.stringify({
265
- toolCall: {
266
- args: part.input,
267
- toolCallId: part.toolCallId,
268
- toolName: part.toolName,
269
- },
270
- })}\n\n`
271
- );
272
- // Persist tool call in history
273
- history.prompts.push({
274
- args: part.input as Record<string, unknown>,
275
- text: `Tool call: ${part.toolName}`,
276
- toolCallId: part.toolCallId,
277
- toolName: part.toolName,
278
- type: "tool-call",
279
- });
280
- } else if (part.type === "tool-result") {
281
- const toolResult = part.output as Record<string, unknown> | undefined;
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
- file: {
288
- filename: toolResult.filename ?? "document",
289
- mimeType: toolResult.mimeType ?? "application/octet-stream",
290
- url: toolResult.fileData,
393
+ toolResult: {
394
+ result: cleanResult,
395
+ toolCallId: part.toolCallId,
396
+ toolName: part.toolName,
291
397
  },
292
398
  })}\n\n`
293
399
  );
294
- logger.debug("Sent generated file from tool result", {
295
- filename: toolResult.filename,
296
- mimeType: toolResult.mimeType,
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
- // Flush any remaining buffered text from the last step
324
- if (!stepHasToolCall && stepTextBuffer) {
325
- const cleaned = stepTextBuffer.replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "").trim();
326
- if (cleaned) {
327
- fullResponse += cleaned;
328
- res.write(`data: ${JSON.stringify({text: cleaned})}\n\n`);
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
- logger.debug("Stream completed", {fullResponseLength: fullResponse.length, partCount});
420
+ logger.debug("Stream completed", {fullResponseLength: fullResponse.length, partCount});
333
421
 
334
- // Check for generated images (e.g. from gemini-2.5-flash-image)
335
- try {
336
- const files = await result.files;
337
- if (files && files.length > 0) {
338
- for (const file of files) {
339
- if (file.mediaType.startsWith("image/")) {
340
- const dataUrl = `data:${file.mediaType};base64,${file.base64}`;
341
- generatedImages.push({mimeType: file.mediaType, url: dataUrl});
342
- res.write(
343
- `data: ${JSON.stringify({
344
- image: {mimeType: file.mediaType, url: dataUrl},
345
- })}\n\n`
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
- logger.debug("Sent generated images", {count: files.length});
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
- // Save assistant response to history
358
- if (fullResponse || generatedImages.length > 0) {
359
- const contentParts: MessageContentPart[] = generatedImages.map((img) => ({
360
- mimeType: img.mimeType,
361
- type: "image" as const,
362
- url: img.url,
363
- }));
364
- const assistantPrompt: GptHistoryPrompt = {
365
- model: aiService.modelId,
366
- text: fullResponse || "(image)",
367
- type: "assistant",
368
- ...(contentParts.length > 0 ? {content: contentParts} : {}),
369
- };
370
- history.prompts.push(assistantPrompt);
371
- }
372
- await history.save();
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
- try {
375
- await AIRequest.logRequest({
376
- aiModel: modelId ?? "unknown",
377
- prompt,
378
- requestType: "general",
379
- response: fullResponse,
380
- responseTime: Date.now() - startTime,
381
- userId: userId ?? undefined,
382
- });
383
- } catch (logErr) {
384
- logger.warn("Failed to log AIRequest", {
385
- error: logErr instanceof Error ? logErr.message : String(logErr),
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
- logger.debug("Sending done event", {
390
- fullResponseLength: fullResponse.length,
391
- historyId: history._id.toString(),
392
- });
393
- res.write(`data: ${JSON.stringify({done: true, historyId: history._id.toString()})}\n\n`);
394
- res.end();
395
- } catch (error) {
396
- logger.error("Error in GPT stream", {
397
- error: error instanceof Error ? error.message : String(error),
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
- try {
401
- await AIRequest.logRequest({
402
- aiModel: modelId ?? "unknown",
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
- } catch (logErr) {
410
- logger.warn("Failed to log AIRequest error", {
411
- error: logErr instanceof Error ? logErr.message : String(logErr),
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
  };