Add package structure and resource path utilities

- Created langbot/ package with __init__.py and __main__.py entry point
- Added paths utility to find frontend and resource files from package installation
- Updated config loading to use resource paths
- Updated frontend serving to use resource paths
- Added MANIFEST.in for package data inclusion
- Updated pyproject.toml with build system and entry points

Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-11-07 13:58:18 +00:00
parent 8fe59da302
commit cab573f3e2
11 changed files with 272 additions and 12 deletions

23
MANIFEST.in Normal file
View File

@@ -0,0 +1,23 @@
include README.md
include LICENSE
include pyproject.toml
# Include all Python packages
recursive-include langbot *.py
recursive-include pkg *.py
recursive-include libs *.py
# Include templates and resources
recursive-include templates *
recursive-include res *
# Include compiled frontend files (will be added during build)
recursive-include web/out *
# Exclude unnecessary files
global-exclude *.pyc
global-exclude *.pyo
global-exclude __pycache__
global-exclude .DS_Store
global-exclude *.so
global-exclude *.dylib

3
langbot/__init__.py Normal file
View File

@@ -0,0 +1,3 @@
"""LangBot - Easy-to-use global IM bot platform designed for LLM era"""
__version__ = "4.4.1"

106
langbot/__main__.py Normal file
View File

@@ -0,0 +1,106 @@
"""LangBot entry point for package execution"""
import asyncio
import argparse
import sys
import os
# ASCII art banner
asciiart = r"""
_ ___ _
| | __ _ _ _ __ _| _ ) ___| |_
| |__/ _` | ' \/ _` | _ \/ _ \ _|
|____\__,_|_||_\__, |___/\___/\__|
|___/
⭐️ Open Source 开源地址: https://github.com/langbot-app/LangBot
📖 Documentation 文档地址: https://docs.langbot.app
"""
async def main_entry(loop: asyncio.AbstractEventLoop):
"""Main entry point for LangBot"""
parser = argparse.ArgumentParser(description='LangBot')
parser.add_argument(
'--standalone-runtime',
action='store_true',
help='Use standalone plugin runtime / 使用独立插件运行时',
default=False,
)
parser.add_argument('--debug', action='store_true', help='Debug mode / 调试模式', default=False)
args = parser.parse_args()
if args.standalone_runtime:
from pkg.utils import platform
platform.standalone_runtime = True
if args.debug:
from pkg.utils import constants
constants.debug_mode = True
print(asciiart)
# Check dependencies
from pkg.core.bootutils import deps
missing_deps = await deps.check_deps()
if missing_deps:
print('以下依赖包未安装,将自动安装,请完成后重启程序:')
print(
'These dependencies are missing, they will be installed automatically, please restart the program after completion:'
)
for dep in missing_deps:
print('-', dep)
await deps.install_deps(missing_deps)
print('已自动安装缺失的依赖包,请重启程序。')
print('The missing dependencies have been installed automatically, please restart the program.')
sys.exit(0)
# Check configuration files
from pkg.core.bootutils import files
generated_files = await files.generate_files()
if generated_files:
print('以下文件不存在,已自动生成:')
print('Following files do not exist and have been automatically generated:')
for file in generated_files:
print('-', file)
from pkg.core import boot
await boot.main(loop)
def main():
"""Main function to be called by console script entry point"""
# Check Python version
if sys.version_info < (3, 10, 1):
print('需要 Python 3.10.1 及以上版本,当前 Python 版本为:', sys.version)
input('按任意键退出...')
print('Your Python version is not supported. Please exit the program by pressing any key.')
sys.exit(1)
# Set up the working directory
# When installed as a package, we need to handle the working directory differently
# We'll create data directory in current working directory if not exists
if not os.path.exists('data'):
print('Creating data directory in current working directory...')
print('在当前工作目录创建 data 目录...')
os.makedirs('data', exist_ok=True)
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(main_entry(loop))
except KeyboardInterrupt:
print('\n正在退出...')
print('Exiting...')
finally:
loop.close()
if __name__ == '__main__':
main()

View File

@@ -86,7 +86,9 @@ class HTTPController:
ginst = g(self.ap, self.quart_app)
await ginst.initialize()
frontend_path = 'web/out'
from ....utils import paths
frontend_path = paths.get_frontend_path()
@self.quart_app.route('/')
async def index():

View File

@@ -74,11 +74,15 @@ class PipelineService:
return self.ap.persistence_mgr.serialize_model(persistence_pipeline.LegacyPipeline, pipeline)
async def create_pipeline(self, pipeline_data: dict, default: bool = False) -> str:
from ....utils import paths as path_utils
pipeline_data['uuid'] = str(uuid.uuid4())
pipeline_data['for_version'] = self.ap.ver_mgr.get_current_version()
pipeline_data['stages'] = default_stage_order.copy()
pipeline_data['is_default'] = default
pipeline_data['config'] = json.load(open('templates/default-pipeline-config.json', 'r', encoding='utf-8'))
template_path = path_utils.get_resource_path('templates/default-pipeline-config.json')
pipeline_data['config'] = json.load(open(template_path, 'r', encoding='utf-8'))
await self.ap.persistence_mgr.execute_async(
sqlalchemy.insert(persistence_pipeline.LegacyPipeline).values(**pipeline_data)

View File

@@ -18,12 +18,22 @@ class JSONConfigFile(file_model.ConfigFile):
self.template_file_name = template_file_name
self.template_data = template_data
def _get_template_path(self) -> str:
"""Get the actual path to the template file, handling package installation"""
if self.template_file_name is None:
return None
from ...utils import paths as path_utils
return path_utils.get_resource_path(self.template_file_name)
def exists(self) -> bool:
return os.path.exists(self.config_file_name)
async def create(self):
if self.template_file_name is not None:
shutil.copyfile(self.template_file_name, self.config_file_name)
template_path = self._get_template_path()
if template_path is not None:
shutil.copyfile(template_path, self.config_file_name)
elif self.template_data is not None:
with open(self.config_file_name, 'w', encoding='utf-8') as f:
json.dump(self.template_data, f, indent=4, ensure_ascii=False)
@@ -34,8 +44,10 @@ class JSONConfigFile(file_model.ConfigFile):
if not self.exists():
await self.create()
if self.template_file_name is not None:
with open(self.template_file_name, 'r', encoding='utf-8') as f:
template_path = self._get_template_path()
if template_path is not None:
with open(template_path, 'r', encoding='utf-8') as f:
self.template_data = json.load(f)
with open(self.config_file_name, 'r', encoding='utf-8') as f:

View File

@@ -18,12 +18,22 @@ class YAMLConfigFile(file_model.ConfigFile):
self.template_file_name = template_file_name
self.template_data = template_data
def _get_template_path(self) -> str:
"""Get the actual path to the template file, handling package installation"""
if self.template_file_name is None:
return None
from ...utils import paths as path_utils
return path_utils.get_resource_path(self.template_file_name)
def exists(self) -> bool:
return os.path.exists(self.config_file_name)
async def create(self):
if self.template_file_name is not None:
shutil.copyfile(self.template_file_name, self.config_file_name)
template_path = self._get_template_path()
if template_path is not None:
shutil.copyfile(template_path, self.config_file_name)
elif self.template_data is not None:
with open(self.config_file_name, 'w', encoding='utf-8') as f:
yaml.dump(self.template_data, f, indent=4, allow_unicode=True)
@@ -34,8 +44,10 @@ class YAMLConfigFile(file_model.ConfigFile):
if not self.exists():
await self.create()
if self.template_file_name is not None:
with open(self.template_file_name, 'r', encoding='utf-8') as f:
template_path = self._get_template_path()
if template_path is not None:
with open(template_path, 'r', encoding='utf-8') as f:
self.template_data = yaml.load(f, Loader=yaml.FullLoader)
with open(self.config_file_name, 'r', encoding='utf-8') as f:

View File

@@ -180,7 +180,11 @@ class Application:
async def print_web_access_info(self):
"""Print access webui tips"""
if not os.path.exists(os.path.join('.', 'web/out')):
from ..utils import paths
frontend_path = paths.get_frontend_path()
if not os.path.exists(frontend_path):
self.logger.warning('WebUI 文件缺失请根据文档部署https://docs.langbot.app/zh')
self.logger.warning(
'WebUI files are missing, please deploy according to the documentation: https://docs.langbot.app/en'

View File

@@ -20,6 +20,8 @@ required_paths = [
async def generate_files() -> list[str]:
global required_files, required_paths
from ...utils import paths as path_utils
for required_paths in required_paths:
if not os.path.exists(required_paths):
os.mkdir(required_paths)
@@ -27,7 +29,8 @@ async def generate_files() -> list[str]:
generated_files = []
for file in required_files:
if not os.path.exists(file):
shutil.copyfile(required_files[file], file)
template_path = path_utils.get_resource_path(required_files[file])
shutil.copyfile(template_path, file)
generated_files.append(file)
return generated_files

73
pkg/utils/paths.py Normal file
View File

@@ -0,0 +1,73 @@
"""Utility functions for finding package resources"""
import os
import sys
from pathlib import Path
def get_frontend_path() -> str:
"""
Get the path to the frontend build files.
Returns the path to web/out directory, handling both:
- Development mode: running from source directory
- Package mode: installed via pip/uvx
"""
# First, check if we're running from source directory
# (main.py exists in current directory)
if os.path.exists('main.py') and os.path.exists('web/out'):
with open('main.py', 'r', encoding='utf-8') as f:
content = f.read()
if 'LangBot/main.py' in content:
return 'web/out'
# Second, check if we're in a development/source directory
# (current directory has web/out)
if os.path.exists('web/out'):
return 'web/out'
# Third, try to find it relative to this file's location
# This handles the case when installed as a package
pkg_dir = Path(__file__).parent.parent.parent
frontend_path = pkg_dir / 'web' / 'out'
if frontend_path.exists():
return str(frontend_path)
# Fourth, check if it's in the package installation directory
# Look for the web/out directory in the sys.path
for path in sys.path:
candidate = Path(path) / 'web' / 'out'
if candidate.exists():
return str(candidate)
# Return the default path (will be checked by caller)
return 'web/out'
def get_resource_path(resource: str) -> str:
"""
Get the path to a resource file.
Args:
resource: Relative path to resource (e.g., 'templates/config.yaml')
Returns:
Absolute path to the resource
"""
# First, check if resource exists in current directory
if os.path.exists(resource):
return resource
# Second, try to find it relative to package directory
pkg_dir = Path(__file__).parent.parent.parent
resource_path = pkg_dir / resource
if resource_path.exists():
return str(resource_path)
# Third, check in sys.path
for path in sys.path:
candidate = Path(path) / resource
if candidate.exists():
return str(candidate)
# Return the original path
return resource

View File

@@ -99,6 +99,24 @@ Homepage = "https://langbot.app"
Documentation = "https://docs.langbot.app"
Repository = "https://github.com/langbot-app/LangBot"
[project.scripts]
langbot = "langbot.__main__:main"
[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"
[tool.setuptools]
packages = { find = {} }
include-package-data = true
[tool.setuptools.package-data]
"*" = [
"web/out/**/*",
"templates/**/*",
"res/**/*",
]
[dependency-groups]
dev = [
"pre-commit>=4.2.0",