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 com.agentsflex.core.util.JsonSchemaTypeMapper; 019import org.jetbrains.annotations.Nullable; 020 021import java.io.Serializable; 022import java.util.ArrayList; 023import java.util.LinkedHashMap; 024import java.util.List; 025import java.util.Map; 026 027/** 028 * 工具方法参数定义 029 * 030 * @author fuhai 031 * @since 2023/10/01 032 */ 033public class Parameter implements Serializable { 034 035 private static final long serialVersionUID = 1L; 036 037 protected String name; 038 protected String type = "string"; 039 protected String description; 040 protected String[] enums; 041 protected boolean required = false; 042 protected Object defaultValue; 043 protected List<Parameter> children; 044 045 /** 046 * object 类型的属性定义(仅当字段有 @ToolParam 时解析) 047 * key: 字段名, value: 字段的 schema map(由序列化器直接使用) 048 */ 049 protected Map<String, Object> properties; 050 051 // =============== Getters and Setters =============== 052 053 public String getName() { 054 return name; 055 } 056 057 public void setName(String name) { 058 this.name = name; 059 } 060 061 public String getType() { 062 return type; 063 } 064 065 public void setType(String type) { 066 this.type = type; 067 } 068 069 public String getDescription() { 070 return description; 071 } 072 073 public void setDescription(String description) { 074 this.description = description; 075 } 076 077 @Nullable 078 public String[] getEnums() { 079 return enums != null ? enums.clone() : null; 080 } 081 082 public void setEnums(@Nullable String[] enums) { 083 this.enums = enums != null ? enums.clone() : null; 084 } 085 086 public boolean isRequired() { 087 return required; 088 } 089 090 public void setRequired(boolean required) { 091 this.required = required; 092 } 093 094 @Nullable 095 public Object getDefaultValue() { 096 return defaultValue; 097 } 098 099 public void setDefaultValue(@Nullable Object defaultValue) { 100 this.defaultValue = defaultValue; 101 } 102 103 @Nullable 104 public List<Parameter> getChildren() { 105 return children != null ? new ArrayList<>(children) : null; 106 } 107 108 public void setChildren(@Nullable List<Parameter> children) { 109 this.children = children != null ? new ArrayList<>(children) : null; 110 } 111 112 public void addChild(Parameter parameter) { 113 if (children == null) { 114 children = new ArrayList<>(); 115 } 116 children.add(parameter); 117 } 118 119 @Nullable 120 public Map<String, Object> getProperties() { 121 return properties != null ? new LinkedHashMap<>(properties) : null; 122 } 123 124 public void setProperties(@Nullable Map<String, Object> properties) { 125 this.properties = properties != null ? new LinkedHashMap<>(properties) : null; 126 } 127 128 // =============== Builder =============== 129 130 public static Builder builder() { 131 return new Builder(); 132 } 133 134 public static class Builder { 135 private String name; 136 private String type = "string"; 137 private String description; 138 private String[] enums; 139 private boolean required = false; 140 private Object defaultValue; 141 private List<Parameter> children; 142 private Map<String, Object> properties; 143 144 public Builder name(String name) { 145 this.name = name; 146 return this; 147 } 148 149 public Builder type(String type) { 150 this.type = type; 151 return this; 152 } 153 154 public Builder typeOfClass(Class<?> type) { 155 this.type = JsonSchemaTypeMapper.mapToSchemaType(type); 156 return this; 157 } 158 159 public Builder description(String description) { 160 this.description = description; 161 return this; 162 } 163 164 public Builder enums(String... enums) { 165 this.enums = enums != null ? enums.clone() : null; 166 return this; 167 } 168 169 public Builder required(boolean required) { 170 this.required = required; 171 return this; 172 } 173 174 public Builder defaultValue(Object defaultValue) { 175 this.defaultValue = defaultValue; 176 return this; 177 } 178 179 public Builder addChild(Parameter child) { 180 if (this.children == null) { 181 this.children = new ArrayList<>(); 182 } 183 this.children.add(child); 184 return this; 185 } 186 187 public Builder children(List<Parameter> children) { 188 this.children = children != null ? new ArrayList<>(children) : null; 189 return this; 190 } 191 192 public Builder properties(Map<String, Object> properties) { 193 this.properties = properties != null ? new LinkedHashMap<>(properties) : null; 194 return this; 195 } 196 197 public Parameter build() { 198 Parameter param = new Parameter(); 199 param.setName(name); 200 param.setType(type); 201 param.setDescription(description); 202 param.setEnums(enums); 203 param.setRequired(required); 204 param.setDefaultValue(defaultValue); 205 param.setChildren(children); 206 param.setProperties(properties); 207 return param; 208 } 209 } 210}