agenthub/scripts/attach.sh

123 lines
5.0 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env bash
# 从 hub 的任务号进到那个 agent 的「原生 CLI 会话」(workbuddy / claude / codex 通吃)。
#
# ./scripts/attach.sh tsk_8b4337cc… 进会话(本机直接进,跨机器自动走 ssh)
# ./scripts/attach.sh -n tsk_8b4337cc… 只打印要执行的命令,不执行
# HUB_URL=http://10.0.0.5:8100 HUB_KEY=agp_xxx ./scripts/attach.sh tsk_xxx
#
# 为什么要 ssh:会话记录(workbuddy/claude 在 ~/.codebuddy|~/.claude/projects/<cwd>/,
# codex 在 ~/.codex/sessions/)存在 **agent 那台机器** 的磁盘上,hub 只有事件流。
# 而且必须 cd 到那次任务用的 workdir —— 会话按工作目录归档,在别的目录里 -r 会报
# “No conversation found with session ID”。
set -euo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
root="$(cd "${here}/.." && pwd)"
hub="${HUB_URL:-http://127.0.0.1:8100}"
usage() {
cat <<EOF
用法:scripts/attach.sh [-n] <task_id>
-n, --dry-run 只打印命令,不执行(远程机器上路径不一样时先看清再改)
HUB_URL 默认 ${hub}
HUB_KEY 默认读 ${root}/data/hub-credentials.json 的 api_key
(连远端 hub 时本地这把 key 不对,要显式给)
EOF
}
log() { printf '\033[36m[attach]\033[0m %s\n' "$*"; }
die() { printf '\033[31m[attach]\033[0m %s\n' "$*" >&2; exit 1; }
# 单引号安全包住一个参数(拼远端命令串用)
sq() { printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")"; }
dry=0
task_id=""
while [ $# -gt 0 ]; do
case "$1" in
-n | --dry-run) dry=1 ;;
-h | --help) usage; exit 0 ;;
-*) die "未知参数:$1" ;;
*) task_id="$1" ;;
esac
shift
done
[ -n "$task_id" ] || { usage >&2; exit 2; }
command -v jq >/dev/null 2>&1 || die "需要 jq"
api_key="${HUB_KEY:-}"
if [ -z "$api_key" ] && [ -f "${root}/data/hub-credentials.json" ]; then
api_key="$(jq -r '.api_key // empty' "${root}/data/hub-credentials.json")"
fi
[ -n "$api_key" ] || die "没有 hub 的 API Key:用 HUB_KEY 指定,或在 hub 所在机器上跑"
get() { curl -fsS -H "X-API-Key: ${api_key}" "${hub}$1"; }
task="$(get "/api/v1/tasks/${task_id}")" || die "hub(${hub})上没有任务 ${task_id}"
agent_id="$(jq -r '.agent_id // empty' <<<"$task")"
[ -n "$agent_id" ] || die "任务 ${task_id} 还没分配 agent"
agent="$(get "/api/v1/agents/${agent_id}")"
agent_name="$(jq -r '.name' <<<"$agent")"
agent_status="$(jq -r '.status' <<<"$agent")"
cli="$(jq -r '.capabilities.cli // empty' <<<"$agent")"
host="$(jq -r '.capabilities.host // .deployment_ref // empty' <<<"$agent")"
remote_user="$(jq -r '.capabilities.user // empty' <<<"$agent")"
workdir="$(jq -r '.capabilities.workdir // empty' <<<"$agent")"
# /tasks/<id>/events 返回裸数组;session_id 在 init 那条 status 和 completed 里
sid="$(get "/api/v1/tasks/${task_id}/events" |
jq -r 'if type == "object" then (.events // []) else . end
| [.[] | .payload.session_id // empty] | last // empty')"
# capabilities 是心跳带来的自省,可能还没到(agent 刚起);agent 名通常就是 --cli 的值
[ -n "$cli" ] || cli="$agent_name"
[ -n "$sid" ] || die "hub 上没记录到 ${task_id} 的 session_id —— 那次任务大概没跑到 CLI 的 init 事件;
到 agent 机器上 cd ${workdir:-<workdir>} 后用 ${cli} 的 -r / --last 自己挑会话"
[ -n "$workdir" ] || die "agent ${agent_name} 没上报 workdir:桥接进程(deploy/cli-bridge)要重启一次才带上它"
case "$cli" in
workbuddy)
bin="/Applications/WorkBuddy.app/Contents/Resources/app.asar.unpacked/cli/bin/codebuddy"
[ -x "$bin" ] || bin="codebuddy"
inner="${bin} -r '${sid}'"
;;
claude)
bin="claude"
inner="${bin} -r '${sid}'"
;;
codex)
bin="/Applications/ChatGPT.app/Contents/Resources/codex-cli/bin/codex"
[ -x "$bin" ] || bin="codex"
inner="${bin} resume '${sid}'"
;;
*) die "还不会进 '${cli}' 的会话:在这个脚本的 case 里加一条(-r / resume 的写法各不相同)" ;;
esac
me="$(uname -n)"
remote=0
target=""
if [ -n "$host" ] && [ "${host%.local}" != "${me%.local}" ]; then
remote=1
target="$host"
[ -n "$remote_user" ] && [ "$remote_user" != "$(id -un)" ] && target="${remote_user}@${host}"
fi
if [ "$remote" = 0 ] && ! command -v "${bin%% *}" >/dev/null 2>&1; then
die "找不到 ${bin}:在 agent 机器上装好 CLI,或用 -n 先看清命令"
fi
full="cd $(sq "$workdir") && ${inner}"
log "任务 ${task_id}($(jq -r '.status' <<<"$task"))→ agent ${agent_name} / ${cli} @ ${host:-本机}"
log "workdir=${workdir} session=${sid}"
[ "$agent_status" = "online" ] || log "注意:agent 当前 ${agent_status},桥接进程不在线不影响进会话"
if [ "$remote" = 1 ]; then
log "跨机器:ssh 到 ${target}"
log " ssh -t ${target} $(sq "$full")"
else
log " ${full}"
fi
[ "$dry" = 1 ] && exit 0
[ -t 0 ] && [ -t 1 ] || die "当前不是交互终端(加 -n 只看命令)"
if [ "$remote" = 1 ]; then exec ssh -t "$target" "$full"; else exec bash -c "$full"; fi