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

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

@@ -179,8 +179,12 @@ class Application:
async def print_web_access_info(self):
"""Print access webui tips"""
from ..utils import paths
frontend_path = paths.get_frontend_path()
if not os.path.exists(os.path.join('.', 'web/out')):
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

@@ -19,6 +19,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):
@@ -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