feat(ai-sdk-middleware): ModelMessage-altitude durable compaction API#40
Merged
Conversation
…dified-path lossiness
…3 compactHistory/planCompaction
…ment pending-buffer drop
…erOptions, undefined tool input (both adapters)
…lMessages for durable compaction
…action + adapter fixes
…ns (not ToolLoopAgent steps) + summary/consecutive-user behavior
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
ModelMessage[]-altitude durable-compaction API to@context-chef/ai-sdk-middleware— the message typegenerateText/prepareStepactually use — and deprecates the mis-altitudeLanguageModelV3Promptvariants. Additive, non-breaking.Motivation
"Durable compaction" means summarize, then write the result back to your persisted message store. Hosts persist
UIMessage[]/ModelMessage[]; nobody persistsLanguageModelV3Prompt— that's the ephemeral provider-protocol layer a middleware'stransformParamsbriefly sees. The existingcompactHistory/planCompactionwere typed at that wrong altitude, so a host driving compaction from aToolLoopAgentprepareStep(which handsmessages: ModelMessage[]andmodel: LanguageModel) had no safe path — and a bare cast crashes, becauseModelMessagecontent may be a plainstringthat the V3 adapter's.content.filter(...)chokes on. (middleware.tsalready documented the V3-vs-ModelMessage type split.)Design + plan:
docs/superpowers/specs/2026-06-17-model-message-durable-compaction-altitude-design.md,docs/superpowers/plans/2026-06-17-model-message-durable-compaction-altitude.md.Changes
createCompressionAdapter; no new compaction/flattening logic):compactModelMessages(messages, model, opts) → ModelMessage[]— one-shot durable compaction; returns the input reference on a no-op so callers can skip persistence.planCompactionModelMessages(messages, opts) → { system, toSummarize, toKeep }(allModelMessage[]).summarizeModelMessages(messages, model, opts?) → string.ModelMessage ↔ IRadapter (modelMessageAdapter.ts), parallel to the V3adapter.ts— handles the three ModelMessage-only shapes (string-shorthand content,ImagePart, approval parts); lossless round-trip incl. reasoning byte-exact.createCompressionAdapterwidenedLanguageModelV3→ai'sLanguageModel(string id | V3 | V2) — the typeprepareStep/generateTextgive you. Backward-compatible (existing callers pass a V3 subtype).compactHistory/planCompaction(still exported & working) → JSDoc points to the ModelMessage versions; repointed the persistence warning, READMEs (EN + zh), andCompressOptionsJSDoc.summarizeMessagesis not deprecated.tool-resultno longer triggers a spurious[No tool result available]placeholder on round-trip;providerOptions(e.g. Anthropic cache breakpoint) preserved on round-trip;tool-callwithinput: undefinedserializes to'{}'(a string), notundefined.Testing
pnpm --filter @context-chef/ai-sdk-middleware test).tsc --noEmit, exit 0).biome checkonsrc, clean).@context-chef/coreis unchanged (the IR engine was already at the correct altitude).Breaking changes
None. Additive; the V3
compactHistory/planCompactionremain exported and functional (now@deprecated, scheduled for removal in the next major).Reviewer notes — intentionally deferred (low / cleanup / V3-inherited)
Adjacent-tool-message coalescing, cross-message leading-approval drop (edge shape), no-op-vs-firing sanitization asymmetry, media-only tool output → empty summary text, multi-reasoning collapse, adapter duplication (acknowledged in-file),
asMM/CompactionPlanclones, eager 3-slice conversion inplanCompactionModelMessages, user-branch multi-pass, and theLanguageModelstring-id widening dropping a compile-time guard. None are correctness blockers; happy to address in follow-ups if desired.