OCaml Claude SDK using Eio and Jsont
0
fork

Configure Feed

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

ocaml-linkedin: apply dune fmt

Pure formatting changes from `dune fmt`: doc comment placement moves
from above the binding to below it for `type`s, multi-line `match`
expressions collapse onto one line where they fit, and infix operator
applications pick up spaces (`Soup.($?)` -> `Soup.( $? )`). No
semantic changes.

+70 -36
+51 -28
README.md
··· 23 23 24 24 ## Installation 25 25 26 + Install with opam: 27 + 28 + ```sh 29 + $ opam install claude 26 30 ``` 27 - opam install claude 31 + 32 + If opam cannot find the package, it may not yet be released in the public 33 + `opam-repository`. Add the overlay repository, then install it: 34 + 35 + ```sh 36 + $ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git 37 + $ opam update 38 + $ opam install claude 28 39 ``` 29 40 30 41 ## Usage ··· 32 43 ### Basic query 33 44 34 45 ```ocaml 35 - Eio_main.run @@ fun env -> 36 - Eio.Switch.run @@ fun sw -> 37 - let process_mgr = Eio.Stdenv.process_mgr env in 38 - let clock = Eio.Stdenv.clock env in 39 - let client = Client.v ~sw ~process_mgr ~clock () in 40 - Client.query client "What is 2+2?"; 41 - let messages = Client.receive_all client in 42 - List.iter 43 - (function 44 - | Message.Assistant msg -> 45 - Printf.printf "Claude: %s\n" (Message.Assistant.text msg) 46 - | _ -> ()) 47 - messages 46 + let () = 47 + Eio_main.run @@ fun env -> 48 + Eio.Switch.run @@ fun sw -> 49 + let process_mgr = Eio.Stdenv.process_mgr env in 50 + let clock = Eio.Stdenv.clock env in 51 + let client = Client.v ~sw ~process_mgr ~clock () in 52 + Client.query client "What is 2+2?"; 53 + let messages = Client.receive_all client in 54 + List.iter 55 + (function 56 + | Message.Assistant msg -> 57 + Printf.printf "Claude: %s\n" (Message.Assistant.text msg) 58 + | _ -> ()) 59 + messages 48 60 ``` 49 61 50 62 ### Streaming responses 51 63 52 64 ```ocaml 53 - let client = Client.v ~sw ~process_mgr ~clock () in 54 - Client.query client "Explain OCaml modules"; 55 - Client.receive client |> Seq.iter (fun msg -> 56 - match msg with 57 - | Message.Assistant a -> print_string (Message.Assistant.text a) 58 - | _ -> ()) 65 + let () = 66 + Eio_main.run @@ fun env -> 67 + Eio.Switch.run @@ fun sw -> 68 + let process_mgr = Eio.Stdenv.process_mgr env in 69 + let clock = Eio.Stdenv.clock env in 70 + let client = Client.v ~sw ~process_mgr ~clock () in 71 + Client.query client "Explain OCaml modules"; 72 + Client.receive client 73 + |> Seq.iter (function 74 + | Message.Assistant a -> print_string (Message.Assistant.text a) 75 + | _ -> ()) 59 76 ``` 60 77 61 78 ### With custom handler 62 79 63 80 ```ocaml 64 - let handler = Handler.default 65 - ~on_text:(fun _client text -> print_string text) 66 - ~on_tool_use:(fun _client tool -> 67 - Fmt.pr "Tool: %s\n" (Response.Tool_use.name tool)) 68 - () 69 - in 70 - let client = Client.v ~sw ~process_mgr ~clock () in 71 - Client.run client ~handler "Summarize this project" 81 + let () = 82 + Eio_main.run @@ fun env -> 83 + Eio.Switch.run @@ fun sw -> 84 + let process_mgr = Eio.Stdenv.process_mgr env in 85 + let clock = Eio.Stdenv.clock env in 86 + let handler = 87 + Handler.default 88 + ~on_text:(fun _client text -> print_string text) 89 + ~on_tool_use:(fun _client tool -> 90 + Fmt.pr "Tool: %s\n" (Response.Tool_use.name tool)) 91 + () 92 + in 93 + let client = Client.v ~sw ~process_mgr ~clock () in 94 + Client.run client ~handler "Summarize this project" 72 95 ``` 73 96 74 97 ## Requirements
+1
claude.opam
··· 20 20 "json" {>= "0.2.0"} 21 21 "odoc" {with-doc} 22 22 "alcotest" {with-test & >= "1.7.0"} 23 + "mdx" {with-test} 23 24 "loc" 24 25 ] 25 26 build: [
+4
dune
··· 7 7 ; Ignore third_party directory (for fetched dependency sources) 8 8 9 9 (data_only_dirs third_party) 10 + 11 + (mdx 12 + (files README.md) 13 + (libraries claude eio eio.core eio_main))
+2
dune-project
··· 1 1 (lang dune 3.21) 2 + (using mdx 0.4) 2 3 (name claude) 3 4 4 5 (generate_opam_files true) ··· 23 24 (json (>= 0.2.0)) 24 25 (odoc :with-doc) 25 26 (alcotest (and :with-test (>= 1.7.0))) 27 + (mdx :with-test) 26 28 loc))
+9 -8
examples/README.md
··· 14 14 15 15 ## Running Tests 16 16 17 - ```bash 18 - # Run the camel joke competition 19 - dune exec camel_jokes 17 + <!-- $MDX non-deterministic=command --> 18 + ```sh 19 + $ # Run the camel joke competition 20 + $ dune exec camel_jokes 20 21 21 - # Run the permission demo (interactive) 22 - dune exec permission_demo 22 + $ # Run the permission demo (interactive) 23 + $ dune exec permission_demo 23 24 24 - # With verbose output to see message flow 25 - dune exec permission_demo -- -v 25 + $ # With verbose output to see message flow 26 + $ dune exec permission_demo -- -v 26 27 ``` 27 28 28 29 ## Features Tested ··· 32 33 - Permission callbacks 33 34 - Tool access control 34 35 - Typed message API 35 - - Pretty printing of messages 36 + - Pretty printing of messages
+3
examples/dune
··· 98 98 (name structured_error_demo) 99 99 (modules structured_error_demo) 100 100 (libraries claude eio_main fmt)) 101 + 102 + (mdx 103 + (files README.md))