shared/nextedit: trim before-context in a few encodes instead of one per line
ci / build (push) Canceled after 0s
ci / build (push) Canceled after 0s
Same output on 397/398 test prompts, ~6x faster prompt building (daemon and export). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J39imRDT8L7xYbWzsVWd8A
This commit is contained in:
+7
-2
@@ -124,11 +124,16 @@ export function buildNextEditPrompt(input: NextEditInput, encode: Encode, ids: N
|
|||||||
out.push(ids.fileSep, ...encode(`${input.path}\n`))
|
out.push(ids.fileSep, ...encode(`${input.path}\n`))
|
||||||
|
|
||||||
// Before the region: last lines that fit; after: first lines that fit.
|
// Before the region: last lines that fit; after: first lines that fit.
|
||||||
|
// (Window to ~6 chars/token first, then drop whole lines in proportion to the excess: a few encodes, not one per line.)
|
||||||
let before = input.file.slice(0, rStart)
|
let before = input.file.slice(0, rStart)
|
||||||
|
if (before.length > beforeTokens * 6) before = before.slice(before.indexOf('\n', before.length - beforeTokens * 6) + 1)
|
||||||
let beforeIds = encode(before)
|
let beforeIds = encode(before)
|
||||||
while (beforeIds.length > beforeTokens && before.includes('\n')) {
|
while (beforeIds.length > beforeTokens && before.includes('\n')) {
|
||||||
before = before.slice(before.indexOf('\n') + 1)
|
const excess = beforeIds.length - beforeTokens // undershoot (×0.8), then single lines: same cut as line-by-line
|
||||||
beforeIds = beforeIds.length > beforeTokens * 2 ? encode(before.slice(-beforeTokens * 6)) : encode(before)
|
const drop = excess > 32 ? Math.floor(0.8 * before.length * excess / beforeIds.length) : 1
|
||||||
|
const nl = before.indexOf('\n', drop - 1)
|
||||||
|
before = nl < 0 ? '' : before.slice(nl + 1)
|
||||||
|
beforeIds = encode(before)
|
||||||
}
|
}
|
||||||
let after = input.file.slice(rEnd)
|
let after = input.file.slice(rEnd)
|
||||||
let afterIds = encode(after.slice(0, afterTokens * 8))
|
let afterIds = encode(after.slice(0, afterTokens * 8))
|
||||||
|
|||||||
Reference in New Issue
Block a user