|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +from v2.nacos import ClientConfig, NacosConfigService |
| 5 | +from v2.nacos.config.model.config_param import ConfigParam |
| 6 | + |
| 7 | +from veadk.agent import Agent |
| 8 | +from veadk.consts import DEFAULT_NACOS_GROUP |
| 9 | +from veadk.utils.logger import get_logger |
| 10 | + |
| 11 | +logger = get_logger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class DynamicConfigManager: |
| 15 | + """ |
| 16 | + DynamicConfigManager is responsible for creating and publishing dynamic config to nacos. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, agents: list[Agent] | Agent, app_name: str): |
| 20 | + """ |
| 21 | + Initialize DynamicConfigManager with agents and app_name. |
| 22 | +
|
| 23 | + Args: |
| 24 | + agents (list[Agent] | Agent): The agent(s) to be included in the dynamic config. |
| 25 | + app_name (str): The name of the application, used as the Nacos group id. Defaults to DEFAULT_NACOS_GROUP. |
| 26 | + """ |
| 27 | + if isinstance(agents, list): |
| 28 | + self.agents = agents |
| 29 | + else: |
| 30 | + self.agents = [agents] |
| 31 | + |
| 32 | + if not app_name: |
| 33 | + logger.warning( |
| 34 | + f"app_name is not provided, use default value {DEFAULT_NACOS_GROUP}. This may lead to unexpected behavior such as configuration override." |
| 35 | + ) |
| 36 | + self.app_name = app_name or DEFAULT_NACOS_GROUP |
| 37 | + |
| 38 | + logger.debug( |
| 39 | + f"DynamicConfigManager init with {len(self.agents)} agent(s) for app {self.app_name}" |
| 40 | + ) |
| 41 | + |
| 42 | + async def create_config(self, config: dict = {}): |
| 43 | + client_config = ClientConfig( |
| 44 | + server_addresses=os.getenv("NACOS_SERVER_ADDRESSES"), |
| 45 | + namespace_id="", |
| 46 | + username=os.getenv("NACOS_USERNAME"), |
| 47 | + password=os.getenv("NACOS_PASSWORD"), |
| 48 | + ) |
| 49 | + |
| 50 | + config_client = await NacosConfigService.create_config_service( |
| 51 | + client_config=client_config |
| 52 | + ) |
| 53 | + |
| 54 | + configs = { |
| 55 | + "agent": [ |
| 56 | + { |
| 57 | + "id": agent.id, |
| 58 | + "name": agent.name, |
| 59 | + "description": agent.description, |
| 60 | + "model_name": agent.model_name, |
| 61 | + "instruction": agent.instruction, |
| 62 | + } |
| 63 | + for agent in self.agents |
| 64 | + ] |
| 65 | + } |
| 66 | + response = await config_client.publish_config( |
| 67 | + param=ConfigParam( |
| 68 | + data_id="veadk", |
| 69 | + group=self.app_name, |
| 70 | + type="json", |
| 71 | + content=json.dumps(configs), |
| 72 | + ) |
| 73 | + ) |
| 74 | + assert response, "publish config to nacos failed" |
| 75 | + logger.info("Publish config to nacos success") |
| 76 | + |
| 77 | + await config_client.add_listener( |
| 78 | + data_id="veadk", |
| 79 | + group="VEADK_GROUP", |
| 80 | + listener=self.handle_config_update, |
| 81 | + ) |
| 82 | + logger.info("Add config listener to nacos success") |
| 83 | + |
| 84 | + return config_client |
| 85 | + |
| 86 | + def register_agent(self, agent: list[Agent] | Agent): |
| 87 | + if isinstance(agent, list): |
| 88 | + self.agents.extend(agent) |
| 89 | + else: |
| 90 | + self.agents.append(agent) |
| 91 | + |
| 92 | + def update_agent(self, configs: dict): |
| 93 | + for agent in self.agents: |
| 94 | + for config in configs["agent"]: |
| 95 | + if agent.id == config["id"]: |
| 96 | + logger.info(f"Update agent {agent.id} with config {config}") |
| 97 | + name = config["name"] |
| 98 | + description = config["description"] |
| 99 | + model_name = config["model_name"] |
| 100 | + instruction = config["instruction"] |
| 101 | + |
| 102 | + agent.name = name |
| 103 | + agent.description = description |
| 104 | + if model_name != agent.model_name: |
| 105 | + agent.update_model(model_name=model_name) |
| 106 | + agent.instruction = instruction |
| 107 | + |
| 108 | + async def handle_config_update(self, tenant, data_id, group, content) -> None: |
| 109 | + logger.debug( |
| 110 | + "listen, tenant:{} data_id:{} group:{} content:{}".format( |
| 111 | + tenant, data_id, group, content |
| 112 | + ) |
| 113 | + ) |
| 114 | + content = json.loads(content) |
| 115 | + self.update_agent(content) |
0 commit comments