diff --git a/agentscope-core/src/main/java/io/agentscope/core/tool/SchemaOnlyTool.java b/agentscope-core/src/main/java/io/agentscope/core/tool/SchemaOnlyTool.java index 9dd7882d53..7958512348 100644 --- a/agentscope-core/src/main/java/io/agentscope/core/tool/SchemaOnlyTool.java +++ b/agentscope-core/src/main/java/io/agentscope/core/tool/SchemaOnlyTool.java @@ -57,6 +57,7 @@ public class SchemaOnlyTool extends ToolBase { private final Boolean strict; private final Boolean deferLoading; + private final Map outputSchema; /** * Creates a new SchemaOnlyTool from a ToolSchema. @@ -71,7 +72,8 @@ public SchemaOnlyTool(ToolSchema schema) { schema.getDescription(), schema.getParameters(), schema.getStrict(), - schema.getDeferLoading()); + schema.getDeferLoading(), + schema.getOutputSchema()); } /** @@ -109,6 +111,32 @@ public SchemaOnlyTool( Map parameters, Boolean strict, Boolean deferLoading) { + this(name, description, parameters, strict, deferLoading, null); + } + + /** + * Creates a new SchemaOnlyTool with the specified name, description, parameters, strict mode, + * deferred loading, and output schema. + * + *

The parameters and output schema maps are defensively copied to prevent external + * mutations from affecting the tool's internal state. + * + * @param name The tool name + * @param description The tool description + * @param parameters The tool parameters schema + * @param strict Whether the tool should use strict schema validation (null if unspecified) + * @param deferLoading Whether to defer loading this tool's schema until tool search (null if + * unspecified) + * @param outputSchema The structured output schema the tool declares, if any + * @throws NullPointerException if name or description is null + */ + public SchemaOnlyTool( + String name, + String description, + Map parameters, + Boolean strict, + Boolean deferLoading, + Map outputSchema) { super( ToolBase.builder() .name(Objects.requireNonNull(name, "name cannot be null")) @@ -123,6 +151,10 @@ public SchemaOnlyTool( .concurrencySafe(true)); this.strict = strict; this.deferLoading = deferLoading; + this.outputSchema = + outputSchema != null + ? Collections.unmodifiableMap(new HashMap<>(outputSchema)) + : null; } /** @@ -152,6 +184,11 @@ public Boolean getDeferLoading() { return deferLoading; } + @Override + public Map getOutputSchema() { + return outputSchema; + } + /** * Throws a ToolSuspendException to signal that this tool requires external execution. * diff --git a/agentscope-core/src/test/java/io/agentscope/core/tool/SchemaOnlyToolTest.java b/agentscope-core/src/test/java/io/agentscope/core/tool/SchemaOnlyToolTest.java index e99ee4f5f5..e24c79a394 100644 --- a/agentscope-core/src/test/java/io/agentscope/core/tool/SchemaOnlyToolTest.java +++ b/agentscope-core/src/test/java/io/agentscope/core/tool/SchemaOnlyToolTest.java @@ -89,6 +89,49 @@ void testDeferLoadingPreservedFromToolSchema() { assertEquals(Boolean.TRUE, tool.getDeferLoading()); } + @Test + @DisplayName("Should preserve outputSchema when creating from ToolSchema") + void testOutputSchemaPreservedFromToolSchema() { + Map outputSchema = + Map.of("type", "object", "properties", Map.of("answer", Map.of("type", "string"))); + ToolSchema schemaWithOutput = + ToolSchema.builder() + .name("external_lookup") + .description("Look something up externally") + .parameters(Map.of("type", "object", "properties", Map.of())) + .outputSchema(outputSchema) + .build(); + + SchemaOnlyTool tool = new SchemaOnlyTool(schemaWithOutput); + + assertEquals(outputSchema, tool.getOutputSchema()); + } + + @Test + @DisplayName("Should keep the declared outputSchema through Toolkit.getToolSchemas") + void testOutputSchemaSurvivesToolkitRegistration() { + Map outputSchema = + Map.of("type", "object", "properties", Map.of("answer", Map.of("type", "string"))); + ToolSchema schema = + ToolSchema.builder() + .name("external_lookup") + .description("Look something up externally") + .parameters(Map.of("type", "object", "properties", Map.of())) + .outputSchema(outputSchema) + .build(); + Toolkit toolkit = new Toolkit(); + + toolkit.registerSchema(schema); + + assertEquals( + outputSchema, + toolkit.getToolSchemas().stream() + .filter(exposed -> "external_lookup".equals(exposed.getName())) + .findFirst() + .orElseThrow() + .getOutputSchema()); + } + @Test @DisplayName("Should create SchemaOnlyTool with name, description, parameters") void testCreateWithParameters() {