Consolidate session events: remove session_before_new/session_new, add reason field to switch events

- Remove session_before_new and session_new hook events
- Add reason: 'new' | 'resume' to session_before_switch and session_switch events
- Remove 'new' reason from custom tool onSession (use 'switch' for both /new and /resume)
- Rename reset() to newSession(options?) in AgentSession
- Add NewSessionOptions with optional parentSession for lineage tracking
- Rename branchedFrom to parentSession in SessionHeader
- Rename RPC reset command to new_session with optional parentSession
- Update example hooks to use new event structure
- Update documentation and changelog

Based on discussion in #293
This commit is contained in:
Mario Zechner 2026-01-01 23:31:26 +01:00
parent 1d9fa13d58
commit 484d7e06bb
19 changed files with 117 additions and 117 deletions

View file

@ -123,13 +123,9 @@ user sends prompt ────────────────────
user sends another prompt ◄────────────────────────────────┘
/new (new session)
├─► session_before_new (can cancel)
└─► session_new
/resume (switch session)
├─► session_before_switch (can cancel)
└─► session_switch
/new (new session) or /resume (switch session)
├─► session_before_switch (can cancel, has reason: "new" | "resume")
└─► session_switch (has reason: "new" | "resume")
/branch
├─► session_before_branch (can cancel)
@ -161,34 +157,27 @@ pi.on("session_start", async (_event, ctx) => {
#### session_before_switch / session_switch
Fired when switching sessions via `/resume`.
Fired when starting a new session (`/new`) or switching sessions (`/resume`).
```typescript
pi.on("session_before_switch", async (event, ctx) => {
// event.targetSessionFile - session we're switching to
return { cancel: true }; // Cancel the switch
// event.reason - "new" (starting fresh) or "resume" (switching to existing)
// event.targetSessionFile - session we're switching to (only for "resume")
if (event.reason === "new") {
const ok = await ctx.ui.confirm("Clear?", "Delete all messages?");
if (!ok) return { cancel: true };
}
return { cancel: true }; // Cancel the switch/new
});
pi.on("session_switch", async (event, ctx) => {
// event.reason - "new" or "resume"
// event.previousSessionFile - session we came from
});
```
#### session_before_new / session_new
Fired when starting a new session via `/new`.
```typescript
pi.on("session_before_new", async (_event, ctx) => {
const ok = await ctx.ui.confirm("Clear?", "Delete all messages?");
if (!ok) return { cancel: true };
});
pi.on("session_new", async (_event, ctx) => {
// New session started
});
```
#### session_before_branch / session_branch
Fired when branching via `/branch`.