from pathlib import Path
p=Path('/opt/struktur/social-media-agent/codex_image_adapter.py'); s=p.read_text()
old='def generate_with_codex(prompt: str, output_path: Path, *, timeout: int = 600) -> dict[str, Any]:'
new='def generate_with_codex(prompt: str, output_path: Path, *, reference_image: Path | None = None, timeout: int = 600) -> dict[str, Any]:'
assert s.count(old)==1; s=s.replace(old,new)
old='''    if not TRUSTED_WORKSPACE.is_dir():
        raise CodexImageError(f"Codex-Arbeitskontext fehlt: {TRUSTED_WORKSPACE}")
    before = _image_paths()'''
new='''    if not TRUSTED_WORKSPACE.is_dir():
        raise CodexImageError(f"Codex-Arbeitskontext fehlt: {TRUSTED_WORKSPACE}")
    if reference_image is not None:
        reference_image = Path(reference_image).resolve()
        if not reference_image.is_file():
            raise CodexImageError(f"Referenzbild fehlt: {reference_image}")
    before = _image_paths()'''
assert s.count(old)==1; s=s.replace(old,new)
old='''    command = [CODEX, "exec", "--sandbox", "workspace-write", "--skip-git-repo-check", "--ephemeral", "-C", str(TRUSTED_WORKSPACE), "--json", prompt]'''
new='''    command = [CODEX, "exec", "--sandbox", "workspace-write", "--skip-git-repo-check", "--ephemeral", "-C", str(TRUSTED_WORKSPACE)]
    if reference_image is not None:
        command.extend(["--image", str(reference_image)])
    command.extend(["--json", prompt])'''
assert s.count(old)==1; s=s.replace(old,new)
p.write_text(s); print('codex adapter accepts visual reference input')
