Appearance
通知配置
CodexZH 官方教程的思路是:当任务完成或需要你确认时,让 Codex 主动发通知,这样你不用一直盯着终端等。
1. 方式一:TUI 内置通知(新手推荐)
该方式不依赖外部程序,直接在 ~/.codex/config.toml 开启即可。
1.1 开启所有通知
toml
# ~/.codex/config.toml
[tui]
notifications = true1.2 按类型过滤(只接收你关心的)
toml
[tui]
notifications = ["agent-turn-complete", "approval-requested"]官方教程给出的类型说明:
agent-turn-complete:任务完成approval-requested:需要你确认
终端兼容性(官方教程信息):
- 支持:iTerm2、Ghostty、WezTerm
- 不支持:macOS Terminal.app、VS Code 终端
2. 方式二:外部程序通知(可高度定制)
你可以写一个脚本接收事件 JSON,然后做桌面通知、发 Slack/Discord、写日志、触发 Webhook 等。
2.1 事件数据(官方教程示例)
json
{
"type": "agent-turn-complete",
"turn-id": "12345",
"input-messages": ["重构 foo 函数"],
"last-assistant-message": "重构完成,已验证构建通过"
}说明:你的脚本会以“命令行参数”的形式收到这个 JSON 字符串。
2.2 macOS 示例:最简单的 Python 对话框通知
- 新建脚本:
~/.codex/notify.py
python
#!/usr/bin/env python3
import json
import subprocess
import sys
def _safe(s):
# 给 osascript 做最基本的转义,避免引号/反斜杠导致脚本报错
return str(s).replace("\\\\", "\\\\\\\\").replace('"', '\\"')
def main():
if len(sys.argv) != 2:
return 0
try:
event = json.loads(sys.argv[1])
except Exception:
return 0
event_type = event.get("type", "")
if event_type not in ("agent-turn-complete", "approval-requested"):
return 0
title = "Codex 通知"
if event_type == "approval-requested":
title = "Codex 需要确认"
message = event.get("last-assistant-message") or ""
if not message:
# 兼容不同字段名(官方示例里出现过 input-messages)
inputs = event.get("input_messages") or event.get("input-messages") or []
if isinstance(inputs, list):
message = " ".join([str(x) for x in inputs if x])
title = _safe(title)
message = _safe(message or "(无内容)")
subprocess.run(
[
"osascript",
"-e",
(
'display dialog "%s" buttons {"确定"} default button 1 '
'with title "%s" giving up after 30'
)
% (message, title),
],
check=False,
)
return 0
if __name__ == "__main__":
sys.exit(main())- 赋予执行权限:
bash
chmod +x ~/.codex/notify.py- 在
~/.codex/config.toml中配置(按你的实际用户名修改路径):
toml
notify = ["python3", "/Users/你的用户名/.codex/notify.py"]官方教程强调的优点:不依赖通知权限,对话框 30 秒自动关闭,避免阻塞。
2.3 Linux 示例:notify-send
依赖:notify-send(Ubuntu/Debian 常见)以及 jq(用于解析 JSON)。
- 创建脚本:
~/.codex/notify.sh
bash
#!/bin/bash
EVENT_JSON="$1"
TYPE=$(echo "$EVENT_JSON" | jq -r '.type // empty')
MESSAGE=$(echo "$EVENT_JSON" | jq -r '."last-assistant-message" // empty')
if [ "$TYPE" = "agent-turn-complete" ]; then
notify-send "Codex 任务完成" "$MESSAGE" -i dialog-information
fi- 赋予执行权限并配置:
bash
chmod +x ~/.codex/notify.shtoml
notify = ["/home/你的用户名/.codex/notify.sh"]3. 快速测试
官方教程给的测试方法:
bash
codex exec "echo hello"4. 重要说明(官方教程要点)
notify与tui.notifications可以同时启用,互不冲突。- 外部脚本会接收一个 JSON 字符串参数,请注意安全,不要执行不可信脚本。