101 lines
5.7 KiB
TypeScript
101 lines
5.7 KiB
TypeScript
import { compactBotRound } from './compact-bot-round.js'
|
|
|
|
/** Read-only history projection. Never removes records or changes cursor/identity/state. */
|
|
const LIMIT = 512
|
|
const MARKER = '\n[Compact history: output omitted]'
|
|
/**
|
|
* Total characters one projection may spend on preserved group rounds. The
|
|
* compact snapshot is a single secure frame (4 MiB ceiling) and the Android
|
|
* follow stream has no transfer fallback, so growth has to stay far below it.
|
|
*/
|
|
const ROUND_BUDGET = 128 * 1024
|
|
/** Tools whose result the client renders as an in-chat card: the component tree lives in
|
|
* `arguments`, and `meta` decides whether the card is rendered at all. */
|
|
const UI_TOOL_NAMES = new Set(['ui_render', 'ui_patch', 'ui_render_widget', 'ui_compose'])
|
|
const object = (value: unknown): value is Record<string, unknown> => value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
|
|
type Budget = { remaining: number }
|
|
|
|
/** Keep a recognised group round inside the document budget, else fall back. */
|
|
function preservedRound(text: string, budget: Budget): string | undefined {
|
|
if (budget.remaining < LIMIT) return undefined
|
|
const round = compactBotRound(text)
|
|
if (round === undefined || round.length > budget.remaining) return undefined
|
|
budget.remaining -= round.length
|
|
return round
|
|
}
|
|
|
|
function summary(value: unknown, budget: Budget): string {
|
|
const text = typeof value === 'string' ? value : JSON.stringify(value) ?? ''
|
|
if (text.length <= LIMIT) return text
|
|
return preservedRound(text, budget) ?? text.slice(0, LIMIT) + MARKER
|
|
}
|
|
|
|
function toolValue(value: unknown, budget: Budget, keepUiPayload = false): unknown {
|
|
if (Array.isArray(value)) return value.map(item => toolValue(item, budget, keepUiPayload))
|
|
if (!object(value)) return value
|
|
const result: Record<string, unknown> = { ...value }
|
|
const toolName = typeof value.name === 'string' ? value.name
|
|
: typeof value.toolName === 'string' ? value.toolName : undefined
|
|
const keepUi = keepUiPayload || (toolName !== undefined && UI_TOOL_NAMES.has(toolName))
|
|
for (const [key, child] of Object.entries(value)) {
|
|
if (key === 'meta' || key === 'uiResultMeta') {
|
|
// The client renders an in-chat card only when `meta.ok` / `meta.ui` survive intact;
|
|
// wrapping this object in `{ summary }` silently drops the whole card.
|
|
result[key] = child
|
|
} else if (['input', 'arguments', 'output', 'result', 'rawInput', 'rawOutput', 'argumentsDelta'].includes(key)) {
|
|
// A UI tool's component tree is its arguments: truncating or wrapping them makes the
|
|
// client's JSON.parse fail, so keep them byte-exact for those tools only.
|
|
if (keepUi && (key === 'arguments' || key === 'argumentsDelta' || key === 'input')) result[key] = child
|
|
else result[key] = typeof child === 'string' ? summary(child, budget) : { summary: summary(child, budget) }
|
|
} else if (key === 'content' && Array.isArray(child)) {
|
|
let previews = 0
|
|
const blocks = child.flatMap(block => {
|
|
// Keep every tool correlation block, even when several share a message.
|
|
if (object(block) && (block.type === 'tool-call' || block.type === 'tool-result')) return [toolValue(block, budget)]
|
|
const preview = object(block) ? block.text ?? block : block
|
|
// A recognised group round is conversation content: keep it even when it
|
|
// would fall outside the two-preview budget. Earlier previews already go
|
|
// through summary(), which preserves the same shape.
|
|
const round = typeof preview === 'string' && preview.length > LIMIT && previews >= 2
|
|
? preservedRound(preview, budget) : undefined
|
|
if (round !== undefined) return [{ type: 'text', text: round }]
|
|
if (previews++ >= 2) return []
|
|
return [{ type: 'text', text: summary(preview, budget) }]
|
|
})
|
|
// An omitted block must never turn a failed result into a success.
|
|
if (child.some(block => object(block) && block.isError === true) && object(blocks[0])) blocks[0].isError = true
|
|
result[key] = blocks
|
|
} else if (key === 'text' && typeof child === 'string') result[key] = summary(child, budget)
|
|
else result[key] = toolValue(child, budget)
|
|
}
|
|
return result
|
|
}
|
|
|
|
function messageValue(value: unknown, budget: Budget): unknown {
|
|
if (Array.isArray(value)) return value.map(item => messageValue(item, budget))
|
|
if (!object(value)) return value
|
|
if (value.type === 'tool-call' || value.type === 'tool-result') return toolValue(value, budget)
|
|
return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, messageValue(child, budget)]))
|
|
}
|
|
|
|
function project(value: unknown, budget: Budget): unknown {
|
|
if (Array.isArray(value)) return value.map(item => project(item, budget))
|
|
if (!object(value)) return value
|
|
// Only history containers and native events are projected. Other RPCs never use this helper.
|
|
if (object(value.event) && ['tool/call', 'tool/result'].includes(String(value.event.type))) {
|
|
const { view, ...rest } = value
|
|
const event = toolValue(value.event, budget) as Record<string, unknown>
|
|
const data = object(event.data) ? event.data : {}
|
|
const title = object(view) && object(view.view) ? view.view.title : undefined
|
|
return { ...rest, event: { ...event, data: { ...data, ...(typeof title === 'string' && !data.name && !data.toolName ? { toolName: title } : {}) } } }
|
|
}
|
|
if (typeof value.type === 'string' && ['tool/call', 'tool/result'].includes(value.type)) return toolValue(value, budget)
|
|
if (typeof value.type === 'string' && ['user/message', 'assistant/message'].includes(value.type)) return messageValue(value, budget)
|
|
return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, project(child, budget)]))
|
|
}
|
|
|
|
export function compactHistory<T>(value: T): T {
|
|
return project(value, { remaining: ROUND_BUDGET }) as T
|
|
}
|