zeitlich 0.2.36 → 0.2.38

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.
Files changed (204) hide show
  1. package/README.md +146 -92
  2. package/dist/{activities-BVI2lTwr.d.ts → activities-BKhMtKDd.d.ts} +4 -2
  3. package/dist/{activities-hd4aNnZE.d.cts → activities-CDcwkRZs.d.cts} +4 -2
  4. package/dist/adapters/sandbox/bedrock/index.cjs +17 -14
  5. package/dist/adapters/sandbox/bedrock/index.cjs.map +1 -1
  6. package/dist/adapters/sandbox/bedrock/index.d.cts +7 -6
  7. package/dist/adapters/sandbox/bedrock/index.d.ts +7 -6
  8. package/dist/adapters/sandbox/bedrock/index.js +17 -14
  9. package/dist/adapters/sandbox/bedrock/index.js.map +1 -1
  10. package/dist/adapters/sandbox/bedrock/workflow.cjs +2 -0
  11. package/dist/adapters/sandbox/bedrock/workflow.cjs.map +1 -1
  12. package/dist/adapters/sandbox/bedrock/workflow.d.cts +2 -2
  13. package/dist/adapters/sandbox/bedrock/workflow.d.ts +2 -2
  14. package/dist/adapters/sandbox/bedrock/workflow.js +2 -0
  15. package/dist/adapters/sandbox/bedrock/workflow.js.map +1 -1
  16. package/dist/adapters/sandbox/daytona/index.cjs +11 -3
  17. package/dist/adapters/sandbox/daytona/index.cjs.map +1 -1
  18. package/dist/adapters/sandbox/daytona/index.d.cts +5 -4
  19. package/dist/adapters/sandbox/daytona/index.d.ts +5 -4
  20. package/dist/adapters/sandbox/daytona/index.js +11 -3
  21. package/dist/adapters/sandbox/daytona/index.js.map +1 -1
  22. package/dist/adapters/sandbox/daytona/workflow.cjs +2 -0
  23. package/dist/adapters/sandbox/daytona/workflow.cjs.map +1 -1
  24. package/dist/adapters/sandbox/daytona/workflow.d.cts +1 -1
  25. package/dist/adapters/sandbox/daytona/workflow.d.ts +1 -1
  26. package/dist/adapters/sandbox/daytona/workflow.js +2 -0
  27. package/dist/adapters/sandbox/daytona/workflow.js.map +1 -1
  28. package/dist/adapters/sandbox/e2b/index.cjs +73 -12
  29. package/dist/adapters/sandbox/e2b/index.cjs.map +1 -1
  30. package/dist/adapters/sandbox/e2b/index.d.cts +26 -4
  31. package/dist/adapters/sandbox/e2b/index.d.ts +26 -4
  32. package/dist/adapters/sandbox/e2b/index.js +73 -12
  33. package/dist/adapters/sandbox/e2b/index.js.map +1 -1
  34. package/dist/adapters/sandbox/e2b/workflow.cjs +2 -0
  35. package/dist/adapters/sandbox/e2b/workflow.cjs.map +1 -1
  36. package/dist/adapters/sandbox/e2b/workflow.d.cts +1 -1
  37. package/dist/adapters/sandbox/e2b/workflow.d.ts +1 -1
  38. package/dist/adapters/sandbox/e2b/workflow.js +2 -0
  39. package/dist/adapters/sandbox/e2b/workflow.js.map +1 -1
  40. package/dist/adapters/sandbox/inmemory/index.cjs +8 -3
  41. package/dist/adapters/sandbox/inmemory/index.cjs.map +1 -1
  42. package/dist/adapters/sandbox/inmemory/index.d.cts +5 -4
  43. package/dist/adapters/sandbox/inmemory/index.d.ts +5 -4
  44. package/dist/adapters/sandbox/inmemory/index.js +8 -3
  45. package/dist/adapters/sandbox/inmemory/index.js.map +1 -1
  46. package/dist/adapters/sandbox/inmemory/workflow.cjs +2 -0
  47. package/dist/adapters/sandbox/inmemory/workflow.cjs.map +1 -1
  48. package/dist/adapters/sandbox/inmemory/workflow.d.cts +1 -1
  49. package/dist/adapters/sandbox/inmemory/workflow.d.ts +1 -1
  50. package/dist/adapters/sandbox/inmemory/workflow.js +2 -0
  51. package/dist/adapters/sandbox/inmemory/workflow.js.map +1 -1
  52. package/dist/adapters/thread/anthropic/index.cjs +94 -39
  53. package/dist/adapters/thread/anthropic/index.cjs.map +1 -1
  54. package/dist/adapters/thread/anthropic/index.d.cts +5 -5
  55. package/dist/adapters/thread/anthropic/index.d.ts +5 -5
  56. package/dist/adapters/thread/anthropic/index.js +94 -39
  57. package/dist/adapters/thread/anthropic/index.js.map +1 -1
  58. package/dist/adapters/thread/anthropic/workflow.cjs +7 -2
  59. package/dist/adapters/thread/anthropic/workflow.cjs.map +1 -1
  60. package/dist/adapters/thread/anthropic/workflow.d.cts +5 -5
  61. package/dist/adapters/thread/anthropic/workflow.d.ts +5 -5
  62. package/dist/adapters/thread/anthropic/workflow.js +7 -2
  63. package/dist/adapters/thread/anthropic/workflow.js.map +1 -1
  64. package/dist/adapters/thread/google-genai/index.cjs +77 -28
  65. package/dist/adapters/thread/google-genai/index.cjs.map +1 -1
  66. package/dist/adapters/thread/google-genai/index.d.cts +5 -5
  67. package/dist/adapters/thread/google-genai/index.d.ts +5 -5
  68. package/dist/adapters/thread/google-genai/index.js +77 -28
  69. package/dist/adapters/thread/google-genai/index.js.map +1 -1
  70. package/dist/adapters/thread/google-genai/workflow.cjs +7 -2
  71. package/dist/adapters/thread/google-genai/workflow.cjs.map +1 -1
  72. package/dist/adapters/thread/google-genai/workflow.d.cts +5 -5
  73. package/dist/adapters/thread/google-genai/workflow.d.ts +5 -5
  74. package/dist/adapters/thread/google-genai/workflow.js +7 -2
  75. package/dist/adapters/thread/google-genai/workflow.js.map +1 -1
  76. package/dist/adapters/thread/langchain/index.cjs +57 -10
  77. package/dist/adapters/thread/langchain/index.cjs.map +1 -1
  78. package/dist/adapters/thread/langchain/index.d.cts +5 -5
  79. package/dist/adapters/thread/langchain/index.d.ts +5 -5
  80. package/dist/adapters/thread/langchain/index.js +57 -10
  81. package/dist/adapters/thread/langchain/index.js.map +1 -1
  82. package/dist/adapters/thread/langchain/workflow.cjs +7 -2
  83. package/dist/adapters/thread/langchain/workflow.cjs.map +1 -1
  84. package/dist/adapters/thread/langchain/workflow.d.cts +5 -5
  85. package/dist/adapters/thread/langchain/workflow.d.ts +5 -5
  86. package/dist/adapters/thread/langchain/workflow.js +7 -2
  87. package/dist/adapters/thread/langchain/workflow.js.map +1 -1
  88. package/dist/index.cjs +322 -146
  89. package/dist/index.cjs.map +1 -1
  90. package/dist/index.d.cts +20 -14
  91. package/dist/index.d.ts +20 -14
  92. package/dist/index.js +323 -147
  93. package/dist/index.js.map +1 -1
  94. package/dist/{proxy-BjdFGPTm.d.ts → proxy-CUlKSvZS.d.ts} +1 -1
  95. package/dist/{proxy-7RnVaPdJ.d.cts → proxy-D_3x7RN4.d.cts} +1 -1
  96. package/dist/{thread-manager-CbpiGq1L.d.ts → thread-manager-CVu7o2cs.d.ts} +4 -2
  97. package/dist/{thread-manager-DzXm9eeI.d.cts → thread-manager-HSwyh28L.d.cts} +4 -2
  98. package/dist/{thread-manager-BBzNgQWH.d.cts → thread-manager-c1gPopAG.d.ts} +4 -2
  99. package/dist/{thread-manager-DjN5JYul.d.ts → thread-manager-wGi-LqIP.d.cts} +4 -2
  100. package/dist/{types-Mc_4BCfT.d.cts → types-BH_IRryz.d.ts} +10 -1
  101. package/dist/{types-yiXmqedU.d.ts → types-BaOw4hKI.d.cts} +10 -1
  102. package/dist/{types-DQ1l_gXL.d.cts → types-C06FwR96.d.cts} +121 -17
  103. package/dist/{types-wiGLvxWf.d.ts → types-DAsQ21Rt.d.ts} +1 -1
  104. package/dist/{types-CADc5V_P.d.ts → types-DNr31FzL.d.ts} +121 -17
  105. package/dist/{types-CBH54cwr.d.cts → types-lm8tMNJQ.d.cts} +1 -1
  106. package/dist/{types-DxCpFNv_.d.cts → types-yx0LzPGn.d.cts} +44 -5
  107. package/dist/{types-DxCpFNv_.d.ts → types-yx0LzPGn.d.ts} +44 -5
  108. package/dist/{workflow-DhtWRovz.d.cts → workflow-CSCkpwAL.d.ts} +2 -2
  109. package/dist/{workflow-P2pTSfKu.d.ts → workflow-DuvMZ8Vm.d.cts} +2 -2
  110. package/dist/workflow.cjs +274 -130
  111. package/dist/workflow.cjs.map +1 -1
  112. package/dist/workflow.d.cts +3 -3
  113. package/dist/workflow.d.ts +3 -3
  114. package/dist/workflow.js +275 -131
  115. package/dist/workflow.js.map +1 -1
  116. package/package.json +2 -2
  117. package/src/adapters/sandbox/bedrock/filesystem.ts +6 -12
  118. package/src/adapters/sandbox/bedrock/index.ts +22 -11
  119. package/src/adapters/sandbox/bedrock/proxy.ts +2 -0
  120. package/src/adapters/sandbox/daytona/index.ts +18 -3
  121. package/src/adapters/sandbox/daytona/proxy.ts +2 -0
  122. package/src/adapters/sandbox/e2b/filesystem.ts +5 -4
  123. package/src/adapters/sandbox/e2b/index.ts +87 -14
  124. package/src/adapters/sandbox/e2b/proxy.ts +2 -0
  125. package/src/adapters/sandbox/e2b/types.ts +16 -0
  126. package/src/adapters/sandbox/inmemory/index.ts +17 -3
  127. package/src/adapters/sandbox/inmemory/proxy.ts +2 -0
  128. package/src/adapters/thread/anthropic/activities.ts +58 -26
  129. package/src/adapters/thread/anthropic/model-invoker.ts +18 -7
  130. package/src/adapters/thread/anthropic/proxy.ts +6 -2
  131. package/src/adapters/thread/anthropic/thread-manager.test.ts +26 -7
  132. package/src/adapters/thread/anthropic/thread-manager.ts +63 -46
  133. package/src/adapters/thread/google-genai/activities.ts +20 -2
  134. package/src/adapters/thread/google-genai/model-invoker.ts +27 -7
  135. package/src/adapters/thread/google-genai/proxy.ts +6 -2
  136. package/src/adapters/thread/google-genai/thread-manager.test.ts +13 -3
  137. package/src/adapters/thread/google-genai/thread-manager.ts +57 -33
  138. package/src/adapters/thread/langchain/activities.ts +55 -24
  139. package/src/adapters/thread/langchain/hooks.test.ts +36 -49
  140. package/src/adapters/thread/langchain/hooks.ts +18 -5
  141. package/src/adapters/thread/langchain/model-invoker.ts +5 -4
  142. package/src/adapters/thread/langchain/proxy.ts +6 -2
  143. package/src/adapters/thread/langchain/thread-manager.test.ts +5 -1
  144. package/src/adapters/thread/langchain/thread-manager.ts +23 -9
  145. package/src/index.ts +4 -1
  146. package/src/lib/activity.ts +16 -6
  147. package/src/lib/hooks/types.ts +6 -6
  148. package/src/lib/lifecycle.ts +18 -3
  149. package/src/lib/model/proxy.ts +2 -2
  150. package/src/lib/model/types.ts +10 -0
  151. package/src/lib/observability/hooks.ts +4 -5
  152. package/src/lib/observability/index.ts +1 -4
  153. package/src/lib/sandbox/manager.ts +45 -20
  154. package/src/lib/sandbox/node-fs.ts +3 -6
  155. package/src/lib/sandbox/sandbox.test.ts +36 -3
  156. package/src/lib/sandbox/tree.integration.test.ts +10 -3
  157. package/src/lib/sandbox/types.ts +60 -6
  158. package/src/lib/session/session-edge-cases.integration.test.ts +316 -14
  159. package/src/lib/session/session.integration.test.ts +161 -1
  160. package/src/lib/session/session.ts +106 -21
  161. package/src/lib/session/types.ts +25 -5
  162. package/src/lib/skills/fs-provider.ts +12 -8
  163. package/src/lib/skills/handler.ts +1 -1
  164. package/src/lib/skills/parse.ts +3 -1
  165. package/src/lib/skills/register.ts +1 -3
  166. package/src/lib/skills/skills.integration.test.ts +25 -15
  167. package/src/lib/state/manager.integration.test.ts +12 -2
  168. package/src/lib/subagent/define.ts +1 -1
  169. package/src/lib/subagent/handler.ts +186 -71
  170. package/src/lib/subagent/index.ts +1 -5
  171. package/src/lib/subagent/register.ts +3 -2
  172. package/src/lib/subagent/signals.ts +1 -10
  173. package/src/lib/subagent/subagent.integration.test.ts +526 -248
  174. package/src/lib/subagent/tool.ts +4 -3
  175. package/src/lib/subagent/types.ts +50 -20
  176. package/src/lib/subagent/workflow.ts +9 -49
  177. package/src/lib/thread/id.test.ts +1 -1
  178. package/src/lib/thread/id.ts +1 -2
  179. package/src/lib/thread/manager.ts +18 -0
  180. package/src/lib/thread/proxy.ts +4 -4
  181. package/src/lib/thread/types.ts +20 -3
  182. package/src/lib/tool-router/index.ts +3 -5
  183. package/src/lib/tool-router/router-edge-cases.integration.test.ts +93 -1
  184. package/src/lib/tool-router/router.integration.test.ts +12 -0
  185. package/src/lib/tool-router/router.ts +90 -16
  186. package/src/lib/tool-router/types.ts +45 -4
  187. package/src/lib/tool-router/with-sandbox.ts +19 -5
  188. package/src/lib/virtual-fs/filesystem.ts +1 -1
  189. package/src/lib/virtual-fs/index.ts +5 -1
  190. package/src/lib/virtual-fs/mutations.ts +2 -4
  191. package/src/lib/virtual-fs/queries.ts +9 -5
  192. package/src/lib/virtual-fs/types.ts +4 -1
  193. package/src/lib/virtual-fs/virtual-fs.test.ts +9 -11
  194. package/src/lib/workflow.test.ts +7 -4
  195. package/src/lib/workflow.ts +1 -5
  196. package/src/tools/ask-user-question/tool.ts +1 -3
  197. package/src/tools/glob/handler.ts +1 -4
  198. package/src/tools/task-get/handler.ts +4 -5
  199. package/src/tools/task-list/handler.ts +1 -4
  200. package/src/tools/task-update/handler.ts +4 -5
  201. package/src/workflow.ts +22 -7
  202. package/tsup.config.ts +9 -6
  203. package/src/lib/.env +0 -1
  204. package/src/tools/bash/.env +0 -1
@@ -24,8 +24,10 @@ import { createLangChainModelInvoker } from "./model-invoker";
24
24
 
25
25
  const ADAPTER_PREFIX = "langChain" as const;
26
26
 
27
- export type LangChainThreadOps<TScope extends string = ""> =
28
- PrefixedThreadOps<ScopedPrefix<TScope, typeof ADAPTER_PREFIX>, LangChainContent>;
27
+ export type LangChainThreadOps<TScope extends string = ""> = PrefixedThreadOps<
28
+ ScopedPrefix<TScope, typeof ADAPTER_PREFIX>,
29
+ LangChainContent
30
+ >;
29
31
 
30
32
  export interface LangChainAdapterConfig {
31
33
  redis: Redis;
@@ -61,9 +63,7 @@ export interface LangChainAdapter {
61
63
  * // → { langChainCodingAgentInitializeThread, langChainCodingAgentAppendHumanMessage, … }
62
64
  * ```
63
65
  */
64
- createActivities<S extends string = "">(
65
- scope?: S,
66
- ): LangChainThreadOps<S>;
66
+ createActivities<S extends string = "">(scope?: S): LangChainThreadOps<S>;
67
67
 
68
68
  /**
69
69
  * Identity wrapper that types a tool handler for this adapter.
@@ -72,8 +72,8 @@ export interface LangChainAdapter {
72
72
  wrapHandler<TArgs, TResult, TContext extends RouterContext = RouterContext>(
73
73
  handler: (
74
74
  args: TArgs,
75
- context: TContext,
76
- ) => Promise<ToolHandlerResponse<TResult, LangChainToolResponse>>,
75
+ context: TContext
76
+ ) => Promise<ToolHandlerResponse<TResult, LangChainToolResponse>>
77
77
  ): ActivityToolHandler<TArgs, TResult, TContext, LangChainToolResponse>;
78
78
  }
79
79
 
@@ -113,13 +113,20 @@ export interface LangChainAdapter {
113
113
  * ```
114
114
  */
115
115
  export function createLangChainAdapter(
116
- config: LangChainAdapterConfig,
116
+ config: LangChainAdapterConfig
117
117
  ): LangChainAdapter {
118
118
  const { redis } = config;
119
119
 
120
120
  const threadOps: ThreadOps<LangChainContent> = {
121
- async initializeThread(threadId: string, threadKey?: string): Promise<void> {
122
- const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });
121
+ async initializeThread(
122
+ threadId: string,
123
+ threadKey?: string
124
+ ): Promise<void> {
125
+ const thread = createLangChainThreadManager({
126
+ redis,
127
+ threadId,
128
+ key: threadKey,
129
+ });
123
130
  await thread.initialize();
124
131
  },
125
132
 
@@ -127,9 +134,13 @@ export function createLangChainAdapter(
127
134
  threadId: string,
128
135
  id: string,
129
136
  content: LangChainContent,
130
- threadKey?: string,
137
+ threadKey?: string
131
138
  ): Promise<void> {
132
- const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });
139
+ const thread = createLangChainThreadManager({
140
+ redis,
141
+ threadId,
142
+ key: threadKey,
143
+ });
133
144
  await thread.appendUserMessage(id, content);
134
145
  },
135
146
 
@@ -137,15 +148,23 @@ export function createLangChainAdapter(
137
148
  threadId: string,
138
149
  id: string,
139
150
  content: LangChainSystemContent,
140
- threadKey?: string,
151
+ threadKey?: string
141
152
  ): Promise<void> {
142
- const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });
153
+ const thread = createLangChainThreadManager({
154
+ redis,
155
+ threadId,
156
+ key: threadKey,
157
+ });
143
158
  await thread.appendSystemMessage(id, content);
144
159
  },
145
160
 
146
161
  async appendToolResult(id: string, cfg: ToolResultConfig): Promise<void> {
147
162
  const { threadId, threadKey, toolCallId, content } = cfg;
148
- const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });
163
+ const thread = createLangChainThreadManager({
164
+ redis,
165
+ threadId,
166
+ key: threadKey,
167
+ });
149
168
  await thread.appendToolResult(id, toolCallId, "", content);
150
169
  },
151
170
 
@@ -153,9 +172,13 @@ export function createLangChainAdapter(
153
172
  threadId: string,
154
173
  id: string,
155
174
  message: StoredMessage,
156
- threadKey?: string,
175
+ threadKey?: string
157
176
  ): Promise<void> {
158
- const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });
177
+ const thread = createLangChainThreadManager({
178
+ redis,
179
+ threadId,
180
+ key: threadKey,
181
+ });
159
182
  const patched = { ...message, data: { ...message.data, id } };
160
183
  await thread.append([patched]);
161
184
  },
@@ -163,7 +186,7 @@ export function createLangChainAdapter(
163
186
  async forkThread(
164
187
  sourceThreadId: string,
165
188
  targetThreadId: string,
166
- threadKey?: string,
189
+ threadKey?: string
167
190
  ): Promise<void> {
168
191
  const thread = createLangChainThreadManager({
169
192
  redis,
@@ -172,24 +195,32 @@ export function createLangChainAdapter(
172
195
  });
173
196
  await thread.fork(targetThreadId);
174
197
  },
198
+
199
+ async truncateThread(
200
+ threadId: string,
201
+ length: number,
202
+ threadKey?: string,
203
+ ): Promise<void> {
204
+ const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });
205
+ await thread.truncate(length);
206
+ },
175
207
  };
176
208
 
177
209
  function createActivities<S extends string = "">(
178
- scope?: S,
210
+ scope?: S
179
211
  ): LangChainThreadOps<S> {
180
212
  const prefix = scope
181
213
  ? `${ADAPTER_PREFIX}${scope.charAt(0).toUpperCase()}${scope.slice(1)}`
182
214
  : ADAPTER_PREFIX;
183
- const cap = (s: string): string =>
184
- s.charAt(0).toUpperCase() + s.slice(1);
215
+ const cap = (s: string): string => s.charAt(0).toUpperCase() + s.slice(1);
185
216
  return Object.fromEntries(
186
- Object.entries(threadOps).map(([k, v]) => [`${prefix}${cap(k)}`, v]),
217
+ Object.entries(threadOps).map(([k, v]) => [`${prefix}${cap(k)}`, v])
187
218
  ) as LangChainThreadOps<S>;
188
219
  }
189
220
 
190
221
  const makeInvoker = (
191
222
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
192
- model: BaseChatModel<any>,
223
+ model: BaseChatModel<any>
193
224
  ): ModelInvoker<StoredMessage> =>
194
225
  createLangChainModelInvoker({ redis, model, hooks: config.hooks });
195
226
 
@@ -198,7 +229,7 @@ export function createLangChainAdapter(
198
229
  : () => {
199
230
  throw new Error(
200
231
  "No default model provided to createLangChainAdapter. " +
201
- "Either pass `model` in the config or use `createModelInvoker(model)` instead.",
232
+ "Either pass `model` in the config or use `createModelInvoker(model)` instead."
202
233
  );
203
234
  };
204
235
 
@@ -3,11 +3,14 @@ import { HumanMessage, AIMessage } from "@langchain/core/messages";
3
3
  import type { BaseMessage } from "@langchain/core/messages";
4
4
  import { appendCachePoint } from "./hooks";
5
5
 
6
- const cacheBlock = { type: "cache_control" as const, cache_control: { type: "ephemeral" as const } };
6
+ const cacheBlock = {
7
+ type: "cache_control" as const,
8
+ cache_control: { type: "ephemeral" as const },
9
+ };
7
10
 
8
11
  function applyHook(
9
12
  messages: BaseMessage[],
10
- hook: ReturnType<typeof appendCachePoint>,
13
+ hook: ReturnType<typeof appendCachePoint>
11
14
  ): BaseMessage[] {
12
15
  return messages.map((m, i, arr) => hook(m, i, arr));
13
16
  }
@@ -47,29 +50,25 @@ describe("appendCachePoint", () => {
47
50
  it("deduplicates within the last message", () => {
48
51
  const messages: BaseMessage[] = [
49
52
  new HumanMessage({
50
- content: [
51
- { type: "text", text: "hello" },
52
- cacheBlock,
53
- ],
53
+ content: [{ type: "text", text: "hello" }, cacheBlock],
54
54
  }),
55
55
  ];
56
56
  const hook = appendCachePoint(cacheBlock);
57
57
  const result = applyHook(messages, hook);
58
58
 
59
- const blocks = (messageAt(result, 0).content as Array<{ type: string }>).filter(
60
- (b) => b.type === cacheBlock.type,
61
- );
59
+ const blocks = (
60
+ messageAt(result, 0).content as Array<{ type: string }>
61
+ ).filter((b) => b.type === cacheBlock.type);
62
62
  expect(blocks).toHaveLength(1);
63
63
  });
64
64
 
65
65
  it("strips old cache blocks when total would exceed maxBlocks", () => {
66
- const messages: BaseMessage[] = Array.from({ length: 6 }, (_, i) =>
67
- new HumanMessage({
68
- content: [
69
- { type: "text", text: `msg ${i}` },
70
- cacheBlock,
71
- ],
72
- }),
66
+ const messages: BaseMessage[] = Array.from(
67
+ { length: 6 },
68
+ (_, i) =>
69
+ new HumanMessage({
70
+ content: [{ type: "text", text: `msg ${i}` }, cacheBlock],
71
+ })
73
72
  );
74
73
  const hook = appendCachePoint(cacheBlock, { maxBlocks: 4 });
75
74
  const result = applyHook(messages, hook);
@@ -78,13 +77,12 @@ describe("appendCachePoint", () => {
78
77
  });
79
78
 
80
79
  it("keeps the most recent cache blocks", () => {
81
- const messages: BaseMessage[] = Array.from({ length: 6 }, (_, i) =>
82
- new HumanMessage({
83
- content: [
84
- { type: "text", text: `msg ${i}` },
85
- cacheBlock,
86
- ],
87
- }),
80
+ const messages: BaseMessage[] = Array.from(
81
+ { length: 6 },
82
+ (_, i) =>
83
+ new HumanMessage({
84
+ content: [{ type: "text", text: `msg ${i}` }, cacheBlock],
85
+ })
88
86
  );
89
87
  const hook = appendCachePoint(cacheBlock, { maxBlocks: 4 });
90
88
  const result = applyHook(messages, hook);
@@ -101,13 +99,12 @@ describe("appendCachePoint", () => {
101
99
  });
102
100
 
103
101
  it("respects a custom maxBlocks value", () => {
104
- const messages: BaseMessage[] = Array.from({ length: 5 }, (_, i) =>
105
- new HumanMessage({
106
- content: [
107
- { type: "text", text: `msg ${i}` },
108
- cacheBlock,
109
- ],
110
- }),
102
+ const messages: BaseMessage[] = Array.from(
103
+ { length: 5 },
104
+ (_, i) =>
105
+ new HumanMessage({
106
+ content: [{ type: "text", text: `msg ${i}` }, cacheBlock],
107
+ })
111
108
  );
112
109
  const hook = appendCachePoint(cacheBlock, { maxBlocks: 2 });
113
110
  const result = applyHook(messages, hook);
@@ -141,13 +138,12 @@ describe("appendCachePoint", () => {
141
138
  });
142
139
 
143
140
  it("defaults maxBlocks to 4", () => {
144
- const messages: BaseMessage[] = Array.from({ length: 8 }, (_, i) =>
145
- new HumanMessage({
146
- content: [
147
- { type: "text", text: `msg ${i}` },
148
- cacheBlock,
149
- ],
150
- }),
141
+ const messages: BaseMessage[] = Array.from(
142
+ { length: 8 },
143
+ (_, i) =>
144
+ new HumanMessage({
145
+ content: [{ type: "text", text: `msg ${i}` }, cacheBlock],
146
+ })
151
147
  );
152
148
  const hook = appendCachePoint(cacheBlock);
153
149
  const result = applyHook(messages, hook);
@@ -165,22 +161,13 @@ describe("appendCachePoint", () => {
165
161
  ],
166
162
  }),
167
163
  new HumanMessage({
168
- content: [
169
- { type: "text", text: "msg 1" },
170
- cacheBlock,
171
- ],
164
+ content: [{ type: "text", text: "msg 1" }, cacheBlock],
172
165
  }),
173
166
  new HumanMessage({
174
- content: [
175
- { type: "text", text: "msg 2" },
176
- cacheBlock,
177
- ],
167
+ content: [{ type: "text", text: "msg 2" }, cacheBlock],
178
168
  }),
179
169
  new HumanMessage({
180
- content: [
181
- { type: "text", text: "msg 3" },
182
- cacheBlock,
183
- ],
170
+ content: [{ type: "text", text: "msg 3" }, cacheBlock],
184
171
  }),
185
172
  new HumanMessage("last"),
186
173
  ];
@@ -12,8 +12,12 @@ type ContentBlock = MessageContent extends (infer U)[] | string ? U : never;
12
12
  */
13
13
  export function appendCachePoint(
14
14
  block: ContentBlock,
15
- { maxBlocks = 4 }: { maxBlocks?: number } = {},
16
- ): (message: BaseMessage, index: number, messages: readonly BaseMessage[]) => BaseMessage {
15
+ { maxBlocks = 4 }: { maxBlocks?: number } = {}
16
+ ): (
17
+ message: BaseMessage,
18
+ index: number,
19
+ messages: readonly BaseMessage[]
20
+ ) => BaseMessage {
17
21
  return (message, index, messages) => {
18
22
  const isLast = index === messages.length - 1;
19
23
 
@@ -23,13 +27,19 @@ export function appendCachePoint(
23
27
  if (content.some((b) => b.type === block.type)) return message;
24
28
  message.content = [...content, block];
25
29
  } else if (typeof content === "string") {
26
- message.content = [{ type: "text", text: content }, block] satisfies MessageContent;
30
+ message.content = [
31
+ { type: "text", text: content },
32
+ block,
33
+ ] satisfies MessageContent;
27
34
  }
28
35
  return message;
29
36
  }
30
37
 
31
38
  const { content } = message;
32
- if (!Array.isArray(content) || !content.some((b) => b.type === block.type)) {
39
+ if (
40
+ !Array.isArray(content) ||
41
+ !content.some((b) => b.type === block.type)
42
+ ) {
33
43
  return message;
34
44
  }
35
45
 
@@ -40,7 +50,10 @@ export function appendCachePoint(
40
50
  const msg = messages[i];
41
51
  if (!msg) continue;
42
52
  const c = msg.content;
43
- if (Array.isArray(c) && c.some((b: ContentBlock) => b.type === block.type)) {
53
+ if (
54
+ Array.isArray(c) &&
55
+ c.some((b: ContentBlock) => b.type === block.type)
56
+ ) {
44
57
  cacheBlocksAfter++;
45
58
  }
46
59
  }
@@ -9,7 +9,7 @@ import {
9
9
  } from "./thread-manager";
10
10
  import { getActivityContext } from "../../../lib/activity";
11
11
 
12
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
+
13
13
  export interface LangChainModelInvokerConfig<
14
14
  TModel extends BaseChatModel<any> = BaseChatModel<any>,
15
15
  > {
@@ -40,7 +40,7 @@ export interface LangChainModelInvokerConfig<
40
40
  * return { ...createRunAgentActivity(client, invoker, "myAgent") };
41
41
  * ```
42
42
  */
43
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
+
44
44
  export function createLangChainModelInvoker<
45
45
  TModel extends BaseChatModel<any> = BaseChatModel<any>,
46
46
  >({ redis, model, hooks }: LangChainModelInvokerConfig<TModel>) {
@@ -58,7 +58,7 @@ export function createLangChainModelInvoker<
58
58
  });
59
59
  const runId = uuidv4();
60
60
 
61
- const { messages } = await thread.prepareForInvocation();
61
+ const { messages, storedLength } = await thread.prepareForInvocation();
62
62
 
63
63
  const heartbeatInterval = heartbeat
64
64
  ? setInterval(() => heartbeat(), 30_000)
@@ -97,6 +97,7 @@ export function createLangChainModelInvoker<
97
97
  response.usage_metadata?.input_token_details?.cache_read ||
98
98
  (providerUsage.cacheReadInputTokens as number | undefined),
99
99
  },
100
+ threadLengthAtCall: storedLength,
100
101
  };
101
102
  } finally {
102
103
  if (heartbeatInterval) clearInterval(heartbeatInterval);
@@ -109,7 +110,7 @@ export function createLangChainModelInvoker<
109
110
  * Convenience wrapper around createLangChainModelInvoker for cases where
110
111
  * you don't need to reuse the invoker.
111
112
  */
112
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
113
+
113
114
  export async function invokeLangChainModel<
114
115
  TModel extends BaseChatModel<any> = BaseChatModel<any>,
115
116
  >({
@@ -27,7 +27,11 @@ const ADAPTER_PREFIX = "langChain";
27
27
 
28
28
  export function proxyLangChainThreadOps(
29
29
  scope?: string,
30
- options?: Parameters<typeof createThreadOpsProxy>[2],
30
+ options?: Parameters<typeof createThreadOpsProxy>[2]
31
31
  ): ActivityInterfaceFor<ThreadOps<LangChainContent>> {
32
- return createThreadOpsProxy(ADAPTER_PREFIX, scope, options) as ActivityInterfaceFor<ThreadOps<LangChainContent>>;
32
+ return createThreadOpsProxy(
33
+ ADAPTER_PREFIX,
34
+ scope,
35
+ options
36
+ ) as ActivityInterfaceFor<ThreadOps<LangChainContent>>;
33
37
  }
@@ -91,7 +91,11 @@ describe("LangChain thread manager hooks", () => {
91
91
 
92
92
  await tm.prepareForInvocation();
93
93
 
94
- const args = hook.mock.calls[0] as unknown as [unknown, number, unknown[]];
94
+ const args = hook.mock.calls[0] as unknown as [
95
+ unknown,
96
+ number,
97
+ unknown[],
98
+ ];
95
99
  expect(args[2]).toHaveLength(2);
96
100
  });
97
101
  });
@@ -23,7 +23,10 @@ export type LangChainContent = string | MessageContent;
23
23
  /** SDK-native content type for LangChain system messages */
24
24
  export type LangChainSystemContent = string | MessageContent;
25
25
 
26
- export type LangChainThreadManagerHooks = ThreadManagerHooks<StoredMessage, BaseMessage>;
26
+ export type LangChainThreadManagerHooks = ThreadManagerHooks<
27
+ StoredMessage,
28
+ BaseMessage
29
+ >;
27
30
 
28
31
  export interface LangChainThreadManagerConfig {
29
32
  redis: Redis;
@@ -36,11 +39,17 @@ export interface LangChainThreadManagerConfig {
36
39
  /** Prepared payload ready to send to a LangChain chat model */
37
40
  export interface LangChainInvocationPayload {
38
41
  messages: BaseMessage[];
42
+ /** Number of stored messages loaded from Redis before preparation. */
43
+ storedLength: number;
39
44
  }
40
45
 
41
46
  /** Thread manager with LangChain StoredMessage convenience helpers */
42
- export interface LangChainThreadManager
43
- extends ProviderThreadManager<StoredMessage, LangChainContent, JsonValue, LangChainSystemContent> {
47
+ export interface LangChainThreadManager extends ProviderThreadManager<
48
+ StoredMessage,
49
+ LangChainContent,
50
+ JsonValue,
51
+ LangChainSystemContent
52
+ > {
44
53
  appendAIMessage(id: string, content: string | MessageContent): Promise<void>;
45
54
  prepareForInvocation(): Promise<LangChainInvocationPayload>;
46
55
  }
@@ -63,7 +72,7 @@ function storedMessageId(msg: StoredMessage): string {
63
72
  * appending typed LangChain messages.
64
73
  */
65
74
  export function createLangChainThreadManager(
66
- config: LangChainThreadManagerConfig,
75
+ config: LangChainThreadManagerConfig
67
76
  ): LangChainThreadManager {
68
77
  const baseConfig: ThreadManagerConfig<StoredMessage> = {
69
78
  redis: config.redis,
@@ -77,7 +86,7 @@ export function createLangChainThreadManager(
77
86
  const helpers: Omit<LangChainThreadManager, keyof typeof base> = {
78
87
  async appendUserMessage(
79
88
  id: string,
80
- content: LangChainContent,
89
+ content: LangChainContent
81
90
  ): Promise<void> {
82
91
  await base.append([
83
92
  new HumanMessage({ id, content: content as MessageContent }).toDict(),
@@ -86,7 +95,7 @@ export function createLangChainThreadManager(
86
95
 
87
96
  async appendSystemMessage(
88
97
  id: string,
89
- content: LangChainSystemContent,
98
+ content: LangChainSystemContent
90
99
  ): Promise<void> {
91
100
  await base.initialize();
92
101
  await base.append([
@@ -99,7 +108,7 @@ export function createLangChainThreadManager(
99
108
 
100
109
  async appendAIMessage(
101
110
  id: string,
102
- content: string | MessageContent,
111
+ content: string | MessageContent
103
112
  ): Promise<void> {
104
113
  await base.append([
105
114
  new AIMessage({ id, content: content as MessageContent }).toDict(),
@@ -110,10 +119,14 @@ export function createLangChainThreadManager(
110
119
  id: string,
111
120
  _toolCallId: string,
112
121
  _toolName: string,
113
- content: JsonValue,
122
+ content: JsonValue
114
123
  ): Promise<void> {
115
124
  await base.append([
116
- new ToolMessage({ id, content: content as MessageContent, tool_call_id: _toolCallId }).toDict(),
125
+ new ToolMessage({
126
+ id,
127
+ content: content as MessageContent,
128
+ tool_call_id: _toolCallId,
129
+ }).toDict(),
117
130
  ]);
118
131
  },
119
132
 
@@ -128,6 +141,7 @@ export function createLangChainThreadManager(
128
141
  messages: onPreparedMessage
129
142
  ? messages.map((msg, i) => onPreparedMessage(msg, i, messages))
130
143
  : messages,
144
+ storedLength: stored.length,
131
145
  };
132
146
  },
133
147
  };
package/src/index.ts CHANGED
@@ -58,7 +58,10 @@ export type { AgentStateContext } from "./lib/activity";
58
58
 
59
59
  // Sandbox (activity-side: manager + Node.js filesystem adapter)
60
60
  export { SandboxManager } from "./lib/sandbox/manager";
61
- export type { SandboxManagerHooks, PreCreateHookResult } from "./lib/sandbox/manager";
61
+ export type {
62
+ SandboxManagerHooks,
63
+ PreCreateHookResult,
64
+ } from "./lib/sandbox/manager";
62
65
  export { NodeFsSandboxFileSystem } from "./lib/sandbox/node-fs";
63
66
 
64
67
  // Virtual filesystem (activity-side)
@@ -59,10 +59,13 @@ export async function queryParentWorkflowState<T>(
59
59
  * };
60
60
  * ```
61
61
  */
62
- export function createRunAgentActivity<R, S extends BaseAgentState = BaseAgentState>(
62
+ export function createRunAgentActivity<
63
+ R,
64
+ S extends BaseAgentState = BaseAgentState,
65
+ >(
63
66
  client: WorkflowClient,
64
67
  handler: (config: RunAgentConfig & { state: S }) => Promise<R>,
65
- scope: string,
68
+ scope: string
66
69
  ): Record<string, (config: RunAgentConfig) => Promise<R>> {
67
70
  const name = `run${scope.charAt(0).toUpperCase()}${scope.slice(1)}`;
68
71
  return {
@@ -76,7 +79,9 @@ export function createRunAgentActivity<R, S extends BaseAgentState = BaseAgentSt
76
79
  /**
77
80
  * Context injected into tool handlers created via {@link withParentWorkflowState}.
78
81
  */
79
- export interface AgentStateContext<S extends BaseAgentState = BaseAgentState> extends RouterContext {
82
+ export interface AgentStateContext<
83
+ S extends BaseAgentState = BaseAgentState,
84
+ > extends RouterContext {
80
85
  state: S;
81
86
  }
82
87
 
@@ -101,12 +106,17 @@ export interface AgentStateContext<S extends BaseAgentState = BaseAgentState> ex
101
106
  * );
102
107
  * ```
103
108
  */
104
- export function withParentWorkflowState<TArgs, TResult, S extends BaseAgentState = BaseAgentState, TToolResponse = JsonValue>(
109
+ export function withParentWorkflowState<
110
+ TArgs,
111
+ TResult,
112
+ S extends BaseAgentState = BaseAgentState,
113
+ TToolResponse = JsonValue,
114
+ >(
105
115
  client: WorkflowClient,
106
116
  handler: (
107
117
  args: TArgs,
108
- context: AgentStateContext<S>,
109
- ) => Promise<ToolHandlerResponse<TResult, TToolResponse>>,
118
+ context: AgentStateContext<S>
119
+ ) => Promise<ToolHandlerResponse<TResult, TToolResponse>>
110
120
  ): ActivityToolHandler<TArgs, TResult, RouterContext, TToolResponse> {
111
121
  return async (args, context) => {
112
122
  const state = await queryParentWorkflowState<S>(client);
@@ -1,8 +1,5 @@
1
1
  import type { SessionExitReason } from "../types";
2
- import type {
3
- ToolMap,
4
- ToolRouterHooks,
5
- } from "../tool-router/types";
2
+ import type { ToolMap, ToolRouterHooks } from "../tool-router/types";
6
3
 
7
4
  // ============================================================================
8
5
  // Session Lifecycle Hooks
@@ -85,8 +82,11 @@ export type PostHumanMessageAppendHook<TContent = unknown> = (
85
82
  * (consumed by the router) with session/message lifecycle hooks
86
83
  * (consumed directly by the session).
87
84
  */
88
- export interface Hooks<T extends ToolMap, TResult = unknown, TContent = unknown>
89
- extends ToolRouterHooks<T, TResult> {
85
+ export interface Hooks<
86
+ T extends ToolMap,
87
+ TResult = unknown,
88
+ TContent = unknown,
89
+ > extends ToolRouterHooks<T, TResult> {
90
90
  /** Called before each human message is appended to the thread */
91
91
  onPreHumanMessageAppend?: PreHumanMessageAppendHook<TContent>;
92
92
  /** Called after each human message is appended to the thread */
@@ -19,6 +19,8 @@ export type ThreadInit =
19
19
  // Sandbox lifecycle
20
20
  // ============================================================================
21
21
 
22
+ import type { SandboxCreateOptions, SandboxSnapshot } from "./sandbox/types";
23
+
22
24
  /**
23
25
  * Sandbox initialization strategy.
24
26
  *
@@ -29,14 +31,24 @@ export type ThreadInit =
29
31
  * Paused sandboxes are automatically resumed. The shutdown policy applies
30
32
  * on exit.
31
33
  * - `"fork"` — fork from an existing (or paused) sandbox; a new sandbox is
32
- * created and owned by this session.
34
+ * created and owned by this session. `options` is an optional per-call
35
+ * override merged on top of the provider's static defaults.
36
+ * - `"from-snapshot"` — restore a fresh sandbox from a previously captured
37
+ * {@link SandboxSnapshot}. The new sandbox is owned by this session.
38
+ * `options` is an optional per-call override merged on top of the
39
+ * provider's static defaults.
33
40
  * - `"inherit"` — use a sandbox owned by someone else (e.g. a parent agent).
34
41
  * The session will **not** manage its lifecycle on exit.
35
42
  */
36
43
  export type SandboxInit =
37
44
  | { mode: "new"; ctx?: unknown }
38
45
  | { mode: "continue"; sandboxId: string }
39
- | { mode: "fork"; sandboxId: string }
46
+ | { mode: "fork"; sandboxId: string; options?: SandboxCreateOptions }
47
+ | {
48
+ mode: "from-snapshot";
49
+ snapshot: SandboxSnapshot;
50
+ options?: SandboxCreateOptions;
51
+ }
40
52
  | {
41
53
  mode: "inherit";
42
54
  sandboxId: string;
@@ -48,8 +60,11 @@ export type SandboxInit =
48
60
  * - `"destroy"` — tear down the sandbox entirely.
49
61
  * - `"pause"` — pause the sandbox so it can be resumed later.
50
62
  * - `"keep"` — leave the sandbox running (no-op on exit).
63
+ * - `"snapshot"` — capture a snapshot then destroy the sandbox. The snapshot
64
+ * is surfaced on the session result so the caller can reuse it to spawn
65
+ * future sandboxes.
51
66
  */
52
- export type SandboxShutdown = "destroy" | "pause" | "keep";
67
+ export type SandboxShutdown = "destroy" | "pause" | "keep" | "snapshot";
53
68
 
54
69
  /**
55
70
  * Extended shutdown options available to subagent workflows.
@@ -29,7 +29,7 @@ import type { RunAgentConfig } from "../types";
29
29
 
30
30
  export function proxyRunAgent<M = unknown>(
31
31
  scope?: string,
32
- options?: Parameters<typeof proxyActivities>[0],
32
+ options?: Parameters<typeof proxyActivities>[0]
33
33
  ): (config: RunAgentConfig) => Promise<AgentResponse<M>> {
34
34
  const resolvedScope = scope ?? workflowInfo().workflowType;
35
35
  const name = `run${resolvedScope.charAt(0).toUpperCase()}${resolvedScope.slice(1)}`;
@@ -44,7 +44,7 @@ export function proxyRunAgent<M = unknown>(
44
44
  maximumInterval: "2m",
45
45
  backoffCoefficient: 3,
46
46
  },
47
- },
47
+ }
48
48
  );
49
49
  return acts[name] as (config: RunAgentConfig) => Promise<AgentResponse<M>>;
50
50
  }