My own corner of monopam
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

ocaml-claude: rewire README examples to current API

The Basic-query and streaming examples referenced the older
Message.Assistant variant. The current type is Response.Text wrapping a
Response.Text.t value with .content. Rewrite the iter clauses against
the new API and 'open Claude' so module references stay short.

Also extend the (mdx (libraries ...)) stanza with eio.unix and fmt so
the examples compile in the test sandbox.

+21 -14
+20 -13
ocaml-claude/README.md
··· 45 45 ### Basic query 46 46 47 47 ```ocaml 48 + open Claude 49 + 48 50 let () = 49 51 Eio_main.run @@ fun env -> 50 52 Eio.Switch.run @@ fun sw -> ··· 52 54 let clock = Eio.Stdenv.clock env in 53 55 let client = Client.v ~sw ~process_mgr ~clock () in 54 56 Client.query client "What is 2+2?"; 55 - let messages = Client.receive_all client in 57 + let responses = Client.receive_all client in 56 58 List.iter 57 59 (function 58 - | Message.Assistant msg -> 59 - Printf.printf "Claude: %s\n" (Message.Assistant.text msg) 60 + | Response.Text t -> Printf.printf "Claude: %s\n" (Response.Text.content t) 60 61 | _ -> ()) 61 - messages 62 + responses 62 63 ``` 63 64 64 65 ### Streaming responses 65 66 66 67 ```ocaml 68 + open Claude 69 + 67 70 let () = 68 71 Eio_main.run @@ fun env -> 69 72 Eio.Switch.run @@ fun sw -> ··· 73 76 Client.query client "Explain OCaml modules"; 74 77 Client.receive client 75 78 |> Seq.iter (function 76 - | Message.Assistant a -> print_string (Message.Assistant.text a) 79 + | Response.Text t -> print_string (Response.Text.content t) 77 80 | _ -> ()) 78 81 ``` 79 82 80 83 ### With custom handler 81 84 85 + Override the methods you care about by inheriting from `Handler.default`: 86 + 82 87 ```ocaml 88 + open Claude 89 + 90 + let handler = object 91 + inherit Handler.default 92 + method! on_text t = print_string (Response.Text.content t) 93 + method! on_tool_use t = Fmt.pr "Tool: %s\n" (Response.Tool_use.name t) 94 + end 95 + 83 96 let () = 84 97 Eio_main.run @@ fun env -> 85 98 Eio.Switch.run @@ fun sw -> 86 99 let process_mgr = Eio.Stdenv.process_mgr env in 87 100 let clock = Eio.Stdenv.clock env in 88 - let handler = 89 - Handler.default 90 - ~on_text:(fun _client text -> print_string text) 91 - ~on_tool_use:(fun _client tool -> 92 - Fmt.pr "Tool: %s\n" (Response.Tool_use.name tool)) 93 - () 94 - in 95 101 let client = Client.v ~sw ~process_mgr ~clock () in 96 - Client.run client ~handler "Summarize this project" 102 + Client.query client "Summarize this project"; 103 + Client.receive client |> Seq.iter (Handler.dispatch handler) 97 104 ``` 98 105 99 106 ## Requirements
+1 -1
ocaml-claude/dune
··· 10 10 11 11 (mdx 12 12 (files README.md) 13 - (libraries claude eio eio.core eio_main)) 13 + (libraries claude eio eio.core eio.unix eio_main fmt))