
Codex Cli Dispatch
- 1 installs
- 12 repo stars
- Updated March 4, 2026
- win4r/codex-cli-dispatch
Helps with ai & agent building tasks.
About
codex-cli-dispatch is a Claude Code skill for ai & agent building. It helps solo builders move faster with AI-assisted development.
- codex-cli-dispatch
- AI & Agent Building
- AI-coding skill
Codex Cli Dispatch by the numbers
- 1 all-time installs (skills.sh)
- Ranked #14,102 of 16,546 AI & Agent Building skills by installs in the Skillselion catalog
- Data as of Jul 27, 2026 (Skillselion catalog sync)
npx skills add https://github.com/win4r/codex-cli-dispatch --skill codex-cli-dispatchAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 1 |
|---|---|
| repo stars | ★ 12 |
| Last updated | March 4, 2026 |
| Repository | win4r/codex-cli-dispatch ↗ |
What it does
Helps with ai & agent building tasks.
Files
Codex CLI Dispatch Skill
Dispatch development tasks to OpenAI's Codex CLI (codex exec) with automatic Telegram notification on completion.
When to Use
- User asks to use Codex / OpenAI Codex to build, fix, or review code
- User explicitly says "use Codex" or "codex cli"
- Tasks that benefit from GPT-5.3-Codex model (OpenAI's coding agent)
Architecture
AGI (main session)
→ exec: dispatch.sh (background)
→ codex exec (headless, sandboxed)
→ on exit: notify via openclaw message send
→ write results to latest.jsonQuick Reference
Dispatch a Task
bash /home/ubuntu/clawd/skills/codex-cli-dispatch/scripts/dispatch.sh \
--prompt "Build a REST API with Express.js" \
--task-name "express-api" \
--workdir "/home/ubuntu/projects/express-api" \
--telegram-group "-5006066016" \
--model "gpt-5.3-codex" \
--sandbox "workspace-write"Parameters
| Flag | Required | Default | Description |
|---|---|---|---|
--prompt | Yes | — | Task instructions for Codex |
--task-name | No | codex-<timestamp> | Human-readable task name |
--workdir | No | /home/ubuntu/clawd | Working directory |
--telegram-group | No | — | Telegram chat ID for notification |
--model | No | (from config.toml) | Model override |
--sandbox | No | workspace-write | read-only, workspace-write, or danger-full-access |
--full-auto | No | true | Skip approvals (sandboxed) |
--image | No | — | Image file(s) to attach |
Dispatch Pattern (from AGI)
⚠️ MUST use `nohup` + background — dispatch.sh blocks until Codex finishes. Short exec timeout will SIGTERM the process.
# 1. Create project dir
mkdir -p /home/ubuntu/projects/my-project
cd /home/ubuntu/projects/my-project && git init
# 2. Dispatch (ALWAYS use nohup + &)
nohup bash /home/ubuntu/clawd/skills/codex-cli-dispatch/scripts/dispatch.sh \
--prompt "..." \
--task-name "my-task" \
--workdir "/home/ubuntu/projects/my-project" \
--telegram-group "-5006066016" \
> /tmp/dispatch-codex.log 2>&1 &Alternative: use exec with background=true so AGI doesn't block.
Result Files
| File | Content |
|---|---|
data/codex-results/latest.json | Last task result (task_name, output, status) |
data/codex-results/task-meta.json | Task metadata (written before run) |
data/codex-results/task-output.txt | Raw Codex output |
⚠️ Critical Gotchas
1. Codex needs a git repo — Use --skip-git-repo-check or git init the workdir first 2. `codex exec` is headless — No PTY wrapper needed (unlike Claude Code), it exits cleanly 3. Sandbox modes matter — workspace-write is safe default; danger-full-access for system tasks only 4. Auth — Codex uses ~/.codex/config.toml credentials. Run codex login if auth fails 5. Model — Default is gpt-5.3-codex; can override with --model o3 etc. 6. Output format — Use --json for JSONL event stream if needed for parsing
Code Review Mode
Codex has a built-in code review:
codex exec review --full-auto -C /path/to/repo⚠️ Critical Gotchas (continued)
7. Don't poll and wait! dispatch.sh is a blocking script. After starting it with exec background=true, do not `process poll`. The script will automatically send a Telegram notification when finished. Polling just wastes agent turns (lesson learned: a security scan task was polled 20+ times) 8. Long tasks may be OOM killed — Codex's gpt-5.3-codex reasoning mode is memory-intensive; long-running scan tasks may be killed by the system (SIGKILL). Consider splitting large tasks or reducing reasoning effort
Debugging
# Check Codex version
codex --version
# Check auth
cat ~/.codex/config.toml
# View last result
cat /home/ubuntu/clawd/data/codex-results/latest.json | jq .MIT License
Copyright (c) 2026
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Codex CLI Dispatch
一个 OpenClaw 技能插件,用于将编码任务分发给 OpenAI Codex CLI(codex exec)后台执行,任务完成后自动发送 Telegram 通知。
无需轮询,无需阻塞。发射即忘 —— 完成时自动通知你。
功能特性
- 🚀 后台执行 — 将 Codex CLI 作为后台进程运行,不阻塞 AI Agent
- 📲 自动 Telegram 通知 — 任务完成时自动发送结果摘要到指定 Telegram 群组
- 🔄 Agent 唤醒钩子 — 完成后通过 webhook 唤醒 OpenClaw Agent 进行后续处理
- 🛡️ 沙盒模式 — 支持
read-only(只读)、workspace-write(工作区写入)、danger-full-access(完全访问) - 📊 结果追踪 — 输出结构化 JSON 结果,方便下游处理
- 🖼️ 图片支持 — 可附加图片到 Codex 提示词,提供视觉上下文
前置要求
- 已安装并运行 OpenClaw
- 已安装 Codex CLI(
npm i -g @openai/codex) - Codex 已完成认证(运行
codex login或配置~/.codex/config.toml) - (可选)OpenClaw 中已配置 Telegram Bot,用于接收通知
安装
作为 OpenClaw 技能安装
将技能文件夹复制到你的 OpenClaw 工作区:
cp -r codex-cli-dispatch /path/to/your/clawd/skills/OpenClaw 会自动从 SKILL.md 发现该技能。
独立使用
你也可以单独使用 scripts/dispatch.sh:
git clone https://github.com/win4r/codex-cli-dispatch.git
cd codex-cli-dispatch
chmod +x scripts/dispatch.sh配置
脚本会自动检测路径,你也可以通过环境变量覆盖:
| 变量 | 默认值 | 说明 |
|---|---|---|
OPENCLAW_WORKSPACE | scripts/ 的上级目录 | OpenClaw 工作区根目录(如 ~/clawd) |
OPENCLAW_BIN | 从 $PATH 自动检测 | openclaw 可执行文件路径 |
CODEX_BIN | 从 $PATH 自动检测 | codex 可执行文件路径 |
OPENCLAW_CONFIG | ~/.openclaw/openclaw.json | OpenClaw 配置文件(用于唤醒钩子) |
示例 — 如果你的工作区在 /opt/openclaw:
export OPENCLAW_WORKSPACE=/opt/openclaw
bash scripts/dispatch.sh --prompt "修复登录 bug" --task-name "login-fix"注意: 结果文件(latest.json、task-meta.json、task-output.txt)会写入$OPENCLAW_WORKSPACE/data/codex-results/目录,请确保该目录可写。
使用方法
基本用法
bash scripts/dispatch.sh \
--prompt "用 Express.js 和 TypeScript 构建一个 REST API" \
--task-name "express-api" \
--workdir "/path/to/project"完整参数示例
bash scripts/dispatch.sh \
--prompt "将认证模块重构为 JWT 方案" \
--task-name "auth-refactor" \
--workdir "/home/user/projects/my-app" \
--telegram-group "-1001234567890" \
--model "gpt-5.3-codex" \
--sandbox "workspace-write" \
--full-auto true \
--image "./screenshot.png"参数说明
| 参数 | 必填 | 默认值 | 说明 |
|---|---|---|---|
--prompt | ✅ | — | Codex 任务指令 |
--task-name | ❌ | codex-<时间戳> | 任务名称(便于识别) |
--workdir | ❌ | 当前目录 | 任务工作目录 |
--telegram-group | ❌ | — | Telegram 群组 ID,用于完成通知 |
--model | ❌ | Codex 配置默认值 | 模型覆盖(如 gpt-5.3-codex、o3) |
--sandbox | ❌ | workspace-write | 沙盒模式:read-only、workspace-write、danger-full-access |
--full-auto | ❌ | true | 跳过交互式确认 |
--image | ❌ | — | 附加图片(可重复使用) |
--skip-git-check | ❌ | false | 跳过 git 仓库初始化 |
--agent-id | ❌ | — | 完成后唤醒的 OpenClaw Agent ID |
在 OpenClaw Agent 中使用
作为 OpenClaw 技能使用时,Agent 这样分发任务:
# 必须使用 nohup + & 后台运行,避免阻塞 Agent
nohup bash /path/to/skills/codex-cli-dispatch/scripts/dispatch.sh \
--prompt "用 React 构建一个 TODO 应用" \
--task-name "todo-app" \
--workdir "/home/user/projects/todo-app" \
--telegram-group "-1001234567890" \
> /tmp/dispatch-codex.log 2>&1 &工作原理
OpenClaw Agent
├── exec: dispatch.sh(后台,非阻塞)
│ ├── 写入任务元数据(task-meta.json)
│ ├── 运行:codex exec --full-auto --sandbox workspace-write "prompt"
│ ├── 捕获输出(task-output.txt)
│ ├── 写入结果(latest.json)
│ ├── 通过 openclaw message send 发送 Telegram 通知
│ └── 通过 /hooks/wake 端点唤醒 Agent
└── (Agent 继续处理其他请求)输出文件
结果写入 data/codex-results/ 目录:
| 文件 | 说明 |
|---|---|
latest.json | 最近一次任务结果(task_name、output、status、exit_code) |
task-meta.json | 任务元数据(prompt、workdir、时间信息) |
task-output.txt | Codex CLI 原始输出 |
latest.json 示例
{
"task_name": "express-api",
"timestamp": "2026-03-04T12:34:56+00:00",
"output": "... Codex 输出的最后 4000 字符 ...",
"telegram_group": "-1001234567890",
"exit_code": 0,
"status": "done"
}注意事项
1. Codex 需要 git 仓库 — 脚本会自动初始化,使用 --skip-git-check 可跳过 2. 不要轮询后台进程 — 脚本会自动发送通知,轮询只会浪费资源 3. 注意沙盒模式 — 默认 workspace-write 最安全,仅在系统级任务时使用 danger-full-access 4. 长任务可能 OOM — Codex 推理模式内存消耗较大,建议拆分大型任务 5. Git 初始化隔离 — 脚本仅在 --workdir 层级创建 git 仓库,不会嵌套
许可证
MIT
相关项目
Codex CLI Dispatch
An OpenClaw skill that dispatches coding tasks to OpenAI Codex CLI (codex exec) in background mode, with automatic Telegram notification on completion.
No polling. No blocking. Fire and forget — get notified when it's done.
Features
- 🚀 Background execution — dispatches Codex CLI as a background process, never blocks your agent
- 📲 Auto Telegram notification — sends task result summary to a Telegram chat when done
- 🔄 Agent wake hook — wakes your OpenClaw agent on completion for follow-up actions
- 🛡️ Sandbox modes — supports
read-only,workspace-write, anddanger-full-access - 📊 Result tracking — writes structured JSON results for downstream processing
- 🖼️ Image support — attach images to the Codex prompt for visual context
Prerequisites
- OpenClaw installed and running
- Codex CLI installed (
npm i -g @openai/codex) - Codex authenticated (
codex loginor~/.codex/config.toml) - (Optional) Telegram bot configured in OpenClaw for notifications
Installation
As an OpenClaw Skill
Copy the skill folder into your OpenClaw workspace:
cp -r codex-cli-dispatch /path/to/your/clawd/skills/OpenClaw will auto-discover the skill from SKILL.md.
Standalone
You can also use scripts/dispatch.sh independently:
git clone https://github.com/win4r/codex-cli-dispatch.git
cd codex-cli-dispatch
chmod +x scripts/dispatch.shConfiguration
The script auto-detects paths, but you can override them via environment variables:
| Variable | Default | Description |
|---|---|---|
OPENCLAW_WORKSPACE | Parent of scripts/ dir | OpenClaw workspace root (e.g., ~/clawd) |
OPENCLAW_BIN | Auto-detect via $PATH | Path to openclaw binary |
CODEX_BIN | Auto-detect via $PATH | Path to codex binary |
OPENCLAW_CONFIG | ~/.openclaw/openclaw.json | OpenClaw config file (for wake hooks) |
Example — if your workspace is at /opt/openclaw:
export OPENCLAW_WORKSPACE=/opt/openclaw
bash scripts/dispatch.sh --prompt "Fix the login bug" --task-name "login-fix"Note: Result files (latest.json,task-meta.json,task-output.txt) are written to$OPENCLAW_WORKSPACE/data/codex-results/. Make sure this directory is writable.
Usage
Basic Dispatch
bash scripts/dispatch.sh \
--prompt "Build a REST API with Express.js and TypeScript" \
--task-name "express-api" \
--workdir "/path/to/project"Full Options
bash scripts/dispatch.sh \
--prompt "Refactor the auth module to use JWT" \
--task-name "auth-refactor" \
--workdir "/home/user/projects/my-app" \
--telegram-group "-1001234567890" \
--model "gpt-5.3-codex" \
--sandbox "workspace-write" \
--full-auto true \
--image "./screenshot.png"Parameters
| Flag | Required | Default | Description |
|---|---|---|---|
--prompt | ✅ | — | Task instructions for Codex |
--task-name | ❌ | codex-<timestamp> | Human-readable task name |
--workdir | ❌ | Current directory | Working directory for the task |
--telegram-group | ❌ | — | Telegram chat ID for completion notification |
--model | ❌ | From Codex config | Model override (e.g., gpt-5.3-codex, o3) |
--sandbox | ❌ | workspace-write | Sandbox mode: read-only, workspace-write, danger-full-access |
--full-auto | ❌ | true | Skip interactive approvals |
--image | ❌ | — | Attach image(s) to the prompt (repeatable) |
--skip-git-check | ❌ | false | Skip git repo initialization |
--agent-id | ❌ | — | OpenClaw agent ID to wake on completion |
From an OpenClaw Agent
When used as an OpenClaw skill, the agent dispatches tasks like this:
# Always use nohup + & to avoid blocking the agent
nohup bash /path/to/skills/codex-cli-dispatch/scripts/dispatch.sh \
--prompt "Build a TODO app with React" \
--task-name "todo-app" \
--workdir "/home/user/projects/todo-app" \
--telegram-group "-1001234567890" \
> /tmp/dispatch-codex.log 2>&1 &How It Works
OpenClaw Agent
├── exec: dispatch.sh (background, non-blocking)
│ ├── writes task metadata (task-meta.json)
│ ├── runs: codex exec --full-auto --sandbox workspace-write "prompt"
│ ├── captures output (task-output.txt)
│ ├── writes result (latest.json)
│ ├── sends Telegram notification via openclaw message send
│ └── wakes agent via /hooks/wake endpoint
└── (agent continues handling other requests)Output Files
Results are written to data/codex-results/:
| File | Description |
|---|---|
latest.json | Last task result (task_name, output, status, exit_code) |
task-meta.json | Task metadata (prompt, workdir, timing) |
task-output.txt | Raw Codex CLI output |
latest.json Example
{
"task_name": "express-api",
"timestamp": "2026-03-04T12:34:56+00:00",
"output": "... last 4000 chars of codex output ...",
"telegram_group": "-1001234567890",
"exit_code": 0,
"status": "done"
}Tips & Gotchas
1. Codex needs a git repo — The script auto-initializes one if missing. Use --skip-git-check to skip. 2. Don't poll the background process — The script sends notifications automatically. Polling wastes resources. 3. Sandbox matters — Use workspace-write (default) for safety. Only use danger-full-access for system-level tasks. 4. Long tasks may OOM — Codex's reasoning mode is memory-intensive. Consider splitting large tasks. 5. Git init isolation — The script creates git repos at --workdir level only, never nested.
License
MIT
Related
#!/bin/bash
# Codex CLI Dispatch Script
# Runs codex exec in headless mode with automatic Telegram notification on completion.
#
# Usage:
# bash dispatch.sh --prompt "Build X" --task-name "my-task" --workdir "/path" --telegram-group "-123"
set -euo pipefail
# ---- Resolve paths (portable) ----
# OPENCLAW_WORKSPACE: root of your OpenClaw workspace (e.g. ~/clawd)
OPENCLAW_WORKSPACE="${OPENCLAW_WORKSPACE:-$(cd "$(dirname "$0")/.." 2>/dev/null && pwd)}"
RESULT_DIR="${OPENCLAW_WORKSPACE}/data/codex-results"
META_FILE="${RESULT_DIR}/task-meta.json"
TASK_OUTPUT="${RESULT_DIR}/task-output.txt"
OPENCLAW_BIN="${OPENCLAW_BIN:-$(command -v openclaw 2>/dev/null || echo "openclaw")}"
CODEX_BIN="${CODEX_BIN:-$(command -v codex 2>/dev/null || echo "codex")}"
# Defaults
PROMPT=""
TASK_NAME="codex-$(date +%s)"
TELEGRAM_GROUP=""
WORKDIR="${OPENCLAW_WORKSPACE}"
MODEL=""
SANDBOX="workspace-write"
FULL_AUTO="true"
IMAGES=()
SKIP_GIT_CHECK="false"
AGENT_ID=""
# ---- Parse args ----
while [[ $# -gt 0 ]]; do
case "$1" in
--prompt) PROMPT="$2"; shift 2 ;;
--task-name) TASK_NAME="$2"; shift 2 ;;
--workdir) WORKDIR="$2"; shift 2 ;;
--telegram-group) TELEGRAM_GROUP="$2"; shift 2 ;;
--model) MODEL="$2"; shift 2 ;;
--sandbox) SANDBOX="$2"; shift 2 ;;
--full-auto) FULL_AUTO="$2"; shift 2 ;;
--image) IMAGES+=("$2"); shift 2 ;;
--skip-git-check) SKIP_GIT_CHECK="true"; shift ;;
--agent-id) AGENT_ID="$2"; shift 2 ;;
*) echo "Unknown arg: $1"; exit 1 ;;
esac
done
if [ -z "$PROMPT" ]; then
echo "Error: --prompt is required"
exit 1
fi
# ---- 1. Write task metadata ----
mkdir -p "$RESULT_DIR"
jq -n \
--arg name "$TASK_NAME" \
--arg group "$TELEGRAM_GROUP" \
--arg prompt "$PROMPT" \
--arg workdir "$WORKDIR" \
--arg model "$MODEL" \
--arg sandbox "$SANDBOX" \
--arg agent_id "$AGENT_ID" \
--arg ts "$(date -Iseconds)" \
'{task_name: $name, telegram_group: $group, prompt: $prompt, workdir: $workdir, model: $model, sandbox: $sandbox, agent_id: $agent_id, started_at: $ts, status: "running"}' \
> "$META_FILE"
echo "[dispatch] Task: $TASK_NAME"
echo "[dispatch] Workdir: $WORKDIR"
echo "[dispatch] Model: ${MODEL:-default}"
echo "[dispatch] Sandbox: $SANDBOX"
# ---- 2. Ensure workdir has git ----
mkdir -p "$WORKDIR"
if [ ! -d "$WORKDIR/.git" ] && [ "$SKIP_GIT_CHECK" != "true" ]; then
echo "[dispatch] Initializing git in $WORKDIR"
cd "$WORKDIR" && git init -q
fi
# ---- 3. Build codex command ----
CMD=("$CODEX_BIN" "exec")
if [ "$FULL_AUTO" = "true" ]; then
CMD+=("--full-auto")
fi
CMD+=("--sandbox" "$SANDBOX")
CMD+=("-C" "$WORKDIR")
if [ -n "$MODEL" ]; then
CMD+=("--model" "$MODEL")
fi
for img in "${IMAGES[@]}"; do
CMD+=("--image" "$img")
done
CMD+=("$PROMPT")
echo "[dispatch] Running: ${CMD[*]}"
# ---- 4. Run Codex ----
"${CMD[@]}" 2>&1 | tee "$TASK_OUTPUT"
EXIT_CODE=${PIPESTATUS[0]}
echo "[dispatch] Codex exited with code: $EXIT_CODE"
# ---- 5. Update metadata ----
COMPLETED_AT="$(date -Iseconds)"
jq --arg status "$([ "$EXIT_CODE" -eq 0 ] && echo 'done' || echo 'failed')" \
--arg exit_code "$EXIT_CODE" \
--arg completed "$COMPLETED_AT" \
'. + {status: $status, exit_code: ($exit_code|tonumber), completed_at: $completed}' \
"$META_FILE" > "${META_FILE}.tmp" && mv "${META_FILE}.tmp" "$META_FILE"
# ---- 6. Write latest.json ----
OUTPUT=$(tail -c 4000 "$TASK_OUTPUT" 2>/dev/null || echo "no output")
jq -n \
--arg task "$TASK_NAME" \
--arg ts "$COMPLETED_AT" \
--arg output "$OUTPUT" \
--arg group "$TELEGRAM_GROUP" \
--arg exit_code "$EXIT_CODE" \
'{task_name: $task, timestamp: $ts, output: $output, telegram_group: $group, exit_code: ($exit_code|tonumber), status: (if ($exit_code|tonumber) == 0 then "done" else "failed" end)}' \
> "${RESULT_DIR}/latest.json"
# ---- 7. Send Telegram notification ----
if [ -n "$TELEGRAM_GROUP" ] && [ -x "$OPENCLAW_BIN" ]; then
STATUS_EMOJI="✅"
[ "$EXIT_CODE" != "0" ] && STATUS_EMOJI="❌"
# Calculate duration
DURATION=""
STARTED=$(jq -r '.started_at // ""' "$META_FILE" 2>/dev/null || echo "")
if [ -n "$STARTED" ]; then
START_TS=$(date -d "$STARTED" +%s 2>/dev/null || echo 0)
END_TS=$(date +%s)
if [ "$START_TS" -gt 0 ]; then
ELAPSED=$(( END_TS - START_TS ))
MINS=$(( ELAPSED / 60 ))
SECS=$(( ELAPSED % 60 ))
DURATION="${MINS}m${SECS}s"
fi
fi
# Extract last meaningful output (skip token counts etc)
SUMMARY=$(tail -20 "$TASK_OUTPUT" | head -c 1000)
MSG="${STATUS_EMOJI} *Codex CLI 任务完成*
📋 *任务:* \`${TASK_NAME}\`
📂 *路径:* \`${WORKDIR}\`"
[ -n "$DURATION" ] && MSG="${MSG}
⏱ *耗时:* ${DURATION}"
[ "$EXIT_CODE" != "0" ] && MSG="${MSG}
⚠️ *Exit Code:* ${EXIT_CODE}"
MSG="${MSG}
📝 *输出摘要:*
\`\`\`
${SUMMARY}
\`\`\`"
"$OPENCLAW_BIN" message send \
--channel telegram \
--target "$TELEGRAM_GROUP" \
--message "$MSG" 2>/dev/null && echo "[dispatch] Telegram notification sent" || echo "[dispatch] Telegram send failed"
fi
# ---- Wake AGI or target agent ----
GATEWAY_PORT="${OPENCLAW_GATEWAY_PORT:-18789}"
HOOK_TOKEN=""
OPENCLAW_CONFIG="${OPENCLAW_CONFIG:-${HOME}/.openclaw/openclaw.json}"
if [ -f "$OPENCLAW_CONFIG" ]; then
HOOK_TOKEN=$(jq -r '.hooks.token // ""' "$OPENCLAW_CONFIG" 2>/dev/null || echo "")
fi
if [ -n "$HOOK_TOKEN" ]; then
WAKE_TEXT="[CODEX_CLI_DONE] task=${TASK_NAME} status=$([ "$EXIT_CODE" -eq 0 ] && echo done || echo failed) group=${TELEGRAM_GROUP:-none} ts=$(date -Iseconds)"
if [ -n "$AGENT_ID" ] && [ "$AGENT_ID" != "main" ]; then
# Wake specific agent
AGENT_PAYLOAD=$(jq -n \
--arg message "$WAKE_TEXT" \
--arg agentId "$AGENT_ID" \
--arg channel "telegram" \
--arg to "${TELEGRAM_GROUP}" \
'{message: $message, agentId: $agentId, wakeMode: "now", deliver: true, channel: $channel, to: $to}')
curl -s -o /dev/null -X POST \
"http://localhost:${GATEWAY_PORT}/hooks/agent" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${HOOK_TOKEN}" \
-d "$AGENT_PAYLOAD" 2>/dev/null && echo "[dispatch] Agent wake sent (agentId=$AGENT_ID)" || echo "[dispatch] Agent wake failed"
else
# Wake main session
curl -s -o /dev/null -X POST \
"http://localhost:${GATEWAY_PORT}/hooks/wake" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${HOOK_TOKEN}" \
-d "{\"text\":\"${WAKE_TEXT}\",\"mode\":\"now\"}" 2>/dev/null && echo "[dispatch] Wake sent to main" || echo "[dispatch] Wake failed"
fi
else
echo "[dispatch] No hook token, skipping wake"
fi
echo "[dispatch] Done."