"""Exercise TOML configuration examples and a notification handler with test input."""
import json
import subprocess
import sys
import tomllib
BASE = '''model = "fixture-default"
approval_policy = "on-request"
[sandbox_workspace_write]
network_access = false
'''
PROFILE = '''model = "fixture-review"
[history]
persistence = "none"
'''
PROVIDER = '''
[model_providers.fixture]
name = "Training endpoint"
base_url = "https://fixture.example.invalid/v1"
wire_api = "responses"
env_key = "FIXTURE_API_KEY"
http_headers = { "X-Demo" = "workshop" }
env_http_headers = { "X-Flag" = "FIXTURE_FLAG" }
'''
HANDLER = """import json,sys
event = json.loads(sys.argv[1])
if event.get("type") == "agent-turn-complete":
print(json.dumps({"thread":event["thread-id"],"turn":event["turn-id"]}))
"""
def case(name, inputs, operation, expected, actual):
assert actual == expected, (name, actual)
return dict(name=name, input=inputs, operation=operation, expected=expected, actual=actual)
def exercise():
base = tomllib.loads(BASE)
profile = tomllib.loads(PROFILE)
merged = {**base, **profile}
cases = [case("advanced_profile_example", [BASE, PROFILE],
"Parse TOML samples and overlay their distinct top-level fields in Python",
["fixture-review", False, "none"],
[merged["model"], merged["sandbox_workspace_write"]["network_access"], merged["history"]["persistence"]])]
provider = tomllib.loads(PROVIDER)["model_providers"]["fixture"]
fields = ("wire_api", "env_key", "http_headers", "env_http_headers")
cases.append(case("advanced_provider_fields", PROVIDER, "Parse provider settings and inspect exact values",
["responses","FIXTURE_API_KEY",{"X-Demo":"workshop"},{"X-Flag":"FIXTURE_FLAG"}],
[provider[field] for field in fields]))
for event, expected in (("agent-turn-complete", {"thread":"fixture-thread","turn":"fixture-turn"}),
("other-event", None)):
data = {"type":event,"thread-id":"fixture-thread","turn-id":"fixture-turn",
"cwd":"fixture","input-messages":["fixture input"],"last-assistant-message":"fixture output"}
p = subprocess.run([sys.executable,"-I","-c",HANDLER,json.dumps(data)], capture_output=True,
encoding="utf-8",timeout=10,check=True)
cases.append(case("advanced_notify_"+event,data,"Run the local handler with a JSON argv payload",expected,
json.loads(p.stdout) if p.stdout else None))
return dict(version={"python":sys.version.split()[0]},cases=cases)
if __name__ == "__main__":
print(json.dumps(exercise()))
本例需在 Windows,CPython 3.12.14。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows,CPython 3.12.14。
入口文件:projects/codex-advanced/advanced_lab.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
advanced_lab.py,或下载右侧课程工程包。 - 确认本地已准备 Windows,CPython 3.12.14。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a87.zip
课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a90.zip