summaryrefslogtreecommitdiff
path: root/makima/src/daemon/api/contract.rs
blob: aac6b9475136b8e6f11efb3f22a5480532c3537a (plain) (blame)
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
//! Contract API methods.

use serde::Serialize;
use uuid::Uuid;

use super::client::{ApiClient, ApiError};
use super::supervisor::JsonValue;

// Request types

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ReportRequest {
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub task_id: Option<Uuid>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionActionRequest {
    pub lines_added: i32,
    pub lines_removed: i32,
    pub has_code_changes: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub task_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub files_modified: Option<Vec<String>>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateFileRequest {
    pub content: String,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateFileRequest {
    pub name: String,
    pub content: String,
}

impl ApiClient {
    /// Get contract status.
    pub async fn contract_status(&self, contract_id: Uuid) -> Result<JsonValue, ApiError> {
        self.get(&format!("/api/v1/contracts/{}/daemon/status", contract_id))
            .await
    }

    /// Get phase checklist.
    pub async fn contract_checklist(&self, contract_id: Uuid) -> Result<JsonValue, ApiError> {
        self.get(&format!("/api/v1/contracts/{}/daemon/checklist", contract_id))
            .await
    }

    /// Get contract goals.
    pub async fn contract_goals(&self, contract_id: Uuid) -> Result<JsonValue, ApiError> {
        self.get(&format!("/api/v1/contracts/{}/daemon/goals", contract_id))
            .await
    }

    /// List contract files.
    pub async fn contract_files(&self, contract_id: Uuid) -> Result<JsonValue, ApiError> {
        self.get(&format!("/api/v1/contracts/{}/daemon/files", contract_id))
            .await
    }

    /// Get a specific file.
    pub async fn contract_file(
        &self,
        contract_id: Uuid,
        file_id: Uuid,
    ) -> Result<JsonValue, ApiError> {
        self.get(&format!(
            "/api/v1/contracts/{}/daemon/files/{}",
            contract_id, file_id
        ))
        .await
    }

    /// Report progress.
    pub async fn contract_report(
        &self,
        contract_id: Uuid,
        message: &str,
        task_id: Option<Uuid>,
    ) -> Result<JsonValue, ApiError> {
        let req = ReportRequest {
            message: message.to_string(),
            task_id,
        };
        self.post(&format!("/api/v1/contracts/{}/daemon/report", contract_id), &req)
            .await
    }

    /// Get suggested action.
    pub async fn contract_suggest_action(&self, contract_id: Uuid) -> Result<JsonValue, ApiError> {
        self.post_empty(&format!(
            "/api/v1/contracts/{}/daemon/suggest-action",
            contract_id
        ))
        .await
    }

    /// Get completion action recommendation.
    pub async fn contract_completion_action(
        &self,
        contract_id: Uuid,
        task_id: Option<Uuid>,
        files_modified: Option<Vec<String>>,
        lines_added: i32,
        lines_removed: i32,
        has_code_changes: bool,
    ) -> Result<JsonValue, ApiError> {
        let req = CompletionActionRequest {
            task_id,
            files_modified,
            lines_added,
            lines_removed,
            has_code_changes,
        };
        self.post(
            &format!("/api/v1/contracts/{}/daemon/completion-action", contract_id),
            &req,
        )
        .await
    }

    /// Update a file.
    pub async fn contract_update_file(
        &self,
        contract_id: Uuid,
        file_id: Uuid,
        content: &str,
    ) -> Result<JsonValue, ApiError> {
        let req = UpdateFileRequest {
            content: content.to_string(),
        };
        self.put(
            &format!("/api/v1/contracts/{}/daemon/files/{}", contract_id, file_id),
            &req,
        )
        .await
    }

    /// Create a new file.
    pub async fn contract_create_file(
        &self,
        contract_id: Uuid,
        name: &str,
        content: &str,
    ) -> Result<JsonValue, ApiError> {
        let req = CreateFileRequest {
            name: name.to_string(),
            content: content.to_string(),
        };
        self.post(&format!("/api/v1/contracts/{}/daemon/files", contract_id), &req)
            .await
    }
}