@posthog/agent 2.1.8 → 2.1.15

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.8",
1186
+ version: "2.1.15",
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: {
@@ -3081,7 +3081,7 @@ import * as path2 from "path";
3081
3081
  var BRANCH_NAMING_INSTRUCTIONS = `
3082
3082
  # Branch Naming
3083
3083
 
3084
- Before pushing a "workspace-*" branch to origin, rename it to something descriptive based on the work done. Do this automatically without asking the user.
3084
+ When working in a detached HEAD state, create a descriptive branch name based on the work being done before committing. Do this automatically without asking the user.
3085
3085
  `;
3086
3086
  function buildSystemPrompt(customPrompt) {
3087
3087
  const defaultPrompt = {
@@ -9084,9 +9084,9 @@ var GitOperationManagerImpl = class _GitOperationManagerImpl {
9084
9084
  const scopedGit = createGitClient(repoPath, {
9085
9085
  abortSignal: options.signal
9086
9086
  });
9087
- return operation(scopedGit.env({ GIT_OPTIONAL_LOCKS: "0" }));
9087
+ return operation(scopedGit.env({ ...process.env, GIT_OPTIONAL_LOCKS: "0" }));
9088
9088
  }
9089
- const git = state.client.env({ GIT_OPTIONAL_LOCKS: "0" });
9089
+ const git = state.client.env({ ...process.env, GIT_OPTIONAL_LOCKS: "0" });
9090
9090
  return operation(git);
9091
9091
  }
9092
9092
  async executeWrite(repoPath, operation, options) {
@@ -9103,9 +9103,9 @@ var GitOperationManagerImpl = class _GitOperationManagerImpl {
9103
9103
  const scopedGit = createGitClient(repoPath, {
9104
9104
  abortSignal: options.signal
9105
9105
  });
9106
- return await operation(scopedGit);
9106
+ return await operation(scopedGit.env(process.env));
9107
9107
  }
9108
- return await operation(state.client);
9108
+ return await operation(state.client.env(process.env));
9109
9109
  } catch (error) {
9110
9110
  if (options?.signal?.aborted) {
9111
9111
  await removeLock(repoPath).catch(() => {
@@ -10310,7 +10310,11 @@ var AgentServer = class {
10310
10310
  await clientConnection.newSession({
10311
10311
  cwd: this.config.repositoryPath,
10312
10312
  mcpServers: [],
10313
- _meta: { sessionId: payload.run_id }
10313
+ _meta: {
10314
+ sessionId: payload.run_id,
10315
+ taskRunId: payload.run_id,
10316
+ systemPrompt: { append: this.buildCloudSystemPrompt() }
10317
+ }
10314
10318
  });
10315
10319
  this.session = {
10316
10320
  payload,
@@ -10322,6 +10326,11 @@ var AgentServer = class {
10322
10326
  logWriter
10323
10327
  };
10324
10328
  this.logger.info("Session initialized successfully");
10329
+ this.posthogAPI.updateTaskRun(payload.task_id, payload.run_id, {
10330
+ status: "in_progress"
10331
+ }).catch(
10332
+ (err) => this.logger.warn("Failed to set task run to in_progress", err)
10333
+ );
10325
10334
  await this.sendInitialTaskMessage(payload);
10326
10335
  }
10327
10336
  async sendInitialTaskMessage(payload) {
@@ -10358,6 +10367,19 @@ var AgentServer = class {
10358
10367
  }
10359
10368
  }
10360
10369
  }
10370
+ buildCloudSystemPrompt() {
10371
+ return `
10372
+ # Cloud Task Execution
10373
+
10374
+ After completing the requested changes:
10375
+ 1. Create a new branch with a descriptive name based on the work done
10376
+ 2. Stage and commit all changes with a clear commit message
10377
+ 3. Push the branch to origin
10378
+ 4. Create a pull request using \`gh pr create\` with a descriptive title and body
10379
+
10380
+ Important: Always create the PR. Do not ask for confirmation.
10381
+ `;
10382
+ }
10361
10383
  async signalTaskComplete(payload, stopReason) {
10362
10384
  const status = stopReason === "cancelled" ? "cancelled" : stopReason === "error" ? "failed" : "completed";
10363
10385
  try {
@@ -10418,10 +10440,70 @@ var AgentServer = class {
10418
10440
  if ((toolName === "Write" || toolName === "Edit") && toolResponse?.filePath) {
10419
10441
  await this.captureTreeState();
10420
10442
  }
10443
+ if (toolName && (toolName.includes("Bash") || toolName.includes("bash"))) {
10444
+ this.detectAndAttachPrUrl(payload, params.update);
10445
+ }
10421
10446
  }
10422
10447
  }
10423
10448
  };
10424
10449
  }
10450
+ detectAndAttachPrUrl(payload, update) {
10451
+ try {
10452
+ const meta = update?._meta?.claudeCode;
10453
+ const toolResponse = meta?.toolResponse;
10454
+ let textToSearch = "";
10455
+ if (toolResponse) {
10456
+ if (typeof toolResponse === "string") {
10457
+ textToSearch = toolResponse;
10458
+ } else if (typeof toolResponse === "object" && toolResponse !== null) {
10459
+ const respObj = toolResponse;
10460
+ textToSearch = String(respObj.stdout || "") + String(respObj.stderr || "");
10461
+ if (!textToSearch && respObj.output) {
10462
+ textToSearch = String(respObj.output);
10463
+ }
10464
+ }
10465
+ }
10466
+ const content = update?.content;
10467
+ if (Array.isArray(content)) {
10468
+ for (const item of content) {
10469
+ if (item.type === "text" && item.text) {
10470
+ textToSearch += ` ${item.text}`;
10471
+ }
10472
+ }
10473
+ }
10474
+ if (!textToSearch) return;
10475
+ const prUrlMatch = textToSearch.match(
10476
+ /https:\/\/github\.com\/[^/]+\/[^/]+\/pull\/\d+/
10477
+ );
10478
+ if (!prUrlMatch) return;
10479
+ const prUrl = prUrlMatch[0];
10480
+ this.logger.info("Detected PR URL in bash output", {
10481
+ runId: payload.run_id,
10482
+ prUrl
10483
+ });
10484
+ this.posthogAPI.updateTaskRun(payload.task_id, payload.run_id, {
10485
+ output: { pr_url: prUrl }
10486
+ }).then(() => {
10487
+ this.logger.info("PR URL attached to task run", {
10488
+ taskId: payload.task_id,
10489
+ runId: payload.run_id,
10490
+ prUrl
10491
+ });
10492
+ }).catch((err) => {
10493
+ this.logger.error("Failed to attach PR URL to task run", {
10494
+ taskId: payload.task_id,
10495
+ runId: payload.run_id,
10496
+ prUrl,
10497
+ error: err
10498
+ });
10499
+ });
10500
+ } catch (err) {
10501
+ this.logger.debug("Error in PR URL detection", {
10502
+ runId: payload.run_id,
10503
+ error: err
10504
+ });
10505
+ }
10506
+ }
10425
10507
  async cleanupSession() {
10426
10508
  if (!this.session) return;
10427
10509
  this.logger.info("Cleaning up session");