My aggregated monorepo of OCaml code, automaintained
at doc-fixes 58 lines 2.2 kB view raw
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 = 11 object 12 method on_text : Response.Text.t -> unit 13 method on_tool_use : Response.Tool_use.t -> unit 14 method on_tool_result : Content_block.Tool_result.t -> unit 15 method on_thinking : Response.Thinking.t -> unit 16 method on_init : Response.Init.t -> unit 17 method on_error : Response.Error.t -> unit 18 method on_complete : Response.Complete.t -> unit 19 end 20 21(** {1 Concrete Implementations} *) 22 23class default : handler = 24 object 25 method on_text (_ : Response.Text.t) = () 26 method on_tool_use (_ : Response.Tool_use.t) = () 27 method on_tool_result (_ : Content_block.Tool_result.t) = () 28 method on_thinking (_ : Response.Thinking.t) = () 29 method on_init (_ : Response.Init.t) = () 30 method on_error (_ : Response.Error.t) = () 31 method on_complete (_ : Response.Complete.t) = () 32 end 33 34class virtual abstract = 35 object 36 method virtual on_text : Response.Text.t -> unit 37 method virtual on_tool_use : Response.Tool_use.t -> unit 38 method virtual on_tool_result : Content_block.Tool_result.t -> unit 39 method virtual on_thinking : Response.Thinking.t -> unit 40 method virtual on_init : Response.Init.t -> unit 41 method virtual on_error : Response.Error.t -> unit 42 method virtual on_complete : Response.Complete.t -> unit 43 end 44 45(** {1 Dispatch Functions} *) 46 47let dispatch (handler : #handler) (response : Response.t) = 48 match response with 49 | Response.Text t -> handler#on_text t 50 | Response.Tool_use t -> handler#on_tool_use t 51 | Response.Tool_result t -> handler#on_tool_result t 52 | Response.Thinking t -> handler#on_thinking t 53 | Response.Init t -> handler#on_init t 54 | Response.Error t -> handler#on_error t 55 | Response.Complete t -> handler#on_complete t 56 57let dispatch_all (handler : #handler) (responses : Response.t list) = 58 List.iter (dispatch handler) responses