@aparte/plugin-compaction

中文文档
0.32.1发布于 2026/11/02MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 5136

Conversation compaction for aparté — summarise the turns that no longer fit the model's window, keep the recent ones verbatim, and answer the context gauge's aparte-compact.

原始使用文档

@aparte/plugin-compaction

Conversation compaction for aparté: when a conversation outgrows the model's window, summarise the older turns for the model and keep the recent ones verbatim — without deleting anything. Nothing in core compacts by itself — no UI kit does, and every agent SDK ships it as an opt-in module — so this is where it lives.

npm install @aparte/plugin-compaction @aparte/core
import { setupCompaction } from '@aparte/plugin-compaction';

const compaction = setupCompaction();   // the global config, the current model's window

That is the whole wiring. The plugin registers itself as the config's compactor and decides on its own: after a turn that leaves the conversation past 80 % of the window (threshold), and before a send whose request would not fit (the turn waits, Stop cancels both), and when the model refuses a request as over its window (it compacts and sends the turn once more).

A compaction aims below the threshold, not at it: the history the model reads afterwards lands at about threshold − 0.2 of the window, less the fixed costs (system prompt, tools, the room for the reply, the summary's slot), so one summary buys several turns instead of being asked for again after the next. The selection is made in the model's own tokens (the character heuristic's undercount is calibrated from the last reply, as the context gauge does) and leaves room for the turn being prepared, so a long paste is made room for. A selector of your own is called with the messages alone and is left as it is. Each chat compacts on its own: a summary being written for one does not hold up another. A Restore cancels a summary still being written for that chat (with listen: false, which hears no window event, the summary is refused when it comes back instead), and Stop cancels it in every case.

A compaction writes a checkpoint, it deletes nothing. The summary goes on the first turn it keeps — contextSummary: { summary, count, createdAt }, saved with the conversation — and the history the model receives starts there, the summary first. Every message stays on screen and in storage; the bubble draws a separator above that turn ("12 messages summarised for the model", Show summary, Restore), and Restore sends the original messages to the model again.

await compaction.compact('main-chat');   // on demand; returns the outcome, never throws
compaction.abort();                      // the summary in flight; the conversation is untouched
compaction.running;                      // true meanwhile
compaction.dispose();                    // remove the listeners and the compactor

The summary is asked of the current model through the config's transport: streamed, with a maxTokens that keeps the answer inside the window and _meta: { compaction: true }, so a backend can route it to a cheaper model. The default prompt asks for fixed sections in the conversation's language; a second compaction updates the summary rather than summarising a summary; the first request is kept word for word. A summary cut off at the length limit, ending in an error, or no shorter than what it replaces is refused and the conversation stays as it was. With no window on the model (and no contextWindow option) nothing says what fits, so the default skips with 'no-window'.

Options — auto (default true; false is manual only: only a person's request compacts — Compact now, Retry, compact(), aparte-compact — and neither the after-turn, the before-send nor the overflow attempt is made, so a model's overflow error stands as it did before the plugin; so does listen: false), threshold (default 0.8), contextWindow (the window when the model declares none), selector (your own (messages) => { keep, drop }; createCompactionSelector builds the budget-aware one), prompt, keyResolver, summarize (replace the model call: your endpoint, a cheaper model), resolveTarget (a transcript in a store: { getMessages, updateMessage }), scopeToTargetId (one setup per chat on a multi-chat page), keepWithoutWindow, listen. The config comes last, like every setup*: setupCompaction(options, config).

Events, on window, each naming the chat and the message the checkpoint sits on: aparte-compact-start, aparte-compact-done ({ summary, kept, dropped, count }, { skipped: true, reason } — empty, nothing-to-drop, running, streaming, no-window — or { restored: true }), aparte-compact-error ({ error }).

The budget, exported for a host that wants the numbers: computeHistoryBudget, splitHistoryBudget, estimateTokens, estimateTokensJson, DEFAULT_COMPACTION_CONFIG; and transcriptForSummary / messageText / DEFAULT_COMPACTION_PROMPT for a summarize of your own that wants the same transcript.

@aparte/core is the only peer dependency. No element, no DOM at import: the same entry serves the browser and Node.

ESM-only. Part of the aparté monorepo.

版本与发布记录

@aparte/plugin-compaction 共发布 46 个版本,首次发布于 2026/08/30;过去 12 个月发布了 46 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.32.1latest2026/11/024 天前
0.32.02026/11/024 天前
0.31.02026/11/024 天前
0.30.12026/11/014 天前
0.30.02026/11/014 天前
0.29.42026/11/015 天前

其他发布通道:alpha → 0.17.0。

关于 @aparte/plugin-compaction 的常见问题

@aparte/plugin-compaction 是什么?

Conversation compaction for aparté — summarise the turns that no longer fit the model's window, keep the recent ones verbatim, and answer the context gauge's aparte-compact.

如何安装 @aparte/plugin-compaction?

在项目目录运行 npm install @aparte/plugin-compaction,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@aparte/core(>=0.32.1 <1.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aparte/plugin-compaction 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aparte/plugin-compaction 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@aparte/plugin-compaction 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aparte/plugin-compaction 可以商用吗?

@aparte/plugin-compaction 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aparte/plugin-compaction 的下载量有多少?

根据 npm 官方下载统计,@aparte/plugin-compaction 在 2026/10/06 – 2026/11/04 这 30 天内被下载 5,136 次,日均约 171 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aparte/plugin-compaction 还在维护吗?

最新版本 0.32.1 发布于 2026/11/02(4 天前);至今共发布 46 个版本,其中过去 12 个月发布了 46 个;首次发布于 2026/08/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。