feat(components): override repr_args to truncate long base64 fields for safe logging#8591
feat(components): override repr_args to truncate long base64 fields for safe logging#8591KBVsent wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider replacing the hardcoded
9inlen(value) - 9withlen("base64://")or a named constant to avoid magic numbers and keep the logic robust if the scheme prefix ever changes. - You may want to lift
max_len = 64to a class-level constant or configuration option so that truncation behaviour can be adjusted or reused consistently across components.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider replacing the hardcoded `9` in `len(value) - 9` with `len("base64://")` or a named constant to avoid magic numbers and keep the logic robust if the scheme prefix ever changes.
- You may want to lift `max_len = 64` to a class-level constant or configuration option so that truncation behaviour can be adjusted or reused consistently across components.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request adds a __repr_args__ method to BaseMessageComponent to truncate long strings and base64-encoded values, preventing log pollution during debugging. The reviewer pointed out that the 64-character limit is too aggressive for general strings like URLs and plain text, which could hinder debugging. They suggested increasing this limit to 1024 characters and adding support for truncating data: URIs containing base64 data.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
When a message component carrying base64-encoded media (e.g. an
Imagebuilt viafromBase64) is logged or appears in a traceback, pydantic's defaultrepr()dumps the entire base64 string into the log. A single image can flood the logs with thousands of characters, making them unreadable and bloating log files. This was observed on the Discord adapter (logger.debug(f"... {image_component}")) but affects any place that stringifies a component.Modifications / 改动点
astrbot/core/message/components.py: Override__repr_args__on theBaseMessageComponentbase class to truncate long / base64 string field values in the repr only. Fields starting withbase64://render asbase64://<N chars>, and any other string over 64 chars is truncated with a length suffix. This is purely a display-layer change — it wraps pydantic's default__repr_args__and never mutates the stored data, so serialization (toDict), sending, and file conversion still see the full payload. Because it lives on the base class, all components (Image/Record/Video/File, etc.) are covered automatically with no per-call-site handling.Screenshots or Test Results / 运行截图或测试结果
Before:


After:
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Enhancements: