@dirgha/code 1.2.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 (360) hide show
  1. package/LICENSE +101 -0
  2. package/README.md +674 -0
  3. package/dist/dirgha.mjs +2185 -0
  4. package/dist_v2/acp/index.d.ts +1 -0
  5. package/dist_v2/acp/index.js +2 -0
  6. package/dist_v2/acp/server.d.ts +31 -0
  7. package/dist_v2/acp/server.js +137 -0
  8. package/dist_v2/cli/auth-cmd.d.ts +10 -0
  9. package/dist_v2/cli/auth-cmd.js +69 -0
  10. package/dist_v2/cli/config.d.ts +46 -0
  11. package/dist_v2/cli/config.js +70 -0
  12. package/dist_v2/cli/flags.d.ts +10 -0
  13. package/dist_v2/cli/flags.js +49 -0
  14. package/dist_v2/cli/flows/full-cycle.d.ts +30 -0
  15. package/dist_v2/cli/flows/full-cycle.js +39 -0
  16. package/dist_v2/cli/flows/index.d.ts +2 -0
  17. package/dist_v2/cli/flows/index.js +3 -0
  18. package/dist_v2/cli/flows/tarball.d.ts +10 -0
  19. package/dist_v2/cli/flows/tarball.js +47 -0
  20. package/dist_v2/cli/index.d.ts +4 -0
  21. package/dist_v2/cli/index.js +5 -0
  22. package/dist_v2/cli/interactive.d.ts +21 -0
  23. package/dist_v2/cli/interactive.js +207 -0
  24. package/dist_v2/cli/main.d.ts +10 -0
  25. package/dist_v2/cli/main.js +173 -0
  26. package/dist_v2/cli/models-cmd.d.ts +6 -0
  27. package/dist_v2/cli/models-cmd.js +50 -0
  28. package/dist_v2/cli/setup.d.ts +13 -0
  29. package/dist_v2/cli/setup.js +128 -0
  30. package/dist_v2/cli/slash/account.d.ts +10 -0
  31. package/dist_v2/cli/slash/account.js +59 -0
  32. package/dist_v2/cli/slash/clear.d.ts +8 -0
  33. package/dist_v2/cli/slash/clear.js +15 -0
  34. package/dist_v2/cli/slash/compact.d.ts +7 -0
  35. package/dist_v2/cli/slash/compact.js +13 -0
  36. package/dist_v2/cli/slash/config.d.ts +8 -0
  37. package/dist_v2/cli/slash/config.js +49 -0
  38. package/dist_v2/cli/slash/exit.d.ts +7 -0
  39. package/dist_v2/cli/slash/exit.js +16 -0
  40. package/dist_v2/cli/slash/fleet.d.ts +7 -0
  41. package/dist_v2/cli/slash/fleet.js +22 -0
  42. package/dist_v2/cli/slash/help.d.ts +9 -0
  43. package/dist_v2/cli/slash/help.js +31 -0
  44. package/dist_v2/cli/slash/history.d.ts +7 -0
  45. package/dist_v2/cli/slash/history.js +42 -0
  46. package/dist_v2/cli/slash/index.d.ts +30 -0
  47. package/dist_v2/cli/slash/index.js +52 -0
  48. package/dist_v2/cli/slash/init.d.ts +8 -0
  49. package/dist_v2/cli/slash/init.js +57 -0
  50. package/dist_v2/cli/slash/keys.d.ts +8 -0
  51. package/dist_v2/cli/slash/keys.js +101 -0
  52. package/dist_v2/cli/slash/login.d.ts +10 -0
  53. package/dist_v2/cli/slash/login.js +47 -0
  54. package/dist_v2/cli/slash/memory.d.ts +8 -0
  55. package/dist_v2/cli/slash/memory.js +101 -0
  56. package/dist_v2/cli/slash/mode.d.ts +8 -0
  57. package/dist_v2/cli/slash/mode.js +30 -0
  58. package/dist_v2/cli/slash/models.d.ts +8 -0
  59. package/dist_v2/cli/slash/models.js +73 -0
  60. package/dist_v2/cli/slash/resume.d.ts +8 -0
  61. package/dist_v2/cli/slash/resume.js +24 -0
  62. package/dist_v2/cli/slash/session.d.ts +8 -0
  63. package/dist_v2/cli/slash/session.js +74 -0
  64. package/dist_v2/cli/slash/setup.d.ts +8 -0
  65. package/dist_v2/cli/slash/setup.js +50 -0
  66. package/dist_v2/cli/slash/status.d.ts +7 -0
  67. package/dist_v2/cli/slash/status.js +37 -0
  68. package/dist_v2/cli/slash/theme.d.ts +9 -0
  69. package/dist_v2/cli/slash/theme.js +53 -0
  70. package/dist_v2/cli/slash/types.d.ts +14 -0
  71. package/dist_v2/cli/slash/types.js +9 -0
  72. package/dist_v2/cli/slash/upgrade.d.ts +9 -0
  73. package/dist_v2/cli/slash/upgrade.js +66 -0
  74. package/dist_v2/cli/slash.d.ts +72 -0
  75. package/dist_v2/cli/slash.js +77 -0
  76. package/dist_v2/cli/subcommands/ask.d.ts +17 -0
  77. package/dist_v2/cli/subcommands/ask.js +76 -0
  78. package/dist_v2/cli/subcommands/audit.d.ts +15 -0
  79. package/dist_v2/cli/subcommands/audit.js +128 -0
  80. package/dist_v2/cli/subcommands/chat.d.ts +13 -0
  81. package/dist_v2/cli/subcommands/chat.js +72 -0
  82. package/dist_v2/cli/subcommands/compact.d.ts +14 -0
  83. package/dist_v2/cli/subcommands/compact.js +114 -0
  84. package/dist_v2/cli/subcommands/doctor.d.ts +11 -0
  85. package/dist_v2/cli/subcommands/doctor.js +103 -0
  86. package/dist_v2/cli/subcommands/export-session.d.ts +10 -0
  87. package/dist_v2/cli/subcommands/export-session.js +60 -0
  88. package/dist_v2/cli/subcommands/import-session.d.ts +11 -0
  89. package/dist_v2/cli/subcommands/import-session.js +77 -0
  90. package/dist_v2/cli/subcommands/index.d.ts +40 -0
  91. package/dist_v2/cli/subcommands/index.js +38 -0
  92. package/dist_v2/cli/subcommands/init.d.ts +10 -0
  93. package/dist_v2/cli/subcommands/init.js +74 -0
  94. package/dist_v2/cli/subcommands/keys.d.ts +12 -0
  95. package/dist_v2/cli/subcommands/keys.js +136 -0
  96. package/dist_v2/cli/subcommands/login.d.ts +10 -0
  97. package/dist_v2/cli/subcommands/login.js +60 -0
  98. package/dist_v2/cli/subcommands/logout.d.ts +10 -0
  99. package/dist_v2/cli/subcommands/logout.js +21 -0
  100. package/dist_v2/cli/subcommands/models.d.ts +14 -0
  101. package/dist_v2/cli/subcommands/models.js +144 -0
  102. package/dist_v2/cli/subcommands/setup.d.ts +11 -0
  103. package/dist_v2/cli/subcommands/setup.js +107 -0
  104. package/dist_v2/cli/subcommands/stats.d.ts +13 -0
  105. package/dist_v2/cli/subcommands/stats.js +148 -0
  106. package/dist_v2/cli/subcommands/status.d.ts +14 -0
  107. package/dist_v2/cli/subcommands/status.js +91 -0
  108. package/dist_v2/cli/submit-paper.d.ts +15 -0
  109. package/dist_v2/cli/submit-paper.js +68 -0
  110. package/dist_v2/context/_fts.d.ts +62 -0
  111. package/dist_v2/context/_fts.js +195 -0
  112. package/dist_v2/context/branch.d.ts +18 -0
  113. package/dist_v2/context/branch.js +66 -0
  114. package/dist_v2/context/compaction.d.ts +26 -0
  115. package/dist_v2/context/compaction.js +120 -0
  116. package/dist_v2/context/index.d.ts +5 -0
  117. package/dist_v2/context/index.js +6 -0
  118. package/dist_v2/context/knowledge.d.ts +38 -0
  119. package/dist_v2/context/knowledge.js +146 -0
  120. package/dist_v2/context/memory.d.ts +80 -0
  121. package/dist_v2/context/memory.js +276 -0
  122. package/dist_v2/context/mode.d.ts +25 -0
  123. package/dist_v2/context/mode.js +81 -0
  124. package/dist_v2/context/session.d.ts +56 -0
  125. package/dist_v2/context/session.js +108 -0
  126. package/dist_v2/daemon/index.d.ts +2 -0
  127. package/dist_v2/daemon/index.js +3 -0
  128. package/dist_v2/daemon/protocol.d.ts +73 -0
  129. package/dist_v2/daemon/protocol.js +6 -0
  130. package/dist_v2/daemon/server.d.ts +35 -0
  131. package/dist_v2/daemon/server.js +191 -0
  132. package/dist_v2/eval/index.d.ts +5 -0
  133. package/dist_v2/eval/index.js +6 -0
  134. package/dist_v2/eval/regression.d.ts +14 -0
  135. package/dist_v2/eval/regression.js +106 -0
  136. package/dist_v2/eval/reporter.d.ts +7 -0
  137. package/dist_v2/eval/reporter.js +34 -0
  138. package/dist_v2/eval/swe-bench.d.ts +18 -0
  139. package/dist_v2/eval/swe-bench.js +23 -0
  140. package/dist_v2/eval/terminal-bench.d.ts +13 -0
  141. package/dist_v2/eval/terminal-bench.js +19 -0
  142. package/dist_v2/eval/types.d.ts +36 -0
  143. package/dist_v2/eval/types.js +5 -0
  144. package/dist_v2/fleet/apply-back.d.ts +27 -0
  145. package/dist_v2/fleet/apply-back.js +191 -0
  146. package/dist_v2/fleet/cli-command.d.ts +61 -0
  147. package/dist_v2/fleet/cli-command.js +329 -0
  148. package/dist_v2/fleet/index.d.ts +21 -0
  149. package/dist_v2/fleet/index.js +21 -0
  150. package/dist_v2/fleet/runner.d.ts +28 -0
  151. package/dist_v2/fleet/runner.js +347 -0
  152. package/dist_v2/fleet/tripleshot.d.ts +21 -0
  153. package/dist_v2/fleet/tripleshot.js +222 -0
  154. package/dist_v2/fleet/types.d.ts +193 -0
  155. package/dist_v2/fleet/types.js +24 -0
  156. package/dist_v2/fleet/worktree.d.ts +51 -0
  157. package/dist_v2/fleet/worktree.js +176 -0
  158. package/dist_v2/hooks/index.d.ts +1 -0
  159. package/dist_v2/hooks/index.js +2 -0
  160. package/dist_v2/hooks/registry.d.ts +21 -0
  161. package/dist_v2/hooks/registry.js +40 -0
  162. package/dist_v2/index.d.ts +12 -0
  163. package/dist_v2/index.js +11 -0
  164. package/dist_v2/integrations/arniko-client.d.ts +42 -0
  165. package/dist_v2/integrations/arniko-client.js +86 -0
  166. package/dist_v2/integrations/auth.d.ts +45 -0
  167. package/dist_v2/integrations/auth.js +54 -0
  168. package/dist_v2/integrations/billing.d.ts +71 -0
  169. package/dist_v2/integrations/billing.js +107 -0
  170. package/dist_v2/integrations/bucky-client.d.ts +105 -0
  171. package/dist_v2/integrations/bucky-client.js +76 -0
  172. package/dist_v2/integrations/deploy-client.d.ts +60 -0
  173. package/dist_v2/integrations/deploy-client.js +87 -0
  174. package/dist_v2/integrations/device-auth.d.ts +48 -0
  175. package/dist_v2/integrations/device-auth.js +192 -0
  176. package/dist_v2/integrations/entitlements.d.ts +34 -0
  177. package/dist_v2/integrations/entitlements.js +43 -0
  178. package/dist_v2/integrations/http.d.ts +22 -0
  179. package/dist_v2/integrations/http.js +114 -0
  180. package/dist_v2/integrations/index.d.ts +8 -0
  181. package/dist_v2/integrations/index.js +15 -0
  182. package/dist_v2/intelligence/cost.d.ts +28 -0
  183. package/dist_v2/intelligence/cost.js +58 -0
  184. package/dist_v2/intelligence/error-classifier.d.ts +8 -0
  185. package/dist_v2/intelligence/error-classifier.js +71 -0
  186. package/dist_v2/intelligence/index.d.ts +5 -0
  187. package/dist_v2/intelligence/index.js +6 -0
  188. package/dist_v2/intelligence/prices.d.ts +13 -0
  189. package/dist_v2/intelligence/prices.js +26 -0
  190. package/dist_v2/intelligence/smart-router.d.ts +21 -0
  191. package/dist_v2/intelligence/smart-router.js +59 -0
  192. package/dist_v2/intelligence/telemetry.d.ts +22 -0
  193. package/dist_v2/intelligence/telemetry.js +31 -0
  194. package/dist_v2/kernel/agent-loop.d.ts +32 -0
  195. package/dist_v2/kernel/agent-loop.js +151 -0
  196. package/dist_v2/kernel/event-stream.d.ts +18 -0
  197. package/dist_v2/kernel/event-stream.js +102 -0
  198. package/dist_v2/kernel/index.d.ts +4 -0
  199. package/dist_v2/kernel/index.js +5 -0
  200. package/dist_v2/kernel/message.d.ts +29 -0
  201. package/dist_v2/kernel/message.js +118 -0
  202. package/dist_v2/kernel/types.d.ts +206 -0
  203. package/dist_v2/kernel/types.js +9 -0
  204. package/dist_v2/mcp/client.d.ts +44 -0
  205. package/dist_v2/mcp/client.js +63 -0
  206. package/dist_v2/mcp/index.d.ts +3 -0
  207. package/dist_v2/mcp/index.js +4 -0
  208. package/dist_v2/mcp/tool-bridge.d.ts +11 -0
  209. package/dist_v2/mcp/tool-bridge.js +33 -0
  210. package/dist_v2/mcp/transport.d.ts +29 -0
  211. package/dist_v2/mcp/transport.js +73 -0
  212. package/dist_v2/parity/index.d.ts +3 -0
  213. package/dist_v2/parity/index.js +4 -0
  214. package/dist_v2/parity/mock-openai-compat.d.ts +15 -0
  215. package/dist_v2/parity/mock-openai-compat.js +35 -0
  216. package/dist_v2/parity/runner.d.ts +21 -0
  217. package/dist_v2/parity/runner.js +76 -0
  218. package/dist_v2/parity/scenarios.d.ts +20 -0
  219. package/dist_v2/parity/scenarios.js +70 -0
  220. package/dist_v2/providers/anthropic.d.ts +21 -0
  221. package/dist_v2/providers/anthropic.js +191 -0
  222. package/dist_v2/providers/dispatch.d.ts +11 -0
  223. package/dist_v2/providers/dispatch.js +45 -0
  224. package/dist_v2/providers/fireworks.d.ts +18 -0
  225. package/dist_v2/providers/fireworks.js +44 -0
  226. package/dist_v2/providers/gemini.d.ts +19 -0
  227. package/dist_v2/providers/gemini.js +188 -0
  228. package/dist_v2/providers/http.d.ts +41 -0
  229. package/dist_v2/providers/http.js +188 -0
  230. package/dist_v2/providers/iface.d.ts +18 -0
  231. package/dist_v2/providers/iface.js +18 -0
  232. package/dist_v2/providers/index.d.ts +39 -0
  233. package/dist_v2/providers/index.js +51 -0
  234. package/dist_v2/providers/nvidia.d.ts +27 -0
  235. package/dist_v2/providers/nvidia.js +142 -0
  236. package/dist_v2/providers/ollama.d.ts +15 -0
  237. package/dist_v2/providers/ollama.js +37 -0
  238. package/dist_v2/providers/openai-compat.d.ts +29 -0
  239. package/dist_v2/providers/openai-compat.js +239 -0
  240. package/dist_v2/providers/openai.d.ts +20 -0
  241. package/dist_v2/providers/openai.js +107 -0
  242. package/dist_v2/providers/openrouter.d.ts +24 -0
  243. package/dist_v2/providers/openrouter.js +69 -0
  244. package/dist_v2/safety/approval-bus.d.ts +26 -0
  245. package/dist_v2/safety/approval-bus.js +48 -0
  246. package/dist_v2/safety/audit-log.d.ts +27 -0
  247. package/dist_v2/safety/audit-log.js +71 -0
  248. package/dist_v2/safety/index.d.ts +4 -0
  249. package/dist_v2/safety/index.js +5 -0
  250. package/dist_v2/safety/policy.d.ts +32 -0
  251. package/dist_v2/safety/policy.js +131 -0
  252. package/dist_v2/safety/sandbox/bwrap.d.ts +11 -0
  253. package/dist_v2/safety/sandbox/bwrap.js +52 -0
  254. package/dist_v2/safety/sandbox/iface.d.ts +29 -0
  255. package/dist_v2/safety/sandbox/iface.js +8 -0
  256. package/dist_v2/safety/sandbox/index.d.ts +7 -0
  257. package/dist_v2/safety/sandbox/index.js +8 -0
  258. package/dist_v2/safety/sandbox/landlock.d.ts +17 -0
  259. package/dist_v2/safety/sandbox/landlock.js +26 -0
  260. package/dist_v2/safety/sandbox/noop.d.ts +12 -0
  261. package/dist_v2/safety/sandbox/noop.js +52 -0
  262. package/dist_v2/safety/sandbox/seatbelt.d.ts +11 -0
  263. package/dist_v2/safety/sandbox/seatbelt.js +49 -0
  264. package/dist_v2/safety/sandbox/select.d.ts +6 -0
  265. package/dist_v2/safety/sandbox/select.js +42 -0
  266. package/dist_v2/safety/sandbox/windows.d.ts +13 -0
  267. package/dist_v2/safety/sandbox/windows.js +22 -0
  268. package/dist_v2/skills/index.d.ts +3 -0
  269. package/dist_v2/skills/index.js +4 -0
  270. package/dist_v2/skills/loader.d.ts +38 -0
  271. package/dist_v2/skills/loader.js +102 -0
  272. package/dist_v2/skills/matcher.d.ts +14 -0
  273. package/dist_v2/skills/matcher.js +72 -0
  274. package/dist_v2/skills/runtime.d.ts +13 -0
  275. package/dist_v2/skills/runtime.js +40 -0
  276. package/dist_v2/subagents/delegator.d.ts +36 -0
  277. package/dist_v2/subagents/delegator.js +82 -0
  278. package/dist_v2/subagents/index.d.ts +2 -0
  279. package/dist_v2/subagents/index.js +3 -0
  280. package/dist_v2/subagents/pool.d.ts +18 -0
  281. package/dist_v2/subagents/pool.js +31 -0
  282. package/dist_v2/tools/browser.d.ts +27 -0
  283. package/dist_v2/tools/browser.js +179 -0
  284. package/dist_v2/tools/checkpoint.d.ts +25 -0
  285. package/dist_v2/tools/checkpoint.js +222 -0
  286. package/dist_v2/tools/cron.d.ts +32 -0
  287. package/dist_v2/tools/cron.js +160 -0
  288. package/dist_v2/tools/diff.d.ts +18 -0
  289. package/dist_v2/tools/diff.js +117 -0
  290. package/dist_v2/tools/exec.d.ts +19 -0
  291. package/dist_v2/tools/exec.js +49 -0
  292. package/dist_v2/tools/fs-edit.d.ts +11 -0
  293. package/dist_v2/tools/fs-edit.js +81 -0
  294. package/dist_v2/tools/fs-ls.d.ts +7 -0
  295. package/dist_v2/tools/fs-ls.js +57 -0
  296. package/dist_v2/tools/fs-read.d.ts +7 -0
  297. package/dist_v2/tools/fs-read.js +60 -0
  298. package/dist_v2/tools/fs-write.d.ts +8 -0
  299. package/dist_v2/tools/fs-write.js +46 -0
  300. package/dist_v2/tools/git.d.ts +8 -0
  301. package/dist_v2/tools/git.js +57 -0
  302. package/dist_v2/tools/index.d.ts +25 -0
  303. package/dist_v2/tools/index.js +39 -0
  304. package/dist_v2/tools/multimodal.d.ts +41 -0
  305. package/dist_v2/tools/multimodal.js +381 -0
  306. package/dist_v2/tools/permission.d.ts +27 -0
  307. package/dist_v2/tools/permission.js +43 -0
  308. package/dist_v2/tools/registry.d.ts +44 -0
  309. package/dist_v2/tools/registry.js +62 -0
  310. package/dist_v2/tools/search-glob.d.ts +7 -0
  311. package/dist_v2/tools/search-glob.js +98 -0
  312. package/dist_v2/tools/search-grep.d.ts +7 -0
  313. package/dist_v2/tools/search-grep.js +109 -0
  314. package/dist_v2/tools/shell.d.ts +10 -0
  315. package/dist_v2/tools/shell.js +76 -0
  316. package/dist_v2/tools/task.d.ts +8 -0
  317. package/dist_v2/tools/task.js +37 -0
  318. package/dist_v2/tui/approval.d.ts +7 -0
  319. package/dist_v2/tui/approval.js +55 -0
  320. package/dist_v2/tui/index.d.ts +4 -0
  321. package/dist_v2/tui/index.js +5 -0
  322. package/dist_v2/tui/ink/App.d.ts +39 -0
  323. package/dist_v2/tui/ink/App.js +206 -0
  324. package/dist_v2/tui/ink/components/AtFileComplete.d.ts +25 -0
  325. package/dist_v2/tui/ink/components/AtFileComplete.js +158 -0
  326. package/dist_v2/tui/ink/components/HelpOverlay.d.ts +24 -0
  327. package/dist_v2/tui/ink/components/HelpOverlay.js +130 -0
  328. package/dist_v2/tui/ink/components/InputBox.d.ts +35 -0
  329. package/dist_v2/tui/ink/components/InputBox.js +153 -0
  330. package/dist_v2/tui/ink/components/Logo.d.ts +12 -0
  331. package/dist_v2/tui/ink/components/Logo.js +27 -0
  332. package/dist_v2/tui/ink/components/ModelPicker.d.ts +26 -0
  333. package/dist_v2/tui/ink/components/ModelPicker.js +80 -0
  334. package/dist_v2/tui/ink/components/PasteCollapse.d.ts +36 -0
  335. package/dist_v2/tui/ink/components/PasteCollapse.js +59 -0
  336. package/dist_v2/tui/ink/components/StatusBar.d.ts +18 -0
  337. package/dist_v2/tui/ink/components/StatusBar.js +41 -0
  338. package/dist_v2/tui/ink/components/StreamingText.d.ts +14 -0
  339. package/dist_v2/tui/ink/components/StreamingText.js +11 -0
  340. package/dist_v2/tui/ink/components/ThinkingBlock.d.ts +13 -0
  341. package/dist_v2/tui/ink/components/ThinkingBlock.js +13 -0
  342. package/dist_v2/tui/ink/components/ToolBox.d.ts +20 -0
  343. package/dist_v2/tui/ink/components/ToolBox.js +63 -0
  344. package/dist_v2/tui/ink/components/vim-bindings.d.ts +43 -0
  345. package/dist_v2/tui/ink/components/vim-bindings.js +135 -0
  346. package/dist_v2/tui/ink/index.d.ts +39 -0
  347. package/dist_v2/tui/ink/index.js +47 -0
  348. package/dist_v2/tui/ink/use-event-projection.d.ts +51 -0
  349. package/dist_v2/tui/ink/use-event-projection.js +126 -0
  350. package/dist_v2/tui/ink/use-overlays.d.ts +24 -0
  351. package/dist_v2/tui/ink/use-overlays.js +50 -0
  352. package/dist_v2/tui/renderer.d.ts +19 -0
  353. package/dist_v2/tui/renderer.js +64 -0
  354. package/dist_v2/tui/status-bar.d.ts +12 -0
  355. package/dist_v2/tui/status-bar.js +16 -0
  356. package/dist_v2/tui/theme.d.ts +27 -0
  357. package/dist_v2/tui/theme.js +56 -0
  358. package/dist_v2/utils/json-repair.d.ts +7 -0
  359. package/dist_v2/utils/json-repair.js +45 -0
  360. package/package.json +113 -0
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Smart router: chooses a cheap model for short, non-code questions
3
+ * and the default model otherwise. Heuristic-driven; deterministic and
4
+ * cheap enough to run per turn.
5
+ */
6
+ import type { Message } from '../kernel/types.js';
7
+ export interface SmartRouterConfig {
8
+ enabled: boolean;
9
+ cheapModel: string;
10
+ defaultModel: string;
11
+ maxCheapChars?: number;
12
+ maxCheapWords?: number;
13
+ }
14
+ export interface RouteDecision {
15
+ model: string;
16
+ reason: string;
17
+ }
18
+ export interface SmartRouter {
19
+ route(messages: Message[]): RouteDecision;
20
+ }
21
+ export declare function createSmartRouter(cfg: SmartRouterConfig): SmartRouter;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Smart router: chooses a cheap model for short, non-code questions
3
+ * and the default model otherwise. Heuristic-driven; deterministic and
4
+ * cheap enough to run per turn.
5
+ */
6
+ const TOOL_INTENT_KEYWORDS = [
7
+ 'edit', 'create', 'delete', 'run', 'install', 'refactor', 'fix',
8
+ 'open', 'write', 'test', 'deploy', 'commit', 'push', 'merge',
9
+ ];
10
+ export function createSmartRouter(cfg) {
11
+ return {
12
+ route(messages) {
13
+ if (!cfg.enabled) {
14
+ return { model: cfg.defaultModel, reason: 'router disabled' };
15
+ }
16
+ const latestUser = latestUserMessage(messages);
17
+ if (!latestUser) {
18
+ return { model: cfg.defaultModel, reason: 'no user message' };
19
+ }
20
+ const text = extractText(latestUser);
21
+ if (looksComplex(text, cfg)) {
22
+ return { model: cfg.defaultModel, reason: 'message looks complex' };
23
+ }
24
+ return { model: cfg.cheapModel, reason: 'short + no code/tool signals' };
25
+ },
26
+ };
27
+ }
28
+ function latestUserMessage(messages) {
29
+ for (let i = messages.length - 1; i >= 0; i--) {
30
+ if (messages[i].role === 'user')
31
+ return messages[i];
32
+ }
33
+ return undefined;
34
+ }
35
+ function extractText(msg) {
36
+ if (typeof msg.content === 'string')
37
+ return msg.content;
38
+ return msg.content
39
+ .filter((p) => p.type === 'text')
40
+ .map(p => p.text)
41
+ .join('');
42
+ }
43
+ function looksComplex(text, cfg) {
44
+ const maxChars = cfg.maxCheapChars ?? 160;
45
+ const maxWords = cfg.maxCheapWords ?? 28;
46
+ if (text.length > maxChars)
47
+ return true;
48
+ if (text.split(/\s+/).length > maxWords)
49
+ return true;
50
+ if (/```/.test(text))
51
+ return true;
52
+ if (/\bhttps?:\/\//.test(text))
53
+ return true;
54
+ if (/[{};]\s*$/m.test(text))
55
+ return true;
56
+ const lower = text.toLowerCase();
57
+ return TOOL_INTENT_KEYWORDS.some(k => lower.includes(k));
58
+ }
59
+ //# sourceMappingURL=smart-router.js.map
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Opt-in anonymised telemetry. Disabled by default; when enabled, posts
3
+ * a small JSON envelope to the configured endpoint. Content is never
4
+ * transmitted — only command name, duration, model, and success state.
5
+ */
6
+ export interface TelemetryEvent {
7
+ command: string;
8
+ model?: string;
9
+ durationMs?: number;
10
+ success: boolean;
11
+ errorReason?: string;
12
+ }
13
+ export interface TelemetryOptions {
14
+ enabled: boolean;
15
+ endpoint?: string;
16
+ timeoutMs?: number;
17
+ anonId?: string;
18
+ }
19
+ export interface Telemetry {
20
+ record(event: TelemetryEvent): Promise<void>;
21
+ }
22
+ export declare function createTelemetry(opts: TelemetryOptions): Telemetry;
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Opt-in anonymised telemetry. Disabled by default; when enabled, posts
3
+ * a small JSON envelope to the configured endpoint. Content is never
4
+ * transmitted — only command name, duration, model, and success state.
5
+ */
6
+ export function createTelemetry(opts) {
7
+ if (!opts.enabled) {
8
+ return { async record() { } };
9
+ }
10
+ const endpoint = opts.endpoint ?? 'https://telemetry.dirgha.ai/events';
11
+ const timeout = opts.timeoutMs ?? 2000;
12
+ const anonId = opts.anonId ?? 'anon';
13
+ return {
14
+ async record(event) {
15
+ const payload = { ...event, anonId, ts: new Date().toISOString() };
16
+ try {
17
+ const signal = AbortSignal.timeout(timeout);
18
+ await fetch(endpoint, {
19
+ method: 'POST',
20
+ headers: { 'Content-Type': 'application/json' },
21
+ body: JSON.stringify(payload),
22
+ signal,
23
+ });
24
+ }
25
+ catch {
26
+ // Telemetry is fire-and-forget; failures are swallowed.
27
+ }
28
+ },
29
+ };
30
+ }
31
+ //# sourceMappingURL=telemetry.js.map
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Agent loop: ReAct with optional plan-then-execute.
3
+ *
4
+ * The loop is the sole caller of Provider.stream and the sole caller of
5
+ * ToolExecutor.execute. All layering conventions are enforced here:
6
+ *
7
+ * 1. Every tool call passes through the ApprovalBus seam.
8
+ * 2. Every event flows through the EventStream (no side-channels).
9
+ * 3. Errors from providers are classified by the injected ErrorClassifier
10
+ * before any retry decision; there is no string matching.
11
+ * 4. Context transform (compaction, skill injection) is invoked once per
12
+ * turn before the provider call, never mid-stream.
13
+ */
14
+ import type { Message, AgentResult, ToolDefinition, Provider, ToolExecutor, ApprovalBus, ErrorClassifier, AgentHooks } from './types.js';
15
+ import type { EventStream } from './event-stream.js';
16
+ export interface AgentLoopConfig {
17
+ sessionId: string;
18
+ model: string;
19
+ messages: Message[];
20
+ tools: ToolDefinition[];
21
+ maxTurns: number;
22
+ provider: Provider;
23
+ toolExecutor: ToolExecutor;
24
+ events: EventStream;
25
+ approvalBus?: ApprovalBus;
26
+ errorClassifier?: ErrorClassifier;
27
+ hooks?: AgentHooks;
28
+ contextTransform?: (messages: Message[]) => Promise<Message[]>;
29
+ toolConcurrency?: 'serial' | 'parallel';
30
+ signal?: AbortSignal;
31
+ }
32
+ export declare function runAgentLoop(cfg: AgentLoopConfig): Promise<AgentResult>;
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Agent loop: ReAct with optional plan-then-execute.
3
+ *
4
+ * The loop is the sole caller of Provider.stream and the sole caller of
5
+ * ToolExecutor.execute. All layering conventions are enforced here:
6
+ *
7
+ * 1. Every tool call passes through the ApprovalBus seam.
8
+ * 2. Every event flows through the EventStream (no side-channels).
9
+ * 3. Errors from providers are classified by the injected ErrorClassifier
10
+ * before any retry decision; there is no string matching.
11
+ * 4. Context transform (compaction, skill injection) is invoked once per
12
+ * turn before the provider call, never mid-stream.
13
+ */
14
+ import { assembleTurn, extractToolUses, appendToolResults } from './message.js';
15
+ export async function runAgentLoop(cfg) {
16
+ const events = cfg.events;
17
+ const history = [...cfg.messages];
18
+ const totals = { inputTokens: 0, outputTokens: 0, cachedTokens: 0, costUsd: 0 };
19
+ let stopReason = 'end_turn';
20
+ let turnCount = 0;
21
+ events.emit({ type: 'agent_start', sessionId: cfg.sessionId, model: cfg.model });
22
+ try {
23
+ for (let turnIndex = 0; turnIndex < cfg.maxTurns; turnIndex++) {
24
+ if (cfg.signal?.aborted) {
25
+ stopReason = 'aborted';
26
+ break;
27
+ }
28
+ if (cfg.hooks?.beforeTurn) {
29
+ const decision = await cfg.hooks.beforeTurn(turnIndex, history);
30
+ if (decision === 'abort') {
31
+ stopReason = 'aborted';
32
+ break;
33
+ }
34
+ }
35
+ turnCount = turnIndex + 1;
36
+ const turnId = `t${turnIndex}-${Date.now().toString(36)}`;
37
+ events.emit({ type: 'turn_start', turnId, turnIndex });
38
+ const messagesForCall = cfg.contextTransform
39
+ ? await cfg.contextTransform(history)
40
+ : history;
41
+ const streamEvents = [];
42
+ try {
43
+ for await (const ev of cfg.provider.stream({
44
+ model: cfg.model,
45
+ messages: messagesForCall,
46
+ tools: cfg.tools,
47
+ signal: cfg.signal,
48
+ })) {
49
+ streamEvents.push(ev);
50
+ events.emit(ev);
51
+ }
52
+ }
53
+ catch (err) {
54
+ const classified = cfg.errorClassifier?.classify(err, cfg.provider.id, cfg.model);
55
+ events.emit({
56
+ type: 'error',
57
+ message: String(err instanceof Error ? err.message : err),
58
+ reason: classified?.reason,
59
+ retryable: classified?.retryable ?? false,
60
+ });
61
+ stopReason = 'error';
62
+ events.emit({ type: 'turn_end', turnId, stopReason });
63
+ break;
64
+ }
65
+ const assembled = assembleTurn(streamEvents);
66
+ totals.inputTokens += assembled.inputTokens;
67
+ totals.outputTokens += assembled.outputTokens;
68
+ totals.cachedTokens += assembled.cachedTokens;
69
+ history.push(assembled.message);
70
+ const toolUses = extractToolUses(assembled.message);
71
+ if (toolUses.length === 0) {
72
+ events.emit({ type: 'turn_end', turnId, stopReason: 'end_turn' });
73
+ await cfg.hooks?.afterTurn?.(turnIndex, totals);
74
+ break;
75
+ }
76
+ const toolResults = await executeToolCalls(toolUses, cfg, events);
77
+ const appended = appendToolResults(history, toolResults.map(r => ({
78
+ toolUseId: r.call.id,
79
+ content: r.result.content,
80
+ isError: r.result.isError,
81
+ })));
82
+ history.length = 0;
83
+ history.push(...appended);
84
+ events.emit({ type: 'turn_end', turnId, stopReason: 'tool_use' });
85
+ await cfg.hooks?.afterTurn?.(turnIndex, totals);
86
+ }
87
+ }
88
+ finally {
89
+ events.emit({ type: 'agent_end', sessionId: cfg.sessionId, stopReason, usage: totals });
90
+ }
91
+ return { messages: history, usage: totals, stopReason, turnCount, sessionId: cfg.sessionId };
92
+ }
93
+ async function executeToolCalls(toolUses, cfg, events) {
94
+ const run = async (call) => {
95
+ let input = call.input;
96
+ if (cfg.hooks?.beforeToolCall) {
97
+ const decision = await cfg.hooks.beforeToolCall(call);
98
+ if (decision?.block) {
99
+ const result = { content: decision.reason, isError: true };
100
+ return { call, result };
101
+ }
102
+ if (decision && !decision.block && decision.replaceInput !== undefined) {
103
+ input = decision.replaceInput;
104
+ }
105
+ }
106
+ if (cfg.approvalBus?.requiresApproval(call.name, input)) {
107
+ const decision = await cfg.approvalBus.request({
108
+ id: call.id,
109
+ tool: call.name,
110
+ summary: `${call.name}: ${truncateForSummary(input)}`,
111
+ });
112
+ if (decision === 'deny' || decision === 'deny_always') {
113
+ return { call, result: { content: `Tool call ${call.name} denied by user.`, isError: true } };
114
+ }
115
+ }
116
+ events.emit({ type: 'tool_exec_start', id: call.id, name: call.name, input });
117
+ const started = Date.now();
118
+ let result;
119
+ try {
120
+ result = await cfg.toolExecutor.execute({ ...call, input }, cfg.signal ?? defaultSignal());
121
+ }
122
+ catch (err) {
123
+ result = { content: `Tool execution failed: ${String(err)}`, isError: true };
124
+ }
125
+ const durationMs = Date.now() - started;
126
+ result = await cfg.hooks?.afterToolCall?.({ ...call, input }, result) ?? result;
127
+ events.emit({
128
+ type: 'tool_exec_end',
129
+ id: call.id,
130
+ output: result.content,
131
+ isError: result.isError,
132
+ durationMs,
133
+ });
134
+ return { call: { ...call, input }, result };
135
+ };
136
+ if (cfg.toolConcurrency === 'parallel' && toolUses.length > 1) {
137
+ return Promise.all(toolUses.map(run));
138
+ }
139
+ const out = [];
140
+ for (const u of toolUses)
141
+ out.push(await run(u));
142
+ return out;
143
+ }
144
+ function truncateForSummary(input, max = 160) {
145
+ const s = typeof input === 'string' ? input : JSON.stringify(input);
146
+ return s.length <= max ? s : s.slice(0, max - 1) + '…';
147
+ }
148
+ function defaultSignal() {
149
+ return new AbortController().signal;
150
+ }
151
+ //# sourceMappingURL=agent-loop.js.map
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Event stream: pub/sub plus async iteration over kernel events.
3
+ *
4
+ * Producers call emit(). Consumers either subscribe for side-effectful
5
+ * handling or iterate via for-await. Multiple consumers are allowed;
6
+ * each independent iterator receives all events from its subscription
7
+ * onward. Back-pressure is bounded by an internal queue.
8
+ */
9
+ import type { AgentEvent } from './types.js';
10
+ export type EventHandler = (event: AgentEvent) => void | Promise<void>;
11
+ export interface EventStream {
12
+ emit(event: AgentEvent): void;
13
+ subscribe(handler: EventHandler): () => void;
14
+ iterator(): AsyncIterableIterator<AgentEvent>;
15
+ close(): void;
16
+ readonly closed: boolean;
17
+ }
18
+ export declare function createEventStream(): EventStream;
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Event stream: pub/sub plus async iteration over kernel events.
3
+ *
4
+ * Producers call emit(). Consumers either subscribe for side-effectful
5
+ * handling or iterate via for-await. Multiple consumers are allowed;
6
+ * each independent iterator receives all events from its subscription
7
+ * onward. Back-pressure is bounded by an internal queue.
8
+ */
9
+ const MAX_QUEUE = 4096;
10
+ class EventStreamImpl {
11
+ subscribers = new Set();
12
+ iterators = new Set();
13
+ closed = false;
14
+ emit(event) {
15
+ if (this.closed)
16
+ return;
17
+ for (const handler of this.subscribers) {
18
+ void Promise.resolve().then(() => handler(event)).catch(err => {
19
+ this.emit({ type: 'error', message: `event handler failed: ${String(err)}` });
20
+ });
21
+ }
22
+ for (const state of this.iterators)
23
+ state.push(event);
24
+ }
25
+ subscribe(handler) {
26
+ this.subscribers.add(handler);
27
+ return () => {
28
+ this.subscribers.delete(handler);
29
+ };
30
+ }
31
+ iterator() {
32
+ const state = new IteratorState();
33
+ this.iterators.add(state);
34
+ const cleanup = () => { this.iterators.delete(state); };
35
+ if (this.closed)
36
+ state.close();
37
+ return makeAsyncIterator(state, cleanup);
38
+ }
39
+ close() {
40
+ if (this.closed)
41
+ return;
42
+ this.closed = true;
43
+ for (const state of this.iterators)
44
+ state.close();
45
+ this.iterators.clear();
46
+ this.subscribers.clear();
47
+ }
48
+ }
49
+ class IteratorState {
50
+ queue = [];
51
+ waiters = [];
52
+ done = false;
53
+ push(event) {
54
+ if (this.done)
55
+ return;
56
+ const waiter = this.waiters.shift();
57
+ if (waiter) {
58
+ waiter.resolve({ value: event, done: false });
59
+ return;
60
+ }
61
+ if (this.queue.length >= MAX_QUEUE)
62
+ this.queue.shift();
63
+ this.queue.push(event);
64
+ }
65
+ close() {
66
+ if (this.done)
67
+ return;
68
+ this.done = true;
69
+ while (this.waiters.length > 0) {
70
+ const w = this.waiters.shift();
71
+ w.resolve({ value: undefined, done: true });
72
+ }
73
+ }
74
+ next() {
75
+ const event = this.queue.shift();
76
+ if (event !== undefined)
77
+ return Promise.resolve({ value: event, done: false });
78
+ if (this.done)
79
+ return Promise.resolve({ value: undefined, done: true });
80
+ return new Promise(resolve => { this.waiters.push({ resolve }); });
81
+ }
82
+ }
83
+ function makeAsyncIterator(state, cleanup) {
84
+ return {
85
+ next: () => state.next(),
86
+ return(value) {
87
+ state.close();
88
+ cleanup();
89
+ return Promise.resolve({ value: value, done: true });
90
+ },
91
+ throw(err) {
92
+ state.close();
93
+ cleanup();
94
+ return Promise.reject(err);
95
+ },
96
+ [Symbol.asyncIterator]() { return this; },
97
+ };
98
+ }
99
+ export function createEventStream() {
100
+ return new EventStreamImpl();
101
+ }
102
+ //# sourceMappingURL=event-stream.js.map
@@ -0,0 +1,4 @@
1
+ export * from './types.js';
2
+ export * from './event-stream.js';
3
+ export * from './message.js';
4
+ export * from './agent-loop.js';
@@ -0,0 +1,5 @@
1
+ export * from './types.js';
2
+ export * from './event-stream.js';
3
+ export * from './message.js';
4
+ export * from './agent-loop.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Message manipulation helpers. Pure functions over Message[].
3
+ */
4
+ import type { Message, ContentPart, ToolUsePart, AgentEvent } from './types.js';
5
+ export declare function normaliseContent(msg: Message): ContentPart[];
6
+ export declare function extractText(msg: Message): string;
7
+ export declare function extractToolUses(msg: Message): ToolUsePart[];
8
+ export declare function toolResultMessage(toolUseId: string, content: string, isError?: boolean): Message;
9
+ export declare function appendToolResults(history: Message[], results: Array<{
10
+ toolUseId: string;
11
+ content: string;
12
+ isError: boolean;
13
+ }>): Message[];
14
+ /**
15
+ * Streaming assembly: fold AgentEvents into a single assistant Message.
16
+ *
17
+ * Accepts the sequence of provider events for one turn and returns the
18
+ * finalised assistant Message plus token-use totals. Partial tool-call
19
+ * JSON deltas are concatenated and parsed at toolcall_end.
20
+ */
21
+ export interface AssembledTurn {
22
+ message: Message;
23
+ inputTokens: number;
24
+ outputTokens: number;
25
+ cachedTokens: number;
26
+ }
27
+ export declare function assembleTurn(events: AgentEvent[]): AssembledTurn;
28
+ /** Rough cl100k heuristic: ~4 characters per token. */
29
+ export declare function estimateTokens(text: string): number;
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Message manipulation helpers. Pure functions over Message[].
3
+ */
4
+ export function normaliseContent(msg) {
5
+ if (typeof msg.content === 'string') {
6
+ return msg.content.length > 0 ? [{ type: 'text', text: msg.content }] : [];
7
+ }
8
+ return msg.content;
9
+ }
10
+ export function extractText(msg) {
11
+ return normaliseContent(msg)
12
+ .filter((p) => p.type === 'text')
13
+ .map(p => p.text)
14
+ .join('');
15
+ }
16
+ export function extractToolUses(msg) {
17
+ return normaliseContent(msg).filter((p) => p.type === 'tool_use');
18
+ }
19
+ export function toolResultMessage(toolUseId, content, isError = false) {
20
+ const part = { type: 'tool_result', toolUseId, content, isError };
21
+ return { role: 'user', content: [part] };
22
+ }
23
+ export function appendToolResults(history, results) {
24
+ if (results.length === 0)
25
+ return history;
26
+ const parts = results.map(r => ({
27
+ type: 'tool_result',
28
+ toolUseId: r.toolUseId,
29
+ content: r.content,
30
+ isError: r.isError,
31
+ }));
32
+ return [...history, { role: 'user', content: parts }];
33
+ }
34
+ export function assembleTurn(events) {
35
+ const parts = [];
36
+ let textBuf = '';
37
+ let thinkingBuf = '';
38
+ const toolJsonBuf = new Map();
39
+ let inputTokens = 0;
40
+ let outputTokens = 0;
41
+ let cachedTokens = 0;
42
+ const flushText = () => {
43
+ if (textBuf.length > 0) {
44
+ parts.push({ type: 'text', text: textBuf });
45
+ textBuf = '';
46
+ }
47
+ };
48
+ const flushThinking = () => {
49
+ if (thinkingBuf.length > 0) {
50
+ parts.push({ type: 'thinking', text: thinkingBuf });
51
+ thinkingBuf = '';
52
+ }
53
+ };
54
+ for (const ev of events) {
55
+ switch (ev.type) {
56
+ case 'text_delta':
57
+ textBuf += ev.delta;
58
+ break;
59
+ case 'text_end':
60
+ flushText();
61
+ break;
62
+ case 'thinking_delta':
63
+ thinkingBuf += ev.delta;
64
+ break;
65
+ case 'thinking_end':
66
+ flushThinking();
67
+ break;
68
+ case 'toolcall_start':
69
+ flushText();
70
+ flushThinking();
71
+ toolJsonBuf.set(ev.id, { name: ev.name, json: '' });
72
+ break;
73
+ case 'toolcall_delta': {
74
+ const entry = toolJsonBuf.get(ev.id);
75
+ if (entry)
76
+ entry.json += ev.deltaJson;
77
+ break;
78
+ }
79
+ case 'toolcall_end': {
80
+ const entry = toolJsonBuf.get(ev.id);
81
+ const input = ev.input ?? (entry ? safeParse(entry.json) : {});
82
+ parts.push({ type: 'tool_use', id: ev.id, name: entry?.name ?? '', input });
83
+ toolJsonBuf.delete(ev.id);
84
+ break;
85
+ }
86
+ case 'usage':
87
+ inputTokens += ev.inputTokens;
88
+ outputTokens += ev.outputTokens;
89
+ cachedTokens += ev.cachedTokens ?? 0;
90
+ break;
91
+ }
92
+ }
93
+ flushText();
94
+ flushThinking();
95
+ return {
96
+ message: { role: 'assistant', content: parts },
97
+ inputTokens,
98
+ outputTokens,
99
+ cachedTokens,
100
+ };
101
+ }
102
+ function safeParse(json) {
103
+ if (!json)
104
+ return {};
105
+ try {
106
+ return JSON.parse(json);
107
+ }
108
+ catch {
109
+ return {};
110
+ }
111
+ }
112
+ /** Rough cl100k heuristic: ~4 characters per token. */
113
+ export function estimateTokens(text) {
114
+ if (!text)
115
+ return 0;
116
+ return Math.ceil(text.length / 4);
117
+ }
118
+ //# sourceMappingURL=message.js.map