bitbadges 0.34.3 → 0.35.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 (286) hide show
  1. package/README.md +50 -0
  2. package/dist/cjs/builder/agent/BitBadgesBuilderAgent.d.ts +55 -0
  3. package/dist/cjs/builder/agent/BitBadgesBuilderAgent.d.ts.map +1 -0
  4. package/dist/cjs/builder/agent/BitBadgesBuilderAgent.js +511 -0
  5. package/dist/cjs/builder/agent/BitBadgesBuilderAgent.js.map +1 -0
  6. package/dist/cjs/builder/agent/BitBadgesBuilderAgent.spec.d.ts +2 -0
  7. package/dist/cjs/builder/agent/BitBadgesBuilderAgent.spec.d.ts.map +1 -0
  8. package/dist/cjs/builder/agent/BitBadgesBuilderAgent.spec.js +640 -0
  9. package/dist/cjs/builder/agent/BitBadgesBuilderAgent.spec.js.map +1 -0
  10. package/dist/cjs/builder/agent/anthropicClient.d.ts +8 -0
  11. package/dist/cjs/builder/agent/anthropicClient.d.ts.map +1 -0
  12. package/dist/cjs/builder/agent/anthropicClient.js +129 -0
  13. package/dist/cjs/builder/agent/anthropicClient.js.map +1 -0
  14. package/dist/cjs/builder/agent/communitySkills.d.ts +9 -0
  15. package/dist/cjs/builder/agent/communitySkills.d.ts.map +1 -0
  16. package/dist/cjs/builder/agent/communitySkills.js +61 -0
  17. package/dist/cjs/builder/agent/communitySkills.js.map +1 -0
  18. package/dist/cjs/builder/agent/communitySkills.spec.d.ts +2 -0
  19. package/dist/cjs/builder/agent/communitySkills.spec.d.ts.map +1 -0
  20. package/dist/cjs/builder/agent/communitySkills.spec.js +130 -0
  21. package/dist/cjs/builder/agent/communitySkills.spec.js.map +1 -0
  22. package/dist/cjs/builder/agent/errors.d.ts +41 -0
  23. package/dist/cjs/builder/agent/errors.d.ts.map +1 -0
  24. package/dist/cjs/builder/agent/errors.js +72 -0
  25. package/dist/cjs/builder/agent/errors.js.map +1 -0
  26. package/dist/cjs/builder/agent/errors.spec.d.ts +2 -0
  27. package/dist/cjs/builder/agent/errors.spec.d.ts.map +1 -0
  28. package/dist/cjs/builder/agent/errors.spec.js +158 -0
  29. package/dist/cjs/builder/agent/errors.spec.js.map +1 -0
  30. package/dist/cjs/builder/agent/images.d.ts +4 -0
  31. package/dist/cjs/builder/agent/images.d.ts.map +1 -0
  32. package/dist/cjs/builder/agent/images.js +53 -0
  33. package/dist/cjs/builder/agent/images.js.map +1 -0
  34. package/dist/cjs/builder/agent/images.spec.d.ts +2 -0
  35. package/dist/cjs/builder/agent/images.spec.d.ts.map +1 -0
  36. package/dist/cjs/builder/agent/images.spec.js +107 -0
  37. package/dist/cjs/builder/agent/images.spec.js.map +1 -0
  38. package/dist/cjs/builder/agent/index.d.ts +10 -0
  39. package/dist/cjs/builder/agent/index.d.ts.map +1 -0
  40. package/dist/cjs/builder/agent/index.js +28 -0
  41. package/dist/cjs/builder/agent/index.js.map +1 -0
  42. package/dist/cjs/builder/agent/internals.d.ts +10 -0
  43. package/dist/cjs/builder/agent/internals.d.ts.map +1 -0
  44. package/dist/cjs/builder/agent/internals.js +36 -0
  45. package/dist/cjs/builder/agent/internals.js.map +1 -0
  46. package/dist/cjs/builder/agent/loop.d.ts +43 -0
  47. package/dist/cjs/builder/agent/loop.d.ts.map +1 -0
  48. package/dist/cjs/builder/agent/loop.js +265 -0
  49. package/dist/cjs/builder/agent/loop.js.map +1 -0
  50. package/dist/cjs/builder/agent/models.d.ts +8 -0
  51. package/dist/cjs/builder/agent/models.d.ts.map +1 -0
  52. package/dist/cjs/builder/agent/models.js +39 -0
  53. package/dist/cjs/builder/agent/models.js.map +1 -0
  54. package/dist/cjs/builder/agent/models.spec.d.ts +2 -0
  55. package/dist/cjs/builder/agent/models.spec.d.ts.map +1 -0
  56. package/dist/cjs/builder/agent/models.spec.js +73 -0
  57. package/dist/cjs/builder/agent/models.spec.js.map +1 -0
  58. package/dist/cjs/builder/agent/prompt.d.ts +29 -0
  59. package/dist/cjs/builder/agent/prompt.d.ts.map +1 -0
  60. package/dist/cjs/builder/agent/prompt.js +619 -0
  61. package/dist/cjs/builder/agent/prompt.js.map +1 -0
  62. package/dist/cjs/builder/agent/prompt.spec.d.ts +2 -0
  63. package/dist/cjs/builder/agent/prompt.spec.d.ts.map +1 -0
  64. package/dist/cjs/builder/agent/prompt.spec.js +180 -0
  65. package/dist/cjs/builder/agent/prompt.spec.js.map +1 -0
  66. package/dist/cjs/builder/agent/sanitize.d.ts +2 -0
  67. package/dist/cjs/builder/agent/sanitize.d.ts.map +1 -0
  68. package/dist/cjs/builder/agent/sanitize.js +43 -0
  69. package/dist/cjs/builder/agent/sanitize.js.map +1 -0
  70. package/dist/cjs/builder/agent/sessionStore.d.ts +27 -0
  71. package/dist/cjs/builder/agent/sessionStore.d.ts.map +1 -0
  72. package/dist/cjs/builder/agent/sessionStore.js +116 -0
  73. package/dist/cjs/builder/agent/sessionStore.js.map +1 -0
  74. package/dist/cjs/builder/agent/sessionStore.spec.d.ts +2 -0
  75. package/dist/cjs/builder/agent/sessionStore.spec.d.ts.map +1 -0
  76. package/dist/cjs/builder/agent/sessionStore.spec.js +162 -0
  77. package/dist/cjs/builder/agent/sessionStore.spec.js.map +1 -0
  78. package/dist/cjs/builder/agent/simulationErrorPatterns.d.ts +8 -0
  79. package/dist/cjs/builder/agent/simulationErrorPatterns.d.ts.map +1 -0
  80. package/dist/cjs/builder/agent/simulationErrorPatterns.js +63 -0
  81. package/dist/cjs/builder/agent/simulationErrorPatterns.js.map +1 -0
  82. package/dist/cjs/builder/agent/toolAdapter.d.ts +27 -0
  83. package/dist/cjs/builder/agent/toolAdapter.d.ts.map +1 -0
  84. package/dist/cjs/builder/agent/toolAdapter.js +87 -0
  85. package/dist/cjs/builder/agent/toolAdapter.js.map +1 -0
  86. package/dist/cjs/builder/agent/toolAdapter.spec.d.ts +2 -0
  87. package/dist/cjs/builder/agent/toolAdapter.spec.d.ts.map +1 -0
  88. package/dist/cjs/builder/agent/toolAdapter.spec.js +148 -0
  89. package/dist/cjs/builder/agent/toolAdapter.spec.js.map +1 -0
  90. package/dist/cjs/builder/agent/types.d.ts +207 -0
  91. package/dist/cjs/builder/agent/types.d.ts.map +1 -0
  92. package/dist/cjs/builder/agent/types.js +3 -0
  93. package/dist/cjs/builder/agent/types.js.map +1 -0
  94. package/dist/cjs/builder/agent/validation.d.ts +41 -0
  95. package/dist/cjs/builder/agent/validation.d.ts.map +1 -0
  96. package/dist/cjs/builder/agent/validation.js +106 -0
  97. package/dist/cjs/builder/agent/validation.js.map +1 -0
  98. package/dist/cjs/builder/resources/conceptsDocs.js +1 -1
  99. package/dist/cjs/builder/resources/learnings.js +1 -1
  100. package/dist/cjs/builder/resources/learnings.js.map +1 -1
  101. package/dist/cjs/builder/resources/masterPrompt.d.ts.map +1 -1
  102. package/dist/cjs/builder/resources/masterPrompt.js +10 -9
  103. package/dist/cjs/builder/resources/masterPrompt.js.map +1 -1
  104. package/dist/cjs/builder/session/sessionState.d.ts.map +1 -1
  105. package/dist/cjs/builder/session/sessionState.js +13 -1
  106. package/dist/cjs/builder/session/sessionState.js.map +1 -1
  107. package/dist/cjs/builder/tools/session/addApproval.js +2 -2
  108. package/dist/cjs/builder/tools/session/addApproval.js.map +1 -1
  109. package/dist/cjs/builder/tools/session/setInvariants.spec.d.ts +2 -0
  110. package/dist/cjs/builder/tools/session/setInvariants.spec.d.ts.map +1 -0
  111. package/dist/cjs/builder/tools/session/setInvariants.spec.js +70 -0
  112. package/dist/cjs/builder/tools/session/setInvariants.spec.js.map +1 -0
  113. package/dist/cjs/builder/tools/session/setMintEscrowCoins.spec.d.ts +2 -0
  114. package/dist/cjs/builder/tools/session/setMintEscrowCoins.spec.d.ts.map +1 -0
  115. package/dist/cjs/builder/tools/session/setMintEscrowCoins.spec.js +82 -0
  116. package/dist/cjs/builder/tools/session/setMintEscrowCoins.spec.js.map +1 -0
  117. package/dist/cjs/builder/tools/session/setStandards.spec.d.ts +2 -0
  118. package/dist/cjs/builder/tools/session/setStandards.spec.d.ts.map +1 -0
  119. package/dist/cjs/builder/tools/session/setStandards.spec.js +91 -0
  120. package/dist/cjs/builder/tools/session/setStandards.spec.js.map +1 -0
  121. package/dist/cjs/builder/tools/session/setValidTokenIds.spec.d.ts +2 -0
  122. package/dist/cjs/builder/tools/session/setValidTokenIds.spec.d.ts.map +1 -0
  123. package/dist/cjs/builder/tools/session/setValidTokenIds.spec.js +115 -0
  124. package/dist/cjs/builder/tools/session/setValidTokenIds.spec.js.map +1 -0
  125. package/dist/cjs/builder/tools/utilities/convertAddress.d.ts.map +1 -1
  126. package/dist/cjs/builder/tools/utilities/convertAddress.js +14 -41
  127. package/dist/cjs/builder/tools/utilities/convertAddress.js.map +1 -1
  128. package/dist/cjs/builder/tools/utilities/convertAddress.spec.js +21 -22
  129. package/dist/cjs/builder/tools/utilities/convertAddress.spec.js.map +1 -1
  130. package/dist/cjs/builder/tools/utilities/validateAddress.d.ts.map +1 -1
  131. package/dist/cjs/builder/tools/utilities/validateAddress.js +31 -62
  132. package/dist/cjs/builder/tools/utilities/validateAddress.js.map +1 -1
  133. package/dist/cjs/builder/tools/utilities/validateAddress.spec.js +16 -21
  134. package/dist/cjs/builder/tools/utilities/validateAddress.spec.js.map +1 -1
  135. package/dist/cjs/gamm/indexer.d.ts +2 -2
  136. package/dist/cjs/gamm/indexer.d.ts.map +1 -1
  137. package/dist/cjs/proto/anchor/tx_pb.d.ts.map +1 -1
  138. package/dist/cjs/proto/anchor/tx_pb.js.map +1 -1
  139. package/dist/cjs/proto/maps/tx_pb.d.ts.map +1 -1
  140. package/dist/cjs/proto/maps/tx_pb.js.map +1 -1
  141. package/dist/cjs/transactions/messages/bitbadges/maps/interfaces.d.ts.map +1 -1
  142. package/dist/cjs/transactions/messages/bitbadges/maps/interfaces.js.map +1 -1
  143. package/dist/cjs/tsconfig.build.tsbuildinfo +1 -1
  144. package/dist/esm/builder/agent/BitBadgesBuilderAgent.d.ts +55 -0
  145. package/dist/esm/builder/agent/BitBadgesBuilderAgent.d.ts.map +1 -0
  146. package/dist/esm/builder/agent/BitBadgesBuilderAgent.js +512 -0
  147. package/dist/esm/builder/agent/BitBadgesBuilderAgent.js.map +1 -0
  148. package/dist/esm/builder/agent/BitBadgesBuilderAgent.spec.d.ts +2 -0
  149. package/dist/esm/builder/agent/BitBadgesBuilderAgent.spec.d.ts.map +1 -0
  150. package/dist/esm/builder/agent/BitBadgesBuilderAgent.spec.js +605 -0
  151. package/dist/esm/builder/agent/BitBadgesBuilderAgent.spec.js.map +1 -0
  152. package/dist/esm/builder/agent/anthropicClient.d.ts +8 -0
  153. package/dist/esm/builder/agent/anthropicClient.d.ts.map +1 -0
  154. package/dist/esm/builder/agent/anthropicClient.js +92 -0
  155. package/dist/esm/builder/agent/anthropicClient.js.map +1 -0
  156. package/dist/esm/builder/agent/communitySkills.d.ts +9 -0
  157. package/dist/esm/builder/agent/communitySkills.d.ts.map +1 -0
  158. package/dist/esm/builder/agent/communitySkills.js +58 -0
  159. package/dist/esm/builder/agent/communitySkills.js.map +1 -0
  160. package/dist/esm/builder/agent/communitySkills.spec.d.ts +2 -0
  161. package/dist/esm/builder/agent/communitySkills.spec.d.ts.map +1 -0
  162. package/dist/esm/builder/agent/communitySkills.spec.js +128 -0
  163. package/dist/esm/builder/agent/communitySkills.spec.js.map +1 -0
  164. package/dist/esm/builder/agent/errors.d.ts +41 -0
  165. package/dist/esm/builder/agent/errors.d.ts.map +1 -0
  166. package/dist/esm/builder/agent/errors.js +71 -0
  167. package/dist/esm/builder/agent/errors.js.map +1 -0
  168. package/dist/esm/builder/agent/errors.spec.d.ts +2 -0
  169. package/dist/esm/builder/agent/errors.spec.d.ts.map +1 -0
  170. package/dist/esm/builder/agent/errors.spec.js +156 -0
  171. package/dist/esm/builder/agent/errors.spec.js.map +1 -0
  172. package/dist/esm/builder/agent/images.d.ts +4 -0
  173. package/dist/esm/builder/agent/images.d.ts.map +1 -0
  174. package/dist/esm/builder/agent/images.js +49 -0
  175. package/dist/esm/builder/agent/images.js.map +1 -0
  176. package/dist/esm/builder/agent/images.spec.d.ts +2 -0
  177. package/dist/esm/builder/agent/images.spec.d.ts.map +1 -0
  178. package/dist/esm/builder/agent/images.spec.js +105 -0
  179. package/dist/esm/builder/agent/images.spec.js.map +1 -0
  180. package/dist/esm/builder/agent/index.d.ts +10 -0
  181. package/dist/esm/builder/agent/index.d.ts.map +1 -0
  182. package/dist/esm/builder/agent/index.js +8 -0
  183. package/dist/esm/builder/agent/index.js.map +1 -0
  184. package/dist/esm/builder/agent/internals.d.ts +10 -0
  185. package/dist/esm/builder/agent/internals.d.ts.map +1 -0
  186. package/dist/esm/builder/agent/internals.js +8 -0
  187. package/dist/esm/builder/agent/internals.js.map +1 -0
  188. package/dist/esm/builder/agent/loop.d.ts +43 -0
  189. package/dist/esm/builder/agent/loop.d.ts.map +1 -0
  190. package/dist/esm/builder/agent/loop.js +261 -0
  191. package/dist/esm/builder/agent/loop.js.map +1 -0
  192. package/dist/esm/builder/agent/models.d.ts +8 -0
  193. package/dist/esm/builder/agent/models.d.ts.map +1 -0
  194. package/dist/esm/builder/agent/models.js +34 -0
  195. package/dist/esm/builder/agent/models.js.map +1 -0
  196. package/dist/esm/builder/agent/models.spec.d.ts +2 -0
  197. package/dist/esm/builder/agent/models.spec.d.ts.map +1 -0
  198. package/dist/esm/builder/agent/models.spec.js +71 -0
  199. package/dist/esm/builder/agent/models.spec.js.map +1 -0
  200. package/dist/esm/builder/agent/prompt.d.ts +29 -0
  201. package/dist/esm/builder/agent/prompt.d.ts.map +1 -0
  202. package/dist/esm/builder/agent/prompt.js +609 -0
  203. package/dist/esm/builder/agent/prompt.js.map +1 -0
  204. package/dist/esm/builder/agent/prompt.spec.d.ts +2 -0
  205. package/dist/esm/builder/agent/prompt.spec.d.ts.map +1 -0
  206. package/dist/esm/builder/agent/prompt.spec.js +178 -0
  207. package/dist/esm/builder/agent/prompt.spec.js.map +1 -0
  208. package/dist/esm/builder/agent/sanitize.d.ts +2 -0
  209. package/dist/esm/builder/agent/sanitize.d.ts.map +1 -0
  210. package/dist/esm/builder/agent/sanitize.js +40 -0
  211. package/dist/esm/builder/agent/sanitize.js.map +1 -0
  212. package/dist/esm/builder/agent/sessionStore.d.ts +27 -0
  213. package/dist/esm/builder/agent/sessionStore.d.ts.map +1 -0
  214. package/dist/esm/builder/agent/sessionStore.js +77 -0
  215. package/dist/esm/builder/agent/sessionStore.js.map +1 -0
  216. package/dist/esm/builder/agent/sessionStore.spec.d.ts +2 -0
  217. package/dist/esm/builder/agent/sessionStore.spec.d.ts.map +1 -0
  218. package/dist/esm/builder/agent/sessionStore.spec.js +127 -0
  219. package/dist/esm/builder/agent/sessionStore.spec.js.map +1 -0
  220. package/dist/esm/builder/agent/simulationErrorPatterns.d.ts +8 -0
  221. package/dist/esm/builder/agent/simulationErrorPatterns.d.ts.map +1 -0
  222. package/dist/esm/builder/agent/simulationErrorPatterns.js +58 -0
  223. package/dist/esm/builder/agent/simulationErrorPatterns.js.map +1 -0
  224. package/dist/esm/builder/agent/toolAdapter.d.ts +27 -0
  225. package/dist/esm/builder/agent/toolAdapter.d.ts.map +1 -0
  226. package/dist/esm/builder/agent/toolAdapter.js +84 -0
  227. package/dist/esm/builder/agent/toolAdapter.js.map +1 -0
  228. package/dist/esm/builder/agent/toolAdapter.spec.d.ts +2 -0
  229. package/dist/esm/builder/agent/toolAdapter.spec.d.ts.map +1 -0
  230. package/dist/esm/builder/agent/toolAdapter.spec.js +146 -0
  231. package/dist/esm/builder/agent/toolAdapter.spec.js.map +1 -0
  232. package/dist/esm/builder/agent/types.d.ts +207 -0
  233. package/dist/esm/builder/agent/types.d.ts.map +1 -0
  234. package/dist/esm/builder/agent/types.js +2 -0
  235. package/dist/esm/builder/agent/types.js.map +1 -0
  236. package/dist/esm/builder/agent/validation.d.ts +41 -0
  237. package/dist/esm/builder/agent/validation.d.ts.map +1 -0
  238. package/dist/esm/builder/agent/validation.js +103 -0
  239. package/dist/esm/builder/agent/validation.js.map +1 -0
  240. package/dist/esm/builder/resources/conceptsDocs.js +1 -1
  241. package/dist/esm/builder/resources/learnings.js +1 -1
  242. package/dist/esm/builder/resources/learnings.js.map +1 -1
  243. package/dist/esm/builder/resources/masterPrompt.d.ts.map +1 -1
  244. package/dist/esm/builder/resources/masterPrompt.js +10 -9
  245. package/dist/esm/builder/resources/masterPrompt.js.map +1 -1
  246. package/dist/esm/builder/session/sessionState.d.ts.map +1 -1
  247. package/dist/esm/builder/session/sessionState.js +13 -1
  248. package/dist/esm/builder/session/sessionState.js.map +1 -1
  249. package/dist/esm/builder/tools/session/addApproval.js +2 -2
  250. package/dist/esm/builder/tools/session/addApproval.js.map +1 -1
  251. package/dist/esm/builder/tools/session/setInvariants.spec.d.ts +2 -0
  252. package/dist/esm/builder/tools/session/setInvariants.spec.d.ts.map +1 -0
  253. package/dist/esm/builder/tools/session/setInvariants.spec.js +68 -0
  254. package/dist/esm/builder/tools/session/setInvariants.spec.js.map +1 -0
  255. package/dist/esm/builder/tools/session/setMintEscrowCoins.spec.d.ts +2 -0
  256. package/dist/esm/builder/tools/session/setMintEscrowCoins.spec.d.ts.map +1 -0
  257. package/dist/esm/builder/tools/session/setMintEscrowCoins.spec.js +80 -0
  258. package/dist/esm/builder/tools/session/setMintEscrowCoins.spec.js.map +1 -0
  259. package/dist/esm/builder/tools/session/setStandards.spec.d.ts +2 -0
  260. package/dist/esm/builder/tools/session/setStandards.spec.d.ts.map +1 -0
  261. package/dist/esm/builder/tools/session/setStandards.spec.js +89 -0
  262. package/dist/esm/builder/tools/session/setStandards.spec.js.map +1 -0
  263. package/dist/esm/builder/tools/session/setValidTokenIds.spec.d.ts +2 -0
  264. package/dist/esm/builder/tools/session/setValidTokenIds.spec.d.ts.map +1 -0
  265. package/dist/esm/builder/tools/session/setValidTokenIds.spec.js +113 -0
  266. package/dist/esm/builder/tools/session/setValidTokenIds.spec.js.map +1 -0
  267. package/dist/esm/builder/tools/utilities/convertAddress.d.ts.map +1 -1
  268. package/dist/esm/builder/tools/utilities/convertAddress.js +14 -41
  269. package/dist/esm/builder/tools/utilities/convertAddress.js.map +1 -1
  270. package/dist/esm/builder/tools/utilities/convertAddress.spec.js +21 -22
  271. package/dist/esm/builder/tools/utilities/convertAddress.spec.js.map +1 -1
  272. package/dist/esm/builder/tools/utilities/validateAddress.d.ts.map +1 -1
  273. package/dist/esm/builder/tools/utilities/validateAddress.js +31 -62
  274. package/dist/esm/builder/tools/utilities/validateAddress.js.map +1 -1
  275. package/dist/esm/builder/tools/utilities/validateAddress.spec.js +16 -21
  276. package/dist/esm/builder/tools/utilities/validateAddress.spec.js.map +1 -1
  277. package/dist/esm/gamm/indexer.d.ts +2 -2
  278. package/dist/esm/gamm/indexer.d.ts.map +1 -1
  279. package/dist/esm/proto/anchor/tx_pb.d.ts.map +1 -1
  280. package/dist/esm/proto/anchor/tx_pb.js.map +1 -1
  281. package/dist/esm/proto/maps/tx_pb.d.ts.map +1 -1
  282. package/dist/esm/proto/maps/tx_pb.js.map +1 -1
  283. package/dist/esm/transactions/messages/bitbadges/maps/interfaces.d.ts.map +1 -1
  284. package/dist/esm/transactions/messages/bitbadges/maps/interfaces.js.map +1 -1
  285. package/dist/esm/tsconfig-esm.build.tsbuildinfo +1 -1
  286. package/package.json +23 -1
package/README.md CHANGED
@@ -14,6 +14,56 @@ npm install bitbadges
14
14
  bun add bitbadges
15
15
  ```
16
16
 
17
+ ## AI agents — three ways to build
18
+
19
+ Pick the path that fits your use case. All three produce the same on-chain collection transactions.
20
+
21
+ ### 1. Programmatic agent (BYO Anthropic key)
22
+
23
+ Scriptable from Node / TypeScript. You bring your own Anthropic API key; BitBadges never sees it.
24
+
25
+ ```bash
26
+ npm install bitbadges @anthropic-ai/sdk
27
+ export ANTHROPIC_API_KEY=sk-...
28
+ ```
29
+
30
+ ```ts
31
+ import { BitBadgesBuilderAgent } from 'bitbadges/builder/agent';
32
+
33
+ const agent = new BitBadgesBuilderAgent({ anthropicKey: process.env.ANTHROPIC_API_KEY });
34
+ const result = await agent.build('create a subscription token for $10/month');
35
+ console.log(result.transaction);
36
+ ```
37
+
38
+ Supports OAuth tokens (`anthropicAuthToken` or `ANTHROPIC_OAUTH_TOKEN` env), a
39
+ BitBadges API key for query/search tools (`bitbadgesApiKey` or `BITBADGES_API_KEY`
40
+ env), skills filter, system prompt additions, hooks, pluggable session store,
41
+ typed errors, cost reporting, image placeholder substitution, health checks,
42
+ and a `/internals` escape hatch for DIY loops.
43
+
44
+ Examples: `examples/builder-agent/`. Full guide:
45
+ <https://docs.bitbadges.io/for-developers/ai-agents/programmatic-agent>
46
+
47
+ ### 2. MCP builder (Claude Desktop / Cursor / Claude Code)
48
+
49
+ Zero-code path for human-in-the-loop builds. Point your MCP client at the
50
+ bundled `bitbadges-builder` stdio server:
51
+
52
+ ```bash
53
+ claude mcp add bitbadges-builder bitbadges-builder
54
+ ```
55
+
56
+ Full walkthrough, tool reference, and skill instructions:
57
+ <https://docs.bitbadges.io/for-developers/ai-agents/builder-tools>
58
+
59
+ ### 3. Hand-rolled transactions via SDK classes
60
+
61
+ For non-AI workflows, import the message builders directly — see the Quick Start below.
62
+
63
+ The package ships three bins:
64
+ - `bitbadges` / `bitbadges-cli` — interactive CLI helpers
65
+ - `bitbadges-builder` — the stdio MCP server (entry point: `src/builder/index.ts`)
66
+
17
67
  ## Quick Start
18
68
 
19
69
  ### 1. Initialize the API Client
@@ -0,0 +1,55 @@
1
+ import { type SkillInstruction } from '../resources/skillInstructions.js';
2
+ import { type ImageMap } from './images.js';
3
+ import { type ModelInfo } from './models.js';
4
+ import { type AgentToolRegistry } from './toolAdapter.js';
5
+ import type { BitBadgesBuilderAgentOptions, BuildMode, BuildOptions, BuildResult, StructuredError, Warning } from './types.js';
6
+ export declare class BitBadgesBuilderAgent {
7
+ private readonly options;
8
+ private readonly model;
9
+ private readonly registry;
10
+ private readonly sessionStore;
11
+ private readonly hooks;
12
+ private client;
13
+ private clientInitPromise;
14
+ private readonly inFlightControllers;
15
+ constructor(options: BitBadgesBuilderAgentOptions);
16
+ abort(): void;
17
+ get modelInfo(): ModelInfo;
18
+ get tools(): AgentToolRegistry;
19
+ substituteImages<T>(transaction: T, images: ImageMap): T;
20
+ collectImageReferences(transaction: any): string[];
21
+ listSkills(): SkillInstruction[];
22
+ describeSkill(id: string): SkillInstruction | null;
23
+ exportPrompt(prompt: string, options?: BuildOptions): Promise<{
24
+ prompt: string;
25
+ communitySkillsIncluded: string[];
26
+ }>;
27
+ build(prompt: string, options?: BuildOptions): Promise<BuildResult>;
28
+ private runBuild;
29
+ getSystemPrompt(mode?: BuildMode): string;
30
+ healthCheck(): Promise<{
31
+ anthropic: {
32
+ ok: boolean;
33
+ model?: string;
34
+ error?: string;
35
+ };
36
+ bitbadgesApi: {
37
+ ok: boolean;
38
+ configured: boolean;
39
+ error?: string;
40
+ };
41
+ }>;
42
+ validate(transaction: any, options?: {
43
+ creatorAddress?: string;
44
+ existingCollectionId?: string;
45
+ abortSignal?: AbortSignal;
46
+ }): Promise<{
47
+ valid: boolean;
48
+ errors: StructuredError[];
49
+ warnings: Warning[];
50
+ validation: any;
51
+ simulation: any | null;
52
+ audit: any | null;
53
+ }>;
54
+ }
55
+ //# sourceMappingURL=BitBadgesBuilderAgent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BitBadgesBuilderAgent.d.ts","sourceRoot":"","sources":["../../../../src/builder/agent/BitBadgesBuilderAgent.ts"],"names":[],"mappings":"AAkBA,OAAO,EAA2B,KAAK,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AAInG,OAAO,EAA4C,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AACtF,OAAO,EAAgB,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AAG3D,OAAO,EAA2B,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAGnF,OAAO,KAAK,EAEV,4BAA4B,EAC5B,SAAS,EACT,YAAY,EACZ,WAAW,EAEX,eAAe,EAEf,OAAO,EACR,MAAM,YAAY,CAAC;AAYpB,qBAAa,qBAAqB;IAChC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA+B;IACvD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAY;IAClC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoB;IAC7C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAU;IACvC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAa;IACnC,OAAO,CAAC,MAAM,CAAa;IAQ3B,OAAO,CAAC,iBAAiB,CAA6B;IAOtD,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA8B;gBAEtD,OAAO,EAAE,4BAA4B;IA+FjD,KAAK,IAAI,IAAI;IAKb,IAAI,SAAS,IAAI,SAAS,CAEzB;IAGD,IAAI,KAAK,IAAI,iBAAiB,CAE7B;IAGD,gBAAgB,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,EAAE,QAAQ,GAAG,CAAC;IAKxD,sBAAsB,CAAC,WAAW,EAAE,GAAG,GAAG,MAAM,EAAE;IAWlD,UAAU,IAAI,gBAAgB,EAAE;IAWhC,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI;IAiB5C,YAAY,CAChB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,YAAY,GACrB,OAAO,CAAC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,uBAAuB,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IAoD3D,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC;YA4C3D,QAAQ;IAoWtB,eAAe,CAAC,IAAI,GAAE,SAAoB,GAAG,MAAM;IAa7C,WAAW,IAAI,OAAO,CAAC;QAC3B,SAAS,EAAE;YAAE,EAAE,EAAE,OAAO,CAAC;YAAC,KAAK,CAAC,EAAE,MAAM,CAAC;YAAC,KAAK,CAAC,EAAE,MAAM,CAAA;SAAE,CAAC;QAC3D,YAAY,EAAE;YAAE,EAAE,EAAE,OAAO,CAAC;YAAC,UAAU,EAAE,OAAO,CAAC;YAAC,KAAK,CAAC,EAAE,MAAM,CAAA;SAAE,CAAC;KACpE,CAAC;IAuDI,QAAQ,CACZ,WAAW,EAAE,GAAG,EAChB,OAAO,CAAC,EAAE;QAAE,cAAc,CAAC,EAAE,MAAM,CAAC;QAAC,oBAAoB,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,WAAW,CAAA;KAAE,GAC9F,OAAO,CAAC;QACT,KAAK,EAAE,OAAO,CAAC;QACf,MAAM,EAAE,eAAe,EAAE,CAAC;QAC1B,QAAQ,EAAE,OAAO,EAAE,CAAC;QACpB,UAAU,EAAE,GAAG,CAAC;QAChB,UAAU,EAAE,GAAG,GAAG,IAAI,CAAC;QACvB,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC;KACnB,CAAC;CAqBH"}
@@ -0,0 +1,511 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BitBadgesBuilderAgent = void 0;
4
+ const crypto_1 = require("crypto");
5
+ const index_js_1 = require("../tools/index.js");
6
+ const sessionState_js_1 = require("../session/sessionState.js");
7
+ const skillInstructions_js_1 = require("../resources/skillInstructions.js");
8
+ const anthropicClient_js_1 = require("./anthropicClient.js");
9
+ const errors_js_1 = require("./errors.js");
10
+ const sanitize_js_1 = require("./sanitize.js");
11
+ const images_js_1 = require("./images.js");
12
+ const models_js_1 = require("./models.js");
13
+ const prompt_js_1 = require("./prompt.js");
14
+ const sessionStore_js_1 = require("./sessionStore.js");
15
+ const toolAdapter_js_1 = require("./toolAdapter.js");
16
+ const loop_js_1 = require("./loop.js");
17
+ const validation_js_1 = require("./validation.js");
18
+ const DEFAULTS = {
19
+ model: 'sonnet',
20
+ validation: 'strict',
21
+ maxRounds: 8,
22
+ fixLoopMaxRounds: 3,
23
+ maxTokensPerBuild: 1500000,
24
+ anthropicTimeoutMs: 120000,
25
+ sessionTtlSeconds: 2 * 60 * 60
26
+ };
27
+ class BitBadgesBuilderAgent {
28
+ constructor(options) {
29
+ this.client = null;
30
+ this.clientInitPromise = null;
31
+ this.inFlightControllers = new Set();
32
+ if (!options) {
33
+ throw new errors_js_1.BitBadgesBuilderAgentError('BitBadgesBuilderAgent requires options', 'INVALID_OPTIONS');
34
+ }
35
+ const envAnthropicKey = typeof process !== 'undefined' ? process.env.ANTHROPIC_API_KEY : undefined;
36
+ const envAnthropicAuth = typeof process !== 'undefined' ? process.env.ANTHROPIC_AUTH_TOKEN || process.env.ANTHROPIC_OAUTH_TOKEN : undefined;
37
+ const hasAnthropicCreds = !!options.anthropicClient ||
38
+ !!options.anthropicKey ||
39
+ !!options.anthropicAuthToken ||
40
+ !!envAnthropicKey ||
41
+ !!envAnthropicAuth;
42
+ if (!hasAnthropicCreds) {
43
+ throw new errors_js_1.BitBadgesBuilderAgentError('BitBadgesBuilderAgent requires Anthropic credentials. Provide one of: `anthropicKey` (API key), `anthropicAuthToken` (OAuth), `anthropicClient` (pre-built), or set ANTHROPIC_API_KEY / ANTHROPIC_OAUTH_TOKEN in the environment. BitBadges never stores API keys.', 'MISSING_ANTHROPIC_CREDS');
44
+ }
45
+ if (options.skills && options.skills.length > 0) {
46
+ const valid = new Set((0, skillInstructions_js_1.getAllSkillInstructions)().map((s) => s.id));
47
+ const bad = options.skills.filter((s) => !valid.has(s));
48
+ if (bad.length > 0) {
49
+ throw new errors_js_1.BitBadgesBuilderAgentError(`Unknown skills: ${bad.join(', ')}. Valid skills: ${[...valid].sort().join(', ')}`, 'INVALID_SKILLS');
50
+ }
51
+ }
52
+ if (options.systemPromptAppend && (0, sanitize_js_1.containsInjection)(options.systemPromptAppend)) {
53
+ throw new errors_js_1.BitBadgesBuilderAgentError('`systemPromptAppend` contains prompt-injection patterns. Sanitize end-user input before passing.', 'INVALID_SYSTEM_PROMPT_APPEND');
54
+ }
55
+ if (options.systemPrompt && (0, sanitize_js_1.containsInjection)(options.systemPrompt)) {
56
+ throw new errors_js_1.BitBadgesBuilderAgentError('`systemPrompt` (full replace) contains prompt-injection patterns. The full-replace option bypasses base-prompt protections; sanitize first.', 'INVALID_SYSTEM_PROMPT');
57
+ }
58
+ const envBbKey = typeof process !== 'undefined' ? process.env.BITBADGES_API_KEY : undefined;
59
+ const envBbUrl = typeof process !== 'undefined' ? process.env.BITBADGES_API_URL : undefined;
60
+ const resolvedBitbadgesApiKey = options.bitbadgesApiKey ?? envBbKey;
61
+ const resolvedBitbadgesApiUrl = options.bitbadgesApiUrl ?? envBbUrl;
62
+ const defaultArgs = {};
63
+ if (resolvedBitbadgesApiKey)
64
+ defaultArgs.apiKey = resolvedBitbadgesApiKey;
65
+ if (resolvedBitbadgesApiUrl)
66
+ defaultArgs.apiUrl = resolvedBitbadgesApiUrl;
67
+ this.options = options;
68
+ this.model = (0, models_js_1.resolveModel)(options.model ?? DEFAULTS.model);
69
+ this.registry = (0, toolAdapter_js_1.createAgentToolRegistry)({
70
+ remove: options.tools?.remove,
71
+ add: options.tools?.add,
72
+ defaultArgs
73
+ });
74
+ this.sessionStore = options.sessionStore ?? new sessionStore_js_1.MemoryStore();
75
+ this.hooks = options.hooks ?? {};
76
+ if (!resolvedBitbadgesApiKey && options.skills && options.skills.length > 0) {
77
+ const allSkills = (0, skillInstructions_js_1.getAllSkillInstructions)();
78
+ const skillsWithRefs = allSkills.filter((s) => options.skills.includes(s.id) && (s.referenceCollectionIds?.length ?? 0) > 0);
79
+ if (skillsWithRefs.length > 0 && options.debug) {
80
+ console.warn(`[BitBadgesBuilderAgent] Skills ${skillsWithRefs.map((s) => s.id).join(', ')} reference on-chain collections ` +
81
+ `but no bitbadgesApiKey is configured. query_collection calls will fail mid-loop. ` +
82
+ `Set bitbadgesApiKey or BITBADGES_API_KEY env to enable.`);
83
+ }
84
+ }
85
+ }
86
+ abort() {
87
+ for (const c of this.inFlightControllers)
88
+ c.abort();
89
+ }
90
+ get modelInfo() {
91
+ return this.model;
92
+ }
93
+ get tools() {
94
+ return this.registry;
95
+ }
96
+ substituteImages(transaction, images) {
97
+ return (0, images_js_1.substituteImages)(transaction, images);
98
+ }
99
+ collectImageReferences(transaction) {
100
+ return (0, images_js_1.collectImageReferences)(transaction);
101
+ }
102
+ listSkills() {
103
+ const all = (0, skillInstructions_js_1.getAllSkillInstructions)();
104
+ if (!this.options.skills || this.options.skills.length === 0)
105
+ return all;
106
+ const allowed = new Set(this.options.skills);
107
+ return all.filter((s) => allowed.has(s.id));
108
+ }
109
+ describeSkill(id) {
110
+ if (this.options.skills && this.options.skills.length > 0) {
111
+ if (!this.options.skills.includes(id))
112
+ return null;
113
+ }
114
+ return (0, skillInstructions_js_1.getAllSkillInstructions)().find((s) => s.id === id) ?? null;
115
+ }
116
+ async exportPrompt(prompt, options) {
117
+ if (!prompt || typeof prompt !== 'string') {
118
+ throw new errors_js_1.BitBadgesBuilderAgentError('`prompt` is required and must be a string', 'INVALID_PROMPT');
119
+ }
120
+ const creatorAddress = options?.creatorAddress ?? this.options.defaultCreatorAddress ?? 'bb1examplecreator000000000000000000000000';
121
+ const mode = options?.mode ?? this.options.defaultMode ?? 'create';
122
+ const effectiveSkills = this.options.skills
123
+ ? (options?.selectedSkills ?? []).filter((s) => this.options.skills.includes(s))
124
+ : (options?.selectedSkills ?? []);
125
+ return (0, prompt_js_1.assembleExportPrompt)({
126
+ prompt,
127
+ creatorAddress,
128
+ selectedSkills: effectiveSkills,
129
+ promptSkillIds: options?.promptSkillIds ?? [],
130
+ contextHelpers: options?.contextHelpers,
131
+ metadata: options?.metadata,
132
+ availableImagePlaceholders: options?.availableImagePlaceholders,
133
+ isRefinement: mode === 'refine',
134
+ isUpdate: mode === 'update',
135
+ existingCollectionId: options?.existingCollectionId,
136
+ sessionId: options?.sessionId,
137
+ originalPrompt: options?.originalPrompt,
138
+ priorRefinePrompts: options?.priorRefinePrompts,
139
+ diffLog: options?.diffLog
140
+ }, {
141
+ communitySkillsFetcher: this.options.communitySkillsFetcher,
142
+ systemPromptAppend: this.options.systemPromptAppend
143
+ });
144
+ }
145
+ async build(prompt, options) {
146
+ if (!prompt || typeof prompt !== 'string') {
147
+ throw new errors_js_1.BitBadgesBuilderAgentError('`prompt` is required and must be a string', 'INVALID_PROMPT');
148
+ }
149
+ const creatorAddress = options?.creatorAddress ?? this.options.defaultCreatorAddress ?? 'bb1auto-creator';
150
+ const mode = options?.mode ?? this.options.defaultMode ?? 'create';
151
+ const sessionId = options?.sessionId ?? `agent-${(0, crypto_1.randomUUID)()}`;
152
+ (0, sessionState_js_1.getOrCreateSession)(sessionId, creatorAddress);
153
+ const abortController = new AbortController();
154
+ this.inFlightControllers.add(abortController);
155
+ const signal = abortController.signal;
156
+ const callerSignal = options?.abortSignal;
157
+ if (callerSignal) {
158
+ if (callerSignal.aborted)
159
+ abortController.abort();
160
+ else
161
+ callerSignal.addEventListener('abort', () => abortController.abort(), { once: true });
162
+ }
163
+ try {
164
+ return await this.runBuild(prompt, options, sessionId, mode, creatorAddress, signal);
165
+ }
166
+ finally {
167
+ this.inFlightControllers.delete(abortController);
168
+ }
169
+ }
170
+ async runBuild(prompt, options, sessionId, mode, creatorAddress, signal) {
171
+ if (!this.client) {
172
+ if (!this.clientInitPromise) {
173
+ this.clientInitPromise = (0, anthropicClient_js_1.getAnthropicClient)({
174
+ client: this.options.anthropicClient,
175
+ apiKey: this.options.anthropicKey,
176
+ authToken: this.options.anthropicAuthToken,
177
+ baseURL: this.options.anthropicBaseUrl
178
+ }).then((c) => {
179
+ this.client = c;
180
+ return c;
181
+ }, (err) => {
182
+ this.clientInitPromise = null;
183
+ throw err;
184
+ });
185
+ }
186
+ await this.clientInitPromise;
187
+ }
188
+ const hooks = { ...this.hooks, ...(options?.hooks ?? {}) };
189
+ const debug = !!this.options.debug;
190
+ const requestedSkills = options?.selectedSkills ?? [];
191
+ const effectiveSkills = this.options.skills
192
+ ? requestedSkills.filter((s) => this.options.skills.includes(s))
193
+ : requestedSkills;
194
+ if (debug && requestedSkills.length > 0) {
195
+ const validIds = new Set((0, skillInstructions_js_1.getAllSkillInstructions)().map((s) => s.id));
196
+ const unknown = requestedSkills.filter((s) => !validIds.has(s));
197
+ if (unknown.length > 0) {
198
+ console.warn(`[BitBadgesBuilderAgent] Unknown selectedSkills dropped: ${unknown.join(', ')}. ` +
199
+ `Valid skills: ${[...validIds].sort().join(', ')}`);
200
+ }
201
+ }
202
+ let existingMessages;
203
+ try {
204
+ const raw = await this.sessionStore.get(sessionId);
205
+ if (raw) {
206
+ const parsed = JSON.parse(raw);
207
+ if (Array.isArray(parsed?.messages))
208
+ existingMessages = parsed.messages;
209
+ }
210
+ }
211
+ catch {
212
+ }
213
+ const promptParts = await (0, prompt_js_1.assemblePromptParts)({
214
+ prompt,
215
+ creatorAddress,
216
+ selectedSkills: effectiveSkills,
217
+ promptSkillIds: options?.promptSkillIds ?? [],
218
+ contextHelpers: options?.contextHelpers,
219
+ metadata: options?.metadata,
220
+ availableImagePlaceholders: options?.availableImagePlaceholders,
221
+ isRefinement: mode === 'refine',
222
+ isUpdate: mode === 'update',
223
+ existingCollectionId: options?.existingCollectionId,
224
+ sessionId,
225
+ originalPrompt: options?.originalPrompt,
226
+ priorRefinePrompts: options?.priorRefinePrompts,
227
+ diffLog: options?.diffLog
228
+ }, {
229
+ communitySkillsFetcher: this.options.communitySkillsFetcher,
230
+ systemPromptOverride: this.options.systemPrompt,
231
+ systemPromptAppend: this.options.systemPromptAppend
232
+ });
233
+ const systemPromptHash = (0, prompt_js_1.getSystemPromptHash)(promptParts.systemPrompt);
234
+ let accRounds = 0;
235
+ let accFixRounds = 0;
236
+ let accTotalTokens = 0;
237
+ let accTotalCostUsd = 0;
238
+ let accCacheCreationTokens = 0;
239
+ let accCacheReadTokens = 0;
240
+ let accMessages = [];
241
+ let accToolCalls = [];
242
+ let onCompletionFired = false;
243
+ const fireOnCompletion = () => {
244
+ if (onCompletionFired)
245
+ return;
246
+ onCompletionFired = true;
247
+ if (!hooks.onCompletion)
248
+ return;
249
+ try {
250
+ const r = hooks.onCompletion({
251
+ systemPrompt: promptParts.systemPrompt,
252
+ userMessage: promptParts.userMessage,
253
+ messages: accMessages,
254
+ toolCalls: accToolCalls,
255
+ rounds: accRounds,
256
+ fixRounds: accFixRounds,
257
+ tokensUsed: accTotalTokens,
258
+ cacheCreationTokens: accCacheCreationTokens,
259
+ cacheReadTokens: accCacheReadTokens,
260
+ costUsd: accTotalCostUsd,
261
+ model: this.model.id,
262
+ systemPromptHash
263
+ });
264
+ if (r && typeof r.catch === 'function')
265
+ r.catch(() => { });
266
+ }
267
+ catch {
268
+ }
269
+ };
270
+ try {
271
+ let loopResult = await (0, loop_js_1.runAgentLoop)({
272
+ client: this.client,
273
+ systemPrompt: promptParts.systemPrompt,
274
+ userMessage: promptParts.userMessage,
275
+ userContent: promptParts.userContent,
276
+ registry: this.registry,
277
+ sessionId,
278
+ creatorAddress,
279
+ model: this.model,
280
+ maxRounds: this.options.maxRounds ?? DEFAULTS.maxRounds,
281
+ maxTokensPerBuild: this.options.maxTokensPerBuild ?? DEFAULTS.maxTokensPerBuild,
282
+ anthropicTimeoutMs: this.options.anthropicTimeoutMs ?? DEFAULTS.anthropicTimeoutMs,
283
+ existingMessages,
284
+ abortSignal: signal,
285
+ hooks,
286
+ debug
287
+ });
288
+ let rounds = loopResult.rounds;
289
+ let fixRounds = 0;
290
+ let totalTokens = loopResult.totalTokens;
291
+ let totalCostUsd = loopResult.totalCostUsd;
292
+ let cacheCreationTokens = loopResult.cacheCreationTokens;
293
+ let cacheReadTokens = loopResult.cacheReadTokens;
294
+ accRounds = rounds;
295
+ accTotalTokens = totalTokens;
296
+ accTotalCostUsd = totalCostUsd;
297
+ accCacheCreationTokens = cacheCreationTokens;
298
+ accCacheReadTokens = cacheReadTokens;
299
+ accMessages = loopResult.messages;
300
+ accToolCalls = loopResult.toolCalls;
301
+ const txResponse = await (0, index_js_1.handleGetTransaction)({ sessionId });
302
+ let transaction = txResponse?.transaction ?? txResponse;
303
+ if (!transaction || typeof transaction !== 'object') {
304
+ transaction = { messages: [] };
305
+ }
306
+ const strictness = this.options.validation ?? DEFAULTS.validation;
307
+ let gate = null;
308
+ if (strictness !== 'off') {
309
+ gate = await (0, validation_js_1.runValidationGate)({
310
+ transaction,
311
+ creatorAddress,
312
+ abortSignal: signal,
313
+ simulate: this.options.simulate,
314
+ onLog: hooks.onLog,
315
+ onChainSnapshot: options?.existingCollectionId && this.options.onChainSnapshotFetcher
316
+ ? await this.options.onChainSnapshotFetcher(options.existingCollectionId).catch(() => null)
317
+ : undefined
318
+ });
319
+ const fixLoopMax = this.options.fixLoopMaxRounds ?? DEFAULTS.fixLoopMaxRounds;
320
+ while (gate && !gate.valid && fixRounds < fixLoopMax) {
321
+ fixRounds++;
322
+ if (signal.aborted)
323
+ throw new errors_js_1.AbortedError(totalTokens);
324
+ const fixUserMsg = (0, prompt_js_1.buildFixPrompt)(gate.hardErrors, gate.advisoryNotes, fixRounds, fixLoopMax);
325
+ loopResult = await (0, loop_js_1.runAgentLoop)({
326
+ client: this.client,
327
+ systemPrompt: promptParts.systemPrompt,
328
+ userMessage: fixUserMsg,
329
+ registry: this.registry,
330
+ sessionId,
331
+ creatorAddress,
332
+ model: this.model,
333
+ maxRounds: Math.max(2, Math.floor((this.options.maxRounds ?? DEFAULTS.maxRounds) / 2)),
334
+ maxTokensPerBuild: this.options.maxTokensPerBuild ?? DEFAULTS.maxTokensPerBuild,
335
+ anthropicTimeoutMs: this.options.anthropicTimeoutMs ?? DEFAULTS.anthropicTimeoutMs,
336
+ existingMessages: loopResult.messages,
337
+ abortSignal: signal,
338
+ hooks,
339
+ debug,
340
+ startingTokens: totalTokens,
341
+ startingCostUsd: totalCostUsd,
342
+ startingCacheCreationTokens: cacheCreationTokens,
343
+ startingCacheReadTokens: cacheReadTokens
344
+ });
345
+ rounds += loopResult.rounds;
346
+ totalTokens = loopResult.totalTokens;
347
+ totalCostUsd = loopResult.totalCostUsd;
348
+ cacheCreationTokens = loopResult.cacheCreationTokens;
349
+ cacheReadTokens = loopResult.cacheReadTokens;
350
+ accRounds = rounds;
351
+ accFixRounds = fixRounds;
352
+ accTotalTokens = totalTokens;
353
+ accTotalCostUsd = totalCostUsd;
354
+ accCacheCreationTokens = cacheCreationTokens;
355
+ accCacheReadTokens = cacheReadTokens;
356
+ accMessages = loopResult.messages;
357
+ accToolCalls = [...accToolCalls, ...loopResult.toolCalls];
358
+ const freshTxResp = await (0, index_js_1.handleGetTransaction)({ sessionId });
359
+ transaction = freshTxResp?.transaction ?? freshTxResp;
360
+ gate = await (0, validation_js_1.runValidationGate)({
361
+ transaction,
362
+ creatorAddress,
363
+ abortSignal: signal,
364
+ simulate: this.options.simulate,
365
+ onLog: hooks.onLog
366
+ });
367
+ }
368
+ if (!gate.valid && strictness === 'strict') {
369
+ const errors = structureErrors(gate);
370
+ throw new errors_js_1.ValidationFailedError(errors, transaction, [...gate.advisoryNotes]);
371
+ }
372
+ }
373
+ try {
374
+ await this.sessionStore.set(sessionId, JSON.stringify({ messages: loopResult.messages, transaction, tokensUsed: totalTokens }), { ttlSeconds: this.options.sessionTtlSeconds ?? DEFAULTS.sessionTtlSeconds });
375
+ }
376
+ catch {
377
+ }
378
+ const trace = {
379
+ systemPrompt: promptParts.systemPrompt,
380
+ userMessage: promptParts.userMessage,
381
+ messages: loopResult.messages,
382
+ toolCalls: loopResult.toolCalls,
383
+ rounds,
384
+ fixRounds,
385
+ tokensUsed: totalTokens,
386
+ cacheCreationTokens,
387
+ cacheReadTokens,
388
+ costUsd: totalCostUsd,
389
+ model: this.model.id,
390
+ systemPromptHash
391
+ };
392
+ accRounds = rounds;
393
+ accFixRounds = fixRounds;
394
+ accTotalTokens = totalTokens;
395
+ accTotalCostUsd = totalCostUsd;
396
+ accCacheCreationTokens = cacheCreationTokens;
397
+ accCacheReadTokens = cacheReadTokens;
398
+ accMessages = loopResult.messages;
399
+ fireOnCompletion();
400
+ const warnings = gate ? gate.advisoryNotes.map((n) => ({ category: 'advisory', message: n })) : [];
401
+ const structuredErrors = gate ? structureErrors(gate) : [];
402
+ const result = {
403
+ valid: gate ? gate.valid : true,
404
+ transaction,
405
+ errors: structuredErrors,
406
+ warnings,
407
+ hardErrors: gate?.hardErrors ?? [],
408
+ advisoryNotes: gate?.advisoryNotes ?? [],
409
+ validation: gate?.validation ?? { valid: true, issues: [] },
410
+ simulation: gate?.simulation ?? null,
411
+ audit: gate?.audit ?? null,
412
+ tokensUsed: totalTokens,
413
+ costUsd: totalCostUsd,
414
+ rounds,
415
+ fixRounds,
416
+ trace,
417
+ toString() {
418
+ const msgCount = Array.isArray(this.transaction?.messages) ? this.transaction.messages.length : 0;
419
+ const validityStr = this.valid ? 'valid' : `${this.errors.length} error${this.errors.length === 1 ? '' : 's'}`;
420
+ const costStr = this.costUsd.toFixed(4);
421
+ const cacheStr = this.trace.cacheReadTokens > 0
422
+ ? `, cache ${this.trace.cacheReadTokens.toLocaleString()} read / ${this.trace.cacheCreationTokens.toLocaleString()} write`
423
+ : '';
424
+ return `BitBadgesBuilderAgent build: ${msgCount} message(s), ${validityStr}, ${this.tokensUsed.toLocaleString()} tokens${cacheStr}, $${costStr}, ${this.rounds} round(s)${this.fixRounds ? ` + ${this.fixRounds} fix` : ''}`;
425
+ }
426
+ };
427
+ return result;
428
+ }
429
+ finally {
430
+ fireOnCompletion();
431
+ }
432
+ }
433
+ getSystemPrompt(mode = 'create') {
434
+ if (this.options.systemPrompt)
435
+ return this.options.systemPrompt;
436
+ const base = (0, prompt_js_1.buildSystemPrompt)(mode);
437
+ return this.options.systemPromptAppend
438
+ ? `${base}\n\n## Additional Guidance (from consumer)\n${this.options.systemPromptAppend}`
439
+ : base;
440
+ }
441
+ async healthCheck() {
442
+ const report = {
443
+ anthropic: { ok: false, error: undefined, model: undefined },
444
+ bitbadgesApi: { ok: false, configured: false, error: undefined }
445
+ };
446
+ try {
447
+ this.client ?? (this.client = await (0, anthropicClient_js_1.getAnthropicClient)({
448
+ client: this.options.anthropicClient,
449
+ apiKey: this.options.anthropicKey,
450
+ authToken: this.options.anthropicAuthToken,
451
+ baseURL: this.options.anthropicBaseUrl
452
+ }));
453
+ await this.client.messages.create({
454
+ model: this.model.id,
455
+ max_tokens: 1,
456
+ messages: [{ role: 'user', content: 'ping' }]
457
+ });
458
+ report.anthropic.ok = true;
459
+ report.anthropic.model = this.model.id;
460
+ }
461
+ catch (e) {
462
+ report.anthropic.error = e?.message || String(e);
463
+ }
464
+ const envBbKey = typeof process !== 'undefined' ? process.env.BITBADGES_API_KEY : undefined;
465
+ const hasBbKey = !!(this.options.bitbadgesApiKey || envBbKey);
466
+ report.bitbadgesApi.configured = hasBbKey;
467
+ if (hasBbKey) {
468
+ try {
469
+ const result = await this.registry.execute('get_current_timestamp', {}, { sessionId: 'healthcheck', callerAddress: this.options.defaultCreatorAddress ?? '' });
470
+ const parsed = JSON.parse(result);
471
+ report.bitbadgesApi.ok = !parsed?.error;
472
+ if (parsed?.error)
473
+ report.bitbadgesApi.error = parsed.error;
474
+ }
475
+ catch (e) {
476
+ report.bitbadgesApi.error = e?.message || String(e);
477
+ }
478
+ }
479
+ return report;
480
+ }
481
+ async validate(transaction, options) {
482
+ const onChainSnapshot = options?.existingCollectionId && this.options.onChainSnapshotFetcher
483
+ ? await this.options.onChainSnapshotFetcher(options.existingCollectionId).catch(() => null)
484
+ : undefined;
485
+ const gate = await (0, validation_js_1.runValidationGate)({
486
+ transaction,
487
+ creatorAddress: options?.creatorAddress ?? this.options.defaultCreatorAddress ?? '',
488
+ simulate: this.options.simulate,
489
+ abortSignal: options?.abortSignal,
490
+ onChainSnapshot
491
+ });
492
+ return {
493
+ valid: gate.valid,
494
+ errors: structureErrors(gate),
495
+ warnings: gate.advisoryNotes.map((n) => ({ category: 'advisory', message: n })),
496
+ validation: gate.validation,
497
+ simulation: gate.simulation,
498
+ audit: gate.audit
499
+ };
500
+ }
501
+ }
502
+ exports.BitBadgesBuilderAgent = BitBadgesBuilderAgent;
503
+ function structureErrors(gate) {
504
+ const errors = [];
505
+ for (const raw of gate.hardErrors) {
506
+ const tag = raw.match(/^\[(\w+)\]/)?.[1] ?? 'error';
507
+ errors.push({ code: tag, message: raw.replace(/^\[\w+\]\s*/, '') });
508
+ }
509
+ return errors;
510
+ }
511
+ //# sourceMappingURL=BitBadgesBuilderAgent.js.map