tenjin-cli 0.1.0-alpha.3 → 0.1.0-alpha.31

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 (284) hide show
  1. package/NOTICE.md +54 -7
  2. package/README.md +100 -266
  3. package/dist/{_esm-FEASBWRP.js → _esm-MEXZI5DL.js} +9 -10
  4. package/dist/alive-J64H46HI.js +31 -0
  5. package/dist/{ccip-NQXCEK2U.js → ccip-7RMRLAGV.js} +4 -4
  6. package/dist/{ccip-XR5NE5QK.js → ccip-NLCSCHKI.js} +5 -5
  7. package/dist/chunk-27EPWVOY.js +108 -0
  8. package/dist/chunk-27EPWVOY.js.map +1 -0
  9. package/dist/chunk-2SNUOHAT.js +75 -0
  10. package/dist/chunk-2SNUOHAT.js.map +1 -0
  11. package/dist/{chunk-Z6MFJV5Z.js → chunk-2XLC3AEX.js} +857 -504
  12. package/dist/chunk-2XLC3AEX.js.map +1 -0
  13. package/dist/chunk-3623BLPV.js +184 -0
  14. package/dist/chunk-3623BLPV.js.map +1 -0
  15. package/dist/chunk-3UCIG4TG.js +65 -0
  16. package/dist/chunk-3UCIG4TG.js.map +1 -0
  17. package/dist/chunk-4ZZO6ULA.js +151 -0
  18. package/dist/chunk-4ZZO6ULA.js.map +1 -0
  19. package/dist/chunk-5C32RLTA.js +115 -0
  20. package/dist/chunk-5C32RLTA.js.map +1 -0
  21. package/dist/{chunk-ZMHYXMO6.js → chunk-5IEP5EGE.js} +3 -3
  22. package/dist/chunk-5K5W4S66.js +105 -0
  23. package/dist/chunk-5K5W4S66.js.map +1 -0
  24. package/dist/chunk-5MARFNSB.js +2018 -0
  25. package/dist/chunk-5MARFNSB.js.map +1 -0
  26. package/dist/{chunk-CYOF7ROH.js → chunk-6E5R45ZO.js} +3 -3
  27. package/dist/chunk-77TCKDEC.js +1987 -0
  28. package/dist/chunk-77TCKDEC.js.map +1 -0
  29. package/dist/chunk-7EKYXMUO.js +7536 -0
  30. package/dist/chunk-7EKYXMUO.js.map +1 -0
  31. package/dist/chunk-7Q5FDF65.js +159 -0
  32. package/dist/chunk-7Q5FDF65.js.map +1 -0
  33. package/dist/chunk-7QKHZQKW.js +1680 -0
  34. package/dist/chunk-7QKHZQKW.js.map +1 -0
  35. package/dist/{chunk-MUBUQVC3.js → chunk-7WEQQJLG.js} +6 -5
  36. package/dist/{chunk-MUBUQVC3.js.map → chunk-7WEQQJLG.js.map} +1 -1
  37. package/dist/chunk-7WU4OPLP.js +244 -0
  38. package/dist/chunk-7WU4OPLP.js.map +1 -0
  39. package/dist/chunk-7ZRL2LBQ.js +192 -0
  40. package/dist/chunk-7ZRL2LBQ.js.map +1 -0
  41. package/dist/chunk-A36LQPHV.js +29 -0
  42. package/dist/chunk-A36LQPHV.js.map +1 -0
  43. package/dist/chunk-AGZ7PJKY.js +326 -0
  44. package/dist/chunk-AGZ7PJKY.js.map +1 -0
  45. package/dist/chunk-AKWGE6JQ.js +49 -0
  46. package/dist/chunk-AKWGE6JQ.js.map +1 -0
  47. package/dist/chunk-B7HUUJOK.js +29 -0
  48. package/dist/chunk-B7HUUJOK.js.map +1 -0
  49. package/dist/{chunk-S3IGX57D.js → chunk-BL3332HQ.js} +290 -7361
  50. package/dist/chunk-BL3332HQ.js.map +1 -0
  51. package/dist/{chunk-5CLSD5YN.js → chunk-BPMWXTFX.js} +2 -2
  52. package/dist/chunk-D65GNPS3.js +12708 -0
  53. package/dist/chunk-D65GNPS3.js.map +1 -0
  54. package/dist/chunk-DOQGNFHM.js +121 -0
  55. package/dist/chunk-DOQGNFHM.js.map +1 -0
  56. package/dist/{chunk-YALGASEU.js → chunk-EGELTSPU.js} +2 -2
  57. package/dist/chunk-FW75G2W4.js +406 -0
  58. package/dist/chunk-FW75G2W4.js.map +1 -0
  59. package/dist/chunk-GA7JETVM.js +150 -0
  60. package/dist/chunk-GA7JETVM.js.map +1 -0
  61. package/dist/chunk-I3EQFHCD.js +175 -0
  62. package/dist/chunk-I3EQFHCD.js.map +1 -0
  63. package/dist/chunk-JKKAIPTU.js +108 -0
  64. package/dist/chunk-JKKAIPTU.js.map +1 -0
  65. package/dist/{chunk-QVX5MWJZ.js → chunk-JRGFKVAZ.js} +2 -2
  66. package/dist/{chunk-NCE5NSN6.js → chunk-K3I7C2MO.js} +142 -12
  67. package/dist/chunk-K3I7C2MO.js.map +1 -0
  68. package/dist/chunk-K72RURN3.js +239 -0
  69. package/dist/chunk-K72RURN3.js.map +1 -0
  70. package/dist/{chunk-HIVZG55D.js → chunk-KG3D7FIF.js} +2 -2
  71. package/dist/chunk-KOGZDMHO.js +21 -0
  72. package/dist/chunk-KOGZDMHO.js.map +1 -0
  73. package/dist/chunk-KXQ4UIP7.js +228 -0
  74. package/dist/chunk-KXQ4UIP7.js.map +1 -0
  75. package/dist/chunk-KXVP2VKA.js +194 -0
  76. package/dist/chunk-KXVP2VKA.js.map +1 -0
  77. package/dist/chunk-LEFIX6RS.js +100 -0
  78. package/dist/chunk-LEFIX6RS.js.map +1 -0
  79. package/dist/chunk-LHHMV4GU.js +27 -0
  80. package/dist/chunk-LHHMV4GU.js.map +1 -0
  81. package/dist/{chunk-YBMMV4Y4.js → chunk-MEFB5KHA.js} +13 -5
  82. package/dist/chunk-MEFB5KHA.js.map +1 -0
  83. package/dist/chunk-MNXS2U45.js +1768 -0
  84. package/dist/chunk-MNXS2U45.js.map +1 -0
  85. package/dist/{chunk-UGX7YPX2.js → chunk-NEFNKLBE.js} +9 -11
  86. package/dist/{chunk-UGX7YPX2.js.map → chunk-NEFNKLBE.js.map} +1 -1
  87. package/dist/chunk-OHRR5OYM.js +63 -0
  88. package/dist/chunk-OHRR5OYM.js.map +1 -0
  89. package/dist/chunk-ORWL5BSN.js +269 -0
  90. package/dist/chunk-ORWL5BSN.js.map +1 -0
  91. package/dist/chunk-P4NH7OUK.js +398 -0
  92. package/dist/chunk-P4NH7OUK.js.map +1 -0
  93. package/dist/{chunk-ZABPQJ3M.js → chunk-POUWHWID.js} +2 -2
  94. package/dist/chunk-QSCPK4F5.js +145 -0
  95. package/dist/chunk-QSCPK4F5.js.map +1 -0
  96. package/dist/chunk-QXP3Z3JN.js +131 -0
  97. package/dist/chunk-QXP3Z3JN.js.map +1 -0
  98. package/dist/{chunk-XJIW7USF.js → chunk-R5LCN54M.js} +118 -9
  99. package/dist/chunk-R5LCN54M.js.map +1 -0
  100. package/dist/chunk-S4SAZ7OD.js +30 -0
  101. package/dist/chunk-S4SAZ7OD.js.map +1 -0
  102. package/dist/chunk-SJLTEINJ.js +117 -0
  103. package/dist/chunk-SJLTEINJ.js.map +1 -0
  104. package/dist/{chunk-RRN3YTK7.js → chunk-TTNESUPQ.js} +3 -3
  105. package/dist/chunk-UKY4CHNC.js +498 -0
  106. package/dist/chunk-UKY4CHNC.js.map +1 -0
  107. package/dist/chunk-UQUOVWBG.js +114 -0
  108. package/dist/chunk-UQUOVWBG.js.map +1 -0
  109. package/dist/chunk-V6BSF6OC.js +260 -0
  110. package/dist/chunk-V6BSF6OC.js.map +1 -0
  111. package/dist/chunk-WJXLQXJY.js +43 -0
  112. package/dist/chunk-WJXLQXJY.js.map +1 -0
  113. package/dist/chunk-WK7F3B2E.js +114 -0
  114. package/dist/chunk-WK7F3B2E.js.map +1 -0
  115. package/dist/chunk-XKESD27X.js +10 -0
  116. package/dist/chunk-XKESD27X.js.map +1 -0
  117. package/dist/chunk-XQJXNMSB.js +23 -0
  118. package/dist/chunk-XQJXNMSB.js.map +1 -0
  119. package/dist/{chunk-HSDCI7OV.js → chunk-XZKD7DIF.js} +3 -3
  120. package/dist/chunk-Y527D4TV.js +56 -0
  121. package/dist/chunk-Y527D4TV.js.map +1 -0
  122. package/dist/chunk-YFPY4ONH.js +127 -0
  123. package/dist/chunk-YFPY4ONH.js.map +1 -0
  124. package/dist/{chunk-OYMYCW7J.js → chunk-YVFXE7RR.js} +606 -97
  125. package/dist/chunk-YVFXE7RR.js.map +1 -0
  126. package/dist/clack-EZ7TDWQU.js +19 -0
  127. package/dist/claude-cli-2YKABJJZ.js +31 -0
  128. package/dist/{cli-SBXI5AMS.js → cli-D5OE3URZ.js} +458 -218
  129. package/dist/cli-D5OE3URZ.js.map +1 -0
  130. package/dist/codex-event-PPNVRRDH.js +16 -0
  131. package/dist/codex-host-2RJOJW2B.js +28 -0
  132. package/dist/codex-install-UUZ6QMBQ.js +221 -0
  133. package/dist/codex-install-UUZ6QMBQ.js.map +1 -0
  134. package/dist/config-ZD4OB2DO.js +51 -0
  135. package/dist/dist-3LOO6M4U.js +2842 -0
  136. package/dist/dist-3LOO6M4U.js.map +1 -0
  137. package/dist/doctor-KBR34Q4B.js +601 -0
  138. package/dist/doctor-KBR34Q4B.js.map +1 -0
  139. package/dist/env-proxy-XL4P73MZ.js +15 -0
  140. package/dist/{ethersCompat-Df4a6QIZ-3YZIAXDK.js → ethersCompat-Df4a6QIZ-THAVUGIJ.js} +5 -5
  141. package/dist/ethersCompat-Df4a6QIZ-THAVUGIJ.js.map +1 -0
  142. package/dist/fund-5ITKQGYF.js +372 -0
  143. package/dist/fund-5ITKQGYF.js.map +1 -0
  144. package/dist/hook-command-4TYCHHRL.js +39 -0
  145. package/dist/hook-command-4TYCHHRL.js.map +1 -0
  146. package/dist/hosts/claude/.claude-plugin/marketplace.json +19 -0
  147. package/dist/hosts/claude/tenjin/.claude-plugin/plugin.json +15 -0
  148. package/dist/hosts/claude/tenjin/.mcp.json +12 -0
  149. package/dist/hosts/claude/tenjin/hooks/hooks.json +224 -0
  150. package/dist/hosts/claude/tenjin/hooks/register.tsx +589 -0
  151. package/dist/hosts/claude/tenjin/types/index.d.ts +41 -0
  152. package/dist/hosts/codex/.agents/plugins/marketplace.json +20 -0
  153. package/dist/hosts/codex/tenjin/.codex-plugin/plugin.json +16 -0
  154. package/dist/hosts/codex/tenjin/.mcp.json +12 -0
  155. package/dist/hosts/codex/tenjin/hooks/hooks.json +52 -0
  156. package/dist/hosts-FWZOV26H.js +216 -0
  157. package/dist/hosts-FWZOV26H.js.map +1 -0
  158. package/dist/index.js +21 -2
  159. package/dist/index.js.map +1 -1
  160. package/dist/install-XWGE4Q7L.js +77 -0
  161. package/dist/install-XWGE4Q7L.js.map +1 -0
  162. package/dist/install-identity-K7EJIQFP.js +30 -0
  163. package/dist/install-identity-K7EJIQFP.js.map +1 -0
  164. package/dist/install-wallet-XQ6XFZV4.js +117 -0
  165. package/dist/install-wallet-XQ6XFZV4.js.map +1 -0
  166. package/dist/mcp-3LIJSSJB.js +13553 -0
  167. package/dist/mcp-3LIJSSJB.js.map +1 -0
  168. package/dist/package-ZKQFDWLE.js +37 -0
  169. package/dist/package-ZKQFDWLE.js.map +1 -0
  170. package/dist/paths-POPTXJO6.js +61 -0
  171. package/dist/paths-POPTXJO6.js.map +1 -0
  172. package/dist/pay-WHBERZ3W.js +55 -0
  173. package/dist/pay-WHBERZ3W.js.map +1 -0
  174. package/dist/payments-KVICEKD2.js +110 -0
  175. package/dist/payments-KVICEKD2.js.map +1 -0
  176. package/dist/plugin-registration-5TIA564H.js +54 -0
  177. package/dist/plugin-registration-5TIA564H.js.map +1 -0
  178. package/dist/redact-6W23GGJC.js +15 -0
  179. package/dist/redact-6W23GGJC.js.map +1 -0
  180. package/dist/{secp256k1-N3ZHU7LB.js → secp256k1-ZZZT2CAX.js} +3 -3
  181. package/dist/secp256k1-ZZZT2CAX.js.map +1 -0
  182. package/dist/send-7G7Y7DLN.js +228 -0
  183. package/dist/send-7G7Y7DLN.js.map +1 -0
  184. package/dist/status-OF7RH5HM.js +120 -0
  185. package/dist/status-OF7RH5HM.js.map +1 -0
  186. package/dist/status-line-XOUE6MPL.js +90 -0
  187. package/dist/status-line-XOUE6MPL.js.map +1 -0
  188. package/dist/uninstall-JXUAFNDB.js +158 -0
  189. package/dist/uninstall-JXUAFNDB.js.map +1 -0
  190. package/dist/update-ZKUERSO2.js +44 -0
  191. package/dist/update-ZKUERSO2.js.map +1 -0
  192. package/dist/usdc-OGOGEHV2.js +40 -0
  193. package/dist/usdc-OGOGEHV2.js.map +1 -0
  194. package/dist/{viemAdapter-By13KInL-ZZE6UV4Y.js → viemAdapter-By13KInL-S3DA7DFW.js} +5 -5
  195. package/dist/viemAdapter-By13KInL-S3DA7DFW.js.map +1 -0
  196. package/dist/wallet-2HA6W4R4.js +43 -0
  197. package/dist/wallet-2HA6W4R4.js.map +1 -0
  198. package/dist/wallet-kdf-worker.mjs +980 -0
  199. package/docs/agent-permissions.md +196 -0
  200. package/docs/safety-model.md +59 -0
  201. package/docs/troubleshooting.md +13 -0
  202. package/package.json +28 -19
  203. package/LICENSE +0 -21
  204. package/dist/buy-EE6SEXP6.js +0 -1598
  205. package/dist/buy-EE6SEXP6.js.map +0 -1
  206. package/dist/candidate-ERMSYYQZ.js +0 -127
  207. package/dist/candidate-ERMSYYQZ.js.map +0 -1
  208. package/dist/chunk-3OV7E5G5.js +0 -18
  209. package/dist/chunk-3OV7E5G5.js.map +0 -1
  210. package/dist/chunk-4ONXEEQL.js +0 -152
  211. package/dist/chunk-4ONXEEQL.js.map +0 -1
  212. package/dist/chunk-662MSGZB.js +0 -286
  213. package/dist/chunk-662MSGZB.js.map +0 -1
  214. package/dist/chunk-7KUW4MJD.js +0 -59
  215. package/dist/chunk-7KUW4MJD.js.map +0 -1
  216. package/dist/chunk-AWPAWHNX.js +0 -333
  217. package/dist/chunk-AWPAWHNX.js.map +0 -1
  218. package/dist/chunk-DXTQ3CSI.js +0 -171
  219. package/dist/chunk-DXTQ3CSI.js.map +0 -1
  220. package/dist/chunk-ESPNTPQ2.js +0 -207
  221. package/dist/chunk-ESPNTPQ2.js.map +0 -1
  222. package/dist/chunk-FJPLRISB.js +0 -79
  223. package/dist/chunk-FJPLRISB.js.map +0 -1
  224. package/dist/chunk-GO75V3NZ.js +0 -160
  225. package/dist/chunk-GO75V3NZ.js.map +0 -1
  226. package/dist/chunk-JPHG2JW3.js +0 -96
  227. package/dist/chunk-JPHG2JW3.js.map +0 -1
  228. package/dist/chunk-NCE5NSN6.js.map +0 -1
  229. package/dist/chunk-OYMYCW7J.js.map +0 -1
  230. package/dist/chunk-RY4XKNV3.js +0 -140
  231. package/dist/chunk-RY4XKNV3.js.map +0 -1
  232. package/dist/chunk-S3IGX57D.js.map +0 -1
  233. package/dist/chunk-TLCMMYLY.js +0 -126
  234. package/dist/chunk-TLCMMYLY.js.map +0 -1
  235. package/dist/chunk-UO22YNY4.js +0 -69
  236. package/dist/chunk-UO22YNY4.js.map +0 -1
  237. package/dist/chunk-XHC6FCGZ.js +0 -261
  238. package/dist/chunk-XHC6FCGZ.js.map +0 -1
  239. package/dist/chunk-XJIW7USF.js.map +0 -1
  240. package/dist/chunk-YBMMV4Y4.js.map +0 -1
  241. package/dist/chunk-YN5SHIXQ.js +0 -126
  242. package/dist/chunk-YN5SHIXQ.js.map +0 -1
  243. package/dist/chunk-Z52RA3NC.js +0 -137
  244. package/dist/chunk-Z52RA3NC.js.map +0 -1
  245. package/dist/chunk-Z6MFJV5Z.js.map +0 -1
  246. package/dist/cli-SBXI5AMS.js.map +0 -1
  247. package/dist/config-VELDKLHY.js +0 -21
  248. package/dist/doctor-T3KWNNP2.js +0 -20
  249. package/dist/inspect-LMKOXE4Y.js +0 -96
  250. package/dist/inspect-LMKOXE4Y.js.map +0 -1
  251. package/dist/install-LEX3ENQ6.js +0 -387
  252. package/dist/install-LEX3ENQ6.js.map +0 -1
  253. package/dist/lookup-ZPN2Q67N.js +0 -129
  254. package/dist/lookup-ZPN2Q67N.js.map +0 -1
  255. package/dist/outcome-N7TAK6WR.js +0 -66
  256. package/dist/outcome-N7TAK6WR.js.map +0 -1
  257. package/dist/publish-BKBQQAW4.js +0 -1478
  258. package/dist/publish-BKBQQAW4.js.map +0 -1
  259. package/dist/usdc-JIAJW4SV.js +0 -22
  260. package/dist/wallet-CWEMYVIH.js +0 -179
  261. package/dist/wallet-CWEMYVIH.js.map +0 -1
  262. package/dist/wallet-PA54CRBL.js +0 -30
  263. package/skills/tenjin/SKILL.md +0 -199
  264. package/skills/tenjin-publish/SKILL.md +0 -111
  265. package/skills/tenjin-search/SKILL.md +0 -133
  266. /package/dist/{_esm-FEASBWRP.js.map → _esm-MEXZI5DL.js.map} +0 -0
  267. /package/dist/{ccip-NQXCEK2U.js.map → alive-J64H46HI.js.map} +0 -0
  268. /package/dist/{ccip-XR5NE5QK.js.map → ccip-7RMRLAGV.js.map} +0 -0
  269. /package/dist/{config-VELDKLHY.js.map → ccip-NLCSCHKI.js.map} +0 -0
  270. /package/dist/{chunk-ZMHYXMO6.js.map → chunk-5IEP5EGE.js.map} +0 -0
  271. /package/dist/{chunk-CYOF7ROH.js.map → chunk-6E5R45ZO.js.map} +0 -0
  272. /package/dist/{chunk-5CLSD5YN.js.map → chunk-BPMWXTFX.js.map} +0 -0
  273. /package/dist/{chunk-YALGASEU.js.map → chunk-EGELTSPU.js.map} +0 -0
  274. /package/dist/{chunk-QVX5MWJZ.js.map → chunk-JRGFKVAZ.js.map} +0 -0
  275. /package/dist/{chunk-HIVZG55D.js.map → chunk-KG3D7FIF.js.map} +0 -0
  276. /package/dist/{chunk-ZABPQJ3M.js.map → chunk-POUWHWID.js.map} +0 -0
  277. /package/dist/{chunk-RRN3YTK7.js.map → chunk-TTNESUPQ.js.map} +0 -0
  278. /package/dist/{chunk-HSDCI7OV.js.map → chunk-XZKD7DIF.js.map} +0 -0
  279. /package/dist/{doctor-T3KWNNP2.js.map → clack-EZ7TDWQU.js.map} +0 -0
  280. /package/dist/{ethersCompat-Df4a6QIZ-3YZIAXDK.js.map → claude-cli-2YKABJJZ.js.map} +0 -0
  281. /package/dist/{secp256k1-N3ZHU7LB.js.map → codex-event-PPNVRRDH.js.map} +0 -0
  282. /package/dist/{usdc-JIAJW4SV.js.map → codex-host-2RJOJW2B.js.map} +0 -0
  283. /package/dist/{viemAdapter-By13KInL-ZZE6UV4Y.js.map → config-ZD4OB2DO.js.map} +0 -0
  284. /package/dist/{wallet-PA54CRBL.js.map → env-proxy-XL4P73MZ.js.map} +0 -0
@@ -0,0 +1,589 @@
1
+ import type { EngineInterface, Register } from 'claude-code';
2
+
3
+ import type { TenjinHeld, TenjinRow } from '../types';
4
+
5
+ /**
6
+ * THE TENJIN MOD: one line per paid lookup, drawn from documented props only.
7
+ *
8
+ * - A LOOKUP THAT STOOD ON ITS OWN draws as Claude Code draws one MCP call:
9
+ * `⏺ Calling Exa search… · $0.04` with the query under it while it runs,
10
+ * then the dim `Called Exa search · $0.04 (ctrl+o to expand)`. A run mixing
11
+ * it with other calls is unfolded (`ToolGroup.isExpanded`) for the engine to
12
+ * draw, our row among its rows.
13
+ * - A LOOKUP THAT REPLACED A NATIVE CALL draws where that call stood:
14
+ * `⏺ ~~Web Search~~("q") · Tenjin offered Exa search` while held, then in
15
+ * place `⏺ Exa search("q") · $0.007`, the dot carrying the state.
16
+ *
17
+ * An over-limit answer draws nothing, and the `approve` call that runs its
18
+ * quote draws as the lookup's own row. Free router calls and `needs_input`
19
+ * answers draw nothing; a charged failure
20
+ * draws the error dot, the amount and the state, never a success.
21
+ *
22
+ * EXPANDED: wherever the view shows rows in full (ctrl+o, --verbose: the
23
+ * engine's `isExpanded`, or a run it expanded), every call of ours steps aside
24
+ * and the engine draws its own MCP row, input and result, the calls hidden
25
+ * here included. Its title there is the tool's static one. A row of a run
26
+ * this mod unfolded in the normal view stays compact.
27
+ *
28
+ * READ ONLY: the spec file, the choice set and the offer record (`src/router/mod-state.ts`),
29
+ * and the request tool's own envelope. The mod writes nothing under the data
30
+ * dir and decides nothing about routing or payment.
31
+ */
32
+
33
+ const REQUEST_TOOL = 'mcp__plugin_tenjin_router__request';
34
+ /** The `approve` call runs a quoted lookup, and draws as that lookup's own row. */
35
+ const APPROVE_TOOL = 'mcp__plugin_tenjin_router__approve';
36
+ const OURS = new Set([REQUEST_TOOL, APPROVE_TOOL]);
37
+ /** The native tools a hook holds, by the names the engine draws them under. */
38
+ const NATIVE_NAMES: Readonly<Record<string, string>> = {
39
+ WebSearch: 'Web Search',
40
+ WebFetch: 'Fetch',
41
+ };
42
+ const ROWS = { plugin: 'tenjin', key: 'rows' } as const;
43
+ const HELD = { plugin: 'tenjin', key: 'held' } as const;
44
+ const LINKS = { plugin: 'tenjin', key: 'links' } as const;
45
+ /**
46
+ * Calls of a run the ENGINE expanded (ctrl+o, --verbose), where our rows step
47
+ * aside for the engine's own. The module's own memory, not `$.state`: a drawing may
48
+ * not write state, and this is a fact about the drawing alone. A reload
49
+ * forgets it, so a row draws one line until its run is expanded again.
50
+ */
51
+ const expandedByEngine = new Set<string>();
52
+ /** Calls of a run this mod unfolded in the normal view, whose rows the engine
53
+ * marks `isExpanded` though nobody asked to see them in full. */
54
+ const unfoldedByUs = new Set<string>();
55
+ const DOT = '⏺';
56
+ const GUTTER = ' ⎿ ';
57
+ const INDENT = ' ';
58
+ const EXPAND_HINT = '(ctrl+o to expand)';
59
+ const LABEL_CHARS = 32;
60
+ const SHOWN_MAX_CHARS = 80;
61
+ const ENVELOPE = '{"status":"';
62
+ const SECRET_NAME = /(api[_-]?key|token|secret|passw|authorization|cookie|private[_-]?key)/i;
63
+ /** Answers that ran nothing the person paid for: the router's own, or a retry ahead. */
64
+ const HIDDEN = new Set(['spec', 'discovered', 'native', 'browse', 'needs_input', 'needs_approval']);
65
+ /** Endings drawn with no state word. */
66
+ const PLAIN = new Set(['fulfilled', 'running']);
67
+ const ERRORS = new Set(['failed', 'interrupted', 'partial']);
68
+
69
+ type Dollar = EngineInterface;
70
+ type Json = Record<string, unknown>;
71
+ type Shape = {
72
+ state: 'running' | 'done' | 'error';
73
+ name: string;
74
+ argument?: string;
75
+ tail?: string;
76
+ };
77
+ type Elements = ReturnType<Dollar['ui']['resolve']>;
78
+
79
+ export const register: Register = (on) => {
80
+ on('tool.call', { tool: REQUEST_TOOL }, async ($, e, next) => {
81
+ const row = await describeCall($, e);
82
+ const ref = { ...ROWS, id: e.tool_use_id };
83
+ if (row !== null) {
84
+ await $.state.set(ref, row);
85
+ if (row.offerId !== undefined) {
86
+ await $.state.set({ ...LINKS, id: `take:${row.offerId}` }, e.tool_use_id);
87
+ }
88
+ $.ui.invalidate('ui.render');
89
+ }
90
+ const ran = await next(e);
91
+ const envelope = envelopeOf(ran.text);
92
+ const ended = endOf(envelope);
93
+ const label = row?.label ?? ended.label;
94
+ // A job poll is not a lookup of its own (see `rowOf`).
95
+ if (label !== undefined && !isJobPoll(e)) {
96
+ await $.state.set(ref, { ...row, ...ended, label });
97
+ // OVER A LIMIT: the quote the `approve` call will run, for its row's name.
98
+ const quote = asRecord(envelope?.quote)?.id;
99
+ if (typeof quote === 'string')
100
+ await $.state.set({ ...LINKS, id: `quote:${quote}` }, e.tool_use_id);
101
+ $.ui.invalidate('ui.render');
102
+ }
103
+ return ran;
104
+ });
105
+
106
+ // THE APPROVE CALL IS THE LOOKUP: named as the request it quoted, priced at
107
+ // the total the person approved, drawn as that lookup's own row.
108
+ on('tool.call', { tool: APPROVE_TOOL }, async ($, e, next) => {
109
+ const from = typeof e.quote === 'string' ? await link($, `quote:${e.quote}`) : undefined;
110
+ const asked = from === undefined ? undefined : await row($, from);
111
+ const ref = { ...ROWS, id: e.tool_use_id };
112
+ const total = typeof e.total === 'string' && /^\$?\d/.test(e.total) ? e.total : undefined;
113
+ const price = total === undefined ? asked?.price : total.startsWith('$') ? total : `$${total}`;
114
+ const quoted: TenjinRow | undefined =
115
+ asked === undefined
116
+ ? undefined
117
+ : {
118
+ label: asked.label,
119
+ ...(price !== undefined ? { price } : {}),
120
+ ...(asked.argument !== undefined ? { argument: asked.argument } : {}),
121
+ };
122
+ if (quoted !== undefined) {
123
+ await $.state.set(ref, quoted);
124
+ $.ui.invalidate('ui.render');
125
+ }
126
+ const ran = await next(e);
127
+ if (quoted !== undefined) {
128
+ await $.state.set(ref, { ...quoted, ...endOf(envelopeOf(ran.text)), label: quoted.label });
129
+ $.ui.invalidate('ui.render');
130
+ }
131
+ return ran;
132
+ });
133
+
134
+ // A native call the router held: its offer, from the CLI's record. One the
135
+ // router let run, for a held call with the same search to give way to.
136
+ on('tool.call', { tool: ['WebSearch', 'WebFetch'] }, async ($, e, next) => {
137
+ const ran = await next(e);
138
+ const key = nativeKey(e.tool, e);
139
+ const held = await offerRecord($, e.tool_use_id, key);
140
+ if (held !== null) {
141
+ await $.state.set({ ...HELD, id: e.tool_use_id }, held);
142
+ await $.state.set({ ...LINKS, id: `hold:${held.offerId}` }, e.tool_use_id);
143
+ } else if (key !== undefined) {
144
+ await $.state.set({ ...LINKS, id: `ran:${key}` }, e.tool_use_id);
145
+ }
146
+ $.ui.invalidate('ui.render');
147
+ return ran;
148
+ });
149
+
150
+ on('ui.render', { component: 'ToolGroup' }, async ($, e, next) => {
151
+ const { calls, isExpanded } = e.props;
152
+ const ids = calls.flatMap((c) => (c.tool_use_id !== undefined ? [c.tool_use_id] : []));
153
+ // EXPANDED BY THE ENGINE (ctrl+o, --verbose): our rows step aside there.
154
+ if (isExpanded) {
155
+ const fresh = ids.filter((id) => !expandedByEngine.has(id));
156
+ for (const id of fresh) expandedByEngine.add(id);
157
+ for (const id of ids) unfoldedByUs.delete(id);
158
+ if (fresh.length > 0) $.ui.invalidate('ui.render');
159
+ return next(e);
160
+ }
161
+ // Back in the normal view, and unfolded by us: one line per row, nothing beneath.
162
+ if (ids.filter((id) => expandedByEngine.delete(id)).length > 0) $.ui.invalidate('ui.render');
163
+ const unfold = () => {
164
+ for (const id of ids) unfoldedByUs.add(id);
165
+ return next({ ...e, props: { ...e.props, isExpanded: true } });
166
+ };
167
+ const ours = calls.filter((c) => OURS.has(c.tool));
168
+ const others = calls.filter((c) => !OURS.has(c.tool));
169
+ if (ours.length === 0) {
170
+ for (const c of others) {
171
+ if (c.tool in NATIVE_NAMES && c.tool_use_id !== undefined) {
172
+ if ((await heldOf($, c.tool_use_id, nativeKey(c.tool, c.input))) !== null)
173
+ return unfold();
174
+ }
175
+ }
176
+ return next(e);
177
+ }
178
+ if (others.length > 0 || ours.some((c) => c.tool_use_id === undefined)) return unfold();
179
+ const rows = await Promise.all(ours.map((c) => rowOf($, c.tool_use_id as string, c)));
180
+ const ui = $.ui.resolve(e);
181
+ const drawn = rows.filter((row): row is TenjinRow => row !== null);
182
+ if (drawn.length === 0) return <ui.Box display="none" />;
183
+ return <ui.Box flexDirection="column">{drawn.map((row) => callRow(ui, row))}</ui.Box>;
184
+ });
185
+
186
+ on('ui.render', { component: 'ToolUse' }, async ($, e, next) => {
187
+ const { tool, tool_use_id, input, isInterrupted } = e.props;
188
+ if (tool in NATIVE_NAMES && !isInterrupted) {
189
+ const key = nativeKey(tool, input);
190
+ const held = await heldOf($, tool_use_id, key);
191
+ if (held === null) return next(e);
192
+ const ui = $.ui.resolve(e);
193
+ const asked = nativeArgument(input);
194
+ const takerId = await link($, `take:${held.offerId}`);
195
+ const taker = takerId === undefined ? undefined : await row($, takerId);
196
+ if (
197
+ taker !== undefined &&
198
+ taker.status !== 'needs_input' &&
199
+ taker.status !== 'needs_approval'
200
+ ) {
201
+ const shape = { ...shapeOf(taker), name: taker.label };
202
+ return line(ui, asked !== undefined ? { ...shape, argument: asked } : shape);
203
+ }
204
+ // An offer nobody took, and the model ran the same search natively: the
205
+ // engine's row for that call is the line.
206
+ const ran = await link($, `ran:${held.key}`);
207
+ if (ran !== undefined && ran !== tool_use_id) return <ui.Box display="none" />;
208
+ const offered = held.label !== undefined ? { tail: ` · Tenjin offered ${held.label}` } : {};
209
+ const name = NATIVE_NAMES[tool] as string;
210
+ const argued = asked !== undefined ? { argument: asked } : {};
211
+ return line(ui, { state: 'running', name, ...offered, ...argued }, true);
212
+ }
213
+ if (!OURS.has(tool)) return next(e);
214
+ const ui = $.ui.resolve(e);
215
+ // SHOWN IN FULL (ctrl+o, --verbose, a run the engine expanded): every call
216
+ // of ours is the engine's own row, input and result, the hidden ones too.
217
+ // A row of a run this mod unfolded is marked expanded, and stays compact.
218
+ const full =
219
+ expandedByEngine.has(tool_use_id) ||
220
+ (e.props.isExpanded === true && !unfoldedByUs.has(tool_use_id));
221
+ if (full) return next(e);
222
+ const drawn = await rowOf($, tool_use_id, e.props);
223
+ // A charged call this mod cannot name is the engine's row, never hidden.
224
+ if (drawn === null)
225
+ return (await unnamedCharge($, tool_use_id, e.props.output)) ? (
226
+ next(e)
227
+ ) : (
228
+ <ui.Box display="none" />
229
+ );
230
+ return callRow(ui, drawn);
231
+ });
232
+
233
+ // The envelope the model read, and a held call's deny: the row says both.
234
+ on('ui.render', { component: 'ToolResult' }, async ($, e, next) => {
235
+ const { Box } = $.ui.resolve(e);
236
+ if (OURS.has(e.props.tool)) {
237
+ if (e.props.isExpanded === true) return next(e);
238
+ const unnamed = await unnamedCharge($, e.props.tool_use_id, e.props.output);
239
+ return unnamed ? next(e) : <Box display="none" />;
240
+ }
241
+ if (!(e.props.tool in NATIVE_NAMES)) return next(e);
242
+ const held = await heldOf($, e.props.tool_use_id, undefined);
243
+ return held === null ? next(e) : <Box display="none" />;
244
+ });
245
+ };
246
+
247
+ /** A paid call before it runs: `{id, input}` with a kept spec, or `{id,
248
+ * choice, input}` with the choice set kept beside the line's id. Null for the
249
+ * router's own calls. */
250
+ async function describeCall($: Dollar, e: Json): Promise<TenjinRow | null> {
251
+ const id = typeof e.id === 'string' && e.id.length > 0 ? e.id : undefined;
252
+ const input = asRecord(e.input);
253
+ const dataDir = await tenjinDataDir($);
254
+ if (id === undefined || input === undefined || dataDir === null) return null;
255
+ const read = async (key: string) =>
256
+ readJson($, `${dataDir}/progress/specs/${await sha256Hex(key)}.json`);
257
+ const choice = typeof e.choice === 'string' && e.choice.length > 0 ? e.choice : undefined;
258
+ if (choice !== undefined) {
259
+ // A CHOICE runs the picked option under its own id, named as the line
260
+ // named it ("GPT Image 2.5 Sunburst"); `all` runs every option.
261
+ const kept = await readJson($, `${dataDir}/progress/specs/${await sha256Hex(id)}.choices.json`);
262
+ const options = (asRecord(kept?.choices)?.options as unknown[] | undefined) ?? [];
263
+ const picked = options.map(asRecord).filter((o) => o?.key === choice || choice === 'all');
264
+ const specs = await Promise.all(picked.map((o) => read(String(o?.id))));
265
+ const label = choice === 'all' ? `${picked.length} services` : labelOf(picked[0]?.label);
266
+ if (picked.length === 0 || label === undefined) return null;
267
+ return { label, offerId: id, ...priced(specs, 1n), ...shownOf(specs, input, choice) };
268
+ }
269
+ const spec = await read(id);
270
+ // A Bazaar spec carries no label: the provider's own name stands in.
271
+ const label = labelOf(spec?.label) ?? labelOf(spec?.provider);
272
+ if (spec === null || label === undefined) return null;
273
+ const takes = typeof e.count === 'number' && e.count > 1 ? BigInt(e.count) : 1n;
274
+ return { label, offerId: id, ...priced([spec], takes), ...shownOf([spec], input) };
275
+ }
276
+
277
+ /** What the specs cost together, `takes` times each: `$0.04`, or `up to $0.05`. */
278
+ function priced(specs: (Json | null)[], takes: bigint): { price?: string } {
279
+ let total = 0n;
280
+ let varies = false;
281
+ for (const spec of specs) {
282
+ varies ||= spec?.priceVaries === true;
283
+ const each = atomicOf(spec?.priceVaries === true ? spec.maxAmountAtomic : spec?.priceAtomic);
284
+ if (each === undefined) return {};
285
+ total += BigInt(each) * takes;
286
+ }
287
+ return { price: `${varies ? 'up to ' : ''}$${usd(total)}` };
288
+ }
289
+
290
+ /** The one input the CLI marked shown for a single spec; none for `all`. */
291
+ function shownOf(specs: (Json | null)[], input: Json, choice?: string): { argument?: string } {
292
+ const field = specs.length === 1 && choice !== 'all' ? specs[0]?.shown : undefined;
293
+ if (typeof field !== 'string' || SECRET_NAME.test(field)) return {};
294
+ const argument = typeof input[field] === 'string' ? shortKey(input[field]) : undefined;
295
+ return argument !== undefined ? { argument } : {};
296
+ }
297
+
298
+ /** How a call ended, from its envelope: the status, what left the wallet, and
299
+ * what came back. A call with no envelope ended before it ran. */
300
+ function endOf(envelope: Json | undefined): Partial<TenjinRow> {
301
+ if (envelope === undefined) return { status: 'failed' };
302
+ const status = String(envelope.status);
303
+ const paid = paidOf(envelope.cost);
304
+ const steps = Array.isArray(envelope.results) ? envelope.results.map(asRecord) : [];
305
+ const label =
306
+ labelOf(envelope.label) ??
307
+ (steps.length > 0 ? `${steps.length} services` : labelOf(envelope.supplier));
308
+ return {
309
+ status,
310
+ ...(label !== undefined ? { label } : {}),
311
+ ...(paid !== undefined ? { paid } : {}),
312
+ };
313
+ }
314
+
315
+ /** A request call's row, or null where it draws nothing: not started, one of
316
+ * the router's own, sent back for input, free, or drawn on a held call's row. */
317
+ async function rowOf(
318
+ $: Dollar,
319
+ id: string,
320
+ call: { input: unknown; output?: unknown; isInterrupted: boolean },
321
+ ): Promise<TenjinRow | null> {
322
+ const stored = await row($, id);
323
+ // A JOB POLL repeats the result and the cost of the call that started it.
324
+ if (isJobPoll(asRecord(call.input))) return null;
325
+ const envelope = envelopeOf(textOf(call.output));
326
+ const ended =
327
+ envelope !== undefined ? endOf(envelope) : call.isInterrupted ? { status: 'interrupted' } : {};
328
+ const label = stored?.label ?? ended.label;
329
+ if (label === undefined) return null;
330
+ const drawn: TenjinRow = { ...stored, ...ended, label };
331
+ if (drawn.status !== undefined && HIDDEN.has(drawn.status)) return null;
332
+ if (drawn.status !== undefined && PLAIN.has(drawn.status) && drawn.paid === undefined)
333
+ return null;
334
+ const offer = drawn.offerId;
335
+ if (offer !== undefined && (await link($, `hold:${offer}`)) !== undefined) {
336
+ if ((await link($, `take:${offer}`)) === id) return null;
337
+ }
338
+ return drawn;
339
+ }
340
+
341
+ function shapeOf(row: TenjinRow): Shape {
342
+ const state = row.status === undefined ? 'running' : ERRORS.has(row.status) ? 'error' : 'done';
343
+ const word =
344
+ row.status !== undefined && !PLAIN.has(row.status) ? row.status.replace(/_/g, ' ') : undefined;
345
+ const tail = [row.paid ?? row.price, word].filter((part) => part !== undefined);
346
+ return { state, name: row.label, ...(tail.length > 0 ? { tail: ` · ${tail.join(' · ')}` } : {}) };
347
+ }
348
+
349
+ /** A request call's own row: `Calling X… · $p` with its input under it while it
350
+ * runs, then the folded `Called X · $p`; a refusal or a charged failure keeps
351
+ * its dot and its state. */
352
+ function callRow(ui: Elements, row: TenjinRow) {
353
+ const shape = shapeOf(row);
354
+ if (shape.state === 'running') {
355
+ const under = row.argument !== undefined ? [`"${row.argument}"`] : [];
356
+ return block(ui, line(ui, { ...shape, name: `Calling ${row.label}…` }), under);
357
+ }
358
+ // Ended and plain, as Claude Code leaves an ended MCP call wherever it is
359
+ // drawn (its one-call run, or the standalone row that run becomes).
360
+ if (row.status === 'fulfilled') return folded(ui, row);
361
+ return line(ui, row.argument !== undefined ? { ...shape, argument: row.argument } : shape);
362
+ }
363
+
364
+ /** An ended lookup folded as the engine folds an ended MCP call: no dot, all dim. */
365
+ function folded({ Box, Text }: Elements, row: TenjinRow) {
366
+ const hint = ` ${EXPAND_HINT}`;
367
+ return (
368
+ <Box flexDirection="row" marginTop={1}>
369
+ <Box minWidth={2} />
370
+ <Text dimColor>{`Called ${row.label}${shapeOf(row).tail ?? ''}${hint}`}</Text>
371
+ </Box>
372
+ );
373
+ }
374
+
375
+ /** The engine's tool row: the dot, the name bold, the input in parentheses
376
+ * (cut at the width, the tail kept), and a dim tail. */
377
+ function line({ Box, Text }: Elements, shape: Shape, struck = false) {
378
+ const color =
379
+ shape.state === 'running'
380
+ ? { dimColor: true }
381
+ : { color: shape.state === 'error' ? 'error' : 'success' };
382
+ return (
383
+ <Box flexDirection="row" marginTop={1}>
384
+ <Box minWidth={2}>
385
+ <Text {...color}>{DOT}</Text>
386
+ </Box>
387
+ <Box flexShrink={0}>
388
+ <Text bold strikethrough={struck} wrap="truncate-end">
389
+ {shape.name}
390
+ </Text>
391
+ </Box>
392
+ <Box flexWrap="nowrap">
393
+ <Text wrap="truncate-end">{shape.argument !== undefined ? `(${shape.argument})` : ''}</Text>
394
+ <Text dimColor>{shape.tail ?? ''}</Text>
395
+ </Box>
396
+ </Box>
397
+ );
398
+ }
399
+
400
+ function block({ Box, Text }: Elements, head: ReturnType<typeof line>, under: string[]) {
401
+ if (under.length === 0) return head;
402
+ return (
403
+ <Box flexDirection="column">
404
+ {head}
405
+ {under.map((text, at) => (
406
+ <Box flexDirection="row">
407
+ <Text dimColor>{at === 0 ? GUTTER : INDENT}</Text>
408
+ <Text dimColor wrap="truncate-end">
409
+ {text}
410
+ </Text>
411
+ </Box>
412
+ ))}
413
+ </Box>
414
+ );
415
+ }
416
+
417
+ /** The held native call's offer: from this session's state, else the CLI's
418
+ * offer record for it (`progress/<sha256(session)>/offer-<sha256(id)>.json`). */
419
+ async function heldOf($: Dollar, id: string, key: string | undefined): Promise<TenjinHeld | null> {
420
+ const { value } = await $.state.get({ ...HELD, id });
421
+ return value ?? offerRecord($, id, key);
422
+ }
423
+
424
+ async function offerRecord(
425
+ $: Dollar,
426
+ id: string,
427
+ key: string | undefined,
428
+ ): Promise<TenjinHeld | null> {
429
+ const dataDir = await tenjinDataDir($);
430
+ if (dataDir === null) return null;
431
+ const session = await sha256Hex(await $.session.id());
432
+ const record = await readJson(
433
+ $,
434
+ `${dataDir}/progress/${session}/offer-${await sha256Hex(id)}.json`,
435
+ );
436
+ if (record?.version !== 1 || typeof record.offerId !== 'string') return null;
437
+ const label = labelOf(record.label);
438
+ return { offerId: record.offerId, key: key ?? '', ...(label !== undefined ? { label } : {}) };
439
+ }
440
+
441
+ const row = async ($: Dollar, id: string) => (await $.state.get({ ...ROWS, id })).value;
442
+ const link = async ($: Dollar, id: string) => (await $.state.get({ ...LINKS, id })).value;
443
+
444
+ /** A `{job}` poll, as the CLI reads one: a nonempty `job` (`tool.ts`). */
445
+ function isJobPoll(args: Json | undefined): boolean {
446
+ return typeof args?.job === 'string' && args.job.length > 0;
447
+ }
448
+
449
+ /** The native tool and what it was asked, as one key. */
450
+ function nativeKey(tool: string, input: unknown): string | undefined {
451
+ const args = asRecord(input);
452
+ const target = typeof args?.query === 'string' ? args.query : args?.url;
453
+ return typeof target === 'string' ? `${tool}\n${target}` : undefined;
454
+ }
455
+
456
+ /** A native call's input as the engine's row shows it: a quoted query, or the URL. */
457
+ function nativeArgument(input: unknown): string | undefined {
458
+ const args = asRecord(input);
459
+ const query = typeof args?.query === 'string' ? shortKey(args.query) : undefined;
460
+ if (query !== undefined) return `"${query}"`;
461
+ return typeof args?.url === 'string' ? shortKey(args.url) : undefined;
462
+ }
463
+
464
+ function labelOf(value: unknown): string | undefined {
465
+ if (typeof value !== 'string') return undefined;
466
+ // "Reseller (Model)" names the model the person asked for: the trailing
467
+ // group's contents. One still over the cap is cut with an ellipsis, never dropped.
468
+ const text = clean(value, value.length + 1);
469
+ const name = (trailingGroup(text) ?? text).trim();
470
+ const chars = Array.from(name);
471
+ if (chars.length === 0) return undefined;
472
+ return chars.length <= LABEL_CHARS
473
+ ? name
474
+ : `${chars
475
+ .slice(0, LABEL_CHARS - 1)
476
+ .join('')
477
+ .trimEnd()}…`;
478
+ }
479
+
480
+ /** The contents of a name's last balanced " (…)" group, or undefined when it
481
+ * ends in none or its parentheses do not balance ("Foo (beta"). */
482
+ function trailingGroup(text: string): string | undefined {
483
+ if (!text.endsWith(')')) return undefined;
484
+ let depth = 0;
485
+ for (let at = text.length - 1; at >= 0; at -= 1) {
486
+ if (text[at] === ')') depth += 1;
487
+ else if (text[at] === '(' && --depth === 0) {
488
+ return at > 1 && text[at - 1] === ' ' ? text.slice(at + 1, -1) : undefined;
489
+ }
490
+ }
491
+ return undefined;
492
+ }
493
+
494
+ function shortKey(value: string): string | undefined {
495
+ const text = clean(value, SHOWN_MAX_CHARS + 1);
496
+ return text.length > 0 && Array.from(text).length <= SHOWN_MAX_CHARS ? text : undefined;
497
+ }
498
+
499
+ /** Outside text is hostile on a terminal: control and format characters go. */
500
+ function clean(value: string, limit: number): string {
501
+ const text = value
502
+ .replace(/[\p{Cc}\p{Cf}]/gu, ' ')
503
+ .replace(/\s+/g, ' ')
504
+ .trim();
505
+ const chars = Array.from(text);
506
+ return chars.length <= limit ? text : `${chars.slice(0, limit - 1).join('')}…`;
507
+ }
508
+
509
+ /** A call whose result says money left the wallet, and that nothing names. */
510
+ async function unnamedCharge($: Dollar, id: string, output: unknown): Promise<boolean> {
511
+ const envelope = envelopeOf(textOf(output));
512
+ if (paidOf(envelope?.cost) === undefined || (await row($, id)) !== undefined) return false;
513
+ return endOf(envelope).label === undefined;
514
+ }
515
+
516
+ /** What the envelope's `cost` (`provider price 0.04 USD`) says left the wallet. */
517
+ function paidOf(cost: unknown): string | undefined {
518
+ const paid = Array.isArray(cost)
519
+ ? /^provider price (\d+\.?\d*) USD$/.exec(String(cost[0]))
520
+ : null;
521
+ const atomic = paid === null ? 0n : BigInt(Math.round(Number(paid[1]) * 1e6));
522
+ return atomic === 0n ? undefined : `$${usd(atomic)}`;
523
+ }
524
+
525
+ /** The request tool's envelope: the JSON block whose `status` comes first. */
526
+ function envelopeOf(text: string | undefined): Json | undefined {
527
+ if (text === undefined) return undefined;
528
+ for (let at = text.indexOf(ENVELOPE); at !== -1; at = text.indexOf(ENVELOPE, at + 1)) {
529
+ const envelope = asRecord(parseJson(text.slice(at)));
530
+ if (typeof envelope?.status === 'string') return envelope;
531
+ }
532
+ return undefined;
533
+ }
534
+
535
+ /** A stored result's text: a string, or MCP content blocks bare or in `content`. */
536
+ function textOf(output: unknown): string | undefined {
537
+ if (typeof output === 'string') return output;
538
+ const blocks = Array.isArray(output) ? output : asRecord(output)?.content;
539
+ if (!Array.isArray(blocks)) return undefined;
540
+ return blocks
541
+ .map((block) => asRecord(block)?.text)
542
+ .filter((text) => typeof text === 'string')
543
+ .join('');
544
+ }
545
+
546
+ /** USDC has 6 decimals; whole cents keep two. */
547
+ function usd(atomic: bigint): string {
548
+ const fraction = (atomic % 1_000_000n).toString().padStart(6, '0').replace(/0+$/, '');
549
+ return `${atomic / 1_000_000n}.${fraction.padEnd(2, '0')}`;
550
+ }
551
+
552
+ function atomicOf(value: unknown): string | undefined {
553
+ return typeof value === 'string' && /^\d{1,30}$/.test(value) ? value : undefined;
554
+ }
555
+
556
+ async function tenjinDataDir($: Dollar): Promise<string | null> {
557
+ const override = await $.env.get('TENJIN_DATA_DIR');
558
+ if (override !== undefined && override.length > 0) return override;
559
+ const home = (await $.env.get('HOME')) ?? (await $.env.get('USERPROFILE'));
560
+ return home !== undefined && home.length > 0 ? `${home}/.tenjin` : null;
561
+ }
562
+
563
+ async function readJson($: Dollar, path: string): Promise<Json | null> {
564
+ try {
565
+ return asRecord(parseJson(await $.fs.read(path))) ?? null;
566
+ } catch {
567
+ return null;
568
+ }
569
+ }
570
+
571
+ function parseJson(text: unknown): unknown {
572
+ if (typeof text !== 'string') return undefined;
573
+ try {
574
+ return JSON.parse(text) as unknown;
575
+ } catch {
576
+ return undefined;
577
+ }
578
+ }
579
+
580
+ function asRecord(value: unknown): Json | undefined {
581
+ return value !== null && typeof value === 'object' && !Array.isArray(value)
582
+ ? (value as Json)
583
+ : undefined;
584
+ }
585
+
586
+ async function sha256Hex(text: string): Promise<string> {
587
+ const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text));
588
+ return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0')).join('');
589
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * One request call as the mod draws it: what it is, written by the `tool.call`
3
+ * hook before the call runs, and how it ended, read from the request tool's
4
+ * result envelope once it has. Every string is user text: a label, a price, a
5
+ * state and the one input shown, never a description, JSON or a tool id.
6
+ */
7
+ export type TenjinRow = {
8
+ /** The service's `label`, from its spec or the result envelope. */
9
+ label: string;
10
+ /** `$0.007`, `$0.09` for three takes, or `up to $0.05` when it varies. */
11
+ price?: string;
12
+ /** The one input the CLI says identifies the lookup, bounded. */
13
+ argument?: string;
14
+ /** The offer id the call carried, which a held native call names too. */
15
+ offerId?: string;
16
+ /** The envelope's `status`, once the call has ended. */
17
+ status?: string;
18
+ /** What left the wallet, from the envelope's `cost`, once it has ended. */
19
+ paid?: string;
20
+ };
21
+
22
+ /** A native call the router held for an offer, from the CLI's offer record. */
23
+ export type TenjinHeld = {
24
+ offerId: string;
25
+ /** The offer's `label`, when the decision carried one. */
26
+ label?: string;
27
+ /** The native tool and its search or URL, to see the same call run natively. */
28
+ key: string;
29
+ };
30
+
31
+ declare module 'claude-code' {
32
+ interface PluginState {
33
+ tenjin: {
34
+ rows: StateFamily<TenjinRow>;
35
+ held: StateFamily<TenjinHeld>;
36
+ /** `hold:<offer id>` and `take:<offer id>`: the held native call and the
37
+ * request call taking it up; `ran:<key>`: a native call that ran. */
38
+ links: StateFamily<string>;
39
+ };
40
+ }
41
+ }
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "tenjin",
3
+ "interface": {
4
+ "displayName": "Tenjin"
5
+ },
6
+ "plugins": [
7
+ {
8
+ "name": "tenjin",
9
+ "source": {
10
+ "source": "local",
11
+ "path": "./tenjin"
12
+ },
13
+ "policy": {
14
+ "installation": "AVAILABLE",
15
+ "authentication": "ON_INSTALL"
16
+ },
17
+ "category": "Productivity"
18
+ }
19
+ ]
20
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "tenjin",
3
+ "description": "Tenjin picks the right tool when your agent needs one, and one wallet on this machine pays for every call.",
4
+ "author": {
5
+ "name": "Tenjin",
6
+ "url": "https://tenjin.sh"
7
+ },
8
+ "homepage": "https://tenjin.sh/agents",
9
+ "repository": "https://github.com/BackTrackCo/tenjin-agent",
10
+ "interface": {
11
+ "displayName": "Tenjin",
12
+ "developerName": "Tenjin",
13
+ "category": "Productivity"
14
+ },
15
+ "mcpServers": "./.mcp.json"
16
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "mcpServers": {
3
+ "router": {
4
+ "command": "tenjin",
5
+ "args": [
6
+ "mcp",
7
+ "--harness",
8
+ "codex"
9
+ ]
10
+ }
11
+ }
12
+ }