Validate required input in agent tool execution
The agentTool.Execute method accepted {} despite the schema
marking input as required. Unlike functionTool, it skipped
required-field validation, silently sending an empty message
to the sub-agent.
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -75,8 +75,22 @@ func (t *agentTool) Execute(ctx context.Context, arguments string) (ToolResult,
|
|||||||
return ToolResult{}, &MaxToolDepthExceededError{MaxDepth: t.agent.maxToolDepth}
|
return ToolResult{}, &MaxToolDepthExceededError{MaxDepth: t.agent.maxToolDepth}
|
||||||
}
|
}
|
||||||
|
|
||||||
var params agentToolParams
|
var fields map[string]json.RawMessage
|
||||||
|
if err := json.Unmarshal([]byte(arguments), &fields); err != nil {
|
||||||
|
return ToolResult{
|
||||||
|
Content: fmt.Sprintf("Invalid parameters: %s", err.Error()),
|
||||||
|
IsError: true,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := fields["input"]; !ok {
|
||||||
|
return ToolResult{
|
||||||
|
Content: "Missing required parameters: input",
|
||||||
|
IsError: true,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var params agentToolParams
|
||||||
if err := json.Unmarshal([]byte(arguments), ¶ms); err != nil {
|
if err := json.Unmarshal([]byte(arguments), ¶ms); err != nil {
|
||||||
return ToolResult{
|
return ToolResult{
|
||||||
Content: fmt.Sprintf("Invalid parameters: %s", err.Error()),
|
Content: fmt.Sprintf("Invalid parameters: %s", err.Error()),
|
||||||
|
|||||||
@@ -195,19 +195,13 @@ func TestAgentTool_Execute(t *testing.T) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
t.Run(
|
t.Run(
|
||||||
"empty JSON object returns tool error",
|
"empty JSON object returns tool error for missing input",
|
||||||
func(t *testing.T) {
|
func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
provider := &mockProvider{
|
|
||||||
responses: []*llm.ChatCompletionResponse{
|
|
||||||
stopResponse("ok"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
ag := agent.New(
|
ag := agent.New(
|
||||||
"sub",
|
"sub",
|
||||||
newTestClient(provider),
|
newTestClient(&mockProvider{}),
|
||||||
agent.WithModel("test-model"),
|
agent.WithModel("test-model"),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -215,7 +209,9 @@ func TestAgentTool_Execute(t *testing.T) {
|
|||||||
result, err := tool.Execute(context.Background(), `{}`)
|
result, err := tool.Execute(context.Background(), `{}`)
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.False(t, result.IsError)
|
assert.True(t, result.IsError)
|
||||||
|
assert.Contains(t, result.Content, "Missing required parameters")
|
||||||
|
assert.Contains(t, result.Content, "input")
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user