@posthog/agent 2.1.53 → 2.1.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1174,7 +1174,7 @@ import { v7 as uuidv7 } from "uuid";
1174
1174
  // package.json
1175
1175
  var package_default = {
1176
1176
  name: "@posthog/agent",
1177
- version: "2.1.53",
1177
+ version: "2.1.60",
1178
1178
  repository: "https://github.com/PostHog/twig",
1179
1179
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
1180
1180
  exports: {
@@ -1239,7 +1239,8 @@ var package_default = {
1239
1239
  test: "vitest run",
1240
1240
  "test:watch": "vitest",
1241
1241
  typecheck: "pnpm exec tsc --noEmit",
1242
- prepublishOnly: "pnpm run build"
1242
+ prepublishOnly: "pnpm run build",
1243
+ clean: "rm -rf dist .turbo"
1243
1244
  },
1244
1245
  engines: {
1245
1246
  node: ">=20.0.0"
@@ -2094,14 +2095,25 @@ ${text2}${text2.endsWith("\n") ? "" : "\n"}${escapedText}`;
2094
2095
  function messageUpdateType(role) {
2095
2096
  return role === "assistant" ? "agent_message_chunk" : "user_message_chunk";
2096
2097
  }
2097
- function toolMeta(toolName, toolResponse) {
2098
- return toolResponse ? { claudeCode: { toolName, toolResponse } } : { claudeCode: { toolName } };
2098
+ function toolMeta(toolName, toolResponse, parentToolCallId) {
2099
+ const meta = { toolName };
2100
+ if (toolResponse !== void 0) meta.toolResponse = toolResponse;
2101
+ if (parentToolCallId) meta.parentToolCallId = parentToolCallId;
2102
+ return { claudeCode: meta };
2099
2103
  }
2100
- function handleTextChunk(chunk, role) {
2101
- return {
2104
+ function handleTextChunk(chunk, role, parentToolCallId) {
2105
+ const update = {
2102
2106
  sessionUpdate: messageUpdateType(role),
2103
2107
  content: text(chunk.text)
2104
2108
  };
2109
+ if (parentToolCallId) {
2110
+ update._meta = toolMeta(
2111
+ "__text__",
2112
+ void 0,
2113
+ parentToolCallId
2114
+ );
2115
+ }
2116
+ return update;
2105
2117
  }
2106
2118
  function handleImageChunk(chunk, role) {
2107
2119
  return {
@@ -2113,11 +2125,19 @@ function handleImageChunk(chunk, role) {
2113
2125
  )
2114
2126
  };
2115
2127
  }
2116
- function handleThinkingChunk(chunk) {
2117
- return {
2128
+ function handleThinkingChunk(chunk, parentToolCallId) {
2129
+ const update = {
2118
2130
  sessionUpdate: "agent_thought_chunk",
2119
2131
  content: text(chunk.thinking)
2120
2132
  };
2133
+ if (parentToolCallId) {
2134
+ update._meta = toolMeta(
2135
+ "__thinking__",
2136
+ void 0,
2137
+ parentToolCallId
2138
+ );
2139
+ }
2140
+ return update;
2121
2141
  }
2122
2142
  function handleToolUseChunk(chunk, ctx) {
2123
2143
  ctx.toolUseCache[chunk.id] = chunk;
@@ -2138,7 +2158,7 @@ function handleToolUseChunk(chunk, ctx) {
2138
2158
  await ctx.client.sessionUpdate({
2139
2159
  sessionId: ctx.sessionId,
2140
2160
  update: {
2141
- _meta: toolMeta(toolUse.name, toolResponse),
2161
+ _meta: toolMeta(toolUse.name, toolResponse, ctx.parentToolCallId),
2142
2162
  toolCallId: toolUseId,
2143
2163
  sessionUpdate: "tool_call_update"
2144
2164
  }
@@ -2156,7 +2176,7 @@ function handleToolUseChunk(chunk, ctx) {
2156
2176
  } catch {
2157
2177
  }
2158
2178
  return {
2159
- _meta: toolMeta(chunk.name),
2179
+ _meta: toolMeta(chunk.name, void 0, ctx.parentToolCallId),
2160
2180
  toolCallId: chunk.id,
2161
2181
  sessionUpdate: "tool_call",
2162
2182
  rawInput,
@@ -2176,7 +2196,7 @@ function handleToolResultChunk(chunk, ctx) {
2176
2196
  return null;
2177
2197
  }
2178
2198
  return {
2179
- _meta: toolMeta(toolUse.name),
2199
+ _meta: toolMeta(toolUse.name, void 0, ctx.parentToolCallId),
2180
2200
  toolCallId: chunk.tool_use_id,
2181
2201
  sessionUpdate: "tool_call_update",
2182
2202
  status: chunk.is_error ? "failed" : "completed",
@@ -2190,12 +2210,12 @@ function processContentChunk(chunk, role, ctx) {
2190
2210
  switch (chunk.type) {
2191
2211
  case "text":
2192
2212
  case "text_delta":
2193
- return handleTextChunk(chunk, role);
2213
+ return handleTextChunk(chunk, role, ctx.parentToolCallId);
2194
2214
  case "image":
2195
2215
  return handleImageChunk(chunk, role);
2196
2216
  case "thinking":
2197
2217
  case "thinking_delta":
2198
- return handleThinkingChunk(chunk);
2218
+ return handleThinkingChunk(chunk, ctx.parentToolCallId);
2199
2219
  case "tool_use":
2200
2220
  case "server_tool_use":
2201
2221
  case "mcp_tool_use":
@@ -2225,24 +2245,28 @@ function processContentChunk(chunk, role, ctx) {
2225
2245
  return null;
2226
2246
  }
2227
2247
  }
2228
- function toAcpNotifications(content, role, sessionId, toolUseCache, fileContentCache, client, logger) {
2248
+ function toAcpNotifications(content, role, sessionId, toolUseCache, fileContentCache, client, logger, parentToolCallId) {
2229
2249
  if (typeof content === "string") {
2230
- return [
2231
- {
2232
- sessionId,
2233
- update: {
2234
- sessionUpdate: messageUpdateType(role),
2235
- content: text(content)
2236
- }
2237
- }
2238
- ];
2250
+ const update = {
2251
+ sessionUpdate: messageUpdateType(role),
2252
+ content: text(content)
2253
+ };
2254
+ if (parentToolCallId) {
2255
+ update._meta = toolMeta(
2256
+ "__text__",
2257
+ void 0,
2258
+ parentToolCallId
2259
+ );
2260
+ }
2261
+ return [{ sessionId, update }];
2239
2262
  }
2240
2263
  const ctx = {
2241
2264
  sessionId,
2242
2265
  toolUseCache,
2243
2266
  fileContentCache,
2244
2267
  client,
2245
- logger
2268
+ logger,
2269
+ parentToolCallId
2246
2270
  };
2247
2271
  const output = [];
2248
2272
  for (const chunk of content) {
@@ -2253,7 +2277,7 @@ function toAcpNotifications(content, role, sessionId, toolUseCache, fileContentC
2253
2277
  }
2254
2278
  return output;
2255
2279
  }
2256
- function streamEventToAcpNotifications(message, sessionId, toolUseCache, fileContentCache, client, logger) {
2280
+ function streamEventToAcpNotifications(message, sessionId, toolUseCache, fileContentCache, client, logger, parentToolCallId) {
2257
2281
  const event = message.event;
2258
2282
  switch (event.type) {
2259
2283
  case "content_block_start":
@@ -2264,7 +2288,8 @@ function streamEventToAcpNotifications(message, sessionId, toolUseCache, fileCon
2264
2288
  toolUseCache,
2265
2289
  fileContentCache,
2266
2290
  client,
2267
- logger
2291
+ logger,
2292
+ parentToolCallId
2268
2293
  );
2269
2294
  case "content_block_delta":
2270
2295
  return toAcpNotifications(
@@ -2274,7 +2299,8 @@ function streamEventToAcpNotifications(message, sessionId, toolUseCache, fileCon
2274
2299
  toolUseCache,
2275
2300
  fileContentCache,
2276
2301
  client,
2277
- logger
2302
+ logger,
2303
+ parentToolCallId
2278
2304
  );
2279
2305
  case "message_start":
2280
2306
  case "message_delta":
@@ -2387,13 +2413,15 @@ function handleResultMessage(message, context) {
2387
2413
  }
2388
2414
  async function handleStreamEvent(message, context) {
2389
2415
  const { sessionId, client, toolUseCache, fileContentCache, logger } = context;
2416
+ const parentToolCallId = message.parent_tool_use_id ?? void 0;
2390
2417
  for (const notification of streamEventToAcpNotifications(
2391
2418
  message,
2392
2419
  sessionId,
2393
2420
  toolUseCache,
2394
2421
  fileContentCache,
2395
2422
  client,
2396
- logger
2423
+ logger,
2424
+ parentToolCallId
2397
2425
  )) {
2398
2426
  await client.sessionUpdate(notification);
2399
2427
  context.session.notificationHistory.push(notification);
@@ -2445,6 +2473,7 @@ async function handleUserAssistantMessage(message, context) {
2445
2473
  }
2446
2474
  const content = message.message.content;
2447
2475
  const contentToProcess = filterMessageContent(content);
2476
+ const parentToolCallId = "parent_tool_use_id" in message ? message.parent_tool_use_id ?? void 0 : void 0;
2448
2477
  for (const notification of toAcpNotifications(
2449
2478
  contentToProcess,
2450
2479
  message.message.role,
@@ -2452,7 +2481,8 @@ async function handleUserAssistantMessage(message, context) {
2452
2481
  toolUseCache,
2453
2482
  fileContentCache,
2454
2483
  client,
2455
- logger
2484
+ logger,
2485
+ parentToolCallId
2456
2486
  )) {
2457
2487
  await client.sessionUpdate(notification);
2458
2488
  session.notificationHistory.push(notification);