Reduce prompt token overhead with leaner context injections
This commit is contained in:
committed by
Peter Steinberger
parent
7a917602c5
commit
412990a139
@@ -16,6 +16,25 @@ import type { WorkspaceBootstrapFile } from "./workspace.js";
|
||||
|
||||
export type EmbeddedContextFile = { path: string; content: string };
|
||||
|
||||
const MAX_BOOTSTRAP_CHARS = 4000;
|
||||
const BOOTSTRAP_HEAD_CHARS = 2800;
|
||||
const BOOTSTRAP_TAIL_CHARS = 800;
|
||||
|
||||
function trimBootstrapContent(content: string, fileName: string): string {
|
||||
const trimmed = content.trimEnd();
|
||||
if (trimmed.length <= MAX_BOOTSTRAP_CHARS) return trimmed;
|
||||
|
||||
const head = trimmed.slice(0, BOOTSTRAP_HEAD_CHARS);
|
||||
const tail = trimmed.slice(-BOOTSTRAP_TAIL_CHARS);
|
||||
return [
|
||||
head,
|
||||
"",
|
||||
`[...truncated, read ${fileName} for full content...]`,
|
||||
"",
|
||||
tail,
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export async function ensureSessionHeader(params: {
|
||||
sessionFile: string;
|
||||
sessionId: string;
|
||||
@@ -88,12 +107,18 @@ export async function sanitizeSessionMessagesImages(
|
||||
export function buildBootstrapContextFiles(
|
||||
files: WorkspaceBootstrapFile[],
|
||||
): EmbeddedContextFile[] {
|
||||
return files.map((file) => ({
|
||||
path: file.name,
|
||||
content: file.missing
|
||||
? `[MISSING] Expected at: ${file.path}`
|
||||
: (file.content ?? ""),
|
||||
}));
|
||||
const result: EmbeddedContextFile[] = [];
|
||||
for (const file of files) {
|
||||
if (file.missing) continue;
|
||||
const content = file.content ?? "";
|
||||
const trimmed = content.trimEnd();
|
||||
if (!trimmed) continue;
|
||||
result.push({
|
||||
path: file.name,
|
||||
content: trimBootstrapContent(trimmed, file.name),
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function formatAssistantErrorText(
|
||||
|
||||
@@ -34,7 +34,7 @@ describe("buildAgentSystemPromptAppend", () => {
|
||||
expect(prompt).toContain("<final>...</final>");
|
||||
});
|
||||
|
||||
it("lists available and unavailable tools when provided", () => {
|
||||
it("lists available tools when provided", () => {
|
||||
const prompt = buildAgentSystemPromptAppend({
|
||||
workspaceDir: "/tmp/clawd",
|
||||
toolNames: ["bash", "sessions_list", "sessions_history", "sessions_send"],
|
||||
@@ -44,7 +44,6 @@ describe("buildAgentSystemPromptAppend", () => {
|
||||
expect(prompt).toContain("sessions_list");
|
||||
expect(prompt).toContain("sessions_history");
|
||||
expect(prompt).toContain("sessions_send");
|
||||
expect(prompt).toContain("Unavailable tools (do not call):");
|
||||
});
|
||||
|
||||
it("includes user time when provided", () => {
|
||||
|
||||
@@ -85,7 +85,6 @@ export function buildAgentSystemPromptAppend(params: {
|
||||
new Set(normalizedTools.filter((tool) => !toolOrder.includes(tool))),
|
||||
);
|
||||
const enabledTools = toolOrder.filter((tool) => availableTools.has(tool));
|
||||
const disabledTools = toolOrder.filter((tool) => !availableTools.has(tool));
|
||||
const toolLines = enabledTools.map((tool) => {
|
||||
const summary = toolSummaries[tool];
|
||||
return summary ? `- ${tool}: ${summary}` : `- ${tool}`;
|
||||
@@ -160,9 +159,6 @@ export function buildAgentSystemPromptAppend(params: {
|
||||
"- sessions_history: fetch session history",
|
||||
"- sessions_send: send to another session",
|
||||
].join("\n"),
|
||||
disabledTools.length > 0
|
||||
? `Unavailable tools (do not call): ${disabledTools.join(", ")}`
|
||||
: "",
|
||||
"TOOLS.md does not control tool availability; it is user guidance for how to use external tools.",
|
||||
"",
|
||||
params.modelAliasLines && params.modelAliasLines.length > 0
|
||||
|
||||
Reference in New Issue
Block a user