@alexkroman1/aai-cli 16.0.1 → 17.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (454) hide show
  1. package/README.md +3 -2
  2. package/dist/{_build-target-55dJisQ-.mjs → _build-target-C08aM-Es.mjs} +511 -41
  3. package/dist/_build-target.d.ts +109 -2
  4. package/dist/_deno-target.d.ts +15 -14
  5. package/dist/_dev-env.d.ts +19 -13
  6. package/dist/{_dev-server-BzWB6-4y.mjs → _dev-server-CLU9xjH5.mjs} +27 -19
  7. package/dist/_dev-server.d.ts +4 -3
  8. package/dist/_dev-vite-config.d.ts +1 -1
  9. package/dist/_init.d.ts +39 -0
  10. package/dist/_modal-target.d.ts +11 -6
  11. package/dist/_preflight-CnW9QiE7.mjs +116 -0
  12. package/dist/_preflight.d.ts +110 -5
  13. package/dist/{_server-common-De0haHr9.mjs → _server-common-DAcv1nC5.mjs} +46 -2
  14. package/dist/_server-common.d.ts +39 -0
  15. package/dist/_target-bundle.d.ts +112 -0
  16. package/dist/_target-entry.d.ts +81 -0
  17. package/dist/{_templates-CuP74F8D.mjs → _templates-IMeKMVIj.mjs} +1 -1
  18. package/dist/{_vitest-runner-AVR9VtXh.mjs → _vitest-runner-CZNGj0ZC.mjs} +22 -17
  19. package/dist/_vitest-runner.d.ts +36 -24
  20. package/dist/{_workflow-determinism-CvpqMpdh.mjs → _workflow-determinism-CLxFQ5ox.mjs} +1 -1
  21. package/dist/_workflow-determinism.d.ts +1 -1
  22. package/dist/{build-ButI2IKc.mjs → build-DrQTSc2R.mjs} +187 -289
  23. package/dist/build.d.ts +88 -2
  24. package/dist/cli.mjs +20 -16
  25. package/dist/{deploy-CGqPU5U-.mjs → deploy-B68-x7xM.mjs} +14 -6
  26. package/dist/deploy.d.ts +9 -0
  27. package/dist/{dev-kodAY23K.mjs → dev-ChsrNHpK.mjs} +1 -1
  28. package/dist/dev.d.ts +1 -1
  29. package/dist/{eval-DvoSz7SZ.mjs → eval-DB07kTxO.mjs} +2 -2
  30. package/dist/init-W-4k-lCF.mjs +559 -0
  31. package/dist/init.d.ts +43 -3
  32. package/dist/scaffold/CLAUDE.md +87 -100
  33. package/dist/scaffold/package.json +6 -6
  34. package/dist/scaffold/vite.config.ts +29 -1
  35. package/dist/start.mjs +1 -1
  36. package/dist/{studio-CtRBVVFR.mjs → studio-DEAJGBXa.mjs} +20 -10
  37. package/dist/templates/{hiring-desk → applicant-screening-agent}/agent.eval.test.ts +20 -6
  38. package/dist/templates/{hiring-desk → applicant-screening-agent}/agent.ts +28 -0
  39. package/dist/templates/{hiring-desk → applicant-screening-agent}/crews.ts +1 -1
  40. package/dist/templates/{hiring-desk → applicant-screening-agent}/shared.ts +11 -11
  41. package/dist/templates/{hiring-desk → applicant-screening-agent}/tools/screening_status.ts +1 -1
  42. package/dist/templates/{call-audit → call-audit-workflow}/agent.ts +4 -2
  43. package/dist/templates/{call-audit → call-audit-workflow}/client.tsx +3 -3
  44. package/dist/templates/{call-audit → call-audit-workflow}/guards.test.ts +2 -2
  45. package/dist/templates/{call-audit → call-audit-workflow}/workflows/summarize.ts +1 -1
  46. package/dist/templates/{code-interpreter → code-interpreter-agent}/agent.eval.test.ts +3 -2
  47. package/dist/templates/{code-interpreter → code-interpreter-agent}/agent.test.ts +1 -1
  48. package/dist/templates/{code-interpreter → code-interpreter-agent}/agent.ts +1 -0
  49. package/dist/templates/coding-agent/agent.eval.test.ts +136 -0
  50. package/dist/templates/coding-agent/agent.test.ts +109 -0
  51. package/dist/templates/coding-agent/agent.ts +45 -0
  52. package/dist/templates/coding-agent/chat.ts +63 -0
  53. package/dist/templates/coding-agent/shared.ts +46 -0
  54. package/dist/templates/coding-agent/system-prompt.md +32 -0
  55. package/dist/templates/coding-agent/tools/bash.ts +6 -0
  56. package/dist/templates/coding-agent/tools/delete_file.ts +6 -0
  57. package/dist/templates/coding-agent/tools/edit_file.ts +6 -0
  58. package/dist/templates/coding-agent/tools/glob.ts +6 -0
  59. package/dist/templates/coding-agent/tools/grep.ts +6 -0
  60. package/dist/templates/coding-agent/tools/list_files.ts +6 -0
  61. package/dist/templates/coding-agent/tools/read_file.ts +6 -0
  62. package/dist/templates/coding-agent/tools/todo_write.ts +6 -0
  63. package/dist/templates/coding-agent/tools/write_file.ts +6 -0
  64. package/dist/templates/{pipeline-simple → custom-pipeline-agent}/agent.test.ts +2 -2
  65. package/dist/templates/{pipeline-simple → custom-pipeline-agent}/agent.ts +2 -1
  66. package/dist/templates/{pipeline-simple → custom-pipeline-agent}/modes.test.ts +1 -1
  67. package/dist/templates/{redline → document-redline-workflow}/agent.ts +4 -2
  68. package/dist/templates/{redline → document-redline-workflow}/client.tsx +1 -1
  69. package/dist/templates/{redline → document-redline-workflow}/workflows/redline.ts +1 -1
  70. package/dist/templates/{dispatch-center → emergency-dispatch-agent}/agent.test.ts +104 -1
  71. package/dist/templates/{dispatch-center → emergency-dispatch-agent}/agent.ts +15 -0
  72. package/dist/templates/{dispatch-center → emergency-dispatch-agent}/events.ts +1 -1
  73. package/dist/templates/emergency-dispatch-agent/guardrails.ts +105 -0
  74. package/dist/templates/{dispatch-center → emergency-dispatch-agent}/system-prompt.md +1 -0
  75. package/dist/templates/{dispatch-center → emergency-dispatch-agent}/tools/incident_create.ts +1 -1
  76. package/dist/templates/{night-owl → entertainment-picks-agent}/agent.eval.test.ts +8 -1
  77. package/dist/templates/{night-owl → entertainment-picks-agent}/agent.test.ts +1 -1
  78. package/dist/templates/{night-owl → entertainment-picks-agent}/agent.ts +1 -0
  79. package/dist/templates/{night-owl → entertainment-picks-agent}/client.tsx +1 -1
  80. package/dist/templates/{night-owl → entertainment-picks-agent}/system-prompt.md +6 -0
  81. package/dist/templates/{night-owl → entertainment-picks-agent}/tools/revisit.ts +7 -0
  82. package/dist/templates/{executive-assistant → executive-inbox-agent}/agent.eval.test.ts +84 -23
  83. package/dist/templates/{executive-assistant → executive-inbox-agent}/agent.ts +1 -0
  84. package/dist/templates/{executive-assistant → executive-inbox-agent}/meeting.ts +2 -2
  85. package/dist/templates/{executive-assistant → executive-inbox-agent}/prompts.ts +3 -1
  86. package/dist/templates/{executive-assistant → executive-inbox-agent}/shared.ts +2 -2
  87. package/dist/templates/{executive-assistant → executive-inbox-agent}/system-prompt.md +3 -3
  88. package/dist/templates/{executive-assistant → executive-inbox-agent}/tools/meeting_assistant.ts +3 -3
  89. package/dist/templates/{executive-assistant → executive-inbox-agent}/tools/open_email.ts +8 -16
  90. package/dist/templates/{executive-assistant → executive-inbox-agent}/tools/send_calendar_invite.ts +1 -1
  91. package/dist/templates/{hotel-desk → hotel-reception-agent}/agent.eval.test.ts +85 -21
  92. package/dist/templates/{hotel-desk → hotel-reception-agent}/agent.test.ts +2 -1
  93. package/dist/templates/{hotel-desk → hotel-reception-agent}/agent.ts +6 -3
  94. package/dist/templates/{hotel-desk → hotel-reception-agent}/client.tsx +14 -2
  95. package/dist/templates/{hotel-desk → hotel-reception-agent}/concierge.ts +3 -2
  96. package/dist/templates/{hotel-desk → hotel-reception-agent}/desk-seams.test.ts +2 -1
  97. package/dist/templates/{hotel-desk → hotel-reception-agent}/disputes.ts +9 -11
  98. package/dist/templates/{hotel-desk → hotel-reception-agent}/events.ts +2 -1
  99. package/dist/templates/hotel-reception-agent/session.ts +56 -0
  100. package/dist/templates/{hotel-desk → hotel-reception-agent}/shared.ts +21 -22
  101. package/dist/templates/{hotel-desk → hotel-reception-agent}/system-prompt.md +1 -1
  102. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/abandon_booking.ts +2 -1
  103. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/add_to_waitlist.ts +2 -1
  104. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/book_airport_car.ts +2 -1
  105. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/cancel_restaurant_reservation.ts +2 -1
  106. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/cancel_room_booking.ts +2 -1
  107. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/check_restaurant_availability.ts +1 -1
  108. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/check_room_availability.ts +1 -1
  109. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/choose_room.ts +1 -1
  110. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/confirm_booking.ts +2 -1
  111. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/dispatch_emergency.ts +2 -1
  112. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/dispute_charge.ts +2 -1
  113. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/flag_late_arrival.ts +2 -1
  114. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/lookup_booking.ts +2 -1
  115. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/lookup_guest_history.ts +1 -1
  116. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/lookup_invoice.ts +2 -1
  117. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/lookup_restaurant_reservation.ts +1 -1
  118. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/modify_restaurant_reservation.ts +1 -1
  119. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/record_card.ts +1 -1
  120. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/record_followup.ts +2 -1
  121. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/record_group_inquiry.ts +2 -1
  122. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/record_guest_details.ts +1 -1
  123. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/reinstate_booking.ts +1 -1
  124. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/request_flight_reconfirmation.ts +2 -1
  125. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/resend_confirmation.ts +2 -1
  126. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/reserve_table.ts +1 -1
  127. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/resolve_room_conflict.ts +2 -1
  128. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/schedule_wakeup_call.ts +2 -1
  129. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/set_do_not_disturb.ts +2 -1
  130. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/set_extras.ts +1 -1
  131. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/set_stay.ts +2 -1
  132. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/start_booking_modification.ts +2 -1
  133. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/start_room_booking.ts +2 -1
  134. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/take_guest_message.ts +2 -1
  135. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/transfer_call.ts +2 -1
  136. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/update_card.ts +2 -1
  137. package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/verify_booking.ts +2 -1
  138. package/dist/templates/{link-digest → link-digest-workflow}/agent.eval.test.ts +16 -1
  139. package/dist/templates/{link-digest → link-digest-workflow}/agent.test.ts +2 -2
  140. package/dist/templates/{link-digest → link-digest-workflow}/agent.ts +8 -3
  141. package/dist/templates/{link-digest → link-digest-workflow}/client.tsx +2 -2
  142. package/dist/templates/{link-digest → link-digest-workflow}/workflows/digest.ts +1 -1
  143. package/dist/templates/{health-assistant → medication-safety-agent}/agent.test.ts +55 -1
  144. package/dist/templates/medication-safety-agent/agent.ts +62 -0
  145. package/dist/templates/{recap-workflow → meeting-recap-agent}/agent.test.ts +4 -4
  146. package/dist/templates/{recap-workflow → meeting-recap-agent}/agent.ts +5 -1
  147. package/dist/templates/{recap-workflow → meeting-recap-agent}/workflows/recap.ts +3 -3
  148. package/dist/templates/{pizza-ordering → pizza-ordering-agent}/agent.ts +3 -0
  149. package/dist/templates/{podcast-digest → podcast-digest-workflow}/agent.eval.test.ts +1 -1
  150. package/dist/templates/{podcast-digest → podcast-digest-workflow}/agent.test.ts +1 -1
  151. package/dist/templates/{podcast-digest → podcast-digest-workflow}/agent.ts +6 -2
  152. package/dist/templates/{podcast-digest → podcast-digest-workflow}/client.tsx +3 -3
  153. package/dist/templates/{podcast-digest → podcast-digest-workflow}/workflows/digest.ts +1 -1
  154. package/dist/templates/{simple → quickstart-agent}/agent.eval.test.ts +39 -3
  155. package/dist/templates/{simple → quickstart-agent}/agent.test.ts +25 -3
  156. package/dist/templates/quickstart-agent/agent.ts +24 -0
  157. package/dist/templates/quickstart-agent/system-prompt.md +8 -0
  158. package/dist/templates/quickstart-agent/tools/get_weather.ts +59 -0
  159. package/dist/templates/{research-workflow → research-handoff-agent}/agent.test.ts +1 -1
  160. package/dist/templates/{research-workflow → research-handoff-agent}/agent.ts +5 -1
  161. package/dist/templates/{research-workflow → research-handoff-agent}/workflows/filing.ts +1 -1
  162. package/dist/templates/{research-workflow → research-handoff-agent}/workflows/research.ts +1 -1
  163. package/dist/templates/{research-workflow → research-handoff-agent}/workflows/review.ts +1 -1
  164. package/dist/templates/{plan-and-execute → research-planner-agent}/agent.eval.test.ts +24 -2
  165. package/dist/templates/{plan-and-execute → research-planner-agent}/agent.ts +3 -0
  166. package/dist/templates/{plan-and-execute → research-planner-agent}/shared.ts +1 -1
  167. package/dist/templates/{plan-and-execute → research-planner-agent}/system-prompt.md +1 -1
  168. package/dist/templates/{retail → retail-orders-agent}/agent.ts +32 -3
  169. package/dist/templates/retail-orders-agent/keyterms.ts +154 -0
  170. package/dist/templates/{retail → retail-orders-agent}/pending.ts +2 -2
  171. package/dist/templates/{retail → retail-orders-agent}/resolve.ts +18 -3
  172. package/dist/templates/{retail → retail-orders-agent}/shared.ts +1 -1
  173. package/dist/templates/{retail → retail-orders-agent}/store.ts +3 -3
  174. package/dist/templates/{roadside-assist → roadside-assistance-agent}/agent.eval.test.ts +37 -5
  175. package/dist/templates/{roadside-assist → roadside-assistance-agent}/agent.test.ts +38 -6
  176. package/dist/templates/{roadside-assist → roadside-assistance-agent}/agent.ts +5 -1
  177. package/dist/templates/{roadside-assist → roadside-assistance-agent}/call.ts +1 -1
  178. package/dist/templates/{roadside-assist → roadside-assistance-agent}/shared.ts +15 -3
  179. package/dist/templates/{roadside-assist → roadside-assistance-agent}/tools/acknowledge_disclosure.ts +14 -0
  180. package/dist/templates/{roadside-assist → roadside-assistance-agent}/tools/dispatch_truck.ts +1 -1
  181. package/dist/templates/{roadside-assist → roadside-assistance-agent}/tools/lookup_coverage.ts +11 -0
  182. package/dist/templates/{roadside-assist → roadside-assistance-agent}/tools/service_disclosure.ts +17 -11
  183. package/dist/templates/{roadside-assist → roadside-assistance-agent}/yard.test.ts +4 -0
  184. package/dist/templates/{spoken-summary → spoken-summary-workflow}/agent.test.ts +1 -1
  185. package/dist/templates/{spoken-summary → spoken-summary-workflow}/agent.ts +5 -1
  186. package/dist/templates/{spoken-summary → spoken-summary-workflow}/client.tsx +1 -1
  187. package/dist/templates/{spoken-summary → spoken-summary-workflow}/workflows/summarize.ts +1 -1
  188. package/dist/templates/{solo-rpg → tabletop-rpg-agent}/agent.eval.test.ts +6 -0
  189. package/dist/templates/{solo-rpg → tabletop-rpg-agent}/agent.test.ts +125 -1
  190. package/dist/templates/tabletop-rpg-agent/agent.ts +58 -0
  191. package/dist/templates/{solo-rpg → tabletop-rpg-agent}/shared.ts +84 -3
  192. package/dist/templates/{solo-rpg → tabletop-rpg-agent}/system-prompt.md +1 -1
  193. package/dist/templates/tabletop-rpg-agent/tools/check_state.ts +37 -0
  194. package/dist/templates/{support-line → technical-support-agent}/agent.test.ts +3 -3
  195. package/dist/templates/{support-line → technical-support-agent}/agent.ts +5 -1
  196. package/dist/templates/technical-support-agent/knowledge.ts +165 -0
  197. package/dist/templates/{support-line → technical-support-agent}/nodes.ts +3 -2
  198. package/dist/templates/{support-line → technical-support-agent}/procedure.ts +4 -3
  199. package/dist/templates/technical-support-agent/shared.ts +145 -0
  200. package/dist/templates/{support-line → technical-support-agent}/tools/list_topics.ts +2 -1
  201. package/dist/templates/{infocom-adventure → text-adventure-agent}/agent.eval.test.ts +39 -6
  202. package/dist/templates/{infocom-adventure → text-adventure-agent}/agent.test.ts +79 -1
  203. package/dist/templates/{infocom-adventure → text-adventure-agent}/agent.ts +30 -1
  204. package/dist/templates/{infocom-adventure → text-adventure-agent}/client.tsx +2 -2
  205. package/dist/templates/{infocom-adventure → text-adventure-agent}/shared.ts +37 -0
  206. package/dist/templates/{infocom-adventure → text-adventure-agent}/system-prompt.md +9 -2
  207. package/dist/templates/{briefing-desk → topic-briefing-agent}/agent.eval.test.ts +21 -7
  208. package/dist/templates/{briefing-desk → topic-briefing-agent}/agent.test.ts +23 -11
  209. package/dist/templates/{briefing-desk → topic-briefing-agent}/agent.ts +5 -2
  210. package/dist/templates/{briefing-desk → topic-briefing-agent}/shared.ts +11 -11
  211. package/dist/templates/{briefing-desk → topic-briefing-agent}/slack.ts +1 -1
  212. package/dist/templates/{briefing-desk → topic-briefing-agent}/system-prompt.md +13 -8
  213. package/dist/templates/{briefing-desk → topic-briefing-agent}/tools/send_briefing.ts +43 -5
  214. package/dist/templates/transcription-workflow/agent.test.ts +1 -1
  215. package/dist/templates/transcription-workflow/agent.ts +4 -1
  216. package/dist/templates/transcription-workflow/client.tsx +1 -1
  217. package/dist/templates/transcription-workflow/workflows/batch.ts +2 -2
  218. package/dist/templates/transcription-workflow/workflows/transcribe.ts +1 -1
  219. package/dist/templates/{travel-concierge → travel-concierge-agent}/agent.ts +4 -0
  220. package/dist/templates/{travel-concierge → travel-concierge-agent}/shared.ts +1 -1
  221. package/dist/templates/{web-researcher → web-research-agent}/agent.eval.test.ts +32 -6
  222. package/dist/templates/{web-researcher → web-research-agent}/agent.test.ts +17 -1
  223. package/dist/templates/{web-researcher → web-research-agent}/agent.ts +31 -1
  224. package/dist/templates/{web-researcher → web-research-agent}/system-prompt.md +8 -2
  225. package/dist/templates/{word-wrangler → word-game-agent}/agent.eval.test.ts +79 -18
  226. package/dist/templates/{word-wrangler → word-game-agent}/agent.ts +20 -0
  227. package/dist/templates/{word-wrangler → word-game-agent}/game.ts +1 -1
  228. package/dist/templates/{word-wrangler → word-game-agent}/shared.ts +1 -1
  229. package/dist/test-CjKPQ9_B.mjs +102 -0
  230. package/dist/test.d.ts +37 -12
  231. package/package.json +5 -5
  232. package/dist/_init-BzQbV8Zh.mjs +0 -199
  233. package/dist/_preflight-CcAR577V.mjs +0 -46
  234. package/dist/init-D363Ls5u.mjs +0 -183
  235. package/dist/templates/health-assistant/agent.ts +0 -15
  236. package/dist/templates/simple/agent.ts +0 -8
  237. package/dist/templates/solo-rpg/agent.ts +0 -29
  238. package/dist/templates/solo-rpg/tools/check_state.ts +0 -28
  239. package/dist/templates/support-line/shared.ts +0 -281
  240. package/dist/test-Cllnx81G.mjs +0 -83
  241. /package/dist/templates/{hiring-desk → applicant-screening-agent}/agent.test.ts +0 -0
  242. /package/dist/templates/{hiring-desk → applicant-screening-agent}/client.tsx +0 -0
  243. /package/dist/templates/{hiring-desk → applicant-screening-agent}/leads.json +0 -0
  244. /package/dist/templates/{hiring-desk → applicant-screening-agent}/screening-lock.test.ts +0 -0
  245. /package/dist/templates/{hiring-desk → applicant-screening-agent}/screening-lock.ts +0 -0
  246. /package/dist/templates/{hiring-desk → applicant-screening-agent}/system-prompt.md +0 -0
  247. /package/dist/templates/{hiring-desk → applicant-screening-agent}/tools/candidate_details.ts +0 -0
  248. /package/dist/templates/{hiring-desk → applicant-screening-agent}/tools/proceed_to_emails.ts +0 -0
  249. /package/dist/templates/{hiring-desk → applicant-screening-agent}/tools/read_email.ts +0 -0
  250. /package/dist/templates/{hiring-desk → applicant-screening-agent}/tools/rescore_with_feedback.ts +0 -0
  251. /package/dist/templates/{hiring-desk → applicant-screening-agent}/tools/screen_candidates.ts +0 -0
  252. /package/dist/templates/{call-audit → call-audit-workflow}/agent.eval.test.ts +0 -0
  253. /package/dist/templates/{call-audit → call-audit-workflow}/agent.test.ts +0 -0
  254. /package/dist/templates/{call-audit → call-audit-workflow}/workflows/audit.ts +0 -0
  255. /package/dist/templates/{call-audit → call-audit-workflow}/workflows/ingest.ts +0 -0
  256. /package/dist/templates/{call-audit → call-audit-workflow}/workflows/media.ts +0 -0
  257. /package/dist/templates/{call-audit → call-audit-workflow}/workflows/sync-api.ts +0 -0
  258. /package/dist/templates/{code-interpreter → code-interpreter-agent}/system-prompt.md +0 -0
  259. /package/dist/templates/{pipeline-simple → custom-pipeline-agent}/agent.eval.test.ts +0 -0
  260. /package/dist/templates/{pipeline-simple → custom-pipeline-agent}/stages.test.ts +0 -0
  261. /package/dist/templates/{redline → document-redline-workflow}/agent.eval.test.ts +0 -0
  262. /package/dist/templates/{redline → document-redline-workflow}/agent.test.ts +0 -0
  263. /package/dist/templates/{redline → document-redline-workflow}/form.ts +0 -0
  264. /package/dist/templates/{redline → document-redline-workflow}/workflows/prompts.ts +0 -0
  265. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/agent.eval.test.ts +0 -0
  266. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/client.tsx +0 -0
  267. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/shared.ts +0 -0
  268. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/tools/incident_add_note.ts +0 -0
  269. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/tools/incident_escalate.ts +0 -0
  270. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/tools/incident_get.ts +0 -0
  271. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/tools/incident_triage.ts +0 -0
  272. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/tools/incident_update_status.ts +0 -0
  273. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/tools/ops_dashboard.ts +0 -0
  274. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/tools/ops_protocols.ts +0 -0
  275. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/tools/ops_run_scenario.ts +0 -0
  276. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/tools/resources_dispatch.ts +0 -0
  277. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/tools/resources_get_available.ts +0 -0
  278. /package/dist/templates/{dispatch-center → emergency-dispatch-agent}/tools/resources_update_status.ts +0 -0
  279. /package/dist/templates/{night-owl → entertainment-picks-agent}/shared.ts +0 -0
  280. /package/dist/templates/{night-owl → entertainment-picks-agent}/tools/recommend.ts +0 -0
  281. /package/dist/templates/{executive-assistant → executive-inbox-agent}/agent.test.ts +0 -0
  282. /package/dist/templates/{executive-assistant → executive-inbox-agent}/calendar.test.ts +0 -0
  283. /package/dist/templates/{executive-assistant → executive-inbox-agent}/client.tsx +0 -0
  284. /package/dist/templates/{executive-assistant → executive-inbox-agent}/inbox.ts +0 -0
  285. /package/dist/templates/{executive-assistant → executive-inbox-agent}/nodes.ts +0 -0
  286. /package/dist/templates/{executive-assistant → executive-inbox-agent}/review.ts +0 -0
  287. /package/dist/templates/{executive-assistant → executive-inbox-agent}/tools/accept.ts +0 -0
  288. /package/dist/templates/{executive-assistant → executive-inbox-agent}/tools/ask_question.ts +0 -0
  289. /package/dist/templates/{executive-assistant → executive-inbox-agent}/tools/draft_reply.ts +0 -0
  290. /package/dist/templates/{executive-assistant → executive-inbox-agent}/tools/edit.ts +0 -0
  291. /package/dist/templates/{executive-assistant → executive-inbox-agent}/tools/ignore.ts +0 -0
  292. /package/dist/templates/{executive-assistant → executive-inbox-agent}/tools/inbox_status.ts +0 -0
  293. /package/dist/templates/{executive-assistant → executive-inbox-agent}/tools/new_email.ts +0 -0
  294. /package/dist/templates/{executive-assistant → executive-inbox-agent}/tools/respond.ts +0 -0
  295. /package/dist/templates/{executive-assistant → executive-inbox-agent}/tools/review_memory.ts +0 -0
  296. /package/dist/templates/{executive-assistant → executive-inbox-agent}/tools/triage_inbox.ts +0 -0
  297. /package/dist/templates/{hotel-desk → hotel-reception-agent}/booking.ts +0 -0
  298. /package/dist/templates/{hotel-desk → hotel-reception-agent}/card.ts +0 -0
  299. /package/dist/templates/{hotel-desk → hotel-reception-agent}/catalogs.ts +0 -0
  300. /package/dist/templates/{hotel-desk → hotel-reception-agent}/desk.ts +0 -0
  301. /package/dist/templates/{hotel-desk → hotel-reception-agent}/hotel.ts +0 -0
  302. /package/dist/templates/{hotel-desk → hotel-reception-agent}/policies.ts +0 -0
  303. /package/dist/templates/{hotel-desk → hotel-reception-agent}/records.ts +0 -0
  304. /package/dist/templates/{hotel-desk → hotel-reception-agent}/restaurant.ts +0 -0
  305. /package/dist/templates/{hotel-desk → hotel-reception-agent}/seed.ts +0 -0
  306. /package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/book_business_center.ts +0 -0
  307. /package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/book_spa_appointment.ts +0 -0
  308. /package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/book_tour.ts +0 -0
  309. /package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/lookup_policy.ts +0 -0
  310. /package/dist/templates/{hotel-desk → hotel-reception-agent}/tools/order_flowers.ts +0 -0
  311. /package/dist/templates/{link-digest → link-digest-workflow}/api-client.ts +0 -0
  312. /package/dist/templates/{health-assistant → medication-safety-agent}/agent.eval.test.ts +0 -0
  313. /package/dist/templates/{health-assistant → medication-safety-agent}/fda.test.ts +0 -0
  314. /package/dist/templates/{health-assistant → medication-safety-agent}/fda.ts +0 -0
  315. /package/dist/templates/{health-assistant → medication-safety-agent}/system-prompt.md +0 -0
  316. /package/dist/templates/{health-assistant → medication-safety-agent}/tools/check_drug_interaction.ts +0 -0
  317. /package/dist/templates/{health-assistant → medication-safety-agent}/tools/medication_lookup.ts +0 -0
  318. /package/dist/templates/{recap-workflow → meeting-recap-agent}/agent.eval.test.ts +0 -0
  319. /package/dist/templates/{recap-workflow → meeting-recap-agent}/shared.ts +0 -0
  320. /package/dist/templates/{recap-workflow → meeting-recap-agent}/tools/cancel_recap.ts +0 -0
  321. /package/dist/templates/{recap-workflow → meeting-recap-agent}/tools/keep_transcript.ts +0 -0
  322. /package/dist/templates/{recap-workflow → meeting-recap-agent}/tools/recap_progress.ts +0 -0
  323. /package/dist/templates/{recap-workflow → meeting-recap-agent}/tools/recap_status.ts +0 -0
  324. /package/dist/templates/{recap-workflow → meeting-recap-agent}/tools/request_recap.ts +0 -0
  325. /package/dist/templates/{recap-workflow → meeting-recap-agent}/workflows/tokens.ts +0 -0
  326. /package/dist/templates/{pizza-ordering → pizza-ordering-agent}/agent.eval.test.ts +0 -0
  327. /package/dist/templates/{pizza-ordering → pizza-ordering-agent}/agent.test.ts +0 -0
  328. /package/dist/templates/{pizza-ordering → pizza-ordering-agent}/client.tsx +0 -0
  329. /package/dist/templates/{pizza-ordering → pizza-ordering-agent}/shared.ts +0 -0
  330. /package/dist/templates/{pizza-ordering → pizza-ordering-agent}/system-prompt.md +0 -0
  331. /package/dist/templates/{pizza-ordering → pizza-ordering-agent}/tools/add_pizza.ts +0 -0
  332. /package/dist/templates/{pizza-ordering → pizza-ordering-agent}/tools/place_order.ts +0 -0
  333. /package/dist/templates/{pizza-ordering → pizza-ordering-agent}/tools/remove_pizza.ts +0 -0
  334. /package/dist/templates/{pizza-ordering → pizza-ordering-agent}/tools/set_customer_name.ts +0 -0
  335. /package/dist/templates/{pizza-ordering → pizza-ordering-agent}/tools/update_pizza.ts +0 -0
  336. /package/dist/templates/{pizza-ordering → pizza-ordering-agent}/tools/view_order.ts +0 -0
  337. /package/dist/templates/{podcast-digest → podcast-digest-workflow}/workflows/feeds.ts +0 -0
  338. /package/dist/templates/{podcast-digest → podcast-digest-workflow}/workflows/slack.ts +0 -0
  339. /package/dist/templates/{research-workflow → research-handoff-agent}/agent.eval.test.ts +0 -0
  340. /package/dist/templates/{research-workflow → research-handoff-agent}/shared.ts +0 -0
  341. /package/dist/templates/{research-workflow → research-handoff-agent}/tools/file_it_now.ts +0 -0
  342. /package/dist/templates/{research-workflow → research-handoff-agent}/tools/request_research.ts +0 -0
  343. /package/dist/templates/{research-workflow → research-handoff-agent}/tools/research_progress.ts +0 -0
  344. /package/dist/templates/{research-workflow → research-handoff-agent}/tools/research_status.ts +0 -0
  345. /package/dist/templates/{research-workflow → research-handoff-agent}/workflows/notes.ts +0 -0
  346. /package/dist/templates/{research-workflow → research-handoff-agent}/workflows/prompts.ts +0 -0
  347. /package/dist/templates/{plan-and-execute → research-planner-agent}/agent.test.ts +0 -0
  348. /package/dist/templates/{plan-and-execute → research-planner-agent}/client.tsx +0 -0
  349. /package/dist/templates/{plan-and-execute → research-planner-agent}/procedure.ts +0 -0
  350. /package/dist/templates/{plan-and-execute → research-planner-agent}/prompts.ts +0 -0
  351. /package/dist/templates/{plan-and-execute → research-planner-agent}/tools/plan_status.ts +0 -0
  352. /package/dist/templates/{plan-and-execute → research-planner-agent}/tools/revise_plan.ts +0 -0
  353. /package/dist/templates/{plan-and-execute → research-planner-agent}/tools/start_plan.ts +0 -0
  354. /package/dist/templates/{plan-and-execute → research-planner-agent}/tools/work_next_step.ts +0 -0
  355. /package/dist/templates/{retail → retail-orders-agent}/address.ts +0 -0
  356. /package/dist/templates/{retail → retail-orders-agent}/agent.eval.test.ts +0 -0
  357. /package/dist/templates/{retail → retail-orders-agent}/agent.test.ts +0 -0
  358. /package/dist/templates/{retail → retail-orders-agent}/authenticate.ts +0 -0
  359. /package/dist/templates/{retail → retail-orders-agent}/cancel.ts +0 -0
  360. /package/dist/templates/{retail → retail-orders-agent}/client.tsx +0 -0
  361. /package/dist/templates/{retail → retail-orders-agent}/payment.ts +0 -0
  362. /package/dist/templates/{retail → retail-orders-agent}/pending.test.ts +0 -0
  363. /package/dist/templates/{retail → retail-orders-agent}/refund.ts +0 -0
  364. /package/dist/templates/{retail → retail-orders-agent}/registry.test.ts +0 -0
  365. /package/dist/templates/{retail → retail-orders-agent}/resolve.test.ts +0 -0
  366. /package/dist/templates/{retail → retail-orders-agent}/returns.ts +0 -0
  367. /package/dist/templates/{retail → retail-orders-agent}/seed.json +0 -0
  368. /package/dist/templates/{retail → retail-orders-agent}/seed.test.ts +0 -0
  369. /package/dist/templates/{retail → retail-orders-agent}/shared.test.ts +0 -0
  370. /package/dist/templates/{retail → retail-orders-agent}/store.test.ts +0 -0
  371. /package/dist/templates/{retail → retail-orders-agent}/swap.test.ts +0 -0
  372. /package/dist/templates/{retail → retail-orders-agent}/swap.ts +0 -0
  373. /package/dist/templates/{retail → retail-orders-agent}/system-prompt.md +0 -0
  374. /package/dist/templates/{retail → retail-orders-agent}/tools/cancel_change.ts +0 -0
  375. /package/dist/templates/{retail → retail-orders-agent}/tools/cancel_pending_order.ts +0 -0
  376. /package/dist/templates/{retail → retail-orders-agent}/tools/confirm_change.ts +0 -0
  377. /package/dist/templates/{retail → retail-orders-agent}/tools/exchange_delivered_order_items.ts +0 -0
  378. /package/dist/templates/{retail → retail-orders-agent}/tools/find_user_id_by_email.ts +0 -0
  379. /package/dist/templates/{retail → retail-orders-agent}/tools/find_user_id_by_name_zip.ts +0 -0
  380. /package/dist/templates/{retail → retail-orders-agent}/tools/get_item_details.ts +0 -0
  381. /package/dist/templates/{retail → retail-orders-agent}/tools/get_order_details.ts +0 -0
  382. /package/dist/templates/{retail → retail-orders-agent}/tools/get_product_details.ts +0 -0
  383. /package/dist/templates/{retail → retail-orders-agent}/tools/get_user_details.ts +0 -0
  384. /package/dist/templates/{retail → retail-orders-agent}/tools/list_all_product_types.ts +0 -0
  385. /package/dist/templates/{retail → retail-orders-agent}/tools/modify_pending_order_address.ts +0 -0
  386. /package/dist/templates/{retail → retail-orders-agent}/tools/modify_pending_order_items.ts +0 -0
  387. /package/dist/templates/{retail → retail-orders-agent}/tools/modify_pending_order_payment.ts +0 -0
  388. /package/dist/templates/{retail → retail-orders-agent}/tools/modify_user_address.ts +0 -0
  389. /package/dist/templates/{retail → retail-orders-agent}/tools/return_delivered_order_items.ts +0 -0
  390. /package/dist/templates/{retail → retail-orders-agent}/tools/transfer_to_human_agents.ts +0 -0
  391. /package/dist/templates/{roadside-assist → roadside-assistance-agent}/events.ts +0 -0
  392. /package/dist/templates/{roadside-assist → roadside-assistance-agent}/system-prompt.md +0 -0
  393. /package/dist/templates/{roadside-assist → roadside-assistance-agent}/tools/job_status.ts +0 -0
  394. /package/dist/templates/{roadside-assist → roadside-assistance-agent}/tools/report_location.ts +0 -0
  395. /package/dist/templates/{roadside-assist → roadside-assistance-agent}/yard.ts +0 -0
  396. /package/dist/templates/{spoken-summary → spoken-summary-workflow}/agent.eval.test.ts +0 -0
  397. /package/dist/templates/{spoken-summary → spoken-summary-workflow}/workflows/transcribe.ts +0 -0
  398. /package/dist/templates/{solo-rpg → tabletop-rpg-agent}/client.tsx +0 -0
  399. /package/dist/templates/{solo-rpg → tabletop-rpg-agent}/tools/action_roll.ts +0 -0
  400. /package/dist/templates/{solo-rpg → tabletop-rpg-agent}/tools/burn_momentum.ts +0 -0
  401. /package/dist/templates/{solo-rpg → tabletop-rpg-agent}/tools/oracle.ts +0 -0
  402. /package/dist/templates/{solo-rpg → tabletop-rpg-agent}/tools/setup_character.ts +0 -0
  403. /package/dist/templates/{solo-rpg → tabletop-rpg-agent}/tools/update_state.ts +0 -0
  404. /package/dist/templates/{support-line → technical-support-agent}/agent.eval.test.ts +0 -0
  405. /package/dist/templates/{support-line → technical-support-agent}/client.tsx +0 -0
  406. /package/dist/templates/{support-line → technical-support-agent}/knowledge.json +0 -0
  407. /package/dist/templates/{support-line → technical-support-agent}/prompts.ts +0 -0
  408. /package/dist/templates/{support-line → technical-support-agent}/system-prompt.md +0 -0
  409. /package/dist/templates/{support-line → technical-support-agent}/tools/answer_question.ts +0 -0
  410. /package/dist/templates/{support-line → technical-support-agent}/tools/log_ticket.ts +0 -0
  411. /package/dist/templates/{infocom-adventure → text-adventure-agent}/tools/game_state_drop.ts +0 -0
  412. /package/dist/templates/{infocom-adventure → text-adventure-agent}/tools/game_state_flag.ts +0 -0
  413. /package/dist/templates/{infocom-adventure → text-adventure-agent}/tools/game_state_get.ts +0 -0
  414. /package/dist/templates/{infocom-adventure → text-adventure-agent}/tools/game_state_move.ts +0 -0
  415. /package/dist/templates/{infocom-adventure → text-adventure-agent}/tools/game_state_restart.ts +0 -0
  416. /package/dist/templates/{infocom-adventure → text-adventure-agent}/tools/game_state_score.ts +0 -0
  417. /package/dist/templates/{infocom-adventure → text-adventure-agent}/tools/game_state_take.ts +0 -0
  418. /package/dist/templates/{briefing-desk → topic-briefing-agent}/tools/briefing_so_far.ts +0 -0
  419. /package/dist/templates/{briefing-desk → topic-briefing-agent}/tools/research_topic.ts +0 -0
  420. /package/dist/templates/{briefing-desk → topic-briefing-agent}/tools/verify_claim.ts +0 -0
  421. /package/dist/templates/{travel-concierge → travel-concierge-agent}/agent.eval.test.ts +0 -0
  422. /package/dist/templates/{travel-concierge → travel-concierge-agent}/agent.test.ts +0 -0
  423. /package/dist/templates/{travel-concierge → travel-concierge-agent}/call-events.ts +0 -0
  424. /package/dist/templates/{travel-concierge → travel-concierge-agent}/client.tsx +0 -0
  425. /package/dist/templates/{travel-concierge → travel-concierge-agent}/routing.ts +0 -0
  426. /package/dist/templates/{travel-concierge → travel-concierge-agent}/system-prompt.md +0 -0
  427. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/book_car_rental.ts +0 -0
  428. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/book_excursion.ts +0 -0
  429. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/book_hotel.ts +0 -0
  430. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/cancel_action.ts +0 -0
  431. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/cancel_ticket.ts +0 -0
  432. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/complete_or_escalate.ts +0 -0
  433. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/confirm_action.ts +0 -0
  434. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/lookup_booking.ts +0 -0
  435. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/search_car_rentals.ts +0 -0
  436. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/search_excursions.ts +0 -0
  437. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/search_flights.ts +0 -0
  438. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/search_hotels.ts +0 -0
  439. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/to_car_rental_assistant.ts +0 -0
  440. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/to_excursion_assistant.ts +0 -0
  441. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/to_flight_assistant.ts +0 -0
  442. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/to_hotel_assistant.ts +0 -0
  443. /package/dist/templates/{travel-concierge → travel-concierge-agent}/tools/update_ticket.ts +0 -0
  444. /package/dist/templates/{word-wrangler → word-game-agent}/agent.test.ts +0 -0
  445. /package/dist/templates/{word-wrangler → word-game-agent}/client.tsx +0 -0
  446. /package/dist/templates/{word-wrangler → word-game-agent}/guess.ts +0 -0
  447. /package/dist/templates/{word-wrangler → word-game-agent}/player.ts +0 -0
  448. /package/dist/templates/{word-wrangler → word-game-agent}/system-prompt.md +0 -0
  449. /package/dist/templates/{word-wrangler → word-game-agent}/tools/final_score.ts +0 -0
  450. /package/dist/templates/{word-wrangler → word-game-agent}/tools/relay_description.ts +0 -0
  451. /package/dist/templates/{word-wrangler → word-game-agent}/tools/repeat_word.ts +0 -0
  452. /package/dist/templates/{word-wrangler → word-game-agent}/tools/skip_word.ts +0 -0
  453. /package/dist/templates/{word-wrangler → word-game-agent}/tools/start_game.ts +0 -0
  454. /package/dist/templates/{word-wrangler → word-game-agent}/words.ts +0 -0
@@ -1,6 +1,11 @@
1
1
  /** The def a DEPLOYED agent runs: authored, plus what `tools/` declares. */
2
2
  import agentDef from "virtual:aai/agent";
3
- import type { InferToolInput, RandomSource, SlotHolder } from "@alexkroman1/aai";
3
+ import type {
4
+ AgentSessionContext,
5
+ InferToolInput,
6
+ RandomSource,
7
+ SlotHolder,
8
+ } from "@alexkroman1/aai";
4
9
  import { createSeededRandom, isToolFailure } from "@alexkroman1/aai";
5
10
  import {
6
11
  createToolContext,
@@ -20,6 +25,7 @@ import {
20
25
  gameView,
21
26
  inCrisis,
22
27
  isGameOver,
28
+ liveSheet,
23
29
  MAX_LOG_ENTRIES,
24
30
  MAX_NPCS,
25
31
  MIN_MOMENTUM,
@@ -894,3 +900,121 @@ describe("the story flow", () => {
894
900
  // restored rather than recomputed) has no path left to exercise it: a session
895
901
  // is the whole life of a game now.
896
902
  });
903
+
904
+ // ── The prompt carries the sheet ─────────────────────────────────────────────
905
+ //
906
+ // `systemPrompt` is a RESOLVER here: `system-prompt.md` supplies the rules and
907
+ // `liveSheet` appends the campaign as it stands before every model request.
908
+ // What that replaced was FLOW rule 1 — "call check_state as your FIRST tool
909
+ // call every turn" — a model round trip in front of every turn of a live voice
910
+ // game, enforced by nothing.
911
+
912
+ describe("the campaign sheet the narrator is given", () => {
913
+ /** The def's resolver, which is what a request assembles the instructions from. */
914
+ const resolve = (ctx: AgentSessionContext): string => {
915
+ const prompt = agentDef.systemPrompt;
916
+ if (typeof prompt !== "function") throw new Error("systemPrompt is not a resolver");
917
+ return prompt(ctx);
918
+ };
919
+
920
+ test("it is a resolver, and the rules file survives into it", () => {
921
+ // The build discovers `system-prompt.md` and hands it to `withSystemPrompt`,
922
+ // which leaves a resolver alone — so the file reaches the model through the
923
+ // resolver's own import, and this is what proves it does.
924
+ expect(typeof agentDef.systemPrompt).toBe("function");
925
+ expect(resolve(createToolContext())).toContain("CORE MECHANIC - ACTION ROLL");
926
+ });
927
+
928
+ test("before setup it says so, rather than printing an empty sheet", () => {
929
+ const ctx = createToolContext();
930
+ const sheet = liveSheet(gameSlot.get(ctx));
931
+ expect(sheet).toContain("no character yet");
932
+ expect(sheet).toContain("setup_character");
933
+ // The tracks of a campaign that does not exist would read as a live sheet.
934
+ // (Asserted on the sheet, not the whole prompt: the rules file has its own
935
+ // MOMENTUM section and always will.)
936
+ expect(sheet).not.toContain("Momentum");
937
+ expect(resolve(ctx).endsWith(sheet)).toBe(true);
938
+ });
939
+
940
+ test("a roll's consequences are in the NEXT request's prompt, with no tool call in between", async () => {
941
+ const ctx = createToolContext();
942
+ seedPlaying(ctx);
943
+ // A MISS: the tracks and the momentum move, and the chaos factor with them.
944
+ const before = resolve(ctx);
945
+ expect(before).toContain(`Momentum ${DEFAULT_STATE.momentum}`);
946
+
947
+ expectToolOk(
948
+ await run(
949
+ "action_roll",
950
+ { move: "face_danger", stat: "iron", position: "desperate", effect: "standard" },
951
+ createToolContext({ ...ctx, random: dice(1, 1, 10, 10) }),
952
+ ),
953
+ );
954
+
955
+ const after = resolve(ctx);
956
+ const game = gameSlot.get(ctx);
957
+ expect(after).toContain(`Momentum ${game.momentum}`);
958
+ expect(after).toContain(`chaos factor ${game.chaosFactor}`);
959
+ expect(after).toContain(`Health ${game.health}/`);
960
+ // And the burn window, which is the one thing the narrator most often lost
961
+ // track of between turns.
962
+ expect(after).toContain("burn_momentum can still upgrade it");
963
+ });
964
+
965
+ test("the cast, the clocks and the act are all on it", () => {
966
+ const ctx = createToolContext();
967
+ const state = playingState();
968
+ state.playerName = "Kael";
969
+ state.currentLocation = "Ashfall Keep";
970
+ state.clocks[0]!.filled = 2;
971
+ state.storyBlueprint = {
972
+ structureType: "3act",
973
+ centralConflict: "c",
974
+ antagonistForce: "a",
975
+ thematicThread: "t",
976
+ currentAct: 2,
977
+ storyComplete: false,
978
+ acts: [
979
+ { phase: "setup", title: "One", goal: "g1", mood: "m1", transitionTrigger: "t1" },
980
+ { phase: "turn", title: "Two", goal: "g2", mood: "m2", transitionTrigger: "t2" },
981
+ ],
982
+ };
983
+ seedPlaying(ctx, state);
984
+
985
+ const sheet = resolve(ctx);
986
+ expect(sheet).toContain("Kael");
987
+ expect(sheet).toContain("Doom 2/4 (threat)");
988
+ expect(sheet).toContain("Mira (neutral, bond 0)");
989
+ expect(sheet).toContain("Act 2 of 2 (turn)");
990
+ });
991
+
992
+ test("crisis and game over are DERIVED, so an emptied track cannot read as survivable", () => {
993
+ const ctx = createToolContext();
994
+ const hurt = playingState();
995
+ hurt.spirit = 0;
996
+ seedPlaying(ctx, hurt);
997
+ expect(liveSheet(gameSlot.get(ctx))).toContain("CRISIS");
998
+ expect(inCrisis(gameSlot.get(ctx))).toBe(true);
999
+
1000
+ const dead = playingState();
1001
+ dead.health = 0;
1002
+ dead.spirit = 0;
1003
+ seedPlaying(ctx, dead);
1004
+ expect(liveSheet(gameSlot.get(ctx))).toContain("GAME OVER");
1005
+ expect(isGameOver(gameSlot.get(ctx))).toBe(true);
1006
+ });
1007
+
1008
+ test("each session is told its OWN campaign", () => {
1009
+ // The resolver reads through `ctx.slots`, so it is per session for the same
1010
+ // reason every tool is.
1011
+ const one = createToolContext();
1012
+ const two = createToolContext();
1013
+ const named = playingState();
1014
+ named.playerName = "Kael";
1015
+ seedPlaying(one, named);
1016
+
1017
+ expect(resolve(one)).toContain("Kael");
1018
+ expect(resolve(two)).toContain("no character yet");
1019
+ });
1020
+ });
@@ -0,0 +1,58 @@
1
+ import { agent } from "@alexkroman1/aai";
2
+ import { gameProjection, gameSlot, liveSheet, storyFlow } from "./shared.ts";
3
+ // The rules of the game. The build discovers `system-prompt.md` on its own —
4
+ // importing it is what lets the resolver below compose against it, and
5
+ // `withSystemPrompt` leaves a resolver exactly as written.
6
+ import prompt from "./system-prompt.md?raw";
7
+
8
+ export default agent({
9
+ name: "Solo RPG",
10
+ // The listing line, for a reader picking an agent out of a list. It says what
11
+ // the borrowed systems DO rather than naming them — the attribution belongs
12
+ // in `shared.ts`, not in a picker row.
13
+ description: "Narrates a solo tabletop campaign and keeps its dice, tracks and clocks",
14
+ greeting:
15
+ "Welcome. Tell me your name, or describe the kind of story you want, and we will begin. You can say something like, dark fantasy warrior named Kael, or just give me a name and I will build a world around you.",
16
+ sttPrompt:
17
+ "Solo RPG terms: strong hit, weak hit, miss, momentum, chaos factor, clock, disposition, bond, edge, heart, iron, shadow, wits, face danger, compel, gather information, secure advantage, clash, strike, endure harm, endure stress, make connection, test bond, resupply, world shaping",
18
+ maxSteps: 8,
19
+
20
+ /**
21
+ * The rules, plus the campaign sheet as it stands RIGHT NOW.
22
+ *
23
+ * A `systemPrompt` resolver is called once per model request, so the tracks,
24
+ * the momentum, the chaos factor, the clocks and the cast are in front of the
25
+ * narrator before it says anything. The prompt's FLOW section used to open
26
+ * with "call check_state as your FIRST tool call every turn … NEVER remember
27
+ * or guess stats from prior turns" — a full model round trip per turn of a
28
+ * live voice game, and only advice, so the turns it was skipped on are the
29
+ * turns the sheet drifted.
30
+ *
31
+ * `ctx` is an `AgentSessionContext`: the session id, the env and the slots.
32
+ * Enough for `gameSlot.get`, and deliberately nothing that could speak — a
33
+ * resolver returns instructions, it does not take a turn.
34
+ *
35
+ * The dialog's own `instruction` is NOT rendered here. `storyFlow` is declared
36
+ * below, and the runtime already appends the active state's instruction to the
37
+ * same prompt; saying it twice is how two copies of one rule drift apart.
38
+ */
39
+ systemPrompt: (ctx) => `${prompt}\n\n${liveSheet(gameSlot.get(ctx))}`,
40
+
41
+ // One declaration replaces a `ctx.send("game_state", state)` in every
42
+ // state-mutating tool — six of them, and adding a seventh meant
43
+ // remembering to push or watching the UI quietly fall out of sync.
44
+ // NOT the identity: `gameView` withholds the seven fields the sidebar never
45
+ // reads and every unplayed act's goal and mood with them, which is the only
46
+ // defence there is against a spoiler leak (see `shared.ts`). The slot's own
47
+ // default is what a session that has run no tool projects.
48
+ syncState: gameProjection,
49
+
50
+ // The flow gates the tools whether or not it is declared here. What DECLARING
51
+ // it buys is the other half the states were written for: the active state's
52
+ // `instruction` goes in front of the model on every turn, rather than only on
53
+ // the turns a tool result happens to carry it back. `system-prompt.md` already
54
+ // tells the narrator to read `instruction` as ground truth — this is what
55
+ // makes that true before the first tool call of a turn, and it is the field
56
+ // six of the other seven flow templates declare.
57
+ dialogs: [storyFlow],
58
+ });
@@ -362,7 +362,7 @@ export const gameSlot = sessionSlot("game", () => structuredClone(DEFAULT_STATE)
362
362
  // `revertConsequences` and `update_state`, which is the shape `after` exists
363
363
  // to replace: a new tool that empties a track now cannot store a stale
364
364
  // `gameOver`, and `gameOver` is what the story flow's `DOWNED` transition
365
- // reads. `dispatch-center`'s board is the same pattern one template over.
365
+ // reads. `emergency-dispatch-agent`'s board is the same pattern one template over.
366
366
  after: updateCrisisFlags,
367
367
  caps: GAME_CAPS,
368
368
  });
@@ -453,7 +453,7 @@ export const gameProjection = gameSlot.projection(gameView);
453
453
  * **What stays in {@link GameState} is what the CLIENT renders** —
454
454
  * `initialized`, `crisisMode`, `gameOver` — because `syncState` carries one
455
455
  * projection and it is the campaign's. The flow holds the POSITION and the slot
456
- * holds the campaign, which is the same split `travel-concierge` makes between
456
+ * holds the campaign, which is the same split `travel-concierge-agent` makes between
457
457
  * its gate and its trip: one tool call moves both, in one synchronous window
458
458
  * each.
459
459
  *
@@ -601,7 +601,7 @@ export function makeNpc(opts: {
601
601
  * ordinal first would silently retarget it at whatever is at index 0. An exact
602
602
  * name always wins; the ordinal is only consulted when nothing was named.
603
603
  *
604
- * Generic over the clock's own type — as `dispatch-center`'s `findIncident` is —
604
+ * Generic over the clock's own type — as `emergency-dispatch-agent`'s `findIncident` is —
605
605
  * so one lookup serves an `updateTool` draft and a frozen read alike.
606
606
  */
607
607
  export function findClock<C extends { id: string; name: string }>(
@@ -669,6 +669,87 @@ export function stateSummary(state: FrozenGameState) {
669
669
  };
670
670
  }
671
671
 
672
+ /**
673
+ * The campaign sheet as the NARRATOR is given it, before every reply.
674
+ *
675
+ * `agent.ts` appends this to the system prompt through a `systemPrompt`
676
+ * resolver, so every number the narrator must not invent — the tracks, the
677
+ * momentum, the chaos factor, the clocks, who is in the scene — is in front of
678
+ * the model on each request.
679
+ *
680
+ * **This is what retired the FLOW rule that opened every turn with a tool
681
+ * call.** `system-prompt.md` used to say "Call check_state as your FIRST tool
682
+ * call every turn, before narrating or rolling … NEVER remember or guess stats
683
+ * from prior turns": an instruction that bought a full model round trip on every
684
+ * turn of a live voice game when it was obeyed, and a desynced sheet when it was
685
+ * not. A number the model may not guess belongs in the prompt, not behind a rule
686
+ * telling it to go and fetch one.
687
+ *
688
+ * `check_state` is NOT retired. It answers with the chronicle, the whole NPC
689
+ * records and the story blueprint as well, and it is the tool for a turn where
690
+ * the narrator wants the full board or the position restated. What went is the
691
+ * compulsory read-back.
692
+ *
693
+ * Prose rather than {@link stateSummary}'s JSON, because this is read as
694
+ * instructions rather than as a tool result — and deliberately SHORTER than that
695
+ * summary: it lands on every request, where a tool result lands once. The
696
+ * position (`state`/`instruction`) is not here either, since `storyFlow` is
697
+ * declared in `agent({ dialogs })` and the runtime already puts the active
698
+ * state's `instruction` in the same prompt.
699
+ */
700
+ export function liveSheet(game: FrozenGameState): string {
701
+ if (!game.initialized) {
702
+ return [
703
+ "LIVE CAMPAIGN SHEET: no character yet, and no campaign. setup_character is",
704
+ "the only way in and it fills every field itself — take whatever the player",
705
+ "gave you, invent the rest, and call it this turn.",
706
+ ].join("\n");
707
+ }
708
+
709
+ const clocks = game.clocks.map((c) => `${c.name} ${c.filled}/${c.segments} (${c.clockType})`);
710
+ const cast = game.npcs
711
+ .filter((n) => n.status !== "deceased")
712
+ .map((n) => `${n.name} (${n.disposition}, bond ${n.bond})`);
713
+
714
+ const lines = [
715
+ "LIVE CAMPAIGN SHEET (the engine's own record, refreshed before every reply —",
716
+ "these are ground truth, they are never a turn behind, and you neither guess",
717
+ "them nor change them by narrating):",
718
+ `- ${game.playerName || "The player"}, ${game.settingArchetype} — ${game.settingGenre}, ${game.settingTone}`,
719
+ `- Stats: edge ${game.edge}, heart ${game.heart}, iron ${game.iron}, shadow ${game.shadow}, wits ${game.wits}`,
720
+ `- Health ${game.health}/${MAX_RESOURCE}, spirit ${game.spirit}/${MAX_RESOURCE}, supply ${game.supply}/${MAX_RESOURCE}`,
721
+ `- Momentum ${game.momentum} (max ${game.maxMomentum}), chaos factor ${game.chaosFactor}`,
722
+ `- Scene ${game.sceneCount}, ${game.currentLocation || "location unset"}, ${game.timeOfDay || "time unset"}`,
723
+ `- Clocks: ${clocks.length > 0 ? clocks.join("; ") : "none"}`,
724
+ `- NPCs: ${cast.length > 0 ? cast.join("; ") : "none"}`,
725
+ ];
726
+
727
+ if (game.storyBlueprint) {
728
+ const { currentAct, acts, storyComplete } = game.storyBlueprint;
729
+ const phase = acts[currentAct - 1]?.phase ?? "unknown";
730
+ lines.push(
731
+ `- Act ${currentAct} of ${acts.length} (${phase})${storyComplete ? ", story complete" : ""}`,
732
+ );
733
+ }
734
+
735
+ // DERIVED, for the reason `stateSummary` derives them: `after` restores the
736
+ // stored flags only once a mutating body has returned, so a sheet rendered
737
+ // from the fields would report a just-emptied track as survivable.
738
+ if (isGameOver(game)) {
739
+ lines.push("- GAME OVER: nothing can be rolled or updated. Narrate the ending.");
740
+ } else if (inCrisis(game)) {
741
+ lines.push("- CRISIS: a track is empty. Every miss now is more dangerous.");
742
+ }
743
+ if (game.kidMode) lines.push("- Kid mode is on.");
744
+ if (game.lastRoll) {
745
+ lines.push(
746
+ `- A ${game.lastRoll.result} on ${game.lastRoll.move} is standing — burn_momentum can still upgrade it.`,
747
+ );
748
+ }
749
+
750
+ return lines.join("\n");
751
+ }
752
+
672
753
  // ── Dice System ──────────────────────────────────────────────────────────────
673
754
  export type RollOutcome = "STRONG_HIT" | "WEAK_HIT" | "MISS";
674
755
 
@@ -98,7 +98,7 @@ CORRECTION SYSTEM:
98
98
  If the player starts a message with ##, treat it as a correction to the previous turn. Acknowledge the correction and rewrite the scene.
99
99
 
100
100
  FLOW:
101
- 1. Call check_state as your FIRST tool call every turn, before narrating or rolling. Read the returned values as ground truth. NEVER remember or guess stats from prior turns. Its `state` and `instruction` say where the story is and what that expects — every other tool answers with the same pair, and a tool that is not available yet refuses and tells you what has to happen first. You do not have to remember any of this between turns.
101
+ 1. The LIVE CAMPAIGN SHEET at the end of these instructions is refreshed before every reply: the tracks, momentum, chaos factor, clocks, cast, and which act you are in. It is the engine's own record and it is never a turn behind, so read it rather than remembering, and never state a number that contradicts it. You do not have to open a turn with a tool call to find out where you are. `check_state` is still there for the full board — the chronicle, the whole NPC records, the story blueprint — and for the position (`state` and `instruction`) restated on demand; every other tool answers with that same pair, and a tool that is not available yet refuses and tells you what has to happen first.
102
102
  2. Present situations with tension and choice. Two to three options, but accept anything.
103
103
  3. For ANY risky action, you MUST call action_roll. NEVER narrate success or failure without rolling. NEVER reduce health, spirit, supply, or momentum yourself — action_roll does this through code. If you narrate damage without calling action_roll, the sidebar will be wrong and the game will break.
104
104
  4. After location changes, new NPCs, or other world changes, call update_state. But NEVER manually set health, spirit, supply, or momentum in update_state unless the player is resting or trading — action_roll handles combat and risk.
@@ -0,0 +1,37 @@
1
+ import { gameSlot, stateSummary, storyFlow } from "../shared.ts";
2
+
3
+ /**
4
+ * `gameSlot.tool`, the READING half: the body is handed the value, so it needs
5
+ * neither a context annotation nor an opening `gameSlot.get(ctx)` — and what it
6
+ * is handed is deep-frozen, which is what makes "this tool only reads" a
7
+ * property of the declaration rather than a claim in a comment.
8
+ *
9
+ * **Legal in every state, which is why it is not a `storyFlow.tool`** — it is
10
+ * answerable before a character exists, and "there is no campaign yet" is a
11
+ * thing the narrator may need said back. It is also the one tool that reports
12
+ * the POSITION, the way `research-planner-agent`'s `plan_status` does: the
13
+ * position comes from the machine rather than from a second reading of the
14
+ * campaign's fields, so "there is no character yet" is the same fact here as the
15
+ * refusal `action_roll` would give.
16
+ *
17
+ * **It is no longer called every turn, and that is the point of `liveSheet`.**
18
+ * The prompt used to open every turn with this call — a full model round trip
19
+ * before a word of narration, on a live voice game — because the tracks and the
20
+ * clocks reached the model no other way. They ride in the system prompt now
21
+ * (`agent.ts`'s `systemPrompt` resolver), so what is left here is the half a
22
+ * sheet cannot carry on every request: the chronicle, the whole NPC records,
23
+ * the story blueprint, and the position on demand.
24
+ */
25
+ export default gameSlot.tool({
26
+ description:
27
+ "Returns the FULL current game state — including the chronicle, the whole NPC records and the story blueprint — and where the story is. The tracks, momentum, clocks and cast are already in your instructions and refreshed every turn, so reach for this when you want the rest of the board or the position restated, not as a matter of routine.",
28
+ execute: (_args, game, ctx) => ({
29
+ // The position SPREAD rather than renamed. `at`/`next`/`storyOver` said the
30
+ // same three things a gated tool's result already carries as `state`,
31
+ // `instruction` and `done` — so within one agent the model was reading its
32
+ // own position under two key sets, and the system prompt's claim that
33
+ // "every other tool answers with the same pair" was simply false.
34
+ ...storyFlow.position(ctx),
35
+ ...stateSummary(game),
36
+ }),
37
+ });
@@ -15,7 +15,7 @@ import {
15
15
  } from "@alexkroman1/aai/testing";
16
16
  import { isToolFailure, type ToolFailure } from "@alexkroman1/aai/utils";
17
17
  import { describe, expect, test } from "vitest";
18
-
18
+ import { retrieve } from "./knowledge.ts";
19
19
  import { MAX_ATTEMPTS, runCorrectiveRag } from "./procedure.ts";
20
20
  import {
21
21
  ANSWER_SYSTEM,
@@ -24,7 +24,7 @@ import {
24
24
  GROUNDED_SYSTEM,
25
25
  REWRITE_SYSTEM,
26
26
  } from "./prompts.ts";
27
- import { ASKED_CAP, retrieve, supportProjection, supportSlot, supportView } from "./shared.ts";
27
+ import { ASKED_CAP, supportProjection, supportSlot, supportView } from "./shared.ts";
28
28
  /** TYPE-only, so nothing here re-registers a tool module: the defs under test
29
29
  * still come from `agentDef`, which is what a deploy resolves. */
30
30
  import type AnswerQuestion from "./tools/answer_question.ts";
@@ -169,7 +169,7 @@ function phases(calls: string[]): string[] {
169
169
 
170
170
  // ─── 1. The retriever ────────────────────────────────────────────────────────
171
171
 
172
- describe("retrieval (shared.ts)", () => {
172
+ describe("retrieval (knowledge.ts)", () => {
173
173
  test("ranks the document a caller's words actually point at", () => {
174
174
  expect(retrieve("my broadband light is red")[0]?.doc.id).toBe("D1");
175
175
  expect(retrieve("nobody was home for the engineer")[0]?.doc.id).toBe("D6");
@@ -4,7 +4,7 @@ import { PRODUCT, supportProjection } from "./shared.ts";
4
4
  /**
5
5
  * A support line that grades its own retrieval before it speaks — the
6
6
  * self-RAG / CRAG graph, ported to voice. `procedure.ts` holds the loop, `prompts.ts`
7
- * the attribution and the graders, `shared.ts` the knowledge base and why its
7
+ * the attribution and the graders, `knowledge.ts` the knowledge base and why its
8
8
  * retriever is lexical.
9
9
  *
10
10
  * **Why this shape is worth the model calls on a phone line.** A naive RAG voice
@@ -18,6 +18,10 @@ import { PRODUCT, supportProjection } from "./shared.ts";
18
18
  */
19
19
  export default agent({
20
20
  name: `${PRODUCT} Support`,
21
+ // For a reader of a LIST of agents, not for the model. It says what the line
22
+ // does with a question it cannot answer, because that is the half a caller
23
+ // reading a directory wants to know.
24
+ description: `Answers ${PRODUCT} support questions from a graded knowledge base, and logs a ticket when it cannot`,
21
25
  // The trace exists before the first tool call, so a resumed connection has
22
26
  // something to project.
23
27
  // The projection is also the privacy boundary: a logged ticket carries the
@@ -0,0 +1,165 @@
1
+ // Copyright 2026 the AAI authors. MIT license.
2
+ /**
3
+ * The knowledge base and the retriever over it.
4
+ *
5
+ * **Split from `shared.ts` to keep `knowledge.json` out of the browser
6
+ * bundle.** The index below is built at MODULE SCOPE — `for (const doc of
7
+ * DOCS)` runs on import — so every document was reachable from any module that
8
+ * touched this file, and `client.tsx` touches `shared.ts` for its view. Measured
9
+ * before this split: the full text of every article was present in the built
10
+ * client bundle.
11
+ *
12
+ * `shared.ts` keeps `PRODUCT` alone, as a named import off the same JSON, so the
13
+ * browser still derives the tab title from the knowledge base without taking
14
+ * the articles with it. Everything that reads `docs` lives here, and the browser
15
+ * never imports this module.
16
+ *
17
+ * **The retriever is lexical, and that is a deliberate difference from the
18
+ * notebooks this template ports.** Their self-RAG and CRAG graphs retrieve from
19
+ * a Chroma vectorstore over OpenAI embeddings; this SDK has no vector store
20
+ * (`ctx.vector` was removed — see the root guide), and a template that needed
21
+ * one would be a template nobody can run. So retrieval here is term overlap
22
+ * with an inverse-document-frequency weight, which is genuinely worse at
23
+ * meaning and genuinely fine at words.
24
+ *
25
+ * That makes the corrective loop MORE valuable rather than less, which is the
26
+ * point worth taking away: CRAG exists because retrieval is imperfect, and a
27
+ * weaker retriever is exactly the case its grader and its query rewriter were
28
+ * designed for. A caller saying "my internet keeps dying at night" retrieves
29
+ * nothing useful by words alone — the rewrite step is what turns it into
30
+ * "evening slowdown congestion peak time" and finds D10.
31
+ */
32
+ import { docs } from "./knowledge.json" with { type: "json" };
33
+
34
+ export interface Doc {
35
+ id: string;
36
+ title: string;
37
+ topic: string;
38
+ text: string;
39
+ }
40
+
41
+ export const DOCS: Doc[] = docs;
42
+
43
+ export const TOPICS: string[] = [...new Set(DOCS.map((doc) => doc.topic))].sort();
44
+
45
+ // ─── Retrieval ───────────────────────────────────────────────────────────────
46
+
47
+ /** Words that match everything and therefore rank nothing. */
48
+ const STOPWORDS = new Set([
49
+ "the",
50
+ "and",
51
+ "for",
52
+ "you",
53
+ "your",
54
+ "are",
55
+ "was",
56
+ "with",
57
+ "that",
58
+ "this",
59
+ "have",
60
+ "has",
61
+ "can",
62
+ "not",
63
+ "but",
64
+ "how",
65
+ "why",
66
+ "what",
67
+ "when",
68
+ "will",
69
+ "from",
70
+ "into",
71
+ "out",
72
+ "get",
73
+ "does",
74
+ "did",
75
+ "any",
76
+ "all",
77
+ "our",
78
+ "their",
79
+ "there",
80
+ "then",
81
+ "than",
82
+ "about",
83
+ "just",
84
+ "been",
85
+ "were",
86
+ "they",
87
+ "them",
88
+ "his",
89
+ "her",
90
+ "its",
91
+ "one",
92
+ "two",
93
+ "who",
94
+ "whom",
95
+ "some",
96
+ ]);
97
+
98
+ function tokenize(text: string): string[] {
99
+ return text
100
+ .toLowerCase()
101
+ .split(/[^a-z0-9]+/)
102
+ .filter((word) => word.length > 2 && !STOPWORDS.has(word));
103
+ }
104
+
105
+ /**
106
+ * Term → how many documents contain it, and per document, term → how often it
107
+ * occurs. Both computed once at module load, in the one pass that has to visit
108
+ * every token anyway.
109
+ *
110
+ * The per-document COUNTS are what scoring needs. Keeping the raw token list
111
+ * instead made `retrieve` walk it end to end once per query term, allocating a
112
+ * filtered array each time, to recover a number this loop already had.
113
+ */
114
+ const DOC_FREQUENCY = new Map<string, number>();
115
+ const DOC_TERM_COUNTS = new Map<string, Map<string, number>>();
116
+ for (const doc of DOCS) {
117
+ const counts = new Map<string, number>();
118
+ for (const token of tokenize(`${doc.title} ${doc.topic} ${doc.text}`)) {
119
+ counts.set(token, (counts.get(token) ?? 0) + 1);
120
+ }
121
+ DOC_TERM_COUNTS.set(doc.id, counts);
122
+ for (const term of counts.keys()) {
123
+ DOC_FREQUENCY.set(term, (DOC_FREQUENCY.get(term) ?? 0) + 1);
124
+ }
125
+ }
126
+
127
+ /** How many documents one retrieval returns before grading. */
128
+ export const RETRIEVE_K = 4;
129
+
130
+ export interface Retrieved {
131
+ doc: Doc;
132
+ score: number;
133
+ }
134
+
135
+ /**
136
+ * The `retrieve` node: top-k by idf-weighted term overlap.
137
+ *
138
+ * A term in one document is worth much more than a term in eight — without the
139
+ * weight, "fibre" and "hub" (in nearly every document) drown the one word that
140
+ * actually distinguishes the caller's question.
141
+ */
142
+ export function retrieve(query: string, k: number = RETRIEVE_K): Retrieved[] {
143
+ const terms = new Set(tokenize(query));
144
+ if (terms.size === 0) return [];
145
+ const scored: Retrieved[] = [];
146
+ for (const doc of DOCS) {
147
+ const counts = DOC_TERM_COUNTS.get(doc.id);
148
+ let score = 0;
149
+ for (const term of terms) {
150
+ const hits = counts?.get(term) ?? 0;
151
+ if (hits === 0) continue;
152
+ const frequency = DOC_FREQUENCY.get(term) ?? 1;
153
+ // Diminishing returns per repeat, so one long document cannot win on
154
+ // repetition alone.
155
+ score += Math.log(1 + hits) * Math.log(DOCS.length / frequency + 1);
156
+ }
157
+ if (score > 0) scored.push({ doc, score });
158
+ }
159
+ return scored.sort((a, b) => b.score - a.score).slice(0, k);
160
+ }
161
+
162
+ /** One document as the answer prompt sees it. */
163
+ export function formatDoc(doc: Doc): string {
164
+ return `[${doc.id}] ${doc.title}\n${doc.text}`;
165
+ }
@@ -22,6 +22,8 @@ import type {
22
22
  GenerateResult,
23
23
  GuardrailVerdict,
24
24
  } from "@alexkroman1/aai";
25
+ import type { Doc } from "./knowledge.ts";
26
+ import { formatDoc } from "./knowledge.ts";
25
27
  import {
26
28
  ANSWER_SYSTEM,
27
29
  ANSWERS_SYSTEM,
@@ -31,8 +33,7 @@ import {
31
33
  GROUNDED_SYSTEM,
32
34
  REWRITE_SYSTEM,
33
35
  } from "./prompts.ts";
34
- import type { Doc, GradedDoc } from "./shared.ts";
35
- import { formatDoc } from "./shared.ts";
36
+ import type { GradedDoc } from "./shared.ts";
36
37
 
37
38
  /**
38
39
  * One closed question, asked with the schema that constrains it.
@@ -62,6 +62,8 @@ import {
62
62
  procedure,
63
63
  } from "@alexkroman1/aai";
64
64
  import { assign, fromPromise, setup } from "xstate";
65
+ import type { Doc } from "./knowledge.ts";
66
+ import { retrieve } from "./knowledge.ts";
65
67
  import {
66
68
  generateAnswer,
67
69
  gradeDocuments,
@@ -69,8 +71,7 @@ import {
69
71
  gradeUseful,
70
72
  transformQuery,
71
73
  } from "./nodes.ts";
72
- import type { AnswerTrace, Doc, GradedDoc, TraceStep } from "./shared.ts";
73
- import { retrieve } from "./shared.ts";
74
+ import type { AnswerTrace, GradedDoc, TraceStep } from "./shared.ts";
74
75
 
75
76
  /** Retrieve-and-answer attempts, i.e. one query rewrite. */
76
77
  export const MAX_ATTEMPTS = 2;
@@ -217,7 +218,7 @@ const machine = setup({
217
218
  }),
218
219
  states: {
219
220
  /**
220
- * Retrieval is LEXICAL and synchronous (`shared.ts`), so it is an entry
221
+ * Retrieval is LEXICAL and synchronous (`knowledge.ts`), so it is an entry
221
222
  * action rather than an invoked actor — there is nothing to await.
222
223
  */
223
224
  retrieve: {