修改日志结构
This commit is contained in:
@@ -23,6 +23,7 @@ import uuid
|
||||
from io import BytesIO
|
||||
from dataclasses import dataclass, asdict
|
||||
from typing import Callable, List, Optional
|
||||
from urllib.error import HTTPError
|
||||
from urllib.parse import urlparse
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
@@ -41,7 +42,7 @@ _SUPPORTED_IMG_EXT = (".png", ".jpg", ".jpeg", ".bmp", ".webp")
|
||||
# 测试环境后端
|
||||
API_BASE_URL = "http://10.201.44.70:9018/ai-agent/"
|
||||
API_PATH = "api/v1/pqtest/generate"
|
||||
API_TIMEOUT = 90.0 # 后端最长 60s,留余量
|
||||
API_TIMEOUT = 300.0 # 后端最长 60s,留余量
|
||||
|
||||
# 进程级会话 id(多轮对话需保持一致),可通过 ``reset_session`` 重置
|
||||
_session_id: str = str(uuid.uuid4())
|
||||
@@ -62,7 +63,6 @@ def set_session_id(session_id: str) -> str:
|
||||
with _session_lock:
|
||||
old = _session_id
|
||||
_session_id = sid
|
||||
logger.info("[AIImage] 会话切换 %s -> %s", _mask_sid(old), _mask_sid(_session_id))
|
||||
return _session_id
|
||||
|
||||
|
||||
@@ -72,7 +72,6 @@ def reset_session() -> str:
|
||||
with _session_lock:
|
||||
old = _session_id
|
||||
_session_id = str(uuid.uuid4())
|
||||
logger.info("[AIImage] 会话切换 %s -> %s", _mask_sid(old), _mask_sid(_session_id))
|
||||
return _session_id
|
||||
|
||||
|
||||
@@ -144,27 +143,66 @@ def _call_pqtest_generate(user_message: str, session_id: str, timeout: float = A
|
||||
"session_id": session_id},
|
||||
ensure_ascii=False,
|
||||
).encode("utf-8")
|
||||
request_headers = {
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
"Accept": "application/json",
|
||||
"User-Agent": "pqAutomationApp/1.0",
|
||||
}
|
||||
endpoint = _api_endpoint()
|
||||
logger.info(
|
||||
"[AIImage] 请求生成 sid=%s prompt_len=%d prompt=%r",
|
||||
_mask_sid(session_id), len(user_message or ""), _truncate(user_message),
|
||||
)
|
||||
logger.debug("[AIImage] POST %s timeout=%.1fs", endpoint, timeout)
|
||||
logger.info(
|
||||
"[AIImage][REQUEST]\\nendpoint=%s\\nmethod=POST\\ntimeout=%.1fs\\nheaders=%s\\nbody=%s",
|
||||
endpoint,
|
||||
timeout,
|
||||
json.dumps(request_headers, ensure_ascii=False),
|
||||
payload.decode("utf-8", errors="replace"),
|
||||
)
|
||||
request = Request(
|
||||
endpoint,
|
||||
data=payload,
|
||||
method="POST",
|
||||
headers={
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
"Accept": "application/json",
|
||||
"User-Agent": "pqAutomationApp/1.0",
|
||||
},
|
||||
headers=request_headers,
|
||||
)
|
||||
t0 = time.monotonic()
|
||||
try:
|
||||
with urlopen(request, timeout=timeout) as response:
|
||||
raw = response.read()
|
||||
http_status = response.status
|
||||
response_headers = dict(response.headers.items())
|
||||
raw_text = raw.decode("utf-8", errors="replace")
|
||||
logger.info(
|
||||
"[AIImage][RESPONSE]\\nstatus=%s\\nheaders=%s\\nbody=%s",
|
||||
http_status,
|
||||
json.dumps(response_headers, ensure_ascii=False),
|
||||
raw_text,
|
||||
)
|
||||
except HTTPError as exc:
|
||||
elapsed = time.monotonic() - t0
|
||||
err_raw = b""
|
||||
try:
|
||||
err_raw = exc.read() or b""
|
||||
except Exception:
|
||||
err_raw = b""
|
||||
err_text = err_raw.decode("utf-8", errors="replace") if err_raw else ""
|
||||
err_headers = {}
|
||||
try:
|
||||
if exc.headers is not None:
|
||||
err_headers = dict(exc.headers.items())
|
||||
except Exception:
|
||||
err_headers = {}
|
||||
logger.error(
|
||||
"[AIImage][RESPONSE_ERROR] sid=%s elapsed=%.2fs status=%s reason=%s\\nheaders=%s\\nbody=%s",
|
||||
_mask_sid(session_id),
|
||||
elapsed,
|
||||
getattr(exc, "code", "?"),
|
||||
str(exc),
|
||||
json.dumps(err_headers, ensure_ascii=False),
|
||||
err_text,
|
||||
)
|
||||
raise
|
||||
except Exception as exc:
|
||||
elapsed = time.monotonic() - t0
|
||||
logger.error(
|
||||
@@ -173,15 +211,13 @@ def _call_pqtest_generate(user_message: str, session_id: str, timeout: float = A
|
||||
)
|
||||
raise
|
||||
elapsed = time.monotonic() - t0
|
||||
logger.debug(
|
||||
"[AIImage] HTTP %s 收到 %d bytes elapsed=%.2fs",
|
||||
http_status, len(raw), elapsed,
|
||||
)
|
||||
logger.info("[AIImage] HTTP %s 收到 %d bytes elapsed=%.2fs", http_status, len(raw), elapsed)
|
||||
try:
|
||||
result = json.loads(raw.decode("utf-8"))
|
||||
except Exception as exc:
|
||||
logger.error("[AIImage] 响应解析失败 sid=%s raw=%r", _mask_sid(session_id), raw[:200])
|
||||
raise RuntimeError(f"AI 接口返回非 JSON:{raw[:200]!r}") from exc
|
||||
raw_text = raw.decode("utf-8", errors="replace")
|
||||
logger.error("[AIImage] 响应解析失败 sid=%s raw=%s", _mask_sid(session_id), raw_text)
|
||||
raise RuntimeError(f"AI 接口返回非 JSON:{raw_text}") from exc
|
||||
|
||||
code = result.get("code")
|
||||
message = result.get("message") or ""
|
||||
|
||||
Reference in New Issue
Block a user