repairHistory
function repairHistory(messages, opts?): object;Defined in: core/src/context.ts:232
Make a saved, resumed or interrupted history valid to send: every call has exactly one result, right after it, under an id no other call uses.
A span runs from an assistant message with tool calls to the next assistant message. Its results move to just after the calls, in call order, and the rest of the span (a steer the user typed while the tools ran) keeps its order after them. Then:
- A call with no result in its span gets one under the same id, saying it was
interrupted.
isReadOnlypicks the wording; without it every tool may have partly run. These calls come back asinterrupted, under the ids the request carries. - A result whose call is not in its span becomes a user note, images kept. Paired against the span only, because chat and Messages both require the result in the turn right after its call: a looser pairing still 400s.
- A repeated id becomes
{id}_{k}, the smallest free k, call and result together. Kimi and Gemini restart their ids every reply (functions.search:0,call_0), and Anthropic and Responses reject the second use. - An empty id becomes
call_{message}_{call}, nevernormalizeToolId’s random fallback. - An assistant message with no calls and only whitespace is dropped, as if it were never there, so a placeholder saved before the tools ran orphans nothing.
No side effects, and the same input always gives the same bytes: a span’s
output and every id depend only on what came before, so repairing a longer
history keeps the shorter one’s bytes and the prompt cache keeps hitting.
Repairing a repaired history changes nothing. Every adapter here runs this
on what it sends; call it yourself to know what interrupted holds.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
messages |
readonly ChatMessage[] |
opts |
{ isReadOnly?: (toolName) => boolean; } |
opts.isReadOnly? |
(toolName) => boolean |
Returns
Section titled “Returns”object
interrupted
Section titled “interrupted”interrupted: ToolCall[];messages
Section titled “messages”messages: ChatMessage[];