| 1 | from agent import AgentConfig |
| 2 | from helpers import runtime, settings, defer, extension |
| 3 | from helpers.print_style import PrintStyle |
| 4 | |
| 5 | |
| 6 | @extension.extensible |
| 7 | def initialize_agent(override_settings: dict | None = None): |
| 8 | current_settings = settings.get_settings() |
| 9 | if override_settings: |
| 10 | current_settings = settings.merge_settings(current_settings, override_settings) |
| 11 | |
| 12 | # agent configuration - models are now resolved at call time via _model_config plugin |
| 13 | config = AgentConfig( |
| 14 | profile=current_settings["agent_profile"], |
| 15 | knowledge_subdirs=[current_settings["agent_knowledge_subdir"], "default"], |
| 16 | mcp_servers=current_settings["mcp_servers"], |
| 17 | ) |
| 18 | |
| 19 | # update config with runtime args |
| 20 | _args_override(config) |
| 21 | |
| 22 | # initialize MCP in deferred task to prevent blocking the main thread |
| 23 | # async def initialize_mcp_async(mcp_servers_config: str): |
| 24 | # return initialize_mcp(mcp_servers_config) |
| 25 | # defer.DeferredTask(thread_name="mcp-initializer").start_task(initialize_mcp_async, config.mcp_servers) |
| 26 | # initialize_mcp(config.mcp_servers) |
| 27 | |
| 28 | # import helpers.mcp_handler as mcp_helper |
| 29 | # import agent as agent_helper |
| 30 | # import helpers.print_style as print_style_helper |
| 31 | # if not mcp_helper.MCPConfig.get_instance().is_initialized(): |
| 32 | # try: |
| 33 | # mcp_helper.MCPConfig.update(config.mcp_servers) |
| 34 | # except Exception as e: |
| 35 | # first_context = agent_helper.AgentContext.first() |
| 36 | # if first_context: |
| 37 | # ( |
| 38 | # first_context.log |
| 39 | # .log(type="warning", content=f"Failed to update MCP settings: {e}") |
| 40 | # ) |
| 41 | # ( |
| 42 | # print_style_helper.PrintStyle(background_color="black", font_color="red", padding=True) |
| 43 | # .print(f"Failed to update MCP settings: {e}") |
| 44 | # ) |
| 45 | |
| 46 | # return config object |
| 47 | return config |
| 48 | |
| 49 | @extension.extensible |
| 50 | def initialize_chats(): |
| 51 | from helpers import persist_chat |
| 52 | async def initialize_chats_async(): |
| 53 | persist_chat.load_tmp_chats() |
| 54 | return defer.DeferredTask().start_task(initialize_chats_async) |
| 55 | |
| 56 | @extension.extensible |
| 57 | def initialize_mcp(): |
| 58 | set = settings.get_settings() |
| 59 | async def initialize_mcp_async(): |
| 60 | from helpers.mcp_handler import initialize_mcp as _initialize_mcp |
| 61 | return _initialize_mcp(set["mcp_servers"]) |
| 62 | return defer.DeferredTask().start_task(initialize_mcp_async) |
| 63 | |
| 64 | @extension.extensible |
| 65 | def initialize_job_loop(): |
| 66 | from helpers.job_loop import run_loop |
| 67 | return defer.DeferredTask("JobLoop").start_task(run_loop) |
| 68 | |
| 69 | @extension.extensible |
| 70 | def initialize_preload(): |
| 71 | import preload |
| 72 | return defer.DeferredTask().start_task(preload.preload) |
| 73 | |
| 74 | @extension.extensible |
| 75 | def initialize_migration(): |
| 76 | from helpers import migration, dotenv |
| 77 | # run migration |
| 78 | migration.startup_migration() |
| 79 | # reload .env as it might have been moved |
| 80 | dotenv.load_dotenv() |
| 81 | # reload settings to ensure new paths are picked up |
| 82 | settings.reload_settings() |
| 83 | |
| 84 | def _args_override(config): |
| 85 | # update config with runtime args |
| 86 | for key, value in runtime.args.items(): |
| 87 | if hasattr(config, key): |
| 88 | # conversion based on type of config[key] |
| 89 | if isinstance(getattr(config, key), bool): |
| 90 | value = value.lower().strip() == "true" |
| 91 | elif isinstance(getattr(config, key), int): |
| 92 | value = int(value) |
| 93 | elif isinstance(getattr(config, key), float): |
| 94 | value = float(value) |
| 95 | elif isinstance(getattr(config, key), str): |
| 96 | value = str(value) |
| 97 | else: |
| 98 | raise Exception( |
| 99 | f"Unsupported argument type of '{key}': {type(getattr(config, key))}" |
| 100 | ) |
| 101 | |
| 102 | setattr(config, key, value) |
| 103 | |
| 104 | |
| 105 |