@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.
@@ -29,9 +29,11 @@ declare class AgentServer {
29
29
  private executeCommand;
30
30
  private initializeSession;
31
31
  private sendInitialTaskMessage;
32
+ private buildCloudSystemPrompt;
32
33
  private signalTaskComplete;
33
34
  private configureEnvironment;
34
35
  private createCloudClient;
36
+ private detectAndAttachPrUrl;
35
37
  private cleanupSession;
36
38
  private captureTreeState;
37
39
  private broadcastEvent;
@@ -1183,7 +1183,7 @@ import { v7 as uuidv7 } from "uuid";
1183
1183
  // package.json
1184
1184
  var package_default = {
1185
1185
  name: "@posthog/agent",
1186
- version: "2.1.13",
1186
+ version: "2.1.16",
1187
1187
  repository: "https://github.com/PostHog/twig",
1188
1188
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
1189
1189
  exports: {
@@ -3688,6 +3688,10 @@ function spawnCodexProcess(options) {
3688
3688
  env.POSTHOG_GATEWAY_API_KEY = options.apiKey;
3689
3689
  }
3690
3690
  const { command, args } = findCodexBinary(options);
3691
+ if (options.binaryPath && existsSync3(options.binaryPath)) {
3692
+ const binDir = options.binaryPath.replace(/\/[^/]+$/, "");
3693
+ env.PATH = `${binDir}:${env.PATH ?? ""}`;
3694
+ }
3691
3695
  logger.info("Spawning codex-acp process", {
3692
3696
  command,
3693
3697
  args,
@@ -10310,7 +10314,11 @@ var AgentServer = class {
10310
10314
  await clientConnection.newSession({
10311
10315
  cwd: this.config.repositoryPath,
10312
10316
  mcpServers: [],
10313
- _meta: { sessionId: payload.run_id }
10317
+ _meta: {
10318
+ sessionId: payload.run_id,
10319
+ taskRunId: payload.run_id,
10320
+ systemPrompt: { append: this.buildCloudSystemPrompt() }
10321
+ }
10314
10322
  });
10315
10323
  this.session = {
10316
10324
  payload,
@@ -10322,6 +10330,11 @@ var AgentServer = class {
10322
10330
  logWriter
10323
10331
  };
10324
10332
  this.logger.info("Session initialized successfully");
10333
+ this.posthogAPI.updateTaskRun(payload.task_id, payload.run_id, {
10334
+ status: "in_progress"
10335
+ }).catch(
10336
+ (err) => this.logger.warn("Failed to set task run to in_progress", err)
10337
+ );
10325
10338
  await this.sendInitialTaskMessage(payload);
10326
10339
  }
10327
10340
  async sendInitialTaskMessage(payload) {
@@ -10358,6 +10371,19 @@ var AgentServer = class {
10358
10371
  }
10359
10372
  }
10360
10373
  }
10374
+ buildCloudSystemPrompt() {
10375
+ return `
10376
+ # Cloud Task Execution
10377
+
10378
+ After completing the requested changes:
10379
+ 1. Create a new branch with a descriptive name based on the work done
10380
+ 2. Stage and commit all changes with a clear commit message
10381
+ 3. Push the branch to origin
10382
+ 4. Create a pull request using \`gh pr create\` with a descriptive title and body
10383
+
10384
+ Important: Always create the PR. Do not ask for confirmation.
10385
+ `;
10386
+ }
10361
10387
  async signalTaskComplete(payload, stopReason) {
10362
10388
  const status = stopReason === "cancelled" ? "cancelled" : stopReason === "error" ? "failed" : "completed";
10363
10389
  try {
@@ -10418,10 +10444,70 @@ var AgentServer = class {
10418
10444
  if ((toolName === "Write" || toolName === "Edit") && toolResponse?.filePath) {
10419
10445
  await this.captureTreeState();
10420
10446
  }
10447
+ if (toolName && (toolName.includes("Bash") || toolName.includes("bash"))) {
10448
+ this.detectAndAttachPrUrl(payload, params.update);
10449
+ }
10421
10450
  }
10422
10451
  }
10423
10452
  };
10424
10453
  }
10454
+ detectAndAttachPrUrl(payload, update) {
10455
+ try {
10456
+ const meta = update?._meta?.claudeCode;
10457
+ const toolResponse = meta?.toolResponse;
10458
+ let textToSearch = "";
10459
+ if (toolResponse) {
10460
+ if (typeof toolResponse === "string") {
10461
+ textToSearch = toolResponse;
10462
+ } else if (typeof toolResponse === "object" && toolResponse !== null) {
10463
+ const respObj = toolResponse;
10464
+ textToSearch = String(respObj.stdout || "") + String(respObj.stderr || "");
10465
+ if (!textToSearch && respObj.output) {
10466
+ textToSearch = String(respObj.output);
10467
+ }
10468
+ }
10469
+ }
10470
+ const content = update?.content;
10471
+ if (Array.isArray(content)) {
10472
+ for (const item of content) {
10473
+ if (item.type === "text" && item.text) {
10474
+ textToSearch += ` ${item.text}`;
10475
+ }
10476
+ }
10477
+ }
10478
+ if (!textToSearch) return;
10479
+ const prUrlMatch = textToSearch.match(
10480
+ /https:\/\/github\.com\/[^/]+\/[^/]+\/pull\/\d+/
10481
+ );
10482
+ if (!prUrlMatch) return;
10483
+ const prUrl = prUrlMatch[0];
10484
+ this.logger.info("Detected PR URL in bash output", {
10485
+ runId: payload.run_id,
10486
+ prUrl
10487
+ });
10488
+ this.posthogAPI.updateTaskRun(payload.task_id, payload.run_id, {
10489
+ output: { pr_url: prUrl }
10490
+ }).then(() => {
10491
+ this.logger.info("PR URL attached to task run", {
10492
+ taskId: payload.task_id,
10493
+ runId: payload.run_id,
10494
+ prUrl
10495
+ });
10496
+ }).catch((err) => {
10497
+ this.logger.error("Failed to attach PR URL to task run", {
10498
+ taskId: payload.task_id,
10499
+ runId: payload.run_id,
10500
+ prUrl,
10501
+ error: err
10502
+ });
10503
+ });
10504
+ } catch (err) {
10505
+ this.logger.debug("Error in PR URL detection", {
10506
+ runId: payload.run_id,
10507
+ error: err
10508
+ });
10509
+ }
10510
+ }
10425
10511
  async cleanupSession() {
10426
10512
  if (!this.session) return;
10427
10513
  this.logger.info("Cleaning up session");