@posthog/agent 2.1.87 → 2.1.98

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.
@@ -901,6 +901,123 @@ var import_sdk4 = require("@agentclientprotocol/sdk");
901
901
  var import_node_server = require("@hono/node-server");
902
902
  var import_hono = require("hono");
903
903
 
904
+ // package.json
905
+ var package_default = {
906
+ name: "@posthog/agent",
907
+ version: "2.1.98",
908
+ repository: "https://github.com/PostHog/twig",
909
+ description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
910
+ exports: {
911
+ ".": {
912
+ types: "./dist/index.d.ts",
913
+ import: "./dist/index.js"
914
+ },
915
+ "./agent": {
916
+ types: "./dist/agent.d.ts",
917
+ import: "./dist/agent.js"
918
+ },
919
+ "./gateway-models": {
920
+ types: "./dist/gateway-models.d.ts",
921
+ import: "./dist/gateway-models.js"
922
+ },
923
+ "./posthog-api": {
924
+ types: "./dist/posthog-api.d.ts",
925
+ import: "./dist/posthog-api.js"
926
+ },
927
+ "./types": {
928
+ types: "./dist/types.d.ts",
929
+ import: "./dist/types.js"
930
+ },
931
+ "./adapters/claude/questions/utils": {
932
+ types: "./dist/adapters/claude/questions/utils.d.ts",
933
+ import: "./dist/adapters/claude/questions/utils.js"
934
+ },
935
+ "./adapters/claude/permissions/permission-options": {
936
+ types: "./dist/adapters/claude/permissions/permission-options.d.ts",
937
+ import: "./dist/adapters/claude/permissions/permission-options.js"
938
+ },
939
+ "./adapters/claude/tools": {
940
+ types: "./dist/adapters/claude/tools.d.ts",
941
+ import: "./dist/adapters/claude/tools.js"
942
+ },
943
+ "./adapters/claude/conversion/tool-use-to-acp": {
944
+ types: "./dist/adapters/claude/conversion/tool-use-to-acp.d.ts",
945
+ import: "./dist/adapters/claude/conversion/tool-use-to-acp.js"
946
+ },
947
+ "./server": {
948
+ types: "./dist/server/agent-server.d.ts",
949
+ import: "./dist/server/agent-server.js"
950
+ }
951
+ },
952
+ bin: {
953
+ "agent-server": "./dist/server/bin.cjs"
954
+ },
955
+ type: "module",
956
+ keywords: [
957
+ "posthog",
958
+ "claude",
959
+ "agent",
960
+ "ai",
961
+ "git",
962
+ "typescript"
963
+ ],
964
+ author: "PostHog",
965
+ license: "SEE LICENSE IN LICENSE",
966
+ scripts: {
967
+ build: "rm -rf dist && tsup",
968
+ dev: "tsup --watch",
969
+ test: "vitest run",
970
+ "test:watch": "vitest",
971
+ typecheck: "pnpm exec tsc --noEmit",
972
+ prepublishOnly: "pnpm run build",
973
+ clean: "rm -rf dist .turbo"
974
+ },
975
+ engines: {
976
+ node: ">=20.0.0"
977
+ },
978
+ devDependencies: {
979
+ "@posthog/shared": "workspace:*",
980
+ "@twig/git": "workspace:*",
981
+ "@types/bun": "latest",
982
+ "@types/tar": "^6.1.13",
983
+ minimatch: "^10.0.3",
984
+ msw: "^2.12.7",
985
+ tsup: "^8.5.1",
986
+ tsx: "^4.20.6",
987
+ typescript: "^5.5.0",
988
+ vitest: "^2.1.8"
989
+ },
990
+ dependencies: {
991
+ "@agentclientprotocol/sdk": "^0.14.0",
992
+ "@anthropic-ai/claude-agent-sdk": "0.2.42",
993
+ "@anthropic-ai/sdk": "^0.71.0",
994
+ "@hono/node-server": "^1.19.9",
995
+ "@modelcontextprotocol/sdk": "^1.25.3",
996
+ "@opentelemetry/api-logs": "^0.208.0",
997
+ "@opentelemetry/exporter-logs-otlp-http": "^0.208.0",
998
+ "@opentelemetry/resources": "^2.0.0",
999
+ "@opentelemetry/sdk-logs": "^0.208.0",
1000
+ "@opentelemetry/semantic-conventions": "^1.28.0",
1001
+ "@types/jsonwebtoken": "^9.0.10",
1002
+ commander: "^14.0.2",
1003
+ hono: "^4.11.7",
1004
+ jsonwebtoken: "^9.0.2",
1005
+ tar: "^7.5.0",
1006
+ uuid: "13.0.0",
1007
+ "yoga-wasm-web": "^0.3.3",
1008
+ zod: "^3.24.1"
1009
+ },
1010
+ files: [
1011
+ "dist/**/*",
1012
+ "src/**/*",
1013
+ "README.md",
1014
+ "CLAUDE.md"
1015
+ ],
1016
+ publishConfig: {
1017
+ access: "public"
1018
+ }
1019
+ };
1020
+
904
1021
  // src/acp-extensions.ts
905
1022
  var POSTHOG_NOTIFICATIONS = {
906
1023
  /** Git branch was created for a task */
@@ -1172,123 +1289,6 @@ var import_sdk2 = require("@agentclientprotocol/sdk");
1172
1289
  var import_claude_agent_sdk = require("@anthropic-ai/claude-agent-sdk");
1173
1290
  var import_uuid = require("uuid");
1174
1291
 
1175
- // package.json
1176
- var package_default = {
1177
- name: "@posthog/agent",
1178
- version: "2.1.87",
1179
- repository: "https://github.com/PostHog/twig",
1180
- description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
1181
- exports: {
1182
- ".": {
1183
- types: "./dist/index.d.ts",
1184
- import: "./dist/index.js"
1185
- },
1186
- "./agent": {
1187
- types: "./dist/agent.d.ts",
1188
- import: "./dist/agent.js"
1189
- },
1190
- "./gateway-models": {
1191
- types: "./dist/gateway-models.d.ts",
1192
- import: "./dist/gateway-models.js"
1193
- },
1194
- "./posthog-api": {
1195
- types: "./dist/posthog-api.d.ts",
1196
- import: "./dist/posthog-api.js"
1197
- },
1198
- "./types": {
1199
- types: "./dist/types.d.ts",
1200
- import: "./dist/types.js"
1201
- },
1202
- "./adapters/claude/questions/utils": {
1203
- types: "./dist/adapters/claude/questions/utils.d.ts",
1204
- import: "./dist/adapters/claude/questions/utils.js"
1205
- },
1206
- "./adapters/claude/permissions/permission-options": {
1207
- types: "./dist/adapters/claude/permissions/permission-options.d.ts",
1208
- import: "./dist/adapters/claude/permissions/permission-options.js"
1209
- },
1210
- "./adapters/claude/tools": {
1211
- types: "./dist/adapters/claude/tools.d.ts",
1212
- import: "./dist/adapters/claude/tools.js"
1213
- },
1214
- "./adapters/claude/conversion/tool-use-to-acp": {
1215
- types: "./dist/adapters/claude/conversion/tool-use-to-acp.d.ts",
1216
- import: "./dist/adapters/claude/conversion/tool-use-to-acp.js"
1217
- },
1218
- "./server": {
1219
- types: "./dist/server/agent-server.d.ts",
1220
- import: "./dist/server/agent-server.js"
1221
- }
1222
- },
1223
- bin: {
1224
- "agent-server": "./dist/server/bin.cjs"
1225
- },
1226
- type: "module",
1227
- keywords: [
1228
- "posthog",
1229
- "claude",
1230
- "agent",
1231
- "ai",
1232
- "git",
1233
- "typescript"
1234
- ],
1235
- author: "PostHog",
1236
- license: "SEE LICENSE IN LICENSE",
1237
- scripts: {
1238
- build: "rm -rf dist && tsup",
1239
- dev: "tsup --watch",
1240
- test: "vitest run",
1241
- "test:watch": "vitest",
1242
- typecheck: "pnpm exec tsc --noEmit",
1243
- prepublishOnly: "pnpm run build",
1244
- clean: "rm -rf dist .turbo"
1245
- },
1246
- engines: {
1247
- node: ">=20.0.0"
1248
- },
1249
- devDependencies: {
1250
- "@posthog/shared": "workspace:*",
1251
- "@twig/git": "workspace:*",
1252
- "@types/bun": "latest",
1253
- "@types/tar": "^6.1.13",
1254
- minimatch: "^10.0.3",
1255
- msw: "^2.12.7",
1256
- tsup: "^8.5.1",
1257
- tsx: "^4.20.6",
1258
- typescript: "^5.5.0",
1259
- vitest: "^2.1.8"
1260
- },
1261
- dependencies: {
1262
- "@agentclientprotocol/sdk": "^0.14.0",
1263
- "@anthropic-ai/claude-agent-sdk": "0.2.42",
1264
- "@anthropic-ai/sdk": "^0.71.0",
1265
- "@hono/node-server": "^1.19.9",
1266
- "@modelcontextprotocol/sdk": "^1.25.3",
1267
- "@opentelemetry/api-logs": "^0.208.0",
1268
- "@opentelemetry/exporter-logs-otlp-http": "^0.208.0",
1269
- "@opentelemetry/resources": "^2.0.0",
1270
- "@opentelemetry/sdk-logs": "^0.208.0",
1271
- "@opentelemetry/semantic-conventions": "^1.28.0",
1272
- "@types/jsonwebtoken": "^9.0.10",
1273
- commander: "^14.0.2",
1274
- hono: "^4.11.7",
1275
- jsonwebtoken: "^9.0.2",
1276
- tar: "^7.5.0",
1277
- uuid: "13.0.0",
1278
- "yoga-wasm-web": "^0.3.3",
1279
- zod: "^3.24.1"
1280
- },
1281
- files: [
1282
- "dist/**/*",
1283
- "src/**/*",
1284
- "README.md",
1285
- "CLAUDE.md"
1286
- ],
1287
- publishConfig: {
1288
- access: "public"
1289
- }
1290
- };
1291
-
1292
1292
  // src/utils/common.ts
1293
1293
  async function withTimeout(operation, timeoutMs) {
1294
1294
  const timeoutPromise = new Promise(
@@ -4203,6 +4203,7 @@ function getLlmGatewayUrl(posthogHost) {
4203
4203
  }
4204
4204
 
4205
4205
  // src/posthog-api.ts
4206
+ var DEFAULT_USER_AGENT = `posthog/agent.hog.dev; version: ${package_default.version}`;
4206
4207
  var PostHogAPIClient = class {
4207
4208
  config;
4208
4209
  constructor(config) {
@@ -4215,7 +4216,8 @@ var PostHogAPIClient = class {
4215
4216
  get headers() {
4216
4217
  return {
4217
4218
  Authorization: `Bearer ${this.config.getApiKey()}`,
4218
- "Content-Type": "application/json"
4219
+ "Content-Type": "application/json",
4220
+ "User-Agent": this.config.userAgent ?? DEFAULT_USER_AGENT
4219
4221
  };
4220
4222
  }
4221
4223
  async apiRequest(endpoint, options = {}) {
@@ -10242,7 +10244,8 @@ var AgentServer = class {
10242
10244
  this.posthogAPI = new PostHogAPIClient({
10243
10245
  apiUrl: config.apiUrl,
10244
10246
  projectId: config.projectId,
10245
- getApiKey: () => config.apiKey
10247
+ getApiKey: () => config.apiKey,
10248
+ userAgent: `posthog/cloud.hog.dev; version: ${config.version ?? package_default.version}`
10246
10249
  });
10247
10250
  this.app = this.createApp();
10248
10251
  }
@@ -10450,16 +10453,18 @@ var AgentServer = class {
10450
10453
  `Processing user message: ${content.substring(0, 100)}...`
10451
10454
  );
10452
10455
  const result = await this.session.clientConnection.prompt({
10453
- sessionId: this.session.payload.run_id,
10456
+ sessionId: this.session.acpSessionId,
10454
10457
  prompt: [{ type: "text", text: content }]
10455
10458
  });
10456
10459
  return { stopReason: result.stopReason };
10457
10460
  }
10458
10461
  case POSTHOG_NOTIFICATIONS.CANCEL:
10459
10462
  case "cancel": {
10460
- this.logger.info("Cancel requested");
10463
+ this.logger.info("Cancel requested", {
10464
+ acpSessionId: this.session.acpSessionId
10465
+ });
10461
10466
  await this.session.clientConnection.cancel({
10462
- sessionId: this.session.payload.run_id
10467
+ sessionId: this.session.acpSessionId
10463
10468
  });
10464
10469
  return { cancelled: true };
10465
10470
  }
@@ -10495,7 +10500,8 @@ var AgentServer = class {
10495
10500
  const posthogAPI = new PostHogAPIClient({
10496
10501
  apiUrl: this.config.apiUrl,
10497
10502
  projectId: this.config.projectId,
10498
- getApiKey: () => this.config.apiKey
10503
+ getApiKey: () => this.config.apiKey,
10504
+ userAgent: `posthog/cloud.hog.dev; version: ${this.config.version ?? package_default.version}`
10499
10505
  });
10500
10506
  const logWriter = new SessionLogWriter({
10501
10507
  posthogAPI,
@@ -10533,7 +10539,7 @@ var AgentServer = class {
10533
10539
  protocolVersion: import_sdk4.PROTOCOL_VERSION,
10534
10540
  clientCapabilities: {}
10535
10541
  });
10536
- await clientConnection.newSession({
10542
+ const sessionResponse = await clientConnection.newSession({
10537
10543
  cwd: this.config.repositoryPath,
10538
10544
  mcpServers: [],
10539
10545
  _meta: {
@@ -10542,8 +10548,14 @@ var AgentServer = class {
10542
10548
  systemPrompt: { append: this.buildCloudSystemPrompt() }
10543
10549
  }
10544
10550
  });
10551
+ const acpSessionId = sessionResponse.sessionId;
10552
+ this.logger.info("ACP session created", {
10553
+ acpSessionId,
10554
+ runId: payload.run_id
10555
+ });
10545
10556
  this.session = {
10546
10557
  payload,
10558
+ acpSessionId,
10547
10559
  acpConnection,
10548
10560
  clientConnection,
10549
10561
  treeTracker,
@@ -10573,28 +10585,18 @@ var AgentServer = class {
10573
10585
  descriptionLength: task.description.length
10574
10586
  });
10575
10587
  const result = await this.session.clientConnection.prompt({
10576
- sessionId: payload.run_id,
10588
+ sessionId: this.session.acpSessionId,
10577
10589
  prompt: [{ type: "text", text: task.description }]
10578
10590
  });
10579
10591
  this.logger.info("Initial task message completed", {
10580
10592
  stopReason: result.stopReason
10581
10593
  });
10582
- const mode = this.getEffectiveMode(payload);
10583
- if (mode === "background") {
10584
- await this.session.logWriter.flushAll();
10585
- await this.signalTaskComplete(payload, result.stopReason);
10586
- } else {
10587
- this.logger.info("Interactive mode - staying open for conversation");
10588
- }
10589
10594
  } catch (error) {
10590
10595
  this.logger.error("Failed to send initial task message", error);
10591
- const mode = this.getEffectiveMode(payload);
10592
- if (mode === "background") {
10593
- if (this.session) {
10594
- await this.session.logWriter.flushAll();
10595
- }
10596
- await this.signalTaskComplete(payload, "error");
10596
+ if (this.session) {
10597
+ await this.session.logWriter.flushAll();
10597
10598
  }
10599
+ await this.signalTaskComplete(payload, "error");
10598
10600
  }
10599
10601
  }
10600
10602
  buildCloudSystemPrompt() {
@@ -10625,7 +10627,13 @@ Important:
10625
10627
  });
10626
10628
  }
10627
10629
  }
10628
- const status = stopReason === "cancelled" ? "cancelled" : stopReason === "error" ? "failed" : "completed";
10630
+ if (stopReason !== "error") {
10631
+ this.logger.info("Skipping status update for non-error stop reason", {
10632
+ stopReason
10633
+ });
10634
+ return;
10635
+ }
10636
+ const status = "failed";
10629
10637
  try {
10630
10638
  await this.posthogAPI.updateTaskRun(payload.task_id, payload.run_id, {
10631
10639
  status,