service-registry

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Table of Contents

目录

Service Registry

服务注册(Service Registry)

Overview

概述

A registry pattern for managing connections to external services. Handles configuration, health checking, and execution across multiple service integrations.
一种用于管理外部服务连接的注册模式,可处理多服务集成中的配置、健康检查和执行操作。

When To Use

适用场景

  • Managing multiple external services.
  • Need consistent execution interface.
  • Want health monitoring across services.
  • Building service failover logic.
  • 管理多个外部服务
  • 需要统一的执行接口
  • 希望对所有服务进行健康监控
  • 构建服务故障转移逻辑

When NOT To Use

不适用场景

  • Single service integration without registry needs
  • 无需注册中心的单一服务集成场景

Core Concepts

核心概念

Service Configuration

服务配置

python
@dataclass
class ServiceConfig:
    name: str
    command: str
    auth_method: str  # "api_key", "oauth", "token"
    auth_env_var: str
    quota_limits: dict
    models: list[str] = field(default_factory=list)
Verification: Run the command with
--help
flag to verify availability.
python
@dataclass
class ServiceConfig:
    name: str
    command: str
    auth_method: str  # "api_key", "oauth", "token"
    auth_env_var: str
    quota_limits: dict
    models: list[str] = field(default_factory=list)
验证方式: 运行带
--help
参数的命令以验证可用性。

Execution Result

执行结果

python
@dataclass
class ExecutionResult:
    success: bool
    stdout: str
    stderr: str
    exit_code: int
    duration: float
    tokens_used: int
Verification: Run the command with
--help
flag to verify availability.
python
@dataclass
class ExecutionResult:
    success: bool
    stdout: str
    stderr: str
    exit_code: int
    duration: float
    tokens_used: int
验证方式: 运行带
--help
参数的命令以验证可用性。

Quick Start

快速开始

Register Services

注册服务

python
from leyline.service_registry import ServiceRegistry

registry = ServiceRegistry()

registry.register("gemini", ServiceConfig(
    name="gemini",
    command="gemini",
    auth_method="api_key",
    auth_env_var="GEMINI_API_KEY",
    quota_limits={"rpm": 60, "daily": 1000}
))
Verification: Run the command with
--help
flag to verify availability.
python
from leyline.service_registry import ServiceRegistry

registry = ServiceRegistry()

registry.register("gemini", ServiceConfig(
    name="gemini",
    command="gemini",
    auth_method="api_key",
    auth_env_var="GEMINI_API_KEY",
    quota_limits={"rpm": 60, "daily": 1000}
))
验证方式: 运行带
--help
参数的命令以验证可用性。

Execute via Service

通过服务执行

python
result = registry.execute(
    service="gemini",
    prompt="Analyze this code",
    files=["src/main.py"],
    model="gemini-2.5-pro"
)

if result.success:
    print(result.stdout)
Verification: Run the command with
--help
flag to verify availability.
python
result = registry.execute(
    service="gemini",
    prompt="Analyze this code",
    files=["src/main.py"],
    model="gemini-2.5-pro"
)

if result.success:
    print(result.stdout)
验证方式: 运行带
--help
参数的命令以验证可用性。

Health Checks

健康检查

python
undefined
python
undefined

Check single service

检查单个服务

status = registry.health_check("gemini")
status = registry.health_check("gemini")

Check all services

检查所有服务

all_status = registry.health_check_all() for service, healthy in all_status.items(): print(f"{service}: {'OK' if healthy else 'FAILED'}")
**Verification:** Run the command with `--help` flag to verify availability.
all_status = registry.health_check_all() for service, healthy in all_status.items(): print(f"{service}: {'OK' if healthy else 'FAILED'}")
**验证方式:** 运行带`--help`参数的命令以验证可用性。

Service Selection

服务选择

Auto-Selection

自动选择

python
undefined
python
undefined

Select best service for task

选择最适合任务的服务

service = registry.select_service( requirements={ "large_context": True, "fast_response": False } )
**Verification:** Run the command with `--help` flag to verify availability.
service = registry.select_service( requirements={ "large_context": True, "fast_response": False } )
**验证方式:** 运行带`--help`参数的命令以验证可用性。

Failover Pattern

故障转移模式

python
def execute_with_failover(prompt: str, files: list) -> ExecutionResult:
    for service in registry.get_healthy_services():
        result = registry.execute(service, prompt, files)
        if result.success:
            return result
    raise AllServicesFailedError()
Verification: Run the command with
--help
flag to verify availability.
python
def execute_with_failover(prompt: str, files: list) -> ExecutionResult:
    for service in registry.get_healthy_services():
        result = registry.execute(service, prompt, files)
        if result.success:
            return result
    raise AllServicesFailedError()
验证方式: 运行带
--help
参数的命令以验证可用性。

Integration Pattern

集成模式

yaml
undefined
yaml
undefined

In your skill's frontmatter

在你的Skill前置配置中

dependencies: [leyline:service-registry]
**Verification:** Run the command with `--help` flag to verify availability.
dependencies: [leyline:service-registry]
**验证方式:** 运行带`--help`参数的命令以验证可用性。

Detailed Resources

详细资源

  • Service Config: See
    modules/service-config.md
    for configuration options.
  • Execution Patterns: See
    modules/execution-patterns.md
    for advanced usage.
  • 服务配置:查看
    modules/service-config.md
    获取配置选项详情。
  • 执行模式:查看
    modules/execution-patterns.md
    获取高级用法说明。

Exit Criteria

验收标准

  • Services registered with configuration.
  • Health checks passing.
  • Execution results properly handled.
  • 已完成服务及配置注册
  • 健康检查全部通过
  • 执行结果已被正确处理

Troubleshooting

故障排除

Common Issues

常见问题

Command not found Ensure all dependencies are installed and in PATH
Permission errors Check file permissions and run with appropriate privileges
Unexpected behavior Enable verbose logging with
--verbose
flag
命令未找到 确保所有依赖已安装且已添加至PATH环境变量
权限错误 检查文件权限并使用合适的权限运行
异常行为 使用
--verbose
参数启用详细日志