mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-08-08 01:05:14 +08:00
using stream: schema to fetch in App
This commit is contained in:
36
src-tauri/Cargo.lock
generated
36
src-tauri/Cargo.lock
generated
@@ -1986,6 +1986,7 @@ checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
|
||||
name = "nextchat"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-util",
|
||||
"percent-encoding",
|
||||
"reqwest",
|
||||
@@ -2216,17 +2217,6 @@ dependencies = [
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "os_info"
|
||||
version = "3.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae99c7fa6dd38c7cafe1ec085e804f8f555a2f8659b0dbe03f1f9963a9b51092"
|
||||
dependencies = [
|
||||
"log",
|
||||
"serde",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "overload"
|
||||
version = "0.1.1"
|
||||
@@ -3251,19 +3241,6 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sys-locale"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8a11bd9c338fdba09f7881ab41551932ad42e405f61d01e8406baea71c07aee"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"libc",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
"windows-sys 0.45.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "system-configuration"
|
||||
version = "0.5.1"
|
||||
@@ -3412,7 +3389,6 @@ dependencies = [
|
||||
"objc",
|
||||
"once_cell",
|
||||
"open",
|
||||
"os_info",
|
||||
"percent-encoding",
|
||||
"rand 0.8.5",
|
||||
"raw-window-handle",
|
||||
@@ -3425,7 +3401,6 @@ dependencies = [
|
||||
"serde_repr",
|
||||
"serialize-to-javascript",
|
||||
"state",
|
||||
"sys-locale",
|
||||
"tar",
|
||||
"tauri-macros",
|
||||
"tauri-runtime",
|
||||
@@ -4345,15 +4320,6 @@ dependencies = [
|
||||
"windows-targets 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.52.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
||||
dependencies = [
|
||||
"windows-targets 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.42.2"
|
||||
|
@@ -41,6 +41,7 @@ tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-works
|
||||
percent-encoding = "2.3.1"
|
||||
reqwest = "0.11.18"
|
||||
futures-util = "0.3.30"
|
||||
bytes = "1.7.2"
|
||||
|
||||
[features]
|
||||
# this feature is used for production builds or when `devPath` points to the filesystem and the built-in dev server is disabled.
|
||||
|
@@ -1,57 +1,14 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use futures_util::{StreamExt};
|
||||
use reqwest::Client;
|
||||
use tauri::{ Manager};
|
||||
use tauri::http::{ResponseBuilder};
|
||||
mod stream;
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_window_state::Builder::default().build())
|
||||
.register_uri_scheme_protocol("sse", |app_handle, request| {
|
||||
let path = request.uri().strip_prefix("sse://localhost/").unwrap();
|
||||
let path = percent_encoding::percent_decode(path.as_bytes())
|
||||
.decode_utf8_lossy()
|
||||
.to_string();
|
||||
// println!("path : {}", path);
|
||||
let client = Client::new();
|
||||
let window = app_handle.get_window("main").unwrap();
|
||||
// send http request
|
||||
let body = reqwest::Body::from(request.body().clone());
|
||||
let response_future = client.request(request.method().clone(), path)
|
||||
.headers(request.headers().clone())
|
||||
.body(body).send();
|
||||
|
||||
// get response and emit to client
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let res = response_future.await;
|
||||
|
||||
match res {
|
||||
Ok(res) => {
|
||||
let mut stream = res.bytes_stream();
|
||||
|
||||
while let Some(chunk) = stream.next().await {
|
||||
match chunk {
|
||||
Ok(bytes) => {
|
||||
window.emit("sse-response", bytes).unwrap();
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
window.emit("sse-response", 0).unwrap();
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
});
|
||||
ResponseBuilder::new()
|
||||
.header("Access-Control-Allow-Origin", "*")
|
||||
.status(200).body("OK".into())
|
||||
})
|
||||
.register_uri_scheme_protocol("stream", move |app_handle, request| {
|
||||
stream::stream(app_handle, request)
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
96
src-tauri/src/stream.rs
Normal file
96
src-tauri/src/stream.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
|
||||
use std::error::Error;
|
||||
use futures_util::{StreamExt};
|
||||
use reqwest::Client;
|
||||
use tauri::{ Manager, AppHandle };
|
||||
use tauri::http::{Request, ResponseBuilder};
|
||||
use tauri::http::Response;
|
||||
|
||||
static mut REQUEST_COUNTER: u32 = 0;
|
||||
|
||||
#[derive(Clone, serde::Serialize)]
|
||||
pub struct ErrorPayload {
|
||||
request_id: u32,
|
||||
error: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, serde::Serialize)]
|
||||
pub struct StatusPayload {
|
||||
request_id: u32,
|
||||
status: u16,
|
||||
}
|
||||
|
||||
#[derive(Clone, serde::Serialize)]
|
||||
pub struct HeaderPayload {
|
||||
request_id: u32,
|
||||
name: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, serde::Serialize)]
|
||||
pub struct ChunkPayload {
|
||||
request_id: u32,
|
||||
chunk: bytes::Bytes,
|
||||
}
|
||||
|
||||
pub fn stream(app_handle: &AppHandle, request: &Request) -> Result<Response, Box<dyn Error>> {
|
||||
let mut request_id = 0;
|
||||
let event_name = "stream-response";
|
||||
unsafe {
|
||||
REQUEST_COUNTER += 1;
|
||||
request_id = REQUEST_COUNTER;
|
||||
}
|
||||
let path = request.uri().to_string().replace("stream://localhost/", "").replace("http://stream.localhost/", "");
|
||||
let path = percent_encoding::percent_decode(path.as_bytes())
|
||||
.decode_utf8_lossy()
|
||||
.to_string();
|
||||
// println!("path : {}", path);
|
||||
let client = Client::new();
|
||||
let handle = app_handle.app_handle();
|
||||
// send http request
|
||||
let body = reqwest::Body::from(request.body().clone());
|
||||
let response_future = client.request(request.method().clone(), path)
|
||||
.headers(request.headers().clone())
|
||||
.body(body).send();
|
||||
|
||||
// get response and emit to client
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let res = response_future.await;
|
||||
|
||||
match res {
|
||||
Ok(res) => {
|
||||
handle.emit_all(event_name, StatusPayload{ request_id, status: res.status().as_u16() }).unwrap();
|
||||
for (name, value) in res.headers() {
|
||||
handle.emit_all(event_name, HeaderPayload {
|
||||
request_id,
|
||||
name: name.to_string(),
|
||||
value: std::str::from_utf8(value.as_bytes()).unwrap().to_string()
|
||||
}).unwrap();
|
||||
}
|
||||
let mut stream = res.bytes_stream();
|
||||
|
||||
while let Some(chunk) = stream.next().await {
|
||||
match chunk {
|
||||
Ok(bytes) => {
|
||||
handle.emit_all(event_name, ChunkPayload{ request_id, chunk: bytes }).unwrap();
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
handle.emit_all(event_name, StatusPayload { request_id, status: 0 }).unwrap();
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err.source().expect("REASON").to_string());
|
||||
handle.emit_all(event_name, ErrorPayload {
|
||||
request_id,
|
||||
error: err.source().expect("REASON").to_string()
|
||||
}).unwrap();
|
||||
}
|
||||
}
|
||||
});
|
||||
return ResponseBuilder::new()
|
||||
.header("Access-Control-Allow-Origin", "*")
|
||||
.status(200).body(request_id.to_string().into())
|
||||
}
|
Reference in New Issue
Block a user