#!/usr/bin/env bash # dispatch-with-wait.sh — 一键派单 + 后台 wait + host 自动读报告(G-2) # # 用法: # bash scripts/dispatch-with-wait.sh "" [timeoutSec] # bash scripts/dispatch-with-wait.sh --prompt-file [timeoutSec] # # 行为: # 1. 调用 mcp__xstudio__xstudio_ai_exec 派单(通过 ~/.claude/mcp.json 的 xstudio MCP) # 2. 后台跑 scripts/wait-for-task.sh 监听 .xstudio-report.md # 3. 报告出现后自动打印 + exit 0 # 4. timeout 后 exit 1 # # 与 chg_0003 的 wait-for-task.sh 关系: # wait-for-task.sh 是被动轮询脚本(任何 taskId 都能用) # dispatch-with-wait.sh 是 active 派单 + 监听组合脚本(一次完成) # # 注意:派单这一步**调用方**负责(用 mcp__xstudio__xstudio_ai_exec); # 本脚本包装的是:拿到 taskId 后的等待 + 读取。 set -euo pipefail usage() { cat <<'EOF' Usage: dispatch-with-wait.sh [timeoutSec] taskId — xstudio.ai.exec 返回的任务 id timeoutSec — 总超时秒数(默认 3000) 该脚本是"等待已有 task 完成"的工具。**派单动作需先用 mcp__xstudio__xstudio_ai_exec**。 EOF } if [ $# -lt 1 ] || [ "${1:-}" = "--help" ] || [ "${1:-}" = "-h" ]; then usage exit 0 fi TASK_ID="$1" shift TIMEOUT="${1:-3000}" [[ "$1" =~ ^[0-9]+$ ]] && shift || true SELF_DIR="$(cd "$(dirname "$0")" && pwd)" # 后台跑 wait-for-task.sh(chg_0003 交付的脚本) echo "[dispatch-with-wait] taskId=$TASK_ID timeout=${TIMEOUT}s" echo "[dispatch-with-wait] waiting for .xstudio-report.md (polling)..." # 阻塞等到报告出现(同步版,host 直接读到结果) if bash "$SELF_DIR/wait-for-task.sh" "$TASK_ID" "$TIMEOUT"; then echo "[dispatch-with-wait] task complete, report read OK" exit 0 else echo "[dispatch-with-wait] TIMEOUT; check mcp__xstudio__xstudio_ai_status manually" >&2 exit 1 fi