@livekit/agents 0.6.4 → 0.7.1

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 (184) hide show
  1. package/dist/cli.cjs +8 -0
  2. package/dist/cli.cjs.map +1 -1
  3. package/dist/cli.d.ts.map +1 -1
  4. package/dist/cli.js +8 -0
  5. package/dist/cli.js.map +1 -1
  6. package/dist/index.cjs +6 -1
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +3 -1
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +3 -0
  11. package/dist/index.js.map +1 -1
  12. package/dist/inference_runner.cjs +38 -0
  13. package/dist/inference_runner.cjs.map +1 -0
  14. package/dist/inference_runner.d.ts +11 -0
  15. package/dist/inference_runner.d.ts.map +1 -0
  16. package/dist/inference_runner.js +14 -0
  17. package/dist/inference_runner.js.map +1 -0
  18. package/dist/ipc/index.cjs +23 -0
  19. package/dist/ipc/index.cjs.map +1 -0
  20. package/dist/ipc/index.d.ts +2 -0
  21. package/dist/ipc/index.d.ts.map +1 -0
  22. package/dist/ipc/index.js +2 -0
  23. package/dist/ipc/index.js.map +1 -0
  24. package/dist/ipc/inference_executor.cjs +17 -0
  25. package/dist/ipc/inference_executor.cjs.map +1 -0
  26. package/dist/ipc/inference_executor.d.ts +4 -0
  27. package/dist/ipc/inference_executor.d.ts.map +1 -0
  28. package/dist/ipc/inference_executor.js +1 -0
  29. package/dist/ipc/inference_executor.js.map +1 -0
  30. package/dist/ipc/inference_proc_executor.cjs +97 -0
  31. package/dist/ipc/inference_proc_executor.cjs.map +1 -0
  32. package/dist/ipc/inference_proc_executor.d.ts +23 -0
  33. package/dist/ipc/inference_proc_executor.d.ts.map +1 -0
  34. package/dist/ipc/inference_proc_executor.js +72 -0
  35. package/dist/ipc/inference_proc_executor.js.map +1 -0
  36. package/dist/ipc/inference_proc_lazy_main.cjs +92 -0
  37. package/dist/ipc/inference_proc_lazy_main.cjs.map +1 -0
  38. package/dist/ipc/inference_proc_lazy_main.d.ts +2 -0
  39. package/dist/ipc/inference_proc_lazy_main.d.ts.map +1 -0
  40. package/dist/ipc/inference_proc_lazy_main.js +69 -0
  41. package/dist/ipc/inference_proc_lazy_main.js.map +1 -0
  42. package/dist/ipc/job_executor.cjs +8 -7
  43. package/dist/ipc/job_executor.cjs.map +1 -1
  44. package/dist/ipc/job_executor.d.ts +14 -15
  45. package/dist/ipc/job_executor.d.ts.map +1 -1
  46. package/dist/ipc/job_executor.js +7 -6
  47. package/dist/ipc/job_executor.js.map +1 -1
  48. package/dist/ipc/job_proc_executor.cjs +108 -0
  49. package/dist/ipc/job_proc_executor.cjs.map +1 -0
  50. package/dist/ipc/job_proc_executor.d.ts +19 -0
  51. package/dist/ipc/job_proc_executor.d.ts.map +1 -0
  52. package/dist/ipc/job_proc_executor.js +83 -0
  53. package/dist/ipc/job_proc_executor.js.map +1 -0
  54. package/dist/ipc/{job_main.cjs → job_proc_lazy_main.cjs} +46 -36
  55. package/dist/ipc/job_proc_lazy_main.cjs.map +1 -0
  56. package/dist/ipc/job_proc_lazy_main.d.ts +2 -0
  57. package/dist/ipc/job_proc_lazy_main.d.ts.map +1 -0
  58. package/dist/ipc/{job_main.js → job_proc_lazy_main.js} +46 -11
  59. package/dist/ipc/job_proc_lazy_main.js.map +1 -0
  60. package/dist/ipc/message.cjs.map +1 -1
  61. package/dist/ipc/message.d.ts +17 -0
  62. package/dist/ipc/message.d.ts.map +1 -1
  63. package/dist/ipc/proc_pool.cjs +30 -4
  64. package/dist/ipc/proc_pool.cjs.map +1 -1
  65. package/dist/ipc/proc_pool.d.ts +5 -1
  66. package/dist/ipc/proc_pool.d.ts.map +1 -1
  67. package/dist/ipc/proc_pool.js +30 -4
  68. package/dist/ipc/proc_pool.js.map +1 -1
  69. package/dist/ipc/{proc_job_executor.cjs → supervised_proc.cjs} +57 -45
  70. package/dist/ipc/supervised_proc.cjs.map +1 -0
  71. package/dist/ipc/supervised_proc.d.ts +30 -0
  72. package/dist/ipc/supervised_proc.d.ts.map +1 -0
  73. package/dist/ipc/{proc_job_executor.js → supervised_proc.js} +53 -31
  74. package/dist/ipc/supervised_proc.js.map +1 -0
  75. package/dist/job.cjs +18 -1
  76. package/dist/job.cjs.map +1 -1
  77. package/dist/job.d.ts +9 -1
  78. package/dist/job.d.ts.map +1 -1
  79. package/dist/job.js +17 -1
  80. package/dist/job.js.map +1 -1
  81. package/dist/multimodal/agent_playout.cjs +18 -16
  82. package/dist/multimodal/agent_playout.cjs.map +1 -1
  83. package/dist/multimodal/agent_playout.d.ts +4 -4
  84. package/dist/multimodal/agent_playout.d.ts.map +1 -1
  85. package/dist/multimodal/agent_playout.js +18 -16
  86. package/dist/multimodal/agent_playout.js.map +1 -1
  87. package/dist/multimodal/multimodal_agent.cjs +12 -8
  88. package/dist/multimodal/multimodal_agent.cjs.map +1 -1
  89. package/dist/multimodal/multimodal_agent.d.ts.map +1 -1
  90. package/dist/multimodal/multimodal_agent.js +13 -9
  91. package/dist/multimodal/multimodal_agent.js.map +1 -1
  92. package/dist/pipeline/agent_output.cjs +22 -4
  93. package/dist/pipeline/agent_output.cjs.map +1 -1
  94. package/dist/pipeline/agent_output.d.ts +4 -2
  95. package/dist/pipeline/agent_output.d.ts.map +1 -1
  96. package/dist/pipeline/agent_output.js +22 -4
  97. package/dist/pipeline/agent_output.js.map +1 -1
  98. package/dist/pipeline/agent_playout.cjs +9 -3
  99. package/dist/pipeline/agent_playout.cjs.map +1 -1
  100. package/dist/pipeline/agent_playout.d.ts +4 -2
  101. package/dist/pipeline/agent_playout.d.ts.map +1 -1
  102. package/dist/pipeline/agent_playout.js +9 -3
  103. package/dist/pipeline/agent_playout.js.map +1 -1
  104. package/dist/pipeline/human_input.cjs +6 -0
  105. package/dist/pipeline/human_input.cjs.map +1 -1
  106. package/dist/pipeline/human_input.d.ts +3 -1
  107. package/dist/pipeline/human_input.d.ts.map +1 -1
  108. package/dist/pipeline/human_input.js +6 -0
  109. package/dist/pipeline/human_input.js.map +1 -1
  110. package/dist/pipeline/pipeline_agent.cjs +79 -12
  111. package/dist/pipeline/pipeline_agent.cjs.map +1 -1
  112. package/dist/pipeline/pipeline_agent.d.ts +8 -0
  113. package/dist/pipeline/pipeline_agent.d.ts.map +1 -1
  114. package/dist/pipeline/pipeline_agent.js +79 -12
  115. package/dist/pipeline/pipeline_agent.js.map +1 -1
  116. package/dist/stt/stream_adapter.cjs +16 -4
  117. package/dist/stt/stream_adapter.cjs.map +1 -1
  118. package/dist/stt/stream_adapter.d.ts.map +1 -1
  119. package/dist/stt/stream_adapter.js +16 -4
  120. package/dist/stt/stream_adapter.js.map +1 -1
  121. package/dist/tokenize/basic/basic.cjs +2 -0
  122. package/dist/tokenize/basic/basic.cjs.map +1 -1
  123. package/dist/tokenize/basic/basic.d.ts +2 -0
  124. package/dist/tokenize/basic/basic.d.ts.map +1 -1
  125. package/dist/tokenize/basic/basic.js +1 -0
  126. package/dist/tokenize/basic/basic.js.map +1 -1
  127. package/dist/tokenize/basic/index.cjs +2 -0
  128. package/dist/tokenize/basic/index.cjs.map +1 -1
  129. package/dist/tokenize/basic/index.d.ts +1 -1
  130. package/dist/tokenize/basic/index.d.ts.map +1 -1
  131. package/dist/tokenize/basic/index.js +8 -1
  132. package/dist/tokenize/basic/index.js.map +1 -1
  133. package/dist/tokenize/token_stream.cjs +5 -3
  134. package/dist/tokenize/token_stream.cjs.map +1 -1
  135. package/dist/tokenize/token_stream.d.ts.map +1 -1
  136. package/dist/tokenize/token_stream.js +5 -3
  137. package/dist/tokenize/token_stream.js.map +1 -1
  138. package/dist/transcription.cjs +203 -86
  139. package/dist/transcription.cjs.map +1 -1
  140. package/dist/transcription.d.ts +24 -17
  141. package/dist/transcription.d.ts.map +1 -1
  142. package/dist/transcription.js +201 -85
  143. package/dist/transcription.js.map +1 -1
  144. package/dist/worker.cjs +42 -9
  145. package/dist/worker.cjs.map +1 -1
  146. package/dist/worker.d.ts +5 -1
  147. package/dist/worker.d.ts.map +1 -1
  148. package/dist/worker.js +42 -9
  149. package/dist/worker.js.map +1 -1
  150. package/package.json +3 -3
  151. package/src/cli.ts +9 -0
  152. package/src/index.ts +3 -1
  153. package/src/inference_runner.ts +19 -0
  154. package/src/ipc/index.ts +5 -0
  155. package/src/ipc/inference_executor.ts +7 -0
  156. package/src/ipc/inference_proc_executor.ts +93 -0
  157. package/src/ipc/inference_proc_lazy_main.ts +90 -0
  158. package/src/ipc/job_executor.ts +15 -17
  159. package/src/ipc/job_proc_executor.ts +112 -0
  160. package/src/ipc/{job_main.ts → job_proc_lazy_main.ts} +52 -14
  161. package/src/ipc/message.ts +14 -1
  162. package/src/ipc/proc_pool.ts +33 -3
  163. package/src/ipc/{proc_job_executor.ts → supervised_proc.ts} +77 -29
  164. package/src/job.ts +21 -0
  165. package/src/multimodal/agent_playout.ts +19 -18
  166. package/src/multimodal/multimodal_agent.ts +13 -9
  167. package/src/pipeline/agent_output.ts +36 -5
  168. package/src/pipeline/agent_playout.ts +10 -1
  169. package/src/pipeline/human_input.ts +8 -0
  170. package/src/pipeline/pipeline_agent.ts +96 -11
  171. package/src/stt/stream_adapter.ts +17 -5
  172. package/src/tokenize/basic/basic.ts +2 -0
  173. package/src/tokenize/basic/index.ts +7 -1
  174. package/src/tokenize/token_stream.ts +6 -3
  175. package/src/transcription.ts +270 -96
  176. package/src/worker.ts +42 -5
  177. package/dist/ipc/job_main.cjs.map +0 -1
  178. package/dist/ipc/job_main.d.ts +0 -8
  179. package/dist/ipc/job_main.d.ts.map +0 -1
  180. package/dist/ipc/job_main.js.map +0 -1
  181. package/dist/ipc/proc_job_executor.cjs.map +0 -1
  182. package/dist/ipc/proc_job_executor.d.ts +0 -15
  183. package/dist/ipc/proc_job_executor.d.ts.map +0 -1
  184. package/dist/ipc/proc_job_executor.js.map +0 -1
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,41 +15,36 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
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
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var proc_job_executor_exports = {};
30
- __export(proc_job_executor_exports, {
31
- ProcJobExecutor: () => ProcJobExecutor
19
+ var supervised_proc_exports = {};
20
+ __export(supervised_proc_exports, {
21
+ SupervisedProc: () => SupervisedProc
32
22
  });
33
- module.exports = __toCommonJS(proc_job_executor_exports);
23
+ module.exports = __toCommonJS(supervised_proc_exports);
34
24
  var import_node_events = require("node:events");
35
25
  var import_log = require("../log.cjs");
36
26
  var import_utils = require("../utils.cjs");
37
- var import_job_executor = require("./job_executor.cjs");
38
- class ProcJobExecutor extends import_job_executor.JobExecutor {
27
+ class SupervisedProc {
39
28
  #opts;
40
29
  #started = false;
41
30
  #closing = false;
42
31
  #runningJob = void 0;
43
- #proc;
32
+ proc;
44
33
  #pingInterval;
34
+ #memoryWatch;
45
35
  #pongTimeout;
46
- #init = new import_utils.Future();
36
+ init = new import_utils.Future();
47
37
  #join = new import_utils.Future();
48
38
  #logger = (0, import_log.log)().child({ runningJob: this.#runningJob });
49
- constructor(agent, initializeTimeout, closeTimeout) {
50
- super();
39
+ constructor(initializeTimeout, closeTimeout, memoryWarnMB, memoryLimitMB, pingInterval, pingTimeout, highPingThreshold) {
51
40
  this.#opts = {
52
- agent,
53
41
  initializeTimeout,
54
- closeTimeout
42
+ closeTimeout,
43
+ memoryWarnMB,
44
+ memoryLimitMB,
45
+ pingInterval,
46
+ pingTimeout,
47
+ highPingThreshold
55
48
  };
56
49
  }
57
50
  get started() {
@@ -66,32 +59,41 @@ class ProcJobExecutor extends import_job_executor.JobExecutor {
66
59
  } else if (this.#closing) {
67
60
  throw new Error("runner is closed");
68
61
  }
69
- this.#proc = await import("./job_main.cjs").then(
70
- (m) => m.runProcess({
71
- agentFile: this.#opts.agent
72
- })
73
- );
62
+ this.proc = this.createProcess();
74
63
  this.#started = true;
75
64
  this.run();
76
65
  }
77
66
  async run() {
78
- await this.#init.await;
67
+ await this.init.await;
79
68
  this.#pingInterval = setInterval(() => {
80
- this.#proc.send({ case: "pingRequest", value: { timestamp: Date.now() } });
81
- }, this.PING_INTERVAL);
69
+ this.proc.send({ case: "pingRequest", value: { timestamp: Date.now() } });
70
+ }, this.#opts.pingInterval);
82
71
  this.#pongTimeout = setTimeout(() => {
83
72
  this.#logger.warn("job is unresponsive");
84
73
  clearTimeout(this.#pongTimeout);
85
74
  clearInterval(this.#pingInterval);
86
- this.#proc.kill();
75
+ this.proc.kill();
87
76
  this.#join.resolve();
88
- }, this.PING_TIMEOUT);
77
+ }, this.#opts.pingTimeout);
78
+ this.#memoryWatch = setInterval(() => {
79
+ const memoryMB = process.memoryUsage().heapUsed / (1024 * 1024);
80
+ if (this.#opts.memoryLimitMB > 0 && memoryMB > this.#opts.memoryLimitMB) {
81
+ this.#logger.child({ memoryUsageMB: memoryMB, memoryLimitMB: this.#opts.memoryLimitMB }).error("process exceeded memory limit, killing process");
82
+ this.close();
83
+ } else if (this.#opts.memoryWarnMB > 0 && memoryMB > this.#opts.memoryWarnMB) {
84
+ this.#logger.child({
85
+ memoryUsageMB: memoryMB,
86
+ memoryWarnMB: this.#opts.memoryWarnMB,
87
+ memoryLimitMB: this.#opts.memoryLimitMB
88
+ }).error("process memory usage is high");
89
+ }
90
+ });
89
91
  const listener = (msg) => {
90
92
  var _a;
91
93
  switch (msg.case) {
92
94
  case "pongResponse": {
93
95
  const delay = Date.now() - msg.value.timestamp;
94
- if (delay > this.HIGH_PING_THRESHOLD) {
96
+ if (delay > this.#opts.highPingThreshold) {
95
97
  this.#logger.child({ delay }).warn("job executor is unresponsive");
96
98
  }
97
99
  (_a = this.#pongTimeout) == null ? void 0 : _a.refresh();
@@ -103,20 +105,22 @@ class ProcJobExecutor extends import_job_executor.JobExecutor {
103
105
  }
104
106
  case "done": {
105
107
  this.#closing = true;
106
- this.#proc.off("message", listener);
108
+ this.proc.off("message", listener);
107
109
  this.#join.resolve();
108
110
  break;
109
111
  }
110
112
  }
111
113
  };
112
- this.#proc.on("message", listener);
113
- this.#proc.on("error", (err) => {
114
+ this.proc.on("message", listener);
115
+ this.proc.on("error", (err) => {
114
116
  if (this.#closing) return;
115
117
  this.#logger.child({ err }).warn("job process exited unexpectedly; this likely means the error above caused a crash");
116
118
  clearTimeout(this.#pongTimeout);
117
119
  clearInterval(this.#pingInterval);
120
+ clearInterval(this.#memoryWatch);
118
121
  this.#join.resolve();
119
122
  });
123
+ this.mainTask(this.proc);
120
124
  await this.#join.await;
121
125
  }
122
126
  async join() {
@@ -128,17 +132,25 @@ class ProcJobExecutor extends import_job_executor.JobExecutor {
128
132
  async initialize() {
129
133
  const timer = setTimeout(() => {
130
134
  const err = new Error("runner initialization timed out");
131
- this.#init.reject(err);
135
+ this.init.reject(err);
132
136
  throw err;
133
137
  }, this.#opts.initializeTimeout);
134
- this.#proc.send({ case: "initializeRequest", value: { loggerOptions: import_log.loggerOptions } });
135
- await (0, import_node_events.once)(this.#proc, "message").then(([msg]) => {
138
+ this.proc.send({
139
+ case: "initializeRequest",
140
+ value: {
141
+ loggerOptions: import_log.loggerOptions,
142
+ pingInterval: this.#opts.pingInterval,
143
+ pingTimeout: this.#opts.pingTimeout,
144
+ highPingThreshold: this.#opts.highPingThreshold
145
+ }
146
+ });
147
+ await (0, import_node_events.once)(this.proc, "message").then(([msg]) => {
136
148
  clearTimeout(timer);
137
149
  if (msg.case !== "initializeResponse") {
138
150
  throw new Error("first message must be InitializeResponse");
139
151
  }
140
152
  });
141
- this.#init.resolve();
153
+ this.init.resolve();
142
154
  }
143
155
  async close() {
144
156
  if (!this.#started) {
@@ -146,10 +158,10 @@ class ProcJobExecutor extends import_job_executor.JobExecutor {
146
158
  }
147
159
  this.#closing = true;
148
160
  if (!this.#runningJob) {
149
- this.#proc.kill();
161
+ this.proc.kill();
150
162
  this.#join.resolve();
151
163
  }
152
- this.#proc.send({ case: "shutdownRequest" });
164
+ this.proc.send({ case: "shutdownRequest" });
153
165
  const timer = setTimeout(() => {
154
166
  this.#logger.error("job shutdown is taking too much time");
155
167
  }, this.#opts.closeTimeout);
@@ -164,11 +176,11 @@ class ProcJobExecutor extends import_job_executor.JobExecutor {
164
176
  throw new Error("executor already has a running job");
165
177
  }
166
178
  this.#runningJob = info;
167
- this.#proc.send({ case: "startJobRequest", value: { runningJob: info } });
179
+ this.proc.send({ case: "startJobRequest", value: { runningJob: info } });
168
180
  }
169
181
  }
170
182
  // Annotate the CommonJS export names for ESM import in node:
171
183
  0 && (module.exports = {
172
- ProcJobExecutor
184
+ SupervisedProc
173
185
  });
174
- //# sourceMappingURL=proc_job_executor.cjs.map
186
+ //# sourceMappingURL=supervised_proc.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/ipc/supervised_proc.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChildProcess } from 'node:child_process';\nimport { once } from 'node:events';\nimport type { RunningJobInfo } from '../job.js';\nimport { log, loggerOptions } from '../log.js';\nimport { Future } from '../utils.js';\nimport type { IPCMessage } from './message.js';\n\nexport interface ProcOpts {\n initializeTimeout: number;\n closeTimeout: number;\n memoryWarnMB: number;\n memoryLimitMB: number;\n pingInterval: number;\n pingTimeout: number;\n highPingThreshold: number;\n}\n\nexport abstract class SupervisedProc {\n #opts: ProcOpts;\n #started = false;\n #closing = false;\n #runningJob?: RunningJobInfo = undefined;\n proc?: ChildProcess;\n #pingInterval?: ReturnType<typeof setInterval>;\n #memoryWatch?: ReturnType<typeof setInterval>;\n #pongTimeout?: ReturnType<typeof setTimeout>;\n protected init = new Future();\n #join = new Future();\n #logger = log().child({ runningJob: this.#runningJob });\n\n constructor(\n initializeTimeout: number,\n closeTimeout: number,\n memoryWarnMB: number,\n memoryLimitMB: number,\n pingInterval: number,\n pingTimeout: number,\n highPingThreshold: number,\n ) {\n this.#opts = {\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n };\n }\n\n abstract createProcess(): ChildProcess;\n abstract mainTask(child: ChildProcess): Promise<void>;\n\n get started(): boolean {\n return this.#started;\n }\n\n get runningJob(): RunningJobInfo | undefined {\n return this.#runningJob;\n }\n\n async start() {\n if (this.#started) {\n throw new Error('runner already started');\n } else if (this.#closing) {\n throw new Error('runner is closed');\n }\n\n this.proc = this.createProcess();\n\n this.#started = true;\n this.run();\n }\n\n async run() {\n await this.init.await;\n\n this.#pingInterval = setInterval(() => {\n this.proc!.send({ case: 'pingRequest', value: { timestamp: Date.now() } });\n }, this.#opts.pingInterval);\n\n this.#pongTimeout = setTimeout(() => {\n this.#logger.warn('job is unresponsive');\n clearTimeout(this.#pongTimeout);\n clearInterval(this.#pingInterval);\n this.proc!.kill();\n this.#join.resolve();\n }, this.#opts.pingTimeout);\n\n this.#memoryWatch = setInterval(() => {\n const memoryMB = process.memoryUsage().heapUsed / (1024 * 1024);\n if (this.#opts.memoryLimitMB > 0 && memoryMB > this.#opts.memoryLimitMB) {\n this.#logger\n .child({ memoryUsageMB: memoryMB, memoryLimitMB: this.#opts.memoryLimitMB })\n .error('process exceeded memory limit, killing process');\n this.close();\n } else if (this.#opts.memoryWarnMB > 0 && memoryMB > this.#opts.memoryWarnMB) {\n this.#logger\n .child({\n memoryUsageMB: memoryMB,\n memoryWarnMB: this.#opts.memoryWarnMB,\n memoryLimitMB: this.#opts.memoryLimitMB,\n })\n .error('process memory usage is high');\n }\n });\n\n const listener = (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pongResponse': {\n const delay = Date.now() - msg.value.timestamp;\n if (delay > this.#opts.highPingThreshold) {\n this.#logger.child({ delay }).warn('job executor is unresponsive');\n }\n this.#pongTimeout?.refresh();\n break;\n }\n case 'exiting': {\n this.#logger.child({ reason: msg.value.reason }).debug('job exiting');\n break;\n }\n case 'done': {\n this.#closing = true;\n this.proc!.off('message', listener);\n this.#join.resolve();\n break;\n }\n }\n };\n this.proc!.on('message', listener);\n this.proc!.on('error', (err) => {\n if (this.#closing) return;\n this.#logger\n .child({ err })\n .warn('job process exited unexpectedly; this likely means the error above caused a crash');\n clearTimeout(this.#pongTimeout);\n clearInterval(this.#pingInterval);\n clearInterval(this.#memoryWatch);\n this.#join.resolve();\n });\n\n this.mainTask(this.proc!);\n\n await this.#join.await;\n }\n\n async join() {\n if (!this.#started) {\n throw new Error('runner not started');\n }\n\n await this.#join.await;\n }\n\n async initialize() {\n const timer = setTimeout(() => {\n const err = new Error('runner initialization timed out');\n this.init.reject(err);\n throw err;\n }, this.#opts.initializeTimeout);\n this.proc!.send({\n case: 'initializeRequest',\n value: {\n loggerOptions,\n pingInterval: this.#opts.pingInterval,\n pingTimeout: this.#opts.pingTimeout,\n highPingThreshold: this.#opts.highPingThreshold,\n },\n });\n await once(this.proc!, 'message').then(([msg]: IPCMessage[]) => {\n clearTimeout(timer);\n if (msg!.case !== 'initializeResponse') {\n throw new Error('first message must be InitializeResponse');\n }\n });\n this.init.resolve();\n }\n\n async close() {\n if (!this.#started) {\n return;\n }\n this.#closing = true;\n\n if (!this.#runningJob) {\n this.proc!.kill();\n this.#join.resolve();\n }\n\n this.proc!.send({ case: 'shutdownRequest' });\n\n const timer = setTimeout(() => {\n this.#logger.error('job shutdown is taking too much time');\n }, this.#opts.closeTimeout);\n await this.#join.await.then(() => {\n clearTimeout(timer);\n clearTimeout(this.#pongTimeout);\n clearInterval(this.#pingInterval);\n });\n }\n\n async launchJob(info: RunningJobInfo) {\n if (this.#runningJob) {\n throw new Error('executor already has a running job');\n }\n this.#runningJob = info;\n this.proc!.send({ case: 'startJobRequest', value: { runningJob: info } });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,yBAAqB;AAErB,iBAAmC;AACnC,mBAAuB;AAahB,MAAe,eAAe;AAAA,EACnC;AAAA,EACA,WAAW;AAAA,EACX,WAAW;AAAA,EACX,cAA+B;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACU,OAAO,IAAI,oBAAO;AAAA,EAC5B,QAAQ,IAAI,oBAAO;AAAA,EACnB,cAAU,gBAAI,EAAE,MAAM,EAAE,YAAY,KAAK,YAAY,CAAC;AAAA,EAEtD,YACE,mBACA,cACA,cACA,eACA,cACA,aACA,mBACA;AACA,SAAK,QAAQ;AAAA,MACX;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA,EAKA,IAAI,UAAmB;AACrB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,aAAyC;AAC3C,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAM,QAAQ;AACZ,QAAI,KAAK,UAAU;AACjB,YAAM,IAAI,MAAM,wBAAwB;AAAA,IAC1C,WAAW,KAAK,UAAU;AACxB,YAAM,IAAI,MAAM,kBAAkB;AAAA,IACpC;AAEA,SAAK,OAAO,KAAK,cAAc;AAE/B,SAAK,WAAW;AAChB,SAAK,IAAI;AAAA,EACX;AAAA,EAEA,MAAM,MAAM;AACV,UAAM,KAAK,KAAK;AAEhB,SAAK,gBAAgB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EACpB;AAAA,EAEA,MAAM,QAAQ;AACZ,QAAI,CAAC,KAAK,UAAU;AAClB;AAAA,IACF;AACA,SAAK,WAAW;AAEhB,QAAI,CAAC,KAAK,aAAa;AACrB,WAAK,KAAM,KAAK;AAChB,WAAK,MAAM,QAAQ;AAAA,IACrB;AAEA,SAAK,KAAM,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAE3C,UAAM,QAAQ,WAAW,MAAM;AAC7B,WAAK,QAAQ,MAAM,sCAAsC;AAAA,IAC3D,GAAG,KAAK,MAAM,YAAY;AAC1B,UAAM,KAAK,MAAM,MAAM,KAAK,MAAM;AAChC,mBAAa,KAAK;AAClB,mBAAa,KAAK,YAAY;AAC9B,oBAAc,KAAK,aAAa;AAAA,IAClC,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,UAAU,MAAsB;AACpC,QAAI,KAAK,aAAa;AACpB,YAAM,IAAI,MAAM,oCAAoC;AAAA,IACtD;AACA,SAAK,cAAc;AACnB,SAAK,KAAM,KAAK,EAAE,MAAM,mBAAmB,OAAO,EAAE,YAAY,KAAK,EAAE,CAAC;AAAA,EAC1E;AACF;","names":[]}
@@ -0,0 +1,30 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import type { ChildProcess } from 'node:child_process';
3
+ import type { RunningJobInfo } from '../job.js';
4
+ import { Future } from '../utils.js';
5
+ export interface ProcOpts {
6
+ initializeTimeout: number;
7
+ closeTimeout: number;
8
+ memoryWarnMB: number;
9
+ memoryLimitMB: number;
10
+ pingInterval: number;
11
+ pingTimeout: number;
12
+ highPingThreshold: number;
13
+ }
14
+ export declare abstract class SupervisedProc {
15
+ #private;
16
+ proc?: ChildProcess;
17
+ protected init: Future;
18
+ constructor(initializeTimeout: number, closeTimeout: number, memoryWarnMB: number, memoryLimitMB: number, pingInterval: number, pingTimeout: number, highPingThreshold: number);
19
+ abstract createProcess(): ChildProcess;
20
+ abstract mainTask(child: ChildProcess): Promise<void>;
21
+ get started(): boolean;
22
+ get runningJob(): RunningJobInfo | undefined;
23
+ start(): Promise<void>;
24
+ run(): Promise<void>;
25
+ join(): Promise<void>;
26
+ initialize(): Promise<void>;
27
+ close(): Promise<void>;
28
+ launchJob(info: RunningJobInfo): Promise<void>;
29
+ }
30
+ //# sourceMappingURL=supervised_proc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"supervised_proc.d.ts","sourceRoot":"","sources":["../../src/ipc/supervised_proc.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEhD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,MAAM,WAAW,QAAQ;IACvB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,iBAAiB,EAAE,MAAM,CAAC;CAC3B;AAED,8BAAsB,cAAc;;IAKlC,IAAI,CAAC,EAAE,YAAY,CAAC;IAIpB,SAAS,CAAC,IAAI,SAAgB;gBAK5B,iBAAiB,EAAE,MAAM,EACzB,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,MAAM,EACnB,iBAAiB,EAAE,MAAM;IAa3B,QAAQ,CAAC,aAAa,IAAI,YAAY;IACtC,QAAQ,CAAC,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;IAErD,IAAI,OAAO,IAAI,OAAO,CAErB;IAED,IAAI,UAAU,IAAI,cAAc,GAAG,SAAS,CAE3C;IAEK,KAAK;IAaL,GAAG;IAwEH,IAAI;IAQJ,UAAU;IAwBV,KAAK;IAuBL,SAAS,CAAC,IAAI,EAAE,cAAc;CAOrC"}
@@ -1,24 +1,27 @@
1
1
  import { once } from "node:events";
2
2
  import { log, loggerOptions } from "../log.js";
3
3
  import { Future } from "../utils.js";
4
- import { JobExecutor } from "./job_executor.js";
5
- class ProcJobExecutor extends JobExecutor {
4
+ class SupervisedProc {
6
5
  #opts;
7
6
  #started = false;
8
7
  #closing = false;
9
8
  #runningJob = void 0;
10
- #proc;
9
+ proc;
11
10
  #pingInterval;
11
+ #memoryWatch;
12
12
  #pongTimeout;
13
- #init = new Future();
13
+ init = new Future();
14
14
  #join = new Future();
15
15
  #logger = log().child({ runningJob: this.#runningJob });
16
- constructor(agent, initializeTimeout, closeTimeout) {
17
- super();
16
+ constructor(initializeTimeout, closeTimeout, memoryWarnMB, memoryLimitMB, pingInterval, pingTimeout, highPingThreshold) {
18
17
  this.#opts = {
19
- agent,
20
18
  initializeTimeout,
21
- closeTimeout
19
+ closeTimeout,
20
+ memoryWarnMB,
21
+ memoryLimitMB,
22
+ pingInterval,
23
+ pingTimeout,
24
+ highPingThreshold
22
25
  };
23
26
  }
24
27
  get started() {
@@ -33,32 +36,41 @@ class ProcJobExecutor extends JobExecutor {
33
36
  } else if (this.#closing) {
34
37
  throw new Error("runner is closed");
35
38
  }
36
- this.#proc = await import("./job_main.js").then(
37
- (m) => m.runProcess({
38
- agentFile: this.#opts.agent
39
- })
40
- );
39
+ this.proc = this.createProcess();
41
40
  this.#started = true;
42
41
  this.run();
43
42
  }
44
43
  async run() {
45
- await this.#init.await;
44
+ await this.init.await;
46
45
  this.#pingInterval = setInterval(() => {
47
- this.#proc.send({ case: "pingRequest", value: { timestamp: Date.now() } });
48
- }, this.PING_INTERVAL);
46
+ this.proc.send({ case: "pingRequest", value: { timestamp: Date.now() } });
47
+ }, this.#opts.pingInterval);
49
48
  this.#pongTimeout = setTimeout(() => {
50
49
  this.#logger.warn("job is unresponsive");
51
50
  clearTimeout(this.#pongTimeout);
52
51
  clearInterval(this.#pingInterval);
53
- this.#proc.kill();
52
+ this.proc.kill();
54
53
  this.#join.resolve();
55
- }, this.PING_TIMEOUT);
54
+ }, this.#opts.pingTimeout);
55
+ this.#memoryWatch = setInterval(() => {
56
+ const memoryMB = process.memoryUsage().heapUsed / (1024 * 1024);
57
+ if (this.#opts.memoryLimitMB > 0 && memoryMB > this.#opts.memoryLimitMB) {
58
+ this.#logger.child({ memoryUsageMB: memoryMB, memoryLimitMB: this.#opts.memoryLimitMB }).error("process exceeded memory limit, killing process");
59
+ this.close();
60
+ } else if (this.#opts.memoryWarnMB > 0 && memoryMB > this.#opts.memoryWarnMB) {
61
+ this.#logger.child({
62
+ memoryUsageMB: memoryMB,
63
+ memoryWarnMB: this.#opts.memoryWarnMB,
64
+ memoryLimitMB: this.#opts.memoryLimitMB
65
+ }).error("process memory usage is high");
66
+ }
67
+ });
56
68
  const listener = (msg) => {
57
69
  var _a;
58
70
  switch (msg.case) {
59
71
  case "pongResponse": {
60
72
  const delay = Date.now() - msg.value.timestamp;
61
- if (delay > this.HIGH_PING_THRESHOLD) {
73
+ if (delay > this.#opts.highPingThreshold) {
62
74
  this.#logger.child({ delay }).warn("job executor is unresponsive");
63
75
  }
64
76
  (_a = this.#pongTimeout) == null ? void 0 : _a.refresh();
@@ -70,20 +82,22 @@ class ProcJobExecutor extends JobExecutor {
70
82
  }
71
83
  case "done": {
72
84
  this.#closing = true;
73
- this.#proc.off("message", listener);
85
+ this.proc.off("message", listener);
74
86
  this.#join.resolve();
75
87
  break;
76
88
  }
77
89
  }
78
90
  };
79
- this.#proc.on("message", listener);
80
- this.#proc.on("error", (err) => {
91
+ this.proc.on("message", listener);
92
+ this.proc.on("error", (err) => {
81
93
  if (this.#closing) return;
82
94
  this.#logger.child({ err }).warn("job process exited unexpectedly; this likely means the error above caused a crash");
83
95
  clearTimeout(this.#pongTimeout);
84
96
  clearInterval(this.#pingInterval);
97
+ clearInterval(this.#memoryWatch);
85
98
  this.#join.resolve();
86
99
  });
100
+ this.mainTask(this.proc);
87
101
  await this.#join.await;
88
102
  }
89
103
  async join() {
@@ -95,17 +109,25 @@ class ProcJobExecutor extends JobExecutor {
95
109
  async initialize() {
96
110
  const timer = setTimeout(() => {
97
111
  const err = new Error("runner initialization timed out");
98
- this.#init.reject(err);
112
+ this.init.reject(err);
99
113
  throw err;
100
114
  }, this.#opts.initializeTimeout);
101
- this.#proc.send({ case: "initializeRequest", value: { loggerOptions } });
102
- await once(this.#proc, "message").then(([msg]) => {
115
+ this.proc.send({
116
+ case: "initializeRequest",
117
+ value: {
118
+ loggerOptions,
119
+ pingInterval: this.#opts.pingInterval,
120
+ pingTimeout: this.#opts.pingTimeout,
121
+ highPingThreshold: this.#opts.highPingThreshold
122
+ }
123
+ });
124
+ await once(this.proc, "message").then(([msg]) => {
103
125
  clearTimeout(timer);
104
126
  if (msg.case !== "initializeResponse") {
105
127
  throw new Error("first message must be InitializeResponse");
106
128
  }
107
129
  });
108
- this.#init.resolve();
130
+ this.init.resolve();
109
131
  }
110
132
  async close() {
111
133
  if (!this.#started) {
@@ -113,10 +135,10 @@ class ProcJobExecutor extends JobExecutor {
113
135
  }
114
136
  this.#closing = true;
115
137
  if (!this.#runningJob) {
116
- this.#proc.kill();
138
+ this.proc.kill();
117
139
  this.#join.resolve();
118
140
  }
119
- this.#proc.send({ case: "shutdownRequest" });
141
+ this.proc.send({ case: "shutdownRequest" });
120
142
  const timer = setTimeout(() => {
121
143
  this.#logger.error("job shutdown is taking too much time");
122
144
  }, this.#opts.closeTimeout);
@@ -131,10 +153,10 @@ class ProcJobExecutor extends JobExecutor {
131
153
  throw new Error("executor already has a running job");
132
154
  }
133
155
  this.#runningJob = info;
134
- this.#proc.send({ case: "startJobRequest", value: { runningJob: info } });
156
+ this.proc.send({ case: "startJobRequest", value: { runningJob: info } });
135
157
  }
136
158
  }
137
159
  export {
138
- ProcJobExecutor
160
+ SupervisedProc
139
161
  };
140
- //# sourceMappingURL=proc_job_executor.js.map
162
+ //# sourceMappingURL=supervised_proc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/ipc/supervised_proc.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChildProcess } from 'node:child_process';\nimport { once } from 'node:events';\nimport type { RunningJobInfo } from '../job.js';\nimport { log, loggerOptions } from '../log.js';\nimport { Future } from '../utils.js';\nimport type { IPCMessage } from './message.js';\n\nexport interface ProcOpts {\n initializeTimeout: number;\n closeTimeout: number;\n memoryWarnMB: number;\n memoryLimitMB: number;\n pingInterval: number;\n pingTimeout: number;\n highPingThreshold: number;\n}\n\nexport abstract class SupervisedProc {\n #opts: ProcOpts;\n #started = false;\n #closing = false;\n #runningJob?: RunningJobInfo = undefined;\n proc?: ChildProcess;\n #pingInterval?: ReturnType<typeof setInterval>;\n #memoryWatch?: ReturnType<typeof setInterval>;\n #pongTimeout?: ReturnType<typeof setTimeout>;\n protected init = new Future();\n #join = new Future();\n #logger = log().child({ runningJob: this.#runningJob });\n\n constructor(\n initializeTimeout: number,\n closeTimeout: number,\n memoryWarnMB: number,\n memoryLimitMB: number,\n pingInterval: number,\n pingTimeout: number,\n highPingThreshold: number,\n ) {\n this.#opts = {\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n };\n }\n\n abstract createProcess(): ChildProcess;\n abstract mainTask(child: ChildProcess): Promise<void>;\n\n get started(): boolean {\n return this.#started;\n }\n\n get runningJob(): RunningJobInfo | undefined {\n return this.#runningJob;\n }\n\n async start() {\n if (this.#started) {\n throw new Error('runner already started');\n } else if (this.#closing) {\n throw new Error('runner is closed');\n }\n\n this.proc = this.createProcess();\n\n this.#started = true;\n this.run();\n }\n\n async run() {\n await this.init.await;\n\n this.#pingInterval = setInterval(() => {\n this.proc!.send({ case: 'pingRequest', value: { timestamp: Date.now() } });\n }, this.#opts.pingInterval);\n\n this.#pongTimeout = setTimeout(() => {\n this.#logger.warn('job is unresponsive');\n clearTimeout(this.#pongTimeout);\n clearInterval(this.#pingInterval);\n this.proc!.kill();\n this.#join.resolve();\n }, this.#opts.pingTimeout);\n\n this.#memoryWatch = setInterval(() => {\n const memoryMB = process.memoryUsage().heapUsed / (1024 * 1024);\n if (this.#opts.memoryLimitMB > 0 && memoryMB > this.#opts.memoryLimitMB) {\n this.#logger\n .child({ memoryUsageMB: memoryMB, memoryLimitMB: this.#opts.memoryLimitMB })\n .error('process exceeded memory limit, killing process');\n this.close();\n } else if (this.#opts.memoryWarnMB > 0 && memoryMB > this.#opts.memoryWarnMB) {\n this.#logger\n .child({\n memoryUsageMB: memoryMB,\n memoryWarnMB: this.#opts.memoryWarnMB,\n memoryLimitMB: this.#opts.memoryLimitMB,\n })\n .error('process memory usage is high');\n }\n });\n\n const listener = (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pongResponse': {\n const delay = Date.now() - msg.value.timestamp;\n if (delay > this.#opts.highPingThreshold) {\n this.#logger.child({ delay }).warn('job executor is unresponsive');\n }\n this.#pongTimeout?.refresh();\n break;\n }\n case 'exiting': {\n this.#logger.child({ reason: msg.value.reason }).debug('job exiting');\n break;\n }\n case 'done': {\n this.#closing = true;\n this.proc!.off('message', listener);\n this.#join.resolve();\n break;\n }\n }\n };\n this.proc!.on('message', listener);\n this.proc!.on('error', (err) => {\n if (this.#closing) return;\n this.#logger\n .child({ err })\n .warn('job process exited unexpectedly; this likely means the error above caused a crash');\n clearTimeout(this.#pongTimeout);\n clearInterval(this.#pingInterval);\n clearInterval(this.#memoryWatch);\n this.#join.resolve();\n });\n\n this.mainTask(this.proc!);\n\n await this.#join.await;\n }\n\n async join() {\n if (!this.#started) {\n throw new Error('runner not started');\n }\n\n await this.#join.await;\n }\n\n async initialize() {\n const timer = setTimeout(() => {\n const err = new Error('runner initialization timed out');\n this.init.reject(err);\n throw err;\n }, this.#opts.initializeTimeout);\n this.proc!.send({\n case: 'initializeRequest',\n value: {\n loggerOptions,\n pingInterval: this.#opts.pingInterval,\n pingTimeout: this.#opts.pingTimeout,\n highPingThreshold: this.#opts.highPingThreshold,\n },\n });\n await once(this.proc!, 'message').then(([msg]: IPCMessage[]) => {\n clearTimeout(timer);\n if (msg!.case !== 'initializeResponse') {\n throw new Error('first message must be InitializeResponse');\n }\n });\n this.init.resolve();\n }\n\n async close() {\n if (!this.#started) {\n return;\n }\n this.#closing = true;\n\n if (!this.#runningJob) {\n this.proc!.kill();\n this.#join.resolve();\n }\n\n this.proc!.send({ case: 'shutdownRequest' });\n\n const timer = setTimeout(() => {\n this.#logger.error('job shutdown is taking too much time');\n }, this.#opts.closeTimeout);\n await this.#join.await.then(() => {\n clearTimeout(timer);\n clearTimeout(this.#pongTimeout);\n clearInterval(this.#pingInterval);\n });\n }\n\n async launchJob(info: RunningJobInfo) {\n if (this.#runningJob) {\n throw new Error('executor already has a running job');\n }\n this.#runningJob = info;\n this.proc!.send({ case: 'startJobRequest', value: { runningJob: info } });\n }\n}\n"],"mappings":"AAIA,SAAS,YAAY;AAErB,SAAS,KAAK,qBAAqB;AACnC,SAAS,cAAc;AAahB,MAAe,eAAe;AAAA,EACnC;AAAA,EACA,WAAW;AAAA,EACX,WAAW;AAAA,EACX,cAA+B;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACU,OAAO,IAAI,OAAO;AAAA,EAC5B,QAAQ,IAAI,OAAO;AAAA,EACnB,UAAU,IAAI,EAAE,MAAM,EAAE,YAAY,KAAK,YAAY,CAAC;AAAA,EAEtD,YACE,mBACA,cACA,cACA,eACA,cACA,aACA,mBACA;AACA,SAAK,QAAQ;AAAA,MACX;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA,EAKA,IAAI,UAAmB;AACrB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,aAAyC;AAC3C,WAAO,KAAK;AAAA,EAC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,QACzB,aAAa,KAAK,MAAM;AAAA,QACxB,mBAAmB,KAAK,MAAM;AAAA,MAChC;AAAA,IACF,CAAC;AACD,UAAM,KAAK,KAAK,MAAO,SAAS,EAAE,KAAK,CAAC,CAAC,GAAG,MAAoB;AAC9D,mBAAa,KAAK;AAClB,UAAI,IAAK,SAAS,sBAAsB;AACtC,cAAM,IAAI,MAAM,0CAA0C;AAAA,MAC5D;AAAA,IACF,CAAC;AACD,SAAK,KAAK,QAAQ;AAAA,EACpB;AAAA,EAEA,MAAM,QAAQ;AACZ,QAAI,CAAC,KAAK,UAAU;AAClB;AAAA,IACF;AACA,SAAK,WAAW;AAEhB,QAAI,CAAC,KAAK,aAAa;AACrB,WAAK,KAAM,KAAK;AAChB,WAAK,MAAM,QAAQ;AAAA,IACrB;AAEA,SAAK,KAAM,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAE3C,UAAM,QAAQ,WAAW,MAAM;AAC7B,WAAK,QAAQ,MAAM,sCAAsC;AAAA,IAC3D,GAAG,KAAK,MAAM,YAAY;AAC1B,UAAM,KAAK,MAAM,MAAM,KAAK,MAAM;AAChC,mBAAa,KAAK;AAClB,mBAAa,KAAK,YAAY;AAC9B,oBAAc,KAAK,aAAa;AAAA,IAClC,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,UAAU,MAAsB;AACpC,QAAI,KAAK,aAAa;AACpB,YAAM,IAAI,MAAM,oCAAoC;AAAA,IACtD;AACA,SAAK,cAAc;AACnB,SAAK,KAAM,KAAK,EAAE,MAAM,mBAAmB,OAAO,EAAE,YAAY,KAAK,EAAE,CAAC;AAAA,EAC1E;AACF;","names":[]}
package/dist/job.cjs CHANGED
@@ -19,6 +19,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var job_exports = {};
20
20
  __export(job_exports, {
21
21
  AutoSubscribe: () => AutoSubscribe,
22
+ CurrentJobContext: () => CurrentJobContext,
22
23
  FunctionExistsError: () => FunctionExistsError,
23
24
  JobContext: () => JobContext,
24
25
  JobProcess: () => JobProcess,
@@ -27,6 +28,15 @@ __export(job_exports, {
27
28
  module.exports = __toCommonJS(job_exports);
28
29
  var import_rtc_node = require("@livekit/rtc-node");
29
30
  var import_log = require("./log.cjs");
31
+ class CurrentJobContext {
32
+ static #current;
33
+ constructor(proc) {
34
+ CurrentJobContext.#current = proc;
35
+ }
36
+ static getCurrent() {
37
+ return CurrentJobContext.#current;
38
+ }
39
+ }
30
40
  var AutoSubscribe = /* @__PURE__ */ ((AutoSubscribe2) => {
31
41
  AutoSubscribe2[AutoSubscribe2["SUBSCRIBE_ALL"] = 0] = "SUBSCRIBE_ALL";
32
42
  AutoSubscribe2[AutoSubscribe2["SUBSCRIBE_NONE"] = 1] = "SUBSCRIBE_NONE";
@@ -51,7 +61,8 @@ class JobContext {
51
61
  #participantEntrypoints = [];
52
62
  #participantTasks = {};
53
63
  #logger;
54
- constructor(proc, info, room, onConnect, onShutdown) {
64
+ #inferenceExecutor;
65
+ constructor(proc, info, room, onConnect, onShutdown, inferenceExecutor) {
55
66
  this.#proc = proc;
56
67
  this.#info = info;
57
68
  this.#room = room;
@@ -60,6 +71,7 @@ class JobContext {
60
71
  this.onParticipantConnected = this.onParticipantConnected.bind(this);
61
72
  this.#room.on(import_rtc_node.RoomEvent.ParticipantConnected, this.onParticipantConnected);
62
73
  this.#logger = (0, import_log.log)().child({ info: this.#info });
74
+ this.#inferenceExecutor = inferenceExecutor;
63
75
  }
64
76
  get proc() {
65
77
  return this.#proc;
@@ -75,6 +87,10 @@ class JobContext {
75
87
  get agent() {
76
88
  return this.#room.localParticipant;
77
89
  }
90
+ /** @returns The global inference executor */
91
+ get inferenceExecutor() {
92
+ return this.#inferenceExecutor;
93
+ }
78
94
  /** Adds a promise to be awaited when {@link JobContext.shutdown | shutdown} is called. */
79
95
  addShutdownCallback(callback) {
80
96
  this.shutdownCallbacks.push(callback);
@@ -222,6 +238,7 @@ class JobRequest {
222
238
  // Annotate the CommonJS export names for ESM import in node:
223
239
  0 && (module.exports = {
224
240
  AutoSubscribe,
241
+ CurrentJobContext,
225
242
  FunctionExistsError,
226
243
  JobContext,
227
244
  JobProcess,
package/dist/job.cjs.map CHANGED
@@ -1 +1 @@
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 attributes?: { [key: string]: 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 = '', attributes?: { [key: string]: string }) {\n if (identity === '') identity = 'agent-' + this.id;\n\n this.#onAccept({ name, identity, metadata, attributes });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWA,sBAAsD;AAEtD,iBAAoB;AAGb,IAAK,gBAAL,kBAAKA,mBAAL;AACL,EAAAA,8BAAA;AACA,EAAAA,8BAAA;AACA,EAAAA,8BAAA;AACA,EAAAA,8BAAA;AAJU,SAAAA;AAAA,GAAA;AAsBL,MAAM,4BAA4B,MAAM;AAAA,EAC7C,YAAY,KAAc;AACxB,UAAM,GAAG;A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M,KAAK,UAAU;AAAA,EACvB;AAAA;AAAA,EAGA,MAAM,OAAO,OAAO,IAAI,WAAW,IAAI,WAAW,IAAI,YAAwC;AAC5F,QAAI,aAAa,GAAI,YAAW,WAAW,KAAK;AAEhD,SAAK,UAAU,EAAE,MAAM,UAAU,UAAU,WAAW,CAAC;AAAA,EACzD;AACF;","names":["AutoSubscribe"]}
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 type { InferenceExecutor } from './ipc/inference_executor.js';\nimport { log } from './log.js';\n\nexport class CurrentJobContext {\n static #current: JobContext;\n\n constructor(proc: JobContext) {\n CurrentJobContext.#current = proc;\n }\n\n static getCurrent(): JobContext {\n return CurrentJobContext.#current;\n }\n}\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 attributes?: { [key: string]: 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 #inferenceExecutor: InferenceExecutor;\n\n constructor(\n proc: JobProcess,\n info: RunningJobInfo,\n room: Room,\n onConnect: () => void,\n onShutdown: (s: string) => void,\n inferenceExecutor: InferenceExecutor,\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 this.#inferenceExecutor = inferenceExecutor;\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 /** @returns The global inference executor */\n get inferenceExecutor(): InferenceExecutor {\n return this.#inferenceExecutor;\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 = '', attributes?: { [key: string]: string }) {\n if (identity === '') identity = 'agent-' + this.id;\n\n this.#onAccept({ name, identity, metadata, attributes });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWA,sBAAsD;AAGtD,iBAAoB;AAEb,MAAM,kBAAkB;AAAA,EAC7B,OAAO;AAAA,EAEP,YAAY,MAAkB;AAC5B,sBAAkB,WAAW;AAAA,EAC/B;AAAA,EAEA,OAAO,aAAyB;AAC9B,WAAO,kBAAkB;AAAA,EAC3B;AACF;AAGO,IAAK,gBAAL,kBAAKA,mBAAL;AACL,EAAAA,8BAAA;AACA,EAAAA,8BAAA;AACA,EAAAA,8BAAA;AACA,EAAAA,8BAAA;AAJU,SAAAA;AAAA,GAAA;AAsBL,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,EACA;AAAA,EAEA,YACE,MACA,MACA,MACA,WACA,YACA,mBACA;AACA,SAAK,QAAQ;AACb,SAAK,QAAQ;AACb,SAAK,QAAQ;AACb,SAAK,aAAa;AAClB,SAAK,cAAc;AACnB,SAAK,yBAAyB,KAAK,uBAAuB,KAAK,IAAI;AACnE,SAAK,MAAM,GAAG,0BAAU,sBAAsB,KAAK,sBAAsB;AACzE,SAAK,cAAU,gBAAI,EAAE,MAAM,EAAE,MAAM,KAAK,MAAM,CAAC;AAC/C,SAAK,qBAAqB;AAAA,EAC5B;AAAA,EAEA,IAAI,OAAmB;AACrB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,MAAiB;AACnB,WAAO,KAAK,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;AAAA,EAC5C;AACF;AAEO,MAAM,WAAW;AAAA,EACtB,OAAO,QAAQ;AAAA,EACf,WAAsC,CAAC;AAAA,EAEvC,IAAI,MAAc;AAChB,WAAO,KAAK;AAAA,EACd;AACF;AAUO,MAAM,WAAW;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGA,YACE,KACA,UACA,UACA;AACA,SAAK,OAAO;AACZ,SAAK,YAAY;AACjB,SAAK,YAAY;AAAA,EACnB;AAAA;AAAA,EAGA,IAAI,KAAa;AACf,WAAO,KAAK,KAAK;AAAA,EACnB;AAAA;AAAA,EAGA,IAAI,MAAiB;AACnB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,OAA+B;AACjC,WAAO,KAAK,KAAK;AAAA,EACnB;AAAA;AAAA,EAGA,IAAI,YAA+C;AACjD,WAAO,KAAK,KAAK;AAAA,EACnB;AAAA;AAAA,EAGA,IAAI,YAAoB;AACtB,WAAO,KAAK,KAAK;AAAA,EACnB;AAAA;AAAA,EAGA,MAAM,SAAS;AACb,UAAM,KAAK,UAAU;AAAA,EACvB;AAAA;AAAA,EAGA,MAAM,OAAO,OAAO,IAAI,WAAW,IAAI,WAAW,IAAI,YAAwC;AAC5F,QAAI,aAAa,GAAI,YAAW,WAAW,KAAK;AAEhD,SAAK,UAAU,EAAE,MAAM,UAAU,UAAU,WAAW,CAAC;AAAA,EACzD;AACF;","names":["AutoSubscribe"]}
package/dist/job.d.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  import type * as proto from '@livekit/protocol';
2
2
  import type { E2EEOptions, LocalParticipant, RemoteParticipant, Room, RtcConfiguration } from '@livekit/rtc-node';
3
+ import type { InferenceExecutor } from './ipc/inference_executor.js';
4
+ export declare class CurrentJobContext {
5
+ #private;
6
+ constructor(proc: JobContext);
7
+ static getCurrent(): JobContext;
8
+ }
3
9
  /** Which tracks, if any, should the agent automatically subscribe to? */
4
10
  export declare enum AutoSubscribe {
5
11
  SUBSCRIBE_ALL = 0,
@@ -30,13 +36,15 @@ export declare class JobContext {
30
36
  #private;
31
37
  /** @internal */
32
38
  shutdownCallbacks: (() => Promise<void>)[];
33
- constructor(proc: JobProcess, info: RunningJobInfo, room: Room, onConnect: () => void, onShutdown: (s: string) => void);
39
+ constructor(proc: JobProcess, info: RunningJobInfo, room: Room, onConnect: () => void, onShutdown: (s: string) => void, inferenceExecutor: InferenceExecutor);
34
40
  get proc(): JobProcess;
35
41
  get job(): proto.Job;
36
42
  /** @returns The room the agent was called into */
37
43
  get room(): Room;
38
44
  /** @returns The agent's participant if connected to the room, otherwise `undefined` */
39
45
  get agent(): LocalParticipant | undefined;
46
+ /** @returns The global inference executor */
47
+ get inferenceExecutor(): InferenceExecutor;
40
48
  /** Adds a promise to be awaited when {@link JobContext.shutdown | shutdown} is called. */
41
49
  addShutdownCallback(callback: () => Promise<void>): void;
42
50
  waitForParticipant(identity?: string): Promise<RemoteParticipant>;
package/dist/job.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"job.d.ts","sourceRoot":"","sources":["../src/job.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,KAAK,MAAM,mBAAmB,CAAC;AAChD,OAAO,KAAK,EACV,WAAW,EACX,gBAAgB,EAChB,iBAAiB,EACjB,IAAI,EACJ,gBAAgB,EACjB,MAAM,mBAAmB,CAAC;AAK3B,yEAAyE;AACzE,oBAAY,aAAa;IACvB,aAAa,IAAA;IACb,cAAc,IAAA;IACd,UAAU,IAAA;IACV,UAAU,IAAA;CACX;AAED,MAAM,MAAM,kBAAkB,GAAG;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;CACxC,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,eAAe,EAAE,kBAAkB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