|
| 1 | +import { normalizeCommandBody, type CommandNormalizeOptions } from "../commands-registry.js"; |
| 2 | + |
| 3 | +const ABORT_TRIGGERS = new Set([ |
| 4 | + "stop", |
| 5 | + "esc", |
| 6 | + "abort", |
| 7 | + "wait", |
| 8 | + "exit", |
| 9 | + "interrupt", |
| 10 | + "detente", |
| 11 | + "deten", |
| 12 | + "detén", |
| 13 | + "arrete", |
| 14 | + "arrête", |
| 15 | + "停止", |
| 16 | + "やめて", |
| 17 | + "止めて", |
| 18 | + "रुको", |
| 19 | + "توقف", |
| 20 | + "стоп", |
| 21 | + "остановись", |
| 22 | + "останови", |
| 23 | + "остановить", |
| 24 | + "прекрати", |
| 25 | + "halt", |
| 26 | + "anhalten", |
| 27 | + "aufhören", |
| 28 | + "hoer auf", |
| 29 | + "stopp", |
| 30 | + "pare", |
| 31 | + "stop openclaw", |
| 32 | + "openclaw stop", |
| 33 | + "stop action", |
| 34 | + "stop current action", |
| 35 | + "stop run", |
| 36 | + "stop current run", |
| 37 | + "stop agent", |
| 38 | + "stop the agent", |
| 39 | + "stop don't do anything", |
| 40 | + "stop dont do anything", |
| 41 | + "stop do not do anything", |
| 42 | + "stop doing anything", |
| 43 | + "do not do that", |
| 44 | + "please stop", |
| 45 | + "stop please", |
| 46 | +]); |
| 47 | +const ABORT_MEMORY = new Map<string, boolean>(); |
| 48 | +const ABORT_MEMORY_MAX = 2000; |
| 49 | +const TRAILING_ABORT_PUNCTUATION_RE = /[.!?…,,。;;::'"’”)\]}]+$/u; |
| 50 | + |
| 51 | +function normalizeAbortTriggerText(text: string): string { |
| 52 | + return text |
| 53 | + .trim() |
| 54 | + .toLowerCase() |
| 55 | + .replace(/[’`]/g, "'") |
| 56 | + .replace(/\s+/g, " ") |
| 57 | + .replace(TRAILING_ABORT_PUNCTUATION_RE, "") |
| 58 | + .trim(); |
| 59 | +} |
| 60 | + |
| 61 | +export function isAbortTrigger(text?: string): boolean { |
| 62 | + if (!text) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + const normalized = normalizeAbortTriggerText(text); |
| 66 | + return ABORT_TRIGGERS.has(normalized); |
| 67 | +} |
| 68 | + |
| 69 | +export function isAbortRequestText(text?: string, options?: CommandNormalizeOptions): boolean { |
| 70 | + if (!text) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + const normalized = normalizeCommandBody(text, options).trim(); |
| 74 | + if (!normalized) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + const normalizedLower = normalized.toLowerCase(); |
| 78 | + return ( |
| 79 | + normalizedLower === "/stop" || |
| 80 | + normalizeAbortTriggerText(normalizedLower) === "/stop" || |
| 81 | + isAbortTrigger(normalizedLower) |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +export function getAbortMemory(key: string): boolean | undefined { |
| 86 | + const normalized = key.trim(); |
| 87 | + if (!normalized) { |
| 88 | + return undefined; |
| 89 | + } |
| 90 | + return ABORT_MEMORY.get(normalized); |
| 91 | +} |
| 92 | + |
| 93 | +function pruneAbortMemory(): void { |
| 94 | + if (ABORT_MEMORY.size <= ABORT_MEMORY_MAX) { |
| 95 | + return; |
| 96 | + } |
| 97 | + const excess = ABORT_MEMORY.size - ABORT_MEMORY_MAX; |
| 98 | + let removed = 0; |
| 99 | + for (const entryKey of ABORT_MEMORY.keys()) { |
| 100 | + ABORT_MEMORY.delete(entryKey); |
| 101 | + removed += 1; |
| 102 | + if (removed >= excess) { |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +export function setAbortMemory(key: string, value: boolean): void { |
| 109 | + const normalized = key.trim(); |
| 110 | + if (!normalized) { |
| 111 | + return; |
| 112 | + } |
| 113 | + if (!value) { |
| 114 | + ABORT_MEMORY.delete(normalized); |
| 115 | + return; |
| 116 | + } |
| 117 | + if (ABORT_MEMORY.has(normalized)) { |
| 118 | + ABORT_MEMORY.delete(normalized); |
| 119 | + } |
| 120 | + ABORT_MEMORY.set(normalized, true); |
| 121 | + pruneAbortMemory(); |
| 122 | +} |
| 123 | + |
| 124 | +export function getAbortMemorySizeForTest(): number { |
| 125 | + return ABORT_MEMORY.size; |
| 126 | +} |
| 127 | + |
| 128 | +export function resetAbortMemoryForTest(): void { |
| 129 | + ABORT_MEMORY.clear(); |
| 130 | +} |
0 commit comments