@earendil-works/pi-coding-agent 0.87.1 → 0.99.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 (721) hide show
  1. package/CHANGELOG.md +79 -0
  2. package/dist/bun/cli.d.ts.map +1 -1
  3. package/dist/bun/restore-sandbox-env.d.ts.map +1 -1
  4. package/dist/bun/restore-sandbox-env.js.map +1 -1
  5. package/dist/bun/runtime-setup.d.ts.map +1 -1
  6. package/dist/bun/runtime-setup.js +4 -1
  7. package/dist/bun/runtime-setup.js.map +1 -1
  8. package/dist/bun/sandbox-env-setup.d.ts.map +1 -1
  9. package/dist/bundle/chunks/anthropic-messages-A6EXUBG6.js +6 -0
  10. package/dist/bundle/chunks/anthropic.js +2 -2
  11. package/dist/bundle/chunks/azure-openai-responses-EN5WOBET.js +2 -0
  12. package/dist/bundle/chunks/bedrock-converse-stream.js +2 -2
  13. package/dist/bundle/chunks/chunk-4CSSZFEW.js +1435 -0
  14. package/dist/bundle/chunks/chunk-7JGR3GZN.js +11 -0
  15. package/dist/bundle/chunks/chunk-AXPY26X7.js +146 -0
  16. package/dist/bundle/chunks/chunk-EPDRUIU6.js +2 -0
  17. package/dist/bundle/chunks/chunk-GOPCREV2.js +4 -0
  18. package/dist/bundle/chunks/chunk-H3QXYIF7.js +31 -0
  19. package/dist/bundle/chunks/{chunk-S7SZN6Z3.js → chunk-HSPCVFST.js} +1 -1
  20. package/dist/bundle/chunks/chunk-IMTXZUO7.js +82 -0
  21. package/dist/bundle/chunks/chunk-J4CWPC4N.js +2 -0
  22. package/dist/bundle/chunks/chunk-LLMKQWZS.js +21 -0
  23. package/dist/bundle/chunks/{chunk-WIUZI2CJ.js → chunk-LRS5S6EK.js} +3 -3
  24. package/dist/bundle/chunks/chunk-PEHWOV5U.js +187 -0
  25. package/dist/bundle/chunks/{chunk-XDUWOHPL.js → chunk-S3GGRYIO.js} +2 -2
  26. package/dist/bundle/chunks/chunk-UTB4DTYQ.js +2 -0
  27. package/dist/bundle/chunks/{chunk-FUXEF6JQ.js → chunk-W2DOMZSC.js} +1 -1
  28. package/dist/bundle/chunks/cli-AZXNGIZH.js +49 -0
  29. package/dist/bundle/chunks/cloudflare-workers-ai-system-one-XJQQYGGU.js +2 -0
  30. package/dist/bundle/chunks/codemode-worker.js +295 -0
  31. package/dist/bundle/chunks/execute-7XXPZAWW.js +308 -0
  32. package/dist/bundle/chunks/github-copilot.js +1 -1
  33. package/dist/bundle/chunks/google-generative-ai-CQAKKJTA.js +2 -0
  34. package/dist/bundle/chunks/google-vertex-AE36YXJU.js +2 -0
  35. package/dist/bundle/chunks/{lib-HGQMNYVT.js → lib-TEDK3E5I.js} +2 -2
  36. package/dist/bundle/chunks/llama-cpp-classify-PJJYKPLD.js +22 -0
  37. package/dist/bundle/chunks/mistral-conversations-KEYTXJM7.js +5 -0
  38. package/dist/bundle/chunks/{openai-codex-responses-QDSOTBBO.js → openai-codex-responses-JQTCOVEG.js} +2 -2
  39. package/dist/bundle/chunks/openai-codex.js +2 -2
  40. package/dist/bundle/chunks/{openai-completions-OBX42CLD.js → openai-completions-XW2Q5HVC.js} +2 -2
  41. package/dist/bundle/chunks/openai-responses-6AXWFL75.js +3 -0
  42. package/dist/bundle/chunks/{openrouter-images-N4XVVFSD.js → openrouter-images-TPQ4V6OR.js} +1 -1
  43. package/dist/bundle/chunks/openrouter.js +2 -2
  44. package/dist/bundle/chunks/{pi-messages-GFKBWJHZ.js → pi-messages-TM5SOOIW.js} +2 -2
  45. package/dist/bundle/chunks/radius.js +2 -2
  46. package/dist/bundle/chunks/runtime-G5WJPRBM.js +2 -0
  47. package/dist/bundle/chunks/typesafe-system-one-E7A4HNJI.js +2 -0
  48. package/dist/bundle/chunks/virtual-modules-AQ3FLU6H.js +2 -0
  49. package/dist/bundle/cli-runtime.js +1 -1
  50. package/dist/bundle/index.js +1 -1
  51. package/dist/bundle/rpc-entry.js +1 -1
  52. package/dist/cli/args.d.ts.map +1 -1
  53. package/dist/cli/args.js +4 -3
  54. package/dist/cli/args.js.map +1 -1
  55. package/dist/cli/auth-check.d.ts.map +1 -1
  56. package/dist/cli/auth-check.js.map +1 -1
  57. package/dist/cli/auth-command.d.ts.map +1 -1
  58. package/dist/cli/auth-command.js.map +1 -1
  59. package/dist/cli/config-selector.d.ts.map +1 -1
  60. package/dist/cli/config-selector.js.map +1 -1
  61. package/dist/cli/credential-print.d.ts.map +1 -1
  62. package/dist/cli/credential-print.js.map +1 -1
  63. package/dist/cli/file-processor.d.ts.map +1 -1
  64. package/dist/cli/file-processor.js.map +1 -1
  65. package/dist/cli/initial-message.d.ts +1 -1
  66. package/dist/cli/initial-message.d.ts.map +1 -1
  67. package/dist/cli/initial-message.js.map +1 -1
  68. package/dist/cli/list-models.d.ts.map +1 -1
  69. package/dist/cli/list-models.js.map +1 -1
  70. package/dist/cli/project-trust.d.ts.map +1 -1
  71. package/dist/cli/project-trust.js.map +1 -1
  72. package/dist/cli/session-picker.d.ts.map +1 -1
  73. package/dist/cli/session-picker.js.map +1 -1
  74. package/dist/cli/setup.d.ts.map +1 -1
  75. package/dist/cli/setup.js.map +1 -1
  76. package/dist/cli/startup-ui.d.ts.map +1 -1
  77. package/dist/cli/startup-ui.js +38 -30
  78. package/dist/cli/startup-ui.js.map +1 -1
  79. package/dist/cli.d.ts.map +1 -1
  80. package/dist/config.d.ts +18 -4
  81. package/dist/config.d.ts.map +1 -1
  82. package/dist/config.js +30 -4
  83. package/dist/config.js.map +1 -1
  84. package/dist/core/agent-session-runtime.d.ts.map +1 -1
  85. package/dist/core/agent-session-runtime.js +1 -1
  86. package/dist/core/agent-session-runtime.js.map +1 -1
  87. package/dist/core/agent-session-services.d.ts.map +1 -1
  88. package/dist/core/agent-session-services.js +13 -0
  89. package/dist/core/agent-session-services.js.map +1 -1
  90. package/dist/core/agent-session.d.ts +135 -11
  91. package/dist/core/agent-session.d.ts.map +1 -1
  92. package/dist/core/agent-session.js +509 -253
  93. package/dist/core/agent-session.js.map +1 -1
  94. package/dist/core/auth-guidance.d.ts.map +1 -1
  95. package/dist/core/auth-guidance.js.map +1 -1
  96. package/dist/core/auth-storage.d.ts.map +1 -1
  97. package/dist/core/auth-storage.js.map +1 -1
  98. package/dist/core/bash-executor.d.ts.map +1 -1
  99. package/dist/core/bash-executor.js.map +1 -1
  100. package/dist/core/bug-report-upload.d.ts.map +1 -1
  101. package/dist/core/bug-report-upload.js.map +1 -1
  102. package/dist/core/bug-report.d.ts +1 -1
  103. package/dist/core/bug-report.d.ts.map +1 -1
  104. package/dist/core/bug-report.js +1 -1
  105. package/dist/core/bug-report.js.map +1 -1
  106. package/dist/core/cache-stats.d.ts.map +1 -1
  107. package/dist/core/cache-stats.js.map +1 -1
  108. package/dist/core/cache-warmer.d.ts.map +1 -1
  109. package/dist/core/cache-warmer.js.map +1 -1
  110. package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
  111. package/dist/core/compaction/branch-summarization.js.map +1 -1
  112. package/dist/core/compaction/compaction.d.ts.map +1 -1
  113. package/dist/core/compaction/compaction.js +1 -22
  114. package/dist/core/compaction/compaction.js.map +1 -1
  115. package/dist/core/compaction/index.d.ts.map +1 -1
  116. package/dist/core/compaction/utils.d.ts +2 -1
  117. package/dist/core/compaction/utils.d.ts.map +1 -1
  118. package/dist/core/compaction/utils.js +25 -18
  119. package/dist/core/compaction/utils.js.map +1 -1
  120. package/dist/core/crash-log.d.ts.map +1 -1
  121. package/dist/core/crash-log.js +2 -1
  122. package/dist/core/crash-log.js.map +1 -1
  123. package/dist/core/defaults.d.ts.map +1 -1
  124. package/dist/core/diagnostics.d.ts.map +1 -1
  125. package/dist/core/event-bus.d.ts.map +1 -1
  126. package/dist/core/event-bus.js.map +1 -1
  127. package/dist/core/exec.d.ts.map +1 -1
  128. package/dist/core/exec.js.map +1 -1
  129. package/dist/core/experimental.d.ts.map +1 -1
  130. package/dist/core/experimental.js.map +1 -1
  131. package/dist/core/export-html/ansi-to-html.d.ts.map +1 -1
  132. package/dist/core/export-html/ansi-to-html.js.map +1 -1
  133. package/dist/core/export-html/index.d.ts.map +1 -1
  134. package/dist/core/export-html/index.js.map +1 -1
  135. package/dist/core/export-html/template.css +10 -0
  136. package/dist/core/export-html/template.js +66 -17
  137. package/dist/core/export-html/tool-renderer.d.ts.map +1 -1
  138. package/dist/core/export-html/tool-renderer.js.map +1 -1
  139. package/dist/core/extensions/index.d.ts +1 -1
  140. package/dist/core/extensions/index.d.ts.map +1 -1
  141. package/dist/core/extensions/index.js.map +1 -1
  142. package/dist/core/extensions/jiti-loader.d.ts.map +1 -1
  143. package/dist/core/extensions/jiti-static-loader.d.ts.map +1 -1
  144. package/dist/core/extensions/loader.d.ts.map +1 -1
  145. package/dist/core/extensions/loader.js +55 -5
  146. package/dist/core/extensions/loader.js.map +1 -1
  147. package/dist/core/extensions/runner.d.ts +18 -1
  148. package/dist/core/extensions/runner.d.ts.map +1 -1
  149. package/dist/core/extensions/runner.js +99 -0
  150. package/dist/core/extensions/runner.js.map +1 -1
  151. package/dist/core/extensions/types.d.ts +296 -22
  152. package/dist/core/extensions/types.d.ts.map +1 -1
  153. package/dist/core/extensions/types.js.map +1 -1
  154. package/dist/core/extensions/virtual-modules.d.ts.map +1 -1
  155. package/dist/core/extensions/wrapper.d.ts +2 -2
  156. package/dist/core/extensions/wrapper.d.ts.map +1 -1
  157. package/dist/core/extensions/wrapper.js +3 -3
  158. package/dist/core/extensions/wrapper.js.map +1 -1
  159. package/dist/core/footer-data-provider.d.ts.map +1 -1
  160. package/dist/core/footer-data-provider.js.map +1 -1
  161. package/dist/core/http-dispatcher.d.ts.map +1 -1
  162. package/dist/core/http-dispatcher.js.map +1 -1
  163. package/dist/core/index.d.ts.map +1 -1
  164. package/dist/core/keybindings.d.ts +1 -1
  165. package/dist/core/keybindings.d.ts.map +1 -1
  166. package/dist/core/keybindings.js +1 -1
  167. package/dist/core/keybindings.js.map +1 -1
  168. package/dist/core/mcp-servers.d.ts +98 -0
  169. package/dist/core/mcp-servers.d.ts.map +1 -0
  170. package/dist/core/mcp-servers.js +157 -0
  171. package/dist/core/mcp-servers.js.map +1 -0
  172. package/dist/core/messages.d.ts.map +1 -1
  173. package/dist/core/messages.js.map +1 -1
  174. package/dist/core/model-config.d.ts.map +1 -1
  175. package/dist/core/model-config.js.map +1 -1
  176. package/dist/core/model-registry.d.ts +13 -1
  177. package/dist/core/model-registry.d.ts.map +1 -1
  178. package/dist/core/model-registry.js +25 -0
  179. package/dist/core/model-registry.js.map +1 -1
  180. package/dist/core/model-resolver.d.ts +2 -2
  181. package/dist/core/model-resolver.d.ts.map +1 -1
  182. package/dist/core/model-resolver.js +4 -4
  183. package/dist/core/model-resolver.js.map +1 -1
  184. package/dist/core/model-runtime.d.ts +41 -3
  185. package/dist/core/model-runtime.d.ts.map +1 -1
  186. package/dist/core/model-runtime.js +167 -17
  187. package/dist/core/model-runtime.js.map +1 -1
  188. package/dist/core/models-store.d.ts.map +1 -1
  189. package/dist/core/models-store.js.map +1 -1
  190. package/dist/core/nested-tool-calls.d.ts +102 -0
  191. package/dist/core/nested-tool-calls.d.ts.map +1 -0
  192. package/dist/core/nested-tool-calls.js +178 -0
  193. package/dist/core/nested-tool-calls.js.map +1 -0
  194. package/dist/core/output-guard.d.ts.map +1 -1
  195. package/dist/core/output-guard.js.map +1 -1
  196. package/dist/core/package-manager.d.ts +4 -0
  197. package/dist/core/package-manager.d.ts.map +1 -1
  198. package/dist/core/package-manager.js +59 -12
  199. package/dist/core/package-manager.js.map +1 -1
  200. package/dist/core/pi-manifest.d.ts.map +1 -1
  201. package/dist/core/pi-manifest.js.map +1 -1
  202. package/dist/core/project-trust.d.ts.map +1 -1
  203. package/dist/core/project-trust.js.map +1 -1
  204. package/dist/core/prompt-templates.d.ts.map +1 -1
  205. package/dist/core/prompt-templates.js.map +1 -1
  206. package/dist/core/provider-attribution.d.ts.map +1 -1
  207. package/dist/core/provider-attribution.js.map +1 -1
  208. package/dist/core/provider-composer.d.ts +40 -21
  209. package/dist/core/provider-composer.d.ts.map +1 -1
  210. package/dist/core/provider-composer.js +92 -40
  211. package/dist/core/provider-composer.js.map +1 -1
  212. package/dist/core/radius.d.ts.map +1 -1
  213. package/dist/core/radius.js.map +1 -1
  214. package/dist/core/remote-catalog-provider.d.ts +8 -1
  215. package/dist/core/remote-catalog-provider.d.ts.map +1 -1
  216. package/dist/core/remote-catalog-provider.js +18 -3
  217. package/dist/core/remote-catalog-provider.js.map +1 -1
  218. package/dist/core/resolve-config-value.d.ts.map +1 -1
  219. package/dist/core/resolve-config-value.js.map +1 -1
  220. package/dist/core/resource-loader.d.ts +7 -1
  221. package/dist/core/resource-loader.d.ts.map +1 -1
  222. package/dist/core/resource-loader.js +161 -35
  223. package/dist/core/resource-loader.js.map +1 -1
  224. package/dist/core/runtime-credentials.d.ts.map +1 -1
  225. package/dist/core/runtime-credentials.js.map +1 -1
  226. package/dist/core/sdk.d.ts +1 -1
  227. package/dist/core/sdk.d.ts.map +1 -1
  228. package/dist/core/sdk.js +24 -6
  229. package/dist/core/sdk.js.map +1 -1
  230. package/dist/core/session-cwd.d.ts.map +1 -1
  231. package/dist/core/session-cwd.js.map +1 -1
  232. package/dist/core/session-export.d.ts.map +1 -1
  233. package/dist/core/session-export.js.map +1 -1
  234. package/dist/core/session-manager.d.ts +9 -0
  235. package/dist/core/session-manager.d.ts.map +1 -1
  236. package/dist/core/session-manager.js +22 -22
  237. package/dist/core/session-manager.js.map +1 -1
  238. package/dist/core/settings-diagnostics.d.ts.map +1 -1
  239. package/dist/core/settings-diagnostics.js.map +1 -1
  240. package/dist/core/settings-manager.d.ts +31 -1
  241. package/dist/core/settings-manager.d.ts.map +1 -1
  242. package/dist/core/settings-manager.js +72 -2
  243. package/dist/core/settings-manager.js.map +1 -1
  244. package/dist/core/skills.d.ts.map +1 -1
  245. package/dist/core/skills.js.map +1 -1
  246. package/dist/core/slash-commands.d.ts.map +1 -1
  247. package/dist/core/source-info.d.ts +8 -0
  248. package/dist/core/source-info.d.ts.map +1 -1
  249. package/dist/core/source-info.js +16 -0
  250. package/dist/core/source-info.js.map +1 -1
  251. package/dist/core/system-prompt.d.ts.map +1 -1
  252. package/dist/core/system-prompt.js +1 -1
  253. package/dist/core/system-prompt.js.map +1 -1
  254. package/dist/core/telemetry.d.ts.map +1 -1
  255. package/dist/core/telemetry.js.map +1 -1
  256. package/dist/core/timings.d.ts.map +1 -1
  257. package/dist/core/timings.js.map +1 -1
  258. package/dist/core/tools/bash.d.ts +12 -0
  259. package/dist/core/tools/bash.d.ts.map +1 -1
  260. package/dist/core/tools/bash.js +35 -2
  261. package/dist/core/tools/bash.js.map +1 -1
  262. package/dist/core/tools/edit-diff.d.ts.map +1 -1
  263. package/dist/core/tools/edit-diff.js.map +1 -1
  264. package/dist/core/tools/edit.d.ts.map +1 -1
  265. package/dist/core/tools/edit.js.map +1 -1
  266. package/dist/core/tools/file-mutation-queue.d.ts.map +1 -1
  267. package/dist/core/tools/file-mutation-queue.js.map +1 -1
  268. package/dist/core/tools/find.d.ts.map +1 -1
  269. package/dist/core/tools/find.js.map +1 -1
  270. package/dist/core/tools/grep.d.ts.map +1 -1
  271. package/dist/core/tools/grep.js.map +1 -1
  272. package/dist/core/tools/index.d.ts.map +1 -1
  273. package/dist/core/tools/index.js.map +1 -1
  274. package/dist/core/tools/ls.d.ts.map +1 -1
  275. package/dist/core/tools/ls.js.map +1 -1
  276. package/dist/core/tools/output-accumulator.d.ts +11 -0
  277. package/dist/core/tools/output-accumulator.d.ts.map +1 -1
  278. package/dist/core/tools/output-accumulator.js +36 -0
  279. package/dist/core/tools/output-accumulator.js.map +1 -1
  280. package/dist/core/tools/path-utils.d.ts.map +1 -1
  281. package/dist/core/tools/path-utils.js.map +1 -1
  282. package/dist/core/tools/powershell.d.ts.map +1 -1
  283. package/dist/core/tools/powershell.js.map +1 -1
  284. package/dist/core/tools/read.d.ts.map +1 -1
  285. package/dist/core/tools/read.js.map +1 -1
  286. package/dist/core/tools/render-utils.d.ts +6 -0
  287. package/dist/core/tools/render-utils.d.ts.map +1 -1
  288. package/dist/core/tools/render-utils.js +26 -0
  289. package/dist/core/tools/render-utils.js.map +1 -1
  290. package/dist/core/tools/renderers/bash.d.ts.map +1 -1
  291. package/dist/core/tools/renderers/bash.js +9 -10
  292. package/dist/core/tools/renderers/bash.js.map +1 -1
  293. package/dist/core/tools/renderers/edit.d.ts.map +1 -1
  294. package/dist/core/tools/renderers/edit.js.map +1 -1
  295. package/dist/core/tools/renderers/find.d.ts.map +1 -1
  296. package/dist/core/tools/renderers/find.js.map +1 -1
  297. package/dist/core/tools/renderers/grep.d.ts.map +1 -1
  298. package/dist/core/tools/renderers/grep.js.map +1 -1
  299. package/dist/core/tools/renderers/index.d.ts.map +1 -1
  300. package/dist/core/tools/renderers/index.js.map +1 -1
  301. package/dist/core/tools/renderers/ls.d.ts.map +1 -1
  302. package/dist/core/tools/renderers/ls.js.map +1 -1
  303. package/dist/core/tools/renderers/read.d.ts.map +1 -1
  304. package/dist/core/tools/renderers/read.js +3 -2
  305. package/dist/core/tools/renderers/read.js.map +1 -1
  306. package/dist/core/tools/renderers/write.d.ts.map +1 -1
  307. package/dist/core/tools/renderers/write.js.map +1 -1
  308. package/dist/core/tools/tool-definition-wrapper.d.ts +5 -3
  309. package/dist/core/tools/tool-definition-wrapper.d.ts.map +1 -1
  310. package/dist/core/tools/tool-definition-wrapper.js +3 -1
  311. package/dist/core/tools/tool-definition-wrapper.js.map +1 -1
  312. package/dist/core/tools/truncate.d.ts +14 -0
  313. package/dist/core/tools/truncate.d.ts.map +1 -1
  314. package/dist/core/tools/truncate.js +29 -0
  315. package/dist/core/tools/truncate.js.map +1 -1
  316. package/dist/core/tools/write.d.ts.map +1 -1
  317. package/dist/core/tools/write.js.map +1 -1
  318. package/dist/core/trust-manager.d.ts.map +1 -1
  319. package/dist/core/trust-manager.js +1 -0
  320. package/dist/core/trust-manager.js.map +1 -1
  321. package/dist/core/usage-totals.d.ts +2 -0
  322. package/dist/core/usage-totals.d.ts.map +1 -1
  323. package/dist/core/usage-totals.js +23 -0
  324. package/dist/core/usage-totals.js.map +1 -1
  325. package/dist/core/virtual-models.d.ts +115 -0
  326. package/dist/core/virtual-models.d.ts.map +1 -0
  327. package/dist/core/virtual-models.js +117 -0
  328. package/dist/core/virtual-models.js.map +1 -0
  329. package/dist/extensions/codemode/execute.d.ts +16 -0
  330. package/dist/extensions/codemode/execute.d.ts.map +1 -0
  331. package/dist/extensions/codemode/execute.js +390 -0
  332. package/dist/extensions/codemode/execute.js.map +1 -0
  333. package/dist/extensions/codemode/execute.lazy.d.ts +3 -0
  334. package/dist/extensions/codemode/execute.lazy.d.ts.map +1 -0
  335. package/dist/extensions/codemode/execute.lazy.js +3 -0
  336. package/dist/extensions/codemode/execute.lazy.js.map +1 -0
  337. package/dist/extensions/codemode/index.d.ts +21 -0
  338. package/dist/extensions/codemode/index.d.ts.map +1 -0
  339. package/dist/extensions/codemode/index.js +31 -0
  340. package/dist/extensions/codemode/index.js.map +1 -0
  341. package/dist/extensions/codemode/renderer.d.ts +12 -0
  342. package/dist/extensions/codemode/renderer.d.ts.map +1 -0
  343. package/dist/extensions/codemode/renderer.js +119 -0
  344. package/dist/extensions/codemode/renderer.js.map +1 -0
  345. package/dist/extensions/codemode/tool.d.ts +122 -0
  346. package/dist/extensions/codemode/tool.d.ts.map +1 -0
  347. package/dist/extensions/codemode/tool.js +324 -0
  348. package/dist/extensions/codemode/tool.js.map +1 -0
  349. package/dist/extensions/codemode/worker.d.ts +7 -0
  350. package/dist/extensions/codemode/worker.d.ts.map +1 -0
  351. package/dist/extensions/codemode/worker.js +7 -0
  352. package/dist/extensions/codemode/worker.js.map +1 -0
  353. package/dist/extensions/index.d.ts.map +1 -1
  354. package/dist/extensions/index.js +11 -1
  355. package/dist/extensions/index.js.map +1 -1
  356. package/dist/extensions/llama/client.d.ts.map +1 -1
  357. package/dist/extensions/llama/client.js.map +1 -1
  358. package/dist/extensions/llama/huggingface.d.ts.map +1 -1
  359. package/dist/extensions/llama/huggingface.js.map +1 -1
  360. package/dist/extensions/llama/index.d.ts.map +1 -1
  361. package/dist/extensions/llama/index.js.map +1 -1
  362. package/dist/extensions/llama/provider.d.ts +1 -1
  363. package/dist/extensions/llama/provider.d.ts.map +1 -1
  364. package/dist/extensions/llama/provider.js +65 -13
  365. package/dist/extensions/llama/provider.js.map +1 -1
  366. package/dist/extensions/llama/ui.d.ts.map +1 -1
  367. package/dist/extensions/llama/ui.js.map +1 -1
  368. package/dist/extensions/mcp/cli.d.ts +21 -0
  369. package/dist/extensions/mcp/cli.d.ts.map +1 -0
  370. package/dist/extensions/mcp/cli.js +485 -0
  371. package/dist/extensions/mcp/cli.js.map +1 -0
  372. package/dist/extensions/mcp/cli.lazy.d.ts +3 -0
  373. package/dist/extensions/mcp/cli.lazy.d.ts.map +1 -0
  374. package/dist/extensions/mcp/cli.lazy.js +3 -0
  375. package/dist/extensions/mcp/cli.lazy.js.map +1 -0
  376. package/dist/extensions/mcp/config.d.ts +70 -0
  377. package/dist/extensions/mcp/config.d.ts.map +1 -0
  378. package/dist/extensions/mcp/config.js +146 -0
  379. package/dist/extensions/mcp/config.js.map +1 -0
  380. package/dist/extensions/mcp/index.d.ts +52 -0
  381. package/dist/extensions/mcp/index.d.ts.map +1 -0
  382. package/dist/extensions/mcp/index.js +902 -0
  383. package/dist/extensions/mcp/index.js.map +1 -0
  384. package/dist/extensions/mcp/log.d.ts +16 -0
  385. package/dist/extensions/mcp/log.d.ts.map +1 -0
  386. package/dist/extensions/mcp/log.js +66 -0
  387. package/dist/extensions/mcp/log.js.map +1 -0
  388. package/dist/extensions/mcp/oauth.d.ts +94 -0
  389. package/dist/extensions/mcp/oauth.d.ts.map +1 -0
  390. package/dist/extensions/mcp/oauth.js +333 -0
  391. package/dist/extensions/mcp/oauth.js.map +1 -0
  392. package/dist/extensions/mcp/resources.d.ts +43 -0
  393. package/dist/extensions/mcp/resources.d.ts.map +1 -0
  394. package/dist/extensions/mcp/resources.js +262 -0
  395. package/dist/extensions/mcp/resources.js.map +1 -0
  396. package/dist/extensions/mcp/runtime.d.ts +96 -0
  397. package/dist/extensions/mcp/runtime.d.ts.map +1 -0
  398. package/dist/extensions/mcp/runtime.js +388 -0
  399. package/dist/extensions/mcp/runtime.js.map +1 -0
  400. package/dist/extensions/mcp/runtime.lazy.d.ts +3 -0
  401. package/dist/extensions/mcp/runtime.lazy.d.ts.map +1 -0
  402. package/dist/extensions/mcp/runtime.lazy.js +3 -0
  403. package/dist/extensions/mcp/runtime.lazy.js.map +1 -0
  404. package/dist/extensions/mcp/tools.d.ts +83 -0
  405. package/dist/extensions/mcp/tools.d.ts.map +1 -0
  406. package/dist/extensions/mcp/tools.js +233 -0
  407. package/dist/extensions/mcp/tools.js.map +1 -0
  408. package/dist/extensions/mcp/ui.d.ts +60 -0
  409. package/dist/extensions/mcp/ui.d.ts.map +1 -0
  410. package/dist/extensions/mcp/ui.js +172 -0
  411. package/dist/extensions/mcp/ui.js.map +1 -0
  412. package/dist/extensions/tool-search/index.d.ts +12 -0
  413. package/dist/extensions/tool-search/index.d.ts.map +1 -0
  414. package/dist/extensions/tool-search/index.js +15 -0
  415. package/dist/extensions/tool-search/index.js.map +1 -0
  416. package/dist/extensions/tool-search/tool.d.ts +72 -0
  417. package/dist/extensions/tool-search/tool.d.ts.map +1 -0
  418. package/dist/extensions/tool-search/tool.js +210 -0
  419. package/dist/extensions/tool-search/tool.js.map +1 -0
  420. package/dist/index.d.ts +9 -2
  421. package/dist/index.d.ts.map +1 -1
  422. package/dist/index.js +5 -0
  423. package/dist/index.js.map +1 -1
  424. package/dist/main.d.ts.map +1 -1
  425. package/dist/main.js +10 -0
  426. package/dist/main.js.map +1 -1
  427. package/dist/migrations.d.ts.map +1 -1
  428. package/dist/migrations.js.map +1 -1
  429. package/dist/modes/index.d.ts.map +1 -1
  430. package/dist/modes/interactive/bug-report.d.ts.map +1 -1
  431. package/dist/modes/interactive/bug-report.js.map +1 -1
  432. package/dist/modes/interactive/chat-viewport.d.ts.map +1 -1
  433. package/dist/modes/interactive/chat-viewport.js.map +1 -1
  434. package/dist/modes/interactive/components/armin.d.ts.map +1 -1
  435. package/dist/modes/interactive/components/armin.js.map +1 -1
  436. package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
  437. package/dist/modes/interactive/components/assistant-message.js.map +1 -1
  438. package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -1
  439. package/dist/modes/interactive/components/bash-execution.js.map +1 -1
  440. package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -1
  441. package/dist/modes/interactive/components/bordered-loader.js.map +1 -1
  442. package/dist/modes/interactive/components/branch-summary-message.d.ts.map +1 -1
  443. package/dist/modes/interactive/components/branch-summary-message.js.map +1 -1
  444. package/dist/modes/interactive/components/compaction-summary-message.d.ts.map +1 -1
  445. package/dist/modes/interactive/components/compaction-summary-message.js.map +1 -1
  446. package/dist/modes/interactive/components/config-selector.d.ts.map +1 -1
  447. package/dist/modes/interactive/components/config-selector.js +13 -3
  448. package/dist/modes/interactive/components/config-selector.js.map +1 -1
  449. package/dist/modes/interactive/components/countdown-timer.d.ts.map +1 -1
  450. package/dist/modes/interactive/components/countdown-timer.js.map +1 -1
  451. package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -1
  452. package/dist/modes/interactive/components/custom-editor.js.map +1 -1
  453. package/dist/modes/interactive/components/custom-entry.d.ts.map +1 -1
  454. package/dist/modes/interactive/components/custom-entry.js.map +1 -1
  455. package/dist/modes/interactive/components/custom-message.d.ts.map +1 -1
  456. package/dist/modes/interactive/components/custom-message.js.map +1 -1
  457. package/dist/modes/interactive/components/daxnuts.d.ts.map +1 -1
  458. package/dist/modes/interactive/components/daxnuts.js.map +1 -1
  459. package/dist/modes/interactive/components/diff.d.ts.map +1 -1
  460. package/dist/modes/interactive/components/diff.js.map +1 -1
  461. package/dist/modes/interactive/components/dynamic-border.d.ts.map +1 -1
  462. package/dist/modes/interactive/components/dynamic-border.js.map +1 -1
  463. package/dist/modes/interactive/components/earendil-announcement.d.ts.map +1 -1
  464. package/dist/modes/interactive/components/earendil-announcement.js.map +1 -1
  465. package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -1
  466. package/dist/modes/interactive/components/extension-editor.js.map +1 -1
  467. package/dist/modes/interactive/components/extension-input.d.ts.map +1 -1
  468. package/dist/modes/interactive/components/extension-input.js.map +1 -1
  469. package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -1
  470. package/dist/modes/interactive/components/extension-selector.js.map +1 -1
  471. package/dist/modes/interactive/components/first-time-setup.d.ts +4 -4
  472. package/dist/modes/interactive/components/first-time-setup.d.ts.map +1 -1
  473. package/dist/modes/interactive/components/first-time-setup.js +8 -2
  474. package/dist/modes/interactive/components/first-time-setup.js.map +1 -1
  475. package/dist/modes/interactive/components/footer.d.ts +7 -0
  476. package/dist/modes/interactive/components/footer.d.ts.map +1 -1
  477. package/dist/modes/interactive/components/footer.js +43 -3
  478. package/dist/modes/interactive/components/footer.js.map +1 -1
  479. package/dist/modes/interactive/components/index.d.ts.map +1 -1
  480. package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -1
  481. package/dist/modes/interactive/components/keybinding-hints.js.map +1 -1
  482. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
  483. package/dist/modes/interactive/components/login-dialog.js.map +1 -1
  484. package/dist/modes/interactive/components/markdown-transform.d.ts.map +1 -1
  485. package/dist/modes/interactive/components/markdown-transform.js.map +1 -1
  486. package/dist/modes/interactive/components/mermaid.d.ts.map +1 -1
  487. package/dist/modes/interactive/components/mermaid.js.map +1 -1
  488. package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
  489. package/dist/modes/interactive/components/model-selector.js.map +1 -1
  490. package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
  491. package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
  492. package/dist/modes/interactive/components/pi-logo.d.ts +12 -0
  493. package/dist/modes/interactive/components/pi-logo.d.ts.map +1 -0
  494. package/dist/modes/interactive/components/pi-logo.js +25 -0
  495. package/dist/modes/interactive/components/pi-logo.js.map +1 -0
  496. package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -1
  497. package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -1
  498. package/dist/modes/interactive/components/session-selector-search.d.ts.map +1 -1
  499. package/dist/modes/interactive/components/session-selector-search.js.map +1 -1
  500. package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
  501. package/dist/modes/interactive/components/session-selector.js.map +1 -1
  502. package/dist/modes/interactive/components/settings-selector.d.ts +3 -1
  503. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  504. package/dist/modes/interactive/components/settings-selector.js +28 -6
  505. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  506. package/dist/modes/interactive/components/settings-submenu.d.ts.map +1 -1
  507. package/dist/modes/interactive/components/settings-submenu.js.map +1 -1
  508. package/dist/modes/interactive/components/show-images-selector.d.ts.map +1 -1
  509. package/dist/modes/interactive/components/show-images-selector.js.map +1 -1
  510. package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -1
  511. package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -1
  512. package/dist/modes/interactive/components/status-indicator.d.ts.map +1 -1
  513. package/dist/modes/interactive/components/status-indicator.js.map +1 -1
  514. package/dist/modes/interactive/components/theme-selector.d.ts.map +1 -1
  515. package/dist/modes/interactive/components/theme-selector.js.map +1 -1
  516. package/dist/modes/interactive/components/themed-text.d.ts +17 -0
  517. package/dist/modes/interactive/components/themed-text.d.ts.map +1 -0
  518. package/dist/modes/interactive/components/themed-text.js +29 -0
  519. package/dist/modes/interactive/components/themed-text.js.map +1 -0
  520. package/dist/modes/interactive/components/thinking-selector.d.ts.map +1 -1
  521. package/dist/modes/interactive/components/thinking-selector.js.map +1 -1
  522. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  523. package/dist/modes/interactive/components/tool-execution.js +2 -2
  524. package/dist/modes/interactive/components/tool-execution.js.map +1 -1
  525. package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -1
  526. package/dist/modes/interactive/components/tree-selector.js.map +1 -1
  527. package/dist/modes/interactive/components/trust-selector.d.ts.map +1 -1
  528. package/dist/modes/interactive/components/trust-selector.js.map +1 -1
  529. package/dist/modes/interactive/components/user-message-selector.d.ts.map +1 -1
  530. package/dist/modes/interactive/components/user-message-selector.js.map +1 -1
  531. package/dist/modes/interactive/components/user-message.d.ts.map +1 -1
  532. package/dist/modes/interactive/components/user-message.js.map +1 -1
  533. package/dist/modes/interactive/components/visual-truncate.d.ts.map +1 -1
  534. package/dist/modes/interactive/components/visual-truncate.js.map +1 -1
  535. package/dist/modes/interactive/external-editor.d.ts.map +1 -1
  536. package/dist/modes/interactive/external-editor.js.map +1 -1
  537. package/dist/modes/interactive/interactive-mode.d.ts +11 -0
  538. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  539. package/dist/modes/interactive/interactive-mode.js +199 -161
  540. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  541. package/dist/modes/interactive/model-catalog-refresh.d.ts.map +1 -1
  542. package/dist/modes/interactive/model-catalog-refresh.js.map +1 -1
  543. package/dist/modes/interactive/model-search.d.ts.map +1 -1
  544. package/dist/modes/interactive/model-search.js.map +1 -1
  545. package/dist/modes/interactive/session-share.d.ts.map +1 -1
  546. package/dist/modes/interactive/session-share.js.map +1 -1
  547. package/dist/modes/interactive/theme/dark.json +59 -72
  548. package/dist/modes/interactive/theme/light.json +57 -69
  549. package/dist/modes/interactive/theme/system-theme.d.ts +54 -0
  550. package/dist/modes/interactive/theme/system-theme.d.ts.map +1 -0
  551. package/dist/modes/interactive/theme/system-theme.js +535 -0
  552. package/dist/modes/interactive/theme/system-theme.js.map +1 -0
  553. package/dist/modes/interactive/theme/theme-controller.d.ts +46 -6
  554. package/dist/modes/interactive/theme/theme-controller.d.ts.map +1 -1
  555. package/dist/modes/interactive/theme/theme-controller.js +127 -47
  556. package/dist/modes/interactive/theme/theme-controller.js.map +1 -1
  557. package/dist/modes/interactive/theme/theme-json.d.ts +1 -0
  558. package/dist/modes/interactive/theme/theme-json.d.ts.map +1 -1
  559. package/dist/modes/interactive/theme/theme-json.js +3 -1
  560. package/dist/modes/interactive/theme/theme-json.js.map +1 -1
  561. package/dist/modes/interactive/theme/theme-schema.json +7 -2
  562. package/dist/modes/interactive/theme/theme.d.ts +66 -39
  563. package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
  564. package/dist/modes/interactive/theme/theme.js +238 -317
  565. package/dist/modes/interactive/theme/theme.js.map +1 -1
  566. package/dist/modes/interactive/tui-renderer.d.ts +2 -1
  567. package/dist/modes/interactive/tui-renderer.d.ts.map +1 -1
  568. package/dist/modes/interactive/tui-renderer.js +1 -0
  569. package/dist/modes/interactive/tui-renderer.js.map +1 -1
  570. package/dist/modes/json-event.d.ts.map +1 -1
  571. package/dist/modes/json-event.js.map +1 -1
  572. package/dist/modes/print-mode.d.ts.map +1 -1
  573. package/dist/modes/print-mode.js.map +1 -1
  574. package/dist/modes/rpc/jsonl.d.ts.map +1 -1
  575. package/dist/modes/rpc/jsonl.js.map +1 -1
  576. package/dist/modes/rpc/rpc-client.d.ts +6 -6
  577. package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  578. package/dist/modes/rpc/rpc-client.js +12 -8
  579. package/dist/modes/rpc/rpc-client.js.map +1 -1
  580. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  581. package/dist/modes/rpc/rpc-mode.js +7 -9
  582. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  583. package/dist/modes/rpc/rpc-types.d.ts +10 -1
  584. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  585. package/dist/modes/rpc/rpc-types.js.map +1 -1
  586. package/dist/package-manager-cli.d.ts.map +1 -1
  587. package/dist/package-manager-cli.js +6 -2
  588. package/dist/package-manager-cli.js.map +1 -1
  589. package/dist/rpc-entry.d.ts.map +1 -1
  590. package/dist/rpc-entry.js.map +1 -1
  591. package/dist/utils/abort.d.ts.map +1 -1
  592. package/dist/utils/abort.js.map +1 -1
  593. package/dist/utils/ansi.d.ts.map +1 -1
  594. package/dist/utils/ansi.js.map +1 -1
  595. package/dist/utils/changelog.d.ts.map +1 -1
  596. package/dist/utils/changelog.js.map +1 -1
  597. package/dist/utils/child-process.d.ts.map +1 -1
  598. package/dist/utils/child-process.js.map +1 -1
  599. package/dist/utils/clipboard-command.d.ts.map +1 -1
  600. package/dist/utils/clipboard-command.js.map +1 -1
  601. package/dist/utils/clipboard-image.d.ts.map +1 -1
  602. package/dist/utils/clipboard-image.js +14 -17
  603. package/dist/utils/clipboard-image.js.map +1 -1
  604. package/dist/utils/clipboard.d.ts +2 -0
  605. package/dist/utils/clipboard.d.ts.map +1 -1
  606. package/dist/utils/clipboard.js +5 -0
  607. package/dist/utils/clipboard.js.map +1 -1
  608. package/dist/utils/deprecation.d.ts.map +1 -1
  609. package/dist/utils/deprecation.js.map +1 -1
  610. package/dist/utils/exif-orientation.d.ts.map +1 -1
  611. package/dist/utils/exif-orientation.js.map +1 -1
  612. package/dist/utils/frontmatter.d.ts.map +1 -1
  613. package/dist/utils/frontmatter.js.map +1 -1
  614. package/dist/utils/fs-watch.d.ts.map +1 -1
  615. package/dist/utils/fs-watch.js.map +1 -1
  616. package/dist/utils/git.d.ts.map +1 -1
  617. package/dist/utils/git.js.map +1 -1
  618. package/dist/utils/html.d.ts.map +1 -1
  619. package/dist/utils/html.js.map +1 -1
  620. package/dist/utils/image-convert.d.ts.map +1 -1
  621. package/dist/utils/image-convert.js.map +1 -1
  622. package/dist/utils/image-process.d.ts.map +1 -1
  623. package/dist/utils/image-process.js.map +1 -1
  624. package/dist/utils/image-resize-core.d.ts.map +1 -1
  625. package/dist/utils/image-resize-core.js.map +1 -1
  626. package/dist/utils/image-resize-worker.d.ts.map +1 -1
  627. package/dist/utils/image-resize-worker.js.map +1 -1
  628. package/dist/utils/image-resize.d.ts.map +1 -1
  629. package/dist/utils/image-resize.js.map +1 -1
  630. package/dist/utils/json.d.ts.map +1 -1
  631. package/dist/utils/json.js.map +1 -1
  632. package/dist/utils/management-http.d.ts.map +1 -1
  633. package/dist/utils/management-http.js.map +1 -1
  634. package/dist/utils/mime.d.ts.map +1 -1
  635. package/dist/utils/mime.js.map +1 -1
  636. package/dist/utils/open-browser.d.ts.map +1 -1
  637. package/dist/utils/open-browser.js.map +1 -1
  638. package/dist/utils/paths.d.ts +3 -3
  639. package/dist/utils/paths.d.ts.map +1 -1
  640. package/dist/utils/paths.js +5 -4
  641. package/dist/utils/paths.js.map +1 -1
  642. package/dist/utils/photon.d.ts.map +1 -1
  643. package/dist/utils/photon.js.map +1 -1
  644. package/dist/utils/pi-user-agent.d.ts.map +1 -1
  645. package/dist/utils/pi-user-agent.js.map +1 -1
  646. package/dist/utils/shell.d.ts +1 -3
  647. package/dist/utils/shell.d.ts.map +1 -1
  648. package/dist/utils/shell.js +3 -30
  649. package/dist/utils/shell.js.map +1 -1
  650. package/dist/utils/sleep.d.ts.map +1 -1
  651. package/dist/utils/sleep.js.map +1 -1
  652. package/dist/utils/syntax-highlight.d.ts.map +1 -1
  653. package/dist/utils/syntax-highlight.js +2 -0
  654. package/dist/utils/syntax-highlight.js.map +1 -1
  655. package/dist/utils/text.d.ts.map +1 -1
  656. package/dist/utils/text.js.map +1 -1
  657. package/dist/utils/tool-result-images.d.ts.map +1 -1
  658. package/dist/utils/tool-result-images.js.map +1 -1
  659. package/dist/utils/tools-manager.d.ts.map +1 -1
  660. package/dist/utils/tools-manager.js.map +1 -1
  661. package/dist/utils/version-check.d.ts.map +1 -1
  662. package/dist/utils/version-check.js.map +1 -1
  663. package/dist/utils/windows-self-update.d.ts.map +1 -1
  664. package/dist/utils/windows-self-update.js.map +1 -1
  665. package/dist/utils/wsl.d.ts.map +1 -1
  666. package/dist/utils/wsl.js.map +1 -1
  667. package/dist/utils/zip.d.ts.map +1 -1
  668. package/dist/utils/zip.js.map +1 -1
  669. package/docs/cli.md +64 -3
  670. package/docs/configuration.md +2 -0
  671. package/docs/custom-provider.md +43 -4
  672. package/docs/docs.json +8 -0
  673. package/docs/extensions.md +55 -0
  674. package/docs/keybindings.md +1 -1
  675. package/docs/llama-cpp.md +13 -0
  676. package/docs/mcp.md +188 -0
  677. package/docs/models.md +35 -0
  678. package/docs/packages.md +4 -2
  679. package/docs/providers.md +1 -0
  680. package/docs/rpc-commands.md +9 -3
  681. package/docs/rpc.md +3 -1
  682. package/docs/sdk.md +7 -2
  683. package/docs/security.md +2 -0
  684. package/docs/session-format.md +5 -1
  685. package/docs/settings.md +20 -3
  686. package/docs/themes.md +33 -7
  687. package/docs/tui.md +18 -0
  688. package/docs/virtual-models.md +114 -0
  689. package/docs/windows.md +2 -0
  690. package/examples/README.md +1 -1
  691. package/examples/extensions/README.md +2 -0
  692. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  693. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  694. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  695. package/examples/extensions/custom-provider-gitlab-duo/test.ts +5 -5
  696. package/examples/extensions/debug-provider.ts +90 -0
  697. package/examples/extensions/gondolin/package-lock.json +2 -2
  698. package/examples/extensions/gondolin/package.json +1 -1
  699. package/examples/extensions/jev-router.ts +113 -0
  700. package/examples/extensions/sandbox/package-lock.json +2 -2
  701. package/examples/extensions/sandbox/package.json +1 -1
  702. package/examples/extensions/with-deps/package-lock.json +2 -2
  703. package/examples/extensions/with-deps/package.json +1 -1
  704. package/examples/rpc-client.ts +1 -1
  705. package/examples/rpc-extension-ui.ts +1 -1
  706. package/examples/sdk/14-codemode-mcp.ts +61 -0
  707. package/examples/sdk/README.md +2 -1
  708. package/npm-shrinkwrap.json +89 -39
  709. package/package.json +14 -12
  710. package/dist/bundle/chunks/anthropic-messages-J5WXPPPC.js +0 -6
  711. package/dist/bundle/chunks/azure-openai-responses-HUSQ3GP2.js +0 -2
  712. package/dist/bundle/chunks/chunk-65HAU2C5.js +0 -2
  713. package/dist/bundle/chunks/chunk-6R7UI4IY.js +0 -2
  714. package/dist/bundle/chunks/chunk-NUHFSC37.js +0 -25
  715. package/dist/bundle/chunks/chunk-OJP47DM6.js +0 -1568
  716. package/dist/bundle/chunks/chunk-RCZIEVGO.js +0 -11
  717. package/dist/bundle/chunks/google-generative-ai-IMS5EK2Y.js +0 -2
  718. package/dist/bundle/chunks/google-vertex-O4YJEQUW.js +0 -2
  719. package/dist/bundle/chunks/mistral-conversations-NOSQHBEY.js +0 -5
  720. package/dist/bundle/chunks/openai-responses-HWEUZ6WZ.js +0 -2
  721. package/dist/bundle/chunks/virtual-modules-VHMJYYWQ.js +0 -2
@@ -1 +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"]}
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;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B,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;CACnB;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,sBAAsB,CAsBxF"}
@@ -212,4 +212,33 @@ export function truncateLine(line, maxChars = GREP_MAX_LINE_LENGTH) {
212
212
  }
213
213
  return { text: `${line.slice(0, maxChars)}... [truncated]`, wasTruncated: true };
214
214
  }
215
+ /**
216
+ * Keep the start and the end of `content`, half of `maxBytes` each, and replace the middle with a
217
+ * `…N chars truncated…` marker, like Codex does for tool output. Cuts only at character boundaries.
218
+ */
219
+ export function truncateMiddle(content, maxBytes) {
220
+ const buf = Buffer.from(content, "utf-8");
221
+ const totalLines = splitLinesForCounting(content).length;
222
+ if (buf.length <= maxBytes) {
223
+ return { content, truncated: false, removedChars: 0, totalBytes: buf.length, totalLines };
224
+ }
225
+ // Continuation bytes (10xxxxxx) are not character starts.
226
+ const isBoundary = (index) => index >= buf.length || (buf[index] & 0xc0) !== 0x80;
227
+ let headEnd = Math.floor(maxBytes / 2);
228
+ while (headEnd > 0 && !isBoundary(headEnd))
229
+ headEnd--;
230
+ let tailStart = buf.length - (maxBytes - Math.floor(maxBytes / 2));
231
+ while (tailStart < buf.length && !isBoundary(tailStart))
232
+ tailStart++;
233
+ const head = buf.subarray(0, headEnd).toString("utf-8");
234
+ const tail = buf.subarray(tailStart).toString("utf-8");
235
+ const removedChars = Array.from(buf.subarray(headEnd, tailStart).toString("utf-8")).length;
236
+ return {
237
+ content: `${head}…${removedChars} chars truncated…${tail}`,
238
+ truncated: true,
239
+ removedChars,
240
+ totalBytes: buf.length,
241
+ totalLines,
242
+ };
243
+ }
215
244
  //# sourceMappingURL=truncate.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"truncate.js","sourceRoot":"","sources":["../../../src/core/tools/truncate.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,CAAC,MAA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,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"]}
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;IAC7C,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,EAAE,CAAC;IACX,CAAC;IAC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AAQ,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACvD,MAAM,YAAY,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC;IAC3F,OAAO;QACN,OAAO,EAAE,GAAG,IAAI,IAAI,YAAY,oBAAoB,IAAI,EAAE;QAC1D,SAAS,EAAE,IAAI;QACf,YAAY;QACZ,UAAU,EAAE,GAAG,CAAC,MAAM;QACtB,UAAU;KACV,CAAC;AACH,CAAC","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\nexport interface MiddleTruncationResult {\n\t/** The start and end of the content with a `…N chars truncated…` marker between them. */\n\tcontent: string;\n\ttruncated: boolean;\n\t/** Characters left out. */\n\tremovedChars: number;\n\ttotalBytes: number;\n\ttotalLines: number;\n}\n\n/**\n * Keep the start and the end of `content`, half of `maxBytes` each, and replace the middle with a\n * `…N chars truncated…` marker, like Codex does for tool output. Cuts only at character boundaries.\n */\nexport function truncateMiddle(content: string, maxBytes: number): MiddleTruncationResult {\n\tconst buf = Buffer.from(content, \"utf-8\");\n\tconst totalLines = splitLinesForCounting(content).length;\n\tif (buf.length <= maxBytes) {\n\t\treturn { content, truncated: false, removedChars: 0, totalBytes: buf.length, totalLines };\n\t}\n\t// Continuation bytes (10xxxxxx) are not character starts.\n\tconst isBoundary = (index: number) => index >= buf.length || (buf[index] & 0xc0) !== 0x80;\n\tlet headEnd = Math.floor(maxBytes / 2);\n\twhile (headEnd > 0 && !isBoundary(headEnd)) headEnd--;\n\tlet tailStart = buf.length - (maxBytes - Math.floor(maxBytes / 2));\n\twhile (tailStart < buf.length && !isBoundary(tailStart)) tailStart++;\n\tconst head = buf.subarray(0, headEnd).toString(\"utf-8\");\n\tconst tail = buf.subarray(tailStart).toString(\"utf-8\");\n\tconst removedChars = Array.from(buf.subarray(headEnd, tailStart).toString(\"utf-8\")).length;\n\treturn {\n\t\tcontent: `${head}…${removedChars} chars truncated…${tail}`,\n\t\ttruncated: true,\n\t\tremovedChars,\n\t\ttotalBytes: buf.length,\n\t\ttotalLines,\n\t};\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"write.d.ts","sourceRoot":"","sources":["../../../src/core/tools/write.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAG/D,OAAO,EAAE,KAAK,MAAM,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAoB,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAM/E,QAAA,MAAM,WAAW;;;EAGf,CAAC;AAEH,eAAO,MAAM,iCAAiC;;;CAGpC,CAAC;AAEX,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;AAED,wBAAgB,yBAAyB,CACxC,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,gBAAgB,GACxB,cAAc,CAAC,OAAO,WAAW,EAAE,SAAS,CAAC,CA8C/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 \"@earendil-works/pi-agent-core\";\nimport { mkdir as fsMkdir, writeFile as fsWriteFile } from \"fs/promises\";\nimport { dirname } from \"path\";\nimport { type Static, Type } from \"typebox\";\nimport type { ExtensionContext, ToolDefinition } from \"../extensions/types.ts\";\nimport { withFileMutationQueue } from \"./file-mutation-queue.ts\";\nimport { resolveToCwd } from \"./path-utils.ts\";\nimport { writeRenderers } from \"./renderers/write.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 const writeToolSystemPromptContribution = {\n\tsnippet: \"Create or overwrite files\",\n\tguidelines: [\"Use write only for new files or complete rewrites.\"],\n} as const;\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\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: writeToolSystemPromptContribution.snippet,\n\t\tpromptGuidelines: [...writeToolSystemPromptContribution.guidelines],\n\t\tparameters: writeSchema,\n\t\tconstrainedSampling: { type: \"json_schema\", strict: \"prefer\" },\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\tctx?: ExtensionContext,\n\t\t) {\n\t\t\tconst absolutePath = resolveToCwd(path, ctx?.cwd || 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 to ${path}` }],\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t});\n\t\t},\n\t\t...writeRenderers,\n\t};\n}\n\nexport function createWriteTool(cwd: string, options?: WriteToolOptions): AgentTool<typeof writeSchema> {\n\treturn wrapToolDefinition(createWriteToolDefinition(cwd, options));\n}\n"]}
1
+ {"version":3,"file":"write.d.ts","sourceRoot":"","sources":["../../../src/core/tools/write.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAG/D,OAAO,EAAE,KAAK,MAAM,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAoB,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAM/E,QAAA,MAAM,WAAW;;;EAGf,CAAC;AAEH,eAAO,MAAM,iCAAiC;aAC7C,OAAO,EAAE,2BAA2B;aACpC,UAAU,YAAG,oDAAoD;CACxD,CAAC;AAEX,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;AAED,wBAAgB,yBAAyB,CACxC,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,gBAAgB,GACxB,cAAc,CAAC,OAAO,WAAW,EAAE,SAAS,CAAC,CA8C/C;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,SAAS,CAAC,OAAO,WAAW,CAAC,CAEtG"}
@@ -1 +1 @@
1
- {"version":3,"file":"write.js","sourceRoot":"","sources":["../../../src/core/tools/write.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,OAAO,EAAE,SAAS,IAAI,WAAW,EAAE,M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AAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;AAAA,CACnE","sourcesContent":["import type { AgentTool } from \"@earendil-works/pi-agent-core\";\nimport { mkdir as fsMkdir, writeFile as fsWriteFile } from \"fs/promises\";\nimport { dirname } from \"path\";\nimport { type Static, Type } from \"typebox\";\nimport type { ExtensionContext, ToolDefinition } from \"../extensions/types.ts\";\nimport { withFileMutationQueue } from \"./file-mutation-queue.ts\";\nimport { resolveToCwd } from \"./path-utils.ts\";\nimport { writeRenderers } from \"./renderers/write.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 const writeToolSystemPromptContribution = {\n\tsnippet: \"Create or overwrite files\",\n\tguidelines: [\"Use write only for new files or complete rewrites.\"],\n} as const;\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\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: writeToolSystemPromptContribution.snippet,\n\t\tpromptGuidelines: [...writeToolSystemPromptContribution.guidelines],\n\t\tparameters: writeSchema,\n\t\tconstrainedSampling: { type: \"json_schema\", strict: \"prefer\" },\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\tctx?: ExtensionContext,\n\t\t) {\n\t\t\tconst absolutePath = resolveToCwd(path, ctx?.cwd || 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 to ${path}` }],\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t});\n\t\t},\n\t\t...writeRenderers,\n\t};\n}\n\nexport function createWriteTool(cwd: string, options?: WriteToolOptions): AgentTool<typeof writeSchema> {\n\treturn wrapToolDefinition(createWriteToolDefinition(cwd, options));\n}\n"]}
1
+ {"version":3,"file":"write.js","sourceRoot":"","sources":["../../../src/core/tools/write.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,OAAO,EAAE,SAAS,IAAI,WAAW,EAAE,MAAM,aAAa,CAAC;AACzE,OAAO,EAAE,OAAO,EAAE,M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il-works/pi-agent-core\";\nimport { mkdir as fsMkdir, writeFile as fsWriteFile } from \"fs/promises\";\nimport { dirname } from \"path\";\nimport { type Static, Type } from \"typebox\";\nimport type { ExtensionContext, ToolDefinition } from \"../extensions/types.ts\";\nimport { withFileMutationQueue } from \"./file-mutation-queue.ts\";\nimport { resolveToCwd } from \"./path-utils.ts\";\nimport { writeRenderers } from \"./renderers/write.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 const writeToolSystemPromptContribution = {\n\tsnippet: \"Create or overwrite files\",\n\tguidelines: [\"Use write only for new files or complete rewrites.\"],\n} as const;\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\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: writeToolSystemPromptContribution.snippet,\n\t\tpromptGuidelines: [...writeToolSystemPromptContribution.guidelines],\n\t\tparameters: writeSchema,\n\t\tconstrainedSampling: { type: \"json_schema\", strict: \"prefer\" },\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\tctx?: ExtensionContext,\n\t\t) {\n\t\t\tconst absolutePath = resolveToCwd(path, ctx?.cwd || 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 to ${path}` }],\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t});\n\t\t},\n\t\t...writeRenderers,\n\t};\n}\n\nexport function createWriteTool(cwd: string, options?: WriteToolOptions): AgentTool<typeof writeSchema> {\n\treturn wrapToolDefinition(createWriteToolDefinition(cwd, options));\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"trust-manager.d.ts","sourceRoot":"","sources":["../../src/core/trust-manager.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,oBAAoB,GAAG,OAAO,GAAG,IAAI,CAAC;AAElD,MAAM,WAAW,sBAAsB;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,kBAAkB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,oBAAoB,CAAC;CAC/B;AAED,MAAM,WAAW,kBAAkB;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,kBAAkB,EAAE,CAAC;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB;AAkCD,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAIzE;AAED,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;IAAE,kBAAkB,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,kBAAkB,EAAE,CA8BpH;AAkFD;;;;;;GAMG;AACH,wBAAgB,iCAAiC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAsBtE;AAED,qBAAa,iBAAiB;IAC7B,OAAO,CAAC,SAAS,CAAS;IAE1B,YAAY,QAAQ,EAAE,MAAM,EAE3B;IAED,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,oBAAoB,CAErC;IAED,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,sBAAsB,GAAG,IAAI,CAKnD;IAED,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,GAAG,IAAI,CAErD;IAED,OAAO,CAAC,SAAS,EAAE,kBAAkB,EAAE,GAAG,IAAI,CAa7C;CACD","sourcesContent":["import { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport lockfile from \"proper-lockfile\";\nimport { CONFIG_DIR_NAME } from \"../config.ts\";\nimport { canonicalizePath, resolvePath } from \"../utils/paths.ts\";\nimport { stripBom } from \"../utils/text.ts\";\n\nexport type ProjectTrustDecision = boolean | null;\n\nexport interface ProjectTrustStoreEntry {\n\tpath: string;\n\tdecision: boolean;\n}\n\nexport interface ProjectTrustUpdate {\n\tpath: string;\n\tdecision: ProjectTrustDecision;\n}\n\nexport interface ProjectTrustOption {\n\tlabel: string;\n\ttrusted: boolean;\n\tupdates: ProjectTrustUpdate[];\n\tsavedPath?: string;\n}\n\ntype TrustFile = Record<string, boolean | null | undefined>;\n\nconst TRUST_REQUIRING_PROJECT_CONFIG_RESOURCES = [\n\t\"settings.json\",\n\t\"extensions\",\n\t\"skills\",\n\t\"prompts\",\n\t\"themes\",\n\t\"SYSTEM.md\",\n\t\"APPEND_SYSTEM.md\",\n] as const;\n\nfunction normalizeCwd(cwd: string): string {\n\treturn canonicalizePath(resolvePath(cwd));\n}\n\nfunction findNearestTrustEntry(data: TrustFile, cwd: string): ProjectTrustStoreEntry | null {\n\tlet currentDir = normalizeCwd(cwd);\n\twhile (true) {\n\t\tconst value = data[currentDir];\n\t\tif (value === true || value === false) {\n\t\t\treturn { path: currentDir, decision: value };\n\t\t}\n\n\t\tconst parentDir = dirname(currentDir);\n\t\tif (parentDir === currentDir) {\n\t\t\treturn null;\n\t\t}\n\t\tcurrentDir = parentDir;\n\t}\n}\n\nexport function getProjectTrustParentPath(cwd: string): string | undefined {\n\tconst trustPath = normalizeCwd(cwd);\n\tconst parentDir = dirname(trustPath);\n\treturn parentDir === trustPath ? undefined : parentDir;\n}\n\nexport function getProjectTrustOptions(cwd: string, options?: { includeSessionOnly?: boolean }): ProjectTrustOption[] {\n\tconst trustPath = normalizeCwd(cwd);\n\tconst trustOptions: ProjectTrustOption[] = [\n\t\t{ label: \"Trust\", trusted: true, updates: [{ path: trustPath, decision: true }], savedPath: trustPath },\n\t];\n\tconst parentPath = getProjectTrustParentPath(cwd);\n\tif (parentPath !== undefined) {\n\t\ttrustOptions.push({\n\t\t\tlabel: `Trust parent folder (${parentPath})`,\n\t\t\ttrusted: true,\n\t\t\tupdates: [\n\t\t\t\t{ path: parentPath, decision: true },\n\t\t\t\t{ path: trustPath, decision: null },\n\t\t\t],\n\t\t\tsavedPath: parentPath,\n\t\t});\n\t}\n\tif (options?.includeSessionOnly) {\n\t\ttrustOptions.push({ label: \"Trust (this session only)\", trusted: true, updates: [] });\n\t}\n\ttrustOptions.push({\n\t\tlabel: \"Do not trust\",\n\t\ttrusted: false,\n\t\tupdates: [{ path: trustPath, decision: false }],\n\t\tsavedPath: trustPath,\n\t});\n\tif (options?.includeSessionOnly) {\n\t\ttrustOptions.push({ label: \"Do not trust (this session only)\", trusted: false, updates: [] });\n\t}\n\treturn trustOptions;\n}\n\nfunction readTrustFile(path: string): TrustFile {\n\tif (!existsSync(path)) {\n\t\treturn {};\n\t}\n\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(stripBom(readFileSync(path, \"utf-8\")));\n\t} catch (error) {\n\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\tthrow new Error(`Failed to read trust store ${path}: ${message}`);\n\t}\n\n\tif (typeof parsed !== \"object\" || parsed === null || Array.isArray(parsed)) {\n\t\tthrow new Error(`Invalid trust store ${path}: expected an object`);\n\t}\n\n\tconst data: TrustFile = {};\n\tfor (const [key, value] of Object.entries(parsed)) {\n\t\tif (value !== true && value !== false && value !== null) {\n\t\t\tthrow new Error(`Invalid trust store ${path}: value for ${JSON.stringify(key)} must be true, false, or null`);\n\t\t}\n\t\tdata[key] = value;\n\t}\n\treturn data;\n}\n\nfunction writeTrustFile(path: string, data: TrustFile): void {\n\tconst sorted: TrustFile = {};\n\tfor (const key of Object.keys(data).sort()) {\n\t\tconst value = data[key];\n\t\tif (value === true || value === false || value === null) {\n\t\t\tsorted[key] = value;\n\t\t}\n\t}\n\tmkdirSync(dirname(path), { recursive: true });\n\twriteFileSync(path, `${JSON.stringify(sorted, null, 2)}\\n`, \"utf-8\");\n}\n\nfunction acquireTrustLockSync(path: string): () => void {\n\tconst trustDir = dirname(path);\n\tmkdirSync(trustDir, { recursive: true });\n\tconst maxAttempts = 10;\n\tconst delayMs = 20;\n\tlet lastError: unknown;\n\n\tfor (let attempt = 1; attempt <= maxAttempts; attempt++) {\n\t\ttry {\n\t\t\treturn lockfile.lockSync(trustDir, { realpath: false, lockfilePath: `${path}.lock` });\n\t\t} catch (error) {\n\t\t\tconst code =\n\t\t\t\ttypeof error === \"object\" && error !== null && \"code\" in error\n\t\t\t\t\t? String((error as { code?: unknown }).code)\n\t\t\t\t\t: undefined;\n\t\t\tif (code !== \"ELOCKED\" || attempt === maxAttempts) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tlastError = error;\n\t\t\tconst start = Date.now();\n\t\t\twhile (Date.now() - start < delayMs) {\n\t\t\t\t// Sleep synchronously to avoid changing trust store callers to async.\n\t\t\t}\n\t\t}\n\t}\n\n\tif (lastError instanceof Error) {\n\t\tthrow lastError;\n\t}\n\tthrow new Error(\"Failed to acquire trust store lock\");\n}\n\nfunction withTrustFileLock<T>(path: string, fn: () => T): T {\n\tconst release = acquireTrustLockSync(path);\n\ttry {\n\t\treturn fn();\n\t} finally {\n\t\trelease();\n\t}\n}\n\n/**\n * Returns true when cwd has project-local resources that must be gated by\n * project trust: trust-requiring entries under cwd/.pi, or .agents/skills in\n * cwd or one of its ancestors. Returns false when no such project resources\n * exist. The user/global ~/.agents/skills directory is always treated as a\n * trusted user resource and is ignored here, even when cwd is $HOME.\n */\nexport function hasTrustRequiringProjectResources(cwd: string): boolean {\n\tconst homeDir = canonicalizePath(resolvePath(process.env.HOME || homedir()));\n\tconst userAgentsSkillsDir = join(homeDir, \".agents\", \"skills\");\n\tlet currentDir = canonicalizePath(resolvePath(cwd));\n\n\tconst configDir = join(currentDir, CONFIG_DIR_NAME);\n\tif (TRUST_REQUIRING_PROJECT_CONFIG_RESOURCES.some((entry) => existsSync(join(configDir, entry)))) {\n\t\treturn true;\n\t}\n\n\twhile (true) {\n\t\tconst agentsSkillsDir = join(currentDir, \".agents\", \"skills\");\n\t\tif (agentsSkillsDir !== userAgentsSkillsDir && existsSync(agentsSkillsDir)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tconst parentDir = dirname(currentDir);\n\t\tif (parentDir === currentDir) {\n\t\t\treturn false;\n\t\t}\n\t\tcurrentDir = parentDir;\n\t}\n}\n\nexport class ProjectTrustStore {\n\tprivate trustPath: string;\n\n\tconstructor(agentDir: string) {\n\t\tthis.trustPath = join(resolvePath(agentDir), \"trust.json\");\n\t}\n\n\tget(cwd: string): ProjectTrustDecision {\n\t\treturn this.getEntry(cwd)?.decision ?? null;\n\t}\n\n\tgetEntry(cwd: string): ProjectTrustStoreEntry | null {\n\t\treturn withTrustFileLock(this.trustPath, () => {\n\t\t\tconst data = readTrustFile(this.trustPath);\n\t\t\treturn findNearestTrustEntry(data, cwd);\n\t\t});\n\t}\n\n\tset(cwd: string, decision: ProjectTrustDecision): void {\n\t\tthis.setMany([{ path: cwd, decision }]);\n\t}\n\n\tsetMany(decisions: ProjectTrustUpdate[]): void {\n\t\twithTrustFileLock(this.trustPath, () => {\n\t\t\tconst data = readTrustFile(this.trustPath);\n\t\t\tfor (const { path, decision } of decisions) {\n\t\t\t\tconst key = normalizeCwd(path);\n\t\t\t\tif (decision === null) {\n\t\t\t\t\tdelete data[key];\n\t\t\t\t} else {\n\t\t\t\t\tdata[key] = decision;\n\t\t\t\t}\n\t\t\t}\n\t\t\twriteTrustFile(this.trustPath, data);\n\t\t});\n\t}\n}\n"]}
1
+ {"version":3,"file":"trust-manager.d.ts","sourceRoot":"","sources":["../../src/core/trust-manager.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,oBAAoB,GAAG,OAAO,GAAG,IAAI,CAAC;AAElD,MAAM,WAAW,sBAAsB;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,kBAAkB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,oBAAoB,CAAC;CAC/B;AAED,MAAM,WAAW,kBAAkB;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,kBAAkB,EAAE,CAAC;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB;AAmCD,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAIzE;AAED,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;IAAE,kBAAkB,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,kBAAkB,EAAE,CA8BpH;AAkFD;;;;;;GAMG;AACH,wBAAgB,iCAAiC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAsBtE;AAED,qBAAa,iBAAiB;IAC7B,OAAO,CAAC,SAAS,CAAS;IAE1B,YAAY,QAAQ,EAAE,MAAM,EAE3B;IAED,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,oBAAoB,CAErC;IAED,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,sBAAsB,GAAG,IAAI,CAKnD;IAED,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,GAAG,IAAI,CAErD;IAED,OAAO,CAAC,SAAS,EAAE,kBAAkB,EAAE,GAAG,IAAI,CAa7C;CACD"}
@@ -7,6 +7,7 @@ import { canonicalizePath, resolvePath } from "../utils/paths.js";
7
7
  import { stripBom } from "../utils/text.js";
8
8
  const TRUST_REQUIRING_PROJECT_CONFIG_RESOURCES = [
9
9
  "settings.json",
10
+ "mcp.json",
10
11
  "extensions",
11
12
  "skills",
12
13
  "prompts",
@@ -1 +1 @@
1
- {"version":3,"file":"trust-manager.js","sourceRoot":"","sources":["../../src/core/trust-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,QAAQ,MAA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;IAED,OAAO,CAAC,SAA+B,EAAQ;QAC9C,iBAAiB,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC;YACvC,MAAM,IAAI,GAAG,aAAa,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YAC3C,KAAK,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,SAAS,EAAE,CAAC;gBAC5C,MAAM,GAAG,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;gBAC/B,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;oBACvB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;gBAClB,CAAC;qBAAM,CAAC;oBACP,IAAI,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC;gBACtB,CAAC;YACF,CAAC;YACD,cAAc,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QAAA,CACrC,CAAC,CAAC;IAAA,CACH;CACD","sourcesContent":["import { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport lockfile from \"proper-lockfile\";\nimport { CONFIG_DIR_NAME } from \"../config.ts\";\nimport { canonicalizePath, resolvePath } from \"../utils/paths.ts\";\nimport { stripBom } from \"../utils/text.ts\";\n\nexport type ProjectTrustDecision = boolean | null;\n\nexport interface ProjectTrustStoreEntry {\n\tpath: string;\n\tdecision: boolean;\n}\n\nexport interface ProjectTrustUpdate {\n\tpath: string;\n\tdecision: ProjectTrustDecision;\n}\n\nexport interface ProjectTrustOption {\n\tlabel: string;\n\ttrusted: boolean;\n\tupdates: ProjectTrustUpdate[];\n\tsavedPath?: string;\n}\n\ntype TrustFile = Record<string, boolean | null | undefined>;\n\nconst TRUST_REQUIRING_PROJECT_CONFIG_RESOURCES = [\n\t\"settings.json\",\n\t\"extensions\",\n\t\"skills\",\n\t\"prompts\",\n\t\"themes\",\n\t\"SYSTEM.md\",\n\t\"APPEND_SYSTEM.md\",\n] as const;\n\nfunction normalizeCwd(cwd: string): string {\n\treturn canonicalizePath(resolvePath(cwd));\n}\n\nfunction findNearestTrustEntry(data: TrustFile, cwd: string): ProjectTrustStoreEntry | null {\n\tlet currentDir = normalizeCwd(cwd);\n\twhile (true) {\n\t\tconst value = data[currentDir];\n\t\tif (value === true || value === false) {\n\t\t\treturn { path: currentDir, decision: value };\n\t\t}\n\n\t\tconst parentDir = dirname(currentDir);\n\t\tif (parentDir === currentDir) {\n\t\t\treturn null;\n\t\t}\n\t\tcurrentDir = parentDir;\n\t}\n}\n\nexport function getProjectTrustParentPath(cwd: string): string | undefined {\n\tconst trustPath = normalizeCwd(cwd);\n\tconst parentDir = dirname(trustPath);\n\treturn parentDir === trustPath ? undefined : parentDir;\n}\n\nexport function getProjectTrustOptions(cwd: string, options?: { includeSessionOnly?: boolean }): ProjectTrustOption[] {\n\tconst trustPath = normalizeCwd(cwd);\n\tconst trustOptions: ProjectTrustOption[] = [\n\t\t{ label: \"Trust\", trusted: true, updates: [{ path: trustPath, decision: true }], savedPath: trustPath },\n\t];\n\tconst parentPath = getProjectTrustParentPath(cwd);\n\tif (parentPath !== undefined) {\n\t\ttrustOptions.push({\n\t\t\tlabel: `Trust parent folder (${parentPath})`,\n\t\t\ttrusted: true,\n\t\t\tupdates: [\n\t\t\t\t{ path: parentPath, decision: true },\n\t\t\t\t{ path: trustPath, decision: null },\n\t\t\t],\n\t\t\tsavedPath: parentPath,\n\t\t});\n\t}\n\tif (options?.includeSessionOnly) {\n\t\ttrustOptions.push({ label: \"Trust (this session only)\", trusted: true, updates: [] });\n\t}\n\ttrustOptions.push({\n\t\tlabel: \"Do not trust\",\n\t\ttrusted: false,\n\t\tupdates: [{ path: trustPath, decision: false }],\n\t\tsavedPath: trustPath,\n\t});\n\tif (options?.includeSessionOnly) {\n\t\ttrustOptions.push({ label: \"Do not trust (this session only)\", trusted: false, updates: [] });\n\t}\n\treturn trustOptions;\n}\n\nfunction readTrustFile(path: string): TrustFile {\n\tif (!existsSync(path)) {\n\t\treturn {};\n\t}\n\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(stripBom(readFileSync(path, \"utf-8\")));\n\t} catch (error) {\n\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\tthrow new Error(`Failed to read trust store ${path}: ${message}`);\n\t}\n\n\tif (typeof parsed !== \"object\" || parsed === null || Array.isArray(parsed)) {\n\t\tthrow new Error(`Invalid trust store ${path}: expected an object`);\n\t}\n\n\tconst data: TrustFile = {};\n\tfor (const [key, value] of Object.entries(parsed)) {\n\t\tif (value !== true && value !== false && value !== null) {\n\t\t\tthrow new Error(`Invalid trust store ${path}: value for ${JSON.stringify(key)} must be true, false, or null`);\n\t\t}\n\t\tdata[key] = value;\n\t}\n\treturn data;\n}\n\nfunction writeTrustFile(path: string, data: TrustFile): void {\n\tconst sorted: TrustFile = {};\n\tfor (const key of Object.keys(data).sort()) {\n\t\tconst value = data[key];\n\t\tif (value === true || value === false || value === null) {\n\t\t\tsorted[key] = value;\n\t\t}\n\t}\n\tmkdirSync(dirname(path), { recursive: true });\n\twriteFileSync(path, `${JSON.stringify(sorted, null, 2)}\\n`, \"utf-8\");\n}\n\nfunction acquireTrustLockSync(path: string): () => void {\n\tconst trustDir = dirname(path);\n\tmkdirSync(trustDir, { recursive: true });\n\tconst maxAttempts = 10;\n\tconst delayMs = 20;\n\tlet lastError: unknown;\n\n\tfor (let attempt = 1; attempt <= maxAttempts; attempt++) {\n\t\ttry {\n\t\t\treturn lockfile.lockSync(trustDir, { realpath: false, lockfilePath: `${path}.lock` });\n\t\t} catch (error) {\n\t\t\tconst code =\n\t\t\t\ttypeof error === \"object\" && error !== null && \"code\" in error\n\t\t\t\t\t? String((error as { code?: unknown }).code)\n\t\t\t\t\t: undefined;\n\t\t\tif (code !== \"ELOCKED\" || attempt === maxAttempts) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tlastError = error;\n\t\t\tconst start = Date.now();\n\t\t\twhile (Date.now() - start < delayMs) {\n\t\t\t\t// Sleep synchronously to avoid changing trust store callers to async.\n\t\t\t}\n\t\t}\n\t}\n\n\tif (lastError instanceof Error) {\n\t\tthrow lastError;\n\t}\n\tthrow new Error(\"Failed to acquire trust store lock\");\n}\n\nfunction withTrustFileLock<T>(path: string, fn: () => T): T {\n\tconst release = acquireTrustLockSync(path);\n\ttry {\n\t\treturn fn();\n\t} finally {\n\t\trelease();\n\t}\n}\n\n/**\n * Returns true when cwd has project-local resources that must be gated by\n * project trust: trust-requiring entries under cwd/.pi, or .agents/skills in\n * cwd or one of its ancestors. Returns false when no such project resources\n * exist. The user/global ~/.agents/skills directory is always treated as a\n * trusted user resource and is ignored here, even when cwd is $HOME.\n */\nexport function hasTrustRequiringProjectResources(cwd: string): boolean {\n\tconst homeDir = canonicalizePath(resolvePath(process.env.HOME || homedir()));\n\tconst userAgentsSkillsDir = join(homeDir, \".agents\", \"skills\");\n\tlet currentDir = canonicalizePath(resolvePath(cwd));\n\n\tconst configDir = join(currentDir, CONFIG_DIR_NAME);\n\tif (TRUST_REQUIRING_PROJECT_CONFIG_RESOURCES.some((entry) => existsSync(join(configDir, entry)))) {\n\t\treturn true;\n\t}\n\n\twhile (true) {\n\t\tconst agentsSkillsDir = join(currentDir, \".agents\", \"skills\");\n\t\tif (agentsSkillsDir !== userAgentsSkillsDir && existsSync(agentsSkillsDir)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tconst parentDir = dirname(currentDir);\n\t\tif (parentDir === currentDir) {\n\t\t\treturn false;\n\t\t}\n\t\tcurrentDir = parentDir;\n\t}\n}\n\nexport class ProjectTrustStore {\n\tprivate trustPath: string;\n\n\tconstructor(agentDir: string) {\n\t\tthis.trustPath = join(resolvePath(agentDir), \"trust.json\");\n\t}\n\n\tget(cwd: string): ProjectTrustDecision {\n\t\treturn this.getEntry(cwd)?.decision ?? null;\n\t}\n\n\tgetEntry(cwd: string): ProjectTrustStoreEntry | null {\n\t\treturn withTrustFileLock(this.trustPath, () => {\n\t\t\tconst data = readTrustFile(this.trustPath);\n\t\t\treturn findNearestTrustEntry(data, cwd);\n\t\t});\n\t}\n\n\tset(cwd: string, decision: ProjectTrustDecision): void {\n\t\tthis.setMany([{ path: cwd, decision }]);\n\t}\n\n\tsetMany(decisions: ProjectTrustUpdate[]): void {\n\t\twithTrustFileLock(this.trustPath, () => {\n\t\t\tconst data = readTrustFile(this.trustPath);\n\t\t\tfor (const { path, decision } of decisions) {\n\t\t\t\tconst key = normalizeCwd(path);\n\t\t\t\tif (decision === null) {\n\t\t\t\t\tdelete data[key];\n\t\t\t\t} else {\n\t\t\t\t\tdata[key] = decision;\n\t\t\t\t}\n\t\t\t}\n\t\t\twriteTrustFile(this.trustPath, data);\n\t\t});\n\t}\n}\n"]}
1
+ {"version":3,"file":"trust-manager.js","sourceRoot":"","sources":["../../src/core/trust-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,QAAQ,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAuB5C,MAAM,wCAAwC,GAAG;IAChD,eAAe;IACf,UAAU;IACV,YAAY;IACZ,QAAQ;IACR,SAAS;IACT,QAAQ;IACR,WAAW;IACX,kBAAkB;CACT,CAAC;AAEX,SAAS,YAAY,CAAC,GAAW;IAChC,OAAO,gBAAgB,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC;AAC3C,CAAC;AAED,SAAS,qBAAqB,CAAC,IAAe,EAAE,GAAW;IA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J,CAAC;CACD","sourcesContent":["import { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport lockfile from \"proper-lockfile\";\nimport { CONFIG_DIR_NAME } from \"../config.ts\";\nimport { canonicalizePath, resolvePath } from \"../utils/paths.ts\";\nimport { stripBom } from \"../utils/text.ts\";\n\nexport type ProjectTrustDecision = boolean | null;\n\nexport interface ProjectTrustStoreEntry {\n\tpath: string;\n\tdecision: boolean;\n}\n\nexport interface ProjectTrustUpdate {\n\tpath: string;\n\tdecision: ProjectTrustDecision;\n}\n\nexport interface ProjectTrustOption {\n\tlabel: string;\n\ttrusted: boolean;\n\tupdates: ProjectTrustUpdate[];\n\tsavedPath?: string;\n}\n\ntype TrustFile = Record<string, boolean | null | undefined>;\n\nconst TRUST_REQUIRING_PROJECT_CONFIG_RESOURCES = [\n\t\"settings.json\",\n\t\"mcp.json\",\n\t\"extensions\",\n\t\"skills\",\n\t\"prompts\",\n\t\"themes\",\n\t\"SYSTEM.md\",\n\t\"APPEND_SYSTEM.md\",\n] as const;\n\nfunction normalizeCwd(cwd: string): string {\n\treturn canonicalizePath(resolvePath(cwd));\n}\n\nfunction findNearestTrustEntry(data: TrustFile, cwd: string): ProjectTrustStoreEntry | null {\n\tlet currentDir = normalizeCwd(cwd);\n\twhile (true) {\n\t\tconst value = data[currentDir];\n\t\tif (value === true || value === false) {\n\t\t\treturn { path: currentDir, decision: value };\n\t\t}\n\n\t\tconst parentDir = dirname(currentDir);\n\t\tif (parentDir === currentDir) {\n\t\t\treturn null;\n\t\t}\n\t\tcurrentDir = parentDir;\n\t}\n}\n\nexport function getProjectTrustParentPath(cwd: string): string | undefined {\n\tconst trustPath = normalizeCwd(cwd);\n\tconst parentDir = dirname(trustPath);\n\treturn parentDir === trustPath ? undefined : parentDir;\n}\n\nexport function getProjectTrustOptions(cwd: string, options?: { includeSessionOnly?: boolean }): ProjectTrustOption[] {\n\tconst trustPath = normalizeCwd(cwd);\n\tconst trustOptions: ProjectTrustOption[] = [\n\t\t{ label: \"Trust\", trusted: true, updates: [{ path: trustPath, decision: true }], savedPath: trustPath },\n\t];\n\tconst parentPath = getProjectTrustParentPath(cwd);\n\tif (parentPath !== undefined) {\n\t\ttrustOptions.push({\n\t\t\tlabel: `Trust parent folder (${parentPath})`,\n\t\t\ttrusted: true,\n\t\t\tupdates: [\n\t\t\t\t{ path: parentPath, decision: true },\n\t\t\t\t{ path: trustPath, decision: null },\n\t\t\t],\n\t\t\tsavedPath: parentPath,\n\t\t});\n\t}\n\tif (options?.includeSessionOnly) {\n\t\ttrustOptions.push({ label: \"Trust (this session only)\", trusted: true, updates: [] });\n\t}\n\ttrustOptions.push({\n\t\tlabel: \"Do not trust\",\n\t\ttrusted: false,\n\t\tupdates: [{ path: trustPath, decision: false }],\n\t\tsavedPath: trustPath,\n\t});\n\tif (options?.includeSessionOnly) {\n\t\ttrustOptions.push({ label: \"Do not trust (this session only)\", trusted: false, updates: [] });\n\t}\n\treturn trustOptions;\n}\n\nfunction readTrustFile(path: string): TrustFile {\n\tif (!existsSync(path)) {\n\t\treturn {};\n\t}\n\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(stripBom(readFileSync(path, \"utf-8\")));\n\t} catch (error) {\n\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\tthrow new Error(`Failed to read trust store ${path}: ${message}`);\n\t}\n\n\tif (typeof parsed !== \"object\" || parsed === null || Array.isArray(parsed)) {\n\t\tthrow new Error(`Invalid trust store ${path}: expected an object`);\n\t}\n\n\tconst data: TrustFile = {};\n\tfor (const [key, value] of Object.entries(parsed)) {\n\t\tif (value !== true && value !== false && value !== null) {\n\t\t\tthrow new Error(`Invalid trust store ${path}: value for ${JSON.stringify(key)} must be true, false, or null`);\n\t\t}\n\t\tdata[key] = value;\n\t}\n\treturn data;\n}\n\nfunction writeTrustFile(path: string, data: TrustFile): void {\n\tconst sorted: TrustFile = {};\n\tfor (const key of Object.keys(data).sort()) {\n\t\tconst value = data[key];\n\t\tif (value === true || value === false || value === null) {\n\t\t\tsorted[key] = value;\n\t\t}\n\t}\n\tmkdirSync(dirname(path), { recursive: true });\n\twriteFileSync(path, `${JSON.stringify(sorted, null, 2)}\\n`, \"utf-8\");\n}\n\nfunction acquireTrustLockSync(path: string): () => void {\n\tconst trustDir = dirname(path);\n\tmkdirSync(trustDir, { recursive: true });\n\tconst maxAttempts = 10;\n\tconst delayMs = 20;\n\tlet lastError: unknown;\n\n\tfor (let attempt = 1; attempt <= maxAttempts; attempt++) {\n\t\ttry {\n\t\t\treturn lockfile.lockSync(trustDir, { realpath: false, lockfilePath: `${path}.lock` });\n\t\t} catch (error) {\n\t\t\tconst code =\n\t\t\t\ttypeof error === \"object\" && error !== null && \"code\" in error\n\t\t\t\t\t? String((error as { code?: unknown }).code)\n\t\t\t\t\t: undefined;\n\t\t\tif (code !== \"ELOCKED\" || attempt === maxAttempts) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tlastError = error;\n\t\t\tconst start = Date.now();\n\t\t\twhile (Date.now() - start < delayMs) {\n\t\t\t\t// Sleep synchronously to avoid changing trust store callers to async.\n\t\t\t}\n\t\t}\n\t}\n\n\tif (lastError instanceof Error) {\n\t\tthrow lastError;\n\t}\n\tthrow new Error(\"Failed to acquire trust store lock\");\n}\n\nfunction withTrustFileLock<T>(path: string, fn: () => T): T {\n\tconst release = acquireTrustLockSync(path);\n\ttry {\n\t\treturn fn();\n\t} finally {\n\t\trelease();\n\t}\n}\n\n/**\n * Returns true when cwd has project-local resources that must be gated by\n * project trust: trust-requiring entries under cwd/.pi, or .agents/skills in\n * cwd or one of its ancestors. Returns false when no such project resources\n * exist. The user/global ~/.agents/skills directory is always treated as a\n * trusted user resource and is ignored here, even when cwd is $HOME.\n */\nexport function hasTrustRequiringProjectResources(cwd: string): boolean {\n\tconst homeDir = canonicalizePath(resolvePath(process.env.HOME || homedir()));\n\tconst userAgentsSkillsDir = join(homeDir, \".agents\", \"skills\");\n\tlet currentDir = canonicalizePath(resolvePath(cwd));\n\n\tconst configDir = join(currentDir, CONFIG_DIR_NAME);\n\tif (TRUST_REQUIRING_PROJECT_CONFIG_RESOURCES.some((entry) => existsSync(join(configDir, entry)))) {\n\t\treturn true;\n\t}\n\n\twhile (true) {\n\t\tconst agentsSkillsDir = join(currentDir, \".agents\", \"skills\");\n\t\tif (agentsSkillsDir !== userAgentsSkillsDir && existsSync(agentsSkillsDir)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tconst parentDir = dirname(currentDir);\n\t\tif (parentDir === currentDir) {\n\t\t\treturn false;\n\t\t}\n\t\tcurrentDir = parentDir;\n\t}\n}\n\nexport class ProjectTrustStore {\n\tprivate trustPath: string;\n\n\tconstructor(agentDir: string) {\n\t\tthis.trustPath = join(resolvePath(agentDir), \"trust.json\");\n\t}\n\n\tget(cwd: string): ProjectTrustDecision {\n\t\treturn this.getEntry(cwd)?.decision ?? null;\n\t}\n\n\tgetEntry(cwd: string): ProjectTrustStoreEntry | null {\n\t\treturn withTrustFileLock(this.trustPath, () => {\n\t\t\tconst data = readTrustFile(this.trustPath);\n\t\t\treturn findNearestTrustEntry(data, cwd);\n\t\t});\n\t}\n\n\tset(cwd: string, decision: ProjectTrustDecision): void {\n\t\tthis.setMany([{ path: cwd, decision }]);\n\t}\n\n\tsetMany(decisions: ProjectTrustUpdate[]): void {\n\t\twithTrustFileLock(this.trustPath, () => {\n\t\t\tconst data = readTrustFile(this.trustPath);\n\t\t\tfor (const { path, decision } of decisions) {\n\t\t\t\tconst key = normalizeCwd(path);\n\t\t\t\tif (decision === null) {\n\t\t\t\t\tdelete data[key];\n\t\t\t\t} else {\n\t\t\t\t\tdata[key] = decision;\n\t\t\t\t}\n\t\t\t}\n\t\t\twriteTrustFile(this.trustPath, data);\n\t\t});\n\t}\n}\n"]}
@@ -9,6 +9,8 @@ export interface UsageTotals {
9
9
  }
10
10
  export declare function createUsageTotals(): UsageTotals;
11
11
  export declare function addUsageToTotals(totals: UsageTotals, usage: Usage): void;
12
+ /** Sum of two usages, keeping the optional token splits when either side reports them. */
13
+ export declare function combineUsage(first: Usage, second: Usage): Usage;
12
14
  export interface UsageCostBreakdownEntry {
13
15
  key: string;
14
16
  cost: number;
@@ -1 +1 @@
1
- {"version":3,"file":"usage-totals.d.ts","sourceRoot":"","sources":["../../src/core/usage-totals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEzD,MAAM,WAAW,WAAW;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACb;AAED,wBAAgB,iBAAiB,IAAI,WAAW,CAQ/C;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,CAMxE;AAED,MAAM,WAAW,uBAAuB;IACvC,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CACf;AAED,wFAAwF;AACxF,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,uBAAuB,EAAE,CAoCxF","sourcesContent":["import type { Usage } from \"@earendil-works/pi-ai/compat\";\nimport type { SessionEntry } from \"./session-manager.ts\";\n\nexport interface UsageTotals {\n\tinput: number;\n\toutput: number;\n\tcacheRead: number;\n\tcacheWrite: number;\n\tcost: number;\n}\n\nexport function createUsageTotals(): UsageTotals {\n\treturn {\n\t\tinput: 0,\n\t\toutput: 0,\n\t\tcacheRead: 0,\n\t\tcacheWrite: 0,\n\t\tcost: 0,\n\t};\n}\n\nexport function addUsageToTotals(totals: UsageTotals, usage: Usage): void {\n\ttotals.input += usage.input;\n\ttotals.output += usage.output;\n\ttotals.cacheRead += usage.cacheRead;\n\ttotals.cacheWrite += usage.cacheWrite;\n\ttotals.cost += usage.cost.total;\n}\n\nexport interface UsageCostBreakdownEntry {\n\tkey: string;\n\tcost: number;\n\ttokens: number;\n}\n\n/** Group model-attributed usage by model and all other usage into a separate bucket. */\nexport function getUsageCostBreakdown(entries: SessionEntry[]): UsageCostBreakdownEntry[] {\n\tconst totalsByKey = new Map<string, UsageTotals>();\n\n\tfor (const entry of entries) {\n\t\tlet key: string | undefined;\n\t\tlet usage: Usage | undefined;\n\t\tif (entry.type === \"message\" && entry.message.role === \"assistant\") {\n\t\t\tkey = `${entry.message.provider}/${entry.message.responseModel ?? entry.message.model}`;\n\t\t\tusage = entry.message.usage;\n\t\t} else if (entry.type === \"usage\") {\n\t\t\tkey = `${entry.provider}/${entry.model}`;\n\t\t\tusage = entry.usage;\n\t\t} else if (entry.type === \"message\" && entry.message.role === \"toolResult\" && entry.message.usage) {\n\t\t\tkey = \"Tools/summaries\";\n\t\t\tusage = entry.message.usage;\n\t\t} else if ((entry.type === \"branch_summary\" || entry.type === \"compaction\") && entry.usage) {\n\t\t\tkey = \"Tools/summaries\";\n\t\t\tusage = entry.usage;\n\t\t}\n\t\tif (!key || !usage) continue;\n\n\t\tlet totals = totalsByKey.get(key);\n\t\tif (!totals) {\n\t\t\ttotals = createUsageTotals();\n\t\t\ttotalsByKey.set(key, totals);\n\t\t}\n\t\taddUsageToTotals(totals, usage);\n\t}\n\n\treturn Array.from(totalsByKey, ([key, totals]) => ({\n\t\tkey,\n\t\tcost: totals.cost,\n\t\ttokens: totals.input + totals.output + totals.cacheRead + totals.cacheWrite,\n\t}))\n\t\t.filter((entry) => entry.cost > 0 || entry.tokens > 0)\n\t\t.sort((a, b) => b.cost - a.cost);\n}\n"]}
1
+ {"version":3,"file":"usage-totals.d.ts","sourceRoot":"","sources":["../../src/core/usage-totals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEzD,MAAM,WAAW,WAAW;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACb;AAED,wBAAgB,iBAAiB,IAAI,WAAW,CAQ/C;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,CAMxE;AAED,0FAA0F;AAC1F,wBAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,GAAG,KAAK,CAqB/D;AAED,MAAM,WAAW,uBAAuB;IACvC,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CACf;AAED,wFAAwF;AACxF,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,uBAAuB,EAAE,CAoCxF"}
@@ -14,6 +14,29 @@ export function addUsageToTotals(totals, usage) {
14
14
  totals.cacheWrite += usage.cacheWrite;
15
15
  totals.cost += usage.cost.total;
16
16
  }
17
+ /** Sum of two usages, keeping the optional token splits when either side reports them. */
18
+ export function combineUsage(first, second) {
19
+ return {
20
+ input: first.input + second.input,
21
+ output: first.output + second.output,
22
+ cacheRead: first.cacheRead + second.cacheRead,
23
+ cacheWrite: first.cacheWrite + second.cacheWrite,
24
+ ...(first.cacheWrite1h !== undefined || second.cacheWrite1h !== undefined
25
+ ? { cacheWrite1h: (first.cacheWrite1h ?? 0) + (second.cacheWrite1h ?? 0) }
26
+ : {}),
27
+ ...(first.reasoning !== undefined || second.reasoning !== undefined
28
+ ? { reasoning: (first.reasoning ?? 0) + (second.reasoning ?? 0) }
29
+ : {}),
30
+ totalTokens: first.totalTokens + second.totalTokens,
31
+ cost: {
32
+ input: first.cost.input + second.cost.input,
33
+ output: first.cost.output + second.cost.output,
34
+ cacheRead: first.cost.cacheRead + second.cost.cacheRead,
35
+ cacheWrite: first.cost.cacheWrite + second.cost.cacheWrite,
36
+ total: first.cost.total + second.cost.total,
37
+ },
38
+ };
39
+ }
17
40
  /** Group model-attributed usage by model and all other usage into a separate bucket. */
18
41
  export function getUsageCostBreakdown(entries) {
19
42
  const totalsByKey = new Map();
@@ -1 +1 @@
1
- {"version":3,"file":"usage-totals.js","sourceRoot":"","sources":["../../src/core/usage-totals.ts"],"names":[],"mappings":"AAWA,MAAM,UAAU,iBAAiB,GAAgB;IAChD,OAAO;QACN,KAAK,EAAE,CAAC;QACR,MAAM,EAAE,CAAC;QACT,SAAS,EAAE,CAAC;QACZ,UAAU,EAAE,CAAC;QACb,IAAI,EAAE,CAAC;KACP,CAAC;AAAA,CACF;AAED,MAAM,UAAU,gBAAgB,CAAC,MAAmB,EAAE,KAAY,EAAQ;IACzE,MAAM,CAAC,KAAK,IAAI,KAAK,CAAC,KAAK,CAAC;IAC5B,MAAM,CAAC,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC;IAC9B,MAAM,CAAC,SAAS,IAAI,KAAK,CAAC,SAAS,CAAC;IACpC,MAAM,CAAC,UAAU,IAAI,KAAK,CAAC,UAAU,CAAC;IACtC,MAAM,CAAC,IAAI,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;AAAA,CAChC;AAQD,wFAAwF;AACxF,MAAM,UAAU,qBAAqB,CAAC,OAAuB,EAA6B;IACzF,MAAM,WAAW,GAAG,IAAI,GAAG,EAAuB,CAAC;IAEnD,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC7B,IAAI,GAAuB,CAAC;QAC5B,IAAI,KAAwB,CAAC;QAC7B,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,KAAK,WA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AW,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC;QAClD,GAAG;QACH,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,MAAM,EAAE,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,UAAU;KAC3E,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;SACrD,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;AAAA,CAClC","sourcesContent":["import type { Usage } from \"@earendil-works/pi-ai/compat\";\nimport type { SessionEntry } from \"./session-manager.ts\";\n\nexport interface UsageTotals {\n\tinput: number;\n\toutput: number;\n\tcacheRead: number;\n\tcacheWrite: number;\n\tcost: number;\n}\n\nexport function createUsageTotals(): UsageTotals {\n\treturn {\n\t\tinput: 0,\n\t\toutput: 0,\n\t\tcacheRead: 0,\n\t\tcacheWrite: 0,\n\t\tcost: 0,\n\t};\n}\n\nexport function addUsageToTotals(totals: UsageTotals, usage: Usage): void {\n\ttotals.input += usage.input;\n\ttotals.output += usage.output;\n\ttotals.cacheRead += usage.cacheRead;\n\ttotals.cacheWrite += usage.cacheWrite;\n\ttotals.cost += usage.cost.total;\n}\n\nexport interface UsageCostBreakdownEntry {\n\tkey: string;\n\tcost: number;\n\ttokens: number;\n}\n\n/** Group model-attributed usage by model and all other usage into a separate bucket. */\nexport function getUsageCostBreakdown(entries: SessionEntry[]): UsageCostBreakdownEntry[] {\n\tconst totalsByKey = new Map<string, UsageTotals>();\n\n\tfor (const entry of entries) {\n\t\tlet key: string | undefined;\n\t\tlet usage: Usage | undefined;\n\t\tif (entry.type === \"message\" && entry.message.role === \"assistant\") {\n\t\t\tkey = `${entry.message.provider}/${entry.message.responseModel ?? entry.message.model}`;\n\t\t\tusage = entry.message.usage;\n\t\t} else if (entry.type === \"usage\") {\n\t\t\tkey = `${entry.provider}/${entry.model}`;\n\t\t\tusage = entry.usage;\n\t\t} else if (entry.type === \"message\" && entry.message.role === \"toolResult\" && entry.message.usage) {\n\t\t\tkey = \"Tools/summaries\";\n\t\t\tusage = entry.message.usage;\n\t\t} else if ((entry.type === \"branch_summary\" || entry.type === \"compaction\") && entry.usage) {\n\t\t\tkey = \"Tools/summaries\";\n\t\t\tusage = entry.usage;\n\t\t}\n\t\tif (!key || !usage) continue;\n\n\t\tlet totals = totalsByKey.get(key);\n\t\tif (!totals) {\n\t\t\ttotals = createUsageTotals();\n\t\t\ttotalsByKey.set(key, totals);\n\t\t}\n\t\taddUsageToTotals(totals, usage);\n\t}\n\n\treturn Array.from(totalsByKey, ([key, totals]) => ({\n\t\tkey,\n\t\tcost: totals.cost,\n\t\ttokens: totals.input + totals.output + totals.cacheRead + totals.cacheWrite,\n\t}))\n\t\t.filter((entry) => entry.cost > 0 || entry.tokens > 0)\n\t\t.sort((a, b) => b.cost - a.cost);\n}\n"]}
1
+ {"version":3,"file":"usage-totals.js","sourceRoot":"","sources":["../../src/core/usage-totals.ts"],"names":[],"mappings":"AAWA,MAAM,UAAU,iBAAiB;IAChC,OAAO;QACN,KAAK,EAAE,CAAC;QACR,MAAM,EAAE,CAAC;QACT,SAAS,EAAE,CAAC;QACZ,UAAU,EAAE,CAAC;QACb,IAAI,EAAE,CAAC;KACP,CAAC;AACH,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,MAAmB,EAAE,KAAY;IACjE,MAAM,CAAC,KAAK,IAAI,KAAK,CAAC,KAAK,CAAC;IAC5B,MAAM,CAAC,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC;IAC9B,MAAM,CAAC,SAAS,IAAI,KAAK,CAAC,SAAS,CAAC;IACpC,MAAM,CAAC,UAAU,IAAI,KAAK,CAA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C,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;YAC5F,GAAG,GAAG,iBAAiB,CAAC;YACxB,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;QACrB,CAAC;QACD,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK;YAAE,SAAS;QAE7B,IAAI,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAClC,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,MAAM,GAAG,iBAAiB,EAAE,CAAC;YAC7B,WAAW,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QAC9B,CAAC;QACD,gBAAgB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC;QAClD,GAAG;QACH,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,MAAM,EAAE,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,UAAU;KAC3E,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;SACrD,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC","sourcesContent":["import type { Usage } from \"@earendil-works/pi-ai/compat\";\nimport type { SessionEntry } from \"./session-manager.ts\";\n\nexport interface UsageTotals {\n\tinput: number;\n\toutput: number;\n\tcacheRead: number;\n\tcacheWrite: number;\n\tcost: number;\n}\n\nexport function createUsageTotals(): UsageTotals {\n\treturn {\n\t\tinput: 0,\n\t\toutput: 0,\n\t\tcacheRead: 0,\n\t\tcacheWrite: 0,\n\t\tcost: 0,\n\t};\n}\n\nexport function addUsageToTotals(totals: UsageTotals, usage: Usage): void {\n\ttotals.input += usage.input;\n\ttotals.output += usage.output;\n\ttotals.cacheRead += usage.cacheRead;\n\ttotals.cacheWrite += usage.cacheWrite;\n\ttotals.cost += usage.cost.total;\n}\n\n/** Sum of two usages, keeping the optional token splits when either side reports them. */\nexport function combineUsage(first: Usage, second: Usage): Usage {\n\treturn {\n\t\tinput: first.input + second.input,\n\t\toutput: first.output + second.output,\n\t\tcacheRead: first.cacheRead + second.cacheRead,\n\t\tcacheWrite: first.cacheWrite + second.cacheWrite,\n\t\t...(first.cacheWrite1h !== undefined || second.cacheWrite1h !== undefined\n\t\t\t? { cacheWrite1h: (first.cacheWrite1h ?? 0) + (second.cacheWrite1h ?? 0) }\n\t\t\t: {}),\n\t\t...(first.reasoning !== undefined || second.reasoning !== undefined\n\t\t\t? { reasoning: (first.reasoning ?? 0) + (second.reasoning ?? 0) }\n\t\t\t: {}),\n\t\ttotalTokens: first.totalTokens + second.totalTokens,\n\t\tcost: {\n\t\t\tinput: first.cost.input + second.cost.input,\n\t\t\toutput: first.cost.output + second.cost.output,\n\t\t\tcacheRead: first.cost.cacheRead + second.cost.cacheRead,\n\t\t\tcacheWrite: first.cost.cacheWrite + second.cost.cacheWrite,\n\t\t\ttotal: first.cost.total + second.cost.total,\n\t\t},\n\t};\n}\n\nexport interface UsageCostBreakdownEntry {\n\tkey: string;\n\tcost: number;\n\ttokens: number;\n}\n\n/** Group model-attributed usage by model and all other usage into a separate bucket. */\nexport function getUsageCostBreakdown(entries: SessionEntry[]): UsageCostBreakdownEntry[] {\n\tconst totalsByKey = new Map<string, UsageTotals>();\n\n\tfor (const entry of entries) {\n\t\tlet key: string | undefined;\n\t\tlet usage: Usage | undefined;\n\t\tif (entry.type === \"message\" && entry.message.role === \"assistant\") {\n\t\t\tkey = `${entry.message.provider}/${entry.message.responseModel ?? entry.message.model}`;\n\t\t\tusage = entry.message.usage;\n\t\t} else if (entry.type === \"usage\") {\n\t\t\tkey = `${entry.provider}/${entry.model}`;\n\t\t\tusage = entry.usage;\n\t\t} else if (entry.type === \"message\" && entry.message.role === \"toolResult\" && entry.message.usage) {\n\t\t\tkey = \"Tools/summaries\";\n\t\t\tusage = entry.message.usage;\n\t\t} else if ((entry.type === \"branch_summary\" || entry.type === \"compaction\") && entry.usage) {\n\t\t\tkey = \"Tools/summaries\";\n\t\t\tusage = entry.usage;\n\t\t}\n\t\tif (!key || !usage) continue;\n\n\t\tlet totals = totalsByKey.get(key);\n\t\tif (!totals) {\n\t\t\ttotals = createUsageTotals();\n\t\t\ttotalsByKey.set(key, totals);\n\t\t}\n\t\taddUsageToTotals(totals, usage);\n\t}\n\n\treturn Array.from(totalsByKey, ([key, totals]) => ({\n\t\tkey,\n\t\tcost: totals.cost,\n\t\ttokens: totals.input + totals.output + totals.cacheRead + totals.cacheWrite,\n\t}))\n\t\t.filter((entry) => entry.cost > 0 || entry.tokens > 0)\n\t\t.sort((a, b) => b.cost - a.cost);\n}\n"]}
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Virtual models are catalog entries that route each request to a physical model.
3
+ *
4
+ * The selection (`model_change`, `agent.state.model`, `ctx.model`) may name a virtual model.
5
+ * Everything below the routing step only sees physical models: providers stream them and
6
+ * assistant messages record them. A virtual model never reaches a provider.
7
+ *
8
+ * Virtual models belong to a provider id but are not provider models. `ModelRuntime` keeps them
9
+ * separately and adds them to the provider's catalog with `withVirtualModels()`, so any provider,
10
+ * including one with physical models, can list several virtual models.
11
+ */
12
+ import type { AgentMessage } from "@earendil-works/pi-agent-core";
13
+ import { type Api, type AssistantMessage, type Message, type Model, type ModelThinkingLevel, type Provider } from "@earendil-works/pi-ai";
14
+ import type { SessionEntry } from "./session-manager.ts";
15
+ /** API id of virtual catalog entries. Requests for it fail unless routed first. */
16
+ export declare const VIRTUAL_MODEL_API = "pi-virtual";
17
+ /** Custom entry type that stores router state on the session branch. */
18
+ export declare const VIRTUAL_MODEL_STATE_ENTRY = "pi.virtual-model-state";
19
+ /** Data of a `pi.virtual-model-state` custom entry. */
20
+ export interface VirtualModelStateData<TState = unknown> {
21
+ provider: string;
22
+ modelId: string;
23
+ state: TState;
24
+ }
25
+ /**
26
+ * Why a request is being routed.
27
+ * - `user`: first request after a message the user wrote (prompt, steering, or follow-up)
28
+ * - `continuation`: any other request in the agent loop, e.g. after tool results or extension messages
29
+ * - `retry`: automatic retry after a failed request, including after compaction for a context overflow
30
+ * - `direct`: a request outside the agent loop, e.g. a compaction summary or an extension call
31
+ */
32
+ export type ModelRouteReason = "user" | "continuation" | "retry" | "direct";
33
+ export interface ModelRouteRequest<TState = unknown> {
34
+ /** The selected virtual model. */
35
+ model: Model<Api>;
36
+ /** The selected thinking level. Its meaning is up to the router. */
37
+ thinkingLevel: ModelThinkingLevel;
38
+ reason: ModelRouteReason;
39
+ /** Physical model and thinking level of the latest successful response in `messages`. */
40
+ previous?: {
41
+ model: Model<Api>;
42
+ thinkingLevel?: ModelThinkingLevel;
43
+ };
44
+ /**
45
+ * For `retry`: the failed request, which `messages` no longer contains. `message` carries its
46
+ * `stopReason` and `errorMessage`. Absent when the router itself failed.
47
+ */
48
+ failed?: {
49
+ model: Model<Api>;
50
+ thinkingLevel?: ModelThinkingLevel;
51
+ message: AssistantMessage;
52
+ };
53
+ /** Router state last returned on this session branch. Undefined before the first state and for `direct` requests. */
54
+ state?: TState;
55
+ /** Conversation for this request, including system messages. */
56
+ messages: readonly Message[];
57
+ signal?: AbortSignal;
58
+ }
59
+ /** Physical model and thinking level for one request. */
60
+ export interface ModelRoute<TState = unknown> {
61
+ model: Model<Api>;
62
+ thinkingLevel: ModelThinkingLevel;
63
+ /**
64
+ * New router state, stored on the session branch unless it is `request.state` itself. Return
65
+ * `request.state` or undefined to keep the current state. Must be JSON-serializable. Ignored for
66
+ * `direct` requests.
67
+ */
68
+ state?: TState;
69
+ }
70
+ export interface VirtualModelDefinition<TState = unknown> {
71
+ /** Provider the virtual model is listed under. May be a provider with physical models. */
72
+ provider: string;
73
+ /** Model id. Must not be the id of a physical model of `provider`. */
74
+ id: string;
75
+ name: string;
76
+ /** Thinking levels offered for selection. Defaults to `["off"]`. */
77
+ thinkingLevels?: readonly ModelThinkingLevel[];
78
+ /**
79
+ * Limits shown before the first response. Afterwards, Pi uses the limits of the physical model
80
+ * that answered. Unset limits are unknown (0).
81
+ */
82
+ contextWindow?: number;
83
+ maxTokens?: number;
84
+ /** Input types accepted for selection. Defaults to text and images; routed models without image support get placeholders. */
85
+ input?: ("text" | "image")[];
86
+ /** Pick the physical model, which must have credentials, and thinking level for one request. */
87
+ route(request: ModelRouteRequest<TState>): ModelRoute<TState> | Promise<ModelRoute<TState>>;
88
+ }
89
+ /** Whether a model or message names a virtual model. Failed routing leaves the virtual model on its message. */
90
+ export declare function isVirtualModel(model: {
91
+ api: string;
92
+ }): boolean;
93
+ /** Latest successful response. Its model is physical: failed or aborted requests, including failed routing, are skipped. */
94
+ export declare function findLatestResponse(messages: readonly AgentMessage[]): AssistantMessage | undefined;
95
+ /**
96
+ * The model selection a session branch records. A virtual `model_change` holds until the next
97
+ * `model_change`, because responses name the physical models it routed to. Otherwise the latest
98
+ * physical response wins, as in sessions without virtual models. A virtual model that is no longer
99
+ * registered does not hold, so the selection falls back to the physical model that answered last.
100
+ */
101
+ export declare function getBranchSelection(branch: readonly SessionEntry[], getModel: (provider: string, modelId: string) => Model<Api> | undefined): {
102
+ provider: string;
103
+ modelId: string;
104
+ } | undefined;
105
+ /** Latest router state a session branch stores for a virtual model. */
106
+ export declare function getVirtualModelState(branch: readonly SessionEntry[], provider: string, modelId: string): unknown;
107
+ /** Build the catalog entry of a virtual model. */
108
+ export declare function createVirtualModel(definition: Omit<VirtualModelDefinition, "route">): Model<Api>;
109
+ /**
110
+ * Add virtual models to a provider's catalog. Without a provider, the result is a keyless provider
111
+ * that only lists the virtual models. A virtual model hides a physical chat model with the same id,
112
+ * which a catalog refresh can add after registration. Availability follows the provider's auth.
113
+ */
114
+ export declare function withVirtualModels(providerId: string, provider: Provider | undefined, virtualModels: readonly Model<Api>[]): Provider;
115
+ //# sourceMappingURL=virtual-models.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"virtual-models.d.ts","sourceRoot":"","sources":["../../src/core/virtual-models.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAEN,KAAK,GAAG,EACR,KAAK,gBAAgB,EAIrB,KAAK,OAAO,EACZ,KAAK,KAAK,EACV,KAAK,kBAAkB,EACvB,KAAK,QAAQ,EAEb,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEzD,mFAAmF;AACnF,eAAO,MAAM,iBAAiB,eAAe,CAAC;AAE9C,wEAAwE;AACxE,eAAO,MAAM,yBAAyB,2BAA2B,CAAC;AAElE,uDAAuD;AACvD,MAAM,WAAW,qBAAqB,CAAC,MAAM,GAAG,OAAO;IACtD,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;CACd;AAID;;;;;;GAMG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,cAAc,GAAG,OAAO,GAAG,QAAQ,CAAC;AAE5E,MAAM,WAAW,iBAAiB,CAAC,MAAM,GAAG,OAAO;IAClD,kCAAkC;IAClC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IAClB,oEAAoE;IACpE,aAAa,EAAE,kBAAkB,CAAC;IAClC,MAAM,EAAE,gBAAgB,CAAC;IACzB,yFAAyF;IACzF,QAAQ,CAAC,EAAE;QAAE,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;QAAC,aAAa,CAAC,EAAE,kBAAkB,CAAA;KAAE,CAAC;IACrE;;;OAGG;IACH,MAAM,CAAC,EA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