Skip to main content

ai_agent/tasks/
types.rs

1// Source: ~/claudecode/openclaudecode/src/tasks/types.ts
2
3#![allow(dead_code)]
4
5use serde::{Deserialize, Serialize};
6
7/// Union of all concrete task state types.
8/// Use this for components that need to work with any task type.
9pub enum TaskState {
10    LocalShell(crate::tasks::guards::LocalShellTaskState),
11    LocalAgent(std::collections::HashMap<String, serde_json::Value>),
12    RemoteAgent(std::collections::HashMap<String, serde_json::Value>),
13    InProcessTeammate(std::collections::HashMap<String, serde_json::Value>),
14    LocalWorkflow(crate::tasks::local_workflow_task::LocalWorkflowTaskState),
15    MonitorMcp(crate::tasks::monitor_mcp_task::MonitorMcpTaskState),
16    Dream(std::collections::HashMap<String, serde_json::Value>),
17}
18
19/// Task types that can appear in the background tasks indicator.
20pub enum BackgroundTaskState {
21    LocalShell(crate::tasks::guards::LocalShellTaskState),
22    LocalAgent(std::collections::HashMap<String, serde_json::Value>),
23    RemoteAgent(std::collections::HashMap<String, serde_json::Value>),
24    InProcessTeammate(std::collections::HashMap<String, serde_json::Value>),
25    LocalWorkflow(crate::tasks::local_workflow_task::LocalWorkflowTaskState),
26    MonitorMcp(crate::tasks::monitor_mcp_task::MonitorMcpTaskState),
27    Dream(std::collections::HashMap<String, serde_json::Value>),
28}
29
30impl BackgroundTaskState {
31    /// Returns the task type string.
32    pub fn task_type(&self) -> &'static str {
33        match self {
34            BackgroundTaskState::LocalShell(_) => "local_bash",
35            BackgroundTaskState::LocalAgent(_) => "local_agent",
36            BackgroundTaskState::RemoteAgent(_) => "remote_agent",
37            BackgroundTaskState::InProcessTeammate(_) => "in_process_teammate",
38            BackgroundTaskState::LocalWorkflow(_) => "local_workflow",
39            BackgroundTaskState::MonitorMcp(_) => "monitor_mcp",
40            BackgroundTaskState::Dream(_) => "dream",
41        }
42    }
43
44    /// Returns the task status.
45    pub fn status(&self) -> crate::task::TaskStatus {
46        match self {
47            BackgroundTaskState::LocalShell(t) => t.status.clone(),
48            BackgroundTaskState::LocalAgent(t) => t
49                .get("status")
50                .and_then(|v| v.as_str())
51                .and_then(|s| match s {
52                    "pending" => Some(crate::task::TaskStatus::pending),
53                    "running" => Some(crate::task::TaskStatus::running),
54                    "completed" => Some(crate::task::TaskStatus::completed),
55                    "failed" => Some(crate::task::TaskStatus::failed),
56                    "killed" => Some(crate::task::TaskStatus::killed),
57                    _ => None,
58                })
59                .unwrap_or(crate::task::TaskStatus::pending),
60            BackgroundTaskState::RemoteAgent(t) => t
61                .get("status")
62                .and_then(|v| v.as_str())
63                .and_then(|s| match s {
64                    "pending" => Some(crate::task::TaskStatus::pending),
65                    "running" => Some(crate::task::TaskStatus::running),
66                    "completed" => Some(crate::task::TaskStatus::completed),
67                    "failed" => Some(crate::task::TaskStatus::failed),
68                    "killed" => Some(crate::task::TaskStatus::killed),
69                    _ => None,
70                })
71                .unwrap_or(crate::task::TaskStatus::pending),
72            BackgroundTaskState::InProcessTeammate(t) => t
73                .get("status")
74                .and_then(|v| v.as_str())
75                .and_then(|s| match s {
76                    "pending" => Some(crate::task::TaskStatus::pending),
77                    "running" => Some(crate::task::TaskStatus::running),
78                    "completed" => Some(crate::task::TaskStatus::completed),
79                    "failed" => Some(crate::task::TaskStatus::failed),
80                    "killed" => Some(crate::task::TaskStatus::killed),
81                    _ => None,
82                })
83                .unwrap_or(crate::task::TaskStatus::pending),
84            BackgroundTaskState::LocalWorkflow(t) => t.status.clone(),
85            BackgroundTaskState::MonitorMcp(t) => t.status.clone(),
86            BackgroundTaskState::Dream(t) => t
87                .get("status")
88                .and_then(|v| v.as_str())
89                .and_then(|s| match s {
90                    "pending" => Some(crate::task::TaskStatus::pending),
91                    "running" => Some(crate::task::TaskStatus::running),
92                    "completed" => Some(crate::task::TaskStatus::completed),
93                    "failed" => Some(crate::task::TaskStatus::failed),
94                    "killed" => Some(crate::task::TaskStatus::killed),
95                    _ => None,
96                })
97                .unwrap_or(crate::task::TaskStatus::pending),
98        }
99    }
100}
101
102/// Check if a task should be shown in the background tasks indicator.
103/// A task is considered a background task if:
104/// 1. It is running or pending
105/// 2. It has been explicitly backgrounded (not a foreground task)
106pub fn is_background_task(task: &BackgroundTaskState) -> bool {
107    let status = task.status();
108    if status != crate::task::TaskStatus::running && status != crate::task::TaskStatus::pending {
109        return false;
110    }
111
112    // Foreground tasks (is_backgrounded === false) are not yet "background tasks"
113    if let BackgroundTaskState::LocalShell(t) = task {
114        if t.is_backgrounded == Some(false) {
115            return false;
116        }
117    }
118
119    true
120}