Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
fix Grok hosted web search mapping
Raine Virta259f2cccfbee
diff --git a/src/providers/grok/translate/accumulate.rs b/src/providers/grok/translate/accumulate.rsindex cef3621..0c59929 100644--- a/src/providers/grok/translate/accumulate.rs+++ b/src/providers/grok/translate/accumulate.rs@@ -50,6 +50,7 @@ pub fn accumulate_response_with_traffic(let mut stop = "end_turn".to_string();let mut input = 0;let mut output = 0;+ let mut web_search_requests = 0;let reduced = match reduce_upstream_bytes(upstream) {Ok(events) => events,Err(error) => {@@ -127,19 +128,50 @@ pub fn accumulate_response_with_traffic(block.as_object_mut().unwrap().remove("_args");}}+ ReducerEvent::WebSearch {+ index,+ result_index,+ id,+ query,+ } => {+ block_positions.insert(index, blocks.len());+ blocks.push(serde_json::json!({"type":"server_tool_use","id":id,"name":"web_search","input":{"query":query}}));+ block_positions.insert(result_index, blocks.len());+ blocks.push(serde_json::json!({"type":"web_search_tool_result","tool_use_id":id,"content":[]}));+ }+ ReducerEvent::Citation(index, annotation) => {+ if let Some(block) = block_positions+ .get(&index)+ .and_then(|position| blocks.get_mut(*position))+ {+ let citations = block+ .as_object_mut()+ .unwrap()+ .entry("citations")+ .or_insert_with(|| Value::Array(Vec::new()));+ citations.as_array_mut().unwrap().push(serde_json::json!({+ "type":"web_search_result_location",+ "url":annotation.get("url").and_then(Value::as_str).unwrap_or_default(),+ "title":annotation.get("title").and_then(Value::as_str).unwrap_or_default(),+ "cited_text":annotation.get("text").and_then(Value::as_str).unwrap_or_default()+ }));+ }+ }ReducerEvent::Finish {stop_reason,input_tokens,output_tokens,+ web_search_requests: requests,} => {stop = stop_reason;input = input_tokens;output = output_tokens;+ web_search_requests = requests;}_ => {}}}- let response = serde_json::json!({"id":message_id,"type":"message","role":"assistant","model":model,"content":blocks,"stop_reason":stop,"stop_sequence":null,"usage":{"input_tokens":input,"output_tokens":output}});+ let response = serde_json::json!({"id":message_id,"type":"message","role":"assistant","model":model,"content":blocks,"stop_reason":stop,"stop_sequence":null,"usage":{"input_tokens":input,"output_tokens":output,"server_tool_use":{"web_search_requests":web_search_requests}}});if let Some(mut capture) = capture {capture.downstream_event("response", response.clone());finish_capture(Some(capture), traffic, "completed");diff --git a/src/providers/grok/translate/reducer.rs b/src/providers/grok/translate/reducer.rsindex 72f1dc0..394c16f 100644--- a/src/providers/grok/translate/reducer.rs+++ b/src/providers/grok/translate/reducer.rs@@ -18,10 +18,18 @@ pub enum ReducerEvent {ToolStart(usize, String, String),ToolDelta(usize, String),ToolStop(usize),+ WebSearch {+ index: usize,+ result_index: usize,+ id: String,+ query: String,+ },+ Citation(usize, Value),Finish {stop_reason: String,input_tokens: u64,output_tokens: u64,+ web_search_requests: u64,},}@@ -33,6 +41,7 @@ pub struct Reducer {item_calls: HashMap<String, String>,tool_args: HashMap<String, String>,completed_arguments: HashMap<String, bool>,+ web_search_requests: u64,saw_tool: bool,completed: bool,}@@ -61,7 +70,24 @@ impl Reducer {),"response.custom_tool_call_input.delta"| "response.custom_tool_call_input.done"- | "response.output_text.annotation.added" => Ok(vec![]),+ | "response.web_search_call.in_progress"+ | "response.web_search_call.searching"+ | "response.web_search_call.completed" => Ok(vec![]),+ "response.output_text.annotation.added" => {+ let Some((kind, index)) = self.active.as_ref() else {+ return Ok(vec![]);+ };+ let Some(annotation) = value.get("annotation") else {+ return Ok(vec![]);+ };+ if kind == "text"+ && annotation.get("type").and_then(Value::as_str) == Some("url_citation")+ {+ Ok(vec![ReducerEvent::Citation(*index, annotation.clone())])+ } else {+ Ok(vec![])+ }+ }"response.output_text.delta" => self.delta("text",value@@ -181,22 +207,47 @@ impl Reducer {.get("item").and_then(Value::as_object).ok_or_else(|| anyhow::anyhow!("completed output item is invalid"))?;- if item.get("type").and_then(Value::as_str) != Some("function_call") {- return Ok(vec![]);+ match item.get("type").and_then(Value::as_str) {+ Some("web_search_call") => {+ let id = item+ .get("id")+ .and_then(Value::as_str)+ .filter(|id| !id.is_empty())+ .ok_or_else(|| anyhow::anyhow!("completed web search lacks id"))?;+ let query = item+ .get("action")+ .and_then(|action| action.get("query"))+ .and_then(Value::as_str)+ .unwrap_or_default();+ let mut out = self.close_active()?;+ let index = self.next_index;+ let result_index = index + 1;+ self.next_index += 2;+ self.web_search_requests += 1;+ out.push(ReducerEvent::WebSearch {+ index,+ result_index,+ id: format!("srvtoolu_{id}"),+ query: query.into(),+ });+ Ok(out)+ }+ Some("function_call") => {+ let id = item.get("call_id").and_then(Value::as_str).ok_or_else(|| {+ anyhow::anyhow!("completed function call lacks call id")+ })?;+ let (index, _) = self+ .calls+ .remove(id)+ .ok_or_else(|| anyhow::anyhow!("completed function call is unknown"))?;+ let args = self.tool_args.remove(id).unwrap_or_default();+ self.completed_arguments.remove(id);+ serde_json::from_str::<Value>(&args)+ .map_err(|_| anyhow::anyhow!("function arguments are incomplete"))?;+ Ok(vec![ReducerEvent::ToolStop(index)])+ }+ _ => Ok(vec![]),}- let id = item- .get("call_id")- .and_then(Value::as_str)- .ok_or_else(|| anyhow::anyhow!("completed function call lacks call id"))?;- let (index, _) = self- .calls- .remove(id)- .ok_or_else(|| anyhow::anyhow!("completed function call is unknown"))?;- let args = self.tool_args.remove(id).unwrap_or_default();- self.completed_arguments.remove(id);- serde_json::from_str::<Value>(&args)- .map_err(|_| anyhow::anyhow!("function arguments are incomplete"))?;- Ok(vec![ReducerEvent::ToolStop(index)])}"response.completed" => {if !self.calls.is_empty() {@@ -223,6 +274,7 @@ impl Reducer {stop_reason: stop.into(),input_tokens: input,output_tokens: output,+ web_search_requests: self.web_search_requests,});Ok(out)}diff --git a/src/providers/grok/translate/request.rs b/src/providers/grok/translate/request.rsindex 108dd26..311e88e 100644--- a/src/providers/grok/translate/request.rs+++ b/src/providers/grok/translate/request.rs@@ -78,18 +78,18 @@ pub fn translate_request(let mut tools = parse_tools(req.extra.get("tools"))?;let hosted_search = tools.as_ref()- .is_some_and(|tools| tools.iter().any(|tool| tool.kind == "x_search"));+ .is_some_and(|tools| tools.iter().any(|tool| tool.kind == "web_search"));let force_hosted_search = hosted_search && requests_web_search(req);if force_hosted_search {tools = Some(vec![GrokTool {- kind: "x_search".into(),+ kind: "web_search".into(),name: None,description: None,parameters: None,}]);}if hosted_search {- let guidance = "For web searches, use the hosted x_search tool. Do not use shell commands, HTTP clients, or local tools to search the web.";+ let guidance = "For general web searches, use the hosted web_search tool. Do not use shell commands, HTTP clients, or local tools to search the web.";instructions = Some(match instructions {Some(existing) if !existing.is_empty() => format!("{existing}\n\n{guidance}"),_ => guidance.into(),@@ -227,7 +227,7 @@ fn parse_tools(value: Option<&Value>) -> anyhow::Result<Option<Vec<GrokTool>>> {}if name == "WebSearch" {out.push(GrokTool {- kind: "x_search".into(),+ kind: "web_search".into(),name: None,description: None,parameters: None,@@ -471,7 +471,7 @@ mod tests {assert_eq!(value["tool_choice"]["type"], "function");}#[test]- fn grok_translation_maps_claude_web_search_to_x_search() {+ fn grok_translation_maps_claude_web_search_to_hosted_web_search() {let request: MessagesRequest = serde_json::from_value(serde_json::json!({"model":"grok-4.5","messages":[{"role":"user","content":"search online for the project"}],@@ -486,13 +486,13 @@ mod tests {serde_json::to_value(translate_request(&request, "grok-4.5".into()).unwrap()).unwrap();assert_eq!(translated["tools"],- serde_json::json!([{"type":"x_search"}])+ serde_json::json!([{"type":"web_search"}]));assert!(translated["instructions"].as_str().unwrap()- .contains("use the hosted x_search tool")+ .contains("use the hosted web_search tool"));assert_eq!(translated["tool_choice"], "required");}diff --git a/src/providers/grok/translate/stream.rs b/src/providers/grok/translate/stream.rsindex 6ede3e6..ad3d669 100644--- a/src/providers/grok/translate/stream.rs+++ b/src/providers/grok/translate/stream.rs@@ -152,6 +152,7 @@ impl StreamTranslator {ReducerEvent::ThinkingStart(_)| ReducerEvent::TextStart(_)| ReducerEvent::ToolStart(_, _, _)+ | ReducerEvent::WebSearch { .. }| ReducerEvent::Finish { .. }){@@ -240,15 +241,61 @@ fn render(out: &mut Vec<u8>, event: ReducerEvent) {"content_block_delta",serde_json::json!({"type":"content_block_delta","index":i,"delta":{"type":"input_json_delta","partial_json":t}}),),+ ReducerEvent::WebSearch {+ index,+ result_index,+ id,+ query,+ } => {+ emit(+ out,+ "content_block_start",+ serde_json::json!({"type":"content_block_start","index":index,"content_block":{"type":"server_tool_use","id":id,"name":"web_search","input":{}}}),+ );+ emit(+ out,+ "content_block_delta",+ serde_json::json!({"type":"content_block_delta","index":index,"delta":{"type":"input_json_delta","partial_json":serde_json::json!({"query":query}).to_string()}}),+ );+ emit(+ out,+ "content_block_stop",+ serde_json::json!({"type":"content_block_stop","index":index}),+ );+ emit(+ out,+ "content_block_start",+ serde_json::json!({"type":"content_block_start","index":result_index,"content_block":{"type":"web_search_tool_result","tool_use_id":id,"content":[]}}),+ );+ emit(+ out,+ "content_block_stop",+ serde_json::json!({"type":"content_block_stop","index":result_index}),+ );+ }+ ReducerEvent::Citation(i, annotation) => {+ let citation = serde_json::json!({+ "type":"web_search_result_location",+ "url":annotation.get("url").and_then(serde_json::Value::as_str).unwrap_or_default(),+ "title":annotation.get("title").and_then(serde_json::Value::as_str).unwrap_or_default(),+ "cited_text":annotation.get("text").and_then(serde_json::Value::as_str).unwrap_or_default()+ });+ emit(+ out,+ "content_block_delta",+ serde_json::json!({"type":"content_block_delta","index":i,"delta":{"type":"citations_delta","citation":citation}}),+ );+ }ReducerEvent::Finish {stop_reason,output_tokens,+ web_search_requests,..} => {emit(out,"message_delta",- serde_json::json!({"type":"message_delta","delta":{"stop_reason":stop_reason,"stop_sequence":null},"usage":{"output_tokens":output_tokens}}),+ serde_json::json!({"type":"message_delta","delta":{"stop_reason":stop_reason,"stop_sequence":null},"usage":{"output_tokens":output_tokens,"server_tool_use":{"web_search_requests":web_search_requests}}}),);emit(out,@@ -298,6 +345,18 @@ mod tests {assert!(decoder.push(b"\n").is_err());}+ #[test]+ fn stream_translates_hosted_web_search_and_citations() {+ let input = b"data: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"web_search_call\",\"id\":\"ws_1\"}}\n\ndata: {\"type\":\"response.web_search_call.in_progress\",\"item_id\":\"ws_1\"}\n\ndata: {\"type\":\"response.web_search_call.searching\",\"item_id\":\"ws_1\"}\n\ndata: {\"type\":\"response.web_search_call.completed\",\"item_id\":\"ws_1\"}\n\ndata: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"web_search_call\",\"id\":\"ws_1\",\"action\":{\"query\":\"rust news\"}}}\n\ndata: {\"type\":\"response.output_text.delta\",\"delta\":\"Result\"}\n\ndata: {\"type\":\"response.output_text.annotation.added\",\"annotation\":{\"type\":\"url_citation\",\"url\":\"https://example.com\",\"title\":\"Example\"}}\n\ndata: {\"type\":\"response.output_text.done\"}\n\ndata: {\"type\":\"response.completed\",\"response\":{\"usage\":{\"input_tokens\":3,\"output_tokens\":2}}}\n\n";+ let output =+ String::from_utf8(translate_stream_bytes(input, "msg_1", "grok-4.5").unwrap()).unwrap();+ assert!(output.contains("server_tool_use"));+ assert!(output.contains("web_search_tool_result"));+ assert!(output.contains("citations_delta"));+ assert!(output.contains("https://example.com"));+ assert!(output.contains("\"web_search_requests\":1"));+ }+#[test]fn live_translator_emits_first_event_before_upstream_completion() {let mut translator = LiveStreamTranslator::new("msg_1".into(), "grok-4.5".into());