Add agent state methods to CustomToolContext and fix abort signature

CustomToolContext now has:
- isIdle() - check if agent is streaming
- hasQueuedMessages() - check if user has queued messages
- abort() - abort current operation (fire-and-forget)

Changed abort() signature from Promise<void> to void in both
HookContext and CustomToolContext. The abort is fire-and-forget:
it calls session.abort() without awaiting, so the abort signal
is set immediately while waitForIdle() runs in the background.

Fixes #388
This commit is contained in:
Mario Zechner 2026-01-02 00:31:23 +01:00
parent 0d9fddec1e
commit 03159d2f4b
10 changed files with 68 additions and 9 deletions

View file

@ -230,11 +230,33 @@ interface CustomToolContext {
sessionManager: ReadonlySessionManager; // Read-only access to session
modelRegistry: ModelRegistry; // For API key resolution
model: Model | undefined; // Current model (may be undefined)
isIdle(): boolean; // Whether agent is streaming
hasQueuedMessages(): boolean; // Whether user has queued messages
abort(): void; // Abort current operation (fire-and-forget)
}
```
Use `ctx.sessionManager.getBranch()` to get entries on the current branch for state reconstruction.
### Checking Queue State
Interactive tools can skip prompts when the user has already queued a message:
```typescript
async execute(toolCallId, params, onUpdate, ctx, signal) {
// If user already queued a message, skip the interactive prompt
if (ctx.hasQueuedMessages()) {
return {
content: [{ type: "text", text: "Skipped - user has queued input" }],
};
}
// Otherwise, prompt for input
const answer = await pi.ui.input("What would you like to do?");
// ...
}
```
## Session Lifecycle
Tools can implement `onSession` to react to session changes: