@posthog/agent 2.3.678 → 2.3.703
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/adapters/claude/conversion/tool-use-to-acp.js.map +1 -1
- package/dist/adapters/claude/mcp/tool-metadata.d.ts +4 -1
- package/dist/adapters/claude/mcp/tool-metadata.js +4 -0
- package/dist/adapters/claude/mcp/tool-metadata.js.map +1 -1
- package/dist/adapters/claude/tools.js.map +1 -1
- package/dist/agent.js +287 -28
- package/dist/agent.js.map +1 -1
- package/dist/handoff-checkpoint.js +70 -0
- package/dist/handoff-checkpoint.js.map +1 -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/resume.js +70 -0
- package/dist/resume.js.map +1 -1
- package/dist/server/agent-server.js +436 -179
- package/dist/server/agent-server.js.map +1 -1
- package/dist/server/bin.cjs +429 -172
- package/dist/server/bin.cjs.map +1 -1
- package/package.json +3 -3
- package/src/adapters/claude/claude-agent.slash-command.test.ts +153 -0
- package/src/adapters/claude/claude-agent.ts +84 -1
- package/src/adapters/claude/context-breakdown.test.ts +130 -0
- package/src/adapters/claude/context-breakdown.ts +127 -0
- package/src/adapters/claude/conversion/acp-to-sdk.test.ts +39 -0
- package/src/adapters/claude/conversion/acp-to-sdk.ts +20 -24
- package/src/adapters/claude/mcp/tool-metadata.ts +6 -0
- package/src/adapters/claude/types.ts +3 -0
- package/src/adapters/codex/codex-agent.ts +46 -5
- package/src/adapters/codex/codex-client.test.ts +49 -1
- package/src/adapters/codex/session-state.ts +36 -5
|
@@ -4429,7 +4429,7 @@ var require_lib5 = __commonJS({
|
|
|
4429
4429
|
|
|
4430
4430
|
// src/server/agent-server.ts
|
|
4431
4431
|
import { mkdir as mkdir5, writeFile as writeFile4 } from "fs/promises";
|
|
4432
|
-
import { basename as basename3, join as
|
|
4432
|
+
import { basename as basename3, join as join14 } from "path";
|
|
4433
4433
|
import { pathToFileURL } from "url";
|
|
4434
4434
|
import {
|
|
4435
4435
|
ClientSideConnection as ClientSideConnection2,
|
|
@@ -9315,7 +9315,7 @@ import { z as z5 } from "zod";
|
|
|
9315
9315
|
// package.json
|
|
9316
9316
|
var package_default = {
|
|
9317
9317
|
name: "@posthog/agent",
|
|
9318
|
-
version: "2.3.
|
|
9318
|
+
version: "2.3.703",
|
|
9319
9319
|
repository: "https://github.com/PostHog/code",
|
|
9320
9320
|
description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
|
|
9321
9321
|
exports: {
|
|
@@ -14517,22 +14517,314 @@ function resolveTaskId(meta) {
|
|
|
14517
14517
|
return meta?.taskId ?? meta?.persistence?.taskId;
|
|
14518
14518
|
}
|
|
14519
14519
|
|
|
14520
|
+
// src/adapters/claude/context-breakdown.ts
|
|
14521
|
+
var CLAUDE_PRESET_ESTIMATE_TOKENS = 4e3;
|
|
14522
|
+
var CHARS_PER_TOKEN = 4;
|
|
14523
|
+
function estimateTokens(text2) {
|
|
14524
|
+
if (!text2) return 0;
|
|
14525
|
+
return Math.max(0, Math.round(text2.length / CHARS_PER_TOKEN));
|
|
14526
|
+
}
|
|
14527
|
+
function estimateJsonTokens(value) {
|
|
14528
|
+
try {
|
|
14529
|
+
return estimateTokens(JSON.stringify(value));
|
|
14530
|
+
} catch {
|
|
14531
|
+
return 0;
|
|
14532
|
+
}
|
|
14533
|
+
}
|
|
14534
|
+
function estimateSkillsTokens(commands) {
|
|
14535
|
+
if (!commands.length) return 0;
|
|
14536
|
+
return estimateJsonTokens(
|
|
14537
|
+
commands.map((c) => ({
|
|
14538
|
+
name: c.name,
|
|
14539
|
+
description: c.description,
|
|
14540
|
+
hint: c.input?.hint
|
|
14541
|
+
}))
|
|
14542
|
+
);
|
|
14543
|
+
}
|
|
14544
|
+
function estimateMcpTokens(tools) {
|
|
14545
|
+
if (!tools.length) return 0;
|
|
14546
|
+
return estimateJsonTokens(
|
|
14547
|
+
tools.map((t) => ({ name: t.name, description: t.description }))
|
|
14548
|
+
);
|
|
14549
|
+
}
|
|
14550
|
+
function estimateRulesTokens(rules) {
|
|
14551
|
+
return estimateTokens(rules);
|
|
14552
|
+
}
|
|
14553
|
+
function emptyBaseline() {
|
|
14554
|
+
return {
|
|
14555
|
+
systemPrompt: 0,
|
|
14556
|
+
tools: 0,
|
|
14557
|
+
rules: 0,
|
|
14558
|
+
skills: 0,
|
|
14559
|
+
mcp: 0,
|
|
14560
|
+
subagents: 0
|
|
14561
|
+
};
|
|
14562
|
+
}
|
|
14563
|
+
function estimateSystemPrompt(systemPrompt) {
|
|
14564
|
+
if (!systemPrompt) return CLAUDE_PRESET_ESTIMATE_TOKENS;
|
|
14565
|
+
if (typeof systemPrompt === "string") return estimateTokens(systemPrompt);
|
|
14566
|
+
if (typeof systemPrompt === "object") {
|
|
14567
|
+
const obj = systemPrompt;
|
|
14568
|
+
const appendTokens = typeof obj.append === "string" ? estimateTokens(obj.append) : 0;
|
|
14569
|
+
if (obj.type === "preset") {
|
|
14570
|
+
return CLAUDE_PRESET_ESTIMATE_TOKENS + appendTokens;
|
|
14571
|
+
}
|
|
14572
|
+
return appendTokens;
|
|
14573
|
+
}
|
|
14574
|
+
return 0;
|
|
14575
|
+
}
|
|
14576
|
+
function buildBreakdown(baseline, currentInputTokens) {
|
|
14577
|
+
const stableSum = baseline.systemPrompt + baseline.tools + baseline.rules + baseline.skills + baseline.mcp + baseline.subagents;
|
|
14578
|
+
const conversation = Math.max(0, currentInputTokens - stableSum);
|
|
14579
|
+
return {
|
|
14580
|
+
systemPrompt: baseline.systemPrompt,
|
|
14581
|
+
tools: baseline.tools,
|
|
14582
|
+
rules: baseline.rules,
|
|
14583
|
+
skills: baseline.skills,
|
|
14584
|
+
mcp: baseline.mcp,
|
|
14585
|
+
subagents: baseline.subagents,
|
|
14586
|
+
conversation
|
|
14587
|
+
};
|
|
14588
|
+
}
|
|
14589
|
+
|
|
14520
14590
|
// src/adapters/claude/conversion/acp-to-sdk.ts
|
|
14521
14591
|
import * as path6 from "path";
|
|
14522
14592
|
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
14523
|
-
|
|
14524
|
-
|
|
14525
|
-
|
|
14526
|
-
"
|
|
14527
|
-
"jpeg",
|
|
14528
|
-
"
|
|
14529
|
-
"
|
|
14530
|
-
"
|
|
14531
|
-
"
|
|
14532
|
-
"
|
|
14533
|
-
"
|
|
14534
|
-
"tiff"
|
|
14593
|
+
|
|
14594
|
+
// ../shared/dist/index.js
|
|
14595
|
+
var IMAGE_MIME_TYPES = {
|
|
14596
|
+
png: "image/png",
|
|
14597
|
+
jpg: "image/jpeg",
|
|
14598
|
+
jpeg: "image/jpeg",
|
|
14599
|
+
gif: "image/gif",
|
|
14600
|
+
webp: "image/webp",
|
|
14601
|
+
bmp: "image/bmp",
|
|
14602
|
+
ico: "image/x-icon",
|
|
14603
|
+
tiff: "image/tiff",
|
|
14604
|
+
tif: "image/tiff",
|
|
14605
|
+
svg: "image/svg+xml",
|
|
14606
|
+
heic: "image/heic",
|
|
14607
|
+
heif: "image/heif",
|
|
14608
|
+
avif: "image/avif"
|
|
14609
|
+
};
|
|
14610
|
+
var IMAGE_EXTENSIONS = new Set(
|
|
14611
|
+
Object.keys(IMAGE_MIME_TYPES)
|
|
14612
|
+
);
|
|
14613
|
+
var CLAUDE_IMAGE_MIME_TYPES = /* @__PURE__ */ new Set([
|
|
14614
|
+
"image/jpeg",
|
|
14615
|
+
"image/png",
|
|
14616
|
+
"image/gif",
|
|
14617
|
+
"image/webp"
|
|
14618
|
+
]);
|
|
14619
|
+
function isClaudeImageMimeType(mimeType) {
|
|
14620
|
+
return CLAUDE_IMAGE_MIME_TYPES.has(mimeType.toLowerCase());
|
|
14621
|
+
}
|
|
14622
|
+
var MAX_DATA_URL_LENGTH = 20 * 1024 * 1024;
|
|
14623
|
+
var MAX_IMAGE_BASE64_LENGTH = 15 * 1024 * 1024;
|
|
14624
|
+
function extensionOf(filename) {
|
|
14625
|
+
const slash = Math.max(filename.lastIndexOf("/"), filename.lastIndexOf("\\"));
|
|
14626
|
+
const basename4 = slash >= 0 ? filename.slice(slash + 1) : filename;
|
|
14627
|
+
const cleanBasename = basename4.split(/[?#]/)[0];
|
|
14628
|
+
const dot = cleanBasename.lastIndexOf(".");
|
|
14629
|
+
return dot > 0 ? cleanBasename.slice(dot + 1).toLowerCase() : "";
|
|
14630
|
+
}
|
|
14631
|
+
function isImageFile(filename) {
|
|
14632
|
+
const ext = extensionOf(filename);
|
|
14633
|
+
return ext.length > 0 && IMAGE_EXTENSIONS.has(ext);
|
|
14634
|
+
}
|
|
14635
|
+
var AUDIO_VIDEO_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
14636
|
+
"mp3",
|
|
14637
|
+
"mp4",
|
|
14638
|
+
"wav",
|
|
14639
|
+
"avi",
|
|
14640
|
+
"mov",
|
|
14641
|
+
"mkv",
|
|
14642
|
+
"webm",
|
|
14643
|
+
"mpg",
|
|
14644
|
+
"mpeg",
|
|
14645
|
+
"flac",
|
|
14646
|
+
"ogg",
|
|
14647
|
+
"m4a",
|
|
14648
|
+
"aac"
|
|
14649
|
+
]);
|
|
14650
|
+
var ARCHIVE_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
14651
|
+
"zip",
|
|
14652
|
+
"tar",
|
|
14653
|
+
"gz",
|
|
14654
|
+
"tgz",
|
|
14655
|
+
"bz2",
|
|
14656
|
+
"xz",
|
|
14657
|
+
"rar",
|
|
14658
|
+
"7z"
|
|
14535
14659
|
]);
|
|
14660
|
+
var EXECUTABLE_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
14661
|
+
"exe",
|
|
14662
|
+
"dll",
|
|
14663
|
+
"so",
|
|
14664
|
+
"dylib",
|
|
14665
|
+
"wasm",
|
|
14666
|
+
"bin",
|
|
14667
|
+
"o"
|
|
14668
|
+
]);
|
|
14669
|
+
var FONT_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
14670
|
+
"ttf",
|
|
14671
|
+
"otf",
|
|
14672
|
+
"woff",
|
|
14673
|
+
"woff2",
|
|
14674
|
+
"eot"
|
|
14675
|
+
]);
|
|
14676
|
+
var DOCUMENT_BINARY_EXTENSIONS = /* @__PURE__ */ new Set(["pdf"]);
|
|
14677
|
+
var BINARY_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
14678
|
+
...Object.keys(IMAGE_MIME_TYPES).filter((ext) => ext !== "svg"),
|
|
14679
|
+
...AUDIO_VIDEO_EXTENSIONS,
|
|
14680
|
+
...ARCHIVE_EXTENSIONS,
|
|
14681
|
+
...EXECUTABLE_EXTENSIONS,
|
|
14682
|
+
...FONT_EXTENSIONS,
|
|
14683
|
+
...DOCUMENT_BINARY_EXTENSIONS
|
|
14684
|
+
]);
|
|
14685
|
+
var CLOUD_PROMPT_PREFIX = "__twig_cloud_prompt_v1__:";
|
|
14686
|
+
function deserializeCloudPrompt(value) {
|
|
14687
|
+
const trimmed2 = value.trim();
|
|
14688
|
+
if (!trimmed2) {
|
|
14689
|
+
return [];
|
|
14690
|
+
}
|
|
14691
|
+
if (!trimmed2.startsWith(CLOUD_PROMPT_PREFIX)) {
|
|
14692
|
+
return [{ type: "text", text: trimmed2 }];
|
|
14693
|
+
}
|
|
14694
|
+
try {
|
|
14695
|
+
const parsed = JSON.parse(trimmed2.slice(CLOUD_PROMPT_PREFIX.length));
|
|
14696
|
+
if (Array.isArray(parsed.blocks) && parsed.blocks.length > 0) {
|
|
14697
|
+
return parsed.blocks;
|
|
14698
|
+
}
|
|
14699
|
+
} catch {
|
|
14700
|
+
}
|
|
14701
|
+
return [{ type: "text", text: trimmed2 }];
|
|
14702
|
+
}
|
|
14703
|
+
function promptBlocksToText(blocks) {
|
|
14704
|
+
return blocks.filter((b) => b.type === "text").map((block) => block.text).join("").trim();
|
|
14705
|
+
}
|
|
14706
|
+
var consoleLogger = {
|
|
14707
|
+
info: (_message, _data) => {
|
|
14708
|
+
},
|
|
14709
|
+
debug: (_message, _data) => {
|
|
14710
|
+
},
|
|
14711
|
+
error: (_message, _data) => {
|
|
14712
|
+
},
|
|
14713
|
+
warn: (_message, _data) => {
|
|
14714
|
+
}
|
|
14715
|
+
};
|
|
14716
|
+
var Saga = class {
|
|
14717
|
+
completedSteps = [];
|
|
14718
|
+
currentStepName = "unknown";
|
|
14719
|
+
stepTimings = [];
|
|
14720
|
+
log;
|
|
14721
|
+
constructor(logger) {
|
|
14722
|
+
this.log = logger ?? consoleLogger;
|
|
14723
|
+
}
|
|
14724
|
+
/**
|
|
14725
|
+
* Run the saga with the given input.
|
|
14726
|
+
* Returns a discriminated union result - either success with data or failure with error details.
|
|
14727
|
+
*/
|
|
14728
|
+
async run(input) {
|
|
14729
|
+
this.completedSteps = [];
|
|
14730
|
+
this.currentStepName = "unknown";
|
|
14731
|
+
this.stepTimings = [];
|
|
14732
|
+
const sagaStart = performance.now();
|
|
14733
|
+
try {
|
|
14734
|
+
const result = await this.execute(input);
|
|
14735
|
+
const totalDuration = performance.now() - sagaStart;
|
|
14736
|
+
this.log.debug("Saga completed successfully", {
|
|
14737
|
+
sagaName: this.sagaName,
|
|
14738
|
+
stepsCompleted: this.completedSteps.length,
|
|
14739
|
+
totalDurationMs: Math.round(totalDuration),
|
|
14740
|
+
stepTimings: this.stepTimings
|
|
14741
|
+
});
|
|
14742
|
+
return { success: true, data: result };
|
|
14743
|
+
} catch (error) {
|
|
14744
|
+
this.log.error("Saga failed, initiating rollback", {
|
|
14745
|
+
sagaName: this.sagaName,
|
|
14746
|
+
failedStep: this.currentStepName,
|
|
14747
|
+
error: error instanceof Error ? error.message : String(error),
|
|
14748
|
+
completedStepTimings: this.stepTimings
|
|
14749
|
+
});
|
|
14750
|
+
await this.rollback();
|
|
14751
|
+
return {
|
|
14752
|
+
success: false,
|
|
14753
|
+
error: error instanceof Error ? error.message : String(error),
|
|
14754
|
+
failedStep: this.currentStepName
|
|
14755
|
+
};
|
|
14756
|
+
}
|
|
14757
|
+
}
|
|
14758
|
+
/**
|
|
14759
|
+
* Execute a step with its rollback action.
|
|
14760
|
+
* If the step succeeds, its rollback action is stored for potential rollback.
|
|
14761
|
+
* The step name is automatically tracked for error reporting.
|
|
14762
|
+
*
|
|
14763
|
+
* @param config - Step configuration with name, execute, and rollback functions
|
|
14764
|
+
* @returns The result of the execute function
|
|
14765
|
+
* @throws Re-throws any error from the execute function (triggers rollback)
|
|
14766
|
+
*/
|
|
14767
|
+
async step(config) {
|
|
14768
|
+
this.currentStepName = config.name;
|
|
14769
|
+
const stepStart = performance.now();
|
|
14770
|
+
const result = await config.execute();
|
|
14771
|
+
const durationMs = Math.round(performance.now() - stepStart);
|
|
14772
|
+
this.stepTimings.push({ name: config.name, durationMs });
|
|
14773
|
+
this.completedSteps.push({
|
|
14774
|
+
name: config.name,
|
|
14775
|
+
rollback: () => config.rollback(result)
|
|
14776
|
+
});
|
|
14777
|
+
return result;
|
|
14778
|
+
}
|
|
14779
|
+
/**
|
|
14780
|
+
* Execute a step that doesn't need rollback.
|
|
14781
|
+
* Useful for read-only operations or operations that are idempotent.
|
|
14782
|
+
* The step name is automatically tracked for error reporting.
|
|
14783
|
+
*
|
|
14784
|
+
* @param name - Step name for logging and error tracking
|
|
14785
|
+
* @param execute - The action to execute
|
|
14786
|
+
* @returns The result of the execute function
|
|
14787
|
+
*/
|
|
14788
|
+
async readOnlyStep(name2, execute) {
|
|
14789
|
+
this.currentStepName = name2;
|
|
14790
|
+
const stepStart = performance.now();
|
|
14791
|
+
const result = await execute();
|
|
14792
|
+
const durationMs = Math.round(performance.now() - stepStart);
|
|
14793
|
+
this.stepTimings.push({ name: name2, durationMs });
|
|
14794
|
+
return result;
|
|
14795
|
+
}
|
|
14796
|
+
/**
|
|
14797
|
+
* Roll back all completed steps in reverse order.
|
|
14798
|
+
* Rollback errors are logged but don't stop the rollback of other steps.
|
|
14799
|
+
*/
|
|
14800
|
+
async rollback() {
|
|
14801
|
+
this.log.info("Rolling back saga", {
|
|
14802
|
+
stepsToRollback: this.completedSteps.length
|
|
14803
|
+
});
|
|
14804
|
+
const stepsReversed = [...this.completedSteps].reverse();
|
|
14805
|
+
for (const step of stepsReversed) {
|
|
14806
|
+
try {
|
|
14807
|
+
await step.rollback();
|
|
14808
|
+
} catch (error) {
|
|
14809
|
+
this.log.error(`Failed to rollback step: ${step.name}`, {
|
|
14810
|
+
error: error instanceof Error ? error.message : String(error)
|
|
14811
|
+
});
|
|
14812
|
+
}
|
|
14813
|
+
}
|
|
14814
|
+
this.log.info("Rollback completed", {
|
|
14815
|
+
stepsAttempted: this.completedSteps.length
|
|
14816
|
+
});
|
|
14817
|
+
}
|
|
14818
|
+
/**
|
|
14819
|
+
* Get the number of completed steps (useful for testing)
|
|
14820
|
+
*/
|
|
14821
|
+
getCompletedStepCount() {
|
|
14822
|
+
return this.completedSteps.length;
|
|
14823
|
+
}
|
|
14824
|
+
};
|
|
14825
|
+
|
|
14826
|
+
// src/adapters/claude/conversion/acp-to-sdk.ts
|
|
14827
|
+
var PDF_EXTENSIONS = /* @__PURE__ */ new Set(["pdf"]);
|
|
14536
14828
|
var VIDEO_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
14537
14829
|
"mp4",
|
|
14538
14830
|
"mov",
|
|
@@ -14561,7 +14853,7 @@ function readToolGuidanceForPath(filePath) {
|
|
|
14561
14853
|
if (PDF_EXTENSIONS.has(ext)) {
|
|
14562
14854
|
return 'Optional `pages` string (e.g. "1-5") per Read call instead of loading the entire PDF.';
|
|
14563
14855
|
}
|
|
14564
|
-
if (
|
|
14856
|
+
if (isImageFile(filePath) || VIDEO_EXTENSIONS.has(ext)) {
|
|
14565
14857
|
return "Binary file \u2014 use Read with `file_path`; prefer bounded reads where supported.";
|
|
14566
14858
|
}
|
|
14567
14859
|
return "Large text \u2014 use multiple Read calls with optional `offset` and `limit`.";
|
|
@@ -14627,14 +14919,22 @@ ${chunk.resource.text}
|
|
|
14627
14919
|
break;
|
|
14628
14920
|
case "image":
|
|
14629
14921
|
if (chunk.data) {
|
|
14630
|
-
|
|
14631
|
-
|
|
14632
|
-
|
|
14633
|
-
|
|
14634
|
-
|
|
14635
|
-
|
|
14636
|
-
|
|
14637
|
-
|
|
14922
|
+
if (isClaudeImageMimeType(chunk.mimeType)) {
|
|
14923
|
+
content.push({
|
|
14924
|
+
type: "image",
|
|
14925
|
+
source: {
|
|
14926
|
+
type: "base64",
|
|
14927
|
+
data: chunk.data,
|
|
14928
|
+
media_type: chunk.mimeType
|
|
14929
|
+
}
|
|
14930
|
+
});
|
|
14931
|
+
} else {
|
|
14932
|
+
content.push(
|
|
14933
|
+
sdkText(
|
|
14934
|
+
`[Unsupported image MIME type: ${chunk.mimeType}. Supported: image/jpeg, image/png, image/gif, image/webp.]`
|
|
14935
|
+
)
|
|
14936
|
+
);
|
|
14937
|
+
}
|
|
14638
14938
|
} else if (chunk.uri?.startsWith("http")) {
|
|
14639
14939
|
content.push({
|
|
14640
14940
|
type: "image",
|
|
@@ -15103,6 +15403,9 @@ function getConnectedMcpServerNames() {
|
|
|
15103
15403
|
}
|
|
15104
15404
|
return [...names];
|
|
15105
15405
|
}
|
|
15406
|
+
function getCachedMcpTools() {
|
|
15407
|
+
return [...mcpToolMetadataCache.values()];
|
|
15408
|
+
}
|
|
15106
15409
|
function getMcpToolApprovalState(toolName) {
|
|
15107
15410
|
return mcpToolMetadataCache.get(toolName)?.approvalState;
|
|
15108
15411
|
}
|
|
@@ -18044,6 +18347,19 @@ var SettingsManager = class {
|
|
|
18044
18347
|
var SESSION_VALIDATION_TIMEOUT_MS = 3e4;
|
|
18045
18348
|
var MAX_TITLE_LENGTH = 256;
|
|
18046
18349
|
var LOCAL_ONLY_COMMANDS = /* @__PURE__ */ new Set(["/context", "/heapdump", "/extra-usage"]);
|
|
18350
|
+
function readClaudeMdQuietly(cwd, logger) {
|
|
18351
|
+
try {
|
|
18352
|
+
return fs10.readFileSync(path12.join(cwd, "CLAUDE.md"), "utf-8");
|
|
18353
|
+
} catch (err2) {
|
|
18354
|
+
if (err2?.code !== "ENOENT") {
|
|
18355
|
+
logger.warn("Failed to read CLAUDE.md for context breakdown", {
|
|
18356
|
+
cwd,
|
|
18357
|
+
error: err2 instanceof Error ? err2.message : String(err2)
|
|
18358
|
+
});
|
|
18359
|
+
}
|
|
18360
|
+
return void 0;
|
|
18361
|
+
}
|
|
18362
|
+
}
|
|
18047
18363
|
function sanitizeTitle(text2) {
|
|
18048
18364
|
const sanitized = text2.replace(/[\r\n]+/g, " ").replace(/\s+/g, " ").trim();
|
|
18049
18365
|
if (sanitized.length <= MAX_TITLE_LENGTH) {
|
|
@@ -18309,6 +18625,24 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
|
|
|
18309
18625
|
}
|
|
18310
18626
|
if (message.subtype === "session_state_changed" && message.state === "idle") {
|
|
18311
18627
|
if (!promptReplayed) {
|
|
18628
|
+
if (commandMatch) {
|
|
18629
|
+
const cmd = commandMatch[1];
|
|
18630
|
+
this.logger.warn(
|
|
18631
|
+
"Slash command produced no output; treating as unsupported",
|
|
18632
|
+
{ sessionId: params.sessionId, command: cmd }
|
|
18633
|
+
);
|
|
18634
|
+
await this.client.sessionUpdate({
|
|
18635
|
+
sessionId: params.sessionId,
|
|
18636
|
+
update: {
|
|
18637
|
+
sessionUpdate: "agent_message_chunk",
|
|
18638
|
+
content: {
|
|
18639
|
+
type: "text",
|
|
18640
|
+
text: `Unsupported slash command: \`${cmd}\`. PostHog Code does not implement this command.`
|
|
18641
|
+
}
|
|
18642
|
+
}
|
|
18643
|
+
});
|
|
18644
|
+
return { stopReason: "end_turn" };
|
|
18645
|
+
}
|
|
18312
18646
|
this.logger.debug("Skipping idle state before prompt replay", {
|
|
18313
18647
|
sessionId: params.sessionId
|
|
18314
18648
|
});
|
|
@@ -18371,6 +18705,7 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
|
|
|
18371
18705
|
}
|
|
18372
18706
|
});
|
|
18373
18707
|
}
|
|
18708
|
+
const breakdownInputTokens = lastStreamUsage.input_tokens + lastStreamUsage.cache_read_input_tokens + lastStreamUsage.cache_creation_input_tokens;
|
|
18374
18709
|
await this.client.extNotification(
|
|
18375
18710
|
POSTHOG_NOTIFICATIONS.USAGE_UPDATE,
|
|
18376
18711
|
{
|
|
@@ -18381,7 +18716,11 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
|
|
|
18381
18716
|
cachedReadTokens: message.usage.cache_read_input_tokens,
|
|
18382
18717
|
cachedWriteTokens: message.usage.cache_creation_input_tokens
|
|
18383
18718
|
},
|
|
18384
|
-
cost: message.total_cost_usd
|
|
18719
|
+
cost: message.total_cost_usd,
|
|
18720
|
+
breakdown: buildBreakdown(
|
|
18721
|
+
this.session.contextBreakdownBaseline ?? emptyBaseline(),
|
|
18722
|
+
breakdownInputTokens
|
|
18723
|
+
)
|
|
18385
18724
|
}
|
|
18386
18725
|
);
|
|
18387
18726
|
const usage = {
|
|
@@ -18832,6 +19171,11 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
|
|
|
18832
19171
|
pendingMessages: /* @__PURE__ */ new Map(),
|
|
18833
19172
|
nextPendingOrder: 0,
|
|
18834
19173
|
emitRawSDKMessages: meta?.claudeCode?.emitRawSDKMessages ?? false,
|
|
19174
|
+
contextBreakdownBaseline: {
|
|
19175
|
+
...emptyBaseline(),
|
|
19176
|
+
systemPrompt: estimateSystemPrompt(systemPrompt),
|
|
19177
|
+
rules: estimateRulesTokens(readClaudeMdQuietly(cwd, this.logger))
|
|
19178
|
+
},
|
|
18835
19179
|
// Custom properties
|
|
18836
19180
|
cwd,
|
|
18837
19181
|
notificationHistory: [],
|
|
@@ -19089,13 +19433,26 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
|
|
|
19089
19433
|
}
|
|
19090
19434
|
async sendAvailableCommandsUpdate() {
|
|
19091
19435
|
const commands = await this.session.query.supportedCommands();
|
|
19436
|
+
const available = getAvailableSlashCommands(commands);
|
|
19092
19437
|
await this.client.sessionUpdate({
|
|
19093
19438
|
sessionId: this.sessionId,
|
|
19094
19439
|
update: {
|
|
19095
19440
|
sessionUpdate: "available_commands_update",
|
|
19096
|
-
availableCommands:
|
|
19441
|
+
availableCommands: available
|
|
19097
19442
|
}
|
|
19098
19443
|
});
|
|
19444
|
+
this.updateBreakdownCategory("skills", estimateSkillsTokens(available));
|
|
19445
|
+
}
|
|
19446
|
+
/** Update one category of the context-breakdown baseline so the next
|
|
19447
|
+
* `_posthog/usage_update` carries fresher numbers. No-op when the baseline
|
|
19448
|
+
* hasn't been initialized yet (e.g. in a unit-test session). */
|
|
19449
|
+
updateBreakdownCategory(key, tokens) {
|
|
19450
|
+
if (!this.session?.contextBreakdownBaseline) return;
|
|
19451
|
+
if (this.session.contextBreakdownBaseline[key] === tokens) return;
|
|
19452
|
+
this.session.contextBreakdownBaseline = {
|
|
19453
|
+
...this.session.contextBreakdownBaseline,
|
|
19454
|
+
[key]: tokens
|
|
19455
|
+
};
|
|
19099
19456
|
}
|
|
19100
19457
|
async replaySessionHistory(sessionId) {
|
|
19101
19458
|
try {
|
|
@@ -19144,6 +19501,10 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
|
|
|
19144
19501
|
() => this.sendAvailableCommandsUpdate()
|
|
19145
19502
|
),
|
|
19146
19503
|
fetchMcpToolMetadata(q, this.logger).then(() => {
|
|
19504
|
+
this.updateBreakdownCategory(
|
|
19505
|
+
"mcp",
|
|
19506
|
+
estimateMcpTokens(getCachedMcpTools())
|
|
19507
|
+
);
|
|
19147
19508
|
const serverNames = getConnectedMcpServerNames();
|
|
19148
19509
|
if (serverNames.length > 0) {
|
|
19149
19510
|
this.options?.onMcpServersReady?.(serverNames);
|
|
@@ -19408,6 +19769,25 @@ function createSessionState(sessionId, cwd, opts) {
|
|
|
19408
19769
|
taskId: opts?.taskId
|
|
19409
19770
|
};
|
|
19410
19771
|
}
|
|
19772
|
+
function resetSessionState(state, sessionId, cwd, opts) {
|
|
19773
|
+
state.sessionId = sessionId;
|
|
19774
|
+
state.cwd = cwd;
|
|
19775
|
+
state.modeId = opts?.modeId ?? "auto";
|
|
19776
|
+
state.modelId = opts?.modelId;
|
|
19777
|
+
state.configOptions = [];
|
|
19778
|
+
state.accumulatedUsage = {
|
|
19779
|
+
inputTokens: 0,
|
|
19780
|
+
outputTokens: 0,
|
|
19781
|
+
cachedReadTokens: 0,
|
|
19782
|
+
cachedWriteTokens: 0
|
|
19783
|
+
};
|
|
19784
|
+
state.contextSize = void 0;
|
|
19785
|
+
state.contextUsed = void 0;
|
|
19786
|
+
state.contextBreakdownBaseline = void 0;
|
|
19787
|
+
state.permissionMode = opts?.permissionMode ?? "auto";
|
|
19788
|
+
state.taskRunId = opts?.taskRunId;
|
|
19789
|
+
state.taskId = opts?.taskId;
|
|
19790
|
+
}
|
|
19411
19791
|
function resetUsage(state) {
|
|
19412
19792
|
state.accumulatedUsage = {
|
|
19413
19793
|
inputTokens: 0,
|
|
@@ -19629,6 +20009,12 @@ function classifyPromptError(error) {
|
|
|
19629
20009
|
message
|
|
19630
20010
|
);
|
|
19631
20011
|
}
|
|
20012
|
+
var CODEX_BASELINE_TOKENS = 12e3;
|
|
20013
|
+
function buildCodexBaseline(meta) {
|
|
20014
|
+
const baseline = emptyBaseline();
|
|
20015
|
+
baseline.systemPrompt = CODEX_BASELINE_TOKENS + estimateTokens(meta?.systemPrompt);
|
|
20016
|
+
return baseline;
|
|
20017
|
+
}
|
|
19632
20018
|
var CODEX_NATIVE_MODE = {
|
|
19633
20019
|
auto: "auto",
|
|
19634
20020
|
default: "auto",
|
|
@@ -19798,7 +20184,7 @@ var CodexAcpAgent = class extends BaseAcpAgent {
|
|
|
19798
20184
|
response.configOptions = normalizeCodexConfigOptions(
|
|
19799
20185
|
response.configOptions
|
|
19800
20186
|
);
|
|
19801
|
-
this.sessionState
|
|
20187
|
+
resetSessionState(this.sessionState, response.sessionId, params.cwd, {
|
|
19802
20188
|
taskRunId: meta?.taskRunId,
|
|
19803
20189
|
taskId: resolveTaskId(meta),
|
|
19804
20190
|
modeId: response.modes?.currentModeId ?? "auto",
|
|
@@ -19807,6 +20193,7 @@ var CodexAcpAgent = class extends BaseAcpAgent {
|
|
|
19807
20193
|
});
|
|
19808
20194
|
this.sessionId = response.sessionId;
|
|
19809
20195
|
this.sessionState.configOptions = response.configOptions ?? [];
|
|
20196
|
+
this.sessionState.contextBreakdownBaseline = buildCodexBaseline(meta);
|
|
19810
20197
|
await this.applyInitialPermissionMode(
|
|
19811
20198
|
response.sessionId,
|
|
19812
20199
|
meta?.permissionMode,
|
|
@@ -19839,7 +20226,7 @@ var CodexAcpAgent = class extends BaseAcpAgent {
|
|
|
19839
20226
|
response.modes?.currentModeId,
|
|
19840
20227
|
meta?.permissionMode
|
|
19841
20228
|
);
|
|
19842
|
-
this.sessionState
|
|
20229
|
+
resetSessionState(this.sessionState, params.sessionId, params.cwd, {
|
|
19843
20230
|
taskRunId: meta?.taskRunId,
|
|
19844
20231
|
taskId: resolveTaskId(meta),
|
|
19845
20232
|
modeId: response.modes?.currentModeId ?? "auto",
|
|
@@ -19847,6 +20234,7 @@ var CodexAcpAgent = class extends BaseAcpAgent {
|
|
|
19847
20234
|
});
|
|
19848
20235
|
this.sessionId = params.sessionId;
|
|
19849
20236
|
this.sessionState.configOptions = response.configOptions ?? [];
|
|
20237
|
+
this.sessionState.contextBreakdownBaseline = buildCodexBaseline(meta);
|
|
19850
20238
|
if (meta?.taskRunId) {
|
|
19851
20239
|
await this.client.extNotification(POSTHOG_NOTIFICATIONS.SDK_SESSION, {
|
|
19852
20240
|
taskRunId: meta.taskRunId,
|
|
@@ -19878,7 +20266,7 @@ var CodexAcpAgent = class extends BaseAcpAgent {
|
|
|
19878
20266
|
loadResponse.modes?.currentModeId,
|
|
19879
20267
|
meta?.permissionMode
|
|
19880
20268
|
);
|
|
19881
|
-
this.sessionState
|
|
20269
|
+
resetSessionState(this.sessionState, params.sessionId, params.cwd, {
|
|
19882
20270
|
taskRunId: meta?.taskRunId,
|
|
19883
20271
|
taskId: resolveTaskId(meta),
|
|
19884
20272
|
modeId: loadResponse.modes?.currentModeId ?? "auto",
|
|
@@ -19886,6 +20274,7 @@ var CodexAcpAgent = class extends BaseAcpAgent {
|
|
|
19886
20274
|
});
|
|
19887
20275
|
this.sessionId = params.sessionId;
|
|
19888
20276
|
this.sessionState.configOptions = loadResponse.configOptions ?? [];
|
|
20277
|
+
this.sessionState.contextBreakdownBaseline = buildCodexBaseline(meta);
|
|
19889
20278
|
if (meta?.taskRunId) {
|
|
19890
20279
|
await this.client.extNotification(POSTHOG_NOTIFICATIONS.SDK_SESSION, {
|
|
19891
20280
|
taskRunId: meta.taskRunId,
|
|
@@ -19917,7 +20306,7 @@ var CodexAcpAgent = class extends BaseAcpAgent {
|
|
|
19917
20306
|
newResponse.configOptions
|
|
19918
20307
|
);
|
|
19919
20308
|
const requestedPermissionMode = toCodexPermissionMode(meta?.permissionMode);
|
|
19920
|
-
this.sessionState
|
|
20309
|
+
resetSessionState(this.sessionState, newResponse.sessionId, params.cwd, {
|
|
19921
20310
|
taskRunId: meta?.taskRunId,
|
|
19922
20311
|
taskId: resolveTaskId(meta),
|
|
19923
20312
|
modeId: newResponse.modes?.currentModeId ?? "auto",
|
|
@@ -19925,6 +20314,7 @@ var CodexAcpAgent = class extends BaseAcpAgent {
|
|
|
19925
20314
|
});
|
|
19926
20315
|
this.sessionId = newResponse.sessionId;
|
|
19927
20316
|
this.sessionState.configOptions = newResponse.configOptions ?? [];
|
|
20317
|
+
this.sessionState.contextBreakdownBaseline = buildCodexBaseline(meta);
|
|
19928
20318
|
await this.applyInitialPermissionMode(
|
|
19929
20319
|
newResponse.sessionId,
|
|
19930
20320
|
meta?.permissionMode,
|
|
@@ -20061,6 +20451,15 @@ var CodexAcpAgent = class extends BaseAcpAgent {
|
|
|
20061
20451
|
});
|
|
20062
20452
|
}
|
|
20063
20453
|
}
|
|
20454
|
+
if (this.sessionState.contextUsed !== void 0) {
|
|
20455
|
+
await this.client.extNotification(POSTHOG_NOTIFICATIONS.USAGE_UPDATE, {
|
|
20456
|
+
sessionId: params.sessionId,
|
|
20457
|
+
breakdown: buildBreakdown(
|
|
20458
|
+
this.sessionState.contextBreakdownBaseline ?? emptyBaseline(),
|
|
20459
|
+
this.sessionState.contextUsed
|
|
20460
|
+
)
|
|
20461
|
+
});
|
|
20462
|
+
}
|
|
20064
20463
|
return response;
|
|
20065
20464
|
}
|
|
20066
20465
|
async interrupt() {
|
|
@@ -20364,7 +20763,7 @@ function createCodexConnection(config) {
|
|
|
20364
20763
|
// src/handoff-checkpoint.ts
|
|
20365
20764
|
import { mkdtemp as mkdtemp2, readFile as readFile4, rm as rm5, writeFile as writeFile2 } from "fs/promises";
|
|
20366
20765
|
import { tmpdir as tmpdir2 } from "os";
|
|
20367
|
-
import { dirname as dirname7, join as
|
|
20766
|
+
import { dirname as dirname7, join as join12 } from "path";
|
|
20368
20767
|
|
|
20369
20768
|
// ../git/dist/handoff.js
|
|
20370
20769
|
import { spawn as spawn5 } from "child_process";
|
|
@@ -20377,148 +20776,6 @@ import { randomUUID as randomUUID2 } from "crypto";
|
|
|
20377
20776
|
import * as fs12 from "fs/promises";
|
|
20378
20777
|
import * as path14 from "path";
|
|
20379
20778
|
|
|
20380
|
-
// ../shared/dist/index.js
|
|
20381
|
-
var CLOUD_PROMPT_PREFIX = "__twig_cloud_prompt_v1__:";
|
|
20382
|
-
function deserializeCloudPrompt(value) {
|
|
20383
|
-
const trimmed2 = value.trim();
|
|
20384
|
-
if (!trimmed2) {
|
|
20385
|
-
return [];
|
|
20386
|
-
}
|
|
20387
|
-
if (!trimmed2.startsWith(CLOUD_PROMPT_PREFIX)) {
|
|
20388
|
-
return [{ type: "text", text: trimmed2 }];
|
|
20389
|
-
}
|
|
20390
|
-
try {
|
|
20391
|
-
const parsed = JSON.parse(trimmed2.slice(CLOUD_PROMPT_PREFIX.length));
|
|
20392
|
-
if (Array.isArray(parsed.blocks) && parsed.blocks.length > 0) {
|
|
20393
|
-
return parsed.blocks;
|
|
20394
|
-
}
|
|
20395
|
-
} catch {
|
|
20396
|
-
}
|
|
20397
|
-
return [{ type: "text", text: trimmed2 }];
|
|
20398
|
-
}
|
|
20399
|
-
function promptBlocksToText(blocks) {
|
|
20400
|
-
return blocks.filter((b) => b.type === "text").map((block) => block.text).join("").trim();
|
|
20401
|
-
}
|
|
20402
|
-
var consoleLogger = {
|
|
20403
|
-
info: (_message, _data) => {
|
|
20404
|
-
},
|
|
20405
|
-
debug: (_message, _data) => {
|
|
20406
|
-
},
|
|
20407
|
-
error: (_message, _data) => {
|
|
20408
|
-
},
|
|
20409
|
-
warn: (_message, _data) => {
|
|
20410
|
-
}
|
|
20411
|
-
};
|
|
20412
|
-
var Saga = class {
|
|
20413
|
-
completedSteps = [];
|
|
20414
|
-
currentStepName = "unknown";
|
|
20415
|
-
stepTimings = [];
|
|
20416
|
-
log;
|
|
20417
|
-
constructor(logger) {
|
|
20418
|
-
this.log = logger ?? consoleLogger;
|
|
20419
|
-
}
|
|
20420
|
-
/**
|
|
20421
|
-
* Run the saga with the given input.
|
|
20422
|
-
* Returns a discriminated union result - either success with data or failure with error details.
|
|
20423
|
-
*/
|
|
20424
|
-
async run(input) {
|
|
20425
|
-
this.completedSteps = [];
|
|
20426
|
-
this.currentStepName = "unknown";
|
|
20427
|
-
this.stepTimings = [];
|
|
20428
|
-
const sagaStart = performance.now();
|
|
20429
|
-
try {
|
|
20430
|
-
const result = await this.execute(input);
|
|
20431
|
-
const totalDuration = performance.now() - sagaStart;
|
|
20432
|
-
this.log.debug("Saga completed successfully", {
|
|
20433
|
-
sagaName: this.sagaName,
|
|
20434
|
-
stepsCompleted: this.completedSteps.length,
|
|
20435
|
-
totalDurationMs: Math.round(totalDuration),
|
|
20436
|
-
stepTimings: this.stepTimings
|
|
20437
|
-
});
|
|
20438
|
-
return { success: true, data: result };
|
|
20439
|
-
} catch (error) {
|
|
20440
|
-
this.log.error("Saga failed, initiating rollback", {
|
|
20441
|
-
sagaName: this.sagaName,
|
|
20442
|
-
failedStep: this.currentStepName,
|
|
20443
|
-
error: error instanceof Error ? error.message : String(error),
|
|
20444
|
-
completedStepTimings: this.stepTimings
|
|
20445
|
-
});
|
|
20446
|
-
await this.rollback();
|
|
20447
|
-
return {
|
|
20448
|
-
success: false,
|
|
20449
|
-
error: error instanceof Error ? error.message : String(error),
|
|
20450
|
-
failedStep: this.currentStepName
|
|
20451
|
-
};
|
|
20452
|
-
}
|
|
20453
|
-
}
|
|
20454
|
-
/**
|
|
20455
|
-
* Execute a step with its rollback action.
|
|
20456
|
-
* If the step succeeds, its rollback action is stored for potential rollback.
|
|
20457
|
-
* The step name is automatically tracked for error reporting.
|
|
20458
|
-
*
|
|
20459
|
-
* @param config - Step configuration with name, execute, and rollback functions
|
|
20460
|
-
* @returns The result of the execute function
|
|
20461
|
-
* @throws Re-throws any error from the execute function (triggers rollback)
|
|
20462
|
-
*/
|
|
20463
|
-
async step(config) {
|
|
20464
|
-
this.currentStepName = config.name;
|
|
20465
|
-
const stepStart = performance.now();
|
|
20466
|
-
const result = await config.execute();
|
|
20467
|
-
const durationMs = Math.round(performance.now() - stepStart);
|
|
20468
|
-
this.stepTimings.push({ name: config.name, durationMs });
|
|
20469
|
-
this.completedSteps.push({
|
|
20470
|
-
name: config.name,
|
|
20471
|
-
rollback: () => config.rollback(result)
|
|
20472
|
-
});
|
|
20473
|
-
return result;
|
|
20474
|
-
}
|
|
20475
|
-
/**
|
|
20476
|
-
* Execute a step that doesn't need rollback.
|
|
20477
|
-
* Useful for read-only operations or operations that are idempotent.
|
|
20478
|
-
* The step name is automatically tracked for error reporting.
|
|
20479
|
-
*
|
|
20480
|
-
* @param name - Step name for logging and error tracking
|
|
20481
|
-
* @param execute - The action to execute
|
|
20482
|
-
* @returns The result of the execute function
|
|
20483
|
-
*/
|
|
20484
|
-
async readOnlyStep(name2, execute) {
|
|
20485
|
-
this.currentStepName = name2;
|
|
20486
|
-
const stepStart = performance.now();
|
|
20487
|
-
const result = await execute();
|
|
20488
|
-
const durationMs = Math.round(performance.now() - stepStart);
|
|
20489
|
-
this.stepTimings.push({ name: name2, durationMs });
|
|
20490
|
-
return result;
|
|
20491
|
-
}
|
|
20492
|
-
/**
|
|
20493
|
-
* Roll back all completed steps in reverse order.
|
|
20494
|
-
* Rollback errors are logged but don't stop the rollback of other steps.
|
|
20495
|
-
*/
|
|
20496
|
-
async rollback() {
|
|
20497
|
-
this.log.info("Rolling back saga", {
|
|
20498
|
-
stepsToRollback: this.completedSteps.length
|
|
20499
|
-
});
|
|
20500
|
-
const stepsReversed = [...this.completedSteps].reverse();
|
|
20501
|
-
for (const step of stepsReversed) {
|
|
20502
|
-
try {
|
|
20503
|
-
await step.rollback();
|
|
20504
|
-
} catch (error) {
|
|
20505
|
-
this.log.error(`Failed to rollback step: ${step.name}`, {
|
|
20506
|
-
error: error instanceof Error ? error.message : String(error)
|
|
20507
|
-
});
|
|
20508
|
-
}
|
|
20509
|
-
}
|
|
20510
|
-
this.log.info("Rollback completed", {
|
|
20511
|
-
stepsAttempted: this.completedSteps.length
|
|
20512
|
-
});
|
|
20513
|
-
}
|
|
20514
|
-
/**
|
|
20515
|
-
* Get the number of completed steps (useful for testing)
|
|
20516
|
-
*/
|
|
20517
|
-
getCompletedStepCount() {
|
|
20518
|
-
return this.completedSteps.length;
|
|
20519
|
-
}
|
|
20520
|
-
};
|
|
20521
|
-
|
|
20522
20779
|
// ../git/dist/git-saga.js
|
|
20523
20780
|
var GitSaga = class extends Saga {
|
|
20524
20781
|
_git = null;
|
|
@@ -21326,10 +21583,10 @@ var HandoffCheckpointTracker = class {
|
|
|
21326
21583
|
}
|
|
21327
21584
|
const gitTracker = this.createGitTracker();
|
|
21328
21585
|
const tmpDir = await mkdtemp2(
|
|
21329
|
-
|
|
21586
|
+
join12(tmpdir2(), `posthog-code-handoff-${checkpoint.checkpointId}-`)
|
|
21330
21587
|
);
|
|
21331
|
-
const packPath =
|
|
21332
|
-
const indexPath =
|
|
21588
|
+
const packPath = join12(tmpDir, `${checkpoint.checkpointId}.pack`);
|
|
21589
|
+
const indexPath = join12(tmpDir, `${checkpoint.checkpointId}.index`);
|
|
21333
21590
|
try {
|
|
21334
21591
|
const downloads = await this.downloadArtifacts([
|
|
21335
21592
|
{
|
|
@@ -21754,7 +22011,7 @@ import { randomUUID as randomUUID3 } from "crypto";
|
|
|
21754
22011
|
import * as fs13 from "fs/promises";
|
|
21755
22012
|
import * as os8 from "os";
|
|
21756
22013
|
import * as path16 from "path";
|
|
21757
|
-
var
|
|
22014
|
+
var CHARS_PER_TOKEN2 = 4;
|
|
21758
22015
|
var DEFAULT_MAX_TOKENS = 15e4;
|
|
21759
22016
|
function estimateTurnTokens(turn) {
|
|
21760
22017
|
let chars = 0;
|
|
@@ -21771,7 +22028,7 @@ function estimateTurnTokens(turn) {
|
|
|
21771
22028
|
}
|
|
21772
22029
|
}
|
|
21773
22030
|
}
|
|
21774
|
-
return Math.ceil(chars /
|
|
22031
|
+
return Math.ceil(chars / CHARS_PER_TOKEN2);
|
|
21775
22032
|
}
|
|
21776
22033
|
function selectRecentTurns(turns, maxTokens = DEFAULT_MAX_TOKENS) {
|
|
21777
22034
|
let budget = maxTokens;
|
|
@@ -24369,7 +24626,7 @@ Continue from where you left off. The user is waiting for your response.`
|
|
|
24369
24626
|
throw new Error(`Failed to download artifact ${artifact.name}`);
|
|
24370
24627
|
}
|
|
24371
24628
|
const safeName = this.getSafeArtifactName(artifact.name);
|
|
24372
|
-
const artifactDir =
|
|
24629
|
+
const artifactDir = join14(
|
|
24373
24630
|
this.config.repositoryPath ?? "/tmp/workspace",
|
|
24374
24631
|
".posthog",
|
|
24375
24632
|
"attachments",
|
|
@@ -24377,7 +24634,7 @@ Continue from where you left off. The user is waiting for your response.`
|
|
|
24377
24634
|
artifact.id ?? safeName
|
|
24378
24635
|
);
|
|
24379
24636
|
await mkdir5(artifactDir, { recursive: true });
|
|
24380
|
-
const artifactPath =
|
|
24637
|
+
const artifactPath = join14(artifactDir, safeName);
|
|
24381
24638
|
await writeFile4(artifactPath, Buffer.from(data));
|
|
24382
24639
|
return resourceLink(pathToFileURL(artifactPath).toString(), artifact.name, {
|
|
24383
24640
|
...artifact.content_type ? { mimeType: artifact.content_type } : {},
|