refactor: move durable compaction engine into core#39
Merged
Conversation
planCompaction/compactHistory move from @context-chef/ai-sdk-middleware into @context-chef/core as a provider-agnostic engine over the IR (Message[]). compactHistory takes an injected `compress` callback so core never calls a model directly; it returns the input history reference unchanged on a no-op. ai-sdk-middleware now re-exports them as thin AI-SDK wrappers (fromAISDK -> core -> toAISDK), preserving the no-op reference guarantee via `result === ir ? prompt : toAISDK(result)`. Public middleware API signatures are unchanged; createCompressionAdapter is exported module-level for the wrapper. core: minor (new exports), ai-sdk-middleware: patch.
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.
What
Moves the durable-compaction engine —
planCompaction/compactHistory— out of@context-chef/ai-sdk-middlewareand into@context-chef/core, where it belongs: the algorithm operates on the provider-agnostic IR (Message[]) and only ever touched AI SDK types at the boundary. The middleware functions become thin wrappers.This corrects an altitude mistake from
a461ef8(unreleased, sitting in release PR #38): the library already factored its compaction primitives (groupIntoTurns,summarizeHistory,compactMessages) into core, and middleware'ssummarizeMessageswas already a thinfromAISDK → core → toAISDKshell. The two new helpers broke that contract by putting the engine in the boundary layer.Doing this before #38 merges means core ships the helpers correctly from day one; the middleware public API is never observed with the logic in the wrong layer.
How
@context-chef/coregainsplanCompaction(history, options)andcompactHistory(history, compress, options)overMessage[].compactHistorytakes an injectedcompresscallback (NOT a model), same pattern assummarizeHistory, so core stays provider-agnostic — noai/@ai-sdk/providerdependency.historyreference unchanged on a no-op (nothing old enough, or a blank summary), so callers can skip persistence viaresult === history.Message[]with system inline; direct adapter users (fromAnthropic/fromOpenAI/fromGemini) reassemble[...system, ...history].@context-chef/ai-sdk-middlewareplanCompaction/compactHistorybecome thin AI-SDK wrappers (fromAISDK → core → toAISDK), bindingmodelviacreateCompressionAdapter. Signatures and behavior unchanged, including the no-op reference guarantee (result === ir ? prompt : toAISDK(result)).createCompressionAdapteris exported module-level (not in the package public index).generateTextstays in middleware — the only true AI SDK binding.Semver
@context-chef/core: minor (new additive exports).@context-chef/ai-sdk-middleware: patch (internal refactor, API unchanged).@context-chef/tanstack-aiauto-patches viaupdateInternalDependencies(dependency-range update only).Verification
pnpm -r typecheckclean (3 packages)pnpm lint(biome) clean (100 files)durableCompaction.test.ts).🤖 Generated with Claude Code