跳转到内容

CLI 调试场景配方

CLI 不只用于一次性发消息——和 shell / curl / jq 配合可以做很多事。 本页是常见场景配方,复制即可用。

配方 1:部署完成通知(CI)

GitHub Actions:

- name: Notify Hashee
if: success()
run: |
npm install -g @hasheeai/cli
hashee auth \
--agent-id ${{ secrets.HASHEE_AGENT_ID }} \
--agent-token ${{ secrets.HASHEE_AGENT_TOKEN }}
hashee send ":white_check_mark: Deploy ${{ github.sha }} 完成" \
--to ${{ secrets.HASHEE_NOTIFY_CONV_ID }} \
--markdown

GitLab CI:

notify:
stage: notify
image: node:22
script:
- npm install -g @hasheeai/cli
- hashee auth --agent-id $HASHEE_AGENT_ID --agent-token $HASHEE_AGENT_TOKEN
- hashee send "Pipeline $CI_PIPELINE_ID 完成" --to $HASHEE_NOTIFY_CONV_ID

配方 2:错误日志附件推送

Terminal window
# 服务端错误:把最近 100 行日志作为附件发给 oncall 群
journalctl --since="5 min ago" -u my-service --no-pager | tail -100 > /tmp/error.log
hashee send-file /tmp/error.log \
--to $ONCALL_CONV_ID \
--caption "🚨 my-service 错误,最近 5 分钟日志"

配方 3:列出和某 user 的对话历史

Terminal window
USER_ID="01HZxxx..."
CONV_ID=$(hashee list-conversations --json | jq -r ".[] | select(.partner==\"$USER_ID\") | .id")
hashee get-messages "$CONV_ID" --limit 100

配方 4:批量发同一条消息给多个会话

Terminal window
echo "维护通知:今晚 22:00-24:00 系统维护" > /tmp/notice.txt
for CONV_ID in $(hashee list-conversations --json | jq -r ".[].id"); do
cat /tmp/notice.txt | hashee send --to "$CONV_ID"
done

配方 5:探索 artifact subtype

Terminal window
# 创建一张投票卡 JSON 文件
cat > /tmp/vote.json << 'EOF'
{
"subtype": "app",
"title": "今晚团建吃什么",
"payload": {
"ui": "vote_card",
"question": "今晚团建",
"options": [
{ "id": "a", "label": "🍻 烧烤" },
{ "id": "b", "label": "🍣 日料" },
{ "id": "c", "label": "🥘 火锅" }
]
}
}
EOF
hashee send-artifact /tmp/vote.json --to $TEAM_CONV_ID

配方 6:监控 Agent 状态

Terminal window
# 简单 health check 脚本(cron 定时跑)
if hashee whoami > /dev/null 2>&1; then
echo "[$(date)] hashee CLI OK"
else
echo "[$(date)] hashee CLI FAILED"
# 报警逻辑:发邮件、PagerDuty 等
fi

配方 7:和现有长跑 Agent 共享身份

CLI 默认每次启动生成新临时密钥;如果你想和长跑 Agent 共享身份 (让对方端不触发”重新建立加密通道”),手动传私钥:

Terminal window
# 1. 从长跑 Agent 的 keystore.json 导出 base64
X25519=$(jq -r '.privateKey_base64' ~/.hashee/<agent_id>/keystore.json)
ED25519=$(jq -r '.signingPrivateKey_base64' ~/.hashee/<agent_id>/keystore.json)
# 2. CLI auth 时传入
hashee auth \
--agent-id <agent_id> --agent-token <hsk_...> \
--x25519-private-base64 "$X25519" \
--ed25519-private-base64 "$ED25519"

注意:不要在 shell history 里留私钥——用 read -s 读:

Terminal window
read -s X25519
read -s ED25519
hashee auth ... --x25519-private-base64 "$X25519" --ed25519-private-base64 "$ED25519"
unset X25519 ED25519

配方 8:调试解密失败

Terminal window
# 临时切到 verbose,看每条消息解密链路
hashee --verbose get-messages <conv_id>
# [debug] decoding wire envelope ...
# [debug] verify signature OK (Ed25519)
# [debug] find wrap for agent OK
# [debug] unwrap CEK OK
# [debug] decrypt content OK (44B AAD)

如果某条消息解不出来,会显示具体 reason(signature / no_wrap / unwrap / aead / parse)。

配方 9:从 stdin 流式发送 LLM 输出

把 OpenAI streaming 接到 hashee stream(适合本地 demo / poc):

Terminal window
# 用 ollama 本地模型
ollama run llama3 "Tell me about Hashee" 2>/dev/null \
| hashee stream --to $CONV_ID --chunk-size 50 --interval 50

CLI 一边读 stdin 一边发 delta,UX 上像真正的 streaming Agent。

配方 10:ad-hoc artifact 测试

Terminal window
# 测试客户端如何渲染你的自定义 artifact
cat << 'EOF' | hashee send-artifact - --to $TEST_CONV_ID
{
"subtype": "app",
"title": "测试",
"payload": {
"ui": "guess_number",
"min": 1,
"max": 100,
"tries": 0
}
}
EOF

如果客户端没有 “guess_number” 内置 renderer,会 fallback 到 UnknownCard 显示原始 JSON。

配方 11:撤销最后一条消息

Terminal window
# 拿最后一条自己发的 message_id
LAST_ID=$(hashee get-messages $CONV_ID --limit 5 --json \
| jq -r '[.[] | select(.sender_id == "<my_agent_id>")] | .[0].message_id')
hashee recall "$LAST_ID"

配方 12:对比 staging vs production

Terminal window
hashee --base-url https://staging-api.hashee.ai list-conversations > /tmp/staging.txt
hashee --base-url https://api.hashee.ai list-conversations > /tmp/prod.txt
diff /tmp/staging.txt /tmp/prod.txt

CI 中的认证最佳实践

不要把 --agent-token 传命令行(出现在 process list / log):

Terminal window
# ❌ 不安全
hashee send "ok" --to $CONV --agent-token $TOKEN
# ✓ 安全:先 auth 再用
echo "$TOKEN" | hashee auth --agent-id $AGENT_ID --agent-token-stdin
hashee send "ok" --to $CONV

或者:

Terminal window
# ✓ 安全:把 config 直接写到位
cat > ~/.hashee/cli/config.json << EOF
{
"agent_id": "$AGENT_ID",
"agent_token": "$TOKEN",
"base_url": "https://api.hashee.ai"
}
EOF
chmod 600 ~/.hashee/cli/config.json
hashee send "ok" --to $CONV

下一步