import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent"; function findLastUserMessageId(ctx: ExtensionContext): string | undefined { for (const entry of ctx.sessionManager.getBranch().reverse()) { if (entry.type === "message" && entry.message.role === "user") { return entry.id; } } return undefined; } export default function undoExtension(pi: ExtensionAPI) { pi.registerCommand("undo", { description: "Revert your last message", handler: async (_args, ctx) => { const entryId = findLastUserMessageId(ctx); if (!entryId) { ctx.ui.notify("Nothing to undo", "warning"); return; } if (entryId === ctx.sessionManager.getLeafId()) { ctx.ui.notify("Your last message has no reply yet, nothing to undo", "warning"); return; } await ctx.navigateTree(entryId); }, }); }