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
@@ -1,25 +1,102 @@
1
1
  import { Agent } from '../ai/agent';
2
- import { RoomTurnMode, SendMessageResult } from '../../../shared/types/app.types';
2
+ import { RoomIllustrationMode, RoomTurnMode, SendMessageResult } from '../../../shared/types/app.types';
3
3
  import { RoomMessage } from './message';
4
4
  import { Artifact } from './artifact';
5
5
  import { ToolStoreService } from '../tools/tool-store.service';
6
6
  import { ToolExecutionService } from '../tools/tool-execution.service';
7
+ import { IllustrationService } from './illustration/illustration.service';
8
+ /** What a rename did, or why it did nothing. */
7
9
  export interface RenameResult {
8
10
  renamed: boolean;
11
+ /** The nickname the agent had. */
9
12
  from?: string;
13
+ /** The nickname it has now. */
10
14
  to?: string;
15
+ /** The full name it has now, which may be longer than the nickname. */
11
16
  fullName?: string;
12
17
  reason?: 'not_found' | 'ambiguous' | 'invalid' | 'unchanged';
18
+ /** For `ambiguous`, the agent the requested name would have collided with. */
13
19
  conflictsWith?: string;
20
+ /** How many artifacts had their creator or viewers rewritten. */
14
21
  artifactsUpdated?: number;
15
22
  }
16
23
  export declare class Room {
24
+ /**
25
+ * Maximum EXECUTE/PUBLISH chain length within a single agent turn.
26
+ *
27
+ * A backstop against an agent looping on its tools forever, not a budget. Five
28
+ * was too tight for anything with real bookkeeping: a single round of combat runs
29
+ * a spell lookup, a resource spend, the attack and damage rolls, the hit point
30
+ * change and then the narration, and a character build runs create, read and
31
+ * patch before the hand-back. Half of one campaign's Dungeon Master turns hit the
32
+ * limit, and hitting it is expensive in its own right — the turn is re-prompted,
33
+ * the work is often redone, and a person has to notice and say "carry on".
34
+ *
35
+ * Raise it with `ROOM_MAX_TOOL_ITERATIONS`. Read per turn so it can be retuned on
36
+ * a running server, and floored at 1 so a nonsense value cannot stop every agent
37
+ * from acting.
38
+ *
39
+ * @returns The number of tool calls an agent may chain in one turn
40
+ */
17
41
  private static maxToolIterations;
42
+ /** What an agent is told when it is prompted again because it still holds the floor. */
18
43
  private static readonly CONTINUE_NOTE;
44
+ /**
45
+ * How many times in a row an agent may be handed the floor back to itself.
46
+ *
47
+ * A self-loop normally means a stuck room, which is why both routing paths broke
48
+ * one unconditionally. But an agent sometimes genuinely still holds the floor: a
49
+ * Battle Master running consecutive monster turns has nobody to address — a
50
+ * monster's turn has no addressee — and when every player character is unconscious
51
+ * there is no player to hand to either. That is exactly where one campaign stalled,
52
+ * needing a person to type "Marshal, continue" seven times in one fight.
53
+ *
54
+ * So the loop is allowed, and bounded. Three is enough for a few rounds of a fight
55
+ * nobody is awake for, and short enough that a genuinely stuck agent is stopped
56
+ * before it costs much. Raise it with `ROOM_MAX_SELF_CONTINUES`.
57
+ *
58
+ * @returns The number of consecutive self-addressed turns permitted
59
+ */
19
60
  private static maxSelfContinues;
61
+ /**
62
+ * How many stalled turns in a row the room may re-route before it gives up.
63
+ *
64
+ * Counted since the last word actually said, not since the room opened: any
65
+ * audible message resets it, so a table that stalls once an hour never runs out.
66
+ * The cap exists for the case where re-routing cannot help — every agent in turn
67
+ * declining the same message — and the room would otherwise pay a moderator call
68
+ * per attempt forever with nobody at the keyboard to stop it.
69
+ *
70
+ * Raise it with `ROOM_MAX_STALL_RECOVERIES`; zero switches recovery off.
71
+ *
72
+ * @returns The number of consecutive recoveries permitted
73
+ */
20
74
  private static maxStallRecoveries;
75
+ /**
76
+ * What the moderator is told when it is asked to re-route a stalled message.
77
+ * @param sender Who sent the message nobody answered
78
+ * @param declined Everyone that has already been asked to answer it
79
+ * @returns A note for the moderator's transient prompt
80
+ */
21
81
  private static rerouteNote;
82
+ /**
83
+ * What the re-routed agent is told, so it can tell this from being handed a
84
+ * message it has already seen answered.
85
+ * @param sender Who sent the message
86
+ * @param declined Everyone that was asked before it
87
+ * @returns A system note for the agent's history
88
+ */
22
89
  private static rerouteHandoffNote;
90
+ /**
91
+ * What the sender is told when its own unanswered message comes back to it.
92
+ *
93
+ * The last resort, and at a game table not a rare one: the leader is usually the
94
+ * one who addressed the wrong agent, so it is both the sender and the fallback.
95
+ * Being told to try again is a real recovery — it is the only agent that knows
96
+ * who the message was meant for.
97
+ * @param declined Everyone that was asked and did not answer
98
+ * @returns A system note for the sender's history
99
+ */
23
100
  private static retryHandoffNote;
24
101
  private id;
25
102
  private name;
@@ -29,6 +106,16 @@ export declare class Room {
29
106
  private roomLeader;
30
107
  private turnMode;
31
108
  private moderator;
109
+ /**
110
+ * Who heard the message the moderator routed last, told to the moderator on its
111
+ * next turn.
112
+ *
113
+ * What makes a conversation with a sub-group hold together: speak to three of six
114
+ * agents, and the follow-up should reach the same three rather than whoever the
115
+ * moderator reconstructs from a 16-message history. Kept in memory only — losing
116
+ * it across a restart costs one turn of continuity, which is not worth persisting
117
+ * for.
118
+ */
32
119
  private lastModeratedAudience;
33
120
  private roomMessages;
34
121
  private pendingMessages;
@@ -39,9 +126,17 @@ export declare class Room {
39
126
  private processingInProgress;
40
127
  private isPaused;
41
128
  private currentSpeaker;
129
+ /** The agent currently keeping the floor by addressing itself, if any. */
42
130
  private selfContinuingAgent;
131
+ /** How many turns in a row {@link selfContinuingAgent} has held it. */
43
132
  private consecutiveSelfContinues;
133
+ /**
134
+ * The last audible message this room asked somebody to answer, and who was
135
+ * asked. Read only by {@link recoverStalledFloor}. In memory only: a restart
136
+ * ends any turn that was in flight, so there is nothing left to re-route.
137
+ */
44
138
  private lastDispatch;
139
+ /** Consecutive stalls re-routed since the last word said. See {@link maxStallRecoveries}. */
45
140
  private stallRecoveries;
46
141
  private isPublished;
47
142
  private isLoading;
@@ -52,13 +147,40 @@ export declare class Room {
52
147
  private onSituationUpdate?;
53
148
  private onModeratorResponseCallback?;
54
149
  private templateRoomId;
150
+ /** The pack this room is filed under. See {@link RoomConfig.pack}. */
55
151
  private pack;
152
+ /** The pack template's key, when this room came from one. */
56
153
  private packRoomKey;
154
+ /** What Begin sends to the leader. See {@link RoomConfig.beginInstruction}. */
155
+ private beginInstruction;
156
+ /** Which image_models.json entry draws this room. See {@link RoomConfig.imageModel}. */
157
+ private imageModel;
158
+ /** When the room illustrates itself. See {@link RoomConfig.illustrationMode}. */
159
+ private illustrationMode;
160
+ /**
161
+ * Agents that work for this room but never take a turn in it: the illustrator
162
+ * and the two artists.
163
+ *
164
+ * Kept out of `agents` deliberately. In broadcast turn mode `selectResponders`
165
+ * returns every agent in the room, so a member illustrator would be prompted
166
+ * on every message. They are here only so their spend is counted — see
167
+ * {@link totalCost}.
168
+ */
169
+ private attachedAgents;
57
170
  private storageDir;
58
171
  private roomStoreService;
59
172
  private toolStoreService?;
60
173
  private toolExecutionService?;
61
174
  private currentToolContext;
175
+ private illustrationService?;
176
+ /**
177
+ * Images each agent has asked for during its current turn, keyed by nickname.
178
+ * An image costs real money and takes minutes, so an agent gets one per turn;
179
+ * `runAgentTurn` clears its entry as the turn begins.
180
+ */
181
+ private imagesThisTurn;
182
+ /** One picture per agent per turn. */
183
+ static readonly IMAGES_PER_TURN = 1;
62
184
  constructor(id: string | null, name: string, goal: string, baseRoomDir?: string, storageDir?: string, roomStoreService?: any);
63
185
  deleteRoom(): void;
64
186
  setLoading(isLoading: boolean): void;
@@ -73,31 +195,187 @@ export declare class Room {
73
195
  getPaused(): boolean;
74
196
  getTemplateRoomId(): string | undefined;
75
197
  setTemplateRoomId(templateId: string): void;
198
+ /** @returns The pack this room is filed under, or undefined when it is not filed */
76
199
  getPack(): string | undefined;
200
+ /**
201
+ * Files this room under a pack, or clears it.
202
+ * @param pack The pack name; empty or undefined clears it
203
+ */
77
204
  setPack(pack: string | undefined): void;
205
+ /** @returns The pack template's key, when this room came from one */
78
206
  getPackRoomKey(): string | undefined;
79
207
  setPackRoomKey(key: string | undefined): void;
208
+ /** @returns What Begin sends, or '' when this room offers no Begin */
209
+ getBeginInstruction(): string;
210
+ /**
211
+ * Sets what Begin sends to the room leader.
212
+ *
213
+ * Normalized to '' rather than left undefined, the same way `getModerator()`
214
+ * normalizes: a settings snapshot read from a room that was never given one
215
+ * must equal a snapshot of a room that was given a blank one, or the
216
+ * round-trip through {@link RoomStoreService.applyRoomSettings} is not one.
217
+ * @param instruction The instruction; empty or undefined clears it
218
+ */
219
+ setBeginInstruction(instruction: string | undefined): void;
220
+ /** @returns The image_models.json entry name, or undefined to use the env default */
221
+ getImageModel(): string | undefined;
222
+ setImageModel(model: string | undefined): void;
223
+ /**
224
+ * @returns When this room illustrates itself. Normalized the way
225
+ * `getModerator()` and `getBeginInstruction()` are, so that a snapshot of a
226
+ * room never given one equals a snapshot of a room given the default.
227
+ */
228
+ getIllustrationMode(): RoomIllustrationMode;
229
+ setIllustrationMode(mode: RoomIllustrationMode | undefined): void;
230
+ /**
231
+ * Registers an agent that works for this room without ever taking a turn.
232
+ * Idempotent, since the illustration service resolves its agents on every call.
233
+ * @param agent The off-roster agent whose spend belongs to this room
234
+ */
235
+ attachAgent(agent: Agent): void;
236
+ /** @returns The off-roster agents, e.g. for cleanup when the room is deleted */
237
+ getAttachedAgents(): Agent[];
238
+ /**
239
+ * What this room has cost so far.
240
+ *
241
+ * The one place that knows the illustrator and artists are not in `agents`.
242
+ * Callers that used to reduce over `getAgents()` must use this instead, or
243
+ * image spend is invisible.
244
+ */
245
+ totalCost(): number;
80
246
  clearRoomMessages(): void;
247
+ /**
248
+ * Adds an agent to the room
249
+ * @param agent The agent to add
250
+ * @returns True if the agent was added, false if it was already in the room
251
+ */
81
252
  arrive(agent: Agent): boolean;
82
253
  getAgents(): Agent[];
254
+ /**
255
+ * Replaces all agents in the room with new agents
256
+ * @param newAgents Array of agents to set
257
+ */
83
258
  setAgents(newAgents: Agent[]): void;
84
259
  describeAgentsInRoom(): string;
85
260
  setRoomLeader(agentNickname: string): void;
86
261
  getRoomLeader(): string;
87
262
  getTurnMode(): RoomTurnMode;
263
+ /**
264
+ * Sets how the room chooses who responds to a message. See {@link RoomTurnMode}.
265
+ * @param mode The turn mode; anything unrecognized falls back to 'broadcast'
266
+ */
88
267
  setTurnMode(mode: RoomTurnMode): void;
268
+ /** @returns The configured moderator nickname, or '' when none is set */
89
269
  getModerator(): string;
270
+ /**
271
+ * Names the agent that places messages the room cannot route mechanically.
272
+ * @param agentNickname The moderator's nickname; empty clears it
273
+ */
90
274
  setModerator(agentNickname: string): void;
91
275
  getAgentCount(): number;
92
276
  ready(): void;
93
277
  getCurrentSpeaker(): string | null;
94
278
  setCurrentSpeaker(agentNickname: string | null): void;
95
279
  isAgentsTurn(agentNickname: string): boolean;
280
+ /**
281
+ * Ends the room's turn: drops the processing lock, settles who holds the floor,
282
+ * and optionally keeps draining the queue.
283
+ *
284
+ * Every path that finishes a turn goes through here. It used to be open-coded
285
+ * at fifteen sites, and the omissions were the bug: the main responder loop
286
+ * released the lock without giving up the floor, so any turn that ended without
287
+ * producing a message pinned the floor to an agent forever and the human could
288
+ * never speak again. Two more sites had the same gap.
289
+ *
290
+ * The invariant it exists to hold: **once no turn is in flight, the floor is
291
+ * either nobody's or the human's.** An AI agent holding it with nothing running
292
+ * is a dead room.
293
+ *
294
+ * In a directed room that invariant is only half of it. The floor being nobody's
295
+ * with nothing queued is a dead room too, because in directed mode the floor is a
296
+ * baton: one agent is prompted, and the room only moves on if that agent's turn
297
+ * produces an audible message. {@link recoverStalledFloor} closes that half, and
298
+ * runs here so that every path ending a turn is covered rather than the handful
299
+ * anyone thought to guard.
300
+ * @param floor Who holds the floor now — see {@link FloorHandover}
301
+ * @param opts.drain True to keep processing anything queued
302
+ */
96
303
  private endTurn;
304
+ /**
305
+ * Puts the floor back in somebody's hands when a directed turn ended without a
306
+ * word said.
307
+ *
308
+ * The stall this exists for: a message is addressed to the wrong agent, that
309
+ * agent answers with a `think` — reasonably, since the message was not for it —
310
+ * and a thought prompts nobody. The turn ends, the floor is released, the queue
311
+ * is empty and the room sits there. A `think` is only the commonest way in; a
312
+ * turn that fails, returns nothing, or spends itself on tool calls leaves the
313
+ * room in exactly the same state, which is why this guards the state rather than
314
+ * the action that produced it.
315
+ *
316
+ * **In a directed room, an empty queue with the floor held by nobody is always a
317
+ * stall.** A message meant for a person hands them the floor by name, so a
318
+ * released floor is never "waiting for the human" — it is nobody's turn at all.
319
+ *
320
+ * Recovery is the moderator's existing job: it is handed the unanswered message
321
+ * and asked who should have answered it. Nothing about the stall reaches the
322
+ * transcript — the message has already been logged and heard once, and a second
323
+ * copy of it, or a note about the plumbing, is not part of the story.
324
+ *
325
+ * Needs a moderator that was actually configured. {@link getModeratorAgent} falls
326
+ * back to the first non-player agent, which at a game table is the Dungeon Master
327
+ * — asking it to route its own unanswered message is the confusion this is
328
+ * supposed to end, so a room without a moderator keeps the old behaviour and
329
+ * waits for a person.
330
+ */
97
331
  private recoverStalledFloor;
332
+ /**
333
+ * The moderator this room was configured with, and only that one.
334
+ *
335
+ * Unlike {@link getModeratorAgent} there is no fallback: a caller that needs a
336
+ * genuine moderator would rather have none than the room leader.
337
+ * @returns The configured moderator agent, or undefined
338
+ */
98
339
  private configuredModerator;
340
+ /**
341
+ * Asks the moderator who should have answered a message nobody did, then gives
342
+ * that agent the floor.
343
+ *
344
+ * The message itself is not re-logged and not re-delivered: everyone who could
345
+ * hear it already did, on its first pass. Only the chosen agent is touched, with
346
+ * a note saying why it is being asked — without one it cannot tell this from
347
+ * being handed something it has already watched somebody else answer.
348
+ * @param dispatch The unanswered message and everyone already asked
349
+ * @param moderator The room's configured moderator
350
+ */
99
351
  private rerouteStalledTurn;
352
+ /**
353
+ * Picks the agent a re-routed message goes to.
354
+ *
355
+ * Three candidates, in order: whoever the moderator named, the room leader — the
356
+ * same contract {@link handleModeratorResponse} works to — and finally the sender
357
+ * itself, told to try again. Everyone that has already declined the message is
358
+ * skipped at each step, and so is the moderator, which never answers.
359
+ *
360
+ * The sender is last rather than excluded because at a game table it is usually
361
+ * also the leader: the Dungeon Master addresses the wrong player far more often
362
+ * than anyone else does, so excluding it would leave the commonest stall with
363
+ * nowhere to go.
364
+ * @param named The nickname the moderator answered with, if any
365
+ * @param dispatch The unanswered message and everyone already asked
366
+ * @param moderator The moderator that was consulted
367
+ * @returns The agent to give the floor to, or undefined if there is nobody left
368
+ */
100
369
  private resolveRerouteResponder;
370
+ /**
371
+ * Files an agent's response from a turn that has just ended.
372
+ *
373
+ * A thought is nobody's message but its own, so it is logged and goes no further.
374
+ * A shout jumps the queue. Anything else joins the back of it — and, being an
375
+ * audible message, is the room making progress, which is what ends a run of
376
+ * stall recoveries.
377
+ * @param message The response, or null if the turn produced nothing
378
+ */
101
379
  private queueAgentResponse;
102
380
  getRoomMessageStats(): {
103
381
  numMessages: number;
@@ -111,51 +389,461 @@ export declare class Room {
111
389
  addRoomMessages(messages: RoomMessage[]): void;
112
390
  getCreatedAt(): Date;
113
391
  setCreatedAt(date: Date): void;
392
+ /**
393
+ * Gets all artifacts in the room
394
+ * @returns Array of artifacts
395
+ */
114
396
  getArtifacts(): Artifact[];
397
+ /**
398
+ * Renames an agent, and everything in this room that named it.
399
+ *
400
+ * A room routes by nickname and nothing else, so an agent whose character is
401
+ * called something different gives every other agent two names for one person.
402
+ * The Dungeon Master addresses the character, the room has never heard of them,
403
+ * and the message falls through to the moderator — see
404
+ * {@link namesNobodyInADirectedRoom}, which exists to catch exactly that and
405
+ * once absorbed about a quarter of a campaign's calls. This is the fix that
406
+ * makes the workaround unnecessary: give the agent the character's name.
407
+ *
408
+ * Expensive in the same way {@link ToolRoomAccess.setDescription} is, and for
409
+ * the same reason: the roster sits in every agent's *cached* prompt prefix, so
410
+ * a rename throws away the whole room's cache. Character creation is the moment
411
+ * for it, because play has not started; mid-scene it is a real cost.
412
+ *
413
+ * **History is deliberately not rewritten.** Past messages are a record of what
414
+ * was said, and rewriting them would mean parsing and re-serializing every
415
+ * entry, with compaction summaries reachable only by regex. Instead every agent
416
+ * is told, once, that the name changed — which is what lets them reconcile a
417
+ * roster that says `Leah` with a history that says `StudentOne`.
418
+ *
419
+ * @param from The agent to rename, matched the way messages are routed
420
+ * @param to The new name — a character name, which may be longer than a nickname
421
+ * @returns What happened, including the reason when nothing did
422
+ */
115
423
  renameAgent(from: string, to: string): RenameResult;
424
+ /**
425
+ * The agent this room would refuse to rename to `nickname`, if any.
426
+ *
427
+ * Separate from {@link renameAgent} so a caller renaming an agent that sits in
428
+ * more than one room can check them all before changing anything — a rename
429
+ * applied to two rooms and refused by a third would leave the agent named one
430
+ * thing and half the rooms pointing at another.
431
+ *
432
+ * @param agent The agent being renamed, excluded from its own check
433
+ * @param nickname The nickname it would take
434
+ * @returns The conflicting agent's nickname, or undefined
435
+ */
116
436
  conflictingNickname(agent: Agent, nickname: string): string | undefined;
437
+ /**
438
+ * Repoints everything in *this room* that named an agent, and tells the room.
439
+ *
440
+ * The room-side half of a rename, split out because an agent may sit in more
441
+ * than one room while its own record is only mutated once. {@link renameAgent}
442
+ * is this plus the agent itself.
443
+ *
444
+ * @param previous The nickname being replaced
445
+ * @param nickname The nickname replacing it
446
+ * @returns How many artifacts changed
447
+ */
117
448
  repointNickname(previous: string, nickname: string): number;
449
+ /**
450
+ * Repoints every artifact that named the old nickname.
451
+ *
452
+ * `viewers` is the one that fails silently: an artifact keeps working, the agent
453
+ * simply stops being able to see it, so a player loses their own character sheet
454
+ * with nothing logged. `creator` decides attribution and counts as a viewer.
455
+ *
456
+ * @param previous The nickname being replaced
457
+ * @param nickname The nickname replacing it
458
+ * @returns How many artifacts changed
459
+ */
118
460
  private renameInArtifacts;
461
+ /**
462
+ * Tells every agent in the room that a name changed.
463
+ *
464
+ * One line each, and the reason the history can be left alone: without it an
465
+ * agent reads `Leah` in the roster and `StudentOne` in its own history with
466
+ * nothing joining the two, which is the confusion the rename exists to remove.
467
+ * The renamed agent is told as well — it is the one most likely to be unsure
468
+ * what it is called.
469
+ */
119
470
  private announceRename;
471
+ /**
472
+ * Checks if the room is currently adjourned
473
+ * @returns True if the room is adjourned, false otherwise
474
+ */
120
475
  isRoomAdjourned(): boolean;
476
+ /**
477
+ * Sets the room's artifacts (used when loading from storage)
478
+ * @param artifacts Array of artifacts to set
479
+ */
121
480
  setArtifacts(artifacts: Artifact[]): void;
481
+ /**
482
+ * Whether a message names recipients but none of them is in this room.
483
+ *
484
+ * Only asked of a directed room, where a message is answered by the one agent it
485
+ * names. `selectResponders` cannot place such a message: no target matches, and
486
+ * the leader is skipped when it is the sender — which it is whenever the leader
487
+ * is the one who misnamed someone. What is left is a fallback that widens to
488
+ * *every* other agent, and a directed room exists precisely to avoid that. One
489
+ * campaign spent about a quarter of its calls there, because the Dungeon Master
490
+ * addressed a player by their character's name rather than the nickname the room
491
+ * knows them by.
492
+ *
493
+ * Moderating instead costs one cheap call and is the moderator's documented job:
494
+ * a message that "did not name a recipient the room could use" is routed from its
495
+ * content. A misnamed target is usually a non-player character anyway, and the
496
+ * moderator already knows to hand those to the room leader.
497
+ *
498
+ * @param action The message's normalized action
499
+ * @param targets The names the message was addressed to
500
+ * @returns True when the room should moderate rather than guess
501
+ */
122
502
  private namesNobodyInADirectedRoom;
503
+ /**
504
+ * Parses AI response into RoomMessage object (private helper moved from Agent)
505
+ * @param agent The agent that made the response
506
+ * @param response Raw AI response object
507
+ * @returns Parsed RoomMessage object
508
+ */
123
509
  private parseAgentResponse;
510
+ /**
511
+ * Extracts an opaque "preamble" block from the top of an agent response.
512
+ *
513
+ * A preamble is one or more consecutive `<tag>value</tag>` lines at the
514
+ * very start of the response, terminated by a blank line or non-tag
515
+ * content. The captured preamble is the verbatim text of those tag
516
+ * lines, suitable for downstream parsing by whoever owns the semantic
517
+ * interpretation (e.g. the story service for narrator responses).
518
+ *
519
+ * The Room itself never parses the preamble's contents. It just hands
520
+ * the text to the situation callback so callers (story, debate, etc.)
521
+ * can interpret it however they want.
522
+ */
124
523
  static extractPreamble(response: string): {
125
524
  preamble: string;
126
525
  content: string;
127
526
  } | null;
527
+ /**
528
+ * Maps an arbitrary action string to a MessageAction enum value,
529
+ * accepting any casing. Returns undefined if the value doesn't correspond
530
+ * to a known action.
531
+ */
128
532
  private static coerceMessageAction;
533
+ /**
534
+ * Determines which character agents are part of a message's audience, so a
535
+ * perceiving agent can be shown (in the transient situation message) the
536
+ * descriptions of only the characters it is currently interacting with.
537
+ *
538
+ * The audience is taken from the message's `targets` (the moderator fills these
539
+ * with every agent who can hear the message) plus the message sender. Only
540
+ * character agents (promptFolder === 'character') are returned; the narrator,
541
+ * moderator and other support agents are excluded. The perceiving agent is never
542
+ * included in its own list. When a message has no explicit targets (e.g. a plain
543
+ * SPEAK broadcast in a non-story room) every character who can hear it is included.
544
+ *
545
+ * @param perceivingAgent The agent that will receive these descriptions
546
+ * @param message The message being perceived
547
+ * @returns Character agents in the message's audience, excluding the perceiver
548
+ */
129
549
  private getPresentCharacters;
550
+ /**
551
+ * Builds a digest of the other agents' private context (in stories, their
552
+ * inventory and conditions), for the oversight agents — the moderator and the room
553
+ * leader (the Narrator in stories) — who need to keep the world consistent. Other
554
+ * agents never see this; each only sees its own private context. The leader's own
555
+ * context is left out, since it already receives that directly. Returns an
556
+ * empty string when nobody else has any private context.
557
+ *
558
+ * @returns A markdown block of per-agent private context, or '' if none
559
+ */
130
560
  private buildCharacterPrivateContextDigest;
561
+ /**
562
+ * Builds the descriptors of the tools granted to an agent, for rendering into
563
+ * its perceive prompt. Tool code is never exposed — only name, description and
564
+ * parameters. Tools that were deleted after being granted are silently dropped.
565
+ * @param agent The agent whose granted tools to describe
566
+ * @returns Array of tool descriptors (empty when the agent has no usable tools)
567
+ */
131
568
  private buildAvailableTools;
569
+ /**
570
+ * Prepares declared parameters for the perceive prompt, pre-joining any
571
+ * `enum` so the template can render it. See {@link PromptToolParameter}.
572
+ * @param parameters The tool's declared parameters
573
+ * @returns The parameters, each with `enumText` when it declares an enum
574
+ */
575
+ private static describeToolParameters;
576
+ /**
577
+ * Renders an artifact for a prompt, dropping any ```briyah-state fenced
578
+ * block.
579
+ *
580
+ * That fence is the convention for machine state an artifact carries for the
581
+ * tools that maintain it — a character sheet keeps its whole state object
582
+ * there, below the human-readable summary. Tools read the artifact directly
583
+ * and are unaffected; a model never needs it, and pasting a page of raw JSON
584
+ * into a prompt full of JSON is how a small model loses track of which JSON
585
+ * it is supposed to be producing.
586
+ *
587
+ * An unterminated fence drops everything after it, which is the right
588
+ * reading: whatever follows an opening state fence is state too.
589
+ * @param artifact The artifact to render
590
+ * @returns The prompt-facing text
591
+ */
132
592
  private static renderArtifactForPrompt;
593
+ /**
594
+ * Builds the Handlebars variables for an agent's perceive prompt,
595
+ * including artifacts and the agent's granted tools.
596
+ * @param agent The agent generating a response
597
+ * @returns The prompt variables
598
+ */
133
599
  private buildPerceiveVariables;
600
+ /**
601
+ * The names of the artifacts an agent cannot read.
602
+ *
603
+ * Volatile by nature: it changes whenever *any* artifact anywhere in the room is
604
+ * created or deleted, for every agent that cannot read the new one. That put it in
605
+ * the wrong half of the prompt for a long time. `transient` protects an artifact's
606
+ * contents and nothing protected the roster, so placing four monsters at the start
607
+ * of a fight rewrote this string for eight of nine agents and invalidated all eight
608
+ * cached prefixes. Measured across one session it was 41.7% of the entire
609
+ * cache-creation spend — and the Battle Master, the one agent that *could* read the
610
+ * new artifacts, was the only one the fight cost nothing.
611
+ *
612
+ * Measured against what the agent can *read*, not against the cached block: a
613
+ * transient artifact arrives in the per-turn prompt, so naming it here as one the
614
+ * agent cannot see would be a lie.
615
+ * @param nickname The agent the list is for
616
+ * @returns A comma-separated list, or '' when the agent can read everything
617
+ */
134
618
  private invisibleArtifactNames;
619
+ /**
620
+ * Builds the transient (uncached) prompt message for an agent. It is sent
621
+ * uncached and is not saved to the agent's history, which makes it the right
622
+ * place to inject context that changes frequently, so a change never
623
+ * invalidates the cached prompt instructions:
624
+ * - the descriptions of the characters present in this message's audience
625
+ * (each character sees how the others currently look without leaking
626
+ * details about characters who are elsewhere), and
627
+ * - the perceiving agent's own private context (e.g. its story inventory
628
+ * and conditions), which only that agent may see.
629
+ * The in-story time and situation text lead the transient prompt; the time
630
+ * is on its own labeled line (see buildSituationHeader).
631
+ * @param agent The agent that will receive this prompt
632
+ * @param message The message being perceived, or null for follow-up prompts
633
+ * within the same turn (e.g. after a tool result), where there is no new
634
+ * message audience to describe
635
+ * @returns The transient prompt, or null if there is nothing to inject
636
+ */
135
637
  private buildTransientPrompt;
638
+ /**
639
+ * Sends a single perceive prompt to an agent and parses the response.
640
+ * The raw response is added to the agent's history. InsufficientBalanceError
641
+ * (and any other error) propagates to the caller.
642
+ * @param agent The agent generating a response
643
+ * @param transientPrompt The uncached prompt message (see buildTransientPrompt)
644
+ * @param variables The Handlebars variables (see buildPerceiveVariables)
645
+ * @returns The agent's parsed response
646
+ */
136
647
  private promptAgentOnce;
648
+ /**
649
+ * Runs an agent's full turn: an initial response to the heard message,
650
+ * followed by a private research loop. EXECUTE and PUBLISH responses are
651
+ * handled here — the tool result (or publish confirmation) is fed back into
652
+ * the agent's history and the agent is re-prompted — until the agent emits
653
+ * an audible action or the iteration cap is hit. The room's processing lock
654
+ * is held by the caller for the duration, so other agents wait.
655
+ * @param agent The agent taking its turn
656
+ * @param heardMessage The message that triggered this turn (already in the
657
+ * agent's history), or null when the trigger was fed to the history
658
+ * directly (e.g. commission deliverables)
659
+ * @param opts inCommission: the agent is a commission target; nested
660
+ * commissions are rejected and DELIVER is the expected terminal action
661
+ * @returns The agent's final audible message, or null if the room was
662
+ * paused/adjourned mid-turn
663
+ */
137
664
  private runAgentTurn;
665
+ /**
666
+ * Runs a tool for an agent's EXECUTE message and returns the feedback string
667
+ * to add to the agent's history. Also logs a TOOL notice to the room so
668
+ * humans watching (with thoughts visible) can see the activity.
669
+ * @param agent The agent that issued the EXECUTE
670
+ * @param message The EXECUTE message (name = tool name, content = JSON args)
671
+ * @returns The feedback string for the agent's history
672
+ */
138
673
  private executeToolForAgent;
674
+ /**
675
+ * How much of a result a TOOL notice shows.
676
+ *
677
+ * Generous because the result is the half a person needs when something has gone
678
+ * wrong, and a clipped one hides whether a call that reported success actually
679
+ * changed anything.
680
+ *
681
+ * Most results never reach this: {@link describeToolResult} prefers a result's own
682
+ * `detail` or `summary` one-liner, and the 5e tools all write one. It binds on the
683
+ * results that have neither, which are the raw ones worth reading in full.
684
+ */
139
685
  private static readonly TOOL_LOG_PREVIEW;
686
+ /**
687
+ * How much of an EXECUTE's arguments the room's TOOL notice keeps.
688
+ *
689
+ * Far longer than the result's allowance because the arguments are the half a
690
+ * person actually reads — what the agent asked the tool to do — and at 200
691
+ * characters a `create_sheet` call was cut off inside its own spec, showing the
692
+ * character's race and nothing after it. The bound is a sanity limit rather than
693
+ * a budget: arguments are written by a model, so they are already short.
694
+ *
695
+ * A TOOL notice is heard by nobody and never enters an agent's history
696
+ * (`RoomMessage.canHear` refuses TOOL), so none of this costs a token. It costs
697
+ * bytes in the room log and on the update stream, which is the right trade for
698
+ * being able to see what was run.
699
+ */
140
700
  private static readonly TOOL_LOG_ARGS_PREVIEW;
701
+ /** Clips a preview so one tool call cannot fill the transcript. */
141
702
  private static previewForToolLog;
703
+ /**
704
+ * Summarizes a successful tool result for the room log.
705
+ *
706
+ * `ok` on its own hid the only part anyone watching wanted: a check that
707
+ * rolled 11 against DC 13 and failed was logged exactly like one that passed.
708
+ * A result carrying its own one-line summary — `detail` or `summary`, the two
709
+ * the 5e tools already write — is quoted; anything else is shown as a clipped
710
+ * JSON preview, which at least says what came back.
711
+ *
712
+ * Deliberately no knowledge of any particular tool: these are conventional
713
+ * field names, and a tool that uses neither still reads better than 'ok'.
714
+ *
715
+ * A TOOL notice is heard by nobody and never enters an agent's history, so
716
+ * none of this costs a token — it is purely for the person reading the room.
717
+ * @param value The `result` of a successful run
718
+ * @returns A single line for the notice
719
+ */
142
720
  private static describeToolResult;
721
+ /**
722
+ * Builds the room access a tool receives as `helpers.room`, scoped to the
723
+ * agent that invoked it. Deliberately a closure rather than a class holding
724
+ * a Room reference: {@link ToolRoomAccess} is part of the tool-facing
725
+ * contract, and importing Room into it would pull the whole room graph into
726
+ * the SDK's public types.
727
+ *
728
+ * The wrapper is not a pass-through to publishArtifact — it corrects three
729
+ * behaviors that are right for an agent's PUBLISH but wrong for a tool:
730
+ * publishArtifact credits the artifact to whoever wrote last, treats an
731
+ * empty viewers list as creator-only, and deletes on an empty body.
732
+ * @param agent The agent whose EXECUTE triggered the tool call
733
+ * @returns The room accessor handed to the tool
734
+ */
143
735
  private buildToolRoomAccess;
736
+ /**
737
+ * Gets AI response for an agent (private helper moved from Agent).
738
+ * Thin callback adapter around runAgentTurn: the callback does not fire
739
+ * until the agent's whole turn (including any tool research loop) is done,
740
+ * which is what holds the room's processing lock for the duration.
741
+ * @param agent The agent generating response
742
+ * @param message The message the agent is responding to
743
+ * @param onResponse Callback with AI response
744
+ */
144
745
  private getAgentAiResponse;
746
+ /**
747
+ * Summarises an agent's history when it has outgrown the compaction threshold.
748
+ *
749
+ * Called at the end of a turn rather than the start of one, which is the only
750
+ * safe point: the turn's tool chain feeds results back into history and
751
+ * re-prompts, so compacting part-way through would summarise away the tool call
752
+ * whose result the agent is waiting on. By the time the turn returns, its own
753
+ * response is already in history and the summary can cover it.
754
+ *
755
+ * It also runs *before* `onResponse`, which is what releases the room to process
756
+ * the next message. Compacting afterwards would rewrite this agent's history
757
+ * while the following turn was reading it.
758
+ *
759
+ * A failed summary is logged and swallowed. The agent keeps a too-long history
760
+ * and the campaign carries on, which is strictly better than ending a turn that
761
+ * already produced a good answer because a bookkeeping call failed.
762
+ *
763
+ * @param agent The agent whose turn has just finished
764
+ */
145
765
  private compactIfOversized;
766
+ /**
767
+ * Delivers a message into an agent's history without asking it to respond.
768
+ * Split out of {@link perceive} so agents that are not responding this turn
769
+ * still stay in sync with the conversation at no LLM cost. Callers that are
770
+ * not counting this agent in their outstanding-response tally must use this
771
+ * rather than `perceive`, which always invokes its callback.
772
+ * @param agent The agent receiving the message
773
+ * @param message The message being delivered
774
+ * @returns True if the agent could hear the message and it was recorded
775
+ */
146
776
  private hear;
777
+ /**
778
+ * Allows an agent to perceive messages and generate a response (moved from Agent)
779
+ * @param agent The agent that is perceiving
780
+ * @param message
781
+ * @param onResponse Callback with agent's response or null
782
+ */
147
783
  perceive(agent: Agent, message: RoomMessage, onResponse: (message: RoomMessage | null) => void, opts?: {
148
784
  continuing?: boolean;
149
785
  }): void;
786
+ /**
787
+ * Moderator agent evaluates and routes a message (moved from Agent)
788
+ * @param moderatorAgent The moderator agent
789
+ * @param message RoomMessage to moderate
790
+ * @param onResponse Callback with routing decision
791
+ * @param opts.note Extra instruction for this one routing decision, put in the
792
+ * transient prompt so it never enters the moderator's cached prefix
793
+ */
150
794
  moderate(moderatorAgent: Agent, message: RoomMessage, onResponse: (message: RoomMessage | null) => void, opts?: {
151
795
  note?: string;
152
796
  }): Promise<void>;
797
+ /**
798
+ * Handles insufficient balance errors by pausing the conversation
799
+ * @param error The insufficient balance error
800
+ * @param onResponse Optional callback to call with null
801
+ */
153
802
  private handleInsufficientBalance;
803
+ /**
804
+ * Gets an artifact by name
805
+ * @param name The name of the artifact
806
+ * @returns The artifact or undefined if not found
807
+ */
154
808
  findArtifact(name: string): Artifact | undefined;
809
+ /**
810
+ * Creates or updates an artifact with PUBLISH action
811
+ * If body is empty, deletes the artifact instead
812
+ * @param name The name of the artifact
813
+ * @param creator The name of the agent who created it
814
+ * @param body The content of the artifact (empty body will delete the artifact)
815
+ * @param viewers Array of agent names who can view the artifact
816
+ * @returns The created or updated artifact, or null if deleted
817
+ */
155
818
  publishArtifact(name: string, creator: string, body: string, viewers?: string[], transient?: boolean): Artifact | null;
819
+ /**
820
+ * Deletes an artifact from the room
821
+ * @param name The name of the artifact to delete
822
+ * @returns True if the artifact was found and deleted, false otherwise
823
+ */
156
824
  deleteArtifact(name: string): boolean;
825
+ /**
826
+ * Gets the artifacts an agent may read, transient ones included.
827
+ * @param agentNickname The name of the agent
828
+ * @returns Array of artifacts readable by the agent
829
+ */
157
830
  private getReadableArtifacts;
831
+ /**
832
+ * Gets the artifacts rendered into an agent's **cached** prompt block.
833
+ *
834
+ * Transient artifacts are deliberately excluded: they go in the per-turn
835
+ * prompt instead (see `buildTransientPrompt`), which is the whole point of
836
+ * marking one transient. They are still readable — `getTransientArtifacts`
837
+ * returns them, and a tool reads any artifact regardless.
838
+ * @param agentNickname The name of the agent
839
+ * @returns Array of artifacts visible to the agent
840
+ */
158
841
  getVisibleArtifacts(agentNickname: string): Artifact[];
842
+ /**
843
+ * Gets the artifacts delivered to an agent in the uncached per-turn prompt.
844
+ * @param agentNickname The name of the agent
845
+ * @returns Array of transient artifacts readable by the agent
846
+ */
159
847
  getTransientArtifacts(agentNickname: string): Artifact[];
160
848
  getId(): string;
161
849
  setId(id: string): void;
@@ -163,49 +851,341 @@ export declare class Room {
163
851
  setStorageDir(dir: string): void;
164
852
  getRoomStoreService(): any;
165
853
  setRoomStoreService(service: any): void;
854
+ /**
855
+ * @internal Injects the user-scoped tool services so agents in this room can
856
+ * EXECUTE user-defined tools. Called by RoomStoreService and RoomFactory at
857
+ * every Room construction site; SDK consumers never call this directly.
858
+ * @param toolStore The user's tool store
859
+ * @param toolExecution The user's tool execution service
860
+ */
166
861
  setToolServices(toolStore: ToolStoreService, toolExecution: ToolExecutionService): void;
862
+ /**
863
+ * @internal Injects the shared illustration pipeline. Called wherever
864
+ * {@link setToolServices} is; SDK consumers never call this directly.
865
+ */
866
+ setIllustrationService(illustration: IllustrationService): void;
867
+ /**
868
+ * Builds the illustration capability a tool sees, for one calling agent.
869
+ *
870
+ * A closure rather than a class holding a Room, for the reason
871
+ * {@link buildToolRoomAccess} is: `ToolIllustration` is part of the tool-facing
872
+ * contract, and importing Room into it would drag the room graph into the SDK's
873
+ * public types.
874
+ *
875
+ * `request` returns the moment the work is queued. Drawing takes minutes and a
876
+ * tool call has at most 120 seconds, so waiting would guarantee a timeout; the
877
+ * finished picture arrives as its own message instead.
878
+ */
879
+ private buildToolIllustration;
880
+ /**
881
+ * Saves messages incrementally to disk
882
+ * @param messages The new messages to append
883
+ */
167
884
  saveMessages(messages: RoomMessage[]): void;
885
+ /**
886
+ * Saves the room metadata to disk
887
+ */
168
888
  saveMetadata(): string;
889
+ /**
890
+ * Registers a state-change listener under a stable key. Each key holds at most
891
+ * one listener — re-registering with the same key replaces its callback.
892
+ * Different keys can coexist so multiple subsystems (e.g. the rooms SSE endpoint
893
+ * and the story service) both receive notifications without clobbering each other.
894
+ * @param key Stable identifier for the subsystem (e.g. 'app-service', 'story')
895
+ * @param callback Function to call on state changes
896
+ */
169
897
  setOnStateChange(key: string, callback: () => void): void;
898
+ /**
899
+ * Removes a previously-registered state-change listener.
900
+ * @param key The key passed to {@link setOnStateChange}
901
+ */
170
902
  removeOnStateChange(key: string): void;
171
903
  notifyStateChange(): void;
904
+ /**
905
+ * Subscribes to errors raised while running a turn.
906
+ *
907
+ * Keyed like {@link setOnStateChange}, and for the same reason: more than one
908
+ * thing wants to hear about a failed turn. A single-callback setter meant the
909
+ * last caller silently replaced the first — the SDK's bridge onto the room's
910
+ * update stream and a story's own handler could not both exist, and whichever
911
+ * registered second won without any sign that it had taken the other's place.
912
+ *
913
+ * @param key Identifies the subscriber; registering the same key twice replaces it
914
+ * @param callback Function to call on errors
915
+ */
172
916
  setOnError(key: string, callback: (error: Error) => void): void;
917
+ /**
918
+ * Stops delivering errors to a subscriber.
919
+ * @param key The key it registered with
920
+ */
173
921
  clearOnError(key: string): void;
174
922
  private notifyError;
923
+ /**
924
+ * Every human-controlled agent in the room, in arrival order.
925
+ *
926
+ * A room may have more than one: several people can share a browser, and one
927
+ * person can play several characters. Code that genuinely needs a single human
928
+ * uses {@link getDefaultHumanAgent} — but anything reacting to a *message* must
929
+ * resolve the human from its sender, because the sender is rarely the default.
930
+ */
175
931
  getHumanAgents(): Agent[];
932
+ /**
933
+ * Names as a prompt reads them aloud: `Rhys`, `Rhys and Mira`, `Rhys, Mira and Tor`.
934
+ *
935
+ * Handlebars has no `join`, and this codebase registers no helpers, so a template
936
+ * wanting a list inline has to either be handed one or spell out an `{{#each}}`
937
+ * with `{{#unless @last}}`. Handing it over is the difference between a prompt
938
+ * that reads like a sentence and one every author gets subtly wrong — which is
939
+ * how a table with two players came to have prompts that only ever named one.
940
+ * @param names The names to list, in order
941
+ * @returns The joined phrase, or an empty string for no names
942
+ */
176
943
  private static listNames;
944
+ /**
945
+ * The human the room falls back to when it has to pick one, which is whoever
946
+ * arrived first. Used to open the conversation and to attribute a message that
947
+ * arrives with no sender. Which one it is carries no meaning.
948
+ */
177
949
  getDefaultHumanAgent(): Agent | undefined;
950
+ /**
951
+ * The human-controlled agent a sender name refers to, matched the same way
952
+ * message targets are, or undefined when no human answers to that name.
953
+ */
178
954
  getHumanAgentByName(sender: string): Agent | undefined;
955
+ /** True when `nickname` names a human-controlled agent in this room. */
179
956
  isHumanNickname(nickname: string | null): boolean;
957
+ /**
958
+ * True when a message is the players talking among themselves: a whisper whose
959
+ * every target is a human-controlled agent.
960
+ *
961
+ * Such a message cannot reach the game. `canHear` restricts a whisper to its
962
+ * targets, so no AI agent hears it; every target is human, so no agent is
963
+ * prompted and nothing is spent. That is what lets the room take one **out of
964
+ * turn** and leave the floor untouched — the players get to chat across the
965
+ * table while an agent is mid-turn, and it still shows up in the transcript for
966
+ * everyone at the table to read.
967
+ */
180
968
  isPlayerAside(message: RoomMessage): boolean;
969
+ /**
970
+ * Gets the name of the default human-controlled agent
971
+ * @returns The human agent's name or empty string if not found
972
+ */
181
973
  getHumanAgentName(): string;
974
+ /**
975
+ * Gets the agent that places messages the room cannot route mechanically.
976
+ *
977
+ * Prefers the configured {@link setModerator} nickname. The fallback to the
978
+ * first agent is how story rooms have always chosen theirs — the narrator is
979
+ * added first — and is kept so they keep working, but it is a guess: in any
980
+ * other room the first agent is whoever happened to be added first and has no
981
+ * `moderate.prompt` at all. Name one explicitly.
982
+ *
983
+ * The fallback prefers an agent that actually has a `moderate.prompt`, because
984
+ * one that does not fails in the worst way available: the prompt lookup throws,
985
+ * the throw is swallowed, and the message disappears. Arrival order is only
986
+ * consulted when nobody in the room can moderate, which keeps a room that was
987
+ * working before this preference existed working now.
988
+ * @returns The moderator agent or undefined if the room is empty
989
+ */
182
990
  getModeratorAgent(): Agent | undefined;
991
+ /**
992
+ * Gets the current story situation
993
+ * @returns The current situation string
994
+ */
183
995
  getSituation(): string;
996
+ /**
997
+ * Gets the current in-story time (24-hour "HH:MM"), stored separately from the
998
+ * situation text.
999
+ * @returns The current time string, or '' if none is set
1000
+ */
184
1001
  getSituationTime(): string;
1002
+ /**
1003
+ * Sets the current in-story time (24-hour "HH:MM").
1004
+ * @param time The time string to set
1005
+ */
185
1006
  setSituationTime(time: string): void;
1007
+ /**
1008
+ * Builds the situation header shared by every agent's transient prompt: the
1009
+ * in-story time on its own labeled line (flagged as a 24-hour clock so a character
1010
+ * doesn't misread "10:30" as 10:30 PM), followed by the situation text. Either
1011
+ * line is omitted when its value is empty.
1012
+ *
1013
+ * @returns The time and situation lines, or '' if both are empty
1014
+ */
186
1015
  private buildSituationHeader;
1016
+ /**
1017
+ * Sets the current story situation
1018
+ * @param situation The situation string to set
1019
+ */
187
1020
  setSituation(situation: string): void;
1021
+ /**
1022
+ * Sets a callback to be invoked whenever situation changes
1023
+ * @param callback Function to call with new situation
1024
+ */
188
1025
  setOnSituationUpdate(callback: (situation: string, messageBody?: string) => void | Promise<void>): void;
189
1026
  setOnModeratorResponse(callback: (message: RoomMessage) => void): void;
1027
+ /**
1028
+ * Handles situation update from an agent message
1029
+ * Updates internal state and notifies callback
1030
+ * @param situation The new situation string
1031
+ * @param messageBody Body of the message that produced this situation, for
1032
+ * consumers that need the triggering message but can't read it from the
1033
+ * room yet (it may not be logged at the time the callback fires).
1034
+ */
190
1035
  private handleSituationUpdate;
1036
+ /**
1037
+ * Draws the new scene, when the room is set to illustrate itself.
1038
+ *
1039
+ * Only in `auto`. The default is `on-request` for a reason: an agent may be
1040
+ * told to restate the situation on *every* response — the dnd5e Dungeon Master
1041
+ * is — so a room wired the way stories are would buy a picture several times a
1042
+ * turn. Stories are unaffected either way, because StoryService still issues
1043
+ * their illustrations itself, with the chapter and character branches this
1044
+ * cannot see.
1045
+ *
1046
+ * Fire-and-forget: drawing takes minutes and nothing is waiting on it.
1047
+ */
1048
+ private requestAutomaticIllustration;
191
1049
  handlePublishMessages(messages: RoomMessage[]): void;
192
1050
  handleAdjournMessages(messages: RoomMessage[]): void;
193
1051
  addPendingMessage(message: RoomMessage, clearQueue?: boolean, context?: unknown): SendMessageResult;
194
1052
  isProcessingInProgress(): boolean;
1053
+ /**
1054
+ * True when the room is idle pending input from the human-controlled agent.
1055
+ *
1056
+ * This is distinct from `isProcessingInProgress()`: after an agent addresses
1057
+ * the human, the room intentionally leaves processing "in progress" while it
1058
+ * waits for the human's reply (the human's perceive never fires a response
1059
+ * callback). Hosts should use this flag — not `processingInProgress` — to
1060
+ * decide whether to show a typing indicator.
1061
+ */
195
1062
  isWaitingForHuman(): boolean;
196
1063
  pause(): void;
197
1064
  resume(): void;
198
1065
  interrupt(): void;
199
1066
  checkAgentNicknameMatch(existingNickname: string, targetNickname: string): boolean;
1067
+ /**
1068
+ * Hands a moderated message to one recipient in the shape that recipient needs.
1069
+ *
1070
+ * An AI agent gets the message itself, so a room whose agents answer in the JSON
1071
+ * protocol sees moderated messages in the same shape as every other one — half a
1072
+ * history in plain prose models the wrong output format, and a prose reply is
1073
+ * silently absorbed by parseAgentResponse rather than corrected, so the drift has
1074
+ * no back pressure.
1075
+ *
1076
+ * A human gets the readable form. Nothing prompts a human agent, so its history
1077
+ * is a record rather than a set of examples, and JSON is the wrong thing to keep.
1078
+ *
1079
+ * With no `relayAs` the two branches produce identical text, which is why story
1080
+ * rooms are unaffected by this distinction.
1081
+ */
200
1082
  private deliverModerated;
1083
+ /**
1084
+ * Handles the moderator's routing response for a single message
1085
+ * @param moderatorMessage The routing response from the moderator
1086
+ * @param originalMessage The original message that needed routing
1087
+ */
201
1088
  private handleModeratorResponse;
1089
+ /**
1090
+ * Runs one commissioned agent's research turn and normalizes the result into
1091
+ * a DELIVER message. If the agent's terminal action isn't DELIVER it gets one
1092
+ * corrective re-prompt; a still-wrong action is coerced into a DELIVER so the
1093
+ * commissioner is never starved. Errors become a synthetic error DELIVER.
1094
+ * @param agent The commissioned agent
1095
+ * @param commission The COMMISSION message (already in the agent's history)
1096
+ * @returns The DELIVER message, or null if the room was paused/adjourned
1097
+ */
202
1098
  private runDeliverTurn;
1099
+ /**
1100
+ * Orchestrates a COMMISSION message. Called from processPendingMessages with
1101
+ * the processing lock held; the lock stays held until the commissioner has
1102
+ * responded to the aggregated deliverables (or the room pauses/adjourns).
1103
+ *
1104
+ * Flow: every agent hears the commission (it is public, like SPEAK) → each AI
1105
+ * target concurrently runs its own research turn ending in a DELIVER → the
1106
+ * DELIVERs are logged and heard by the room → the commissioner receives the
1107
+ * concatenated deliverables and responds, re-entering normal processing.
1108
+ *
1109
+ * Human-controlled targets are skipped (waiting on a human while the lock is
1110
+ * held would deadlock the room) and noted in the aggregate. Nothing about an
1111
+ * in-flight commission is persisted: a server restart kills it, leaving only
1112
+ * the already-logged messages.
1113
+ * @param msg The COMMISSION message (already added to the room log)
1114
+ */
203
1115
  private handleCommissionMessage;
1116
+ /**
1117
+ * Delivers a system note (commission results or failure) to the commissioner,
1118
+ * lets it respond, queues that response, and releases the processing lock.
1119
+ * A human or missing commissioner just gets the note; the room then waits
1120
+ * for their next message as usual.
1121
+ * @param commissioner The commissioning agent, if found
1122
+ * @param note The system note to add to the commissioner's history
1123
+ */
204
1124
  private respondToCommissioner;
205
1125
  private processPendingMessages;
1126
+ /**
1127
+ * Protects a specialist's history when the room leader hands it a task.
1128
+ *
1129
+ * A specialist runs on a short rolling window so it does not carry the last job
1130
+ * into the next one. That window is wrong *within* a job: a whole encounter or a
1131
+ * whole character interview can run longer than the window is wide. Pinning at
1132
+ * the handoff keeps everything from that message on, while the window still
1133
+ * drops what came before — so the second job forgets the first, and neither
1134
+ * forgets itself.
1135
+ *
1136
+ * Three conditions, all needed:
1137
+ *
1138
+ * - **the sender is the room leader**, because a handoff is the leader delegating;
1139
+ * - **the message names this agent**, so a general announcement is not a new task
1140
+ * and cannot re-pin one already running;
1141
+ * - **the agent has a window**, which makes this a no-op for everyone else. The
1142
+ * Dungeon Master keeps its whole campaign whether or not this runs.
1143
+ *
1144
+ * Called *before* {@link perceive}, so the handoff instruction itself is inside
1145
+ * the pin rather than the first casualty of the next trim.
1146
+ *
1147
+ * There is no unpin. Re-pinning ends the previous pin, so an abandoned task is
1148
+ * reclaimed by the next one and the worst a stale pin does is keep more history
1149
+ * than intended.
1150
+ * @param agent The agent about to be prompted
1151
+ * @param message The message being dispatched to it
1152
+ */
206
1153
  private pinHistoryOnHandoff;
1154
+ /**
1155
+ * Decides which agents are asked to respond to a message.
1156
+ *
1157
+ * In 'broadcast' mode every agent responds and {@link filterConcurrentMessages}
1158
+ * later keeps one reply, discarding the rest. In 'directed' mode exactly one
1159
+ * agent responds — the first target that resolves to an agent, or the room
1160
+ * leader when no target does — so a leader agent controls the order of play
1161
+ * and no work is paid for and thrown away. Agents that are not selected still
1162
+ * receive the message through {@link hear}.
1163
+ *
1164
+ * The sender is never selected: a self-loop would have the agent answer
1165
+ * itself and stall the room. If neither a target nor the leader resolves to
1166
+ * someone other than the sender, this widens to everyone else who can hear
1167
+ * the message, so a misaddressed message still gets a reply from somebody.
1168
+ * @param message The message being processed
1169
+ * @returns The agents to prompt, in the order they should be dispatched
1170
+ */
1171
+ /**
1172
+ * Whether an agent that has addressed itself may keep the floor.
1173
+ *
1174
+ * Counts consecutive turns rather than total ones: a run ends the moment anyone
1175
+ * else takes the floor, so an agent that legitimately keeps it twice during one
1176
+ * fight and twice more during the next is never penalised for the first pair.
1177
+ * @param sender The agent asking to answer itself
1178
+ * @returns True when it keeps the floor, false when the run has gone on too long
1179
+ */
207
1180
  private mayContinueAlone;
1181
+ /** Ends a self-continue run, because the floor has gone to somebody else. */
208
1182
  private clearSelfContinue;
209
1183
  private selectResponders;
1184
+ /**
1185
+ * Filters concurrent messages to ensure only one message is processed
1186
+ * Priority: SHOUT > SPEAK > WHISPER, then by agent order in room
1187
+ * @param messages Array of messages to filter
1188
+ * @returns Single message (or original if <= 1)
1189
+ */
210
1190
  private filterConcurrentMessages;
211
1191
  }