Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.6 KB
Raw Blame
import logging
import os
from collections.abc import Mapping
from typing import Any, override

from pydantic.fields import FieldInfo

from configs.remote_settings_sources.base import RemoteSettingsSource

from .http_request import NacosHttpClient
from .utils import parse_config

logger = logging.getLogger(__name__)


class NacosSettingsSource(RemoteSettingsSource):
    def __init__(self, configs: Mapping[str, Any]):
        self.configs = configs
        self.remote_configs: dict[str, str] = {}
        self.async_init()

    def async_init(self) -> None:
        data_id = os.getenv("DIFY_ENV_NACOS_DATA_ID", "dify-api-env.properties")
        group = os.getenv("DIFY_ENV_NACOS_GROUP", "nacos-dify")
        tenant = os.getenv("DIFY_ENV_NACOS_NAMESPACE", "")

        params = {"dataId": data_id, "group": group, "tenant": tenant}
        try:
            content = NacosHttpClient().http_request("/nacos/v1/cs/configs", method="GET", headers={}, params=params)
            self.remote_configs = self._parse_config(content)
        except Exception:
            logger.exception("[get-access-token] exception occurred")
            raise

    def _parse_config(self, content: str) -> dict[str, str]:
        if not content:
            return {}
        try:
            return parse_config(content)
        except Exception as e:
            raise RuntimeError(f"Failed to parse config: {e}") from e

    @override
    def get_field_value(self, field: FieldInfo, field_name: str) -> tuple[Any, str, bool]:
        return self.resolve_field_value(self.remote_configs, field, field_name)