@nervekit/tools 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (267) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +5 -0
  3. package/dist/catalog/core/filesystem.tools.d.ts +140 -0
  4. package/dist/catalog/core/filesystem.tools.d.ts.map +1 -0
  5. package/dist/catalog/core/filesystem.tools.js +221 -0
  6. package/dist/catalog/core/filesystem.tools.js.map +1 -0
  7. package/dist/catalog/core/interaction.tools.d.ts +37 -0
  8. package/dist/catalog/core/interaction.tools.d.ts.map +1 -0
  9. package/dist/catalog/core/interaction.tools.js +55 -0
  10. package/dist/catalog/core/interaction.tools.js.map +1 -0
  11. package/dist/catalog/core/python.tools.d.ts +17 -0
  12. package/dist/catalog/core/python.tools.d.ts.map +1 -0
  13. package/dist/catalog/core/python.tools.js +32 -0
  14. package/dist/catalog/core/python.tools.js.map +1 -0
  15. package/dist/catalog/core/shell.tools.d.ts +14 -0
  16. package/dist/catalog/core/shell.tools.d.ts.map +1 -0
  17. package/dist/catalog/core/shell.tools.js +22 -0
  18. package/dist/catalog/core/shell.tools.js.map +1 -0
  19. package/dist/catalog/core/web.tools.d.ts +23 -0
  20. package/dist/catalog/core/web.tools.d.ts.map +1 -0
  21. package/dist/catalog/core/web.tools.js +34 -0
  22. package/dist/catalog/core/web.tools.js.map +1 -0
  23. package/dist/catalog/descriptors.d.ts +4 -0
  24. package/dist/catalog/descriptors.d.ts.map +1 -0
  25. package/dist/catalog/descriptors.js +17 -0
  26. package/dist/catalog/descriptors.js.map +1 -0
  27. package/dist/catalog/index.d.ts +18 -0
  28. package/dist/catalog/index.d.ts.map +1 -0
  29. package/dist/catalog/index.js +44 -0
  30. package/dist/catalog/index.js.map +1 -0
  31. package/dist/catalog/orchestration/explore.tools.d.ts +20 -0
  32. package/dist/catalog/orchestration/explore.tools.d.ts.map +1 -0
  33. package/dist/catalog/orchestration/explore.tools.js +42 -0
  34. package/dist/catalog/orchestration/explore.tools.js.map +1 -0
  35. package/dist/catalog/orchestration/plan-mode.tools.d.ts +32 -0
  36. package/dist/catalog/orchestration/plan-mode.tools.d.ts.map +1 -0
  37. package/dist/catalog/orchestration/plan-mode.tools.js +43 -0
  38. package/dist/catalog/orchestration/plan-mode.tools.js.map +1 -0
  39. package/dist/catalog/orchestration/process.tools.d.ts +68 -0
  40. package/dist/catalog/orchestration/process.tools.d.ts.map +1 -0
  41. package/dist/catalog/orchestration/process.tools.js +96 -0
  42. package/dist/catalog/orchestration/process.tools.js.map +1 -0
  43. package/dist/catalog/orchestration/subagent.tools.d.ts +16 -0
  44. package/dist/catalog/orchestration/subagent.tools.d.ts.map +1 -0
  45. package/dist/catalog/orchestration/subagent.tools.js +25 -0
  46. package/dist/catalog/orchestration/subagent.tools.js.map +1 -0
  47. package/dist/catalog/orchestration/task.tools.d.ts +107 -0
  48. package/dist/catalog/orchestration/task.tools.d.ts.map +1 -0
  49. package/dist/catalog/orchestration/task.tools.js +170 -0
  50. package/dist/catalog/orchestration/task.tools.js.map +1 -0
  51. package/dist/catalog/risk.d.ts +3 -0
  52. package/dist/catalog/risk.d.ts.map +1 -0
  53. package/dist/catalog/risk.js +29 -0
  54. package/dist/catalog/risk.js.map +1 -0
  55. package/dist/catalog/types.d.ts +14 -0
  56. package/dist/catalog/types.d.ts.map +1 -0
  57. package/dist/catalog/types.js +2 -0
  58. package/dist/catalog/types.js.map +1 -0
  59. package/dist/definitions.d.ts +2 -0
  60. package/dist/definitions.d.ts.map +1 -0
  61. package/dist/definitions.js +2 -0
  62. package/dist/definitions.js.map +1 -0
  63. package/dist/execution/atomic-write.d.ts +2 -0
  64. package/dist/execution/atomic-write.d.ts.map +1 -0
  65. package/dist/execution/atomic-write.js +7 -0
  66. package/dist/execution/atomic-write.js.map +1 -0
  67. package/dist/execution/bash.d.ts +3 -0
  68. package/dist/execution/bash.d.ts.map +1 -0
  69. package/dist/execution/bash.js +133 -0
  70. package/dist/execution/bash.js.map +1 -0
  71. package/dist/execution/common/args.d.ts +2 -0
  72. package/dist/execution/common/args.d.ts.map +1 -0
  73. package/dist/execution/common/args.js +6 -0
  74. package/dist/execution/common/args.js.map +1 -0
  75. package/dist/execution/common/output-budget.d.ts +61 -0
  76. package/dist/execution/common/output-budget.d.ts.map +1 -0
  77. package/dist/execution/common/output-budget.js +140 -0
  78. package/dist/execution/common/output-budget.js.map +1 -0
  79. package/dist/execution/common/process-result.d.ts +44 -0
  80. package/dist/execution/common/process-result.d.ts.map +1 -0
  81. package/dist/execution/common/process-result.js +322 -0
  82. package/dist/execution/common/process-result.js.map +1 -0
  83. package/dist/execution/common/search-utils.d.ts +8 -0
  84. package/dist/execution/common/search-utils.d.ts.map +1 -0
  85. package/dist/execution/common/search-utils.js +110 -0
  86. package/dist/execution/common/search-utils.js.map +1 -0
  87. package/dist/execution/common/tool-error.d.ts +8 -0
  88. package/dist/execution/common/tool-error.d.ts.map +1 -0
  89. package/dist/execution/common/tool-error.js +14 -0
  90. package/dist/execution/common/tool-error.js.map +1 -0
  91. package/dist/execution/common/truncate.d.ts +27 -0
  92. package/dist/execution/common/truncate.d.ts.map +1 -0
  93. package/dist/execution/common/truncate.js +108 -0
  94. package/dist/execution/common/truncate.js.map +1 -0
  95. package/dist/execution/common.d.ts +2 -0
  96. package/dist/execution/common.d.ts.map +1 -0
  97. package/dist/execution/common.js +6 -0
  98. package/dist/execution/common.js.map +1 -0
  99. package/dist/execution/core-dispatch.d.ts +4 -0
  100. package/dist/execution/core-dispatch.d.ts.map +1 -0
  101. package/dist/execution/core-dispatch.js +58 -0
  102. package/dist/execution/core-dispatch.js.map +1 -0
  103. package/dist/execution/dispatch.d.ts +4 -0
  104. package/dist/execution/dispatch.d.ts.map +1 -0
  105. package/dist/execution/dispatch.js +54 -0
  106. package/dist/execution/dispatch.js.map +1 -0
  107. package/dist/execution/edit.d.ts +9 -0
  108. package/dist/execution/edit.d.ts.map +1 -0
  109. package/dist/execution/edit.js +175 -0
  110. package/dist/execution/edit.js.map +1 -0
  111. package/dist/execution/file-mutation-queue.d.ts +2 -0
  112. package/dist/execution/file-mutation-queue.d.ts.map +1 -0
  113. package/dist/execution/file-mutation-queue.js +19 -0
  114. package/dist/execution/file-mutation-queue.js.map +1 -0
  115. package/dist/execution/filesystem/atomic-write.d.ts +2 -0
  116. package/dist/execution/filesystem/atomic-write.d.ts.map +1 -0
  117. package/dist/execution/filesystem/atomic-write.js +7 -0
  118. package/dist/execution/filesystem/atomic-write.js.map +1 -0
  119. package/dist/execution/filesystem/edit-args.d.ts +46 -0
  120. package/dist/execution/filesystem/edit-args.d.ts.map +1 -0
  121. package/dist/execution/filesystem/edit-args.js +212 -0
  122. package/dist/execution/filesystem/edit-args.js.map +1 -0
  123. package/dist/execution/filesystem/edit-errors.d.ts +4 -0
  124. package/dist/execution/filesystem/edit-errors.d.ts.map +1 -0
  125. package/dist/execution/filesystem/edit-errors.js +8 -0
  126. package/dist/execution/filesystem/edit-errors.js.map +1 -0
  127. package/dist/execution/filesystem/edit.d.ts +4 -0
  128. package/dist/execution/filesystem/edit.d.ts.map +1 -0
  129. package/dist/execution/filesystem/edit.js +330 -0
  130. package/dist/execution/filesystem/edit.js.map +1 -0
  131. package/dist/execution/filesystem/file-mutation-queue.d.ts +2 -0
  132. package/dist/execution/filesystem/file-mutation-queue.d.ts.map +1 -0
  133. package/dist/execution/filesystem/file-mutation-queue.js +19 -0
  134. package/dist/execution/filesystem/file-mutation-queue.js.map +1 -0
  135. package/dist/execution/filesystem/find.d.ts +3 -0
  136. package/dist/execution/filesystem/find.d.ts.map +1 -0
  137. package/dist/execution/filesystem/find.js +93 -0
  138. package/dist/execution/filesystem/find.js.map +1 -0
  139. package/dist/execution/filesystem/legacy-edit.d.ts +9 -0
  140. package/dist/execution/filesystem/legacy-edit.d.ts.map +1 -0
  141. package/dist/execution/filesystem/legacy-edit.js +122 -0
  142. package/dist/execution/filesystem/legacy-edit.js.map +1 -0
  143. package/dist/execution/filesystem/list.d.ts +3 -0
  144. package/dist/execution/filesystem/list.d.ts.map +1 -0
  145. package/dist/execution/filesystem/list.js +49 -0
  146. package/dist/execution/filesystem/list.js.map +1 -0
  147. package/dist/execution/filesystem/path.d.ts +7 -0
  148. package/dist/execution/filesystem/path.d.ts.map +1 -0
  149. package/dist/execution/filesystem/path.js +69 -0
  150. package/dist/execution/filesystem/path.js.map +1 -0
  151. package/dist/execution/filesystem/read.d.ts +3 -0
  152. package/dist/execution/filesystem/read.d.ts.map +1 -0
  153. package/dist/execution/filesystem/read.js +225 -0
  154. package/dist/execution/filesystem/read.js.map +1 -0
  155. package/dist/execution/filesystem/search.d.ts +3 -0
  156. package/dist/execution/filesystem/search.d.ts.map +1 -0
  157. package/dist/execution/filesystem/search.js +149 -0
  158. package/dist/execution/filesystem/search.js.map +1 -0
  159. package/dist/execution/filesystem/smart-match.d.ts +31 -0
  160. package/dist/execution/filesystem/smart-match.d.ts.map +1 -0
  161. package/dist/execution/filesystem/smart-match.js +309 -0
  162. package/dist/execution/filesystem/smart-match.js.map +1 -0
  163. package/dist/execution/filesystem/text-editing.d.ts +9 -0
  164. package/dist/execution/filesystem/text-editing.d.ts.map +1 -0
  165. package/dist/execution/filesystem/text-editing.js +43 -0
  166. package/dist/execution/filesystem/text-editing.js.map +1 -0
  167. package/dist/execution/filesystem/write.d.ts +3 -0
  168. package/dist/execution/filesystem/write.d.ts.map +1 -0
  169. package/dist/execution/filesystem/write.js +21 -0
  170. package/dist/execution/filesystem/write.js.map +1 -0
  171. package/dist/execution/find.d.ts +3 -0
  172. package/dist/execution/find.d.ts.map +1 -0
  173. package/dist/execution/find.js +90 -0
  174. package/dist/execution/find.js.map +1 -0
  175. package/dist/execution/index.d.ts +18 -0
  176. package/dist/execution/index.d.ts.map +1 -0
  177. package/dist/execution/index.js +17 -0
  178. package/dist/execution/index.js.map +1 -0
  179. package/dist/execution/list.d.ts +3 -0
  180. package/dist/execution/list.d.ts.map +1 -0
  181. package/dist/execution/list.js +46 -0
  182. package/dist/execution/list.js.map +1 -0
  183. package/dist/execution/path.d.ts +7 -0
  184. package/dist/execution/path.d.ts.map +1 -0
  185. package/dist/execution/path.js +69 -0
  186. package/dist/execution/path.js.map +1 -0
  187. package/dist/execution/python/python.d.ts +3 -0
  188. package/dist/execution/python/python.d.ts.map +1 -0
  189. package/dist/execution/python/python.js +566 -0
  190. package/dist/execution/python/python.js.map +1 -0
  191. package/dist/execution/python/runtime.d.ts +22 -0
  192. package/dist/execution/python/runtime.d.ts.map +1 -0
  193. package/dist/execution/python/runtime.js +178 -0
  194. package/dist/execution/python/runtime.js.map +1 -0
  195. package/dist/execution/read.d.ts +3 -0
  196. package/dist/execution/read.d.ts.map +1 -0
  197. package/dist/execution/read.js +97 -0
  198. package/dist/execution/read.js.map +1 -0
  199. package/dist/execution/search-utils.d.ts +8 -0
  200. package/dist/execution/search-utils.d.ts.map +1 -0
  201. package/dist/execution/search-utils.js +110 -0
  202. package/dist/execution/search-utils.js.map +1 -0
  203. package/dist/execution/search.d.ts +3 -0
  204. package/dist/execution/search.d.ts.map +1 -0
  205. package/dist/execution/search.js +131 -0
  206. package/dist/execution/search.js.map +1 -0
  207. package/dist/execution/shell/bash.d.ts +3 -0
  208. package/dist/execution/shell/bash.d.ts.map +1 -0
  209. package/dist/execution/shell/bash.js +134 -0
  210. package/dist/execution/shell/bash.js.map +1 -0
  211. package/dist/execution/truncate.d.ts +21 -0
  212. package/dist/execution/truncate.d.ts.map +1 -0
  213. package/dist/execution/truncate.js +94 -0
  214. package/dist/execution/truncate.js.map +1 -0
  215. package/dist/execution/web/web-fetch.d.ts +3 -0
  216. package/dist/execution/web/web-fetch.d.ts.map +1 -0
  217. package/dist/execution/web/web-fetch.js +132 -0
  218. package/dist/execution/web/web-fetch.js.map +1 -0
  219. package/dist/execution/web/web-search.d.ts +3 -0
  220. package/dist/execution/web/web-search.d.ts.map +1 -0
  221. package/dist/execution/web/web-search.js +59 -0
  222. package/dist/execution/web/web-search.js.map +1 -0
  223. package/dist/execution/web-fetch.d.ts +3 -0
  224. package/dist/execution/web-fetch.d.ts.map +1 -0
  225. package/dist/execution/web-fetch.js +132 -0
  226. package/dist/execution/web-fetch.js.map +1 -0
  227. package/dist/execution/web-search.d.ts +3 -0
  228. package/dist/execution/web-search.d.ts.map +1 -0
  229. package/dist/execution/web-search.js +59 -0
  230. package/dist/execution/web-search.js.map +1 -0
  231. package/dist/execution/write.d.ts +3 -0
  232. package/dist/execution/write.d.ts.map +1 -0
  233. package/dist/execution/write.js +21 -0
  234. package/dist/execution/write.js.map +1 -0
  235. package/dist/index.d.ts +9 -0
  236. package/dist/index.d.ts.map +1 -0
  237. package/dist/index.js +11 -0
  238. package/dist/index.js.map +1 -0
  239. package/dist/safety/command-policy-git.d.ts +2 -0
  240. package/dist/safety/command-policy-git.d.ts.map +1 -0
  241. package/dist/safety/command-policy-git.js +88 -0
  242. package/dist/safety/command-policy-git.js.map +1 -0
  243. package/dist/safety/command-policy-options.d.ts +6 -0
  244. package/dist/safety/command-policy-options.d.ts.map +1 -0
  245. package/dist/safety/command-policy-options.js +112 -0
  246. package/dist/safety/command-policy-options.js.map +1 -0
  247. package/dist/safety/command-policy-packages.d.ts +4 -0
  248. package/dist/safety/command-policy-packages.d.ts.map +1 -0
  249. package/dist/safety/command-policy-packages.js +161 -0
  250. package/dist/safety/command-policy-packages.js.map +1 -0
  251. package/dist/safety/command-policy-parser.d.ts +11 -0
  252. package/dist/safety/command-policy-parser.d.ts.map +1 -0
  253. package/dist/safety/command-policy-parser.js +121 -0
  254. package/dist/safety/command-policy-parser.js.map +1 -0
  255. package/dist/safety/command-policy-wrappers.d.ts +6 -0
  256. package/dist/safety/command-policy-wrappers.d.ts.map +1 -0
  257. package/dist/safety/command-policy-wrappers.js +95 -0
  258. package/dist/safety/command-policy-wrappers.js.map +1 -0
  259. package/dist/safety/command-policy.d.ts +6 -0
  260. package/dist/safety/command-policy.d.ts.map +1 -0
  261. package/dist/safety/command-policy.js +118 -0
  262. package/dist/safety/command-policy.js.map +1 -0
  263. package/dist/types.d.ts +78 -0
  264. package/dist/types.d.ts.map +1 -0
  265. package/dist/types.js +2 -0
  266. package/dist/types.js.map +1 -0
  267. package/package.json +48 -0
@@ -0,0 +1,134 @@
1
+ import { spawn } from "node:child_process";
2
+ import { numberArg } from "../common/args.js";
3
+ import { boundLiveOutputChunk } from "../common/output-budget.js";
4
+ import { buildProcessResult } from "../common/process-result.js";
5
+ const FORCE_KILL_AFTER_MS = 2000;
6
+ export async function executeBash(args, context) {
7
+ if (typeof args.command !== "string" || args.command.trim().length === 0) {
8
+ throw new Error("Tool argument 'command' must be a non-empty string.");
9
+ }
10
+ const timeoutSeconds = typeof args.timeout === "number"
11
+ ? Math.max(0, numberArg(args.timeout, 0))
12
+ : undefined;
13
+ const stdoutChunks = [];
14
+ const stderrChunks = [];
15
+ const combinedChunks = [];
16
+ const startedAt = performance.now();
17
+ return await new Promise((resolve, reject) => {
18
+ if (context.signal?.aborted) {
19
+ reject(new Error("Command aborted."));
20
+ return;
21
+ }
22
+ const child = spawn(args.command, {
23
+ cwd: context.cwd,
24
+ shell: true,
25
+ detached: process.platform !== "win32",
26
+ env: process.env,
27
+ });
28
+ let settled = false;
29
+ let timedOut = false;
30
+ let timeoutKilled = false;
31
+ let timeout;
32
+ let forceKillTimeout;
33
+ const cleanup = () => {
34
+ if (timeout)
35
+ clearTimeout(timeout);
36
+ if (forceKillTimeout)
37
+ clearTimeout(forceKillTimeout);
38
+ context.signal?.removeEventListener("abort", onAbort);
39
+ };
40
+ const kill = (signal = "SIGTERM") => {
41
+ try {
42
+ if (process.platform !== "win32" && child.pid) {
43
+ process.kill(-child.pid, signal);
44
+ }
45
+ else {
46
+ child.kill(signal);
47
+ }
48
+ }
49
+ catch {
50
+ child.kill(signal);
51
+ }
52
+ };
53
+ const onAbort = () => {
54
+ if (settled)
55
+ return;
56
+ kill();
57
+ settled = true;
58
+ cleanup();
59
+ reject(new Error("Command aborted."));
60
+ };
61
+ context.signal?.addEventListener("abort", onAbort, { once: true });
62
+ if (timeoutSeconds !== undefined && timeoutSeconds > 0) {
63
+ timeout = setTimeout(() => {
64
+ if (settled)
65
+ return;
66
+ timedOut = true;
67
+ timeoutKilled = true;
68
+ kill("SIGTERM");
69
+ forceKillTimeout = setTimeout(() => {
70
+ if (!settled)
71
+ kill("SIGKILL");
72
+ }, FORCE_KILL_AFTER_MS);
73
+ }, timeoutSeconds * 1000);
74
+ }
75
+ child.stdout?.on("data", (chunk) => {
76
+ stdoutChunks.push(chunk);
77
+ combinedChunks.push(chunk);
78
+ context.onUpdate?.({
79
+ kind: "output",
80
+ stream: "stdout",
81
+ chunk: boundLiveOutputChunk(chunk.toString("utf8")),
82
+ });
83
+ });
84
+ child.stderr?.on("data", (chunk) => {
85
+ stderrChunks.push(chunk);
86
+ combinedChunks.push(chunk);
87
+ context.onUpdate?.({
88
+ kind: "output",
89
+ stream: "stderr",
90
+ chunk: boundLiveOutputChunk(chunk.toString("utf8")),
91
+ });
92
+ });
93
+ child.on("error", (error) => {
94
+ if (settled)
95
+ return;
96
+ settled = true;
97
+ cleanup();
98
+ reject(error);
99
+ });
100
+ child.on("close", (code, signal) => {
101
+ if (settled)
102
+ return;
103
+ settled = true;
104
+ cleanup();
105
+ void buildResult(stdoutChunks, stderrChunks, combinedChunks, code, signal, context.dataDir, {
106
+ durationMs: Math.round(performance.now() - startedAt),
107
+ timedOut,
108
+ timeoutKilled,
109
+ timeoutMessage: timeoutSeconds !== undefined
110
+ ? `Command timed out after ${timeoutSeconds}s and ${timeoutKilled ? "was killed" : "was not killed"}.`
111
+ : undefined,
112
+ })
113
+ .then(resolve)
114
+ .catch(reject);
115
+ });
116
+ });
117
+ }
118
+ async function buildResult(stdoutChunks, stderrChunks, combinedChunks, code, signal, dataDir, options = {}) {
119
+ return buildProcessResult({
120
+ stdoutChunks,
121
+ stderrChunks,
122
+ combinedChunks,
123
+ code,
124
+ signal,
125
+ outputFilePrefix: "nerve-bash",
126
+ exitMessagePrefix: "Command",
127
+ dataDir,
128
+ durationMs: options.durationMs,
129
+ timedOut: options.timedOut,
130
+ timeoutKilled: options.timeoutKilled,
131
+ timeoutMessage: options.timeoutMessage,
132
+ });
133
+ }
134
+ //# sourceMappingURL=bash.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bash.js","sourceRoot":"","sources":["../../../src/execution/shell/bash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE3C,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAC;AAEjE,MAAM,mBAAmB,GAAG,IAAI,CAAC;AAEjC,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,IAA6B,EAC7B,OAA6B;IAE7B,IAAI,OAAO,IAAI,CAAC,OAAO,KAAK,QAAQ,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzE,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;IACzE,CAAC;IAED,MAAM,cAAc,GAClB,OAAO,IAAI,CAAC,OAAO,KAAK,QAAQ;QAC9B,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;QACzC,CAAC,CAAC,SAAS,CAAC;IAChB,MAAM,YAAY,GAAa,EAAE,CAAC;IAClC,MAAM,YAAY,GAAa,EAAE,CAAC;IAClC,MAAM,cAAc,GAAa,EAAE,CAAC;IACpC,MAAM,SAAS,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;IAEpC,OAAO,MAAM,IAAI,OAAO,CAAsB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAChE,IAAI,OAAO,CAAC,MAAM,EAAE,OAAO,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,UAKI,EAAE;IAEN,OAAO,kBAAkB,CAAC;QACxB,YAAY;QACZ,YAAY;QACZ,cAAc;QACd,IAAI;QACJ,MAAM;QACN,gBAAgB,EAAE,YAAY;QAC9B,iBAAiB,EAAE,SAAS;QAC5B,OAAO;QACP,UAAU,EAAE,OAAO,CAAC,UAAU;QAC9B,QAAQ,EAAE,OAAO,CAAC,QAAQ;QAC1B,aAAa,EAAE,OAAO,CAAC,aAAa;QACpC,cAAc,EAAE,OAAO,CAAC,cAAc;KACvC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,21 @@
1
+ export declare const DEFAULT_MAX_LINES = 2000;
2
+ export declare const DEFAULT_MAX_BYTES: number;
3
+ export declare const GREP_MAX_LINE_LENGTH = 500;
4
+ export type TruncationDirection = "head" | "tail" | "line";
5
+ export type TruncationResult = {
6
+ text: string;
7
+ truncated: boolean;
8
+ omittedLines: number;
9
+ omittedBytes: number;
10
+ direction: TruncationDirection;
11
+ };
12
+ type TruncateOptions = {
13
+ maxLines?: number;
14
+ maxBytes?: number;
15
+ };
16
+ export declare function truncateHead(input: string, options?: TruncateOptions): TruncationResult;
17
+ export declare function truncateTail(input: string, options?: TruncateOptions): TruncationResult;
18
+ export declare function truncateLine(input: string, maxChars?: number): TruncationResult;
19
+ export declare function formatSize(bytes: number): string;
20
+ export {};
21
+ //# sourceMappingURL=truncate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"truncate.d.ts","sourceRoot":"","sources":["../../src/execution/truncate.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iBAAiB,OAAO,CAAC;AACtC,eAAO,MAAM,iBAAiB,QAAY,CAAC;AAC3C,eAAO,MAAM,oBAAoB,MAAM,CAAC;AAExC,MAAM,MAAM,mBAAmB,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;AAE3D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,OAAO,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,mBAAmB,CAAC;CAChC,CAAC;AAEF,KAAK,eAAe,GAAG;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,eAAoB,GAC5B,gBAAgB,CA6BlB;AAED,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,eAAoB,GAC5B,gBAAgB,CA8BlB;AAED,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,EACb,QAAQ,SAAuB,GAC9B,gBAAgB,CAkBlB;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAIhD"}
@@ -0,0 +1,94 @@
1
+ export const DEFAULT_MAX_LINES = 2000;
2
+ export const DEFAULT_MAX_BYTES = 50 * 1024;
3
+ export const GREP_MAX_LINE_LENGTH = 500;
4
+ export function truncateHead(input, options = {}) {
5
+ const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
6
+ const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
7
+ const lines = input.split("\n");
8
+ const selected = lines.slice(0, maxLines);
9
+ let text = selected.join("\n");
10
+ let omittedLines = Math.max(0, lines.length - selected.length);
11
+ let omittedBytes = 0;
12
+ while (byteLength(text) > maxBytes && selected.length > 0) {
13
+ const removed = selected.pop() ?? "";
14
+ omittedLines += 1;
15
+ omittedBytes += byteLength(removed) + 1;
16
+ text = selected.join("\n");
17
+ }
18
+ if (byteLength(text) > maxBytes) {
19
+ const truncated = truncateUtf8Bytes(text, maxBytes);
20
+ omittedBytes += byteLength(text) - byteLength(truncated);
21
+ text = truncated;
22
+ }
23
+ return {
24
+ text,
25
+ truncated: omittedLines > 0 || omittedBytes > 0,
26
+ omittedLines,
27
+ omittedBytes,
28
+ direction: "head",
29
+ };
30
+ }
31
+ export function truncateTail(input, options = {}) {
32
+ const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
33
+ const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
34
+ const lines = input.split("\n");
35
+ const selected = lines.slice(Math.max(0, lines.length - maxLines));
36
+ let text = selected.join("\n");
37
+ let omittedLines = Math.max(0, lines.length - selected.length);
38
+ let omittedBytes = 0;
39
+ while (byteLength(text) > maxBytes && selected.length > 0) {
40
+ const removed = selected.shift() ?? "";
41
+ omittedLines += 1;
42
+ omittedBytes += byteLength(removed) + 1;
43
+ text = selected.join("\n");
44
+ }
45
+ if (byteLength(text) > maxBytes) {
46
+ const bytes = Buffer.from(text, "utf8");
47
+ const truncated = bytes.subarray(bytes.length - maxBytes).toString("utf8");
48
+ omittedBytes += byteLength(text) - byteLength(truncated);
49
+ text = truncated;
50
+ }
51
+ return {
52
+ text,
53
+ truncated: omittedLines > 0 || omittedBytes > 0,
54
+ omittedLines,
55
+ omittedBytes,
56
+ direction: "tail",
57
+ };
58
+ }
59
+ export function truncateLine(input, maxChars = GREP_MAX_LINE_LENGTH) {
60
+ if (input.length <= maxChars) {
61
+ return {
62
+ text: input,
63
+ truncated: false,
64
+ omittedLines: 0,
65
+ omittedBytes: 0,
66
+ direction: "line",
67
+ };
68
+ }
69
+ const omitted = input.slice(maxChars);
70
+ return {
71
+ text: `${input.slice(0, maxChars)}…[truncated ${omitted.length} chars]`,
72
+ truncated: true,
73
+ omittedLines: 0,
74
+ omittedBytes: byteLength(omitted),
75
+ direction: "line",
76
+ };
77
+ }
78
+ export function formatSize(bytes) {
79
+ if (bytes < 1024)
80
+ return `${bytes}B`;
81
+ if (bytes < 1024 * 1024)
82
+ return `${(bytes / 1024).toFixed(1)}KB`;
83
+ return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
84
+ }
85
+ function truncateUtf8Bytes(input, maxBytes) {
86
+ const bytes = Buffer.from(input, "utf8");
87
+ if (bytes.length <= maxBytes)
88
+ return input;
89
+ return bytes.subarray(0, maxBytes).toString("utf8");
90
+ }
91
+ function byteLength(input) {
92
+ return Buffer.byteLength(input, "utf8");
93
+ }
94
+ //# sourceMappingURL=truncate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"truncate.js","sourceRoot":"","sources":["../../src/execution/truncate.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,CAAC;AACtC,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC;AAC3C,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAAG,CAAC;AAiBxC,MAAM,UAAU,YAAY,CAC1B,KAAa,EACb,UAA2B,EAAE;IAE7B,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,iBAAiB,CAAC;IACvD,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,iBAAiB,CAAC;IACvD,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAChC,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;IAC1C,IAAI,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC/B,IAAI,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC/D,IAAI,YAAY,GAAG,CAAC,CAAC;IAErB,OAAO,UAAU,CAAC,IAAI,CAAC,GAAG,QAAQ,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC1D,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC;QACrC,YAAY,IAAI,CAAC,CAAC;QAClB,YAAY,IAAI,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,GAAG,QA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C,OAAO,CAAC;QACjC,SAAS,EAAE,MAAM;KAClB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,IAAI,KAAK,GAAG,IAAI;QAAE,OAAO,GAAG,KAAK,GAAG,CAAC;IACrC,IAAI,KAAK,GAAG,IAAI,GAAG,IAAI;QAAE,OAAO,GAAG,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;IACjE,OAAO,GAAG,CAAC,KAAK,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;AACnD,CAAC;AAED,SAAS,iBAAiB,CAAC,KAAa,EAAE,QAAgB;IACxD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IACzC,IAAI,KAAK,CAAC,MAAM,IAAI,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC3C,OAAO,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;AACtD,CAAC;AAED,SAAS,UAAU,CAAC,KAAa;IAC/B,OAAO,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AAC1C,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { ToolExecutionContext, ToolExecutionResult } from "../../types.js";
2
+ export declare function executeWebFetch(args: Record<string, unknown>, context: ToolExecutionContext): Promise<ToolExecutionResult>;
3
+ //# sourceMappingURL=web-fetch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-fetch.d.ts","sourceRoot":"","sources":["../../../src/execution/web/web-fetch.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AA2FhF,wBAAsB,eAAe,CACnC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,mBAAmB,CAAC,CAqE9B"}
@@ -0,0 +1,132 @@
1
+ import { createHash } from "node:crypto";
2
+ import { mkdir, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { NodeHtmlMarkdown } from "node-html-markdown";
6
+ const MAX_INLINE_BYTES = 512 * 1024;
7
+ const CONTENT_TYPE_EXT = {
8
+ "text/html": ".html",
9
+ "text/plain": ".txt",
10
+ "text/xml": ".xml",
11
+ "text/css": ".css",
12
+ "text/csv": ".csv",
13
+ "application/json": ".json",
14
+ "application/xml": ".xml",
15
+ "application/pdf": ".pdf",
16
+ "image/png": ".png",
17
+ "image/jpeg": ".jpg",
18
+ "image/gif": ".gif",
19
+ "image/webp": ".webp",
20
+ "image/svg+xml": ".svg",
21
+ };
22
+ function stringArg(value, name) {
23
+ if (typeof value !== "string" || value.trim().length === 0) {
24
+ throw new Error(`${name} must be a non-empty string.`);
25
+ }
26
+ return value;
27
+ }
28
+ function baseContentType(contentType) {
29
+ return contentType.split(";")[0]?.trim().toLowerCase() || "";
30
+ }
31
+ function getExtension(contentType) {
32
+ return CONTENT_TYPE_EXT[baseContentType(contentType)] ?? ".bin";
33
+ }
34
+ function isTextType(contentType) {
35
+ const base = baseContentType(contentType);
36
+ return (base.startsWith("text/") ||
37
+ base === "application/json" ||
38
+ base === "application/xml" ||
39
+ base === "application/javascript");
40
+ }
41
+ function isHtml(contentType) {
42
+ return baseContentType(contentType) === "text/html";
43
+ }
44
+ function saveDir(context) {
45
+ return context.dataDir
46
+ ? join(context.dataDir, "tmp", "web-fetch")
47
+ : join(tmpdir(), "nerve-web-fetch");
48
+ }
49
+ function tmpPath(context, url, ext) {
50
+ const hash = createHash("sha256").update(url).digest("hex").slice(0, 12);
51
+ return join(saveDir(context), `${hash}${ext}`);
52
+ }
53
+ function formatSize(bytes) {
54
+ if (bytes < 1024)
55
+ return `${bytes} B`;
56
+ const kb = bytes / 1024;
57
+ if (kb < 1024)
58
+ return `${kb.toFixed(1)} KB`;
59
+ return `${(kb / 1024).toFixed(1)} MB`;
60
+ }
61
+ function timeoutSignal(signal, milliseconds) {
62
+ const timeout = AbortSignal.timeout(milliseconds);
63
+ return signal ? AbortSignal.any([signal, timeout]) : timeout;
64
+ }
65
+ async function saveContent(context, url, ext, content) {
66
+ await mkdir(saveDir(context), { recursive: true });
67
+ const path = tmpPath(context, url, ext);
68
+ await writeFile(path, content);
69
+ return path;
70
+ }
71
+ export async function executeWebFetch(args, context) {
72
+ const url = stringArg(args.url, "url");
73
+ const raw = args.raw === true;
74
+ const response = await fetch(url, {
75
+ headers: {
76
+ "User-Agent": "nerve/1.0",
77
+ Accept: "text/html,application/json,text/plain,*/*",
78
+ },
79
+ redirect: "follow",
80
+ signal: timeoutSignal(context.signal, 60_000),
81
+ });
82
+ if (!response.ok) {
83
+ throw new Error(`Fetch failed: ${response.status} ${response.statusText}`);
84
+ }
85
+ const contentType = response.headers.get("content-type") ?? "application/octet-stream";
86
+ const buffer = Buffer.from(await response.arrayBuffer());
87
+ const size = buffer.byteLength;
88
+ const ext = getExtension(contentType);
89
+ const details = {
90
+ url,
91
+ status: response.status,
92
+ contentType,
93
+ size,
94
+ converted: false,
95
+ savedTo: undefined,
96
+ };
97
+ if (raw) {
98
+ details.savedTo = await saveContent(context, url, ext, buffer);
99
+ const content = `Raw content saved to: ${details.savedTo}\nSize: ${formatSize(size)}\nContent-Type: ${contentType}`;
100
+ return {
101
+ content,
102
+ contentBlocks: [{ type: "text", text: content }],
103
+ details,
104
+ };
105
+ }
106
+ if (!isTextType(contentType)) {
107
+ details.savedTo = await saveContent(context, url, ext, buffer);
108
+ const content = `Binary content saved to: ${details.savedTo}\nSize: ${formatSize(size)}\nContent-Type: ${contentType}\nUse the read tool to inspect it.`;
109
+ return {
110
+ content,
111
+ contentBlocks: [{ type: "text", text: content }],
112
+ details,
113
+ };
114
+ }
115
+ let text = buffer.toString("utf8");
116
+ if (isHtml(contentType)) {
117
+ text = NodeHtmlMarkdown.translate(text);
118
+ details.converted = true;
119
+ }
120
+ if (Buffer.byteLength(text) <= MAX_INLINE_BYTES) {
121
+ return {
122
+ content: text,
123
+ contentBlocks: [{ type: "text", text }],
124
+ details,
125
+ };
126
+ }
127
+ const saveExt = details.converted ? ".md" : ext;
128
+ details.savedTo = await saveContent(context, url, saveExt, text);
129
+ const content = `Response saved to: ${details.savedTo}\nSize: ${formatSize(Buffer.byteLength(text))}${details.converted ? " (converted to markdown)" : ""}\nThe content is large — use grep or read to inspect it.`;
130
+ return { content, contentBlocks: [{ type: "text", text: content }], details };
131
+ }
132
+ //# sourceMappingURL=web-fetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-fetch.js","sourceRoot":"","sources":["../../../src/execution/web/web-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAGtD,MAAM,gBAAgB,GAAG,GAAG,GAAG,IAAI,CAAC;AAEpC,MAAM,gBAAgB,GAA2B;IAC/C,WAAW,EAAE,OAAO;IACpB,YAAY,EAAE,MAAM;IACpB,UAAU,EAAE,MAAM;IAClB,UAAU,EAAE,MAAM;IAClB,UAAU,EAAE,MAAM;IAClB,kBAAkB,EAAE,OAAO;IAC3B,iBAAiB,EAAE,MAAM;IACzB,iBAAiB,EAAE,MAAM;IACzB,WAAW,EAAE,MAAM;IACnB,YAAY,EAAE,MAAM;IACpB,WAAW,EAAE,MAAM;IACnB,YAAY,EAAE,OAAO;IACrB,eAAe,EAAE,MAAM;CACxB,CAAC;A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,CAAC,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,0BAA0B,CAAC,CAAC,CAAC,EAAE,0DAA0D,CAAC;IACpN,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC;AAChF,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { ToolExecutionContext, ToolExecutionResult } from "../../types.js";
2
+ export declare function executeWebSearch(args: Record<string, unknown>, context: ToolExecutionContext): Promise<ToolExecutionResult>;
3
+ //# sourceMappingURL=web-search.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-search.d.ts","sourceRoot":"","sources":["../../../src/execution/web/web-search.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAmChF,wBAAsB,gBAAgB,CACpC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,mBAAmB,CAAC,CAmD9B"}
@@ -0,0 +1,59 @@
1
+ import { numberArg } from "../common/args.js";
2
+ function stringArg(value, name) {
3
+ if (typeof value !== "string" || value.trim().length === 0) {
4
+ throw new Error(`${name} must be a non-empty string.`);
5
+ }
6
+ return value;
7
+ }
8
+ function maxResultsArg(value) {
9
+ const parsed = numberArg(value, 5);
10
+ return Math.min(20, Math.max(1, parsed));
11
+ }
12
+ function timeoutSignal(signal, milliseconds) {
13
+ const timeout = AbortSignal.timeout(milliseconds);
14
+ return signal ? AbortSignal.any([signal, timeout]) : timeout;
15
+ }
16
+ export async function executeWebSearch(args, context) {
17
+ const query = stringArg(args.query, "query");
18
+ const maxResults = maxResultsArg(args.max_results);
19
+ const apiKey = await context.getApiKey?.("tavily");
20
+ if (!apiKey) {
21
+ throw new Error("Tavily API key is not configured. Configure Web Search in Nerve Settings.");
22
+ }
23
+ const response = await fetch("https://api.tavily.com/search", {
24
+ method: "POST",
25
+ headers: { "Content-Type": "application/json" },
26
+ body: JSON.stringify({
27
+ api_key: apiKey,
28
+ query,
29
+ max_results: maxResults,
30
+ include_answer: true,
31
+ }),
32
+ signal: timeoutSignal(context.signal, 60_000),
33
+ });
34
+ if (!response.ok) {
35
+ throw new Error(`Tavily API error: ${response.status} ${await response.text()}`);
36
+ }
37
+ const data = (await response.json());
38
+ const results = Array.isArray(data.results) ? data.results : [];
39
+ const lines = [];
40
+ if (data.answer)
41
+ lines.push(`**Answer:** ${data.answer}`, "");
42
+ for (const result of results) {
43
+ lines.push(`### ${result.title}`, result.url, "", result.content, "");
44
+ }
45
+ const content = lines.join("\n").trimEnd();
46
+ return {
47
+ content,
48
+ contentBlocks: [{ type: "text", text: content }],
49
+ details: {
50
+ query,
51
+ answer: data.answer,
52
+ results: results.map((result) => ({
53
+ title: result.title,
54
+ url: result.url,
55
+ })),
56
+ },
57
+ };
58
+ }
59
+ //# sourceMappingURL=web-search.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-search.js","sourceRoot":"","sources":["../../../src/execution/web/web-search.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAc9C,SAAS,SAAS,CAAC,KAAc,EAAE,IAAY;IAC7C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC3D,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,8BAA8B,CAAC,CAAC;IACzD,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,aAAa,CAAC,KAAc;IACnC,MAAM,MAAM,GAAG,SAAS,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACnC,OAAO,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,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G,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAmB,CAAC;IACvD,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;IAEhE,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,IAAI,IAAI,CAAC,MAAM;QAAE,KAAK,CAAC,IAAI,CAAC,eAAe,IAAI,CAAC,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC;IAC9D,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,KAAK,CAAC,IAAI,CAAC,OAAO,MAAM,CAAC,KAAK,EAAE,EAAE,MAAM,CAAC,GAAG,EAAE,EAAE,EAAE,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IACxE,CAAC;IACD,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,CAAC;IAE3C,OAAO;QACL,OAAO;QACP,aAAa,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;QAChD,OAAO,EAAE;YACP,KAAK;YACL,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;gBAChC,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,GAAG,EAAE,MAAM,CAAC,GAAG;aAChB,CAAC,CAAC;SACJ;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { ToolExecutionContext, ToolExecutionResult } from "../types.js";
2
+ export declare function executeWebFetch(args: Record<string, unknown>, context: ToolExecutionContext): Promise<ToolExecutionResult>;
3
+ //# sourceMappingURL=web-fetch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-fetch.d.ts","sourceRoot":"","sources":["../../src/execution/web-fetch.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AA2F7E,wBAAsB,eAAe,CACnC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,mBAAmB,CAAC,CAqE9B"}
@@ -0,0 +1,132 @@
1
+ import { createHash } from "node:crypto";
2
+ import { mkdir, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { NodeHtmlMarkdown } from "node-html-markdown";
6
+ const MAX_INLINE_BYTES = 512 * 1024;
7
+ const CONTENT_TYPE_EXT = {
8
+ "text/html": ".html",
9
+ "text/plain": ".txt",
10
+ "text/xml": ".xml",
11
+ "text/css": ".css",
12
+ "text/csv": ".csv",
13
+ "application/json": ".json",
14
+ "application/xml": ".xml",
15
+ "application/pdf": ".pdf",
16
+ "image/png": ".png",
17
+ "image/jpeg": ".jpg",
18
+ "image/gif": ".gif",
19
+ "image/webp": ".webp",
20
+ "image/svg+xml": ".svg",
21
+ };
22
+ function stringArg(value, name) {
23
+ if (typeof value !== "string" || value.trim().length === 0) {
24
+ throw new Error(`${name} must be a non-empty string.`);
25
+ }
26
+ return value;
27
+ }
28
+ function baseContentType(contentType) {
29
+ return contentType.split(";")[0]?.trim().toLowerCase() || "";
30
+ }
31
+ function getExtension(contentType) {
32
+ return CONTENT_TYPE_EXT[baseContentType(contentType)] ?? ".bin";
33
+ }
34
+ function isTextType(contentType) {
35
+ const base = baseContentType(contentType);
36
+ return (base.startsWith("text/") ||
37
+ base === "application/json" ||
38
+ base === "application/xml" ||
39
+ base === "application/javascript");
40
+ }
41
+ function isHtml(contentType) {
42
+ return baseContentType(contentType) === "text/html";
43
+ }
44
+ function saveDir(context) {
45
+ return context.dataDir
46
+ ? join(context.dataDir, "tmp", "web-fetch")
47
+ : join(tmpdir(), "nerve-web-fetch");
48
+ }
49
+ function tmpPath(context, url, ext) {
50
+ const hash = createHash("sha256").update(url).digest("hex").slice(0, 12);
51
+ return join(saveDir(context), `${hash}${ext}`);
52
+ }
53
+ function formatSize(bytes) {
54
+ if (bytes < 1024)
55
+ return `${bytes} B`;
56
+ const kb = bytes / 1024;
57
+ if (kb < 1024)
58
+ return `${kb.toFixed(1)} KB`;
59
+ return `${(kb / 1024).toFixed(1)} MB`;
60
+ }
61
+ function timeoutSignal(signal, milliseconds) {
62
+ const timeout = AbortSignal.timeout(milliseconds);
63
+ return signal ? AbortSignal.any([signal, timeout]) : timeout;
64
+ }
65
+ async function saveContent(context, url, ext, content) {
66
+ await mkdir(saveDir(context), { recursive: true });
67
+ const path = tmpPath(context, url, ext);
68
+ await writeFile(path, content);
69
+ return path;
70
+ }
71
+ export async function executeWebFetch(args, context) {
72
+ const url = stringArg(args.url, "url");
73
+ const raw = args.raw === true;
74
+ const response = await fetch(url, {
75
+ headers: {
76
+ "User-Agent": "nerve/1.0",
77
+ Accept: "text/html,application/json,text/plain,*/*",
78
+ },
79
+ redirect: "follow",
80
+ signal: timeoutSignal(context.signal, 60_000),
81
+ });
82
+ if (!response.ok) {
83
+ throw new Error(`Fetch failed: ${response.status} ${response.statusText}`);
84
+ }
85
+ const contentType = response.headers.get("content-type") ?? "application/octet-stream";
86
+ const buffer = Buffer.from(await response.arrayBuffer());
87
+ const size = buffer.byteLength;
88
+ const ext = getExtension(contentType);
89
+ const details = {
90
+ url,
91
+ status: response.status,
92
+ contentType,
93
+ size,
94
+ converted: false,
95
+ savedTo: undefined,
96
+ };
97
+ if (raw) {
98
+ details.savedTo = await saveContent(context, url, ext, buffer);
99
+ const content = `Raw content saved to: ${details.savedTo}\nSize: ${formatSize(size)}\nContent-Type: ${contentType}`;
100
+ return {
101
+ content,
102
+ contentBlocks: [{ type: "text", text: content }],
103
+ details,
104
+ };
105
+ }
106
+ if (!isTextType(contentType)) {
107
+ details.savedTo = await saveContent(context, url, ext, buffer);
108
+ const content = `Binary content saved to: ${details.savedTo}\nSize: ${formatSize(size)}\nContent-Type: ${contentType}\nUse the read tool to inspect it.`;
109
+ return {
110
+ content,
111
+ contentBlocks: [{ type: "text", text: content }],
112
+ details,
113
+ };
114
+ }
115
+ let text = buffer.toString("utf8");
116
+ if (isHtml(contentType)) {
117
+ text = NodeHtmlMarkdown.translate(text);
118
+ details.converted = true;
119
+ }
120
+ if (Buffer.byteLength(text) <= MAX_INLINE_BYTES) {
121
+ return {
122
+ content: text,
123
+ contentBlocks: [{ type: "text", text }],
124
+ details,
125
+ };
126
+ }
127
+ const saveExt = details.converted ? ".md" : ext;
128
+ details.savedTo = await saveContent(context, url, saveExt, text);
129
+ const content = `Response saved to: ${details.savedTo}\nSize: ${formatSize(Buffer.byteLength(text))}${details.converted ? " (converted to markdown)" : ""}\nThe content is large — use grep or read to inspect it.`;
130
+ return { content, contentBlocks: [{ type: "text", text: content }], details };
131
+ }
132
+ //# sourceMappingURL=web-fetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-fetch.js","sourceRoot":"","sources":["../../src/execution/web-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAGtD,MAAM,gBAAgB,GAAG,GAAG,GAAG,IAAI,CAAC;AAEpC,MAAM,gBAAgB,GAA2B;IAC/C,WAAW,EAAE,OAAO;IACpB,YAAY,EAAE,MAAM;IACpB,UAAU,EAAE,MAAM;IAClB,UAAU,EAAE,MAAM;IAClB,UAAU,EAAE,MAAM;IAClB,kBAAkB,EAAE,OAAO;IAC3B,iBAAiB,EAAE,MAAM;I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