Explorer
/tmp/patch_publish_ready.py
← Zurück ↓ Download
from pathlib import Path
p=Path('/opt/struktur/social-media-agent/publish_ready.py')
s=p.read_text()
old='from openai_image_adapter import OpenAIImageError, generate_with_openai_image'
new='from codex_image_adapter import CodexImageError, generate_with_codex'
assert s.count(old)==1
s=s.replace(old,new)
start=s.index('def generate_package_image(')
end=s.index('\n\ndef _direct_url', start)
replacement='''def generate_package_image(package_id: str, *, force: bool = False) -> dict:
    """Generate exactly one image through Codex built-in image_generation."""
    con = sqlite3.connect(CASES_DB); con.row_factory = sqlite3.Row
    row = con.execute("SELECT * FROM publication_packages WHERE package_id=?", (package_id,)).fetchone()
    if not row:
        con.close(); raise ValueError("publication package not found")
    package = dict(row)
    old_path = valid_package_image_path(package)
    old_version = int(package.get("image_version") or 0)
    prompt = fach_image_prompt(package)
    prompt_ok, prompt_reason = image_relevance_gate(package, prompt)
    if not prompt_ok:
        con.execute("UPDATE publication_packages SET image_status='error',image_error=?,updated_at=? WHERE package_id=?", (prompt_reason, _now(), package_id)); con.commit(); con.close()
        return {"ok": False, "status": "Bild fachlich unpassend", "error": prompt_reason}
    target = (BASE_DIR / "generated_visuals" / f"{package['signal_id']}_codex-image-gen-v{old_version + 1}.png").resolve()
    try:
        result = generate_with_codex(prompt, target)
        now = _now(); version = old_version + 1
        con.execute("UPDATE publication_packages SET image_status='present',image_path=?,image_created_at=?,image_generator=?,image_version=?,image_error='',image_prompt=?,image_prompt_used=?,media_source_type='Codex built-in image_gen',media_source_ref=?,media_quality='visuelle Abnahme offen',updated_at=? WHERE package_id=?", (str(target), now, "Codex built-in image_gen", version, prompt, prompt, result["origin_path"], now, package_id))
        con.commit(); con.close()
        return {"ok": True, "status": "Bild vorhanden — visuelle Abnahme offen", "path": str(target), "version": version, "generator": "Codex built-in image_gen", "origin_path": result["origin_path"], "sha256": result["sha256"], "bytes": result["bytes"]}
    except (CodexImageError, OSError, ValueError) as exc:
        now = _now()
        con.execute("UPDATE publication_packages SET image_status='error',image_error=?,image_generator='Codex built-in image_gen',image_prompt_used=?,updated_at=? WHERE package_id=?", (str(exc)[:500], prompt, now, package_id)); con.commit(); con.close()
        return {"ok": False, "status": "Codex-Bild konnte nicht erzeugt werden", "error": str(exc), "generator": "Codex built-in image_gen"}
'''
s=s[:start]+replacement+s[end:]
p.write_text(s)
print('patched',p,'old_bytes',len(s))