bookmark.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * Entry bookmarking example.
  3. *
  4. * Shows setLabel to mark entries with labels for easy navigation in /tree.
  5. * Labels appear in the tree view and help you find important points.
  6. *
  7. * Usage: /bookmark [label] - bookmark the last assistant message
  8. */
  9. import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
  10. export default function (pi: ExtensionAPI) {
  11. pi.registerCommand("bookmark", {
  12. description: "Bookmark last message (usage: /bookmark [label])",
  13. handler: async (args, ctx) => {
  14. const label = args.trim() || `bookmark-${Date.now()}`;
  15. // Find the last assistant message entry
  16. const entries = ctx.sessionManager.getEntries();
  17. for (let i = entries.length - 1; i >= 0; i--) {
  18. const entry = entries[i];
  19. if (entry.type === "message" && entry.message.role === "assistant") {
  20. pi.setLabel(entry.id, label);
  21. ctx.ui.notify(`Bookmarked as: ${label}`, "info");
  22. return;
  23. }
  24. }
  25. ctx.ui.notify("No assistant message to bookmark", "warning");
  26. },
  27. });
  28. // Remove bookmark
  29. pi.registerCommand("unbookmark", {
  30. description: "Remove bookmark from last labeled entry",
  31. handler: async (_args, ctx) => {
  32. const entries = ctx.sessionManager.getEntries();
  33. for (let i = entries.length - 1; i >= 0; i--) {
  34. const entry = entries[i];
  35. const label = ctx.sessionManager.getLabel(entry.id);
  36. if (label) {
  37. pi.setLabel(entry.id, undefined);
  38. ctx.ui.notify(`Removed bookmark: ${label}`, "info");
  39. return;
  40. }
  41. }
  42. ctx.ui.notify("No bookmarked entry found", "warning");
  43. },
  44. });
  45. }