"""COPY 흐름. 단계 구현: copy_steps / 프롬프트: prompts/copy / 호출·검증: external/gemini_text.""" from common.logger import LOG from services.copy_steps import CopyAborted, prepare_copy, generate_copy, save_copy, fill_faqs from services.external import gemini_text from services.job_progress import JobProgress COPY_STEPS = ("prepare", "generate", "save", "faq_fill") async def run_copy(job: dict) -> dict: progress = JobProgress(job, COPY_STEPS) payload = job["payload"] async with progress.step("prepare"): inputs = await prepare_copy(payload["place_id"], payload["owner_user_id"]) copy = None note = None if inputs.ungrounded or not gemini_text.is_configured(): note = "근거로 쓸 확인된 fact 가 없다" if inputs.ungrounded else "GEMINI_API_KEY 미설정" await progress.skip("generate", "no_facts" if inputs.ungrounded else "not_configured") if inputs.catalog is None and not inputs.ungrounded: raise CopyAborted(note) else: try: async with progress.step("generate"): copy = await generate_copy(inputs) except gemini_text.GeminiError as ex: # ★ 호출 실패는 미설정과 같은 취급이다 — fact 만으로도 편집·발행이 되고 # (publish_gate.check_unique_content), 발행은 고유 콘텐츠 0건으로 막지 않는다. # 자세한 배경은 DEVLOG.md 참고. note = f"생성 호출 실패: {ex}" LOG.w(f"[copy] 생성 실패, fact 만으로 계속: {ex}") await progress.skip("generate", "generation_failed") async with progress.step("save"): result = await save_copy(inputs, copy) if inputs.catalog is not None: async with progress.step("faq_fill"): result["faq_fill"] = await fill_faqs( inputs.place.place_id, inputs.catalog, inputs.known_fact_keys, inputs.place.phone, ) else: await progress.skip("faq_fill", "no_catalog") if note: result["note"] = note return result