opencrabs 0.3.68

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Prompt Analysis and Transformation (shared soft-nudge)
//!
//! Analyzes natural-language user prompts to detect keywords and appends
//! explicit tool call hints for the LLM to ensure proper tool usage.
//!
//! Shared by every surface (TUI and Telegram). Hints are LLM-only: they are
//! appended to the agent input string and must never reach user-visible
//! display paths (Telegram `display_text`, TUI chat bubbles). Slash commands
//! and skill/user-command expansions are never analyzed: soft-nudge means
//! the USER said keyword-shaped language, not that a skill author wrote it.
//!
//! ## Multilanguage support
//!
//! Keywords are loaded from per-language TOML files at compile time via
//! `include_str!`. Language detection uses character-set heuristics
//! (Cyrillic → ru, ã/õ/ç → pt, ñ/¿/¡ → es, accented Latin → fr, else en).
//!
//! Multi-word phrases are scanned across all languages to catch code-switched
//! input (e.g. "Voy a usar write_file" with no ñ/¿). Single-word keywords
//! are gated to the detected language to avoid false positives.

use regex::Regex;
use serde::Deserialize;
use std::sync::LazyLock;

/// Language-specific prompt analyzer configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct AnalyzerLang {
    #[serde(default)]
    pub plan: Vec<String>,
    #[serde(default)]
    pub read_file: Vec<String>,
    #[serde(default)]
    pub search: Vec<String>,
    #[serde(default)]
    pub write_file: Vec<String>,
    #[serde(default)]
    pub edit_file: Vec<String>,
    #[serde(default)]
    pub bash: Vec<String>,
    #[serde(default)]
    pub web_search: Vec<String>,
}

/// Embedded TOML content (compile-time validated).
const EN_TOML: &str = include_str!("prompt_analyzer_lang/en.toml");
const RU_TOML: &str = include_str!("prompt_analyzer_lang/ru.toml");
const ES_TOML: &str = include_str!("prompt_analyzer_lang/es.toml");
const PT_TOML: &str = include_str!("prompt_analyzer_lang/pt.toml");
const FR_TOML: &str = include_str!("prompt_analyzer_lang/fr.toml");
const ID_TOML: &str = include_str!("prompt_analyzer_lang/id.toml");

pub(crate) static LANG_EN: LazyLock<AnalyzerLang> =
    LazyLock::new(|| toml::from_str(EN_TOML).expect("BUG: en.toml failed to parse at runtime"));
pub(crate) static LANG_RU: LazyLock<AnalyzerLang> =
    LazyLock::new(|| toml::from_str(RU_TOML).expect("BUG: ru.toml failed to parse at runtime"));
pub(crate) static LANG_ES: LazyLock<AnalyzerLang> =
    LazyLock::new(|| toml::from_str(ES_TOML).expect("BUG: es.toml failed to parse at runtime"));
pub(crate) static LANG_PT: LazyLock<AnalyzerLang> =
    LazyLock::new(|| toml::from_str(PT_TOML).expect("BUG: pt.toml failed to parse at runtime"));
pub(crate) static LANG_FR: LazyLock<AnalyzerLang> =
    LazyLock::new(|| toml::from_str(FR_TOML).expect("BUG: fr.toml failed to parse at runtime"));
pub(crate) static LANG_ID: LazyLock<AnalyzerLang> =
    LazyLock::new(|| toml::from_str(ID_TOML).expect("BUG: id.toml failed to parse at runtime"));

/// Detect language from text content using character-set heuristics.
/// Returns a static reference to the appropriate language config.
pub fn detect_language(text: &str) -> &'static AnalyzerLang {
    let mut cyrillic = 0u32;
    let mut latin_accent = 0u32;
    let mut total_alpha = 0u32;

    for ch in text.chars().take(500) {
        if ch.is_alphabetic() {
            total_alpha += 1;
            if ('\u{0400}'..='\u{04FF}').contains(&ch) {
                cyrillic += 1;
            } else if ('\u{00C0}'..='\u{024F}').contains(&ch) {
                latin_accent += 1;
            }
        }
    }

    if total_alpha == 0 {
        return &LANG_EN;
    }

    // Cyrillic > 20% of alpha chars → Russian
    if cyrillic * 5 > total_alpha {
        return &LANG_RU;
    }

    // For Latin-accent text, distinguish Spanish/Portuguese/French
    // by looking for language-specific characters
    if latin_accent > 0 {
        // Portuguese-specific: ã, õ, ç
        if text.contains('ã')
            || text.contains('õ')
            || text.contains('ç')
            || text.contains('Ã')
            || text.contains('Õ')
            || text.contains('Ç')
        {
            return &LANG_PT;
        }
        // Spanish-specific: ñ, ¿, ¡
        if text.contains('ñ') || text.contains('Ñ') || text.contains('¿') || text.contains('¡')
        {
            return &LANG_ES;
        }
        // If we have significant accented Latin but no PT/ES markers,
        // check for French patterns (à, â, ç, é, è, ê, ë, î, ï, ô, ù, û, ü, ÿ)
        // French is the fallback for accented Latin since it's the most
        // common accented-Latin language after Spanish/Portuguese
        if text.contains('à')
            || text.contains('â')
            || text.contains('é')
            || text.contains('è')
            || text.contains('ê')
            || text.contains('ë')
            || text.contains('î')
            || text.contains('ï')
            || text.contains('ô')
            || text.contains('û')
            || text.contains('ù')
            || text.contains('ü')
            || text.contains('ÿ')
        {
            return &LANG_FR;
        }
    }

    &LANG_EN
}

/// Every loaded language config, in detection-priority order.
///
/// Multi-word phrases are scanned across all languages to catch code-switched
/// input. Single-word keywords stay gated to the detected language to avoid
/// false positives.
pub fn all_langs() -> [&'static AnalyzerLang; 6] {
    [&LANG_EN, &LANG_RU, &LANG_ES, &LANG_PT, &LANG_FR, &LANG_ID]
}

static SHARED: LazyLock<PromptAnalyzer> = LazyLock::new(PromptAnalyzer::new);

/// True when an utterance is natural-language chat the analyzer may inspect.
/// Slash commands and system triggers are never soft-nudged.
pub fn is_natural_chat(text: &str) -> bool {
    let t = text.trim_start();
    !t.starts_with('/') && !t.to_ascii_lowercase().starts_with("[system")
}

/// Prompt analyzer that detects keywords and suggests tool usage
pub struct PromptAnalyzer {
    /// Multi-word phrase regexes (scanned across all languages)
    plan_phrase: Regex,
    read_file_phrase: Regex,
    search_phrase: Regex,
    write_file_phrase: Regex,
    edit_file_phrase: Regex,
    bash_phrase: Regex,
    web_search_phrase: Regex,
}

impl PromptAnalyzer {
    /// Create a new prompt analyzer
    pub fn new() -> Self {
        // Collect all multi-word phrases across all languages
        let plan_phrases: Vec<&str> = all_langs()
            .iter()
            .flat_map(|lang| {
                lang.plan
                    .iter()
                    .filter(|k| k.contains(' '))
                    .map(|s| s.as_str())
            })
            .collect();
        let read_file_phrases: Vec<&str> = all_langs()
            .iter()
            .flat_map(|lang| {
                lang.read_file
                    .iter()
                    .filter(|k| k.contains(' '))
                    .map(|s| s.as_str())
            })
            .collect();
        let search_phrases: Vec<&str> = all_langs()
            .iter()
            .flat_map(|lang| {
                lang.search
                    .iter()
                    .filter(|k| k.contains(' '))
                    .map(|s| s.as_str())
            })
            .collect();
        let write_file_phrases: Vec<&str> = all_langs()
            .iter()
            .flat_map(|lang| {
                lang.write_file
                    .iter()
                    .filter(|k| k.contains(' '))
                    .map(|s| s.as_str())
            })
            .collect();
        let edit_file_phrases: Vec<&str> = all_langs()
            .iter()
            .flat_map(|lang| {
                lang.edit_file
                    .iter()
                    .filter(|k| k.contains(' '))
                    .map(|s| s.as_str())
            })
            .collect();
        let bash_phrases: Vec<&str> = all_langs()
            .iter()
            .flat_map(|lang| {
                lang.bash
                    .iter()
                    .filter(|k| k.contains(' '))
                    .map(|s| s.as_str())
            })
            .collect();
        let web_search_phrases: Vec<&str> = all_langs()
            .iter()
            .flat_map(|lang| {
                lang.web_search
                    .iter()
                    .filter(|k| k.contains(' '))
                    .map(|s| s.as_str())
            })
            .collect();

        Self {
            plan_phrase: Self::build_keyword_regex(&plan_phrases),
            read_file_phrase: Self::build_keyword_regex(&read_file_phrases),
            search_phrase: Self::build_keyword_regex(&search_phrases),
            write_file_phrase: Self::build_keyword_regex(&write_file_phrases),
            edit_file_phrase: Self::build_keyword_regex(&edit_file_phrases),
            bash_phrase: Self::build_keyword_regex(&bash_phrases),
            web_search_phrase: Self::build_keyword_regex(&web_search_phrases),
        }
    }

    /// Process-wide shared instance, so the keyword regexes compile once.
    pub fn shared() -> &'static PromptAnalyzer {
        &SHARED
    }

    /// Build a regex from keywords (case-insensitive, word boundaries)
    fn build_keyword_regex(keywords: &[&str]) -> Regex {
        if keywords.is_empty() {
            // Return a regex that never matches
            return Regex::new(r"(?i)\b(NEVER_MATCH_THIS)\b").expect("Failed to compile regex");
        }
        let pattern = keywords
            .iter()
            .map(|k| regex::escape(k))
            .collect::<Vec<_>>()
            .join("|");
        Regex::new(&format!(r"(?i)\b({})\b", pattern)).expect("Failed to compile keyword regex")
    }

    /// Build a regex for a specific language's single-word keywords
    fn build_word_regex_for_lang(keywords: &[String]) -> Regex {
        let words: Vec<&str> = keywords
            .iter()
            .filter(|k| !k.contains(' '))
            .map(|s| s.as_str())
            .collect();
        Self::build_keyword_regex(&words)
    }

    /// Return the hint section for a prompt, or `None` when no keyword
    /// family matches. Callers append this to the LLM agent input only,
    /// never to user-visible display text.
    pub fn hints_for(&self, prompt: &str) -> Option<String> {
        let mut transformations = Vec::new();
        let lower_prompt = prompt.to_lowercase();

        // Detect language for single-word keyword gating
        let lang = detect_language(prompt);
        let lang_name = if std::ptr::eq(lang, &*LANG_RU) {
            "ru"
        } else if std::ptr::eq(lang, &*LANG_ES) {
            "es"
        } else if std::ptr::eq(lang, &*LANG_PT) {
            "pt"
        } else if std::ptr::eq(lang, &*LANG_FR) {
            "fr"
        } else if std::ptr::eq(lang, &*LANG_ID) {
            "id"
        } else {
            "en"
        };

        // Build language-specific word regexes on the fly (cheap, only when needed)
        let plan_word_lang = Self::build_word_regex_for_lang(&lang.plan);
        let read_file_word_lang = Self::build_word_regex_for_lang(&lang.read_file);
        let search_word_lang = Self::build_word_regex_for_lang(&lang.search);
        let write_file_word_lang = Self::build_word_regex_for_lang(&lang.write_file);
        let edit_file_word_lang = Self::build_word_regex_for_lang(&lang.edit_file);
        let bash_word_lang = Self::build_word_regex_for_lang(&lang.bash);
        let web_search_word_lang = Self::build_word_regex_for_lang(&lang.web_search);

        // Prompt snippet for logging (first 40 chars, respecting char boundaries)
        let snippet = if prompt.len() > 40 {
            let mut end = 40;
            while end > 0 && !prompt.is_char_boundary(end) {
                end -= 1;
            }
            format!("{}...", &prompt[..end])
        } else {
            prompt.to_string()
        };

        // Check for plan keywords: design track (the user said "plan", so
        // they get a reviewable SESSION PLAN and an Approve step, not a
        // checklist that starts executing on its own).
        // Multi-word phrases scanned across all languages, single-word gated to detected lang
        let plan_match = self.plan_phrase.captures(&lower_prompt).or_else(|| {
            if plan_word_lang.is_match(&lower_prompt) {
                plan_word_lang.captures(&lower_prompt)
            } else {
                None
            }
        });
        if let Some(caps) = plan_match {
            let matched = caps.get(1).map_or("unknown", |m| m.as_str());
            tracing::info!(
                "🔍 Detected PLAN intent: keyword='{}', lang={}, prompt='{}'",
                matched,
                lang_name,
                snippet
            );
            transformations.push(
                "\n\n**CRITICAL**: The user wants a PLAN — enter the design track NOW:\n\
                1. Explore first if needed (reads, search, bash are available pre-init).\n\
                2. plan(operation='init', mode='design', title='...') creates the \
                SESSION PLAN .md and returns its absolute path.\n\
                3. Write the design INTO that .md (write_file/edit_file on that exact \
                path — the only writable file) using the template: ## Context with \
                **Problem:** / **Target state:** / **Intent:**, then numbered \
                ## Implementation steps.\n\
                4. STOP and wait for the user to APPROVE the plan (/execute). Do NOT \
                call start, do NOT edit project files, do NOT paste the plan in chat.\n\
                The checklist is seeded automatically after Approve. Valid operations \
                are EXACTLY: init, add_tasks, add_task, start, complete. There is NO \
                'create' and NO 'finalize' operation, never call those.",
            );
        }

        // Check for read_file keywords
        let read_file_match = self.read_file_phrase.captures(&lower_prompt).or_else(|| {
            if read_file_word_lang.is_match(&lower_prompt) {
                read_file_word_lang.captures(&lower_prompt)
            } else {
                None
            }
        });
        if let Some(caps) = read_file_match {
            let matched = caps.get(1).map_or("unknown", |m| m.as_str());
            tracing::info!(
                "🔍 Detected READ_FILE intent: keyword='{}', lang={}, prompt='{}'",
                matched,
                lang_name,
                snippet
            );
            transformations
                .push("\n\n**TOOL HINT**: Use the `read_file` tool to read the contents of files.");
        }

        // Check for search keywords
        let search_match = self.search_phrase.captures(&lower_prompt).or_else(|| {
            if search_word_lang.is_match(&lower_prompt) {
                search_word_lang.captures(&lower_prompt)
            } else {
                None
            }
        });
        if let Some(caps) = search_match {
            let matched = caps.get(1).map_or("unknown", |m| m.as_str());
            tracing::info!(
                "🔍 Detected SEARCH/GREP intent: keyword='{}', lang={}, prompt='{}'",
                matched,
                lang_name,
                snippet
            );
            transformations.push(
                "\n\n**TOOL HINT**: Use the `grep` tool to search for patterns in files, \
                or use `glob` to find files by pattern.",
            );
        }

        // Check for write_file keywords
        let write_file_match = self.write_file_phrase.captures(&lower_prompt).or_else(|| {
            if write_file_word_lang.is_match(&lower_prompt) {
                write_file_word_lang.captures(&lower_prompt)
            } else {
                None
            }
        });
        if let Some(caps) = write_file_match {
            let matched = caps.get(1).map_or("unknown", |m| m.as_str());
            tracing::info!(
                "🔍 Detected WRITE_FILE intent: keyword='{}', lang={}, prompt='{}'",
                matched,
                lang_name,
                snippet
            );
            transformations
                .push("\n\n**TOOL HINT**: Use the `write_file` tool to create new files.");
        }

        // Check for edit_file keywords
        let edit_file_match = self.edit_file_phrase.captures(&lower_prompt).or_else(|| {
            if edit_file_word_lang.is_match(&lower_prompt) {
                edit_file_word_lang.captures(&lower_prompt)
            } else {
                None
            }
        });
        if let Some(caps) = edit_file_match {
            let matched = caps.get(1).map_or("unknown", |m| m.as_str());
            tracing::info!(
                "🔍 Detected EDIT_FILE intent: keyword='{}', lang={}, prompt='{}'",
                matched,
                lang_name,
                snippet
            );
            transformations
                .push("\n\n**TOOL HINT**: Use the `edit_file` tool to modify existing files.");
        }

        // Check for bash keywords
        let bash_match = self.bash_phrase.captures(&lower_prompt).or_else(|| {
            if bash_word_lang.is_match(&lower_prompt) {
                bash_word_lang.captures(&lower_prompt)
            } else {
                None
            }
        });
        if let Some(caps) = bash_match {
            let matched = caps.get(1).map_or("unknown", |m| m.as_str());
            tracing::info!(
                "🔍 Detected BASH intent: keyword='{}', lang={}, prompt='{}'",
                matched,
                lang_name,
                snippet
            );
            transformations
                .push("\n\n**TOOL HINT**: Use the `bash` tool to execute shell commands.");
        }

        // Check for web_search keywords
        let web_search_match = self.web_search_phrase.captures(&lower_prompt).or_else(|| {
            if web_search_word_lang.is_match(&lower_prompt) {
                web_search_word_lang.captures(&lower_prompt)
            } else {
                None
            }
        });
        if let Some(caps) = web_search_match {
            let matched = caps.get(1).map_or("unknown", |m| m.as_str());
            tracing::info!(
                "🔍 Detected WEB_SEARCH intent: keyword='{}', lang={}, prompt='{}'",
                matched,
                lang_name,
                snippet
            );
            transformations.push(
                "\n\n**TOOL HINT**: Use the `web_search` tool to search the internet for \
                real-time information.",
            );
        }

        if transformations.is_empty() {
            None
        } else {
            Some(transformations.join(""))
        }
    }

    /// Whether the prompt matches the plan keyword family. Callers use
    /// this to set the durable `pre_init_editing` flag when a plan-shaped
    /// message arrives on natural-language chat.
    pub fn plan_intent(&self, prompt: &str) -> bool {
        let lower_prompt = prompt.to_lowercase();
        let lang = detect_language(prompt);
        let plan_word_lang = Self::build_word_regex_for_lang(&lang.plan);
        self.plan_phrase.is_match(&lower_prompt) || plan_word_lang.is_match(&lower_prompt)
    }

    /// Analyze a prompt and transform it if needed
    pub fn analyze_and_transform(&self, prompt: &str) -> String {
        match self.hints_for(prompt) {
            Some(hint_section) => format!("{}{}", prompt, hint_section),
            None => prompt.to_string(),
        }
    }
}

impl Default for PromptAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}