utils.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * Pure utility functions for plan mode.
  3. * Extracted for testability.
  4. */
  5. // Destructive commands blocked in plan mode
  6. const DESTRUCTIVE_PATTERNS = [
  7. /\brm\b/i,
  8. /\brmdir\b/i,
  9. /\bmv\b/i,
  10. /\bcp\b/i,
  11. /\bmkdir\b/i,
  12. /\btouch\b/i,
  13. /\bchmod\b/i,
  14. /\bchown\b/i,
  15. /\bchgrp\b/i,
  16. /\bln\b/i,
  17. /\btee\b/i,
  18. /\btruncate\b/i,
  19. /\bdd\b/i,
  20. /\bshred\b/i,
  21. /(^|[^<])>(?!>)/,
  22. />>/,
  23. /\bnpm\s+(install|uninstall|update|ci|link|publish)/i,
  24. /\byarn\s+(add|remove|install|publish)/i,
  25. /\bpnpm\s+(add|remove|install|publish)/i,
  26. /\bpip\s+(install|uninstall)/i,
  27. /\bapt(-get)?\s+(install|remove|purge|update|upgrade)/i,
  28. /\bbrew\s+(install|uninstall|upgrade)/i,
  29. /\bgit\s+(add|commit|push|pull|merge|rebase|reset|checkout|branch\s+-[dD]|stash|cherry-pick|revert|tag|init|clone)/i,
  30. /\bsudo\b/i,
  31. /\bsu\b/i,
  32. /\bkill\b/i,
  33. /\bpkill\b/i,
  34. /\bkillall\b/i,
  35. /\breboot\b/i,
  36. /\bshutdown\b/i,
  37. /\bsystemctl\s+(start|stop|restart|enable|disable)/i,
  38. /\bservice\s+\S+\s+(start|stop|restart)/i,
  39. /\b(vim?|nano|emacs|code|subl)\b/i,
  40. ];
  41. // Safe read-only commands allowed in plan mode
  42. const SAFE_PATTERNS = [
  43. /^\s*cat\b/,
  44. /^\s*head\b/,
  45. /^\s*tail\b/,
  46. /^\s*less\b/,
  47. /^\s*more\b/,
  48. /^\s*grep\b/,
  49. /^\s*find\b/,
  50. /^\s*ls\b/,
  51. /^\s*pwd\b/,
  52. /^\s*echo\b/,
  53. /^\s*printf\b/,
  54. /^\s*wc\b/,
  55. /^\s*sort\b/,
  56. /^\s*uniq\b/,
  57. /^\s*diff\b/,
  58. /^\s*file\b/,
  59. /^\s*stat\b/,
  60. /^\s*du\b/,
  61. /^\s*df\b/,
  62. /^\s*tree\b/,
  63. /^\s*which\b/,
  64. /^\s*whereis\b/,
  65. /^\s*type\b/,
  66. /^\s*env\b/,
  67. /^\s*printenv\b/,
  68. /^\s*uname\b/,
  69. /^\s*whoami\b/,
  70. /^\s*id\b/,
  71. /^\s*date\b/,
  72. /^\s*cal\b/,
  73. /^\s*uptime\b/,
  74. /^\s*ps\b/,
  75. /^\s*top\b/,
  76. /^\s*htop\b/,
  77. /^\s*free\b/,
  78. /^\s*git\s+(status|log|diff|show|branch|remote|config\s+--get)/i,
  79. /^\s*git\s+ls-/i,
  80. /^\s*npm\s+(list|ls|view|info|search|outdated|audit)/i,
  81. /^\s*yarn\s+(list|info|why|audit)/i,
  82. /^\s*node\s+--version/i,
  83. /^\s*python\s+--version/i,
  84. /^\s*curl\s/i,
  85. /^\s*wget\s+-O\s*-/i,
  86. /^\s*jq\b/,
  87. /^\s*sed\s+-n/i,
  88. /^\s*awk\b/,
  89. /^\s*rg\b/,
  90. /^\s*fd\b/,
  91. /^\s*bat\b/,
  92. /^\s*eza\b/,
  93. ];
  94. export function isSafeCommand(command: string): boolean {
  95. const isDestructive = DESTRUCTIVE_PATTERNS.some((p) => p.test(command));
  96. const isSafe = SAFE_PATTERNS.some((p) => p.test(command));
  97. return !isDestructive && isSafe;
  98. }
  99. export interface TodoItem {
  100. step: number;
  101. text: string;
  102. completed: boolean;
  103. }
  104. export function cleanStepText(text: string): string {
  105. let cleaned = text
  106. .replace(/\*{1,2}([^*]+)\*{1,2}/g, "$1") // Remove bold/italic
  107. .replace(/`([^`]+)`/g, "$1") // Remove code
  108. .replace(
  109. /^(Use|Run|Execute|Create|Write|Read|Check|Verify|Update|Modify|Add|Remove|Delete|Install)\s+(the\s+)?/i,
  110. "",
  111. )
  112. .replace(/\s+/g, " ")
  113. .trim();
  114. if (cleaned.length > 0) {
  115. cleaned = cleaned.charAt(0).toUpperCase() + cleaned.slice(1);
  116. }
  117. if (cleaned.length > 50) {
  118. cleaned = `${cleaned.slice(0, 47)}...`;
  119. }
  120. return cleaned;
  121. }
  122. export function extractTodoItems(message: string): TodoItem[] {
  123. const items: TodoItem[] = [];
  124. const headerMatch = message.match(/\*{0,2}Plan:\*{0,2}\s*\n/i);
  125. if (!headerMatch) return items;
  126. const planSection = message.slice(message.indexOf(headerMatch[0]) + headerMatch[0].length);
  127. const numberedPattern = /^\s*(\d+)[.)]\s+\*{0,2}([^*\n]+)/gm;
  128. for (const match of planSection.matchAll(numberedPattern)) {
  129. const text = match[2]
  130. .trim()
  131. .replace(/\*{1,2}$/, "")
  132. .trim();
  133. if (text.length > 5 && !text.startsWith("`") && !text.startsWith("/") && !text.startsWith("-")) {
  134. const cleaned = cleanStepText(text);
  135. if (cleaned.length > 3) {
  136. items.push({ step: items.length + 1, text: cleaned, completed: false });
  137. }
  138. }
  139. }
  140. return items;
  141. }
  142. export function extractDoneSteps(message: string): number[] {
  143. const steps: number[] = [];
  144. for (const match of message.matchAll(/\[DONE:(\d+)\]/gi)) {
  145. const step = Number(match[1]);
  146. if (Number.isFinite(step)) steps.push(step);
  147. }
  148. return steps;
  149. }
  150. export function markCompletedSteps(text: string, items: TodoItem[]): number {
  151. const doneSteps = extractDoneSteps(text);
  152. for (const step of doneSteps) {
  153. const item = items.find((t) => t.step === step);
  154. if (item) item.completed = true;
  155. }
  156. return doneSteps.length;
  157. }