agent-desktop-ffi
它能做什么
为程序员提供桌面自动化库的 C 接口,并附带构建步骤和内存所有权规则。适合希望从自己的代码控制电脑上应用的开发者。
适用场景
- ✓当你想从自己的 Python、Go 或 Swift 程序中调用桌面自动化时。
- ✓当你需要构建 agent-desktop 库并将其链接到应用中时。
- ✓当你希望避免为每个操作都启动命令行工具时。
技能链接
https://github.com/NanmiCoder/cc-haha/blob/main/native/agent-desktop/skills/agent-desktop-ffi/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请展示如何在我的 Python 脚本中调用桌面自动化库,为 Finder 拍一个快照。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能说明如何构建共享库,并按固定顺序调用其函数:初始化、创建适配器、获取快照,然后对引用进行操作。它也涵盖内存所有权和错误处理。
技术内容(原始 SKILL.md)
作者在 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
常见问题
支持哪些平台?
指南涵盖在 macOS、Linux 和 Windows 上构建库,这些平台生成不同类型的库文件。
为什么需要特殊的构建配置?
标准发布配置会在崩溃时使整个宿主程序停止,因此单独的配置可以让错误可以被捕获。
哪一步必须最先完成?
在创建适配器之前,先检查头文件和库的版本是否匹配。
详情
作者的原始描述
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
热度
- GitHub 星标
- 15k
- 排名
- #10913
- 许可证
- —
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
agent-desktop-linux
15k教你的智能体如何使用 agent-desktop 控制 Linux 桌面应用,包括在 X11 和 Wayland 上哪些功能可用,以及常见的首次运行问题修复方法。
agent-desktop-macos
15k教你的智能体如何自动化 Mac 应用,包括授予所需权限,以及处理菜单、对话框和 Chromium 或 Electron 应用。
agent-desktop-windows
15k教你的智能体如何自动化 Windows 应用,涵盖哪些功能可用、PowerShell 引号的用法、最小化窗口以及提权应用的限制。
agent-desktop
15k让你的智能体通过读取无障碍树、点击、输入并检查结果,在 Mac、Windows 或 Linux 上查看和操作桌面应用。