(&self, request: ChatRequest)
| 156 | } |
| 157 | |
| 158 | async fn chat_stream(&self, request: ChatRequest) -> Result<StreamResult, ProviderError> { |
| 159 | let url = self.config.base_url.as_deref().unwrap_or(VERCEL_API_URL); |
| 160 | let mut vercel_request = self.convert_request(request); |
| 161 | vercel_request.stream = true; |
| 162 | |
| 163 | let response = self |
| 164 | .client |
| 165 | .post(url) |
| 166 | .header("Content-Type", "application/json") |
| 167 | .header("Authorization", format!("Bearer {}", self.config.api_key)) |
| 168 | .header("Accept", "text/event-stream") |
| 169 | .json(&vercel_request) |
| 170 | .send() |
| 171 | .await |
| 172 | .map_err(|e| ProviderError::NetworkError(e.to_string()))?; |
| 173 | |
| 174 | if !response.status().is_success() { |
| 175 | let status = response.status(); |
| 176 | let body = response.text().await.unwrap_or_default(); |
| 177 | return Err(ProviderError::api_error_with_status( |
| 178 | format!("{}: {}", status, body), |
| 179 | status.as_u16(), |
| 180 | )); |
| 181 | } |
| 182 | |
| 183 | let stream = response |
| 184 | .bytes_stream() |
| 185 | .map(move |chunk_result| match chunk_result { |
| 186 | Ok(bytes) => { |
| 187 | let text = String::from_utf8_lossy(&bytes); |
| 188 | for line in text.lines() { |
| 189 | if line.starts_with("data: ") { |
| 190 | let data = &line[6..]; |
| 191 | if data == "[DONE]" { |
| 192 | return Ok(StreamEvent::Done); |
| 193 | } |
| 194 | if let Some(event) = parse_vercel_sse(data) { |
| 195 | return Ok(event); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | Ok(StreamEvent::TextDelta(String::new())) |
| 200 | } |
| 201 | Err(e) => Err(ProviderError::StreamError(e.to_string())), |
| 202 | }); |
| 203 | |
| 204 | Ok(Box::pin(stream)) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | #[derive(Debug, Serialize)] |
nothing calls this directly
no test coverage detected