app_config.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from dataclasses import dataclass, field, fields, is_dataclass
  2. from typing import ClassVar
  3. from openhands.core import logger
  4. from openhands.core.config.agent_config import AgentConfig
  5. from openhands.core.config.config_utils import (
  6. OH_DEFAULT_AGENT,
  7. OH_MAX_ITERATIONS,
  8. get_field_info,
  9. )
  10. from openhands.core.config.llm_config import LLMConfig
  11. from openhands.core.config.sandbox_config import SandboxConfig
  12. from openhands.core.config.security_config import SecurityConfig
  13. @dataclass
  14. class AppConfig:
  15. """Configuration for the app.
  16. Attributes:
  17. llms: Dictionary mapping LLM names to their configurations.
  18. The default configuration is stored under the 'llm' key.
  19. agents: Dictionary mapping agent names to their configurations.
  20. The default configuration is stored under the 'agent' key.
  21. default_agent: Name of the default agent to use.
  22. sandbox: Sandbox configuration settings.
  23. runtime: Runtime environment identifier.
  24. file_store: Type of file store to use.
  25. file_store_path: Path to the file store.
  26. trajectories_path: Folder path to store trajectories.
  27. workspace_base: Base path for the workspace. Defaults to `./workspace` as absolute path.
  28. workspace_mount_path: Path to mount the workspace. Defaults to `workspace_base`.
  29. workspace_mount_path_in_sandbox: Path to mount the workspace in sandbox. Defaults to `/workspace`.
  30. workspace_mount_rewrite: Path to rewrite the workspace mount path.
  31. cache_dir: Path to cache directory. Defaults to `/tmp/cache`.
  32. run_as_openhands: Whether to run as openhands.
  33. max_iterations: Maximum number of iterations allowed.
  34. max_budget_per_task: Maximum budget per task, agent stops if exceeded.
  35. e2b_api_key: E2B API key.
  36. disable_color: Whether to disable terminal colors. For terminals that don't support color.
  37. debug: Whether to enable debugging mode.
  38. file_uploads_max_file_size_mb: Maximum file upload size in MB. `0` means unlimited.
  39. file_uploads_restrict_file_types: Whether to restrict upload file types.
  40. file_uploads_allowed_extensions: Allowed file extensions. `['.*']` allows all.
  41. """
  42. llms: dict[str, LLMConfig] = field(default_factory=dict)
  43. agents: dict = field(default_factory=dict)
  44. default_agent: str = OH_DEFAULT_AGENT
  45. sandbox: SandboxConfig = field(default_factory=SandboxConfig)
  46. security: SecurityConfig = field(default_factory=SecurityConfig)
  47. runtime: str = 'eventstream'
  48. file_store: str = 'memory'
  49. file_store_path: str = '/tmp/file_store'
  50. trajectories_path: str | None = None
  51. workspace_base: str | None = None
  52. workspace_mount_path: str | None = None
  53. workspace_mount_path_in_sandbox: str = '/workspace'
  54. workspace_mount_rewrite: str | None = None
  55. cache_dir: str = '/tmp/cache'
  56. run_as_openhands: bool = True
  57. max_iterations: int = OH_MAX_ITERATIONS
  58. max_budget_per_task: float | None = None
  59. e2b_api_key: str = ''
  60. modal_api_token_id: str = ''
  61. modal_api_token_secret: str = ''
  62. disable_color: bool = False
  63. jwt_secret: str = ''
  64. debug: bool = False
  65. file_uploads_max_file_size_mb: int = 0
  66. file_uploads_restrict_file_types: bool = False
  67. file_uploads_allowed_extensions: list[str] = field(default_factory=lambda: ['.*'])
  68. runloop_api_key: str | None = None
  69. defaults_dict: ClassVar[dict] = {}
  70. def get_llm_config(self, name='llm') -> LLMConfig:
  71. """'llm' is the name for default config (for backward compatibility prior to 0.8)."""
  72. if name in self.llms:
  73. return self.llms[name]
  74. if name is not None and name != 'llm':
  75. logger.openhands_logger.warning(
  76. f'llm config group {name} not found, using default config'
  77. )
  78. if 'llm' not in self.llms:
  79. self.llms['llm'] = LLMConfig()
  80. return self.llms['llm']
  81. def set_llm_config(self, value: LLMConfig, name='llm') -> None:
  82. self.llms[name] = value
  83. def get_agent_config(self, name='agent') -> AgentConfig:
  84. """'agent' is the name for default config (for backward compatibility prior to 0.8)."""
  85. if name in self.agents:
  86. return self.agents[name]
  87. if 'agent' not in self.agents:
  88. self.agents['agent'] = AgentConfig()
  89. return self.agents['agent']
  90. def set_agent_config(self, value: AgentConfig, name='agent') -> None:
  91. self.agents[name] = value
  92. def get_agent_to_llm_config_map(self) -> dict[str, LLMConfig]:
  93. """Get a map of agent names to llm configs."""
  94. return {name: self.get_llm_config_from_agent(name) for name in self.agents}
  95. def get_llm_config_from_agent(self, name='agent') -> LLMConfig:
  96. agent_config: AgentConfig = self.get_agent_config(name)
  97. llm_config_name = agent_config.llm_config
  98. return self.get_llm_config(llm_config_name)
  99. def get_agent_configs(self) -> dict[str, AgentConfig]:
  100. return self.agents
  101. def __post_init__(self):
  102. """Post-initialization hook, called when the instance is created with only default values."""
  103. AppConfig.defaults_dict = self.defaults_to_dict()
  104. def defaults_to_dict(self) -> dict:
  105. """Serialize fields to a dict for the frontend, including type hints, defaults, and whether it's optional."""
  106. result = {}
  107. for f in fields(self):
  108. field_value = getattr(self, f.name)
  109. # dataclasses compute their defaults themselves
  110. if is_dataclass(type(field_value)):
  111. result[f.name] = field_value.defaults_to_dict()
  112. else:
  113. result[f.name] = get_field_info(f)
  114. return result
  115. def __str__(self):
  116. attr_str = []
  117. for f in fields(self):
  118. attr_name = f.name
  119. attr_value = getattr(self, f.name)
  120. if attr_name in [
  121. 'e2b_api_key',
  122. 'github_token',
  123. 'jwt_secret',
  124. 'modal_api_token_id',
  125. 'modal_api_token_secret',
  126. 'runloop_api_key',
  127. ]:
  128. attr_value = '******' if attr_value else None
  129. attr_str.append(f'{attr_name}={repr(attr_value)}')
  130. return f"AppConfig({', '.join(attr_str)}"
  131. def __repr__(self):
  132. return self.__str__()