OCaml Claude SDK using Eio and Jsont
1(*---------------------------------------------------------------------------
2 Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
3 SPDX-License-Identifier: ISC
4 ---------------------------------------------------------------------------*)
5
6(** Object-oriented response handler implementations. *)
7
8(** {1 Handler Interface} *)
9
10class type handler = object
11 method on_text : Response.Text.t -> unit
12 method on_tool_use : Response.Tool_use.t -> unit
13 method on_tool_result : Content_block.Tool_result.t -> unit
14 method on_thinking : Response.Thinking.t -> unit
15 method on_init : Response.Init.t -> unit
16 method on_error : Response.Error.t -> unit
17 method on_complete : Response.Complete.t -> unit
18end
19
20(** {1 Concrete Implementations} *)
21
22class default : handler =
23 object
24 method on_text (_ : Response.Text.t) = ()
25 method on_tool_use (_ : Response.Tool_use.t) = ()
26 method on_tool_result (_ : Content_block.Tool_result.t) = ()
27 method on_thinking (_ : Response.Thinking.t) = ()
28 method on_init (_ : Response.Init.t) = ()
29 method on_error (_ : Response.Error.t) = ()
30 method on_complete (_ : Response.Complete.t) = ()
31 end
32
33class virtual abstract =
34 object
35 method virtual on_text : Response.Text.t -> unit
36 method virtual on_tool_use : Response.Tool_use.t -> unit
37 method virtual on_tool_result : Content_block.Tool_result.t -> unit
38 method virtual on_thinking : Response.Thinking.t -> unit
39 method virtual on_init : Response.Init.t -> unit
40 method virtual on_error : Response.Error.t -> unit
41 method virtual on_complete : Response.Complete.t -> unit
42 end
43
44(** {1 Dispatch Functions} *)
45
46let dispatch (handler : #handler) (response : Response.t) =
47 match response with
48 | Response.Text t -> handler#on_text t
49 | Response.Tool_use t -> handler#on_tool_use t
50 | Response.Tool_result t -> handler#on_tool_result t
51 | Response.Thinking t -> handler#on_thinking t
52 | Response.Init t -> handler#on_init t
53 | Response.Error t -> handler#on_error t
54 | Response.Complete t -> handler#on_complete t
55
56let dispatch_all (handler : #handler) (responses : Response.t list) =
57 List.iter (dispatch handler) responses