@posthog/agent 2.3.202 → 2.3.209
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 +14 -6
- package/dist/agent.js.map +1 -1
- package/dist/index.d.ts +57 -1
- package/dist/index.js +45 -1
- package/dist/index.js.map +1 -1
- package/dist/posthog-api.js +1 -1
- package/dist/posthog-api.js.map +1 -1
- package/dist/server/agent-server.js +22 -9
- package/dist/server/agent-server.js.map +1 -1
- package/dist/server/bin.cjs +22 -9
- package/dist/server/bin.cjs.map +1 -1
- package/package.json +1 -1
- package/src/acp-extensions.ts +15 -4
- package/src/adapters/claude/claude-agent.ts +17 -1
- package/src/adapters/claude/conversion/sdk-to-acp.ts +4 -3
- package/src/index.ts +1 -0
- package/src/sagas/resume-saga.test.ts +0 -29
- package/src/sagas/resume-saga.ts +5 -6
package/package.json
CHANGED
package/src/acp-extensions.ts
CHANGED
|
@@ -5,10 +5,6 @@
|
|
|
5
5
|
* - Custom notification methods are prefixed with `_posthog/`
|
|
6
6
|
* - Custom data can be attached via `_meta` fields
|
|
7
7
|
*
|
|
8
|
-
* Note: When using `extNotification()` from the ACP SDK, it automatically
|
|
9
|
-
* adds an extra underscore prefix (e.g., `_posthog/tree_snapshot` becomes
|
|
10
|
-
* `__posthog/tree_snapshot` in the log). Code that reads logs should handle both.
|
|
11
|
-
*
|
|
12
8
|
* See: https://agentclientprotocol.com/docs/extensibility
|
|
13
9
|
*/
|
|
14
10
|
|
|
@@ -68,3 +64,18 @@ export const POSTHOG_NOTIFICATIONS = {
|
|
|
68
64
|
/** Token usage update for a session turn */
|
|
69
65
|
USAGE_UPDATE: "_posthog/usage_update",
|
|
70
66
|
} as const;
|
|
67
|
+
|
|
68
|
+
type NotificationMethod =
|
|
69
|
+
(typeof POSTHOG_NOTIFICATIONS)[keyof typeof POSTHOG_NOTIFICATIONS];
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Check if an ACP method matches a PostHog notification, handling the
|
|
73
|
+
* possible `__posthog/` double-prefix from extNotification().
|
|
74
|
+
*/
|
|
75
|
+
export function isNotification(
|
|
76
|
+
method: string | undefined,
|
|
77
|
+
notification: NotificationMethod,
|
|
78
|
+
): boolean {
|
|
79
|
+
if (!method) return false;
|
|
80
|
+
return method === notification || method === `_${notification}`;
|
|
81
|
+
}
|
|
@@ -371,8 +371,18 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
|
|
|
371
371
|
switch (message.type) {
|
|
372
372
|
case "system":
|
|
373
373
|
if (message.subtype === "compact_boundary") {
|
|
374
|
+
// Send used:0 immediately so the client doesn't keep showing
|
|
375
|
+
// the stale pre-compaction context size until the next turn.
|
|
374
376
|
lastAssistantTotalUsage = 0;
|
|
375
377
|
promptReplayed = true;
|
|
378
|
+
await this.client.sessionUpdate({
|
|
379
|
+
sessionId: params.sessionId,
|
|
380
|
+
update: {
|
|
381
|
+
sessionUpdate: "usage_update",
|
|
382
|
+
used: 0,
|
|
383
|
+
size: lastContextWindowSize,
|
|
384
|
+
},
|
|
385
|
+
});
|
|
376
386
|
}
|
|
377
387
|
if (message.subtype === "local_command_output") {
|
|
378
388
|
promptReplayed = true;
|
|
@@ -530,6 +540,11 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
|
|
|
530
540
|
}
|
|
531
541
|
|
|
532
542
|
// Store latest assistant usage (excluding subagents)
|
|
543
|
+
// Sum all token types as a proxy for post-turn context occupancy:
|
|
544
|
+
// current turn's output will become next turn's input.
|
|
545
|
+
// Note: per the Anthropic API, input_tokens excludes cache tokens —
|
|
546
|
+
// cache_read and cache_creation are reported separately, so summing
|
|
547
|
+
// all four fields is not double-counting.
|
|
533
548
|
if (
|
|
534
549
|
"usage" in message.message &&
|
|
535
550
|
message.parent_tool_use_id === null
|
|
@@ -544,6 +559,7 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
|
|
|
544
559
|
};
|
|
545
560
|
lastAssistantTotalUsage =
|
|
546
561
|
usage.input_tokens +
|
|
562
|
+
usage.output_tokens +
|
|
547
563
|
usage.cache_read_input_tokens +
|
|
548
564
|
usage.cache_creation_input_tokens;
|
|
549
565
|
|
|
@@ -931,7 +947,7 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
|
|
|
931
947
|
),
|
|
932
948
|
...(meta?.taskRunId
|
|
933
949
|
? [
|
|
934
|
-
this.client.extNotification(
|
|
950
|
+
this.client.extNotification(POSTHOG_NOTIFICATIONS.SDK_SESSION, {
|
|
935
951
|
taskRunId: meta.taskRunId,
|
|
936
952
|
sessionId,
|
|
937
953
|
adapter: "claude",
|
|
@@ -17,6 +17,7 @@ import type {
|
|
|
17
17
|
BetaContentBlock,
|
|
18
18
|
BetaRawContentBlockDelta,
|
|
19
19
|
} from "@anthropic-ai/sdk/resources/beta.mjs";
|
|
20
|
+
import { POSTHOG_NOTIFICATIONS } from "@/acp-extensions";
|
|
20
21
|
import { image, text } from "../../../utils/acp-content";
|
|
21
22
|
import { unreachable } from "../../../utils/common";
|
|
22
23
|
import type { Logger } from "../../../utils/logger";
|
|
@@ -550,7 +551,7 @@ export async function handleSystemMessage(
|
|
|
550
551
|
case "init":
|
|
551
552
|
break;
|
|
552
553
|
case "compact_boundary":
|
|
553
|
-
await client.extNotification(
|
|
554
|
+
await client.extNotification(POSTHOG_NOTIFICATIONS.COMPACT_BOUNDARY, {
|
|
554
555
|
sessionId,
|
|
555
556
|
trigger: message.compact_metadata.trigger,
|
|
556
557
|
preTokens: message.compact_metadata.pre_tokens,
|
|
@@ -566,7 +567,7 @@ export async function handleSystemMessage(
|
|
|
566
567
|
case "status":
|
|
567
568
|
if (message.status === "compacting") {
|
|
568
569
|
logger.info("Session compacting started", { sessionId });
|
|
569
|
-
await client.extNotification(
|
|
570
|
+
await client.extNotification(POSTHOG_NOTIFICATIONS.STATUS, {
|
|
570
571
|
sessionId,
|
|
571
572
|
status: "compacting",
|
|
572
573
|
});
|
|
@@ -579,7 +580,7 @@ export async function handleSystemMessage(
|
|
|
579
580
|
status: message.status,
|
|
580
581
|
summary: message.summary,
|
|
581
582
|
});
|
|
582
|
-
await client.extNotification(
|
|
583
|
+
await client.extNotification(POSTHOG_NOTIFICATIONS.TASK_NOTIFICATION, {
|
|
583
584
|
sessionId,
|
|
584
585
|
taskId: message.task_id,
|
|
585
586
|
status: message.status,
|
package/src/index.ts
CHANGED
|
@@ -441,35 +441,6 @@ describe("ResumeSaga", () => {
|
|
|
441
441
|
expect(result.data.latestSnapshot?.treeHash).toBe("hash-2");
|
|
442
442
|
});
|
|
443
443
|
|
|
444
|
-
it("finds snapshot with SDK double-underscore prefix", async () => {
|
|
445
|
-
(mockApiClient.getTaskRun as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
446
|
-
createTaskRun(),
|
|
447
|
-
);
|
|
448
|
-
(
|
|
449
|
-
mockApiClient.fetchTaskRunLogs as ReturnType<typeof vi.fn>
|
|
450
|
-
).mockResolvedValue([
|
|
451
|
-
createNotification(`_${POSTHOG_NOTIFICATIONS.TREE_SNAPSHOT}`, {
|
|
452
|
-
treeHash: "sdk-prefixed-hash",
|
|
453
|
-
baseCommit: "abc",
|
|
454
|
-
changes: [],
|
|
455
|
-
timestamp: new Date().toISOString(),
|
|
456
|
-
}),
|
|
457
|
-
]);
|
|
458
|
-
|
|
459
|
-
const saga = new ResumeSaga(mockLogger);
|
|
460
|
-
const result = await saga.run({
|
|
461
|
-
taskId: "task-1",
|
|
462
|
-
runId: "run-1",
|
|
463
|
-
repositoryPath: repo.path,
|
|
464
|
-
apiClient: mockApiClient,
|
|
465
|
-
});
|
|
466
|
-
|
|
467
|
-
expect(result.success).toBe(true);
|
|
468
|
-
if (!result.success) return;
|
|
469
|
-
|
|
470
|
-
expect(result.data.latestSnapshot?.treeHash).toBe("sdk-prefixed-hash");
|
|
471
|
-
});
|
|
472
|
-
|
|
473
444
|
it("returns interrupted flag from snapshot", async () => {
|
|
474
445
|
(mockApiClient.getTaskRun as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
475
446
|
createTaskRun(),
|
package/src/sagas/resume-saga.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ContentBlock } from "@agentclientprotocol/sdk";
|
|
2
2
|
import { Saga } from "@posthog/shared";
|
|
3
|
-
import { POSTHOG_NOTIFICATIONS } from "../acp-extensions";
|
|
3
|
+
import { isNotification, POSTHOG_NOTIFICATIONS } from "../acp-extensions";
|
|
4
4
|
import type { PostHogAPIClient } from "../posthog-api";
|
|
5
5
|
import { TreeTracker } from "../tree-tracker";
|
|
6
6
|
import type {
|
|
@@ -178,14 +178,13 @@ export class ResumeSaga extends Saga<ResumeInput, ResumeOutput> {
|
|
|
178
178
|
private findLatestTreeSnapshot(
|
|
179
179
|
entries: StoredNotification[],
|
|
180
180
|
): TreeSnapshotEvent | null {
|
|
181
|
-
const sdkPrefixedMethod = `_${POSTHOG_NOTIFICATIONS.TREE_SNAPSHOT}`;
|
|
182
|
-
|
|
183
181
|
for (let i = entries.length - 1; i >= 0; i--) {
|
|
184
182
|
const entry = entries[i];
|
|
185
|
-
const method = entry.notification?.method;
|
|
186
183
|
if (
|
|
187
|
-
|
|
188
|
-
|
|
184
|
+
isNotification(
|
|
185
|
+
entry.notification?.method,
|
|
186
|
+
POSTHOG_NOTIFICATIONS.TREE_SNAPSHOT,
|
|
187
|
+
)
|
|
189
188
|
) {
|
|
190
189
|
const params = entry.notification.params as
|
|
191
190
|
| TreeSnapshotEvent
|