Skip to content

Commit

Permalink
fix(agent): Full fix for CLI breakage introduced in cf00c33
Browse files Browse the repository at this point in the history
  • Loading branch information
Pwuts committed Apr 22, 2024
1 parent 0704404 commit 898317c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
7 changes: 5 additions & 2 deletions autogpts/autogpt/autogpt/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ async def run_auto_gpt(
level=log_level,
log_format=log_format,
log_file_format=log_file_format,
config=config.logging,
tts_config=config.tts_config,
)

Expand Down Expand Up @@ -389,6 +390,7 @@ async def run_auto_gpt_server(
level=log_level,
log_format=log_format,
log_file_format=log_file_format,
config=config.logging,
tts_config=config.tts_config,
)

Expand Down Expand Up @@ -486,12 +488,13 @@ async def run_interaction_loop(
legacy_config = agent.legacy_config
ai_profile = agent.ai_profile
logger = logging.getLogger(__name__)
config = LoggingConfig.from_env()

cycle_budget = cycles_remaining = _get_cycle_budget(
legacy_config.continuous_mode, legacy_config.continuous_limit
)
spinner = Spinner("Thinking...", plain_output=config.plain_console_output)
spinner = Spinner(
"Thinking...", plain_output=legacy_config.logging.plain_console_output
)
stop_reason = None

def graceful_agent_interrupt(signum: int, frame: Optional[FrameType]) -> None:
Expand Down
2 changes: 2 additions & 0 deletions autogpts/autogpt/autogpt/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
OpenAIModelName,
)
from autogpt.file_storage import FileStorageBackendName
from autogpt.logs.config import LoggingConfig
from autogpt.plugins.plugins_config import PluginsConfig
from autogpt.speech import TTSConfig

Expand Down Expand Up @@ -58,6 +59,7 @@ class Config(SystemSettings, arbitrary_types_allowed=True):
)

# TTS configuration
logging: LoggingConfig = LoggingConfig()
tts_config: TTSConfig = TTSConfig()

# File storage
Expand Down
52 changes: 30 additions & 22 deletions autogpts/autogpt/autogpt/logs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ def configure_logging(
log_format: Optional[LogFormatName | str] = None,
log_file_format: Optional[LogFormatName | str] = None,
plain_console_output: Optional[bool] = None,
config: Optional[LoggingConfig] = None,
tts_config: Optional[TTSConfig] = None,
) -> None:
"""Configure the native logging module, based on the environment config and any
specified overrides.
Arguments override values specified in the environment.
Overrides are also applied to `config`, if passed.
Should be usable as `configure_logging(**config.logging.dict())`, where
`config.logging` is a `LoggingConfig` object.
Expand All @@ -111,14 +113,16 @@ def configure_logging(
elif not isinstance(log_file_format, LogFormatName):
raise ValueError(f"Unknown log format '{log_format}'")

config = LoggingConfig.from_env()
config = config or LoggingConfig.from_env()

# Aggregate arguments + env config
level = logging.DEBUG if debug else level or config.level
log_dir = log_dir or config.log_dir
log_format = log_format or (LogFormatName.DEBUG if debug else config.log_format)
log_file_format = log_file_format or log_format or config.log_file_format
plain_console_output = (
# Aggregate env config + arguments
config.level = logging.DEBUG if debug else level or config.level
config.log_dir = log_dir or config.log_dir
config.log_format = log_format or (
LogFormatName.DEBUG if debug else config.log_format
)
config.log_file_format = log_file_format or log_format or config.log_file_format
config.plain_console_output = (
plain_console_output
if plain_console_output is not None
else config.plain_console_output
Expand All @@ -127,25 +131,25 @@ def configure_logging(
# Structured logging is used for cloud environments,
# where logging to a file makes no sense.
if log_format == LogFormatName.STRUCTURED:
plain_console_output = True
log_file_format = None
config.plain_console_output = True
config.log_file_format = None

# create log directory if it doesn't exist
if not log_dir.exists():
log_dir.mkdir()
if not config.log_dir.exists():
config.log_dir.mkdir()

log_handlers: list[logging.Handler] = []

if log_format in (LogFormatName.DEBUG, LogFormatName.SIMPLE):
console_format_template = TEXT_LOG_FORMAT_MAP[log_format]
if config.log_format in (LogFormatName.DEBUG, LogFormatName.SIMPLE):
console_format_template = TEXT_LOG_FORMAT_MAP[config.log_format]
console_formatter = AutoGptFormatter(console_format_template)
else:
console_formatter = StructuredLoggingFormatter()
console_format_template = SIMPLE_LOG_FORMAT

# Console output handlers
stdout = logging.StreamHandler(stream=sys.stdout)
stdout.setLevel(level)
stdout.setLevel(config.level)
stdout.addFilter(BelowLevelFilter(logging.WARNING))
stdout.setFormatter(console_formatter)
stderr = logging.StreamHandler()
Expand All @@ -162,30 +166,34 @@ def configure_logging(
user_friendly_output_logger = logging.getLogger(USER_FRIENDLY_OUTPUT_LOGGER)
user_friendly_output_logger.setLevel(logging.INFO)
user_friendly_output_logger.addHandler(
typing_console_handler if not plain_console_output else stdout
typing_console_handler if not config.plain_console_output else stdout
)
if tts_config:
user_friendly_output_logger.addHandler(TTSHandler(tts_config))
user_friendly_output_logger.addHandler(stderr)
user_friendly_output_logger.propagate = False

# File output handlers
if log_file_format is not None:
if level < logging.ERROR:
file_output_format_template = TEXT_LOG_FORMAT_MAP[log_file_format]
if config.log_file_format is not None:
if config.level < logging.ERROR:
file_output_format_template = TEXT_LOG_FORMAT_MAP[config.log_file_format]
file_output_formatter = AutoGptFormatter(
file_output_format_template, no_color=True
)

# INFO log file handler
activity_log_handler = logging.FileHandler(log_dir / LOG_FILE, "a", "utf-8")
activity_log_handler.setLevel(level)
activity_log_handler = logging.FileHandler(
config.log_dir / LOG_FILE, "a", "utf-8"
)
activity_log_handler.setLevel(config.level)
activity_log_handler.setFormatter(file_output_formatter)
log_handlers += [activity_log_handler]
user_friendly_output_logger.addHandler(activity_log_handler)

# ERROR log file handler
error_log_handler = logging.FileHandler(log_dir / ERROR_LOG_FILE, "a", "utf-8")
error_log_handler = logging.FileHandler(
config.log_dir / ERROR_LOG_FILE, "a", "utf-8"
)
error_log_handler.setLevel(logging.ERROR)
error_log_handler.setFormatter(
AutoGptFormatter(DEBUG_LOG_FORMAT, no_color=True)
Expand All @@ -196,7 +204,7 @@ def configure_logging(
# Configure the root logger
logging.basicConfig(
format=console_format_template,
level=level,
level=config.level,
handlers=log_handlers,
)

Expand Down

0 comments on commit 898317c

Please sign in to comment.