zeitlich 0.2.49 → 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.
Files changed (127) hide show
  1. package/README.md +26 -23
  2. package/dist/adapters/sandbox/daytona/index.cjs.map +1 -1
  3. package/dist/adapters/sandbox/daytona/index.d.cts +3 -3
  4. package/dist/adapters/sandbox/daytona/index.d.ts +3 -3
  5. package/dist/adapters/sandbox/daytona/index.js.map +1 -1
  6. package/dist/adapters/sandbox/daytona/workflow.d.cts +2 -2
  7. package/dist/adapters/sandbox/daytona/workflow.d.ts +2 -2
  8. package/dist/adapters/sandbox/e2b/index.cjs.map +1 -1
  9. package/dist/adapters/sandbox/e2b/index.d.cts +1 -1
  10. package/dist/adapters/sandbox/e2b/index.d.ts +1 -1
  11. package/dist/adapters/sandbox/e2b/index.js.map +1 -1
  12. package/dist/adapters/sandbox/e2b/workflow.d.cts +1 -1
  13. package/dist/adapters/sandbox/e2b/workflow.d.ts +1 -1
  14. package/dist/adapters/thread/anthropic/index.cjs +60 -55
  15. package/dist/adapters/thread/anthropic/index.cjs.map +1 -1
  16. package/dist/adapters/thread/anthropic/index.d.cts +20 -15
  17. package/dist/adapters/thread/anthropic/index.d.ts +20 -15
  18. package/dist/adapters/thread/anthropic/index.js +60 -55
  19. package/dist/adapters/thread/anthropic/index.js.map +1 -1
  20. package/dist/adapters/thread/anthropic/workflow.d.cts +7 -7
  21. package/dist/adapters/thread/anthropic/workflow.d.ts +7 -7
  22. package/dist/adapters/thread/google-genai/index.cjs +135 -66
  23. package/dist/adapters/thread/google-genai/index.cjs.map +1 -1
  24. package/dist/adapters/thread/google-genai/index.d.cts +200 -26
  25. package/dist/adapters/thread/google-genai/index.d.ts +200 -26
  26. package/dist/adapters/thread/google-genai/index.js +135 -66
  27. package/dist/adapters/thread/google-genai/index.js.map +1 -1
  28. package/dist/adapters/thread/google-genai/workflow.d.cts +8 -8
  29. package/dist/adapters/thread/google-genai/workflow.d.ts +8 -8
  30. package/dist/adapters/thread/langchain/index.cjs +67 -55
  31. package/dist/adapters/thread/langchain/index.cjs.map +1 -1
  32. package/dist/adapters/thread/langchain/index.d.cts +20 -15
  33. package/dist/adapters/thread/langchain/index.d.ts +20 -15
  34. package/dist/adapters/thread/langchain/index.js +67 -55
  35. package/dist/adapters/thread/langchain/index.js.map +1 -1
  36. package/dist/adapters/thread/langchain/workflow.d.cts +7 -7
  37. package/dist/adapters/thread/langchain/workflow.d.ts +7 -7
  38. package/dist/{cold-store-DKMAO1Dd.d.ts → cold-store-DyHodfAB.d.ts} +1 -1
  39. package/dist/{cold-store-CkWoNtMh.d.cts → cold-store-YOx9nmgR.d.cts} +1 -1
  40. package/dist/index.cjs +15050 -420
  41. package/dist/index.cjs.map +1 -1
  42. package/dist/index.d.cts +79 -83
  43. package/dist/index.d.ts +79 -83
  44. package/dist/index.js +15051 -417
  45. package/dist/index.js.map +1 -1
  46. package/dist/{proxy-B7CWEV-T.d.cts → proxy-2htgGQrc.d.cts} +1 -1
  47. package/dist/{proxy-ByFHMVRX.d.ts → proxy-CmiTP4pp.d.ts} +1 -1
  48. package/dist/{thread-manager-nK-WcFzM.d.ts → thread-manager-BJ5pz5Cx.d.cts} +6 -7
  49. package/dist/{thread-manager-7AW4rhfu.d.ts → thread-manager-BQAbrYXH.d.cts} +6 -7
  50. package/dist/{thread-manager-Cibe0X5m.d.cts → thread-manager-CcvltOuq.d.ts} +6 -7
  51. package/dist/{thread-manager-B9rtMEVn.d.cts → thread-manager-DHAbncHX.d.ts} +6 -7
  52. package/dist/{types-gVa5XCWD.d.ts → types-BQvXWcft.d.ts} +1 -1
  53. package/dist/{types-XUUFvrJ9.d.cts → types-BjdqxKYp.d.cts} +709 -709
  54. package/dist/{types-CJ7tCdl6.d.ts → types-D8W5TnSa.d.cts} +3 -3
  55. package/dist/{types-CJ7tCdl6.d.cts → types-D8W5TnSa.d.ts} +3 -3
  56. package/dist/{types-DO4Tkwxo.d.ts → types-DEbkLA06.d.ts} +3 -3
  57. package/dist/{types-DeVNWqlb.d.ts → types-DiI7mZhI.d.ts} +709 -709
  58. package/dist/{types-BR-k7h0e.d.cts → types-N_LTWe4b.d.cts} +3 -3
  59. package/dist/{types-CjY93AWZ.d.cts → types-OEN1xrFg.d.cts} +1 -1
  60. package/dist/{workflow-uhOIj9D-.d.ts → workflow-CcgD6EUB.d.cts} +34 -3
  61. package/dist/{workflow-KbGsxpfh.d.cts → workflow-DBjPOKBr.d.ts} +34 -3
  62. package/dist/workflow.cjs +15008 -377
  63. package/dist/workflow.cjs.map +1 -1
  64. package/dist/workflow.d.cts +3 -3
  65. package/dist/workflow.d.ts +3 -3
  66. package/dist/workflow.js +15009 -374
  67. package/dist/workflow.js.map +1 -1
  68. package/package.json +10 -37
  69. package/src/adapters/thread/anthropic/activities.test.ts +115 -0
  70. package/src/adapters/thread/anthropic/activities.ts +11 -19
  71. package/src/adapters/thread/anthropic/fork-transform.test.ts +17 -11
  72. package/src/adapters/thread/anthropic/model-invoker.test.ts +54 -3
  73. package/src/adapters/thread/anthropic/model-invoker.ts +11 -1
  74. package/src/adapters/thread/anthropic/thread-manager.test.ts +2 -2
  75. package/src/adapters/thread/anthropic/thread-manager.ts +3 -4
  76. package/src/adapters/thread/google-genai/activities.test.ts +162 -0
  77. package/src/adapters/thread/google-genai/activities.ts +38 -15
  78. package/src/adapters/thread/google-genai/fork-transform.test.ts +17 -11
  79. package/src/adapters/thread/google-genai/model-invoker.test.ts +386 -0
  80. package/src/adapters/thread/google-genai/model-invoker.ts +118 -23
  81. package/src/adapters/thread/google-genai/thread-manager.test.ts +2 -2
  82. package/src/adapters/thread/google-genai/thread-manager.ts +3 -4
  83. package/src/adapters/thread/langchain/activities.test.ts +88 -0
  84. package/src/adapters/thread/langchain/activities.ts +15 -12
  85. package/src/adapters/thread/langchain/fork-transform.test.ts +17 -11
  86. package/src/adapters/thread/langchain/model-invoker.test.ts +74 -0
  87. package/src/adapters/thread/langchain/model-invoker.ts +16 -3
  88. package/src/adapters/thread/langchain/thread-manager.test.ts +2 -2
  89. package/src/adapters/thread/langchain/thread-manager.ts +3 -4
  90. package/src/index.ts +2 -2
  91. package/src/lib/sandbox/capability-types.test.ts +2 -2
  92. package/src/lib/sandbox/manager.ts +2 -6
  93. package/src/lib/sandbox/sandbox.test.ts +1 -1
  94. package/src/lib/sandbox/types.ts +2 -2
  95. package/src/lib/session/session.integration.test.ts +92 -0
  96. package/src/lib/session/session.ts +23 -11
  97. package/src/lib/thread/keys.test.ts +9 -9
  98. package/src/lib/thread/keys.ts +1 -1
  99. package/src/lib/thread/manager.test.ts +24 -14
  100. package/src/lib/thread/manager.ts +19 -23
  101. package/src/lib/thread/snapshot.test.ts +51 -43
  102. package/src/lib/thread/snapshot.ts +54 -32
  103. package/src/lib/thread/test-utils.ts +106 -59
  104. package/src/lib/thread/tiered.test.ts +1 -1
  105. package/src/lib/thread/types.ts +2 -2
  106. package/src/lib/tool-router/router.integration.test.ts +44 -0
  107. package/src/lib/tool-router/router.ts +140 -32
  108. package/src/lib/workflow.ts +49 -0
  109. package/src/{adapters/sandbox/inmemory/proxy.ts → test-utils/in-memory-sandbox-proxy.ts} +5 -16
  110. package/src/{adapters/sandbox/inmemory/index.ts → test-utils/in-memory-sandbox.ts} +11 -3
  111. package/src/tools/bash/bash.test.ts +1 -1
  112. package/src/tools/edit/handler.test.ts +1 -1
  113. package/tsup.config.ts +2 -4
  114. package/dist/activities-7OcT_vdR.d.cts +0 -162
  115. package/dist/activities-zG_FBoY2.d.ts +0 -162
  116. package/dist/adapters/sandbox/inmemory/index.cjs +0 -214
  117. package/dist/adapters/sandbox/inmemory/index.cjs.map +0 -1
  118. package/dist/adapters/sandbox/inmemory/index.d.cts +0 -40
  119. package/dist/adapters/sandbox/inmemory/index.d.ts +0 -40
  120. package/dist/adapters/sandbox/inmemory/index.js +0 -211
  121. package/dist/adapters/sandbox/inmemory/index.js.map +0 -1
  122. package/dist/adapters/sandbox/inmemory/workflow.cjs +0 -36
  123. package/dist/adapters/sandbox/inmemory/workflow.cjs.map +0 -1
  124. package/dist/adapters/sandbox/inmemory/workflow.d.cts +0 -27
  125. package/dist/adapters/sandbox/inmemory/workflow.d.ts +0 -27
  126. package/dist/adapters/sandbox/inmemory/workflow.js +0 -34
  127. package/dist/adapters/sandbox/inmemory/workflow.js.map +0 -1
@@ -1,26 +1,15 @@
1
1
  /**
2
- * Workflow-safe proxy for in-memory sandbox operations.
2
+ * Test-only workflow-safe proxy for the in-memory sandbox fixture.
3
3
  *
4
- * Import this from `zeitlich/adapters/sandbox/inmemory/workflow`
5
- * in your Temporal workflow files.
6
- *
7
- * By default the scope is derived from `workflowInfo().workflowType`,
8
- * so activities are automatically namespaced per workflow.
9
- *
10
- * @example
11
- * ```typescript
12
- * import { proxyInMemorySandboxOps } from 'zeitlich/adapters/sandbox/inmemory/workflow';
13
- *
14
- * // Auto-scoped to the current workflow name
15
- * const sandbox = proxyInMemorySandboxOps();
16
- * ```
4
+ * Not part of the shipped public surface — used by the capability-type
5
+ * fixtures to exercise the full-capability proxy shape.
17
6
  */
18
7
  import { proxyActivities, workflowInfo } from "@temporalio/workflow";
19
8
  import type {
20
9
  SandboxCreateOptions,
21
10
  SandboxOps,
22
- } from "../../../lib/sandbox/types";
23
- import type { InMemoryCaps } from "./index";
11
+ } from "../lib/sandbox/types";
12
+ import type { InMemoryCaps } from "./in-memory-sandbox";
24
13
 
25
14
  const ADAPTER_PREFIX = "inMemory";
26
15
 
@@ -1,3 +1,11 @@
1
+ /**
2
+ * Test-only in-memory {@link Sandbox} provider backed by `just-bash`.
3
+ *
4
+ * This is **not** part of the shipped public surface — it lives under
5
+ * `src/test-utils` purely so the sandbox manager, tool handlers, and
6
+ * capability-type fixtures have a lightweight, fully-featured backend to
7
+ * exercise against. `just-bash` is a dev dependency for the same reason.
8
+ */
1
9
  import {
2
10
  Bash,
3
11
  InMemoryFs,
@@ -18,9 +26,9 @@ import type {
18
26
  ExecResult,
19
27
  DirentEntry,
20
28
  FileStat,
21
- } from "../../../lib/sandbox/types";
22
- import { SandboxNotFoundError } from "../../../lib/sandbox/types";
23
- import { getShortId } from "../../../lib/thread/id";
29
+ } from "../lib/sandbox/types";
30
+ import { SandboxNotFoundError } from "../lib/sandbox/types";
31
+ import { getShortId } from "../lib/thread/id";
24
32
 
25
33
  // ============================================================================
26
34
  // Adapter: IFileSystem → SandboxFileSystem
@@ -2,7 +2,7 @@ import { describe, expect, it, beforeEach } from "vitest";
2
2
  import { bashHandler } from "./handler";
3
3
  import { withSandbox } from "../../lib/tool-router/with-sandbox";
4
4
  import { SandboxManager } from "../../lib/sandbox/manager";
5
- import { InMemorySandboxProvider } from "../../adapters/sandbox/inmemory/index";
5
+ import { InMemorySandboxProvider } from "../../test-utils/in-memory-sandbox";
6
6
  import type { RouterContext } from "../../lib/tool-router/types";
7
7
  import type { Sandbox, SandboxCreateOptions } from "../../lib/sandbox";
8
8
 
@@ -1,5 +1,5 @@
1
1
  import { beforeEach, describe, expect, it } from "vitest";
2
- import { InMemorySandboxProvider } from "../../adapters/sandbox/inmemory/index";
2
+ import { InMemorySandboxProvider } from "../../test-utils/in-memory-sandbox";
3
3
  import type { Sandbox, SandboxCreateOptions } from "../../lib/sandbox";
4
4
  import { SandboxManager } from "../../lib/sandbox/manager";
5
5
  import type { RouterContext } from "../../lib/tool-router/types";
package/tsup.config.ts CHANGED
@@ -15,9 +15,6 @@ export default defineConfig({
15
15
  "adapters/thread/anthropic/index": "src/adapters/thread/anthropic/index.ts",
16
16
  "adapters/thread/anthropic/workflow":
17
17
  "src/adapters/thread/anthropic/proxy.ts",
18
- "adapters/sandbox/inmemory/index": "src/adapters/sandbox/inmemory/index.ts",
19
- "adapters/sandbox/inmemory/workflow":
20
- "src/adapters/sandbox/inmemory/proxy.ts",
21
18
  "adapters/sandbox/daytona/index": "src/adapters/sandbox/daytona/index.ts",
22
19
  "adapters/sandbox/daytona/workflow":
23
20
  "src/adapters/sandbox/daytona/proxy.ts",
@@ -38,7 +35,8 @@ export default defineConfig({
38
35
  /^@anthropic-ai\//,
39
36
  /^@daytonaio\//,
40
37
  /^@e2b\//,
41
- "ioredis",
38
+ "redis",
39
+ /^@redis\//,
42
40
  "@mongodb-js/zstd",
43
41
  "node-liblzma",
44
42
  ],
@@ -1,162 +0,0 @@
1
- import Redis from 'ioredis';
2
- import { Part, Content, GoogleGenAI } from '@google/genai';
3
- import { a as ModelInvoker, b as PrefixedThreadOps, S as ScopedPrefix, R as RouterContext, c as ToolHandlerResponse, d as ActivityToolHandler } from './types-XUUFvrJ9.cjs';
4
- import { C as ColdThreadStore } from './cold-store-CkWoNtMh.cjs';
5
- import { T as ThreadManagerHooks, P as ProviderThreadManager } from './types-BR-k7h0e.cjs';
6
- import { A as ADAPTER_ID } from './adapter-id-BB-mmrts.cjs';
7
-
8
- /** SDK-native content type for Google GenAI human messages */
9
- type GoogleGenAIContent = string | Part[];
10
- /** SDK-native content type for Google GenAI system instructions */
11
- type GoogleGenAISystemContent = string | Part[];
12
- /** A Content with a unique ID for idempotent Redis storage */
13
- interface StoredContent {
14
- id: string;
15
- content: Content;
16
- }
17
- type GoogleGenAIThreadManagerHooks = ThreadManagerHooks<StoredContent, Content>;
18
- interface GoogleGenAIThreadManagerConfig {
19
- redis: Redis;
20
- threadId: string;
21
- /** Thread key, defaults to 'messages' */
22
- key?: string;
23
- hooks?: GoogleGenAIThreadManagerHooks;
24
- /**
25
- * Override the default thread TTL (90 days). When pairing the
26
- * adapter with a durable cold tier, a shorter TTL (hours) is
27
- * typically more appropriate.
28
- */
29
- ttlSeconds?: number;
30
- }
31
- /** Prepared payload ready to send to the Google GenAI API */
32
- interface GoogleGenAIInvocationPayload {
33
- contents: Content[];
34
- systemInstruction?: Part[];
35
- }
36
- /** Thread manager with Google GenAI Content convenience helpers */
37
- interface GoogleGenAIThreadManager extends ProviderThreadManager<StoredContent, GoogleGenAIContent, GoogleGenAIToolResponse, GoogleGenAISystemContent> {
38
- appendModelContent(id: string, parts: Part[]): Promise<void>;
39
- prepareForInvocation(): Promise<GoogleGenAIInvocationPayload>;
40
- }
41
- /**
42
- * Creates a Google GenAI-specific thread manager that stores StoredContent
43
- * instances in Redis and provides convenience helpers for creating and
44
- * appending typed Content messages.
45
- */
46
- declare function createGoogleGenAIThreadManager(config: GoogleGenAIThreadManagerConfig): GoogleGenAIThreadManager;
47
-
48
- type GoogleGenAIThreadOps<TScope extends string = ""> = PrefixedThreadOps<ScopedPrefix<TScope, typeof ADAPTER_ID>, GoogleGenAIContent>;
49
- interface GoogleGenAIAdapterConfig {
50
- redis: Redis;
51
- client?: GoogleGenAI;
52
- /** Default model name (e.g. 'gemini-2.5-flash'). If omitted, use `createModelInvoker()` */
53
- model?: string;
54
- hooks?: GoogleGenAIThreadManagerHooks;
55
- /**
56
- * Optional durable cold tier (e.g. S3, R2, GCS). When provided,
57
- * the session hydrates the thread on entry (`continue`/`fork`) and
58
- * flushes it on every exit path. When omitted, the adapter is
59
- * Redis-only and `hydrateThread`/`flushThread` activities are no-ops.
60
- */
61
- coldStore?: ColdThreadStore;
62
- /**
63
- * Override the default Redis TTL (90 days). When pairing the
64
- * adapter with a `coldStore`, a shorter TTL (hours) is typically
65
- * more appropriate.
66
- */
67
- ttlSeconds?: number;
68
- }
69
- /**
70
- * Tool response type accepted by the Google GenAI adapter.
71
- *
72
- * Handlers can return:
73
- * - **`string`** — plain text, wrapped in a `functionResponse` part.
74
- * - **`Record<string, unknown>`** — structured object used as `functionResponse.response`.
75
- * - **`Part[]`** — pre-built parts used directly as `Content.parts`.
76
- * The handler is responsible for building correct Part objects (e.g. `functionResponse`,
77
- * `inlineData`, `text`). Use `context.toolCallId` and `context.toolName` to construct
78
- * `functionResponse` parts.
79
- *
80
- * @example
81
- * ```typescript
82
- * adapter.wrapHandler(async (args, ctx) => ({
83
- * toolResponse: [
84
- * { functionResponse: { id: ctx.toolCallId, name: ctx.toolName, response: { result: "done" } } },
85
- * { inlineData: { data: base64, mimeType: "image/png" } },
86
- * ],
87
- * data: null,
88
- * }));
89
- * ```
90
- */
91
- type GoogleGenAIToolResponse = string | Record<string, unknown> | Part[];
92
- interface GoogleGenAIAdapter {
93
- /** Model invoker using the default model (only available when `model` was provided) */
94
- invoker: ModelInvoker<Content>;
95
- /** Create an invoker for a specific model name (for multi-model setups) */
96
- createModelInvoker(model: string, client: GoogleGenAI): ModelInvoker<Content>;
97
- /**
98
- * Create prefixed thread activities for registration on the worker.
99
- *
100
- * @param scope - Workflow name appended to the adapter prefix.
101
- * Use different scopes for the main agent vs subagents to avoid collisions.
102
- *
103
- * @example
104
- * ```typescript
105
- * adapter.createActivities("codingAgent")
106
- * // → { googleGenAICodingAgentInitializeThread, googleGenAICodingAgentAppendHumanMessage, … }
107
- *
108
- * adapter.createActivities("researchAgent")
109
- * // → { googleGenAIResearchAgentInitializeThread, … }
110
- * ```
111
- */
112
- createActivities<S extends string = "">(scope?: S): GoogleGenAIThreadOps<S>;
113
- /**
114
- * Identity wrapper that types a tool handler for this adapter.
115
- * Constrains `toolResponse` to {@link GoogleGenAIToolResponse}.
116
- */
117
- wrapHandler<TArgs, TResult, TContext extends RouterContext = RouterContext>(handler: (args: TArgs, context: TContext) => Promise<ToolHandlerResponse<TResult, GoogleGenAIToolResponse>>): ActivityToolHandler<TArgs, TResult, TContext, GoogleGenAIToolResponse>;
118
- }
119
- /**
120
- * Creates a Google GenAI adapter that bundles thread operations and model
121
- * invocation using the `@google/genai` SDK.
122
- *
123
- * Use `createActivities(scope)` to register scoped thread operations as
124
- * Temporal activities on the worker. The `invoker` (or invokers created via
125
- * `createModelInvoker`) should be wrapped with `createRunAgentActivity`.
126
- *
127
- * @example
128
- * ```typescript
129
- * import { createGoogleGenAIAdapter } from 'zeitlich/adapters/thread/google-genai';
130
- * import { createRunAgentActivity } from 'zeitlich';
131
- * import { GoogleGenAI } from '@google/genai';
132
- *
133
- * const client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
134
- * const adapter = createGoogleGenAIAdapter({ redis, client, model: 'gemini-2.5-flash' });
135
- *
136
- * export function createActivities(temporalClient: WorkflowClient) {
137
- * return {
138
- * ...adapter.createActivities("codingAgent"),
139
- * ...createRunAgentActivity(temporalClient, adapter.invoker, "codingAgent"),
140
- * };
141
- * }
142
- * ```
143
- *
144
- * @example Multi-agent worker (main + subagent share the adapter)
145
- * ```typescript
146
- * export function createActivities(temporalClient: WorkflowClient) {
147
- * return {
148
- * ...adapter.createActivities("codingAgent"),
149
- * ...adapter.createActivities("researchAgent"),
150
- * ...createRunAgentActivity(temporalClient, adapter.invoker, "codingAgent"),
151
- * ...createRunAgentActivity(
152
- * temporalClient,
153
- * adapter.createModelInvoker('gemini-2.5-pro'),
154
- * "researchAgent",
155
- * ),
156
- * };
157
- * }
158
- * ```
159
- */
160
- declare function createGoogleGenAIAdapter(config: GoogleGenAIAdapterConfig): GoogleGenAIAdapter;
161
-
162
- export { type GoogleGenAIThreadManagerHooks as G, type StoredContent as S, type GoogleGenAIAdapter as a, type GoogleGenAIAdapterConfig as b, type GoogleGenAIContent as c, type GoogleGenAIInvocationPayload as d, type GoogleGenAIThreadManager as e, type GoogleGenAIThreadManagerConfig as f, type GoogleGenAIThreadOps as g, type GoogleGenAIToolResponse as h, createGoogleGenAIAdapter as i, createGoogleGenAIThreadManager as j };
@@ -1,162 +0,0 @@
1
- import Redis from 'ioredis';
2
- import { Part, Content, GoogleGenAI } from '@google/genai';
3
- import { a as ModelInvoker, b as PrefixedThreadOps, S as ScopedPrefix, R as RouterContext, c as ToolHandlerResponse, d as ActivityToolHandler } from './types-DeVNWqlb.js';
4
- import { C as ColdThreadStore } from './cold-store-DKMAO1Dd.js';
5
- import { T as ThreadManagerHooks, P as ProviderThreadManager } from './types-DO4Tkwxo.js';
6
- import { A as ADAPTER_ID } from './adapter-id-BB-mmrts.js';
7
-
8
- /** SDK-native content type for Google GenAI human messages */
9
- type GoogleGenAIContent = string | Part[];
10
- /** SDK-native content type for Google GenAI system instructions */
11
- type GoogleGenAISystemContent = string | Part[];
12
- /** A Content with a unique ID for idempotent Redis storage */
13
- interface StoredContent {
14
- id: string;
15
- content: Content;
16
- }
17
- type GoogleGenAIThreadManagerHooks = ThreadManagerHooks<StoredContent, Content>;
18
- interface GoogleGenAIThreadManagerConfig {
19
- redis: Redis;
20
- threadId: string;
21
- /** Thread key, defaults to 'messages' */
22
- key?: string;
23
- hooks?: GoogleGenAIThreadManagerHooks;
24
- /**
25
- * Override the default thread TTL (90 days). When pairing the
26
- * adapter with a durable cold tier, a shorter TTL (hours) is
27
- * typically more appropriate.
28
- */
29
- ttlSeconds?: number;
30
- }
31
- /** Prepared payload ready to send to the Google GenAI API */
32
- interface GoogleGenAIInvocationPayload {
33
- contents: Content[];
34
- systemInstruction?: Part[];
35
- }
36
- /** Thread manager with Google GenAI Content convenience helpers */
37
- interface GoogleGenAIThreadManager extends ProviderThreadManager<StoredContent, GoogleGenAIContent, GoogleGenAIToolResponse, GoogleGenAISystemContent> {
38
- appendModelContent(id: string, parts: Part[]): Promise<void>;
39
- prepareForInvocation(): Promise<GoogleGenAIInvocationPayload>;
40
- }
41
- /**
42
- * Creates a Google GenAI-specific thread manager that stores StoredContent
43
- * instances in Redis and provides convenience helpers for creating and
44
- * appending typed Content messages.
45
- */
46
- declare function createGoogleGenAIThreadManager(config: GoogleGenAIThreadManagerConfig): GoogleGenAIThreadManager;
47
-
48
- type GoogleGenAIThreadOps<TScope extends string = ""> = PrefixedThreadOps<ScopedPrefix<TScope, typeof ADAPTER_ID>, GoogleGenAIContent>;
49
- interface GoogleGenAIAdapterConfig {
50
- redis: Redis;
51
- client?: GoogleGenAI;
52
- /** Default model name (e.g. 'gemini-2.5-flash'). If omitted, use `createModelInvoker()` */
53
- model?: string;
54
- hooks?: GoogleGenAIThreadManagerHooks;
55
- /**
56
- * Optional durable cold tier (e.g. S3, R2, GCS). When provided,
57
- * the session hydrates the thread on entry (`continue`/`fork`) and
58
- * flushes it on every exit path. When omitted, the adapter is
59
- * Redis-only and `hydrateThread`/`flushThread` activities are no-ops.
60
- */
61
- coldStore?: ColdThreadStore;
62
- /**
63
- * Override the default Redis TTL (90 days). When pairing the
64
- * adapter with a `coldStore`, a shorter TTL (hours) is typically
65
- * more appropriate.
66
- */
67
- ttlSeconds?: number;
68
- }
69
- /**
70
- * Tool response type accepted by the Google GenAI adapter.
71
- *
72
- * Handlers can return:
73
- * - **`string`** — plain text, wrapped in a `functionResponse` part.
74
- * - **`Record<string, unknown>`** — structured object used as `functionResponse.response`.
75
- * - **`Part[]`** — pre-built parts used directly as `Content.parts`.
76
- * The handler is responsible for building correct Part objects (e.g. `functionResponse`,
77
- * `inlineData`, `text`). Use `context.toolCallId` and `context.toolName` to construct
78
- * `functionResponse` parts.
79
- *
80
- * @example
81
- * ```typescript
82
- * adapter.wrapHandler(async (args, ctx) => ({
83
- * toolResponse: [
84
- * { functionResponse: { id: ctx.toolCallId, name: ctx.toolName, response: { result: "done" } } },
85
- * { inlineData: { data: base64, mimeType: "image/png" } },
86
- * ],
87
- * data: null,
88
- * }));
89
- * ```
90
- */
91
- type GoogleGenAIToolResponse = string | Record<string, unknown> | Part[];
92
- interface GoogleGenAIAdapter {
93
- /** Model invoker using the default model (only available when `model` was provided) */
94
- invoker: ModelInvoker<Content>;
95
- /** Create an invoker for a specific model name (for multi-model setups) */
96
- createModelInvoker(model: string, client: GoogleGenAI): ModelInvoker<Content>;
97
- /**
98
- * Create prefixed thread activities for registration on the worker.
99
- *
100
- * @param scope - Workflow name appended to the adapter prefix.
101
- * Use different scopes for the main agent vs subagents to avoid collisions.
102
- *
103
- * @example
104
- * ```typescript
105
- * adapter.createActivities("codingAgent")
106
- * // → { googleGenAICodingAgentInitializeThread, googleGenAICodingAgentAppendHumanMessage, … }
107
- *
108
- * adapter.createActivities("researchAgent")
109
- * // → { googleGenAIResearchAgentInitializeThread, … }
110
- * ```
111
- */
112
- createActivities<S extends string = "">(scope?: S): GoogleGenAIThreadOps<S>;
113
- /**
114
- * Identity wrapper that types a tool handler for this adapter.
115
- * Constrains `toolResponse` to {@link GoogleGenAIToolResponse}.
116
- */
117
- wrapHandler<TArgs, TResult, TContext extends RouterContext = RouterContext>(handler: (args: TArgs, context: TContext) => Promise<ToolHandlerResponse<TResult, GoogleGenAIToolResponse>>): ActivityToolHandler<TArgs, TResult, TContext, GoogleGenAIToolResponse>;
118
- }
119
- /**
120
- * Creates a Google GenAI adapter that bundles thread operations and model
121
- * invocation using the `@google/genai` SDK.
122
- *
123
- * Use `createActivities(scope)` to register scoped thread operations as
124
- * Temporal activities on the worker. The `invoker` (or invokers created via
125
- * `createModelInvoker`) should be wrapped with `createRunAgentActivity`.
126
- *
127
- * @example
128
- * ```typescript
129
- * import { createGoogleGenAIAdapter } from 'zeitlich/adapters/thread/google-genai';
130
- * import { createRunAgentActivity } from 'zeitlich';
131
- * import { GoogleGenAI } from '@google/genai';
132
- *
133
- * const client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
134
- * const adapter = createGoogleGenAIAdapter({ redis, client, model: 'gemini-2.5-flash' });
135
- *
136
- * export function createActivities(temporalClient: WorkflowClient) {
137
- * return {
138
- * ...adapter.createActivities("codingAgent"),
139
- * ...createRunAgentActivity(temporalClient, adapter.invoker, "codingAgent"),
140
- * };
141
- * }
142
- * ```
143
- *
144
- * @example Multi-agent worker (main + subagent share the adapter)
145
- * ```typescript
146
- * export function createActivities(temporalClient: WorkflowClient) {
147
- * return {
148
- * ...adapter.createActivities("codingAgent"),
149
- * ...adapter.createActivities("researchAgent"),
150
- * ...createRunAgentActivity(temporalClient, adapter.invoker, "codingAgent"),
151
- * ...createRunAgentActivity(
152
- * temporalClient,
153
- * adapter.createModelInvoker('gemini-2.5-pro'),
154
- * "researchAgent",
155
- * ),
156
- * };
157
- * }
158
- * ```
159
- */
160
- declare function createGoogleGenAIAdapter(config: GoogleGenAIAdapterConfig): GoogleGenAIAdapter;
161
-
162
- export { type GoogleGenAIThreadManagerHooks as G, type StoredContent as S, type GoogleGenAIAdapter as a, type GoogleGenAIAdapterConfig as b, type GoogleGenAIContent as c, type GoogleGenAIInvocationPayload as d, type GoogleGenAIThreadManager as e, type GoogleGenAIThreadManagerConfig as f, type GoogleGenAIThreadOps as g, type GoogleGenAIToolResponse as h, createGoogleGenAIAdapter as i, createGoogleGenAIThreadManager as j };
@@ -1,214 +0,0 @@
1
- 'use strict';
2
-
3
- var justBash = require('just-bash');
4
- var common = require('@temporalio/common');
5
- var workflow = require('@temporalio/workflow');
6
-
7
- // src/adapters/sandbox/inmemory/index.ts
8
- var SandboxNotFoundError = class extends common.ApplicationFailure {
9
- constructor(sandboxId) {
10
- super(`Sandbox not found: ${sandboxId}`, "SandboxNotFoundError", true);
11
- }
12
- };
13
- var BASE62 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
14
- function getShortId(length = 12) {
15
- const hex = workflow.uuid4().replace(/-/g, "");
16
- let result = "";
17
- for (let i = 0; i < length; i++) {
18
- const byte = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
19
- result += BASE62[byte % BASE62.length];
20
- }
21
- return result;
22
- }
23
-
24
- // src/adapters/sandbox/inmemory/index.ts
25
- function toSandboxFs(fs) {
26
- const workspaceBase = "/";
27
- const normalisePath = (path) => fs.resolvePath(workspaceBase, path);
28
- return {
29
- workspaceBase,
30
- readFile: (path) => fs.readFile(normalisePath(path)),
31
- readFileBuffer: (path) => fs.readFileBuffer(normalisePath(path)),
32
- writeFile: (path, content) => fs.writeFile(normalisePath(path), content),
33
- appendFile: (path, content) => fs.appendFile(normalisePath(path), content),
34
- exists: (path) => fs.exists(normalisePath(path)),
35
- stat: async (path) => {
36
- const s = await fs.stat(normalisePath(path));
37
- return {
38
- isFile: s.isFile,
39
- isDirectory: s.isDirectory,
40
- isSymbolicLink: s.isSymbolicLink,
41
- size: s.size,
42
- mtime: s.mtime
43
- };
44
- },
45
- mkdir: (path, opts) => fs.mkdir(normalisePath(path), opts),
46
- readdir: (path) => fs.readdir(normalisePath(path)),
47
- readdirWithFileTypes: async (path) => {
48
- const dirPath = normalisePath(path);
49
- if (!fs.readdirWithFileTypes) {
50
- const names = await fs.readdir(dirPath);
51
- return Promise.all(
52
- names.map(async (name) => {
53
- const childPath = fs.resolvePath(dirPath, name);
54
- const s = await fs.stat(childPath);
55
- return {
56
- name,
57
- isFile: s.isFile,
58
- isDirectory: s.isDirectory,
59
- isSymbolicLink: s.isSymbolicLink
60
- };
61
- })
62
- );
63
- }
64
- return fs.readdirWithFileTypes(dirPath);
65
- },
66
- rm: (path, opts) => fs.rm(normalisePath(path), opts),
67
- cp: (src, dest, opts) => fs.cp(normalisePath(src), normalisePath(dest), opts),
68
- mv: (src, dest) => fs.mv(normalisePath(src), normalisePath(dest)),
69
- readlink: (path) => fs.readlink(normalisePath(path)),
70
- resolvePath: (base, p) => fs.resolvePath(normalisePath(base), p)
71
- };
72
- }
73
- var InMemorySandboxImpl = class {
74
- constructor(id, justBashFs, options) {
75
- this.id = id;
76
- this.justBashFs = justBashFs;
77
- this.fs = toSandboxFs(justBashFs);
78
- this.bashOptions = {
79
- executionLimits: { maxStringLength: 52428800 },
80
- ...options?.bashOptions
81
- };
82
- }
83
- id;
84
- justBashFs;
85
- capabilities = {
86
- filesystem: true,
87
- execution: true,
88
- persistence: true
89
- };
90
- fs;
91
- bashOptions;
92
- async exec(command, _options) {
93
- const bash = new justBash.Bash({ ...this.bashOptions, fs: this.justBashFs });
94
- const { exitCode, stderr, stdout } = await bash.exec(command);
95
- return { exitCode, stdout, stderr };
96
- }
97
- async destroy() {
98
- }
99
- /** Expose the underlying IFileSystem for snapshot serialisation */
100
- _getJustBashFs() {
101
- return this.justBashFs;
102
- }
103
- };
104
- var IN_MEMORY_CAPS = [
105
- "pause",
106
- "resume",
107
- "snapshot",
108
- "restore",
109
- "fork"
110
- ];
111
- var InMemorySandboxProvider = class {
112
- constructor(defaultOptions) {
113
- this.defaultOptions = defaultOptions;
114
- }
115
- defaultOptions;
116
- id = "inMemory";
117
- capabilities = {
118
- filesystem: true,
119
- execution: true,
120
- persistence: true
121
- };
122
- supportedCapabilities = new Set(
123
- IN_MEMORY_CAPS
124
- );
125
- sandboxes = /* @__PURE__ */ new Map();
126
- async get(id) {
127
- const sandbox = this.sandboxes.get(id);
128
- if (!sandbox) throw new SandboxNotFoundError(id);
129
- return sandbox;
130
- }
131
- async destroy(id) {
132
- const sandbox = this.sandboxes.get(id);
133
- if (sandbox) {
134
- await sandbox.destroy();
135
- this.sandboxes.delete(id);
136
- }
137
- }
138
- async pause(_sandboxId, _ttlSeconds) {
139
- }
140
- async resume(_sandboxId) {
141
- }
142
- async create(options) {
143
- const id = options?.id ?? getShortId();
144
- const initialFiles = {};
145
- if (options?.initialFiles) {
146
- for (const [path, content] of Object.entries(options.initialFiles)) {
147
- initialFiles[path] = content;
148
- }
149
- }
150
- const fs = new justBash.InMemoryFs(initialFiles);
151
- const sandbox = new InMemorySandboxImpl(id, fs, this.defaultOptions);
152
- this.sandboxes.set(id, sandbox);
153
- return { sandbox };
154
- }
155
- async snapshot(sandboxId, _options) {
156
- const sandbox = this.sandboxes.get(sandboxId);
157
- if (!sandbox) throw new SandboxNotFoundError(sandboxId);
158
- const fs = sandbox._getJustBashFs();
159
- const paths = fs.getAllPaths();
160
- const files = {};
161
- for (const p of paths) {
162
- try {
163
- const stat = await fs.stat(p);
164
- if (stat.isFile) {
165
- files[p] = await fs.readFile(p);
166
- }
167
- } catch {
168
- }
169
- }
170
- return {
171
- sandboxId,
172
- providerId: this.id,
173
- data: { files },
174
- createdAt: (/* @__PURE__ */ new Date()).toISOString()
175
- };
176
- }
177
- async fork(sandboxId, _options) {
178
- const sandbox = await this.get(sandboxId);
179
- const entries = await sandbox.fs.readdirWithFileTypes("/");
180
- const initialFiles = {};
181
- for (const entry of entries) {
182
- if (entry.isFile) {
183
- initialFiles[entry.name] = await sandbox.fs.readFileBuffer(entry.name);
184
- }
185
- }
186
- const newSandbox = await this.create({
187
- id: getShortId(),
188
- initialFiles
189
- });
190
- return newSandbox.sandbox;
191
- }
192
- async restore(snapshot, _options) {
193
- const { files } = snapshot.data;
194
- const initialFiles = {};
195
- for (const [path, content] of Object.entries(files)) {
196
- initialFiles[path] = content;
197
- }
198
- const fs = new justBash.InMemoryFs(initialFiles);
199
- const sandbox = new InMemorySandboxImpl(
200
- snapshot.sandboxId,
201
- fs,
202
- this.defaultOptions
203
- );
204
- this.sandboxes.set(sandbox.id, sandbox);
205
- return sandbox;
206
- }
207
- async deleteSnapshot(_snapshot) {
208
- }
209
- };
210
-
211
- exports.IN_MEMORY_CAPS = IN_MEMORY_CAPS;
212
- exports.InMemorySandboxProvider = InMemorySandboxProvider;
213
- //# sourceMappingURL=index.cjs.map
214
- //# sourceMappingURL=index.cjs.map