@livekit/agents 0.7.2 → 0.7.4
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/audio.cjs +1 -1
- package/dist/audio.cjs.map +1 -1
- package/dist/audio.js +1 -1
- package/dist/audio.js.map +1 -1
- package/dist/cli.cjs.map +1 -1
- package/dist/constants.cjs +38 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.d.ts +5 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +11 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.cjs +14 -14
- package/dist/index.cjs.map +1 -1
- package/dist/ipc/inference_proc_lazy_main.cjs +14 -27
- package/dist/ipc/inference_proc_lazy_main.cjs.map +1 -1
- package/dist/ipc/inference_proc_lazy_main.js +14 -5
- package/dist/ipc/inference_proc_lazy_main.js.map +1 -1
- package/dist/ipc/job_proc_lazy_main.cjs +23 -10
- package/dist/ipc/job_proc_lazy_main.cjs.map +1 -1
- package/dist/ipc/job_proc_lazy_main.js +23 -10
- package/dist/ipc/job_proc_lazy_main.js.map +1 -1
- package/dist/ipc/supervised_proc.cjs +4 -5
- package/dist/ipc/supervised_proc.cjs.map +1 -1
- package/dist/ipc/supervised_proc.d.ts.map +1 -1
- package/dist/ipc/supervised_proc.js +4 -5
- package/dist/ipc/supervised_proc.js.map +1 -1
- package/dist/multimodal/agent_playout.cjs +1 -0
- package/dist/multimodal/agent_playout.cjs.map +1 -1
- package/dist/multimodal/agent_playout.js +1 -0
- package/dist/multimodal/agent_playout.js.map +1 -1
- package/dist/multimodal/multimodal_agent.cjs +36 -11
- package/dist/multimodal/multimodal_agent.cjs.map +1 -1
- package/dist/multimodal/multimodal_agent.d.ts +3 -2
- package/dist/multimodal/multimodal_agent.d.ts.map +1 -1
- package/dist/multimodal/multimodal_agent.js +40 -11
- package/dist/multimodal/multimodal_agent.js.map +1 -1
- package/dist/pipeline/agent_playout.cjs +1 -1
- package/dist/pipeline/agent_playout.cjs.map +1 -1
- package/dist/pipeline/agent_playout.d.ts.map +1 -1
- package/dist/pipeline/agent_playout.js +1 -1
- package/dist/pipeline/agent_playout.js.map +1 -1
- package/dist/pipeline/human_input.cjs +9 -2
- package/dist/pipeline/human_input.cjs.map +1 -1
- package/dist/pipeline/human_input.d.ts +2 -2
- package/dist/pipeline/human_input.d.ts.map +1 -1
- package/dist/pipeline/human_input.js +9 -2
- package/dist/pipeline/human_input.js.map +1 -1
- package/dist/pipeline/pipeline_agent.cjs +59 -37
- package/dist/pipeline/pipeline_agent.cjs.map +1 -1
- package/dist/pipeline/pipeline_agent.d.ts +3 -1
- package/dist/pipeline/pipeline_agent.d.ts.map +1 -1
- package/dist/pipeline/pipeline_agent.js +63 -37
- package/dist/pipeline/pipeline_agent.js.map +1 -1
- package/dist/worker.cjs +1 -1
- package/dist/worker.cjs.map +1 -1
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +1 -1
- package/dist/worker.js.map +1 -1
- package/package.json +4 -4
- package/src/audio.ts +1 -1
- package/src/constants.ts +7 -0
- package/src/ipc/inference_proc_lazy_main.ts +21 -6
- package/src/ipc/job_proc_lazy_main.ts +27 -9
- package/src/ipc/supervised_proc.ts +5 -6
- package/src/multimodal/multimodal_agent.ts +43 -13
- package/src/pipeline/agent_playout.ts +1 -7
- package/src/pipeline/human_input.ts +17 -3
- package/src/pipeline/pipeline_agent.ts +79 -38
- package/src/worker.ts +1 -1
- package/dist/llm/function_context.test.d.ts +0 -2
- package/dist/llm/function_context.test.d.ts.map +0 -1
- package/dist/tokenize/tokenizer.test.d.ts +0 -2
- package/dist/tokenize/tokenizer.test.d.ts.map +0 -1
package/dist/worker.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/worker.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type {\n JobAssignment,\n JobTermination,\n ParticipantInfo,\n TrackSource,\n} from '@livekit/protocol';\nimport {\n type AvailabilityRequest,\n JobType,\n ParticipantPermission,\n ServerMessage,\n WorkerMessage,\n WorkerStatus,\n} from '@livekit/protocol';\nimport { AccessToken, RoomServiceClient } from 'livekit-server-sdk';\nimport { EventEmitter } from 'node:events';\nimport os from 'node:os';\nimport { WebSocket } from 'ws';\nimport { HTTPServer } from './http_server.js';\nimport { InferenceRunner } from './inference_runner.js';\nimport { InferenceProcExecutor } from './ipc/inference_proc_executor.js';\nimport { ProcPool } from './ipc/proc_pool.js';\nimport type { JobAcceptArguments, JobProcess, RunningJobInfo } from './job.js';\nimport { JobRequest } from './job.js';\nimport { log } from './log.js';\nimport { Future } from './utils.js';\nimport { version } from './version.js';\n\nconst MAX_RECONNECT_ATTEMPTS = 10;\nconst ASSIGNMENT_TIMEOUT = 7.5 * 1000;\nconst UPDATE_LOAD_INTERVAL = 2.5 * 1000;\n\nclass Default {\n static loadThreshold(production: boolean): number {\n if (production) {\n return 0.65;\n } else {\n return Infinity;\n }\n }\n\n static numIdleProcesses(production: boolean): number {\n if (production) {\n return 3;\n } else {\n return 0;\n }\n }\n\n static port(production: boolean): number {\n if (production) {\n return 8081;\n } else {\n return 0;\n }\n }\n}\n\n/** Necessary credentials not provided and not found in an appropriate environmental variable. */\nexport class MissingCredentialsError extends Error {\n constructor(msg?: string) {\n super(msg);\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Worker did not run as expected. */\nexport class WorkerError extends Error {\n constructor(msg?: string) {\n super(msg);\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** @internal */\nexport const defaultInitializeProcessFunc = (_: JobProcess) => _;\nconst defaultRequestFunc = async (ctx: JobRequest) => {\n await ctx.accept();\n};\nconst defaultCpuLoad = async (): Promise<number> => {\n return new Promise((resolve) => {\n const cpus1 = os.cpus();\n\n setTimeout(() => {\n const cpus2 = os.cpus();\n\n let idle = 0;\n let total = 0;\n\n for (let i = 0; i < cpus1.length; i++) {\n const cpu1 = cpus1[i]!.times;\n const cpu2 = cpus2[i]!.times;\n\n idle += cpu2.idle - cpu1.idle;\n\n const total1 = Object.values(cpu1).reduce((acc, i) => acc + i, 0);\n const total2 = Object.values(cpu2).reduce((acc, i) => acc + i, 0);\n\n total += total2 - total1;\n }\n\n resolve(+(1 - idle / total).toFixed(2));\n }, UPDATE_LOAD_INTERVAL);\n });\n};\n\n/** Participant permissions to pass to every agent spun up by this worker. */\nexport class WorkerPermissions {\n canPublish: boolean;\n canSubscribe: boolean;\n canPublishData: boolean;\n canUpdateMetadata: boolean;\n canPublishSources: TrackSource[];\n hidden: boolean;\n\n constructor(\n canPublish = true,\n canSubscribe = true,\n canPublishData = true,\n canUpdateMetadata = true,\n canPublishSources: TrackSource[] = [],\n hidden = false,\n ) {\n this.canPublish = canPublish;\n this.canSubscribe = canSubscribe;\n this.canPublishData = canPublishData;\n this.canUpdateMetadata = canUpdateMetadata;\n this.canPublishSources = canPublishSources;\n this.hidden = hidden;\n }\n}\n\n/**\n * Data class describing worker behaviour.\n *\n * @remarks\n * The Agents framework provides sane worker defaults, and works out-of-the-box with no tweaking\n * necessary. The only mandatory parameter is `agent`, which points to the entry function.\n *\n * This class is mostly useful in conjunction with {@link cli.runApp}.\n */\nexport class WorkerOptions {\n agent: string;\n requestFunc: (job: JobRequest) => Promise<void>;\n loadFunc: () => Promise<number>;\n loadThreshold: number;\n numIdleProcesses: number;\n shutdownProcessTimeout: number;\n initializeProcessTimeout: number;\n permissions: WorkerPermissions;\n agentName: string;\n workerType: JobType;\n maxRetry: number;\n wsURL: string;\n apiKey?: string;\n apiSecret?: string;\n host: string;\n port: number;\n logLevel: string;\n production: boolean;\n jobMemoryWarnMB: number;\n jobMemoryLimitMB: number;\n\n /** @param options */\n constructor({\n agent,\n requestFunc = defaultRequestFunc,\n loadFunc = defaultCpuLoad,\n loadThreshold = undefined,\n numIdleProcesses = undefined,\n shutdownProcessTimeout = 60 * 1000,\n initializeProcessTimeout = 10 * 1000,\n permissions = new WorkerPermissions(),\n agentName = '',\n workerType = JobType.JT_ROOM,\n maxRetry = MAX_RECONNECT_ATTEMPTS,\n wsURL = 'ws://localhost:7880',\n apiKey = undefined,\n apiSecret = undefined,\n host = 'localhost',\n port = undefined,\n logLevel = 'info',\n production = false,\n jobMemoryWarnMB = 300,\n jobMemoryLimitMB = 0,\n }: {\n /**\n * Path to a file that has {@link Agent} as a default export, dynamically imported later for\n * entrypoint and prewarm functions\n */\n agent: string;\n requestFunc?: (job: JobRequest) => Promise<void>;\n /** Called to determine the current load of the worker. Should return a value between 0 and 1. */\n loadFunc?: () => Promise<number>;\n /** When the load exceeds this threshold, the worker will be marked as unavailable. */\n loadThreshold?: number;\n numIdleProcesses?: number;\n shutdownProcessTimeout?: number;\n initializeProcessTimeout?: number;\n permissions?: WorkerPermissions;\n agentName?: string;\n workerType?: JobType;\n maxRetry?: number;\n wsURL?: string;\n apiKey?: string;\n apiSecret?: string;\n host?: string;\n port?: number;\n logLevel?: string;\n production?: boolean;\n jobMemoryWarnMB?: number;\n jobMemoryLimitMB?: number;\n }) {\n this.agent = agent;\n if (!this.agent) {\n throw new Error('No Agent file was passed to the worker');\n }\n this.requestFunc = requestFunc;\n this.loadFunc = loadFunc;\n this.loadThreshold = loadThreshold || Default.loadThreshold(production);\n this.numIdleProcesses = numIdleProcesses || Default.numIdleProcesses(production);\n this.shutdownProcessTimeout = shutdownProcessTimeout;\n this.initializeProcessTimeout = initializeProcessTimeout;\n this.permissions = permissions;\n this.agentName = agentName;\n this.workerType = workerType;\n this.maxRetry = maxRetry;\n this.wsURL = wsURL;\n this.apiKey = apiKey;\n this.apiSecret = apiSecret;\n this.host = host;\n this.port = port || Default.port(production);\n this.logLevel = logLevel;\n this.production = production;\n this.jobMemoryWarnMB = jobMemoryWarnMB;\n this.jobMemoryLimitMB = jobMemoryLimitMB;\n }\n}\n\nclass PendingAssignment {\n promise = new Promise<JobAssignment>((resolve) => {\n this.resolve = resolve; // this is how JavaScript lets you resolve promises externally\n });\n resolve(arg: JobAssignment) {\n arg; // useless call to counteract TypeScript E6133\n }\n}\n\n/**\n * Central orchestrator for all processes and job requests.\n *\n * @remarks\n * For most usecases, Worker should not be initialized or handled directly; you should instead call\n * for its creation through {@link cli.runApp}. This could, however, be useful in situations where\n * you don't have access to a command line, such as a headless program, or one that uses Agents\n * behind a wrapper.\n */\nexport class Worker {\n #opts: WorkerOptions;\n #procPool: ProcPool;\n\n #id = 'unregistered';\n #closed = true;\n #draining = false;\n #connecting = false;\n #tasks: Promise<void>[] = [];\n #pending: { [id: string]: PendingAssignment } = {};\n #close = new Future();\n\n event = new EventEmitter();\n #session: WebSocket | undefined = undefined;\n #httpServer: HTTPServer;\n #logger = log().child({ version });\n #inferenceExecutor?: InferenceProcExecutor;\n\n /* @throws {@link MissingCredentialsError} if URL, API key or API secret are missing */\n constructor(opts: WorkerOptions) {\n opts.wsURL = opts.wsURL || process.env.LIVEKIT_URL || '';\n opts.apiKey = opts.apiKey || process.env.LIVEKIT_API_KEY || '';\n opts.apiSecret = opts.apiSecret || process.env.LIVEKIT_API_SECRET || '';\n\n if (opts.wsURL === '')\n throw new MissingCredentialsError(\n 'URL is required: Set LIVEKIT_URL, run with --url, or pass wsURL in WorkerOptions',\n );\n if (opts.apiKey === '')\n throw new MissingCredentialsError(\n 'API Key is required: Set LIVEKIT_API_KEY, run with --api-key, or pass apiKey in WorkerOptions',\n );\n if (opts.apiSecret === '')\n throw new MissingCredentialsError(\n 'API Secret is required: Set LIVEKIT_API_SECRET, run with --api-secret, or pass apiSecret in WorkerOptions',\n );\n\n if (Object.entries(InferenceRunner.registeredRunners).length) {\n this.#inferenceExecutor = new InferenceProcExecutor({\n runners: InferenceRunner.registeredRunners,\n initializeTimeout: 30000,\n closeTimeout: 5000,\n memoryWarnMB: 2000,\n memoryLimitMB: 0,\n pingInterval: 5000,\n pingTimeout: 60000,\n highPingThreshold: 2500,\n });\n }\n\n this.#procPool = new ProcPool(\n opts.agent,\n opts.numIdleProcesses,\n opts.initializeProcessTimeout,\n opts.shutdownProcessTimeout,\n this.#inferenceExecutor,\n opts.jobMemoryWarnMB,\n opts.jobMemoryLimitMB,\n );\n\n this.#opts = opts;\n this.#httpServer = new HTTPServer(opts.host, opts.port, () => ({\n agent_name: opts.agentName,\n worker_type: JobType[opts.workerType],\n active_jobs: this.activeJobs.length,\n }));\n }\n\n /* @throws {@link WorkerError} if worker failed to connect or already running */\n async run() {\n if (!this.#closed) {\n throw new WorkerError('worker is already running');\n }\n\n if (this.#inferenceExecutor) {\n await this.#inferenceExecutor.start();\n await this.#inferenceExecutor.initialize();\n }\n\n this.#logger.info('starting worker');\n this.#closed = false;\n this.#procPool.start();\n\n const workerWS = async () => {\n let retries = 0;\n this.#connecting = true;\n\n while (!this.#closed) {\n const url = new URL(this.#opts.wsURL);\n url.protocol = url.protocol.replace('http', 'ws');\n const token = new AccessToken(this.#opts.apiKey, this.#opts.apiSecret);\n token.addGrant({ agent: true });\n const jwt = await token.toJwt();\n this.#session = new WebSocket(url + 'agent', {\n headers: { authorization: 'Bearer ' + jwt },\n });\n\n try {\n await new Promise((resolve, reject) => {\n this.#session!.on('open', resolve);\n this.#session!.on('error', (error) => reject(error.message));\n this.#session!.on('close', (code) => reject(`WebSocket returned ${code}`));\n });\n\n retries = 0;\n this.#logger.debug('connected to LiveKit server');\n await this.#runWS(this.#session);\n } catch (e: unknown) {\n if (e instanceof Error || e instanceof ErrorEvent) {\n e = e.message;\n }\n\n if (this.#closed) return;\n if (retries >= this.#opts.maxRetry) {\n throw new WorkerError(\n `failed to connect to LiveKit server after ${retries} attempts: ${e}`,\n );\n }\n\n retries++;\n const delay = Math.min(retries * 2, 10);\n\n this.#logger.warn(\n `failed to connect to LiveKit server, retrying in ${delay} seconds: ${e} (${retries}/${this.#opts.maxRetry})`,\n );\n\n await new Promise((resolve) => setTimeout(resolve, delay * 1000));\n }\n }\n };\n\n await Promise.all([workerWS(), this.#httpServer.run()]);\n this.#close.resolve();\n }\n\n get id(): string {\n return this.#id;\n }\n\n get activeJobs(): RunningJobInfo[] {\n return this.#procPool.processes\n .filter((proc) => proc.runningJob)\n .map((proc) => proc.runningJob!);\n }\n\n /* @throws {@link WorkerError} if worker did not drain in time */\n async drain(timeout?: number) {\n if (this.#draining) {\n return;\n }\n\n this.#logger.info('draining worker');\n this.#draining = true;\n\n this.event.emit(\n 'worker_msg',\n new WorkerMessage({\n message: {\n case: 'updateWorker',\n value: {\n status: WorkerStatus.WS_FULL,\n },\n },\n }),\n );\n\n const joinJobs = async () => {\n return Promise.all(\n this.#procPool.processes.map((proc) => {\n if (!proc.runningJob) {\n proc.close();\n }\n return proc.join();\n }),\n );\n };\n\n let timer: NodeJS.Timeout | undefined;\n if (timeout) {\n timer = setTimeout(() => {\n throw new WorkerError('timed out draining');\n }, timeout);\n }\n await joinJobs().then(() => {\n if (timeout) {\n clearTimeout(timer);\n }\n });\n }\n\n async simulateJob(roomName: string, participantIdentity?: string) {\n const client = new RoomServiceClient(this.#opts.wsURL, this.#opts.apiKey, this.#opts.apiSecret);\n const room = await client.createRoom({ name: roomName });\n let participant: ParticipantInfo | undefined = undefined;\n if (participantIdentity) {\n try {\n participant = await client.getParticipant(roomName, participantIdentity);\n } catch (e) {\n this.#logger.fatal(\n `participant with identity ${participantIdentity} not found in room ${roomName}`,\n );\n throw e;\n }\n }\n\n this.event.emit(\n 'worker_msg',\n new WorkerMessage({\n message: {\n case: 'simulateJob',\n value: {\n type: JobType.JT_PUBLISHER,\n room,\n participant,\n },\n },\n }),\n );\n }\n\n async #runWS(ws: WebSocket) {\n let closingWS = false;\n\n const send = (msg: WorkerMessage) => {\n if (closingWS) {\n this.event.off('worker_msg', send);\n return;\n }\n ws.send(msg.toBinary());\n };\n this.event.on('worker_msg', send);\n\n const close = new Promise<void>((resolve) => {\n ws.addEventListener('close', () => {\n closingWS = true;\n if (!this.#closed) {\n this.#logger.error('worker connection closed unexpectedly');\n }\n resolve();\n });\n });\n\n ws.addEventListener('error', (event) => {\n this.#logger.error('worker error:', event.message);\n });\n\n ws.addEventListener('message', (event) => {\n if (event.type !== 'message') {\n this.#logger.warn('unexpected message type: ' + event.type);\n return;\n }\n\n const msg = new ServerMessage();\n msg.fromBinary(event.data as Uint8Array);\n\n // register is the only valid first message, and it is only valid as the\n // first message\n if (this.#connecting && msg.message.case !== 'register') {\n throw new WorkerError('expected register response as first message');\n }\n\n switch (msg.message.case) {\n case 'register': {\n this.#id = msg.message.value.workerId;\n this.#logger\n .child({ id: this.id, server_info: msg.message.value.serverInfo })\n .info('registered worker');\n this.event.emit(\n 'worker_registered',\n msg.message.value.workerId,\n msg.message.value.serverInfo,\n );\n this.#connecting = false;\n break;\n }\n case 'availability': {\n if (!msg.message.value.job) return;\n const task = this.#availability(msg.message.value);\n this.#tasks.push(task);\n task.finally(() => this.#tasks.splice(this.#tasks.indexOf(task)));\n break;\n }\n case 'assignment': {\n if (!msg.message.value.job) return;\n const job = msg.message.value.job;\n if (job.id in this.#pending) {\n const task = this.#pending[job.id];\n delete this.#pending[job.id];\n task?.resolve(msg.message.value);\n } else {\n this.#logger.child({ job }).warn('received assignment for unknown job ' + job.id);\n }\n break;\n }\n case 'termination': {\n const task = this.#termination(msg.message.value);\n this.#tasks.push(task);\n task.finally(() => this.#tasks.splice(this.#tasks.indexOf(task)));\n break;\n }\n }\n });\n\n this.event.emit(\n 'worker_msg',\n new WorkerMessage({\n message: {\n case: 'register',\n value: {\n type: this.#opts.workerType,\n agentName: this.#opts.agentName,\n allowedPermissions: new ParticipantPermission({\n canPublish: this.#opts.permissions.canPublish,\n canSubscribe: this.#opts.permissions.canSubscribe,\n canPublishData: this.#opts.permissions.canPublishData,\n canUpdateMetadata: this.#opts.permissions.canUpdateMetadata,\n hidden: this.#opts.permissions.hidden,\n agent: true,\n }),\n version,\n },\n },\n }),\n );\n\n let currentStatus = WorkerStatus.WS_AVAILABLE;\n const loadMonitor = setInterval(() => {\n if (closingWS) clearInterval(loadMonitor);\n\n const oldStatus = currentStatus;\n this.#opts.loadFunc().then((currentLoad: number) => {\n const isFull = currentLoad >= this.#opts.loadThreshold;\n const currentlyAvailable = !isFull;\n currentStatus = currentlyAvailable ? WorkerStatus.WS_AVAILABLE : WorkerStatus.WS_FULL;\n\n if (oldStatus != currentStatus) {\n const extra = { load: currentLoad, loadThreshold: this.#opts.loadThreshold };\n if (isFull) {\n this.#logger.child(extra).info('worker is at full capacity, marking as unavailable');\n } else {\n this.#logger.child(extra).info('worker is below capacity, marking as available');\n }\n }\n\n this.event.emit(\n 'worker_msg',\n new WorkerMessage({\n message: {\n case: 'updateWorker',\n value: {\n load: currentLoad,\n status: currentStatus,\n },\n },\n }),\n );\n });\n }, UPDATE_LOAD_INTERVAL);\n\n await close;\n ws.removeAllListeners();\n }\n\n async #availability(msg: AvailabilityRequest) {\n let answered = false;\n\n const onReject = async () => {\n answered = true;\n this.event.emit(\n 'worker_msg',\n new WorkerMessage({\n message: {\n case: 'availability',\n value: {\n jobId: msg.job!.id,\n available: false,\n },\n },\n }),\n );\n };\n\n const onAccept = async (args: JobAcceptArguments) => {\n answered = true;\n\n this.event.emit(\n 'worker_msg',\n new WorkerMessage({\n message: {\n case: 'availability',\n value: {\n jobId: msg.job!.id,\n available: true,\n participantIdentity: args.identity,\n participantName: args.name,\n participantMetadata: args.metadata,\n participantAttributes: args.attributes,\n },\n },\n }),\n );\n\n this.#pending[req.id] = new PendingAssignment();\n const timer = setTimeout(() => {\n this.#logger.child({ req }).warn(`assignment for job ${req.id} timed out`);\n return;\n }, ASSIGNMENT_TIMEOUT);\n const asgn = await this.#pending[req.id]?.promise.then(async (asgn) => {\n clearTimeout(timer);\n return asgn;\n });\n\n if (asgn) {\n await this.#procPool.launchJob({\n acceptArguments: args,\n job: msg.job!,\n url: asgn.url || this.#opts.wsURL,\n token: asgn.token,\n });\n } else {\n this.#logger.child({ requestId: req.id }).warn('pending assignment not found');\n }\n };\n\n const req = new JobRequest(msg.job!, onReject, onAccept);\n this.#logger\n .child({ job: msg.job, resuming: msg.resuming, agentName: this.#opts.agentName })\n .info('received job request');\n\n const jobRequestTask = async () => {\n try {\n await this.#opts.requestFunc(req);\n } catch (e) {\n this.#logger\n .child({ job: msg.job, resuming: msg.resuming, agentName: this.#opts.agentName })\n .info('jobRequestFunc failed');\n await onReject();\n }\n\n if (!answered) {\n this.#logger\n .child({ job: msg.job, resuming: msg.resuming, agentName: this.#opts.agentName })\n .info('no answer was given inside the jobRequestFunc, automatically rejecting the job');\n }\n };\n\n const task = jobRequestTask();\n this.#tasks.push(task);\n task.finally(() => this.#tasks.splice(this.#tasks.indexOf(task)));\n }\n\n async #termination(msg: JobTermination) {\n const proc = this.#procPool.getByJobId(msg.jobId);\n if (proc === null) {\n // safe to ignore\n return;\n }\n await proc.close();\n }\n\n async close() {\n if (this.#closed) {\n await this.#close.await;\n return;\n }\n\n this.#logger.info('shutting down worker');\n\n this.#closed = true;\n\n await this.#inferenceExecutor?.close();\n await this.#procPool.close();\n await this.#httpServer.close();\n await Promise.allSettled(this.#tasks);\n\n this.#session?.close();\n await this.#close.await;\n }\n}\n"],"mappings":"AASA;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,aAAa,yBAAyB;AAC/C,SAAS,oBAAoB;AAC7B,OAAO,QAAQ;AACf,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAC3B,SAAS,uBAAuB;AAChC,SAAS,6BAA6B;AACtC,SAAS,gBAAgB;AAEzB,SAAS,kBAAkB;AAC3B,SAAS,WAAW;AACpB,SAAS,cAAc;AACvB,SAAS,eAAe;AAExB,MAAM,yBAAyB;AAC/B,MAAM,qBAAqB,MAAM;AACjC,MAAM,uBAAuB,MAAM;AAEnC,MAAM,QAAQ;AAAA,EACZ,OAAO,cAAc,YAA6B;AAChD,QAAI,YAAY;AACd,aAAO;AAAA,IACT,OAAO;AACL,aAAO;AAAA,IACT;AAAA,EACF;AAAA,EAEA,OAAO,iBAAiB,YAA6B;AACnD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AM,EAAE,KAAK,IAAI,KAAK,UAAU,IAAI,UAAU,WAAW,KAAK,MAAM,UAAU,CAAC,EAC/E,KAAK,uBAAuB;AAC/B,cAAM,SAAS;AAAA,MACjB;AAEA,UAAI,CAAC,UAAU;AACb,aAAK,QACF,MAAM,EAAE,KAAK,IAAI,KAAK,UAAU,IAAI,UAAU,WAAW,KAAK,MAAM,UAAU,CAAC,EAC/E,KAAK,gFAAgF;AAAA,MAC1F;AAAA,IACF;AAEA,UAAM,OAAO,eAAe;AAC5B,SAAK,OAAO,KAAK,IAAI;AACrB,SAAK,QAAQ,MAAM,KAAK,OAAO,OAAO,KAAK,OAAO,QAAQ,IAAI,CAAC,CAAC;AAAA,EAClE;AAAA,EAEA,MAAM,aAAa,KAAqB;AACtC,UAAM,OAAO,KAAK,UAAU,WAAW,IAAI,KAAK;AAChD,QAAI,SAAS,MAAM;AAEjB;AAAA,IACF;AACA,UAAM,KAAK,MAAM;AAAA,EACnB;AAAA,EAEA,MAAM,QAAQ;AAhtBhB;AAitBI,QAAI,KAAK,SAAS;AAChB,YAAM,KAAK,OAAO;AAClB;AAAA,IACF;AAEA,SAAK,QAAQ,KAAK,sBAAsB;AAExC,SAAK,UAAU;AAEf,YAAM,UAAK,uBAAL,mBAAyB;AAC/B,UAAM,KAAK,UAAU,MAAM;AAC3B,UAAM,KAAK,YAAY,MAAM;AAC7B,UAAM,QAAQ,WAAW,KAAK,MAAM;AAEpC,eAAK,aAAL,mBAAe;AACf,UAAM,KAAK,OAAO;AAAA,EACpB;AACF;","names":["i","asgn"]}
|
|
1
|
+
{"version":3,"sources":["../src/worker.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type {\n JobAssignment,\n JobTermination,\n ParticipantInfo,\n TrackSource,\n} from '@livekit/protocol';\nimport {\n type AvailabilityRequest,\n JobType,\n ParticipantPermission,\n ServerMessage,\n WorkerMessage,\n WorkerStatus,\n} from '@livekit/protocol';\nimport { AccessToken, RoomServiceClient } from 'livekit-server-sdk';\nimport { EventEmitter } from 'node:events';\nimport os from 'node:os';\nimport { WebSocket } from 'ws';\nimport { HTTPServer } from './http_server.js';\nimport { InferenceRunner } from './inference_runner.js';\nimport { InferenceProcExecutor } from './ipc/inference_proc_executor.js';\nimport { ProcPool } from './ipc/proc_pool.js';\nimport type { JobAcceptArguments, JobProcess, RunningJobInfo } from './job.js';\nimport { JobRequest } from './job.js';\nimport { log } from './log.js';\nimport { Future } from './utils.js';\nimport { version } from './version.js';\n\nconst MAX_RECONNECT_ATTEMPTS = 10;\nconst ASSIGNMENT_TIMEOUT = 7.5 * 1000;\nconst UPDATE_LOAD_INTERVAL = 2.5 * 1000;\n\nclass Default {\n static loadThreshold(production: boolean): number {\n if (production) {\n return 0.65;\n } else {\n return Infinity;\n }\n }\n\n static numIdleProcesses(production: boolean): number {\n if (production) {\n return 3;\n } else {\n return 0;\n }\n }\n\n static port(production: boolean): number {\n if (production) {\n return 8081;\n } else {\n return 0;\n }\n }\n}\n\n/** Necessary credentials not provided and not found in an appropriate environmental variable. */\nexport class MissingCredentialsError extends Error {\n constructor(msg?: string) {\n super(msg);\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Worker did not run as expected. */\nexport class WorkerError extends Error {\n constructor(msg?: string) {\n super(msg);\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** @internal */\nexport const defaultInitializeProcessFunc = (_: JobProcess) => _;\nconst defaultRequestFunc = async (ctx: JobRequest) => {\n await ctx.accept();\n};\nconst defaultCpuLoad = async (): Promise<number> => {\n return new Promise((resolve) => {\n const cpus1 = os.cpus();\n\n setTimeout(() => {\n const cpus2 = os.cpus();\n\n let idle = 0;\n let total = 0;\n\n for (let i = 0; i < cpus1.length; i++) {\n const cpu1 = cpus1[i]!.times;\n const cpu2 = cpus2[i]!.times;\n\n idle += cpu2.idle - cpu1.idle;\n\n const total1 = Object.values(cpu1).reduce((acc, i) => acc + i, 0);\n const total2 = Object.values(cpu2).reduce((acc, i) => acc + i, 0);\n\n total += total2 - total1;\n }\n\n resolve(+(1 - idle / total).toFixed(2));\n }, UPDATE_LOAD_INTERVAL);\n });\n};\n\n/** Participant permissions to pass to every agent spun up by this worker. */\nexport class WorkerPermissions {\n canPublish: boolean;\n canSubscribe: boolean;\n canPublishData: boolean;\n canUpdateMetadata: boolean;\n canPublishSources: TrackSource[];\n hidden: boolean;\n\n constructor(\n canPublish = true,\n canSubscribe = true,\n canPublishData = true,\n canUpdateMetadata = true,\n canPublishSources: TrackSource[] = [],\n hidden = false,\n ) {\n this.canPublish = canPublish;\n this.canSubscribe = canSubscribe;\n this.canPublishData = canPublishData;\n this.canUpdateMetadata = canUpdateMetadata;\n this.canPublishSources = canPublishSources;\n this.hidden = hidden;\n }\n}\n\n/**\n * Data class describing worker behaviour.\n *\n * @remarks\n * The Agents framework provides sane worker defaults, and works out-of-the-box with no tweaking\n * necessary. The only mandatory parameter is `agent`, which points to the entry function.\n *\n * This class is mostly useful in conjunction with {@link cli.runApp}.\n */\nexport class WorkerOptions {\n agent: string;\n requestFunc: (job: JobRequest) => Promise<void>;\n loadFunc: () => Promise<number>;\n loadThreshold: number;\n numIdleProcesses: number;\n shutdownProcessTimeout: number;\n initializeProcessTimeout: number;\n permissions: WorkerPermissions;\n agentName: string;\n workerType: JobType;\n maxRetry: number;\n wsURL: string;\n apiKey?: string;\n apiSecret?: string;\n host: string;\n port: number;\n logLevel: string;\n production: boolean;\n jobMemoryWarnMB: number;\n jobMemoryLimitMB: number;\n\n /** @param options */\n constructor({\n agent,\n requestFunc = defaultRequestFunc,\n loadFunc = defaultCpuLoad,\n loadThreshold = undefined,\n numIdleProcesses = undefined,\n shutdownProcessTimeout = 60 * 1000,\n initializeProcessTimeout = 10 * 1000,\n permissions = new WorkerPermissions(),\n agentName = '',\n workerType = JobType.JT_ROOM,\n maxRetry = MAX_RECONNECT_ATTEMPTS,\n wsURL = 'ws://localhost:7880',\n apiKey = undefined,\n apiSecret = undefined,\n host = '0.0.0.0',\n port = undefined,\n logLevel = 'info',\n production = false,\n jobMemoryWarnMB = 300,\n jobMemoryLimitMB = 0,\n }: {\n /**\n * Path to a file that has {@link Agent} as a default export, dynamically imported later for\n * entrypoint and prewarm functions\n */\n agent: string;\n requestFunc?: (job: JobRequest) => Promise<void>;\n /** Called to determine the current load of the worker. Should return a value between 0 and 1. */\n loadFunc?: () => Promise<number>;\n /** When the load exceeds this threshold, the worker will be marked as unavailable. */\n loadThreshold?: number;\n numIdleProcesses?: number;\n shutdownProcessTimeout?: number;\n initializeProcessTimeout?: number;\n permissions?: WorkerPermissions;\n agentName?: string;\n workerType?: JobType;\n maxRetry?: number;\n wsURL?: string;\n apiKey?: string;\n apiSecret?: string;\n host?: string;\n port?: number;\n logLevel?: string;\n production?: boolean;\n jobMemoryWarnMB?: number;\n jobMemoryLimitMB?: number;\n }) {\n this.agent = agent;\n if (!this.agent) {\n throw new Error('No Agent file was passed to the worker');\n }\n this.requestFunc = requestFunc;\n this.loadFunc = loadFunc;\n this.loadThreshold = loadThreshold || Default.loadThreshold(production);\n this.numIdleProcesses = numIdleProcesses || Default.numIdleProcesses(production);\n this.shutdownProcessTimeout = shutdownProcessTimeout;\n this.initializeProcessTimeout = initializeProcessTimeout;\n this.permissions = permissions;\n this.agentName = agentName;\n this.workerType = workerType;\n this.maxRetry = maxRetry;\n this.wsURL = wsURL;\n this.apiKey = apiKey;\n this.apiSecret = apiSecret;\n this.host = host;\n this.port = port || Default.port(production);\n this.logLevel = logLevel;\n this.production = production;\n this.jobMemoryWarnMB = jobMemoryWarnMB;\n this.jobMemoryLimitMB = jobMemoryLimitMB;\n }\n}\n\nclass PendingAssignment {\n promise = new Promise<JobAssignment>((resolve) => {\n this.resolve = resolve; // this is how JavaScript lets you resolve promises externally\n });\n resolve(arg: JobAssignment) {\n arg; // useless call to counteract TypeScript E6133\n }\n}\n\n/**\n * Central orchestrator for all processes and job requests.\n *\n * @remarks\n * For most usecases, Worker should not be initialized or handled directly; you should instead call\n * for its creation through {@link cli.runApp}. This could, however, be useful in situations where\n * you don't have access to a command line, such as a headless program, or one that uses Agents\n * behind a wrapper.\n */\nexport class Worker {\n #opts: WorkerOptions;\n #procPool: ProcPool;\n\n #id = 'unregistered';\n #closed = true;\n #draining = false;\n #connecting = false;\n #tasks: Promise<void>[] = [];\n #pending: { [id: string]: PendingAssignment } = {};\n #close = new Future();\n\n event = new EventEmitter();\n #session: WebSocket | undefined = undefined;\n #httpServer: HTTPServer;\n #logger = log().child({ version });\n #inferenceExecutor?: InferenceProcExecutor;\n\n /* @throws {@link MissingCredentialsError} if URL, API key or API secret are missing */\n constructor(opts: WorkerOptions) {\n opts.wsURL = opts.wsURL || process.env.LIVEKIT_URL || '';\n opts.apiKey = opts.apiKey || process.env.LIVEKIT_API_KEY || '';\n opts.apiSecret = opts.apiSecret || process.env.LIVEKIT_API_SECRET || '';\n\n if (opts.wsURL === '')\n throw new MissingCredentialsError(\n 'URL is required: Set LIVEKIT_URL, run with --url, or pass wsURL in WorkerOptions',\n );\n if (opts.apiKey === '')\n throw new MissingCredentialsError(\n 'API Key is required: Set LIVEKIT_API_KEY, run with --api-key, or pass apiKey in WorkerOptions',\n );\n if (opts.apiSecret === '')\n throw new MissingCredentialsError(\n 'API Secret is required: Set LIVEKIT_API_SECRET, run with --api-secret, or pass apiSecret in WorkerOptions',\n );\n\n if (Object.entries(InferenceRunner.registeredRunners).length) {\n this.#inferenceExecutor = new InferenceProcExecutor({\n runners: InferenceRunner.registeredRunners,\n initializeTimeout: 30000,\n closeTimeout: 5000,\n memoryWarnMB: 2000,\n memoryLimitMB: 0,\n pingInterval: 5000,\n pingTimeout: 60000,\n highPingThreshold: 2500,\n });\n }\n\n this.#procPool = new ProcPool(\n opts.agent,\n opts.numIdleProcesses,\n opts.initializeProcessTimeout,\n opts.shutdownProcessTimeout,\n this.#inferenceExecutor,\n opts.jobMemoryWarnMB,\n opts.jobMemoryLimitMB,\n );\n\n this.#opts = opts;\n this.#httpServer = new HTTPServer(opts.host, opts.port, () => ({\n agent_name: opts.agentName,\n worker_type: JobType[opts.workerType],\n active_jobs: this.activeJobs.length,\n }));\n }\n\n /* @throws {@link WorkerError} if worker failed to connect or already running */\n async run() {\n if (!this.#closed) {\n throw new WorkerError('worker is already running');\n }\n\n if (this.#inferenceExecutor) {\n await this.#inferenceExecutor.start();\n await this.#inferenceExecutor.initialize();\n }\n\n this.#logger.info('starting worker');\n this.#closed = false;\n this.#procPool.start();\n\n const workerWS = async () => {\n let retries = 0;\n this.#connecting = true;\n\n while (!this.#closed) {\n const url = new URL(this.#opts.wsURL);\n url.protocol = url.protocol.replace('http', 'ws');\n const token = new AccessToken(this.#opts.apiKey, this.#opts.apiSecret);\n token.addGrant({ agent: true });\n const jwt = await token.toJwt();\n this.#session = new WebSocket(url + 'agent', {\n headers: { authorization: 'Bearer ' + jwt },\n });\n\n try {\n await new Promise((resolve, reject) => {\n this.#session!.on('open', resolve);\n this.#session!.on('error', (error) => reject(error.message));\n this.#session!.on('close', (code) => reject(`WebSocket returned ${code}`));\n });\n\n retries = 0;\n this.#logger.debug('connected to LiveKit server');\n await this.#runWS(this.#session);\n } catch (e: unknown) {\n if (e instanceof Error || e instanceof ErrorEvent) {\n e = e.message;\n }\n\n if (this.#closed) return;\n if (retries >= this.#opts.maxRetry) {\n throw new WorkerError(\n `failed to connect to LiveKit server after ${retries} attempts: ${e}`,\n );\n }\n\n retries++;\n const delay = Math.min(retries * 2, 10);\n\n this.#logger.warn(\n `failed to connect to LiveKit server, retrying in ${delay} seconds: ${e} (${retries}/${this.#opts.maxRetry})`,\n );\n\n await new Promise((resolve) => setTimeout(resolve, delay * 1000));\n }\n }\n };\n\n await Promise.all([workerWS(), this.#httpServer.run()]);\n this.#close.resolve();\n }\n\n get id(): string {\n return this.#id;\n }\n\n get activeJobs(): RunningJobInfo[] {\n return this.#procPool.processes\n .filter((proc) => proc.runningJob)\n .map((proc) => proc.runningJob!);\n }\n\n /* @throws {@link WorkerError} if worker did not drain in time */\n async drain(timeout?: number) {\n if (this.#draining) {\n return;\n }\n\n this.#logger.info('draining worker');\n this.#draining = true;\n\n this.event.emit(\n 'worker_msg',\n new WorkerMessage({\n message: {\n case: 'updateWorker',\n value: {\n status: WorkerStatus.WS_FULL,\n },\n },\n }),\n );\n\n const joinJobs = async () => {\n return Promise.all(\n this.#procPool.processes.map((proc) => {\n if (!proc.runningJob) {\n proc.close();\n }\n return proc.join();\n }),\n );\n };\n\n let timer: NodeJS.Timeout | undefined;\n if (timeout) {\n timer = setTimeout(() => {\n throw new WorkerError('timed out draining');\n }, timeout);\n }\n await joinJobs().then(() => {\n if (timeout) {\n clearTimeout(timer);\n }\n });\n }\n\n async simulateJob(roomName: string, participantIdentity?: string) {\n const client = new RoomServiceClient(this.#opts.wsURL, this.#opts.apiKey, this.#opts.apiSecret);\n const room = await client.createRoom({ name: roomName });\n let participant: ParticipantInfo | undefined = undefined;\n if (participantIdentity) {\n try {\n participant = await client.getParticipant(roomName, participantIdentity);\n } catch (e) {\n this.#logger.fatal(\n `participant with identity ${participantIdentity} not found in room ${roomName}`,\n );\n throw e;\n }\n }\n\n this.event.emit(\n 'worker_msg',\n new WorkerMessage({\n message: {\n case: 'simulateJob',\n value: {\n type: JobType.JT_PUBLISHER,\n room,\n participant,\n },\n },\n }),\n );\n }\n\n async #runWS(ws: WebSocket) {\n let closingWS = false;\n\n const send = (msg: WorkerMessage) => {\n if (closingWS) {\n this.event.off('worker_msg', send);\n return;\n }\n ws.send(msg.toBinary());\n };\n this.event.on('worker_msg', send);\n\n const close = new Promise<void>((resolve) => {\n ws.addEventListener('close', () => {\n closingWS = true;\n if (!this.#closed) {\n this.#logger.error('worker connection closed unexpectedly');\n }\n resolve();\n });\n });\n\n ws.addEventListener('error', (event) => {\n this.#logger.error('worker error:', event.message);\n });\n\n ws.addEventListener('message', (event) => {\n if (event.type !== 'message') {\n this.#logger.warn('unexpected message type: ' + event.type);\n return;\n }\n\n const msg = new ServerMessage();\n msg.fromBinary(event.data as Uint8Array);\n\n // register is the only valid first message, and it is only valid as the\n // first message\n if (this.#connecting && msg.message.case !== 'register') {\n throw new WorkerError('expected register response as first message');\n }\n\n switch (msg.message.case) {\n case 'register': {\n this.#id = msg.message.value.workerId;\n this.#logger\n .child({ id: this.id, server_info: msg.message.value.serverInfo })\n .info('registered worker');\n this.event.emit(\n 'worker_registered',\n msg.message.value.workerId,\n msg.message.value.serverInfo,\n );\n this.#connecting = false;\n break;\n }\n case 'availability': {\n if (!msg.message.value.job) return;\n const task = this.#availability(msg.message.value);\n this.#tasks.push(task);\n task.finally(() => this.#tasks.splice(this.#tasks.indexOf(task)));\n break;\n }\n case 'assignment': {\n if (!msg.message.value.job) return;\n const job = msg.message.value.job;\n if (job.id in this.#pending) {\n const task = this.#pending[job.id];\n delete this.#pending[job.id];\n task?.resolve(msg.message.value);\n } else {\n this.#logger.child({ job }).warn('received assignment for unknown job ' + job.id);\n }\n break;\n }\n case 'termination': {\n const task = this.#termination(msg.message.value);\n this.#tasks.push(task);\n task.finally(() => this.#tasks.splice(this.#tasks.indexOf(task)));\n break;\n }\n }\n });\n\n this.event.emit(\n 'worker_msg',\n new WorkerMessage({\n message: {\n case: 'register',\n value: {\n type: this.#opts.workerType,\n agentName: this.#opts.agentName,\n allowedPermissions: new ParticipantPermission({\n canPublish: this.#opts.permissions.canPublish,\n canSubscribe: this.#opts.permissions.canSubscribe,\n canPublishData: this.#opts.permissions.canPublishData,\n canUpdateMetadata: this.#opts.permissions.canUpdateMetadata,\n hidden: this.#opts.permissions.hidden,\n agent: true,\n }),\n version,\n },\n },\n }),\n );\n\n let currentStatus = WorkerStatus.WS_AVAILABLE;\n const loadMonitor = setInterval(() => {\n if (closingWS) clearInterval(loadMonitor);\n\n const oldStatus = currentStatus;\n this.#opts.loadFunc().then((currentLoad: number) => {\n const isFull = currentLoad >= this.#opts.loadThreshold;\n const currentlyAvailable = !isFull;\n currentStatus = currentlyAvailable ? WorkerStatus.WS_AVAILABLE : WorkerStatus.WS_FULL;\n\n if (oldStatus != currentStatus) {\n const extra = { load: currentLoad, loadThreshold: this.#opts.loadThreshold };\n if (isFull) {\n this.#logger.child(extra).info('worker is at full capacity, marking as unavailable');\n } else {\n this.#logger.child(extra).info('worker is below capacity, marking as available');\n }\n }\n\n this.event.emit(\n 'worker_msg',\n new WorkerMessage({\n message: {\n case: 'updateWorker',\n value: {\n load: currentLoad,\n status: currentStatus,\n },\n },\n }),\n );\n });\n }, UPDATE_LOAD_INTERVAL);\n\n await close;\n ws.removeAllListeners();\n }\n\n async #availability(msg: AvailabilityRequest) {\n let answered = false;\n\n const onReject = async () => {\n answered = true;\n this.event.emit(\n 'worker_msg',\n new WorkerMessage({\n message: {\n case: 'availability',\n value: {\n jobId: msg.job!.id,\n available: false,\n },\n },\n }),\n );\n };\n\n const onAccept = async (args: JobAcceptArguments) => {\n answered = true;\n\n this.event.emit(\n 'worker_msg',\n new WorkerMessage({\n message: {\n case: 'availability',\n value: {\n jobId: msg.job!.id,\n available: true,\n participantIdentity: args.identity,\n participantName: args.name,\n participantMetadata: args.metadata,\n participantAttributes: args.attributes,\n },\n },\n }),\n );\n\n this.#pending[req.id] = new PendingAssignment();\n const timer = setTimeout(() => {\n this.#logger.child({ req }).warn(`assignment for job ${req.id} timed out`);\n return;\n }, ASSIGNMENT_TIMEOUT);\n const asgn = await this.#pending[req.id]?.promise.then(async (asgn) => {\n clearTimeout(timer);\n return asgn;\n });\n\n if (asgn) {\n await this.#procPool.launchJob({\n acceptArguments: args,\n job: msg.job!,\n url: asgn.url || this.#opts.wsURL,\n token: asgn.token,\n });\n } else {\n this.#logger.child({ requestId: req.id }).warn('pending assignment not found');\n }\n };\n\n const req = new JobRequest(msg.job!, onReject, onAccept);\n this.#logger\n .child({ job: msg.job, resuming: msg.resuming, agentName: this.#opts.agentName })\n .info('received job request');\n\n const jobRequestTask = async () => {\n try {\n await this.#opts.requestFunc(req);\n } catch (e) {\n this.#logger\n .child({ job: msg.job, resuming: msg.resuming, agentName: this.#opts.agentName })\n .info('jobRequestFunc failed');\n await onReject();\n }\n\n if (!answered) {\n this.#logger\n .child({ job: msg.job, resuming: msg.resuming, agentName: this.#opts.agentName })\n .info('no answer was given inside the jobRequestFunc, automatically rejecting the job');\n }\n };\n\n const task = jobRequestTask();\n this.#tasks.push(task);\n task.finally(() => this.#tasks.splice(this.#tasks.indexOf(task)));\n }\n\n async #termination(msg: JobTermination) {\n const proc = this.#procPool.getByJobId(msg.jobId);\n if (proc === null) {\n // safe to ignore\n return;\n }\n await proc.close();\n }\n\n async close() {\n if (this.#closed) {\n await this.#close.await;\n return;\n }\n\n this.#logger.info('shutting down worker');\n\n this.#closed = true;\n\n await this.#inferenceExecutor?.close();\n await this.#procPool.close();\n await this.#httpServer.close();\n await Promise.allSettled(this.#tasks);\n\n this.#session?.close();\n await this.#close.await;\n }\n}\n"],"mappings":"AASA;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,aAAa,yBAAyB;AAC/C,SAAS,oBAAoB;AAC7B,OAAO,QAAQ;AACf,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAC3B,SAAS,uBAAuB;AAChC,SAAS,6BAA6B;AACtC,SAAS,gBAAgB;AAEzB,SAAS,kBAAkB;AAC3B,SAAS,WAAW;AACpB,SAAS,cAAc;AACvB,SAAS,eAAe;AAExB,MAAM,yBAAyB;AAC/B,MAAM,qBAAqB,MAAM;AACjC,MAAM,uBAAuB,MAAM;AAEnC,MAAM,QAAQ;AAAA,EACZ,OAAO,cAAc,YAA6B;AAChD,QAAI,YAAY;AACd,aAAO;AAAA,IACT,OAAO;AACL,aAAO;AAAA,IACT;AAAA,EACF;AAAA,EAEA,OAAO,iBAAiB,YAA6B;AACnD,QAAI,YAAY;AACd,aAAO;AAAA,IACT,OAAO;AACL,aAAO;AAAA,IACT;AAAA,EACF;AAAA,EAEA,OAAO,KAAK,YAA6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B,cAAM,SAAS;AAAA,MACjB;AAEA,UAAI,CAAC,UAAU;AACb,aAAK,QACF,MAAM,EAAE,KAAK,IAAI,KAAK,UAAU,IAAI,UAAU,WAAW,KAAK,MAAM,UAAU,CAAC,EAC/E,KAAK,gFAAgF;AAAA,MAC1F;AAAA,IACF;AAEA,UAAM,OAAO,eAAe;AAC5B,SAAK,OAAO,KAAK,IAAI;AACrB,SAAK,QAAQ,MAAM,KAAK,OAAO,OAAO,KAAK,OAAO,QAAQ,IAAI,CAAC,CAAC;AAAA,EAClE;AAAA,EAEA,MAAM,aAAa,KAAqB;AACtC,UAAM,OAAO,KAAK,UAAU,WAAW,IAAI,KAAK;AAChD,QAAI,SAAS,MAAM;AAEjB;AAAA,IACF;AACA,UAAM,KAAK,MAAM;AAAA,EACnB;AAAA,EAEA,MAAM,QAAQ;AAhtBhB;AAitBI,QAAI,KAAK,SAAS;AAChB,YAAM,KAAK,OAAO;AAClB;AAAA,IACF;AAEA,SAAK,QAAQ,KAAK,sBAAsB;AAExC,SAAK,UAAU;AAEf,YAAM,UAAK,uBAAL,mBAAyB;AAC/B,UAAM,KAAK,UAAU,MAAM;AAC3B,UAAM,KAAK,YAAY,MAAM;AAC7B,UAAM,QAAQ,WAAW,KAAK,MAAM;AAEpC,eAAK,aAAL,mBAAe;AACf,UAAM,KAAK,OAAO;AAAA,EACpB;AACF;","names":["i","asgn"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@livekit/agents",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.4",
|
|
4
4
|
"description": "LiveKit Agents - Node.js",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"require": "dist/index.cjs",
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"README.md"
|
|
23
23
|
],
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@livekit/rtc-node": "^0.13.
|
|
25
|
+
"@livekit/rtc-node": "^0.13.11",
|
|
26
26
|
"@microsoft/api-extractor": "^7.35.0",
|
|
27
27
|
"@types/node": "^22.5.5",
|
|
28
28
|
"@types/ws": "^8.5.10",
|
|
29
|
-
"tsup": "^8.
|
|
29
|
+
"tsup": "^8.4.0",
|
|
30
30
|
"typescript": "^5.0.0"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"zod": "^3.23.8"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@livekit/rtc-node": "^0.13.
|
|
44
|
+
"@livekit/rtc-node": "^0.13.11"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "tsup --onSuccess \"tsc --declaration --emitDeclarationOnly\"",
|
package/src/audio.ts
CHANGED
|
@@ -17,7 +17,7 @@ export class AudioByteStream {
|
|
|
17
17
|
this.#numChannels = numChannels;
|
|
18
18
|
|
|
19
19
|
if (samplesPerChannel === null) {
|
|
20
|
-
samplesPerChannel = Math.floor(sampleRate /
|
|
20
|
+
samplesPerChannel = Math.floor(sampleRate / 10); // 100ms by default
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
this.#bytesPerFrame = numChannels * samplesPerChannel * 2; // 2 bytes per sample (Int16)
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
export const ATTRIBUTE_TRANSCRIPTION_TRACK_ID = 'lk.transcribed_track_id';
|
|
5
|
+
export const ATTRIBUTE_TRANSCRIPTION_FINAL = 'lk.transcription_final';
|
|
6
|
+
export const TOPIC_TRANSCRIPTION = 'lk.transcription';
|
|
7
|
+
export const TOPIC_CHAT = 'lk.chat';
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2025 LiveKit, Inc.
|
|
2
2
|
//
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
-
import
|
|
4
|
+
import { once } from 'node:events';
|
|
5
5
|
import type { InferenceRunner } from '../inference_runner.js';
|
|
6
6
|
import { initializeLogger, log } from '../log.js';
|
|
7
|
+
import { Future } from '../utils.js';
|
|
7
8
|
import type { IPCMessage } from './message.js';
|
|
8
9
|
|
|
9
10
|
const ORPHANED_TIMEOUT = 15 * 1000;
|
|
10
11
|
|
|
11
12
|
(async () => {
|
|
12
13
|
if (process.send) {
|
|
14
|
+
const join = new Future();
|
|
15
|
+
|
|
13
16
|
// don't do anything on C-c
|
|
14
17
|
// this is handled in cli, triggering a termination of all child processes at once.
|
|
15
18
|
process.on('SIGINT', () => {
|
|
@@ -46,8 +49,6 @@ const ORPHANED_TIMEOUT = 15 * 1000;
|
|
|
46
49
|
logger.debug('all inference runners initialized');
|
|
47
50
|
process.send({ case: 'initializeResponse' });
|
|
48
51
|
|
|
49
|
-
const closeEvent = new EventEmitter();
|
|
50
|
-
|
|
51
52
|
const orphanedTimeout = setTimeout(() => {
|
|
52
53
|
logger.warn('inference process orphaned, shutting down.');
|
|
53
54
|
process.exit();
|
|
@@ -74,7 +75,7 @@ const ORPHANED_TIMEOUT = 15 * 1000;
|
|
|
74
75
|
}
|
|
75
76
|
};
|
|
76
77
|
|
|
77
|
-
|
|
78
|
+
const messageHandler = (msg: IPCMessage) => {
|
|
78
79
|
switch (msg.case) {
|
|
79
80
|
case 'pingRequest':
|
|
80
81
|
orphanedTimeout.refresh();
|
|
@@ -84,11 +85,25 @@ const ORPHANED_TIMEOUT = 15 * 1000;
|
|
|
84
85
|
});
|
|
85
86
|
break;
|
|
86
87
|
case 'shutdownRequest':
|
|
87
|
-
|
|
88
|
+
logger.info('inference process received shutdown request');
|
|
89
|
+
process.send!({ case: 'done' });
|
|
90
|
+
clearTimeout(orphanedTimeout);
|
|
91
|
+
// Remove our message handler to stop processing new messages
|
|
92
|
+
process.off('message', messageHandler);
|
|
93
|
+
// Resolve the future to allow the process to continue to completion
|
|
94
|
+
join.resolve();
|
|
88
95
|
break;
|
|
89
96
|
case 'inferenceRequest':
|
|
90
97
|
handleInferenceRequest(msg.value);
|
|
91
98
|
}
|
|
92
|
-
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
process.on('message', messageHandler);
|
|
102
|
+
|
|
103
|
+
await join.await;
|
|
104
|
+
|
|
105
|
+
logger.info('Inference process shutdown');
|
|
106
|
+
|
|
107
|
+
return process.exitCode;
|
|
93
108
|
}
|
|
94
109
|
})();
|
|
@@ -9,6 +9,7 @@ import type { Logger } from 'pino';
|
|
|
9
9
|
import { type Agent, isAgent } from '../generator.js';
|
|
10
10
|
import { CurrentJobContext, JobContext, JobProcess, type RunningJobInfo } from '../job.js';
|
|
11
11
|
import { initializeLogger, log } from '../log.js';
|
|
12
|
+
import { Future } from '../utils.js';
|
|
12
13
|
import { defaultInitializeProcessFunc } from '../worker.js';
|
|
13
14
|
import type { InferenceExecutor } from './inference_executor.js';
|
|
14
15
|
import type { IPCMessage } from './message.js';
|
|
@@ -66,13 +67,16 @@ const startJob = (
|
|
|
66
67
|
info: RunningJobInfo,
|
|
67
68
|
closeEvent: EventEmitter,
|
|
68
69
|
logger: Logger,
|
|
70
|
+
joinFuture: Future,
|
|
69
71
|
): JobTask => {
|
|
70
72
|
let connect = false;
|
|
71
73
|
let shutdown = false;
|
|
72
74
|
|
|
73
75
|
const room = new Room();
|
|
74
76
|
room.on(RoomEvent.Disconnected, () => {
|
|
75
|
-
|
|
77
|
+
if (!shutdown) {
|
|
78
|
+
closeEvent.emit('close', false);
|
|
79
|
+
}
|
|
76
80
|
});
|
|
77
81
|
|
|
78
82
|
const onConnect = () => {
|
|
@@ -99,6 +103,7 @@ const startJob = (
|
|
|
99
103
|
|
|
100
104
|
await once(closeEvent, 'close').then((close) => {
|
|
101
105
|
logger.debug('shutting down');
|
|
106
|
+
shutdown = true;
|
|
102
107
|
process.send!({ case: 'exiting', value: { reason: close[1] } });
|
|
103
108
|
});
|
|
104
109
|
|
|
@@ -109,11 +114,13 @@ const startJob = (
|
|
|
109
114
|
for (const callback of ctx.shutdownCallbacks) {
|
|
110
115
|
shutdownTasks.push(callback());
|
|
111
116
|
}
|
|
112
|
-
await Promise.all(shutdownTasks).catch(() =>
|
|
117
|
+
await Promise.all(shutdownTasks).catch((error) =>
|
|
118
|
+
logger.error('error while shutting down the job', error),
|
|
119
|
+
);
|
|
113
120
|
|
|
114
121
|
process.send!({ case: 'done' });
|
|
115
122
|
logger.info('job completed.');
|
|
116
|
-
|
|
123
|
+
joinFuture.resolve();
|
|
117
124
|
});
|
|
118
125
|
|
|
119
126
|
return { ctx, task };
|
|
@@ -121,6 +128,8 @@ const startJob = (
|
|
|
121
128
|
|
|
122
129
|
(async () => {
|
|
123
130
|
if (process.send) {
|
|
131
|
+
const join = new Future();
|
|
132
|
+
|
|
124
133
|
// process.argv:
|
|
125
134
|
// [0] `node'
|
|
126
135
|
// [1] import.meta.filename
|
|
@@ -173,10 +182,10 @@ const startJob = (
|
|
|
173
182
|
|
|
174
183
|
const orphanedTimeout = setTimeout(() => {
|
|
175
184
|
logger.warn('job process orphaned, shutting down.');
|
|
176
|
-
|
|
185
|
+
join.resolve();
|
|
177
186
|
}, ORPHANED_TIMEOUT);
|
|
178
187
|
|
|
179
|
-
|
|
188
|
+
const messageHandler = (msg: IPCMessage) => {
|
|
180
189
|
switch (msg.case) {
|
|
181
190
|
case 'pingRequest': {
|
|
182
191
|
orphanedTimeout.refresh();
|
|
@@ -193,17 +202,26 @@ const startJob = (
|
|
|
193
202
|
|
|
194
203
|
logger = logger.child({ jobID: msg.value.runningJob.job.id });
|
|
195
204
|
|
|
196
|
-
job = startJob(proc, agent.entry, msg.value.runningJob, closeEvent, logger);
|
|
205
|
+
job = startJob(proc, agent.entry, msg.value.runningJob, closeEvent, logger, join);
|
|
197
206
|
logger.debug('job started');
|
|
198
207
|
break;
|
|
199
208
|
}
|
|
200
209
|
case 'shutdownRequest': {
|
|
201
210
|
if (!job) {
|
|
202
|
-
|
|
211
|
+
join.resolve();
|
|
203
212
|
}
|
|
204
|
-
closeEvent.emit('close', '');
|
|
213
|
+
closeEvent.emit('close', 'shutdownRequest');
|
|
214
|
+
clearTimeout(orphanedTimeout);
|
|
215
|
+
process.off('message', messageHandler);
|
|
205
216
|
}
|
|
206
217
|
}
|
|
207
|
-
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
process.on('message', messageHandler);
|
|
221
|
+
|
|
222
|
+
await join.await;
|
|
223
|
+
|
|
224
|
+
logger.info('Job process shutdown');
|
|
225
|
+
return process.exitCode;
|
|
208
226
|
}
|
|
209
227
|
})();
|
|
@@ -125,7 +125,6 @@ export abstract class SupervisedProc {
|
|
|
125
125
|
case 'done': {
|
|
126
126
|
this.#closing = true;
|
|
127
127
|
this.proc!.off('message', listener);
|
|
128
|
-
this.#join.resolve();
|
|
129
128
|
break;
|
|
130
129
|
}
|
|
131
130
|
}
|
|
@@ -142,6 +141,10 @@ export abstract class SupervisedProc {
|
|
|
142
141
|
this.#join.resolve();
|
|
143
142
|
});
|
|
144
143
|
|
|
144
|
+
this.proc!.on('exit', () => {
|
|
145
|
+
this.#join.resolve();
|
|
146
|
+
});
|
|
147
|
+
|
|
145
148
|
this.mainTask(this.proc!);
|
|
146
149
|
|
|
147
150
|
await this.#join.await;
|
|
@@ -185,15 +188,11 @@ export abstract class SupervisedProc {
|
|
|
185
188
|
}
|
|
186
189
|
this.#closing = true;
|
|
187
190
|
|
|
188
|
-
if (!this.#runningJob) {
|
|
189
|
-
this.proc!.kill();
|
|
190
|
-
this.#join.resolve();
|
|
191
|
-
}
|
|
192
|
-
|
|
193
191
|
this.proc!.send({ case: 'shutdownRequest' });
|
|
194
192
|
|
|
195
193
|
const timer = setTimeout(() => {
|
|
196
194
|
this.#logger.error('job shutdown is taking too much time');
|
|
195
|
+
this.proc!.kill();
|
|
197
196
|
}, this.#opts.closeTimeout);
|
|
198
197
|
await this.#join.await.then(() => {
|
|
199
198
|
clearTimeout(timer);
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
import type {
|
|
5
5
|
LocalTrackPublication,
|
|
6
|
+
NoiseCancellationOptions,
|
|
6
7
|
RemoteAudioTrack,
|
|
7
8
|
RemoteParticipant,
|
|
8
9
|
RemoteTrack,
|
|
@@ -19,6 +20,11 @@ import {
|
|
|
19
20
|
} from '@livekit/rtc-node';
|
|
20
21
|
import { EventEmitter } from 'node:events';
|
|
21
22
|
import { AudioByteStream } from '../audio.js';
|
|
23
|
+
import {
|
|
24
|
+
ATTRIBUTE_TRANSCRIPTION_FINAL,
|
|
25
|
+
ATTRIBUTE_TRANSCRIPTION_TRACK_ID,
|
|
26
|
+
TOPIC_TRANSCRIPTION,
|
|
27
|
+
} from '../constants.js';
|
|
22
28
|
import * as llm from '../llm/index.js';
|
|
23
29
|
import { log } from '../log.js';
|
|
24
30
|
import type { MultimodalLLMMetrics } from '../metrics/base.js';
|
|
@@ -72,17 +78,20 @@ export class MultimodalAgent extends EventEmitter {
|
|
|
72
78
|
chatCtx,
|
|
73
79
|
fncCtx,
|
|
74
80
|
maxTextResponseRetries = 5,
|
|
81
|
+
noiseCancellation,
|
|
75
82
|
}: {
|
|
76
83
|
model: RealtimeModel;
|
|
77
84
|
chatCtx?: llm.ChatContext;
|
|
78
85
|
fncCtx?: llm.FunctionContext;
|
|
79
86
|
maxTextResponseRetries?: number;
|
|
87
|
+
noiseCancellation?: NoiseCancellationOptions;
|
|
80
88
|
}) {
|
|
81
89
|
super();
|
|
82
90
|
this.model = model;
|
|
83
91
|
this.#chatCtx = chatCtx;
|
|
84
92
|
this.#fncCtx = fncCtx;
|
|
85
93
|
this.#maxTextResponseRetries = maxTextResponseRetries;
|
|
94
|
+
this.#noiseCancellation = noiseCancellation;
|
|
86
95
|
}
|
|
87
96
|
|
|
88
97
|
#participant: RemoteParticipant | string | null = null;
|
|
@@ -95,6 +104,7 @@ export class MultimodalAgent extends EventEmitter {
|
|
|
95
104
|
#session: RealtimeSession | null = null;
|
|
96
105
|
#fncCtx: llm.FunctionContext | undefined = undefined;
|
|
97
106
|
#chatCtx: llm.ChatContext | undefined = undefined;
|
|
107
|
+
#noiseCancellation: NoiseCancellationOptions | undefined = undefined;
|
|
98
108
|
|
|
99
109
|
#_started: boolean = false;
|
|
100
110
|
#_pendingFunctionCalls: Set<string> = new Set();
|
|
@@ -246,8 +256,8 @@ export class MultimodalAgent extends EventEmitter {
|
|
|
246
256
|
if (message.contentType === 'text') return;
|
|
247
257
|
|
|
248
258
|
const synchronizer = new TextAudioSynchronizer(defaultTextSyncOptions);
|
|
249
|
-
synchronizer.on('textUpdated', (text) => {
|
|
250
|
-
this.#publishTranscription(
|
|
259
|
+
synchronizer.on('textUpdated', async (text) => {
|
|
260
|
+
await this.#publishTranscription(
|
|
251
261
|
this.room!.localParticipant!.identity!,
|
|
252
262
|
this.#getLocalTrackSid()!,
|
|
253
263
|
text.text,
|
|
@@ -297,25 +307,31 @@ export class MultimodalAgent extends EventEmitter {
|
|
|
297
307
|
});
|
|
298
308
|
|
|
299
309
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
300
|
-
this.#session.on('input_speech_committed', (ev: any) => {
|
|
310
|
+
this.#session.on('input_speech_committed', async (ev: any) => {
|
|
301
311
|
// openai.realtime.InputSpeechCommittedEvent
|
|
302
312
|
const participantIdentity = this.linkedParticipant?.identity;
|
|
303
313
|
const trackSid = this.subscribedTrack?.sid;
|
|
304
314
|
if (participantIdentity && trackSid) {
|
|
305
|
-
this.#publishTranscription(participantIdentity, trackSid, '…', false, ev.itemId);
|
|
315
|
+
await this.#publishTranscription(participantIdentity, trackSid, '…', false, ev.itemId);
|
|
306
316
|
} else {
|
|
307
317
|
this.#logger.error('Participant or track not set');
|
|
308
318
|
}
|
|
309
319
|
});
|
|
310
320
|
|
|
311
321
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
312
|
-
this.#session.on('input_speech_transcription_completed', (ev: any) => {
|
|
322
|
+
this.#session.on('input_speech_transcription_completed', async (ev: any) => {
|
|
313
323
|
// openai.realtime.InputSpeechTranscriptionCompletedEvent
|
|
314
324
|
const transcription = ev.transcript;
|
|
315
325
|
const participantIdentity = this.linkedParticipant?.identity;
|
|
316
326
|
const trackSid = this.subscribedTrack?.sid;
|
|
317
327
|
if (participantIdentity && trackSid) {
|
|
318
|
-
this.#publishTranscription(
|
|
328
|
+
await this.#publishTranscription(
|
|
329
|
+
participantIdentity,
|
|
330
|
+
trackSid,
|
|
331
|
+
transcription,
|
|
332
|
+
true,
|
|
333
|
+
ev.itemId,
|
|
334
|
+
);
|
|
319
335
|
} else {
|
|
320
336
|
this.#logger.error('Participant or track not set');
|
|
321
337
|
}
|
|
@@ -327,7 +343,7 @@ export class MultimodalAgent extends EventEmitter {
|
|
|
327
343
|
this.#logger.child({ transcription }).debug('committed user speech');
|
|
328
344
|
});
|
|
329
345
|
|
|
330
|
-
this.#session.on('input_speech_started', (ev: any) => {
|
|
346
|
+
this.#session.on('input_speech_started', async (ev: any) => {
|
|
331
347
|
this.emit('user_started_speaking');
|
|
332
348
|
if (this.#playingHandle && !this.#playingHandle.done) {
|
|
333
349
|
this.#playingHandle.interrupt();
|
|
@@ -344,7 +360,7 @@ export class MultimodalAgent extends EventEmitter {
|
|
|
344
360
|
const participantIdentity = this.linkedParticipant?.identity;
|
|
345
361
|
const trackSid = this.subscribedTrack?.sid;
|
|
346
362
|
if (participantIdentity && trackSid) {
|
|
347
|
-
this.#publishTranscription(participantIdentity, trackSid, '…', false, ev.itemId);
|
|
363
|
+
await this.#publishTranscription(participantIdentity, trackSid, '…', false, ev.itemId);
|
|
348
364
|
}
|
|
349
365
|
});
|
|
350
366
|
|
|
@@ -454,9 +470,12 @@ export class MultimodalAgent extends EventEmitter {
|
|
|
454
470
|
this.subscribedTrack = track;
|
|
455
471
|
|
|
456
472
|
this.readMicroTask = new Promise<void>((resolve, reject) => {
|
|
457
|
-
|
|
458
|
-
.
|
|
459
|
-
.
|
|
473
|
+
const audioStreamOptions = {
|
|
474
|
+
sampleRate: this.model.sampleRate,
|
|
475
|
+
numChannels: this.model.numChannels,
|
|
476
|
+
...(this.#noiseCancellation ? { noiseCancellation: this.#noiseCancellation } : {}),
|
|
477
|
+
};
|
|
478
|
+
readAudioStreamTask(new AudioStream(track, audioStreamOptions)).then(resolve).catch(reject);
|
|
460
479
|
});
|
|
461
480
|
}
|
|
462
481
|
|
|
@@ -467,13 +486,13 @@ export class MultimodalAgent extends EventEmitter {
|
|
|
467
486
|
return this.#localTrackSid;
|
|
468
487
|
}
|
|
469
488
|
|
|
470
|
-
#publishTranscription(
|
|
489
|
+
async #publishTranscription(
|
|
471
490
|
participantIdentity: string,
|
|
472
491
|
trackSid: string,
|
|
473
492
|
text: string,
|
|
474
493
|
isFinal: boolean,
|
|
475
494
|
id: string,
|
|
476
|
-
): void {
|
|
495
|
+
): Promise<void> {
|
|
477
496
|
this.#logger.debug(
|
|
478
497
|
`Publishing transcription ${participantIdentity} ${trackSid} ${text} ${isFinal} ${id}`,
|
|
479
498
|
);
|
|
@@ -496,6 +515,17 @@ export class MultimodalAgent extends EventEmitter {
|
|
|
496
515
|
},
|
|
497
516
|
],
|
|
498
517
|
});
|
|
518
|
+
|
|
519
|
+
const stream = await this.room.localParticipant.streamText({
|
|
520
|
+
topic: TOPIC_TRANSCRIPTION,
|
|
521
|
+
senderIdentity: participantIdentity,
|
|
522
|
+
attributes: {
|
|
523
|
+
[ATTRIBUTE_TRANSCRIPTION_TRACK_ID]: trackSid,
|
|
524
|
+
[ATTRIBUTE_TRANSCRIPTION_FINAL]: isFinal.toString(),
|
|
525
|
+
},
|
|
526
|
+
});
|
|
527
|
+
await stream.write(text);
|
|
528
|
+
await stream.close();
|
|
499
529
|
}
|
|
500
530
|
|
|
501
531
|
#updateState() {
|
|
@@ -168,15 +168,9 @@ export class AgentPlayout extends (EventEmitter as new () => TypedEmitter<AgentP
|
|
|
168
168
|
handle.pushedDuration += (frame.samplesPerChannel / frame.sampleRate) * 1000;
|
|
169
169
|
handle.synchronizer.pushAudio(frame);
|
|
170
170
|
await this.#audioSource.captureFrame(frame);
|
|
171
|
-
await this.#audioSource.waitForPlayout();
|
|
172
171
|
}
|
|
173
172
|
|
|
174
|
-
|
|
175
|
-
// but for some reason too many TTS frames can gunk up the buffer and lead to
|
|
176
|
-
// FFI errors. this works 🤷♀️
|
|
177
|
-
// if (this.#audioSource.queuedDuration > 0) {
|
|
178
|
-
// await this.#audioSource.waitForPlayout();
|
|
179
|
-
// }
|
|
173
|
+
await this.#audioSource.waitForPlayout();
|
|
180
174
|
|
|
181
175
|
handle.synchronizer.close(false);
|
|
182
176
|
resolve();
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
//
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
import type {
|
|
5
|
+
NoiseCancellationOptions,
|
|
5
6
|
RemoteAudioTrack,
|
|
6
7
|
RemoteParticipant,
|
|
7
8
|
RemoteTrackPublication,
|
|
@@ -44,13 +45,21 @@ export class HumanInput extends (EventEmitter as new () => TypedEmitter<HumanInp
|
|
|
44
45
|
#speaking = false;
|
|
45
46
|
#speechProbability = 0;
|
|
46
47
|
#logger = log();
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
#noiseCancellation?: NoiseCancellationOptions;
|
|
49
|
+
|
|
50
|
+
constructor(
|
|
51
|
+
room: Room,
|
|
52
|
+
vad: VAD,
|
|
53
|
+
stt: STT,
|
|
54
|
+
participant: RemoteParticipant,
|
|
55
|
+
noiseCancellation?: NoiseCancellationOptions,
|
|
56
|
+
) {
|
|
49
57
|
super();
|
|
50
58
|
this.#room = room;
|
|
51
59
|
this.#vad = vad;
|
|
52
60
|
this.#stt = stt;
|
|
53
61
|
this.#participant = participant;
|
|
62
|
+
this.#noiseCancellation = noiseCancellation;
|
|
54
63
|
|
|
55
64
|
this.#room.on(RoomEvent.TrackPublished, this.#subscribeToMicrophone.bind(this));
|
|
56
65
|
this.#room.on(RoomEvent.TrackSubscribed, this.#subscribeToMicrophone.bind(this));
|
|
@@ -93,7 +102,12 @@ export class HumanInput extends (EventEmitter as new () => TypedEmitter<HumanInp
|
|
|
93
102
|
this.#recognizeTask.cancel();
|
|
94
103
|
}
|
|
95
104
|
|
|
96
|
-
const
|
|
105
|
+
const audioStreamOptions = {
|
|
106
|
+
sampleRate: 16000,
|
|
107
|
+
numChannels: 1,
|
|
108
|
+
...(this.#noiseCancellation ? { noiseCancellation: this.#noiseCancellation } : {}),
|
|
109
|
+
};
|
|
110
|
+
const audioStream = new AudioStream(track, audioStreamOptions);
|
|
97
111
|
|
|
98
112
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
99
113
|
this.#recognizeTask = new CancellablePromise(async (resolve, _, onCancel) => {
|