1use std::collections::HashMap;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use futures_util::Stream;
6use pin_project_lite::pin_project;
7use serde::{Deserialize, Serialize};
8
9use crate::client::Client;
10use crate::error::Result;
11
12fn null_as_empty_vec<'de, D, T>(deserializer: D) -> std::result::Result<Vec<T>, D::Error>
14where
15 D: serde::Deserializer<'de>,
16 T: Deserialize<'de>,
17{
18 Option::<Vec<T>>::deserialize(deserializer).map(|v| v.unwrap_or_default())
19}
20
21fn deserialize_opt_vec<'de, D, T>(deserializer: D) -> std::result::Result<Option<Vec<T>>, D::Error>
23where
24 D: serde::Deserializer<'de>,
25 T: Deserialize<'de>,
26{
27 Ok(Option::<Vec<T>>::deserialize(deserializer).unwrap_or(None))
29}
30
31#[derive(Debug, Clone, Serialize, Default)]
33pub struct ChatRequest {
34 pub model: String,
36
37 pub messages: Vec<ChatMessage>,
39
40 #[serde(skip_serializing_if = "Option::is_none")]
42 pub tools: Option<Vec<ChatTool>>,
43
44 #[serde(skip_serializing_if = "Option::is_none")]
46 pub tool_choice: Option<String>,
47
48 #[serde(skip_serializing_if = "Option::is_none")]
50 pub output_schema: Option<serde_json::Value>,
51
52 #[serde(skip_serializing_if = "Option::is_none")]
54 pub stream: Option<bool>,
55
56 #[serde(skip_serializing_if = "Option::is_none")]
58 pub temperature: Option<f64>,
59
60 #[serde(skip_serializing_if = "Option::is_none")]
62 pub max_tokens: Option<i32>,
63
64 #[serde(skip_serializing_if = "Option::is_none")]
66 pub provider_options: Option<HashMap<String, serde_json::Value>>,
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize, Default)]
71pub struct ChatMessage {
72 pub role: String,
74
75 #[serde(skip_serializing_if = "Option::is_none")]
77 pub content: Option<String>,
78
79 #[serde(skip_serializing_if = "Option::is_none", deserialize_with = "deserialize_opt_vec", default)]
82 pub content_blocks: Option<Vec<ContentBlock>>,
83
84 #[serde(skip_serializing_if = "Option::is_none")]
86 pub tool_call_id: Option<String>,
87
88 #[serde(skip_serializing_if = "Option::is_none")]
90 pub is_error: Option<bool>,
91}
92
93impl ChatMessage {
94 pub fn user(content: impl Into<String>) -> Self {
96 Self {
97 role: "user".to_string(),
98 content: Some(content.into()),
99 ..Default::default()
100 }
101 }
102
103 pub fn assistant(content: impl Into<String>) -> Self {
105 Self {
106 role: "assistant".to_string(),
107 content: Some(content.into()),
108 ..Default::default()
109 }
110 }
111
112 pub fn system(content: impl Into<String>) -> Self {
114 Self {
115 role: "system".to_string(),
116 content: Some(content.into()),
117 ..Default::default()
118 }
119 }
120
121 pub fn tool_result(tool_call_id: impl Into<String>, content: impl Into<String>) -> Self {
123 Self {
124 role: "tool".to_string(),
125 content: Some(content.into()),
126 tool_call_id: Some(tool_call_id.into()),
127 ..Default::default()
128 }
129 }
130
131 pub fn tool_error(tool_call_id: impl Into<String>, content: impl Into<String>) -> Self {
133 Self {
134 role: "tool".to_string(),
135 content: Some(content.into()),
136 tool_call_id: Some(tool_call_id.into()),
137 is_error: Some(true),
138 ..Default::default()
139 }
140 }
141}
142
143#[derive(Debug, Clone, Serialize, Deserialize, Default)]
145pub struct ContentBlock {
146 #[serde(rename = "type")]
148 pub block_type: String,
149
150 #[serde(skip_serializing_if = "Option::is_none")]
152 pub text: Option<String>,
153
154 #[serde(skip_serializing_if = "Option::is_none")]
156 pub id: Option<String>,
157
158 #[serde(skip_serializing_if = "Option::is_none")]
160 pub name: Option<String>,
161
162 #[serde(skip_serializing_if = "Option::is_none")]
164 pub input: Option<HashMap<String, serde_json::Value>>,
165
166 #[serde(skip_serializing_if = "Option::is_none")]
168 pub thought_signature: Option<String>,
169
170 #[serde(skip_serializing_if = "Option::is_none")]
172 pub data: Option<String>,
173
174 #[serde(skip_serializing_if = "Option::is_none")]
176 pub file_name: Option<String>,
177
178 #[serde(skip_serializing_if = "Option::is_none")]
180 pub mime_type: Option<String>,
181}
182
183#[derive(Debug, Clone, Serialize, Default)]
185pub struct ChatTool {
186 pub name: String,
188
189 pub description: String,
191
192 #[serde(skip_serializing_if = "Option::is_none")]
194 pub parameters: Option<serde_json::Value>,
195
196 #[serde(skip_serializing_if = "Option::is_none")]
198 pub strict: Option<bool>,
199}
200
201#[derive(Debug, Clone, Deserialize)]
203pub struct ChatResponse {
204 pub id: String,
206
207 pub model: String,
209
210 #[serde(default, deserialize_with = "null_as_empty_vec")]
212 pub content: Vec<ContentBlock>,
213
214 pub usage: Option<ChatUsage>,
216
217 #[serde(default)]
219 pub stop_reason: String,
220
221 #[serde(default, deserialize_with = "null_as_empty_vec")]
223 pub citations: Vec<Citation>,
224
225 #[serde(skip)]
227 pub cost_ticks: i64,
228
229 #[serde(skip)]
231 pub request_id: String,
232}
233
234impl ChatResponse {
235 pub fn text(&self) -> String {
237 self.content
238 .iter()
239 .filter(|b| b.block_type == "text")
240 .filter_map(|b| b.text.as_deref())
241 .collect::<Vec<_>>()
242 .join("")
243 }
244
245 pub fn thinking(&self) -> String {
247 self.content
248 .iter()
249 .filter(|b| b.block_type == "thinking")
250 .filter_map(|b| b.text.as_deref())
251 .collect::<Vec<_>>()
252 .join("")
253 }
254
255 pub fn tool_calls(&self) -> Vec<&ContentBlock> {
257 self.content
258 .iter()
259 .filter(|b| b.block_type == "tool_use")
260 .collect()
261 }
262}
263
264#[derive(Debug, Clone, Deserialize, Serialize)]
266pub struct Citation {
267 #[serde(default)]
269 pub title: String,
270
271 #[serde(default)]
273 pub url: String,
274
275 #[serde(default)]
277 pub text: String,
278
279 #[serde(default)]
281 pub index: i32,
282}
283
284#[derive(Debug, Clone, Deserialize)]
286pub struct ChatUsage {
287 pub input_tokens: i32,
288 pub output_tokens: i32,
289 pub cost_ticks: i64,
290}
291
292#[derive(Debug, Clone)]
301pub struct StreamEvent {
302 pub event_type: String,
306
307 pub delta: Option<StreamDelta>,
309
310 pub tool_use: Option<StreamToolUse>,
312
313 pub tool_use_start: Option<StreamToolUseStart>,
315
316 pub tool_use_input_delta: Option<StreamToolUseInputDelta>,
318
319 pub tool_use_complete: Option<StreamToolUseComplete>,
321
322 pub usage: Option<ChatUsage>,
324
325 pub error: Option<String>,
327
328 pub done: bool,
330}
331
332#[derive(Debug, Clone, Deserialize)]
334pub struct StreamDelta {
335 pub text: String,
336}
337
338#[derive(Debug, Clone, Deserialize)]
340pub struct StreamToolUse {
341 pub id: String,
342 pub name: String,
343 pub input: HashMap<String, serde_json::Value>,
344}
345
346#[derive(Debug, Clone, Deserialize)]
348pub struct StreamToolUseStart {
349 pub id: String,
350 pub name: String,
351}
352
353#[derive(Debug, Clone, Deserialize)]
355pub struct StreamToolUseInputDelta {
356 pub id: String,
357 pub partial_json: String,
361}
362
363#[derive(Debug, Clone, Deserialize)]
366pub struct StreamToolUseComplete {
367 pub id: String,
368 pub name: String,
369 pub input: HashMap<String, serde_json::Value>,
370}
371
372#[derive(Deserialize)]
374struct RawStreamEvent {
375 #[serde(rename = "type")]
376 event_type: String,
377 #[serde(default)]
378 delta: Option<StreamDelta>,
379 #[serde(default)]
380 id: Option<String>,
381 #[serde(default)]
382 name: Option<String>,
383 #[serde(default)]
384 input: Option<HashMap<String, serde_json::Value>>,
385 #[serde(default)]
387 partial_json: Option<String>,
388 #[serde(default)]
389 input_tokens: Option<i32>,
390 #[serde(default)]
391 output_tokens: Option<i32>,
392 #[serde(default)]
393 cost_ticks: Option<i64>,
394 #[serde(default)]
395 message: Option<String>,
396}
397
398pin_project! {
399 pub struct ChatStream {
401 #[pin]
402 inner: Pin<Box<dyn Stream<Item = StreamEvent> + Send>>,
403 }
404}
405
406impl Stream for ChatStream {
407 type Item = StreamEvent;
408
409 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
410 self.project().inner.poll_next(cx)
411 }
412}
413
414impl Client {
415 pub async fn chat(&self, req: &ChatRequest) -> Result<ChatResponse> {
417 let mut req = req.clone();
418 req.stream = Some(false);
419
420 let (mut resp, meta) = self.post_json::<ChatRequest, ChatResponse>("/qai/v1/chat", &req).await?;
421 resp.cost_ticks = meta.cost_ticks;
422 resp.request_id = meta.request_id;
423 if resp.model.is_empty() {
424 resp.model = meta.model;
425 }
426 Ok(resp)
427 }
428
429 pub async fn chat_stream(&self, req: &ChatRequest) -> Result<ChatStream> {
453 let mut req = req.clone();
454 req.stream = Some(true);
455
456 let (resp, _meta) = self.post_stream_raw("/qai/v1/chat", &req).await?;
457
458 let byte_stream = resp.bytes_stream();
459 let event_stream = sse_to_events(byte_stream);
460
461 Ok(ChatStream {
462 inner: Box::pin(event_stream),
463 })
464 }
465}
466
467fn sse_to_events<S>(byte_stream: S) -> impl Stream<Item = StreamEvent> + Send
469where
470 S: Stream<Item = std::result::Result<bytes::Bytes, reqwest::Error>> + Send + 'static,
471{
472 let pinned_stream = Box::pin(byte_stream);
474
475 let line_stream = futures_util::stream::unfold(
478 (pinned_stream, Vec::<u8>::new()),
479 |(mut stream, mut buffer)| async move {
480 use futures_util::StreamExt;
481 loop {
482 if let Some(newline_pos) = buffer.iter().position(|&b| b == b'\n') {
484 let mut line_bytes = buffer[..newline_pos].to_vec();
485 buffer = buffer[newline_pos + 1..].to_vec();
486 if line_bytes.last() == Some(&b'\r') {
488 line_bytes.pop();
489 }
490 let line = String::from_utf8_lossy(&line_bytes).into_owned();
491 return Some((line, (stream, buffer)));
492 }
493
494 match stream.next().await {
496 Some(Ok(chunk)) => {
497 buffer.extend_from_slice(&chunk);
498 }
499 Some(Err(_)) | None => {
500 if !buffer.is_empty() {
502 let remaining = String::from_utf8_lossy(&buffer).into_owned();
503 buffer.clear();
504 return Some((remaining, (stream, buffer)));
505 }
506 return None;
507 }
508 }
509 }
510 },
511 );
512
513 let pinned_lines = Box::pin(line_stream);
514 futures_util::stream::unfold(pinned_lines, |mut lines| async move {
515 use futures_util::StreamExt;
516 loop {
517 let line = lines.next().await?;
518
519 if !line.starts_with("data: ") {
520 continue;
521 }
522 let payload = &line["data: ".len()..];
523
524 if payload == "[DONE]" {
525 let ev = StreamEvent {
526 event_type: "done".to_string(),
527 delta: None,
528 tool_use: None,
529 tool_use_start: None,
530 tool_use_input_delta: None,
531 tool_use_complete: None,
532 usage: None,
533 error: None,
534 done: true,
535 };
536 return Some((ev, lines));
537 }
538
539 let raw: RawStreamEvent = match serde_json::from_str(payload) {
540 Ok(r) => r,
541 Err(e) => {
542 let ev = StreamEvent {
543 event_type: "error".to_string(),
544 delta: None,
545 tool_use: None,
546 tool_use_start: None,
547 tool_use_input_delta: None,
548 tool_use_complete: None,
549 usage: None,
550 error: Some(format!("parse SSE: {e}")),
551 done: false,
552 };
553 return Some((ev, lines));
554 }
555 };
556
557 let mut ev = StreamEvent {
558 event_type: raw.event_type.clone(),
559 delta: None,
560 tool_use: None,
561 tool_use_start: None,
562 tool_use_input_delta: None,
563 tool_use_complete: None,
564 usage: None,
565 error: None,
566 done: false,
567 };
568
569 match raw.event_type.as_str() {
570 "content_delta" | "thinking_delta" => {
571 ev.delta = raw.delta;
572 }
573 "tool_use" => {
574 ev.tool_use = Some(StreamToolUse {
577 id: raw.id.unwrap_or_default(),
578 name: raw.name.unwrap_or_default(),
579 input: raw.input.unwrap_or_default(),
580 });
581 }
582 "tool_use_start" => {
583 ev.tool_use_start = Some(StreamToolUseStart {
584 id: raw.id.unwrap_or_default(),
585 name: raw.name.unwrap_or_default(),
586 });
587 }
588 "tool_use_input_delta" => {
589 ev.tool_use_input_delta = Some(StreamToolUseInputDelta {
590 id: raw.id.unwrap_or_default(),
591 partial_json: raw.partial_json.unwrap_or_default(),
592 });
593 }
594 "tool_use_complete" => {
595 ev.tool_use_complete = Some(StreamToolUseComplete {
596 id: raw.id.unwrap_or_default(),
597 name: raw.name.unwrap_or_default(),
598 input: raw.input.unwrap_or_default(),
599 });
600 }
601 "usage" => {
602 ev.usage = Some(ChatUsage {
603 input_tokens: raw.input_tokens.unwrap_or(0),
604 output_tokens: raw.output_tokens.unwrap_or(0),
605 cost_ticks: raw.cost_ticks.unwrap_or(0),
606 });
607 }
608 "error" => {
609 ev.error = raw.message;
610 }
611 "heartbeat" => {}
612 _ => {}
613 }
614
615 return Some((ev, lines));
616 }
617 })
618}