diff --git a/internal/agent/tools/web_fetch.go b/internal/agent/tools/web_fetch.go index 31a36c2d..a7ec6aff 100644 --- a/internal/agent/tools/web_fetch.go +++ b/internal/agent/tools/web_fetch.go @@ -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 diff --git a/internal/agent/tools/web_fetch_test.go b/internal/agent/tools/web_fetch_test.go index df716674..31b9c59f 100644 --- a/internal/agent/tools/web_fetch_test.go +++ b/internal/agent/tools/web_fetch_test.go @@ -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,