operon-agents-core 0.1.0-alpha.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 (511) hide show
  1. package/LICENSE +55 -0
  2. package/README.md +7 -0
  3. package/dist/agent/agent.d.ts +65 -0
  4. package/dist/agent/agent.js +39 -0
  5. package/dist/agent/compose-hooks.d.ts +13 -0
  6. package/dist/agent/compose-hooks.js +197 -0
  7. package/dist/agent/concurrency.d.ts +22 -0
  8. package/dist/agent/concurrency.js +70 -0
  9. package/dist/agent/context-report.d.ts +60 -0
  10. package/dist/agent/context-report.js +45 -0
  11. package/dist/agent/graph.d.ts +48 -0
  12. package/dist/agent/graph.js +129 -0
  13. package/dist/agent/guardrail.d.ts +105 -0
  14. package/dist/agent/guardrail.js +314 -0
  15. package/dist/agent/handoff-executor.d.ts +21 -0
  16. package/dist/agent/handoff-executor.js +95 -0
  17. package/dist/agent/handoff.d.ts +42 -0
  18. package/dist/agent/handoff.js +70 -0
  19. package/dist/agent/index.d.ts +25 -0
  20. package/dist/agent/index.js +19 -0
  21. package/dist/agent/instruction-context.d.ts +33 -0
  22. package/dist/agent/instruction-context.js +100 -0
  23. package/dist/agent/pause.d.ts +12 -0
  24. package/dist/agent/pause.js +82 -0
  25. package/dist/agent/profile-builtins.d.ts +8 -0
  26. package/dist/agent/profile-builtins.js +130 -0
  27. package/dist/agent/profiles.d.ts +94 -0
  28. package/dist/agent/profiles.js +264 -0
  29. package/dist/agent/recovery.d.ts +10 -0
  30. package/dist/agent/recovery.js +151 -0
  31. package/dist/agent/run-support.d.ts +79 -0
  32. package/dist/agent/run-support.js +211 -0
  33. package/dist/agent/runner.d.ts +246 -0
  34. package/dist/agent/runner.js +786 -0
  35. package/dist/agent/session.d.ts +363 -0
  36. package/dist/agent/session.js +937 -0
  37. package/dist/agent/subagent-tools.d.ts +18 -0
  38. package/dist/agent/subagent-tools.js +650 -0
  39. package/dist/agent/subagent.d.ts +56 -0
  40. package/dist/agent/subagent.js +50 -0
  41. package/dist/agent/toolset.d.ts +24 -0
  42. package/dist/agent/toolset.js +112 -0
  43. package/dist/agent/workflow/index.d.ts +9 -0
  44. package/dist/agent/workflow/index.js +8 -0
  45. package/dist/agent/workflow/journal.d.ts +180 -0
  46. package/dist/agent/workflow/journal.js +224 -0
  47. package/dist/agent/workflow/manager.d.ts +27 -0
  48. package/dist/agent/workflow/manager.js +33 -0
  49. package/dist/agent/workflow/parse.d.ts +17 -0
  50. package/dist/agent/workflow/parse.js +174 -0
  51. package/dist/agent/workflow/prompt.d.ts +8 -0
  52. package/dist/agent/workflow/prompt.js +84 -0
  53. package/dist/agent/workflow/runtime.d.ts +64 -0
  54. package/dist/agent/workflow/runtime.js +380 -0
  55. package/dist/agent/workflow/sandbox.d.ts +35 -0
  56. package/dist/agent/workflow/sandbox.js +90 -0
  57. package/dist/agent/workflow/snapshot.d.ts +36 -0
  58. package/dist/agent/workflow/snapshot.js +9 -0
  59. package/dist/agent/workflow/structured.d.ts +35 -0
  60. package/dist/agent/workflow/structured.js +79 -0
  61. package/dist/agent/workflow/tool.d.ts +14 -0
  62. package/dist/agent/workflow/tool.js +569 -0
  63. package/dist/agent/workflow/types.d.ts +100 -0
  64. package/dist/agent/workflow/types.js +17 -0
  65. package/dist/agent/worktree.d.ts +38 -0
  66. package/dist/agent/worktree.js +56 -0
  67. package/dist/capabilities/assembler.d.ts +39 -0
  68. package/dist/capabilities/assembler.js +171 -0
  69. package/dist/capabilities/background/agent-task.d.ts +51 -0
  70. package/dist/capabilities/background/agent-task.js +101 -0
  71. package/dist/capabilities/background/command-task.d.ts +43 -0
  72. package/dist/capabilities/background/command-task.js +86 -0
  73. package/dist/capabilities/background/index.d.ts +20 -0
  74. package/dist/capabilities/background/index.js +57 -0
  75. package/dist/capabilities/background/manager.d.ts +194 -0
  76. package/dist/capabilities/background/manager.js +1131 -0
  77. package/dist/capabilities/background/persist-disk.d.ts +17 -0
  78. package/dist/capabilities/background/persist-disk.js +66 -0
  79. package/dist/capabilities/background/persist.d.ts +61 -0
  80. package/dist/capabilities/background/persist.js +117 -0
  81. package/dist/capabilities/background/question-task.d.ts +32 -0
  82. package/dist/capabilities/background/question-task.js +56 -0
  83. package/dist/capabilities/background/registrar.d.ts +12 -0
  84. package/dist/capabilities/background/registrar.js +9 -0
  85. package/dist/capabilities/background/swappable-sink.d.ts +20 -0
  86. package/dist/capabilities/background/swappable-sink.js +20 -0
  87. package/dist/capabilities/background/task.d.ts +114 -0
  88. package/dist/capabilities/background/task.js +20 -0
  89. package/dist/capabilities/background/tools.d.ts +6 -0
  90. package/dist/capabilities/background/tools.js +190 -0
  91. package/dist/capabilities/background/workflow-task.d.ts +48 -0
  92. package/dist/capabilities/background/workflow-task.js +91 -0
  93. package/dist/capabilities/capability-state.d.ts +13 -0
  94. package/dist/capabilities/capability-state.js +37 -0
  95. package/dist/capabilities/capability.d.ts +145 -0
  96. package/dist/capabilities/capability.js +2 -0
  97. package/dist/capabilities/compaction/full.d.ts +27 -0
  98. package/dist/capabilities/compaction/full.js +155 -0
  99. package/dist/capabilities/compaction/index.d.ts +17 -0
  100. package/dist/capabilities/compaction/index.js +145 -0
  101. package/dist/capabilities/compaction/instruction.d.ts +2 -0
  102. package/dist/capabilities/compaction/instruction.js +31 -0
  103. package/dist/capabilities/compaction/micro.d.ts +17 -0
  104. package/dist/capabilities/compaction/micro.js +57 -0
  105. package/dist/capabilities/compaction/service.d.ts +28 -0
  106. package/dist/capabilities/compaction/service.js +52 -0
  107. package/dist/capabilities/compaction/strategy.d.ts +43 -0
  108. package/dist/capabilities/compaction/strategy.js +116 -0
  109. package/dist/capabilities/compaction/tokens.d.ts +5 -0
  110. package/dist/capabilities/compaction/tokens.js +28 -0
  111. package/dist/capabilities/goal/goal-store.d.ts +69 -0
  112. package/dist/capabilities/goal/goal-store.js +172 -0
  113. package/dist/capabilities/goal/index.d.ts +6 -0
  114. package/dist/capabilities/goal/index.js +31 -0
  115. package/dist/capabilities/goal/injector.d.ts +9 -0
  116. package/dist/capabilities/goal/injector.js +79 -0
  117. package/dist/capabilities/goal/tools.d.ts +6 -0
  118. package/dist/capabilities/goal/tools.js +86 -0
  119. package/dist/capabilities/index.d.ts +25 -0
  120. package/dist/capabilities/index.js +22 -0
  121. package/dist/capabilities/injection.d.ts +44 -0
  122. package/dist/capabilities/injection.js +112 -0
  123. package/dist/capabilities/plan/index.d.ts +6 -0
  124. package/dist/capabilities/plan/index.js +25 -0
  125. package/dist/capabilities/plan/injector.d.ts +13 -0
  126. package/dist/capabilities/plan/injector.js +83 -0
  127. package/dist/capabilities/plan/plan-mode.d.ts +38 -0
  128. package/dist/capabilities/plan/plan-mode.js +113 -0
  129. package/dist/capabilities/plan/policies.d.ts +6 -0
  130. package/dist/capabilities/plan/policies.js +89 -0
  131. package/dist/capabilities/plan/tools.d.ts +15 -0
  132. package/dist/capabilities/plan/tools.js +87 -0
  133. package/dist/capabilities/skills/flow-provider.d.ts +6 -0
  134. package/dist/capabilities/skills/flow-provider.js +50 -0
  135. package/dist/capabilities/skills/index.d.ts +43 -0
  136. package/dist/capabilities/skills/index.js +48 -0
  137. package/dist/capabilities/skills/injector.d.ts +11 -0
  138. package/dist/capabilities/skills/injector.js +27 -0
  139. package/dist/capabilities/skills/parser.d.ts +36 -0
  140. package/dist/capabilities/skills/parser.js +334 -0
  141. package/dist/capabilities/skills/registry.d.ts +33 -0
  142. package/dist/capabilities/skills/registry.js +118 -0
  143. package/dist/capabilities/skills/scanner.d.ts +26 -0
  144. package/dist/capabilities/skills/scanner.js +154 -0
  145. package/dist/capabilities/skills/service.d.ts +39 -0
  146. package/dist/capabilities/skills/service.js +68 -0
  147. package/dist/capabilities/skills/skill-tool.d.ts +13 -0
  148. package/dist/capabilities/skills/skill-tool.js +72 -0
  149. package/dist/capabilities/skills/types.d.ts +62 -0
  150. package/dist/capabilities/skills/types.js +26 -0
  151. package/dist/capabilities/task/index.d.ts +23 -0
  152. package/dist/capabilities/task/index.js +38 -0
  153. package/dist/capabilities/task/injector.d.ts +9 -0
  154. package/dist/capabilities/task/injector.js +62 -0
  155. package/dist/capabilities/task/persist-disk.d.ts +19 -0
  156. package/dist/capabilities/task/persist-disk.js +84 -0
  157. package/dist/capabilities/task/persist.d.ts +52 -0
  158. package/dist/capabilities/task/persist.js +84 -0
  159. package/dist/capabilities/task/task-store.d.ts +44 -0
  160. package/dist/capabilities/task/task-store.js +106 -0
  161. package/dist/capabilities/task/tools.d.ts +12 -0
  162. package/dist/capabilities/task/tools.js +164 -0
  163. package/dist/capabilities/todo/index.d.ts +8 -0
  164. package/dist/capabilities/todo/index.js +21 -0
  165. package/dist/capabilities/todo/injector.d.ts +9 -0
  166. package/dist/capabilities/todo/injector.js +68 -0
  167. package/dist/capabilities/todo/todo-store.d.ts +19 -0
  168. package/dist/capabilities/todo/todo-store.js +36 -0
  169. package/dist/capabilities/todo/tools.d.ts +6 -0
  170. package/dist/capabilities/todo/tools.js +70 -0
  171. package/dist/capabilities/tool-provider.d.ts +12 -0
  172. package/dist/capabilities/tool-provider.js +20 -0
  173. package/dist/capabilities/user-hooks/engine.d.ts +22 -0
  174. package/dist/capabilities/user-hooks/engine.js +105 -0
  175. package/dist/capabilities/user-hooks/index.d.ts +13 -0
  176. package/dist/capabilities/user-hooks/index.js +126 -0
  177. package/dist/capabilities/user-hooks/types.d.ts +26 -0
  178. package/dist/capabilities/user-hooks/types.js +19 -0
  179. package/dist/capabilities/workflow/index.d.ts +16 -0
  180. package/dist/capabilities/workflow/index.js +15 -0
  181. package/dist/commands/index.d.ts +31 -0
  182. package/dist/commands/index.js +318 -0
  183. package/dist/config/config-store.d.ts +29 -0
  184. package/dist/config/config-store.js +103 -0
  185. package/dist/config/file-source.d.ts +40 -0
  186. package/dist/config/file-source.js +78 -0
  187. package/dist/config/flags.d.ts +13 -0
  188. package/dist/config/flags.js +25 -0
  189. package/dist/config/index.d.ts +17 -0
  190. package/dist/config/index.js +10 -0
  191. package/dist/config/merge.d.ts +5 -0
  192. package/dist/config/merge.js +57 -0
  193. package/dist/config/model-catalog.d.ts +17 -0
  194. package/dist/config/model-catalog.js +37 -0
  195. package/dist/config/paths.d.ts +31 -0
  196. package/dist/config/paths.js +28 -0
  197. package/dist/config/provider-manager.d.ts +57 -0
  198. package/dist/config/provider-manager.js +344 -0
  199. package/dist/config/resolver.d.ts +40 -0
  200. package/dist/config/resolver.js +52 -0
  201. package/dist/config/schema.d.ts +389 -0
  202. package/dist/config/schema.js +132 -0
  203. package/dist/events/events.d.ts +348 -0
  204. package/dist/events/events.js +146 -0
  205. package/dist/events/index.d.ts +10 -0
  206. package/dist/events/index.js +5 -0
  207. package/dist/events/projection.d.ts +207 -0
  208. package/dist/events/projection.js +438 -0
  209. package/dist/events/publisher.d.ts +48 -0
  210. package/dist/events/publisher.js +404 -0
  211. package/dist/events/redacting-sink.d.ts +11 -0
  212. package/dist/events/redacting-sink.js +31 -0
  213. package/dist/home.d.ts +16 -0
  214. package/dist/home.js +20 -0
  215. package/dist/index.d.ts +22 -0
  216. package/dist/index.js +22 -0
  217. package/dist/internal.d.ts +33 -0
  218. package/dist/internal.js +29 -0
  219. package/dist/llm/auth.d.ts +22 -0
  220. package/dist/llm/auth.js +112 -0
  221. package/dist/llm/capability.d.ts +13 -0
  222. package/dist/llm/capability.js +45 -0
  223. package/dist/llm/define-model.d.ts +110 -0
  224. package/dist/llm/define-model.js +252 -0
  225. package/dist/llm/errors.d.ts +33 -0
  226. package/dist/llm/errors.js +154 -0
  227. package/dist/llm/index.d.ts +14 -0
  228. package/dist/llm/index.js +8 -0
  229. package/dist/llm/model.d.ts +43 -0
  230. package/dist/llm/model.js +9 -0
  231. package/dist/llm/retry.d.ts +35 -0
  232. package/dist/llm/retry.js +160 -0
  233. package/dist/llm/runtime.d.ts +25 -0
  234. package/dist/llm/runtime.js +38 -0
  235. package/dist/logging/diagnostics.d.ts +21 -0
  236. package/dist/logging/diagnostics.js +46 -0
  237. package/dist/logging/index.d.ts +11 -0
  238. package/dist/logging/index.js +7 -0
  239. package/dist/logging/logger.d.ts +25 -0
  240. package/dist/logging/logger.js +54 -0
  241. package/dist/logging/paths.d.ts +11 -0
  242. package/dist/logging/paths.js +20 -0
  243. package/dist/logging/policy.d.ts +5 -0
  244. package/dist/logging/policy.js +19 -0
  245. package/dist/logging/redact.d.ts +10 -0
  246. package/dist/logging/redact.js +95 -0
  247. package/dist/logging/sinks.d.ts +67 -0
  248. package/dist/logging/sinks.js +117 -0
  249. package/dist/loop/context.d.ts +69 -0
  250. package/dist/loop/context.js +129 -0
  251. package/dist/loop/errors.d.ts +8 -0
  252. package/dist/loop/errors.js +27 -0
  253. package/dist/loop/events.d.ts +14 -0
  254. package/dist/loop/events.js +2 -0
  255. package/dist/loop/index.d.ts +10 -0
  256. package/dist/loop/index.js +7 -0
  257. package/dist/loop/interruption.d.ts +118 -0
  258. package/dist/loop/interruption.js +191 -0
  259. package/dist/loop/run-turn.d.ts +54 -0
  260. package/dist/loop/run-turn.js +203 -0
  261. package/dist/loop/scheduler.d.ts +18 -0
  262. package/dist/loop/scheduler.js +71 -0
  263. package/dist/loop/steer.d.ts +122 -0
  264. package/dist/loop/steer.js +209 -0
  265. package/dist/loop/tool-call.d.ts +41 -0
  266. package/dist/loop/tool-call.js +326 -0
  267. package/dist/loop/turn-step.d.ts +57 -0
  268. package/dist/loop/turn-step.js +298 -0
  269. package/dist/loop/types.d.ts +217 -0
  270. package/dist/loop/types.js +2 -0
  271. package/dist/loop/usage.d.ts +6 -0
  272. package/dist/loop/usage.js +44 -0
  273. package/dist/mcp/auth-tool.d.ts +12 -0
  274. package/dist/mcp/auth-tool.js +92 -0
  275. package/dist/mcp/capability.d.ts +7 -0
  276. package/dist/mcp/capability.js +91 -0
  277. package/dist/mcp/config-loader.d.ts +28 -0
  278. package/dist/mcp/config-loader.js +115 -0
  279. package/dist/mcp/filter.d.ts +18 -0
  280. package/dist/mcp/filter.js +35 -0
  281. package/dist/mcp/index.d.ts +23 -0
  282. package/dist/mcp/index.js +13 -0
  283. package/dist/mcp/manager.d.ts +51 -0
  284. package/dist/mcp/manager.js +412 -0
  285. package/dist/mcp/mock-transport.d.ts +40 -0
  286. package/dist/mcp/mock-transport.js +68 -0
  287. package/dist/mcp/oauth/callback-server.d.ts +14 -0
  288. package/dist/mcp/oauth/callback-server.js +122 -0
  289. package/dist/mcp/oauth/index.d.ts +9 -0
  290. package/dist/mcp/oauth/index.js +5 -0
  291. package/dist/mcp/oauth/provider.d.ts +39 -0
  292. package/dist/mcp/oauth/provider.js +121 -0
  293. package/dist/mcp/oauth/service.d.ts +34 -0
  294. package/dist/mcp/oauth/service.js +130 -0
  295. package/dist/mcp/oauth/store.d.ts +40 -0
  296. package/dist/mcp/oauth/store.js +134 -0
  297. package/dist/mcp/output.d.ts +12 -0
  298. package/dist/mcp/output.js +110 -0
  299. package/dist/mcp/provider.d.ts +7 -0
  300. package/dist/mcp/provider.js +56 -0
  301. package/dist/mcp/sdk-transport.d.ts +25 -0
  302. package/dist/mcp/sdk-transport.js +228 -0
  303. package/dist/mcp/server.d.ts +77 -0
  304. package/dist/mcp/server.js +120 -0
  305. package/dist/mcp/tool-naming.d.ts +4 -0
  306. package/dist/mcp/tool-naming.js +26 -0
  307. package/dist/mcp/types.d.ts +75 -0
  308. package/dist/mcp/types.js +2 -0
  309. package/dist/net/index.d.ts +3 -0
  310. package/dist/net/index.js +2 -0
  311. package/dist/net/proxy.d.ts +16 -0
  312. package/dist/net/proxy.js +42 -0
  313. package/dist/permission/auto-approver/index.d.ts +3 -0
  314. package/dist/permission/auto-approver/index.js +3 -0
  315. package/dist/permission/auto-approver/llm-auto-approver.d.ts +53 -0
  316. package/dist/permission/auto-approver/llm-auto-approver.js +122 -0
  317. package/dist/permission/auto-approver/prompts-data.d.ts +9 -0
  318. package/dist/permission/auto-approver/prompts-data.js +155 -0
  319. package/dist/permission/auto-approver/prompts.d.ts +18 -0
  320. package/dist/permission/auto-approver/prompts.js +40 -0
  321. package/dist/permission/auto-approver/transcript.d.ts +22 -0
  322. package/dist/permission/auto-approver/transcript.js +75 -0
  323. package/dist/permission/auto-approver/xml.d.ts +5 -0
  324. package/dist/permission/auto-approver/xml.js +25 -0
  325. package/dist/permission/index.d.ts +7 -0
  326. package/dist/permission/index.js +5 -0
  327. package/dist/permission/manager.d.ts +74 -0
  328. package/dist/permission/manager.js +162 -0
  329. package/dist/permission/matches-rule.d.ts +27 -0
  330. package/dist/permission/matches-rule.js +54 -0
  331. package/dist/permission/policies.d.ts +14 -0
  332. package/dist/permission/policies.js +233 -0
  333. package/dist/permission/types.d.ts +84 -0
  334. package/dist/permission/types.js +2 -0
  335. package/dist/plugins/archive.d.ts +3 -0
  336. package/dist/plugins/archive.js +58 -0
  337. package/dist/plugins/github-resolver.d.ts +13 -0
  338. package/dist/plugins/github-resolver.js +20 -0
  339. package/dist/plugins/hooks.d.ts +28 -0
  340. package/dist/plugins/hooks.js +296 -0
  341. package/dist/plugins/index.d.ts +23 -0
  342. package/dist/plugins/index.js +23 -0
  343. package/dist/plugins/injector.d.ts +11 -0
  344. package/dist/plugins/injector.js +32 -0
  345. package/dist/plugins/manager.d.ts +37 -0
  346. package/dist/plugins/manager.js +330 -0
  347. package/dist/plugins/manifest.d.ts +9 -0
  348. package/dist/plugins/manifest.js +234 -0
  349. package/dist/plugins/marketplace.d.ts +85 -0
  350. package/dist/plugins/marketplace.js +205 -0
  351. package/dist/plugins/source.d.ts +23 -0
  352. package/dist/plugins/source.js +87 -0
  353. package/dist/plugins/store.d.ts +20 -0
  354. package/dist/plugins/store.js +30 -0
  355. package/dist/plugins/types.d.ts +118 -0
  356. package/dist/plugins/types.js +5 -0
  357. package/dist/protocol/index.d.ts +4 -0
  358. package/dist/protocol/index.js +2 -0
  359. package/dist/protocol/tool-schema.d.ts +6 -0
  360. package/dist/protocol/tool-schema.js +2 -0
  361. package/dist/store/blob-store.d.ts +45 -0
  362. package/dist/store/blob-store.js +120 -0
  363. package/dist/store/catalog-store.d.ts +32 -0
  364. package/dist/store/catalog-store.js +73 -0
  365. package/dist/store/disk-store.d.ts +58 -0
  366. package/dist/store/disk-store.js +369 -0
  367. package/dist/store/index.d.ts +23 -0
  368. package/dist/store/index.js +15 -0
  369. package/dist/store/lock.d.ts +86 -0
  370. package/dist/store/lock.js +104 -0
  371. package/dist/store/log-store.d.ts +89 -0
  372. package/dist/store/log-store.js +296 -0
  373. package/dist/store/migrate.d.ts +26 -0
  374. package/dist/store/migrate.js +32 -0
  375. package/dist/store/origin.d.ts +89 -0
  376. package/dist/store/origin.js +16 -0
  377. package/dist/store/pg.d.ts +90 -0
  378. package/dist/store/pg.js +344 -0
  379. package/dist/store/redacting-store.d.ts +21 -0
  380. package/dist/store/redacting-store.js +46 -0
  381. package/dist/store/redis.d.ts +89 -0
  382. package/dist/store/redis.js +370 -0
  383. package/dist/store/repository.d.ts +150 -0
  384. package/dist/store/repository.js +508 -0
  385. package/dist/store/session-catalog.d.ts +32 -0
  386. package/dist/store/session-catalog.js +117 -0
  387. package/dist/store/storage.d.ts +27 -0
  388. package/dist/store/storage.js +54 -0
  389. package/dist/store/store.d.ts +262 -0
  390. package/dist/store/store.js +129 -0
  391. package/dist/store/workdir-key.d.ts +4 -0
  392. package/dist/store/workdir-key.js +28 -0
  393. package/dist/tool/access.d.ts +37 -0
  394. package/dist/tool/access.js +79 -0
  395. package/dist/tool/ask.d.ts +22 -0
  396. package/dist/tool/ask.js +38 -0
  397. package/dist/tool/background.d.ts +109 -0
  398. package/dist/tool/background.js +39 -0
  399. package/dist/tool/builtin/ask-user-question.d.ts +2 -0
  400. package/dist/tool/builtin/ask-user-question.js +175 -0
  401. package/dist/tool/builtin/bash.d.ts +2 -0
  402. package/dist/tool/builtin/bash.js +407 -0
  403. package/dist/tool/builtin/edit.d.ts +2 -0
  404. package/dist/tool/builtin/edit.js +202 -0
  405. package/dist/tool/builtin/glob.d.ts +3 -0
  406. package/dist/tool/builtin/glob.js +260 -0
  407. package/dist/tool/builtin/grep.d.ts +2 -0
  408. package/dist/tool/builtin/grep.js +574 -0
  409. package/dist/tool/builtin/line-endings.d.ts +10 -0
  410. package/dist/tool/builtin/line-endings.js +41 -0
  411. package/dist/tool/builtin/read.d.ts +28 -0
  412. package/dist/tool/builtin/read.js +498 -0
  413. package/dist/tool/builtin/write.d.ts +2 -0
  414. package/dist/tool/builtin/write.js +179 -0
  415. package/dist/tool/define.d.ts +46 -0
  416. package/dist/tool/define.js +45 -0
  417. package/dist/tool/file-freshness.d.ts +81 -0
  418. package/dist/tool/file-freshness.js +67 -0
  419. package/dist/tool/filesystem-tools.d.ts +16 -0
  420. package/dist/tool/filesystem-tools.js +19 -0
  421. package/dist/tool/git-context.d.ts +20 -0
  422. package/dist/tool/git-context.js +114 -0
  423. package/dist/tool/index.d.ts +37 -0
  424. package/dist/tool/index.js +34 -0
  425. package/dist/tool/machine-base.d.ts +138 -0
  426. package/dist/tool/machine-base.js +388 -0
  427. package/dist/tool/machine-local.d.ts +35 -0
  428. package/dist/tool/machine-local.js +316 -0
  429. package/dist/tool/machine-null.d.ts +36 -0
  430. package/dist/tool/machine-null.js +64 -0
  431. package/dist/tool/machine-ssh.d.ts +86 -0
  432. package/dist/tool/machine-ssh.js +576 -0
  433. package/dist/tool/machine.d.ts +333 -0
  434. package/dist/tool/machine.js +15 -0
  435. package/dist/tool/policies/path-access.d.ts +48 -0
  436. package/dist/tool/policies/path-access.js +186 -0
  437. package/dist/tool/policies/sensitive.d.ts +3 -0
  438. package/dist/tool/policies/sensitive.js +60 -0
  439. package/dist/tool/questions.d.ts +40 -0
  440. package/dist/tool/questions.js +8 -0
  441. package/dist/tool/search/activation.d.ts +13 -0
  442. package/dist/tool/search/activation.js +31 -0
  443. package/dist/tool/search/deferral.d.ts +37 -0
  444. package/dist/tool/search/deferral.js +128 -0
  445. package/dist/tool/search/search-tool.d.ts +9 -0
  446. package/dist/tool/search/search-tool.js +62 -0
  447. package/dist/tool/shell-env.d.ts +9 -0
  448. package/dist/tool/shell-env.js +34 -0
  449. package/dist/tool/support/bash-destructive.d.ts +24 -0
  450. package/dist/tool/support/bash-destructive.js +63 -0
  451. package/dist/tool/support/bash-paths.d.ts +7 -0
  452. package/dist/tool/support/bash-paths.js +247 -0
  453. package/dist/tool/support/bash-read-only.d.ts +8 -0
  454. package/dist/tool/support/bash-read-only.js +165 -0
  455. package/dist/tool/support/bash-rule-match.d.ts +20 -0
  456. package/dist/tool/support/bash-rule-match.js +191 -0
  457. package/dist/tool/support/file-type.d.ts +10 -0
  458. package/dist/tool/support/file-type.js +149 -0
  459. package/dist/tool/support/git-worktree.d.ts +10 -0
  460. package/dist/tool/support/git-worktree.js +63 -0
  461. package/dist/tool/support/image-compress.d.ts +69 -0
  462. package/dist/tool/support/image-compress.js +265 -0
  463. package/dist/tool/support/machine-ops.d.ts +34 -0
  464. package/dist/tool/support/machine-ops.js +34 -0
  465. package/dist/tool/support/path-glob-match.d.ts +12 -0
  466. package/dist/tool/support/path-glob-match.js +84 -0
  467. package/dist/tool/support/result-builder.d.ts +27 -0
  468. package/dist/tool/support/result-builder.js +108 -0
  469. package/dist/tool/support/rg-locator.d.ts +20 -0
  470. package/dist/tool/support/rg-locator.js +42 -0
  471. package/dist/tool/support/ripgrep-run.d.ts +65 -0
  472. package/dist/tool/support/ripgrep-run.js +40 -0
  473. package/dist/tool/support/rule-match.d.ts +6 -0
  474. package/dist/tool/support/rule-match.js +23 -0
  475. package/dist/tool/support/tool-path.d.ts +8 -0
  476. package/dist/tool/support/tool-path.js +28 -0
  477. package/dist/tool/support/workspace.d.ts +5 -0
  478. package/dist/tool/support/workspace.js +2 -0
  479. package/dist/tool/types.d.ts +131 -0
  480. package/dist/tool/types.js +2 -0
  481. package/dist/tool/web/fetch-url.d.ts +4 -0
  482. package/dist/tool/web/fetch-url.js +61 -0
  483. package/dist/tool/web/index.d.ts +11 -0
  484. package/dist/tool/web/index.js +7 -0
  485. package/dist/tool/web/providers/direct-fetch.d.ts +33 -0
  486. package/dist/tool/web/providers/direct-fetch.js +198 -0
  487. package/dist/tool/web/providers/firecrawl.d.ts +16 -0
  488. package/dist/tool/web/providers/firecrawl.js +59 -0
  489. package/dist/tool/web/providers/tavily.d.ts +14 -0
  490. package/dist/tool/web/providers/tavily.js +43 -0
  491. package/dist/tool/web/types.d.ts +41 -0
  492. package/dist/tool/web/types.js +7 -0
  493. package/dist/tool/web/web-search.d.ts +4 -0
  494. package/dist/tool/web/web-search.js +68 -0
  495. package/dist/tool/workspace-spec.d.ts +74 -0
  496. package/dist/tool/workspace-spec.js +177 -0
  497. package/dist/tracing/bridge.d.ts +7 -0
  498. package/dist/tracing/bridge.js +171 -0
  499. package/dist/tracing/index.d.ts +9 -0
  500. package/dist/tracing/index.js +5 -0
  501. package/dist/tracing/otel.d.ts +38 -0
  502. package/dist/tracing/otel.js +221 -0
  503. package/dist/tracing/processor.d.ts +47 -0
  504. package/dist/tracing/processor.js +94 -0
  505. package/dist/tracing/spans.d.ts +94 -0
  506. package/dist/tracing/spans.js +56 -0
  507. package/dist/ui/ai-sdk.d.ts +114 -0
  508. package/dist/ui/ai-sdk.js +175 -0
  509. package/dist/ui/index.d.ts +2 -0
  510. package/dist/ui/index.js +2 -0
  511. package/package.json +101 -0
@@ -0,0 +1,149 @@
1
+ import { extname } from "node:path";
2
+ export const MEDIA_SNIFF_BYTES = 512;
3
+ export const IMAGE_MIME_BY_SUFFIX = Object.freeze({
4
+ ".png": "image/png",
5
+ ".jpg": "image/jpeg",
6
+ ".jpeg": "image/jpeg",
7
+ ".gif": "image/gif",
8
+ ".bmp": "image/bmp",
9
+ ".tif": "image/tiff",
10
+ ".tiff": "image/tiff",
11
+ ".webp": "image/webp",
12
+ ".ico": "image/x-icon",
13
+ ".heic": "image/heic",
14
+ ".heif": "image/heif",
15
+ ".avif": "image/avif",
16
+ ".svgz": "image/svg+xml",
17
+ });
18
+ const TEXT_MIME_BY_SUFFIX = Object.freeze({
19
+ ".svg": "image/svg+xml",
20
+ });
21
+ export const NON_TEXT_SUFFIXES = new Set([
22
+ ".icns", ".psd", ".ai", ".eps", ".pdf", ".doc", ".docx", ".dot", ".dotx", ".rtf", ".odt",
23
+ ".xls", ".xlsx", ".xlsm", ".xlt", ".xltx", ".xltm", ".ods", ".ppt", ".pptx", ".pptm", ".pps",
24
+ ".ppsx", ".odp", ".pages", ".numbers", ".key", ".zip", ".rar", ".7z", ".tar", ".gz", ".tgz",
25
+ ".bz2", ".xz", ".zst", ".lz", ".lz4", ".br", ".cab", ".ar", ".deb", ".rpm", ".mp3", ".wav",
26
+ ".flac", ".ogg", ".oga", ".opus", ".aac", ".m4a", ".wma", ".ttf", ".otf", ".woff", ".woff2",
27
+ ".exe", ".dll", ".so", ".dylib", ".bin", ".apk", ".ipa", ".jar", ".class", ".pyc", ".pyo",
28
+ ".wasm", ".dmg", ".iso", ".img", ".sqlite", ".sqlite3", ".db", ".db3",
29
+ ".mp4", ".mpg", ".mpeg", ".mkv", ".avi", ".mov", ".ogv", ".wmv", ".webm", ".m4v", ".flv",
30
+ ".3gp", ".3g2",
31
+ ]);
32
+ const ASF_HEADER = Buffer.from([
33
+ 0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66, 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c,
34
+ ]);
35
+ const FTYP_IMAGE_BRANDS = Object.freeze({
36
+ avif: "image/avif", avis: "image/avif", heic: "image/heic", heif: "image/heif",
37
+ heix: "image/heif", hevc: "image/heic", mif1: "image/heif", msf1: "image/heif",
38
+ });
39
+ const UNSUPPORTED_FTYP_VIDEO_BRANDS = new Set([
40
+ "isom", "iso2", "iso5", "mp41", "mp42", "avc1", "mp4v", "m4v", "qt",
41
+ "3gp4", "3gp5", "3gp6", "3gp7", "3g2",
42
+ ]);
43
+ function toBuffer(data) {
44
+ return Buffer.isBuffer(data) ? data : Buffer.from(data.buffer, data.byteOffset, data.byteLength);
45
+ }
46
+ function startsWith(buf, prefix) {
47
+ const needle = Buffer.isBuffer(prefix) ? prefix : Buffer.from(prefix);
48
+ if (buf.length < needle.length)
49
+ return false;
50
+ for (let i = 0; i < needle.length; i += 1) {
51
+ if (buf[i] !== needle[i])
52
+ return false;
53
+ }
54
+ return true;
55
+ }
56
+ function sniffFtypBrand(header) {
57
+ if (header.length < 12)
58
+ return null;
59
+ if (header.subarray(4, 8).toString("latin1") !== "ftyp")
60
+ return null;
61
+ const raw = header.subarray(8, 12).toString("latin1").toLowerCase();
62
+ return raw.replaceAll(/[\s\u0000]+$/g, "").trim();
63
+ }
64
+ export function sniffMediaFromMagic(data) {
65
+ const buf = toBuffer(data);
66
+ const header = buf.length > MEDIA_SNIFF_BYTES ? buf.subarray(0, MEDIA_SNIFF_BYTES) : buf;
67
+ if (startsWith(header, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) {
68
+ return { kind: "image", mimeType: "image/png" };
69
+ }
70
+ if (startsWith(header, [0xff, 0xd8, 0xff])) {
71
+ return { kind: "image", mimeType: "image/jpeg" };
72
+ }
73
+ if (startsWith(header, Buffer.from("GIF87a")) || startsWith(header, Buffer.from("GIF89a"))) {
74
+ return { kind: "image", mimeType: "image/gif" };
75
+ }
76
+ if (startsWith(header, Buffer.from("BM"))) {
77
+ return { kind: "image", mimeType: "image/bmp" };
78
+ }
79
+ if (startsWith(header, [0x49, 0x49, 0x2a, 0x00]) || startsWith(header, [0x4d, 0x4d, 0x00, 0x2a])) {
80
+ return { kind: "image", mimeType: "image/tiff" };
81
+ }
82
+ if (startsWith(header, [0x00, 0x00, 0x01, 0x00])) {
83
+ return { kind: "image", mimeType: "image/x-icon" };
84
+ }
85
+ if (startsWith(header, Buffer.from("RIFF")) && header.length >= 12) {
86
+ const chunk = header.subarray(8, 12).toString("latin1");
87
+ if (chunk === "WEBP")
88
+ return { kind: "image", mimeType: "image/webp" };
89
+ }
90
+ const brand = sniffFtypBrand(header);
91
+ if (brand !== null && brand !== "") {
92
+ if (brand in FTYP_IMAGE_BRANDS)
93
+ return { kind: "image", mimeType: FTYP_IMAGE_BRANDS[brand] };
94
+ }
95
+ return null;
96
+ }
97
+ function sniffUnsupportedMediaFromMagic(data) {
98
+ const buf = toBuffer(data);
99
+ const header = buf.length > MEDIA_SNIFF_BYTES ? buf.subarray(0, MEDIA_SNIFF_BYTES) : buf;
100
+ if (startsWith(header, Buffer.from("RIFF")) && header.length >= 12) {
101
+ return header.subarray(8, 12).toString("latin1") === "AVI ";
102
+ }
103
+ if (startsWith(header, Buffer.from("FLV")))
104
+ return true;
105
+ if (startsWith(header, ASF_HEADER))
106
+ return true;
107
+ if (startsWith(header, [0x1a, 0x45, 0xdf, 0xa3])) {
108
+ const lowered = header.toString("latin1").toLowerCase();
109
+ return lowered.includes("webm") || lowered.includes("matroska");
110
+ }
111
+ const brand = sniffFtypBrand(header);
112
+ return brand !== null && UNSUPPORTED_FTYP_VIDEO_BRANDS.has(brand);
113
+ }
114
+ function getSuffix(path) {
115
+ return extname(path).toLowerCase();
116
+ }
117
+ export function detectFileType(path, header) {
118
+ const suffix = getSuffix(path);
119
+ let mediaHint = null;
120
+ if (suffix in TEXT_MIME_BY_SUFFIX) {
121
+ mediaHint = { kind: "text", mimeType: TEXT_MIME_BY_SUFFIX[suffix] };
122
+ }
123
+ else if (suffix in IMAGE_MIME_BY_SUFFIX) {
124
+ mediaHint = { kind: "image", mimeType: IMAGE_MIME_BY_SUFFIX[suffix] };
125
+ }
126
+ // Cross-validate ext hint against sniffed magic; mismatch on kind → unknown.
127
+ if (header !== undefined) {
128
+ const buf = toBuffer(header);
129
+ const sniffed = sniffMediaFromMagic(buf);
130
+ if (sniffed) {
131
+ if (mediaHint) {
132
+ if (sniffed.kind !== mediaHint.kind)
133
+ return { kind: "unknown", mimeType: "" };
134
+ return mediaHint;
135
+ }
136
+ return sniffed;
137
+ }
138
+ if (sniffUnsupportedMediaFromMagic(buf))
139
+ return { kind: "unknown", mimeType: "" };
140
+ if (buf.includes(0x00))
141
+ return { kind: "unknown", mimeType: "" };
142
+ }
143
+ if (mediaHint)
144
+ return mediaHint;
145
+ if (NON_TEXT_SUFFIXES.has(suffix))
146
+ return { kind: "unknown", mimeType: "" };
147
+ return { kind: "text", mimeType: "text/plain" };
148
+ }
149
+ //# sourceMappingURL=file-type.js.map
@@ -0,0 +1,10 @@
1
+ import type { Machine } from "../machine.ts";
2
+ import { type PathClass } from "../policies/path-access.ts";
3
+ export type GitWorkTreeMachine = Pick<Machine, "fileInfo" | "readBytes">;
4
+ export interface GitWorkTreeMarker {
5
+ readonly dotGitPath: string;
6
+ readonly controlDirPath: string;
7
+ }
8
+ export declare function findGitWorkTreeMarker(machine: GitWorkTreeMachine, cwd: string): Promise<GitWorkTreeMarker | null>;
9
+ export declare function isGitControlPath(targetPath: string, marker: GitWorkTreeMarker, pathClass: PathClass): boolean;
10
+ //# sourceMappingURL=git-worktree.d.ts.map
@@ -0,0 +1,63 @@
1
+ import * as pathe from "pathe";
2
+ import { isWithinDirectory } from "../policies/path-access.js";
3
+ import { readTextFile } from "./machine-ops.js";
4
+ const MAX_WALK_DEPTH = 256;
5
+ export async function findGitWorkTreeMarker(machine, cwd) {
6
+ if (cwd.length === 0 || !pathe.isAbsolute(cwd))
7
+ return null;
8
+ let current = pathe.normalize(cwd);
9
+ for (let depth = 0; depth < MAX_WALK_DEPTH; depth += 1) {
10
+ const dotGitPath = pathe.join(current, ".git");
11
+ const hit = await probeGitMarker(machine, dotGitPath, current);
12
+ if (hit !== null)
13
+ return hit;
14
+ const parent = pathe.dirname(current);
15
+ if (parent === current)
16
+ return null;
17
+ current = parent;
18
+ }
19
+ return null;
20
+ }
21
+ async function probeGitMarker(machine, dotGitPath, markerParent) {
22
+ let kind;
23
+ try {
24
+ kind = (await machine.fileInfo(dotGitPath)).kind;
25
+ }
26
+ catch {
27
+ return null;
28
+ }
29
+ if (kind === "dir")
30
+ return { dotGitPath, controlDirPath: dotGitPath };
31
+ if (kind !== "file")
32
+ return null;
33
+ let content;
34
+ try {
35
+ content = await readTextFile(machine, dotGitPath);
36
+ }
37
+ catch {
38
+ return null;
39
+ }
40
+ const controlDirPath = parseGitDir(content, markerParent);
41
+ return controlDirPath === undefined ? null : { dotGitPath, controlDirPath };
42
+ }
43
+ export function isGitControlPath(targetPath, marker, pathClass) {
44
+ return (isWithinDirectory(targetPath, marker.dotGitPath, pathClass) ||
45
+ isWithinDirectory(targetPath, marker.controlDirPath, pathClass));
46
+ }
47
+ function stripLeadingNoise(content) {
48
+ let s = content;
49
+ if (s.codePointAt(0) === 0xfeff)
50
+ s = s.slice(1);
51
+ return s.trimStart();
52
+ }
53
+ function parseGitDir(content, markerParent) {
54
+ const line = stripLeadingNoise(content).split(/\r?\n/, 1)[0]?.trim();
55
+ if (line === undefined || !line.startsWith("gitdir:"))
56
+ return undefined;
57
+ const rawPath = line.slice("gitdir:".length).trim();
58
+ if (rawPath.length === 0)
59
+ return undefined;
60
+ const absolute = pathe.isAbsolute(rawPath) ? rawPath : pathe.join(markerParent, rawPath);
61
+ return pathe.normalize(absolute);
62
+ }
63
+ //# sourceMappingURL=git-worktree.js.map
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Shrink oversized images before they reach the model.
3
+ *
4
+ * Deliberately narrow in scope: a pixel + byte budget, decompression-bomb guards,
5
+ * a lossless-first / JPEG-ladder encode strategy, and a best-effort contract (any
6
+ * decode/encode failure returns the original bytes unchanged, `changed: false`).
7
+ * Out of scope: WebP wasm decoding (WebP/GIF pass through here), region readback,
8
+ * DI config ownership, and telemetry. `jimp` is imported lazily so the codec is
9
+ * only paid for when an image actually needs work.
10
+ *
11
+ * Compression is never silent to the model: the result carries the original
12
+ * dimensions so the caller can annotate "you are looking at a downsampled copy".
13
+ */
14
+ /** Built-in longest-edge ceiling (px). Larger images are scaled down to fit. */
15
+ export declare const MAX_IMAGE_EDGE_PX = 2000;
16
+ /** Env override for the longest-edge ceiling (px). Ignored unless a positive int. */
17
+ export declare const MAX_IMAGE_EDGE_ENV = "AGENTS_IMAGE_MAX_EDGE_PX";
18
+ /**
19
+ * Raw-byte budget for images the model reads for itself (Read's image path). A
20
+ * session that keeps screenshotting accumulates every image in the request body
21
+ * on every turn, so per-image size — not the provider's per-image ceiling — is
22
+ * what keeps the total under the request-size limit. 256 KB keeps a clean 2000px
23
+ * UI screenshot on the lossless fast path while capping dense content at a
24
+ * readable q80/1000px JPEG.
25
+ */
26
+ export declare const READ_IMAGE_BYTE_BUDGET: number;
27
+ /** Env override for the read-image byte budget (bytes). Ignored unless a positive int. */
28
+ export declare const READ_IMAGE_BYTE_BUDGET_ENV = "AGENTS_IMAGE_READ_BYTE_BUDGET";
29
+ /**
30
+ * Raw-byte ceiling above which compression is skipped rather than decoded. The
31
+ * byte budget bounds the output, but the compressor still has to load the input
32
+ * first; this bounds that input allocation.
33
+ */
34
+ export declare const MAX_IMAGE_DECODE_BYTES: number;
35
+ export declare function resolveMaxImageEdgePx(): number;
36
+ export declare function resolveReadImageByteBudget(): number;
37
+ export interface CompressImageOptions {
38
+ /** Override the longest-edge ceiling (px). Defaults to {@link resolveMaxImageEdgePx}. */
39
+ readonly maxEdge?: number;
40
+ /** Override the raw-byte budget. Defaults to {@link resolveReadImageByteBudget}. */
41
+ readonly byteBudget?: number;
42
+ /** Override the raw-byte ceiling above which compression is skipped. */
43
+ readonly maxDecodeBytes?: number;
44
+ }
45
+ export interface CompressImageResult {
46
+ /** Bytes to send: the re-encoded image, or the original when unchanged. */
47
+ readonly data: Uint8Array;
48
+ /** MIME of `data`. May differ from the input (e.g. png → jpeg). */
49
+ readonly mimeType: string;
50
+ /** Pixel width of `data`; 0 when unknown. */
51
+ readonly width: number;
52
+ /** Pixel height of `data`; 0 when unknown. */
53
+ readonly height: number;
54
+ /** Pixel width of the input image (decoded width when re-encoded, else header sniff, 0 when unknown). */
55
+ readonly originalWidth: number;
56
+ /** Pixel height of the input image; see {@link originalWidth}. */
57
+ readonly originalHeight: number;
58
+ /** True only when `data` differs from the input bytes. */
59
+ readonly changed: boolean;
60
+ readonly originalByteLength: number;
61
+ readonly finalByteLength: number;
62
+ }
63
+ /**
64
+ * Downsample/re-encode `bytes` to fit the pixel + byte budget. Never throws: on
65
+ * any failure (unsupported format, decode error, a result no smaller than the
66
+ * input) the original bytes are returned with `changed: false`.
67
+ */
68
+ export declare function compressImageForModel(bytes: Uint8Array, mimeType: string, options?: CompressImageOptions): Promise<CompressImageResult>;
69
+ //# sourceMappingURL=image-compress.d.ts.map
@@ -0,0 +1,265 @@
1
+ /**
2
+ * Shrink oversized images before they reach the model.
3
+ *
4
+ * Deliberately narrow in scope: a pixel + byte budget, decompression-bomb guards,
5
+ * a lossless-first / JPEG-ladder encode strategy, and a best-effort contract (any
6
+ * decode/encode failure returns the original bytes unchanged, `changed: false`).
7
+ * Out of scope: WebP wasm decoding (WebP/GIF pass through here), region readback,
8
+ * DI config ownership, and telemetry. `jimp` is imported lazily so the codec is
9
+ * only paid for when an image actually needs work.
10
+ *
11
+ * Compression is never silent to the model: the result carries the original
12
+ * dimensions so the caller can annotate "you are looking at a downsampled copy".
13
+ */
14
+ /** Built-in longest-edge ceiling (px). Larger images are scaled down to fit. */
15
+ export const MAX_IMAGE_EDGE_PX = 2000;
16
+ /** Env override for the longest-edge ceiling (px). Ignored unless a positive int. */
17
+ export const MAX_IMAGE_EDGE_ENV = "AGENTS_IMAGE_MAX_EDGE_PX";
18
+ /**
19
+ * Raw-byte budget for images the model reads for itself (Read's image path). A
20
+ * session that keeps screenshotting accumulates every image in the request body
21
+ * on every turn, so per-image size — not the provider's per-image ceiling — is
22
+ * what keeps the total under the request-size limit. 256 KB keeps a clean 2000px
23
+ * UI screenshot on the lossless fast path while capping dense content at a
24
+ * readable q80/1000px JPEG.
25
+ */
26
+ export const READ_IMAGE_BYTE_BUDGET = 256 * 1024;
27
+ /** Env override for the read-image byte budget (bytes). Ignored unless a positive int. */
28
+ export const READ_IMAGE_BYTE_BUDGET_ENV = "AGENTS_IMAGE_READ_BYTE_BUDGET";
29
+ /** Progressively lower JPEG quality until the payload fits the byte budget. */
30
+ const JPEG_QUALITY_STEPS = [80, 60, 40, 20];
31
+ /**
32
+ * Longest-edge step-downs tried when the budget cannot be met at the fitted
33
+ * size. With the built-in 2000px ceiling the first step is a no-op; the
34
+ * sub-1000px tail lets even entropy-upper-bound content (noise, photos) land
35
+ * within a read-scale budget of a few tens of KB.
36
+ */
37
+ const FALLBACK_EDGES_PX = [2000, 1000, 768, 512, 384, 256];
38
+ /**
39
+ * PNG rescales stop at this edge; below it the ladder goes lossy. For a
40
+ * text-bearing screenshot a q80 JPEG at 1000px reads better than a lossless PNG
41
+ * at 512px — resolution beats losslessness once both are degraded.
42
+ */
43
+ const PNG_RESCALE_FLOOR_PX = 1000;
44
+ /**
45
+ * Pixel-count ceiling above which compression is skipped entirely. A tiny-byte,
46
+ * huge-dimension image (e.g. a solid 30000×30000 PNG) would otherwise be fully
47
+ * decoded into a multi-gigabyte bitmap before any resize — a decompression-bomb
48
+ * OOM vector the byte budget alone never catches. The header sniff gives us the
49
+ * dimensions without decoding, so we gate on them first.
50
+ */
51
+ const MAX_DECODE_PIXELS = 100_000_000;
52
+ /**
53
+ * Raw-byte ceiling above which compression is skipped rather than decoded. The
54
+ * byte budget bounds the output, but the compressor still has to load the input
55
+ * first; this bounds that input allocation.
56
+ */
57
+ export const MAX_IMAGE_DECODE_BYTES = 64 * 1024 * 1024;
58
+ /** Formats we can decode and re-encode. Everything else passes through. */
59
+ const RECODABLE_MIME = new Set(["image/png", "image/jpeg"]);
60
+ function positiveIntFromEnv(name) {
61
+ const raw = process.env[name]?.trim();
62
+ if (raw === undefined || raw.length === 0 || !/^\d+$/.test(raw))
63
+ return undefined;
64
+ const parsed = Number(raw);
65
+ return Number.isInteger(parsed) && parsed > 0 ? parsed : undefined;
66
+ }
67
+ export function resolveMaxImageEdgePx() {
68
+ return positiveIntFromEnv(MAX_IMAGE_EDGE_ENV) ?? MAX_IMAGE_EDGE_PX;
69
+ }
70
+ export function resolveReadImageByteBudget() {
71
+ return positiveIntFromEnv(READ_IMAGE_BYTE_BUDGET_ENV) ?? READ_IMAGE_BYTE_BUDGET;
72
+ }
73
+ function normalizeImageMime(mimeType) {
74
+ const bare = mimeType.split(";")[0].trim().toLowerCase();
75
+ return bare === "image/jpg" ? "image/jpeg" : bare;
76
+ }
77
+ /**
78
+ * Downsample/re-encode `bytes` to fit the pixel + byte budget. Never throws: on
79
+ * any failure (unsupported format, decode error, a result no smaller than the
80
+ * input) the original bytes are returned with `changed: false`.
81
+ */
82
+ export async function compressImageForModel(bytes, mimeType, options = {}) {
83
+ const maxEdge = options.maxEdge ?? resolveMaxImageEdgePx();
84
+ const byteBudget = options.byteBudget ?? resolveReadImageByteBudget();
85
+ const maxDecodeBytes = options.maxDecodeBytes ?? MAX_IMAGE_DECODE_BYTES;
86
+ const normalizedMime = normalizeImageMime(mimeType);
87
+ const dims = sniffDimensions(bytes);
88
+ const passthrough = () => ({
89
+ data: bytes,
90
+ mimeType,
91
+ width: dims?.width ?? 0,
92
+ height: dims?.height ?? 0,
93
+ originalWidth: dims?.width ?? 0,
94
+ originalHeight: dims?.height ?? 0,
95
+ changed: false,
96
+ originalByteLength: bytes.length,
97
+ finalByteLength: bytes.length,
98
+ });
99
+ if (bytes.length === 0)
100
+ return passthrough();
101
+ if (!RECODABLE_MIME.has(normalizedMime))
102
+ return passthrough();
103
+ // Fast path: already within both budgets — no codec load, no allocation.
104
+ const longestEdge = dims ? Math.max(dims.width, dims.height) : 0;
105
+ const withinBytes = bytes.length <= byteBudget;
106
+ const withinEdge = longestEdge > 0 && longestEdge <= maxEdge;
107
+ if (withinBytes && (withinEdge || longestEdge === 0))
108
+ return passthrough();
109
+ // Decompression-bomb guards: refuse to decode absurd pixel counts or byte
110
+ // payloads. The sniff above gave us the dimensions without decoding.
111
+ if (dims && dims.width * dims.height > MAX_DECODE_PIXELS)
112
+ return passthrough();
113
+ if (bytes.length > maxDecodeBytes)
114
+ return passthrough();
115
+ try {
116
+ const { Jimp } = await import("jimp");
117
+ const image = await Jimp.fromBuffer(Buffer.from(bytes));
118
+ const preferLossless = normalizedMime !== "image/jpeg";
119
+ const decodedWidth = image.width;
120
+ const decodedHeight = image.height;
121
+ fitWithinEdge(image, maxEdge);
122
+ const encoded = await encodeWithinBudget(image, { preferLossless, byteBudget });
123
+ // Keep the result only when it actually helps: fewer bytes, or fewer pixels
124
+ // (a smaller image costs fewer vision tokens even if the byte count is flat).
125
+ const shrankBytes = encoded.data.length < bytes.length;
126
+ const shrankPixels = encoded.width * encoded.height < decodedWidth * decodedHeight;
127
+ if (!shrankBytes && !shrankPixels)
128
+ return passthrough();
129
+ return {
130
+ data: encoded.data,
131
+ mimeType: encoded.mimeType,
132
+ width: encoded.width,
133
+ height: encoded.height,
134
+ originalWidth: decodedWidth,
135
+ originalHeight: decodedHeight,
136
+ changed: true,
137
+ originalByteLength: bytes.length,
138
+ finalByteLength: encoded.data.length,
139
+ };
140
+ }
141
+ catch {
142
+ return passthrough();
143
+ }
144
+ }
145
+ /**
146
+ * Encode `image` (already fitted to the edge ceiling) under the byte budget.
147
+ * PNG source: PNG at the fitted size → smaller PNG rescales to the floor → JPEG
148
+ * ladder at that size → JPEG ladder at each sub-floor edge. JPEG source: the
149
+ * quality ladder at the fitted size, then again at each fallback rescale.
150
+ */
151
+ async function encodeWithinBudget(image, opts) {
152
+ const { preferLossless, byteBudget } = opts;
153
+ let smallest = null;
154
+ const consider = (data, mimeType) => {
155
+ const candidate = { data, mimeType, width: image.width, height: image.height };
156
+ if (smallest === null || candidate.data.length < smallest.data.length)
157
+ smallest = candidate;
158
+ return candidate;
159
+ };
160
+ const jpegLadder = async () => {
161
+ for (const quality of JPEG_QUALITY_STEPS) {
162
+ const jpeg = await image.getBuffer("image/jpeg", { quality });
163
+ if (jpeg.length <= byteBudget)
164
+ return consider(jpeg, "image/jpeg");
165
+ consider(jpeg, "image/jpeg");
166
+ }
167
+ return null;
168
+ };
169
+ if (preferLossless) {
170
+ const png = await image.getBuffer("image/png");
171
+ if (png.length <= byteBudget)
172
+ return consider(png, "image/png");
173
+ consider(png, "image/png");
174
+ for (const edge of FALLBACK_EDGES_PX) {
175
+ if (edge < PNG_RESCALE_FLOOR_PX)
176
+ break;
177
+ if (!fitWithinEdge(image, edge))
178
+ continue;
179
+ const smallerPng = await image.getBuffer("image/png");
180
+ if (smallerPng.length <= byteBudget)
181
+ return consider(smallerPng, "image/png");
182
+ consider(smallerPng, "image/png");
183
+ }
184
+ const atFloor = await jpegLadder();
185
+ if (atFloor !== null)
186
+ return atFloor;
187
+ for (const edge of FALLBACK_EDGES_PX) {
188
+ if (edge >= PNG_RESCALE_FLOOR_PX)
189
+ continue;
190
+ if (!fitWithinEdge(image, edge))
191
+ continue;
192
+ const atEdge = await jpegLadder();
193
+ if (atEdge !== null)
194
+ return atEdge;
195
+ }
196
+ return smallest;
197
+ }
198
+ const atFitted = await jpegLadder();
199
+ if (atFitted !== null)
200
+ return atFitted;
201
+ for (const edge of FALLBACK_EDGES_PX) {
202
+ if (!fitWithinEdge(image, edge))
203
+ continue;
204
+ const atEdge = await jpegLadder();
205
+ if (atEdge !== null)
206
+ return atEdge;
207
+ }
208
+ return smallest;
209
+ }
210
+ /**
211
+ * Scale `image` so its longest edge is at most `edge`, preserving aspect ratio.
212
+ * No-op (returns false) when the image already fits. Passes no `mode`: jimp's
213
+ * default resizer downscales with a full-coverage area average that does not
214
+ * alias — do not "upgrade" this to a named ResizeStrategy.
215
+ */
216
+ function fitWithinEdge(image, edge) {
217
+ const longest = Math.max(image.width, image.height);
218
+ if (longest <= edge)
219
+ return false;
220
+ const factor = edge / longest;
221
+ image.resize({
222
+ w: Math.max(1, Math.round(image.width * factor)),
223
+ h: Math.max(1, Math.round(image.height * factor)),
224
+ });
225
+ return true;
226
+ }
227
+ /** Header-only dimension sniff for PNG/JPEG (no decode). Returns null if unknown. */
228
+ function sniffDimensions(data) {
229
+ const buf = Buffer.isBuffer(data) ? data : Buffer.from(data.buffer, data.byteOffset, data.byteLength);
230
+ if (buf.length >= 24 &&
231
+ buf.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) {
232
+ return { width: buf.readUInt32BE(16), height: buf.readUInt32BE(20) };
233
+ }
234
+ return sniffJpeg(buf);
235
+ }
236
+ function sniffJpeg(data) {
237
+ if (data.length < 4 || data[0] !== 0xff || data[1] !== 0xd8)
238
+ return null;
239
+ let offset = 2;
240
+ while (offset + 9 < data.length) {
241
+ if (data[offset] !== 0xff)
242
+ return null;
243
+ const marker = data[offset + 1];
244
+ offset += 2;
245
+ if (marker === 0xd8 || marker === 0xd9)
246
+ continue;
247
+ if (offset + 2 > data.length)
248
+ return null;
249
+ const length = data.readUInt16BE(offset);
250
+ if (length < 2 || offset + length > data.length)
251
+ return null;
252
+ if (isJpegStartOfFrame(marker) && length >= 7) {
253
+ return { width: data.readUInt16BE(offset + 5), height: data.readUInt16BE(offset + 3) };
254
+ }
255
+ offset += length;
256
+ }
257
+ return null;
258
+ }
259
+ function isJpegStartOfFrame(marker) {
260
+ return ((marker >= 0xc0 && marker <= 0xc3) ||
261
+ (marker >= 0xc5 && marker <= 0xc7) ||
262
+ (marker >= 0xc9 && marker <= 0xcb) ||
263
+ (marker >= 0xcd && marker <= 0xcf));
264
+ }
265
+ //# sourceMappingURL=image-compress.js.map
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Shared building blocks for Machine implementations and their callers.
3
+ *
4
+ * The high-level operations (writeTextIfUnchanged / realpath) are mandatory
5
+ * Machine members; their default compositions live in BaseMachine
6
+ * (machine-base.ts). This module carries what both sides agree on: FileVersion
7
+ * helpers, the decode contract, and the caller-side text-file helpers
8
+ * (readTextFile / writeTextFile) composed from the core members.
9
+ */
10
+ import type { DecodeErrors, Machine, FileInfo, FileVersion } from "../machine.ts";
11
+ export declare function fileVersionFromInfo(info: FileInfo): FileVersion;
12
+ /** Strip a UTF BOM and normalize CRLF to LF — the form ledger content comparisons use. */
13
+ export declare function normalizeForCompare(text: string): string;
14
+ /**
15
+ * Both sides must HAVE an mtime and agree on it. A missing mtime is never a match:
16
+ * it is the absence of evidence, and the caller's next move is a content review,
17
+ * not a pass.
18
+ */
19
+ export declare function fileVersionsMatch(expected: FileVersion, current: FileVersion): boolean;
20
+ /** Decode with the shared contract: strict UTF-8 throws on invalid bytes (binary detection). */
21
+ export declare function decodeText(data: Buffer, options?: {
22
+ encoding?: BufferEncoding;
23
+ errors?: DecodeErrors;
24
+ }): string;
25
+ /** Whole-file text read composed from the core members (readBytes + decode). */
26
+ export declare function readTextFile(host: Pick<Machine, "readBytes">, path: string, options?: {
27
+ encoding?: BufferEncoding;
28
+ errors?: DecodeErrors;
29
+ }): Promise<string>;
30
+ /** Unconditional whole-file text write — thin sugar over Machine.writeText. */
31
+ export declare function writeTextFile(host: Pick<Machine, "writeText">, path: string, data: string, options?: {
32
+ encoding?: BufferEncoding;
33
+ }): Promise<void>;
34
+ //# sourceMappingURL=machine-ops.d.ts.map
@@ -0,0 +1,34 @@
1
+ export function fileVersionFromInfo(info) {
2
+ return info.mtimeMs === undefined ? {} : { mtimeMs: info.mtimeMs };
3
+ }
4
+ /** Strip a UTF BOM and normalize CRLF to LF — the form ledger content comparisons use. */
5
+ export function normalizeForCompare(text) {
6
+ const stripped = text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
7
+ return stripped.replaceAll("\r\n", "\n");
8
+ }
9
+ /**
10
+ * Both sides must HAVE an mtime and agree on it. A missing mtime is never a match:
11
+ * it is the absence of evidence, and the caller's next move is a content review,
12
+ * not a pass.
13
+ */
14
+ export function fileVersionsMatch(expected, current) {
15
+ return expected.mtimeMs !== undefined && current.mtimeMs !== undefined && expected.mtimeMs === current.mtimeMs;
16
+ }
17
+ /** Decode with the shared contract: strict UTF-8 throws on invalid bytes (binary detection). */
18
+ export function decodeText(data, options) {
19
+ const encoding = options?.encoding ?? "utf8";
20
+ const errors = options?.errors ?? "strict";
21
+ if ((encoding === "utf8" || encoding === "utf-8") && errors === "strict") {
22
+ return new TextDecoder("utf-8", { fatal: true }).decode(data);
23
+ }
24
+ return data.toString(encoding);
25
+ }
26
+ /** Whole-file text read composed from the core members (readBytes + decode). */
27
+ export async function readTextFile(host, path, options) {
28
+ return decodeText(await host.readBytes(path), options);
29
+ }
30
+ /** Unconditional whole-file text write — thin sugar over Machine.writeText. */
31
+ export async function writeTextFile(host, path, data, options) {
32
+ await host.writeText(path, data, options?.encoding !== undefined ? { encoding: options.encoding } : {});
33
+ }
34
+ //# sourceMappingURL=machine-ops.js.map
@@ -0,0 +1,12 @@
1
+ import { type PathClass } from "../policies/path-access.ts";
2
+ export interface PermissionPathMatchOptions {
3
+ readonly cwd?: string;
4
+ readonly pathClass?: PathClass;
5
+ readonly homeDir?: string;
6
+ readonly caseInsensitivePaths?: boolean;
7
+ }
8
+ export declare function globMatch(value: string, pattern: string, options?: {
9
+ nocase?: boolean;
10
+ }): boolean;
11
+ export declare function pathGlobMatch(value: string, pattern: string, pathOptions?: PermissionPathMatchOptions): boolean;
12
+ //# sourceMappingURL=path-glob-match.d.ts.map