@posthog/agent 2.3.207 → 2.3.213

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.
@@ -904,7 +904,7 @@ var import_hono = require("hono");
904
904
  // package.json
905
905
  var package_default = {
906
906
  name: "@posthog/agent",
907
- version: "2.3.207",
907
+ version: "2.3.213",
908
908
  repository: "https://github.com/PostHog/code",
909
909
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
910
910
  exports: {
@@ -4354,6 +4354,14 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
4354
4354
  if (message.subtype === "compact_boundary") {
4355
4355
  lastAssistantTotalUsage = 0;
4356
4356
  promptReplayed = true;
4357
+ await this.client.sessionUpdate({
4358
+ sessionId: params.sessionId,
4359
+ update: {
4360
+ sessionUpdate: "usage_update",
4361
+ used: 0,
4362
+ size: lastContextWindowSize
4363
+ }
4364
+ });
4357
4365
  }
4358
4366
  if (message.subtype === "local_command_output") {
4359
4367
  promptReplayed = true;
@@ -4469,7 +4477,7 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
4469
4477
  }
4470
4478
  if ("usage" in message.message && message.parent_tool_use_id === null) {
4471
4479
  const usage = message.message.usage;
4472
- lastAssistantTotalUsage = usage.input_tokens + usage.cache_read_input_tokens + usage.cache_creation_input_tokens;
4480
+ lastAssistantTotalUsage = usage.input_tokens + usage.output_tokens + usage.cache_read_input_tokens + usage.cache_creation_input_tokens;
4473
4481
  await this.client.sessionUpdate({
4474
4482
  sessionId: params.sessionId,
4475
4483
  update: {
@@ -5970,6 +5978,27 @@ var PostHogAPIClient = class {
5970
5978
  };
5971
5979
 
5972
5980
  // ../shared/dist/index.js
5981
+ var CLOUD_PROMPT_PREFIX = "__twig_cloud_prompt_v1__:";
5982
+ function deserializeCloudPrompt(value) {
5983
+ const trimmed2 = value.trim();
5984
+ if (!trimmed2) {
5985
+ return [];
5986
+ }
5987
+ if (!trimmed2.startsWith(CLOUD_PROMPT_PREFIX)) {
5988
+ return [{ type: "text", text: trimmed2 }];
5989
+ }
5990
+ try {
5991
+ const parsed = JSON.parse(trimmed2.slice(CLOUD_PROMPT_PREFIX.length));
5992
+ if (Array.isArray(parsed.blocks) && parsed.blocks.length > 0) {
5993
+ return parsed.blocks;
5994
+ }
5995
+ } catch {
5996
+ }
5997
+ return [{ type: "text", text: trimmed2 }];
5998
+ }
5999
+ function promptBlocksToText(blocks) {
6000
+ return blocks.filter((b) => b.type === "text").map((block) => block.text).join("").trim();
6001
+ }
5973
6002
  var consoleLogger = {
5974
6003
  info: (_message, _data) => {
5975
6004
  },
@@ -12065,6 +12094,14 @@ var AsyncMutex = class {
12065
12094
  }
12066
12095
  };
12067
12096
 
12097
+ // src/server/cloud-prompt.ts
12098
+ function normalizeCloudPromptContent(content) {
12099
+ if (typeof content === "string") {
12100
+ return deserializeCloudPrompt(content);
12101
+ }
12102
+ return content;
12103
+ }
12104
+
12068
12105
  // src/server/jwt.ts
12069
12106
  var import_jsonwebtoken = __toESM(require("jsonwebtoken"), 1);
12070
12107
  var import_zod2 = require("zod");
@@ -12148,7 +12185,10 @@ var jsonRpcRequestSchema = import_v4.z.object({
12148
12185
  id: import_v4.z.union([import_v4.z.string(), import_v4.z.number()]).optional()
12149
12186
  });
12150
12187
  var userMessageParamsSchema = import_v4.z.object({
12151
- content: import_v4.z.string().min(1, "Content is required")
12188
+ content: import_v4.z.union([
12189
+ import_v4.z.string().min(1, "Content is required"),
12190
+ import_v4.z.array(import_v4.z.record(import_v4.z.string(), import_v4.z.unknown())).min(1, "Content is required")
12191
+ ])
12152
12192
  });
12153
12193
  var commandParamsSchemas = {
12154
12194
  user_message: userMessageParamsSchema,
@@ -12529,14 +12569,17 @@ var AgentServer = class _AgentServer {
12529
12569
  switch (method) {
12530
12570
  case POSTHOG_NOTIFICATIONS.USER_MESSAGE:
12531
12571
  case "user_message": {
12532
- const content = params.content;
12572
+ const prompt = normalizeCloudPromptContent(
12573
+ params.content
12574
+ );
12575
+ const promptPreview = promptBlocksToText(prompt);
12533
12576
  this.logger.info(
12534
- `Processing user message (detectedPrUrl=${this.detectedPrUrl ?? "none"}): ${content.substring(0, 100)}...`
12577
+ `Processing user message (detectedPrUrl=${this.detectedPrUrl ?? "none"}): ${promptPreview.substring(0, 100)}...`
12535
12578
  );
12536
12579
  this.session.logWriter.resetTurnMessages(this.session.payload.run_id);
12537
12580
  const result = await this.session.clientConnection.prompt({
12538
12581
  sessionId: this.session.acpSessionId,
12539
- prompt: [{ type: "text", text: content }],
12582
+ prompt,
12540
12583
  ...this.detectedPrUrl && {
12541
12584
  _meta: {
12542
12585
  prContext: `IMPORTANT \u2014 OVERRIDE PREVIOUS INSTRUCTIONS ABOUT CREATING BRANCHES/PRs.
@@ -12796,20 +12839,29 @@ You MUST NOT create a new branch, close the existing PR, or create a new PR.`
12796
12839
  try {
12797
12840
  const task = await this.posthogAPI.getTask(payload.task_id);
12798
12841
  const initialPromptOverride = taskRun ? this.getInitialPromptOverride(taskRun) : null;
12799
- const initialPrompt = initialPromptOverride ?? task.description;
12800
- if (!initialPrompt) {
12842
+ const pendingUserPrompt = this.getPendingUserPrompt(taskRun);
12843
+ let initialPrompt = [];
12844
+ if (pendingUserPrompt?.length) {
12845
+ initialPrompt = pendingUserPrompt;
12846
+ } else if (initialPromptOverride) {
12847
+ initialPrompt = [{ type: "text", text: initialPromptOverride }];
12848
+ } else if (task.description) {
12849
+ initialPrompt = [{ type: "text", text: task.description }];
12850
+ }
12851
+ if (initialPrompt.length === 0) {
12801
12852
  this.logger.warn("Task has no description, skipping initial message");
12802
12853
  return;
12803
12854
  }
12804
12855
  this.logger.info("Sending initial task message", {
12805
12856
  taskId: payload.task_id,
12806
- descriptionLength: initialPrompt.length,
12807
- usedInitialPromptOverride: !!initialPromptOverride
12857
+ descriptionLength: promptBlocksToText(initialPrompt).length,
12858
+ usedInitialPromptOverride: !!initialPromptOverride,
12859
+ usedPendingUserMessage: !!pendingUserPrompt?.length
12808
12860
  });
12809
12861
  this.session.logWriter.resetTurnMessages(payload.run_id);
12810
12862
  const result = await this.session.clientConnection.prompt({
12811
12863
  sessionId: this.session.acpSessionId,
12812
- prompt: [{ type: "text", text: initialPrompt }]
12864
+ prompt: initialPrompt
12813
12865
  });
12814
12866
  this.logger.info("Initial task message completed", {
12815
12867
  stopReason: result.stopReason
@@ -12832,11 +12884,14 @@ You MUST NOT create a new branch, close the existing PR, or create a new PR.`
12832
12884
  const conversationSummary = this.formatConversationForResume(
12833
12885
  this.resumeState.conversation
12834
12886
  );
12835
- const pendingUserMessage = this.getPendingUserMessage(taskRun);
12887
+ const pendingUserPrompt = this.getPendingUserPrompt(taskRun);
12836
12888
  const sandboxContext = this.resumeState.snapshotApplied ? `The workspace environment (all files, packages, and code changes) has been fully restored from where you left off.` : `The workspace files from the previous session were not restored (the file snapshot may have expired), so you are starting with a fresh environment. Your conversation history is fully preserved below.`;
12837
- let resumePrompt;
12838
- if (pendingUserMessage) {
12839
- resumePrompt = `You are resuming a previous conversation. ${sandboxContext}
12889
+ let resumePromptBlocks;
12890
+ if (pendingUserPrompt?.length) {
12891
+ resumePromptBlocks = [
12892
+ {
12893
+ type: "text",
12894
+ text: `You are resuming a previous conversation. ${sandboxContext}
12840
12895
 
12841
12896
  Here is the conversation history from the previous session:
12842
12897
 
@@ -12844,30 +12899,40 @@ ${conversationSummary}
12844
12899
 
12845
12900
  The user has sent a new message:
12846
12901
 
12847
- ${pendingUserMessage}
12848
-
12849
- Respond to the user's new message above. You have full context from the previous session.`;
12902
+ `
12903
+ },
12904
+ ...pendingUserPrompt,
12905
+ {
12906
+ type: "text",
12907
+ text: "\n\nRespond to the user's new message above. You have full context from the previous session."
12908
+ }
12909
+ ];
12850
12910
  } else {
12851
- resumePrompt = `You are resuming a previous conversation. ${sandboxContext}
12911
+ resumePromptBlocks = [
12912
+ {
12913
+ type: "text",
12914
+ text: `You are resuming a previous conversation. ${sandboxContext}
12852
12915
 
12853
12916
  Here is the conversation history from the previous session:
12854
12917
 
12855
12918
  ${conversationSummary}
12856
12919
 
12857
- Continue from where you left off. The user is waiting for your response.`;
12920
+ Continue from where you left off. The user is waiting for your response.`
12921
+ }
12922
+ ];
12858
12923
  }
12859
12924
  this.logger.info("Sending resume message", {
12860
12925
  taskId: payload.task_id,
12861
12926
  conversationTurns: this.resumeState.conversation.length,
12862
- promptLength: resumePrompt.length,
12863
- hasPendingUserMessage: !!pendingUserMessage,
12927
+ promptLength: promptBlocksToText(resumePromptBlocks).length,
12928
+ hasPendingUserMessage: !!pendingUserPrompt?.length,
12864
12929
  snapshotApplied: this.resumeState.snapshotApplied
12865
12930
  });
12866
12931
  this.resumeState = null;
12867
12932
  this.session.logWriter.resetTurnMessages(payload.run_id);
12868
12933
  const result = await this.session.clientConnection.prompt({
12869
12934
  sessionId: this.session.acpSessionId,
12870
- prompt: [{ type: "text", text: resumePrompt }]
12935
+ prompt: resumePromptBlocks
12871
12936
  });
12872
12937
  this.logger.info("Resume message completed", {
12873
12938
  stopReason: result.stopReason
@@ -12927,15 +12992,15 @@ ${toolSummary}`);
12927
12992
  const trimmed2 = override.trim();
12928
12993
  return trimmed2.length > 0 ? trimmed2 : null;
12929
12994
  }
12930
- getPendingUserMessage(taskRun) {
12995
+ getPendingUserPrompt(taskRun) {
12931
12996
  if (!taskRun) return null;
12932
12997
  const state = taskRun.state;
12933
12998
  const message = state?.pending_user_message;
12934
12999
  if (typeof message !== "string") {
12935
13000
  return null;
12936
13001
  }
12937
- const trimmed2 = message.trim();
12938
- return trimmed2.length > 0 ? trimmed2 : null;
13002
+ const prompt = deserializeCloudPrompt(message);
13003
+ return prompt.length > 0 ? prompt : null;
12939
13004
  }
12940
13005
  getResumeRunId(taskRun) {
12941
13006
  const envRunId = process.env.POSTHOG_RESUME_RUN_ID;