|
1 | 1 | // Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. |
2 | 2 | // This product includes software developed at Datadog (https://www.datadoghq.com/). |
3 | 3 | // Copyright 2019-Present Datadog, Inc. |
4 | | -use serde::de::{Error, MapAccess, Visitor}; |
5 | 4 | use serde::{Deserialize, Deserializer, Serialize}; |
6 | | -use serde_with::skip_serializing_none; |
7 | | -use std::fmt::{self, Formatter}; |
8 | 5 |
|
9 | 6 | /// An interaction with its associated annotations. |
10 | 7 | #[non_exhaustive] |
11 | | -#[skip_serializing_none] |
12 | 8 | #[derive(Clone, Debug, PartialEq, Serialize)] |
13 | | -pub struct LLMObsAnnotatedInteractionItem { |
14 | | - /// List of annotations for this interaction. |
15 | | - #[serde(rename = "annotations")] |
16 | | - pub annotations: Vec<crate::datadogV2::model::LLMObsAnnotationItem>, |
17 | | - /// Identifier of the content (trace ID or session ID) for this interaction. |
18 | | - #[serde(rename = "content_id")] |
19 | | - pub content_id: String, |
20 | | - /// Unique identifier of the interaction. |
21 | | - #[serde(rename = "id")] |
22 | | - pub id: String, |
23 | | - /// Type of interaction in an annotation queue. |
24 | | - #[serde(rename = "type")] |
25 | | - pub type_: crate::datadogV2::model::LLMObsInteractionType, |
26 | | - #[serde(flatten)] |
27 | | - pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>, |
28 | | - #[serde(skip)] |
29 | | - #[serde(default)] |
30 | | - pub(crate) _unparsed: bool, |
31 | | -} |
32 | | - |
33 | | -impl LLMObsAnnotatedInteractionItem { |
34 | | - pub fn new( |
35 | | - annotations: Vec<crate::datadogV2::model::LLMObsAnnotationItem>, |
36 | | - content_id: String, |
37 | | - id: String, |
38 | | - type_: crate::datadogV2::model::LLMObsInteractionType, |
39 | | - ) -> LLMObsAnnotatedInteractionItem { |
40 | | - LLMObsAnnotatedInteractionItem { |
41 | | - annotations, |
42 | | - content_id, |
43 | | - id, |
44 | | - type_, |
45 | | - additional_properties: std::collections::BTreeMap::new(), |
46 | | - _unparsed: false, |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - pub fn additional_properties( |
51 | | - mut self, |
52 | | - value: std::collections::BTreeMap<String, serde_json::Value>, |
53 | | - ) -> Self { |
54 | | - self.additional_properties = value; |
55 | | - self |
56 | | - } |
| 9 | +#[serde(untagged)] |
| 10 | +pub enum LLMObsAnnotatedInteractionItem { |
| 11 | + LLMObsTraceAnnotatedInteractionItem( |
| 12 | + Box<crate::datadogV2::model::LLMObsTraceAnnotatedInteractionItem>, |
| 13 | + ), |
| 14 | + LLMObsDisplayBlockAnnotatedInteractionItem( |
| 15 | + Box<crate::datadogV2::model::LLMObsDisplayBlockAnnotatedInteractionItem>, |
| 16 | + ), |
| 17 | + UnparsedObject(crate::datadog::UnparsedObject), |
57 | 18 | } |
58 | 19 |
|
59 | 20 | impl<'de> Deserialize<'de> for LLMObsAnnotatedInteractionItem { |
60 | 21 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
61 | 22 | where |
62 | 23 | D: Deserializer<'de>, |
63 | 24 | { |
64 | | - struct LLMObsAnnotatedInteractionItemVisitor; |
65 | | - impl<'a> Visitor<'a> for LLMObsAnnotatedInteractionItemVisitor { |
66 | | - type Value = LLMObsAnnotatedInteractionItem; |
67 | | - |
68 | | - fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result { |
69 | | - f.write_str("a mapping") |
| 25 | + let value: serde_json::Value = Deserialize::deserialize(deserializer)?; |
| 26 | + if let Ok(_v) = serde_json::from_value::< |
| 27 | + Box<crate::datadogV2::model::LLMObsTraceAnnotatedInteractionItem>, |
| 28 | + >(value.clone()) |
| 29 | + { |
| 30 | + if !_v._unparsed { |
| 31 | + return Ok(LLMObsAnnotatedInteractionItem::LLMObsTraceAnnotatedInteractionItem(_v)); |
70 | 32 | } |
71 | | - |
72 | | - fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> |
73 | | - where |
74 | | - M: MapAccess<'a>, |
75 | | - { |
76 | | - let mut annotations: Option<Vec<crate::datadogV2::model::LLMObsAnnotationItem>> = |
77 | | - None; |
78 | | - let mut content_id: Option<String> = None; |
79 | | - let mut id: Option<String> = None; |
80 | | - let mut type_: Option<crate::datadogV2::model::LLMObsInteractionType> = None; |
81 | | - let mut additional_properties: std::collections::BTreeMap< |
82 | | - String, |
83 | | - serde_json::Value, |
84 | | - > = std::collections::BTreeMap::new(); |
85 | | - let mut _unparsed = false; |
86 | | - |
87 | | - while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? { |
88 | | - match k.as_str() { |
89 | | - "annotations" => { |
90 | | - annotations = |
91 | | - Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
92 | | - } |
93 | | - "content_id" => { |
94 | | - content_id = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
95 | | - } |
96 | | - "id" => { |
97 | | - id = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
98 | | - } |
99 | | - "type" => { |
100 | | - type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
101 | | - if let Some(ref _type_) = type_ { |
102 | | - match _type_ { |
103 | | - crate::datadogV2::model::LLMObsInteractionType::UnparsedObject(_type_) => { |
104 | | - _unparsed = true; |
105 | | - }, |
106 | | - _ => {} |
107 | | - } |
108 | | - } |
109 | | - } |
110 | | - &_ => { |
111 | | - if let Ok(value) = serde_json::from_value(v.clone()) { |
112 | | - additional_properties.insert(k, value); |
113 | | - } |
114 | | - } |
115 | | - } |
116 | | - } |
117 | | - let annotations = |
118 | | - annotations.ok_or_else(|| M::Error::missing_field("annotations"))?; |
119 | | - let content_id = content_id.ok_or_else(|| M::Error::missing_field("content_id"))?; |
120 | | - let id = id.ok_or_else(|| M::Error::missing_field("id"))?; |
121 | | - let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?; |
122 | | - |
123 | | - let content = LLMObsAnnotatedInteractionItem { |
124 | | - annotations, |
125 | | - content_id, |
126 | | - id, |
127 | | - type_, |
128 | | - additional_properties, |
129 | | - _unparsed, |
130 | | - }; |
131 | | - |
132 | | - Ok(content) |
| 33 | + } |
| 34 | + if let Ok(_v) = serde_json::from_value::< |
| 35 | + Box<crate::datadogV2::model::LLMObsDisplayBlockAnnotatedInteractionItem>, |
| 36 | + >(value.clone()) |
| 37 | + { |
| 38 | + if !_v._unparsed { |
| 39 | + return Ok( |
| 40 | + LLMObsAnnotatedInteractionItem::LLMObsDisplayBlockAnnotatedInteractionItem(_v), |
| 41 | + ); |
133 | 42 | } |
134 | 43 | } |
135 | 44 |
|
136 | | - deserializer.deserialize_any(LLMObsAnnotatedInteractionItemVisitor) |
| 45 | + return Ok(LLMObsAnnotatedInteractionItem::UnparsedObject( |
| 46 | + crate::datadog::UnparsedObject { value }, |
| 47 | + )); |
137 | 48 | } |
138 | 49 | } |
0 commit comments