OCaml Claude SDK using Eio and Jsont
at main 31 lines 1.3 kB view raw
1(*--------------------------------------------------------------------------- 2 Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved. 3 SPDX-License-Identifier: ISC 4 ---------------------------------------------------------------------------*) 5 6(* Helper functions for JSON operations in tests using jsont codecs *) 7 8let to_string ?(minify = false) json = 9 let format = if minify then Jsont.Minify else Jsont.Indent in 10 match Jsont_bytesrw.encode_string' ~format Jsont.json json with 11 | Ok s -> s 12 | Error err -> Jsont.Error.to_string err 13 14(* Helper to decode an optional field with a given codec *) 15let get_opt (type a) (codec : a Jsont.t) json key : a option = 16 let field_codec = 17 Jsont.Object.map ~kind:"field" (fun v -> v) 18 |> Jsont.Object.opt_mem key codec ~enc:Fun.id 19 |> Jsont.Object.finish 20 in 21 match Jsont.Json.decode field_codec json with Ok v -> v | Error _ -> None 22 23let get_string json key = get_opt Jsont.string json key 24let get_int json key = get_opt Jsont.int json key 25let get_bool json key = get_opt Jsont.bool json key 26let get_array json key = get_opt (Jsont.list Jsont.json) json key 27 28let as_string json = 29 match Jsont.Json.decode Jsont.string json with 30 | Ok s -> Some s 31 | Error _ -> None