|
2 | 2 | // |
3 | 3 | // SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | | -import { version } from './index'; |
| 5 | +import { version } from '.'; |
6 | 6 | import { Option, Command } from 'commander'; |
| 7 | +import { WorkerOptions, Worker } from './worker'; |
| 8 | +import { EventEmitter } from 'events'; |
| 9 | +import { log } from './log'; |
7 | 10 |
|
8 | | -const program = new Command(); |
9 | | -program |
10 | | - .name('agents') |
11 | | - .description('LiveKit Agents CLI') |
12 | | - .version(version) |
13 | | - .addOption( |
14 | | - new Option('--log-level', 'Set the logging level').choices([ |
15 | | - 'DEBUG', |
16 | | - 'INFO', |
17 | | - 'WARNING', |
18 | | - 'ERROR', |
19 | | - 'CRITICAL', |
20 | | - ]), |
21 | | - ); |
22 | | - |
23 | | -program |
24 | | - .command('start') |
25 | | - .description('Start the worker') |
26 | | - .addOption( |
27 | | - new Option('--url <string>', 'LiveKit server or Cloud project websocket URL') |
28 | | - .makeOptionMandatory(true) |
29 | | - .env('LIVEKIT_URL'), |
30 | | - ) |
31 | | - .addOption( |
32 | | - new Option('--api-key <string>', "LiveKit server or Cloud project's API key") |
33 | | - .makeOptionMandatory(true) |
34 | | - .env('LIVEKIT_API_KEY'), |
35 | | - ) |
36 | | - .addOption( |
37 | | - new Option('--api-secret <string>', "LiveKit server or Cloud project's API secret") |
38 | | - .makeOptionMandatory(true) |
39 | | - .env('LIVEKIT_API_SECRET'), |
40 | | - ) |
41 | | - .action(() => { |
42 | | - return; |
| 11 | +type CliArgs = { |
| 12 | + opts: WorkerOptions; |
| 13 | + logLevel: string; |
| 14 | + production: boolean; |
| 15 | + watch: boolean; |
| 16 | + event?: EventEmitter; |
| 17 | +}; |
| 18 | + |
| 19 | +const runWorker = async (args: CliArgs) => { |
| 20 | + log.level = args.logLevel; |
| 21 | + const worker = new Worker(args.opts); |
| 22 | + |
| 23 | + process.on('SIGINT', async () => { |
| 24 | + await worker.close(); |
| 25 | + process.exit(130); // SIGINT exit code |
43 | 26 | }); |
44 | 27 |
|
45 | | -program.parse(); |
| 28 | + try { |
| 29 | + await worker.run(); |
| 30 | + } catch { |
| 31 | + log.fatal('worker failed'); |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +export const runApp = (opts: WorkerOptions) => { |
| 36 | + const program = new Command() |
| 37 | + .name('agents') |
| 38 | + .description('LiveKit Agents CLI') |
| 39 | + .version(version) |
| 40 | + .addOption( |
| 41 | + new Option('--log-level <level>', 'Set the logging level') |
| 42 | + .choices(['trace', 'debug', 'info', 'warn', 'error', 'fatal']) |
| 43 | + .default('trace'), |
| 44 | + ) |
| 45 | + .addOption( |
| 46 | + new Option('--url <string>', 'LiveKit server or Cloud project websocket URL') |
| 47 | + .makeOptionMandatory(true) |
| 48 | + .env('LIVEKIT_URL'), |
| 49 | + ) |
| 50 | + .addOption( |
| 51 | + new Option('--api-key <string>', "LiveKit server or Cloud project's API key") |
| 52 | + .makeOptionMandatory(true) |
| 53 | + .env('LIVEKIT_API_KEY'), |
| 54 | + ) |
| 55 | + .addOption( |
| 56 | + new Option('--api-secret <string>', "LiveKit server or Cloud project's API secret") |
| 57 | + .makeOptionMandatory(true) |
| 58 | + .env('LIVEKIT_API_SECRET'), |
| 59 | + ) |
| 60 | + |
| 61 | + program |
| 62 | + .command('start') |
| 63 | + .description('Start the worker in production mode') |
| 64 | + .action(() => { |
| 65 | + const options = program.optsWithGlobals() |
| 66 | + opts.wsURL = options.url || opts.wsURL; |
| 67 | + opts.apiKey = options.apiKey || opts.apiKey; |
| 68 | + opts.apiSecret = options.apiSecret || opts.apiSecret; |
| 69 | + runWorker({ |
| 70 | + opts, |
| 71 | + production: true, |
| 72 | + watch: false, |
| 73 | + logLevel: options.logLevel, |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + program.parse(); |
| 78 | +}; |
0 commit comments