@terreno/ai 0.11.4-beta.8 → 0.11.6
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 +4 -3
- package/src/langfuseClient.test.ts +92 -0
- package/src/routes/routes.test.ts +72 -0
package/package.json
CHANGED
|
@@ -10,7 +10,6 @@
|
|
|
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.11.4-beta.8",
|
|
14
13
|
"ai": "^6.0.97",
|
|
15
14
|
"multer": "^1.4.5-lts.1"
|
|
16
15
|
},
|
|
@@ -23,6 +22,8 @@
|
|
|
23
22
|
"@types/multer": "^1.4.12",
|
|
24
23
|
"@types/passport-local-mongoose": "^6.1.5",
|
|
25
24
|
"@types/supertest": "^6.0.2",
|
|
25
|
+
"@terreno/api": "0.11.6",
|
|
26
|
+
"mongoose": "8.23.0",
|
|
26
27
|
"passport-local-mongoose": "^9.0.1",
|
|
27
28
|
"supertest": "^7.0.0",
|
|
28
29
|
"typescript": "5.8.2",
|
|
@@ -62,7 +63,7 @@
|
|
|
62
63
|
"access": "public"
|
|
63
64
|
},
|
|
64
65
|
"peerDependencies": {
|
|
65
|
-
"@terreno/api": "0.11.
|
|
66
|
+
"@terreno/api": "0.11.6",
|
|
66
67
|
"mongoose": "^8.0.0"
|
|
67
68
|
},
|
|
68
69
|
"repository": {
|
|
@@ -81,5 +82,5 @@
|
|
|
81
82
|
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
82
83
|
},
|
|
83
84
|
"types": "dist/index.d.ts",
|
|
84
|
-
"version": "0.11.
|
|
85
|
+
"version": "0.11.6"
|
|
85
86
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import {afterEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
getLangfuseClient,
|
|
5
|
+
initLangfuseClient,
|
|
6
|
+
isLangfuseInitialized,
|
|
7
|
+
shutdownLangfuseClient,
|
|
8
|
+
} from "./langfuseClient";
|
|
9
|
+
|
|
10
|
+
mock.module("@langfuse/client", () => {
|
|
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
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("langfuseClient", () => {
|
|
29
|
+
afterEach(async () => {
|
|
30
|
+
await shutdownLangfuseClient();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("throws when getLangfuseClient is called before init", () => {
|
|
34
|
+
expect(() => getLangfuseClient()).toThrow(
|
|
35
|
+
"Langfuse client not initialized. Call initLangfuseClient first."
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("isLangfuseInitialized returns false before init", () => {
|
|
40
|
+
expect(isLangfuseInitialized()).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("initLangfuseClient creates and returns a client", () => {
|
|
44
|
+
const client = initLangfuseClient({
|
|
45
|
+
publicKey: "pk-test",
|
|
46
|
+
secretKey: "sk-test",
|
|
47
|
+
});
|
|
48
|
+
expect(client).toBeDefined();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("uses default baseUrl when not provided", () => {
|
|
52
|
+
const client = initLangfuseClient({
|
|
53
|
+
publicKey: "pk-test",
|
|
54
|
+
secretKey: "sk-test",
|
|
55
|
+
});
|
|
56
|
+
expect((client as unknown as {baseUrl: string}).baseUrl).toBe("https://cloud.langfuse.com");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("uses custom baseUrl when provided", () => {
|
|
60
|
+
const client = initLangfuseClient({
|
|
61
|
+
baseUrl: "https://custom.langfuse.com",
|
|
62
|
+
publicKey: "pk-test",
|
|
63
|
+
secretKey: "sk-test",
|
|
64
|
+
});
|
|
65
|
+
expect((client as unknown as {baseUrl: string}).baseUrl).toBe("https://custom.langfuse.com");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("isLangfuseInitialized returns true after init", () => {
|
|
69
|
+
initLangfuseClient({publicKey: "pk-test", secretKey: "sk-test"});
|
|
70
|
+
expect(isLangfuseInitialized()).toBe(true);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("getLangfuseClient returns the initialized client", () => {
|
|
74
|
+
const created = initLangfuseClient({publicKey: "pk-test", secretKey: "sk-test"});
|
|
75
|
+
const retrieved = getLangfuseClient();
|
|
76
|
+
expect(retrieved).toBe(created);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("shutdownLangfuseClient shuts down and nullifies the instance", async () => {
|
|
80
|
+
const client = initLangfuseClient({publicKey: "pk-test", secretKey: "sk-test"});
|
|
81
|
+
expect(isLangfuseInitialized()).toBe(true);
|
|
82
|
+
await shutdownLangfuseClient();
|
|
83
|
+
expect(isLangfuseInitialized()).toBe(false);
|
|
84
|
+
expect(client.shutdown).toHaveBeenCalled();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("shutdownLangfuseClient is a no-op when not initialized", async () => {
|
|
88
|
+
expect(isLangfuseInitialized()).toBe(false);
|
|
89
|
+
await shutdownLangfuseClient();
|
|
90
|
+
expect(isLangfuseInitialized()).toBe(false);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -944,6 +944,78 @@ describe("AI Routes", () => {
|
|
|
944
944
|
});
|
|
945
945
|
});
|
|
946
946
|
|
|
947
|
+
describe("GPT Prompt error handling", () => {
|
|
948
|
+
it("catches errors thrown mid-stream iteration and sends SSE error", async () => {
|
|
949
|
+
const streamIterationErrorModel = {
|
|
950
|
+
doGenerate: mock(async () => ({
|
|
951
|
+
content: [{text: "ok", type: "text" as const}],
|
|
952
|
+
finishReason: "stop" as const,
|
|
953
|
+
usage: {inputTokens: 1, outputTokens: 1},
|
|
954
|
+
})),
|
|
955
|
+
doStream: mock(async () => ({
|
|
956
|
+
stream: new ReadableStream({
|
|
957
|
+
start(controller) {
|
|
958
|
+
controller.enqueue({id: "t1", type: "text-start" as const});
|
|
959
|
+
controller.enqueue({
|
|
960
|
+
delta: "partial",
|
|
961
|
+
id: "t1",
|
|
962
|
+
type: "text-delta" as const,
|
|
963
|
+
});
|
|
964
|
+
controller.enqueue({id: "t1", type: "text-end" as const});
|
|
965
|
+
controller.error(new Error("stream iteration failure"));
|
|
966
|
+
},
|
|
967
|
+
}),
|
|
968
|
+
})),
|
|
969
|
+
modelId: "stream-iter-error",
|
|
970
|
+
provider: "mock-provider",
|
|
971
|
+
specificationVersion: "v2" as const,
|
|
972
|
+
supportedUrls: {},
|
|
973
|
+
};
|
|
974
|
+
const errApp = setupServer({
|
|
975
|
+
addRoutes: (router, options) => {
|
|
976
|
+
addGptRoutes(router, {
|
|
977
|
+
aiService: new AIService({
|
|
978
|
+
model: streamIterationErrorModel as unknown as LanguageModel,
|
|
979
|
+
}),
|
|
980
|
+
openApiOptions: options,
|
|
981
|
+
});
|
|
982
|
+
},
|
|
983
|
+
skipListen: true,
|
|
984
|
+
userModel: UserModel,
|
|
985
|
+
});
|
|
986
|
+
const agent = await authAsUser(errApp, "notAdmin");
|
|
987
|
+
const res = await agent
|
|
988
|
+
.post("/gpt/prompt")
|
|
989
|
+
.send({prompt: "Hi"})
|
|
990
|
+
.buffer(true)
|
|
991
|
+
.parse(sseCollect);
|
|
992
|
+
expect(res.status).toBe(200);
|
|
993
|
+
const body = (res as SseResponse).body;
|
|
994
|
+
expect(body).toContain("error");
|
|
995
|
+
});
|
|
996
|
+
|
|
997
|
+
it("handles AIRequest.logRequest failure gracefully", async () => {
|
|
998
|
+
const originalLogRequest = AIRequest.logRequest;
|
|
999
|
+
// biome-ignore lint/suspicious/noExplicitAny: Override static method for test mock.
|
|
1000
|
+
AIRequest.logRequest = mock(async () => {
|
|
1001
|
+
throw new Error("database write failed");
|
|
1002
|
+
}) as unknown as typeof AIRequest.logRequest;
|
|
1003
|
+
try {
|
|
1004
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
1005
|
+
const res = await agent
|
|
1006
|
+
.post("/gpt/prompt")
|
|
1007
|
+
.send({prompt: "Hi"})
|
|
1008
|
+
.buffer(true)
|
|
1009
|
+
.parse(sseCollect);
|
|
1010
|
+
expect(res.status).toBe(200);
|
|
1011
|
+
const body = (res as SseResponse).body;
|
|
1012
|
+
expect(body).toContain("done");
|
|
1013
|
+
} finally {
|
|
1014
|
+
AIRequest.logRequest = originalLogRequest;
|
|
1015
|
+
}
|
|
1016
|
+
});
|
|
1017
|
+
});
|
|
1018
|
+
|
|
947
1019
|
describe("GPT tools route", () => {
|
|
948
1020
|
it("lists built-in tools from route config", async () => {
|
|
949
1021
|
const customApp = setupServer({
|