@@ -16,6 +16,7 @@ import io.cequence.openaiscala.anthropic.domain.Content.ContentBlock.{
1616 TextsContentBlock ,
1717 ThinkingBlock ,
1818 ToolUseBlock ,
19+ WebFetchToolResultBlock ,
1920 WebSearchToolResultBlock
2021}
2122import io .cequence .openaiscala .anthropic .domain .Content .{
@@ -47,6 +48,7 @@ import io.cequence.openaiscala.anthropic.domain._
4748import io .cequence .openaiscala .anthropic .domain .CodeExecutionToolResultContent .CodeExecutionErrorCode
4849import io .cequence .openaiscala .anthropic .domain .BashCodeExecutionToolResultContent .BashCodeExecutionErrorCode
4950import io .cequence .openaiscala .anthropic .domain .WebSearchToolResultContent .WebSearchErrorCode
51+ import io .cequence .openaiscala .anthropic .domain .WebFetchToolResultContent .WebFetchErrorCode
5052import io .cequence .openaiscala .JsonFormats .jsonSchemaFormat
5153import io .cequence .openaiscala .anthropic .domain .skills .{
5254 Container ,
@@ -139,7 +141,7 @@ trait JsonFormats {
139141 // content block - raw - one to one with json
140142 implicit val textContentRawFormat : Format [TextContentRaw ] = Json .format[TextContentRaw ]
141143
142- implicit val citationsFlagRawFormat : Format [CitationsFlagRaw ] = Json .format[CitationsFlagRaw ]
144+ implicit val citationsFlagRawFormat : Format [CitationsFlag ] = Json .format[CitationsFlag ]
143145
144146 implicit val sourceBlockRawFormat : Format [SourceBlockRaw ] = {
145147 implicit val config : JsonConfiguration = JsonConfiguration (SnakeCase )
@@ -267,6 +269,58 @@ trait JsonFormats {
267269 Json .format[WebSearchToolResultBlock ]
268270 }
269271
272+ implicit lazy val webFetchErrorCodeFormat : Format [WebFetchToolResultContent .WebFetchErrorCode ] =
273+ JsonUtil .enumFormat[WebFetchToolResultContent .WebFetchErrorCode ](
274+ WebFetchToolResultContent .WebFetchErrorCode .values: _*
275+ )
276+
277+ private implicit val webFetchSourceFormat : OFormat [WebFetchToolResultContent .Source ] = {
278+ implicit val config : JsonConfiguration = JsonConfiguration (SnakeCase )
279+ Json .format[WebFetchToolResultContent .Source ]
280+ }
281+
282+ private implicit val webFetchDocumentFormat : OFormat [WebFetchToolResultContent .Document ] = {
283+ implicit val config : JsonConfiguration = JsonConfiguration (SnakeCase )
284+ formatWithType(Json .format[WebFetchToolResultContent .Document ])
285+ }
286+
287+ private implicit val webFetchSuccessFormat : OFormat [WebFetchToolResultContent .Success ] = {
288+ implicit val config : JsonConfiguration = JsonConfiguration (SnakeCase )
289+ formatWithType(Json .format[WebFetchToolResultContent .Success ])
290+ }
291+
292+ private implicit val webFetchErrorFormat : Format [WebFetchToolResultContent .Error ] = {
293+ implicit val config : JsonConfiguration = JsonConfiguration (SnakeCase )
294+ formatWithType(Json .format[WebFetchToolResultContent .Error ])
295+ }
296+
297+ implicit lazy val webFetchToolResultContentReads : Reads [WebFetchToolResultContent ] = {
298+ case obj : JsObject =>
299+ (obj \ " type" ).asOpt[String ] match {
300+ case Some (" web_fetch_tool_result_error" ) =>
301+ obj.validate[WebFetchToolResultContent .Error ]
302+ case _ =>
303+ obj.validate[WebFetchToolResultContent .Success ]
304+ }
305+ case _ =>
306+ JsError (" Expected object for web fetch tool result content" )
307+ }
308+
309+ implicit lazy val webFetchToolResultContentWrites : Writes [WebFetchToolResultContent ] = {
310+ case success : WebFetchToolResultContent .Success =>
311+ Json .toJson(success)(webFetchSuccessFormat)
312+ case error : WebFetchToolResultContent .Error =>
313+ Json .toJson(error)(webFetchErrorFormat)
314+ }
315+
316+ implicit lazy val webFetchToolResultContentFormat : Format [WebFetchToolResultContent ] =
317+ Format (webFetchToolResultContentReads, webFetchToolResultContentWrites)
318+
319+ private val webFetchToolResultBlockFormat : OFormat [WebFetchToolResultBlock ] = {
320+ implicit val config : JsonConfiguration = JsonConfiguration (SnakeCase )
321+ Json .format[WebFetchToolResultBlock ]
322+ }
323+
270324 private implicit val toolResultContentFormat : OFormat [MCPToolResultItem ] = {
271325 implicit val config : JsonConfiguration = JsonConfiguration (SnakeCase )
272326 formatWithType(Json .using[Json .WithDefaultValues ].format[MCPToolResultItem ])
@@ -533,6 +587,9 @@ trait JsonFormats {
533587 case " web_search_tool_result" =>
534588 json.validate[WebSearchToolResultBlock ](webSearchToolResultBlockFormat)
535589
590+ case " web_fetch_tool_result" =>
591+ json.validate[WebFetchToolResultBlock ](webFetchToolResultBlockFormat)
592+
536593 case " mcp_tool_use" =>
537594 json.validate[McpToolUseBlock ](mcpToolUseBlockFormat)
538595
@@ -613,6 +670,9 @@ trait JsonFormats {
613670 case x : WebSearchToolResultBlock =>
614671 Json .toJsObject(x)(webSearchToolResultBlockFormat)
615672
673+ case x : WebFetchToolResultBlock =>
674+ Json .toJsObject(x)(webFetchToolResultBlockFormat)
675+
616676 case x : McpToolUseBlock =>
617677 Json .toJsObject(x)(mcpToolUseBlockFormat)
618678
@@ -641,7 +701,7 @@ trait JsonFormats {
641701 ),
642702 title = x.title,
643703 context = x.context,
644- citations = if (x.citations.getOrElse(false )) Some (CitationsFlagRaw (true )) else None
704+ citations = if (x.citations.getOrElse(false )) Some (CitationsFlag (true )) else None
645705 )
646706 )(sourceContentBlockRawFormat)
647707
@@ -658,7 +718,7 @@ trait JsonFormats {
658718 ),
659719 title = x.title,
660720 context = x.context,
661- citations = if (x.citations.getOrElse(false )) Some (CitationsFlagRaw (true )) else None
721+ citations = if (x.citations.getOrElse(false )) Some (CitationsFlag (true )) else None
662722 )
663723 )(sourceContentBlockRawFormat)
664724
@@ -671,7 +731,7 @@ trait JsonFormats {
671731 ),
672732 title = x.title,
673733 context = x.context,
674- citations = if (x.citations.getOrElse(false )) Some (CitationsFlagRaw (true )) else None
734+ citations = if (x.citations.getOrElse(false )) Some (CitationsFlag (true )) else None
675735 )
676736 )(sourceContentBlockRawFormat)
677737 }
0 commit comments