@posthog/agent 2.3.169 → 2.3.171

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/types.d.ts CHANGED
@@ -82,6 +82,7 @@ interface TaskExecutionOptions {
82
82
  model?: string;
83
83
  gatewayUrl?: string;
84
84
  codexBinaryPath?: string;
85
+ instructions?: string;
85
86
  processCallbacks?: ProcessSpawnedCallback;
86
87
  }
87
88
  type LogLevel = "debug" | "info" | "warn" | "error";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/agent",
3
- "version": "2.3.169",
3
+ "version": "2.3.171",
4
4
  "repository": "https://github.com/PostHog/code",
5
5
  "description": "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
6
6
  "exports": {
@@ -282,18 +282,49 @@ export class CodexAcpAgent extends BaseAcpAgent {
282
282
 
283
283
  const response = await this.codexConnection.prompt(params);
284
284
 
285
- // Emit PostHog usage notification
286
- if (this.sessionState?.taskRunId && response.usage) {
287
- await this.client.extNotification("_posthog/usage_update", {
285
+ if (this.sessionState && response.usage) {
286
+ // Accumulate token usage from the prompt response
287
+ this.sessionState.accumulatedUsage.inputTokens +=
288
+ response.usage.inputTokens ?? 0;
289
+ this.sessionState.accumulatedUsage.outputTokens +=
290
+ response.usage.outputTokens ?? 0;
291
+ this.sessionState.accumulatedUsage.cachedReadTokens +=
292
+ response.usage.cachedReadTokens ?? 0;
293
+ this.sessionState.accumulatedUsage.cachedWriteTokens +=
294
+ response.usage.cachedWriteTokens ?? 0;
295
+ }
296
+
297
+ if (this.sessionState?.taskRunId) {
298
+ const { accumulatedUsage } = this.sessionState;
299
+
300
+ await this.client.extNotification(POSTHOG_NOTIFICATIONS.TURN_COMPLETE, {
288
301
  sessionId: params.sessionId,
289
- used: {
290
- inputTokens: response.usage.inputTokens ?? 0,
291
- outputTokens: response.usage.outputTokens ?? 0,
292
- cachedReadTokens: response.usage.cachedReadTokens ?? 0,
293
- cachedWriteTokens: response.usage.cachedWriteTokens ?? 0,
302
+ stopReason: response.stopReason ?? "end_turn",
303
+ usage: {
304
+ inputTokens: accumulatedUsage.inputTokens,
305
+ outputTokens: accumulatedUsage.outputTokens,
306
+ cachedReadTokens: accumulatedUsage.cachedReadTokens,
307
+ cachedWriteTokens: accumulatedUsage.cachedWriteTokens,
308
+ totalTokens:
309
+ accumulatedUsage.inputTokens +
310
+ accumulatedUsage.outputTokens +
311
+ accumulatedUsage.cachedReadTokens +
312
+ accumulatedUsage.cachedWriteTokens,
294
313
  },
295
- cost: null,
296
314
  });
315
+
316
+ if (response.usage) {
317
+ await this.client.extNotification("_posthog/usage_update", {
318
+ sessionId: params.sessionId,
319
+ used: {
320
+ inputTokens: response.usage.inputTokens ?? 0,
321
+ outputTokens: response.usage.outputTokens ?? 0,
322
+ cachedReadTokens: response.usage.cachedReadTokens ?? 0,
323
+ cachedWriteTokens: response.usage.cachedWriteTokens ?? 0,
324
+ },
325
+ cost: null,
326
+ });
327
+ }
297
328
  }
298
329
 
299
330
  return response;
@@ -60,17 +60,34 @@ export function createCodexClient(
60
60
  },
61
61
 
62
62
  async sessionUpdate(params: SessionNotification): Promise<void> {
63
- // Parse usage data from session updates
64
63
  const update = params.update as Record<string, unknown> | undefined;
65
64
  if (update?.sessionUpdate === "usage_update") {
66
65
  const used = update.used as number | undefined;
67
66
  const size = update.size as number | undefined;
68
67
  if (used !== undefined) sessionState.contextUsed = used;
69
68
  if (size !== undefined) sessionState.contextSize = size;
69
+
70
+ // Accumulate per-message token usage when available
71
+ const inputTokens = update.inputTokens as number | undefined;
72
+ const outputTokens = update.outputTokens as number | undefined;
73
+ if (inputTokens !== undefined) {
74
+ sessionState.accumulatedUsage.inputTokens += inputTokens;
75
+ }
76
+ if (outputTokens !== undefined) {
77
+ sessionState.accumulatedUsage.outputTokens += outputTokens;
78
+ }
79
+ const cachedRead = update.cachedReadTokens as number | undefined;
80
+ const cachedWrite = update.cachedWriteTokens as number | undefined;
81
+ if (cachedRead !== undefined) {
82
+ sessionState.accumulatedUsage.cachedReadTokens += cachedRead;
83
+ }
84
+ if (cachedWrite !== undefined) {
85
+ sessionState.accumulatedUsage.cachedWriteTokens += cachedWrite;
86
+ }
87
+
70
88
  callbacks?.onUsageUpdate?.(update);
71
89
  }
72
90
 
73
- // Forward to upstream client
74
91
  await upstreamClient.sessionUpdate(params);
75
92
  },
76
93
 
@@ -10,6 +10,7 @@ export interface CodexProcessOptions {
10
10
  apiBaseUrl?: string;
11
11
  apiKey?: string;
12
12
  model?: string;
13
+ instructions?: string;
13
14
  binaryPath?: string;
14
15
  logger?: Logger;
15
16
  processCallbacks?: ProcessSpawnedCallback;
@@ -42,6 +43,13 @@ function buildConfigArgs(options: CodexProcessOptions): string[] {
42
43
  args.push("-c", `model="${options.model}"`);
43
44
  }
44
45
 
46
+ if (options.instructions) {
47
+ const escaped = options.instructions
48
+ .replace(/\\/g, "\\\\")
49
+ .replace(/"/g, '\\"');
50
+ args.push("-c", `instructions="${escaped}"`);
51
+ }
52
+
45
53
  return args;
46
54
  }
47
55
 
package/src/agent.ts CHANGED
@@ -131,6 +131,7 @@ export class Agent {
131
131
  apiKey: gatewayConfig.apiKey,
132
132
  binaryPath: options.codexBinaryPath,
133
133
  model: sanitizedModel,
134
+ instructions: options.instructions,
134
135
  }
135
136
  : undefined,
136
137
  });
package/src/types.ts CHANGED
@@ -112,6 +112,7 @@ export interface TaskExecutionOptions {
112
112
  model?: string;
113
113
  gatewayUrl?: string;
114
114
  codexBinaryPath?: string;
115
+ instructions?: string;
115
116
  processCallbacks?: ProcessSpawnedCallback;
116
117
  }
117
118