Install

Cargo.toml

[dependencies]
autohand-sdk = { git = "https://github.com/autohandai/code-agent-sdk-rust" }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

`Config`

Configuration sets the working directory, CLI binary path, debug output, request timeout, model override, skills, system prompt additions, and execution-mode flags.

rust

let config = Config::from_env()
    .with_cwd(".")
    .with_model("fantail2")
    .with_skill("rust")
    .with_instructions("Prefer small, typed Rust APIs.");

`AutohandSdk`

Use the low-level wrapper when you need direct JSON-RPC control.

  • start() / stop()
  • request(method, params)
  • prompt(message, options)
  • stream_prompt(message, options)
  • interrupt()
  • set_plan_mode(enabled)
  • set_permission_mode(mode)
  • set_model(model)
  • get_state()
  • get_messages()
  • permission_response(request_id, decision)

`Agent`

The high-level agent API is the best fit for product code that sends prompts, streams events, and waits for final results.

rust

let mut agent = Agent::create(Config::from_env().with_cwd(".")).await?;
let mut run = agent.send("Review the public API.").await?;
let result = run.wait().await?;
agent.close().await?;
  • Agent::create(config)
  • Agent::from_sdk(sdk)
  • send(prompt)
  • run(prompt)
  • run_json<T>(prompt, options)
  • allow_permission(request_id)
  • deny_permission(request_id)
  • set_plan_mode(enabled)
  • close()

Run

  • next(): receive the next event
  • wait(): wait until the run finishes and collect text/events
  • json<T>(): parse final output as JSON
  • abort(): interrupt the current run

CLI Runtime Control

Core lifecycle and auto-mode operations are available on Agent and AutohandSdk. Use AutohandSdk for the complete approval, session, MCP, learning, tool-registry, and context surface.

  • Conversation and handoff: reset, create_browser_handoff, attach_browser_handoff, attach_latest_browser_handoff
  • Auto-mode: start_automode, get_automode_status, pause_automode, resume_automode, cancel_automode, get_automode_log
  • Approvals and sessions: acknowledge_permission, respond_to_directory_access, acknowledge_directory_access, decide_changes, get_history, get_session, attach_session
  • Integrations and context: set_yolo, set_yolo_alias, set_vscode_mcp_tools, respond_to_mcp_invocation, recommend_project_learning, update_project_learning, generate_project_skill, get_tools_registry, set_context_compact

See Control the CLI runtime for behavior, safety contracts, and the equivalent names in every CLI-backed SDK.

SDK events

All CLI-backed SDKs expose the same runtime event names, with language-specific wrappers or helper methods around the raw JSON payload.

  • agent_start
  • turn_start
  • message_update
  • message_end
  • tool_start
  • tool_update
  • tool_end
  • permission_request
  • automode_iteration, automode_complete, automode_error
  • error

See Hooks and events for the complete normalized event contract.

Structured JSON

Use the JSON helpers when the host application needs typed output from the final assistant response.

rust

#[derive(serde::Deserialize)]
struct ReleaseRisk {
    summary: String,
}

let risk: ReleaseRisk = agent
    .run_json("Assess release readiness.", JsonRunOptions::default())
    .await?;