V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
爱意满满的作品展示区。
2code

[分享创造] noni — 让 AI agent 也能跑 `gh auth login` / `ssh-copy-id` 这类交互式 CLI

  •  
  •   2code · Apr 30 · 714 views

    做了个小工具叫 noni,开源在 GitHub: https://github.com/williamwa/noni

    解决什么问题

    gh auth loginssh-copy-idnpm publishnpm login 这些命令默认是交互式的,默认有一个 Terminal, 是人坐在终端前面:选 GitHub.com 还是 Enterprise 、按 y/n 、粘贴 token 、输入密码……

    放到 AI agent 场景就很尴尬:agent 在 sandbox 里跑 bash/exec,碰到这种交互,agent 直接卡住。常见的 workaround:

    • 翻文档找有没有 --non-interactive flag (很多没有)
    • expect 写脚本(脆弱、每个 CLI 重写一次)
    • 让 agent 自己驱动 PTY (要它处理 ANSI 转义、终端尺寸、ECHO 检测,太重)

    noni 的思路是把这件事变成一系列无状态 RPC 调用

    agent: noni run "gh auth login"
    noni:  {id: abc, status: waiting_input, prompt: {type: select, options: [...]}}
    agent: noni key abc enter
    noni:  {id: abc, status: waiting_input, prompt: {type: yesno, default: "y"}}
    agent: noni input abc Y
    noni:  {id: abc, status: waiting_input, prompt: {type: password, echo: false}}
    agent: noni secret abc --env GH_TOKEN
    noni:  {id: abc, status: exited, exit_code: 0}
    

    agent 看到的是结构化的 prompt,也可以用 read --raw 来读这屏幕,然后自己决定下一步发什么。这样整个工具调用流程不会卡住。

    几个设计细节

    1. prompt 类型靠几个不同信号判断:

      • password:termios ECHO 关掉了(最硬的信号,置信度 0.99 )
      • yesno:识别 (y/n) [Y/n] 等模式,从大写字母抽 default
      • select:找 > * 标记 + 缩进选项块
      • input:行尾 : ? >
      • unknown:1s idle 还匹配不上就 fallback ,让 agent 看 screen 自己判断
    2. noni secret --env VAR —— token 不走 RPC wire 。读的是 daemon 进程的环境变量,所以不会出现在 agent 的 prompt / 日志 / context 里。这是协议层的硬保障,不是约定。

    3. 架构是 stateless CLI + 常驻 daemon:

      noni (CLI) ──JSON-RPC over Unix socket──▶ nonid (daemon) ──PTY──▶ child
      

      socket 在 $XDG_RUNTIME_DIR/noni/sock,0600 。daemon 由 CLI 首次调用时自动 spawn 。

    4. 终端模拟用 hinshun/vt10x,detector 看的是"稳定后的虚拟屏幕"而不是原始字节流,避免被光标移动 / 重绘干扰。

    实测

    让 agent 在容器里跑 gh auth login 整个 device flow 是能走通的(用户授权那一步还是要人)。完整一次跑下来 14 个 RPC 调用,包括 3 次 select( host / protocol / method )、1 次 yesno(是否用 git credentials )、最后 enter 等待 device 授权完成。

    安装

    brew install williamwa/tap/noni
    # 或
    go install github.com/williamwa/noni/cmd/noni@latest
    go install github.com/williamwa/noni/cmd/nonid@latest
    

    Linux 主测,macOS 能跑。

    Go 单文件,无运行时依赖。还在 v0.1.0-dev ,欢迎拍砖 / issue / PR:

    👉 https://github.com/williamwa/noni

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   808 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 19:45 · PVG 03:45 · LAX 12:45 · JFK 15:45
    ♥ Do have faith in what you're doing.