zeitlich 0.2.31 → 0.2.32

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 (82) hide show
  1. package/dist/{activities-qPkJDAiq.d.cts → activities-DA-bQM12.d.cts} +2 -2
  2. package/dist/{activities-DRSdt8Y3.d.ts → activities-FIXVz7DT.d.ts} +2 -2
  3. package/dist/adapters/thread/anthropic/index.cjs +4 -48
  4. package/dist/adapters/thread/anthropic/index.cjs.map +1 -1
  5. package/dist/adapters/thread/anthropic/index.d.cts +6 -6
  6. package/dist/adapters/thread/anthropic/index.d.ts +6 -6
  7. package/dist/adapters/thread/anthropic/index.js +4 -48
  8. package/dist/adapters/thread/anthropic/index.js.map +1 -1
  9. package/dist/adapters/thread/anthropic/workflow.cjs +1 -0
  10. package/dist/adapters/thread/anthropic/workflow.cjs.map +1 -1
  11. package/dist/adapters/thread/anthropic/workflow.d.cts +4 -4
  12. package/dist/adapters/thread/anthropic/workflow.d.ts +4 -4
  13. package/dist/adapters/thread/anthropic/workflow.js +1 -0
  14. package/dist/adapters/thread/anthropic/workflow.js.map +1 -1
  15. package/dist/adapters/thread/google-genai/index.cjs +8 -48
  16. package/dist/adapters/thread/google-genai/index.cjs.map +1 -1
  17. package/dist/adapters/thread/google-genai/index.d.cts +6 -6
  18. package/dist/adapters/thread/google-genai/index.d.ts +6 -6
  19. package/dist/adapters/thread/google-genai/index.js +8 -48
  20. package/dist/adapters/thread/google-genai/index.js.map +1 -1
  21. package/dist/adapters/thread/google-genai/workflow.cjs +1 -0
  22. package/dist/adapters/thread/google-genai/workflow.cjs.map +1 -1
  23. package/dist/adapters/thread/google-genai/workflow.d.cts +4 -4
  24. package/dist/adapters/thread/google-genai/workflow.d.ts +4 -4
  25. package/dist/adapters/thread/google-genai/workflow.js +1 -0
  26. package/dist/adapters/thread/google-genai/workflow.js.map +1 -1
  27. package/dist/adapters/thread/langchain/index.cjs +5 -1
  28. package/dist/adapters/thread/langchain/index.cjs.map +1 -1
  29. package/dist/adapters/thread/langchain/index.d.cts +6 -5
  30. package/dist/adapters/thread/langchain/index.d.ts +6 -5
  31. package/dist/adapters/thread/langchain/index.js +5 -1
  32. package/dist/adapters/thread/langchain/index.js.map +1 -1
  33. package/dist/adapters/thread/langchain/workflow.cjs +1 -0
  34. package/dist/adapters/thread/langchain/workflow.cjs.map +1 -1
  35. package/dist/adapters/thread/langchain/workflow.d.cts +4 -4
  36. package/dist/adapters/thread/langchain/workflow.d.ts +4 -4
  37. package/dist/adapters/thread/langchain/workflow.js +1 -0
  38. package/dist/adapters/thread/langchain/workflow.js.map +1 -1
  39. package/dist/index.cjs +8 -5
  40. package/dist/index.cjs.map +1 -1
  41. package/dist/index.d.cts +6 -6
  42. package/dist/index.d.ts +6 -6
  43. package/dist/index.js +8 -5
  44. package/dist/index.js.map +1 -1
  45. package/dist/{proxy-BkvkV2oU.d.ts → proxy-Br4unLTC.d.ts} +1 -1
  46. package/dist/{proxy-BDQ3Rj6R.d.cts → proxy-CTCYWjkr.d.cts} +1 -1
  47. package/dist/{thread-manager-BLgvv9Gf.d.cts → thread-manager-CUubPYPH.d.cts} +1 -1
  48. package/dist/{thread-manager-DowU4ntB.d.cts → thread-manager-Cv_BR28i.d.cts} +1 -1
  49. package/dist/{thread-manager-Cv82H1wi.d.ts → thread-manager-DKWxHUzD.d.ts} +1 -1
  50. package/dist/{thread-manager-HsAYkyAV.d.ts → thread-manager-YJLoc1vH.d.ts} +1 -1
  51. package/dist/{types-CjeGWQm1.d.cts → types-Bpq5fDI5.d.cts} +7 -4
  52. package/dist/{types-D6UKZZtj.d.ts → types-BxiT8w9d.d.ts} +1 -1
  53. package/dist/{types-BmS-Huc0.d.ts → types-CheCTLeV.d.ts} +7 -4
  54. package/dist/{types-e_38QaKo.d.cts → types-NJDyMyUx.d.cts} +1 -1
  55. package/dist/{workflow-CNshfqSO.d.cts → workflow-BWKQcz9d.d.cts} +1 -1
  56. package/dist/{workflow-CTcrPZAV.d.ts → workflow-D8wK7TJY.d.ts} +1 -1
  57. package/dist/workflow.cjs +4 -1
  58. package/dist/workflow.cjs.map +1 -1
  59. package/dist/workflow.d.cts +2 -2
  60. package/dist/workflow.d.ts +2 -2
  61. package/dist/workflow.js +4 -1
  62. package/dist/workflow.js.map +1 -1
  63. package/package.json +1 -1
  64. package/src/adapters/thread/anthropic/activities.ts +10 -0
  65. package/src/adapters/thread/anthropic/model-invoker.ts +2 -5
  66. package/src/adapters/thread/google-genai/activities.ts +14 -0
  67. package/src/adapters/thread/google-genai/model-invoker.ts +2 -5
  68. package/src/adapters/thread/langchain/activities.ts +11 -0
  69. package/src/adapters/thread/langchain/model-invoker.ts +2 -3
  70. package/src/lib/.env +1 -0
  71. package/src/lib/model/types.ts +3 -2
  72. package/src/lib/session/session-edge-cases.integration.test.ts +6 -0
  73. package/src/lib/session/session.integration.test.ts +3 -0
  74. package/src/lib/session/session.ts +4 -0
  75. package/src/lib/session/types.ts +7 -0
  76. package/src/lib/thread/proxy.ts +1 -0
  77. package/src/lib/types.ts +1 -0
  78. package/src/lib/virtual-fs/manager.ts +3 -3
  79. package/src/lib/virtual-fs/proxy.ts +3 -3
  80. package/src/lib/virtual-fs/types.ts +1 -2
  81. package/src/lib/virtual-fs/with-virtual-fs.ts +4 -4
  82. package/src/tools/bash/.env +1 -0
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/thread/manager.ts","../../../../src/adapters/thread/google-genai/thread-manager.ts","../../../../node_modules/uuid/dist/esm/stringify.js","../../../../node_modules/uuid/dist/esm/rng.js","../../../../node_modules/uuid/dist/esm/native.js","../../../../node_modules/uuid/dist/esm/v4.js","../../../../src/adapters/thread/google-genai/model-invoker.ts","../../../../src/adapters/thread/google-genai/activities.ts"],"names":["invokeGoogleGenAIModel"],"mappings":";;;AAEA,IAAM,kBAAA,GAAqB,EAAA,GAAK,EAAA,GAAK,EAAA,GAAK,EAAA;AAW1C,IAAM,wBAAA,GAA2B;AAAA;AAAA;AAA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AAA,KAEA,6BAAA,CAA8B,EAAE,KAAA,EAAO,MAAA,EAAQ,KAAA,EAAO,KAAA,EAAO,MAAA,CAAO,KAAA,EAAO,CAAA;AAE7E,EAAA,MAAM,OAAA,GACJ,MAAA,CAAO,KAAA,IAAS,MAAA,CAAO,MAAA,GACnB,WAAA,CAAY,MAAA,CAAO,KAAA,EAAO,MAAA,CAAO,MAAM,CAAA,IACrC,MAAM;AACN,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KAEF;AAAA,EACF,CAAA,CAAA;AAEN,EAAA,OAAO;AAAA,IACL,gBAAA;AAAA,IACA,OAAA;AAAA,IACA,kBAAA,EAAoB,WAAA;AAAA,IACpB,WAAA,EAAa,CAAC,OAAA,KAAY;AAAA,GAC5B;AACF","file":"index.js","sourcesContent":["import type { ThreadManagerConfig, BaseThreadManager } from \"./types\";\n\nconst THREAD_TTL_SECONDS = 60 * 60 * 24 * 90; // 90 days\n\n/**\n * Lua script for atomic idempotent append.\n * Checks a dedup key; if it exists the message was already appended and we\n * return 0. Otherwise appends all messages to the list, sets TTL on both\n * the list and the dedup key, and returns 1.\n *\n * KEYS[1] = dedup key, KEYS[2] = list key\n * ARGV[1] = TTL seconds, ARGV[2..N] = serialised messages\n */\nconst APPEND_IDEMPOTENT_SCRIPT = `\nif redis.call('EXISTS', KEYS[1]) == 1 then\n return 0\nend\nfor i = 2, #ARGV do\n redis.call('RPUSH', KEYS[2], ARGV[i])\nend\nredis.call('EXPIRE', KEYS[2], tonumber(ARGV[1]))\nredis.call('SET', KEYS[1], '1', 'EX', tonumber(ARGV[1]))\nreturn 1\n`;\n\nfunction getThreadKey(threadId: string, key: string): string {\n return `${key}:thread:${threadId}`;\n}\n\n/**\n * Creates a generic thread manager for handling conversation state in Redis.\n * Framework-agnostic — works with any serializable message type.\n */\nexport function createThreadManager<T>(\n config: ThreadManagerConfig<T>\n): BaseThreadManager<T> {\n const {\n redis,\n threadId,\n key = \"messages\",\n serialize = (m: T): string => JSON.stringify(m),\n deserialize = (raw: string): T => JSON.parse(raw) as T,\n idOf,\n } = config;\n const redisKey = getThreadKey(threadId, key);\n const metaKey = getThreadKey(threadId, `${key}:meta`);\n\n async function assertThreadExists(): Promise<void> {\n const exists = await redis.exists(metaKey);\n if (!exists) {\n throw new Error(`Thread \"${threadId}\" (key: ${key}) does not exist`);\n }\n }\n\n return {\n async initialize(): Promise<void> {\n await redis.del(redisKey);\n await redis.set(metaKey, \"1\", \"EX\", THREAD_TTL_SECONDS);\n },\n\n async load(): Promise<T[]> {\n await assertThreadExists();\n const data = await redis.lrange(redisKey, 0, -1);\n return data.map(deserialize);\n },\n\n async append(messages: T[]): Promise<void> {\n if (messages.length === 0) return;\n await assertThreadExists();\n\n if (idOf) {\n const dedupId = messages.map(idOf).join(\":\");\n const dedupKey = getThreadKey(threadId, `dedup:${dedupId}`);\n await redis.eval(\n APPEND_IDEMPOTENT_SCRIPT,\n 2,\n dedupKey,\n redisKey,\n String(THREAD_TTL_SECONDS),\n ...messages.map(serialize)\n );\n } else {\n await redis.rpush(redisKey, ...messages.map(serialize));\n await redis.expire(redisKey, THREAD_TTL_SECONDS);\n }\n },\n\n async fork(newThreadId: string): Promise<BaseThreadManager<T>> {\n await assertThreadExists();\n const data = await redis.lrange(redisKey, 0, -1);\n const forked = createThreadManager({\n ...config,\n threadId: newThreadId,\n });\n await forked.initialize();\n if (data.length > 0) {\n const newKey = getThreadKey(newThreadId, key);\n await redis.rpush(newKey, ...data);\n await redis.expire(newKey, THREAD_TTL_SECONDS);\n }\n return forked;\n },\n\n async delete(): Promise<void> {\n await redis.del(redisKey, metaKey);\n },\n };\n}\n","import type Redis from \"ioredis\";\nimport type { Content, Part } from \"@google/genai\";\nimport { createThreadManager } from \"../../../lib/thread/manager\";\nimport type {\n ProviderThreadManager,\n ThreadManagerConfig,\n ThreadManagerHooks,\n} from \"../../../lib/thread/types\";\nimport type { GoogleGenAIToolResponse } from \"./activities\";\n\n/** SDK-native content type for Google GenAI human messages */\nexport type GoogleGenAIContent = string | Part[];\n\n/** A Content with a unique ID for idempotent Redis storage */\nexport interface StoredContent {\n id: string;\n content: Content;\n}\n\nexport type GoogleGenAIThreadManagerHooks = ThreadManagerHooks<StoredContent, Content>;\n\nexport interface GoogleGenAIThreadManagerConfig {\n redis: Redis;\n threadId: string;\n /** Thread key, defaults to 'messages' */\n key?: string;\n hooks?: GoogleGenAIThreadManagerHooks;\n}\n\n/** Prepared payload ready to send to the Google GenAI API */\nexport interface GoogleGenAIInvocationPayload {\n contents: Content[];\n systemInstruction?: string;\n}\n\n/** Thread manager with Google GenAI Content convenience helpers */\nexport interface GoogleGenAIThreadManager\n extends ProviderThreadManager<StoredContent, GoogleGenAIContent, GoogleGenAIToolResponse> {\n appendModelContent(id: string, parts: Part[]): Promise<void>;\n prepareForInvocation(): Promise<GoogleGenAIInvocationPayload>;\n}\n\nfunction storedContentId(msg: StoredContent): string {\n return msg.id;\n}\n\n/** Normalise content into Part[] */\nfunction toParts(content: GoogleGenAIContent): Part[] {\n if (typeof content === \"string\") {\n return [{ text: content }];\n }\n return content;\n}\n\n/** Convert a string or object into a Record suitable for functionResponse.response */\nfunction toFunctionResponse(content: string | Record<string, unknown>): Record<string, unknown> {\n if (typeof content === \"object\") {\n return content;\n }\n return { result: content };\n}\n\n/**\n * Merge consecutive Content objects sharing the same role.\n * The Gemini API requires alternating user/model turns; without\n * merging, multiple sequential tool-result messages would violate this.\n */\nfunction mergeConsecutiveContents(contents: Content[]): Content[] {\n const merged: Content[] = [];\n for (const content of contents) {\n const last = merged[merged.length - 1];\n if (last && last.role === content.role) {\n last.parts = [...(last.parts ?? []), ...(content.parts ?? [])];\n } else {\n merged.push({ ...content, parts: [...(content.parts ?? [])] });\n }\n }\n return merged;\n}\n\n/**\n * Creates a Google GenAI-specific thread manager that stores StoredContent\n * instances in Redis and provides convenience helpers for creating and\n * appending typed Content messages.\n */\nexport function createGoogleGenAIThreadManager(\n config: GoogleGenAIThreadManagerConfig,\n): GoogleGenAIThreadManager {\n const baseConfig: ThreadManagerConfig<StoredContent> = {\n redis: config.redis,\n threadId: config.threadId,\n key: config.key,\n idOf: storedContentId,\n };\n\n const base = createThreadManager(baseConfig);\n\n const helpers: Omit<GoogleGenAIThreadManager, keyof typeof base> = {\n async appendUserMessage(\n id: string,\n content: GoogleGenAIContent,\n ): Promise<void> {\n await base.append([{\n id,\n content: { role: \"user\", parts: toParts(content) },\n }]);\n },\n\n async appendSystemMessage(id: string, content: string): Promise<void> {\n await base.initialize();\n await base.append([{\n id,\n content: { role: \"system\", parts: [{ text: content }] },\n }]);\n },\n\n async appendModelContent(id: string, parts: Part[]): Promise<void> {\n await base.append([{\n id,\n content: { role: \"model\", parts },\n }]);\n },\n\n async appendToolResult(\n id: string,\n toolCallId: string,\n toolName: string,\n content: GoogleGenAIToolResponse,\n ): Promise<void> {\n const parts: Part[] = Array.isArray(content)\n ? content as Part[]\n : [{\n functionResponse: {\n id: toolCallId,\n name: toolName,\n response: toFunctionResponse(content),\n },\n }];\n\n await base.append([{\n id,\n content: { role: \"user\", parts },\n }]);\n },\n\n async prepareForInvocation(): Promise<GoogleGenAIInvocationPayload> {\n const stored = await base.load();\n const { onPrepareMessage, onPreparedMessage } = config.hooks ?? {};\n const mapped = onPrepareMessage\n ? stored.map((msg, i) => onPrepareMessage(msg, i, stored))\n : stored;\n\n let systemInstruction: string | undefined;\n const conversationContents: Content[] = [];\n\n for (const item of mapped) {\n if (item.content.role === \"system\") {\n systemInstruction = item.content.parts?.[0]?.text;\n } else {\n conversationContents.push(item.content);\n }\n }\n\n const contents = mergeConsecutiveContents(conversationContents);\n return {\n contents: onPreparedMessage\n ? contents.map((msg, i) => onPreparedMessage(msg, i, contents))\n : contents,\n ...(systemInstruction ? { systemInstruction } : {}),\n };\n },\n };\n\n return Object.assign(base, helpers);\n}\n","import validate from './validate.js';\nconst byteToHex = [];\nfor (let i = 0; i < 256; ++i) {\n byteToHex.push((i + 0x100).toString(16).slice(1));\n}\nexport function unsafeStringify(arr, offset = 0) {\n return (byteToHex[arr[offset + 0]] +\n byteToHex[arr[offset + 1]] +\n byteToHex[arr[offset + 2]] +\n byteToHex[arr[offset + 3]] +\n '-' +\n byteToHex[arr[offset + 4]] +\n byteToHex[arr[offset + 5]] +\n '-' +\n byteToHex[arr[offset + 6]] +\n byteToHex[arr[offset + 7]] +\n '-' +\n byteToHex[arr[offset + 8]] +\n byteToHex[arr[offset + 9]] +\n '-' +\n byteToHex[arr[offset + 10]] +\n byteToHex[arr[offset + 11]] +\n byteToHex[arr[offset + 12]] +\n byteToHex[arr[offset + 13]] +\n byteToHex[arr[offset + 14]] +\n byteToHex[arr[offset + 15]]).toLowerCase();\n}\nfunction stringify(arr, offset = 0) {\n const uuid = unsafeStringify(arr, offset);\n if (!validate(uuid)) {\n throw TypeError('Stringified UUID is invalid');\n }\n return uuid;\n}\nexport default stringify;\n","import { randomFillSync } from 'crypto';\nconst rnds8Pool = new Uint8Array(256);\nlet poolPtr = rnds8Pool.length;\nexport default function rng() {\n if (poolPtr > rnds8Pool.length - 16) {\n randomFillSync(rnds8Pool);\n poolPtr = 0;\n }\n return rnds8Pool.slice(poolPtr, (poolPtr += 16));\n}\n","import { randomUUID } from 'crypto';\nexport default { randomUUID };\n","import native from './native.js';\nimport rng from './rng.js';\nimport { unsafeStringify } from './stringify.js';\nfunction v4(options, buf, offset) {\n if (native.randomUUID && !buf && !options) {\n return native.randomUUID();\n }\n options = options || {};\n const rnds = options.random ?? options.rng?.() ?? rng();\n if (rnds.length < 16) {\n throw new Error('Random bytes length must be >= 16');\n }\n rnds[6] = (rnds[6] & 0x0f) | 0x40;\n rnds[8] = (rnds[8] & 0x3f) | 0x80;\n if (buf) {\n offset = offset || 0;\n if (offset < 0 || offset + 16 > buf.length) {\n throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);\n }\n for (let i = 0; i < 16; ++i) {\n buf[offset + i] = rnds[i];\n }\n return buf;\n }\n return unsafeStringify(rnds);\n}\nexport default v4;\n","import type Redis from \"ioredis\";\nimport type { GoogleGenAI, Content, FunctionDeclaration } from \"@google/genai\";\nimport type { SerializableToolDefinition } from \"../../../lib/types\";\nimport type { AgentResponse, ModelInvokerConfig } from \"../../../lib/model\";\nimport { createGoogleGenAIThreadManager, type GoogleGenAIThreadManagerHooks } from \"./thread-manager\";\nimport { v4 as uuidv4 } from \"uuid\";\n\nexport interface GoogleGenAIModelInvokerConfig {\n redis: Redis;\n client: GoogleGenAI;\n model: string;\n hooks?: GoogleGenAIThreadManagerHooks;\n}\n\nfunction toFunctionDeclarations(\n tools: SerializableToolDefinition[],\n): FunctionDeclaration[] {\n return tools.map((t) => ({\n name: t.name,\n description: t.description,\n parametersJsonSchema: t.schema,\n }));\n}\n\n/**\n * Creates a Google GenAI model invoker that satisfies the generic\n * `ModelInvoker<Content>` contract.\n *\n * Loads the conversation thread from Redis, invokes the Gemini model via\n * `client.models.generateContent`, appends the AI response, and returns\n * a normalised AgentResponse.\n *\n * @example\n * ```typescript\n * import { createGoogleGenAIModelInvoker } from 'zeitlich/adapters/thread/google-genai';\n * import { createRunAgentActivity } from 'zeitlich';\n * import { GoogleGenAI } from '@google/genai';\n *\n * const client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });\n * const invoker = createGoogleGenAIModelInvoker({\n * redis,\n * client,\n * model: 'gemini-2.5-flash',\n * });\n *\n * return { runAgent: createRunAgentActivity(client, invoker) };\n * ```\n */\nexport function createGoogleGenAIModelInvoker({\n redis,\n client,\n model,\n hooks,\n}: GoogleGenAIModelInvokerConfig) {\n return async function invokeGoogleGenAIModel(\n config: ModelInvokerConfig,\n ): Promise<AgentResponse<Content>> {\n const { threadId, threadKey, state } = config;\n\n const thread = createGoogleGenAIThreadManager({ redis, threadId, key: threadKey, hooks });\n const { contents, systemInstruction } =\n await thread.prepareForInvocation();\n\n const functionDeclarations = toFunctionDeclarations(state.tools);\n const tools =\n functionDeclarations.length > 0 ? [{ functionDeclarations }] : undefined;\n\n const response = await client.models.generateContent({\n model,\n contents,\n config: {\n ...(systemInstruction ? { systemInstruction } : {}),\n ...(tools ? { tools } : {}),\n },\n });\n\n const responseParts = response.candidates?.[0]?.content?.parts ?? [];\n const modelContent: Content = { role: \"model\", parts: responseParts };\n\n await thread.appendModelContent(uuidv4(), responseParts);\n\n const functionCalls = response.functionCalls ?? [];\n\n return {\n message: modelContent,\n rawToolCalls: functionCalls.map((fc) => ({\n id: fc.id,\n name: fc.name ?? \"\",\n args: fc.args ?? {},\n })),\n usage: {\n inputTokens: response.usageMetadata?.promptTokenCount,\n outputTokens: response.usageMetadata?.candidatesTokenCount,\n cachedReadTokens: response.usageMetadata?.cachedContentTokenCount,\n },\n };\n };\n}\n\n/**\n * Standalone function for one-shot Google GenAI model invocation.\n * Convenience wrapper around createGoogleGenAIModelInvoker for cases\n * where you don't need to reuse the invoker.\n */\nexport async function invokeGoogleGenAIModel({\n redis,\n client,\n model,\n hooks,\n config,\n}: {\n redis: Redis;\n client: GoogleGenAI;\n model: string;\n hooks?: GoogleGenAIThreadManagerHooks;\n config: ModelInvokerConfig;\n}): Promise<AgentResponse<Content>> {\n const invoker = createGoogleGenAIModelInvoker({ redis, client, model, hooks });\n return invoker(config);\n}\n","import type Redis from \"ioredis\";\nimport type { GoogleGenAI, Content, Part } from \"@google/genai\";\nimport type { ToolResultConfig } from \"../../../lib/types\";\nimport type {\n ActivityToolHandler,\n RouterContext,\n ToolHandlerResponse,\n} from \"../../../lib/tool-router/types\";\nimport type {\n ThreadOps,\n PrefixedThreadOps,\n ScopedPrefix,\n} from \"../../../lib/session/types\";\nimport type { ModelInvoker } from \"../../../lib/model\";\nimport {\n createGoogleGenAIThreadManager,\n type GoogleGenAIContent,\n type GoogleGenAIThreadManagerHooks,\n} from \"./thread-manager\";\nimport { createGoogleGenAIModelInvoker } from \"./model-invoker\";\n\nconst ADAPTER_PREFIX = \"googleGenAI\" as const;\n\nexport type GoogleGenAIThreadOps<TScope extends string = \"\"> =\n PrefixedThreadOps<\n ScopedPrefix<TScope, typeof ADAPTER_PREFIX>,\n GoogleGenAIContent\n >;\n\nexport interface GoogleGenAIAdapterConfig {\n redis: Redis;\n client?: GoogleGenAI;\n /** Default model name (e.g. 'gemini-2.5-flash'). If omitted, use `createModelInvoker()` */\n model?: string;\n hooks?: GoogleGenAIThreadManagerHooks;\n}\n\n/**\n * Tool response type accepted by the Google GenAI adapter.\n *\n * Handlers can return:\n * - **`string`** — plain text, wrapped in a `functionResponse` part.\n * - **`Record<string, unknown>`** — structured object used as `functionResponse.response`.\n * - **`Part[]`** — pre-built parts used directly as `Content.parts`.\n * The handler is responsible for building correct Part objects (e.g. `functionResponse`,\n * `inlineData`, `text`). Use `context.toolCallId` and `context.toolName` to construct\n * `functionResponse` parts.\n *\n * @example\n * ```typescript\n * adapter.wrapHandler(async (args, ctx) => ({\n * toolResponse: [\n * { functionResponse: { id: ctx.toolCallId, name: ctx.toolName, response: { result: \"done\" } } },\n * { inlineData: { data: base64, mimeType: \"image/png\" } },\n * ],\n * data: null,\n * }));\n * ```\n */\nexport type GoogleGenAIToolResponse = string | Record<string, unknown> | Part[];\n\nexport interface GoogleGenAIAdapter {\n /** Model invoker using the default model (only available when `model` was provided) */\n invoker: ModelInvoker<Content>;\n /** Create an invoker for a specific model name (for multi-model setups) */\n createModelInvoker(model: string, client: GoogleGenAI): ModelInvoker<Content>;\n /**\n * Create prefixed thread activities for registration on the worker.\n *\n * @param scope - Workflow name appended to the adapter prefix.\n * Use different scopes for the main agent vs subagents to avoid collisions.\n *\n * @example\n * ```typescript\n * adapter.createActivities(\"codingAgent\")\n * // → { googleGenAICodingAgentInitializeThread, googleGenAICodingAgentAppendHumanMessage, … }\n *\n * adapter.createActivities(\"researchAgent\")\n * // → { googleGenAIResearchAgentInitializeThread, … }\n * ```\n */\n createActivities<S extends string = \"\">(scope?: S): GoogleGenAIThreadOps<S>;\n\n /**\n * Identity wrapper that types a tool handler for this adapter.\n * Constrains `toolResponse` to {@link GoogleGenAIToolResponse}.\n */\n wrapHandler<TArgs, TResult, TContext extends RouterContext = RouterContext>(\n handler: (\n args: TArgs,\n context: TContext\n ) => Promise<ToolHandlerResponse<TResult, GoogleGenAIToolResponse>>\n ): ActivityToolHandler<TArgs, TResult, TContext, GoogleGenAIToolResponse>;\n}\n\n/**\n * Creates a Google GenAI adapter that bundles thread operations and model\n * invocation using the `@google/genai` SDK.\n *\n * Use `createActivities(scope)` to register scoped thread operations as\n * Temporal activities on the worker. The `invoker` (or invokers created via\n * `createModelInvoker`) should be wrapped with `createRunAgentActivity`.\n *\n * @example\n * ```typescript\n * import { createGoogleGenAIAdapter } from 'zeitlich/adapters/thread/google-genai';\n * import { createRunAgentActivity } from 'zeitlich';\n * import { GoogleGenAI } from '@google/genai';\n *\n * const client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });\n * const adapter = createGoogleGenAIAdapter({ redis, client, model: 'gemini-2.5-flash' });\n *\n * export function createActivities(temporalClient: WorkflowClient) {\n * return {\n * ...adapter.createActivities(\"codingAgent\"),\n * runCodingAgent: createRunAgentActivity(temporalClient, adapter.invoker),\n * };\n * }\n * ```\n *\n * @example Multi-agent worker (main + subagent share the adapter)\n * ```typescript\n * export function createActivities(temporalClient: WorkflowClient) {\n * return {\n * ...adapter.createActivities(\"codingAgent\"),\n * ...adapter.createActivities(\"researchAgent\"),\n * runCodingAgent: createRunAgentActivity(temporalClient, adapter.invoker),\n * runResearchAgent: createRunAgentActivity(\n * temporalClient,\n * adapter.createModelInvoker('gemini-2.5-pro'),\n * ),\n * };\n * }\n * ```\n */\nexport function createGoogleGenAIAdapter(\n config: GoogleGenAIAdapterConfig\n): GoogleGenAIAdapter {\n const { redis } = config;\n\n const threadOps: ThreadOps<GoogleGenAIContent> = {\n async initializeThread(\n threadId: string,\n threadKey?: string\n ): Promise<void> {\n const thread = createGoogleGenAIThreadManager({\n redis,\n threadId,\n key: threadKey,\n });\n await thread.initialize();\n },\n\n async appendHumanMessage(\n threadId: string,\n id: string,\n content: GoogleGenAIContent,\n threadKey?: string\n ): Promise<void> {\n const thread = createGoogleGenAIThreadManager({\n redis,\n threadId,\n key: threadKey,\n });\n await thread.appendUserMessage(id, content);\n },\n\n async appendSystemMessage(\n threadId: string,\n id: string,\n content: string,\n threadKey?: string\n ): Promise<void> {\n const thread = createGoogleGenAIThreadManager({\n redis,\n threadId,\n key: threadKey,\n });\n await thread.appendSystemMessage(id, content);\n },\n\n async appendToolResult(id: string, cfg: ToolResultConfig): Promise<void> {\n const { threadId, threadKey, toolCallId, toolName, content } = cfg;\n const thread = createGoogleGenAIThreadManager({\n redis,\n threadId,\n key: threadKey,\n });\n await thread.appendToolResult(\n id,\n toolCallId,\n toolName,\n content as GoogleGenAIToolResponse\n );\n },\n\n async forkThread(\n sourceThreadId: string,\n targetThreadId: string,\n threadKey?: string\n ): Promise<void> {\n const thread = createGoogleGenAIThreadManager({\n redis,\n threadId: sourceThreadId,\n key: threadKey,\n });\n await thread.fork(targetThreadId);\n },\n };\n\n function createActivities<S extends string = \"\">(\n scope?: S\n ): GoogleGenAIThreadOps<S> {\n const prefix = scope\n ? `${ADAPTER_PREFIX}${scope.charAt(0).toUpperCase()}${scope.slice(1)}`\n : ADAPTER_PREFIX;\n const cap = (s: string): string => s.charAt(0).toUpperCase() + s.slice(1);\n return Object.fromEntries(\n Object.entries(threadOps).map(([k, v]) => [`${prefix}${cap(k)}`, v])\n ) as GoogleGenAIThreadOps<S>;\n }\n\n const makeInvoker = (\n model: string,\n client: GoogleGenAI\n ): ModelInvoker<Content> =>\n createGoogleGenAIModelInvoker({ redis, client, model, hooks: config.hooks });\n\n const invoker: ModelInvoker<Content> =\n config.model && config.client\n ? makeInvoker(config.model, config.client)\n : ((() => {\n throw new Error(\n \"No default model provided to createGoogleGenAIAdapter. \" +\n \"Either pass `model` in the config or use `createModelInvoker(model)` instead.\"\n );\n }) as unknown as ModelInvoker<Content>);\n\n return {\n createActivities,\n invoker,\n createModelInvoker: makeInvoker,\n wrapHandler: (handler) => handler,\n };\n}\n"]}
1
+ {"version":3,"sources":["../../../../src/lib/thread/manager.ts","../../../../src/adapters/thread/google-genai/thread-manager.ts","../../../../src/adapters/thread/google-genai/model-invoker.ts","../../../../src/adapters/thread/google-genai/activities.ts"],"names":["invokeGoogleGenAIModel"],"mappings":";AAEA,IAAM,kBAAA,GAAqB,EAAA,GAAK,EAAA,GAAK,EAAA,GAAK,EAAA;AAW1C,IAAM,wBAAA,GAA2B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,CAAA;AAYjC,SAAS,YAAA,CAAa,UAAkB,GAAA,EAAqB;AAC3D,EAAA,OAAO,CAAA,EAAG,GAAG,CAAA,QAAA,EAAW,QAAQ,CAAA,CAAA;AAClC;AAMO,SAAS,oBACd,MAAA,EACsB;AACtB,EAAA,MAAM;AAAA,IACJ,KAAA;AAAA,IACA,QAAA;AAAA,IACA,GAAA,GAAM,UAAA;AAAA,IACN,SAAA,GAAY,CAAC,CAAA,KAAiB,IAAA,CAAK,UAAU,CAAC,CAAA;AAAA,IAC9C,WAAA,GAAc,CAAC,GAAA,KAAmB,IAAA,CAAK,MAAM,GAAG,CAAA;AAAA,IAChD;AAAA,GACF,GAAI,MAAA;AACJ,EAAA,MAAM,QAAA,GAAW,YAAA,CAAa,QAAA,EAAU,GAAG,CAAA;AAC3C,EAAA,MAAM,OAAA,GAAU,YAAA,CAAa,QAAA,EAAU,CAAA,EAAG,GAAG,CAAA,KAAA,CAAO,CAAA;AAEpD,EAAA,eAAe,kBAAA,GAAoC;AACjD,IAAA,MAAM,MAAA,GAAS,MAAM,KAAA,CAAM,MAAA,CAAO,OAAO,CAAA;AACzC,IAAA,IAAI,CAAC,MAAA,EAAQ;AACX,MAAA,MAAM,IAAI,KAAA,CAAM,CAAA,QAAA,EAAW,QAAQ,CAAA,QAAA,EAAW,GAAG,CAAA,gBAAA,CAAkB,CAAA;AAAA,IACrE;AAAA,EACF;AAEA,EAAA,OAAO;AAAA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AG,CAAC,MAAM,CAAC,CAAA,EAAG,MAAM,CAAA,EAAG,GAAA,CAAI,CAAC,CAAC,CAAA,CAAA,EAAI,CAAC,CAAC;AAAA,KACrE;AAAA,EACF;AAEA,EAAA,MAAM,WAAA,GAAc,CAClB,KAAA,EACA,MAAA,KAEA,6BAAA,CAA8B,EAAE,KAAA,EAAO,MAAA,EAAQ,KAAA,EAAO,KAAA,EAAO,MAAA,CAAO,KAAA,EAAO,CAAA;AAE7E,EAAA,MAAM,OAAA,GACJ,MAAA,CAAO,KAAA,IAAS,MAAA,CAAO,MAAA,GACnB,WAAA,CAAY,MAAA,CAAO,KAAA,EAAO,MAAA,CAAO,MAAM,CAAA,IACrC,MAAM;AACN,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KAEF;AAAA,EACF,CAAA,CAAA;AAEN,EAAA,OAAO;AAAA,IACL,gBAAA;AAAA,IACA,OAAA;AAAA,IACA,kBAAA,EAAoB,WAAA;AAAA,IACpB,WAAA,EAAa,CAAC,OAAA,KAAY;AAAA,GAC5B;AACF","file":"index.js","sourcesContent":["import type { ThreadManagerConfig, BaseThreadManager } from \"./types\";\n\nconst THREAD_TTL_SECONDS = 60 * 60 * 24 * 90; // 90 days\n\n/**\n * Lua script for atomic idempotent append.\n * Checks a dedup key; if it exists the message was already appended and we\n * return 0. Otherwise appends all messages to the list, sets TTL on both\n * the list and the dedup key, and returns 1.\n *\n * KEYS[1] = dedup key, KEYS[2] = list key\n * ARGV[1] = TTL seconds, ARGV[2..N] = serialised messages\n */\nconst APPEND_IDEMPOTENT_SCRIPT = `\nif redis.call('EXISTS', KEYS[1]) == 1 then\n return 0\nend\nfor i = 2, #ARGV do\n redis.call('RPUSH', KEYS[2], ARGV[i])\nend\nredis.call('EXPIRE', KEYS[2], tonumber(ARGV[1]))\nredis.call('SET', KEYS[1], '1', 'EX', tonumber(ARGV[1]))\nreturn 1\n`;\n\nfunction getThreadKey(threadId: string, key: string): string {\n return `${key}:thread:${threadId}`;\n}\n\n/**\n * Creates a generic thread manager for handling conversation state in Redis.\n * Framework-agnostic — works with any serializable message type.\n */\nexport function createThreadManager<T>(\n config: ThreadManagerConfig<T>\n): BaseThreadManager<T> {\n const {\n redis,\n threadId,\n key = \"messages\",\n serialize = (m: T): string => JSON.stringify(m),\n deserialize = (raw: string): T => JSON.parse(raw) as T,\n idOf,\n } = config;\n const redisKey = getThreadKey(threadId, key);\n const metaKey = getThreadKey(threadId, `${key}:meta`);\n\n async function assertThreadExists(): Promise<void> {\n const exists = await redis.exists(metaKey);\n if (!exists) {\n throw new Error(`Thread \"${threadId}\" (key: ${key}) does not exist`);\n }\n }\n\n return {\n async initialize(): Promise<void> {\n await redis.del(redisKey);\n await redis.set(metaKey, \"1\", \"EX\", THREAD_TTL_SECONDS);\n },\n\n async load(): Promise<T[]> {\n await assertThreadExists();\n const data = await redis.lrange(redisKey, 0, -1);\n return data.map(deserialize);\n },\n\n async append(messages: T[]): Promise<void> {\n if (messages.length === 0) return;\n await assertThreadExists();\n\n if (idOf) {\n const dedupId = messages.map(idOf).join(\":\");\n const dedupKey = getThreadKey(threadId, `dedup:${dedupId}`);\n await redis.eval(\n APPEND_IDEMPOTENT_SCRIPT,\n 2,\n dedupKey,\n redisKey,\n String(THREAD_TTL_SECONDS),\n ...messages.map(serialize)\n );\n } else {\n await redis.rpush(redisKey, ...messages.map(serialize));\n await redis.expire(redisKey, THREAD_TTL_SECONDS);\n }\n },\n\n async fork(newThreadId: string): Promise<BaseThreadManager<T>> {\n await assertThreadExists();\n const data = await redis.lrange(redisKey, 0, -1);\n const forked = createThreadManager({\n ...config,\n threadId: newThreadId,\n });\n await forked.initialize();\n if (data.length > 0) {\n const newKey = getThreadKey(newThreadId, key);\n await redis.rpush(newKey, ...data);\n await redis.expire(newKey, THREAD_TTL_SECONDS);\n }\n return forked;\n },\n\n async delete(): Promise<void> {\n await redis.del(redisKey, metaKey);\n },\n };\n}\n","import type Redis from \"ioredis\";\nimport type { Content, Part } from \"@google/genai\";\nimport { createThreadManager } from \"../../../lib/thread/manager\";\nimport type {\n ProviderThreadManager,\n ThreadManagerConfig,\n ThreadManagerHooks,\n} from \"../../../lib/thread/types\";\nimport type { GoogleGenAIToolResponse } from \"./activities\";\n\n/** SDK-native content type for Google GenAI human messages */\nexport type GoogleGenAIContent = string | Part[];\n\n/** A Content with a unique ID for idempotent Redis storage */\nexport interface StoredContent {\n id: string;\n content: Content;\n}\n\nexport type GoogleGenAIThreadManagerHooks = ThreadManagerHooks<StoredContent, Content>;\n\nexport interface GoogleGenAIThreadManagerConfig {\n redis: Redis;\n threadId: string;\n /** Thread key, defaults to 'messages' */\n key?: string;\n hooks?: GoogleGenAIThreadManagerHooks;\n}\n\n/** Prepared payload ready to send to the Google GenAI API */\nexport interface GoogleGenAIInvocationPayload {\n contents: Content[];\n systemInstruction?: string;\n}\n\n/** Thread manager with Google GenAI Content convenience helpers */\nexport interface GoogleGenAIThreadManager\n extends ProviderThreadManager<StoredContent, GoogleGenAIContent, GoogleGenAIToolResponse> {\n appendModelContent(id: string, parts: Part[]): Promise<void>;\n prepareForInvocation(): Promise<GoogleGenAIInvocationPayload>;\n}\n\nfunction storedContentId(msg: StoredContent): string {\n return msg.id;\n}\n\n/** Normalise content into Part[] */\nfunction toParts(content: GoogleGenAIContent): Part[] {\n if (typeof content === \"string\") {\n return [{ text: content }];\n }\n return content;\n}\n\n/** Convert a string or object into a Record suitable for functionResponse.response */\nfunction toFunctionResponse(content: string | Record<string, unknown>): Record<string, unknown> {\n if (typeof content === \"object\") {\n return content;\n }\n return { result: content };\n}\n\n/**\n * Merge consecutive Content objects sharing the same role.\n * The Gemini API requires alternating user/model turns; without\n * merging, multiple sequential tool-result messages would violate this.\n */\nfunction mergeConsecutiveContents(contents: Content[]): Content[] {\n const merged: Content[] = [];\n for (const content of contents) {\n const last = merged[merged.length - 1];\n if (last && last.role === content.role) {\n last.parts = [...(last.parts ?? []), ...(content.parts ?? [])];\n } else {\n merged.push({ ...content, parts: [...(content.parts ?? [])] });\n }\n }\n return merged;\n}\n\n/**\n * Creates a Google GenAI-specific thread manager that stores StoredContent\n * instances in Redis and provides convenience helpers for creating and\n * appending typed Content messages.\n */\nexport function createGoogleGenAIThreadManager(\n config: GoogleGenAIThreadManagerConfig,\n): GoogleGenAIThreadManager {\n const baseConfig: ThreadManagerConfig<StoredContent> = {\n redis: config.redis,\n threadId: config.threadId,\n key: config.key,\n idOf: storedContentId,\n };\n\n const base = createThreadManager(baseConfig);\n\n const helpers: Omit<GoogleGenAIThreadManager, keyof typeof base> = {\n async appendUserMessage(\n id: string,\n content: GoogleGenAIContent,\n ): Promise<void> {\n await base.append([{\n id,\n content: { role: \"user\", parts: toParts(content) },\n }]);\n },\n\n async appendSystemMessage(id: string, content: string): Promise<void> {\n await base.initialize();\n await base.append([{\n id,\n content: { role: \"system\", parts: [{ text: content }] },\n }]);\n },\n\n async appendModelContent(id: string, parts: Part[]): Promise<void> {\n await base.append([{\n id,\n content: { role: \"model\", parts },\n }]);\n },\n\n async appendToolResult(\n id: string,\n toolCallId: string,\n toolName: string,\n content: GoogleGenAIToolResponse,\n ): Promise<void> {\n const parts: Part[] = Array.isArray(content)\n ? content as Part[]\n : [{\n functionResponse: {\n id: toolCallId,\n name: toolName,\n response: toFunctionResponse(content),\n },\n }];\n\n await base.append([{\n id,\n content: { role: \"user\", parts },\n }]);\n },\n\n async prepareForInvocation(): Promise<GoogleGenAIInvocationPayload> {\n const stored = await base.load();\n const { onPrepareMessage, onPreparedMessage } = config.hooks ?? {};\n const mapped = onPrepareMessage\n ? stored.map((msg, i) => onPrepareMessage(msg, i, stored))\n : stored;\n\n let systemInstruction: string | undefined;\n const conversationContents: Content[] = [];\n\n for (const item of mapped) {\n if (item.content.role === \"system\") {\n systemInstruction = item.content.parts?.[0]?.text;\n } else {\n conversationContents.push(item.content);\n }\n }\n\n const contents = mergeConsecutiveContents(conversationContents);\n return {\n contents: onPreparedMessage\n ? contents.map((msg, i) => onPreparedMessage(msg, i, contents))\n : contents,\n ...(systemInstruction ? { systemInstruction } : {}),\n };\n },\n };\n\n return Object.assign(base, helpers);\n}\n","import type Redis from \"ioredis\";\nimport type { GoogleGenAI, Content, FunctionDeclaration } from \"@google/genai\";\nimport type { SerializableToolDefinition } from \"../../../lib/types\";\nimport type { AgentResponse, ModelInvokerConfig } from \"../../../lib/model\";\nimport { createGoogleGenAIThreadManager, type GoogleGenAIThreadManagerHooks } from \"./thread-manager\";\n\nexport interface GoogleGenAIModelInvokerConfig {\n redis: Redis;\n client: GoogleGenAI;\n model: string;\n hooks?: GoogleGenAIThreadManagerHooks;\n}\n\nfunction toFunctionDeclarations(\n tools: SerializableToolDefinition[],\n): FunctionDeclaration[] {\n return tools.map((t) => ({\n name: t.name,\n description: t.description,\n parametersJsonSchema: t.schema,\n }));\n}\n\n/**\n * Creates a Google GenAI model invoker that satisfies the generic\n * `ModelInvoker<Content>` contract.\n *\n * Loads the conversation thread from Redis, invokes the Gemini model via\n * `client.models.generateContent`, and returns a normalised AgentResponse.\n * The caller is responsible for appending the response to the thread.\n *\n * @example\n * ```typescript\n * import { createGoogleGenAIModelInvoker } from 'zeitlich/adapters/thread/google-genai';\n * import { createRunAgentActivity } from 'zeitlich';\n * import { GoogleGenAI } from '@google/genai';\n *\n * const client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });\n * const invoker = createGoogleGenAIModelInvoker({\n * redis,\n * client,\n * model: 'gemini-2.5-flash',\n * });\n *\n * return { runAgent: createRunAgentActivity(client, invoker) };\n * ```\n */\nexport function createGoogleGenAIModelInvoker({\n redis,\n client,\n model,\n hooks,\n}: GoogleGenAIModelInvokerConfig) {\n return async function invokeGoogleGenAIModel(\n config: ModelInvokerConfig,\n ): Promise<AgentResponse<Content>> {\n const { threadId, threadKey, state } = config;\n\n const thread = createGoogleGenAIThreadManager({ redis, threadId, key: threadKey, hooks });\n const { contents, systemInstruction } =\n await thread.prepareForInvocation();\n\n const functionDeclarations = toFunctionDeclarations(state.tools);\n const tools =\n functionDeclarations.length > 0 ? [{ functionDeclarations }] : undefined;\n\n const response = await client.models.generateContent({\n model,\n contents,\n config: {\n ...(systemInstruction ? { systemInstruction } : {}),\n ...(tools ? { tools } : {}),\n },\n });\n\n const responseParts = response.candidates?.[0]?.content?.parts ?? [];\n const modelContent: Content = { role: \"model\", parts: responseParts };\n\n const functionCalls = response.functionCalls ?? [];\n\n return {\n message: modelContent,\n rawToolCalls: functionCalls.map((fc) => ({\n id: fc.id,\n name: fc.name ?? \"\",\n args: fc.args ?? {},\n })),\n usage: {\n inputTokens: response.usageMetadata?.promptTokenCount,\n outputTokens: response.usageMetadata?.candidatesTokenCount,\n cachedReadTokens: response.usageMetadata?.cachedContentTokenCount,\n },\n };\n };\n}\n\n/**\n * Standalone function for one-shot Google GenAI model invocation.\n * Convenience wrapper around createGoogleGenAIModelInvoker for cases\n * where you don't need to reuse the invoker.\n */\nexport async function invokeGoogleGenAIModel({\n redis,\n client,\n model,\n hooks,\n config,\n}: {\n redis: Redis;\n client: GoogleGenAI;\n model: string;\n hooks?: GoogleGenAIThreadManagerHooks;\n config: ModelInvokerConfig;\n}): Promise<AgentResponse<Content>> {\n const invoker = createGoogleGenAIModelInvoker({ redis, client, model, hooks });\n return invoker(config);\n}\n","import type Redis from \"ioredis\";\nimport type { GoogleGenAI, Content, Part } from \"@google/genai\";\nimport type { ToolResultConfig } from \"../../../lib/types\";\nimport type {\n ActivityToolHandler,\n RouterContext,\n ToolHandlerResponse,\n} from \"../../../lib/tool-router/types\";\nimport type {\n ThreadOps,\n PrefixedThreadOps,\n ScopedPrefix,\n} from \"../../../lib/session/types\";\nimport type { ModelInvoker } from \"../../../lib/model\";\nimport {\n createGoogleGenAIThreadManager,\n type GoogleGenAIContent,\n type GoogleGenAIThreadManagerHooks,\n} from \"./thread-manager\";\nimport { createGoogleGenAIModelInvoker } from \"./model-invoker\";\n\nconst ADAPTER_PREFIX = \"googleGenAI\" as const;\n\nexport type GoogleGenAIThreadOps<TScope extends string = \"\"> =\n PrefixedThreadOps<\n ScopedPrefix<TScope, typeof ADAPTER_PREFIX>,\n GoogleGenAIContent\n >;\n\nexport interface GoogleGenAIAdapterConfig {\n redis: Redis;\n client?: GoogleGenAI;\n /** Default model name (e.g. 'gemini-2.5-flash'). If omitted, use `createModelInvoker()` */\n model?: string;\n hooks?: GoogleGenAIThreadManagerHooks;\n}\n\n/**\n * Tool response type accepted by the Google GenAI adapter.\n *\n * Handlers can return:\n * - **`string`** — plain text, wrapped in a `functionResponse` part.\n * - **`Record<string, unknown>`** — structured object used as `functionResponse.response`.\n * - **`Part[]`** — pre-built parts used directly as `Content.parts`.\n * The handler is responsible for building correct Part objects (e.g. `functionResponse`,\n * `inlineData`, `text`). Use `context.toolCallId` and `context.toolName` to construct\n * `functionResponse` parts.\n *\n * @example\n * ```typescript\n * adapter.wrapHandler(async (args, ctx) => ({\n * toolResponse: [\n * { functionResponse: { id: ctx.toolCallId, name: ctx.toolName, response: { result: \"done\" } } },\n * { inlineData: { data: base64, mimeType: \"image/png\" } },\n * ],\n * data: null,\n * }));\n * ```\n */\nexport type GoogleGenAIToolResponse = string | Record<string, unknown> | Part[];\n\nexport interface GoogleGenAIAdapter {\n /** Model invoker using the default model (only available when `model` was provided) */\n invoker: ModelInvoker<Content>;\n /** Create an invoker for a specific model name (for multi-model setups) */\n createModelInvoker(model: string, client: GoogleGenAI): ModelInvoker<Content>;\n /**\n * Create prefixed thread activities for registration on the worker.\n *\n * @param scope - Workflow name appended to the adapter prefix.\n * Use different scopes for the main agent vs subagents to avoid collisions.\n *\n * @example\n * ```typescript\n * adapter.createActivities(\"codingAgent\")\n * // → { googleGenAICodingAgentInitializeThread, googleGenAICodingAgentAppendHumanMessage, … }\n *\n * adapter.createActivities(\"researchAgent\")\n * // → { googleGenAIResearchAgentInitializeThread, … }\n * ```\n */\n createActivities<S extends string = \"\">(scope?: S): GoogleGenAIThreadOps<S>;\n\n /**\n * Identity wrapper that types a tool handler for this adapter.\n * Constrains `toolResponse` to {@link GoogleGenAIToolResponse}.\n */\n wrapHandler<TArgs, TResult, TContext extends RouterContext = RouterContext>(\n handler: (\n args: TArgs,\n context: TContext\n ) => Promise<ToolHandlerResponse<TResult, GoogleGenAIToolResponse>>\n ): ActivityToolHandler<TArgs, TResult, TContext, GoogleGenAIToolResponse>;\n}\n\n/**\n * Creates a Google GenAI adapter that bundles thread operations and model\n * invocation using the `@google/genai` SDK.\n *\n * Use `createActivities(scope)` to register scoped thread operations as\n * Temporal activities on the worker. The `invoker` (or invokers created via\n * `createModelInvoker`) should be wrapped with `createRunAgentActivity`.\n *\n * @example\n * ```typescript\n * import { createGoogleGenAIAdapter } from 'zeitlich/adapters/thread/google-genai';\n * import { createRunAgentActivity } from 'zeitlich';\n * import { GoogleGenAI } from '@google/genai';\n *\n * const client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });\n * const adapter = createGoogleGenAIAdapter({ redis, client, model: 'gemini-2.5-flash' });\n *\n * export function createActivities(temporalClient: WorkflowClient) {\n * return {\n * ...adapter.createActivities(\"codingAgent\"),\n * runCodingAgent: createRunAgentActivity(temporalClient, adapter.invoker),\n * };\n * }\n * ```\n *\n * @example Multi-agent worker (main + subagent share the adapter)\n * ```typescript\n * export function createActivities(temporalClient: WorkflowClient) {\n * return {\n * ...adapter.createActivities(\"codingAgent\"),\n * ...adapter.createActivities(\"researchAgent\"),\n * runCodingAgent: createRunAgentActivity(temporalClient, adapter.invoker),\n * runResearchAgent: createRunAgentActivity(\n * temporalClient,\n * adapter.createModelInvoker('gemini-2.5-pro'),\n * ),\n * };\n * }\n * ```\n */\nexport function createGoogleGenAIAdapter(\n config: GoogleGenAIAdapterConfig\n): GoogleGenAIAdapter {\n const { redis } = config;\n\n const threadOps: ThreadOps<GoogleGenAIContent> = {\n async initializeThread(\n threadId: string,\n threadKey?: string\n ): Promise<void> {\n const thread = createGoogleGenAIThreadManager({\n redis,\n threadId,\n key: threadKey,\n });\n await thread.initialize();\n },\n\n async appendHumanMessage(\n threadId: string,\n id: string,\n content: GoogleGenAIContent,\n threadKey?: string\n ): Promise<void> {\n const thread = createGoogleGenAIThreadManager({\n redis,\n threadId,\n key: threadKey,\n });\n await thread.appendUserMessage(id, content);\n },\n\n async appendSystemMessage(\n threadId: string,\n id: string,\n content: string,\n threadKey?: string\n ): Promise<void> {\n const thread = createGoogleGenAIThreadManager({\n redis,\n threadId,\n key: threadKey,\n });\n await thread.appendSystemMessage(id, content);\n },\n\n async appendToolResult(id: string, cfg: ToolResultConfig): Promise<void> {\n const { threadId, threadKey, toolCallId, toolName, content } = cfg;\n const thread = createGoogleGenAIThreadManager({\n redis,\n threadId,\n key: threadKey,\n });\n await thread.appendToolResult(\n id,\n toolCallId,\n toolName,\n content as GoogleGenAIToolResponse\n );\n },\n\n async appendAgentMessage(\n threadId: string,\n id: string,\n message: Content,\n threadKey?: string,\n ): Promise<void> {\n const thread = createGoogleGenAIThreadManager({\n redis,\n threadId,\n key: threadKey,\n });\n await thread.appendModelContent(id, message.parts ?? []);\n },\n\n async forkThread(\n sourceThreadId: string,\n targetThreadId: string,\n threadKey?: string\n ): Promise<void> {\n const thread = createGoogleGenAIThreadManager({\n redis,\n threadId: sourceThreadId,\n key: threadKey,\n });\n await thread.fork(targetThreadId);\n },\n };\n\n function createActivities<S extends string = \"\">(\n scope?: S\n ): GoogleGenAIThreadOps<S> {\n const prefix = scope\n ? `${ADAPTER_PREFIX}${scope.charAt(0).toUpperCase()}${scope.slice(1)}`\n : ADAPTER_PREFIX;\n const cap = (s: string): string => s.charAt(0).toUpperCase() + s.slice(1);\n return Object.fromEntries(\n Object.entries(threadOps).map(([k, v]) => [`${prefix}${cap(k)}`, v])\n ) as GoogleGenAIThreadOps<S>;\n }\n\n const makeInvoker = (\n model: string,\n client: GoogleGenAI\n ): ModelInvoker<Content> =>\n createGoogleGenAIModelInvoker({ redis, client, model, hooks: config.hooks });\n\n const invoker: ModelInvoker<Content> =\n config.model && config.client\n ? makeInvoker(config.model, config.client)\n : ((() => {\n throw new Error(\n \"No default model provided to createGoogleGenAIAdapter. \" +\n \"Either pass `model` in the config or use `createModelInvoker(model)` instead.\"\n );\n }) as unknown as ModelInvoker<Content>);\n\n return {\n createActivities,\n invoker,\n createModelInvoker: makeInvoker,\n wrapHandler: (handler) => handler,\n };\n}\n"]}
@@ -22,6 +22,7 @@ function createThreadOpsProxy(adapterPrefix, scope, options) {
22
22
  initializeThread: acts[p("initializeThread")],
23
23
  appendHumanMessage: acts[p("appendHumanMessage")],
24
24
  appendToolResult: acts[p("appendToolResult")],
25
+ appendAgentMessage: acts[p("appendAgentMessage")],
25
26
  appendSystemMessage: acts[p("appendSystemMessage")],
26
27
  forkThread: acts[p("forkThread")]
27
28
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/thread/proxy.ts","../../../../src/adapters/thread/google-genai/proxy.ts"],"names":["workflowInfo","proxyActivities"],"mappings":";;;;;AAwBO,SAAS,oBAAA,CACd,aAAA,EACA,KAAA,EACA,OAAA,EACiC;AACjC,EAAA,MAAM,aAAA,GAAgB,KAAA,IAASA,qBAAA,EAAa,CAAE,YAAA;AAG9C,EAAA,MAAM,IAAA,GAAOC,wBAAA;AAAA,IACX,OAAA,IAAW;AAAA,MACT,mBAAA,EAAqB,KAAA;AAAA,MACrB,KAAA,EAAO;AAAA,QACL,eAAA,EAAiB,CAAA;AAAA,QACjB,eAAA,EAAiB,IAAA;AAAA,QACjB,eAAA,EAAiB,KAAA;AAAA,QACjB,kBAAA,EAAoB;AAAA;AACtB;AACF,GACF;AAEA,EAAA,MAAM,MAAA,GACJ,CAAA,EAAG,aAAa,CAAA,EAAG,cAAc,MAAA,CAAO,CAAC,CAAA,CAAE,WAAA,EAAa,CAAA,EAAG,aAAA,CAAc,KAAA,CAAM,CAAC,CAAC,CAAA,CAAA;AACnF,EAAA,MAAM,IAAI,CAAC,GAAA,KACT,CAAA,EAAG,MAAM,GAAG,GAAA,CAAI,MAAA,CAAO,CAAC,CAAA,CAAE,aAAa,CAAA,EAAG,GAAA,CAAI,KAAA,CAAM,CAAC,CAAC,CAAA,CAAA;AAExD,EAAA,OAAO;AAAA,IACL,gBAAA,EAAkB,IAAA,CAAK,CAAA,CAAE,kBAAkB,CAAC,CAAA;AAAA,IAC5C,kBAAA,EAAoB,IAAA,CAAK,CAAA,CAAE,oBAAoB,CAAC,CAAA;AAAA,IAChD,gBAAA,EAAkB,IAAA,CAAK,CAAA,CAAE,kBAAkB,CAAC,CAAA;AAAA,IAC5C,mBAAA,EAAqB,IAAA,CAAK,CAAA,CAAE,qBAAqB,CAAC,CAAA;AAAA,IAClD,UAAA,EAAY,IAAA,CAAK,CAAA,CAAE,YAAY,CAAC;AAAA,GAClC;AACF;;;AC/BA,IAAM,cAAA,GAAiB,aAAA;AAEhB,SAAS,yBAAA,CACd,OACA,OAAA,EACqD;AACrD,EAAA,OAAO,oBAAA,CAAqB,cAAA,EAAgB,KAAA,EAAO,OAAO,CAAA;AAC5D","file":"workflow.cjs","sourcesContent":["/**\n * Shared proxy helper for thread operations.\n *\n * Each adapter re-exports a thin wrapper that supplies its prefix and\n * casts the return type to carry the adapter's native content type.\n */\nimport {\n proxyActivities,\n workflowInfo,\n type ActivityInterfaceFor,\n} from \"@temporalio/workflow\";\nimport type { ThreadOps } from \"../session/types\";\n\n/**\n * Creates a workflow-safe Temporal activity proxy for {@link ThreadOps}.\n *\n * The proxy resolves activity names by combining the adapter prefix with\n * the workflow scope, so each adapter + workflow combination gets its own\n * namespace.\n *\n * @param adapterPrefix - Adapter identifier (e.g. \"anthropic\", \"googleGenAI\", \"langChain\")\n * @param scope - Optional workflow scope override. Defaults to `workflowInfo().workflowType`.\n * @param options - Optional Temporal `proxyActivities` options.\n */\nexport function createThreadOpsProxy(\n adapterPrefix: string,\n scope?: string,\n options?: Parameters<typeof proxyActivities>[0],\n): ActivityInterfaceFor<ThreadOps> {\n const resolvedScope = scope ?? workflowInfo().workflowType;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const acts = proxyActivities<Record<string, (...args: any[]) => any>>(\n options ?? {\n startToCloseTimeout: \"10s\",\n retry: {\n maximumAttempts: 6,\n initialInterval: \"5s\",\n maximumInterval: \"15m\",\n backoffCoefficient: 4,\n },\n },\n );\n\n const prefix =\n `${adapterPrefix}${resolvedScope.charAt(0).toUpperCase()}${resolvedScope.slice(1)}`;\n const p = (key: string): string =>\n `${prefix}${key.charAt(0).toUpperCase()}${key.slice(1)}`;\n\n return {\n initializeThread: acts[p(\"initializeThread\")],\n appendHumanMessage: acts[p(\"appendHumanMessage\")],\n appendToolResult: acts[p(\"appendToolResult\")],\n appendSystemMessage: acts[p(\"appendSystemMessage\")],\n forkThread: acts[p(\"forkThread\")],\n } as ActivityInterfaceFor<ThreadOps>;\n}\n","/**\n * Workflow-safe proxy for Google GenAI thread operations.\n *\n * Import this from `zeitlich/adapters/thread/google-genai/workflow`\n * in your Temporal workflow files.\n *\n * By default the scope is derived from `workflowInfo().workflowType`,\n * so activities are automatically namespaced per workflow.\n *\n * @example\n * ```typescript\n * import { proxyGoogleGenAIThreadOps } from 'zeitlich/adapters/thread/google-genai/workflow';\n *\n * // Auto-scoped to the current workflow name\n * const threadOps = proxyGoogleGenAIThreadOps();\n *\n * // Explicit scope override\n * const threadOps = proxyGoogleGenAIThreadOps(\"customScope\");\n * ```\n */\nimport { type ActivityInterfaceFor } from \"@temporalio/workflow\";\nimport type { ThreadOps } from \"../../../lib/session/types\";\nimport type { GoogleGenAIContent } from \"./thread-manager\";\nimport { createThreadOpsProxy } from \"../../../lib/thread/proxy\";\n\nconst ADAPTER_PREFIX = \"googleGenAI\";\n\nexport function proxyGoogleGenAIThreadOps(\n scope?: string,\n options?: Parameters<typeof createThreadOpsProxy>[2],\n): ActivityInterfaceFor<ThreadOps<GoogleGenAIContent>> {\n return createThreadOpsProxy(ADAPTER_PREFIX, scope, options) as ActivityInterfaceFor<ThreadOps<GoogleGenAIContent>>;\n}\n"]}
1
+ {"version":3,"sources":["../../../../src/lib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AY,CAAC;AAAA,GAClC;AACF;;;AChCA,IAAM,cAAA,GAAiB,aAAA;AAEhB,SAAS,yBAAA,CACd,OACA,OAAA,EACqD;AACrD,EAAA,OAAO,oBAAA,CAAqB,cAAA,EAAgB,KAAA,EAAO,OAAO,CAAA;AAC5D","file":"workflow.cjs","sourcesContent":["/**\n * Shared proxy helper for thread operations.\n *\n * Each adapter re-exports a thin wrapper that supplies its prefix and\n * casts the return type to carry the adapter's native content type.\n */\nimport {\n proxyActivities,\n workflowInfo,\n type ActivityInterfaceFor,\n} from \"@temporalio/workflow\";\nimport type { ThreadOps } from \"../session/types\";\n\n/**\n * Creates a workflow-safe Temporal activity proxy for {@link ThreadOps}.\n *\n * The proxy resolves activity names by combining the adapter prefix with\n * the workflow scope, so each adapter + workflow combination gets its own\n * namespace.\n *\n * @param adapterPrefix - Adapter identifier (e.g. \"anthropic\", \"googleGenAI\", \"langChain\")\n * @param scope - Optional workflow scope override. Defaults to `workflowInfo().workflowType`.\n * @param options - Optional Temporal `proxyActivities` options.\n */\nexport function createThreadOpsProxy(\n adapterPrefix: string,\n scope?: string,\n options?: Parameters<typeof proxyActivities>[0],\n): ActivityInterfaceFor<ThreadOps> {\n const resolvedScope = scope ?? workflowInfo().workflowType;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const acts = proxyActivities<Record<string, (...args: any[]) => any>>(\n options ?? {\n startToCloseTimeout: \"10s\",\n retry: {\n maximumAttempts: 6,\n initialInterval: \"5s\",\n maximumInterval: \"15m\",\n backoffCoefficient: 4,\n },\n },\n );\n\n const prefix =\n `${adapterPrefix}${resolvedScope.charAt(0).toUpperCase()}${resolvedScope.slice(1)}`;\n const p = (key: string): string =>\n `${prefix}${key.charAt(0).toUpperCase()}${key.slice(1)}`;\n\n return {\n initializeThread: acts[p(\"initializeThread\")],\n appendHumanMessage: acts[p(\"appendHumanMessage\")],\n appendToolResult: acts[p(\"appendToolResult\")],\n appendAgentMessage: acts[p(\"appendAgentMessage\")],\n appendSystemMessage: acts[p(\"appendSystemMessage\")],\n forkThread: acts[p(\"forkThread\")],\n } as ActivityInterfaceFor<ThreadOps>;\n}\n","/**\n * Workflow-safe proxy for Google GenAI thread operations.\n *\n * Import this from `zeitlich/adapters/thread/google-genai/workflow`\n * in your Temporal workflow files.\n *\n * By default the scope is derived from `workflowInfo().workflowType`,\n * so activities are automatically namespaced per workflow.\n *\n * @example\n * ```typescript\n * import { proxyGoogleGenAIThreadOps } from 'zeitlich/adapters/thread/google-genai/workflow';\n *\n * // Auto-scoped to the current workflow name\n * const threadOps = proxyGoogleGenAIThreadOps();\n *\n * // Explicit scope override\n * const threadOps = proxyGoogleGenAIThreadOps(\"customScope\");\n * ```\n */\nimport { type ActivityInterfaceFor } from \"@temporalio/workflow\";\nimport type { ThreadOps } from \"../../../lib/session/types\";\nimport type { GoogleGenAIContent } from \"./thread-manager\";\nimport { createThreadOpsProxy } from \"../../../lib/thread/proxy\";\n\nconst ADAPTER_PREFIX = \"googleGenAI\";\n\nexport function proxyGoogleGenAIThreadOps(\n scope?: string,\n options?: Parameters<typeof createThreadOpsProxy>[2],\n): ActivityInterfaceFor<ThreadOps<GoogleGenAIContent>> {\n return createThreadOpsProxy(ADAPTER_PREFIX, scope, options) as ActivityInterfaceFor<ThreadOps<GoogleGenAIContent>>;\n}\n"]}
@@ -1,14 +1,14 @@
1
1
  import { ActivityInterfaceFor } from '@temporalio/workflow';
2
- import { T as ThreadOps } from '../../../types-CjeGWQm1.cjs';
3
- import { c as GoogleGenAIContent } from '../../../activities-qPkJDAiq.cjs';
4
- import { c as createThreadOpsProxy } from '../../../proxy-BDQ3Rj6R.cjs';
2
+ import { T as ThreadOps } from '../../../types-Bpq5fDI5.cjs';
3
+ import { c as GoogleGenAIContent } from '../../../activities-DA-bQM12.cjs';
4
+ import { c as createThreadOpsProxy } from '../../../proxy-CTCYWjkr.cjs';
5
5
  import '@temporalio/common';
6
6
  import '@temporalio/common/lib/interfaces';
7
7
  import 'zod';
8
8
  import '../../../types-AujBIMMn.cjs';
9
9
  import 'ioredis';
10
10
  import '@google/genai';
11
- import '../../../types-e_38QaKo.cjs';
11
+ import '../../../types-NJDyMyUx.cjs';
12
12
 
13
13
  /**
14
14
  * Workflow-safe proxy for Google GenAI thread operations.
@@ -1,14 +1,14 @@
1
1
  import { ActivityInterfaceFor } from '@temporalio/workflow';
2
- import { T as ThreadOps } from '../../../types-BmS-Huc0.js';
3
- import { c as GoogleGenAIContent } from '../../../activities-DRSdt8Y3.js';
4
- import { c as createThreadOpsProxy } from '../../../proxy-BkvkV2oU.js';
2
+ import { T as ThreadOps } from '../../../types-CheCTLeV.js';
3
+ import { c as GoogleGenAIContent } from '../../../activities-FIXVz7DT.js';
4
+ import { c as createThreadOpsProxy } from '../../../proxy-Br4unLTC.js';
5
5
  import '@temporalio/common';
6
6
  import '@temporalio/common/lib/interfaces';
7
7
  import 'zod';
8
8
  import '../../../types-AujBIMMn.js';
9
9
  import 'ioredis';
10
10
  import '@google/genai';
11
- import '../../../types-D6UKZZtj.js';
11
+ import '../../../types-BxiT8w9d.js';
12
12
 
13
13
  /**
14
14
  * Workflow-safe proxy for Google GenAI thread operations.
@@ -20,6 +20,7 @@ function createThreadOpsProxy(adapterPrefix, scope, options) {
20
20
  initializeThread: acts[p("initializeThread")],
21
21
  appendHumanMessage: acts[p("appendHumanMessage")],
22
22
  appendToolResult: acts[p("appendToolResult")],
23
+ appendAgentMessage: acts[p("appendAgentMessage")],
23
24
  appendSystemMessage: acts[p("appendSystemMessage")],
24
25
  forkThread: acts[p("forkThread")]
25
26
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/thread/proxy.ts","../../../../src/adapters/thread/google-genai/proxy.ts"],"names":[],"mappings":";;;AAwBO,SAAS,oBAAA,CACd,aAAA,EACA,KAAA,EACA,OAAA,EACiC;AACjC,EAAA,MAAM,aAAA,GAAgB,KAAA,IAAS,YAAA,EAAa,CAAE,YAAA;AAG9C,EAAA,MAAM,IAAA,GAAO,eAAA;AAAA,IACX,OAAA,IAAW;AAAA,MACT,mBAAA,EAAqB,KAAA;AAAA,MACrB,KAAA,EAAO;AAAA,QACL,eAAA,EAAiB,CAAA;AAAA,QACjB,eAAA,EAAiB,IAAA;AAAA,QACjB,eAAA,EAAiB,KAAA;AAAA,QACjB,kBAAA,EAAoB;AAAA;AACtB;AACF,GACF;AAEA,EAAA,MAAM,MAAA,GACJ,CAAA,EAAG,aAAa,CAAA,EAAG,cAAc,MAAA,CAAO,CAAC,CAAA,CAAE,WAAA,EAAa,CAAA,EAAG,aAAA,CAAc,KAAA,CAAM,CAAC,CAAC,CAAA,CAAA;AACnF,EAAA,MAAM,IAAI,CAAC,GAAA,KACT,CAAA,EAAG,MAAM,GAAG,GAAA,CAAI,MAAA,CAAO,CAAC,CAAA,CAAE,aAAa,CAAA,EAAG,GAAA,CAAI,KAAA,CAAM,CAAC,CAAC,CAAA,CAAA;AAExD,EAAA,OAAO;AAAA,IACL,gBAAA,EAAkB,IAAA,CAAK,CAAA,CAAE,kBAAkB,CAAC,CAAA;AAAA,IAC5C,kBAAA,EAAoB,IAAA,CAAK,CAAA,CAAE,oBAAoB,CAAC,CAAA;AAAA,IAChD,gBAAA,EAAkB,IAAA,CAAK,CAAA,CAAE,kBAAkB,CAAC,CAAA;AAAA,IAC5C,mBAAA,EAAqB,IAAA,CAAK,CAAA,CAAE,qBAAqB,CAAC,CAAA;AAAA,IAClD,UAAA,EAAY,IAAA,CAAK,CAAA,CAAE,YAAY,CAAC;AAAA,GAClC;AACF;;;AC/BA,IAAM,cAAA,GAAiB,aAAA;AAEhB,SAAS,yBAAA,CACd,OACA,OAAA,EACqD;AACrD,EAAA,OAAO,oBAAA,CAAqB,cAAA,EAAgB,KAAA,EAAO,OAAO,CAAA;AAC5D","file":"workflow.js","sourcesContent":["/**\n * Shared proxy helper for thread operations.\n *\n * Each adapter re-exports a thin wrapper that supplies its prefix and\n * casts the return type to carry the adapter's native content type.\n */\nimport {\n proxyActivities,\n workflowInfo,\n type ActivityInterfaceFor,\n} from \"@temporalio/workflow\";\nimport type { ThreadOps } from \"../session/types\";\n\n/**\n * Creates a workflow-safe Temporal activity proxy for {@link ThreadOps}.\n *\n * The proxy resolves activity names by combining the adapter prefix with\n * the workflow scope, so each adapter + workflow combination gets its own\n * namespace.\n *\n * @param adapterPrefix - Adapter identifier (e.g. \"anthropic\", \"googleGenAI\", \"langChain\")\n * @param scope - Optional workflow scope override. Defaults to `workflowInfo().workflowType`.\n * @param options - Optional Temporal `proxyActivities` options.\n */\nexport function createThreadOpsProxy(\n adapterPrefix: string,\n scope?: string,\n options?: Parameters<typeof proxyActivities>[0],\n): ActivityInterfaceFor<ThreadOps> {\n const resolvedScope = scope ?? workflowInfo().workflowType;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const acts = proxyActivities<Record<string, (...args: any[]) => any>>(\n options ?? {\n startToCloseTimeout: \"10s\",\n retry: {\n maximumAttempts: 6,\n initialInterval: \"5s\",\n maximumInterval: \"15m\",\n backoffCoefficient: 4,\n },\n },\n );\n\n const prefix =\n `${adapterPrefix}${resolvedScope.charAt(0).toUpperCase()}${resolvedScope.slice(1)}`;\n const p = (key: string): string =>\n `${prefix}${key.charAt(0).toUpperCase()}${key.slice(1)}`;\n\n return {\n initializeThread: acts[p(\"initializeThread\")],\n appendHumanMessage: acts[p(\"appendHumanMessage\")],\n appendToolResult: acts[p(\"appendToolResult\")],\n appendSystemMessage: acts[p(\"appendSystemMessage\")],\n forkThread: acts[p(\"forkThread\")],\n } as ActivityInterfaceFor<ThreadOps>;\n}\n","/**\n * Workflow-safe proxy for Google GenAI thread operations.\n *\n * Import this from `zeitlich/adapters/thread/google-genai/workflow`\n * in your Temporal workflow files.\n *\n * By default the scope is derived from `workflowInfo().workflowType`,\n * so activities are automatically namespaced per workflow.\n *\n * @example\n * ```typescript\n * import { proxyGoogleGenAIThreadOps } from 'zeitlich/adapters/thread/google-genai/workflow';\n *\n * // Auto-scoped to the current workflow name\n * const threadOps = proxyGoogleGenAIThreadOps();\n *\n * // Explicit scope override\n * const threadOps = proxyGoogleGenAIThreadOps(\"customScope\");\n * ```\n */\nimport { type ActivityInterfaceFor } from \"@temporalio/workflow\";\nimport type { ThreadOps } from \"../../../lib/session/types\";\nimport type { GoogleGenAIContent } from \"./thread-manager\";\nimport { createThreadOpsProxy } from \"../../../lib/thread/proxy\";\n\nconst ADAPTER_PREFIX = \"googleGenAI\";\n\nexport function proxyGoogleGenAIThreadOps(\n scope?: string,\n options?: Parameters<typeof createThreadOpsProxy>[2],\n): ActivityInterfaceFor<ThreadOps<GoogleGenAIContent>> {\n return createThreadOpsProxy(ADAPTER_PREFIX, scope, options) as ActivityInterfaceFor<ThreadOps<GoogleGenAIContent>>;\n}\n"]}
1
+ {"version":3,"sources":["../../../../src/lib/thread/proxy.ts","../../../../src/adapters/thread/google-genai/proxy.ts"],"names":[],"mappings":";;;AAwBO,SAAS,oBAAA,CACd,aAAA,EACA,KAAA,EACA,OAAA,EACiC;AACjC,EAAA,MAAM,aAAA,GAAgB,KAAA,IAAS,YAAA,EAAa,CAAE,YAAA;AAG9C,EAAA,MAAM,IAAA,GAAO,eAAA;AAAA,IACX,OAAA,IAAW;AAAA,MACT,mBAAA,EAAqB,KAAA;AAAA,MACrB,KAAA,EAAO;AAAA,QACL,eAAA,EAAiB,CAAA;AAAA,QACjB,eAAA,EAAiB,IAAA;AAAA,QACjB,eAAA,EAAiB,KAAA;AAAA,QACjB,kBAAA,EAAoB;AAAA;AACtB;AACF,GACF;AAEA,EAAA,MAAM,MAAA,GACJ,CAAA,EAAG,aAAa,CAAA,EAAG,cAAc,MAAA,CAAO,CAAC,CAAA,CAAE,WAAA,EAAa,CAAA,EAAG,aAAA,CAAc,KAAA,CAAM,CAAC,CAAC,CAAA,CAAA;AACnF,EAAA,MAAM,IAAI,CAAC,GAAA,KACT,CAAA,EAAG,MAAM,GAAG,GAAA,CAAI,MAAA,CAAO,CAAC,CAAA,CAAE,aAAa,CAAA,EAAG,GAAA,CAAI,KAAA,CAAM,CAAC,CAAC,CAAA,CAAA;AAExD,EAAA,OAAO;AAAA,IACL,gBAAA,EAAkB,IAAA,CAAK,CAAA,CAAE,kBAAkB,CAAC,CAAA;AAAA,IAC5C,kBAAA,EAAoB,IAAA,CAAK,CAAA,CAAE,oBAAoB,CAAC,CAAA;AAAA,IAChD,gBAAA,EAAkB,IAAA,CAAK,CAAA,CAAE,kBAAkB,CAAC,CAAA;AAAA,IAC5C,kBAAA,EAAoB,IAAA,CAAK,CAAA,CAAE,oBAAoB,CAAC,CAAA;AAAA,IAChD,mBAAA,EAAqB,IAAA,CAAK,CAAA,CAAE,qBAAqB,CAAC,CAAA;AAAA,IAClD,UAAA,EAAY,IAAA,CAAK,CAAA,CAAE,YAAY,CAAC;AAAA,GAClC;AACF;;;AChCA,IAAM,cAAA,GAAiB,aAAA;AAEhB,SAAS,yBAAA,CACd,OACA,OAAA,EACqD;AACrD,EAAA,OAAO,oBAAA,CAAqB,cAAA,EAAgB,KAAA,EAAO,OAAO,CAAA;AAC5D","file":"workflow.js","sourcesContent":["/**\n * Shared proxy helper for thread operations.\n *\n * Each adapter re-exports a thin wrapper that supplies its prefix and\n * casts the return type to carry the adapter's native content type.\n */\nimport {\n proxyActivities,\n workflowInfo,\n type ActivityInterfaceFor,\n} from \"@temporalio/workflow\";\nimport type { ThreadOps } from \"../session/types\";\n\n/**\n * Creates a workflow-safe Temporal activity proxy for {@link ThreadOps}.\n *\n * The proxy resolves activity names by combining the adapter prefix with\n * the workflow scope, so each adapter + workflow combination gets its own\n * namespace.\n *\n * @param adapterPrefix - Adapter identifier (e.g. \"anthropic\", \"googleGenAI\", \"langChain\")\n * @param scope - Optional workflow scope override. Defaults to `workflowInfo().workflowType`.\n * @param options - Optional Temporal `proxyActivities` options.\n */\nexport function createThreadOpsProxy(\n adapterPrefix: string,\n scope?: string,\n options?: Parameters<typeof proxyActivities>[0],\n): ActivityInterfaceFor<ThreadOps> {\n const resolvedScope = scope ?? workflowInfo().workflowType;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const acts = proxyActivities<Record<string, (...args: any[]) => any>>(\n options ?? {\n startToCloseTimeout: \"10s\",\n retry: {\n maximumAttempts: 6,\n initialInterval: \"5s\",\n maximumInterval: \"15m\",\n backoffCoefficient: 4,\n },\n },\n );\n\n const prefix =\n `${adapterPrefix}${resolvedScope.charAt(0).toUpperCase()}${resolvedScope.slice(1)}`;\n const p = (key: string): string =>\n `${prefix}${key.charAt(0).toUpperCase()}${key.slice(1)}`;\n\n return {\n initializeThread: acts[p(\"initializeThread\")],\n appendHumanMessage: acts[p(\"appendHumanMessage\")],\n appendToolResult: acts[p(\"appendToolResult\")],\n appendAgentMessage: acts[p(\"appendAgentMessage\")],\n appendSystemMessage: acts[p(\"appendSystemMessage\")],\n forkThread: acts[p(\"forkThread\")],\n } as ActivityInterfaceFor<ThreadOps>;\n}\n","/**\n * Workflow-safe proxy for Google GenAI thread operations.\n *\n * Import this from `zeitlich/adapters/thread/google-genai/workflow`\n * in your Temporal workflow files.\n *\n * By default the scope is derived from `workflowInfo().workflowType`,\n * so activities are automatically namespaced per workflow.\n *\n * @example\n * ```typescript\n * import { proxyGoogleGenAIThreadOps } from 'zeitlich/adapters/thread/google-genai/workflow';\n *\n * // Auto-scoped to the current workflow name\n * const threadOps = proxyGoogleGenAIThreadOps();\n *\n * // Explicit scope override\n * const threadOps = proxyGoogleGenAIThreadOps(\"customScope\");\n * ```\n */\nimport { type ActivityInterfaceFor } from \"@temporalio/workflow\";\nimport type { ThreadOps } from \"../../../lib/session/types\";\nimport type { GoogleGenAIContent } from \"./thread-manager\";\nimport { createThreadOpsProxy } from \"../../../lib/thread/proxy\";\n\nconst ADAPTER_PREFIX = \"googleGenAI\";\n\nexport function proxyGoogleGenAIThreadOps(\n scope?: string,\n options?: Parameters<typeof createThreadOpsProxy>[2],\n): ActivityInterfaceFor<ThreadOps<GoogleGenAIContent>> {\n return createThreadOpsProxy(ADAPTER_PREFIX, scope, options) as ActivityInterfaceFor<ThreadOps<GoogleGenAIContent>>;\n}\n"]}
@@ -202,7 +202,6 @@ function createLangChainModelInvoker({ redis, model, hooks }) {
202
202
  tools: state.tools
203
203
  }
204
204
  );
205
- await thread.append([response.toDict()]);
206
205
  const toolCalls = response.tool_calls ?? [];
207
206
  return {
208
207
  message: response.toDict(),
@@ -253,6 +252,11 @@ function createLangChainAdapter(config) {
253
252
  const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });
254
253
  await thread.appendToolResult(id, toolCallId, "", content);
255
254
  },
255
+ async appendAgentMessage(threadId, id, message, threadKey) {
256
+ const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });
257
+ const patched = { ...message, data: { ...message.data, id } };
258
+ await thread.append([patched]);
259
+ },
256
260
  async forkThread(sourceThreadId, targetThreadId, threadKey) {
257
261
  const thread = createLangChainThreadManager({
258
262
  redis,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/thread/manager.ts","../../../../src/adapters/thread/langchain/thread-manager.ts","../../../../node_modules/uuid/dist/esm/stringify.js","../../../../node_modules/uuid/dist/esm/rng.js","../../../../node_modules/uuid/dist/esm/native.js","../../../../node_modules/uuid/dist/esm/v4.js","../../../../src/adapters/thread/langchain/model-invoker.ts","../../../../src/adapters/thread/langchain/activities.ts","../../../../src/adapters/thread/langchain/hooks.ts"],"names":["HumanMessage","SystemMessage","AIMessage","ToolMessage","messages","mapStoredMessagesToChatMessages","randomFillSync","randomUUID","invokeLangChainModel","content"],"mappings":";;;;;;;;AAEA,IAAM,kBAAA,GAAqB,EAAA,GAAK,EAAA,GAAK,EAAA,GAAK,EAAA;AAW1C,IAAM,wBAAA,GAA2B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,CAAA;AAYjC,SAAS,YAAA,CAAa,UAAkB,GAAA,EAAqB;AAC3D,EAAA,OAAO,CAAA,EAAG,GAAG,CAAA,QAAA,EAAW,QAAQ,CAAA,C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**\n * Lua script for atomic idempotent append.\n * Checks a dedup key; if it exists the message was already appended and we\n * return 0. Otherwise appends all messages to the list, sets TTL on both\n * the list and the dedup key, and returns 1.\n *\n * KEYS[1] = dedup key, KEYS[2] = list key\n * ARGV[1] = TTL seconds, ARGV[2..N] = serialised messages\n */\nconst APPEND_IDEMPOTENT_SCRIPT = `\nif redis.call('EXISTS', KEYS[1]) == 1 then\n return 0\nend\nfor i = 2, #ARGV do\n redis.call('RPUSH', KEYS[2], ARGV[i])\nend\nredis.call('EXPIRE', KEYS[2], tonumber(ARGV[1]))\nredis.call('SET', KEYS[1], '1', 'EX', tonumber(ARGV[1]))\nreturn 1\n`;\n\nfunction getThreadKey(threadId: string, key: string): string {\n return `${key}:thread:${threadId}`;\n}\n\n/**\n * Creates a generic thread manager for handling conversation state in Redis.\n * Framework-agnostic — works with any serializable message type.\n */\nexport function createThreadManager<T>(\n config: ThreadManagerConfig<T>\n): BaseThreadManager<T> {\n const {\n redis,\n threadId,\n key = \"messages\",\n serialize = (m: T): string => JSON.stringify(m),\n deserialize = (raw: string): T => JSON.parse(raw) as T,\n idOf,\n } = config;\n const redisKey = getThreadKey(threadId, key);\n const metaKey = getThreadKey(threadId, `${key}:meta`);\n\n async function assertThreadExists(): Promise<void> {\n const exists = await redis.exists(metaKey);\n if (!exists) {\n throw new Error(`Thread \"${threadId}\" (key: ${key}) does not exist`);\n }\n }\n\n return {\n async initialize(): Promise<void> {\n await redis.del(redisKey);\n await redis.set(metaKey, \"1\", \"EX\", THREAD_TTL_SECONDS);\n },\n\n async load(): Promise<T[]> {\n await assertThreadExists();\n const data = await redis.lrange(redisKey, 0, -1);\n return data.map(deserialize);\n },\n\n async append(messages: T[]): Promise<void> {\n if (messages.length === 0) return;\n await assertThreadExists();\n\n if (idOf) {\n const dedupId = messages.map(idOf).join(\":\");\n const dedupKey = getThreadKey(threadId, `dedup:${dedupId}`);\n await redis.eval(\n APPEND_IDEMPOTENT_SCRIPT,\n 2,\n dedupKey,\n redisKey,\n String(THREAD_TTL_SECONDS),\n ...messages.map(serialize)\n );\n } else {\n await redis.rpush(redisKey, ...messages.map(serialize));\n await redis.expire(redisKey, THREAD_TTL_SECONDS);\n }\n },\n\n async fork(newThreadId: string): Promise<BaseThreadManager<T>> {\n await assertThreadExists();\n const data = await redis.lrange(redisKey, 0, -1);\n const forked = createThreadManager({\n ...config,\n threadId: newThreadId,\n });\n await forked.initialize();\n if (data.length > 0) {\n const newKey = getThreadKey(newThreadId, key);\n await redis.rpush(newKey, ...data);\n await redis.expire(newKey, THREAD_TTL_SECONDS);\n }\n return forked;\n },\n\n async delete(): Promise<void> {\n await redis.del(redisKey, metaKey);\n },\n };\n}\n","import type Redis from \"ioredis\";\nimport type { JsonValue } from \"../../../lib/state/types\";\nimport {\n AIMessage,\n type BaseMessage,\n HumanMessage,\n type MessageContent,\n type StoredMessage,\n SystemMessage,\n ToolMessage,\n mapStoredMessagesToChatMessages,\n} from \"@langchain/core/messages\";\nimport { createThreadManager } from \"../../../lib/thread/manager\";\nimport type {\n ProviderThreadManager,\n ThreadManagerConfig,\n ThreadManagerHooks,\n} from \"../../../lib/thread/types\";\n\n/** SDK-native content type for LangChain human messages */\nexport type LangChainContent = string | MessageContent;\n\nexport type LangChainThreadManagerHooks = ThreadManagerHooks<StoredMessage, BaseMessage>;\n\nexport interface LangChainThreadManagerConfig {\n redis: Redis;\n threadId: string;\n /** Thread key, defaults to 'messages' */\n key?: string;\n hooks?: LangChainThreadManagerHooks;\n}\n\n/** Prepared payload ready to send to a LangChain chat model */\nexport interface LangChainInvocationPayload {\n messages: BaseMessage[];\n}\n\n/** Thread manager with LangChain StoredMessage convenience helpers */\nexport interface LangChainThreadManager\n extends ProviderThreadManager<StoredMessage, LangChainContent> {\n appendAIMessage(id: string, content: string | MessageContent): Promise<void>;\n prepareForInvocation(): Promise<LangChainInvocationPayload>;\n}\n\nfunction storedMessageId(msg: StoredMessage): string {\n if (msg.type === \"tool\" && msg.data.tool_call_id) {\n return msg.data.tool_call_id;\n }\n\n if (msg.data.id) {\n return msg.data.id;\n }\n\n throw new Error(\"No id found for message\");\n}\n\n/**\n * Creates a LangChain-specific thread manager that stores StoredMessage\n * instances in Redis and provides convenience helpers for creating and\n * appending typed LangChain messages.\n */\nexport function createLangChainThreadManager(\n config: LangChainThreadManagerConfig,\n): LangChainThreadManager {\n const baseConfig: ThreadManagerConfig<StoredMessage> = {\n redis: config.redis,\n threadId: config.threadId,\n key: config.key,\n idOf: storedMessageId,\n };\n\n const base = createThreadManager(baseConfig);\n\n const helpers: Omit<LangChainThreadManager, keyof typeof base> = {\n async appendUserMessage(\n id: string,\n content: LangChainContent,\n ): Promise<void> {\n await base.append([\n new HumanMessage({ id, content: content as MessageContent }).toDict(),\n ]);\n },\n\n async appendSystemMessage(id: string, content: string): Promise<void> {\n await base.initialize();\n await base.append([\n new SystemMessage({ id, content }).toDict(),\n ]);\n },\n\n async appendAIMessage(\n id: string,\n content: string | MessageContent,\n ): Promise<void> {\n await base.append([\n new AIMessage({ id, content: content as MessageContent }).toDict(),\n ]);\n },\n\n async appendToolResult(\n id: string,\n _toolCallId: string,\n _toolName: string,\n content: JsonValue,\n ): Promise<void> {\n await base.append([\n new ToolMessage({ id, content: content as MessageContent, tool_call_id: _toolCallId }).toDict(),\n ]);\n },\n\n async prepareForInvocation(): Promise<LangChainInvocationPayload> {\n const stored = await base.load();\n const { onPrepareMessage, onPreparedMessage } = config.hooks ?? {};\n const mapped = onPrepareMessage\n ? stored.map((msg, i) => onPrepareMessage(msg, i, stored))\n : stored;\n const messages = mapStoredMessagesToChatMessages(mapped);\n return {\n messages: onPreparedMessage\n ? messages.map((msg, i) => onPreparedMessage(msg, i, messages))\n : messages,\n };\n },\n };\n\n return Object.assign(base, helpers);\n}\n","import validate from './validate.js';\nconst byteToHex = [];\nfor (let i = 0; i < 256; ++i) {\n byteToHex.push((i + 0x100).toString(16).slice(1));\n}\nexport function unsafeStringify(arr, offset = 0) {\n return (byteToHex[arr[offset + 0]] +\n byteToHex[arr[offset + 1]] +\n byteToHex[arr[offset + 2]] +\n byteToHex[arr[offset + 3]] +\n '-' +\n byteToHex[arr[offset + 4]] +\n byteToHex[arr[offset + 5]] +\n '-' +\n byteToHex[arr[offset + 6]] +\n byteToHex[arr[offset + 7]] +\n '-' +\n byteToHex[arr[offset + 8]] +\n byteToHex[arr[offset + 9]] +\n '-' +\n byteToHex[arr[offset + 10]] +\n byteToHex[arr[offset + 11]] +\n byteToHex[arr[offset + 12]] +\n byteToHex[arr[offset + 13]] +\n byteToHex[arr[offset + 14]] +\n byteToHex[arr[offset + 15]]).toLowerCase();\n}\nfunction stringify(arr, offset = 0) {\n const uuid = unsafeStringify(arr, offset);\n if (!validate(uuid)) {\n throw TypeError('Stringified UUID is invalid');\n }\n return uuid;\n}\nexport default stringify;\n","import { randomFillSync } from 'crypto';\nconst rnds8Pool = new Uint8Array(256);\nlet poolPtr = rnds8Pool.length;\nexport default function rng() {\n if (poolPtr > rnds8Pool.length - 16) {\n randomFillSync(rnds8Pool);\n poolPtr = 0;\n }\n return rnds8Pool.slice(poolPtr, (poolPtr += 16));\n}\n","import { randomUUID } from 'crypto';\nexport default { randomUUID };\n","import native from './native.js';\nimport rng from './rng.js';\nimport { unsafeStringify } from './stringify.js';\nfunction v4(options, buf, offset) {\n if (native.randomUUID && !buf && !options) {\n return native.randomUUID();\n }\n options = options || {};\n const rnds = options.random ?? options.rng?.() ?? rng();\n if (rnds.length < 16) {\n throw new Error('Random bytes length must be >= 16');\n }\n rnds[6] = (rnds[6] & 0x0f) | 0x40;\n rnds[8] = (rnds[8] & 0x3f) | 0x80;\n if (buf) {\n offset = offset || 0;\n if (offset < 0 || offset + 16 > buf.length) {\n throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);\n }\n for (let i = 0; i < 16; ++i) {\n buf[offset + i] = rnds[i];\n }\n return buf;\n }\n return unsafeStringify(rnds);\n}\nexport default v4;\n","import type Redis from \"ioredis\";\nimport type { AgentResponse, ModelInvokerConfig } from \"../../../lib/model\";\nimport type { StoredMessage } from \"@langchain/core/messages\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport type { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport { createLangChainThreadManager, type LangChainThreadManagerHooks } from \"./thread-manager\";\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface LangChainModelInvokerConfig<TModel extends BaseChatModel<any> = BaseChatModel<any>> {\n redis: Redis;\n model: TModel;\n hooks?: LangChainThreadManagerHooks;\n}\n\n/**\n * Creates a LangChain-based model invoker that satisfies the generic\n * `ModelInvoker<StoredMessage>` contract.\n *\n * Loads the conversation thread from Redis, invokes a LangChain chat model,\n * appends the AI response, and returns a normalised AgentResponse.\n *\n * @example\n * ```typescript\n * import { createLangChainModelInvoker } from 'zeitlich/adapters/thread/langchain';\n * import { createRunAgentActivity } from 'zeitlich';\n * import { ChatAnthropic } from '@langchain/anthropic';\n *\n * const model = new ChatAnthropic({ model: \"claude-sonnet-4-6\" });\n * const invoker = createLangChainModelInvoker({ redis, model });\n *\n * return { runAgent: createRunAgentActivity(client, invoker) };\n * ```\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function createLangChainModelInvoker<TModel extends BaseChatModel<any> = BaseChatModel<any>>(\n { redis, model, hooks }: LangChainModelInvokerConfig<TModel>,\n) {\n return async function invokeLangChainModel(\n config: ModelInvokerConfig,\n ): Promise<AgentResponse<StoredMessage>> {\n const { threadId, threadKey, agentName, state, metadata } = config;\n\n const thread = createLangChainThreadManager({ redis, threadId, key: threadKey, hooks });\n const runId = uuidv4();\n\n const { messages } = await thread.prepareForInvocation();\n const response = await model.invoke(\n messages,\n {\n runName: agentName,\n runId,\n metadata: { thread_id: `${agentName}-${threadId}`, ...metadata },\n tools: state.tools,\n },\n );\n\n await thread.append([response.toDict()]);\n\n const toolCalls = response.tool_calls ?? [];\n\n return {\n message: response.toDict(),\n rawToolCalls: toolCalls.map((tc) => ({\n id: tc.id,\n name: tc.name,\n args: tc.args,\n })),\n usage: {\n inputTokens: response.usage_metadata?.input_tokens,\n outputTokens: response.usage_metadata?.output_tokens,\n reasonTokens: response.usage_metadata?.output_token_details?.reasoning,\n cachedWriteTokens:\n response.usage_metadata?.input_token_details?.cache_creation,\n cachedReadTokens:\n response.usage_metadata?.input_token_details?.cache_read,\n },\n };\n };\n}\n\n/**\n * Standalone function for one-shot LangChain model invocation.\n * Convenience wrapper around createLangChainModelInvoker for cases where\n * you don't need to reuse the invoker.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport async function invokeLangChainModel<TModel extends BaseChatModel<any> = BaseChatModel<any>>({\n redis,\n model,\n hooks,\n config,\n}: {\n redis: Redis;\n config: ModelInvokerConfig;\n model: TModel;\n hooks?: LangChainThreadManagerHooks;\n}): Promise<AgentResponse<StoredMessage>> {\n const invoker = createLangChainModelInvoker({ redis, model, hooks });\n return invoker(config);\n}\n","import type Redis from \"ioredis\";\nimport type { ToolResultConfig } from \"../../../lib/types\";\nimport type { MessageContent } from \"@langchain/core/messages\";\nimport type {\n ActivityToolHandler,\n RouterContext,\n ToolHandlerResponse,\n} from \"../../../lib/tool-router/types\";\nimport type {\n ThreadOps,\n PrefixedThreadOps,\n ScopedPrefix,\n} from \"../../../lib/session/types\";\nimport type { ModelInvoker } from \"../../../lib/model\";\nimport type { StoredMessage } from \"@langchain/core/messages\";\nimport type { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport {\n createLangChainThreadManager,\n type LangChainContent,\n type LangChainThreadManagerHooks,\n} from \"./thread-manager\";\nimport { createLangChainModelInvoker } from \"./model-invoker\";\n\nconst ADAPTER_PREFIX = \"langChain\" as const;\n\nexport type LangChainThreadOps<TScope extends string = \"\"> =\n PrefixedThreadOps<ScopedPrefix<TScope, typeof ADAPTER_PREFIX>, LangChainContent>;\n\nexport interface LangChainAdapterConfig {\n redis: Redis;\n /** Optional default model — if omitted, use `createModelInvoker()` to create invokers later */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n model?: BaseChatModel<any>;\n hooks?: LangChainThreadManagerHooks;\n}\n\n/**\n * Tool response type accepted by the LangChain adapter.\n *\n * Content is passed directly to `ToolMessage` as `MessageContent`.\n * Handlers can return a string or an array of content blocks\n * (e.g. `{ type: \"text\", text: \"...\" }`, `{ type: \"image_url\", image_url: { ... } }`).\n */\nexport type LangChainToolResponse = MessageContent;\n\nexport interface LangChainAdapter {\n /** Model invoker using the default model (only available when `model` was provided) */\n invoker: ModelInvoker<StoredMessage>;\n /** Create an invoker for a specific model (for multi-model setups) */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n createModelInvoker(model: BaseChatModel<any>): ModelInvoker<StoredMessage>;\n /**\n * Create prefixed thread activities for registration on the worker.\n *\n * @param scope - Workflow name appended to the adapter prefix.\n *\n * @example\n * ```typescript\n * adapter.createActivities(\"codingAgent\")\n * // → { langChainCodingAgentInitializeThread, langChainCodingAgentAppendHumanMessage, … }\n * ```\n */\n createActivities<S extends string = \"\">(\n scope?: S,\n ): LangChainThreadOps<S>;\n\n /**\n * Identity wrapper that types a tool handler for this adapter.\n * Constrains `toolResponse` to {@link LangChainToolResponse}.\n */\n wrapHandler<TArgs, TResult, TContext extends RouterContext = RouterContext>(\n handler: (\n args: TArgs,\n context: TContext,\n ) => Promise<ToolHandlerResponse<TResult, LangChainToolResponse>>,\n ): ActivityToolHandler<TArgs, TResult, TContext, LangChainToolResponse>;\n}\n\n/**\n * Creates a LangChain adapter that bundles thread operations and model\n * invocation using a consistent message format (StoredMessage).\n *\n * Use `createActivities(scope)` to register scoped thread operations as\n * Temporal activities on the worker. The `invoker` (or invokers created via\n * `createModelInvoker`) should be wrapped with `createRunAgentActivity`.\n *\n * @example\n * ```typescript\n * import { createLangChainAdapter } from 'zeitlich/adapters/thread/langchain';\n * import { createRunAgentActivity } from 'zeitlich';\n *\n * const adapter = createLangChainAdapter({ redis, model });\n *\n * export function createActivities(client: WorkflowClient) {\n * return {\n * ...adapter.createActivities(\"codingAgent\"),\n * runCodingAgent: createRunAgentActivity(client, adapter.invoker),\n * };\n * }\n * ```\n *\n * @example Multi-agent worker\n * ```typescript\n * export function createActivities(client: WorkflowClient) {\n * return {\n * ...adapter.createActivities(\"codingAgent\"),\n * ...adapter.createActivities(\"researchAgent\"),\n * runCodingAgent: createRunAgentActivity(client, adapter.invoker),\n * runResearchAgent: createRunAgentActivity(client, adapter.createModelInvoker(claude)),\n * };\n * }\n * ```\n */\nexport function createLangChainAdapter(\n config: LangChainAdapterConfig,\n): LangChainAdapter {\n const { redis } = config;\n\n const threadOps: ThreadOps<LangChainContent> = {\n async initializeThread(threadId: string, threadKey?: string): Promise<void> {\n const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });\n await thread.initialize();\n },\n\n async appendHumanMessage(\n threadId: string,\n id: string,\n content: LangChainContent,\n threadKey?: string,\n ): Promise<void> {\n const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });\n await thread.appendUserMessage(id, content);\n },\n\n async appendSystemMessage(\n threadId: string,\n id: string,\n content: string,\n threadKey?: string,\n ): Promise<void> {\n const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });\n await thread.appendSystemMessage(id, content);\n },\n\n async appendToolResult(id: string, cfg: ToolResultConfig): Promise<void> {\n const { threadId, threadKey, toolCallId, content } = cfg;\n const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });\n await thread.appendToolResult(id, toolCallId, \"\", content);\n },\n\n async forkThread(\n sourceThreadId: string,\n targetThreadId: string,\n threadKey?: string,\n ): Promise<void> {\n const thread = createLangChainThreadManager({\n redis,\n threadId: sourceThreadId,\n key: threadKey,\n });\n await thread.fork(targetThreadId);\n },\n };\n\n function createActivities<S extends string = \"\">(\n scope?: S,\n ): LangChainThreadOps<S> {\n const prefix = scope\n ? `${ADAPTER_PREFIX}${scope.charAt(0).toUpperCase()}${scope.slice(1)}`\n : ADAPTER_PREFIX;\n const cap = (s: string): string =>\n s.charAt(0).toUpperCase() + s.slice(1);\n return Object.fromEntries(\n Object.entries(threadOps).map(([k, v]) => [`${prefix}${cap(k)}`, v]),\n ) as LangChainThreadOps<S>;\n }\n\n const makeInvoker = (\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n model: BaseChatModel<any>,\n ): ModelInvoker<StoredMessage> =>\n createLangChainModelInvoker({ redis, model, hooks: config.hooks });\n\n const invoker: ModelInvoker<StoredMessage> = config.model\n ? makeInvoker(config.model)\n : () => {\n throw new Error(\n \"No default model provided to createLangChainAdapter. \" +\n \"Either pass `model` in the config or use `createModelInvoker(model)` instead.\",\n );\n };\n\n return {\n createActivities,\n invoker,\n createModelInvoker: makeInvoker,\n wrapHandler: (handler) => handler,\n };\n}\n","import type { BaseMessage, MessageContent } from \"@langchain/core/messages\";\n\ntype ContentBlock = MessageContent extends (infer U)[] | string ? U : never;\n\n/**\n * Creates an `onPreparedMessage` hook that appends a cache-point content\n * block to the last message in the thread, and strips excess cache-point\n * blocks from earlier messages so the total never exceeds `maxBlocks`.\n *\n * Older cache-point blocks are removed first, keeping the most recent\n * `maxBlocks - 1` positions plus the last message's block.\n */\nexport function appendCachePoint(\n block: ContentBlock,\n { maxBlocks = 4 }: { maxBlocks?: number } = {},\n): (message: BaseMessage, index: number, messages: readonly BaseMessage[]) => BaseMessage {\n return (message, index, messages) => {\n const isLast = index === messages.length - 1;\n\n if (isLast) {\n const { content } = message;\n if (Array.isArray(content)) {\n if (content.some((b) => b.type === block.type)) return message;\n message.content = [...content, block];\n } else if (typeof content === \"string\") {\n message.content = [{ type: \"text\", text: content }, block] satisfies MessageContent;\n }\n return message;\n }\n\n const { content } = message;\n if (!Array.isArray(content) || !content.some((b) => b.type === block.type)) {\n return message;\n }\n\n // Count cache blocks in messages after this one (excluding the last,\n // which always gets one) plus 1 for the last message itself.\n let cacheBlocksAfter = 1;\n for (let i = index + 1; i < messages.length - 1; i++) {\n const msg = messages[i];\n if (!msg) continue;\n const c = msg.content;\n if (Array.isArray(c) && c.some((b: ContentBlock) => b.type === block.type)) {\n cacheBlocksAfter++;\n }\n }\n\n if (cacheBlocksAfter >= maxBlocks) {\n message.content = content.filter((b) => b.type !== block.type);\n }\n\n return message;\n };\n}\n"]}
1
+ {"version":3,"sources":["../../../../src/lib/thread/manager.ts","../../../../src/adapters/thread/langchain/thread-manager.ts","../../../../node_modules/uuid/dist/esm/stringify.js","../../../../node_modules/uuid/dist/esm/rng.js","../../../../node_modules/uuid/dist/esm/native.js","../../../../node_modules/uuid/dist/esm/v4.js","../../../../src/adapters/thread/langchain/model-invoker.ts","../../../../src/adapters/thread/langchain/activities.ts","../../../../src/adapters/thread/langchain/hooks.ts"],"names":["HumanMessage","SystemMessage","AIMessage","ToolMessage","messages","mapStoredMessagesToChatMessages","randomFillSync","randomUUID","invokeLangChainModel","content"],"mappings":";;;;;;;;AAEA,IAAM,kBAAA,GAAqB,EAAA,GAAK,EAAA,GAAK,EAAA,GAAK,EAAA;AAW1C,IAAM,wBAAA,GAA2B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,CAAA;AAYjC,SAAS,YAAA,CAAa,UAAkB,GAAA,EAAqB;AAC3D,EAAA,OAAO,CAAA,EAAG,GAAG,CAAA,QAAA,EAAW,QAAQ,CAAA,CAAA;AAClC;AAMO,SAAS,oBACd,MAAA,EACsB;AACtB,EAAA,MAAM;AAAA,IACJ,KAAA;AAAA,IACA,QAAA;AAAA,IACA,GAAA,GAAM,UAAA;AAAA,IACN,SAAA,GAAY,CAAC,CAAA,KAAiB,IAAA,CAAK,UAAU,CAAC,CAAA;AAAA,IAC9C,WAAA,GAAc,CAAC,GAAA,KAAmB,IAAA,CAAK,MAAM,GAAG,CAAA;AAAA,IAChD;AAAA,GACF,GAAI,MAAA;AACJ,EAAA,MAAM,QAAA,GAAW,YAAA,CAAa,QAAA,EAAU,GAAG,CAAA;AAC3C,EAAA,MAAM,OAAA,GAAU,YAAA,CAAa,QAAA,EAAU,CAAA,EAAG,GAAG,CAAA,KAAA,C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AA;AAAA,IACT;AAEA,IAAA,MAAM,EAAE,SAAQ,GAAI,OAAA;AACpB,IAAA,IAAI,CAAC,KAAA,CAAM,OAAA,CAAQ,OAAO,KAAK,CAAC,OAAA,CAAQ,IAAA,CAAK,CAAC,CAAA,KAAM,CAAA,CAAE,IAAA,KAAS,KAAA,CAAM,IAAI,CAAA,EAAG;AAC1E,MAAA,OAAO,OAAA;AAAA,IACT;AAIA,IAAA,IAAI,gBAAA,GAAmB,CAAA;AACvB,IAAA,KAAA,IAAS,IAAI,KAAA,GAAQ,CAAA,EAAG,IAAI,QAAA,CAAS,MAAA,GAAS,GAAG,CAAA,EAAA,EAAK;AACpD,MAAA,MAAM,GAAA,GAAM,SAAS,CAAC,CAAA;AACtB,MAAA,IAAI,CAAC,GAAA,EAAK;AACV,MAAA,MAAM,IAAI,GAAA,CAAI,OAAA;AACd,MAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,CAAC,CAAA,IAAK,CAAA,CAAE,IAAA,CAAK,CAAC,CAAA,KAAoB,CAAA,CAAE,IAAA,KAAS,KAAA,CAAM,IAAI,CAAA,EAAG;AAC1E,QAAA,gBAAA,EAAA;AAAA,MACF;AAAA,IACF;AAEA,IAAA,IAAI,oBAAoB,SAAA,EAAW;AACjC,MAAA,OAAA,CAAQ,OAAA,GAAU,QAAQ,MAAA,CAAO,CAAC,MAAM,CAAA,CAAE,IAAA,KAAS,MAAM,IAAI,CAAA;AAAA,IAC/D;AAEA,IAAA,OAAO,OAAA;AAAA,EACT,CAAA;AACF","file":"index.cjs","sourcesContent":["import type { ThreadManagerConfig, BaseThreadManager } from \"./types\";\n\nconst THREAD_TTL_SECONDS = 60 * 60 * 24 * 90; // 90 days\n\n/**\n * Lua script for atomic idempotent append.\n * Checks a dedup key; if it exists the message was already appended and we\n * return 0. Otherwise appends all messages to the list, sets TTL on both\n * the list and the dedup key, and returns 1.\n *\n * KEYS[1] = dedup key, KEYS[2] = list key\n * ARGV[1] = TTL seconds, ARGV[2..N] = serialised messages\n */\nconst APPEND_IDEMPOTENT_SCRIPT = `\nif redis.call('EXISTS', KEYS[1]) == 1 then\n return 0\nend\nfor i = 2, #ARGV do\n redis.call('RPUSH', KEYS[2], ARGV[i])\nend\nredis.call('EXPIRE', KEYS[2], tonumber(ARGV[1]))\nredis.call('SET', KEYS[1], '1', 'EX', tonumber(ARGV[1]))\nreturn 1\n`;\n\nfunction getThreadKey(threadId: string, key: string): string {\n return `${key}:thread:${threadId}`;\n}\n\n/**\n * Creates a generic thread manager for handling conversation state in Redis.\n * Framework-agnostic — works with any serializable message type.\n */\nexport function createThreadManager<T>(\n config: ThreadManagerConfig<T>\n): BaseThreadManager<T> {\n const {\n redis,\n threadId,\n key = \"messages\",\n serialize = (m: T): string => JSON.stringify(m),\n deserialize = (raw: string): T => JSON.parse(raw) as T,\n idOf,\n } = config;\n const redisKey = getThreadKey(threadId, key);\n const metaKey = getThreadKey(threadId, `${key}:meta`);\n\n async function assertThreadExists(): Promise<void> {\n const exists = await redis.exists(metaKey);\n if (!exists) {\n throw new Error(`Thread \"${threadId}\" (key: ${key}) does not exist`);\n }\n }\n\n return {\n async initialize(): Promise<void> {\n await redis.del(redisKey);\n await redis.set(metaKey, \"1\", \"EX\", THREAD_TTL_SECONDS);\n },\n\n async load(): Promise<T[]> {\n await assertThreadExists();\n const data = await redis.lrange(redisKey, 0, -1);\n return data.map(deserialize);\n },\n\n async append(messages: T[]): Promise<void> {\n if (messages.length === 0) return;\n await assertThreadExists();\n\n if (idOf) {\n const dedupId = messages.map(idOf).join(\":\");\n const dedupKey = getThreadKey(threadId, `dedup:${dedupId}`);\n await redis.eval(\n APPEND_IDEMPOTENT_SCRIPT,\n 2,\n dedupKey,\n redisKey,\n String(THREAD_TTL_SECONDS),\n ...messages.map(serialize)\n );\n } else {\n await redis.rpush(redisKey, ...messages.map(serialize));\n await redis.expire(redisKey, THREAD_TTL_SECONDS);\n }\n },\n\n async fork(newThreadId: string): Promise<BaseThreadManager<T>> {\n await assertThreadExists();\n const data = await redis.lrange(redisKey, 0, -1);\n const forked = createThreadManager({\n ...config,\n threadId: newThreadId,\n });\n await forked.initialize();\n if (data.length > 0) {\n const newKey = getThreadKey(newThreadId, key);\n await redis.rpush(newKey, ...data);\n await redis.expire(newKey, THREAD_TTL_SECONDS);\n }\n return forked;\n },\n\n async delete(): Promise<void> {\n await redis.del(redisKey, metaKey);\n },\n };\n}\n","import type Redis from \"ioredis\";\nimport type { JsonValue } from \"../../../lib/state/types\";\nimport {\n AIMessage,\n type BaseMessage,\n HumanMessage,\n type MessageContent,\n type StoredMessage,\n SystemMessage,\n ToolMessage,\n mapStoredMessagesToChatMessages,\n} from \"@langchain/core/messages\";\nimport { createThreadManager } from \"../../../lib/thread/manager\";\nimport type {\n ProviderThreadManager,\n ThreadManagerConfig,\n ThreadManagerHooks,\n} from \"../../../lib/thread/types\";\n\n/** SDK-native content type for LangChain human messages */\nexport type LangChainContent = string | MessageContent;\n\nexport type LangChainThreadManagerHooks = ThreadManagerHooks<StoredMessage, BaseMessage>;\n\nexport interface LangChainThreadManagerConfig {\n redis: Redis;\n threadId: string;\n /** Thread key, defaults to 'messages' */\n key?: string;\n hooks?: LangChainThreadManagerHooks;\n}\n\n/** Prepared payload ready to send to a LangChain chat model */\nexport interface LangChainInvocationPayload {\n messages: BaseMessage[];\n}\n\n/** Thread manager with LangChain StoredMessage convenience helpers */\nexport interface LangChainThreadManager\n extends ProviderThreadManager<StoredMessage, LangChainContent> {\n appendAIMessage(id: string, content: string | MessageContent): Promise<void>;\n prepareForInvocation(): Promise<LangChainInvocationPayload>;\n}\n\nfunction storedMessageId(msg: StoredMessage): string {\n if (msg.type === \"tool\" && msg.data.tool_call_id) {\n return msg.data.tool_call_id;\n }\n\n if (msg.data.id) {\n return msg.data.id;\n }\n\n throw new Error(\"No id found for message\");\n}\n\n/**\n * Creates a LangChain-specific thread manager that stores StoredMessage\n * instances in Redis and provides convenience helpers for creating and\n * appending typed LangChain messages.\n */\nexport function createLangChainThreadManager(\n config: LangChainThreadManagerConfig,\n): LangChainThreadManager {\n const baseConfig: ThreadManagerConfig<StoredMessage> = {\n redis: config.redis,\n threadId: config.threadId,\n key: config.key,\n idOf: storedMessageId,\n };\n\n const base = createThreadManager(baseConfig);\n\n const helpers: Omit<LangChainThreadManager, keyof typeof base> = {\n async appendUserMessage(\n id: string,\n content: LangChainContent,\n ): Promise<void> {\n await base.append([\n new HumanMessage({ id, content: content as MessageContent }).toDict(),\n ]);\n },\n\n async appendSystemMessage(id: string, content: string): Promise<void> {\n await base.initialize();\n await base.append([\n new SystemMessage({ id, content }).toDict(),\n ]);\n },\n\n async appendAIMessage(\n id: string,\n content: string | MessageContent,\n ): Promise<void> {\n await base.append([\n new AIMessage({ id, content: content as MessageContent }).toDict(),\n ]);\n },\n\n async appendToolResult(\n id: string,\n _toolCallId: string,\n _toolName: string,\n content: JsonValue,\n ): Promise<void> {\n await base.append([\n new ToolMessage({ id, content: content as MessageContent, tool_call_id: _toolCallId }).toDict(),\n ]);\n },\n\n async prepareForInvocation(): Promise<LangChainInvocationPayload> {\n const stored = await base.load();\n const { onPrepareMessage, onPreparedMessage } = config.hooks ?? {};\n const mapped = onPrepareMessage\n ? stored.map((msg, i) => onPrepareMessage(msg, i, stored))\n : stored;\n const messages = mapStoredMessagesToChatMessages(mapped);\n return {\n messages: onPreparedMessage\n ? messages.map((msg, i) => onPreparedMessage(msg, i, messages))\n : messages,\n };\n },\n };\n\n return Object.assign(base, helpers);\n}\n","import validate from './validate.js';\nconst byteToHex = [];\nfor (let i = 0; i < 256; ++i) {\n byteToHex.push((i + 0x100).toString(16).slice(1));\n}\nexport function unsafeStringify(arr, offset = 0) {\n return (byteToHex[arr[offset + 0]] +\n byteToHex[arr[offset + 1]] +\n byteToHex[arr[offset + 2]] +\n byteToHex[arr[offset + 3]] +\n '-' +\n byteToHex[arr[offset + 4]] +\n byteToHex[arr[offset + 5]] +\n '-' +\n byteToHex[arr[offset + 6]] +\n byteToHex[arr[offset + 7]] +\n '-' +\n byteToHex[arr[offset + 8]] +\n byteToHex[arr[offset + 9]] +\n '-' +\n byteToHex[arr[offset + 10]] +\n byteToHex[arr[offset + 11]] +\n byteToHex[arr[offset + 12]] +\n byteToHex[arr[offset + 13]] +\n byteToHex[arr[offset + 14]] +\n byteToHex[arr[offset + 15]]).toLowerCase();\n}\nfunction stringify(arr, offset = 0) {\n const uuid = unsafeStringify(arr, offset);\n if (!validate(uuid)) {\n throw TypeError('Stringified UUID is invalid');\n }\n return uuid;\n}\nexport default stringify;\n","import { randomFillSync } from 'crypto';\nconst rnds8Pool = new Uint8Array(256);\nlet poolPtr = rnds8Pool.length;\nexport default function rng() {\n if (poolPtr > rnds8Pool.length - 16) {\n randomFillSync(rnds8Pool);\n poolPtr = 0;\n }\n return rnds8Pool.slice(poolPtr, (poolPtr += 16));\n}\n","import { randomUUID } from 'crypto';\nexport default { randomUUID };\n","import native from './native.js';\nimport rng from './rng.js';\nimport { unsafeStringify } from './stringify.js';\nfunction v4(options, buf, offset) {\n if (native.randomUUID && !buf && !options) {\n return native.randomUUID();\n }\n options = options || {};\n const rnds = options.random ?? options.rng?.() ?? rng();\n if (rnds.length < 16) {\n throw new Error('Random bytes length must be >= 16');\n }\n rnds[6] = (rnds[6] & 0x0f) | 0x40;\n rnds[8] = (rnds[8] & 0x3f) | 0x80;\n if (buf) {\n offset = offset || 0;\n if (offset < 0 || offset + 16 > buf.length) {\n throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);\n }\n for (let i = 0; i < 16; ++i) {\n buf[offset + i] = rnds[i];\n }\n return buf;\n }\n return unsafeStringify(rnds);\n}\nexport default v4;\n","import type Redis from \"ioredis\";\nimport type { AgentResponse, ModelInvokerConfig } from \"../../../lib/model\";\nimport type { StoredMessage } from \"@langchain/core/messages\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport type { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport { createLangChainThreadManager, type LangChainThreadManagerHooks } from \"./thread-manager\";\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface LangChainModelInvokerConfig<TModel extends BaseChatModel<any> = BaseChatModel<any>> {\n redis: Redis;\n model: TModel;\n hooks?: LangChainThreadManagerHooks;\n}\n\n/**\n * Creates a LangChain-based model invoker that satisfies the generic\n * `ModelInvoker<StoredMessage>` contract.\n *\n * Loads the conversation thread from Redis, invokes a LangChain chat model,\n * and returns a normalised AgentResponse.\n * The caller is responsible for appending the response to the thread.\n *\n * @example\n * ```typescript\n * import { createLangChainModelInvoker } from 'zeitlich/adapters/thread/langchain';\n * import { createRunAgentActivity } from 'zeitlich';\n * import { ChatAnthropic } from '@langchain/anthropic';\n *\n * const model = new ChatAnthropic({ model: \"claude-sonnet-4-6\" });\n * const invoker = createLangChainModelInvoker({ redis, model });\n *\n * return { runAgent: createRunAgentActivity(client, invoker) };\n * ```\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function createLangChainModelInvoker<TModel extends BaseChatModel<any> = BaseChatModel<any>>(\n { redis, model, hooks }: LangChainModelInvokerConfig<TModel>,\n) {\n return async function invokeLangChainModel(\n config: ModelInvokerConfig,\n ): Promise<AgentResponse<StoredMessage>> {\n const { threadId, threadKey, agentName, state, metadata } = config;\n\n const thread = createLangChainThreadManager({ redis, threadId, key: threadKey, hooks });\n const runId = uuidv4();\n\n const { messages } = await thread.prepareForInvocation();\n const response = await model.invoke(\n messages,\n {\n runName: agentName,\n runId,\n metadata: { thread_id: `${agentName}-${threadId}`, ...metadata },\n tools: state.tools,\n },\n );\n\n const toolCalls = response.tool_calls ?? [];\n\n return {\n message: response.toDict(),\n rawToolCalls: toolCalls.map((tc) => ({\n id: tc.id,\n name: tc.name,\n args: tc.args,\n })),\n usage: {\n inputTokens: response.usage_metadata?.input_tokens,\n outputTokens: response.usage_metadata?.output_tokens,\n reasonTokens: response.usage_metadata?.output_token_details?.reasoning,\n cachedWriteTokens:\n response.usage_metadata?.input_token_details?.cache_creation,\n cachedReadTokens:\n response.usage_metadata?.input_token_details?.cache_read,\n },\n };\n };\n}\n\n/**\n * Standalone function for one-shot LangChain model invocation.\n * Convenience wrapper around createLangChainModelInvoker for cases where\n * you don't need to reuse the invoker.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport async function invokeLangChainModel<TModel extends BaseChatModel<any> = BaseChatModel<any>>({\n redis,\n model,\n hooks,\n config,\n}: {\n redis: Redis;\n config: ModelInvokerConfig;\n model: TModel;\n hooks?: LangChainThreadManagerHooks;\n}): Promise<AgentResponse<StoredMessage>> {\n const invoker = createLangChainModelInvoker({ redis, model, hooks });\n return invoker(config);\n}\n","import type Redis from \"ioredis\";\nimport type { ToolResultConfig } from \"../../../lib/types\";\nimport type { MessageContent } from \"@langchain/core/messages\";\nimport type {\n ActivityToolHandler,\n RouterContext,\n ToolHandlerResponse,\n} from \"../../../lib/tool-router/types\";\nimport type {\n ThreadOps,\n PrefixedThreadOps,\n ScopedPrefix,\n} from \"../../../lib/session/types\";\nimport type { ModelInvoker } from \"../../../lib/model\";\nimport type { StoredMessage } from \"@langchain/core/messages\";\nimport type { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport {\n createLangChainThreadManager,\n type LangChainContent,\n type LangChainThreadManagerHooks,\n} from \"./thread-manager\";\nimport { createLangChainModelInvoker } from \"./model-invoker\";\n\nconst ADAPTER_PREFIX = \"langChain\" as const;\n\nexport type LangChainThreadOps<TScope extends string = \"\"> =\n PrefixedThreadOps<ScopedPrefix<TScope, typeof ADAPTER_PREFIX>, LangChainContent>;\n\nexport interface LangChainAdapterConfig {\n redis: Redis;\n /** Optional default model — if omitted, use `createModelInvoker()` to create invokers later */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n model?: BaseChatModel<any>;\n hooks?: LangChainThreadManagerHooks;\n}\n\n/**\n * Tool response type accepted by the LangChain adapter.\n *\n * Content is passed directly to `ToolMessage` as `MessageContent`.\n * Handlers can return a string or an array of content blocks\n * (e.g. `{ type: \"text\", text: \"...\" }`, `{ type: \"image_url\", image_url: { ... } }`).\n */\nexport type LangChainToolResponse = MessageContent;\n\nexport interface LangChainAdapter {\n /** Model invoker using the default model (only available when `model` was provided) */\n invoker: ModelInvoker<StoredMessage>;\n /** Create an invoker for a specific model (for multi-model setups) */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n createModelInvoker(model: BaseChatModel<any>): ModelInvoker<StoredMessage>;\n /**\n * Create prefixed thread activities for registration on the worker.\n *\n * @param scope - Workflow name appended to the adapter prefix.\n *\n * @example\n * ```typescript\n * adapter.createActivities(\"codingAgent\")\n * // → { langChainCodingAgentInitializeThread, langChainCodingAgentAppendHumanMessage, … }\n * ```\n */\n createActivities<S extends string = \"\">(\n scope?: S,\n ): LangChainThreadOps<S>;\n\n /**\n * Identity wrapper that types a tool handler for this adapter.\n * Constrains `toolResponse` to {@link LangChainToolResponse}.\n */\n wrapHandler<TArgs, TResult, TContext extends RouterContext = RouterContext>(\n handler: (\n args: TArgs,\n context: TContext,\n ) => Promise<ToolHandlerResponse<TResult, LangChainToolResponse>>,\n ): ActivityToolHandler<TArgs, TResult, TContext, LangChainToolResponse>;\n}\n\n/**\n * Creates a LangChain adapter that bundles thread operations and model\n * invocation using a consistent message format (StoredMessage).\n *\n * Use `createActivities(scope)` to register scoped thread operations as\n * Temporal activities on the worker. The `invoker` (or invokers created via\n * `createModelInvoker`) should be wrapped with `createRunAgentActivity`.\n *\n * @example\n * ```typescript\n * import { createLangChainAdapter } from 'zeitlich/adapters/thread/langchain';\n * import { createRunAgentActivity } from 'zeitlich';\n *\n * const adapter = createLangChainAdapter({ redis, model });\n *\n * export function createActivities(client: WorkflowClient) {\n * return {\n * ...adapter.createActivities(\"codingAgent\"),\n * runCodingAgent: createRunAgentActivity(client, adapter.invoker),\n * };\n * }\n * ```\n *\n * @example Multi-agent worker\n * ```typescript\n * export function createActivities(client: WorkflowClient) {\n * return {\n * ...adapter.createActivities(\"codingAgent\"),\n * ...adapter.createActivities(\"researchAgent\"),\n * runCodingAgent: createRunAgentActivity(client, adapter.invoker),\n * runResearchAgent: createRunAgentActivity(client, adapter.createModelInvoker(claude)),\n * };\n * }\n * ```\n */\nexport function createLangChainAdapter(\n config: LangChainAdapterConfig,\n): LangChainAdapter {\n const { redis } = config;\n\n const threadOps: ThreadOps<LangChainContent> = {\n async initializeThread(threadId: string, threadKey?: string): Promise<void> {\n const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });\n await thread.initialize();\n },\n\n async appendHumanMessage(\n threadId: string,\n id: string,\n content: LangChainContent,\n threadKey?: string,\n ): Promise<void> {\n const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });\n await thread.appendUserMessage(id, content);\n },\n\n async appendSystemMessage(\n threadId: string,\n id: string,\n content: string,\n threadKey?: string,\n ): Promise<void> {\n const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });\n await thread.appendSystemMessage(id, content);\n },\n\n async appendToolResult(id: string, cfg: ToolResultConfig): Promise<void> {\n const { threadId, threadKey, toolCallId, content } = cfg;\n const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });\n await thread.appendToolResult(id, toolCallId, \"\", content);\n },\n\n async appendAgentMessage(\n threadId: string,\n id: string,\n message: StoredMessage,\n threadKey?: string,\n ): Promise<void> {\n const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });\n const patched = { ...message, data: { ...message.data, id } };\n await thread.append([patched]);\n },\n\n async forkThread(\n sourceThreadId: string,\n targetThreadId: string,\n threadKey?: string,\n ): Promise<void> {\n const thread = createLangChainThreadManager({\n redis,\n threadId: sourceThreadId,\n key: threadKey,\n });\n await thread.fork(targetThreadId);\n },\n };\n\n function createActivities<S extends string = \"\">(\n scope?: S,\n ): LangChainThreadOps<S> {\n const prefix = scope\n ? `${ADAPTER_PREFIX}${scope.charAt(0).toUpperCase()}${scope.slice(1)}`\n : ADAPTER_PREFIX;\n const cap = (s: string): string =>\n s.charAt(0).toUpperCase() + s.slice(1);\n return Object.fromEntries(\n Object.entries(threadOps).map(([k, v]) => [`${prefix}${cap(k)}`, v]),\n ) as LangChainThreadOps<S>;\n }\n\n const makeInvoker = (\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n model: BaseChatModel<any>,\n ): ModelInvoker<StoredMessage> =>\n createLangChainModelInvoker({ redis, model, hooks: config.hooks });\n\n const invoker: ModelInvoker<StoredMessage> = config.model\n ? makeInvoker(config.model)\n : () => {\n throw new Error(\n \"No default model provided to createLangChainAdapter. \" +\n \"Either pass `model` in the config or use `createModelInvoker(model)` instead.\",\n );\n };\n\n return {\n createActivities,\n invoker,\n createModelInvoker: makeInvoker,\n wrapHandler: (handler) => handler,\n };\n}\n","import type { BaseMessage, MessageContent } from \"@langchain/core/messages\";\n\ntype ContentBlock = MessageContent extends (infer U)[] | string ? U : never;\n\n/**\n * Creates an `onPreparedMessage` hook that appends a cache-point content\n * block to the last message in the thread, and strips excess cache-point\n * blocks from earlier messages so the total never exceeds `maxBlocks`.\n *\n * Older cache-point blocks are removed first, keeping the most recent\n * `maxBlocks - 1` positions plus the last message's block.\n */\nexport function appendCachePoint(\n block: ContentBlock,\n { maxBlocks = 4 }: { maxBlocks?: number } = {},\n): (message: BaseMessage, index: number, messages: readonly BaseMessage[]) => BaseMessage {\n return (message, index, messages) => {\n const isLast = index === messages.length - 1;\n\n if (isLast) {\n const { content } = message;\n if (Array.isArray(content)) {\n if (content.some((b) => b.type === block.type)) return message;\n message.content = [...content, block];\n } else if (typeof content === \"string\") {\n message.content = [{ type: \"text\", text: content }, block] satisfies MessageContent;\n }\n return message;\n }\n\n const { content } = message;\n if (!Array.isArray(content) || !content.some((b) => b.type === block.type)) {\n return message;\n }\n\n // Count cache blocks in messages after this one (excluding the last,\n // which always gets one) plus 1 for the last message itself.\n let cacheBlocksAfter = 1;\n for (let i = index + 1; i < messages.length - 1; i++) {\n const msg = messages[i];\n if (!msg) continue;\n const c = msg.content;\n if (Array.isArray(c) && c.some((b: ContentBlock) => b.type === block.type)) {\n cacheBlocksAfter++;\n }\n }\n\n if (cacheBlocksAfter >= maxBlocks) {\n message.content = content.filter((b) => b.type !== block.type);\n }\n\n return message;\n };\n}\n"]}
@@ -1,15 +1,15 @@
1
1
  import Redis from 'ioredis';
2
2
  import { StoredMessage, MessageContent, BaseMessage } from '@langchain/core/messages';
3
- import { a as ModelInvoker, P as PrefixedThreadOps, S as ScopedPrefix, R as RouterContext, b as ToolHandlerResponse, c as ActivityToolHandler, M as ModelInvokerConfig, A as AgentResponse } from '../../../types-CjeGWQm1.cjs';
3
+ import { a as ModelInvoker, P as PrefixedThreadOps, S as ScopedPrefix, R as RouterContext, b as ToolHandlerResponse, c as ActivityToolHandler, M as ModelInvokerConfig, A as AgentResponse } from '../../../types-Bpq5fDI5.cjs';
4
4
  import { BaseChatModel } from '@langchain/core/language_models/chat_models';
5
- import { L as LangChainContent, a as LangChainThreadManagerHooks } from '../../../thread-manager-DowU4ntB.cjs';
6
- export { b as LangChainInvocationPayload, c as LangChainThreadManager, d as LangChainThreadManagerConfig, e as createLangChainThreadManager } from '../../../thread-manager-DowU4ntB.cjs';
5
+ import { L as LangChainContent, a as LangChainThreadManagerHooks } from '../../../thread-manager-Cv_BR28i.cjs';
6
+ export { b as LangChainInvocationPayload, c as LangChainThreadManager, d as LangChainThreadManagerConfig, e as createLangChainThreadManager } from '../../../thread-manager-Cv_BR28i.cjs';
7
7
  import '@temporalio/common';
8
8
  import '@temporalio/workflow';
9
9
  import '@temporalio/common/lib/interfaces';
10
10
  import 'zod';
11
11
  import '../../../types-AujBIMMn.cjs';
12
- import '../../../types-e_38QaKo.cjs';
12
+ import '../../../types-NJDyMyUx.cjs';
13
13
 
14
14
  declare const ADAPTER_PREFIX: "langChain";
15
15
  type LangChainThreadOps<TScope extends string = ""> = PrefixedThreadOps<ScopedPrefix<TScope, typeof ADAPTER_PREFIX>, LangChainContent>;
@@ -97,7 +97,8 @@ interface LangChainModelInvokerConfig<TModel extends BaseChatModel<any> = BaseCh
97
97
  * `ModelInvoker<StoredMessage>` contract.
98
98
  *
99
99
  * Loads the conversation thread from Redis, invokes a LangChain chat model,
100
- * appends the AI response, and returns a normalised AgentResponse.
100
+ * and returns a normalised AgentResponse.
101
+ * The caller is responsible for appending the response to the thread.
101
102
  *
102
103
  * @example
103
104
  * ```typescript
@@ -1,15 +1,15 @@
1
1
  import Redis from 'ioredis';
2
2
  import { StoredMessage, MessageContent, BaseMessage } from '@langchain/core/messages';
3
- import { a as ModelInvoker, P as PrefixedThreadOps, S as ScopedPrefix, R as RouterContext, b as ToolHandlerResponse, c as ActivityToolHandler, M as ModelInvokerConfig, A as AgentResponse } from '../../../types-BmS-Huc0.js';
3
+ import { a as ModelInvoker, P as PrefixedThreadOps, S as ScopedPrefix, R as RouterContext, b as ToolHandlerResponse, c as ActivityToolHandler, M as ModelInvokerConfig, A as AgentResponse } from '../../../types-CheCTLeV.js';
4
4
  import { BaseChatModel } from '@langchain/core/language_models/chat_models';
5
- import { L as LangChainContent, a as LangChainThreadManagerHooks } from '../../../thread-manager-Cv82H1wi.js';
6
- export { b as LangChainInvocationPayload, c as LangChainThreadManager, d as LangChainThreadManagerConfig, e as createLangChainThreadManager } from '../../../thread-manager-Cv82H1wi.js';
5
+ import { L as LangChainContent, a as LangChainThreadManagerHooks } from '../../../thread-manager-DKWxHUzD.js';
6
+ export { b as LangChainInvocationPayload, c as LangChainThreadManager, d as LangChainThreadManagerConfig, e as createLangChainThreadManager } from '../../../thread-manager-DKWxHUzD.js';
7
7
  import '@temporalio/common';
8
8
  import '@temporalio/workflow';
9
9
  import '@temporalio/common/lib/interfaces';
10
10
  import 'zod';
11
11
  import '../../../types-AujBIMMn.js';
12
- import '../../../types-D6UKZZtj.js';
12
+ import '../../../types-BxiT8w9d.js';
13
13
 
14
14
  declare const ADAPTER_PREFIX: "langChain";
15
15
  type LangChainThreadOps<TScope extends string = ""> = PrefixedThreadOps<ScopedPrefix<TScope, typeof ADAPTER_PREFIX>, LangChainContent>;
@@ -97,7 +97,8 @@ interface LangChainModelInvokerConfig<TModel extends BaseChatModel<any> = BaseCh
97
97
  * `ModelInvoker<StoredMessage>` contract.
98
98
  *
99
99
  * Loads the conversation thread from Redis, invokes a LangChain chat model,
100
- * appends the AI response, and returns a normalised AgentResponse.
100
+ * and returns a normalised AgentResponse.
101
+ * The caller is responsible for appending the response to the thread.
101
102
  *
102
103
  * @example
103
104
  * ```typescript
@@ -200,7 +200,6 @@ function createLangChainModelInvoker({ redis, model, hooks }) {
200
200
  tools: state.tools
201
201
  }
202
202
  );
203
- await thread.append([response.toDict()]);
204
203
  const toolCalls = response.tool_calls ?? [];
205
204
  return {
206
205
  message: response.toDict(),
@@ -251,6 +250,11 @@ function createLangChainAdapter(config) {
251
250
  const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });
252
251
  await thread.appendToolResult(id, toolCallId, "", content);
253
252
  },
253
+ async appendAgentMessage(threadId, id, message, threadKey) {
254
+ const thread = createLangChainThreadManager({ redis, threadId, key: threadKey });
255
+ const patched = { ...message, data: { ...message.data, id } };
256
+ await thread.append([patched]);
257
+ },
254
258
  async forkThread(sourceThreadId, targetThreadId, threadKey) {
255
259
  const thread = createLangChainThreadManager({
256
260
  redis,