@terreno/ai 0.3.2 → 0.5.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.
Files changed (49) hide show
  1. package/README.md +5 -0
  2. package/dist/aiApp.d.ts +2 -0
  3. package/dist/aiApp.js +4 -1
  4. package/dist/models/aiRequest.js +68 -9
  5. package/dist/models/aiRequest.test.js +7 -5
  6. package/dist/models/gptHistory.js +6 -8
  7. package/dist/models/gptHistory.test.js +4 -22
  8. package/dist/models/index.d.ts +1 -0
  9. package/dist/models/index.js +3 -1
  10. package/dist/models/project.d.ts +2 -0
  11. package/dist/models/project.js +47 -0
  12. package/dist/routes/files.js +2 -2
  13. package/dist/routes/gpt.js +368 -128
  14. package/dist/routes/gptHistories.js +1 -1
  15. package/dist/routes/index.d.ts +1 -0
  16. package/dist/routes/index.js +3 -1
  17. package/dist/routes/projects.d.ts +3 -0
  18. package/dist/routes/projects.js +147 -0
  19. package/dist/service/aiService.js +21 -5
  20. package/dist/service/fileStorage.d.ts +3 -1
  21. package/dist/service/fileStorage.js +2 -2
  22. package/dist/service/index.d.ts +2 -1
  23. package/dist/service/index.js +2 -1
  24. package/dist/service/prompts.d.ts +1 -0
  25. package/dist/service/prompts.js +3 -1
  26. package/dist/service/webSearchTool.d.ts +39 -0
  27. package/dist/service/webSearchTool.js +2 -0
  28. package/dist/types/index.d.ts +39 -2
  29. package/dist/types/index.js +7 -2
  30. package/package.json +5 -5
  31. package/src/aiApp.ts +6 -0
  32. package/src/models/aiRequest.test.ts +8 -8
  33. package/src/models/aiRequest.ts +68 -10
  34. package/src/models/gptHistory.test.ts +4 -16
  35. package/src/models/gptHistory.ts +6 -9
  36. package/src/models/index.ts +1 -0
  37. package/src/models/project.ts +54 -0
  38. package/src/routes/files.ts +4 -4
  39. package/src/routes/gpt.ts +493 -295
  40. package/src/routes/gptHistories.ts +1 -1
  41. package/src/routes/index.ts +1 -0
  42. package/src/routes/mcp.ts +1 -1
  43. package/src/routes/projects.ts +115 -0
  44. package/src/service/aiService.ts +22 -6
  45. package/src/service/fileStorage.ts +3 -3
  46. package/src/service/index.ts +2 -0
  47. package/src/service/prompts.ts +4 -0
  48. package/src/service/webSearchTool.ts +40 -0
  49. package/src/types/index.ts +54 -2
@@ -1,23 +1,59 @@
1
1
  import {createdUpdatedPlugin, findExactlyOne, findOneOrNone, isDeletedPlugin} from "@terreno/api";
2
2
  import mongoose from "mongoose";
3
3
 
4
- import type {AIRequestDocument, AIRequestModel, LogRequestParams} from "../types";
4
+ import type {
5
+ AIRequestDocument,
6
+ AIRequestModel,
7
+ LogMultiAgentRequestParams,
8
+ LogRequestParams,
9
+ } from "../types";
5
10
 
6
11
  const aiRequestSchema = new mongoose.Schema<AIRequestDocument, AIRequestModel>(
7
12
  {
8
- aiModel: {required: true, type: String},
9
- error: {type: String},
10
- metadata: {type: mongoose.Schema.Types.Mixed},
11
- prompt: {required: true, type: String},
13
+ aiModel: {
14
+ description: "The AI model identifier used for this request",
15
+ required: true,
16
+ type: String,
17
+ },
18
+ error: {description: "Error message if the request failed", type: String},
19
+ metadata: {
20
+ description: "Additional metadata associated with this request",
21
+ type: mongoose.Schema.Types.Mixed,
22
+ },
23
+ parentRequestId: {
24
+ description: "Reference to the parent request in a multi-agent workflow",
25
+ ref: "AIRequest",
26
+ type: mongoose.Schema.Types.ObjectId,
27
+ },
28
+ prompt: {description: "The input prompt sent to the AI model", required: true, type: String},
12
29
  requestType: {
13
- enum: ["general", "remix", "summarization", "translation"],
30
+ description: "The type of AI request (e.g. general, translation, summarization)",
14
31
  required: true,
15
32
  type: String,
16
33
  },
17
- response: {type: String},
18
- responseTime: {type: Number},
19
- tokensUsed: {type: Number},
20
- userId: {ref: "User", type: mongoose.Schema.Types.ObjectId},
34
+ response: {description: "The AI model response text", type: String},
35
+ responseTime: {description: "Response time in milliseconds", type: Number},
36
+ subRequestIds: [
37
+ {
38
+ description: "References to child requests in a multi-agent workflow",
39
+ ref: "AIRequest",
40
+ type: mongoose.Schema.Types.ObjectId,
41
+ },
42
+ ],
43
+ tokensUsed: {description: "Total tokens consumed by this request", type: Number},
44
+ totalResponseTime: {
45
+ description: "Combined response time across all sub-requests in milliseconds",
46
+ type: Number,
47
+ },
48
+ totalTokensUsed: {
49
+ description: "Combined tokens consumed across all sub-requests",
50
+ type: Number,
51
+ },
52
+ userId: {
53
+ description: "The user who initiated this request",
54
+ ref: "User",
55
+ type: mongoose.Schema.Types.ObjectId,
56
+ },
21
57
  },
22
58
  {strict: "throw", toJSON: {virtuals: true}, toObject: {virtuals: true}}
23
59
  );
@@ -30,6 +66,7 @@ aiRequestSchema.plugin(findExactlyOne);
30
66
  aiRequestSchema.index({created: -1, userId: 1});
31
67
  aiRequestSchema.index({aiModel: 1, created: -1});
32
68
  aiRequestSchema.index({created: -1, requestType: 1});
69
+ aiRequestSchema.index({parentRequestId: 1});
33
70
 
34
71
  (aiRequestSchema.statics as any).logRequest = async function (
35
72
  this: AIRequestModel,
@@ -39,6 +76,27 @@ aiRequestSchema.index({created: -1, requestType: 1});
39
76
  return request.save();
40
77
  };
41
78
 
79
+ (aiRequestSchema.statics as any).logMultiAgentRequest = async function (
80
+ this: AIRequestModel,
81
+ params: LogMultiAgentRequestParams
82
+ ): Promise<AIRequestDocument> {
83
+ const parent = await this.create({
84
+ aiModel: params.aiModel,
85
+ metadata: params.metadata ?? {},
86
+ prompt: "[multi-agent parent request]",
87
+ requestType: params.requestType,
88
+ response: "",
89
+ subRequestIds: params.subRequestIds,
90
+ totalResponseTime: params.totalResponseTime,
91
+ totalTokensUsed: params.totalTokensUsed,
92
+ userId: params.userId,
93
+ });
94
+
95
+ await this.updateMany({_id: {$in: params.subRequestIds}}, {$set: {parentRequestId: parent._id}});
96
+
97
+ return parent;
98
+ };
99
+
42
100
  export const AIRequest = mongoose.model<AIRequestDocument, AIRequestModel>(
43
101
  "AIRequest",
44
102
  aiRequestSchema
@@ -50,8 +50,8 @@ describe("GptHistory Model", () => {
50
50
  });
51
51
  });
52
52
 
53
- describe("auto-title", () => {
54
- it("should auto-generate title from first assistant response", async () => {
53
+ describe("title", () => {
54
+ it("should not auto-generate title on save", async () => {
55
55
  const userId = new mongoose.Types.ObjectId();
56
56
  const history = new GptHistory({
57
57
  prompts: [
@@ -66,10 +66,10 @@ describe("GptHistory Model", () => {
66
66
 
67
67
  await history.save();
68
68
 
69
- expect(history.title).toBe("Artificial Intelligence is a branch of computer sc");
69
+ expect(history.title).toBeUndefined();
70
70
  });
71
71
 
72
- it("should not overwrite existing title", async () => {
72
+ it("should preserve title when explicitly set", async () => {
73
73
  const userId = new mongoose.Types.ObjectId();
74
74
  const history = new GptHistory({
75
75
  prompts: [{text: "Some response", type: "assistant"}],
@@ -81,18 +81,6 @@ describe("GptHistory Model", () => {
81
81
 
82
82
  expect(history.title).toBe("Custom Title");
83
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
84
  });
97
85
 
98
86
  describe("multi-modal content", () => {
@@ -39,6 +39,12 @@ const gptHistoryPromptSchema = new mongoose.Schema(
39
39
 
40
40
  const gptHistorySchema = new mongoose.Schema<GptHistoryDocument, GptHistoryModel>(
41
41
  {
42
+ projectId: {
43
+ description: "Project this conversation belongs to",
44
+ index: true,
45
+ ref: "Project",
46
+ type: mongoose.Schema.Types.ObjectId,
47
+ },
42
48
  prompts: {default: [], type: [gptHistoryPromptSchema]},
43
49
  title: {type: String},
44
50
  userId: {index: true, ref: "User", required: true, type: mongoose.Schema.Types.ObjectId},
@@ -56,15 +62,6 @@ gptHistorySchema.virtual("ownerId").get(function () {
56
62
  return this.userId;
57
63
  });
58
64
 
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
65
  export const GptHistory = mongoose.model<GptHistoryDocument, GptHistoryModel>(
69
66
  "GptHistory",
70
67
  gptHistorySchema
@@ -1,3 +1,4 @@
1
1
  export {AIRequest} from "./aiRequest";
2
2
  export {FileAttachment} from "./fileAttachment";
3
3
  export {GptHistory} from "./gptHistory";
4
+ export {Project} from "./project";
@@ -0,0 +1,54 @@
1
+ import {createdUpdatedPlugin, findExactlyOne, findOneOrNone, isDeletedPlugin} from "@terreno/api";
2
+ import mongoose from "mongoose";
3
+
4
+ import type {ProjectDocument, ProjectModel} from "../types";
5
+
6
+ const memorySchema = new mongoose.Schema(
7
+ {
8
+ category: {description: "Optional grouping category for this memory", type: String},
9
+ source: {
10
+ default: "user",
11
+ description: "How this memory was created",
12
+ enum: ["user", "auto"],
13
+ type: String,
14
+ },
15
+ text: {description: "The memory content", required: true, type: String},
16
+ },
17
+ {timestamps: {createdAt: "created", updatedAt: false}}
18
+ );
19
+
20
+ const projectSchema = new mongoose.Schema<ProjectDocument, ProjectModel>(
21
+ {
22
+ memories: {
23
+ default: [],
24
+ description: "Persistent memories for this project",
25
+ type: [memorySchema],
26
+ },
27
+ name: {description: "Project name", required: true, type: String},
28
+ systemContext: {
29
+ default: "",
30
+ description: "Persistent system instructions prepended to every conversation in this project",
31
+ type: String,
32
+ },
33
+ userId: {
34
+ description: "The user who owns this project",
35
+ index: true,
36
+ ref: "User",
37
+ required: true,
38
+ type: mongoose.Schema.Types.ObjectId,
39
+ },
40
+ },
41
+ {strict: "throw", toJSON: {virtuals: true}, toObject: {virtuals: true}}
42
+ );
43
+
44
+ projectSchema.plugin(createdUpdatedPlugin);
45
+ projectSchema.plugin(isDeletedPlugin);
46
+ projectSchema.plugin(findOneOrNone);
47
+ projectSchema.plugin(findExactlyOne);
48
+
49
+ // Virtual ownerId alias so Permissions.IsOwner works with userId field
50
+ projectSchema.virtual("ownerId").get(function () {
51
+ return this.userId;
52
+ });
53
+
54
+ export const Project = mongoose.model<ProjectDocument, ProjectModel>("Project", projectSchema);
@@ -75,7 +75,7 @@ export const addFileRoutes = (
75
75
  );
76
76
 
77
77
  router.get(
78
- "/files/:gcsKey(*)",
78
+ "/files/*gcsKey",
79
79
  [
80
80
  createOpenApiBuilder(options.openApiOptions ?? {})
81
81
  .withTags(["files"])
@@ -85,7 +85,7 @@ export const addFileRoutes = (
85
85
  .build(),
86
86
  ],
87
87
  asyncHandler(async (req: express.Request, res: express.Response) => {
88
- const {gcsKey} = req.params;
88
+ const gcsKey = req.params.gcsKey as string;
89
89
 
90
90
  const attachment = await FileAttachment.findOne({deleted: false, gcsKey});
91
91
  if (!attachment) {
@@ -98,7 +98,7 @@ export const addFileRoutes = (
98
98
  );
99
99
 
100
100
  router.delete(
101
- "/files/:gcsKey(*)",
101
+ "/files/*gcsKey",
102
102
  [
103
103
  authenticateMiddleware(),
104
104
  createOpenApiBuilder(options.openApiOptions ?? {})
@@ -109,7 +109,7 @@ export const addFileRoutes = (
109
109
  .build(),
110
110
  ],
111
111
  asyncHandler(async (req: express.Request, res: express.Response) => {
112
- const {gcsKey} = req.params;
112
+ const gcsKey = req.params.gcsKey as string;
113
113
  const userId = (req as any).user?._id as mongoose.Types.ObjectId;
114
114
 
115
115
  const attachment = await FileAttachment.findOne({deleted: false, gcsKey});