Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add request support (sharedarraybuffer) #495

Merged
merged 20 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions apps/shinkai-desktop/src-tauri/src/commands/fetch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use serde::Serialize;
use std::collections::HashMap;
use reqwest::header::HeaderMap;
use serde_json;

#[derive(Serialize)]
pub struct FetchResponse {
status: u16,
headers: HashMap<String, Vec<String>>,
body: String,
}

/// Converts a `HeaderMap` to a `HashMap<String, Vec<String>>`.
pub fn header_map_to_hashmap(headers: &HeaderMap) -> HashMap<String, Vec<String>> {
headers.iter().fold(HashMap::new(), |mut acc, (key, value)| {
let key_str = key.to_string();
let value_str = value.to_str().unwrap_or("").to_string();
acc.entry(key_str)
.or_default()
.push(value_str);
acc
})
}

#[tauri::command]
pub async fn get_request(url: String, custom_headers: String) -> Result<FetchResponse, String> {
log::debug!("get_request called with url: {}", url);
log::debug!("get_request called with custom_headers: {}", custom_headers);
println!("get_request called with url: {}", url);
eprintln!("get_request");

// Deserialize the JSON string into a HashMap
let custom_headers: HashMap<String, String> = serde_json::from_str(&custom_headers)
.map_err(|e| e.to_string())?;

// Create a client
let client = reqwest::Client::new();

// Convert custom headers to HeaderMap
let mut headers = HeaderMap::new();
for (key, value) in custom_headers {
let header_name = key.parse::<reqwest::header::HeaderName>().unwrap();
let header_value = value.parse::<reqwest::header::HeaderValue>().unwrap();
headers.insert(header_name, header_value);
}

// Perform the HTTP GET request with headers
let response = client.get(&url)
.headers(headers)
.send()
.await
.map_err(|e| e.to_string())?;

// Extract the status code
let status = response.status().as_u16();

// Convert response headers to a serializable HashMap
let response_headers = header_map_to_hashmap(response.headers());

// Extract the response body as text
let body = response.text().await.map_err(|e| e.to_string())?;

// Construct the FetchResponse
Ok(FetchResponse { status, headers: response_headers, body })
}

#[tauri::command]
pub async fn post_request(url: String, custom_headers: String, body: String) -> Result<FetchResponse, String> {
log::debug!("post_request called with url: {}", url);
println!("post_request called with url: {}", url);
eprintln!("posting data");

// Deserialize the JSON string into a HashMap
let custom_headers: HashMap<String, String> = serde_json::from_str(&custom_headers)
.map_err(|e| e.to_string())?;

// Create a client
let client = reqwest::Client::new();

// Convert custom headers to HeaderMap
let mut headers = HeaderMap::new();
for (key, value) in custom_headers {
let header_name = key.parse::<reqwest::header::HeaderName>().unwrap();
let header_value = value.parse::<reqwest::header::HeaderValue>().unwrap();
headers.insert(header_name, header_value);
}

// Perform the HTTP POST request with headers and body
let response = client.post(&url)
.headers(headers)
.body(body)
.send()
.await
.map_err(|e| e.to_string())?;

// Extract the status code
let status = response.status().as_u16();

// Convert response headers to a serializable HashMap
let response_headers = header_map_to_hashmap(response.headers());

// Extract the response body as text
let response_body = response.text().await.map_err(|e| e.to_string())?;

// Construct the FetchResponse
Ok(FetchResponse { status, headers: response_headers, body: response_body })
}
1 change: 1 addition & 0 deletions apps/shinkai-desktop/src-tauri/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod galxe;
pub mod hardware;
pub mod shinkai_node_manager_commands;
pub mod spotlight_commands;
pub mod fetch;
3 changes: 3 additions & 0 deletions apps/shinkai-desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use std::sync::Arc;

use crate::commands::fetch::{get_request, post_request};
use crate::commands::galxe::galxe_generate_proof;
use crate::commands::hardware::hardware_get_summary;
use crate::commands::shinkai_node_manager_commands::{
Expand Down Expand Up @@ -85,6 +86,8 @@ fn main() {
shinkai_node_get_default_model,
hardware_get_summary,
galxe_generate_proof,
get_request,
post_request,
])
.setup(|app| {
let app_resource_dir = app.path().resource_dir()?;
Expand Down
2 changes: 1 addition & 1 deletion apps/shinkai-desktop/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"app": {
"withGlobalTauri": false,
"security": {
"csp": null
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost; cross-origin-embedder-policy 'require-corp'; cross-origin-opener-policy 'same-origin'"
},
"windows": [
{
Expand Down
4 changes: 4 additions & 0 deletions apps/shinkai-desktop/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export default defineConfig(() => ({
// Exclude output.wav from being watched
ignored: ['**/output.wav'],
},
headers: {
'Cross-Origin-Embedder-Policy': 'require-corp',
'Cross-Origin-Opener-Policy': 'same-origin',
}
},
// 3. to make use of `TAURI_DEBUG` and other env variables
// https://tauri.studio/v1/api/config#buildconfig.beforedevcommand
Expand Down
Loading