@alexkroman1/aai-cli 0.12.3 → 0.14.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 (591) hide show
  1. package/README.md +69 -0
  2. package/bin.mjs +58 -0
  3. package/dist/_agent-BopNO5Bq.mjs +152 -0
  4. package/dist/_agent.d.ts +83 -0
  5. package/dist/_api-client.d.ts +92 -0
  6. package/dist/_artifacts-BJOYGQPp.mjs +21 -0
  7. package/dist/_artifacts.d.ts +16 -0
  8. package/dist/_build-target-C08aM-Es.mjs +1119 -0
  9. package/dist/_build-target.d.ts +276 -0
  10. package/dist/_bundler-B6v31ROV.mjs +102 -0
  11. package/dist/_bundler.d.ts +60 -0
  12. package/dist/_cli-common.d.ts +173 -0
  13. package/dist/_client-plugins.d.ts +57 -0
  14. package/dist/_config-mq1I5Cva.mjs +263 -0
  15. package/dist/_config.d.ts +122 -0
  16. package/dist/_console-audio.d.ts +35 -0
  17. package/dist/_console-session.d.ts +70 -0
  18. package/dist/_default-html.d.ts +33 -0
  19. package/dist/_deno-output.d.ts +23 -0
  20. package/dist/_deno-target.d.ts +74 -0
  21. package/dist/_deploy.d.ts +22 -0
  22. package/dist/_dev-agent-env.d.ts +27 -0
  23. package/dist/_dev-env-D9Uy5wNt.mjs +89 -0
  24. package/dist/_dev-env.d.ts +77 -0
  25. package/dist/_dev-restart.d.ts +64 -0
  26. package/dist/_dev-server-GoEXEVpA.mjs +737 -0
  27. package/dist/_dev-server.d.ts +89 -0
  28. package/dist/_dev-session-ticket.d.ts +63 -0
  29. package/dist/_dev-source.d.ts +55 -0
  30. package/dist/_dev-tunnel.d.ts +90 -0
  31. package/dist/_dev-typecheck.d.ts +38 -0
  32. package/dist/_dev-vite-config.d.ts +100 -0
  33. package/dist/_dev-watch.d.ts +35 -0
  34. package/dist/_dotenv-file.d.ts +45 -0
  35. package/dist/_e2e-test-utils.d.ts +120 -0
  36. package/dist/_fault-mode.d.ts +194 -0
  37. package/dist/_help.d.ts +31 -0
  38. package/dist/_init.d.ts +66 -0
  39. package/dist/_mock-api.d.ts +22 -0
  40. package/dist/_mock-registry.d.ts +17 -0
  41. package/dist/_modal-app.d.ts +77 -0
  42. package/dist/_modal-output.d.ts +31 -0
  43. package/dist/_modal-target.d.ts +69 -0
  44. package/dist/_output-DBP9Op_d.mjs +120 -0
  45. package/dist/_output.d.ts +74 -0
  46. package/dist/_preflight-Bv6Ct4ss.mjs +149 -0
  47. package/dist/_preflight.d.ts +170 -0
  48. package/dist/_resource-commands.d.ts +54 -0
  49. package/dist/_run-code-deno-C0AtfHeH.mjs +268 -0
  50. package/dist/_run-code-deno.d.ts +130 -0
  51. package/dist/_server-common-CVXIeWrY.mjs +114 -0
  52. package/dist/_server-common.d.ts +75 -0
  53. package/dist/_slug-api-BnmTHtjd.mjs +68 -0
  54. package/dist/_slug-api.d.ts +61 -0
  55. package/dist/_stop-server.d.ts +17 -0
  56. package/dist/_studio-B5m4jBTD.mjs +256 -0
  57. package/dist/_studio-commands.d.ts +73 -0
  58. package/dist/_studio.d.ts +92 -0
  59. package/dist/_target-bundle.d.ts +141 -0
  60. package/dist/_target-drain.d.ts +44 -0
  61. package/dist/_target-entry.d.ts +81 -0
  62. package/dist/_target-output.d.ts +63 -0
  63. package/dist/_templates-BVMVsQh0.mjs +139 -0
  64. package/dist/_templates.d.ts +89 -0
  65. package/dist/_typecheck-gate-CcX2gLJZ.mjs +19 -0
  66. package/dist/_typecheck-gate.d.ts +11 -0
  67. package/dist/_ui-Coe0zwmm.mjs +102 -0
  68. package/dist/_ui.d.ts +90 -0
  69. package/dist/_utils-BVLdBvdT.mjs +186 -0
  70. package/dist/_utils.d.ts +126 -0
  71. package/dist/_vercel-output.d.ts +48 -0
  72. package/dist/_vercel-target.d.ts +169 -0
  73. package/dist/_vite-env-BiyJ0GQH.mjs +105 -0
  74. package/dist/_vite-env.d.ts +38 -0
  75. package/dist/_vitest-runner-CKI8_K1b.mjs +207 -0
  76. package/dist/_vitest-runner.d.ts +182 -0
  77. package/dist/_workflow-determinism-CLxFQ5ox.mjs +198 -0
  78. package/dist/_workflow-determinism.d.ts +118 -0
  79. package/dist/build-CKhanHVv.mjs +588 -0
  80. package/dist/build.d.ts +146 -0
  81. package/dist/cli-workflow.d.ts +17 -0
  82. package/dist/cli.d.ts +1 -0
  83. package/dist/cli.mjs +1092 -195
  84. package/dist/client-bundler-DZ_Zoznn.mjs +241 -0
  85. package/dist/client-bundler.d.ts +38 -0
  86. package/dist/client-bundler.mjs +3 -0
  87. package/dist/console-Dxq26Adz.mjs +447 -0
  88. package/dist/console.d.ts +70 -0
  89. package/dist/delete-DwXCHzTl.mjs +55 -0
  90. package/dist/delete.d.ts +26 -0
  91. package/dist/deploy-CbA8Ibiu.mjs +97 -0
  92. package/dist/deploy.d.ts +25 -0
  93. package/dist/dev-BWBDbUEb.mjs +274 -0
  94. package/dist/dev.d.ts +34 -0
  95. package/dist/eval-Dl8o91yC.mjs +74 -0
  96. package/dist/eval.d.ts +46 -0
  97. package/dist/init-CidslZZz.mjs +609 -0
  98. package/dist/init.d.ts +101 -0
  99. package/dist/login-oJdBWu2B.mjs +166 -0
  100. package/dist/login.d.ts +54 -0
  101. package/dist/logs-CnPK-Prw.mjs +102 -0
  102. package/dist/logs.d.ts +65 -0
  103. package/dist/project-config.d.ts +23 -0
  104. package/dist/project-config.mjs +3 -0
  105. package/dist/scaffold/.env.example +51 -0
  106. package/dist/scaffold/CLAUDE.md +390 -0
  107. package/dist/scaffold/agent-guide/AGENT-API.md +223 -0
  108. package/dist/scaffold/agent-guide/HOSTING.md +139 -0
  109. package/dist/scaffold/agent-guide/PIPELINE-TUNING.md +232 -0
  110. package/dist/scaffold/agent-guide/PROVIDERS.md +140 -0
  111. package/dist/scaffold/agent-guide/TESTING-EVALS.md +158 -0
  112. package/dist/scaffold/agent-guide/TOOLS.md +627 -0
  113. package/dist/scaffold/agent-guide/UI.md +292 -0
  114. package/dist/scaffold/agent-guide/WORKFLOWS.md +803 -0
  115. package/dist/scaffold/package.json +38 -0
  116. package/dist/scaffold/pnpm-workspace.yaml +45 -0
  117. package/dist/scaffold/tsconfig.json +3 -0
  118. package/dist/scaffold/vitest.config.ts +8 -0
  119. package/dist/secret-DPdPEet2.mjs +300 -0
  120. package/dist/secret.d.ts +70 -0
  121. package/dist/start.d.ts +112 -0
  122. package/dist/start.mjs +183 -0
  123. package/dist/studio-COjK25Rl.mjs +208 -0
  124. package/dist/studio.d.ts +56 -0
  125. package/dist/templates/applicant-screening-agent/agent.eval.test.ts +258 -0
  126. package/dist/templates/applicant-screening-agent/agent.test.ts +987 -0
  127. package/dist/templates/applicant-screening-agent/agent.ts +66 -0
  128. package/dist/templates/applicant-screening-agent/client.tsx +154 -0
  129. package/dist/templates/applicant-screening-agent/crews.ts +432 -0
  130. package/dist/templates/applicant-screening-agent/leads.json +86 -0
  131. package/dist/templates/applicant-screening-agent/screening-lock.test.ts +77 -0
  132. package/dist/templates/applicant-screening-agent/screening-lock.ts +51 -0
  133. package/dist/templates/applicant-screening-agent/shared.ts +483 -0
  134. package/dist/templates/applicant-screening-agent/system-prompt.md +21 -0
  135. package/dist/templates/applicant-screening-agent/tools/candidate_details.ts +49 -0
  136. package/dist/templates/applicant-screening-agent/tools/proceed_to_emails.ts +127 -0
  137. package/dist/templates/applicant-screening-agent/tools/read_email.ts +50 -0
  138. package/dist/templates/applicant-screening-agent/tools/rescore_with_feedback.ts +140 -0
  139. package/dist/templates/applicant-screening-agent/tools/screen_candidates.ts +128 -0
  140. package/dist/templates/applicant-screening-agent/tools/screening_status.ts +51 -0
  141. package/dist/templates/call-audit-workflow/agent.eval.test.ts +492 -0
  142. package/dist/templates/call-audit-workflow/agent.test.ts +1032 -0
  143. package/dist/templates/call-audit-workflow/agent.ts +161 -0
  144. package/dist/templates/call-audit-workflow/client.tsx +236 -0
  145. package/dist/templates/call-audit-workflow/guards.test.ts +233 -0
  146. package/dist/templates/call-audit-workflow/workflows/audit.ts +391 -0
  147. package/dist/templates/call-audit-workflow/workflows/ingest.ts +279 -0
  148. package/dist/templates/call-audit-workflow/workflows/media.ts +729 -0
  149. package/dist/templates/call-audit-workflow/workflows/summarize.ts +201 -0
  150. package/dist/templates/call-audit-workflow/workflows/sync-api.ts +54 -0
  151. package/dist/templates/code-interpreter-agent/agent.eval.test.ts +195 -0
  152. package/dist/templates/code-interpreter-agent/agent.test.ts +91 -0
  153. package/dist/templates/code-interpreter-agent/agent.ts +9 -0
  154. package/dist/templates/code-interpreter-agent/system-prompt.md +18 -0
  155. package/dist/templates/coding-agent/agent.eval.test.ts +135 -0
  156. package/dist/templates/coding-agent/agent.test.ts +109 -0
  157. package/dist/templates/coding-agent/agent.ts +45 -0
  158. package/dist/templates/coding-agent/chat.ts +63 -0
  159. package/dist/templates/coding-agent/shared.ts +46 -0
  160. package/dist/templates/coding-agent/system-prompt.md +32 -0
  161. package/dist/templates/coding-agent/tools/bash.ts +6 -0
  162. package/dist/templates/coding-agent/tools/delete_file.ts +6 -0
  163. package/dist/templates/coding-agent/tools/edit_file.ts +6 -0
  164. package/dist/templates/coding-agent/tools/glob.ts +6 -0
  165. package/dist/templates/coding-agent/tools/grep.ts +6 -0
  166. package/dist/templates/coding-agent/tools/list_files.ts +6 -0
  167. package/dist/templates/coding-agent/tools/read_file.ts +6 -0
  168. package/dist/templates/coding-agent/tools/todo_write.ts +6 -0
  169. package/dist/templates/coding-agent/tools/write_file.ts +6 -0
  170. package/dist/templates/custom-pipeline-agent/agent.eval.test.ts +79 -0
  171. package/dist/templates/custom-pipeline-agent/agent.test.ts +79 -0
  172. package/dist/templates/custom-pipeline-agent/agent.ts +13 -0
  173. package/dist/templates/custom-pipeline-agent/modes.test.ts +162 -0
  174. package/dist/templates/custom-pipeline-agent/stages.test.ts +330 -0
  175. package/dist/templates/document-redline-workflow/agent.eval.test.ts +251 -0
  176. package/dist/templates/document-redline-workflow/agent.test.ts +552 -0
  177. package/dist/templates/document-redline-workflow/agent.ts +112 -0
  178. package/dist/templates/document-redline-workflow/client.tsx +224 -0
  179. package/dist/templates/document-redline-workflow/form.ts +74 -0
  180. package/dist/templates/document-redline-workflow/workflows/prompts.ts +68 -0
  181. package/dist/templates/document-redline-workflow/workflows/redline.ts +317 -0
  182. package/dist/templates/emergency-dispatch-agent/agent.eval.test.ts +372 -0
  183. package/dist/templates/emergency-dispatch-agent/agent.test.ts +576 -0
  184. package/dist/templates/emergency-dispatch-agent/agent.ts +63 -0
  185. package/dist/templates/emergency-dispatch-agent/client.tsx +453 -0
  186. package/dist/templates/emergency-dispatch-agent/events.ts +48 -0
  187. package/dist/templates/emergency-dispatch-agent/guardrails.ts +105 -0
  188. package/dist/templates/emergency-dispatch-agent/shared.ts +768 -0
  189. package/dist/templates/emergency-dispatch-agent/system-prompt.md +42 -0
  190. package/dist/templates/emergency-dispatch-agent/tools/incident_add_note.ts +28 -0
  191. package/dist/templates/emergency-dispatch-agent/tools/incident_create.ts +95 -0
  192. package/dist/templates/emergency-dispatch-agent/tools/incident_escalate.ts +96 -0
  193. package/dist/templates/emergency-dispatch-agent/tools/incident_get.ts +40 -0
  194. package/dist/templates/emergency-dispatch-agent/tools/incident_triage.ts +90 -0
  195. package/dist/templates/emergency-dispatch-agent/tools/incident_update_status.ts +75 -0
  196. package/dist/templates/emergency-dispatch-agent/tools/ops_dashboard.ts +72 -0
  197. package/dist/templates/emergency-dispatch-agent/tools/ops_protocols.ts +27 -0
  198. package/dist/templates/emergency-dispatch-agent/tools/ops_run_scenario.ts +154 -0
  199. package/dist/templates/emergency-dispatch-agent/tools/resources_dispatch.ts +112 -0
  200. package/dist/templates/emergency-dispatch-agent/tools/resources_get_available.ts +34 -0
  201. package/dist/templates/emergency-dispatch-agent/tools/resources_update_status.ts +63 -0
  202. package/dist/templates/entertainment-picks-agent/agent.eval.test.ts +281 -0
  203. package/dist/templates/entertainment-picks-agent/agent.test.ts +260 -0
  204. package/dist/templates/entertainment-picks-agent/agent.ts +14 -0
  205. package/dist/templates/entertainment-picks-agent/client.tsx +169 -0
  206. package/dist/templates/entertainment-picks-agent/shared.ts +50 -0
  207. package/dist/templates/entertainment-picks-agent/system-prompt.md +19 -0
  208. package/dist/templates/entertainment-picks-agent/tools/recommend.ts +79 -0
  209. package/dist/templates/entertainment-picks-agent/tools/revisit.ts +56 -0
  210. package/dist/templates/executive-inbox-agent/agent.eval.test.ts +296 -0
  211. package/dist/templates/executive-inbox-agent/agent.test.ts +682 -0
  212. package/dist/templates/executive-inbox-agent/agent.ts +51 -0
  213. package/dist/templates/executive-inbox-agent/calendar.test.ts +203 -0
  214. package/dist/templates/executive-inbox-agent/client.tsx +195 -0
  215. package/dist/templates/executive-inbox-agent/inbox.ts +233 -0
  216. package/dist/templates/executive-inbox-agent/meeting.ts +138 -0
  217. package/dist/templates/executive-inbox-agent/nodes.ts +148 -0
  218. package/dist/templates/executive-inbox-agent/prompts.ts +333 -0
  219. package/dist/templates/executive-inbox-agent/review.ts +425 -0
  220. package/dist/templates/executive-inbox-agent/shared.ts +486 -0
  221. package/dist/templates/executive-inbox-agent/system-prompt.md +15 -0
  222. package/dist/templates/executive-inbox-agent/tools/accept.ts +10 -0
  223. package/dist/templates/executive-inbox-agent/tools/ask_question.ts +30 -0
  224. package/dist/templates/executive-inbox-agent/tools/draft_reply.ts +67 -0
  225. package/dist/templates/executive-inbox-agent/tools/edit.ts +10 -0
  226. package/dist/templates/executive-inbox-agent/tools/ignore.ts +10 -0
  227. package/dist/templates/executive-inbox-agent/tools/inbox_status.ts +27 -0
  228. package/dist/templates/executive-inbox-agent/tools/meeting_assistant.ts +52 -0
  229. package/dist/templates/executive-inbox-agent/tools/new_email.ts +50 -0
  230. package/dist/templates/executive-inbox-agent/tools/open_email.ts +157 -0
  231. package/dist/templates/executive-inbox-agent/tools/respond.ts +10 -0
  232. package/dist/templates/executive-inbox-agent/tools/review_memory.ts +22 -0
  233. package/dist/templates/executive-inbox-agent/tools/send_calendar_invite.ts +68 -0
  234. package/dist/templates/executive-inbox-agent/tools/triage_inbox.ts +75 -0
  235. package/dist/templates/front-desk-agent/agent.eval.test.ts +177 -0
  236. package/dist/templates/front-desk-agent/agent.test.ts +155 -0
  237. package/dist/templates/front-desk-agent/agent.ts +22 -0
  238. package/dist/templates/front-desk-agent/shared.ts +239 -0
  239. package/dist/templates/front-desk-agent/system-prompt.md +13 -0
  240. package/dist/templates/front-desk-agent/tools/verify_account.ts +41 -0
  241. package/dist/templates/home-assistant-agent/agent.eval.test.ts +176 -0
  242. package/dist/templates/home-assistant-agent/agent.test.ts +512 -0
  243. package/dist/templates/home-assistant-agent/agent.ts +37 -0
  244. package/dist/templates/home-assistant-agent/client.tsx +176 -0
  245. package/dist/templates/home-assistant-agent/ring.tsx +55 -0
  246. package/dist/templates/home-assistant-agent/shared.ts +130 -0
  247. package/dist/templates/home-assistant-agent/system-prompt.md +43 -0
  248. package/dist/templates/home-assistant-agent/tools/cancel_reminders.ts +16 -0
  249. package/dist/templates/home-assistant-agent/tools/deep_research.ts +55 -0
  250. package/dist/templates/home-assistant-agent/tools/remind_me.ts +53 -0
  251. package/dist/templates/home-assistant-agent/tools/stop.ts +25 -0
  252. package/dist/templates/home-assistant-agent/use-speaker.ts +98 -0
  253. package/dist/templates/home-assistant-agent/workflows/remind.ts +25 -0
  254. package/dist/templates/home-assistant-agent/workflows/research-prompts.ts +41 -0
  255. package/dist/templates/home-assistant-agent/workflows/research.ts +110 -0
  256. package/dist/templates/home-assistant-agent/workflows/text.ts +30 -0
  257. package/dist/templates/hotel-reception-agent/agent.eval.test.ts +241 -0
  258. package/dist/templates/hotel-reception-agent/agent.test.ts +1121 -0
  259. package/dist/templates/hotel-reception-agent/agent.ts +66 -0
  260. package/dist/templates/hotel-reception-agent/booking.ts +159 -0
  261. package/dist/templates/hotel-reception-agent/card.ts +92 -0
  262. package/dist/templates/hotel-reception-agent/catalogs.ts +219 -0
  263. package/dist/templates/hotel-reception-agent/client.tsx +150 -0
  264. package/dist/templates/hotel-reception-agent/concierge.ts +104 -0
  265. package/dist/templates/hotel-reception-agent/desk-seams.test.ts +339 -0
  266. package/dist/templates/hotel-reception-agent/desk.ts +220 -0
  267. package/dist/templates/hotel-reception-agent/disputes.ts +214 -0
  268. package/dist/templates/hotel-reception-agent/events.ts +80 -0
  269. package/dist/templates/hotel-reception-agent/hotel.ts +412 -0
  270. package/dist/templates/hotel-reception-agent/policies.ts +209 -0
  271. package/dist/templates/hotel-reception-agent/records.ts +384 -0
  272. package/dist/templates/hotel-reception-agent/restaurant.ts +147 -0
  273. package/dist/templates/hotel-reception-agent/seed.ts +955 -0
  274. package/dist/templates/hotel-reception-agent/session.ts +50 -0
  275. package/dist/templates/hotel-reception-agent/shared.ts +353 -0
  276. package/dist/templates/hotel-reception-agent/system-prompt.md +76 -0
  277. package/dist/templates/hotel-reception-agent/tools/abandon_booking.ts +31 -0
  278. package/dist/templates/hotel-reception-agent/tools/add_to_waitlist.ts +42 -0
  279. package/dist/templates/hotel-reception-agent/tools/book_airport_car.ts +58 -0
  280. package/dist/templates/hotel-reception-agent/tools/book_business_center.ts +44 -0
  281. package/dist/templates/hotel-reception-agent/tools/book_spa_appointment.ts +39 -0
  282. package/dist/templates/hotel-reception-agent/tools/book_tour.ts +35 -0
  283. package/dist/templates/hotel-reception-agent/tools/cancel_restaurant_reservation.ts +26 -0
  284. package/dist/templates/hotel-reception-agent/tools/cancel_room_booking.ts +64 -0
  285. package/dist/templates/hotel-reception-agent/tools/check_restaurant_availability.ts +24 -0
  286. package/dist/templates/hotel-reception-agent/tools/check_room_availability.ts +40 -0
  287. package/dist/templates/hotel-reception-agent/tools/choose_room.ts +93 -0
  288. package/dist/templates/hotel-reception-agent/tools/confirm_booking.ts +154 -0
  289. package/dist/templates/hotel-reception-agent/tools/dispatch_emergency.ts +62 -0
  290. package/dist/templates/hotel-reception-agent/tools/dispute_charge.ts +91 -0
  291. package/dist/templates/hotel-reception-agent/tools/flag_late_arrival.ts +27 -0
  292. package/dist/templates/hotel-reception-agent/tools/lookup_booking.ts +49 -0
  293. package/dist/templates/hotel-reception-agent/tools/lookup_guest_history.ts +28 -0
  294. package/dist/templates/hotel-reception-agent/tools/lookup_invoice.ts +27 -0
  295. package/dist/templates/hotel-reception-agent/tools/lookup_policy.ts +20 -0
  296. package/dist/templates/hotel-reception-agent/tools/lookup_restaurant_reservation.ts +27 -0
  297. package/dist/templates/hotel-reception-agent/tools/modify_restaurant_reservation.ts +75 -0
  298. package/dist/templates/hotel-reception-agent/tools/order_flowers.ts +40 -0
  299. package/dist/templates/hotel-reception-agent/tools/record_card.ts +46 -0
  300. package/dist/templates/hotel-reception-agent/tools/record_followup.ts +51 -0
  301. package/dist/templates/hotel-reception-agent/tools/record_group_inquiry.ts +45 -0
  302. package/dist/templates/hotel-reception-agent/tools/record_guest_details.ts +71 -0
  303. package/dist/templates/hotel-reception-agent/tools/reinstate_booking.ts +65 -0
  304. package/dist/templates/hotel-reception-agent/tools/request_flight_reconfirmation.ts +55 -0
  305. package/dist/templates/hotel-reception-agent/tools/resend_confirmation.ts +40 -0
  306. package/dist/templates/hotel-reception-agent/tools/reserve_table.ts +87 -0
  307. package/dist/templates/hotel-reception-agent/tools/resolve_room_conflict.ts +49 -0
  308. package/dist/templates/hotel-reception-agent/tools/schedule_wakeup_call.ts +49 -0
  309. package/dist/templates/hotel-reception-agent/tools/set_do_not_disturb.ts +29 -0
  310. package/dist/templates/hotel-reception-agent/tools/set_extras.ts +51 -0
  311. package/dist/templates/hotel-reception-agent/tools/set_stay.ts +98 -0
  312. package/dist/templates/hotel-reception-agent/tools/start_booking_modification.ts +58 -0
  313. package/dist/templates/hotel-reception-agent/tools/start_room_booking.ts +50 -0
  314. package/dist/templates/hotel-reception-agent/tools/take_guest_message.ts +72 -0
  315. package/dist/templates/hotel-reception-agent/tools/transfer_call.ts +48 -0
  316. package/dist/templates/hotel-reception-agent/tools/update_card.ts +46 -0
  317. package/dist/templates/hotel-reception-agent/tools/verify_booking.ts +96 -0
  318. package/dist/templates/link-digest-workflow/agent.eval.test.ts +231 -0
  319. package/dist/templates/link-digest-workflow/agent.test.ts +646 -0
  320. package/dist/templates/link-digest-workflow/agent.ts +100 -0
  321. package/dist/templates/link-digest-workflow/api-client.ts +144 -0
  322. package/dist/templates/link-digest-workflow/client.tsx +231 -0
  323. package/dist/templates/link-digest-workflow/workflows/digest.ts +334 -0
  324. package/dist/templates/medication-safety-agent/agent.eval.test.ts +182 -0
  325. package/dist/templates/medication-safety-agent/agent.test.ts +329 -0
  326. package/dist/templates/medication-safety-agent/agent.ts +62 -0
  327. package/dist/templates/medication-safety-agent/fda.test.ts +129 -0
  328. package/dist/templates/medication-safety-agent/fda.ts +166 -0
  329. package/dist/templates/medication-safety-agent/system-prompt.md +26 -0
  330. package/dist/templates/medication-safety-agent/tools/check_drug_interaction.ts +70 -0
  331. package/dist/templates/medication-safety-agent/tools/medication_lookup.ts +46 -0
  332. package/dist/templates/meeting-recap-agent/agent.eval.test.ts +459 -0
  333. package/dist/templates/meeting-recap-agent/agent.test.ts +1408 -0
  334. package/dist/templates/meeting-recap-agent/agent.ts +104 -0
  335. package/dist/templates/meeting-recap-agent/shared.ts +44 -0
  336. package/dist/templates/meeting-recap-agent/tools/cancel_recap.ts +23 -0
  337. package/dist/templates/meeting-recap-agent/tools/keep_transcript.ts +31 -0
  338. package/dist/templates/meeting-recap-agent/tools/recap_progress.ts +24 -0
  339. package/dist/templates/meeting-recap-agent/tools/recap_status.ts +45 -0
  340. package/dist/templates/meeting-recap-agent/tools/request_recap.ts +44 -0
  341. package/dist/templates/meeting-recap-agent/workflows/recap.ts +838 -0
  342. package/dist/templates/meeting-recap-agent/workflows/tokens.ts +47 -0
  343. package/dist/templates/pizza-ordering-agent/agent.eval.test.ts +228 -0
  344. package/dist/templates/pizza-ordering-agent/agent.test.ts +299 -0
  345. package/dist/templates/pizza-ordering-agent/agent.ts +24 -0
  346. package/dist/templates/pizza-ordering-agent/client.tsx +104 -0
  347. package/dist/templates/pizza-ordering-agent/shared.ts +225 -0
  348. package/dist/templates/pizza-ordering-agent/system-prompt.md +23 -0
  349. package/dist/templates/pizza-ordering-agent/tools/add_pizza.ts +27 -0
  350. package/dist/templates/pizza-ordering-agent/tools/place_order.ts +26 -0
  351. package/dist/templates/pizza-ordering-agent/tools/remove_pizza.ts +18 -0
  352. package/dist/templates/pizza-ordering-agent/tools/set_customer_name.ts +13 -0
  353. package/dist/templates/pizza-ordering-agent/tools/update_pizza.ts +28 -0
  354. package/dist/templates/pizza-ordering-agent/tools/view_order.ts +21 -0
  355. package/dist/templates/podcast-digest-workflow/agent.eval.test.ts +421 -0
  356. package/dist/templates/podcast-digest-workflow/agent.test.ts +1225 -0
  357. package/dist/templates/podcast-digest-workflow/agent.ts +143 -0
  358. package/dist/templates/podcast-digest-workflow/client.tsx +259 -0
  359. package/dist/templates/podcast-digest-workflow/workflows/digest.ts +499 -0
  360. package/dist/templates/podcast-digest-workflow/workflows/feeds.ts +575 -0
  361. package/dist/templates/podcast-digest-workflow/workflows/slack.ts +153 -0
  362. package/dist/templates/push-to-talk-agent/agent.eval.test.ts +78 -0
  363. package/dist/templates/push-to-talk-agent/agent.test.ts +63 -0
  364. package/dist/templates/push-to-talk-agent/agent.ts +22 -0
  365. package/dist/templates/push-to-talk-agent/client.tsx +124 -0
  366. package/dist/templates/push-to-talk-agent/shared.ts +28 -0
  367. package/dist/templates/push-to-talk-agent/system-prompt.md +11 -0
  368. package/dist/templates/push-to-talk-agent/tools/list_notes.ts +15 -0
  369. package/dist/templates/push-to-talk-agent/tools/save_note.ts +26 -0
  370. package/dist/templates/quickstart-agent/agent.eval.test.ts +148 -0
  371. package/dist/templates/quickstart-agent/agent.test.ts +96 -0
  372. package/dist/templates/quickstart-agent/agent.ts +25 -0
  373. package/dist/templates/quickstart-agent/system-prompt.md +8 -0
  374. package/dist/templates/quickstart-agent/tools/get_weather.ts +59 -0
  375. package/dist/templates/research-handoff-agent/agent.eval.test.ts +348 -0
  376. package/dist/templates/research-handoff-agent/agent.test.ts +818 -0
  377. package/dist/templates/research-handoff-agent/agent.ts +118 -0
  378. package/dist/templates/research-handoff-agent/shared.ts +41 -0
  379. package/dist/templates/research-handoff-agent/tools/file_it_now.ts +32 -0
  380. package/dist/templates/research-handoff-agent/tools/request_research.ts +33 -0
  381. package/dist/templates/research-handoff-agent/tools/research_progress.ts +23 -0
  382. package/dist/templates/research-handoff-agent/tools/research_status.ts +73 -0
  383. package/dist/templates/research-handoff-agent/workflows/filing.ts +164 -0
  384. package/dist/templates/research-handoff-agent/workflows/prompts.ts +46 -0
  385. package/dist/templates/research-handoff-agent/workflows/research.ts +100 -0
  386. package/dist/templates/research-handoff-agent/workflows/review.ts +43 -0
  387. package/dist/templates/research-planner-agent/agent.eval.test.ts +241 -0
  388. package/dist/templates/research-planner-agent/agent.test.ts +640 -0
  389. package/dist/templates/research-planner-agent/agent.ts +26 -0
  390. package/dist/templates/research-planner-agent/client.tsx +120 -0
  391. package/dist/templates/research-planner-agent/procedure.ts +287 -0
  392. package/dist/templates/research-planner-agent/prompts.ts +153 -0
  393. package/dist/templates/research-planner-agent/shared.ts +356 -0
  394. package/dist/templates/research-planner-agent/system-prompt.md +18 -0
  395. package/dist/templates/research-planner-agent/tools/plan_status.ts +41 -0
  396. package/dist/templates/research-planner-agent/tools/revise_plan.ts +71 -0
  397. package/dist/templates/research-planner-agent/tools/start_plan.ts +62 -0
  398. package/dist/templates/research-planner-agent/tools/work_next_step.ts +177 -0
  399. package/dist/templates/retail-orders-agent/address.ts +109 -0
  400. package/dist/templates/retail-orders-agent/agent.eval.test.ts +305 -0
  401. package/dist/templates/retail-orders-agent/agent.test.ts +1260 -0
  402. package/dist/templates/retail-orders-agent/agent.ts +103 -0
  403. package/dist/templates/retail-orders-agent/authenticate.ts +34 -0
  404. package/dist/templates/retail-orders-agent/cancel.ts +114 -0
  405. package/dist/templates/retail-orders-agent/client.tsx +579 -0
  406. package/dist/templates/retail-orders-agent/keyterms.ts +154 -0
  407. package/dist/templates/retail-orders-agent/payment.ts +103 -0
  408. package/dist/templates/retail-orders-agent/pending.test.ts +204 -0
  409. package/dist/templates/retail-orders-agent/pending.ts +137 -0
  410. package/dist/templates/retail-orders-agent/refund.ts +19 -0
  411. package/dist/templates/retail-orders-agent/registry.test.ts +446 -0
  412. package/dist/templates/retail-orders-agent/resolve.test.ts +100 -0
  413. package/dist/templates/retail-orders-agent/resolve.ts +114 -0
  414. package/dist/templates/retail-orders-agent/returns.ts +101 -0
  415. package/dist/templates/retail-orders-agent/seed.json +4260 -0
  416. package/dist/templates/retail-orders-agent/seed.test.ts +233 -0
  417. package/dist/templates/retail-orders-agent/shared.test.ts +240 -0
  418. package/dist/templates/retail-orders-agent/shared.ts +547 -0
  419. package/dist/templates/retail-orders-agent/store.test.ts +331 -0
  420. package/dist/templates/retail-orders-agent/store.ts +485 -0
  421. package/dist/templates/retail-orders-agent/swap.test.ts +189 -0
  422. package/dist/templates/retail-orders-agent/swap.ts +358 -0
  423. package/dist/templates/retail-orders-agent/system-prompt.md +152 -0
  424. package/dist/templates/retail-orders-agent/tools/cancel_change.ts +33 -0
  425. package/dist/templates/retail-orders-agent/tools/cancel_pending_order.ts +31 -0
  426. package/dist/templates/retail-orders-agent/tools/confirm_change.ts +42 -0
  427. package/dist/templates/retail-orders-agent/tools/exchange_delivered_order_items.ts +47 -0
  428. package/dist/templates/retail-orders-agent/tools/find_user_id_by_email.ts +31 -0
  429. package/dist/templates/retail-orders-agent/tools/find_user_id_by_name_zip.ts +36 -0
  430. package/dist/templates/retail-orders-agent/tools/get_item_details.ts +27 -0
  431. package/dist/templates/retail-orders-agent/tools/get_order_details.ts +37 -0
  432. package/dist/templates/retail-orders-agent/tools/get_product_details.ts +30 -0
  433. package/dist/templates/retail-orders-agent/tools/get_user_details.ts +37 -0
  434. package/dist/templates/retail-orders-agent/tools/list_all_product_types.ts +22 -0
  435. package/dist/templates/retail-orders-agent/tools/modify_pending_order_address.ts +27 -0
  436. package/dist/templates/retail-orders-agent/tools/modify_pending_order_items.ts +48 -0
  437. package/dist/templates/retail-orders-agent/tools/modify_pending_order_payment.ts +32 -0
  438. package/dist/templates/retail-orders-agent/tools/modify_user_address.ts +26 -0
  439. package/dist/templates/retail-orders-agent/tools/return_delivered_order_items.ts +37 -0
  440. package/dist/templates/retail-orders-agent/tools/transfer_to_human_agents.ts +28 -0
  441. package/dist/templates/roadside-assistance-agent/agent.eval.test.ts +283 -0
  442. package/dist/templates/roadside-assistance-agent/agent.test.ts +650 -0
  443. package/dist/templates/roadside-assistance-agent/agent.ts +89 -0
  444. package/dist/templates/roadside-assistance-agent/call.ts +232 -0
  445. package/dist/templates/roadside-assistance-agent/events.ts +92 -0
  446. package/dist/templates/roadside-assistance-agent/shared.ts +346 -0
  447. package/dist/templates/roadside-assistance-agent/system-prompt.md +37 -0
  448. package/dist/templates/roadside-assistance-agent/tools/acknowledge_disclosure.ts +61 -0
  449. package/dist/templates/roadside-assistance-agent/tools/dispatch_truck.ts +149 -0
  450. package/dist/templates/roadside-assistance-agent/tools/job_status.ts +38 -0
  451. package/dist/templates/roadside-assistance-agent/tools/lookup_coverage.ts +83 -0
  452. package/dist/templates/roadside-assistance-agent/tools/report_location.ts +79 -0
  453. package/dist/templates/roadside-assistance-agent/tools/service_disclosure.ts +51 -0
  454. package/dist/templates/roadside-assistance-agent/yard.test.ts +162 -0
  455. package/dist/templates/roadside-assistance-agent/yard.ts +183 -0
  456. package/dist/templates/spoken-summary-workflow/agent.eval.test.ts +354 -0
  457. package/dist/templates/spoken-summary-workflow/agent.test.ts +493 -0
  458. package/dist/templates/spoken-summary-workflow/agent.ts +164 -0
  459. package/dist/templates/spoken-summary-workflow/client.tsx +220 -0
  460. package/dist/templates/spoken-summary-workflow/workflows/summarize.ts +260 -0
  461. package/dist/templates/spoken-summary-workflow/workflows/transcribe.ts +137 -0
  462. package/dist/templates/tabletop-rpg-agent/agent.eval.test.ts +314 -0
  463. package/dist/templates/tabletop-rpg-agent/agent.test.ts +1000 -0
  464. package/dist/templates/tabletop-rpg-agent/agent.ts +58 -0
  465. package/dist/templates/tabletop-rpg-agent/client.tsx +814 -0
  466. package/dist/templates/tabletop-rpg-agent/shared.ts +1028 -0
  467. package/dist/templates/tabletop-rpg-agent/system-prompt.md +116 -0
  468. package/dist/templates/tabletop-rpg-agent/tools/action_roll.ts +119 -0
  469. package/dist/templates/tabletop-rpg-agent/tools/burn_momentum.ts +110 -0
  470. package/dist/templates/tabletop-rpg-agent/tools/check_state.ts +37 -0
  471. package/dist/templates/tabletop-rpg-agent/tools/oracle.ts +255 -0
  472. package/dist/templates/tabletop-rpg-agent/tools/setup_character.ts +228 -0
  473. package/dist/templates/tabletop-rpg-agent/tools/update_state.ts +194 -0
  474. package/dist/templates/technical-support-agent/agent.eval.test.ts +260 -0
  475. package/dist/templates/technical-support-agent/agent.test.ts +424 -0
  476. package/dist/templates/technical-support-agent/agent.ts +39 -0
  477. package/dist/templates/technical-support-agent/client.tsx +132 -0
  478. package/dist/templates/technical-support-agent/knowledge.json +65 -0
  479. package/dist/templates/technical-support-agent/knowledge.ts +165 -0
  480. package/dist/templates/technical-support-agent/nodes.ts +143 -0
  481. package/dist/templates/technical-support-agent/procedure.ts +453 -0
  482. package/dist/templates/technical-support-agent/prompts.ts +102 -0
  483. package/dist/templates/technical-support-agent/shared.ts +128 -0
  484. package/dist/templates/technical-support-agent/system-prompt.md +18 -0
  485. package/dist/templates/technical-support-agent/tools/answer_question.ts +134 -0
  486. package/dist/templates/technical-support-agent/tools/list_topics.ts +23 -0
  487. package/dist/templates/technical-support-agent/tools/log_ticket.ts +30 -0
  488. package/dist/templates/text-adventure-agent/agent.eval.test.ts +222 -0
  489. package/dist/templates/text-adventure-agent/agent.test.ts +429 -0
  490. package/dist/templates/text-adventure-agent/agent.ts +76 -0
  491. package/dist/templates/text-adventure-agent/client.tsx +359 -0
  492. package/dist/templates/text-adventure-agent/shared.ts +210 -0
  493. package/dist/templates/text-adventure-agent/system-prompt.md +50 -0
  494. package/dist/templates/text-adventure-agent/tools/game_state_drop.ts +29 -0
  495. package/dist/templates/text-adventure-agent/tools/game_state_flag.ts +13 -0
  496. package/dist/templates/text-adventure-agent/tools/game_state_get.ts +17 -0
  497. package/dist/templates/text-adventure-agent/tools/game_state_move.ts +15 -0
  498. package/dist/templates/text-adventure-agent/tools/game_state_restart.ts +13 -0
  499. package/dist/templates/text-adventure-agent/tools/game_state_score.ts +19 -0
  500. package/dist/templates/text-adventure-agent/tools/game_state_take.ts +13 -0
  501. package/dist/templates/topic-briefing-agent/agent.eval.test.ts +202 -0
  502. package/dist/templates/topic-briefing-agent/agent.test.ts +651 -0
  503. package/dist/templates/topic-briefing-agent/agent.ts +45 -0
  504. package/dist/templates/topic-briefing-agent/shared.ts +392 -0
  505. package/dist/templates/topic-briefing-agent/slack.ts +108 -0
  506. package/dist/templates/topic-briefing-agent/system-prompt.md +54 -0
  507. package/dist/templates/topic-briefing-agent/tools/briefing_so_far.ts +43 -0
  508. package/dist/templates/topic-briefing-agent/tools/research_topic.ts +80 -0
  509. package/dist/templates/topic-briefing-agent/tools/send_briefing.ts +115 -0
  510. package/dist/templates/topic-briefing-agent/tools/verify_claim.ts +123 -0
  511. package/dist/templates/transcription-workflow/agent.eval.test.ts +237 -0
  512. package/dist/templates/transcription-workflow/agent.test.ts +2033 -0
  513. package/dist/templates/transcription-workflow/agent.ts +188 -0
  514. package/dist/templates/transcription-workflow/client.tsx +511 -0
  515. package/dist/templates/transcription-workflow/recover.test.ts +219 -0
  516. package/dist/templates/transcription-workflow/recover.ts +154 -0
  517. package/dist/templates/transcription-workflow/run-panel.tsx +212 -0
  518. package/dist/templates/transcription-workflow/total-latency.tsx +170 -0
  519. package/dist/templates/transcription-workflow/workflows/batch.ts +189 -0
  520. package/dist/templates/transcription-workflow/workflows/downsample.ts +273 -0
  521. package/dist/templates/transcription-workflow/workflows/normalize.ts +385 -0
  522. package/dist/templates/transcription-workflow/workflows/stitch.ts +122 -0
  523. package/dist/templates/transcription-workflow/workflows/stream.ts +690 -0
  524. package/dist/templates/transcription-workflow/workflows/sync-api.ts +81 -0
  525. package/dist/templates/transcription-workflow/workflows/transcribe.ts +479 -0
  526. package/dist/templates/transcription-workflow/workflows/wav.ts +250 -0
  527. package/dist/templates/travel-concierge-agent/agent.eval.test.ts +300 -0
  528. package/dist/templates/travel-concierge-agent/agent.test.ts +601 -0
  529. package/dist/templates/travel-concierge-agent/agent.ts +62 -0
  530. package/dist/templates/travel-concierge-agent/call-events.ts +80 -0
  531. package/dist/templates/travel-concierge-agent/client.tsx +146 -0
  532. package/dist/templates/travel-concierge-agent/routing.ts +170 -0
  533. package/dist/templates/travel-concierge-agent/shared.ts +862 -0
  534. package/dist/templates/travel-concierge-agent/system-prompt.md +30 -0
  535. package/dist/templates/travel-concierge-agent/tools/book_car_rental.ts +21 -0
  536. package/dist/templates/travel-concierge-agent/tools/book_excursion.ts +19 -0
  537. package/dist/templates/travel-concierge-agent/tools/book_hotel.ts +21 -0
  538. package/dist/templates/travel-concierge-agent/tools/cancel_action.ts +9 -0
  539. package/dist/templates/travel-concierge-agent/tools/cancel_ticket.ts +21 -0
  540. package/dist/templates/travel-concierge-agent/tools/complete_or_escalate.ts +9 -0
  541. package/dist/templates/travel-concierge-agent/tools/confirm_action.ts +9 -0
  542. package/dist/templates/travel-concierge-agent/tools/lookup_booking.ts +36 -0
  543. package/dist/templates/travel-concierge-agent/tools/search_car_rentals.ts +36 -0
  544. package/dist/templates/travel-concierge-agent/tools/search_excursions.ts +40 -0
  545. package/dist/templates/travel-concierge-agent/tools/search_flights.ts +58 -0
  546. package/dist/templates/travel-concierge-agent/tools/search_hotels.ts +39 -0
  547. package/dist/templates/travel-concierge-agent/tools/to_car_rental_assistant.ts +11 -0
  548. package/dist/templates/travel-concierge-agent/tools/to_excursion_assistant.ts +11 -0
  549. package/dist/templates/travel-concierge-agent/tools/to_flight_assistant.ts +11 -0
  550. package/dist/templates/travel-concierge-agent/tools/to_hotel_assistant.ts +11 -0
  551. package/dist/templates/travel-concierge-agent/tools/update_ticket.ts +23 -0
  552. package/dist/templates/web-research-agent/agent.eval.test.ts +144 -0
  553. package/dist/templates/web-research-agent/agent.test.ts +182 -0
  554. package/dist/templates/web-research-agent/agent.ts +128 -0
  555. package/dist/templates/web-research-agent/system-prompt.md +33 -0
  556. package/dist/templates/word-game-agent/agent.eval.test.ts +222 -0
  557. package/dist/templates/word-game-agent/agent.test.ts +490 -0
  558. package/dist/templates/word-game-agent/agent.ts +55 -0
  559. package/dist/templates/word-game-agent/client.tsx +211 -0
  560. package/dist/templates/word-game-agent/game.ts +133 -0
  561. package/dist/templates/word-game-agent/guess.ts +63 -0
  562. package/dist/templates/word-game-agent/player.ts +67 -0
  563. package/dist/templates/word-game-agent/shared.ts +249 -0
  564. package/dist/templates/word-game-agent/system-prompt.md +15 -0
  565. package/dist/templates/word-game-agent/tools/final_score.ts +37 -0
  566. package/dist/templates/word-game-agent/tools/relay_description.ts +137 -0
  567. package/dist/templates/word-game-agent/tools/repeat_word.ts +19 -0
  568. package/dist/templates/word-game-agent/tools/skip_word.ts +31 -0
  569. package/dist/templates/word-game-agent/tools/start_game.ts +52 -0
  570. package/dist/templates/word-game-agent/words.ts +278 -0
  571. package/dist/test-XufelYGW.mjs +104 -0
  572. package/dist/test.d.ts +81 -0
  573. package/dist/typecheck.d.ts +44 -0
  574. package/dist/typecheck.mjs +185 -0
  575. package/dist/worker-bundler.d.ts +37 -0
  576. package/dist/worker-bundler.mjs +156 -0
  577. package/dist/workflow-CDUlTD5B.mjs +239 -0
  578. package/dist/workflow.d.ts +76 -0
  579. package/package.json +74 -28
  580. package/dist/_api-client-H4MFOr8j.mjs +0 -35
  581. package/dist/_bundler-DQtFVeww.mjs +0 -125
  582. package/dist/_discover-DzsMlg3G.mjs +0 -218
  583. package/dist/_init-CbMs9S2O.mjs +0 -56
  584. package/dist/_templates-BDkbj3TM.mjs +0 -72
  585. package/dist/_ui-DWGXImbO.mjs +0 -30
  586. package/dist/delete-CalIL6Pg.mjs +0 -34
  587. package/dist/deploy-CLs8gLHV.mjs +0 -53
  588. package/dist/dev-DEZKrw8v.mjs +0 -18
  589. package/dist/init-CNuntiij.mjs +0 -114
  590. package/dist/secret-xmLQ96Yz.mjs +0 -46
  591. package/dist/test-yqHGZPF3.mjs +0 -44
@@ -0,0 +1,862 @@
1
+ /**
2
+ * The concierge's booking world, its dialog stack, and its confirmation gate.
3
+ *
4
+ * **Adapted from LangGraph's customer-support tutorial** (MIT,
5
+ * <https://github.com/langchain-ai/langgraph>,
6
+ * `docs/docs/tutorials/customer-support/customer-support.ipynb`) — the Swiss
7
+ * Airlines support bot. Two things in it are the reason to take its shape
8
+ * rather than invent one, and both land differently over a phone than in a
9
+ * chat window:
10
+ *
11
+ * | customer-support | here |
12
+ * | --- | --- |
13
+ * | `State.dialog_state` (an annotated push/pop stack) | {@link TripState.dialogState} |
14
+ * | `ToFlightBookingAssistant` & friends (delegation tools) | the four tools {@link SPECIALISTS} generates in `routing.ts` |
15
+ * | `CompleteOrEscalate` | `complete_or_escalate`, which pops the same stack |
16
+ * | `interrupt_before=["…_sensitive_tools"]` | {@link stageAction} + `confirm_action` |
17
+ * | a specialist node's BOUND tool set | {@link deskTool} / {@link deskUpdateTool}, which every desk tool is declared through |
18
+ * | `fetch_user_flight_information` (sqlite) | `lookup_booking` over {@link seedTrip} |
19
+ *
20
+ * **The dialog stack is what keeps a long call on the rails.** Their insight is
21
+ * that one prompt holding every tool degrades as the tool list grows, so the
22
+ * primary assistant delegates to a specialist and the specialist works with a
23
+ * narrow brief. The specialist's brief arrives as the DELEGATION TOOL'S RESULT,
24
+ * which is the last thing the model reads before it answers.
25
+ *
26
+ * That used to be stated here as a hard limit — "a voice agent cannot swap its
27
+ * system prompt mid-session" — and it is no longer true in general: a state's
28
+ * `instruction` reaches the model on EVERY turn now, on the pipeline and on
29
+ * OpenAI Realtime (AssemblyAI S2S still resolves once). It stays true for THIS
30
+ * template, for a different reason: the desks are a STACK in a session slot,
31
+ * not a dialog position, and a dialog holds one position per session where a
32
+ * stack holds several. Reaching for the new mechanism here would mean modelling
33
+ * the desks as states, which is the port's own design question rather than a
34
+ * fix. The stack itself is real state either way, which is what
35
+ * `complete_or_escalate` pops and what the sidebar renders — and what
36
+ * {@link deskTool} makes binding, so the position is never merely a label on
37
+ * work that happened somewhere else.
38
+ *
39
+ * **`interrupt_before` becomes a spoken confirmation, and that is not a
40
+ * downgrade.** Their graph halts before a sensitive tool and waits for a human
41
+ * to approve in the notebook; a caller cannot type "y", but asking out loud and
42
+ * hearing "yes" is the same gate with a better interface. So a sensitive tool
43
+ * never mutates: it STAGES a {@link PendingAction} and returns the sentence the
44
+ * model should read back, and `confirm_action` is the only thing that applies
45
+ * one. The mechanism is worth copying whole — it is the difference between an
46
+ * agent that can rebook a flight and one that can rebook a flight *by mistake*.
47
+ */
48
+
49
+ import {
50
+ type DeepReadonly,
51
+ type DialogSpec,
52
+ dialog,
53
+ isClockTime,
54
+ type SlotToolDef,
55
+ sessionSlot,
56
+ type ToolContext,
57
+ type ToolDef,
58
+ type ToolFailure,
59
+ type ToolInputSchema,
60
+ } from "@alexkroman1/aai";
61
+ import { formatMoney, plural, roundMoney } from "@alexkroman1/aai/utils";
62
+
63
+ // ─── The booking world ───────────────────────────────────────────────────────
64
+ // Their notebook downloads a sqlite database of a real airline's schedule and
65
+ // rewrites its timestamps to "now". A template ships no database, so the world
66
+ // is a handful of frozen rows — enough for every tool to return something a
67
+ // caller could act on, and small enough to read.
68
+
69
+ export interface Flight {
70
+ id: string;
71
+ route: string;
72
+ departs: string;
73
+ arrives: string;
74
+ fare: number;
75
+ seatsLeft: number;
76
+ }
77
+
78
+ export interface Hotel {
79
+ id: string;
80
+ name: string;
81
+ city: string;
82
+ area: string;
83
+ pricePerNight: number;
84
+ stars: number;
85
+ }
86
+
87
+ export interface CarRental {
88
+ id: string;
89
+ vendor: string;
90
+ city: string;
91
+ tier: string;
92
+ pricePerDay: number;
93
+ }
94
+
95
+ export interface Excursion {
96
+ id: string;
97
+ name: string;
98
+ city: string;
99
+ kind: string;
100
+ price: number;
101
+ }
102
+
103
+ export const FLIGHTS: Flight[] = [
104
+ {
105
+ id: "LX40",
106
+ route: "Zurich to Boston",
107
+ departs: "Tue 13:05",
108
+ arrives: "Tue 15:45",
109
+ fare: 640,
110
+ seatsLeft: 9,
111
+ },
112
+ {
113
+ id: "LX52",
114
+ route: "Zurich to Boston",
115
+ departs: "Wed 09:20",
116
+ arrives: "Wed 12:05",
117
+ fare: 590,
118
+ seatsLeft: 2,
119
+ },
120
+ {
121
+ id: "LX54",
122
+ route: "Zurich to Boston",
123
+ departs: "Thu 17:40",
124
+ arrives: "Thu 20:25",
125
+ fare: 505,
126
+ seatsLeft: 21,
127
+ },
128
+ {
129
+ id: "LX15",
130
+ route: "Boston to Zurich",
131
+ departs: "Sun 21:15",
132
+ arrives: "Mon 10:40",
133
+ fare: 615,
134
+ seatsLeft: 6,
135
+ },
136
+ {
137
+ id: "LX17",
138
+ route: "Boston to Zurich",
139
+ departs: "Mon 19:55",
140
+ arrives: "Tue 09:30",
141
+ fare: 700,
142
+ seatsLeft: 14,
143
+ },
144
+ ];
145
+
146
+ export const HOTELS: Hotel[] = [
147
+ {
148
+ id: "H1",
149
+ name: "Harborview Suites",
150
+ city: "Boston",
151
+ area: "Seaport",
152
+ pricePerNight: 265,
153
+ stars: 4,
154
+ },
155
+ {
156
+ id: "H2",
157
+ name: "The Back Bay",
158
+ city: "Boston",
159
+ area: "Back Bay",
160
+ pricePerNight: 340,
161
+ stars: 5,
162
+ },
163
+ {
164
+ // The one rate with cents in it, and it is deliberate: three nights here is
165
+ // `179.95 * 3 === 539.8499999999999`, so this row is what makes
166
+ // {@link roundMoney} load-bearing rather than decorative — see the totals in
167
+ // `applyPending` and {@link tripView}.
168
+ id: "H3",
169
+ name: "Cambridge Rooms",
170
+ city: "Boston",
171
+ area: "Cambridge",
172
+ pricePerNight: 179.95,
173
+ stars: 3,
174
+ },
175
+ {
176
+ id: "H4",
177
+ name: "Limmat Riverside",
178
+ city: "Zurich",
179
+ area: "Old Town",
180
+ pricePerNight: 295,
181
+ stars: 4,
182
+ },
183
+ ];
184
+
185
+ export const CAR_RENTALS: CarRental[] = [
186
+ { id: "C1", vendor: "Alpine Rentals", city: "Boston", tier: "compact", pricePerDay: 48 },
187
+ { id: "C2", vendor: "Alpine Rentals", city: "Boston", tier: "midsize", pricePerDay: 62 },
188
+ { id: "C3", vendor: "Harbor Auto", city: "Boston", tier: "suv", pricePerDay: 95 },
189
+ { id: "C4", vendor: "Limmat Cars", city: "Zurich", tier: "compact", pricePerDay: 54 },
190
+ ];
191
+
192
+ export const EXCURSIONS: Excursion[] = [
193
+ { id: "E1", name: "Freedom Trail walk", city: "Boston", kind: "walking tour", price: 35 },
194
+ { id: "E2", name: "Harbor sunset sail", city: "Boston", kind: "boat", price: 78 },
195
+ { id: "E3", name: "North End food crawl", city: "Boston", kind: "food", price: 95 },
196
+ { id: "E4", name: "Lake Zurich cruise", city: "Zurich", kind: "boat", price: 52 },
197
+ ];
198
+
199
+ // ─── The dialog stack ────────────────────────────────────────────────────────
200
+
201
+ export const SPECIALIST_IDS = ["flight", "hotel", "car_rental", "excursion"] as const;
202
+ export type SpecialistId = (typeof SPECIALIST_IDS)[number];
203
+
204
+ /** Who is holding the call. `"primary"` is the host assistant — their
205
+ * `dialog_state` bottom, reached by popping everything else off. */
206
+ export type DialogState = "primary" | SpecialistId;
207
+
208
+ /**
209
+ * One specialist's brief, handed back as the delegation tool's RESULT.
210
+ *
211
+ * The instructions are adapted from the four `…_prompt` templates in the
212
+ * notebook, compressed for a phone: theirs end in a paragraph about not making
213
+ * up tools, which a chat model needs and a voice model has no room for. What is
214
+ * kept verbatim in spirit is the part that does the work — search persistently,
215
+ * confirm the details back, and escalate rather than improvise.
216
+ */
217
+ export interface Specialist {
218
+ /** What the caller hears this desk called. */
219
+ title: string;
220
+ /** The brief the model is handed on delegation. */
221
+ instructions: string;
222
+ }
223
+
224
+ /**
225
+ * Each brief spells the STAGE-then-read-back ORDER out, and that is a fix
226
+ * measured rather than a preference.
227
+ *
228
+ * The flight desk's used to say "read the fare change back to the caller before
229
+ * you change a ticket", which is true of `confirm_action` and reads as a rule
230
+ * about `update_ticket` — and live, that is how it was read: the desk searched,
231
+ * quoted a fare it had not staged, waited for a yes, and then called
232
+ * `confirm_action` with nothing waiting for it. Two of three runs never staged
233
+ * anything at all. The sentence to read back IS the staging tool's return
234
+ * value, so a brief that puts the read-back first describes a call the desk
235
+ * cannot make.
236
+ */
237
+ export const SPECIALISTS: Record<SpecialistId, Specialist> = {
238
+ flight: {
239
+ title: "flight desk",
240
+ instructions: [
241
+ "You are now the flight desk. Handle searching and changing flights only.",
242
+ "Search before you quote anything, and be persistent: if the first search",
243
+ "finds nothing, widen it rather than telling the caller there is nothing.",
244
+ "update_ticket is a QUOTE, not a change: it moves nothing, so call it as",
245
+ "soon as you know which flight they want and never ask permission first.",
246
+ "Changing a ticket is TWO steps and never one: call update_ticket first —",
247
+ "it changes nothing and hands you the sentence to say — then read the",
248
+ "flight number, the day, the departure time and the fare change back to",
249
+ "the caller and ask them plainly to confirm. A ticket is only changed once",
250
+ "confirm_action has run, and confirm_action has nothing to apply until",
251
+ "update_ticket has staged it.",
252
+ "Never quote a departure time or a fare you did not just read out of a",
253
+ "tool result, and never end a turn on 'shall I confirm?' with nothing",
254
+ "staged: update_ticket goes out in the SAME turn as that question, before",
255
+ "you stop. There is nothing for a yes to apply until it has.",
256
+ "Your next action, right now, is a TOOL and not a sentence: call",
257
+ "update_ticket if you already know which flight they want, or",
258
+ "search_flights if you do not. Do not speak until one of them has",
259
+ "answered — anything you would say before that is invented.",
260
+ "If the caller wants a hotel, a car or something to do, or changes their",
261
+ "mind, call complete_or_escalate instead of improvising.",
262
+ ].join(" "),
263
+ },
264
+ hotel: {
265
+ title: "hotel desk",
266
+ instructions: [
267
+ "You are now the hotel desk. Handle searching and booking hotels only.",
268
+ "Ask which city and roughly what budget, then search. Offer at most three",
269
+ "options out loud, cheapest first, with the neighbourhood and nightly rate.",
270
+ "book_hotel is a QUOTE, not a change: it holds nothing, so call it as",
271
+ "soon as you know what the caller wants and never ask permission",
272
+ "first.",
273
+ "Booking is TWO steps and never one: call book_hotel first — it holds",
274
+ "nothing and hands you the sentence to say — then read the hotel, the",
275
+ "number of nights and the total back and ask them to confirm. A room is",
276
+ "only held once confirm_action has run, and confirm_action has nothing to",
277
+ "apply until book_hotel has staged it.",
278
+ "Your next action, right now, is a TOOL and not a sentence: call",
279
+ "book_hotel if you already know which room and how many nights, or",
280
+ "search_hotels if you do not. Do not speak until one of them has",
281
+ "answered — anything you would say before that is invented.",
282
+ "If the caller wants flights, a car or an excursion, call",
283
+ "complete_or_escalate.",
284
+ ].join(" "),
285
+ },
286
+ car_rental: {
287
+ title: "car rental desk",
288
+ instructions: [
289
+ "You are now the car rental desk. Handle searching and booking cars only.",
290
+ "Ask the city and how many days, then search. Name the vendor, the tier",
291
+ "and the daily rate.",
292
+ "book_car_rental is a QUOTE, not a change: it holds nothing, so call it as",
293
+ "soon as you know what the caller wants and never ask permission",
294
+ "first.",
295
+ "Reserving is TWO steps and never one: call book_car_rental first — it",
296
+ "reserves nothing and hands you the sentence to say — then read the total",
297
+ "back and ask them to confirm. A car is only reserved once confirm_action",
298
+ "has run, and confirm_action has nothing to apply until book_car_rental",
299
+ "has staged it.",
300
+ "Your next action, right now, is a TOOL and not a sentence: call",
301
+ "book_car_rental if you already know which car and how many days, or",
302
+ "search_car_rentals if you do not. Do not speak until one of them has",
303
+ "answered — anything you would say before that is invented.",
304
+ "If the caller wants flights, a hotel or an excursion, call",
305
+ "complete_or_escalate.",
306
+ ].join(" "),
307
+ },
308
+ excursion: {
309
+ title: "excursions desk",
310
+ instructions: [
311
+ "You are now the excursions desk. Handle recommending and booking things",
312
+ "to do only. Ask the city and what kind of thing they enjoy, then search.",
313
+ "Describe two or three options in a sentence each — this is the part of",
314
+ "the call a caller actually wants to hear about.",
315
+ "book_excursion is a QUOTE, not a change: it holds nothing, so call it as",
316
+ "soon as you know what the caller wants and never ask permission",
317
+ "first.",
318
+ "Booking is TWO steps and never one: call book_excursion first — it books",
319
+ "nothing and hands you the sentence to say — then read it back and ask",
320
+ "them to confirm. A booking is only made once confirm_action has run, and",
321
+ "confirm_action has nothing to apply until book_excursion has staged it.",
322
+ "Your next action, right now, is a TOOL and not a sentence: call",
323
+ "book_excursion if you already know which one they want, or",
324
+ "search_excursions if you do not. Do not speak until one of them has",
325
+ "answered — anything you would say before that is invented.",
326
+ "If the caller wants flights, a hotel or a car, call complete_or_escalate.",
327
+ ].join(" "),
328
+ },
329
+ };
330
+
331
+ // ─── Session state ───────────────────────────────────────────────────────────
332
+
333
+ /** A confirmed booking, as the itinerary carries it. */
334
+ export interface BookingRecord {
335
+ kind: "flight" | "hotel" | "car" | "excursion";
336
+ reference: string;
337
+ summary: string;
338
+ price: number;
339
+ }
340
+
341
+ /**
342
+ * A sensitive action that has been described to the caller and not yet applied.
343
+ *
344
+ * A discriminated union rather than a stored closure: the whole point is that
345
+ * the staged action is INSPECTABLE — `confirm_action` re-derives the effect
346
+ * from it, the projection renders it in the sidebar, and a spec can assert on
347
+ * it. A closure would be none of those things.
348
+ */
349
+ export type PendingAction =
350
+ | { kind: "update_ticket"; flightId: string }
351
+ | { kind: "cancel_ticket" }
352
+ | { kind: "book_hotel"; hotelId: string; nights: number }
353
+ | { kind: "book_car"; carId: string; days: number }
354
+ | { kind: "book_excursion"; excursionId: string };
355
+
356
+ export interface TripState {
357
+ passenger: string;
358
+ /** The ticket the caller is holding, or `null` once they cancel it. */
359
+ ticket: { reference: string; flightId: string } | null;
360
+ /** Their `dialog_state` stack: `primary` is always the bottom. */
361
+ dialogState: DialogState[];
362
+ /** Staged-but-unconfirmed sensitive action — at most one at a time, which
363
+ * {@link stageAction} ENFORCES by refusing a second rather than by saying so
364
+ * here. A concurrent step emitting two sensitive tools is ordinary. */
365
+ pending: PendingAction | null;
366
+ bookings: BookingRecord[];
367
+ /** What has happened on this call, for the sidebar. Capped by the slot. */
368
+ log: string[];
369
+ bookingCounter: number;
370
+ }
371
+
372
+ /** The caller as the airline already knows them — their
373
+ * `fetch_user_flight_information`, minus the sqlite. */
374
+ export function seedTrip(): TripState {
375
+ return {
376
+ passenger: "Nadia Rossi",
377
+ ticket: { reference: "QZ7F2K", flightId: "LX40" },
378
+ dialogState: ["primary"],
379
+ pending: null,
380
+ bookings: [],
381
+ log: [],
382
+ bookingCounter: 0,
383
+ };
384
+ }
385
+
386
+ /** How many call-log lines survive in the projection the browser is pushed. */
387
+ export const LOG_CAP = 40;
388
+
389
+ /**
390
+ * The call's state, as one typed slot.
391
+ *
392
+ * `after` holds the one invariant nothing else should have to remember: the
393
+ * stack always has `primary` at the bottom, so a `complete_or_escalate` that
394
+ * pops one too many cannot leave the session with no assistant at all.
395
+ */
396
+ export const tripSlot = sessionSlot("trip", seedTrip, {
397
+ view: tripView,
398
+ after: (state) => {
399
+ if (state.dialogState.length === 0) state.dialogState.push("primary");
400
+ },
401
+ // The call log rides in every `syncState` frame, and its writers are no longer
402
+ // only tools — `call-events.ts` appends on a hang-up and on a reported error.
403
+ // The bound is declared here rather than at each push, and `LOG_CAP` is
404
+ // exported because a spec reads it.
405
+ caps: { log: LOG_CAP },
406
+ });
407
+
408
+ /**
409
+ * The call as a READ hands it out: deep-frozen, and typed to say so.
410
+ *
411
+ * The pure helpers below take this rather than {@link TripState}, which is the
412
+ * widening a deep-readonly slot forces and the reason it is worth doing: a
413
+ * mutable state still satisfies it, so every `updateTool` draft passes
414
+ * unchanged, while a helper that WOULD have mutated stops compiling instead of
415
+ * throwing at its first call.
416
+ */
417
+ export type FrozenTripState = DeepReadonly<TripState>;
418
+
419
+ export function activeAssistant(state: FrozenTripState): DialogState {
420
+ return state.dialogState.at(-1) ?? "primary";
421
+ }
422
+
423
+ // ─── The desk gate ───────────────────────────────────────────────────────────
424
+
425
+ /**
426
+ * Is the call at `id`'s desk? A {@link ToolFailure} naming the way in if not.
427
+ *
428
+ * **This is the narrowing their graph gets for free, and it used to be prose.**
429
+ * Each specialist node there binds its own tool set, so a node physically cannot
430
+ * call another desk's tools; a voice session has ONE model with ONE tool list
431
+ * for its whole life, so for a long time the stack was real state that the model
432
+ * was merely ASKED to respect — `agent.ts` said so in place, and this guide's
433
+ * own eval predicted the failure. Measured: the prompt lost 0 of 5 live runs.
434
+ * The caller said "I need a hotel in Boston" and the model called
435
+ * `search_hotels` at the concierge desk every time, so `dialogState` said
436
+ * `primary` while the call was plainly doing hotel work — a stack the sidebar
437
+ * renders and nothing keeps true.
438
+ *
439
+ * A tool list cannot be narrowed mid-session, but a tool can REFUSE, and a
440
+ * refusal is a thing the model recovers from inside the same turn: it reads the
441
+ * failure, calls `to_hotel_assistant`, gets the desk's brief, and searches. So
442
+ * the delegation is now structural — every desk tool is reachable only from its
443
+ * own desk — and the brief the delegation returns is guaranteed to have been
444
+ * read before any of that desk's work happens, which was the whole point of
445
+ * handing it back as a tool result.
446
+ *
447
+ * It costs one wasted round trip the first time the model reaches past the
448
+ * stack, which is the right price: the alternative is a desk whose position is
449
+ * decoration.
450
+ *
451
+ * Not exported: {@link deskTool} and {@link deskUpdateTool} are the only callers,
452
+ * which is what makes a desk tool unable to skip it.
453
+ */
454
+ function requireDesk(state: FrozenTripState, id: SpecialistId): ToolFailure | undefined {
455
+ const at = activeAssistant(state);
456
+ if (at === id) return undefined;
457
+ const here = at === "primary" ? "the main concierge" : SPECIALISTS[at].title;
458
+ return {
459
+ error:
460
+ `That tool belongs to the ${SPECIALISTS[id].title} and this call is at ${here}. ` +
461
+ `Nothing was searched, staged or booked. Call to_${id}_assistant with what the ` +
462
+ "caller asked for — its answer is that desk's brief — and then call this tool again. " +
463
+ "Do not tell the caller about any of this; they should hear one continuous conversation.",
464
+ };
465
+ }
466
+
467
+ /**
468
+ * What a desk tool declares: the slot's own {@link SlotToolDef}, with the schema
469
+ * REQUIRED — `cancel_ticket` takes no arguments and passes `z.object({})`.
470
+ *
471
+ * One code path in the wrapper is worth more than a line saved at one call
472
+ * site: `exactOptionalPropertyTypes` is on, so an `inputSchema` forwarded
473
+ * through a spread arrives as `P | undefined`, which is not what "this tool
474
+ * takes no arguments" means to the slot.
475
+ */
476
+ type DeskToolDef<P extends ToolInputSchema, V, R> = Omit<SlotToolDef<P, V, R>, "inputSchema"> & {
477
+ inputSchema: P;
478
+ };
479
+
480
+ /**
481
+ * A desk's own tool: {@link requireDesk} first, then the body — declared once.
482
+ *
483
+ * Nine tools used to open with the same two lines, and a wrapper is what makes
484
+ * the narrowing STRUCTURAL rather than remembered: their graph binds a tool set
485
+ * per node, and the tenth desk tool written here cannot forget the gate the way
486
+ * a copied prologue can. It is the per-agent wrapper `retail-orders-agent` demonstrates, at
487
+ * its smallest — all this one adds is the gate, so what it takes is the SLOT's
488
+ * OWN {@link SlotToolDef} rather than a spec of its own, and a tool file reads
489
+ * exactly as it did minus the prologue.
490
+ *
491
+ * Two of them because the slot has two halves and choosing wrong is a compile
492
+ * error: a search READS the frozen state, a staging tool takes the mutable
493
+ * draft. `requireDesk` short-circuits before either body runs, so a refused call
494
+ * really searches, stages and books nothing.
495
+ */
496
+ export function deskTool<P extends ToolInputSchema, R>(
497
+ id: SpecialistId,
498
+ def: DeskToolDef<P, FrozenTripState, R>,
499
+ ): ToolDef<P, R | ToolFailure> {
500
+ return tripSlot.tool<P, R | ToolFailure>({
501
+ description: def.description,
502
+ inputSchema: def.inputSchema,
503
+ execute: (args, trip, ctx) => requireDesk(trip, id) ?? def.execute(args, trip, ctx),
504
+ });
505
+ }
506
+
507
+ /**
508
+ * {@link deskTool} for a tool that WRITES — the draft half of the slot.
509
+ *
510
+ * Its result type is FIXED at {@link StagedReadBack} rather than generic, and
511
+ * that is the template's central rule stated as a type: a desk tool that touches
512
+ * the draft may only STAGE. There is nothing else for one to answer — a body
513
+ * that applied a change itself would have to invent a receipt, and the compiler
514
+ * asks for the read-back instead.
515
+ */
516
+ export function deskUpdateTool<P extends ToolInputSchema>(
517
+ id: SpecialistId,
518
+ def: DeskToolDef<P, TripState, StagedReadBack | ToolFailure>,
519
+ ): ToolDef<P, StagedReadBack | ToolFailure> {
520
+ return tripSlot.updateTool<P, StagedReadBack | ToolFailure>({
521
+ description: def.description,
522
+ inputSchema: def.inputSchema,
523
+ execute: (args, trip, ctx) => requireDesk(trip, id) ?? def.execute(args, trip, ctx),
524
+ });
525
+ }
526
+
527
+ /**
528
+ * A flight's departure as a comparable `HH:MM`, or `undefined` for a row that
529
+ * does not carry one.
530
+ *
531
+ * The schedule writes a departure as `"Tue 13:05"` — a weekday a caller says
532
+ * and a clock reading a caller filters by — and `search_flights` compares the
533
+ * clock half as a STRING, which is legal for exactly the reason
534
+ * {@link isClockTime} exists: a zero-padded 24-hour reading sorts the way the
535
+ * clock runs, and `"9:05"` does not. So the predicate is what stands between a
536
+ * row and a comparison, rather than a regex written here: a row it refuses is
537
+ * left out of a time-filtered search instead of being ordered against `"Tue"`.
538
+ */
539
+ export function departureClock(flight: DeepReadonly<Flight>): string | undefined {
540
+ const clock = flight.departs.slice(-5);
541
+ return isClockTime(clock) ? clock : undefined;
542
+ }
543
+
544
+ // ─── The confirmation gate ───────────────────────────────────────────────────
545
+
546
+ /**
547
+ * The gate as a STATE MAP — their `interrupt_before`, which is a graph construct.
548
+ *
549
+ * This port had to hand-roll it: a voice session had no graph, so "halt before a
550
+ * sensitive tool and resume on approval" became "every sensitive tool STAGES and
551
+ * `confirm_action` is the only thing that applies one". That half is unchanged
552
+ * and is still the mechanism — see {@link stageAction}. What the machine adds is
553
+ * the PRECONDITION on the other side of the halt: `confirm_action` and
554
+ * `cancel_action` are legal only while something is actually waiting, which was
555
+ * a null check inside each of them and is now `when`.
556
+ *
557
+ * **The staging tools are deliberately NOT gated on `browsing`.**
558
+ * {@link stageAction}'s refusal NAMES the sentence already waiting, which is what
559
+ * lets the model settle that one first and restage the rest; a state gate can
560
+ * only say "you are in awaitingConfirmation", since a state's instruction is
561
+ * static. So the specific refusal stays where it is and the flow follows it.
562
+ */
563
+ const gateSpec = {
564
+ initial: "browsing",
565
+ states: {
566
+ browsing: {
567
+ instruction:
568
+ "Nothing is waiting for the caller's yes. Stage a change with a booking tool first.",
569
+ on: { STAGED: "awaitingConfirmation", "@session.timedOut": "abandoned" },
570
+ },
571
+ awaitingConfirmation: {
572
+ instruction:
573
+ "Read the staged change back and hear a clear yes or no, then use confirm_action or cancel_action.",
574
+ on: { SETTLED: "browsing", "@session.timedOut": "abandoned" },
575
+ },
576
+ /**
577
+ * The caller hung up, or the session timed out under them.
578
+ *
579
+ * Declared on both live states rather than on a parent, because wrapping
580
+ * them would rename `browsing` and `awaitingConfirmation` to
581
+ * `onCall.browsing` and `onCall.awaitingConfirmation` — the two names every
582
+ * `when` in `tools/` is written against, and the sidebar renders.
583
+ *
584
+ * What it does NOT do is settle the staged action: a deadline or a session
585
+ * event SENDS AN EVENT, it does not run a tool, so `pending` is still
586
+ * whatever `stageAction` left there. That is fine here and is the reason
587
+ * this state is `final` — nothing may act on this call again, so nothing
588
+ * reads `pending` again either. It would NOT be fine as a timeout that
589
+ * returned to `browsing`, which is the trap recorded in
590
+ * `aai-runtime`'s `DIALOG-CLAUDE.md`.
591
+ */
592
+ abandoned: {
593
+ final: true,
594
+ instruction:
595
+ "The caller is gone. Do nothing further — do not confirm the staged change, do not " +
596
+ "book anything, and do not promise a callback.",
597
+ },
598
+ },
599
+ } as const satisfies DialogSpec;
600
+
601
+ /**
602
+ * Whether a change is waiting on the caller's word.
603
+ *
604
+ * Its own slot, beside {@link tripSlot}: the flow holds the POSITION and the
605
+ * trip holds the staged action itself, because an inspectable
606
+ * {@link PendingAction} is what `confirm_action` re-derives the effect from. One
607
+ * tool call always moves both — {@link stageAction} sends `STAGED` in the same
608
+ * synchronous window it writes `pending` in, and the two settling tools send
609
+ * `SETTLED` only on success.
610
+ */
611
+ export const gateFlow = dialog("gate", gateSpec);
612
+
613
+ /**
614
+ * Describe a staged action in one sentence, in the second person — this is
615
+ * read aloud, so it is a question's worth of text and not a receipt.
616
+ */
617
+ export function describeAction(action: DeepReadonly<PendingAction>): string | ToolFailure {
618
+ switch (action.kind) {
619
+ case "update_ticket": {
620
+ const flight = FLIGHTS.find((f) => f.id === action.flightId);
621
+ if (!flight) return { error: `No flight ${action.flightId} in the schedule.` };
622
+ return `move your ticket to ${flight.id}, ${flight.route}, departing ${flight.departs}, at ${formatMoney(flight.fare)}`;
623
+ }
624
+ case "cancel_ticket":
625
+ return "cancel your ticket entirely";
626
+ case "book_hotel": {
627
+ const hotel = HOTELS.find((h) => h.id === action.hotelId);
628
+ if (!hotel) return { error: `No hotel ${action.hotelId}.` };
629
+ return `book ${hotel.name} in ${hotel.area} for ${action.nights} ${plural(action.nights, "night")}, ${formatMoney(hotel.pricePerNight * action.nights)} total`;
630
+ }
631
+ case "book_car": {
632
+ const car = CAR_RENTALS.find((c) => c.id === action.carId);
633
+ if (!car) return { error: `No car ${action.carId}.` };
634
+ return `reserve the ${car.tier} from ${car.vendor} for ${action.days} ${plural(action.days, "day")}, ${formatMoney(car.pricePerDay * action.days)} total`;
635
+ }
636
+ case "book_excursion": {
637
+ const excursion = EXCURSIONS.find((e) => e.id === action.excursionId);
638
+ if (!excursion) return { error: `No excursion ${action.excursionId}.` };
639
+ return `book ${excursion.name} at ${formatMoney(excursion.price)}`;
640
+ }
641
+ // Unreachable while `PendingAction` is exhausted above — and the arm a new
642
+ // member of the union lands in until it has one of its own, which is a
643
+ // refusal rather than an unexplained sentence read down a phone.
644
+ default:
645
+ return { error: "That change cannot be described, so it will not be staged." };
646
+ }
647
+ }
648
+
649
+ /**
650
+ * The sentence for an action that has ALREADY been staged.
651
+ *
652
+ * `describeAction` answers `string | ToolFailure` because {@link stageAction} is
653
+ * the one caller that can be handed an id naming nothing, and its refusal names
654
+ * that id. Everything else describes something `stageAction` already accepted,
655
+ * so the failure arm is unreachable — and every such site had written
656
+ * `typeof described === "string" ? described : ...` to guard it, one of which
657
+ * read as if a `ToolFailure` might be spoken to the caller. The fallback is the
658
+ * action's own kind, so a catalog that somehow lost an entry mid-call degrades
659
+ * to a rough noun rather than to a stray `[object Object]`.
660
+ */
661
+ export function describeStaged(action: DeepReadonly<PendingAction>): string {
662
+ const described = describeAction(action);
663
+ return typeof described === "string" ? described : action.kind;
664
+ }
665
+
666
+ /**
667
+ * What a sensitive tool answers with: the sentence to read aloud, and the fact
668
+ * that nothing has happened yet.
669
+ *
670
+ * Named because {@link deskUpdateTool} pins every writing desk tool to it — see
671
+ * there for why that is the gate rather than a convenience.
672
+ */
673
+ export type StagedReadBack = {
674
+ awaitingConfirmation: true;
675
+ readBack: string;
676
+ expires: "on the caller's next answer";
677
+ };
678
+
679
+ /**
680
+ * Stage a sensitive action and return what a sensitive tool answers with.
681
+ *
682
+ * Every sensitive tool ends in this call and none of them mutate anything —
683
+ * that is the whole gate, and keeping it to one helper is what stops the next
684
+ * sensitive tool from being the one that forgets it.
685
+ *
686
+ * **It REFUSES a second staging, and that is the enforcement half of
687
+ * {@link TripState.pending}'s "at most one at a time".** The LLM loop runs a
688
+ * step's tool calls CONCURRENTLY, so two sensitive tools in one step is an
689
+ * ordinary outcome — the model says "change my flight and book the hotel" and
690
+ * emits both. An unconditional assignment made the second win: both tools
691
+ * answered `awaitingConfirmation`, the model read both back, the caller said
692
+ * yes, and `confirm_action` applied ONE of them with nothing anywhere saying
693
+ * the other had been dropped. A gate whose failure is a silent no-op is not a
694
+ * gate. Refusing names the sentence already waiting, which is what lets the
695
+ * model ask about that one first and restage the rest afterwards.
696
+ */
697
+ export function stageAction(
698
+ ctx: ToolContext,
699
+ state: TripState,
700
+ action: PendingAction,
701
+ ): StagedReadBack | ToolFailure {
702
+ const described = describeAction(action);
703
+ if (typeof described !== "string") return described;
704
+ if (state.pending) {
705
+ return {
706
+ error:
707
+ `Something is already waiting for the caller's yes: ${describeStaged(state.pending)}. ` +
708
+ "Settle it with confirm_action or cancel_action, then ask for this one. " +
709
+ "Only one change can be waiting at a time — nothing about this request has been staged.",
710
+ };
711
+ }
712
+ state.pending = action;
713
+ // The flow moves in the SAME synchronous window `pending` is written in, so
714
+ // the position and the payload cannot be observed disagreeing. `gateFlow` is
715
+ // a different slot from `tripSlot`, so this is not a nested write to the draft
716
+ // being held — the open-draft guard is per slot.
717
+ gateFlow.send(ctx, { type: "STAGED" });
718
+ state.log.push(`Awaiting confirmation: ${described}`);
719
+ return {
720
+ awaitingConfirmation: true,
721
+ readBack: `Ask the caller to confirm, out loud, that they want to ${described}. Nothing has changed yet.`,
722
+ expires: "on the caller's next answer",
723
+ };
724
+ }
725
+
726
+ /**
727
+ * Apply the staged action. The ONE place any of them takes effect.
728
+ *
729
+ * The "nothing is waiting" guard is gone: `confirm_action` is gated on
730
+ * `awaitingConfirmation`, so reaching here means something IS staged. The
731
+ * remaining `!action` arm is the flow and the payload disagreeing, which one
732
+ * synchronous window per transition is what rules out — it reports rather than
733
+ * throwing, because a live call is the wrong place to find out.
734
+ */
735
+ export function applyPending(
736
+ state: TripState,
737
+ ): { applied: string; reference?: string } | ToolFailure {
738
+ const action = state.pending;
739
+ if (!action) {
740
+ return { error: "Nothing is staged after all — ask the caller again what they want." };
741
+ }
742
+ state.pending = null;
743
+ state.bookingCounter++;
744
+ const reference = `BK${String(1000 + state.bookingCounter)}`;
745
+
746
+ switch (action.kind) {
747
+ case "update_ticket": {
748
+ const flight = FLIGHTS.find((f) => f.id === action.flightId);
749
+ if (!flight) return { error: `No flight ${action.flightId}.` };
750
+ if (flight.seatsLeft <= 0) return { error: `${flight.id} has no seats left.` };
751
+ if (!state.ticket) return { error: "There is no ticket to move — it was cancelled." };
752
+ state.ticket = { reference: state.ticket.reference, flightId: flight.id };
753
+ state.log.push(`Ticket ${state.ticket.reference} moved to ${flight.id}`);
754
+ return { applied: `Ticket moved to ${flight.id}, ${flight.departs}.` };
755
+ }
756
+ case "cancel_ticket": {
757
+ if (!state.ticket) return { error: "There is no ticket to cancel." };
758
+ const cancelled = state.ticket.reference;
759
+ state.ticket = null;
760
+ state.log.push(`Ticket ${cancelled} cancelled`);
761
+ return { applied: `Ticket ${cancelled} cancelled.` };
762
+ }
763
+ case "book_hotel": {
764
+ const hotel = HOTELS.find((h) => h.id === action.hotelId);
765
+ if (!hotel) return { error: `No hotel ${action.hotelId}.` };
766
+ // `roundMoney` because this is STORED and then summed into the itinerary
767
+ // total: `179.95 * 3` is `539.8499999999999`, which prints as $539.85 and
768
+ // compares equal to nothing anyone would write down.
769
+ const price = roundMoney(hotel.pricePerNight * action.nights);
770
+ state.bookings.push({
771
+ kind: "hotel",
772
+ reference,
773
+ summary: `${hotel.name} (${hotel.area}), ${action.nights} ${plural(action.nights, "night")}`,
774
+ price,
775
+ });
776
+ state.log.push(`Hotel booked: ${hotel.name} — ${reference}`);
777
+ return { applied: `${hotel.name} booked, ${formatMoney(price)}.`, reference };
778
+ }
779
+ case "book_car": {
780
+ const car = CAR_RENTALS.find((c) => c.id === action.carId);
781
+ if (!car) return { error: `No car ${action.carId}.` };
782
+ const price = roundMoney(car.pricePerDay * action.days);
783
+ state.bookings.push({
784
+ kind: "car",
785
+ reference,
786
+ summary: `${car.vendor} ${car.tier}, ${action.days} ${plural(action.days, "day")}`,
787
+ price,
788
+ });
789
+ state.log.push(`Car reserved: ${car.vendor} ${car.tier} — ${reference}`);
790
+ return {
791
+ applied: `${car.tier} from ${car.vendor} reserved, ${formatMoney(price)}.`,
792
+ reference,
793
+ };
794
+ }
795
+ case "book_excursion": {
796
+ const excursion = EXCURSIONS.find((e) => e.id === action.excursionId);
797
+ if (!excursion) return { error: `No excursion ${action.excursionId}.` };
798
+ state.bookings.push({
799
+ kind: "excursion",
800
+ reference,
801
+ summary: `${excursion.name} (${excursion.city})`,
802
+ price: excursion.price,
803
+ });
804
+ state.log.push(`Excursion booked: ${excursion.name} — ${reference}`);
805
+ return { applied: `${excursion.name} booked, ${formatMoney(excursion.price)}.`, reference };
806
+ }
807
+ // Same as `describeAction`: unreachable today, and a refusal rather than a
808
+ // silent no-op for whatever the union grows next.
809
+ default:
810
+ return { error: "That change has no handler, so nothing was applied." };
811
+ }
812
+ }
813
+
814
+ // ─── The projection ──────────────────────────────────────────────────────────
815
+
816
+ export interface TripView {
817
+ passenger: string;
818
+ /** Which desk is holding the call — the stack's top, rendered as a badge. */
819
+ assistant: DialogState;
820
+ assistantTitle: string;
821
+ ticket: { reference: string; flightId: string; route: string; departs: string } | null;
822
+ bookings: readonly DeepReadonly<BookingRecord>[];
823
+ total: number;
824
+ /** The staged action's sentence, so the browser shows what voice just asked. */
825
+ pending: string | null;
826
+ log: readonly string[];
827
+ }
828
+
829
+ /**
830
+ * What the browser sees. A projection rather than the raw state, for the reason
831
+ * `syncState` takes one: the caller's ticket reference is theirs, the staged
832
+ * action has to be rendered as PROSE rather than as a union the client would
833
+ * have to re-switch on, and the stack top is the one field the sidebar is
834
+ * actually built around.
835
+ */
836
+ export function tripView(state: FrozenTripState): TripView {
837
+ const assistant = activeAssistant(state);
838
+ const flight = state.ticket ? FLIGHTS.find((f) => f.id === state.ticket?.flightId) : undefined;
839
+ const described = state.pending ? describeStaged(state.pending) : null;
840
+ return {
841
+ passenger: state.passenger,
842
+ assistant,
843
+ assistantTitle: assistant === "primary" ? "concierge" : SPECIALISTS[assistant].title,
844
+ ticket:
845
+ state.ticket && flight
846
+ ? {
847
+ reference: state.ticket.reference,
848
+ flightId: flight.id,
849
+ route: flight.route,
850
+ departs: flight.departs,
851
+ }
852
+ : null,
853
+ bookings: state.bookings,
854
+ // The running total the sidebar prints and a spec compares. Each booking's
855
+ // price was already rounded where it was STORED — which is where today's
856
+ // catalog actually produces dust — and the sum goes through the same rule
857
+ // rather than through a rate table that happens not to need it.
858
+ total: roundMoney(state.bookings.reduce((sum, b) => sum + b.price, 0)),
859
+ pending: described,
860
+ log: state.log,
861
+ };
862
+ }