Skip to content

A programming framework for agentic AI

Add an optional base path to FileSurfer (#5886)

Hussein Mozannar7d17b2292506
diff --git a/python/packages/autogen-ext/src/autogen_ext/agents/file_surfer/_file_surfer.py b/python/packages/autogen-ext/src/autogen_ext/agents/file_surfer/_file_surfer.py
index 8ab6ff9c6..aec34cc63 100644
--- a/python/packages/autogen-ext/src/autogen_ext/agents/file_surfer/_file_surfer.py
+++ b/python/packages/autogen-ext/src/autogen_ext/agents/file_surfer/_file_surfer.py
@@ -1,4 +1,5 @@
import json
+import os
import traceback
from typing import List, Sequence, Tuple
@@ -54,6 +55,7 @@ class FileSurfer(BaseChatAgent, Component[FileSurferConfig]):
name (str): The agent's name
model_client (ChatCompletionClient): The model to use (must be tool-use enabled)
description (str): The agent's description used by the team. Defaults to DEFAULT_DESCRIPTION
+ base_path (str): The base path to use for the file browser. Defaults to the current working directory.
"""
@@ -75,11 +77,12 @@ class FileSurfer(BaseChatAgent, Component[FileSurferConfig]):
name: str,
model_client: ChatCompletionClient,
description: str = DEFAULT_DESCRIPTION,
+ base_path: str = os.getcwd(),
) -> None:
super().__init__(name, description)
self._model_client = model_client
self._chat_history: List[LLMMessage] = []
- self._browser = MarkdownFileBrowser(viewport_size=1024 * 5)
+ self._browser = MarkdownFileBrowser(viewport_size=1024 * 5, base_path=base_path)
@property
def produced_message_types(self) -> Sequence[type[ChatMessage]]:
diff --git a/python/packages/autogen-ext/src/autogen_ext/agents/file_surfer/_markdown_file_browser.py b/python/packages/autogen-ext/src/autogen_ext/agents/file_surfer/_markdown_file_browser.py
index b26fa49c2..03d3e72ea 100644
--- a/python/packages/autogen-ext/src/autogen_ext/agents/file_surfer/_markdown_file_browser.py
+++ b/python/packages/autogen-ext/src/autogen_ext/agents/file_surfer/_markdown_file_browser.py
@@ -17,13 +17,14 @@ class MarkdownFileBrowser:
# TODO: Fix unfollowed import
def __init__( # type: ignore
- self, viewport_size: Union[int, None] = 1024 * 8
+ self, viewport_size: Union[int, None] = 1024 * 8, base_path: str = os.getcwd()
):
"""
Instantiate a new MarkdownFileBrowser.
Arguments:
viewport_size: Approximately how many *characters* fit in the viewport. Viewport dimensions are adjusted dynamically to avoid cutting off words (default: 8192).
+ base_path: The base path to use for the file browser. Defaults to the current working directory.
"""
self.viewport_size = viewport_size # Applies only to the standard uri types
self.history: List[Tuple[str, float]] = list()
@@ -31,16 +32,17 @@ class MarkdownFileBrowser:
self.viewport_current_page = 0
self.viewport_pages: List[Tuple[int, int]] = list()
self._markdown_converter = MarkItDown()
- self.set_path(os.getcwd())
+ self._base_path = base_path
self._page_content: str = ""
self._find_on_page_query: Union[str, None] = None
self._find_on_page_last_result: Union[int, None] = None # Location of the last result
+ self.set_path(self._base_path)
@property
def path(self) -> str:
"""Return the path of the current page."""
if len(self.history) == 0:
- return os.getcwd()
+ return self._base_path
else:
return self.history[-1][0]