Skip to content

Commit ac8214d

Browse files
brianyin/agt-2100-rename-worker-to-agentserver (#713)
Co-authored-by: Toubat <[email protected]>
1 parent e7223b0 commit ac8214d

File tree

3 files changed

+49
-34
lines changed

3 files changed

+49
-34
lines changed

.changeset/open-mice-bathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@livekit/agents': patch
3+
---
4+
5+
Rename Worker to AgentServer

agents/src/cli.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@ import type { EventEmitter } from 'node:events';
66
import { initializeLogger, log } from './log.js';
77
import { Plugin } from './plugin.js';
88
import { version } from './version.js';
9-
import { Worker, WorkerOptions } from './worker.js';
9+
import { AgentServer, ServerOptions } from './worker.js';
1010

1111
type CliArgs = {
12-
opts: WorkerOptions;
12+
opts: ServerOptions;
1313
production: boolean;
1414
watch: boolean;
1515
event?: EventEmitter;
1616
room?: string;
1717
participantIdentity?: string;
1818
};
1919

20-
const runWorker = async (args: CliArgs) => {
20+
const runServer = async (args: CliArgs) => {
2121
initializeLogger({ pretty: !args.production, level: args.opts.logLevel });
2222
const logger = log();
2323

24-
// though `production` is defined in WorkerOptions, it will always be overridden by CLI.
24+
// though `production` is defined in ServerOptions, it will always be overridden by CLI.
2525
const { production: _, ...opts } = args.opts; // eslint-disable-line @typescript-eslint/no-unused-vars
26-
const worker = new Worker(new WorkerOptions({ production: args.production, ...opts }));
26+
const server = new AgentServer(new ServerOptions({ production: args.production, ...opts }));
2727

2828
if (args.room) {
29-
worker.event.once('worker_registered', () => {
29+
server.event.once('worker_registered', () => {
3030
logger.info(`connecting to room ${args.room}`);
31-
worker.simulateJob(args.room!, args.participantIdentity);
31+
server.simulateJob(args.room!, args.participantIdentity);
3232
});
3333
}
3434

@@ -40,25 +40,25 @@ const runWorker = async (args: CliArgs) => {
4040
process.exit(130); // SIGINT exit code
4141
});
4242
if (args.production) {
43-
await worker.drain();
43+
await server.drain();
4444
}
45-
await worker.close();
45+
await server.close();
4646
logger.debug('worker closed due to SIGINT.');
4747
process.exit(130); // SIGINT exit code
4848
});
4949

5050
process.once('SIGTERM', async () => {
5151
logger.debug('SIGTERM received in CLI.');
5252
if (args.production) {
53-
await worker.drain();
53+
await server.drain();
5454
}
55-
await worker.close();
55+
await server.close();
5656
logger.debug('worker closed due to SIGTERM.');
5757
process.exit(143); // SIGTERM exit code
5858
});
5959

6060
try {
61-
await worker.run();
61+
await server.run();
6262
} catch {
6363
logger.fatal('closing worker due to error.');
6464
process.exit(1);
@@ -72,11 +72,11 @@ const runWorker = async (args: CliArgs) => {
7272
* @example
7373
* ```
7474
* if (process.argv[1] === fileURLToPath(import.meta.url)) {
75-
* cli.runApp(new WorkerOptions({ agent: import.meta.filename }));
75+
* cli.runApp(new ServerOptions({ agent: import.meta.filename }));
7676
* }
7777
* ```
7878
*/
79-
export const runApp = (opts: WorkerOptions) => {
79+
export const runApp = (opts: ServerOptions) => {
8080
const program = new Command()
8181
.name('agents')
8282
.description('LiveKit Agents CLI')
@@ -127,7 +127,7 @@ export const runApp = (opts: WorkerOptions) => {
127127
opts.apiSecret = options.apiSecret || opts.apiSecret;
128128
opts.logLevel = options.logLevel || opts.logLevel;
129129
opts.workerToken = options.workerToken || opts.workerToken;
130-
runWorker({
130+
runServer({
131131
opts,
132132
production: true,
133133
watch: false,
@@ -150,7 +150,7 @@ export const runApp = (opts: WorkerOptions) => {
150150
opts.apiSecret = options.apiSecret || opts.apiSecret;
151151
opts.logLevel = options.logLevel || opts.logLevel;
152152
opts.workerToken = options.workerToken || opts.workerToken;
153-
runWorker({
153+
runServer({
154154
opts,
155155
production: false,
156156
watch: false,
@@ -175,7 +175,7 @@ export const runApp = (opts: WorkerOptions) => {
175175
opts.apiSecret = options.apiSecret || opts.apiSecret;
176176
opts.logLevel = options.logLevel || opts.logLevel;
177177
opts.workerToken = options.workerToken || opts.workerToken;
178-
runWorker({
178+
runServer({
179179
opts,
180180
production: false,
181181
watch: false,

agents/src/worker.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const defaultRequestFunc = async (ctx: JobRequest) => {
7979
};
8080

8181
// eslint-disable-next-line @typescript-eslint/no-unused-vars
82-
const defaultCpuLoad = async (worker: Worker): Promise<number> => {
82+
const defaultCpuLoad = async (worker: AgentServer): Promise<number> => {
8383
return new Promise((resolve) => {
8484
const cpus1 = os.cpus();
8585

@@ -141,17 +141,17 @@ export class WorkerPermissions {
141141
*
142142
* This class is mostly useful in conjunction with {@link cli.runApp}.
143143
*/
144-
export class WorkerOptions {
144+
export class ServerOptions {
145145
agent: string;
146146
requestFunc: (job: JobRequest) => Promise<void>;
147-
loadFunc: (worker: Worker) => Promise<number>;
147+
loadFunc: (worker: AgentServer) => Promise<number>;
148148
loadThreshold: number;
149149
numIdleProcesses: number;
150150
shutdownProcessTimeout: number;
151151
initializeProcessTimeout: number;
152152
permissions: WorkerPermissions;
153153
agentName: string;
154-
workerType: JobType;
154+
serverType: JobType;
155155
maxRetry: number;
156156
wsURL: string;
157157
apiKey?: string;
@@ -175,7 +175,7 @@ export class WorkerOptions {
175175
initializeProcessTimeout = 10 * 1000,
176176
permissions = new WorkerPermissions(),
177177
agentName = '',
178-
workerType = JobType.JT_ROOM,
178+
serverType = JobType.JT_ROOM,
179179
maxRetry = MAX_RECONNECT_ATTEMPTS,
180180
wsURL = 'ws://localhost:7880',
181181
apiKey = undefined,
@@ -195,15 +195,15 @@ export class WorkerOptions {
195195
agent: string;
196196
requestFunc?: (job: JobRequest) => Promise<void>;
197197
/** Called to determine the current load of the worker. Should return a value between 0 and 1. */
198-
loadFunc?: (worker: Worker) => Promise<number>;
198+
loadFunc?: (worker: AgentServer) => Promise<number>;
199199
/** When the load exceeds this threshold, the worker will be marked as unavailable. */
200200
loadThreshold?: number;
201201
numIdleProcesses?: number;
202202
shutdownProcessTimeout?: number;
203203
initializeProcessTimeout?: number;
204204
permissions?: WorkerPermissions;
205205
agentName?: string;
206-
workerType?: JobType;
206+
serverType?: JobType;
207207
maxRetry?: number;
208208
wsURL?: string;
209209
apiKey?: string;
@@ -228,7 +228,7 @@ export class WorkerOptions {
228228
this.initializeProcessTimeout = initializeProcessTimeout;
229229
this.permissions = permissions;
230230
this.agentName = agentName;
231-
this.workerType = workerType;
231+
this.serverType = serverType;
232232
this.maxRetry = maxRetry;
233233
this.wsURL = wsURL;
234234
this.apiKey = apiKey;
@@ -261,8 +261,8 @@ class PendingAssignment {
261261
* you don't have access to a command line, such as a headless program, or one that uses Agents
262262
* behind a wrapper.
263263
*/
264-
export class Worker {
265-
#opts: WorkerOptions;
264+
export class AgentServer {
265+
#opts: ServerOptions;
266266
#procPool: ProcPool;
267267

268268
#id = 'unregistered';
@@ -279,23 +279,23 @@ export class Worker {
279279
#logger = log().child({ version });
280280
#inferenceExecutor?: InferenceProcExecutor;
281281

282-
/** @throws {@link MissingCredentialsError} if URL, API key or API secret are missing */
283-
constructor(opts: WorkerOptions) {
282+
/* @throws {@link MissingCredentialsError} if URL, API key or API secret are missing */
283+
constructor(opts: ServerOptions) {
284284
opts.wsURL = opts.wsURL || process.env.LIVEKIT_URL || '';
285285
opts.apiKey = opts.apiKey || process.env.LIVEKIT_API_KEY || '';
286286
opts.apiSecret = opts.apiSecret || process.env.LIVEKIT_API_SECRET || '';
287287

288288
if (opts.wsURL === '')
289289
throw new MissingCredentialsError(
290-
'URL is required: Set LIVEKIT_URL, run with --url, or pass wsURL in WorkerOptions',
290+
'URL is required: Set LIVEKIT_URL, run with --url, or pass wsURL in ServerOptions',
291291
);
292292
if (opts.apiKey === '')
293293
throw new MissingCredentialsError(
294-
'API Key is required: Set LIVEKIT_API_KEY, run with --api-key, or pass apiKey in WorkerOptions',
294+
'API Key is required: Set LIVEKIT_API_KEY, run with --api-key, or pass apiKey in ServerOptions',
295295
);
296296
if (opts.apiSecret === '')
297297
throw new MissingCredentialsError(
298-
'API Secret is required: Set LIVEKIT_API_SECRET, run with --api-secret, or pass apiSecret in WorkerOptions',
298+
'API Secret is required: Set LIVEKIT_API_SECRET, run with --api-secret, or pass apiSecret in ServerOptions',
299299
);
300300

301301
if (opts.workerToken) {
@@ -340,7 +340,7 @@ export class Worker {
340340
this.#opts = opts;
341341
this.#httpServer = new HTTPServer(opts.host, opts.port, () => ({
342342
agent_name: opts.agentName,
343-
worker_type: JobType[opts.workerType],
343+
worker_type: JobType[opts.serverType],
344344
active_jobs: this.activeJobs.length,
345345
sdk_version: version,
346346
project_type: PROJECT_TYPE,
@@ -610,7 +610,7 @@ export class Worker {
610610
message: {
611611
case: 'register',
612612
value: {
613-
type: this.#opts.workerType,
613+
type: this.#opts.serverType,
614614
agentName: this.#opts.agentName,
615615
allowedPermissions: new ParticipantPermission({
616616
canPublish: this.#opts.permissions.canPublish,
@@ -788,3 +788,13 @@ export class Worker {
788788
await this.#close.await;
789789
}
790790
}
791+
792+
/**
793+
* @deprecated Use {@link AgentServer} instead. This alias is provided for backward compatibility.
794+
*/
795+
export const Worker = AgentServer;
796+
797+
/**
798+
* @deprecated Use {@link ServerOptions} instead. This alias is provided for backward compatibility.
799+
*/
800+
export const WorkerOptions = ServerOptions;

0 commit comments

Comments
 (0)