zeitlich 0.2.21 → 0.2.23

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 (129) hide show
  1. package/README.md +303 -105
  2. package/dist/adapters/sandbox/daytona/index.cjs +7 -1
  3. package/dist/adapters/sandbox/daytona/index.cjs.map +1 -1
  4. package/dist/adapters/sandbox/daytona/index.d.cts +3 -1
  5. package/dist/adapters/sandbox/daytona/index.d.ts +3 -1
  6. package/dist/adapters/sandbox/daytona/index.js +7 -1
  7. package/dist/adapters/sandbox/daytona/index.js.map +1 -1
  8. package/dist/adapters/sandbox/daytona/workflow.cjs +33 -0
  9. package/dist/adapters/sandbox/daytona/workflow.cjs.map +1 -0
  10. package/dist/adapters/sandbox/daytona/workflow.d.cts +27 -0
  11. package/dist/adapters/sandbox/daytona/workflow.d.ts +27 -0
  12. package/dist/adapters/sandbox/daytona/workflow.js +31 -0
  13. package/dist/adapters/sandbox/daytona/workflow.js.map +1 -0
  14. package/dist/adapters/sandbox/inmemory/index.cjs +18 -1
  15. package/dist/adapters/sandbox/inmemory/index.cjs.map +1 -1
  16. package/dist/adapters/sandbox/inmemory/index.d.cts +4 -2
  17. package/dist/adapters/sandbox/inmemory/index.d.ts +4 -2
  18. package/dist/adapters/sandbox/inmemory/index.js +18 -1
  19. package/dist/adapters/sandbox/inmemory/index.js.map +1 -1
  20. package/dist/adapters/sandbox/inmemory/workflow.cjs +33 -0
  21. package/dist/adapters/sandbox/inmemory/workflow.cjs.map +1 -0
  22. package/dist/adapters/sandbox/inmemory/workflow.d.cts +25 -0
  23. package/dist/adapters/sandbox/inmemory/workflow.d.ts +25 -0
  24. package/dist/adapters/sandbox/inmemory/workflow.js +31 -0
  25. package/dist/adapters/sandbox/inmemory/workflow.js.map +1 -0
  26. package/dist/adapters/sandbox/virtual/index.cjs +36 -9
  27. package/dist/adapters/sandbox/virtual/index.cjs.map +1 -1
  28. package/dist/adapters/sandbox/virtual/index.d.cts +8 -5
  29. package/dist/adapters/sandbox/virtual/index.d.ts +8 -5
  30. package/dist/adapters/sandbox/virtual/index.js +36 -9
  31. package/dist/adapters/sandbox/virtual/index.js.map +1 -1
  32. package/dist/adapters/sandbox/virtual/workflow.cjs +33 -0
  33. package/dist/adapters/sandbox/virtual/workflow.cjs.map +1 -0
  34. package/dist/adapters/sandbox/virtual/workflow.d.cts +27 -0
  35. package/dist/adapters/sandbox/virtual/workflow.d.ts +27 -0
  36. package/dist/adapters/sandbox/virtual/workflow.js +31 -0
  37. package/dist/adapters/sandbox/virtual/workflow.js.map +1 -0
  38. package/dist/adapters/thread/google-genai/index.cjs +9 -1
  39. package/dist/adapters/thread/google-genai/index.cjs.map +1 -1
  40. package/dist/adapters/thread/google-genai/index.d.cts +31 -19
  41. package/dist/adapters/thread/google-genai/index.d.ts +31 -19
  42. package/dist/adapters/thread/google-genai/index.js +9 -1
  43. package/dist/adapters/thread/google-genai/index.js.map +1 -1
  44. package/dist/adapters/thread/google-genai/workflow.cjs +33 -0
  45. package/dist/adapters/thread/google-genai/workflow.cjs.map +1 -0
  46. package/dist/adapters/thread/google-genai/workflow.d.cts +32 -0
  47. package/dist/adapters/thread/google-genai/workflow.d.ts +32 -0
  48. package/dist/adapters/thread/google-genai/workflow.js +31 -0
  49. package/dist/adapters/thread/google-genai/workflow.js.map +1 -0
  50. package/dist/adapters/thread/langchain/index.cjs +9 -1
  51. package/dist/adapters/thread/langchain/index.cjs.map +1 -1
  52. package/dist/adapters/thread/langchain/index.d.cts +27 -16
  53. package/dist/adapters/thread/langchain/index.d.ts +27 -16
  54. package/dist/adapters/thread/langchain/index.js +9 -1
  55. package/dist/adapters/thread/langchain/index.js.map +1 -1
  56. package/dist/adapters/thread/langchain/workflow.cjs +33 -0
  57. package/dist/adapters/thread/langchain/workflow.cjs.map +1 -0
  58. package/dist/adapters/thread/langchain/workflow.d.cts +32 -0
  59. package/dist/adapters/thread/langchain/workflow.d.ts +32 -0
  60. package/dist/adapters/thread/langchain/workflow.js +31 -0
  61. package/dist/adapters/thread/langchain/workflow.js.map +1 -0
  62. package/dist/index.cjs +282 -90
  63. package/dist/index.cjs.map +1 -1
  64. package/dist/index.d.cts +38 -16
  65. package/dist/index.d.ts +38 -16
  66. package/dist/index.js +281 -87
  67. package/dist/index.js.map +1 -1
  68. package/dist/queries-DModcWRy.d.cts +44 -0
  69. package/dist/queries-byD0jr1Y.d.ts +44 -0
  70. package/dist/{types-BkAYmc96.d.ts → types-B50pBPEV.d.ts} +190 -38
  71. package/dist/{types-YbL7JpEA.d.cts → types-Bll19FZJ.d.cts} +7 -0
  72. package/dist/{types-YbL7JpEA.d.ts → types-Bll19FZJ.d.ts} +7 -0
  73. package/dist/{queries-6Avfh74U.d.ts → types-BuXdFhaZ.d.cts} +7 -48
  74. package/dist/{types-BMRzfELQ.d.cts → types-ChAMwU3q.d.cts} +17 -1
  75. package/dist/{types-BMRzfELQ.d.ts → types-ChAMwU3q.d.ts} +17 -1
  76. package/dist/{types-CES_30qx.d.cts → types-DQW8l7pY.d.cts} +190 -38
  77. package/dist/{queries-CHa2iv_I.d.cts → types-GZ76HZSj.d.ts} +7 -48
  78. package/dist/workflow.cjs +244 -86
  79. package/dist/workflow.cjs.map +1 -1
  80. package/dist/workflow.d.cts +54 -65
  81. package/dist/workflow.d.ts +54 -65
  82. package/dist/workflow.js +243 -83
  83. package/dist/workflow.js.map +1 -1
  84. package/package.json +54 -2
  85. package/src/adapters/sandbox/daytona/filesystem.ts +1 -1
  86. package/src/adapters/sandbox/daytona/index.ts +8 -0
  87. package/src/adapters/sandbox/daytona/proxy.ts +56 -0
  88. package/src/adapters/sandbox/e2b/filesystem.ts +147 -0
  89. package/src/adapters/sandbox/e2b/index.ts +164 -0
  90. package/src/adapters/sandbox/e2b/types.ts +23 -0
  91. package/src/adapters/sandbox/inmemory/index.ts +27 -3
  92. package/src/adapters/sandbox/inmemory/proxy.ts +53 -0
  93. package/src/adapters/sandbox/virtual/filesystem.ts +41 -17
  94. package/src/adapters/sandbox/virtual/provider.ts +9 -1
  95. package/src/adapters/sandbox/virtual/proxy.ts +53 -0
  96. package/src/adapters/sandbox/virtual/types.ts +9 -4
  97. package/src/adapters/thread/google-genai/activities.ts +51 -17
  98. package/src/adapters/thread/google-genai/index.ts +1 -0
  99. package/src/adapters/thread/google-genai/proxy.ts +61 -0
  100. package/src/adapters/thread/langchain/activities.ts +47 -14
  101. package/src/adapters/thread/langchain/index.ts +1 -0
  102. package/src/adapters/thread/langchain/proxy.ts +61 -0
  103. package/src/lib/lifecycle.ts +57 -0
  104. package/src/lib/sandbox/manager.ts +52 -6
  105. package/src/lib/sandbox/sandbox.test.ts +12 -11
  106. package/src/lib/sandbox/types.ts +31 -4
  107. package/src/lib/session/index.ts +4 -5
  108. package/src/lib/session/session-edge-cases.integration.test.ts +491 -66
  109. package/src/lib/session/session.integration.test.ts +92 -80
  110. package/src/lib/session/session.ts +108 -96
  111. package/src/lib/session/types.ts +87 -17
  112. package/src/lib/subagent/define.ts +6 -5
  113. package/src/lib/subagent/handler.ts +148 -16
  114. package/src/lib/subagent/index.ts +4 -0
  115. package/src/lib/subagent/register.ts +10 -3
  116. package/src/lib/subagent/signals.ts +8 -0
  117. package/src/lib/subagent/subagent.integration.test.ts +893 -128
  118. package/src/lib/subagent/tool.ts +2 -2
  119. package/src/lib/subagent/types.ts +84 -21
  120. package/src/lib/subagent/workflow.ts +83 -12
  121. package/src/lib/tool-router/router-edge-cases.integration.test.ts +4 -1
  122. package/src/lib/tool-router/router.integration.test.ts +141 -5
  123. package/src/lib/tool-router/router.ts +13 -3
  124. package/src/lib/tool-router/types.ts +7 -0
  125. package/src/lib/workflow.test.ts +104 -27
  126. package/src/lib/workflow.ts +37 -19
  127. package/src/tools/bash/bash.test.ts +16 -7
  128. package/src/workflow.ts +11 -14
  129. package/tsup.config.ts +6 -0
@@ -0,0 +1,44 @@
1
+ import { F as FileEntry } from './types-BuXdFhaZ.cjs';
2
+
3
+ /**
4
+ * Structural constraint: accepts any `AgentStateManager<T>` whose custom
5
+ * state includes `fileTree: FileEntry<TMeta>[]`.
6
+ */
7
+ interface FileTreeAccessor<TMeta> {
8
+ get(key: "fileTree"): FileEntry<TMeta>[];
9
+ }
10
+ /**
11
+ * Check whether any file in the tree has a `metadata.mimeType` that matches
12
+ * the given pattern.
13
+ *
14
+ * Patterns:
15
+ * - Exact: `"application/pdf"`
16
+ * - Wildcard type: `"image/*"`
17
+ *
18
+ * Useful for conditionally enabling tools:
19
+ *
20
+ * ```ts
21
+ * { enabled: hasFileWithMimeType(stateManager, "image/*") }
22
+ * { enabled: hasFileWithMimeType(stateManager, ["image/*", "application/pdf"]) }
23
+ * ```
24
+ */
25
+ declare function hasFileWithMimeType<TMeta>(stateManager: FileTreeAccessor<TMeta>, pattern: string | string[]): boolean;
26
+ /**
27
+ * Return all entries whose `metadata.mimeType` matches the given pattern.
28
+ */
29
+ declare function filesWithMimeType<TMeta>(stateManager: FileTreeAccessor<TMeta>, pattern: string): FileEntry<TMeta>[];
30
+ /**
31
+ * Check whether the tree contains a directory whose name matches the given
32
+ * pattern. Directories are inferred from file paths.
33
+ *
34
+ * Patterns:
35
+ * - Exact: `"src"`
36
+ * - Glob with `*` wildcard: `"test*"`, `"*.generated"`
37
+ *
38
+ * ```ts
39
+ * { enabled: hasDirectory(stateManager, "test*") }
40
+ * ```
41
+ */
42
+ declare function hasDirectory<TMeta>(stateManager: FileTreeAccessor<TMeta>, pattern: string): boolean;
43
+
44
+ export { type FileTreeAccessor as F, hasFileWithMimeType as a, filesWithMimeType as f, hasDirectory as h };
@@ -0,0 +1,44 @@
1
+ import { F as FileEntry } from './types-GZ76HZSj.js';
2
+
3
+ /**
4
+ * Structural constraint: accepts any `AgentStateManager<T>` whose custom
5
+ * state includes `fileTree: FileEntry<TMeta>[]`.
6
+ */
7
+ interface FileTreeAccessor<TMeta> {
8
+ get(key: "fileTree"): FileEntry<TMeta>[];
9
+ }
10
+ /**
11
+ * Check whether any file in the tree has a `metadata.mimeType` that matches
12
+ * the given pattern.
13
+ *
14
+ * Patterns:
15
+ * - Exact: `"application/pdf"`
16
+ * - Wildcard type: `"image/*"`
17
+ *
18
+ * Useful for conditionally enabling tools:
19
+ *
20
+ * ```ts
21
+ * { enabled: hasFileWithMimeType(stateManager, "image/*") }
22
+ * { enabled: hasFileWithMimeType(stateManager, ["image/*", "application/pdf"]) }
23
+ * ```
24
+ */
25
+ declare function hasFileWithMimeType<TMeta>(stateManager: FileTreeAccessor<TMeta>, pattern: string | string[]): boolean;
26
+ /**
27
+ * Return all entries whose `metadata.mimeType` matches the given pattern.
28
+ */
29
+ declare function filesWithMimeType<TMeta>(stateManager: FileTreeAccessor<TMeta>, pattern: string): FileEntry<TMeta>[];
30
+ /**
31
+ * Check whether the tree contains a directory whose name matches the given
32
+ * pattern. Directories are inferred from file paths.
33
+ *
34
+ * Patterns:
35
+ * - Exact: `"src"`
36
+ * - Glob with `*` wildcard: `"test*"`, `"*.generated"`
37
+ *
38
+ * ```ts
39
+ * { enabled: hasDirectory(stateManager, "test*") }
40
+ * ```
41
+ */
42
+ declare function hasDirectory<TMeta>(stateManager: FileTreeAccessor<TMeta>, pattern: string): boolean;
43
+
44
+ export { type FileTreeAccessor as F, hasFileWithMimeType as a, filesWithMimeType as f, hasDirectory as h };
@@ -1,7 +1,7 @@
1
1
  import { Duration } from '@temporalio/common';
2
- import { a as ToolMap, b as ToolRouterHooks, M as MessageContent, S as SessionExitReason, c as ToolHandlerResponse, P as PreToolUseHookResult, d as PostToolUseFailureHookResult, e as RawToolCall, f as TokenUsage, B as BaseAgentState, g as RunAgentConfig, h as AgentStatus, W as WorkflowTask, i as ToolDefinition, j as ToolResultConfig, k as ToolCallResultUnion, I as InferToolResults } from './types-YbL7JpEA.js';
2
+ import { a as ToolMap, b as ToolRouterHooks, M as MessageContent, S as SessionExitReason, c as ToolHandlerResponse, P as PreToolUseHookResult, d as PostToolUseFailureHookResult, e as RawToolCall, f as TokenUsage, B as BaseAgentState, g as RunAgentConfig, h as AgentStatus, W as WorkflowTask, i as ToolDefinition, j as ToolResultConfig, k as ToolCallResultUnion, I as InferToolResults } from './types-Bll19FZJ.js';
3
3
  import { z } from 'zod';
4
- import { g as SandboxOps } from './types-BMRzfELQ.js';
4
+ import { g as SandboxOps } from './types-ChAMwU3q.js';
5
5
  import { QueryDefinition, ActivityInterfaceFor } from '@temporalio/workflow';
6
6
  import { UpdateDefinition } from '@temporalio/common/lib/interfaces';
7
7
 
@@ -69,19 +69,80 @@ interface Hooks<T extends ToolMap, TResult = unknown> extends ToolRouterHooks<T,
69
69
  onSessionEnd?: SessionEndHook;
70
70
  }
71
71
 
72
+ /**
73
+ * Thread initialization strategy.
74
+ *
75
+ * - `"new"` — start a fresh thread (optionally specify its ID).
76
+ * - `"continue"` — append directly to an existing thread in-place.
77
+ * - `"fork"` — copy all messages from an existing thread into a new one and
78
+ * continue there.
79
+ */
80
+ type ThreadInit = {
81
+ mode: "new";
82
+ threadId?: string;
83
+ } | {
84
+ mode: "continue";
85
+ threadId: string;
86
+ } | {
87
+ mode: "fork";
88
+ threadId: string;
89
+ };
90
+ /**
91
+ * Sandbox initialization strategy.
92
+ *
93
+ * - `"new"` — create a fresh sandbox.
94
+ * - `"continue"` — resume a previously-paused sandbox (this session takes
95
+ * ownership and the shutdown policy applies on exit).
96
+ * - `"fork"` — fork from an existing (or paused) sandbox; a new sandbox is
97
+ * created and owned by this session.
98
+ * - `"inherit"` — use a sandbox owned by someone else (e.g. a parent agent).
99
+ * The session will **not** manage its lifecycle on exit.
100
+ */
101
+ type SandboxInit = {
102
+ mode: "new";
103
+ } | {
104
+ mode: "continue";
105
+ sandboxId: string;
106
+ } | {
107
+ mode: "fork";
108
+ sandboxId: string;
109
+ } | {
110
+ mode: "inherit";
111
+ sandboxId: string;
112
+ };
113
+ /**
114
+ * What to do with the sandbox when the session exits.
115
+ *
116
+ * - `"destroy"` — tear down the sandbox entirely.
117
+ * - `"pause"` — pause the sandbox so it can be resumed later.
118
+ * - `"keep"` — leave the sandbox running (no-op on exit).
119
+ */
120
+ type SandboxShutdown = "destroy" | "pause" | "keep";
121
+ /**
122
+ * Extended shutdown options available to subagent workflows.
123
+ *
124
+ * Includes all base {@link SandboxShutdown} values plus:
125
+ * - `"pause-until-parent-close"` — pause the sandbox on exit, then wait for
126
+ * the parent workflow to signal when to destroy it.
127
+ */
128
+ type SubagentSandboxShutdown = SandboxShutdown | "pause-until-parent-close";
129
+
72
130
  /** ToolHandlerResponse with threadId required (subagents must always surface their thread) */
73
131
  type SubagentHandlerResponse<TResult = null> = ToolHandlerResponse<TResult> & {
74
132
  threadId: string;
133
+ sandboxId?: string;
75
134
  };
76
135
  /**
77
136
  * Raw workflow input fields passed from parent to child workflow.
78
137
  * `defineSubagentWorkflow` maps this into `SubagentSessionInput`.
79
138
  */
80
139
  interface SubagentWorkflowInput {
81
- /** Thread ID from parent for continuation */
82
- previousThreadId?: string;
83
- /** Sandbox ID inherited from parent */
84
- sandboxId?: string;
140
+ /** Thread initialization strategy forwarded from the parent */
141
+ thread?: ThreadInit;
142
+ /** Sandbox initialization strategy forwarded from the parent */
143
+ sandbox?: SandboxInit;
144
+ /** Sandbox shutdown override from the parent (takes precedence over workflow default) */
145
+ sandboxShutdown?: SubagentSandboxShutdown;
85
146
  }
86
147
  type SubagentWorkflow<TResult extends z.ZodType = z.ZodType> = (prompt: string, workflowInput: SubagentWorkflowInput, context?: Record<string, unknown>) => Promise<SubagentHandlerResponse<z.infer<TResult> | null>>;
87
148
  /**
@@ -93,6 +154,23 @@ type SubagentDefinition<TResult extends z.ZodType = z.ZodType, TContext extends
93
154
  readonly description: string;
94
155
  readonly resultSchema?: TResult;
95
156
  };
157
+ /** Context value or factory — resolved at invocation time when a function is provided */
158
+ type SubagentContext = Record<string, unknown> | (() => Record<string, unknown>);
159
+ /**
160
+ * Sandbox configuration for a subagent.
161
+ *
162
+ * String shorthands:
163
+ * - `"none"` — no sandbox (default).
164
+ * - `"inherit"` — reuse the parent's sandbox (shared filesystem/exec).
165
+ * - `"own"` — the child creates and owns its own sandbox (shutdown defaults to `"destroy"`).
166
+ *
167
+ * Object form (only for `source: "own"`):
168
+ * - `{ source: "own", shutdown?: SubagentSandboxShutdown }` — own sandbox with explicit shutdown policy.
169
+ */
170
+ type SubagentSandboxConfig = "none" | "inherit" | "own" | {
171
+ source: "own";
172
+ shutdown?: SubagentSandboxShutdown;
173
+ };
96
174
  /**
97
175
  * Configuration for a subagent that can be spawned by the parent workflow.
98
176
  *
@@ -106,23 +184,34 @@ interface SubagentConfig<TResult extends z.ZodType = z.ZodType> {
106
184
  /** Whether this subagent is available (default: true). Disabled subagents are excluded from the Subagent tool. */
107
185
  enabled?: boolean | (() => boolean);
108
186
  /** Temporal workflow function or type name (used with executeChild) */
109
- workflow: string | SubagentWorkflow<TResult>;
187
+ workflow: SubagentWorkflow<TResult>;
110
188
  /** Optional task queue - defaults to parent's queue if not specified */
111
189
  taskQueue?: string;
112
190
  /** Optional Zod schema to validate the child workflow's result. If omitted, result is passed through as-is. */
113
191
  resultSchema?: TResult;
114
- /** Optional static context passed to the subagent on every invocation */
115
- context?: Record<string, unknown>;
116
- /** Allow the parent agent to pass a threadId for this subagent to continue (default: false) */
117
- allowThreadContinuation?: boolean;
192
+ /** Optional context passed to the subagent a static object or a function evaluated at invocation time */
193
+ context?: SubagentContext;
118
194
  /** Per-subagent lifecycle hooks */
119
195
  hooks?: SubagentHooks;
196
+ /**
197
+ * Thread mode for this subagent.
198
+ *
199
+ * - `"new"` (default) — always start a fresh thread.
200
+ * - `"fork"` — the parent can pass a `threadId`; messages are copied into
201
+ * a new thread and the subagent continues there.
202
+ * - `"continue"` — the parent can pass a `threadId`; the subagent appends
203
+ * directly to the existing thread in-place.
204
+ */
205
+ thread?: "new" | "fork" | "continue";
120
206
  /**
121
207
  * Sandbox strategy for this subagent.
122
- * - `'inherit'` (default): reuse the parent's sandbox (shared filesystem/exec).
123
- * - `'own'`: the child creates and owns its own sandbox.
208
+ *
209
+ * String shorthands: `"none"` (default) | `"inherit"` | `"own"`.
210
+ * Object form: `{ source: "own", shutdown?: SubagentSandboxShutdown }`.
211
+ *
212
+ * @see {@link SubagentSandboxConfig}
124
213
  */
125
- sandbox?: "inherit" | "own";
214
+ sandbox?: SubagentSandboxConfig;
126
215
  }
127
216
  /**
128
217
  * Per-subagent lifecycle hooks - defined on a SubagentConfig.
@@ -142,6 +231,8 @@ interface SubagentHooks<TArgs = unknown, TResult = unknown> {
142
231
  threadId: string;
143
232
  turn: number;
144
233
  durationMs: number;
234
+ /** Unvalidated metadata from the child workflow (e.g. infrastructure state) */
235
+ metadata?: Record<string, unknown>;
145
236
  }) => void | Promise<void>;
146
237
  /** Called when this subagent execution fails */
147
238
  onExecutionFailure?: (ctx: {
@@ -151,18 +242,31 @@ interface SubagentHooks<TArgs = unknown, TResult = unknown> {
151
242
  turn: number;
152
243
  }) => PostToolUseFailureHookResult | Promise<PostToolUseFailureHookResult>;
153
244
  }
245
+ /**
246
+ * Extended response from the subagent `fn` — includes optional cleanup callbacks
247
+ * stripped before signaling the parent.
248
+ *
249
+ * When `TSandboxShutdown` is `"pause-until-parent-close"`, both `destroySandbox`
250
+ * and `sandboxId` become required so the parent can coordinate cleanup.
251
+ */
252
+ type SubagentFnResult<TResult = null, TSandboxShutdown extends SubagentSandboxShutdown = SubagentSandboxShutdown> = SubagentHandlerResponse<TResult> & (TSandboxShutdown extends "pause-until-parent-close" ? {
253
+ destroySandbox: () => Promise<void>;
254
+ sandboxId: string;
255
+ } : {
256
+ destroySandbox?: () => Promise<void>;
257
+ });
154
258
  /**
155
259
  * Session config fields passed from parent to child workflow.
156
260
  */
157
261
  interface SubagentSessionInput {
158
262
  /** Agent name — spread directly into `createSession` */
159
263
  agentName: string;
160
- /** Thread ID to continue from */
161
- threadId?: string;
162
- /** Whether to continue an existing thread */
163
- continueThread?: boolean;
164
- /** Sandbox ID inherited from the parent agent */
165
- sandboxId?: string;
264
+ /** Thread initialization strategy */
265
+ thread?: ThreadInit;
266
+ /** Sandbox initialization strategy */
267
+ sandbox?: SandboxInit;
268
+ /** Sandbox shutdown policy (default: "destroy") */
269
+ sandboxShutdown?: SubagentSandboxShutdown;
166
270
  }
167
271
 
168
272
  /**
@@ -350,14 +454,38 @@ interface ThreadOps {
350
454
  /** Copy all messages from sourceThreadId into a new thread at targetThreadId */
351
455
  forkThread(sourceThreadId: string, targetThreadId: string): Promise<void>;
352
456
  }
457
+ /**
458
+ * Composes an adapter prefix + workflow scope for activity naming.
459
+ *
460
+ * The adapter prefix stays first (camelCase); the workflow scope is
461
+ * capitalised and appended. When `TScope` is empty the adapter prefix
462
+ * is used as-is.
463
+ *
464
+ * @example
465
+ * ```typescript
466
+ * ScopedPrefix<"codingAgent", "googleGenAI"> // "googleGenAICodingAgent"
467
+ * ScopedPrefix<"", "googleGenAI"> // "googleGenAI"
468
+ * ```
469
+ */
470
+ type ScopedPrefix<TScope extends string, TAdapter extends string> = TScope extends "" ? TAdapter : `${TAdapter}${Capitalize<TScope>}`;
471
+ /**
472
+ * Maps generic {@link ThreadOps} method names to adapter-prefixed names.
473
+ *
474
+ * @example
475
+ * ```typescript
476
+ * type GoogleOps = PrefixedThreadOps<"googleGenAI">;
477
+ * // → { googleGenAIInitializeThread, googleGenAIAppendHumanMessage, … }
478
+ * ```
479
+ */
480
+ type PrefixedThreadOps<TPrefix extends string> = {
481
+ [K in keyof ThreadOps as `${TPrefix}${Capitalize<K & string>}`]: ThreadOps[K];
482
+ };
353
483
  /**
354
484
  * Configuration for a Zeitlich agent session
355
485
  */
356
486
  interface SessionConfig<T extends ToolMap, M = unknown> {
357
487
  /** The name of the agent, should be unique within the workflows */
358
488
  agentName: string;
359
- /** The thread ID to use for the session (defaults to a short generated ID) */
360
- threadId?: string;
361
489
  /** Metadata for the session */
362
490
  metadata?: Record<string, unknown>;
363
491
  /** Whether to append the system prompt as message to the thread */
@@ -367,7 +495,7 @@ interface SessionConfig<T extends ToolMap, M = unknown> {
367
495
  /** Workflow-specific runAgent activity (with tools pre-bound) */
368
496
  runAgent: RunAgentActivity<M>;
369
497
  /** Thread operations (initialize, append messages, parse tool calls) */
370
- threadOps?: ActivityInterfaceFor<ThreadOps>;
498
+ threadOps: ActivityInterfaceFor<ThreadOps>;
371
499
  /** Tool router for processing tool calls (optional if agent has no tools) */
372
500
  tools?: T;
373
501
  /** Subagent configurations */
@@ -383,28 +511,52 @@ interface SessionConfig<T extends ToolMap, M = unknown> {
383
511
  * Returns MessageContent array for the initial HumanMessage.
384
512
  */
385
513
  buildContextMessage: () => MessageContent | Promise<MessageContent>;
386
- /** When true, skip thread initialization and system prompt — append only the new human message to the existing thread. */
387
- continueThread?: boolean;
388
514
  /** How long to wait for input before cancelling the workflow */
389
515
  waitForInputTimeout?: Duration;
516
+ /**
517
+ * Thread initialization strategy (default: `{ mode: "new" }`).
518
+ *
519
+ * - `{ mode: "new" }` — start a fresh thread.
520
+ * - `{ mode: "new", threadId: "..." }` — start a fresh thread with a specific ID.
521
+ * - `{ mode: "continue", threadId: "..." }` — append to an existing thread in-place.
522
+ * - `{ mode: "fork", threadId: "..." }` — fork an existing thread and continue in the copy.
523
+ */
524
+ thread?: ThreadInit;
390
525
  /** Sandbox lifecycle operations (optional — omit for agents that don't need a sandbox) */
391
- sandbox?: SandboxOps;
526
+ sandboxOps?: SandboxOps;
392
527
  /**
393
- * Pre-existing sandbox ID to reuse (e.g. inherited from a parent agent).
394
- * When set, the session skips `createSandbox` and will not destroy the
395
- * sandbox on exit (the owner is responsible for cleanup).
528
+ * Sandbox initialization strategy.
529
+ *
530
+ * - `{ mode: "new" }` create a fresh sandbox.
531
+ * - `{ mode: "continue", sandboxId: "..." }` — resume a paused sandbox (session owns it).
532
+ * - `{ mode: "fork", sandboxId: "..." }` — fork from an existing sandbox.
533
+ * - `{ mode: "inherit", sandboxId: "..." }` — use a parent's sandbox without ownership.
534
+ *
535
+ * When omitted and `sandboxOps` is provided, defaults to `{ mode: "new" }`.
396
536
  */
397
- sandboxId?: string;
537
+ sandbox?: SandboxInit;
538
+ /**
539
+ * What to do with the sandbox when this session exits.
540
+ *
541
+ * Defaults to `"destroy"` when omitted.
542
+ * Has no effect when the sandbox is inherited (`sandbox.mode === "inherit"`).
543
+ */
544
+ sandboxShutdown?: SubagentSandboxShutdown;
398
545
  }
399
- interface ZeitlichSession<M = unknown> {
546
+ type SessionResult<M, TState extends JsonSerializable<TState>, HasSandbox extends boolean = boolean> = {
547
+ threadId: string;
548
+ finalMessage: M | null;
549
+ exitReason: SessionExitReason;
550
+ usage: ReturnType<AgentStateManager<TState>["getTotalUsage"]>;
551
+ } & (HasSandbox extends true ? {
552
+ sandboxId: string;
553
+ } : {
554
+ sandboxId?: undefined;
555
+ });
556
+ interface ZeitlichSession<M = unknown, HasSandbox extends boolean = boolean> {
400
557
  runSession<T extends JsonSerializable<T>>(args: {
401
558
  stateManager: AgentStateManager<T>;
402
- }): Promise<{
403
- threadId: string;
404
- finalMessage: M | null;
405
- exitReason: SessionExitReason;
406
- usage: ReturnType<AgentStateManager<T>["getTotalUsage"]>;
407
- }>;
559
+ }): Promise<SessionResult<M, T, HasSandbox>>;
408
560
  }
409
561
 
410
- export type { AgentResponse as A, Hooks as H, JsonPrimitive as J, ModelInvoker as M, PostHumanMessageAppendHook as P, RunAgentActivity as R, SkillProvider as S, ThreadOps as T, ZeitlichSession as Z, ModelInvokerConfig as a, SkillMetadata as b, Skill as c, AgentState as d, AgentStateManager as e, JsonSerializable as f, JsonValue as g, PostHumanMessageAppendHookContext as h, PreHumanMessageAppendHook as i, PreHumanMessageAppendHookContext as j, SessionConfig as k, SessionEndHook as l, SessionEndHookContext as m, SessionStartHook as n, SessionStartHookContext as o, SubagentConfig as p, SubagentDefinition as q, SubagentHandlerResponse as r, SubagentHooks as s, SubagentSessionInput as t, SubagentWorkflow as u, SubagentWorkflowInput as v };
562
+ export type { AgentResponse as A, SubagentSandboxShutdown as B, SubagentSessionInput as C, SubagentWorkflow as D, SubagentWorkflowInput as E, ThreadInit as F, Hooks as H, JsonPrimitive as J, ModelInvoker as M, PrefixedThreadOps as P, RunAgentActivity as R, ScopedPrefix as S, ThreadOps as T, ZeitlichSession as Z, ModelInvokerConfig as a, SkillProvider as b, SkillMetadata as c, Skill as d, AgentState as e, AgentStateManager as f, JsonSerializable as g, JsonValue as h, PostHumanMessageAppendHook as i, PostHumanMessageAppendHookContext as j, PreHumanMessageAppendHook as k, PreHumanMessageAppendHookContext as l, SandboxInit as m, SandboxShutdown as n, SessionConfig as o, SessionEndHook as p, SessionEndHookContext as q, SessionResult as r, SessionStartHook as s, SessionStartHookContext as t, SubagentConfig as u, SubagentDefinition as v, SubagentFnResult as w, SubagentHandlerResponse as x, SubagentHooks as y, SubagentSandboxConfig as z };
@@ -224,6 +224,10 @@ interface ToolHandlerResponse<TResult = null> {
224
224
  usage?: TokenUsage;
225
225
  /** Thread ID used by the handler (surfaced to the LLM for subagent thread continuation) */
226
226
  threadId?: string;
227
+ /** Sandbox ID created or used by the handler (e.g. child agent sandbox) */
228
+ sandboxId?: string;
229
+ /** Unvalidated metadata passthrough from handler to hooks (e.g. infrastructure state) */
230
+ metadata?: Record<string, unknown>;
227
231
  }
228
232
  /**
229
233
  * Base context the router always injects into every handler invocation.
@@ -278,6 +282,8 @@ interface ToolCallResult<TName extends string = string, TResult = unknown> {
278
282
  name: TName;
279
283
  data: TResult;
280
284
  usage?: TokenUsage;
285
+ /** Unvalidated metadata passthrough from handler to hooks (e.g. infrastructure state) */
286
+ metadata?: Record<string, unknown>;
281
287
  }
282
288
  /**
283
289
  * Infer result types from a tool map based on handler return types.
@@ -336,6 +342,7 @@ interface ToolHooks<TArgs = unknown, TResult = unknown> {
336
342
  threadId: string;
337
343
  turn: number;
338
344
  durationMs: number;
345
+ metadata?: Record<string, unknown>;
339
346
  }) => void | Promise<void>;
340
347
  /** Called when this tool execution fails */
341
348
  onPostToolUseFailure?: (ctx: {
@@ -224,6 +224,10 @@ interface ToolHandlerResponse<TResult = null> {
224
224
  usage?: TokenUsage;
225
225
  /** Thread ID used by the handler (surfaced to the LLM for subagent thread continuation) */
226
226
  threadId?: string;
227
+ /** Sandbox ID created or used by the handler (e.g. child agent sandbox) */
228
+ sandboxId?: string;
229
+ /** Unvalidated metadata passthrough from handler to hooks (e.g. infrastructure state) */
230
+ metadata?: Record<string, unknown>;
227
231
  }
228
232
  /**
229
233
  * Base context the router always injects into every handler invocation.
@@ -278,6 +282,8 @@ interface ToolCallResult<TName extends string = string, TResult = unknown> {
278
282
  name: TName;
279
283
  data: TResult;
280
284
  usage?: TokenUsage;
285
+ /** Unvalidated metadata passthrough from handler to hooks (e.g. infrastructure state) */
286
+ metadata?: Record<string, unknown>;
281
287
  }
282
288
  /**
283
289
  * Infer result types from a tool map based on handler return types.
@@ -336,6 +342,7 @@ interface ToolHooks<TArgs = unknown, TResult = unknown> {
336
342
  threadId: string;
337
343
  turn: number;
338
344
  durationMs: number;
345
+ metadata?: Record<string, unknown>;
339
346
  }) => void | Promise<void>;
340
347
  /** Called when this tool execution fails */
341
348
  onPostToolUseFailure?: (ctx: {
@@ -1,5 +1,5 @@
1
- import { a as SandboxFileSystem, F as FileStat, D as DirentEntry, S as Sandbox, d as SandboxCreateOptions } from './types-BMRzfELQ.js';
2
- import { R as RouterContext } from './types-YbL7JpEA.js';
1
+ import { a as SandboxFileSystem, F as FileStat, D as DirentEntry, S as Sandbox, d as SandboxCreateOptions } from './types-ChAMwU3q.cjs';
2
+ import { R as RouterContext } from './types-Bll19FZJ.cjs';
3
3
 
4
4
  /**
5
5
  * Ephemeral {@link SandboxFileSystem} backed by a {@link FileResolver}.
@@ -82,11 +82,11 @@ type TreeMutation<TMeta = FileEntryMetadata> = {
82
82
  */
83
83
  interface FileResolver<TCtx = unknown, TMeta = FileEntryMetadata> {
84
84
  resolveEntries(ctx: TCtx): Promise<FileEntry<TMeta>[]>;
85
- readFile(id: string, ctx: TCtx): Promise<string>;
86
- readFileBuffer(id: string, ctx: TCtx): Promise<Uint8Array>;
87
- writeFile(id: string, content: string | Uint8Array, ctx: TCtx): Promise<void>;
85
+ readFile(id: string, ctx: TCtx, metadata: TMeta): Promise<string>;
86
+ readFileBuffer(id: string, ctx: TCtx, metadata: TMeta): Promise<Uint8Array>;
87
+ writeFile(id: string, content: string | Uint8Array, ctx: TCtx, metadata: TMeta): Promise<void>;
88
88
  createFile(path: string, content: string | Uint8Array, ctx: TCtx): Promise<FileEntry<TMeta>>;
89
- deleteFile(id: string, ctx: TCtx): Promise<void>;
89
+ deleteFile(id: string, ctx: TCtx, metadata: TMeta): Promise<void>;
90
90
  }
91
91
  /**
92
92
  * Options for {@link VirtualSandboxProvider.create}.
@@ -122,45 +122,4 @@ interface VirtualSandboxContext<TCtx = unknown, TMeta = FileEntryMetadata> exten
122
122
  sandbox: VirtualSandbox<TCtx, TMeta>;
123
123
  }
124
124
 
125
- /**
126
- * Structural constraint: accepts any `AgentStateManager<T>` whose custom
127
- * state includes `fileTree: FileEntry<TMeta>[]`.
128
- */
129
- interface FileTreeAccessor<TMeta> {
130
- get(key: "fileTree"): FileEntry<TMeta>[];
131
- }
132
- /**
133
- * Check whether any file in the tree has a `metadata.mimeType` that matches
134
- * the given pattern.
135
- *
136
- * Patterns:
137
- * - Exact: `"application/pdf"`
138
- * - Wildcard type: `"image/*"`
139
- *
140
- * Useful for conditionally enabling tools:
141
- *
142
- * ```ts
143
- * { enabled: hasFileWithMimeType(stateManager, "image/*") }
144
- * { enabled: hasFileWithMimeType(stateManager, ["image/*", "application/pdf"]) }
145
- * ```
146
- */
147
- declare function hasFileWithMimeType<TMeta>(stateManager: FileTreeAccessor<TMeta>, pattern: string | string[]): boolean;
148
- /**
149
- * Return all entries whose `metadata.mimeType` matches the given pattern.
150
- */
151
- declare function filesWithMimeType<TMeta>(stateManager: FileTreeAccessor<TMeta>, pattern: string): FileEntry<TMeta>[];
152
- /**
153
- * Check whether the tree contains a directory whose name matches the given
154
- * pattern. Directories are inferred from file paths.
155
- *
156
- * Patterns:
157
- * - Exact: `"src"`
158
- * - Glob with `*` wildcard: `"test*"`, `"*.generated"`
159
- *
160
- * ```ts
161
- * { enabled: hasDirectory(stateManager, "test*") }
162
- * ```
163
- */
164
- declare function hasDirectory<TMeta>(stateManager: FileTreeAccessor<TMeta>, pattern: string): boolean;
165
-
166
- export { type FileEntryMetadata as F, type TreeMutation as T, type VirtualSandboxCreateOptions as V, type FileResolver as a, type VirtualSandboxContext as b, type FileEntry as c, type VirtualSandbox as d, type FileTreeAccessor as e, type VirtualFileTree as f, VirtualSandboxFileSystem as g, type VirtualSandboxState as h, filesWithMimeType as i, hasDirectory as j, hasFileWithMimeType as k };
125
+ export { type FileEntry as F, type TreeMutation as T, type VirtualSandboxCreateOptions as V, type FileEntryMetadata as a, type FileResolver as b, type VirtualSandboxContext as c, type VirtualSandbox as d, type VirtualFileTree as e, VirtualSandboxFileSystem as f, type VirtualSandboxState as g };
@@ -95,8 +95,10 @@ interface SandboxProvider<TOptions extends SandboxCreateOptions = SandboxCreateO
95
95
  create(options?: TOptions): Promise<SandboxCreateResult>;
96
96
  get(sandboxId: string): Promise<TSandbox>;
97
97
  destroy(sandboxId: string): Promise<void>;
98
+ pause(sandboxId: string, ttlSeconds?: number): Promise<void>;
98
99
  snapshot(sandboxId: string): Promise<SandboxSnapshot>;
99
100
  restore(snapshot: SandboxSnapshot): Promise<Sandbox>;
101
+ fork(sandboxId: string): Promise<Sandbox>;
100
102
  }
101
103
  interface SandboxOps<TOptions extends SandboxCreateOptions = SandboxCreateOptions> {
102
104
  createSandbox(options?: TOptions): Promise<{
@@ -104,8 +106,22 @@ interface SandboxOps<TOptions extends SandboxCreateOptions = SandboxCreateOption
104
106
  stateUpdate?: Record<string, unknown>;
105
107
  }>;
106
108
  destroySandbox(sandboxId: string): Promise<void>;
109
+ pauseSandbox(sandboxId: string): Promise<void>;
107
110
  snapshotSandbox(sandboxId: string): Promise<SandboxSnapshot>;
111
+ forkSandbox(sandboxId: string): Promise<string>;
108
112
  }
113
+ /**
114
+ * Maps generic {@link SandboxOps} method names to adapter-prefixed names.
115
+ *
116
+ * @example
117
+ * ```typescript
118
+ * type InMemOps = PrefixedSandboxOps<"inMemory">;
119
+ * // → { inMemoryCreateSandbox, inMemoryDestroySandbox, inMemorySnapshotSandbox }
120
+ * ```
121
+ */
122
+ type PrefixedSandboxOps<TPrefix extends string, TOptions extends SandboxCreateOptions = SandboxCreateOptions> = {
123
+ [K in keyof SandboxOps<TOptions> as `${TPrefix}${Capitalize<K & string>}`]: SandboxOps<TOptions>[K];
124
+ };
109
125
 
110
126
  declare class SandboxNotSupportedError extends ApplicationFailure {
111
127
  constructor(operation: string);
@@ -114,4 +130,4 @@ declare class SandboxNotFoundError extends ApplicationFailure {
114
130
  constructor(sandboxId: string);
115
131
  }
116
132
 
117
- export { type DirentEntry as D, type ExecResult as E, type FileStat as F, type Sandbox as S, type SandboxFileSystem as a, type SandboxProvider as b, type SandboxCapabilities as c, type SandboxCreateOptions as d, type SandboxCreateResult as e, type SandboxSnapshot as f, type SandboxOps as g, type ExecOptions as h, SandboxNotFoundError as i, SandboxNotSupportedError as j };
133
+ export { type DirentEntry as D, type ExecResult as E, type FileStat as F, type PrefixedSandboxOps as P, type Sandbox as S, type SandboxFileSystem as a, type SandboxProvider as b, type SandboxCapabilities as c, type SandboxCreateOptions as d, type SandboxCreateResult as e, type SandboxSnapshot as f, type SandboxOps as g, type ExecOptions as h, SandboxNotFoundError as i, SandboxNotSupportedError as j };
@@ -95,8 +95,10 @@ interface SandboxProvider<TOptions extends SandboxCreateOptions = SandboxCreateO
95
95
  create(options?: TOptions): Promise<SandboxCreateResult>;
96
96
  get(sandboxId: string): Promise<TSandbox>;
97
97
  destroy(sandboxId: string): Promise<void>;
98
+ pause(sandboxId: string, ttlSeconds?: number): Promise<void>;
98
99
  snapshot(sandboxId: string): Promise<SandboxSnapshot>;
99
100
  restore(snapshot: SandboxSnapshot): Promise<Sandbox>;
101
+ fork(sandboxId: string): Promise<Sandbox>;
100
102
  }
101
103
  interface SandboxOps<TOptions extends SandboxCreateOptions = SandboxCreateOptions> {
102
104
  createSandbox(options?: TOptions): Promise<{
@@ -104,8 +106,22 @@ interface SandboxOps<TOptions extends SandboxCreateOptions = SandboxCreateOption
104
106
  stateUpdate?: Record<string, unknown>;
105
107
  }>;
106
108
  destroySandbox(sandboxId: string): Promise<void>;
109
+ pauseSandbox(sandboxId: string): Promise<void>;
107
110
  snapshotSandbox(sandboxId: string): Promise<SandboxSnapshot>;
111
+ forkSandbox(sandboxId: string): Promise<string>;
108
112
  }
113
+ /**
114
+ * Maps generic {@link SandboxOps} method names to adapter-prefixed names.
115
+ *
116
+ * @example
117
+ * ```typescript
118
+ * type InMemOps = PrefixedSandboxOps<"inMemory">;
119
+ * // → { inMemoryCreateSandbox, inMemoryDestroySandbox, inMemorySnapshotSandbox }
120
+ * ```
121
+ */
122
+ type PrefixedSandboxOps<TPrefix extends string, TOptions extends SandboxCreateOptions = SandboxCreateOptions> = {
123
+ [K in keyof SandboxOps<TOptions> as `${TPrefix}${Capitalize<K & string>}`]: SandboxOps<TOptions>[K];
124
+ };
109
125
 
110
126
  declare class SandboxNotSupportedError extends ApplicationFailure {
111
127
  constructor(operation: string);
@@ -114,4 +130,4 @@ declare class SandboxNotFoundError extends ApplicationFailure {
114
130
  constructor(sandboxId: string);
115
131
  }
116
132
 
117
- export { type DirentEntry as D, type ExecResult as E, type FileStat as F, type Sandbox as S, type SandboxFileSystem as a, type SandboxProvider as b, type SandboxCapabilities as c, type SandboxCreateOptions as d, type SandboxCreateResult as e, type SandboxSnapshot as f, type SandboxOps as g, type ExecOptions as h, SandboxNotFoundError as i, SandboxNotSupportedError as j };
133
+ export { type DirentEntry as D, type ExecResult as E, type FileStat as F, type PrefixedSandboxOps as P, type Sandbox as S, type SandboxFileSystem as a, type SandboxProvider as b, type SandboxCapabilities as c, type SandboxCreateOptions as d, type SandboxCreateResult as e, type SandboxSnapshot as f, type SandboxOps as g, type ExecOptions as h, SandboxNotFoundError as i, SandboxNotSupportedError as j };