edge-completions 0.4.0

Typed Rust SDK and CLI for OpenAI-compatible chat completions through Cloudflare
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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
use std::{fmt, marker::PhantomData, str::FromStr};

use schemars::{JsonSchema, Schema};
use serde::{Deserialize, Deserializer, Serialize, de};

use crate::{Error, InvalidConfiguration, ToolError};

/// A validated provider model identifier, such as `moonshotai/kimi-k3`.
///
/// ```
/// use edge_completions::ModelId;
///
/// let model = ModelId::new("moonshotai/kimi-k3")?;
/// assert_eq!(model.as_str(), "moonshotai/kimi-k3");
/// # Ok::<(), edge_completions::InvalidConfiguration>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
#[serde(transparent)]
pub struct ModelId(String);

impl ModelId {
    /// Returns the supported Kimi K3 model identifier.
    #[must_use]
    pub fn kimi_k3() -> Self {
        Self("moonshotai/kimi-k3".to_owned())
    }

    /// Validates and constructs a model identifier.
    ///
    /// Leading and trailing whitespace is removed before storage.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidConfiguration::EmptyModelId`] when the trimmed value is
    /// empty.
    pub fn new(value: impl Into<String>) -> Result<Self, InvalidConfiguration> {
        let value = value.into();
        let trimmed = value.trim();
        if trimmed.is_empty() {
            return Err(InvalidConfiguration::EmptyModelId);
        }
        Ok(Self(trimmed.to_owned()))
    }

    /// Returns the validated provider model identifier.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for ModelId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.0)
    }
}

impl<'de> Deserialize<'de> for ModelId {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let value = String::deserialize(deserializer)?;
        Self::new(value).map_err(de::Error::custom)
    }
}

impl TryFrom<&str> for ModelId {
    type Error = InvalidConfiguration;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl FromStr for ModelId {
    type Err = InvalidConfiguration;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::new(value)
    }
}

/// A typed chat message accepted by the provider request contract.
///
/// Use the role-specific constructors instead of constructing provider payloads
/// directly:
///
/// ```
/// use edge_completions::ChatMessage;
///
/// let message = ChatMessage::user("Explain trait boundaries.");
/// assert_eq!(message, ChatMessage::user("Explain trait boundaries."));
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ChatMessage(MessagePayload);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "role", rename_all = "lowercase")]
enum MessagePayload {
    System {
        content: String,
    },
    User {
        content: String,
    },
    Assistant {
        content: Option<String>,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        tool_calls: Vec<ToolCall>,
    },
    Tool {
        tool_call_id: ToolCallId,
        content: String,
    },
}

impl ChatMessage {
    /// Creates a system instruction message.
    #[must_use]
    pub fn system(content: impl Into<String>) -> Self {
        Self(MessagePayload::System {
            content: content.into(),
        })
    }

    /// Creates a user message.
    #[must_use]
    pub fn user(content: impl Into<String>) -> Self {
        Self(MessagePayload::User {
            content: content.into(),
        })
    }

    /// Creates an assistant text message.
    #[must_use]
    pub fn assistant(content: impl Into<String>) -> Self {
        Self(MessagePayload::Assistant {
            content: Some(content.into()),
            tool_calls: Vec::new(),
        })
    }

    /// Replays provider-proposed tool calls in a follow-up request.
    #[must_use]
    pub fn assistant_tool_calls(tool_calls: Vec<ToolCall>) -> Self {
        Self(MessagePayload::Assistant {
            content: None,
            tool_calls,
        })
    }

    /// Encodes a typed tool result after confirming that the call name matches `T`.
    ///
    /// Prefer [`ValidatedToolCall::result`] after calling
    /// [`ToolCall::validate`]. That path carries the successful name and
    /// argument validation in the type system.
    ///
    /// # Errors
    ///
    /// Returns [`ToolError::UnexpectedName`] when the proposed function name
    /// does not match `T::NAME`, or [`ToolError::ResultEncoding`] when the typed
    /// output cannot be serialized.
    pub fn tool_result<T: ToolDefinition>(
        call: &ToolCall,
        result: &T::Output,
    ) -> Result<Self, ToolError> {
        call.ensure_name::<T>()?;
        let content =
            serde_json::to_string(result).map_err(|source| ToolError::ResultEncoding {
                tool: call.name().to_owned(),
                source,
            })?;
        Ok(Self(MessagePayload::Tool {
            tool_call_id: call.id.clone(),
            content,
        }))
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(transparent)]
struct ToolCallId(String);

impl<'de> Deserialize<'de> for ToolCallId {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let value = String::deserialize(deserializer)?;
        if value.trim().is_empty() {
            return Err(de::Error::custom("tool-call ID must not be empty"));
        }
        Ok(Self(value))
    }
}

#[derive(Debug, Clone, PartialEq, Serialize)]
struct FunctionDefinition {
    name: String,
    description: String,
    parameters: Schema,
}

impl FunctionDefinition {
    fn new(name: impl Into<String>, description: impl Into<String>, parameters: Schema) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            parameters,
        }
    }
}

/// A typed tool contract used for schema generation, argument decoding, and result encoding.
///
/// The associated types ensure that one tool name, one validated argument type,
/// and one result type travel together through the SDK.
///
/// ```
/// use edge_completions::ToolDefinition;
/// use schemars::JsonSchema;
/// use serde::{Deserialize, Serialize};
///
/// struct LookupWeather;
///
/// #[derive(Deserialize, JsonSchema)]
/// struct Arguments { city: String }
///
/// #[derive(Serialize)]
/// struct Report { temperature_celsius: i16 }
///
/// impl ToolDefinition for LookupWeather {
///     type Arguments = Arguments;
///     type Output = Report;
///     const NAME: &'static str = "lookup_weather";
///     const DESCRIPTION: &'static str = "Look up the weather for a city";
/// }
///
/// assert_eq!(LookupWeather::NAME, "lookup_weather");
/// ```
pub trait ToolDefinition {
    /// Validated application type decoded from model-produced JSON arguments.
    type Arguments: serde::de::DeserializeOwned + JsonSchema;

    /// Application type serialized into the tool-result message.
    type Output: Serialize;

    /// Stable function name exposed to the model.
    const NAME: &'static str;

    /// Clear function description exposed to the model.
    const DESCRIPTION: &'static str;
}

/// A validated OpenAI-compatible function-tool definition.
///
/// ```
/// # use edge_completions::{FunctionTool, ToolDefinition};
/// # use schemars::JsonSchema;
/// # use serde::{Deserialize, Serialize};
/// # struct LookupWeather;
/// # #[derive(Deserialize, JsonSchema)] struct Arguments { city: String }
/// # #[derive(Serialize)] struct Report { temperature_celsius: i16 }
/// # impl ToolDefinition for LookupWeather {
/// #     type Arguments = Arguments;
/// #     type Output = Report;
/// #     const NAME: &'static str = "lookup_weather";
/// #     const DESCRIPTION: &'static str = "Look up the weather for a city";
/// # }
/// let tool = FunctionTool::for_tool::<LookupWeather>()?;
/// assert_eq!(tool.name(), "lookup_weather");
/// # Ok::<(), edge_completions::ToolError>(())
/// ```
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct FunctionTool {
    #[serde(rename = "type")]
    kind: FunctionToolKind,
    function: FunctionDefinition,
}

impl FunctionTool {
    /// Generates a function definition from the typed contract `T`.
    ///
    /// # Errors
    ///
    /// Returns [`ToolError::InvalidDefinition`] when `T::NAME` or
    /// `T::DESCRIPTION` is empty after trimming.
    pub fn for_tool<T: ToolDefinition>() -> Result<Self, ToolError> {
        if T::NAME.trim().is_empty() {
            return Err(ToolError::InvalidDefinition { field: "name" });
        }
        if T::DESCRIPTION.trim().is_empty() {
            return Err(ToolError::InvalidDefinition {
                field: "description",
            });
        }
        Ok(Self {
            kind: FunctionToolKind::Function,
            function: FunctionDefinition::new(
                T::NAME,
                T::DESCRIPTION,
                schemars::schema_for!(T::Arguments),
            ),
        })
    }

    /// Returns the function name exposed by this definition.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.function.name
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
enum FunctionToolKind {
    Function,
}

/// Sampling temperature constrained to the provider's documented `[0, 2]` range.
///
/// ```
/// use edge_completions::Temperature;
///
/// let temperature = Temperature::new(0.2)?;
/// assert_eq!(temperature.value(), 0.2);
/// # Ok::<(), edge_completions::InvalidConfiguration>(())
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
#[serde(transparent)]
pub struct Temperature(f32);

impl Temperature {
    /// Creates a temperature in the inclusive range from zero to two.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidConfiguration::InvalidTemperature`] when `value` is not
    /// finite or falls outside the inclusive range `0.0..=2.0`.
    pub fn new(value: f32) -> Result<Self, InvalidConfiguration> {
        if value.is_finite() && (0.0..=2.0).contains(&value) {
            Ok(Self(value))
        } else {
            Err(InvalidConfiguration::InvalidTemperature { value })
        }
    }

    /// Returns the validated temperature.
    #[must_use]
    pub fn value(self) -> f32 {
        self.0
    }
}

/// A validated, non-zero completion-token limit.
///
/// ```
/// use edge_completions::MaxTokens;
///
/// let limit = MaxTokens::new(256)?;
/// assert_eq!(limit.value(), 256);
/// # Ok::<(), edge_completions::InvalidConfiguration>(())
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct MaxTokens(u32);

impl MaxTokens {
    /// Creates a non-zero completion-token limit.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidConfiguration::ZeroMaxTokens`] when `value` is zero.
    pub fn new(value: u32) -> Result<Self, InvalidConfiguration> {
        if value == 0 {
            Err(InvalidConfiguration::ZeroMaxTokens)
        } else {
            Ok(Self(value))
        }
    }

    /// Returns the validated token limit.
    #[must_use]
    pub fn value(self) -> u32 {
        self.0
    }
}

/// Provider setting for selecting function tools.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum ToolChoice {
    /// Do not let the model propose a tool call.
    None,
    /// Let the model choose whether to propose a tool call.
    Auto,
    /// Require the model to propose a tool call.
    Required,
}

/// Compile-time states used by [`ChatRequestBuilder`].
///
/// The traits are sealed: callers can name the states in generic APIs, but
/// only this crate can define new legal request states.
pub mod request_state {
    mod sealed {
        pub trait Sealed {}
    }

    /// A sealed witness for the message-cardinality state of a request builder.
    pub trait MessageState: sealed::Sealed {}

    /// A sealed witness for the tool-cardinality state of a request builder.
    pub trait ToolState: sealed::Sealed {}

    /// Witness that a request builder does not yet contain a message.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct NeedsMessage;

    /// Witness that a request builder contains at least one message.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct HasMessages;

    /// Witness that a request builder does not contain a tool definition.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct WithoutTools;

    /// Witness that a request builder contains at least one tool definition.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct WithTools;

    impl sealed::Sealed for NeedsMessage {}
    impl sealed::Sealed for HasMessages {}
    impl sealed::Sealed for WithoutTools {}
    impl sealed::Sealed for WithTools {}

    impl MessageState for NeedsMessage {}
    impl MessageState for HasMessages {}
    impl ToolState for WithoutTools {}
    impl ToolState for WithTools {}
}

use request_state::{HasMessages, MessageState, NeedsMessage, ToolState, WithTools, WithoutTools};

/// A typestate builder that makes invalid request-construction sequences fail
/// to compile.
///
/// `M` witnesses whether the builder has a message, and `T` witnesses whether
/// it has a tool. Methods consume one state and return the next legal state.
/// In categorical terms, the state types are objects and the available methods
/// are composable morphisms; Rust's trait bounds reject illegal compositions.
///
/// # Example
///
/// ```
/// use edge_completions::{ChatMessage, ChatRequest};
///
/// let request = ChatRequest::kimi_k3_builder()
///     .message(ChatMessage::user("Explain typestate briefly."))
///     .build();
///
/// assert_eq!(request.model().as_str(), "moonshotai/kimi-k3");
/// ```
///
/// # Compile-time guarantees
///
/// A request without a message has no `build` method:
///
/// ```compile_fail
/// use edge_completions::ChatRequest;
///
/// let _request = ChatRequest::kimi_k3_builder().build();
/// ```
///
/// A tool choice cannot be selected before a tool exists:
///
/// ```compile_fail
/// use edge_completions::{ChatRequest, ToolChoice};
///
/// let _builder = ChatRequest::kimi_k3_builder()
///     .tool_choice(ToolChoice::Required);
/// ```
#[must_use = "request builders must be transitioned and built"]
#[derive(Debug, Clone)]
pub struct ChatRequestBuilder<M: MessageState, T: ToolState> {
    model: ModelId,
    messages: Vec<ChatMessage>,
    tools: Vec<FunctionTool>,
    tool_choice: Option<ToolChoice>,
    temperature: Option<Temperature>,
    max_tokens: Option<MaxTokens>,
    state: PhantomData<(M, T)>,
}

impl<M: MessageState, T: ToolState> ChatRequestBuilder<M, T> {
    fn transition<NextM: MessageState, NextT: ToolState>(self) -> ChatRequestBuilder<NextM, NextT> {
        ChatRequestBuilder {
            model: self.model,
            messages: self.messages,
            tools: self.tools,
            tool_choice: self.tool_choice,
            temperature: self.temperature,
            max_tokens: self.max_tokens,
            state: PhantomData,
        }
    }

    /// Sets a validated sampling temperature without changing either state witness.
    pub fn temperature(mut self, temperature: Temperature) -> Self {
        self.temperature = Some(temperature);
        self
    }

    /// Sets a validated completion-token limit without changing either state witness.
    pub fn max_tokens(mut self, max_tokens: MaxTokens) -> Self {
        self.max_tokens = Some(max_tokens);
        self
    }
}

impl<T: ToolState> ChatRequestBuilder<NeedsMessage, T> {
    /// Adds the first message and returns the [`HasMessages`] witness state.
    pub fn message(mut self, message: ChatMessage) -> ChatRequestBuilder<HasMessages, T> {
        self.messages.push(message);
        self.transition()
    }
}

impl<T: ToolState> ChatRequestBuilder<HasMessages, T> {
    /// Appends another message while preserving the [`HasMessages`] witness.
    pub fn message(mut self, message: ChatMessage) -> Self {
        self.messages.push(message);
        self
    }

    /// Builds a request after the [`HasMessages`] witness proves that it is non-empty.
    #[must_use]
    pub fn build(self) -> ChatRequest {
        ChatRequest {
            model: self.model,
            messages: self.messages,
            tools: self.tools,
            tool_choice: self.tool_choice,
            temperature: self.temperature,
            max_tokens: self.max_tokens,
        }
    }
}

impl<M: MessageState> ChatRequestBuilder<M, WithoutTools> {
    /// Adds the first tool and returns the [`WithTools`] witness state.
    ///
    /// Tool selection defaults to [`ToolChoice::Auto`] and can be changed only
    /// after this transition.
    pub fn tool(mut self, tool: FunctionTool) -> ChatRequestBuilder<M, WithTools> {
        self.tools.push(tool);
        self.tool_choice = Some(ToolChoice::Auto);
        self.transition()
    }
}

impl<M: MessageState> ChatRequestBuilder<M, WithTools> {
    /// Appends another tool while preserving the [`WithTools`] witness.
    pub fn tool(mut self, tool: FunctionTool) -> Self {
        self.tools.push(tool);
        self
    }

    /// Selects the provider's tool-choice setting after at least one tool exists.
    pub fn tool_choice(mut self, choice: ToolChoice) -> Self {
        self.tool_choice = Some(choice);
        self
    }
}

/// A validated, serializable chat-completion request.
///
/// Prefer [`ChatRequest::builder`] or [`ChatRequest::kimi_k3_builder`] for new
/// code. Their state parameters make an empty request impossible to build.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ChatRequest {
    model: ModelId,
    messages: Vec<ChatMessage>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    tools: Vec<FunctionTool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_choice: Option<ToolChoice>,
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<Temperature>,
    #[serde(skip_serializing_if = "Option::is_none")]
    max_tokens: Option<MaxTokens>,
}

impl ChatRequest {
    /// Starts a typestate builder for a validated model.
    pub fn builder(model: ModelId) -> ChatRequestBuilder<NeedsMessage, WithoutTools> {
        ChatRequestBuilder {
            model,
            messages: Vec::new(),
            tools: Vec::new(),
            tool_choice: None,
            temperature: None,
            max_tokens: None,
            state: PhantomData,
        }
    }

    /// Starts a typestate builder for `moonshotai/kimi-k3`.
    pub fn kimi_k3_builder() -> ChatRequestBuilder<NeedsMessage, WithoutTools> {
        Self::builder(ModelId::kimi_k3())
    }

    /// Creates a request for `moonshotai/kimi-k3` with at least one message.
    ///
    /// This compatibility constructor performs the message-cardinality check at
    /// runtime. New code can use [`ChatRequest::kimi_k3_builder`] to move the
    /// same invariant to compile time.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidConfiguration::EmptyMessages`] when `messages` is empty.
    pub fn kimi_k3(messages: Vec<ChatMessage>) -> Result<Self, InvalidConfiguration> {
        Self::new(ModelId::kimi_k3(), messages)
    }

    /// Creates a request for a validated model with at least one message.
    ///
    /// This compatibility constructor performs the message-cardinality check at
    /// runtime. New code can use [`ChatRequest::builder`] instead.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidConfiguration::EmptyMessages`] when `messages` is empty.
    pub fn new(model: ModelId, messages: Vec<ChatMessage>) -> Result<Self, InvalidConfiguration> {
        if messages.is_empty() {
            return Err(InvalidConfiguration::EmptyMessages);
        }
        Ok(Self {
            model,
            messages,
            tools: Vec::new(),
            tool_choice: None,
            temperature: None,
            max_tokens: None,
        })
    }

    /// Enables one typed function tool using the selected tool choice.
    #[must_use]
    pub fn with_tool(mut self, tool: FunctionTool, choice: ToolChoice) -> Self {
        self.tools = vec![tool];
        self.tool_choice = Some(choice);
        self
    }

    /// Enables one or more typed function tools using the selected tool choice.
    ///
    /// This compatibility method validates tool cardinality at runtime. The
    /// typestate builder makes [`ChatRequestBuilder::tool_choice`] available only
    /// after at least one call to [`ChatRequestBuilder::tool`].
    ///
    /// # Errors
    ///
    /// Returns [`InvalidConfiguration::EmptyTools`] when `tools` is empty.
    pub fn with_tools(
        mut self,
        tools: Vec<FunctionTool>,
        choice: ToolChoice,
    ) -> Result<Self, InvalidConfiguration> {
        if tools.is_empty() {
            return Err(InvalidConfiguration::EmptyTools);
        }
        self.tools = tools;
        self.tool_choice = Some(choice);
        Ok(self)
    }

    /// Sets a validated sampling temperature.
    #[must_use]
    pub fn with_temperature(mut self, temperature: Temperature) -> Self {
        self.temperature = Some(temperature);
        self
    }

    /// Sets a validated completion-token limit.
    #[must_use]
    pub fn with_max_tokens(mut self, max_tokens: MaxTokens) -> Self {
        self.max_tokens = Some(max_tokens);
        self
    }

    /// Returns the selected model.
    #[must_use]
    pub fn model(&self) -> &ModelId {
        &self.model
    }
}

/// A typed chat-completion response.
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct ChatCompletion {
    id: CompletionId,
    object: CompletionObject,
    created: u64,
    model: ModelId,
    choices: Vec<ChatChoice>,
    usage: Option<Usage>,
}

impl ChatCompletion {
    /// Returns the provider completion identifier.
    #[must_use]
    pub fn id(&self) -> &str {
        &self.id.0
    }

    /// Returns the model reported by the provider.
    #[must_use]
    pub fn model(&self) -> &ModelId {
        &self.model
    }

    /// Returns the provider's Unix creation timestamp in seconds.
    #[must_use]
    pub fn created_unix_seconds(&self) -> u64 {
        self.created
    }

    /// Returns all completion choices.
    #[must_use]
    pub fn choices(&self) -> &[ChatChoice] {
        &self.choices
    }

    /// Returns the first choice or a typed missing-choice error.
    ///
    /// # Errors
    ///
    /// Returns [`Error::MissingChoice`] when the provider response contains no
    /// choices.
    pub fn first_choice(&self) -> Result<&ChatChoice, Error> {
        self.choices.first().ok_or(Error::MissingChoice)
    }

    /// Returns token usage when the provider supplied it.
    #[must_use]
    pub fn usage(&self) -> Option<&Usage> {
        self.usage.as_ref()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct CompletionId(String);

impl<'de> Deserialize<'de> for CompletionId {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let value = String::deserialize(deserializer)?;
        if value.trim().is_empty() {
            return Err(de::Error::custom("completion ID must not be empty"));
        }
        Ok(Self(value))
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
enum CompletionObject {
    #[serde(rename = "chat.completion")]
    ChatCompletion,
}

/// One provider-generated completion choice.
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct ChatChoice {
    index: u32,
    message: AssistantMessage,
    finish_reason: Option<FinishReason>,
}

impl ChatChoice {
    /// Returns this choice's provider index.
    #[must_use]
    pub fn index(&self) -> u32 {
        self.index
    }

    /// Returns the assistant message.
    #[must_use]
    pub fn message(&self) -> &AssistantMessage {
        &self.message
    }

    /// Returns the typed finish reason when supplied by the provider.
    #[must_use]
    pub fn finish_reason(&self) -> Option<&FinishReason> {
        self.finish_reason.as_ref()
    }
}

/// A typed assistant message returned by the provider.
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct AssistantMessage {
    role: AssistantRole,
    content: Option<String>,
    #[serde(default)]
    tool_calls: Vec<ToolCall>,
}

/// Exhaustive alternatives carried by a typed assistant message.
///
/// This enum is a sum type (a coproduct): callers must select one branch for
/// every supported provider outcome. `TextAndToolCalls` preserves providers
/// that return both values rather than silently discarding either one.
///
/// ```
/// use edge_completions::{AssistantMessage, AssistantOutput};
///
/// fn output_kind(message: &AssistantMessage) -> &'static str {
///     match message.output() {
///         AssistantOutput::Text(_) => "text",
///         AssistantOutput::ToolCalls(_) => "tools",
///         AssistantOutput::TextAndToolCalls { .. } => "text-and-tool-calls",
///         AssistantOutput::Empty => "empty",
///     }
/// }
/// ```
#[must_use = "assistant output should be handled explicitly"]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AssistantOutput<'a> {
    /// The assistant returned final text without tool calls.
    Text(&'a str),
    /// The assistant proposed tool calls without text.
    ToolCalls(&'a [ToolCall]),
    /// The assistant returned text and proposed one or more tool calls.
    TextAndToolCalls {
        /// Provider-generated assistant text.
        text: &'a str,
        /// Untrusted tool calls that still require typed validation.
        tool_calls: &'a [ToolCall],
    },
    /// The assistant returned neither text nor tool calls.
    Empty,
}

impl AssistantMessage {
    /// Classifies assistant output into an exhaustive typed alternative.
    pub fn output(&self) -> AssistantOutput<'_> {
        match (self.content.as_deref(), self.tool_calls.as_slice()) {
            (Some(text), []) => AssistantOutput::Text(text),
            (None, []) => AssistantOutput::Empty,
            (None, tool_calls) => AssistantOutput::ToolCalls(tool_calls),
            (Some(text), tool_calls) => AssistantOutput::TextAndToolCalls { text, tool_calls },
        }
    }

    /// Returns text content when the assistant produced a final answer.
    #[must_use]
    pub fn content(&self) -> Option<&str> {
        self.content.as_deref()
    }

    /// Returns all tool calls proposed by the model.
    #[must_use]
    pub fn tool_calls(&self) -> &[ToolCall] {
        &self.tool_calls
    }

    /// Returns the first proposed tool call or a typed missing-call error.
    ///
    /// # Errors
    ///
    /// Returns [`Error::MissingToolCall`] when the assistant proposed no tool
    /// calls.
    pub fn first_tool_call(&self) -> Result<&ToolCall, Error> {
        self.tool_calls.first().ok_or(Error::MissingToolCall)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
enum AssistantRole {
    Assistant,
}

/// A function-tool call proposed by the model.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ToolCall {
    id: ToolCallId,
    #[serde(rename = "type")]
    kind: ToolCallKind,
    function: ToolCallFunction,
}

/// Proof that a model-produced tool call matched and decoded through `T`.
///
/// The witness binds the validated arguments and any encoded result to the
/// same [`ToolDefinition`]. Construct it only through [`ToolCall::validate`].
///
/// A validated call gives application code typed arguments and accepts only the
/// matching output type:
///
/// ```
/// use edge_completions::{ChatMessage, ToolCall, ToolDefinition, ToolError};
/// use schemars::JsonSchema;
/// use serde::{Deserialize, Serialize};
///
/// struct LookupWeather;
/// #[derive(Deserialize, JsonSchema)]
/// struct Arguments { city: String }
/// #[derive(Serialize)]
/// struct Report { temperature_celsius: i16 }
///
/// impl ToolDefinition for LookupWeather {
///     type Arguments = Arguments;
///     type Output = Report;
///     const NAME: &'static str = "lookup_weather";
///     const DESCRIPTION: &'static str = "Look up weather";
/// }
///
/// fn result_for(call: &ToolCall) -> Result<ChatMessage, ToolError> {
///     let validated = call.validate::<LookupWeather>()?;
///     assert!(!validated.arguments().city.is_empty());
///     validated.result(&Report { temperature_celsius: 18 })
/// }
/// ```
///
/// Passing a different output type fails to compile:
///
/// ```compile_fail
/// use edge_completions::{ToolCall, ToolDefinition, ToolError};
/// use schemars::JsonSchema;
/// use serde::{Deserialize, Serialize};
///
/// struct LookupWeather;
/// #[derive(Deserialize, JsonSchema)]
/// struct Arguments { city: String }
/// #[derive(Serialize)]
/// struct WeatherReport { temperature_celsius: i16 }
///
/// impl ToolDefinition for LookupWeather {
///     type Arguments = Arguments;
///     type Output = WeatherReport;
///     const NAME: &'static str = "lookup_weather";
///     const DESCRIPTION: &'static str = "Look up weather";
/// }
///
/// fn invalid_result(call: &ToolCall) -> Result<(), ToolError> {
///     let validated = call.validate::<LookupWeather>()?;
///     validated.result(&String::from("wrong output type"))?;
///     Ok(())
/// }
/// ```
#[must_use = "validated tool calls should be inspected or converted into results"]
pub struct ValidatedToolCall<'a, T: ToolDefinition> {
    call: &'a ToolCall,
    arguments: T::Arguments,
    tool: PhantomData<T>,
}

impl<T: ToolDefinition> ValidatedToolCall<'_, T> {
    /// Returns the validated arguments associated with `T`.
    #[must_use]
    pub fn arguments(&self) -> &T::Arguments {
        &self.arguments
    }

    /// Consumes the proof and returns its validated arguments.
    #[must_use]
    pub fn into_arguments(self) -> T::Arguments {
        self.arguments
    }

    /// Encodes an output whose type is associated with the same tool proof.
    ///
    /// # Errors
    ///
    /// Returns [`ToolError::ResultEncoding`] when `result` cannot be serialized.
    /// A name mismatch is unrepresentable here because this witness is created
    /// only after [`ToolCall::validate`] succeeds for the same `T`.
    pub fn result(&self, result: &T::Output) -> Result<ChatMessage, ToolError> {
        ChatMessage::tool_result::<T>(self.call, result)
    }

    /// Returns the underlying provider tool-call identifier.
    #[must_use]
    pub fn id(&self) -> &str {
        self.call.id()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
enum ToolCallKind {
    Function,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
struct ToolCallFunction {
    name: String,
    // The provider encodes JSON arguments inside a string.
    arguments: String,
}

impl ToolCall {
    /// Returns the provider's tool-call identifier.
    #[must_use]
    pub fn id(&self) -> &str {
        &self.id.0
    }

    /// Returns the untrusted function name proposed by the model.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.function.name
    }

    /// Validates the proposed name and arguments, returning a proof bound to
    /// the matching tool contract.
    ///
    /// This is a runtime trust-boundary check: tool calls come from the model and
    /// cannot be proven valid at compile time. After validation, the returned
    /// witness carries the established relationship between `T::Arguments` and
    /// `T::Output`.
    ///
    /// # Errors
    ///
    /// Returns [`ToolError::UnexpectedName`] when the model proposed a different
    /// function, or [`ToolError::InvalidArguments`] when the arguments cannot be
    /// decoded into `T::Arguments`.
    pub fn validate<T: ToolDefinition>(&self) -> Result<ValidatedToolCall<'_, T>, ToolError> {
        self.ensure_name::<T>()?;
        let arguments = serde_json::from_str(&self.function.arguments).map_err(|source| {
            ToolError::InvalidArguments {
                tool: self.name().to_owned(),
                source,
            }
        })?;
        Ok(ValidatedToolCall {
            call: self,
            arguments,
            tool: PhantomData,
        })
    }

    /// Validates the proposed name and decodes arguments into `T::Arguments`.
    ///
    /// Prefer [`ToolCall::validate`] when the application also needs to encode a
    /// result. This compatibility helper consumes the proof and returns only the
    /// validated arguments.
    ///
    /// # Errors
    ///
    /// Returns [`ToolError::UnexpectedName`] when the model proposed a different
    /// function, or [`ToolError::InvalidArguments`] when the arguments cannot be
    /// decoded into `T::Arguments`.
    pub fn arguments_for<T: ToolDefinition>(&self) -> Result<T::Arguments, ToolError> {
        Ok(self.validate::<T>()?.into_arguments())
    }

    fn ensure_name<T: ToolDefinition>(&self) -> Result<(), ToolError> {
        if self.name() != T::NAME {
            return Err(ToolError::UnexpectedName {
                expected: T::NAME,
                actual: self.name().to_owned(),
            });
        }
        Ok(())
    }
}

/// Why the provider stopped generating a completion.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum FinishReason {
    /// The model reached a natural stopping point.
    Stop,
    /// The configured or provider token limit was reached.
    Length,
    /// The model proposed one or more tool calls.
    ToolCalls,
    /// The provider filtered generated content.
    ContentFilter,
    /// The provider returned a newer finish reason not yet modeled by this crate.
    #[serde(other)]
    Unknown,
}

/// Token accounting returned by the provider.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct Usage {
    prompt_tokens: u64,
    completion_tokens: u64,
    total_tokens: u64,
}

impl Usage {
    /// Returns the number of input tokens reported by the provider.
    #[must_use]
    pub fn prompt_tokens(&self) -> u64 {
        self.prompt_tokens
    }

    /// Returns the number of generated tokens reported by the provider.
    #[must_use]
    pub fn completion_tokens(&self) -> u64 {
        self.completion_tokens
    }

    /// Returns the total token count reported by the provider.
    #[must_use]
    pub fn total_tokens(&self) -> u64 {
        self.total_tokens
    }
}

#[cfg(test)]
mod tests {
    use schemars::JsonSchema;
    use serde::Deserialize;

    use super::*;

    struct GetWeather;

    impl ToolDefinition for GetWeather {
        type Arguments = WeatherArgs;
        type Output = WeatherReport;

        const NAME: &'static str = "get_weather";
        const DESCRIPTION: &'static str = "Get the weather for a city";
    }

    #[derive(Debug, Deserialize, JsonSchema, PartialEq)]
    struct WeatherArgs {
        city: String,
    }

    #[derive(Serialize)]
    struct WeatherReport {
        temperature_celsius: i16,
    }

    fn tool_call(name: &str, arguments: &str) -> ToolCall {
        ToolCall {
            id: ToolCallId("call_1".into()),
            kind: ToolCallKind::Function,
            function: ToolCallFunction {
                name: name.into(),
                arguments: arguments.into(),
            },
        }
    }

    fn assistant_message(content: Option<&str>, tool_calls: Vec<ToolCall>) -> AssistantMessage {
        AssistantMessage {
            role: AssistantRole::Assistant,
            content: content.map(ToOwned::to_owned),
            tool_calls,
        }
    }

    #[test]
    fn serializes_required_function_tool_request() -> Result<(), Box<dyn std::error::Error>> {
        let request = ChatRequest::kimi_k3(vec![ChatMessage::user("What is the weather?")])?
            .with_tool(
                FunctionTool::for_tool::<GetWeather>()?,
                ToolChoice::Required,
            );

        let value = serde_json::to_value(request)?;
        assert_eq!(value["model"], "moonshotai/kimi-k3");
        assert_eq!(value["tool_choice"], "required");
        assert_eq!(value["tools"][0]["type"], "function");
        assert_eq!(value["tools"][0]["function"]["name"], "get_weather");
        assert_eq!(
            value["tools"][0]["function"]["parameters"]["type"],
            "object"
        );
        Ok(())
    }

    #[test]
    fn typestate_builder_preserves_the_existing_request_contract()
    -> Result<(), Box<dyn std::error::Error>> {
        let tool = FunctionTool::for_tool::<GetWeather>()?;
        let legacy = ChatRequest::kimi_k3(vec![ChatMessage::user("What is the weather?")])?
            .with_tool(tool.clone(), ToolChoice::Required);
        let typestate = ChatRequest::kimi_k3_builder()
            .tool(tool)
            .tool_choice(ToolChoice::Required)
            .message(ChatMessage::user("What is the weather?"))
            .build();

        assert_eq!(typestate, legacy);
        Ok(())
    }

    #[test]
    fn independent_builder_endomorphisms_commute() -> Result<(), Box<dyn std::error::Error>> {
        let temperature = Temperature::new(0.4)?;
        let max_tokens = MaxTokens::new(120)?;
        let temperature_then_tokens = ChatRequest::kimi_k3_builder()
            .temperature(temperature)
            .max_tokens(max_tokens)
            .message(ChatMessage::user("Explain composition"))
            .build();
        let tokens_then_temperature = ChatRequest::kimi_k3_builder()
            .max_tokens(max_tokens)
            .temperature(temperature)
            .message(ChatMessage::user("Explain composition"))
            .build();

        assert_eq!(temperature_then_tokens, tokens_then_temperature);
        Ok(())
    }

    #[test]
    fn classifies_every_assistant_output_sum_variant() {
        let call = tool_call("get_weather", r#"{"city":"Paris"}"#);
        let text = assistant_message(Some("Clear skies"), Vec::new());
        let tools = assistant_message(None, vec![call.clone()]);
        let both = assistant_message(Some("Checking"), vec![call]);
        let empty = assistant_message(None, Vec::new());

        assert_eq!(text.output(), AssistantOutput::Text("Clear skies"));
        assert!(matches!(
            tools.output(),
            AssistantOutput::ToolCalls(tool_calls) if tool_calls.len() == 1
        ));
        assert!(matches!(
            both.output(),
            AssistantOutput::TextAndToolCalls {
                text: "Checking",
                tool_calls
            } if tool_calls.len() == 1
        ));
        assert_eq!(empty.output(), AssistantOutput::Empty);
    }

    #[test]
    fn rejects_an_empty_message_list() {
        assert!(matches!(
            ChatRequest::kimi_k3(Vec::new()),
            Err(InvalidConfiguration::EmptyMessages)
        ));
    }

    #[test]
    fn rejects_an_empty_tool_definition_field() {
        struct InvalidTool;
        impl ToolDefinition for InvalidTool {
            type Arguments = WeatherArgs;
            type Output = WeatherReport;
            const NAME: &'static str = "";
            const DESCRIPTION: &'static str = "Description";
        }

        assert!(matches!(
            FunctionTool::for_tool::<InvalidTool>(),
            Err(ToolError::InvalidDefinition { field: "name" })
        ));
    }

    #[test]
    fn assistant_tool_call_round_trip_preserves_null_content()
    -> Result<(), Box<dyn std::error::Error>> {
        let message = ChatMessage::assistant_tool_calls(vec![tool_call(
            "get_weather",
            r#"{"city":"Paris"}"#,
        )]);
        let value = serde_json::to_value(message)?;
        assert!(value["content"].is_null());
        assert_eq!(value["tool_calls"][0]["id"], "call_1");
        Ok(())
    }

    #[test]
    fn parses_typed_tool_arguments() -> Result<(), Box<dyn std::error::Error>> {
        let call = tool_call("get_weather", r#"{"city":"Paris"}"#);
        assert_eq!(
            call.arguments_for::<GetWeather>()?,
            WeatherArgs {
                city: "Paris".into()
            }
        );
        Ok(())
    }

    #[test]
    fn validated_tool_call_carries_arguments_and_output_contract()
    -> Result<(), Box<dyn std::error::Error>> {
        let call = tool_call("get_weather", r#"{"city":"Paris"}"#);
        let validated = call.validate::<GetWeather>()?;

        assert_eq!(validated.id(), "call_1");
        assert_eq!(validated.arguments().city, "Paris");
        assert_eq!(
            serde_json::to_value(validated.result(&WeatherReport {
                temperature_celsius: 18,
            })?)?,
            serde_json::json!({
                "role": "tool",
                "tool_call_id": "call_1",
                "content": "{\"temperature_celsius\":18}"
            })
        );
        Ok(())
    }

    #[test]
    fn rejects_arguments_for_a_different_typed_tool() {
        struct OtherTool;
        impl ToolDefinition for OtherTool {
            type Arguments = WeatherArgs;
            type Output = WeatherReport;
            const NAME: &'static str = "other_tool";
            const DESCRIPTION: &'static str = "A different tool";
        }

        let result = tool_call("get_weather", r#"{"city":"Paris"}"#).arguments_for::<OtherTool>();

        assert!(matches!(result, Err(ToolError::UnexpectedName { .. })));
    }

    #[test]
    fn rejects_malformed_tool_arguments_with_a_typed_error() {
        assert!(matches!(
            tool_call("get_weather", "not-json").arguments_for::<GetWeather>(),
            Err(ToolError::InvalidArguments { .. })
        ));
    }

    #[test]
    fn encodes_a_typed_tool_result_without_exposing_raw_json()
    -> Result<(), Box<dyn std::error::Error>> {
        let message = ChatMessage::tool_result::<GetWeather>(
            &tool_call("get_weather", r#"{"city":"Paris"}"#),
            &WeatherReport {
                temperature_celsius: 18,
            },
        )?;
        let encoded = serde_json::to_value(message)?;

        assert_eq!(encoded["tool_call_id"], "call_1");
        assert_eq!(encoded["content"], r#"{"temperature_celsius":18}"#);
        Ok(())
    }

    #[test]
    fn rejects_a_tool_result_for_a_different_contract() {
        struct OtherTool;
        impl ToolDefinition for OtherTool {
            type Arguments = WeatherArgs;
            type Output = WeatherReport;
            const NAME: &'static str = "other_tool";
            const DESCRIPTION: &'static str = "A different tool";
        }

        assert!(matches!(
            ChatMessage::tool_result::<OtherTool>(
                &tool_call("get_weather", r#"{"city":"Paris"}"#),
                &WeatherReport {
                    temperature_celsius: 18,
                },
            ),
            Err(ToolError::UnexpectedName { .. })
        ));
    }

    #[test]
    fn rejects_a_completion_without_choices() -> Result<(), Box<dyn std::error::Error>> {
        let completion: ChatCompletion = serde_json::from_value(serde_json::json!({
            "id": "chatcmpl-1",
            "object": "chat.completion",
            "created": 1,
            "model": "moonshotai/kimi-k3",
            "choices": [],
            "usage": null
        }))?;

        assert!(matches!(
            completion.first_choice(),
            Err(Error::MissingChoice)
        ));
        Ok(())
    }

    #[test]
    fn rejects_an_unexpected_response_object() {
        let result = serde_json::from_value::<ChatCompletion>(serde_json::json!({
            "id": "chatcmpl-1",
            "object": "unexpected",
            "created": 1,
            "model": "moonshotai/kimi-k3",
            "choices": [],
            "usage": null
        }));

        assert!(result.is_err());
    }

    #[test]
    fn rejects_generation_values_outside_provider_contract() {
        assert!(matches!(
            Temperature::new(f32::NAN),
            Err(InvalidConfiguration::InvalidTemperature { .. })
        ));
        assert!(matches!(
            Temperature::new(2.1),
            Err(InvalidConfiguration::InvalidTemperature { .. })
        ));
        assert!(matches!(
            MaxTokens::new(0),
            Err(InvalidConfiguration::ZeroMaxTokens)
        ));
    }
}