Skip to content

refactor: move durable compaction engine into core#39

Merged
MyPrototypeWhat merged 3 commits into
mainfrom
m13t/funny-galileo-b194e3
Jun 16, 2026
Merged

refactor: move durable compaction engine into core#39
MyPrototypeWhat merged 3 commits into
mainfrom
m13t/funny-galileo-b194e3

Conversation

@MyPrototypeWhat

Copy link
Copy Markdown
Owner

What

Moves the durable-compaction engine — planCompaction / compactHistory — out of @context-chef/ai-sdk-middleware and 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's summarizeMessages was already a thin fromAISDK → core → toAISDK shell. 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/core gains planCompaction(history, options) and compactHistory(history, compress, options) over Message[].
    • compactHistory takes an injected compress callback (NOT a model), same pattern as summarizeHistory, so core stays provider-agnostic — no ai / @ai-sdk/provider dependency.
    • Returns the input history reference unchanged on a no-op (nothing old enough, or a blank summary), so callers can skip persistence via result === history.
    • Input contract (documented): a flat Message[] with system inline; direct adapter users (fromAnthropic/fromOpenAI/fromGemini) reassemble [...system, ...history].
  • @context-chef/ai-sdk-middleware planCompaction / compactHistory become thin AI-SDK wrappers (fromAISDK → core → toAISDK), binding model via createCompressionAdapter. Signatures and behavior unchanged, including the no-op reference guarantee (result === ir ? prompt : toAISDK(result)). createCompressionAdapter is exported module-level (not in the package public index).
  • generateText stays 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-ai auto-patches via updateInternalDependencies (dependency-range update only).

Verification

  • pnpm -r typecheck clean (3 packages)
  • Tests: core 714, ai-sdk-middleware 91, tanstack-ai 66 — all pass
  • pnpm lint (biome) clean (100 files)
  • Behavior parity proven by keeping the original middleware test suite green through the refactor; then slimmed (algorithm assertions migrated to core's new durableCompaction.test.ts).
  • Reviewed adversarially: no behavior defects found (no-op reference parity, byte-identical AI-SDK output, no AI-SDK leak into core, no import cycle, semver all confirmed). Doc-precision and edge-case test gaps surfaced by the review were fixed in this branch.

🤖 Generated with Claude Code

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.
@MyPrototypeWhat MyPrototypeWhat merged commit 2e83a55 into main Jun 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant