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

Method chat_stream

crates/opencode-provider/src/perplexity.rs:111–151  ·  view source on GitHub ↗
(&self, request: ChatRequest)

Source from the content-addressed store, hash-verified

109 }
110
111 async fn chat_stream(&self, request: ChatRequest) -> Result<StreamResult, ProviderError> {
112 let mut stream_request = request;
113 stream_request.stream = Some(true);
114
115 let response = self
116 .client
117 .post(PERPLEXITY_API_URL)
118 .header("Authorization", format!("Bearer {}", self.api_key))
119 .header("Content-Type", "application/json")
120 .header("Accept", "text/event-stream")
121 .json(&stream_request)
122 .send()
123 .await
124 .map_err(|e| ProviderError::NetworkError(e.to_string()))?;
125
126 if !response.status().is_success() {
127 let status = response.status();
128 let body = response.text().await.unwrap_or_default();
129 return Err(ProviderError::ApiError(format!("{}: {}", status, body)));
130 }
131
132 let stream = response
133 .bytes_stream()
134 .map(|chunk_result| match chunk_result {
135 Ok(bytes) => {
136 let text = String::from_utf8_lossy(&bytes);
137 for line in text.lines() {
138 if line.starts_with("data: ") {
139 let data = &line[6..];
140 if let Some(event) = crate::stream::parse_openai_sse(data) {
141 return Ok(event);
142 }
143 }
144 }
145 Ok(StreamEvent::TextDelta(String::new()))
146 }
147 Err(e) => Err(ProviderError::StreamError(e.to_string())),
148 });
149
150 Ok(Box::pin(stream))
151 }
152}

Callers

nothing calls this directly

Calls 6

parse_openai_sseFunction · 0.85
newFunction · 0.85
ApiErrorEnum · 0.50
sendMethod · 0.45
statusMethod · 0.45
textMethod · 0.45

Tested by

no test coverage detected