@terreno/ai 0.9.3 → 0.11.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/bunfig.toml +5 -0
- package/bunfig.unit.toml +2 -0
- package/dist/routes/projects.js +11 -11
- package/package.json +6 -5
- package/src/aiApp.test.ts +150 -0
- package/src/isolated/langfuseClient.isolated.ts +64 -0
- package/src/langfuseApp.test.ts +135 -0
- package/src/langfuseCache.test.ts +59 -67
- package/src/langfusePrompts.test.ts +122 -67
- package/src/langfuseRoutes.test.ts +140 -28
- package/src/langfuseTracing.test.ts +68 -0
- package/src/langfuseVercelAi.test.ts +83 -0
- package/src/models/aiRequest.test.ts +81 -0
- package/src/models/fileAttachment.test.ts +3 -1
- package/src/routes/files.test.ts +126 -0
- package/src/routes/mcp.test.ts +132 -0
- package/src/routes/projects.test.ts +205 -0
- package/src/routes/projects.ts +25 -25
- package/src/routes/routes.test.ts +999 -12
- package/src/service/aiService.test.ts +238 -13
- package/src/service/fileStorage.test.ts +90 -0
- package/src/service/mcpService.test.ts +139 -0
- package/src/tests/bunSetup.ts +16 -10
- package/tsconfig.json +8 -1
package/bunfig.toml
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
[test]
|
|
2
2
|
preload = ["./src/tests/bunSetup.ts"]
|
|
3
3
|
root = "./src"
|
|
4
|
+
coveragePathIgnorePatterns = ["**/*.test.ts", "**/tests/**", "../**", "dist/**", "**/node_modules/**"]
|
|
5
|
+
# Enforced by scripts/check-coverage.ts; Bun parses this key but does not
|
|
6
|
+
# fail the run when coverage is below threshold. See
|
|
7
|
+
# https://github.com/oven-sh/bun/issues/7367 and https://github.com/oven-sh/bun/pull/27933.
|
|
8
|
+
coverageThreshold = { line = 95, function = 95 }
|
package/bunfig.unit.toml
ADDED
package/dist/routes/projects.js
CHANGED
|
@@ -52,17 +52,7 @@ var api_1 = require("@terreno/api");
|
|
|
52
52
|
var project_1 = require("../models/project");
|
|
53
53
|
var addProjectRoutes = function (router, options) {
|
|
54
54
|
var _a, _b;
|
|
55
|
-
|
|
56
|
-
create: [api_1.Permissions.IsAuthenticated],
|
|
57
|
-
delete: [api_1.Permissions.IsOwner],
|
|
58
|
-
list: [api_1.Permissions.IsAuthenticated],
|
|
59
|
-
read: [api_1.Permissions.IsOwner],
|
|
60
|
-
update: [api_1.Permissions.IsOwner],
|
|
61
|
-
}, preCreate: function (body, req) {
|
|
62
|
-
var _a;
|
|
63
|
-
return (__assign(__assign({}, body), { userId: (_a = req.user) === null || _a === void 0 ? void 0 : _a._id }));
|
|
64
|
-
}, queryFields: ["userId"], queryFilter: function (user) { return ({ userId: user === null || user === void 0 ? void 0 : user.id }); }, sort: "-updated" })));
|
|
65
|
-
// Add memory to a project
|
|
55
|
+
// Add memory to a project (registered before modelRouter so it isn't shadowed)
|
|
66
56
|
router.post("/gpt/projects/:id/memories", [
|
|
67
57
|
(0, api_1.authenticateMiddleware)(),
|
|
68
58
|
(0, api_1.createOpenApiBuilder)((_a = options === null || options === void 0 ? void 0 : options.openApiOptions) !== null && _a !== void 0 ? _a : {})
|
|
@@ -143,5 +133,15 @@ var addProjectRoutes = function (router, options) {
|
|
|
143
133
|
}
|
|
144
134
|
});
|
|
145
135
|
}); }));
|
|
136
|
+
router.use("/gpt/projects", (0, api_1.modelRouter)(project_1.Project, __assign(__assign({}, options === null || options === void 0 ? void 0 : options.openApiOptions), { permissions: {
|
|
137
|
+
create: [api_1.Permissions.IsAuthenticated],
|
|
138
|
+
delete: [api_1.Permissions.IsOwner],
|
|
139
|
+
list: [api_1.Permissions.IsAuthenticated],
|
|
140
|
+
read: [api_1.Permissions.IsOwner],
|
|
141
|
+
update: [api_1.Permissions.IsOwner],
|
|
142
|
+
}, preCreate: function (body, req) {
|
|
143
|
+
var _a;
|
|
144
|
+
return (__assign(__assign({}, body), { userId: (_a = req.user) === null || _a === void 0 ? void 0 : _a._id }));
|
|
145
|
+
}, queryFields: ["userId"], queryFilter: function (user) { return ({ userId: user === null || user === void 0 ? void 0 : user.id }); }, sort: "-updated" })));
|
|
146
146
|
};
|
|
147
147
|
exports.addProjectRoutes = addProjectRoutes;
|
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"@langfuse/otel": "^5.0.1",
|
|
11
11
|
"@opentelemetry/api": "^1.9.0",
|
|
12
12
|
"@opentelemetry/sdk-node": "^0.213.0",
|
|
13
|
-
"@terreno/api": "0.
|
|
13
|
+
"@terreno/api": "0.11.0",
|
|
14
14
|
"ai": "^6.0.97",
|
|
15
15
|
"multer": "^1.4.5-lts.1"
|
|
16
16
|
},
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"access": "public"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"@terreno/api": "0.
|
|
51
|
+
"@terreno/api": "0.11.0",
|
|
52
52
|
"mongoose": "^8.0.0"
|
|
53
53
|
},
|
|
54
54
|
"repository": {
|
|
@@ -62,9 +62,10 @@
|
|
|
62
62
|
"lint": "biome check ./src",
|
|
63
63
|
"lint:fix": "biome check --write ./src",
|
|
64
64
|
"lint:unsafefix": "biome check --fix --unsafe ./src",
|
|
65
|
-
"test": "bun test --preload ./src/tests/bunSetup.ts",
|
|
66
|
-
"test:ci": "bun test --preload ./src/tests/bunSetup.ts"
|
|
65
|
+
"test": "bun test --preload ./src/tests/bunSetup.ts && for f in ./src/isolated/*.isolated.ts; do bun test --preload ./src/tests/bunSetup.ts \"$f\" || exit 1; done",
|
|
66
|
+
"test:ci": "bun test --preload ./src/tests/bunSetup.ts && for f in ./src/isolated/*.isolated.ts; do bun test --preload ./src/tests/bunSetup.ts \"$f\" || exit 1; done",
|
|
67
|
+
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
67
68
|
},
|
|
68
69
|
"types": "dist/index.d.ts",
|
|
69
|
-
"version": "0.
|
|
70
|
+
"version": "0.11.0"
|
|
70
71
|
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import {beforeAll, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {createdUpdatedPlugin, setupServer} from "@terreno/api";
|
|
3
|
+
import type {LanguageModel} from "ai";
|
|
4
|
+
import type express from "express";
|
|
5
|
+
import mongoose from "mongoose";
|
|
6
|
+
import passportLocalMongoose from "passport-local-mongoose";
|
|
7
|
+
import supertest from "supertest";
|
|
8
|
+
|
|
9
|
+
import {AiApp} from "./aiApp";
|
|
10
|
+
import type {FileStorageService} from "./service/fileStorage";
|
|
11
|
+
import type {MCPService} from "./service/mcpService";
|
|
12
|
+
|
|
13
|
+
type PasswordedUser = {setPassword: (password: string) => Promise<void>};
|
|
14
|
+
|
|
15
|
+
const userSchema = new mongoose.Schema({
|
|
16
|
+
admin: {default: false, type: Boolean},
|
|
17
|
+
email: {index: true, type: String},
|
|
18
|
+
name: String,
|
|
19
|
+
});
|
|
20
|
+
userSchema.plugin(
|
|
21
|
+
passportLocalMongoose as unknown as (
|
|
22
|
+
schema: mongoose.Schema,
|
|
23
|
+
options: {usernameField: string}
|
|
24
|
+
) => void,
|
|
25
|
+
{usernameField: "email"}
|
|
26
|
+
);
|
|
27
|
+
userSchema.plugin(createdUpdatedPlugin);
|
|
28
|
+
const UserModel = mongoose.models.User || mongoose.model("User", userSchema);
|
|
29
|
+
|
|
30
|
+
const createMockModel = () => ({
|
|
31
|
+
doGenerate: mock(async () => ({
|
|
32
|
+
content: [{text: "ok", type: "text" as const}],
|
|
33
|
+
finishReason: "stop" as const,
|
|
34
|
+
usage: {inputTokens: 1, outputTokens: 1},
|
|
35
|
+
})),
|
|
36
|
+
doStream: mock(async () => ({
|
|
37
|
+
stream: new ReadableStream({
|
|
38
|
+
start(controller) {
|
|
39
|
+
controller.enqueue({
|
|
40
|
+
finishReason: "stop" as const,
|
|
41
|
+
type: "finish" as const,
|
|
42
|
+
usage: {inputTokens: 1, outputTokens: 1},
|
|
43
|
+
});
|
|
44
|
+
controller.close();
|
|
45
|
+
},
|
|
46
|
+
}),
|
|
47
|
+
})),
|
|
48
|
+
modelId: "mock-model",
|
|
49
|
+
provider: "mock-provider",
|
|
50
|
+
specificationVersion: "v2" as const,
|
|
51
|
+
supportedUrls: {},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("AiApp", () => {
|
|
55
|
+
beforeAll(async () => {
|
|
56
|
+
await UserModel.deleteMany({});
|
|
57
|
+
const user = await UserModel.create({email: "aiapp@example.com", name: "User"});
|
|
58
|
+
await (user as unknown as PasswordedUser).setPassword("password");
|
|
59
|
+
await user.save();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const authAsUser = async (app: express.Application) => {
|
|
63
|
+
const agent = supertest.agent(app);
|
|
64
|
+
const res = await agent
|
|
65
|
+
.post("/auth/login")
|
|
66
|
+
.send({email: "aiapp@example.com", password: "password"})
|
|
67
|
+
.expect(200);
|
|
68
|
+
await agent.set("authorization", `Bearer ${res.body.data.token}`);
|
|
69
|
+
return agent;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
it("registers gpt and project routes by default", async () => {
|
|
73
|
+
const {AIService} = await import("./service/aiService");
|
|
74
|
+
const aiService = new AIService({model: createMockModel() as unknown as LanguageModel});
|
|
75
|
+
const plugin = new AiApp({aiService});
|
|
76
|
+
const app = setupServer({
|
|
77
|
+
addRoutes: (router) => plugin.register(router as unknown as express.Application),
|
|
78
|
+
skipListen: true,
|
|
79
|
+
userModel: UserModel,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const agent = await authAsUser(app);
|
|
83
|
+
const tools = await agent.get("/gpt/tools");
|
|
84
|
+
expect(tools.status).toBe(200);
|
|
85
|
+
const histories = await agent.get("/gpt/histories");
|
|
86
|
+
expect(histories.status).toBe(200);
|
|
87
|
+
const projects = await agent.get("/gpt/projects");
|
|
88
|
+
expect(projects.status).toBe(200);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("registers file routes only when fileStorageService and gcsBucket are provided", async () => {
|
|
92
|
+
const fileStorageService = {
|
|
93
|
+
delete: mock(async () => {}),
|
|
94
|
+
getSignedUrl: mock(async () => "https://gcs/signed"),
|
|
95
|
+
upload: mock(async () => ({
|
|
96
|
+
filename: "file",
|
|
97
|
+
gcsKey: "uploads/file",
|
|
98
|
+
mimeType: "application/octet-stream",
|
|
99
|
+
size: 0,
|
|
100
|
+
url: "https://gcs/file",
|
|
101
|
+
})),
|
|
102
|
+
} as unknown as FileStorageService;
|
|
103
|
+
const plugin = new AiApp({fileStorageService, gcsBucket: "test-bucket"});
|
|
104
|
+
const app = setupServer({
|
|
105
|
+
addRoutes: (router) => plugin.register(router as unknown as express.Application),
|
|
106
|
+
skipListen: true,
|
|
107
|
+
userModel: UserModel,
|
|
108
|
+
});
|
|
109
|
+
// Unauthenticated access to upload route: ensure it exists (returns 401, not 404).
|
|
110
|
+
const upload = await supertest(app).post("/files/upload");
|
|
111
|
+
expect(upload.status).not.toBe(404);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("skips file routes when only fileStorageService is provided", async () => {
|
|
115
|
+
const fileStorageService = {
|
|
116
|
+
upload: mock(async () => ({
|
|
117
|
+
filename: "x",
|
|
118
|
+
gcsKey: "uploads/x",
|
|
119
|
+
mimeType: "application/octet-stream",
|
|
120
|
+
size: 0,
|
|
121
|
+
url: "x",
|
|
122
|
+
})),
|
|
123
|
+
} as unknown as FileStorageService;
|
|
124
|
+
const plugin = new AiApp({fileStorageService});
|
|
125
|
+
const app = setupServer({
|
|
126
|
+
addRoutes: (router) => plugin.register(router as unknown as express.Application),
|
|
127
|
+
skipListen: true,
|
|
128
|
+
userModel: UserModel,
|
|
129
|
+
});
|
|
130
|
+
const upload = await supertest(app).post("/files/upload");
|
|
131
|
+
expect(upload.status).toBe(404);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("registers mcp routes when mcpService is provided", async () => {
|
|
135
|
+
const mcpService = {
|
|
136
|
+
getServerStatus: mock(() => []),
|
|
137
|
+
getTools: mock(async () => ({})),
|
|
138
|
+
reconnectServer: mock(async () => {}),
|
|
139
|
+
} as unknown as MCPService;
|
|
140
|
+
const plugin = new AiApp({mcpService});
|
|
141
|
+
const app = setupServer({
|
|
142
|
+
addRoutes: (router) => plugin.register(router as unknown as express.Application),
|
|
143
|
+
skipListen: true,
|
|
144
|
+
userModel: UserModel,
|
|
145
|
+
});
|
|
146
|
+
const agent = await authAsUser(app);
|
|
147
|
+
const servers = await agent.get("/mcp/servers");
|
|
148
|
+
expect(servers.status).not.toBe(404);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
|
|
3
|
+
const shutdownMock = mock(async () => {});
|
|
4
|
+
let ctorCalls = 0;
|
|
5
|
+
class FakeLangfuseClient {
|
|
6
|
+
constructor(public opts: {baseUrl?: string; publicKey: string; secretKey: string}) {
|
|
7
|
+
ctorCalls += 1;
|
|
8
|
+
}
|
|
9
|
+
async shutdown(): Promise<void> {
|
|
10
|
+
return shutdownMock();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
mock.module("@langfuse/client", () => ({
|
|
15
|
+
LangfuseClient: FakeLangfuseClient,
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
const {getLangfuseClient, initLangfuseClient, isLangfuseInitialized, shutdownLangfuseClient} =
|
|
19
|
+
await import("../langfuseClient");
|
|
20
|
+
|
|
21
|
+
describe("langfuseClient", () => {
|
|
22
|
+
beforeEach(async () => {
|
|
23
|
+
await shutdownLangfuseClient();
|
|
24
|
+
ctorCalls = 0;
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("throws when accessing client before initialization", () => {
|
|
28
|
+
expect(() => getLangfuseClient()).toThrow(/not initialized/);
|
|
29
|
+
expect(isLangfuseInitialized()).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("initializes a client with defaults", () => {
|
|
33
|
+
const client = initLangfuseClient({
|
|
34
|
+
publicKey: "pk",
|
|
35
|
+
secretKey: "sk",
|
|
36
|
+
});
|
|
37
|
+
expect((client as unknown as FakeLangfuseClient).opts.baseUrl).toBe(
|
|
38
|
+
"https://cloud.langfuse.com"
|
|
39
|
+
);
|
|
40
|
+
expect(isLangfuseInitialized()).toBe(true);
|
|
41
|
+
expect(getLangfuseClient()).toBe(client);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("respects a custom baseUrl", () => {
|
|
45
|
+
const client = initLangfuseClient({
|
|
46
|
+
baseUrl: "https://lf.example.com",
|
|
47
|
+
publicKey: "pk",
|
|
48
|
+
secretKey: "sk",
|
|
49
|
+
});
|
|
50
|
+
expect((client as unknown as FakeLangfuseClient).opts.baseUrl).toBe("https://lf.example.com");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("shuts down the client", async () => {
|
|
54
|
+
initLangfuseClient({publicKey: "pk", secretKey: "sk"});
|
|
55
|
+
await shutdownLangfuseClient();
|
|
56
|
+
expect(shutdownMock).toHaveBeenCalled();
|
|
57
|
+
expect(isLangfuseInitialized()).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("no-ops shutdown when no client is initialized", async () => {
|
|
61
|
+
await shutdownLangfuseClient();
|
|
62
|
+
expect(isLangfuseInitialized()).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {beforeAll, beforeEach, describe, expect, it} from "bun:test";
|
|
2
|
+
import {createdUpdatedPlugin, setupServer} from "@terreno/api";
|
|
3
|
+
import type express from "express";
|
|
4
|
+
import mongoose from "mongoose";
|
|
5
|
+
import passportLocalMongoose from "passport-local-mongoose";
|
|
6
|
+
import supertest from "supertest";
|
|
7
|
+
|
|
8
|
+
import {LangfuseApp} from "./langfuseApp";
|
|
9
|
+
|
|
10
|
+
type PasswordedUser = {setPassword: (password: string) => Promise<void>};
|
|
11
|
+
|
|
12
|
+
const userSchema = new mongoose.Schema({
|
|
13
|
+
admin: {default: false, type: Boolean},
|
|
14
|
+
email: {index: true, type: String},
|
|
15
|
+
name: String,
|
|
16
|
+
});
|
|
17
|
+
userSchema.plugin(
|
|
18
|
+
passportLocalMongoose as unknown as (
|
|
19
|
+
schema: mongoose.Schema,
|
|
20
|
+
options: {usernameField: string}
|
|
21
|
+
) => void,
|
|
22
|
+
{usernameField: "email"}
|
|
23
|
+
);
|
|
24
|
+
userSchema.plugin(createdUpdatedPlugin);
|
|
25
|
+
const UserModel = mongoose.models.User || mongoose.model("User", userSchema);
|
|
26
|
+
|
|
27
|
+
const buildApp = (plugin: LangfuseApp) =>
|
|
28
|
+
setupServer({
|
|
29
|
+
addRoutes: (router) => plugin.register(router as unknown as express.Application),
|
|
30
|
+
skipListen: true,
|
|
31
|
+
userModel: UserModel,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("LangfuseApp", () => {
|
|
35
|
+
beforeAll(async () => {
|
|
36
|
+
await UserModel.deleteMany({});
|
|
37
|
+
const u = await UserModel.create({email: "lf@example.com", name: "User"});
|
|
38
|
+
await (u as unknown as PasswordedUser).setPassword("password");
|
|
39
|
+
await u.save();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
beforeEach(async () => {
|
|
43
|
+
const {shutdownLangfuseClient} = await import("./langfuseClient");
|
|
44
|
+
await shutdownLangfuseClient();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("mounts admin routes at the default path", async () => {
|
|
48
|
+
const plugin = new LangfuseApp({
|
|
49
|
+
enableTracing: false,
|
|
50
|
+
publicKey: "pk",
|
|
51
|
+
secretKey: "sk",
|
|
52
|
+
});
|
|
53
|
+
const app = buildApp(plugin);
|
|
54
|
+
const res = await supertest(app).get("/admin/langfuse/prompts");
|
|
55
|
+
expect(res.status).not.toBe(404);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("mounts admin routes at a custom path", async () => {
|
|
59
|
+
const plugin = new LangfuseApp({
|
|
60
|
+
adminPath: "/lf-admin",
|
|
61
|
+
enableTracing: false,
|
|
62
|
+
publicKey: "pk",
|
|
63
|
+
secretKey: "sk",
|
|
64
|
+
});
|
|
65
|
+
const app = buildApp(plugin);
|
|
66
|
+
const custom = await supertest(app).get("/lf-admin/prompts");
|
|
67
|
+
expect(custom.status).not.toBe(404);
|
|
68
|
+
const defaultPath = await supertest(app).get("/admin/langfuse/prompts");
|
|
69
|
+
expect(defaultPath.status).toBe(404);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("skips admin routes when enableAdminUI is false", async () => {
|
|
73
|
+
const plugin = new LangfuseApp({
|
|
74
|
+
enableAdminUI: false,
|
|
75
|
+
enableTracing: false,
|
|
76
|
+
publicKey: "pk",
|
|
77
|
+
secretKey: "sk",
|
|
78
|
+
});
|
|
79
|
+
const app = buildApp(plugin);
|
|
80
|
+
const res = await supertest(app).get("/admin/langfuse/prompts");
|
|
81
|
+
expect(res.status).toBe(404);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("registers evaluation routes when evaluation.enabled is true", async () => {
|
|
85
|
+
const plugin = new LangfuseApp({
|
|
86
|
+
enableTracing: false,
|
|
87
|
+
evaluation: {enabled: true, scoringFunctions: []},
|
|
88
|
+
publicKey: "pk",
|
|
89
|
+
secretKey: "sk",
|
|
90
|
+
});
|
|
91
|
+
const app = buildApp(plugin);
|
|
92
|
+
const res = await supertest(app).get("/admin/langfuse/evaluations/config");
|
|
93
|
+
expect(res.status).not.toBe(404);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("does not register evaluation routes when evaluation.enabled is false", async () => {
|
|
97
|
+
const plugin = new LangfuseApp({
|
|
98
|
+
enableTracing: false,
|
|
99
|
+
evaluation: {enabled: false, scoringFunctions: []},
|
|
100
|
+
publicKey: "pk",
|
|
101
|
+
secretKey: "sk",
|
|
102
|
+
});
|
|
103
|
+
const app = buildApp(plugin);
|
|
104
|
+
const res = await supertest(app).get("/admin/langfuse/evaluations/config");
|
|
105
|
+
expect(res.status).toBe(404);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("shuts down cleanly", async () => {
|
|
109
|
+
const plugin = new LangfuseApp({
|
|
110
|
+
enableTracing: false,
|
|
111
|
+
publicKey: "pk",
|
|
112
|
+
secretKey: "sk",
|
|
113
|
+
});
|
|
114
|
+
const app = buildApp(plugin);
|
|
115
|
+
expect(app).toBeDefined();
|
|
116
|
+
await plugin.shutdown();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("invokes shutdown via the SIGTERM handler registered on register", async () => {
|
|
120
|
+
const plugin = new LangfuseApp({
|
|
121
|
+
enableTracing: false,
|
|
122
|
+
publicKey: "pk",
|
|
123
|
+
secretKey: "sk",
|
|
124
|
+
});
|
|
125
|
+
buildApp(plugin);
|
|
126
|
+
// Find the SIGTERM listener that was added last by register() and invoke it directly.
|
|
127
|
+
const listeners = process.listeners("SIGTERM");
|
|
128
|
+
const last = listeners[listeners.length - 1] as (sig: NodeJS.Signals) => void;
|
|
129
|
+
expect(typeof last).toBe("function");
|
|
130
|
+
last("SIGTERM");
|
|
131
|
+
process.removeListener("SIGTERM", last);
|
|
132
|
+
// Give the fire-and-forget shutdown a tick to settle.
|
|
133
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
134
|
+
});
|
|
135
|
+
});
|
|
@@ -1,83 +1,75 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {beforeEach, describe, expect, it} from "bun:test";
|
|
2
2
|
|
|
3
3
|
import {getCached, invalidateCache, LangfuseCache, setCached} from "./langfuseCache";
|
|
4
|
-
import type {LangfuseCachedPrompt} from "./langfuseTypes";
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
config: {},
|
|
8
|
-
labels: ["production"],
|
|
9
|
-
name: "test-prompt",
|
|
10
|
-
prompt: "Hello world",
|
|
11
|
-
tags: [],
|
|
12
|
-
type: "text",
|
|
13
|
-
version: 1,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
describe("cache", () => {
|
|
5
|
+
describe("langfuseCache", () => {
|
|
17
6
|
beforeEach(async () => {
|
|
18
7
|
await LangfuseCache.deleteMany({});
|
|
19
8
|
});
|
|
20
9
|
|
|
21
|
-
|
|
22
|
-
await
|
|
10
|
+
it("returns null for missing keys", async () => {
|
|
11
|
+
expect(await getCached("missing")).toBeNull();
|
|
23
12
|
});
|
|
24
13
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
expect(result?.name).toBe("test-prompt");
|
|
42
|
-
expect(result?.type).toBe("text");
|
|
43
|
-
});
|
|
14
|
+
it("stores and retrieves a cached prompt", async () => {
|
|
15
|
+
await setCached(
|
|
16
|
+
"prompt:hello:production",
|
|
17
|
+
{
|
|
18
|
+
config: {},
|
|
19
|
+
labels: [],
|
|
20
|
+
name: "hello",
|
|
21
|
+
prompt: "Hi",
|
|
22
|
+
tags: [],
|
|
23
|
+
type: "text",
|
|
24
|
+
version: 1,
|
|
25
|
+
},
|
|
26
|
+
60
|
|
27
|
+
);
|
|
28
|
+
const got = await getCached("prompt:hello:production");
|
|
29
|
+
expect(got?.name).toBe("hello");
|
|
44
30
|
});
|
|
45
31
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
expect(count).toBe(1);
|
|
60
|
-
|
|
61
|
-
const result = await getCached("prompt:test:production");
|
|
62
|
-
expect(result?.version).toBe(2);
|
|
63
|
-
});
|
|
32
|
+
it("upserts existing entries via setCached", async () => {
|
|
33
|
+
await setCached(
|
|
34
|
+
"prompt:x:production",
|
|
35
|
+
{config: {}, labels: [], name: "x", prompt: "old", tags: [], type: "text", version: 1},
|
|
36
|
+
60
|
|
37
|
+
);
|
|
38
|
+
await setCached(
|
|
39
|
+
"prompt:x:production",
|
|
40
|
+
{config: {}, labels: [], name: "x", prompt: "new", tags: [], type: "text", version: 2},
|
|
41
|
+
60
|
|
42
|
+
);
|
|
43
|
+
const got = await getCached("prompt:x:production");
|
|
44
|
+
expect(got?.version).toBe(2);
|
|
64
45
|
});
|
|
65
46
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
47
|
+
it("returns null for expired entries", async () => {
|
|
48
|
+
await setCached(
|
|
49
|
+
"prompt:expired:production",
|
|
50
|
+
{config: {}, labels: [], name: "expired", prompt: "", tags: [], type: "text", version: 1},
|
|
51
|
+
60
|
|
52
|
+
);
|
|
53
|
+
await LangfuseCache.updateOne(
|
|
54
|
+
{key: "prompt:expired:production"},
|
|
55
|
+
{expiresAt: new Date(Date.now() - 1000)}
|
|
56
|
+
);
|
|
57
|
+
expect(await getCached("prompt:expired:production")).toBeNull();
|
|
58
|
+
});
|
|
78
59
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
60
|
+
it("invalidates entries matching a pattern", async () => {
|
|
61
|
+
await setCached(
|
|
62
|
+
"prompt:keep:production",
|
|
63
|
+
{config: {}, labels: [], name: "keep", prompt: "", tags: [], type: "text", version: 1},
|
|
64
|
+
60
|
|
65
|
+
);
|
|
66
|
+
await setCached(
|
|
67
|
+
"prompt:drop:production",
|
|
68
|
+
{config: {}, labels: [], name: "drop", prompt: "", tags: [], type: "text", version: 1},
|
|
69
|
+
60
|
|
70
|
+
);
|
|
71
|
+
await invalidateCache("prompt:drop");
|
|
72
|
+
expect(await getCached("prompt:drop:production")).toBeNull();
|
|
73
|
+
expect(await getCached("prompt:keep:production")).not.toBeNull();
|
|
82
74
|
});
|
|
83
75
|
});
|