@oh-my-pi/pi-coding-agent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (337) hide show
  1. package/CHANGELOG.md +1629 -0
  2. package/README.md +1041 -0
  3. package/docs/compaction.md +403 -0
  4. package/docs/config-usage.md +113 -0
  5. package/docs/custom-tools.md +541 -0
  6. package/docs/extension-loading.md +1004 -0
  7. package/docs/hooks.md +867 -0
  8. package/docs/rpc.md +1040 -0
  9. package/docs/sdk.md +994 -0
  10. package/docs/session-tree-plan.md +441 -0
  11. package/docs/session.md +240 -0
  12. package/docs/skills.md +290 -0
  13. package/docs/theme.md +670 -0
  14. package/docs/tree.md +197 -0
  15. package/docs/tui.md +341 -0
  16. package/examples/README.md +21 -0
  17. package/examples/custom-tools/README.md +124 -0
  18. package/examples/custom-tools/hello/index.ts +20 -0
  19. package/examples/custom-tools/question/index.ts +84 -0
  20. package/examples/custom-tools/subagent/README.md +172 -0
  21. package/examples/custom-tools/subagent/agents/planner.md +37 -0
  22. package/examples/custom-tools/subagent/agents/scout.md +50 -0
  23. package/examples/custom-tools/subagent/agents/worker.md +24 -0
  24. package/examples/custom-tools/subagent/agents.ts +156 -0
  25. package/examples/custom-tools/subagent/commands/implement-and-review.md +10 -0
  26. package/examples/custom-tools/subagent/commands/implement.md +10 -0
  27. package/examples/custom-tools/subagent/commands/scout-and-plan.md +9 -0
  28. package/examples/custom-tools/subagent/index.ts +1002 -0
  29. package/examples/custom-tools/todo/index.ts +212 -0
  30. package/examples/hooks/README.md +56 -0
  31. package/examples/hooks/auto-commit-on-exit.ts +49 -0
  32. package/examples/hooks/confirm-destructive.ts +59 -0
  33. package/examples/hooks/custom-compaction.ts +116 -0
  34. package/examples/hooks/dirty-repo-guard.ts +52 -0
  35. package/examples/hooks/file-trigger.ts +41 -0
  36. package/examples/hooks/git-checkpoint.ts +53 -0
  37. package/examples/hooks/handoff.ts +150 -0
  38. package/examples/hooks/permission-gate.ts +34 -0
  39. package/examples/hooks/protected-paths.ts +30 -0
  40. package/examples/hooks/qna.ts +119 -0
  41. package/examples/hooks/snake.ts +343 -0
  42. package/examples/hooks/status-line.ts +40 -0
  43. package/examples/sdk/01-minimal.ts +22 -0
  44. package/examples/sdk/02-custom-model.ts +49 -0
  45. package/examples/sdk/03-custom-prompt.ts +44 -0
  46. package/examples/sdk/04-skills.ts +44 -0
  47. package/examples/sdk/05-tools.ts +90 -0
  48. package/examples/sdk/06-hooks.ts +61 -0
  49. package/examples/sdk/07-context-files.ts +36 -0
  50. package/examples/sdk/08-slash-commands.ts +42 -0
  51. package/examples/sdk/09-api-keys-and-oauth.ts +55 -0
  52. package/examples/sdk/10-settings.ts +38 -0
  53. package/examples/sdk/11-sessions.ts +48 -0
  54. package/examples/sdk/12-full-control.ts +95 -0
  55. package/examples/sdk/README.md +154 -0
  56. package/package.json +89 -0
  57. package/src/bun-imports.d.ts +16 -0
  58. package/src/capability/context-file.ts +40 -0
  59. package/src/capability/extension.ts +48 -0
  60. package/src/capability/hook.ts +40 -0
  61. package/src/capability/index.ts +616 -0
  62. package/src/capability/instruction.ts +37 -0
  63. package/src/capability/mcp.ts +52 -0
  64. package/src/capability/prompt.ts +35 -0
  65. package/src/capability/rule.ts +56 -0
  66. package/src/capability/settings.ts +35 -0
  67. package/src/capability/skill.ts +49 -0
  68. package/src/capability/slash-command.ts +40 -0
  69. package/src/capability/system-prompt.ts +35 -0
  70. package/src/capability/tool.ts +38 -0
  71. package/src/capability/types.ts +166 -0
  72. package/src/cli/args.ts +259 -0
  73. package/src/cli/file-processor.ts +121 -0
  74. package/src/cli/list-models.ts +104 -0
  75. package/src/cli/plugin-cli.ts +661 -0
  76. package/src/cli/session-picker.ts +41 -0
  77. package/src/cli/update-cli.ts +274 -0
  78. package/src/cli.ts +10 -0
  79. package/src/config.ts +391 -0
  80. package/src/core/agent-session.ts +2178 -0
  81. package/src/core/auth-storage.ts +258 -0
  82. package/src/core/bash-executor.ts +197 -0
  83. package/src/core/compaction/branch-summarization.ts +315 -0
  84. package/src/core/compaction/compaction.ts +664 -0
  85. package/src/core/compaction/index.ts +7 -0
  86. package/src/core/compaction/utils.ts +153 -0
  87. package/src/core/custom-commands/bundled/review/index.ts +156 -0
  88. package/src/core/custom-commands/index.ts +15 -0
  89. package/src/core/custom-commands/loader.ts +226 -0
  90. package/src/core/custom-commands/types.ts +112 -0
  91. package/src/core/custom-tools/index.ts +22 -0
  92. package/src/core/custom-tools/loader.ts +248 -0
  93. package/src/core/custom-tools/types.ts +185 -0
  94. package/src/core/custom-tools/wrapper.ts +29 -0
  95. package/src/core/exec.ts +139 -0
  96. package/src/core/export-html/index.ts +159 -0
  97. package/src/core/export-html/template.css +774 -0
  98. package/src/core/export-html/template.generated.ts +2 -0
  99. package/src/core/export-html/template.html +45 -0
  100. package/src/core/export-html/template.js +1185 -0
  101. package/src/core/export-html/template.macro.ts +24 -0
  102. package/src/core/file-mentions.ts +54 -0
  103. package/src/core/hooks/index.ts +16 -0
  104. package/src/core/hooks/loader.ts +288 -0
  105. package/src/core/hooks/runner.ts +434 -0
  106. package/src/core/hooks/tool-wrapper.ts +98 -0
  107. package/src/core/hooks/types.ts +770 -0
  108. package/src/core/index.ts +53 -0
  109. package/src/core/logger.ts +112 -0
  110. package/src/core/mcp/client.ts +185 -0
  111. package/src/core/mcp/config.ts +248 -0
  112. package/src/core/mcp/index.ts +45 -0
  113. package/src/core/mcp/loader.ts +99 -0
  114. package/src/core/mcp/manager.ts +235 -0
  115. package/src/core/mcp/tool-bridge.ts +156 -0
  116. package/src/core/mcp/transports/http.ts +316 -0
  117. package/src/core/mcp/transports/index.ts +6 -0
  118. package/src/core/mcp/transports/stdio.ts +252 -0
  119. package/src/core/mcp/types.ts +228 -0
  120. package/src/core/messages.ts +211 -0
  121. package/src/core/model-registry.ts +334 -0
  122. package/src/core/model-resolver.ts +494 -0
  123. package/src/core/plugins/doctor.ts +67 -0
  124. package/src/core/plugins/index.ts +38 -0
  125. package/src/core/plugins/installer.ts +189 -0
  126. package/src/core/plugins/loader.ts +339 -0
  127. package/src/core/plugins/manager.ts +672 -0
  128. package/src/core/plugins/parser.ts +105 -0
  129. package/src/core/plugins/paths.ts +37 -0
  130. package/src/core/plugins/types.ts +190 -0
  131. package/src/core/sdk.ts +900 -0
  132. package/src/core/session-manager.ts +1837 -0
  133. package/src/core/settings-manager.ts +860 -0
  134. package/src/core/skills.ts +352 -0
  135. package/src/core/slash-commands.ts +132 -0
  136. package/src/core/system-prompt.ts +442 -0
  137. package/src/core/timings.ts +25 -0
  138. package/src/core/title-generator.ts +110 -0
  139. package/src/core/tools/ask.ts +193 -0
  140. package/src/core/tools/bash-interceptor.ts +120 -0
  141. package/src/core/tools/bash.ts +91 -0
  142. package/src/core/tools/context.ts +32 -0
  143. package/src/core/tools/edit-diff.ts +487 -0
  144. package/src/core/tools/edit.ts +140 -0
  145. package/src/core/tools/exa/company.ts +59 -0
  146. package/src/core/tools/exa/index.ts +63 -0
  147. package/src/core/tools/exa/linkedin.ts +59 -0
  148. package/src/core/tools/exa/mcp-client.ts +368 -0
  149. package/src/core/tools/exa/render.ts +200 -0
  150. package/src/core/tools/exa/researcher.ts +90 -0
  151. package/src/core/tools/exa/search.ts +338 -0
  152. package/src/core/tools/exa/types.ts +167 -0
  153. package/src/core/tools/exa/websets.ts +248 -0
  154. package/src/core/tools/find.ts +244 -0
  155. package/src/core/tools/grep.ts +584 -0
  156. package/src/core/tools/index.ts +283 -0
  157. package/src/core/tools/ls.ts +142 -0
  158. package/src/core/tools/lsp/client.ts +767 -0
  159. package/src/core/tools/lsp/clients/biome-client.ts +207 -0
  160. package/src/core/tools/lsp/clients/index.ts +49 -0
  161. package/src/core/tools/lsp/clients/lsp-linter-client.ts +98 -0
  162. package/src/core/tools/lsp/config.ts +845 -0
  163. package/src/core/tools/lsp/edits.ts +110 -0
  164. package/src/core/tools/lsp/index.ts +1364 -0
  165. package/src/core/tools/lsp/render.ts +560 -0
  166. package/src/core/tools/lsp/rust-analyzer.ts +145 -0
  167. package/src/core/tools/lsp/types.ts +495 -0
  168. package/src/core/tools/lsp/utils.ts +526 -0
  169. package/src/core/tools/notebook.ts +182 -0
  170. package/src/core/tools/output.ts +198 -0
  171. package/src/core/tools/path-utils.ts +61 -0
  172. package/src/core/tools/read.ts +507 -0
  173. package/src/core/tools/renderers.ts +820 -0
  174. package/src/core/tools/review.ts +275 -0
  175. package/src/core/tools/rulebook.ts +124 -0
  176. package/src/core/tools/task/agents.ts +158 -0
  177. package/src/core/tools/task/artifacts.ts +114 -0
  178. package/src/core/tools/task/commands.ts +157 -0
  179. package/src/core/tools/task/discovery.ts +217 -0
  180. package/src/core/tools/task/executor.ts +531 -0
  181. package/src/core/tools/task/index.ts +548 -0
  182. package/src/core/tools/task/model-resolver.ts +176 -0
  183. package/src/core/tools/task/parallel.ts +38 -0
  184. package/src/core/tools/task/render.ts +502 -0
  185. package/src/core/tools/task/subprocess-tool-registry.ts +89 -0
  186. package/src/core/tools/task/types.ts +142 -0
  187. package/src/core/tools/truncate.ts +265 -0
  188. package/src/core/tools/web-fetch.ts +2511 -0
  189. package/src/core/tools/web-search/auth.ts +199 -0
  190. package/src/core/tools/web-search/index.ts +583 -0
  191. package/src/core/tools/web-search/providers/anthropic.ts +198 -0
  192. package/src/core/tools/web-search/providers/exa.ts +196 -0
  193. package/src/core/tools/web-search/providers/perplexity.ts +195 -0
  194. package/src/core/tools/web-search/render.ts +372 -0
  195. package/src/core/tools/web-search/types.ts +180 -0
  196. package/src/core/tools/write.ts +63 -0
  197. package/src/core/ttsr.ts +211 -0
  198. package/src/core/utils.ts +187 -0
  199. package/src/discovery/agents-md.ts +75 -0
  200. package/src/discovery/builtin.ts +647 -0
  201. package/src/discovery/claude.ts +623 -0
  202. package/src/discovery/cline.ts +104 -0
  203. package/src/discovery/codex.ts +571 -0
  204. package/src/discovery/cursor.ts +266 -0
  205. package/src/discovery/gemini.ts +368 -0
  206. package/src/discovery/github.ts +120 -0
  207. package/src/discovery/helpers.test.ts +127 -0
  208. package/src/discovery/helpers.ts +249 -0
  209. package/src/discovery/index.ts +84 -0
  210. package/src/discovery/mcp-json.ts +127 -0
  211. package/src/discovery/vscode.ts +99 -0
  212. package/src/discovery/windsurf.ts +219 -0
  213. package/src/index.ts +192 -0
  214. package/src/main.ts +507 -0
  215. package/src/migrations.ts +156 -0
  216. package/src/modes/cleanup.ts +23 -0
  217. package/src/modes/index.ts +48 -0
  218. package/src/modes/interactive/components/armin.ts +382 -0
  219. package/src/modes/interactive/components/assistant-message.ts +86 -0
  220. package/src/modes/interactive/components/bash-execution.ts +199 -0
  221. package/src/modes/interactive/components/bordered-loader.ts +41 -0
  222. package/src/modes/interactive/components/branch-summary-message.ts +42 -0
  223. package/src/modes/interactive/components/compaction-summary-message.ts +45 -0
  224. package/src/modes/interactive/components/custom-editor.ts +122 -0
  225. package/src/modes/interactive/components/diff.ts +147 -0
  226. package/src/modes/interactive/components/dynamic-border.ts +25 -0
  227. package/src/modes/interactive/components/extensions/extension-dashboard.ts +296 -0
  228. package/src/modes/interactive/components/extensions/extension-list.ts +479 -0
  229. package/src/modes/interactive/components/extensions/index.ts +9 -0
  230. package/src/modes/interactive/components/extensions/inspector-panel.ts +313 -0
  231. package/src/modes/interactive/components/extensions/state-manager.ts +558 -0
  232. package/src/modes/interactive/components/extensions/types.ts +191 -0
  233. package/src/modes/interactive/components/hook-editor.ts +117 -0
  234. package/src/modes/interactive/components/hook-input.ts +64 -0
  235. package/src/modes/interactive/components/hook-message.ts +96 -0
  236. package/src/modes/interactive/components/hook-selector.ts +91 -0
  237. package/src/modes/interactive/components/model-selector.ts +560 -0
  238. package/src/modes/interactive/components/oauth-selector.ts +136 -0
  239. package/src/modes/interactive/components/plugin-settings.ts +481 -0
  240. package/src/modes/interactive/components/queue-mode-selector.ts +56 -0
  241. package/src/modes/interactive/components/session-selector.ts +220 -0
  242. package/src/modes/interactive/components/settings-defs.ts +597 -0
  243. package/src/modes/interactive/components/settings-selector.ts +545 -0
  244. package/src/modes/interactive/components/show-images-selector.ts +45 -0
  245. package/src/modes/interactive/components/status-line/index.ts +4 -0
  246. package/src/modes/interactive/components/status-line/presets.ts +94 -0
  247. package/src/modes/interactive/components/status-line/segments.ts +350 -0
  248. package/src/modes/interactive/components/status-line/separators.ts +55 -0
  249. package/src/modes/interactive/components/status-line/types.ts +81 -0
  250. package/src/modes/interactive/components/status-line-segment-editor.ts +357 -0
  251. package/src/modes/interactive/components/status-line.ts +384 -0
  252. package/src/modes/interactive/components/theme-selector.ts +62 -0
  253. package/src/modes/interactive/components/thinking-selector.ts +64 -0
  254. package/src/modes/interactive/components/tool-execution.ts +946 -0
  255. package/src/modes/interactive/components/tree-selector.ts +877 -0
  256. package/src/modes/interactive/components/ttsr-notification.ts +82 -0
  257. package/src/modes/interactive/components/user-message-selector.ts +159 -0
  258. package/src/modes/interactive/components/user-message.ts +18 -0
  259. package/src/modes/interactive/components/visual-truncate.ts +50 -0
  260. package/src/modes/interactive/components/welcome.ts +228 -0
  261. package/src/modes/interactive/interactive-mode.ts +2669 -0
  262. package/src/modes/interactive/theme/dark.json +102 -0
  263. package/src/modes/interactive/theme/defaults/dark-arctic.json +111 -0
  264. package/src/modes/interactive/theme/defaults/dark-catppuccin.json +106 -0
  265. package/src/modes/interactive/theme/defaults/dark-cyberpunk.json +109 -0
  266. package/src/modes/interactive/theme/defaults/dark-dracula.json +105 -0
  267. package/src/modes/interactive/theme/defaults/dark-forest.json +103 -0
  268. package/src/modes/interactive/theme/defaults/dark-github.json +112 -0
  269. package/src/modes/interactive/theme/defaults/dark-gruvbox.json +119 -0
  270. package/src/modes/interactive/theme/defaults/dark-monochrome.json +101 -0
  271. package/src/modes/interactive/theme/defaults/dark-monokai.json +105 -0
  272. package/src/modes/interactive/theme/defaults/dark-nord.json +104 -0
  273. package/src/modes/interactive/theme/defaults/dark-ocean.json +108 -0
  274. package/src/modes/interactive/theme/defaults/dark-one.json +107 -0
  275. package/src/modes/interactive/theme/defaults/dark-retro.json +99 -0
  276. package/src/modes/interactive/theme/defaults/dark-rose-pine.json +95 -0
  277. package/src/modes/interactive/theme/defaults/dark-solarized.json +96 -0
  278. package/src/modes/interactive/theme/defaults/dark-sunset.json +106 -0
  279. package/src/modes/interactive/theme/defaults/dark-synthwave.json +102 -0
  280. package/src/modes/interactive/theme/defaults/dark-tokyo-night.json +108 -0
  281. package/src/modes/interactive/theme/defaults/index.ts +67 -0
  282. package/src/modes/interactive/theme/defaults/light-arctic.json +106 -0
  283. package/src/modes/interactive/theme/defaults/light-catppuccin.json +105 -0
  284. package/src/modes/interactive/theme/defaults/light-cyberpunk.json +103 -0
  285. package/src/modes/interactive/theme/defaults/light-forest.json +107 -0
  286. package/src/modes/interactive/theme/defaults/light-github.json +114 -0
  287. package/src/modes/interactive/theme/defaults/light-gruvbox.json +115 -0
  288. package/src/modes/interactive/theme/defaults/light-monochrome.json +100 -0
  289. package/src/modes/interactive/theme/defaults/light-ocean.json +106 -0
  290. package/src/modes/interactive/theme/defaults/light-one.json +105 -0
  291. package/src/modes/interactive/theme/defaults/light-retro.json +105 -0
  292. package/src/modes/interactive/theme/defaults/light-solarized.json +101 -0
  293. package/src/modes/interactive/theme/defaults/light-sunset.json +106 -0
  294. package/src/modes/interactive/theme/defaults/light-synthwave.json +105 -0
  295. package/src/modes/interactive/theme/defaults/light-tokyo-night.json +118 -0
  296. package/src/modes/interactive/theme/light.json +99 -0
  297. package/src/modes/interactive/theme/theme-schema.json +424 -0
  298. package/src/modes/interactive/theme/theme.ts +2211 -0
  299. package/src/modes/print-mode.ts +163 -0
  300. package/src/modes/rpc/rpc-client.ts +527 -0
  301. package/src/modes/rpc/rpc-mode.ts +494 -0
  302. package/src/modes/rpc/rpc-types.ts +203 -0
  303. package/src/prompts/architect-plan.md +10 -0
  304. package/src/prompts/branch-summary-preamble.md +3 -0
  305. package/src/prompts/branch-summary.md +28 -0
  306. package/src/prompts/browser.md +71 -0
  307. package/src/prompts/compaction-summary.md +34 -0
  308. package/src/prompts/compaction-turn-prefix.md +16 -0
  309. package/src/prompts/compaction-update-summary.md +41 -0
  310. package/src/prompts/explore.md +82 -0
  311. package/src/prompts/implement-with-critic.md +11 -0
  312. package/src/prompts/implement.md +11 -0
  313. package/src/prompts/init.md +30 -0
  314. package/src/prompts/plan.md +54 -0
  315. package/src/prompts/reviewer.md +81 -0
  316. package/src/prompts/summarization-system.md +3 -0
  317. package/src/prompts/system-prompt.md +27 -0
  318. package/src/prompts/task.md +56 -0
  319. package/src/prompts/title-system.md +8 -0
  320. package/src/prompts/tools/ask.md +24 -0
  321. package/src/prompts/tools/bash.md +23 -0
  322. package/src/prompts/tools/edit.md +9 -0
  323. package/src/prompts/tools/find.md +6 -0
  324. package/src/prompts/tools/grep.md +12 -0
  325. package/src/prompts/tools/lsp.md +14 -0
  326. package/src/prompts/tools/output.md +23 -0
  327. package/src/prompts/tools/read.md +25 -0
  328. package/src/prompts/tools/web-fetch.md +8 -0
  329. package/src/prompts/tools/web-search.md +10 -0
  330. package/src/prompts/tools/write.md +10 -0
  331. package/src/utils/changelog.ts +99 -0
  332. package/src/utils/clipboard.ts +265 -0
  333. package/src/utils/fuzzy.ts +108 -0
  334. package/src/utils/mime.ts +30 -0
  335. package/src/utils/shell-snapshot.ts +218 -0
  336. package/src/utils/shell.ts +364 -0
  337. package/src/utils/tools-manager.ts +265 -0
@@ -0,0 +1,2511 @@
1
+ import { spawnSync } from "node:child_process";
2
+ import * as fs from "node:fs";
3
+ import * as os from "node:os";
4
+ import * as path from "node:path";
5
+ import type { AgentTool } from "@oh-my-pi/pi-agent-core";
6
+ import { Type } from "@sinclair/typebox";
7
+ import { parse as parseHtml } from "node-html-parser";
8
+ import webFetchDescription from "../../prompts/tools/web-fetch.md" with { type: "text" };
9
+ import { logger } from "../logger";
10
+
11
+ // =============================================================================
12
+ // Types and Constants
13
+ // =============================================================================
14
+
15
+ interface RenderResult {
16
+ url: string;
17
+ finalUrl: string;
18
+ contentType: string;
19
+ method: string;
20
+ content: string;
21
+ fetchedAt: string;
22
+ truncated: boolean;
23
+ notes: string[];
24
+ }
25
+
26
+ const DEFAULT_TIMEOUT = 20;
27
+ const MAX_BYTES = 50 * 1024 * 1024; // 50MB for binary files
28
+ const MAX_OUTPUT_CHARS = 500_000;
29
+
30
+ // Convertible document types (markitdown supported)
31
+ const CONVERTIBLE_MIMES = new Set([
32
+ "application/pdf",
33
+ "application/msword",
34
+ "application/vnd.ms-powerpoint",
35
+ "application/vnd.ms-excel",
36
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
37
+ "application/vnd.openxmlformats-officedocument.presentationml.presentation",
38
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
39
+ "application/rtf",
40
+ "application/epub+zip",
41
+ "application/zip",
42
+ "image/png",
43
+ "image/jpeg",
44
+ "image/gif",
45
+ "image/webp",
46
+ "audio/mpeg",
47
+ "audio/wav",
48
+ "audio/ogg",
49
+ ]);
50
+
51
+ const CONVERTIBLE_EXTENSIONS = new Set([
52
+ ".pdf",
53
+ ".doc",
54
+ ".docx",
55
+ ".ppt",
56
+ ".pptx",
57
+ ".xls",
58
+ ".xlsx",
59
+ ".rtf",
60
+ ".epub",
61
+ ".png",
62
+ ".jpg",
63
+ ".jpeg",
64
+ ".gif",
65
+ ".webp",
66
+ ".mp3",
67
+ ".wav",
68
+ ".ogg",
69
+ ]);
70
+
71
+ const isWindows = process.platform === "win32";
72
+
73
+ const USER_AGENTS = [
74
+ "curl/8.0",
75
+ "Mozilla/5.0 (compatible; TextBot/1.0)",
76
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
77
+ ];
78
+
79
+ // =============================================================================
80
+ // Utilities
81
+ // =============================================================================
82
+
83
+ interface LoadPageResult {
84
+ content: string;
85
+ contentType: string;
86
+ finalUrl: string;
87
+ ok: boolean;
88
+ status?: number;
89
+ }
90
+
91
+ interface LoadPageOptions {
92
+ timeout?: number;
93
+ headers?: Record<string, string>;
94
+ maxBytes?: number;
95
+ }
96
+
97
+ /**
98
+ * Check if response indicates bot blocking (Cloudflare, etc.)
99
+ */
100
+ function isBotBlocked(status: number, content: string): boolean {
101
+ if (status === 403 || status === 503) {
102
+ const lower = content.toLowerCase();
103
+ return (
104
+ lower.includes("cloudflare") ||
105
+ lower.includes("captcha") ||
106
+ lower.includes("challenge") ||
107
+ lower.includes("blocked") ||
108
+ lower.includes("access denied") ||
109
+ lower.includes("bot detection")
110
+ );
111
+ }
112
+ return false;
113
+ }
114
+
115
+ /**
116
+ * Fetch a page with timeout, size limit, and automatic retry with browser UA if blocked
117
+ */
118
+ async function loadPage(url: string, options: LoadPageOptions = {}): Promise<LoadPageResult> {
119
+ const { timeout = 20, headers = {}, maxBytes = MAX_BYTES } = options;
120
+
121
+ for (let attempt = 0; attempt < USER_AGENTS.length; attempt++) {
122
+ const userAgent = USER_AGENTS[attempt];
123
+
124
+ try {
125
+ const controller = new AbortController();
126
+ const timeoutId = setTimeout(() => controller.abort(), timeout * 1000);
127
+
128
+ const response = await fetch(url, {
129
+ signal: controller.signal,
130
+ headers: {
131
+ "User-Agent": userAgent,
132
+ Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
133
+ "Accept-Language": "en-US,en;q=0.5",
134
+ ...headers,
135
+ },
136
+ redirect: "follow",
137
+ });
138
+
139
+ clearTimeout(timeoutId);
140
+
141
+ const contentType = response.headers.get("content-type")?.split(";")[0]?.trim().toLowerCase() ?? "";
142
+ const finalUrl = response.url;
143
+
144
+ // Read with size limit
145
+ const reader = response.body?.getReader();
146
+ if (!reader) {
147
+ return { content: "", contentType, finalUrl, ok: false, status: response.status };
148
+ }
149
+
150
+ const chunks: Uint8Array[] = [];
151
+ let totalSize = 0;
152
+
153
+ while (true) {
154
+ const { done, value } = await reader.read();
155
+ if (done) break;
156
+
157
+ chunks.push(value);
158
+ totalSize += value.length;
159
+
160
+ if (totalSize > maxBytes) {
161
+ reader.cancel();
162
+ break;
163
+ }
164
+ }
165
+
166
+ const decoder = new TextDecoder();
167
+ const content = decoder.decode(Buffer.concat(chunks));
168
+
169
+ // Check if we got blocked and should retry with browser UA
170
+ if (isBotBlocked(response.status, content) && attempt < USER_AGENTS.length - 1) {
171
+ continue;
172
+ }
173
+
174
+ if (!response.ok) {
175
+ return { content, contentType, finalUrl, ok: false, status: response.status };
176
+ }
177
+
178
+ return { content, contentType, finalUrl, ok: true, status: response.status };
179
+ } catch (err) {
180
+ // On last attempt, return failure
181
+ if (attempt === USER_AGENTS.length - 1) {
182
+ logger.debug("Web fetch failed after retries", { url, error: String(err) });
183
+ return { content: "", contentType: "", finalUrl: url, ok: false };
184
+ }
185
+ // Otherwise retry with next UA
186
+ }
187
+ }
188
+
189
+ return { content: "", contentType: "", finalUrl: url, ok: false };
190
+ }
191
+
192
+ /**
193
+ * Execute a command and return stdout
194
+ */
195
+ function exec(
196
+ cmd: string,
197
+ args: string[],
198
+ options?: { timeout?: number; input?: string | Buffer },
199
+ ): { stdout: string; stderr: string; ok: boolean } {
200
+ const timeout = (options?.timeout ?? DEFAULT_TIMEOUT) * 1000;
201
+ const result = spawnSync(cmd, args, {
202
+ encoding: options?.input instanceof Buffer ? "buffer" : "utf-8",
203
+ timeout,
204
+ maxBuffer: MAX_BYTES,
205
+ input: options?.input,
206
+ shell: true,
207
+ });
208
+ return {
209
+ stdout: result.stdout?.toString() ?? "",
210
+ stderr: result.stderr?.toString() ?? "",
211
+ ok: result.status === 0,
212
+ };
213
+ }
214
+
215
+ /**
216
+ * Check if a command exists (cross-platform)
217
+ */
218
+ function hasCommand(cmd: string): boolean {
219
+ const checkCmd = isWindows ? "where" : "which";
220
+ const result = spawnSync(checkCmd, [cmd], { encoding: "utf-8", shell: true });
221
+ return result.status === 0;
222
+ }
223
+
224
+ /**
225
+ * Extract origin from URL
226
+ */
227
+ function getOrigin(url: string): string {
228
+ try {
229
+ const parsed = new URL(url);
230
+ return `${parsed.protocol}//${parsed.host}`;
231
+ } catch {
232
+ return "";
233
+ }
234
+ }
235
+
236
+ /**
237
+ * Normalize URL (add scheme if missing)
238
+ */
239
+ function normalizeUrl(url: string): string {
240
+ if (!url.match(/^https?:\/\//i)) {
241
+ return `https://${url}`;
242
+ }
243
+ return url;
244
+ }
245
+
246
+ /**
247
+ * Normalize MIME type (lowercase, strip charset/params)
248
+ */
249
+ function normalizeMime(contentType: string): string {
250
+ return contentType.split(";")[0].trim().toLowerCase();
251
+ }
252
+
253
+ /**
254
+ * Get extension from URL or Content-Disposition
255
+ */
256
+ function getExtensionHint(url: string, contentDisposition?: string): string {
257
+ // Try Content-Disposition filename first
258
+ if (contentDisposition) {
259
+ const match = contentDisposition.match(/filename[*]?=["']?([^"';\n]+)/i);
260
+ if (match) {
261
+ const ext = path.extname(match[1]).toLowerCase();
262
+ if (ext) return ext;
263
+ }
264
+ }
265
+
266
+ // Fall back to URL path
267
+ try {
268
+ const pathname = new URL(url).pathname;
269
+ const ext = path.extname(pathname).toLowerCase();
270
+ if (ext) return ext;
271
+ } catch {}
272
+
273
+ return "";
274
+ }
275
+
276
+ /**
277
+ * Check if content type is convertible via markitdown
278
+ */
279
+ function isConvertible(mime: string, extensionHint: string): boolean {
280
+ if (CONVERTIBLE_MIMES.has(mime)) return true;
281
+ if (mime === "application/octet-stream" && CONVERTIBLE_EXTENSIONS.has(extensionHint)) return true;
282
+ if (CONVERTIBLE_EXTENSIONS.has(extensionHint)) return true;
283
+ return false;
284
+ }
285
+
286
+ /**
287
+ * Check if content looks like HTML
288
+ */
289
+ function looksLikeHtml(content: string): boolean {
290
+ const trimmed = content.trim().toLowerCase();
291
+ return (
292
+ trimmed.startsWith("<!doctype") ||
293
+ trimmed.startsWith("<html") ||
294
+ trimmed.startsWith("<head") ||
295
+ trimmed.startsWith("<body")
296
+ );
297
+ }
298
+
299
+ /**
300
+ * Convert binary file to markdown using markitdown
301
+ */
302
+ function convertWithMarkitdown(
303
+ content: Buffer,
304
+ extensionHint: string,
305
+ timeout: number,
306
+ ): { content: string; ok: boolean } {
307
+ if (!hasCommand("markitdown")) {
308
+ return { content: "", ok: false };
309
+ }
310
+
311
+ // Write to temp file with extension hint
312
+ const ext = extensionHint || ".bin";
313
+ const tmpFile = path.join(os.tmpdir(), `omp-convert-${Date.now()}${ext}`);
314
+
315
+ try {
316
+ fs.writeFileSync(tmpFile, content);
317
+ const result = exec("markitdown", [tmpFile], { timeout });
318
+ return { content: result.stdout, ok: result.ok };
319
+ } finally {
320
+ try {
321
+ fs.unlinkSync(tmpFile);
322
+ } catch {}
323
+ }
324
+ }
325
+
326
+ /**
327
+ * Try fetching URL with .md appended (llms.txt convention)
328
+ */
329
+ async function tryMdSuffix(url: string, timeout: number): Promise<string | null> {
330
+ const candidates: string[] = [];
331
+
332
+ try {
333
+ const parsed = new URL(url);
334
+ const pathname = parsed.pathname;
335
+
336
+ if (pathname.endsWith("/")) {
337
+ // /foo/bar/ -> /foo/bar/index.html.md
338
+ candidates.push(`${parsed.origin}${pathname}index.html.md`);
339
+ } else if (pathname.includes(".")) {
340
+ // /foo/bar.html -> /foo/bar.html.md
341
+ candidates.push(`${parsed.origin}${pathname}.md`);
342
+ } else {
343
+ // /foo/bar -> /foo/bar.md
344
+ candidates.push(`${parsed.origin}${pathname}.md`);
345
+ }
346
+ } catch {
347
+ return null;
348
+ }
349
+
350
+ for (const candidate of candidates) {
351
+ const result = await loadPage(candidate, { timeout: Math.min(timeout, 5) });
352
+ if (result.ok && result.content.trim().length > 100 && !looksLikeHtml(result.content)) {
353
+ return result.content;
354
+ }
355
+ }
356
+
357
+ return null;
358
+ }
359
+
360
+ /**
361
+ * Try to fetch LLM-friendly endpoints
362
+ */
363
+ async function tryLlmEndpoints(origin: string, timeout: number): Promise<string | null> {
364
+ const endpoints = [`${origin}/.well-known/llms.txt`, `${origin}/llms.txt`, `${origin}/llms.md`];
365
+
366
+ for (const endpoint of endpoints) {
367
+ const result = await loadPage(endpoint, { timeout: Math.min(timeout, 5) });
368
+ if (result.ok && result.content.trim().length > 100 && !looksLikeHtml(result.content)) {
369
+ return result.content;
370
+ }
371
+ }
372
+ return null;
373
+ }
374
+
375
+ /**
376
+ * Try content negotiation for markdown/plain
377
+ */
378
+ async function tryContentNegotiation(url: string, timeout: number): Promise<{ content: string; type: string } | null> {
379
+ const result = await loadPage(url, {
380
+ timeout,
381
+ headers: { Accept: "text/markdown, text/plain;q=0.9, text/html;q=0.8" },
382
+ });
383
+
384
+ if (!result.ok) return null;
385
+
386
+ const mime = normalizeMime(result.contentType);
387
+ if (mime.includes("markdown") || mime === "text/plain") {
388
+ return { content: result.content, type: result.contentType };
389
+ }
390
+
391
+ return null;
392
+ }
393
+
394
+ /**
395
+ * Parse alternate links from HTML head
396
+ */
397
+ function parseAlternateLinks(html: string, pageUrl: string): string[] {
398
+ const links: string[] = [];
399
+
400
+ try {
401
+ const doc = parseHtml(html.slice(0, 262144));
402
+ const alternateLinks = doc.querySelectorAll('link[rel="alternate"]');
403
+
404
+ for (const link of alternateLinks) {
405
+ const href = link.getAttribute("href");
406
+ const type = link.getAttribute("type")?.toLowerCase() ?? "";
407
+
408
+ if (!href) continue;
409
+
410
+ // Skip site-wide feeds
411
+ if (
412
+ href.includes("RecentChanges") ||
413
+ href.includes("Special:") ||
414
+ href.includes("/feed/") ||
415
+ href.includes("action=feed")
416
+ ) {
417
+ continue;
418
+ }
419
+
420
+ if (type.includes("markdown")) {
421
+ links.push(href);
422
+ } else if (
423
+ (type.includes("rss") || type.includes("atom") || type.includes("feed")) &&
424
+ (href.includes(new URL(pageUrl).pathname) || href.includes("comments"))
425
+ ) {
426
+ links.push(href);
427
+ }
428
+ }
429
+ } catch {}
430
+
431
+ return links;
432
+ }
433
+
434
+ /**
435
+ * Extract document links from HTML (for PDF/DOCX wrapper pages)
436
+ */
437
+ function extractDocumentLinks(html: string, baseUrl: string): string[] {
438
+ const links: string[] = [];
439
+
440
+ try {
441
+ const doc = parseHtml(html);
442
+ const anchors = doc.querySelectorAll("a[href]");
443
+
444
+ for (const anchor of anchors) {
445
+ const href = anchor.getAttribute("href");
446
+ if (!href) continue;
447
+
448
+ const ext = path.extname(href).toLowerCase();
449
+ if (CONVERTIBLE_EXTENSIONS.has(ext)) {
450
+ const resolved = href.startsWith("http") ? href : new URL(href, baseUrl).href;
451
+ links.push(resolved);
452
+ }
453
+ }
454
+ } catch {}
455
+
456
+ return links;
457
+ }
458
+
459
+ /**
460
+ * Strip CDATA wrapper and clean text
461
+ */
462
+ function cleanFeedText(text: string): string {
463
+ return text
464
+ .replace(/<!\[CDATA\[/g, "")
465
+ .replace(/\]\]>/g, "")
466
+ .replace(/&lt;/g, "<")
467
+ .replace(/&gt;/g, ">")
468
+ .replace(/&amp;/g, "&")
469
+ .replace(/&quot;/g, '"')
470
+ .replace(/<[^>]+>/g, "") // Strip HTML tags
471
+ .trim();
472
+ }
473
+
474
+ /**
475
+ * Parse RSS/Atom feed to markdown
476
+ */
477
+ function parseFeedToMarkdown(content: string, maxItems = 10): string {
478
+ try {
479
+ const doc = parseHtml(content, { parseNoneClosedTags: true });
480
+
481
+ // Try RSS
482
+ const channel = doc.querySelector("channel");
483
+ if (channel) {
484
+ const title = cleanFeedText(channel.querySelector("title")?.text || "RSS Feed");
485
+ const items = channel.querySelectorAll("item").slice(0, maxItems);
486
+
487
+ let md = `# ${title}\n\n`;
488
+ for (const item of items) {
489
+ const itemTitle = cleanFeedText(item.querySelector("title")?.text || "Untitled");
490
+ const link = cleanFeedText(item.querySelector("link")?.text || "");
491
+ const pubDate = cleanFeedText(item.querySelector("pubDate")?.text || "");
492
+ const desc = cleanFeedText(item.querySelector("description")?.text || "");
493
+
494
+ md += `## ${itemTitle}\n`;
495
+ if (pubDate) md += `*${pubDate}*\n\n`;
496
+ if (desc) md += `${desc.slice(0, 500)}${desc.length > 500 ? "..." : ""}\n\n`;
497
+ if (link) md += `[Read more](${link})\n\n`;
498
+ md += "---\n\n";
499
+ }
500
+ return md;
501
+ }
502
+
503
+ // Try Atom
504
+ const feed = doc.querySelector("feed");
505
+ if (feed) {
506
+ const title = cleanFeedText(feed.querySelector("title")?.text || "Atom Feed");
507
+ const entries = feed.querySelectorAll("entry").slice(0, maxItems);
508
+
509
+ let md = `# ${title}\n\n`;
510
+ for (const entry of entries) {
511
+ const entryTitle = cleanFeedText(entry.querySelector("title")?.text || "Untitled");
512
+ const link = entry.querySelector("link")?.getAttribute("href") || "";
513
+ const updated = cleanFeedText(entry.querySelector("updated")?.text || "");
514
+ const summary = cleanFeedText(
515
+ entry.querySelector("summary")?.text || entry.querySelector("content")?.text || "",
516
+ );
517
+
518
+ md += `## ${entryTitle}\n`;
519
+ if (updated) md += `*${updated}*\n\n`;
520
+ if (summary) md += `${summary.slice(0, 500)}${summary.length > 500 ? "..." : ""}\n\n`;
521
+ if (link) md += `[Read more](${link})\n\n`;
522
+ md += "---\n\n";
523
+ }
524
+ return md;
525
+ }
526
+ } catch {}
527
+
528
+ return content; // Fall back to raw content
529
+ }
530
+
531
+ /**
532
+ * Render HTML to text using lynx
533
+ */
534
+ function renderWithLynx(html: string, timeout: number): { content: string; ok: boolean } {
535
+ const tmpFile = path.join(os.tmpdir(), `omp-render-${Date.now()}.html`);
536
+ try {
537
+ fs.writeFileSync(tmpFile, html);
538
+ // Convert path to file URL (handles Windows paths correctly)
539
+ const normalizedPath = tmpFile.replace(/\\/g, "/");
540
+ const fileUrl = normalizedPath.startsWith("/") ? `file://${normalizedPath}` : `file:///${normalizedPath}`;
541
+ const result = exec("lynx", ["-dump", "-nolist", "-width", "120", fileUrl], { timeout });
542
+ return { content: result.stdout, ok: result.ok };
543
+ } finally {
544
+ try {
545
+ fs.unlinkSync(tmpFile);
546
+ } catch {}
547
+ }
548
+ }
549
+
550
+ /**
551
+ * Check if lynx output looks JS-gated or mostly navigation
552
+ */
553
+ function isLowQualityOutput(content: string): boolean {
554
+ const lower = content.toLowerCase();
555
+
556
+ // JS-gated indicators
557
+ const jsGated = [
558
+ "enable javascript",
559
+ "javascript required",
560
+ "turn on javascript",
561
+ "please enable javascript",
562
+ "browser not supported",
563
+ ];
564
+ if (content.length < 1024 && jsGated.some((t) => lower.includes(t))) {
565
+ return true;
566
+ }
567
+
568
+ // Mostly navigation (high link/menu density)
569
+ const lines = content.split("\n").filter((l) => l.trim());
570
+ const shortLines = lines.filter((l) => l.trim().length < 40);
571
+ if (lines.length > 10 && shortLines.length / lines.length > 0.7) {
572
+ return true;
573
+ }
574
+
575
+ return false;
576
+ }
577
+
578
+ /**
579
+ * Format JSON
580
+ */
581
+ function formatJson(content: string): string {
582
+ try {
583
+ return JSON.stringify(JSON.parse(content), null, 2);
584
+ } catch {
585
+ return content;
586
+ }
587
+ }
588
+
589
+ /**
590
+ * Truncate and cleanup output
591
+ */
592
+ function finalizeOutput(content: string): { content: string; truncated: boolean } {
593
+ const cleaned = content.replace(/\n{3,}/g, "\n\n").trim();
594
+ const truncated = cleaned.length > MAX_OUTPUT_CHARS;
595
+ return {
596
+ content: cleaned.slice(0, MAX_OUTPUT_CHARS),
597
+ truncated,
598
+ };
599
+ }
600
+
601
+ /**
602
+ * Fetch page as binary buffer (for convertible files)
603
+ */
604
+ async function fetchBinary(
605
+ url: string,
606
+ timeout: number,
607
+ ): Promise<{ buffer: Buffer; contentType: string; contentDisposition?: string; ok: boolean }> {
608
+ try {
609
+ const controller = new AbortController();
610
+ const timeoutId = setTimeout(() => controller.abort(), timeout * 1000);
611
+
612
+ const response = await fetch(url, {
613
+ signal: controller.signal,
614
+ headers: {
615
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/131.0.0.0",
616
+ },
617
+ redirect: "follow",
618
+ });
619
+
620
+ clearTimeout(timeoutId);
621
+
622
+ if (!response.ok) {
623
+ return { buffer: Buffer.alloc(0), contentType: "", ok: false };
624
+ }
625
+
626
+ const contentType = response.headers.get("content-type") ?? "";
627
+ const contentDisposition = response.headers.get("content-disposition") ?? undefined;
628
+ const buffer = Buffer.from(await response.arrayBuffer());
629
+
630
+ return { buffer, contentType, contentDisposition, ok: true };
631
+ } catch {
632
+ return { buffer: Buffer.alloc(0), contentType: "", ok: false };
633
+ }
634
+ }
635
+
636
+ // =============================================================================
637
+ // GitHub Special Handling
638
+ // =============================================================================
639
+
640
+ interface GitHubUrl {
641
+ type: "blob" | "tree" | "repo" | "issue" | "issues" | "pull" | "pulls" | "discussion" | "discussions" | "other";
642
+ owner: string;
643
+ repo: string;
644
+ ref?: string;
645
+ path?: string;
646
+ number?: number;
647
+ }
648
+
649
+ /**
650
+ * Parse GitHub URL into components
651
+ */
652
+ function parseGitHubUrl(url: string): GitHubUrl | null {
653
+ try {
654
+ const parsed = new URL(url);
655
+ if (parsed.hostname !== "github.com") return null;
656
+
657
+ const parts = parsed.pathname.split("/").filter(Boolean);
658
+ if (parts.length < 2) return null;
659
+
660
+ const [owner, repo, ...rest] = parts;
661
+
662
+ if (rest.length === 0) {
663
+ return { type: "repo", owner, repo };
664
+ }
665
+
666
+ const [section, ...subParts] = rest;
667
+
668
+ switch (section) {
669
+ case "blob":
670
+ case "tree": {
671
+ const [ref, ...pathParts] = subParts;
672
+ return { type: section, owner, repo, ref, path: pathParts.join("/") };
673
+ }
674
+ case "issues":
675
+ if (subParts.length > 0 && /^\d+$/.test(subParts[0])) {
676
+ return { type: "issue", owner, repo, number: parseInt(subParts[0], 10) };
677
+ }
678
+ return { type: "issues", owner, repo };
679
+ case "pull":
680
+ if (subParts.length > 0 && /^\d+$/.test(subParts[0])) {
681
+ return { type: "pull", owner, repo, number: parseInt(subParts[0], 10) };
682
+ }
683
+ return { type: "pulls", owner, repo };
684
+ case "pulls":
685
+ return { type: "pulls", owner, repo };
686
+ case "discussions":
687
+ if (subParts.length > 0 && /^\d+$/.test(subParts[0])) {
688
+ return { type: "discussion", owner, repo, number: parseInt(subParts[0], 10) };
689
+ }
690
+ return { type: "discussions", owner, repo };
691
+ default:
692
+ return { type: "other", owner, repo };
693
+ }
694
+ } catch {
695
+ return null;
696
+ }
697
+ }
698
+
699
+ /**
700
+ * Convert GitHub blob URL to raw URL
701
+ */
702
+ function toRawGitHubUrl(gh: GitHubUrl): string {
703
+ return `https://raw.githubusercontent.com/${gh.owner}/${gh.repo}/refs/heads/${gh.ref}/${gh.path}`;
704
+ }
705
+
706
+ /**
707
+ * Fetch from GitHub API
708
+ */
709
+ async function fetchGitHubApi(endpoint: string, timeout: number): Promise<{ data: unknown; ok: boolean }> {
710
+ try {
711
+ const controller = new AbortController();
712
+ const timeoutId = setTimeout(() => controller.abort(), timeout * 1000);
713
+
714
+ const headers: Record<string, string> = {
715
+ Accept: "application/vnd.github.v3+json",
716
+ "User-Agent": "omp-web-fetch/1.0",
717
+ };
718
+
719
+ // Use GITHUB_TOKEN if available
720
+ const token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN;
721
+ if (token) {
722
+ headers.Authorization = `Bearer ${token}`;
723
+ }
724
+
725
+ const response = await fetch(`https://api.github.com${endpoint}`, {
726
+ signal: controller.signal,
727
+ headers,
728
+ });
729
+
730
+ clearTimeout(timeoutId);
731
+
732
+ if (!response.ok) {
733
+ return { data: null, ok: false };
734
+ }
735
+
736
+ return { data: await response.json(), ok: true };
737
+ } catch {
738
+ return { data: null, ok: false };
739
+ }
740
+ }
741
+
742
+ /**
743
+ * Render GitHub issue/PR to markdown
744
+ */
745
+ async function renderGitHubIssue(gh: GitHubUrl, timeout: number): Promise<{ content: string; ok: boolean }> {
746
+ const endpoint =
747
+ gh.type === "pull"
748
+ ? `/repos/${gh.owner}/${gh.repo}/pulls/${gh.number}`
749
+ : `/repos/${gh.owner}/${gh.repo}/issues/${gh.number}`;
750
+
751
+ const result = await fetchGitHubApi(endpoint, timeout);
752
+ if (!result.ok || !result.data) return { content: "", ok: false };
753
+
754
+ const issue = result.data as {
755
+ title: string;
756
+ number: number;
757
+ state: string;
758
+ user: { login: string };
759
+ created_at: string;
760
+ updated_at: string;
761
+ body: string | null;
762
+ labels: Array<{ name: string }>;
763
+ comments: number;
764
+ html_url: string;
765
+ };
766
+
767
+ let md = `# ${issue.title}\n\n`;
768
+ md += `**#${issue.number}** · ${issue.state} · opened by @${issue.user.login}\n`;
769
+ md += `Created: ${issue.created_at} · Updated: ${issue.updated_at}\n`;
770
+ if (issue.labels.length > 0) {
771
+ md += `Labels: ${issue.labels.map((l) => l.name).join(", ")}\n`;
772
+ }
773
+ md += `\n---\n\n`;
774
+ md += issue.body || "*No description provided.*";
775
+ md += `\n\n---\n\n`;
776
+
777
+ // Fetch comments if any
778
+ if (issue.comments > 0) {
779
+ const commentsResult = await fetchGitHubApi(
780
+ `/repos/${gh.owner}/${gh.repo}/issues/${gh.number}/comments?per_page=50`,
781
+ timeout,
782
+ );
783
+ if (commentsResult.ok && Array.isArray(commentsResult.data)) {
784
+ md += `## Comments (${issue.comments})\n\n`;
785
+ for (const comment of commentsResult.data as Array<{
786
+ user: { login: string };
787
+ created_at: string;
788
+ body: string;
789
+ }>) {
790
+ md += `### @${comment.user.login} · ${comment.created_at}\n\n`;
791
+ md += `${comment.body}\n\n---\n\n`;
792
+ }
793
+ }
794
+ }
795
+
796
+ return { content: md, ok: true };
797
+ }
798
+
799
+ /**
800
+ * Render GitHub issues list to markdown
801
+ */
802
+ async function renderGitHubIssuesList(gh: GitHubUrl, timeout: number): Promise<{ content: string; ok: boolean }> {
803
+ const result = await fetchGitHubApi(`/repos/${gh.owner}/${gh.repo}/issues?state=open&per_page=30`, timeout);
804
+ if (!result.ok || !Array.isArray(result.data)) return { content: "", ok: false };
805
+
806
+ const issues = result.data as Array<{
807
+ number: number;
808
+ title: string;
809
+ state: string;
810
+ user: { login: string };
811
+ created_at: string;
812
+ comments: number;
813
+ labels: Array<{ name: string }>;
814
+ pull_request?: unknown;
815
+ }>;
816
+
817
+ let md = `# ${gh.owner}/${gh.repo} - Open Issues\n\n`;
818
+
819
+ for (const issue of issues) {
820
+ if (issue.pull_request) continue; // Skip PRs in issues list
821
+ const labels = issue.labels.length > 0 ? ` [${issue.labels.map((l) => l.name).join(", ")}]` : "";
822
+ md += `- **#${issue.number}** ${issue.title}${labels}\n`;
823
+ md += ` by @${issue.user.login} · ${issue.comments} comments · ${issue.created_at}\n\n`;
824
+ }
825
+
826
+ return { content: md, ok: true };
827
+ }
828
+
829
+ /**
830
+ * Render GitHub tree (directory) to markdown
831
+ */
832
+ async function renderGitHubTree(gh: GitHubUrl, timeout: number): Promise<{ content: string; ok: boolean }> {
833
+ // Fetch repo info first to get default branch if ref not specified
834
+ const repoResult = await fetchGitHubApi(`/repos/${gh.owner}/${gh.repo}`, timeout);
835
+ if (!repoResult.ok) return { content: "", ok: false };
836
+
837
+ const repo = repoResult.data as {
838
+ full_name: string;
839
+ default_branch: string;
840
+ };
841
+
842
+ const ref = gh.ref || repo.default_branch;
843
+ const dirPath = gh.path || "";
844
+
845
+ let md = `# ${repo.full_name}/${dirPath || "(root)"}\n\n`;
846
+ md += `**Branch:** ${ref}\n\n`;
847
+
848
+ // Fetch directory contents
849
+ const contentsResult = await fetchGitHubApi(`/repos/${gh.owner}/${gh.repo}/contents/${dirPath}?ref=${ref}`, timeout);
850
+
851
+ if (contentsResult.ok && Array.isArray(contentsResult.data)) {
852
+ const items = contentsResult.data as Array<{
853
+ name: string;
854
+ type: "file" | "dir" | "symlink" | "submodule";
855
+ size?: number;
856
+ path: string;
857
+ }>;
858
+
859
+ // Sort: directories first, then files, alphabetically
860
+ items.sort((a, b) => {
861
+ if (a.type === "dir" && b.type !== "dir") return -1;
862
+ if (a.type !== "dir" && b.type === "dir") return 1;
863
+ return a.name.localeCompare(b.name);
864
+ });
865
+
866
+ md += `## Contents\n\n`;
867
+ md += "```\n";
868
+ for (const item of items) {
869
+ const prefix = item.type === "dir" ? "[dir] " : " ";
870
+ const size = item.size ? ` (${item.size} bytes)` : "";
871
+ md += `${prefix}${item.name}${item.type === "file" ? size : ""}\n`;
872
+ }
873
+ md += "```\n\n";
874
+
875
+ // Look for README in this directory
876
+ const readmeFile = items.find((item) => item.type === "file" && /^readme\.md$/i.test(item.name));
877
+ if (readmeFile) {
878
+ const readmePath = dirPath ? `${dirPath}/${readmeFile.name}` : readmeFile.name;
879
+ const rawUrl = `https://raw.githubusercontent.com/${gh.owner}/${gh.repo}/refs/heads/${ref}/${readmePath}`;
880
+ const readmeResult = await loadPage(rawUrl, { timeout });
881
+ if (readmeResult.ok) {
882
+ md += `---\n\n## README\n\n${readmeResult.content}`;
883
+ }
884
+ }
885
+ }
886
+
887
+ return { content: md, ok: true };
888
+ }
889
+
890
+ /**
891
+ * Render GitHub repo to markdown (file list + README)
892
+ */
893
+ async function renderGitHubRepo(gh: GitHubUrl, timeout: number): Promise<{ content: string; ok: boolean }> {
894
+ // Fetch repo info
895
+ const repoResult = await fetchGitHubApi(`/repos/${gh.owner}/${gh.repo}`, timeout);
896
+ if (!repoResult.ok) return { content: "", ok: false };
897
+
898
+ const repo = repoResult.data as {
899
+ full_name: string;
900
+ description: string | null;
901
+ stargazers_count: number;
902
+ forks_count: number;
903
+ open_issues_count: number;
904
+ default_branch: string;
905
+ language: string | null;
906
+ license: { name: string } | null;
907
+ };
908
+
909
+ let md = `# ${repo.full_name}\n\n`;
910
+ if (repo.description) md += `${repo.description}\n\n`;
911
+ md += `Stars: ${repo.stargazers_count} · Forks: ${repo.forks_count} · Issues: ${repo.open_issues_count}\n`;
912
+ if (repo.language) md += `Language: ${repo.language}\n`;
913
+ if (repo.license) md += `License: ${repo.license.name}\n`;
914
+ md += `\n---\n\n`;
915
+
916
+ // Fetch file tree
917
+ const treeResult = await fetchGitHubApi(
918
+ `/repos/${gh.owner}/${gh.repo}/git/trees/${repo.default_branch}?recursive=1`,
919
+ timeout,
920
+ );
921
+ if (treeResult.ok && treeResult.data) {
922
+ const tree = (treeResult.data as { tree: Array<{ path: string; type: string }> }).tree;
923
+ md += `## Files\n\n`;
924
+ md += "```\n";
925
+ for (const item of tree.slice(0, 100)) {
926
+ const prefix = item.type === "tree" ? "[dir] " : " ";
927
+ md += `${prefix}${item.path}\n`;
928
+ }
929
+ if (tree.length > 100) {
930
+ md += `... and ${tree.length - 100} more files\n`;
931
+ }
932
+ md += "```\n\n";
933
+ }
934
+
935
+ // Fetch README
936
+ const readmeResult = await fetchGitHubApi(`/repos/${gh.owner}/${gh.repo}/readme`, timeout);
937
+ if (readmeResult.ok && readmeResult.data) {
938
+ const readme = readmeResult.data as { content: string; encoding: string };
939
+ if (readme.encoding === "base64") {
940
+ const decoded = Buffer.from(readme.content, "base64").toString("utf-8");
941
+ md += `## README\n\n${decoded}`;
942
+ }
943
+ }
944
+
945
+ return { content: md, ok: true };
946
+ }
947
+
948
+ /**
949
+ * Handle GitHub URLs specially
950
+ */
951
+ async function handleGitHub(url: string, timeout: number): Promise<RenderResult | null> {
952
+ const gh = parseGitHubUrl(url);
953
+ if (!gh) return null;
954
+
955
+ const fetchedAt = new Date().toISOString();
956
+ const notes: string[] = [];
957
+
958
+ switch (gh.type) {
959
+ case "blob": {
960
+ // Convert to raw URL and fetch
961
+ const rawUrl = toRawGitHubUrl(gh);
962
+ notes.push(`Fetched raw: ${rawUrl}`);
963
+ const result = await loadPage(rawUrl, { timeout });
964
+ if (result.ok) {
965
+ const output = finalizeOutput(result.content);
966
+ return {
967
+ url,
968
+ finalUrl: rawUrl,
969
+ contentType: "text/plain",
970
+ method: "github-raw",
971
+ content: output.content,
972
+ fetchedAt,
973
+ truncated: output.truncated,
974
+ notes,
975
+ };
976
+ }
977
+ break;
978
+ }
979
+
980
+ case "tree": {
981
+ notes.push(`Fetched via GitHub API`);
982
+ const result = await renderGitHubTree(gh, timeout);
983
+ if (result.ok) {
984
+ const output = finalizeOutput(result.content);
985
+ return {
986
+ url,
987
+ finalUrl: url,
988
+ contentType: "text/markdown",
989
+ method: "github-tree",
990
+ content: output.content,
991
+ fetchedAt,
992
+ truncated: output.truncated,
993
+ notes,
994
+ };
995
+ }
996
+ break;
997
+ }
998
+
999
+ case "issue":
1000
+ case "pull": {
1001
+ notes.push(`Fetched via GitHub API`);
1002
+ const result = await renderGitHubIssue(gh, timeout);
1003
+ if (result.ok) {
1004
+ const output = finalizeOutput(result.content);
1005
+ return {
1006
+ url,
1007
+ finalUrl: url,
1008
+ contentType: "text/markdown",
1009
+ method: gh.type === "pull" ? "github-pr" : "github-issue",
1010
+ content: output.content,
1011
+ fetchedAt,
1012
+ truncated: output.truncated,
1013
+ notes,
1014
+ };
1015
+ }
1016
+ break;
1017
+ }
1018
+
1019
+ case "issues": {
1020
+ notes.push(`Fetched via GitHub API`);
1021
+ const result = await renderGitHubIssuesList(gh, timeout);
1022
+ if (result.ok) {
1023
+ const output = finalizeOutput(result.content);
1024
+ return {
1025
+ url,
1026
+ finalUrl: url,
1027
+ contentType: "text/markdown",
1028
+ method: "github-issues",
1029
+ content: output.content,
1030
+ fetchedAt,
1031
+ truncated: output.truncated,
1032
+ notes,
1033
+ };
1034
+ }
1035
+ break;
1036
+ }
1037
+
1038
+ case "repo": {
1039
+ notes.push(`Fetched via GitHub API`);
1040
+ const result = await renderGitHubRepo(gh, timeout);
1041
+ if (result.ok) {
1042
+ const output = finalizeOutput(result.content);
1043
+ return {
1044
+ url,
1045
+ finalUrl: url,
1046
+ contentType: "text/markdown",
1047
+ method: "github-repo",
1048
+ content: output.content,
1049
+ fetchedAt,
1050
+ truncated: output.truncated,
1051
+ notes,
1052
+ };
1053
+ }
1054
+ break;
1055
+ }
1056
+ }
1057
+
1058
+ // Fall back to null (let normal rendering handle it)
1059
+ return null;
1060
+ }
1061
+
1062
+ // =============================================================================
1063
+ // Twitter/X Special Handling (via Nitter)
1064
+ // =============================================================================
1065
+
1066
+ // Active Nitter instances - check https://status.d420.de/instances for current status
1067
+ const NITTER_INSTANCES = [
1068
+ "nitter.privacyredirect.com",
1069
+ "nitter.tiekoetter.com",
1070
+ "nitter.poast.org",
1071
+ "nitter.woodland.cafe",
1072
+ ];
1073
+
1074
+ /**
1075
+ * Handle Twitter/X URLs via Nitter
1076
+ */
1077
+ async function handleTwitter(url: string, timeout: number): Promise<RenderResult | null> {
1078
+ try {
1079
+ const parsed = new URL(url);
1080
+ if (!["twitter.com", "x.com", "www.twitter.com", "www.x.com"].includes(parsed.hostname)) {
1081
+ return null;
1082
+ }
1083
+
1084
+ const fetchedAt = new Date().toISOString();
1085
+
1086
+ // Try Nitter instances
1087
+ for (const instance of NITTER_INSTANCES) {
1088
+ const nitterUrl = `https://${instance}${parsed.pathname}`;
1089
+ const result = await loadPage(nitterUrl, { timeout: Math.min(timeout, 10) });
1090
+
1091
+ if (result.ok && result.content.length > 500) {
1092
+ // Parse the Nitter HTML
1093
+ const doc = parseHtml(result.content);
1094
+
1095
+ // Extract tweet content
1096
+ const tweetContent = doc.querySelector(".tweet-content")?.text?.trim();
1097
+ const fullname = doc.querySelector(".fullname")?.text?.trim();
1098
+ const username = doc.querySelector(".username")?.text?.trim();
1099
+ const date = doc.querySelector(".tweet-date a")?.text?.trim();
1100
+ const stats = doc.querySelector(".tweet-stats")?.text?.trim();
1101
+
1102
+ if (tweetContent) {
1103
+ let md = `# Tweet by ${fullname || "Unknown"} (${username || "@?"})\n\n`;
1104
+ if (date) md += `*${date}*\n\n`;
1105
+ md += `${tweetContent}\n\n`;
1106
+ if (stats) md += `---\n${stats.replace(/\s+/g, " ")}\n`;
1107
+
1108
+ // Check for replies/thread
1109
+ const replies = doc.querySelectorAll(".timeline-item .tweet-content");
1110
+ if (replies.length > 1) {
1111
+ md += `\n---\n\n## Thread/Replies\n\n`;
1112
+ for (const reply of Array.from(replies).slice(1, 10)) {
1113
+ const replyUser = reply.parentNode?.querySelector(".username")?.text?.trim();
1114
+ md += `**${replyUser || "@?"}**: ${reply.text?.trim()}\n\n`;
1115
+ }
1116
+ }
1117
+
1118
+ const output = finalizeOutput(md);
1119
+ return {
1120
+ url,
1121
+ finalUrl: nitterUrl,
1122
+ contentType: "text/markdown",
1123
+ method: "twitter-nitter",
1124
+ content: output.content,
1125
+ fetchedAt,
1126
+ truncated: output.truncated,
1127
+ notes: [`Via Nitter: ${instance}`],
1128
+ };
1129
+ }
1130
+ }
1131
+ }
1132
+ } catch {}
1133
+
1134
+ // X.com blocks all bots - return a helpful error instead of falling through
1135
+ return {
1136
+ url,
1137
+ finalUrl: url,
1138
+ contentType: "text/plain",
1139
+ method: "twitter-blocked",
1140
+ content:
1141
+ "Twitter/X blocks automated access. Nitter instances were unavailable.\n\nTry:\n- Opening the link in a browser\n- Using a different Nitter instance manually\n- Checking if the tweet is available via an archive service",
1142
+ fetchedAt: new Date().toISOString(),
1143
+ truncated: false,
1144
+ notes: ["X.com blocks bots; Nitter instances unavailable"],
1145
+ };
1146
+ }
1147
+
1148
+ // =============================================================================
1149
+ // Stack Overflow Special Handling
1150
+ // =============================================================================
1151
+
1152
+ interface SOQuestion {
1153
+ title: string;
1154
+ body: string;
1155
+ score: number;
1156
+ owner: { display_name: string };
1157
+ creation_date: number;
1158
+ tags: string[];
1159
+ answer_count: number;
1160
+ is_answered: boolean;
1161
+ }
1162
+
1163
+ interface SOAnswer {
1164
+ body: string;
1165
+ score: number;
1166
+ is_accepted: boolean;
1167
+ owner: { display_name: string };
1168
+ creation_date: number;
1169
+ }
1170
+
1171
+ /**
1172
+ * Convert basic HTML to markdown (for SO bodies)
1173
+ */
1174
+ function htmlToBasicMarkdown(html: string): string {
1175
+ return html
1176
+ .replace(/<pre><code[^>]*>/g, "\n```\n")
1177
+ .replace(/<\/code><\/pre>/g, "\n```\n")
1178
+ .replace(/<code>/g, "`")
1179
+ .replace(/<\/code>/g, "`")
1180
+ .replace(/<strong>/g, "**")
1181
+ .replace(/<\/strong>/g, "**")
1182
+ .replace(/<em>/g, "*")
1183
+ .replace(/<\/em>/g, "*")
1184
+ .replace(/<a href="([^"]+)"[^>]*>([^<]+)<\/a>/g, "[$2]($1)")
1185
+ .replace(/<p>/g, "\n\n")
1186
+ .replace(/<\/p>/g, "")
1187
+ .replace(/<br\s*\/?>/g, "\n")
1188
+ .replace(/<li>/g, "- ")
1189
+ .replace(/<\/li>/g, "\n")
1190
+ .replace(/<\/?[uo]l>/g, "\n")
1191
+ .replace(/<h(\d)>/g, (_, n) => `\n${"#".repeat(parseInt(n, 10))} `)
1192
+ .replace(/<\/h\d>/g, "\n")
1193
+ .replace(/<blockquote>/g, "\n> ")
1194
+ .replace(/<\/blockquote>/g, "\n")
1195
+ .replace(/<[^>]+>/g, "") // Strip remaining tags
1196
+ .replace(/&lt;/g, "<")
1197
+ .replace(/&gt;/g, ">")
1198
+ .replace(/&amp;/g, "&")
1199
+ .replace(/&quot;/g, '"')
1200
+ .replace(/&#39;/g, "'")
1201
+ .replace(/\n{3,}/g, "\n\n")
1202
+ .trim();
1203
+ }
1204
+
1205
+ /**
1206
+ * Handle Stack Overflow URLs via API
1207
+ */
1208
+ async function handleStackOverflow(url: string, timeout: number): Promise<RenderResult | null> {
1209
+ try {
1210
+ const parsed = new URL(url);
1211
+ if (!parsed.hostname.includes("stackoverflow.com") && !parsed.hostname.includes("stackexchange.com")) {
1212
+ return null;
1213
+ }
1214
+
1215
+ // Extract question ID from URL patterns like /questions/12345/...
1216
+ const match = parsed.pathname.match(/\/questions\/(\d+)/);
1217
+ if (!match) return null;
1218
+
1219
+ const questionId = match[1];
1220
+ const site = parsed.hostname.includes("stackoverflow") ? "stackoverflow" : parsed.hostname.split(".")[0];
1221
+ const fetchedAt = new Date().toISOString();
1222
+
1223
+ // Fetch question with answers
1224
+ const apiUrl = `https://api.stackexchange.com/2.3/questions/${questionId}?order=desc&sort=votes&site=${site}&filter=withbody`;
1225
+ const qResult = await loadPage(apiUrl, { timeout });
1226
+
1227
+ if (!qResult.ok) return null;
1228
+
1229
+ const qData = JSON.parse(qResult.content) as { items: SOQuestion[] };
1230
+ if (!qData.items?.length) return null;
1231
+
1232
+ const question = qData.items[0];
1233
+
1234
+ let md = `# ${question.title}\n\n`;
1235
+ md += `**Score:** ${question.score} · **Answers:** ${question.answer_count}`;
1236
+ md += question.is_answered ? " (Answered)" : "";
1237
+ md += `\n**Tags:** ${question.tags.join(", ")}\n`;
1238
+ md += `**Asked by:** ${question.owner.display_name} · ${
1239
+ new Date(question.creation_date * 1000).toISOString().split("T")[0]
1240
+ }\n\n`;
1241
+ md += `---\n\n## Question\n\n${htmlToBasicMarkdown(question.body)}\n\n`;
1242
+
1243
+ // Fetch answers
1244
+ const aUrl = `https://api.stackexchange.com/2.3/questions/${questionId}/answers?order=desc&sort=votes&site=${site}&filter=withbody`;
1245
+ const aResult = await loadPage(aUrl, { timeout });
1246
+
1247
+ if (aResult.ok) {
1248
+ const aData = JSON.parse(aResult.content) as { items: SOAnswer[] };
1249
+ if (aData.items?.length) {
1250
+ md += `---\n\n## Answers\n\n`;
1251
+ for (const answer of aData.items.slice(0, 5)) {
1252
+ const accepted = answer.is_accepted ? " (Accepted)" : "";
1253
+ md += `### Score: ${answer.score}${accepted} · by ${answer.owner.display_name}\n\n`;
1254
+ md += `${htmlToBasicMarkdown(answer.body)}\n\n---\n\n`;
1255
+ }
1256
+ }
1257
+ }
1258
+
1259
+ const output = finalizeOutput(md);
1260
+ return {
1261
+ url,
1262
+ finalUrl: url,
1263
+ contentType: "text/markdown",
1264
+ method: "stackoverflow",
1265
+ content: output.content,
1266
+ fetchedAt,
1267
+ truncated: output.truncated,
1268
+ notes: ["Fetched via Stack Exchange API"],
1269
+ };
1270
+ } catch {}
1271
+
1272
+ return null;
1273
+ }
1274
+
1275
+ // =============================================================================
1276
+ // Wikipedia Special Handling
1277
+ // =============================================================================
1278
+
1279
+ /**
1280
+ * Handle Wikipedia URLs via API
1281
+ */
1282
+ async function handleWikipedia(url: string, timeout: number): Promise<RenderResult | null> {
1283
+ try {
1284
+ const parsed = new URL(url);
1285
+ // Match *.wikipedia.org
1286
+ const wikiMatch = parsed.hostname.match(/^(\w+)\.wikipedia\.org$/);
1287
+ if (!wikiMatch) return null;
1288
+
1289
+ const lang = wikiMatch[1];
1290
+ const titleMatch = parsed.pathname.match(/\/wiki\/(.+)/);
1291
+ if (!titleMatch) return null;
1292
+
1293
+ const title = decodeURIComponent(titleMatch[1]);
1294
+ const fetchedAt = new Date().toISOString();
1295
+
1296
+ // Use Wikipedia API to get plain text extract
1297
+ const apiUrl = `https://${lang}.wikipedia.org/api/rest_v1/page/summary/${encodeURIComponent(title)}`;
1298
+ const summaryResult = await loadPage(apiUrl, { timeout });
1299
+
1300
+ let md = "";
1301
+
1302
+ if (summaryResult.ok) {
1303
+ const summary = JSON.parse(summaryResult.content) as {
1304
+ title: string;
1305
+ description?: string;
1306
+ extract: string;
1307
+ };
1308
+ md = `# ${summary.title}\n\n`;
1309
+ if (summary.description) md += `*${summary.description}*\n\n`;
1310
+ md += `${summary.extract}\n\n---\n\n`;
1311
+ }
1312
+
1313
+ // Get full article content via mobile-html or parse API
1314
+ const contentUrl = `https://${lang}.wikipedia.org/api/rest_v1/page/mobile-html/${encodeURIComponent(title)}`;
1315
+ const contentResult = await loadPage(contentUrl, { timeout });
1316
+
1317
+ if (contentResult.ok) {
1318
+ const doc = parseHtml(contentResult.content);
1319
+
1320
+ // Extract main content sections
1321
+ const sections = doc.querySelectorAll("section");
1322
+ for (const section of sections) {
1323
+ const heading = section.querySelector("h2, h3, h4");
1324
+ const headingText = heading?.text?.trim();
1325
+
1326
+ // Skip certain sections
1327
+ if (
1328
+ headingText &&
1329
+ ["References", "External links", "See also", "Notes", "Further reading"].includes(headingText)
1330
+ ) {
1331
+ continue;
1332
+ }
1333
+
1334
+ if (headingText) {
1335
+ const level = heading?.tagName === "H2" ? "##" : "###";
1336
+ md += `${level} ${headingText}\n\n`;
1337
+ }
1338
+
1339
+ const paragraphs = section.querySelectorAll("p");
1340
+ for (const p of paragraphs) {
1341
+ const text = p.text?.trim();
1342
+ if (text && text.length > 20) {
1343
+ md += `${text}\n\n`;
1344
+ }
1345
+ }
1346
+ }
1347
+ }
1348
+
1349
+ if (!md) return null;
1350
+
1351
+ const output = finalizeOutput(md);
1352
+ return {
1353
+ url,
1354
+ finalUrl: url,
1355
+ contentType: "text/markdown",
1356
+ method: "wikipedia",
1357
+ content: output.content,
1358
+ fetchedAt,
1359
+ truncated: output.truncated,
1360
+ notes: ["Fetched via Wikipedia API"],
1361
+ };
1362
+ } catch {}
1363
+
1364
+ return null;
1365
+ }
1366
+
1367
+ // =============================================================================
1368
+ // Reddit Special Handling
1369
+ // =============================================================================
1370
+
1371
+ interface RedditPost {
1372
+ title: string;
1373
+ selftext: string;
1374
+ author: string;
1375
+ score: number;
1376
+ num_comments: number;
1377
+ created_utc: number;
1378
+ subreddit: string;
1379
+ url: string;
1380
+ is_self: boolean;
1381
+ }
1382
+
1383
+ interface RedditComment {
1384
+ body: string;
1385
+ author: string;
1386
+ score: number;
1387
+ created_utc: number;
1388
+ replies?: { data: { children: Array<{ data: RedditComment }> } };
1389
+ }
1390
+
1391
+ /**
1392
+ * Handle Reddit URLs via JSON API
1393
+ */
1394
+ async function handleReddit(url: string, timeout: number): Promise<RenderResult | null> {
1395
+ try {
1396
+ const parsed = new URL(url);
1397
+ if (!parsed.hostname.includes("reddit.com")) return null;
1398
+
1399
+ const fetchedAt = new Date().toISOString();
1400
+
1401
+ // Append .json to get JSON response
1402
+ let jsonUrl = `${url.replace(/\/$/, "")}.json`;
1403
+ if (parsed.search) {
1404
+ jsonUrl = `${url.replace(/\/$/, "").replace(parsed.search, "")}.json${parsed.search}`;
1405
+ }
1406
+
1407
+ const result = await loadPage(jsonUrl, { timeout });
1408
+ if (!result.ok) return null;
1409
+
1410
+ const data = JSON.parse(result.content);
1411
+ let md = "";
1412
+
1413
+ // Handle different Reddit URL types
1414
+ if (Array.isArray(data) && data.length >= 1) {
1415
+ // Post page (with comments)
1416
+ const postData = data[0]?.data?.children?.[0]?.data as RedditPost | undefined;
1417
+ if (postData) {
1418
+ md = `# ${postData.title}\n\n`;
1419
+ md += `**r/${postData.subreddit}** · u/${postData.author} · ${postData.score} points · ${postData.num_comments} comments\n`;
1420
+ md += `*${new Date(postData.created_utc * 1000).toISOString().split("T")[0]}*\n\n`;
1421
+
1422
+ if (postData.is_self && postData.selftext) {
1423
+ md += `---\n\n${postData.selftext}\n\n`;
1424
+ } else if (!postData.is_self) {
1425
+ md += `**Link:** ${postData.url}\n\n`;
1426
+ }
1427
+
1428
+ // Add comments if available
1429
+ if (data.length >= 2 && data[1]?.data?.children) {
1430
+ md += `---\n\n## Top Comments\n\n`;
1431
+ const comments = data[1].data.children.filter((c: { kind: string }) => c.kind === "t1").slice(0, 10);
1432
+
1433
+ for (const { data: comment } of comments as Array<{ data: RedditComment }>) {
1434
+ md += `### u/${comment.author} · ${comment.score} points\n\n`;
1435
+ md += `${comment.body}\n\n---\n\n`;
1436
+ }
1437
+ }
1438
+ }
1439
+ } else if (data?.data?.children) {
1440
+ // Subreddit or listing page
1441
+ const posts = data.data.children.slice(0, 20) as Array<{ data: RedditPost }>;
1442
+ const subreddit = posts[0]?.data?.subreddit;
1443
+
1444
+ md = `# r/${subreddit || "Reddit"}\n\n`;
1445
+ for (const { data: post } of posts) {
1446
+ md += `- **${post.title}** (${post.score} pts, ${post.num_comments} comments)\n`;
1447
+ md += ` by u/${post.author}\n\n`;
1448
+ }
1449
+ }
1450
+
1451
+ if (!md) return null;
1452
+
1453
+ const output = finalizeOutput(md);
1454
+ return {
1455
+ url,
1456
+ finalUrl: url,
1457
+ contentType: "text/markdown",
1458
+ method: "reddit",
1459
+ content: output.content,
1460
+ fetchedAt,
1461
+ truncated: output.truncated,
1462
+ notes: ["Fetched via Reddit JSON API"],
1463
+ };
1464
+ } catch {}
1465
+
1466
+ return null;
1467
+ }
1468
+
1469
+ // =============================================================================
1470
+ // NPM Special Handling
1471
+ // =============================================================================
1472
+
1473
+ /**
1474
+ * Handle NPM URLs via registry API
1475
+ */
1476
+ async function handleNpm(url: string, timeout: number): Promise<RenderResult | null> {
1477
+ try {
1478
+ const parsed = new URL(url);
1479
+ if (parsed.hostname !== "www.npmjs.com" && parsed.hostname !== "npmjs.com") return null;
1480
+
1481
+ // Extract package name from /package/[scope/]name
1482
+ const match = parsed.pathname.match(/^\/package\/(.+?)(?:\/|$)/);
1483
+ if (!match) return null;
1484
+
1485
+ let packageName = decodeURIComponent(match[1]);
1486
+ // Handle scoped packages: /package/@scope/name
1487
+ if (packageName.startsWith("@")) {
1488
+ const scopeMatch = parsed.pathname.match(/^\/package\/(@[^/]+\/[^/]+)/);
1489
+ if (scopeMatch) packageName = decodeURIComponent(scopeMatch[1]);
1490
+ }
1491
+
1492
+ const fetchedAt = new Date().toISOString();
1493
+
1494
+ // Fetch from npm registry - use /latest endpoint for smaller response
1495
+ const latestUrl = `https://registry.npmjs.org/${packageName}/latest`;
1496
+ const downloadsUrl = `https://api.npmjs.org/downloads/point/last-week/${encodeURIComponent(packageName)}`;
1497
+
1498
+ // Fetch package info and download stats in parallel
1499
+ const [result, downloadsResult] = await Promise.all([
1500
+ loadPage(latestUrl, { timeout }),
1501
+ loadPage(downloadsUrl, { timeout: Math.min(timeout, 5) }),
1502
+ ]);
1503
+
1504
+ if (!result.ok) return null;
1505
+
1506
+ // Parse download stats
1507
+ let weeklyDownloads: number | null = null;
1508
+ if (downloadsResult.ok) {
1509
+ try {
1510
+ const dlData = JSON.parse(downloadsResult.content) as { downloads?: number };
1511
+ weeklyDownloads = dlData.downloads ?? null;
1512
+ } catch {}
1513
+ }
1514
+
1515
+ let pkg: {
1516
+ name: string;
1517
+ version: string;
1518
+ description?: string;
1519
+ license?: string;
1520
+ homepage?: string;
1521
+ repository?: { url: string } | string;
1522
+ keywords?: string[];
1523
+ maintainers?: Array<{ name: string }>;
1524
+ dependencies?: Record<string, string>;
1525
+ readme?: string;
1526
+ };
1527
+
1528
+ try {
1529
+ pkg = JSON.parse(result.content);
1530
+ } catch {
1531
+ return null; // JSON parse failed (truncated response)
1532
+ }
1533
+
1534
+ let md = `# ${pkg.name}\n\n`;
1535
+ if (pkg.description) md += `${pkg.description}\n\n`;
1536
+
1537
+ md += `**Latest:** ${pkg.version || "unknown"}`;
1538
+ if (pkg.license) md += ` · **License:** ${typeof pkg.license === "string" ? pkg.license : pkg.license}`;
1539
+ md += "\n";
1540
+ if (weeklyDownloads !== null) {
1541
+ const formatted =
1542
+ weeklyDownloads >= 1_000_000
1543
+ ? `${(weeklyDownloads / 1_000_000).toFixed(1)}M`
1544
+ : weeklyDownloads >= 1_000
1545
+ ? `${(weeklyDownloads / 1_000).toFixed(1)}K`
1546
+ : String(weeklyDownloads);
1547
+ md += `**Weekly Downloads:** ${formatted}\n`;
1548
+ }
1549
+ md += "\n";
1550
+
1551
+ if (pkg.homepage) md += `**Homepage:** ${pkg.homepage}\n`;
1552
+ const repoUrl = typeof pkg.repository === "string" ? pkg.repository : pkg.repository?.url;
1553
+ if (repoUrl) md += `**Repository:** ${repoUrl.replace(/^git\+/, "").replace(/\.git$/, "")}\n`;
1554
+ if (pkg.keywords?.length) md += `**Keywords:** ${pkg.keywords.join(", ")}\n`;
1555
+ if (pkg.maintainers?.length) md += `**Maintainers:** ${pkg.maintainers.map((m) => m.name).join(", ")}\n`;
1556
+
1557
+ if (pkg.dependencies && Object.keys(pkg.dependencies).length > 0) {
1558
+ md += `\n## Dependencies\n\n`;
1559
+ for (const [dep, version] of Object.entries(pkg.dependencies)) {
1560
+ md += `- ${dep}: ${version}\n`;
1561
+ }
1562
+ }
1563
+
1564
+ if (pkg.readme) {
1565
+ md += `\n---\n\n## README\n\n${pkg.readme}\n`;
1566
+ }
1567
+
1568
+ const output = finalizeOutput(md);
1569
+ return {
1570
+ url,
1571
+ finalUrl: url,
1572
+ contentType: "text/markdown",
1573
+ method: "npm",
1574
+ content: output.content,
1575
+ fetchedAt,
1576
+ truncated: output.truncated,
1577
+ notes: ["Fetched via npm registry"],
1578
+ };
1579
+ } catch {}
1580
+
1581
+ return null;
1582
+ }
1583
+
1584
+ // =============================================================================
1585
+ // Crates.io Special Handling
1586
+ // =============================================================================
1587
+
1588
+ /**
1589
+ * Handle crates.io URLs via API
1590
+ */
1591
+ async function handleCratesIo(url: string, timeout: number): Promise<RenderResult | null> {
1592
+ try {
1593
+ const parsed = new URL(url);
1594
+ if (parsed.hostname !== "crates.io" && parsed.hostname !== "www.crates.io") return null;
1595
+
1596
+ // Extract crate name from /crates/name or /crates/name/version
1597
+ const match = parsed.pathname.match(/^\/crates\/([^/]+)/);
1598
+ if (!match) return null;
1599
+
1600
+ const crateName = decodeURIComponent(match[1]);
1601
+ const fetchedAt = new Date().toISOString();
1602
+
1603
+ // Fetch from crates.io API
1604
+ const apiUrl = `https://crates.io/api/v1/crates/${crateName}`;
1605
+ const result = await loadPage(apiUrl, {
1606
+ timeout,
1607
+ headers: { "User-Agent": "omp-web-fetch/1.0 (https://github.com/anthropics)" },
1608
+ });
1609
+
1610
+ if (!result.ok) return null;
1611
+
1612
+ let data: {
1613
+ crate: {
1614
+ name: string;
1615
+ description: string | null;
1616
+ downloads: number;
1617
+ recent_downloads: number;
1618
+ max_version: string;
1619
+ repository: string | null;
1620
+ homepage: string | null;
1621
+ documentation: string | null;
1622
+ categories: string[];
1623
+ keywords: string[];
1624
+ created_at: string;
1625
+ updated_at: string;
1626
+ };
1627
+ versions: Array<{
1628
+ num: string;
1629
+ downloads: number;
1630
+ created_at: string;
1631
+ license: string | null;
1632
+ rust_version: string | null;
1633
+ }>;
1634
+ };
1635
+
1636
+ try {
1637
+ data = JSON.parse(result.content);
1638
+ } catch {
1639
+ return null;
1640
+ }
1641
+
1642
+ const crate = data.crate;
1643
+ const latestVersion = data.versions?.[0];
1644
+
1645
+ // Format download counts
1646
+ const formatDownloads = (n: number): string =>
1647
+ n >= 1_000_000 ? `${(n / 1_000_000).toFixed(1)}M` : n >= 1_000 ? `${(n / 1_000).toFixed(1)}K` : String(n);
1648
+
1649
+ let md = `# ${crate.name}\n\n`;
1650
+ if (crate.description) md += `${crate.description}\n\n`;
1651
+
1652
+ md += `**Latest:** ${crate.max_version}`;
1653
+ if (latestVersion?.license) md += ` · **License:** ${latestVersion.license}`;
1654
+ if (latestVersion?.rust_version) md += ` · **MSRV:** ${latestVersion.rust_version}`;
1655
+ md += "\n";
1656
+ md += `**Downloads:** ${formatDownloads(crate.downloads)} total · ${formatDownloads(crate.recent_downloads)} recent\n\n`;
1657
+
1658
+ if (crate.repository) md += `**Repository:** ${crate.repository}\n`;
1659
+ if (crate.homepage && crate.homepage !== crate.repository) md += `**Homepage:** ${crate.homepage}\n`;
1660
+ if (crate.documentation) md += `**Docs:** ${crate.documentation}\n`;
1661
+ if (crate.keywords?.length) md += `**Keywords:** ${crate.keywords.join(", ")}\n`;
1662
+ if (crate.categories?.length) md += `**Categories:** ${crate.categories.join(", ")}\n`;
1663
+
1664
+ // Show recent versions
1665
+ if (data.versions?.length > 0) {
1666
+ md += `\n## Recent Versions\n\n`;
1667
+ for (const ver of data.versions.slice(0, 5)) {
1668
+ const date = ver.created_at.split("T")[0];
1669
+ md += `- **${ver.num}** (${date}) - ${formatDownloads(ver.downloads)} downloads\n`;
1670
+ }
1671
+ }
1672
+
1673
+ // Try to fetch README from docs.rs or repository
1674
+ const docsRsUrl = `https://docs.rs/crate/${crateName}/${crate.max_version}/source/README.md`;
1675
+ const readmeResult = await loadPage(docsRsUrl, { timeout: Math.min(timeout, 5) });
1676
+ if (readmeResult.ok && readmeResult.content.length > 100 && !looksLikeHtml(readmeResult.content)) {
1677
+ md += `\n---\n\n## README\n\n${readmeResult.content}\n`;
1678
+ }
1679
+
1680
+ const output = finalizeOutput(md);
1681
+ return {
1682
+ url,
1683
+ finalUrl: url,
1684
+ contentType: "text/markdown",
1685
+ method: "crates.io",
1686
+ content: output.content,
1687
+ fetchedAt,
1688
+ truncated: output.truncated,
1689
+ notes: ["Fetched via crates.io API"],
1690
+ };
1691
+ } catch {}
1692
+
1693
+ return null;
1694
+ }
1695
+
1696
+ // =============================================================================
1697
+ // arXiv Special Handling
1698
+ // =============================================================================
1699
+
1700
+ /**
1701
+ * Handle arXiv URLs - fetch abstract + optionally PDF
1702
+ */
1703
+ async function handleArxiv(url: string, timeout: number): Promise<RenderResult | null> {
1704
+ try {
1705
+ const parsed = new URL(url);
1706
+ if (parsed.hostname !== "arxiv.org") return null;
1707
+
1708
+ // Extract paper ID from various URL formats
1709
+ // /abs/1234.56789, /pdf/1234.56789, /abs/cs/0123456
1710
+ const match = parsed.pathname.match(/\/(abs|pdf)\/(.+?)(?:\.pdf)?$/);
1711
+ if (!match) return null;
1712
+
1713
+ const paperId = match[2];
1714
+ const fetchedAt = new Date().toISOString();
1715
+ const notes: string[] = [];
1716
+
1717
+ // Fetch metadata via arXiv API
1718
+ const apiUrl = `https://export.arxiv.org/api/query?id_list=${paperId}`;
1719
+ const result = await loadPage(apiUrl, { timeout });
1720
+
1721
+ if (!result.ok) return null;
1722
+
1723
+ // Parse the Atom feed response
1724
+ const doc = parseHtml(result.content, { parseNoneClosedTags: true });
1725
+ const entry = doc.querySelector("entry");
1726
+
1727
+ if (!entry) return null;
1728
+
1729
+ const title = entry.querySelector("title")?.text?.trim()?.replace(/\s+/g, " ");
1730
+ const summary = entry.querySelector("summary")?.text?.trim();
1731
+ const authors = entry
1732
+ .querySelectorAll("author name")
1733
+ .map((n) => n.text?.trim())
1734
+ .filter(Boolean);
1735
+ const published = entry.querySelector("published")?.text?.trim()?.split("T")[0];
1736
+ const categories = entry
1737
+ .querySelectorAll("category")
1738
+ .map((c) => c.getAttribute("term"))
1739
+ .filter(Boolean);
1740
+ const pdfLink = entry.querySelector('link[title="pdf"]')?.getAttribute("href");
1741
+
1742
+ let md = `# ${title || "arXiv Paper"}\n\n`;
1743
+ if (authors.length) md += `**Authors:** ${authors.join(", ")}\n`;
1744
+ if (published) md += `**Published:** ${published}\n`;
1745
+ if (categories.length) md += `**Categories:** ${categories.join(", ")}\n`;
1746
+ md += `**arXiv:** ${paperId}\n\n`;
1747
+ md += `---\n\n## Abstract\n\n${summary || "No abstract available."}\n\n`;
1748
+
1749
+ // If it was a PDF link or we want full content, try to fetch and convert PDF
1750
+ if (match[1] === "pdf" || parsed.pathname.includes(".pdf")) {
1751
+ if (pdfLink) {
1752
+ notes.push("Fetching PDF for full content...");
1753
+ const pdfResult = await fetchBinary(pdfLink, timeout);
1754
+ if (pdfResult.ok) {
1755
+ const converted = convertWithMarkitdown(pdfResult.buffer, ".pdf", timeout);
1756
+ if (converted.ok && converted.content.length > 500) {
1757
+ md += `---\n\n## Full Paper\n\n${converted.content}\n`;
1758
+ notes.push("PDF converted via markitdown");
1759
+ }
1760
+ }
1761
+ }
1762
+ }
1763
+
1764
+ const output = finalizeOutput(md);
1765
+ return {
1766
+ url,
1767
+ finalUrl: url,
1768
+ contentType: "text/markdown",
1769
+ method: "arxiv",
1770
+ content: output.content,
1771
+ fetchedAt,
1772
+ truncated: output.truncated,
1773
+ notes: notes.length ? notes : ["Fetched via arXiv API"],
1774
+ };
1775
+ } catch {}
1776
+
1777
+ return null;
1778
+ }
1779
+
1780
+ // =============================================================================
1781
+ // IACR ePrint Special Handling
1782
+ // =============================================================================
1783
+
1784
+ /**
1785
+ * Handle IACR Cryptology ePrint Archive URLs
1786
+ */
1787
+ async function handleIacr(url: string, timeout: number): Promise<RenderResult | null> {
1788
+ try {
1789
+ const parsed = new URL(url);
1790
+ if (parsed.hostname !== "eprint.iacr.org") return null;
1791
+
1792
+ // Extract paper ID from /year/number or /year/number.pdf
1793
+ const match = parsed.pathname.match(/\/(\d{4})\/(\d+)(?:\.pdf)?$/);
1794
+ if (!match) return null;
1795
+
1796
+ const [, year, number] = match;
1797
+ const paperId = `${year}/${number}`;
1798
+ const fetchedAt = new Date().toISOString();
1799
+ const notes: string[] = [];
1800
+
1801
+ // Fetch the HTML page for metadata
1802
+ const pageUrl = `https://eprint.iacr.org/${paperId}`;
1803
+ const result = await loadPage(pageUrl, { timeout });
1804
+
1805
+ if (!result.ok) return null;
1806
+
1807
+ const doc = parseHtml(result.content);
1808
+
1809
+ // Extract metadata from the page
1810
+ const title =
1811
+ doc.querySelector("h3.mb-3")?.text?.trim() ||
1812
+ doc.querySelector('meta[name="citation_title"]')?.getAttribute("content");
1813
+ const authors = doc
1814
+ .querySelectorAll('meta[name="citation_author"]')
1815
+ .map((m) => m.getAttribute("content"))
1816
+ .filter(Boolean);
1817
+ // Abstract is in <p> after <h5>Abstract</h5>
1818
+ const abstractHeading = doc.querySelectorAll("h5").find((h) => h.text?.includes("Abstract"));
1819
+ const abstract =
1820
+ abstractHeading?.parentNode?.querySelector("p")?.text?.trim() ||
1821
+ doc.querySelector('meta[name="description"]')?.getAttribute("content");
1822
+ const keywords = doc.querySelector(".keywords")?.text?.replace("Keywords:", "").trim();
1823
+ const pubDate = doc.querySelector('meta[name="citation_publication_date"]')?.getAttribute("content");
1824
+
1825
+ let md = `# ${title || "IACR ePrint Paper"}\n\n`;
1826
+ if (authors.length) md += `**Authors:** ${authors.join(", ")}\n`;
1827
+ if (pubDate) md += `**Date:** ${pubDate}\n`;
1828
+ md += `**ePrint:** ${paperId}\n`;
1829
+ if (keywords) md += `**Keywords:** ${keywords}\n`;
1830
+ md += `\n---\n\n## Abstract\n\n${abstract || "No abstract available."}\n\n`;
1831
+
1832
+ // If it was a PDF link, try to fetch and convert PDF
1833
+ if (parsed.pathname.endsWith(".pdf")) {
1834
+ const pdfUrl = `https://eprint.iacr.org/${paperId}.pdf`;
1835
+ notes.push("Fetching PDF for full content...");
1836
+ const pdfResult = await fetchBinary(pdfUrl, timeout);
1837
+ if (pdfResult.ok) {
1838
+ const converted = convertWithMarkitdown(pdfResult.buffer, ".pdf", timeout);
1839
+ if (converted.ok && converted.content.length > 500) {
1840
+ md += `---\n\n## Full Paper\n\n${converted.content}\n`;
1841
+ notes.push("PDF converted via markitdown");
1842
+ }
1843
+ }
1844
+ }
1845
+
1846
+ const output = finalizeOutput(md);
1847
+ return {
1848
+ url,
1849
+ finalUrl: url,
1850
+ contentType: "text/markdown",
1851
+ method: "iacr",
1852
+ content: output.content,
1853
+ fetchedAt,
1854
+ truncated: output.truncated,
1855
+ notes: notes.length ? notes : ["Fetched from IACR ePrint Archive"],
1856
+ };
1857
+ } catch {}
1858
+
1859
+ return null;
1860
+ }
1861
+
1862
+ // =============================================================================
1863
+ // GitHub Gist Special Handling
1864
+ // =============================================================================
1865
+
1866
+ /**
1867
+ * Handle GitHub Gist URLs via API
1868
+ */
1869
+ async function handleGitHubGist(url: string, timeout: number): Promise<RenderResult | null> {
1870
+ try {
1871
+ const parsed = new URL(url);
1872
+ if (parsed.hostname !== "gist.github.com") return null;
1873
+
1874
+ // Extract gist ID from /username/gistId or just /gistId
1875
+ const parts = parsed.pathname.split("/").filter(Boolean);
1876
+ if (parts.length === 0) return null;
1877
+
1878
+ // Gist ID is always the last path segment (or only segment for anonymous gists)
1879
+ const gistId = parts[parts.length - 1];
1880
+ if (!gistId || !/^[a-f0-9]+$/i.test(gistId)) return null;
1881
+
1882
+ const fetchedAt = new Date().toISOString();
1883
+
1884
+ // Fetch via GitHub API
1885
+ const result = await fetchGitHubApi(`/gists/${gistId}`, timeout);
1886
+ if (!result.ok || !result.data) return null;
1887
+
1888
+ const gist = result.data as {
1889
+ description: string | null;
1890
+ owner?: { login: string };
1891
+ created_at: string;
1892
+ updated_at: string;
1893
+ files: Record<string, { filename: string; language: string | null; size: number; content: string }>;
1894
+ html_url: string;
1895
+ };
1896
+
1897
+ const files = Object.values(gist.files);
1898
+ const owner = gist.owner?.login || "anonymous";
1899
+
1900
+ let md = `# Gist by ${owner}\n\n`;
1901
+ if (gist.description) md += `${gist.description}\n\n`;
1902
+ md += `**Created:** ${gist.created_at} · **Updated:** ${gist.updated_at}\n`;
1903
+ md += `**Files:** ${files.length}\n\n`;
1904
+
1905
+ for (const file of files) {
1906
+ const lang = file.language?.toLowerCase() || "";
1907
+ md += `---\n\n## ${file.filename}\n\n`;
1908
+ md += `\`\`\`${lang}\n${file.content}\n\`\`\`\n\n`;
1909
+ }
1910
+
1911
+ const output = finalizeOutput(md);
1912
+ return {
1913
+ url,
1914
+ finalUrl: url,
1915
+ contentType: "text/markdown",
1916
+ method: "github-gist",
1917
+ content: output.content,
1918
+ fetchedAt,
1919
+ truncated: output.truncated,
1920
+ notes: ["Fetched via GitHub API"],
1921
+ };
1922
+ } catch {}
1923
+
1924
+ return null;
1925
+ }
1926
+
1927
+ // =============================================================================
1928
+ // Unified Special Handler Dispatch
1929
+ // =============================================================================
1930
+
1931
+ /**
1932
+ * Try all special handlers
1933
+ */
1934
+ async function handleSpecialUrls(url: string, timeout: number): Promise<RenderResult | null> {
1935
+ // Order matters - more specific first
1936
+ return (
1937
+ (await handleGitHubGist(url, timeout)) ||
1938
+ (await handleGitHub(url, timeout)) ||
1939
+ (await handleTwitter(url, timeout)) ||
1940
+ (await handleStackOverflow(url, timeout)) ||
1941
+ (await handleWikipedia(url, timeout)) ||
1942
+ (await handleReddit(url, timeout)) ||
1943
+ (await handleNpm(url, timeout)) ||
1944
+ (await handleCratesIo(url, timeout)) ||
1945
+ (await handleArxiv(url, timeout)) ||
1946
+ (await handleIacr(url, timeout))
1947
+ );
1948
+ }
1949
+
1950
+ // =============================================================================
1951
+ // Main Render Function
1952
+ // =============================================================================
1953
+
1954
+ /**
1955
+ * Main render function implementing the full pipeline
1956
+ */
1957
+ async function renderUrl(url: string, timeout: number, raw: boolean = false): Promise<RenderResult> {
1958
+ const notes: string[] = [];
1959
+ const fetchedAt = new Date().toISOString();
1960
+
1961
+ // Step 0: Try special handlers for known sites (unless raw mode)
1962
+ if (!raw) {
1963
+ const specialResult = await handleSpecialUrls(url, timeout);
1964
+ if (specialResult) return specialResult;
1965
+ }
1966
+
1967
+ // Step 1: Normalize URL
1968
+ url = normalizeUrl(url);
1969
+ const origin = getOrigin(url);
1970
+
1971
+ // Step 2: Fetch page
1972
+ const response = await loadPage(url, { timeout });
1973
+ if (!response.ok) {
1974
+ return {
1975
+ url,
1976
+ finalUrl: url,
1977
+ contentType: "unknown",
1978
+ method: "failed",
1979
+ content: "",
1980
+ fetchedAt,
1981
+ truncated: false,
1982
+ notes: ["Failed to fetch URL"],
1983
+ };
1984
+ }
1985
+
1986
+ const { finalUrl, content: rawContent } = response;
1987
+ const mime = normalizeMime(response.contentType);
1988
+ const extHint = getExtensionHint(finalUrl);
1989
+
1990
+ // Step 3: Handle convertible binary files (PDF, DOCX, etc.)
1991
+ if (isConvertible(mime, extHint)) {
1992
+ const binary = await fetchBinary(finalUrl, timeout);
1993
+ if (binary.ok) {
1994
+ const ext = getExtensionHint(finalUrl, binary.contentDisposition) || extHint;
1995
+ const converted = convertWithMarkitdown(binary.buffer, ext, timeout);
1996
+ if (converted.ok && converted.content.trim().length > 50) {
1997
+ notes.push(`Converted with markitdown`);
1998
+ const output = finalizeOutput(converted.content);
1999
+ return {
2000
+ url,
2001
+ finalUrl,
2002
+ contentType: mime,
2003
+ method: "markitdown",
2004
+ content: output.content,
2005
+ fetchedAt,
2006
+ truncated: output.truncated,
2007
+ notes,
2008
+ };
2009
+ }
2010
+ }
2011
+ notes.push("markitdown conversion failed");
2012
+ }
2013
+
2014
+ // Step 4: Handle non-HTML text content
2015
+ const isHtml = mime.includes("html") || mime.includes("xhtml");
2016
+ const isJson = mime.includes("json");
2017
+ const isXml = mime.includes("xml") && !isHtml;
2018
+ const isText = mime.includes("text/plain") || mime.includes("text/markdown");
2019
+ const isFeed = mime.includes("rss") || mime.includes("atom") || mime.includes("feed");
2020
+
2021
+ if (isJson) {
2022
+ const output = finalizeOutput(formatJson(rawContent));
2023
+ return {
2024
+ url,
2025
+ finalUrl,
2026
+ contentType: mime,
2027
+ method: "json",
2028
+ content: output.content,
2029
+ fetchedAt,
2030
+ truncated: output.truncated,
2031
+ notes,
2032
+ };
2033
+ }
2034
+
2035
+ if (isFeed || (isXml && (rawContent.includes("<rss") || rawContent.includes("<feed")))) {
2036
+ const parsed = parseFeedToMarkdown(rawContent);
2037
+ const output = finalizeOutput(parsed);
2038
+ return {
2039
+ url,
2040
+ finalUrl,
2041
+ contentType: mime,
2042
+ method: "feed",
2043
+ content: output.content,
2044
+ fetchedAt,
2045
+ truncated: output.truncated,
2046
+ notes,
2047
+ };
2048
+ }
2049
+
2050
+ if (isText && !looksLikeHtml(rawContent)) {
2051
+ const output = finalizeOutput(rawContent);
2052
+ return {
2053
+ url,
2054
+ finalUrl,
2055
+ contentType: mime,
2056
+ method: "text",
2057
+ content: output.content,
2058
+ fetchedAt,
2059
+ truncated: output.truncated,
2060
+ notes,
2061
+ };
2062
+ }
2063
+
2064
+ // Step 5: For HTML, try digestible formats first (unless raw mode)
2065
+ if (isHtml && !raw) {
2066
+ // 5A: Check for page-specific markdown alternate
2067
+ const alternates = parseAlternateLinks(rawContent, finalUrl);
2068
+ const markdownAlt = alternates.find((alt) => alt.endsWith(".md") || alt.includes("markdown"));
2069
+ if (markdownAlt) {
2070
+ const resolved = markdownAlt.startsWith("http") ? markdownAlt : new URL(markdownAlt, finalUrl).href;
2071
+ const altResult = await loadPage(resolved, { timeout });
2072
+ if (altResult.ok && altResult.content.trim().length > 100 && !looksLikeHtml(altResult.content)) {
2073
+ notes.push(`Used markdown alternate: ${resolved}`);
2074
+ const output = finalizeOutput(altResult.content);
2075
+ return {
2076
+ url,
2077
+ finalUrl,
2078
+ contentType: "text/markdown",
2079
+ method: "alternate-markdown",
2080
+ content: output.content,
2081
+ fetchedAt,
2082
+ truncated: output.truncated,
2083
+ notes,
2084
+ };
2085
+ }
2086
+ }
2087
+
2088
+ // 5B: Try URL.md suffix (llms.txt convention)
2089
+ const mdSuffix = await tryMdSuffix(finalUrl, timeout);
2090
+ if (mdSuffix) {
2091
+ notes.push("Found .md suffix version");
2092
+ const output = finalizeOutput(mdSuffix);
2093
+ return {
2094
+ url,
2095
+ finalUrl,
2096
+ contentType: "text/markdown",
2097
+ method: "md-suffix",
2098
+ content: output.content,
2099
+ fetchedAt,
2100
+ truncated: output.truncated,
2101
+ notes,
2102
+ };
2103
+ }
2104
+
2105
+ // 5C: LLM-friendly endpoints
2106
+ const llmContent = await tryLlmEndpoints(origin, timeout);
2107
+ if (llmContent) {
2108
+ notes.push("Found llms.txt");
2109
+ const output = finalizeOutput(llmContent);
2110
+ return {
2111
+ url,
2112
+ finalUrl,
2113
+ contentType: "text/plain",
2114
+ method: "llms.txt",
2115
+ content: output.content,
2116
+ fetchedAt,
2117
+ truncated: output.truncated,
2118
+ notes,
2119
+ };
2120
+ }
2121
+
2122
+ // 5D: Content negotiation
2123
+ const negotiated = await tryContentNegotiation(url, timeout);
2124
+ if (negotiated) {
2125
+ notes.push(`Content negotiation returned ${negotiated.type}`);
2126
+ const output = finalizeOutput(negotiated.content);
2127
+ return {
2128
+ url,
2129
+ finalUrl,
2130
+ contentType: normalizeMime(negotiated.type),
2131
+ method: "content-negotiation",
2132
+ content: output.content,
2133
+ fetchedAt,
2134
+ truncated: output.truncated,
2135
+ notes,
2136
+ };
2137
+ }
2138
+
2139
+ // 5E: Check for feed alternates
2140
+ const feedAlternates = alternates.filter((alt) => !alt.endsWith(".md") && !alt.includes("markdown"));
2141
+ for (const altUrl of feedAlternates.slice(0, 2)) {
2142
+ const resolved = altUrl.startsWith("http") ? altUrl : new URL(altUrl, finalUrl).href;
2143
+ const altResult = await loadPage(resolved, { timeout });
2144
+ if (altResult.ok && altResult.content.trim().length > 200) {
2145
+ notes.push(`Used feed alternate: ${resolved}`);
2146
+ const parsed = parseFeedToMarkdown(altResult.content);
2147
+ const output = finalizeOutput(parsed);
2148
+ return {
2149
+ url,
2150
+ finalUrl,
2151
+ contentType: "application/feed",
2152
+ method: "alternate-feed",
2153
+ content: output.content,
2154
+ fetchedAt,
2155
+ truncated: output.truncated,
2156
+ notes,
2157
+ };
2158
+ }
2159
+ }
2160
+
2161
+ // Step 6: Render HTML with lynx
2162
+ if (!hasCommand("lynx")) {
2163
+ notes.push("lynx not installed");
2164
+ const output = finalizeOutput(rawContent);
2165
+ return {
2166
+ url,
2167
+ finalUrl,
2168
+ contentType: mime,
2169
+ method: "raw-html",
2170
+ content: output.content,
2171
+ fetchedAt,
2172
+ truncated: output.truncated,
2173
+ notes,
2174
+ };
2175
+ }
2176
+
2177
+ const lynxResult = renderWithLynx(rawContent, timeout);
2178
+ if (!lynxResult.ok) {
2179
+ notes.push("lynx failed");
2180
+ const output = finalizeOutput(rawContent);
2181
+ return {
2182
+ url,
2183
+ finalUrl,
2184
+ contentType: mime,
2185
+ method: "raw-html",
2186
+ content: output.content,
2187
+ fetchedAt,
2188
+ truncated: output.truncated,
2189
+ notes,
2190
+ };
2191
+ }
2192
+
2193
+ // Step 7: If lynx output is low quality, try extracting document links
2194
+ if (isLowQualityOutput(lynxResult.content)) {
2195
+ const docLinks = extractDocumentLinks(rawContent, finalUrl);
2196
+ if (docLinks.length > 0) {
2197
+ const docUrl = docLinks[0];
2198
+ const binary = await fetchBinary(docUrl, timeout);
2199
+ if (binary.ok) {
2200
+ const ext = getExtensionHint(docUrl, binary.contentDisposition);
2201
+ const converted = convertWithMarkitdown(binary.buffer, ext, timeout);
2202
+ if (converted.ok && converted.content.trim().length > lynxResult.content.length) {
2203
+ notes.push(`Extracted and converted document: ${docUrl}`);
2204
+ const output = finalizeOutput(converted.content);
2205
+ return {
2206
+ url,
2207
+ finalUrl,
2208
+ contentType: "application/document",
2209
+ method: "extracted-document",
2210
+ content: output.content,
2211
+ fetchedAt,
2212
+ truncated: output.truncated,
2213
+ notes,
2214
+ };
2215
+ }
2216
+ }
2217
+ }
2218
+ notes.push("Page appears to require JavaScript or is mostly navigation");
2219
+ }
2220
+
2221
+ const output = finalizeOutput(lynxResult.content);
2222
+ return {
2223
+ url,
2224
+ finalUrl,
2225
+ contentType: mime,
2226
+ method: "lynx",
2227
+ content: output.content,
2228
+ fetchedAt,
2229
+ truncated: output.truncated,
2230
+ notes,
2231
+ };
2232
+ }
2233
+
2234
+ // Fallback: return raw content
2235
+ const output = finalizeOutput(rawContent);
2236
+ return {
2237
+ url,
2238
+ finalUrl,
2239
+ contentType: mime,
2240
+ method: "raw",
2241
+ content: output.content,
2242
+ fetchedAt,
2243
+ truncated: output.truncated,
2244
+ notes,
2245
+ };
2246
+ }
2247
+
2248
+ // =============================================================================
2249
+ // Tool Definition
2250
+ // =============================================================================
2251
+
2252
+ const webFetchSchema = Type.Object({
2253
+ url: Type.String({ description: "The URL to fetch and render" }),
2254
+ timeout: Type.Optional(Type.Number({ description: "Timeout in seconds (default: 20, max: 120)" })),
2255
+ raw: Type.Optional(
2256
+ Type.Boolean({ description: "Return raw content without site-specific rendering or LLM-friendly transforms" }),
2257
+ ),
2258
+ });
2259
+
2260
+ export interface WebFetchToolDetails {
2261
+ url: string;
2262
+ finalUrl: string;
2263
+ contentType: string;
2264
+ method: string;
2265
+ truncated: boolean;
2266
+ notes: string[];
2267
+ }
2268
+
2269
+ export function createWebFetchTool(_cwd: string): AgentTool<typeof webFetchSchema> {
2270
+ return {
2271
+ name: "web_fetch",
2272
+ label: "web_fetch",
2273
+ description: webFetchDescription,
2274
+ parameters: webFetchSchema,
2275
+ execute: async (
2276
+ _toolCallId: string,
2277
+ { url, timeout = DEFAULT_TIMEOUT, raw = false }: { url: string; timeout?: number; raw?: boolean },
2278
+ ) => {
2279
+ // Clamp timeout
2280
+ const effectiveTimeout = Math.min(Math.max(timeout, 1), 120);
2281
+
2282
+ const result = await renderUrl(url, effectiveTimeout, raw);
2283
+
2284
+ // Format output
2285
+ let output = "";
2286
+ output += `URL: ${result.finalUrl}\n`;
2287
+ output += `Content-Type: ${result.contentType}\n`;
2288
+ output += `Method: ${result.method}\n`;
2289
+ if (result.truncated) {
2290
+ output += `Warning: Output was truncated\n`;
2291
+ }
2292
+ if (result.notes.length > 0) {
2293
+ output += `Notes: ${result.notes.join("; ")}\n`;
2294
+ }
2295
+ output += `\n---\n\n`;
2296
+ output += result.content;
2297
+
2298
+ const details: WebFetchToolDetails = {
2299
+ url: result.url,
2300
+ finalUrl: result.finalUrl,
2301
+ contentType: result.contentType,
2302
+ method: result.method,
2303
+ truncated: result.truncated,
2304
+ notes: result.notes,
2305
+ };
2306
+
2307
+ return {
2308
+ content: [{ type: "text", text: output }],
2309
+ details,
2310
+ };
2311
+ },
2312
+ };
2313
+ }
2314
+
2315
+ /** Default web fetch tool using process.cwd() - for backwards compatibility */
2316
+ export const webFetchTool = createWebFetchTool(process.cwd());
2317
+
2318
+ // =============================================================================
2319
+ // TUI Rendering
2320
+ // =============================================================================
2321
+
2322
+ import type { Component } from "@oh-my-pi/pi-tui";
2323
+ import { Text } from "@oh-my-pi/pi-tui";
2324
+ import { type Theme, theme } from "../../modes/interactive/theme/theme";
2325
+ import type { CustomTool, CustomToolContext, RenderResultOptions } from "../custom-tools/types";
2326
+
2327
+ /** Truncate text to max length with ellipsis */
2328
+ function truncate(text: string, maxLen: number, ellipsis: string): string {
2329
+ if (text.length <= maxLen) return text;
2330
+ const sliceLen = Math.max(0, maxLen - ellipsis.length);
2331
+ return `${text.slice(0, sliceLen)}${ellipsis}`;
2332
+ }
2333
+
2334
+ /** Extract domain from URL */
2335
+ function getDomain(url: string): string {
2336
+ try {
2337
+ const u = new URL(url);
2338
+ return u.hostname.replace(/^www\./, "");
2339
+ } catch {
2340
+ return url;
2341
+ }
2342
+ }
2343
+
2344
+ /** Get first N lines of text as preview */
2345
+ function getPreviewLines(text: string, maxLines: number, maxLineLen: number, ellipsis: string): string[] {
2346
+ const lines = text.split("\n").filter((l) => l.trim());
2347
+ return lines.slice(0, maxLines).map((l) => truncate(l.trim(), maxLineLen, ellipsis));
2348
+ }
2349
+
2350
+ /** Count non-empty lines */
2351
+ function countNonEmptyLines(text: string): number {
2352
+ return text.split("\n").filter((l) => l.trim()).length;
2353
+ }
2354
+
2355
+ /** Render web fetch call (URL preview) */
2356
+ export function renderWebFetchCall(
2357
+ args: { url: string; timeout?: number; raw?: boolean },
2358
+ uiTheme: Theme = theme,
2359
+ ): Component {
2360
+ const domain = getDomain(args.url);
2361
+ const path = truncate(args.url.replace(/^https?:\/\/[^/]+/, ""), 50, uiTheme.format.ellipsis);
2362
+ const icon = uiTheme.styledSymbol("status.pending", "muted");
2363
+ const text = `${icon} ${uiTheme.fg("toolTitle", "Web Fetch")} ${uiTheme.fg("accent", domain)}${uiTheme.fg("dim", path)}`;
2364
+ return new Text(text, 0, 0);
2365
+ }
2366
+
2367
+ /** Render web fetch result with tree-based layout */
2368
+ export function renderWebFetchResult(
2369
+ result: { content: Array<{ type: string; text?: string }>; details?: WebFetchToolDetails },
2370
+ options: RenderResultOptions,
2371
+ uiTheme: Theme = theme,
2372
+ ): Component {
2373
+ const { expanded } = options;
2374
+ const details = result.details;
2375
+
2376
+ if (!details) {
2377
+ return new Text(uiTheme.fg("error", "No response data"), 0, 0);
2378
+ }
2379
+
2380
+ const domain = getDomain(details.finalUrl);
2381
+ const hasRedirect = details.url !== details.finalUrl;
2382
+ const hasNotes = details.notes.length > 0;
2383
+ const statusIcon = details.truncated
2384
+ ? uiTheme.styledSymbol("status.warning", "warning")
2385
+ : uiTheme.styledSymbol("status.success", "success");
2386
+ const expandHint = expanded ? "" : uiTheme.fg("dim", " (Ctrl+O to expand)");
2387
+ let text = `${statusIcon} ${uiTheme.fg("toolTitle", "Web Fetch")} ${uiTheme.fg("accent", `(${domain})`)}${uiTheme.sep.dot}${uiTheme.fg("dim", details.method)}${expandHint}`;
2388
+
2389
+ // Get content text
2390
+ const contentText = result.content[0]?.text ?? "";
2391
+ // Extract just the content part (after the --- separator)
2392
+ const contentBody = contentText.includes("---\n\n")
2393
+ ? contentText.split("---\n\n").slice(1).join("---\n\n")
2394
+ : contentText;
2395
+ const lineCount = countNonEmptyLines(contentBody);
2396
+ const charCount = contentBody.trim().length;
2397
+
2398
+ if (!expanded) {
2399
+ // Collapsed view: metadata + preview
2400
+ const metaLines: string[] = [
2401
+ `${uiTheme.fg("muted", "Content-Type:")} ${details.contentType || "unknown"}`,
2402
+ `${uiTheme.fg("muted", "Method:")} ${details.method}`,
2403
+ ];
2404
+ if (hasRedirect) {
2405
+ metaLines.push(`${uiTheme.fg("muted", "Final URL:")} ${uiTheme.fg("mdLinkUrl", details.finalUrl)}`);
2406
+ }
2407
+ if (details.truncated) {
2408
+ metaLines.push(uiTheme.fg("warning", `${uiTheme.status.warning} Output truncated`));
2409
+ }
2410
+ if (hasNotes) {
2411
+ metaLines.push(`${uiTheme.fg("muted", "Notes:")} ${details.notes.join("; ")}`);
2412
+ }
2413
+
2414
+ const previewLines = getPreviewLines(contentBody, 3, 100, uiTheme.format.ellipsis);
2415
+ const detailLines: string[] = [...metaLines];
2416
+
2417
+ if (previewLines.length === 0) {
2418
+ detailLines.push(uiTheme.fg("dim", "(no content)"));
2419
+ } else {
2420
+ for (const line of previewLines) {
2421
+ detailLines.push(uiTheme.fg("dim", line));
2422
+ }
2423
+ }
2424
+
2425
+ const remaining = Math.max(0, lineCount - previewLines.length);
2426
+ if (remaining > 0) {
2427
+ detailLines.push(uiTheme.fg("muted", `${uiTheme.format.ellipsis} ${remaining} more lines`));
2428
+ } else {
2429
+ const lineLabel = `${lineCount} line${lineCount === 1 ? "" : "s"}`;
2430
+ detailLines.push(uiTheme.fg("muted", `${lineLabel}${uiTheme.sep.dot}${charCount} chars`));
2431
+ }
2432
+
2433
+ for (let i = 0; i < detailLines.length; i++) {
2434
+ const isLast = i === detailLines.length - 1;
2435
+ const branch = isLast ? uiTheme.tree.last : uiTheme.tree.vertical;
2436
+ text += `\n ${uiTheme.fg("dim", branch)} ${detailLines[i]}`;
2437
+ }
2438
+ } else {
2439
+ // Expanded view: structured metadata + bounded content preview
2440
+ const metaLines: string[] = [
2441
+ `${uiTheme.fg("muted", "Content-Type:")} ${details.contentType || "unknown"}`,
2442
+ `${uiTheme.fg("muted", "Method:")} ${details.method}`,
2443
+ ];
2444
+ if (hasRedirect) {
2445
+ metaLines.push(`${uiTheme.fg("muted", "Final URL:")} ${uiTheme.fg("mdLinkUrl", details.finalUrl)}`);
2446
+ }
2447
+ const lineLabel = `${lineCount} line${lineCount === 1 ? "" : "s"}`;
2448
+ metaLines.push(`${uiTheme.fg("muted", "Lines:")} ${lineLabel}`);
2449
+ metaLines.push(`${uiTheme.fg("muted", "Chars:")} ${charCount}`);
2450
+ if (details.truncated) {
2451
+ metaLines.push(uiTheme.fg("warning", `${uiTheme.status.warning} Output truncated`));
2452
+ }
2453
+ if (hasNotes) {
2454
+ metaLines.push(`${uiTheme.fg("muted", "Notes:")} ${details.notes.join("; ")}`);
2455
+ }
2456
+
2457
+ text += `\n ${uiTheme.fg("dim", uiTheme.tree.branch)} ${uiTheme.fg("accent", "Metadata")}`;
2458
+ for (let i = 0; i < metaLines.length; i++) {
2459
+ const isLast = i === metaLines.length - 1;
2460
+ const branch = isLast ? uiTheme.tree.last : uiTheme.tree.branch;
2461
+ text += `\n ${uiTheme.fg("dim", uiTheme.tree.vertical)} ${uiTheme.fg("dim", branch)} ${metaLines[i]}`;
2462
+ }
2463
+
2464
+ text += `\n ${uiTheme.fg("dim", uiTheme.tree.last)} ${uiTheme.fg("accent", "Content Preview")}`;
2465
+ const previewLines = getPreviewLines(contentBody, 12, 120, uiTheme.format.ellipsis);
2466
+ const remaining = Math.max(0, lineCount - previewLines.length);
2467
+ const contentPrefix = uiTheme.fg("dim", " ");
2468
+
2469
+ if (previewLines.length === 0) {
2470
+ text += `\n ${contentPrefix} ${uiTheme.fg("dim", "(no content)")}`;
2471
+ } else {
2472
+ for (const line of previewLines) {
2473
+ text += `\n ${contentPrefix} ${uiTheme.fg("dim", line)}`;
2474
+ }
2475
+ }
2476
+
2477
+ if (remaining > 0) {
2478
+ text += `\n ${contentPrefix} ${uiTheme.fg("muted", `${uiTheme.format.ellipsis} ${remaining} more lines`)}`;
2479
+ }
2480
+ }
2481
+
2482
+ return new Text(text, 0, 0);
2483
+ }
2484
+
2485
+ type WebFetchParams = { url: string; timeout?: number; raw?: boolean };
2486
+
2487
+ /** Web fetch tool as CustomTool (for TUI rendering support) */
2488
+ export const webFetchCustomTool: CustomTool<typeof webFetchSchema, WebFetchToolDetails> = {
2489
+ name: "web_fetch",
2490
+ label: "Web Fetch",
2491
+ description: webFetchDescription,
2492
+ parameters: webFetchSchema,
2493
+
2494
+ async execute(
2495
+ toolCallId: string,
2496
+ params: WebFetchParams,
2497
+ _onUpdate,
2498
+ _ctx: CustomToolContext,
2499
+ _signal?: AbortSignal,
2500
+ ) {
2501
+ return webFetchTool.execute(toolCallId, params);
2502
+ },
2503
+
2504
+ renderCall(args: WebFetchParams, uiTheme: Theme) {
2505
+ return renderWebFetchCall(args, uiTheme);
2506
+ },
2507
+
2508
+ renderResult(result, options: RenderResultOptions, uiTheme: Theme) {
2509
+ return renderWebFetchResult(result, options, uiTheme);
2510
+ },
2511
+ };