109 lines
3.3 KiB
Python
Raw Normal View History

# api/initialization.py
import os
from pathlib import Path
from dotenv import load_dotenv
from loguru import logger
from pydantic import BaseModel
from .config import Settings
class _AppPaths(BaseModel):
"""Internal model representing all application paths"""
app_root: Path
static_root: Path
user_files: Path
ui_root: Path
config_dir: Path
database_uri: str
class AppInitializer:
"""Handles application initialization including paths and environment setup"""
def __init__(self, settings: Settings, app_path: str):
"""
Initialize the application structure.
Args:
settings: Application settings
app_path: Path to the application code directory
"""
self.settings = settings
self._app_path = Path(app_path)
self._paths = self._init_paths()
self._create_directories()
self._load_environment()
logger.info(f"Initializing application data folder: {self.app_root} ")
def _get_app_root(self) -> Path:
"""Determine application root directory"""
if app_dir := os.getenv("AUTOGENSTUDIO_APPDIR"):
return Path(app_dir)
return Path.home() / ".autogenstudio"
def _get_database_uri(self, app_root: Path) -> str:
"""Generate database URI based on settings or environment"""
if db_uri := os.getenv("AUTOGENSTUDIO_DATABASE_URI"):
return db_uri
return self.settings.DATABASE_URI.replace("./", str(app_root) + "/")
def _init_paths(self) -> _AppPaths:
"""Initialize and return AppPaths instance"""
app_root = self._get_app_root()
return _AppPaths(
app_root=app_root,
static_root=app_root / "files",
user_files=app_root / "files" / "user",
ui_root=self._app_path / "ui",
config_dir=app_root / self.settings.CONFIG_DIR,
database_uri=self._get_database_uri(app_root),
)
def _create_directories(self) -> None:
"""Create all required directories"""
self.app_root.mkdir(parents=True, exist_ok=True)
dirs = [self.static_root, self.user_files, self.ui_root, self.config_dir]
for path in dirs:
path.mkdir(parents=True, exist_ok=True)
def _load_environment(self) -> None:
"""Load environment variables from .env file if it exists"""
env_file = self.app_root / ".env"
if env_file.exists():
Add Token Streaming in AGS , Support Env variables (#5659) <!-- Thank you for your contribution! Please review https://microsoft.github.io/autogen/docs/Contribute before opening a pull request. --> This PR has 3 main improvements. - Token streaming - Adds support for environment variables in the app settings - Updates AGS to persist Gallery entry in db. ## Adds Token Streaming in AGS. Agentchat now supports streaming of tokens via `ModelClientStreamingChunkEvent `. This PR is to track progress on supporting that in the AutoGen Studio UI. If `model_client_stream` is enabled in an assitant agent, then token will be streamed in UI. ```python streaming_assistant = AssistantAgent( name="assistant", model_client=model_client, system_message="You are a helpful assistant.", model_client_stream=True, # Enable streaming tokens. ) ``` https://github.com/user-attachments/assets/74d43d78-6359-40c3-a78e-c84dcb5e02a1 ## Env Variables Also adds support for env variables in AGS Settings You can set env variables that are loaded just before a team is run. Handy to set variable to be used by tools etc. <img width="1291" alt="image" src="https://github.com/user-attachments/assets/437b9d90-ccee-42f7-be5d-94ab191afd67" /> > Note: the set variables are available to the server process. <!-- Please add a reviewer to the assignee section when you create a PR. If you don't have the access to it, we will shortly find a reviewer and assign them to your PR. --> ## Why are these changes needed? <!-- Please give a short summary of the change and the problem this solves. --> ## Related issue number <!-- For example: "Closes #1234" --> Closes #5627 Closes #5662 Closes #5619 ## Checks - [ ] I've included any doc changes needed for <https://microsoft.github.io/autogen/>. See <https://github.com/microsoft/autogen/blob/main/CONTRIBUTING.md> to build and test documentation locally. - [ ] I've added tests (if relevant) corresponding to the changes introduced in this PR. - [ ] I've made sure all auto checks have passed.
2025-02-25 07:21:08 -08:00
# logger.info(f"Loading environment variables from {env_file}")
load_dotenv(str(env_file))
# Properties for accessing paths
@property
def app_root(self) -> Path:
"""Root directory for the application"""
return self._paths.app_root
@property
def static_root(self) -> Path:
"""Directory for static files"""
return self._paths.static_root
@property
def user_files(self) -> Path:
"""Directory for user files"""
return self._paths.user_files
@property
def ui_root(self) -> Path:
"""Directory for UI files"""
return self._paths.ui_root
@property
def config_dir(self) -> Path:
"""Directory for configuration files"""
return self._paths.config_dir
@property
def database_uri(self) -> str:
"""Database connection URI"""
return self._paths.database_uri