pub struct ClaudeHarness;Expand description
Claude Code CLI as a Harness.
Implementations§
Source§impl ClaudeHarness
impl ClaudeHarness
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
examples/setup.rs (line 16)
15fn main() -> Result<(), HarnessError> {
16 let claude = Claude::new();
17
18 // A logger for the install/login progress stream.
19 let log: harness::InstallCallback = Arc::new(|ev| match ev {
20 InstallEvent::Step { text } => eprintln!("• {text}"),
21 InstallEvent::Stdout { text } | InstallEvent::Stderr { text } => eprintln!(" {text}"),
22 InstallEvent::Done { ok, .. } => eprintln!("done (ok={ok})"),
23 });
24
25 let r = claude.readiness();
26 // Fallible calls return the typed `HarnessError`; `?` propagates it.
27 if !r.installed {
28 claude.install(Arc::clone(&log))?; // npm i -g @anthropic-ai/claude-code
29 }
30 if !r.auth_configured {
31 claude.login(log)?; // `claude auth login` — opens the browser
32 }
33
34 println!("ready: {}", claude.readiness().ready);
35 Ok(())
36}More examples
examples/run_prompt.rs (line 12)
9fn main() -> Result<(), HarnessError> {
10 // Pick a harness. `Claude` drives the `claude` CLI (must be installed +
11 // signed in). Swap for `harness::Bob::new()` or `harness::Codex::new()`.
12 let claude = Claude::new();
13
14 // `run_channel()` starts the run and hands back the events on a channel,
15 // so there's no callback/`Sender` plumbing to write by hand. It returns
16 // immediately; events arrive on background threads. (`run()` is still
17 // there for push semantics — forwarding straight onto a Tauri Channel or
18 // SSE sink from inside a callback.)
19 let (_handle, rx) = claude.run_channel(RunRequest {
20 run_id: "demo".into(),
21 prompt: "In one sentence, what is a Markdown heading?".into(),
22 cwd: None, // working dir for the agent's tool calls
23 mode: RunMode::Ask, // Ask = answer only; Edit = may edit files
24 tuning: RunTuning::default(), // optional: model / effort / max_turns
25 })?; // keep `_handle` to `.cancel()`; dropping it does NOT stop the run
26
27 // ONE normalized event stream, regardless of the backing CLI. `rx` hangs
28 // up on its own when the run ends, so this loop terminates without
29 // touching the handle:
30 for ev in rx {
31 match ev {
32 RunEvent::Text { delta, .. } => print!("{delta}"), // the answer
33 RunEvent::Thinking { delta, .. } => eprint!("{delta}"), // model reasoning
34 RunEvent::ToolStart { name, .. } => eprintln!("\n[tool] {name}"),
35 RunEvent::Error { message, .. } => eprintln!("\n[error] {message}"),
36 RunEvent::Exited { .. } => break,
37 _ => {}
38 }
39 }
40 Ok(())
41}Trait Implementations§
Source§impl Clone for ClaudeHarness
impl Clone for ClaudeHarness
Source§fn clone(&self) -> ClaudeHarness
fn clone(&self) -> ClaudeHarness
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ClaudeHarness
impl Debug for ClaudeHarness
Source§impl Default for ClaudeHarness
impl Default for ClaudeHarness
Source§fn default() -> ClaudeHarness
fn default() -> ClaudeHarness
Returns the “default value” for a type. Read more
Source§impl Harness for ClaudeHarness
impl Harness for ClaudeHarness
Source§fn info(&self) -> HarnessInfo
fn info(&self) -> HarnessInfo
Static metadata for the UI.
Source§fn readiness(&self) -> HarnessReadiness
fn readiness(&self) -> HarnessReadiness
Probe availability / version / auth. May shell out; callers
should treat it as blocking and run it off the UI thread.
Source§fn install(&self, on_event: InstallCallback) -> Result<(), HarnessError>
fn install(&self, on_event: InstallCallback) -> Result<(), HarnessError>
Stream a one-time install. Harnesses that need no install
(e.g. a hosted-API adapter) return
Ok(()) immediately.Source§fn run(
&self,
request: RunRequest,
on_event: RunCallback,
) -> Result<RunHandle, HarnessError>
fn run( &self, request: RunRequest, on_event: RunCallback, ) -> Result<RunHandle, HarnessError>
Start a run, streaming events through
on_event. Returns a
handle immediately; work continues on background threads.Source§fn credential(&self) -> CredentialSpec
fn credential(&self) -> CredentialSpec
The credential this harness needs.
Source§fn login(&self, on_event: InstallCallback) -> Result<(), HarnessError>
fn login(&self, on_event: InstallCallback) -> Result<(), HarnessError>
Trigger the harness’s own interactive sign-in (its CLI’s OAuth),
streaming progress as
InstallEvents — the same subprocess
stream shape as install. The flow opens the
user’s browser; this blocks until the login process exits, then
Done { ok } reports success. Default: unsupported — harnesses
that Compose authenticates itself (bob, via its API key) keep it.Source§fn run_channel(
&self,
request: RunRequest,
) -> Result<(RunHandle, Receiver<RunEvent>), HarnessError>
fn run_channel( &self, request: RunRequest, ) -> Result<(RunHandle, Receiver<RunEvent>), HarnessError>
Convenience over
run for callers that want to
pull events off a channel instead of supplying a push callback.
Forwards each RunEvent into an mpsc channel and hands the
receiver back alongside the run handle, so the caller can simply
for event in rx { … } rather than re-write the
Arc::new(move |ev| tx.send(ev)) plumbing at every call site. Read moreAuto Trait Implementations§
impl Freeze for ClaudeHarness
impl RefUnwindSafe for ClaudeHarness
impl Send for ClaudeHarness
impl Sync for ClaudeHarness
impl Unpin for ClaudeHarness
impl UnsafeUnpin for ClaudeHarness
impl UnwindSafe for ClaudeHarness
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more