@terreno/ai 0.3.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.
Files changed (81) hide show
  1. package/.claude/CLAUDE.local.md +208 -0
  2. package/.cursor/rules/00-root.mdc +342 -0
  3. package/.cursorrules +221 -0
  4. package/.github/copilot-instructions.md +337 -0
  5. package/AGENTS.md +337 -0
  6. package/biome.jsonc +20 -0
  7. package/bunfig.toml +3 -0
  8. package/dist/aiApp.d.ts +59 -0
  9. package/dist/aiApp.js +67 -0
  10. package/dist/index.d.ts +6 -0
  11. package/dist/index.js +23 -0
  12. package/dist/models/aiRequest.d.ts +2 -0
  13. package/dist/models/aiRequest.js +76 -0
  14. package/dist/models/aiRequest.test.d.ts +1 -0
  15. package/dist/models/aiRequest.test.js +187 -0
  16. package/dist/models/fileAttachment.d.ts +2 -0
  17. package/dist/models/fileAttachment.js +25 -0
  18. package/dist/models/fileAttachment.test.d.ts +1 -0
  19. package/dist/models/fileAttachment.test.js +217 -0
  20. package/dist/models/gptHistory.d.ts +2 -0
  21. package/dist/models/gptHistory.js +56 -0
  22. package/dist/models/gptHistory.test.d.ts +1 -0
  23. package/dist/models/gptHistory.test.js +336 -0
  24. package/dist/models/index.d.ts +3 -0
  25. package/dist/models/index.js +9 -0
  26. package/dist/routes/aiRequestsExplorer.d.ts +2 -0
  27. package/dist/routes/aiRequestsExplorer.js +153 -0
  28. package/dist/routes/files.d.ts +5 -0
  29. package/dist/routes/files.js +168 -0
  30. package/dist/routes/gpt.d.ts +2 -0
  31. package/dist/routes/gpt.js +593 -0
  32. package/dist/routes/gptHistories.d.ts +3 -0
  33. package/dist/routes/gptHistories.js +29 -0
  34. package/dist/routes/index.d.ts +5 -0
  35. package/dist/routes/index.js +13 -0
  36. package/dist/routes/mcp.d.ts +2 -0
  37. package/dist/routes/mcp.js +133 -0
  38. package/dist/routes/routes.test.d.ts +1 -0
  39. package/dist/routes/routes.test.js +303 -0
  40. package/dist/service/aiService.d.ts +24 -0
  41. package/dist/service/aiService.js +453 -0
  42. package/dist/service/aiService.test.d.ts +1 -0
  43. package/dist/service/aiService.test.js +367 -0
  44. package/dist/service/fileStorage.d.ts +24 -0
  45. package/dist/service/fileStorage.js +143 -0
  46. package/dist/service/index.d.ts +4 -0
  47. package/dist/service/index.js +15 -0
  48. package/dist/service/mcpService.d.ts +14 -0
  49. package/dist/service/mcpService.js +271 -0
  50. package/dist/service/prompts.d.ts +4 -0
  51. package/dist/service/prompts.js +14 -0
  52. package/dist/tests/bunSetup.d.ts +1 -0
  53. package/dist/tests/bunSetup.js +227 -0
  54. package/dist/types/index.d.ts +170 -0
  55. package/dist/types/index.js +7 -0
  56. package/package.json +55 -0
  57. package/src/aiApp.ts +110 -0
  58. package/src/index.ts +6 -0
  59. package/src/models/aiRequest.test.ts +101 -0
  60. package/src/models/aiRequest.ts +45 -0
  61. package/src/models/fileAttachment.test.ts +119 -0
  62. package/src/models/fileAttachment.ts +31 -0
  63. package/src/models/gptHistory.test.ts +209 -0
  64. package/src/models/gptHistory.ts +71 -0
  65. package/src/models/index.ts +3 -0
  66. package/src/routes/aiRequestsExplorer.ts +107 -0
  67. package/src/routes/files.ts +128 -0
  68. package/src/routes/gpt.ts +503 -0
  69. package/src/routes/gptHistories.ts +31 -0
  70. package/src/routes/index.ts +5 -0
  71. package/src/routes/mcp.ts +76 -0
  72. package/src/routes/routes.test.ts +168 -0
  73. package/src/service/aiService.test.ts +242 -0
  74. package/src/service/aiService.ts +269 -0
  75. package/src/service/fileStorage.ts +74 -0
  76. package/src/service/index.ts +9 -0
  77. package/src/service/mcpService.ts +120 -0
  78. package/src/service/prompts.ts +18 -0
  79. package/src/tests/bunSetup.ts +163 -0
  80. package/src/types/index.ts +224 -0
  81. package/tsconfig.json +31 -0
@@ -0,0 +1,336 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ 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);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ var bun_test_1 = require("bun:test");
43
+ var mongoose_1 = __importDefault(require("mongoose"));
44
+ var gptHistory_1 = require("./gptHistory");
45
+ (0, bun_test_1.describe)("GptHistory Model", function () {
46
+ (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
47
+ return __generator(this, function (_a) {
48
+ switch (_a.label) {
49
+ case 0: return [4 /*yield*/, gptHistory_1.GptHistory.deleteMany({})];
50
+ case 1:
51
+ _a.sent();
52
+ return [2 /*return*/];
53
+ }
54
+ });
55
+ }); });
56
+ (0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
57
+ return __generator(this, function (_a) {
58
+ switch (_a.label) {
59
+ case 0: return [4 /*yield*/, gptHistory_1.GptHistory.deleteMany({})];
60
+ case 1:
61
+ _a.sent();
62
+ return [2 /*return*/];
63
+ }
64
+ });
65
+ }); });
66
+ (0, bun_test_1.describe)("schema", function () {
67
+ (0, bun_test_1.it)("should create a history with required fields", function () { return __awaiter(void 0, void 0, void 0, function () {
68
+ var userId, history;
69
+ return __generator(this, function (_a) {
70
+ switch (_a.label) {
71
+ case 0:
72
+ userId = new mongoose_1.default.Types.ObjectId();
73
+ return [4 /*yield*/, gptHistory_1.GptHistory.create({
74
+ prompts: [],
75
+ userId: userId,
76
+ })];
77
+ case 1:
78
+ history = _a.sent();
79
+ (0, bun_test_1.expect)(history.userId.toString()).toBe(userId.toString());
80
+ (0, bun_test_1.expect)(history.prompts).toEqual([]);
81
+ (0, bun_test_1.expect)(history.created).toBeDefined();
82
+ (0, bun_test_1.expect)(history.updated).toBeDefined();
83
+ (0, bun_test_1.expect)(history.deleted).toBe(false);
84
+ return [2 /*return*/];
85
+ }
86
+ });
87
+ }); });
88
+ (0, bun_test_1.it)("should create a history with prompts", function () { return __awaiter(void 0, void 0, void 0, function () {
89
+ var userId, history;
90
+ return __generator(this, function (_a) {
91
+ switch (_a.label) {
92
+ case 0:
93
+ userId = new mongoose_1.default.Types.ObjectId();
94
+ return [4 /*yield*/, gptHistory_1.GptHistory.create({
95
+ prompts: [
96
+ { text: "Hello", type: "user" },
97
+ { model: "gpt-4", text: "Hi there!", type: "assistant" },
98
+ ],
99
+ userId: userId,
100
+ })];
101
+ case 1:
102
+ history = _a.sent();
103
+ (0, bun_test_1.expect)(history.prompts.length).toBe(2);
104
+ (0, bun_test_1.expect)(history.prompts[0].text).toBe("Hello");
105
+ (0, bun_test_1.expect)(history.prompts[0].type).toBe("user");
106
+ (0, bun_test_1.expect)(history.prompts[1].text).toBe("Hi there!");
107
+ (0, bun_test_1.expect)(history.prompts[1].type).toBe("assistant");
108
+ (0, bun_test_1.expect)(history.prompts[1].model).toBe("gpt-4");
109
+ return [2 /*return*/];
110
+ }
111
+ });
112
+ }); });
113
+ (0, bun_test_1.it)("should require userId", function () { return __awaiter(void 0, void 0, void 0, function () {
114
+ return __generator(this, function (_a) {
115
+ switch (_a.label) {
116
+ case 0: return [4 /*yield*/, (0, bun_test_1.expect)(gptHistory_1.GptHistory.create({ prompts: [] })).rejects.toThrow()];
117
+ case 1:
118
+ _a.sent();
119
+ return [2 /*return*/];
120
+ }
121
+ });
122
+ }); });
123
+ });
124
+ (0, bun_test_1.describe)("auto-title", function () {
125
+ (0, bun_test_1.it)("should auto-generate title from first assistant response", function () { return __awaiter(void 0, void 0, void 0, function () {
126
+ var userId, history;
127
+ return __generator(this, function (_a) {
128
+ switch (_a.label) {
129
+ case 0:
130
+ userId = new mongoose_1.default.Types.ObjectId();
131
+ history = new gptHistory_1.GptHistory({
132
+ prompts: [
133
+ { text: "What is AI?", type: "user" },
134
+ {
135
+ text: "Artificial Intelligence is a branch of computer science that aims to create intelligent machines.",
136
+ type: "assistant",
137
+ },
138
+ ],
139
+ userId: userId,
140
+ });
141
+ return [4 /*yield*/, history.save()];
142
+ case 1:
143
+ _a.sent();
144
+ (0, bun_test_1.expect)(history.title).toBe("Artificial Intelligence is a branch of computer sc");
145
+ return [2 /*return*/];
146
+ }
147
+ });
148
+ }); });
149
+ (0, bun_test_1.it)("should not overwrite existing title", function () { return __awaiter(void 0, void 0, void 0, function () {
150
+ var userId, history;
151
+ return __generator(this, function (_a) {
152
+ switch (_a.label) {
153
+ case 0:
154
+ userId = new mongoose_1.default.Types.ObjectId();
155
+ history = new gptHistory_1.GptHistory({
156
+ prompts: [{ text: "Some response", type: "assistant" }],
157
+ title: "Custom Title",
158
+ userId: userId,
159
+ });
160
+ return [4 /*yield*/, history.save()];
161
+ case 1:
162
+ _a.sent();
163
+ (0, bun_test_1.expect)(history.title).toBe("Custom Title");
164
+ return [2 /*return*/];
165
+ }
166
+ });
167
+ }); });
168
+ (0, bun_test_1.it)("should not set title if no assistant response", function () { return __awaiter(void 0, void 0, void 0, function () {
169
+ var userId, history;
170
+ return __generator(this, function (_a) {
171
+ switch (_a.label) {
172
+ case 0:
173
+ userId = new mongoose_1.default.Types.ObjectId();
174
+ history = new gptHistory_1.GptHistory({
175
+ prompts: [{ text: "Hello", type: "user" }],
176
+ userId: userId,
177
+ });
178
+ return [4 /*yield*/, history.save()];
179
+ case 1:
180
+ _a.sent();
181
+ (0, bun_test_1.expect)(history.title).toBeUndefined();
182
+ return [2 /*return*/];
183
+ }
184
+ });
185
+ }); });
186
+ });
187
+ (0, bun_test_1.describe)("multi-modal content", function () {
188
+ (0, bun_test_1.it)("should store content parts on a prompt", function () { return __awaiter(void 0, void 0, void 0, function () {
189
+ var userId, history, imagePart;
190
+ var _a, _b, _c, _d;
191
+ return __generator(this, function (_e) {
192
+ switch (_e.label) {
193
+ case 0:
194
+ userId = new mongoose_1.default.Types.ObjectId();
195
+ return [4 /*yield*/, gptHistory_1.GptHistory.create({
196
+ prompts: [
197
+ {
198
+ content: [
199
+ { text: "What is in this image?", type: "text" },
200
+ { mimeType: "image/jpeg", type: "image", url: "https://example.com/img.jpg" },
201
+ ],
202
+ text: "What is in this image?",
203
+ type: "user",
204
+ },
205
+ ],
206
+ userId: userId,
207
+ })];
208
+ case 1:
209
+ history = _e.sent();
210
+ (0, bun_test_1.expect)(history.prompts[0].content).toBeDefined();
211
+ (0, bun_test_1.expect)((_a = history.prompts[0].content) === null || _a === void 0 ? void 0 : _a.length).toBe(2);
212
+ (0, bun_test_1.expect)((_b = history.prompts[0].content) === null || _b === void 0 ? void 0 : _b[0].type).toBe("text");
213
+ (0, bun_test_1.expect)((_c = history.prompts[0].content) === null || _c === void 0 ? void 0 : _c[1].type).toBe("image");
214
+ imagePart = (_d = history.prompts[0].content) === null || _d === void 0 ? void 0 : _d[1];
215
+ (0, bun_test_1.expect)(imagePart.url).toBe("https://example.com/img.jpg");
216
+ return [2 /*return*/];
217
+ }
218
+ });
219
+ }); });
220
+ (0, bun_test_1.it)("should store file content parts", function () { return __awaiter(void 0, void 0, void 0, function () {
221
+ var userId, history, filePart;
222
+ var _a, _b;
223
+ return __generator(this, function (_c) {
224
+ switch (_c.label) {
225
+ case 0:
226
+ userId = new mongoose_1.default.Types.ObjectId();
227
+ return [4 /*yield*/, gptHistory_1.GptHistory.create({
228
+ prompts: [
229
+ {
230
+ content: [
231
+ { text: "Summarize this PDF", type: "text" },
232
+ {
233
+ filename: "report.pdf",
234
+ mimeType: "application/pdf",
235
+ type: "file",
236
+ url: "https://example.com/report.pdf",
237
+ },
238
+ ],
239
+ text: "Summarize this PDF",
240
+ type: "user",
241
+ },
242
+ ],
243
+ userId: userId,
244
+ })];
245
+ case 1:
246
+ history = _c.sent();
247
+ (0, bun_test_1.expect)((_a = history.prompts[0].content) === null || _a === void 0 ? void 0 : _a[1].type).toBe("file");
248
+ filePart = (_b = history.prompts[0].content) === null || _b === void 0 ? void 0 : _b[1];
249
+ (0, bun_test_1.expect)(filePart.filename).toBe("report.pdf");
250
+ (0, bun_test_1.expect)(filePart.mimeType).toBe("application/pdf");
251
+ return [2 /*return*/];
252
+ }
253
+ });
254
+ }); });
255
+ });
256
+ (0, bun_test_1.describe)("tool call prompts", function () {
257
+ (0, bun_test_1.it)("should store tool-call type prompts", function () { return __awaiter(void 0, void 0, void 0, function () {
258
+ var userId, history;
259
+ return __generator(this, function (_a) {
260
+ switch (_a.label) {
261
+ case 0:
262
+ userId = new mongoose_1.default.Types.ObjectId();
263
+ return [4 /*yield*/, gptHistory_1.GptHistory.create({
264
+ prompts: [
265
+ { text: "What time is it?", type: "user" },
266
+ {
267
+ args: { timezone: "UTC" },
268
+ text: "Tool call: get_time",
269
+ toolCallId: "call-123",
270
+ toolName: "get_time",
271
+ type: "tool-call",
272
+ },
273
+ ],
274
+ userId: userId,
275
+ })];
276
+ case 1:
277
+ history = _a.sent();
278
+ (0, bun_test_1.expect)(history.prompts[1].type).toBe("tool-call");
279
+ (0, bun_test_1.expect)(history.prompts[1].toolName).toBe("get_time");
280
+ (0, bun_test_1.expect)(history.prompts[1].toolCallId).toBe("call-123");
281
+ (0, bun_test_1.expect)(history.prompts[1].args).toEqual({ timezone: "UTC" });
282
+ return [2 /*return*/];
283
+ }
284
+ });
285
+ }); });
286
+ (0, bun_test_1.it)("should store tool-result type prompts", function () { return __awaiter(void 0, void 0, void 0, function () {
287
+ var userId, history;
288
+ return __generator(this, function (_a) {
289
+ switch (_a.label) {
290
+ case 0:
291
+ userId = new mongoose_1.default.Types.ObjectId();
292
+ return [4 /*yield*/, gptHistory_1.GptHistory.create({
293
+ prompts: [
294
+ {
295
+ result: { time: "2024-01-01T00:00:00Z" },
296
+ text: "Tool result: get_time",
297
+ toolCallId: "call-123",
298
+ toolName: "get_time",
299
+ type: "tool-result",
300
+ },
301
+ ],
302
+ userId: userId,
303
+ })];
304
+ case 1:
305
+ history = _a.sent();
306
+ (0, bun_test_1.expect)(history.prompts[0].type).toBe("tool-result");
307
+ (0, bun_test_1.expect)(history.prompts[0].toolName).toBe("get_time");
308
+ (0, bun_test_1.expect)(history.prompts[0].result).toEqual({ time: "2024-01-01T00:00:00Z" });
309
+ return [2 /*return*/];
310
+ }
311
+ });
312
+ }); });
313
+ });
314
+ (0, bun_test_1.describe)("soft delete", function () {
315
+ (0, bun_test_1.it)("should filter out deleted records by default", function () { return __awaiter(void 0, void 0, void 0, function () {
316
+ var userId, results;
317
+ return __generator(this, function (_a) {
318
+ switch (_a.label) {
319
+ case 0:
320
+ userId = new mongoose_1.default.Types.ObjectId();
321
+ return [4 /*yield*/, gptHistory_1.GptHistory.create({ deleted: true, prompts: [], userId: userId })];
322
+ case 1:
323
+ _a.sent();
324
+ return [4 /*yield*/, gptHistory_1.GptHistory.create({ prompts: [], userId: userId })];
325
+ case 2:
326
+ _a.sent();
327
+ return [4 /*yield*/, gptHistory_1.GptHistory.find({})];
328
+ case 3:
329
+ results = _a.sent();
330
+ (0, bun_test_1.expect)(results.length).toBe(1);
331
+ return [2 /*return*/];
332
+ }
333
+ });
334
+ }); });
335
+ });
336
+ });
@@ -0,0 +1,3 @@
1
+ export { AIRequest } from "./aiRequest";
2
+ export { FileAttachment } from "./fileAttachment";
3
+ export { GptHistory } from "./gptHistory";
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GptHistory = exports.FileAttachment = exports.AIRequest = void 0;
4
+ var aiRequest_1 = require("./aiRequest");
5
+ Object.defineProperty(exports, "AIRequest", { enumerable: true, get: function () { return aiRequest_1.AIRequest; } });
6
+ var fileAttachment_1 = require("./fileAttachment");
7
+ Object.defineProperty(exports, "FileAttachment", { enumerable: true, get: function () { return fileAttachment_1.FileAttachment; } });
8
+ var gptHistory_1 = require("./gptHistory");
9
+ Object.defineProperty(exports, "GptHistory", { enumerable: true, get: function () { return gptHistory_1.GptHistory; } });
@@ -0,0 +1,2 @@
1
+ import type { AiRequestsExplorerRouteOptions } from "../types";
2
+ export declare const addAiRequestsExplorerRoutes: (router: any, options?: AiRequestsExplorerRouteOptions) => void;
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ 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);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __read = (this && this.__read) || function (o, n) {
39
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
40
+ if (!m) return o;
41
+ var i = m.call(o), r, ar = [], e;
42
+ try {
43
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
44
+ }
45
+ catch (error) { e = { error: error }; }
46
+ finally {
47
+ try {
48
+ if (r && !r.done && (m = i["return"])) m.call(i);
49
+ }
50
+ finally { if (e) throw e.error; }
51
+ }
52
+ return ar;
53
+ };
54
+ Object.defineProperty(exports, "__esModule", { value: true });
55
+ exports.addAiRequestsExplorerRoutes = void 0;
56
+ var api_1 = require("@terreno/api");
57
+ var aiRequest_1 = require("../models/aiRequest");
58
+ var addAiRequestsExplorerRoutes = function (router, options) {
59
+ var _a;
60
+ router.get("/aiRequestsExplorer", [
61
+ (0, api_1.authenticateMiddleware)(),
62
+ (0, api_1.createOpenApiBuilder)((_a = options === null || options === void 0 ? void 0 : options.openApiOptions) !== null && _a !== void 0 ? _a : {})
63
+ .withTags(["admin"])
64
+ .withSummary("List AI requests (admin only)")
65
+ .withQueryParameter("page", { type: "number" }, { required: false })
66
+ .withQueryParameter("limit", { type: "number" }, { required: false })
67
+ .withQueryParameter("requestType", { type: "string" }, { required: false })
68
+ .withQueryParameter("model", { type: "string" }, { required: false })
69
+ .withQueryParameter("startDate", { type: "string" }, { required: false })
70
+ .withQueryParameter("endDate", { type: "string" }, { required: false })
71
+ .withResponse(200, {
72
+ data: { items: { type: "object" }, type: "array" },
73
+ limit: { type: "number" },
74
+ more: { type: "boolean" },
75
+ page: { type: "number" },
76
+ total: { type: "number" },
77
+ })
78
+ .build(),
79
+ ], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
80
+ var user, page, limit, skip, match, _a, results, totalCount;
81
+ return __generator(this, function (_b) {
82
+ switch (_b.label) {
83
+ case 0:
84
+ user = req.user;
85
+ if (!(user === null || user === void 0 ? void 0 : user.admin)) {
86
+ return [2 /*return*/, res.status(403).json({ error: "Admin access required" })];
87
+ }
88
+ page = Math.max(1, Number.parseInt(req.query.page, 10) || 1);
89
+ limit = Math.min(100, Math.max(1, Number.parseInt(req.query.limit, 10) || 20));
90
+ skip = (page - 1) * limit;
91
+ match = { deleted: { $ne: true } };
92
+ if (req.query.requestType) {
93
+ match.requestType = req.query.requestType;
94
+ }
95
+ if (req.query.model) {
96
+ match.aiModel = req.query.model;
97
+ }
98
+ if (req.query.startDate || req.query.endDate) {
99
+ match.created = {};
100
+ if (req.query.startDate) {
101
+ match.created.$gte = new Date(req.query.startDate);
102
+ }
103
+ if (req.query.endDate) {
104
+ match.created.$lte = new Date(req.query.endDate);
105
+ }
106
+ }
107
+ return [4 /*yield*/, Promise.all([
108
+ aiRequest_1.AIRequest.aggregate([
109
+ { $match: match },
110
+ { $sort: { created: -1 } },
111
+ { $skip: skip },
112
+ { $limit: limit },
113
+ {
114
+ $lookup: {
115
+ as: "user",
116
+ foreignField: "_id",
117
+ from: "users",
118
+ localField: "userId",
119
+ },
120
+ },
121
+ { $unwind: { path: "$user", preserveNullAndEmptyArrays: true } },
122
+ {
123
+ $project: {
124
+ aiModel: 1,
125
+ created: 1,
126
+ error: 1,
127
+ prompt: { $substrCP: ["$prompt", 0, 200] },
128
+ requestType: 1,
129
+ response: { $substrCP: [{ $ifNull: ["$response", ""] }, 0, 200] },
130
+ responseTime: 1,
131
+ tokensUsed: 1,
132
+ "user.email": 1,
133
+ "user.name": 1,
134
+ userId: 1,
135
+ },
136
+ },
137
+ ]),
138
+ aiRequest_1.AIRequest.countDocuments(match),
139
+ ])];
140
+ case 1:
141
+ _a = __read.apply(void 0, [_b.sent(), 2]), results = _a[0], totalCount = _a[1];
142
+ return [2 /*return*/, res.json({
143
+ data: results,
144
+ limit: limit,
145
+ more: skip + limit < totalCount,
146
+ page: page,
147
+ total: totalCount,
148
+ })];
149
+ }
150
+ });
151
+ }); }));
152
+ };
153
+ exports.addAiRequestsExplorerRoutes = addAiRequestsExplorerRoutes;
@@ -0,0 +1,5 @@
1
+ import type { FileStorageService } from "../service/fileStorage";
2
+ import type { FileRouteOptions } from "../types";
3
+ export declare const addFileRoutes: (router: any, options: FileRouteOptions & {
4
+ fileStorageService: FileStorageService;
5
+ }) => void;