package chatoyant
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
OCaml-first LLM SDK with Melange-generated JavaScript
Install
dune-project
Dependency
Authors
Maintainers
Sources
chatoyant-v0.12.4.tbz
sha256=fbf19dea0d39482e0263812504d94c682a9f3a6e93f68fe0c57b2d38ed4166b3
sha512=4e01be9c0ec9c16880f98360dec4ef5e0dddd9192e2113991b154e891b3bdaefb614051c2cb63afa0b714efa0e4cc4fd8054528fdc1995f3a4858fe2fcd50256
doc/src/chatoyant.schema/codec.ml.html
Source file codec.ml
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 297type 'a t = { schema : Schema.field; encode : 'a -> Chatoyant_runtime.Json.t; decode : Chatoyant_runtime.Json.t -> ('a, string) result; missing : (unit -> ('a, string) result) option; } type ('record, 'value) field = { name : string; codec : 'value t; get : 'record -> 'value; } let schema codec = codec.schema let custom ~schema ~encode ~decode = { schema; encode; decode; missing = None } let map_schema map codec = { codec with schema = map codec.schema } let to_json_schema codec = Schema.to_json_schema codec.schema let encode codec value = codec.encode value let decode codec json = codec.decode json let ok value = Ok value let json ?description () = { schema = Schema.object_ ?description []; encode = Fun.id; decode = ok; missing = None; } let string ?description () = { schema = Schema.string ?description (); encode = (fun value -> Chatoyant_runtime.Json.String value); decode = (function | Chatoyant_runtime.Json.String value -> Ok value | _ -> Error "expected string"); missing = None; } let float ?description () = { schema = Schema.number ?description (); encode = (fun value -> Chatoyant_runtime.Json.Float value); decode = (function | Chatoyant_runtime.Json.Float value -> Ok value | _ -> Error "expected number"); missing = None; } let int ?description () = { schema = Schema.integer ?description (); encode = (fun value -> Chatoyant_runtime.Json.Float (Float.of_int value)); decode = (fun json -> match Chatoyant_runtime.Json.as_int json with | Some value -> Ok value | None -> Error "expected integer"); missing = None; } let bool ?description () = { schema = Schema.boolean ?description (); encode = (fun value -> Chatoyant_runtime.Json.Bool value); decode = (function | Chatoyant_runtime.Json.Bool value -> Ok value | _ -> Error "expected boolean"); missing = None; } let string_enum ?description values = let json_values = List.map (fun value -> Chatoyant_runtime.Json.String value) values in { schema = Schema.enum ?description json_values; encode = (fun value -> Chatoyant_runtime.Json.String value); decode = (function | Chatoyant_runtime.Json.String value when List.mem value values -> Ok value | Chatoyant_runtime.Json.String value -> Error ("unexpected enum value: " ^ value) | _ -> Error "expected enum string"); missing = None; } let array ?description item = { schema = Schema.array ?description item.schema; encode = (fun values -> Chatoyant_runtime.Json.Array (List.map item.encode values)); decode = (function | Chatoyant_runtime.Json.Array values -> let rec loop acc = function | [] -> Ok (List.rev acc) | value :: rest -> ( match item.decode value with | Ok decoded -> loop (decoded :: acc) rest | Error message -> Error message) in loop [] values | _ -> Error "expected array"); missing = None; } let set_base_optional optional base = { base with Schema.optional } let with_optional = function | Schema.String options -> Schema.String { options with string_base = set_base_optional true options.string_base; } | Schema.Number options -> Schema.Number { options with number_base = set_base_optional true options.number_base; } | Schema.Integer options -> Schema.Integer { options with number_base = set_base_optional true options.number_base; } | Schema.Boolean options -> Schema.Boolean { options with boolean_base = set_base_optional true options.boolean_base; } | Schema.Null base -> Schema.Null (set_base_optional true base) | Schema.Array options -> Schema.Array { options with array_base = set_base_optional true options.array_base } | Schema.Object options -> Schema.Object { options with object_base = set_base_optional true options.object_base; } | Schema.Enum options -> Schema.Enum { options with enum_base = set_base_optional true options.enum_base } | Schema.Literal options -> Schema.Literal { options with literal_base = set_base_optional true options.literal_base; } let optional codec = { schema = with_optional codec.schema; encode = (function | None -> Chatoyant_runtime.Json.Null | Some value -> codec.encode value); decode = (function | Chatoyant_runtime.Json.Null -> Ok None | json -> Result.map Option.some (codec.decode json)); missing = Some (fun () -> Ok None); } let field name codec ~get = { name; codec; get } let decode_field name codec json = match Chatoyant_runtime.Json.field name json with | None -> ( match codec.missing with | Some missing -> missing () | None -> Error ("missing field: " ^ name)) | Some value -> ( match codec.decode value with | Ok _ as ok -> ok | Error message -> Error (name ^ ": " ^ message)) let decode_record_field json field = decode_field field.name field.codec json let encode_field record field = (field.name, field.codec.encode (field.get record)) let schema_field field = (field.name, field.codec.schema) let object1 ?description f1 make = { schema = Schema.object_ ?description [ schema_field f1 ]; encode = (fun record -> Chatoyant_runtime.Json.Object [ encode_field record f1 ]); decode = (fun json -> Result.map make (decode_record_field json f1)); missing = None; } let object2 ?description f1 f2 make = { schema = Schema.object_ ?description [ schema_field f1; schema_field f2 ]; encode = (fun record -> Chatoyant_runtime.Json.Object [ encode_field record f1; encode_field record f2 ]); decode = (fun json -> let ( let* ) = Result.bind in let* v1 = decode_record_field json f1 in let* v2 = decode_record_field json f2 in Ok (make v1 v2)); missing = None; } let object3 ?description f1 f2 f3 make = { schema = Schema.object_ ?description [ schema_field f1; schema_field f2; schema_field f3 ]; encode = (fun record -> Chatoyant_runtime.Json.Object [ encode_field record f1; encode_field record f2; encode_field record f3; ]); decode = (fun json -> let ( let* ) = Result.bind in let* v1 = decode_record_field json f1 in let* v2 = decode_record_field json f2 in let* v3 = decode_record_field json f3 in Ok (make v1 v2 v3)); missing = None; } let object4 ?description f1 f2 f3 f4 make = { schema = Schema.object_ ?description [ schema_field f1; schema_field f2; schema_field f3; schema_field f4 ]; encode = (fun record -> Chatoyant_runtime.Json.Object [ encode_field record f1; encode_field record f2; encode_field record f3; encode_field record f4; ]); decode = (fun json -> let ( let* ) = Result.bind in let* v1 = decode_record_field json f1 in let* v2 = decode_record_field json f2 in let* v3 = decode_record_field json f3 in let* v4 = decode_record_field json f4 in Ok (make v1 v2 v3 v4)); missing = None; } let object5 ?description f1 f2 f3 f4 f5 make = { schema = Schema.object_ ?description [ schema_field f1; schema_field f2; schema_field f3; schema_field f4; schema_field f5; ]; encode = (fun record -> Chatoyant_runtime.Json.Object [ encode_field record f1; encode_field record f2; encode_field record f3; encode_field record f4; encode_field record f5; ]); decode = (fun json -> let ( let* ) = Result.bind in let* v1 = decode_record_field json f1 in let* v2 = decode_record_field json f2 in let* v3 = decode_record_field json f3 in let* v4 = decode_record_field json f4 in let* v5 = decode_record_field json f5 in Ok (make v1 v2 v3 v4 v5)); missing = None; }
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>