@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/agent.js CHANGED
@@ -276,7 +276,7 @@ import { v7 as uuidv7 } from "uuid";
276
276
  // package.json
277
277
  var package_default = {
278
278
  name: "@posthog/agent",
279
- version: "2.1.53",
279
+ version: "2.1.60",
280
280
  repository: "https://github.com/PostHog/twig",
281
281
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
282
282
  exports: {
@@ -341,7 +341,8 @@ var package_default = {
341
341
  test: "vitest run",
342
342
  "test:watch": "vitest",
343
343
  typecheck: "pnpm exec tsc --noEmit",
344
- prepublishOnly: "pnpm run build"
344
+ prepublishOnly: "pnpm run build",
345
+ clean: "rm -rf dist .turbo"
345
346
  },
346
347
  engines: {
347
348
  node: ">=20.0.0"
@@ -1188,14 +1189,25 @@ ${text2}${text2.endsWith("\n") ? "" : "\n"}${escapedText}`;
1188
1189
  function messageUpdateType(role) {
1189
1190
  return role === "assistant" ? "agent_message_chunk" : "user_message_chunk";
1190
1191
  }
1191
- function toolMeta(toolName, toolResponse) {
1192
- return toolResponse ? { claudeCode: { toolName, toolResponse } } : { claudeCode: { toolName } };
1192
+ function toolMeta(toolName, toolResponse, parentToolCallId) {
1193
+ const meta = { toolName };
1194
+ if (toolResponse !== void 0) meta.toolResponse = toolResponse;
1195
+ if (parentToolCallId) meta.parentToolCallId = parentToolCallId;
1196
+ return { claudeCode: meta };
1193
1197
  }
1194
- function handleTextChunk(chunk, role) {
1195
- return {
1198
+ function handleTextChunk(chunk, role, parentToolCallId) {
1199
+ const update = {
1196
1200
  sessionUpdate: messageUpdateType(role),
1197
1201
  content: text(chunk.text)
1198
1202
  };
1203
+ if (parentToolCallId) {
1204
+ update._meta = toolMeta(
1205
+ "__text__",
1206
+ void 0,
1207
+ parentToolCallId
1208
+ );
1209
+ }
1210
+ return update;
1199
1211
  }
1200
1212
  function handleImageChunk(chunk, role) {
1201
1213
  return {
@@ -1207,11 +1219,19 @@ function handleImageChunk(chunk, role) {
1207
1219
  )
1208
1220
  };
1209
1221
  }
1210
- function handleThinkingChunk(chunk) {
1211
- return {
1222
+ function handleThinkingChunk(chunk, parentToolCallId) {
1223
+ const update = {
1212
1224
  sessionUpdate: "agent_thought_chunk",
1213
1225
  content: text(chunk.thinking)
1214
1226
  };
1227
+ if (parentToolCallId) {
1228
+ update._meta = toolMeta(
1229
+ "__thinking__",
1230
+ void 0,
1231
+ parentToolCallId
1232
+ );
1233
+ }
1234
+ return update;
1215
1235
  }
1216
1236
  function handleToolUseChunk(chunk, ctx) {
1217
1237
  ctx.toolUseCache[chunk.id] = chunk;
@@ -1232,7 +1252,7 @@ function handleToolUseChunk(chunk, ctx) {
1232
1252
  await ctx.client.sessionUpdate({
1233
1253
  sessionId: ctx.sessionId,
1234
1254
  update: {
1235
- _meta: toolMeta(toolUse.name, toolResponse),
1255
+ _meta: toolMeta(toolUse.name, toolResponse, ctx.parentToolCallId),
1236
1256
  toolCallId: toolUseId,
1237
1257
  sessionUpdate: "tool_call_update"
1238
1258
  }
@@ -1250,7 +1270,7 @@ function handleToolUseChunk(chunk, ctx) {
1250
1270
  } catch {
1251
1271
  }
1252
1272
  return {
1253
- _meta: toolMeta(chunk.name),
1273
+ _meta: toolMeta(chunk.name, void 0, ctx.parentToolCallId),
1254
1274
  toolCallId: chunk.id,
1255
1275
  sessionUpdate: "tool_call",
1256
1276
  rawInput,
@@ -1270,7 +1290,7 @@ function handleToolResultChunk(chunk, ctx) {
1270
1290
  return null;
1271
1291
  }
1272
1292
  return {
1273
- _meta: toolMeta(toolUse.name),
1293
+ _meta: toolMeta(toolUse.name, void 0, ctx.parentToolCallId),
1274
1294
  toolCallId: chunk.tool_use_id,
1275
1295
  sessionUpdate: "tool_call_update",
1276
1296
  status: chunk.is_error ? "failed" : "completed",
@@ -1284,12 +1304,12 @@ function processContentChunk(chunk, role, ctx) {
1284
1304
  switch (chunk.type) {
1285
1305
  case "text":
1286
1306
  case "text_delta":
1287
- return handleTextChunk(chunk, role);
1307
+ return handleTextChunk(chunk, role, ctx.parentToolCallId);
1288
1308
  case "image":
1289
1309
  return handleImageChunk(chunk, role);
1290
1310
  case "thinking":
1291
1311
  case "thinking_delta":
1292
- return handleThinkingChunk(chunk);
1312
+ return handleThinkingChunk(chunk, ctx.parentToolCallId);
1293
1313
  case "tool_use":
1294
1314
  case "server_tool_use":
1295
1315
  case "mcp_tool_use":
@@ -1319,24 +1339,28 @@ function processContentChunk(chunk, role, ctx) {
1319
1339
  return null;
1320
1340
  }
1321
1341
  }
1322
- function toAcpNotifications(content, role, sessionId, toolUseCache, fileContentCache, client, logger) {
1342
+ function toAcpNotifications(content, role, sessionId, toolUseCache, fileContentCache, client, logger, parentToolCallId) {
1323
1343
  if (typeof content === "string") {
1324
- return [
1325
- {
1326
- sessionId,
1327
- update: {
1328
- sessionUpdate: messageUpdateType(role),
1329
- content: text(content)
1330
- }
1331
- }
1332
- ];
1344
+ const update = {
1345
+ sessionUpdate: messageUpdateType(role),
1346
+ content: text(content)
1347
+ };
1348
+ if (parentToolCallId) {
1349
+ update._meta = toolMeta(
1350
+ "__text__",
1351
+ void 0,
1352
+ parentToolCallId
1353
+ );
1354
+ }
1355
+ return [{ sessionId, update }];
1333
1356
  }
1334
1357
  const ctx = {
1335
1358
  sessionId,
1336
1359
  toolUseCache,
1337
1360
  fileContentCache,
1338
1361
  client,
1339
- logger
1362
+ logger,
1363
+ parentToolCallId
1340
1364
  };
1341
1365
  const output = [];
1342
1366
  for (const chunk of content) {
@@ -1347,7 +1371,7 @@ function toAcpNotifications(content, role, sessionId, toolUseCache, fileContentC
1347
1371
  }
1348
1372
  return output;
1349
1373
  }
1350
- function streamEventToAcpNotifications(message, sessionId, toolUseCache, fileContentCache, client, logger) {
1374
+ function streamEventToAcpNotifications(message, sessionId, toolUseCache, fileContentCache, client, logger, parentToolCallId) {
1351
1375
  const event = message.event;
1352
1376
  switch (event.type) {
1353
1377
  case "content_block_start":
@@ -1358,7 +1382,8 @@ function streamEventToAcpNotifications(message, sessionId, toolUseCache, fileCon
1358
1382
  toolUseCache,
1359
1383
  fileContentCache,
1360
1384
  client,
1361
- logger
1385
+ logger,
1386
+ parentToolCallId
1362
1387
  );
1363
1388
  case "content_block_delta":
1364
1389
  return toAcpNotifications(
@@ -1368,7 +1393,8 @@ function streamEventToAcpNotifications(message, sessionId, toolUseCache, fileCon
1368
1393
  toolUseCache,
1369
1394
  fileContentCache,
1370
1395
  client,
1371
- logger
1396
+ logger,
1397
+ parentToolCallId
1372
1398
  );
1373
1399
  case "message_start":
1374
1400
  case "message_delta":
@@ -1481,13 +1507,15 @@ function handleResultMessage(message, context) {
1481
1507
  }
1482
1508
  async function handleStreamEvent(message, context) {
1483
1509
  const { sessionId, client, toolUseCache, fileContentCache, logger } = context;
1510
+ const parentToolCallId = message.parent_tool_use_id ?? void 0;
1484
1511
  for (const notification of streamEventToAcpNotifications(
1485
1512
  message,
1486
1513
  sessionId,
1487
1514
  toolUseCache,
1488
1515
  fileContentCache,
1489
1516
  client,
1490
- logger
1517
+ logger,
1518
+ parentToolCallId
1491
1519
  )) {
1492
1520
  await client.sessionUpdate(notification);
1493
1521
  context.session.notificationHistory.push(notification);
@@ -1539,6 +1567,7 @@ async function handleUserAssistantMessage(message, context) {
1539
1567
  }
1540
1568
  const content = message.message.content;
1541
1569
  const contentToProcess = filterMessageContent(content);
1570
+ const parentToolCallId = "parent_tool_use_id" in message ? message.parent_tool_use_id ?? void 0 : void 0;
1542
1571
  for (const notification of toAcpNotifications(
1543
1572
  contentToProcess,
1544
1573
  message.message.role,
@@ -1546,7 +1575,8 @@ async function handleUserAssistantMessage(message, context) {
1546
1575
  toolUseCache,
1547
1576
  fileContentCache,
1548
1577
  client,
1549
- logger
1578
+ logger,
1579
+ parentToolCallId
1550
1580
  )) {
1551
1581
  await client.sessionUpdate(notification);
1552
1582
  session.notificationHistory.push(notification);