Skip to content

Commit a36c650

Browse files
committed
fix: correctly prune edit tool inputs (oldString/newString instead of content)
1 parent fecca94 commit a36c650

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/messages/prune.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,18 @@ const pruneToolInputs = (
206206
continue
207207
}
208208

209-
if (part.state.input?.content !== undefined) {
209+
// Write tool has content field, edit tool has oldString/newString fields
210+
if (part.tool === 'write' && part.state.input?.content !== undefined) {
210211
part.state.input.content = PRUNED_TOOL_INPUT_REPLACEMENT
211212
}
213+
if (part.tool === 'edit') {
214+
if (part.state.input?.oldString !== undefined) {
215+
part.state.input.oldString = PRUNED_TOOL_INPUT_REPLACEMENT
216+
}
217+
if (part.state.input?.newString !== undefined) {
218+
part.state.input.newString = PRUNED_TOOL_INPUT_REPLACEMENT
219+
}
220+
}
212221
}
213222
}
214223
}

lib/strategies/utils.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,25 @@ export const calculateTokensSaved = (
5555
}
5656
// For write and edit tools, count input content as that is all we prune for these tools
5757
// (input is present in both completed and error states)
58-
if (part.tool === "write" || part.tool === "edit") {
58+
if (part.tool === "write") {
5959
const inputContent = part.state.input?.content
6060
const content = typeof inputContent === 'string'
6161
? inputContent
6262
: JSON.stringify(inputContent ?? '')
6363
contents.push(content)
6464
continue
6565
}
66+
if (part.tool === "edit") {
67+
const oldString = part.state.input?.oldString
68+
const newString = part.state.input?.newString
69+
if (typeof oldString === 'string') {
70+
contents.push(oldString)
71+
}
72+
if (typeof newString === 'string') {
73+
contents.push(newString)
74+
}
75+
continue
76+
}
6677
// For other tools, count output or error based on status
6778
if (part.state.status === "completed") {
6879
const content = typeof part.state.output === 'string'

0 commit comments

Comments
 (0)