@posthog/agent 2.3.370 → 2.3.386

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.
@@ -80,9 +80,6 @@ declare class AgentServer {
80
80
  private classifyAndSignalFailure;
81
81
  private sendInitialTaskMessage;
82
82
  private sendResumeMessage;
83
- private static RESUME_HISTORY_TOKEN_BUDGET;
84
- private static TOOL_RESULT_MAX_CHARS;
85
- private formatConversationForResume;
86
83
  private getInitialPromptOverride;
87
84
  private getPendingUserPrompt;
88
85
  private getClearedPendingUserState;
@@ -8605,7 +8605,7 @@ import { z as z4 } from "zod";
8605
8605
  // package.json
8606
8606
  var package_default = {
8607
8607
  name: "@posthog/agent",
8608
- version: "2.3.370",
8608
+ version: "2.3.386",
8609
8609
  repository: "https://github.com/PostHog/code",
8610
8610
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
8611
8611
  exports: {
@@ -8661,6 +8661,14 @@ var package_default = {
8661
8661
  types: "./dist/execution-mode.d.ts",
8662
8662
  import: "./dist/execution-mode.js"
8663
8663
  },
8664
+ "./resume": {
8665
+ types: "./dist/resume.d.ts",
8666
+ import: "./dist/resume.js"
8667
+ },
8668
+ "./tree-tracker": {
8669
+ types: "./dist/tree-tracker.d.ts",
8670
+ import: "./dist/tree-tracker.js"
8671
+ },
8664
8672
  "./server": {
8665
8673
  types: "./dist/server/agent-server.d.ts",
8666
8674
  import: "./dist/server/agent-server.js"
@@ -16440,14 +16448,6 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
16440
16448
  return this.listSessions(params);
16441
16449
  }
16442
16450
  async prompt(params) {
16443
- this.session.cancelled = false;
16444
- this.session.interruptReason = void 0;
16445
- this.session.accumulatedUsage = {
16446
- inputTokens: 0,
16447
- outputTokens: 0,
16448
- cachedReadTokens: 0,
16449
- cachedWriteTokens: 0
16450
- };
16451
16451
  const userMessage = promptToClaude(params);
16452
16452
  const promptUuid = randomUUID();
16453
16453
  userMessage.uuid = promptUuid;
@@ -16483,6 +16483,14 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
16483
16483
  } else {
16484
16484
  this.session.input.push(userMessage);
16485
16485
  }
16486
+ this.session.cancelled = false;
16487
+ this.session.interruptReason = void 0;
16488
+ this.session.accumulatedUsage = {
16489
+ inputTokens: 0,
16490
+ outputTokens: 0,
16491
+ cachedReadTokens: 0,
16492
+ cachedWriteTokens: 0
16493
+ };
16486
16494
  await this.broadcastUserMessage(params);
16487
16495
  this.session.promptRunning = true;
16488
16496
  let handedOff = false;
@@ -16688,9 +16696,6 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
16688
16696
  }
16689
16697
  case "user":
16690
16698
  case "assistant": {
16691
- if (this.session.cancelled) {
16692
- break;
16693
- }
16694
16699
  if (message.type === "user" && "uuid" in message && message.uuid) {
16695
16700
  if (message.uuid === promptUuid) {
16696
16701
  promptReplayed = true;
@@ -16703,9 +16708,14 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
16703
16708
  pending.resolve(false);
16704
16709
  this.session.pendingMessages.delete(message.uuid);
16705
16710
  handedOff = true;
16706
- return { stopReason: "end_turn" };
16711
+ return {
16712
+ stopReason: this.session.cancelled ? "cancelled" : "end_turn"
16713
+ };
16707
16714
  }
16708
16715
  }
16716
+ if (this.session.cancelled) {
16717
+ break;
16718
+ }
16709
16719
  if ("isReplay" in message && message.isReplay) {
16710
16720
  break;
16711
16721
  }
@@ -18313,45 +18323,6 @@ function createCodexConnection(config) {
18313
18323
  };
18314
18324
  }
18315
18325
 
18316
- // src/adapters/claude/session/jsonl-hydration.ts
18317
- import { randomUUID as randomUUID2 } from "crypto";
18318
- import * as fs10 from "fs/promises";
18319
- import * as os6 from "os";
18320
- import * as path12 from "path";
18321
- var CHARS_PER_TOKEN = 4;
18322
- var DEFAULT_MAX_TOKENS = 15e4;
18323
- function estimateTurnTokens(turn) {
18324
- let chars = 0;
18325
- for (const block of turn.content) {
18326
- if ("text" in block && typeof block.text === "string") {
18327
- chars += block.text.length;
18328
- }
18329
- }
18330
- if (turn.toolCalls) {
18331
- for (const tc of turn.toolCalls) {
18332
- chars += JSON.stringify(tc.input ?? "").length;
18333
- if (tc.result !== void 0) {
18334
- chars += typeof tc.result === "string" ? tc.result.length : JSON.stringify(tc.result).length;
18335
- }
18336
- }
18337
- }
18338
- return Math.ceil(chars / CHARS_PER_TOKEN);
18339
- }
18340
- function selectRecentTurns(turns, maxTokens = DEFAULT_MAX_TOKENS) {
18341
- let budget = maxTokens;
18342
- let startIndex = turns.length;
18343
- for (let i2 = turns.length - 1; i2 >= 0; i2--) {
18344
- const cost = estimateTurnTokens(turns[i2]);
18345
- if (cost > budget) break;
18346
- budget -= cost;
18347
- startIndex = i2;
18348
- }
18349
- while (startIndex < turns.length && turns[startIndex].role !== "user") {
18350
- startIndex++;
18351
- }
18352
- return turns.slice(startIndex);
18353
- }
18354
-
18355
18326
  // src/utils/gateway.ts
18356
18327
  function getGatewayBaseUrl(posthogHost) {
18357
18328
  const url = new URL(posthogHost);
@@ -18553,6 +18524,45 @@ var PostHogAPIClient = class {
18553
18524
  }
18554
18525
  };
18555
18526
 
18527
+ // src/adapters/claude/session/jsonl-hydration.ts
18528
+ import { randomUUID as randomUUID2 } from "crypto";
18529
+ import * as fs10 from "fs/promises";
18530
+ import * as os6 from "os";
18531
+ import * as path12 from "path";
18532
+ var CHARS_PER_TOKEN = 4;
18533
+ var DEFAULT_MAX_TOKENS = 15e4;
18534
+ function estimateTurnTokens(turn) {
18535
+ let chars = 0;
18536
+ for (const block of turn.content) {
18537
+ if ("text" in block && typeof block.text === "string") {
18538
+ chars += block.text.length;
18539
+ }
18540
+ }
18541
+ if (turn.toolCalls) {
18542
+ for (const tc of turn.toolCalls) {
18543
+ chars += JSON.stringify(tc.input ?? "").length;
18544
+ if (tc.result !== void 0) {
18545
+ chars += typeof tc.result === "string" ? tc.result.length : JSON.stringify(tc.result).length;
18546
+ }
18547
+ }
18548
+ }
18549
+ return Math.ceil(chars / CHARS_PER_TOKEN);
18550
+ }
18551
+ function selectRecentTurns(turns, maxTokens = DEFAULT_MAX_TOKENS) {
18552
+ let budget = maxTokens;
18553
+ let startIndex = turns.length;
18554
+ for (let i2 = turns.length - 1; i2 >= 0; i2--) {
18555
+ const cost = estimateTurnTokens(turns[i2]);
18556
+ if (cost > budget) break;
18557
+ budget -= cost;
18558
+ startIndex = i2;
18559
+ }
18560
+ while (startIndex < turns.length && turns[startIndex].role !== "user") {
18561
+ startIndex++;
18562
+ }
18563
+ return turns.slice(startIndex);
18564
+ }
18565
+
18556
18566
  // ../shared/dist/index.js
18557
18567
  var CLOUD_PROMPT_PREFIX = "__twig_cloud_prompt_v1__:";
18558
18568
  function deserializeCloudPrompt(value) {
@@ -19522,6 +19532,37 @@ async function resumeFromLog(config) {
19522
19532
  logEntryCount: result.data.logEntryCount
19523
19533
  };
19524
19534
  }
19535
+ var RESUME_HISTORY_TOKEN_BUDGET = 5e4;
19536
+ var TOOL_RESULT_MAX_CHARS = 2e3;
19537
+ function formatConversationForResume(conversation) {
19538
+ const selected = selectRecentTurns(conversation, RESUME_HISTORY_TOKEN_BUDGET);
19539
+ const parts2 = [];
19540
+ if (selected.length < conversation.length) {
19541
+ parts2.push(
19542
+ `*(${conversation.length - selected.length} earlier turns omitted)*`
19543
+ );
19544
+ }
19545
+ for (const turn of selected) {
19546
+ const role = turn.role === "user" ? "User" : "Assistant";
19547
+ const textParts = turn.content.filter((block) => block.type === "text").map((block) => block.text);
19548
+ if (textParts.length > 0) {
19549
+ parts2.push(`**${role}**: ${textParts.join("\n")}`);
19550
+ }
19551
+ if (turn.toolCalls?.length) {
19552
+ const toolSummary = turn.toolCalls.map((tc) => {
19553
+ let resultStr = "";
19554
+ if (tc.result !== void 0) {
19555
+ const raw = typeof tc.result === "string" ? tc.result : JSON.stringify(tc.result);
19556
+ resultStr = raw.length > TOOL_RESULT_MAX_CHARS ? ` \u2192 ${raw.substring(0, TOOL_RESULT_MAX_CHARS)}...(truncated)` : ` \u2192 ${raw}`;
19557
+ }
19558
+ return ` - ${tc.toolName}${resultStr}`;
19559
+ }).join("\n");
19560
+ parts2.push(`**${role} (tools)**:
19561
+ ${toolSummary}`);
19562
+ }
19563
+ }
19564
+ return parts2.join("\n\n");
19565
+ }
19525
19566
 
19526
19567
  // src/session-log-writer.ts
19527
19568
  import fs12 from "fs";
@@ -20107,7 +20148,7 @@ function getTaskRunStateString(taskRun, key) {
20107
20148
  const value = state[key];
20108
20149
  return typeof value === "string" ? value : null;
20109
20150
  }
20110
- var AgentServer = class _AgentServer {
20151
+ var AgentServer = class {
20111
20152
  config;
20112
20153
  logger;
20113
20154
  server = null;
@@ -20832,7 +20873,7 @@ var AgentServer = class _AgentServer {
20832
20873
  async sendResumeMessage(payload, taskRun) {
20833
20874
  if (!this.session || !this.resumeState) return;
20834
20875
  try {
20835
- const conversationSummary = this.formatConversationForResume(
20876
+ const conversationSummary = formatConversationForResume(
20836
20877
  this.resumeState.conversation
20837
20878
  );
20838
20879
  const pendingUserPrompt = await this.getPendingUserPrompt(taskRun);
@@ -20903,40 +20944,6 @@ Continue from where you left off. The user is waiting for your response.`
20903
20944
  await this.classifyAndSignalFailure(payload, "resume", error);
20904
20945
  }
20905
20946
  }
20906
- static RESUME_HISTORY_TOKEN_BUDGET = 5e4;
20907
- static TOOL_RESULT_MAX_CHARS = 2e3;
20908
- formatConversationForResume(conversation) {
20909
- const selected = selectRecentTurns(
20910
- conversation,
20911
- _AgentServer.RESUME_HISTORY_TOKEN_BUDGET
20912
- );
20913
- const parts2 = [];
20914
- if (selected.length < conversation.length) {
20915
- parts2.push(
20916
- `*(${conversation.length - selected.length} earlier turns omitted)*`
20917
- );
20918
- }
20919
- for (const turn of selected) {
20920
- const role = turn.role === "user" ? "User" : "Assistant";
20921
- const textParts = turn.content.filter((block) => block.type === "text").map((block) => block.text);
20922
- if (textParts.length > 0) {
20923
- parts2.push(`**${role}**: ${textParts.join("\n")}`);
20924
- }
20925
- if (turn.toolCalls?.length) {
20926
- const toolSummary = turn.toolCalls.map((tc) => {
20927
- let resultStr = "";
20928
- if (tc.result !== void 0) {
20929
- const raw = typeof tc.result === "string" ? tc.result : JSON.stringify(tc.result);
20930
- resultStr = raw.length > _AgentServer.TOOL_RESULT_MAX_CHARS ? ` \u2192 ${raw.substring(0, _AgentServer.TOOL_RESULT_MAX_CHARS)}...(truncated)` : ` \u2192 ${raw}`;
20931
- }
20932
- return ` - ${tc.toolName}${resultStr}`;
20933
- }).join("\n");
20934
- parts2.push(`**${role} (tools)**:
20935
- ${toolSummary}`);
20936
- }
20937
- }
20938
- return parts2.join("\n\n");
20939
- }
20940
20947
  getInitialPromptOverride(taskRun) {
20941
20948
  const state = taskRun.state;
20942
20949
  const override = state?.initial_prompt_override;