@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.
- package/dist/agent.js +23 -13
- package/dist/agent.js.map +1 -1
- package/dist/logger-RC7sPv0S.d.ts +24 -0
- package/dist/posthog-api.js +9 -1
- package/dist/posthog-api.js.map +1 -1
- package/dist/resume.d.ts +69 -0
- package/dist/resume.js +6801 -0
- package/dist/resume.js.map +1 -0
- package/dist/server/agent-server.d.ts +0 -3
- package/dist/server/agent-server.js +95 -88
- package/dist/server/agent-server.js.map +1 -1
- package/dist/server/bin.cjs +95 -88
- package/dist/server/bin.cjs.map +1 -1
- package/dist/tree-tracker.d.ts +67 -0
- package/dist/tree-tracker.js +6417 -0
- package/dist/tree-tracker.js.map +1 -0
- package/package.json +9 -1
- package/src/adapters/claude/claude-agent.ts +20 -17
- package/src/resume.ts +51 -0
- package/src/server/agent-server.ts +2 -56
package/dist/server/bin.cjs
CHANGED
|
@@ -8729,7 +8729,7 @@ var import_zod3 = require("zod");
|
|
|
8729
8729
|
// package.json
|
|
8730
8730
|
var package_default = {
|
|
8731
8731
|
name: "@posthog/agent",
|
|
8732
|
-
version: "2.3.
|
|
8732
|
+
version: "2.3.386",
|
|
8733
8733
|
repository: "https://github.com/PostHog/code",
|
|
8734
8734
|
description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
|
|
8735
8735
|
exports: {
|
|
@@ -8785,6 +8785,14 @@ var package_default = {
|
|
|
8785
8785
|
types: "./dist/execution-mode.d.ts",
|
|
8786
8786
|
import: "./dist/execution-mode.js"
|
|
8787
8787
|
},
|
|
8788
|
+
"./resume": {
|
|
8789
|
+
types: "./dist/resume.d.ts",
|
|
8790
|
+
import: "./dist/resume.js"
|
|
8791
|
+
},
|
|
8792
|
+
"./tree-tracker": {
|
|
8793
|
+
types: "./dist/tree-tracker.d.ts",
|
|
8794
|
+
import: "./dist/tree-tracker.js"
|
|
8795
|
+
},
|
|
8788
8796
|
"./server": {
|
|
8789
8797
|
types: "./dist/server/agent-server.d.ts",
|
|
8790
8798
|
import: "./dist/server/agent-server.js"
|
|
@@ -16452,14 +16460,6 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
|
|
|
16452
16460
|
return this.listSessions(params);
|
|
16453
16461
|
}
|
|
16454
16462
|
async prompt(params) {
|
|
16455
|
-
this.session.cancelled = false;
|
|
16456
|
-
this.session.interruptReason = void 0;
|
|
16457
|
-
this.session.accumulatedUsage = {
|
|
16458
|
-
inputTokens: 0,
|
|
16459
|
-
outputTokens: 0,
|
|
16460
|
-
cachedReadTokens: 0,
|
|
16461
|
-
cachedWriteTokens: 0
|
|
16462
|
-
};
|
|
16463
16463
|
const userMessage = promptToClaude(params);
|
|
16464
16464
|
const promptUuid = (0, import_node_crypto.randomUUID)();
|
|
16465
16465
|
userMessage.uuid = promptUuid;
|
|
@@ -16495,6 +16495,14 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
|
|
|
16495
16495
|
} else {
|
|
16496
16496
|
this.session.input.push(userMessage);
|
|
16497
16497
|
}
|
|
16498
|
+
this.session.cancelled = false;
|
|
16499
|
+
this.session.interruptReason = void 0;
|
|
16500
|
+
this.session.accumulatedUsage = {
|
|
16501
|
+
inputTokens: 0,
|
|
16502
|
+
outputTokens: 0,
|
|
16503
|
+
cachedReadTokens: 0,
|
|
16504
|
+
cachedWriteTokens: 0
|
|
16505
|
+
};
|
|
16498
16506
|
await this.broadcastUserMessage(params);
|
|
16499
16507
|
this.session.promptRunning = true;
|
|
16500
16508
|
let handedOff = false;
|
|
@@ -16700,9 +16708,6 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
|
|
|
16700
16708
|
}
|
|
16701
16709
|
case "user":
|
|
16702
16710
|
case "assistant": {
|
|
16703
|
-
if (this.session.cancelled) {
|
|
16704
|
-
break;
|
|
16705
|
-
}
|
|
16706
16711
|
if (message.type === "user" && "uuid" in message && message.uuid) {
|
|
16707
16712
|
if (message.uuid === promptUuid) {
|
|
16708
16713
|
promptReplayed = true;
|
|
@@ -16715,9 +16720,14 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
|
|
|
16715
16720
|
pending.resolve(false);
|
|
16716
16721
|
this.session.pendingMessages.delete(message.uuid);
|
|
16717
16722
|
handedOff = true;
|
|
16718
|
-
return {
|
|
16723
|
+
return {
|
|
16724
|
+
stopReason: this.session.cancelled ? "cancelled" : "end_turn"
|
|
16725
|
+
};
|
|
16719
16726
|
}
|
|
16720
16727
|
}
|
|
16728
|
+
if (this.session.cancelled) {
|
|
16729
|
+
break;
|
|
16730
|
+
}
|
|
16721
16731
|
if ("isReplay" in message && message.isReplay) {
|
|
16722
16732
|
break;
|
|
16723
16733
|
}
|
|
@@ -18321,45 +18331,6 @@ function createCodexConnection(config) {
|
|
|
18321
18331
|
};
|
|
18322
18332
|
}
|
|
18323
18333
|
|
|
18324
|
-
// src/adapters/claude/session/jsonl-hydration.ts
|
|
18325
|
-
var import_node_crypto2 = require("crypto");
|
|
18326
|
-
var fs10 = __toESM(require("fs/promises"), 1);
|
|
18327
|
-
var os6 = __toESM(require("os"), 1);
|
|
18328
|
-
var path12 = __toESM(require("path"), 1);
|
|
18329
|
-
var CHARS_PER_TOKEN = 4;
|
|
18330
|
-
var DEFAULT_MAX_TOKENS = 15e4;
|
|
18331
|
-
function estimateTurnTokens(turn) {
|
|
18332
|
-
let chars = 0;
|
|
18333
|
-
for (const block of turn.content) {
|
|
18334
|
-
if ("text" in block && typeof block.text === "string") {
|
|
18335
|
-
chars += block.text.length;
|
|
18336
|
-
}
|
|
18337
|
-
}
|
|
18338
|
-
if (turn.toolCalls) {
|
|
18339
|
-
for (const tc of turn.toolCalls) {
|
|
18340
|
-
chars += JSON.stringify(tc.input ?? "").length;
|
|
18341
|
-
if (tc.result !== void 0) {
|
|
18342
|
-
chars += typeof tc.result === "string" ? tc.result.length : JSON.stringify(tc.result).length;
|
|
18343
|
-
}
|
|
18344
|
-
}
|
|
18345
|
-
}
|
|
18346
|
-
return Math.ceil(chars / CHARS_PER_TOKEN);
|
|
18347
|
-
}
|
|
18348
|
-
function selectRecentTurns(turns, maxTokens = DEFAULT_MAX_TOKENS) {
|
|
18349
|
-
let budget = maxTokens;
|
|
18350
|
-
let startIndex = turns.length;
|
|
18351
|
-
for (let i2 = turns.length - 1; i2 >= 0; i2--) {
|
|
18352
|
-
const cost = estimateTurnTokens(turns[i2]);
|
|
18353
|
-
if (cost > budget) break;
|
|
18354
|
-
budget -= cost;
|
|
18355
|
-
startIndex = i2;
|
|
18356
|
-
}
|
|
18357
|
-
while (startIndex < turns.length && turns[startIndex].role !== "user") {
|
|
18358
|
-
startIndex++;
|
|
18359
|
-
}
|
|
18360
|
-
return turns.slice(startIndex);
|
|
18361
|
-
}
|
|
18362
|
-
|
|
18363
18334
|
// src/utils/gateway.ts
|
|
18364
18335
|
function getGatewayBaseUrl(posthogHost) {
|
|
18365
18336
|
const url = new URL(posthogHost);
|
|
@@ -18561,6 +18532,45 @@ var PostHogAPIClient = class {
|
|
|
18561
18532
|
}
|
|
18562
18533
|
};
|
|
18563
18534
|
|
|
18535
|
+
// src/adapters/claude/session/jsonl-hydration.ts
|
|
18536
|
+
var import_node_crypto2 = require("crypto");
|
|
18537
|
+
var fs10 = __toESM(require("fs/promises"), 1);
|
|
18538
|
+
var os6 = __toESM(require("os"), 1);
|
|
18539
|
+
var path12 = __toESM(require("path"), 1);
|
|
18540
|
+
var CHARS_PER_TOKEN = 4;
|
|
18541
|
+
var DEFAULT_MAX_TOKENS = 15e4;
|
|
18542
|
+
function estimateTurnTokens(turn) {
|
|
18543
|
+
let chars = 0;
|
|
18544
|
+
for (const block of turn.content) {
|
|
18545
|
+
if ("text" in block && typeof block.text === "string") {
|
|
18546
|
+
chars += block.text.length;
|
|
18547
|
+
}
|
|
18548
|
+
}
|
|
18549
|
+
if (turn.toolCalls) {
|
|
18550
|
+
for (const tc of turn.toolCalls) {
|
|
18551
|
+
chars += JSON.stringify(tc.input ?? "").length;
|
|
18552
|
+
if (tc.result !== void 0) {
|
|
18553
|
+
chars += typeof tc.result === "string" ? tc.result.length : JSON.stringify(tc.result).length;
|
|
18554
|
+
}
|
|
18555
|
+
}
|
|
18556
|
+
}
|
|
18557
|
+
return Math.ceil(chars / CHARS_PER_TOKEN);
|
|
18558
|
+
}
|
|
18559
|
+
function selectRecentTurns(turns, maxTokens = DEFAULT_MAX_TOKENS) {
|
|
18560
|
+
let budget = maxTokens;
|
|
18561
|
+
let startIndex = turns.length;
|
|
18562
|
+
for (let i2 = turns.length - 1; i2 >= 0; i2--) {
|
|
18563
|
+
const cost = estimateTurnTokens(turns[i2]);
|
|
18564
|
+
if (cost > budget) break;
|
|
18565
|
+
budget -= cost;
|
|
18566
|
+
startIndex = i2;
|
|
18567
|
+
}
|
|
18568
|
+
while (startIndex < turns.length && turns[startIndex].role !== "user") {
|
|
18569
|
+
startIndex++;
|
|
18570
|
+
}
|
|
18571
|
+
return turns.slice(startIndex);
|
|
18572
|
+
}
|
|
18573
|
+
|
|
18564
18574
|
// ../shared/dist/index.js
|
|
18565
18575
|
var CLOUD_PROMPT_PREFIX = "__twig_cloud_prompt_v1__:";
|
|
18566
18576
|
function deserializeCloudPrompt(value) {
|
|
@@ -19530,6 +19540,37 @@ async function resumeFromLog(config) {
|
|
|
19530
19540
|
logEntryCount: result.data.logEntryCount
|
|
19531
19541
|
};
|
|
19532
19542
|
}
|
|
19543
|
+
var RESUME_HISTORY_TOKEN_BUDGET = 5e4;
|
|
19544
|
+
var TOOL_RESULT_MAX_CHARS = 2e3;
|
|
19545
|
+
function formatConversationForResume(conversation) {
|
|
19546
|
+
const selected = selectRecentTurns(conversation, RESUME_HISTORY_TOKEN_BUDGET);
|
|
19547
|
+
const parts2 = [];
|
|
19548
|
+
if (selected.length < conversation.length) {
|
|
19549
|
+
parts2.push(
|
|
19550
|
+
`*(${conversation.length - selected.length} earlier turns omitted)*`
|
|
19551
|
+
);
|
|
19552
|
+
}
|
|
19553
|
+
for (const turn of selected) {
|
|
19554
|
+
const role = turn.role === "user" ? "User" : "Assistant";
|
|
19555
|
+
const textParts = turn.content.filter((block) => block.type === "text").map((block) => block.text);
|
|
19556
|
+
if (textParts.length > 0) {
|
|
19557
|
+
parts2.push(`**${role}**: ${textParts.join("\n")}`);
|
|
19558
|
+
}
|
|
19559
|
+
if (turn.toolCalls?.length) {
|
|
19560
|
+
const toolSummary = turn.toolCalls.map((tc) => {
|
|
19561
|
+
let resultStr = "";
|
|
19562
|
+
if (tc.result !== void 0) {
|
|
19563
|
+
const raw = typeof tc.result === "string" ? tc.result : JSON.stringify(tc.result);
|
|
19564
|
+
resultStr = raw.length > TOOL_RESULT_MAX_CHARS ? ` \u2192 ${raw.substring(0, TOOL_RESULT_MAX_CHARS)}...(truncated)` : ` \u2192 ${raw}`;
|
|
19565
|
+
}
|
|
19566
|
+
return ` - ${tc.toolName}${resultStr}`;
|
|
19567
|
+
}).join("\n");
|
|
19568
|
+
parts2.push(`**${role} (tools)**:
|
|
19569
|
+
${toolSummary}`);
|
|
19570
|
+
}
|
|
19571
|
+
}
|
|
19572
|
+
return parts2.join("\n\n");
|
|
19573
|
+
}
|
|
19533
19574
|
|
|
19534
19575
|
// src/session-log-writer.ts
|
|
19535
19576
|
var import_node_fs5 = __toESM(require("fs"), 1);
|
|
@@ -20115,7 +20156,7 @@ function getTaskRunStateString(taskRun, key) {
|
|
|
20115
20156
|
const value = state[key];
|
|
20116
20157
|
return typeof value === "string" ? value : null;
|
|
20117
20158
|
}
|
|
20118
|
-
var AgentServer = class
|
|
20159
|
+
var AgentServer = class {
|
|
20119
20160
|
config;
|
|
20120
20161
|
logger;
|
|
20121
20162
|
server = null;
|
|
@@ -20840,7 +20881,7 @@ var AgentServer = class _AgentServer {
|
|
|
20840
20881
|
async sendResumeMessage(payload, taskRun) {
|
|
20841
20882
|
if (!this.session || !this.resumeState) return;
|
|
20842
20883
|
try {
|
|
20843
|
-
const conversationSummary =
|
|
20884
|
+
const conversationSummary = formatConversationForResume(
|
|
20844
20885
|
this.resumeState.conversation
|
|
20845
20886
|
);
|
|
20846
20887
|
const pendingUserPrompt = await this.getPendingUserPrompt(taskRun);
|
|
@@ -20911,40 +20952,6 @@ Continue from where you left off. The user is waiting for your response.`
|
|
|
20911
20952
|
await this.classifyAndSignalFailure(payload, "resume", error);
|
|
20912
20953
|
}
|
|
20913
20954
|
}
|
|
20914
|
-
static RESUME_HISTORY_TOKEN_BUDGET = 5e4;
|
|
20915
|
-
static TOOL_RESULT_MAX_CHARS = 2e3;
|
|
20916
|
-
formatConversationForResume(conversation) {
|
|
20917
|
-
const selected = selectRecentTurns(
|
|
20918
|
-
conversation,
|
|
20919
|
-
_AgentServer.RESUME_HISTORY_TOKEN_BUDGET
|
|
20920
|
-
);
|
|
20921
|
-
const parts2 = [];
|
|
20922
|
-
if (selected.length < conversation.length) {
|
|
20923
|
-
parts2.push(
|
|
20924
|
-
`*(${conversation.length - selected.length} earlier turns omitted)*`
|
|
20925
|
-
);
|
|
20926
|
-
}
|
|
20927
|
-
for (const turn of selected) {
|
|
20928
|
-
const role = turn.role === "user" ? "User" : "Assistant";
|
|
20929
|
-
const textParts = turn.content.filter((block) => block.type === "text").map((block) => block.text);
|
|
20930
|
-
if (textParts.length > 0) {
|
|
20931
|
-
parts2.push(`**${role}**: ${textParts.join("\n")}`);
|
|
20932
|
-
}
|
|
20933
|
-
if (turn.toolCalls?.length) {
|
|
20934
|
-
const toolSummary = turn.toolCalls.map((tc) => {
|
|
20935
|
-
let resultStr = "";
|
|
20936
|
-
if (tc.result !== void 0) {
|
|
20937
|
-
const raw = typeof tc.result === "string" ? tc.result : JSON.stringify(tc.result);
|
|
20938
|
-
resultStr = raw.length > _AgentServer.TOOL_RESULT_MAX_CHARS ? ` \u2192 ${raw.substring(0, _AgentServer.TOOL_RESULT_MAX_CHARS)}...(truncated)` : ` \u2192 ${raw}`;
|
|
20939
|
-
}
|
|
20940
|
-
return ` - ${tc.toolName}${resultStr}`;
|
|
20941
|
-
}).join("\n");
|
|
20942
|
-
parts2.push(`**${role} (tools)**:
|
|
20943
|
-
${toolSummary}`);
|
|
20944
|
-
}
|
|
20945
|
-
}
|
|
20946
|
-
return parts2.join("\n\n");
|
|
20947
|
-
}
|
|
20948
20955
|
getInitialPromptOverride(taskRun) {
|
|
20949
20956
|
const state = taskRun.state;
|
|
20950
20957
|
const override = state?.initial_prompt_override;
|