@livekit/agents 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.cjs +13 -3
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +13 -3
- package/dist/cli.js.map +1 -1
- package/dist/http_server.cjs +4 -1
- package/dist/http_server.cjs.map +1 -1
- package/dist/http_server.d.ts +7 -1
- package/dist/http_server.d.ts.map +1 -1
- package/dist/http_server.js +4 -1
- package/dist/http_server.js.map +1 -1
- package/dist/ipc/inference_proc_lazy_main.cjs +5 -1
- package/dist/ipc/inference_proc_lazy_main.cjs.map +1 -1
- package/dist/ipc/inference_proc_lazy_main.js +5 -1
- package/dist/ipc/inference_proc_lazy_main.js.map +1 -1
- package/dist/ipc/job_proc_lazy_main.cjs +9 -1
- package/dist/ipc/job_proc_lazy_main.cjs.map +1 -1
- package/dist/ipc/job_proc_lazy_main.js +9 -1
- package/dist/ipc/job_proc_lazy_main.js.map +1 -1
- package/dist/multimodal/agent_playout.cjs +13 -8
- package/dist/multimodal/agent_playout.cjs.map +1 -1
- package/dist/multimodal/agent_playout.d.ts.map +1 -1
- package/dist/multimodal/agent_playout.js +13 -8
- package/dist/multimodal/agent_playout.js.map +1 -1
- package/dist/pipeline/agent_output.cjs +3 -1
- package/dist/pipeline/agent_output.cjs.map +1 -1
- package/dist/pipeline/agent_output.js +3 -1
- package/dist/pipeline/agent_output.js.map +1 -1
- package/dist/worker.cjs +20 -8
- package/dist/worker.cjs.map +1 -1
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +20 -8
- package/dist/worker.js.map +1 -1
- package/package.json +1 -1
- package/src/cli.ts +14 -3
- package/src/http_server.ts +10 -1
- package/src/ipc/inference_proc_lazy_main.ts +10 -2
- package/src/ipc/job_proc_lazy_main.ts +15 -2
- package/src/multimodal/agent_playout.ts +15 -9
- package/src/pipeline/agent_output.ts +3 -1
- package/src/worker.ts +22 -8
package/dist/cli.cjs
CHANGED
|
@@ -39,21 +39,31 @@ const runWorker = async (args) => {
|
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
41
|
process.once("SIGINT", async () => {
|
|
42
|
+
logger.info("SIGINT received in CLI");
|
|
42
43
|
process.once("SIGINT", () => {
|
|
43
|
-
logger.info("worker closed forcefully");
|
|
44
|
+
logger.info("worker closed forcefully due to SIGINT.");
|
|
44
45
|
process.exit(130);
|
|
45
46
|
});
|
|
46
47
|
if (args.production) {
|
|
47
48
|
await worker.drain();
|
|
48
49
|
}
|
|
49
50
|
await worker.close();
|
|
50
|
-
logger.info("worker closed");
|
|
51
|
+
logger.info("worker closed due to SIGINT.");
|
|
51
52
|
process.exit(130);
|
|
52
53
|
});
|
|
54
|
+
process.once("SIGTERM", async () => {
|
|
55
|
+
logger.info("SIGTERM received in CLI.");
|
|
56
|
+
if (args.production) {
|
|
57
|
+
await worker.drain();
|
|
58
|
+
}
|
|
59
|
+
await worker.close();
|
|
60
|
+
logger.info("worker closed due to SIGTERM.");
|
|
61
|
+
process.exit(143);
|
|
62
|
+
});
|
|
53
63
|
try {
|
|
54
64
|
await worker.run();
|
|
55
65
|
} catch {
|
|
56
|
-
logger.fatal("worker
|
|
66
|
+
logger.fatal("closing worker due to error.");
|
|
57
67
|
process.exit(1);
|
|
58
68
|
}
|
|
59
69
|
};
|
package/dist/cli.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/cli.ts","../../node_modules/.pnpm/tsup@8.3.5_@microsoft+api-extractor@7.43.7_@types+node@22.5.5__postcss@8.4.38_tsx@4.19.2_typescript@5.4.5/node_modules/tsup/assets/cjs_shims.js"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Command, Option } from 'commander';\nimport type { EventEmitter } from 'node:events';\nimport { initializeLogger, log } from './log.js';\nimport { version } from './version.js';\nimport { Worker, WorkerOptions } from './worker.js';\n\ntype CliArgs = {\n opts: WorkerOptions;\n production: boolean;\n watch: boolean;\n event?: EventEmitter;\n room?: string;\n participantIdentity?: string;\n};\n\nconst runWorker = async (args: CliArgs) => {\n initializeLogger({ pretty: !args.production, level: args.opts.logLevel });\n const logger = log();\n\n // though `production` is defined in WorkerOptions, it will always be overriddden by CLI.\n const { production: _, ...opts } = args.opts; // eslint-disable-line @typescript-eslint/no-unused-vars\n const worker = new Worker(new WorkerOptions({ production: args.production, ...opts }));\n\n if (args.room) {\n worker.event.once('worker_registered', () => {\n logger.info(`connecting to room ${args.room}`);\n worker.simulateJob(args.room!, args.participantIdentity);\n });\n }\n\n process.once('SIGINT', async () => {\n // allow C-c C-c for force interrupt\n process.once('SIGINT', () => {\n logger.info('worker closed forcefully');\n process.exit(130); // SIGINT exit code\n });\n if (args.production) {\n await worker.drain();\n }\n await worker.close();\n logger.info('worker closed');\n process.exit(130); // SIGINT exit code\n });\n\n try {\n await worker.run();\n } catch {\n logger.fatal('worker
|
|
1
|
+
{"version":3,"sources":["../src/cli.ts","../../node_modules/.pnpm/tsup@8.3.5_@microsoft+api-extractor@7.43.7_@types+node@22.5.5__postcss@8.4.38_tsx@4.19.2_typescript@5.4.5/node_modules/tsup/assets/cjs_shims.js"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Command, Option } from 'commander';\nimport type { EventEmitter } from 'node:events';\nimport { initializeLogger, log } from './log.js';\nimport { version } from './version.js';\nimport { Worker, WorkerOptions } from './worker.js';\n\ntype CliArgs = {\n opts: WorkerOptions;\n production: boolean;\n watch: boolean;\n event?: EventEmitter;\n room?: string;\n participantIdentity?: string;\n};\n\nconst runWorker = async (args: CliArgs) => {\n initializeLogger({ pretty: !args.production, level: args.opts.logLevel });\n const logger = log();\n\n // though `production` is defined in WorkerOptions, it will always be overriddden by CLI.\n const { production: _, ...opts } = args.opts; // eslint-disable-line @typescript-eslint/no-unused-vars\n const worker = new Worker(new WorkerOptions({ production: args.production, ...opts }));\n\n if (args.room) {\n worker.event.once('worker_registered', () => {\n logger.info(`connecting to room ${args.room}`);\n worker.simulateJob(args.room!, args.participantIdentity);\n });\n }\n\n process.once('SIGINT', async () => {\n logger.info('SIGINT received in CLI');\n // allow C-c C-c for force interrupt\n process.once('SIGINT', () => {\n logger.info('worker closed forcefully due to SIGINT.');\n process.exit(130); // SIGINT exit code\n });\n if (args.production) {\n await worker.drain();\n }\n await worker.close();\n logger.info('worker closed due to SIGINT.');\n process.exit(130); // SIGINT exit code\n });\n\n process.once('SIGTERM', async () => {\n logger.info('SIGTERM received in CLI.');\n if (args.production) {\n await worker.drain();\n }\n await worker.close();\n logger.info('worker closed due to SIGTERM.');\n process.exit(143); // SIGTERM exit code\n });\n\n try {\n await worker.run();\n } catch {\n logger.fatal('closing worker due to error.');\n process.exit(1);\n }\n};\n\n/**\n * Exposes a CLI for creating a new worker, in development or production mode.\n *\n * @param opts - Options to launch the worker with\n * @example\n * ```\n * if (process.argv[1] === fileURLToPath(import.meta.url)) {\n * cli.runApp(new WorkerOptions({ agent: import.meta.filename }));\n * }\n * ```\n */\nexport const runApp = (opts: WorkerOptions) => {\n const program = new Command()\n .name('agents')\n .description('LiveKit Agents CLI')\n .version(version)\n .addOption(\n new Option('--log-level <level>', 'Set the logging level')\n .choices(['trace', 'debug', 'info', 'warn', 'error', 'fatal'])\n .default('info')\n .env('LOG_LEVEL'),\n )\n .addOption(\n new Option('--url <string>', 'LiveKit server or Cloud project websocket URL').env(\n 'LIVEKIT_URL',\n ),\n )\n .addOption(\n new Option('--api-key <string>', \"LiveKit server or Cloud project's API key\").env(\n 'LIVEKIT_API_KEY',\n ),\n )\n .addOption(\n new Option('--api-secret <string>', \"LiveKit server or Cloud project's API secret\").env(\n 'LIVEKIT_API_SECRET',\n ),\n )\n .action(() => {\n if (\n // do not run CLI if origin file is agents/ipc/job_main.js\n process.argv[1] !== new URL('ipc/job_main.js', import.meta.url).pathname &&\n process.argv.length < 3\n ) {\n program.help();\n }\n });\n\n program\n .command('start')\n .description('Start the worker in production mode')\n .action(() => {\n const options = program.optsWithGlobals();\n opts.wsURL = options.url || opts.wsURL;\n opts.apiKey = options.apiKey || opts.apiKey;\n opts.apiSecret = options.apiSecret || opts.apiSecret;\n opts.logLevel = options.logLevel || opts.logLevel;\n runWorker({\n opts,\n production: true,\n watch: false,\n });\n });\n\n program\n .command('dev')\n .description('Start the worker in development mode')\n .addOption(\n new Option('--log-level <level>', 'Set the logging level')\n .choices(['trace', 'debug', 'info', 'warn', 'error', 'fatal'])\n .default('debug')\n .env('LOG_LEVEL'),\n )\n .action(() => {\n const options = program.optsWithGlobals();\n opts.wsURL = options.url || opts.wsURL;\n opts.apiKey = options.apiKey || opts.apiKey;\n opts.apiSecret = options.apiSecret || opts.apiSecret;\n opts.logLevel = options.logLevel || opts.logLevel;\n runWorker({\n opts,\n production: false,\n watch: false,\n });\n });\n\n program\n .command('connect')\n .description('Connect to a specific room')\n .requiredOption('--room <string>', 'Room name to connect to')\n .option('--participant-identity <string>', 'Identity of user to listen to')\n .addOption(\n new Option('--log-level <level>', 'Set the logging level')\n .choices(['trace', 'debug', 'info', 'warn', 'error', 'fatal'])\n .default('debug')\n .env('LOG_LEVEL'),\n )\n .action((...[, command]) => {\n const options = command.optsWithGlobals();\n opts.wsURL = options.url || opts.wsURL;\n opts.apiKey = options.apiKey || opts.apiKey;\n opts.apiSecret = options.apiSecret || opts.apiSecret;\n opts.logLevel = options.logLevel || opts.logLevel;\n runWorker({\n opts,\n production: false,\n watch: false,\n room: options.room,\n participantIdentity: options.participantIdentity,\n });\n });\n\n program.parse();\n};\n","// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () =>\n typeof document === 'undefined'\n ? new URL(`file:${__filename}`).href\n : (document.currentScript && document.currentScript.src) ||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;AAChB;","names":[]}
|
package/dist/cli.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAOA,OAAO,EAAU,aAAa,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAOA,OAAO,EAAU,aAAa,EAAE,MAAM,aAAa,CAAC;AA2DpD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,MAAM,SAAU,aAAa,SAqGzC,CAAC"}
|
package/dist/cli.js
CHANGED
|
@@ -14,21 +14,31 @@ const runWorker = async (args) => {
|
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
16
|
process.once("SIGINT", async () => {
|
|
17
|
+
logger.info("SIGINT received in CLI");
|
|
17
18
|
process.once("SIGINT", () => {
|
|
18
|
-
logger.info("worker closed forcefully");
|
|
19
|
+
logger.info("worker closed forcefully due to SIGINT.");
|
|
19
20
|
process.exit(130);
|
|
20
21
|
});
|
|
21
22
|
if (args.production) {
|
|
22
23
|
await worker.drain();
|
|
23
24
|
}
|
|
24
25
|
await worker.close();
|
|
25
|
-
logger.info("worker closed");
|
|
26
|
+
logger.info("worker closed due to SIGINT.");
|
|
26
27
|
process.exit(130);
|
|
27
28
|
});
|
|
29
|
+
process.once("SIGTERM", async () => {
|
|
30
|
+
logger.info("SIGTERM received in CLI.");
|
|
31
|
+
if (args.production) {
|
|
32
|
+
await worker.drain();
|
|
33
|
+
}
|
|
34
|
+
await worker.close();
|
|
35
|
+
logger.info("worker closed due to SIGTERM.");
|
|
36
|
+
process.exit(143);
|
|
37
|
+
});
|
|
28
38
|
try {
|
|
29
39
|
await worker.run();
|
|
30
40
|
} catch {
|
|
31
|
-
logger.fatal("worker
|
|
41
|
+
logger.fatal("closing worker due to error.");
|
|
32
42
|
process.exit(1);
|
|
33
43
|
}
|
|
34
44
|
};
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/cli.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Command, Option } from 'commander';\nimport type { EventEmitter } from 'node:events';\nimport { initializeLogger, log } from './log.js';\nimport { version } from './version.js';\nimport { Worker, WorkerOptions } from './worker.js';\n\ntype CliArgs = {\n opts: WorkerOptions;\n production: boolean;\n watch: boolean;\n event?: EventEmitter;\n room?: string;\n participantIdentity?: string;\n};\n\nconst runWorker = async (args: CliArgs) => {\n initializeLogger({ pretty: !args.production, level: args.opts.logLevel });\n const logger = log();\n\n // though `production` is defined in WorkerOptions, it will always be overriddden by CLI.\n const { production: _, ...opts } = args.opts; // eslint-disable-line @typescript-eslint/no-unused-vars\n const worker = new Worker(new WorkerOptions({ production: args.production, ...opts }));\n\n if (args.room) {\n worker.event.once('worker_registered', () => {\n logger.info(`connecting to room ${args.room}`);\n worker.simulateJob(args.room!, args.participantIdentity);\n });\n }\n\n process.once('SIGINT', async () => {\n // allow C-c C-c for force interrupt\n process.once('SIGINT', () => {\n logger.info('worker closed forcefully');\n process.exit(130); // SIGINT exit code\n });\n if (args.production) {\n await worker.drain();\n }\n await worker.close();\n logger.info('worker closed');\n process.exit(130); // SIGINT exit code\n });\n\n try {\n await worker.run();\n } catch {\n logger.fatal('worker
|
|
1
|
+
{"version":3,"sources":["../src/cli.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Command, Option } from 'commander';\nimport type { EventEmitter } from 'node:events';\nimport { initializeLogger, log } from './log.js';\nimport { version } from './version.js';\nimport { Worker, WorkerOptions } from './worker.js';\n\ntype CliArgs = {\n opts: WorkerOptions;\n production: boolean;\n watch: boolean;\n event?: EventEmitter;\n room?: string;\n participantIdentity?: string;\n};\n\nconst runWorker = async (args: CliArgs) => {\n initializeLogger({ pretty: !args.production, level: args.opts.logLevel });\n const logger = log();\n\n // though `production` is defined in WorkerOptions, it will always be overriddden by CLI.\n const { production: _, ...opts } = args.opts; // eslint-disable-line @typescript-eslint/no-unused-vars\n const worker = new Worker(new WorkerOptions({ production: args.production, ...opts }));\n\n if (args.room) {\n worker.event.once('worker_registered', () => {\n logger.info(`connecting to room ${args.room}`);\n worker.simulateJob(args.room!, args.participantIdentity);\n });\n }\n\n process.once('SIGINT', async () => {\n logger.info('SIGINT received in CLI');\n // allow C-c C-c for force interrupt\n process.once('SIGINT', () => {\n logger.info('worker closed forcefully due to SIGINT.');\n process.exit(130); // SIGINT exit code\n });\n if (args.production) {\n await worker.drain();\n }\n await worker.close();\n logger.info('worker closed due to SIGINT.');\n process.exit(130); // SIGINT exit code\n });\n\n process.once('SIGTERM', async () => {\n logger.info('SIGTERM received in CLI.');\n if (args.production) {\n await worker.drain();\n }\n await worker.close();\n logger.info('worker closed due to SIGTERM.');\n process.exit(143); // SIGTERM exit code\n });\n\n try {\n await worker.run();\n } catch {\n logger.fatal('closing worker due to error.');\n process.exit(1);\n }\n};\n\n/**\n * Exposes a CLI for creating a new worker, in development or production mode.\n *\n * @param opts - Options to launch the worker with\n * @example\n * ```\n * if (process.argv[1] === fileURLToPath(import.meta.url)) {\n * cli.runApp(new WorkerOptions({ agent: import.meta.filename }));\n * }\n * ```\n */\nexport const runApp = (opts: WorkerOptions) => {\n const program = new Command()\n .name('agents')\n .description('LiveKit Agents CLI')\n .version(version)\n .addOption(\n new Option('--log-level <level>', 'Set the logging level')\n .choices(['trace', 'debug', 'info', 'warn', 'error', 'fatal'])\n .default('info')\n .env('LOG_LEVEL'),\n )\n .addOption(\n new Option('--url <string>', 'LiveKit server or Cloud project websocket URL').env(\n 'LIVEKIT_URL',\n ),\n )\n .addOption(\n new Option('--api-key <string>', \"LiveKit server or Cloud project's API key\").env(\n 'LIVEKIT_API_KEY',\n ),\n )\n .addOption(\n new Option('--api-secret <string>', \"LiveKit server or Cloud project's API secret\").env(\n 'LIVEKIT_API_SECRET',\n ),\n )\n .action(() => {\n if (\n // do not run CLI if origin file is agents/ipc/job_main.js\n process.argv[1] !== new URL('ipc/job_main.js', import.meta.url).pathname &&\n process.argv.length < 3\n ) {\n program.help();\n }\n });\n\n program\n .command('start')\n .description('Start the worker in production mode')\n .action(() => {\n const options = program.optsWithGlobals();\n opts.wsURL = options.url || opts.wsURL;\n opts.apiKey = options.apiKey || opts.apiKey;\n opts.apiSecret = options.apiSecret || opts.apiSecret;\n opts.logLevel = options.logLevel || opts.logLevel;\n runWorker({\n opts,\n production: true,\n watch: false,\n });\n });\n\n program\n .command('dev')\n .description('Start the worker in development mode')\n .addOption(\n new Option('--log-level <level>', 'Set the logging level')\n .choices(['trace', 'debug', 'info', 'warn', 'error', 'fatal'])\n .default('debug')\n .env('LOG_LEVEL'),\n )\n .action(() => {\n const options = program.optsWithGlobals();\n opts.wsURL = options.url || opts.wsURL;\n opts.apiKey = options.apiKey || opts.apiKey;\n opts.apiSecret = options.apiSecret || opts.apiSecret;\n opts.logLevel = options.logLevel || opts.logLevel;\n runWorker({\n opts,\n production: false,\n watch: false,\n });\n });\n\n program\n .command('connect')\n .description('Connect to a specific room')\n .requiredOption('--room <string>', 'Room name to connect to')\n .option('--participant-identity <string>', 'Identity of user to listen to')\n .addOption(\n new Option('--log-level <level>', 'Set the logging level')\n .choices(['trace', 'debug', 'info', 'warn', 'error', 'fatal'])\n .default('debug')\n .env('LOG_LEVEL'),\n )\n .action((...[, command]) => {\n const options = command.optsWithGlobals();\n opts.wsURL = options.url || opts.wsURL;\n opts.apiKey = options.apiKey || opts.apiKey;\n opts.apiSecret = options.apiSecret || opts.apiSecret;\n opts.logLevel = options.logLevel || opts.logLevel;\n runWorker({\n opts,\n production: false,\n watch: false,\n room: options.room,\n participantIdentity: options.participantIdentity,\n });\n });\n\n program.parse();\n};\n"],"mappings":"AAGA,SAAS,SAAS,cAAc;AAEhC,SAAS,kBAAkB,WAAW;AAC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xC,SAAK,QAAQ,QAAQ,OAAO,KAAK;AACjC,SAAK,SAAS,QAAQ,UAAU,KAAK;AACrC,SAAK,YAAY,QAAQ,aAAa,KAAK;AAC3C,SAAK,WAAW,QAAQ,YAAY,KAAK;AACzC,cAAU;AAAA,MACR;AAAA,MACA,YAAY;AAAA,MACZ,OAAO;AAAA,IACT,CAAC;AAAA,EACH,CAAC;AAEH,UACG,QAAQ,SAAS,EACjB,YAAY,4BAA4B,EACxC,eAAe,mBAAmB,yBAAyB,EAC3D,OAAO,mCAAmC,+BAA+B,EACzE;AAAA,IACC,IAAI,OAAO,uBAAuB,uBAAuB,EACtD,QAAQ,CAAC,SAAS,SAAS,QAAQ,QAAQ,SAAS,OAAO,CAAC,EAC5D,QAAQ,OAAO,EACf,IAAI,WAAW;AAAA,EACpB,EACC,OAAO,IAAI,CAAC,EAAE,OAAO,MAAM;AAC1B,UAAM,UAAU,QAAQ,gBAAgB;AACxC,SAAK,QAAQ,QAAQ,OAAO,KAAK;AACjC,SAAK,SAAS,QAAQ,UAAU,KAAK;AACrC,SAAK,YAAY,QAAQ,aAAa,KAAK;AAC3C,SAAK,WAAW,QAAQ,YAAY,KAAK;AACzC,cAAU;AAAA,MACR;AAAA,MACA,YAAY;AAAA,MACZ,OAAO;AAAA,MACP,MAAM,QAAQ;AAAA,MACd,qBAAqB,QAAQ;AAAA,IAC/B,CAAC;AAAA,EACH,CAAC;AAEH,UAAQ,MAAM;AAChB;","names":[]}
|
package/dist/http_server.cjs
CHANGED
|
@@ -32,12 +32,15 @@ class HTTPServer {
|
|
|
32
32
|
port;
|
|
33
33
|
app;
|
|
34
34
|
#logger = (0, import_log.log)();
|
|
35
|
-
constructor(host, port) {
|
|
35
|
+
constructor(host, port, workerListener) {
|
|
36
36
|
this.host = host;
|
|
37
37
|
this.port = port;
|
|
38
38
|
this.app = (0, import_node_http.createServer)((req, res) => {
|
|
39
39
|
if (req.url === "/") {
|
|
40
40
|
healthCheck(res);
|
|
41
|
+
} else if (req.url === "/worker") {
|
|
42
|
+
res.writeHead(200, { "Contet-Type": "application/json" });
|
|
43
|
+
res.end(JSON.stringify(workerListener()));
|
|
41
44
|
} else {
|
|
42
45
|
res.writeHead(404);
|
|
43
46
|
res.end("not found");
|
package/dist/http_server.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/http_server.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { type IncomingMessage, type Server, type ServerResponse, createServer } from 'node:http';\nimport { log } from './log.js';\n\nconst healthCheck = async (res: ServerResponse) => {\n res.writeHead(200);\n res.end('OK');\n};\n\nexport class HTTPServer {\n host: string;\n port: number;\n app: Server;\n #logger = log();\n\n constructor(host: string, port: number) {\n this.host = host;\n this.port = port;\n\n this.app = createServer((req: IncomingMessage, res: ServerResponse) => {\n if (req.url === '/') {\n healthCheck(res);\n } else {\n res.writeHead(404);\n res.end('not found');\n }\n });\n }\n\n async run(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.listen(this.port, this.host, (err?: Error) => {\n if (err) reject(err);\n const address = this.app.address();\n if (typeof address! !== 'string') {\n this.#logger.info(`Server is listening on port ${address!.port}`);\n }\n resolve();\n });\n });\n }\n\n async close(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.close((err?: Error) => {\n if (err) reject(err);\n resolve();\n });\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,uBAAqF;AACrF,iBAAoB;AAEpB,MAAM,cAAc,OAAO,QAAwB;AACjD,MAAI,UAAU,GAAG;AACjB,MAAI,IAAI,IAAI;AACd;
|
|
1
|
+
{"version":3,"sources":["../src/http_server.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { type IncomingMessage, type Server, type ServerResponse, createServer } from 'node:http';\nimport { log } from './log.js';\n\nconst healthCheck = async (res: ServerResponse) => {\n res.writeHead(200);\n res.end('OK');\n};\n\ninterface WorkerResponse {\n agent_name: string;\n worker_type: string;\n active_jobs: number;\n}\n\nexport class HTTPServer {\n host: string;\n port: number;\n app: Server;\n #logger = log();\n\n constructor(host: string, port: number, workerListener: () => WorkerResponse) {\n this.host = host;\n this.port = port;\n\n this.app = createServer((req: IncomingMessage, res: ServerResponse) => {\n if (req.url === '/') {\n healthCheck(res);\n } else if (req.url === '/worker') {\n res.writeHead(200, { 'Contet-Type': 'application/json' });\n res.end(JSON.stringify(workerListener()));\n } else {\n res.writeHead(404);\n res.end('not found');\n }\n });\n }\n\n async run(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.listen(this.port, this.host, (err?: Error) => {\n if (err) reject(err);\n const address = this.app.address();\n if (typeof address! !== 'string') {\n this.#logger.info(`Server is listening on port ${address!.port}`);\n }\n resolve();\n });\n });\n }\n\n async close(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.close((err?: Error) => {\n if (err) reject(err);\n resolve();\n });\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,uBAAqF;AACrF,iBAAoB;AAEpB,MAAM,cAAc,OAAO,QAAwB;AACjD,MAAI,UAAU,GAAG;AACjB,MAAI,IAAI,IAAI;AACd;AAQO,MAAM,WAAW;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA,cAAU,gBAAI;AAAA,EAEd,YAAY,MAAc,MAAc,gBAAsC;AAC5E,SAAK,OAAO;AACZ,SAAK,OAAO;AAEZ,SAAK,UAAM,+BAAa,CAAC,KAAsB,QAAwB;AACrE,UAAI,IAAI,QAAQ,KAAK;AACnB,oBAAY,GAAG;AAAA,MACjB,WAAW,IAAI,QAAQ,WAAW;AAChC,YAAI,UAAU,KAAK,EAAE,eAAe,mBAAmB,CAAC;AACxD,YAAI,IAAI,KAAK,UAAU,eAAe,CAAC,CAAC;AAAA,MAC1C,OAAO;AACL,YAAI,UAAU,GAAG;AACjB,YAAI,IAAI,WAAW;AAAA,MACrB;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,MAAqB;AACzB,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,WAAK,IAAI,OAAO,KAAK,MAAM,KAAK,MAAM,CAAC,QAAgB;AACrD,YAAI,IAAK,QAAO,GAAG;AACnB,cAAM,UAAU,KAAK,IAAI,QAAQ;AACjC,YAAI,OAAO,YAAa,UAAU;AAChC,eAAK,QAAQ,KAAK,+BAA+B,QAAS,IAAI,EAAE;AAAA,QAClE;AACA,gBAAQ;AAAA,MACV,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,QAAuB;AAC3B,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,WAAK,IAAI,MAAM,CAAC,QAAgB;AAC9B,YAAI,IAAK,QAAO,GAAG;AACnB,gBAAQ;AAAA,MACV,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;","names":[]}
|
package/dist/http_server.d.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
import { type Server } from 'node:http';
|
|
3
|
+
interface WorkerResponse {
|
|
4
|
+
agent_name: string;
|
|
5
|
+
worker_type: string;
|
|
6
|
+
active_jobs: number;
|
|
7
|
+
}
|
|
3
8
|
export declare class HTTPServer {
|
|
4
9
|
#private;
|
|
5
10
|
host: string;
|
|
6
11
|
port: number;
|
|
7
12
|
app: Server;
|
|
8
|
-
constructor(host: string, port: number);
|
|
13
|
+
constructor(host: string, port: number, workerListener: () => WorkerResponse);
|
|
9
14
|
run(): Promise<void>;
|
|
10
15
|
close(): Promise<void>;
|
|
11
16
|
}
|
|
17
|
+
export {};
|
|
12
18
|
//# sourceMappingURL=http_server.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http_server.d.ts","sourceRoot":"","sources":["../src/http_server.ts"],"names":[],"mappings":";AAGA,OAAO,EAAwB,KAAK,MAAM,EAAqC,MAAM,WAAW,CAAC;AAQjG,qBAAa,UAAU;;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;gBAGA,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;
|
|
1
|
+
{"version":3,"file":"http_server.d.ts","sourceRoot":"","sources":["../src/http_server.ts"],"names":[],"mappings":";AAGA,OAAO,EAAwB,KAAK,MAAM,EAAqC,MAAM,WAAW,CAAC;AAQjG,UAAU,cAAc;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,qBAAa,UAAU;;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;gBAGA,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,cAAc;IAiBtE,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;IAapB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAQ7B"}
|
package/dist/http_server.js
CHANGED
|
@@ -9,12 +9,15 @@ class HTTPServer {
|
|
|
9
9
|
port;
|
|
10
10
|
app;
|
|
11
11
|
#logger = log();
|
|
12
|
-
constructor(host, port) {
|
|
12
|
+
constructor(host, port, workerListener) {
|
|
13
13
|
this.host = host;
|
|
14
14
|
this.port = port;
|
|
15
15
|
this.app = createServer((req, res) => {
|
|
16
16
|
if (req.url === "/") {
|
|
17
17
|
healthCheck(res);
|
|
18
|
+
} else if (req.url === "/worker") {
|
|
19
|
+
res.writeHead(200, { "Contet-Type": "application/json" });
|
|
20
|
+
res.end(JSON.stringify(workerListener()));
|
|
18
21
|
} else {
|
|
19
22
|
res.writeHead(404);
|
|
20
23
|
res.end("not found");
|
package/dist/http_server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/http_server.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { type IncomingMessage, type Server, type ServerResponse, createServer } from 'node:http';\nimport { log } from './log.js';\n\nconst healthCheck = async (res: ServerResponse) => {\n res.writeHead(200);\n res.end('OK');\n};\n\nexport class HTTPServer {\n host: string;\n port: number;\n app: Server;\n #logger = log();\n\n constructor(host: string, port: number) {\n this.host = host;\n this.port = port;\n\n this.app = createServer((req: IncomingMessage, res: ServerResponse) => {\n if (req.url === '/') {\n healthCheck(res);\n } else {\n res.writeHead(404);\n res.end('not found');\n }\n });\n }\n\n async run(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.listen(this.port, this.host, (err?: Error) => {\n if (err) reject(err);\n const address = this.app.address();\n if (typeof address! !== 'string') {\n this.#logger.info(`Server is listening on port ${address!.port}`);\n }\n resolve();\n });\n });\n }\n\n async close(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.close((err?: Error) => {\n if (err) reject(err);\n resolve();\n });\n });\n }\n}\n"],"mappings":"AAGA,SAAiE,oBAAoB;AACrF,SAAS,WAAW;AAEpB,MAAM,cAAc,OAAO,QAAwB;AACjD,MAAI,UAAU,GAAG;AACjB,MAAI,IAAI,IAAI;AACd;
|
|
1
|
+
{"version":3,"sources":["../src/http_server.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { type IncomingMessage, type Server, type ServerResponse, createServer } from 'node:http';\nimport { log } from './log.js';\n\nconst healthCheck = async (res: ServerResponse) => {\n res.writeHead(200);\n res.end('OK');\n};\n\ninterface WorkerResponse {\n agent_name: string;\n worker_type: string;\n active_jobs: number;\n}\n\nexport class HTTPServer {\n host: string;\n port: number;\n app: Server;\n #logger = log();\n\n constructor(host: string, port: number, workerListener: () => WorkerResponse) {\n this.host = host;\n this.port = port;\n\n this.app = createServer((req: IncomingMessage, res: ServerResponse) => {\n if (req.url === '/') {\n healthCheck(res);\n } else if (req.url === '/worker') {\n res.writeHead(200, { 'Contet-Type': 'application/json' });\n res.end(JSON.stringify(workerListener()));\n } else {\n res.writeHead(404);\n res.end('not found');\n }\n });\n }\n\n async run(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.listen(this.port, this.host, (err?: Error) => {\n if (err) reject(err);\n const address = this.app.address();\n if (typeof address! !== 'string') {\n this.#logger.info(`Server is listening on port ${address!.port}`);\n }\n resolve();\n });\n });\n }\n\n async close(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.close((err?: Error) => {\n if (err) reject(err);\n resolve();\n });\n });\n }\n}\n"],"mappings":"AAGA,SAAiE,oBAAoB;AACrF,SAAS,WAAW;AAEpB,MAAM,cAAc,OAAO,QAAwB;AACjD,MAAI,UAAU,GAAG;AACjB,MAAI,IAAI,IAAI;AACd;AAQO,MAAM,WAAW;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA,UAAU,IAAI;AAAA,EAEd,YAAY,MAAc,MAAc,gBAAsC;AAC5E,SAAK,OAAO;AACZ,SAAK,OAAO;AAEZ,SAAK,MAAM,aAAa,CAAC,KAAsB,QAAwB;AACrE,UAAI,IAAI,QAAQ,KAAK;AACnB,oBAAY,GAAG;AAAA,MACjB,WAAW,IAAI,QAAQ,WAAW;AAChC,YAAI,UAAU,KAAK,EAAE,eAAe,mBAAmB,CAAC;AACxD,YAAI,IAAI,KAAK,UAAU,eAAe,CAAC,CAAC;AAAA,MAC1C,OAAO;AACL,YAAI,UAAU,GAAG;AACjB,YAAI,IAAI,WAAW;AAAA,MACrB;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,MAAqB;AACzB,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,WAAK,IAAI,OAAO,KAAK,MAAM,KAAK,MAAM,CAAC,QAAgB;AACrD,YAAI,IAAK,QAAO,GAAG;AACnB,cAAM,UAAU,KAAK,IAAI,QAAQ;AACjC,YAAI,OAAO,YAAa,UAAU;AAChC,eAAK,QAAQ,KAAK,+BAA+B,QAAS,IAAI,EAAE;AAAA,QAClE;AACA,gBAAQ;AAAA,MACV,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,QAAuB;AAC3B,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,WAAK,IAAI,MAAM,CAAC,QAAgB;AAC9B,YAAI,IAAK,QAAO,GAAG;AACnB,gBAAQ;AAAA,MACV,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;","names":[]}
|
|
@@ -27,6 +27,10 @@ const ORPHANED_TIMEOUT = 15 * 1e3;
|
|
|
27
27
|
(async () => {
|
|
28
28
|
if (process.send) {
|
|
29
29
|
process.on("SIGINT", () => {
|
|
30
|
+
logger.info("SIGINT received in inference proc");
|
|
31
|
+
});
|
|
32
|
+
process.on("SIGTERM", () => {
|
|
33
|
+
logger.info("SIGTERM received in inference proc");
|
|
30
34
|
});
|
|
31
35
|
await (0, import_node_events.once)(process, "message").then(([msg]) => {
|
|
32
36
|
msg = msg;
|
|
@@ -51,7 +55,7 @@ const ORPHANED_TIMEOUT = 15 * 1e3;
|
|
|
51
55
|
process.send({ case: "initializeResponse" });
|
|
52
56
|
const closeEvent = new import_node_events.default();
|
|
53
57
|
const orphanedTimeout = setTimeout(() => {
|
|
54
|
-
logger.warn("process orphaned, shutting down");
|
|
58
|
+
logger.warn("inference process orphaned, shutting down.");
|
|
55
59
|
process.exit();
|
|
56
60
|
}, ORPHANED_TIMEOUT);
|
|
57
61
|
const handleInferenceRequest = async ({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ipc/inference_proc_lazy_main.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport EventEmitter, { once } from 'node:events';\nimport type { InferenceRunner } from '../inference_runner.js';\nimport { initializeLogger, log } from '../log.js';\nimport type { IPCMessage } from './message.js';\n\nconst ORPHANED_TIMEOUT = 15 * 1000;\n\n(async () => {\n if (process.send) {\n // don't do anything on C-c\n // this is handled in cli, triggering a termination of all child processes at once.\n process.on('SIGINT', () => {});\n\n await once(process, 'message').then(([msg]: IPCMessage[]) => {\n msg = msg!;\n if (msg.case !== 'initializeRequest') {\n throw new Error('first message must be InitializeRequest');\n }\n initializeLogger(msg.value.loggerOptions);\n });\n const logger = log().child({ pid: process.pid });\n\n const runners: { [id: string]: InferenceRunner } = await Promise.all(\n Object.entries(JSON.parse(process.argv[2]!)).map(async ([k, v]) => {\n return [k, await import(v as string).then((m) => new m.default())];\n }),\n ).then(Object.fromEntries);\n\n await Promise.all(\n Object.entries(runners).map(async ([runner, v]) => {\n logger.child({ runner }).debug('initializing inference runner');\n await v.initialize();\n }),\n );\n logger.debug('all inference runners initialized');\n process.send({ case: 'initializeResponse' });\n\n const closeEvent = new EventEmitter();\n\n const orphanedTimeout = setTimeout(() => {\n logger.warn('process orphaned, shutting down');\n process.exit();\n }, ORPHANED_TIMEOUT);\n\n const handleInferenceRequest = async ({\n method,\n requestId,\n data,\n }: {\n method: string;\n requestId: string;\n data: unknown;\n }) => {\n if (!runners[method]) {\n logger.child({ method }).warn('unknown inference method');\n }\n\n try {\n const resp = await runners[method]!.run(data);\n process.send!({ case: 'inferenceResponse', value: { requestId, data: resp } });\n } catch (error) {\n process.send!({ case: 'inferenceResponse', value: { requestId, error } });\n }\n };\n\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pingRequest':\n orphanedTimeout.refresh();\n process.send!({\n case: 'pongResponse',\n value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },\n });\n break;\n case 'shutdownRequest':\n closeEvent.emit('close');\n break;\n case 'inferenceRequest':\n handleInferenceRequest(msg.value);\n }\n });\n }\n})();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAGA,yBAAmC;AAEnC,iBAAsC;AAGtC,MAAM,mBAAmB,KAAK;AAAA,CAE7B,YAAY;AACX,MAAI,QAAQ,MAAM;AAGhB,YAAQ,GAAG,UAAU,MAAM;AAAA,
|
|
1
|
+
{"version":3,"sources":["../../src/ipc/inference_proc_lazy_main.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport EventEmitter, { once } from 'node:events';\nimport type { InferenceRunner } from '../inference_runner.js';\nimport { initializeLogger, log } from '../log.js';\nimport type { IPCMessage } from './message.js';\n\nconst ORPHANED_TIMEOUT = 15 * 1000;\n\n(async () => {\n if (process.send) {\n // don't do anything on C-c\n // this is handled in cli, triggering a termination of all child processes at once.\n process.on('SIGINT', () => {\n logger.info('SIGINT received in inference proc');\n });\n\n // don't do anything on SIGTERM\n // Render uses SIGTERM in autoscale, this ensures the processes are properly drained if needed\n process.on('SIGTERM', () => {\n logger.info('SIGTERM received in inference proc');\n });\n\n await once(process, 'message').then(([msg]: IPCMessage[]) => {\n msg = msg!;\n if (msg.case !== 'initializeRequest') {\n throw new Error('first message must be InitializeRequest');\n }\n initializeLogger(msg.value.loggerOptions);\n });\n const logger = log().child({ pid: process.pid });\n\n const runners: { [id: string]: InferenceRunner } = await Promise.all(\n Object.entries(JSON.parse(process.argv[2]!)).map(async ([k, v]) => {\n return [k, await import(v as string).then((m) => new m.default())];\n }),\n ).then(Object.fromEntries);\n\n await Promise.all(\n Object.entries(runners).map(async ([runner, v]) => {\n logger.child({ runner }).debug('initializing inference runner');\n await v.initialize();\n }),\n );\n logger.debug('all inference runners initialized');\n process.send({ case: 'initializeResponse' });\n\n const closeEvent = new EventEmitter();\n\n const orphanedTimeout = setTimeout(() => {\n logger.warn('inference process orphaned, shutting down.');\n process.exit();\n }, ORPHANED_TIMEOUT);\n\n const handleInferenceRequest = async ({\n method,\n requestId,\n data,\n }: {\n method: string;\n requestId: string;\n data: unknown;\n }) => {\n if (!runners[method]) {\n logger.child({ method }).warn('unknown inference method');\n }\n\n try {\n const resp = await runners[method]!.run(data);\n process.send!({ case: 'inferenceResponse', value: { requestId, data: resp } });\n } catch (error) {\n process.send!({ case: 'inferenceResponse', value: { requestId, error } });\n }\n };\n\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pingRequest':\n orphanedTimeout.refresh();\n process.send!({\n case: 'pongResponse',\n value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },\n });\n break;\n case 'shutdownRequest':\n closeEvent.emit('close');\n break;\n case 'inferenceRequest':\n handleInferenceRequest(msg.value);\n }\n });\n }\n})();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAGA,yBAAmC;AAEnC,iBAAsC;AAGtC,MAAM,mBAAmB,KAAK;AAAA,CAE7B,YAAY;AACX,MAAI,QAAQ,MAAM;AAGhB,YAAQ,GAAG,UAAU,MAAM;AACzB,aAAO,KAAK,mCAAmC;AAAA,IACjD,CAAC;AAID,YAAQ,GAAG,WAAW,MAAM;AAC1B,aAAO,KAAK,oCAAoC;AAAA,IAClD,CAAC;AAED,cAAM,yBAAK,SAAS,SAAS,EAAE,KAAK,CAAC,CAAC,GAAG,MAAoB;AAC3D,YAAM;AACN,UAAI,IAAI,SAAS,qBAAqB;AACpC,cAAM,IAAI,MAAM,yCAAyC;AAAA,MAC3D;AACA,uCAAiB,IAAI,MAAM,aAAa;AAAA,IAC1C,CAAC;AACD,UAAM,aAAS,gBAAI,EAAE,MAAM,EAAE,KAAK,QAAQ,IAAI,CAAC;AAE/C,UAAM,UAA6C,MAAM,QAAQ;AAAA,MAC/D,OAAO,QAAQ,KAAK,MAAM,QAAQ,KAAK,CAAC,CAAE,CAAC,EAAE,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM;AACjE,eAAO,CAAC,GAAG,MAAM,OAAO,GAAa,KAAK,CAAC,MAAM,IAAI,EAAE,QAAQ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AA,QACF,KAAK;AACH,iCAAuB,IAAI,KAAK;AAAA,MACpC;AAAA,IACF,CAAC;AAAA,EACH;AACF,GAAG;","names":["EventEmitter"]}
|
|
@@ -4,6 +4,10 @@ const ORPHANED_TIMEOUT = 15 * 1e3;
|
|
|
4
4
|
(async () => {
|
|
5
5
|
if (process.send) {
|
|
6
6
|
process.on("SIGINT", () => {
|
|
7
|
+
logger.info("SIGINT received in inference proc");
|
|
8
|
+
});
|
|
9
|
+
process.on("SIGTERM", () => {
|
|
10
|
+
logger.info("SIGTERM received in inference proc");
|
|
7
11
|
});
|
|
8
12
|
await once(process, "message").then(([msg]) => {
|
|
9
13
|
msg = msg;
|
|
@@ -28,7 +32,7 @@ const ORPHANED_TIMEOUT = 15 * 1e3;
|
|
|
28
32
|
process.send({ case: "initializeResponse" });
|
|
29
33
|
const closeEvent = new EventEmitter();
|
|
30
34
|
const orphanedTimeout = setTimeout(() => {
|
|
31
|
-
logger.warn("process orphaned, shutting down");
|
|
35
|
+
logger.warn("inference process orphaned, shutting down.");
|
|
32
36
|
process.exit();
|
|
33
37
|
}, ORPHANED_TIMEOUT);
|
|
34
38
|
const handleInferenceRequest = async ({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ipc/inference_proc_lazy_main.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport EventEmitter, { once } from 'node:events';\nimport type { InferenceRunner } from '../inference_runner.js';\nimport { initializeLogger, log } from '../log.js';\nimport type { IPCMessage } from './message.js';\n\nconst ORPHANED_TIMEOUT = 15 * 1000;\n\n(async () => {\n if (process.send) {\n // don't do anything on C-c\n // this is handled in cli, triggering a termination of all child processes at once.\n process.on('SIGINT', () => {});\n\n await once(process, 'message').then(([msg]: IPCMessage[]) => {\n msg = msg!;\n if (msg.case !== 'initializeRequest') {\n throw new Error('first message must be InitializeRequest');\n }\n initializeLogger(msg.value.loggerOptions);\n });\n const logger = log().child({ pid: process.pid });\n\n const runners: { [id: string]: InferenceRunner } = await Promise.all(\n Object.entries(JSON.parse(process.argv[2]!)).map(async ([k, v]) => {\n return [k, await import(v as string).then((m) => new m.default())];\n }),\n ).then(Object.fromEntries);\n\n await Promise.all(\n Object.entries(runners).map(async ([runner, v]) => {\n logger.child({ runner }).debug('initializing inference runner');\n await v.initialize();\n }),\n );\n logger.debug('all inference runners initialized');\n process.send({ case: 'initializeResponse' });\n\n const closeEvent = new EventEmitter();\n\n const orphanedTimeout = setTimeout(() => {\n logger.warn('process orphaned, shutting down');\n process.exit();\n }, ORPHANED_TIMEOUT);\n\n const handleInferenceRequest = async ({\n method,\n requestId,\n data,\n }: {\n method: string;\n requestId: string;\n data: unknown;\n }) => {\n if (!runners[method]) {\n logger.child({ method }).warn('unknown inference method');\n }\n\n try {\n const resp = await runners[method]!.run(data);\n process.send!({ case: 'inferenceResponse', value: { requestId, data: resp } });\n } catch (error) {\n process.send!({ case: 'inferenceResponse', value: { requestId, error } });\n }\n };\n\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pingRequest':\n orphanedTimeout.refresh();\n process.send!({\n case: 'pongResponse',\n value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },\n });\n break;\n case 'shutdownRequest':\n closeEvent.emit('close');\n break;\n case 'inferenceRequest':\n handleInferenceRequest(msg.value);\n }\n });\n }\n})();\n"],"mappings":"AAGA,OAAO,gBAAgB,YAAY;AAEnC,SAAS,kBAAkB,WAAW;AAGtC,MAAM,mBAAmB,KAAK;AAAA,CAE7B,YAAY;AACX,MAAI,QAAQ,MAAM;AAGhB,YAAQ,GAAG,UAAU,MAAM;AAAA,
|
|
1
|
+
{"version":3,"sources":["../../src/ipc/inference_proc_lazy_main.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport EventEmitter, { once } from 'node:events';\nimport type { InferenceRunner } from '../inference_runner.js';\nimport { initializeLogger, log } from '../log.js';\nimport type { IPCMessage } from './message.js';\n\nconst ORPHANED_TIMEOUT = 15 * 1000;\n\n(async () => {\n if (process.send) {\n // don't do anything on C-c\n // this is handled in cli, triggering a termination of all child processes at once.\n process.on('SIGINT', () => {\n logger.info('SIGINT received in inference proc');\n });\n\n // don't do anything on SIGTERM\n // Render uses SIGTERM in autoscale, this ensures the processes are properly drained if needed\n process.on('SIGTERM', () => {\n logger.info('SIGTERM received in inference proc');\n });\n\n await once(process, 'message').then(([msg]: IPCMessage[]) => {\n msg = msg!;\n if (msg.case !== 'initializeRequest') {\n throw new Error('first message must be InitializeRequest');\n }\n initializeLogger(msg.value.loggerOptions);\n });\n const logger = log().child({ pid: process.pid });\n\n const runners: { [id: string]: InferenceRunner } = await Promise.all(\n Object.entries(JSON.parse(process.argv[2]!)).map(async ([k, v]) => {\n return [k, await import(v as string).then((m) => new m.default())];\n }),\n ).then(Object.fromEntries);\n\n await Promise.all(\n Object.entries(runners).map(async ([runner, v]) => {\n logger.child({ runner }).debug('initializing inference runner');\n await v.initialize();\n }),\n );\n logger.debug('all inference runners initialized');\n process.send({ case: 'initializeResponse' });\n\n const closeEvent = new EventEmitter();\n\n const orphanedTimeout = setTimeout(() => {\n logger.warn('inference process orphaned, shutting down.');\n process.exit();\n }, ORPHANED_TIMEOUT);\n\n const handleInferenceRequest = async ({\n method,\n requestId,\n data,\n }: {\n method: string;\n requestId: string;\n data: unknown;\n }) => {\n if (!runners[method]) {\n logger.child({ method }).warn('unknown inference method');\n }\n\n try {\n const resp = await runners[method]!.run(data);\n process.send!({ case: 'inferenceResponse', value: { requestId, data: resp } });\n } catch (error) {\n process.send!({ case: 'inferenceResponse', value: { requestId, error } });\n }\n };\n\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pingRequest':\n orphanedTimeout.refresh();\n process.send!({\n case: 'pongResponse',\n value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },\n });\n break;\n case 'shutdownRequest':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;AAEpC,UAAM,kBAAkB,WAAW,MAAM;AACvC,aAAO,KAAK,4CAA4C;AACxD,cAAQ,KAAK;AAAA,IACf,GAAG,gBAAgB;AAEnB,UAAM,yBAAyB,OAAO;AAAA,MACpC;AAAA,MACA;AAAA,MACA;AAAA,IACF,MAIM;AACJ,UAAI,CAAC,QAAQ,MAAM,GAAG;AACpB,eAAO,MAAM,EAAE,OAAO,CAAC,EAAE,KAAK,0BAA0B;AAAA,MAC1D;AAEA,UAAI;AACF,cAAM,OAAO,MAAM,QAAQ,MAAM,EAAG,IAAI,IAAI;AAC5C,gBAAQ,KAAM,EAAE,MAAM,qBAAqB,OAAO,EAAE,WAAW,MAAM,KAAK,EAAE,CAAC;AAAA,MAC/E,SAAS,OAAO;AACd,gBAAQ,KAAM,EAAE,MAAM,qBAAqB,OAAO,EAAE,WAAW,MAAM,EAAE,CAAC;AAAA,MAC1E;AAAA,IACF;AAEA,YAAQ,GAAG,WAAW,CAAC,QAAoB;AACzC,cAAQ,IAAI,MAAM;AAAA,QAChB,KAAK;AACH,0BAAgB,QAAQ;AACxB,kBAAQ,KAAM;AAAA,YACZ,MAAM;AAAA,YACN,OAAO,EAAE,eAAe,IAAI,MAAM,WAAW,WAAW,KAAK,IAAI,EAAE;AAAA,UACrE,CAAC;AACD;AAAA,QACF,KAAK;AACH,qBAAW,KAAK,OAAO;AACvB;AAAA,QACF,KAAK;AACH,iCAAuB,IAAI,KAAK;AAAA,MACpC;AAAA,IACF,CAAC;AAAA,EACH;AACF,GAAG;","names":[]}
|
|
@@ -82,6 +82,7 @@ const startJob = (proc, func, info, closeEvent, logger) => {
|
|
|
82
82
|
}
|
|
83
83
|
await Promise.all(shutdownTasks).catch(() => logger.error("error while shutting down the job"));
|
|
84
84
|
process.send({ case: "done" });
|
|
85
|
+
logger.info("job completed.");
|
|
85
86
|
process.exit();
|
|
86
87
|
});
|
|
87
88
|
return { ctx, task };
|
|
@@ -100,6 +101,10 @@ const startJob = (proc, func, info, closeEvent, logger) => {
|
|
|
100
101
|
agent.prewarm = import_worker.defaultInitializeProcessFunc;
|
|
101
102
|
}
|
|
102
103
|
process.on("SIGINT", () => {
|
|
104
|
+
logger.info("SIGINT received in job proc");
|
|
105
|
+
});
|
|
106
|
+
process.on("SIGTERM", () => {
|
|
107
|
+
logger.info("SIGTERM received in job proc");
|
|
103
108
|
});
|
|
104
109
|
await (0, import_node_events.once)(process, "message").then(([msg]) => {
|
|
105
110
|
msg = msg;
|
|
@@ -110,6 +115,9 @@ const startJob = (proc, func, info, closeEvent, logger) => {
|
|
|
110
115
|
});
|
|
111
116
|
const proc = new import_job.JobProcess();
|
|
112
117
|
let logger = (0, import_log.log)().child({ pid: proc.pid });
|
|
118
|
+
process.on("unhandledRejection", (reason) => {
|
|
119
|
+
logger.error(reason);
|
|
120
|
+
});
|
|
113
121
|
logger.debug("initializing job runner");
|
|
114
122
|
agent.prewarm(proc);
|
|
115
123
|
logger.debug("job runner initialized");
|
|
@@ -117,7 +125,7 @@ const startJob = (proc, func, info, closeEvent, logger) => {
|
|
|
117
125
|
let job = void 0;
|
|
118
126
|
const closeEvent = new import_node_events.EventEmitter();
|
|
119
127
|
const orphanedTimeout = setTimeout(() => {
|
|
120
|
-
logger.warn("process orphaned, shutting down");
|
|
128
|
+
logger.warn("job process orphaned, shutting down.");
|
|
121
129
|
process.exit();
|
|
122
130
|
}, ORPHANED_TIMEOUT);
|
|
123
131
|
process.on("message", (msg) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ipc/job_proc_lazy_main.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Room, RoomEvent } from '@livekit/rtc-node';\nimport { randomUUID } from 'node:crypto';\nimport { EventEmitter, once } from 'node:events';\nimport { pathToFileURL } from 'node:url';\nimport type { Logger } from 'pino';\nimport { type Agent, isAgent } from '../generator.js';\nimport { CurrentJobContext, JobContext, JobProcess, type RunningJobInfo } from '../job.js';\nimport { initializeLogger, log } from '../log.js';\nimport { defaultInitializeProcessFunc } from '../worker.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { IPCMessage } from './message.js';\n\nconst ORPHANED_TIMEOUT = 15 * 1000;\n\ntype JobTask = {\n ctx: JobContext;\n task: Promise<void>;\n};\n\nclass PendingInference {\n promise = new Promise<{ requestId: string; data: unknown; error?: Error }>((resolve) => {\n this.resolve = resolve; // this is how JavaScript lets you resolve promises externally\n });\n resolve(arg: { requestId: string; data: unknown; error?: Error }) {\n arg; // useless call to counteract TypeScript E6133\n }\n}\n\nclass InfClient implements InferenceExecutor {\n #requests: { [id: string]: PendingInference } = {};\n\n constructor() {\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'inferenceResponse':\n const fut = this.#requests[msg.value.requestId];\n delete this.#requests[msg.value.requestId];\n if (!fut) {\n log().child({ resp: msg.value }).warn('received unexpected inference response');\n return;\n }\n fut.resolve(msg.value);\n break;\n }\n });\n }\n\n async doInference(method: string, data: unknown): Promise<unknown> {\n const requestId = 'inference_job_' + randomUUID;\n process.send!({ case: 'inferenceRequest', value: { requestId, method, data } });\n this.#requests[requestId] = new PendingInference();\n const resp = await this.#requests[requestId]!.promise;\n if (resp.error) {\n throw new Error(`inference of ${method} failed: ${resp.error.message}`);\n }\n return resp.data;\n }\n}\n\nconst startJob = (\n proc: JobProcess,\n func: (ctx: JobContext) => Promise<void>,\n info: RunningJobInfo,\n closeEvent: EventEmitter,\n logger: Logger,\n): JobTask => {\n let connect = false;\n let shutdown = false;\n\n const room = new Room();\n room.on(RoomEvent.Disconnected, () => {\n closeEvent.emit('close', false);\n });\n\n const onConnect = () => {\n connect = true;\n };\n const onShutdown = (reason: string) => {\n shutdown = true;\n closeEvent.emit('close', true, reason);\n };\n\n const ctx = new JobContext(proc, info, room, onConnect, onShutdown, new InfClient());\n new CurrentJobContext(ctx);\n\n const task = new Promise<void>(async () => {\n const unconnectedTimeout = setTimeout(() => {\n if (!(connect || shutdown)) {\n logger.warn(\n 'room not connect after job_entry was called after 10 seconds, ',\n 'did you forget to call ctx.connect()?',\n );\n }\n }, 10000);\n func(ctx).finally(() => clearTimeout(unconnectedTimeout));\n\n await once(closeEvent, 'close').then((close) => {\n logger.debug('shutting down');\n process.send!({ case: 'exiting', value: { reason: close[1] } });\n });\n\n await room.disconnect();\n logger.debug('disconnected from room');\n\n const shutdownTasks = [];\n for (const callback of ctx.shutdownCallbacks) {\n shutdownTasks.push(callback());\n }\n await Promise.all(shutdownTasks).catch(() => logger.error('error while shutting down the job'));\n\n process.send!({ case: 'done' });\n process.exit();\n });\n\n return { ctx, task };\n};\n\n(async () => {\n if (process.send) {\n // process.argv:\n // [0] `node'\n // [1] import.meta.filename\n // [2] import.meta.filename of function containing entry file\n const moduleFile = process.argv[2];\n const agent: Agent = await import(pathToFileURL(moduleFile!).pathname).then((module) => {\n const agent = module.default;\n if (agent === undefined || !isAgent(agent)) {\n throw new Error(`Unable to load agent: Missing or invalid default export in ${moduleFile}`);\n }\n return agent;\n });\n if (!agent.prewarm) {\n agent.prewarm = defaultInitializeProcessFunc;\n }\n\n // don't do anything on C-c\n // this is handled in cli, triggering a termination of all child processes at once.\n process.on('SIGINT', () => {});\n\n await once(process, 'message').then(([msg]: IPCMessage[]) => {\n msg = msg!;\n if (msg.case !== 'initializeRequest') {\n throw new Error('first message must be InitializeRequest');\n }\n initializeLogger(msg.value.loggerOptions);\n });\n const proc = new JobProcess();\n let logger = log().child({ pid: proc.pid });\n\n logger.debug('initializing job runner');\n agent.prewarm(proc);\n logger.debug('job runner initialized');\n process.send({ case: 'initializeResponse' });\n\n let job: JobTask | undefined = undefined;\n const closeEvent = new EventEmitter();\n\n const orphanedTimeout = setTimeout(() => {\n logger.warn('process orphaned, shutting down');\n process.exit();\n }, ORPHANED_TIMEOUT);\n\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pingRequest': {\n orphanedTimeout.refresh();\n process.send!({\n case: 'pongResponse',\n value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },\n });\n break;\n }\n case 'startJobRequest': {\n if (job) {\n throw new Error('job task already running');\n }\n\n logger = logger.child({ jobID: msg.value.runningJob.job.id });\n\n job = startJob(proc, agent.entry, msg.value.runningJob, closeEvent, logger);\n logger.debug('job started');\n break;\n }\n case 'shutdownRequest': {\n if (!job) {\n break;\n }\n closeEvent.emit('close', '');\n }\n }\n });\n }\n})();\n"],"mappings":";AAGA,sBAAgC;AAChC,yBAA2B;AAC3B,yBAAmC;AACnC,sBAA8B;AAE9B,uBAAoC;AACpC,iBAA+E;AAC/E,iBAAsC;AACtC,oBAA6C;AAI7C,MAAM,mBAAmB,KAAK;AAO9B,MAAM,iBAAiB;AAAA,EACrB,UAAU,IAAI,QAA6D,CAAC,YAAY;AACtF,SAAK,UAAU;AAAA,EACjB,CAAC;AAAA,EACD,QAAQ,KAA0D;AAChE;AAAA,EACF;AACF;AAEA,MAAM,UAAuC;AAAA,EAC3C,YAAgD,CAAC;AAAA,EAEjD,cAAc;AACZ,YAAQ,GAAG,WAAW,CAAC,QAAoB;AACzC,cAAQ,IAAI,MAAM;AAAA,QAChB,KAAK;AACH,gBAAM,MAAM,KAAK,UAAU,IAAI,MAAM,SAAS;AAC9C,iBAAO,KAAK,UAAU,IAAI,MAAM,SAAS;AACzC,cAAI,CAAC,KAAK;AACR,gCAAI,EAAE,MAAM,EAAE,MAAM,IAAI,MAAM,CAAC,EAAE,KAAK,wCAAwC;AAC9E;AAAA,UACF;AACA,cAAI,QAAQ,IAAI,KAAK;AACrB;AAAA,MACJ;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,YAAY,QAAgB,MAAiC;AACjE,UAAM,YAAY,mBAAmB;AACrC,YAAQ,KAAM,EAAE,MAAM,oBAAoB,OAAO,EAAE,WAAW,QAAQ,KAAK,EAAE,CAAC;AAC9E,SAAK,UAAU,SAAS,IAAI,IAAI,iBAAiB;AACjD,UAAM,OAA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;AAAA,QAChB,KAAK,eAAe;AAClB,0BAAgB,QAAQ;AACxB,kBAAQ,KAAM;AAAA,YACZ,MAAM;AAAA,YACN,OAAO,EAAE,eAAe,IAAI,MAAM,WAAW,WAAW,KAAK,IAAI,EAAE;AAAA,UACrE,CAAC;AACD;AAAA,QACF;AAAA,QACA,KAAK,mBAAmB;AACtB,cAAI,KAAK;AACP,kBAAM,IAAI,MAAM,0BAA0B;AAAA,UAC5C;AAEA,mBAAS,OAAO,MAAM,EAAE,OAAO,IAAI,MAAM,WAAW,IAAI,GAAG,CAAC;AAE5D,gBAAM,SAAS,MAAM,MAAM,OAAO,IAAI,MAAM,YAAY,YAAY,MAAM;AAC1E,iBAAO,MAAM,aAAa;AAC1B;AAAA,QACF;AAAA,QACA,KAAK,mBAAmB;AACtB,cAAI,CAAC,KAAK;AACR;AAAA,UACF;AACA,qBAAW,KAAK,SAAS,EAAE;AAAA,QAC7B;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACF,GAAG;","names":["module","agent"]}
|
|
1
|
+
{"version":3,"sources":["../../src/ipc/job_proc_lazy_main.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Room, RoomEvent } from '@livekit/rtc-node';\nimport { randomUUID } from 'node:crypto';\nimport { EventEmitter, once } from 'node:events';\nimport { pathToFileURL } from 'node:url';\nimport type { Logger } from 'pino';\nimport { type Agent, isAgent } from '../generator.js';\nimport { CurrentJobContext, JobContext, JobProcess, type RunningJobInfo } from '../job.js';\nimport { initializeLogger, log } from '../log.js';\nimport { defaultInitializeProcessFunc } from '../worker.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { IPCMessage } from './message.js';\n\nconst ORPHANED_TIMEOUT = 15 * 1000;\n\ntype JobTask = {\n ctx: JobContext;\n task: Promise<void>;\n};\n\nclass PendingInference {\n promise = new Promise<{ requestId: string; data: unknown; error?: Error }>((resolve) => {\n this.resolve = resolve; // this is how JavaScript lets you resolve promises externally\n });\n resolve(arg: { requestId: string; data: unknown; error?: Error }) {\n arg; // useless call to counteract TypeScript E6133\n }\n}\n\nclass InfClient implements InferenceExecutor {\n #requests: { [id: string]: PendingInference } = {};\n\n constructor() {\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'inferenceResponse':\n const fut = this.#requests[msg.value.requestId];\n delete this.#requests[msg.value.requestId];\n if (!fut) {\n log().child({ resp: msg.value }).warn('received unexpected inference response');\n return;\n }\n fut.resolve(msg.value);\n break;\n }\n });\n }\n\n async doInference(method: string, data: unknown): Promise<unknown> {\n const requestId = 'inference_job_' + randomUUID;\n process.send!({ case: 'inferenceRequest', value: { requestId, method, data } });\n this.#requests[requestId] = new PendingInference();\n const resp = await this.#requests[requestId]!.promise;\n if (resp.error) {\n throw new Error(`inference of ${method} failed: ${resp.error.message}`);\n }\n return resp.data;\n }\n}\n\nconst startJob = (\n proc: JobProcess,\n func: (ctx: JobContext) => Promise<void>,\n info: RunningJobInfo,\n closeEvent: EventEmitter,\n logger: Logger,\n): JobTask => {\n let connect = false;\n let shutdown = false;\n\n const room = new Room();\n room.on(RoomEvent.Disconnected, () => {\n closeEvent.emit('close', false);\n });\n\n const onConnect = () => {\n connect = true;\n };\n const onShutdown = (reason: string) => {\n shutdown = true;\n closeEvent.emit('close', true, reason);\n };\n\n const ctx = new JobContext(proc, info, room, onConnect, onShutdown, new InfClient());\n new CurrentJobContext(ctx);\n\n const task = new Promise<void>(async () => {\n const unconnectedTimeout = setTimeout(() => {\n if (!(connect || shutdown)) {\n logger.warn(\n 'room not connect after job_entry was called after 10 seconds, ',\n 'did you forget to call ctx.connect()?',\n );\n }\n }, 10000);\n func(ctx).finally(() => clearTimeout(unconnectedTimeout));\n\n await once(closeEvent, 'close').then((close) => {\n logger.debug('shutting down');\n process.send!({ case: 'exiting', value: { reason: close[1] } });\n });\n\n await room.disconnect();\n logger.debug('disconnected from room');\n\n const shutdownTasks = [];\n for (const callback of ctx.shutdownCallbacks) {\n shutdownTasks.push(callback());\n }\n await Promise.all(shutdownTasks).catch(() => logger.error('error while shutting down the job'));\n\n process.send!({ case: 'done' });\n logger.info('job completed.');\n process.exit();\n });\n\n return { ctx, task };\n};\n\n(async () => {\n if (process.send) {\n // process.argv:\n // [0] `node'\n // [1] import.meta.filename\n // [2] import.meta.filename of function containing entry file\n const moduleFile = process.argv[2];\n const agent: Agent = await import(pathToFileURL(moduleFile!).pathname).then((module) => {\n const agent = module.default;\n if (agent === undefined || !isAgent(agent)) {\n throw new Error(`Unable to load agent: Missing or invalid default export in ${moduleFile}`);\n }\n return agent;\n });\n if (!agent.prewarm) {\n agent.prewarm = defaultInitializeProcessFunc;\n }\n\n // don't do anything on C-c\n // this is handled in cli, triggering a termination of all child processes at once.\n process.on('SIGINT', () => {\n logger.info('SIGINT received in job proc');\n });\n\n // don't do anything on SIGTERM\n // Render uses SIGTERM in autoscale, this ensures the processes are properly drained if needed\n process.on('SIGTERM', () => {\n logger.info('SIGTERM received in job proc');\n });\n\n await once(process, 'message').then(([msg]: IPCMessage[]) => {\n msg = msg!;\n if (msg.case !== 'initializeRequest') {\n throw new Error('first message must be InitializeRequest');\n }\n initializeLogger(msg.value.loggerOptions);\n });\n const proc = new JobProcess();\n let logger = log().child({ pid: proc.pid });\n\n process.on('unhandledRejection', (reason) => {\n logger.error(reason);\n });\n\n logger.debug('initializing job runner');\n agent.prewarm(proc);\n logger.debug('job runner initialized');\n process.send({ case: 'initializeResponse' });\n\n let job: JobTask | undefined = undefined;\n const closeEvent = new EventEmitter();\n\n const orphanedTimeout = setTimeout(() => {\n logger.warn('job process orphaned, shutting down.');\n process.exit();\n }, ORPHANED_TIMEOUT);\n\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pingRequest': {\n orphanedTimeout.refresh();\n process.send!({\n case: 'pongResponse',\n value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },\n });\n break;\n }\n case 'startJobRequest': {\n if (job) {\n throw new Error('job task already running');\n }\n\n logger = logger.child({ jobID: msg.value.runningJob.job.id });\n\n job = startJob(proc, agent.entry, msg.value.runningJob, closeEvent, logger);\n logger.debug('job started');\n break;\n }\n case 'shutdownRequest': {\n if (!job) {\n break;\n }\n closeEvent.emit('close', '');\n }\n }\n });\n }\n})();\n"],"mappings":";AAGA,sBAAgC;AAChC,yBAA2B;AAC3B,yBAAmC;AACnC,sBAA8B;AAE9B,uBAAoC;AACpC,iBAA+E;AAC/E,iBAAsC;AACtC,oBAA6C;AAI7C,MAAM,mBAAmB,KAAK;AAO9B,MAAM,iBAAiB;AAAA,EACrB,UAAU,IAAI,QAA6D,CAAC,YAAY;AACtF,SAAK,UAAU;AAAA,EACjB,CAAC;AAAA,EACD,QAAQ,KAA0D;AAChE;AAAA,EACF;AACF;AAEA,MAAM,UAAuC;AAAA,EAC3C,YAAgD,CAAC;AAAA,EAEjD,cAAc;AACZ,YAAQ,GAAG,WAAW,CAAC,QAAoB;AACzC,cAAQ,IAAI,MAAM;AAAA,QAChB,KAAK;AACH,gBAAM,MAAM,KAAK,UAAU,IAAI,MAAM,SAAS;AAC9C,iBAAO,KAAK,UAAU,IAAI,MAAM,SAAS;AACzC,cAAI,CAAC,KAAK;AACR,gCAAI,EAAE,MAAM,EAAE,MAAM,IAAI,MAAM,CAAC,EAAE,KAAK,wCAAwC;AAC9E;AAAA,UACF;AACA,cAAI,QAAQ,IAAI,KAAK;AACrB;AAAA,MACJ;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,YAAY,QAAgB,MAAiC;AACjE,UAAM,YAAY,mBAAmB;AACrC,YAAQ,KAAM,EAAE,MAAM,oBAAoB,OAAO,EAAE,WAAW,QAAQ,KAAK,EAAE,CAAC;AAC9E,SAAK,UAAU,SAAS,IAAI,IAAI,iBAAiB;AACjD,UAAM,OAAO,MAAM,KAAK,UAAU,SAAS,EAAG;AAC9C,QAAI,KAAK,OAAO;AACd,YAAM,IAAI,MAAM,gBAAgB,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AAK,sCAAsC;AAClD,cAAQ,KAAK;AAAA,IACf,GAAG,gBAAgB;AAEnB,YAAQ,GAAG,WAAW,CAAC,QAAoB;AACzC,cAAQ,IAAI,MAAM;AAAA,QAChB,KAAK,eAAe;AAClB,0BAAgB,QAAQ;AACxB,kBAAQ,KAAM;AAAA,YACZ,MAAM;AAAA,YACN,OAAO,EAAE,eAAe,IAAI,MAAM,WAAW,WAAW,KAAK,IAAI,EAAE;AAAA,UACrE,CAAC;AACD;AAAA,QACF;AAAA,QACA,KAAK,mBAAmB;AACtB,cAAI,KAAK;AACP,kBAAM,IAAI,MAAM,0BAA0B;AAAA,UAC5C;AAEA,mBAAS,OAAO,MAAM,EAAE,OAAO,IAAI,MAAM,WAAW,IAAI,GAAG,CAAC;AAE5D,gBAAM,SAAS,MAAM,MAAM,OAAO,IAAI,MAAM,YAAY,YAAY,MAAM;AAC1E,iBAAO,MAAM,aAAa;AAC1B;AAAA,QACF;AAAA,QACA,KAAK,mBAAmB;AACtB,cAAI,CAAC,KAAK;AACR;AAAA,UACF;AACA,qBAAW,KAAK,SAAS,EAAE;AAAA,QAC7B;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACF,GAAG;","names":["module","agent"]}
|
|
@@ -81,6 +81,7 @@ const startJob = (proc, func, info, closeEvent, logger) => {
|
|
|
81
81
|
}
|
|
82
82
|
await Promise.all(shutdownTasks).catch(() => logger.error("error while shutting down the job"));
|
|
83
83
|
process.send({ case: "done" });
|
|
84
|
+
logger.info("job completed.");
|
|
84
85
|
process.exit();
|
|
85
86
|
});
|
|
86
87
|
return { ctx, task };
|
|
@@ -99,6 +100,10 @@ const startJob = (proc, func, info, closeEvent, logger) => {
|
|
|
99
100
|
agent.prewarm = defaultInitializeProcessFunc;
|
|
100
101
|
}
|
|
101
102
|
process.on("SIGINT", () => {
|
|
103
|
+
logger.info("SIGINT received in job proc");
|
|
104
|
+
});
|
|
105
|
+
process.on("SIGTERM", () => {
|
|
106
|
+
logger.info("SIGTERM received in job proc");
|
|
102
107
|
});
|
|
103
108
|
await once(process, "message").then(([msg]) => {
|
|
104
109
|
msg = msg;
|
|
@@ -109,6 +114,9 @@ const startJob = (proc, func, info, closeEvent, logger) => {
|
|
|
109
114
|
});
|
|
110
115
|
const proc = new JobProcess();
|
|
111
116
|
let logger = log().child({ pid: proc.pid });
|
|
117
|
+
process.on("unhandledRejection", (reason) => {
|
|
118
|
+
logger.error(reason);
|
|
119
|
+
});
|
|
112
120
|
logger.debug("initializing job runner");
|
|
113
121
|
agent.prewarm(proc);
|
|
114
122
|
logger.debug("job runner initialized");
|
|
@@ -116,7 +124,7 @@ const startJob = (proc, func, info, closeEvent, logger) => {
|
|
|
116
124
|
let job = void 0;
|
|
117
125
|
const closeEvent = new EventEmitter();
|
|
118
126
|
const orphanedTimeout = setTimeout(() => {
|
|
119
|
-
logger.warn("process orphaned, shutting down");
|
|
127
|
+
logger.warn("job process orphaned, shutting down.");
|
|
120
128
|
process.exit();
|
|
121
129
|
}, ORPHANED_TIMEOUT);
|
|
122
130
|
process.on("message", (msg) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ipc/job_proc_lazy_main.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Room, RoomEvent } from '@livekit/rtc-node';\nimport { randomUUID } from 'node:crypto';\nimport { EventEmitter, once } from 'node:events';\nimport { pathToFileURL } from 'node:url';\nimport type { Logger } from 'pino';\nimport { type Agent, isAgent } from '../generator.js';\nimport { CurrentJobContext, JobContext, JobProcess, type RunningJobInfo } from '../job.js';\nimport { initializeLogger, log } from '../log.js';\nimport { defaultInitializeProcessFunc } from '../worker.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { IPCMessage } from './message.js';\n\nconst ORPHANED_TIMEOUT = 15 * 1000;\n\ntype JobTask = {\n ctx: JobContext;\n task: Promise<void>;\n};\n\nclass PendingInference {\n promise = new Promise<{ requestId: string; data: unknown; error?: Error }>((resolve) => {\n this.resolve = resolve; // this is how JavaScript lets you resolve promises externally\n });\n resolve(arg: { requestId: string; data: unknown; error?: Error }) {\n arg; // useless call to counteract TypeScript E6133\n }\n}\n\nclass InfClient implements InferenceExecutor {\n #requests: { [id: string]: PendingInference } = {};\n\n constructor() {\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'inferenceResponse':\n const fut = this.#requests[msg.value.requestId];\n delete this.#requests[msg.value.requestId];\n if (!fut) {\n log().child({ resp: msg.value }).warn('received unexpected inference response');\n return;\n }\n fut.resolve(msg.value);\n break;\n }\n });\n }\n\n async doInference(method: string, data: unknown): Promise<unknown> {\n const requestId = 'inference_job_' + randomUUID;\n process.send!({ case: 'inferenceRequest', value: { requestId, method, data } });\n this.#requests[requestId] = new PendingInference();\n const resp = await this.#requests[requestId]!.promise;\n if (resp.error) {\n throw new Error(`inference of ${method} failed: ${resp.error.message}`);\n }\n return resp.data;\n }\n}\n\nconst startJob = (\n proc: JobProcess,\n func: (ctx: JobContext) => Promise<void>,\n info: RunningJobInfo,\n closeEvent: EventEmitter,\n logger: Logger,\n): JobTask => {\n let connect = false;\n let shutdown = false;\n\n const room = new Room();\n room.on(RoomEvent.Disconnected, () => {\n closeEvent.emit('close', false);\n });\n\n const onConnect = () => {\n connect = true;\n };\n const onShutdown = (reason: string) => {\n shutdown = true;\n closeEvent.emit('close', true, reason);\n };\n\n const ctx = new JobContext(proc, info, room, onConnect, onShutdown, new InfClient());\n new CurrentJobContext(ctx);\n\n const task = new Promise<void>(async () => {\n const unconnectedTimeout = setTimeout(() => {\n if (!(connect || shutdown)) {\n logger.warn(\n 'room not connect after job_entry was called after 10 seconds, ',\n 'did you forget to call ctx.connect()?',\n );\n }\n }, 10000);\n func(ctx).finally(() => clearTimeout(unconnectedTimeout));\n\n await once(closeEvent, 'close').then((close) => {\n logger.debug('shutting down');\n process.send!({ case: 'exiting', value: { reason: close[1] } });\n });\n\n await room.disconnect();\n logger.debug('disconnected from room');\n\n const shutdownTasks = [];\n for (const callback of ctx.shutdownCallbacks) {\n shutdownTasks.push(callback());\n }\n await Promise.all(shutdownTasks).catch(() => logger.error('error while shutting down the job'));\n\n process.send!({ case: 'done' });\n process.exit();\n });\n\n return { ctx, task };\n};\n\n(async () => {\n if (process.send) {\n // process.argv:\n // [0] `node'\n // [1] import.meta.filename\n // [2] import.meta.filename of function containing entry file\n const moduleFile = process.argv[2];\n const agent: Agent = await import(pathToFileURL(moduleFile!).pathname).then((module) => {\n const agent = module.default;\n if (agent === undefined || !isAgent(agent)) {\n throw new Error(`Unable to load agent: Missing or invalid default export in ${moduleFile}`);\n }\n return agent;\n });\n if (!agent.prewarm) {\n agent.prewarm = defaultInitializeProcessFunc;\n }\n\n // don't do anything on C-c\n // this is handled in cli, triggering a termination of all child processes at once.\n process.on('SIGINT', () => {});\n\n await once(process, 'message').then(([msg]: IPCMessage[]) => {\n msg = msg!;\n if (msg.case !== 'initializeRequest') {\n throw new Error('first message must be InitializeRequest');\n }\n initializeLogger(msg.value.loggerOptions);\n });\n const proc = new JobProcess();\n let logger = log().child({ pid: proc.pid });\n\n logger.debug('initializing job runner');\n agent.prewarm(proc);\n logger.debug('job runner initialized');\n process.send({ case: 'initializeResponse' });\n\n let job: JobTask | undefined = undefined;\n const closeEvent = new EventEmitter();\n\n const orphanedTimeout = setTimeout(() => {\n logger.warn('process orphaned, shutting down');\n process.exit();\n }, ORPHANED_TIMEOUT);\n\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pingRequest': {\n orphanedTimeout.refresh();\n process.send!({\n case: 'pongResponse',\n value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },\n });\n break;\n }\n case 'startJobRequest': {\n if (job) {\n throw new Error('job task already running');\n }\n\n logger = logger.child({ jobID: msg.value.runningJob.job.id });\n\n job = startJob(proc, agent.entry, msg.value.runningJob, closeEvent, logger);\n logger.debug('job started');\n break;\n }\n case 'shutdownRequest': {\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rE,CAAC;AACD;AAAA,QACF;AAAA,QACA,KAAK,mBAAmB;AACtB,cAAI,KAAK;AACP,kBAAM,IAAI,MAAM,0BAA0B;AAAA,UAC5C;AAEA,mBAAS,OAAO,MAAM,EAAE,OAAO,IAAI,MAAM,WAAW,IAAI,GAAG,CAAC;AAE5D,gBAAM,SAAS,MAAM,MAAM,OAAO,IAAI,MAAM,YAAY,YAAY,MAAM;AAC1E,iBAAO,MAAM,aAAa;AAC1B;AAAA,QACF;AAAA,QACA,KAAK,mBAAmB;AACtB,cAAI,CAAC,KAAK;AACR;AAAA,UACF;AACA,qBAAW,KAAK,SAAS,EAAE;AAAA,QAC7B;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACF,GAAG;","names":["agent"]}
|
|
1
|
+
{"version":3,"sources":["../../src/ipc/job_proc_lazy_main.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Room, RoomEvent } from '@livekit/rtc-node';\nimport { randomUUID } from 'node:crypto';\nimport { EventEmitter, once } from 'node:events';\nimport { pathToFileURL } from 'node:url';\nimport type { Logger } from 'pino';\nimport { type Agent, isAgent } from '../generator.js';\nimport { CurrentJobContext, JobContext, JobProcess, type RunningJobInfo } from '../job.js';\nimport { initializeLogger, log } from '../log.js';\nimport { defaultInitializeProcessFunc } from '../worker.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { IPCMessage } from './message.js';\n\nconst ORPHANED_TIMEOUT = 15 * 1000;\n\ntype JobTask = {\n ctx: JobContext;\n task: Promise<void>;\n};\n\nclass PendingInference {\n promise = new Promise<{ requestId: string; data: unknown; error?: Error }>((resolve) => {\n this.resolve = resolve; // this is how JavaScript lets you resolve promises externally\n });\n resolve(arg: { requestId: string; data: unknown; error?: Error }) {\n arg; // useless call to counteract TypeScript E6133\n }\n}\n\nclass InfClient implements InferenceExecutor {\n #requests: { [id: string]: PendingInference } = {};\n\n constructor() {\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'inferenceResponse':\n const fut = this.#requests[msg.value.requestId];\n delete this.#requests[msg.value.requestId];\n if (!fut) {\n log().child({ resp: msg.value }).warn('received unexpected inference response');\n return;\n }\n fut.resolve(msg.value);\n break;\n }\n });\n }\n\n async doInference(method: string, data: unknown): Promise<unknown> {\n const requestId = 'inference_job_' + randomUUID;\n process.send!({ case: 'inferenceRequest', value: { requestId, method, data } });\n this.#requests[requestId] = new PendingInference();\n const resp = await this.#requests[requestId]!.promise;\n if (resp.error) {\n throw new Error(`inference of ${method} failed: ${resp.error.message}`);\n }\n return resp.data;\n }\n}\n\nconst startJob = (\n proc: JobProcess,\n func: (ctx: JobContext) => Promise<void>,\n info: RunningJobInfo,\n closeEvent: EventEmitter,\n logger: Logger,\n): JobTask => {\n let connect = false;\n let shutdown = false;\n\n const room = new Room();\n room.on(RoomEvent.Disconnected, () => {\n closeEvent.emit('close', false);\n });\n\n const onConnect = () => {\n connect = true;\n };\n const onShutdown = (reason: string) => {\n shutdown = true;\n closeEvent.emit('close', true, reason);\n };\n\n const ctx = new JobContext(proc, info, room, onConnect, onShutdown, new InfClient());\n new CurrentJobContext(ctx);\n\n const task = new Promise<void>(async () => {\n const unconnectedTimeout = setTimeout(() => {\n if (!(connect || shutdown)) {\n logger.warn(\n 'room not connect after job_entry was called after 10 seconds, ',\n 'did you forget to call ctx.connect()?',\n );\n }\n }, 10000);\n func(ctx).finally(() => clearTimeout(unconnectedTimeout));\n\n await once(closeEvent, 'close').then((close) => {\n logger.debug('shutting down');\n process.send!({ case: 'exiting', value: { reason: close[1] } });\n });\n\n await room.disconnect();\n logger.debug('disconnected from room');\n\n const shutdownTasks = [];\n for (const callback of ctx.shutdownCallbacks) {\n shutdownTasks.push(callback());\n }\n await Promise.all(shutdownTasks).catch(() => logger.error('error while shutting down the job'));\n\n process.send!({ case: 'done' });\n logger.info('job completed.');\n process.exit();\n });\n\n return { ctx, task };\n};\n\n(async () => {\n if (process.send) {\n // process.argv:\n // [0] `node'\n // [1] import.meta.filename\n // [2] import.meta.filename of function containing entry file\n const moduleFile = process.argv[2];\n const agent: Agent = await import(pathToFileURL(moduleFile!).pathname).then((module) => {\n const agent = module.default;\n if (agent === undefined || !isAgent(agent)) {\n throw new Error(`Unable to load agent: Missing or invalid default export in ${moduleFile}`);\n }\n return agent;\n });\n if (!agent.prewarm) {\n agent.prewarm = defaultInitializeProcessFunc;\n }\n\n // don't do anything on C-c\n // this is handled in cli, triggering a termination of all child processes at once.\n process.on('SIGINT', () => {\n logger.info('SIGINT received in job proc');\n });\n\n // don't do anything on SIGTERM\n // Render uses SIGTERM in autoscale, this ensures the processes are properly drained if needed\n process.on('SIGTERM', () => {\n logger.info('SIGTERM received in job proc');\n });\n\n await once(process, 'message').then(([msg]: IPCMessage[]) => {\n msg = msg!;\n if (msg.case !== 'initializeRequest') {\n throw new Error('first message must be InitializeRequest');\n }\n initializeLogger(msg.value.loggerOptions);\n });\n const proc = new JobProcess();\n let logger = log().child({ pid: proc.pid });\n\n process.on('unhandledRejection', (reason) => {\n logger.error(reason);\n });\n\n logger.debug('initializing job runner');\n agent.prewarm(proc);\n logger.debug('job runner initialized');\n process.send({ case: 'initializeResponse' });\n\n let job: JobTask | undefined = undefined;\n const closeEvent = new EventEmitter();\n\n const orphanedTimeout = setTimeout(() => {\n logger.warn('job process orphaned, shutting down.');\n process.exit();\n }, ORPHANED_TIMEOUT);\n\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pingRequest': {\n orphanedTimeout.refresh();\n process.send!({\n case: 'pongResponse',\n value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },\n });\n break;\n }\n case 'startJobRequest': {\n if (job) {\n throw new Error('job task already running');\n }\n\n logger = logger.child({ jobID: msg.value.runningJob.job.id });\n\n job = startJob(proc, agent.entry, msg.value.runningJob, closeEvent, logger);\n logger.debug('job started');\n break;\n }\n case 'shutdownRequest': {\n if (!job) {\n break;\n }\n closeEvent.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,cAAQ,IAAI,MAAM;AAAA,QAChB,KAAK,eAAe;AAClB,0BAAgB,QAAQ;AACxB,kBAAQ,KAAM;AAAA,YACZ,MAAM;AAAA,YACN,OAAO,EAAE,eAAe,IAAI,MAAM,WAAW,WAAW,KAAK,IAAI,EAAE;AAAA,UACrE,CAAC;AACD;AAAA,QACF;AAAA,QACA,KAAK,mBAAmB;AACtB,cAAI,KAAK;AACP,kBAAM,IAAI,MAAM,0BAA0B;AAAA,UAC5C;AAEA,mBAAS,OAAO,MAAM,EAAE,OAAO,IAAI,MAAM,WAAW,IAAI,GAAG,CAAC;AAE5D,gBAAM,SAAS,MAAM,MAAM,OAAO,IAAI,MAAM,YAAY,YAAY,MAAM;AAC1E,iBAAO,MAAM,aAAa;AAC1B;AAAA,QACF;AAAA,QACA,KAAK,mBAAmB;AACtB,cAAI,CAAC,KAAK;AACR;AAAA,UACF;AACA,qBAAW,KAAK,SAAS,EAAE;AAAA,QAC7B;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACF,GAAG;","names":["agent"]}
|
|
@@ -65,8 +65,11 @@ class PlayoutHandle extends import_node_events.EventEmitter {
|
|
|
65
65
|
if (this.totalPlayedTime !== void 0) {
|
|
66
66
|
return Math.floor(this.totalPlayedTime * this.#sampleRate);
|
|
67
67
|
}
|
|
68
|
-
return Math.
|
|
69
|
-
|
|
68
|
+
return Math.max(
|
|
69
|
+
0,
|
|
70
|
+
Math.floor(
|
|
71
|
+
(this.pushedDuration - this.#audioSource.queuedDuration) * (this.#sampleRate / 1e3)
|
|
72
|
+
)
|
|
70
73
|
);
|
|
71
74
|
}
|
|
72
75
|
get textChars() {
|
|
@@ -139,7 +142,9 @@ class AgentPlayout extends import_node_events.EventEmitter {
|
|
|
139
142
|
}
|
|
140
143
|
handle.synchronizer.pushText(text);
|
|
141
144
|
}
|
|
142
|
-
|
|
145
|
+
if (!cancelled) {
|
|
146
|
+
handle.synchronizer.markTextSegmentEnd();
|
|
147
|
+
}
|
|
143
148
|
resolveText();
|
|
144
149
|
} catch (error) {
|
|
145
150
|
rejectText(error);
|
|
@@ -196,19 +201,19 @@ class AgentPlayout extends import_node_events.EventEmitter {
|
|
|
196
201
|
if (!captureTask.isCancelled) {
|
|
197
202
|
await (0, import_utils.gracefullyCancel)(captureTask);
|
|
198
203
|
}
|
|
204
|
+
if (!readTextTask.isCancelled) {
|
|
205
|
+
await (0, import_utils.gracefullyCancel)(readTextTask);
|
|
206
|
+
}
|
|
199
207
|
handle.totalPlayedTime = handle.pushedDuration - this.#audioSource.queuedDuration;
|
|
200
208
|
if (handle.interrupted || captureTask.error) {
|
|
201
|
-
await handle.synchronizer.close(true);
|
|
202
209
|
this.#audioSource.clearQueue();
|
|
203
210
|
}
|
|
204
|
-
if (!readTextTask.isCancelled) {
|
|
205
|
-
await (0, import_utils.gracefullyCancel)(readTextTask);
|
|
206
|
-
}
|
|
207
211
|
if (!firstFrame) {
|
|
208
212
|
this.emit("playout_stopped", handle.interrupted);
|
|
209
213
|
}
|
|
210
214
|
handle.doneFut.resolve();
|
|
211
|
-
|
|
215
|
+
const isInterrupted = handle.interrupted || !!captureTask.error;
|
|
216
|
+
await handle.synchronizer.close(isInterrupted);
|
|
212
217
|
}
|
|
213
218
|
resolve();
|
|
214
219
|
} catch (error) {
|