1pub const EXIT_DIRECT_COMMANDS: &[&str] = &[
15 "implement",
16 "yes",
17 "go",
18 "start",
19 "approve",
20 "approved",
21 "implement now",
22 "start implementing",
23 "start implementation",
24 "execute plan",
25 "execute the plan",
26 "execute this plan",
27 "switch to agent mode",
28 "exit planning workflow",
29 "exit planning workflow and implement",
30];
31
32pub const APPROVAL_WORDS: &[&str] = &["approve", "approved", "lgtm", "accepted", "accept"];
34
35pub const APPROVAL_PHRASES: &[&str] = &[
37 "approve the plan",
38 "approve this plan",
39 "approve the proposed plan",
40 "looks good",
41 "ship it",
42 "accept the plan",
43 "accept this plan",
44];
45
46pub const EXIT_TRIGGER_PHRASES: &[&str] = &[
48 "start implement",
49 "start implementation",
50 "start implementing",
51 "implement now",
52 "implement the plan",
53 "implement this plan",
54 "begin implement",
55 "begin implementation",
56 "begin coding",
57 "proceed to implement",
58 "proceed with implementation",
59 "proceed to coding",
60 "proceed with coding",
61 "execute the plan",
62 "execute this plan",
63 "let s implement",
64 "lets implement",
65 "go ahead and implement",
66 "go ahead and code",
67 "ready to implement",
68 "start coding",
69 "start building",
70 "switch to agent mode",
71 "exit planning workflow",
72 "exit planning workflow and implement",
73];
74
75pub const STAY_PHRASES: &[&str] = &[
81 "stay in planning workflow",
82 "keep in planning workflow",
83 "continue planning",
84 "keep planning",
85 "do not implement",
86 "don t implement",
87 "not ready to implement",
88 "don t exit planning workflow",
89 "do not exit planning workflow",
90 "edit the plan",
91 "edit plan",
92 "revise the plan",
93 "revise plan",
94];
95
96pub const ENTER_PHRASES: &[&str] = &[
99 "make a plan",
100 "create a plan",
101 "write a plan",
102 "come up with a plan",
103 "plan this",
104 "use plan mode",
105 "use planning mode",
106 "enter plan mode",
107 "switch to plan mode",
108 "stay in planning workflow",
109 "keep planning",
110 "continue planning",
111 "before you implement make a plan",
112 "before implementing make a plan",
113 "outline the implementation plan",
114];
115
116pub const IMPLEMENTATION_CUES: &[&str] = &[
118 "implement this plan",
119 "implement the plan",
120 "ready to implement",
121 "exit planning workflow",
122 "execute the plan",
123 "switch out of planning workflow",
124 "start implementation",
125 "start implementing",
126 "start coding",
127];
128
129pub const EXECUTION_MODE_ALIASES: &[&str] = &[
134 "implement",
135 "continue",
136 "go",
137 "start",
138 "yes",
139 "approve",
140 "approved",
141 "lgtm",
142 "accepted",
143 "accept",
144 "ship it",
145 "implement now",
146 "start implementing",
147 "start implementation",
148 "execute plan",
149 "execute the plan",
150 "execute this plan",
151 "switch to agent mode",
152 "exit planning workflow and implement",
153];
154
155pub const FLAG_CLEARING_ONLY_ALIASES: &[&str] = &["exit planning workflow"];
159
160pub fn normalize_plan_intent(text: &str) -> String {
163 text.chars()
164 .map(|c| {
165 if c.is_alphanumeric() {
166 c.to_ascii_lowercase()
167 } else {
168 ' '
169 }
170 })
171 .collect()
172}
173
174fn is_plan_intent_negation(word: &str) -> bool {
175 matches!(word, "not" | "never" | "no" | "avoid" | "without" | "t")
176}
177
178fn has_unnegated_phrase(normalized: &str, phrase: &str, whole_word: bool) -> bool {
179 normalized.match_indices(phrase).any(|(index, _)| {
180 let Some(prefix) = normalized.get(..index) else {
181 return false;
182 };
183 let Some(suffix) = normalized.get(index..).and_then(|rest| rest.strip_prefix(phrase)) else {
184 return false;
185 };
186 if whole_word
187 && (prefix.chars().next_back().is_some_and(|character| !character.is_whitespace())
188 || suffix.chars().next().is_some_and(|character| !character.is_whitespace()))
189 {
190 return false;
191 }
192 !prefix
193 .split_whitespace()
194 .rev()
195 .take_while(|word| !matches!(*word, "but" | "however" | "instead" | "then"))
196 .any(is_plan_intent_negation)
197 })
198}
199
200pub fn matches_exit_intent(normalized: &str) -> bool {
207 let trimmed = normalized.trim();
208 EXIT_DIRECT_COMMANDS.contains(&trimmed)
209 || APPROVAL_WORDS.iter().any(|word| has_unnegated_phrase(normalized, word, true))
210 || APPROVAL_PHRASES
211 .iter()
212 .any(|phrase| has_unnegated_phrase(normalized, phrase, false))
213 || EXIT_TRIGGER_PHRASES
214 .iter()
215 .any(|phrase| has_unnegated_phrase(normalized, phrase, false))
216}
217
218pub fn matches_stay_intent(normalized: &str) -> bool {
220 STAY_PHRASES.iter().any(|phrase| normalized.contains(phrase))
221}
222
223pub fn matches_enter_intent(normalized: &str) -> bool {
226 ENTER_PHRASES
227 .iter()
228 .any(|phrase| has_unnegated_phrase(normalized, phrase, false))
229}
230
231pub fn contains_implementation_cue(normalized: &str) -> bool {
233 IMPLEMENTATION_CUES.iter().any(|cue| normalized.contains(cue))
234}
235
236pub fn is_execution_mode_alias(input: &str) -> bool {
239 let normalized = input.trim().to_ascii_lowercase();
240 EXECUTION_MODE_ALIASES
241 .iter()
242 .chain(FLAG_CLEARING_ONLY_ALIASES.iter())
243 .any(|alias| *alias == normalized)
244}
245
246pub fn is_implementation_alias(input: &str) -> bool {
249 let normalized = input.trim().to_ascii_lowercase();
250 EXECUTION_MODE_ALIASES.iter().any(|alias| *alias == normalized)
251}
252
253#[cfg(test)]
254mod tests {
255 use super::*;
256
257 fn is_exit(text: &str) -> bool {
258 matches_exit_intent(&normalize_plan_intent(text))
259 }
260
261 fn is_enter(text: &str) -> bool {
262 matches_enter_intent(&normalize_plan_intent(text))
263 }
264
265 #[test]
266 fn normalization_splits_contractions_for_negation_matching() {
267 assert_eq!(normalize_plan_intent("Don't!"), "don t ");
269 }
270
271 #[test]
272 fn exit_intent_matches_unnegated_approvals_and_triggers() {
273 for text in [
274 "approve",
275 "looks good to me",
276 "lgtm",
277 "please proceed to implement now",
278 "do not cancel but approve",
279 ] {
280 assert!(is_exit(text), "{text}");
281 }
282 }
283
284 #[test]
285 fn exit_intent_rejects_negated_approvals_and_lookalikes() {
286 for text in [
287 "disapprove",
288 "do not approve yet",
289 "don't approve",
290 "never approve without asking",
291 "no changes",
292 ] {
293 assert!(!is_exit(text), "{text}");
294 }
295 }
296
297 #[test]
298 fn enter_intent_matches_plan_mode_requests() {
299 for text in [
300 "use plan mode, refine readme",
301 "please make a plan first",
302 "switch to plan mode",
303 ] {
304 assert!(is_enter(text), "{text}");
305 }
306 }
307
308 #[test]
309 fn enter_intent_rejects_negated_requests() {
310 for text in ["don't use plan mode", "do not make a plan, just implement it"] {
311 assert!(!is_enter(text), "{text}");
312 }
313 }
314}