@posthog/agent 2.3.172 → 2.3.187

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.
package/dist/agent.js CHANGED
@@ -245,7 +245,7 @@ import { v7 as uuidv7 } from "uuid";
245
245
  // package.json
246
246
  var package_default = {
247
247
  name: "@posthog/agent",
248
- version: "2.3.172",
248
+ version: "2.3.187",
249
249
  repository: "https://github.com/PostHog/code",
250
250
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
251
251
  exports: {
@@ -370,6 +370,44 @@ var package_default = {
370
370
  }
371
371
  };
372
372
 
373
+ // src/acp-extensions.ts
374
+ var POSTHOG_NOTIFICATIONS = {
375
+ /** Git branch was created for a task */
376
+ BRANCH_CREATED: "_posthog/branch_created",
377
+ /** Task run has started execution */
378
+ RUN_STARTED: "_posthog/run_started",
379
+ /** Task has completed (success or failure) */
380
+ TASK_COMPLETE: "_posthog/task_complete",
381
+ /** Agent finished processing a turn (prompt returned, waiting for next input) */
382
+ TURN_COMPLETE: "_posthog/turn_complete",
383
+ /** Error occurred during task execution */
384
+ ERROR: "_posthog/error",
385
+ /** Console/log output from the agent */
386
+ CONSOLE: "_posthog/console",
387
+ /** Maps taskRunId to agent's sessionId and adapter type (for resumption) */
388
+ SDK_SESSION: "_posthog/sdk_session",
389
+ /** Tree state snapshot captured (git tree hash + file archive) */
390
+ TREE_SNAPSHOT: "_posthog/tree_snapshot",
391
+ /** Agent mode changed (interactive/background) */
392
+ MODE_CHANGE: "_posthog/mode_change",
393
+ /** Request to resume a session from previous state */
394
+ SESSION_RESUME: "_posthog/session/resume",
395
+ /** User message sent from client to agent */
396
+ USER_MESSAGE: "_posthog/user_message",
397
+ /** Request to cancel current operation */
398
+ CANCEL: "_posthog/cancel",
399
+ /** Request to close the session */
400
+ CLOSE: "_posthog/close",
401
+ /** Agent status update (thinking, working, etc.) */
402
+ STATUS: "_posthog/status",
403
+ /** Task-level notification (progress, milestones) */
404
+ TASK_NOTIFICATION: "_posthog/task_notification",
405
+ /** Marks a boundary for log compaction */
406
+ COMPACT_BOUNDARY: "_posthog/compact_boundary",
407
+ /** Token usage update for a session turn */
408
+ USAGE_UPDATE: "_posthog/usage_update"
409
+ };
410
+
373
411
  // src/utils/common.ts
374
412
  async function withTimeout(operation, timeoutMs) {
375
413
  const timeoutPromise = new Promise(
@@ -2107,7 +2145,6 @@ var CODE_EXECUTION_MODES = [
2107
2145
  function getAvailableModes() {
2108
2146
  return IS_ROOT ? availableModes.filter((m) => m.id !== "bypassPermissions") : availableModes;
2109
2147
  }
2110
- var CODEX_NATIVE_MODES = ["auto", "read-only", "full-access"];
2111
2148
  var codexModes = [
2112
2149
  {
2113
2150
  id: "read-only",
@@ -3493,16 +3530,19 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
3493
3530
  }
3494
3531
  });
3495
3532
  }
3496
- await this.client.extNotification("_posthog/usage_update", {
3497
- sessionId: params.sessionId,
3498
- used: {
3499
- inputTokens: message.usage.input_tokens,
3500
- outputTokens: message.usage.output_tokens,
3501
- cachedReadTokens: message.usage.cache_read_input_tokens,
3502
- cachedWriteTokens: message.usage.cache_creation_input_tokens
3503
- },
3504
- cost: message.total_cost_usd
3505
- });
3533
+ await this.client.extNotification(
3534
+ POSTHOG_NOTIFICATIONS.USAGE_UPDATE,
3535
+ {
3536
+ sessionId: params.sessionId,
3537
+ used: {
3538
+ inputTokens: message.usage.input_tokens,
3539
+ outputTokens: message.usage.output_tokens,
3540
+ cachedReadTokens: message.usage.cache_read_input_tokens,
3541
+ cachedWriteTokens: message.usage.cache_creation_input_tokens
3542
+ },
3543
+ cost: message.total_cost_usd
3544
+ }
3545
+ );
3506
3546
  const usage = {
3507
3547
  inputTokens: this.session.accumulatedUsage.inputTokens,
3508
3548
  outputTokens: this.session.accumulatedUsage.outputTokens,
@@ -4128,47 +4168,12 @@ import {
4128
4168
  ndJsonStream
4129
4169
  } from "@agentclientprotocol/sdk";
4130
4170
 
4131
- // src/acp-extensions.ts
4132
- var POSTHOG_NOTIFICATIONS = {
4133
- /** Git branch was created for a task */
4134
- BRANCH_CREATED: "_posthog/branch_created",
4135
- /** Task run has started execution */
4136
- RUN_STARTED: "_posthog/run_started",
4137
- /** Task has completed (success or failure) */
4138
- TASK_COMPLETE: "_posthog/task_complete",
4139
- /** Agent finished processing a turn (prompt returned, waiting for next input) */
4140
- TURN_COMPLETE: "_posthog/turn_complete",
4141
- /** Error occurred during task execution */
4142
- ERROR: "_posthog/error",
4143
- /** Console/log output from the agent */
4144
- CONSOLE: "_posthog/console",
4145
- /** Maps taskRunId to agent's sessionId and adapter type (for resumption) */
4146
- SDK_SESSION: "_posthog/sdk_session",
4147
- /** Tree state snapshot captured (git tree hash + file archive) */
4148
- TREE_SNAPSHOT: "_posthog/tree_snapshot",
4149
- /** Agent mode changed (interactive/background) */
4150
- MODE_CHANGE: "_posthog/mode_change",
4151
- /** Request to resume a session from previous state */
4152
- SESSION_RESUME: "_posthog/session/resume",
4153
- /** User message sent from client to agent */
4154
- USER_MESSAGE: "_posthog/user_message",
4155
- /** Request to cancel current operation */
4156
- CANCEL: "_posthog/cancel",
4157
- /** Request to close the session */
4158
- CLOSE: "_posthog/close",
4159
- /** Agent status update (thinking, working, etc.) */
4160
- STATUS: "_posthog/status",
4161
- /** Task-level notification (progress, milestones) */
4162
- TASK_NOTIFICATION: "_posthog/task_notification",
4163
- /** Marks a boundary for log compaction */
4164
- COMPACT_BOUNDARY: "_posthog/compact_boundary"
4165
- };
4166
-
4167
4171
  // src/adapters/codex/codex-client.ts
4168
4172
  var AUTO_APPROVED_KINDS = {
4169
- auto: /* @__PURE__ */ new Set(["read", "search", "fetch", "think"]),
4170
- "read-only": /* @__PURE__ */ new Set(["read", "search", "fetch", "think"]),
4171
- "full-access": /* @__PURE__ */ new Set([
4173
+ default: /* @__PURE__ */ new Set(["read", "search", "fetch", "think"]),
4174
+ acceptEdits: /* @__PURE__ */ new Set(["read", "edit", "search", "fetch", "think"]),
4175
+ plan: /* @__PURE__ */ new Set(["read", "search", "fetch", "think"]),
4176
+ bypassPermissions: /* @__PURE__ */ new Set([
4172
4177
  "read",
4173
4178
  "edit",
4174
4179
  "delete",
@@ -4182,7 +4187,7 @@ var AUTO_APPROVED_KINDS = {
4182
4187
  ])
4183
4188
  };
4184
4189
  function shouldAutoApprove(mode, kind) {
4185
- if (mode === "full-access") return true;
4190
+ if (mode === "bypassPermissions") return true;
4186
4191
  if (!kind) return false;
4187
4192
  return AUTO_APPROVED_KINDS[mode]?.has(kind) ?? false;
4188
4193
  }
@@ -4289,7 +4294,7 @@ function createSessionState(sessionId, cwd, opts) {
4289
4294
  return {
4290
4295
  sessionId,
4291
4296
  cwd,
4292
- modeId: opts?.modeId ?? "auto",
4297
+ modeId: opts?.modeId ?? "default",
4293
4298
  modelId: opts?.modelId,
4294
4299
  configOptions: [],
4295
4300
  accumulatedUsage: {
@@ -4298,8 +4303,7 @@ function createSessionState(sessionId, cwd, opts) {
4298
4303
  cachedReadTokens: 0,
4299
4304
  cachedWriteTokens: 0
4300
4305
  },
4301
- permissionMode: opts?.permissionMode ?? "auto",
4302
- cancelled: false,
4306
+ permissionMode: opts?.permissionMode ?? "default",
4303
4307
  taskRunId: opts?.taskRunId,
4304
4308
  taskId: opts?.taskId
4305
4309
  };
@@ -4413,7 +4417,7 @@ function buildConfigArgs(options) {
4413
4417
  args.push("-c", `model="${options.model}"`);
4414
4418
  }
4415
4419
  if (options.instructions) {
4416
- const escaped = options.instructions.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
4420
+ const escaped = options.instructions.replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/"/g, '\\"');
4417
4421
  args.push("-c", `instructions="${escaped}"`);
4418
4422
  }
4419
4423
  return args;
@@ -4493,12 +4497,18 @@ function spawnCodexProcess(options) {
4493
4497
  }
4494
4498
 
4495
4499
  // src/adapters/codex/codex-agent.ts
4496
- function toPermissionMode(mode) {
4497
- if (mode && CODEX_NATIVE_MODES.includes(mode)) {
4500
+ function toCodeExecutionMode(mode) {
4501
+ if (mode && CODE_EXECUTION_MODES.includes(mode)) {
4498
4502
  return mode;
4499
4503
  }
4500
- return "auto";
4504
+ return "default";
4501
4505
  }
4506
+ var CODEX_NATIVE_MODE = {
4507
+ default: "default",
4508
+ acceptEdits: "default",
4509
+ plan: "plan",
4510
+ bypassPermissions: "default"
4511
+ };
4502
4512
  var CodexAcpAgent = class extends BaseAcpAgent {
4503
4513
  adapterName = "codex";
4504
4514
  codexProcess;
@@ -4524,25 +4534,9 @@ var CodexAcpAgent = class extends BaseAcpAgent {
4524
4534
  notificationHistory: [],
4525
4535
  cancelled: false
4526
4536
  };
4537
+ this.sessionState = createSessionState("", cwd);
4527
4538
  this.codexConnection = new ClientSideConnection(
4528
- (_agent) => createCodexClient(
4529
- this.client,
4530
- this.logger,
4531
- this.sessionState ?? {
4532
- sessionId: "",
4533
- cwd: "",
4534
- modeId: "auto",
4535
- configOptions: [],
4536
- accumulatedUsage: {
4537
- inputTokens: 0,
4538
- outputTokens: 0,
4539
- cachedReadTokens: 0,
4540
- cachedWriteTokens: 0
4541
- },
4542
- permissionMode: "auto",
4543
- cancelled: false
4544
- }
4545
- ),
4539
+ (_agent) => createCodexClient(this.client, this.logger, this.sessionState),
4546
4540
  codexStream
4547
4541
  );
4548
4542
  }
@@ -4579,7 +4573,7 @@ var CodexAcpAgent = class extends BaseAcpAgent {
4579
4573
  taskId: meta?.taskId ?? meta?.persistence?.taskId,
4580
4574
  modeId: response.modes?.currentModeId ?? "default",
4581
4575
  modelId: response.models?.currentModelId,
4582
- permissionMode: toPermissionMode(meta?.permissionMode)
4576
+ permissionMode: toCodeExecutionMode(meta?.permissionMode)
4583
4577
  });
4584
4578
  this.sessionId = response.sessionId;
4585
4579
  this.sessionState.configOptions = response.configOptions ?? [];
@@ -4598,7 +4592,10 @@ var CodexAcpAgent = class extends BaseAcpAgent {
4598
4592
  }
4599
4593
  async loadSession(params) {
4600
4594
  const response = await this.codexConnection.loadSession(params);
4601
- this.sessionState = createSessionState(params.sessionId, params.cwd);
4595
+ const meta = params._meta;
4596
+ this.sessionState = createSessionState(params.sessionId, params.cwd, {
4597
+ permissionMode: toCodeExecutionMode(meta?.permissionMode)
4598
+ });
4602
4599
  this.sessionId = params.sessionId;
4603
4600
  this.sessionState.configOptions = response.configOptions ?? [];
4604
4601
  return response;
@@ -4609,10 +4606,14 @@ var CodexAcpAgent = class extends BaseAcpAgent {
4609
4606
  cwd: params.cwd,
4610
4607
  mcpServers: params.mcpServers ?? []
4611
4608
  });
4612
- this.sessionState = createSessionState(params.sessionId, params.cwd);
4609
+ const meta = params._meta;
4610
+ this.sessionState = createSessionState(params.sessionId, params.cwd, {
4611
+ taskRunId: meta?.taskRunId,
4612
+ taskId: meta?.taskId ?? meta?.persistence?.taskId,
4613
+ permissionMode: toCodeExecutionMode(meta?.permissionMode)
4614
+ });
4613
4615
  this.sessionId = params.sessionId;
4614
4616
  this.sessionState.configOptions = loadResponse.configOptions ?? [];
4615
- const meta = params._meta;
4616
4617
  if (meta?.taskRunId) {
4617
4618
  await this.client.extNotification(POSTHOG_NOTIFICATIONS.SDK_SESSION, {
4618
4619
  taskRunId: meta.taskRunId,
@@ -4632,7 +4633,12 @@ var CodexAcpAgent = class extends BaseAcpAgent {
4632
4633
  mcpServers: params.mcpServers ?? [],
4633
4634
  _meta: params._meta
4634
4635
  });
4635
- this.sessionState = createSessionState(newResponse.sessionId, params.cwd);
4636
+ const meta = params._meta;
4637
+ this.sessionState = createSessionState(newResponse.sessionId, params.cwd, {
4638
+ taskRunId: meta?.taskRunId,
4639
+ taskId: meta?.taskId ?? meta?.persistence?.taskId,
4640
+ permissionMode: toCodeExecutionMode(meta?.permissionMode)
4641
+ });
4636
4642
  this.sessionId = newResponse.sessionId;
4637
4643
  this.sessionState.configOptions = newResponse.configOptions ?? [];
4638
4644
  return newResponse;
@@ -4641,22 +4647,14 @@ var CodexAcpAgent = class extends BaseAcpAgent {
4641
4647
  return this.codexConnection.listSessions(params);
4642
4648
  }
4643
4649
  async unstable_listSessions(params) {
4644
- return this.codexConnection.listSessions(params);
4650
+ return this.listSessions(params);
4645
4651
  }
4646
4652
  async prompt(params) {
4647
- if (this.sessionState) {
4648
- this.sessionState.cancelled = false;
4649
- this.sessionState.interruptReason = void 0;
4650
- resetUsage(this.sessionState);
4651
- }
4653
+ this.session.cancelled = false;
4654
+ this.session.interruptReason = void 0;
4655
+ resetUsage(this.sessionState);
4652
4656
  const response = await this.codexConnection.prompt(params);
4653
- if (this.sessionState && response.usage) {
4654
- this.sessionState.accumulatedUsage.inputTokens += response.usage.inputTokens ?? 0;
4655
- this.sessionState.accumulatedUsage.outputTokens += response.usage.outputTokens ?? 0;
4656
- this.sessionState.accumulatedUsage.cachedReadTokens += response.usage.cachedReadTokens ?? 0;
4657
- this.sessionState.accumulatedUsage.cachedWriteTokens += response.usage.cachedWriteTokens ?? 0;
4658
- }
4659
- if (this.sessionState?.taskRunId) {
4657
+ if (this.sessionState.taskRunId) {
4660
4658
  const { accumulatedUsage } = this.sessionState;
4661
4659
  await this.client.extNotification(POSTHOG_NOTIFICATIONS.TURN_COMPLETE, {
4662
4660
  sessionId: params.sessionId,
@@ -4670,7 +4668,7 @@ var CodexAcpAgent = class extends BaseAcpAgent {
4670
4668
  }
4671
4669
  });
4672
4670
  if (response.usage) {
4673
- await this.client.extNotification("_posthog/usage_update", {
4671
+ await this.client.extNotification(POSTHOG_NOTIFICATIONS.USAGE_UPDATE, {
4674
4672
  sessionId: params.sessionId,
4675
4673
  used: {
4676
4674
  inputTokens: response.usage.inputTokens ?? 0,
@@ -4685,38 +4683,24 @@ var CodexAcpAgent = class extends BaseAcpAgent {
4685
4683
  return response;
4686
4684
  }
4687
4685
  async interrupt() {
4688
- if (this.sessionState) {
4689
- this.sessionState.cancelled = true;
4690
- }
4691
4686
  await this.codexConnection.cancel({
4692
4687
  sessionId: this.sessionId
4693
4688
  });
4694
4689
  }
4695
- async cancel(params) {
4696
- if (this.sessionState) {
4697
- this.sessionState.cancelled = true;
4698
- const meta = params._meta;
4699
- if (meta?.interruptReason) {
4700
- this.sessionState.interruptReason = meta.interruptReason;
4701
- }
4702
- }
4703
- await this.codexConnection.cancel(params);
4704
- }
4705
4690
  async setSessionMode(params) {
4706
- const permissionMode = toPermissionMode(params.modeId);
4691
+ const requestedMode = toCodeExecutionMode(params.modeId);
4692
+ const nativeMode = CODEX_NATIVE_MODE[requestedMode];
4707
4693
  const response = await this.codexConnection.setSessionMode({
4708
4694
  ...params,
4709
- modeId: permissionMode
4695
+ modeId: nativeMode
4710
4696
  });
4711
- if (this.sessionState) {
4712
- this.sessionState.modeId = permissionMode;
4713
- this.sessionState.permissionMode = permissionMode;
4714
- }
4697
+ this.sessionState.modeId = nativeMode;
4698
+ this.sessionState.permissionMode = requestedMode;
4715
4699
  return response ?? {};
4716
4700
  }
4717
4701
  async setSessionConfigOption(params) {
4718
4702
  const response = await this.codexConnection.setSessionConfigOption(params);
4719
- if (this.sessionState && response.configOptions) {
4703
+ if (response.configOptions) {
4720
4704
  this.sessionState.configOptions = response.configOptions;
4721
4705
  }
4722
4706
  return response;
@@ -4725,6 +4709,7 @@ var CodexAcpAgent = class extends BaseAcpAgent {
4725
4709
  }
4726
4710
  async closeSession() {
4727
4711
  this.logger.info("Closing Codex session", { sessionId: this.sessionId });
4712
+ this.session.abortController.abort();
4728
4713
  this.session.settingsManager.dispose();
4729
4714
  try {
4730
4715
  this.codexProcess.kill();