log request also

This commit is contained in:
Muzhen Gaming
2025-10-16 10:06:47 +08:00
parent 1f9f921829
commit 5901254405
3 changed files with 204 additions and 30 deletions

View File

@@ -17,6 +17,11 @@ from PIL import ImageGrab
import keyboard
from .config import Settings, ensure_dirs, data_paths
from .debug_http import chat_completion_with_logging, log_attempt_error
def _now_stamp() -> str:
return dt.datetime.now().strftime("%Y%m%d-%H%M%S")
class State:
@@ -99,10 +104,6 @@ def _setup_logging(log_path: str):
# No file logging when debug is off
def _now_stamp() -> str:
return dt.datetime.now().strftime("%Y%m%d-%H%M%S")
def capture_active_window(state: State):
"""Capture the current active window (Windows). Fallback to full screen if needed."""
logging.debug("capture_active_window: start; captures_dir=%s", state.captures_dir)
@@ -187,34 +188,49 @@ def send_to_openai(state: State):
attempts = max(1, state.cfg.retries)
last_err = None
for i in range(attempts):
try:
logging.debug("send_to_openai: attempt %d/%d", i + 1, attempts)
resp = client.chat.completions.create(
model=state.cfg.model,
messages=[{"role": "user", "content": content_items}],
)
text = resp.choices[0].message.content or ""
state.response_text = text
try:
for i in range(attempts):
try:
with open(state.response_path, "w", encoding="utf-8") as f:
f.write(text)
except Exception:
pass
logging.info("OpenAI response received and stored.")
logging.debug(
"send_to_openai: response_len=%d written_to=%s",
len(text),
state.response_path,
)
return
except Exception as e:
last_err = e
backoff = min(8, 2 ** i)
logging.warning(f"OpenAI send failed (attempt {i+1}/{attempts}): {e}; retrying in {backoff}s")
time.sleep(backoff)
logging.debug("send_to_openai: attempt %d/%d", i + 1, attempts)
messages = [{"role": "user", "content": content_items}]
text = chat_completion_with_logging(
client,
base,
api_key,
model=state.cfg.model,
messages=messages,
app_dir=state.cfg.app_dir,
attempt=i + 1,
)
logging.exception(f"All attempts to send to OpenAI failed: {last_err}")
state.response_text = text
try:
with open(state.response_path, "w", encoding="utf-8") as f:
f.write(text)
except Exception:
pass
logging.info("OpenAI response received and stored.")
logging.debug(
"send_to_openai: response_len=%d written_to=%s",
len(text),
state.response_path,
)
return
except Exception as e:
last_err = e
backoff = min(8, 2 ** i)
logging.warning(
f"OpenAI send failed (attempt {i+1}/{attempts}): {e}; retrying in {backoff}s"
)
log_attempt_error(state.cfg.app_dir, i + 1, e)
time.sleep(backoff)
logging.exception(f"All attempts to send to OpenAI failed: {last_err}")
finally:
try:
client.close()
except Exception:
pass
def type_response(state: State):