|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.tool.augment; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.function.Consumer; |
| 22 | + |
| 23 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 24 | + |
| 25 | +import org.springframework.ai.chat.model.ToolContext; |
| 26 | +import org.springframework.ai.tool.ToolCallback; |
| 27 | +import org.springframework.ai.tool.augment.ToolInputSchemaAugmenter.AugmentedArgumentType; |
| 28 | +import org.springframework.ai.tool.definition.ToolDefinition; |
| 29 | +import org.springframework.ai.util.json.JsonParser; |
| 30 | +import org.springframework.lang.Nullable; |
| 31 | +import org.springframework.util.Assert; |
| 32 | + |
| 33 | +/** |
| 34 | + * This class wraps an existing {@link ToolCallback} and modifies its input schema to |
| 35 | + * include additional fields defined in the provided Record type. It also provides a |
| 36 | + * mechanism to handle these extended arguments, either by consuming them via a provided |
| 37 | + * {@link Consumer} or by removing them from the input after processing. |
| 38 | + * |
| 39 | + * @author Christian Tzolov |
| 40 | + */ |
| 41 | +public class AugmentedToolCallback<T extends Record> implements ToolCallback { |
| 42 | + |
| 43 | + /** |
| 44 | + * The delegate ToolCallback that this class extends. |
| 45 | + */ |
| 46 | + private final ToolCallback delegate; |
| 47 | + |
| 48 | + /** |
| 49 | + * The augmented ToolDefinition that includes the augmented input schema. |
| 50 | + */ |
| 51 | + private ToolDefinition augmentedToolDefinition; |
| 52 | + |
| 53 | + /** |
| 54 | + * The record class type that defines the structure of the augmented arguments. |
| 55 | + */ |
| 56 | + private Class<T> augmentedArgumentsClass; |
| 57 | + |
| 58 | + /** |
| 59 | + * A consumer that processes the augmented arguments extracted from the tool input. |
| 60 | + */ |
| 61 | + private Consumer<AugmentedArgumentEvent<T>> augmentedArgumentsConsumer; |
| 62 | + |
| 63 | + /** |
| 64 | + * The list of tool argument types that have been added to the tool input schema. |
| 65 | + */ |
| 66 | + private List<AugmentedArgumentType> augmentedArgumentTypes; |
| 67 | + |
| 68 | + /** |
| 69 | + * A flag indicating whether to remove the augmented arguments from the tool input |
| 70 | + * after they have been processed. If the arguments are not removed, they will remain |
| 71 | + * in the tool input for the delegate to process. In many cases this could be useful. |
| 72 | + */ |
| 73 | + private boolean removeAugmentedArgumentsAfterProcessing = false; |
| 74 | + |
| 75 | + public AugmentedToolCallback(ToolCallback delegate, Class<T> augmentedArgumentsClass, |
| 76 | + Consumer<AugmentedArgumentEvent<T>> augmentedArgumentsConsumer, |
| 77 | + boolean removeExtraArgumentsAfterProcessing) { |
| 78 | + Assert.notNull(delegate, "Delegate ToolCallback must not be null"); |
| 79 | + Assert.notNull(augmentedArgumentsClass, "Argument types must not be null"); |
| 80 | + Assert.isTrue(augmentedArgumentsClass.isRecord(), "Argument types must be a Record type"); |
| 81 | + Assert.isTrue(augmentedArgumentsClass.getRecordComponents().length > 0, |
| 82 | + "Argument types must have at least one field"); |
| 83 | + |
| 84 | + this.delegate = delegate; |
| 85 | + this.augmentedArgumentTypes = ToolInputSchemaAugmenter.toAugmentedArgumentTypes(augmentedArgumentsClass); |
| 86 | + String originalSchema = this.delegate.getToolDefinition().inputSchema(); |
| 87 | + String augmentedSchema = ToolInputSchemaAugmenter.augmentToolInputSchema(originalSchema, |
| 88 | + this.augmentedArgumentTypes); |
| 89 | + this.augmentedToolDefinition = ToolDefinition.builder() |
| 90 | + .name(this.delegate.getToolDefinition().name()) |
| 91 | + .description(this.delegate.getToolDefinition().description()) |
| 92 | + .inputSchema(augmentedSchema) |
| 93 | + .build(); |
| 94 | + |
| 95 | + this.augmentedArgumentsClass = augmentedArgumentsClass; |
| 96 | + this.augmentedArgumentsConsumer = augmentedArgumentsConsumer; |
| 97 | + this.removeAugmentedArgumentsAfterProcessing = removeExtraArgumentsAfterProcessing; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public ToolDefinition getToolDefinition() { |
| 102 | + return this.augmentedToolDefinition; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public String call(String toolInput) { |
| 107 | + return this.delegate.call(this.handleAugmentedArguments(toolInput)); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String call(String toolInput, @Nullable ToolContext tooContext) { |
| 112 | + return this.delegate.call(this.handleAugmentedArguments(toolInput), tooContext); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Handles the augmented arguments in the tool input. It extracts the augmented |
| 117 | + * arguments from the tool input, processes them using the provided consumer, and |
| 118 | + * optionally removes them from the tool input. |
| 119 | + * @param toolInput the input as received from the LLM. |
| 120 | + * @return the input to send to the delegate ToolCallback |
| 121 | + */ |
| 122 | + private String handleAugmentedArguments(String toolInput) { |
| 123 | + |
| 124 | + // Extract the augmented arguments from the toolInput and send them to the |
| 125 | + // consumer if provided. |
| 126 | + if (this.augmentedArgumentsConsumer != null) { |
| 127 | + T augmentedArguments = JsonParser.fromJson(toolInput, this.augmentedArgumentsClass); |
| 128 | + this.augmentedArgumentsConsumer |
| 129 | + .accept(new AugmentedArgumentEvent<>(this.augmentedToolDefinition, toolInput, augmentedArguments)); |
| 130 | + } |
| 131 | + |
| 132 | + // Optionally remove the extra arguments from the toolInput |
| 133 | + if (this.removeAugmentedArgumentsAfterProcessing) { |
| 134 | + var args = JsonParser.fromJson(toolInput, new TypeReference<Map<String, Object>>() { |
| 135 | + }); |
| 136 | + |
| 137 | + for (AugmentedArgumentType newFieldType : this.augmentedArgumentTypes) { |
| 138 | + args.remove(newFieldType.name()); |
| 139 | + } |
| 140 | + toolInput = JsonParser.toJson(args); |
| 141 | + } |
| 142 | + |
| 143 | + return toolInput; |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments