Simplify compaction: remove proactive abort, use Agent.continue() for retry

- Add agentLoopContinue() to pi-ai for resuming from existing context
- Add Agent.continue() method and transport.continue() interface
- Simplify AgentSession compaction to two cases: overflow (auto-retry) and threshold (no retry)
- Remove proactive mid-turn compaction abort
- Merge turn prefix summary into main summary
- Add isCompacting property to AgentSession and RPC state
- Block input during compaction in interactive mode
- Show compaction count on session resume
- Rename RPC.md to rpc.md for consistency

Related to #128
This commit is contained in:
Mario Zechner 2025-12-09 21:43:49 +01:00
parent d67c69c6e9
commit 5a9d844f9a
27 changed files with 1261 additions and 1011 deletions

View file

@ -898,6 +898,34 @@ const messages = await stream.result();
context.messages.push(...messages);
```
### Continuing from Existing Context
Use `agentLoopContinue` to resume an agent loop without adding a new user message. This is useful for:
- Retrying after context overflow (after compaction reduces context size)
- Resuming from tool results that were added manually to the context
```typescript
import { agentLoopContinue, AgentContext } from '@mariozechner/pi-ai';
// Context already has messages - last must be 'user' or 'toolResult'
const context: AgentContext = {
systemPrompt: 'You are helpful.',
messages: [userMessage, assistantMessage, toolResult],
tools: [myTool]
};
// Continue processing from the tool result
const stream = agentLoopContinue(context, { model });
for await (const event of stream) {
// Same events as agentLoop, but no user message events emitted
}
const newMessages = await stream.result();
```
**Validation**: Throws if context has no messages or if the last message is an assistant message.
### Defining Tools with TypeBox
Tools use TypeBox schemas for runtime validation and type inference: