여러 줄 주석이 설명보다 경위(예전·실측·지적)를 적고 있어 읽는 사람이 결론을 찾기 어려웠다. - ts·tsx·js·mjs·css·py 478개: 여러 줄 주석은 첫 문장 한 줄로, 과거형·날짜 문장은 삭제 - 주석 위치는 TypeScript 파서·파이썬 tokenize/ast 로 찾는다 — 문자열 안의 # · /* 는 건드리지 않는다 - eslint·ts·noqa·type: ignore 같은 지시 주석은 그대로 둔다 파이썬 275개 정리 전후 AST 동일, TS 298개 주석 뺀 토큰 동일(빈 JSX 주석 10곳만 차이). site·frontend·admin tsc, site vitest 105 passed Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
62 lines
2.4 KiB
Python
62 lines
2.4 KiB
Python
"""SiteOntology(o2o-site-ontology) 클라이언트 — 이 가게에 맞는 검색 키워드를 받아 온다."""
|
|
|
|
import httpx
|
|
|
|
from common.logger import LOG
|
|
from config.server_configs import external_api_config
|
|
|
|
# 로컬 임베딩 모델이라 호출당 1~2초다.
|
|
TIMEOUT_SEC = 15.0
|
|
DEFAULT_LIMIT = 40
|
|
|
|
PUBLISH_PATH = "/v1/merchants/publish"
|
|
MATCH_PATH = "/v1/match"
|
|
|
|
|
|
class SiteOntologyError(RuntimeError):
|
|
"""SiteOntology 호출 실패(네트워크·타임아웃·4xx/5xx·업체 미해석)."""
|
|
|
|
|
|
def base_url() -> str:
|
|
return (external_api_config.site_ontology_url or "").strip().rstrip("/")
|
|
|
|
|
|
def is_configured() -> bool:
|
|
return bool(base_url())
|
|
|
|
|
|
async def _post(client: httpx.AsyncClient, path: str, body: dict) -> dict:
|
|
res = await client.post(f"{base_url()}{path}", json=body)
|
|
if res.status_code >= 400:
|
|
raise SiteOntologyError(f"{path} HTTP {res.status_code}: {res.text[:200]}")
|
|
try:
|
|
data = res.json()
|
|
except ValueError as ex:
|
|
raise SiteOntologyError(f"{path} 응답이 JSON 이 아니다: {res.text[:200]}") from ex
|
|
if not isinstance(data, dict):
|
|
raise SiteOntologyError(f"{path} 응답 모양이 다르다: {type(data).__name__}")
|
|
return data
|
|
|
|
|
|
async def match_for_merchant(merchant: dict, limit: int = DEFAULT_LIMIT) -> dict:
|
|
"""업체를 저장하고, 그 업체로 해석된 추천 결과(/v1/match 응답)를 돌려준다."""
|
|
body = {**merchant, "generate": False}
|
|
try:
|
|
async with httpx.AsyncClient(timeout=TIMEOUT_SEC) as client:
|
|
try:
|
|
await _post(client, PUBLISH_PATH, body)
|
|
except SiteOntologyError as ex:
|
|
if not body.get("regionId"):
|
|
raise
|
|
LOG.w(f"[site-ontology] regionId={body['regionId']} 로 저장하지 못했다 — 지역 없이 다시 보낸다: {ex}")
|
|
body = {**body, "regionId": None}
|
|
await _post(client, PUBLISH_PATH, body)
|
|
result = await _post(client, MATCH_PATH, {"query": merchant["externalId"], "limit": limit})
|
|
except httpx.HTTPError as ex:
|
|
raise SiteOntologyError(f"{type(ex).__name__}: {ex}") from ex
|
|
|
|
resolved = result.get("resolved") or {}
|
|
if resolved.get("externalId") != merchant["externalId"]:
|
|
raise SiteOntologyError(f"업체가 해석되지 않았다 — externalId={merchant['externalId']}")
|
|
return result
|