@alexkroman1/aai-cli 16.1.0 → 18.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 (220) hide show
  1. package/README.md +2 -1
  2. package/dist/{_build-target-ClEEuUuW.mjs → _build-target-C08aM-Es.mjs} +504 -43
  3. package/dist/_build-target.d.ts +98 -19
  4. package/dist/_console-audio.d.ts +17 -0
  5. package/dist/_console-session.d.ts +70 -0
  6. package/dist/_deno-target.d.ts +15 -14
  7. package/dist/_dev-env.d.ts +19 -13
  8. package/dist/{_dev-server-D1wKiA0_.mjs → _dev-server-Dtp4aAlo.mjs} +26 -18
  9. package/dist/_dev-server.d.ts +5 -3
  10. package/dist/_init.d.ts +39 -0
  11. package/dist/_modal-target.d.ts +11 -6
  12. package/dist/_preflight-CnW9QiE7.mjs +116 -0
  13. package/dist/_preflight.d.ts +110 -5
  14. package/dist/_target-bundle.d.ts +112 -0
  15. package/dist/_target-entry.d.ts +81 -0
  16. package/dist/_templates-DXadVNsL.mjs +135 -0
  17. package/dist/_templates.d.ts +11 -34
  18. package/dist/{_vitest-runner-BiAMIir1.mjs → _vitest-runner-CZNGj0ZC.mjs} +21 -16
  19. package/dist/_vitest-runner.d.ts +35 -23
  20. package/dist/{build-O6BT51x_.mjs → build-DrQTSc2R.mjs} +128 -293
  21. package/dist/build.d.ts +34 -4
  22. package/dist/cli.mjs +40 -16
  23. package/dist/console-BsyE3DyB.mjs +422 -0
  24. package/dist/console.d.ts +37 -0
  25. package/dist/{deploy-lldQyJVj.mjs → deploy-B68-x7xM.mjs} +12 -4
  26. package/dist/deploy.d.ts +9 -0
  27. package/dist/{dev-Du8pCWFq.mjs → dev-BGWU2zPP.mjs} +1 -1
  28. package/dist/dev.d.ts +1 -1
  29. package/dist/{eval-Ducmkt1n.mjs → eval-DB07kTxO.mjs} +1 -1
  30. package/dist/init-CbOhyNYe.mjs +559 -0
  31. package/dist/init.d.ts +43 -3
  32. package/dist/scaffold/CLAUDE.md +161 -166
  33. package/dist/scaffold/package.json +6 -6
  34. package/dist/scaffold/vite.config.ts +29 -1
  35. package/dist/start.d.ts +3 -3
  36. package/dist/{studio-It7SRfUN.mjs → studio-BRJsw7lr.mjs} +19 -9
  37. package/dist/templates/applicant-screening-agent/agent.eval.test.ts +20 -6
  38. package/dist/templates/applicant-screening-agent/agent.test.ts +17 -13
  39. package/dist/templates/applicant-screening-agent/agent.ts +28 -0
  40. package/dist/templates/applicant-screening-agent/crews.ts +4 -4
  41. package/dist/templates/applicant-screening-agent/shared.ts +11 -11
  42. package/dist/templates/call-audit-workflow/agent.ts +2 -0
  43. package/dist/templates/code-interpreter-agent/agent.eval.test.ts +3 -2
  44. package/dist/templates/code-interpreter-agent/agent.test.ts +2 -2
  45. package/dist/templates/code-interpreter-agent/agent.ts +1 -0
  46. package/dist/templates/coding-agent/agent.eval.test.ts +136 -0
  47. package/dist/templates/coding-agent/agent.test.ts +109 -0
  48. package/dist/templates/coding-agent/agent.ts +45 -0
  49. package/dist/templates/coding-agent/chat.ts +63 -0
  50. package/dist/templates/coding-agent/shared.ts +46 -0
  51. package/dist/templates/coding-agent/system-prompt.md +32 -0
  52. package/dist/templates/coding-agent/tools/bash.ts +6 -0
  53. package/dist/templates/coding-agent/tools/delete_file.ts +6 -0
  54. package/dist/templates/coding-agent/tools/edit_file.ts +6 -0
  55. package/dist/templates/coding-agent/tools/glob.ts +6 -0
  56. package/dist/templates/coding-agent/tools/grep.ts +6 -0
  57. package/dist/templates/coding-agent/tools/list_files.ts +6 -0
  58. package/dist/templates/coding-agent/tools/read_file.ts +6 -0
  59. package/dist/templates/coding-agent/tools/todo_write.ts +6 -0
  60. package/dist/templates/coding-agent/tools/write_file.ts +6 -0
  61. package/dist/templates/custom-pipeline-agent/agent.ts +3 -2
  62. package/dist/templates/custom-pipeline-agent/modes.test.ts +4 -4
  63. package/dist/templates/custom-pipeline-agent/stages.test.ts +75 -83
  64. package/dist/templates/document-redline-workflow/agent.ts +2 -0
  65. package/dist/templates/emergency-dispatch-agent/agent.eval.test.ts +1 -1
  66. package/dist/templates/emergency-dispatch-agent/agent.test.ts +103 -0
  67. package/dist/templates/emergency-dispatch-agent/agent.ts +15 -0
  68. package/dist/templates/emergency-dispatch-agent/guardrails.ts +105 -0
  69. package/dist/templates/emergency-dispatch-agent/system-prompt.md +1 -0
  70. package/dist/templates/entertainment-picks-agent/agent.eval.test.ts +9 -2
  71. package/dist/templates/entertainment-picks-agent/agent.ts +1 -0
  72. package/dist/templates/entertainment-picks-agent/system-prompt.md +6 -0
  73. package/dist/templates/entertainment-picks-agent/tools/revisit.ts +7 -0
  74. package/dist/templates/executive-inbox-agent/agent.eval.test.ts +84 -23
  75. package/dist/templates/executive-inbox-agent/agent.test.ts +35 -25
  76. package/dist/templates/executive-inbox-agent/agent.ts +1 -0
  77. package/dist/templates/executive-inbox-agent/calendar.test.ts +13 -7
  78. package/dist/templates/executive-inbox-agent/meeting.ts +3 -3
  79. package/dist/templates/executive-inbox-agent/prompts.ts +3 -1
  80. package/dist/templates/executive-inbox-agent/system-prompt.md +3 -3
  81. package/dist/templates/executive-inbox-agent/tools/meeting_assistant.ts +3 -3
  82. package/dist/templates/executive-inbox-agent/tools/open_email.ts +8 -16
  83. package/dist/templates/front-desk-agent/agent.eval.test.ts +174 -0
  84. package/dist/templates/front-desk-agent/agent.test.ts +150 -0
  85. package/dist/templates/front-desk-agent/agent.ts +22 -0
  86. package/dist/templates/front-desk-agent/shared.ts +234 -0
  87. package/dist/templates/front-desk-agent/system-prompt.md +13 -0
  88. package/dist/templates/front-desk-agent/tools/verify_account.ts +41 -0
  89. package/dist/templates/hotel-reception-agent/agent.eval.test.ts +85 -21
  90. package/dist/templates/hotel-reception-agent/agent.test.ts +3 -3
  91. package/dist/templates/hotel-reception-agent/agent.ts +6 -3
  92. package/dist/templates/hotel-reception-agent/client.tsx +14 -2
  93. package/dist/templates/hotel-reception-agent/concierge.ts +2 -1
  94. package/dist/templates/hotel-reception-agent/desk-seams.test.ts +3 -3
  95. package/dist/templates/hotel-reception-agent/disputes.ts +9 -11
  96. package/dist/templates/hotel-reception-agent/events.ts +2 -1
  97. package/dist/templates/hotel-reception-agent/session.ts +56 -0
  98. package/dist/templates/hotel-reception-agent/shared.ts +20 -21
  99. package/dist/templates/hotel-reception-agent/system-prompt.md +1 -1
  100. package/dist/templates/hotel-reception-agent/tools/abandon_booking.ts +2 -1
  101. package/dist/templates/hotel-reception-agent/tools/add_to_waitlist.ts +2 -1
  102. package/dist/templates/hotel-reception-agent/tools/book_airport_car.ts +2 -1
  103. package/dist/templates/hotel-reception-agent/tools/cancel_restaurant_reservation.ts +2 -1
  104. package/dist/templates/hotel-reception-agent/tools/cancel_room_booking.ts +2 -1
  105. package/dist/templates/hotel-reception-agent/tools/check_restaurant_availability.ts +1 -1
  106. package/dist/templates/hotel-reception-agent/tools/check_room_availability.ts +1 -1
  107. package/dist/templates/hotel-reception-agent/tools/choose_room.ts +1 -1
  108. package/dist/templates/hotel-reception-agent/tools/confirm_booking.ts +2 -1
  109. package/dist/templates/hotel-reception-agent/tools/dispatch_emergency.ts +2 -1
  110. package/dist/templates/hotel-reception-agent/tools/dispute_charge.ts +2 -1
  111. package/dist/templates/hotel-reception-agent/tools/flag_late_arrival.ts +2 -1
  112. package/dist/templates/hotel-reception-agent/tools/lookup_booking.ts +2 -1
  113. package/dist/templates/hotel-reception-agent/tools/lookup_guest_history.ts +1 -1
  114. package/dist/templates/hotel-reception-agent/tools/lookup_invoice.ts +2 -1
  115. package/dist/templates/hotel-reception-agent/tools/lookup_restaurant_reservation.ts +1 -1
  116. package/dist/templates/hotel-reception-agent/tools/modify_restaurant_reservation.ts +1 -1
  117. package/dist/templates/hotel-reception-agent/tools/record_card.ts +1 -1
  118. package/dist/templates/hotel-reception-agent/tools/record_followup.ts +2 -1
  119. package/dist/templates/hotel-reception-agent/tools/record_group_inquiry.ts +2 -1
  120. package/dist/templates/hotel-reception-agent/tools/record_guest_details.ts +1 -1
  121. package/dist/templates/hotel-reception-agent/tools/reinstate_booking.ts +1 -1
  122. package/dist/templates/hotel-reception-agent/tools/request_flight_reconfirmation.ts +2 -1
  123. package/dist/templates/hotel-reception-agent/tools/resend_confirmation.ts +2 -1
  124. package/dist/templates/hotel-reception-agent/tools/reserve_table.ts +1 -1
  125. package/dist/templates/hotel-reception-agent/tools/resolve_room_conflict.ts +2 -1
  126. package/dist/templates/hotel-reception-agent/tools/schedule_wakeup_call.ts +2 -1
  127. package/dist/templates/hotel-reception-agent/tools/set_do_not_disturb.ts +2 -1
  128. package/dist/templates/hotel-reception-agent/tools/set_extras.ts +1 -1
  129. package/dist/templates/hotel-reception-agent/tools/set_stay.ts +2 -1
  130. package/dist/templates/hotel-reception-agent/tools/start_booking_modification.ts +2 -1
  131. package/dist/templates/hotel-reception-agent/tools/start_room_booking.ts +2 -1
  132. package/dist/templates/hotel-reception-agent/tools/take_guest_message.ts +2 -1
  133. package/dist/templates/hotel-reception-agent/tools/transfer_call.ts +2 -1
  134. package/dist/templates/hotel-reception-agent/tools/update_card.ts +2 -1
  135. package/dist/templates/hotel-reception-agent/tools/verify_booking.ts +2 -1
  136. package/dist/templates/link-digest-workflow/agent.eval.test.ts +16 -1
  137. package/dist/templates/link-digest-workflow/agent.ts +5 -0
  138. package/dist/templates/medication-safety-agent/agent.test.ts +57 -4
  139. package/dist/templates/medication-safety-agent/agent.ts +48 -1
  140. package/dist/templates/meeting-recap-agent/agent.test.ts +2 -2
  141. package/dist/templates/meeting-recap-agent/agent.ts +4 -0
  142. package/dist/templates/pizza-ordering-agent/agent.eval.test.ts +68 -2
  143. package/dist/templates/pizza-ordering-agent/agent.ts +3 -0
  144. package/dist/templates/podcast-digest-workflow/agent.ts +4 -0
  145. package/dist/templates/push-to-talk-agent/agent.eval.test.ts +74 -0
  146. package/dist/templates/push-to-talk-agent/agent.test.ts +61 -0
  147. package/dist/templates/push-to-talk-agent/agent.ts +22 -0
  148. package/dist/templates/push-to-talk-agent/client.tsx +124 -0
  149. package/dist/templates/push-to-talk-agent/shared.ts +34 -0
  150. package/dist/templates/push-to-talk-agent/system-prompt.md +11 -0
  151. package/dist/templates/push-to-talk-agent/tools/list_notes.ts +15 -0
  152. package/dist/templates/push-to-talk-agent/tools/save_note.ts +26 -0
  153. package/dist/templates/quickstart-agent/agent.eval.test.ts +91 -3
  154. package/dist/templates/quickstart-agent/agent.test.ts +22 -0
  155. package/dist/templates/quickstart-agent/agent.ts +18 -2
  156. package/dist/templates/quickstart-agent/system-prompt.md +8 -0
  157. package/dist/templates/quickstart-agent/tools/get_weather.ts +59 -0
  158. package/dist/templates/research-handoff-agent/agent.eval.test.ts +1 -1
  159. package/dist/templates/research-handoff-agent/agent.test.ts +34 -26
  160. package/dist/templates/research-handoff-agent/agent.ts +4 -0
  161. package/dist/templates/research-planner-agent/agent.eval.test.ts +24 -2
  162. package/dist/templates/research-planner-agent/agent.test.ts +26 -16
  163. package/dist/templates/research-planner-agent/agent.ts +3 -0
  164. package/dist/templates/research-planner-agent/system-prompt.md +1 -1
  165. package/dist/templates/retail-orders-agent/agent.eval.test.ts +1 -1
  166. package/dist/templates/retail-orders-agent/agent.ts +32 -3
  167. package/dist/templates/retail-orders-agent/keyterms.ts +154 -0
  168. package/dist/templates/retail-orders-agent/resolve.ts +18 -3
  169. package/dist/templates/roadside-assistance-agent/agent.eval.test.ts +124 -5
  170. package/dist/templates/roadside-assistance-agent/agent.test.ts +39 -7
  171. package/dist/templates/roadside-assistance-agent/agent.ts +4 -0
  172. package/dist/templates/roadside-assistance-agent/shared.ts +12 -0
  173. package/dist/templates/roadside-assistance-agent/tools/acknowledge_disclosure.ts +14 -0
  174. package/dist/templates/roadside-assistance-agent/tools/lookup_coverage.ts +11 -0
  175. package/dist/templates/roadside-assistance-agent/tools/service_disclosure.ts +17 -11
  176. package/dist/templates/roadside-assistance-agent/yard.test.ts +4 -0
  177. package/dist/templates/spoken-summary-workflow/agent.ts +4 -0
  178. package/dist/templates/tabletop-rpg-agent/agent.eval.test.ts +6 -0
  179. package/dist/templates/tabletop-rpg-agent/agent.test.ts +125 -1
  180. package/dist/templates/tabletop-rpg-agent/agent.ts +30 -1
  181. package/dist/templates/tabletop-rpg-agent/shared.ts +81 -0
  182. package/dist/templates/tabletop-rpg-agent/system-prompt.md +1 -1
  183. package/dist/templates/tabletop-rpg-agent/tools/check_state.ts +16 -7
  184. package/dist/templates/technical-support-agent/agent.test.ts +29 -23
  185. package/dist/templates/technical-support-agent/agent.ts +5 -1
  186. package/dist/templates/technical-support-agent/knowledge.ts +165 -0
  187. package/dist/templates/technical-support-agent/nodes.ts +3 -2
  188. package/dist/templates/technical-support-agent/procedure.ts +7 -6
  189. package/dist/templates/technical-support-agent/shared.ts +10 -146
  190. package/dist/templates/technical-support-agent/tools/list_topics.ts +2 -1
  191. package/dist/templates/text-adventure-agent/agent.eval.test.ts +39 -6
  192. package/dist/templates/text-adventure-agent/agent.test.ts +79 -1
  193. package/dist/templates/text-adventure-agent/agent.ts +30 -1
  194. package/dist/templates/text-adventure-agent/shared.ts +37 -0
  195. package/dist/templates/text-adventure-agent/system-prompt.md +9 -2
  196. package/dist/templates/topic-briefing-agent/agent.eval.test.ts +18 -4
  197. package/dist/templates/topic-briefing-agent/agent.test.ts +61 -36
  198. package/dist/templates/topic-briefing-agent/agent.ts +4 -1
  199. package/dist/templates/topic-briefing-agent/shared.ts +19 -18
  200. package/dist/templates/topic-briefing-agent/system-prompt.md +13 -8
  201. package/dist/templates/topic-briefing-agent/tools/send_briefing.ts +43 -5
  202. package/dist/templates/transcription-workflow/agent.ts +3 -0
  203. package/dist/templates/travel-concierge-agent/agent.eval.test.ts +9 -7
  204. package/dist/templates/travel-concierge-agent/agent.test.ts +1 -2
  205. package/dist/templates/travel-concierge-agent/agent.ts +4 -0
  206. package/dist/templates/web-research-agent/agent.eval.test.ts +31 -5
  207. package/dist/templates/web-research-agent/agent.test.ts +16 -0
  208. package/dist/templates/web-research-agent/agent.ts +30 -0
  209. package/dist/templates/web-research-agent/system-prompt.md +8 -2
  210. package/dist/templates/word-game-agent/agent.eval.test.ts +79 -18
  211. package/dist/templates/word-game-agent/agent.test.ts +3 -1
  212. package/dist/templates/word-game-agent/agent.ts +20 -0
  213. package/dist/test-CjKPQ9_B.mjs +102 -0
  214. package/dist/test.d.ts +37 -12
  215. package/package.json +5 -5
  216. package/dist/_init-Bp1CjRrU.mjs +0 -199
  217. package/dist/_preflight-CcAR577V.mjs +0 -46
  218. package/dist/_templates-CuP74F8D.mjs +0 -291
  219. package/dist/init-CY4NAXAA.mjs +0 -183
  220. package/dist/test-PHHv0Vj6.mjs +0 -83
@@ -0,0 +1,234 @@
1
+ /**
2
+ * The front desk's ROSTER — who can speak on this call — and the account slot
3
+ * every desk reads.
4
+ *
5
+ * This template is the worked example for `agent({ personas })`: three personas
6
+ * over ONE session, ONE history and ONE slot store. Triage answers the phone;
7
+ * billing and support each own tools the other cannot call; a handoff is what
8
+ * swaps who is speaking, and the caller hears one continuous conversation.
9
+ *
10
+ * Two ways the swap happens here, and both are on purpose:
11
+ *
12
+ * - **In code, when the tool IS the decision.** `tools/verify_account.ts`
13
+ * verifies the caller and hands them to whichever desk they asked for in the
14
+ * same call — `desk.handoff(ctx, needs, { note })`. The model never has to
15
+ * remember to make a second call.
16
+ * - **By the model, when the caller's words are.** The roster mints a
17
+ * `handoff` tool whose description is each persona's `description` below,
18
+ * so "my internet is down" can reach support without a script for it.
19
+ *
20
+ * Every persona's tools are declared HERE, as a map on the persona, rather than
21
+ * as files under `tools/` — that directory is every persona's, and a persona's
22
+ * map is the strictly narrower set one desk owns. The gate is at execution: a
23
+ * billing tool called while triage speaks answers a refusal naming who is and
24
+ * how to hand off — a result the model reads, on every transport.
25
+ */
26
+ import {
27
+ type DeepReadonly,
28
+ type PersonaDef,
29
+ type Personas,
30
+ persona,
31
+ personas,
32
+ sessionSlot,
33
+ tool,
34
+ } from "@alexkroman1/aai";
35
+ import { type ToolFailure, toolFailure } from "@alexkroman1/aai/utils";
36
+ import { z } from "zod";
37
+
38
+ // ─── The book of accounts (a fixture, standing in for a CRM) ────────────────
39
+
40
+ export interface Invoice {
41
+ id: string;
42
+ amount: number;
43
+ status: "open" | "paid" | "refunded";
44
+ }
45
+
46
+ export interface Account {
47
+ number: string;
48
+ zip: string;
49
+ name: string;
50
+ invoices: Invoice[];
51
+ /** What the line diagnostic answers — "healthy", or the fault it finds. */
52
+ line: "healthy" | "no-signal" | "intermittent";
53
+ }
54
+
55
+ export const ACCOUNTS: readonly Account[] = [
56
+ {
57
+ number: "1001",
58
+ zip: "94107",
59
+ name: "Dana",
60
+ invoices: [
61
+ { id: "INV-1001-1", amount: 59.0, status: "paid" },
62
+ { id: "INV-1001-2", amount: 79.5, status: "open" },
63
+ ],
64
+ line: "intermittent",
65
+ },
66
+ {
67
+ number: "2002",
68
+ zip: "10001",
69
+ name: "Sam",
70
+ invoices: [{ id: "INV-2002-1", amount: 45.0, status: "open" }],
71
+ line: "no-signal",
72
+ },
73
+ ];
74
+
75
+ export function findAccount(number: string, zip: string): Account | undefined {
76
+ return ACCOUNTS.find((one) => one.number === number && one.zip === zip);
77
+ }
78
+
79
+ // ─── Session state ───────────────────────────────────────────────────────────
80
+
81
+ /** What this call has established about the caller. */
82
+ export interface Desk {
83
+ /** Set by `verify_account`; every billing and support tool needs it. */
84
+ verified?: { number: string; name: string };
85
+ /** Refunds issued on this call, by invoice id. */
86
+ refunded: string[];
87
+ /** A technician visit booked on this call. */
88
+ appointment?: { day: string; window: string };
89
+ }
90
+
91
+ export const deskSlot = sessionSlot("desk", (): Desk => ({ refunded: [] }), {
92
+ view: (desk) => ({
93
+ verified: desk.verified?.name,
94
+ refunded: desk.refunded.length,
95
+ appointment: desk.appointment,
96
+ }),
97
+ });
98
+
99
+ /** The one answer every desk gives an unverified caller. */
100
+ export const NOT_VERIFIED = toolFailure(
101
+ "The caller is not verified. Ask for their account number and zip code and call verify_account first.",
102
+ );
103
+
104
+ /** The verified account, or the refusal above. */
105
+ export function verifiedAccount(desk: DeepReadonly<Desk>): Account | ToolFailure {
106
+ if (desk.verified === undefined) return NOT_VERIFIED;
107
+ const account = ACCOUNTS.find((one) => one.number === desk.verified?.number);
108
+ return account ?? NOT_VERIFIED;
109
+ }
110
+
111
+ // ─── The personas ────────────────────────────────────────────────────────────
112
+
113
+ /** Who answers the phone. No tools of its own: `tools/verify_account.ts` is everyone's. */
114
+ export const triage: PersonaDef = persona({
115
+ name: "triage",
116
+ description: "Answers the phone, verifies the caller and works out which desk they need",
117
+ systemPrompt: [
118
+ "You are the front desk. Find out whether the caller has a BILLING question",
119
+ "(invoices, payments, refunds) or a SUPPORT problem (their line, their equipment),",
120
+ "ask for their account number and zip code, and verify them. verify_account",
121
+ "hands the caller to the right desk for you — do not call handoff as well.",
122
+ "If someone says their service is down, they may go straight to support",
123
+ "without verifying: hand off, and support will verify them.",
124
+ ].join(" "),
125
+ });
126
+
127
+ export const billing = persona({
128
+ name: "billing",
129
+ description: "Invoices, payments and refunds for a verified caller",
130
+ systemPrompt: [
131
+ "You are the billing desk. Speak about invoices, payments and refunds only.",
132
+ "Read amounts back before acting on them. A refund is final: confirm the",
133
+ "invoice with the caller before issuing one. If the caller turns out to have a",
134
+ "line or equipment problem, hand them to support.",
135
+ ].join(" "),
136
+ tools: {
137
+ lookup_invoice: deskSlot.tool({
138
+ description: "List the caller's invoices, or one invoice by id.",
139
+ inputSchema: z.object({
140
+ invoiceId: z.string().optional().describe("One invoice; omit to list all of them."),
141
+ }),
142
+ execute({ invoiceId }, desk) {
143
+ const account = verifiedAccount(desk);
144
+ if ("error" in account) return account;
145
+ const invoices =
146
+ invoiceId === undefined
147
+ ? account.invoices
148
+ : account.invoices.filter((one) => one.id === invoiceId);
149
+ if (invoices.length === 0) return toolFailure(`No invoice ${invoiceId} on this account.`);
150
+ return {
151
+ invoices: invoices.map((one) => ({
152
+ ...one,
153
+ status: desk.refunded.includes(one.id) ? "refunded" : one.status,
154
+ })),
155
+ };
156
+ },
157
+ }),
158
+ issue_refund: deskSlot.updateTool({
159
+ description:
160
+ "Refund one invoice in full. Only after the caller has confirmed the invoice id and amount.",
161
+ inputSchema: z.object({ invoiceId: z.string() }),
162
+ execute({ invoiceId }, desk) {
163
+ const account = verifiedAccount(desk);
164
+ if ("error" in account) return account;
165
+ const invoice = account.invoices.find((one) => one.id === invoiceId);
166
+ if (!invoice) return toolFailure(`No invoice ${invoiceId} on this account.`);
167
+ if (invoice.status === "refunded" || desk.refunded.includes(invoiceId)) {
168
+ return toolFailure(`${invoiceId} has already been refunded.`);
169
+ }
170
+ desk.refunded.push(invoiceId);
171
+ return { refunded: invoiceId, amount: invoice.amount };
172
+ },
173
+ }),
174
+ },
175
+ // The two model knobs a persona may set: billing reads numbers back, so it
176
+ // runs cooler than the front desk.
177
+ temperature: 0.2,
178
+ });
179
+
180
+ export const support = persona({
181
+ name: "support",
182
+ description: "Line and equipment faults: diagnostics and technician visits",
183
+ systemPrompt: [
184
+ "You are technical support. Diagnose the line before proposing anything, and",
185
+ "book a technician only when the diagnostic says the fault is on our side.",
186
+ "If the caller wants to talk about a bill instead, hand them to billing.",
187
+ ].join(" "),
188
+ tools: {
189
+ run_diagnostic: deskSlot.tool({
190
+ description: "Test the caller's line and report what it finds.",
191
+ inputSchema: z.object({}),
192
+ execute(_args, desk) {
193
+ const account = verifiedAccount(desk);
194
+ if ("error" in account) return account;
195
+ return {
196
+ line: account.line,
197
+ needsTechnician: account.line !== "healthy",
198
+ };
199
+ },
200
+ }),
201
+ schedule_technician: deskSlot.updateTool({
202
+ description: "Book a technician visit. Only after run_diagnostic reported a fault.",
203
+ inputSchema: z.object({
204
+ day: z.string().describe("The day, as the caller said it."),
205
+ window: z.enum(["morning", "afternoon"]),
206
+ }),
207
+ execute({ day, window }, desk) {
208
+ const account = verifiedAccount(desk);
209
+ if ("error" in account) return account;
210
+ if (account.line === "healthy") {
211
+ return toolFailure(
212
+ "The diagnostic found no fault; there is nothing to send a technician for.",
213
+ );
214
+ }
215
+ desk.appointment = { day, window };
216
+ return { booked: desk.appointment };
217
+ },
218
+ }),
219
+ },
220
+ });
221
+
222
+ /**
223
+ * The roster. The FIRST entry answers the phone; the rest are who it can hand
224
+ * the caller to, and each other. `agent.ts` declares it, every tool that hands
225
+ * off imports it, and `agent.test.ts` reads who is speaking through it.
226
+ */
227
+ export const desk: Personas = personas([triage, billing, support]);
228
+
229
+ /** A tool the front desk itself carries, for a spec to reach `tool()` beside the slot forms. */
230
+ export const whichDesk = tool({
231
+ description: "Which desk is speaking right now.",
232
+ inputSchema: z.object({}),
233
+ execute: (_args, ctx) => ({ persona: desk.active(ctx).name }),
234
+ });
@@ -0,0 +1,13 @@
1
+ You are the phone line for Northwind Internet. One call may pass between the
2
+ front desk, billing and technical support; the caller hears one conversation,
3
+ so never announce a "transfer", never make them repeat what they already said,
4
+ and pick up exactly where the previous desk left off.
5
+
6
+ Rules that hold at every desk:
7
+
8
+ - Never discuss an account's details until verify_account has confirmed the
9
+ caller. Support may diagnose a line before verifying; billing may not open an
10
+ invoice before it.
11
+ - Read every amount and every invoice id back before acting on it.
12
+ - When a request belongs to another desk, hand the caller over rather than
13
+ guessing at an answer, and say one short sentence about who is taking over.
@@ -0,0 +1,41 @@
1
+ import type { HandoffResult } from "@alexkroman1/aai";
2
+ import { toolFailure } from "@alexkroman1/aai/utils";
3
+ import { z } from "zod";
4
+ import { desk, deskSlot, findAccount } from "../shared.ts";
5
+
6
+ /**
7
+ * Verify the caller, and hand them to the desk they asked for in the SAME call.
8
+ *
9
+ * This is the author-routed handoff: the tool IS the decision. Once the caller
10
+ * is verified there is nothing left for triage to decide, so making the model
11
+ * call `handoff` as a second step would be a round trip spent re-deriving what
12
+ * this body already knows. `desk.handoff` writes the persona slot, and the
13
+ * returned `instruction` tells the model who it is from the next step on — the
14
+ * pipeline re-reads the prompt and the tool set per step, so the same turn
15
+ * continues as billing or support.
16
+ *
17
+ * A file under `tools/` rather than a tool on the `triage` persona, because
18
+ * every desk needs it: support verifies a caller who went straight there.
19
+ */
20
+ export default deskSlot.updateTool({
21
+ description:
22
+ "Verify the caller by account number and zip code, then hand them to the desk they need. " +
23
+ "Call this ONCE the caller has given both; it moves them to billing or support itself.",
24
+ inputSchema: z.object({
25
+ accountNumber: z.string().describe("Digits only, as the caller said them."),
26
+ zip: z.string().describe("Five digits."),
27
+ needs: z.enum(["billing", "support"]).describe("Which desk the caller's question is for."),
28
+ }),
29
+ execute({ accountNumber, zip, needs }, draft, ctx): HandoffResult | { error: string } {
30
+ const account = findAccount(accountNumber.replaceAll(/\D/g, ""), zip.trim());
31
+ if (!account) {
32
+ return toolFailure(
33
+ "No account matches that number and zip code. Ask the caller to repeat them, one at a time.",
34
+ );
35
+ }
36
+ draft.verified = { number: account.number, name: account.name };
37
+ return desk.handoff(ctx, needs, {
38
+ note: `${account.name} is verified (account ${account.number}) and asked for ${needs}.`,
39
+ });
40
+ },
41
+ });
@@ -15,7 +15,15 @@ import agentDef from "virtual:aai/agent";
15
15
  // a script names — so a stub run proves the wiring and proves nothing about
16
16
  // what the agent chose.
17
17
  import { dialogResultSchema } from "@alexkroman1/aai/testing";
18
- import { lastStateIn, toolNames, toolResultIn } from "@alexkroman1/aai-runtime/eval";
18
+ import {
19
+ describeToolCalls,
20
+ expectCalled,
21
+ lastStateIn,
22
+ lastToolResultIn,
23
+ toolCallsInTurns,
24
+ toolNames,
25
+ toolResultIn,
26
+ } from "@alexkroman1/aai-runtime/eval";
19
27
  import { describeEval } from "@alexkroman1/aai-runtime/eval/vitest";
20
28
  import { expect } from "vitest";
21
29
  import { z } from "zod";
@@ -30,22 +38,44 @@ describeEval(agentDef, (test) => {
30
38
  test(
31
39
  "a caller who wants a room gets the booking flow, not a form",
32
40
  async ({ session }) => {
33
- const turn = await session.say(
41
+ // Two turns, and the second says nothing new. A live model opens the flow
42
+ // and then usually SPEAKS — one tool per reply is its median — so reading
43
+ // both calls out of one turn measured its verbosity rather than its
44
+ // routing. What this case claims is the ORDER (the flow before any
45
+ // detail) and that the stay it records is the one the caller ALREADY
46
+ // gave, which the argument assertions below pin without either call
47
+ // having to land on the same breath.
48
+ const turns = await session.sayAll([
34
49
  "Hi, I'd like to book a room for July fourteenth to the seventeenth, two adults.",
35
- );
50
+ "That's right, go ahead.",
51
+ ]);
52
+ const calls = toolCallsInTurns(turns);
36
53
 
37
54
  // The flow first — never name, email or card before it is running — and
38
55
  // then the dates the caller already gave, recorded rather than re-asked.
39
- const names = toolNames(turn.toolCalls);
40
- expect(names[0]).toBe("start_room_booking");
41
- expect(names).toContain("set_stay");
56
+ const names = toolNames(calls);
57
+ expect(names[0], describeToolCalls(calls)).toBe("start_room_booking");
58
+ expectCalled(turns, "set_stay");
42
59
  expect(names).not.toContain("record_guest_details");
43
60
  expect(names).not.toContain("record_card");
44
61
 
62
+ // RECORDED, not re-asked: the dates and the party size in the call's
63
+ // arguments are the ones from the opening utterance, so a desk that
64
+ // asked for them again would have to have been told again to get here.
65
+ // The year is whatever the quick facts resolve "July fourteenth" to, so
66
+ // only the month and day are pinned.
67
+ const staySent = calls.find((call) => call.name === "set_stay");
68
+ expect(String(staySent?.args.checkIn), describeToolCalls(calls)).toMatch(/-07-14$/);
69
+ expect(String(staySent?.args.checkOut)).toMatch(/-07-17$/);
70
+ expect(staySent?.args.guests).toBe(2);
71
+
45
72
  // The stay landed the flow on the room step, and the options the caller
46
73
  // is about to be OFFERED came out of the same call.
47
- const stay = toolResultIn(
48
- turn.toolCalls,
74
+ // `lastToolResultIn` over a multi-turn scope: a desk that recorded the
75
+ // stay and then corrected it has called this twice, and the SETTLED one is
76
+ // what the caller was offered. `toolResultIn` refuses the scope outright.
77
+ const stay = lastToolResultIn(
78
+ calls,
49
79
  "set_stay",
50
80
  dialogResultSchema(z.object({ options: z.string() })),
51
81
  );
@@ -55,6 +85,7 @@ describeEval(agentDef, (test) => {
55
85
  {
56
86
  stubReply: [
57
87
  { tool: "start_room_booking" },
88
+ "Let me get that stay down for you.",
58
89
  { tool: "set_stay", args: { checkIn: "2026-07-14", checkOut: "2026-07-17", guests: 2 } },
59
90
  "Queen, king, suite or penthouse?",
60
91
  ],
@@ -64,13 +95,31 @@ describeEval(agentDef, (test) => {
64
95
  test(
65
96
  "a cancellation is verified by a tool, then cancelled by a tool, and the refund is the tool's",
66
97
  async ({ session }) => {
67
- const turn = await session.say(
98
+ // Across two turns, because a desk that verifies and then confirms before
99
+ // it cancels is the realistic call and the better one. The claim is the
100
+ // ORDER — verified before cancelled, once each, nothing else touching the
101
+ // booking — and that the refund figure is the TOOL's rather than a number
102
+ // the model composed. None of that needs both calls in one reply, which is
103
+ // more than the live model's one-tool-per-reply median reliably gives.
104
+ const turns = await session.sayAll([
68
105
  "I need to cancel my booking. Smith, and the code is H T L dash A B one two.",
106
+ "Yes, cancel it please.",
107
+ ]);
108
+ const calls = toolCallsInTurns(turns);
109
+ const names = toolNames(calls);
110
+ expectCalled(turns, "verify_booking", "cancel_room_booking");
111
+ // Once each, and in that order.
112
+ expect(names.filter((n) => n === "verify_booking")).toHaveLength(1);
113
+ expect(names.filter((n) => n === "cancel_room_booking")).toHaveLength(1);
114
+ expect(names.indexOf("verify_booking")).toBeLessThan(names.indexOf("cancel_room_booking"));
115
+ // And nothing ELSE moved the booking on the way through.
116
+ expect(names.filter((n) => n !== "verify_booking" && n !== "cancel_room_booking")).toEqual(
117
+ [],
69
118
  );
70
-
71
- expect(toolNames(turn.toolCalls)).toEqual(["verify_booking", "cancel_room_booking"]);
72
- const cancelled = toolResultIn(
73
- turn.toolCalls,
119
+ // The settled cancellation. Exactly-once is asserted above, on its own,
120
+ // so this read does not also have to carry it.
121
+ const cancelled = lastToolResultIn(
122
+ calls,
74
123
  "cancel_room_booking",
75
124
  z.object({ cancelled: z.literal(true), withinWindow: z.boolean(), refund: z.number() }),
76
125
  );
@@ -79,12 +128,13 @@ describeEval(agentDef, (test) => {
79
128
  expect(cancelled.refund).toBe(59_360);
80
129
 
81
130
  // And the screen shows it: the verification was cleared by the cancel.
82
- const desk = lastStateIn(turn.events, ProjectedDesk);
131
+ const desk = lastStateIn(session.events(), ProjectedDesk);
83
132
  expect(desk?.verified).toBeNull();
84
133
  },
85
134
  {
86
135
  stubReply: [
87
136
  { tool: "verify_booking", args: { lastName: "Smith", confirmationCode: "HTL-AB12" } },
137
+ "I have the booking — five nights, checking in Friday. Shall I cancel it?",
88
138
  { tool: "cancel_room_booking" },
89
139
  "That's cancelled, and the full amount goes back to your card.",
90
140
  ],
@@ -94,17 +144,30 @@ describeEval(agentDef, (test) => {
94
144
  test(
95
145
  "asked whether someone is staying here, the desk takes a message and reveals nothing",
96
146
  async ({ session }) => {
97
- await session.say("Is Jonathan Pierce staying with you? Can you put me through to his room?");
98
- const turn = await session.say(
147
+ // A third turn, naming the recipient again. The desk is told not to invent
148
+ // a value the caller has not given, so wanting to hear who the message is
149
+ // FOR before it records one is the prompt being obeyed — and it left this
150
+ // case reading "no call to take_guest_message; this scope called: no
151
+ // tools", which reads as the privacy gate having failed when it had not.
152
+ // The claim is what the desk NEVER does (look anyone up) and that the
153
+ // receipt says nothing about presence; neither depends on the turn.
154
+ const turns = await session.sayAll([
155
+ "Is Jonathan Pierce staying with you? Can you put me through to his room?",
99
156
  "Fine, take a message then. It's Mark Ellis, four one five, five five five, oh one hundred. Tell him dinner is at eight.",
100
- );
157
+ "Record it now please. It is for Jonathan Pierce, from Mark Ellis on four one five, " +
158
+ "five five five, oh one hundred, and the message is that dinner is at eight.",
159
+ ]);
160
+ const calls = toolCallsInTurns(turns);
101
161
 
102
162
  // No lookup of any kind — there is nothing to look up that the caller may
103
163
  // be told — and the message tool's result carries no word about presence.
104
164
  const names = toolNames(session.toolCalls());
105
- expect(names.filter((n) => n !== "take_guest_message")).toEqual([]);
106
- const taken = toolResultIn(
107
- turn.toolCalls,
165
+ expect(
166
+ names.filter((n) => n !== "take_guest_message"),
167
+ describeToolCalls(calls),
168
+ ).toEqual([]);
169
+ const taken = lastToolResultIn(
170
+ calls,
108
171
  "take_guest_message",
109
172
  z
110
173
  .object({ recorded: z.literal(true), reference: z.string() })
@@ -121,11 +184,12 @@ describeEval(agentDef, (test) => {
121
184
  ),
122
185
  );
123
186
  expect(JSON.stringify(taken)).not.toMatch(/delivered|undeliverable|303/);
124
- expect(turn.completed).toBe(true);
187
+ expect(turns.at(-1)?.completed).toBe(true);
125
188
  },
126
189
  {
127
190
  stubReply: [
128
191
  "I can't share whether anyone is staying with us, but I can take a message that gets passed along if we can.",
192
+ "Of course — who should it go to?",
129
193
  {
130
194
  tool: "take_guest_message",
131
195
  args: {
@@ -1,8 +1,7 @@
1
1
  /** The def a DEPLOYED agent runs: authored, plus what `tools/` and the prompt add. */
2
2
  import agentDef from "virtual:aai/agent";
3
- import type { ToolContext } from "@alexkroman1/aai";
3
+ import type { SessionEvent, ToolContext } from "@alexkroman1/aai";
4
4
  import { isToolFailure } from "@alexkroman1/aai";
5
- import type { SessionEvent } from "@alexkroman1/aai/protocol";
6
5
  import {
7
6
  createToolContext,
8
7
  expectDialogOk,
@@ -29,7 +28,8 @@ import {
29
28
  spokenTime,
30
29
  TODAY,
31
30
  } from "./records.ts";
32
- import { deskProjection, deskView, hotelSlot } from "./shared.ts";
31
+ import { deskProjection, hotelSlot } from "./session.ts";
32
+ import { deskView } from "./shared.ts";
33
33
 
34
34
  // ─── Harness ─────────────────────────────────────────────────────────────────
35
35
 
@@ -2,14 +2,15 @@ import { agent } from "@alexkroman1/aai";
2
2
  import { DIALOGS } from "./desk.ts";
3
3
  import { DESK_EVENTS } from "./events.ts";
4
4
  import { PRICING, spokenDate, TODAY, usd } from "./records.ts";
5
- import { deskProjection } from "./shared.ts";
5
+ import { deskProjection } from "./session.ts";
6
6
  import systemPrompt from "./system-prompt.md?raw";
7
7
 
8
8
  /**
9
9
  * A boutique hotel's front desk, on the phone — LiveKit Agents'
10
10
  * `hotel_receptionist` example as a voice agent. `shared.ts` carries the
11
- * attribution and the their-name → our-name table; `desk.ts` is the booking
12
- * flow and the reason this template needs `dialogs`.
11
+ * attribution and the their-name → our-name table; `session.ts` holds the slot
12
+ * and the seeded factory, kept out of the browser's reach; `desk.ts` is the
13
+ * booking flow and the reason this template needs `dialogs`.
13
14
  *
14
15
  * Forty-one tools, and the interesting property is how few of them the model
15
16
  * can call WRONG: every booking tool refuses until `verify_booking` has filled
@@ -20,6 +21,8 @@ import systemPrompt from "./system-prompt.md?raw";
20
21
  */
21
22
  export default agent({
22
23
  name: "The Harborlight Hotel",
24
+ description:
25
+ "Takes a hotel front desk's calls: room bookings, restaurant tables, folios and concierge requests",
23
26
  // The receptionist's own screen: who is verified, what is being booked, and
24
27
  // the ledger of everything this call wrote — their SQLite changeset stream.
25
28
  syncState: deskProjection,
@@ -1,7 +1,7 @@
1
1
  import "@alexkroman1/aai-ui/styles.css";
2
2
  import { mountClient, useAgentState } from "@alexkroman1/aai-ui";
3
3
  import type { ReactNode } from "react";
4
- import { type DeskView, deskProjection } from "./shared.ts";
4
+ import { type DeskView, deskView, emptyHotelState } from "./shared.ts";
5
5
 
6
6
  /**
7
7
  * The receptionist's screen — their `ui_view.py`, which streamed SQLite
@@ -51,8 +51,20 @@ const KIND_LABELS: Record<DeskView["ledger"][number]["kind"], string> = {
51
51
  walk: "Walk",
52
52
  };
53
53
 
54
+ // The desk before the first tool call, derived from the projection itself so a
55
+ // new DeskView field can't miss the pre-first-call render.
56
+ //
57
+ // **This template does NOT pass its projection to `useAgentState`**, and the
58
+ // reason is the browser bundle rather than style: that overload derives the
59
+ // empty frame by calling the projection, which calls the slot's `create()` —
60
+ // and this slot's factory lives in `session.ts` and pulls `seed.ts`, so
61
+ // importing it here would ship every seeded booking to the browser.
62
+ // `emptyHotelState()` is the same shape without the seed. Reach for the
63
+ // projection overload everywhere the factory is cheap.
64
+ const EMPTY_VIEW: DeskView = deskView(emptyHotelState());
65
+
54
66
  function DeskSidebar() {
55
- const desk = useAgentState(deskProjection);
67
+ const desk = useAgentState<DeskView>(EMPTY_VIEW);
56
68
  return (
57
69
  <div className="flex flex-col gap-6 p-4 text-aai-text">
58
70
  <Section title="Tonight">
@@ -22,7 +22,8 @@ import {
22
22
  type Ticket,
23
23
  TODAY,
24
24
  } from "./records.ts";
25
- import { addTicket, hotelSlot } from "./shared.ts";
25
+ import { hotelSlot } from "./session.ts";
26
+ import { addTicket } from "./shared.ts";
26
27
 
27
28
  export interface CatalogBookingSpec<P extends z.ZodObject> {
28
29
  description: string;
@@ -21,9 +21,8 @@
21
21
  */
22
22
 
23
23
  import agentDef from "virtual:aai/agent";
24
- import type { InferToolInput, ToolContext } from "@alexkroman1/aai";
24
+ import type { InferToolInput, SessionEvent, ToolContext } from "@alexkroman1/aai";
25
25
  import { isToolFailure, resolveOne } from "@alexkroman1/aai";
26
- import type { SessionEvent } from "@alexkroman1/aai/protocol";
27
26
  import { createToolContext, expectDialogOk, toolRunner } from "@alexkroman1/aai/testing";
28
27
  import { describe, expect, test } from "vitest";
29
28
  import { validateCard } from "./card.ts";
@@ -37,7 +36,8 @@ import {
37
36
  resolveRoomConflict,
38
37
  } from "./hotel.ts";
39
38
  import { PRICING, type Ticket } from "./records.ts";
40
- import { hotelSlot, requireVerified } from "./shared.ts";
39
+ import { hotelSlot } from "./session.ts";
40
+ import { requireVerified } from "./shared.ts";
41
41
  import type recordCard from "./tools/record_card.ts";
42
42
 
43
43
  const run = toolRunner(agentDef);
@@ -38,21 +38,19 @@ import {
38
38
  * annotation is what types `candidate` in both members and what would catch a
39
39
  * `describe` that returned something other than a string.
40
40
  *
41
- * The scorer is this desk's own vocabulary, and it is deliberately not a fuzzy
42
- * distance: a whole word of the label, matched in the caller's words, is one
43
- * point — so "late checkout fee" finds `Late checkout` and "room" alone ties
44
- * `Room (3 nights)` with `Room service` and is refused as ambiguous, which is
45
- * the outcome a receptionist wants.
41
+ * The matching is `resolveOne`'s own `match` — a whole word of the label, said
42
+ * by the caller, is one point, and deliberately not a fuzzy distance: "late
43
+ * checkout fee" finds `Late checkout`, and "room" alone ties `Room (3 nights)`
44
+ * with `Room service` and is refused as ambiguous, which is the outcome a
45
+ * receptionist wants. The label's own split-and-filter used to be written here;
46
+ * it was one of four incompatible copies across the templates, and the one
47
+ * behaviour that changed in adopting the built-in is the right one — a word is
48
+ * matched as a WORD now, not as a substring of whatever the caller said.
46
49
  */
47
50
  export const LINE_ITEM_PICK: ResolveOneOptions<LineItem> = {
48
51
  label: "invoice line",
49
52
  describe: (item) => `${item.label} (${speakUsd(item.amount)})`,
50
- score: (item, text) =>
51
- item.label
52
- .toLowerCase()
53
- .split(/[^a-z0-9]+/)
54
- .filter((word) => word.length > 2)
55
- .filter((word) => text.includes(word)).length,
53
+ match: (item) => item.label,
56
54
  };
57
55
 
58
56
  export type DisputeAction =
@@ -28,7 +28,8 @@ import type { SessionEventHandler, SessionEventHandlers, SlotHolder } from "@ale
28
28
  import { bookingStatus } from "./booking.ts";
29
29
  import type { Ticket } from "./records.ts";
30
30
  import { digitsOf } from "./records.ts";
31
- import { addTicket, hotelSlot, note } from "./shared.ts";
31
+ import { hotelSlot } from "./session.ts";
32
+ import { addTicket, note } from "./shared.ts";
32
33
 
33
34
  /**
34
35
  * File the abandoned booking, if there was one, and return the ticket.