@terreno/ai 57.3.0 → 57.6.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.
- package/README.md +2 -2
- package/dist/aiAdminApp.js +9 -10
- package/dist/aiApp.js +31 -31
- package/dist/langfuseApp.js +33 -76
- package/dist/langfuseCache.js +20 -79
- package/dist/langfuseClient.js +13 -58
- package/dist/langfusePrompts.js +70 -173
- package/dist/langfuseRoutesEvaluations.js +26 -73
- package/dist/langfuseRoutesMiddleware.js +4 -4
- package/dist/langfuseRoutesPlayground.js +29 -99
- package/dist/langfuseRoutesPrompts.js +48 -121
- package/dist/langfuseRoutesTraces.js +24 -76
- package/dist/langfuseTracing.js +15 -60
- package/dist/langfuseVercelAi.js +38 -87
- package/dist/models/aiRequest.js +19 -72
- package/dist/models/fileAttachment.js +3 -3
- package/dist/models/gptHistory.js +5 -5
- package/dist/models/project.js +4 -4
- package/dist/routes/aiRequestsExplorer.js +89 -150
- package/dist/routes/files.js +50 -115
- package/dist/routes/gpt.js +457 -694
- package/dist/routes/gptHistories.js +17 -19
- package/dist/routes/mcp.js +30 -102
- package/dist/routes/projects.js +53 -116
- package/dist/service/aiService.js +393 -706
- package/dist/service/fileStorage.js +42 -134
- package/dist/service/gemini.js +55 -130
- package/dist/service/getMCPTools.js +17 -86
- package/dist/service/mcpService.js +87 -258
- package/dist/service/parseAiJson.js +32 -32
- package/dist/service/vertex.js +129 -283
- package/package.json +30 -24
- package/src/langfuseApp.ts +6 -1
- package/src/tests/helpers.ts +3 -3
- package/tsconfig.json +6 -21
|
@@ -4,9 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.GptHistory = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
const api_1 = require("@terreno/api");
|
|
8
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
9
|
+
const contentPartSchema = new mongoose_1.default.Schema({
|
|
10
10
|
filename: { description: "Original filename of the attached file", type: String },
|
|
11
11
|
mimeType: { description: "MIME type of the content part", type: String },
|
|
12
12
|
text: { description: "Text content of this part", type: String },
|
|
@@ -18,7 +18,7 @@ var contentPartSchema = new mongoose_1.default.Schema({
|
|
|
18
18
|
},
|
|
19
19
|
url: { description: "URL pointing to the content resource", type: String },
|
|
20
20
|
}, { _id: false, strict: "throw" });
|
|
21
|
-
|
|
21
|
+
const gptHistoryPromptSchema = new mongoose_1.default.Schema({
|
|
22
22
|
args: { description: "Arguments passed to a tool call", type: mongoose_1.default.Schema.Types.Mixed },
|
|
23
23
|
content: { description: "Multipart content attached to this prompt", type: [contentPartSchema] },
|
|
24
24
|
model: { description: "AI model identifier used for this prompt", type: String },
|
|
@@ -41,7 +41,7 @@ var gptHistoryPromptSchema = new mongoose_1.default.Schema({
|
|
|
41
41
|
type: String,
|
|
42
42
|
},
|
|
43
43
|
}, { _id: false, strict: "throw" });
|
|
44
|
-
|
|
44
|
+
const gptHistorySchema = new mongoose_1.default.Schema({
|
|
45
45
|
projectId: {
|
|
46
46
|
description: "Project this conversation belongs to",
|
|
47
47
|
index: true,
|
package/dist/models/project.js
CHANGED
|
@@ -4,9 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Project = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
const api_1 = require("@terreno/api");
|
|
8
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
9
|
+
const memorySchema = new mongoose_1.default.Schema({
|
|
10
10
|
category: { description: "Optional grouping category for this memory", type: String },
|
|
11
11
|
source: {
|
|
12
12
|
default: "user",
|
|
@@ -16,7 +16,7 @@ var memorySchema = new mongoose_1.default.Schema({
|
|
|
16
16
|
},
|
|
17
17
|
text: { description: "The memory content", required: true, type: String },
|
|
18
18
|
}, { strict: "throw", timestamps: { createdAt: "created", updatedAt: false } });
|
|
19
|
-
|
|
19
|
+
const projectSchema = new mongoose_1.default.Schema({
|
|
20
20
|
memories: {
|
|
21
21
|
default: [],
|
|
22
22
|
description: "Persistent memories for this project",
|
|
@@ -1,78 +1,25 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
55
3
|
exports.addAiRequestsExplorerRoutes = void 0;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
4
|
+
const api_1 = require("@terreno/api");
|
|
5
|
+
const luxon_1 = require("luxon");
|
|
6
|
+
const aiRequest_1 = require("../models/aiRequest");
|
|
7
|
+
const parseRequestTypes = (raw) => {
|
|
60
8
|
if (Array.isArray(raw)) {
|
|
61
|
-
return raw.flatMap(
|
|
9
|
+
return raw.flatMap((item) => parseRequestTypes(item));
|
|
62
10
|
}
|
|
63
11
|
if (typeof raw !== "string") {
|
|
64
12
|
return [];
|
|
65
13
|
}
|
|
66
14
|
return raw
|
|
67
15
|
.split(",")
|
|
68
|
-
.map(
|
|
69
|
-
.filter(
|
|
16
|
+
.map((token) => token.trim())
|
|
17
|
+
.filter((token) => token.length > 0);
|
|
70
18
|
};
|
|
71
|
-
|
|
72
|
-
var _a;
|
|
19
|
+
const addAiRequestsExplorerRoutes = (router, options) => {
|
|
73
20
|
router.get("/aiRequestsExplorer", [
|
|
74
21
|
(0, api_1.authenticateMiddleware)(),
|
|
75
|
-
(0, api_1.createOpenApiBuilder)(
|
|
22
|
+
(0, api_1.createOpenApiBuilder)(options?.openApiOptions ?? {})
|
|
76
23
|
.withTags(["admin"])
|
|
77
24
|
.withSummary("List AI requests (admin only)")
|
|
78
25
|
.withQueryParameter("page", { type: "number" }, { required: false })
|
|
@@ -89,95 +36,87 @@ var addAiRequestsExplorerRoutes = function (router, options) {
|
|
|
89
36
|
total: { type: "number" },
|
|
90
37
|
})
|
|
91
38
|
.build(),
|
|
92
|
-
], (0, api_1.asyncHandler)(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
if (!endDt.isValid) {
|
|
132
|
-
throw new api_1.APIError({ status: 400, title: "Invalid endDate format (expected ISO 8601)" });
|
|
133
|
-
}
|
|
134
|
-
match.created.$lte = endDt.toJSDate();
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return [4 /*yield*/, Promise.all([
|
|
138
|
-
aiRequest_1.AIRequest.aggregate([
|
|
139
|
-
{ $match: match },
|
|
140
|
-
{ $sort: { created: -1 } },
|
|
141
|
-
{ $skip: skip },
|
|
142
|
-
{ $limit: limit },
|
|
143
|
-
{
|
|
144
|
-
$lookup: {
|
|
145
|
-
as: "user",
|
|
146
|
-
foreignField: "_id",
|
|
147
|
-
from: "users",
|
|
148
|
-
localField: "userId",
|
|
149
|
-
},
|
|
150
|
-
},
|
|
151
|
-
{ $unwind: { path: "$user", preserveNullAndEmptyArrays: true } },
|
|
152
|
-
{
|
|
153
|
-
$project: {
|
|
154
|
-
aiModel: 1,
|
|
155
|
-
created: 1,
|
|
156
|
-
error: 1,
|
|
157
|
-
prompt: { $substrCP: ["$prompt", 0, 200] },
|
|
158
|
-
requestType: 1,
|
|
159
|
-
response: { $substrCP: [{ $ifNull: ["$response", ""] }, 0, 200] },
|
|
160
|
-
responseTime: 1,
|
|
161
|
-
tokensUsed: 1,
|
|
162
|
-
"user.email": 1,
|
|
163
|
-
"user.name": 1,
|
|
164
|
-
userId: 1,
|
|
165
|
-
},
|
|
166
|
-
},
|
|
167
|
-
]),
|
|
168
|
-
aiRequest_1.AIRequest.countDocuments(match),
|
|
169
|
-
])];
|
|
170
|
-
case 1:
|
|
171
|
-
_a = __read.apply(void 0, [_b.sent(), 2]), results = _a[0], totalCount = _a[1];
|
|
172
|
-
return [2 /*return*/, res.json({
|
|
173
|
-
data: results,
|
|
174
|
-
limit: limit,
|
|
175
|
-
more: skip + limit < totalCount,
|
|
176
|
-
page: page,
|
|
177
|
-
total: totalCount,
|
|
178
|
-
})];
|
|
39
|
+
], (0, api_1.asyncHandler)(async (req, res) => {
|
|
40
|
+
const user = req.user;
|
|
41
|
+
if (!user?.admin) {
|
|
42
|
+
return res.status(403).json({ error: "Admin access required" });
|
|
43
|
+
}
|
|
44
|
+
const page = Math.max(1, Number.parseInt(req.query.page, 10) || 1);
|
|
45
|
+
const limit = Math.min(100, Math.max(1, Number.parseInt(req.query.limit, 10) || 20));
|
|
46
|
+
const skip = (page - 1) * limit;
|
|
47
|
+
const match = {
|
|
48
|
+
deleted: { $ne: true },
|
|
49
|
+
};
|
|
50
|
+
const requestTypes = parseRequestTypes(req.query.requestType);
|
|
51
|
+
if (requestTypes.length === 1) {
|
|
52
|
+
match.requestType = requestTypes[0];
|
|
53
|
+
}
|
|
54
|
+
else if (requestTypes.length > 1) {
|
|
55
|
+
match.requestType = { $in: requestTypes };
|
|
56
|
+
}
|
|
57
|
+
if (req.query.model) {
|
|
58
|
+
match.aiModel = req.query.model;
|
|
59
|
+
}
|
|
60
|
+
if (req.query.startDate || req.query.endDate) {
|
|
61
|
+
match.created = {};
|
|
62
|
+
if (req.query.startDate) {
|
|
63
|
+
const startDt = luxon_1.DateTime.fromISO(req.query.startDate, { zone: "utc" });
|
|
64
|
+
if (!startDt.isValid) {
|
|
65
|
+
throw new api_1.APIError({
|
|
66
|
+
status: 400,
|
|
67
|
+
title: "Invalid startDate format (expected ISO 8601)",
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
match.created.$gte = startDt.toJSDate();
|
|
71
|
+
}
|
|
72
|
+
if (req.query.endDate) {
|
|
73
|
+
const endDt = luxon_1.DateTime.fromISO(req.query.endDate, { zone: "utc" });
|
|
74
|
+
if (!endDt.isValid) {
|
|
75
|
+
throw new api_1.APIError({ status: 400, title: "Invalid endDate format (expected ISO 8601)" });
|
|
76
|
+
}
|
|
77
|
+
match.created.$lte = endDt.toJSDate();
|
|
179
78
|
}
|
|
79
|
+
}
|
|
80
|
+
const [results, totalCount] = await Promise.all([
|
|
81
|
+
aiRequest_1.AIRequest.aggregate([
|
|
82
|
+
{ $match: match },
|
|
83
|
+
{ $sort: { created: -1 } },
|
|
84
|
+
{ $skip: skip },
|
|
85
|
+
{ $limit: limit },
|
|
86
|
+
{
|
|
87
|
+
$lookup: {
|
|
88
|
+
as: "user",
|
|
89
|
+
foreignField: "_id",
|
|
90
|
+
from: "users",
|
|
91
|
+
localField: "userId",
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
{ $unwind: { path: "$user", preserveNullAndEmptyArrays: true } },
|
|
95
|
+
{
|
|
96
|
+
$project: {
|
|
97
|
+
aiModel: 1,
|
|
98
|
+
created: 1,
|
|
99
|
+
error: 1,
|
|
100
|
+
prompt: { $substrCP: ["$prompt", 0, 200] },
|
|
101
|
+
requestType: 1,
|
|
102
|
+
response: { $substrCP: [{ $ifNull: ["$response", ""] }, 0, 200] },
|
|
103
|
+
responseTime: 1,
|
|
104
|
+
tokensUsed: 1,
|
|
105
|
+
"user.email": 1,
|
|
106
|
+
"user.name": 1,
|
|
107
|
+
userId: 1,
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
]),
|
|
111
|
+
aiRequest_1.AIRequest.countDocuments(match),
|
|
112
|
+
]);
|
|
113
|
+
return res.json({
|
|
114
|
+
data: results,
|
|
115
|
+
limit,
|
|
116
|
+
more: skip + limit < totalCount,
|
|
117
|
+
page,
|
|
118
|
+
total: totalCount,
|
|
180
119
|
});
|
|
181
|
-
})
|
|
120
|
+
}));
|
|
182
121
|
};
|
|
183
122
|
exports.addAiRequestsExplorerRoutes = addAiRequestsExplorerRoutes;
|
package/dist/routes/files.js
CHANGED
|
@@ -1,49 +1,13 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
4
|
};
|
|
41
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
6
|
exports.addFileRoutes = void 0;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
7
|
+
const api_1 = require("@terreno/api");
|
|
8
|
+
const multer_1 = __importDefault(require("multer"));
|
|
9
|
+
const fileAttachment_1 = require("../models/fileAttachment");
|
|
10
|
+
const ALLOWED_MIME_TYPES = new Set([
|
|
47
11
|
"image/jpeg",
|
|
48
12
|
"image/png",
|
|
49
13
|
"image/gif",
|
|
@@ -53,17 +17,16 @@ var ALLOWED_MIME_TYPES = new Set([
|
|
|
53
17
|
"text/csv",
|
|
54
18
|
"application/json",
|
|
55
19
|
]);
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
fileFilter: function (_req, file, cb) {
|
|
20
|
+
const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
|
|
21
|
+
const addFileRoutes = (router, options) => {
|
|
22
|
+
const { fileStorageService, maxFileSize = DEFAULT_MAX_FILE_SIZE } = options;
|
|
23
|
+
const upload = (0, multer_1.default)({
|
|
24
|
+
fileFilter: (_req, file, cb) => {
|
|
62
25
|
if (ALLOWED_MIME_TYPES.has(file.mimetype)) {
|
|
63
26
|
cb(null, true);
|
|
64
27
|
}
|
|
65
28
|
else {
|
|
66
|
-
cb(new Error(
|
|
29
|
+
cb(new Error(`File type ${file.mimetype} is not allowed`));
|
|
67
30
|
}
|
|
68
31
|
},
|
|
69
32
|
limits: { fileSize: maxFileSize },
|
|
@@ -72,7 +35,7 @@ var addFileRoutes = function (router, options) {
|
|
|
72
35
|
router.post("/files/upload", [
|
|
73
36
|
(0, api_1.authenticateMiddleware)(),
|
|
74
37
|
upload.single("file"),
|
|
75
|
-
(0, api_1.createOpenApiBuilder)(
|
|
38
|
+
(0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
|
|
76
39
|
.withTags(["files"])
|
|
77
40
|
.withSummary("Upload a file")
|
|
78
41
|
.withResponse(200, {
|
|
@@ -83,86 +46,58 @@ var addFileRoutes = function (router, options) {
|
|
|
83
46
|
url: { type: "string" },
|
|
84
47
|
})
|
|
85
48
|
.build(),
|
|
86
|
-
], (0, api_1.asyncHandler)(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
buffer: file.buffer,
|
|
99
|
-
filename: file.originalname,
|
|
100
|
-
mimeType: file.mimetype,
|
|
101
|
-
userId: userId,
|
|
102
|
-
})];
|
|
103
|
-
case 1:
|
|
104
|
-
result = _b.sent();
|
|
105
|
-
return [2 /*return*/, res.json({ data: result })];
|
|
106
|
-
}
|
|
49
|
+
], (0, api_1.asyncHandler)(async (req, res) => {
|
|
50
|
+
const file = req.file;
|
|
51
|
+
const userId = req.user
|
|
52
|
+
?._id;
|
|
53
|
+
if (!file) {
|
|
54
|
+
throw new api_1.APIError({ status: 400, title: "No file provided" });
|
|
55
|
+
}
|
|
56
|
+
const result = await fileStorageService.upload({
|
|
57
|
+
buffer: file.buffer,
|
|
58
|
+
filename: file.originalname,
|
|
59
|
+
mimeType: file.mimetype,
|
|
60
|
+
userId,
|
|
107
61
|
});
|
|
108
|
-
|
|
62
|
+
return res.json({ data: result });
|
|
63
|
+
}));
|
|
109
64
|
router.get("/files/*gcsKey", [
|
|
110
|
-
(0, api_1.createOpenApiBuilder)(
|
|
65
|
+
(0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
|
|
111
66
|
.withTags(["files"])
|
|
112
67
|
.withSummary("Get file URL")
|
|
113
68
|
.withPathParameter("gcsKey", { type: "string" })
|
|
114
69
|
.withResponse(200, { url: { type: "string" } })
|
|
115
70
|
.build(),
|
|
116
|
-
], (0, api_1.asyncHandler)(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (!attachment) {
|
|
126
|
-
throw new api_1.APIError({ status: 404, title: "File not found" });
|
|
127
|
-
}
|
|
128
|
-
return [4 /*yield*/, fileStorageService.getSignedUrl(gcsKey)];
|
|
129
|
-
case 2:
|
|
130
|
-
url = _a.sent();
|
|
131
|
-
return [2 /*return*/, res.json({ data: { url: url } })];
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
}); }));
|
|
71
|
+
], (0, api_1.asyncHandler)(async (req, res) => {
|
|
72
|
+
const gcsKey = req.params.gcsKey;
|
|
73
|
+
const attachment = await fileAttachment_1.FileAttachment.findOneOrNone({ deleted: false, gcsKey });
|
|
74
|
+
if (!attachment) {
|
|
75
|
+
throw new api_1.APIError({ status: 404, title: "File not found" });
|
|
76
|
+
}
|
|
77
|
+
const url = await fileStorageService.getSignedUrl(gcsKey);
|
|
78
|
+
return res.json({ data: { url } });
|
|
79
|
+
}));
|
|
135
80
|
router.delete("/files/*gcsKey", [
|
|
136
81
|
(0, api_1.authenticateMiddleware)(),
|
|
137
|
-
(0, api_1.createOpenApiBuilder)(
|
|
82
|
+
(0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
|
|
138
83
|
.withTags(["files"])
|
|
139
84
|
.withSummary("Delete a file")
|
|
140
85
|
.withPathParameter("gcsKey", { type: "string" })
|
|
141
86
|
.withResponse(200, { success: { type: "boolean" } })
|
|
142
87
|
.build(),
|
|
143
|
-
], (0, api_1.asyncHandler)(
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (attachment.userId.toString() !== userId.toString()) {
|
|
158
|
-
throw new api_1.APIError({ status: 403, title: "Not authorized to delete this file" });
|
|
159
|
-
}
|
|
160
|
-
return [4 /*yield*/, fileStorageService.delete(gcsKey)];
|
|
161
|
-
case 2:
|
|
162
|
-
_b.sent();
|
|
163
|
-
return [2 /*return*/, res.json({ data: { success: true } })];
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
}); }));
|
|
88
|
+
], (0, api_1.asyncHandler)(async (req, res) => {
|
|
89
|
+
const gcsKey = req.params.gcsKey;
|
|
90
|
+
const userId = req.user
|
|
91
|
+
?._id;
|
|
92
|
+
const attachment = await fileAttachment_1.FileAttachment.findOneOrNone({ deleted: false, gcsKey });
|
|
93
|
+
if (!attachment) {
|
|
94
|
+
throw new api_1.APIError({ status: 404, title: "File not found" });
|
|
95
|
+
}
|
|
96
|
+
if (attachment.userId.toString() !== userId.toString()) {
|
|
97
|
+
throw new api_1.APIError({ status: 403, title: "Not authorized to delete this file" });
|
|
98
|
+
}
|
|
99
|
+
await fileStorageService.delete(gcsKey);
|
|
100
|
+
return res.json({ data: { success: true } });
|
|
101
|
+
}));
|
|
167
102
|
};
|
|
168
103
|
exports.addFileRoutes = addFileRoutes;
|