working-indicator.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Working Indicator Extension
  3. *
  4. * Demonstrates `ctx.ui.setWorkingIndicator()` for customizing the inline
  5. * working indicator shown while pi is streaming a response.
  6. *
  7. * Usage:
  8. * pi --extension examples/extensions/working-indicator.ts
  9. *
  10. * Commands:
  11. * /working-indicator Show current mode
  12. * /working-indicator dot Use a static dot indicator
  13. * /working-indicator pulse Use a custom animated indicator
  14. * /working-indicator none Hide the indicator entirely
  15. * /working-indicator spinner Restore an animated spinner
  16. * /working-indicator reset Restore pi's default spinner
  17. */
  18. import type { ExtensionAPI, ExtensionContext, WorkingIndicatorOptions } from "@earendil-works/pi-coding-agent";
  19. type WorkingIndicatorMode = "dot" | "none" | "pulse" | "spinner" | "default";
  20. const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
  21. const PASTEL_RAINBOW = [
  22. "\x1b[38;2;255;179;186m",
  23. "\x1b[38;2;255;223;186m",
  24. "\x1b[38;2;255;255;186m",
  25. "\x1b[38;2;186;255;201m",
  26. "\x1b[38;2;186;225;255m",
  27. "\x1b[38;2;218;186;255m",
  28. ];
  29. const RESET_FG = "\x1b[39m";
  30. const HIDDEN_INDICATOR: WorkingIndicatorOptions = {
  31. frames: [],
  32. };
  33. function colorize(text: string, color: string): string {
  34. return `${color}${text}${RESET_FG}`;
  35. }
  36. function getIndicator(mode: WorkingIndicatorMode): WorkingIndicatorOptions | undefined {
  37. switch (mode) {
  38. case "dot":
  39. return {
  40. frames: [colorize("●", PASTEL_RAINBOW[0])],
  41. };
  42. case "none":
  43. return HIDDEN_INDICATOR;
  44. case "pulse":
  45. return {
  46. frames: [
  47. colorize("·", PASTEL_RAINBOW[0]),
  48. colorize("•", PASTEL_RAINBOW[2]),
  49. colorize("●", PASTEL_RAINBOW[4]),
  50. colorize("•", PASTEL_RAINBOW[5]),
  51. ],
  52. intervalMs: 120,
  53. };
  54. case "spinner":
  55. return {
  56. frames: SPINNER_FRAMES.map((frame, index) =>
  57. colorize(frame, PASTEL_RAINBOW[index % PASTEL_RAINBOW.length]!),
  58. ),
  59. intervalMs: 80,
  60. };
  61. case "default":
  62. return undefined;
  63. }
  64. }
  65. function describeMode(mode: WorkingIndicatorMode): string {
  66. switch (mode) {
  67. case "dot":
  68. return "static dot";
  69. case "none":
  70. return "hidden";
  71. case "pulse":
  72. return "custom pulse";
  73. case "spinner":
  74. return "custom spinner";
  75. case "default":
  76. return "pi default spinner";
  77. }
  78. }
  79. export default function (pi: ExtensionAPI) {
  80. let mode: WorkingIndicatorMode = "spinner";
  81. const applyIndicator = (ctx: ExtensionContext) => {
  82. ctx.ui.setWorkingIndicator(getIndicator(mode));
  83. ctx.ui.setStatus("working-indicator", ctx.ui.theme.fg("dim", `Indicator: ${describeMode(mode)}`));
  84. };
  85. pi.on("session_start", async (_event, ctx) => {
  86. applyIndicator(ctx);
  87. });
  88. pi.registerCommand("working-indicator", {
  89. description: "Set the streaming working indicator: dot, pulse, none, spinner, or reset.",
  90. handler: async (args, ctx) => {
  91. const nextMode = args.trim().toLowerCase();
  92. if (!nextMode) {
  93. ctx.ui.notify(`Working indicator: ${describeMode(mode)}`, "info");
  94. return;
  95. }
  96. if (
  97. nextMode !== "dot" &&
  98. nextMode !== "none" &&
  99. nextMode !== "pulse" &&
  100. nextMode !== "spinner" &&
  101. nextMode !== "reset"
  102. ) {
  103. ctx.ui.notify("Usage: /working-indicator [dot|pulse|none|spinner|reset]", "error");
  104. return;
  105. }
  106. mode = nextMode === "reset" ? "default" : nextMode;
  107. applyIndicator(ctx);
  108. ctx.ui.notify(`Working indicator set to: ${describeMode(mode)}`, "info");
  109. },
  110. });
  111. }