openai-core 0.1.1

Rust SDK for OpenAI-compatible ecosystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
//! Chat namespace implementations, builders, and tool-runner helpers.

use std::collections::BTreeMap;
#[cfg(feature = "structured-output")]
use std::marker::PhantomData;
use std::time::Duration;

use bytes::Bytes;
use http::Method;
#[cfg(feature = "structured-output")]
use schemars::JsonSchema;
#[cfg(feature = "tool-runner")]
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
use tokio_util::sync::CancellationToken;

use crate::Client;
use crate::config::RequestOptions;
use crate::error::{Error, Result};
use crate::generated::endpoints;
#[cfg(feature = "structured-output")]
use crate::helpers::{ParsedChatCompletion, parse_json_payload};
#[cfg(feature = "tool-runner")]
use crate::helpers::{ToolDefinition, ToolRegistry};
use crate::json_payload::JsonPayload;
use crate::response_meta::ApiResponse;
#[cfg(feature = "tool-runner")]
use crate::stream::ChatCompletionRuntimeEvent;
use crate::stream::{
    AssistantEventStream, AssistantStream, ChatCompletionEventStream, ChatCompletionStream,
};
use crate::transport::{RequestSpec, merge_json_body};
#[cfg(feature = "tool-runner")]
use futures_util::StreamExt;

#[cfg(feature = "tool-runner")]
use super::ChatCompletionToolCall;
#[cfg(feature = "tool-runner")]
use super::CompletionUsage;
use super::{
    ChatCompletion, ChatCompletionCreateParams, ChatCompletionMessage,
    ChatCompletionMessagesResource, ChatCompletionStoreContentPart, ChatCompletionsResource,
    ChatResource, ChatToolChoice, ChatToolDefinition, DeleteResponse, JsonRequestBuilder,
    ListRequestBuilder, encode_path_segment, value_from,
};

/// 表示已存储 chat completion 下的消息对象。
#[derive(Debug, Clone, Serialize, serde::Deserialize, Default)]
pub struct ChatCompletionStoreMessage {
    /// 消息 ID。
    pub id: String,
    /// 角色。
    #[serde(default)]
    pub role: String,
    /// 文本内容。
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<String>,
    /// content parts。
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub content_parts: Vec<ChatCompletionStoreContentPart>,
    /// 工具调用。
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tool_calls: Vec<super::ChatCompletionToolCall>,
    /// 额外字段。
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl ChatResource {
    /// 返回聊天补全资源。
    pub fn completions(&self) -> ChatCompletionsResource {
        ChatCompletionsResource::new(self.client.clone())
    }
}

impl ChatCompletionsResource {
    /// 创建聊天补全请求构建器。
    pub fn create(&self) -> ChatCompletionCreateRequestBuilder {
        ChatCompletionCreateRequestBuilder::new(self.client.clone())
    }

    /// 创建聊天补全流式请求构建器。
    pub fn stream(&self) -> ChatCompletionStreamRequestBuilder {
        ChatCompletionStreamRequestBuilder::new(self.client.clone())
    }

    /// 创建结构化解析请求构建器。
    #[cfg(feature = "structured-output")]
    #[cfg_attr(docsrs, doc(cfg(feature = "structured-output")))]
    pub fn parse<T>(&self) -> ChatCompletionParseRequestBuilder<T> {
        ChatCompletionParseRequestBuilder::new(self.client.clone())
    }

    /// 创建工具运行构建器。
    #[cfg(feature = "tool-runner")]
    #[cfg_attr(docsrs, doc(cfg(feature = "tool-runner")))]
    pub fn run_tools(&self) -> ChatCompletionRunToolsRequestBuilder {
        ChatCompletionRunToolsRequestBuilder::new(self.client.clone())
    }

    /// 根据 ID 获取聊天补全对象。
    pub fn retrieve(&self, id: impl Into<String>) -> JsonRequestBuilder<ChatCompletion> {
        JsonRequestBuilder::new(
            self.client.clone(),
            "chat.completions.retrieve",
            Method::GET,
            format!("/chat/completions/{}", encode_path_segment(id.into())),
        )
    }

    /// 更新聊天补全对象。
    pub fn update(&self, id: impl Into<String>) -> JsonRequestBuilder<ChatCompletion> {
        JsonRequestBuilder::new(
            self.client.clone(),
            "chat.completions.update",
            Method::POST,
            format!("/chat/completions/{}", encode_path_segment(id.into())),
        )
    }

    /// 列出聊天补全对象。
    pub fn list(&self) -> ListRequestBuilder<ChatCompletion> {
        ListRequestBuilder::new(
            self.client.clone(),
            "chat.completions.list",
            "/chat/completions",
        )
    }

    /// 删除聊天补全对象。
    pub fn delete(&self, id: impl Into<String>) -> JsonRequestBuilder<DeleteResponse> {
        JsonRequestBuilder::new(
            self.client.clone(),
            "chat.completions.delete",
            Method::DELETE,
            format!("/chat/completions/{}", encode_path_segment(id.into())),
        )
    }

    /// 返回聊天补全消息子资源。
    pub fn messages(&self) -> ChatCompletionMessagesResource {
        ChatCompletionMessagesResource::new(self.client.clone())
    }
}

impl ChatCompletionMessagesResource {
    /// 列出某个聊天补全下的消息。
    pub fn list(
        &self,
        completion_id: impl Into<String>,
    ) -> ListRequestBuilder<ChatCompletionStoreMessage> {
        let endpoint = endpoints::chat::CHAT_COMPLETIONS_MESSAGES_LIST;
        ListRequestBuilder::new(
            self.client.clone(),
            endpoint.id,
            endpoint.render(&[("completion_id", &encode_path_segment(completion_id.into()))]),
        )
    }
}

/// 表示聊天补全创建构建器。
#[derive(Debug, Clone, Default)]
pub struct ChatCompletionCreateRequestBuilder {
    client: Option<Client>,
    pub(crate) params: ChatCompletionCreateParams,
    options: RequestOptions,
    extra_body: BTreeMap<String, Value>,
    provider_options: BTreeMap<String, Value>,
}

impl ChatCompletionCreateRequestBuilder {
    pub(crate) fn new(client: Client) -> Self {
        Self {
            client: Some(client),
            ..Self::default()
        }
    }

    /// 设置模型。
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.params.model = Some(model.into());
        self
    }

    /// 直接设置消息列表。
    pub fn messages(mut self, messages: Vec<ChatCompletionMessage>) -> Self {
        self.params.messages = messages;
        self
    }

    /// 追加一条 system 消息。
    pub fn message_system(mut self, content: impl Into<String>) -> Self {
        self.params
            .messages
            .push(ChatCompletionMessage::system(content));
        self
    }

    /// 追加一条 user 消息。
    pub fn message_user(mut self, content: impl Into<String>) -> Self {
        self.params
            .messages
            .push(ChatCompletionMessage::user(content));
        self
    }

    /// 追加一条 assistant 消息。
    pub fn message_assistant(mut self, content: impl Into<String>) -> Self {
        self.params
            .messages
            .push(ChatCompletionMessage::assistant(content));
        self
    }

    /// 设置温度。
    pub fn temperature(mut self, temperature: f32) -> Self {
        self.params.temperature = Some(temperature);
        self
    }

    /// 设置候选数量。
    pub fn n(mut self, n: u32) -> Self {
        self.params.n = Some(n);
        self
    }

    /// 设置最大 token 数。
    pub fn max_tokens(mut self, max_tokens: u32) -> Self {
        self.params.max_tokens = Some(max_tokens);
        self
    }

    /// 追加工具定义。
    pub fn tool(mut self, tool: ChatToolDefinition) -> Self {
        self.params.tools.push(tool);
        self
    }

    /// 设置工具选择策略。
    pub fn tool_choice(mut self, tool_choice: impl Into<ChatToolChoice>) -> Self {
        self.params.tool_choice = Some(tool_choice.into());
        self
    }

    /// 设置附加请求头。
    pub fn extra_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.options.insert_header(key, value);
        self
    }

    /// 设置附加查询参数。
    pub fn extra_query(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.options.insert_query(key, value);
        self
    }

    /// 在请求体根对象中追加字段。
    pub fn extra_body(mut self, key: impl Into<String>, value: impl Into<JsonPayload>) -> Self {
        self.extra_body.insert(key.into(), value.into().into_raw());
        self
    }

    /// 在 provider 对应的 `provider_options` 节点下追加字段。
    pub fn provider_option(
        mut self,
        key: impl Into<String>,
        value: impl Into<JsonPayload>,
    ) -> Self {
        self.provider_options
            .insert(key.into(), value.into().into_raw());
        self
    }

    /// 覆盖超时时间。
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.options.timeout = Some(timeout);
        self
    }

    /// 设置取消令牌。
    pub fn cancellation_token(mut self, token: CancellationToken) -> Self {
        self.options.cancellation_token = Some(token);
        self
    }

    pub(crate) fn build_spec(mut self, stream: bool) -> Result<(Client, RequestSpec)> {
        let client = self
            .client
            .take()
            .ok_or_else(|| Error::InvalidConfig("聊天补全构建器缺少客户端".into()))?;
        if self.params.model.as_deref().unwrap_or_default().is_empty() {
            return Err(Error::MissingRequiredField { field: "model" });
        }
        if self.params.messages.is_empty() {
            return Err(Error::MissingRequiredField { field: "messages" });
        }
        self.params.stream = Some(stream);
        let provider_key = client.provider().kind().as_key();
        let body = merge_json_body(
            Some(value_from(&self.params)?),
            &self.extra_body,
            provider_key,
            &self.provider_options,
        );
        let mut spec = RequestSpec::new(
            if stream {
                "chat.completions.stream"
            } else {
                "chat.completions.create"
            },
            Method::POST,
            "/chat/completions",
        );
        spec.body = Some(body);
        spec.options = self.options;
        Ok((client, spec))
    }

    /// 发送普通聊天补全请求。
    ///
    /// # Errors
    ///
    /// 当参数校验失败、请求失败或反序列化失败时返回错误。
    pub async fn send(self) -> Result<ChatCompletion> {
        Ok(self.send_with_meta().await?.data)
    }

    /// 发送普通聊天补全请求并保留元信息。
    ///
    /// # Errors
    ///
    /// 当参数校验失败、请求失败或反序列化失败时返回错误。
    pub async fn send_with_meta(self) -> Result<ApiResponse<ChatCompletion>> {
        let (client, spec) = self.build_spec(false)?;
        client.execute_json(spec).await
    }

    /// 发送普通聊天补全请求并返回原始 HTTP 响应。
    ///
    /// # Errors
    ///
    /// 当参数校验失败或请求失败时返回错误。
    pub async fn send_raw(self) -> Result<http::Response<Bytes>> {
        let (client, spec) = self.build_spec(false)?;
        client.execute_raw_http(spec).await
    }
}

/// 表示聊天补全流式请求构建器。
#[derive(Debug, Clone)]
pub struct ChatCompletionStreamRequestBuilder {
    inner: ChatCompletionCreateRequestBuilder,
}

/// 表示 Assistants/Beta Threads 流式请求构建器。
#[derive(Debug, Clone)]
pub struct AssistantStreamRequestBuilder {
    inner: JsonRequestBuilder<Value>,
}

impl ChatCompletionStreamRequestBuilder {
    pub(crate) fn new(client: Client) -> Self {
        Self {
            inner: ChatCompletionCreateRequestBuilder::new(client),
        }
    }

    /// 设置模型。
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.inner = self.inner.model(model);
        self
    }

    /// 设置消息列表。
    pub fn messages(mut self, messages: Vec<ChatCompletionMessage>) -> Self {
        self.inner = self.inner.messages(messages);
        self
    }

    /// 追加一条 system 消息。
    pub fn message_system(mut self, content: impl Into<String>) -> Self {
        self.inner = self.inner.message_system(content);
        self
    }

    /// 追加一条 user 消息。
    pub fn message_user(mut self, content: impl Into<String>) -> Self {
        self.inner = self.inner.message_user(content);
        self
    }

    /// 追加一条 assistant 消息。
    pub fn message_assistant(mut self, content: impl Into<String>) -> Self {
        self.inner = self.inner.message_assistant(content);
        self
    }

    /// 设置温度。
    pub fn temperature(mut self, temperature: f32) -> Self {
        self.inner = self.inner.temperature(temperature);
        self
    }

    /// 设置候选数量。
    pub fn n(mut self, n: u32) -> Self {
        self.inner = self.inner.n(n);
        self
    }

    /// 设置最大 token 数。
    pub fn max_tokens(mut self, max_tokens: u32) -> Self {
        self.inner = self.inner.max_tokens(max_tokens);
        self
    }

    /// 添加额外字段。
    pub fn extra_body(mut self, key: impl Into<String>, value: impl Into<JsonPayload>) -> Self {
        self.inner = self.inner.extra_body(key, value);
        self
    }

    /// 添加 provider 选项。
    pub fn provider_option(
        mut self,
        key: impl Into<String>,
        value: impl Into<JsonPayload>,
    ) -> Self {
        self.inner = self.inner.provider_option(key, value);
        self
    }

    /// 发送流式聊天补全请求。
    ///
    /// # Errors
    ///
    /// 当参数校验失败、请求失败或流初始化失败时返回错误。
    pub async fn send(self) -> Result<ChatCompletionStream> {
        let (client, spec) = self.inner.build_spec(true)?;
        Ok(ChatCompletionStream::new(client.execute_sse(spec).await?))
    }

    /// 发送流式聊天补全请求,并返回带高层语义事件的运行时流。
    ///
    /// # Errors
    ///
    /// 当参数校验失败、请求失败或流初始化失败时返回错误。
    pub async fn send_events(self) -> Result<ChatCompletionEventStream> {
        Ok(self.send().await?.events())
    }
}

impl AssistantStreamRequestBuilder {
    pub(crate) fn new(
        client: Client,
        endpoint_id: &'static str,
        method: Method,
        path: impl Into<String>,
    ) -> Self {
        Self {
            inner: JsonRequestBuilder::new(client, endpoint_id, method, path),
        }
    }

    /// 设置整个请求体为一个 `serde_json::Value`。
    pub fn body_value(mut self, body: impl Into<JsonPayload>) -> Self {
        self.inner = self.inner.body_value(body);
        self
    }

    /// 使用任意可序列化对象设置请求体。
    ///
    /// # Errors
    ///
    /// 当序列化失败时返回错误。
    pub fn json_body<U>(mut self, body: &U) -> Result<Self>
    where
        U: Serialize,
    {
        self.inner = self.inner.json_body(body)?;
        Ok(self)
    }

    /// 添加一个额外请求头。
    pub fn extra_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.inner = self.inner.extra_header(key, value);
        self
    }

    /// 删除一个默认请求头。
    pub fn remove_header(mut self, key: impl Into<String>) -> Self {
        self.inner = self.inner.remove_header(key);
        self
    }

    /// 添加一个额外查询参数。
    pub fn extra_query(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.inner = self.inner.extra_query(key, value);
        self
    }

    /// 在 JSON 根对象中追加字段。
    pub fn extra_body(mut self, key: impl Into<String>, value: impl Into<JsonPayload>) -> Self {
        self.inner = self.inner.extra_body(key, value);
        self
    }

    /// 在 provider 对应的 `provider_options` 下追加字段。
    pub fn provider_option(
        mut self,
        key: impl Into<String>,
        value: impl Into<JsonPayload>,
    ) -> Self {
        self.inner = self.inner.provider_option(key, value);
        self
    }

    /// 覆盖请求超时时间。
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.inner = self.inner.timeout(timeout);
        self
    }

    /// 覆盖最大重试次数。
    pub fn max_retries(mut self, max_retries: u32) -> Self {
        self.inner = self.inner.max_retries(max_retries);
        self
    }

    /// 设置取消令牌。
    pub fn cancellation_token(mut self, token: CancellationToken) -> Self {
        self.inner = self.inner.cancellation_token(token);
        self
    }

    /// 发送流式 Assistants 请求。
    ///
    /// # Errors
    ///
    /// 当请求失败或流初始化失败时返回错误。
    pub async fn send(self) -> Result<AssistantStream> {
        let client = self.inner.client.clone();
        let stream = client.execute_raw_sse(self.inner.into_spec()).await?;
        Ok(AssistantStream::new(stream))
    }

    /// 发送流式 Assistants 请求,并返回带高层派生事件的运行时流。
    ///
    /// # Errors
    ///
    /// 当请求失败或流初始化失败时返回错误。
    pub async fn send_events(self) -> Result<AssistantEventStream> {
        Ok(self.send().await?.events())
    }
}

/// 表示聊天补全结构化解析构建器。
#[cfg(feature = "structured-output")]
#[derive(Debug, Clone)]
pub struct ChatCompletionParseRequestBuilder<T> {
    inner: ChatCompletionCreateRequestBuilder,
    _marker: PhantomData<T>,
}

#[cfg(feature = "structured-output")]
impl<T> ChatCompletionParseRequestBuilder<T> {
    pub(crate) fn new(client: Client) -> Self {
        Self {
            inner: ChatCompletionCreateRequestBuilder::new(client),
            _marker: PhantomData,
        }
    }

    /// 设置模型。
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.inner = self.inner.model(model);
        self
    }

    /// 设置消息列表。
    pub fn messages(mut self, messages: Vec<ChatCompletionMessage>) -> Self {
        self.inner = self.inner.messages(messages);
        self
    }

    /// 追加一条 user 消息。
    pub fn message_user(mut self, content: impl Into<String>) -> Self {
        self.inner = self.inner.message_user(content);
        self
    }

    /// 追加一个额外请求体字段。
    pub fn extra_body(mut self, key: impl Into<String>, value: impl Into<JsonPayload>) -> Self {
        self.inner = self.inner.extra_body(key, value);
        self
    }
}

#[cfg(feature = "structured-output")]
impl<T> ChatCompletionParseRequestBuilder<T>
where
    T: JsonSchema + serde::de::DeserializeOwned,
{
    /// 发送请求并把首条 assistant 文本解析成结构化对象。
    ///
    /// # Errors
    ///
    /// 当模型返回内容为空或 JSON 解析失败时返回错误。
    pub async fn send(self) -> Result<ParsedChatCompletion<T>> {
        let response = self.inner.send().await?;
        response.ensure_not_truncated()?;
        let choice = response
            .choices
            .first()
            .ok_or_else(|| Error::InvalidConfig("聊天补全返回中缺少 choice".into()))?;
        let parsed = if let Some(content) = choice.message.content.as_deref() {
            parse_json_payload(content)?
        } else if let Some(parsed_arguments) = choice.message.parse_tool_arguments()? {
            parsed_arguments
        } else {
            return Err(Error::InvalidConfig(
                "聊天补全返回中既没有 assistant 文本,也没有可解析的工具参数".into(),
            ));
        };
        Ok(ParsedChatCompletion { response, parsed })
    }
}

/// 表示工具运行构建器。
#[cfg(feature = "tool-runner")]
#[derive(Debug, Clone)]
pub struct ChatCompletionRunToolsRequestBuilder {
    inner: ChatCompletionCreateRequestBuilder,
    registry: ToolRegistry,
    max_rounds: usize,
}

#[cfg(feature = "tool-runner")]
impl ChatCompletionRunToolsRequestBuilder {
    pub(crate) fn new(client: Client) -> Self {
        Self {
            inner: ChatCompletionCreateRequestBuilder::new(client),
            registry: ToolRegistry::new(),
            max_rounds: 8,
        }
    }

    /// 设置模型。
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.inner = self.inner.model(model);
        self
    }

    /// 设置消息列表。
    pub fn messages(mut self, messages: Vec<ChatCompletionMessage>) -> Self {
        self.inner = self.inner.messages(messages);
        self
    }

    /// 追加一条 user 消息。
    pub fn message_user(mut self, content: impl Into<String>) -> Self {
        self.inner = self.inner.message_user(content);
        self
    }

    /// 注册一个工具。
    pub fn register_tool(mut self, tool: ToolDefinition) -> Self {
        self.registry.register(tool);
        self
    }

    /// 设置最大工具轮次。
    pub fn max_rounds(mut self, max_rounds: usize) -> Self {
        self.max_rounds = max_rounds;
        self
    }

    /// 发送请求并返回工具调用运行 trace。
    ///
    /// # Errors
    ///
    /// 当工具不存在、工具执行失败或请求失败时返回错误。
    pub async fn into_runner(self) -> Result<ChatCompletionRunner> {
        let execution = self.execute(false).await?;
        Ok(ChatCompletionRunner::from_execution(execution))
    }

    /// 使用流式请求执行工具调用,并返回包含流式事件的运行 trace。
    ///
    /// # Errors
    ///
    /// 当工具不存在、工具执行失败、流式请求失败或结束原因不可解析时返回错误。
    pub async fn into_streaming_runner(self) -> Result<ChatCompletionStreamingRunner> {
        let execution = self.execute(true).await?;
        Ok(ChatCompletionStreamingRunner::from_execution(execution))
    }

    /// 发送请求并自动处理工具调用。
    ///
    /// # Errors
    ///
    /// 当工具不存在、工具执行失败或请求失败时返回错误。
    pub async fn send(self) -> Result<ChatCompletion> {
        self.into_runner()
            .await?
            .final_chat_completion()
            .cloned()
            .ok_or_else(|| Error::InvalidConfig("工具运行未返回最终聊天补全结果".into()))
    }

    /// 使用流式请求执行工具调用轮次,并返回最终聊天补全结果。
    ///
    /// # Errors
    ///
    /// 当工具不存在、工具执行失败、流式请求失败或结束原因不可解析时返回错误。
    pub async fn send_streaming(self) -> Result<ChatCompletion> {
        self.into_streaming_runner()
            .await?
            .final_chat_completion()
            .cloned()
            .ok_or_else(|| Error::InvalidConfig("工具运行未返回最终聊天补全结果".into()))
    }

    async fn execute(self, stream: bool) -> Result<ChatCompletionRunExecution> {
        let mut inner = self.inner;
        if inner.params.tools.is_empty() {
            inner.params.tools = self
                .registry
                .all()
                .map(ChatToolDefinition::from_tool)
                .collect();
        }

        let mut messages = inner.params.messages.clone();
        let mut execution = ChatCompletionRunExecution {
            messages: messages.clone(),
            ..ChatCompletionRunExecution::default()
        };
        for _ in 0..self.max_rounds {
            let request = ChatCompletionCreateRequestBuilder {
                params: ChatCompletionCreateParams {
                    messages: messages.clone(),
                    ..inner.params.clone()
                },
                ..inner.clone()
            };
            let response = if stream {
                let (client, spec) = request.build_spec(true)?;
                let mut event_stream =
                    ChatCompletionStream::new(client.execute_sse(spec).await?).events();
                while let Some(event) = event_stream.next().await {
                    execution.stream_events.push(event?);
                }
                let response = event_stream
                    .snapshot()
                    .ok_or_else(|| Error::InvalidConfig("流式聊天补全未返回最终结果".into()))?;
                response.ensure_not_truncated()?;
                response
            } else {
                request.send().await?
            };
            execution.chat_completions.push(response.clone());
            let Some(choice) = response.choices.first() else {
                return Ok(execution);
            };
            response.ensure_not_truncated()?;
            execution.messages.push(choice.message.clone());
            if choice.message.tool_calls.is_empty() {
                return Ok(execution);
            }

            messages.push(choice.message.clone());

            for tool_call in &choice.message.tool_calls {
                let tool = self.registry.get(&tool_call.function.name).ok_or_else(|| {
                    Error::InvalidConfig(format!("未注册工具: {}", tool_call.function.name))
                })?;
                let arguments = if tool_call.function.arguments.trim().is_empty() {
                    Value::Object(Default::default())
                } else {
                    serde_json::from_str(&tool_call.function.arguments)
                        .unwrap_or_else(|_| Value::String(tool_call.function.arguments.clone()))
                };
                let output = tool.invoke(arguments).await?;
                let content = if output.is_string() {
                    output.as_str().unwrap_or_default().to_owned()
                } else {
                    output.to_string()
                };
                let tool_message =
                    ChatCompletionMessage::tool(tool_call.id.clone(), content.clone());
                messages.push(tool_message.clone());
                execution.messages.push(tool_message);
                execution.tool_results.push(ChatCompletionToolResult {
                    tool_call: tool_call.clone(),
                    output: content,
                });
            }
        }

        Err(Error::InvalidConfig("工具调用轮次已超过上限".into()))
    }
}

/// 表示一次工具调用返回的结果。
#[cfg(feature = "tool-runner")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatCompletionToolResult {
    /// 对应的工具调用。
    pub tool_call: ChatCompletionToolCall,
    /// 工具执行输出文本。
    pub output: String,
}

#[cfg(feature = "tool-runner")]
#[derive(Debug, Clone, Default)]
struct ChatCompletionRunExecution {
    messages: Vec<ChatCompletionMessage>,
    chat_completions: Vec<ChatCompletion>,
    tool_results: Vec<ChatCompletionToolResult>,
    stream_events: Vec<ChatCompletionRuntimeEvent>,
}

/// 表示非流式工具调用运行 trace。
#[cfg(feature = "tool-runner")]
#[derive(Debug, Clone, Default)]
pub struct ChatCompletionRunner {
    messages: Vec<ChatCompletionMessage>,
    chat_completions: Vec<ChatCompletion>,
    tool_results: Vec<ChatCompletionToolResult>,
}

#[cfg(feature = "tool-runner")]
impl ChatCompletionRunner {
    fn from_execution(execution: ChatCompletionRunExecution) -> Self {
        Self {
            messages: execution.messages,
            chat_completions: execution.chat_completions,
            tool_results: execution.tool_results,
        }
    }

    /// 返回运行过程中累积的全部消息。
    pub fn messages(&self) -> &[ChatCompletionMessage] {
        &self.messages
    }

    /// 返回运行过程中累积的全部聊天补全。
    pub fn all_chat_completions(&self) -> &[ChatCompletion] {
        &self.chat_completions
    }

    /// 返回运行过程中累积的全部工具调用结果。
    pub fn tool_results(&self) -> &[ChatCompletionToolResult] {
        &self.tool_results
    }

    /// 返回最终聊天补全结果。
    pub fn final_chat_completion(&self) -> Option<&ChatCompletion> {
        self.chat_completions.last()
    }

    /// 返回最终 assistant 消息。
    pub fn final_message(&self) -> Option<&ChatCompletionMessage> {
        self.messages
            .iter()
            .rev()
            .find(|message| message.role == "assistant")
    }

    /// 返回最终 assistant 文本。
    pub fn final_content(&self) -> Option<&str> {
        self.final_message()
            .and_then(|message| message.content.as_deref())
    }

    /// 返回最终工具调用。
    pub fn final_function_tool_call(&self) -> Option<&ChatCompletionToolCall> {
        self.tool_results.last().map(|result| &result.tool_call)
    }

    /// 返回最终工具调用结果。
    pub fn final_function_tool_call_result(&self) -> Option<&str> {
        self.tool_results
            .last()
            .map(|result| result.output.as_str())
    }

    /// 汇总所有补全响应中的 usage 字段。
    pub fn total_usage(&self) -> Option<CompletionUsage> {
        let mut completion_tokens = 0u64;
        let mut prompt_tokens = 0u64;
        let mut total_tokens = 0u64;
        let mut found = false;
        for completion in &self.chat_completions {
            let Some(usage) = completion.usage.as_ref() else {
                continue;
            };
            completion_tokens += usage.completion_tokens;
            prompt_tokens += usage.prompt_tokens;
            total_tokens += usage.total_tokens;
            found = true;
        }

        found.then(|| CompletionUsage {
            completion_tokens,
            prompt_tokens,
            total_tokens,
            ..CompletionUsage::default()
        })
    }
}

/// 表示流式工具调用运行 trace。
#[cfg(feature = "tool-runner")]
#[derive(Debug, Clone, Default)]
pub struct ChatCompletionStreamingRunner {
    runner: ChatCompletionRunner,
    stream_events: Vec<ChatCompletionRuntimeEvent>,
}

#[cfg(feature = "tool-runner")]
impl ChatCompletionStreamingRunner {
    fn from_execution(execution: ChatCompletionRunExecution) -> Self {
        Self {
            runner: ChatCompletionRunner::from_execution(ChatCompletionRunExecution {
                messages: execution.messages,
                chat_completions: execution.chat_completions,
                tool_results: execution.tool_results,
                stream_events: Vec::new(),
            }),
            stream_events: execution.stream_events,
        }
    }

    /// 返回流式运行过程中产生的全部高层事件。
    pub fn events(&self) -> &[ChatCompletionRuntimeEvent] {
        &self.stream_events
    }

    /// 返回运行过程中累积的全部消息。
    pub fn messages(&self) -> &[ChatCompletionMessage] {
        self.runner.messages()
    }

    /// 返回运行过程中累积的全部聊天补全。
    pub fn all_chat_completions(&self) -> &[ChatCompletion] {
        self.runner.all_chat_completions()
    }

    /// 返回运行过程中累积的全部工具调用结果。
    pub fn tool_results(&self) -> &[ChatCompletionToolResult] {
        self.runner.tool_results()
    }

    /// 返回最终聊天补全结果。
    pub fn final_chat_completion(&self) -> Option<&ChatCompletion> {
        self.runner.final_chat_completion()
    }

    /// 返回最终 assistant 消息。
    pub fn final_message(&self) -> Option<&ChatCompletionMessage> {
        self.runner.final_message()
    }

    /// 返回最终 assistant 文本。
    pub fn final_content(&self) -> Option<&str> {
        self.runner.final_content()
    }

    /// 返回最终工具调用。
    pub fn final_function_tool_call(&self) -> Option<&ChatCompletionToolCall> {
        self.runner.final_function_tool_call()
    }

    /// 返回最终工具调用结果。
    pub fn final_function_tool_call_result(&self) -> Option<&str> {
        self.runner.final_function_tool_call_result()
    }

    /// 汇总所有补全响应中的 usage 字段。
    pub fn total_usage(&self) -> Option<CompletionUsage> {
        self.runner.total_usage()
    }
}