docs(coding-agent): document ctx.compact and ctx.getContextUsage

This commit is contained in:
Mario Zechner 2026-01-17 11:51:39 +01:00
parent 05ee8e3334
commit 57fe00ced4

View file

@ -384,7 +384,7 @@ pi.on("session_compact", async (event, ctx) => {
});
```
**Examples:** [custom-compaction.ts](../examples/extensions/custom-compaction.ts)
**Examples:** [custom-compaction.ts](../examples/extensions/custom-compaction.ts), [trigger-compact.ts](../examples/extensions/trigger-compact.ts)
#### session_before_tree / session_tree
@ -677,6 +677,33 @@ pi.on("tool_call", (event, ctx) => {
});
```
### ctx.getContextUsage()
Returns current context usage for the active model. Uses last assistant usage when available, then estimates tokens for trailing messages.
```typescript
const usage = ctx.getContextUsage();
if (usage && usage.tokens > 100_000) {
// ...
}
```
### ctx.compact()
Trigger compaction without awaiting completion. Use `onComplete` and `onError` for follow-up actions.
```typescript
ctx.compact({
customInstructions: "Focus on recent changes",
onComplete: (result) => {
ctx.ui.notify("Compaction completed", "info");
},
onError: (error) => {
ctx.ui.notify(`Compaction failed: ${error.message}`, "error");
},
});
```
## ExtensionCommandContext
Command handlers receive `ExtensionCommandContext`, which extends `ExtensionContext` with session control methods. These are only available in commands because they can deadlock if called from event handlers.