Files
xiazemin c2f9ad7f34 agents: Add more robust response handling to executor.go (#1316)
* agents: fix handling of observation suffix in tool inputs

Add test coverage to ensure the executor properly trims nObservation:
suffix from tool inputs before passing them to tools. This prevents
tools from receiving malformed input that includes the observation
marker.

- Add TestExecutorTrimsObservationSuffix to verify trimming behavior
- Add test case in MRKL output parser for action inputs with observation suffix
- Add tools field to testAgent to support tool testing

---------

Co-authored-by: Travis Cline <travis.cline@gmail.com>
2025-06-29 01:09:12 +02:00

59 lines
1.4 KiB
Go

package agents
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/tmc/langchaingo/schema"
)
func TestMRKLOutputParser(t *testing.T) {
t.Parallel()
testCases := []struct {
input string
expectedActions []schema.AgentAction
expectedFinish *schema.AgentFinish
expectedErr error
}{
{
input: "Action: foo Action Input: bar",
expectedActions: []schema.AgentAction{{
Tool: "foo",
ToolInput: "bar",
Log: "Action: foo Action Input: bar",
}},
expectedFinish: nil,
expectedErr: nil,
},
{
input: "Action: foo\nAction Input:\nbar\nbaz",
expectedActions: []schema.AgentAction{{
Tool: "foo",
ToolInput: "bar\nbaz",
Log: "Action: foo\nAction Input:\nbar\nbaz",
}},
expectedFinish: nil,
expectedErr: nil,
},
{
input: "Action: calculator\nAction Input: 5 + 3\nObservation:",
expectedActions: []schema.AgentAction{{
Tool: "calculator",
ToolInput: "5 + 3\nObservation:",
Log: "Action: calculator\nAction Input: 5 + 3\nObservation:",
}},
expectedFinish: nil,
expectedErr: nil,
},
}
a := OneShotZeroAgent{}
for _, tc := range testCases {
actions, finish, err := a.parseOutput(tc.input)
require.ErrorIs(t, tc.expectedErr, err)
require.Equal(t, tc.expectedActions, actions)
require.Equal(t, tc.expectedFinish, finish)
}
}