@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,209 @@
1
+ import {afterEach, beforeEach, describe, expect, it} from "bun:test";
2
+ import mongoose from "mongoose";
3
+
4
+ import {GptHistory} from "./gptHistory";
5
+
6
+ describe("GptHistory Model", () => {
7
+ beforeEach(async () => {
8
+ await GptHistory.deleteMany({});
9
+ });
10
+
11
+ afterEach(async () => {
12
+ await GptHistory.deleteMany({});
13
+ });
14
+
15
+ describe("schema", () => {
16
+ it("should create a history with required fields", async () => {
17
+ const userId = new mongoose.Types.ObjectId();
18
+ const history = await GptHistory.create({
19
+ prompts: [],
20
+ userId,
21
+ });
22
+
23
+ expect(history.userId.toString()).toBe(userId.toString());
24
+ expect(history.prompts).toEqual([]);
25
+ expect(history.created).toBeDefined();
26
+ expect(history.updated).toBeDefined();
27
+ expect(history.deleted).toBe(false);
28
+ });
29
+
30
+ it("should create a history with prompts", async () => {
31
+ const userId = new mongoose.Types.ObjectId();
32
+ const history = await GptHistory.create({
33
+ prompts: [
34
+ {text: "Hello", type: "user"},
35
+ {model: "gpt-4", text: "Hi there!", type: "assistant"},
36
+ ],
37
+ userId,
38
+ });
39
+
40
+ expect(history.prompts.length).toBe(2);
41
+ expect(history.prompts[0].text).toBe("Hello");
42
+ expect(history.prompts[0].type).toBe("user");
43
+ expect(history.prompts[1].text).toBe("Hi there!");
44
+ expect(history.prompts[1].type).toBe("assistant");
45
+ expect(history.prompts[1].model).toBe("gpt-4");
46
+ });
47
+
48
+ it("should require userId", async () => {
49
+ await expect(GptHistory.create({prompts: []})).rejects.toThrow();
50
+ });
51
+ });
52
+
53
+ describe("auto-title", () => {
54
+ it("should auto-generate title from first assistant response", async () => {
55
+ const userId = new mongoose.Types.ObjectId();
56
+ const history = new GptHistory({
57
+ prompts: [
58
+ {text: "What is AI?", type: "user"},
59
+ {
60
+ text: "Artificial Intelligence is a branch of computer science that aims to create intelligent machines.",
61
+ type: "assistant",
62
+ },
63
+ ],
64
+ userId,
65
+ });
66
+
67
+ await history.save();
68
+
69
+ expect(history.title).toBe("Artificial Intelligence is a branch of computer sc");
70
+ });
71
+
72
+ it("should not overwrite existing title", async () => {
73
+ const userId = new mongoose.Types.ObjectId();
74
+ const history = new GptHistory({
75
+ prompts: [{text: "Some response", type: "assistant"}],
76
+ title: "Custom Title",
77
+ userId,
78
+ });
79
+
80
+ await history.save();
81
+
82
+ expect(history.title).toBe("Custom Title");
83
+ });
84
+
85
+ it("should not set title if no assistant response", async () => {
86
+ const userId = new mongoose.Types.ObjectId();
87
+ const history = new GptHistory({
88
+ prompts: [{text: "Hello", type: "user"}],
89
+ userId,
90
+ });
91
+
92
+ await history.save();
93
+
94
+ expect(history.title).toBeUndefined();
95
+ });
96
+ });
97
+
98
+ describe("multi-modal content", () => {
99
+ it("should store content parts on a prompt", async () => {
100
+ const userId = new mongoose.Types.ObjectId();
101
+ const history = await GptHistory.create({
102
+ prompts: [
103
+ {
104
+ content: [
105
+ {text: "What is in this image?", type: "text"},
106
+ {mimeType: "image/jpeg", type: "image", url: "https://example.com/img.jpg"},
107
+ ],
108
+ text: "What is in this image?",
109
+ type: "user",
110
+ },
111
+ ],
112
+ userId,
113
+ });
114
+
115
+ expect(history.prompts[0].content).toBeDefined();
116
+ expect(history.prompts[0].content?.length).toBe(2);
117
+ expect(history.prompts[0].content?.[0].type).toBe("text");
118
+ expect(history.prompts[0].content?.[1].type).toBe("image");
119
+ const imagePart = history.prompts[0].content?.[1] as {type: string; url: string};
120
+ expect(imagePart.url).toBe("https://example.com/img.jpg");
121
+ });
122
+
123
+ it("should store file content parts", async () => {
124
+ const userId = new mongoose.Types.ObjectId();
125
+ const history = await GptHistory.create({
126
+ prompts: [
127
+ {
128
+ content: [
129
+ {text: "Summarize this PDF", type: "text"},
130
+ {
131
+ filename: "report.pdf",
132
+ mimeType: "application/pdf",
133
+ type: "file",
134
+ url: "https://example.com/report.pdf",
135
+ },
136
+ ],
137
+ text: "Summarize this PDF",
138
+ type: "user",
139
+ },
140
+ ],
141
+ userId,
142
+ });
143
+
144
+ expect(history.prompts[0].content?.[1].type).toBe("file");
145
+ const filePart = history.prompts[0].content?.[1] as {
146
+ type: string;
147
+ filename: string;
148
+ mimeType: string;
149
+ };
150
+ expect(filePart.filename).toBe("report.pdf");
151
+ expect(filePart.mimeType).toBe("application/pdf");
152
+ });
153
+ });
154
+
155
+ describe("tool call prompts", () => {
156
+ it("should store tool-call type prompts", async () => {
157
+ const userId = new mongoose.Types.ObjectId();
158
+ const history = await GptHistory.create({
159
+ prompts: [
160
+ {text: "What time is it?", type: "user"},
161
+ {
162
+ args: {timezone: "UTC"},
163
+ text: "Tool call: get_time",
164
+ toolCallId: "call-123",
165
+ toolName: "get_time",
166
+ type: "tool-call",
167
+ },
168
+ ],
169
+ userId,
170
+ });
171
+
172
+ expect(history.prompts[1].type).toBe("tool-call");
173
+ expect(history.prompts[1].toolName).toBe("get_time");
174
+ expect(history.prompts[1].toolCallId).toBe("call-123");
175
+ expect(history.prompts[1].args).toEqual({timezone: "UTC"});
176
+ });
177
+
178
+ it("should store tool-result type prompts", async () => {
179
+ const userId = new mongoose.Types.ObjectId();
180
+ const history = await GptHistory.create({
181
+ prompts: [
182
+ {
183
+ result: {time: "2024-01-01T00:00:00Z"},
184
+ text: "Tool result: get_time",
185
+ toolCallId: "call-123",
186
+ toolName: "get_time",
187
+ type: "tool-result",
188
+ },
189
+ ],
190
+ userId,
191
+ });
192
+
193
+ expect(history.prompts[0].type).toBe("tool-result");
194
+ expect(history.prompts[0].toolName).toBe("get_time");
195
+ expect(history.prompts[0].result).toEqual({time: "2024-01-01T00:00:00Z"});
196
+ });
197
+ });
198
+
199
+ describe("soft delete", () => {
200
+ it("should filter out deleted records by default", async () => {
201
+ const userId = new mongoose.Types.ObjectId();
202
+ await GptHistory.create({deleted: true, prompts: [], userId});
203
+ await GptHistory.create({prompts: [], userId});
204
+
205
+ const results = await GptHistory.find({});
206
+ expect(results.length).toBe(1);
207
+ });
208
+ });
209
+ });
@@ -0,0 +1,71 @@
1
+ import {createdUpdatedPlugin, findExactlyOne, findOneOrNone, isDeletedPlugin} from "@terreno/api";
2
+ import mongoose from "mongoose";
3
+
4
+ import type {GptHistoryDocument, GptHistoryModel} from "../types";
5
+
6
+ const contentPartSchema = new mongoose.Schema(
7
+ {
8
+ filename: {type: String},
9
+ mimeType: {type: String},
10
+ text: {type: String},
11
+ type: {enum: ["text", "image", "file"], required: true, type: String},
12
+ url: {type: String},
13
+ },
14
+ {_id: false}
15
+ );
16
+
17
+ const gptHistoryPromptSchema = new mongoose.Schema(
18
+ {
19
+ args: {type: mongoose.Schema.Types.Mixed},
20
+ content: {type: [contentPartSchema]},
21
+ model: {type: String},
22
+ rating: {
23
+ description: "User feedback rating for this prompt",
24
+ enum: ["up", "down"],
25
+ type: String,
26
+ },
27
+ result: {type: mongoose.Schema.Types.Mixed},
28
+ text: {required: true, type: String},
29
+ toolCallId: {type: String},
30
+ toolName: {type: String},
31
+ type: {
32
+ enum: ["user", "assistant", "system", "tool-call", "tool-result"],
33
+ required: true,
34
+ type: String,
35
+ },
36
+ },
37
+ {_id: false}
38
+ );
39
+
40
+ const gptHistorySchema = new mongoose.Schema<GptHistoryDocument, GptHistoryModel>(
41
+ {
42
+ prompts: {default: [], type: [gptHistoryPromptSchema]},
43
+ title: {type: String},
44
+ userId: {index: true, ref: "User", required: true, type: mongoose.Schema.Types.ObjectId},
45
+ },
46
+ {strict: "throw", toJSON: {virtuals: true}, toObject: {virtuals: true}}
47
+ );
48
+
49
+ gptHistorySchema.plugin(createdUpdatedPlugin);
50
+ gptHistorySchema.plugin(isDeletedPlugin);
51
+ gptHistorySchema.plugin(findOneOrNone);
52
+ gptHistorySchema.plugin(findExactlyOne);
53
+
54
+ // Virtual ownerId alias so Permissions.IsOwner works with userId field
55
+ gptHistorySchema.virtual("ownerId").get(function () {
56
+ return this.userId;
57
+ });
58
+
59
+ gptHistorySchema.pre("save", function () {
60
+ if (!this.title && this.prompts && this.prompts.length > 0) {
61
+ const firstAssistant = this.prompts.find((p) => p.type === "assistant");
62
+ if (firstAssistant) {
63
+ this.title = firstAssistant.text.substring(0, 50);
64
+ }
65
+ }
66
+ });
67
+
68
+ export const GptHistory = mongoose.model<GptHistoryDocument, GptHistoryModel>(
69
+ "GptHistory",
70
+ gptHistorySchema
71
+ );
@@ -0,0 +1,3 @@
1
+ export {AIRequest} from "./aiRequest";
2
+ export {FileAttachment} from "./fileAttachment";
3
+ export {GptHistory} from "./gptHistory";
@@ -0,0 +1,107 @@
1
+ import {asyncHandler, authenticateMiddleware, createOpenApiBuilder} from "@terreno/api";
2
+ import type express from "express";
3
+
4
+ import {AIRequest} from "../models/aiRequest";
5
+ import type {AiRequestsExplorerRouteOptions} from "../types";
6
+
7
+ export const addAiRequestsExplorerRoutes = (
8
+ router: any,
9
+ options?: AiRequestsExplorerRouteOptions
10
+ ): void => {
11
+ router.get(
12
+ "/aiRequestsExplorer",
13
+ [
14
+ authenticateMiddleware(),
15
+ createOpenApiBuilder(options?.openApiOptions ?? {})
16
+ .withTags(["admin"])
17
+ .withSummary("List AI requests (admin only)")
18
+ .withQueryParameter("page", {type: "number"}, {required: false})
19
+ .withQueryParameter("limit", {type: "number"}, {required: false})
20
+ .withQueryParameter("requestType", {type: "string"}, {required: false})
21
+ .withQueryParameter("model", {type: "string"}, {required: false})
22
+ .withQueryParameter("startDate", {type: "string"}, {required: false})
23
+ .withQueryParameter("endDate", {type: "string"}, {required: false})
24
+ .withResponse(200, {
25
+ data: {items: {type: "object"}, type: "array"},
26
+ limit: {type: "number"},
27
+ more: {type: "boolean"},
28
+ page: {type: "number"},
29
+ total: {type: "number"},
30
+ })
31
+ .build(),
32
+ ],
33
+ asyncHandler(async (req: express.Request, res: express.Response) => {
34
+ const user = (req as any).user;
35
+ if (!user?.admin) {
36
+ return res.status(403).json({error: "Admin access required"});
37
+ }
38
+
39
+ const page = Math.max(1, Number.parseInt(req.query.page as string, 10) || 1);
40
+ const limit = Math.min(
41
+ 100,
42
+ Math.max(1, Number.parseInt(req.query.limit as string, 10) || 20)
43
+ );
44
+ const skip = (page - 1) * limit;
45
+
46
+ const match: Record<string, any> = {deleted: {$ne: true}};
47
+
48
+ if (req.query.requestType) {
49
+ match.requestType = req.query.requestType;
50
+ }
51
+ if (req.query.model) {
52
+ match.aiModel = req.query.model;
53
+ }
54
+ if (req.query.startDate || req.query.endDate) {
55
+ match.created = {};
56
+ if (req.query.startDate) {
57
+ match.created.$gte = new Date(req.query.startDate as string);
58
+ }
59
+ if (req.query.endDate) {
60
+ match.created.$lte = new Date(req.query.endDate as string);
61
+ }
62
+ }
63
+
64
+ const [results, totalCount] = await Promise.all([
65
+ AIRequest.aggregate([
66
+ {$match: match},
67
+ {$sort: {created: -1}},
68
+ {$skip: skip},
69
+ {$limit: limit},
70
+ {
71
+ $lookup: {
72
+ as: "user",
73
+ foreignField: "_id",
74
+ from: "users",
75
+ localField: "userId",
76
+ },
77
+ },
78
+ {$unwind: {path: "$user", preserveNullAndEmptyArrays: true}},
79
+ {
80
+ $project: {
81
+ aiModel: 1,
82
+ created: 1,
83
+ error: 1,
84
+ prompt: {$substrCP: ["$prompt", 0, 200]},
85
+ requestType: 1,
86
+ response: {$substrCP: [{$ifNull: ["$response", ""]}, 0, 200]},
87
+ responseTime: 1,
88
+ tokensUsed: 1,
89
+ "user.email": 1,
90
+ "user.name": 1,
91
+ userId: 1,
92
+ },
93
+ },
94
+ ]),
95
+ AIRequest.countDocuments(match),
96
+ ]);
97
+
98
+ return res.json({
99
+ data: results,
100
+ limit,
101
+ more: skip + limit < totalCount,
102
+ page,
103
+ total: totalCount,
104
+ });
105
+ })
106
+ );
107
+ };
@@ -0,0 +1,128 @@
1
+ import {APIError, asyncHandler, authenticateMiddleware, createOpenApiBuilder} from "@terreno/api";
2
+ import type express from "express";
3
+ import type mongoose from "mongoose";
4
+ import multer from "multer";
5
+
6
+ import {FileAttachment} from "../models/fileAttachment";
7
+ import type {FileStorageService} from "../service/fileStorage";
8
+ import type {FileRouteOptions} from "../types";
9
+
10
+ const ALLOWED_MIME_TYPES = new Set([
11
+ "image/jpeg",
12
+ "image/png",
13
+ "image/gif",
14
+ "image/webp",
15
+ "application/pdf",
16
+ "text/plain",
17
+ "text/csv",
18
+ "application/json",
19
+ ]);
20
+
21
+ const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
22
+
23
+ export const addFileRoutes = (
24
+ router: any,
25
+ options: FileRouteOptions & {fileStorageService: FileStorageService}
26
+ ): void => {
27
+ const {fileStorageService, maxFileSize = DEFAULT_MAX_FILE_SIZE} = options;
28
+
29
+ const upload = multer({
30
+ fileFilter: (_req, file, cb) => {
31
+ if (ALLOWED_MIME_TYPES.has(file.mimetype)) {
32
+ cb(null, true);
33
+ } else {
34
+ cb(new Error(`File type ${file.mimetype} is not allowed`));
35
+ }
36
+ },
37
+ limits: {fileSize: maxFileSize},
38
+ storage: multer.memoryStorage(),
39
+ });
40
+
41
+ router.post(
42
+ "/files/upload",
43
+ [
44
+ authenticateMiddleware(),
45
+ upload.single("file"),
46
+ createOpenApiBuilder(options.openApiOptions ?? {})
47
+ .withTags(["files"])
48
+ .withSummary("Upload a file")
49
+ .withResponse(200, {
50
+ filename: {type: "string"},
51
+ gcsKey: {type: "string"},
52
+ mimeType: {type: "string"},
53
+ size: {type: "number"},
54
+ url: {type: "string"},
55
+ })
56
+ .build(),
57
+ ],
58
+ asyncHandler(async (req: express.Request, res: express.Response) => {
59
+ const file = (req as any).file as Express.Multer.File | undefined;
60
+ const userId = (req as any).user?._id as mongoose.Types.ObjectId;
61
+
62
+ if (!file) {
63
+ throw new APIError({status: 400, title: "No file provided"});
64
+ }
65
+
66
+ const result = await fileStorageService.upload({
67
+ buffer: file.buffer,
68
+ filename: file.originalname,
69
+ mimeType: file.mimetype,
70
+ userId,
71
+ });
72
+
73
+ return res.json({data: result});
74
+ })
75
+ );
76
+
77
+ router.get(
78
+ "/files/:gcsKey(*)",
79
+ [
80
+ createOpenApiBuilder(options.openApiOptions ?? {})
81
+ .withTags(["files"])
82
+ .withSummary("Get file URL")
83
+ .withPathParameter("gcsKey", {type: "string"})
84
+ .withResponse(200, {url: {type: "string"}})
85
+ .build(),
86
+ ],
87
+ asyncHandler(async (req: express.Request, res: express.Response) => {
88
+ const {gcsKey} = req.params;
89
+
90
+ const attachment = await FileAttachment.findOne({deleted: false, gcsKey});
91
+ if (!attachment) {
92
+ throw new APIError({status: 404, title: "File not found"});
93
+ }
94
+
95
+ const url = await fileStorageService.getSignedUrl(gcsKey);
96
+ return res.json({data: {url}});
97
+ })
98
+ );
99
+
100
+ router.delete(
101
+ "/files/:gcsKey(*)",
102
+ [
103
+ authenticateMiddleware(),
104
+ createOpenApiBuilder(options.openApiOptions ?? {})
105
+ .withTags(["files"])
106
+ .withSummary("Delete a file")
107
+ .withPathParameter("gcsKey", {type: "string"})
108
+ .withResponse(200, {success: {type: "boolean"}})
109
+ .build(),
110
+ ],
111
+ asyncHandler(async (req: express.Request, res: express.Response) => {
112
+ const {gcsKey} = req.params;
113
+ const userId = (req as any).user?._id as mongoose.Types.ObjectId;
114
+
115
+ const attachment = await FileAttachment.findOne({deleted: false, gcsKey});
116
+ if (!attachment) {
117
+ throw new APIError({status: 404, title: "File not found"});
118
+ }
119
+
120
+ if (attachment.userId.toString() !== userId.toString()) {
121
+ throw new APIError({status: 403, title: "Not authorized to delete this file"});
122
+ }
123
+
124
+ await fileStorageService.delete(gcsKey);
125
+ return res.json({data: {success: true}});
126
+ })
127
+ );
128
+ };