Skip to main content

ClaudeHarness

Struct ClaudeHarness 

Source
pub struct ClaudeHarness;
Expand description

Claude Code CLI as a Harness.

Implementations§

Source§

impl ClaudeHarness

Source

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
Hide additional 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

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for ClaudeHarness

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ClaudeHarness

Source§

fn default() -> ClaudeHarness

Returns the “default value” for a type. Read more
Source§

impl Harness for ClaudeHarness

Source§

fn info(&self) -> HarnessInfo

Static metadata for the UI.
Source§

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>

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>

Start a run, streaming events through on_event. Returns a handle immediately; work continues on background threads.
Source§

fn credential(&self) -> CredentialSpec

The credential this harness needs.
Source§

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>

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 more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.