leaf-coding-agent 1.0.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 (598) hide show
  1. package/dist/cli/args.d.ts +55 -0
  2. package/dist/cli/args.d.ts.map +1 -0
  3. package/dist/cli/args.js +356 -0
  4. package/dist/cli/args.js.map +1 -0
  5. package/dist/cli/config-selector.d.ts +14 -0
  6. package/dist/cli/config-selector.d.ts.map +1 -0
  7. package/dist/cli/config-selector.js +31 -0
  8. package/dist/cli/config-selector.js.map +1 -0
  9. package/dist/cli/file-processor.d.ts +15 -0
  10. package/dist/cli/file-processor.d.ts.map +1 -0
  11. package/dist/cli/file-processor.js +82 -0
  12. package/dist/cli/file-processor.js.map +1 -0
  13. package/dist/cli/initial-message.d.ts +18 -0
  14. package/dist/cli/initial-message.d.ts.map +1 -0
  15. package/dist/cli/initial-message.js +22 -0
  16. package/dist/cli/initial-message.js.map +1 -0
  17. package/dist/cli/list-models.d.ts +9 -0
  18. package/dist/cli/list-models.d.ts.map +1 -0
  19. package/dist/cli/list-models.js +98 -0
  20. package/dist/cli/list-models.js.map +1 -0
  21. package/dist/cli/session-picker.d.ts +9 -0
  22. package/dist/cli/session-picker.d.ts.map +1 -0
  23. package/dist/cli/session-picker.js +35 -0
  24. package/dist/cli/session-picker.js.map +1 -0
  25. package/dist/cli.d.ts +3 -0
  26. package/dist/cli.d.ts.map +1 -0
  27. package/dist/cli.js +18 -0
  28. package/dist/cli.js.map +1 -0
  29. package/dist/config.d.ts +92 -0
  30. package/dist/config.d.ts.map +1 -0
  31. package/dist/config.js +428 -0
  32. package/dist/config.js.map +1 -0
  33. package/dist/core/agent-session-runtime.d.ts +117 -0
  34. package/dist/core/agent-session-runtime.d.ts.map +1 -0
  35. package/dist/core/agent-session-runtime.js +300 -0
  36. package/dist/core/agent-session-runtime.js.map +1 -0
  37. package/dist/core/agent-session-services.d.ts +87 -0
  38. package/dist/core/agent-session-services.d.ts.map +1 -0
  39. package/dist/core/agent-session-services.js +119 -0
  40. package/dist/core/agent-session-services.js.map +1 -0
  41. package/dist/core/agent-session.d.ts +602 -0
  42. package/dist/core/agent-session.d.ts.map +1 -0
  43. package/dist/core/agent-session.js +2523 -0
  44. package/dist/core/agent-session.js.map +1 -0
  45. package/dist/core/auth-guidance.d.ts +5 -0
  46. package/dist/core/auth-guidance.d.ts.map +1 -0
  47. package/dist/core/auth-guidance.js +21 -0
  48. package/dist/core/auth-guidance.js.map +1 -0
  49. package/dist/core/auth-storage.d.ts +141 -0
  50. package/dist/core/auth-storage.d.ts.map +1 -0
  51. package/dist/core/auth-storage.js +442 -0
  52. package/dist/core/auth-storage.js.map +1 -0
  53. package/dist/core/bash-executor.d.ts +32 -0
  54. package/dist/core/bash-executor.d.ts.map +1 -0
  55. package/dist/core/bash-executor.js +111 -0
  56. package/dist/core/bash-executor.js.map +1 -0
  57. package/dist/core/compaction/branch-summarization.d.ts +88 -0
  58. package/dist/core/compaction/branch-summarization.d.ts.map +1 -0
  59. package/dist/core/compaction/branch-summarization.js +243 -0
  60. package/dist/core/compaction/branch-summarization.js.map +1 -0
  61. package/dist/core/compaction/compaction.d.ts +121 -0
  62. package/dist/core/compaction/compaction.d.ts.map +1 -0
  63. package/dist/core/compaction/compaction.js +619 -0
  64. package/dist/core/compaction/compaction.js.map +1 -0
  65. package/dist/core/compaction/index.d.ts +7 -0
  66. package/dist/core/compaction/index.d.ts.map +1 -0
  67. package/dist/core/compaction/index.js +7 -0
  68. package/dist/core/compaction/index.js.map +1 -0
  69. package/dist/core/compaction/utils.d.ts +38 -0
  70. package/dist/core/compaction/utils.d.ts.map +1 -0
  71. package/dist/core/compaction/utils.js +153 -0
  72. package/dist/core/compaction/utils.js.map +1 -0
  73. package/dist/core/defaults.d.ts +3 -0
  74. package/dist/core/defaults.d.ts.map +1 -0
  75. package/dist/core/defaults.js +2 -0
  76. package/dist/core/defaults.js.map +1 -0
  77. package/dist/core/diagnostics.d.ts +15 -0
  78. package/dist/core/diagnostics.d.ts.map +1 -0
  79. package/dist/core/diagnostics.js +2 -0
  80. package/dist/core/diagnostics.js.map +1 -0
  81. package/dist/core/event-bus.d.ts +9 -0
  82. package/dist/core/event-bus.d.ts.map +1 -0
  83. package/dist/core/event-bus.js +25 -0
  84. package/dist/core/event-bus.js.map +1 -0
  85. package/dist/core/exec.d.ts +29 -0
  86. package/dist/core/exec.d.ts.map +1 -0
  87. package/dist/core/exec.js +75 -0
  88. package/dist/core/exec.js.map +1 -0
  89. package/dist/core/export-html/ansi-to-html.d.ts +22 -0
  90. package/dist/core/export-html/ansi-to-html.d.ts.map +1 -0
  91. package/dist/core/export-html/ansi-to-html.js +249 -0
  92. package/dist/core/export-html/ansi-to-html.js.map +1 -0
  93. package/dist/core/export-html/index.d.ts +37 -0
  94. package/dist/core/export-html/index.d.ts.map +1 -0
  95. package/dist/core/export-html/index.js +226 -0
  96. package/dist/core/export-html/index.js.map +1 -0
  97. package/dist/core/export-html/template.css +1066 -0
  98. package/dist/core/export-html/template.html +55 -0
  99. package/dist/core/export-html/template.js +1851 -0
  100. package/dist/core/export-html/tool-renderer.d.ts +34 -0
  101. package/dist/core/export-html/tool-renderer.d.ts.map +1 -0
  102. package/dist/core/export-html/tool-renderer.js +108 -0
  103. package/dist/core/export-html/tool-renderer.js.map +1 -0
  104. package/dist/core/export-html/vendor/highlight.min.js +1213 -0
  105. package/dist/core/export-html/vendor/marked.min.js +6 -0
  106. package/dist/core/extensions/index.d.ts +12 -0
  107. package/dist/core/extensions/index.d.ts.map +1 -0
  108. package/dist/core/extensions/index.js +9 -0
  109. package/dist/core/extensions/index.js.map +1 -0
  110. package/dist/core/extensions/loader.d.ts +24 -0
  111. package/dist/core/extensions/loader.d.ts.map +1 -0
  112. package/dist/core/extensions/loader.js +481 -0
  113. package/dist/core/extensions/loader.js.map +1 -0
  114. package/dist/core/extensions/runner.d.ts +159 -0
  115. package/dist/core/extensions/runner.d.ts.map +1 -0
  116. package/dist/core/extensions/runner.js +830 -0
  117. package/dist/core/extensions/runner.js.map +1 -0
  118. package/dist/core/extensions/types.d.ts +1175 -0
  119. package/dist/core/extensions/types.d.ts.map +1 -0
  120. package/dist/core/extensions/types.js +45 -0
  121. package/dist/core/extensions/types.js.map +1 -0
  122. package/dist/core/extensions/wrapper.d.ts +20 -0
  123. package/dist/core/extensions/wrapper.d.ts.map +1 -0
  124. package/dist/core/extensions/wrapper.js +22 -0
  125. package/dist/core/extensions/wrapper.js.map +1 -0
  126. package/dist/core/footer-data-provider.d.ts +52 -0
  127. package/dist/core/footer-data-provider.d.ts.map +1 -0
  128. package/dist/core/footer-data-provider.js +310 -0
  129. package/dist/core/footer-data-provider.js.map +1 -0
  130. package/dist/core/http-dispatcher.d.ts +21 -0
  131. package/dist/core/http-dispatcher.d.ts.map +1 -0
  132. package/dist/core/http-dispatcher.js +48 -0
  133. package/dist/core/http-dispatcher.js.map +1 -0
  134. package/dist/core/index.d.ts +12 -0
  135. package/dist/core/index.d.ts.map +1 -0
  136. package/dist/core/index.js +12 -0
  137. package/dist/core/index.js.map +1 -0
  138. package/dist/core/keybindings.d.ts +353 -0
  139. package/dist/core/keybindings.d.ts.map +1 -0
  140. package/dist/core/keybindings.js +295 -0
  141. package/dist/core/keybindings.js.map +1 -0
  142. package/dist/core/messages.d.ts +77 -0
  143. package/dist/core/messages.d.ts.map +1 -0
  144. package/dist/core/messages.js +123 -0
  145. package/dist/core/messages.js.map +1 -0
  146. package/dist/core/model-registry.d.ts +150 -0
  147. package/dist/core/model-registry.d.ts.map +1 -0
  148. package/dist/core/model-registry.js +784 -0
  149. package/dist/core/model-registry.js.map +1 -0
  150. package/dist/core/model-resolver.d.ts +110 -0
  151. package/dist/core/model-resolver.d.ts.map +1 -0
  152. package/dist/core/model-resolver.js +495 -0
  153. package/dist/core/model-resolver.js.map +1 -0
  154. package/dist/core/output-guard.d.ts +7 -0
  155. package/dist/core/output-guard.d.ts.map +1 -0
  156. package/dist/core/output-guard.js +89 -0
  157. package/dist/core/output-guard.js.map +1 -0
  158. package/dist/core/package-manager.d.ts +204 -0
  159. package/dist/core/package-manager.d.ts.map +1 -0
  160. package/dist/core/package-manager.js +2040 -0
  161. package/dist/core/package-manager.js.map +1 -0
  162. package/dist/core/prompt-templates.d.ts +52 -0
  163. package/dist/core/prompt-templates.d.ts.map +1 -0
  164. package/dist/core/prompt-templates.js +238 -0
  165. package/dist/core/prompt-templates.js.map +1 -0
  166. package/dist/core/provider-display-names.d.ts +2 -0
  167. package/dist/core/provider-display-names.d.ts.map +1 -0
  168. package/dist/core/provider-display-names.js +33 -0
  169. package/dist/core/provider-display-names.js.map +1 -0
  170. package/dist/core/resolve-config-value.d.ts +31 -0
  171. package/dist/core/resolve-config-value.d.ts.map +1 -0
  172. package/dist/core/resolve-config-value.js +249 -0
  173. package/dist/core/resolve-config-value.js.map +1 -0
  174. package/dist/core/resource-loader.d.ts +194 -0
  175. package/dist/core/resource-loader.d.ts.map +1 -0
  176. package/dist/core/resource-loader.js +734 -0
  177. package/dist/core/resource-loader.js.map +1 -0
  178. package/dist/core/sdk.d.ts +109 -0
  179. package/dist/core/sdk.d.ts.map +1 -0
  180. package/dist/core/sdk.js +291 -0
  181. package/dist/core/sdk.js.map +1 -0
  182. package/dist/core/session-cwd.d.ts +19 -0
  183. package/dist/core/session-cwd.d.ts.map +1 -0
  184. package/dist/core/session-cwd.js +38 -0
  185. package/dist/core/session-cwd.js.map +1 -0
  186. package/dist/core/session-manager.d.ts +332 -0
  187. package/dist/core/session-manager.d.ts.map +1 -0
  188. package/dist/core/session-manager.js +1194 -0
  189. package/dist/core/session-manager.js.map +1 -0
  190. package/dist/core/settings-manager.d.ts +266 -0
  191. package/dist/core/settings-manager.d.ts.map +1 -0
  192. package/dist/core/settings-manager.js +800 -0
  193. package/dist/core/settings-manager.js.map +1 -0
  194. package/dist/core/skills.d.ts +60 -0
  195. package/dist/core/skills.d.ts.map +1 -0
  196. package/dist/core/skills.js +387 -0
  197. package/dist/core/skills.js.map +1 -0
  198. package/dist/core/slash-commands.d.ts +14 -0
  199. package/dist/core/slash-commands.d.ts.map +1 -0
  200. package/dist/core/slash-commands.js +25 -0
  201. package/dist/core/slash-commands.js.map +1 -0
  202. package/dist/core/source-info.d.ts +18 -0
  203. package/dist/core/source-info.d.ts.map +1 -0
  204. package/dist/core/source-info.js +19 -0
  205. package/dist/core/source-info.js.map +1 -0
  206. package/dist/core/system-prompt.d.ts +30 -0
  207. package/dist/core/system-prompt.d.ts.map +1 -0
  208. package/dist/core/system-prompt.js +117 -0
  209. package/dist/core/system-prompt.js.map +1 -0
  210. package/dist/core/telemetry.d.ts +3 -0
  211. package/dist/core/telemetry.d.ts.map +1 -0
  212. package/dist/core/telemetry.js +9 -0
  213. package/dist/core/telemetry.js.map +1 -0
  214. package/dist/core/timings.d.ts +8 -0
  215. package/dist/core/timings.d.ts.map +1 -0
  216. package/dist/core/timings.js +31 -0
  217. package/dist/core/timings.js.map +1 -0
  218. package/dist/core/tools/bash.d.ts +68 -0
  219. package/dist/core/tools/bash.d.ts.map +1 -0
  220. package/dist/core/tools/bash.js +337 -0
  221. package/dist/core/tools/bash.js.map +1 -0
  222. package/dist/core/tools/edit-diff.d.ts +87 -0
  223. package/dist/core/tools/edit-diff.d.ts.map +1 -0
  224. package/dist/core/tools/edit-diff.js +345 -0
  225. package/dist/core/tools/edit-diff.js.map +1 -0
  226. package/dist/core/tools/edit.d.ts +51 -0
  227. package/dist/core/tools/edit.d.ts.map +1 -0
  228. package/dist/core/tools/edit.js +287 -0
  229. package/dist/core/tools/edit.js.map +1 -0
  230. package/dist/core/tools/file-mutation-queue.d.ts +6 -0
  231. package/dist/core/tools/file-mutation-queue.d.ts.map +1 -0
  232. package/dist/core/tools/file-mutation-queue.js +52 -0
  233. package/dist/core/tools/file-mutation-queue.js.map +1 -0
  234. package/dist/core/tools/find.d.ts +35 -0
  235. package/dist/core/tools/find.d.ts.map +1 -0
  236. package/dist/core/tools/find.js +297 -0
  237. package/dist/core/tools/find.js.map +1 -0
  238. package/dist/core/tools/grep.d.ts +37 -0
  239. package/dist/core/tools/grep.d.ts.map +1 -0
  240. package/dist/core/tools/grep.js +304 -0
  241. package/dist/core/tools/grep.js.map +1 -0
  242. package/dist/core/tools/index.d.ts +40 -0
  243. package/dist/core/tools/index.d.ts.map +1 -0
  244. package/dist/core/tools/index.js +112 -0
  245. package/dist/core/tools/index.js.map +1 -0
  246. package/dist/core/tools/ls.d.ts +37 -0
  247. package/dist/core/tools/ls.d.ts.map +1 -0
  248. package/dist/core/tools/ls.js +169 -0
  249. package/dist/core/tools/ls.js.map +1 -0
  250. package/dist/core/tools/output-accumulator.d.ts +52 -0
  251. package/dist/core/tools/output-accumulator.d.ts.map +1 -0
  252. package/dist/core/tools/output-accumulator.js +184 -0
  253. package/dist/core/tools/output-accumulator.js.map +1 -0
  254. package/dist/core/tools/path-utils.d.ts +10 -0
  255. package/dist/core/tools/path-utils.d.ts.map +1 -0
  256. package/dist/core/tools/path-utils.js +99 -0
  257. package/dist/core/tools/path-utils.js.map +1 -0
  258. package/dist/core/tools/read.d.ts +35 -0
  259. package/dist/core/tools/read.d.ts.map +1 -0
  260. package/dist/core/tools/read.js +290 -0
  261. package/dist/core/tools/read.js.map +1 -0
  262. package/dist/core/tools/render-utils.d.ts +21 -0
  263. package/dist/core/tools/render-utils.d.ts.map +1 -0
  264. package/dist/core/tools/render-utils.js +49 -0
  265. package/dist/core/tools/render-utils.js.map +1 -0
  266. package/dist/core/tools/tool-definition-wrapper.d.ts +14 -0
  267. package/dist/core/tools/tool-definition-wrapper.d.ts.map +1 -0
  268. package/dist/core/tools/tool-definition-wrapper.js +34 -0
  269. package/dist/core/tools/tool-definition-wrapper.js.map +1 -0
  270. package/dist/core/tools/truncate.d.ts +70 -0
  271. package/dist/core/tools/truncate.d.ts.map +1 -0
  272. package/dist/core/tools/truncate.js +215 -0
  273. package/dist/core/tools/truncate.js.map +1 -0
  274. package/dist/core/tools/worker.d.ts +23 -0
  275. package/dist/core/tools/worker.d.ts.map +1 -0
  276. package/dist/core/tools/worker.js +150 -0
  277. package/dist/core/tools/worker.js.map +1 -0
  278. package/dist/core/tools/write.d.ts +26 -0
  279. package/dist/core/tools/write.d.ts.map +1 -0
  280. package/dist/core/tools/write.js +198 -0
  281. package/dist/core/tools/write.js.map +1 -0
  282. package/dist/core/worker-integration.d.ts +79 -0
  283. package/dist/core/worker-integration.d.ts.map +1 -0
  284. package/dist/core/worker-integration.js +108 -0
  285. package/dist/core/worker-integration.js.map +1 -0
  286. package/dist/index.d.ts +29 -0
  287. package/dist/index.d.ts.map +1 -0
  288. package/dist/index.js +42 -0
  289. package/dist/index.js.map +1 -0
  290. package/dist/main.d.ts +12 -0
  291. package/dist/main.d.ts.map +1 -0
  292. package/dist/main.js +623 -0
  293. package/dist/main.js.map +1 -0
  294. package/dist/migrations.d.ts +33 -0
  295. package/dist/migrations.d.ts.map +1 -0
  296. package/dist/migrations.js +398 -0
  297. package/dist/migrations.js.map +1 -0
  298. package/dist/modes/index.d.ts +9 -0
  299. package/dist/modes/index.d.ts.map +1 -0
  300. package/dist/modes/index.js +8 -0
  301. package/dist/modes/index.js.map +1 -0
  302. package/dist/modes/interactive/assets/clankolas.png +0 -0
  303. package/dist/modes/interactive/components/armin.d.ts +34 -0
  304. package/dist/modes/interactive/components/armin.d.ts.map +1 -0
  305. package/dist/modes/interactive/components/armin.js +333 -0
  306. package/dist/modes/interactive/components/armin.js.map +1 -0
  307. package/dist/modes/interactive/components/assistant-message.d.ts +20 -0
  308. package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -0
  309. package/dist/modes/interactive/components/assistant-message.js +121 -0
  310. package/dist/modes/interactive/components/assistant-message.js.map +1 -0
  311. package/dist/modes/interactive/components/bash-execution.d.ts +34 -0
  312. package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -0
  313. package/dist/modes/interactive/components/bash-execution.js +175 -0
  314. package/dist/modes/interactive/components/bash-execution.js.map +1 -0
  315. package/dist/modes/interactive/components/bordered-loader.d.ts +16 -0
  316. package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -0
  317. package/dist/modes/interactive/components/bordered-loader.js +54 -0
  318. package/dist/modes/interactive/components/bordered-loader.js.map +1 -0
  319. package/dist/modes/interactive/components/branch-summary-message.d.ts +16 -0
  320. package/dist/modes/interactive/components/branch-summary-message.d.ts.map +1 -0
  321. package/dist/modes/interactive/components/branch-summary-message.js +44 -0
  322. package/dist/modes/interactive/components/branch-summary-message.js.map +1 -0
  323. package/dist/modes/interactive/components/compaction-summary-message.d.ts +16 -0
  324. package/dist/modes/interactive/components/compaction-summary-message.d.ts.map +1 -0
  325. package/dist/modes/interactive/components/compaction-summary-message.js +45 -0
  326. package/dist/modes/interactive/components/compaction-summary-message.js.map +1 -0
  327. package/dist/modes/interactive/components/config-selector.d.ts +71 -0
  328. package/dist/modes/interactive/components/config-selector.d.ts.map +1 -0
  329. package/dist/modes/interactive/components/config-selector.js +506 -0
  330. package/dist/modes/interactive/components/config-selector.js.map +1 -0
  331. package/dist/modes/interactive/components/countdown-timer.d.ts +14 -0
  332. package/dist/modes/interactive/components/countdown-timer.d.ts.map +1 -0
  333. package/dist/modes/interactive/components/countdown-timer.js +33 -0
  334. package/dist/modes/interactive/components/countdown-timer.js.map +1 -0
  335. package/dist/modes/interactive/components/custom-editor.d.ts +21 -0
  336. package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -0
  337. package/dist/modes/interactive/components/custom-editor.js +70 -0
  338. package/dist/modes/interactive/components/custom-editor.js.map +1 -0
  339. package/dist/modes/interactive/components/custom-message.d.ts +20 -0
  340. package/dist/modes/interactive/components/custom-message.d.ts.map +1 -0
  341. package/dist/modes/interactive/components/custom-message.js +79 -0
  342. package/dist/modes/interactive/components/custom-message.js.map +1 -0
  343. package/dist/modes/interactive/components/daxnuts.d.ts +23 -0
  344. package/dist/modes/interactive/components/daxnuts.d.ts.map +1 -0
  345. package/dist/modes/interactive/components/daxnuts.js +140 -0
  346. package/dist/modes/interactive/components/daxnuts.js.map +1 -0
  347. package/dist/modes/interactive/components/diff.d.ts +12 -0
  348. package/dist/modes/interactive/components/diff.d.ts.map +1 -0
  349. package/dist/modes/interactive/components/diff.js +133 -0
  350. package/dist/modes/interactive/components/diff.js.map +1 -0
  351. package/dist/modes/interactive/components/dynamic-border.d.ts +15 -0
  352. package/dist/modes/interactive/components/dynamic-border.d.ts.map +1 -0
  353. package/dist/modes/interactive/components/dynamic-border.js +21 -0
  354. package/dist/modes/interactive/components/dynamic-border.js.map +1 -0
  355. package/dist/modes/interactive/components/earendil-announcement.d.ts +5 -0
  356. package/dist/modes/interactive/components/earendil-announcement.d.ts.map +1 -0
  357. package/dist/modes/interactive/components/earendil-announcement.js +40 -0
  358. package/dist/modes/interactive/components/earendil-announcement.js.map +1 -0
  359. package/dist/modes/interactive/components/extension-editor.d.ts +20 -0
  360. package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -0
  361. package/dist/modes/interactive/components/extension-editor.js +119 -0
  362. package/dist/modes/interactive/components/extension-editor.js.map +1 -0
  363. package/dist/modes/interactive/components/extension-input.d.ts +23 -0
  364. package/dist/modes/interactive/components/extension-input.d.ts.map +1 -0
  365. package/dist/modes/interactive/components/extension-input.js +61 -0
  366. package/dist/modes/interactive/components/extension-input.js.map +1 -0
  367. package/dist/modes/interactive/components/extension-selector.d.ts +26 -0
  368. package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -0
  369. package/dist/modes/interactive/components/extension-selector.js +83 -0
  370. package/dist/modes/interactive/components/extension-selector.js.map +1 -0
  371. package/dist/modes/interactive/components/footer.d.ts +28 -0
  372. package/dist/modes/interactive/components/footer.d.ts.map +1 -0
  373. package/dist/modes/interactive/components/footer.js +210 -0
  374. package/dist/modes/interactive/components/footer.js.map +1 -0
  375. package/dist/modes/interactive/components/index.d.ts +32 -0
  376. package/dist/modes/interactive/components/index.d.ts.map +1 -0
  377. package/dist/modes/interactive/components/index.js +33 -0
  378. package/dist/modes/interactive/components/index.js.map +1 -0
  379. package/dist/modes/interactive/components/keybinding-hints.d.ts +13 -0
  380. package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -0
  381. package/dist/modes/interactive/components/keybinding-hints.js +36 -0
  382. package/dist/modes/interactive/components/keybinding-hints.js.map +1 -0
  383. package/dist/modes/interactive/components/login-dialog.d.ts +52 -0
  384. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -0
  385. package/dist/modes/interactive/components/login-dialog.js +183 -0
  386. package/dist/modes/interactive/components/login-dialog.js.map +1 -0
  387. package/dist/modes/interactive/components/model-selector.d.ts +47 -0
  388. package/dist/modes/interactive/components/model-selector.d.ts.map +1 -0
  389. package/dist/modes/interactive/components/model-selector.js +278 -0
  390. package/dist/modes/interactive/components/model-selector.js.map +1 -0
  391. package/dist/modes/interactive/components/oauth-selector.d.ts +31 -0
  392. package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -0
  393. package/dist/modes/interactive/components/oauth-selector.js +165 -0
  394. package/dist/modes/interactive/components/oauth-selector.js.map +1 -0
  395. package/dist/modes/interactive/components/scoped-models-selector.d.ts +42 -0
  396. package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -0
  397. package/dist/modes/interactive/components/scoped-models-selector.js +290 -0
  398. package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -0
  399. package/dist/modes/interactive/components/session-selector-search.d.ts +23 -0
  400. package/dist/modes/interactive/components/session-selector-search.d.ts.map +1 -0
  401. package/dist/modes/interactive/components/session-selector-search.js +155 -0
  402. package/dist/modes/interactive/components/session-selector-search.js.map +1 -0
  403. package/dist/modes/interactive/components/session-selector.d.ts +96 -0
  404. package/dist/modes/interactive/components/session-selector.d.ts.map +1 -0
  405. package/dist/modes/interactive/components/session-selector.js +861 -0
  406. package/dist/modes/interactive/components/session-selector.js.map +1 -0
  407. package/dist/modes/interactive/components/settings-selector.d.ts +69 -0
  408. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -0
  409. package/dist/modes/interactive/components/settings-selector.js +390 -0
  410. package/dist/modes/interactive/components/settings-selector.js.map +1 -0
  411. package/dist/modes/interactive/components/show-images-selector.d.ts +10 -0
  412. package/dist/modes/interactive/components/show-images-selector.d.ts.map +1 -0
  413. package/dist/modes/interactive/components/show-images-selector.js +39 -0
  414. package/dist/modes/interactive/components/show-images-selector.js.map +1 -0
  415. package/dist/modes/interactive/components/skill-invocation-message.d.ts +17 -0
  416. package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -0
  417. package/dist/modes/interactive/components/skill-invocation-message.js +47 -0
  418. package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -0
  419. package/dist/modes/interactive/components/theme-selector.d.ts +11 -0
  420. package/dist/modes/interactive/components/theme-selector.d.ts.map +1 -0
  421. package/dist/modes/interactive/components/theme-selector.js +50 -0
  422. package/dist/modes/interactive/components/theme-selector.js.map +1 -0
  423. package/dist/modes/interactive/components/thinking-selector.d.ts +11 -0
  424. package/dist/modes/interactive/components/thinking-selector.d.ts.map +1 -0
  425. package/dist/modes/interactive/components/thinking-selector.js +51 -0
  426. package/dist/modes/interactive/components/thinking-selector.js.map +1 -0
  427. package/dist/modes/interactive/components/tool-execution.d.ts +63 -0
  428. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -0
  429. package/dist/modes/interactive/components/tool-execution.js +295 -0
  430. package/dist/modes/interactive/components/tool-execution.js.map +1 -0
  431. package/dist/modes/interactive/components/tree-selector.d.ts +89 -0
  432. package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -0
  433. package/dist/modes/interactive/components/tree-selector.js +1093 -0
  434. package/dist/modes/interactive/components/tree-selector.js.map +1 -0
  435. package/dist/modes/interactive/components/user-message-selector.d.ts +30 -0
  436. package/dist/modes/interactive/components/user-message-selector.d.ts.map +1 -0
  437. package/dist/modes/interactive/components/user-message-selector.js +114 -0
  438. package/dist/modes/interactive/components/user-message-selector.js.map +1 -0
  439. package/dist/modes/interactive/components/user-message.d.ts +10 -0
  440. package/dist/modes/interactive/components/user-message.d.ts.map +1 -0
  441. package/dist/modes/interactive/components/user-message.js +29 -0
  442. package/dist/modes/interactive/components/user-message.js.map +1 -0
  443. package/dist/modes/interactive/components/visual-truncate.d.ts +24 -0
  444. package/dist/modes/interactive/components/visual-truncate.d.ts.map +1 -0
  445. package/dist/modes/interactive/components/visual-truncate.js +33 -0
  446. package/dist/modes/interactive/components/visual-truncate.js.map +1 -0
  447. package/dist/modes/interactive/components/worker-status.d.ts +23 -0
  448. package/dist/modes/interactive/components/worker-status.d.ts.map +1 -0
  449. package/dist/modes/interactive/components/worker-status.js +77 -0
  450. package/dist/modes/interactive/components/worker-status.js.map +1 -0
  451. package/dist/modes/interactive/interactive-mode.d.ts +370 -0
  452. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -0
  453. package/dist/modes/interactive/interactive-mode.js +4674 -0
  454. package/dist/modes/interactive/interactive-mode.js.map +1 -0
  455. package/dist/modes/interactive/theme/dark.json +86 -0
  456. package/dist/modes/interactive/theme/light.json +85 -0
  457. package/dist/modes/interactive/theme/theme-schema.json +335 -0
  458. package/dist/modes/interactive/theme/theme.d.ts +100 -0
  459. package/dist/modes/interactive/theme/theme.d.ts.map +1 -0
  460. package/dist/modes/interactive/theme/theme.js +1034 -0
  461. package/dist/modes/interactive/theme/theme.js.map +1 -0
  462. package/dist/modes/interactive/worker-ui-integration.d.ts +102 -0
  463. package/dist/modes/interactive/worker-ui-integration.d.ts.map +1 -0
  464. package/dist/modes/interactive/worker-ui-integration.js +147 -0
  465. package/dist/modes/interactive/worker-ui-integration.js.map +1 -0
  466. package/dist/modes/print-mode.d.ts +28 -0
  467. package/dist/modes/print-mode.d.ts.map +1 -0
  468. package/dist/modes/print-mode.js +131 -0
  469. package/dist/modes/print-mode.js.map +1 -0
  470. package/dist/modes/rpc/jsonl.d.ts +17 -0
  471. package/dist/modes/rpc/jsonl.d.ts.map +1 -0
  472. package/dist/modes/rpc/jsonl.js +49 -0
  473. package/dist/modes/rpc/jsonl.js.map +1 -0
  474. package/dist/modes/rpc/rpc-client.d.ts +227 -0
  475. package/dist/modes/rpc/rpc-client.d.ts.map +1 -0
  476. package/dist/modes/rpc/rpc-client.js +467 -0
  477. package/dist/modes/rpc/rpc-client.js.map +1 -0
  478. package/dist/modes/rpc/rpc-mode.d.ts +20 -0
  479. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -0
  480. package/dist/modes/rpc/rpc-mode.js +615 -0
  481. package/dist/modes/rpc/rpc-mode.js.map +1 -0
  482. package/dist/modes/rpc/rpc-types.d.ts +420 -0
  483. package/dist/modes/rpc/rpc-types.d.ts.map +1 -0
  484. package/dist/modes/rpc/rpc-types.js +8 -0
  485. package/dist/modes/rpc/rpc-types.js.map +1 -0
  486. package/dist/package-manager-cli.d.ts +4 -0
  487. package/dist/package-manager-cli.d.ts.map +1 -0
  488. package/dist/package-manager-cli.js +515 -0
  489. package/dist/package-manager-cli.js.map +1 -0
  490. package/dist/utils/ansi.d.ts +2 -0
  491. package/dist/utils/ansi.d.ts.map +1 -0
  492. package/dist/utils/ansi.js +52 -0
  493. package/dist/utils/ansi.js.map +1 -0
  494. package/dist/utils/changelog.d.ts +21 -0
  495. package/dist/utils/changelog.d.ts.map +1 -0
  496. package/dist/utils/changelog.js +87 -0
  497. package/dist/utils/changelog.js.map +1 -0
  498. package/dist/utils/child-process.d.ts +15 -0
  499. package/dist/utils/child-process.d.ts.map +1 -0
  500. package/dist/utils/child-process.js +88 -0
  501. package/dist/utils/child-process.js.map +1 -0
  502. package/dist/utils/clipboard-image.d.ts +11 -0
  503. package/dist/utils/clipboard-image.d.ts.map +1 -0
  504. package/dist/utils/clipboard-image.js +245 -0
  505. package/dist/utils/clipboard-image.js.map +1 -0
  506. package/dist/utils/clipboard-native.d.ts +10 -0
  507. package/dist/utils/clipboard-native.d.ts.map +1 -0
  508. package/dist/utils/clipboard-native.js +20 -0
  509. package/dist/utils/clipboard-native.js.map +1 -0
  510. package/dist/utils/clipboard.d.ts +2 -0
  511. package/dist/utils/clipboard.d.ts.map +1 -0
  512. package/dist/utils/clipboard.js +117 -0
  513. package/dist/utils/clipboard.js.map +1 -0
  514. package/dist/utils/deprecation.d.ts +4 -0
  515. package/dist/utils/deprecation.d.ts.map +1 -0
  516. package/dist/utils/deprecation.js +13 -0
  517. package/dist/utils/deprecation.js.map +1 -0
  518. package/dist/utils/exif-orientation.d.ts +5 -0
  519. package/dist/utils/exif-orientation.d.ts.map +1 -0
  520. package/dist/utils/exif-orientation.js +158 -0
  521. package/dist/utils/exif-orientation.js.map +1 -0
  522. package/dist/utils/frontmatter.d.ts +8 -0
  523. package/dist/utils/frontmatter.d.ts.map +1 -0
  524. package/dist/utils/frontmatter.js +26 -0
  525. package/dist/utils/frontmatter.js.map +1 -0
  526. package/dist/utils/fs-watch.d.ts +5 -0
  527. package/dist/utils/fs-watch.d.ts.map +1 -0
  528. package/dist/utils/fs-watch.js +25 -0
  529. package/dist/utils/fs-watch.js.map +1 -0
  530. package/dist/utils/git.d.ts +26 -0
  531. package/dist/utils/git.d.ts.map +1 -0
  532. package/dist/utils/git.js +163 -0
  533. package/dist/utils/git.js.map +1 -0
  534. package/dist/utils/html.d.ts +7 -0
  535. package/dist/utils/html.d.ts.map +1 -0
  536. package/dist/utils/html.js +40 -0
  537. package/dist/utils/html.js.map +1 -0
  538. package/dist/utils/image-convert.d.ts +9 -0
  539. package/dist/utils/image-convert.d.ts.map +1 -0
  540. package/dist/utils/image-convert.js +39 -0
  541. package/dist/utils/image-convert.js.map +1 -0
  542. package/dist/utils/image-resize-core.d.ts +30 -0
  543. package/dist/utils/image-resize-core.d.ts.map +1 -0
  544. package/dist/utils/image-resize-core.js +124 -0
  545. package/dist/utils/image-resize-core.js.map +1 -0
  546. package/dist/utils/image-resize-worker.d.ts +2 -0
  547. package/dist/utils/image-resize-worker.d.ts.map +1 -0
  548. package/dist/utils/image-resize-worker.js +31 -0
  549. package/dist/utils/image-resize-worker.js.map +1 -0
  550. package/dist/utils/image-resize.d.ts +16 -0
  551. package/dist/utils/image-resize.d.ts.map +1 -0
  552. package/dist/utils/image-resize.js +97 -0
  553. package/dist/utils/image-resize.js.map +1 -0
  554. package/dist/utils/json.d.ts +3 -0
  555. package/dist/utils/json.d.ts.map +1 -0
  556. package/dist/utils/json.js +7 -0
  557. package/dist/utils/json.js.map +1 -0
  558. package/dist/utils/leaf-user-agent.d.ts +2 -0
  559. package/dist/utils/leaf-user-agent.d.ts.map +1 -0
  560. package/dist/utils/leaf-user-agent.js +5 -0
  561. package/dist/utils/leaf-user-agent.js.map +1 -0
  562. package/dist/utils/mime.d.ts +3 -0
  563. package/dist/utils/mime.d.ts.map +1 -0
  564. package/dist/utils/mime.js +69 -0
  565. package/dist/utils/mime.js.map +1 -0
  566. package/dist/utils/paths.d.ts +31 -0
  567. package/dist/utils/paths.d.ts.map +1 -0
  568. package/dist/utils/paths.js +92 -0
  569. package/dist/utils/paths.js.map +1 -0
  570. package/dist/utils/photon.d.ts +21 -0
  571. package/dist/utils/photon.d.ts.map +1 -0
  572. package/dist/utils/photon.js +121 -0
  573. package/dist/utils/photon.js.map +1 -0
  574. package/dist/utils/shell.d.ts +30 -0
  575. package/dist/utils/shell.d.ts.map +1 -0
  576. package/dist/utils/shell.js +195 -0
  577. package/dist/utils/shell.js.map +1 -0
  578. package/dist/utils/sleep.d.ts +5 -0
  579. package/dist/utils/sleep.d.ts.map +1 -0
  580. package/dist/utils/sleep.js +17 -0
  581. package/dist/utils/sleep.js.map +1 -0
  582. package/dist/utils/syntax-highlight.d.ts +12 -0
  583. package/dist/utils/syntax-highlight.d.ts.map +1 -0
  584. package/dist/utils/syntax-highlight.js +118 -0
  585. package/dist/utils/syntax-highlight.js.map +1 -0
  586. package/dist/utils/tools-manager.d.ts +3 -0
  587. package/dist/utils/tools-manager.d.ts.map +1 -0
  588. package/dist/utils/tools-manager.js +328 -0
  589. package/dist/utils/tools-manager.js.map +1 -0
  590. package/dist/utils/version-check.d.ts +15 -0
  591. package/dist/utils/version-check.d.ts.map +1 -0
  592. package/dist/utils/version-check.js +82 -0
  593. package/dist/utils/version-check.js.map +1 -0
  594. package/dist/utils/windows-self-update.d.ts +3 -0
  595. package/dist/utils/windows-self-update.d.ts.map +1 -0
  596. package/dist/utils/windows-self-update.js +77 -0
  597. package/dist/utils/windows-self-update.js.map +1 -0
  598. package/package.json +100 -0
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Shared truncation utilities for tool outputs.
3
+ *
4
+ * Truncation is based on two independent limits - whichever is hit first wins:
5
+ * - Line limit (default: 2000 lines)
6
+ * - Byte limit (default: 50KB)
7
+ *
8
+ * Never returns partial lines (except bash tail truncation edge case).
9
+ */
10
+ export declare const DEFAULT_MAX_LINES = 2000;
11
+ export declare const DEFAULT_MAX_BYTES: number;
12
+ export declare const GREP_MAX_LINE_LENGTH = 500;
13
+ export interface TruncationResult {
14
+ /** The truncated content */
15
+ content: string;
16
+ /** Whether truncation occurred */
17
+ truncated: boolean;
18
+ /** Which limit was hit: "lines", "bytes", or null if not truncated */
19
+ truncatedBy: "lines" | "bytes" | null;
20
+ /** Total number of lines in the original content */
21
+ totalLines: number;
22
+ /** Total number of bytes in the original content */
23
+ totalBytes: number;
24
+ /** Number of complete lines in the truncated output */
25
+ outputLines: number;
26
+ /** Number of bytes in the truncated output */
27
+ outputBytes: number;
28
+ /** Whether the last line was partially truncated (only for tail truncation edge case) */
29
+ lastLinePartial: boolean;
30
+ /** Whether the first line exceeded the byte limit (for head truncation) */
31
+ firstLineExceedsLimit: boolean;
32
+ /** The max lines limit that was applied */
33
+ maxLines: number;
34
+ /** The max bytes limit that was applied */
35
+ maxBytes: number;
36
+ }
37
+ export interface TruncationOptions {
38
+ /** Maximum number of lines (default: 2000) */
39
+ maxLines?: number;
40
+ /** Maximum number of bytes (default: 50KB) */
41
+ maxBytes?: number;
42
+ }
43
+ /**
44
+ * Format bytes as human-readable size.
45
+ */
46
+ export declare function formatSize(bytes: number): string;
47
+ /**
48
+ * Truncate content from the head (keep first N lines/bytes).
49
+ * Suitable for file reads where you want to see the beginning.
50
+ *
51
+ * Never returns partial lines. If first line exceeds byte limit,
52
+ * returns empty content with firstLineExceedsLimit=true.
53
+ */
54
+ export declare function truncateHead(content: string, options?: TruncationOptions): TruncationResult;
55
+ /**
56
+ * Truncate content from the tail (keep last N lines/bytes).
57
+ * Suitable for bash output where you want to see the end (errors, final results).
58
+ *
59
+ * May return partial first line if the last line of original content exceeds byte limit.
60
+ */
61
+ export declare function truncateTail(content: string, options?: TruncationOptions): TruncationResult;
62
+ /**
63
+ * Truncate a single line to max characters, adding [truncated] suffix.
64
+ * Used for grep match lines.
65
+ */
66
+ export declare function truncateLine(line: string, maxChars?: number): {
67
+ text: string;
68
+ wasTruncated: boolean;
69
+ };
70
+ //# sourceMappingURL=truncate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"truncate.d.ts","sourceRoot":"","sources":["../../../src/core/tools/truncate.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,eAAO,MAAM,iBAAiB,OAAO,CAAC;AACtC,eAAO,MAAM,iBAAiB,QAAY,CAAC;AAC3C,eAAO,MAAM,oBAAoB,MAAM,CAAC;AAExC,MAAM,WAAW,gBAAgB;IAChC,4BAA4B;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,kCAAkC;IAClC,SAAS,EAAE,OAAO,CAAC;IACnB,sEAAsE;IACtE,WAAW,EAAE,OAAO,GAAG,OAAO,GAAG,IAAI,CAAC;IACtC,oDAAoD;IACpD,UAAU,EAAE,MAAM,CAAC;IACnB,oDAAoD;IACpD,UAAU,EAAE,MAAM,CAAC;IACnB,uDAAuD;IACvD,WAAW,EAAE,MAAM,CAAC;IACpB,8CAA8C;IAC9C,WAAW,EAAE,MAAM,CAAC;IACpB,yFAAyF;IACzF,eAAe,EAAE,OAAO,CAAC;IACzB,2EAA2E;IAC3E,qBAAqB,EAAE,OAAO,CAAC;IAC/B,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAC;IACjB,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,iBAAiB;IACjC,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAaD;;GAEG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAQhD;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,gBAAgB,CAkF/F;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,gBAAgB,CAyE/F;AAuBD;;;GAGG;AACH,wBAAgB,YAAY,CAC3B,IAAI,EAAE,MAAM,EACZ,QAAQ,GAAE,MAA6B,GACrC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,OAAO,CAAA;CAAE,CAKzC","sourcesContent":["/**\n * Shared truncation utilities for tool outputs.\n *\n * Truncation is based on two independent limits - whichever is hit first wins:\n * - Line limit (default: 2000 lines)\n * - Byte limit (default: 50KB)\n *\n * Never returns partial lines (except bash tail truncation edge case).\n */\n\nexport const DEFAULT_MAX_LINES = 2000;\nexport const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB\nexport const GREP_MAX_LINE_LENGTH = 500; // Max chars per grep match line\n\nexport interface TruncationResult {\n\t/** The truncated content */\n\tcontent: string;\n\t/** Whether truncation occurred */\n\ttruncated: boolean;\n\t/** Which limit was hit: \"lines\", \"bytes\", or null if not truncated */\n\ttruncatedBy: \"lines\" | \"bytes\" | null;\n\t/** Total number of lines in the original content */\n\ttotalLines: number;\n\t/** Total number of bytes in the original content */\n\ttotalBytes: number;\n\t/** Number of complete lines in the truncated output */\n\toutputLines: number;\n\t/** Number of bytes in the truncated output */\n\toutputBytes: number;\n\t/** Whether the last line was partially truncated (only for tail truncation edge case) */\n\tlastLinePartial: boolean;\n\t/** Whether the first line exceeded the byte limit (for head truncation) */\n\tfirstLineExceedsLimit: boolean;\n\t/** The max lines limit that was applied */\n\tmaxLines: number;\n\t/** The max bytes limit that was applied */\n\tmaxBytes: number;\n}\n\nexport interface TruncationOptions {\n\t/** Maximum number of lines (default: 2000) */\n\tmaxLines?: number;\n\t/** Maximum number of bytes (default: 50KB) */\n\tmaxBytes?: number;\n}\n\nfunction splitLinesForCounting(content: string): string[] {\n\tif (content.length === 0) {\n\t\treturn [];\n\t}\n\tconst lines = content.split(\"\\n\");\n\tif (content.endsWith(\"\\n\")) {\n\t\tlines.pop();\n\t}\n\treturn lines;\n}\n\n/**\n * Format bytes as human-readable size.\n */\nexport function formatSize(bytes: number): string {\n\tif (bytes < 1024) {\n\t\treturn `${bytes}B`;\n\t} else if (bytes < 1024 * 1024) {\n\t\treturn `${(bytes / 1024).toFixed(1)}KB`;\n\t} else {\n\t\treturn `${(bytes / (1024 * 1024)).toFixed(1)}MB`;\n\t}\n}\n\n/**\n * Truncate content from the head (keep first N lines/bytes).\n * Suitable for file reads where you want to see the beginning.\n *\n * Never returns partial lines. If first line exceeds byte limit,\n * returns empty content with firstLineExceedsLimit=true.\n */\nexport function truncateHead(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = Buffer.byteLength(content, \"utf-8\");\n\tconst lines = splitLinesForCounting(content);\n\tconst totalLines = lines.length;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Check if first line alone exceeds byte limit\n\tconst firstLineBytes = Buffer.byteLength(lines[0], \"utf-8\");\n\tif (firstLineBytes > maxBytes) {\n\t\treturn {\n\t\t\tcontent: \"\",\n\t\t\ttruncated: true,\n\t\t\ttruncatedBy: \"bytes\",\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: 0,\n\t\t\toutputBytes: 0,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: true,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Collect complete lines that fit\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\n\tfor (let i = 0; i < lines.length && i < maxLines; i++) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = Buffer.byteLength(line, \"utf-8\") + (i > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.push(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = Buffer.byteLength(outputContent, \"utf-8\");\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial: false,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n\n/**\n * Truncate content from the tail (keep last N lines/bytes).\n * Suitable for bash output where you want to see the end (errors, final results).\n *\n * May return partial first line if the last line of original content exceeds byte limit.\n */\nexport function truncateTail(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = Buffer.byteLength(content, \"utf-8\");\n\tconst lines = splitLinesForCounting(content);\n\tconst totalLines = lines.length;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Work backwards from the end\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\tlet lastLinePartial = false;\n\n\tfor (let i = lines.length - 1; i >= 0 && outputLinesArr.length < maxLines; i--) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = Buffer.byteLength(line, \"utf-8\") + (outputLinesArr.length > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\t// Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes,\n\t\t\t// take the end of the line (partial)\n\t\t\tif (outputLinesArr.length === 0) {\n\t\t\t\tconst truncatedLine = truncateStringToBytesFromEnd(line, maxBytes);\n\t\t\t\toutputLinesArr.unshift(truncatedLine);\n\t\t\t\toutputBytesCount = Buffer.byteLength(truncatedLine, \"utf-8\");\n\t\t\t\tlastLinePartial = true;\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.unshift(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = Buffer.byteLength(outputContent, \"utf-8\");\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n\n/**\n * Truncate a string to fit within a byte limit (from the end).\n * Handles multi-byte UTF-8 characters correctly.\n */\nfunction truncateStringToBytesFromEnd(str: string, maxBytes: number): string {\n\tconst buf = Buffer.from(str, \"utf-8\");\n\tif (buf.length <= maxBytes) {\n\t\treturn str;\n\t}\n\n\t// Start from the end, skip maxBytes back\n\tlet start = buf.length - maxBytes;\n\n\t// Find a valid UTF-8 boundary (start of a character)\n\twhile (start < buf.length && (buf[start] & 0xc0) === 0x80) {\n\t\tstart++;\n\t}\n\n\treturn buf.slice(start).toString(\"utf-8\");\n}\n\n/**\n * Truncate a single line to max characters, adding [truncated] suffix.\n * Used for grep match lines.\n */\nexport function truncateLine(\n\tline: string,\n\tmaxChars: number = GREP_MAX_LINE_LENGTH,\n): { text: string; wasTruncated: boolean } {\n\tif (line.length <= maxChars) {\n\t\treturn { text: line, wasTruncated: false };\n\t}\n\treturn { text: `${line.slice(0, maxChars)}... [truncated]`, wasTruncated: true };\n}\n"]}
@@ -0,0 +1,215 @@
1
+ /**
2
+ * Shared truncation utilities for tool outputs.
3
+ *
4
+ * Truncation is based on two independent limits - whichever is hit first wins:
5
+ * - Line limit (default: 2000 lines)
6
+ * - Byte limit (default: 50KB)
7
+ *
8
+ * Never returns partial lines (except bash tail truncation edge case).
9
+ */
10
+ export const DEFAULT_MAX_LINES = 2000;
11
+ export const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB
12
+ export const GREP_MAX_LINE_LENGTH = 500; // Max chars per grep match line
13
+ function splitLinesForCounting(content) {
14
+ if (content.length === 0) {
15
+ return [];
16
+ }
17
+ const lines = content.split("\n");
18
+ if (content.endsWith("\n")) {
19
+ lines.pop();
20
+ }
21
+ return lines;
22
+ }
23
+ /**
24
+ * Format bytes as human-readable size.
25
+ */
26
+ export function formatSize(bytes) {
27
+ if (bytes < 1024) {
28
+ return `${bytes}B`;
29
+ }
30
+ else if (bytes < 1024 * 1024) {
31
+ return `${(bytes / 1024).toFixed(1)}KB`;
32
+ }
33
+ else {
34
+ return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
35
+ }
36
+ }
37
+ /**
38
+ * Truncate content from the head (keep first N lines/bytes).
39
+ * Suitable for file reads where you want to see the beginning.
40
+ *
41
+ * Never returns partial lines. If first line exceeds byte limit,
42
+ * returns empty content with firstLineExceedsLimit=true.
43
+ */
44
+ export function truncateHead(content, options = {}) {
45
+ const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
46
+ const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
47
+ const totalBytes = Buffer.byteLength(content, "utf-8");
48
+ const lines = splitLinesForCounting(content);
49
+ const totalLines = lines.length;
50
+ // Check if no truncation needed
51
+ if (totalLines <= maxLines && totalBytes <= maxBytes) {
52
+ return {
53
+ content,
54
+ truncated: false,
55
+ truncatedBy: null,
56
+ totalLines,
57
+ totalBytes,
58
+ outputLines: totalLines,
59
+ outputBytes: totalBytes,
60
+ lastLinePartial: false,
61
+ firstLineExceedsLimit: false,
62
+ maxLines,
63
+ maxBytes,
64
+ };
65
+ }
66
+ // Check if first line alone exceeds byte limit
67
+ const firstLineBytes = Buffer.byteLength(lines[0], "utf-8");
68
+ if (firstLineBytes > maxBytes) {
69
+ return {
70
+ content: "",
71
+ truncated: true,
72
+ truncatedBy: "bytes",
73
+ totalLines,
74
+ totalBytes,
75
+ outputLines: 0,
76
+ outputBytes: 0,
77
+ lastLinePartial: false,
78
+ firstLineExceedsLimit: true,
79
+ maxLines,
80
+ maxBytes,
81
+ };
82
+ }
83
+ // Collect complete lines that fit
84
+ const outputLinesArr = [];
85
+ let outputBytesCount = 0;
86
+ let truncatedBy = "lines";
87
+ for (let i = 0; i < lines.length && i < maxLines; i++) {
88
+ const line = lines[i];
89
+ const lineBytes = Buffer.byteLength(line, "utf-8") + (i > 0 ? 1 : 0); // +1 for newline
90
+ if (outputBytesCount + lineBytes > maxBytes) {
91
+ truncatedBy = "bytes";
92
+ break;
93
+ }
94
+ outputLinesArr.push(line);
95
+ outputBytesCount += lineBytes;
96
+ }
97
+ // If we exited due to line limit
98
+ if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
99
+ truncatedBy = "lines";
100
+ }
101
+ const outputContent = outputLinesArr.join("\n");
102
+ const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8");
103
+ return {
104
+ content: outputContent,
105
+ truncated: true,
106
+ truncatedBy,
107
+ totalLines,
108
+ totalBytes,
109
+ outputLines: outputLinesArr.length,
110
+ outputBytes: finalOutputBytes,
111
+ lastLinePartial: false,
112
+ firstLineExceedsLimit: false,
113
+ maxLines,
114
+ maxBytes,
115
+ };
116
+ }
117
+ /**
118
+ * Truncate content from the tail (keep last N lines/bytes).
119
+ * Suitable for bash output where you want to see the end (errors, final results).
120
+ *
121
+ * May return partial first line if the last line of original content exceeds byte limit.
122
+ */
123
+ export function truncateTail(content, options = {}) {
124
+ const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
125
+ const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
126
+ const totalBytes = Buffer.byteLength(content, "utf-8");
127
+ const lines = splitLinesForCounting(content);
128
+ const totalLines = lines.length;
129
+ // Check if no truncation needed
130
+ if (totalLines <= maxLines && totalBytes <= maxBytes) {
131
+ return {
132
+ content,
133
+ truncated: false,
134
+ truncatedBy: null,
135
+ totalLines,
136
+ totalBytes,
137
+ outputLines: totalLines,
138
+ outputBytes: totalBytes,
139
+ lastLinePartial: false,
140
+ firstLineExceedsLimit: false,
141
+ maxLines,
142
+ maxBytes,
143
+ };
144
+ }
145
+ // Work backwards from the end
146
+ const outputLinesArr = [];
147
+ let outputBytesCount = 0;
148
+ let truncatedBy = "lines";
149
+ let lastLinePartial = false;
150
+ for (let i = lines.length - 1; i >= 0 && outputLinesArr.length < maxLines; i--) {
151
+ const line = lines[i];
152
+ const lineBytes = Buffer.byteLength(line, "utf-8") + (outputLinesArr.length > 0 ? 1 : 0); // +1 for newline
153
+ if (outputBytesCount + lineBytes > maxBytes) {
154
+ truncatedBy = "bytes";
155
+ // Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes,
156
+ // take the end of the line (partial)
157
+ if (outputLinesArr.length === 0) {
158
+ const truncatedLine = truncateStringToBytesFromEnd(line, maxBytes);
159
+ outputLinesArr.unshift(truncatedLine);
160
+ outputBytesCount = Buffer.byteLength(truncatedLine, "utf-8");
161
+ lastLinePartial = true;
162
+ }
163
+ break;
164
+ }
165
+ outputLinesArr.unshift(line);
166
+ outputBytesCount += lineBytes;
167
+ }
168
+ // If we exited due to line limit
169
+ if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
170
+ truncatedBy = "lines";
171
+ }
172
+ const outputContent = outputLinesArr.join("\n");
173
+ const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8");
174
+ return {
175
+ content: outputContent,
176
+ truncated: true,
177
+ truncatedBy,
178
+ totalLines,
179
+ totalBytes,
180
+ outputLines: outputLinesArr.length,
181
+ outputBytes: finalOutputBytes,
182
+ lastLinePartial,
183
+ firstLineExceedsLimit: false,
184
+ maxLines,
185
+ maxBytes,
186
+ };
187
+ }
188
+ /**
189
+ * Truncate a string to fit within a byte limit (from the end).
190
+ * Handles multi-byte UTF-8 characters correctly.
191
+ */
192
+ function truncateStringToBytesFromEnd(str, maxBytes) {
193
+ const buf = Buffer.from(str, "utf-8");
194
+ if (buf.length <= maxBytes) {
195
+ return str;
196
+ }
197
+ // Start from the end, skip maxBytes back
198
+ let start = buf.length - maxBytes;
199
+ // Find a valid UTF-8 boundary (start of a character)
200
+ while (start < buf.length && (buf[start] & 0xc0) === 0x80) {
201
+ start++;
202
+ }
203
+ return buf.slice(start).toString("utf-8");
204
+ }
205
+ /**
206
+ * Truncate a single line to max characters, adding [truncated] suffix.
207
+ * Used for grep match lines.
208
+ */
209
+ export function truncateLine(line, maxChars = GREP_MAX_LINE_LENGTH) {
210
+ if (line.length <= maxChars) {
211
+ return { text: line, wasTruncated: false };
212
+ }
213
+ return { text: `${line.slice(0, maxChars)}... [truncated]`, wasTruncated: true };
214
+ }
215
+ //# sourceMappingURL=truncate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"truncate.js","sourceRoot":"","sources":["../../../src/core/tools/truncate.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,CAAC;AACtC,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,OAAO;AACnD,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAAG,CAAC,CAAC,gCAAgC;AAkCzE,SAAS,qBAAqB,CAAC,OAAe,EAAY;IACzD,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,EAAE,CAAC;IACX,CAAC;IACD,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAClC,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QAC5B,KAAK,CAAC,GAAG,EAAE,CAAC;IACb,CAAC;IACD,OAAO,KAAK,CAAC;AAAA,CACb;AAED;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,KAAa,EAAU;IACjD,IAAI,KAAK,GAAG,IAAI,EAAE,CAAC;QAClB,OAAO,GAAG,KAAK,GAAG,CA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B,eAAe;QACf,qBAAqB,EAAE,KAAK;QAC5B,QAAQ;QACR,QAAQ;KACR,CAAC;AAAA,CACF;AAED;;;GAGG;AACH,SAAS,4BAA4B,CAAC,GAAW,EAAE,QAAgB,EAAU;IAC5E,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IACtC,IAAI,GAAG,CAAC,MAAM,IAAI,QAAQ,EAAE,CAAC;QAC5B,OAAO,GAAG,CAAC;IACZ,CAAC;IAED,yCAAyC;IACzC,IAAI,KAAK,GAAG,GAAG,CAAC,MAAM,GAAG,QAAQ,CAAC;IAElC,qDAAqD;IACrD,OAAO,KAAK,GAAG,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;QAC3D,KAAK,EAAE,CAAC;IACT,CAAC;IAED,OAAO,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;AAAA,CAC1C;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY,CAC3B,IAAY,EACZ,QAAQ,GAAW,oBAAoB,EACG;IAC1C,IAAI,IAAI,CAAC,MAAM,IAAI,QAAQ,EAAE,CAAC;QAC7B,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC;IAC5C,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,iBAAiB,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC;AAAA,CACjF","sourcesContent":["/**\n * Shared truncation utilities for tool outputs.\n *\n * Truncation is based on two independent limits - whichever is hit first wins:\n * - Line limit (default: 2000 lines)\n * - Byte limit (default: 50KB)\n *\n * Never returns partial lines (except bash tail truncation edge case).\n */\n\nexport const DEFAULT_MAX_LINES = 2000;\nexport const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB\nexport const GREP_MAX_LINE_LENGTH = 500; // Max chars per grep match line\n\nexport interface TruncationResult {\n\t/** The truncated content */\n\tcontent: string;\n\t/** Whether truncation occurred */\n\ttruncated: boolean;\n\t/** Which limit was hit: \"lines\", \"bytes\", or null if not truncated */\n\ttruncatedBy: \"lines\" | \"bytes\" | null;\n\t/** Total number of lines in the original content */\n\ttotalLines: number;\n\t/** Total number of bytes in the original content */\n\ttotalBytes: number;\n\t/** Number of complete lines in the truncated output */\n\toutputLines: number;\n\t/** Number of bytes in the truncated output */\n\toutputBytes: number;\n\t/** Whether the last line was partially truncated (only for tail truncation edge case) */\n\tlastLinePartial: boolean;\n\t/** Whether the first line exceeded the byte limit (for head truncation) */\n\tfirstLineExceedsLimit: boolean;\n\t/** The max lines limit that was applied */\n\tmaxLines: number;\n\t/** The max bytes limit that was applied */\n\tmaxBytes: number;\n}\n\nexport interface TruncationOptions {\n\t/** Maximum number of lines (default: 2000) */\n\tmaxLines?: number;\n\t/** Maximum number of bytes (default: 50KB) */\n\tmaxBytes?: number;\n}\n\nfunction splitLinesForCounting(content: string): string[] {\n\tif (content.length === 0) {\n\t\treturn [];\n\t}\n\tconst lines = content.split(\"\\n\");\n\tif (content.endsWith(\"\\n\")) {\n\t\tlines.pop();\n\t}\n\treturn lines;\n}\n\n/**\n * Format bytes as human-readable size.\n */\nexport function formatSize(bytes: number): string {\n\tif (bytes < 1024) {\n\t\treturn `${bytes}B`;\n\t} else if (bytes < 1024 * 1024) {\n\t\treturn `${(bytes / 1024).toFixed(1)}KB`;\n\t} else {\n\t\treturn `${(bytes / (1024 * 1024)).toFixed(1)}MB`;\n\t}\n}\n\n/**\n * Truncate content from the head (keep first N lines/bytes).\n * Suitable for file reads where you want to see the beginning.\n *\n * Never returns partial lines. If first line exceeds byte limit,\n * returns empty content with firstLineExceedsLimit=true.\n */\nexport function truncateHead(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = Buffer.byteLength(content, \"utf-8\");\n\tconst lines = splitLinesForCounting(content);\n\tconst totalLines = lines.length;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Check if first line alone exceeds byte limit\n\tconst firstLineBytes = Buffer.byteLength(lines[0], \"utf-8\");\n\tif (firstLineBytes > maxBytes) {\n\t\treturn {\n\t\t\tcontent: \"\",\n\t\t\ttruncated: true,\n\t\t\ttruncatedBy: \"bytes\",\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: 0,\n\t\t\toutputBytes: 0,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: true,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Collect complete lines that fit\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\n\tfor (let i = 0; i < lines.length && i < maxLines; i++) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = Buffer.byteLength(line, \"utf-8\") + (i > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.push(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = Buffer.byteLength(outputContent, \"utf-8\");\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial: false,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n\n/**\n * Truncate content from the tail (keep last N lines/bytes).\n * Suitable for bash output where you want to see the end (errors, final results).\n *\n * May return partial first line if the last line of original content exceeds byte limit.\n */\nexport function truncateTail(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = Buffer.byteLength(content, \"utf-8\");\n\tconst lines = splitLinesForCounting(content);\n\tconst totalLines = lines.length;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Work backwards from the end\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\tlet lastLinePartial = false;\n\n\tfor (let i = lines.length - 1; i >= 0 && outputLinesArr.length < maxLines; i--) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = Buffer.byteLength(line, \"utf-8\") + (outputLinesArr.length > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\t// Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes,\n\t\t\t// take the end of the line (partial)\n\t\t\tif (outputLinesArr.length === 0) {\n\t\t\t\tconst truncatedLine = truncateStringToBytesFromEnd(line, maxBytes);\n\t\t\t\toutputLinesArr.unshift(truncatedLine);\n\t\t\t\toutputBytesCount = Buffer.byteLength(truncatedLine, \"utf-8\");\n\t\t\t\tlastLinePartial = true;\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.unshift(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = Buffer.byteLength(outputContent, \"utf-8\");\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n\n/**\n * Truncate a string to fit within a byte limit (from the end).\n * Handles multi-byte UTF-8 characters correctly.\n */\nfunction truncateStringToBytesFromEnd(str: string, maxBytes: number): string {\n\tconst buf = Buffer.from(str, \"utf-8\");\n\tif (buf.length <= maxBytes) {\n\t\treturn str;\n\t}\n\n\t// Start from the end, skip maxBytes back\n\tlet start = buf.length - maxBytes;\n\n\t// Find a valid UTF-8 boundary (start of a character)\n\twhile (start < buf.length && (buf[start] & 0xc0) === 0x80) {\n\t\tstart++;\n\t}\n\n\treturn buf.slice(start).toString(\"utf-8\");\n}\n\n/**\n * Truncate a single line to max characters, adding [truncated] suffix.\n * Used for grep match lines.\n */\nexport function truncateLine(\n\tline: string,\n\tmaxChars: number = GREP_MAX_LINE_LENGTH,\n): { text: string; wasTruncated: boolean } {\n\tif (line.length <= maxChars) {\n\t\treturn { text: line, wasTruncated: false };\n\t}\n\treturn { text: `${line.slice(0, maxChars)}... [truncated]`, wasTruncated: true };\n}\n"]}
@@ -0,0 +1,23 @@
1
+ import { Type } from "typebox";
2
+ import type { ToolDefinition } from "../extensions/types.ts";
3
+ /**
4
+ * Worker Tool 定义
5
+ *
6
+ * 用于创建和管理 Worker,并行执行任务
7
+ */
8
+ declare const workerSchema: Type.TObject<{
9
+ enable: Type.TBoolean;
10
+ tasks: Type.TArray<Type.TObject<{
11
+ id: Type.TString;
12
+ description: Type.TString;
13
+ dependencies: Type.TOptional<Type.TArray<Type.TString>>;
14
+ system_prompt: Type.TOptional<Type.TString>;
15
+ }>>;
16
+ system_prompt: Type.TOptional<Type.TString>;
17
+ max_workers: Type.TOptional<Type.TNumber>;
18
+ communication_mode: Type.TOptional<Type.TString>;
19
+ }>;
20
+ export type WorkerToolInput = typeof workerSchema;
21
+ export declare function createWorkerToolDefinition(cwd: string): ToolDefinition<typeof workerSchema>;
22
+ export {};
23
+ //# sourceMappingURL=worker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../../src/core/tools/worker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC/B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAE7D;;;;GAIG;AAEH,QAAA,MAAM,YAAY;;;;;;;;;;;EAWhB,CAAC;AAEH,MAAM,MAAM,eAAe,GAAG,OAAO,YAAY,CAAC;AAElD,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,MAAM,GAAG,cAAc,CAAC,OAAO,YAAY,CAAC,CAgH3F","sourcesContent":["import { Type } from \"typebox\";\nimport type { ToolDefinition } from \"../extensions/types.ts\";\n\n/**\n * Worker Tool 定义\n * \n * 用于创建和管理 Worker,并行执行任务\n */\n\nconst workerSchema = Type.Object({\n enable: Type.Boolean({ description: \"是否启用 Worker 模式\" }),\n tasks: Type.Array(Type.Object({\n id: Type.String({ description: \"任务 ID\" }),\n description: Type.String({ description: \"任务描述\" }),\n dependencies: Type.Optional(Type.Array(Type.String({ description: \"依赖的任务 ID\" }))),\n system_prompt: Type.Optional(Type.String({ description: \"Worker 的系统提示词\" })),\n })),\n system_prompt: Type.Optional(Type.String({ description: \"默认的 Worker 系统提示词\" })),\n max_workers: Type.Optional(Type.Number({ description: \"最大 Worker 数量\" })),\n communication_mode: Type.Optional(Type.String({ description: \"通信模式:socket/message/shared\" })),\n});\n\nexport type WorkerToolInput = typeof workerSchema;\n\nexport function createWorkerToolDefinition(cwd: string): ToolDefinition<typeof workerSchema> {\n return {\n name: \"worker\",\n label: \"Worker\",\n description: \"创建和管理 Worker,并行执行任务。启用 Worker 模式时,会创建多个独立的 Agent 并行执行任务。\",\n promptSnippet: \"创建 Worker 并行执行任务\",\n promptGuidelines: [\n \"Use worker when you need to execute multiple independent tasks in parallel.\",\n \"Worker 模式适合需要并行执行的复杂任务。\",\n \"每个 Worker 有独立的系统提示词和执行环境。\",\n ],\n parameters: workerSchema,\n \n async execute(toolCallId, params, signal, onUpdate, ctx) {\n const { enable, tasks, system_prompt, max_workers, communication_mode } = params;\n \n if (!enable) {\n return {\n content: [{ type: \"text\", text: \"Worker 模式未启用,使用普通模式执行。\" }],\n details: { enabled: false } as any,\n };\n }\n \n // 更新进度\n onUpdate?.({ \n content: [{ type: \"text\", text: `启动 ${tasks.length} 个 Worker...` }],\n details: {} as any\n });\n \n try {\n // 模拟执行所有任务\n const results = new Map<string, any>();\n const errors = new Map<string, string>();\n const startTime = Date.now();\n \n for (const task of tasks) {\n try {\n // 这里可以调用实际的 Agent\n results.set(`worker-${task.id}`, {\n taskId: task.id,\n workerId: `worker-${task.id}`,\n result: { status: \"completed\" },\n status: \"completed\",\n });\n } catch (error) {\n errors.set(`worker-${task.id}`, String(error));\n }\n }\n \n const result = {\n success: errors.size === 0,\n results,\n errors,\n duration: Date.now() - startTime,\n };\n \n // 格式化结果\n const resultText = formatWorkerResult(result);\n \n return {\n content: [{ type: \"text\", text: resultText }],\n details: { \n enabled: true, \n success: result.success,\n results: Object.fromEntries(result.results),\n errors: Object.fromEntries(result.errors),\n duration: result.duration,\n } as any,\n };\n } catch (error) {\n return {\n content: [{ type: \"text\", text: `Worker 执行失败: ${error}` }],\n details: { \n enabled: true, \n success: false,\n error: String(error),\n },\n isError: true,\n };\n }\n },\n \n renderCall(args, theme, context) {\n const text = (context.lastComponent as any) ?? { setText: () => {} };\n \n if (args.enable) {\n const taskCount = args.tasks?.length || 0;\n text.setText(`worker 启用 Worker 模式,${taskCount} 个任务`);\n } else {\n text.setText(\"worker 未启用\");\n }\n \n return text;\n },\n \n renderResult(result, options, theme, context) {\n const text = (context.lastComponent as any) ?? { setText: () => {} };\n \n const details = result.details as any;\n if (details?.enabled) {\n const success = details.success ? \"✓\" : \"✗\";\n const duration = details.duration;\n const taskCount = Object.keys(details.results || {}).length;\n \n text.setText(`${success} Worker 完成 (${taskCount} 个任务, ${duration}ms)`);\n } else {\n text.setText(\"Worker 模式未启用\");\n }\n \n return text;\n },\n };\n}\n\n/**\n * 格式化 Worker 结果\n */\nfunction formatWorkerResult(result: any): string {\n const lines: string[] = [];\n \n lines.push(`## Worker 执行结果`);\n lines.push(\"\");\n lines.push(`- 状态: ${result.success ? \"成功\" : \"失败\"}`);\n lines.push(`- 耗时: ${result.duration}ms`);\n lines.push(`- 任务数: ${result.results.size}`);\n lines.push(\"\");\n \n // 任务结果\n if (result.results.size > 0) {\n lines.push(`### 任务结果`);\n for (const [workerId, workerResult] of result.results) {\n lines.push(`- **${workerId}**: ${JSON.stringify(workerResult.result)}`);\n }\n lines.push(\"\");\n }\n \n // 错误信息\n if (result.errors.size > 0) {\n lines.push(`### 错误信息`);\n for (const [workerId, error] of result.errors) {\n lines.push(`- **${workerId}**: ${error}`);\n }\n lines.push(\"\");\n }\n \n return lines.join(\"\\n\");\n}\n"]}
@@ -0,0 +1,150 @@
1
+ import { Type } from "typebox";
2
+ /**
3
+ * Worker Tool 定义
4
+ *
5
+ * 用于创建和管理 Worker,并行执行任务
6
+ */
7
+ const workerSchema = Type.Object({
8
+ enable: Type.Boolean({ description: "是否启用 Worker 模式" }),
9
+ tasks: Type.Array(Type.Object({
10
+ id: Type.String({ description: "任务 ID" }),
11
+ description: Type.String({ description: "任务描述" }),
12
+ dependencies: Type.Optional(Type.Array(Type.String({ description: "依赖的任务 ID" }))),
13
+ system_prompt: Type.Optional(Type.String({ description: "Worker 的系统提示词" })),
14
+ })),
15
+ system_prompt: Type.Optional(Type.String({ description: "默认的 Worker 系统提示词" })),
16
+ max_workers: Type.Optional(Type.Number({ description: "最大 Worker 数量" })),
17
+ communication_mode: Type.Optional(Type.String({ description: "通信模式:socket/message/shared" })),
18
+ });
19
+ export function createWorkerToolDefinition(cwd) {
20
+ return {
21
+ name: "worker",
22
+ label: "Worker",
23
+ description: "创建和管理 Worker,并行执行任务。启用 Worker 模式时,会创建多个独立的 Agent 并行执行任务。",
24
+ promptSnippet: "创建 Worker 并行执行任务",
25
+ promptGuidelines: [
26
+ "Use worker when you need to execute multiple independent tasks in parallel.",
27
+ "Worker 模式适合需要并行执行的复杂任务。",
28
+ "每个 Worker 有独立的系统提示词和执行环境。",
29
+ ],
30
+ parameters: workerSchema,
31
+ async execute(toolCallId, params, signal, onUpdate, ctx) {
32
+ const { enable, tasks, system_prompt, max_workers, communication_mode } = params;
33
+ if (!enable) {
34
+ return {
35
+ content: [{ type: "text", text: "Worker 模式未启用,使用普通模式执行。" }],
36
+ details: { enabled: false },
37
+ };
38
+ }
39
+ // 更新进度
40
+ onUpdate?.({
41
+ content: [{ type: "text", text: `启动 ${tasks.length} 个 Worker...` }],
42
+ details: {}
43
+ });
44
+ try {
45
+ // 模拟执行所有任务
46
+ const results = new Map();
47
+ const errors = new Map();
48
+ const startTime = Date.now();
49
+ for (const task of tasks) {
50
+ try {
51
+ // 这里可以调用实际的 Agent
52
+ results.set(`worker-${task.id}`, {
53
+ taskId: task.id,
54
+ workerId: `worker-${task.id}`,
55
+ result: { status: "completed" },
56
+ status: "completed",
57
+ });
58
+ }
59
+ catch (error) {
60
+ errors.set(`worker-${task.id}`, String(error));
61
+ }
62
+ }
63
+ const result = {
64
+ success: errors.size === 0,
65
+ results,
66
+ errors,
67
+ duration: Date.now() - startTime,
68
+ };
69
+ // 格式化结果
70
+ const resultText = formatWorkerResult(result);
71
+ return {
72
+ content: [{ type: "text", text: resultText }],
73
+ details: {
74
+ enabled: true,
75
+ success: result.success,
76
+ results: Object.fromEntries(result.results),
77
+ errors: Object.fromEntries(result.errors),
78
+ duration: result.duration,
79
+ },
80
+ };
81
+ }
82
+ catch (error) {
83
+ return {
84
+ content: [{ type: "text", text: `Worker 执行失败: ${error}` }],
85
+ details: {
86
+ enabled: true,
87
+ success: false,
88
+ error: String(error),
89
+ },
90
+ isError: true,
91
+ };
92
+ }
93
+ },
94
+ renderCall(args, theme, context) {
95
+ const text = context.lastComponent ?? { setText: () => { } };
96
+ if (args.enable) {
97
+ const taskCount = args.tasks?.length || 0;
98
+ text.setText(`worker 启用 Worker 模式,${taskCount} 个任务`);
99
+ }
100
+ else {
101
+ text.setText("worker 未启用");
102
+ }
103
+ return text;
104
+ },
105
+ renderResult(result, options, theme, context) {
106
+ const text = context.lastComponent ?? { setText: () => { } };
107
+ const details = result.details;
108
+ if (details?.enabled) {
109
+ const success = details.success ? "✓" : "✗";
110
+ const duration = details.duration;
111
+ const taskCount = Object.keys(details.results || {}).length;
112
+ text.setText(`${success} Worker 完成 (${taskCount} 个任务, ${duration}ms)`);
113
+ }
114
+ else {
115
+ text.setText("Worker 模式未启用");
116
+ }
117
+ return text;
118
+ },
119
+ };
120
+ }
121
+ /**
122
+ * 格式化 Worker 结果
123
+ */
124
+ function formatWorkerResult(result) {
125
+ const lines = [];
126
+ lines.push(`## Worker 执行结果`);
127
+ lines.push("");
128
+ lines.push(`- 状态: ${result.success ? "成功" : "失败"}`);
129
+ lines.push(`- 耗时: ${result.duration}ms`);
130
+ lines.push(`- 任务数: ${result.results.size}`);
131
+ lines.push("");
132
+ // 任务结果
133
+ if (result.results.size > 0) {
134
+ lines.push(`### 任务结果`);
135
+ for (const [workerId, workerResult] of result.results) {
136
+ lines.push(`- **${workerId}**: ${JSON.stringify(workerResult.result)}`);
137
+ }
138
+ lines.push("");
139
+ }
140
+ // 错误信息
141
+ if (result.errors.size > 0) {
142
+ lines.push(`### 错误信息`);
143
+ for (const [workerId, error] of result.errors) {
144
+ lines.push(`- **${workerId}**: ${error}`);
145
+ }
146
+ lines.push("");
147
+ }
148
+ return lines.join("\n");
149
+ }
150
+ //# sourceMappingURL=worker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker.js","sourceRoot":"","sources":["../../../src/core/tools/worker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAG/B;;;;GAIG;AAEH,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC;IAC/B,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,WAAW,EAAE,4BAAgB,EAAE,CAAC;IACvD,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC;QAC5B,EAAE,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,WAAO,EAAE,CAAC;QACzC,WAAW,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,cAAM,EAAE,CAAC;QACjD,YAAY,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,oBAAU,EAAE,CAAC,CAAC,CAAC;QACjF,aAAa,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE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M,CAAC,EAAE,CAAC,CAAC;QAC1E,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACjB,CAAC;IAED,eAAO;IACP,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC;QAC3B,KAAK,CAAC,IAAI,CAAC,kBAAU,CAAC,CAAC;QACvB,KAAK,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;YAC9C,KAAK,CAAC,IAAI,CAAC,OAAO,QAAQ,OAAO,KAAK,EAAE,CAAC,CAAC;QAC5C,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACjB,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAAA,CACzB","sourcesContent":["import { Type } from \"typebox\";\nimport type { ToolDefinition } from \"../extensions/types.ts\";\n\n/**\n * Worker Tool 定义\n * \n * 用于创建和管理 Worker,并行执行任务\n */\n\nconst workerSchema = Type.Object({\n enable: Type.Boolean({ description: \"是否启用 Worker 模式\" }),\n tasks: Type.Array(Type.Object({\n id: Type.String({ description: \"任务 ID\" }),\n description: Type.String({ description: \"任务描述\" }),\n dependencies: Type.Optional(Type.Array(Type.String({ description: \"依赖的任务 ID\" }))),\n system_prompt: Type.Optional(Type.String({ description: \"Worker 的系统提示词\" })),\n })),\n system_prompt: Type.Optional(Type.String({ description: \"默认的 Worker 系统提示词\" })),\n max_workers: Type.Optional(Type.Number({ description: \"最大 Worker 数量\" })),\n communication_mode: Type.Optional(Type.String({ description: \"通信模式:socket/message/shared\" })),\n});\n\nexport type WorkerToolInput = typeof workerSchema;\n\nexport function createWorkerToolDefinition(cwd: string): ToolDefinition<typeof workerSchema> {\n return {\n name: \"worker\",\n label: \"Worker\",\n description: \"创建和管理 Worker,并行执行任务。启用 Worker 模式时,会创建多个独立的 Agent 并行执行任务。\",\n promptSnippet: \"创建 Worker 并行执行任务\",\n promptGuidelines: [\n \"Use worker when you need to execute multiple independent tasks in parallel.\",\n \"Worker 模式适合需要并行执行的复杂任务。\",\n \"每个 Worker 有独立的系统提示词和执行环境。\",\n ],\n parameters: workerSchema,\n \n async execute(toolCallId, params, signal, onUpdate, ctx) {\n const { enable, tasks, system_prompt, max_workers, communication_mode } = params;\n \n if (!enable) {\n return {\n content: [{ type: \"text\", text: \"Worker 模式未启用,使用普通模式执行。\" }],\n details: { enabled: false } as any,\n };\n }\n \n // 更新进度\n onUpdate?.({ \n content: [{ type: \"text\", text: `启动 ${tasks.length} 个 Worker...` }],\n details: {} as any\n });\n \n try {\n // 模拟执行所有任务\n const results = new Map<string, any>();\n const errors = new Map<string, string>();\n const startTime = Date.now();\n \n for (const task of tasks) {\n try {\n // 这里可以调用实际的 Agent\n results.set(`worker-${task.id}`, {\n taskId: task.id,\n workerId: `worker-${task.id}`,\n result: { status: \"completed\" },\n status: \"completed\",\n });\n } catch (error) {\n errors.set(`worker-${task.id}`, String(error));\n }\n }\n \n const result = {\n success: errors.size === 0,\n results,\n errors,\n duration: Date.now() - startTime,\n };\n \n // 格式化结果\n const resultText = formatWorkerResult(result);\n \n return {\n content: [{ type: \"text\", text: resultText }],\n details: { \n enabled: true, \n success: result.success,\n results: Object.fromEntries(result.results),\n errors: Object.fromEntries(result.errors),\n duration: result.duration,\n } as any,\n };\n } catch (error) {\n return {\n content: [{ type: \"text\", text: `Worker 执行失败: ${error}` }],\n details: { \n enabled: true, \n success: false,\n error: String(error),\n },\n isError: true,\n };\n }\n },\n \n renderCall(args, theme, context) {\n const text = (context.lastComponent as any) ?? { setText: () => {} };\n \n if (args.enable) {\n const taskCount = args.tasks?.length || 0;\n text.setText(`worker 启用 Worker 模式,${taskCount} 个任务`);\n } else {\n text.setText(\"worker 未启用\");\n }\n \n return text;\n },\n \n renderResult(result, options, theme, context) {\n const text = (context.lastComponent as any) ?? { setText: () => {} };\n \n const details = result.details as any;\n if (details?.enabled) {\n const success = details.success ? \"✓\" : \"✗\";\n const duration = details.duration;\n const taskCount = Object.keys(details.results || {}).length;\n \n text.setText(`${success} Worker 完成 (${taskCount} 个任务, ${duration}ms)`);\n } else {\n text.setText(\"Worker 模式未启用\");\n }\n \n return text;\n },\n };\n}\n\n/**\n * 格式化 Worker 结果\n */\nfunction formatWorkerResult(result: any): string {\n const lines: string[] = [];\n \n lines.push(`## Worker 执行结果`);\n lines.push(\"\");\n lines.push(`- 状态: ${result.success ? \"成功\" : \"失败\"}`);\n lines.push(`- 耗时: ${result.duration}ms`);\n lines.push(`- 任务数: ${result.results.size}`);\n lines.push(\"\");\n \n // 任务结果\n if (result.results.size > 0) {\n lines.push(`### 任务结果`);\n for (const [workerId, workerResult] of result.results) {\n lines.push(`- **${workerId}**: ${JSON.stringify(workerResult.result)}`);\n }\n lines.push(\"\");\n }\n \n // 错误信息\n if (result.errors.size > 0) {\n lines.push(`### 错误信息`);\n for (const [workerId, error] of result.errors) {\n lines.push(`- **${workerId}**: ${error}`);\n }\n lines.push(\"\");\n }\n \n return lines.join(\"\\n\");\n}\n"]}
@@ -0,0 +1,26 @@
1
+ import type { AgentTool } from "@looze/leaf-agent-core";
2
+ import { type Static, Type } from "typebox";
3
+ import type { ToolDefinition } from "../extensions/types.ts";
4
+ declare const writeSchema: Type.TObject<{
5
+ path: Type.TString;
6
+ content: Type.TString;
7
+ }>;
8
+ export type WriteToolInput = Static<typeof writeSchema>;
9
+ /**
10
+ * Pluggable operations for the write tool.
11
+ * Override these to delegate file writing to remote systems (for example SSH).
12
+ */
13
+ export interface WriteOperations {
14
+ /** Write content to a file */
15
+ writeFile: (absolutePath: string, content: string) => Promise<void>;
16
+ /** Create directory recursively */
17
+ mkdir: (dir: string) => Promise<void>;
18
+ }
19
+ export interface WriteToolOptions {
20
+ /** Custom operations for file writing. Default: local filesystem */
21
+ operations?: WriteOperations;
22
+ }
23
+ export declare function createWriteToolDefinition(cwd: string, options?: WriteToolOptions): ToolDefinition<typeof writeSchema, undefined>;
24
+ export declare function createWriteTool(cwd: string, options?: WriteToolOptions): AgentTool<typeof writeSchema>;
25
+ export {};
26
+ //# sourceMappingURL=write.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"write.d.ts","sourceRoot":"","sources":["../../../src/core/tools/write.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAIxD,OAAO,EAAE,KAAK,MAAM,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAG5C,OAAO,KAAK,EAAE,cAAc,EAA2B,MAAM,wBAAwB,CAAC;AAMtF,QAAA,MAAM,WAAW;;;EAGf,CAAC;AAEH,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,OAAO,WAAW,CAAC,CAAC;AAExD;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC/B,8BAA8B;IAC9B,SAAS,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACpE,mCAAmC;IACnC,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACtC;AAOD,MAAM,WAAW,gBAAgB;IAChC,oEAAoE;IACpE,UAAU,CAAC,EAAE,eAAe,CAAC;CAC7B;AA6ID,wBAAgB,yBAAyB,CACxC,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,gBAAgB,GACxB,cAAc,CAAC,OAAO,WAAW,EAAE,SAAS,CAAC,CA8E/C;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,SAAS,CAAC,OAAO,WAAW,CAAC,CAEtG","sourcesContent":["import type { AgentTool } from \"@looze/leaf-agent-core\";\nimport { Container, Text } from \"@looze/leaf-tui\";\nimport { mkdir as fsMkdir, writeFile as fsWriteFile } from \"fs/promises\";\nimport { dirname } from \"path\";\nimport { type Static, Type } from \"typebox\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.ts\";\nimport { getLanguageFromPath, highlightCode } from \"../../modes/interactive/theme/theme.ts\";\nimport type { ToolDefinition, ToolRenderResultOptions } from \"../extensions/types.ts\";\nimport { withFileMutationQueue } from \"./file-mutation-queue.ts\";\nimport { resolveToCwd } from \"./path-utils.ts\";\nimport { invalidArgText, normalizeDisplayText, replaceTabs, shortenPath, str } from \"./render-utils.ts\";\nimport { wrapToolDefinition } from \"./tool-definition-wrapper.ts\";\n\nconst writeSchema = Type.Object({\n\tpath: Type.String({ description: \"Path to the file to write (relative or absolute)\" }),\n\tcontent: Type.String({ description: \"Content to write to the file\" }),\n});\n\nexport type WriteToolInput = Static<typeof writeSchema>;\n\n/**\n * Pluggable operations for the write tool.\n * Override these to delegate file writing to remote systems (for example SSH).\n */\nexport interface WriteOperations {\n\t/** Write content to a file */\n\twriteFile: (absolutePath: string, content: string) => Promise<void>;\n\t/** Create directory recursively */\n\tmkdir: (dir: string) => Promise<void>;\n}\n\nconst defaultWriteOperations: WriteOperations = {\n\twriteFile: (path, content) => fsWriteFile(path, content, \"utf-8\"),\n\tmkdir: (dir) => fsMkdir(dir, { recursive: true }).then(() => {}),\n};\n\nexport interface WriteToolOptions {\n\t/** Custom operations for file writing. Default: local filesystem */\n\toperations?: WriteOperations;\n}\n\ntype WriteHighlightCache = {\n\trawPath: string | null;\n\tlang: string;\n\trawContent: string;\n\tnormalizedLines: string[];\n\thighlightedLines: string[];\n};\n\nclass WriteCallRenderComponent extends Text {\n\tcache?: WriteHighlightCache;\n\n\tconstructor() {\n\t\tsuper(\"\", 0, 0);\n\t}\n}\n\nconst WRITE_PARTIAL_FULL_HIGHLIGHT_LINES = 50;\n\nfunction highlightSingleLine(line: string, lang: string): string {\n\tconst highlighted = highlightCode(line, lang);\n\treturn highlighted[0] ?? \"\";\n}\n\nfunction refreshWriteHighlightPrefix(cache: WriteHighlightCache): void {\n\tconst prefixCount = Math.min(WRITE_PARTIAL_FULL_HIGHLIGHT_LINES, cache.normalizedLines.length);\n\tif (prefixCount === 0) return;\n\tconst prefixSource = cache.normalizedLines.slice(0, prefixCount).join(\"\\n\");\n\tconst prefixHighlighted = highlightCode(prefixSource, cache.lang);\n\tfor (let i = 0; i < prefixCount; i++) {\n\t\tcache.highlightedLines[i] =\n\t\t\tprefixHighlighted[i] ?? highlightSingleLine(cache.normalizedLines[i] ?? \"\", cache.lang);\n\t}\n}\n\nfunction rebuildWriteHighlightCacheFull(rawPath: string | null, fileContent: string): WriteHighlightCache | undefined {\n\tconst lang = rawPath ? getLanguageFromPath(rawPath) : undefined;\n\tif (!lang) return undefined;\n\tconst displayContent = normalizeDisplayText(fileContent);\n\tconst normalized = replaceTabs(displayContent);\n\treturn {\n\t\trawPath,\n\t\tlang,\n\t\trawContent: fileContent,\n\t\tnormalizedLines: normalized.split(\"\\n\"),\n\t\thighlightedLines: highlightCode(normalized, lang),\n\t};\n}\n\nfunction updateWriteHighlightCacheIncremental(\n\tcache: WriteHighlightCache | undefined,\n\trawPath: string | null,\n\tfileContent: string,\n): WriteHighlightCache | undefined {\n\tconst lang = rawPath ? getLanguageFromPath(rawPath) : undefined;\n\tif (!lang) return undefined;\n\tif (!cache) return rebuildWriteHighlightCacheFull(rawPath, fileContent);\n\tif (cache.lang !== lang || cache.rawPath !== rawPath) return rebuildWriteHighlightCacheFull(rawPath, fileContent);\n\tif (!fileContent.startsWith(cache.rawContent)) return rebuildWriteHighlightCacheFull(rawPath, fileContent);\n\tif (fileContent.length === cache.rawContent.length) return cache;\n\n\tconst deltaRaw = fileContent.slice(cache.rawContent.length);\n\tconst deltaDisplay = normalizeDisplayText(deltaRaw);\n\tconst deltaNormalized = replaceTabs(deltaDisplay);\n\tcache.rawContent = fileContent;\n\tif (cache.normalizedLines.length === 0) {\n\t\tcache.normalizedLines.push(\"\");\n\t\tcache.highlightedLines.push(\"\");\n\t}\n\n\tconst segments = deltaNormalized.split(\"\\n\");\n\tconst lastIndex = cache.normalizedLines.length - 1;\n\tcache.normalizedLines[lastIndex] += segments[0];\n\tcache.highlightedLines[lastIndex] = highlightSingleLine(cache.normalizedLines[lastIndex], cache.lang);\n\tfor (let i = 1; i < segments.length; i++) {\n\t\tcache.normalizedLines.push(segments[i]);\n\t\tcache.highlightedLines.push(highlightSingleLine(segments[i], cache.lang));\n\t}\n\trefreshWriteHighlightPrefix(cache);\n\treturn cache;\n}\n\nfunction trimTrailingEmptyLines(lines: string[]): string[] {\n\tlet end = lines.length;\n\twhile (end > 0 && lines[end - 1] === \"\") {\n\t\tend--;\n\t}\n\treturn lines.slice(0, end);\n}\n\nfunction formatWriteCall(\n\targs: { path?: string; file_path?: string; content?: string } | undefined,\n\toptions: ToolRenderResultOptions,\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.ts\").theme,\n\tcache: WriteHighlightCache | undefined,\n): string {\n\tconst rawPath = str(args?.file_path ?? args?.path);\n\tconst fileContent = str(args?.content);\n\tconst path = rawPath !== null ? shortenPath(rawPath) : null;\n\tconst invalidArg = invalidArgText(theme);\n\tlet text = `${theme.fg(\"toolTitle\", theme.bold(\"write\"))} ${path === null ? invalidArg : path ? theme.fg(\"accent\", path) : theme.fg(\"toolOutput\", \"...\")}`;\n\n\tif (fileContent === null) {\n\t\ttext += `\\n\\n${theme.fg(\"error\", \"[invalid content arg - expected string]\")}`;\n\t} else if (fileContent) {\n\t\tconst lang = rawPath ? getLanguageFromPath(rawPath) : undefined;\n\t\tconst renderedLines = lang\n\t\t\t? (cache?.highlightedLines ?? highlightCode(replaceTabs(normalizeDisplayText(fileContent)), lang))\n\t\t\t: normalizeDisplayText(fileContent).split(\"\\n\");\n\t\tconst lines = trimTrailingEmptyLines(renderedLines);\n\t\tconst totalLines = lines.length;\n\t\tconst maxLines = options.expanded ? lines.length : 10;\n\t\tconst displayLines = lines.slice(0, maxLines);\n\t\tconst remaining = lines.length - maxLines;\n\t\ttext += `\\n\\n${displayLines.map((line) => (lang ? line : theme.fg(\"toolOutput\", replaceTabs(line)))).join(\"\\n\")}`;\n\t\tif (remaining > 0) {\n\t\t\ttext += `${theme.fg(\"muted\", `\\n... (${remaining} more lines, ${totalLines} total,`)} ${keyHint(\"app.tools.expand\", \"to expand\")})`;\n\t\t}\n\t}\n\n\treturn text;\n}\n\nfunction formatWriteResult(\n\tresult: { content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>; isError?: boolean },\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.ts\").theme,\n): string | undefined {\n\tif (!result.isError) {\n\t\treturn undefined;\n\t}\n\tconst output = result.content\n\t\t.filter((c) => c.type === \"text\")\n\t\t.map((c) => c.text || \"\")\n\t\t.join(\"\\n\");\n\tif (!output) {\n\t\treturn undefined;\n\t}\n\treturn `\\n${theme.fg(\"error\", output)}`;\n}\n\nexport function createWriteToolDefinition(\n\tcwd: string,\n\toptions?: WriteToolOptions,\n): ToolDefinition<typeof writeSchema, undefined> {\n\tconst ops = options?.operations ?? defaultWriteOperations;\n\treturn {\n\t\tname: \"write\",\n\t\tlabel: \"write\",\n\t\tdescription:\n\t\t\t\"Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.\",\n\t\tpromptSnippet: \"Create or overwrite files\",\n\t\tpromptGuidelines: [\"Use write only for new files or complete rewrites.\"],\n\t\tparameters: writeSchema,\n\t\tasync execute(\n\t\t\t_toolCallId,\n\t\t\t{ path, content }: { path: string; content: string },\n\t\t\tsignal?: AbortSignal,\n\t\t\t_onUpdate?,\n\t\t\t_ctx?,\n\t\t) {\n\t\t\tconst absolutePath = resolveToCwd(path, cwd);\n\t\t\tconst dir = dirname(absolutePath);\n\t\t\treturn withFileMutationQueue(absolutePath, async () => {\n\t\t\t\t// Do not reject from an abort event listener here: that would release the\n\t\t\t\t// mutation queue while an in-flight filesystem operation may still finish.\n\t\t\t\t// Checking signal.aborted after each await observes the same aborts while\n\t\t\t\t// keeping the queue locked until the current operation has settled.\n\t\t\t\tconst throwIfAborted = (): void => {\n\t\t\t\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\t\t\t};\n\n\t\t\t\tthrowIfAborted();\n\t\t\t\t// Create parent directories if needed.\n\t\t\t\tawait ops.mkdir(dir);\n\t\t\t\tthrowIfAborted();\n\n\t\t\t\t// Write the file contents.\n\t\t\t\tawait ops.writeFile(absolutePath, content);\n\t\t\t\tthrowIfAborted();\n\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [{ type: \"text\", text: `Successfully wrote ${content.length} bytes to ${path}` }],\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t});\n\t\t},\n\t\trenderCall(args, theme, context) {\n\t\t\tconst renderArgs = args as { path?: string; file_path?: string; content?: string } | undefined;\n\t\t\tconst rawPath = str(renderArgs?.file_path ?? renderArgs?.path);\n\t\t\tconst fileContent = str(renderArgs?.content);\n\t\t\tconst component =\n\t\t\t\t(context.lastComponent as WriteCallRenderComponent | undefined) ?? new WriteCallRenderComponent();\n\t\t\tif (fileContent !== null) {\n\t\t\t\tcomponent.cache = context.argsComplete\n\t\t\t\t\t? rebuildWriteHighlightCacheFull(rawPath, fileContent)\n\t\t\t\t\t: updateWriteHighlightCacheIncremental(component.cache, rawPath, fileContent);\n\t\t\t} else {\n\t\t\t\tcomponent.cache = undefined;\n\t\t\t}\n\t\t\tcomponent.setText(\n\t\t\t\tformatWriteCall(\n\t\t\t\t\trenderArgs,\n\t\t\t\t\t{ expanded: context.expanded, isPartial: context.isPartial },\n\t\t\t\t\ttheme,\n\t\t\t\t\tcomponent.cache,\n\t\t\t\t),\n\t\t\t);\n\t\t\treturn component;\n\t\t},\n\t\trenderResult(result, _options, theme, context) {\n\t\t\tconst output = formatWriteResult({ ...result, isError: context.isError }, theme);\n\t\t\tif (!output) {\n\t\t\t\tconst component = (context.lastComponent as Container | undefined) ?? new Container();\n\t\t\t\tcomponent.clear();\n\t\t\t\treturn component;\n\t\t\t}\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(output);\n\t\t\treturn text;\n\t\t},\n\t};\n}\n\nexport function createWriteTool(cwd: string, options?: WriteToolOptions): AgentTool<typeof writeSchema> {\n\treturn wrapToolDefinition(createWriteToolDefinition(cwd, options));\n}\n"]}