Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(channels): stdlib logging placeholders in dingtalk

shadowinlife61d5fe762595
diff --git a/agent/src/channels/dingtalk.py b/agent/src/channels/dingtalk.py
index d0a53db79..b1cf140e9 100644
--- a/agent/src/channels/dingtalk.py
+++ b/agent/src/channels/dingtalk.py
@@ -126,7 +126,7 @@ class VibeTradingDingTalkHandler(CallbackHandler):
if not content:
self.channel.logger.warning(
- "Received empty or unsupported message type: {}",
+ "Received empty or unsupported message type: %s",
chatbot_msg.message_type,
)
return AckMessage.STATUS_OK, "OK"
@@ -140,7 +140,7 @@ class VibeTradingDingTalkHandler(CallbackHandler):
or message.data.get("openConversationId")
)
- self.channel.logger.info("Received message from {} ({}): {}", sender_name, sender_id, content)
+ self.channel.logger.info("Received message from %s (%s): %s", sender_name, sender_id, content)
# Forward to Vibe-Trading via _on_message (non-blocking).
# Store reference to prevent GC before task completes.
@@ -232,7 +232,7 @@ class DingTalkChannel(BaseChannel):
)
self.logger.info(
- "Initializing Stream Client with Client ID: {}...",
+ "Initializing Stream Client with Client ID: %s...",
self.config.client_id,
)
credential = Credential(self.config.client_id, self.config.client_secret)
@@ -249,7 +249,7 @@ class DingTalkChannel(BaseChannel):
try:
await self._client.start()
except Exception as e:
- self.logger.warning("stream error: {}", e)
+ self.logger.warning("stream error: %s", e)
if self._running:
self.logger.info("Reconnecting stream in 5 seconds...")
await asyncio.sleep(5)
@@ -333,7 +333,7 @@ class DingTalkChannel(BaseChannel):
ext = Path(filename).suffix.lower()
if ext in self._ZIP_BEFORE_UPLOAD_EXTS or content_type == "text/html":
self.logger.info(
- "does not accept raw HTML attachments, zipping {} before upload",
+ "does not accept raw HTML attachments, zipping %s before upload",
filename,
)
return self._zip_bytes(filename, data)
@@ -342,7 +342,7 @@ class DingTalkChannel(BaseChannel):
def _validate_remote_media_url(self, media_ref: str) -> bool:
ok, err = validate_url_target(media_ref)
if not ok:
- self.logger.warning("remote media URL blocked ref={} reason={}", media_ref, err)
+ self.logger.warning("remote media URL blocked ref=%s reason=%s", media_ref, err)
return False
return True
@@ -358,15 +358,15 @@ class DingTalkChannel(BaseChannel):
def _next_remote_media_url(self, current_url: str, location: str | None) -> str | None:
if not self.config.allow_remote_media_redirects:
- self.logger.warning("media download redirect refused ref={}", current_url)
+ self.logger.warning("media download redirect refused ref=%s", current_url)
return None
if not location:
- self.logger.warning("media download redirect without Location ref={}", current_url)
+ self.logger.warning("media download redirect without Location ref=%s", current_url)
return None
next_url = urljoin(current_url, location)
if not self._redirect_host_allowed(current_url, next_url):
self.logger.warning(
- "media download cross-host redirect refused ref={} next={}",
+ "media download cross-host redirect refused ref=%s next=%s",
current_url,
next_url,
)
@@ -398,7 +398,7 @@ class DingTalkChannel(BaseChannel):
final_ok, final_err = validate_resolved_url(str(resp.url))
if not final_ok:
self.logger.warning(
- "remote media redirect blocked ref={} final={} reason={}",
+ "remote media redirect blocked ref=%s final=%s reason=%s",
media_ref,
resp.url,
final_err,
@@ -414,7 +414,7 @@ class DingTalkChannel(BaseChannel):
continue
if resp.status_code >= 400:
self.logger.warning(
- "media download failed status={} ref={}",
+ "media download failed status=%s ref=%s",
resp.status_code,
current_url,
)
@@ -425,14 +425,14 @@ class DingTalkChannel(BaseChannel):
total += len(chunk)
if total > DINGTALK_MAX_REMOTE_MEDIA_BYTES:
self.logger.warning(
- "media download too large ref={} bytes>{}",
+ "media download too large ref=%s bytes>%s",
current_url,
DINGTALK_MAX_REMOTE_MEDIA_BYTES,
)
return None, None
chunks.append(chunk)
return b"".join(chunks), (resp.headers.get("content-type") or "")
- self.logger.warning("media download exceeded redirect limit ref={}", media_ref)
+ self.logger.warning("media download exceeded redirect limit ref=%s", media_ref)
return None, None
current_url = media_ref
@@ -441,7 +441,7 @@ class DingTalkChannel(BaseChannel):
final_ok, final_err = validate_resolved_url(str(getattr(resp, "url", current_url)))
if not final_ok:
self.logger.warning(
- "remote media redirect blocked ref={} final={} reason={}",
+ "remote media redirect blocked ref=%s final=%s reason=%s",
media_ref,
getattr(resp, "url", current_url),
final_err,
@@ -457,26 +457,26 @@ class DingTalkChannel(BaseChannel):
continue
if resp.status_code >= 400:
self.logger.warning(
- "media download failed status={} ref={}",
+ "media download failed status=%s ref=%s",
resp.status_code,
current_url,
)
return None, None
if len(resp.content) > DINGTALK_MAX_REMOTE_MEDIA_BYTES:
self.logger.warning(
- "media download too large ref={} bytes>{}",
+ "media download too large ref=%s bytes>%s",
current_url,
DINGTALK_MAX_REMOTE_MEDIA_BYTES,
)
return None, None
return resp.content, (resp.headers.get("content-type") or "")
- self.logger.warning("media download exceeded redirect limit ref={}", media_ref)
+ self.logger.warning("media download exceeded redirect limit ref=%s", media_ref)
return None, None
except httpx.TransportError:
- self.logger.exception("media download network error ref={}", media_ref)
+ self.logger.exception("media download network error ref=%s", media_ref)
raise
except Exception:
- self.logger.exception("media download error ref={}", media_ref)
+ self.logger.exception("media download error ref=%s", media_ref)
return None, None
async def _read_media_bytes(
@@ -501,13 +501,13 @@ class DingTalkChannel(BaseChannel):
else:
local_path = Path(os.path.expanduser(media_ref))
if not local_path.is_file():
- self.logger.warning("media file not found: {}", local_path)
+ self.logger.warning("media file not found: %s", local_path)
return None, None, None
data = await asyncio.to_thread(local_path.read_bytes)
content_type = mimetypes.guess_type(local_path.name)[0]
return data, local_path.name, content_type
except Exception:
- self.logger.exception("media read error ref={}", media_ref)
+ self.logger.exception("media read error ref=%s", media_ref)
return None, None, None
async def _upload_media(
@@ -529,23 +529,23 @@ class DingTalkChannel(BaseChannel):
text = resp.text
result = resp.json() if resp.headers.get("content-type", "").startswith("application/json") else {}
if resp.status_code >= 400:
- self.logger.error("media upload failed status={} type={} body={}", resp.status_code, media_type, text[:500])
+ self.logger.error("media upload failed status=%s type=%s body=%s", resp.status_code, media_type, text[:500])
return None
errcode = result.get("errcode", 0)
if errcode != 0:
- self.logger.error("media upload api error type={} errcode={} body={}", media_type, errcode, text[:500])
+ self.logger.error("media upload api error type=%s errcode=%s body=%s", media_type, errcode, text[:500])
return None
sub = result.get("result") or {}
media_id = result.get("media_id") or result.get("mediaId") or sub.get("media_id") or sub.get("mediaId")
if not media_id:
- self.logger.error("media upload missing media_id body={}", text[:500])
+ self.logger.error("media upload missing media_id body=%s", text[:500])
return None
return str(media_id)
except httpx.TransportError:
- self.logger.exception("media upload network error type={}", media_type)
+ self.logger.exception("media upload network error type=%s", media_type)
raise
except Exception:
- self.logger.exception("media upload error type={}", media_type)
+ self.logger.exception("media upload error type=%s", media_type)
return None
async def _send_batch_message(
@@ -583,7 +583,7 @@ class DingTalkChannel(BaseChannel):
resp = await self._http.post(url, json=payload, headers=headers)
body = resp.text
if resp.status_code != 200:
- self.logger.error("send failed msgKey={} status={} body={}", msg_key, resp.status_code, body[:500])
+ self.logger.error("send failed msgKey=%s status=%s body=%s", msg_key, resp.status_code, body[:500])
return False
try:
result = resp.json()
@@ -591,15 +591,15 @@ class DingTalkChannel(BaseChannel):
result = {}
errcode = result.get("errcode")
if errcode not in (None, 0):
- self.logger.error("send api error msgKey={} errcode={} body={}", msg_key, errcode, body[:500])
+ self.logger.error("send api error msgKey=%s errcode=%s body=%s", msg_key, errcode, body[:500])
return False
- self.logger.debug("message sent to {} with msgKey={}", chat_id, msg_key)
+ self.logger.debug("message sent to %s with msgKey=%s", chat_id, msg_key)
return True
except httpx.TransportError:
- self.logger.exception("network error sending message msgKey={}", msg_key)
+ self.logger.exception("network error sending message msgKey=%s", msg_key)
raise
except Exception:
- self.logger.exception("Error sending message msgKey={}", msg_key)
+ self.logger.exception("Error sending message msgKey=%s", msg_key)
return False
async def _send_markdown_text(self, token: str, chat_id: str, content: str) -> bool:
@@ -625,11 +625,11 @@ class DingTalkChannel(BaseChannel):
)
if ok:
return True
- self.logger.warning("image url send failed, trying upload fallback: {}", media_ref)
+ self.logger.warning("image url send failed, trying upload fallback: %s", media_ref)
data, filename, content_type = await self._read_media_bytes(media_ref)
if not data:
- self.logger.error("media read failed: {}", media_ref)
+ self.logger.error("media read failed: %s", media_ref)
return False
filename = filename or self._guess_filename(media_ref, upload_type)
@@ -661,7 +661,7 @@ class DingTalkChannel(BaseChannel):
)
if ok:
return True
- self.logger.warning("image media_id send failed, falling back to file: {}", media_ref)
+ self.logger.warning("image media_id send failed, falling back to file: %s", media_ref)
return await self._send_batch_message(
token,
@@ -683,7 +683,7 @@ class DingTalkChannel(BaseChannel):
ok = await self._send_media_ref(token, msg.chat_id, media_ref)
if ok:
continue
- self.logger.error("media send failed for {}", media_ref)
+ self.logger.error("media send failed for %s", media_ref)
# Send visible fallback so failures are observable by the user.
filename = self._guess_filename(media_ref, self._guess_upload_type(media_ref))
await self._send_markdown_text(
@@ -706,7 +706,7 @@ class DingTalkChannel(BaseChannel):
permission checks before publishing to the bus.
"""
try:
- self.logger.info("inbound: {} from {}", content, sender_name)
+ self.logger.info("inbound: %s from %s", content, sender_name)
is_group = conversation_type == "2" and conversation_id
chat_id = f"group:{conversation_id}" if is_group else sender_id
session_key = None
@@ -747,19 +747,19 @@ class DingTalkChannel(BaseChannel):
payload = {"downloadCode": download_code, "robotCode": self.config.client_id}
resp = await self._http.post(api_url, json=payload, headers=headers)
if resp.status_code != 200:
- self.logger.error("get download URL failed: status={}, body={}", resp.status_code, resp.text)
+ self.logger.error("get download URL failed: status=%s, body=%s", resp.status_code, resp.text)
return None
result = resp.json()
download_url = result.get("downloadUrl")
if not download_url:
- self.logger.error("download URL not found in response: {}", result)
+ self.logger.error("download URL not found in response: %s", result)
return None
# Step 2: Download the file content
file_resp = await self._http.get(download_url, follow_redirects=True)
if file_resp.status_code != 200:
- self.logger.error("file download failed: status={}", file_resp.status_code)
+ self.logger.error("file download failed: status=%s", file_resp.status_code)
return None
# Save to media directory (accessible under workspace). filename
@@ -772,7 +772,7 @@ class DingTalkChannel(BaseChannel):
download_dir.mkdir(parents=True, exist_ok=True)
file_path = download_dir / safe_filename(filename)
await asyncio.to_thread(file_path.write_bytes, file_resp.content)
- self.logger.info("file saved: {}", file_path)
+ self.logger.info("file saved: %s", file_path)
return str(file_path)
except Exception:
self.logger.exception("file download error")