从入门到精通

CLI 命令行

哪些场景用 CLI、从安装和连接跑第一条命令,到退出码和安全用法。

一、grape CLI 是什么

grape 是 Grape 的官方命令行工具。它做两件事:

  1. 调 Grape 平台 API
  2. 在本机执行原生 git,并自动注入平台凭据

它既适合人手工用,也适合脚本和 Agent:

  • 人看:默认输出多行文本
  • 脚本/Agent 看:加 --json-out 输出单行 JSON
  • 判断成败:看退出码,不用解析报错文字

CLI 的命令来自服务端契约,不是每个接口单独手写。服务端新增接口后,grape refresh 就能拿到新命令。

二、哪些场景会用到

CLI 不是日常必须在用的东西。用得多的是下面几类场景:

场景 使用频率 谁在用 典型操作
Agent 执行任务 最高 平台 Agent / 技能 读 Issue、更新状态、写评论、建 PR、交接
本地排障 高 开发、运维 doctor、看当前任务、查事件、看日志
脚本 / CI 中 自动化脚本、流水线 --json-out、批量查询、等待任务结束
Git 操作 中 开发者 grape git status/log/push,凭据由 CLI 注入
技能开发 / 上传 中 技能开发者 查技能、看分类、绑定 Agent
平台管理 低 管理员 设置、凭据、引擎;多数在 Web 控制台完成

用得最多的是 Agent 执行任务:很多技能本身就是 CLI 版,Agent 通过 grape 读写平台对象,而不是自己拼 API 请求。

什么时候不用 CLI

任务 更推荐
浏览需求、看流水线、点合并 Web 控制台
一次性改配置、看图表 Web 控制台
需要人工确认的危险操作 Web 控制台或 Issue 评论
临时看一个状态 Web 控制台

CLI 最适合:重复操作、脚本、Agent、终端里的批量查询。

最常用的几条命令

grape doctor                 # 先确认连接和 token
grape tasks current --json-out
grape skills list --json-out
grape git status
grape commands               # 看当前部署有哪些命令

三、安装

用 PyPI 安装(推荐)

pip install grape-cli
grape --version

升级:

pip install -U grape-cli

源码或内网环境

pip install "git+https://gitcode.com/grape-dev/grape-cli.git@main"

开发调试:

git clone https://gitcode.com/grape-dev/grape-cli.git
cd grape-cli
pip install -e .

完成标志:grape --version 能输出版本号。

四、连接服务端

CLI 需要两个值:

  • 服务端地址
  • token

三种来源,优先级从高到低

来源 示例 适合
一次性参数 --base-url / --token 临时调试
环境变量 GRAPE_BASE_URL、GRAPE_SESSION_TOKEN、GRAPE_API_TOKEN CI、脚本、Agent
配置文件 ~/.grape/config.json 本机长期默认

地址查找顺序:

--base-url
  > GRAPE_BASE_URL
    > GRAPE_SERVER_URL
      > ~/.grape/config.json

token 有两种:

token 用途 生命周期
tk_ 任务/会话内使用 任务结束、超时或失败后失效
grape_ 个人长期 API Key 手动吊销前有效

不要把 token 写进命令行参数。--token 会留在 shell history 和进程列表里,自动化场景用环境变量或配置文件。

推荐的环境变量写法

export GRAPE_BASE_URL=https://grape.example.com
export GRAPE_SESSION_TOKEN=tk_xxx

然后验证:

grape doctor

grape doctor 会检查:

  • token 通道
  • base_url
  • 代理变量
  • 契约缓存
  • CLI 版本

每项给 OK / WARN / FAIL,适合第一次连不上时先跑。

五、跑第一条命令

先看平台上有哪些技能:

grape skills list --json-out

再看当前任务:

grape tasks current --json-out

最后试一次原生 git:

grape git status
grape git log --oneline -5

grape git 的凭据由进程内配置注入,不会写进 .git/config,也不会出现在命令行。

六、常用命令组

命令集合由所连服务端决定,不同部署可能略有差异。查看当前部署的完整清单:

grape commands
grape <group> --help

常用的组:

组 用途
skills 技能列表、上传、分类、Agent 绑定
tasks / task 当前任务、任务详情、等待、日志
issues Issue 卡片、状态、待输入/待评审
scenes 场景模板、节点、应用
runtime Worker、引擎、凭据
repos 仓库托管、收藏、默认场景、平台凭据
git-repo 分支、提交、文件、Issue/PR、标签
events 事件列表、详情、重派、终止
settings 平台设置、凭据、引擎、事件配置
obs 可观测性、告警、日志源、探活
dashboard 效率与运维看板
a2a / takeover Agent 交接、接管会话
knowledge / org / plugins 知识库、组织、插件市场
chat 对话、会话、消息
git 原生 git 透传

部分组按凭据类型分组可见:用任务级 tk_ 时,用户态组(如 chat)会被隐藏,需要 GRAPE_CLI_MODE=USER/ALL 或配置 GRAPE_API_TOKEN 才看得到。

不要背命令;先 grape commands,再 grape <group> --help。

七、写操作与长请求体

写命令的请求体有三种写法。以 Issue 元数据更新为例(issues 组没有 update 动词,改 Issue 元数据走 git-repo issues update):

# 推荐:从文件读,适合长文本
grape git-repo issues update --owner o --repo r --number 7 --json-file body.json

# curl 风格
grape git-repo issues update --owner o --repo r --number 7 --json @body.json

# 从 stdin 读
grape git-repo issues update --owner o --repo r --number 7 --json - < body.json
  • --json-file 推荐用于多行、长文本和特殊字符
  • --json -、--json-file -、--json @- 都从 stdin 读
  • --json 与 --json-file 互斥,同用会直接报错

写操作(POST/PUT/PATCH/DELETE)可以先加 --dry-run:

grape git-repo issues update --owner o --repo r --number 7 --dry-run

它只打印脱敏后的 method、path、query 和 body,不发请求,不产生副作用。

八、给脚本和 Agent 用

需求 用法
机器可读 --json-out,单行 JSON
判断成功失败 看退出码,失败非 0
等任务结束 grape tasks wait --task-id <id> --timeout 120 --interval 3
只看当前 token 能用的命令 GRAPE_CLI_MODE=WORKER
不暴露 git 凭据 用 grape git,不要自己拼 token

九、退出码

退出码 含义
0 成功
1 通用/配置错误:地址、token、网络
2 用法错误:参数缺漏、未知子命令
>=400 HTTP 状态透传,如 401 token 失效、403 越权
130 Ctrl+C 中断

脚本里优先判断退出码,不要匹配报错文本。

十、安全约定

  • CLI 提供 settings key create / settings key delete --key_id <id> 管理用户级 API Key(明文只在创建时返回一次,吊销为立即生效的软删);这两条属 user_only 接口,需要 grape_ 或 Web 登录态,tk_ 会话返回 403,GRAPE_CLI_MODE=WORKER 下默认不列出(grape commands --all 仍会列出并标 [user_only])。tk_ 会话 token 由平台在任务执行期签发,CLI 不创建也不吊销。
  • token 不出现在输出和错误信息里
  • grape git 注入的凭据不落盘、不进 .git/config
  • 不要用 --token 跑自动化;改用环境变量或 ~/.grape/config.json
  • Agent 通常使用 tk_;需要长期管理操作时用 grape_,不要混用

十一、常见问题

grape doctor 有 FAIL

先修 FAIL 项。常见是:

  • base_url 不是浏览器/本机可达地址
  • token 过期或类型不对
  • 代理变量干扰,加 --no-proxy
  • 契约缓存旧,跑 grape refresh

401 / token 失效

tk_ 会话 token 会随任务结束失效。重新取会话 token,或改用 grape_。

连不上、超时

先跑 grape doctor 看地址和 token 通道;自签证书环境确认 scheme + host 实际可达。

命令列表和文档对不上

先 grape refresh;仍不一致说明所连服务端契约版本不同。grape commands 是当前部署的权威清单。

想临时换地址

一次性参数优先于环境变量和配置文件:

grape --base-url https://other.example.com --timeout 300 tasks current --json-out

十二、完成标准

  • [ ] grape --version 有输出
  • [ ] grape doctor 全部 OK
  • [ ] 能列出技能或当前任务
  • [ ] 能用 grape git status 操作仓库
  • [ ] 知道 --json-out 和退出码的用途
  • [ ] 不把 token 写进命令行参数

相关

运行时与 Worker | Agent 详解 | 账号开通与登录 | Chat Agent 与 IM 渠道