agent-desktop-ffi
What it does
Gives programmers the C interface for the desktop automation library, with build steps and ownership rules. It is for developers who want to control apps on a computer from their own code.
When to use it
- βWhen you want to call desktop automation from your own Python, Go or Swift program.
- βWhen you need to build the agent-desktop library and link it into an app.
- βWhen you want to avoid starting the command-line tool for each action.
Skill link
https://github.com/NanmiCoder/cc-haha/blob/main/native/agent-desktop/skills/agent-desktop-ffi/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agentβs skills folder.
Example of what to ask your AI
βShow me how to call the desktop automation library from my Python script to take a snapshot of Finder.β
5 new skills a week. 1 minute to read.
How it works
The skill explains how to build the shared library and call its functions in a fixed order: initialize, create an adapter, take a snapshot, then act on references. It also covers memory ownership and errors.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
agent-desktop-ffi
Direct C-ABI access to PlatformAdapter operations. Build the cdylib with the
release-ffi profile:
cargo build --profile release-ffi -p agent-desktop-ffi
The output is target/release-ffi/libagent_desktop_ffi.dylib (.so on Linux,
agent_desktop_ffi.dll on Windows) plus a committed C header at
crates/ffi/include/agent_desktop.h. The default release profile uses
panic = "abort": a Rust panic inside an extern "C" function would SIGABRT
the host. release-ffi keeps panic = "unwind", which the panic traps need.
Four reference topics, loaded as needed:
- ownership.md: who allocates and who frees every pointer the FFI hands back.
- error-handling.md: errno-style last-error contract, error codes, enum validation, panic boundary.
- threading.md: host-thread contract, mutation ordering across threads and processes, adapter-bound native handles, AXIsProcessTrusted inheritance.
- build-and-link.md: prebuilt archives, ABI handshake, struct size validation, C and Python examples.
Observe-act workflow (canonical path)
ad_init(AD_ABI_VERSION_MAJOR) // verify header β dylib match
adapter = ad_adapter_create_with_session("s1") // or ad_adapter_create()
rc = ad_snapshot(adapter, "Finder", 0, 10, false, false, &json_out)
// parse json_out: locate snapshot-qualified refs in data.tree
ad_free_string(json_out)
// build action:
AdAction act = {0}; act.kind = AD_ACTION_KIND_CLICK;
rc = ad_execute_by_ref(adapter, "@s8f3k2p9:e5", &act, 0, &result_out)
ad_free_string(result_out)
ad_adapter_destroy(adapter)
ad_snapshot returns the CLI envelope {version, ok, command, data}.
data.tree carries snapshot-qualified refs for interactive elements. Read
data.complete before trusting a tree: false means the observation budget ran
out and the tree is partial, not an error. ad_snapshot always targets the
focused window of app; window targeting, skeleton mode and --root drill-down
are not exposed over the ABI. A bare @eN ref returns
AD_RESULT_ERR_INVALID_ARGS.
Core constraints
ABI handshake. Call
ad_init(AD_ABI_VERSION_MAJOR)once after loading the library. A mismatch returnsAD_RESULT_ERR_INVALID_ARGS: abort.ad_abi_version()returns the library's major for display. The major increments on a removed symbol or a layout change; newad_*symbols and error codes are additive. Pin the exactlibagent_desktop_ffiversion you link against.Session adapters.
ad_adapter_create_with_session("session-id")scopes refmap persistence to a session namespace, like CLI--session <id>. IDs are 1 to 64 ASCII alphanumeric,-, or_characters. A null ID behaves likead_adapter_create(). An invalid ID returns null (readad_last_error_*).Ref actions.
ad_execute_by_refis the primary action entrypoint. It loads the RefStore, resolves the ref strictly (STALE_REFwhen the snapshot has no such ref or the element changed,AMBIGUOUS_TARGETon several candidates), runs the live actionability preflight (visible, stable, enabled, supported action, policy, editable), then dispatches. A disabled or unsupported target fails before any platform call; onAD_RESULT_ERR_ACTION_FAILEDthe check report is JSON inad_last_error_details(). It auto-waits 5000 ms before the preflight;ad_execute_by_ref_timeouttakestimeout_ms(-1= 5000 ms default,0= single-shot).PressKeydefaults tofocus_fallback; every other action, includingTypeText, defaults toheadless. PassAD_POLICY_KIND_FOCUS_FALLBACK(1) to allow focus without cursor movement, orAD_POLICY_KIND_HEADED(2) to allow physical cursor and keyboard delivery.ad_execute_ref_action_exact_with_policyruns the same preflight from anAdExactRefEntry.Generation-safe direct APIs. Every direct targeting function takes process-generation evidence:
AdExactRefEntry,AdExactWindowInfo,ad_list_windows_exact,ad_launch_app_exact, and the other*_exactsymbols.AdRefEntry,AdWindowInfo, andAdSurfaceInfoexist only as fields embedded in the exact structs.ad_list_surfaces_exactkeeps the surfaceid. FromAD_EXACT_SURFACE_INFO_VERSION2,AdExactSurfaceInfo.unclassifiednames the surface kinds its probe could not read (for examplesheet,menu), or is null. A missi
FAQ
Which platforms are supported?
The guide covers building the library on macOS, Linux and Windows, which produce different library file types.
Why is a special build profile needed?
The standard release profile would stop the whole host program on a crash, so the separate profile keeps errors catchable.
Which step must come first?
Check that the header and library versions match before creating an adapter.
Details
Original description by the author
C-ABI bindings over agent-desktop's PlatformAdapter. Consumers (Python ctypes, Swift, Node ffi-napi, Go cgo, C++, Ruby fiddle) link libagent_desktop_ffi.{dylib,so,dll} and call `ad_*` functions directly instead of spawning the CLI binary per call. The canonical observe-act workflow is: ad_init β ad_adapter_create[_with_session] β ad_snapshot β read qualified refs (@s8f3k2p9:e5) β ad_execute_by_ref β ad_free_string β ad_adapter_destroy.
repo: NanmiCoder/cc-haha
Popularity
- GitHub stars
- 15k
- Ranking position
- #10913
- License
- β
- Updated
- 11/2/2026
Comments
- β¦
More skills from this repository
agent-desktop-linux
15kTeaches your agent how to control Linux desktop apps with agent-desktop, including what works on X11 and Wayland and common first-run fixes.
agent-desktop-macos
15kShows your agent how to automate Mac apps, including granting the needed permissions and working with menus, dialogs and Chromium or Electron apps.
agent-desktop-windows
15kShows your agent how to automate Windows apps, covering what works, PowerShell quoting, minimized windows and elevated-app limits.
agent-desktop
15kLets your agent see and operate desktop apps on Mac, Windows or Linux by reading the accessibility tree, clicking, typing and checking results.
Similar skills
api-connector-builder
280kHas your agent build new API connections following the existing pattern. Use when you need to add one more integration without creating a new way of doing things.
api-design
280kHelps your agent design REST APIs following best practices. It's useful for defining how APIs communicate, how errors are shown, and how to organize data.
backend-patterns
280kHelps your agent with server-side architecture patterns and database optimization. It's useful for building or reviewing code that runs on the server.