vellum 0.2.1 → 0.2.2

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 (349) hide show
  1. package/README.md +15 -2
  2. package/bun.lock +5 -2
  3. package/package.json +4 -2
  4. package/scripts/capture-x-graphql.ts +562 -0
  5. package/scripts/ipc/check-swift-decoder-drift.ts +2 -1
  6. package/scripts/test.sh +5 -0
  7. package/src/__tests__/__snapshots__/ipc-snapshot.test.ts.snap +133 -34
  8. package/src/__tests__/account-registry.test.ts +2 -1
  9. package/src/__tests__/agent-heartbeat-service.test.ts +250 -0
  10. package/src/__tests__/asset-materialize-tool.test.ts +16 -15
  11. package/src/__tests__/asset-search-tool.test.ts +23 -22
  12. package/src/__tests__/attachments-store.test.ts +56 -127
  13. package/src/__tests__/browser-skill-baseline-tool-payload.test.ts +5 -4
  14. package/src/__tests__/browser-skill-endstate.test.ts +4 -3
  15. package/src/__tests__/call-bridge.test.ts +385 -0
  16. package/src/__tests__/call-constants.test.ts +40 -0
  17. package/src/__tests__/call-orchestrator.test.ts +130 -4
  18. package/src/__tests__/call-recovery.test.ts +518 -0
  19. package/src/__tests__/call-routes-http.test.ts +459 -0
  20. package/src/__tests__/call-state-machine.test.ts +143 -0
  21. package/src/__tests__/call-store.test.ts +216 -1
  22. package/src/__tests__/cli-discover.test.ts +1 -1
  23. package/src/__tests__/commit-message-enrichment-service.test.ts +148 -7
  24. package/src/__tests__/compaction.benchmark.test.ts +176 -0
  25. package/src/__tests__/computer-use-tools.test.ts +250 -0
  26. package/src/__tests__/config-schema.test.ts +299 -3
  27. package/src/__tests__/conflict-store.test.ts +2 -1
  28. package/src/__tests__/contacts-tools.test.ts +331 -0
  29. package/src/__tests__/conversation-store.test.ts +30 -32
  30. package/src/__tests__/credential-security-invariants.test.ts +4 -0
  31. package/src/__tests__/date-context.test.ts +373 -0
  32. package/src/__tests__/db-schedule-syntax-migration.test.ts +129 -0
  33. package/src/__tests__/fixtures/media-reuse-fixtures.ts +3 -3
  34. package/src/__tests__/followup-tools.test.ts +303 -0
  35. package/src/__tests__/handlers-twitter-config.test.ts +718 -0
  36. package/src/__tests__/intent-routing.test.ts +64 -57
  37. package/src/__tests__/ipc-roundtrip.benchmark.test.ts +237 -0
  38. package/src/__tests__/ipc-snapshot.test.ts +62 -28
  39. package/src/__tests__/llm-usage-store.test.ts +3 -8
  40. package/src/__tests__/media-generate-image.test.ts +1 -1
  41. package/src/__tests__/media-reuse-story.e2e.test.ts +7 -7
  42. package/src/__tests__/memory-retrieval.benchmark.test.ts +430 -0
  43. package/src/__tests__/parallel-tool.benchmark.test.ts +294 -0
  44. package/src/__tests__/playbook-tools.test.ts +342 -0
  45. package/src/__tests__/profile-compiler.test.ts +2 -1
  46. package/src/__tests__/provider-streaming.benchmark.test.ts +773 -0
  47. package/src/__tests__/recurrence-engine-rruleset.test.ts +78 -0
  48. package/src/__tests__/recurrence-engine.test.ts +69 -0
  49. package/src/__tests__/recurrence-types.test.ts +71 -0
  50. package/src/__tests__/registry.test.ts +5 -3
  51. package/src/__tests__/relay-server.test.ts +633 -0
  52. package/src/__tests__/reminder-store.test.ts +6 -3
  53. package/src/__tests__/reminder.test.ts +43 -77
  54. package/src/__tests__/run-orchestrator-assistant-events.test.ts +8 -4
  55. package/src/__tests__/run-orchestrator.test.ts +4 -4
  56. package/src/__tests__/runtime-attachment-metadata.test.ts +7 -6
  57. package/src/__tests__/runtime-runs-http.test.ts +4 -4
  58. package/src/__tests__/runtime-runs.test.ts +4 -4
  59. package/src/__tests__/schedule-store.test.ts +482 -0
  60. package/src/__tests__/schedule-tools.test.ts +700 -0
  61. package/src/__tests__/scheduler-recurrence.test.ts +329 -0
  62. package/src/__tests__/server-history-render.test.ts +14 -13
  63. package/src/__tests__/session-error.test.ts +28 -0
  64. package/src/__tests__/session-init.benchmark.test.ts +462 -0
  65. package/src/__tests__/session-queue.test.ts +71 -48
  66. package/src/__tests__/session-runtime-assembly.test.ts +161 -0
  67. package/src/__tests__/session-surfaces-task-progress.test.ts +104 -0
  68. package/src/__tests__/signup-e2e.test.ts +2 -1
  69. package/src/__tests__/skill-projection.benchmark.test.ts +328 -0
  70. package/src/__tests__/skill-script-runner.test.ts +159 -0
  71. package/src/__tests__/speaker-identification.test.ts +52 -0
  72. package/src/__tests__/subagent-manager-notify.test.ts +42 -10
  73. package/src/__tests__/subagent-tools.test.ts +141 -41
  74. package/src/__tests__/task-compiler.test.ts +2 -1
  75. package/src/__tests__/task-runner.test.ts +2 -1
  76. package/src/__tests__/task-scheduler.test.ts +2 -1
  77. package/src/__tests__/task-tools.test.ts +49 -56
  78. package/src/__tests__/tool-audit-listener.test.ts +1 -0
  79. package/src/__tests__/tool-domain-event-publisher.test.ts +2 -0
  80. package/src/__tests__/tool-execution-pipeline.benchmark.test.ts +500 -0
  81. package/src/__tests__/tool-executor.test.ts +13 -17
  82. package/src/__tests__/turn-commit.test.ts +218 -3
  83. package/src/__tests__/twilio-provider.test.ts +143 -0
  84. package/src/__tests__/twilio-routes.test.ts +789 -0
  85. package/src/__tests__/twitter-auth-handler.test.ts +581 -0
  86. package/src/__tests__/view-image-tool.test.ts +217 -0
  87. package/src/__tests__/workspace-git-service.test.ts +186 -0
  88. package/src/__tests__/workspace-heartbeat-service.test.ts +13 -3
  89. package/src/agent-heartbeat/agent-heartbeat-service.ts +155 -0
  90. package/src/bundler/app-bundler.ts +12 -8
  91. package/src/calls/call-bridge.ts +95 -0
  92. package/src/calls/call-constants.ts +43 -5
  93. package/src/calls/call-domain.ts +276 -0
  94. package/src/calls/call-orchestrator.ts +43 -17
  95. package/src/calls/call-recovery.ts +207 -0
  96. package/src/calls/call-state-machine.ts +68 -0
  97. package/src/calls/call-store.ts +192 -5
  98. package/src/calls/relay-server.ts +41 -4
  99. package/src/calls/speaker-identification.ts +213 -0
  100. package/src/calls/twilio-provider.ts +10 -6
  101. package/src/calls/twilio-routes.ts +90 -76
  102. package/src/calls/types.ts +1 -1
  103. package/src/cli/config-commands.ts +334 -0
  104. package/src/cli/core-commands.ts +776 -0
  105. package/src/cli/doordash.ts +251 -1
  106. package/src/cli/ipc-client.ts +82 -0
  107. package/src/cli/map.ts +246 -0
  108. package/src/cli/twitter.ts +575 -0
  109. package/src/cli.ts +7 -5
  110. package/src/commands/__tests__/cc-command-registry.test.ts +319 -0
  111. package/src/commands/cc-command-registry.ts +209 -0
  112. package/src/config/bundled-skills/contacts/SKILL.md +39 -0
  113. package/src/config/bundled-skills/contacts/TOOLS.json +122 -0
  114. package/src/config/bundled-skills/contacts/tools/contact-merge.ts +9 -0
  115. package/src/config/bundled-skills/contacts/tools/contact-search.ts +9 -0
  116. package/src/config/bundled-skills/contacts/tools/contact-upsert.ts +9 -0
  117. package/src/config/bundled-skills/document/SKILL.md +18 -0
  118. package/src/config/bundled-skills/document/TOOLS.json +53 -0
  119. package/src/config/bundled-skills/document/tools/document-create.ts +9 -0
  120. package/src/config/bundled-skills/document/tools/document-update.ts +9 -0
  121. package/src/config/bundled-skills/doordash/SKILL.md +82 -23
  122. package/src/config/bundled-skills/followups/SKILL.md +32 -0
  123. package/src/config/bundled-skills/followups/TOOLS.json +100 -0
  124. package/src/config/bundled-skills/followups/tools/followup-create.ts +9 -0
  125. package/src/config/bundled-skills/followups/tools/followup-list.ts +9 -0
  126. package/src/config/bundled-skills/followups/tools/followup-resolve.ts +9 -0
  127. package/src/config/bundled-skills/image-studio/tools/media-generate-image.ts +1 -23
  128. package/src/config/bundled-skills/messaging/tools/messaging-analyze-style.ts +2 -1
  129. package/src/config/bundled-skills/playbooks/SKILL.md +31 -0
  130. package/src/config/bundled-skills/playbooks/TOOLS.json +126 -0
  131. package/src/config/bundled-skills/playbooks/tools/playbook-create.ts +9 -0
  132. package/src/config/bundled-skills/playbooks/tools/playbook-delete.ts +9 -0
  133. package/src/config/bundled-skills/playbooks/tools/playbook-list.ts +9 -0
  134. package/src/config/bundled-skills/playbooks/tools/playbook-update.ts +9 -0
  135. package/src/config/bundled-skills/reminder/SKILL.md +20 -0
  136. package/src/config/bundled-skills/reminder/TOOLS.json +67 -0
  137. package/src/config/bundled-skills/reminder/tools/reminder-cancel.ts +9 -0
  138. package/src/config/bundled-skills/reminder/tools/reminder-create.ts +9 -0
  139. package/src/config/bundled-skills/reminder/tools/reminder-list.ts +9 -0
  140. package/src/config/bundled-skills/schedule/SKILL.md +74 -0
  141. package/src/config/bundled-skills/schedule/TOOLS.json +135 -0
  142. package/src/config/bundled-skills/schedule/tools/schedule-create.ts +9 -0
  143. package/src/config/bundled-skills/schedule/tools/schedule-delete.ts +9 -0
  144. package/src/config/bundled-skills/schedule/tools/schedule-list.ts +9 -0
  145. package/src/config/bundled-skills/schedule/tools/schedule-update.ts +9 -0
  146. package/src/config/bundled-skills/subagent/SKILL.md +25 -0
  147. package/src/config/bundled-skills/subagent/TOOLS.json +107 -0
  148. package/src/config/bundled-skills/subagent/tools/subagent-abort.ts +9 -0
  149. package/src/config/bundled-skills/subagent/tools/subagent-message.ts +9 -0
  150. package/src/config/bundled-skills/subagent/tools/subagent-read.ts +9 -0
  151. package/src/config/bundled-skills/subagent/tools/subagent-spawn.ts +9 -0
  152. package/src/config/bundled-skills/subagent/tools/subagent-status.ts +9 -0
  153. package/src/config/bundled-skills/tasks/SKILL.md +28 -0
  154. package/src/config/bundled-skills/tasks/TOOLS.json +256 -0
  155. package/src/config/bundled-skills/tasks/tools/task-delete.ts +9 -0
  156. package/src/config/bundled-skills/tasks/tools/task-list-add.ts +9 -0
  157. package/src/config/bundled-skills/tasks/tools/task-list-remove.ts +9 -0
  158. package/src/config/bundled-skills/tasks/tools/task-list-show.ts +9 -0
  159. package/src/config/bundled-skills/tasks/tools/task-list-update.ts +9 -0
  160. package/src/config/bundled-skills/tasks/tools/task-list.ts +9 -0
  161. package/src/config/bundled-skills/tasks/tools/task-run.ts +9 -0
  162. package/src/config/bundled-skills/tasks/tools/task-save.ts +9 -0
  163. package/src/config/bundled-skills/twitter/SKILL.md +134 -0
  164. package/src/config/bundled-skills/watcher/SKILL.md +27 -0
  165. package/src/config/bundled-skills/watcher/TOOLS.json +147 -0
  166. package/src/config/bundled-skills/watcher/tools/watcher-create.ts +9 -0
  167. package/src/config/bundled-skills/watcher/tools/watcher-delete.ts +9 -0
  168. package/src/config/bundled-skills/watcher/tools/watcher-digest.ts +9 -0
  169. package/src/config/bundled-skills/watcher/tools/watcher-list.ts +9 -0
  170. package/src/config/bundled-skills/watcher/tools/watcher-update.ts +9 -0
  171. package/src/config/defaults.ts +33 -0
  172. package/src/config/loader.ts +4 -1
  173. package/src/config/schema.ts +161 -1
  174. package/src/config/system-prompt.ts +61 -16
  175. package/src/config/templates/IDENTITY.md +7 -0
  176. package/src/config/types.ts +4 -0
  177. package/src/contacts/contact-store.ts +4 -4
  178. package/src/daemon/assistant-attachments.ts +10 -0
  179. package/src/daemon/classifier.ts +3 -1
  180. package/src/daemon/computer-use-session.ts +3 -1
  181. package/src/daemon/date-context.ts +136 -0
  182. package/src/daemon/handlers/apps.ts +16 -1
  183. package/src/daemon/handlers/browser.ts +54 -0
  184. package/src/daemon/handlers/computer-use.ts +7 -1
  185. package/src/daemon/handlers/config.ts +163 -5
  186. package/src/daemon/handlers/diagnostics.ts +5 -1
  187. package/src/daemon/handlers/documents.ts +18 -29
  188. package/src/daemon/handlers/home-base.ts +5 -1
  189. package/src/daemon/handlers/index.ts +40 -277
  190. package/src/daemon/handlers/misc.ts +9 -1
  191. package/src/daemon/handlers/publish.ts +6 -1
  192. package/src/daemon/handlers/sessions.ts +65 -12
  193. package/src/daemon/handlers/shared.ts +36 -1
  194. package/src/daemon/handlers/signing.ts +37 -0
  195. package/src/daemon/handlers/skills.ts +20 -6
  196. package/src/daemon/handlers/subagents.ts +8 -3
  197. package/src/daemon/handlers/twitter-auth.ts +169 -0
  198. package/src/daemon/handlers/work-items.ts +384 -68
  199. package/src/daemon/ipc-contract-inventory.json +28 -4
  200. package/src/daemon/ipc-contract.ts +133 -37
  201. package/src/daemon/ipc-protocol.ts +7 -2
  202. package/src/daemon/lifecycle.ts +21 -0
  203. package/src/daemon/main.ts +10 -4
  204. package/src/daemon/ride-shotgun-handler.ts +74 -10
  205. package/src/daemon/server.ts +143 -26
  206. package/src/daemon/session-agent-loop.ts +887 -0
  207. package/src/daemon/session-attachments.ts +28 -5
  208. package/src/daemon/session-error.ts +24 -3
  209. package/src/daemon/session-lifecycle.ts +147 -0
  210. package/src/daemon/session-media-retry.ts +147 -0
  211. package/src/daemon/session-messaging.ts +145 -0
  212. package/src/daemon/session-notifiers.ts +164 -0
  213. package/src/daemon/session-process.ts +2 -2
  214. package/src/daemon/session-queue-manager.ts +1 -0
  215. package/src/daemon/session-runtime-assembly.ts +52 -0
  216. package/src/daemon/session-skill-tools.ts +124 -5
  217. package/src/daemon/session-slash.ts +3 -0
  218. package/src/daemon/session-surfaces.ts +77 -2
  219. package/src/daemon/session-tool-setup.ts +216 -2
  220. package/src/daemon/session-usage.ts +0 -2
  221. package/src/daemon/session.ts +114 -1404
  222. package/src/daemon/video-thumbnail.ts +60 -0
  223. package/src/doordash/client.ts +121 -27
  224. package/src/doordash/queries.ts +1 -2
  225. package/src/export/formatter.ts +3 -1
  226. package/src/followups/followup-store.ts +4 -2
  227. package/src/followups/types.ts +6 -0
  228. package/src/hooks/templates.ts +1 -1
  229. package/src/index.ts +32 -1153
  230. package/src/memory/attachments-store.ts +28 -83
  231. package/src/memory/channel-delivery-store.ts +7 -21
  232. package/src/memory/clarification-resolver.ts +6 -5
  233. package/src/memory/contradiction-checker.ts +3 -2
  234. package/src/memory/conversation-key-store.ts +10 -29
  235. package/src/memory/conversation-store.ts +2 -1
  236. package/src/memory/db.ts +96 -2
  237. package/src/memory/entity-extractor.ts +6 -3
  238. package/src/memory/items-extractor.ts +5 -4
  239. package/src/memory/jobs-store.ts +3 -2
  240. package/src/memory/llm-usage-store.ts +1 -2
  241. package/src/memory/runs-store.ts +1 -2
  242. package/src/memory/schema.ts +23 -2
  243. package/src/messaging/style-analyzer.ts +3 -2
  244. package/src/messaging/thread-summarizer.ts +8 -12
  245. package/src/messaging/triage-engine.ts +4 -2
  246. package/src/providers/openrouter/client.ts +20 -0
  247. package/src/providers/registry.ts +8 -0
  248. package/src/runtime/http-server.ts +108 -20
  249. package/src/runtime/routes/attachment-routes.ts +2 -3
  250. package/src/runtime/routes/call-routes.ts +140 -0
  251. package/src/runtime/routes/channel-routes.ts +5 -10
  252. package/src/runtime/routes/conversation-routes.ts +5 -5
  253. package/src/runtime/routes/run-routes.ts +2 -2
  254. package/src/runtime/run-orchestrator.ts +9 -3
  255. package/src/schedule/recurrence-engine.ts +138 -0
  256. package/src/schedule/recurrence-types.ts +67 -0
  257. package/src/schedule/schedule-store.ts +102 -57
  258. package/src/schedule/scheduler.ts +9 -6
  259. package/src/security/oauth2.ts +29 -4
  260. package/src/security/secret-allowlist.ts +46 -0
  261. package/src/skills/clawhub.ts +1 -1
  262. package/src/subagent/manager.ts +40 -8
  263. package/src/swarm/backend-claude-code.ts +64 -9
  264. package/src/swarm/worker-prompts.ts +2 -1
  265. package/src/tasks/SPEC.md +34 -28
  266. package/src/tasks/ephemeral-permissions.ts +16 -7
  267. package/src/tasks/task-compiler.ts +5 -4
  268. package/src/tasks/task-runner.ts +10 -5
  269. package/src/tasks/task-scheduler.ts +1 -1
  270. package/src/tasks/tool-sanitizer.ts +36 -0
  271. package/src/tools/assets/search.ts +4 -4
  272. package/src/tools/browser/api-map.ts +220 -0
  273. package/src/tools/browser/auto-navigate.ts +270 -0
  274. package/src/tools/browser/browser-execution.ts +2 -1
  275. package/src/tools/browser/browser-manager.ts +2 -2
  276. package/src/tools/browser/network-recorder.ts +5 -4
  277. package/src/tools/browser/x-auto-navigate.ts +207 -0
  278. package/src/tools/calls/call-end.ts +17 -67
  279. package/src/tools/calls/call-start.ts +24 -85
  280. package/src/tools/calls/call-status.ts +35 -51
  281. package/src/tools/claude-code/claude-code.ts +77 -11
  282. package/src/tools/contacts/contact-merge.ts +46 -78
  283. package/src/tools/contacts/contact-search.ts +35 -79
  284. package/src/tools/contacts/contact-upsert.ts +35 -108
  285. package/src/tools/credentials/vault.ts +20 -4
  286. package/src/tools/document/document-tool.ts +71 -144
  287. package/src/tools/executor.ts +129 -10
  288. package/src/tools/followups/followup_create.ts +46 -88
  289. package/src/tools/followups/followup_list.ts +34 -74
  290. package/src/tools/followups/followup_resolve.ts +31 -66
  291. package/src/tools/host-terminal/cli-discover.ts +2 -1
  292. package/src/tools/host-terminal/host-shell.ts +10 -0
  293. package/src/tools/memory/handlers.ts +5 -4
  294. package/src/tools/network/__tests__/web-search.test.ts +427 -0
  295. package/src/tools/network/script-proxy/__tests__/logging.test.ts +248 -0
  296. package/src/tools/network/script-proxy/__tests__/policy.test.ts +234 -0
  297. package/src/tools/network/script-proxy/__tests__/router.test.ts +76 -0
  298. package/src/tools/network/web-fetch.ts +18 -6
  299. package/src/tools/playbooks/index.ts +4 -5
  300. package/src/tools/playbooks/playbook-create.ts +3 -47
  301. package/src/tools/playbooks/playbook-delete.ts +1 -25
  302. package/src/tools/playbooks/playbook-list.ts +1 -28
  303. package/src/tools/playbooks/playbook-update.ts +3 -51
  304. package/src/tools/reminder/reminder.ts +5 -78
  305. package/src/tools/schedule/create.ts +69 -74
  306. package/src/tools/schedule/delete.ts +21 -47
  307. package/src/tools/schedule/list.ts +55 -74
  308. package/src/tools/schedule/update.ts +77 -84
  309. package/src/tools/subagent/abort.ts +29 -58
  310. package/src/tools/subagent/message.ts +30 -63
  311. package/src/tools/subagent/read.ts +53 -84
  312. package/src/tools/subagent/spawn.ts +43 -82
  313. package/src/tools/subagent/status.ts +42 -71
  314. package/src/tools/swarm/delegate.ts +2 -1
  315. package/src/tools/tasks/index.ts +8 -8
  316. package/src/tools/tasks/task-delete.ts +60 -88
  317. package/src/tools/tasks/task-list.ts +31 -52
  318. package/src/tools/tasks/task-run.ts +72 -108
  319. package/src/tools/tasks/task-save.ts +33 -65
  320. package/src/tools/tasks/work-item-enqueue.ts +183 -215
  321. package/src/tools/tasks/work-item-list.ts +33 -63
  322. package/src/tools/tasks/work-item-remove.ts +45 -97
  323. package/src/tools/tasks/work-item-update.ts +91 -163
  324. package/src/tools/terminal/backends/native.ts +3 -1
  325. package/src/tools/tool-manifest.ts +0 -62
  326. package/src/tools/types.ts +6 -0
  327. package/src/tools/ui-surface/definitions.ts +3 -1
  328. package/src/tools/watch/screen-watch.ts +3 -1
  329. package/src/tools/watcher/create.ts +52 -98
  330. package/src/tools/watcher/delete.ts +20 -46
  331. package/src/tools/watcher/digest.ts +36 -70
  332. package/src/tools/watcher/list.ts +49 -79
  333. package/src/tools/watcher/update.ts +45 -91
  334. package/src/twitter/client.ts +690 -0
  335. package/src/twitter/session.ts +91 -0
  336. package/src/usage/types.ts +0 -1
  337. package/src/util/truncate.ts +6 -0
  338. package/src/watcher/providers/slack.ts +2 -1
  339. package/src/watcher/watcher-store.ts +3 -2
  340. package/src/work-items/work-item-store.ts +27 -2
  341. package/src/workspace/commit-message-enrichment-service.ts +31 -7
  342. package/src/workspace/git-service.ts +87 -22
  343. package/src/workspace/provider-commit-message-generator.ts +242 -0
  344. package/src/workspace/turn-commit.ts +62 -3
  345. package/src/tools/contacts/index.ts +0 -4
  346. package/src/tools/document/index.ts +0 -5
  347. package/src/tools/followups/index.ts +0 -3
  348. package/src/tools/subagent/index.ts +0 -5
  349. /package/src/__tests__/{memory-context-benchmark.test.ts → memory-context-benchmark.benchmark.test.ts} +0 -0
@@ -0,0 +1,207 @@
1
+ /**
2
+ * Call recovery — reconciles in-flight calls on daemon restart.
3
+ *
4
+ * When the daemon restarts, any calls left in non-terminal states may be stale
5
+ * (the daemon crashed mid-call) or still active on the provider side. This
6
+ * module fetches the actual provider status and transitions each call
7
+ * accordingly.
8
+ */
9
+
10
+ import { getLogger } from '../util/logger.js';
11
+ import { listRecoverableCalls, updateCallSession, expirePendingQuestions } from './call-store.js';
12
+ import type { VoiceProvider } from './voice-provider.js';
13
+ import type { CallStatus } from './types.js';
14
+
15
+ type Logger = ReturnType<typeof getLogger>;
16
+
17
+ const defaultLog = getLogger('call-recovery');
18
+
19
+ /**
20
+ * Grace period (in ms) for no-SID sessions during startup recovery.
21
+ *
22
+ * A daemon crash can leave a live Twilio call without a persisted SID
23
+ * (crash after `initiateCall` succeeds but before the SID is written).
24
+ * Webhooks carrying the SID may still arrive after restart.
25
+ *
26
+ * Sessions younger than this threshold are annotated but left in their
27
+ * current non-terminal state so incoming webhooks can still deliver the
28
+ * SID and resume the call normally.
29
+ *
30
+ * Sessions older than this threshold are transitioned to `failed` to
31
+ * prevent orphan sessions from creating false "active call" state
32
+ * indefinitely. 5 minutes is long enough for any legitimate webhook
33
+ * to arrive; after that the session is considered abandoned.
34
+ */
35
+ export const NO_SID_GRACE_PERIOD_MS = 5 * 60_000;
36
+
37
+ /**
38
+ * Map a Twilio provider status string to our internal CallStatus.
39
+ * Returns the mapped status or null if the status is unrecognised.
40
+ */
41
+ function mapProviderStatus(providerStatus: string): CallStatus | null {
42
+ switch (providerStatus) {
43
+ case 'queued':
44
+ case 'ringing':
45
+ return 'ringing';
46
+ case 'in-progress':
47
+ return 'in_progress';
48
+ case 'completed':
49
+ return 'completed';
50
+ case 'failed':
51
+ case 'busy':
52
+ case 'no-answer':
53
+ case 'canceled':
54
+ return 'failed';
55
+ default:
56
+ return null;
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Check whether a CallStatus is terminal (no further transitions allowed).
62
+ */
63
+ function isTerminal(status: CallStatus): boolean {
64
+ return status === 'completed' || status === 'failed' || status === 'cancelled';
65
+ }
66
+
67
+ /**
68
+ * Reconcile all non-terminal call sessions at daemon startup.
69
+ *
70
+ * For each recoverable call:
71
+ * - If it has a provider SID, fetch the current status from the provider
72
+ * and transition the call to match.
73
+ * - If no provider SID exists and the session is older than the grace
74
+ * period, transition it to `failed` to prevent orphan sessions from
75
+ * creating false "active call" state indefinitely.
76
+ * - If no provider SID exists but the session is within the grace period,
77
+ * leave it non-terminal so webhooks can still deliver the SID.
78
+ * - If the call transitions to a terminal state, expire any pending questions.
79
+ */
80
+ export async function reconcileCallsOnStartup(
81
+ provider: VoiceProvider,
82
+ log: Logger = defaultLog,
83
+ ): Promise<void> {
84
+ const recoverableCalls = listRecoverableCalls();
85
+
86
+ if (recoverableCalls.length === 0) {
87
+ log.info('No recoverable calls found at startup');
88
+ return;
89
+ }
90
+
91
+ log.info({ count: recoverableCalls.length }, 'Reconciling non-terminal calls at startup');
92
+
93
+ for (const session of recoverableCalls) {
94
+ try {
95
+ if (!session.providerCallSid) {
96
+ const sessionAgeMs = Date.now() - session.createdAt;
97
+ const isStale = sessionAgeMs >= NO_SID_GRACE_PERIOD_MS;
98
+
99
+ if (isStale) {
100
+ // Session is old enough that any legitimate webhook should
101
+ // have arrived by now. Transition to `failed` so it no
102
+ // longer appears as an active call.
103
+ log.info(
104
+ { callSessionId: session.id, previousStatus: session.status, sessionAgeMs },
105
+ 'No-SID session past grace period — failing orphan session',
106
+ );
107
+ updateCallSession(session.id, {
108
+ status: 'failed',
109
+ endedAt: Date.now(),
110
+ lastError: 'Daemon restarted before provider SID persisted; grace period expired — orphan session failed',
111
+ });
112
+ expirePendingQuestions(session.id);
113
+ } else {
114
+ // Recent session — webhooks carrying the SID may still arrive.
115
+ // Leave in its current non-terminal state.
116
+ log.info(
117
+ { callSessionId: session.id, previousStatus: session.status, sessionAgeMs },
118
+ 'Skipping recent no-SID session (within grace period, webhooks may still arrive)',
119
+ );
120
+ updateCallSession(session.id, {
121
+ lastError: 'Daemon restarted before provider SID persisted; awaiting webhook',
122
+ });
123
+ }
124
+ continue;
125
+ }
126
+
127
+ // Fetch actual status from provider
128
+ let providerStatus: string;
129
+ try {
130
+ providerStatus = await provider.getCallStatus(session.providerCallSid);
131
+ } catch (err) {
132
+ const msg = err instanceof Error ? err.message : String(err);
133
+ log.warn(
134
+ { callSessionId: session.id, callSid: session.providerCallSid, err },
135
+ 'Failed to fetch provider status during recovery — failing call',
136
+ );
137
+ updateCallSession(session.id, {
138
+ status: 'failed',
139
+ endedAt: Date.now(),
140
+ lastError: `Recovery: failed to fetch provider status: ${msg}`,
141
+ });
142
+ expirePendingQuestions(session.id);
143
+ continue;
144
+ }
145
+
146
+ const mappedStatus = mapProviderStatus(providerStatus);
147
+
148
+ if (!mappedStatus) {
149
+ log.warn(
150
+ { callSessionId: session.id, providerStatus },
151
+ 'Unrecognised provider status during recovery — failing call',
152
+ );
153
+ updateCallSession(session.id, {
154
+ status: 'failed',
155
+ endedAt: Date.now(),
156
+ lastError: `Recovery: unrecognised provider status '${providerStatus}'`,
157
+ });
158
+ expirePendingQuestions(session.id);
159
+ continue;
160
+ }
161
+
162
+ if (isTerminal(mappedStatus)) {
163
+ // Provider says the call has ended
164
+ log.info(
165
+ { callSessionId: session.id, providerStatus, mappedStatus },
166
+ 'Provider reports call ended — transitioning to terminal state',
167
+ );
168
+ updateCallSession(session.id, {
169
+ status: mappedStatus,
170
+ endedAt: Date.now(),
171
+ });
172
+ expirePendingQuestions(session.id);
173
+ } else {
174
+ // Provider says call is still active — leave it for webhooks to handle
175
+ log.info(
176
+ { callSessionId: session.id, providerStatus, mappedStatus },
177
+ 'Provider reports call still active — leaving for webhook handling',
178
+ );
179
+ }
180
+ } catch (err) {
181
+ log.error(
182
+ { callSessionId: session.id, err },
183
+ 'Unexpected error during call recovery',
184
+ );
185
+ }
186
+ }
187
+
188
+ log.info('Call recovery reconciliation complete');
189
+ }
190
+
191
+ /**
192
+ * Log a dead-letter provider event — a provider callback payload that
193
+ * could not be processed (malformed, unknown format, etc.).
194
+ *
195
+ * Rather than silently dropping these events, we log the full payload
196
+ * so operators can investigate later.
197
+ */
198
+ export function logDeadLetterEvent(
199
+ reason: string,
200
+ payload: unknown,
201
+ log: Logger = defaultLog,
202
+ ): void {
203
+ log.error(
204
+ { reason, payload },
205
+ 'Dead-letter provider event: callback could not be processed',
206
+ );
207
+ }
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Call state machine — defines allowed status transitions and validates
3
+ * all state changes in a single place.
4
+ *
5
+ * Terminal states (completed, failed, cancelled) are immutable: no further
6
+ * transitions are permitted once a call reaches one of these states.
7
+ */
8
+
9
+ import type { CallStatus } from './types.js';
10
+
11
+ // ── Transition table ─────────────────────────────────────────────────
12
+
13
+ /**
14
+ * Maps each call status to the set of statuses it may transition to.
15
+ * Terminal states map to an empty set.
16
+ */
17
+ const ALLOWED_TRANSITIONS: Record<CallStatus, Set<CallStatus>> = {
18
+ initiated: new Set<CallStatus>(['ringing', 'in_progress', 'waiting_on_user', 'completed', 'failed', 'cancelled']),
19
+ ringing: new Set<CallStatus>(['in_progress', 'waiting_on_user', 'completed', 'failed', 'cancelled']),
20
+ in_progress: new Set<CallStatus>(['waiting_on_user', 'completed', 'failed', 'cancelled']),
21
+ waiting_on_user: new Set<CallStatus>(['in_progress', 'completed', 'failed', 'cancelled']),
22
+ // Terminal states — no further transitions allowed
23
+ completed: new Set<CallStatus>(),
24
+ failed: new Set<CallStatus>(),
25
+ cancelled: new Set<CallStatus>(),
26
+ };
27
+
28
+ const TERMINAL_STATES: Set<CallStatus> = new Set(['completed', 'failed', 'cancelled']);
29
+
30
+ // ── Public API ───────────────────────────────────────────────────────
31
+
32
+ export interface TransitionResult {
33
+ valid: boolean;
34
+ reason?: string;
35
+ }
36
+
37
+ /**
38
+ * Check whether a transition from `current` to `next` is allowed.
39
+ */
40
+ export function validateTransition(current: CallStatus, next: CallStatus): TransitionResult {
41
+ if (current === next) {
42
+ return { valid: true };
43
+ }
44
+
45
+ if (isTerminalState(current)) {
46
+ return {
47
+ valid: false,
48
+ reason: `Cannot transition from terminal state '${current}'`,
49
+ };
50
+ }
51
+
52
+ const allowed = ALLOWED_TRANSITIONS[current];
53
+ if (!allowed || !allowed.has(next)) {
54
+ return {
55
+ valid: false,
56
+ reason: `Invalid transition from '${current}' to '${next}'`,
57
+ };
58
+ }
59
+
60
+ return { valid: true };
61
+ }
62
+
63
+ /**
64
+ * Returns true if the given status is a terminal (immutable) state.
65
+ */
66
+ export function isTerminalState(status: CallStatus): boolean {
67
+ return TERMINAL_STATES.has(status);
68
+ }
@@ -2,7 +2,8 @@ import { eq, and, notInArray, desc } from 'drizzle-orm';
2
2
  import { v4 as uuid } from 'uuid';
3
3
  import { getDb } from '../memory/db.js';
4
4
  import { callSessions, callEvents, callPendingQuestions } from '../memory/schema.js';
5
- import type { CallSession, CallEvent, CallPendingQuestion, CallEventType } from './types.js';
5
+ import type { CallSession, CallEvent, CallPendingQuestion, CallEventType, CallStatus } from './types.js';
6
+ import { validateTransition } from './call-state-machine.js';
6
7
  import { getLogger } from '../util/logger.js';
7
8
 
8
9
  const log = getLogger('call-store');
@@ -105,7 +106,7 @@ export function getActiveCallSessionForConversation(conversationId: string): Cal
105
106
  .where(
106
107
  and(
107
108
  eq(callSessions.conversationId, conversationId),
108
- notInArray(callSessions.status, ['completed', 'failed']),
109
+ notInArray(callSessions.status, ['completed', 'failed', 'cancelled']),
109
110
  ),
110
111
  )
111
112
  .orderBy(desc(callSessions.createdAt))
@@ -119,12 +120,44 @@ export function updateCallSession(
119
120
  updates: Partial<Pick<CallSession, 'status' | 'providerCallSid' | 'startedAt' | 'endedAt' | 'lastError'>>,
120
121
  ): void {
121
122
  const db = getDb();
123
+
124
+ // Validate status transition when a new status is provided
125
+ if (updates.status) {
126
+ const current = getCallSession(id);
127
+ if (current) {
128
+ const result = validateTransition(current.status, updates.status as CallStatus);
129
+ if (!result.valid) {
130
+ log.warn({ callSessionId: id, from: current.status, to: updates.status, reason: result.reason }, 'Invalid call status transition — skipping update');
131
+ return;
132
+ }
133
+ }
134
+ }
135
+
122
136
  db.update(callSessions)
123
137
  .set({ ...updates, updatedAt: Date.now() })
124
138
  .where(eq(callSessions.id, id))
125
139
  .run();
126
140
  }
127
141
 
142
+ // ── Recovery queries ─────────────────────────────────────────────────
143
+
144
+ /**
145
+ * Returns all call sessions that are in a non-terminal state
146
+ * (i.e. not completed, failed, or cancelled). Used during daemon startup
147
+ * to reconcile in-flight calls.
148
+ */
149
+ export function listRecoverableCalls(): CallSession[] {
150
+ const db = getDb();
151
+ const rows = db
152
+ .select()
153
+ .from(callSessions)
154
+ .where(
155
+ notInArray(callSessions.status, ['completed', 'failed', 'cancelled']),
156
+ )
157
+ .all();
158
+ return rows.map(parseCallSession);
159
+ }
160
+
128
161
  // ── Call Events ──────────────────────────────────────────────────────
129
162
 
130
163
  export function recordCallEvent(
@@ -207,10 +240,10 @@ export function answerPendingQuestion(id: string, answerText: string): void {
207
240
  ),
208
241
  )
209
242
  .run();
210
- // Drizzle's .run() returns void for bun:sqlite, so verify via raw client.
243
+ // Drizzle's .run() returns void for bun:sqlite, so check affected rows via raw client.
211
244
  const raw = (db as unknown as { $client: import('bun:sqlite').Database }).$client;
212
- const check = raw.query('SELECT status FROM call_pending_questions WHERE id = ?').get(id) as { status: string } | null;
213
- if (!check || check.status !== 'answered') {
245
+ const changes = raw.query('SELECT changes() as c').get() as { c: number };
246
+ if (changes.c === 0) {
214
247
  log.warn({ questionId: id }, 'answerPendingQuestion: no rows updated — question may have already been answered or expired');
215
248
  }
216
249
  }
@@ -227,3 +260,157 @@ export function expirePendingQuestions(callSessionId: string): void {
227
260
  )
228
261
  .run();
229
262
  }
263
+
264
+ // ── Callback Idempotency ─────────────────────────────────────────────
265
+
266
+ /** Claims older than this are considered orphaned (crashed mid-processing) and can be reclaimed. */
267
+ const CLAIM_EXPIRY_MS = 60_000; // 60 seconds
268
+
269
+ /**
270
+ * Build a dedupe key for a Twilio status callback.
271
+ * Combines CallSid + CallStatus + Timestamp (or SequenceNumber if present)
272
+ * to uniquely identify each callback.
273
+ */
274
+ export function buildCallbackDedupeKey(
275
+ callSid: string,
276
+ callStatus: string,
277
+ timestamp?: string | null,
278
+ sequenceNumber?: string | null,
279
+ ): string {
280
+ const discriminator = sequenceNumber ?? timestamp ?? '';
281
+ return `${callSid}:${callStatus}:${discriminator}`;
282
+ }
283
+
284
+ /**
285
+ * Check whether a callback dedupe key has already been processed (read-only).
286
+ * Returns true if the key already exists, false otherwise.
287
+ */
288
+ export function isCallbackProcessed(dedupeKey: string): boolean {
289
+ const db = getDb();
290
+ const raw = (db as unknown as { $client: import('bun:sqlite').Database }).$client;
291
+
292
+ const row = raw.query(
293
+ `SELECT 1 FROM processed_callbacks WHERE dedupe_key = ?`,
294
+ ).get(dedupeKey);
295
+ return row != null;
296
+ }
297
+
298
+ /**
299
+ * Record a callback as processed. Should be called AFTER downstream writes
300
+ * (session updates, event recording) have succeeded so that Twilio retries
301
+ * are not silently dropped if those writes fail.
302
+ *
303
+ * Uses INSERT OR IGNORE so concurrent calls for the same key are safe.
304
+ */
305
+ export function recordProcessedCallback(
306
+ dedupeKey: string,
307
+ callSessionId: string,
308
+ ): void {
309
+ const db = getDb();
310
+ const raw = (db as unknown as { $client: import('bun:sqlite').Database }).$client;
311
+
312
+ raw.query(
313
+ `INSERT OR IGNORE INTO processed_callbacks (id, dedupe_key, call_session_id, created_at) VALUES (?, ?, ?, ?)`,
314
+ ).run(uuid(), dedupeKey, callSessionId, Date.now());
315
+ }
316
+
317
+ /**
318
+ * Try to record a processed callback. Returns true if this is a new callback
319
+ * (inserted successfully). Returns false if the callback was already processed
320
+ * (dedupe key already exists), indicating a replay.
321
+ *
322
+ * Uses INSERT ... ON CONFLICT DO NOTHING pattern for atomicity.
323
+ *
324
+ * @deprecated Use claimCallback + releaseCallbackClaim instead to
325
+ * atomically claim a callback and release on failure.
326
+ */
327
+ export function tryRecordProcessedCallback(
328
+ dedupeKey: string,
329
+ callSessionId: string,
330
+ ): boolean {
331
+ const db = getDb();
332
+ const raw = (db as unknown as { $client: import('bun:sqlite').Database }).$client;
333
+
334
+ raw.query(
335
+ `INSERT OR IGNORE INTO processed_callbacks (id, dedupe_key, call_session_id, created_at) VALUES (?, ?, ?, ?)`,
336
+ ).run(uuid(), dedupeKey, callSessionId, Date.now());
337
+
338
+ const changes = raw.query('SELECT changes() as c').get() as { c: number };
339
+ return changes.c > 0;
340
+ }
341
+
342
+ /**
343
+ * Atomically claim a callback for processing. Returns a unique claim ID
344
+ * (string) if this caller won the claim, or null if another caller already
345
+ * claimed it (dedupe_key conflict).
346
+ *
347
+ * Expired orphaned claims (older than CLAIM_EXPIRY_MS) are automatically
348
+ * cleared before attempting the insert, so crashes mid-processing don't
349
+ * permanently block retries.
350
+ *
351
+ * If processing fails, call `releaseCallbackClaim(dedupeKey, claimId)` to allow retries.
352
+ * On success, call `finalizeCallbackClaim(dedupeKey, claimId)` to make the claim permanent.
353
+ *
354
+ * The returned claim ID acts as an ownership token: release and finalize
355
+ * operations require it so that handler A cannot accidentally release or
356
+ * finalize a claim that was reclaimed by handler B after expiry.
357
+ */
358
+ export function claimCallback(dedupeKey: string, callSessionId: string): string | null {
359
+ const db = getDb();
360
+ const raw = (db as unknown as { $client: import('bun:sqlite').Database }).$client;
361
+
362
+ // Clear any expired orphaned claims so they can be reprocessed
363
+ raw.query(
364
+ `DELETE FROM processed_callbacks WHERE dedupe_key = ? AND created_at < ?`,
365
+ ).run(dedupeKey, Date.now() - CLAIM_EXPIRY_MS);
366
+
367
+ const claimId = uuid();
368
+ raw.query(
369
+ `INSERT OR IGNORE INTO processed_callbacks (id, dedupe_key, call_session_id, claim_id, created_at) VALUES (?, ?, ?, ?, ?)`,
370
+ ).run(uuid(), dedupeKey, callSessionId, claimId, Date.now());
371
+ const changes = raw.query('SELECT changes() as c').get() as { c: number };
372
+ return changes.c > 0 ? claimId : null;
373
+ }
374
+
375
+ /**
376
+ * Release a callback claim so that retries can reprocess it.
377
+ * Called when processing fails after a successful claim.
378
+ *
379
+ * Only deletes the row if both dedupe_key AND claim_id match, preventing
380
+ * handler A from releasing a claim that was reclaimed by handler B.
381
+ */
382
+ export function releaseCallbackClaim(dedupeKey: string, claimId: string): void {
383
+ const db = getDb();
384
+ const raw = (db as unknown as { $client: import('bun:sqlite').Database }).$client;
385
+ raw.query(`DELETE FROM processed_callbacks WHERE dedupe_key = ? AND claim_id = ?`).run(dedupeKey, claimId);
386
+ }
387
+
388
+ /**
389
+ * Finalize a callback claim after successful processing.
390
+ * Sets the created_at to a far-future value so the claim never expires,
391
+ * distinguishing it from in-flight claims that may need to be reclaimed.
392
+ *
393
+ * Only updates the row if both dedupe_key AND claim_id match, preventing
394
+ * handler A from finalizing a claim that was reclaimed by handler B.
395
+ *
396
+ * Returns true if the claim was successfully finalized, or false if 0 rows
397
+ * were updated — meaning the claim was reclaimed by another handler after
398
+ * expiry. Callers should treat a false return as a lost-claim signal: the
399
+ * business writes already happened but the dedupe row belongs to someone
400
+ * else, so duplicate processing may occur on later retries.
401
+ */
402
+ export function finalizeCallbackClaim(dedupeKey: string, claimId: string): boolean {
403
+ const db = getDb();
404
+ const raw = (db as unknown as { $client: import('bun:sqlite').Database }).$client;
405
+ // Set created_at far in the future so expiry check never matches
406
+ const NEVER_EXPIRE = Date.now() + 100 * 365 * 24 * 60 * 60 * 1000; // ~100 years
407
+ raw.query(
408
+ `UPDATE processed_callbacks SET created_at = ? WHERE dedupe_key = ? AND claim_id = ?`,
409
+ ).run(NEVER_EXPIRE, dedupeKey, claimId);
410
+ const changes = raw.query('SELECT changes() as c').get() as { c: number };
411
+ if (changes.c === 0) {
412
+ log.warn({ dedupeKey, claimId }, 'finalizeCallbackClaim: claim was lost — another handler reclaimed this key after expiry');
413
+ return false;
414
+ }
415
+ return true;
416
+ }
@@ -14,6 +14,11 @@ import {
14
14
  recordCallEvent,
15
15
  } from './call-store.js';
16
16
  import { CallOrchestrator } from './call-orchestrator.js';
17
+ import {
18
+ extractPromptSpeakerMetadata,
19
+ SpeakerIdentityTracker,
20
+ type PromptSpeakerContext,
21
+ } from './speaker-identification.js';
17
22
 
18
23
  const log = getLogger('relay-server');
19
24
 
@@ -33,6 +38,23 @@ export interface RelayPromptMessage {
33
38
  voicePrompt: string;
34
39
  lang: string;
35
40
  last: boolean;
41
+ speakerId?: string;
42
+ speakerLabel?: string;
43
+ speakerName?: string;
44
+ speakerConfidence?: number;
45
+ participantId?: string;
46
+ participant?: {
47
+ id?: string;
48
+ name?: string;
49
+ };
50
+ speaker?: {
51
+ id?: string;
52
+ label?: string;
53
+ name?: string;
54
+ confidence?: number;
55
+ };
56
+ metadata?: Record<string, unknown>;
57
+ providerMetadata?: Record<string, unknown>;
36
58
  }
37
59
 
38
60
  export interface RelayInterruptMessage {
@@ -88,15 +110,22 @@ export const activeRelayConnections = new Map<string, RelayConnection>();
88
110
  export class RelayConnection {
89
111
  private ws: ServerWebSocket<RelayWebSocketData>;
90
112
  private callSessionId: string;
91
- private conversationHistory: Array<{ role: 'caller' | 'assistant'; text: string; timestamp: number }>;
113
+ private conversationHistory: Array<{
114
+ role: 'caller' | 'assistant';
115
+ text: string;
116
+ timestamp: number;
117
+ speaker?: PromptSpeakerContext;
118
+ }>;
92
119
  private abortController: AbortController;
93
120
  private orchestrator: CallOrchestrator | null = null;
121
+ private speakerIdentityTracker: SpeakerIdentityTracker;
94
122
 
95
123
  constructor(ws: ServerWebSocket<RelayWebSocketData>, callSessionId: string) {
96
124
  this.ws = ws;
97
125
  this.callSessionId = callSessionId;
98
126
  this.conversationHistory = [];
99
127
  this.abortController = new AbortController();
128
+ this.speakerIdentityTracker = new SpeakerIdentityTracker();
100
129
  }
101
130
 
102
131
  /**
@@ -162,8 +191,8 @@ export class RelayConnection {
162
191
  /**
163
192
  * Get the conversation history for context.
164
193
  */
165
- getConversationHistory(): Array<{ role: string; text: string }> {
166
- return this.conversationHistory.map(({ role, text }) => ({ role, text }));
194
+ getConversationHistory(): Array<{ role: string; text: string; speaker?: PromptSpeakerContext }> {
195
+ return this.conversationHistory.map(({ role, text, speaker }) => ({ role, text, speaker }));
167
196
  }
168
197
 
169
198
  /**
@@ -236,22 +265,30 @@ export class RelayConnection {
236
265
  'Caller transcript received (final)',
237
266
  );
238
267
 
268
+ const speakerMetadata = extractPromptSpeakerMetadata(msg as unknown as Record<string, unknown>);
269
+ const speaker = this.speakerIdentityTracker.identifySpeaker(speakerMetadata);
270
+
239
271
  // Record in conversation history
240
272
  this.conversationHistory.push({
241
273
  role: 'caller',
242
274
  text: msg.voicePrompt,
243
275
  timestamp: Date.now(),
276
+ speaker,
244
277
  });
245
278
 
246
279
  // Record event
247
280
  recordCallEvent(this.callSessionId, 'caller_spoke', {
248
281
  transcript: msg.voicePrompt,
249
282
  lang: msg.lang,
283
+ speakerId: speaker.speakerId,
284
+ speakerLabel: speaker.speakerLabel,
285
+ speakerConfidence: speaker.speakerConfidence,
286
+ speakerSource: speaker.source,
250
287
  });
251
288
 
252
289
  // Route to orchestrator for LLM-driven response
253
290
  if (this.orchestrator) {
254
- await this.orchestrator.handleCallerUtterance(msg.voicePrompt);
291
+ await this.orchestrator.handleCallerUtterance(msg.voicePrompt, speaker);
255
292
  } else {
256
293
  // Fallback if orchestrator not yet initialized
257
294
  this.sendTextToken('I\'m still setting up. Please hold.', true);