@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/README.md
ADDED
package/dist/aiApp.d.ts
CHANGED
|
@@ -23,6 +23,8 @@ export interface AiAppOptions {
|
|
|
23
23
|
openApiOptions?: Record<string, unknown>;
|
|
24
24
|
/** Tool choice strategy for chat requests. Defaults to "auto" when tools are present. */
|
|
25
25
|
toolChoice?: "auto" | "none" | "required";
|
|
26
|
+
/** Cheap model ID used for generating conversation titles (e.g. "gemini-2.0-flash-lite"). Falls back to the main model if not set. */
|
|
27
|
+
titleModelId?: string;
|
|
26
28
|
/** Tool definitions available to the AI model during chat. */
|
|
27
29
|
tools?: Record<string, Tool>;
|
|
28
30
|
}
|
package/dist/aiApp.js
CHANGED
|
@@ -6,6 +6,7 @@ var files_1 = require("./routes/files");
|
|
|
6
6
|
var gpt_1 = require("./routes/gpt");
|
|
7
7
|
var gptHistories_1 = require("./routes/gptHistories");
|
|
8
8
|
var mcp_1 = require("./routes/mcp");
|
|
9
|
+
var projects_1 = require("./routes/projects");
|
|
9
10
|
/**
|
|
10
11
|
* TerrenoPlugin that mounts all AI routes (GPT chat, history, file uploads, MCP, admin explorer).
|
|
11
12
|
*
|
|
@@ -38,7 +39,7 @@ var AiApp = /** @class */ (function () {
|
|
|
38
39
|
}
|
|
39
40
|
AiApp.prototype.register = function (app) {
|
|
40
41
|
var router = app;
|
|
41
|
-
var _a = this.options, aiService = _a.aiService, createModelFn = _a.createModelFn, demoMode = _a.demoMode, fileStorageService = _a.fileStorageService, gcsBucket = _a.gcsBucket, maxSteps = _a.maxSteps, mcpService = _a.mcpService, openApiOptions = _a.openApiOptions, toolChoice = _a.toolChoice, tools = _a.tools;
|
|
42
|
+
var _a = this.options, aiService = _a.aiService, createModelFn = _a.createModelFn, demoMode = _a.demoMode, fileStorageService = _a.fileStorageService, gcsBucket = _a.gcsBucket, maxSteps = _a.maxSteps, mcpService = _a.mcpService, openApiOptions = _a.openApiOptions, titleModelId = _a.titleModelId, toolChoice = _a.toolChoice, tools = _a.tools;
|
|
42
43
|
(0, gptHistories_1.addGptHistoryRoutes)(router, { openApiOptions: openApiOptions });
|
|
43
44
|
(0, gpt_1.addGptRoutes)(router, {
|
|
44
45
|
aiService: aiService,
|
|
@@ -47,10 +48,12 @@ var AiApp = /** @class */ (function () {
|
|
|
47
48
|
maxSteps: maxSteps,
|
|
48
49
|
mcpService: mcpService,
|
|
49
50
|
openApiOptions: openApiOptions,
|
|
51
|
+
titleModelId: titleModelId,
|
|
50
52
|
toolChoice: toolChoice,
|
|
51
53
|
tools: tools,
|
|
52
54
|
});
|
|
53
55
|
(0, aiRequestsExplorer_1.addAiRequestsExplorerRoutes)(router, { openApiOptions: openApiOptions });
|
|
56
|
+
(0, projects_1.addProjectRoutes)(router, { openApiOptions: openApiOptions });
|
|
54
57
|
if (fileStorageService && gcsBucket) {
|
|
55
58
|
(0, files_1.addFileRoutes)(router, {
|
|
56
59
|
fileStorageService: fileStorageService,
|
package/dist/models/aiRequest.js
CHANGED
|
@@ -43,19 +43,50 @@ exports.AIRequest = void 0;
|
|
|
43
43
|
var api_1 = require("@terreno/api");
|
|
44
44
|
var mongoose_1 = __importDefault(require("mongoose"));
|
|
45
45
|
var aiRequestSchema = new mongoose_1.default.Schema({
|
|
46
|
-
aiModel: {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
aiModel: {
|
|
47
|
+
description: "The AI model identifier used for this request",
|
|
48
|
+
required: true,
|
|
49
|
+
type: String,
|
|
50
|
+
},
|
|
51
|
+
error: { description: "Error message if the request failed", type: String },
|
|
52
|
+
metadata: {
|
|
53
|
+
description: "Additional metadata associated with this request",
|
|
54
|
+
type: mongoose_1.default.Schema.Types.Mixed,
|
|
55
|
+
},
|
|
56
|
+
parentRequestId: {
|
|
57
|
+
description: "Reference to the parent request in a multi-agent workflow",
|
|
58
|
+
ref: "AIRequest",
|
|
59
|
+
type: mongoose_1.default.Schema.Types.ObjectId,
|
|
60
|
+
},
|
|
61
|
+
prompt: { description: "The input prompt sent to the AI model", required: true, type: String },
|
|
50
62
|
requestType: {
|
|
51
|
-
|
|
63
|
+
description: "The type of AI request (e.g. general, translation, summarization)",
|
|
52
64
|
required: true,
|
|
53
65
|
type: String,
|
|
54
66
|
},
|
|
55
|
-
response: { type: String },
|
|
56
|
-
responseTime: { type: Number },
|
|
57
|
-
|
|
58
|
-
|
|
67
|
+
response: { description: "The AI model response text", type: String },
|
|
68
|
+
responseTime: { description: "Response time in milliseconds", type: Number },
|
|
69
|
+
subRequestIds: [
|
|
70
|
+
{
|
|
71
|
+
description: "References to child requests in a multi-agent workflow",
|
|
72
|
+
ref: "AIRequest",
|
|
73
|
+
type: mongoose_1.default.Schema.Types.ObjectId,
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
tokensUsed: { description: "Total tokens consumed by this request", type: Number },
|
|
77
|
+
totalResponseTime: {
|
|
78
|
+
description: "Combined response time across all sub-requests in milliseconds",
|
|
79
|
+
type: Number,
|
|
80
|
+
},
|
|
81
|
+
totalTokensUsed: {
|
|
82
|
+
description: "Combined tokens consumed across all sub-requests",
|
|
83
|
+
type: Number,
|
|
84
|
+
},
|
|
85
|
+
userId: {
|
|
86
|
+
description: "The user who initiated this request",
|
|
87
|
+
ref: "User",
|
|
88
|
+
type: mongoose_1.default.Schema.Types.ObjectId,
|
|
89
|
+
},
|
|
59
90
|
}, { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
|
|
60
91
|
aiRequestSchema.plugin(api_1.createdUpdatedPlugin);
|
|
61
92
|
aiRequestSchema.plugin(api_1.isDeletedPlugin);
|
|
@@ -64,6 +95,7 @@ aiRequestSchema.plugin(api_1.findExactlyOne);
|
|
|
64
95
|
aiRequestSchema.index({ created: -1, userId: 1 });
|
|
65
96
|
aiRequestSchema.index({ aiModel: 1, created: -1 });
|
|
66
97
|
aiRequestSchema.index({ created: -1, requestType: 1 });
|
|
98
|
+
aiRequestSchema.index({ parentRequestId: 1 });
|
|
67
99
|
aiRequestSchema.statics.logRequest = function (params) {
|
|
68
100
|
return __awaiter(this, void 0, void 0, function () {
|
|
69
101
|
var request;
|
|
@@ -73,4 +105,31 @@ aiRequestSchema.statics.logRequest = function (params) {
|
|
|
73
105
|
});
|
|
74
106
|
});
|
|
75
107
|
};
|
|
108
|
+
aiRequestSchema.statics.logMultiAgentRequest = function (params) {
|
|
109
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
110
|
+
var parent;
|
|
111
|
+
var _a;
|
|
112
|
+
return __generator(this, function (_b) {
|
|
113
|
+
switch (_b.label) {
|
|
114
|
+
case 0: return [4 /*yield*/, this.create({
|
|
115
|
+
aiModel: params.aiModel,
|
|
116
|
+
metadata: (_a = params.metadata) !== null && _a !== void 0 ? _a : {},
|
|
117
|
+
prompt: "[multi-agent parent request]",
|
|
118
|
+
requestType: params.requestType,
|
|
119
|
+
response: "",
|
|
120
|
+
subRequestIds: params.subRequestIds,
|
|
121
|
+
totalResponseTime: params.totalResponseTime,
|
|
122
|
+
totalTokensUsed: params.totalTokensUsed,
|
|
123
|
+
userId: params.userId,
|
|
124
|
+
})];
|
|
125
|
+
case 1:
|
|
126
|
+
parent = _b.sent();
|
|
127
|
+
return [4 /*yield*/, this.updateMany({ _id: { $in: params.subRequestIds } }, { $set: { parentRequestId: parent._id } })];
|
|
128
|
+
case 2:
|
|
129
|
+
_b.sent();
|
|
130
|
+
return [2 /*return*/, parent];
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
};
|
|
76
135
|
exports.AIRequest = mongoose_1.default.model("AIRequest", aiRequestSchema);
|
|
@@ -116,16 +116,18 @@ var aiRequest_1 = require("./aiRequest");
|
|
|
116
116
|
}
|
|
117
117
|
});
|
|
118
118
|
}); });
|
|
119
|
-
(0, bun_test_1.it)("should
|
|
119
|
+
(0, bun_test_1.it)("should accept custom request types", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
120
|
+
var request;
|
|
120
121
|
return __generator(this, function (_a) {
|
|
121
122
|
switch (_a.label) {
|
|
122
|
-
case 0: return [4 /*yield*/,
|
|
123
|
+
case 0: return [4 /*yield*/, aiRequest_1.AIRequest.create({
|
|
123
124
|
aiModel: "gpt-4",
|
|
124
125
|
prompt: "test",
|
|
125
|
-
requestType: "
|
|
126
|
-
})
|
|
126
|
+
requestType: "custom-type",
|
|
127
|
+
})];
|
|
127
128
|
case 1:
|
|
128
|
-
_a.sent();
|
|
129
|
+
request = _a.sent();
|
|
130
|
+
(0, bun_test_1.expect)(request.requestType).toBe("custom-type");
|
|
129
131
|
return [2 /*return*/];
|
|
130
132
|
}
|
|
131
133
|
});
|
|
@@ -33,6 +33,12 @@ var gptHistoryPromptSchema = new mongoose_1.default.Schema({
|
|
|
33
33
|
},
|
|
34
34
|
}, { _id: false });
|
|
35
35
|
var gptHistorySchema = new mongoose_1.default.Schema({
|
|
36
|
+
projectId: {
|
|
37
|
+
description: "Project this conversation belongs to",
|
|
38
|
+
index: true,
|
|
39
|
+
ref: "Project",
|
|
40
|
+
type: mongoose_1.default.Schema.Types.ObjectId,
|
|
41
|
+
},
|
|
36
42
|
prompts: { default: [], type: [gptHistoryPromptSchema] },
|
|
37
43
|
title: { type: String },
|
|
38
44
|
userId: { index: true, ref: "User", required: true, type: mongoose_1.default.Schema.Types.ObjectId },
|
|
@@ -45,12 +51,4 @@ gptHistorySchema.plugin(api_1.findExactlyOne);
|
|
|
45
51
|
gptHistorySchema.virtual("ownerId").get(function () {
|
|
46
52
|
return this.userId;
|
|
47
53
|
});
|
|
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
54
|
exports.GptHistory = mongoose_1.default.model("GptHistory", gptHistorySchema);
|
|
@@ -121,8 +121,8 @@ var gptHistory_1 = require("./gptHistory");
|
|
|
121
121
|
});
|
|
122
122
|
}); });
|
|
123
123
|
});
|
|
124
|
-
(0, bun_test_1.describe)("
|
|
125
|
-
(0, bun_test_1.it)("should auto-generate title
|
|
124
|
+
(0, bun_test_1.describe)("title", function () {
|
|
125
|
+
(0, bun_test_1.it)("should not auto-generate title on save", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
126
126
|
var userId, history;
|
|
127
127
|
return __generator(this, function (_a) {
|
|
128
128
|
switch (_a.label) {
|
|
@@ -141,12 +141,12 @@ var gptHistory_1 = require("./gptHistory");
|
|
|
141
141
|
return [4 /*yield*/, history.save()];
|
|
142
142
|
case 1:
|
|
143
143
|
_a.sent();
|
|
144
|
-
(0, bun_test_1.expect)(history.title).
|
|
144
|
+
(0, bun_test_1.expect)(history.title).toBeUndefined();
|
|
145
145
|
return [2 /*return*/];
|
|
146
146
|
}
|
|
147
147
|
});
|
|
148
148
|
}); });
|
|
149
|
-
(0, bun_test_1.it)("should
|
|
149
|
+
(0, bun_test_1.it)("should preserve title when explicitly set", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
150
150
|
var userId, history;
|
|
151
151
|
return __generator(this, function (_a) {
|
|
152
152
|
switch (_a.label) {
|
|
@@ -165,24 +165,6 @@ var gptHistory_1 = require("./gptHistory");
|
|
|
165
165
|
}
|
|
166
166
|
});
|
|
167
167
|
}); });
|
|
168
|
-
(0, bun_test_1.it)("should not set title if no assistant response", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
169
|
-
var userId, history;
|
|
170
|
-
return __generator(this, function (_a) {
|
|
171
|
-
switch (_a.label) {
|
|
172
|
-
case 0:
|
|
173
|
-
userId = new mongoose_1.default.Types.ObjectId();
|
|
174
|
-
history = new gptHistory_1.GptHistory({
|
|
175
|
-
prompts: [{ text: "Hello", type: "user" }],
|
|
176
|
-
userId: userId,
|
|
177
|
-
});
|
|
178
|
-
return [4 /*yield*/, history.save()];
|
|
179
|
-
case 1:
|
|
180
|
-
_a.sent();
|
|
181
|
-
(0, bun_test_1.expect)(history.title).toBeUndefined();
|
|
182
|
-
return [2 /*return*/];
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
}); });
|
|
186
168
|
});
|
|
187
169
|
(0, bun_test_1.describe)("multi-modal content", function () {
|
|
188
170
|
(0, bun_test_1.it)("should store content parts on a prompt", function () { return __awaiter(void 0, void 0, void 0, function () {
|
package/dist/models/index.d.ts
CHANGED
package/dist/models/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GptHistory = exports.FileAttachment = exports.AIRequest = void 0;
|
|
3
|
+
exports.Project = exports.GptHistory = exports.FileAttachment = exports.AIRequest = void 0;
|
|
4
4
|
var aiRequest_1 = require("./aiRequest");
|
|
5
5
|
Object.defineProperty(exports, "AIRequest", { enumerable: true, get: function () { return aiRequest_1.AIRequest; } });
|
|
6
6
|
var fileAttachment_1 = require("./fileAttachment");
|
|
7
7
|
Object.defineProperty(exports, "FileAttachment", { enumerable: true, get: function () { return fileAttachment_1.FileAttachment; } });
|
|
8
8
|
var gptHistory_1 = require("./gptHistory");
|
|
9
9
|
Object.defineProperty(exports, "GptHistory", { enumerable: true, get: function () { return gptHistory_1.GptHistory; } });
|
|
10
|
+
var project_1 = require("./project");
|
|
11
|
+
Object.defineProperty(exports, "Project", { enumerable: true, get: function () { return project_1.Project; } });
|
|
@@ -0,0 +1,47 @@
|
|
|
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.Project = void 0;
|
|
7
|
+
var api_1 = require("@terreno/api");
|
|
8
|
+
var mongoose_1 = __importDefault(require("mongoose"));
|
|
9
|
+
var memorySchema = new mongoose_1.default.Schema({
|
|
10
|
+
category: { description: "Optional grouping category for this memory", type: String },
|
|
11
|
+
source: {
|
|
12
|
+
default: "user",
|
|
13
|
+
description: "How this memory was created",
|
|
14
|
+
enum: ["user", "auto"],
|
|
15
|
+
type: String,
|
|
16
|
+
},
|
|
17
|
+
text: { description: "The memory content", required: true, type: String },
|
|
18
|
+
}, { timestamps: { createdAt: "created", updatedAt: false } });
|
|
19
|
+
var projectSchema = new mongoose_1.default.Schema({
|
|
20
|
+
memories: {
|
|
21
|
+
default: [],
|
|
22
|
+
description: "Persistent memories for this project",
|
|
23
|
+
type: [memorySchema],
|
|
24
|
+
},
|
|
25
|
+
name: { description: "Project name", required: true, type: String },
|
|
26
|
+
systemContext: {
|
|
27
|
+
default: "",
|
|
28
|
+
description: "Persistent system instructions prepended to every conversation in this project",
|
|
29
|
+
type: String,
|
|
30
|
+
},
|
|
31
|
+
userId: {
|
|
32
|
+
description: "The user who owns this project",
|
|
33
|
+
index: true,
|
|
34
|
+
ref: "User",
|
|
35
|
+
required: true,
|
|
36
|
+
type: mongoose_1.default.Schema.Types.ObjectId,
|
|
37
|
+
},
|
|
38
|
+
}, { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
|
|
39
|
+
projectSchema.plugin(api_1.createdUpdatedPlugin);
|
|
40
|
+
projectSchema.plugin(api_1.isDeletedPlugin);
|
|
41
|
+
projectSchema.plugin(api_1.findOneOrNone);
|
|
42
|
+
projectSchema.plugin(api_1.findExactlyOne);
|
|
43
|
+
// Virtual ownerId alias so Permissions.IsOwner works with userId field
|
|
44
|
+
projectSchema.virtual("ownerId").get(function () {
|
|
45
|
+
return this.userId;
|
|
46
|
+
});
|
|
47
|
+
exports.Project = mongoose_1.default.model("Project", projectSchema);
|
package/dist/routes/files.js
CHANGED
|
@@ -106,7 +106,7 @@ var addFileRoutes = function (router, options) {
|
|
|
106
106
|
}
|
|
107
107
|
});
|
|
108
108
|
}); }));
|
|
109
|
-
router.get("/files
|
|
109
|
+
router.get("/files/*gcsKey", [
|
|
110
110
|
(0, api_1.createOpenApiBuilder)((_b = options.openApiOptions) !== null && _b !== void 0 ? _b : {})
|
|
111
111
|
.withTags(["files"])
|
|
112
112
|
.withSummary("Get file URL")
|
|
@@ -132,7 +132,7 @@ var addFileRoutes = function (router, options) {
|
|
|
132
132
|
}
|
|
133
133
|
});
|
|
134
134
|
}); }));
|
|
135
|
-
router.delete("/files
|
|
135
|
+
router.delete("/files/*gcsKey", [
|
|
136
136
|
(0, api_1.authenticateMiddleware)(),
|
|
137
137
|
(0, api_1.createOpenApiBuilder)((_c = options.openApiOptions) !== null && _c !== void 0 ? _c : {})
|
|
138
138
|
.withTags(["files"])
|