自媒体自动化的常见失败模式,是把“生成”当成了“完成”。一篇草稿写出来,封面没配、平台格式不对、标题在某个渠道被截断,整条链路仍然需要人工补救。真正的工程化自动化,需要把选题、生成、适配、门禁、发布串成一条状态可追踪、失败可定位、产物可回滚的流水线。Codex 的 exec 非交互模式恰好提供了这样一个执行基底:它不试图在一个提示词里完成所有事,而是允许你把每个阶段拆成独立的、输出结构化的命令调用,阶段之间用文件传递状态。
一个值得自动化的自媒体内容流水线,最少包含五个阶段:素材摄入(RSS、选题池、热点来源)、内容生成(长文或脚本)、多平台适配(同一内容在不同渠道的长度、格式、标题约束)、门禁(格式、事实、平台合规)、发布(写入草稿箱或调用平台 API)。n8n 的社区模板已经验证了“RSS → LLM 初稿 → 写库 → 通知”这条最小链路的可行性。但 n8n 的局限在于,它的节点逻辑一旦复杂起来,调试成本会迅速上升,尤其是涉及文件系统操作和自定义校验时。
用 Codex exec 来编排,核心思路是每个阶段一个独立的 codex exec 调用,输出 JSON 或文件,由外部脚本决定下一步。codex exec 支持 --output-schema 强制结构化输出,也支持 --json 把执行过程输出为 JSONL 事件流,这意味着阶段之间的交接不需要解析自由文本。
下面的 pipeline.py 用 Python 的 subprocess 驱动 codex exec,实现“选题 → 初稿 → 平台适配”三段式流水线。每个阶段的产物落到独立目录,后一阶段只读取前一阶段已经通过基本检查的文件。
# pipeline.py
import json, subprocess, pathlib, sys
ROOT = pathlib.Path("./media_workspace")
(ROOT / "01_topic").mkdir(parents=True, exist_ok=True)
(ROOT / "02_draft").mkdir(parents=True, exist_ok=True)
(ROOT / "03_adapted").mkdir(parents=True, exist_ok=True)
SCHEMA_TOPIC = {
"type": "object",
"properties": {
"title": {"type": "string"},
"angle": {"type": "string"},
"platforms": {"type": "array", "items": {"type": "string"}},
"risk_notes": {"type": "array", "items": {"type": "string"}},
},
"required": ["title", "angle", "platforms"],
}
def codex_exec(prompt: str, schema: dict, cwd: pathlib.Path) -> dict:
schema_file = cwd / "schema.json"
schema_file.write_text(json.dumps(schema, ensure_ascii=False), encoding="utf-8")
out_file = cwd / "out.json"
cmd = [
"codex", "exec",
"--sandbox", "workspace-write",
"--output-schema", str(schema_file),
"-o", str(out_file),
prompt,
]
subprocess.run(cmd, check=True, cwd=cwd)
return json.loads(out_file.read_text(encoding="utf-8"))
def stage_topic(raw_input: str) -> dict:
prompt = f"""你是自媒体选题编辑。根据以下素材生成一个选题决策:
素材:{raw_input}
输出必须匹配给定 schema。platforms 从 wechat、zhihu、x 中选。"""
return codex_exec(prompt, SCHEMA_TOPIC, ROOT / "01_topic")
def stage_draft(topic: dict) -> pathlib.Path:
prompt = f"""根据以下选题写一篇 1200 字左右的技术文章草稿,Markdown 格式:
标题:{topic['title']}
角度:{topic['angle']}
写入当前目录 draft.md。不要输出解释,只写文件。"""
subprocess.run(
["codex", "exec", "--sandbox", "workspace-write", prompt],
check=True, cwd=ROOT / "02_draft",
)
return ROOT / "02_draft" / "draft.md"
if __name__ == "__main__":
raw = sys.argv[1] if len(sys.argv) > 1 else "Codex exec 在内容流水线中的应用"
topic = stage_topic(raw)
print("topic:", topic["title"], topic["platforms"])
draft_path = stage_draft(topic)
print("draft:", draft_path)这段代码的关键设计是 --output-schema + -o 的组合:选题阶段输出严格 JSON,后续阶段可以安全地通过 topic['title'] 取值,而不需要正则解析模型回复。-o 把最终消息单独落盘,--json 如果同时加上,还能拿到执行过程的事件流用于日志和审计。
同一篇内容发到公众号、知乎和 X Article,差异不在字数,而在结构约束。公众号段落短、重点前置;知乎需要问题意识;X Article 有更严格的长度上限和代码块呈现要求。把适配阶段也做成一个受 schema 约束的 codex exec 调用,输出每个平台的“适配计划”,再由后续脚本执行实际改写:
# adapt.py
import json, subprocess, pathlib
SCHEMA_ADAPT = {
"type": "object",
"properties": {
"platforms": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"max_chars": {"type": "integer"},
"required_elements": {"type": "array", "items": {"type": "string"}},
"removals": {"type": "array", "items": {"type": "string"}},
},
"required": ["name", "max_chars", "required_elements"],
},
}
},
"required": ["platforms"],
}
draft = pathlib.Path("./media_workspace/02_draft/draft.md").read_text(encoding="utf-8")[:3000]
prompt = f"""分析以下草稿,为 wechat、zhihu、x 三个平台输出适配计划。
草稿:{draft}"""
result = subprocess.run(
["codex", "exec", "--sandbox", "read-only",
"--output-schema", "adapt_schema.json", "-o", "adapt_plan.json", prompt],
capture_output=True, text=True, check=True,
)
plan = json.loads(pathlib.Path("adapt_plan.json").read_text())
print(json.dumps(plan, ensure_ascii=False, indent=2))这里使用 read-only 沙箱是刻意的:适配阶段只需要分析草稿,不需要写入权限。把每个阶段的沙箱权限收紧到刚好够用,是自动化流水线不失控的基本纪律。
视频自动化项目 CreatorFlow 的做法提供了一个重要参照:它的流水线是“选题 → 评分 → 脚本 → 素材 → visual plan → 组装 → 渲染 → QA → 发布包”,QA 在渲染和发布之间,而不是在发布之后。更关键的是,脚本或成片一旦变更,旧评分卡会主动失效,必须重新验收。这意味着门禁不是一个可选的“检查步骤”,而是流水线状态机的一部分。
一个轻量的门禁函数可以这样写,它在每个阶段出口处运行,失败则阻断后续阶段:
# gate.py
import re, hashlib, pathlib
def content_gate(text: str, platform: str) -> dict:
checks = {
"min_len": len(text) >= {"wechat": 800, "zhihu": 600, "x": 300}.get(platform, 300),
"has_structure": bool(re.search(r"^##\s", text, re.M)),
"no_placeholder": "TODO" not in text and "在此处" not in text,
}
if platform == "x":
checks["max_len"] = len(text) <= 1000
passed = all(checks.values())
return {"platform": platform, "passed": passed, "checks": checks}
def file_fingerprint(path: pathlib.Path) -> str:
return hashlib.sha256(path.read_bytes()).hexdigest()[:16]file_fingerprint 的作用不是装饰性的。当脚本或成片发生变化时,指纹变化可以用来判定关联的 QA 结果是否过期。这正是 CreatorFlow 中“旧评分卡主动失效”机制的最简实现。
第一,codex exec 的价值不在“生成”,而在“可编排的结构化执行”。 它把智能体的每一步变成一条有退出码、有结构化输出、有沙箱边界的命令。媒体自动化的复杂度不在于单点生成质量,而在于阶段之间的交接可靠性,而可靠性来自契约(schema)而非提示词措辞。
第二,门禁必须是代码,不能是 checklist。 平台适配、字数限制、占位符残留、文件指纹一致性,这些都可以被脚本无条件执行。把可脚本化的检查写成人工清单,是自动化流水线中最昂贵的反模式。
第三,多平台发布层已经被 MCP 和 SDK 抽象出来了。 unified-publish-mcp 提供了覆盖 28+ 平台的统一发布接口,RedFox SDK 也把公众号、知乎、头条等平台的 API 封装成了统一的客户端调用。编排器的职责是把内容送到“可发布”状态,发布动作本身可以委托给这些专用工具,而不是让 Codex 去处理每个平台的鉴权和接口差异。
自媒体的自动化不是一个“让 AI 写文章”的问题,而是一个内容生产的状态机设计问题。Codex 在这个系统中的正确位置,是那个受约束的、可重复调用的阶段执行器,而不是一个吞掉整个流程的黑箱。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。