@livekit/agents 1.1.0-dev.0 → 1.2.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 (292) hide show
  1. package/dist/cli.cjs +2 -0
  2. package/dist/cli.cjs.map +1 -1
  3. package/dist/cli.d.ts.map +1 -1
  4. package/dist/cli.js +2 -0
  5. package/dist/cli.js.map +1 -1
  6. package/dist/constants.cjs +3 -0
  7. package/dist/constants.cjs.map +1 -1
  8. package/dist/constants.d.cts +1 -0
  9. package/dist/constants.d.ts +1 -0
  10. package/dist/constants.d.ts.map +1 -1
  11. package/dist/constants.js +2 -0
  12. package/dist/constants.js.map +1 -1
  13. package/dist/cpu.cjs +189 -0
  14. package/dist/cpu.cjs.map +1 -0
  15. package/dist/cpu.d.cts +24 -0
  16. package/dist/cpu.d.ts +24 -0
  17. package/dist/cpu.d.ts.map +1 -0
  18. package/dist/cpu.js +152 -0
  19. package/dist/cpu.js.map +1 -0
  20. package/dist/cpu.test.cjs +227 -0
  21. package/dist/cpu.test.cjs.map +1 -0
  22. package/dist/cpu.test.js +204 -0
  23. package/dist/cpu.test.js.map +1 -0
  24. package/dist/index.cjs +12 -10
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.d.cts +13 -13
  27. package/dist/index.d.ts +13 -13
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +11 -10
  30. package/dist/index.js.map +1 -1
  31. package/dist/inference/interruption/defaults.cjs +1 -1
  32. package/dist/inference/interruption/defaults.cjs.map +1 -1
  33. package/dist/inference/interruption/defaults.d.cts +1 -1
  34. package/dist/inference/interruption/defaults.d.ts +1 -1
  35. package/dist/inference/interruption/defaults.d.ts.map +1 -1
  36. package/dist/inference/interruption/defaults.js +1 -1
  37. package/dist/inference/interruption/defaults.js.map +1 -1
  38. package/dist/inference/interruption/http_transport.cjs +44 -28
  39. package/dist/inference/interruption/http_transport.cjs.map +1 -1
  40. package/dist/inference/interruption/http_transport.d.ts.map +1 -1
  41. package/dist/inference/interruption/http_transport.js +45 -29
  42. package/dist/inference/interruption/http_transport.js.map +1 -1
  43. package/dist/inference/interruption/interruption_detector.cjs +22 -5
  44. package/dist/inference/interruption/interruption_detector.cjs.map +1 -1
  45. package/dist/inference/interruption/interruption_detector.d.cts +2 -2
  46. package/dist/inference/interruption/interruption_detector.d.ts +2 -2
  47. package/dist/inference/interruption/interruption_detector.d.ts.map +1 -1
  48. package/dist/inference/interruption/interruption_detector.js +22 -5
  49. package/dist/inference/interruption/interruption_detector.js.map +1 -1
  50. package/dist/inference/interruption/interruption_stream.cjs +4 -4
  51. package/dist/inference/interruption/interruption_stream.cjs.map +1 -1
  52. package/dist/inference/interruption/interruption_stream.js +4 -4
  53. package/dist/inference/interruption/interruption_stream.js.map +1 -1
  54. package/dist/inference/interruption/types.cjs.map +1 -1
  55. package/dist/inference/interruption/types.d.cts +2 -2
  56. package/dist/inference/interruption/types.d.ts +2 -2
  57. package/dist/inference/interruption/types.d.ts.map +1 -1
  58. package/dist/inference/interruption/ws_transport.cjs +60 -47
  59. package/dist/inference/interruption/ws_transport.cjs.map +1 -1
  60. package/dist/inference/interruption/ws_transport.d.ts.map +1 -1
  61. package/dist/inference/interruption/ws_transport.js +60 -47
  62. package/dist/inference/interruption/ws_transport.js.map +1 -1
  63. package/dist/inference/llm.cjs.map +1 -1
  64. package/dist/inference/llm.d.cts +1 -1
  65. package/dist/inference/llm.d.ts +1 -1
  66. package/dist/inference/llm.d.ts.map +1 -1
  67. package/dist/inference/llm.js.map +1 -1
  68. package/dist/inference/stt.cjs +20 -12
  69. package/dist/inference/stt.cjs.map +1 -1
  70. package/dist/inference/stt.d.cts +3 -2
  71. package/dist/inference/stt.d.ts +3 -2
  72. package/dist/inference/stt.d.ts.map +1 -1
  73. package/dist/inference/stt.js +20 -12
  74. package/dist/inference/stt.js.map +1 -1
  75. package/dist/inference/stt.test.cjs +14 -0
  76. package/dist/inference/stt.test.cjs.map +1 -1
  77. package/dist/inference/stt.test.js +14 -0
  78. package/dist/inference/stt.test.js.map +1 -1
  79. package/dist/inference/tts.cjs +13 -4
  80. package/dist/inference/tts.cjs.map +1 -1
  81. package/dist/inference/tts.d.cts +8 -1
  82. package/dist/inference/tts.d.ts +8 -1
  83. package/dist/inference/tts.d.ts.map +1 -1
  84. package/dist/inference/tts.js +13 -4
  85. package/dist/inference/tts.js.map +1 -1
  86. package/dist/inference/tts.test.cjs +10 -0
  87. package/dist/inference/tts.test.cjs.map +1 -1
  88. package/dist/inference/tts.test.js +10 -0
  89. package/dist/inference/tts.test.js.map +1 -1
  90. package/dist/ipc/job_proc_lazy_main.cjs +41 -23
  91. package/dist/ipc/job_proc_lazy_main.cjs.map +1 -1
  92. package/dist/ipc/job_proc_lazy_main.js +41 -23
  93. package/dist/ipc/job_proc_lazy_main.js.map +1 -1
  94. package/dist/job.cjs +1 -1
  95. package/dist/job.cjs.map +1 -1
  96. package/dist/job.js +1 -1
  97. package/dist/job.js.map +1 -1
  98. package/dist/language.cjs +394 -0
  99. package/dist/language.cjs.map +1 -0
  100. package/dist/language.d.cts +15 -0
  101. package/dist/language.d.ts +15 -0
  102. package/dist/language.d.ts.map +1 -0
  103. package/dist/language.js +363 -0
  104. package/dist/language.js.map +1 -0
  105. package/dist/language.test.cjs +43 -0
  106. package/dist/language.test.cjs.map +1 -0
  107. package/dist/language.test.js +49 -0
  108. package/dist/language.test.js.map +1 -0
  109. package/dist/llm/index.cjs +2 -0
  110. package/dist/llm/index.cjs.map +1 -1
  111. package/dist/llm/index.d.cts +1 -1
  112. package/dist/llm/index.d.ts +1 -1
  113. package/dist/llm/index.d.ts.map +1 -1
  114. package/dist/llm/index.js +2 -0
  115. package/dist/llm/index.js.map +1 -1
  116. package/dist/stream/deferred_stream.cjs +6 -2
  117. package/dist/stream/deferred_stream.cjs.map +1 -1
  118. package/dist/stream/deferred_stream.d.ts.map +1 -1
  119. package/dist/stream/deferred_stream.js +6 -2
  120. package/dist/stream/deferred_stream.js.map +1 -1
  121. package/dist/stt/stt.cjs.map +1 -1
  122. package/dist/stt/stt.d.cts +2 -1
  123. package/dist/stt/stt.d.ts +2 -1
  124. package/dist/stt/stt.d.ts.map +1 -1
  125. package/dist/stt/stt.js.map +1 -1
  126. package/dist/utils.cjs +15 -0
  127. package/dist/utils.cjs.map +1 -1
  128. package/dist/utils.d.cts +8 -0
  129. package/dist/utils.d.ts +8 -0
  130. package/dist/utils.d.ts.map +1 -1
  131. package/dist/utils.js +13 -0
  132. package/dist/utils.js.map +1 -1
  133. package/dist/version.cjs +1 -1
  134. package/dist/version.js +1 -1
  135. package/dist/voice/agent.cjs +14 -17
  136. package/dist/voice/agent.cjs.map +1 -1
  137. package/dist/voice/agent.d.cts +10 -11
  138. package/dist/voice/agent.d.ts +10 -11
  139. package/dist/voice/agent.d.ts.map +1 -1
  140. package/dist/voice/agent.js +15 -18
  141. package/dist/voice/agent.js.map +1 -1
  142. package/dist/voice/agent.test.cjs +194 -0
  143. package/dist/voice/agent.test.cjs.map +1 -1
  144. package/dist/voice/agent.test.js +195 -1
  145. package/dist/voice/agent.test.js.map +1 -1
  146. package/dist/voice/agent_activity.cjs +116 -39
  147. package/dist/voice/agent_activity.cjs.map +1 -1
  148. package/dist/voice/agent_activity.d.cts +2 -0
  149. package/dist/voice/agent_activity.d.ts +2 -0
  150. package/dist/voice/agent_activity.d.ts.map +1 -1
  151. package/dist/voice/agent_activity.js +117 -40
  152. package/dist/voice/agent_activity.js.map +1 -1
  153. package/dist/voice/agent_activity.test.cjs +135 -0
  154. package/dist/voice/agent_activity.test.cjs.map +1 -0
  155. package/dist/voice/agent_activity.test.js +134 -0
  156. package/dist/voice/agent_activity.test.js.map +1 -0
  157. package/dist/voice/agent_session.cjs +38 -38
  158. package/dist/voice/agent_session.cjs.map +1 -1
  159. package/dist/voice/agent_session.d.cts +65 -56
  160. package/dist/voice/agent_session.d.ts +65 -56
  161. package/dist/voice/agent_session.d.ts.map +1 -1
  162. package/dist/voice/agent_session.js +37 -37
  163. package/dist/voice/agent_session.js.map +1 -1
  164. package/dist/voice/audio_recognition.cjs +106 -52
  165. package/dist/voice/audio_recognition.cjs.map +1 -1
  166. package/dist/voice/audio_recognition.d.cts +4 -2
  167. package/dist/voice/audio_recognition.d.ts +4 -2
  168. package/dist/voice/audio_recognition.d.ts.map +1 -1
  169. package/dist/voice/audio_recognition.js +106 -52
  170. package/dist/voice/audio_recognition.js.map +1 -1
  171. package/dist/voice/audio_recognition_span.test.cjs +84 -22
  172. package/dist/voice/audio_recognition_span.test.cjs.map +1 -1
  173. package/dist/voice/audio_recognition_span.test.js +90 -23
  174. package/dist/voice/audio_recognition_span.test.js.map +1 -1
  175. package/dist/voice/events.cjs +1 -1
  176. package/dist/voice/events.cjs.map +1 -1
  177. package/dist/voice/events.d.cts +4 -3
  178. package/dist/voice/events.d.ts +4 -3
  179. package/dist/voice/events.d.ts.map +1 -1
  180. package/dist/voice/events.js +1 -1
  181. package/dist/voice/events.js.map +1 -1
  182. package/dist/voice/index.cjs +9 -1
  183. package/dist/voice/index.cjs.map +1 -1
  184. package/dist/voice/index.d.cts +1 -1
  185. package/dist/voice/index.d.ts +1 -1
  186. package/dist/voice/index.d.ts.map +1 -1
  187. package/dist/voice/index.js +10 -1
  188. package/dist/voice/index.js.map +1 -1
  189. package/dist/voice/remote_session.cjs +922 -0
  190. package/dist/voice/remote_session.cjs.map +1 -0
  191. package/dist/voice/remote_session.d.cts +108 -0
  192. package/dist/voice/remote_session.d.ts +108 -0
  193. package/dist/voice/remote_session.d.ts.map +1 -0
  194. package/dist/voice/remote_session.js +887 -0
  195. package/dist/voice/remote_session.js.map +1 -0
  196. package/dist/voice/report.cjs +11 -10
  197. package/dist/voice/report.cjs.map +1 -1
  198. package/dist/voice/report.d.cts +5 -3
  199. package/dist/voice/report.d.ts +5 -3
  200. package/dist/voice/report.d.ts.map +1 -1
  201. package/dist/voice/report.js +11 -10
  202. package/dist/voice/report.js.map +1 -1
  203. package/dist/voice/report.test.cjs +15 -0
  204. package/dist/voice/report.test.cjs.map +1 -1
  205. package/dist/voice/report.test.js +15 -0
  206. package/dist/voice/report.test.js.map +1 -1
  207. package/dist/voice/room_io/room_io.cjs +39 -0
  208. package/dist/voice/room_io/room_io.cjs.map +1 -1
  209. package/dist/voice/room_io/room_io.d.cts +3 -1
  210. package/dist/voice/room_io/room_io.d.ts +3 -1
  211. package/dist/voice/room_io/room_io.d.ts.map +1 -1
  212. package/dist/voice/room_io/room_io.js +40 -1
  213. package/dist/voice/room_io/room_io.js.map +1 -1
  214. package/dist/voice/turn_config/interruption.cjs.map +1 -1
  215. package/dist/voice/turn_config/interruption.d.cts +1 -1
  216. package/dist/voice/turn_config/interruption.d.ts +1 -1
  217. package/dist/voice/turn_config/interruption.d.ts.map +1 -1
  218. package/dist/voice/turn_config/interruption.js.map +1 -1
  219. package/dist/voice/turn_config/utils.cjs +95 -35
  220. package/dist/voice/turn_config/utils.cjs.map +1 -1
  221. package/dist/voice/turn_config/utils.d.cts +17 -5
  222. package/dist/voice/turn_config/utils.d.ts +17 -5
  223. package/dist/voice/turn_config/utils.d.ts.map +1 -1
  224. package/dist/voice/turn_config/utils.js +93 -35
  225. package/dist/voice/turn_config/utils.js.map +1 -1
  226. package/dist/voice/turn_config/utils.test.cjs +83 -41
  227. package/dist/voice/turn_config/utils.test.cjs.map +1 -1
  228. package/dist/voice/turn_config/utils.test.js +84 -42
  229. package/dist/voice/turn_config/utils.test.js.map +1 -1
  230. package/dist/worker.cjs +6 -29
  231. package/dist/worker.cjs.map +1 -1
  232. package/dist/worker.d.ts.map +1 -1
  233. package/dist/worker.js +6 -19
  234. package/dist/worker.js.map +1 -1
  235. package/package.json +3 -2
  236. package/src/cli.ts +2 -0
  237. package/src/constants.ts +1 -0
  238. package/src/cpu.test.ts +239 -0
  239. package/src/cpu.ts +173 -0
  240. package/src/index.ts +13 -15
  241. package/src/inference/interruption/defaults.ts +1 -1
  242. package/src/inference/interruption/http_transport.ts +49 -30
  243. package/src/inference/interruption/interruption_detector.ts +22 -6
  244. package/src/inference/interruption/interruption_stream.ts +4 -4
  245. package/src/inference/interruption/types.ts +2 -2
  246. package/src/inference/interruption/ws_transport.ts +63 -59
  247. package/src/inference/llm.ts +3 -1
  248. package/src/inference/stt.test.ts +17 -0
  249. package/src/inference/stt.ts +22 -14
  250. package/src/inference/tts.test.ts +12 -0
  251. package/src/inference/tts.ts +22 -6
  252. package/src/ipc/job_proc_lazy_main.ts +44 -24
  253. package/src/job.ts +1 -1
  254. package/src/language.test.ts +62 -0
  255. package/src/language.ts +380 -0
  256. package/src/llm/index.ts +2 -0
  257. package/src/stream/deferred_stream.ts +5 -1
  258. package/src/stt/stt.ts +2 -1
  259. package/src/utils.ts +20 -0
  260. package/src/voice/agent.test.ts +208 -1
  261. package/src/voice/agent.ts +21 -22
  262. package/src/voice/agent_activity.test.ts +194 -0
  263. package/src/voice/agent_activity.ts +161 -43
  264. package/src/voice/agent_session.ts +103 -92
  265. package/src/voice/audio_recognition.ts +124 -61
  266. package/src/voice/audio_recognition_span.test.ts +115 -35
  267. package/src/voice/events.ts +4 -3
  268. package/src/voice/index.ts +10 -1
  269. package/src/voice/remote_session.ts +1083 -0
  270. package/src/voice/report.test.ts +22 -3
  271. package/src/voice/report.ts +31 -14
  272. package/src/voice/room_io/room_io.ts +52 -2
  273. package/src/voice/turn_config/interruption.ts +1 -1
  274. package/src/voice/turn_config/utils.test.ts +91 -43
  275. package/src/voice/turn_config/utils.ts +120 -56
  276. package/src/worker.ts +34 -50
  277. package/dist/voice/client_events.cjs +0 -554
  278. package/dist/voice/client_events.cjs.map +0 -1
  279. package/dist/voice/client_events.d.cts +0 -195
  280. package/dist/voice/client_events.d.ts +0 -195
  281. package/dist/voice/client_events.d.ts.map +0 -1
  282. package/dist/voice/client_events.js +0 -548
  283. package/dist/voice/client_events.js.map +0 -1
  284. package/dist/voice/wire_format.cjs +0 -798
  285. package/dist/voice/wire_format.cjs.map +0 -1
  286. package/dist/voice/wire_format.d.cts +0 -5503
  287. package/dist/voice/wire_format.d.ts +0 -5503
  288. package/dist/voice/wire_format.d.ts.map +0 -1
  289. package/dist/voice/wire_format.js +0 -728
  290. package/dist/voice/wire_format.js.map +0 -1
  291. package/src/voice/client_events.ts +0 -838
  292. package/src/voice/wire_format.ts +0 -827
@@ -0,0 +1,922 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var remote_session_exports = {};
30
+ __export(remote_session_exports, {
31
+ RemoteSession: () => RemoteSession,
32
+ RoomSessionTransport: () => RoomSessionTransport,
33
+ SessionHost: () => SessionHost,
34
+ SessionTransport: () => SessionTransport
35
+ });
36
+ module.exports = __toCommonJS(remote_session_exports);
37
+ var import_protobuf = require("@bufbuild/protobuf");
38
+ var import_protocol = require("@livekit/protocol");
39
+ var import_events = __toESM(require("events"), 1);
40
+ var import_constants = require("../constants.cjs");
41
+ var import_log = require("../log.cjs");
42
+ var import_utils = require("../utils.cjs");
43
+ var import_events2 = require("./events.cjs");
44
+ class SessionTransport {
45
+ async start() {
46
+ }
47
+ }
48
+ class RoomSessionTransport extends SessionTransport {
49
+ room;
50
+ handlerRegistered = false;
51
+ closed = false;
52
+ pendingMessages = [];
53
+ waitingResolve = null;
54
+ roomIO;
55
+ constructor(room, roomIO) {
56
+ super();
57
+ this.room = room;
58
+ this.roomIO = roomIO;
59
+ }
60
+ getRemoteIdentity() {
61
+ var _a;
62
+ return (_a = this.roomIO.linkedParticipant) == null ? void 0 : _a.identity;
63
+ }
64
+ async start() {
65
+ if (this.handlerRegistered) return;
66
+ this.room.registerByteStreamHandler(import_constants.TOPIC_SESSION_MESSAGES, this.onByteStream);
67
+ this.handlerRegistered = true;
68
+ }
69
+ onByteStream = (reader, participantInfo) => {
70
+ if (this.getRemoteIdentity() && participantInfo.identity !== this.getRemoteIdentity()) {
71
+ return;
72
+ }
73
+ this.readStream(reader).catch((e) => {
74
+ (0, import_log.log)().warn({ error: e }, "failed to read binary stream message");
75
+ });
76
+ };
77
+ async readStream(reader) {
78
+ try {
79
+ const chunks = await reader.readAll();
80
+ let totalLength = 0;
81
+ for (const chunk of chunks) {
82
+ totalLength += chunk.length;
83
+ }
84
+ const data = new Uint8Array(totalLength);
85
+ let offset = 0;
86
+ for (const chunk of chunks) {
87
+ data.set(chunk, offset);
88
+ offset += chunk.length;
89
+ }
90
+ const msg = import_protocol.AgentSession.AgentSessionMessage.fromBinary(data);
91
+ this.enqueue(msg);
92
+ } catch (e) {
93
+ if (!this.closed) {
94
+ (0, import_log.log)().warn({ error: e }, "failed to parse binary stream message");
95
+ }
96
+ }
97
+ }
98
+ async sendMessage(msg) {
99
+ if (this.closed || !this.room.isConnected) return;
100
+ try {
101
+ const data = msg.toBinary();
102
+ const opts = {
103
+ topic: import_constants.TOPIC_SESSION_MESSAGES,
104
+ name: (0, import_utils.shortuuid)("AS_")
105
+ };
106
+ const remoteIdentity = this.getRemoteIdentity();
107
+ if (remoteIdentity) {
108
+ opts.destinationIdentities = [remoteIdentity];
109
+ }
110
+ const writer = await this.room.localParticipant.streamBytes(opts);
111
+ await writer.write(new Uint8Array(data));
112
+ await writer.close();
113
+ } catch (e) {
114
+ (0, import_log.log)().warn({ error: e }, "failed to send binary stream message");
115
+ }
116
+ }
117
+ async close() {
118
+ if (this.closed) return;
119
+ this.closed = true;
120
+ if (this.handlerRegistered) {
121
+ try {
122
+ this.room.unregisterByteStreamHandler(import_constants.TOPIC_SESSION_MESSAGES);
123
+ } catch (e) {
124
+ (0, import_log.log)().debug({ error: e }, "byte stream handler already unregistered");
125
+ }
126
+ this.handlerRegistered = false;
127
+ }
128
+ if (this.waitingResolve) {
129
+ this.waitingResolve({
130
+ value: void 0,
131
+ done: true
132
+ });
133
+ this.waitingResolve = null;
134
+ }
135
+ }
136
+ enqueue(msg) {
137
+ if (this.closed) return;
138
+ if (this.waitingResolve) {
139
+ const resolve = this.waitingResolve;
140
+ this.waitingResolve = null;
141
+ resolve({ value: msg, done: false });
142
+ } else {
143
+ this.pendingMessages.push(msg);
144
+ }
145
+ }
146
+ [Symbol.asyncIterator]() {
147
+ return {
148
+ next: () => {
149
+ if (this.closed && this.pendingMessages.length === 0) {
150
+ return Promise.resolve({
151
+ value: void 0,
152
+ done: true
153
+ });
154
+ }
155
+ const pending = this.pendingMessages.shift();
156
+ if (pending) {
157
+ return Promise.resolve({ value: pending, done: false });
158
+ }
159
+ return new Promise((resolve) => {
160
+ this.waitingResolve = resolve;
161
+ });
162
+ },
163
+ return: () => {
164
+ this.close();
165
+ return Promise.resolve({
166
+ value: void 0,
167
+ done: true
168
+ });
169
+ }
170
+ };
171
+ }
172
+ }
173
+ const AGENT_STATE_MAP = {
174
+ initializing: import_protocol.AgentSession.AgentState.AS_INITIALIZING,
175
+ idle: import_protocol.AgentSession.AgentState.AS_IDLE,
176
+ listening: import_protocol.AgentSession.AgentState.AS_LISTENING,
177
+ thinking: import_protocol.AgentSession.AgentState.AS_THINKING,
178
+ speaking: import_protocol.AgentSession.AgentState.AS_SPEAKING
179
+ };
180
+ const USER_STATE_MAP = {
181
+ speaking: import_protocol.AgentSession.UserState.US_SPEAKING,
182
+ listening: import_protocol.AgentSession.UserState.US_LISTENING,
183
+ away: import_protocol.AgentSession.UserState.US_AWAY
184
+ };
185
+ function msToTimestamp(ms) {
186
+ return import_protobuf.Timestamp.fromDate(new Date(ms));
187
+ }
188
+ function nowTimestamp() {
189
+ return import_protobuf.Timestamp.fromDate(/* @__PURE__ */ new Date());
190
+ }
191
+ function chatItemToProto(item) {
192
+ switch (item.type) {
193
+ case "message": {
194
+ const msg = item;
195
+ const roleMap = {
196
+ developer: import_protocol.AgentSession.ChatRole.DEVELOPER,
197
+ system: import_protocol.AgentSession.ChatRole.SYSTEM,
198
+ user: import_protocol.AgentSession.ChatRole.USER,
199
+ assistant: import_protocol.AgentSession.ChatRole.ASSISTANT
200
+ };
201
+ const content = [];
202
+ for (const c of msg.content) {
203
+ if (typeof c === "string") {
204
+ content.push(new import_protocol.AgentSession.ChatMessage_ChatContent({ payload: { case: "text", value: c } }));
205
+ }
206
+ }
207
+ const metricsReport = new import_protocol.AgentSession.MetricsReport();
208
+ if (msg.metrics.transcriptionDelay !== void 0)
209
+ metricsReport.transcriptionDelay = msg.metrics.transcriptionDelay;
210
+ if (msg.metrics.endOfTurnDelay !== void 0)
211
+ metricsReport.endOfTurnDelay = msg.metrics.endOfTurnDelay;
212
+ if (msg.metrics.onUserTurnCompletedDelay !== void 0)
213
+ metricsReport.onUserTurnCompletedDelay = msg.metrics.onUserTurnCompletedDelay;
214
+ if (msg.metrics.llmNodeTtft !== void 0)
215
+ metricsReport.llmNodeTtft = msg.metrics.llmNodeTtft;
216
+ if (msg.metrics.ttsNodeTtfb !== void 0)
217
+ metricsReport.ttsNodeTtfb = msg.metrics.ttsNodeTtfb;
218
+ if (msg.metrics.e2eLatency !== void 0) metricsReport.e2eLatency = msg.metrics.e2eLatency;
219
+ const pbMsg = new import_protocol.AgentSession.ChatMessage({
220
+ id: msg.id,
221
+ role: roleMap[msg.role] ?? import_protocol.AgentSession.ChatRole.ASSISTANT,
222
+ content,
223
+ interrupted: msg.interrupted,
224
+ metrics: metricsReport,
225
+ createdAt: msToTimestamp(msg.createdAt)
226
+ });
227
+ if (msg.transcriptConfidence !== void 0) {
228
+ pbMsg.transcriptConfidence = msg.transcriptConfidence;
229
+ }
230
+ return new import_protocol.AgentSession.ChatContext_ChatItem({ item: { case: "message", value: pbMsg } });
231
+ }
232
+ case "function_call": {
233
+ const fc = item;
234
+ return new import_protocol.AgentSession.ChatContext_ChatItem({
235
+ item: {
236
+ case: "functionCall",
237
+ value: new import_protocol.AgentSession.FunctionCall({
238
+ id: fc.id,
239
+ callId: fc.callId,
240
+ name: fc.name,
241
+ arguments: fc.args,
242
+ createdAt: msToTimestamp(fc.createdAt)
243
+ })
244
+ }
245
+ });
246
+ }
247
+ case "function_call_output": {
248
+ const fco = item;
249
+ return new import_protocol.AgentSession.ChatContext_ChatItem({
250
+ item: {
251
+ case: "functionCallOutput",
252
+ value: new import_protocol.AgentSession.FunctionCallOutput({
253
+ id: fco.id,
254
+ callId: fco.callId,
255
+ name: fco.name,
256
+ output: fco.output,
257
+ isError: fco.isError,
258
+ createdAt: msToTimestamp(fco.createdAt)
259
+ })
260
+ }
261
+ });
262
+ }
263
+ case "agent_handoff": {
264
+ const ah = item;
265
+ return new import_protocol.AgentSession.ChatContext_ChatItem({
266
+ item: {
267
+ case: "agentHandoff",
268
+ value: new import_protocol.AgentSession.AgentHandoff({
269
+ id: ah.id,
270
+ oldAgentId: ah.oldAgentId,
271
+ newAgentId: ah.newAgentId,
272
+ createdAt: msToTimestamp(ah.createdAt)
273
+ })
274
+ }
275
+ });
276
+ }
277
+ }
278
+ }
279
+ function sessionUsageToProto(usage) {
280
+ const modelUsages = [];
281
+ for (const mu of usage.modelUsage) {
282
+ switch (mu.type) {
283
+ case "llm_usage": {
284
+ const lu = mu;
285
+ modelUsages.push(
286
+ new import_protocol.AgentSession.ModelUsage({
287
+ usage: {
288
+ case: "llm",
289
+ value: new import_protocol.AgentSession.LLMModelUsage({
290
+ provider: lu.provider ?? "",
291
+ model: lu.model ?? "",
292
+ inputTokens: lu.inputTokens ?? 0,
293
+ inputCachedTokens: lu.inputCachedTokens ?? 0,
294
+ inputAudioTokens: lu.inputAudioTokens ?? 0,
295
+ inputCachedAudioTokens: lu.inputCachedAudioTokens ?? 0,
296
+ inputTextTokens: lu.inputTextTokens ?? 0,
297
+ inputCachedTextTokens: lu.inputCachedTextTokens ?? 0,
298
+ inputImageTokens: lu.inputImageTokens ?? 0,
299
+ inputCachedImageTokens: lu.inputCachedImageTokens ?? 0,
300
+ outputTokens: lu.outputTokens ?? 0,
301
+ outputAudioTokens: lu.outputAudioTokens ?? 0,
302
+ outputTextTokens: lu.outputTextTokens ?? 0,
303
+ sessionDuration: (lu.sessionDurationMs ?? 0) / 1e3
304
+ })
305
+ }
306
+ })
307
+ );
308
+ break;
309
+ }
310
+ case "tts_usage": {
311
+ const tu = mu;
312
+ modelUsages.push(
313
+ new import_protocol.AgentSession.ModelUsage({
314
+ usage: {
315
+ case: "tts",
316
+ value: new import_protocol.AgentSession.TTSModelUsage({
317
+ provider: tu.provider ?? "",
318
+ model: tu.model ?? "",
319
+ inputTokens: tu.inputTokens ?? 0,
320
+ outputTokens: tu.outputTokens ?? 0,
321
+ charactersCount: tu.charactersCount ?? 0,
322
+ audioDuration: (tu.audioDurationMs ?? 0) / 1e3
323
+ })
324
+ }
325
+ })
326
+ );
327
+ break;
328
+ }
329
+ case "stt_usage": {
330
+ const su = mu;
331
+ modelUsages.push(
332
+ new import_protocol.AgentSession.ModelUsage({
333
+ usage: {
334
+ case: "stt",
335
+ value: new import_protocol.AgentSession.STTModelUsage({
336
+ provider: su.provider ?? "",
337
+ model: su.model ?? "",
338
+ inputTokens: su.inputTokens ?? 0,
339
+ outputTokens: su.outputTokens ?? 0,
340
+ audioDuration: (su.audioDurationMs ?? 0) / 1e3
341
+ })
342
+ }
343
+ })
344
+ );
345
+ break;
346
+ }
347
+ case "interruption_usage": {
348
+ const iu = mu;
349
+ modelUsages.push(
350
+ new import_protocol.AgentSession.ModelUsage({
351
+ usage: {
352
+ case: "interruption",
353
+ value: new import_protocol.AgentSession.InterruptionModelUsage({
354
+ provider: iu.provider ?? "",
355
+ model: iu.model ?? "",
356
+ totalRequests: iu.totalRequests ?? 0
357
+ })
358
+ }
359
+ })
360
+ );
361
+ break;
362
+ }
363
+ }
364
+ }
365
+ return new import_protocol.AgentSession.AgentSessionUsage({ modelUsage: modelUsages });
366
+ }
367
+ function toolNames(toolCtx) {
368
+ if (!toolCtx) return [];
369
+ return Object.keys(toolCtx);
370
+ }
371
+ function protoSerializeOptions(opts) {
372
+ var _a, _b;
373
+ return {
374
+ endpointing: JSON.stringify(((_a = opts.turnHandling) == null ? void 0 : _a.endpointing) ?? {}),
375
+ interruption: JSON.stringify(((_b = opts.turnHandling) == null ? void 0 : _b.interruption) ?? {}),
376
+ max_tool_steps: String(opts.maxToolSteps ?? 0),
377
+ user_away_timeout: String(opts.userAwayTimeout ?? ""),
378
+ preemptive_generation: String(opts.preemptiveGeneration ?? false),
379
+ use_tts_aligned_transcript: String(opts.useTtsAlignedTranscript ?? false)
380
+ };
381
+ }
382
+ class SessionHost {
383
+ transport;
384
+ session;
385
+ started = false;
386
+ eventsRegistered = false;
387
+ recvTask;
388
+ tasks = /* @__PURE__ */ new Set();
389
+ textInputCb;
390
+ constructor(transport) {
391
+ this.transport = transport;
392
+ }
393
+ registerSession(session) {
394
+ this.session = session;
395
+ if (!this.eventsRegistered) {
396
+ this.eventsRegistered = true;
397
+ session.on(import_events2.AgentSessionEventTypes.AgentStateChanged, this.onAgentStateChanged);
398
+ session.on(import_events2.AgentSessionEventTypes.UserStateChanged, this.onUserStateChanged);
399
+ session.on(import_events2.AgentSessionEventTypes.ConversationItemAdded, this.onConversationItemAdded);
400
+ session.on(import_events2.AgentSessionEventTypes.UserInputTranscribed, this.onUserInputTranscribed);
401
+ session.on(import_events2.AgentSessionEventTypes.FunctionToolsExecuted, this.onFunctionToolsExecuted);
402
+ session.on(import_events2.AgentSessionEventTypes.MetricsCollected, this.onMetricsCollected);
403
+ session.on(import_events2.AgentSessionEventTypes.OverlappingSpeech, this.onOverlappingSpeech);
404
+ session.on(import_events2.AgentSessionEventTypes.Error, this.onHostError);
405
+ }
406
+ }
407
+ registerTextInput(textInputCb) {
408
+ this.textInputCb = textInputCb;
409
+ }
410
+ async start() {
411
+ if (this.started) return;
412
+ this.started = true;
413
+ await this.transport.start();
414
+ this.recvTask = import_utils.Task.from(async () => this.recvLoop());
415
+ }
416
+ async close() {
417
+ if (!this.started) return;
418
+ this.started = false;
419
+ if (this.session && this.eventsRegistered) {
420
+ this.eventsRegistered = false;
421
+ this.session.off(import_events2.AgentSessionEventTypes.AgentStateChanged, this.onAgentStateChanged);
422
+ this.session.off(import_events2.AgentSessionEventTypes.UserStateChanged, this.onUserStateChanged);
423
+ this.session.off(import_events2.AgentSessionEventTypes.ConversationItemAdded, this.onConversationItemAdded);
424
+ this.session.off(import_events2.AgentSessionEventTypes.UserInputTranscribed, this.onUserInputTranscribed);
425
+ this.session.off(import_events2.AgentSessionEventTypes.FunctionToolsExecuted, this.onFunctionToolsExecuted);
426
+ this.session.off(import_events2.AgentSessionEventTypes.MetricsCollected, this.onMetricsCollected);
427
+ this.session.off(import_events2.AgentSessionEventTypes.OverlappingSpeech, this.onOverlappingSpeech);
428
+ this.session.off(import_events2.AgentSessionEventTypes.Error, this.onHostError);
429
+ }
430
+ if (this.recvTask) {
431
+ this.recvTask.cancel();
432
+ }
433
+ await Promise.allSettled([...this.tasks].map((task) => task.cancelAndWait()));
434
+ this.tasks.clear();
435
+ await this.transport.close();
436
+ }
437
+ async recvLoop() {
438
+ try {
439
+ for await (const msg of this.transport) {
440
+ if (msg.message.case === "request") {
441
+ if (this.session) {
442
+ this.trackTask(
443
+ import_utils.Task.from(async () => this.handleRequestSafe(msg.message.value))
444
+ );
445
+ }
446
+ }
447
+ }
448
+ } catch (e) {
449
+ if (this.started) {
450
+ (0, import_log.log)().warn({ error: e }, "error processing session message");
451
+ }
452
+ }
453
+ }
454
+ sendEvent(event) {
455
+ const msg = new import_protocol.AgentSession.AgentSessionMessage({
456
+ message: { case: "event", value: event }
457
+ });
458
+ this.trackTask(import_utils.Task.from(async () => this.transport.sendMessage(msg)));
459
+ }
460
+ emitEvent(event, createdAt) {
461
+ this.sendEvent(
462
+ new import_protocol.AgentSession.AgentSessionEvent({
463
+ createdAt: createdAt ? msToTimestamp(createdAt) : nowTimestamp(),
464
+ event
465
+ })
466
+ );
467
+ }
468
+ onAgentStateChanged = (event) => {
469
+ this.emitEvent(
470
+ {
471
+ case: "agentStateChanged",
472
+ value: new import_protocol.AgentSession.AgentSessionEvent_AgentStateChanged({
473
+ oldState: AGENT_STATE_MAP[event.oldState],
474
+ newState: AGENT_STATE_MAP[event.newState]
475
+ })
476
+ },
477
+ event.createdAt
478
+ );
479
+ };
480
+ onUserStateChanged = (event) => {
481
+ this.emitEvent(
482
+ {
483
+ case: "userStateChanged",
484
+ value: new import_protocol.AgentSession.AgentSessionEvent_UserStateChanged({
485
+ oldState: USER_STATE_MAP[event.oldState],
486
+ newState: USER_STATE_MAP[event.newState]
487
+ })
488
+ },
489
+ event.createdAt
490
+ );
491
+ };
492
+ onUserInputTranscribed = (event) => {
493
+ this.emitEvent(
494
+ {
495
+ case: "userInputTranscribed",
496
+ value: new import_protocol.AgentSession.AgentSessionEvent_UserInputTranscribed({
497
+ transcript: event.transcript,
498
+ isFinal: event.isFinal
499
+ })
500
+ },
501
+ event.createdAt
502
+ );
503
+ };
504
+ onConversationItemAdded = (event) => {
505
+ this.emitEvent(
506
+ {
507
+ case: "conversationItemAdded",
508
+ value: new import_protocol.AgentSession.AgentSessionEvent_ConversationItemAdded({
509
+ item: chatItemToProto(event.item)
510
+ })
511
+ },
512
+ event.createdAt
513
+ );
514
+ };
515
+ onFunctionToolsExecuted = (event) => {
516
+ const pbCalls = event.functionCalls.map(
517
+ (fc) => new import_protocol.AgentSession.FunctionCall({ name: fc.name, arguments: fc.args, callId: fc.callId })
518
+ );
519
+ const pbOutputs = event.functionCallOutputs.filter((fco) => fco != null).map(
520
+ (fco) => new import_protocol.AgentSession.FunctionCallOutput({
521
+ callId: fco.callId,
522
+ output: fco.output,
523
+ isError: fco.isError
524
+ })
525
+ );
526
+ this.emitEvent(
527
+ {
528
+ case: "functionToolsExecuted",
529
+ value: new import_protocol.AgentSession.AgentSessionEvent_FunctionToolsExecuted({
530
+ functionCalls: pbCalls,
531
+ functionCallOutputs: pbOutputs
532
+ })
533
+ },
534
+ event.createdAt
535
+ );
536
+ };
537
+ onOverlappingSpeech = (event) => {
538
+ const value = new import_protocol.AgentSession.AgentSessionEvent_OverlappingSpeech({
539
+ isInterruption: event.isInterruption,
540
+ detectionDelay: event.detectionDelayInS,
541
+ detectedAt: msToTimestamp(event.detectedAt)
542
+ });
543
+ if (event.overlapStartedAt != null) {
544
+ value.overlapStartedAt = msToTimestamp(event.overlapStartedAt);
545
+ }
546
+ this.emitEvent({ case: "overlappingSpeech", value });
547
+ };
548
+ onMetricsCollected = (event) => {
549
+ if (!this.session) return;
550
+ this.emitEvent(
551
+ {
552
+ case: "sessionUsageUpdated",
553
+ value: new import_protocol.AgentSession.AgentSessionEvent_SessionUsageUpdated({
554
+ usage: sessionUsageToProto(this.session.usage)
555
+ })
556
+ },
557
+ event.createdAt
558
+ );
559
+ };
560
+ onHostError = (event) => {
561
+ this.emitEvent(
562
+ {
563
+ case: "error",
564
+ value: new import_protocol.AgentSession.AgentSessionEvent_Error({
565
+ message: event.error ? String(event.error) : "Unknown error"
566
+ })
567
+ },
568
+ event.createdAt
569
+ );
570
+ };
571
+ async handleRequestSafe(req) {
572
+ try {
573
+ await this.handleRequest(req);
574
+ } catch (e) {
575
+ (0, import_log.log)().warn({ error: e, requestId: req.requestId }, "error handling session request");
576
+ try {
577
+ const resp = new import_protocol.AgentSession.AgentSessionMessage({
578
+ message: {
579
+ case: "response",
580
+ value: new import_protocol.AgentSession.SessionResponse({
581
+ requestId: req.requestId,
582
+ error: "internal error"
583
+ })
584
+ }
585
+ });
586
+ await this.transport.sendMessage(resp);
587
+ } catch (e2) {
588
+ (0, import_log.log)().debug({ error: e2 }, "failed to send error response");
589
+ }
590
+ }
591
+ }
592
+ async handleRequest(req) {
593
+ if (!this.session) return;
594
+ switch (req.request.case) {
595
+ case "ping":
596
+ return this.sendResponse(req.requestId, {
597
+ case: "pong",
598
+ value: new import_protocol.AgentSession.SessionResponse_Pong()
599
+ });
600
+ case "getChatHistory":
601
+ return this.handleGetChatHistory(req.requestId);
602
+ case "getAgentInfo":
603
+ return this.handleGetAgentInfo(req.requestId);
604
+ case "runInput":
605
+ return this.handleRunInput(req.requestId, req.request.value);
606
+ case "getSessionState":
607
+ return this.handleGetSessionState(req.requestId);
608
+ case "getRtcStats":
609
+ return this.sendResponse(req.requestId, {
610
+ case: "getRtcStats",
611
+ value: new import_protocol.AgentSession.SessionResponse_GetRTCStatsResponse({
612
+ publisherStats: [],
613
+ subscriberStats: []
614
+ })
615
+ });
616
+ case "getSessionUsage":
617
+ return this.handleGetSessionUsage(req.requestId);
618
+ }
619
+ }
620
+ async handleGetChatHistory(requestId) {
621
+ const items = this.session.history.items.map(chatItemToProto);
622
+ return this.sendResponse(requestId, {
623
+ case: "getChatHistory",
624
+ value: new import_protocol.AgentSession.SessionResponse_GetChatHistoryResponse({ items })
625
+ });
626
+ }
627
+ async handleGetAgentInfo(requestId) {
628
+ const agent = this.session.currentAgent;
629
+ return this.sendResponse(requestId, {
630
+ case: "getAgentInfo",
631
+ value: new import_protocol.AgentSession.SessionResponse_GetAgentInfoResponse({
632
+ id: agent.id,
633
+ instructions: agent.instructions,
634
+ tools: toolNames(agent.toolCtx),
635
+ chatCtx: agent.chatCtx.items.map(chatItemToProto)
636
+ })
637
+ });
638
+ }
639
+ async handleRunInput(requestId, input) {
640
+ const text = input.text;
641
+ let items = [];
642
+ let error;
643
+ if (text) {
644
+ if (this.textInputCb) {
645
+ const cbResult = this.textInputCb(this.session, { text });
646
+ if (cbResult instanceof Promise) {
647
+ await cbResult;
648
+ }
649
+ } else {
650
+ try {
651
+ await this.session.interrupt({ force: true }).await;
652
+ } catch {
653
+ }
654
+ const result = this.session.run({ userInput: text });
655
+ try {
656
+ await result.wait();
657
+ } catch (e) {
658
+ error = e instanceof Error ? e.message : String(e);
659
+ }
660
+ items = result.events.map((ev) => chatItemToProto(ev.item));
661
+ }
662
+ }
663
+ return this.sendResponse(
664
+ requestId,
665
+ {
666
+ case: "runInput",
667
+ value: new import_protocol.AgentSession.SessionResponse_RunInputResponse({ items })
668
+ },
669
+ error
670
+ );
671
+ }
672
+ async handleGetSessionState(requestId) {
673
+ const agent = this.session.currentAgent;
674
+ const startedAt = this.session._startedAt ?? Date.now();
675
+ return this.sendResponse(requestId, {
676
+ case: "getSessionState",
677
+ value: new import_protocol.AgentSession.SessionResponse_GetSessionStateResponse({
678
+ agentState: AGENT_STATE_MAP[this.session.agentState],
679
+ userState: USER_STATE_MAP[this.session.userState],
680
+ agentId: agent.id,
681
+ options: protoSerializeOptions({
682
+ turnHandling: this.session.sessionOptions.turnHandling,
683
+ maxToolSteps: this.session.sessionOptions.maxToolSteps,
684
+ userAwayTimeout: this.session.sessionOptions.userAwayTimeout,
685
+ preemptiveGeneration: this.session.sessionOptions.preemptiveGeneration,
686
+ useTtsAlignedTranscript: this.session.sessionOptions.useTtsAlignedTranscript
687
+ }),
688
+ createdAt: msToTimestamp(startedAt)
689
+ })
690
+ });
691
+ }
692
+ async handleGetSessionUsage(requestId) {
693
+ return this.sendResponse(requestId, {
694
+ case: "getSessionUsage",
695
+ value: new import_protocol.AgentSession.SessionResponse_GetSessionUsageResponse({
696
+ usage: sessionUsageToProto(this.session.usage),
697
+ createdAt: nowTimestamp()
698
+ })
699
+ });
700
+ }
701
+ async sendResponse(requestId, response, error) {
702
+ await this.transport.sendMessage(
703
+ new import_protocol.AgentSession.AgentSessionMessage({
704
+ message: {
705
+ case: "response",
706
+ value: new import_protocol.AgentSession.SessionResponse({ requestId, response, error })
707
+ }
708
+ })
709
+ );
710
+ }
711
+ trackTask(task) {
712
+ this.tasks.add(task);
713
+ task.addDoneCallback(() => {
714
+ this.tasks.delete(task);
715
+ });
716
+ }
717
+ }
718
+ class RemoteSession extends import_events.default {
719
+ transport;
720
+ started = false;
721
+ tasks = /* @__PURE__ */ new Set();
722
+ pendingRequests = /* @__PURE__ */ new Map();
723
+ recvTask;
724
+ _logger = (0, import_log.log)();
725
+ constructor(transport) {
726
+ super();
727
+ this.transport = transport;
728
+ }
729
+ static fromRoom(room, roomIO) {
730
+ const transport = new RoomSessionTransport(room, roomIO);
731
+ return new RemoteSession(transport);
732
+ }
733
+ async start() {
734
+ if (this.started) return;
735
+ this.started = true;
736
+ await this.transport.start();
737
+ this.recvTask = import_utils.Task.from(async () => this.recvLoop());
738
+ }
739
+ async close() {
740
+ if (!this.started) return;
741
+ this.started = false;
742
+ if (this.recvTask) {
743
+ this.recvTask.cancel();
744
+ }
745
+ for (const pending of this.pendingRequests.values()) {
746
+ pending.reject(new Error("RemoteSession closed"));
747
+ }
748
+ this.pendingRequests.clear();
749
+ for (const task of this.tasks) {
750
+ task.cancel();
751
+ }
752
+ this.tasks.clear();
753
+ await this.transport.close();
754
+ }
755
+ async recvLoop() {
756
+ try {
757
+ for await (const msg of this.transport) {
758
+ switch (msg.message.case) {
759
+ case "event":
760
+ this.dispatchEvent(msg.message.value);
761
+ break;
762
+ case "response":
763
+ this.dispatchResponse(msg.message.value);
764
+ break;
765
+ }
766
+ }
767
+ } catch (e) {
768
+ if (this.started) {
769
+ this._logger.warn({ error: e }, "error in RemoteSession recv loop");
770
+ }
771
+ }
772
+ }
773
+ dispatchEvent(event) {
774
+ const ev = event.event;
775
+ switch (ev.case) {
776
+ case "agentStateChanged":
777
+ this.emit("agent_state_changed", ev.value);
778
+ break;
779
+ case "userStateChanged":
780
+ this.emit("user_state_changed", ev.value);
781
+ break;
782
+ case "userInputTranscribed":
783
+ this.emit("user_input_transcribed", ev.value);
784
+ break;
785
+ case "conversationItemAdded":
786
+ this.emit("conversation_item_added", ev.value);
787
+ break;
788
+ case "functionToolsExecuted":
789
+ this.emit("function_tools_executed", ev.value);
790
+ break;
791
+ case "overlappingSpeech":
792
+ this.emit("overlapping_speech", ev.value);
793
+ break;
794
+ case "sessionUsageUpdated":
795
+ this.emit("session_usage", ev.value);
796
+ break;
797
+ case "error":
798
+ this.emit("error", ev.value);
799
+ break;
800
+ }
801
+ }
802
+ dispatchResponse(response) {
803
+ const future = this.pendingRequests.get(response.requestId);
804
+ this.pendingRequests.delete(response.requestId);
805
+ if (future && !future.done) {
806
+ future.resolve(response);
807
+ }
808
+ }
809
+ async sendRequest(buildReq, timeout = 6e4) {
810
+ const requestId = (0, import_utils.shortuuid)("req_");
811
+ const req = buildReq(requestId);
812
+ req.requestId = requestId;
813
+ const future = new import_utils.Future();
814
+ this.pendingRequests.set(requestId, future);
815
+ const msg = new import_protocol.AgentSession.AgentSessionMessage({
816
+ message: { case: "request", value: req }
817
+ });
818
+ await this.transport.sendMessage(msg);
819
+ const timer = setTimeout(() => {
820
+ if (!future.done) {
821
+ this.pendingRequests.delete(requestId);
822
+ future.reject(new Error("RemoteSession request timed out"));
823
+ }
824
+ }, timeout);
825
+ try {
826
+ const response = await future.await;
827
+ if (response.error) {
828
+ throw new Error(response.error);
829
+ }
830
+ return response;
831
+ } finally {
832
+ clearTimeout(timer);
833
+ }
834
+ }
835
+ async fetchSessionState() {
836
+ const resp = await this.sendRequest(
837
+ (id) => new import_protocol.AgentSession.SessionRequest({
838
+ requestId: id,
839
+ request: { case: "getSessionState", value: new import_protocol.AgentSession.SessionRequest_GetSessionState() }
840
+ })
841
+ );
842
+ if (resp.response.case !== "getSessionState") {
843
+ throw new Error("unexpected response type");
844
+ }
845
+ return resp.response.value;
846
+ }
847
+ async fetchChatHistory() {
848
+ const resp = await this.sendRequest(
849
+ (id) => new import_protocol.AgentSession.SessionRequest({
850
+ requestId: id,
851
+ request: { case: "getChatHistory", value: new import_protocol.AgentSession.SessionRequest_GetChatHistory() }
852
+ })
853
+ );
854
+ if (resp.response.case !== "getChatHistory") {
855
+ throw new Error("unexpected response type");
856
+ }
857
+ return resp.response.value;
858
+ }
859
+ async fetchAgentInfo() {
860
+ const resp = await this.sendRequest(
861
+ (id) => new import_protocol.AgentSession.SessionRequest({
862
+ requestId: id,
863
+ request: { case: "getAgentInfo", value: new import_protocol.AgentSession.SessionRequest_GetAgentInfo() }
864
+ })
865
+ );
866
+ if (resp.response.case !== "getAgentInfo") {
867
+ throw new Error("unexpected response type");
868
+ }
869
+ return resp.response.value;
870
+ }
871
+ async sendMessage(text, responseTimeout = 6e4) {
872
+ const resp = await this.sendRequest(
873
+ (id) => new import_protocol.AgentSession.SessionRequest({
874
+ requestId: id,
875
+ request: { case: "runInput", value: new import_protocol.AgentSession.SessionRequest_RunInput({ text }) }
876
+ }),
877
+ responseTimeout
878
+ );
879
+ if (resp.response.case !== "runInput") {
880
+ throw new Error("unexpected response type");
881
+ }
882
+ return resp.response.value;
883
+ }
884
+ async fetchRtcStats() {
885
+ const resp = await this.sendRequest(
886
+ (id) => new import_protocol.AgentSession.SessionRequest({
887
+ requestId: id,
888
+ request: { case: "getRtcStats", value: new import_protocol.AgentSession.SessionRequest_GetRTCStats() }
889
+ })
890
+ );
891
+ if (resp.response.case !== "getRtcStats") {
892
+ throw new Error("unexpected response type");
893
+ }
894
+ return resp.response.value;
895
+ }
896
+ async fetchSessionUsage() {
897
+ const resp = await this.sendRequest(
898
+ (id) => new import_protocol.AgentSession.SessionRequest({
899
+ requestId: id,
900
+ request: { case: "getSessionUsage", value: new import_protocol.AgentSession.SessionRequest_GetSessionUsage() }
901
+ })
902
+ );
903
+ if (resp.response.case !== "getSessionUsage") {
904
+ throw new Error("unexpected response type");
905
+ }
906
+ return resp.response.value;
907
+ }
908
+ trackTask(task) {
909
+ this.tasks.add(task);
910
+ task.addDoneCallback(() => {
911
+ this.tasks.delete(task);
912
+ });
913
+ }
914
+ }
915
+ // Annotate the CommonJS export names for ESM import in node:
916
+ 0 && (module.exports = {
917
+ RemoteSession,
918
+ RoomSessionTransport,
919
+ SessionHost,
920
+ SessionTransport
921
+ });
922
+ //# sourceMappingURL=remote_session.cjs.map