dsh-compaction-adaptive 0.0.0-stage → 0.1.3
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/LICENSE +21 -0
- package/README.md +170 -2
- package/cordis.patch.yml +17 -0
- package/docs/DETAIL.md +174 -0
- package/icon.svg +18 -0
- package/lib/chunking.js +459 -0
- package/lib/index.js +667 -0
- package/lib/policy.js +315 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +51 -4
package/lib/chunking.js
ADDED
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Byte-bounded chunked summarization for requests the single-shot path cannot
|
|
3
|
+
* ship.
|
|
4
|
+
*
|
|
5
|
+
* The compaction backend receives one summarization input per attempt: the
|
|
6
|
+
* replayed prefix (system prompt, tool schemas, shadowed messages) plus the
|
|
7
|
+
* summarizer's trailing instruction. For a large window that payload can exceed
|
|
8
|
+
* what a relay or a small-window summary model accepts in one request, and the
|
|
9
|
+
* whole attempt then fails. This module cuts the *shadowed content* into
|
|
10
|
+
* byte-bounded parts, digests each part, and folds the digests into one
|
|
11
|
+
* checkpoint in the format the harness expects.
|
|
12
|
+
*
|
|
13
|
+
* Everything here is pure except `streamText`, which takes a caller-supplied
|
|
14
|
+
* stream factory: packing, text projection, and prompt construction are
|
|
15
|
+
* testable without a Context, a Session, or a provider.
|
|
16
|
+
*
|
|
17
|
+
* @module dsh-compaction-adaptive/chunking
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Chunking defaults, chosen to stay below a 3 MB request-body limit. */
|
|
21
|
+
export const CHUNK_DEFAULTS = Object.freeze({
|
|
22
|
+
chunking: 'auto',
|
|
23
|
+
chunkSizing: 'adaptive',
|
|
24
|
+
chunkTriggerBytes: 2_400_000,
|
|
25
|
+
chunkBytes: 700_000,
|
|
26
|
+
chunkConcurrency: 2,
|
|
27
|
+
chunkMaxTokens: 4_000,
|
|
28
|
+
mergeMaxTokens: 6_000,
|
|
29
|
+
chunkRetries: 1,
|
|
30
|
+
chunkShrinkAttempts: 2,
|
|
31
|
+
chunkSingleShotRatio: 0.7,
|
|
32
|
+
chunkTokenRatio: 0.45,
|
|
33
|
+
chunkShrinkFactor: 0.6,
|
|
34
|
+
chunkRecoverFactor: 1.15,
|
|
35
|
+
chunkMinScale: 0.25,
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
/** Supported chunking modes. */
|
|
39
|
+
export const CHUNK_MODES = Object.freeze(['auto', 'off'])
|
|
40
|
+
|
|
41
|
+
/** Supported sizing modes: adaptive derives budgets from the summary model. */
|
|
42
|
+
export const SIZING_MODES = Object.freeze(['adaptive', 'fixed'])
|
|
43
|
+
|
|
44
|
+
function fail(message) {
|
|
45
|
+
throw new Error(`dsh-compaction-adaptive: ${message}`)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function assertNumber(name, value, { min, max, integer = false } = {}) {
|
|
49
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
50
|
+
fail(`${name} (${String(value)}) must be a finite number`)
|
|
51
|
+
}
|
|
52
|
+
if (integer && !Number.isInteger(value)) fail(`${name} (${value}) must be an integer`)
|
|
53
|
+
if (min !== undefined && value < min) fail(`${name} (${value}) must be >= ${min}`)
|
|
54
|
+
if (max !== undefined && value > max) fail(`${name} (${value}) must be <= ${max}`)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Validate the chunking half of the plugin configuration.
|
|
59
|
+
* @param {Record<string, unknown>} [raw] - raw plugin configuration.
|
|
60
|
+
* @returns {Readonly<Record<string, any>>} resolved chunking configuration.
|
|
61
|
+
*/
|
|
62
|
+
export function resolveChunkConfig(raw = {}) {
|
|
63
|
+
const config = { ...CHUNK_DEFAULTS }
|
|
64
|
+
for (const key of Object.keys(CHUNK_DEFAULTS)) {
|
|
65
|
+
if (raw[key] !== undefined) config[key] = raw[key]
|
|
66
|
+
}
|
|
67
|
+
if (!CHUNK_MODES.includes(config.chunking)) {
|
|
68
|
+
fail(`chunking (${String(config.chunking)}) must be one of ${CHUNK_MODES.join(', ')}`)
|
|
69
|
+
}
|
|
70
|
+
if (!SIZING_MODES.includes(config.chunkSizing)) {
|
|
71
|
+
fail(`chunkSizing (${String(config.chunkSizing)}) must be one of ${SIZING_MODES.join(', ')}`)
|
|
72
|
+
}
|
|
73
|
+
assertNumber('chunkTriggerBytes', config.chunkTriggerBytes, { min: 10_000, integer: true })
|
|
74
|
+
assertNumber('chunkBytes', config.chunkBytes, { min: 10_000, integer: true })
|
|
75
|
+
assertNumber('chunkConcurrency', config.chunkConcurrency, { min: 1, max: 8, integer: true })
|
|
76
|
+
assertNumber('chunkMaxTokens', config.chunkMaxTokens, { min: 128, integer: true })
|
|
77
|
+
assertNumber('mergeMaxTokens', config.mergeMaxTokens, { min: 128, integer: true })
|
|
78
|
+
assertNumber('chunkRetries', config.chunkRetries, { min: 0, max: 5, integer: true })
|
|
79
|
+
assertNumber('chunkShrinkAttempts', config.chunkShrinkAttempts, { min: 0, max: 4, integer: true })
|
|
80
|
+
assertNumber('chunkSingleShotRatio', config.chunkSingleShotRatio, { min: 0.1, max: 1 })
|
|
81
|
+
assertNumber('chunkTokenRatio', config.chunkTokenRatio, { min: 0.05, max: 1 })
|
|
82
|
+
assertNumber('chunkShrinkFactor', config.chunkShrinkFactor, { min: 0.1, max: 0.95 })
|
|
83
|
+
assertNumber('chunkRecoverFactor', config.chunkRecoverFactor, { min: 1, max: 2 })
|
|
84
|
+
assertNumber('chunkMinScale', config.chunkMinScale, { min: 0.05, max: 1 })
|
|
85
|
+
if (config.chunkBytes > config.chunkTriggerBytes) {
|
|
86
|
+
fail(`chunkBytes (${config.chunkBytes}) must not exceed chunkTriggerBytes (${config.chunkTriggerBytes})`)
|
|
87
|
+
}
|
|
88
|
+
return Object.freeze(config)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** UTF-8 byte length of a string, as a relay counts it. */
|
|
92
|
+
export function utf8Bytes(text) {
|
|
93
|
+
return Buffer.byteLength(text, 'utf8')
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Size one summarization request the way a transport would: the serialized
|
|
98
|
+
* messages plus the tool schemas the call would carry.
|
|
99
|
+
* @param {{messages?: readonly any[], tools?: readonly any[]}} input - summarization input.
|
|
100
|
+
* @returns {number} approximate request body size in bytes.
|
|
101
|
+
*/
|
|
102
|
+
export function requestBytes(input) {
|
|
103
|
+
let bytes = 0
|
|
104
|
+
for (const message of input.messages ?? []) {
|
|
105
|
+
bytes += utf8Bytes(JSON.stringify(message)) + 1
|
|
106
|
+
}
|
|
107
|
+
if (input.tools !== undefined && input.tools.length > 0) {
|
|
108
|
+
bytes += utf8Bytes(JSON.stringify(input.tools))
|
|
109
|
+
}
|
|
110
|
+
return bytes
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Split the summarization input into a system head and per-message text parts.
|
|
115
|
+
*
|
|
116
|
+
* Reasoning blocks are dropped: they are not part of what another model needs,
|
|
117
|
+
* and they dominate the byte count of long reasoning-heavy sessions.
|
|
118
|
+
* @param {readonly any[]} messages - replayed conversation prefix.
|
|
119
|
+
* @returns {{system: string | null, parts: string[]}} projected text.
|
|
120
|
+
*/
|
|
121
|
+
export function projectMessages(messages) {
|
|
122
|
+
let system = null
|
|
123
|
+
const parts = []
|
|
124
|
+
for (const message of messages) {
|
|
125
|
+
const content = Array.isArray(message?.content) ? message.content : []
|
|
126
|
+
const blocks = []
|
|
127
|
+
for (const block of content) {
|
|
128
|
+
switch (block?.type) {
|
|
129
|
+
case 'text':
|
|
130
|
+
if (typeof block.text === 'string' && block.text.length > 0) blocks.push(block.text)
|
|
131
|
+
break
|
|
132
|
+
case 'tool-call':
|
|
133
|
+
blocks.push(`<tool-call ${block.name ?? ''}>\n${String(block.arguments ?? '')}`)
|
|
134
|
+
break
|
|
135
|
+
case 'reasoning':
|
|
136
|
+
break
|
|
137
|
+
case 'image':
|
|
138
|
+
blocks.push('[image omitted]')
|
|
139
|
+
break
|
|
140
|
+
default:
|
|
141
|
+
blocks.push(JSON.stringify(block))
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const body = blocks.join('\n').trim()
|
|
145
|
+
if (body.length === 0) continue
|
|
146
|
+
if (message.role === 'system' && system === null) {
|
|
147
|
+
system = body
|
|
148
|
+
continue
|
|
149
|
+
}
|
|
150
|
+
const label = message.role === 'assistant' ? '### assistant'
|
|
151
|
+
: message.role === 'tool' ? '### tool result'
|
|
152
|
+
: '### user'
|
|
153
|
+
parts.push(`${label}\n${body}`)
|
|
154
|
+
}
|
|
155
|
+
return { system, parts }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Pack text parts into byte-bounded chunks, preserving order and never
|
|
160
|
+
* splitting a part that already fits. One oversized part is cut by characters
|
|
161
|
+
* with a 3-bytes-per-character worst case, so a CJK part cannot overshoot.
|
|
162
|
+
* @param {readonly string[]} parts - ordered text parts.
|
|
163
|
+
* @param {number} limitBytes - per-chunk UTF-8 budget.
|
|
164
|
+
* @returns {string[]} chunks in conversation order.
|
|
165
|
+
*/
|
|
166
|
+
export function packChunks(parts, limitBytes) {
|
|
167
|
+
const chunks = []
|
|
168
|
+
let current = ''
|
|
169
|
+
let currentBytes = 0
|
|
170
|
+
const flush = () => {
|
|
171
|
+
if (current.length > 0) chunks.push(current)
|
|
172
|
+
current = ''
|
|
173
|
+
currentBytes = 0
|
|
174
|
+
}
|
|
175
|
+
const pieceChars = Math.max(1000, Math.floor(limitBytes / 3))
|
|
176
|
+
for (const part of parts) {
|
|
177
|
+
if (!part) continue
|
|
178
|
+
const bytes = utf8Bytes(part)
|
|
179
|
+
if (bytes > limitBytes) {
|
|
180
|
+
flush()
|
|
181
|
+
for (let offset = 0; offset < part.length; offset += pieceChars) {
|
|
182
|
+
chunks.push(part.slice(offset, offset + pieceChars))
|
|
183
|
+
}
|
|
184
|
+
continue
|
|
185
|
+
}
|
|
186
|
+
if (currentBytes + bytes + 1 > limitBytes) flush()
|
|
187
|
+
current = current === '' ? part : `${current}\n${part}`
|
|
188
|
+
currentBytes += bytes + 1
|
|
189
|
+
}
|
|
190
|
+
flush()
|
|
191
|
+
return chunks
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Conservative token price for one text: the harness heuristic, raised to one
|
|
196
|
+
* token per three UTF-8 bytes when that is larger.
|
|
197
|
+
*
|
|
198
|
+
* The harness heuristic is density-based on characters, so CJK content can
|
|
199
|
+
* undercount by ~3x against a real tokenizer. The byte floor is exact for CJK
|
|
200
|
+
* and only ~33% pessimistic for ASCII, which is the right side to err on when
|
|
201
|
+
* the number decides whether a request fits.
|
|
202
|
+
* @param {string} text - measured text.
|
|
203
|
+
* @param {number} estimate - heuristic estimate for the same text.
|
|
204
|
+
* @returns {number} upper-bound tokens.
|
|
205
|
+
*/
|
|
206
|
+
export function conservativeTokens(text, estimate) {
|
|
207
|
+
const heuristic = Number.isFinite(estimate) ? estimate : 0
|
|
208
|
+
return Math.max(heuristic, Math.ceil(utf8Bytes(text) / 3))
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Decide whether the single-shot request fits, and how big one part may be.
|
|
213
|
+
*
|
|
214
|
+
* Adaptive sizing derives both budgets from the model that will actually
|
|
215
|
+
* summarize: its declared window, the output it reserves, and the fixed
|
|
216
|
+
* overhead of the system prompt and instruction. The byte caps remain the
|
|
217
|
+
* transport guard, and `scale` is the learned multiplier for a route that has
|
|
218
|
+
* already rejected a request.
|
|
219
|
+
*
|
|
220
|
+
* @param {object} input - planning input.
|
|
221
|
+
* @param {number} input.contentTokens - conservative tokens the single-shot call would carry.
|
|
222
|
+
* @param {number} input.contentBytes - UTF-8 bytes of the same payload.
|
|
223
|
+
* @param {{window: number, reservedTokens: number, overheadTokens: number}} input.capacity - summarizer capacity.
|
|
224
|
+
* @param {Readonly<Record<string, any>>} input.config - resolved chunking configuration.
|
|
225
|
+
* @param {number} [input.scale] - learned capacity multiplier in [minScale, 1].
|
|
226
|
+
* @returns {{needed: boolean, reason: string, scale: number, tokenBudget: number, byteBudget: number, partTokenBudget: number, partByteBudget: number}} plan.
|
|
227
|
+
*/
|
|
228
|
+
export function planChunking(input) {
|
|
229
|
+
const config = input.config
|
|
230
|
+
const scale = Math.min(1, Math.max(config.chunkMinScale, input.scale ?? 1))
|
|
231
|
+
const fixed = config.chunkSizing === 'fixed'
|
|
232
|
+
|
|
233
|
+
const capacityTokens = input.capacity.window > 0
|
|
234
|
+
? Math.max(0, input.capacity.window - input.capacity.reservedTokens - input.capacity.overheadTokens)
|
|
235
|
+
: Number.POSITIVE_INFINITY
|
|
236
|
+
const singleShotTokens = fixed ? capacityTokens : Math.floor(capacityTokens * config.chunkSingleShotRatio)
|
|
237
|
+
const partTokens = fixed ? capacityTokens : Math.floor(capacityTokens * config.chunkTokenRatio)
|
|
238
|
+
|
|
239
|
+
const tokenBudget = Math.floor(singleShotTokens * scale)
|
|
240
|
+
const byteBudget = Math.floor(config.chunkTriggerBytes * scale)
|
|
241
|
+
const partTokenBudget = Math.max(256, Math.floor(partTokens * scale))
|
|
242
|
+
const partByteBudget = Math.max(10_000, Math.floor(config.chunkBytes * scale))
|
|
243
|
+
|
|
244
|
+
if (config.chunking === 'off') {
|
|
245
|
+
return {
|
|
246
|
+
needed: false, reason: 'chunking disabled', scale, tokenBudget, byteBudget, partTokenBudget, partByteBudget,
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const overTokens = input.contentTokens > tokenBudget
|
|
250
|
+
const overBytes = input.contentBytes > byteBudget
|
|
251
|
+
const reason = overTokens
|
|
252
|
+
? `${input.contentTokens} tokens > ${tokenBudget} budget`
|
|
253
|
+
: overBytes
|
|
254
|
+
? `${input.contentBytes} bytes > ${byteBudget} budget`
|
|
255
|
+
: `${input.contentTokens} tokens / ${input.contentBytes} bytes fit the single-shot budget`
|
|
256
|
+
return {
|
|
257
|
+
needed: overTokens || overBytes,
|
|
258
|
+
reason,
|
|
259
|
+
scale,
|
|
260
|
+
tokenBudget,
|
|
261
|
+
byteBudget,
|
|
262
|
+
partTokenBudget,
|
|
263
|
+
partByteBudget,
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Pack text parts into chunks that satisfy both a token and a byte budget.
|
|
269
|
+
*
|
|
270
|
+
* An oversized single part is cut into character pieces sized so either bound
|
|
271
|
+
* holds in the worst case (1 token per character, 3 bytes per character).
|
|
272
|
+
* @param {readonly string[]} parts - ordered text parts.
|
|
273
|
+
* @param {{tokenBudget: number, byteBudget: number}} budget - per-chunk limits.
|
|
274
|
+
* @param {(text: string) => number} estimate - heuristic token estimator.
|
|
275
|
+
* @returns {string[]} chunks in conversation order.
|
|
276
|
+
*/
|
|
277
|
+
export function packByBudget(parts, budget, estimate) {
|
|
278
|
+
const tokenBudget = Math.max(1, budget.tokenBudget)
|
|
279
|
+
const byteBudget = Math.max(3, budget.byteBudget)
|
|
280
|
+
const charsPerPiece = Math.max(500, Math.min(Math.floor(byteBudget / 3), tokenBudget))
|
|
281
|
+
const chunks = []
|
|
282
|
+
let current = ''
|
|
283
|
+
let currentTokens = 0
|
|
284
|
+
let currentBytes = 0
|
|
285
|
+
const flush = () => {
|
|
286
|
+
if (current.length > 0) chunks.push(current)
|
|
287
|
+
current = ''
|
|
288
|
+
currentTokens = 0
|
|
289
|
+
currentBytes = 0
|
|
290
|
+
}
|
|
291
|
+
for (const part of parts) {
|
|
292
|
+
if (!part) continue
|
|
293
|
+
const partBytes = utf8Bytes(part)
|
|
294
|
+
const partTokens = conservativeTokens(part, estimate(part))
|
|
295
|
+
if (partBytes > byteBudget || partTokens > tokenBudget) {
|
|
296
|
+
flush()
|
|
297
|
+
for (let offset = 0; offset < part.length; offset += charsPerPiece) {
|
|
298
|
+
chunks.push(part.slice(offset, offset + charsPerPiece))
|
|
299
|
+
}
|
|
300
|
+
continue
|
|
301
|
+
}
|
|
302
|
+
if (currentBytes + partBytes + 1 > byteBudget || currentTokens + partTokens + 1 > tokenBudget) flush()
|
|
303
|
+
current = current === '' ? part : `${current}\n${part}`
|
|
304
|
+
currentBytes += partBytes + 1
|
|
305
|
+
currentTokens += partTokens + 1
|
|
306
|
+
}
|
|
307
|
+
flush()
|
|
308
|
+
return chunks
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Whether a failure looks like "the request was too big" rather than a real fault. */
|
|
312
|
+
export function isSizeFailure(error) {
|
|
313
|
+
if (error === null || typeof error !== 'object') return false
|
|
314
|
+
const code = String(error.code ?? '')
|
|
315
|
+
if (code === 'MAX_TOKENS' || code === 'CONTEXT_WINDOW_EXCEEDED' || code === 'request_too_large') return true
|
|
316
|
+
const message = `${error.message ?? ''} ${error.cause?.message ?? ''}`
|
|
317
|
+
return /context (window|length)|too (large|long)|maximum context|max(imum)? tokens|request entity too large|\b413\b|payload/i.test(message)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Update a route's learned capacity multiplier.
|
|
322
|
+
* @param {number} previous - current multiplier.
|
|
323
|
+
* @param {'shrink' | 'recover'} outcome - observed outcome.
|
|
324
|
+
* @param {Readonly<Record<string, any>>} config - resolved chunking configuration.
|
|
325
|
+
* @returns {number} next multiplier.
|
|
326
|
+
*/
|
|
327
|
+
export function nextScale(previous, outcome, config) {
|
|
328
|
+
const base = Number.isFinite(previous) ? previous : 1
|
|
329
|
+
const scaled = outcome === 'shrink' ? base * config.chunkShrinkFactor : base * config.chunkRecoverFactor
|
|
330
|
+
return Math.min(1, Math.max(config.chunkMinScale, scaled))
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** Instruction for one digest call. */
|
|
334
|
+
export function chunkInstruction(index, total) {
|
|
335
|
+
return [
|
|
336
|
+
`You are condensing part ${index + 1} of ${total} of one long conversation, so another model can resume the work from the digests alone.`,
|
|
337
|
+
'',
|
|
338
|
+
'Write terse engineering notes for THIS part only: goals and their evolution, decisions with rationale, exact file paths, commands, identifiers, error strings, numbers, and unfinished work.',
|
|
339
|
+
'Preserve verbatim anything whose exact wording matters. Do not restate the conversation, do not address the reader, and do not call any tool.',
|
|
340
|
+
'If this part contains a prior checkpoint block, keep the facts that are still true and drop what is stale.',
|
|
341
|
+
].join('\n')
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** Instruction for the folding call, matching the single-shot checkpoint format. */
|
|
345
|
+
export function mergeInstruction(partCount) {
|
|
346
|
+
return [
|
|
347
|
+
`You are now acting as a compaction engine for this AI coding assistant. ${partCount} partial digests of one conversation follow. Condense them into a single structured checkpoint that lets another model resume the work with no loss of essential context.`,
|
|
348
|
+
'',
|
|
349
|
+
'Output EXACTLY the Markdown structure below: keep every section, in order. Use terse bullets, not prose paragraphs. Write "(none)" for an empty section — never drop a section.',
|
|
350
|
+
'',
|
|
351
|
+
'## Primary Request and Intent',
|
|
352
|
+
"- [the user's original and evolving goals; quote verbatim where the exact wording matters]",
|
|
353
|
+
'',
|
|
354
|
+
'## Key Technical Concepts',
|
|
355
|
+
'- [technologies, frameworks, patterns, and conventions in play]',
|
|
356
|
+
'',
|
|
357
|
+
'## Files and Code',
|
|
358
|
+
'- [exact path: why it matters, key changes or snippets]',
|
|
359
|
+
'',
|
|
360
|
+
'## Errors and Fixes',
|
|
361
|
+
'- [error: how it was resolved, plus any related user feedback]',
|
|
362
|
+
'',
|
|
363
|
+
'## Pending Jobs',
|
|
364
|
+
'- [explicitly requested work not yet completed]',
|
|
365
|
+
'',
|
|
366
|
+
'## Current Work',
|
|
367
|
+
'- [precisely what was in progress at this checkpoint]',
|
|
368
|
+
'',
|
|
369
|
+
'## Next Step',
|
|
370
|
+
'- [the single next action, directly in line with the most recent request, or "(none)"]',
|
|
371
|
+
'',
|
|
372
|
+
'## Critical Context',
|
|
373
|
+
'- [decisions and their rationale, constraints, user preferences, open questions, data needed to continue]',
|
|
374
|
+
'',
|
|
375
|
+
'Rules:',
|
|
376
|
+
'- Write concise English engineering prose. Preserve exact file paths, commands, error strings, identifiers, numeric values, function signatures, and syntax fragments.',
|
|
377
|
+
'- Capture user feedback and explicit instructions faithfully, especially corrections.',
|
|
378
|
+
'- Do NOT mention this summarization request or that the context was compacted.',
|
|
379
|
+
'- Output only the checkpoint text: do not call any tool or take any other action.',
|
|
380
|
+
].join('\n')
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** Build the request messages for one digest call. */
|
|
384
|
+
export function digestMessages(system, chunkText, index, total) {
|
|
385
|
+
const messages = []
|
|
386
|
+
if (system !== null && system.length > 0) {
|
|
387
|
+
messages.push({ role: 'system', content: [{ type: 'text', text: system }] })
|
|
388
|
+
}
|
|
389
|
+
messages.push({
|
|
390
|
+
role: 'user',
|
|
391
|
+
content: [{ type: 'text', text: `${chunkInstruction(index, total)}\n\n---\n\n${chunkText}` }],
|
|
392
|
+
})
|
|
393
|
+
return messages
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** Build the request messages for the folding call. */
|
|
397
|
+
export function mergeMessages(system, digests) {
|
|
398
|
+
const messages = []
|
|
399
|
+
if (system !== null && system.length > 0) {
|
|
400
|
+
messages.push({ role: 'system', content: [{ type: 'text', text: system }] })
|
|
401
|
+
}
|
|
402
|
+
const body = digests.map((digest, index) => `--- digest ${index + 1} ---\n${digest}`).join('\n\n')
|
|
403
|
+
messages.push({
|
|
404
|
+
role: 'user',
|
|
405
|
+
content: [{ type: 'text', text: `${mergeInstruction(digests.length)}\n\n${body}` }],
|
|
406
|
+
})
|
|
407
|
+
return messages
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Run one streaming completion and return its text.
|
|
412
|
+
* @param {(options: any) => AsyncIterable<any>} stream - LLM stream factory.
|
|
413
|
+
* @param {object} options - generate options, without messages.
|
|
414
|
+
* @param {readonly any[]} messages - request messages.
|
|
415
|
+
* @param {{BlockAssembler: any, LlmError: any}} llm - assembly classes from the LLM seam.
|
|
416
|
+
* @returns {Promise<string>} trimmed text output.
|
|
417
|
+
*/
|
|
418
|
+
export async function streamText(stream, options, messages, llm) {
|
|
419
|
+
const assembler = new llm.BlockAssembler()
|
|
420
|
+
for await (const chunk of stream({ ...options, messages })) assembler.push(chunk)
|
|
421
|
+
const finish = assembler.finish
|
|
422
|
+
if (finish?.kind === 'error' || finish?.kind === 'aborted') {
|
|
423
|
+
throw llm.LlmError.fromFailure(finish.failure)
|
|
424
|
+
}
|
|
425
|
+
if (finish?.kind === 'max-tokens') {
|
|
426
|
+
const error = new Error('chunked summarization truncated at the token cap')
|
|
427
|
+
error.code = 'MAX_TOKENS'
|
|
428
|
+
throw error
|
|
429
|
+
}
|
|
430
|
+
const text = assembler.blocks()
|
|
431
|
+
.filter((block) => block.type === 'text')
|
|
432
|
+
.map((block) => block.text)
|
|
433
|
+
.join('\n')
|
|
434
|
+
.trim()
|
|
435
|
+
if (text.length === 0) throw new Error('chunked summarization produced no text')
|
|
436
|
+
return text
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Map over items with bounded concurrency, preserving input order.
|
|
441
|
+
* @param {readonly any[]} items - work items.
|
|
442
|
+
* @param {number} limit - maximum in-flight tasks.
|
|
443
|
+
* @param {(item: any, index: number) => Promise<any>} run - task.
|
|
444
|
+
* @returns {Promise<any[]>} results in input order.
|
|
445
|
+
*/
|
|
446
|
+
export async function mapLimit(items, limit, run) {
|
|
447
|
+
const results = new Array(items.length)
|
|
448
|
+
let next = 0
|
|
449
|
+
const workers = Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
450
|
+
for (;;) {
|
|
451
|
+
const index = next
|
|
452
|
+
next += 1
|
|
453
|
+
if (index >= items.length) return
|
|
454
|
+
results[index] = await run(items[index], index)
|
|
455
|
+
}
|
|
456
|
+
})
|
|
457
|
+
await Promise.all(workers)
|
|
458
|
+
return results
|
|
459
|
+
}
|