pi-compact-context
v1.0.2
Published
Let the AI trigger context compaction in pi via a compact_context tool and /compact-context command
Readme
pi-compact-context
Let the AI compact your context — just ask in plain English.
pi's built-in /compact command can only be typed by you. If
you ask the model to "compact the conversation", it can't: compaction isn't a
tool it can call. This package fixes that. It gives the model a
compact_context tool, so natural-language requests like these just work:
"compact context"
"compact context after pushing the docs"
"summarize what we've done and compact the session"
"we're done with the debugging phase — compact, then start fresh on the API"
The model calls the tool itself, the current turn finishes, and pi compacts the
conversation exactly as if you'd typed /compact.
Install
pi install npm:pi-compact-contextor straight from git:
pi install git:github.com/aerialcombat/[email protected]Then restart pi (or run /reload). That's it — no configuration.
What it adds
| Interface | Name | Description |
|-----------|------|-------------|
| AI tool | compact_context | Callable by the model. Optional instructions parameter shapes the compaction summary. |
| Slash command | /compact-context [instructions] | Same behavior, typed by you. Handy alias if you don't like pi's built-in /compact spelling. |
Both queue the same compaction; results appear via a toast notification
(Compacted: N → M est. tokens).
How it works (and why it returns immediately)
Compaction in pi must not run mid-turn: AgentSession.compact() aborts the
running agent turn before it starts. If a tool called it synchronously from
inside that very turn, the run and the compaction would kill each other —
the tool deadlocks, and you get a stuck "No result provided".
So this extension is deliberately fire-and-forget:
- You say "compact context".
- The model calls
compact_context. - The tool queues the compaction and returns instantly ("Compaction queued — it will run as soon as this turn ends").
- The turn finishes normally.
- pi compacts the now-idle session and shows the token counts.
You keep one full turn of headroom for the model to wrap up whatever it was doing — like pushing the docs — before the history gets summarized.
Requirements
- pi coding agent
- Any model — compaction uses pi's normal summarization path
FAQ
Does this replace /compact?
No. /compact still works exactly as before. This adds an AI-triggerable
path on top.
What about auto-compaction? Unaffected. pi still compacts automatically near the context-window threshold; this package only adds manual, conversational control.
Can the model compact itself mid-task and keep going? Not in the same turn — compaction needs an idle agent. The model should finish its current work, then call the tool. This is a safety property of pi's compaction, not a limitation of this package.
Where does the summary come from?
pi's built-in compaction: an LLM summarizes the older history, recent messages
are kept verbatim. Pass instructions (or type
/compact-context focus on the API migration) to steer what the summary
emphasizes.
License
MIT
