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.model.chat.tool; 017 018import java.util.ArrayList; 019import java.util.List; 020import java.util.Map; 021import java.util.function.Function; 022 023public interface Tool { 024 025 String getName(); 026 027 String getDescription(); 028 029 Parameter[] getParameters(); 030 031 Object invoke(Map<String, Object> argsMap); 032 033 static Tool.Builder builder() { 034 return new Tool.Builder(); 035 } 036 037 class Builder { 038 private String name; 039 private String description; 040 private final List<Parameter> parameters = new ArrayList<>(); 041 private Function<Map<String, Object>, Object> invoker; 042 043 public Builder name(String name) { 044 this.name = name; 045 return this; 046 } 047 048 public Builder description(String description) { 049 this.description = description; 050 return this; 051 } 052 053 public Builder addParameter(Parameter parameter) { 054 this.parameters.add(parameter); 055 return this; 056 } 057 058 public Builder function(Function<Map<String, Object>, Object> function) { 059 this.invoker = function; 060 return this; 061 } 062 063 public Tool build() { 064 FunctionTool tool = new FunctionTool(); 065 tool.setName(name); 066 tool.setDescription(description); 067 tool.setParameters(parameters.toArray(new Parameter[0])); 068 tool.setInvoker(invoker); 069 return tool; 070 } 071 } 072}