Skip to content

Commit 35fc428

Browse files
author
aoife cassidy
authored
chore(treewide): native CommonJS (#176)
1 parent 6c12af9 commit 35fc428

File tree

20 files changed

+1272
-155
lines changed

20 files changed

+1272
-155
lines changed

.changeset/eight-carrots-notice.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@livekit/agents": minor
3+
"@livekit/agents-plugin-deepgram": minor
4+
"@livekit/agents-plugin-elevenlabs": minor
5+
"@livekit/agents-plugin-openai": minor
6+
"@livekit/agents-plugin-silero": minor
7+
---
8+
9+
support native CommonJS

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ originally written in Python.
2727

2828
<!--END_DESCRIPTION-->
2929

30-
> [!WARNING]
31-
> We are aware of a bug concerning users of tsx under certain conditions. See [this
32-
> issue](https://github.com/livekit/agents-js/issues/147) for more details and a fix.
33-
3430
## [NEW] OpenAI Realtime API support
3531

3632
We're partnering with OpenAI on a new MultimodalAgent API in the Agents framework. This class

REUSE.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ SPDX-License-Identifier = "Apache-2.0"
2121

2222
# project configuration files
2323
[[annotations]]
24-
path = [".prettierrc", ".prettierignore", ".eslintrc", "**.json"]
24+
path = [".prettierrc", ".prettierignore", ".eslintrc", "**.json", "**/tsup.config.ts"]
2525
SPDX-FileCopyrightText = "2024 LiveKit, Inc."
2626
SPDX-License-Identifier = "Apache-2.0"
2727

agents/package.json

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,52 @@
33
"version": "0.4.6",
44
"description": "LiveKit Agents - Node.js",
55
"main": "dist/index.js",
6+
"require": "dist/index.cjs",
67
"types": "dist/index.d.ts",
8+
"exports": {
9+
".": {
10+
"types": "./dist/index.d.ts",
11+
"import": "./dist/index.js",
12+
"require": "./dist/index.cjs"
13+
}
14+
},
715
"author": "LiveKit",
816
"type": "module",
917
"repository": "[email protected]:livekit/agents-js.git",
1018
"license": "Apache-2.0",
1119
"files": [
1220
"dist",
13-
"src"
21+
"src",
22+
"README.md"
1423
],
1524
"scripts": {
16-
"build": "tsc",
25+
"build": "tsup --onSuccess \"tsc --declaration --emitDeclarationOnly\"",
1726
"clean": "rm -rf dist",
1827
"clean:build": "pnpm clean && pnpm build",
1928
"lint": "eslint -f unix \"src/**/*.ts\"",
2029
"api:check": "api-extractor run --typescript-compiler-folder ../node_modules/typescript",
2130
"api:update": "api-extractor run --local --typescript-compiler-folder ../node_modules/typescript --verbose"
2231
},
2332
"devDependencies": {
33+
"@livekit/rtc-node": "^0.12.1",
2434
"@microsoft/api-extractor": "^7.35.0",
25-
"@livekit/rtc-node": "^0.11.1",
2635
"@types/node": "^22.5.5",
2736
"@types/ws": "^8.5.10",
37+
"tsup": "^8.3.5",
2838
"typescript": "^5.0.0"
2939
},
3040
"dependencies": {
31-
"@livekit/mutex": "^1.1.0",
32-
"@livekit/protocol": "^1.27.1",
41+
"@livekit/mutex": "^1.1.1",
42+
"@livekit/protocol": "^1.29.1",
3343
"@livekit/typed-emitter": "^3.0.0",
3444
"commander": "^12.0.0",
35-
"livekit-server-sdk": "^2.8.1",
45+
"livekit-server-sdk": "^2.9.2",
3646
"pino": "^8.19.0",
3747
"pino-pretty": "^11.0.0",
3848
"ws": "^8.16.0",
3949
"zod": "^3.23.8"
4050
},
4151
"peerDependencies": {
42-
"@livekit/rtc-node": "^0.11.1"
52+
"@livekit/rtc-node": "^0.12.1"
4353
}
4454
}

agents/src/ipc/job_main.ts

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -88,77 +88,79 @@ const startJob = (
8888
return { ctx, task };
8989
};
9090

91-
if (process.send) {
92-
// process.argv:
93-
// [0] `node'
94-
// [1] import.meta.filename
95-
// [2] import.meta.filename of function containing entry file
96-
const moduleFile = process.argv[2];
97-
const agent: Agent = await import(pathToFileURL(moduleFile!).href).then((module) => {
98-
const agent = module.default;
99-
if (agent === undefined || !isAgent(agent)) {
100-
throw new Error(`Unable to load agent: Missing or invalid default export in ${moduleFile}`);
101-
}
102-
return agent;
103-
});
104-
if (!agent.prewarm) {
105-
agent.prewarm = defaultInitializeProcessFunc;
106-
}
107-
108-
// don't do anything on C-c
109-
// this is handled in cli, triggering a termination of all child processes at once.
110-
process.on('SIGINT', () => {});
111-
112-
await once(process, 'message').then(([msg]: IPCMessage[]) => {
113-
msg = msg!;
114-
if (msg.case !== 'initializeRequest') {
115-
throw new Error('first message must be InitializeRequest');
91+
(async () => {
92+
if (process.send) {
93+
// process.argv:
94+
// [0] `node'
95+
// [1] import.meta.filename
96+
// [2] import.meta.filename of function containing entry file
97+
const moduleFile = process.argv[2];
98+
const agent: Agent = await import(pathToFileURL(moduleFile!).pathname).then((module) => {
99+
const agent = module.default;
100+
if (agent === undefined || !isAgent(agent)) {
101+
throw new Error(`Unable to load agent: Missing or invalid default export in ${moduleFile}`);
102+
}
103+
return agent;
104+
});
105+
if (!agent.prewarm) {
106+
agent.prewarm = defaultInitializeProcessFunc;
116107
}
117-
initializeLogger(msg.value.loggerOptions);
118-
});
119-
const proc = new JobProcess();
120-
let logger = log().child({ pid: proc.pid });
121-
122-
logger.debug('initializing job runner');
123-
agent.prewarm(proc);
124-
logger.debug('job runner initialized');
125-
process.send({ case: 'initializeResponse' });
126108

127-
let job: JobTask | undefined = undefined;
128-
const closeEvent = new EventEmitter();
109+
// don't do anything on C-c
110+
// this is handled in cli, triggering a termination of all child processes at once.
111+
process.on('SIGINT', () => {});
129112

130-
const orphanedTimeout = setTimeout(() => {
131-
logger.warn('process orphaned, shutting down');
132-
process.exit();
133-
}, ORPHANED_TIMEOUT);
134-
135-
process.on('message', (msg: IPCMessage) => {
136-
switch (msg.case) {
137-
case 'pingRequest': {
138-
orphanedTimeout.refresh();
139-
process.send!({
140-
case: 'pongResponse',
141-
value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },
142-
});
143-
break;
113+
await once(process, 'message').then(([msg]: IPCMessage[]) => {
114+
msg = msg!;
115+
if (msg.case !== 'initializeRequest') {
116+
throw new Error('first message must be InitializeRequest');
144117
}
145-
case 'startJobRequest': {
146-
if (job) {
147-
throw new Error('job task already running');
118+
initializeLogger(msg.value.loggerOptions);
119+
});
120+
const proc = new JobProcess();
121+
let logger = log().child({ pid: proc.pid });
122+
123+
logger.debug('initializing job runner');
124+
agent.prewarm(proc);
125+
logger.debug('job runner initialized');
126+
process.send({ case: 'initializeResponse' });
127+
128+
let job: JobTask | undefined = undefined;
129+
const closeEvent = new EventEmitter();
130+
131+
const orphanedTimeout = setTimeout(() => {
132+
logger.warn('process orphaned, shutting down');
133+
process.exit();
134+
}, ORPHANED_TIMEOUT);
135+
136+
process.on('message', (msg: IPCMessage) => {
137+
switch (msg.case) {
138+
case 'pingRequest': {
139+
orphanedTimeout.refresh();
140+
process.send!({
141+
case: 'pongResponse',
142+
value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },
143+
});
144+
break;
148145
}
146+
case 'startJobRequest': {
147+
if (job) {
148+
throw new Error('job task already running');
149+
}
149150

150-
logger = logger.child({ jobID: msg.value.runningJob.job.id });
151+
logger = logger.child({ jobID: msg.value.runningJob.job.id });
151152

152-
job = startJob(proc, agent.entry, msg.value.runningJob, closeEvent, logger);
153-
logger.debug('job started');
154-
break;
155-
}
156-
case 'shutdownRequest': {
157-
if (!job) {
153+
job = startJob(proc, agent.entry, msg.value.runningJob, closeEvent, logger);
154+
logger.debug('job started');
158155
break;
159156
}
160-
closeEvent.emit('close', '');
157+
case 'shutdownRequest': {
158+
if (!job) {
159+
break;
160+
}
161+
closeEvent.emit('close', '');
162+
}
161163
}
162-
}
163-
});
164-
}
164+
});
165+
}
166+
})();

agents/tsup.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from 'tsup';
2+
3+
import defaults from '../tsup.config';
4+
5+
export default defineConfig({
6+
...defaults,
7+
});
8+

examples/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010
"minimal": "pnpm exec tsx src/multimodal_agent.ts"
1111
},
1212
"devDependencies": {
13+
"@types/node": "^22.5.5",
14+
"tsx": "^4.19.2",
1315
"typescript": "^5.0.0"
1416
},
1517
"dependencies": {
1618
"@livekit/agents": "workspace:*",
1719
"@livekit/agents-plugin-deepgram": "workspace:*",
1820
"@livekit/agents-plugin-elevenlabs": "workspace:*",
19-
"@livekit/agents-plugin-silero": "workspace:*",
2021
"@livekit/agents-plugin-openai": "workspace:*",
21-
"@livekit/rtc-node": "^0.11.1",
22+
"@livekit/agents-plugin-silero": "workspace:*",
23+
"@livekit/rtc-node": "^0.12.1",
2224
"zod": "^3.23.8"
2325
},
2426
"version": null

plugins/deepgram/package.json

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,45 @@
33
"version": "0.4.6",
44
"description": "Deepgram plugin for LiveKit Agents for Node.js",
55
"main": "dist/index.js",
6+
"require": "dist/index.cjs",
67
"types": "dist/index.d.ts",
8+
"exports": {
9+
".": {
10+
"types": "./dist/index.d.ts",
11+
"import": "./dist/index.js",
12+
"require": "./dist/index.cjs"
13+
}
14+
},
715
"author": "LiveKit",
816
"type": "module",
917
"repository": "[email protected]:livekit/agents-js.git",
1018
"license": "Apache-2.0",
1119
"files": [
1220
"dist",
13-
"src"
21+
"src",
22+
"README.md"
1423
],
1524
"scripts": {
16-
"build": "tsc",
25+
"build": "tsup --onSuccess \"tsc --declaration --emitDeclarationOnly\"",
1726
"clean": "rm -rf dist",
1827
"clean:build": "pnpm clean && pnpm build",
1928
"lint": "eslint -f unix \"src/**/*.{ts,js}\"",
2029
"api:check": "api-extractor run --typescript-compiler-folder ../../node_modules/typescript",
2130
"api:update": "api-extractor run --local --typescript-compiler-folder ../../node_modules/typescript --verbose"
2231
},
2332
"devDependencies": {
24-
"@microsoft/api-extractor": "^7.35.0",
2533
"@livekit/agents": "workspace:^",
26-
"@livekit/rtc-node": "^0.11.1",
34+
"@livekit/rtc-node": "^0.12.1",
35+
"@microsoft/api-extractor": "^7.35.0",
2736
"@types/ws": "^8.5.10",
37+
"tsup": "^8.3.5",
2838
"typescript": "^5.0.0"
2939
},
3040
"dependencies": {
3141
"ws": "^8.16.0"
3242
},
3343
"peerDependencies": {
3444
"@livekit/agents": "workspace:^",
35-
"@livekit/rtc-node": "^0.11.1"
45+
"@livekit/rtc-node": "^0.12.1"
3646
}
3747
}

plugins/deepgram/tsup.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'tsup';
2+
3+
import defaults from '../../tsup.config';
4+
5+
export default defineConfig({
6+
...defaults,
7+
});

plugins/elevenlabs/package.json

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,45 @@
33
"version": "0.4.6",
44
"description": "ElevenLabs plugin for LiveKit Node Agents",
55
"main": "dist/index.js",
6+
"require": "dist/index.cjs",
67
"types": "dist/index.d.ts",
8+
"exports": {
9+
".": {
10+
"types": "./dist/index.d.ts",
11+
"import": "./dist/index.js",
12+
"require": "./dist/index.cjs"
13+
}
14+
},
715
"author": "LiveKit",
816
"type": "module",
917
"repository": "[email protected]:livekit/agents-js.git",
1018
"license": "Apache-2.0",
1119
"files": [
1220
"dist",
13-
"src"
21+
"src",
22+
"README.md"
1423
],
1524
"scripts": {
16-
"build": "tsc",
25+
"build": "tsup --onSuccess \"tsc --declaration --emitDeclarationOnly\"",
1726
"clean": "rm -rf dist",
1827
"clean:build": "pnpm clean && pnpm build",
1928
"lint": "eslint -f unix \"src/**/*.{ts,js}\"",
2029
"api:check": "api-extractor run --typescript-compiler-folder ../../node_modules/typescript",
2130
"api:update": "api-extractor run --local --typescript-compiler-folder ../../node_modules/typescript --verbose"
2231
},
2332
"devDependencies": {
24-
"@microsoft/api-extractor": "^7.35.0",
2533
"@livekit/agents": "workspace:^",
26-
"@livekit/rtc-node": "^0.11.1",
34+
"@livekit/rtc-node": "^0.12.1",
35+
"@microsoft/api-extractor": "^7.35.0",
2736
"@types/ws": "^8.5.10",
37+
"tsup": "^8.3.5",
2838
"typescript": "^5.0.0"
2939
},
3040
"dependencies": {
3141
"ws": "^8.16.0"
3242
},
3343
"peerDependencies": {
3444
"@livekit/agents": "workspace:^",
35-
"@livekit/rtc-node": "^0.11.1"
45+
"@livekit/rtc-node": "^0.12.1"
3646
}
3747
}

0 commit comments

Comments
 (0)