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