deps: use libcurl

This commit is contained in:
iff 2024-11-22 10:49:24 +01:00
parent 98a00674f3
commit c97d6440f3
4 changed files with 152 additions and 29 deletions

107
Cargo.lock generated
View file

@ -45,6 +45,15 @@ dependencies = [
"serde",
]
[[package]]
name = "cc"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47"
dependencies = [
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -86,6 +95,36 @@ version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
[[package]]
name = "curl"
version = "0.4.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9fb4d13a1be2b58f14d60adba57c9834b78c62fd86c3e76a148f732686e9265"
dependencies = [
"curl-sys",
"libc",
"openssl-probe",
"openssl-sys",
"schannel",
"socket2",
"windows-sys 0.52.0",
]
[[package]]
name = "curl-sys"
version = "0.4.78+curl-8.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8eec768341c5c7789611ae51cf6c459099f22e64a5d5d0ce4892434e33821eaf"
dependencies = [
"cc",
"libc",
"libz-sys",
"openssl-sys",
"pkg-config",
"vcpkg",
"windows-sys 0.52.0",
]
[[package]]
name = "either"
version = "1.13.0"
@ -226,6 +265,18 @@ version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64804cc6a5042d4f05379909ba25b503ec04e2c082151d62122d5dcaa274b961"
[[package]]
name = "libz-sys"
version = "1.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2d16453e800a8cf6dd2fc3eb4bc99b786a9b90c663b8559a5b1a041bf89e472"
dependencies = [
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "linux-raw-sys"
version = "0.3.8"
@ -265,11 +316,30 @@ version = "1.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
[[package]]
name = "openssl-probe"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-sys"
version = "0.9.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45abf306cbf99debc8195b66b7346498d7b10c210de50418b5ccd7ceba08c741"
dependencies = [
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "pay-respects"
version = "0.5.12"
dependencies = [
"colored",
"curl",
"pay-respects-parser",
"regex-lite",
"rust-i18n",
@ -293,6 +363,12 @@ dependencies = [
"toml 0.8.19",
]
[[package]]
name = "pkg-config"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
[[package]]
name = "proc-macro2"
version = "1.0.91"
@ -442,6 +518,15 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "schannel"
version = "0.1.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f29ebaa345f945cec9fbbc532eb307f0fdad8161f281b6369539c8d84876b3d"
dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "serde"
version = "1.0.215"
@ -500,6 +585,12 @@ dependencies = [
"tempfile",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "siphasher"
version = "1.0.1"
@ -518,6 +609,16 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c"
[[package]]
name = "socket2"
version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c"
dependencies = [
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
@ -678,6 +779,12 @@ version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "walkdir"
version = "2.5.0"

View file

@ -16,14 +16,15 @@ include = [
]
[dependencies]
colored = "2.0"
sys-locale = "0.3.2"
colored = "2"
sys-locale = "0.3"
rust-i18n = "3"
regex-lite = "0.1"
toml = { version = "0.8", optional = true }
serde_json = { version = "1.0", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
curl = { version = "0.4", optional = true }
textwrap = { version = "0.16", features = ["terminal_size"], optional = true}
pay-respects-parser = "0.2.6"
@ -32,7 +33,7 @@ pay-respects-parser = "0.2.6"
[features]
runtime-rules = ["dep:serde", "dep:toml"]
request-ai = ["dep:serde", "dep:serde_json", "dep:textwrap"]
request-ai = ["dep:serde", "dep:serde_json", "dep:curl", "dep:textwrap"]
[profile.release]
strip = true

View file

@ -63,7 +63,7 @@ fn main() {
}
};
let mut last_command = shell::last_command(&shell);
let mut last_command = shell::last_command(&shell).trim().to_string();
last_command = shell::expand_alias(&shell, &last_command);
let mut error_msg = command_output(&shell, &last_command);
error_msg = error_msg

View file

@ -1,8 +1,12 @@
use std::collections::HashMap;
use std::io::Read;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use curl::easy::Easy as Curl;
use curl::easy::List;
#[derive(Serialize, Deserialize)]
struct Input {
role: String,
@ -97,32 +101,43 @@ The command `{last_command}` returns the following error message: `{error_msg}`.
model,
};
let res = std::process::Command::new("curl")
.arg("-X")
.arg("POST")
.arg("-H")
.arg(format!("Authorization: Bearer {}", api_key))
.arg("-H")
.arg("Content-Type: application/json")
.arg("-d")
.arg(serde_json::to_string(&messages).unwrap())
.arg(request_url)
.output();
let str_json = serde_json::to_string(&messages).unwrap();
let mut data = str_json.as_bytes();
let res = match res {
Ok(res) => res.stdout,
Err(_) => {
return None;
}
};
let mut dst = Vec::new();
let mut handle = Curl::new();
let json: Value = {
let json = serde_json::from_str(std::str::from_utf8(&res).unwrap());
if json.is_err() {
return None;
handle.url(&request_url).unwrap();
handle.post(true).unwrap();
handle.post_field_size(data.len() as u64).unwrap();
let mut headers = List::new();
headers
.append(&format!("Authorization: Bearer {}", api_key))
.unwrap();
headers.append("Content-Type: application/json").unwrap();
handle.http_headers(headers).unwrap();
{
let mut transfer = handle.transfer();
transfer
.read_function(|buf| Ok(data.read(buf).unwrap_or(0)))
.unwrap();
transfer
.write_function(|buf| {
dst.extend_from_slice(buf);
Ok(buf.len())
})
.unwrap();
transfer.perform().expect("Failed to perform request");
}
json.unwrap()
};
let res = String::from_utf8(dst).unwrap();
let json: Value = serde_json::from_str(&res).unwrap();
let content = &json["choices"][0]["message"]["content"];
@ -130,7 +145,7 @@ The command `{last_command}` returns the following error message: `{error_msg}`.
let str = {
let str = content.as_str();
str?;
str.unwrap()
str.expect("Failed to get content from response")
.trim_start_matches("```")
.trim_end_matches("```")
};