briyah 2.0.3 → 2.0.5

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 (391) hide show
  1. package/README.md +55 -8
  2. package/data/common/config/LICENSE-model-prices.md +65 -0
  3. package/data/common/config/markup +1 -1
  4. package/data/common/config/model_prices.json +47443 -46295
  5. package/data/common/config/story_models.json +3 -3
  6. package/data/common/prompts/illustrator/describe_scene.prompt +4 -4
  7. package/data/common/prompts/narrator/create_story.prompt +2 -0
  8. package/data/common/prompts/perceive.prompt +1 -1
  9. package/data/packs/dnd5e/prompts/battle_master/perceive.prompt +1 -1
  10. package/data/packs/dnd5e/prompts/campaign_designer/perceive.prompt +1 -1
  11. package/data/packs/dnd5e/prompts/character_creator/perceive.prompt +795 -787
  12. package/data/packs/dnd5e/prompts/dungeon_master/defaults_config.json +2 -1
  13. package/data/packs/dnd5e/prompts/dungeon_master/perceive.prompt +1 -1
  14. package/data/packs/dnd5e/rooms/campaign.json +18 -15
  15. package/data/packs/dnd5e/rooms/table.json +17 -16
  16. package/dist-sdk/server/src/ai/LLM/anthropic.service.d.ts +103 -0
  17. package/dist-sdk/server/src/ai/LLM/anthropic.service.js +174 -3
  18. package/dist-sdk/server/src/ai/LLM/base-ai.service.d.ts +194 -3
  19. package/dist-sdk/server/src/ai/LLM/base-ai.service.js +299 -19
  20. package/dist-sdk/server/src/ai/LLM/blank-image.d.ts +36 -0
  21. package/dist-sdk/server/src/ai/LLM/blank-image.js +80 -0
  22. package/dist-sdk/server/src/ai/LLM/comfyui-client.d.ts +26 -0
  23. package/dist-sdk/server/src/ai/LLM/comfyui-client.js +25 -0
  24. package/dist-sdk/server/src/ai/LLM/comfyui-workflow.d.ts +54 -0
  25. package/dist-sdk/server/src/ai/LLM/comfyui-workflow.js +59 -1
  26. package/dist-sdk/server/src/ai/LLM/comfyui.service.d.ts +21 -1
  27. package/dist-sdk/server/src/ai/LLM/comfyui.service.js +25 -1
  28. package/dist-sdk/server/src/ai/LLM/deepseek.service.d.ts +52 -0
  29. package/dist-sdk/server/src/ai/LLM/deepseek.service.js +103 -4
  30. package/dist-sdk/server/src/ai/LLM/fal.service.d.ts +25 -0
  31. package/dist-sdk/server/src/ai/LLM/fal.service.js +56 -2
  32. package/dist-sdk/server/src/ai/LLM/googleai.service.d.ts +19 -0
  33. package/dist-sdk/server/src/ai/LLM/googleai.service.js +43 -2
  34. package/dist-sdk/server/src/ai/LLM/grok.service.d.ts +22 -0
  35. package/dist-sdk/server/src/ai/LLM/grok.service.js +55 -3
  36. package/dist-sdk/server/src/ai/LLM/mock.service.js +31 -4
  37. package/dist-sdk/server/src/ai/LLM/openai.service.d.ts +30 -0
  38. package/dist-sdk/server/src/ai/LLM/openai.service.js +81 -3
  39. package/dist-sdk/server/src/ai/LLM/self-hosted.service.d.ts +21 -0
  40. package/dist-sdk/server/src/ai/LLM/self-hosted.service.js +45 -3
  41. package/dist-sdk/server/src/ai/LLM/together.service.d.ts +30 -0
  42. package/dist-sdk/server/src/ai/LLM/together.service.js +61 -3
  43. package/dist-sdk/server/src/ai/agent-config.d.ts +15 -1
  44. package/dist-sdk/server/src/ai/agent-factory.d.ts +16 -0
  45. package/dist-sdk/server/src/ai/agent-factory.js +20 -0
  46. package/dist-sdk/server/src/ai/agent-message.service.d.ts +30 -0
  47. package/dist-sdk/server/src/ai/agent-message.service.js +30 -0
  48. package/dist-sdk/server/src/ai/agent-store.service.d.ts +80 -0
  49. package/dist-sdk/server/src/ai/agent-store.service.js +166 -7
  50. package/dist-sdk/server/src/ai/agent.d.ts +197 -2
  51. package/dist-sdk/server/src/ai/agent.js +225 -14
  52. package/dist-sdk/server/src/ai/ai-debug-logger.d.ts +18 -0
  53. package/dist-sdk/server/src/ai/ai-debug-logger.js +19 -0
  54. package/dist-sdk/server/src/ai/ai-factory.service.d.ts +9 -0
  55. package/dist-sdk/server/src/ai/ai-factory.service.js +9 -0
  56. package/dist-sdk/server/src/ai/artifact.service.d.ts +131 -0
  57. package/dist-sdk/server/src/ai/artifact.service.js +148 -3
  58. package/dist-sdk/server/src/ai/attached-file.service.d.ts +87 -0
  59. package/dist-sdk/server/src/ai/attached-file.service.js +105 -0
  60. package/dist-sdk/server/src/ai/model_prices.d.ts +30 -0
  61. package/dist-sdk/server/src/ai/model_prices.js +91 -5
  62. package/dist-sdk/server/src/ai/pack-artifacts.d.ts +84 -0
  63. package/dist-sdk/server/src/ai/pack-artifacts.js +168 -0
  64. package/dist-sdk/server/src/ai/published-agents.service.d.ts +25 -0
  65. package/dist-sdk/server/src/ai/published-agents.service.js +30 -3
  66. package/dist-sdk/server/src/app/balance-message.service.d.ts +24 -0
  67. package/dist-sdk/server/src/app/balance-message.service.js +24 -0
  68. package/dist-sdk/server/src/app/balance.module.d.ts +4 -0
  69. package/dist-sdk/server/src/app/balance.module.js +4 -0
  70. package/dist-sdk/server/src/app/balance.service.d.ts +37 -0
  71. package/dist-sdk/server/src/app/balance.service.js +54 -0
  72. package/dist-sdk/server/src/app/stripe.controller.d.ts +21 -0
  73. package/dist-sdk/server/src/app/stripe.controller.js +35 -0
  74. package/dist-sdk/server/src/app/stripe.service.d.ts +43 -0
  75. package/dist-sdk/server/src/app/stripe.service.js +62 -0
  76. package/dist-sdk/server/src/app/transaction.service.d.ts +27 -0
  77. package/dist-sdk/server/src/app/transaction.service.js +36 -0
  78. package/dist-sdk/server/src/app/user-service-factory.d.ts +8 -0
  79. package/dist-sdk/server/src/app/user-service-factory.js +46 -3
  80. package/dist-sdk/server/src/app/user-service-manager.d.ts +27 -0
  81. package/dist-sdk/server/src/app/user-service-manager.js +43 -0
  82. package/dist-sdk/server/src/app.controller.d.ts +120 -0
  83. package/dist-sdk/server/src/app.controller.js +365 -14
  84. package/dist-sdk/server/src/app.module.js +28 -6
  85. package/dist-sdk/server/src/app.service.d.ts +1710 -4
  86. package/dist-sdk/server/src/app.service.js +1938 -20
  87. package/dist-sdk/server/src/auth/agent-access.decorator.d.ts +17 -0
  88. package/dist-sdk/server/src/auth/agent-access.decorator.js +17 -0
  89. package/dist-sdk/server/src/auth/auth.controller.js +46 -0
  90. package/dist-sdk/server/src/auth/auth.module.js +3 -2
  91. package/dist-sdk/server/src/auth/auth.service.d.ts +33 -0
  92. package/dist-sdk/server/src/auth/auth.service.js +39 -3
  93. package/dist-sdk/server/src/auth/jwt-auth.guard.js +44 -0
  94. package/dist-sdk/server/src/auth/jwt.strategy.js +2 -2
  95. package/dist-sdk/server/src/auth/owner-only.decorator.d.ts +9 -0
  96. package/dist-sdk/server/src/auth/owner-only.decorator.js +9 -0
  97. package/dist-sdk/server/src/auth/phone-validation.service.d.ts +25 -0
  98. package/dist-sdk/server/src/auth/phone-validation.service.js +30 -0
  99. package/dist-sdk/server/src/auth/rate-limit.service.d.ts +19 -0
  100. package/dist-sdk/server/src/auth/rate-limit.service.js +24 -0
  101. package/dist-sdk/server/src/auth/room-access.decorator.d.ts +8 -0
  102. package/dist-sdk/server/src/auth/room-access.decorator.js +8 -0
  103. package/dist-sdk/server/src/auth/scopes.decorator.d.ts +7 -0
  104. package/dist-sdk/server/src/auth/scopes.decorator.js +7 -0
  105. package/dist-sdk/server/src/auth/scopes.guard.d.ts +16 -0
  106. package/dist-sdk/server/src/auth/scopes.guard.js +30 -0
  107. package/dist-sdk/server/src/auth/session.guard.d.ts +4 -0
  108. package/dist-sdk/server/src/auth/session.guard.js +8 -0
  109. package/dist-sdk/server/src/auth/twilio.service.d.ts +27 -0
  110. package/dist-sdk/server/src/auth/twilio.service.js +30 -4
  111. package/dist-sdk/server/src/auth/users.service.d.ts +30 -0
  112. package/dist-sdk/server/src/auth/users.service.js +42 -0
  113. package/dist-sdk/server/src/common/errors.d.ts +20 -0
  114. package/dist-sdk/server/src/common/errors.js +21 -0
  115. package/dist-sdk/server/src/config/env-config.d.ts +7 -0
  116. package/dist-sdk/server/src/config/prompt-resolver.d.ts +180 -0
  117. package/dist-sdk/server/src/config/prompt-resolver.js +182 -0
  118. package/dist-sdk/server/src/config/user-config.service.d.ts +59 -0
  119. package/dist-sdk/server/src/config/user-config.service.js +57 -0
  120. package/dist-sdk/server/src/config/user-paths.service.d.ts +61 -0
  121. package/dist-sdk/server/src/config/user-paths.service.js +67 -0
  122. package/dist-sdk/server/src/documents/pdf-conversion.d.ts +121 -1
  123. package/dist-sdk/server/src/documents/pdf-conversion.js +94 -0
  124. package/dist-sdk/server/src/packs/pack-instantiation.service.d.ts +98 -0
  125. package/dist-sdk/server/src/packs/pack-instantiation.service.js +104 -0
  126. package/dist-sdk/server/src/packs/pack-manifest.d.ts +114 -0
  127. package/dist-sdk/server/src/packs/pack-manifest.js +69 -0
  128. package/dist-sdk/server/src/packs/pack.service.d.ts +99 -1
  129. package/dist-sdk/server/src/packs/pack.service.js +98 -1
  130. package/dist-sdk/server/src/room/artifact-store.service.d.ts +27 -0
  131. package/dist-sdk/server/src/room/artifact-store.service.js +49 -3
  132. package/dist-sdk/server/src/room/artifact.d.ts +18 -0
  133. package/dist-sdk/server/src/room/illustration/illustration.service.d.ts +211 -0
  134. package/dist-sdk/server/src/room/illustration/illustration.service.js +771 -0
  135. package/dist-sdk/server/src/room/illustration/illustration.types.d.ts +101 -0
  136. package/dist-sdk/server/src/room/illustration/illustration.types.js +2 -0
  137. package/dist-sdk/server/src/room/message.d.ts +64 -0
  138. package/dist-sdk/server/src/room/message.js +77 -2
  139. package/dist-sdk/server/src/room/nickname.d.ts +26 -0
  140. package/dist-sdk/server/src/room/nickname.js +26 -0
  141. package/dist-sdk/server/src/room/published-rooms.service.js +2 -0
  142. package/dist-sdk/server/src/room/room-config.d.ts +84 -1
  143. package/dist-sdk/server/src/room/room-config.js +19 -0
  144. package/dist-sdk/server/src/room/room-factory.d.ts +20 -0
  145. package/dist-sdk/server/src/room/room-factory.js +25 -0
  146. package/dist-sdk/server/src/room/room-message.service.d.ts +28 -0
  147. package/dist-sdk/server/src/room/room-message.service.js +28 -0
  148. package/dist-sdk/server/src/room/room-store.service.d.ts +184 -0
  149. package/dist-sdk/server/src/room/room-store.service.js +280 -6
  150. package/dist-sdk/server/src/room/room.d.ts +981 -1
  151. package/dist-sdk/server/src/room/room.js +1531 -27
  152. package/dist-sdk/server/src/rpg/rpg-models.d.ts +50 -0
  153. package/dist-sdk/server/src/rpg/rpg-models.js +53 -0
  154. package/dist-sdk/server/src/rpg/rpg-roster.d.ts +37 -0
  155. package/dist-sdk/server/src/rpg/rpg-roster.js +37 -0
  156. package/dist-sdk/server/src/rpg/rpg.service.d.ts +87 -0
  157. package/dist-sdk/server/src/rpg/rpg.service.js +108 -1
  158. package/dist-sdk/server/src/sdk/briyah-config.d.ts +57 -0
  159. package/dist-sdk/server/src/sdk/briyah-config.js +17 -0
  160. package/dist-sdk/server/src/sdk/briyah.d.ts +228 -0
  161. package/dist-sdk/server/src/sdk/briyah.js +285 -7
  162. package/dist-sdk/server/src/sdk/index.js +23 -0
  163. package/dist-sdk/server/src/shared/shared.module.d.ts +8 -0
  164. package/dist-sdk/server/src/shared/shared.module.js +13 -5
  165. package/dist-sdk/server/src/story/story-message.service.d.ts +43 -0
  166. package/dist-sdk/server/src/story/story-message.service.js +43 -0
  167. package/dist-sdk/server/src/story/story-progress.service.d.ts +23 -0
  168. package/dist-sdk/server/src/story/story-progress.service.js +23 -0
  169. package/dist-sdk/server/src/story/story-store.service.d.ts +92 -0
  170. package/dist-sdk/server/src/story/story-store.service.js +136 -1
  171. package/dist-sdk/server/src/story/story.service.d.ts +366 -9
  172. package/dist-sdk/server/src/story/story.service.js +825 -374
  173. package/dist-sdk/server/src/tools/core/illustrate.tool.d.ts +14 -0
  174. package/dist-sdk/server/src/tools/core/illustrate.tool.js +80 -0
  175. package/dist-sdk/server/src/tools/core/index.d.ts +22 -0
  176. package/dist-sdk/server/src/tools/core/index.js +24 -1
  177. package/dist-sdk/server/src/tools/core/rename.tool.d.ts +8 -0
  178. package/dist-sdk/server/src/tools/core/rename.tool.js +11 -0
  179. package/dist-sdk/server/src/tools/native-tool-registry.service.d.ts +22 -0
  180. package/dist-sdk/server/src/tools/native-tool-registry.service.js +22 -0
  181. package/dist-sdk/server/src/tools/native-tool.types.d.ts +266 -0
  182. package/dist-sdk/server/src/tools/packs/dnd5e/data/backgrounds.d.ts +29 -0
  183. package/dist-sdk/server/src/tools/packs/dnd5e/data/backgrounds.js +29 -0
  184. package/dist-sdk/server/src/tools/packs/dnd5e/data/cantrips.d.ts +46 -0
  185. package/dist-sdk/server/src/tools/packs/dnd5e/data/cantrips.js +40 -0
  186. package/dist-sdk/server/src/tools/packs/dnd5e/data/feats.d.ts +33 -0
  187. package/dist-sdk/server/src/tools/packs/dnd5e/data/feats.js +19 -0
  188. package/dist-sdk/server/src/tools/packs/dnd5e/data/gear.d.ts +37 -0
  189. package/dist-sdk/server/src/tools/packs/dnd5e/data/gear.generated.d.ts +17 -0
  190. package/dist-sdk/server/src/tools/packs/dnd5e/data/gear.generated.js +17 -0
  191. package/dist-sdk/server/src/tools/packs/dnd5e/data/gear.js +27 -0
  192. package/dist-sdk/server/src/tools/packs/dnd5e/data/kits.d.ts +36 -0
  193. package/dist-sdk/server/src/tools/packs/dnd5e/data/kits.js +32 -0
  194. package/dist-sdk/server/src/tools/packs/dnd5e/data/magic-items.d.ts +32 -0
  195. package/dist-sdk/server/src/tools/packs/dnd5e/data/magic-items.generated.d.ts +13 -0
  196. package/dist-sdk/server/src/tools/packs/dnd5e/data/magic-items.generated.js +13 -0
  197. package/dist-sdk/server/src/tools/packs/dnd5e/data/magic-items.js +18 -0
  198. package/dist-sdk/server/src/tools/packs/dnd5e/data/monsters.d.ts +54 -0
  199. package/dist-sdk/server/src/tools/packs/dnd5e/data/monsters.generated.d.ts +13 -0
  200. package/dist-sdk/server/src/tools/packs/dnd5e/data/monsters.generated.js +13 -0
  201. package/dist-sdk/server/src/tools/packs/dnd5e/data/monsters.js +20 -0
  202. package/dist-sdk/server/src/tools/packs/dnd5e/data/races.d.ts +36 -0
  203. package/dist-sdk/server/src/tools/packs/dnd5e/data/races.js +18 -0
  204. package/dist-sdk/server/src/tools/packs/dnd5e/data/spells.d.ts +29 -0
  205. package/dist-sdk/server/src/tools/packs/dnd5e/data/spells.generated.d.ts +13 -0
  206. package/dist-sdk/server/src/tools/packs/dnd5e/data/spells.generated.js +13 -0
  207. package/dist-sdk/server/src/tools/packs/dnd5e/data/spells.js +21 -0
  208. package/dist-sdk/server/src/tools/packs/dnd5e/data/srd.d.ts +95 -0
  209. package/dist-sdk/server/src/tools/packs/dnd5e/data/srd.js +66 -2
  210. package/dist-sdk/server/src/tools/packs/dnd5e/data/weapons.d.ts +50 -0
  211. package/dist-sdk/server/src/tools/packs/dnd5e/data/weapons.js +28 -0
  212. package/dist-sdk/server/src/tools/packs/dnd5e/engine/bestiary.d.ts +92 -0
  213. package/dist-sdk/server/src/tools/packs/dnd5e/engine/bestiary.js +152 -0
  214. package/dist-sdk/server/src/tools/packs/dnd5e/engine/campaign-items.d.ts +28 -0
  215. package/dist-sdk/server/src/tools/packs/dnd5e/engine/campaign-items.js +29 -0
  216. package/dist-sdk/server/src/tools/packs/dnd5e/engine/cantrips.d.ts +31 -0
  217. package/dist-sdk/server/src/tools/packs/dnd5e/engine/cantrips.js +36 -2
  218. package/dist-sdk/server/src/tools/packs/dnd5e/engine/character.d.ts +261 -0
  219. package/dist-sdk/server/src/tools/packs/dnd5e/engine/character.js +401 -0
  220. package/dist-sdk/server/src/tools/packs/dnd5e/engine/coerce.d.ts +11 -0
  221. package/dist-sdk/server/src/tools/packs/dnd5e/engine/coerce.js +11 -0
  222. package/dist-sdk/server/src/tools/packs/dnd5e/engine/dice.d.ts +65 -0
  223. package/dist-sdk/server/src/tools/packs/dnd5e/engine/dice.js +67 -0
  224. package/dist-sdk/server/src/tools/packs/dnd5e/engine/encounter.d.ts +73 -0
  225. package/dist-sdk/server/src/tools/packs/dnd5e/engine/encounter.js +63 -0
  226. package/dist-sdk/server/src/tools/packs/dnd5e/engine/encumbrance.d.ts +66 -0
  227. package/dist-sdk/server/src/tools/packs/dnd5e/engine/encumbrance.js +59 -0
  228. package/dist-sdk/server/src/tools/packs/dnd5e/engine/feats.d.ts +54 -0
  229. package/dist-sdk/server/src/tools/packs/dnd5e/engine/feats.js +49 -0
  230. package/dist-sdk/server/src/tools/packs/dnd5e/engine/foes.d.ts +42 -0
  231. package/dist-sdk/server/src/tools/packs/dnd5e/engine/foes.js +59 -0
  232. package/dist-sdk/server/src/tools/packs/dnd5e/engine/guides.d.ts +72 -0
  233. package/dist-sdk/server/src/tools/packs/dnd5e/engine/guides.js +63 -0
  234. package/dist-sdk/server/src/tools/packs/dnd5e/engine/items.d.ts +57 -0
  235. package/dist-sdk/server/src/tools/packs/dnd5e/engine/items.js +54 -0
  236. package/dist-sdk/server/src/tools/packs/dnd5e/engine/kits.d.ts +81 -0
  237. package/dist-sdk/server/src/tools/packs/dnd5e/engine/kits.js +85 -0
  238. package/dist-sdk/server/src/tools/packs/dnd5e/engine/races.d.ts +60 -0
  239. package/dist-sdk/server/src/tools/packs/dnd5e/engine/races.js +64 -0
  240. package/dist-sdk/server/src/tools/packs/dnd5e/engine/rules.d.ts +169 -0
  241. package/dist-sdk/server/src/tools/packs/dnd5e/engine/rules.js +203 -0
  242. package/dist-sdk/server/src/tools/packs/dnd5e/engine/sheet.d.ts +201 -0
  243. package/dist-sdk/server/src/tools/packs/dnd5e/engine/sheet.js +149 -0
  244. package/dist-sdk/server/src/tools/packs/dnd5e/engine/spells.d.ts +56 -0
  245. package/dist-sdk/server/src/tools/packs/dnd5e/engine/spells.js +54 -0
  246. package/dist-sdk/server/src/tools/packs/dnd5e/engine/weapons.d.ts +65 -0
  247. package/dist-sdk/server/src/tools/packs/dnd5e/engine/weapons.js +73 -0
  248. package/dist-sdk/server/src/tools/packs/dnd5e/index.d.ts +18 -0
  249. package/dist-sdk/server/src/tools/packs/dnd5e/index.js +18 -0
  250. package/dist-sdk/server/src/tools/packs/dnd5e/tools/check.tool.d.ts +4 -0
  251. package/dist-sdk/server/src/tools/packs/dnd5e/tools/check.tool.js +116 -1
  252. package/dist-sdk/server/src/tools/packs/dnd5e/tools/class.tool.d.ts +14 -0
  253. package/dist-sdk/server/src/tools/packs/dnd5e/tools/class.tool.js +17 -0
  254. package/dist-sdk/server/src/tools/packs/dnd5e/tools/dice.tools.d.ts +1 -0
  255. package/dist-sdk/server/src/tools/packs/dnd5e/tools/dice.tools.js +35 -1
  256. package/dist-sdk/server/src/tools/packs/dnd5e/tools/encounter.tool.d.ts +6 -0
  257. package/dist-sdk/server/src/tools/packs/dnd5e/tools/encounter.tool.js +25 -0
  258. package/dist-sdk/server/src/tools/packs/dnd5e/tools/item.tool.d.ts +7 -0
  259. package/dist-sdk/server/src/tools/packs/dnd5e/tools/item.tool.js +13 -0
  260. package/dist-sdk/server/src/tools/packs/dnd5e/tools/monster.tool.d.ts +8 -0
  261. package/dist-sdk/server/src/tools/packs/dnd5e/tools/monster.tool.js +55 -0
  262. package/dist-sdk/server/src/tools/packs/dnd5e/tools/shared.d.ts +194 -0
  263. package/dist-sdk/server/src/tools/packs/dnd5e/tools/shared.js +263 -1
  264. package/dist-sdk/server/src/tools/packs/dnd5e/tools/sheet.tools.d.ts +1 -0
  265. package/dist-sdk/server/src/tools/packs/dnd5e/tools/sheet.tools.js +127 -2
  266. package/dist-sdk/server/src/tools/packs/dnd5e/tools/spell.tool.d.ts +13 -0
  267. package/dist-sdk/server/src/tools/packs/dnd5e/tools/spell.tool.js +18 -0
  268. package/dist-sdk/server/src/tools/packs/dnd5e/tools/state.tools.d.ts +1 -0
  269. package/dist-sdk/server/src/tools/packs/dnd5e/tools/state.tools.js +23 -0
  270. package/dist-sdk/server/src/tools/packs/dnd5e/tools/world.tools.d.ts +1 -0
  271. package/dist-sdk/server/src/tools/packs/dnd5e/tools/world.tools.js +35 -0
  272. package/dist-sdk/server/src/tools/packs/index.d.ts +49 -0
  273. package/dist-sdk/server/src/tools/packs/index.js +66 -0
  274. package/dist-sdk/server/src/tools/shared/coerce.d.ts +95 -0
  275. package/dist-sdk/server/src/tools/shared/coerce.js +98 -0
  276. package/dist-sdk/server/src/tools/tool-execution.service.d.ts +107 -3
  277. package/dist-sdk/server/src/tools/tool-execution.service.js +163 -19
  278. package/dist-sdk/server/src/tools/tool-store.service.d.ts +11 -0
  279. package/dist-sdk/server/src/tools/tool-store.service.js +13 -1
  280. package/dist-sdk/shared/types/app.types.d.ts +328 -13
  281. package/dist-sdk/shared/types/app.types.js +18 -1
  282. package/package.json +167 -168
  283. package/data/common/config/random_words.txt +0 -2241
  284. package/data/common/story_ideas/biblical.json +0 -122
  285. package/data/common/story_ideas/fantasy.json +0 -82
  286. package/data/common/story_ideas/historical_fiction.json +0 -82
  287. package/data/common/story_ideas/science_fiction.json +0 -82
  288. package/docs/.nojekyll +0 -1
  289. package/docs/assets/hierarchy.js +0 -1
  290. package/docs/assets/highlight.css +0 -113
  291. package/docs/assets/icons.js +0 -18
  292. package/docs/assets/icons.svg +0 -1
  293. package/docs/assets/main.js +0 -60
  294. package/docs/assets/navigation.js +0 -1
  295. package/docs/assets/search.js +0 -1
  296. package/docs/assets/style.css +0 -1648
  297. package/docs/classes/Agent.html +0 -801
  298. package/docs/classes/Briyah.html +0 -296
  299. package/docs/classes/BriyahConfigService.html +0 -119
  300. package/docs/classes/NativeToolRegistry.html +0 -166
  301. package/docs/classes/PackInstantiationService.html +0 -157
  302. package/docs/classes/PackService.html +0 -247
  303. package/docs/classes/Room.html +0 -1434
  304. package/docs/classes/RoomMessage.html +0 -290
  305. package/docs/enums/MessageAction.html +0 -136
  306. package/docs/functions/availableToolPacks.html +0 -41
  307. package/docs/functions/buildMarkdown.html +0 -54
  308. package/docs/functions/dnd5eTools.html +0 -44
  309. package/docs/functions/installToolPacks.html +0 -51
  310. package/docs/functions/pageMarker.html +0 -45
  311. package/docs/functions/registerToolPacks.html +0 -54
  312. package/docs/functions/splitMarkdownIntoPages.html +0 -51
  313. package/docs/functions/stripWrappingFence.html +0 -52
  314. package/docs/hierarchy.html +0 -46
  315. package/docs/index.html +0 -595
  316. package/docs/interfaces/AgentCreationOptions.html +0 -125
  317. package/docs/interfaces/AgentInfo.html +0 -218
  318. package/docs/interfaces/AgentMessagesResponse.html +0 -74
  319. package/docs/interfaces/AppService.html +0 -3340
  320. package/docs/interfaces/Artifact.html +0 -95
  321. package/docs/interfaces/ArtifactMetadata.html +0 -112
  322. package/docs/interfaces/ArtifactWriteOptions.html +0 -90
  323. package/docs/interfaces/AttachDocumentResponse.html +0 -62
  324. package/docs/interfaces/BriyahConfigOptions.html +0 -124
  325. package/docs/interfaces/ChapterInfo.html +0 -62
  326. package/docs/interfaces/Character.html +0 -86
  327. package/docs/interfaces/ConversionAgent.html +0 -78
  328. package/docs/interfaces/CreateAgentResponse.html +0 -56
  329. package/docs/interfaces/CreateRoomResponse.html +0 -62
  330. package/docs/interfaces/CreateStoryResponse.html +0 -56
  331. package/docs/interfaces/FileList.html +0 -56
  332. package/docs/interfaces/FileMetadata.html +0 -89
  333. package/docs/interfaces/IConfigService.html +0 -79
  334. package/docs/interfaces/InstantiateModels.html +0 -64
  335. package/docs/interfaces/InstantiateRoomResult.html +0 -70
  336. package/docs/interfaces/LoadedPack.html +0 -131
  337. package/docs/interfaces/LoggingOptions.html +0 -92
  338. package/docs/interfaces/Message.html +0 -84
  339. package/docs/interfaces/ModelChoice.html +0 -73
  340. package/docs/interfaces/ModelInfo.html +0 -74
  341. package/docs/interfaces/NativeToolDefinition.html +0 -107
  342. package/docs/interfaces/PackAgentTemplate.html +0 -101
  343. package/docs/interfaces/PackRoomTemplate.html +0 -108
  344. package/docs/interfaces/PackView.html +0 -172
  345. package/docs/interfaces/PdfConversionOptions.html +0 -102
  346. package/docs/interfaces/PdfConversionResult.html +0 -97
  347. package/docs/interfaces/PdfMarkdownOptions.html +0 -198
  348. package/docs/interfaces/PdfMarkdownResult.html +0 -106
  349. package/docs/interfaces/PdfPageMarkdown.html +0 -94
  350. package/docs/interfaces/PreparedPromptResponse.html +0 -62
  351. package/docs/interfaces/ProcessTextResponse.html +0 -86
  352. package/docs/interfaces/PromptFile.html +0 -70
  353. package/docs/interfaces/PromptFileContent.html +0 -76
  354. package/docs/interfaces/PromptFilesResponse.html +0 -56
  355. package/docs/interfaces/PromptFolder.html +0 -80
  356. package/docs/interfaces/PromptFolderDefaults.html +0 -155
  357. package/docs/interfaces/PromptFolderDefaultsResponse.html +0 -79
  358. package/docs/interfaces/PromptFoldersResponse.html +0 -56
  359. package/docs/interfaces/RoomDetails.html +0 -151
  360. package/docs/interfaces/RoomErrorUpdate.html +0 -82
  361. package/docs/interfaces/RoomInfo.html +0 -88
  362. package/docs/interfaces/RoomMessageUpdate.html +0 -113
  363. package/docs/interfaces/RoomMessagesResponse.html +0 -62
  364. package/docs/interfaces/SendMessageResult.html +0 -62
  365. package/docs/interfaces/StoryErrorEvent.html +0 -84
  366. package/docs/interfaces/StoryIdea.html +0 -74
  367. package/docs/interfaces/StoryInfo.html +0 -198
  368. package/docs/interfaces/StoryIntroduceCharacterEvent.html +0 -79
  369. package/docs/interfaces/StoryProgressChapterEvent.html +0 -72
  370. package/docs/interfaces/StoryState.html +0 -108
  371. package/docs/interfaces/StoryStateEvent.html +0 -76
  372. package/docs/interfaces/ToolArtifact.html +0 -85
  373. package/docs/interfaces/ToolCaller.html +0 -77
  374. package/docs/interfaces/ToolDefinition.html +0 -80
  375. package/docs/interfaces/ToolHelpers.html +0 -105
  376. package/docs/interfaces/ToolPackDocuments.html +0 -100
  377. package/docs/interfaces/ToolParameter.html +0 -74
  378. package/docs/interfaces/ToolRoomAccess.html +0 -260
  379. package/docs/interfaces/ToolSummary.html +0 -74
  380. package/docs/interfaces/ToolsResponse.html +0 -68
  381. package/docs/interfaces/Transaction.html +0 -104
  382. package/docs/interfaces/TransactionHistoryResponse.html +0 -62
  383. package/docs/modules.html +0 -53
  384. package/docs/types/PackOrigin.html +0 -34
  385. package/docs/types/PromptScope.html +0 -34
  386. package/docs/types/RoomTurnMode.html +0 -42
  387. package/docs/types/RoomUpdate.html +0 -36
  388. package/docs/types/RosterOverrides.html +0 -42
  389. package/docs/types/ToolContext.html +0 -40
  390. package/docs/types/ToolRunResult.html +0 -34
  391. package/src/tools/packs/dnd5e/README.md +0 -867
package/docs/index.html DELETED
@@ -1,595 +0,0 @@
1
- <!DOCTYPE html><html class="default" lang="en" data-base="./"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>Briyah SDK - v2.0.3</title><meta name="description" content="Documentation for Briyah SDK"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="assets/style.css"/><link rel="stylesheet" href="assets/highlight.css"/><script defer src="assets/main.js"></script><script async src="assets/icons.js" id="tsd-icons-script"></script><script async src="assets/search.js" id="tsd-search-script"></script><script async src="assets/navigation.js" id="tsd-nav-script"></script><script async src="assets/hierarchy.js" id="tsd-hierarchy-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar">
2
- <div class="tsd-toolbar-contents container"><a href="index.html" class="title">Briyah SDK - v2.0.3</a>
3
- <div id="tsd-toolbar-links"><a href="https://github.com/your-org/briyah">GitHub</a><a href="https://www.npmjs.com/package/briyah">npm</a></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/>
4
- <ul role="listbox" id="tsd-search-results"></ul>
5
- <div id="tsd-search-status" aria-live="polite" aria-atomic="true">
6
- <div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-menu"></use></svg></a></div></header>
7
- <div class="container container-main">
8
- <div class="col-content">
9
- <div class="tsd-page-title">
10
- <h1>Briyah SDK - v2.0.3</h1></div>
11
- <div class="tsd-panel tsd-typography"><h1 id="briyah-sdk" class="tsd-anchor-link">Briyah SDK<a href="#briyah-sdk" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h1>
12
- <p>Briyah is an SDK for AI agents, multi-agent rooms, and interactive stories. It wraps the Briyah runtime as a single class with per-user data isolation and balance tracking, and supports Anthropic, OpenAI, Google, Grok, DeepSeek, Together, and Fal as LLM providers.</p>
13
- <p>What's in the package:</p>
14
- <ul>
15
- <li><strong>Agents.</strong> AI agents with a system prompt, persistent conversation history, attached documents, and per-call cost and token tracking. Histories can be summarized/compacted or cleared.</li>
16
- <li><strong>Rooms.</strong> Multi-agent conversations with a shared goal. Agents exchange messages at different visibility levels (speak, whisper, think, shout), publish shared artifacts, run tools, commission each other for parallel research, and a moderator can route turns.</li>
17
- <li><strong>Tools.</strong> Functions agents can call from a room, granted per agent. Two kinds:
18
- <ul>
19
- <li><strong>Native tools</strong> — real functions you register in your host code with <code>briyah.registerTool</code> (see <a href="#example-8-registering-native-tools-with-opaque-context">Example 8</a>). They have full access to your imports, database, and filesystem, and receive an <strong>opaque per-invocation context</strong> you supply — use it to scope a call to trusted data the model cannot set. This is the recommended way for SDK integrations.</li>
20
- <li><strong>User-authored tools</strong> — named JS-string bodies stored per user and run in-process with a timeout (<code>helpers.fetch</code> available). Off by default; enable with <code>allowUserAuthoredTools</code> only when you trust the authors. On multi-user hosts, leave off and expose native tools instead.
21
- An agent may chain several tool calls privately before responding.</li>
22
- </ul>
23
- </li>
24
- <li><strong>Stories.</strong> Text-based interactive fiction with a narrator, AI-played characters, and a human player. Stories progress through chapters and can introduce new characters mid-game. Story events (state updates, character suggestions, chapter transitions, errors) are delivered through an emitter the host app subscribes to.</li>
25
- <li><strong>Text processing.</strong> One-shot <code>processText</code> for non-conversational use, with cost and token counts on the result.</li>
26
- <li><strong>File attachments.</strong> Attach documents to agents as context. Supported file types vary by LLM provider.</li>
27
- <li><strong>Per-user data.</strong> Agents, rooms, stories, attached files, balances, and transactions are scoped to a userId the host controls. Per-user directories keep state isolated.</li>
28
- <li><strong>Balances and transactions.</strong> Track per-user balances against usage cost. Record pending, succeeded, failed, or cancelled transactions when integrating with a payment provider, credit balances on webhook confirmation, and resume stories that paused on insufficient funds. Briyah does not process payments itself.</li>
29
- <li><strong>Real-time event emitters.</strong> Story-state and balance updates can be forwarded to SSE or websockets.</li>
30
- </ul>
31
- <p>State is persisted as JSON files under a configurable data path; no external database is required.</p>
32
- <h2 id="installation" class="tsd-anchor-link">Installation<a href="#installation" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
33
- <pre><code class="bash"><span class="hl-0">npm</span><span class="hl-1"> </span><span class="hl-2">install</span><span class="hl-1"> </span><span class="hl-2">briyah</span>
34
- </code><button type="button">Copy</button></pre>
35
-
36
- <pre><code class="typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">Briyah</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;briyah&#39;</span><span class="hl-1">;</span>
37
- </code><button type="button">Copy</button></pre>
38
-
39
- <h2 id="quick-start" class="tsd-anchor-link">Quick Start<a href="#quick-start" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
40
- <pre><code class="typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">Briyah</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;briyah&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-5">// 1. Create and initialize Briyah instance</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({</span><br/><span class="hl-1"> </span><span class="hl-4">dataPath:</span><span class="hl-1"> </span><span class="hl-2">&#39;./my-briyah-data&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">userServiceCacheTimeoutMinutes:</span><span class="hl-1"> </span><span class="hl-8">30</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">startingBalance:</span><span class="hl-1"> </span><span class="hl-8">10.00</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">init</span><span class="hl-1">();</span><br/><br/><span class="hl-5">// 2. Get user-specific service (userId managed by your app)</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">appService</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-2">&#39;user-123&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// 3. Use full Briyah functionality</span><br/><span class="hl-5">// createAgent() registers the agent in memory immediately — agent.id is usable right away.</span><br/><span class="hl-5">// Call agent.save() when you want to persist it to disk.</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">agent</span><span class="hl-1"> = </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createAgent</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;AI Assistant&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;James&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;A helpful AI assistant&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;claude-haiku-4-5&#39;</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-3">if</span><span class="hl-1"> (!</span><span class="hl-4">agent</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">) </span><span class="hl-3">throw</span><span class="hl-1"> </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Error</span><span class="hl-1">(</span><span class="hl-2">&#39;Agent creation failed&#39;</span><span class="hl-1">);</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">result</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">processText</span><span class="hl-1">(</span><span class="hl-4">agent</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">, </span><span class="hl-2">&#39;Hello! How are you?&#39;</span><span class="hl-1">);</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-4">result</span><span class="hl-1">.</span><span class="hl-4">result</span><span class="hl-1">);</span><br/><br/><span class="hl-4">agent</span><span class="hl-1">.</span><span class="hl-0">save</span><span class="hl-1">(); </span><span class="hl-5">// persist to disk</span><br/><br/><span class="hl-5">// 4. Cleanup when done</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">shutdown</span><span class="hl-1">();</span>
41
- </code><button type="button">Copy</button></pre>
42
-
43
- <h2 id="upgrading-to-20" class="tsd-anchor-link">Upgrading to 2.0<a href="#upgrading-to-20" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
44
- <p>2.0 moves the D&amp;D content into a <strong>pack</strong>. Three things change for a host coming from 1.5; the
45
- first two are a line each, and the third is something to know about your data directory.</p>
46
- <p><strong>The D&amp;D prompt folders are now the pack's.</strong> <code>dungeon_master</code>, <code>dnd_player</code>,
47
- <code>character_creator</code>, <code>campaign_designer</code> and <code>dnd_room_moderator</code> belong to <code>dnd5e</code>. Enable the
48
- pack for the user, and qualify the folder name:</p>
49
- <pre><code class="typescript"><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-4">packs</span><span class="hl-1">.</span><span class="hl-0">setEnabled</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e&#39;</span><span class="hl-1">, </span><span class="hl-6">true</span><span class="hl-1">); </span><span class="hl-5">// per user, and required</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">createAgentFromFolder</span><span class="hl-1">({ </span><span class="hl-4">folder:</span><span class="hl-1"> </span><span class="hl-2">&#39;dnd5e:dungeon_master&#39;</span><span class="hl-1">, </span><span class="hl-5">/* ... */</span><span class="hl-1"> });</span>
50
- </code><button type="button">Copy</button></pre>
51
-
52
- <p>A bare <code>dungeon_master</code> also reaches the pack once it is enabled, so enabling alone is enough if
53
- you would rather not touch the strings. Doing neither is what breaks: in 1.5 those folders sat in
54
- <code>common/prompts</code> and the bare name resolved, and in 2.0 it falls through to the shared root
55
- prompts instead. <code>createAgentFromFolder</code> now refuses that rather than building a generic agent
56
- from it — see <a href="#prompt-folders">Prompt Folders</a>.</p>
57
- <p><strong><code>enabledToolPacks</code> is gone</strong>, replaced by <code>installToolPacks</code>. Most hosts can delete the call
58
- entirely: the <code>Briyah</code> constructor installs every bundled pack's tools already. <code>BRIYAH_TOOL_PACKS</code>
59
- no longer does anything either, and is not read.</p>
60
- <p><strong><code>init()</code> rewrites two things under your data path.</strong> Both are stated here because neither is
61
- visible until it has happened:</p>
62
- <ul>
63
- <li><code>{dataPath}/packs/&lt;bundled pack&gt;/</code> is <strong>deleted and re-copied on every init</strong>. A pack's
64
- directory is package-canonical, so a host that hand-edits <code>packs/dnd5e/</code> loses those edits on
65
- the next boot. Edit it as a user instead — the change lands in
66
- <code>{dataPath}/user/&lt;userId&gt;/packs/dnd5e/</code>, an overlay that takes precedence — or install your own
67
- pack under a name Briyah does not bundle, which is never touched.</li>
68
- <li>Any <code>common/prompts/&lt;folder&gt;</code> whose name a pack now owns is <strong>moved</strong> to
69
- <code>{dataPath}/retired-prompts/&lt;folder&gt;</code>, and the move is logged as a warning. On a 1.5 data
70
- directory that is the five D&amp;D folders. Moved rather than deleted because a leftover copy
71
- outranks the pack for a bare name and would silently shadow it. Nothing reads
72
- <code>retired-prompts/</code>: keep it if you had local edits worth porting into an overlay, delete it
73
- otherwise.</li>
74
- </ul>
75
- <h2 id="configuration" class="tsd-anchor-link">Configuration<a href="#configuration" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
76
- <h3 id="briyahconfigoptions" class="tsd-anchor-link">BriyahConfigOptions<a href="#briyahconfigoptions" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
77
- <pre><code class="typescript"><span class="hl-6">interface</span><span class="hl-1"> </span><span class="hl-9">BriyahConfigOptions</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-5">/**</span><br/><span class="hl-5"> * Base directory for Briyah data storage</span><br/><span class="hl-5"> * Default: &#39;./briyah-data&#39; relative to process.cwd()</span><br/><span class="hl-5"> */</span><br/><span class="hl-1"> </span><span class="hl-4">dataPath</span><span class="hl-1">?: </span><span class="hl-9">string</span><span class="hl-1">;</span><br/><br/><span class="hl-1"> </span><span class="hl-5">/**</span><br/><span class="hl-5"> * Timeout in minutes for cached user services</span><br/><span class="hl-5"> * Default: 30 minutes</span><br/><span class="hl-5"> */</span><br/><span class="hl-1"> </span><span class="hl-4">userServiceCacheTimeoutMinutes</span><span class="hl-1">?: </span><span class="hl-9">number</span><span class="hl-1">;</span><br/><br/><span class="hl-1"> </span><span class="hl-5">/**</span><br/><span class="hl-5"> * Starting balance for new users (in dollars)</span><br/><span class="hl-5"> * Default: value from process.env.STARTING_BALANCE</span><br/><span class="hl-5"> */</span><br/><span class="hl-1"> </span><span class="hl-4">startingBalance</span><span class="hl-1">?: </span><span class="hl-9">number</span><span class="hl-1">;</span><br/><br/><span class="hl-1"> </span><span class="hl-5">/**</span><br/><span class="hl-5"> * Native tools to register up front (equivalent to calling registerTool</span><br/><span class="hl-5"> * for each). See Example 8.</span><br/><span class="hl-5"> */</span><br/><span class="hl-1"> </span><span class="hl-4">tools</span><span class="hl-1">?: </span><span class="hl-9">NativeToolDefinition</span><span class="hl-1">[];</span><br/><br/><span class="hl-1"> </span><span class="hl-5">/**</span><br/><span class="hl-5"> * Allow end users to author and run their own JS-string tools. Native tools</span><br/><span class="hl-5"> * are unaffected. Leave off for untrusted multi-user hosts. Default: false.</span><br/><span class="hl-5"> */</span><br/><span class="hl-1"> </span><span class="hl-4">allowUserAuthoredTools</span><span class="hl-1">?: </span><span class="hl-9">boolean</span><span class="hl-1">;</span><br/><br/><span class="hl-1"> </span><span class="hl-5">/**</span><br/><span class="hl-5"> * Override environment variables</span><br/><span class="hl-5"> * Useful for testing or multi-instance configurations</span><br/><span class="hl-5"> */</span><br/><span class="hl-1"> </span><span class="hl-4">envOverrides</span><span class="hl-1">?: </span><span class="hl-9">Record</span><span class="hl-1">&lt;</span><span class="hl-9">string</span><span class="hl-1">, </span><span class="hl-9">string</span><span class="hl-1">&gt;;</span><br/><span class="hl-1">}</span>
78
- </code><button type="button">Copy</button></pre>
79
-
80
- <h3 id="environment-variables" class="tsd-anchor-link">Environment Variables<a href="#environment-variables" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
81
- <p><strong>LLM Provider API Keys</strong> (at least one required):</p>
82
- <ul>
83
- <li><code>ANTHROPIC_API_KEY</code> - For Claude models</li>
84
- <li><code>OPENAI_API_KEY</code> - For GPT models</li>
85
- <li><code>GOOGLE_GENAI_API_KEY</code> - For Google AI (Gemini) models</li>
86
- <li><code>XAI_API_KEY</code> - For Grok models</li>
87
- <li><code>DEEPSEEK_API_KEY</code> - For DeepSeek models</li>
88
- <li><code>TOGETHER_API_KEY</code> - For Together AI models</li>
89
- <li><code>FAL_API_KEY</code> - For Fal image-generation models</li>
90
- </ul>
91
- <p><strong>Self-hosted providers</strong> (optional, count toward the &quot;at least one&quot; requirement):</p>
92
- <ul>
93
- <li><code>SELF_HOSTED_BASE_URL</code> - OpenAI-compatible inference server for the <code>SelfHosted</code> text provider (vLLM, Ollama, llama.cpp, LM Studio). Must include the <code>/v1</code> path, e.g. <code>http://gpu-box:8000/v1</code></li>
94
- <li><code>SELF_HOSTED_API_KEY</code> - Optional bearer token for the above</li>
95
- <li><code>COMFYUI_BASE_URL</code> - ComfyUI server for the <code>ComfyUI</code> image provider, no trailing slash, e.g. <code>http://gpu-box:8188</code> (see Example 9)</li>
96
- <li><code>COMFYUI_EXTRA_HEADERS</code> - Optional JSON object of headers sent on every ComfyUI request, e.g. <code>{&quot;Authorization&quot;:&quot;Basic ...&quot;}</code> for a reverse proxy</li>
97
- <li><code>COMFYUI_TIMEOUT_MS</code> - Total wait for a queued generation, default <code>600000</code> (covers queue wait and model cold start)</li>
98
- <li><code>COMFYUI_POLL_INTERVAL_MS</code> - Poll interval while waiting, default <code>2000</code></li>
99
- </ul>
100
- <p><strong>Optional:</strong></p>
101
- <ul>
102
- <li><code>STARTING_BALANCE</code> - Starting balance for new users (e.g., &quot;10.00&quot;)</li>
103
- <li><code>BRIYAH_DATA_PATH</code> - Global default data path (overridden by constructor <code>dataPath</code>)</li>
104
- <li><code>USER_SERVICE_CACHE_TIMEOUT_MINUTES</code> - Cache timeout (overridden by constructor option)</li>
105
- <li><code>ALLOW_USER_AUTHORED_TOOLS</code> - <code>&quot;true&quot;</code> to let users author/run their own tools (overridden by the <code>allowUserAuthoredTools</code> option). Default off.</li>
106
- </ul>
107
- <p><strong>Retired:</strong></p>
108
- <ul>
109
- <li><code>BRIYAH_TOOL_PACKS</code> - Read in 1.5 to choose which tool packs to register. It is not read in
110
- 2.0 and setting it does nothing. Every bundled pack's tools are registered at construction;
111
- what a user can reach is decided by pack enablement and per-agent tool grants. See <a href="#packs">Packs</a>.</li>
112
- </ul>
113
- <h3 id="logging" class="tsd-anchor-link">Logging<a href="#logging" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
114
- <p>Briyah writes logs to <code>{dataPath}/logs/briyah.log</code> by default. Configure or disable via the <code>logging</code> option:</p>
115
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({</span><br/><span class="hl-1"> </span><span class="hl-4">dataPath:</span><span class="hl-1"> </span><span class="hl-2">&#39;./my-data&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">logging:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">enabled:</span><span class="hl-1"> </span><span class="hl-6">true</span><span class="hl-1">, </span><span class="hl-5">// default</span><br/><span class="hl-1"> </span><span class="hl-4">logFile:</span><span class="hl-1"> </span><span class="hl-2">&#39;./my-data/logs/app.log&#39;</span><span class="hl-1">, </span><span class="hl-5">// default: {dataPath}/logs/briyah.log</span><br/><span class="hl-1"> </span><span class="hl-4">console:</span><span class="hl-1"> </span><span class="hl-6">false</span><span class="hl-1">, </span><span class="hl-5">// set true to also log to stdout/stderr</span><br/><span class="hl-1"> </span><span class="hl-4">level:</span><span class="hl-1"> </span><span class="hl-2">&#39;warn&#39;</span><span class="hl-1">, </span><span class="hl-5">// &#39;debug&#39; | &#39;log&#39; | &#39;warn&#39; | &#39;error&#39; — default: &#39;log&#39;</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-5">// Log to console only (no file):</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({ </span><span class="hl-4">logging:</span><span class="hl-1"> { </span><span class="hl-4">console:</span><span class="hl-1"> </span><span class="hl-6">true</span><span class="hl-1">, </span><span class="hl-4">logFile:</span><span class="hl-1"> </span><span class="hl-6">null</span><span class="hl-1"> } });</span><br/><br/><span class="hl-5">// Disable logging entirely:</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({ </span><span class="hl-4">logging:</span><span class="hl-1"> { </span><span class="hl-4">enabled:</span><span class="hl-1"> </span><span class="hl-6">false</span><span class="hl-1"> } });</span>
116
- </code><button type="button">Copy</button></pre>
117
-
118
- <h2 id="api-reference" class="tsd-anchor-link">API Reference<a href="#api-reference" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
119
- <h3 id="briyah-class" class="tsd-anchor-link">Briyah Class<a href="#briyah-class" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
120
- <h4 id="constructoroptions-briyahconfigoptions" class="tsd-anchor-link">constructor(options?: BriyahConfigOptions)<a href="#constructoroptions-briyahconfigoptions" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h4>
121
- <p>Creates a new Briyah SDK instance.</p>
122
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({</span><br/><span class="hl-1"> </span><span class="hl-4">dataPath:</span><span class="hl-1"> </span><span class="hl-2">&#39;./briyah-data&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">startingBalance:</span><span class="hl-1"> </span><span class="hl-8">10.00</span><br/><span class="hl-1">});</span>
123
- </code><button type="button">Copy</button></pre>
124
-
125
- <h4 id="async-init-promisevoid" class="tsd-anchor-link">async init(): Promise&lt;void&gt;<a href="#async-init-promisevoid" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h4>
126
- <p>Initializes the Briyah system. Must be called before using <code>getAppService()</code>.</p>
127
- <pre><code class="typescript"><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">init</span><span class="hl-1">();</span>
128
- </code><button type="button">Copy</button></pre>
129
-
130
- <h4 id="getappserviceuserid-string-appservice" class="tsd-anchor-link">getAppService(userId: string): AppService<a href="#getappserviceuserid-string-appservice" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h4>
131
- <p>Gets the AppService instance for a specific user. The AppService provides full access to:</p>
132
- <ul>
133
- <li>Agent management</li>
134
- <li>Room conversations</li>
135
- <li>Story generation</li>
136
- <li>Text processing</li>
137
- <li>File attachments</li>
138
- </ul>
139
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">appService</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-2">&#39;user-123&#39;</span><span class="hl-1">);</span>
140
- </code><button type="button">Copy</button></pre>
141
-
142
- <p><strong>Throws:</strong> Error if <code>init()</code> has not been called.</p>
143
- <h4 id="removeuserserviceuserid-string-void" class="tsd-anchor-link">removeUserService(userId: string): void<a href="#removeuserserviceuserid-string-void" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h4>
144
- <p>Manually removes a user's service from the cache. Useful for:</p>
145
- <ul>
146
- <li>Forcing a fresh AppService instance</li>
147
- <li>Cleaning up when a user logs out</li>
148
- </ul>
149
- <pre><code class="typescript"><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">removeUserService</span><span class="hl-1">(</span><span class="hl-2">&#39;user-123&#39;</span><span class="hl-1">);</span>
150
- </code><button type="button">Copy</button></pre>
151
-
152
- <h4 id="getcachestats" class="tsd-anchor-link">getCacheStats()<a href="#getcachestats" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h4>
153
- <p>Returns statistics about cached user services.</p>
154
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">stats</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getCacheStats</span><span class="hl-1">();</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-2">`Cached users: </span><span class="hl-6">${</span><span class="hl-4">stats</span><span class="hl-10">.</span><span class="hl-4">cachedUsers</span><span class="hl-6">}</span><span class="hl-2">`</span><span class="hl-1">);</span>
155
- </code><button type="button">Copy</button></pre>
156
-
157
- <h4 id="async-shutdown-promisevoid" class="tsd-anchor-link">async shutdown(): Promise&lt;void&gt;<a href="#async-shutdown-promisevoid" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h4>
158
- <p>Shuts down the Briyah system and cleans up resources.</p>
159
- <pre><code class="typescript"><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">shutdown</span><span class="hl-1">();</span>
160
- </code><button type="button">Copy</button></pre>
161
-
162
- <h4 id="isinitialized-boolean" class="tsd-anchor-link">isInitialized(): boolean<a href="#isinitialized-boolean" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h4>
163
- <p>Checks if Briyah has been initialized.</p>
164
- <pre><code class="typescript"><span class="hl-3">if</span><span class="hl-1"> (</span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">isInitialized</span><span class="hl-1">()) {</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">appService</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-2">&#39;user-123&#39;</span><span class="hl-1">);</span><br/><span class="hl-1">}</span>
165
- </code><button type="button">Copy</button></pre>
166
-
167
- <h3 id="appservice" class="tsd-anchor-link">AppService<a href="#appservice" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
168
- <p>The AppService provides full access to Briyah functionality.</p>
169
- <p><strong>Common operations:</strong></p>
170
- <pre><code class="typescript"><span class="hl-5">// Agent Management</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">agent</span><span class="hl-1"> = </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createAgent</span><span class="hl-1">(</span><span class="hl-4">provider</span><span class="hl-1">, </span><span class="hl-4">name</span><span class="hl-1">, </span><span class="hl-4">nickname</span><span class="hl-1">, </span><span class="hl-4">description</span><span class="hl-1">, </span><span class="hl-4">model</span><span class="hl-1">);</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">agents</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">listAgents</span><span class="hl-1">();</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">deleteAgent</span><span class="hl-1">(</span><span class="hl-4">agentId</span><span class="hl-1">);</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">reloadAgent</span><span class="hl-1">(</span><span class="hl-4">agentId</span><span class="hl-1">); </span><span class="hl-5">// Clear conversation history (keeps any prior summary)</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">compactAgentConversation</span><span class="hl-1">(</span><span class="hl-4">agentId</span><span class="hl-1">); </span><span class="hl-5">// Summarize history into a single message</span><br/><br/><span class="hl-5">// Room Management</span><br/><span class="hl-5">// roomLeader answers anything the room cannot place; turnMode &#39;directed&#39; asks only the</span><br/><span class="hl-5">// agent a message names (&#39;broadcast&#39;, the default, asks every agent every time);</span><br/><span class="hl-5">// moderator is only needed when no agent leads the turn order.</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">room</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createRoom</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-4">name</span><span class="hl-1">, </span><span class="hl-4">goal</span><span class="hl-1">, </span><span class="hl-4">agentIds</span><span class="hl-1">, </span><span class="hl-4">roomLeader</span><span class="hl-1">?, </span><span class="hl-4">turnMode</span><span class="hl-1">?, </span><span class="hl-4">moderator</span><span class="hl-1">?,</span><br/><span class="hl-1">);</span><br/><span class="hl-4">const</span><span class="hl-1"> </span><span class="hl-4">rooms</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">listRooms</span><span class="hl-1">();</span><br/><span class="hl-4">const</span><span class="hl-1"> </span><span class="hl-4">details</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">getRoomDetails</span><span class="hl-1">(</span><span class="hl-4">roomId</span><span class="hl-1">);</span><br/><span class="hl-5">// action defaults to &#39;moderate&#39;. Returns {accepted, reason} — a message sent out of</span><br/><span class="hl-5">// turn is refused, not thrown. `sender` must be an agentNickname.</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">sendRoomMessage</span><span class="hl-1">(</span><span class="hl-4">roomId</span><span class="hl-1">, </span><span class="hl-4">content</span><span class="hl-1">, </span><span class="hl-4">sender</span><span class="hl-1">, </span><span class="hl-4">action</span><span class="hl-1">?, </span><span class="hl-4">targets</span><span class="hl-1">?);</span><br/><span class="hl-5">// Required before subscribing to a room this process did not create.</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">ensureRoomStateCallback</span><span class="hl-1">(</span><span class="hl-4">roomId</span><span class="hl-1">);</span><br/><span class="hl-4">const</span><span class="hl-1"> </span><span class="hl-4">emitter</span><span class="hl-1"> = </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">getRoomMessageEmitter</span><span class="hl-1">(</span><span class="hl-4">roomId</span><span class="hl-1">); </span><span class="hl-5">// &#39;update&#39; events</span><br/><br/><span class="hl-5">// Prompt folder defaults — the role&#39;s name, tools and documents, as the create-agent</span><br/><span class="hl-5">// dialog would fill them in. Read these rather than hard-coding a pack&#39;s tool names.</span><br/><span class="hl-5">// `exists` is false for a folder nothing ships, which otherwise looks identical to a</span><br/><span class="hl-5">// folder that declares no defaults.</span><br/><span class="hl-4">const</span><span class="hl-1"> { </span><span class="hl-4">defaults</span><span class="hl-1">, </span><span class="hl-4">exists</span><span class="hl-1"> } = </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">getPromptFolderDefaults</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e:dungeon_master&#39;</span><span class="hl-1">);</span><br/><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">promptFolderExists</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e:dungeon_master&#39;</span><span class="hl-1">); </span><span class="hl-5">// the same question, alone</span><br/><br/><span class="hl-5">// Or let the folder build the agent: applies those defaults, saves, and attaches the</span><br/><span class="hl-5">// folder&#39;s documents. Async (attaching is), unlike createAgent. `overrides` takes the</span><br/><span class="hl-5">// same shape as the defaults; pass `attachDocuments: false` to decline the documents.</span><br/><span class="hl-4">const</span><span class="hl-1"> </span><span class="hl-4">agent</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createAgentFromFolder</span><span class="hl-1">({</span><br/><span class="hl-1"> </span><span class="hl-4">folder:</span><span class="hl-1"> </span><span class="hl-2">&#39;dnd5e:dungeon_master&#39;</span><span class="hl-1">, </span><span class="hl-4">aiServiceName:</span><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-4">aiModel:</span><span class="hl-1"> </span><span class="hl-2">&#39;claude-sonnet-5&#39;</span><span class="hl-1">,</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-5">// Agents that belong to a room: created inside it, named after it, deleted with it,</span><br/><span class="hl-5">// and left out of listAgents(). agentName is the bare name; the room&#39;s is prefixed on.</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createRoomAgent</span><span class="hl-1">(</span><span class="hl-4">roomId</span><span class="hl-1">, { </span><span class="hl-4">aiServiceName</span><span class="hl-1">, </span><span class="hl-4">agentName</span><span class="hl-1">, </span><span class="hl-4">description</span><span class="hl-1">, </span><span class="hl-4">aiModel</span><span class="hl-1">, </span><span class="hl-4">promptName</span><span class="hl-1"> });</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">removeRoomAgent</span><span class="hl-1">(</span><span class="hl-4">roomId</span><span class="hl-1">, </span><span class="hl-4">agentId</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// Native tools (recommended for SDK integrations — see Example 8)</span><br/><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">registerTool</span><span class="hl-1">({ </span><span class="hl-4">name</span><span class="hl-1">, </span><span class="hl-4">description</span><span class="hl-1">, </span><span class="hl-4">parameters</span><span class="hl-1">, </span><span class="hl-4">handler</span><span class="hl-1"> }); </span><span class="hl-5">// process-level</span><br/><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">listNativeTools</span><span class="hl-1">();</span><br/><br/><span class="hl-5">// User-authored (string-code) tools — only when allowUserAuthoredTools is enabled</span><br/><span class="hl-4">const</span><span class="hl-1"> </span><span class="hl-4">tools</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">listTools</span><span class="hl-1">();</span><br/><span class="hl-4">const</span><span class="hl-1"> </span><span class="hl-4">tool</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createTool</span><span class="hl-1">({ </span><span class="hl-4">name</span><span class="hl-1">, </span><span class="hl-4">description</span><span class="hl-1">, </span><span class="hl-4">parameters</span><span class="hl-1">, </span><span class="hl-4">code</span><span class="hl-1">, timeoutMs? });</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">updateTool</span><span class="hl-1">(</span><span class="hl-4">name</span><span class="hl-1">, </span><span class="hl-4">tool</span><span class="hl-1">);</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">deleteTool</span><span class="hl-1">(</span><span class="hl-4">name</span><span class="hl-1">);</span><br/><span class="hl-4">const</span><span class="hl-1"> </span><span class="hl-4">result</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">testTool</span><span class="hl-1">(</span><span class="hl-4">name</span><span class="hl-1">, </span><span class="hl-4">args</span><span class="hl-1">); </span><span class="hl-5">// { ok: true, result } | { ok: false, error }</span><br/><br/><span class="hl-5">// Story Management</span><br/><span class="hl-5">// illustrateStory is required and positional — it is the fifth argument, not an option.</span><br/><span class="hl-4">const</span><span class="hl-1"> </span><span class="hl-4">story</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createStory</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-4">name</span><span class="hl-1">, </span><span class="hl-4">idea</span><span class="hl-1">, </span><span class="hl-4">userCharacterDesc</span><span class="hl-1">, </span><span class="hl-4">otherCharactersDesc</span><span class="hl-1">, </span><span class="hl-4">illustrateStory</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">storyModel</span><span class="hl-1">?, </span><span class="hl-4">isImport</span><span class="hl-1">?, </span><span class="hl-4">imageModelName</span><span class="hl-1">?, </span><span class="hl-4">skipDetailedPlot</span><span class="hl-1">?,</span><br/><span class="hl-1">);</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">progressStory</span><span class="hl-1">(</span><span class="hl-4">storyId</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// Text Processing</span><br/><span class="hl-4">const</span><span class="hl-1"> </span><span class="hl-4">result</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">processText</span><span class="hl-1">(</span><span class="hl-4">agentId</span><span class="hl-1">, </span><span class="hl-4">text</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// Image Generation (image-capable providers; called on the Agent object — see Example 9)</span><br/><span class="hl-4">const</span><span class="hl-1"> { </span><span class="hl-4">artifactId</span><span class="hl-1"> } = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">agent</span><span class="hl-1">.</span><span class="hl-0">generateImage</span><span class="hl-1">(</span><span class="hl-4">prompt</span><span class="hl-1">, </span><span class="hl-4">imageProperties</span><span class="hl-1">);</span><br/><span class="hl-4">const</span><span class="hl-1"> </span><span class="hl-4">edited</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">agent</span><span class="hl-1">.</span><span class="hl-0">editImage</span><span class="hl-1">(</span><span class="hl-4">prompt</span><span class="hl-1">, </span><span class="hl-4">imageProperties</span><span class="hl-1">, </span><span class="hl-4">referenceArtifactIds</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// File Attachments</span><br/><span class="hl-4">const</span><span class="hl-1"> </span><span class="hl-4">attachment</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">attachDocument</span><span class="hl-1">(</span><span class="hl-4">agentId</span><span class="hl-1">, </span><span class="hl-4">fileName</span><span class="hl-1">, </span><span class="hl-4">fileData</span><span class="hl-1">);</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">deleteAttachedFile</span><span class="hl-1">(</span><span class="hl-4">agentId</span><span class="hl-1">, </span><span class="hl-4">fileName</span><span class="hl-1">); </span><span class="hl-5">// by filename</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">deleteAttachedFileById</span><span class="hl-1">(</span><span class="hl-4">documentId</span><span class="hl-1">); </span><span class="hl-5">// by document ID</span>
171
- </code><button type="button">Copy</button></pre>
172
-
173
- <h2 id="prompt-folders" class="tsd-anchor-link">Prompt Folders<a href="#prompt-folders" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
174
- <p>An agent's behaviour comes from a <strong>prompt folder</strong>, not from its description. The folder
175
- holds the templates the room renders on every turn — most importantly <code>perceive.prompt</code>, which
176
- tells the agent what it is and what it may do, and <code>perceive.json</code>, the JSON schema its replies
177
- must satisfy. Two agents on the same model with the same description behave completely
178
- differently if they were built from different folders.</p>
179
- <p><code>init()</code> seeds the shared folders into <code>{dataPath}/common/prompts/</code>, so they are on disk before
180
- your first <code>createAgent</code> call. Existing files are preserved on upgrade, so anything you edit
181
- survives.</p>
182
- <p>A folder can also come from a <strong>pack</strong> — a domain's prompts, documents, tools and templates in
183
- one directory. A pack's folders are referenced <strong>pack-qualified</strong>, and they are only listed once
184
- the user has that pack enabled:</p>
185
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">appService</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-2">&#39;user-123&#39;</span><span class="hl-1">);</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">listPromptFolders</span><span class="hl-1">());</span><br/><span class="hl-5">// default, narrator, character, illustrator, pdf_converter, room_moderator, ...</span><br/><br/><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-4">packs</span><span class="hl-1">.</span><span class="hl-0">setEnabled</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e&#39;</span><span class="hl-1">, </span><span class="hl-6">true</span><span class="hl-1">);</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">listPromptFolders</span><span class="hl-1">());</span><br/><span class="hl-5">// ... plus dnd5e:dungeon_master, dnd5e:dnd_player, dnd5e:character_creator,</span><br/><span class="hl-5">// dnd5e:campaign_designer, dnd5e:battle_master, dnd5e:dnd_room_moderator</span>
186
- </code><button type="button">Copy</button></pre>
187
-
188
- <p>See <a href="#packs">Packs</a> — <strong>if you are building on <code>dnd5e</code> you have to enable it first</strong>, or a bare
189
- folder name will not reach it.</p>
190
- <h3 id="when-a-folder-name-is-wrong" class="tsd-anchor-link">When a folder name is wrong<a href="#when-a-folder-name-is-wrong" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
191
- <p>A prompt lookup always ends at the shared root prompts, so a folder that does not exist does not
192
- fail — it produces a working, generic agent. Three things now say so instead:</p>
193
- <pre><code class="typescript"><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">promptFolderExists</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e:dungeon_master&#39;</span><span class="hl-1">); </span><span class="hl-5">// false if the pack is not installed</span><br/><span class="hl-6">const</span><span class="hl-1"> { </span><span class="hl-7">defaults</span><span class="hl-1">, </span><span class="hl-7">exists</span><span class="hl-1"> } = </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">getPromptFolderDefaults</span><span class="hl-1">(</span><span class="hl-2">&#39;dungeon_master&#39;</span><span class="hl-1">);</span><br/><span class="hl-5">// exists distinguishes &quot;no such folder&quot; from &quot;a folder that declares no defaults&quot;:</span><br/><span class="hl-5">// both give you `defaults: {}`. A bare pack folder reports false until the pack is enabled.</span>
194
- </code><button type="button">Copy</button></pre>
195
-
196
- <ul>
197
- <li><code>createAgentFromFolder</code> and <code>createRoomAgentFromFolder</code> <strong>throw</strong> a <code>NotFoundError</code> naming the
198
- folder. The whole point of those calls is the folder's recipe — its tools, its documents, its
199
- nickname — and a folder that is not there has none.</li>
200
- <li><code>createAgent</code> <strong>warns</strong> and carries on, since it takes all of that from its arguments and the
201
- folder is only one of them. The warning is in <code>{dataPath}/logs/briyah.log</code>.</li>
202
- </ul>
203
- <p><strong>The folder is the sixth argument to <code>createAgent</code>, and it defaults to <code>'default'</code></strong> — a
204
- generic conversational agent. Passing a role folder is what makes the agent that role:</p>
205
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">dm</span><span class="hl-1"> = </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createAgent</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Dungeon Master&#39;</span><span class="hl-1">, </span><span class="hl-5">// agentName — a label; used in logs and the UI</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;DM&#39;</span><span class="hl-1">, </span><span class="hl-5">// agentNickname — the name the room routes by</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Runs the game.&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;claude-sonnet-5&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;dnd5e:dungeon_master&#39;</span><span class="hl-1">, </span><span class="hl-5">// &lt;- the prompt folder</span><br/><span class="hl-1">);</span>
206
- </code><button type="button">Copy</button></pre>
207
-
208
- <p><code>agentNickname</code> is the identity: messages are addressed to it, artifact viewers are lists of
209
- it, and it is what an agent writes into <code>targets</code>. <code>agentName</code> is a label. Give a character the
210
- nickname you want it called by.</p>
211
- <p>To write your own folder, copy a bundled one out of <code>{dataPath}/common/prompts/</code> (or a pack's
212
- <code>{dataPath}/packs/&lt;pack&gt;/prompts/</code>), rename the directory, and edit it — a folder in the user data
213
- directory takes precedence over the common one of the same name. Editing a <em>pack's</em> file instead
214
- writes a private copy under <code>{dataPath}/user/&lt;userId&gt;/packs/&lt;pack&gt;/</code>, so one user's change does
215
- not reach the others.</p>
216
- <h2 id="packs" class="tsd-anchor-link">Packs<a href="#packs" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
217
- <p>A <strong>pack</strong> is one domain's prompt folders, documents, native tools, and agent and room templates
218
- in a single directory. <code>dnd5e</code> is the one that ships: eighteen tools for dice, character sheets,
219
- the rules engine and a bestiary, six prompt folders, sixteen rules documents, and a room template
220
- that builds a whole table.</p>
221
- <p>It exists so a domain arrives as a unit — and so <strong>your</strong> prompts live in your own directory
222
- rather than beside Briyah's. Install one by copying a directory into <code>{dataPath}/packs/</code>; there is
223
- no import mechanism and nothing to register.</p>
224
- <pre><code><span class="hl-1">{</span><span class="hl-4">dataPath</span><span class="hl-1">}/</span><span class="hl-4">packs</span><span class="hl-1">/&lt;</span><span class="hl-4">name</span><span class="hl-1">&gt;/</span><br/><span class="hl-1">├── </span><span class="hl-4">pack</span><span class="hl-1">.</span><span class="hl-4">json</span><span class="hl-1"> </span><span class="hl-11">optional</span><span class="hl-1">: </span><span class="hl-4">title</span><span class="hl-1">, </span><span class="hl-4">description</span><span class="hl-1">, </span><span class="hl-4">version</span><br/><span class="hl-1">├── </span><span class="hl-4">prompts</span><span class="hl-1">/ </span><span class="hl-4">folders</span><span class="hl-1">, </span><span class="hl-4">referenced</span><span class="hl-1"> </span><span class="hl-3">as</span><span class="hl-1"> &lt;</span><span class="hl-9">name</span><span class="hl-1">&gt;:&lt;</span><span class="hl-9">folder</span><span class="hl-1">&gt;</span><br/><span class="hl-1">├── </span><span class="hl-4">artifacts</span><span class="hl-1">/ </span><span class="hl-4">manifest</span><span class="hl-1">.</span><span class="hl-4">json</span><span class="hl-1"> + .</span><span class="hl-4">md</span><span class="hl-1"> </span><span class="hl-4">documents</span><br/><span class="hl-1">├── </span><span class="hl-4">agents</span><span class="hl-1">/ </span><span class="hl-4">agent</span><span class="hl-1"> </span><span class="hl-4">templates</span><br/><span class="hl-1">└── </span><span class="hl-4">rooms</span><span class="hl-1">/ </span><span class="hl-4">room</span><span class="hl-1"> </span><span class="hl-4">templates</span>
225
- </code><button>Copy</button></pre>
226
-
227
- <h3 id="installed-for-everyone-enabled-per-user" class="tsd-anchor-link">Installed for everyone, enabled per user<a href="#installed-for-everyone-enabled-per-user" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
228
- <p>Tools register process-wide when you construct <code>Briyah</code>. Enabling is <strong>per user</strong>, and it is what
229
- makes a pack's prompt folders resolve by bare name, its documents appear in <code>listUserArtifacts()</code>,
230
- and its tools grantable:</p>
231
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">appService</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-2">&#39;user-123&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-4">packs</span><span class="hl-1">.</span><span class="hl-0">listPacks</span><span class="hl-1">();</span><br/><span class="hl-5">// [{</span><br/><span class="hl-5">// name: &#39;dnd5e&#39;, // &lt;- the id; `title` is for display</span><br/><span class="hl-5">// title: &#39;D&amp;D 5th Edition&#39;,</span><br/><span class="hl-5">// description: &#39;Run a fifth-edition tabletop game in a room: ...&#39;,</span><br/><span class="hl-5">// version: &#39;1.0.0&#39;,</span><br/><span class="hl-5">// dir: &#39;/srv/app/briyah-data/packs/dnd5e&#39;,</span><br/><span class="hl-5">// origin: &#39;host&#39;, // &#39;host&#39; installed it, or &#39;user&#39; created it</span><br/><span class="hl-5">// enabled: false,</span><br/><span class="hl-5">// promptFolders: [&#39;battle_master&#39;, &#39;campaign_designer&#39;, &#39;character_creator&#39;,</span><br/><span class="hl-5">// &#39;dnd_player&#39;, &#39;dnd_room_moderator&#39;, &#39;dungeon_master&#39;],</span><br/><span class="hl-5">// agents: [{ key: &#39;dungeon_master&#39;, promptFolder: &#39;dungeon_master&#39;,</span><br/><span class="hl-5">// label: &#39;Dungeon Master&#39;, modelRole: &#39;primary&#39; }, ...],</span><br/><span class="hl-5">// rooms: [{ key: &#39;table&#39;, name: &#39;D&amp;D Table&#39;, goal: &#39;...&#39;, turnMode: &#39;directed&#39;,</span><br/><span class="hl-5">// agents: [{ key: &#39;table_moderator&#39; }, ...],</span><br/><span class="hl-5">// roomLeader: &#39;dungeon_master&#39;, moderator: &#39;table_moderator&#39; }],</span><br/><span class="hl-5">// documentCount: 16,</span><br/><span class="hl-5">// requiresTools: [&#39;apply_hp&#39;, &#39;check&#39;, &#39;roll_dice&#39;, ...], // derived from its grants</span><br/><span class="hl-5">// requiresPacks: [],</span><br/><span class="hl-5">// missingTools: [], // requiresTools this process has not registered</span><br/><span class="hl-5">// missingPacks: [], // requiresPacks this user has not enabled</span><br/><span class="hl-5">// }]</span><br/><span class="hl-5">// The REST API returns a trimmed version of this (`pack` instead of `name`, and a</span><br/><span class="hl-5">// `counts` object) — `listPacks()` in-process returns the `PackView` above.</span><br/><br/><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-4">packs</span><span class="hl-1">.</span><span class="hl-0">setEnabled</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e&#39;</span><span class="hl-1">, </span><span class="hl-6">true</span><span class="hl-1">);</span>
232
- </code><button type="button">Copy</button></pre>
233
-
234
- <p><strong>Do this before building anything on the pack.</strong> A pack-qualified folder
235
- (<code>dnd5e:dungeon_master</code>) always resolves, but a bare <code>dungeon_master</code> only reaches the pack when
236
- it is enabled — otherwise it falls back to the shared root prompts and you get a generic
237
- conversational agent with no error to tell you.</p>
238
- <p>Disabling later <strong>hides</strong> rather than switches off: the pack leaves the listings, but a room that
239
- already exists keeps working, an attached document still loads, and a granted tool still runs.</p>
240
- <p>If you would rather filter or wrap the tool definitions than take them all:</p>
241
- <pre><code class="typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">dnd5eTools</span><span class="hl-1">, </span><span class="hl-4">availableToolPacks</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;briyah&#39;</span><span class="hl-1">;</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({ </span><span class="hl-4">tools:</span><span class="hl-1"> </span><span class="hl-0">dnd5eTools</span><span class="hl-1">() });</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-0">availableToolPacks</span><span class="hl-1">()); </span><span class="hl-5">// [&#39;dnd5e&#39;]</span>
242
- </code><button type="button">Copy</button></pre>
243
-
244
- <h3 id="building-a-room-from-a-template" class="tsd-anchor-link">Building a room from a template<a href="#building-a-room-from-a-template" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
245
- <p>The fastest path to a working table, and the one that gets the details right — the moderator is
246
- created before the agents that need one, the leader and moderator are resolved to the nicknames
247
- the created agents actually ended up with, and a failure rolls the whole room back:</p>
248
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> { </span><span class="hl-7">roomId</span><span class="hl-1">, </span><span class="hl-7">agentIds</span><span class="hl-1">, </span><span class="hl-7">warnings</span><span class="hl-1"> } = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-4">packInstantiation</span><span class="hl-1">.</span><span class="hl-0">instantiateRoom</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;dnd5e&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;table&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">primary:</span><span class="hl-1"> { </span><span class="hl-4">aiServiceName:</span><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-4">aiModel:</span><span class="hl-1"> </span><span class="hl-2">&#39;claude-sonnet-5&#39;</span><span class="hl-1"> },</span><br/><span class="hl-1"> </span><span class="hl-4">economy:</span><span class="hl-1"> { </span><span class="hl-4">aiServiceName:</span><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-4">aiModel:</span><span class="hl-1"> </span><span class="hl-2">&#39;claude-haiku-4-5-20251001&#39;</span><span class="hl-1"> },</span><br/><span class="hl-1"> },</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Tuesday Night Game&#39;</span><span class="hl-1">, </span><span class="hl-5">// optional; auto-suffixed if the name is taken</span><br/><span class="hl-1"> </span><span class="hl-5">// optional; per-agent overrides, keyed by the template&#39;s own key for the seat</span><br/><span class="hl-1"> { </span><span class="hl-4">player:</span><span class="hl-1"> { </span><span class="hl-4">agentName:</span><span class="hl-1"> </span><span class="hl-2">&#39;Homer&#39;</span><span class="hl-1">, </span><span class="hl-4">agentNickname:</span><span class="hl-1"> </span><span class="hl-2">&#39;Homer&#39;</span><span class="hl-1"> } },</span><br/><span class="hl-1">);</span>
249
- </code><button type="button">Copy</button></pre>
250
-
251
- <p>That is a six-agent room with <code>turnMode: 'directed'</code>, the Dungeon Master as leader, a table
252
- moderator, and every agent already carrying its tool grants and rules documents.</p>
253
- <p><strong>Naming the player.</strong> The template's player seat ships as <code>PlayerOne</code>, which is not what the
254
- person at the keyboard is called. The fifth argument changes any seat: the keys are the room
255
- template's <code>agents[].key</code> (<code>table_moderator</code>, <code>dungeon_master</code>, <code>battle_master</code>,
256
- <code>character_creator</code>, <code>campaign_designer</code>, <code>player</code> for <code>dnd5e:table</code>), and the value is the same
257
- <code>PromptFolderDefaults</code> shape as a folder's own defaults, applied over them. A key no seat has is
258
- refused before the room is created, rather than quietly doing nothing.</p>
259
- <p><strong>More than one player.</strong> The template seats one. Add the others afterwards with
260
- <code>createRoomAgentFromFolder</code>, which is the same call instantiation makes per agent — a room-owned
261
- agent, named after the room, deleted with it:</p>
262
- <pre><code class="typescript"><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createRoomAgentFromFolder</span><span class="hl-1">(</span><span class="hl-4">roomId</span><span class="hl-1">, {</span><br/><span class="hl-1"> </span><span class="hl-4">folder:</span><span class="hl-1"> </span><span class="hl-2">&#39;dnd5e:dnd_player&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">aiServiceName:</span><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">aiModel:</span><span class="hl-1"> </span><span class="hl-2">&#39;claude-haiku-4-5-20251001&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">overrides:</span><span class="hl-1"> { </span><span class="hl-4">agentName:</span><span class="hl-1"> </span><span class="hl-2">&#39;Vex&#39;</span><span class="hl-1">, </span><span class="hl-4">agentNickname:</span><span class="hl-1"> </span><span class="hl-2">&#39;Vex&#39;</span><span class="hl-1">, </span><span class="hl-4">description:</span><span class="hl-1"> </span><span class="hl-2">&#39;A wary scout.&#39;</span><span class="hl-1"> },</span><br/><span class="hl-1"> </span><span class="hl-4">pack:</span><span class="hl-1"> </span><span class="hl-2">&#39;dnd5e&#39;</span><span class="hl-1">, </span><span class="hl-5">// files it under the pack, so it hides and lists with one</span><br/><span class="hl-1"> </span><span class="hl-4">packAgentKey:</span><span class="hl-1"> </span><span class="hl-2">&#39;player&#39;</span><span class="hl-1">, </span><span class="hl-5">// records which template it came from</span><br/><span class="hl-1">});</span>
263
- </code><button type="button">Copy</button></pre>
264
-
265
- <p>Do it after <code>instantiateRoom</code> returns, not during: the room already has its moderator by then, so
266
- a player carrying <code>useModerator: true</code> has one to route through.</p>
267
- <p>A template names a <strong>role</strong> rather than a model — <code>primary</code> or <code>economy</code> — because a model id goes
268
- stale and assumes your provider keys. The Dungeon Master takes the primary; the agents that do
269
- bookkeeping rather than prose take the economy one. Omit <code>economy</code> and everything uses the
270
- primary. <code>warnings</code> reports anything that did not go to plan without stopping the room, such as a
271
- document that failed to attach; it is worth logging, because a missing document is a capability
272
- the agent appears to have and does not.</p>
273
- <p>For a single agent, with no room:</p>
274
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> { </span><span class="hl-7">agentId</span><span class="hl-1"> } = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-4">packInstantiation</span><span class="hl-1">.</span><span class="hl-0">instantiateAgent</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;dnd5e&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;character_creator&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> { </span><span class="hl-4">primary:</span><span class="hl-1"> { </span><span class="hl-4">aiServiceName:</span><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-4">aiModel:</span><span class="hl-1"> </span><span class="hl-2">&#39;claude-sonnet-5&#39;</span><span class="hl-1"> } },</span><br/><span class="hl-1">);</span>
275
- </code><button type="button">Copy</button></pre>
276
-
277
- <h3 id="building-the-same-room-by-hand" class="tsd-anchor-link">Building the same room by hand<a href="#building-the-same-room-by-hand" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
278
- <p>If you want the pieces rather than the template, registering a pack makes its tools <em>grantable</em>,
279
- not granted. An agent may only run the ones named in its <code>toolNames</code>:</p>
280
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">dm</span><span class="hl-1"> = </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createAgent</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Dungeon Master&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;DM&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Runs the game.&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;claude-sonnet-5&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;dnd5e:dungeon_master&#39;</span><span class="hl-1">, </span><span class="hl-5">// &lt;- pack-qualified</span><br/><span class="hl-1"> </span><span class="hl-6">undefined</span><span class="hl-1">, </span><span class="hl-6">null</span><span class="hl-1">, </span><span class="hl-8">0</span><span class="hl-1">, </span><span class="hl-2">&#39;&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> { </span><span class="hl-4">toolNames:</span><span class="hl-1"> [</span><span class="hl-2">&#39;roll_dice&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;check&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;apply_hp&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;stat_block&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;read_sheet&#39;</span><span class="hl-1">] },</span><br/><span class="hl-1">);</span>
281
- </code><button type="button">Copy</button></pre>
282
-
283
- <p>A pack's <strong>documents</strong> are read-only rules text an agent carries in every call. They live in
284
- <code>{dataPath}/packs/&lt;pack&gt;/artifacts/</code>, appear in <code>listUserArtifacts()</code> once the pack is enabled,
285
- and attach by an id that never changes:</p>
286
- <pre><code class="typescript"><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">attachArtifactToAgent</span><span class="hl-1">(</span><span class="hl-4">dm</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">, </span><span class="hl-2">&#39;pack:dnd5e.dungeon_master-procedure&#39;</span><span class="hl-1">);</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">attachArtifactToAgent</span><span class="hl-1">(</span><span class="hl-4">player</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">, </span><span class="hl-2">&#39;pack:dnd5e.class-bard&#39;</span><span class="hl-1">);</span>
287
- </code><button type="button">Copy</button></pre>
288
-
289
- <p>A document sits in that agent's cached prompt prefix on every call, so attach what it needs and
290
- nothing more. <code>src/tools/packs/dnd5e/README.md</code> in this package lists all eighteen tools and which
291
- agent each document belongs to.</p>
292
- <h2 id="usage-examples" class="tsd-anchor-link">Usage Examples<a href="#usage-examples" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
293
- <h3 id="example-1-multi-user-setup" class="tsd-anchor-link">Example 1: Multi-User Setup<a href="#example-1-multi-user-setup" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
294
- <pre><code class="typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">Briyah</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;briyah&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-5">// Initialize once at application startup</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({</span><br/><span class="hl-1"> </span><span class="hl-4">dataPath:</span><span class="hl-1"> </span><span class="hl-2">&#39;./data/briyah&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">startingBalance:</span><span class="hl-1"> </span><span class="hl-8">10.00</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">init</span><span class="hl-1">();</span><br/><br/><span class="hl-5">// In your route handler or service</span><br/><span class="hl-6">async</span><span class="hl-1"> </span><span class="hl-6">function</span><span class="hl-1"> </span><span class="hl-0">handleUserRequest</span><span class="hl-1">(</span><span class="hl-4">userId</span><span class="hl-1">: </span><span class="hl-9">string</span><span class="hl-1">, </span><span class="hl-4">message</span><span class="hl-1">: </span><span class="hl-9">string</span><span class="hl-1">) {</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">appService</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-4">userId</span><span class="hl-1">);</span><br/><br/><span class="hl-1"> </span><span class="hl-5">// Each user gets their own isolated AppService</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">agents</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">listAgents</span><span class="hl-1">();</span><br/><br/><span class="hl-1"> </span><span class="hl-5">// ... process user request</span><br/><span class="hl-1">}</span><br/><br/><span class="hl-5">// Cleanup at application shutdown</span><br/><span class="hl-4">process</span><span class="hl-1">.</span><span class="hl-0">on</span><span class="hl-1">(</span><span class="hl-2">&#39;SIGINT&#39;</span><span class="hl-1">, </span><span class="hl-6">async</span><span class="hl-1"> () </span><span class="hl-6">=&gt;</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">shutdown</span><span class="hl-1">();</span><br/><span class="hl-1"> </span><span class="hl-4">process</span><span class="hl-1">.</span><span class="hl-0">exit</span><span class="hl-1">(</span><span class="hl-8">0</span><span class="hl-1">);</span><br/><span class="hl-1">});</span>
295
- </code><button type="button">Copy</button></pre>
296
-
297
- <h3 id="example-2-custom-data-path" class="tsd-anchor-link">Example 2: Custom Data Path<a href="#example-2-custom-data-path" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
298
- <pre><code class="typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">Briyah</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;briyah&#39;</span><span class="hl-1">;</span><br/><span class="hl-3">import</span><span class="hl-1"> </span><span class="hl-4">path</span><span class="hl-1"> </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;path&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({</span><br/><span class="hl-1"> </span><span class="hl-4">dataPath:</span><span class="hl-1"> </span><span class="hl-4">path</span><span class="hl-1">.</span><span class="hl-0">join</span><span class="hl-1">(</span><span class="hl-4">__dirname</span><span class="hl-1">, </span><span class="hl-2">&#39;..&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;user-data&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;briyah&#39;</span><span class="hl-1">),</span><br/><span class="hl-1"> </span><span class="hl-4">userServiceCacheTimeoutMinutes:</span><span class="hl-1"> </span><span class="hl-8">60</span><span class="hl-1"> </span><span class="hl-5">// 1 hour cache</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">init</span><span class="hl-1">();</span>
299
- </code><button type="button">Copy</button></pre>
300
-
301
- <h3 id="example-3-testing-configuration" class="tsd-anchor-link">Example 3: Testing Configuration<a href="#example-3-testing-configuration" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
302
- <pre><code class="typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">Briyah</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;briyah&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">testBriyah</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({</span><br/><span class="hl-1"> </span><span class="hl-4">dataPath:</span><span class="hl-1"> </span><span class="hl-2">&#39;./test-data&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">startingBalance:</span><span class="hl-1"> </span><span class="hl-8">100.00</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">userServiceCacheTimeoutMinutes:</span><span class="hl-1"> </span><span class="hl-8">5</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">envOverrides:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">ANTHROPIC_API_KEY:</span><span class="hl-1"> </span><span class="hl-4">process</span><span class="hl-1">.</span><span class="hl-4">env</span><span class="hl-1">.</span><span class="hl-7">TEST_ANTHROPIC_KEY</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">OPENAI_API_KEY:</span><span class="hl-1"> </span><span class="hl-4">process</span><span class="hl-1">.</span><span class="hl-4">env</span><span class="hl-1">.</span><span class="hl-7">TEST_OPENAI_KEY</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">testBriyah</span><span class="hl-1">.</span><span class="hl-0">init</span><span class="hl-1">();</span><br/><br/><span class="hl-5">// Run tests...</span><br/><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">testBriyah</span><span class="hl-1">.</span><span class="hl-0">shutdown</span><span class="hl-1">();</span>
303
- </code><button type="button">Copy</button></pre>
304
-
305
- <h3 id="example-4-agent-creation-and-text-processing" class="tsd-anchor-link">Example 4: Agent Creation and Text Processing<a href="#example-4-agent-creation-and-text-processing" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
306
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">appService</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-2">&#39;user-123&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// Create an agent</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">agent</span><span class="hl-1"> = </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createAgent</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-5">// provider</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;AI Assistant&#39;</span><span class="hl-1">, </span><span class="hl-5">// name</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;James&#39;</span><span class="hl-1">, </span><span class="hl-5">// nickname</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;A helpful AI assistant&#39;</span><span class="hl-1">, </span><span class="hl-5">// description</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;claude-haiku-4-5&#39;</span><span class="hl-1"> </span><span class="hl-5">// model</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-3">if</span><span class="hl-1"> (!</span><span class="hl-4">agent</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">) </span><span class="hl-3">throw</span><span class="hl-1"> </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Error</span><span class="hl-1">(</span><span class="hl-2">&#39;Agent creation failed&#39;</span><span class="hl-1">);</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-2">`Created agent: </span><span class="hl-6">${</span><span class="hl-4">agent</span><span class="hl-10">.</span><span class="hl-4">id</span><span class="hl-6">}</span><span class="hl-2">`</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// Process text with the agent</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">result</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">processText</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-4">agent</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;What is the capital of France?&#39;</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-2">`Response: </span><span class="hl-6">${</span><span class="hl-4">result</span><span class="hl-10">.</span><span class="hl-4">result</span><span class="hl-6">}</span><span class="hl-2">`</span><span class="hl-1">);</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-2">`Cost: $</span><span class="hl-6">${</span><span class="hl-4">result</span><span class="hl-10">.</span><span class="hl-4">totalCost</span><span class="hl-6">}</span><span class="hl-2">`</span><span class="hl-1">);</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-2">`Tokens: </span><span class="hl-6">${</span><span class="hl-10">(</span><span class="hl-4">result</span><span class="hl-10">.</span><span class="hl-4">totalInputTokens</span><span class="hl-10"> </span><span class="hl-1">??</span><span class="hl-10"> </span><span class="hl-8">0</span><span class="hl-10">) </span><span class="hl-1">+</span><span class="hl-10"> (</span><span class="hl-4">result</span><span class="hl-10">.</span><span class="hl-4">totalOutputTokens</span><span class="hl-10"> </span><span class="hl-1">??</span><span class="hl-10"> </span><span class="hl-8">0</span><span class="hl-10">)</span><span class="hl-6">}</span><span class="hl-2">`</span><span class="hl-1">);</span>
307
- </code><button type="button">Copy</button></pre>
308
-
309
- <h3 id="example-5-multi-agent-room-conversation" class="tsd-anchor-link">Example 5: Multi-Agent Room Conversation<a href="#example-5-multi-agent-room-conversation" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
310
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">appService</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-2">&#39;user-123&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// Create agents</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">analyst</span><span class="hl-1"> = </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createAgent</span><span class="hl-1">(</span><span class="hl-2">&#39;OpenAI&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Analyst&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Analyst&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Data analyst&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;gpt-4&#39;</span><span class="hl-1">);</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">writer</span><span class="hl-1"> = </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createAgent</span><span class="hl-1">(</span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Writer&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Writer&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Content writer&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;claude-haiku-4-5&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-3">if</span><span class="hl-1"> (!</span><span class="hl-4">analyst</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1"> || !</span><span class="hl-4">writer</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">) </span><span class="hl-3">throw</span><span class="hl-1"> </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Error</span><span class="hl-1">(</span><span class="hl-2">&#39;Agent creation failed&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// Create a room with both agents</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">room</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">createRoom</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Analysis Session&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Analyze sales data and produce a written summary&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> [</span><span class="hl-4">analyst</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">, </span><span class="hl-4">writer</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">]</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-5">// Send a message to start the conversation</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">sendRoomMessage</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-4">room</span><span class="hl-1">.</span><span class="hl-4">roomId</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Please analyze the sales data for Q4&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Analyst&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;speak&#39;</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-5">// The agents will automatically respond and interact</span><br/><span class="hl-5">// Check room messages to see the conversation</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">messages</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">appService</span><span class="hl-1">.</span><span class="hl-0">getRoomMessages</span><span class="hl-1">(</span><span class="hl-4">room</span><span class="hl-1">.</span><span class="hl-4">roomId</span><span class="hl-1">);</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-2">`Messages: </span><span class="hl-6">${</span><span class="hl-4">messages</span><span class="hl-10">.</span><span class="hl-4">messages</span><span class="hl-10">.</span><span class="hl-4">length</span><span class="hl-6">}</span><span class="hl-2">`</span><span class="hl-1">);</span>
311
- </code><button type="button">Copy</button></pre>
312
-
313
- <h3 id="example-6-story-message-emitter" class="tsd-anchor-link">Example 6: Story Message Emitter<a href="#example-6-story-message-emitter" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
314
- <p>The story message emitter delivers real-time events during gameplay. Subscribe to it after creating the story but before the first player turn, so no events are missed.</p>
315
- <pre><code class="typescript"><span class="hl-3">import</span><span class="hl-1"> </span><span class="hl-3">type</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">StoryStateEvent</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">StoryIntroduceCharacterEvent</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">StoryProgressChapterEvent</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">StoryErrorEvent</span><span class="hl-1">,</span><br/><span class="hl-1">} </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;briyah&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">app</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-2">&#39;user-123&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">story</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">createStory</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;The Lost Kingdom&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;A medieval fantasy where ancient ruins hold a terrible secret&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;A disgraced knight seeking redemption&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;A scholar and a sellsword&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-6">false</span><span class="hl-1">, </span><span class="hl-5">// illustrateStory — required, and positional</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">emitter</span><span class="hl-1"> = </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">getStoryMessageEmitter</span><span class="hl-1">(</span><span class="hl-4">story</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// Fires after every turn with the full updated game state.</span><br/><span class="hl-4">emitter</span><span class="hl-1">.</span><span class="hl-0">on</span><span class="hl-1">(</span><span class="hl-2">&#39;story-state&#39;</span><span class="hl-1">, </span><span class="hl-6">async</span><span class="hl-1"> (</span><span class="hl-4">event</span><span class="hl-1">: </span><span class="hl-9">StoryStateEvent</span><span class="hl-1">) </span><span class="hl-6">=&gt;</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> { </span><span class="hl-7">state</span><span class="hl-1"> } = </span><span class="hl-4">event</span><span class="hl-1">;</span><br/><span class="hl-1"> </span><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-2">&#39;Latest message:&#39;</span><span class="hl-1">, </span><span class="hl-4">state</span><span class="hl-1">.</span><span class="hl-4">latestRoomMessage</span><span class="hl-1">?.</span><span class="hl-4">content</span><span class="hl-1">);</span><br/><br/><span class="hl-1"> </span><span class="hl-5">// Detect the player&#39;s turn by comparing speaker names.</span><br/><span class="hl-1"> </span><span class="hl-5">// Do NOT use state.humanPrompt for this — it is a UI display string,</span><br/><span class="hl-1"> </span><span class="hl-5">// not a reliable turn indicator.</span><br/><span class="hl-1"> </span><span class="hl-3">if</span><span class="hl-1"> (</span><span class="hl-4">state</span><span class="hl-1">.</span><span class="hl-4">currentSpeaker</span><span class="hl-1"> === </span><span class="hl-4">state</span><span class="hl-1">.</span><span class="hl-4">userAgentName</span><span class="hl-1">) {</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">input</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">promptPlayer</span><span class="hl-1">(</span><span class="hl-4">state</span><span class="hl-1">.</span><span class="hl-4">humanPrompt</span><span class="hl-1">);</span><br/><span class="hl-1"> </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">respondToStory</span><span class="hl-1">(</span><span class="hl-4">story</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">, </span><span class="hl-4">input</span><span class="hl-1">);</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-5">// Fires when the narrator wants to introduce a new character.</span><br/><span class="hl-5">// The host app should present this to the player before acting.</span><br/><span class="hl-5">// Accept by calling introduceCharacterToStory, or decline with declineCharacter.</span><br/><span class="hl-4">emitter</span><span class="hl-1">.</span><span class="hl-0">on</span><span class="hl-1">(</span><span class="hl-2">&#39;suggest-introduce-character&#39;</span><span class="hl-1">, </span><span class="hl-6">async</span><span class="hl-1"> (</span><span class="hl-4">event</span><span class="hl-1">: </span><span class="hl-9">StoryIntroduceCharacterEvent</span><span class="hl-1">) </span><span class="hl-6">=&gt;</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">accept</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">askPlayer</span><span class="hl-1">(</span><span class="hl-2">`Add </span><span class="hl-6">${</span><span class="hl-4">event</span><span class="hl-10">.</span><span class="hl-4">characterName</span><span class="hl-6">}</span><span class="hl-2"> to the story?`</span><span class="hl-1">);</span><br/><span class="hl-1"> </span><span class="hl-3">if</span><span class="hl-1"> (</span><span class="hl-4">accept</span><span class="hl-1">) {</span><br/><span class="hl-1"> </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">introduceCharacterToStory</span><span class="hl-1">(</span><span class="hl-4">story</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">, </span><span class="hl-4">event</span><span class="hl-1">.</span><span class="hl-4">characterName</span><span class="hl-1">!, </span><span class="hl-2">&#39;&#39;</span><span class="hl-1">, </span><span class="hl-6">undefined</span><span class="hl-1">, </span><span class="hl-6">true</span><span class="hl-1">);</span><br/><span class="hl-1"> } </span><span class="hl-3">else</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">declineCharacter</span><span class="hl-1">(</span><span class="hl-4">story</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">, </span><span class="hl-4">event</span><span class="hl-1">.</span><span class="hl-4">characterName</span><span class="hl-1">!);</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-5">// Fires when the narrator wants to advance to the next chapter.</span><br/><span class="hl-5">// Call progressStory to accept. Ignoring the event stays in the current chapter —</span><br/><span class="hl-5">// there is no explicit rejection call.</span><br/><span class="hl-4">emitter</span><span class="hl-1">.</span><span class="hl-0">on</span><span class="hl-1">(</span><span class="hl-2">&#39;suggest-progress-chapter&#39;</span><span class="hl-1">, </span><span class="hl-6">async</span><span class="hl-1"> (</span><span class="hl-4">_event</span><span class="hl-1">: </span><span class="hl-9">StoryProgressChapterEvent</span><span class="hl-1">) </span><span class="hl-6">=&gt;</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">accept</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">askPlayer</span><span class="hl-1">(</span><span class="hl-2">&#39;The narrator suggests ending this chapter. Continue?&#39;</span><span class="hl-1">);</span><br/><span class="hl-1"> </span><span class="hl-3">if</span><span class="hl-1"> (</span><span class="hl-4">accept</span><span class="hl-1">) {</span><br/><span class="hl-1"> </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">progressStory</span><span class="hl-1">(</span><span class="hl-4">story</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">);</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-5">// Fires on fatal errors during room processing.</span><br/><span class="hl-5">// Named &#39;story-error&#39; rather than &#39;error&#39; to avoid Node.js throwing when no</span><br/><span class="hl-5">// listener is attached.</span><br/><span class="hl-4">emitter</span><span class="hl-1">.</span><span class="hl-0">on</span><span class="hl-1">(</span><span class="hl-2">&#39;story-error&#39;</span><span class="hl-1">, (</span><span class="hl-4">event</span><span class="hl-1">: </span><span class="hl-9">StoryErrorEvent</span><span class="hl-1">) </span><span class="hl-6">=&gt;</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-3">if</span><span class="hl-1"> (</span><span class="hl-4">event</span><span class="hl-1">.</span><span class="hl-4">errorType</span><span class="hl-1"> === </span><span class="hl-2">&#39;InsufficientBalanceError&#39;</span><span class="hl-1">) {</span><br/><span class="hl-1"> </span><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">error</span><span class="hl-1">(</span><span class="hl-2">&#39;Out of credits:&#39;</span><span class="hl-1">, </span><span class="hl-4">event</span><span class="hl-1">.</span><span class="hl-4">message</span><span class="hl-1">);</span><br/><span class="hl-1"> } </span><span class="hl-3">else</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">error</span><span class="hl-1">(</span><span class="hl-2">`Story error [</span><span class="hl-6">${</span><span class="hl-4">event</span><span class="hl-10">.</span><span class="hl-4">errorType</span><span class="hl-6">}</span><span class="hl-2">]:`</span><span class="hl-1">, </span><span class="hl-4">event</span><span class="hl-1">.</span><span class="hl-4">message</span><span class="hl-1">);</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">});</span>
316
- </code><button type="button">Copy</button></pre>
317
-
318
- <h3 id="example-7-recording-payments-and-managing-balance" class="tsd-anchor-link">Example 7: Recording Payments and Managing Balance<a href="#example-7-recording-payments-and-managing-balance" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
319
- <p>Briyah does not process payments itself — your application handles the payment provider interaction. Once a payment is confirmed, use <code>AppService</code> to record the transaction, credit the user's balance, and restart any stories that stalled due to insufficient funds.</p>
320
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">app</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-4">userId</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// --- When your payment provider initiates a charge ---</span><br/><span class="hl-5">// Record it as pending so the transaction log stays consistent.</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">recordTransaction</span><span class="hl-1">(</span><span class="hl-4">amount</span><span class="hl-1">, </span><span class="hl-4">paymentIntentId</span><span class="hl-1">, </span><span class="hl-2">&#39;pending&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// --- When your payment provider webhook confirms success ---</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">updateTransactionStatus</span><span class="hl-1">(</span><span class="hl-4">paymentIntentId</span><span class="hl-1">, </span><span class="hl-2">&#39;succeeded&#39;</span><span class="hl-1">);</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">addBalance</span><span class="hl-1">(</span><span class="hl-4">amount</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// Resume any stories that paused mid-turn due to InsufficientBalanceError.</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">resumePausedStories</span><span class="hl-1">();</span><br/><br/><span class="hl-5">// --- When a payment fails or is cancelled ---</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">updateTransactionStatus</span><span class="hl-1">(</span><span class="hl-4">paymentIntentId</span><span class="hl-1">, </span><span class="hl-2">&#39;failed&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// --- Querying balance and history ---</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">balance</span><span class="hl-1"> = </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">getBalance</span><span class="hl-1">();</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> { </span><span class="hl-7">transactions</span><span class="hl-1">, </span><span class="hl-7">total</span><span class="hl-1"> } = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">getTransactions</span><span class="hl-1">(</span><span class="hl-8">50</span><span class="hl-1">, </span><span class="hl-8">0</span><span class="hl-1">);</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">tx</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">getTransactionByPaymentId</span><span class="hl-1">(</span><span class="hl-4">paymentIntentId</span><span class="hl-1">);</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-4">tx</span><span class="hl-1">?.</span><span class="hl-4">status</span><span class="hl-1">); </span><span class="hl-5">// &#39;pending&#39; | &#39;succeeded&#39; | &#39;failed&#39; | &#39;cancelled&#39;</span>
321
- </code><button type="button">Copy</button></pre>
322
-
323
- <p>To push real-time balance updates to a connected client (e.g. via SSE), subscribe to the balance emitter before crediting the user:</p>
324
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">emitter</span><span class="hl-1"> = </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">getBalanceMessageEmitter</span><span class="hl-1">(</span><span class="hl-4">userId</span><span class="hl-1">);</span><br/><span class="hl-4">emitter</span><span class="hl-1">.</span><span class="hl-0">on</span><span class="hl-1">(</span><span class="hl-2">&#39;update&#39;</span><span class="hl-1">, (</span><span class="hl-4">newBalance</span><span class="hl-1">: </span><span class="hl-9">number</span><span class="hl-1">) </span><span class="hl-6">=&gt;</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-5">// Push newBalance to the client</span><br/><span class="hl-1">});</span>
325
- </code><button type="button">Copy</button></pre>
326
-
327
- <h3 id="example-8-registering-native-tools-with-opaque-context" class="tsd-anchor-link">Example 8: Registering Native Tools with Opaque Context<a href="#example-8-registering-native-tools-with-opaque-context" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
328
- <p>Native tools are functions you implement in your host code and register once with <code>briyah.registerTool</code>. Grant a tool to an agent by adding its name to <code>agent.toolNames</code>; the agent then calls it on its own (via the EXECUTE room action) when a conversation needs it, chaining several calls privately before it replies.</p>
329
- <p>Each call receives a third argument — an <strong>opaque context</strong> you pass to <code>sendRoomMessage</code>. Briyah never inspects it; it hands it straight to your handler. Use it for per-interaction data the model must not be able to choose. The declared <code>parameters</code> are what the LLM fills in; scoping data (which record, which tenant, a DB handle) belongs in context, so a confused or adversarial agent cannot target data outside the current interaction.</p>
330
- <pre><code class="typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">Briyah</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;briyah&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({ </span><span class="hl-4">dataPath:</span><span class="hl-1"> </span><span class="hl-2">&#39;./data/briyah&#39;</span><span class="hl-1">, </span><span class="hl-4">startingBalance:</span><span class="hl-1"> </span><span class="hl-8">10.00</span><span class="hl-1"> });</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">init</span><span class="hl-1">();</span><br/><br/><span class="hl-5">// Register a tool that reads its scope from context, not from the LLM&#39;s args.</span><br/><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">registerTool</span><span class="hl-1">({</span><br/><span class="hl-1"> </span><span class="hl-4">name:</span><span class="hl-1"> </span><span class="hl-2">&#39;add_note&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">description:</span><span class="hl-1"> </span><span class="hl-2">&quot;Add a note to the record the user currently has open&quot;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">parameters:</span><span class="hl-1"> [</span><br/><span class="hl-1"> { </span><span class="hl-4">name:</span><span class="hl-1"> </span><span class="hl-2">&#39;text&#39;</span><span class="hl-1">, </span><span class="hl-4">type:</span><span class="hl-1"> </span><span class="hl-2">&#39;string&#39;</span><span class="hl-1">, </span><span class="hl-4">description:</span><span class="hl-1"> </span><span class="hl-2">&#39;The note text&#39;</span><span class="hl-1">, </span><span class="hl-4">required:</span><span class="hl-1"> </span><span class="hl-6">true</span><span class="hl-1"> }</span><br/><span class="hl-1"> ],</span><br/><span class="hl-1"> </span><span class="hl-0">handler</span><span class="hl-4">:</span><span class="hl-1"> </span><span class="hl-6">async</span><span class="hl-1"> (</span><span class="hl-4">args</span><span class="hl-1">, </span><span class="hl-4">helpers</span><span class="hl-1">, </span><span class="hl-4">context</span><span class="hl-1">) </span><span class="hl-6">=&gt;</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> { </span><span class="hl-7">db</span><span class="hl-1">, </span><span class="hl-7">recordId</span><span class="hl-1"> } = </span><span class="hl-4">context</span><span class="hl-1"> </span><span class="hl-3">as</span><span class="hl-1"> { </span><span class="hl-4">db</span><span class="hl-1">: </span><span class="hl-9">MyDb</span><span class="hl-1">; </span><span class="hl-4">recordId</span><span class="hl-1">: </span><span class="hl-9">string</span><span class="hl-1"> };</span><br/><span class="hl-1"> </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">db</span><span class="hl-1">.</span><span class="hl-0">addNote</span><span class="hl-1">(</span><span class="hl-4">recordId</span><span class="hl-1">, </span><span class="hl-4">args</span><span class="hl-1">.</span><span class="hl-4">text</span><span class="hl-1">); </span><span class="hl-5">// recordId comes from context — the LLM cannot set it</span><br/><span class="hl-1"> </span><span class="hl-3">return</span><span class="hl-1"> { </span><span class="hl-4">added:</span><span class="hl-1"> </span><span class="hl-6">true</span><span class="hl-1"> };</span><br/><span class="hl-1"> },</span><br/><span class="hl-1"> </span><span class="hl-4">timeoutMs:</span><span class="hl-1"> </span><span class="hl-8">15000</span><span class="hl-1"> </span><span class="hl-5">// optional; default 30s, max 120s</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-5">// Grant it to an agent (grants are enforced server-side).</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">assistant</span><span class="hl-1"> = </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">createAgent</span><span class="hl-1">(</span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Assistant&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Assistant&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;A helpful assistant that manages the open record&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;claude-haiku-4-5&#39;</span><span class="hl-1">);</span><br/><span class="hl-4">assistant</span><span class="hl-1">.</span><span class="hl-4">toolNames</span><span class="hl-1"> = [</span><span class="hl-2">&#39;add_note&#39;</span><span class="hl-1">];</span><br/><span class="hl-4">assistant</span><span class="hl-1">.</span><span class="hl-0">save</span><span class="hl-1">();</span><br/><br/><span class="hl-5">// When the user acts on record &quot;rec_42&quot;, pass that scope as context. Any tool the</span><br/><span class="hl-5">// agent calls during the resulting turn receives it; other rooms/interactions are</span><br/><span class="hl-5">// unaffected. A log-only &#39;tool&#39; message records each call (visible when fetching</span><br/><span class="hl-5">// messages with includeThoughts=true).</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">sendRoomMessage</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-4">roomId</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Add a note that the client called about renewal.&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;User&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;speak&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> [],</span><br/><span class="hl-1"> </span><span class="hl-6">false</span><span class="hl-1">,</span><br/><span class="hl-1"> { </span><span class="hl-4">db</span><span class="hl-1">, </span><span class="hl-4">recordId:</span><span class="hl-1"> </span><span class="hl-2">&#39;rec_42&#39;</span><span class="hl-1"> } </span><span class="hl-5">// opaque context</span><br/><span class="hl-1">);</span>
331
- </code><button type="button">Copy</button></pre>
332
-
333
- <p>Because each Briyah <code>userId</code> gets its own isolated <code>AppService</code>, data directory, and balance, mapping one Briyah user per tenant (customer, workspace, org) gives you tenant isolation and per-tenant billing for free; the opaque context then scopes each call within that tenant.</p>
334
- <p>Agents can also delegate: an agent may respond with a <code>commission</code> action naming other agents, each of which researches independently (using its granted tools, with the same context) and replies with a <code>deliver</code>. The commissioner receives all deliverables at once and responds before the conversation continues. No SDK calls are needed for this — it is driven by the agents themselves.</p>
335
- <p>User-authored (string-code) tools remain available via <code>createTool</code>/<code>updateTool</code>/<code>testTool</code> when <code>allowUserAuthoredTools</code> is enabled, for trusted single-user setups.</p>
336
- <h3 id="example-9-self-hosted-image-generation-with-comfyui" class="tsd-anchor-link">Example 9: Self-Hosted Image Generation with ComfyUI<a href="#example-9-self-hosted-image-generation-with-comfyui" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
337
- <p>The <code>ComfyUI</code> provider generates images on a self-hosted <a href="https://github.com/comfyanonymous/ComfyUI">ComfyUI</a> server. It is image-only: <code>generateImage</code>/<code>editImage</code> work, <code>textPrompt</code> throws. Set <code>COMFYUI_BASE_URL</code> to enable it. ComfyUI has no authentication of its own — keep it on a LAN/VPN or behind an authenticated reverse proxy (pass proxy credentials via <code>COMFYUI_EXTRA_HEADERS</code>).</p>
338
- <p>For this provider the agent's <em>model name</em> selects a <strong>workflow-template pair</strong>, not a checkpoint. Two pairs ship by default:</p>
339
- <table>
340
- <thead>
341
- <tr>
342
- <th>Pair name</th>
343
- <th>Model</th>
344
- <th>Files required on the ComfyUI server</th>
345
- </tr>
346
- </thead>
347
- <tbody>
348
- <tr>
349
- <td><code>flux2-dev</code></td>
350
- <td>FLUX.2 dev (FP8 mixed)</td>
351
- <td><code>diffusion_models/flux2_dev_fp8mixed.safetensors</code>, <code>text_encoders/mistral_3_small_flux2_bf16.safetensors</code>, <code>vae/flux2-vae.safetensors</code></td>
352
- </tr>
353
- <tr>
354
- <td><code>flux-kontext</code></td>
355
- <td>Flux.1 Kontext dev (FP8)</td>
356
- <td><code>diffusion_models/flux1-dev-kontext_fp8_scaled.safetensors</code>, <code>text_encoders/clip_l.safetensors</code> + <code>t5xxl_fp8_e4m3fn_scaled.safetensors</code>, <code>vae/ae.safetensors</code></td>
357
- </tr>
358
- </tbody>
359
- </table>
360
- <p><code>flux2-dev</code> has substantially better multi-reference character consistency and wants a 32 GB-class GPU; <code>flux-kontext</code> runs on 16 GB-class cards.</p>
361
- <pre><code class="typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">Briyah</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;briyah&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({ </span><span class="hl-4">dataPath:</span><span class="hl-1"> </span><span class="hl-2">&#39;./data/briyah&#39;</span><span class="hl-1">, </span><span class="hl-4">startingBalance:</span><span class="hl-1"> </span><span class="hl-8">10.00</span><span class="hl-1"> });</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">init</span><span class="hl-1">();</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">app</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-2">&#39;user-123&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// Model name = workflow pair name</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">artist</span><span class="hl-1"> = </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">createAgent</span><span class="hl-1">(</span><span class="hl-2">&#39;ComfyUI&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Artist&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Artist&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Image generation agent&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;flux2-dev&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// Text-to-image. Resolves to { artifactId } on success or { error } on failure.</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">generated</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">artist</span><span class="hl-1">.</span><span class="hl-0">generateImage</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;A lighthouse on a rocky coast at dusk&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> { </span><span class="hl-4">width:</span><span class="hl-1"> </span><span class="hl-8">1024</span><span class="hl-1">, </span><span class="hl-4">height:</span><span class="hl-1"> </span><span class="hl-8">1024</span><span class="hl-1"> } </span><span class="hl-5">// optional; a fixed seed may also be passed</span><br/><span class="hl-1">);</span><br/><span class="hl-3">if</span><span class="hl-1"> (</span><span class="hl-4">generated</span><span class="hl-1">.</span><span class="hl-4">error</span><span class="hl-1">) </span><span class="hl-3">throw</span><span class="hl-1"> </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Error</span><span class="hl-1">(</span><span class="hl-0">String</span><span class="hl-1">(</span><span class="hl-4">generated</span><span class="hl-1">.</span><span class="hl-4">error</span><span class="hl-1">));</span><br/><br/><span class="hl-5">// Generated images are stored as artifacts (PNG)</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">png</span><span class="hl-1"> = </span><span class="hl-4">artist</span><span class="hl-1">.</span><span class="hl-4">artifactService</span><span class="hl-1">.</span><span class="hl-0">getArtifact</span><span class="hl-1">(</span><span class="hl-4">generated</span><span class="hl-1">.</span><span class="hl-4">artifactId</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// Image-to-image with reference images. Each referenced artifact is uploaded</span><br/><span class="hl-5">// and conditioned on separately (chained ReferenceLatent nodes). Order is</span><br/><span class="hl-5">// preserved, so the prompt can refer to &quot;Image 1&quot;, &quot;Image 2&quot;, ...</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">edited</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">artist</span><span class="hl-1">.</span><span class="hl-0">editImage</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;The character from Image 2 stands in the setting from Image 1&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> { </span><span class="hl-4">width:</span><span class="hl-1"> </span><span class="hl-8">1024</span><span class="hl-1">, </span><span class="hl-4">height:</span><span class="hl-1"> </span><span class="hl-8">1024</span><span class="hl-1"> },</span><br/><span class="hl-1"> [</span><span class="hl-4">sceneArtifactId</span><span class="hl-1">, </span><span class="hl-4">portraitArtifactId</span><span class="hl-1">]</span><br/><span class="hl-1">);</span>
362
- </code><button type="button">Copy</button></pre>
363
-
364
- <p>Notes:</p>
365
- <ul>
366
- <li><strong>Cost</strong>: generation is free by default — no balance is checked or decremented. To charge users, pass <code>centsPerImage</code> in the image properties; the balance gate then applies.</li>
367
- <li><strong>Latency</strong>: the GPU processes one job at a time and the first generation after server start loads the model weights. <code>COMFYUI_TIMEOUT_MS</code> (default 10 minutes) covers queue wait plus cold start.</li>
368
- <li><strong>Stories</strong>: to make a pair selectable in the story UI's image-model dropdown, add an entry to <code>{dataPath}/common/config/image_models.json</code> with <code>&quot;service&quot;: &quot;ComfyUI&quot;</code> and the pair name as <code>&quot;model&quot;</code> in the <code>generation</code> and <code>editing</code> blocks, with <code>&quot;centsPerImage&quot;: 0</code>.</li>
369
- </ul>
370
- <p><strong>Custom workflows.</strong> Any ComfyUI workflow can be used: build it in the ComfyUI web UI, run it once, then use Export (API format) and replace the values you want parameterized with tokens — <code>{{prompt}}</code> (may be embedded in a longer string), <code>{{seed}}</code>, <code>{{width}}</code>, <code>{{height}}</code> (replaced as numbers), and <code>{{ref_image}}</code> (edit workflows, the <code>LoadImage</code> input). Save the files as <code>{name}.t2i.json</code> (used by <code>generateImage</code>) and <code>{name}.edit.json</code> (used by <code>editImage</code>) in <code>{dataPath}/common/config/comfyui-workflows/</code>, or in <code>{dataPath}/user/{userId}/comfyui-workflows/</code> for a per-user override (checked first). The pair name becomes the model name. For multi-reference support, an edit workflow must contain exactly one <code>ReferenceLatent</code> node fed by a <code>VAEEncode</code> fed by a <code>LoadImage</code>; extra references are added at request time by cloning that chain.</p>
371
- <h3 id="example-10-a-turn-based-table-with-a-human-player" class="tsd-anchor-link">Example 10: A turn-based table with a human player<a href="#example-10-a-turn-based-table-with-a-human-player" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
372
- <p>Example 5 sends one message and reads the transcript back, which suits a batch job. A game or
373
- any human-in-the-loop app needs the other shape: the agents take several turns among themselves,
374
- then stop and wait for the person. Three things make that work.</p>
375
- <p><strong><code>turnMode: 'directed'</code></strong> — only the agent a message names is asked to reply, and the room
376
- leader answers anything it cannot place. The default, <code>'broadcast'</code>, prompts <em>every</em> agent for
377
- <em>every</em> message and keeps one reply, which costs several times as much and is wrong for a table
378
- where one person speaks at a time.</p>
379
- <p><strong>A human-controlled agent</strong> — <code>createAgent</code>'s seventh argument. The room delivers messages to
380
- it and then stops rather than generating a reply, which is what yields the floor to your console.</p>
381
- <p><strong>The room message emitter</strong> — the only way to see the conversation as it happens. It fires on
382
- every change with the newest message and the room's state.</p>
383
- <pre><code class="typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">Briyah</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;briyah&#39;</span><span class="hl-1">;</span><br/><span class="hl-3">import</span><span class="hl-1"> </span><span class="hl-3">type</span><span class="hl-1"> { </span><span class="hl-4">RoomUpdate</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;briyah&#39;</span><span class="hl-1">;</span><br/><span class="hl-3">import</span><span class="hl-1"> </span><span class="hl-6">*</span><span class="hl-1"> </span><span class="hl-3">as</span><span class="hl-1"> </span><span class="hl-4">readline</span><span class="hl-1"> </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;node:readline/promises&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-5">// The constructor installs every bundled pack&#39;s tools already; pass `tools` only to</span><br/><span class="hl-5">// filter or wrap them, or to add your own.</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({ </span><span class="hl-4">dataPath:</span><span class="hl-1"> </span><span class="hl-2">&#39;./briyah-data&#39;</span><span class="hl-1"> });</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">init</span><span class="hl-1">(); </span><span class="hl-5">// seeds prompt folders and the packs</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">app</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-2">&#39;user-123&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-5">// Enable the pack for this user, or a bare folder name will not reach it. Enabling is</span><br/><span class="hl-5">// per user; installing is not.</span><br/><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-4">packs</span><span class="hl-1">.</span><span class="hl-0">setEnabled</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e&#39;</span><span class="hl-1">, </span><span class="hl-6">true</span><span class="hl-1">);</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">dm</span><span class="hl-1"> = </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">createAgent</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Dungeon Master&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;DM&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Runs the game.&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;claude-sonnet-5&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;dnd5e:dungeon_master&#39;</span><span class="hl-1">, </span><span class="hl-6">undefined</span><span class="hl-1">, </span><span class="hl-6">null</span><span class="hl-1">, </span><span class="hl-8">0</span><span class="hl-1">, </span><span class="hl-2">&#39;&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> { </span><span class="hl-4">toolNames:</span><span class="hl-1"> [</span><span class="hl-2">&#39;roll_dice&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;check&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;apply_hp&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;stat_block&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;read_sheet&#39;</span><span class="hl-1">] },</span><br/><span class="hl-1">);</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">rogue</span><span class="hl-1"> = </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">createAgent</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Vex&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Vex&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;A wary scout.&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;claude-haiku-4-5&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;dnd5e:dnd_player&#39;</span><span class="hl-1">,</span><br/><span class="hl-1">);</span><br/><span class="hl-5">// The seventh argument is what makes this one wait for a person.</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">you</span><span class="hl-1"> = </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">createAgent</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Homer&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Homer&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;A hopeful bard.&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;claude-haiku-4-5&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;dnd5e:dnd_player&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-6">true</span><span class="hl-1">,</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-5">// createAgent builds the agent in memory; save() writes it to disk. Without this the</span><br/><span class="hl-5">// table exists for this process only and is gone on restart.</span><br/><span class="hl-1">[</span><span class="hl-4">dm</span><span class="hl-1">, </span><span class="hl-4">rogue</span><span class="hl-1">, </span><span class="hl-4">you</span><span class="hl-1">].</span><span class="hl-0">forEach</span><span class="hl-1">((</span><span class="hl-4">agent</span><span class="hl-1">) </span><span class="hl-6">=&gt;</span><span class="hl-1"> </span><span class="hl-4">agent</span><span class="hl-1">.</span><span class="hl-0">save</span><span class="hl-1">());</span><br/><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">attachArtifactToAgent</span><span class="hl-1">(</span><span class="hl-4">dm</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">!, </span><span class="hl-2">&#39;pack:dnd5e.dungeon_master-procedure&#39;</span><span class="hl-1">);</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">attachArtifactToAgent</span><span class="hl-1">(</span><span class="hl-4">rogue</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">!, </span><span class="hl-2">&#39;pack:dnd5e.class-rogue&#39;</span><span class="hl-1">);</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">attachArtifactToAgent</span><span class="hl-1">(</span><span class="hl-4">you</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">!, </span><span class="hl-2">&#39;pack:dnd5e.class-bard&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> { </span><span class="hl-7">roomId</span><span class="hl-1"> } = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">createRoom</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;The Sunken Barrow&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;A three-room dungeon crawl.&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> [</span><span class="hl-4">dm</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">!, </span><span class="hl-4">rogue</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">!, </span><span class="hl-4">you</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">!],</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;DM&#39;</span><span class="hl-1">, </span><span class="hl-5">// roomLeader — answers anything the room cannot place</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;directed&#39;</span><span class="hl-1">, </span><span class="hl-5">// turnMode</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-5">// A room loaded from disk has no state-change callback, so its emitter would never</span><br/><span class="hl-5">// fire. Harmless on a room you just created; required for one you resumed.</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">ensureRoomStateCallback</span><span class="hl-1">(</span><span class="hl-4">roomId</span><span class="hl-1">);</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">io</span><span class="hl-1"> = </span><span class="hl-4">readline</span><span class="hl-1">.</span><span class="hl-0">createInterface</span><span class="hl-1">({ </span><span class="hl-4">input:</span><span class="hl-1"> </span><span class="hl-4">process</span><span class="hl-1">.</span><span class="hl-4">stdin</span><span class="hl-1">, </span><span class="hl-4">output:</span><span class="hl-1"> </span><span class="hl-4">process</span><span class="hl-1">.</span><span class="hl-4">stdout</span><span class="hl-1"> });</span><br/><span class="hl-6">let</span><span class="hl-1"> </span><span class="hl-4">lastIndex</span><span class="hl-1"> = -</span><span class="hl-8">1</span><span class="hl-1">;</span><br/><span class="hl-6">let</span><span class="hl-1"> </span><span class="hl-4">atKeyboard</span><span class="hl-1"> = </span><span class="hl-6">false</span><span class="hl-1">; </span><span class="hl-5">// the handler is async; without this it re-enters</span><br/><span class="hl-6">let</span><span class="hl-1"> </span><span class="hl-4">replyTo</span><span class="hl-1"> = </span><span class="hl-2">&#39;DM&#39;</span><span class="hl-1">; </span><span class="hl-5">// who last spoke to the player</span><br/><span class="hl-6">let</span><span class="hl-1"> </span><span class="hl-4">replyAs</span><span class="hl-1">: </span><span class="hl-2">&#39;speak&#39;</span><span class="hl-1"> | </span><span class="hl-2">&#39;whisper&#39;</span><span class="hl-1"> = </span><span class="hl-2">&#39;speak&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">getRoomMessageEmitter</span><span class="hl-1">(</span><span class="hl-4">roomId</span><span class="hl-1">).</span><span class="hl-0">on</span><span class="hl-1">(</span><span class="hl-2">&#39;update&#39;</span><span class="hl-1">, </span><span class="hl-6">async</span><span class="hl-1"> (</span><span class="hl-4">state</span><span class="hl-1">: </span><span class="hl-9">RoomUpdate</span><span class="hl-1">) </span><span class="hl-6">=&gt;</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-3">if</span><span class="hl-1"> (</span><span class="hl-4">state</span><span class="hl-1">.</span><span class="hl-4">type</span><span class="hl-1"> === </span><span class="hl-2">&#39;error&#39;</span><span class="hl-1">) { </span><span class="hl-5">// a turn failed — see &quot;Errors&quot; below</span><br/><span class="hl-1"> </span><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">error</span><span class="hl-1">(</span><span class="hl-2">`[</span><span class="hl-6">${</span><span class="hl-4">state</span><span class="hl-10">.</span><span class="hl-4">errorType</span><span class="hl-6">}</span><span class="hl-2">] </span><span class="hl-6">${</span><span class="hl-4">state</span><span class="hl-10">.</span><span class="hl-4">message</span><span class="hl-6">}</span><span class="hl-2">`</span><span class="hl-1">);</span><br/><span class="hl-1"> </span><span class="hl-3">return</span><span class="hl-1">;</span><br/><span class="hl-1"> }</span><br/><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">message</span><span class="hl-1"> = </span><span class="hl-4">state</span><span class="hl-1">.</span><span class="hl-4">latestMessage</span><span class="hl-1">;</span><br/><span class="hl-1"> </span><span class="hl-3">if</span><span class="hl-1"> (</span><span class="hl-4">message</span><span class="hl-1"> &amp;&amp; </span><span class="hl-4">state</span><span class="hl-1">.</span><span class="hl-4">messageIndex</span><span class="hl-1"> !== </span><span class="hl-6">undefined</span><span class="hl-1"> &amp;&amp; </span><span class="hl-4">state</span><span class="hl-1">.</span><span class="hl-4">messageIndex</span><span class="hl-1"> &gt; </span><span class="hl-4">lastIndex</span><span class="hl-1">) {</span><br/><span class="hl-1"> </span><span class="hl-4">lastIndex</span><span class="hl-1"> = </span><span class="hl-4">state</span><span class="hl-1">.</span><span class="hl-4">messageIndex</span><span class="hl-1">; </span><span class="hl-5">// the emitter can repeat a state</span><br/><span class="hl-1"> </span><span class="hl-5">// Everything the room logged comes down this stream, an agent&#39;s thinking</span><br/><span class="hl-1"> </span><span class="hl-5">// and its tool calls included. Nobody at the table hears either, and a DM</span><br/><span class="hl-1"> </span><span class="hl-5">// thinking out loud is the adventure with its answers written in.</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">audible</span><span class="hl-1"> = </span><span class="hl-4">message</span><span class="hl-1">.</span><span class="hl-4">action</span><span class="hl-1"> !== </span><span class="hl-2">&#39;think&#39;</span><span class="hl-1"> &amp;&amp; </span><span class="hl-4">message</span><span class="hl-1">.</span><span class="hl-4">action</span><span class="hl-1"> !== </span><span class="hl-2">&#39;tool&#39;</span><span class="hl-1">;</span><br/><span class="hl-1"> </span><span class="hl-3">if</span><span class="hl-1"> (</span><span class="hl-4">audible</span><span class="hl-1"> &amp;&amp; </span><span class="hl-4">message</span><span class="hl-1">.</span><span class="hl-4">sender</span><span class="hl-1"> !== </span><span class="hl-2">&#39;Homer&#39;</span><span class="hl-1">) {</span><br/><span class="hl-1"> </span><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-2">`</span><span class="hl-6">${</span><span class="hl-4">message</span><span class="hl-10">.</span><span class="hl-4">sender</span><span class="hl-6">}</span><span class="hl-2">: </span><span class="hl-6">${</span><span class="hl-4">message</span><span class="hl-10">.</span><span class="hl-4">content</span><span class="hl-6">}</span><span class="hl-2">`</span><span class="hl-1">);</span><br/><span class="hl-1"> </span><span class="hl-5">// Answer whoever addressed you, the way they addressed you. During</span><br/><span class="hl-1"> </span><span class="hl-5">// session zero that is the designer or the creator, whispering —</span><br/><span class="hl-1"> </span><span class="hl-5">// replying to the DM instead drops the interview.</span><br/><span class="hl-1"> </span><span class="hl-3">if</span><span class="hl-1"> (</span><span class="hl-4">message</span><span class="hl-1">.</span><span class="hl-4">targets</span><span class="hl-1">?.</span><span class="hl-0">includes</span><span class="hl-1">(</span><span class="hl-2">&#39;Homer&#39;</span><span class="hl-1">)) {</span><br/><span class="hl-1"> </span><span class="hl-4">replyTo</span><span class="hl-1"> = </span><span class="hl-4">message</span><span class="hl-1">.</span><span class="hl-4">sender</span><span class="hl-1">;</span><br/><span class="hl-1"> </span><span class="hl-4">replyAs</span><span class="hl-1"> = </span><span class="hl-4">message</span><span class="hl-1">.</span><span class="hl-4">action</span><span class="hl-1"> === </span><span class="hl-2">&#39;whisper&#39;</span><span class="hl-1"> ? </span><span class="hl-2">&#39;whisper&#39;</span><span class="hl-1"> : </span><span class="hl-2">&#39;speak&#39;</span><span class="hl-1">;</span><br/><span class="hl-1"> }</span><br/><span class="hl-1"> }</span><br/><span class="hl-1"> }</span><br/><br/><span class="hl-1"> </span><span class="hl-5">// The floor is the player&#39;s only when the room *names* them. `waitingForHuman`</span><br/><span class="hl-1"> </span><span class="hl-5">// is also true for a moment after sendRoomMessage clears currentSpeaker and</span><br/><span class="hl-1"> </span><span class="hl-5">// before processing starts, so gating on it alone opens a second prompt before</span><br/><span class="hl-1"> </span><span class="hl-5">// the table has said a word.</span><br/><span class="hl-1"> </span><span class="hl-3">if</span><span class="hl-1"> (!</span><span class="hl-4">atKeyboard</span><span class="hl-1"> &amp;&amp; </span><span class="hl-4">state</span><span class="hl-1">.</span><span class="hl-4">currentSpeaker</span><span class="hl-1"> === </span><span class="hl-2">&#39;Homer&#39;</span><span class="hl-1">) {</span><br/><span class="hl-1"> </span><span class="hl-4">atKeyboard</span><span class="hl-1"> = </span><span class="hl-6">true</span><span class="hl-1">;</span><br/><span class="hl-1"> </span><span class="hl-3">try</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">line</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">io</span><span class="hl-1">.</span><span class="hl-0">question</span><span class="hl-1">(</span><span class="hl-2">&#39;&gt; &#39;</span><span class="hl-1">);</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">result</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">sendRoomMessage</span><span class="hl-1">(</span><span class="hl-4">roomId</span><span class="hl-1">, </span><span class="hl-4">line</span><span class="hl-1">, </span><span class="hl-2">&#39;Homer&#39;</span><span class="hl-1">, </span><span class="hl-4">replyAs</span><span class="hl-1">, [</span><span class="hl-4">replyTo</span><span class="hl-1">]);</span><br/><span class="hl-1"> </span><span class="hl-3">if</span><span class="hl-1"> (!</span><span class="hl-4">result</span><span class="hl-1">.</span><span class="hl-4">accepted</span><span class="hl-1">) </span><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-2">`(not your turn — </span><span class="hl-6">${</span><span class="hl-4">result</span><span class="hl-10">.</span><span class="hl-4">reason</span><span class="hl-6">}</span><span class="hl-2">)`</span><span class="hl-1">);</span><br/><span class="hl-1"> } </span><span class="hl-3">finally</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">atKeyboard</span><span class="hl-1"> = </span><span class="hl-6">false</span><span class="hl-1">;</span><br/><span class="hl-1"> }</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-5">// The emitter only fires on a *change*, so the opening line comes from here.</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">sendRoomMessage</span><span class="hl-1">(</span><span class="hl-4">roomId</span><span class="hl-1">, </span><span class="hl-2">&#39;Begin the adventure.&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;Homer&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;speak&#39;</span><span class="hl-1">, [</span><span class="hl-2">&#39;DM&#39;</span><span class="hl-1">]);</span>
384
- </code><button type="button">Copy</button></pre>
385
-
386
- <p><strong>The emitter payload</strong> is <code>RoomUpdate</code>, a two-member union discriminated by <code>type</code> —
387
- <code>RoomMessageUpdate</code> (<code>type: 'state'</code>) and <code>RoomErrorUpdate</code> (<code>type: 'error'</code>). Both are exported,
388
- so the narrowing above compiles under <code>strict</code> with nothing hand-written. A state update carries
389
- <code>latestMessage</code>, <code>messageIndex</code>, <code>totalCost</code>, <code>isPaused</code>, <code>currentSpeaker</code>,
390
- <code>processingInProgress</code> and <code>waitingForHuman</code>. A state update carries <strong>every</strong> message the room
391
- logged — thoughts and tool notices included — and <code>messageIndex</code> is that message's own index, so
392
- it lines up with <code>getRoomMessages(roomId, 0, true)</code> and a gap between the two is a real gap
393
- rather than a difference of bookkeeping. Drop what a host does not want on <code>action</code>, as the loop
394
- above does; asking the room for a narrower list instead leaves the two counting differently, and
395
- nothing in the update can say by how much.</p>
396
- <p><strong>Errors</strong> arrive on the same emitter, as <code>{type: 'error', errorType, message, timestamp}</code>. Same
397
- event name rather than <code>'error'</code>, because an EventEmitter throws when an <code>'error'</code> event has no
398
- listener and that would take down a host that subscribed only to updates. Check for it first, as
399
- above: a turn that dies — most often on a spent balance — otherwise just goes quiet, with no
400
- message and nothing to await.</p>
401
- <p><strong><code>waitingForHuman</code> means &quot;the human may send something&quot;, not &quot;the human is being asked
402
- something&quot;.</strong> It is true while a human holds the floor, and also true for an idle room — no
403
- speaker, nothing queued, nothing in flight — which includes the gap between <code>sendRoomMessage</code>
404
- accepting a line and processing starting. Gate a prompt on it alone and you open a second one
405
- before the table has said a word. <code>currentSpeaker === &lt;the human's nickname&gt;</code> is the test for
406
- &quot;it is their turn&quot;, which is what the loop above uses; <code>waitingForHuman</code> is the right test for
407
- whether to enable a composer.</p>
408
- <p><strong><code>sendRoomMessage</code>'s <code>action</code> defaults to <code>'moderate'</code></strong>, not <code>'speak'</code>. Pass the action you
409
- mean. A message sent out of turn is refused rather than thrown: the result is
410
- <code>{accepted: false, reason}</code>, which is what to show as &quot;not your turn&quot;.</p>
411
- <p><strong>The <code>sender</code> string must be an <code>agentNickname</code></strong> — that is the name the room routes by.</p>
412
- <p><strong>The <code>replyTo</code>/<code>replyAs</code> bookkeeping is only for a table with no moderator.</strong> This room has
413
- none, so the player's line has to name its own recipient. Give the human agent
414
- <code>useModerator: true</code> in a room that has a moderator and all of it goes away — the room forces the
415
- message to <code>moderate</code>, clears the targets, and the moderator decides who answers. That is how the
416
- <code>dnd5e</code> room template ships; see <a href="#example-11-seating-a-whole-dd-table">Example 11</a>.</p>
417
- <p>Two notes for a long-running process. Call <code>await briyah.shutdown()</code> before exiting or it will
418
- hang on open handles. And a room with no human-controlled agent never stops on its own: the
419
- agents talk until the leader adjourns.</p>
420
- <h3 id="example-11-seating-a-whole-dd-table" class="tsd-anchor-link">Example 11: Seating a whole D&amp;D table<a href="#example-11-seating-a-whole-dd-table" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
421
- <p>Example 10 seats a Dungeon Master and players, which is enough to see the loop but not enough to
422
- play. The <code>dungeon_master</code> prompt whispers a <strong>campaign designer</strong> to build the world and a
423
- <strong>character creator</strong> to build sheets, and hands fights to a <strong>battle master</strong>, so a table
424
- without those three has a leader whose own instructions reference agents that are not there.</p>
425
- <p>The pack ships that table as a room template, and instantiating it is the whole of the setup:</p>
426
- <pre><code class="typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">Briyah</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;briyah&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({ </span><span class="hl-4">dataPath:</span><span class="hl-1"> </span><span class="hl-2">&#39;./briyah-data&#39;</span><span class="hl-1"> });</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">init</span><span class="hl-1">();</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">app</span><span class="hl-1"> = </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-2">&#39;cli-user&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-4">packs</span><span class="hl-1">.</span><span class="hl-0">setEnabled</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e&#39;</span><span class="hl-1">, </span><span class="hl-6">true</span><span class="hl-1">); </span><span class="hl-5">// per user, and required</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> { </span><span class="hl-7">roomId</span><span class="hl-1">, </span><span class="hl-7">warnings</span><span class="hl-1"> } = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-4">packInstantiation</span><span class="hl-1">.</span><span class="hl-0">instantiateRoom</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;dnd5e&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;table&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">primary:</span><span class="hl-1"> { </span><span class="hl-4">aiServiceName:</span><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-4">aiModel:</span><span class="hl-1"> </span><span class="hl-2">&#39;claude-sonnet-5&#39;</span><span class="hl-1"> },</span><br/><span class="hl-1"> </span><span class="hl-4">economy:</span><span class="hl-1"> { </span><span class="hl-4">aiServiceName:</span><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-4">aiModel:</span><span class="hl-1"> </span><span class="hl-2">&#39;claude-haiku-4-5&#39;</span><span class="hl-1"> },</span><br/><span class="hl-1"> },</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Campaign — Homer&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> { </span><span class="hl-4">player:</span><span class="hl-1"> { </span><span class="hl-4">agentName:</span><span class="hl-1"> </span><span class="hl-2">&#39;Homer&#39;</span><span class="hl-1">, </span><span class="hl-4">agentNickname:</span><span class="hl-1"> </span><span class="hl-2">&#39;Homer&#39;</span><span class="hl-1"> } },</span><br/><span class="hl-1">);</span><br/><span class="hl-4">warnings</span><span class="hl-1">.</span><span class="hl-0">forEach</span><span class="hl-1">((</span><span class="hl-4">warning</span><span class="hl-1">) </span><span class="hl-6">=&gt;</span><span class="hl-1"> </span><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">warn</span><span class="hl-1">(</span><span class="hl-4">warning</span><span class="hl-1">));</span>
427
- </code><button type="button">Copy</button></pre>
428
-
429
- <p>That is six agents — moderator, Dungeon Master, battle master, character creator, campaign
430
- designer, and Homer at the keyboard — each with its folder's tool grants and rules documents,
431
- <code>turnMode: 'directed'</code>, the Dungeon Master leading, and the moderator created before the agents
432
- that route through it. Then run the loop from Example 10.</p>
433
- <p><strong>The goal.</strong> The template's goal describes the game in general. A first session usually wants
434
- one that says what has <em>not</em> happened yet, or the Dungeon Master opens a scene for characters
435
- who have no sheets:</p>
436
- <pre><code class="typescript"><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">editRoom</span><span class="hl-1">(</span><span class="hl-4">roomId</span><span class="hl-1">, </span><span class="hl-2">&#39;Campaign — Homer&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;A fifth-edition campaign for Homer. Nothing is prepared: no campaign has been designed &#39;</span><span class="hl-1"> +</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;and no character has a sheet. Run session zero first — have the Campaign Designer &#39;</span><span class="hl-1"> +</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;interview Homer and publish the Campaign document, then hand the Character Creator one &#39;</span><span class="hl-1"> +</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;player at a time until every sheet exists — and only then open the first scene.&#39;</span><span class="hl-1">);</span>
437
- </code><button type="button">Copy</button></pre>
438
-
439
- <p><strong>Models by role.</strong> The Dungeon Master reasons and makes every tool call, and is the only agent
440
- the template puts on the primary model. Everyone else is <code>economy</code>, including the campaign
441
- designer, whose output is long but infrequent:</p>
442
- <table>
443
- <thead>
444
- <tr>
445
- <th>Role</th>
446
- <th><code>modelRole</code></th>
447
- <th>Suggested</th>
448
- </tr>
449
- </thead>
450
- <tbody>
451
- <tr>
452
- <td>Dungeon Master</td>
453
- <td><code>primary</code></td>
454
- <td><code>claude-sonnet-5</code></td>
455
- </tr>
456
- <tr>
457
- <td>Battle master, character creator, campaign designer, moderator</td>
458
- <td><code>economy</code></td>
459
- <td><code>claude-haiku-4-5</code></td>
460
- </tr>
461
- <tr>
462
- <td>Players, human and AI</td>
463
- <td><code>economy</code></td>
464
- <td><code>claude-haiku-4-5</code></td>
465
- </tr>
466
- </tbody>
467
- </table>
468
- <p>Pass only <code>primary</code> and everything runs on it.</p>
469
- <p><strong>The moderator, and what it saves your loop.</strong> The shipped table seats one
470
- (<code>dnd5e:dnd_room_moderator</code>), and the player template sets <code>useModerator: true</code>. That pairing is
471
- not decoration: when a human agent has the flag, the room forces their message to <code>moderate</code> and
472
- clears its targets — server-side, whatever action you passed — and the moderator decides who
473
- hears it and who answers. A person typing a line does not know whether they are answering the
474
- designer's interview question or the DM's &quot;what do you do&quot;, and with a moderator they do not have
475
- to: the <code>replyTo</code>/<code>replyAs</code> mirroring in Example 10 exists only because that table has none.</p>
476
- <p>Skip the moderator when nothing addresses the player except the leader. Then the Dungeon Master
477
- names who acts next in a <code>directed</code> room, which is the same job for less money. If any agent has
478
- <code>useModerator: true</code>, the room must name a moderator or <code>createRoom</code> raises a <code>ValidationError</code>.</p>
479
- <p><strong>Building it by hand.</strong> If you want the pieces rather than the template, <code>createAgentFromFolder</code>
480
- reads a folder's <code>defaults_config.json</code> and applies all of it — the same wiring the web client's
481
- create-agent dialog performs — so you never transcribe eighteen tool names or thirteen document
482
- ids, and nothing drifts when the pack changes:</p>
483
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-0">from</span><span class="hl-1"> = (</span><span class="hl-4">folder</span><span class="hl-1">: </span><span class="hl-9">string</span><span class="hl-1">, </span><span class="hl-4">aiModel</span><span class="hl-1">: </span><span class="hl-9">string</span><span class="hl-1">, </span><span class="hl-4">overrides</span><span class="hl-1"> = {}) </span><span class="hl-6">=&gt;</span><br/><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">createAgentFromFolder</span><span class="hl-1">({ </span><span class="hl-4">folder</span><span class="hl-1">, </span><span class="hl-4">aiServiceName:</span><span class="hl-1"> </span><span class="hl-2">&#39;Anthropic&#39;</span><span class="hl-1">, </span><span class="hl-4">aiModel</span><span class="hl-1">, </span><span class="hl-4">overrides</span><span class="hl-1"> });</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">mod</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">from</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e:dnd_room_moderator&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;claude-haiku-4-5&#39;</span><span class="hl-1">);</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">dm</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">from</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e:dungeon_master&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;claude-sonnet-5&#39;</span><span class="hl-1">);</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">marshal</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">from</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e:battle_master&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;claude-haiku-4-5&#39;</span><span class="hl-1">);</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">designer</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">from</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e:campaign_designer&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;claude-haiku-4-5&#39;</span><span class="hl-1">);</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">creator</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">from</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e:character_creator&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;claude-haiku-4-5&#39;</span><span class="hl-1">);</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">you</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">from</span><span class="hl-1">(</span><span class="hl-2">&#39;dnd5e:dnd_player&#39;</span><span class="hl-1">, </span><span class="hl-2">&#39;claude-haiku-4-5&#39;</span><span class="hl-1">, {</span><br/><span class="hl-1"> </span><span class="hl-4">agentName:</span><span class="hl-1"> </span><span class="hl-2">&#39;Homer&#39;</span><span class="hl-1">, </span><span class="hl-4">agentNickname:</span><span class="hl-1"> </span><span class="hl-2">&#39;Homer&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">description:</span><span class="hl-1"> </span><span class="hl-2">&#39;A player character with no sheet yet, played by a person.&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">controlledByHuman:</span><span class="hl-1"> </span><span class="hl-6">true</span><span class="hl-1">, </span><span class="hl-4">useModerator:</span><span class="hl-1"> </span><span class="hl-6">true</span><span class="hl-1">,</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> { </span><span class="hl-7">roomId</span><span class="hl-1"> } = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">app</span><span class="hl-1">.</span><span class="hl-0">createRoom</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Campaign — Homer&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;A fifth-edition campaign for Homer. ...&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> [</span><span class="hl-4">mod</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">!, </span><span class="hl-4">dm</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">!, </span><span class="hl-4">marshal</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">!, </span><span class="hl-4">designer</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">!, </span><span class="hl-4">creator</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">!, </span><span class="hl-4">you</span><span class="hl-1">.</span><span class="hl-4">id</span><span class="hl-1">!],</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;DM&#39;</span><span class="hl-1">, </span><span class="hl-5">// roomLeader</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;directed&#39;</span><span class="hl-1">, </span><span class="hl-5">// turnMode</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;Moderator&#39;</span><span class="hl-1">, </span><span class="hl-5">// moderator — required, because Homer has useModerator</span><br/><span class="hl-1">);</span>
484
- </code><button type="button">Copy</button></pre>
485
-
486
- <p>The nicknames come from the folders: <code>Moderator</code>, <code>DM</code>, <code>Marshal</code>, <code>Designer</code>, <code>Creator</code>. These
487
- agents are free-standing rather than room-owned, so they appear in <code>listAgents()</code> and outlive the
488
- room; <code>createRoomAgentFromFolder</code> is the room-owned equivalent, and is what the template uses.</p>
489
- <p><strong>Resuming.</strong> <code>listRooms()</code> gives saved campaigns; <code>getRoomDetails(roomId)</code> gives the roster,
490
- where <code>agents[].controlledByHuman</code> identifies the person's character. Then
491
- <code>ensureRoomStateCallback(roomId)</code> before subscribing — without it the emitter on a resumed room
492
- never fires.</p>
493
- <h2 id="data-storage" class="tsd-anchor-link">Data Storage<a href="#data-storage" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
494
- <p>Briyah stores data in the following structure:</p>
495
- <pre><code><span class="hl-1">{</span><span class="hl-4">dataPath</span><span class="hl-1">}/</span><br/><span class="hl-1">├── </span><span class="hl-4">common</span><span class="hl-1">/ # </span><span class="hl-4">Shared</span><span class="hl-1"> </span><span class="hl-4">across</span><span class="hl-1"> </span><span class="hl-4">all</span><span class="hl-1"> </span><span class="hl-4">users</span><br/><span class="hl-1">│ ├── </span><span class="hl-4">config</span><span class="hl-1">/ # </span><span class="hl-7">LLM</span><span class="hl-1"> </span><span class="hl-4">pricing</span><span class="hl-1"> </span><span class="hl-4">and</span><span class="hl-1"> </span><span class="hl-4">image</span><span class="hl-1">-</span><span class="hl-4">model</span><span class="hl-1"> </span><span class="hl-4">config</span><br/><span class="hl-1">│ ├── </span><span class="hl-4">prompts</span><span class="hl-1">/ # </span><span class="hl-4">Global</span><span class="hl-1"> </span><span class="hl-4">prompt</span><span class="hl-1"> </span><span class="hl-4">templates</span><br/><span class="hl-1">│ └── </span><span class="hl-4">published</span><span class="hl-1">-</span><span class="hl-4">agents</span><span class="hl-1">.</span><span class="hl-4">json</span><span class="hl-1"> # </span><span class="hl-4">Published</span><span class="hl-1"> </span><span class="hl-4">agent</span><span class="hl-1"> </span><span class="hl-4">mappings</span><br/><span class="hl-1">├── </span><span class="hl-4">packs</span><span class="hl-1">/ # </span><span class="hl-4">Installed</span><span class="hl-1"> </span><span class="hl-4">packs</span><span class="hl-1">, </span><span class="hl-4">one</span><span class="hl-1"> </span><span class="hl-4">directory</span><span class="hl-1"> </span><span class="hl-4">each</span><br/><span class="hl-1">│ └── {</span><span class="hl-4">pack</span><span class="hl-1">}/ # </span><span class="hl-4">pack</span><span class="hl-1">.</span><span class="hl-4">json</span><span class="hl-1">, </span><span class="hl-4">prompts</span><span class="hl-1">/, </span><span class="hl-4">artifacts</span><span class="hl-1">/, </span><span class="hl-4">agents</span><span class="hl-1">/, </span><span class="hl-4">rooms</span><span class="hl-1">/</span><br/><span class="hl-1">├── </span><span class="hl-4">retired</span><span class="hl-1">-</span><span class="hl-4">prompts</span><span class="hl-1">/ # </span><span class="hl-4">Folders</span><span class="hl-1"> </span><span class="hl-4">a</span><span class="hl-1"> </span><span class="hl-4">pack</span><span class="hl-1"> </span><span class="hl-4">took</span><span class="hl-1"> </span><span class="hl-4">over</span><span class="hl-1">; </span><span class="hl-4">see</span><span class="hl-1"> </span><span class="hl-2">&quot;Upgrading to 2.0&quot;</span><br/><span class="hl-1">├── </span><span class="hl-4">logs</span><span class="hl-1">/ # </span><span class="hl-4">briyah</span><span class="hl-1">.</span><span class="hl-4">log</span><br/><span class="hl-1">└── </span><span class="hl-4">user</span><span class="hl-1">/</span><br/><span class="hl-1"> └── {</span><span class="hl-4">userId</span><span class="hl-1">}/ # </span><span class="hl-4">Per</span><span class="hl-1">-</span><span class="hl-4">user</span><span class="hl-1"> </span><span class="hl-4">data</span><br/><span class="hl-1"> ├── </span><span class="hl-4">agents</span><span class="hl-1">/ # </span><span class="hl-4">Agent</span><span class="hl-1"> </span><span class="hl-4">configs</span><span class="hl-1"> </span><span class="hl-4">and</span><span class="hl-1"> </span><span class="hl-0">histories</span><span class="hl-1"> (</span><span class="hl-4">one</span><span class="hl-1"> </span><span class="hl-7">JSON</span><span class="hl-1"> </span><span class="hl-4">per</span><span class="hl-1"> </span><span class="hl-4">agent</span><span class="hl-1">)</span><br/><span class="hl-1"> ├── </span><span class="hl-4">rooms</span><span class="hl-1">/ # </span><span class="hl-4">Room</span><span class="hl-1"> </span><span class="hl-4">metadata</span><span class="hl-1">, </span><span class="hl-4">message</span><span class="hl-1"> </span><span class="hl-4">logs</span><span class="hl-1">, </span><span class="hl-0">artifacts</span><span class="hl-1"> (</span><span class="hl-4">one</span><span class="hl-1"> </span><span class="hl-4">folder</span><span class="hl-1"> </span><span class="hl-4">per</span><span class="hl-1"> </span><span class="hl-4">room</span><span class="hl-1">)</span><br/><span class="hl-1"> ├── </span><span class="hl-4">stories</span><span class="hl-1">/ # </span><span class="hl-4">Story</span><span class="hl-1"> </span><span class="hl-4">state</span><br/><span class="hl-1"> ├── </span><span class="hl-4">tools</span><span class="hl-1">/ # </span><span class="hl-4">User</span><span class="hl-1">-</span><span class="hl-4">defined</span><span class="hl-1"> </span><span class="hl-0">tools</span><span class="hl-1"> (</span><span class="hl-4">one</span><span class="hl-1"> </span><span class="hl-7">JSON</span><span class="hl-1"> </span><span class="hl-4">per</span><span class="hl-1"> </span><span class="hl-4">tool</span><span class="hl-1">)</span><br/><span class="hl-1"> ├── </span><span class="hl-4">prompts</span><span class="hl-1">/ # </span><span class="hl-4">User</span><span class="hl-1">-</span><span class="hl-4">specific</span><span class="hl-1"> </span><span class="hl-4">prompts</span><br/><span class="hl-1"> ├── </span><span class="hl-4">packs</span><span class="hl-1">/{</span><span class="hl-4">pack</span><span class="hl-1">}/ # </span><span class="hl-4">This</span><span class="hl-1"> </span><span class="hl-4">user</span><span class="hl-2">&#39;s edits to a pack, or a pack they create</span><span class="hl-12">d</span><br/><span class="hl-1"> ├── </span><span class="hl-4">artifacts</span><span class="hl-1">/ # </span><span class="hl-4">This</span><span class="hl-1"> </span><span class="hl-4">user</span><span class="hl-2">&#39;s own document</span><span class="hl-12">s</span><br/><span class="hl-1"> ├── </span><span class="hl-4">upload</span><span class="hl-1">/ # </span><span class="hl-4">User</span><span class="hl-1"> </span><span class="hl-4">file</span><span class="hl-1"> </span><span class="hl-4">attachments</span><br/><span class="hl-1"> ├── </span><span class="hl-4">transactions</span><span class="hl-1">/ # </span><span class="hl-4">Transaction</span><span class="hl-1"> </span><span class="hl-4">records</span><br/><span class="hl-1"> └── </span><span class="hl-4">userconfig</span><span class="hl-1">/ # </span><span class="hl-4">User</span><span class="hl-1"> </span><span class="hl-4">preferences</span><span class="hl-1">, </span><span class="hl-4">balance</span><span class="hl-1">, </span><span class="hl-4">and</span><span class="hl-1"> </span><span class="hl-4">enabled</span><span class="hl-1"> </span><span class="hl-4">packs</span>
496
- </code><button>Copy</button></pre>
497
-
498
- <p><code>init()</code> seeds <code>common/</code> additively and <strong>replaces</strong> every bundled pack under <code>packs/</code> — see
499
- <a href="#upgrading-to-20">Upgrading to 2.0</a> for exactly what is rewritten and where your own edits
500
- belong instead.</p>
501
- <h2 id="multi-instance-support" class="tsd-anchor-link">Multi-Instance Support<a href="#multi-instance-support" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
502
- <p>You can create multiple Briyah instances with different configurations:</p>
503
- <pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah1</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({ </span><span class="hl-4">dataPath:</span><span class="hl-1"> </span><span class="hl-2">&#39;./data1&#39;</span><span class="hl-1"> });</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">briyah2</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Briyah</span><span class="hl-1">({ </span><span class="hl-4">dataPath:</span><span class="hl-1"> </span><span class="hl-2">&#39;./data2&#39;</span><span class="hl-1"> });</span><br/><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">briyah1</span><span class="hl-1">.</span><span class="hl-0">init</span><span class="hl-1">();</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">briyah2</span><span class="hl-1">.</span><span class="hl-0">init</span><span class="hl-1">();</span><br/><br/><span class="hl-5">// Each instance has isolated data</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">user1Service</span><span class="hl-1"> = </span><span class="hl-4">briyah1</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-2">&#39;user-123&#39;</span><span class="hl-1">);</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">user2Service</span><span class="hl-1"> = </span><span class="hl-4">briyah2</span><span class="hl-1">.</span><span class="hl-0">getAppService</span><span class="hl-1">(</span><span class="hl-2">&#39;user-123&#39;</span><span class="hl-1">);</span><br/><span class="hl-5">// These are completely separate services with different data paths</span>
504
- </code><button type="button">Copy</button></pre>
505
-
506
- <p><strong>Note:</strong> Singleton services (LLM providers, message emitters) are shared across all Briyah instances within the same process. This is safe because they are stateless or keyed by ID.</p>
507
- <h2 id="troubleshooting" class="tsd-anchor-link">Troubleshooting<a href="#troubleshooting" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
508
- <h3 id="must-call-init-before-getappservice" class="tsd-anchor-link">&quot;Must call init() before getAppService()&quot;<a href="#must-call-init-before-getappservice" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
509
- <p>Make sure to call <code>await briyah.init()</code> before calling <code>getAppService()</code>.</p>
510
- <h3 id="no-llm-provider-api-keys-configured" class="tsd-anchor-link">&quot;No LLM provider API keys configured&quot;<a href="#no-llm-provider-api-keys-configured" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
511
- <p>Set at least one of the required API key environment variables (ANTHROPIC_API_KEY, OPENAI_API_KEY, etc.).</p>
512
- <h3 id="starting_balance-environment-variable-is-required" class="tsd-anchor-link">&quot;STARTING_BALANCE environment variable is required&quot;<a href="#starting_balance-environment-variable-is-required" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
513
- <p>Set the <code>STARTING_BALANCE</code> environment variable or pass <code>startingBalance</code> in the constructor options.</p>
514
- <h3 id="cache-not-working-as-expected" class="tsd-anchor-link">Cache not working as expected<a href="#cache-not-working-as-expected" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
515
- <p>Check the cache statistics with <code>getCacheStats()</code> and adjust <code>userServiceCacheTimeoutMinutes</code> in the constructor options.</p>
516
- </div></div>
517
- <div class="col-sidebar">
518
- <div class="page-menu">
519
- <div class="tsd-navigation settings">
520
- <details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg>
521
- <h3>Settings</h3></summary>
522
- <div class="tsd-accordion-details">
523
- <div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span>
524
- <ul id="tsd-filter-options">
525
- <li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li>
526
- <li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li>
527
- <li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div>
528
- <div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div>
529
- <details open class="tsd-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg>
530
- <h3>On This Page</h3></summary>
531
- <div class="tsd-accordion-details"><a href="#briyah-sdk"><span>Briyah <wbr/>SDK</span></a>
532
- <ul>
533
- <li><a href="#installation"><span>Installation</span></a></li>
534
- <li><a href="#quick-start"><span>Quick <wbr/>Start</span></a></li>
535
- <li><a href="#upgrading-to-20"><span>Upgrading to 2.0</span></a></li>
536
- <li><a href="#configuration"><span>Configuration</span></a></li>
537
- <li>
538
- <ul>
539
- <li><a href="#briyahconfigoptions"><span>Briyah<wbr/>Config<wbr/>Options</span></a></li>
540
- <li><a href="#environment-variables"><span>Environment <wbr/>Variables</span></a></li>
541
- <li><a href="#logging"><span>Logging</span></a></li></ul></li>
542
- <li><a href="#api-reference"><span>API <wbr/>Reference</span></a></li>
543
- <li>
544
- <ul>
545
- <li><a href="#briyah-class"><span>Briyah <wbr/>Class</span></a></li>
546
- <li>
547
- <ul>
548
- <li><a href="#constructoroptions-briyahconfigoptions"><span>constructor(options?: <wbr/>Briyah<wbr/>Config<wbr/>Options)</span></a></li>
549
- <li><a href="#async-init-promisevoid"><span>async init(): <wbr/>Promise&lt;void&gt;</span></a></li>
550
- <li><a href="#getappserviceuserid-string-appservice"><span>get<wbr/>App<wbr/>Service(user<wbr/>Id: string): <wbr/>App<wbr/>Service</span></a></li>
551
- <li><a href="#removeuserserviceuserid-string-void"><span>remove<wbr/>User<wbr/>Service(user<wbr/>Id: string): void</span></a></li>
552
- <li><a href="#getcachestats"><span>get<wbr/>Cache<wbr/>Stats()</span></a></li>
553
- <li><a href="#async-shutdown-promisevoid"><span>async shutdown(): <wbr/>Promise&lt;void&gt;</span></a></li>
554
- <li><a href="#isinitialized-boolean"><span>is<wbr/>Initialized(): boolean</span></a></li></ul></li>
555
- <li><a href="#appservice"><span>App<wbr/>Service</span></a></li></ul></li>
556
- <li><a href="#prompt-folders"><span>Prompt <wbr/>Folders</span></a></li>
557
- <li>
558
- <ul>
559
- <li><a href="#when-a-folder-name-is-wrong"><span>When a folder name is wrong</span></a></li></ul></li>
560
- <li><a href="#packs"><span>Packs</span></a></li>
561
- <li>
562
- <ul>
563
- <li><a href="#installed-for-everyone-enabled-per-user"><span>Installed for everyone, enabled per user</span></a></li>
564
- <li><a href="#building-a-room-from-a-template"><span>Building a room from a template</span></a></li>
565
- <li><a href="#building-the-same-room-by-hand"><span>Building the same room by hand</span></a></li></ul></li>
566
- <li><a href="#usage-examples"><span>Usage <wbr/>Examples</span></a></li>
567
- <li>
568
- <ul>
569
- <li><a href="#example-1-multi-user-setup"><span>Example 1: <wbr/>Multi-<wbr/>User <wbr/>Setup</span></a></li>
570
- <li><a href="#example-2-custom-data-path"><span>Example 2: <wbr/>Custom <wbr/>Data <wbr/>Path</span></a></li>
571
- <li><a href="#example-3-testing-configuration"><span>Example 3: <wbr/>Testing <wbr/>Configuration</span></a></li>
572
- <li><a href="#example-4-agent-creation-and-text-processing"><span>Example 4: <wbr/>Agent <wbr/>Creation and <wbr/>Text <wbr/>Processing</span></a></li>
573
- <li><a href="#example-5-multi-agent-room-conversation"><span>Example 5: <wbr/>Multi-<wbr/>Agent <wbr/>Room <wbr/>Conversation</span></a></li>
574
- <li><a href="#example-6-story-message-emitter"><span>Example 6: <wbr/>Story <wbr/>Message <wbr/>Emitter</span></a></li>
575
- <li><a href="#example-7-recording-payments-and-managing-balance"><span>Example 7: <wbr/>Recording <wbr/>Payments and <wbr/>Managing <wbr/>Balance</span></a></li>
576
- <li><a href="#example-8-registering-native-tools-with-opaque-context"><span>Example 8: <wbr/>Registering <wbr/>Native <wbr/>Tools with <wbr/>Opaque <wbr/>Context</span></a></li>
577
- <li><a href="#example-9-self-hosted-image-generation-with-comfyui"><span>Example 9: <wbr/>Self-<wbr/>Hosted <wbr/>Image <wbr/>Generation with <wbr/>Comfy<wbr/>UI</span></a></li>
578
- <li><a href="#example-10-a-turn-based-table-with-a-human-player"><span>Example 10: <wbr/>A turn-<wbr/>based table with a human player</span></a></li>
579
- <li><a href="#example-11-seating-a-whole-dd-table"><span>Example 11: <wbr/>Seating a whole <wbr/>D&amp;<wbr/>D table</span></a></li></ul></li>
580
- <li><a href="#data-storage"><span>Data <wbr/>Storage</span></a></li>
581
- <li><a href="#multi-instance-support"><span>Multi-<wbr/>Instance <wbr/>Support</span></a></li>
582
- <li><a href="#troubleshooting"><span>Troubleshooting</span></a></li>
583
- <li>
584
- <ul>
585
- <li><a href="#must-call-init-before-getappservice"><span>&quot;<wbr/>Must call init() before get<wbr/>App<wbr/>Service()&quot;</span></a></li>
586
- <li><a href="#no-llm-provider-api-keys-configured"><span>&quot;<wbr/>No <wbr/>LLM provider <wbr/>API keys configured&quot;</span></a></li>
587
- <li><a href="#starting_balance-environment-variable-is-required"><span>&quot;<wbr/>STARTING_<wbr/>BALANCE environment variable is required&quot;</span></a></li>
588
- <li><a href="#cache-not-working-as-expected"><span>Cache not working as expected</span></a></li></ul></li></ul></div></details></div>
589
- <div class="site-menu">
590
- <nav id="tsd-sidebar-links" class="tsd-navigation"><a href="https://github.com/your-org/briyah" class="tsd-nav-link">GitHub</a><a href="https://www.npmjs.com/package/briyah" class="tsd-nav-link">npm</a></nav>
591
- <nav class="tsd-navigation"><a href="modules.html">Briyah SDK - v2.0.3</a>
592
- <ul class="tsd-small-nested-navigation" id="tsd-nav-container">
593
- <li>Loading...</li></ul></nav></div></div></div><footer>
594
- <p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer>
595
- <div class="overlay"></div></body></html>