@posthog/agent 2.1.13 → 2.1.16

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.
@@ -1175,7 +1175,7 @@ var import_uuid = require("uuid");
1175
1175
  // package.json
1176
1176
  var package_default = {
1177
1177
  name: "@posthog/agent",
1178
- version: "2.1.13",
1178
+ version: "2.1.16",
1179
1179
  repository: "https://github.com/PostHog/twig",
1180
1180
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
1181
1181
  exports: {
@@ -3680,6 +3680,10 @@ function spawnCodexProcess(options) {
3680
3680
  env.POSTHOG_GATEWAY_API_KEY = options.apiKey;
3681
3681
  }
3682
3682
  const { command, args } = findCodexBinary(options);
3683
+ if (options.binaryPath && (0, import_node_fs.existsSync)(options.binaryPath)) {
3684
+ const binDir = options.binaryPath.replace(/\/[^/]+$/, "");
3685
+ env.PATH = `${binDir}:${env.PATH ?? ""}`;
3686
+ }
3683
3687
  logger.info("Spawning codex-acp process", {
3684
3688
  command,
3685
3689
  args,
@@ -10302,7 +10306,11 @@ var AgentServer = class {
10302
10306
  await clientConnection.newSession({
10303
10307
  cwd: this.config.repositoryPath,
10304
10308
  mcpServers: [],
10305
- _meta: { sessionId: payload.run_id }
10309
+ _meta: {
10310
+ sessionId: payload.run_id,
10311
+ taskRunId: payload.run_id,
10312
+ systemPrompt: { append: this.buildCloudSystemPrompt() }
10313
+ }
10306
10314
  });
10307
10315
  this.session = {
10308
10316
  payload,
@@ -10314,6 +10322,11 @@ var AgentServer = class {
10314
10322
  logWriter
10315
10323
  };
10316
10324
  this.logger.info("Session initialized successfully");
10325
+ this.posthogAPI.updateTaskRun(payload.task_id, payload.run_id, {
10326
+ status: "in_progress"
10327
+ }).catch(
10328
+ (err) => this.logger.warn("Failed to set task run to in_progress", err)
10329
+ );
10317
10330
  await this.sendInitialTaskMessage(payload);
10318
10331
  }
10319
10332
  async sendInitialTaskMessage(payload) {
@@ -10350,6 +10363,19 @@ var AgentServer = class {
10350
10363
  }
10351
10364
  }
10352
10365
  }
10366
+ buildCloudSystemPrompt() {
10367
+ return `
10368
+ # Cloud Task Execution
10369
+
10370
+ After completing the requested changes:
10371
+ 1. Create a new branch with a descriptive name based on the work done
10372
+ 2. Stage and commit all changes with a clear commit message
10373
+ 3. Push the branch to origin
10374
+ 4. Create a pull request using \`gh pr create\` with a descriptive title and body
10375
+
10376
+ Important: Always create the PR. Do not ask for confirmation.
10377
+ `;
10378
+ }
10353
10379
  async signalTaskComplete(payload, stopReason) {
10354
10380
  const status = stopReason === "cancelled" ? "cancelled" : stopReason === "error" ? "failed" : "completed";
10355
10381
  try {
@@ -10410,10 +10436,70 @@ var AgentServer = class {
10410
10436
  if ((toolName === "Write" || toolName === "Edit") && toolResponse?.filePath) {
10411
10437
  await this.captureTreeState();
10412
10438
  }
10439
+ if (toolName && (toolName.includes("Bash") || toolName.includes("bash"))) {
10440
+ this.detectAndAttachPrUrl(payload, params.update);
10441
+ }
10413
10442
  }
10414
10443
  }
10415
10444
  };
10416
10445
  }
10446
+ detectAndAttachPrUrl(payload, update) {
10447
+ try {
10448
+ const meta = update?._meta?.claudeCode;
10449
+ const toolResponse = meta?.toolResponse;
10450
+ let textToSearch = "";
10451
+ if (toolResponse) {
10452
+ if (typeof toolResponse === "string") {
10453
+ textToSearch = toolResponse;
10454
+ } else if (typeof toolResponse === "object" && toolResponse !== null) {
10455
+ const respObj = toolResponse;
10456
+ textToSearch = String(respObj.stdout || "") + String(respObj.stderr || "");
10457
+ if (!textToSearch && respObj.output) {
10458
+ textToSearch = String(respObj.output);
10459
+ }
10460
+ }
10461
+ }
10462
+ const content = update?.content;
10463
+ if (Array.isArray(content)) {
10464
+ for (const item of content) {
10465
+ if (item.type === "text" && item.text) {
10466
+ textToSearch += ` ${item.text}`;
10467
+ }
10468
+ }
10469
+ }
10470
+ if (!textToSearch) return;
10471
+ const prUrlMatch = textToSearch.match(
10472
+ /https:\/\/github\.com\/[^/]+\/[^/]+\/pull\/\d+/
10473
+ );
10474
+ if (!prUrlMatch) return;
10475
+ const prUrl = prUrlMatch[0];
10476
+ this.logger.info("Detected PR URL in bash output", {
10477
+ runId: payload.run_id,
10478
+ prUrl
10479
+ });
10480
+ this.posthogAPI.updateTaskRun(payload.task_id, payload.run_id, {
10481
+ output: { pr_url: prUrl }
10482
+ }).then(() => {
10483
+ this.logger.info("PR URL attached to task run", {
10484
+ taskId: payload.task_id,
10485
+ runId: payload.run_id,
10486
+ prUrl
10487
+ });
10488
+ }).catch((err) => {
10489
+ this.logger.error("Failed to attach PR URL to task run", {
10490
+ taskId: payload.task_id,
10491
+ runId: payload.run_id,
10492
+ prUrl,
10493
+ error: err
10494
+ });
10495
+ });
10496
+ } catch (err) {
10497
+ this.logger.debug("Error in PR URL detection", {
10498
+ runId: payload.run_id,
10499
+ error: err
10500
+ });
10501
+ }
10502
+ }
10417
10503
  async cleanupSession() {
10418
10504
  if (!this.session) return;
10419
10505
  this.logger.info("Cleaning up session");