@livekit/agents 0.7.9 → 1.0.0-next.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 (627) hide show
  1. package/dist/_exceptions.cjs +109 -0
  2. package/dist/_exceptions.cjs.map +1 -0
  3. package/dist/_exceptions.d.cts +64 -0
  4. package/dist/_exceptions.d.ts +64 -0
  5. package/dist/_exceptions.d.ts.map +1 -0
  6. package/dist/_exceptions.js +80 -0
  7. package/dist/_exceptions.js.map +1 -0
  8. package/dist/audio.cjs +10 -3
  9. package/dist/audio.cjs.map +1 -1
  10. package/dist/audio.d.cts +2 -0
  11. package/dist/audio.d.ts +2 -0
  12. package/dist/audio.d.ts.map +1 -1
  13. package/dist/audio.js +8 -2
  14. package/dist/audio.js.map +1 -1
  15. package/dist/cli.cjs +25 -0
  16. package/dist/cli.cjs.map +1 -1
  17. package/dist/cli.d.ts.map +1 -1
  18. package/dist/cli.js +25 -0
  19. package/dist/cli.js.map +1 -1
  20. package/dist/constants.cjs +6 -3
  21. package/dist/constants.cjs.map +1 -1
  22. package/dist/constants.d.cts +2 -1
  23. package/dist/constants.d.ts +2 -1
  24. package/dist/constants.d.ts.map +1 -1
  25. package/dist/constants.js +4 -2
  26. package/dist/constants.js.map +1 -1
  27. package/dist/http_server.cjs.map +1 -1
  28. package/dist/http_server.d.cts +1 -0
  29. package/dist/http_server.d.ts +1 -0
  30. package/dist/http_server.d.ts.map +1 -1
  31. package/dist/http_server.js.map +1 -1
  32. package/dist/index.cjs +27 -20
  33. package/dist/index.cjs.map +1 -1
  34. package/dist/index.d.cts +13 -10
  35. package/dist/index.d.ts +13 -10
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +15 -11
  38. package/dist/index.js.map +1 -1
  39. package/dist/inference_runner.cjs +0 -1
  40. package/dist/inference_runner.cjs.map +1 -1
  41. package/dist/inference_runner.d.cts +2 -3
  42. package/dist/inference_runner.d.ts +2 -3
  43. package/dist/inference_runner.d.ts.map +1 -1
  44. package/dist/inference_runner.js +0 -1
  45. package/dist/inference_runner.js.map +1 -1
  46. package/dist/ipc/inference_proc_executor.cjs +2 -2
  47. package/dist/ipc/inference_proc_executor.cjs.map +1 -1
  48. package/dist/ipc/inference_proc_executor.js +2 -2
  49. package/dist/ipc/inference_proc_executor.js.map +1 -1
  50. package/dist/ipc/job_executor.cjs.map +1 -1
  51. package/dist/ipc/job_executor.js.map +1 -1
  52. package/dist/ipc/job_proc_executor.cjs +1 -0
  53. package/dist/ipc/job_proc_executor.cjs.map +1 -1
  54. package/dist/ipc/job_proc_executor.js +1 -0
  55. package/dist/ipc/job_proc_executor.js.map +1 -1
  56. package/dist/ipc/job_proc_lazy_main.cjs +1 -1
  57. package/dist/ipc/job_proc_lazy_main.cjs.map +1 -1
  58. package/dist/ipc/job_proc_lazy_main.js +1 -1
  59. package/dist/ipc/job_proc_lazy_main.js.map +1 -1
  60. package/dist/ipc/supervised_proc.d.cts +1 -1
  61. package/dist/ipc/supervised_proc.d.ts +1 -1
  62. package/dist/ipc/supervised_proc.d.ts.map +1 -1
  63. package/dist/job.cjs +14 -2
  64. package/dist/job.cjs.map +1 -1
  65. package/dist/job.d.cts +8 -0
  66. package/dist/job.d.ts +8 -0
  67. package/dist/job.d.ts.map +1 -1
  68. package/dist/job.js +12 -1
  69. package/dist/job.js.map +1 -1
  70. package/dist/llm/chat_context.cjs +332 -82
  71. package/dist/llm/chat_context.cjs.map +1 -1
  72. package/dist/llm/chat_context.d.cts +152 -48
  73. package/dist/llm/chat_context.d.ts +152 -48
  74. package/dist/llm/chat_context.d.ts.map +1 -1
  75. package/dist/llm/chat_context.js +327 -81
  76. package/dist/llm/chat_context.js.map +1 -1
  77. package/dist/llm/chat_context.test.cjs +380 -0
  78. package/dist/llm/chat_context.test.cjs.map +1 -0
  79. package/dist/llm/chat_context.test.js +385 -0
  80. package/dist/llm/chat_context.test.js.map +1 -0
  81. package/dist/llm/index.cjs +37 -8
  82. package/dist/llm/index.cjs.map +1 -1
  83. package/dist/llm/index.d.cts +7 -3
  84. package/dist/llm/index.d.ts +7 -3
  85. package/dist/llm/index.d.ts.map +1 -1
  86. package/dist/llm/index.js +39 -9
  87. package/dist/llm/index.js.map +1 -1
  88. package/dist/llm/llm.cjs +98 -33
  89. package/dist/llm/llm.cjs.map +1 -1
  90. package/dist/llm/llm.d.cts +50 -24
  91. package/dist/llm/llm.d.ts +50 -24
  92. package/dist/llm/llm.d.ts.map +1 -1
  93. package/dist/llm/llm.js +99 -33
  94. package/dist/llm/llm.js.map +1 -1
  95. package/dist/llm/provider_format/google.cjs +128 -0
  96. package/dist/llm/provider_format/google.cjs.map +1 -0
  97. package/dist/llm/provider_format/google.d.cts +6 -0
  98. package/dist/llm/provider_format/google.d.ts +6 -0
  99. package/dist/llm/provider_format/google.d.ts.map +1 -0
  100. package/dist/llm/provider_format/google.js +104 -0
  101. package/dist/llm/provider_format/google.js.map +1 -0
  102. package/dist/llm/provider_format/google.test.cjs +676 -0
  103. package/dist/llm/provider_format/google.test.cjs.map +1 -0
  104. package/dist/llm/provider_format/google.test.js +675 -0
  105. package/dist/llm/provider_format/google.test.js.map +1 -0
  106. package/dist/llm/provider_format/index.cjs +40 -0
  107. package/dist/llm/provider_format/index.cjs.map +1 -0
  108. package/dist/llm/provider_format/index.d.cts +4 -0
  109. package/dist/llm/provider_format/index.d.ts +4 -0
  110. package/dist/llm/provider_format/index.d.ts.map +1 -0
  111. package/dist/llm/provider_format/index.js +16 -0
  112. package/dist/llm/provider_format/index.js.map +1 -0
  113. package/dist/llm/provider_format/openai.cjs +116 -0
  114. package/dist/llm/provider_format/openai.cjs.map +1 -0
  115. package/dist/llm/provider_format/openai.d.cts +3 -0
  116. package/dist/llm/provider_format/openai.d.ts +3 -0
  117. package/dist/llm/provider_format/openai.d.ts.map +1 -0
  118. package/dist/llm/provider_format/openai.js +92 -0
  119. package/dist/llm/provider_format/openai.js.map +1 -0
  120. package/dist/llm/provider_format/openai.test.cjs +490 -0
  121. package/dist/llm/provider_format/openai.test.cjs.map +1 -0
  122. package/dist/llm/provider_format/openai.test.js +489 -0
  123. package/dist/llm/provider_format/openai.test.js.map +1 -0
  124. package/dist/llm/provider_format/utils.cjs +146 -0
  125. package/dist/llm/provider_format/utils.cjs.map +1 -0
  126. package/dist/llm/provider_format/utils.d.cts +38 -0
  127. package/dist/llm/provider_format/utils.d.ts +38 -0
  128. package/dist/llm/provider_format/utils.d.ts.map +1 -0
  129. package/dist/llm/provider_format/utils.js +122 -0
  130. package/dist/llm/provider_format/utils.js.map +1 -0
  131. package/dist/llm/realtime.cjs +77 -0
  132. package/dist/llm/realtime.cjs.map +1 -0
  133. package/dist/llm/realtime.d.cts +98 -0
  134. package/dist/llm/realtime.d.ts +98 -0
  135. package/dist/llm/realtime.d.ts.map +1 -0
  136. package/dist/llm/realtime.js +52 -0
  137. package/dist/llm/realtime.js.map +1 -0
  138. package/dist/llm/remote_chat_context.cjs +112 -0
  139. package/dist/llm/remote_chat_context.cjs.map +1 -0
  140. package/dist/llm/remote_chat_context.d.cts +23 -0
  141. package/dist/llm/remote_chat_context.d.ts +23 -0
  142. package/dist/llm/remote_chat_context.d.ts.map +1 -0
  143. package/dist/llm/remote_chat_context.js +88 -0
  144. package/dist/llm/remote_chat_context.js.map +1 -0
  145. package/dist/llm/remote_chat_context.test.cjs +225 -0
  146. package/dist/llm/remote_chat_context.test.cjs.map +1 -0
  147. package/dist/llm/remote_chat_context.test.js +224 -0
  148. package/dist/llm/remote_chat_context.test.js.map +1 -0
  149. package/dist/llm/tool_context.cjs +111 -0
  150. package/dist/llm/tool_context.cjs.map +1 -0
  151. package/dist/llm/tool_context.d.cts +125 -0
  152. package/dist/llm/tool_context.d.ts +125 -0
  153. package/dist/llm/tool_context.d.ts.map +1 -0
  154. package/dist/llm/tool_context.js +80 -0
  155. package/dist/llm/tool_context.js.map +1 -0
  156. package/dist/llm/tool_context.test.cjs +162 -0
  157. package/dist/llm/tool_context.test.cjs.map +1 -0
  158. package/dist/llm/tool_context.test.js +161 -0
  159. package/dist/llm/tool_context.test.js.map +1 -0
  160. package/dist/llm/tool_context.type.test.cjs +92 -0
  161. package/dist/llm/tool_context.type.test.cjs.map +1 -0
  162. package/dist/llm/tool_context.type.test.js +91 -0
  163. package/dist/llm/tool_context.type.test.js.map +1 -0
  164. package/dist/llm/utils.cjs +260 -0
  165. package/dist/llm/utils.cjs.map +1 -0
  166. package/dist/llm/utils.d.cts +42 -0
  167. package/dist/llm/utils.d.ts +42 -0
  168. package/dist/llm/utils.d.ts.map +1 -0
  169. package/dist/llm/utils.js +223 -0
  170. package/dist/llm/utils.js.map +1 -0
  171. package/dist/llm/utils.test.cjs +513 -0
  172. package/dist/llm/utils.test.cjs.map +1 -0
  173. package/dist/llm/utils.test.js +490 -0
  174. package/dist/llm/utils.test.js.map +1 -0
  175. package/dist/metrics/base.cjs +0 -27
  176. package/dist/metrics/base.cjs.map +1 -1
  177. package/dist/metrics/base.d.cts +105 -63
  178. package/dist/metrics/base.d.ts +105 -63
  179. package/dist/metrics/base.d.ts.map +1 -1
  180. package/dist/metrics/base.js +0 -19
  181. package/dist/metrics/base.js.map +1 -1
  182. package/dist/metrics/index.cjs +0 -3
  183. package/dist/metrics/index.cjs.map +1 -1
  184. package/dist/metrics/index.d.cts +2 -3
  185. package/dist/metrics/index.d.ts +2 -3
  186. package/dist/metrics/index.d.ts.map +1 -1
  187. package/dist/metrics/index.js +0 -2
  188. package/dist/metrics/index.js.map +1 -1
  189. package/dist/metrics/usage_collector.cjs +17 -12
  190. package/dist/metrics/usage_collector.cjs.map +1 -1
  191. package/dist/metrics/usage_collector.d.cts +3 -2
  192. package/dist/metrics/usage_collector.d.ts +3 -2
  193. package/dist/metrics/usage_collector.d.ts.map +1 -1
  194. package/dist/metrics/usage_collector.js +17 -12
  195. package/dist/metrics/usage_collector.js.map +1 -1
  196. package/dist/metrics/utils.cjs +22 -59
  197. package/dist/metrics/utils.cjs.map +1 -1
  198. package/dist/metrics/utils.d.cts +1 -8
  199. package/dist/metrics/utils.d.ts +1 -8
  200. package/dist/metrics/utils.d.ts.map +1 -1
  201. package/dist/metrics/utils.js +22 -52
  202. package/dist/metrics/utils.js.map +1 -1
  203. package/dist/multimodal/index.cjs +0 -2
  204. package/dist/multimodal/index.cjs.map +1 -1
  205. package/dist/multimodal/index.d.cts +0 -1
  206. package/dist/multimodal/index.d.ts +0 -1
  207. package/dist/multimodal/index.d.ts.map +1 -1
  208. package/dist/multimodal/index.js +0 -1
  209. package/dist/multimodal/index.js.map +1 -1
  210. package/dist/plugin.cjs +24 -8
  211. package/dist/plugin.cjs.map +1 -1
  212. package/dist/plugin.d.cts +18 -4
  213. package/dist/plugin.d.ts +18 -4
  214. package/dist/plugin.d.ts.map +1 -1
  215. package/dist/plugin.js +22 -7
  216. package/dist/plugin.js.map +1 -1
  217. package/dist/stream/deferred_stream.cjs +98 -0
  218. package/dist/stream/deferred_stream.cjs.map +1 -0
  219. package/dist/stream/deferred_stream.d.cts +27 -0
  220. package/dist/stream/deferred_stream.d.ts +27 -0
  221. package/dist/stream/deferred_stream.d.ts.map +1 -0
  222. package/dist/stream/deferred_stream.js +73 -0
  223. package/dist/stream/deferred_stream.js.map +1 -0
  224. package/dist/stream/deferred_stream.test.cjs +527 -0
  225. package/dist/stream/deferred_stream.test.cjs.map +1 -0
  226. package/dist/stream/deferred_stream.test.js +526 -0
  227. package/dist/stream/deferred_stream.test.js.map +1 -0
  228. package/dist/stream/identity_transform.cjs +42 -0
  229. package/dist/stream/identity_transform.cjs.map +1 -0
  230. package/dist/stream/identity_transform.d.cts +6 -0
  231. package/dist/stream/identity_transform.d.ts +6 -0
  232. package/dist/stream/identity_transform.d.ts.map +1 -0
  233. package/dist/stream/identity_transform.js +18 -0
  234. package/dist/stream/identity_transform.js.map +1 -0
  235. package/dist/stream/identity_transform.test.cjs +125 -0
  236. package/dist/stream/identity_transform.test.cjs.map +1 -0
  237. package/dist/stream/identity_transform.test.js +124 -0
  238. package/dist/stream/identity_transform.test.js.map +1 -0
  239. package/dist/stream/index.cjs +38 -0
  240. package/dist/stream/index.cjs.map +1 -0
  241. package/dist/stream/index.d.cts +5 -0
  242. package/dist/stream/index.d.ts +5 -0
  243. package/dist/stream/index.d.ts.map +1 -0
  244. package/dist/stream/index.js +11 -0
  245. package/dist/stream/index.js.map +1 -0
  246. package/dist/stream/merge_readable_streams.cjs +59 -0
  247. package/dist/stream/merge_readable_streams.cjs.map +1 -0
  248. package/dist/stream/merge_readable_streams.d.cts +4 -0
  249. package/dist/stream/merge_readable_streams.d.ts +4 -0
  250. package/dist/stream/merge_readable_streams.d.ts.map +1 -0
  251. package/dist/stream/merge_readable_streams.js +35 -0
  252. package/dist/stream/merge_readable_streams.js.map +1 -0
  253. package/dist/stream/stream_channel.cjs +47 -0
  254. package/dist/stream/stream_channel.cjs.map +1 -0
  255. package/dist/stream/stream_channel.d.cts +9 -0
  256. package/dist/stream/stream_channel.d.ts +9 -0
  257. package/dist/stream/stream_channel.d.ts.map +1 -0
  258. package/dist/stream/stream_channel.js +23 -0
  259. package/dist/stream/stream_channel.js.map +1 -0
  260. package/dist/stream/stream_channel.test.cjs +97 -0
  261. package/dist/stream/stream_channel.test.cjs.map +1 -0
  262. package/dist/stream/stream_channel.test.js +96 -0
  263. package/dist/stream/stream_channel.test.js.map +1 -0
  264. package/dist/stt/stream_adapter.cjs +3 -4
  265. package/dist/stt/stream_adapter.cjs.map +1 -1
  266. package/dist/stt/stream_adapter.d.cts +1 -0
  267. package/dist/stt/stream_adapter.d.ts +1 -0
  268. package/dist/stt/stream_adapter.d.ts.map +1 -1
  269. package/dist/stt/stream_adapter.js +3 -4
  270. package/dist/stt/stream_adapter.js.map +1 -1
  271. package/dist/stt/stt.cjs +101 -10
  272. package/dist/stt/stt.cjs.map +1 -1
  273. package/dist/stt/stt.d.cts +26 -5
  274. package/dist/stt/stt.d.ts +26 -5
  275. package/dist/stt/stt.d.ts.map +1 -1
  276. package/dist/stt/stt.js +102 -11
  277. package/dist/stt/stt.js.map +1 -1
  278. package/dist/tokenize/basic/basic.cjs +10 -5
  279. package/dist/tokenize/basic/basic.cjs.map +1 -1
  280. package/dist/tokenize/basic/basic.d.cts +7 -1
  281. package/dist/tokenize/basic/basic.d.ts +7 -1
  282. package/dist/tokenize/basic/basic.d.ts.map +1 -1
  283. package/dist/tokenize/basic/basic.js +10 -5
  284. package/dist/tokenize/basic/basic.js.map +1 -1
  285. package/dist/tokenize/basic/sentence.cjs +14 -6
  286. package/dist/tokenize/basic/sentence.cjs.map +1 -1
  287. package/dist/tokenize/basic/sentence.d.cts +1 -1
  288. package/dist/tokenize/basic/sentence.d.ts +1 -1
  289. package/dist/tokenize/basic/sentence.d.ts.map +1 -1
  290. package/dist/tokenize/basic/sentence.js +14 -6
  291. package/dist/tokenize/basic/sentence.js.map +1 -1
  292. package/dist/tokenize/token_stream.cjs +5 -3
  293. package/dist/tokenize/token_stream.cjs.map +1 -1
  294. package/dist/tokenize/token_stream.d.cts +1 -0
  295. package/dist/tokenize/token_stream.d.ts +1 -0
  296. package/dist/tokenize/token_stream.d.ts.map +1 -1
  297. package/dist/tokenize/token_stream.js +6 -4
  298. package/dist/tokenize/token_stream.js.map +1 -1
  299. package/dist/transcription.cjs +1 -2
  300. package/dist/transcription.cjs.map +1 -1
  301. package/dist/transcription.d.ts.map +1 -1
  302. package/dist/transcription.js +2 -3
  303. package/dist/transcription.js.map +1 -1
  304. package/dist/tts/index.cjs +2 -4
  305. package/dist/tts/index.cjs.map +1 -1
  306. package/dist/tts/index.d.cts +1 -1
  307. package/dist/tts/index.d.ts +1 -1
  308. package/dist/tts/index.d.ts.map +1 -1
  309. package/dist/tts/index.js +1 -3
  310. package/dist/tts/index.js.map +1 -1
  311. package/dist/tts/stream_adapter.cjs +26 -13
  312. package/dist/tts/stream_adapter.cjs.map +1 -1
  313. package/dist/tts/stream_adapter.d.cts +1 -1
  314. package/dist/tts/stream_adapter.d.ts +1 -1
  315. package/dist/tts/stream_adapter.d.ts.map +1 -1
  316. package/dist/tts/stream_adapter.js +27 -14
  317. package/dist/tts/stream_adapter.js.map +1 -1
  318. package/dist/tts/tts.cjs +157 -25
  319. package/dist/tts/tts.cjs.map +1 -1
  320. package/dist/tts/tts.d.cts +29 -5
  321. package/dist/tts/tts.d.ts +29 -5
  322. package/dist/tts/tts.d.ts.map +1 -1
  323. package/dist/tts/tts.js +157 -24
  324. package/dist/tts/tts.js.map +1 -1
  325. package/dist/types.cjs +60 -0
  326. package/dist/types.cjs.map +1 -0
  327. package/dist/types.d.cts +13 -0
  328. package/dist/types.d.ts +13 -0
  329. package/dist/types.d.ts.map +1 -0
  330. package/dist/types.js +35 -0
  331. package/dist/types.js.map +1 -0
  332. package/dist/utils.cjs +281 -27
  333. package/dist/utils.cjs.map +1 -1
  334. package/dist/utils.d.cts +134 -9
  335. package/dist/utils.d.ts +134 -9
  336. package/dist/utils.d.ts.map +1 -1
  337. package/dist/utils.js +265 -26
  338. package/dist/utils.js.map +1 -1
  339. package/dist/utils.test.cjs +492 -0
  340. package/dist/utils.test.cjs.map +1 -0
  341. package/dist/utils.test.js +498 -0
  342. package/dist/utils.test.js.map +1 -0
  343. package/dist/vad.cjs +76 -20
  344. package/dist/vad.cjs.map +1 -1
  345. package/dist/vad.d.cts +25 -5
  346. package/dist/vad.d.ts +25 -5
  347. package/dist/vad.d.ts.map +1 -1
  348. package/dist/vad.js +76 -20
  349. package/dist/vad.js.map +1 -1
  350. package/dist/voice/agent.cjs +245 -0
  351. package/dist/voice/agent.cjs.map +1 -0
  352. package/dist/voice/agent.d.cts +78 -0
  353. package/dist/voice/agent.d.ts +78 -0
  354. package/dist/voice/agent.d.ts.map +1 -0
  355. package/dist/voice/agent.js +220 -0
  356. package/dist/voice/agent.js.map +1 -0
  357. package/dist/voice/agent.test.cjs +61 -0
  358. package/dist/voice/agent.test.cjs.map +1 -0
  359. package/dist/voice/agent.test.js +60 -0
  360. package/dist/voice/agent.test.js.map +1 -0
  361. package/dist/voice/agent_activity.cjs +1453 -0
  362. package/dist/voice/agent_activity.cjs.map +1 -0
  363. package/dist/voice/agent_activity.d.cts +94 -0
  364. package/dist/voice/agent_activity.d.ts +94 -0
  365. package/dist/voice/agent_activity.d.ts.map +1 -0
  366. package/dist/voice/agent_activity.js +1449 -0
  367. package/dist/voice/agent_activity.js.map +1 -0
  368. package/dist/voice/agent_session.cjs +312 -0
  369. package/dist/voice/agent_session.cjs.map +1 -0
  370. package/dist/voice/agent_session.d.cts +121 -0
  371. package/dist/voice/agent_session.d.ts +121 -0
  372. package/dist/voice/agent_session.d.ts.map +1 -0
  373. package/dist/voice/agent_session.js +295 -0
  374. package/dist/voice/agent_session.js.map +1 -0
  375. package/dist/voice/audio_recognition.cjs +375 -0
  376. package/dist/voice/audio_recognition.cjs.map +1 -0
  377. package/dist/voice/audio_recognition.d.cts +80 -0
  378. package/dist/voice/audio_recognition.d.ts +80 -0
  379. package/dist/voice/audio_recognition.d.ts.map +1 -0
  380. package/dist/voice/audio_recognition.js +351 -0
  381. package/dist/voice/audio_recognition.js.map +1 -0
  382. package/dist/voice/events.cjs +145 -0
  383. package/dist/voice/events.cjs.map +1 -0
  384. package/dist/voice/events.d.cts +124 -0
  385. package/dist/voice/events.d.ts +124 -0
  386. package/dist/voice/events.d.ts.map +1 -0
  387. package/dist/voice/events.js +110 -0
  388. package/dist/voice/events.js.map +1 -0
  389. package/dist/voice/generation.cjs +700 -0
  390. package/dist/voice/generation.cjs.map +1 -0
  391. package/dist/voice/generation.d.cts +115 -0
  392. package/dist/voice/generation.d.ts +115 -0
  393. package/dist/voice/generation.d.ts.map +1 -0
  394. package/dist/voice/generation.js +672 -0
  395. package/dist/voice/generation.js.map +1 -0
  396. package/dist/voice/index.cjs +40 -0
  397. package/dist/voice/index.cjs.map +1 -0
  398. package/dist/voice/index.d.cts +5 -0
  399. package/dist/voice/index.d.ts +5 -0
  400. package/dist/voice/index.d.ts.map +1 -0
  401. package/dist/voice/index.js +11 -0
  402. package/dist/voice/index.js.map +1 -0
  403. package/dist/voice/io.cjs +245 -0
  404. package/dist/voice/io.cjs.map +1 -0
  405. package/dist/voice/io.d.cts +101 -0
  406. package/dist/voice/io.d.ts +101 -0
  407. package/dist/voice/io.d.ts.map +1 -0
  408. package/dist/voice/io.js +217 -0
  409. package/dist/voice/io.js.map +1 -0
  410. package/dist/voice/room_io/_input.cjs +121 -0
  411. package/dist/voice/room_io/_input.cjs.map +1 -0
  412. package/dist/voice/room_io/_input.d.cts +24 -0
  413. package/dist/voice/room_io/_input.d.ts +24 -0
  414. package/dist/voice/room_io/_input.d.ts.map +1 -0
  415. package/dist/voice/room_io/_input.js +102 -0
  416. package/dist/voice/room_io/_input.js.map +1 -0
  417. package/dist/voice/room_io/_output.cjs +358 -0
  418. package/dist/voice/room_io/_output.cjs.map +1 -0
  419. package/dist/voice/room_io/_output.d.cts +75 -0
  420. package/dist/voice/room_io/_output.d.ts +75 -0
  421. package/dist/voice/room_io/_output.d.ts.map +1 -0
  422. package/dist/voice/room_io/_output.js +342 -0
  423. package/dist/voice/room_io/_output.js.map +1 -0
  424. package/dist/voice/room_io/index.cjs +25 -0
  425. package/dist/voice/room_io/index.cjs.map +1 -0
  426. package/dist/voice/room_io/index.d.cts +3 -0
  427. package/dist/voice/room_io/index.d.ts +3 -0
  428. package/dist/voice/room_io/index.d.ts.map +1 -0
  429. package/dist/voice/room_io/index.js +3 -0
  430. package/dist/voice/room_io/index.js.map +1 -0
  431. package/dist/voice/room_io/room_io.cjs +370 -0
  432. package/dist/voice/room_io/room_io.cjs.map +1 -0
  433. package/dist/voice/room_io/room_io.d.cts +73 -0
  434. package/dist/voice/room_io/room_io.d.ts +73 -0
  435. package/dist/voice/room_io/room_io.d.ts.map +1 -0
  436. package/dist/voice/room_io/room_io.js +361 -0
  437. package/dist/voice/room_io/room_io.js.map +1 -0
  438. package/dist/{pipeline/index.cjs → voice/run_context.cjs} +16 -11
  439. package/dist/voice/run_context.cjs.map +1 -0
  440. package/dist/voice/run_context.d.cts +12 -0
  441. package/dist/voice/run_context.d.ts +12 -0
  442. package/dist/voice/run_context.d.ts.map +1 -0
  443. package/dist/voice/run_context.js +14 -0
  444. package/dist/voice/run_context.js.map +1 -0
  445. package/dist/voice/speech_handle.cjs +105 -0
  446. package/dist/voice/speech_handle.cjs.map +1 -0
  447. package/dist/voice/speech_handle.d.cts +46 -0
  448. package/dist/voice/speech_handle.d.ts +46 -0
  449. package/dist/voice/speech_handle.d.ts.map +1 -0
  450. package/dist/voice/speech_handle.js +81 -0
  451. package/dist/voice/speech_handle.js.map +1 -0
  452. package/dist/voice/transcription/_utils.cjs +45 -0
  453. package/dist/voice/transcription/_utils.cjs.map +1 -0
  454. package/dist/voice/transcription/_utils.d.cts +3 -0
  455. package/dist/voice/transcription/_utils.d.ts +3 -0
  456. package/dist/voice/transcription/_utils.d.ts.map +1 -0
  457. package/dist/voice/transcription/_utils.js +21 -0
  458. package/dist/voice/transcription/_utils.js.map +1 -0
  459. package/dist/voice/transcription/index.cjs +23 -0
  460. package/dist/voice/transcription/index.cjs.map +1 -0
  461. package/dist/voice/transcription/index.d.cts +2 -0
  462. package/dist/voice/transcription/index.d.ts +2 -0
  463. package/dist/voice/transcription/index.d.ts.map +1 -0
  464. package/dist/voice/transcription/index.js +2 -0
  465. package/dist/voice/transcription/index.js.map +1 -0
  466. package/dist/voice/transcription/synchronizer.cjs +380 -0
  467. package/dist/voice/transcription/synchronizer.cjs.map +1 -0
  468. package/dist/voice/transcription/synchronizer.d.cts +86 -0
  469. package/dist/voice/transcription/synchronizer.d.ts +86 -0
  470. package/dist/voice/transcription/synchronizer.d.ts.map +1 -0
  471. package/dist/voice/transcription/synchronizer.js +355 -0
  472. package/dist/voice/transcription/synchronizer.js.map +1 -0
  473. package/dist/worker.cjs +22 -4
  474. package/dist/worker.cjs.map +1 -1
  475. package/dist/worker.d.cts +1 -1
  476. package/dist/worker.d.ts +1 -1
  477. package/dist/worker.d.ts.map +1 -1
  478. package/dist/worker.js +22 -4
  479. package/dist/worker.js.map +1 -1
  480. package/package.json +9 -2
  481. package/src/_exceptions.ts +137 -0
  482. package/src/audio.ts +12 -1
  483. package/src/cli.ts +37 -0
  484. package/src/constants.ts +2 -1
  485. package/src/http_server.ts +1 -0
  486. package/src/index.ts +13 -10
  487. package/src/inference_runner.ts +2 -3
  488. package/src/ipc/inference_proc_executor.ts +2 -2
  489. package/src/ipc/job_executor.ts +1 -1
  490. package/src/ipc/job_proc_executor.ts +1 -1
  491. package/src/ipc/job_proc_lazy_main.ts +1 -1
  492. package/src/job.ts +18 -0
  493. package/src/llm/__snapshots__/chat_context.test.ts.snap +527 -0
  494. package/src/llm/__snapshots__/tool_context.test.ts.snap +177 -0
  495. package/src/llm/__snapshots__/utils.test.ts.snap +65 -0
  496. package/src/llm/chat_context.test.ts +450 -0
  497. package/src/llm/chat_context.ts +501 -103
  498. package/src/llm/index.ts +53 -18
  499. package/src/llm/llm.ts +149 -50
  500. package/src/llm/provider_format/google.test.ts +772 -0
  501. package/src/llm/provider_format/google.ts +130 -0
  502. package/src/llm/provider_format/index.ts +23 -0
  503. package/src/llm/provider_format/openai.test.ts +581 -0
  504. package/src/llm/provider_format/openai.ts +118 -0
  505. package/src/llm/provider_format/utils.ts +183 -0
  506. package/src/llm/realtime.ts +151 -0
  507. package/src/llm/remote_chat_context.test.ts +290 -0
  508. package/src/llm/remote_chat_context.ts +114 -0
  509. package/src/llm/tool_context.test.ts +198 -0
  510. package/src/llm/tool_context.ts +259 -0
  511. package/src/llm/tool_context.type.test.ts +115 -0
  512. package/src/llm/utils.test.ts +670 -0
  513. package/src/llm/utils.ts +324 -0
  514. package/src/metrics/base.ts +110 -78
  515. package/src/metrics/index.ts +3 -9
  516. package/src/metrics/usage_collector.ts +19 -13
  517. package/src/metrics/utils.ts +24 -69
  518. package/src/multimodal/index.ts +0 -1
  519. package/src/plugin.ts +26 -8
  520. package/src/stream/deferred_stream.test.ts +755 -0
  521. package/src/stream/deferred_stream.ts +110 -0
  522. package/src/stream/identity_transform.test.ts +179 -0
  523. package/src/stream/identity_transform.ts +18 -0
  524. package/src/stream/index.ts +7 -0
  525. package/src/stream/merge_readable_streams.ts +40 -0
  526. package/src/stream/stream_channel.test.ts +129 -0
  527. package/src/stream/stream_channel.ts +32 -0
  528. package/src/stt/stream_adapter.ts +3 -5
  529. package/src/stt/stt.ts +135 -17
  530. package/src/tokenize/basic/basic.ts +13 -5
  531. package/src/tokenize/basic/sentence.ts +20 -6
  532. package/src/tokenize/token_stream.ts +7 -4
  533. package/src/transcription.ts +2 -3
  534. package/src/tts/index.ts +0 -1
  535. package/src/tts/stream_adapter.ts +42 -16
  536. package/src/tts/tts.ts +203 -21
  537. package/src/types.ts +42 -0
  538. package/src/utils.test.ts +658 -0
  539. package/src/utils.ts +375 -44
  540. package/src/vad.ts +90 -22
  541. package/src/voice/agent.test.ts +80 -0
  542. package/src/voice/agent.ts +332 -0
  543. package/src/voice/agent_activity.ts +1913 -0
  544. package/src/voice/agent_session.ts +460 -0
  545. package/src/voice/audio_recognition.ts +474 -0
  546. package/src/voice/events.ts +252 -0
  547. package/src/voice/generation.ts +881 -0
  548. package/src/voice/index.ts +7 -0
  549. package/src/voice/io.ts +304 -0
  550. package/src/voice/room_io/_input.ts +144 -0
  551. package/src/voice/room_io/_output.ts +436 -0
  552. package/src/voice/room_io/index.ts +5 -0
  553. package/src/voice/room_io/room_io.ts +495 -0
  554. package/src/voice/run_context.ts +20 -0
  555. package/src/voice/speech_handle.ts +104 -0
  556. package/src/voice/transcription/_utils.ts +25 -0
  557. package/src/voice/transcription/index.ts +4 -0
  558. package/src/voice/transcription/synchronizer.ts +478 -0
  559. package/src/worker.ts +22 -2
  560. package/dist/llm/function_context.cjs +0 -103
  561. package/dist/llm/function_context.cjs.map +0 -1
  562. package/dist/llm/function_context.d.cts +0 -47
  563. package/dist/llm/function_context.d.ts +0 -47
  564. package/dist/llm/function_context.d.ts.map +0 -1
  565. package/dist/llm/function_context.js +0 -78
  566. package/dist/llm/function_context.js.map +0 -1
  567. package/dist/llm/function_context.test.cjs +0 -218
  568. package/dist/llm/function_context.test.cjs.map +0 -1
  569. package/dist/llm/function_context.test.js +0 -217
  570. package/dist/llm/function_context.test.js.map +0 -1
  571. package/dist/multimodal/multimodal_agent.cjs +0 -486
  572. package/dist/multimodal/multimodal_agent.cjs.map +0 -1
  573. package/dist/multimodal/multimodal_agent.d.cts +0 -48
  574. package/dist/multimodal/multimodal_agent.d.ts +0 -48
  575. package/dist/multimodal/multimodal_agent.d.ts.map +0 -1
  576. package/dist/multimodal/multimodal_agent.js +0 -461
  577. package/dist/multimodal/multimodal_agent.js.map +0 -1
  578. package/dist/pipeline/agent_output.cjs +0 -197
  579. package/dist/pipeline/agent_output.cjs.map +0 -1
  580. package/dist/pipeline/agent_output.d.cts +0 -33
  581. package/dist/pipeline/agent_output.d.ts +0 -33
  582. package/dist/pipeline/agent_output.d.ts.map +0 -1
  583. package/dist/pipeline/agent_output.js +0 -172
  584. package/dist/pipeline/agent_output.js.map +0 -1
  585. package/dist/pipeline/agent_playout.cjs +0 -175
  586. package/dist/pipeline/agent_playout.cjs.map +0 -1
  587. package/dist/pipeline/agent_playout.d.cts +0 -40
  588. package/dist/pipeline/agent_playout.d.ts +0 -40
  589. package/dist/pipeline/agent_playout.d.ts.map +0 -1
  590. package/dist/pipeline/agent_playout.js +0 -139
  591. package/dist/pipeline/agent_playout.js.map +0 -1
  592. package/dist/pipeline/human_input.cjs +0 -171
  593. package/dist/pipeline/human_input.cjs.map +0 -1
  594. package/dist/pipeline/human_input.d.cts +0 -30
  595. package/dist/pipeline/human_input.d.ts +0 -30
  596. package/dist/pipeline/human_input.d.ts.map +0 -1
  597. package/dist/pipeline/human_input.js +0 -146
  598. package/dist/pipeline/human_input.js.map +0 -1
  599. package/dist/pipeline/index.cjs.map +0 -1
  600. package/dist/pipeline/index.d.cts +0 -2
  601. package/dist/pipeline/index.d.ts +0 -2
  602. package/dist/pipeline/index.d.ts.map +0 -1
  603. package/dist/pipeline/index.js +0 -11
  604. package/dist/pipeline/index.js.map +0 -1
  605. package/dist/pipeline/pipeline_agent.cjs +0 -859
  606. package/dist/pipeline/pipeline_agent.cjs.map +0 -1
  607. package/dist/pipeline/pipeline_agent.d.cts +0 -150
  608. package/dist/pipeline/pipeline_agent.d.ts +0 -150
  609. package/dist/pipeline/pipeline_agent.d.ts.map +0 -1
  610. package/dist/pipeline/pipeline_agent.js +0 -837
  611. package/dist/pipeline/pipeline_agent.js.map +0 -1
  612. package/dist/pipeline/speech_handle.cjs +0 -176
  613. package/dist/pipeline/speech_handle.cjs.map +0 -1
  614. package/dist/pipeline/speech_handle.d.cts +0 -37
  615. package/dist/pipeline/speech_handle.d.ts +0 -37
  616. package/dist/pipeline/speech_handle.d.ts.map +0 -1
  617. package/dist/pipeline/speech_handle.js +0 -152
  618. package/dist/pipeline/speech_handle.js.map +0 -1
  619. package/src/llm/function_context.test.ts +0 -248
  620. package/src/llm/function_context.ts +0 -142
  621. package/src/multimodal/multimodal_agent.ts +0 -592
  622. package/src/pipeline/agent_output.ts +0 -219
  623. package/src/pipeline/agent_playout.ts +0 -192
  624. package/src/pipeline/human_input.ts +0 -188
  625. package/src/pipeline/index.ts +0 -15
  626. package/src/pipeline/pipeline_agent.ts +0 -1197
  627. package/src/pipeline/speech_handle.ts +0 -201
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var utils_exports = {};
20
+ __export(utils_exports, {
21
+ groupToolCalls: () => groupToolCalls
22
+ });
23
+ module.exports = __toCommonJS(utils_exports);
24
+ var import_log = require("../../log.cjs");
25
+ class ChatItemGroup {
26
+ message;
27
+ toolCalls;
28
+ toolOutputs;
29
+ logger = (0, import_log.log)();
30
+ constructor(params) {
31
+ this.message = params.message;
32
+ this.toolCalls = params.toolCalls;
33
+ this.toolOutputs = params.toolOutputs;
34
+ }
35
+ static create(params) {
36
+ const { message, toolCalls = [], toolOutputs = [] } = params ?? {};
37
+ return new ChatItemGroup({ message, toolCalls, toolOutputs });
38
+ }
39
+ get isEmpty() {
40
+ return this.message === void 0 && this.toolCalls.length === 0 && this.toolOutputs.length === 0;
41
+ }
42
+ add(item) {
43
+ if (item.type === "message") {
44
+ if (this.message) {
45
+ throw new Error("only one message is allowed in a group");
46
+ }
47
+ this.message = item;
48
+ } else if (item.type === "function_call") {
49
+ this.toolCalls.push(item);
50
+ } else if (item.type === "function_call_output") {
51
+ this.toolOutputs.push(item);
52
+ }
53
+ return this;
54
+ }
55
+ removeInvalidToolCalls() {
56
+ if (this.toolCalls.length === this.toolOutputs.length) {
57
+ return;
58
+ }
59
+ const toolCallIds = new Set(this.toolCalls.map((call) => call.callId));
60
+ const toolOutputIds = new Set(this.toolOutputs.map((output) => output.callId));
61
+ const validCallIds = intersection(toolCallIds, toolOutputIds);
62
+ this.toolCalls = this.toolCalls.filter((call) => {
63
+ if (validCallIds.has(call.callId)) return true;
64
+ this.logger.warn(
65
+ {
66
+ callId: call.callId,
67
+ toolName: call.name
68
+ },
69
+ "function call missing the corresponding function output, ignoring"
70
+ );
71
+ return false;
72
+ });
73
+ this.toolOutputs = this.toolOutputs.filter((output) => {
74
+ if (validCallIds.has(output.callId)) return true;
75
+ this.logger.warn(
76
+ {
77
+ callId: output.callId,
78
+ toolName: output.name
79
+ },
80
+ "function output missing the corresponding function call, ignoring"
81
+ );
82
+ return false;
83
+ });
84
+ }
85
+ flatten() {
86
+ const items = [];
87
+ if (this.message) items.push(this.message);
88
+ items.push(...this.toolCalls, ...this.toolOutputs);
89
+ return items;
90
+ }
91
+ }
92
+ function intersection(set1, set2) {
93
+ return new Set([...set1].filter((item) => set2.has(item)));
94
+ }
95
+ function groupToolCalls(chatCtx) {
96
+ const itemGroups = {};
97
+ const insertionOrder = {};
98
+ const toolOutputs = [];
99
+ const logger = (0, import_log.log)();
100
+ let insertionIndex = 0;
101
+ for (const item of chatCtx.items) {
102
+ const isAssistantMessage = item.type === "message" && item.role === "assistant";
103
+ const isFunctionCall = item.type === "function_call";
104
+ const isFunctionCallOutput = item.type === "function_call_output";
105
+ if (isAssistantMessage || isFunctionCall) {
106
+ const groupId = item.id.split("/")[0];
107
+ if (itemGroups[groupId] === void 0) {
108
+ itemGroups[groupId] = ChatItemGroup.create();
109
+ insertionOrder[groupId] = insertionIndex;
110
+ insertionIndex++;
111
+ }
112
+ itemGroups[groupId].add(item);
113
+ } else if (isFunctionCallOutput) {
114
+ toolOutputs.push(item);
115
+ } else {
116
+ itemGroups[item.id] = ChatItemGroup.create().add(item);
117
+ }
118
+ }
119
+ const callIdToGroup = {};
120
+ for (const group of Object.values(itemGroups)) {
121
+ for (const toolCall of group.toolCalls) {
122
+ callIdToGroup[toolCall.callId] = group;
123
+ }
124
+ }
125
+ for (const toolOutput of toolOutputs) {
126
+ const group = callIdToGroup[toolOutput.callId];
127
+ if (group === void 0) {
128
+ logger.warn(
129
+ { callId: toolOutput.callId, toolName: toolOutput.name },
130
+ "function output missing the corresponding function call, ignoring"
131
+ );
132
+ continue;
133
+ }
134
+ group.add(toolOutput);
135
+ }
136
+ for (const group of Object.values(itemGroups)) {
137
+ group.removeInvalidToolCalls();
138
+ }
139
+ const orderedGroups = Object.entries(itemGroups).sort((a, b) => insertionOrder[a[0]] - insertionOrder[b[0]]).map(([, group]) => group);
140
+ return orderedGroups;
141
+ }
142
+ // Annotate the CommonJS export names for ESM import in node:
143
+ 0 && (module.exports = {
144
+ groupToolCalls
145
+ });
146
+ //# sourceMappingURL=utils.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/llm/provider_format/utils.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { log } from '../../log.js';\nimport type {\n ChatContext,\n ChatItem,\n ChatMessage,\n FunctionCall,\n FunctionCallOutput,\n} from '../chat_context.js';\n\nclass ChatItemGroup {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n logger = log();\n\n constructor(params: {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n }) {\n this.message = params.message;\n this.toolCalls = params.toolCalls;\n this.toolOutputs = params.toolOutputs;\n }\n\n static create(params?: {\n message?: ChatMessage;\n toolCalls?: FunctionCall[];\n toolOutputs?: FunctionCallOutput[];\n }) {\n const { message, toolCalls = [], toolOutputs = [] } = params ?? {};\n return new ChatItemGroup({ message, toolCalls, toolOutputs });\n }\n\n get isEmpty() {\n return (\n this.message === undefined && this.toolCalls.length === 0 && this.toolOutputs.length === 0\n );\n }\n\n add(item: ChatItem) {\n if (item.type === 'message') {\n if (this.message) {\n throw new Error('only one message is allowed in a group');\n }\n this.message = item;\n } else if (item.type === 'function_call') {\n this.toolCalls.push(item);\n } else if (item.type === 'function_call_output') {\n this.toolOutputs.push(item);\n }\n return this;\n }\n\n removeInvalidToolCalls() {\n if (this.toolCalls.length === this.toolOutputs.length) {\n return;\n }\n\n const toolCallIds = new Set(this.toolCalls.map((call) => call.callId));\n const toolOutputIds = new Set(this.toolOutputs.map((output) => output.callId));\n\n // intersection of tool call ids and tool output ids\n const validCallIds = intersection(toolCallIds, toolOutputIds);\n\n // filter out tool calls that don't have a corresponding tool output\n this.toolCalls = this.toolCalls.filter((call) => {\n if (validCallIds.has(call.callId)) return true;\n this.logger.warn(\n {\n callId: call.callId,\n toolName: call.name,\n },\n 'function call missing the corresponding function output, ignoring',\n );\n return false;\n });\n\n // filter out tool outputs that don't have a corresponding tool call\n this.toolOutputs = this.toolOutputs.filter((output) => {\n if (validCallIds.has(output.callId)) return true;\n this.logger.warn(\n {\n callId: output.callId,\n toolName: output.name,\n },\n 'function output missing the corresponding function call, ignoring',\n );\n return false;\n });\n }\n\n flatten() {\n const items: ChatItem[] = [];\n if (this.message) items.push(this.message);\n items.push(...this.toolCalls, ...this.toolOutputs);\n return items;\n }\n}\n\nfunction intersection<T>(set1: Set<T>, set2: Set<T>): Set<T> {\n return new Set([...set1].filter((item) => set2.has(item)));\n}\n\n/**\n * Group chat items (messages, function calls, and function outputs)\n * into coherent groups based on their item IDs and call IDs.\n *\n * Each group will contain:\n * - Zero or one assistant message\n * - Zero or more function/tool calls\n * - The corresponding function/tool outputs matched by call_id\n *\n * User and system messages are placed in their own individual groups.\n *\n * @param chatCtx - The chat context containing all conversation items\n * @returns A list of ChatItemGroup objects representing the grouped conversation\n */\nexport function groupToolCalls(chatCtx: ChatContext) {\n const itemGroups: Record<string, ChatItemGroup> = {};\n const insertionOrder: Record<string, number> = {};\n const toolOutputs: FunctionCallOutput[] = [];\n const logger = log();\n\n let insertionIndex = 0;\n for (const item of chatCtx.items) {\n const isAssistantMessage = item.type === 'message' && item.role === 'assistant';\n const isFunctionCall = item.type === 'function_call';\n const isFunctionCallOutput = item.type === 'function_call_output';\n\n if (isAssistantMessage || isFunctionCall) {\n // only assistant messages and function calls can be grouped\n const groupId = item.id.split('/')[0]!;\n if (itemGroups[groupId] === undefined) {\n itemGroups[groupId] = ChatItemGroup.create();\n\n // we use insertion order to sort the groups as they are added to the context\n // simulating the OrderedDict in python\n insertionOrder[groupId] = insertionIndex;\n insertionIndex++;\n }\n itemGroups[groupId]!.add(item);\n } else if (isFunctionCallOutput) {\n toolOutputs.push(item);\n } else {\n itemGroups[item.id] = ChatItemGroup.create().add(item);\n }\n }\n\n // add tool outputs to their corresponding groups\n const callIdToGroup: Record<string, ChatItemGroup> = {};\n for (const group of Object.values(itemGroups)) {\n for (const toolCall of group.toolCalls) {\n callIdToGroup[toolCall.callId] = group;\n }\n }\n\n for (const toolOutput of toolOutputs) {\n const group = callIdToGroup[toolOutput.callId];\n if (group === undefined) {\n logger.warn(\n { callId: toolOutput.callId, toolName: toolOutput.name },\n 'function output missing the corresponding function call, ignoring',\n );\n continue;\n }\n group.add(toolOutput);\n }\n\n // validate that each group and remove invalid tool calls and tool outputs\n for (const group of Object.values(itemGroups)) {\n group.removeInvalidToolCalls();\n }\n\n // sort groups by their item id\n const orderedGroups = Object.entries(itemGroups)\n .sort((a, b) => insertionOrder[a[0]]! - insertionOrder[b[0]]!)\n .map(([, group]) => group);\n return orderedGroups;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAAoB;AASpB,MAAM,cAAc;AAAA,EAClB;AAAA,EACA;AAAA,EACA;AAAA,EACA,aAAS,gBAAI;AAAA,EAEb,YAAY,QAIT;AACD,SAAK,UAAU,OAAO;AACtB,SAAK,YAAY,OAAO;AACxB,SAAK,cAAc,OAAO;AAAA,EAC5B;AAAA,EAEA,OAAO,OAAO,QAIX;AACD,UAAM,EAAE,SAAS,YAAY,CAAC,GAAG,cAAc,CAAC,EAAE,IAAI,UAAU,CAAC;AACjE,WAAO,IAAI,cAAc,EAAE,SAAS,WAAW,YAAY,CAAC;AAAA,EAC9D;AAAA,EAEA,IAAI,UAAU;AACZ,WACE,KAAK,YAAY,UAAa,KAAK,UAAU,WAAW,KAAK,KAAK,YAAY,WAAW;AAAA,EAE7F;AAAA,EAEA,IAAI,MAAgB;AAClB,QAAI,KAAK,SAAS,WAAW;AAC3B,UAAI,KAAK,SAAS;AAChB,cAAM,IAAI,MAAM,wCAAwC;AAAA,MAC1D;AACA,WAAK,UAA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AAG,MAAM,eAAe,EAAE,CAAC,CAAC,IAAK,eAAe,EAAE,CAAC,CAAC,CAAE,EAC5D,IAAI,CAAC,CAAC,EAAE,KAAK,MAAM,KAAK;AAC3B,SAAO;AACT;","names":[]}
@@ -0,0 +1,38 @@
1
+ import type { ChatContext, ChatItem, ChatMessage, FunctionCall, FunctionCallOutput } from '../chat_context.js';
2
+ declare class ChatItemGroup {
3
+ message?: ChatMessage;
4
+ toolCalls: FunctionCall[];
5
+ toolOutputs: FunctionCallOutput[];
6
+ logger: import("pino").Logger;
7
+ constructor(params: {
8
+ message?: ChatMessage;
9
+ toolCalls: FunctionCall[];
10
+ toolOutputs: FunctionCallOutput[];
11
+ });
12
+ static create(params?: {
13
+ message?: ChatMessage;
14
+ toolCalls?: FunctionCall[];
15
+ toolOutputs?: FunctionCallOutput[];
16
+ }): ChatItemGroup;
17
+ get isEmpty(): boolean;
18
+ add(item: ChatItem): this;
19
+ removeInvalidToolCalls(): void;
20
+ flatten(): ChatItem[];
21
+ }
22
+ /**
23
+ * Group chat items (messages, function calls, and function outputs)
24
+ * into coherent groups based on their item IDs and call IDs.
25
+ *
26
+ * Each group will contain:
27
+ * - Zero or one assistant message
28
+ * - Zero or more function/tool calls
29
+ * - The corresponding function/tool outputs matched by call_id
30
+ *
31
+ * User and system messages are placed in their own individual groups.
32
+ *
33
+ * @param chatCtx - The chat context containing all conversation items
34
+ * @returns A list of ChatItemGroup objects representing the grouped conversation
35
+ */
36
+ export declare function groupToolCalls(chatCtx: ChatContext): ChatItemGroup[];
37
+ export {};
38
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1,38 @@
1
+ import type { ChatContext, ChatItem, ChatMessage, FunctionCall, FunctionCallOutput } from '../chat_context.js';
2
+ declare class ChatItemGroup {
3
+ message?: ChatMessage;
4
+ toolCalls: FunctionCall[];
5
+ toolOutputs: FunctionCallOutput[];
6
+ logger: import("pino").Logger;
7
+ constructor(params: {
8
+ message?: ChatMessage;
9
+ toolCalls: FunctionCall[];
10
+ toolOutputs: FunctionCallOutput[];
11
+ });
12
+ static create(params?: {
13
+ message?: ChatMessage;
14
+ toolCalls?: FunctionCall[];
15
+ toolOutputs?: FunctionCallOutput[];
16
+ }): ChatItemGroup;
17
+ get isEmpty(): boolean;
18
+ add(item: ChatItem): this;
19
+ removeInvalidToolCalls(): void;
20
+ flatten(): ChatItem[];
21
+ }
22
+ /**
23
+ * Group chat items (messages, function calls, and function outputs)
24
+ * into coherent groups based on their item IDs and call IDs.
25
+ *
26
+ * Each group will contain:
27
+ * - Zero or one assistant message
28
+ * - Zero or more function/tool calls
29
+ * - The corresponding function/tool outputs matched by call_id
30
+ *
31
+ * User and system messages are placed in their own individual groups.
32
+ *
33
+ * @param chatCtx - The chat context containing all conversation items
34
+ * @returns A list of ChatItemGroup objects representing the grouped conversation
35
+ */
36
+ export declare function groupToolCalls(chatCtx: ChatContext): ChatItemGroup[];
37
+ export {};
38
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/llm/provider_format/utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,WAAW,EACX,QAAQ,EACR,WAAW,EACX,YAAY,EACZ,kBAAkB,EACnB,MAAM,oBAAoB,CAAC;AAE5B,cAAM,aAAa;IACjB,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,SAAS,EAAE,YAAY,EAAE,CAAC;IAC1B,WAAW,EAAE,kBAAkB,EAAE,CAAC;IAClC,MAAM,wBAAS;gBAEH,MAAM,EAAE;QAClB,OAAO,CAAC,EAAE,WAAW,CAAC;QACtB,SAAS,EAAE,YAAY,EAAE,CAAC;QAC1B,WAAW,EAAE,kBAAkB,EAAE,CAAC;KACnC;IAMD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE;QACrB,OAAO,CAAC,EAAE,WAAW,CAAC;QACtB,SAAS,CAAC,EAAE,YAAY,EAAE,CAAC;QAC3B,WAAW,CAAC,EAAE,kBAAkB,EAAE,CAAC;KACpC;IAKD,IAAI,OAAO,YAIV;IAED,GAAG,CAAC,IAAI,EAAE,QAAQ;IAclB,sBAAsB;IAsCtB,OAAO;CAMR;AAMD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,WAAW,mBA6DlD"}
@@ -0,0 +1,122 @@
1
+ import { log } from "../../log.js";
2
+ class ChatItemGroup {
3
+ message;
4
+ toolCalls;
5
+ toolOutputs;
6
+ logger = log();
7
+ constructor(params) {
8
+ this.message = params.message;
9
+ this.toolCalls = params.toolCalls;
10
+ this.toolOutputs = params.toolOutputs;
11
+ }
12
+ static create(params) {
13
+ const { message, toolCalls = [], toolOutputs = [] } = params ?? {};
14
+ return new ChatItemGroup({ message, toolCalls, toolOutputs });
15
+ }
16
+ get isEmpty() {
17
+ return this.message === void 0 && this.toolCalls.length === 0 && this.toolOutputs.length === 0;
18
+ }
19
+ add(item) {
20
+ if (item.type === "message") {
21
+ if (this.message) {
22
+ throw new Error("only one message is allowed in a group");
23
+ }
24
+ this.message = item;
25
+ } else if (item.type === "function_call") {
26
+ this.toolCalls.push(item);
27
+ } else if (item.type === "function_call_output") {
28
+ this.toolOutputs.push(item);
29
+ }
30
+ return this;
31
+ }
32
+ removeInvalidToolCalls() {
33
+ if (this.toolCalls.length === this.toolOutputs.length) {
34
+ return;
35
+ }
36
+ const toolCallIds = new Set(this.toolCalls.map((call) => call.callId));
37
+ const toolOutputIds = new Set(this.toolOutputs.map((output) => output.callId));
38
+ const validCallIds = intersection(toolCallIds, toolOutputIds);
39
+ this.toolCalls = this.toolCalls.filter((call) => {
40
+ if (validCallIds.has(call.callId)) return true;
41
+ this.logger.warn(
42
+ {
43
+ callId: call.callId,
44
+ toolName: call.name
45
+ },
46
+ "function call missing the corresponding function output, ignoring"
47
+ );
48
+ return false;
49
+ });
50
+ this.toolOutputs = this.toolOutputs.filter((output) => {
51
+ if (validCallIds.has(output.callId)) return true;
52
+ this.logger.warn(
53
+ {
54
+ callId: output.callId,
55
+ toolName: output.name
56
+ },
57
+ "function output missing the corresponding function call, ignoring"
58
+ );
59
+ return false;
60
+ });
61
+ }
62
+ flatten() {
63
+ const items = [];
64
+ if (this.message) items.push(this.message);
65
+ items.push(...this.toolCalls, ...this.toolOutputs);
66
+ return items;
67
+ }
68
+ }
69
+ function intersection(set1, set2) {
70
+ return new Set([...set1].filter((item) => set2.has(item)));
71
+ }
72
+ function groupToolCalls(chatCtx) {
73
+ const itemGroups = {};
74
+ const insertionOrder = {};
75
+ const toolOutputs = [];
76
+ const logger = log();
77
+ let insertionIndex = 0;
78
+ for (const item of chatCtx.items) {
79
+ const isAssistantMessage = item.type === "message" && item.role === "assistant";
80
+ const isFunctionCall = item.type === "function_call";
81
+ const isFunctionCallOutput = item.type === "function_call_output";
82
+ if (isAssistantMessage || isFunctionCall) {
83
+ const groupId = item.id.split("/")[0];
84
+ if (itemGroups[groupId] === void 0) {
85
+ itemGroups[groupId] = ChatItemGroup.create();
86
+ insertionOrder[groupId] = insertionIndex;
87
+ insertionIndex++;
88
+ }
89
+ itemGroups[groupId].add(item);
90
+ } else if (isFunctionCallOutput) {
91
+ toolOutputs.push(item);
92
+ } else {
93
+ itemGroups[item.id] = ChatItemGroup.create().add(item);
94
+ }
95
+ }
96
+ const callIdToGroup = {};
97
+ for (const group of Object.values(itemGroups)) {
98
+ for (const toolCall of group.toolCalls) {
99
+ callIdToGroup[toolCall.callId] = group;
100
+ }
101
+ }
102
+ for (const toolOutput of toolOutputs) {
103
+ const group = callIdToGroup[toolOutput.callId];
104
+ if (group === void 0) {
105
+ logger.warn(
106
+ { callId: toolOutput.callId, toolName: toolOutput.name },
107
+ "function output missing the corresponding function call, ignoring"
108
+ );
109
+ continue;
110
+ }
111
+ group.add(toolOutput);
112
+ }
113
+ for (const group of Object.values(itemGroups)) {
114
+ group.removeInvalidToolCalls();
115
+ }
116
+ const orderedGroups = Object.entries(itemGroups).sort((a, b) => insertionOrder[a[0]] - insertionOrder[b[0]]).map(([, group]) => group);
117
+ return orderedGroups;
118
+ }
119
+ export {
120
+ groupToolCalls
121
+ };
122
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/llm/provider_format/utils.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { log } from '../../log.js';\nimport type {\n ChatContext,\n ChatItem,\n ChatMessage,\n FunctionCall,\n FunctionCallOutput,\n} from '../chat_context.js';\n\nclass ChatItemGroup {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n logger = log();\n\n constructor(params: {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n }) {\n this.message = params.message;\n this.toolCalls = params.toolCalls;\n this.toolOutputs = params.toolOutputs;\n }\n\n static create(params?: {\n message?: ChatMessage;\n toolCalls?: FunctionCall[];\n toolOutputs?: FunctionCallOutput[];\n }) {\n const { message, toolCalls = [], toolOutputs = [] } = params ?? {};\n return new ChatItemGroup({ message, toolCalls, toolOutputs });\n }\n\n get isEmpty() {\n return (\n this.message === undefined && this.toolCalls.length === 0 && this.toolOutputs.length === 0\n );\n }\n\n add(item: ChatItem) {\n if (item.type === 'message') {\n if (this.message) {\n throw new Error('only one message is allowed in a group');\n }\n this.message = item;\n } else if (item.type === 'function_call') {\n this.toolCalls.push(item);\n } else if (item.type === 'function_call_output') {\n this.toolOutputs.push(item);\n }\n return this;\n }\n\n removeInvalidToolCalls() {\n if (this.toolCalls.length === this.toolOutputs.length) {\n return;\n }\n\n const toolCallIds = new Set(this.toolCalls.map((call) => call.callId));\n const toolOutputIds = new Set(this.toolOutputs.map((output) => output.callId));\n\n // intersection of tool call ids and tool output ids\n const validCallIds = intersection(toolCallIds, toolOutputIds);\n\n // filter out tool calls that don't have a corresponding tool output\n this.toolCalls = this.toolCalls.filter((call) => {\n if (validCallIds.has(call.callId)) return true;\n this.logger.warn(\n {\n callId: call.callId,\n toolName: call.name,\n },\n 'function call missing the corresponding function output, ignoring',\n );\n return false;\n });\n\n // filter out tool outputs that don't have a corresponding tool call\n this.toolOutputs = this.toolOutputs.filter((output) => {\n if (validCallIds.has(output.callId)) return true;\n this.logger.warn(\n {\n callId: output.callId,\n toolName: output.name,\n },\n 'function output missing the corresponding function call, ignoring',\n );\n return false;\n });\n }\n\n flatten() {\n const items: ChatItem[] = [];\n if (this.message) items.push(this.message);\n items.push(...this.toolCalls, ...this.toolOutputs);\n return items;\n }\n}\n\nfunction intersection<T>(set1: Set<T>, set2: Set<T>): Set<T> {\n return new Set([...set1].filter((item) => set2.has(item)));\n}\n\n/**\n * Group chat items (messages, function calls, and function outputs)\n * into coherent groups based on their item IDs and call IDs.\n *\n * Each group will contain:\n * - Zero or one assistant message\n * - Zero or more function/tool calls\n * - The corresponding function/tool outputs matched by call_id\n *\n * User and system messages are placed in their own individual groups.\n *\n * @param chatCtx - The chat context containing all conversation items\n * @returns A list of ChatItemGroup objects representing the grouped conversation\n */\nexport function groupToolCalls(chatCtx: ChatContext) {\n const itemGroups: Record<string, ChatItemGroup> = {};\n const insertionOrder: Record<string, number> = {};\n const toolOutputs: FunctionCallOutput[] = [];\n const logger = log();\n\n let insertionIndex = 0;\n for (const item of chatCtx.items) {\n const isAssistantMessage = item.type === 'message' && item.role === 'assistant';\n const isFunctionCall = item.type === 'function_call';\n const isFunctionCallOutput = item.type === 'function_call_output';\n\n if (isAssistantMessage || isFunctionCall) {\n // only assistant messages and function calls can be grouped\n const groupId = item.id.split('/')[0]!;\n if (itemGroups[groupId] === undefined) {\n itemGroups[groupId] = ChatItemGroup.create();\n\n // we use insertion order to sort the groups as they are added to the context\n // simulating the OrderedDict in python\n insertionOrder[groupId] = insertionIndex;\n insertionIndex++;\n }\n itemGroups[groupId]!.add(item);\n } else if (isFunctionCallOutput) {\n toolOutputs.push(item);\n } else {\n itemGroups[item.id] = ChatItemGroup.create().add(item);\n }\n }\n\n // add tool outputs to their corresponding groups\n const callIdToGroup: Record<string, ChatItemGroup> = {};\n for (const group of Object.values(itemGroups)) {\n for (const toolCall of group.toolCalls) {\n callIdToGroup[toolCall.callId] = group;\n }\n }\n\n for (const toolOutput of toolOutputs) {\n const group = callIdToGroup[toolOutput.callId];\n if (group === undefined) {\n logger.warn(\n { callId: toolOutput.callId, toolName: toolOutput.name },\n 'function output missing the corresponding function call, ignoring',\n );\n continue;\n }\n group.add(toolOutput);\n }\n\n // validate that each group and remove invalid tool calls and tool outputs\n for (const group of Object.values(itemGroups)) {\n group.removeInvalidToolCalls();\n }\n\n // sort groups by their item id\n const orderedGroups = Object.entries(itemGroups)\n .sort((a, b) => insertionOrder[a[0]]! - insertionOrder[b[0]]!)\n .map(([, group]) => group);\n return orderedGroups;\n}\n"],"mappings":"AAGA,SAAS,WAAW;AASpB,MAAM,cAAc;AAAA,EAClB;AAAA,EACA;AAAA,EACA;AAAA,EACA,SAAS,IAAI;AAAA,EAEb,YAAY,QAIT;AACD,SAAK,UAAU,OAAO;AACtB,SAAK,YAAY,OAAO;AACxB,SAAK,cAAc,OAAO;AAAA,EAC5B;AAAA,EAEA,OAAO,OAAO,QAIX;AACD,UAAM,EAAE,SAAS,YAAY,CAAC,GAAG,cAAc,CAAC,EAAE,IAAI,UAAU,CAAC;AACjE,WAAO,IAAI,cAAc,EAAE,SAAS,WAAW,YAAY,CAAC;AAAA,EAC9D;AAAA,EAEA,IAAI,UAAU;AACZ,WACE,KAAK,YAAY,UAAa,KAAK,UAAU,WAAW,KAAK,KAAK,YAAY,WAAW;AAAA,EAE7F;AAAA,EAEA,IAAI,MAAgB;AAClB,QAAI,KAAK,SAAS,WAAW;AAC3B,UAAI,KAAK,SAAS;AAChB,cAAM,IAAI,MAAM,wCAAwC;AAAA,MAC1D;AACA,WAAK,UAAU;AAAA,IACjB,WAAW,KAAK,SAAS,iBAAiB;AACxC,WAAK,UAAU,KAAK,IAAI;AAA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D,IAAI,CAAC,CAAC,EAAE,KAAK,MAAM,KAAK;AAC3B,SAAO;AACT;","names":[]}
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var realtime_exports = {};
20
+ __export(realtime_exports, {
21
+ RealtimeModel: () => RealtimeModel,
22
+ RealtimeSession: () => RealtimeSession
23
+ });
24
+ module.exports = __toCommonJS(realtime_exports);
25
+ var import_events = require("events");
26
+ var import_deferred_stream = require("../stream/deferred_stream.cjs");
27
+ var import_utils = require("../utils.cjs");
28
+ class RealtimeModel {
29
+ _capabilities;
30
+ constructor(capabilities) {
31
+ this._capabilities = capabilities;
32
+ }
33
+ get capabilities() {
34
+ return this._capabilities;
35
+ }
36
+ }
37
+ class RealtimeSession extends import_events.EventEmitter {
38
+ _realtimeModel;
39
+ deferredInputStream = new import_deferred_stream.DeferredReadableStream();
40
+ _mainTask;
41
+ constructor(realtimeModel) {
42
+ super();
43
+ this._realtimeModel = realtimeModel;
44
+ this._mainTask = import_utils.Task.from((controller) => this._mainTaskImpl(controller.signal));
45
+ }
46
+ get realtimeModel() {
47
+ return this._realtimeModel;
48
+ }
49
+ async close() {
50
+ this._mainTask.cancel();
51
+ }
52
+ /**
53
+ * Notifies the model that user activity has started
54
+ */
55
+ startUserActivity() {
56
+ return;
57
+ }
58
+ async _mainTaskImpl(signal) {
59
+ const reader = this.deferredInputStream.stream.getReader();
60
+ while (true) {
61
+ const { done, value } = await reader.read();
62
+ if (done || signal.aborted) {
63
+ break;
64
+ }
65
+ this.pushAudio(value);
66
+ }
67
+ }
68
+ setInputAudioStream(audioStream) {
69
+ this.deferredInputStream.setSource(audioStream);
70
+ }
71
+ }
72
+ // Annotate the CommonJS export names for ESM import in node:
73
+ 0 && (module.exports = {
74
+ RealtimeModel,
75
+ RealtimeSession
76
+ });
77
+ //# sourceMappingURL=realtime.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/llm/realtime.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { AudioFrame } from '@livekit/rtc-node';\nimport { EventEmitter } from 'events';\nimport type { ReadableStream } from 'node:stream/web';\nimport { DeferredReadableStream } from '../stream/deferred_stream.js';\nimport { Task } from '../utils.js';\nimport type { ChatContext, FunctionCall } from './chat_context.js';\nimport type { ToolChoice, ToolContext } from './tool_context.js';\n\nexport type InputSpeechStartedEvent = object;\n\nexport interface InputSpeechStoppedEvent {\n userTranscriptionEnabled: boolean;\n}\n\nexport interface MessageGeneration {\n messageId: string;\n textStream: ReadableStream<string>;\n audioStream: ReadableStream<AudioFrame>;\n}\n\nexport interface GenerationCreatedEvent {\n messageStream: ReadableStream<MessageGeneration>;\n functionStream: ReadableStream<FunctionCall>;\n userInitiated: boolean;\n}\n\nexport interface RealtimeModelError {\n type: 'realtime_model_error';\n timestamp: number;\n label: string;\n error: Error;\n recoverable: boolean;\n}\n\nexport interface RealtimeCapabilities {\n messageTruncation: boolean;\n turnDetection: boolean;\n userTranscription: boolean;\n autoToolReplyGeneration: boolean;\n}\n\nexport interface InputTranscriptionCompleted {\n itemId: string;\n transcript: string;\n isFinal: boolean;\n}\n\nexport interface RealtimeSessionReconnectedEvent {}\n\nexport abstract class RealtimeModel {\n private _capabilities: RealtimeCapabilities;\n\n constructor(capabilities: RealtimeCapabilities) {\n this._capabilities = capabilities;\n }\n\n get capabilities() {\n return this._capabilities;\n }\n\n abstract session(): RealtimeSession;\n\n abstract close(): Promise<void>;\n}\n\nexport abstract class RealtimeSession extends EventEmitter {\n protected _realtimeModel: RealtimeModel;\n private deferredInputStream = new DeferredReadableStream<AudioFrame>();\n private _mainTask: Task<void>;\n\n constructor(realtimeModel: RealtimeModel) {\n super();\n this._realtimeModel = realtimeModel;\n this._mainTask = Task.from((controller) => this._mainTaskImpl(controller.signal));\n }\n\n get realtimeModel() {\n return this._realtimeModel;\n }\n\n abstract get chatCtx(): ChatContext;\n\n abstract get tools(): ToolContext;\n\n abstract updateInstructions(instructions: string): Promise<void>;\n\n /**\n * @throws RealtimeError on Timeout\n */\n abstract updateChatCtx(chatCtx: ChatContext): Promise<void>;\n\n abstract updateTools(tools: ToolContext): Promise<void>;\n\n abstract updateOptions(options: { toolChoice?: ToolChoice | null }): void;\n\n abstract pushAudio(frame: AudioFrame): void;\n\n /**\n * @throws RealtimeError on Timeout\n */\n abstract generateReply(instructions?: string): Promise<GenerationCreatedEvent>;\n\n /**\n * Commit the input audio buffer to the server\n */\n abstract commitAudio(): Promise<void>;\n\n /**\n * Clear the input audio buffer to the server\n */\n abstract clearAudio(): Promise<void>;\n\n /**\n * Cancel the current generation (do nothing if no generation is in progress)\n */\n abstract interrupt(): Promise<void>;\n\n /**\n * Truncate the message at the given audio end time\n */\n abstract truncate(options: { messageId: string; audioEndMs: number }): Promise<void>;\n\n async close(): Promise<void> {\n this._mainTask.cancel();\n }\n\n /**\n * Notifies the model that user activity has started\n */\n startUserActivity(): void {\n return;\n }\n\n private async _mainTaskImpl(signal: AbortSignal): Promise<void> {\n const reader = this.deferredInputStream.stream.getReader();\n while (true) {\n const { done, value } = await reader.read();\n if (done || signal.aborted) {\n break;\n }\n this.pushAudio(value);\n }\n }\n\n setInputAudioStream(audioStream: ReadableStream<AudioFrame>): void {\n this.deferredInputStream.setSource(audioStream);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,oBAA6B;AAE7B,6BAAuC;AACvC,mBAAqB;AA6Cd,MAAe,cAAc;AAAA,EAC1B;AAAA,EAER,YAAY,cAAoC;AAC9C,SAAK,gBAAgB;AAAA,EACvB;AAAA,EAEA,IAAI,eAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAKF;AAEO,MAAe,wBAAwB,2BAAa;AAAA,EAC/C;AAAA,EACF,sBAAsB,IAAI,8CAAmC;AAAA,EAC7D;AAAA,EAER,YAAY,eAA8B;AACxC,UAAM;AACN,SAAK,iBAAiB;AACtB,SAAK,YAAY,kBAAK,KAAK,CAAC,eAAe,KAAK,cAAc,WAAW,MAAM,CAAC;AAAA,EAClF;AAAA,EAEA,IAAI,gBAAgB;AAClB,WAAO,KAAK;AAAA,EACd;AAAA,EA4CA,MAAM,QAAuB;AAC3B,SAAK,UAAU,OAAO;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA,EAKA,oBAA0B;AACxB;AAAA,EACF;AAAA,EAEA,MAAc,cAAc,QAAoC;AAC9D,UAAM,SAAS,KAAK,oBAAoB,OAAO,UAAU;AACzD,WAAO,MAAM;AACX,YAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAC1C,UAAI,QAAQ,OAAO,SAAS;AAC1B;AAAA,MACF;AACA,WAAK,UAAU,KAAK;AAAA,IACtB;AAAA,EACF;AAAA,EAEA,oBAAoB,aAA+C;AACjE,SAAK,oBAAoB,UAAU,WAAW;AAAA,EAChD;AACF;","names":[]}
@@ -0,0 +1,98 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ /// <reference types="node" resolution-mode="require"/>
3
+ import type { AudioFrame } from '@livekit/rtc-node';
4
+ import { EventEmitter } from 'events';
5
+ import type { ReadableStream } from 'node:stream/web';
6
+ import type { ChatContext, FunctionCall } from './chat_context.js';
7
+ import type { ToolChoice, ToolContext } from './tool_context.js';
8
+ export type InputSpeechStartedEvent = object;
9
+ export interface InputSpeechStoppedEvent {
10
+ userTranscriptionEnabled: boolean;
11
+ }
12
+ export interface MessageGeneration {
13
+ messageId: string;
14
+ textStream: ReadableStream<string>;
15
+ audioStream: ReadableStream<AudioFrame>;
16
+ }
17
+ export interface GenerationCreatedEvent {
18
+ messageStream: ReadableStream<MessageGeneration>;
19
+ functionStream: ReadableStream<FunctionCall>;
20
+ userInitiated: boolean;
21
+ }
22
+ export interface RealtimeModelError {
23
+ type: 'realtime_model_error';
24
+ timestamp: number;
25
+ label: string;
26
+ error: Error;
27
+ recoverable: boolean;
28
+ }
29
+ export interface RealtimeCapabilities {
30
+ messageTruncation: boolean;
31
+ turnDetection: boolean;
32
+ userTranscription: boolean;
33
+ autoToolReplyGeneration: boolean;
34
+ }
35
+ export interface InputTranscriptionCompleted {
36
+ itemId: string;
37
+ transcript: string;
38
+ isFinal: boolean;
39
+ }
40
+ export interface RealtimeSessionReconnectedEvent {
41
+ }
42
+ export declare abstract class RealtimeModel {
43
+ private _capabilities;
44
+ constructor(capabilities: RealtimeCapabilities);
45
+ get capabilities(): RealtimeCapabilities;
46
+ abstract session(): RealtimeSession;
47
+ abstract close(): Promise<void>;
48
+ }
49
+ export declare abstract class RealtimeSession extends EventEmitter {
50
+ protected _realtimeModel: RealtimeModel;
51
+ private deferredInputStream;
52
+ private _mainTask;
53
+ constructor(realtimeModel: RealtimeModel);
54
+ get realtimeModel(): RealtimeModel;
55
+ abstract get chatCtx(): ChatContext;
56
+ abstract get tools(): ToolContext;
57
+ abstract updateInstructions(instructions: string): Promise<void>;
58
+ /**
59
+ * @throws RealtimeError on Timeout
60
+ */
61
+ abstract updateChatCtx(chatCtx: ChatContext): Promise<void>;
62
+ abstract updateTools(tools: ToolContext): Promise<void>;
63
+ abstract updateOptions(options: {
64
+ toolChoice?: ToolChoice | null;
65
+ }): void;
66
+ abstract pushAudio(frame: AudioFrame): void;
67
+ /**
68
+ * @throws RealtimeError on Timeout
69
+ */
70
+ abstract generateReply(instructions?: string): Promise<GenerationCreatedEvent>;
71
+ /**
72
+ * Commit the input audio buffer to the server
73
+ */
74
+ abstract commitAudio(): Promise<void>;
75
+ /**
76
+ * Clear the input audio buffer to the server
77
+ */
78
+ abstract clearAudio(): Promise<void>;
79
+ /**
80
+ * Cancel the current generation (do nothing if no generation is in progress)
81
+ */
82
+ abstract interrupt(): Promise<void>;
83
+ /**
84
+ * Truncate the message at the given audio end time
85
+ */
86
+ abstract truncate(options: {
87
+ messageId: string;
88
+ audioEndMs: number;
89
+ }): Promise<void>;
90
+ close(): Promise<void>;
91
+ /**
92
+ * Notifies the model that user activity has started
93
+ */
94
+ startUserActivity(): void;
95
+ private _mainTaskImpl;
96
+ setInputAudioStream(audioStream: ReadableStream<AudioFrame>): void;
97
+ }
98
+ //# sourceMappingURL=realtime.d.ts.map
@@ -0,0 +1,98 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ /// <reference types="node" resolution-mode="require"/>
3
+ import type { AudioFrame } from '@livekit/rtc-node';
4
+ import { EventEmitter } from 'events';
5
+ import type { ReadableStream } from 'node:stream/web';
6
+ import type { ChatContext, FunctionCall } from './chat_context.js';
7
+ import type { ToolChoice, ToolContext } from './tool_context.js';
8
+ export type InputSpeechStartedEvent = object;
9
+ export interface InputSpeechStoppedEvent {
10
+ userTranscriptionEnabled: boolean;
11
+ }
12
+ export interface MessageGeneration {
13
+ messageId: string;
14
+ textStream: ReadableStream<string>;
15
+ audioStream: ReadableStream<AudioFrame>;
16
+ }
17
+ export interface GenerationCreatedEvent {
18
+ messageStream: ReadableStream<MessageGeneration>;
19
+ functionStream: ReadableStream<FunctionCall>;
20
+ userInitiated: boolean;
21
+ }
22
+ export interface RealtimeModelError {
23
+ type: 'realtime_model_error';
24
+ timestamp: number;
25
+ label: string;
26
+ error: Error;
27
+ recoverable: boolean;
28
+ }
29
+ export interface RealtimeCapabilities {
30
+ messageTruncation: boolean;
31
+ turnDetection: boolean;
32
+ userTranscription: boolean;
33
+ autoToolReplyGeneration: boolean;
34
+ }
35
+ export interface InputTranscriptionCompleted {
36
+ itemId: string;
37
+ transcript: string;
38
+ isFinal: boolean;
39
+ }
40
+ export interface RealtimeSessionReconnectedEvent {
41
+ }
42
+ export declare abstract class RealtimeModel {
43
+ private _capabilities;
44
+ constructor(capabilities: RealtimeCapabilities);
45
+ get capabilities(): RealtimeCapabilities;
46
+ abstract session(): RealtimeSession;
47
+ abstract close(): Promise<void>;
48
+ }
49
+ export declare abstract class RealtimeSession extends EventEmitter {
50
+ protected _realtimeModel: RealtimeModel;
51
+ private deferredInputStream;
52
+ private _mainTask;
53
+ constructor(realtimeModel: RealtimeModel);
54
+ get realtimeModel(): RealtimeModel;
55
+ abstract get chatCtx(): ChatContext;
56
+ abstract get tools(): ToolContext;
57
+ abstract updateInstructions(instructions: string): Promise<void>;
58
+ /**
59
+ * @throws RealtimeError on Timeout
60
+ */
61
+ abstract updateChatCtx(chatCtx: ChatContext): Promise<void>;
62
+ abstract updateTools(tools: ToolContext): Promise<void>;
63
+ abstract updateOptions(options: {
64
+ toolChoice?: ToolChoice | null;
65
+ }): void;
66
+ abstract pushAudio(frame: AudioFrame): void;
67
+ /**
68
+ * @throws RealtimeError on Timeout
69
+ */
70
+ abstract generateReply(instructions?: string): Promise<GenerationCreatedEvent>;
71
+ /**
72
+ * Commit the input audio buffer to the server
73
+ */
74
+ abstract commitAudio(): Promise<void>;
75
+ /**
76
+ * Clear the input audio buffer to the server
77
+ */
78
+ abstract clearAudio(): Promise<void>;
79
+ /**
80
+ * Cancel the current generation (do nothing if no generation is in progress)
81
+ */
82
+ abstract interrupt(): Promise<void>;
83
+ /**
84
+ * Truncate the message at the given audio end time
85
+ */
86
+ abstract truncate(options: {
87
+ messageId: string;
88
+ audioEndMs: number;
89
+ }): Promise<void>;
90
+ close(): Promise<void>;
91
+ /**
92
+ * Notifies the model that user activity has started
93
+ */
94
+ startUserActivity(): void;
95
+ private _mainTaskImpl;
96
+ setInputAudioStream(audioStream: ReadableStream<AudioFrame>): void;
97
+ }
98
+ //# sourceMappingURL=realtime.d.ts.map