@@ -64,43 +64,7 @@ trait JsonFormats {
64
64
}
65
65
}
66
66
67
- implicit lazy val userMessageFormat : Format [UserMessage ] = Json .format[UserMessage ]
68
- implicit lazy val userMessageContentFormat : Format [UserMessageContent ] =
69
- Json .format[UserMessageContent ]
70
- implicit lazy val assistantMessageFormat : Format [AssistantMessage ] =
71
- Json .format[AssistantMessage ]
72
- implicit lazy val assistantMessageContentFormat : Format [AssistantMessageContent ] =
73
- Json .format[AssistantMessageContent ]
74
-
75
- implicit lazy val textBlockFormat : Format [TextBlock ] = Json .format[TextBlock ]
76
-
77
- implicit lazy val contentBlocksFormat : Format [ContentBlocks ] = Json .format[ContentBlocks ]
78
-
79
- implicit lazy val textBlockReads : Reads [TextBlock ] = {
80
- implicit val config : JsonConfiguration = JsonConfiguration (SnakeCase )
81
- Json .reads[TextBlock ]
82
- }
83
-
84
- implicit lazy val textBlockWrites : Writes [TextBlock ] = {
85
- implicit val config : JsonConfiguration = JsonConfiguration (SnakeCase )
86
- Json .writes[TextBlock ]
87
- }
88
-
89
- implicit lazy val mediaBlockWrites : Writes [MediaBlock ] =
90
- (block : MediaBlock ) =>
91
- Json .obj(
92
- " type" -> block.`type`,
93
- " source" -> Json .obj(
94
- " type" -> block.encoding,
95
- " media_type" -> block.mediaType,
96
- " data" -> block.data
97
- )
98
- )
99
-
100
- private def cacheControlToJsObject (maybeCacheControl : Option [CacheControl ]): JsObject =
101
- maybeCacheControl.fold(Json .obj())(cc => writeJsObject(cc))
102
-
103
- implicit lazy val contentBlockWrites : Writes [ContentBlockBase ] = {
67
+ implicit lazy val contentBlockBaseWrites : Writes [ContentBlockBase ] = {
104
68
case ContentBlockBase (textBlock @ TextBlock (_), cacheControl) =>
105
69
Json .obj(" type" -> " text" ) ++
106
70
Json .toJson(textBlock)(textBlockWrites).as[JsObject ] ++
@@ -111,7 +75,7 @@ trait JsonFormats {
111
75
112
76
}
113
77
114
- implicit lazy val contentBlockReads : Reads [ContentBlockBase ] =
78
+ implicit lazy val contentBlockBaseReads : Reads [ContentBlockBase ] =
115
79
(json : JsValue ) => {
116
80
(json \ " type" ).validate[String ].flatMap {
117
81
case " text" =>
@@ -138,6 +102,51 @@ trait JsonFormats {
138
102
}
139
103
}
140
104
105
+ implicit lazy val contentBlockBaseFormat : Format [ContentBlockBase ] = Format (
106
+ contentBlockBaseReads,
107
+ contentBlockBaseWrites
108
+ )
109
+ implicit lazy val contentBlockBaseSeqFormat : Format [Seq [ContentBlockBase ]] = Format (
110
+ Reads .seq(contentBlockBaseReads),
111
+ Writes .seq(contentBlockBaseWrites)
112
+ )
113
+
114
+ implicit lazy val userMessageFormat : Format [UserMessage ] = Json .format[UserMessage ]
115
+ implicit lazy val userMessageContentFormat : Format [UserMessageContent ] =
116
+ Json .format[UserMessageContent ]
117
+ implicit lazy val assistantMessageFormat : Format [AssistantMessage ] =
118
+ Json .format[AssistantMessage ]
119
+ implicit lazy val assistantMessageContentFormat : Format [AssistantMessageContent ] =
120
+ Json .format[AssistantMessageContent ]
121
+
122
+ implicit lazy val textBlockFormat : Format [TextBlock ] = Json .format[TextBlock ]
123
+
124
+ implicit lazy val contentBlocksFormat : Format [ContentBlocks ] = Json .format[ContentBlocks ]
125
+
126
+ implicit lazy val textBlockReads : Reads [TextBlock ] = {
127
+ implicit val config : JsonConfiguration = JsonConfiguration (SnakeCase )
128
+ Json .reads[TextBlock ]
129
+ }
130
+
131
+ implicit lazy val textBlockWrites : Writes [TextBlock ] = {
132
+ implicit val config : JsonConfiguration = JsonConfiguration (SnakeCase )
133
+ Json .writes[TextBlock ]
134
+ }
135
+
136
+ implicit lazy val mediaBlockWrites : Writes [MediaBlock ] =
137
+ (block : MediaBlock ) =>
138
+ Json .obj(
139
+ " type" -> block.`type`,
140
+ " source" -> Json .obj(
141
+ " type" -> block.encoding,
142
+ " media_type" -> block.mediaType,
143
+ " data" -> block.data
144
+ )
145
+ )
146
+
147
+ private def cacheControlToJsObject (maybeCacheControl : Option [CacheControl ]): JsObject =
148
+ maybeCacheControl.fold(Json .obj())(cc => writeJsObject(cc))
149
+
141
150
implicit lazy val contentReads : Reads [Content ] = new Reads [Content ] {
142
151
def reads (json : JsValue ): JsResult [Content ] = json match {
143
152
case JsString (str) => JsSuccess (SingleString (str))
@@ -151,7 +160,7 @@ trait JsonFormats {
151
160
case SingleString (text, cacheControl) =>
152
161
Json .obj(" content" -> text) ++ cacheControlToJsObject(cacheControl)
153
162
case ContentBlocks (blocks) =>
154
- Json .obj(" content" -> Json .toJson(blocks)(Writes .seq(contentBlockWrites )))
163
+ Json .obj(" content" -> Json .toJson(blocks)(Writes .seq(contentBlockBaseWrites )))
155
164
}
156
165
}
157
166
@@ -164,7 +173,7 @@ trait JsonFormats {
164
173
case UserMessageContent (content) =>
165
174
Json .obj(
166
175
" role" -> " user" ,
167
- " content" -> content.map(Json .toJson(_)(contentBlockWrites ))
176
+ " content" -> content.map(Json .toJson(_)(contentBlockBaseWrites ))
168
177
)
169
178
170
179
case AssistantMessage (content, cacheControl) =>
@@ -174,7 +183,7 @@ trait JsonFormats {
174
183
case AssistantMessageContent (content) =>
175
184
Json .obj(
176
185
" role" -> " assistant" ,
177
- " content" -> content.map(Json .toJson(_)(contentBlockWrites ))
186
+ " content" -> content.map(Json .toJson(_)(contentBlockBaseWrites ))
178
187
)
179
188
// Add cases for other subclasses if necessary
180
189
}
0 commit comments