@terreno/ai 0.13.1 → 0.13.3
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/package.json +3 -3
- package/src/langfuseClient.test.ts +2 -18
- package/src/langfusePrompts.test.ts +11 -10
- package/src/langfuseRoutes.test.ts +27 -19
- package/src/langfuseVercelAi.test.ts +8 -12
- package/src/tests/bunSetup.ts +59 -0
package/package.json
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"@types/multer": "^1.4.12",
|
|
23
23
|
"@types/passport-local-mongoose": "^6.1.5",
|
|
24
24
|
"@types/supertest": "^6.0.2",
|
|
25
|
-
"@terreno/api": "0.13.
|
|
25
|
+
"@terreno/api": "0.13.3",
|
|
26
26
|
"mongoose": "8.23.0",
|
|
27
27
|
"passport-local-mongoose": "^9.0.1",
|
|
28
28
|
"supertest": "^7.0.0",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"access": "public"
|
|
64
64
|
},
|
|
65
65
|
"peerDependencies": {
|
|
66
|
-
"@terreno/api": "0.13.
|
|
66
|
+
"@terreno/api": "0.13.3",
|
|
67
67
|
"mongoose": "^8.0.0"
|
|
68
68
|
},
|
|
69
69
|
"repository": {
|
|
@@ -82,5 +82,5 @@
|
|
|
82
82
|
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
83
83
|
},
|
|
84
84
|
"types": "dist/index.d.ts",
|
|
85
|
-
"version": "0.13.
|
|
85
|
+
"version": "0.13.3"
|
|
86
86
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {afterEach, describe, expect, it
|
|
1
|
+
import {afterEach, describe, expect, it} from "bun:test";
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
getLangfuseClient,
|
|
@@ -7,23 +7,7 @@ import {
|
|
|
7
7
|
shutdownLangfuseClient,
|
|
8
8
|
} from "./langfuseClient";
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
return {
|
|
12
|
-
LangfuseClient: class MockLangfuseClient {
|
|
13
|
-
baseUrl: string;
|
|
14
|
-
publicKey: string;
|
|
15
|
-
secretKey: string;
|
|
16
|
-
|
|
17
|
-
constructor(opts: {baseUrl: string; publicKey: string; secretKey: string}) {
|
|
18
|
-
this.baseUrl = opts.baseUrl;
|
|
19
|
-
this.publicKey = opts.publicKey;
|
|
20
|
-
this.secretKey = opts.secretKey;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
shutdown = mock(async () => {});
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
});
|
|
10
|
+
// @langfuse/client is mocked globally in tests/bunSetup.ts.
|
|
27
11
|
|
|
28
12
|
describe("langfuseClient", () => {
|
|
29
13
|
afterEach(async () => {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
2
|
|
|
3
3
|
import {LangfuseCache} from "./langfuseCache";
|
|
4
|
+
import {getLangfuseClient, initLangfuseClient} from "./langfuseClient";
|
|
5
|
+
import {compilePrompt, createPrompt, getPrompt, invalidatePromptCache} from "./langfusePrompts";
|
|
4
6
|
import type {LangfuseCachedPrompt} from "./langfuseTypes";
|
|
5
7
|
|
|
6
8
|
const mockPromptCreate = mock(async (_params: Record<string, unknown>) => {});
|
|
@@ -14,16 +16,6 @@ const mockPromptGet = mock(async (_name: string, _options?: Record<string, unkno
|
|
|
14
16
|
version: 1,
|
|
15
17
|
}));
|
|
16
18
|
|
|
17
|
-
const realLangfuseClient = await import("./langfuseClient");
|
|
18
|
-
mock.module("./langfuseClient", () => ({
|
|
19
|
-
...realLangfuseClient,
|
|
20
|
-
getLangfuseClient: () => ({prompt: {create: mockPromptCreate, get: mockPromptGet}}),
|
|
21
|
-
}));
|
|
22
|
-
|
|
23
|
-
const {compilePrompt, createPrompt, getPrompt, invalidatePromptCache} = await import(
|
|
24
|
-
"./langfusePrompts"
|
|
25
|
-
);
|
|
26
|
-
|
|
27
19
|
const textPrompt: LangfuseCachedPrompt = {
|
|
28
20
|
config: {},
|
|
29
21
|
labels: ["production"],
|
|
@@ -60,6 +52,15 @@ beforeEach(async () => {
|
|
|
60
52
|
type: "text" as const,
|
|
61
53
|
version: 1,
|
|
62
54
|
}));
|
|
55
|
+
// Init the (fake) langfuse client and wire our local mocks onto its prompt
|
|
56
|
+
// methods so the real `langfusePrompts` code exercises customizable
|
|
57
|
+
// responses without hitting the network.
|
|
58
|
+
initLangfuseClient({publicKey: "pk-test", secretKey: "sk-test"});
|
|
59
|
+
const client = getLangfuseClient() as unknown as {
|
|
60
|
+
prompt: {create: typeof mockPromptCreate; get: typeof mockPromptGet};
|
|
61
|
+
};
|
|
62
|
+
client.prompt.get = mockPromptGet;
|
|
63
|
+
client.prompt.create = mockPromptCreate;
|
|
63
64
|
});
|
|
64
65
|
|
|
65
66
|
describe("compilePrompt", () => {
|
|
@@ -4,6 +4,12 @@ import mongoose from "mongoose";
|
|
|
4
4
|
import passportLocalMongoose from "passport-local-mongoose";
|
|
5
5
|
import supertest from "supertest";
|
|
6
6
|
|
|
7
|
+
import {getLangfuseClient, initLangfuseClient} from "./langfuseClient";
|
|
8
|
+
import {addEvaluationRoutes} from "./langfuseRoutesEvaluations";
|
|
9
|
+
import {addPlaygroundRoutes} from "./langfuseRoutesPlayground";
|
|
10
|
+
import {addPromptRoutes} from "./langfuseRoutesPrompts";
|
|
11
|
+
import {addTraceRoutes} from "./langfuseRoutesTraces";
|
|
12
|
+
|
|
7
13
|
const scoreCreate = mock(() => {});
|
|
8
14
|
const promptCreate = mock(async (_params: Record<string, unknown>) => ({}));
|
|
9
15
|
const promptGet = mock(async (name: string) => ({
|
|
@@ -26,25 +32,6 @@ const traceList = mock(async () => ({
|
|
|
26
32
|
const traceGet = mock(async (traceId: string) => ({id: traceId, name: "Trace"}));
|
|
27
33
|
const flush = mock(async () => {});
|
|
28
34
|
|
|
29
|
-
const realLangfuseClient = await import("./langfuseClient");
|
|
30
|
-
mock.module("./langfuseClient", () => ({
|
|
31
|
-
...realLangfuseClient,
|
|
32
|
-
getLangfuseClient: () => ({
|
|
33
|
-
api: {
|
|
34
|
-
prompts: {list: promptsList},
|
|
35
|
-
trace: {get: traceGet, list: traceList},
|
|
36
|
-
},
|
|
37
|
-
flush,
|
|
38
|
-
prompt: {create: promptCreate, get: promptGet},
|
|
39
|
-
score: {create: scoreCreate},
|
|
40
|
-
}),
|
|
41
|
-
}));
|
|
42
|
-
|
|
43
|
-
const {addPromptRoutes} = await import("./langfuseRoutesPrompts");
|
|
44
|
-
const {addTraceRoutes} = await import("./langfuseRoutesTraces");
|
|
45
|
-
const {addPlaygroundRoutes} = await import("./langfuseRoutesPlayground");
|
|
46
|
-
const {addEvaluationRoutes} = await import("./langfuseRoutesEvaluations");
|
|
47
|
-
|
|
48
35
|
const userSchema = new mongoose.Schema({
|
|
49
36
|
admin: {default: false, type: Boolean},
|
|
50
37
|
email: {index: true, type: String},
|
|
@@ -89,6 +76,27 @@ describe("Langfuse routes", () => {
|
|
|
89
76
|
});
|
|
90
77
|
|
|
91
78
|
beforeEach(() => {
|
|
79
|
+
// Init the (fake) langfuse client and wire our local mocks onto its
|
|
80
|
+
// methods so the route handlers exercise customizable responses without
|
|
81
|
+
// making real SDK calls.
|
|
82
|
+
initLangfuseClient({publicKey: "pk-test", secretKey: "sk-test"});
|
|
83
|
+
const client = getLangfuseClient() as unknown as {
|
|
84
|
+
api: {
|
|
85
|
+
prompts: {list: typeof promptsList};
|
|
86
|
+
trace: {get: typeof traceGet; list: typeof traceList};
|
|
87
|
+
};
|
|
88
|
+
flush: typeof flush;
|
|
89
|
+
prompt: {create: typeof promptCreate; get: typeof promptGet};
|
|
90
|
+
score: {create: typeof scoreCreate};
|
|
91
|
+
};
|
|
92
|
+
client.api.prompts.list = promptsList;
|
|
93
|
+
client.api.trace.get = traceGet;
|
|
94
|
+
client.api.trace.list = traceList;
|
|
95
|
+
client.flush = flush;
|
|
96
|
+
client.prompt.create = promptCreate;
|
|
97
|
+
client.prompt.get = promptGet;
|
|
98
|
+
client.score.create = scoreCreate;
|
|
99
|
+
|
|
92
100
|
app = setupServer({
|
|
93
101
|
addRoutes: (router) => {
|
|
94
102
|
addPromptRoutes(router, "/admin/langfuse");
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
2
|
|
|
3
3
|
import {LangfuseCache} from "./langfuseCache";
|
|
4
|
+
import {getLangfuseClient, initLangfuseClient} from "./langfuseClient";
|
|
5
|
+
import {createTelemetryConfig, preparePromptForAI} from "./langfuseVercelAi";
|
|
4
6
|
|
|
5
7
|
const mockPromptGet = mock(async (_name: string, _options?: Record<string, unknown>) => ({
|
|
6
8
|
config: {temperature: 0.5},
|
|
@@ -12,22 +14,16 @@ const mockPromptGet = mock(async (_name: string, _options?: Record<string, unkno
|
|
|
12
14
|
version: 3,
|
|
13
15
|
}));
|
|
14
16
|
|
|
15
|
-
// Mock the langfuse SDK client so the real `getPrompt`/`compilePrompt` from
|
|
16
|
-
// `./langfusePrompts` can be exercised without network access. We intentionally
|
|
17
|
-
// do NOT mock `./langfusePrompts` directly because that would leak into other
|
|
18
|
-
// test suites that share the module cache in the same bun process.
|
|
19
|
-
const realLangfuseClient = await import("./langfuseClient");
|
|
20
|
-
mock.module("./langfuseClient", () => ({
|
|
21
|
-
...realLangfuseClient,
|
|
22
|
-
getLangfuseClient: () => ({prompt: {get: mockPromptGet}}),
|
|
23
|
-
}));
|
|
24
|
-
|
|
25
|
-
const {createTelemetryConfig, preparePromptForAI} = await import("./langfuseVercelAi");
|
|
26
|
-
|
|
27
17
|
describe("langfuseVercelAi", () => {
|
|
28
18
|
beforeEach(async () => {
|
|
29
19
|
mockPromptGet.mockClear();
|
|
30
20
|
await LangfuseCache.deleteMany({});
|
|
21
|
+
// Re-init the (fake) langfuse client and wire the prompt.get mock so the
|
|
22
|
+
// real `getPrompt`/`compilePrompt` from `./langfusePrompts` runs against
|
|
23
|
+
// a customizable response without network access.
|
|
24
|
+
initLangfuseClient({publicKey: "pk-test", secretKey: "sk-test"});
|
|
25
|
+
(getLangfuseClient() as unknown as {prompt: {get: typeof mockPromptGet}}).prompt.get =
|
|
26
|
+
mockPromptGet;
|
|
31
27
|
});
|
|
32
28
|
|
|
33
29
|
describe("preparePromptForAI", () => {
|
package/src/tests/bunSetup.ts
CHANGED
|
@@ -99,6 +99,65 @@ afterEach(() => {
|
|
|
99
99
|
logs = [];
|
|
100
100
|
});
|
|
101
101
|
|
|
102
|
+
// Mock @langfuse/client globally so the real `./langfuseClient` module runs
|
|
103
|
+
// without making network calls. Each `new LangfuseClient(...)` gets its own
|
|
104
|
+
// mock functions so tests can customize per-instance behavior (via
|
|
105
|
+
// `getLangfuseClient()` after `initLangfuseClient(...)`). Mocking the SDK
|
|
106
|
+
// here, rather than mocking `./langfuseClient` per-test-file, prevents the
|
|
107
|
+
// `mock.module()` global cache from leaking different shapes across files.
|
|
108
|
+
mock.module("@langfuse/client", () => {
|
|
109
|
+
return {
|
|
110
|
+
LangfuseClient: class FakeLangfuseClient {
|
|
111
|
+
baseUrl: string;
|
|
112
|
+
publicKey: string;
|
|
113
|
+
secretKey: string;
|
|
114
|
+
api = {
|
|
115
|
+
prompts: {
|
|
116
|
+
list: mock(async () => ({
|
|
117
|
+
data: [],
|
|
118
|
+
meta: {limit: 20, page: 1, total: 0, totalPages: 0},
|
|
119
|
+
})),
|
|
120
|
+
},
|
|
121
|
+
trace: {
|
|
122
|
+
get: mock(async (id: string) => ({id, name: "Trace"})),
|
|
123
|
+
list: mock(async () => ({
|
|
124
|
+
data: [],
|
|
125
|
+
meta: {limit: 20, page: 1, total: 0, totalPages: 0},
|
|
126
|
+
})),
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
prompt = {
|
|
130
|
+
create: mock(async (_params: Record<string, unknown>) => undefined),
|
|
131
|
+
get: mock(async (name: string) => ({
|
|
132
|
+
config: {},
|
|
133
|
+
labels: [],
|
|
134
|
+
name,
|
|
135
|
+
prompt: "",
|
|
136
|
+
tags: [],
|
|
137
|
+
type: "text" as const,
|
|
138
|
+
version: 1,
|
|
139
|
+
})),
|
|
140
|
+
};
|
|
141
|
+
score = {create: mock(() => {})};
|
|
142
|
+
flush = mock(async () => {});
|
|
143
|
+
shutdown = mock(async () => {});
|
|
144
|
+
|
|
145
|
+
constructor(opts: {baseUrl: string; publicKey: string; secretKey: string}) {
|
|
146
|
+
this.baseUrl = opts.baseUrl;
|
|
147
|
+
this.publicKey = opts.publicKey;
|
|
148
|
+
this.secretKey = opts.secretKey;
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Ensure no langfuse client instance leaks across tests in different files.
|
|
155
|
+
// The real langfuseInstance module variable is reset to null between tests.
|
|
156
|
+
beforeEach(async () => {
|
|
157
|
+
const {shutdownLangfuseClient} = await import("../langfuseClient");
|
|
158
|
+
await shutdownLangfuseClient();
|
|
159
|
+
});
|
|
160
|
+
|
|
102
161
|
// Mock @sentry/bun module
|
|
103
162
|
mock.module("@sentry/bun", () => {
|
|
104
163
|
const mockFn = (): ReturnType<typeof mock> => mock(() => {});
|