|
1
|
//! siGit Code Cloud Agent runs. |
|
2
|
|
|
3
|
use { |
|
4
|
crate::{client::Client, error::Result, models::AgentRun}, |
|
5
|
reqwest::{Method, RequestBuilder}, |
|
6
|
serde::Deserialize, |
|
7
|
}; |
|
8
|
|
|
9
|
#[derive(Deserialize)] |
|
10
|
struct AgentRuns { |
|
11
|
agent_runs: Vec<AgentRun>, |
|
12
|
} |
|
13
|
|
|
14
|
impl Client { |
|
15
|
/// `GET /api/v1/agent/runs` — your runs, newest first. |
|
16
|
pub async fn agent_runs(&self) -> Result<Vec<AgentRun>> { |
|
17
|
let runs: AgentRuns = self.get("agent/runs").await?; |
|
18
|
Ok(runs.agent_runs) |
|
19
|
} |
|
20
|
|
|
21
|
/// `GET /api/v1/agent/runs/:id` — one run, with the tail of its |
|
22
|
/// transcript in `transcript_tail`. |
|
23
|
pub async fn agent_run(&self, id: i64) -> Result<AgentRun> { |
|
24
|
self.get(&format!("agent/runs/{id}")).await |
|
25
|
} |
|
26
|
|
|
27
|
/// `POST /api/v1/agent/runs` — start a run on a GitHub repository that has |
|
28
|
/// the siGit Code GitHub App installed and that your connected GitHub |
|
29
|
/// account can write to. |
|
30
|
pub async fn create_agent_run(&self, repo_full_name: &str, prompt: &str) -> Result<AgentRun> { |
|
31
|
self.send(self.create_agent_run_request(repo_full_name, prompt)) |
|
32
|
.await |
|
33
|
} |
|
34
|
|
|
35
|
/// `POST /api/v1/agent/runs/:id/cancel` |
|
36
|
pub async fn cancel_agent_run(&self, id: i64) -> Result<AgentRun> { |
|
37
|
self.send(self.request(Method::POST, &format!("agent/runs/{id}/cancel"))) |
|
38
|
.await |
|
39
|
} |
|
40
|
|
|
41
|
/// `GET /api/v1/agent/runs/:id/handoff` — the branch, prompt and command to |
|
42
|
/// carry on with a run on your own machine. Returned as the server sent it. |
|
43
|
pub async fn agent_run_handoff(&self, id: i64) -> Result<serde_json::Value> { |
|
44
|
self.get(&format!("agent/runs/{id}/handoff")).await |
|
45
|
} |
|
46
|
|
|
47
|
fn create_agent_run_request(&self, repo_full_name: &str, prompt: &str) -> RequestBuilder { |
|
48
|
self.request(Method::POST, "agent/runs") |
|
49
|
.json(&serde_json::json!({ |
|
50
|
"repo_full_name": repo_full_name, |
|
51
|
"prompt": prompt, |
|
52
|
})) |
|
53
|
} |
|
54
|
} |
|
55
|
|
|
56
|
#[cfg(test)] |
|
57
|
mod tests { |
|
58
|
use {super::*, crate::Environment}; |
|
59
|
|
|
60
|
fn client() -> Client { |
|
61
|
Client::builder(Environment::Production) |
|
62
|
.base_url("https://sigit.si") |
|
63
|
.token("tok") |
|
64
|
.build() |
|
65
|
.unwrap() |
|
66
|
} |
|
67
|
|
|
68
|
#[test] |
|
69
|
fn a_run_is_started_with_the_repo_and_prompt() { |
|
70
|
let request = client() |
|
71
|
.create_agent_run_request("seto/demo", "Fix the build") |
|
72
|
.build() |
|
73
|
.unwrap(); |
|
74
|
|
|
75
|
assert_eq!(request.method(), Method::POST); |
|
76
|
assert_eq!(request.url().as_str(), "https://sigit.si/api/v1/agent/runs"); |
|
77
|
let body = request.body().and_then(|b| b.as_bytes()).unwrap(); |
|
78
|
let body: serde_json::Value = serde_json::from_slice(body).unwrap(); |
|
79
|
assert_eq!(body["repo_full_name"], "seto/demo"); |
|
80
|
assert_eq!(body["prompt"], "Fix the build"); |
|
81
|
} |
|
82
|
|
|
83
|
#[test] |
|
84
|
fn the_list_is_unwrapped() { |
|
85
|
let body = r#"{"agent_runs":[{"id":7,"title":null,"repo_full_name":"seto/demo", |
|
86
|
"trigger_kind":"api","status":"running","cancel_requested":false, |
|
87
|
"exit_reason":null,"base_branch":"main","head_branch":"sigit/x", |
|
88
|
"pull_request_url":null,"issue_number":null,"pr_number":null, |
|
89
|
"inference_requests_used":3,"created_at":"2026-10-01T10:00:00Z", |
|
90
|
"started_at":null,"finished_at":null}]}"#; |
|
91
|
let runs: AgentRuns = serde_json::from_str(body).unwrap(); |
|
92
|
assert_eq!(runs.agent_runs[0].id, 7); |
|
93
|
assert!(runs.agent_runs[0].transcript_tail.is_none()); |
|
94
|
} |
|
95
|
|
|
96
|
#[test] |
|
97
|
fn a_single_run_carries_its_transcript_tail() { |
|
98
|
let body = r#"{"id":7,"title":"t","repo_full_name":"seto/demo","trigger_kind":"api", |
|
99
|
"status":"succeeded","cancel_requested":false,"exit_reason":null, |
|
100
|
"base_branch":null,"head_branch":null,"pull_request_url":null, |
|
101
|
"issue_number":null,"pr_number":null,"inference_requests_used":0, |
|
102
|
"created_at":"2026-10-01T10:00:00Z","started_at":null,"finished_at":null, |
|
103
|
"transcript_tail":"line 1\nline 2\n"}"#; |
|
104
|
let run: AgentRun = serde_json::from_str(body).unwrap(); |
|
105
|
assert_eq!(run.transcript_tail.as_deref(), Some("line 1\nline 2\n")); |
|
106
|
} |
|
107
|
} |