Skip to content

Agent Zero AI framework

image_get fix, history bulk compression fix

frdel5db74202d632
diff --git a/python/api/image_get.py b/python/api/image_get.py
index 005bbea6f..2ed1d1e13 100644
--- a/python/api/image_get.py
+++ b/python/api/image_get.py
@@ -1,5 +1,7 @@
import os
+import re
from python.helpers.api import ApiHandler
+from python.helpers import files
from flask import Request, Response, send_file
@@ -9,7 +11,23 @@ class ImageGet(ApiHandler):
path = input.get("path", request.args.get("path", ""))
if not path:
raise ValueError("No path provided")
-
+
+ # check if path is within base directory
+ if not files.is_in_base_dir(path):
+ raise ValueError("Path is outside of allowed directory")
+
+ # check if file has an image extension
+ # list of allowed image extensions
+ allowed_extensions = [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".svg"]
+ # get file extension
+ file_ext = os.path.splitext(path)[1].lower()
+ if file_ext not in allowed_extensions:
+ raise ValueError(f"File type not allowed. Allowed types: {', '.join(allowed_extensions)}")
+
+ # check if file exists
+ if not os.path.exists(path):
+ raise ValueError("File not found")
+
# send file
return send_file(path)
diff --git a/python/extensions/message_loop_prompts/_30_include_attachments._py b/python/extensions/message_loop_prompts/_30_include_attachments._py
deleted file mode 100644
index b58f58e7e..000000000
--- a/python/extensions/message_loop_prompts/_30_include_attachments._py
+++ /dev/null
@@ -1,40 +0,0 @@
-# python/extensions/monologue_start/include_attachments.py
-from python.helpers.extension import Extension
-from python.helpers.attachment_manager import AttachmentManager
-from agent import Agent, LoopData
-import os
-
-class IncludeAttachments(Extension):
- async def execute(self, loop_data: LoopData = LoopData(), **kwargs):
- attachments = self.agent.get_data('attachments') or []
- if attachments:
- loop_data.attachments = []
- file_manager = AttachmentManager(os.path.join(os.getcwd(), 'work_dir'))
-
- for attachment in attachments:
- if os.path.exists(attachment):
- filename = os.path.basename(attachment)
- file_type = file_manager.get_file_type(filename)
-
- attachment_html = f'<div class="attachment-item attachment-{file_type}">'
- if file_type == 'image':
- preview = file_manager.generate_image_preview(attachment)
- if preview:
- attachment_html += f'<img src="data:image/jpeg;base64,{preview}" alt="{filename}" class="attachment-preview"/>'
- else:
- # Add placeholder for non-image files
- attachment_html += f'<div class="attachment-placeholder">{file_type.upper()}</div>'
-
- # Add filename and extension badge
- ext = file_manager.get_file_extension(filename)
- attachment_html += f'''
- <div class="attachment-info">
- <span class="attachment-name">{filename}</span>
- <span class="attachment-badge">{ext}</span>
- </div>
- </div>'''
-
- loop_data.attachments.append(attachment_html)
-
- # Clear attachments after processing
- self.agent.set_data('attachments', [])
\ No newline at end of file
diff --git a/python/helpers/files.py b/python/helpers/files.py
index d5674c934..b0694bf06 100644
--- a/python/helpers/files.py
+++ b/python/helpers/files.py
@@ -249,6 +249,14 @@ def get_base_dir():
base_dir = os.path.dirname(os.path.abspath(os.path.join(__file__, "../../")))
return base_dir
+def is_in_base_dir(path: str):
+ # check if the given path is within the base directory
+ base_dir = get_base_dir()
+ # normalize paths to handle relative paths and symlinks
+ abs_path = os.path.abspath(path)
+ # check if the absolute path starts with the base directory
+ return os.path.commonpath([abs_path, base_dir]) == base_dir
+
def get_subdirectories(relative_path: str, include: str | list[str] = "*", exclude: str | list[str] | None = None):
abs_path = get_abs_path(relative_path)
diff --git a/python/helpers/history.py b/python/helpers/history.py
index daa6a45af..4e44fd0ef 100644
--- a/python/helpers/history.py
+++ b/python/helpers/history.py
@@ -248,6 +248,12 @@ class Bulk(Record):
self.summary: str = ""
self.records: list[Record] = []
+ def get_tokens(self):
+ if self.summary:
+ return tokens.approximate_tokens(self.summary)
+ else:
+ return sum([r.get_tokens() for r in self.records])
+
def output(
self, human_label: str = "user", ai_label: str = "ai"
) -> list[OutputMessage]:
@@ -402,7 +408,8 @@ class History(Record):
await bulk.summarize()
self.bulks.append(bulk)
self.topics.remove(topic)
- return True
+ return True
+ return False
async def compress_bulks(self):
# merge bulks if possible
@@ -410,11 +417,14 @@ class History(Record):
# remove oldest bulk if necessary
if not compressed:
self.bulks.pop(0)
+ return True
return compressed
async def merge_bulks_by(self, count: int):
- if len(self.bulks) > 0:
+ # if bulks is empty, return False
+ if len(self.bulks) == 0:
return False
+ # merge bulks in groups of count, even if there are fewer than count
bulks = await asyncio.gather(
*[
self.merge_bulks(self.bulks[i : i + count])