@aria-cli/tools 1.0.12 → 1.0.14

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 (233) hide show
  1. package/dist/index.js +378 -70
  2. package/dist/network-runtime/index.js +8 -12
  3. package/dist-cjs/index.js +400 -435
  4. package/dist-cjs/network-runtime/index.js +8 -172
  5. package/package.json +8 -6
  6. package/dist/.tsbuildinfo +0 -1
  7. package/dist/ask-user-interaction.js +0 -22
  8. package/dist/cache/web-cache.js +0 -66
  9. package/dist/definitions/arion.js +0 -104
  10. package/dist/definitions/browser/browser.js +0 -418
  11. package/dist/definitions/browser/index.js +0 -4
  12. package/dist/definitions/browser/pw-downloads.js +0 -114
  13. package/dist/definitions/browser/pw-interactions.js +0 -199
  14. package/dist/definitions/browser/pw-responses.js +0 -76
  15. package/dist/definitions/browser/pw-session.js +0 -310
  16. package/dist/definitions/browser/pw-shared.js +0 -66
  17. package/dist/definitions/browser/pw-snapshot.js +0 -301
  18. package/dist/definitions/browser/pw-state.js +0 -62
  19. package/dist/definitions/browser/types.js +0 -4
  20. package/dist/definitions/code-intelligence.js +0 -470
  21. package/dist/definitions/core.js +0 -109
  22. package/dist/definitions/delegation.js +0 -512
  23. package/dist/definitions/deploy.js +0 -65
  24. package/dist/definitions/filesystem.js +0 -196
  25. package/dist/definitions/frg.js +0 -63
  26. package/dist/definitions/index.js +0 -20
  27. package/dist/definitions/memory.js +0 -123
  28. package/dist/definitions/messaging.js +0 -625
  29. package/dist/definitions/meta.js +0 -349
  30. package/dist/definitions/network.js +0 -159
  31. package/dist/definitions/outlook.js +0 -277
  32. package/dist/definitions/patch/apply-patch.js +0 -184
  33. package/dist/definitions/patch/fuzzy-match.js +0 -166
  34. package/dist/definitions/patch/index.js +0 -1
  35. package/dist/definitions/patch/patch-parser.js +0 -207
  36. package/dist/definitions/patch/sandbox-paths.js +0 -105
  37. package/dist/definitions/process/index.js +0 -4
  38. package/dist/definitions/process/process-registry.js +0 -213
  39. package/dist/definitions/process/process.js +0 -386
  40. package/dist/definitions/process/pty-keys.js +0 -254
  41. package/dist/definitions/process/session-slug.js +0 -142
  42. package/dist/definitions/quip.js +0 -195
  43. package/dist/definitions/search.js +0 -60
  44. package/dist/definitions/session-history.js +0 -69
  45. package/dist/definitions/shell.js +0 -181
  46. package/dist/definitions/slack.js +0 -180
  47. package/dist/definitions/web.js +0 -109
  48. package/dist/executors/apply-patch.js +0 -901
  49. package/dist/executors/arion.js +0 -119
  50. package/dist/executors/code-intelligence.js +0 -882
  51. package/dist/executors/deploy.js +0 -848
  52. package/dist/executors/filesystem.js +0 -1122
  53. package/dist/executors/frg-freshness.js +0 -576
  54. package/dist/executors/frg.js +0 -298
  55. package/dist/executors/index.js +0 -46
  56. package/dist/executors/learning-meta.js +0 -1146
  57. package/dist/executors/lsp-client.js +0 -296
  58. package/dist/executors/memory.js +0 -750
  59. package/dist/executors/meta.js +0 -220
  60. package/dist/executors/process-registry.js +0 -465
  61. package/dist/executors/pty-session-store.js +0 -30
  62. package/dist/executors/pty.js +0 -271
  63. package/dist/executors/restart.js +0 -119
  64. package/dist/executors/search-freshness.js +0 -195
  65. package/dist/executors/search-types.js +0 -52
  66. package/dist/executors/search.js +0 -66
  67. package/dist/executors/self-diagnose.js +0 -398
  68. package/dist/executors/session-history.js +0 -283
  69. package/dist/executors/shell-safety.js +0 -473
  70. package/dist/executors/shell.js +0 -954
  71. package/dist/executors/utils.js +0 -33
  72. package/dist/executors/web.js +0 -542
  73. package/dist/extraction/content-extraction.js +0 -235
  74. package/dist/extraction/index.js +0 -4
  75. package/dist/headless-control-contract.js +0 -967
  76. package/dist/local-control-http-auth.js +0 -2
  77. package/dist/mcp/client.js +0 -181
  78. package/dist/mcp/connection.js +0 -480
  79. package/dist/mcp/index.js +0 -10
  80. package/dist/mcp/jsonrpc.js +0 -144
  81. package/dist/mcp/types.js +0 -7
  82. package/dist/network-control-adapter.js +0 -72
  83. package/dist/network-runtime/address-types.js +0 -165
  84. package/dist/network-runtime/db-owner-fencing.js +0 -69
  85. package/dist/network-runtime/delivery-receipts.js +0 -267
  86. package/dist/network-runtime/direct-endpoint-authority.js +0 -25
  87. package/dist/network-runtime/local-control-contract.js +0 -627
  88. package/dist/network-runtime/node-store-contract.js +0 -34
  89. package/dist/network-runtime/pair-route-contract.js +0 -77
  90. package/dist/network-runtime/peer-capabilities.js +0 -28
  91. package/dist/network-runtime/peer-principal-ref.js +0 -12
  92. package/dist/network-runtime/peer-state-machine.js +0 -121
  93. package/dist/network-runtime/protocol-schemas.js +0 -205
  94. package/dist/network-runtime/runtime-bootstrap-contract.js +0 -60
  95. package/dist/outlook/desktop-session.js +0 -279
  96. package/dist/policy.js +0 -149
  97. package/dist/providers/brave.js +0 -62
  98. package/dist/providers/duckduckgo.js +0 -176
  99. package/dist/providers/exa.js +0 -63
  100. package/dist/providers/firecrawl.js +0 -55
  101. package/dist/providers/index.js +0 -7
  102. package/dist/providers/jina.js +0 -49
  103. package/dist/providers/router.js +0 -96
  104. package/dist/providers/search-provider.js +0 -32
  105. package/dist/providers/tavily.js +0 -54
  106. package/dist/quip/desktop-session.js +0 -317
  107. package/dist/registry/index.js +0 -1
  108. package/dist/registry/registry.js +0 -756
  109. package/dist/runtime-socket-local-control-client.js +0 -330
  110. package/dist/security/dns-normalization.js +0 -19
  111. package/dist/security/dns-pinning.js +0 -123
  112. package/dist/security/external-content.js +0 -91
  113. package/dist/security/ssrf.js +0 -181
  114. package/dist/slack/desktop-session.js +0 -324
  115. package/dist/tool-factory.js +0 -47
  116. package/dist/types.js +0 -7
  117. package/dist/utils/retry.js +0 -132
  118. package/dist/utils/safe-parse-json.js +0 -160
  119. package/dist/utils/url.js +0 -19
  120. package/dist-cjs/.tsbuildinfo +0 -1
  121. package/dist-cjs/ask-user-interaction.js +0 -27
  122. package/dist-cjs/cache/web-cache.js +0 -70
  123. package/dist-cjs/definitions/arion.js +0 -107
  124. package/dist-cjs/definitions/browser/browser.js +0 -421
  125. package/dist-cjs/definitions/browser/index.js +0 -8
  126. package/dist-cjs/definitions/browser/pw-downloads.js +0 -117
  127. package/dist-cjs/definitions/browser/pw-interactions.js +0 -213
  128. package/dist-cjs/definitions/browser/pw-responses.js +0 -84
  129. package/dist-cjs/definitions/browser/pw-session.js +0 -326
  130. package/dist-cjs/definitions/browser/pw-shared.js +0 -72
  131. package/dist-cjs/definitions/browser/pw-snapshot.js +0 -307
  132. package/dist-cjs/definitions/browser/pw-state.js +0 -70
  133. package/dist-cjs/definitions/browser/types.js +0 -5
  134. package/dist-cjs/definitions/code-intelligence.js +0 -473
  135. package/dist-cjs/definitions/core.js +0 -133
  136. package/dist-cjs/definitions/delegation.js +0 -515
  137. package/dist-cjs/definitions/deploy.js +0 -68
  138. package/dist-cjs/definitions/filesystem.js +0 -199
  139. package/dist-cjs/definitions/frg.js +0 -66
  140. package/dist-cjs/definitions/index.js +0 -43
  141. package/dist-cjs/definitions/memory.js +0 -126
  142. package/dist-cjs/definitions/messaging.js +0 -631
  143. package/dist-cjs/definitions/meta.js +0 -352
  144. package/dist-cjs/definitions/network.js +0 -162
  145. package/dist-cjs/definitions/outlook.js +0 -280
  146. package/dist-cjs/definitions/patch/apply-patch.js +0 -191
  147. package/dist-cjs/definitions/patch/fuzzy-match.js +0 -172
  148. package/dist-cjs/definitions/patch/index.js +0 -5
  149. package/dist-cjs/definitions/patch/patch-parser.js +0 -215
  150. package/dist-cjs/definitions/patch/sandbox-paths.js +0 -113
  151. package/dist-cjs/definitions/process/index.js +0 -8
  152. package/dist-cjs/definitions/process/process-registry.js +0 -231
  153. package/dist-cjs/definitions/process/process.js +0 -389
  154. package/dist-cjs/definitions/process/pty-keys.js +0 -259
  155. package/dist-cjs/definitions/process/session-slug.js +0 -145
  156. package/dist-cjs/definitions/quip.js +0 -198
  157. package/dist-cjs/definitions/search.js +0 -63
  158. package/dist-cjs/definitions/session-history.js +0 -72
  159. package/dist-cjs/definitions/shell.js +0 -184
  160. package/dist-cjs/definitions/slack.js +0 -183
  161. package/dist-cjs/definitions/web.js +0 -112
  162. package/dist-cjs/executors/apply-patch.js +0 -938
  163. package/dist-cjs/executors/arion.js +0 -125
  164. package/dist-cjs/executors/code-intelligence.js +0 -925
  165. package/dist-cjs/executors/deploy.js +0 -869
  166. package/dist-cjs/executors/filesystem.js +0 -1167
  167. package/dist-cjs/executors/frg-freshness.js +0 -627
  168. package/dist-cjs/executors/frg.js +0 -334
  169. package/dist-cjs/executors/index.js +0 -143
  170. package/dist-cjs/executors/learning-meta.js +0 -1165
  171. package/dist-cjs/executors/lsp-client.js +0 -310
  172. package/dist-cjs/executors/memory.js +0 -796
  173. package/dist-cjs/executors/meta.js +0 -226
  174. package/dist-cjs/executors/process-registry.js +0 -469
  175. package/dist-cjs/executors/pty-session-store.js +0 -34
  176. package/dist-cjs/executors/pty.js +0 -312
  177. package/dist-cjs/executors/restart.js +0 -155
  178. package/dist-cjs/executors/search-freshness.js +0 -234
  179. package/dist-cjs/executors/search-types.js +0 -56
  180. package/dist-cjs/executors/search.js +0 -102
  181. package/dist-cjs/executors/self-diagnose.js +0 -434
  182. package/dist-cjs/executors/session-history.js +0 -320
  183. package/dist-cjs/executors/shell-safety.js +0 -478
  184. package/dist-cjs/executors/shell.js +0 -1001
  185. package/dist-cjs/executors/utils.js +0 -73
  186. package/dist-cjs/executors/web.js +0 -547
  187. package/dist-cjs/extraction/content-extraction.js +0 -243
  188. package/dist-cjs/extraction/index.js +0 -8
  189. package/dist-cjs/headless-control-contract.js +0 -972
  190. package/dist-cjs/local-control-http-auth.js +0 -5
  191. package/dist-cjs/mcp/client.js +0 -185
  192. package/dist-cjs/mcp/connection.js +0 -484
  193. package/dist-cjs/mcp/index.js +0 -30
  194. package/dist-cjs/mcp/jsonrpc.js +0 -148
  195. package/dist-cjs/mcp/types.js +0 -8
  196. package/dist-cjs/network-control-adapter.js +0 -77
  197. package/dist-cjs/network-runtime/address-types.js +0 -168
  198. package/dist-cjs/network-runtime/db-owner-fencing.js +0 -76
  199. package/dist-cjs/network-runtime/delivery-receipts.js +0 -276
  200. package/dist-cjs/network-runtime/direct-endpoint-authority.js +0 -29
  201. package/dist-cjs/network-runtime/local-control-contract.js +0 -633
  202. package/dist-cjs/network-runtime/node-store-contract.js +0 -38
  203. package/dist-cjs/network-runtime/pair-route-contract.js +0 -80
  204. package/dist-cjs/network-runtime/peer-capabilities.js +0 -37
  205. package/dist-cjs/network-runtime/peer-principal-ref.js +0 -15
  206. package/dist-cjs/network-runtime/peer-state-machine.js +0 -129
  207. package/dist-cjs/network-runtime/protocol-schemas.js +0 -212
  208. package/dist-cjs/network-runtime/runtime-bootstrap-contract.js +0 -63
  209. package/dist-cjs/outlook/desktop-session.js +0 -318
  210. package/dist-cjs/policy.js +0 -155
  211. package/dist-cjs/providers/brave.js +0 -66
  212. package/dist-cjs/providers/duckduckgo.js +0 -180
  213. package/dist-cjs/providers/exa.js +0 -67
  214. package/dist-cjs/providers/firecrawl.js +0 -59
  215. package/dist-cjs/providers/index.js +0 -17
  216. package/dist-cjs/providers/jina.js +0 -53
  217. package/dist-cjs/providers/router.js +0 -100
  218. package/dist-cjs/providers/search-provider.js +0 -36
  219. package/dist-cjs/providers/tavily.js +0 -58
  220. package/dist-cjs/quip/desktop-session.js +0 -353
  221. package/dist-cjs/registry/index.js +0 -6
  222. package/dist-cjs/registry/registry.js +0 -761
  223. package/dist-cjs/runtime-socket-local-control-client.js +0 -367
  224. package/dist-cjs/security/dns-normalization.js +0 -22
  225. package/dist-cjs/security/dns-pinning.js +0 -160
  226. package/dist-cjs/security/external-content.js +0 -95
  227. package/dist-cjs/security/ssrf.js +0 -221
  228. package/dist-cjs/slack/desktop-session.js +0 -366
  229. package/dist-cjs/tool-factory.js +0 -50
  230. package/dist-cjs/types.js +0 -8
  231. package/dist-cjs/utils/retry.js +0 -169
  232. package/dist-cjs/utils/safe-parse-json.js +0 -164
  233. package/dist-cjs/utils/url.js +0 -23
@@ -1,33 +0,0 @@
1
- /**
2
- * @aria/tools - Shared executor utilities
3
- *
4
- * Common helper functions used across all executor modules.
5
- */
6
- import * as nodePath from "node:path";
7
- export { getErrorMessage } from "@aria-cli/types";
8
- /**
9
- * Creates a successful result.
10
- */
11
- export function success(message, data) {
12
- return { success: true, message, data };
13
- }
14
- /**
15
- * Creates a failure result.
16
- */
17
- export function fail(message, data) {
18
- return { success: false, message, data };
19
- }
20
- /**
21
- * Checks if a resolved path is within the allowed base directory.
22
- * Prevents path traversal attacks.
23
- */
24
- export function isPathWithinBase(resolvedPath, baseDir) {
25
- const normalizedPath = nodePath.normalize(resolvedPath);
26
- const normalizedBase = nodePath.normalize(baseDir);
27
- // normalize() strips trailing separators except for root "/".
28
- // Guard against root-as-base which would allow every absolute path.
29
- const baseWithSep = normalizedBase.endsWith(nodePath.sep)
30
- ? normalizedBase
31
- : normalizedBase + nodePath.sep;
32
- return normalizedPath === normalizedBase || normalizedPath.startsWith(baseWithSep);
33
- }
@@ -1,542 +0,0 @@
1
- /**
2
- * @aria/tools - Web tool executors
3
- *
4
- * Implementation of web operations for ARIA tool system.
5
- */
6
- import { createHash } from "node:crypto";
7
- import { log } from "@aria-cli/types";
8
- import { z } from "zod";
9
- import { success, fail, getErrorMessage } from "./utils.js";
10
- import { safeParseJson } from "../utils/safe-parse-json.js";
11
- import { validateUrlStructure, followRedirects, discardResponseBody } from "../security/ssrf.js";
12
- import { SearchProviderRouter } from "../providers/router.js";
13
- import { TavilySearchProvider, DuckDuckGoSearchProvider, BraveSearchProvider, JinaSearchProvider, ExaSearchProvider, FirecrawlSearchProvider, } from "../providers/index.js";
14
- import { resolveSearchProviderEnv } from "../providers/search-provider.js";
15
- import { searchCache, fetchCache, browseCache, } from "../cache/web-cache.js";
16
- import { fetchWithSsrf } from "../utils/retry.js";
17
- import { wrapExternalContent } from "../security/external-content.js";
18
- import { extractFromResponse } from "../extraction/content-extraction.js";
19
- import { normalizeGitHubUrl, getUserAgent } from "../utils/url.js";
20
- function buildSearchEnv(ctx) {
21
- const env = ctx.env ?? {};
22
- return {
23
- ARIA_SEARCH_PROVIDER: env.ARIA_SEARCH_PROVIDER ?? process.env.ARIA_SEARCH_PROVIDER,
24
- BRAVE_API_KEY: env.BRAVE_API_KEY ?? process.env.BRAVE_API_KEY,
25
- FIRECRAWL_API_KEY: env.FIRECRAWL_API_KEY ?? process.env.FIRECRAWL_API_KEY,
26
- EXA_API_KEY: env.EXA_API_KEY ?? process.env.EXA_API_KEY,
27
- TAVILY_API_KEY: env.TAVILY_API_KEY ?? process.env.TAVILY_API_KEY,
28
- JINA_API_KEY: env.JINA_API_KEY ?? process.env.JINA_API_KEY,
29
- };
30
- }
31
- function createSearchRouter(env) {
32
- return new SearchProviderRouter([
33
- new BraveSearchProvider(env),
34
- new TavilySearchProvider(env),
35
- new JinaSearchProvider(env),
36
- new ExaSearchProvider(env),
37
- new FirecrawlSearchProvider(env),
38
- new DuckDuckGoSearchProvider(),
39
- ], env);
40
- }
41
- /** Default timeout for fetch requests in milliseconds */
42
- const DEFAULT_TIMEOUT_MS = 30_000;
43
- /** Default maximum response size in bytes for fetch (1MB) */
44
- const FETCH_MAX_SIZE_BYTES = 1 * 1024 * 1024;
45
- /** Default maximum response size in bytes for browse (2MB) */
46
- const BROWSE_MAX_SIZE_BYTES = 2 * 1024 * 1024;
47
- const JsonFetchContentSchema = z.union([
48
- z.record(z.string(), z.unknown()),
49
- z.array(z.unknown()),
50
- z.string(),
51
- z.number(),
52
- z.boolean(),
53
- z.null(),
54
- ]);
55
- /**
56
- * Creates an AbortController with a timeout.
57
- */
58
- function createTimeoutController(timeoutMs) {
59
- const controller = new AbortController();
60
- const timeoutId = setTimeout(() => {
61
- if (!controller.signal.aborted) {
62
- controller.abort();
63
- }
64
- }, timeoutMs);
65
- return { controller, timeoutId };
66
- }
67
- /**
68
- * Links a parent AbortSignal to a child AbortController, forwarding abort.
69
- * Returns a cleanup function that MUST be called when the operation completes
70
- * to prevent the listener from firing after the try/catch scope has exited.
71
- */
72
- function linkAbortSignal(parentSignal, childController) {
73
- if (!parentSignal)
74
- return () => { };
75
- const onAbort = () => {
76
- if (!childController.signal.aborted) {
77
- childController.abort();
78
- }
79
- };
80
- parentSignal.addEventListener("abort", onAbort, { once: true });
81
- return () => {
82
- parentSignal.removeEventListener("abort", onAbort);
83
- };
84
- }
85
- /**
86
- * Reads a response body with a streaming byte limit.
87
- * Unlike checking Content-Length alone, this protects against chunked/streaming
88
- * responses that lack a Content-Length header.
89
- *
90
- * When `truncate` is false (default), throws if the limit is exceeded.
91
- * When `truncate` is true, returns whatever was read up to the limit.
92
- */
93
- async function readResponseWithLimit(response, maxBytes, options) {
94
- const body = response.body;
95
- if (!body) {
96
- return { text: "", truncated: false, contentBytes: 0 };
97
- }
98
- const truncateMode = options?.truncate ?? false;
99
- const reader = body.getReader();
100
- const decoder = new TextDecoder();
101
- const chunks = [];
102
- let totalBytes = 0;
103
- try {
104
- for (;;) {
105
- const { done, value } = await reader.read();
106
- if (done)
107
- break;
108
- const chunkBytes = value.byteLength;
109
- if (totalBytes + chunkBytes > maxBytes) {
110
- if (truncateMode) {
111
- // Keep what we have so far (exclude the chunk that exceeded the limit)
112
- reader.cancel();
113
- chunks.push(decoder.decode());
114
- return {
115
- text: chunks.join(""),
116
- truncated: true,
117
- contentBytes: totalBytes,
118
- };
119
- }
120
- reader.cancel();
121
- throw new Error(`Response body exceeds maximum size of ${maxBytes} bytes`);
122
- }
123
- totalBytes += chunkBytes;
124
- chunks.push(decoder.decode(value, { stream: true }));
125
- }
126
- // Flush the decoder
127
- chunks.push(decoder.decode());
128
- return { text: chunks.join(""), truncated: false, contentBytes: totalBytes };
129
- }
130
- catch (err) {
131
- reader.cancel();
132
- throw err;
133
- }
134
- }
135
- function ensureWrappedExternalContent(content, source) {
136
- return wrapExternalContent(content, source).content;
137
- }
138
- function wrapWebSearchOutput(output) {
139
- return {
140
- query: output.query,
141
- results: output.results.map((result) => ({
142
- ...result,
143
- content: ensureWrappedExternalContent(result.content, "web_search"),
144
- })),
145
- };
146
- }
147
- function hasAdvancedSearchOptions(input) {
148
- return (input.limit !== undefined ||
149
- input.topic !== undefined ||
150
- (input.domains?.length ?? 0) > 0 ||
151
- (input.excludeDomains?.length ?? 0) > 0 ||
152
- input.timeRange !== undefined);
153
- }
154
- function buildSearchCachePayload(query, options) {
155
- return JSON.stringify({ query, ...options });
156
- }
157
- function buildRouterSearchCacheKey(env, query, options) {
158
- const resolved = resolveSearchProviderEnv(env);
159
- const providerOverride = resolved.ARIA_SEARCH_PROVIDER ?? "auto";
160
- const providerAvailability = [
161
- resolved.BRAVE_API_KEY ? "brave=1" : "brave=0",
162
- resolved.TAVILY_API_KEY ? "tavily=1" : "tavily=0",
163
- resolved.JINA_API_KEY ? "jina=1" : "jina=0",
164
- resolved.EXA_API_KEY ? "exa=1" : "exa=0",
165
- resolved.FIRECRAWL_API_KEY ? "firecrawl=1" : "firecrawl=0",
166
- ].join(",");
167
- return `search:router:${providerOverride}:${providerAvailability}:${buildSearchCachePayload(query, options)}`;
168
- }
169
- function buildNativeSearchCacheKey(providerName, query, options) {
170
- return `search:native:${providerName}:${buildSearchCachePayload(query, options)}`;
171
- }
172
- /**
173
- * Searches the web using the SearchProviderRouter with automatic fallback.
174
- * Providers are selected based on available API keys (Brave, Tavily, Jina, Exa,
175
- * Firecrawl, DuckDuckGo). DuckDuckGo is always available as a last resort.
176
- *
177
- * Results are cached via LRU+TTL cache for 15 minutes.
178
- */
179
- export async function executeWebSearch(input, ctx) {
180
- const limit = input.limit ?? 10;
181
- const searchOptions = {
182
- limit,
183
- ...(input.topic ? { topic: input.topic } : {}),
184
- ...(input.domains ? { domains: input.domains } : {}),
185
- ...(input.excludeDomains ? { excludeDomains: input.excludeDomains } : {}),
186
- ...(input.timeRange ? { timeRange: input.timeRange } : {}),
187
- };
188
- const searchEnv = buildSearchEnv(ctx);
189
- const advancedOptionsProvided = hasAdvancedSearchOptions(input);
190
- const nativeSearchAvailable = Boolean(ctx.nativeSearchAdapter && ctx.providerContext?.capabilities?.nativeSearch);
191
- const nativeProviderName = ctx.providerContext?.name ?? "unknown";
192
- const routerCacheKey = buildRouterSearchCacheKey(searchEnv, input.query, searchOptions);
193
- const nativeCacheKey = nativeSearchAvailable && !advancedOptionsProvided
194
- ? buildNativeSearchCacheKey(nativeProviderName, input.query, searchOptions)
195
- : undefined;
196
- if (nativeCacheKey) {
197
- const cachedNative = searchCache.get(nativeCacheKey);
198
- if (cachedNative) {
199
- return success(`Found ${cachedNative.results.length} cached results for "${input.query}"`, wrapWebSearchOutput(cachedNative));
200
- }
201
- }
202
- else {
203
- const cachedRouter = searchCache.get(routerCacheKey);
204
- if (cachedRouter) {
205
- return success(`Found ${cachedRouter.results.length} cached results for "${input.query}"`, wrapWebSearchOutput(cachedRouter));
206
- }
207
- }
208
- if (advancedOptionsProvided && nativeSearchAvailable) {
209
- log.debug("[web_search] advanced options provided; bypassing native adapter");
210
- }
211
- // Native search routing: If provider supports native search and adapter is wired, use it
212
- // Phase 1: Gemini isolated adapter (router-based callback pattern)
213
- if (nativeSearchAvailable && ctx.nativeSearchAdapter && !advancedOptionsProvided) {
214
- try {
215
- const rawResults = await ctx.nativeSearchAdapter(input.query);
216
- if (rawResults.length === 0) {
217
- throw new Error("Native search returned no results");
218
- }
219
- const results = rawResults.map((r) => ({
220
- title: r.title,
221
- url: r.url,
222
- content: r.content,
223
- score: r.score,
224
- }));
225
- const output = { query: input.query, results };
226
- searchCache.set(nativeCacheKey, output);
227
- return success(`Found ${results.length} results for "${input.query}" (native search)`, wrapWebSearchOutput(output));
228
- }
229
- catch (err) {
230
- // Fall through to SearchProviderRouter on native search failure
231
- log.debug(`[web_search] native adapter failed, falling back: ${getErrorMessage(err)}`);
232
- const cachedRouterFallback = searchCache.get(routerCacheKey);
233
- if (cachedRouterFallback) {
234
- return success(`Found ${cachedRouterFallback.results.length} cached results for "${input.query}"`, wrapWebSearchOutput(cachedRouterFallback));
235
- }
236
- }
237
- }
238
- const searchRouter = createSearchRouter(searchEnv);
239
- try {
240
- // Race search against context abort signal for cancellation support
241
- let results;
242
- if (ctx.abortSignal) {
243
- const abortPromise = new Promise((_resolve, reject) => {
244
- if (ctx.abortSignal.aborted) {
245
- reject(new DOMException("The operation was aborted", "AbortError"));
246
- return;
247
- }
248
- ctx.abortSignal.addEventListener("abort", () => reject(new DOMException("The operation was aborted", "AbortError")), { once: true });
249
- });
250
- results = await Promise.race([searchRouter.search(input.query, searchOptions), abortPromise]);
251
- }
252
- else {
253
- results = await searchRouter.search(input.query, searchOptions);
254
- }
255
- // Normalize results to WebSearchResult shape (cache raw; wrap at return-time)
256
- const webResults = results.map((r) => ({
257
- title: r.title,
258
- url: r.url,
259
- content: r.content,
260
- score: r.score,
261
- }));
262
- const output = { query: input.query, results: webResults };
263
- searchCache.set(routerCacheKey, output);
264
- return success(`Found ${webResults.length} results for "${input.query}"`, wrapWebSearchOutput(output));
265
- }
266
- catch (err) {
267
- if (ctx.abortSignal?.aborted) {
268
- return fail("Web search cancelled");
269
- }
270
- return fail(`Web search failed: ${getErrorMessage(err)}`);
271
- }
272
- }
273
- function wrapWebFetchOutput(output) {
274
- if (typeof output.content !== "string") {
275
- return output;
276
- }
277
- return {
278
- ...output,
279
- content: ensureWrappedExternalContent(output.content, "web_fetch"),
280
- };
281
- }
282
- function markFetchOutputFromCache(cached) {
283
- return {
284
- ...cached,
285
- fromCache: true,
286
- };
287
- }
288
- function normalizeRequestHeaders(headers) {
289
- if (!headers) {
290
- return [];
291
- }
292
- return Object.entries(headers)
293
- .map(([name, value]) => [name.trim().toLowerCase(), value.trim()])
294
- .sort(([left], [right]) => left.localeCompare(right));
295
- }
296
- function hashRequestHeaders(headers) {
297
- const normalizedHeaders = normalizeRequestHeaders(headers);
298
- if (normalizedHeaders.length === 0) {
299
- return "none";
300
- }
301
- return createHash("sha256").update(JSON.stringify(normalizedHeaders)).digest("hex");
302
- }
303
- function createFetchCacheKey({ url, format, headers, timeoutMs, maxSizeBytes, }) {
304
- return `fetch:${url}:${format}:headers=${hashRequestHeaders(headers)}:maxSizeBytes=${maxSizeBytes}:timeoutMs=${timeoutMs}`;
305
- }
306
- /**
307
- * Fetches content from a URL with retry resilience, caching, and security wrapping.
308
- * Uses fetchWithRetry for automatic retry on transient failures (429, 5xx, ECONNRESET).
309
- * Results are cached via LRU+TTL cache for 15 minutes.
310
- */
311
- export async function executeWebFetch(input, ctx) {
312
- // Normalize GitHub blob URLs to raw content URLs before fetching
313
- const url = normalizeGitHubUrl(input.url);
314
- // Validate URL structure. DNS/SSRF safety is enforced by fetchWithSsrf
315
- // so validation and fetch share one trust boundary.
316
- const urlError = validateUrlStructure(url);
317
- if (urlError) {
318
- return fail(urlError);
319
- }
320
- const format = input.format ?? "text";
321
- const timeoutMs = input.timeoutMs ?? DEFAULT_TIMEOUT_MS;
322
- const maxSizeBytes = input.maxSizeBytes ?? FETCH_MAX_SIZE_BYTES;
323
- // Check cache first (use normalized URL + request variants to avoid collisions)
324
- const cacheKey = createFetchCacheKey({
325
- url,
326
- format,
327
- headers: input.headers,
328
- timeoutMs,
329
- maxSizeBytes,
330
- });
331
- const cached = fetchCache.get(cacheKey);
332
- if (cached) {
333
- return success(`Fetched ${input.url} (cached)`, wrapWebFetchOutput(markFetchOutputFromCache(cached)));
334
- }
335
- const { controller, timeoutId } = createTimeoutController(timeoutMs);
336
- // Forward context abort signal to our controller
337
- const unlinkAbort = linkAbortSignal(ctx.abortSignal, controller);
338
- try {
339
- const fetchOptions = {
340
- headers: input.headers ?? {},
341
- signal: controller.signal,
342
- redirect: "manual",
343
- };
344
- // Use DNS-pinned SSRF fetch wrapper with transient retry support.
345
- const initialResponse = await fetchWithSsrf(url, fetchOptions, {
346
- maxAttempts: 2,
347
- });
348
- // Follow redirects manually and keep each hop on the same DNS-pinned path.
349
- const response = await followRedirects(initialResponse, fetchOptions, {
350
- baseUrl: url,
351
- fetchFn: (redirectUrl, redirectInit) => fetchWithSsrf(redirectUrl, redirectInit, { maxAttempts: 2 }),
352
- validateRedirectUrl: validateUrlStructure,
353
- });
354
- clearTimeout(timeoutId);
355
- if (!response.ok) {
356
- await discardResponseBody(response);
357
- unlinkAbort();
358
- return fail(`HTTP error: ${response.status} ${response.statusText}`, {
359
- status: response.status,
360
- statusText: response.statusText,
361
- });
362
- }
363
- // Check Content-Length header for size limit
364
- const contentLength = response.headers.get("Content-Length");
365
- if (contentLength) {
366
- const size = parseInt(contentLength, 10);
367
- if (!isNaN(size) && size > maxSizeBytes) {
368
- await discardResponseBody(response);
369
- unlinkAbort();
370
- return fail(`Response too large: ${size} bytes exceeds maximum of ${maxSizeBytes} bytes`);
371
- }
372
- }
373
- const contentType = response.headers.get("Content-Type") ?? undefined;
374
- let content;
375
- // Use streaming reader to enforce size limit even for chunked responses
376
- let text;
377
- let contentBytes = 0;
378
- try {
379
- const result = await readResponseWithLimit(response, maxSizeBytes);
380
- text = result.text;
381
- contentBytes = result.contentBytes;
382
- if (result.truncated) {
383
- unlinkAbort();
384
- return fail(`Response body exceeds maximum size of ${maxSizeBytes} bytes`);
385
- }
386
- }
387
- catch (sizeErr) {
388
- unlinkAbort();
389
- return fail(getErrorMessage(sizeErr));
390
- }
391
- if (format === "json") {
392
- const parsed = safeParseJson(text, JsonFetchContentSchema);
393
- if (!parsed.ok) {
394
- unlinkAbort();
395
- return fail(`Failed to parse JSON response (${parsed.reason})`);
396
- }
397
- content = parsed.data;
398
- }
399
- else {
400
- content = text;
401
- }
402
- const output = {
403
- content,
404
- status: response.status,
405
- contentType,
406
- fromCache: false,
407
- fetchedAt: new Date().toISOString(),
408
- finalUrl: response.url || url,
409
- contentBytes,
410
- truncated: false,
411
- };
412
- fetchCache.set(cacheKey, output);
413
- unlinkAbort();
414
- return success(`Fetched ${input.url} (${response.status})`, wrapWebFetchOutput(output));
415
- }
416
- catch (err) {
417
- clearTimeout(timeoutId);
418
- unlinkAbort();
419
- if (err instanceof Error &&
420
- (err.name === "AbortError" || (err instanceof DOMException && err.name === "AbortError"))) {
421
- if (ctx.abortSignal?.aborted) {
422
- return fail("Request cancelled");
423
- }
424
- return fail(`Request timed out after ${timeoutMs}ms`);
425
- }
426
- return fail(getErrorMessage(err));
427
- }
428
- }
429
- // ============================================================================
430
- // Browse
431
- // ============================================================================
432
- /** Default timeout for browsing in milliseconds */
433
- const BROWSE_TIMEOUT_MS = 30_000;
434
- /** Maximum content length returned by browse (characters) — increased from 10K to 50K */
435
- const BROWSE_MAX_CONTENT_LENGTH = 50_000;
436
- function wrapBrowseOutput(output) {
437
- return {
438
- ...output,
439
- content: ensureWrappedExternalContent(output.content, "browse"),
440
- };
441
- }
442
- function markBrowseOutputFromCache(cached) {
443
- return {
444
- ...cached,
445
- fromCache: true,
446
- };
447
- }
448
- /**
449
- * Browses a URL by fetching its HTML content and extracting Markdown.
450
- * Uses Readability.js + Turndown for article extraction with three-tier fallback.
451
- * Supports GitHub URL normalization, retry on transient failures, LRU+TTL caching,
452
- * and nonce-based external content wrapping.
453
- */
454
- export async function executeBrowse(input, ctx) {
455
- // Validate URL
456
- if (!input.url) {
457
- return fail("URL is required for browse");
458
- }
459
- // Normalize GitHub blob URLs to raw content URLs
460
- const url = normalizeGitHubUrl(input.url);
461
- // Check cache (using normalized URL as key)
462
- const cached = browseCache.get(url);
463
- if (cached) {
464
- return success(`Browsed ${input.url} (cached)`, wrapBrowseOutput(markBrowseOutputFromCache(cached)));
465
- }
466
- const urlError = validateUrlStructure(url);
467
- if (urlError) {
468
- return fail(urlError);
469
- }
470
- const timeoutMs = input.timeoutMs ?? BROWSE_TIMEOUT_MS;
471
- const { controller, timeoutId } = createTimeoutController(timeoutMs);
472
- // Forward context abort signal to our controller
473
- const unlinkAbort = linkAbortSignal(ctx.abortSignal, controller);
474
- try {
475
- const fetchOptions = {
476
- headers: {
477
- "User-Agent": getUserAgent(),
478
- Accept: "text/html, application/xhtml+xml, */*",
479
- },
480
- signal: controller.signal,
481
- redirect: "manual",
482
- };
483
- // Use DNS-pinned SSRF fetch wrapper with transient retry support.
484
- const initialResponse = await fetchWithSsrf(url, fetchOptions, {
485
- maxAttempts: 2,
486
- });
487
- // Follow redirects manually and keep each hop on the same DNS-pinned path.
488
- const response = await followRedirects(initialResponse, fetchOptions, {
489
- baseUrl: url,
490
- fetchFn: (redirectUrl, redirectInit) => fetchWithSsrf(redirectUrl, redirectInit, { maxAttempts: 2 }),
491
- validateRedirectUrl: validateUrlStructure,
492
- });
493
- clearTimeout(timeoutId);
494
- if (!response.ok) {
495
- await discardResponseBody(response);
496
- unlinkAbort();
497
- return fail(`HTTP error fetching ${input.url}: ${response.status} ${response.statusText}`);
498
- }
499
- // Truncate large pages gracefully — content gets reduced to 50K chars by
500
- // Readability+Turndown anyway, so the first 2MB of HTML is plenty.
501
- const { text: html, truncated: responseTruncated, contentBytes, } = await readResponseWithLimit(response, BROWSE_MAX_SIZE_BYTES, {
502
- truncate: true,
503
- });
504
- // Extract content using Content-Type-aware extraction pipeline.
505
- const { title, content: extractedContent } = await extractFromResponse(html, url, response.headers.get("Content-Type"));
506
- // Truncate to 50K character limit
507
- // extractContent() internally truncates to MAX_CONTENT_LENGTH (50K),
508
- // so content that was originally longer arrives here at exactly 50K.
509
- // Use >= to detect content that hit the internal limit.
510
- const contentLimitReached = extractedContent.length >= BROWSE_MAX_CONTENT_LENGTH;
511
- const truncated = contentLimitReached || responseTruncated;
512
- const truncatedContent = contentLimitReached
513
- ? extractedContent.slice(0, BROWSE_MAX_CONTENT_LENGTH) + "\n\n[Content truncated]"
514
- : responseTruncated
515
- ? extractedContent + "\n\n[Content truncated]"
516
- : extractedContent;
517
- const output = {
518
- url: input.url,
519
- title,
520
- content: truncatedContent,
521
- fromCache: false,
522
- fetchedAt: new Date().toISOString(),
523
- finalUrl: response.url || url,
524
- contentBytes,
525
- truncated,
526
- };
527
- browseCache.set(url, output);
528
- unlinkAbort();
529
- return success(`Browsed ${input.url}`, wrapBrowseOutput(output));
530
- }
531
- catch (err) {
532
- clearTimeout(timeoutId);
533
- unlinkAbort();
534
- if (err instanceof Error && err.name === "AbortError") {
535
- if (ctx.abortSignal?.aborted) {
536
- return fail("Browse cancelled");
537
- }
538
- return fail(`Browse timed out after ${timeoutMs}ms: ${input.url}`);
539
- }
540
- return fail(`Browse failed: ${getErrorMessage(err)}`);
541
- }
542
- }