-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.rs
More file actions
346 lines (286 loc) · 9.53 KB
/
client.rs
File metadata and controls
346 lines (286 loc) · 9.53 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::io::{BufRead, BufReader, Read, Write};
use std::net::TcpStream;
use std::path::Path;
#[derive(Debug, Serialize)]
struct SearchRequest {
query: String,
path: Option<String>,
max_results: usize,
}
#[derive(Debug, Serialize)]
struct EmbedBatchRequest {
texts: Vec<String>,
}
#[derive(Debug, Deserialize)]
pub struct EmbedBatchResponse {
pub embeddings: Vec<Vec<f32>>,
#[allow(dead_code)]
pub count: usize,
}
#[derive(Debug, Deserialize)]
pub struct SearchResponse {
pub results: Vec<SearchResultJson>,
pub query: String,
pub total: usize,
}
#[derive(Debug, Deserialize)]
pub struct SearchResultJson {
pub path: String,
pub score: f32,
pub score_percent: String,
pub preview: Option<String>,
pub start_line: i32,
pub end_line: i32,
}
pub struct Client {
base_url: String,
}
impl Client {
pub fn new(host: &str, port: u16) -> Self {
Self {
base_url: format!("http://{}:{}", host, port),
}
}
pub fn search(
&self,
query: &str,
path: Option<&Path>,
max_results: usize,
) -> Result<SearchResponse> {
let request = SearchRequest {
query: query.to_string(),
path: path.map(|p| p.to_string_lossy().to_string()),
max_results,
};
let body = serde_json::to_string(&request)?;
// Simple HTTP POST using TCP (avoiding extra dependencies)
let host_port = self.base_url.trim_start_matches("http://");
let mut stream = TcpStream::connect(host_port)
.context("Failed to connect to vgrep server. Is it running? Start with: vgrep serve")?;
let request = format!(
"POST /search HTTP/1.1\r\n\
Host: {}\r\n\
Content-Type: application/json\r\n\
Content-Length: {}\r\n\
Connection: close\r\n\
\r\n\
{}",
host_port,
body.len(),
body
);
stream.write_all(request.as_bytes())?;
stream.flush()?;
let mut reader = BufReader::new(stream);
let response = read_http_response(&mut reader)?;
if !(200..=299).contains(&response.status_code) {
return Err(server_error_from_response(&response));
}
let search_response: SearchResponse =
serde_json::from_str(&response.body).context("Failed to parse server response")?;
Ok(search_response)
}
pub fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>> {
let request = EmbedBatchRequest {
texts: texts.iter().map(|s| s.to_string()).collect(),
};
let body = serde_json::to_string(&request)?;
let host_port = self.base_url.trim_start_matches("http://");
let mut stream = TcpStream::connect(host_port)
.context("Failed to connect to vgrep server. Is it running? Start with: vgrep serve")?;
let http_request = format!(
"POST /embed_batch HTTP/1.1\r\n\
Host: {}\r\n\
Content-Type: application/json\r\n\
Content-Length: {}\r\n\
Connection: close\r\n\
\r\n\
{}",
host_port,
body.len(),
body
);
stream.write_all(http_request.as_bytes())?;
stream.flush()?;
let mut reader = BufReader::new(stream);
let response = read_http_response(&mut reader)?;
if !(200..=299).contains(&response.status_code) {
return Err(server_error_from_response(&response));
}
let embed_response: EmbedBatchResponse =
serde_json::from_str(&response.body).context("Failed to parse server response")?;
Ok(embed_response.embeddings)
}
pub fn health(&self) -> Result<bool> {
let host_port = self.base_url.trim_start_matches("http://");
match TcpStream::connect(host_port) {
Ok(mut stream) => {
let request = format!(
"GET /health HTTP/1.1\r\n\
Host: {}\r\n\
Connection: close\r\n\
\r\n",
host_port
);
if stream.write_all(request.as_bytes()).is_ok() {
return Ok(true);
}
Ok(false)
}
Err(_) => Ok(false),
}
}
}
#[derive(Debug)]
struct HttpResponse {
status_code: u16,
status_line: String,
body: String,
}
fn read_http_response(reader: &mut BufReader<TcpStream>) -> Result<HttpResponse> {
let mut status_line = String::new();
reader
.read_line(&mut status_line)
.context("Failed to read HTTP status line")?;
if status_line.is_empty() {
anyhow::bail!("Empty response from server");
}
let status_line = status_line
.trim_end_matches(['\r', '\n'].as_ref())
.to_string();
let status_code = parse_http_status_code(&status_line)?;
// Read headers
loop {
let mut line = String::new();
reader
.read_line(&mut line)
.context("Failed to read HTTP header")?;
if line == "\r\n" || line.is_empty() {
break;
}
}
// Read body
let mut body = String::new();
reader
.read_to_string(&mut body)
.context("Failed to read HTTP response body")?;
Ok(HttpResponse {
status_code,
status_line,
body,
})
}
fn parse_http_status_code(status_line: &str) -> Result<u16> {
let mut parts = status_line.split_whitespace();
let http_version = parts
.next()
.context("Invalid HTTP status line: missing version")?;
let code_str = parts
.next()
.context("Invalid HTTP status line: missing status code")?;
if !http_version.starts_with("HTTP/") {
anyhow::bail!("Invalid HTTP status line: {status_line}");
}
let code: u16 = code_str
.parse()
.with_context(|| format!("Invalid HTTP status code in status line: {status_line}"))?;
Ok(code)
}
fn server_error_from_response(response: &HttpResponse) -> anyhow::Error {
let body_trimmed = response.body.trim();
if let Ok(value) = serde_json::from_str::<serde_json::Value>(&response.body) {
if let Some(error) = value.get("error").and_then(|v| v.as_str()) {
return anyhow::anyhow!("Server returned HTTP {}: {}", response.status_code, error);
}
}
if body_trimmed.is_empty() {
return anyhow::anyhow!(
"Server returned HTTP {} ({}) with empty body",
response.status_code,
response.status_line
);
}
anyhow::anyhow!(
"Server returned HTTP {} ({}): {}",
response.status_code,
response.status_line,
truncate_for_error(body_trimmed, 500)
)
}
fn truncate_for_error(s: &str, max_len: usize) -> String {
if s.len() <= max_len {
return s.to_string();
}
let mut end = max_len;
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
let mut out = s[..end].to_string();
out.push_str("...");
out
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::{Read, Write};
use std::net::TcpListener;
use std::thread;
fn spawn_stub_server(response: String) -> (u16, thread::JoinHandle<()>) {
let listener = TcpListener::bind(("127.0.0.1", 0)).expect("bind stub server");
let port = listener
.local_addr()
.expect("stub server local addr")
.port();
let handle = thread::spawn(move || {
let (mut stream, _) = listener.accept().expect("accept stub connection");
// Read and ignore the request.
let mut buf = [0u8; 4096];
let _ = stream.read(&mut buf);
stream
.write_all(response.as_bytes())
.expect("write stub response");
let _ = stream.flush();
});
(port, handle)
}
#[test]
fn search_includes_http_status_on_error() {
let body = r#"{"error":"bad request"}"#;
let response = format!(
"HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
body.len(),
body
);
let (port, handle) = spawn_stub_server(response);
let client = Client::new("127.0.0.1", port);
let err = client.search("q", None, 10).unwrap_err();
let msg = err.to_string();
assert!(msg.contains("HTTP 400"), "unexpected error message: {msg}");
assert!(
msg.contains("bad request"),
"unexpected error message: {msg}"
);
handle.join().expect("stub server thread join");
}
#[test]
fn embed_batch_includes_http_status_on_error() {
let body = "internal error";
let response = format!(
"HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/plain\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
body.len(),
body
);
let (port, handle) = spawn_stub_server(response);
let client = Client::new("127.0.0.1", port);
let err = client.embed_batch(&["hello"]).unwrap_err();
let msg = err.to_string();
assert!(msg.contains("HTTP 500"), "unexpected error message: {msg}");
assert!(
msg.contains("internal error"),
"unexpected error message: {msg}"
);
handle.join().expect("stub server thread join");
}
}