Represent tool results as tool-role messages and migrate released wrappers without losing outer own JSON fields, call identity, error state, or writer field order. Refuse nested or wrapper-only information that has no lossless current representation before successor publication. Validate current event relationships directly instead of projecting V4 data back into V3. Keep lifecycle, PTC, title/command, compaction, system-field and retired-syntax checks on catalog restoration and ordinary JSONL opens. The Worker JSON equality helper must inspect own keys so it cannot admit PTC arguments that Node rejects. Keep the source role change, required native admission and consumer fixture updates in one complete review layer. Explain shared-type changes in the persistence record and the native reader rationale in its existing note. Use checked branded fixture identities and discriminant narrowing instead of unknown double casts; fixture values and assertions remain intact. This recomposes the original tool-role work by _Kerman with the independently validated migration and admission repairs by Tianyi Cui. Frozen generations and snapshot input version selection are retained. Co-authored-by: Tianyi Cui <[email protected]>
37 lines
1.5 KiB
TypeScript
37 lines
1.5 KiB
TypeScript
/** Immutable application of the image occurrences recorded by image/offload. */
|
|
|
|
import type { ContentBlock, Message } from '@deepseek-ai/dsh-llm'
|
|
import { deepFreeze } from '@deepseek-ai/dsh-util-values'
|
|
|
|
/**
|
|
* Project selected image occurrences to immutable offloaded blocks.
|
|
* @param message - message projected before this decision.
|
|
* @param indexes - nonempty, strictly increasing depth-first image indexes.
|
|
* @returns an immutable message with the same identity and selected images marked.
|
|
* @throws when a selected occurrence is missing or already offloaded.
|
|
*/
|
|
export function offloadMessageImages(message: Message, indexes: readonly number[]): Message {
|
|
let imageIndex = 0
|
|
let selected = 0
|
|
const visit = (blocks: readonly ContentBlock[]): ContentBlock[] => {
|
|
let next: ContentBlock[] | undefined
|
|
for (const [index, block] of blocks.entries()) {
|
|
let projected = block
|
|
if (block.type === 'image') {
|
|
if (imageIndex === indexes[selected]) {
|
|
if (block.offloaded === true) throw new Error(`image/offload: image index ${imageIndex} is already offloaded`)
|
|
projected = { ...block, offloaded: true }
|
|
selected += 1
|
|
}
|
|
imageIndex += 1
|
|
}
|
|
if (projected !== block) next ??= blocks.slice(0, index)
|
|
next?.push(projected)
|
|
}
|
|
return next ?? blocks as ContentBlock[]
|
|
}
|
|
const content = visit(message.content)
|
|
if (selected !== indexes.length) throw new Error(`image/offload: image index ${indexes[selected]} does not exist`)
|
|
return deepFreeze({ ...message, content })
|
|
}
|