fix(agent): web_fetch 兼容 LLM 双重序列化的 items 参数

main
asmh1989 2026-09-04 11:07:12 +08:00 committed by lyingbug
parent 2d6b84cb23
commit 5b057af467
2 changed files with 24 additions and 1 deletions

View File

@ -78,7 +78,14 @@ func (t *WebFetchTool) Execute(ctx context.Context, args json.RawMessage) (*type
logger.Infof(ctx, "[Tool][WebFetch] Execute started")
var input WebFetchInput
if err := json.Unmarshal(args, &input); err != nil {
return &types.ToolResult{Success: false, Error: fmt.Sprintf("failed to parse args: %v", err)}, err
// Some models double-encode the items array as a JSON string; unwrap and retry.
var wrapped struct {
Items string `json:"items"`
}
if json.Unmarshal(args, &wrapped) != nil || json.Unmarshal([]byte(wrapped.Items), &input.Items) != nil || len(input.Items) == 0 {
return &types.ToolResult{Success: false, Error: fmt.Sprintf("failed to parse args: %v", err)}, err
}
logger.Warnf(ctx, "[Tool][WebFetch] Unwrapped double-encoded items string (%d item(s))", len(input.Items))
}
if len(input.Items) == 0 {
return &types.ToolResult{Success: false, Error: "missing required parameter: items"}, nil

View File

@ -129,6 +129,22 @@ func TestWebFetchToolDeduplicatesGitHubBlobAndRawURLs(t *testing.T) {
assert.Equal(t, 1, result.Data["skipped_count"])
}
func TestWebFetchToolUnwrapsDoubleEncodedItemsString(t *testing.T) {
const rawURL = "https://example.com/article"
fetcher := newStubWebContentFetcher(map[string]string{rawURL: "article content"}, nil)
tool := newWebFetchTool(nil, fetcher)
// Some models emit {"items":"[{\"url\":...}]"} instead of a real array.
itemsJSON, _ := json.Marshal([]WebFetchItem{{URL: rawURL, Prompt: "extract facts"}})
encoded, _ := json.Marshal(map[string]string{"items": string(itemsJSON)})
result, err := tool.Execute(context.Background(), encoded)
require.NoError(t, err)
require.True(t, result.Success)
assert.Equal(t, 1, fetcher.callCount[rawURL])
}
func newStubWebContentFetcher(contents map[string]string, failures map[string]error) *stubWebContentFetcher {
return &stubWebContentFetcher{
contents: contents,