66
77def _get_env_var (name : str , default : any = "" ) -> any :
88 value = os .getenv (name , "" )
9- # print(f"_get_env_var: {name} = {value}. default = {default}")
109 if value == "" and default != "" :
1110 value = default
12- # print(f"Debug: {name} not found. Setting to default {str(default)}")
13-
1411 return value
1512
1613class ModuleOptions :
@@ -30,10 +27,6 @@ def getEnvVariable(name: str, default: any = "") -> any:
3027 return _get_env_var (name , default )
3128
3229
33- # Needed in a moment...
34- current_working_dir = os .getcwd ()
35- current_dir_name = os .path .basename (current_working_dir )
36-
3730 # Settings
3831
3932 # Was this module launched by the server or launched externally (eg by a debugger)
@@ -43,18 +36,50 @@ def getEnvVariable(name: str, default: any = "") -> any:
4336 python_dir = os .path .dirname (sys .executable )
4437
4538 # Path to the current module
46- module_path = os .path .normpath (_get_env_var ("CPAI_MODULE_PATH" , current_working_dir ))
39+ current_file_path = os .path .dirname (__file__ )
40+ module_path = os .path .normpath (_get_env_var ("CPAI_MODULE_PATH" , os .getcwd ()))
4741
4842 # These can be read from the modulesettings.json file if we're keen
49- module_id = _get_env_var ("CPAI_MODULE_ID" , current_dir_name )
50- module_name = _get_env_var ("CPAI_MODULE_NAME" , module_id )
43+ module_id = _get_env_var ("CPAI_MODULE_ID" , None )
44+ module_name = _get_env_var ("CPAI_MODULE_NAME" , None )
45+
46+ # If module ID and name weren't set, get it from the modulesettings file
47+ if not module_id or not module_name :
48+ try :
49+ with open (os .path .join (module_path , "modulesettings.json" )) as json_file :
50+ # data = json.load(json_file)
51+ file_data = json_file .read ()
52+ import commentjson
53+ data = commentjson .loads (file_data )
54+
55+ module_id = list (data ["Modules" ].keys ())[0 ]
56+ module_name = list (data ["Modules" ].values ())[0 ]["Name" ]
57+ except Exception as ex :
58+ print (f"Unable to read module info in { module_path } " )
59+ module_id = os .path .basename (module_path )
60+ module_name = module_id
61+
5162 queue_name = _get_env_var ("CPAI_MODULE_QUEUENAME" , module_id .lower () + "_queue" )
5263
5364 # Port the server listens on, both for clients and for backend modules
5465 port = _get_env_var ("CPAI_PORT" , "32168" )
5566
5667 # The path to the root folder containing the application
57- server_root_path = _get_env_var ("CPAI_APPROOTPATH" , os .path .normpath (os .path .join (os .path .dirname (__file__ ), "../../.." )))
68+ server_root_path = _get_env_var ("CPAI_APPROOTPATH" , None )
69+
70+ # If path root not set, we start taking wild guesses
71+ if not server_root_path :
72+ root_path = os .path .normpath (os .path .join (module_path , "../../.." )) # root/src/modules/module
73+ if root_path .lower ().endswith ("codeproject.ai-server" ):
74+ server_root_path = root_path
75+ else :
76+ root_path = os .path .normpath (os .path .join (module_path , "../.." )) # root/modules/module
77+ if root_path .lower ().endswith ("codeproject.ai-server" ):
78+ server_root_path = root_path
79+ else :
80+ root_path = os .path .normpath (os .path .join (module_path , ".." )) # CodeProject.AI-Modules/module
81+ if root_path .lower ().endswith ("codeproject.ai-modules" ):
82+ server_root_path = os .path .normpath (os .path .join (module_path , "../../CodeProject.AI-Server" ))
5883
5984 # How many tasks to spin up for a module
6085 parallelism = _get_env_var ("CPAI_MODULE_PARALLELISM" , "0" );
0 commit comments