MCPcopy Index your code
hub / github.com/ChrisFeldmeier/OpenCodeRust / chat_stream

Method chat_stream

crates/opencode-provider/src/anthropic.rs:197–241  ·  view source on GitHub ↗
(&self, request: ChatRequest)

Source from the content-addressed store, hash-verified

195 }
196
197 async fn chat_stream(&self, request: ChatRequest) -> Result<StreamResult, ProviderError> {
198 let mut anthropic_request = self.convert_request(request);
199 anthropic_request.stream = Some(true);
200
201 let url = self.config.base_url.as_deref().unwrap_or(ANTHROPIC_API_URL);
202
203 let response = self
204 .client
205 .post(url)
206 .header("x-api-key", &self.config.api_key)
207 .header("anthropic-version", "2023-06-01")
208 .header("anthropic-beta", "claude-code-20250219,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14")
209 .header("content-type", "application/json")
210 .header("accept", "text/event-stream")
211 .json(&anthropic_request)
212 .send()
213 .await
214 .map_err(|e| ProviderError::NetworkError(e.to_string()))?;
215
216 if !response.status().is_success() {
217 let status = response.status();
218 let body = response.text().await.unwrap_or_default();
219 return Err(ProviderError::ApiError(format!("{}: {}", status, body)));
220 }
221
222 let stream = response
223 .bytes_stream()
224 .map(move |chunk_result| match chunk_result {
225 Ok(bytes) => {
226 let text = String::from_utf8_lossy(&bytes);
227 for line in text.lines() {
228 if line.starts_with("data: ") {
229 let data = &line[6..];
230 if let Some(event) = crate::stream::parse_anthropic_sse(data) {
231 return Ok(event);
232 }
233 }
234 }
235 Ok(StreamEvent::TextDelta(String::new()))
236 }
237 Err(e) => Err(ProviderError::StreamError(e.to_string())),
238 });
239
240 Ok(Box::pin(stream))
241 }
242}
243
244#[derive(Debug, Serialize)]

Callers

nothing calls this directly

Calls 7

parse_anthropic_sseFunction · 0.85
newFunction · 0.85
ApiErrorEnum · 0.50
convert_requestMethod · 0.45
sendMethod · 0.45
statusMethod · 0.45
textMethod · 0.45

Tested by

no test coverage detected