@terreno/ai 0.9.3 → 0.10.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.
@@ -0,0 +1,2 @@
1
+ [test]
2
+ root = "./src"
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.9.3",
13
+ "@terreno/api": "0.10.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.9.3",
51
+ "@terreno/api": "0.10.0",
52
52
  "mongoose": "^8.0.0"
53
53
  },
54
54
  "repository": {
@@ -66,5 +66,5 @@
66
66
  "test:ci": "bun test --preload ./src/tests/bunSetup.ts"
67
67
  },
68
68
  "types": "dist/index.d.ts",
69
- "version": "0.9.3"
69
+ "version": "0.10.0"
70
70
  }
@@ -1,4 +1,4 @@
1
- import {afterEach, beforeEach, describe, expect, it} from "bun:test";
1
+ import {afterEach, describe, expect, it, mock} from "bun:test";
2
2
 
3
3
  import {getCached, invalidateCache, LangfuseCache, setCached} from "./langfuseCache";
4
4
  import type {LangfuseCachedPrompt} from "./langfuseTypes";
@@ -13,71 +13,77 @@ const samplePrompt: LangfuseCachedPrompt = {
13
13
  version: 1,
14
14
  };
15
15
 
16
- describe("cache", () => {
17
- beforeEach(async () => {
18
- await LangfuseCache.deleteMany({});
19
- });
16
+ const originalFindOne = LangfuseCache.findOne;
17
+ const originalFindOneAndUpdate = LangfuseCache.findOneAndUpdate;
18
+ const originalDeleteMany = LangfuseCache.deleteMany;
20
19
 
21
- afterEach(async () => {
22
- await LangfuseCache.deleteMany({});
20
+ describe("cache", () => {
21
+ afterEach(() => {
22
+ LangfuseCache.findOne = originalFindOne;
23
+ LangfuseCache.findOneAndUpdate = originalFindOneAndUpdate;
24
+ LangfuseCache.deleteMany = originalDeleteMany;
23
25
  });
24
26
 
25
27
  describe("getCached", () => {
26
28
  it("returns null when no entry exists", async () => {
29
+ const lean = mock(async () => null);
30
+ const findOne = mock(() => ({lean}));
31
+ LangfuseCache.findOne = findOne as unknown as typeof LangfuseCache.findOne;
32
+
27
33
  const result = await getCached("prompt:missing:production");
28
- expect(result).toBeNull();
29
- });
30
34
 
31
- it("returns null for expired entries", async () => {
32
- await setCached("prompt:test:production", samplePrompt, -1);
33
- const result = await getCached("prompt:test:production");
34
35
  expect(result).toBeNull();
36
+ expect(findOne).toHaveBeenCalledTimes(1);
37
+ const [query] = (findOne as {mock: {calls: unknown[][]}}).mock.calls[0];
38
+ expect((query as {key: string}).key).toBe("prompt:missing:production");
39
+ expect((query as {expiresAt: {$gt: Date}}).expiresAt.$gt).toBeInstanceOf(Date);
35
40
  });
36
41
 
37
- it("returns the value for valid non-expired entries", async () => {
38
- await setCached("prompt:test:production", samplePrompt, 60);
42
+ it("returns cached value when entry exists", async () => {
43
+ const lean = mock(async () => ({value: samplePrompt}));
44
+ const findOne = mock(() => ({lean}));
45
+ LangfuseCache.findOne = findOne as unknown as typeof LangfuseCache.findOne;
46
+
39
47
  const result = await getCached("prompt:test:production");
40
- expect(result).not.toBeNull();
41
- expect(result?.name).toBe("test-prompt");
42
- expect(result?.type).toBe("text");
48
+
49
+ expect(result).toEqual(samplePrompt);
50
+ expect(lean).toHaveBeenCalledTimes(1);
43
51
  });
44
52
  });
45
53
 
46
54
  describe("setCached", () => {
47
- it("stores a new entry", async () => {
48
- await setCached("prompt:test:production", samplePrompt, 60);
49
- const count = await LangfuseCache.countDocuments({key: "prompt:test:production"});
50
- expect(count).toBe(1);
51
- });
55
+ it("upserts cache entries with computed expiry", async () => {
56
+ const findOneAndUpdate = mock(async () => ({}));
57
+ LangfuseCache.findOneAndUpdate =
58
+ findOneAndUpdate as unknown as typeof LangfuseCache.findOneAndUpdate;
52
59
 
53
- it("upserts when the same key is set twice", async () => {
60
+ const beforeCall = Date.now();
54
61
  await setCached("prompt:test:production", samplePrompt, 60);
55
- const updated: LangfuseCachedPrompt = {...samplePrompt, version: 2};
56
- await setCached("prompt:test:production", updated, 60);
62
+ const afterCall = Date.now();
57
63
 
58
- const count = await LangfuseCache.countDocuments({key: "prompt:test:production"});
59
- expect(count).toBe(1);
64
+ expect(findOneAndUpdate).toHaveBeenCalledTimes(1);
65
+ const [filter, update, options] = (findOneAndUpdate as {mock: {calls: unknown[][]}}).mock
66
+ .calls[0];
67
+ expect(filter).toEqual({key: "prompt:test:production"});
68
+ expect((update as {key: string}).key).toBe("prompt:test:production");
69
+ expect((update as {value: LangfuseCachedPrompt}).value).toEqual(samplePrompt);
70
+ expect(options).toEqual({upsert: true});
60
71
 
61
- const result = await getCached("prompt:test:production");
62
- expect(result?.version).toBe(2);
72
+ const expiresAt = (update as {expiresAt: Date}).expiresAt.getTime();
73
+ expect(expiresAt).toBeGreaterThanOrEqual(beforeCall + 59000);
74
+ expect(expiresAt).toBeLessThanOrEqual(afterCall + 61000);
63
75
  });
64
76
  });
65
77
 
66
78
  describe("invalidateCache", () => {
67
- it("removes entries matching the pattern", async () => {
68
- await setCached("prompt:test-prompt:production", samplePrompt, 60);
69
- await setCached("prompt:test-prompt:staging", {...samplePrompt, version: 2}, 60);
70
- await setCached("prompt:other-prompt:production", {...samplePrompt, name: "other"}, 60);
79
+ it("passes regex key pattern to deleteMany", async () => {
80
+ const deleteMany = mock(async () => ({}));
81
+ LangfuseCache.deleteMany = deleteMany as unknown as typeof LangfuseCache.deleteMany;
71
82
 
72
83
  await invalidateCache("prompt:test-prompt:");
73
84
 
74
- const remaining = await LangfuseCache.find({}).lean();
75
- expect(remaining.length).toBe(1);
76
- expect(remaining[0].key).toBe("prompt:other-prompt:production");
77
- });
78
-
79
- it("does not throw when no entries match", async () => {
80
- await expect(invalidateCache("prompt:nonexistent:")).resolves.toBeUndefined();
85
+ expect(deleteMany).toHaveBeenCalledTimes(1);
86
+ expect(deleteMany).toHaveBeenCalledWith({key: {$regex: "prompt:test-prompt:"}});
81
87
  });
82
88
  });
83
89
  });
@@ -1,8 +1,26 @@
1
- import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
1
+ import {beforeEach, describe, expect, it, mock} from "bun:test";
2
2
 
3
- import {LangfuseCache} from "./langfuseCache";
4
3
  import type {LangfuseCachedPrompt} from "./langfuseTypes";
5
4
 
5
+ const cacheValues = new Map<string, LangfuseCachedPrompt>();
6
+
7
+ const mockLoggerDebug = mock(() => {});
8
+ const mockLoggerInfo = mock(() => {});
9
+ const mockGetCached = mock(async (key: string) => {
10
+ return cacheValues.get(key) ?? null;
11
+ });
12
+ const mockSetCached = mock(async (key: string, value: LangfuseCachedPrompt) => {
13
+ cacheValues.set(key, value);
14
+ });
15
+ const mockInvalidateCache = mock(async (prefix: string) => {
16
+ const keys = [...cacheValues.keys()];
17
+ for (const key of keys) {
18
+ if (key.startsWith(prefix)) {
19
+ cacheValues.delete(key);
20
+ }
21
+ }
22
+ });
23
+ const mockPromptCreate = mock(async (_params: Record<string, unknown>) => {});
6
24
  const mockPromptGet = mock(async (_name: string, _options?: Record<string, unknown>) => ({
7
25
  config: {},
8
26
  labels: ["production"],
@@ -13,11 +31,26 @@ const mockPromptGet = mock(async (_name: string, _options?: Record<string, unkno
13
31
  version: 1,
14
32
  }));
15
33
 
34
+ mock.module("@terreno/api", () => ({
35
+ logger: {
36
+ debug: mockLoggerDebug,
37
+ info: mockLoggerInfo,
38
+ },
39
+ }));
40
+
41
+ mock.module("./langfuseCache", () => ({
42
+ getCached: mockGetCached,
43
+ invalidateCache: mockInvalidateCache,
44
+ setCached: mockSetCached,
45
+ }));
46
+
16
47
  mock.module("./langfuseClient", () => ({
17
- getLangfuseClient: () => ({prompt: {get: mockPromptGet}}),
48
+ getLangfuseClient: () => ({prompt: {create: mockPromptCreate, get: mockPromptGet}}),
18
49
  }));
19
50
 
20
- const {compilePrompt, getPrompt} = await import("./langfusePrompts");
51
+ const {compilePrompt, createPrompt, getPrompt, invalidatePromptCache} = await import(
52
+ "./langfusePrompts"
53
+ );
21
54
 
22
55
  const textPrompt: LangfuseCachedPrompt = {
23
56
  config: {},
@@ -42,84 +75,92 @@ const chatPrompt: LangfuseCachedPrompt = {
42
75
  version: 1,
43
76
  };
44
77
 
45
- describe("compilePrompt", () => {
46
- describe("text prompts", () => {
47
- it("replaces variables in a text prompt", () => {
48
- const result = compilePrompt(textPrompt, {age: "30", name: "Alice"});
49
- expect(result).toBe("Hello Alice, you are 30 years old.");
50
- });
78
+ beforeEach(() => {
79
+ cacheValues.clear();
80
+ mockLoggerDebug.mockClear();
81
+ mockLoggerInfo.mockClear();
82
+ mockGetCached.mockClear();
83
+ mockSetCached.mockClear();
84
+ mockInvalidateCache.mockClear();
85
+ mockPromptCreate.mockClear();
86
+ mockPromptGet.mockClear();
87
+ mockPromptGet.mockImplementation(async () => ({
88
+ config: {},
89
+ labels: ["production"],
90
+ name: "test-prompt",
91
+ prompt: "Hello world",
92
+ tags: [],
93
+ type: "text" as const,
94
+ version: 1,
95
+ }));
96
+ });
51
97
 
52
- it("leaves unreplaced variables as-is when not provided", () => {
53
- const result = compilePrompt(textPrompt, {name: "Alice"});
54
- expect(result).toBe("Hello Alice, you are {{age}} years old.");
55
- });
98
+ describe("compilePrompt", () => {
99
+ it("replaces variables in text prompts", () => {
100
+ const result = compilePrompt(textPrompt, {age: "30", name: "Alice"});
101
+ expect(result).toBe("Hello Alice, you are 30 years old.");
102
+ });
56
103
 
57
- it("returns prompt unchanged when no variables provided", () => {
58
- const noVarPrompt: LangfuseCachedPrompt = {
59
- ...textPrompt,
60
- prompt: "Hello world.",
61
- };
62
- const result = compilePrompt(noVarPrompt);
63
- expect(result).toBe("Hello world.");
64
- });
104
+ it("keeps missing text variables unreplaced", () => {
105
+ const result = compilePrompt(textPrompt, {name: "Alice"});
106
+ expect(result).toBe("Hello Alice, you are {{age}} years old.");
65
107
  });
66
108
 
67
- describe("chat prompts", () => {
68
- it("replaces variables in all chat messages", () => {
69
- const result = compilePrompt(chatPrompt, {company: "Acme", name: "Bob"});
70
- expect(Array.isArray(result)).toBe(true);
71
- const messages = result as Array<{role: string; content: string}>;
72
- expect(messages[0].content).toBe("You are a helpful assistant for Acme.");
73
- expect(messages[1].content).toBe("My name is Bob.");
74
- });
109
+ it("replaces variables in chat prompts", () => {
110
+ const result = compilePrompt(chatPrompt, {company: "Acme", name: "Bob"}) as Array<{
111
+ content: string;
112
+ role: string;
113
+ }>;
75
114
 
76
- it("preserves roles in chat messages", () => {
77
- const result = compilePrompt(chatPrompt, {company: "X", name: "Y"}) as Array<{
78
- role: string;
79
- content: string;
80
- }>;
81
- expect(result[0].role).toBe("system");
82
- expect(result[1].role).toBe("user");
83
- });
115
+ expect(result[0]).toEqual({content: "You are a helpful assistant for Acme.", role: "system"});
116
+ expect(result[1]).toEqual({content: "My name is Bob.", role: "user"});
84
117
  });
85
118
  });
86
119
 
87
120
  describe("getPrompt", () => {
88
- beforeEach(async () => {
89
- await LangfuseCache.deleteMany({});
90
- mockPromptGet.mockClear();
91
- mockPromptGet.mockImplementation(async (_name: string, _options?: Record<string, unknown>) => ({
92
- config: {},
93
- labels: ["production"],
94
- name: "test-prompt",
95
- prompt: "Hello world",
96
- tags: [],
97
- type: "text" as const,
98
- version: 1,
99
- }));
100
- });
121
+ it("returns cached prompts without calling Langfuse", async () => {
122
+ cacheValues.set("prompt:test-prompt:production", textPrompt);
101
123
 
102
- afterEach(async () => {
103
- await LangfuseCache.deleteMany({});
124
+ const result = await getPrompt("test-prompt");
125
+
126
+ expect(result).toEqual(textPrompt);
127
+ expect(mockPromptGet).toHaveBeenCalledTimes(0);
128
+ expect(mockLoggerDebug).toHaveBeenCalledWith(
129
+ 'Langfuse prompt cache hit: "test-prompt" v1 (label: production)'
130
+ );
104
131
  });
105
132
 
106
- it("fetches from Langfuse when cache is empty", async () => {
133
+ it("fetches and caches prompts with default ttl/label", async () => {
107
134
  const result = await getPrompt("test-prompt");
108
- expect(result.name).toBe("test-prompt");
109
- expect(result.type).toBe("text");
110
- expect(mockPromptGet).toHaveBeenCalledTimes(1);
111
- });
112
135
 
113
- it("returns cached result on second call", async () => {
114
- await getPrompt("test-prompt");
115
- await getPrompt("test-prompt");
116
- expect(mockPromptGet).toHaveBeenCalledTimes(1);
136
+ expect(mockPromptGet).toHaveBeenCalledWith("test-prompt", {
137
+ cacheTtlSeconds: 0,
138
+ label: "production",
139
+ });
140
+ expect(mockSetCached).toHaveBeenCalledWith("prompt:test-prompt:production", result, 60);
141
+ expect(cacheValues.get("prompt:test-prompt:production")).toEqual(result);
117
142
  });
118
143
 
119
- it("writes fetched prompt to cache", async () => {
120
- await getPrompt("test-prompt");
121
- const countAfter = await LangfuseCache.countDocuments({key: "prompt:test-prompt:production"});
122
- expect(countAfter).toBe(1);
144
+ it("fetches and caches prompts with custom ttl/label", async () => {
145
+ await getPrompt("test-prompt", {label: "staging"}, {cache: {promptTtlSeconds: 120}});
146
+
147
+ expect(mockPromptGet).toHaveBeenCalledWith("test-prompt", {
148
+ cacheTtlSeconds: 0,
149
+ label: "staging",
150
+ });
151
+ expect(mockSetCached).toHaveBeenCalledWith(
152
+ "prompt:test-prompt:staging",
153
+ {
154
+ config: {},
155
+ labels: ["production"],
156
+ name: "test-prompt",
157
+ prompt: "Hello world",
158
+ tags: [],
159
+ type: "text",
160
+ version: 1,
161
+ },
162
+ 120
163
+ );
123
164
  });
124
165
 
125
166
  it("throws when Langfuse fetch fails", async () => {
@@ -130,3 +171,72 @@ describe("getPrompt", () => {
130
171
  await expect(getPrompt("missing-prompt")).rejects.toThrow("Prompt not found");
131
172
  });
132
173
  });
174
+
175
+ describe("createPrompt", () => {
176
+ it("creates text prompts with default labels/tags", async () => {
177
+ const result = await createPrompt({
178
+ name: "created-prompt",
179
+ prompt: "Text prompt body",
180
+ type: "text",
181
+ });
182
+
183
+ expect(mockPromptCreate).toHaveBeenCalledWith({
184
+ config: undefined,
185
+ labels: ["production"],
186
+ name: "created-prompt",
187
+ prompt: "Text prompt body",
188
+ tags: [],
189
+ type: "text",
190
+ });
191
+ expect(mockInvalidateCache).toHaveBeenCalledWith("prompt:created-prompt:");
192
+ expect(mockPromptGet).toHaveBeenCalledWith("created-prompt", {
193
+ cacheTtlSeconds: 0,
194
+ label: "production",
195
+ });
196
+ expect(result.name).toBe("test-prompt");
197
+ });
198
+
199
+ it("creates chat prompts preserving role and content", async () => {
200
+ await createPrompt({
201
+ config: {temperature: 0.3},
202
+ labels: ["staging"],
203
+ name: "chat-prompt",
204
+ prompt: [
205
+ {content: "System says hello", role: "system"},
206
+ {content: "User asks question", role: "user"},
207
+ ],
208
+ tags: ["support"],
209
+ type: "chat",
210
+ });
211
+
212
+ expect(mockPromptCreate).toHaveBeenCalledWith({
213
+ config: {temperature: 0.3},
214
+ labels: ["staging"],
215
+ name: "chat-prompt",
216
+ prompt: [
217
+ {content: "System says hello", role: "system"},
218
+ {content: "User asks question", role: "user"},
219
+ ],
220
+ tags: ["support"],
221
+ type: "chat",
222
+ });
223
+ });
224
+ });
225
+
226
+ describe("invalidatePromptCache", () => {
227
+ it("invalidates cache entries for a prompt prefix", async () => {
228
+ cacheValues.set("prompt:target-prompt:production", textPrompt);
229
+ cacheValues.set("prompt:target-prompt:staging", textPrompt);
230
+ cacheValues.set("prompt:other-prompt:production", textPrompt);
231
+
232
+ await invalidatePromptCache("target-prompt");
233
+
234
+ expect(mockInvalidateCache).toHaveBeenCalledWith("prompt:target-prompt:");
235
+ expect(cacheValues.has("prompt:target-prompt:production")).toBe(false);
236
+ expect(cacheValues.has("prompt:target-prompt:staging")).toBe(false);
237
+ expect(cacheValues.has("prompt:other-prompt:production")).toBe(true);
238
+ expect(mockLoggerInfo).toHaveBeenCalledWith(
239
+ "Langfuse prompt cache invalidated for: target-prompt"
240
+ );
241
+ });
242
+ });
@@ -4,19 +4,25 @@ import {setupEnvironment, winstonLogger} from "@terreno/api";
4
4
  import mongoose from "mongoose";
5
5
  import winston from "winston";
6
6
 
7
+ const shouldConnectToTestDb = process.env.BUN_TEST_DISABLE_DB !== "true";
8
+
7
9
  // Connect to MongoDB once for all tests
8
- beforeAll(async () => {
9
- await mongoose
10
- .connect("mongodb://127.0.0.1/terreno-ai-test?&connectTimeoutMS=360000")
11
- .catch((err) => {
12
- console.error("Failed to connect to MongoDB:", err);
13
- });
14
- });
10
+ if (shouldConnectToTestDb) {
11
+ beforeAll(async () => {
12
+ await mongoose
13
+ .connect("mongodb://127.0.0.1/terreno-ai-test?&connectTimeoutMS=360000")
14
+ .catch((err) => {
15
+ console.error("Failed to connect to MongoDB:", err);
16
+ });
17
+ });
18
+ }
15
19
 
16
20
  // Close MongoDB connection after all tests
17
- afterAll(async () => {
18
- await mongoose.connection.close();
19
- });
21
+ if (shouldConnectToTestDb) {
22
+ afterAll(async () => {
23
+ await mongoose.connection.close();
24
+ });
25
+ }
20
26
 
21
27
  let logs: string[] = [];
22
28