(&self, request: ChatRequest)
| 205 | } |
| 206 | |
| 207 | async fn chat_stream(&self, request: ChatRequest) -> Result<StreamResult, ProviderError> { |
| 208 | let base_url = self.config.base_url.as_deref().unwrap_or(GOOGLE_API_URL); |
| 209 | let url = format!( |
| 210 | "{}/{}:streamGenerateContent?key={}&alt=sse", |
| 211 | base_url, request.model, self.config.api_key |
| 212 | ); |
| 213 | |
| 214 | let google_request = self.convert_request(request); |
| 215 | |
| 216 | let response = self |
| 217 | .client |
| 218 | .post(&url) |
| 219 | .header("Content-Type", "application/json") |
| 220 | .header("Accept", "text/event-stream") |
| 221 | .json(&google_request) |
| 222 | .send() |
| 223 | .await |
| 224 | .map_err(|e| ProviderError::NetworkError(e.to_string()))?; |
| 225 | |
| 226 | if !response.status().is_success() { |
| 227 | let status = response.status(); |
| 228 | let body = response.text().await.unwrap_or_default(); |
| 229 | return Err(ProviderError::ApiError(format!("{}: {}", status, body))); |
| 230 | } |
| 231 | |
| 232 | let stream = response |
| 233 | .bytes_stream() |
| 234 | .map(move |chunk_result| match chunk_result { |
| 235 | Ok(bytes) => { |
| 236 | let text = String::from_utf8_lossy(&bytes); |
| 237 | for line in text.lines() { |
| 238 | if line.starts_with("data: ") { |
| 239 | let data = &line[6..]; |
| 240 | if let Some(event) = parse_google_sse(data) { |
| 241 | return Ok(event); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | Ok(StreamEvent::TextDelta(String::new())) |
| 246 | } |
| 247 | Err(e) => Err(ProviderError::StreamError(e.to_string())), |
| 248 | }); |
| 249 | |
| 250 | Ok(Box::pin(stream)) |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | #[derive(Debug, Serialize)] |
nothing calls this directly
no test coverage detected