@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.
- package/.claude/CLAUDE.local.md +208 -0
- package/.cursor/rules/00-root.mdc +342 -0
- package/.cursorrules +221 -0
- package/.github/copilot-instructions.md +337 -0
- package/AGENTS.md +337 -0
- package/biome.jsonc +20 -0
- package/bunfig.toml +3 -0
- package/dist/aiApp.d.ts +59 -0
- package/dist/aiApp.js +67 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +23 -0
- package/dist/models/aiRequest.d.ts +2 -0
- package/dist/models/aiRequest.js +76 -0
- package/dist/models/aiRequest.test.d.ts +1 -0
- package/dist/models/aiRequest.test.js +187 -0
- package/dist/models/fileAttachment.d.ts +2 -0
- package/dist/models/fileAttachment.js +25 -0
- package/dist/models/fileAttachment.test.d.ts +1 -0
- package/dist/models/fileAttachment.test.js +217 -0
- package/dist/models/gptHistory.d.ts +2 -0
- package/dist/models/gptHistory.js +56 -0
- package/dist/models/gptHistory.test.d.ts +1 -0
- package/dist/models/gptHistory.test.js +336 -0
- package/dist/models/index.d.ts +3 -0
- package/dist/models/index.js +9 -0
- package/dist/routes/aiRequestsExplorer.d.ts +2 -0
- package/dist/routes/aiRequestsExplorer.js +153 -0
- package/dist/routes/files.d.ts +5 -0
- package/dist/routes/files.js +168 -0
- package/dist/routes/gpt.d.ts +2 -0
- package/dist/routes/gpt.js +593 -0
- package/dist/routes/gptHistories.d.ts +3 -0
- package/dist/routes/gptHistories.js +29 -0
- package/dist/routes/index.d.ts +5 -0
- package/dist/routes/index.js +13 -0
- package/dist/routes/mcp.d.ts +2 -0
- package/dist/routes/mcp.js +133 -0
- package/dist/routes/routes.test.d.ts +1 -0
- package/dist/routes/routes.test.js +303 -0
- package/dist/service/aiService.d.ts +24 -0
- package/dist/service/aiService.js +453 -0
- package/dist/service/aiService.test.d.ts +1 -0
- package/dist/service/aiService.test.js +367 -0
- package/dist/service/fileStorage.d.ts +24 -0
- package/dist/service/fileStorage.js +143 -0
- package/dist/service/index.d.ts +4 -0
- package/dist/service/index.js +15 -0
- package/dist/service/mcpService.d.ts +14 -0
- package/dist/service/mcpService.js +271 -0
- package/dist/service/prompts.d.ts +4 -0
- package/dist/service/prompts.js +14 -0
- package/dist/tests/bunSetup.d.ts +1 -0
- package/dist/tests/bunSetup.js +227 -0
- package/dist/types/index.d.ts +170 -0
- package/dist/types/index.js +7 -0
- package/package.json +55 -0
- package/src/aiApp.ts +110 -0
- package/src/index.ts +6 -0
- package/src/models/aiRequest.test.ts +101 -0
- package/src/models/aiRequest.ts +45 -0
- package/src/models/fileAttachment.test.ts +119 -0
- package/src/models/fileAttachment.ts +31 -0
- package/src/models/gptHistory.test.ts +209 -0
- package/src/models/gptHistory.ts +71 -0
- package/src/models/index.ts +3 -0
- package/src/routes/aiRequestsExplorer.ts +107 -0
- package/src/routes/files.ts +128 -0
- package/src/routes/gpt.ts +503 -0
- package/src/routes/gptHistories.ts +31 -0
- package/src/routes/index.ts +5 -0
- package/src/routes/mcp.ts +76 -0
- package/src/routes/routes.test.ts +168 -0
- package/src/service/aiService.test.ts +242 -0
- package/src/service/aiService.ts +269 -0
- package/src/service/fileStorage.ts +74 -0
- package/src/service/index.ts +9 -0
- package/src/service/mcpService.ts +120 -0
- package/src/service/prompts.ts +18 -0
- package/src/tests/bunSetup.ts +163 -0
- package/src/types/index.ts +224 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,187 @@
|
|
|
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 aiRequest_1 = require("./aiRequest");
|
|
45
|
+
(0, bun_test_1.describe)("AIRequest 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*/, aiRequest_1.AIRequest.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*/, aiRequest_1.AIRequest.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 an AI request with required fields", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
68
|
+
var request;
|
|
69
|
+
return __generator(this, function (_a) {
|
|
70
|
+
switch (_a.label) {
|
|
71
|
+
case 0: return [4 /*yield*/, aiRequest_1.AIRequest.create({
|
|
72
|
+
aiModel: "gpt-4",
|
|
73
|
+
prompt: "Hello",
|
|
74
|
+
requestType: "general",
|
|
75
|
+
})];
|
|
76
|
+
case 1:
|
|
77
|
+
request = _a.sent();
|
|
78
|
+
(0, bun_test_1.expect)(request.aiModel).toBe("gpt-4");
|
|
79
|
+
(0, bun_test_1.expect)(request.prompt).toBe("Hello");
|
|
80
|
+
(0, bun_test_1.expect)(request.requestType).toBe("general");
|
|
81
|
+
(0, bun_test_1.expect)(request.created).toBeDefined();
|
|
82
|
+
(0, bun_test_1.expect)(request.updated).toBeDefined();
|
|
83
|
+
(0, bun_test_1.expect)(request.deleted).toBe(false);
|
|
84
|
+
return [2 /*return*/];
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}); });
|
|
88
|
+
(0, bun_test_1.it)("should create a request with all fields", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
89
|
+
var userId, request;
|
|
90
|
+
var _a;
|
|
91
|
+
return __generator(this, function (_b) {
|
|
92
|
+
switch (_b.label) {
|
|
93
|
+
case 0:
|
|
94
|
+
userId = new mongoose_1.default.Types.ObjectId();
|
|
95
|
+
return [4 /*yield*/, aiRequest_1.AIRequest.create({
|
|
96
|
+
aiModel: "gemini-2.5-flash",
|
|
97
|
+
error: undefined,
|
|
98
|
+
metadata: { key: "value" },
|
|
99
|
+
prompt: "Translate this",
|
|
100
|
+
requestType: "translation",
|
|
101
|
+
response: "Translated text",
|
|
102
|
+
responseTime: 1500,
|
|
103
|
+
tokensUsed: 100,
|
|
104
|
+
userId: userId,
|
|
105
|
+
})];
|
|
106
|
+
case 1:
|
|
107
|
+
request = _b.sent();
|
|
108
|
+
(0, bun_test_1.expect)(request.aiModel).toBe("gemini-2.5-flash");
|
|
109
|
+
(0, bun_test_1.expect)(request.requestType).toBe("translation");
|
|
110
|
+
(0, bun_test_1.expect)(request.response).toBe("Translated text");
|
|
111
|
+
(0, bun_test_1.expect)(request.responseTime).toBe(1500);
|
|
112
|
+
(0, bun_test_1.expect)(request.tokensUsed).toBe(100);
|
|
113
|
+
(0, bun_test_1.expect)((_a = request.userId) === null || _a === void 0 ? void 0 : _a.toString()).toBe(userId.toString());
|
|
114
|
+
(0, bun_test_1.expect)(request.metadata).toEqual({ key: "value" });
|
|
115
|
+
return [2 /*return*/];
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}); });
|
|
119
|
+
(0, bun_test_1.it)("should reject invalid request types", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
120
|
+
return __generator(this, function (_a) {
|
|
121
|
+
switch (_a.label) {
|
|
122
|
+
case 0: return [4 /*yield*/, (0, bun_test_1.expect)(aiRequest_1.AIRequest.create({
|
|
123
|
+
aiModel: "gpt-4",
|
|
124
|
+
prompt: "test",
|
|
125
|
+
requestType: "invalid",
|
|
126
|
+
})).rejects.toThrow()];
|
|
127
|
+
case 1:
|
|
128
|
+
_a.sent();
|
|
129
|
+
return [2 /*return*/];
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}); });
|
|
133
|
+
});
|
|
134
|
+
(0, bun_test_1.describe)("logRequest static", function () {
|
|
135
|
+
(0, bun_test_1.it)("should create a request via logRequest", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
136
|
+
var request;
|
|
137
|
+
return __generator(this, function (_a) {
|
|
138
|
+
switch (_a.label) {
|
|
139
|
+
case 0: return [4 /*yield*/, aiRequest_1.AIRequest.logRequest({
|
|
140
|
+
aiModel: "gpt-4",
|
|
141
|
+
prompt: "test prompt",
|
|
142
|
+
requestType: "general",
|
|
143
|
+
response: "test response",
|
|
144
|
+
responseTime: 500,
|
|
145
|
+
tokensUsed: 50,
|
|
146
|
+
})];
|
|
147
|
+
case 1:
|
|
148
|
+
request = _a.sent();
|
|
149
|
+
(0, bun_test_1.expect)(request._id).toBeDefined();
|
|
150
|
+
(0, bun_test_1.expect)(request.prompt).toBe("test prompt");
|
|
151
|
+
(0, bun_test_1.expect)(request.response).toBe("test response");
|
|
152
|
+
return [2 /*return*/];
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}); });
|
|
156
|
+
});
|
|
157
|
+
(0, bun_test_1.describe)("soft delete", function () {
|
|
158
|
+
(0, bun_test_1.it)("should filter out deleted records by default", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
159
|
+
var results;
|
|
160
|
+
return __generator(this, function (_a) {
|
|
161
|
+
switch (_a.label) {
|
|
162
|
+
case 0: return [4 /*yield*/, aiRequest_1.AIRequest.create({
|
|
163
|
+
aiModel: "gpt-4",
|
|
164
|
+
deleted: true,
|
|
165
|
+
prompt: "deleted",
|
|
166
|
+
requestType: "general",
|
|
167
|
+
})];
|
|
168
|
+
case 1:
|
|
169
|
+
_a.sent();
|
|
170
|
+
return [4 /*yield*/, aiRequest_1.AIRequest.create({
|
|
171
|
+
aiModel: "gpt-4",
|
|
172
|
+
prompt: "active",
|
|
173
|
+
requestType: "general",
|
|
174
|
+
})];
|
|
175
|
+
case 2:
|
|
176
|
+
_a.sent();
|
|
177
|
+
return [4 /*yield*/, aiRequest_1.AIRequest.find({})];
|
|
178
|
+
case 3:
|
|
179
|
+
results = _a.sent();
|
|
180
|
+
(0, bun_test_1.expect)(results.length).toBe(1);
|
|
181
|
+
(0, bun_test_1.expect)(results[0].prompt).toBe("active");
|
|
182
|
+
return [2 /*return*/];
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}); });
|
|
186
|
+
});
|
|
187
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.FileAttachment = void 0;
|
|
7
|
+
var api_1 = require("@terreno/api");
|
|
8
|
+
var mongoose_1 = __importDefault(require("mongoose"));
|
|
9
|
+
var fileAttachmentSchema = new mongoose_1.default.Schema({
|
|
10
|
+
filename: { required: true, type: String },
|
|
11
|
+
gcsKey: { required: true, type: String, unique: true },
|
|
12
|
+
mimeType: { required: true, type: String },
|
|
13
|
+
size: { required: true, type: Number },
|
|
14
|
+
url: { required: true, type: String },
|
|
15
|
+
userId: { index: true, ref: "User", required: true, type: mongoose_1.default.Schema.Types.ObjectId },
|
|
16
|
+
}, { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
|
|
17
|
+
fileAttachmentSchema.plugin(api_1.createdUpdatedPlugin);
|
|
18
|
+
fileAttachmentSchema.plugin(api_1.isDeletedPlugin);
|
|
19
|
+
fileAttachmentSchema.plugin(api_1.findOneOrNone);
|
|
20
|
+
fileAttachmentSchema.plugin(api_1.findExactlyOne);
|
|
21
|
+
// Virtual ownerId alias so Permissions.IsOwner works with userId field
|
|
22
|
+
fileAttachmentSchema.virtual("ownerId").get(function () {
|
|
23
|
+
return this.userId;
|
|
24
|
+
});
|
|
25
|
+
exports.FileAttachment = mongoose_1.default.model("FileAttachment", fileAttachmentSchema);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,217 @@
|
|
|
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 fileAttachment_1 = require("./fileAttachment");
|
|
45
|
+
(0, bun_test_1.describe)("FileAttachment 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*/, fileAttachment_1.FileAttachment.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*/, fileAttachment_1.FileAttachment.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 file attachment with required fields", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
68
|
+
var userId, attachment;
|
|
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*/, fileAttachment_1.FileAttachment.create({
|
|
74
|
+
filename: "test.pdf",
|
|
75
|
+
gcsKey: "uploads/user123/1234-test.pdf",
|
|
76
|
+
mimeType: "application/pdf",
|
|
77
|
+
size: 1024,
|
|
78
|
+
url: "https://storage.googleapis.com/bucket/uploads/user123/1234-test.pdf",
|
|
79
|
+
userId: userId,
|
|
80
|
+
})];
|
|
81
|
+
case 1:
|
|
82
|
+
attachment = _a.sent();
|
|
83
|
+
(0, bun_test_1.expect)(attachment.filename).toBe("test.pdf");
|
|
84
|
+
(0, bun_test_1.expect)(attachment.gcsKey).toBe("uploads/user123/1234-test.pdf");
|
|
85
|
+
(0, bun_test_1.expect)(attachment.mimeType).toBe("application/pdf");
|
|
86
|
+
(0, bun_test_1.expect)(attachment.size).toBe(1024);
|
|
87
|
+
(0, bun_test_1.expect)(attachment.url).toContain("storage.googleapis.com");
|
|
88
|
+
(0, bun_test_1.expect)(attachment.userId.toString()).toBe(userId.toString());
|
|
89
|
+
(0, bun_test_1.expect)(attachment.created).toBeDefined();
|
|
90
|
+
(0, bun_test_1.expect)(attachment.deleted).toBe(false);
|
|
91
|
+
return [2 /*return*/];
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}); });
|
|
95
|
+
(0, bun_test_1.it)("should require filename", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
96
|
+
var userId;
|
|
97
|
+
return __generator(this, function (_a) {
|
|
98
|
+
switch (_a.label) {
|
|
99
|
+
case 0:
|
|
100
|
+
userId = new mongoose_1.default.Types.ObjectId();
|
|
101
|
+
return [4 /*yield*/, (0, bun_test_1.expect)(fileAttachment_1.FileAttachment.create({
|
|
102
|
+
gcsKey: "key",
|
|
103
|
+
mimeType: "text/plain",
|
|
104
|
+
size: 100,
|
|
105
|
+
url: "https://example.com/file",
|
|
106
|
+
userId: userId,
|
|
107
|
+
})).rejects.toThrow()];
|
|
108
|
+
case 1:
|
|
109
|
+
_a.sent();
|
|
110
|
+
return [2 /*return*/];
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}); });
|
|
114
|
+
(0, bun_test_1.it)("should require gcsKey", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
115
|
+
var userId;
|
|
116
|
+
return __generator(this, function (_a) {
|
|
117
|
+
switch (_a.label) {
|
|
118
|
+
case 0:
|
|
119
|
+
userId = new mongoose_1.default.Types.ObjectId();
|
|
120
|
+
return [4 /*yield*/, (0, bun_test_1.expect)(fileAttachment_1.FileAttachment.create({
|
|
121
|
+
filename: "test.txt",
|
|
122
|
+
mimeType: "text/plain",
|
|
123
|
+
size: 100,
|
|
124
|
+
url: "https://example.com/file",
|
|
125
|
+
userId: userId,
|
|
126
|
+
})).rejects.toThrow()];
|
|
127
|
+
case 1:
|
|
128
|
+
_a.sent();
|
|
129
|
+
return [2 /*return*/];
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}); });
|
|
133
|
+
(0, bun_test_1.it)("should enforce unique gcsKey", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
134
|
+
var userId, data;
|
|
135
|
+
return __generator(this, function (_a) {
|
|
136
|
+
switch (_a.label) {
|
|
137
|
+
case 0:
|
|
138
|
+
userId = new mongoose_1.default.Types.ObjectId();
|
|
139
|
+
data = {
|
|
140
|
+
filename: "test.pdf",
|
|
141
|
+
gcsKey: "uploads/unique-key",
|
|
142
|
+
mimeType: "application/pdf",
|
|
143
|
+
size: 1024,
|
|
144
|
+
url: "https://example.com/file",
|
|
145
|
+
userId: userId,
|
|
146
|
+
};
|
|
147
|
+
return [4 /*yield*/, fileAttachment_1.FileAttachment.create(data)];
|
|
148
|
+
case 1:
|
|
149
|
+
_a.sent();
|
|
150
|
+
return [4 /*yield*/, (0, bun_test_1.expect)(fileAttachment_1.FileAttachment.create(data)).rejects.toThrow()];
|
|
151
|
+
case 2:
|
|
152
|
+
_a.sent();
|
|
153
|
+
return [2 /*return*/];
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}); });
|
|
157
|
+
(0, bun_test_1.it)("should have virtual ownerId field", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
158
|
+
var userId, attachment;
|
|
159
|
+
return __generator(this, function (_a) {
|
|
160
|
+
switch (_a.label) {
|
|
161
|
+
case 0:
|
|
162
|
+
userId = new mongoose_1.default.Types.ObjectId();
|
|
163
|
+
return [4 /*yield*/, fileAttachment_1.FileAttachment.create({
|
|
164
|
+
filename: "test.txt",
|
|
165
|
+
gcsKey: "uploads/key",
|
|
166
|
+
mimeType: "text/plain",
|
|
167
|
+
size: 100,
|
|
168
|
+
url: "https://example.com/file",
|
|
169
|
+
userId: userId,
|
|
170
|
+
})];
|
|
171
|
+
case 1:
|
|
172
|
+
attachment = _a.sent();
|
|
173
|
+
(0, bun_test_1.expect)(attachment.ownerId.toString()).toBe(userId.toString());
|
|
174
|
+
return [2 /*return*/];
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}); });
|
|
178
|
+
});
|
|
179
|
+
(0, bun_test_1.describe)("soft delete", function () {
|
|
180
|
+
(0, bun_test_1.it)("should filter out deleted records by default", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
181
|
+
var userId, results;
|
|
182
|
+
return __generator(this, function (_a) {
|
|
183
|
+
switch (_a.label) {
|
|
184
|
+
case 0:
|
|
185
|
+
userId = new mongoose_1.default.Types.ObjectId();
|
|
186
|
+
return [4 /*yield*/, fileAttachment_1.FileAttachment.create({
|
|
187
|
+
deleted: true,
|
|
188
|
+
filename: "deleted.txt",
|
|
189
|
+
gcsKey: "uploads/deleted",
|
|
190
|
+
mimeType: "text/plain",
|
|
191
|
+
size: 100,
|
|
192
|
+
url: "https://example.com/deleted",
|
|
193
|
+
userId: userId,
|
|
194
|
+
})];
|
|
195
|
+
case 1:
|
|
196
|
+
_a.sent();
|
|
197
|
+
return [4 /*yield*/, fileAttachment_1.FileAttachment.create({
|
|
198
|
+
filename: "active.txt",
|
|
199
|
+
gcsKey: "uploads/active",
|
|
200
|
+
mimeType: "text/plain",
|
|
201
|
+
size: 200,
|
|
202
|
+
url: "https://example.com/active",
|
|
203
|
+
userId: userId,
|
|
204
|
+
})];
|
|
205
|
+
case 2:
|
|
206
|
+
_a.sent();
|
|
207
|
+
return [4 /*yield*/, fileAttachment_1.FileAttachment.find({})];
|
|
208
|
+
case 3:
|
|
209
|
+
results = _a.sent();
|
|
210
|
+
(0, bun_test_1.expect)(results.length).toBe(1);
|
|
211
|
+
(0, bun_test_1.expect)(results[0].filename).toBe("active.txt");
|
|
212
|
+
return [2 /*return*/];
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}); });
|
|
216
|
+
});
|
|
217
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.GptHistory = void 0;
|
|
7
|
+
var api_1 = require("@terreno/api");
|
|
8
|
+
var mongoose_1 = __importDefault(require("mongoose"));
|
|
9
|
+
var contentPartSchema = new mongoose_1.default.Schema({
|
|
10
|
+
filename: { type: String },
|
|
11
|
+
mimeType: { type: String },
|
|
12
|
+
text: { type: String },
|
|
13
|
+
type: { enum: ["text", "image", "file"], required: true, type: String },
|
|
14
|
+
url: { type: String },
|
|
15
|
+
}, { _id: false });
|
|
16
|
+
var gptHistoryPromptSchema = new mongoose_1.default.Schema({
|
|
17
|
+
args: { type: mongoose_1.default.Schema.Types.Mixed },
|
|
18
|
+
content: { type: [contentPartSchema] },
|
|
19
|
+
model: { type: String },
|
|
20
|
+
rating: {
|
|
21
|
+
description: "User feedback rating for this prompt",
|
|
22
|
+
enum: ["up", "down"],
|
|
23
|
+
type: String,
|
|
24
|
+
},
|
|
25
|
+
result: { type: mongoose_1.default.Schema.Types.Mixed },
|
|
26
|
+
text: { required: true, type: String },
|
|
27
|
+
toolCallId: { type: String },
|
|
28
|
+
toolName: { type: String },
|
|
29
|
+
type: {
|
|
30
|
+
enum: ["user", "assistant", "system", "tool-call", "tool-result"],
|
|
31
|
+
required: true,
|
|
32
|
+
type: String,
|
|
33
|
+
},
|
|
34
|
+
}, { _id: false });
|
|
35
|
+
var gptHistorySchema = new mongoose_1.default.Schema({
|
|
36
|
+
prompts: { default: [], type: [gptHistoryPromptSchema] },
|
|
37
|
+
title: { type: String },
|
|
38
|
+
userId: { index: true, ref: "User", required: true, type: mongoose_1.default.Schema.Types.ObjectId },
|
|
39
|
+
}, { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
|
|
40
|
+
gptHistorySchema.plugin(api_1.createdUpdatedPlugin);
|
|
41
|
+
gptHistorySchema.plugin(api_1.isDeletedPlugin);
|
|
42
|
+
gptHistorySchema.plugin(api_1.findOneOrNone);
|
|
43
|
+
gptHistorySchema.plugin(api_1.findExactlyOne);
|
|
44
|
+
// Virtual ownerId alias so Permissions.IsOwner works with userId field
|
|
45
|
+
gptHistorySchema.virtual("ownerId").get(function () {
|
|
46
|
+
return this.userId;
|
|
47
|
+
});
|
|
48
|
+
gptHistorySchema.pre("save", function () {
|
|
49
|
+
if (!this.title && this.prompts && this.prompts.length > 0) {
|
|
50
|
+
var firstAssistant = this.prompts.find(function (p) { return p.type === "assistant"; });
|
|
51
|
+
if (firstAssistant) {
|
|
52
|
+
this.title = firstAssistant.text.substring(0, 50);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
exports.GptHistory = mongoose_1.default.model("GptHistory", gptHistorySchema);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|