Skip to main content

codex_state/model/
thread_goal.rs

1use anyhow::Result;
2use anyhow::anyhow;
3use chrono::DateTime;
4use chrono::Utc;
5use codex_protocol::ThreadId;
6use serde::Serialize;
7use sqlx::Row;
8use sqlx::sqlite::SqliteRow;
9
10use super::epoch_millis_to_datetime;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
13#[serde(rename_all = "snake_case")]
14pub enum ThreadGoalStatus {
15    Active,
16    Paused,
17    Blocked,
18    UsageLimited,
19    BudgetLimited,
20    Complete,
21}
22
23impl ThreadGoalStatus {
24    pub fn as_str(self) -> &'static str {
25        match self {
26            Self::Active => "active",
27            Self::Paused => "paused",
28            Self::Blocked => "blocked",
29            Self::UsageLimited => "usage_limited",
30            Self::BudgetLimited => "budget_limited",
31            Self::Complete => "complete",
32        }
33    }
34
35    pub fn is_active(self) -> bool {
36        self == Self::Active
37    }
38
39    pub fn is_terminal(self) -> bool {
40        matches!(self, Self::BudgetLimited | Self::Complete)
41    }
42}
43
44impl TryFrom<&str> for ThreadGoalStatus {
45    type Error = anyhow::Error;
46
47    fn try_from(value: &str) -> Result<Self> {
48        match value {
49            "active" => Ok(Self::Active),
50            "paused" => Ok(Self::Paused),
51            "blocked" => Ok(Self::Blocked),
52            "usage_limited" => Ok(Self::UsageLimited),
53            "budget_limited" => Ok(Self::BudgetLimited),
54            "complete" => Ok(Self::Complete),
55            other => Err(anyhow!("unknown thread goal status `{other}`")),
56        }
57    }
58}
59
60#[derive(Debug, Clone, PartialEq, Eq)]
61pub struct ThreadGoal {
62    pub thread_id: ThreadId,
63    pub goal_id: String,
64    pub objective: String,
65    pub status: ThreadGoalStatus,
66    pub token_budget: Option<i64>,
67    pub tokens_used: i64,
68    pub time_used_seconds: i64,
69    pub created_at: DateTime<Utc>,
70    pub updated_at: DateTime<Utc>,
71}
72
73pub(crate) struct ThreadGoalRow {
74    pub thread_id: String,
75    pub goal_id: String,
76    pub objective: String,
77    pub status: String,
78    pub token_budget: Option<i64>,
79    pub tokens_used: i64,
80    pub time_used_seconds: i64,
81    pub created_at_ms: i64,
82    pub updated_at_ms: i64,
83}
84
85impl ThreadGoalRow {
86    pub(crate) fn try_from_row(row: &SqliteRow) -> Result<Self> {
87        Ok(Self {
88            thread_id: row.try_get("thread_id")?,
89            goal_id: row.try_get("goal_id")?,
90            objective: row.try_get("objective")?,
91            status: row.try_get("status")?,
92            token_budget: row.try_get("token_budget")?,
93            tokens_used: row.try_get("tokens_used")?,
94            time_used_seconds: row.try_get("time_used_seconds")?,
95            created_at_ms: row.try_get("created_at_ms")?,
96            updated_at_ms: row.try_get("updated_at_ms")?,
97        })
98    }
99}
100
101impl TryFrom<ThreadGoalRow> for ThreadGoal {
102    type Error = anyhow::Error;
103
104    fn try_from(row: ThreadGoalRow) -> Result<Self> {
105        Ok(Self {
106            thread_id: ThreadId::try_from(row.thread_id)?,
107            goal_id: row.goal_id,
108            objective: row.objective,
109            status: ThreadGoalStatus::try_from(row.status.as_str())?,
110            token_budget: row.token_budget,
111            tokens_used: row.tokens_used,
112            time_used_seconds: row.time_used_seconds,
113            created_at: epoch_millis_to_datetime(row.created_at_ms)?,
114            updated_at: epoch_millis_to_datetime(row.updated_at_ms)?,
115        })
116    }
117}