@terreno/ai 0.3.2 → 0.4.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/README.md +5 -0
- package/dist/aiApp.d.ts +2 -0
- package/dist/aiApp.js +4 -1
- package/dist/models/aiRequest.js +68 -9
- package/dist/models/aiRequest.test.js +7 -5
- package/dist/models/gptHistory.js +6 -8
- package/dist/models/gptHistory.test.js +4 -22
- package/dist/models/index.d.ts +1 -0
- package/dist/models/index.js +3 -1
- package/dist/models/project.d.ts +2 -0
- package/dist/models/project.js +47 -0
- package/dist/routes/files.js +2 -2
- package/dist/routes/gpt.js +368 -128
- package/dist/routes/gptHistories.js +1 -1
- package/dist/routes/index.d.ts +1 -0
- package/dist/routes/index.js +3 -1
- package/dist/routes/projects.d.ts +3 -0
- package/dist/routes/projects.js +147 -0
- package/dist/service/aiService.js +21 -5
- package/dist/service/fileStorage.d.ts +3 -1
- package/dist/service/fileStorage.js +2 -2
- package/dist/service/index.d.ts +2 -1
- package/dist/service/index.js +2 -1
- package/dist/service/prompts.d.ts +1 -0
- package/dist/service/prompts.js +3 -1
- package/dist/service/webSearchTool.d.ts +39 -0
- package/dist/service/webSearchTool.js +2 -0
- package/dist/types/index.d.ts +39 -2
- package/dist/types/index.js +7 -2
- package/package.json +5 -5
- package/src/aiApp.ts +6 -0
- package/src/models/aiRequest.test.ts +8 -8
- package/src/models/aiRequest.ts +68 -10
- package/src/models/gptHistory.test.ts +4 -16
- package/src/models/gptHistory.ts +6 -9
- package/src/models/index.ts +1 -0
- package/src/models/project.ts +54 -0
- package/src/routes/files.ts +4 -4
- package/src/routes/gpt.ts +493 -295
- package/src/routes/gptHistories.ts +1 -1
- package/src/routes/index.ts +1 -0
- package/src/routes/mcp.ts +1 -1
- package/src/routes/projects.ts +115 -0
- package/src/service/aiService.ts +22 -6
- package/src/service/fileStorage.ts +3 -3
- package/src/service/index.ts +2 -0
- package/src/service/prompts.ts +4 -0
- package/src/service/webSearchTool.ts +40 -0
- package/src/types/index.ts +54 -2
package/src/models/aiRequest.ts
CHANGED
|
@@ -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 {
|
|
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: {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
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
|
-
|
|
20
|
-
|
|
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("
|
|
54
|
-
it("should auto-generate title
|
|
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).
|
|
69
|
+
expect(history.title).toBeUndefined();
|
|
70
70
|
});
|
|
71
71
|
|
|
72
|
-
it("should
|
|
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", () => {
|
package/src/models/gptHistory.ts
CHANGED
|
@@ -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
|
package/src/models/index.ts
CHANGED
|
@@ -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);
|
package/src/routes/files.ts
CHANGED
|
@@ -75,7 +75,7 @@ export const addFileRoutes = (
|
|
|
75
75
|
);
|
|
76
76
|
|
|
77
77
|
router.get(
|
|
78
|
-
"/files
|
|
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
|
|
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
|
|
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
|
|
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});
|