@terreno/ai 0.6.0 → 0.7.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.
Files changed (63) hide show
  1. package/dist/index.d.ts +7 -0
  2. package/dist/index.js +24 -1
  3. package/dist/langfuseApp.d.ts +9 -0
  4. package/dist/langfuseApp.js +96 -0
  5. package/dist/langfuseCache.d.ts +16 -0
  6. package/dist/langfuseCache.js +104 -0
  7. package/dist/langfuseClient.d.ts +6 -0
  8. package/dist/langfuseClient.js +82 -0
  9. package/dist/langfusePrompts.d.ts +12 -0
  10. package/dist/langfusePrompts.js +215 -0
  11. package/dist/langfuseRoutesEvaluations.d.ts +3 -0
  12. package/dist/langfuseRoutesEvaluations.js +79 -0
  13. package/dist/langfuseRoutesMiddleware.d.ts +2 -0
  14. package/dist/langfuseRoutesMiddleware.js +13 -0
  15. package/dist/langfuseRoutesPlayground.d.ts +2 -0
  16. package/dist/langfuseRoutesPlayground.js +106 -0
  17. package/dist/langfuseRoutesPrompts.d.ts +2 -0
  18. package/dist/langfuseRoutesPrompts.js +129 -0
  19. package/dist/langfuseRoutesTraces.d.ts +2 -0
  20. package/dist/langfuseRoutesTraces.js +82 -0
  21. package/dist/langfuseTracing.d.ts +4 -0
  22. package/dist/langfuseTracing.js +77 -0
  23. package/dist/langfuseTypes.d.ts +96 -0
  24. package/dist/langfuseTypes.js +2 -0
  25. package/dist/langfuseVercelAi.d.ts +12 -0
  26. package/dist/langfuseVercelAi.js +98 -0
  27. package/dist/routes/gpt.js +110 -79
  28. package/dist/service/aiService.js +3 -0
  29. package/dist/types/index.d.ts +3 -0
  30. package/package.json +16 -3
  31. package/src/index.ts +24 -0
  32. package/src/langfuseApp.ts +57 -0
  33. package/src/langfuseCache.test.ts +83 -0
  34. package/src/langfuseCache.ts +58 -0
  35. package/src/langfuseClient.ts +32 -0
  36. package/src/langfusePrompts.test.ts +143 -0
  37. package/src/langfusePrompts.ts +150 -0
  38. package/src/langfuseRoutes.test.ts +143 -0
  39. package/src/langfuseRoutesEvaluations.ts +48 -0
  40. package/src/langfuseRoutesMiddleware.ts +12 -0
  41. package/src/langfuseRoutesPlayground.ts +50 -0
  42. package/src/langfuseRoutesPrompts.ts +86 -0
  43. package/src/langfuseRoutesTraces.ts +40 -0
  44. package/src/langfuseTracing.ts +33 -0
  45. package/src/langfuseTypes.ts +103 -0
  46. package/src/langfuseVercelAi.ts +72 -0
  47. package/src/routes/gpt.ts +30 -0
  48. package/src/routes/routes.test.ts +9 -1
  49. package/src/service/aiService.ts +3 -0
  50. package/src/types/index.ts +3 -0
  51. package/tsconfig.json +1 -1
  52. package/dist/models/aiRequest.test.d.ts +0 -1
  53. package/dist/models/aiRequest.test.js +0 -189
  54. package/dist/models/fileAttachment.test.d.ts +0 -1
  55. package/dist/models/fileAttachment.test.js +0 -217
  56. package/dist/models/gptHistory.test.d.ts +0 -1
  57. package/dist/models/gptHistory.test.js +0 -318
  58. package/dist/routes/routes.test.d.ts +0 -1
  59. package/dist/routes/routes.test.js +0 -303
  60. package/dist/service/aiService.test.d.ts +0 -1
  61. package/dist/service/aiService.test.js +0 -367
  62. package/dist/tests/bunSetup.d.ts +0 -1
  63. package/dist/tests/bunSetup.js +0 -227
@@ -1,318 +0,0 @@
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)("title", function () {
125
- (0, bun_test_1.it)("should not auto-generate title on save", 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).toBeUndefined();
145
- return [2 /*return*/];
146
- }
147
- });
148
- }); });
149
- (0, bun_test_1.it)("should preserve title when explicitly set", 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
- });
169
- (0, bun_test_1.describe)("multi-modal content", function () {
170
- (0, bun_test_1.it)("should store content parts on a prompt", function () { return __awaiter(void 0, void 0, void 0, function () {
171
- var userId, history, imagePart;
172
- var _a, _b, _c, _d;
173
- return __generator(this, function (_e) {
174
- switch (_e.label) {
175
- case 0:
176
- userId = new mongoose_1.default.Types.ObjectId();
177
- return [4 /*yield*/, gptHistory_1.GptHistory.create({
178
- prompts: [
179
- {
180
- content: [
181
- { text: "What is in this image?", type: "text" },
182
- { mimeType: "image/jpeg", type: "image", url: "https://example.com/img.jpg" },
183
- ],
184
- text: "What is in this image?",
185
- type: "user",
186
- },
187
- ],
188
- userId: userId,
189
- })];
190
- case 1:
191
- history = _e.sent();
192
- (0, bun_test_1.expect)(history.prompts[0].content).toBeDefined();
193
- (0, bun_test_1.expect)((_a = history.prompts[0].content) === null || _a === void 0 ? void 0 : _a.length).toBe(2);
194
- (0, bun_test_1.expect)((_b = history.prompts[0].content) === null || _b === void 0 ? void 0 : _b[0].type).toBe("text");
195
- (0, bun_test_1.expect)((_c = history.prompts[0].content) === null || _c === void 0 ? void 0 : _c[1].type).toBe("image");
196
- imagePart = (_d = history.prompts[0].content) === null || _d === void 0 ? void 0 : _d[1];
197
- (0, bun_test_1.expect)(imagePart.url).toBe("https://example.com/img.jpg");
198
- return [2 /*return*/];
199
- }
200
- });
201
- }); });
202
- (0, bun_test_1.it)("should store file content parts", function () { return __awaiter(void 0, void 0, void 0, function () {
203
- var userId, history, filePart;
204
- var _a, _b;
205
- return __generator(this, function (_c) {
206
- switch (_c.label) {
207
- case 0:
208
- userId = new mongoose_1.default.Types.ObjectId();
209
- return [4 /*yield*/, gptHistory_1.GptHistory.create({
210
- prompts: [
211
- {
212
- content: [
213
- { text: "Summarize this PDF", type: "text" },
214
- {
215
- filename: "report.pdf",
216
- mimeType: "application/pdf",
217
- type: "file",
218
- url: "https://example.com/report.pdf",
219
- },
220
- ],
221
- text: "Summarize this PDF",
222
- type: "user",
223
- },
224
- ],
225
- userId: userId,
226
- })];
227
- case 1:
228
- history = _c.sent();
229
- (0, bun_test_1.expect)((_a = history.prompts[0].content) === null || _a === void 0 ? void 0 : _a[1].type).toBe("file");
230
- filePart = (_b = history.prompts[0].content) === null || _b === void 0 ? void 0 : _b[1];
231
- (0, bun_test_1.expect)(filePart.filename).toBe("report.pdf");
232
- (0, bun_test_1.expect)(filePart.mimeType).toBe("application/pdf");
233
- return [2 /*return*/];
234
- }
235
- });
236
- }); });
237
- });
238
- (0, bun_test_1.describe)("tool call prompts", function () {
239
- (0, bun_test_1.it)("should store tool-call type prompts", function () { return __awaiter(void 0, void 0, void 0, function () {
240
- var userId, history;
241
- return __generator(this, function (_a) {
242
- switch (_a.label) {
243
- case 0:
244
- userId = new mongoose_1.default.Types.ObjectId();
245
- return [4 /*yield*/, gptHistory_1.GptHistory.create({
246
- prompts: [
247
- { text: "What time is it?", type: "user" },
248
- {
249
- args: { timezone: "UTC" },
250
- text: "Tool call: get_time",
251
- toolCallId: "call-123",
252
- toolName: "get_time",
253
- type: "tool-call",
254
- },
255
- ],
256
- userId: userId,
257
- })];
258
- case 1:
259
- history = _a.sent();
260
- (0, bun_test_1.expect)(history.prompts[1].type).toBe("tool-call");
261
- (0, bun_test_1.expect)(history.prompts[1].toolName).toBe("get_time");
262
- (0, bun_test_1.expect)(history.prompts[1].toolCallId).toBe("call-123");
263
- (0, bun_test_1.expect)(history.prompts[1].args).toEqual({ timezone: "UTC" });
264
- return [2 /*return*/];
265
- }
266
- });
267
- }); });
268
- (0, bun_test_1.it)("should store tool-result type prompts", function () { return __awaiter(void 0, void 0, void 0, function () {
269
- var userId, history;
270
- return __generator(this, function (_a) {
271
- switch (_a.label) {
272
- case 0:
273
- userId = new mongoose_1.default.Types.ObjectId();
274
- return [4 /*yield*/, gptHistory_1.GptHistory.create({
275
- prompts: [
276
- {
277
- result: { time: "2024-01-01T00:00:00Z" },
278
- text: "Tool result: get_time",
279
- toolCallId: "call-123",
280
- toolName: "get_time",
281
- type: "tool-result",
282
- },
283
- ],
284
- userId: userId,
285
- })];
286
- case 1:
287
- history = _a.sent();
288
- (0, bun_test_1.expect)(history.prompts[0].type).toBe("tool-result");
289
- (0, bun_test_1.expect)(history.prompts[0].toolName).toBe("get_time");
290
- (0, bun_test_1.expect)(history.prompts[0].result).toEqual({ time: "2024-01-01T00:00:00Z" });
291
- return [2 /*return*/];
292
- }
293
- });
294
- }); });
295
- });
296
- (0, bun_test_1.describe)("soft delete", function () {
297
- (0, bun_test_1.it)("should filter out deleted records by default", function () { return __awaiter(void 0, void 0, void 0, function () {
298
- var userId, results;
299
- return __generator(this, function (_a) {
300
- switch (_a.label) {
301
- case 0:
302
- userId = new mongoose_1.default.Types.ObjectId();
303
- return [4 /*yield*/, gptHistory_1.GptHistory.create({ deleted: true, prompts: [], userId: userId })];
304
- case 1:
305
- _a.sent();
306
- return [4 /*yield*/, gptHistory_1.GptHistory.create({ prompts: [], userId: userId })];
307
- case 2:
308
- _a.sent();
309
- return [4 /*yield*/, gptHistory_1.GptHistory.find({})];
310
- case 3:
311
- results = _a.sent();
312
- (0, bun_test_1.expect)(results.length).toBe(1);
313
- return [2 /*return*/];
314
- }
315
- });
316
- }); });
317
- });
318
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,303 +0,0 @@
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 api_1 = require("@terreno/api");
44
- var mongoose_1 = __importDefault(require("mongoose"));
45
- var passport_local_mongoose_1 = __importDefault(require("passport-local-mongoose"));
46
- var supertest_1 = __importDefault(require("supertest"));
47
- var aiRequest_1 = require("../models/aiRequest");
48
- var gptHistory_1 = require("../models/gptHistory");
49
- var aiService_1 = require("../service/aiService");
50
- var aiRequestsExplorer_1 = require("./aiRequestsExplorer");
51
- var gpt_1 = require("./gpt");
52
- var gptHistories_1 = require("./gptHistories");
53
- // Test user schema
54
- var userSchema = new mongoose_1.default.Schema({
55
- admin: { default: false, type: Boolean },
56
- email: { index: true, type: String },
57
- name: String,
58
- });
59
- userSchema.plugin(passport_local_mongoose_1.default, {
60
- usernameField: "email",
61
- });
62
- userSchema.plugin(api_1.createdUpdatedPlugin);
63
- var UserModel = mongoose_1.default.models.User || mongoose_1.default.model("User", userSchema);
64
- // Create mock AI model (LanguageModelV2)
65
- var createMockModel = function () { return ({
66
- doGenerate: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () {
67
- return __generator(this, function (_a) {
68
- return [2 /*return*/, ({
69
- content: [{ text: "AI response", type: "text" }],
70
- finishReason: "stop",
71
- usage: { inputTokens: 5, outputTokens: 10 },
72
- })];
73
- });
74
- }); }),
75
- doStream: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () {
76
- return __generator(this, function (_a) {
77
- return [2 /*return*/, ({
78
- stream: new ReadableStream({
79
- start: function (controller) {
80
- controller.enqueue({ id: "t1", type: "text-start" });
81
- controller.enqueue({ delta: "AI ", id: "t1", type: "text-delta" });
82
- controller.enqueue({ delta: "response", id: "t1", type: "text-delta" });
83
- controller.enqueue({ id: "t1", type: "text-end" });
84
- controller.enqueue({
85
- finishReason: "stop",
86
- type: "finish",
87
- usage: { inputTokens: 5, outputTokens: 10 },
88
- });
89
- controller.close();
90
- },
91
- }),
92
- })];
93
- });
94
- }); }),
95
- modelId: "mock-model",
96
- provider: "mock-provider",
97
- specificationVersion: "v2",
98
- supportedUrls: {},
99
- }); };
100
- var app;
101
- var aiService;
102
- var authAsUser = function (appInstance, type) { return __awaiter(void 0, void 0, void 0, function () {
103
- var email, password, agent, res;
104
- return __generator(this, function (_a) {
105
- switch (_a.label) {
106
- case 0:
107
- email = type === "admin" ? "admin@example.com" : "notAdmin@example.com";
108
- password = type === "admin" ? "securePassword" : "password";
109
- agent = supertest_1.default.agent(appInstance);
110
- return [4 /*yield*/, agent.post("/auth/login").send({ email: email, password: password }).expect(200)];
111
- case 1:
112
- res = _a.sent();
113
- return [4 /*yield*/, agent.set("authorization", "Bearer ".concat(res.body.data.token))];
114
- case 2:
115
- _a.sent();
116
- return [2 /*return*/, agent];
117
- }
118
- });
119
- }); };
120
- (0, bun_test_1.describe)("AI Routes", function () {
121
- (0, bun_test_1.beforeAll)(function () { return __awaiter(void 0, void 0, void 0, function () {
122
- var admin, user;
123
- return __generator(this, function (_a) {
124
- switch (_a.label) {
125
- case 0:
126
- // Clean up and create test users
127
- return [4 /*yield*/, UserModel.deleteMany({})];
128
- case 1:
129
- // Clean up and create test users
130
- _a.sent();
131
- return [4 /*yield*/, aiRequest_1.AIRequest.deleteMany({})];
132
- case 2:
133
- _a.sent();
134
- return [4 /*yield*/, gptHistory_1.GptHistory.deleteMany({})];
135
- case 3:
136
- _a.sent();
137
- return [4 /*yield*/, UserModel.create({ admin: true, email: "admin@example.com", name: "Admin" })];
138
- case 4:
139
- admin = _a.sent();
140
- return [4 /*yield*/, admin.setPassword("securePassword")];
141
- case 5:
142
- _a.sent();
143
- return [4 /*yield*/, admin.save()];
144
- case 6:
145
- _a.sent();
146
- return [4 /*yield*/, UserModel.create({ email: "notAdmin@example.com", name: "User" })];
147
- case 7:
148
- user = _a.sent();
149
- return [4 /*yield*/, user.setPassword("password")];
150
- case 8:
151
- _a.sent();
152
- return [4 /*yield*/, user.save()];
153
- case 9:
154
- _a.sent();
155
- return [2 /*return*/];
156
- }
157
- });
158
- }); });
159
- (0, bun_test_1.beforeEach)(function () {
160
- var mockModel = createMockModel();
161
- aiService = new aiService_1.AIService({ model: mockModel });
162
- app = (0, api_1.setupServer)({
163
- addRoutes: function (router, options) {
164
- (0, gptHistories_1.addGptHistoryRoutes)(router, options);
165
- (0, gpt_1.addGptRoutes)(router, { aiService: aiService, openApiOptions: options });
166
- (0, aiRequestsExplorer_1.addAiRequestsExplorerRoutes)(router, { openApiOptions: options });
167
- },
168
- skipListen: true,
169
- userModel: UserModel,
170
- });
171
- });
172
- (0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
173
- return __generator(this, function (_a) {
174
- switch (_a.label) {
175
- case 0: return [4 /*yield*/, aiRequest_1.AIRequest.deleteMany({})];
176
- case 1:
177
- _a.sent();
178
- return [4 /*yield*/, gptHistory_1.GptHistory.deleteMany({})];
179
- case 2:
180
- _a.sent();
181
- return [2 /*return*/];
182
- }
183
- });
184
- }); });
185
- (0, bun_test_1.describe)("GPT History routes", function () {
186
- (0, bun_test_1.it)("should create a history", function () { return __awaiter(void 0, void 0, void 0, function () {
187
- var agent, res;
188
- return __generator(this, function (_a) {
189
- switch (_a.label) {
190
- case 0: return [4 /*yield*/, authAsUser(app, "notAdmin")];
191
- case 1:
192
- agent = _a.sent();
193
- return [4 /*yield*/, agent
194
- .post("/gpt/histories")
195
- .send({ prompts: [{ text: "Hello", type: "user" }] })];
196
- case 2:
197
- res = _a.sent();
198
- (0, bun_test_1.expect)(res.status).toBe(201);
199
- (0, bun_test_1.expect)(res.body.data.prompts.length).toBe(1);
200
- return [2 /*return*/];
201
- }
202
- });
203
- }); });
204
- (0, bun_test_1.it)("should list histories for the current user", function () { return __awaiter(void 0, void 0, void 0, function () {
205
- var agent, res;
206
- return __generator(this, function (_a) {
207
- switch (_a.label) {
208
- case 0: return [4 /*yield*/, authAsUser(app, "notAdmin")];
209
- case 1:
210
- agent = _a.sent();
211
- return [4 /*yield*/, agent.post("/gpt/histories").send({ prompts: [{ text: "Hello", type: "user" }] })];
212
- case 2:
213
- _a.sent();
214
- return [4 /*yield*/, agent.get("/gpt/histories")];
215
- case 3:
216
- res = _a.sent();
217
- (0, bun_test_1.expect)(res.status).toBe(200);
218
- (0, bun_test_1.expect)(res.body.data.length).toBeGreaterThanOrEqual(1);
219
- return [2 /*return*/];
220
- }
221
- });
222
- }); });
223
- });
224
- (0, bun_test_1.describe)("GPT Remix route", function () {
225
- (0, bun_test_1.it)("should remix text", function () { return __awaiter(void 0, void 0, void 0, function () {
226
- var agent, res;
227
- return __generator(this, function (_a) {
228
- switch (_a.label) {
229
- case 0: return [4 /*yield*/, authAsUser(app, "notAdmin")];
230
- case 1:
231
- agent = _a.sent();
232
- return [4 /*yield*/, agent.post("/gpt/remix").send({ text: "Hello world" })];
233
- case 2:
234
- res = _a.sent();
235
- (0, bun_test_1.expect)(res.status).toBe(200);
236
- (0, bun_test_1.expect)(res.body.data).toBeDefined();
237
- return [2 /*return*/];
238
- }
239
- });
240
- }); });
241
- (0, bun_test_1.it)("should require text", function () { return __awaiter(void 0, void 0, void 0, function () {
242
- var agent, res;
243
- return __generator(this, function (_a) {
244
- switch (_a.label) {
245
- case 0: return [4 /*yield*/, authAsUser(app, "notAdmin")];
246
- case 1:
247
- agent = _a.sent();
248
- return [4 /*yield*/, agent.post("/gpt/remix").send({})];
249
- case 2:
250
- res = _a.sent();
251
- (0, bun_test_1.expect)(res.status).toBe(400);
252
- return [2 /*return*/];
253
- }
254
- });
255
- }); });
256
- });
257
- (0, bun_test_1.describe)("AI Requests Explorer route", function () {
258
- (0, bun_test_1.it)("should return 403 for non-admin users", function () { return __awaiter(void 0, void 0, void 0, function () {
259
- var agent, res;
260
- return __generator(this, function (_a) {
261
- switch (_a.label) {
262
- case 0: return [4 /*yield*/, authAsUser(app, "notAdmin")];
263
- case 1:
264
- agent = _a.sent();
265
- return [4 /*yield*/, agent.get("/aiRequestsExplorer")];
266
- case 2:
267
- res = _a.sent();
268
- (0, bun_test_1.expect)(res.status).toBe(403);
269
- return [2 /*return*/];
270
- }
271
- });
272
- }); });
273
- (0, bun_test_1.it)("should return data for admin users", function () { return __awaiter(void 0, void 0, void 0, function () {
274
- var agent, res;
275
- return __generator(this, function (_a) {
276
- switch (_a.label) {
277
- case 0:
278
- // Create some test data
279
- return [4 /*yield*/, aiRequest_1.AIRequest.create({
280
- aiModel: "gpt-4",
281
- prompt: "test",
282
- requestType: "general",
283
- response: "response",
284
- })];
285
- case 1:
286
- // Create some test data
287
- _a.sent();
288
- return [4 /*yield*/, authAsUser(app, "admin")];
289
- case 2:
290
- agent = _a.sent();
291
- return [4 /*yield*/, agent.get("/aiRequestsExplorer")];
292
- case 3:
293
- res = _a.sent();
294
- (0, bun_test_1.expect)(res.status).toBe(200);
295
- (0, bun_test_1.expect)(res.body.data).toBeDefined();
296
- (0, bun_test_1.expect)(res.body.total).toBeGreaterThanOrEqual(1);
297
- (0, bun_test_1.expect)(res.body.page).toBe(1);
298
- return [2 /*return*/];
299
- }
300
- });
301
- }); });
302
- });
303
- });
@@ -1 +0,0 @@
1
- export {};