Appearance
通知配置
任务完成或需要确认时,让 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。
- 创建脚本:
~/.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 字符串参数,不要执行不可信脚本。