mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-16 17:47:06 +08:00
# Conflicts: # AGENTS.md # astrbot/__init__.py # astrbot/cli/utils/__init__.py # astrbot/core/astr_agent_tool_exec.py # astrbot/core/computer/booters/local.py # astrbot/core/cron/manager.py # astrbot/core/knowledge_base/kb_mgr.py # astrbot/core/knowledge_base/retrieval/manager.py # astrbot/core/platform/sources/discord/discord_platform_adapter.py # astrbot/core/platform/sources/qqofficial/qqofficial_message_event.py # astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py # astrbot/core/platform/sources/qqofficial_webhook/qo_webhook_server.py # astrbot/core/platform/sources/wecom/wecom_adapter.py # astrbot/core/platform/sources/wecom_ai_bot/wecomai_adapter.py # astrbot/core/provider/func_tool_manager.py # astrbot/core/provider/sources/mimo_api_common.py # astrbot/core/star/star_manager.py # astrbot/core/tools/computer_tools/fs.py # astrbot/core/tools/computer_tools/python.py # astrbot/core/tools/computer_tools/shell.py # astrbot/core/tools/message_tools.py # astrbot/core/tools/web_search_tools.py # astrbot/core/utils/io.py # astrbot/core/utils/tencent_record_helper.py # dashboard/package.json # dashboard/src/assets/mdi-subset/materialdesignicons-subset.css # dashboard/src/assets/mdi-subset/materialdesignicons-webfont-subset.woff # dashboard/src/assets/mdi-subset/materialdesignicons-webfont-subset.woff2 # dashboard/src/components/ConfirmDialog.vue # dashboard/src/components/chat/Chat.vue # dashboard/src/components/chat/ChatInput.vue # dashboard/src/components/chat/ChatMessageList.vue # dashboard/src/components/chat/ConfigSelector.vue # dashboard/src/components/chat/MessageList.vue # dashboard/src/components/chat/ProjectDialog.vue # dashboard/src/components/chat/ProjectList.vue # dashboard/src/components/chat/ProjectView.vue # dashboard/src/components/chat/ProviderModelMenu.vue # dashboard/src/components/chat/RegenerateMenu.vue # dashboard/src/components/chat/StandaloneChat.vue # dashboard/src/components/chat/ThreadPanel.vue # dashboard/src/components/chat/ThreadedMarkdownMessagePart.vue # dashboard/src/components/chat/message_list_comps/MarkdownMessagePart.vue # dashboard/src/components/chat/message_list_comps/ReasoningTimeline.vue # dashboard/src/components/chat/message_list_comps/RefsSidebar.vue # dashboard/src/components/config/UnsavedChangesConfirmDialog.vue # dashboard/src/components/extension/MarketPluginCard.vue # dashboard/src/components/extension/McpServersSection.vue # dashboard/src/components/extension/SkillsSection.vue # dashboard/src/components/extension/componentPanel/components/DetailsDialog.vue # dashboard/src/components/extension/componentPanel/components/RenameDialog.vue # dashboard/src/components/folder/BaseCreateFolderDialog.vue # dashboard/src/components/folder/BaseFolderItemSelector.vue # dashboard/src/components/folder/BaseMoveToFolderDialog.vue # dashboard/src/components/platform/AddNewPlatform.vue # dashboard/src/components/provider/AddNewProvider.vue # dashboard/src/components/provider/ProviderModelsPanel.vue # dashboard/src/components/shared/BackupDialog.vue # dashboard/src/components/shared/ChangelogDialog.vue # dashboard/src/components/shared/ExtensionCard.vue # dashboard/src/components/shared/FileConfigItem.vue # dashboard/src/components/shared/KnowledgeBaseSelector.vue # dashboard/src/components/shared/ListConfigItem.vue # dashboard/src/components/shared/ObjectEditor.vue # dashboard/src/components/shared/PersonaForm.vue # dashboard/src/components/shared/PluginSetSelector.vue # dashboard/src/components/shared/ProviderSelector.vue # dashboard/src/components/shared/ReadmeDialog.vue # dashboard/src/components/shared/SidebarCustomizer.vue # dashboard/src/components/shared/T2ITemplateEditor.vue # dashboard/src/components/shared/UninstallConfirmDialog.vue # dashboard/src/components/shared/WaitingForRestart.vue # dashboard/src/composables/useMessages.ts # dashboard/src/composables/useProjects.ts # dashboard/src/composables/useProviderModelConfigDialog.ts # dashboard/src/composables/useProviderSources.ts # dashboard/src/i18n/locales/ru-RU/features/config-metadata.json # dashboard/src/i18n/locales/ru-RU/features/extension.json # dashboard/src/i18n/locales/ru-RU/features/provider.json # dashboard/src/layouts/full/vertical-header/VerticalHeader.vue # dashboard/src/scss/_override.scss # dashboard/src/stores/common.js # dashboard/src/stores/customizer.ts # dashboard/src/views/ConfigPage.vue # dashboard/src/views/ConversationPage.vue # dashboard/src/views/PlatformPage.vue # dashboard/src/views/SessionManagementPage.vue # dashboard/src/views/WelcomePage.vue # dashboard/src/views/alkaid/KnowledgeBase.vue # dashboard/src/views/alkaid/LongTermMemory.vue # dashboard/src/views/authentication/auth/LoginPage.vue # dashboard/src/views/extension/InstalledPluginsTab.vue # dashboard/src/views/extension/useExtensionPage.js # dashboard/src/views/knowledge-base/DocumentDetail.vue # dashboard/src/views/knowledge-base/KBList.vue # dashboard/src/views/knowledge-base/components/DocumentsTab.vue # dashboard/src/views/knowledge-base/components/SettingsTab.vue # dashboard/src/views/knowledge-base/components/TavilyKeyDialog.vue # dashboard/src/views/persona/FolderTree.vue # dashboard/src/views/persona/MoveToFolderDialog.vue # dashboard/src/views/persona/PersonaManager.vue # requirements.txt # tests/test_local_shell_component.py # tests/unit/test_func_tool_manager.py # tests/unit/test_web_search_tools.py
219 lines
7.8 KiB
Python
219 lines
7.8 KiB
Python
import os
|
|
import zipfile
|
|
|
|
import yaml
|
|
|
|
from astrbot.core import logger
|
|
from astrbot.core.star.star import StarMetadata
|
|
from astrbot.core.utils.astrbot_path import get_astrbot_plugin_path
|
|
from astrbot.core.utils.io import ensure_dir, remove_dir
|
|
from astrbot.core.zip_updator import RepoZipUpdator
|
|
|
|
PLUGIN_METADATA_FILENAMES = ("metadata.yaml", "metadata.yml")
|
|
PLUGIN_METADATA_REQUIRED_FIELDS = ("name", "desc", "version", "author")
|
|
|
|
|
|
class PluginUpdator(RepoZipUpdator):
|
|
def __init__(self, repo_mirror: str = "", verify: str | bool | None = None) -> None:
|
|
super().__init__(repo_mirror, verify=verify)
|
|
self.plugin_store_path = get_astrbot_plugin_path()
|
|
|
|
def get_plugin_store_path(self) -> str:
|
|
return self.plugin_store_path
|
|
|
|
async def install(self, repo_url: str, proxy="", download_url: str = "") -> str:
|
|
_, repo_name, _ = self.parse_github_url(repo_url)
|
|
repo_name = self.format_name(repo_name)
|
|
plugin_path = os.path.join(self.plugin_store_path, repo_name)
|
|
if download_url:
|
|
logger.info(f"Downloading plugin archive for {repo_name}: {download_url}")
|
|
await self._download_file(download_url, plugin_path + ".zip")
|
|
else:
|
|
await self.download_from_repo_url(plugin_path, repo_url, proxy)
|
|
self.unzip_file(plugin_path + ".zip", plugin_path)
|
|
|
|
return plugin_path
|
|
|
|
async def update(
|
|
self,
|
|
plugin: StarMetadata,
|
|
proxy="",
|
|
download_url: str = "",
|
|
) -> str:
|
|
repo_url = plugin.repo
|
|
|
|
if not repo_url and not download_url:
|
|
raise Exception(
|
|
f"Plugin {plugin.name} does not specify a repository URL or download URL.",
|
|
)
|
|
|
|
if not plugin.root_dir_name:
|
|
raise Exception(
|
|
f"Plugin {plugin.name} does not specify a root directory name.",
|
|
)
|
|
|
|
plugin_path = os.path.join(self.plugin_store_path, plugin.root_dir_name)
|
|
|
|
logger.info(
|
|
f"Updating plugin at path: {plugin_path}, repository URL: {repo_url}",
|
|
)
|
|
if download_url:
|
|
logger.info(
|
|
f"Downloading plugin update archive for {plugin.name}: {download_url}",
|
|
)
|
|
await self._download_file(download_url, plugin_path + ".zip")
|
|
elif repo_url:
|
|
await self.download_from_repo_url(plugin_path, repo_url, proxy=proxy)
|
|
|
|
self.validate_plugin_archive(plugin_path + ".zip")
|
|
try:
|
|
remove_dir(plugin_path)
|
|
except BaseException as e:
|
|
logger.error(
|
|
f"Failed to remove old plugin directory {plugin_path}: {e!s}; using overwrite installation.",
|
|
)
|
|
|
|
self.unzip_file(plugin_path + ".zip", plugin_path)
|
|
|
|
return plugin_path
|
|
|
|
@classmethod
|
|
def find_plugin_metadata_entry(cls, entries: list[str]) -> str | None:
|
|
"""Find AstrBot plugin metadata in archive entries.
|
|
|
|
Args:
|
|
entries: Zip archive member names.
|
|
|
|
Returns:
|
|
The original archive entry name for plugin metadata, or None.
|
|
"""
|
|
update_dir = cls._resolve_archive_root_dir(entries)
|
|
portable_update_dir = os.path.normpath(update_dir).replace("\\", "/")
|
|
if portable_update_dir == ".":
|
|
portable_update_dir = ""
|
|
|
|
entries_by_portable_path = {}
|
|
for entry in entries:
|
|
portable_entry = os.path.normpath(entry).replace("\\", "/")
|
|
if portable_entry in ("", "."):
|
|
continue
|
|
entries_by_portable_path[portable_entry] = entry
|
|
|
|
metadata_candidates = (
|
|
[
|
|
f"{portable_update_dir}/{filename}"
|
|
for filename in PLUGIN_METADATA_FILENAMES
|
|
]
|
|
if portable_update_dir
|
|
else list(PLUGIN_METADATA_FILENAMES)
|
|
)
|
|
for candidate in metadata_candidates:
|
|
if candidate in entries_by_portable_path:
|
|
return entries_by_portable_path[candidate]
|
|
return None
|
|
|
|
@staticmethod
|
|
def validate_plugin_metadata(metadata: object, metadata_label: str) -> None:
|
|
"""Validate AstrBot plugin metadata content.
|
|
|
|
Args:
|
|
metadata: Parsed metadata YAML content.
|
|
metadata_label: Metadata filename or archive entry for error messages.
|
|
|
|
Raises:
|
|
ValueError: If metadata is malformed or misses required fields.
|
|
"""
|
|
if not isinstance(metadata, dict):
|
|
raise ValueError(f"{metadata_label} 格式错误。")
|
|
|
|
normalized_metadata = dict(metadata)
|
|
if "desc" not in normalized_metadata and "description" in normalized_metadata:
|
|
normalized_metadata["desc"] = normalized_metadata["description"]
|
|
|
|
missing_fields = [
|
|
field
|
|
for field in PLUGIN_METADATA_REQUIRED_FIELDS
|
|
if field not in normalized_metadata
|
|
]
|
|
if missing_fields:
|
|
raise ValueError(
|
|
f"{metadata_label} 中缺少必需字段: {', '.join(missing_fields)}。"
|
|
)
|
|
|
|
invalid_fields = [
|
|
field
|
|
for field in PLUGIN_METADATA_REQUIRED_FIELDS
|
|
if not isinstance(normalized_metadata[field], str)
|
|
or not normalized_metadata[field].strip()
|
|
]
|
|
if invalid_fields:
|
|
raise ValueError(
|
|
f"{metadata_label} 中字段 {', '.join(invalid_fields)} 必须是非空字符串。"
|
|
)
|
|
|
|
@classmethod
|
|
def inspect_plugin_archive(cls, zip_path: str) -> dict[str, object]:
|
|
"""Inspect plugin metadata in an AstrBot plugin archive.
|
|
|
|
Args:
|
|
zip_path: Path to the plugin archive.
|
|
|
|
Returns:
|
|
A dict containing the metadata entry and parsed metadata.
|
|
|
|
Raises:
|
|
ValueError: If the archive is not a valid AstrBot plugin.
|
|
"""
|
|
try:
|
|
with zipfile.ZipFile(zip_path, "r") as z:
|
|
metadata_entry = cls.find_plugin_metadata_entry(z.namelist())
|
|
if metadata_entry is None:
|
|
raise ValueError(
|
|
"压缩包不是合法的 AstrBot 插件:未找到 metadata.yaml 或 metadata.yml。"
|
|
)
|
|
|
|
try:
|
|
metadata_text = z.read(metadata_entry).decode("utf-8")
|
|
metadata = yaml.safe_load(metadata_text)
|
|
except UnicodeDecodeError as exc:
|
|
raise ValueError(f"{metadata_entry} 必须使用 UTF-8 编码。") from exc
|
|
except yaml.YAMLError as exc:
|
|
raise ValueError(f"{metadata_entry} 格式错误。") from exc
|
|
|
|
cls.validate_plugin_metadata(metadata, metadata_entry)
|
|
return {
|
|
"metadata_entry": metadata_entry,
|
|
"metadata": metadata,
|
|
}
|
|
except zipfile.BadZipFile as exc:
|
|
raise ValueError("插件压缩包格式错误。") from exc
|
|
|
|
@classmethod
|
|
def validate_plugin_archive(cls, zip_path: str) -> str:
|
|
"""Validate that an archive contains a valid AstrBot plugin.
|
|
|
|
Args:
|
|
zip_path: Path to the plugin archive.
|
|
|
|
Returns:
|
|
The archive entry name of the plugin metadata file.
|
|
|
|
Raises:
|
|
ValueError: If the archive is not a valid AstrBot plugin.
|
|
"""
|
|
inspection = cls.inspect_plugin_archive(zip_path)
|
|
return str(inspection["metadata_entry"])
|
|
|
|
def unzip_file(self, zip_path: str, target_dir: str) -> None:
|
|
self.validate_plugin_archive(zip_path)
|
|
ensure_dir(target_dir)
|
|
logger.info(f"Extracting archive: {zip_path}")
|
|
with zipfile.ZipFile(zip_path, "r") as z:
|
|
update_dir = self._resolve_archive_root_dir(z.namelist())
|
|
z.extractall(target_dir)
|
|
|
|
self._finalize_extracted_archive(zip_path, target_dir, update_dir)
|
|
|
|
# Backward compatibility alias
|
|
update_plugin = update
|