@livekit/agents 0.4.6 → 0.5.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 (197) hide show
  1. package/README.md +17 -0
  2. package/dist/audio.cjs +77 -0
  3. package/dist/audio.cjs.map +1 -0
  4. package/dist/audio.js +48 -37
  5. package/dist/audio.js.map +1 -1
  6. package/dist/cli.cjs +131 -0
  7. package/dist/cli.cjs.map +1 -0
  8. package/dist/cli.js +96 -122
  9. package/dist/cli.js.map +1 -1
  10. package/dist/generator.cjs +36 -0
  11. package/dist/generator.cjs.map +1 -0
  12. package/dist/generator.js +8 -22
  13. package/dist/generator.js.map +1 -1
  14. package/dist/http_server.cjs +72 -0
  15. package/dist/http_server.cjs.map +1 -0
  16. package/dist/http_server.d.ts +1 -1
  17. package/dist/http_server.js +44 -47
  18. package/dist/http_server.js.map +1 -1
  19. package/dist/index.cjs +78 -0
  20. package/dist/index.cjs.map +1 -0
  21. package/dist/index.js +26 -28
  22. package/dist/index.js.map +1 -1
  23. package/dist/ipc/job_executor.cjs +33 -0
  24. package/dist/ipc/job_executor.cjs.map +1 -0
  25. package/dist/ipc/job_executor.js +7 -4
  26. package/dist/ipc/job_executor.js.map +1 -1
  27. package/dist/ipc/job_main.cjs +147 -0
  28. package/dist/ipc/job_main.cjs.map +1 -0
  29. package/dist/ipc/job_main.d.ts +1 -1
  30. package/dist/ipc/job_main.js +103 -103
  31. package/dist/ipc/job_main.js.map +1 -1
  32. package/dist/ipc/message.cjs +17 -0
  33. package/dist/ipc/message.cjs.map +1 -0
  34. package/dist/ipc/message.js +0 -1
  35. package/dist/ipc/message.js.map +1 -1
  36. package/dist/ipc/proc_job_executor.cjs +174 -0
  37. package/dist/ipc/proc_job_executor.cjs.map +1 -0
  38. package/dist/ipc/proc_job_executor.js +130 -126
  39. package/dist/ipc/proc_job_executor.js.map +1 -1
  40. package/dist/ipc/proc_pool.cjs +126 -0
  41. package/dist/ipc/proc_pool.cjs.map +1 -0
  42. package/dist/ipc/proc_pool.js +93 -96
  43. package/dist/ipc/proc_pool.js.map +1 -1
  44. package/dist/job.cjs +230 -0
  45. package/dist/job.cjs.map +1 -0
  46. package/dist/job.js +195 -198
  47. package/dist/job.js.map +1 -1
  48. package/dist/llm/chat_context.cjs +131 -0
  49. package/dist/llm/chat_context.cjs.map +1 -0
  50. package/dist/llm/chat_context.js +98 -86
  51. package/dist/llm/chat_context.js.map +1 -1
  52. package/dist/llm/function_context.cjs +103 -0
  53. package/dist/llm/function_context.cjs.map +1 -0
  54. package/dist/llm/function_context.js +72 -81
  55. package/dist/llm/function_context.js.map +1 -1
  56. package/dist/llm/function_context.test.cjs +218 -0
  57. package/dist/llm/function_context.test.cjs.map +1 -0
  58. package/dist/llm/function_context.test.js +209 -210
  59. package/dist/llm/function_context.test.js.map +1 -1
  60. package/dist/llm/index.cjs +43 -0
  61. package/dist/llm/index.cjs.map +1 -0
  62. package/dist/llm/index.js +22 -6
  63. package/dist/llm/index.js.map +1 -1
  64. package/dist/llm/llm.cjs +76 -0
  65. package/dist/llm/llm.cjs.map +1 -0
  66. package/dist/llm/llm.js +48 -42
  67. package/dist/llm/llm.js.map +1 -1
  68. package/dist/log.cjs +57 -0
  69. package/dist/log.cjs.map +1 -0
  70. package/dist/log.js +27 -26
  71. package/dist/log.js.map +1 -1
  72. package/dist/multimodal/agent_playout.cjs +228 -0
  73. package/dist/multimodal/agent_playout.cjs.map +1 -0
  74. package/dist/multimodal/agent_playout.d.ts +1 -1
  75. package/dist/multimodal/agent_playout.js +193 -180
  76. package/dist/multimodal/agent_playout.js.map +1 -1
  77. package/dist/multimodal/index.cjs +25 -0
  78. package/dist/multimodal/index.cjs.map +1 -0
  79. package/dist/multimodal/index.js +2 -5
  80. package/dist/multimodal/index.js.map +1 -1
  81. package/dist/multimodal/multimodal_agent.cjs +404 -0
  82. package/dist/multimodal/multimodal_agent.cjs.map +1 -0
  83. package/dist/multimodal/multimodal_agent.d.ts +1 -1
  84. package/dist/multimodal/multimodal_agent.js +351 -330
  85. package/dist/multimodal/multimodal_agent.js.map +1 -1
  86. package/dist/pipeline/agent_output.cjs +172 -0
  87. package/dist/pipeline/agent_output.cjs.map +1 -0
  88. package/dist/pipeline/agent_output.js +136 -138
  89. package/dist/pipeline/agent_output.js.map +1 -1
  90. package/dist/pipeline/agent_playout.cjs +169 -0
  91. package/dist/pipeline/agent_playout.cjs.map +1 -0
  92. package/dist/pipeline/agent_playout.js +126 -136
  93. package/dist/pipeline/agent_playout.js.map +1 -1
  94. package/dist/pipeline/human_input.cjs +158 -0
  95. package/dist/pipeline/human_input.cjs.map +1 -0
  96. package/dist/pipeline/human_input.js +124 -125
  97. package/dist/pipeline/human_input.js.map +1 -1
  98. package/dist/pipeline/index.cjs +31 -0
  99. package/dist/pipeline/index.cjs.map +1 -0
  100. package/dist/pipeline/index.js +8 -4
  101. package/dist/pipeline/index.js.map +1 -1
  102. package/dist/pipeline/pipeline_agent.cjs +642 -0
  103. package/dist/pipeline/pipeline_agent.cjs.map +1 -0
  104. package/dist/pipeline/pipeline_agent.js +595 -651
  105. package/dist/pipeline/pipeline_agent.js.map +1 -1
  106. package/dist/pipeline/speech_handle.cjs +128 -0
  107. package/dist/pipeline/speech_handle.cjs.map +1 -0
  108. package/dist/pipeline/speech_handle.js +102 -100
  109. package/dist/pipeline/speech_handle.js.map +1 -1
  110. package/dist/plugin.cjs +46 -0
  111. package/dist/plugin.cjs.map +1 -0
  112. package/dist/plugin.js +20 -20
  113. package/dist/plugin.js.map +1 -1
  114. package/dist/stt/index.cjs +38 -0
  115. package/dist/stt/index.cjs.map +1 -0
  116. package/dist/stt/index.js +13 -5
  117. package/dist/stt/index.js.map +1 -1
  118. package/dist/stt/stream_adapter.cjs +87 -0
  119. package/dist/stt/stream_adapter.cjs.map +1 -0
  120. package/dist/stt/stream_adapter.js +58 -55
  121. package/dist/stt/stream_adapter.js.map +1 -1
  122. package/dist/stt/stt.cjs +98 -0
  123. package/dist/stt/stt.cjs.map +1 -0
  124. package/dist/stt/stt.js +63 -98
  125. package/dist/stt/stt.js.map +1 -1
  126. package/dist/tokenize/basic/basic.cjs +98 -0
  127. package/dist/tokenize/basic/basic.cjs.map +1 -0
  128. package/dist/tokenize/basic/basic.js +56 -45
  129. package/dist/tokenize/basic/basic.js.map +1 -1
  130. package/dist/tokenize/basic/hyphenator.cjs +425 -0
  131. package/dist/tokenize/basic/hyphenator.cjs.map +1 -0
  132. package/dist/tokenize/basic/hyphenator.js +66 -82
  133. package/dist/tokenize/basic/hyphenator.js.map +1 -1
  134. package/dist/tokenize/basic/index.cjs +35 -0
  135. package/dist/tokenize/basic/index.cjs.map +1 -0
  136. package/dist/tokenize/basic/index.js +7 -4
  137. package/dist/tokenize/basic/index.js.map +1 -1
  138. package/dist/tokenize/basic/paragraph.cjs +57 -0
  139. package/dist/tokenize/basic/paragraph.cjs.map +1 -0
  140. package/dist/tokenize/basic/paragraph.js +30 -35
  141. package/dist/tokenize/basic/paragraph.js.map +1 -1
  142. package/dist/tokenize/basic/sentence.cjs +83 -0
  143. package/dist/tokenize/basic/sentence.cjs.map +1 -0
  144. package/dist/tokenize/basic/sentence.js +56 -57
  145. package/dist/tokenize/basic/sentence.js.map +1 -1
  146. package/dist/tokenize/basic/word.cjs +44 -0
  147. package/dist/tokenize/basic/word.cjs.map +1 -0
  148. package/dist/tokenize/basic/word.js +17 -20
  149. package/dist/tokenize/basic/word.js.map +1 -1
  150. package/dist/tokenize/index.cjs +55 -0
  151. package/dist/tokenize/index.cjs.map +1 -0
  152. package/dist/tokenize/index.js +18 -7
  153. package/dist/tokenize/index.js.map +1 -1
  154. package/dist/tokenize/token_stream.cjs +164 -0
  155. package/dist/tokenize/token_stream.cjs.map +1 -0
  156. package/dist/tokenize/token_stream.js +133 -139
  157. package/dist/tokenize/token_stream.js.map +1 -1
  158. package/dist/tokenize/tokenizer.cjs +184 -0
  159. package/dist/tokenize/tokenizer.cjs.map +1 -0
  160. package/dist/tokenize/tokenizer.js +138 -99
  161. package/dist/tokenize/tokenizer.js.map +1 -1
  162. package/dist/transcription.cjs +131 -0
  163. package/dist/transcription.cjs.map +1 -0
  164. package/dist/transcription.js +99 -96
  165. package/dist/transcription.js.map +1 -1
  166. package/dist/tts/index.cjs +38 -0
  167. package/dist/tts/index.cjs.map +1 -0
  168. package/dist/tts/index.js +13 -5
  169. package/dist/tts/index.js.map +1 -1
  170. package/dist/tts/stream_adapter.cjs +78 -0
  171. package/dist/tts/stream_adapter.cjs.map +1 -0
  172. package/dist/tts/stream_adapter.js +50 -47
  173. package/dist/tts/stream_adapter.js.map +1 -1
  174. package/dist/tts/tts.cjs +127 -0
  175. package/dist/tts/tts.cjs.map +1 -0
  176. package/dist/tts/tts.js +90 -120
  177. package/dist/tts/tts.js.map +1 -1
  178. package/dist/utils.cjs +284 -0
  179. package/dist/utils.cjs.map +1 -0
  180. package/dist/utils.js +242 -247
  181. package/dist/utils.js.map +1 -1
  182. package/dist/vad.cjs +92 -0
  183. package/dist/vad.cjs.map +1 -0
  184. package/dist/vad.js +57 -52
  185. package/dist/vad.js.map +1 -1
  186. package/dist/version.cjs +29 -0
  187. package/dist/version.cjs.map +1 -0
  188. package/dist/version.js +4 -4
  189. package/dist/version.js.map +1 -1
  190. package/dist/worker.cjs +576 -0
  191. package/dist/worker.cjs.map +1 -0
  192. package/dist/worker.d.ts +1 -1
  193. package/dist/worker.js +511 -484
  194. package/dist/worker.js.map +1 -1
  195. package/package.json +18 -8
  196. package/src/ipc/job_main.ts +66 -64
  197. package/src/pipeline/pipeline_agent.ts +23 -23
package/dist/job.js CHANGED
@@ -1,205 +1,202 @@
1
- import { ParticipantKind, RoomEvent, TrackKind } from '@livekit/rtc-node';
2
- import { log } from './log.js';
3
- /** Which tracks, if any, should the agent automatically subscribe to? */
4
- export var AutoSubscribe;
5
- (function (AutoSubscribe) {
6
- AutoSubscribe[AutoSubscribe["SUBSCRIBE_ALL"] = 0] = "SUBSCRIBE_ALL";
7
- AutoSubscribe[AutoSubscribe["SUBSCRIBE_NONE"] = 1] = "SUBSCRIBE_NONE";
8
- AutoSubscribe[AutoSubscribe["VIDEO_ONLY"] = 2] = "VIDEO_ONLY";
9
- AutoSubscribe[AutoSubscribe["AUDIO_ONLY"] = 3] = "AUDIO_ONLY";
10
- })(AutoSubscribe || (AutoSubscribe = {}));
11
- /** Attempted to add a function callback, but the function already exists. */
12
- export class FunctionExistsError extends Error {
13
- constructor(msg) {
14
- super(msg);
15
- Object.setPrototypeOf(this, new.target.prototype);
16
- }
1
+ import { ParticipantKind, RoomEvent, TrackKind } from "@livekit/rtc-node";
2
+ import { log } from "./log.js";
3
+ var AutoSubscribe = /* @__PURE__ */ ((AutoSubscribe2) => {
4
+ AutoSubscribe2[AutoSubscribe2["SUBSCRIBE_ALL"] = 0] = "SUBSCRIBE_ALL";
5
+ AutoSubscribe2[AutoSubscribe2["SUBSCRIBE_NONE"] = 1] = "SUBSCRIBE_NONE";
6
+ AutoSubscribe2[AutoSubscribe2["VIDEO_ONLY"] = 2] = "VIDEO_ONLY";
7
+ AutoSubscribe2[AutoSubscribe2["AUDIO_ONLY"] = 3] = "AUDIO_ONLY";
8
+ return AutoSubscribe2;
9
+ })(AutoSubscribe || {});
10
+ class FunctionExistsError extends Error {
11
+ constructor(msg) {
12
+ super(msg);
13
+ Object.setPrototypeOf(this, new.target.prototype);
14
+ }
17
15
  }
18
- /** The job and environment context as seen by the agent, accessible by the entrypoint function. */
19
- export class JobContext {
20
- #proc;
21
- #info;
22
- #room;
23
- #onConnect;
24
- #onShutdown;
25
- /** @internal */
26
- shutdownCallbacks = [];
27
- #participantEntrypoints = [];
28
- #participantTasks = {};
29
- #logger;
30
- constructor(proc, info, room, onConnect, onShutdown) {
31
- this.#proc = proc;
32
- this.#info = info;
33
- this.#room = room;
34
- this.#onConnect = onConnect;
35
- this.#onShutdown = onShutdown;
36
- this.onParticipantConnected = this.onParticipantConnected.bind(this);
37
- this.#room.on(RoomEvent.ParticipantConnected, this.onParticipantConnected);
38
- this.#logger = log().child({ info: this.#info });
39
- }
40
- get proc() {
41
- return this.#proc;
42
- }
43
- get job() {
44
- return this.#info.job;
45
- }
46
- /** @returns The room the agent was called into */
47
- get room() {
48
- return this.#room;
49
- }
50
- /** @returns The agent's participant if connected to the room, otherwise `undefined` */
51
- get agent() {
52
- return this.#room.localParticipant;
53
- }
54
- /** Adds a promise to be awaited when {@link JobContext.shutdown | shutdown} is called. */
55
- addShutdownCallback(callback) {
56
- this.shutdownCallbacks.push(callback);
57
- }
58
- async waitForParticipant(identity) {
59
- if (!this.#room.isConnected) {
60
- throw new Error('room is not connected');
16
+ class JobContext {
17
+ #proc;
18
+ #info;
19
+ #room;
20
+ #onConnect;
21
+ #onShutdown;
22
+ /** @internal */
23
+ shutdownCallbacks = [];
24
+ #participantEntrypoints = [];
25
+ #participantTasks = {};
26
+ #logger;
27
+ constructor(proc, info, room, onConnect, onShutdown) {
28
+ this.#proc = proc;
29
+ this.#info = info;
30
+ this.#room = room;
31
+ this.#onConnect = onConnect;
32
+ this.#onShutdown = onShutdown;
33
+ this.onParticipantConnected = this.onParticipantConnected.bind(this);
34
+ this.#room.on(RoomEvent.ParticipantConnected, this.onParticipantConnected);
35
+ this.#logger = log().child({ info: this.#info });
36
+ }
37
+ get proc() {
38
+ return this.#proc;
39
+ }
40
+ get job() {
41
+ return this.#info.job;
42
+ }
43
+ /** @returns The room the agent was called into */
44
+ get room() {
45
+ return this.#room;
46
+ }
47
+ /** @returns The agent's participant if connected to the room, otherwise `undefined` */
48
+ get agent() {
49
+ return this.#room.localParticipant;
50
+ }
51
+ /** Adds a promise to be awaited when {@link JobContext.shutdown | shutdown} is called. */
52
+ addShutdownCallback(callback) {
53
+ this.shutdownCallbacks.push(callback);
54
+ }
55
+ async waitForParticipant(identity) {
56
+ if (!this.#room.isConnected) {
57
+ throw new Error("room is not connected");
58
+ }
59
+ for (const p of this.#room.remoteParticipants.values()) {
60
+ if ((!identity || p.identity === identity) && p.info.kind != ParticipantKind.AGENT) {
61
+ return p;
62
+ }
63
+ }
64
+ return new Promise((resolve, reject) => {
65
+ const onParticipantConnected = (participant) => {
66
+ if ((!identity || participant.identity === identity) && participant.info.kind != ParticipantKind.AGENT) {
67
+ clearHandlers();
68
+ resolve(participant);
61
69
  }
62
- for (const p of this.#room.remoteParticipants.values()) {
63
- if ((!identity || p.identity === identity) && p.info.kind != ParticipantKind.AGENT) {
64
- return p;
65
- }
66
- }
67
- return new Promise((resolve, reject) => {
68
- const onParticipantConnected = (participant) => {
69
- if ((!identity || participant.identity === identity) &&
70
- participant.info.kind != ParticipantKind.AGENT) {
71
- clearHandlers();
72
- resolve(participant);
73
- }
74
- };
75
- const onDisconnected = () => {
76
- clearHandlers();
77
- reject(new Error('Room disconnected while waiting for participant'));
78
- };
79
- const clearHandlers = () => {
80
- this.#room.off(RoomEvent.ParticipantConnected, onParticipantConnected);
81
- this.#room.off(RoomEvent.Disconnected, onDisconnected);
82
- };
83
- this.#room.on(RoomEvent.ParticipantConnected, onParticipantConnected);
84
- this.#room.on(RoomEvent.Disconnected, onDisconnected);
70
+ };
71
+ const onDisconnected = () => {
72
+ clearHandlers();
73
+ reject(new Error("Room disconnected while waiting for participant"));
74
+ };
75
+ const clearHandlers = () => {
76
+ this.#room.off(RoomEvent.ParticipantConnected, onParticipantConnected);
77
+ this.#room.off(RoomEvent.Disconnected, onDisconnected);
78
+ };
79
+ this.#room.on(RoomEvent.ParticipantConnected, onParticipantConnected);
80
+ this.#room.on(RoomEvent.Disconnected, onDisconnected);
81
+ });
82
+ }
83
+ /**
84
+ * Connects the agent to the room.
85
+ *
86
+ * @remarks
87
+ * It is recommended to run this command as early in the function as possible, as executing it
88
+ * later may cause noticeable delay between user and agent joins.
89
+ *
90
+ * @see {@link https://github.com/livekit/node-sdks/tree/main/packages/livekit-rtc#readme |
91
+ * @livekit/rtc-node} for more information about the parameters.
92
+ */
93
+ async connect(e2ee, autoSubscribe = 0 /* SUBSCRIBE_ALL */, rtcConfig) {
94
+ const opts = {
95
+ e2ee,
96
+ autoSubscribe: autoSubscribe == 0 /* SUBSCRIBE_ALL */,
97
+ rtcConfig,
98
+ dynacast: false
99
+ };
100
+ await this.#room.connect(this.#info.url, this.#info.token, opts);
101
+ this.#onConnect();
102
+ this.#room.remoteParticipants.forEach(this.onParticipantConnected);
103
+ if ([3 /* AUDIO_ONLY */, 2 /* VIDEO_ONLY */].includes(autoSubscribe)) {
104
+ this.#room.remoteParticipants.forEach((p) => {
105
+ p.trackPublications.forEach((pub) => {
106
+ if (autoSubscribe === 3 /* AUDIO_ONLY */ && pub.kind === TrackKind.KIND_AUDIO || autoSubscribe === 2 /* VIDEO_ONLY */ && pub.kind === TrackKind.KIND_VIDEO) {
107
+ pub.setSubscribed(true);
108
+ }
85
109
  });
86
- }
87
- /**
88
- * Connects the agent to the room.
89
- *
90
- * @remarks
91
- * It is recommended to run this command as early in the function as possible, as executing it
92
- * later may cause noticeable delay between user and agent joins.
93
- *
94
- * @see {@link https://github.com/livekit/node-sdks/tree/main/packages/livekit-rtc#readme |
95
- * @livekit/rtc-node} for more information about the parameters.
96
- */
97
- async connect(e2ee, autoSubscribe = AutoSubscribe.SUBSCRIBE_ALL, rtcConfig) {
98
- const opts = {
99
- e2ee,
100
- autoSubscribe: autoSubscribe == AutoSubscribe.SUBSCRIBE_ALL,
101
- rtcConfig,
102
- dynacast: false,
103
- };
104
- await this.#room.connect(this.#info.url, this.#info.token, opts);
105
- this.#onConnect();
106
- this.#room.remoteParticipants.forEach(this.onParticipantConnected);
107
- if ([AutoSubscribe.AUDIO_ONLY, AutoSubscribe.VIDEO_ONLY].includes(autoSubscribe)) {
108
- this.#room.remoteParticipants.forEach((p) => {
109
- p.trackPublications.forEach((pub) => {
110
- if ((autoSubscribe === AutoSubscribe.AUDIO_ONLY && pub.kind === TrackKind.KIND_AUDIO) ||
111
- (autoSubscribe === AutoSubscribe.VIDEO_ONLY && pub.kind === TrackKind.KIND_VIDEO)) {
112
- pub.setSubscribed(true);
113
- }
114
- });
115
- });
116
- }
117
- }
118
- /**
119
- * Gracefully shuts down the job, and runs all shutdown promises.
120
- *
121
- * @param reason - Optional reason for shutdown
122
- */
123
- shutdown(reason = '') {
124
- this.#onShutdown(reason);
125
- }
126
- /** @internal */
127
- onParticipantConnected(p) {
128
- for (const callback of this.#participantEntrypoints) {
129
- if (this.#participantTasks[p.identity]?.callback == callback) {
130
- this.#logger.warn('a participant has joined before a prior prticipant task matching the same identity has finished:', p.identity);
131
- }
132
- const result = callback(this, p);
133
- result.finally(() => delete this.#participantTasks[p.identity]);
134
- this.#participantTasks[p.identity] = { callback, result };
135
- }
136
- }
137
- /**
138
- * Adds a promise to be awaited whenever a new participant joins the room.
139
- *
140
- * @throws {@link FunctionExistsError} if an entrypoint already exists
141
- */
142
- addParticipantEntrypoint(callback) {
143
- if (this.#participantEntrypoints.includes(callback)) {
144
- throw new FunctionExistsError('entrypoints cannot be added more than once');
145
- }
146
- this.#participantEntrypoints.push(callback);
147
- }
110
+ });
111
+ }
112
+ }
113
+ /**
114
+ * Gracefully shuts down the job, and runs all shutdown promises.
115
+ *
116
+ * @param reason - Optional reason for shutdown
117
+ */
118
+ shutdown(reason = "") {
119
+ this.#onShutdown(reason);
120
+ }
121
+ /** @internal */
122
+ onParticipantConnected(p) {
123
+ var _a;
124
+ for (const callback of this.#participantEntrypoints) {
125
+ if (((_a = this.#participantTasks[p.identity]) == null ? void 0 : _a.callback) == callback) {
126
+ this.#logger.warn(
127
+ "a participant has joined before a prior prticipant task matching the same identity has finished:",
128
+ p.identity
129
+ );
130
+ }
131
+ const result = callback(this, p);
132
+ result.finally(() => delete this.#participantTasks[p.identity]);
133
+ this.#participantTasks[p.identity] = { callback, result };
134
+ }
135
+ }
136
+ /**
137
+ * Adds a promise to be awaited whenever a new participant joins the room.
138
+ *
139
+ * @throws {@link FunctionExistsError} if an entrypoint already exists
140
+ */
141
+ addParticipantEntrypoint(callback) {
142
+ if (this.#participantEntrypoints.includes(callback)) {
143
+ throw new FunctionExistsError("entrypoints cannot be added more than once");
144
+ }
145
+ this.#participantEntrypoints.push(callback);
146
+ }
148
147
  }
149
- export class JobProcess {
150
- #pid = process.pid;
151
- userData = {};
152
- get pid() {
153
- return this.#pid;
154
- }
148
+ class JobProcess {
149
+ #pid = process.pid;
150
+ userData = {};
151
+ get pid() {
152
+ return this.#pid;
153
+ }
155
154
  }
156
- /**
157
- * A request sent by the server to spawn a new agent job.
158
- *
159
- * @remarks
160
- * For most applications, this is best left to the default, which simply accepts the job and
161
- * handles the logic inside the entrypoint function. This class is useful for vetting which
162
- * requests should fill idle processes and which should be outright rejected.
163
- */
164
- export class JobRequest {
165
- #job;
166
- #onReject;
167
- #onAccept;
168
- /** @internal */
169
- constructor(job, onReject, onAccept) {
170
- this.#job = job;
171
- this.#onReject = onReject;
172
- this.#onAccept = onAccept;
173
- }
174
- /** @returns The ID of the job, set by the LiveKit server */
175
- get id() {
176
- return this.#job.id;
177
- }
178
- /** @see {@link https://www.npmjs.com/package/@livekit/protocol | @livekit/protocol} */
179
- get job() {
180
- return this.#job;
181
- }
182
- /** @see {@link https://www.npmjs.com/package/@livekit/protocol | @livekit/protocol} */
183
- get room() {
184
- return this.#job.room;
185
- }
186
- /** @see {@link https://www.npmjs.com/package/@livekit/protocol | @livekit/protocol} */
187
- get publisher() {
188
- return this.#job.participant;
189
- }
190
- /** @returns The agent's name, as set in {@link WorkerOptions} */
191
- get agentName() {
192
- return this.#job.agentName;
193
- }
194
- /** Rejects the job. */
195
- async reject() {
196
- await this.#onReject();
197
- }
198
- /** Accepts the job, launching it on an idle child process. */
199
- async accept(name = '', identity = '', metadata = '') {
200
- if (identity === '')
201
- identity = 'agent-' + this.id;
202
- this.#onAccept({ name, identity, metadata });
203
- }
155
+ class JobRequest {
156
+ #job;
157
+ #onReject;
158
+ #onAccept;
159
+ /** @internal */
160
+ constructor(job, onReject, onAccept) {
161
+ this.#job = job;
162
+ this.#onReject = onReject;
163
+ this.#onAccept = onAccept;
164
+ }
165
+ /** @returns The ID of the job, set by the LiveKit server */
166
+ get id() {
167
+ return this.#job.id;
168
+ }
169
+ /** @see {@link https://www.npmjs.com/package/@livekit/protocol | @livekit/protocol} */
170
+ get job() {
171
+ return this.#job;
172
+ }
173
+ /** @see {@link https://www.npmjs.com/package/@livekit/protocol | @livekit/protocol} */
174
+ get room() {
175
+ return this.#job.room;
176
+ }
177
+ /** @see {@link https://www.npmjs.com/package/@livekit/protocol | @livekit/protocol} */
178
+ get publisher() {
179
+ return this.#job.participant;
180
+ }
181
+ /** @returns The agent's name, as set in {@link WorkerOptions} */
182
+ get agentName() {
183
+ return this.#job.agentName;
184
+ }
185
+ /** Rejects the job. */
186
+ async reject() {
187
+ await this.#onReject();
188
+ }
189
+ /** Accepts the job, launching it on an idle child process. */
190
+ async accept(name = "", identity = "", metadata = "") {
191
+ if (identity === "") identity = "agent-" + this.id;
192
+ this.#onAccept({ name, identity, metadata });
193
+ }
204
194
  }
195
+ export {
196
+ AutoSubscribe,
197
+ FunctionExistsError,
198
+ JobContext,
199
+ JobProcess,
200
+ JobRequest
201
+ };
205
202
  //# sourceMappingURL=job.js.map
package/dist/job.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"job.js","sourceRoot":"","sources":["../src/job.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE1E,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,yEAAyE;AACzE,MAAM,CAAN,IAAY,aAKX;AALD,WAAY,aAAa;IACvB,mEAAa,CAAA;IACb,qEAAc,CAAA;IACd,6DAAU,CAAA;IACV,6DAAU,CAAA;AACZ,CAAC,EALW,aAAa,KAAb,aAAa,QAKxB;AAeD,6EAA6E;AAC7E,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC5C,YAAY,GAAY;QACtB,KAAK,CAAC,GAAG,CAAC,CAAC;QACX,MAAM,CAAC,cAAc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C;CACF"}
1
+ {"version":3,"sources":["../src/job.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type * as proto from '@livekit/protocol';\nimport type {\n E2EEOptions,\n LocalParticipant,\n RemoteParticipant,\n Room,\n RtcConfiguration,\n} from '@livekit/rtc-node';\nimport { ParticipantKind, RoomEvent, TrackKind } from '@livekit/rtc-node';\nimport type { Logger } from 'pino';\nimport { log } from './log.js';\n\n/** Which tracks, if any, should the agent automatically subscribe to? */\nexport enum AutoSubscribe {\n SUBSCRIBE_ALL,\n SUBSCRIBE_NONE,\n VIDEO_ONLY,\n AUDIO_ONLY,\n}\n\nexport type JobAcceptArguments = {\n name: string;\n identity: string;\n metadata: string;\n};\n\nexport type RunningJobInfo = {\n acceptArguments: JobAcceptArguments;\n job: proto.Job;\n url: string;\n token: string;\n};\n\n/** Attempted to add a function callback, but the function already exists. */\nexport class FunctionExistsError extends Error {\n constructor(msg?: string) {\n super(msg);\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** The job and environment context as seen by the agent, accessible by the entrypoint function. */\nexport class JobContext {\n #proc: JobProcess;\n #info: RunningJobInfo;\n #room: Room;\n #onConnect: () => void;\n #onShutdown: (s: string) => void;\n /** @internal */\n shutdownCallbacks: (() => Promise<void>)[] = [];\n #participantEntrypoints: ((job: JobContext, p: RemoteParticipant) => Promise<void>)[] = [];\n #participantTasks: {\n [id: string]: {\n callback: (job: JobContext, p: RemoteParticipant) => Promise<void>;\n result: Promise<void>;\n };\n } = {};\n #logger: Logger;\n\n constructor(\n proc: JobProcess,\n info: RunningJobInfo,\n room: Room,\n onConnect: () => void,\n onShutdown: (s: string) => void,\n ) {\n this.#proc = proc;\n this.#info = info;\n this.#room = room;\n this.#onConnect = onConnect;\n this.#onShutdown = onShutdown;\n this.onParticipantConnected = this.onParticipantConnected.bind(this);\n this.#room.on(RoomEvent.ParticipantConnected, this.onParticipantConnected);\n this.#logger = log().child({ info: this.#info });\n }\n\n get proc(): JobProcess {\n return this.#proc;\n }\n\n get job(): proto.Job {\n return this.#info.job;\n }\n\n /** @returns The room the agent was called into */\n get room(): Room {\n return this.#room;\n }\n\n /** @returns The agent's participant if connected to the room, otherwise `undefined` */\n get agent(): LocalParticipant | undefined {\n return this.#room.localParticipant;\n }\n\n /** Adds a promise to be awaited when {@link JobContext.shutdown | shutdown} is called. */\n addShutdownCallback(callback: () => Promise<void>) {\n this.shutdownCallbacks.push(callback);\n }\n\n async waitForParticipant(identity?: string): Promise<RemoteParticipant> {\n if (!this.#room.isConnected) {\n throw new Error('room is not connected');\n }\n\n for (const p of this.#room.remoteParticipants.values()) {\n if ((!identity || p.identity === identity) && p.info.kind != ParticipantKind.AGENT) {\n return p;\n }\n }\n\n return new Promise((resolve, reject) => {\n const onParticipantConnected = (participant: RemoteParticipant) => {\n if (\n (!identity || participant.identity === identity) &&\n participant.info.kind != ParticipantKind.AGENT\n ) {\n clearHandlers();\n resolve(participant);\n }\n };\n const onDisconnected = () => {\n clearHandlers();\n reject(new Error('Room disconnected while waiting for participant'));\n };\n\n const clearHandlers = () => {\n this.#room.off(RoomEvent.ParticipantConnected, onParticipantConnected);\n this.#room.off(RoomEvent.Disconnected, onDisconnected);\n };\n\n this.#room.on(RoomEvent.ParticipantConnected, onParticipantConnected);\n this.#room.on(RoomEvent.Disconnected, onDisconnected);\n });\n }\n\n /**\n * Connects the agent to the room.\n *\n * @remarks\n * It is recommended to run this command as early in the function as possible, as executing it\n * later may cause noticeable delay between user and agent joins.\n *\n * @see {@link https://github.com/livekit/node-sdks/tree/main/packages/livekit-rtc#readme |\n * @livekit/rtc-node} for more information about the parameters.\n */\n async connect(\n e2ee?: E2EEOptions,\n autoSubscribe: AutoSubscribe = AutoSubscribe.SUBSCRIBE_ALL,\n rtcConfig?: RtcConfiguration,\n ) {\n const opts = {\n e2ee,\n autoSubscribe: autoSubscribe == AutoSubscribe.SUBSCRIBE_ALL,\n rtcConfig,\n dynacast: false,\n };\n\n await this.#room.connect(this.#info.url, this.#info.token, opts);\n this.#onConnect();\n\n this.#room.remoteParticipants.forEach(this.onParticipantConnected);\n\n if ([AutoSubscribe.AUDIO_ONLY, AutoSubscribe.VIDEO_ONLY].includes(autoSubscribe)) {\n this.#room.remoteParticipants.forEach((p) => {\n p.trackPublications.forEach((pub) => {\n if (\n (autoSubscribe === AutoSubscribe.AUDIO_ONLY && pub.kind === TrackKind.KIND_AUDIO) ||\n (autoSubscribe === AutoSubscribe.VIDEO_ONLY && pub.kind === TrackKind.KIND_VIDEO)\n ) {\n pub.setSubscribed(true);\n }\n });\n });\n }\n }\n\n /**\n * Gracefully shuts down the job, and runs all shutdown promises.\n *\n * @param reason - Optional reason for shutdown\n */\n shutdown(reason = '') {\n this.#onShutdown(reason);\n }\n\n /** @internal */\n onParticipantConnected(p: RemoteParticipant) {\n for (const callback of this.#participantEntrypoints) {\n if (this.#participantTasks[p.identity]?.callback == callback) {\n this.#logger.warn(\n 'a participant has joined before a prior prticipant task matching the same identity has finished:',\n p.identity,\n );\n }\n const result = callback(this, p);\n result.finally(() => delete this.#participantTasks[p.identity]);\n this.#participantTasks[p.identity] = { callback, result };\n }\n }\n\n /**\n * Adds a promise to be awaited whenever a new participant joins the room.\n *\n * @throws {@link FunctionExistsError} if an entrypoint already exists\n */\n addParticipantEntrypoint(callback: (job: JobContext, p: RemoteParticipant) => Promise<void>) {\n if (this.#participantEntrypoints.includes(callback)) {\n throw new FunctionExistsError('entrypoints cannot be added more than once');\n }\n\n this.#participantEntrypoints.push(callback);\n }\n}\n\nexport class JobProcess {\n #pid = process.pid;\n userData: { [id: string]: unknown } = {};\n\n get pid(): number {\n return this.#pid;\n }\n}\n\n/**\n * A request sent by the server to spawn a new agent job.\n *\n * @remarks\n * For most applications, this is best left to the default, which simply accepts the job and\n * handles the logic inside the entrypoint function. This class is useful for vetting which\n * requests should fill idle processes and which should be outright rejected.\n */\nexport class JobRequest {\n #job: proto.Job;\n #onReject: () => Promise<void>;\n #onAccept: (args: JobAcceptArguments) => Promise<void>;\n\n /** @internal */\n constructor(\n job: proto.Job,\n onReject: () => Promise<void>,\n onAccept: (args: JobAcceptArguments) => Promise<void>,\n ) {\n this.#job = job;\n this.#onReject = onReject;\n this.#onAccept = onAccept;\n }\n\n /** @returns The ID of the job, set by the LiveKit server */\n get id(): string {\n return this.#job.id;\n }\n\n /** @see {@link https://www.npmjs.com/package/@livekit/protocol | @livekit/protocol} */\n get job(): proto.Job {\n return this.#job;\n }\n\n /** @see {@link https://www.npmjs.com/package/@livekit/protocol | @livekit/protocol} */\n get room(): proto.Room | undefined {\n return this.#job.room;\n }\n\n /** @see {@link https://www.npmjs.com/package/@livekit/protocol | @livekit/protocol} */\n get publisher(): proto.ParticipantInfo | undefined {\n return this.#job.participant;\n }\n\n /** @returns The agent's name, as set in {@link WorkerOptions} */\n get agentName(): string {\n return this.#job.agentName;\n }\n\n /** Rejects the job. */\n async reject() {\n await this.#onReject();\n }\n\n /** Accepts the job, launching it on an idle child process. */\n async accept(name = '', identity = '', metadata = '') {\n if (identity === '') identity = 'agent-' + this.id;\n\n this.#onAccept({ name, identity, metadata });\n }\n}\n"],"mappings":"AAWA,SAAS,iBAAiB,WAAW,iBAAiB;AAEtD,SAAS,WAAW;AAGb,IAAK,gBAAL,kBAAKA,mBAAL;AACL,EAAAA,8BAAA;AACA,EAAAA,8BAAA;AACA,EAAAA,8BAAA;AACA,EAAAA,8BAAA;AAJU,SAAAA;AAAA,GAAA;AAqBL,MAAM,4BAA4B,MAAM;AAAA,EAC7C,YAAY,KAAc;AACxB,UAAM,GAAG;AACT,WAAO,eAAe,MAAM,WAAW,SAAS;AAAA,EAClD;AACF;AAGO,MAAM,WAAW;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAEA,oBAA6C,CAAC;AAAA,EAC9C,0BAAwF,CAAC;AAAA,EACzF,oBAKI,CAAC;AAAA,EACL;AAAA,EAE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 @@
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 chat_context_exports = {};
20
+ __export(chat_context_exports, {
21
+ ChatContext: () => ChatContext,
22
+ ChatMessage: () => ChatMessage,
23
+ ChatRole: () => ChatRole
24
+ });
25
+ module.exports = __toCommonJS(chat_context_exports);
26
+ var ChatRole = /* @__PURE__ */ ((ChatRole2) => {
27
+ ChatRole2[ChatRole2["SYSTEM"] = 0] = "SYSTEM";
28
+ ChatRole2[ChatRole2["USER"] = 1] = "USER";
29
+ ChatRole2[ChatRole2["ASSISTANT"] = 2] = "ASSISTANT";
30
+ ChatRole2[ChatRole2["TOOL"] = 3] = "TOOL";
31
+ return ChatRole2;
32
+ })(ChatRole || {});
33
+ const defaultCreateChatMessage = {
34
+ text: "",
35
+ images: [],
36
+ role: 0 /* SYSTEM */
37
+ };
38
+ class ChatMessage {
39
+ role;
40
+ id;
41
+ name;
42
+ content;
43
+ toolCalls;
44
+ toolCallId;
45
+ toolException;
46
+ /** @internal */
47
+ constructor({
48
+ role,
49
+ id,
50
+ name,
51
+ content,
52
+ toolCalls,
53
+ toolCallId,
54
+ toolException
55
+ }) {
56
+ this.role = role;
57
+ this.id = id;
58
+ this.name = name;
59
+ this.content = content;
60
+ this.toolCalls = toolCalls;
61
+ this.toolCallId = toolCallId;
62
+ this.toolException = toolException;
63
+ }
64
+ static createToolFromFunctionResult(func) {
65
+ if (!func.result && !func.error) {
66
+ throw new TypeError("CallableFunctionResult must include result or error");
67
+ }
68
+ return new ChatMessage({
69
+ role: 3 /* TOOL */,
70
+ name: func.name,
71
+ content: func.result || `Error: ${func.error}`,
72
+ toolCallId: func.toolCallId,
73
+ toolException: func.error
74
+ });
75
+ }
76
+ static createToolCalls(toolCalls, text = "") {
77
+ return new ChatMessage({
78
+ role: 2 /* ASSISTANT */,
79
+ toolCalls,
80
+ content: text
81
+ });
82
+ }
83
+ static create(options) {
84
+ const { text, images, role } = { ...defaultCreateChatMessage, ...options };
85
+ if (!images.length) {
86
+ return new ChatMessage({
87
+ role,
88
+ content: text
89
+ });
90
+ } else {
91
+ return new ChatMessage({
92
+ role,
93
+ content: [...text ? [text] : [], ...images]
94
+ });
95
+ }
96
+ }
97
+ /** Returns a structured clone of this message. */
98
+ copy() {
99
+ return new ChatMessage({
100
+ role: this.role,
101
+ id: this.id,
102
+ name: this.name,
103
+ content: this.content,
104
+ toolCalls: this.toolCalls,
105
+ toolCallId: this.toolCallId,
106
+ toolException: this.toolException
107
+ });
108
+ }
109
+ }
110
+ class ChatContext {
111
+ messages = [];
112
+ metadata = {};
113
+ append(msg) {
114
+ this.messages.push(ChatMessage.create(msg));
115
+ return this;
116
+ }
117
+ /** Returns a structured clone of this context. */
118
+ copy() {
119
+ const ctx = new ChatContext();
120
+ ctx.messages.push(...this.messages.map((msg) => msg.copy()));
121
+ ctx.metadata = structuredClone(this.metadata);
122
+ return ctx;
123
+ }
124
+ }
125
+ // Annotate the CommonJS export names for ESM import in node:
126
+ 0 && (module.exports = {
127
+ ChatContext,
128
+ ChatMessage,
129
+ ChatRole
130
+ });
131
+ //# sourceMappingURL=chat_context.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/llm/chat_context.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { AudioFrame, VideoFrame } from '@livekit/rtc-node';\nimport type { CallableFunctionResult, FunctionCallInfo } from './function_context.js';\n\nexport enum ChatRole {\n SYSTEM,\n USER,\n ASSISTANT,\n TOOL,\n}\n\nexport interface ChatImage {\n image: string | VideoFrame;\n inferenceWidth?: number;\n inferenceHeight?: number;\n /**\n * @internal\n * Used by LLM implementations to store a processed version of the image for later use.\n */\n cache: { [id: string | number | symbol]: any };\n}\n\nexport interface ChatAudio {\n frame: AudioFrame | AudioFrame[];\n}\n\nexport type ChatContent = string | ChatImage | ChatAudio;\n\nconst defaultCreateChatMessage = {\n text: '',\n images: [],\n role: ChatRole.SYSTEM,\n};\n\nexport class ChatMessage {\n readonly role: ChatRole;\n readonly id?: string;\n readonly name?: string;\n readonly content?: ChatContent | ChatContent[];\n readonly toolCalls?: FunctionCallInfo[];\n readonly toolCallId?: string;\n readonly toolException?: Error;\n\n /** @internal */\n constructor({\n role,\n id,\n name,\n content,\n toolCalls,\n toolCallId,\n toolException,\n }: {\n role: ChatRole;\n id?: string;\n name?: string;\n content?: ChatContent | ChatContent[];\n toolCalls?: FunctionCallInfo[];\n toolCallId?: string;\n toolException?: Error;\n }) {\n this.role = role;\n this.id = id;\n this.name = name;\n this.content = content;\n this.toolCalls = toolCalls;\n this.toolCallId = toolCallId;\n this.toolException = toolException;\n }\n\n static createToolFromFunctionResult(func: CallableFunctionResult): ChatMessage {\n if (!func.result && !func.error) {\n throw new TypeError('CallableFunctionResult must include result or error');\n }\n\n return new ChatMessage({\n role: ChatRole.TOOL,\n name: func.name,\n content: func.result || `Error: ${func.error}`,\n toolCallId: func.toolCallId,\n toolException: func.error,\n });\n }\n\n static createToolCalls(toolCalls: FunctionCallInfo[], text = '') {\n return new ChatMessage({\n role: ChatRole.ASSISTANT,\n toolCalls,\n content: text,\n });\n }\n\n static create(\n options: Partial<{\n text?: string;\n images: ChatImage[];\n role: ChatRole;\n }>,\n ): ChatMessage {\n const { text, images, role } = { ...defaultCreateChatMessage, ...options };\n\n if (!images.length) {\n return new ChatMessage({\n role,\n content: text,\n });\n } else {\n return new ChatMessage({\n role,\n content: [...(text ? [text] : []), ...images],\n });\n }\n }\n\n /** Returns a structured clone of this message. */\n copy(): ChatMessage {\n return new ChatMessage({\n role: this.role,\n id: this.id,\n name: this.name,\n content: this.content,\n toolCalls: this.toolCalls,\n toolCallId: this.toolCallId,\n toolException: this.toolException,\n });\n }\n}\n\nexport class ChatContext {\n messages: ChatMessage[] = [];\n metadata: { [id: string]: any } = {};\n\n append(msg: { text?: string; images?: ChatImage[]; role: ChatRole }): ChatContext {\n this.messages.push(ChatMessage.create(msg));\n return this;\n }\n\n /** Returns a structured clone of this context. */\n copy(): ChatContext {\n const ctx = new ChatContext();\n ctx.messages.push(...this.messages.map((msg) => msg.copy()));\n ctx.metadata = structuredClone(this.metadata);\n return ctx;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAMO,IAAK,WAAL,kBAAKA,cAAL;AACL,EAAAA,oBAAA;AACA,EAAAA,oBAAA;AACA,EAAAA,oBAAA;AACA,EAAAA,oBAAA;AAJU,SAAAA;AAAA,GAAA;AAwBZ,MAAM,2BAA2B;AAAA,EAC/B,MAAM;AAAA,EACN,QAAQ,CAAC;AAAA,EACT,MAAM;AACR;AAEO,MAAM,YAAY;AAAA,EACd;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGT,YAAY;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAQG;AACD,SAAK,OAAO;AACZ,SAAK,KAAK;AACV,SAAK,OAAO;AACZ,SAAK,UAAU;AACf,SAAK,YAAY;AACjB,SAAK,aAAa;AAClB,SAAK,gBAAgB;AAAA,EACvB;A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