@aria-cli/tools 1.0.9 → 1.0.11

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 (241) hide show
  1. package/package.json +9 -5
  2. package/src/__tests__/web-fetch-download.test.ts +0 -433
  3. package/src/__tests__/web-tools.test.ts +0 -619
  4. package/src/ask-user-interaction.ts +0 -33
  5. package/src/cache/web-cache.ts +0 -110
  6. package/src/definitions/arion.ts +0 -118
  7. package/src/definitions/browser/browser.ts +0 -502
  8. package/src/definitions/browser/index.ts +0 -5
  9. package/src/definitions/browser/pw-downloads.ts +0 -142
  10. package/src/definitions/browser/pw-interactions.ts +0 -282
  11. package/src/definitions/browser/pw-responses.ts +0 -98
  12. package/src/definitions/browser/pw-session.ts +0 -405
  13. package/src/definitions/browser/pw-shared.ts +0 -85
  14. package/src/definitions/browser/pw-snapshot.ts +0 -383
  15. package/src/definitions/browser/pw-state.ts +0 -101
  16. package/src/definitions/browser/types.ts +0 -203
  17. package/src/definitions/code-intelligence.ts +0 -526
  18. package/src/definitions/core.ts +0 -118
  19. package/src/definitions/delegation.ts +0 -567
  20. package/src/definitions/deploy.ts +0 -73
  21. package/src/definitions/filesystem.ts +0 -217
  22. package/src/definitions/frg.ts +0 -67
  23. package/src/definitions/index.ts +0 -28
  24. package/src/definitions/memory.ts +0 -150
  25. package/src/definitions/messaging.ts +0 -734
  26. package/src/definitions/meta.ts +0 -392
  27. package/src/definitions/network.ts +0 -179
  28. package/src/definitions/outlook.ts +0 -318
  29. package/src/definitions/patch/apply-patch.ts +0 -235
  30. package/src/definitions/patch/fuzzy-match.ts +0 -217
  31. package/src/definitions/patch/index.ts +0 -1
  32. package/src/definitions/patch/patch-parser.ts +0 -297
  33. package/src/definitions/patch/sandbox-paths.ts +0 -129
  34. package/src/definitions/process/index.ts +0 -5
  35. package/src/definitions/process/process-registry.ts +0 -303
  36. package/src/definitions/process/process.ts +0 -456
  37. package/src/definitions/process/pty-keys.ts +0 -298
  38. package/src/definitions/process/session-slug.ts +0 -147
  39. package/src/definitions/quip.ts +0 -225
  40. package/src/definitions/search.ts +0 -67
  41. package/src/definitions/session-history.ts +0 -79
  42. package/src/definitions/shell.ts +0 -202
  43. package/src/definitions/slack.ts +0 -211
  44. package/src/definitions/web.ts +0 -119
  45. package/src/executors/apply-patch.ts +0 -1035
  46. package/src/executors/arion.ts +0 -199
  47. package/src/executors/code-intelligence.ts +0 -1179
  48. package/src/executors/deploy.ts +0 -1066
  49. package/src/executors/filesystem.ts +0 -1428
  50. package/src/executors/frg-freshness.ts +0 -743
  51. package/src/executors/frg.ts +0 -394
  52. package/src/executors/index.ts +0 -280
  53. package/src/executors/learning-meta.ts +0 -1367
  54. package/src/executors/lsp-client.ts +0 -355
  55. package/src/executors/memory.ts +0 -978
  56. package/src/executors/meta.ts +0 -293
  57. package/src/executors/process-registry.ts +0 -570
  58. package/src/executors/pty-session-store.ts +0 -43
  59. package/src/executors/pty.ts +0 -342
  60. package/src/executors/restart.ts +0 -133
  61. package/src/executors/search-freshness.ts +0 -249
  62. package/src/executors/search-types.ts +0 -98
  63. package/src/executors/search.ts +0 -89
  64. package/src/executors/self-diagnose.ts +0 -552
  65. package/src/executors/session-history.ts +0 -435
  66. package/src/executors/shell-safety.ts +0 -519
  67. package/src/executors/shell.ts +0 -1243
  68. package/src/executors/utils.ts +0 -40
  69. package/src/executors/web.ts +0 -786
  70. package/src/extraction/content-extraction.ts +0 -281
  71. package/src/extraction/index.ts +0 -5
  72. package/src/headless-control-contract.ts +0 -1149
  73. package/src/index.ts +0 -788
  74. package/src/local-control-http-auth.ts +0 -2
  75. package/src/mcp/client.ts +0 -218
  76. package/src/mcp/connection.ts +0 -568
  77. package/src/mcp/index.ts +0 -11
  78. package/src/mcp/jsonrpc.ts +0 -195
  79. package/src/mcp/types.ts +0 -199
  80. package/src/network-control-adapter.ts +0 -88
  81. package/src/network-runtime/address-types.ts +0 -218
  82. package/src/network-runtime/db-owner-fencing.ts +0 -91
  83. package/src/network-runtime/delivery-receipts.ts +0 -372
  84. package/src/network-runtime/direct-endpoint-authority.ts +0 -35
  85. package/src/network-runtime/index.ts +0 -316
  86. package/src/network-runtime/local-control-contract.ts +0 -784
  87. package/src/network-runtime/node-store-contract.ts +0 -46
  88. package/src/network-runtime/pair-route-contract.ts +0 -97
  89. package/src/network-runtime/peer-capabilities.ts +0 -48
  90. package/src/network-runtime/peer-principal-ref.ts +0 -20
  91. package/src/network-runtime/peer-state-machine.ts +0 -160
  92. package/src/network-runtime/protocol-schemas.ts +0 -265
  93. package/src/network-runtime/runtime-bootstrap-contract.ts +0 -83
  94. package/src/outlook/desktop-session.ts +0 -409
  95. package/src/policy.ts +0 -171
  96. package/src/providers/brave.ts +0 -80
  97. package/src/providers/duckduckgo.ts +0 -199
  98. package/src/providers/exa.ts +0 -85
  99. package/src/providers/firecrawl.ts +0 -77
  100. package/src/providers/index.ts +0 -8
  101. package/src/providers/jina.ts +0 -70
  102. package/src/providers/router.ts +0 -121
  103. package/src/providers/search-provider.ts +0 -74
  104. package/src/providers/tavily.ts +0 -74
  105. package/src/quip/desktop-session.ts +0 -435
  106. package/src/registry/index.ts +0 -1
  107. package/src/registry/registry.ts +0 -905
  108. package/src/runtime-socket-local-control-client.ts +0 -632
  109. package/src/security/dns-normalization.ts +0 -34
  110. package/src/security/dns-pinning.ts +0 -138
  111. package/src/security/external-content.ts +0 -129
  112. package/src/security/ssrf.ts +0 -207
  113. package/src/slack/desktop-session.ts +0 -493
  114. package/src/tool-factory.ts +0 -91
  115. package/src/types.ts +0 -1341
  116. package/src/utils/retry.ts +0 -163
  117. package/src/utils/safe-parse-json.ts +0 -176
  118. package/src/utils/url.ts +0 -20
  119. package/tests/benchmarks/registry.bench.ts +0 -57
  120. package/tests/cache/web-cache.test.ts +0 -147
  121. package/tests/critical-integration.test.ts +0 -1465
  122. package/tests/definitions/apply-patch.test.ts +0 -586
  123. package/tests/definitions/browser.test.ts +0 -495
  124. package/tests/definitions/delegation-pause-resume.test.ts +0 -758
  125. package/tests/definitions/execution.test.ts +0 -671
  126. package/tests/definitions/messaging-inbox-scope.test.ts +0 -229
  127. package/tests/definitions/messaging.test.ts +0 -1468
  128. package/tests/definitions/outlook.test.ts +0 -30
  129. package/tests/definitions/process.test.ts +0 -469
  130. package/tests/definitions/slack.test.ts +0 -28
  131. package/tests/definitions/tool-inventory.test.ts +0 -218
  132. package/tests/e2e/delegation-quest-orchestration.e2e.test.ts +0 -433
  133. package/tests/e2e/memory-tool-discovery-contract.e2e.test.ts +0 -81
  134. package/tests/executors/apply-patch.test.ts +0 -538
  135. package/tests/executors/arion.test.ts +0 -309
  136. package/tests/executors/conversation-primitives.test.ts +0 -250
  137. package/tests/executors/deploy.test.ts +0 -746
  138. package/tests/executors/filesystem-tools.test.ts +0 -357
  139. package/tests/executors/filesystem.test.ts +0 -959
  140. package/tests/executors/frg-freshness.test.ts +0 -136
  141. package/tests/executors/frg-merge.test.ts +0 -70
  142. package/tests/executors/frg-session-content.test.ts +0 -40
  143. package/tests/executors/frg.test.ts +0 -56
  144. package/tests/executors/memory-bugfixes.test.ts +0 -257
  145. package/tests/executors/memory-real-memoria.integration.test.ts +0 -316
  146. package/tests/executors/memory.test.ts +0 -853
  147. package/tests/executors/meta-tools.test.ts +0 -411
  148. package/tests/executors/meta.test.ts +0 -683
  149. package/tests/executors/path-containment.test.ts +0 -51
  150. package/tests/executors/process-registry.test.ts +0 -505
  151. package/tests/executors/pty.test.ts +0 -664
  152. package/tests/executors/quest-security.test.ts +0 -249
  153. package/tests/executors/read-file-media.test.ts +0 -230
  154. package/tests/executors/recall-knowledge-schema.test.ts +0 -209
  155. package/tests/executors/recall-tags.test.ts +0 -278
  156. package/tests/executors/remember-null-safety.contract.test.ts +0 -41
  157. package/tests/executors/restart.test.ts +0 -67
  158. package/tests/executors/search-unified.test.ts +0 -381
  159. package/tests/executors/session-history.test.ts +0 -340
  160. package/tests/executors/session-transcript.test.ts +0 -561
  161. package/tests/executors/shell-abort.test.ts +0 -416
  162. package/tests/executors/shell-env-blocklist.test.ts +0 -648
  163. package/tests/executors/shell-env-process.test.ts +0 -245
  164. package/tests/executors/shell-process-registry.test.ts +0 -334
  165. package/tests/executors/shell-tools.test.ts +0 -393
  166. package/tests/executors/shell.test.ts +0 -690
  167. package/tests/executors/web-abort-vs-timeout.test.ts +0 -213
  168. package/tests/executors/web-integration.test.ts +0 -633
  169. package/tests/executors/web-symlink.test.ts +0 -18
  170. package/tests/executors/web.test.ts +0 -1400
  171. package/tests/executors/write-stdin.test.ts +0 -145
  172. package/tests/extraction/content-extraction.test.ts +0 -153
  173. package/tests/guards/tools-default-test-lane.integration.test.ts +0 -21
  174. package/tests/guards/tools-package-test-commands.e2e.test.ts +0 -43
  175. package/tests/guards/tools-test-lane-manifest.contract.test.ts +0 -76
  176. package/tests/guards/tools-vitest-workspace-alias.contract.test.ts +0 -63
  177. package/tests/helpers/async-waits.ts +0 -53
  178. package/tests/integration/headless-control-contract.integration.test.ts +0 -153
  179. package/tests/integration/memory-tool-schema-parity.integration.test.ts +0 -67
  180. package/tests/integration/meta-tools-round-trip.integration.test.ts +0 -506
  181. package/tests/integration/quest-round-trip.test.ts +0 -303
  182. package/tests/integration/registry-executor-flow.test.ts +0 -85
  183. package/tests/integration.test.ts +0 -177
  184. package/tests/loading-tier.test.ts +0 -126
  185. package/tests/mcp/client-reconnect.test.ts +0 -267
  186. package/tests/mcp/connection.test.ts +0 -846
  187. package/tests/mcp/injectable-logger.test.ts +0 -83
  188. package/tests/mcp/jsonrpc.test.ts +0 -109
  189. package/tests/mcp/lifecycle.test.ts +0 -879
  190. package/tests/network-runtime/address-types.contract.test.ts +0 -143
  191. package/tests/network-runtime/continuity-bind-schema.contract.test.ts +0 -203
  192. package/tests/network-runtime/local-control-contract.test.ts +0 -869
  193. package/tests/network-runtime/local-control-invite-token.contract.test.ts +0 -146
  194. package/tests/network-runtime/node-store-contract.test.ts +0 -11
  195. package/tests/network-runtime/pair-protocol-nodeid.contract.test.ts +0 -15
  196. package/tests/network-runtime/peer-state-machine.contract.test.ts +0 -148
  197. package/tests/network-runtime/protocol-schemas.contract.test.ts +0 -512
  198. package/tests/network-runtime/relay-pending-nodeid.contract.test.ts +0 -62
  199. package/tests/network-runtime/runtime-bootstrap-contract.test.ts +0 -227
  200. package/tests/network-runtime/runtime-socket-local-control-client.test.ts +0 -621
  201. package/tests/network-runtime/wait-for-message-script.test.ts +0 -288
  202. package/tests/parallel.test.ts +0 -71
  203. package/tests/policy.test.ts +0 -184
  204. package/tests/print-default-test-lane.ts +0 -14
  205. package/tests/print-test-lane-manifest.ts +0 -22
  206. package/tests/providers/brave.test.ts +0 -159
  207. package/tests/providers/duckduckgo.test.ts +0 -207
  208. package/tests/providers/exa.test.ts +0 -175
  209. package/tests/providers/firecrawl.test.ts +0 -168
  210. package/tests/providers/jina.test.ts +0 -144
  211. package/tests/providers/router.test.ts +0 -328
  212. package/tests/providers/tavily.test.ts +0 -165
  213. package/tests/registry/discovery.test.ts +0 -154
  214. package/tests/registry/injectable-logger.test.ts +0 -230
  215. package/tests/registry/input-validation.test.ts +0 -361
  216. package/tests/registry/interface-completeness.test.ts +0 -85
  217. package/tests/registry/mcp-integration.test.ts +0 -103
  218. package/tests/registry/mcp-read-only-hint.test.ts +0 -60
  219. package/tests/registry/memoria-discovery.test.ts +0 -390
  220. package/tests/registry/nested-validation.test.ts +0 -283
  221. package/tests/registry/pseudo-tool-filtering.test.ts +0 -258
  222. package/tests/registry/registration-lifecycle.test.ts +0 -133
  223. package/tests/registry-validation.test.ts +0 -424
  224. package/tests/registry.test.ts +0 -460
  225. package/tests/security/dns-pinning.test.ts +0 -162
  226. package/tests/security/external-content.test.ts +0 -144
  227. package/tests/security/ssrf.test.ts +0 -118
  228. package/tests/shell-safety-integration.test.ts +0 -32
  229. package/tests/shell-safety.test.ts +0 -365
  230. package/tests/slack/desktop-session.test.ts +0 -50
  231. package/tests/test-lane-manifest.ts +0 -440
  232. package/tests/test-utils.ts +0 -27
  233. package/tests/tool-factory.test.ts +0 -188
  234. package/tests/utils/retry.test.ts +0 -231
  235. package/tests/utils/url.test.ts +0 -63
  236. package/tsconfig.cjs.json +0 -24
  237. package/tsconfig.json +0 -12
  238. package/vitest.config.ts +0 -55
  239. package/vitest.e2e.config.ts +0 -24
  240. package/vitest.integration.config.ts +0 -24
  241. package/vitest.native.config.ts +0 -24
@@ -1,786 +0,0 @@
1
- /**
2
- * @aria/tools - Web tool executors
3
- *
4
- * Implementation of web operations for ARIA tool system.
5
- */
6
-
7
- import { createHash } from "node:crypto";
8
- import { log } from "@aria-cli/types";
9
- import { z } from "zod";
10
- import type { ToolContext, ToolResult } from "../types.js";
11
- import { success, fail, getErrorMessage } from "./utils.js";
12
- import { safeParseJson } from "../utils/safe-parse-json.js";
13
- import { validateUrlStructure, followRedirects, discardResponseBody } from "../security/ssrf.js";
14
- import { SearchProviderRouter } from "../providers/router.js";
15
- import {
16
- TavilySearchProvider,
17
- DuckDuckGoSearchProvider,
18
- BraveSearchProvider,
19
- JinaSearchProvider,
20
- ExaSearchProvider,
21
- FirecrawlSearchProvider,
22
- } from "../providers/index.js";
23
- import type { SearchProviderEnv, SearchOptions } from "../providers/search-provider.js";
24
- import { resolveSearchProviderEnv } from "../providers/search-provider.js";
25
- import {
26
- searchCache,
27
- fetchCache,
28
- browseCache,
29
- type CachedFetchOutput,
30
- type CachedBrowseOutput,
31
- } from "../cache/web-cache.js";
32
- import { fetchWithSsrf } from "../utils/retry.js";
33
- import { wrapExternalContent } from "../security/external-content.js";
34
- import { extractFromResponse } from "../extraction/content-extraction.js";
35
- import { normalizeGitHubUrl, getUserAgent } from "../utils/url.js";
36
-
37
- function buildSearchEnv(ctx: ToolContext): SearchProviderEnv {
38
- const env = ctx.env ?? {};
39
- return {
40
- ARIA_SEARCH_PROVIDER: env.ARIA_SEARCH_PROVIDER ?? process.env.ARIA_SEARCH_PROVIDER,
41
- BRAVE_API_KEY: env.BRAVE_API_KEY ?? process.env.BRAVE_API_KEY,
42
- FIRECRAWL_API_KEY: env.FIRECRAWL_API_KEY ?? process.env.FIRECRAWL_API_KEY,
43
- EXA_API_KEY: env.EXA_API_KEY ?? process.env.EXA_API_KEY,
44
- TAVILY_API_KEY: env.TAVILY_API_KEY ?? process.env.TAVILY_API_KEY,
45
- JINA_API_KEY: env.JINA_API_KEY ?? process.env.JINA_API_KEY,
46
- };
47
- }
48
-
49
- function createSearchRouter(env: SearchProviderEnv): SearchProviderRouter {
50
- return new SearchProviderRouter(
51
- [
52
- new BraveSearchProvider(env),
53
- new TavilySearchProvider(env),
54
- new JinaSearchProvider(env),
55
- new ExaSearchProvider(env),
56
- new FirecrawlSearchProvider(env),
57
- new DuckDuckGoSearchProvider(),
58
- ],
59
- env,
60
- );
61
- }
62
-
63
- /** Default timeout for fetch requests in milliseconds */
64
- const DEFAULT_TIMEOUT_MS = 30_000;
65
-
66
- /** Default maximum response size in bytes for fetch (1MB) */
67
- const FETCH_MAX_SIZE_BYTES = 1 * 1024 * 1024;
68
-
69
- /** Default maximum response size in bytes for browse (2MB) */
70
- const BROWSE_MAX_SIZE_BYTES = 2 * 1024 * 1024;
71
-
72
- const JsonFetchContentSchema = z.union([
73
- z.record(z.string(), z.unknown()),
74
- z.array(z.unknown()),
75
- z.string(),
76
- z.number(),
77
- z.boolean(),
78
- z.null(),
79
- ]);
80
-
81
- /**
82
- * Creates an AbortController with a timeout.
83
- */
84
- function createTimeoutController(timeoutMs: number): {
85
- controller: AbortController;
86
- timeoutId: NodeJS.Timeout;
87
- } {
88
- const controller = new AbortController();
89
- const timeoutId = setTimeout(() => {
90
- if (!controller.signal.aborted) {
91
- controller.abort();
92
- }
93
- }, timeoutMs);
94
- return { controller, timeoutId };
95
- }
96
-
97
- /**
98
- * Links a parent AbortSignal to a child AbortController, forwarding abort.
99
- * Returns a cleanup function that MUST be called when the operation completes
100
- * to prevent the listener from firing after the try/catch scope has exited.
101
- */
102
- function linkAbortSignal(
103
- parentSignal: AbortSignal | undefined,
104
- childController: AbortController,
105
- ): () => void {
106
- if (!parentSignal) return () => {};
107
-
108
- const onAbort = () => {
109
- if (!childController.signal.aborted) {
110
- childController.abort();
111
- }
112
- };
113
-
114
- parentSignal.addEventListener("abort", onAbort, { once: true });
115
- return () => {
116
- parentSignal.removeEventListener("abort", onAbort);
117
- };
118
- }
119
-
120
- interface ReadResponseResult {
121
- text: string;
122
- truncated: boolean;
123
- contentBytes: number;
124
- }
125
-
126
- /**
127
- * Reads a response body with a streaming byte limit.
128
- * Unlike checking Content-Length alone, this protects against chunked/streaming
129
- * responses that lack a Content-Length header.
130
- *
131
- * When `truncate` is false (default), throws if the limit is exceeded.
132
- * When `truncate` is true, returns whatever was read up to the limit.
133
- */
134
- async function readResponseWithLimit(
135
- response: Response,
136
- maxBytes: number,
137
- options?: { truncate?: boolean },
138
- ): Promise<ReadResponseResult> {
139
- const body = response.body;
140
- if (!body) {
141
- return { text: "", truncated: false, contentBytes: 0 };
142
- }
143
-
144
- const truncateMode = options?.truncate ?? false;
145
- const reader = body.getReader();
146
- const decoder = new TextDecoder();
147
- const chunks: string[] = [];
148
- let totalBytes = 0;
149
-
150
- try {
151
- for (;;) {
152
- const { done, value } = await reader.read();
153
- if (done) break;
154
-
155
- const chunkBytes = value.byteLength;
156
- if (totalBytes + chunkBytes > maxBytes) {
157
- if (truncateMode) {
158
- // Keep what we have so far (exclude the chunk that exceeded the limit)
159
- reader.cancel();
160
- chunks.push(decoder.decode());
161
- return {
162
- text: chunks.join(""),
163
- truncated: true,
164
- contentBytes: totalBytes,
165
- };
166
- }
167
- reader.cancel();
168
- throw new Error(`Response body exceeds maximum size of ${maxBytes} bytes`);
169
- }
170
- totalBytes += chunkBytes;
171
- chunks.push(decoder.decode(value, { stream: true }));
172
- }
173
- // Flush the decoder
174
- chunks.push(decoder.decode());
175
- return { text: chunks.join(""), truncated: false, contentBytes: totalBytes };
176
- } catch (err) {
177
- reader.cancel();
178
- throw err;
179
- }
180
- }
181
-
182
- // ============================================================================
183
- // Web Search
184
- // ============================================================================
185
-
186
- export interface WebSearchInput {
187
- /** Search query */
188
- query: string;
189
- /** Maximum number of results */
190
- limit?: number;
191
- /** Search topic classification */
192
- topic?: "general" | "news";
193
- /** Restrict results to these domains */
194
- domains?: string[];
195
- /** Exclude results from these domains */
196
- excludeDomains?: string[];
197
- /** Restrict results by freshness window */
198
- timeRange?: "day" | "week" | "month" | "year";
199
- }
200
-
201
- export interface WebSearchResult {
202
- title: string;
203
- url: string;
204
- content: string;
205
- score?: number;
206
- }
207
-
208
- export interface WebSearchOutput {
209
- query: string;
210
- results: WebSearchResult[];
211
- }
212
-
213
- function ensureWrappedExternalContent(
214
- content: string,
215
- source: Parameters<typeof wrapExternalContent>[1],
216
- ): string {
217
- return wrapExternalContent(content, source).content;
218
- }
219
-
220
- function wrapWebSearchOutput(output: WebSearchOutput): WebSearchOutput {
221
- return {
222
- query: output.query,
223
- results: output.results.map((result) => ({
224
- ...result,
225
- content: ensureWrappedExternalContent(result.content, "web_search"),
226
- })),
227
- };
228
- }
229
-
230
- function hasAdvancedSearchOptions(input: WebSearchInput): boolean {
231
- return (
232
- input.limit !== undefined ||
233
- input.topic !== undefined ||
234
- (input.domains?.length ?? 0) > 0 ||
235
- (input.excludeDomains?.length ?? 0) > 0 ||
236
- input.timeRange !== undefined
237
- );
238
- }
239
-
240
- function buildSearchCachePayload(query: string, options: SearchOptions): string {
241
- return JSON.stringify({ query, ...options });
242
- }
243
-
244
- function buildRouterSearchCacheKey(
245
- env: SearchProviderEnv,
246
- query: string,
247
- options: SearchOptions,
248
- ): string {
249
- const resolved = resolveSearchProviderEnv(env);
250
- const providerOverride = resolved.ARIA_SEARCH_PROVIDER ?? "auto";
251
- const providerAvailability = [
252
- resolved.BRAVE_API_KEY ? "brave=1" : "brave=0",
253
- resolved.TAVILY_API_KEY ? "tavily=1" : "tavily=0",
254
- resolved.JINA_API_KEY ? "jina=1" : "jina=0",
255
- resolved.EXA_API_KEY ? "exa=1" : "exa=0",
256
- resolved.FIRECRAWL_API_KEY ? "firecrawl=1" : "firecrawl=0",
257
- ].join(",");
258
- return `search:router:${providerOverride}:${providerAvailability}:${buildSearchCachePayload(query, options)}`;
259
- }
260
-
261
- function buildNativeSearchCacheKey(
262
- providerName: string,
263
- query: string,
264
- options: SearchOptions,
265
- ): string {
266
- return `search:native:${providerName}:${buildSearchCachePayload(query, options)}`;
267
- }
268
-
269
- /**
270
- * Searches the web using the SearchProviderRouter with automatic fallback.
271
- * Providers are selected based on available API keys (Brave, Tavily, Jina, Exa,
272
- * Firecrawl, DuckDuckGo). DuckDuckGo is always available as a last resort.
273
- *
274
- * Results are cached via LRU+TTL cache for 15 minutes.
275
- */
276
- export async function executeWebSearch(
277
- input: WebSearchInput,
278
- ctx: ToolContext,
279
- ): Promise<ToolResult> {
280
- const limit = input.limit ?? 10;
281
- const searchOptions: SearchOptions = {
282
- limit,
283
- ...(input.topic ? { topic: input.topic } : {}),
284
- ...(input.domains ? { domains: input.domains } : {}),
285
- ...(input.excludeDomains ? { excludeDomains: input.excludeDomains } : {}),
286
- ...(input.timeRange ? { timeRange: input.timeRange } : {}),
287
- };
288
- const searchEnv = buildSearchEnv(ctx);
289
-
290
- const advancedOptionsProvided = hasAdvancedSearchOptions(input);
291
- const nativeSearchAvailable = Boolean(
292
- ctx.nativeSearchAdapter && ctx.providerContext?.capabilities?.nativeSearch,
293
- );
294
- const nativeProviderName = ctx.providerContext?.name ?? "unknown";
295
-
296
- const routerCacheKey = buildRouterSearchCacheKey(searchEnv, input.query, searchOptions);
297
- const nativeCacheKey =
298
- nativeSearchAvailable && !advancedOptionsProvided
299
- ? buildNativeSearchCacheKey(nativeProviderName, input.query, searchOptions)
300
- : undefined;
301
-
302
- if (nativeCacheKey) {
303
- const cachedNative = searchCache.get(nativeCacheKey);
304
- if (cachedNative) {
305
- return success(
306
- `Found ${cachedNative.results.length} cached results for "${input.query}"`,
307
- wrapWebSearchOutput(cachedNative),
308
- );
309
- }
310
- } else {
311
- const cachedRouter = searchCache.get(routerCacheKey);
312
- if (cachedRouter) {
313
- return success(
314
- `Found ${cachedRouter.results.length} cached results for "${input.query}"`,
315
- wrapWebSearchOutput(cachedRouter),
316
- );
317
- }
318
- }
319
-
320
- if (advancedOptionsProvided && nativeSearchAvailable) {
321
- log.debug("[web_search] advanced options provided; bypassing native adapter");
322
- }
323
-
324
- // Native search routing: If provider supports native search and adapter is wired, use it
325
- // Phase 1: Gemini isolated adapter (router-based callback pattern)
326
- if (nativeSearchAvailable && ctx.nativeSearchAdapter && !advancedOptionsProvided) {
327
- try {
328
- const rawResults = await ctx.nativeSearchAdapter(input.query);
329
- if (rawResults.length === 0) {
330
- throw new Error("Native search returned no results");
331
- }
332
- const results: WebSearchResult[] = rawResults.map((r) => ({
333
- title: r.title,
334
- url: r.url,
335
- content: r.content,
336
- score: r.score,
337
- }));
338
- const output: WebSearchOutput = { query: input.query, results };
339
- searchCache.set(nativeCacheKey!, output);
340
- return success(
341
- `Found ${results.length} results for "${input.query}" (native search)`,
342
- wrapWebSearchOutput(output),
343
- );
344
- } catch (err) {
345
- // Fall through to SearchProviderRouter on native search failure
346
- log.debug(`[web_search] native adapter failed, falling back: ${getErrorMessage(err)}`);
347
- const cachedRouterFallback = searchCache.get(routerCacheKey);
348
- if (cachedRouterFallback) {
349
- return success(
350
- `Found ${cachedRouterFallback.results.length} cached results for "${input.query}"`,
351
- wrapWebSearchOutput(cachedRouterFallback),
352
- );
353
- }
354
- }
355
- }
356
- const searchRouter = createSearchRouter(searchEnv);
357
-
358
- try {
359
- // Race search against context abort signal for cancellation support
360
- let results: Awaited<ReturnType<typeof searchRouter.search>>;
361
- if (ctx.abortSignal) {
362
- const abortPromise = new Promise<never>((_resolve, reject) => {
363
- if (ctx.abortSignal!.aborted) {
364
- reject(new DOMException("The operation was aborted", "AbortError"));
365
- return;
366
- }
367
- ctx.abortSignal!.addEventListener(
368
- "abort",
369
- () => reject(new DOMException("The operation was aborted", "AbortError")),
370
- { once: true },
371
- );
372
- });
373
- results = await Promise.race([searchRouter.search(input.query, searchOptions), abortPromise]);
374
- } else {
375
- results = await searchRouter.search(input.query, searchOptions);
376
- }
377
-
378
- // Normalize results to WebSearchResult shape (cache raw; wrap at return-time)
379
- const webResults: WebSearchResult[] = results.map((r) => ({
380
- title: r.title,
381
- url: r.url,
382
- content: r.content,
383
- score: r.score,
384
- }));
385
-
386
- const output: WebSearchOutput = { query: input.query, results: webResults };
387
- searchCache.set(routerCacheKey, output);
388
-
389
- return success(
390
- `Found ${webResults.length} results for "${input.query}"`,
391
- wrapWebSearchOutput(output),
392
- );
393
- } catch (err) {
394
- if (ctx.abortSignal?.aborted) {
395
- return fail("Web search cancelled");
396
- }
397
- return fail(`Web search failed: ${getErrorMessage(err)}`);
398
- }
399
- }
400
-
401
- // ============================================================================
402
- // Web Fetch
403
- // ============================================================================
404
-
405
- export interface WebFetchInput {
406
- /** URL to fetch */
407
- url: string;
408
- /** Response format: 'text', 'html', or 'json' */
409
- format?: "text" | "html" | "json";
410
- /** Custom headers to include in the request */
411
- headers?: Record<string, string>;
412
- /** Request timeout in milliseconds (default: 30000) */
413
- timeoutMs?: number;
414
- /** Maximum response size in bytes (default: 1MB) */
415
- maxSizeBytes?: number;
416
- }
417
-
418
- export interface WebFetchOutput {
419
- content: unknown;
420
- status: number;
421
- contentType?: string;
422
- fromCache: boolean;
423
- fetchedAt: string;
424
- finalUrl: string;
425
- contentBytes: number;
426
- truncated: boolean;
427
- }
428
-
429
- function wrapWebFetchOutput(output: WebFetchOutput): WebFetchOutput {
430
- if (typeof output.content !== "string") {
431
- return output;
432
- }
433
- return {
434
- ...output,
435
- content: ensureWrappedExternalContent(output.content, "web_fetch"),
436
- };
437
- }
438
-
439
- function markFetchOutputFromCache(cached: CachedFetchOutput): WebFetchOutput {
440
- return {
441
- ...cached,
442
- fromCache: true,
443
- };
444
- }
445
-
446
- function normalizeRequestHeaders(
447
- headers: Record<string, string> | undefined,
448
- ): Array<[string, string]> {
449
- if (!headers) {
450
- return [];
451
- }
452
- return Object.entries(headers)
453
- .map(([name, value]) => [name.trim().toLowerCase(), value.trim()] as [string, string])
454
- .sort(([left], [right]) => left.localeCompare(right));
455
- }
456
-
457
- function hashRequestHeaders(headers: Record<string, string> | undefined): string {
458
- const normalizedHeaders = normalizeRequestHeaders(headers);
459
- if (normalizedHeaders.length === 0) {
460
- return "none";
461
- }
462
- return createHash("sha256").update(JSON.stringify(normalizedHeaders)).digest("hex");
463
- }
464
-
465
- function createFetchCacheKey({
466
- url,
467
- format,
468
- headers,
469
- timeoutMs,
470
- maxSizeBytes,
471
- }: {
472
- url: string;
473
- format: "text" | "html" | "json";
474
- headers: Record<string, string> | undefined;
475
- timeoutMs: number;
476
- maxSizeBytes: number;
477
- }): string {
478
- return `fetch:${url}:${format}:headers=${hashRequestHeaders(headers)}:maxSizeBytes=${maxSizeBytes}:timeoutMs=${timeoutMs}`;
479
- }
480
-
481
- /**
482
- * Fetches content from a URL with retry resilience, caching, and security wrapping.
483
- * Uses fetchWithRetry for automatic retry on transient failures (429, 5xx, ECONNRESET).
484
- * Results are cached via LRU+TTL cache for 15 minutes.
485
- */
486
- export async function executeWebFetch(input: WebFetchInput, ctx: ToolContext): Promise<ToolResult> {
487
- // Normalize GitHub blob URLs to raw content URLs before fetching
488
- const url = normalizeGitHubUrl(input.url);
489
-
490
- // Validate URL structure. DNS/SSRF safety is enforced by fetchWithSsrf
491
- // so validation and fetch share one trust boundary.
492
- const urlError = validateUrlStructure(url);
493
- if (urlError) {
494
- return fail(urlError);
495
- }
496
-
497
- const format = input.format ?? "text";
498
- const timeoutMs = input.timeoutMs ?? DEFAULT_TIMEOUT_MS;
499
- const maxSizeBytes = input.maxSizeBytes ?? FETCH_MAX_SIZE_BYTES;
500
-
501
- // Check cache first (use normalized URL + request variants to avoid collisions)
502
- const cacheKey = createFetchCacheKey({
503
- url,
504
- format,
505
- headers: input.headers,
506
- timeoutMs,
507
- maxSizeBytes,
508
- });
509
- const cached = fetchCache.get(cacheKey);
510
- if (cached) {
511
- return success(
512
- `Fetched ${input.url} (cached)`,
513
- wrapWebFetchOutput(markFetchOutputFromCache(cached)),
514
- );
515
- }
516
-
517
- const { controller, timeoutId } = createTimeoutController(timeoutMs);
518
-
519
- // Forward context abort signal to our controller
520
- const unlinkAbort = linkAbortSignal(ctx.abortSignal, controller);
521
-
522
- try {
523
- const fetchOptions: RequestInit = {
524
- headers: input.headers ?? {},
525
- signal: controller.signal,
526
- redirect: "manual",
527
- };
528
-
529
- // Use DNS-pinned SSRF fetch wrapper with transient retry support.
530
- const initialResponse = await fetchWithSsrf(url, fetchOptions, {
531
- maxAttempts: 2,
532
- });
533
-
534
- // Follow redirects manually and keep each hop on the same DNS-pinned path.
535
- const response = await followRedirects(initialResponse, fetchOptions, {
536
- baseUrl: url,
537
- fetchFn: (redirectUrl, redirectInit) =>
538
- fetchWithSsrf(redirectUrl, redirectInit, { maxAttempts: 2 }),
539
- validateRedirectUrl: validateUrlStructure,
540
- });
541
-
542
- clearTimeout(timeoutId);
543
-
544
- if (!response.ok) {
545
- await discardResponseBody(response);
546
- unlinkAbort();
547
- return fail(`HTTP error: ${response.status} ${response.statusText}`, {
548
- status: response.status,
549
- statusText: response.statusText,
550
- });
551
- }
552
-
553
- // Check Content-Length header for size limit
554
- const contentLength = response.headers.get("Content-Length");
555
- if (contentLength) {
556
- const size = parseInt(contentLength, 10);
557
- if (!isNaN(size) && size > maxSizeBytes) {
558
- await discardResponseBody(response);
559
- unlinkAbort();
560
- return fail(`Response too large: ${size} bytes exceeds maximum of ${maxSizeBytes} bytes`);
561
- }
562
- }
563
-
564
- const contentType = response.headers.get("Content-Type") ?? undefined;
565
- let content: unknown;
566
-
567
- // Use streaming reader to enforce size limit even for chunked responses
568
- let text: string;
569
- let contentBytes = 0;
570
- try {
571
- const result = await readResponseWithLimit(response, maxSizeBytes);
572
- text = result.text;
573
- contentBytes = result.contentBytes;
574
- if (result.truncated) {
575
- unlinkAbort();
576
- return fail(`Response body exceeds maximum size of ${maxSizeBytes} bytes`);
577
- }
578
- } catch (sizeErr) {
579
- unlinkAbort();
580
- return fail(getErrorMessage(sizeErr));
581
- }
582
-
583
- if (format === "json") {
584
- const parsed = safeParseJson(text, JsonFetchContentSchema);
585
- if (!parsed.ok) {
586
- unlinkAbort();
587
- return fail(`Failed to parse JSON response (${parsed.reason})`);
588
- }
589
- content = parsed.data;
590
- } else {
591
- content = text;
592
- }
593
-
594
- const output: WebFetchOutput = {
595
- content,
596
- status: response.status,
597
- contentType,
598
- fromCache: false,
599
- fetchedAt: new Date().toISOString(),
600
- finalUrl: response.url || url,
601
- contentBytes,
602
- truncated: false,
603
- };
604
- fetchCache.set(cacheKey, output);
605
-
606
- unlinkAbort();
607
- return success(`Fetched ${input.url} (${response.status})`, wrapWebFetchOutput(output));
608
- } catch (err) {
609
- clearTimeout(timeoutId);
610
- unlinkAbort();
611
- if (
612
- err instanceof Error &&
613
- (err.name === "AbortError" || (err instanceof DOMException && err.name === "AbortError"))
614
- ) {
615
- if (ctx.abortSignal?.aborted) {
616
- return fail("Request cancelled");
617
- }
618
- return fail(`Request timed out after ${timeoutMs}ms`);
619
- }
620
- return fail(getErrorMessage(err));
621
- }
622
- }
623
-
624
- // ============================================================================
625
- // Browse
626
- // ============================================================================
627
-
628
- /** Default timeout for browsing in milliseconds */
629
- const BROWSE_TIMEOUT_MS = 30_000;
630
-
631
- /** Maximum content length returned by browse (characters) — increased from 10K to 50K */
632
- const BROWSE_MAX_CONTENT_LENGTH = 50_000;
633
-
634
- export interface BrowseInput {
635
- /** URL to browse */
636
- url: string;
637
- /** Timeout in milliseconds (default: 30000) */
638
- timeoutMs?: number;
639
- }
640
-
641
- export interface BrowseOutput {
642
- url: string;
643
- title: string;
644
- content: string;
645
- fromCache: boolean;
646
- fetchedAt: string;
647
- finalUrl: string;
648
- contentBytes: number;
649
- truncated: boolean;
650
- }
651
-
652
- function wrapBrowseOutput(output: BrowseOutput): BrowseOutput {
653
- return {
654
- ...output,
655
- content: ensureWrappedExternalContent(output.content, "browse"),
656
- };
657
- }
658
-
659
- function markBrowseOutputFromCache(cached: CachedBrowseOutput): BrowseOutput {
660
- return {
661
- ...cached,
662
- fromCache: true,
663
- };
664
- }
665
-
666
- /**
667
- * Browses a URL by fetching its HTML content and extracting Markdown.
668
- * Uses Readability.js + Turndown for article extraction with three-tier fallback.
669
- * Supports GitHub URL normalization, retry on transient failures, LRU+TTL caching,
670
- * and nonce-based external content wrapping.
671
- */
672
- export async function executeBrowse(input: BrowseInput, ctx: ToolContext): Promise<ToolResult> {
673
- // Validate URL
674
- if (!input.url) {
675
- return fail("URL is required for browse");
676
- }
677
-
678
- // Normalize GitHub blob URLs to raw content URLs
679
- const url = normalizeGitHubUrl(input.url);
680
-
681
- // Check cache (using normalized URL as key)
682
- const cached = browseCache.get(url);
683
- if (cached) {
684
- return success(
685
- `Browsed ${input.url} (cached)`,
686
- wrapBrowseOutput(markBrowseOutputFromCache(cached)),
687
- );
688
- }
689
-
690
- const urlError = validateUrlStructure(url);
691
- if (urlError) {
692
- return fail(urlError);
693
- }
694
-
695
- const timeoutMs = input.timeoutMs ?? BROWSE_TIMEOUT_MS;
696
- const { controller, timeoutId } = createTimeoutController(timeoutMs);
697
-
698
- // Forward context abort signal to our controller
699
- const unlinkAbort = linkAbortSignal(ctx.abortSignal, controller);
700
-
701
- try {
702
- const fetchOptions: RequestInit = {
703
- headers: {
704
- "User-Agent": getUserAgent(),
705
- Accept: "text/html, application/xhtml+xml, */*",
706
- },
707
- signal: controller.signal,
708
- redirect: "manual",
709
- };
710
-
711
- // Use DNS-pinned SSRF fetch wrapper with transient retry support.
712
- const initialResponse = await fetchWithSsrf(url, fetchOptions, {
713
- maxAttempts: 2,
714
- });
715
-
716
- // Follow redirects manually and keep each hop on the same DNS-pinned path.
717
- const response = await followRedirects(initialResponse, fetchOptions, {
718
- baseUrl: url,
719
- fetchFn: (redirectUrl, redirectInit) =>
720
- fetchWithSsrf(redirectUrl, redirectInit, { maxAttempts: 2 }),
721
- validateRedirectUrl: validateUrlStructure,
722
- });
723
-
724
- clearTimeout(timeoutId);
725
-
726
- if (!response.ok) {
727
- await discardResponseBody(response);
728
- unlinkAbort();
729
- return fail(`HTTP error fetching ${input.url}: ${response.status} ${response.statusText}`);
730
- }
731
-
732
- // Truncate large pages gracefully — content gets reduced to 50K chars by
733
- // Readability+Turndown anyway, so the first 2MB of HTML is plenty.
734
- const {
735
- text: html,
736
- truncated: responseTruncated,
737
- contentBytes,
738
- } = await readResponseWithLimit(response, BROWSE_MAX_SIZE_BYTES, {
739
- truncate: true,
740
- });
741
-
742
- // Extract content using Content-Type-aware extraction pipeline.
743
- const { title, content: extractedContent } = await extractFromResponse(
744
- html,
745
- url,
746
- response.headers.get("Content-Type"),
747
- );
748
-
749
- // Truncate to 50K character limit
750
- // extractContent() internally truncates to MAX_CONTENT_LENGTH (50K),
751
- // so content that was originally longer arrives here at exactly 50K.
752
- // Use >= to detect content that hit the internal limit.
753
- const contentLimitReached = extractedContent.length >= BROWSE_MAX_CONTENT_LENGTH;
754
- const truncated = contentLimitReached || responseTruncated;
755
- const truncatedContent = contentLimitReached
756
- ? extractedContent.slice(0, BROWSE_MAX_CONTENT_LENGTH) + "\n\n[Content truncated]"
757
- : responseTruncated
758
- ? extractedContent + "\n\n[Content truncated]"
759
- : extractedContent;
760
-
761
- const output: BrowseOutput = {
762
- url: input.url,
763
- title,
764
- content: truncatedContent,
765
- fromCache: false,
766
- fetchedAt: new Date().toISOString(),
767
- finalUrl: response.url || url,
768
- contentBytes,
769
- truncated,
770
- };
771
- browseCache.set(url, output);
772
-
773
- unlinkAbort();
774
- return success(`Browsed ${input.url}`, wrapBrowseOutput(output));
775
- } catch (err) {
776
- clearTimeout(timeoutId);
777
- unlinkAbort();
778
- if (err instanceof Error && err.name === "AbortError") {
779
- if (ctx.abortSignal?.aborted) {
780
- return fail("Browse cancelled");
781
- }
782
- return fail(`Browse timed out after ${timeoutMs}ms: ${input.url}`);
783
- }
784
- return fail(`Browse failed: ${getErrorMessage(err)}`);
785
- }
786
- }