zeitlich 0.2.50 → 0.2.51
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/adapters/thread/anthropic/index.cjs +15 -13
- package/dist/adapters/thread/anthropic/index.cjs.map +1 -1
- package/dist/adapters/thread/anthropic/index.d.cts +15 -10
- package/dist/adapters/thread/anthropic/index.d.ts +15 -10
- package/dist/adapters/thread/anthropic/index.js +15 -13
- package/dist/adapters/thread/anthropic/index.js.map +1 -1
- package/dist/adapters/thread/anthropic/workflow.d.cts +5 -5
- package/dist/adapters/thread/anthropic/workflow.d.ts +5 -5
- package/dist/adapters/thread/google-genai/index.cjs +18 -12
- package/dist/adapters/thread/google-genai/index.cjs.map +1 -1
- package/dist/adapters/thread/google-genai/index.d.cts +181 -11
- package/dist/adapters/thread/google-genai/index.d.ts +181 -11
- package/dist/adapters/thread/google-genai/index.js +18 -12
- package/dist/adapters/thread/google-genai/index.js.map +1 -1
- package/dist/adapters/thread/google-genai/workflow.d.cts +6 -6
- package/dist/adapters/thread/google-genai/workflow.d.ts +6 -6
- package/dist/adapters/thread/langchain/index.cjs +22 -13
- package/dist/adapters/thread/langchain/index.cjs.map +1 -1
- package/dist/adapters/thread/langchain/index.d.cts +15 -10
- package/dist/adapters/thread/langchain/index.d.ts +15 -10
- package/dist/adapters/thread/langchain/index.js +22 -13
- package/dist/adapters/thread/langchain/index.js.map +1 -1
- package/dist/adapters/thread/langchain/workflow.d.cts +5 -5
- package/dist/adapters/thread/langchain/workflow.d.ts +5 -5
- package/dist/{cold-store-CCnZYWjx.d.ts → cold-store-DyHodfAB.d.ts} +1 -1
- package/dist/{cold-store-C0uvYTSi.d.cts → cold-store-YOx9nmgR.d.cts} +1 -1
- package/dist/index.d.cts +8 -8
- package/dist/index.d.ts +8 -8
- package/dist/{proxy-BVznA2_p.d.cts → proxy-2htgGQrc.d.cts} +1 -1
- package/dist/{proxy-C4J1pNUk.d.ts → proxy-CmiTP4pp.d.ts} +1 -1
- package/dist/{thread-manager-BqjzWsP7.d.ts → thread-manager-BJ5pz5Cx.d.cts} +4 -5
- package/dist/{thread-manager-SkSWRPRc.d.ts → thread-manager-BQAbrYXH.d.cts} +4 -5
- package/dist/{thread-manager-Dzl1fHhV.d.cts → thread-manager-CcvltOuq.d.ts} +4 -5
- package/dist/{thread-manager-CzIs47uG.d.cts → thread-manager-DHAbncHX.d.ts} +4 -5
- package/dist/{types-DZnUqCAP.d.cts → types-BjdqxKYp.d.cts} +1 -1
- package/dist/{types-CbPnU4RM.d.ts → types-DEbkLA06.d.ts} +1 -1
- package/dist/{types-YNesmGKV.d.ts → types-DiI7mZhI.d.ts} +1 -1
- package/dist/{types-d2RvEP6v.d.cts → types-N_LTWe4b.d.cts} +1 -1
- package/dist/{workflow-B3oTe2_D.d.cts → workflow-CcgD6EUB.d.cts} +1 -1
- package/dist/{workflow-Bkzg0cjB.d.ts → workflow-DBjPOKBr.d.ts} +1 -1
- package/dist/workflow.d.cts +2 -2
- package/dist/workflow.d.ts +2 -2
- package/package.json +1 -1
- package/src/adapters/thread/anthropic/activities.test.ts +115 -0
- package/src/adapters/thread/anthropic/activities.ts +10 -18
- package/src/adapters/thread/anthropic/model-invoker.test.ts +50 -0
- package/src/adapters/thread/anthropic/model-invoker.ts +10 -0
- package/src/adapters/thread/anthropic/thread-manager.ts +2 -3
- package/src/adapters/thread/google-genai/activities.test.ts +162 -0
- package/src/adapters/thread/google-genai/activities.ts +37 -14
- package/src/adapters/thread/google-genai/model-invoker.test.ts +53 -4
- package/src/adapters/thread/google-genai/model-invoker.ts +11 -0
- package/src/adapters/thread/google-genai/thread-manager.ts +2 -3
- package/src/adapters/thread/langchain/activities.test.ts +88 -0
- package/src/adapters/thread/langchain/activities.ts +14 -11
- package/src/adapters/thread/langchain/model-invoker.test.ts +74 -0
- package/src/adapters/thread/langchain/model-invoker.ts +15 -2
- package/src/adapters/thread/langchain/thread-manager.ts +2 -3
- package/dist/activities-IuOIvPHO.d.ts +0 -162
- package/dist/activities-cIlq1y1y.d.cts +0 -162
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { RedisClientType as Redis } from "redis";
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
GoogleGenAI,
|
|
4
|
+
Content,
|
|
5
|
+
Part,
|
|
6
|
+
GenerateContentConfig,
|
|
7
|
+
} from "@google/genai";
|
|
3
8
|
import type { ToolResultConfig } from "../../../lib/types";
|
|
4
9
|
import type { PersistedThreadState } from "../../../lib/state/types";
|
|
5
10
|
import type {
|
|
@@ -23,7 +28,10 @@ import {
|
|
|
23
28
|
type GoogleGenAIThreadManagerHooks,
|
|
24
29
|
type StoredContent,
|
|
25
30
|
} from "./thread-manager";
|
|
26
|
-
import {
|
|
31
|
+
import {
|
|
32
|
+
createGoogleGenAIModelInvoker,
|
|
33
|
+
type GoogleGenAIModelInvokerConfig,
|
|
34
|
+
} from "./model-invoker";
|
|
27
35
|
import { ADAPTER_ID } from "./adapter-id";
|
|
28
36
|
|
|
29
37
|
export type GoogleGenAIThreadOps<TScope extends string = ""> =
|
|
@@ -46,11 +54,22 @@ export interface GoogleGenAIAdapterConfig {
|
|
|
46
54
|
*/
|
|
47
55
|
coldStore?: ColdThreadStore;
|
|
48
56
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* more appropriate.
|
|
57
|
+
* Redis TTL for the thread's keys; defaults to 90 days. Use a shorter
|
|
58
|
+
* value (hours) with a cold tier.
|
|
52
59
|
*/
|
|
53
60
|
ttlSeconds?: number;
|
|
61
|
+
/**
|
|
62
|
+
* Default generation config forwarded to every invoker the adapter
|
|
63
|
+
* builds (`invoker` and `createModelInvoker`). `systemInstruction`,
|
|
64
|
+
* `tools`, and `abortSignal` are managed by the invoker and override
|
|
65
|
+
* any values set here.
|
|
66
|
+
*/
|
|
67
|
+
generationConfig?: GenerateContentConfig;
|
|
68
|
+
/**
|
|
69
|
+
* Default server-side context caching config forwarded to every
|
|
70
|
+
* invoker the adapter builds. See {@link createGoogleGenAIModelInvoker}.
|
|
71
|
+
*/
|
|
72
|
+
cache?: GoogleGenAIModelInvokerConfig["cache"];
|
|
54
73
|
}
|
|
55
74
|
|
|
56
75
|
/**
|
|
@@ -145,7 +164,7 @@ export interface GoogleGenAIAdapter {
|
|
|
145
164
|
* ...createRunAgentActivity(temporalClient, adapter.invoker, "codingAgent"),
|
|
146
165
|
* ...createRunAgentActivity(
|
|
147
166
|
* temporalClient,
|
|
148
|
-
* adapter.createModelInvoker('gemini-2.5-pro'),
|
|
167
|
+
* adapter.createModelInvoker('gemini-2.5-pro', client),
|
|
149
168
|
* "researchAgent",
|
|
150
169
|
* ),
|
|
151
170
|
* };
|
|
@@ -157,25 +176,26 @@ export function createGoogleGenAIAdapter(
|
|
|
157
176
|
): GoogleGenAIAdapter {
|
|
158
177
|
const { redis } = config;
|
|
159
178
|
|
|
160
|
-
|
|
179
|
+
// Single source for the adapter's `redis` handle and configured TTL, spread
|
|
180
|
+
// into every internal thread manager so all of them share one configuration.
|
|
181
|
+
const base = {
|
|
182
|
+
redis,
|
|
161
183
|
...(config.ttlSeconds !== undefined && { ttlSeconds: config.ttlSeconds }),
|
|
162
184
|
};
|
|
163
185
|
|
|
164
186
|
const makeProviderThread = (threadId: string, threadKey?: string) =>
|
|
165
187
|
createGoogleGenAIThreadManager({
|
|
166
|
-
|
|
188
|
+
...base,
|
|
167
189
|
threadId,
|
|
168
190
|
key: threadKey,
|
|
169
|
-
...baseExtras,
|
|
170
191
|
});
|
|
171
192
|
|
|
172
193
|
const makeTieredBase = (threadId: string, threadKey?: string) =>
|
|
173
194
|
createTieredThreadManager<StoredContent>({
|
|
174
|
-
|
|
195
|
+
...base,
|
|
175
196
|
threadId,
|
|
176
197
|
key: threadKey,
|
|
177
198
|
idOf: storedContentId,
|
|
178
|
-
...baseExtras,
|
|
179
199
|
...(config.coldStore && { coldStore: config.coldStore }),
|
|
180
200
|
});
|
|
181
201
|
|
|
@@ -235,11 +255,10 @@ export function createGoogleGenAIAdapter(
|
|
|
235
255
|
threadKey?: string
|
|
236
256
|
): Promise<void> {
|
|
237
257
|
const thread = createGoogleGenAIThreadManager({
|
|
238
|
-
|
|
258
|
+
...base,
|
|
239
259
|
threadId: sourceThreadId,
|
|
240
260
|
key: threadKey,
|
|
241
261
|
hooks: config.hooks,
|
|
242
|
-
...baseExtras,
|
|
243
262
|
});
|
|
244
263
|
await thread.fork(targetThreadId);
|
|
245
264
|
},
|
|
@@ -304,10 +323,14 @@ export function createGoogleGenAIAdapter(
|
|
|
304
323
|
client: GoogleGenAI
|
|
305
324
|
): ModelInvoker<Content> =>
|
|
306
325
|
createGoogleGenAIModelInvoker({
|
|
307
|
-
|
|
326
|
+
...base,
|
|
308
327
|
client,
|
|
309
328
|
model,
|
|
310
329
|
hooks: config.hooks,
|
|
330
|
+
...(config.generationConfig !== undefined && {
|
|
331
|
+
config: config.generationConfig,
|
|
332
|
+
}),
|
|
333
|
+
...(config.cache !== undefined && { cache: config.cache }),
|
|
311
334
|
});
|
|
312
335
|
|
|
313
336
|
const invoker: ModelInvoker<Content> =
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
import { createGoogleGenAIModelInvoker } from "./model-invoker";
|
|
9
9
|
import type { StoredContent } from "./thread-manager";
|
|
10
10
|
import type { AgentResponse } from "../../../lib/model";
|
|
11
|
+
import { THREAD_TTL_SECONDS } from "../../../lib/thread/keys";
|
|
11
12
|
|
|
12
13
|
const textReply: Part[] = [{ text: "ok" }];
|
|
13
14
|
|
|
@@ -17,7 +18,8 @@ function createMockRedis(
|
|
|
17
18
|
) {
|
|
18
19
|
return {
|
|
19
20
|
exists: vi.fn().mockResolvedValue(1),
|
|
20
|
-
|
|
21
|
+
lRange: vi.fn().mockResolvedValue(stored.map((m) => JSON.stringify(m))),
|
|
22
|
+
lTrim: vi.fn().mockResolvedValue("OK"),
|
|
21
23
|
get: vi
|
|
22
24
|
.fn()
|
|
23
25
|
.mockImplementation((key: string) =>
|
|
@@ -25,7 +27,7 @@ function createMockRedis(
|
|
|
25
27
|
),
|
|
26
28
|
del: vi.fn().mockResolvedValue(1),
|
|
27
29
|
set: vi.fn().mockResolvedValue("OK"),
|
|
28
|
-
|
|
30
|
+
rPush: vi.fn().mockResolvedValue(1),
|
|
29
31
|
expire: vi.fn().mockResolvedValue(1),
|
|
30
32
|
eval: vi.fn().mockResolvedValue(1),
|
|
31
33
|
};
|
|
@@ -311,8 +313,7 @@ describe("Google GenAI model invoker — context caching", () => {
|
|
|
311
313
|
);
|
|
312
314
|
expect(setCall).toBeDefined();
|
|
313
315
|
expect(setCall?.[1]).toBe("cached-content-ref");
|
|
314
|
-
expect(setCall?.[2]).
|
|
315
|
-
expect(setCall?.[3]).toBe(595);
|
|
316
|
+
expect(setCall?.[2]).toEqual({ EX: 595 });
|
|
316
317
|
});
|
|
317
318
|
|
|
318
319
|
it("reports cachedWriteTokens from cache creation", async () => {
|
|
@@ -335,3 +336,51 @@ describe("Google GenAI model invoker — context caching", () => {
|
|
|
335
336
|
expect(result.usage?.cachedWriteTokens).toBe(4200);
|
|
336
337
|
});
|
|
337
338
|
});
|
|
339
|
+
|
|
340
|
+
describe("Google GenAI model invoker — thread TTL", () => {
|
|
341
|
+
// A thread whose tail is a prior attempt's assistant message stored
|
|
342
|
+
// under `assistant-1`, so the invoker's `truncateFromId(assistant-1)`
|
|
343
|
+
// trims it and re-stamps the surviving list key's TTL.
|
|
344
|
+
const retriedThread: StoredContent[] = [
|
|
345
|
+
{ id: "msg-1", content: { role: "user", parts: [{ text: "hi" }] } },
|
|
346
|
+
{
|
|
347
|
+
id: "assistant-1",
|
|
348
|
+
content: { role: "model", parts: [{ text: "prior attempt" }] },
|
|
349
|
+
},
|
|
350
|
+
];
|
|
351
|
+
const listKey = "messages:thread:thread-1";
|
|
352
|
+
|
|
353
|
+
it("re-stamps trimmed hot keys at the configured ttlSeconds", async () => {
|
|
354
|
+
const redis = createMockRedis(retriedThread);
|
|
355
|
+
const client = createMockClient();
|
|
356
|
+
|
|
357
|
+
const invoker = createGoogleGenAIModelInvoker({
|
|
358
|
+
redis: redis as never,
|
|
359
|
+
client: client as never,
|
|
360
|
+
model: "gemini-2.5-flash",
|
|
361
|
+
ttlSeconds: 3600,
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
await invoker(invokerConfig);
|
|
365
|
+
|
|
366
|
+
expect(redis.lTrim).toHaveBeenCalledWith(listKey, 0, 0);
|
|
367
|
+
expect(redis.expire).toHaveBeenCalledWith(listKey, 3600);
|
|
368
|
+
expect(redis.expire).not.toHaveBeenCalledWith(listKey, THREAD_TTL_SECONDS);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it("defaults to THREAD_TTL_SECONDS when ttlSeconds is omitted", async () => {
|
|
372
|
+
const redis = createMockRedis(retriedThread);
|
|
373
|
+
const client = createMockClient();
|
|
374
|
+
|
|
375
|
+
const invoker = createGoogleGenAIModelInvoker({
|
|
376
|
+
redis: redis as never,
|
|
377
|
+
client: client as never,
|
|
378
|
+
model: "gemini-2.5-flash",
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
await invoker(invokerConfig);
|
|
382
|
+
|
|
383
|
+
expect(redis.lTrim).toHaveBeenCalledWith(listKey, 0, 0);
|
|
384
|
+
expect(redis.expire).toHaveBeenCalledWith(listKey, THREAD_TTL_SECONDS);
|
|
385
|
+
});
|
|
386
|
+
});
|
|
@@ -21,6 +21,12 @@ export interface GoogleGenAIModelInvokerConfig {
|
|
|
21
21
|
client: GoogleGenAI;
|
|
22
22
|
model: string;
|
|
23
23
|
hooks?: GoogleGenAIThreadManagerHooks;
|
|
24
|
+
/**
|
|
25
|
+
* Redis TTL for the thread's keys; defaults to 90 days. Use a shorter
|
|
26
|
+
* value (hours) with a cold tier. Distinct from `cache.ttlSeconds`
|
|
27
|
+
* (server-side context caching).
|
|
28
|
+
*/
|
|
29
|
+
ttlSeconds?: number;
|
|
24
30
|
/** Passed through to `generateContentStream().config`.
|
|
25
31
|
* `systemInstruction`, `tools`, and `abortSignal` are managed by the
|
|
26
32
|
* invoker and will override any values set here. */
|
|
@@ -69,6 +75,7 @@ export function createGoogleGenAIModelInvoker({
|
|
|
69
75
|
client,
|
|
70
76
|
model,
|
|
71
77
|
hooks,
|
|
78
|
+
ttlSeconds,
|
|
72
79
|
config: generationConfig,
|
|
73
80
|
cache: cacheConfig,
|
|
74
81
|
}: GoogleGenAIModelInvokerConfig) {
|
|
@@ -83,6 +90,7 @@ export function createGoogleGenAIModelInvoker({
|
|
|
83
90
|
threadId,
|
|
84
91
|
key: threadKey,
|
|
85
92
|
hooks,
|
|
93
|
+
...(ttlSeconds !== undefined && { ttlSeconds }),
|
|
86
94
|
});
|
|
87
95
|
// Truncate the thread starting at the id the assistant message
|
|
88
96
|
// will be stored under. No-op on the first attempt; on rewind
|
|
@@ -214,6 +222,7 @@ export async function invokeGoogleGenAIModel({
|
|
|
214
222
|
client,
|
|
215
223
|
model,
|
|
216
224
|
hooks,
|
|
225
|
+
ttlSeconds,
|
|
217
226
|
config,
|
|
218
227
|
generationConfig,
|
|
219
228
|
cache,
|
|
@@ -222,6 +231,7 @@ export async function invokeGoogleGenAIModel({
|
|
|
222
231
|
client: GoogleGenAI;
|
|
223
232
|
model: string;
|
|
224
233
|
hooks?: GoogleGenAIThreadManagerHooks;
|
|
234
|
+
ttlSeconds?: number;
|
|
225
235
|
config: ModelInvokerConfig;
|
|
226
236
|
generationConfig?: GenerateContentConfig;
|
|
227
237
|
cache?: GoogleGenAIModelInvokerConfig["cache"];
|
|
@@ -231,6 +241,7 @@ export async function invokeGoogleGenAIModel({
|
|
|
231
241
|
client,
|
|
232
242
|
model,
|
|
233
243
|
hooks,
|
|
244
|
+
...(ttlSeconds !== undefined && { ttlSeconds }),
|
|
234
245
|
config: generationConfig,
|
|
235
246
|
cache,
|
|
236
247
|
});
|
|
@@ -32,9 +32,8 @@ export interface GoogleGenAIThreadManagerConfig {
|
|
|
32
32
|
key?: string;
|
|
33
33
|
hooks?: GoogleGenAIThreadManagerHooks;
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* typically more appropriate.
|
|
35
|
+
* Redis TTL for the thread's keys; defaults to 90 days. Use a shorter
|
|
36
|
+
* value (hours) with a cold tier.
|
|
38
37
|
*/
|
|
39
38
|
ttlSeconds?: number;
|
|
40
39
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { AIMessage, HumanMessage } from "@langchain/core/messages";
|
|
3
|
+
import { createLangChainAdapter } from "./activities";
|
|
4
|
+
import { THREAD_TTL_SECONDS } from "../../../lib/thread/keys";
|
|
5
|
+
|
|
6
|
+
function createMockRedis(stored: unknown[]) {
|
|
7
|
+
return {
|
|
8
|
+
exists: vi.fn().mockResolvedValue(1),
|
|
9
|
+
lRange: vi.fn().mockResolvedValue(stored.map((m) => JSON.stringify(m))),
|
|
10
|
+
lTrim: vi.fn().mockResolvedValue("OK"),
|
|
11
|
+
del: vi.fn().mockResolvedValue(1),
|
|
12
|
+
set: vi.fn().mockResolvedValue("OK"),
|
|
13
|
+
rPush: vi.fn().mockResolvedValue(1),
|
|
14
|
+
expire: vi.fn().mockResolvedValue(1),
|
|
15
|
+
eval: vi.fn().mockResolvedValue(1),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function createMockModel() {
|
|
20
|
+
const response = {
|
|
21
|
+
tool_calls: [],
|
|
22
|
+
response_metadata: {},
|
|
23
|
+
usage_metadata: { input_tokens: 1, output_tokens: 1 },
|
|
24
|
+
toDict: () => ({ type: "ai", data: { content: "ok" } }),
|
|
25
|
+
};
|
|
26
|
+
return { invoke: vi.fn().mockResolvedValue(response) };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Tail stored under the `assistantMessageId`, so the invoker's
|
|
30
|
+
// `truncateFromId` trims it and re-stamps the surviving list key's TTL.
|
|
31
|
+
const retriedThread = [
|
|
32
|
+
new HumanMessage({ id: "msg-1", content: "hi" }).toDict(),
|
|
33
|
+
new AIMessage({ id: "assistant-1", content: "prior" }).toDict(),
|
|
34
|
+
];
|
|
35
|
+
const listKey = "messages:thread:thread-1";
|
|
36
|
+
const metaKey = "messages:meta:thread:thread-1";
|
|
37
|
+
const invokerCall = {
|
|
38
|
+
threadId: "thread-1",
|
|
39
|
+
assistantMessageId: "assistant-1",
|
|
40
|
+
state: { tools: [] } as never,
|
|
41
|
+
agentName: "TestAgent",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
describe("createLangChainAdapter — TTL propagation", () => {
|
|
45
|
+
it("forwards adapter ttlSeconds to a created invoker's writes", async () => {
|
|
46
|
+
const redis = createMockRedis(retriedThread);
|
|
47
|
+
const model = createMockModel();
|
|
48
|
+
const adapter = createLangChainAdapter({
|
|
49
|
+
redis: redis as never,
|
|
50
|
+
ttlSeconds: 3600,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
await adapter.createModelInvoker(model as never)(invokerCall);
|
|
54
|
+
|
|
55
|
+
expect(redis.expire).toHaveBeenCalledWith(listKey, 3600);
|
|
56
|
+
expect(redis.expire).not.toHaveBeenCalledWith(listKey, THREAD_TTL_SECONDS);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("forwards adapter ttlSeconds to thread-op writes", async () => {
|
|
60
|
+
const redis = createMockRedis([]);
|
|
61
|
+
const adapter = createLangChainAdapter({
|
|
62
|
+
redis: redis as never,
|
|
63
|
+
ttlSeconds: 3600,
|
|
64
|
+
});
|
|
65
|
+
const acts = adapter.createActivities() as unknown as Record<
|
|
66
|
+
string,
|
|
67
|
+
(threadId: string, threadKey?: string) => Promise<void>
|
|
68
|
+
>;
|
|
69
|
+
const initialize = Object.entries(acts).find(([k]) =>
|
|
70
|
+
k.endsWith("InitializeThread")
|
|
71
|
+
)?.[1];
|
|
72
|
+
if (!initialize) throw new Error("initializeThread activity not found");
|
|
73
|
+
|
|
74
|
+
await initialize("thread-1");
|
|
75
|
+
|
|
76
|
+
expect(redis.set).toHaveBeenCalledWith(metaKey, "1", { EX: 3600 });
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("defaults to THREAD_TTL_SECONDS when adapter ttlSeconds is omitted", async () => {
|
|
80
|
+
const redis = createMockRedis(retriedThread);
|
|
81
|
+
const model = createMockModel();
|
|
82
|
+
const adapter = createLangChainAdapter({ redis: redis as never });
|
|
83
|
+
|
|
84
|
+
await adapter.createModelInvoker(model as never)(invokerCall);
|
|
85
|
+
|
|
86
|
+
expect(redis.expire).toHaveBeenCalledWith(listKey, THREAD_TTL_SECONDS);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -46,9 +46,8 @@ export interface LangChainAdapterConfig {
|
|
|
46
46
|
*/
|
|
47
47
|
coldStore?: ColdThreadStore;
|
|
48
48
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* more appropriate.
|
|
49
|
+
* Redis TTL for the thread's keys; defaults to 90 days. Use a shorter
|
|
50
|
+
* value (hours) with a cold tier.
|
|
52
51
|
*/
|
|
53
52
|
ttlSeconds?: number;
|
|
54
53
|
}
|
|
@@ -133,25 +132,26 @@ export function createLangChainAdapter(
|
|
|
133
132
|
): LangChainAdapter {
|
|
134
133
|
const { redis } = config;
|
|
135
134
|
|
|
136
|
-
|
|
135
|
+
// Single source for the adapter's `redis` handle and configured TTL, spread
|
|
136
|
+
// into every internal thread manager so all of them share one configuration.
|
|
137
|
+
const base = {
|
|
138
|
+
redis,
|
|
137
139
|
...(config.ttlSeconds !== undefined && { ttlSeconds: config.ttlSeconds }),
|
|
138
140
|
};
|
|
139
141
|
|
|
140
142
|
const makeProviderThread = (threadId: string, threadKey?: string) =>
|
|
141
143
|
createLangChainThreadManager({
|
|
142
|
-
|
|
144
|
+
...base,
|
|
143
145
|
threadId,
|
|
144
146
|
key: threadKey,
|
|
145
|
-
...baseExtras,
|
|
146
147
|
});
|
|
147
148
|
|
|
148
149
|
const makeTieredBase = (threadId: string, threadKey?: string) =>
|
|
149
150
|
createTieredThreadManager<StoredMessage>({
|
|
150
|
-
|
|
151
|
+
...base,
|
|
151
152
|
threadId,
|
|
152
153
|
key: threadKey,
|
|
153
154
|
idOf: storedMessageId,
|
|
154
|
-
...baseExtras,
|
|
155
155
|
...(config.coldStore && { coldStore: config.coldStore }),
|
|
156
156
|
});
|
|
157
157
|
|
|
@@ -207,11 +207,10 @@ export function createLangChainAdapter(
|
|
|
207
207
|
threadKey?: string
|
|
208
208
|
): Promise<void> {
|
|
209
209
|
const thread = createLangChainThreadManager({
|
|
210
|
-
|
|
210
|
+
...base,
|
|
211
211
|
threadId: sourceThreadId,
|
|
212
212
|
key: threadKey,
|
|
213
213
|
hooks: config.hooks,
|
|
214
|
-
...baseExtras,
|
|
215
214
|
});
|
|
216
215
|
await thread.fork(targetThreadId);
|
|
217
216
|
},
|
|
@@ -275,7 +274,11 @@ export function createLangChainAdapter(
|
|
|
275
274
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
276
275
|
model: BaseChatModel<any>
|
|
277
276
|
): ModelInvoker<StoredMessage> =>
|
|
278
|
-
createLangChainModelInvoker({
|
|
277
|
+
createLangChainModelInvoker({
|
|
278
|
+
...base,
|
|
279
|
+
model,
|
|
280
|
+
hooks: config.hooks,
|
|
281
|
+
});
|
|
279
282
|
|
|
280
283
|
const invoker: ModelInvoker<StoredMessage> = config.model
|
|
281
284
|
? makeInvoker(config.model)
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { AIMessage, HumanMessage } from "@langchain/core/messages";
|
|
3
|
+
import { createLangChainModelInvoker } from "./model-invoker";
|
|
4
|
+
import { THREAD_TTL_SECONDS } from "../../../lib/thread/keys";
|
|
5
|
+
|
|
6
|
+
function createMockRedis(stored: unknown[]) {
|
|
7
|
+
return {
|
|
8
|
+
exists: vi.fn().mockResolvedValue(1),
|
|
9
|
+
lRange: vi.fn().mockResolvedValue(stored.map((m) => JSON.stringify(m))),
|
|
10
|
+
lTrim: vi.fn().mockResolvedValue("OK"),
|
|
11
|
+
del: vi.fn().mockResolvedValue(1),
|
|
12
|
+
set: vi.fn().mockResolvedValue("OK"),
|
|
13
|
+
rPush: vi.fn().mockResolvedValue(1),
|
|
14
|
+
expire: vi.fn().mockResolvedValue(1),
|
|
15
|
+
eval: vi.fn().mockResolvedValue(1),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function createMockModel() {
|
|
20
|
+
const response = {
|
|
21
|
+
tool_calls: [],
|
|
22
|
+
response_metadata: {},
|
|
23
|
+
usage_metadata: { input_tokens: 1, output_tokens: 1 },
|
|
24
|
+
toDict: () => ({ type: "ai", data: { content: "ok" } }),
|
|
25
|
+
};
|
|
26
|
+
return { invoke: vi.fn().mockResolvedValue(response) };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe("createLangChainModelInvoker thread TTL", () => {
|
|
30
|
+
// The tail message is stored under `assistant-1`, so the invoker's
|
|
31
|
+
// `truncateFromId(assistant-1)` trims it and re-stamps the surviving
|
|
32
|
+
// list key's TTL.
|
|
33
|
+
const retriedThread = [
|
|
34
|
+
new HumanMessage({ id: "msg-1", content: "hi" }).toDict(),
|
|
35
|
+
new AIMessage({ id: "assistant-1", content: "prior" }).toDict(),
|
|
36
|
+
];
|
|
37
|
+
const listKey = "messages:thread:thread-1";
|
|
38
|
+
const invokerConfig = {
|
|
39
|
+
threadId: "thread-1",
|
|
40
|
+
assistantMessageId: "assistant-1",
|
|
41
|
+
state: { tools: [] } as never,
|
|
42
|
+
agentName: "Agent",
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
it("re-stamps trimmed hot keys at the configured ttlSeconds", async () => {
|
|
46
|
+
const redis = createMockRedis(retriedThread);
|
|
47
|
+
const model = createMockModel();
|
|
48
|
+
const invoker = createLangChainModelInvoker({
|
|
49
|
+
redis: redis as never,
|
|
50
|
+
model: model as never,
|
|
51
|
+
ttlSeconds: 3600,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
await invoker(invokerConfig);
|
|
55
|
+
|
|
56
|
+
expect(redis.lTrim).toHaveBeenCalledWith(listKey, 0, 0);
|
|
57
|
+
expect(redis.expire).toHaveBeenCalledWith(listKey, 3600);
|
|
58
|
+
expect(redis.expire).not.toHaveBeenCalledWith(listKey, THREAD_TTL_SECONDS);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("defaults to THREAD_TTL_SECONDS when ttlSeconds is omitted", async () => {
|
|
62
|
+
const redis = createMockRedis(retriedThread);
|
|
63
|
+
const model = createMockModel();
|
|
64
|
+
const invoker = createLangChainModelInvoker({
|
|
65
|
+
redis: redis as never,
|
|
66
|
+
model: model as never,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
await invoker(invokerConfig);
|
|
70
|
+
|
|
71
|
+
expect(redis.lTrim).toHaveBeenCalledWith(listKey, 0, 0);
|
|
72
|
+
expect(redis.expire).toHaveBeenCalledWith(listKey, THREAD_TTL_SECONDS);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -16,6 +16,11 @@ export interface LangChainModelInvokerConfig<
|
|
|
16
16
|
redis: Redis;
|
|
17
17
|
model: TModel;
|
|
18
18
|
hooks?: LangChainThreadManagerHooks;
|
|
19
|
+
/**
|
|
20
|
+
* Redis TTL for the thread's keys; defaults to 90 days. Use a shorter
|
|
21
|
+
* value (hours) with a cold tier.
|
|
22
|
+
*/
|
|
23
|
+
ttlSeconds?: number;
|
|
19
24
|
}
|
|
20
25
|
|
|
21
26
|
/**
|
|
@@ -43,7 +48,7 @@ export interface LangChainModelInvokerConfig<
|
|
|
43
48
|
|
|
44
49
|
export function createLangChainModelInvoker<
|
|
45
50
|
TModel extends BaseChatModel<any> = BaseChatModel<any>,
|
|
46
|
-
>({ redis, model, hooks }: LangChainModelInvokerConfig<TModel>) {
|
|
51
|
+
>({ redis, model, hooks, ttlSeconds }: LangChainModelInvokerConfig<TModel>) {
|
|
47
52
|
return async function invokeLangChainModel(
|
|
48
53
|
config: ModelInvokerConfig
|
|
49
54
|
): Promise<AgentResponse<StoredMessage>> {
|
|
@@ -56,6 +61,7 @@ export function createLangChainModelInvoker<
|
|
|
56
61
|
threadId,
|
|
57
62
|
key: threadKey,
|
|
58
63
|
hooks,
|
|
64
|
+
...(ttlSeconds !== undefined && { ttlSeconds }),
|
|
59
65
|
});
|
|
60
66
|
const runId = uuidv4();
|
|
61
67
|
|
|
@@ -122,13 +128,20 @@ export async function invokeLangChainModel<
|
|
|
122
128
|
redis,
|
|
123
129
|
model,
|
|
124
130
|
hooks,
|
|
131
|
+
ttlSeconds,
|
|
125
132
|
config,
|
|
126
133
|
}: {
|
|
127
134
|
redis: Redis;
|
|
128
135
|
config: ModelInvokerConfig;
|
|
129
136
|
model: TModel;
|
|
130
137
|
hooks?: LangChainThreadManagerHooks;
|
|
138
|
+
ttlSeconds?: number;
|
|
131
139
|
}): Promise<AgentResponse<StoredMessage>> {
|
|
132
|
-
const invoker = createLangChainModelInvoker({
|
|
140
|
+
const invoker = createLangChainModelInvoker({
|
|
141
|
+
redis,
|
|
142
|
+
model,
|
|
143
|
+
hooks,
|
|
144
|
+
...(ttlSeconds !== undefined && { ttlSeconds }),
|
|
145
|
+
});
|
|
133
146
|
return invoker(config);
|
|
134
147
|
}
|
|
@@ -35,9 +35,8 @@ export interface LangChainThreadManagerConfig {
|
|
|
35
35
|
key?: string;
|
|
36
36
|
hooks?: LangChainThreadManagerHooks;
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* typically more appropriate.
|
|
38
|
+
* Redis TTL for the thread's keys; defaults to 90 days. Use a shorter
|
|
39
|
+
* value (hours) with a cold tier.
|
|
41
40
|
*/
|
|
42
41
|
ttlSeconds?: number;
|
|
43
42
|
}
|