Skip to main content

vtcode_desktop_protocol/
goal.rs

1//! Quiet `/goal` execution for desktop clients.
2//!
3//! Typed slash commands submitted as turns would wedge (a non-turn slash
4//! produces no terminal turn event), so the desktop client executes goal
5//! commands through `goal/execute` instead. The service parses `args` with
6//! the shared [`parse_goal_slash_command`](crate::slash_command::parse_goal_slash_command)
7//! grammar, applies the shared runloop core, and returns render lines plus
8//! the fresh snapshot; a `submit` prompt starts a real turn when set.
9
10use serde::{Deserialize, Serialize};
11use vtcode_exec_events::goal::GoalSnapshot;
12
13/// Parameters for executing a `/goal` command against a session.
14#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15#[serde(rename_all = "camelCase")]
16pub struct GoalExecuteParams {
17    /// Session owning the goal loop.
18    pub session_id: String,
19    /// Raw arguments after `/goal` (empty shows status).
20    pub args: String,
21}
22
23/// Result of a `/goal` execution.
24#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
25#[serde(rename_all = "camelCase")]
26pub struct GoalExecuteResult {
27    /// Session that executed the command, echoed so clients discard stale responses.
28    pub session_id: String,
29    /// Status lines to show the user, in order.
30    pub lines: Vec<String>,
31    /// Whether the tracker changed (persisted; bar refreshes from `goal`).
32    pub mutated: bool,
33    /// Turn prompt to submit after rendering (`Set` starts work on the
34    /// objective; successful `Resume` continues it; otherwise `None`).
35    #[serde(default, skip_serializing_if = "Option::is_none")]
36    pub submit: Option<String>,
37    /// Fresh snapshot after applying, or `None` when no goal exists.
38    #[serde(default, skip_serializing_if = "Option::is_none")]
39    pub goal: Option<GoalSnapshot>,
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use vtcode_exec_events::goal::GoalState;
46
47    #[test]
48    fn goal_execute_params_use_camel_case_keys() {
49        let params = GoalExecuteParams { session_id: "s".into(), args: "pause".into() };
50        let json = serde_json::to_value(&params).unwrap();
51        assert_eq!(json["sessionId"], "s");
52        assert_eq!(json["args"], "pause");
53    }
54
55    #[test]
56    fn goal_execute_result_round_trips_and_omits_empty_fields() {
57        let full = GoalExecuteResult {
58            session_id: "s".into(),
59            lines: vec!["Goal set: Ship it".into()],
60            mutated: true,
61            submit: Some("Ship it".into()),
62            goal: Some(GoalSnapshot {
63                goal_id: "goal-1".into(),
64                state: GoalState::Active,
65                objective: "Ship it".into(),
66                elapsed_ms: 0,
67                tokens_used: 0,
68                token_budget: None,
69                pause_message: None,
70            }),
71        };
72        let json = serde_json::to_string(&full).unwrap();
73        let restored: GoalExecuteResult = serde_json::from_str(&json).unwrap();
74        assert_eq!(restored, full);
75
76        let minimal = GoalExecuteResult {
77            session_id: "s".into(),
78            lines: vec![],
79            mutated: false,
80            submit: None,
81            goal: None,
82        };
83        let json = serde_json::to_value(&minimal).unwrap();
84        assert_eq!(json["sessionId"], "s");
85        assert!(json.get("submit").is_none());
86        assert!(json.get("goal").is_none());
87    }
88}