|
1
|
//! siGit Code Cloud Sessions — named, resumable conversations. |
|
2
|
|
|
3
|
use crate::{ |
|
4
|
client::Client, |
|
5
|
error::Result, |
|
6
|
models::{AppendedMessage, CloudSession, Model}, |
|
7
|
}; |
|
8
|
|
|
9
|
impl Client { |
|
10
|
/// `GET /api/v1/sessions` — most recently updated first. |
|
11
|
pub async fn sessions(&self) -> Result<Vec<CloudSession>> { |
|
12
|
self.get("sessions").await |
|
13
|
} |
|
14
|
|
|
15
|
/// `GET /api/v1/sessions/:id` — the session plus its full transcript. |
|
16
|
pub async fn session(&self, id: i64) -> Result<CloudSession> { |
|
17
|
self.get(&format!("sessions/{id}")).await |
|
18
|
} |
|
19
|
|
|
20
|
/// `POST /api/v1/sessions` |
|
21
|
pub async fn create_session( |
|
22
|
&self, |
|
23
|
title: Option<&str>, |
|
24
|
model: Option<&str>, |
|
25
|
) -> Result<CloudSession> { |
|
26
|
self.post("sessions", &session_body(title, model)).await |
|
27
|
} |
|
28
|
|
|
29
|
/// `PATCH /api/v1/sessions/:id` |
|
30
|
pub async fn update_session( |
|
31
|
&self, |
|
32
|
id: i64, |
|
33
|
title: Option<&str>, |
|
34
|
model: Option<&str>, |
|
35
|
) -> Result<CloudSession> { |
|
36
|
self.patch(&format!("sessions/{id}"), &session_body(title, model)) |
|
37
|
.await |
|
38
|
} |
|
39
|
|
|
40
|
/// `GET /api/v1/models` — the models a session can use. The server lists |
|
41
|
/// them in the OpenAI shape, `{"data": [...]}`; this unwraps it. |
|
42
|
pub async fn models(&self) -> Result<Vec<Model>> { |
|
43
|
#[derive(serde::Deserialize)] |
|
44
|
struct Models { |
|
45
|
data: Vec<Model>, |
|
46
|
} |
|
47
|
let models: Models = self.get("models").await?; |
|
48
|
Ok(models.data) |
|
49
|
} |
|
50
|
|
|
51
|
/// `DELETE /api/v1/sessions/:id` |
|
52
|
pub async fn delete_session(&self, id: i64) -> Result<()> { |
|
53
|
self.delete(&format!("sessions/{id}")).await |
|
54
|
} |
|
55
|
|
|
56
|
/// `POST /api/v1/sessions/:id/messages` — append one message. |
|
57
|
pub async fn append_message( |
|
58
|
&self, |
|
59
|
id: i64, |
|
60
|
role: &str, |
|
61
|
content: &str, |
|
62
|
) -> Result<AppendedMessage> { |
|
63
|
let body = serde_json::json!({ "role": role, "content": content }); |
|
64
|
self.post(&format!("sessions/{id}/messages"), &body).await |
|
65
|
} |
|
66
|
} |
|
67
|
|
|
68
|
/// The server treats absent keys as "leave unchanged", so only send what the |
|
69
|
/// caller actually supplied. |
|
70
|
fn session_body(title: Option<&str>, model: Option<&str>) -> serde_json::Value { |
|
71
|
let mut body = serde_json::Map::new(); |
|
72
|
if let Some(title) = title { |
|
73
|
body.insert("title".into(), title.into()); |
|
74
|
} |
|
75
|
if let Some(model) = model { |
|
76
|
body.insert("model".into(), model.into()); |
|
77
|
} |
|
78
|
serde_json::Value::Object(body) |
|
79
|
} |
|
80
|
|
|
81
|
#[cfg(test)] |
|
82
|
mod tests { |
|
83
|
use super::*; |
|
84
|
|
|
85
|
#[test] |
|
86
|
fn a_model_without_the_default_flag_is_not_the_default() { |
|
87
|
let model: Model = serde_json::from_str( |
|
88
|
r#"{"id":"m","object":"model","owned_by":"sigit","display_name":"M", |
|
89
|
"capability":"fast","vision":false}"#, |
|
90
|
) |
|
91
|
.unwrap(); |
|
92
|
assert!(!model.default); |
|
93
|
} |
|
94
|
|
|
95
|
#[test] |
|
96
|
fn omitted_fields_stay_out_of_the_update_body() { |
|
97
|
let body = session_body(Some("Refactor auth"), None); |
|
98
|
assert_eq!(body["title"], "Refactor auth"); |
|
99
|
assert!(body.get("model").is_none()); |
|
100
|
} |
|
101
|
} |