Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/langbot/pkg/plugin/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,11 @@ def _inspect_plugin_package(
self,
file_bytes: bytes,
task_context: taskmgr.TaskContext | None,
) -> tuple[str | None, str | None]:
) -> tuple[str | None, str | None, str | None]:
"""Extract plugin identity and dependency metadata from a plugin package."""
plugin_author = None
plugin_name = None
plugin_version = None

try:
with zipfile.ZipFile(io.BytesIO(file_bytes)) as zf:
Expand All @@ -209,6 +210,7 @@ def _inspect_plugin_package(
metadata = manifest.get('metadata', {})
plugin_author = metadata.get('author')
plugin_name = metadata.get('name')
plugin_version = metadata.get('version')
except Exception:
pass

Expand All @@ -227,7 +229,7 @@ def _inspect_plugin_package(
except Exception:
pass

return plugin_author, plugin_name
return plugin_author, plugin_name, plugin_version

async def _install_mcp_from_marketplace(
self,
Expand Down Expand Up @@ -369,6 +371,7 @@ async def install_plugin(
):
plugin_author = install_info.get('plugin_author')
plugin_name = install_info.get('plugin_name')
plugin_file_transferred = False

if install_source == PluginInstallSource.MARKETPLACE:
# Handle marketplace plugin/mcp/skill installation
Expand Down Expand Up @@ -463,9 +466,18 @@ async def install_plugin(
)

file_bytes = download_resp.content
self._inspect_plugin_package(file_bytes, task_context)
plugin_author, plugin_name, plugin_version = self._inspect_plugin_package(
file_bytes,
task_context,
)
if task_context is not None and plugin_author and plugin_name:
task_context.metadata['plugin_name'] = f'{plugin_author}/{plugin_name}'
if task_context is not None and plugin_version:
task_context.metadata['plugin_version'] = plugin_version
file_key = await self.handler.send_file(file_bytes, 'lbpkg')
install_info['plugin_file_key'] = file_key
install_source = PluginInstallSource.LOCAL
plugin_file_transferred = True
self.ap.logger.info(f'Transfered file {file_key} to plugin runtime')
# Continue to install via runtime
else:
Expand All @@ -481,12 +493,14 @@ async def install_plugin(
mcp_resp.raise_for_status()
raise Exception(f'Failed to get MCP {plugin_author}/{plugin_name}')

if install_source == PluginInstallSource.LOCAL:
if install_source == PluginInstallSource.LOCAL and not plugin_file_transferred:
# transfer file before install
file_bytes = install_info['plugin_file']
plugin_author, plugin_name = self._inspect_plugin_package(file_bytes, task_context)
plugin_author, plugin_name, plugin_version = self._inspect_plugin_package(file_bytes, task_context)
if task_context is not None and plugin_author and plugin_name:
task_context.metadata['plugin_name'] = f'{plugin_author}/{plugin_name}'
if task_context is not None and plugin_version:
task_context.metadata['plugin_version'] = plugin_version
file_key = await self.handler.send_file(file_bytes, 'lbpkg')
install_info['plugin_file_key'] = file_key
del install_info['plugin_file']
Expand Down Expand Up @@ -523,9 +537,11 @@ async def install_plugin(
task_context.metadata['download_speed'] = downloaded / elapsed if elapsed > 0 else 0

file_bytes = b''.join(chunks)
plugin_author, plugin_name = self._inspect_plugin_package(file_bytes, task_context)
plugin_author, plugin_name, plugin_version = self._inspect_plugin_package(file_bytes, task_context)
if task_context is not None and plugin_author and plugin_name:
task_context.metadata['plugin_name'] = f'{plugin_author}/{plugin_name}'
if task_context is not None and plugin_version:
task_context.metadata['plugin_version'] = plugin_version
file_key = await self.handler.send_file(file_bytes, 'lbpkg')
install_info['plugin_file_key'] = file_key
self.ap.logger.info(f'Transfered file {file_key} to plugin runtime')
Expand Down
24 changes: 24 additions & 0 deletions tests/unit_tests/plugin/test_connector_pure.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ def test_extract_deps_with_requirements_txt(self):
assert 'flask' in task_context.metadata['deps_list']
assert 'numpy' in task_context.metadata['deps_list']

def test_extract_plugin_identity_includes_version(self):
"""Extract plugin identity and version from manifest.yaml."""
connector = self._create_connector()

zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w') as zf:
zf.writestr(
'manifest.yaml',
'\n'.join(
[
'metadata:',
' author: langbot-team',
' name: LangRAG',
' version: 0.1.8',
]
),
)

assert connector._inspect_plugin_package(zip_buffer.getvalue(), None) == (
'langbot-team',
'LangRAG',
'0.1.8',
)

def test_extract_deps_empty_requirements(self):
"""Handle empty requirements.txt."""
connector = self._create_connector()
Expand Down
Loading