chore: Add PyPI package support for uvx/pip installation (#1764)

* Initial plan

* 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>

* Add PyPI publishing workflow and update license

- Created GitHub Actions workflow to build frontend and publish to PyPI
- Added license field to pyproject.toml to fix deprecation warning
- Updated .gitignore to exclude build artifacts
- Tested package building successfully

Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>

* Add PyPI installation documentation

- Created PYPI_INSTALLATION.md with detailed installation and usage instructions
- Updated README.md to feature uvx/pip installation as recommended method
- Updated README_EN.md with same changes for English documentation

Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>

* Address code review feedback

- Made package-data configuration more specific to langbot package only
- Improved path detection with caching to avoid repeated file I/O
- Removed sys.path searching which was incorrect for package data
- Removed interactive input() call for non-interactive environment compatibility
- Simplified error messages for version check

Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>

* Fix code review issues

- Use specific exception types instead of bare except
- Fix misleading comments about directory levels
- Remove redundant existence check before makedirs with exist_ok=True
- Use context manager for file opening to ensure proper cleanup

Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>

* Simplify package configuration and document behavioral differences

- Removed redundant package-data configuration, relying on MANIFEST.in
- Added documentation about behavioral differences between package and source installation
- Clarified that include-package-data=true uses MANIFEST.in for data files

Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>

* chore: update pyproject.toml

* chore: try pack templates in langbot/

* chore: update

* chore: update

* chore: update

* chore: update

* chore: update

* chore: adjust dir structure

* chore: fix imports

* fix: read default-pipeline-config.json

* fix: read default-pipeline-config.json

* fix: tests

* ci: publish pypi

* chore: bump version 4.6.0-beta.1 for testing

* chore: add templates/**

* fix: send adapters and requesters icons

* chore: bump version 4.6.0b2 for testing

* chore: add platform field for docker-compose.yaml

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>
Co-authored-by: Junyan Qin <rockchinq@gmail.com>
This commit is contained in:
Copilot
2025-11-16 19:53:01 +08:00
committed by GitHub
parent 6a24c951e0
commit e642ffa5b3
477 changed files with 1001 additions and 1002 deletions

View File

View File

@@ -0,0 +1,30 @@
from __future__ import annotations
from ..core import app
from . import provider
from .providers import localstorage, s3storage
class StorageMgr:
"""Storage manager"""
ap: app.Application
storage_provider: provider.StorageProvider
def __init__(self, ap: app.Application):
self.ap = ap
async def initialize(self):
storage_config = self.ap.instance_config.data.get('storage', {})
storage_type = storage_config.get('use', 'local')
if storage_type == 's3':
self.storage_provider = s3storage.S3StorageProvider(self.ap)
self.ap.logger.info('Initialized S3 storage backend.')
else:
self.storage_provider = localstorage.LocalStorageProvider(self.ap)
self.ap.logger.info('Initialized local storage backend.')
await self.storage_provider.initialize()

View File

@@ -0,0 +1,51 @@
from __future__ import annotations
import abc
from ..core import app
class StorageProvider(abc.ABC):
ap: app.Application
def __init__(self, ap: app.Application):
self.ap = ap
async def initialize(self):
pass
@abc.abstractmethod
async def save(
self,
key: str,
value: bytes,
):
pass
@abc.abstractmethod
async def load(
self,
key: str,
) -> bytes:
pass
@abc.abstractmethod
async def exists(
self,
key: str,
) -> bool:
pass
@abc.abstractmethod
async def delete(
self,
key: str,
):
pass
@abc.abstractmethod
async def delete_dir_recursive(
self,
dir_path: str,
):
pass

View File

@@ -0,0 +1,56 @@
from __future__ import annotations
import os
import aiofiles
import shutil
from ...core import app
from .. import provider
LOCAL_STORAGE_PATH = os.path.join('data', 'storage')
class LocalStorageProvider(provider.StorageProvider):
def __init__(self, ap: app.Application):
super().__init__(ap)
if not os.path.exists(LOCAL_STORAGE_PATH):
os.makedirs(LOCAL_STORAGE_PATH)
async def save(
self,
key: str,
value: bytes,
):
if not os.path.exists(os.path.join(LOCAL_STORAGE_PATH, os.path.dirname(key))):
os.makedirs(os.path.join(LOCAL_STORAGE_PATH, os.path.dirname(key)))
async with aiofiles.open(os.path.join(LOCAL_STORAGE_PATH, f'{key}'), 'wb') as f:
await f.write(value)
async def load(
self,
key: str,
) -> bytes:
async with aiofiles.open(os.path.join(LOCAL_STORAGE_PATH, f'{key}'), 'rb') as f:
return await f.read()
async def exists(
self,
key: str,
) -> bool:
return os.path.exists(os.path.join(LOCAL_STORAGE_PATH, f'{key}'))
async def delete(
self,
key: str,
):
os.remove(os.path.join(LOCAL_STORAGE_PATH, f'{key}'))
async def delete_dir_recursive(
self,
dir_path: str,
):
# 直接删除整个目录
if os.path.exists(os.path.join(LOCAL_STORAGE_PATH, dir_path)):
shutil.rmtree(os.path.join(LOCAL_STORAGE_PATH, dir_path))

View File

@@ -0,0 +1,145 @@
from __future__ import annotations
import boto3
from botocore.exceptions import ClientError
from ...core import app
from .. import provider
class S3StorageProvider(provider.StorageProvider):
"""S3 object storage provider"""
def __init__(self, ap: app.Application):
super().__init__(ap)
self.s3_client = None
self.bucket_name = None
async def initialize(self):
"""Initialize S3 client with configuration from config.yaml"""
storage_config = self.ap.instance_config.data.get('storage', {})
s3_config = storage_config.get('s3', {})
# Get S3 configuration
endpoint_url = s3_config.get('endpoint_url', '')
access_key_id = s3_config.get('access_key_id', '')
secret_access_key = s3_config.get('secret_access_key', '')
region_name = s3_config.get('region', 'us-east-1')
self.bucket_name = s3_config.get('bucket', 'langbot-storage')
# Initialize S3 client
session = boto3.session.Session()
self.s3_client = session.client(
service_name='s3',
region_name=region_name,
endpoint_url=endpoint_url if endpoint_url else None,
aws_access_key_id=access_key_id,
aws_secret_access_key=secret_access_key,
)
# Ensure bucket exists
try:
self.s3_client.head_bucket(Bucket=self.bucket_name)
except ClientError as e:
error_code = e.response['Error']['Code']
if error_code == '404':
# Bucket doesn't exist, create it
try:
self.s3_client.create_bucket(Bucket=self.bucket_name)
self.ap.logger.info(f'Created S3 bucket: {self.bucket_name}')
except Exception as create_error:
self.ap.logger.error(f'Failed to create S3 bucket: {create_error}')
raise
else:
self.ap.logger.error(f'Failed to access S3 bucket: {e}')
raise
async def save(
self,
key: str,
value: bytes,
):
"""Save bytes to S3"""
try:
self.s3_client.put_object(
Bucket=self.bucket_name,
Key=key,
Body=value,
)
except Exception as e:
self.ap.logger.error(f'Failed to save to S3: {e}')
raise
async def load(
self,
key: str,
) -> bytes:
"""Load bytes from S3"""
try:
response = self.s3_client.get_object(
Bucket=self.bucket_name,
Key=key,
)
return response['Body'].read()
except Exception as e:
self.ap.logger.error(f'Failed to load from S3: {e}')
raise
async def exists(
self,
key: str,
) -> bool:
"""Check if object exists in S3"""
try:
self.s3_client.head_object(
Bucket=self.bucket_name,
Key=key,
)
return True
except ClientError as e:
if e.response['Error']['Code'] == '404':
return False
else:
self.ap.logger.error(f'Failed to check existence in S3: {e}')
raise
async def delete(
self,
key: str,
):
"""Delete object from S3"""
try:
self.s3_client.delete_object(
Bucket=self.bucket_name,
Key=key,
)
except Exception as e:
self.ap.logger.error(f'Failed to delete from S3: {e}')
raise
async def delete_dir_recursive(
self,
dir_path: str,
):
"""Delete all objects with the given prefix (directory)"""
try:
# Ensure dir_path ends with /
if not dir_path.endswith('/'):
dir_path = dir_path + '/'
# List all objects with the prefix
paginator = self.s3_client.get_paginator('list_objects_v2')
pages = paginator.paginate(Bucket=self.bucket_name, Prefix=dir_path)
# Delete all objects
for page in pages:
if 'Contents' in page:
objects_to_delete = [{'Key': obj['Key']} for obj in page['Contents']]
if objects_to_delete:
self.s3_client.delete_objects(
Bucket=self.bucket_name,
Delete={'Objects': objects_to_delete},
)
except Exception as e:
self.ap.logger.error(f'Failed to delete directory from S3: {e}')
raise