(signal: AbortSignal)
| 74 | } |
| 75 | |
| 76 | private async executeFallback(signal: AbortSignal): Promise<ToolResult> { |
| 77 | const urls = extractUrls(this.params.prompt); |
| 78 | if (urls.length === 0) { |
| 79 | return { |
| 80 | llmContent: 'Error: No URL found in the prompt for fallback.', |
| 81 | returnDisplay: 'Error: No URL found in the prompt for fallback.', |
| 82 | }; |
| 83 | } |
| 84 | // For now, we only support one URL for fallback |
| 85 | let url = urls[0]; |
| 86 | |
| 87 | // Convert GitHub blob URL to raw URL |
| 88 | if (url.includes('github.com') && url.includes('/blob/')) { |
| 89 | url = url |
| 90 | .replace('github.com', 'raw.githubusercontent.com') |
| 91 | .replace('/blob/', '/'); |
| 92 | } |
| 93 | |
| 94 | try { |
| 95 | const response = await fetchWithTimeout(url, URL_FETCH_TIMEOUT_MS); |
| 96 | if (!response.ok) { |
| 97 | throw new Error( |
| 98 | `Request failed with status code ${response.status} ${response.statusText}`, |
| 99 | ); |
| 100 | } |
| 101 | const html = await response.text(); |
| 102 | const textContent = convert(html, { |
| 103 | wordwrap: false, |
| 104 | selectors: [ |
| 105 | { selector: 'a', options: { ignoreHref: true } }, |
| 106 | { selector: 'img', format: 'skip' }, |
| 107 | ], |
| 108 | }).substring(0, MAX_CONTENT_LENGTH); |
| 109 | |
| 110 | const anusClient = this.config.getAnusClient(); |
| 111 | const fallbackPrompt = `The user requested the following: "${this.params.prompt}". |
| 112 | |
| 113 | I was unable to access the URL directly. Instead, I have fetched the raw content of the page. Please use the following content to answer the request. Do not attempt to access the URL again. |
| 114 | |
| 115 | --- |
| 116 | ${textContent} |
| 117 | --- |
| 118 | `; |
| 119 | const result = await anusClient.generateContent( |
| 120 | [{ role: 'user', parts: [{ text: fallbackPrompt }] }], |
| 121 | {}, |
| 122 | signal, |
| 123 | ); |
| 124 | const resultText = getResponseText(result) || ''; |
| 125 | return { |
| 126 | llmContent: resultText, |
| 127 | returnDisplay: `Content for ${url} processed using fallback fetch.`, |
| 128 | }; |
| 129 | } catch (e) { |
| 130 | const error = e as Error; |
| 131 | const errorMessage = `Error during fallback fetch for ${url}: ${error.message}`; |
| 132 | return { |
| 133 | llmContent: `Error: ${errorMessage}`, |
no test coverage detected