@terreno/ai 0.13.3 → 0.14.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/dist/langfuseCache.d.ts +5 -6
- package/dist/langfuseCache.js +11 -2
- package/dist/langfuseClient.js +5 -1
- package/dist/models/fileAttachment.js +17 -6
- package/dist/models/gptHistory.js +36 -17
- package/dist/routes/aiRequestsExplorer.d.ts +2 -1
- package/dist/routes/aiRequestsExplorer.js +18 -4
- package/dist/routes/files.d.ts +2 -1
- package/dist/routes/files.js +2 -2
- package/dist/routes/gpt.d.ts +2 -1
- package/dist/routes/gptHistories.d.ts +2 -1
- package/dist/routes/mcp.d.ts +2 -1
- package/dist/routes/mcp.js +2 -5
- package/dist/routes/projects.d.ts +2 -1
- package/dist/service/aiService.d.ts +3 -3
- package/dist/service/fileStorage.js +3 -2
- package/package.json +4 -3
- package/src/langfuseCache.ts +19 -5
- package/src/langfuseClient.test.ts +29 -36
- package/src/langfuseClient.ts +5 -1
- package/src/models/aiRequest.ts +2 -2
- package/src/models/fileAttachment.ts +17 -6
- package/src/models/gptHistory.ts +36 -17
- package/src/routes/aiRequestsExplorer.ts +20 -6
- package/src/routes/files.ts +8 -6
- package/src/routes/gpt.ts +21 -16
- package/src/routes/gptHistories.ts +2 -1
- package/src/routes/mcp.ts +7 -8
- package/src/routes/projects.ts +1 -1
- package/src/routes/routes.test.ts +64 -1
- package/src/service/aiService.test.ts +1 -0
- package/src/service/aiService.ts +4 -4
- package/src/service/fileStorage.ts +3 -2
- package/src/tests/bunSetup.ts +1 -0
- package/src/types/index.ts +2 -0
package/dist/langfuseCache.d.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
1
|
+
import mongoose, { type Document } from "mongoose";
|
|
2
2
|
import type { LangfuseCachedPrompt } from "./langfuseTypes";
|
|
3
3
|
interface LangfuseCacheDocument extends mongoose.Document {
|
|
4
4
|
key: string;
|
|
5
5
|
value: LangfuseCachedPrompt;
|
|
6
6
|
expiresAt: Date;
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
}, any>;
|
|
8
|
+
interface LangfuseCacheModel extends mongoose.Model<LangfuseCacheDocument> {
|
|
9
|
+
findOneOrNone(query: Record<string, unknown>): Promise<(Document & LangfuseCacheDocument) | null>;
|
|
10
|
+
}
|
|
11
|
+
export declare const LangfuseCache: LangfuseCacheModel;
|
|
13
12
|
export declare const getCached: (key: string) => Promise<LangfuseCachedPrompt | null>;
|
|
14
13
|
export declare const setCached: (key: string, value: LangfuseCachedPrompt, ttlSeconds: number) => Promise<void>;
|
|
15
14
|
export declare const invalidateCache: (keyPattern: string) => Promise<void>;
|
package/dist/langfuseCache.js
CHANGED
|
@@ -40,6 +40,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
40
40
|
};
|
|
41
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
42
|
exports.invalidateCache = exports.setCached = exports.getCached = exports.LangfuseCache = void 0;
|
|
43
|
+
var api_1 = require("@terreno/api");
|
|
44
|
+
var luxon_1 = require("luxon");
|
|
43
45
|
var mongoose_1 = __importDefault(require("mongoose"));
|
|
44
46
|
var langfuseCacheSchema = new mongoose_1.default.Schema({
|
|
45
47
|
expiresAt: {
|
|
@@ -60,13 +62,20 @@ var langfuseCacheSchema = new mongoose_1.default.Schema({
|
|
|
60
62
|
},
|
|
61
63
|
}, { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
|
|
62
64
|
langfuseCacheSchema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 });
|
|
65
|
+
langfuseCacheSchema.plugin(api_1.createdUpdatedPlugin);
|
|
66
|
+
langfuseCacheSchema.plugin(api_1.isDeletedPlugin);
|
|
67
|
+
langfuseCacheSchema.plugin(api_1.findOneOrNone);
|
|
68
|
+
langfuseCacheSchema.plugin(api_1.findExactlyOne);
|
|
63
69
|
exports.LangfuseCache = mongoose_1.default.models.LangfuseCache ||
|
|
64
70
|
mongoose_1.default.model("LangfuseCache", langfuseCacheSchema);
|
|
65
71
|
var getCached = function (key) { return __awaiter(void 0, void 0, void 0, function () {
|
|
66
72
|
var entry;
|
|
67
73
|
return __generator(this, function (_a) {
|
|
68
74
|
switch (_a.label) {
|
|
69
|
-
case 0: return [4 /*yield*/, exports.LangfuseCache.
|
|
75
|
+
case 0: return [4 /*yield*/, exports.LangfuseCache.findOneOrNone({
|
|
76
|
+
expiresAt: { $gt: luxon_1.DateTime.now().toJSDate() },
|
|
77
|
+
key: key,
|
|
78
|
+
})];
|
|
70
79
|
case 1:
|
|
71
80
|
entry = _a.sent();
|
|
72
81
|
if (!entry) {
|
|
@@ -82,7 +91,7 @@ var setCached = function (key, value, ttlSeconds) { return __awaiter(void 0, voi
|
|
|
82
91
|
return __generator(this, function (_a) {
|
|
83
92
|
switch (_a.label) {
|
|
84
93
|
case 0:
|
|
85
|
-
expiresAt =
|
|
94
|
+
expiresAt = luxon_1.DateTime.now().plus({ seconds: ttlSeconds }).toJSDate();
|
|
86
95
|
return [4 /*yield*/, exports.LangfuseCache.findOneAndUpdate({ key: key }, { expiresAt: expiresAt, key: key, value: value }, { upsert: true })];
|
|
87
96
|
case 1:
|
|
88
97
|
_a.sent();
|
package/dist/langfuseClient.js
CHANGED
|
@@ -38,6 +38,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.shutdownLangfuseClient = exports.isLangfuseInitialized = exports.getLangfuseClient = exports.initLangfuseClient = void 0;
|
|
40
40
|
var client_1 = require("@langfuse/client");
|
|
41
|
+
var api_1 = require("@terreno/api");
|
|
41
42
|
var langfuseInstance = null;
|
|
42
43
|
var initLangfuseClient = function (options) {
|
|
43
44
|
var _a;
|
|
@@ -51,7 +52,10 @@ var initLangfuseClient = function (options) {
|
|
|
51
52
|
exports.initLangfuseClient = initLangfuseClient;
|
|
52
53
|
var getLangfuseClient = function () {
|
|
53
54
|
if (!langfuseInstance) {
|
|
54
|
-
throw new
|
|
55
|
+
throw new api_1.APIError({
|
|
56
|
+
status: 500,
|
|
57
|
+
title: "Langfuse client not initialized. Call initLangfuseClient first.",
|
|
58
|
+
});
|
|
55
59
|
}
|
|
56
60
|
return langfuseInstance;
|
|
57
61
|
};
|
|
@@ -7,12 +7,23 @@ exports.FileAttachment = void 0;
|
|
|
7
7
|
var api_1 = require("@terreno/api");
|
|
8
8
|
var mongoose_1 = __importDefault(require("mongoose"));
|
|
9
9
|
var fileAttachmentSchema = new mongoose_1.default.Schema({
|
|
10
|
-
filename: { required: true, type: String },
|
|
11
|
-
gcsKey: {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
filename: { description: "Original name of the uploaded file", required: true, type: String },
|
|
11
|
+
gcsKey: {
|
|
12
|
+
description: "Google Cloud Storage key identifying this file",
|
|
13
|
+
required: true,
|
|
14
|
+
type: String,
|
|
15
|
+
unique: true,
|
|
16
|
+
},
|
|
17
|
+
mimeType: { description: "MIME type of the uploaded file", required: true, type: String },
|
|
18
|
+
size: { description: "File size in bytes", required: true, type: Number },
|
|
19
|
+
url: { description: "Public or signed URL for accessing the file", required: true, type: String },
|
|
20
|
+
userId: {
|
|
21
|
+
description: "The user who uploaded this file",
|
|
22
|
+
index: true,
|
|
23
|
+
ref: "User",
|
|
24
|
+
required: true,
|
|
25
|
+
type: mongoose_1.default.Schema.Types.ObjectId,
|
|
26
|
+
},
|
|
16
27
|
}, { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
|
|
17
28
|
fileAttachmentSchema.plugin(api_1.createdUpdatedPlugin);
|
|
18
29
|
fileAttachmentSchema.plugin(api_1.isDeletedPlugin);
|
|
@@ -7,31 +7,40 @@ exports.GptHistory = void 0;
|
|
|
7
7
|
var api_1 = require("@terreno/api");
|
|
8
8
|
var mongoose_1 = __importDefault(require("mongoose"));
|
|
9
9
|
var contentPartSchema = new mongoose_1.default.Schema({
|
|
10
|
-
filename: { type: String },
|
|
11
|
-
mimeType: { type: String },
|
|
12
|
-
text: { type: String },
|
|
13
|
-
type: {
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
filename: { description: "Original filename of the attached file", type: String },
|
|
11
|
+
mimeType: { description: "MIME type of the content part", type: String },
|
|
12
|
+
text: { description: "Text content of this part", type: String },
|
|
13
|
+
type: {
|
|
14
|
+
description: "The kind of content this part represents",
|
|
15
|
+
enum: ["text", "image", "file"],
|
|
16
|
+
required: true,
|
|
17
|
+
type: String,
|
|
18
|
+
},
|
|
19
|
+
url: { description: "URL pointing to the content resource", type: String },
|
|
20
|
+
}, { _id: false, strict: "throw" });
|
|
16
21
|
var gptHistoryPromptSchema = new mongoose_1.default.Schema({
|
|
17
|
-
args: { type: mongoose_1.default.Schema.Types.Mixed },
|
|
18
|
-
content: { type: [contentPartSchema] },
|
|
19
|
-
model: { type: String },
|
|
22
|
+
args: { description: "Arguments passed to a tool call", type: mongoose_1.default.Schema.Types.Mixed },
|
|
23
|
+
content: { description: "Multipart content attached to this prompt", type: [contentPartSchema] },
|
|
24
|
+
model: { description: "AI model identifier used for this prompt", type: String },
|
|
20
25
|
rating: {
|
|
21
26
|
description: "User feedback rating for this prompt",
|
|
22
27
|
enum: ["up", "down"],
|
|
23
28
|
type: String,
|
|
24
29
|
},
|
|
25
|
-
result: { type: mongoose_1.default.Schema.Types.Mixed },
|
|
26
|
-
text: { required: true, type: String },
|
|
27
|
-
toolCallId: {
|
|
28
|
-
|
|
30
|
+
result: { description: "Result returned from a tool call", type: mongoose_1.default.Schema.Types.Mixed },
|
|
31
|
+
text: { description: "Text content of the prompt or response", required: true, type: String },
|
|
32
|
+
toolCallId: {
|
|
33
|
+
description: "Identifier linking a tool result to its originating call",
|
|
34
|
+
type: String,
|
|
35
|
+
},
|
|
36
|
+
toolName: { description: "Name of the tool that was invoked", type: String },
|
|
29
37
|
type: {
|
|
38
|
+
description: "Role of this message in the conversation",
|
|
30
39
|
enum: ["user", "assistant", "system", "tool-call", "tool-result"],
|
|
31
40
|
required: true,
|
|
32
41
|
type: String,
|
|
33
42
|
},
|
|
34
|
-
}, { _id: false });
|
|
43
|
+
}, { _id: false, strict: "throw" });
|
|
35
44
|
var gptHistorySchema = new mongoose_1.default.Schema({
|
|
36
45
|
projectId: {
|
|
37
46
|
description: "Project this conversation belongs to",
|
|
@@ -39,9 +48,19 @@ var gptHistorySchema = new mongoose_1.default.Schema({
|
|
|
39
48
|
ref: "Project",
|
|
40
49
|
type: mongoose_1.default.Schema.Types.ObjectId,
|
|
41
50
|
},
|
|
42
|
-
prompts: {
|
|
43
|
-
|
|
44
|
-
|
|
51
|
+
prompts: {
|
|
52
|
+
default: [],
|
|
53
|
+
description: "Ordered list of messages in this conversation",
|
|
54
|
+
type: [gptHistoryPromptSchema],
|
|
55
|
+
},
|
|
56
|
+
title: { description: "Auto-generated title from the first assistant response", type: String },
|
|
57
|
+
userId: {
|
|
58
|
+
description: "The user who owns this conversation history",
|
|
59
|
+
index: true,
|
|
60
|
+
ref: "User",
|
|
61
|
+
required: true,
|
|
62
|
+
type: mongoose_1.default.Schema.Types.ObjectId,
|
|
63
|
+
},
|
|
45
64
|
}, { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
|
|
46
65
|
gptHistorySchema.plugin(api_1.createdUpdatedPlugin);
|
|
47
66
|
gptHistorySchema.plugin(api_1.isDeletedPlugin);
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
+
import type express from "express";
|
|
1
2
|
import type { AiRequestsExplorerRouteOptions } from "../types";
|
|
2
|
-
export declare const addAiRequestsExplorerRoutes: (router:
|
|
3
|
+
export declare const addAiRequestsExplorerRoutes: (router: express.Router, options?: AiRequestsExplorerRouteOptions) => void;
|
|
@@ -54,6 +54,7 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
54
54
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
55
55
|
exports.addAiRequestsExplorerRoutes = void 0;
|
|
56
56
|
var api_1 = require("@terreno/api");
|
|
57
|
+
var luxon_1 = require("luxon");
|
|
57
58
|
var aiRequest_1 = require("../models/aiRequest");
|
|
58
59
|
var addAiRequestsExplorerRoutes = function (router, options) {
|
|
59
60
|
var _a;
|
|
@@ -77,7 +78,7 @@ var addAiRequestsExplorerRoutes = function (router, options) {
|
|
|
77
78
|
})
|
|
78
79
|
.build(),
|
|
79
80
|
], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
80
|
-
var user, page, limit, skip, match, _a, results, totalCount;
|
|
81
|
+
var user, page, limit, skip, match, startDt, endDt, _a, results, totalCount;
|
|
81
82
|
return __generator(this, function (_b) {
|
|
82
83
|
switch (_b.label) {
|
|
83
84
|
case 0:
|
|
@@ -88,7 +89,9 @@ var addAiRequestsExplorerRoutes = function (router, options) {
|
|
|
88
89
|
page = Math.max(1, Number.parseInt(req.query.page, 10) || 1);
|
|
89
90
|
limit = Math.min(100, Math.max(1, Number.parseInt(req.query.limit, 10) || 20));
|
|
90
91
|
skip = (page - 1) * limit;
|
|
91
|
-
match = {
|
|
92
|
+
match = {
|
|
93
|
+
deleted: { $ne: true },
|
|
94
|
+
};
|
|
92
95
|
if (req.query.requestType) {
|
|
93
96
|
match.requestType = req.query.requestType;
|
|
94
97
|
}
|
|
@@ -98,10 +101,21 @@ var addAiRequestsExplorerRoutes = function (router, options) {
|
|
|
98
101
|
if (req.query.startDate || req.query.endDate) {
|
|
99
102
|
match.created = {};
|
|
100
103
|
if (req.query.startDate) {
|
|
101
|
-
|
|
104
|
+
startDt = luxon_1.DateTime.fromISO(req.query.startDate, { zone: "utc" });
|
|
105
|
+
if (!startDt.isValid) {
|
|
106
|
+
throw new api_1.APIError({
|
|
107
|
+
status: 400,
|
|
108
|
+
title: "Invalid startDate format (expected ISO 8601)",
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
match.created.$gte = startDt.toJSDate();
|
|
102
112
|
}
|
|
103
113
|
if (req.query.endDate) {
|
|
104
|
-
|
|
114
|
+
endDt = luxon_1.DateTime.fromISO(req.query.endDate, { zone: "utc" });
|
|
115
|
+
if (!endDt.isValid) {
|
|
116
|
+
throw new api_1.APIError({ status: 400, title: "Invalid endDate format (expected ISO 8601)" });
|
|
117
|
+
}
|
|
118
|
+
match.created.$lte = endDt.toJSDate();
|
|
105
119
|
}
|
|
106
120
|
}
|
|
107
121
|
return [4 /*yield*/, Promise.all([
|
package/dist/routes/files.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import type express from "express";
|
|
1
2
|
import type { FileStorageService } from "../service/fileStorage";
|
|
2
3
|
import type { FileRouteOptions } from "../types";
|
|
3
|
-
export declare const addFileRoutes: (router:
|
|
4
|
+
export declare const addFileRoutes: (router: express.Router, options: FileRouteOptions & {
|
|
4
5
|
fileStorageService: FileStorageService;
|
|
5
6
|
}) => void;
|
package/dist/routes/files.js
CHANGED
|
@@ -119,7 +119,7 @@ var addFileRoutes = function (router, options) {
|
|
|
119
119
|
switch (_a.label) {
|
|
120
120
|
case 0:
|
|
121
121
|
gcsKey = req.params.gcsKey;
|
|
122
|
-
return [4 /*yield*/, fileAttachment_1.FileAttachment.
|
|
122
|
+
return [4 /*yield*/, fileAttachment_1.FileAttachment.findOneOrNone({ deleted: false, gcsKey: gcsKey })];
|
|
123
123
|
case 1:
|
|
124
124
|
attachment = _a.sent();
|
|
125
125
|
if (!attachment) {
|
|
@@ -148,7 +148,7 @@ var addFileRoutes = function (router, options) {
|
|
|
148
148
|
case 0:
|
|
149
149
|
gcsKey = req.params.gcsKey;
|
|
150
150
|
userId = (_a = req.user) === null || _a === void 0 ? void 0 : _a._id;
|
|
151
|
-
return [4 /*yield*/, fileAttachment_1.FileAttachment.
|
|
151
|
+
return [4 /*yield*/, fileAttachment_1.FileAttachment.findOneOrNone({ deleted: false, gcsKey: gcsKey })];
|
|
152
152
|
case 1:
|
|
153
153
|
attachment = _b.sent();
|
|
154
154
|
if (!attachment) {
|
package/dist/routes/gpt.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { type ModelRouterOptions } from "@terreno/api";
|
|
2
|
+
import type express from "express";
|
|
2
3
|
import type { GptHistoryDocument, GptHistoryRouteOptions } from "../types";
|
|
3
|
-
export declare const addGptHistoryRoutes: (router:
|
|
4
|
+
export declare const addGptHistoryRoutes: (router: express.Router, options?: Partial<ModelRouterOptions<GptHistoryDocument>> & GptHistoryRouteOptions) => void;
|
package/dist/routes/mcp.d.ts
CHANGED
package/dist/routes/mcp.js
CHANGED
|
@@ -94,11 +94,8 @@ var addMcpRoutes = function (router, options) {
|
|
|
94
94
|
tools = _a.sent();
|
|
95
95
|
toolList = Object.entries(tools).map(function (_a) {
|
|
96
96
|
var _b = __read(_a, 2), name = _b[0], tool = _b[1];
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
name: name,
|
|
100
|
-
parameters: tool.parameters,
|
|
101
|
-
});
|
|
97
|
+
var t = tool;
|
|
98
|
+
return { description: t.description, name: name, parameters: t.parameters };
|
|
102
99
|
});
|
|
103
100
|
return [2 /*return*/, res.json({ data: toolList })];
|
|
104
101
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ModelMessage } from "ai";
|
|
1
|
+
import type { LanguageModel, ModelMessage } from "ai";
|
|
2
2
|
import type { AIServiceOptions, GenerateChatStreamOptions, GenerateStreamOptions, GenerateTextOptions, GptHistoryPrompt, RemixOptions, SummaryOptions, TranslateOptions } from "../types";
|
|
3
3
|
export declare const TemperaturePresets: {
|
|
4
4
|
readonly BALANCED: 0.7;
|
|
@@ -9,8 +9,8 @@ export declare const TemperaturePresets: {
|
|
|
9
9
|
readonly MAXIMUM: 2;
|
|
10
10
|
};
|
|
11
11
|
export declare class AIService {
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
readonly model: LanguageModel;
|
|
13
|
+
readonly defaultTemperature: number;
|
|
14
14
|
constructor({ model, defaultTemperature }: AIServiceOptions);
|
|
15
15
|
get modelId(): string;
|
|
16
16
|
private logRequest;
|
|
@@ -54,6 +54,7 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
54
54
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
55
55
|
exports.FileStorageService = void 0;
|
|
56
56
|
var storage_1 = require("@google-cloud/storage");
|
|
57
|
+
var luxon_1 = require("luxon");
|
|
57
58
|
var fileAttachment_1 = require("../models/fileAttachment");
|
|
58
59
|
var FileStorageService = /** @class */ (function () {
|
|
59
60
|
function FileStorageService(_a) {
|
|
@@ -75,7 +76,7 @@ var FileStorageService = /** @class */ (function () {
|
|
|
75
76
|
return __generator(this, function (_c) {
|
|
76
77
|
switch (_c.label) {
|
|
77
78
|
case 0:
|
|
78
|
-
timestamp =
|
|
79
|
+
timestamp = luxon_1.DateTime.now().toMillis();
|
|
79
80
|
sanitizedFilename = filename.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
80
81
|
gcsKey = "uploads/".concat(userId.toString(), "/").concat(timestamp, "-").concat(sanitizedFilename);
|
|
81
82
|
file = this.bucket.file(gcsKey);
|
|
@@ -110,7 +111,7 @@ var FileStorageService = /** @class */ (function () {
|
|
|
110
111
|
file = this.bucket.file(gcsKey);
|
|
111
112
|
return [4 /*yield*/, file.getSignedUrl({
|
|
112
113
|
action: "read",
|
|
113
|
-
expires:
|
|
114
|
+
expires: luxon_1.DateTime.now().plus({ hours: 1 }).toMillis(),
|
|
114
115
|
version: "v4",
|
|
115
116
|
})];
|
|
116
117
|
case 1:
|
package/package.json
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"@opentelemetry/api": "^1.9.0",
|
|
12
12
|
"@opentelemetry/sdk-node": "0.213.0",
|
|
13
13
|
"ai": "^6.0.97",
|
|
14
|
+
"luxon": "^3.7.2",
|
|
14
15
|
"multer": "^1.4.5-lts.1"
|
|
15
16
|
},
|
|
16
17
|
"description": "AI service layer for Terreno apps - GPT chat, request logging, and admin tools.",
|
|
@@ -22,7 +23,7 @@
|
|
|
22
23
|
"@types/multer": "^1.4.12",
|
|
23
24
|
"@types/passport-local-mongoose": "^6.1.5",
|
|
24
25
|
"@types/supertest": "^6.0.2",
|
|
25
|
-
"@terreno/api": "0.
|
|
26
|
+
"@terreno/api": "0.14.0",
|
|
26
27
|
"mongoose": "8.23.0",
|
|
27
28
|
"passport-local-mongoose": "^9.0.1",
|
|
28
29
|
"supertest": "^7.0.0",
|
|
@@ -63,7 +64,7 @@
|
|
|
63
64
|
"access": "public"
|
|
64
65
|
},
|
|
65
66
|
"peerDependencies": {
|
|
66
|
-
"@terreno/api": "0.
|
|
67
|
+
"@terreno/api": "0.14.0",
|
|
67
68
|
"mongoose": "^8.0.0"
|
|
68
69
|
},
|
|
69
70
|
"repository": {
|
|
@@ -82,5 +83,5 @@
|
|
|
82
83
|
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
83
84
|
},
|
|
84
85
|
"types": "dist/index.d.ts",
|
|
85
|
-
"version": "0.
|
|
86
|
+
"version": "0.14.0"
|
|
86
87
|
}
|
package/src/langfuseCache.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {createdUpdatedPlugin, findExactlyOne, findOneOrNone, isDeletedPlugin} from "@terreno/api";
|
|
2
|
+
import {DateTime} from "luxon";
|
|
3
|
+
import mongoose, {type Document} from "mongoose";
|
|
2
4
|
|
|
3
5
|
import type {LangfuseCachedPrompt} from "./langfuseTypes";
|
|
4
6
|
|
|
@@ -8,6 +10,10 @@ interface LangfuseCacheDocument extends mongoose.Document {
|
|
|
8
10
|
expiresAt: Date;
|
|
9
11
|
}
|
|
10
12
|
|
|
13
|
+
interface LangfuseCacheModel extends mongoose.Model<LangfuseCacheDocument> {
|
|
14
|
+
findOneOrNone(query: Record<string, unknown>): Promise<(Document & LangfuseCacheDocument) | null>;
|
|
15
|
+
}
|
|
16
|
+
|
|
11
17
|
const langfuseCacheSchema = new mongoose.Schema<LangfuseCacheDocument>(
|
|
12
18
|
{
|
|
13
19
|
expiresAt: {
|
|
@@ -32,12 +38,20 @@ const langfuseCacheSchema = new mongoose.Schema<LangfuseCacheDocument>(
|
|
|
32
38
|
|
|
33
39
|
langfuseCacheSchema.index({expiresAt: 1}, {expireAfterSeconds: 0});
|
|
34
40
|
|
|
41
|
+
langfuseCacheSchema.plugin(createdUpdatedPlugin);
|
|
42
|
+
langfuseCacheSchema.plugin(isDeletedPlugin);
|
|
43
|
+
langfuseCacheSchema.plugin(findOneOrNone);
|
|
44
|
+
langfuseCacheSchema.plugin(findExactlyOne);
|
|
45
|
+
|
|
35
46
|
export const LangfuseCache =
|
|
36
|
-
(mongoose.models.LangfuseCache as
|
|
37
|
-
mongoose.model<LangfuseCacheDocument>("LangfuseCache", langfuseCacheSchema);
|
|
47
|
+
(mongoose.models.LangfuseCache as LangfuseCacheModel) ||
|
|
48
|
+
mongoose.model<LangfuseCacheDocument, LangfuseCacheModel>("LangfuseCache", langfuseCacheSchema);
|
|
38
49
|
|
|
39
50
|
export const getCached = async (key: string): Promise<LangfuseCachedPrompt | null> => {
|
|
40
|
-
const entry = await LangfuseCache.
|
|
51
|
+
const entry = await LangfuseCache.findOneOrNone({
|
|
52
|
+
expiresAt: {$gt: DateTime.now().toJSDate()},
|
|
53
|
+
key,
|
|
54
|
+
});
|
|
41
55
|
if (!entry) {
|
|
42
56
|
return null;
|
|
43
57
|
}
|
|
@@ -49,7 +63,7 @@ export const setCached = async (
|
|
|
49
63
|
value: LangfuseCachedPrompt,
|
|
50
64
|
ttlSeconds: number
|
|
51
65
|
): Promise<void> => {
|
|
52
|
-
const expiresAt =
|
|
66
|
+
const expiresAt = DateTime.now().plus({seconds: ttlSeconds}).toJSDate();
|
|
53
67
|
await LangfuseCache.findOneAndUpdate({key}, {expiresAt, key, value}, {upsert: true});
|
|
54
68
|
};
|
|
55
69
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {afterEach, describe, expect, it} from "bun:test";
|
|
1
|
+
import {afterEach, describe, expect, it, spyOn} from "bun:test";
|
|
2
|
+
import {LangfuseClient} from "@langfuse/client";
|
|
2
3
|
|
|
3
4
|
import {
|
|
4
5
|
getLangfuseClient,
|
|
@@ -7,65 +8,57 @@ import {
|
|
|
7
8
|
shutdownLangfuseClient,
|
|
8
9
|
} from "./langfuseClient";
|
|
9
10
|
|
|
10
|
-
// @langfuse/client is mocked globally in tests/bunSetup.ts.
|
|
11
|
-
|
|
12
11
|
describe("langfuseClient", () => {
|
|
13
12
|
afterEach(async () => {
|
|
14
13
|
await shutdownLangfuseClient();
|
|
15
14
|
});
|
|
16
15
|
|
|
17
|
-
it("
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("isLangfuseInitialized returns false before init", () => {
|
|
24
|
-
expect(isLangfuseInitialized()).toBe(false);
|
|
16
|
+
it("initLangfuseClient creates a client with the given options", () => {
|
|
17
|
+
const client = initLangfuseClient({publicKey: "pk-test", secretKey: "sk-test"});
|
|
18
|
+
expect(client).toBeInstanceOf(LangfuseClient);
|
|
25
19
|
});
|
|
26
20
|
|
|
27
|
-
it("initLangfuseClient
|
|
28
|
-
const client = initLangfuseClient({
|
|
29
|
-
publicKey: "pk-test",
|
|
30
|
-
secretKey: "sk-test",
|
|
31
|
-
});
|
|
21
|
+
it("initLangfuseClient uses default baseUrl when not provided", () => {
|
|
22
|
+
const client = initLangfuseClient({publicKey: "pk", secretKey: "sk"});
|
|
32
23
|
expect(client).toBeDefined();
|
|
33
24
|
});
|
|
34
25
|
|
|
35
|
-
it("
|
|
26
|
+
it("initLangfuseClient respects a custom baseUrl", () => {
|
|
36
27
|
const client = initLangfuseClient({
|
|
37
|
-
|
|
38
|
-
|
|
28
|
+
baseUrl: "https://custom.langfuse.dev",
|
|
29
|
+
publicKey: "pk",
|
|
30
|
+
secretKey: "sk",
|
|
39
31
|
});
|
|
40
|
-
expect(
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it("uses custom baseUrl when provided", () => {
|
|
44
|
-
const client = initLangfuseClient({
|
|
45
|
-
baseUrl: "https://custom.langfuse.com",
|
|
46
|
-
publicKey: "pk-test",
|
|
47
|
-
secretKey: "sk-test",
|
|
48
|
-
});
|
|
49
|
-
expect((client as unknown as {baseUrl: string}).baseUrl).toBe("https://custom.langfuse.com");
|
|
32
|
+
expect(client).toBeDefined();
|
|
50
33
|
});
|
|
51
34
|
|
|
52
|
-
it("
|
|
53
|
-
|
|
54
|
-
|
|
35
|
+
it("getLangfuseClient throws when not initialized", () => {
|
|
36
|
+
expect(() => getLangfuseClient()).toThrow(
|
|
37
|
+
"Langfuse client not initialized. Call initLangfuseClient first."
|
|
38
|
+
);
|
|
55
39
|
});
|
|
56
40
|
|
|
57
41
|
it("getLangfuseClient returns the initialized client", () => {
|
|
58
|
-
const created = initLangfuseClient({publicKey: "pk
|
|
42
|
+
const created = initLangfuseClient({publicKey: "pk", secretKey: "sk"});
|
|
59
43
|
const retrieved = getLangfuseClient();
|
|
60
44
|
expect(retrieved).toBe(created);
|
|
61
45
|
});
|
|
62
46
|
|
|
63
|
-
it("
|
|
64
|
-
|
|
47
|
+
it("isLangfuseInitialized returns false before init and true after", () => {
|
|
48
|
+
expect(isLangfuseInitialized()).toBe(false);
|
|
49
|
+
initLangfuseClient({publicKey: "pk", secretKey: "sk"});
|
|
50
|
+
expect(isLangfuseInitialized()).toBe(true);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("shutdownLangfuseClient calls shutdown and resets state", async () => {
|
|
54
|
+
const client = initLangfuseClient({publicKey: "pk", secretKey: "sk"});
|
|
55
|
+
const shutdownSpy = spyOn(client, "shutdown").mockResolvedValue(undefined);
|
|
65
56
|
expect(isLangfuseInitialized()).toBe(true);
|
|
57
|
+
|
|
66
58
|
await shutdownLangfuseClient();
|
|
67
59
|
expect(isLangfuseInitialized()).toBe(false);
|
|
68
|
-
expect(
|
|
60
|
+
expect(shutdownSpy).toHaveBeenCalledTimes(1);
|
|
61
|
+
shutdownSpy.mockRestore();
|
|
69
62
|
});
|
|
70
63
|
|
|
71
64
|
it("shutdownLangfuseClient is a no-op when not initialized", async () => {
|
package/src/langfuseClient.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {LangfuseClient} from "@langfuse/client";
|
|
2
|
+
import {APIError} from "@terreno/api";
|
|
2
3
|
|
|
3
4
|
import type {LangfuseAppOptions} from "./langfuseTypes";
|
|
4
5
|
|
|
@@ -15,7 +16,10 @@ export const initLangfuseClient = (options: LangfuseAppOptions): LangfuseClient
|
|
|
15
16
|
|
|
16
17
|
export const getLangfuseClient = (): LangfuseClient => {
|
|
17
18
|
if (!langfuseInstance) {
|
|
18
|
-
throw new
|
|
19
|
+
throw new APIError({
|
|
20
|
+
status: 500,
|
|
21
|
+
title: "Langfuse client not initialized. Call initLangfuseClient first.",
|
|
22
|
+
});
|
|
19
23
|
}
|
|
20
24
|
return langfuseInstance;
|
|
21
25
|
};
|
package/src/models/aiRequest.ts
CHANGED
|
@@ -68,7 +68,7 @@ aiRequestSchema.index({aiModel: 1, created: -1});
|
|
|
68
68
|
aiRequestSchema.index({created: -1, requestType: 1});
|
|
69
69
|
aiRequestSchema.index({parentRequestId: 1});
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
aiRequestSchema.statics.logRequest = async function (
|
|
72
72
|
this: AIRequestModel,
|
|
73
73
|
params: LogRequestParams
|
|
74
74
|
): Promise<AIRequestDocument> {
|
|
@@ -76,7 +76,7 @@ aiRequestSchema.index({parentRequestId: 1});
|
|
|
76
76
|
return request.save();
|
|
77
77
|
};
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
aiRequestSchema.statics.logMultiAgentRequest = async function (
|
|
80
80
|
this: AIRequestModel,
|
|
81
81
|
params: LogMultiAgentRequestParams
|
|
82
82
|
): Promise<AIRequestDocument> {
|
|
@@ -5,12 +5,23 @@ import type {FileAttachmentDocument, FileAttachmentModel} from "../types";
|
|
|
5
5
|
|
|
6
6
|
const fileAttachmentSchema = new mongoose.Schema<FileAttachmentDocument, FileAttachmentModel>(
|
|
7
7
|
{
|
|
8
|
-
filename: {required: true, type: String},
|
|
9
|
-
gcsKey: {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
filename: {description: "Original name of the uploaded file", required: true, type: String},
|
|
9
|
+
gcsKey: {
|
|
10
|
+
description: "Google Cloud Storage key identifying this file",
|
|
11
|
+
required: true,
|
|
12
|
+
type: String,
|
|
13
|
+
unique: true,
|
|
14
|
+
},
|
|
15
|
+
mimeType: {description: "MIME type of the uploaded file", required: true, type: String},
|
|
16
|
+
size: {description: "File size in bytes", required: true, type: Number},
|
|
17
|
+
url: {description: "Public or signed URL for accessing the file", required: true, type: String},
|
|
18
|
+
userId: {
|
|
19
|
+
description: "The user who uploaded this file",
|
|
20
|
+
index: true,
|
|
21
|
+
ref: "User",
|
|
22
|
+
required: true,
|
|
23
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
24
|
+
},
|
|
14
25
|
},
|
|
15
26
|
{strict: "throw", toJSON: {virtuals: true}, toObject: {virtuals: true}}
|
|
16
27
|
);
|
package/src/models/gptHistory.ts
CHANGED
|
@@ -5,36 +5,45 @@ import type {GptHistoryDocument, GptHistoryModel} from "../types";
|
|
|
5
5
|
|
|
6
6
|
const contentPartSchema = new mongoose.Schema(
|
|
7
7
|
{
|
|
8
|
-
filename: {type: String},
|
|
9
|
-
mimeType: {type: String},
|
|
10
|
-
text: {type: String},
|
|
11
|
-
type: {
|
|
12
|
-
|
|
8
|
+
filename: {description: "Original filename of the attached file", type: String},
|
|
9
|
+
mimeType: {description: "MIME type of the content part", type: String},
|
|
10
|
+
text: {description: "Text content of this part", type: String},
|
|
11
|
+
type: {
|
|
12
|
+
description: "The kind of content this part represents",
|
|
13
|
+
enum: ["text", "image", "file"],
|
|
14
|
+
required: true,
|
|
15
|
+
type: String,
|
|
16
|
+
},
|
|
17
|
+
url: {description: "URL pointing to the content resource", type: String},
|
|
13
18
|
},
|
|
14
|
-
{_id: false}
|
|
19
|
+
{_id: false, strict: "throw"}
|
|
15
20
|
);
|
|
16
21
|
|
|
17
22
|
const gptHistoryPromptSchema = new mongoose.Schema(
|
|
18
23
|
{
|
|
19
|
-
args: {type: mongoose.Schema.Types.Mixed},
|
|
20
|
-
content: {type: [contentPartSchema]},
|
|
21
|
-
model: {type: String},
|
|
24
|
+
args: {description: "Arguments passed to a tool call", type: mongoose.Schema.Types.Mixed},
|
|
25
|
+
content: {description: "Multipart content attached to this prompt", type: [contentPartSchema]},
|
|
26
|
+
model: {description: "AI model identifier used for this prompt", type: String},
|
|
22
27
|
rating: {
|
|
23
28
|
description: "User feedback rating for this prompt",
|
|
24
29
|
enum: ["up", "down"],
|
|
25
30
|
type: String,
|
|
26
31
|
},
|
|
27
|
-
result: {type: mongoose.Schema.Types.Mixed},
|
|
28
|
-
text: {required: true, type: String},
|
|
29
|
-
toolCallId: {
|
|
30
|
-
|
|
32
|
+
result: {description: "Result returned from a tool call", type: mongoose.Schema.Types.Mixed},
|
|
33
|
+
text: {description: "Text content of the prompt or response", required: true, type: String},
|
|
34
|
+
toolCallId: {
|
|
35
|
+
description: "Identifier linking a tool result to its originating call",
|
|
36
|
+
type: String,
|
|
37
|
+
},
|
|
38
|
+
toolName: {description: "Name of the tool that was invoked", type: String},
|
|
31
39
|
type: {
|
|
40
|
+
description: "Role of this message in the conversation",
|
|
32
41
|
enum: ["user", "assistant", "system", "tool-call", "tool-result"],
|
|
33
42
|
required: true,
|
|
34
43
|
type: String,
|
|
35
44
|
},
|
|
36
45
|
},
|
|
37
|
-
{_id: false}
|
|
46
|
+
{_id: false, strict: "throw"}
|
|
38
47
|
);
|
|
39
48
|
|
|
40
49
|
const gptHistorySchema = new mongoose.Schema<GptHistoryDocument, GptHistoryModel>(
|
|
@@ -45,9 +54,19 @@ const gptHistorySchema = new mongoose.Schema<GptHistoryDocument, GptHistoryModel
|
|
|
45
54
|
ref: "Project",
|
|
46
55
|
type: mongoose.Schema.Types.ObjectId,
|
|
47
56
|
},
|
|
48
|
-
prompts: {
|
|
49
|
-
|
|
50
|
-
|
|
57
|
+
prompts: {
|
|
58
|
+
default: [],
|
|
59
|
+
description: "Ordered list of messages in this conversation",
|
|
60
|
+
type: [gptHistoryPromptSchema],
|
|
61
|
+
},
|
|
62
|
+
title: {description: "Auto-generated title from the first assistant response", type: String},
|
|
63
|
+
userId: {
|
|
64
|
+
description: "The user who owns this conversation history",
|
|
65
|
+
index: true,
|
|
66
|
+
ref: "User",
|
|
67
|
+
required: true,
|
|
68
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
69
|
+
},
|
|
51
70
|
},
|
|
52
71
|
{strict: "throw", toJSON: {virtuals: true}, toObject: {virtuals: true}}
|
|
53
72
|
);
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {asyncHandler, authenticateMiddleware, createOpenApiBuilder} from "@terreno/api";
|
|
1
|
+
import {APIError, asyncHandler, authenticateMiddleware, createOpenApiBuilder} from "@terreno/api";
|
|
2
2
|
import type express from "express";
|
|
3
|
+
import {DateTime} from "luxon";
|
|
3
4
|
|
|
4
5
|
import {AIRequest} from "../models/aiRequest";
|
|
5
6
|
import type {AiRequestsExplorerRouteOptions} from "../types";
|
|
6
7
|
|
|
7
8
|
export const addAiRequestsExplorerRoutes = (
|
|
8
|
-
router:
|
|
9
|
+
router: express.Router,
|
|
9
10
|
options?: AiRequestsExplorerRouteOptions
|
|
10
11
|
): void => {
|
|
11
12
|
router.get(
|
|
@@ -31,7 +32,7 @@ export const addAiRequestsExplorerRoutes = (
|
|
|
31
32
|
.build(),
|
|
32
33
|
],
|
|
33
34
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
34
|
-
const user =
|
|
35
|
+
const user = req.user as {admin?: boolean} | undefined;
|
|
35
36
|
if (!user?.admin) {
|
|
36
37
|
return res.status(403).json({error: "Admin access required"});
|
|
37
38
|
}
|
|
@@ -43,7 +44,9 @@ export const addAiRequestsExplorerRoutes = (
|
|
|
43
44
|
);
|
|
44
45
|
const skip = (page - 1) * limit;
|
|
45
46
|
|
|
46
|
-
const match: Record<string,
|
|
47
|
+
const match: Record<string, unknown> & {created?: {$gte?: Date; $lte?: Date}} = {
|
|
48
|
+
deleted: {$ne: true},
|
|
49
|
+
};
|
|
47
50
|
|
|
48
51
|
if (req.query.requestType) {
|
|
49
52
|
match.requestType = req.query.requestType;
|
|
@@ -54,10 +57,21 @@ export const addAiRequestsExplorerRoutes = (
|
|
|
54
57
|
if (req.query.startDate || req.query.endDate) {
|
|
55
58
|
match.created = {};
|
|
56
59
|
if (req.query.startDate) {
|
|
57
|
-
|
|
60
|
+
const startDt = DateTime.fromISO(req.query.startDate as string, {zone: "utc"});
|
|
61
|
+
if (!startDt.isValid) {
|
|
62
|
+
throw new APIError({
|
|
63
|
+
status: 400,
|
|
64
|
+
title: "Invalid startDate format (expected ISO 8601)",
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
match.created.$gte = startDt.toJSDate();
|
|
58
68
|
}
|
|
59
69
|
if (req.query.endDate) {
|
|
60
|
-
|
|
70
|
+
const endDt = DateTime.fromISO(req.query.endDate as string, {zone: "utc"});
|
|
71
|
+
if (!endDt.isValid) {
|
|
72
|
+
throw new APIError({status: 400, title: "Invalid endDate format (expected ISO 8601)"});
|
|
73
|
+
}
|
|
74
|
+
match.created.$lte = endDt.toJSDate();
|
|
61
75
|
}
|
|
62
76
|
}
|
|
63
77
|
|
package/src/routes/files.ts
CHANGED
|
@@ -21,7 +21,7 @@ const ALLOWED_MIME_TYPES = new Set([
|
|
|
21
21
|
const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
|
|
22
22
|
|
|
23
23
|
export const addFileRoutes = (
|
|
24
|
-
router:
|
|
24
|
+
router: express.Router,
|
|
25
25
|
options: FileRouteOptions & {fileStorageService: FileStorageService}
|
|
26
26
|
): void => {
|
|
27
27
|
const {fileStorageService, maxFileSize = DEFAULT_MAX_FILE_SIZE} = options;
|
|
@@ -56,8 +56,9 @@ export const addFileRoutes = (
|
|
|
56
56
|
.build(),
|
|
57
57
|
],
|
|
58
58
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
59
|
-
const file = (req as
|
|
60
|
-
const userId = (req as
|
|
59
|
+
const file = (req as express.Request & {file?: Express.Multer.File}).file;
|
|
60
|
+
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)
|
|
61
|
+
?._id as mongoose.Types.ObjectId;
|
|
61
62
|
|
|
62
63
|
if (!file) {
|
|
63
64
|
throw new APIError({status: 400, title: "No file provided"});
|
|
@@ -87,7 +88,7 @@ export const addFileRoutes = (
|
|
|
87
88
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
88
89
|
const gcsKey = req.params.gcsKey as string;
|
|
89
90
|
|
|
90
|
-
const attachment = await FileAttachment.
|
|
91
|
+
const attachment = await FileAttachment.findOneOrNone({deleted: false, gcsKey});
|
|
91
92
|
if (!attachment) {
|
|
92
93
|
throw new APIError({status: 404, title: "File not found"});
|
|
93
94
|
}
|
|
@@ -110,9 +111,10 @@ export const addFileRoutes = (
|
|
|
110
111
|
],
|
|
111
112
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
112
113
|
const gcsKey = req.params.gcsKey as string;
|
|
113
|
-
const userId = (req as
|
|
114
|
+
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)
|
|
115
|
+
?._id as mongoose.Types.ObjectId;
|
|
114
116
|
|
|
115
|
-
const attachment = await FileAttachment.
|
|
117
|
+
const attachment = await FileAttachment.findOneOrNone({deleted: false, gcsKey});
|
|
116
118
|
if (!attachment) {
|
|
117
119
|
throw new APIError({status: 404, title: "File not found"});
|
|
118
120
|
}
|
package/src/routes/gpt.ts
CHANGED
|
@@ -16,7 +16,12 @@ import {GptHistory} from "../models/gptHistory";
|
|
|
16
16
|
import {Project} from "../models/project";
|
|
17
17
|
import {AIService} from "../service/aiService";
|
|
18
18
|
import {TITLE_GENERATION_PROMPT} from "../service/prompts";
|
|
19
|
-
import type {
|
|
19
|
+
import type {
|
|
20
|
+
GptHistoryDocument,
|
|
21
|
+
GptHistoryPrompt,
|
|
22
|
+
GptRouteOptions,
|
|
23
|
+
MessageContentPart,
|
|
24
|
+
} from "../types";
|
|
20
25
|
|
|
21
26
|
const DEMO_RESPONSE =
|
|
22
27
|
"This is demo mode. To use AI features, paste your Gemini API key in Settings.";
|
|
@@ -90,7 +95,7 @@ const generateTitle = async (
|
|
|
90
95
|
}
|
|
91
96
|
};
|
|
92
97
|
|
|
93
|
-
export const addGptRoutes = (router:
|
|
98
|
+
export const addGptRoutes = (router: express.Router, options: GptRouteOptions): void => {
|
|
94
99
|
const {mcpService, tools: routeTools, createRequestTools, toolChoice, maxSteps} = options;
|
|
95
100
|
|
|
96
101
|
router.post(
|
|
@@ -135,7 +140,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
135
140
|
model: requestModel,
|
|
136
141
|
projectId,
|
|
137
142
|
} = req.body;
|
|
138
|
-
const userId = (req as
|
|
143
|
+
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
|
|
139
144
|
|
|
140
145
|
if (!prompt || typeof prompt !== "string") {
|
|
141
146
|
throw new APIError({status: 400, title: "prompt is required"});
|
|
@@ -158,7 +163,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
158
163
|
}
|
|
159
164
|
|
|
160
165
|
// Load or create history
|
|
161
|
-
let history;
|
|
166
|
+
let history: GptHistoryDocument | null;
|
|
162
167
|
if (historyId) {
|
|
163
168
|
history = await GptHistory.findById(historyId);
|
|
164
169
|
if (!history) {
|
|
@@ -228,7 +233,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
228
233
|
if (attachments && Array.isArray(attachments)) {
|
|
229
234
|
logger.debug("Processing attachments", {
|
|
230
235
|
count: attachments.length,
|
|
231
|
-
types: attachments.map((a:
|
|
236
|
+
types: attachments.map((a: {mimeType?: string; type?: string; url?: string}) => ({
|
|
232
237
|
mimeType: a.mimeType,
|
|
233
238
|
type: a.type,
|
|
234
239
|
urlLength: a.url?.length ?? 0,
|
|
@@ -311,7 +316,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
311
316
|
const result = streamText({
|
|
312
317
|
experimental_telemetry: telemetry,
|
|
313
318
|
messages,
|
|
314
|
-
model:
|
|
319
|
+
model: aiService.model,
|
|
315
320
|
providerOptions: !supportsTools
|
|
316
321
|
? {
|
|
317
322
|
google: {responseModalities: ["TEXT", "IMAGE"]},
|
|
@@ -320,7 +325,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
320
325
|
: undefined,
|
|
321
326
|
stopWhen: allTools ? stepCountIs(maxSteps ?? 5) : stepCountIs(1),
|
|
322
327
|
system: effectiveSystemPrompt ?? undefined,
|
|
323
|
-
temperature:
|
|
328
|
+
temperature: aiService.defaultTemperature,
|
|
324
329
|
toolChoice: allTools ? (toolChoice ?? "auto") : undefined,
|
|
325
330
|
tools: allTools,
|
|
326
331
|
});
|
|
@@ -332,7 +337,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
332
337
|
|
|
333
338
|
for await (const part of result.fullStream as AsyncIterable<{
|
|
334
339
|
type: string;
|
|
335
|
-
[key: string]:
|
|
340
|
+
[key: string]: unknown;
|
|
336
341
|
}>) {
|
|
337
342
|
partCount++;
|
|
338
343
|
if (partCount <= 5 || part.type === "error" || part.type === "file") {
|
|
@@ -407,9 +412,9 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
407
412
|
// Persist tool call in history
|
|
408
413
|
history.prompts.push({
|
|
409
414
|
args: part.input as Record<string, unknown>,
|
|
410
|
-
text: `Tool call: ${part.toolName}`,
|
|
411
|
-
toolCallId: part.toolCallId,
|
|
412
|
-
toolName: part.toolName,
|
|
415
|
+
text: `Tool call: ${part.toolName as string}`,
|
|
416
|
+
toolCallId: part.toolCallId as string,
|
|
417
|
+
toolName: part.toolName as string,
|
|
413
418
|
type: "tool-call",
|
|
414
419
|
});
|
|
415
420
|
} else if (part.type === "tool-result") {
|
|
@@ -447,9 +452,9 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
447
452
|
// Persist tool result in history (without the large file data)
|
|
448
453
|
history.prompts.push({
|
|
449
454
|
result: cleanResult as unknown,
|
|
450
|
-
text: `Tool result: ${part.toolName}`,
|
|
451
|
-
toolCallId: part.toolCallId,
|
|
452
|
-
toolName: part.toolName,
|
|
455
|
+
text: `Tool result: ${part.toolName as string}`,
|
|
456
|
+
toolCallId: part.toolCallId as string,
|
|
457
|
+
toolName: part.toolName as string,
|
|
453
458
|
type: "tool-result",
|
|
454
459
|
});
|
|
455
460
|
}
|
|
@@ -615,7 +620,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
615
620
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
616
621
|
const {id} = req.params;
|
|
617
622
|
const {promptIndex, rating} = req.body;
|
|
618
|
-
const userId = (req as
|
|
623
|
+
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
|
|
619
624
|
|
|
620
625
|
if (typeof promptIndex !== "number" || promptIndex < 0) {
|
|
621
626
|
throw new APIError({status: 400, title: "promptIndex must be a non-negative number"});
|
|
@@ -662,7 +667,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
662
667
|
],
|
|
663
668
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
664
669
|
const {text} = req.body;
|
|
665
|
-
const userId = (req as
|
|
670
|
+
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
|
|
666
671
|
|
|
667
672
|
if (!text || typeof text !== "string") {
|
|
668
673
|
throw new APIError({status: 400, title: "text is required"});
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {type ModelRouterOptions, modelRouter, Permissions} from "@terreno/api";
|
|
2
|
+
import type express from "express";
|
|
2
3
|
import {GptHistory} from "../models/gptHistory";
|
|
3
4
|
import type {GptHistoryDocument, GptHistoryRouteOptions} from "../types";
|
|
4
5
|
|
|
5
6
|
export const addGptHistoryRoutes = (
|
|
6
|
-
router:
|
|
7
|
+
router: express.Router,
|
|
7
8
|
options?: Partial<ModelRouterOptions<GptHistoryDocument>> & GptHistoryRouteOptions
|
|
8
9
|
): void => {
|
|
9
10
|
router.use(
|
package/src/routes/mcp.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type express from "express";
|
|
|
3
3
|
|
|
4
4
|
import type {McpRouteOptions} from "../types";
|
|
5
5
|
|
|
6
|
-
export const addMcpRoutes = (router:
|
|
6
|
+
export const addMcpRoutes = (router: express.Router, options: McpRouteOptions): void => {
|
|
7
7
|
const {mcpService} = options;
|
|
8
8
|
|
|
9
9
|
router.get(
|
|
@@ -20,7 +20,7 @@ export const addMcpRoutes = (router: any, options: McpRouteOptions): void => {
|
|
|
20
20
|
.build(),
|
|
21
21
|
],
|
|
22
22
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
23
|
-
const user =
|
|
23
|
+
const user = req.user as {admin?: boolean} | undefined;
|
|
24
24
|
if (!user?.admin) {
|
|
25
25
|
throw new APIError({status: 403, title: "Admin access required"});
|
|
26
26
|
}
|
|
@@ -42,11 +42,10 @@ export const addMcpRoutes = (router: any, options: McpRouteOptions): void => {
|
|
|
42
42
|
],
|
|
43
43
|
asyncHandler(async (_req: express.Request, res: express.Response) => {
|
|
44
44
|
const tools = await mcpService.getTools();
|
|
45
|
-
const toolList = Object.entries(tools).map(([name, tool]) =>
|
|
46
|
-
|
|
47
|
-
name,
|
|
48
|
-
|
|
49
|
-
}));
|
|
45
|
+
const toolList = Object.entries(tools).map(([name, tool]) => {
|
|
46
|
+
const t = tool as {description?: string; parameters?: unknown};
|
|
47
|
+
return {description: t.description, name, parameters: t.parameters};
|
|
48
|
+
});
|
|
50
49
|
return res.json({data: toolList});
|
|
51
50
|
})
|
|
52
51
|
);
|
|
@@ -63,7 +62,7 @@ export const addMcpRoutes = (router: any, options: McpRouteOptions): void => {
|
|
|
63
62
|
.build(),
|
|
64
63
|
],
|
|
65
64
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
66
|
-
const user =
|
|
65
|
+
const user = req.user as {admin?: boolean} | undefined;
|
|
67
66
|
if (!user?.admin) {
|
|
68
67
|
throw new APIError({status: 403, title: "Admin access required"});
|
|
69
68
|
}
|
package/src/routes/projects.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {Project} from "../models/project";
|
|
|
13
13
|
import type {ProjectDocument} from "../types";
|
|
14
14
|
|
|
15
15
|
export const addProjectRoutes = (
|
|
16
|
-
router:
|
|
16
|
+
router: express.Router,
|
|
17
17
|
options?: {openApiOptions?: Record<string, unknown>}
|
|
18
18
|
): void => {
|
|
19
19
|
// Add memory to a project (registered before modelRouter so it isn't shadowed)
|
|
@@ -916,6 +916,54 @@ describe("AI Routes", () => {
|
|
|
916
916
|
await Project.deleteMany({});
|
|
917
917
|
});
|
|
918
918
|
|
|
919
|
+
it("streams text-delta events through the SSE path with a minimal mock model", async () => {
|
|
920
|
+
// Covers the text-delta SSE branch by using a model that emits only
|
|
921
|
+
// text-delta + finish (no step-start/step-end events).
|
|
922
|
+
const noStepModel = {
|
|
923
|
+
doGenerate: mock(async () => ({
|
|
924
|
+
content: [{text: "ok", type: "text" as const}],
|
|
925
|
+
finishReason: "stop" as const,
|
|
926
|
+
usage: {inputTokens: 1, outputTokens: 1},
|
|
927
|
+
})),
|
|
928
|
+
doStream: mock(async () => ({
|
|
929
|
+
stream: new ReadableStream({
|
|
930
|
+
start(controller) {
|
|
931
|
+
controller.enqueue({delta: "buffered tail", id: "t1", type: "text-delta" as const});
|
|
932
|
+
controller.enqueue({
|
|
933
|
+
finishReason: "stop" as const,
|
|
934
|
+
type: "finish" as const,
|
|
935
|
+
usage: {inputTokens: 1, outputTokens: 1},
|
|
936
|
+
});
|
|
937
|
+
controller.close();
|
|
938
|
+
},
|
|
939
|
+
}),
|
|
940
|
+
})),
|
|
941
|
+
modelId: "no-step-model",
|
|
942
|
+
provider: "mock-provider",
|
|
943
|
+
specificationVersion: "v2" as const,
|
|
944
|
+
supportedUrls: {},
|
|
945
|
+
};
|
|
946
|
+
const customApp = setupServer({
|
|
947
|
+
addRoutes: (router, options) => {
|
|
948
|
+
addGptRoutes(router, {
|
|
949
|
+
aiService: new AIService({model: noStepModel as unknown as LanguageModel}),
|
|
950
|
+
openApiOptions: options,
|
|
951
|
+
});
|
|
952
|
+
},
|
|
953
|
+
skipListen: true,
|
|
954
|
+
userModel: UserModel,
|
|
955
|
+
});
|
|
956
|
+
const agent = await authAsUser(customApp, "notAdmin");
|
|
957
|
+
const res = await agent
|
|
958
|
+
.post("/gpt/prompt")
|
|
959
|
+
.send({prompt: "Hi"})
|
|
960
|
+
.buffer(true)
|
|
961
|
+
.parse(sseCollect);
|
|
962
|
+
expect(res.status).toBe(200);
|
|
963
|
+
const body = (res as SseResponse).body;
|
|
964
|
+
expect(body).toContain("buffered tail");
|
|
965
|
+
});
|
|
966
|
+
|
|
919
967
|
it("uses per-request tools via createRequestTools", async () => {
|
|
920
968
|
const createRequestTools = mock(() => ({
|
|
921
969
|
localLookup: {description: "Per-request tool"} as unknown as Tool,
|
|
@@ -1101,7 +1149,6 @@ describe("AI Routes", () => {
|
|
|
1101
1149
|
|
|
1102
1150
|
it("handles AIRequest.logRequest failure gracefully", async () => {
|
|
1103
1151
|
const originalLogRequest = AIRequest.logRequest;
|
|
1104
|
-
// biome-ignore lint/suspicious/noExplicitAny: Override static method for test mock.
|
|
1105
1152
|
AIRequest.logRequest = mock(async () => {
|
|
1106
1153
|
throw new Error("database write failed");
|
|
1107
1154
|
}) as unknown as typeof AIRequest.logRequest;
|
|
@@ -1417,6 +1464,22 @@ describe("AI Routes", () => {
|
|
|
1417
1464
|
expect(res.body.total).toBe(1);
|
|
1418
1465
|
});
|
|
1419
1466
|
|
|
1467
|
+
it("returns 400 for invalid startDate format", async () => {
|
|
1468
|
+
const agent = await authAsUser(app, "admin");
|
|
1469
|
+
const res = await agent.get("/aiRequestsExplorer?startDate=not-a-date");
|
|
1470
|
+
|
|
1471
|
+
expect(res.status).toBe(400);
|
|
1472
|
+
expect(res.body.title).toContain("Invalid startDate");
|
|
1473
|
+
});
|
|
1474
|
+
|
|
1475
|
+
it("returns 400 for invalid endDate format", async () => {
|
|
1476
|
+
const agent = await authAsUser(app, "admin");
|
|
1477
|
+
const res = await agent.get("/aiRequestsExplorer?endDate=not-a-date");
|
|
1478
|
+
|
|
1479
|
+
expect(res.status).toBe(400);
|
|
1480
|
+
expect(res.body.title).toContain("Invalid endDate");
|
|
1481
|
+
});
|
|
1482
|
+
|
|
1420
1483
|
it("filters by startDate and endDate range", async () => {
|
|
1421
1484
|
const a = await AIRequest.create({
|
|
1422
1485
|
aiModel: "gpt-4",
|
package/src/service/aiService.ts
CHANGED
|
@@ -39,8 +39,8 @@ const getModelId = (model: LanguageModel): string => {
|
|
|
39
39
|
};
|
|
40
40
|
|
|
41
41
|
export class AIService {
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
readonly model: LanguageModel;
|
|
43
|
+
readonly defaultTemperature: number;
|
|
44
44
|
|
|
45
45
|
constructor({model, defaultTemperature = TemperaturePresets.DEFAULT}: AIServiceOptions) {
|
|
46
46
|
this.model = model;
|
|
@@ -216,13 +216,13 @@ export class AIService {
|
|
|
216
216
|
parts.push({image: new URL(part.url), mediaType: part.mimeType, type: "image"});
|
|
217
217
|
} else if (part.type === "file") {
|
|
218
218
|
logger.debug("Building file message part", {
|
|
219
|
-
filename:
|
|
219
|
+
filename: part.filename,
|
|
220
220
|
mediaType: part.mimeType,
|
|
221
221
|
urlPrefix: part.url?.substring(0, 50),
|
|
222
222
|
});
|
|
223
223
|
parts.push({
|
|
224
224
|
data: new URL(part.url),
|
|
225
|
-
filename:
|
|
225
|
+
filename: part.filename,
|
|
226
226
|
mediaType: part.mimeType,
|
|
227
227
|
type: "file",
|
|
228
228
|
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {Storage, type StorageOptions} from "@google-cloud/storage";
|
|
2
|
+
import {DateTime} from "luxon";
|
|
2
3
|
|
|
3
4
|
import {FileAttachment} from "../models/fileAttachment";
|
|
4
5
|
|
|
@@ -31,7 +32,7 @@ export class FileStorageService {
|
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
async upload({buffer, filename, mimeType, userId}: UploadFileParams): Promise<UploadFileResult> {
|
|
34
|
-
const timestamp =
|
|
35
|
+
const timestamp = DateTime.now().toMillis();
|
|
35
36
|
const sanitizedFilename = filename.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
36
37
|
const gcsKey = `uploads/${userId.toString()}/${timestamp}-${sanitizedFilename}`;
|
|
37
38
|
|
|
@@ -59,7 +60,7 @@ export class FileStorageService {
|
|
|
59
60
|
const file = this.bucket.file(gcsKey);
|
|
60
61
|
const [url] = await file.getSignedUrl({
|
|
61
62
|
action: "read",
|
|
62
|
-
expires:
|
|
63
|
+
expires: DateTime.now().plus({hours: 1}).toMillis(),
|
|
63
64
|
version: "v4",
|
|
64
65
|
});
|
|
65
66
|
return url;
|
package/src/tests/bunSetup.ts
CHANGED
package/src/types/index.ts
CHANGED
|
@@ -161,6 +161,7 @@ export interface AIServiceOptions {
|
|
|
161
161
|
export interface GenerateTextOptions {
|
|
162
162
|
maxOutputTokens?: number;
|
|
163
163
|
prompt: string;
|
|
164
|
+
// biome-ignore lint/suspicious/noExplicitAny: Vercel AI SDK's StopCondition is generic over the tool set; consumers may pass any tool shape.
|
|
164
165
|
stopWhen?: import("ai").StopCondition<any>;
|
|
165
166
|
systemPrompt?: string;
|
|
166
167
|
temperature?: number;
|
|
@@ -179,6 +180,7 @@ export interface GenerateStreamOptions {
|
|
|
179
180
|
|
|
180
181
|
export interface GenerateChatStreamOptions {
|
|
181
182
|
messages: Array<{content: string; role: "user" | "assistant" | "system"}>;
|
|
183
|
+
// biome-ignore lint/suspicious/noExplicitAny: Vercel AI SDK's StopCondition is generic over the tool set; consumers may pass any tool shape.
|
|
182
184
|
stopWhen?: import("ai").StopCondition<any>;
|
|
183
185
|
systemPrompt?: string;
|
|
184
186
|
toolChoice?: "auto" | "none" | "required";
|