agent-framework-foundry-hosting 0.15.0

Host agents and workflows as Azure AI Foundry hosted agents (Responses and Invocations protocols, Foundry state storage)
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
//! The Voice Live Bridge text/control profile messages (upstream
//! `voice/_models.py`).
//!
//! Inbound messages come from the Bridge and are built by the codec
//! ([`decode_inbound_message`](super::decode_inbound_message)), which
//! validates them first. Outbound messages are built by the agent; each
//! constructor generates the message's own `id` (`m_…`) and `ts` (UTC
//! RFC 3339, millisecond precision), as upstream's defaults do. Field names
//! are the wire names.
//!
//! `Debug` output redacts payload fields (text, caller context, data,
//! voice patches, messages) and is bounded to 1024 characters, as
//! upstream's `repr`; identifiers, codes and counts are shown.

use std::fmt;
use std::time::{SystemTime, UNIX_EPOCH};

use serde::Serialize;
use serde_json::{Map, Value};

/// A JSON object (application data, caller context, voice patches).
pub type JsonObject = Map<String, Value>;

/// A new Bridge message id (`m_` and 32 hex digits).
pub fn new_message_id() -> String {
    format!("m_{}", uuid::Uuid::new_v4().simple())
}

/// A new agent response id (`r_` and 32 hex digits).
pub fn new_response_id() -> String {
    format!("r_{}", uuid::Uuid::new_v4().simple())
}

/// A new agent output-item id (`it_` and 32 hex digits).
pub fn new_item_id() -> String {
    format!("it_{}", uuid::Uuid::new_v4().simple())
}

/// The current UTC time as RFC 3339 with millisecond precision
/// (`2026-08-12T00:00:00.000Z`).
pub fn new_timestamp() -> String {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default();
    let secs = now.as_secs() as i64;
    let (year, month, day) = civil_from_days(secs.div_euclid(86_400));
    let of_day = secs.rem_euclid(86_400);
    format!(
        "{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}.{:03}Z",
        of_day / 3600,
        of_day % 3600 / 60,
        of_day % 60,
        now.subsec_millis()
    )
}

/// Days since 1970-01-01 to a proleptic Gregorian date.
fn civil_from_days(days: i64) -> (i64, u32, u32) {
    let z = days + 719_468;
    let era = z.div_euclid(146_097);
    let doe = z.rem_euclid(146_097);
    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let day = (doy - (153 * mp + 2) / 5 + 1) as u32;
    let month = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
    let year = yoe + era * 400 + i64::from(month <= 2);
    (year, month, day)
}

// ---------------------------------------------------------------------------
// Bounded, redacting Debug (upstream `_voice_model_repr`).

const MAX_MODEL_DEBUG_LENGTH: usize = 1024;
const MAX_FIELD_DEBUG_LENGTH: usize = 128;
const REDACTED: &str = "<redacted>";
const SAFE_DEBUG_FIELDS: &[&str] = &[
    "admission_timeout_ms",
    "code",
    "count",
    "first_output_ms",
    "id",
    "idle_ms",
    "in_reply_to",
    "item_id",
    "item_ids",
    "max_duration_ms",
    "mode",
    "no_input_timeout_ms",
    "protocol_version",
    "reconnect",
    "response_id",
    "response_timeouts",
    "retriable",
    "stage",
    "ts",
    "type",
];

fn truncate(text: &str, max: usize) -> String {
    if text.chars().count() <= max {
        return text.to_string();
    }
    let keep: String = text.chars().take(max.saturating_sub(3)).collect();
    format!("{keep}...")
}

#[doc(hidden)]
pub fn debug_field<T: Serialize + ?Sized>(name: &str, value: &T) -> String {
    let rendered = serde_json::to_value(value).unwrap_or(Value::Null);
    let shown = if !rendered.is_null() && !SAFE_DEBUG_FIELDS.contains(&name) {
        REDACTED.to_string()
    } else {
        truncate(&rendered.to_string(), MAX_FIELD_DEBUG_LENGTH)
    };
    format!("{name}={shown}")
}

#[doc(hidden)]
pub fn debug_finish(name: &str, fields: &[String], f: &mut fmt::Formatter<'_>) -> fmt::Result {
    let rendered = format!("{name}({})", fields.join(", "));
    if rendered.chars().count() <= MAX_MODEL_DEBUG_LENGTH {
        return f.write_str(&rendered);
    }
    let keep: String = rendered.chars().take(MAX_MODEL_DEBUG_LENGTH - 4).collect();
    write!(f, "{keep}...)")
}

macro_rules! redacted_debug {
    ($ty:ident { $($field:ident),* $(,)? }) => {
        impl fmt::Debug for $ty {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                let fields = [$(debug_field(stringify!($field), &self.$field)),*];
                debug_finish(stringify!($ty), &fields, f)
            }
        }
    };
}

// ---------------------------------------------------------------------------
// Shared value types.

/// Response deadlines selected by the Bridge.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct ResponseTimeouts {
    /// Maximum milliseconds until first output.
    pub first_output_ms: u64,
    /// Maximum milliseconds between output progress messages.
    pub idle_ms: u64,
    /// Absolute maximum response duration in milliseconds.
    pub max_duration_ms: u64,
}
redacted_debug!(ResponseTimeouts {
    first_output_ms,
    idle_ms,
    max_duration_ms
});

/// One part of an ordered `user.message` (wire `content[]`).
#[derive(Clone, PartialEq, Serialize)]
#[serde(tag = "type")]
pub enum InputContentPart {
    /// `input_text`: final recognized or application-supplied text.
    #[serde(rename = "input_text")]
    InputText {
        /// The text.
        text: String,
    },
    /// `input_data`: one application-defined JSON object.
    #[serde(rename = "input_data")]
    InputData {
        /// The object.
        data: JsonObject,
    },
}

impl fmt::Debug for InputContentPart {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InputText { text } => debug_finish(
                "InputTextPart",
                &[debug_field("text", text), "type=\"input_text\"".into()],
                f,
            ),
            Self::InputData { data } => debug_finish(
                "InputDataPart",
                &[debug_field("data", data), "type=\"input_data\"".into()],
                f,
            ),
        }
    }
}

/// The local transport-disconnect event (not a Bridge wire message): the
/// peer or a proxy closed the WebSocket, or a send found it gone.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct SessionDisconnected {
    /// The RFC 6455 close code the transport reported.
    pub code: u16,
    /// The close reason the transport reported, if any.
    pub reason: Option<String>,
}
redacted_debug!(SessionDisconnected { code, reason });

// ---------------------------------------------------------------------------
// Inbound (Bridge to agent).

/// `session.start`: the Bridge application-start event.
#[derive(Clone, PartialEq, Serialize)]
pub struct SessionStart {
    /// Wire message id.
    pub id: String,
    /// Sender timestamp.
    pub ts: String,
    /// The exact Bridge protocol version (see
    /// [`PROTOCOL_VERSION`](super::PROTOCOL_VERSION)).
    pub protocol_version: String,
    /// Whether this transport reattaches the logical session.
    pub reconnect: bool,
    /// Effective response deadlines.
    pub response_timeouts: ResponseTimeouts,
    /// A Bridge-owned greeting (never present on reconnect).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub greeting: Option<String>,
    /// The user-silence threshold.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub no_input_timeout_ms: Option<u64>,
    /// Caller context (never contains credential-named fields).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub caller: Option<JsonObject>,
}
redacted_debug!(SessionStart {
    id,
    ts,
    protocol_version,
    reconnect,
    response_timeouts,
    greeting,
    no_input_timeout_ms,
    caller
});

/// `user.message`: a completed user turn of ordered text and data parts.
#[derive(Clone, PartialEq, Serialize)]
pub struct UserMessage {
    /// Wire message id.
    pub id: String,
    /// Sender timestamp.
    pub ts: String,
    /// The Bridge-allocated input item id (`in_…`).
    pub item_id: String,
    /// The supported parts, in wire order (unknown part types are skipped).
    pub content: Vec<InputContentPart>,
}
redacted_debug!(UserMessage {
    id,
    ts,
    item_id,
    content
});

/// `user.no_input`: a Bridge-generated user-silence turn.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct UserNoInput {
    /// Wire message id.
    pub id: String,
    /// Sender timestamp.
    pub ts: String,
    /// The Bridge-allocated input item id (`in_…`).
    pub item_id: String,
    /// The consecutive no-input count.
    pub count: u64,
}
redacted_debug!(UserNoInput {
    id,
    ts,
    item_id,
    count
});

/// `user.speech_started`: caller speech began while no response was open
/// (advisory).
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct UserSpeechStarted {
    /// Wire message id.
    pub id: String,
    /// Sender timestamp.
    pub ts: String,
}
redacted_debug!(UserSpeechStarted { id, ts });

/// `user.barge_in`: the caller interrupted, with the playback snapshot.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct UserBargeIn {
    /// Wire message id.
    pub id: String,
    /// Sender timestamp.
    pub ts: String,
    /// The interrupted response (`r_…`).
    pub response_id: String,
    /// The text played before the interruption.
    pub heard_text: String,
    /// The output item playing at the interruption (`it_…`), if any.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub item_id: Option<String>,
}
redacted_debug!(UserBargeIn {
    id,
    ts,
    response_id,
    heard_text,
    item_id
});

/// `response.accepted`: the Bridge admitted a proactive response.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct ResponseAccepted {
    /// Wire message id.
    pub id: String,
    /// Sender timestamp.
    pub ts: String,
    /// The accepted proactive response (`r_…`).
    pub response_id: String,
}
redacted_debug!(ResponseAccepted {
    id,
    ts,
    response_id
});

/// `response.dropped`: the Bridge rejected or expired a proactive request.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct ResponseDropped {
    /// Wire message id.
    pub id: String,
    /// Sender timestamp.
    pub ts: String,
    /// The dropped proactive response (`r_…`).
    pub response_id: String,
    /// The open-enum drop reason.
    pub reason: String,
}
redacted_debug!(ResponseDropped {
    id,
    ts,
    response_id,
    reason
});

/// `response.cancelled`: the winning self-cancel playback outcome.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct ResponseCancelled {
    /// Wire message id.
    pub id: String,
    /// Sender timestamp.
    pub ts: String,
    /// The cancelled response (`r_…`).
    pub response_id: String,
    /// The text played before cancellation completed.
    pub heard_text: String,
    /// The output item playing at cancellation (`it_…`), if any.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub item_id: Option<String>,
}
redacted_debug!(ResponseCancelled {
    id,
    ts,
    response_id,
    heard_text,
    item_id
});

/// `response.timeout`: a response or pending-input timeout. Exactly one of
/// `response_id` and `item_ids` is set.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct ResponseTimeout {
    /// Wire message id.
    pub id: String,
    /// Sender timestamp.
    pub ts: String,
    /// The open-enum timeout stage.
    pub stage: String,
    /// The timed-out response, when one was open.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_id: Option<String>,
    /// The timed-out inputs, before a response was created.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub item_ids: Option<Vec<String>>,
}
redacted_debug!(ResponseTimeout {
    id,
    ts,
    stage,
    response_id,
    item_ids
});

/// `session.end`: Bridge-initiated session termination.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct SessionEnd {
    /// Wire message id.
    pub id: String,
    /// Sender timestamp.
    pub ts: String,
    /// The open-enum termination reason.
    pub reason: String,
}
redacted_debug!(SessionEnd { id, ts, reason });

/// Every inbound message the profile selects, tagged by its wire `type`.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "type")]
pub enum InboundVoiceMessage {
    /// `session.start`
    #[serde(rename = "session.start")]
    SessionStart(SessionStart),
    /// `user.message`
    #[serde(rename = "user.message")]
    UserMessage(UserMessage),
    /// `user.no_input`
    #[serde(rename = "user.no_input")]
    UserNoInput(UserNoInput),
    /// `user.speech_started`
    #[serde(rename = "user.speech_started")]
    UserSpeechStarted(UserSpeechStarted),
    /// `user.barge_in`
    #[serde(rename = "user.barge_in")]
    UserBargeIn(UserBargeIn),
    /// `response.accepted`
    #[serde(rename = "response.accepted")]
    ResponseAccepted(ResponseAccepted),
    /// `response.dropped`
    #[serde(rename = "response.dropped")]
    ResponseDropped(ResponseDropped),
    /// `response.cancelled`
    #[serde(rename = "response.cancelled")]
    ResponseCancelled(ResponseCancelled),
    /// `response.timeout`
    #[serde(rename = "response.timeout")]
    ResponseTimeout(ResponseTimeout),
    /// `session.end`
    #[serde(rename = "session.end")]
    SessionEnd(SessionEnd),
}

impl InboundVoiceMessage {
    /// The wire `type`.
    pub fn message_type(&self) -> &'static str {
        match self {
            Self::SessionStart(_) => "session.start",
            Self::UserMessage(_) => "user.message",
            Self::UserNoInput(_) => "user.no_input",
            Self::UserSpeechStarted(_) => "user.speech_started",
            Self::UserBargeIn(_) => "user.barge_in",
            Self::ResponseAccepted(_) => "response.accepted",
            Self::ResponseDropped(_) => "response.dropped",
            Self::ResponseCancelled(_) => "response.cancelled",
            Self::ResponseTimeout(_) => "response.timeout",
            Self::SessionEnd(_) => "session.end",
        }
    }

    /// The wire message id.
    pub fn id(&self) -> &str {
        match self {
            Self::SessionStart(m) => &m.id,
            Self::UserMessage(m) => &m.id,
            Self::UserNoInput(m) => &m.id,
            Self::UserSpeechStarted(m) => &m.id,
            Self::UserBargeIn(m) => &m.id,
            Self::ResponseAccepted(m) => &m.id,
            Self::ResponseDropped(m) => &m.id,
            Self::ResponseCancelled(m) => &m.id,
            Self::ResponseTimeout(m) => &m.id,
            Self::SessionEnd(m) => &m.id,
        }
    }
}

// ---------------------------------------------------------------------------
// Outbound (agent to Bridge).

/// `session.ready`: explicit positive readiness acknowledgement.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct SessionReady {
    /// Generated wire message id.
    pub id: String,
    /// Generated sender timestamp.
    pub ts: String,
}
redacted_debug!(SessionReady { id, ts });

impl SessionReady {
    /// A new acknowledgement.
    pub fn new() -> Self {
        Self {
            id: new_message_id(),
            ts: new_timestamp(),
        }
    }
}

impl Default for SessionReady {
    fn default() -> Self {
        Self::new()
    }
}

/// `session.rejected`: explicit negative readiness acknowledgement.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct SessionRejected {
    /// Generated wire message id.
    pub id: String,
    /// Generated sender timestamp.
    pub ts: String,
    /// Open-enum rejection code (for example `protocol_mismatch`).
    pub code: String,
    /// Whether the Bridge may retry activation.
    pub retriable: bool,
    /// Sanitized diagnostic detail.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}
redacted_debug!(SessionRejected {
    id,
    ts,
    code,
    retriable,
    message
});

impl SessionRejected {
    /// A rejection with `code`.
    pub fn new(code: impl Into<String>, retriable: bool) -> Self {
        Self {
            id: new_message_id(),
            ts: new_timestamp(),
            code: code.into(),
            retriable,
            message: None,
        }
    }

    /// Add a sanitized diagnostic.
    pub fn with_message(mut self, message: impl Into<String>) -> Self {
        self.message = Some(message.into());
        self
    }
}

/// `response.created`: open a reply response (`in_reply_to` set) or request
/// proactive admission (`in_reply_to` absent).
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct ResponseCreated {
    /// Generated wire message id.
    pub id: String,
    /// Generated sender timestamp.
    pub ts: String,
    /// The agent-allocated response id (`r_…`).
    pub response_id: String,
    /// The ordered input prefix a reply answers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub in_reply_to: Option<Vec<String>>,
    /// Proactive admission timeout (at most 60000).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub admission_timeout_ms: Option<u64>,
    /// Proactive supersession key.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub supersede_key: Option<String>,
}
redacted_debug!(ResponseCreated {
    id,
    ts,
    response_id,
    in_reply_to,
    admission_timeout_ms,
    supersede_key
});

impl ResponseCreated {
    /// A reply to the ordered inputs `in_reply_to`.
    pub fn reply<I, S>(response_id: impl Into<String>, in_reply_to: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            in_reply_to: Some(in_reply_to.into_iter().map(Into::into).collect()),
            ..Self::proactive(response_id)
        }
    }

    /// A proactive admission request.
    pub fn proactive(response_id: impl Into<String>) -> Self {
        Self {
            id: new_message_id(),
            ts: new_timestamp(),
            response_id: response_id.into(),
            in_reply_to: None,
            admission_timeout_ms: None,
            supersede_key: None,
        }
    }

    /// Set the proactive admission timeout.
    pub fn with_admission_timeout_ms(mut self, timeout: u64) -> Self {
        self.admission_timeout_ms = Some(timeout);
        self
    }

    /// Set the proactive supersession key.
    pub fn with_supersede_key(mut self, key: impl Into<String>) -> Self {
        self.supersede_key = Some(key.into());
        self
    }
}

/// `response.none`: explicitly decline an ordered input prefix.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct ResponseNone {
    /// Generated wire message id.
    pub id: String,
    /// Generated sender timestamp.
    pub ts: String,
    /// The non-empty ordered input prefix being declined.
    pub in_reply_to: Vec<String>,
    /// Open-enum decline reason.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
}
redacted_debug!(ResponseNone {
    id,
    ts,
    in_reply_to,
    reason
});

impl ResponseNone {
    /// Decline `in_reply_to`.
    pub fn new<I, S>(in_reply_to: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            id: new_message_id(),
            ts: new_timestamp(),
            in_reply_to: in_reply_to.into_iter().map(Into::into).collect(),
            reason: None,
        }
    }

    /// Add the decline reason.
    pub fn with_reason(mut self, reason: impl Into<String>) -> Self {
        self.reason = Some(reason.into());
        self
    }
}

/// `response.output_text.delta`: one streaming text increment.
#[derive(Clone, PartialEq, Serialize)]
pub struct ResponseOutputTextDelta {
    /// Generated wire message id.
    pub id: String,
    /// Generated sender timestamp.
    pub ts: String,
    /// The owning response (`r_…`).
    pub response_id: String,
    /// The output item (`it_…`).
    pub item_id: String,
    /// The non-empty text increment.
    pub delta: String,
    /// A Voice Live synthesis merge patch.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub voice: Option<JsonObject>,
}
redacted_debug!(ResponseOutputTextDelta {
    id,
    ts,
    response_id,
    item_id,
    delta,
    voice
});

impl ResponseOutputTextDelta {
    /// A text increment.
    pub fn new(
        response_id: impl Into<String>,
        item_id: impl Into<String>,
        delta: impl Into<String>,
    ) -> Self {
        Self {
            id: new_message_id(),
            ts: new_timestamp(),
            response_id: response_id.into(),
            item_id: item_id.into(),
            delta: delta.into(),
            voice: None,
        }
    }

    /// Add a voice merge patch.
    pub fn with_voice(mut self, voice: JsonObject) -> Self {
        self.voice = Some(voice);
        self
    }
}

/// `response.output_text.done`: complete one output item's text.
#[derive(Clone, PartialEq, Serialize)]
pub struct ResponseOutputTextDone {
    /// Generated wire message id.
    pub id: String,
    /// Generated sender timestamp.
    pub ts: String,
    /// The owning response (`r_…`).
    pub response_id: String,
    /// The output item (`it_…`).
    pub item_id: String,
    /// The complete item text.
    pub text: String,
    /// A Voice Live synthesis merge patch.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub voice: Option<JsonObject>,
}
redacted_debug!(ResponseOutputTextDone {
    id,
    ts,
    response_id,
    item_id,
    text,
    voice
});

impl ResponseOutputTextDone {
    /// A completed text item.
    pub fn new(
        response_id: impl Into<String>,
        item_id: impl Into<String>,
        text: impl Into<String>,
    ) -> Self {
        Self {
            id: new_message_id(),
            ts: new_timestamp(),
            response_id: response_id.into(),
            item_id: item_id.into(),
            text: text.into(),
            voice: None,
        }
    }

    /// Add a voice merge patch.
    pub fn with_voice(mut self, voice: JsonObject) -> Self {
        self.voice = Some(voice);
        self
    }
}

/// `response.output_data.done`: complete one application-data item.
#[derive(Clone, PartialEq, Serialize)]
pub struct ResponseOutputDataDone {
    /// Generated wire message id.
    pub id: String,
    /// Generated sender timestamp.
    pub ts: String,
    /// The owning response (`r_…`).
    pub response_id: String,
    /// The output item (`it_…`).
    pub item_id: String,
    /// The application-defined object.
    pub data: JsonObject,
}
redacted_debug!(ResponseOutputDataDone {
    id,
    ts,
    response_id,
    item_id,
    data
});

impl ResponseOutputDataDone {
    /// A completed data item.
    pub fn new(
        response_id: impl Into<String>,
        item_id: impl Into<String>,
        data: JsonObject,
    ) -> Self {
        Self {
            id: new_message_id(),
            ts: new_timestamp(),
            response_id: response_id.into(),
            item_id: item_id.into(),
            data,
        }
    }
}

/// `response.done`: the normal response-level terminator.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct ResponseDone {
    /// Generated wire message id.
    pub id: String,
    /// Generated sender timestamp.
    pub ts: String,
    /// The completed response (`r_…`).
    pub response_id: String,
}
redacted_debug!(ResponseDone {
    id,
    ts,
    response_id
});

impl ResponseDone {
    /// Complete `response_id`.
    pub fn new(response_id: impl Into<String>) -> Self {
        Self {
            id: new_message_id(),
            ts: new_timestamp(),
            response_id: response_id.into(),
        }
    }
}

/// `response.cancel`: cancel an open or pending proactive response.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct ResponseCancel {
    /// Generated wire message id.
    pub id: String,
    /// Generated sender timestamp.
    pub ts: String,
    /// The response or pending proactive request (`r_…`).
    pub response_id: String,
    /// Open-enum cancellation reason.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
}
redacted_debug!(ResponseCancel {
    id,
    ts,
    response_id,
    reason
});

impl ResponseCancel {
    /// Cancel `response_id`.
    pub fn new(response_id: impl Into<String>) -> Self {
        Self {
            id: new_message_id(),
            ts: new_timestamp(),
            response_id: response_id.into(),
            reason: None,
        }
    }

    /// Add the cancellation reason.
    pub fn with_reason(mut self, reason: impl Into<String>) -> Self {
        self.reason = Some(reason.into());
        self
    }
}

/// How `end_call` ends the call.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum EndCallMode {
    /// Finish queued playback first (the default).
    #[default]
    Drain,
    /// End now.
    Immediate,
}

impl EndCallMode {
    /// The wire value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Drain => "drain",
            Self::Immediate => "immediate",
        }
    }
}

/// `end_call`: ask the Bridge to end the call.
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct EndCall {
    /// Generated wire message id.
    pub id: String,
    /// Generated sender timestamp.
    pub ts: String,
    /// The non-empty call-ending reason.
    pub reason: String,
    /// The call-ending mode.
    pub mode: EndCallMode,
}
redacted_debug!(EndCall {
    id,
    ts,
    reason,
    mode
});

impl EndCall {
    /// End the call (mode `drain`).
    pub fn new(reason: impl Into<String>) -> Self {
        Self {
            id: new_message_id(),
            ts: new_timestamp(),
            reason: reason.into(),
            mode: EndCallMode::Drain,
        }
    }

    /// Set the mode.
    pub fn with_mode(mut self, mode: EndCallMode) -> Self {
        self.mode = mode;
        self
    }
}

/// `error`: an explicit response- or session-scoped agent failure
/// (upstream `AgentError`).
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct AgentError {
    /// Generated wire message id.
    pub id: String,
    /// Generated sender timestamp.
    pub ts: String,
    /// Open-enum error code.
    pub code: String,
    /// Sanitized diagnostic detail.
    pub message: String,
    /// The related response, when response-scoped (`r_…`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_id: Option<String>,
    /// The related output item (`it_…`; requires `response_id`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub item_id: Option<String>,
}
redacted_debug!(AgentError {
    id,
    ts,
    code,
    message,
    response_id,
    item_id
});

impl AgentError {
    /// A session-scoped error.
    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            id: new_message_id(),
            ts: new_timestamp(),
            code: code.into(),
            message: message.into(),
            response_id: None,
            item_id: None,
        }
    }

    /// Scope the error to a response.
    pub fn with_response_id(mut self, response_id: impl Into<String>) -> Self {
        self.response_id = Some(response_id.into());
        self
    }

    /// Name the related output item.
    pub fn with_item_id(mut self, item_id: impl Into<String>) -> Self {
        self.item_id = Some(item_id.into());
        self
    }
}

/// Every outbound message the profile selects, tagged by its wire `type`.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "type")]
pub enum OutboundVoiceMessage {
    /// `session.ready`
    #[serde(rename = "session.ready")]
    SessionReady(SessionReady),
    /// `session.rejected`
    #[serde(rename = "session.rejected")]
    SessionRejected(SessionRejected),
    /// `response.created`
    #[serde(rename = "response.created")]
    ResponseCreated(ResponseCreated),
    /// `response.none`
    #[serde(rename = "response.none")]
    ResponseNone(ResponseNone),
    /// `response.output_data.done`
    #[serde(rename = "response.output_data.done")]
    ResponseOutputDataDone(ResponseOutputDataDone),
    /// `response.output_text.delta`
    #[serde(rename = "response.output_text.delta")]
    ResponseOutputTextDelta(ResponseOutputTextDelta),
    /// `response.output_text.done`
    #[serde(rename = "response.output_text.done")]
    ResponseOutputTextDone(ResponseOutputTextDone),
    /// `response.done`
    #[serde(rename = "response.done")]
    ResponseDone(ResponseDone),
    /// `response.cancel`
    #[serde(rename = "response.cancel")]
    ResponseCancel(ResponseCancel),
    /// `end_call`
    #[serde(rename = "end_call")]
    EndCall(EndCall),
    /// `error`
    #[serde(rename = "error")]
    Error(AgentError),
}

impl OutboundVoiceMessage {
    /// The wire `type`.
    pub fn message_type(&self) -> &'static str {
        match self {
            Self::SessionReady(_) => "session.ready",
            Self::SessionRejected(_) => "session.rejected",
            Self::ResponseCreated(_) => "response.created",
            Self::ResponseNone(_) => "response.none",
            Self::ResponseOutputDataDone(_) => "response.output_data.done",
            Self::ResponseOutputTextDelta(_) => "response.output_text.delta",
            Self::ResponseOutputTextDone(_) => "response.output_text.done",
            Self::ResponseDone(_) => "response.done",
            Self::ResponseCancel(_) => "response.cancel",
            Self::EndCall(_) => "end_call",
            Self::Error(_) => "error",
        }
    }
}

macro_rules! outbound_from {
    ($($variant:ident($ty:ident)),* $(,)?) => {
        $(impl From<$ty> for OutboundVoiceMessage {
            fn from(message: $ty) -> Self {
                Self::$variant(message)
            }
        })*
    };
}

outbound_from!(
    SessionReady(SessionReady),
    SessionRejected(SessionRejected),
    ResponseCreated(ResponseCreated),
    ResponseNone(ResponseNone),
    ResponseOutputDataDone(ResponseOutputDataDone),
    ResponseOutputTextDelta(ResponseOutputTextDelta),
    ResponseOutputTextDone(ResponseOutputTextDone),
    ResponseDone(ResponseDone),
    ResponseCancel(ResponseCancel),
    EndCall(EndCall),
    Error(AgentError),
);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dates_are_proleptic_gregorian() {
        assert_eq!(civil_from_days(0), (1970, 1, 1));
        assert_eq!(civil_from_days(19_723), (2024, 1, 1));
        assert_eq!(civil_from_days(19_782), (2024, 2, 29));
        assert_eq!(civil_from_days(-1), (1969, 12, 31));
    }
}