001/* 002 * Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.agentsflex.core.agent.route; 017 018import java.util.HashMap; 019import java.util.List; 020import java.util.Map; 021 022/** 023 * Agent 注册中心,用于管理所有可用的 ReActAgent 工厂。 024 */ 025public class RoutingAgentRegistry { 026 027 private final Map<String, RoutingAgentFactory> agentFactories = new HashMap<>(); 028 private final Map<String, String> agentDescriptions = new HashMap<>(); 029 private final Map<String, String> keywordToAgent = new HashMap<>(); 030 031 /** 032 * 注册 Agent,并可选绑定关键字(用于快速匹配) 033 */ 034 public void register(String name, String description, RoutingAgentFactory factory) { 035 register(name, description, null, factory); 036 } 037 038 public void register(String name, String description, List<String> keywords, RoutingAgentFactory factory) { 039 agentFactories.put(name, factory); 040 agentDescriptions.put(name, description); 041 042 if (keywords != null && !keywords.isEmpty()) { 043 for (String kw : keywords) { 044 if (kw != null && !kw.trim().isEmpty()) { 045 keywordToAgent.put(kw.trim().toLowerCase(), name); 046 } 047 } 048 } 049 } 050 051 // 按关键字查找 Agent 052 public String findAgentByKeyword(String userQuery) { 053 if (userQuery == null) return null; 054 String lowerQuery = userQuery.toLowerCase(); 055 for (Map.Entry<String, String> entry : keywordToAgent.entrySet()) { 056 if (lowerQuery.contains(entry.getKey())) { 057 return entry.getValue(); 058 } 059 } 060 return null; 061 } 062 063 064 public RoutingAgentFactory getAgentFactory(String name) { 065 return agentFactories.get(name); 066 } 067 068 public String getAgentDescriptions() { 069 StringBuilder sb = new StringBuilder(); 070 for (Map.Entry<String, String> entry : agentDescriptions.entrySet()) { 071 sb.append("- ").append(entry.getKey()).append(": ").append(entry.getValue()).append("\n"); 072 } 073 return sb.toString().trim(); 074 } 075}