Skip to content

Commit d385745

Browse files
committed
ci fix
1 parent d6f56a0 commit d385745

File tree

17 files changed

+225
-214
lines changed

17 files changed

+225
-214
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[workspace]
22
resolver = "2"
33

4-
members = [ "golem-llm/llm", "golem-llm/llm-anthropic", "golem-llm/llm-grok", "golem-llm/llm-ollama", "golem-llm/llm-openai", "golem-llm/llm-openrouter", "golem-web-search/web-search", "golem-web-search/websearch-brave", "golem-web-search/websearch-google", "golem-web-search/websearch-serper", "golem-web-search/websearch-tavily"]
4+
members = [ "llm", "llm-anthropic", "llm-grok", "llm-ollama", "llm-openai", "llm-openrouter", "golem-web-search/web-search", "golem-web-search/websearch-brave", "golem-web-search/websearch-google", "golem-web-search/websearch-serper", "golem-web-search/websearch-tavily"]
55

66
[profile.release]
77
debug = false

golem-web-search/web-search/src/config.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use crate::exports::golem::web_search::web_search::{SearchError};
1+
use crate::exports::golem::web_search::web_search::SearchError;
22
use std::{collections::HashMap, ffi::OsStr};
33

44
/// Gets an expected configuration value from the environment, and fails if its is not found
55
/// using the `fail` function. Otherwise, it runs `succeed` with the configuration value.
66
pub fn with_config_key<R>(
77
keys: &[impl AsRef<OsStr>],
88
fail: impl FnOnce(SearchError) -> R,
9-
succeed: impl FnOnce(HashMap<String,String>) -> R,
9+
succeed: impl FnOnce(HashMap<String, String>) -> R,
1010
) -> R {
1111
let mut hashmap = HashMap::new();
1212
for key in keys {
@@ -15,8 +15,11 @@ pub fn with_config_key<R>(
1515
hashmap.insert(key.as_ref().to_string_lossy().to_string(), value);
1616
}
1717
Err(_) => {
18-
let error = SearchError::BackendError(format!("Missing config key: {}", key.as_ref().to_string_lossy()));
19-
return fail(error);
18+
let error = SearchError::BackendError(format!(
19+
"Missing config key: {}",
20+
key.as_ref().to_string_lossy()
21+
));
22+
return fail(error);
2023
}
2124
}
2225
}

golem-web-search/web-search/src/durability.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ mod durable_impl {
7676
fn search_once(
7777
params: SearchParams,
7878
) -> Result<(Vec<SearchResult>, Option<SearchMetadata>), SearchError> {
79-
let durability = Durability::<(Vec<SearchResult>, Option<SearchMetadata>), UnusedError>::new(
80-
"golem_web_search",
81-
"search_once",
82-
DurableFunctionType::WriteRemote,
83-
);
79+
let durability =
80+
Durability::<(Vec<SearchResult>, Option<SearchMetadata>), UnusedError>::new(
81+
"golem_web_search",
82+
"search_once",
83+
DurableFunctionType::WriteRemote,
84+
);
8485
if durability.is_live() {
8586
let result = with_persistence_level(PersistenceLevel::PersistNothing, || {
8687
Impl::search_once(params.clone())
@@ -108,4 +109,4 @@ mod durable_impl {
108109
write!(f, "UnusedError")
109110
}
110111
}
111-
}
112+
}

golem-web-search/web-search/src/error.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
use crate::exports::golem::web_search::web_search::SearchError;
32

43
/// Creates an `Error` value representing that something is unsuported
@@ -9,4 +8,3 @@ pub fn unsupported(what: impl AsRef<str>) -> SearchError {
98
pub fn from_reqwest_error(details: impl AsRef<str>, err: reqwest::Error) -> SearchError {
109
SearchError::BackendError(format!("{}: {err}", details.as_ref()))
1110
}
12-

golem-web-search/web-search/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ pub mod config;
22
pub mod durability;
33
pub mod error;
44

5-
65
wit_bindgen::generate!({
76
path: "../wit",
87
world: "web-search-library",
@@ -26,9 +25,10 @@ impl LoggingState {
2625
pub fn init(&mut self) {
2726
if !self.logging_initialized {
2827
let _ = wasi_logger::Logger::install();
29-
let max_level: log::LevelFilter =
30-
log::LevelFilter::from_str(&std::env::var("GOLEM_WEB_SEARCH_LOG").unwrap_or_default())
31-
.unwrap_or(log::LevelFilter::Info);
28+
let max_level: log::LevelFilter = log::LevelFilter::from_str(
29+
&std::env::var("GOLEM_WEB_SEARCH_LOG").unwrap_or_default(),
30+
)
31+
.unwrap_or(log::LevelFilter::Info);
3232
log::set_max_level(max_level);
3333
self.logging_initialized = true;
3434
}

golem-web-search/websearch-brave/src/client.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
use golem_web_search::error::from_reqwest_error;
32
use golem_web_search::golem::web_search::types::SearchError;
43
use log::trace;
@@ -20,10 +19,7 @@ impl BraveSearchApi {
2019
Self { api_key, client }
2120
}
2221

23-
pub fn search(
24-
&self,
25-
request: BraveSearchRequest,
26-
) -> Result<BraveSearchResponse, SearchError> {
22+
pub fn search(&self, request: BraveSearchRequest) -> Result<BraveSearchResponse, SearchError> {
2723
trace!("Sending request to Brave Search API: {request:?}");
2824

2925
let response: Response = self
@@ -372,26 +368,28 @@ pub struct BraveMixedItem {
372368
fn parse_response(response: Response) -> Result<BraveSearchResponse, SearchError> {
373369
match response.status() {
374370
StatusCode::OK => {
375-
let body = response.text().map_err(|e| SearchError::BackendError(format!(
376-
"Failed to read response body: {}", e
377-
)))?;
371+
let body = response.text().map_err(|e| {
372+
SearchError::BackendError(format!("Failed to read response body: {}", e))
373+
})?;
378374
match serde_json::from_str::<BraveSearchResponse>(&body) {
379375
Ok(parsed) => Ok(parsed),
380376
Err(e) => Err(SearchError::BackendError(format!(
381377
"Failed to parse response: {} \nRaw body: {}",
382378
e, body
383379
))),
384380
}
385-
},
381+
}
386382
StatusCode::TOO_MANY_REQUESTS => Err(SearchError::RateLimited(60)),
387383
StatusCode::BAD_REQUEST => Err(SearchError::InvalidQuery),
388384
_ => {
389385
let status = response.status();
390-
let body = response.text().unwrap_or_else(|_| "<failed to read body>".into());
386+
let body = response
387+
.text()
388+
.unwrap_or_else(|_| "<failed to read body>".into());
391389
Err(SearchError::BackendError(format!(
392390
"Request failed: {} \nRaw body: {}",
393391
status, body
394392
)))
395-
},
393+
}
396394
}
397-
}
395+
}

golem-web-search/websearch-brave/src/conversions.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use golem_web_search::golem::web_search::types::{
55
ImageResult, SafeSearchLevel, SearchMetadata, SearchParams, SearchResult, TimeRange,
66
};
77

8-
pub fn convert_params_to_request(
9-
params: &SearchParams,
10-
offset: Option<u32>,
11-
) -> BraveSearchRequest {
8+
pub fn convert_params_to_request(params: &SearchParams, offset: Option<u32>) -> BraveSearchRequest {
129
let mut request = BraveSearchRequest {
1310
q: params.query.clone(),
1411
country: params.region.clone(),
@@ -98,13 +95,17 @@ pub fn convert_response_to_results(
9895
}
9996
}
10097

101-
102-
10398
let total_results = response
10499
.query
105100
.as_ref()
106101
.and_then(|q| q.more_results_available)
107-
.map(|has_more| if has_more { 1000u64 } else { results.len() as u64 });
102+
.map(|has_more| {
103+
if has_more {
104+
1000u64
105+
} else {
106+
results.len() as u64
107+
}
108+
});
108109

109110
let next_page_token = response
110111
.query
@@ -120,7 +121,7 @@ pub fn convert_response_to_results(
120121
query: params.query.clone(),
121122
total_results,
122123
search_time_ms: None,
123-
safe_search: params.safe_search.clone(),
124+
safe_search: params.safe_search,
124125
language: params.language.clone(),
125126
region: params.region.clone(),
126127
next_page_token,
@@ -273,10 +274,7 @@ mod tests {
273274
let params = create_test_params();
274275
let request = convert_params_to_request(&params, Some(20));
275276

276-
assert_eq!(
277-
request.q,
278-
"test query (site:example.com OR site:test.org)"
279-
);
277+
assert_eq!(request.q, "test query (site:example.com OR site:test.org)");
280278
assert_eq!(request.country, Some("us".to_string()));
281279
assert_eq!(request.search_lang, Some("en".to_string()));
282280
assert_eq!(request.ui_lang, Some("en".to_string()));
@@ -456,4 +454,4 @@ mod tests {
456454
assert_eq!(meta.search_time_ms, None);
457455
assert_eq!(meta.next_page_token, None);
458456
}
459-
}
457+
}

golem-web-search/websearch-brave/src/lib.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use golem_web_search::durability::DurableWebSearch;
66
use golem_web_search::golem::web_search::types::{
77
SearchError, SearchMetadata, SearchParams, SearchResult,
88
};
9-
use golem_web_search::golem_web_search::web_search::web_search::{Guest, GuestSearchSession, SearchSession};
9+
use golem_web_search::golem_web_search::web_search::web_search::{
10+
Guest, GuestSearchSession, SearchSession,
11+
};
1012

1113
use golem_web_search::LOGGING_STATE;
1214
use std::cell::RefCell;
@@ -87,34 +89,26 @@ impl Guest for BraveWebSearchComponent {
8789
fn start_search(params: SearchParams) -> Result<SearchSession, SearchError> {
8890
LOGGING_STATE.with_borrow_mut(|state| state.init());
8991

90-
with_config_key(
91-
&[Self::API_KEY_ENV_VAR],
92-
|e| Err(e),
93-
|keys| {
94-
let api_key = keys.get(Self::API_KEY_ENV_VAR).unwrap().to_owned();
95-
let client = BraveSearchApi::new(api_key);
96-
Ok(SearchSession::new(BraveSearchSession::new(client, params)))
97-
},
98-
)
92+
with_config_key(&[Self::API_KEY_ENV_VAR], Err, |keys| {
93+
let api_key = keys.get(Self::API_KEY_ENV_VAR).unwrap().to_owned();
94+
let client = BraveSearchApi::new(api_key);
95+
Ok(SearchSession::new(BraveSearchSession::new(client, params)))
96+
})
9997
}
10098

10199
fn search_once(
102100
params: SearchParams,
103101
) -> Result<(Vec<SearchResult>, Option<SearchMetadata>), SearchError> {
104102
LOGGING_STATE.with_borrow_mut(|state| state.init());
105103

106-
with_config_key(
107-
&[Self::API_KEY_ENV_VAR],
108-
|e| Err(e),
109-
|keys| {
110-
let api_key = keys.get(Self::API_KEY_ENV_VAR).unwrap().to_owned();
111-
let client = BraveSearchApi::new(api_key);
112-
let request = convert_params_to_request(&params, None);
113-
let response = client.search(request)?;
114-
let (results, metadata) = convert_response_to_results(response, &params);
115-
Ok((results, metadata))
116-
},
117-
)
104+
with_config_key(&[Self::API_KEY_ENV_VAR], Err, |keys| {
105+
let api_key = keys.get(Self::API_KEY_ENV_VAR).unwrap().to_owned();
106+
let client = BraveSearchApi::new(api_key);
107+
let request = convert_params_to_request(&params, None);
108+
let response = client.search(request)?;
109+
let (results, metadata) = convert_response_to_results(response, &params);
110+
Ok((results, metadata))
111+
})
118112
}
119113
}
120114

golem-web-search/websearch-google/src/client.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl GoogleSearchApi {
4444
}
4545
}
4646

47-
#[derive(Debug, Serialize)]
47+
#[derive(Debug, Serialize, Deserialize, Clone)]
4848
pub struct GoogleSearchRequest {
4949
pub q: String,
5050
pub cx: String,
@@ -70,56 +70,68 @@ pub struct GoogleSearchRequest {
7070
pub site_search_filter: Option<String>,
7171
}
7272

73-
#[derive(Debug, Deserialize)]
73+
#[derive(Debug, Serialize, Deserialize, Clone)]
7474
pub struct GoogleSearchResponse {
75+
#[serde(skip_serializing_if = "Option::is_none")]
7576
pub items: Option<Vec<GoogleSearchItem>>,
77+
#[serde(skip_serializing_if = "Option::is_none")]
7678
#[serde(rename = "searchInformation")]
7779
pub search_information: Option<GoogleSearchInformation>,
80+
#[serde(skip_serializing_if = "Option::is_none")]
7881
pub queries: Option<GoogleQueries>,
82+
#[serde(skip_serializing_if = "Option::is_none")]
7983
pub error: Option<GoogleError>,
8084
}
8185

82-
#[derive(Debug, Deserialize)]
86+
#[derive(Debug, Serialize, Deserialize, Clone)]
8387
pub struct GoogleSearchItem {
8488
pub title: String,
8589
pub link: String,
8690
pub snippet: String,
91+
#[serde(skip_serializing_if = "Option::is_none")]
8792
#[serde(rename = "displayLink")]
8893
pub display_link: Option<String>,
94+
#[serde(skip_serializing_if = "Option::is_none")]
8995
#[serde(rename = "htmlSnippet")]
9096
pub html_snippet: Option<String>,
97+
#[serde(skip_serializing_if = "Option::is_none")]
9198
#[serde(rename = "pagemap")]
9299
pub pagemap: Option<serde_json::Value>,
93100
}
94101

95-
#[derive(Debug, Deserialize)]
102+
#[derive(Debug, Serialize, Deserialize, Clone)]
96103
pub struct GoogleSearchInformation {
104+
#[serde(skip_serializing_if = "Option::is_none")]
97105
#[serde(rename = "totalResults")]
98106
pub total_results: Option<String>,
107+
#[serde(skip_serializing_if = "Option::is_none")]
99108
#[serde(rename = "searchTime")]
100109
pub search_time: Option<f64>,
101110
}
102111

103-
#[derive(Debug, Deserialize)]
112+
#[derive(Debug, Serialize, Deserialize, Clone)]
104113
pub struct GoogleQueries {
114+
#[serde(skip_serializing_if = "Option::is_none")]
105115
pub request: Option<Vec<GoogleQueryInfo>>,
116+
#[serde(skip_serializing_if = "Option::is_none")]
106117
#[serde(rename = "nextPage")]
107118
pub next_page: Option<Vec<GoogleQueryInfo>>,
108119
}
109120

110-
#[derive(Debug, Deserialize)]
121+
#[derive(Debug, Serialize, Deserialize, Clone)]
111122
pub struct GoogleQueryInfo {
112123
pub title: String,
113124
#[serde(rename = "totalResults")]
114125
pub total_results: Option<String>,
115126
#[serde(rename = "searchTerms")]
116127
pub search_terms: String,
128+
#[serde(skip_serializing_if = "Option::is_none")]
117129
pub count: Option<u32>,
118130
#[serde(rename = "startIndex")]
119131
pub start_index: Option<u32>,
120132
}
121133

122-
#[derive(Debug, Deserialize)]
134+
#[derive(Debug, Serialize, Deserialize, Clone)]
123135
pub struct GoogleError {
124136
pub code: u32,
125137
pub message: String,
@@ -142,7 +154,7 @@ fn parse_response(response: Response) -> Result<GoogleSearchResponse, SearchErro
142154

143155
if let Some(error) = &search_response.error {
144156
return match error.code {
145-
429 => Err(SearchError::RateLimited(60)),
157+
429 => Err(SearchError::RateLimited(60)),
146158
400 => Err(SearchError::InvalidQuery),
147159
_ => Err(SearchError::BackendError(format!(
148160
"Google API error: {}",

golem-web-search/websearch-google/src/conversions.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn convert_params_to_request(
1111
) -> GoogleSearchRequest {
1212
let mut request = GoogleSearchRequest {
1313
q: params.query.clone(),
14-
cx: String::new(),
14+
cx: String::new(),
1515
key: String::new(),
1616
num: params.max_results,
1717
start: start_index,
@@ -75,14 +75,14 @@ pub fn convert_response_to_results(
7575
snippet: item.snippet,
7676
display_url: item.display_link,
7777
source: Some("Google".to_string()),
78-
score: None,
78+
score: None,
7979
html_snippet: if params.include_html.unwrap_or(false) {
8080
item.html_snippet
8181
} else {
8282
None
8383
},
84-
date_published: None,
85-
images: None,
84+
date_published: None,
85+
images: None,
8686
content_chunks: None,
8787
})
8888
.collect()
@@ -102,7 +102,7 @@ pub fn convert_response_to_results(
102102
.as_ref()
103103
.and_then(|info| info.search_time)
104104
.map(|t| t * 1000.0),
105-
safe_search: params.safe_search.clone(),
105+
safe_search: params.safe_search,
106106
language: params.language.clone(),
107107
region: params.region.clone(),
108108
next_page_token: response

0 commit comments

Comments
 (0)