mirror of
https://github.com/AstrBotDevs/AstrBot.git
synced 2026-08-30 17:33:24 +08:00
fix: 修复工具图片缓存目录清理后无法保存 (#9490)
This commit is contained in:
@@ -88,8 +88,9 @@ class ToolImageCache:
|
||||
file_name = f"{tool_call_id}_{index}{ext}"
|
||||
file_path = os.path.join(self._cache_dir, file_name)
|
||||
|
||||
# Decode and save the image
|
||||
try:
|
||||
# Runtime cache cleanup may remove empty subdirectories.
|
||||
os.makedirs(self._cache_dir, exist_ok=True)
|
||||
image_bytes = base64.b64decode(base64_data)
|
||||
with open(file_path, "wb") as f:
|
||||
f.write(image_bytes)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import base64
|
||||
from pathlib import Path
|
||||
|
||||
from astrbot.core.agent.tool_image_cache import tool_image_cache
|
||||
from astrbot.core.utils.storage_cleaner import StorageCleaner
|
||||
|
||||
|
||||
@@ -83,3 +85,29 @@ def test_storage_cleaner_cleanup_truncates_active_log_and_removes_cache(tmp_path
|
||||
assert not (temp_dir / "nested").exists()
|
||||
assert result["status"]["logs"]["size_bytes"] == 0
|
||||
assert result["status"]["cache"]["size_bytes"] == 0
|
||||
|
||||
|
||||
def test_tool_image_cache_recovers_after_storage_cleanup(tmp_path, monkeypatch):
|
||||
data_dir = tmp_path / "data"
|
||||
temp_dir = data_dir / "temp"
|
||||
cache_dir = temp_dir / tool_image_cache.CACHE_DIR_NAME
|
||||
image_bytes = b"generated-image"
|
||||
|
||||
_write_bytes(cache_dir / "stale.png", 32)
|
||||
monkeypatch.setattr(tool_image_cache, "_cache_dir", str(cache_dir))
|
||||
|
||||
cleaner = StorageCleaner({}, data_dir=data_dir, temp_dir=temp_dir)
|
||||
cleaner.cleanup("cache")
|
||||
|
||||
assert not cache_dir.exists()
|
||||
|
||||
cached_image = tool_image_cache.save_image(
|
||||
base64_data=base64.b64encode(image_bytes).decode("ascii"),
|
||||
tool_call_id="call-test",
|
||||
tool_name="test-tool",
|
||||
mime_type="image/jpeg",
|
||||
)
|
||||
cached_path = Path(cached_image.file_path)
|
||||
|
||||
assert cached_path == cache_dir / "call-test_0.jpg"
|
||||
assert cached_path.read_bytes() == image_bytes
|
||||
|
||||
Reference in New Issue
Block a user