-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensionLoader.py
More file actions
96 lines (82 loc) · 3 KB
/
Copy pathextensionLoader.py
File metadata and controls
96 lines (82 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import importlib
import os
from colorama import Fore
from typing import List,Any
from chatContent import ChatContent
from localization import t
import logging
extensions=[]
extension_names=[]
warn=False
os.makedirs('Extensions',exist_ok=True)
print()
print()
print()
print()
for mod in os.listdir('Extensions'):
if mod.endswith('.py'):
if not warn:
warn=True
print(f"{Fore.YELLOW}{t('extension.warning_risky')}{Fore.RESET}")
print(f"{t('extension.loading')}\t{mod}",end='\r')
try:
extension_names.append(mod[:-3])
extensions.append(importlib.import_module('Extensions.'+mod[:-3]))
print(f"{Fore.GREEN}_{t('extension.loaded')}\t\t{mod}{Fore.RESET}\t{extensions[-1].description.replace('\n','')}")
except Exception as e:
print(f'{Fore.YELLOW}{t("extension.load_error")}\t{mod}\t{e}{Fore.RESET}')
print()
print()
print()
print()
def callEveryExtension(func_name,*args,**kwargs)->Any:
result=args
for mod_name,mod in zip(extension_names,extensions):
try:
func=getattr(mod,func_name)
# 检查获取的对象是否为可调用函数
if callable(func):
# 判断result是否可以解包
if isinstance(result, (list, tuple)):
result2=func(*result,**kwargs)
else:
result2=func(result,**kwargs)
if result2 is not None:
result=result2
logging.debug(f'{mod_name}.{func_name} called successfully')
else:
logging.warning(f'{mod_name}.{func_name} is not callable')
except AttributeError as e:
# 忽略没有该函数的模块,这不是错误情况
logging.debug(f'Function {func_name} not found in {mod_name}, skipping')
except Exception as e:
logging.error(f'Error calling {mod_name}.{func_name}: {e}',exc_info=True)
return result
if __name__=='__main__':
callEveryExtension('after_receiving_messages',[ChatContent('',[],'','',True)])
callEveryExtension('after_screenshot')
callEveryExtension('before_sending_the_message_by_AI_generated','1')
callEveryExtension('after_screenshot')
# def after_receiving_messages(messages: List[ChatContent]) -> None:
# # 信息被处理后调用
# # 参数
# # messages : 收到的信息
# pass
# def before_sending_the_message_by_AI_generated(answer:str) -> str:
# # 信息被发送前调用
# # 参数
# # answer : 获取的答案
# # 返回值
# # 对答案的修改,直接返回答案则无修改
# # 必须有返回值
# return answer
# def after_screenshot() -> None:
# # 截图后调用
# # 截图保存在当前目录下的screenshot.bmp
# pass
# for mod_name,mod in zip(mod_names,mods):
# print(mod_name)
# print(mod.description)
# mod.after_receiving_messages(None)
# mod.after_screenshot()
# mod.before_sending_the_message_by_AI_generated('')