@terreno/ai 0.13.3 → 0.14.1
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/models/project.js +1 -1
- 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/models/project.ts +1 -1
- 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 +174 -1
- package/src/service/aiService.test.ts +11 -9
- 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);
|
package/dist/models/project.js
CHANGED
|
@@ -15,7 +15,7 @@ var memorySchema = new mongoose_1.default.Schema({
|
|
|
15
15
|
type: String,
|
|
16
16
|
},
|
|
17
17
|
text: { description: "The memory content", required: true, type: String },
|
|
18
|
-
}, { timestamps: { createdAt: "created", updatedAt: false } });
|
|
18
|
+
}, { strict: "throw", timestamps: { createdAt: "created", updatedAt: false } });
|
|
19
19
|
var projectSchema = new mongoose_1.default.Schema({
|
|
20
20
|
memories: {
|
|
21
21
|
default: [],
|
|
@@ -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.1",
|
|
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.1",
|
|
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.1"
|
|
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
|
);
|