(&self, request: ChatRequest)
| 169 | } |
| 170 | |
| 171 | async fn chat_stream(&self, request: ChatRequest) -> Result<StreamResult, ProviderError> { |
| 172 | let url = self.get_api_url(); |
| 173 | let mut gitlab_request = self.convert_request(request); |
| 174 | gitlab_request.stream = true; |
| 175 | |
| 176 | let response = self |
| 177 | .client |
| 178 | .post(&url) |
| 179 | .header("Content-Type", "application/json") |
| 180 | .header("PRIVATE-TOKEN", &self.config.api_key) |
| 181 | .header("Accept", "text/event-stream") |
| 182 | .json(&gitlab_request) |
| 183 | .send() |
| 184 | .await |
| 185 | .map_err(|e| ProviderError::NetworkError(e.to_string()))?; |
| 186 | |
| 187 | if !response.status().is_success() { |
| 188 | let status = response.status(); |
| 189 | let body = response.text().await.unwrap_or_default(); |
| 190 | return Err(ProviderError::api_error_with_status( |
| 191 | format!("{}: {}", status, body), |
| 192 | status.as_u16(), |
| 193 | )); |
| 194 | } |
| 195 | |
| 196 | let stream = response |
| 197 | .bytes_stream() |
| 198 | .map(move |chunk_result| match chunk_result { |
| 199 | Ok(bytes) => { |
| 200 | let text = String::from_utf8_lossy(&bytes); |
| 201 | for line in text.lines() { |
| 202 | if line.starts_with("data: ") { |
| 203 | let data = &line[6..]; |
| 204 | if data == "[DONE]" { |
| 205 | return Ok(StreamEvent::Done); |
| 206 | } |
| 207 | if let Some(event) = parse_gitlab_sse(data) { |
| 208 | return Ok(event); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | Ok(StreamEvent::TextDelta(String::new())) |
| 213 | } |
| 214 | Err(e) => Err(ProviderError::StreamError(e.to_string())), |
| 215 | }); |
| 216 | |
| 217 | Ok(Box::pin(stream)) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | #[derive(Debug, Serialize)] |
nothing calls this directly
no test coverage detected