Commit ae6079fb9c27

Vincent Demeester <vincent@sbr.pm>
2026-02-05 22:38:09
fix(ai-storage): use correct session API for shutdown handler
Changed from ctx.messages to ctx.sessionManager.getEntries() which is the correct pi API. Also handle content blocks properly and add notifications for debugging feedback on shutdown. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b1c1076
Changed files (1)
dots
pi
agent
extensions
ai-storage
dots/pi/agent/extensions/ai-storage/index.ts
@@ -448,15 +448,22 @@ export default function (pi: ExtensionAPI) {
 	});
 
 	// Save transcript on session shutdown if not already saved
-	pi.on("session_shutdown", async (event, ctx) => {
+	pi.on("session_shutdown", async (_event, ctx) => {
 		try {
 			// Skip if session was already saved manually
 			if (currentSessionFile) {
+				if (ctx.hasUI) {
+					ctx.ui.notify("Session already saved, skipping auto-save", "info");
+				}
 				return;
 			}
 
-			// Get conversation messages from context
-			const messages = ctx.messages || [];
+			// Get conversation entries from session manager (correct API)
+			const entries = ctx.sessionManager?.getEntries() || [];
+			const messages = entries
+				.filter((e: any) => e.type === "message")
+				.map((e: any) => e.message);
+
 			if (messages.length < 2) {
 				// Not enough content to save (just greeting or empty)
 				return;
@@ -469,10 +476,20 @@ export default function (pi: ExtensionAPI) {
 				host: hostname(),
 				tool: detectTool(),
 				messageCount: messages.length,
-				messages: messages.map((m: any) => ({
-					role: m.role,
-					content: typeof m.content === "string" ? m.content : JSON.stringify(m.content),
-				})),
+				messages: messages.map((m: any) => {
+					let content = m.content;
+					if (Array.isArray(content)) {
+						// Extract text from content blocks
+						content = content
+							.filter((c: any) => c.type === "text")
+							.map((c: any) => c.text)
+							.join("\n");
+					}
+					return {
+						role: m.role,
+						content: typeof content === "string" ? content : JSON.stringify(content),
+					};
+				}),
 			};
 
 			await mkdir(PENDING_DIR, { recursive: true });
@@ -482,8 +499,16 @@ export default function (pi: ExtensionAPI) {
 
 			// Log that we saved a pending transcript
 			await appendToSessionLog(`Pending transcript saved: ${filename}`);
+
+			// Notify user (if UI available)
+			if (ctx.hasUI) {
+				ctx.ui.notify(`๐Ÿ“ Session transcript saved for recovery`, "info");
+			}
 		} catch (error) {
-			// Silent failure - don't interrupt shutdown
+			// Log error but don't interrupt shutdown
+			if (ctx.hasUI) {
+				ctx.ui.notify(`Auto-save failed: ${error}`, "error");
+			}
 		}
 	});