ReMe/reme2/component/service/base_service.py
jinli.yl fa249a3e38 feat(components): add Anthropic LLM support and enhance component architecture
- Integrate AnthropicChatModel with new AnthropicAsLLM component
- Add component formatters for OpenAI and Anthropic chat models
- Implement token counter component with estimated token counting
- Create base client component for ReMe service communication
- Refactor BaseComponent to remove app_context parameter from _start/_close
- Update embedding model base class to remove retry logic and use npz cache
- Add job component for sequential step execution with BaseJob
- Implement step component base class for LLM workflow execution
- Enhance application context with proper type annotations
- Update component initialization to pass app_context automatically
- Remove asyncio dependency from embedding model cache operations
2026-04-23 14:18:57 +08:00

62 lines
1.9 KiB
Python

"""Abstract base class for service implementations."""
from abc import abstractmethod
from typing import TYPE_CHECKING
from ..base_component import BaseComponent
from ..job.base_job import BaseJob
from ...enumeration import ComponentEnum
if TYPE_CHECKING:
from ...application import Application
class BaseService(BaseComponent):
"""Abstract base class for services that expose jobs.
Services provide different ways to invoke jobs (HTTP, CLI, MCP, etc.).
Subclasses must implement add_job to register jobs with the service.
"""
component_type = ComponentEnum.SERVICE
def __init__(self, **kwargs):
"""Initialize the service."""
super().__init__(**kwargs)
self.service = None
async def _start(self) -> None:
async def _close(self) -> None:
"""Default empty implementation for sync services."""
@abstractmethod
def build_service(self, app: "Application") -> None:
"""Build the service."""
@abstractmethod
def add_job(self, job: BaseJob) -> None:
"""Register a job with the service."""
@abstractmethod
def start_service(self, app: "Application") -> None:
"""Start the service."""
def add_jobs(self, app: "Application") -> None:
"""Register all jobs from the application context."""
for name, job in app.context.jobs.values():
try:
self.add_job(job)
self.logger.info(f"Successfully Added job {name}")
except Exception as e:
self.logger.error(f"Failed to add job {name}: {e}")
def run_app(self, app: "Application") -> None:
"""Register all jobs from the application and start the service.
Args:
app: The application containing jobs to register.
"""
self.build_service(app)
self.add_jobs(app)
self.start_service(app)