Skip to content

Commit b25f820

Browse files
authored
fix: chat history was not show up (#377)
* fix: query chat history * chore: update release notes * chore: update parameter check
1 parent a6205ef commit b25f820

3 files changed

Lines changed: 45 additions & 19 deletions

File tree

docs/content.en/docs/release-notes/_index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Information about release notes of Coco Server is provided here.
2727

2828
- fix: fixed the problem of not being able to search in secondary directories #338
2929
- fix: active shadow setting #354
30+
- fix: chat history was not show up #377
31+
3032

3133
### Improvements
3234

src-tauri/src/assistant/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ pub async fn chat_history<R: Runtime>(
2323
}
2424

2525
if let Some(query) = query {
26+
if !query.is_empty() {
2627
query_params.insert("query".to_string(), query.into());
2728
}
29+
}
2830

2931
let response = HttpClient::get(&server_id, "/chat/_history", Some(query_params))
3032
.await
31-
.map_err(|e| format!("Error get sessions: {}", e))?;
33+
.map_err(|e| {
34+
dbg!("Error get history: {}", &e);
35+
format!("Error get history: {}", e)
36+
})?;
3237

3338
handle_raw_response(response).await?
3439
}

src-tauri/src/server/http_client.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::server::servers::{get_server_by_id, get_server_token};
2-
use http::HeaderName;
2+
use http::{HeaderName, HeaderValue};
33
use once_cell::sync::Lazy;
44
use reqwest::{Client, Method, RequestBuilder};
5-
use serde_json::Value;
65
use std::collections::HashMap;
76
use std::time::Duration;
7+
use tauri_plugin_store::JsonValue;
88
use tokio::sync::Mutex;
99

1010
pub static HTTP_CLIENT: Lazy<Mutex<Client>> = Lazy::new(|| {
@@ -31,25 +31,29 @@ impl HttpClient {
3131
pub async fn send_raw_request(
3232
method: Method,
3333
url: &str,
34-
query_params: Option<HashMap<String, Value>>,
34+
query_params: Option<HashMap<String, JsonValue>>,
3535
headers: Option<HashMap<String, String>>,
3636
body: Option<reqwest::Body>,
3737
) -> Result<reqwest::Response, String> {
3838
let request_builder =
3939
Self::get_request_builder(method, url, headers, query_params, body).await;
4040

41+
4142
let response = request_builder
4243
.send()
4344
.await
44-
.map_err(|e| format!("Failed to send request: {}", e))?;
45+
.map_err(|e| {
46+
dbg!("Failed to send request: {}", &e);
47+
format!("Failed to send request: {}", e)
48+
})?;
4549
Ok(response)
4650
}
4751

4852
pub async fn get_request_builder(
4953
method: Method,
5054
url: &str,
5155
headers: Option<HashMap<String, String>>,
52-
query_params: Option<HashMap<String, Value>>, // Add query parameters
56+
query_params: Option<HashMap<String, JsonValue>>, // Add query parameters
5357
body: Option<reqwest::Body>,
5458
) -> RequestBuilder {
5559
let client = HTTP_CLIENT.lock().await; // Acquire the lock on HTTP_CLIENT
@@ -60,17 +64,32 @@ impl HttpClient {
6064
if let Some(h) = headers {
6165
let mut req_headers = reqwest::header::HeaderMap::new();
6266
for (key, value) in h.into_iter() {
63-
let _ = req_headers.insert(
64-
HeaderName::from_bytes(key.as_bytes()).unwrap(),
65-
reqwest::header::HeaderValue::from_str(&value).unwrap(),
66-
);
67+
68+
match (
69+
HeaderName::from_bytes(key.as_bytes()),
70+
HeaderValue::from_str(value.trim()),
71+
) {
72+
(Ok(name), Ok(val)) => {
73+
req_headers.insert(name, val);
74+
}
75+
(Err(e), _) => {
76+
eprintln!("Invalid header name: {:?}", key);
77+
}
78+
(_, Err(e)) => {
79+
eprintln!("Invalid header value for {}: {:?}", key, value);
80+
}
81+
}
6782
}
6883
request_builder = request_builder.headers(req_headers);
6984
}
7085

7186
if let Some(query) = query_params {
87+
let query: HashMap<String, String> = query.into_iter()
88+
.map(|(k, v)| (k, v.to_string()))
89+
.collect();
7290
request_builder = request_builder.query(&query);
7391
}
92+
7493
// Add body if present
7594
if let Some(b) = body {
7695
request_builder = request_builder.body(b);
@@ -84,7 +103,7 @@ impl HttpClient {
84103
method: Method,
85104
path: &str,
86105
custom_headers: Option<HashMap<String, String>>,
87-
query_params: Option<HashMap<String, Value>>,
106+
query_params: Option<HashMap<String, JsonValue>>,
88107
body: Option<reqwest::Body>,
89108
) -> Result<reqwest::Response, String> {
90109
// Fetch the server using the server_id
@@ -123,7 +142,7 @@ impl HttpClient {
123142
pub async fn get(
124143
server_id: &str,
125144
path: &str,
126-
query_params: Option<HashMap<String, Value>>, // Add query parameters
145+
query_params: Option<HashMap<String, JsonValue>>, // Add query parameters
127146
) -> Result<reqwest::Response, String> {
128147
HttpClient::send_request(server_id, Method::GET, path, None, query_params, None).await
129148
}
@@ -132,7 +151,7 @@ impl HttpClient {
132151
pub async fn post(
133152
server_id: &str,
134153
path: &str,
135-
query_params: Option<HashMap<String, Value>>, // Add query parameters
154+
query_params: Option<HashMap<String, JsonValue>>, // Add query parameters
136155
body: Option<reqwest::Body>,
137156
) -> Result<reqwest::Response, String> {
138157
HttpClient::send_request(server_id, Method::POST, path, None, query_params, body).await
@@ -142,7 +161,7 @@ impl HttpClient {
142161
server_id: &str,
143162
path: &str,
144163
custom_headers: Option<HashMap<String, String>>,
145-
query_params: Option<HashMap<String, Value>>, // Add query parameters
164+
query_params: Option<HashMap<String, JsonValue>>, // Add query parameters
146165
body: Option<reqwest::Body>,
147166
) -> Result<reqwest::Response, String> {
148167
HttpClient::send_request(
@@ -153,7 +172,7 @@ impl HttpClient {
153172
query_params,
154173
body,
155174
)
156-
.await
175+
.await
157176
}
158177

159178
// Convenience method for PUT requests
@@ -162,7 +181,7 @@ impl HttpClient {
162181
server_id: &str,
163182
path: &str,
164183
custom_headers: Option<HashMap<String, String>>,
165-
query_params: Option<HashMap<String, Value>>, // Add query parameters
184+
query_params: Option<HashMap<String, JsonValue>>, // Add query parameters
166185
body: Option<reqwest::Body>,
167186
) -> Result<reqwest::Response, String> {
168187
HttpClient::send_request(
@@ -173,7 +192,7 @@ impl HttpClient {
173192
query_params,
174193
body,
175194
)
176-
.await
195+
.await
177196
}
178197

179198
// Convenience method for DELETE requests
@@ -182,7 +201,7 @@ impl HttpClient {
182201
server_id: &str,
183202
path: &str,
184203
custom_headers: Option<HashMap<String, String>>,
185-
query_params: Option<HashMap<String, Value>>, // Add query parameters
204+
query_params: Option<HashMap<String, JsonValue>>, // Add query parameters
186205
) -> Result<reqwest::Response, String> {
187206
HttpClient::send_request(
188207
server_id,
@@ -192,6 +211,6 @@ impl HttpClient {
192211
query_params,
193212
None,
194213
)
195-
.await
214+
.await
196215
}
197216
}

0 commit comments

Comments
 (0)