跳转至

Client 模块

核心文件

  • src/by_framework/client/client.py - GatewayClient
  • src/by_framework/client/byai_client.py - ByaiGatewayClient
  • client/GatewayClient.java - GatewayClient
  • client/ByaiGatewayClient.java - ByaiGatewayClient
  • src/client.ts - GatewayClient
  • src/byai_client.ts - ByaiGatewayClient

GatewayClient

向 Redis Streams 发送命令的客户端。

构造方法

class GatewayClient:
    def __init__(
        self,
        redis_client: Redis,
        registry: WorkerRegistry,
    ) -> None:
public class GatewayClient<T> {
    public GatewayClient(RedisClient redisClient)
    public GatewayClient(RedisClient redisClient, WorkerRegistry registry, List<GatewayInterceptor> interceptors)
}
export class GatewayClient {
    constructor(params: {
        redisClient: Redis;
        registry?: WorkerRegistry;
        interceptors?: GatewayInterceptor[];
    })
}

sendMessage

async def send_message(
    self,
    target_agent_type: str,
    session_id: str,
    content: Any,
    user_code: str = "",
    action_type: str = "ASK_AGENT",
    metadata: Optional[dict] = None,
    target_worker_id: Optional[str] = None,
    route_policy: str = RoutePolicy.FAIL_FAST,  # 取代旧版 require_online_worker: bool
) -> SendMessageResponse:

route_policy 取代了 require_online_worker

旧版 require_online_worker: bool 参数已经被 route_policy 枚举取代(FAIL_FAST 大致对应旧版 TrueSEND_ANYWAY 对应 False,另外还新增了 WAKE_AND_WAIT/WAKE_AND_QUEUE/QUEUE_ONLY 三种配合唤醒控制器使用的策略)。TypeScript 目前公开的 API 还是 requireOnlineWorker: boolean, 内部会转换成同样的 route_policy 协议字段;Java 两种参数都支持(requireOnlineWorker 作为向后 兼容重载)。

public SendResponse sendMessage(
    String targetAgentType,
    String sessionId,
    Object content,
    String userCode, String userName,
    ActionType actionType, String targetWorkerId,
    String parentMessageId, String sourceAgentType,
    Map<String, Object> extraPayload,
    Map<String, Object> metadata
)
async sendMessage(params: SendMessageParams): Promise<SendMessageResponse>

cancelTask

async def cancel_task(
    self, message_id: str, session_id: str, reason: str = "",
) -> CancelTaskResponse:
public CancelResponse cancelTask(String messageId, String sessionId, String reason)
async cancelTask(params: CancelTaskParams): Promise<CancelTaskResponse>

ByaiGatewayClient

GatewayClient 的封装,增加了 ByaiMessageInterceptor,支持更高级的消息协议。所有语言的 ByaiGatewayClient 都自动包含消息拦截器。