aboutsummaryrefslogtreecommitdiff
path: root/src/detect/agents/mod.rs
blob: 46a25f661414166f1b8a9efd91def8bef1e681ef (plain)
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
pub(super) mod amp;
pub(super) mod antigravity;
pub(super) mod claude_code;
pub(super) mod cline;
pub(super) mod codex;
pub(super) mod cursor;
pub(super) mod droid;
pub(super) mod gemini;
pub(super) mod github_copilot;
pub(super) mod grok;
pub(super) mod hermes;
pub(super) mod kilo;
pub(super) mod kimi;
pub(super) mod kiro;
pub(super) mod opencode;
pub(super) mod pi;
pub(super) mod qodercli;

use super::{Agent, AgentDetection, AgentState};

pub(super) fn detect(agent: Agent, screen_content: &str) -> AgentDetection {
    let state = match agent {
        Agent::Pi => pi::detect(screen_content),
        Agent::Claude => claude_code::detect(screen_content),
        Agent::Codex => codex::detect(screen_content),
        Agent::Gemini => gemini::detect(screen_content),
        Agent::Cursor => cursor::detect(screen_content),
        Agent::Antigravity => antigravity::detect(screen_content),
        Agent::Cline => cline::detect(screen_content),
        Agent::OpenCode => opencode::detect(screen_content),
        Agent::GithubCopilot => github_copilot::detect(screen_content),
        Agent::Kimi => kimi::detect(screen_content),
        Agent::Kiro => kiro::detect(screen_content),
        Agent::Droid => droid::detect(screen_content),
        Agent::Amp => amp::detect(screen_content),
        Agent::Grok => grok::detect(screen_content),
        Agent::Hermes => hermes::detect(screen_content),
        Agent::Kilo => kilo::detect(screen_content),
        Agent::Qodercli => qodercli::detect(screen_content),
    };

    let skip_state_update = should_skip_state_update(agent, screen_content);
    if skip_state_update {
        return AgentDetection {
            state,
            skip_state_update: true,
            visible_blocker: false,
            visible_idle: false,
            visible_working: false,
        };
    }

    AgentDetection {
        state,
        skip_state_update: false,
        visible_blocker: has_visible_blocker(agent, screen_content, state),
        visible_idle: has_visible_idle(agent, screen_content, state),
        visible_working: has_visible_working(agent, screen_content, state),
    }
}

pub(super) fn should_skip_state_update(agent: Agent, content: &str) -> bool {
    match agent {
        Agent::Claude => claude_code::is_transcript_viewer(content),
        Agent::Codex => codex::is_transcript_viewer(content),
        _ => false,
    }
}

fn has_visible_blocker(agent: Agent, content: &str, state: AgentState) -> bool {
    if state != AgentState::Blocked {
        return false;
    }

    match agent {
        // Strong visible blockers are opt-in because this flag can override
        // hook authority. Plain blocked heuristics remain valid fallback state,
        // but they must not become hook overrides unless the current UI chrome
        // is known to be structural and live.
        Agent::Claude => claude_code::has_visible_blocker(content),
        Agent::Codex => codex::has_visible_blocker(content),
        Agent::Kimi => kimi::has_visible_blocker(content),
        _ => false,
    }
}

fn has_visible_idle(agent: Agent, content: &str, state: AgentState) -> bool {
    if state != AgentState::Idle {
        return false;
    }

    match agent {
        Agent::Claude => claude_code::has_prompt_box(content),
        Agent::Codex => codex::has_prompt(content),
        Agent::Kimi => kimi::has_prompt_box(content),
        _ => false,
    }
}

fn has_visible_working(agent: Agent, content: &str, state: AgentState) -> bool {
    if state != AgentState::Working {
        return false;
    }

    match agent {
        Agent::Claude => claude_code::has_working_chrome(content),
        Agent::Codex => codex::has_visible_working(content),
        Agent::Kimi => kimi::has_visible_working(content),
        _ => false,
    }
}