mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-15 17:30:13 +08:00
Update context.py
This commit is contained in:
@@ -55,13 +55,13 @@ class PlatformManagerProtocol(Protocol):
|
||||
|
||||
|
||||
class Context:
|
||||
"""暴露给插件的接口上下文。
|
||||
"""暴露给插件的接口上下文。"""
|
||||
|
||||
Runtime registration containers are instance-local to avoid cross-instance
|
||||
leakage when multiple Context objects are created (e.g. during tests or
|
||||
in-process reinitialization). The legacy class-level attributes were moved
|
||||
into the instance initializer for correctness and compatibility.
|
||||
"""
|
||||
registered_web_apis: list = []
|
||||
|
||||
# 向后兼容的变量
|
||||
_register_tasks: list[Awaitable] = []
|
||||
_star_manager = None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -80,14 +80,6 @@ class Context:
|
||||
) -> None:
|
||||
self._event_queue = event_queue
|
||||
"""事件队列。消息平台通过事件队列传递消息事件。"""
|
||||
|
||||
# Runtime registration containers are instance-local to avoid leakage
|
||||
# across Context instances. Tests and runtime components expect these to
|
||||
# be per-instance.
|
||||
self.registered_web_apis: list = []
|
||||
self._register_tasks: list = []
|
||||
self._star_manager = None
|
||||
|
||||
self._config = config
|
||||
"""AstrBot 默认配置"""
|
||||
self._db = db
|
||||
@@ -139,7 +131,6 @@ class Context:
|
||||
Raises:
|
||||
ChatProviderNotFoundError: If the specified chat provider ID is not found
|
||||
Exception: For other errors during LLM generation
|
||||
|
||||
"""
|
||||
prov = await self.provider_manager.get_provider_by_id(chat_provider_id)
|
||||
if not prov or not isinstance(prov, Provider):
|
||||
@@ -197,7 +188,6 @@ class Context:
|
||||
Raises:
|
||||
ChatProviderNotFoundError: If the specified chat provider ID is not found
|
||||
Exception: For other errors during LLM generation
|
||||
|
||||
"""
|
||||
# Import here to avoid circular imports
|
||||
from astrbot.core.astr_agent_context import (
|
||||
@@ -245,12 +235,10 @@ class Context:
|
||||
}
|
||||
if request.func_tool and request.func_tool.get_tool("astrbot_file_read_tool"):
|
||||
other_kwargs.setdefault(
|
||||
"tool_result_overflow_dir",
|
||||
get_astrbot_system_tmp_path(),
|
||||
"tool_result_overflow_dir", get_astrbot_system_tmp_path()
|
||||
)
|
||||
other_kwargs.setdefault(
|
||||
"read_tool",
|
||||
request.func_tool.get_tool("astrbot_file_read_tool"),
|
||||
"read_tool", request.func_tool.get_tool("astrbot_file_read_tool")
|
||||
)
|
||||
|
||||
await agent_runner.reset(
|
||||
@@ -283,7 +271,6 @@ class Context:
|
||||
|
||||
Raises:
|
||||
ProviderNotFoundError: 未找到。
|
||||
|
||||
"""
|
||||
prov = self.get_using_provider(umo)
|
||||
if not prov:
|
||||
@@ -315,7 +302,6 @@ class Context:
|
||||
|
||||
Note:
|
||||
注册的工具默认是激活状态。
|
||||
|
||||
"""
|
||||
return self.provider_manager.llm_tools.activate_llm_tool(name, star_map)
|
||||
|
||||
@@ -327,7 +313,6 @@ class Context:
|
||||
|
||||
Returns:
|
||||
如果成功停用返回 True,如果没找到工具返回 False。
|
||||
|
||||
"""
|
||||
return self.provider_manager.llm_tools.deactivate_llm_tool(name)
|
||||
|
||||
@@ -347,12 +332,11 @@ class Context:
|
||||
|
||||
Note:
|
||||
如果提供者 ID 存在但未找到提供者,会记录警告日志。
|
||||
|
||||
"""
|
||||
prov = self.provider_manager.inst_map.get(provider_id)
|
||||
if provider_id and not prov:
|
||||
logger.warning(
|
||||
f"没有找到 ID 为 {provider_id} 的提供商,这可能是由于您修改了提供商(模型)ID 导致的。",
|
||||
f"没有找到 ID 为 {provider_id} 的提供商,这可能是由于您修改了提供商(模型)ID 导致的。"
|
||||
)
|
||||
return prov
|
||||
|
||||
@@ -384,7 +368,6 @@ class Context:
|
||||
|
||||
Raises:
|
||||
ValueError: 该会话来源配置的的对话模型(提供商)的类型不正确。
|
||||
|
||||
"""
|
||||
prov = self.provider_manager.get_using_provider(
|
||||
provider_type=ProviderType.CHAT_COMPLETION,
|
||||
@@ -394,7 +377,7 @@ class Context:
|
||||
return None
|
||||
if not isinstance(prov, Provider):
|
||||
raise ValueError(
|
||||
f"该会话来源的对话模型(提供商)的类型不正确: {type(prov)}",
|
||||
f"该会话来源的对话模型(提供商)的类型不正确: {type(prov)}"
|
||||
)
|
||||
return prov
|
||||
|
||||
@@ -409,7 +392,6 @@ class Context:
|
||||
|
||||
Raises:
|
||||
ValueError: 返回的提供者不是 TTSProvider 类型。
|
||||
|
||||
"""
|
||||
prov = self.provider_manager.get_using_provider(
|
||||
provider_type=ProviderType.TEXT_TO_SPEECH,
|
||||
@@ -430,7 +412,6 @@ class Context:
|
||||
|
||||
Raises:
|
||||
ValueError: 返回的提供者不是 STTProvider 类型。
|
||||
|
||||
"""
|
||||
prov = self.provider_manager.get_using_provider(
|
||||
provider_type=ProviderType.SPEECH_TO_TEXT,
|
||||
@@ -451,7 +432,6 @@ class Context:
|
||||
|
||||
Note:
|
||||
如果不提供 umo 参数,将返回默认配置。
|
||||
|
||||
"""
|
||||
if not umo:
|
||||
# 使用默认配置
|
||||
@@ -478,7 +458,6 @@ class Context:
|
||||
Note:
|
||||
当 session 为字符串时,会尝试解析为 MessageSession 对象。(类名为MessageSesion是因为历史遗留拼写错误)
|
||||
qq_official(QQ 官方 API 平台) 不支持此方法。
|
||||
|
||||
"""
|
||||
if isinstance(session, str):
|
||||
try:
|
||||
@@ -491,7 +470,7 @@ class Context:
|
||||
await platform.send_by_session(session, message_chain)
|
||||
return True
|
||||
logger.warning(
|
||||
f"cannot find platform for session {session!s}, message not sent",
|
||||
f"cannot find platform for session {str(session)}, message not sent"
|
||||
)
|
||||
return False
|
||||
|
||||
@@ -503,7 +482,6 @@ class Context:
|
||||
|
||||
Note:
|
||||
如果工具已存在,会替换已存在的工具。
|
||||
|
||||
"""
|
||||
tool_name = {tool.name for tool in self.provider_manager.llm_tools.func_list}
|
||||
module_path = ""
|
||||
@@ -523,7 +501,7 @@ class Context:
|
||||
else:
|
||||
tool.handler_module_path = module_path
|
||||
logger.info(
|
||||
f"plugin(module_path {module_path}) added LLM tool: {tool.name}",
|
||||
f"plugin(module_path {module_path}) added LLM tool: {tool.name}"
|
||||
)
|
||||
|
||||
if tool.name in tool_name:
|
||||
@@ -548,7 +526,6 @@ class Context:
|
||||
|
||||
Note:
|
||||
如果相同路由和方法已注册,会替换现有的 API。
|
||||
|
||||
"""
|
||||
for idx, api in enumerate(self.registered_web_apis):
|
||||
if api[0] == route and methods == api[2]:
|
||||
@@ -576,7 +553,6 @@ class Context:
|
||||
|
||||
Note:
|
||||
该方法已经过时,请使用 get_platform_inst 方法。(>= AstrBot v4.0.0)
|
||||
|
||||
"""
|
||||
for platform in self.platform_manager.platform_insts:
|
||||
name = platform.meta().name
|
||||
@@ -600,7 +576,6 @@ class Context:
|
||||
|
||||
Note:
|
||||
可以通过 event.get_platform_id() 获取平台 ID。
|
||||
|
||||
"""
|
||||
for platform in self.platform_manager.platform_insts:
|
||||
if platform.meta().id == platform_id:
|
||||
@@ -611,7 +586,6 @@ class Context:
|
||||
|
||||
Returns:
|
||||
数据库实例。
|
||||
|
||||
"""
|
||||
return self._db
|
||||
|
||||
@@ -620,7 +594,6 @@ class Context:
|
||||
|
||||
Args:
|
||||
provider: 提供者实例。
|
||||
|
||||
"""
|
||||
self.provider_manager.provider_insts.append(provider)
|
||||
|
||||
@@ -643,7 +616,6 @@ class Context:
|
||||
Note:
|
||||
异步处理函数会接收到额外的关键词参数:event: AstrMessageEvent, context: Context。
|
||||
该方法已弃用,请使用新的注册方式。
|
||||
|
||||
"""
|
||||
md = StarHandlerMetadata(
|
||||
event_type=EventType.OnLLMRequestEvent,
|
||||
@@ -666,7 +638,6 @@ class Context:
|
||||
Note:
|
||||
如果再要启用,需要重新注册。
|
||||
该方法已弃用。
|
||||
|
||||
"""
|
||||
self.provider_manager.llm_tools.remove_func(name)
|
||||
|
||||
@@ -693,7 +664,6 @@ class Context:
|
||||
|
||||
Note:
|
||||
推荐使用装饰器注册指令。该方法将在未来的版本中被移除。
|
||||
|
||||
"""
|
||||
md = StarHandlerMetadata(
|
||||
event_type=EventType.AdapterMessageEvent,
|
||||
@@ -721,6 +691,5 @@ class Context:
|
||||
|
||||
Note:
|
||||
该方法已弃用。
|
||||
|
||||
"""
|
||||
self._register_tasks.append(task)
|
||||
|
||||
Reference in New Issue
Block a user