fix(webfetch): avoid rewriting successful redirect content
This commit is contained in:
@@ -56,6 +56,10 @@ function isRedirectLoopError(output: string): boolean {
|
||||
return WEBFETCH_REDIRECT_ERROR_PATTERNS.some((pattern) => pattern.test(output))
|
||||
}
|
||||
|
||||
function isToolErrorOutput(output: string): boolean {
|
||||
return output.trimStart().toLowerCase().startsWith("error:")
|
||||
}
|
||||
|
||||
function buildRedirectLimitMessage(url?: string): string {
|
||||
const suffix = url ? ` for ${url}` : ""
|
||||
return `Error: WebFetch failed: exceeded maximum redirects (${MAX_WEBFETCH_REDIRECTS})${suffix}`
|
||||
@@ -111,7 +115,7 @@ export function createWebFetchRedirectGuardHook(_ctx: PluginInput) {
|
||||
return
|
||||
}
|
||||
|
||||
if (isRedirectLoopError(output.output)) {
|
||||
if (isToolErrorOutput(output.output) && isRedirectLoopError(output.output)) {
|
||||
output.output = buildRedirectLimitMessage()
|
||||
}
|
||||
},
|
||||
|
||||
@@ -49,7 +49,7 @@ function createFetchMock(
|
||||
implementation: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>,
|
||||
): typeof fetch {
|
||||
return Object.assign(implementation, {
|
||||
preconnect: originalFetch.preconnect,
|
||||
preconnect: Reflect.get(originalFetch, "preconnect"),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -152,6 +152,20 @@ describe("createWebFetchRedirectGuardHook", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("#when successful fetched content mentions redirect loops", () => {
|
||||
it("#then should keep the content unchanged", async () => {
|
||||
const hook = createWebFetchRedirectGuardHook({} as never)
|
||||
const input = createInput()
|
||||
const output = createAfterOutput("This page explains why browsers hit too many redirects in some setups.")
|
||||
|
||||
await hook["tool.execute.after"](input, output)
|
||||
|
||||
expect(output.output).toBe(
|
||||
"This page explains why browsers hit too many redirects in some setups.",
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given a non-webfetch tool", () => {
|
||||
|
||||
Reference in New Issue
Block a user