Skip to content

Commit

Permalink
cli: add runHeadless function
Browse files Browse the repository at this point in the history
  • Loading branch information
nbsp authored Jun 30, 2024
1 parent 00dca94 commit 2514709
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 41 deletions.
2 changes: 1 addition & 1 deletion agents/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"typescript": "^5.0.0"
},
"dependencies": {
"@livekit/rtc-node": "^0.4.4",
"@livekit/rtc-node": "^0.5.0",
"@livekit/protocol": "^1.13.0",
"commander": "^12.0.0",
"livekit-server-sdk": "^2.1.2",
Expand Down
23 changes: 19 additions & 4 deletions agents/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import { Worker, type WorkerOptions } from './worker.js';

type CliArgs = {
opts: WorkerOptions;
logLevel: string;
production: boolean;
watch: boolean;
event?: EventEmitter;
};

const runWorker = async (args: CliArgs) => {
log.level = args.logLevel;
log.level = args.opts.logLevel;
const worker = new Worker(args.opts);

process.on('SIGINT', async () => {
Expand All @@ -41,7 +40,8 @@ export const runApp = (opts: WorkerOptions) => {
.addOption(
new Option('--log-level <level>', 'Set the logging level')
.choices(['trace', 'debug', 'info', 'warn', 'error', 'fatal'])
.default('trace'),
.default('info')
.env('LOG_LEVEL'),
)
.addOption(
new Option('--url <string>', 'LiveKit server or Cloud project websocket URL')
Expand All @@ -67,13 +67,28 @@ export const runApp = (opts: WorkerOptions) => {
opts.wsURL = options.url || opts.wsURL;
opts.apiKey = options.apiKey || opts.apiKey;
opts.apiSecret = options.apiSecret || opts.apiSecret;
opts.logLevel = options.logLevel || opts.logLevel;
runWorker({
opts,
production: true,
watch: false,
logLevel: options.logLevel,
});
});

program.parse();
};

// like runApp but without calling `start' in the CLI.
// useful for wrapped applications
export const runHeadless = (opts: WorkerOptions) => {
opts.wsURL = process.env.LIVEKIT_URL || opts.wsURL;
opts.apiKey = process.env.LIVEKIT_API_KEY || opts.apiKey;
opts.apiSecret = process.env.LIVEKIT_API_SECRET || opts.apiSecret;
opts.logLevel = process.env.LOG_LEVEL || opts.logLevel;

runWorker({
opts,
production: true,
watch: false,
});
};
4 changes: 2 additions & 2 deletions agents/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const mergeFrames = (buffer: AudioBuffer): AudioFrame => {
const sampleRate = buffer[0].sampleRate;
const channels = buffer[0].channels;
let samplesPerChannel = 0;
let data = new Uint16Array();
let data = new Int16Array();

for (const frame of buffer) {
if (frame.sampleRate !== sampleRate) {
Expand All @@ -31,7 +31,7 @@ export const mergeFrames = (buffer: AudioBuffer): AudioFrame => {
throw new TypeError('channel count mismatch');
}

data = new Uint16Array([...data, ...frame.data]);
data = new Int16Array([...data, ...frame.data]);
samplesPerChannel += frame.samplesPerChannel;
}

Expand Down
4 changes: 4 additions & 0 deletions agents/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class WorkerOptions {
apiSecret?: string;
host: string;
port: number;
logLevel: string;

constructor({
requestFunc,
Expand All @@ -82,6 +83,7 @@ export class WorkerOptions {
apiSecret = undefined,
host = 'localhost',
port = 8081,
logLevel = 'info',
}: {
requestFunc: (arg: JobRequest) => Promise<void>;
cpuLoadFunc?: () => number;
Expand All @@ -94,6 +96,7 @@ export class WorkerOptions {
apiSecret?: string;
host?: string;
port?: number;
logLevel?: string;
}) {
this.requestFunc = requestFunc;
this.cpuLoadFunc = cpuLoadFunc;
Expand All @@ -106,6 +109,7 @@ export class WorkerOptions {
this.apiSecret = apiSecret;
this.host = host;
this.port = port;
this.logLevel = logLevel;
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"dependencies": {
"@livekit/agents": "workspace:*",
"@livekit/agents-plugin-elevenlabs": "workspace:*",
"@livekit/rtc-node": "^0.4.4"
"@livekit/rtc-node": "^0.5.0"
}
}
2 changes: 1 addition & 1 deletion plugins/elevenlabs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"dependencies": {
"@livekit/agents": "workspace:*",
"@livekit/rtc-node": "^0.4.4",
"@livekit/rtc-node": "^0.5.0",
"ws": "^8.16.0"
}
}
4 changes: 2 additions & 2 deletions plugins/elevenlabs/src/tts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class SynthesizeStream extends tts.SynthesizeStream {
}).then((msg) => {
const json = JSON.parse(msg.toString());
if ('audio' in json) {
const data = new Uint16Array(Buffer.from(json.audio, 'base64'));
const data = new Int16Array(Buffer.from(json.audio, 'base64'));
const audioFrame = new AudioFrame(
data,
this.config.sampleRate,
Expand Down Expand Up @@ -317,7 +317,7 @@ class ChunkedStream extends tts.ChunkedStream {
{
text: this.text,
data: new AudioFrame(
new Uint16Array(data.buffer),
new Int16Array(data.buffer),
this.config.sampleRate,
1,
data.byteLength / 2,
Expand Down
58 changes: 29 additions & 29 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion turbo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://turborepo.org/schema.json",
"globalEnv": ["ELEVEN_API_KEY", "LIVEKIT_URL", "LIVEKIT_API_KEY", "LIVEKIT_API_SECRET"],
"globalEnv": ["ELEVEN_API_KEY", "LIVEKIT_URL", "LIVEKIT_API_KEY", "LIVEKIT_API_SECRET", "LOG_LEVEL"],
"pipeline": {
"build": {
"dependsOn": ["^build"],
Expand Down

0 comments on commit 2514709

Please sign in to comment.