@livekit/agents 0.7.6 → 0.7.7
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.d.cts +9 -0
- package/dist/cli.d.cts +14 -0
- package/dist/constants.d.cts +5 -0
- package/dist/generator.d.cts +23 -0
- package/dist/http_server.cjs.map +1 -1
- package/dist/http_server.d.cts +19 -0
- package/dist/http_server.d.ts +1 -0
- package/dist/http_server.d.ts.map +1 -1
- package/dist/http_server.js.map +1 -1
- package/dist/index.d.cts +29 -0
- package/dist/inference_runner.d.cts +12 -0
- package/dist/ipc/index.d.cts +2 -0
- package/dist/ipc/inference_executor.d.cts +4 -0
- package/dist/ipc/inference_proc_executor.cjs +3 -2
- package/dist/ipc/inference_proc_executor.cjs.map +1 -1
- package/dist/ipc/inference_proc_executor.d.cts +23 -0
- package/dist/ipc/inference_proc_executor.js +1 -1
- package/dist/ipc/inference_proc_executor.js.map +1 -1
- package/dist/ipc/inference_proc_lazy_main.d.cts +2 -0
- package/dist/ipc/job_executor.d.cts +18 -0
- package/dist/ipc/job_proc_executor.cjs +3 -2
- package/dist/ipc/job_proc_executor.cjs.map +1 -1
- package/dist/ipc/job_proc_executor.d.cts +19 -0
- package/dist/ipc/job_proc_executor.js +1 -1
- package/dist/ipc/job_proc_executor.js.map +1 -1
- package/dist/ipc/job_proc_lazy_main.cjs +1 -1
- package/dist/ipc/job_proc_lazy_main.cjs.map +1 -1
- package/dist/ipc/job_proc_lazy_main.d.cts +2 -0
- package/dist/ipc/job_proc_lazy_main.js +1 -1
- package/dist/ipc/job_proc_lazy_main.js.map +1 -1
- package/dist/ipc/message.d.cts +58 -0
- package/dist/ipc/proc_pool.d.cts +31 -0
- package/dist/ipc/supervised_proc.d.cts +30 -0
- package/dist/job.d.cts +113 -0
- package/dist/llm/chat_context.d.cts +66 -0
- package/dist/llm/function_context.d.cts +47 -0
- package/dist/llm/index.d.cts +4 -0
- package/dist/llm/llm.cjs +3 -3
- package/dist/llm/llm.cjs.map +1 -1
- package/dist/llm/llm.d.cts +66 -0
- package/dist/llm/llm.js +3 -3
- package/dist/llm/llm.js.map +1 -1
- package/dist/log.d.cts +13 -0
- package/dist/metrics/base.d.cts +96 -0
- package/dist/metrics/index.d.cts +5 -0
- package/dist/metrics/usage_collector.d.cts +14 -0
- package/dist/metrics/utils.d.cts +10 -0
- package/dist/multimodal/agent_playout.d.cts +34 -0
- package/dist/multimodal/index.d.cts +3 -0
- package/dist/multimodal/multimodal_agent.d.cts +48 -0
- package/dist/pipeline/agent_output.d.cts +33 -0
- package/dist/pipeline/agent_playout.d.cts +40 -0
- package/dist/pipeline/human_input.d.cts +30 -0
- package/dist/pipeline/index.d.cts +2 -0
- package/dist/pipeline/pipeline_agent.d.cts +151 -0
- package/dist/pipeline/speech_handle.d.cts +37 -0
- package/dist/plugin.d.cts +10 -0
- package/dist/stt/index.d.cts +3 -0
- package/dist/stt/stream_adapter.d.cts +18 -0
- package/dist/stt/stt.d.cts +124 -0
- package/dist/tokenize/basic/basic.d.cts +18 -0
- package/dist/tokenize/basic/hyphenator.d.cts +17 -0
- package/dist/tokenize/basic/index.d.cts +2 -0
- package/dist/tokenize/basic/paragraph.d.cts +5 -0
- package/dist/tokenize/basic/sentence.d.cts +5 -0
- package/dist/tokenize/basic/word.d.cts +5 -0
- package/dist/tokenize/index.d.cts +5 -0
- package/dist/tokenize/token_stream.d.cts +39 -0
- package/dist/tokenize/tokenizer.d.cts +55 -0
- package/dist/transcription.d.cts +31 -0
- package/dist/tts/index.d.cts +3 -0
- package/dist/tts/stream_adapter.d.cts +17 -0
- package/dist/tts/tts.cjs +4 -4
- package/dist/tts/tts.cjs.map +1 -1
- package/dist/tts/tts.d.cts +127 -0
- package/dist/tts/tts.js +4 -4
- package/dist/tts/tts.js.map +1 -1
- package/dist/utils.d.cts +72 -0
- package/dist/vad.d.cts +78 -0
- package/dist/version.d.cts +2 -0
- package/dist/worker.cjs +2 -1
- package/dist/worker.cjs.map +1 -1
- package/dist/worker.d.cts +109 -0
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +2 -1
- package/dist/worker.js.map +1 -1
- package/package.json +9 -5
- package/src/http_server.ts +1 -0
- package/src/ipc/inference_proc_executor.ts +1 -1
- package/src/ipc/job_proc_executor.ts +1 -1
- package/src/ipc/job_proc_lazy_main.ts +1 -1
- package/src/llm/llm.ts +3 -3
- package/src/tts/tts.ts +4 -4
- package/src/worker.ts +1 -0
package/dist/audio.d.cts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AudioFrame } from '@livekit/rtc-node';
|
|
2
|
+
/** AudioByteStream translates between LiveKit AudioFrame packets and raw byte data. */
|
|
3
|
+
export declare class AudioByteStream {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(sampleRate: number, numChannels: number, samplesPerChannel?: number | null);
|
|
6
|
+
write(data: ArrayBuffer): AudioFrame[];
|
|
7
|
+
flush(): AudioFrame[];
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=audio.d.ts.map
|
package/dist/cli.d.cts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { WorkerOptions } from './worker.js';
|
|
2
|
+
/**
|
|
3
|
+
* Exposes a CLI for creating a new worker, in development or production mode.
|
|
4
|
+
*
|
|
5
|
+
* @param opts - Options to launch the worker with
|
|
6
|
+
* @example
|
|
7
|
+
* ```
|
|
8
|
+
* if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
9
|
+
* cli.runApp(new WorkerOptions({ agent: import.meta.filename }));
|
|
10
|
+
* }
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare const runApp: (opts: WorkerOptions) => void;
|
|
14
|
+
//# sourceMappingURL=cli.d.ts.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const ATTRIBUTE_TRANSCRIPTION_TRACK_ID = "lk.transcribed_track_id";
|
|
2
|
+
export declare const ATTRIBUTE_TRANSCRIPTION_FINAL = "lk.transcription_final";
|
|
3
|
+
export declare const TOPIC_TRANSCRIPTION = "lk.transcription";
|
|
4
|
+
export declare const TOPIC_CHAT = "lk.chat";
|
|
5
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { JobContext, JobProcess } from './job.js';
|
|
2
|
+
/** @see {@link defineAgent} */
|
|
3
|
+
export interface Agent {
|
|
4
|
+
entry: (ctx: JobContext) => Promise<void>;
|
|
5
|
+
prewarm?: (proc: JobProcess) => unknown;
|
|
6
|
+
}
|
|
7
|
+
/** Helper to check if an object is an agent before running it.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export declare function isAgent(obj: unknown): obj is Agent;
|
|
12
|
+
/**
|
|
13
|
+
* Helper to define an agent according to the required interface.
|
|
14
|
+
* @example A basic agent with entry and prewarm functions
|
|
15
|
+
* ```
|
|
16
|
+
* export default defineAgent({
|
|
17
|
+
* entry: async (ctx: JobContext) => { ... },
|
|
18
|
+
* prewarm: (proc: JobProcess) => { ... },
|
|
19
|
+
* })
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function defineAgent(agent: Agent): Agent;
|
|
23
|
+
//# sourceMappingURL=generator.d.ts.map
|
package/dist/http_server.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/http_server.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { type IncomingMessage, type Server, type ServerResponse, createServer } from 'node:http';\nimport { log } from './log.js';\n\nconst healthCheck = async (res: ServerResponse) => {\n res.writeHead(200);\n res.end('OK');\n};\n\ninterface WorkerResponse {\n agent_name: string;\n worker_type: string;\n active_jobs: number;\n}\n\nexport class HTTPServer {\n host: string;\n port: number;\n app: Server;\n #logger = log();\n\n constructor(host: string, port: number, workerListener: () => WorkerResponse) {\n this.host = host;\n this.port = port;\n\n this.app = createServer((req: IncomingMessage, res: ServerResponse) => {\n if (req.url === '/') {\n healthCheck(res);\n } else if (req.url === '/worker') {\n res.writeHead(200, { 'Contet-Type': 'application/json' });\n res.end(JSON.stringify(workerListener()));\n } else {\n res.writeHead(404);\n res.end('not found');\n }\n });\n }\n\n async run(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.listen(this.port, this.host, (err?: Error) => {\n if (err) reject(err);\n const address = this.app.address();\n if (typeof address! !== 'string') {\n this.#logger.info(`Server is listening on port ${address!.port}`);\n }\n resolve();\n });\n });\n }\n\n async close(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.close((err?: Error) => {\n if (err) reject(err);\n resolve();\n });\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,uBAAqF;AACrF,iBAAoB;AAEpB,MAAM,cAAc,OAAO,QAAwB;AACjD,MAAI,UAAU,GAAG;AACjB,MAAI,IAAI,IAAI;AACd;
|
|
1
|
+
{"version":3,"sources":["../src/http_server.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { type IncomingMessage, type Server, type ServerResponse, createServer } from 'node:http';\nimport { log } from './log.js';\n\nconst healthCheck = async (res: ServerResponse) => {\n res.writeHead(200);\n res.end('OK');\n};\n\ninterface WorkerResponse {\n agent_name: string;\n worker_type: string;\n active_jobs: number;\n sdk_version: string;\n}\n\nexport class HTTPServer {\n host: string;\n port: number;\n app: Server;\n #logger = log();\n\n constructor(host: string, port: number, workerListener: () => WorkerResponse) {\n this.host = host;\n this.port = port;\n\n this.app = createServer((req: IncomingMessage, res: ServerResponse) => {\n if (req.url === '/') {\n healthCheck(res);\n } else if (req.url === '/worker') {\n res.writeHead(200, { 'Contet-Type': 'application/json' });\n res.end(JSON.stringify(workerListener()));\n } else {\n res.writeHead(404);\n res.end('not found');\n }\n });\n }\n\n async run(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.listen(this.port, this.host, (err?: Error) => {\n if (err) reject(err);\n const address = this.app.address();\n if (typeof address! !== 'string') {\n this.#logger.info(`Server is listening on port ${address!.port}`);\n }\n resolve();\n });\n });\n }\n\n async close(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.close((err?: Error) => {\n if (err) reject(err);\n resolve();\n });\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,uBAAqF;AACrF,iBAAoB;AAEpB,MAAM,cAAc,OAAO,QAAwB;AACjD,MAAI,UAAU,GAAG;AACjB,MAAI,IAAI,IAAI;AACd;AASO,MAAM,WAAW;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA,cAAU,gBAAI;AAAA,EAEd,YAAY,MAAc,MAAc,gBAAsC;AAC5E,SAAK,OAAO;AACZ,SAAK,OAAO;AAEZ,SAAK,UAAM,+BAAa,CAAC,KAAsB,QAAwB;AACrE,UAAI,IAAI,QAAQ,KAAK;AACnB,oBAAY,GAAG;AAAA,MACjB,WAAW,IAAI,QAAQ,WAAW;AAChC,YAAI,UAAU,KAAK,EAAE,eAAe,mBAAmB,CAAC;AACxD,YAAI,IAAI,KAAK,UAAU,eAAe,CAAC,CAAC;AAAA,MAC1C,OAAO;AACL,YAAI,UAAU,GAAG;AACjB,YAAI,IAAI,WAAW;AAAA,MACrB;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,MAAqB;AACzB,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,WAAK,IAAI,OAAO,KAAK,MAAM,KAAK,MAAM,CAAC,QAAgB;AACrD,YAAI,IAAK,QAAO,GAAG;AACnB,cAAM,UAAU,KAAK,IAAI,QAAQ;AACjC,YAAI,OAAO,YAAa,UAAU;AAChC,eAAK,QAAQ,KAAK,+BAA+B,QAAS,IAAI,EAAE;AAAA,QAClE;AACA,gBAAQ;AAAA,MACV,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,QAAuB;AAC3B,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,WAAK,IAAI,MAAM,CAAC,QAAgB;AAC9B,YAAI,IAAK,QAAO,GAAG;AACnB,gBAAQ;AAAA,MACV,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;","names":[]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { type Server } from 'node:http';
|
|
3
|
+
interface WorkerResponse {
|
|
4
|
+
agent_name: string;
|
|
5
|
+
worker_type: string;
|
|
6
|
+
active_jobs: number;
|
|
7
|
+
sdk_version: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class HTTPServer {
|
|
10
|
+
#private;
|
|
11
|
+
host: string;
|
|
12
|
+
port: number;
|
|
13
|
+
app: Server;
|
|
14
|
+
constructor(host: string, port: number, workerListener: () => WorkerResponse);
|
|
15
|
+
run(): Promise<void>;
|
|
16
|
+
close(): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=http_server.d.ts.map
|
package/dist/http_server.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http_server.d.ts","sourceRoot":"","sources":["../src/http_server.ts"],"names":[],"mappings":";AAGA,OAAO,EAAwB,KAAK,MAAM,EAAqC,MAAM,WAAW,CAAC;AAQjG,UAAU,cAAc;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,qBAAa,UAAU;;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;gBAGA,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,cAAc;IAiBtE,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;IAapB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAQ7B"}
|
|
1
|
+
{"version":3,"file":"http_server.d.ts","sourceRoot":"","sources":["../src/http_server.ts"],"names":[],"mappings":";AAGA,OAAO,EAAwB,KAAK,MAAM,EAAqC,MAAM,WAAW,CAAC;AAQjG,UAAU,cAAc;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,qBAAa,UAAU;;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;gBAGA,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,cAAc;IAiBtE,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;IAapB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAQ7B"}
|
package/dist/http_server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/http_server.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { type IncomingMessage, type Server, type ServerResponse, createServer } from 'node:http';\nimport { log } from './log.js';\n\nconst healthCheck = async (res: ServerResponse) => {\n res.writeHead(200);\n res.end('OK');\n};\n\ninterface WorkerResponse {\n agent_name: string;\n worker_type: string;\n active_jobs: number;\n}\n\nexport class HTTPServer {\n host: string;\n port: number;\n app: Server;\n #logger = log();\n\n constructor(host: string, port: number, workerListener: () => WorkerResponse) {\n this.host = host;\n this.port = port;\n\n this.app = createServer((req: IncomingMessage, res: ServerResponse) => {\n if (req.url === '/') {\n healthCheck(res);\n } else if (req.url === '/worker') {\n res.writeHead(200, { 'Contet-Type': 'application/json' });\n res.end(JSON.stringify(workerListener()));\n } else {\n res.writeHead(404);\n res.end('not found');\n }\n });\n }\n\n async run(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.listen(this.port, this.host, (err?: Error) => {\n if (err) reject(err);\n const address = this.app.address();\n if (typeof address! !== 'string') {\n this.#logger.info(`Server is listening on port ${address!.port}`);\n }\n resolve();\n });\n });\n }\n\n async close(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.close((err?: Error) => {\n if (err) reject(err);\n resolve();\n });\n });\n }\n}\n"],"mappings":"AAGA,SAAiE,oBAAoB;AACrF,SAAS,WAAW;AAEpB,MAAM,cAAc,OAAO,QAAwB;AACjD,MAAI,UAAU,GAAG;AACjB,MAAI,IAAI,IAAI;AACd;
|
|
1
|
+
{"version":3,"sources":["../src/http_server.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { type IncomingMessage, type Server, type ServerResponse, createServer } from 'node:http';\nimport { log } from './log.js';\n\nconst healthCheck = async (res: ServerResponse) => {\n res.writeHead(200);\n res.end('OK');\n};\n\ninterface WorkerResponse {\n agent_name: string;\n worker_type: string;\n active_jobs: number;\n sdk_version: string;\n}\n\nexport class HTTPServer {\n host: string;\n port: number;\n app: Server;\n #logger = log();\n\n constructor(host: string, port: number, workerListener: () => WorkerResponse) {\n this.host = host;\n this.port = port;\n\n this.app = createServer((req: IncomingMessage, res: ServerResponse) => {\n if (req.url === '/') {\n healthCheck(res);\n } else if (req.url === '/worker') {\n res.writeHead(200, { 'Contet-Type': 'application/json' });\n res.end(JSON.stringify(workerListener()));\n } else {\n res.writeHead(404);\n res.end('not found');\n }\n });\n }\n\n async run(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app.listen(this.port, this.host, (err?: Error) => {\n if (err) reject(err);\n const address = this.app.address();\n if (typeof address! !== 'string') {\n this.#logger.info(`Server is listening on port ${address!.port}`);\n }\n resolve();\n });\n });\n }\n\n async close(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.app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|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LiveKit Agents is a framework for building realtime programmable participants that run on
|
|
3
|
+
* servers.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://docs.livekit.io/agents/overview | LiveKit Agents documentation}
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
import * as cli from './cli.js';
|
|
9
|
+
import * as ipc from './ipc/index.js';
|
|
10
|
+
import * as llm from './llm/index.js';
|
|
11
|
+
import * as metrics from './metrics/index.js';
|
|
12
|
+
import * as multimodal from './multimodal/index.js';
|
|
13
|
+
import * as pipeline from './pipeline/index.js';
|
|
14
|
+
import * as stt from './stt/index.js';
|
|
15
|
+
import * as tokenize from './tokenize/index.js';
|
|
16
|
+
import * as tts from './tts/index.js';
|
|
17
|
+
export * from './vad.js';
|
|
18
|
+
export * from './plugin.js';
|
|
19
|
+
export * from './version.js';
|
|
20
|
+
export * from './job.js';
|
|
21
|
+
export * from './worker.js';
|
|
22
|
+
export * from './utils.js';
|
|
23
|
+
export * from './log.js';
|
|
24
|
+
export * from './generator.js';
|
|
25
|
+
export * from './audio.js';
|
|
26
|
+
export * from './transcription.js';
|
|
27
|
+
export * from './inference_runner.js';
|
|
28
|
+
export { cli, stt, tts, llm, pipeline, multimodal, tokenize, metrics, ipc };
|
|
29
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** @internal */
|
|
2
|
+
export declare abstract class InferenceRunner {
|
|
3
|
+
static INFERENCE_METHOD: string;
|
|
4
|
+
static registeredRunners: {
|
|
5
|
+
[id: string]: string;
|
|
6
|
+
};
|
|
7
|
+
static registerRunner(method: string, importPath: string): void;
|
|
8
|
+
abstract initialize(): Promise<void>;
|
|
9
|
+
abstract run(data: unknown): Promise<unknown>;
|
|
10
|
+
abstract close(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=inference_runner.d.ts.map
|
|
@@ -21,11 +21,12 @@ __export(inference_proc_executor_exports, {
|
|
|
21
21
|
InferenceProcExecutor: () => InferenceProcExecutor
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(inference_proc_executor_exports);
|
|
24
|
+
var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
|
|
25
|
+
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
24
26
|
var import_node_child_process = require("node:child_process");
|
|
25
27
|
var import_node_crypto = require("node:crypto");
|
|
26
28
|
var import_log = require("../log.cjs");
|
|
27
29
|
var import_supervised_proc = require("./supervised_proc.cjs");
|
|
28
|
-
const import_meta = {};
|
|
29
30
|
class PendingInference {
|
|
30
31
|
promise = new Promise((resolve) => {
|
|
31
32
|
this.resolve = resolve;
|
|
@@ -60,7 +61,7 @@ class InferenceProcExecutor extends import_supervised_proc.SupervisedProc {
|
|
|
60
61
|
this.#runners = runners;
|
|
61
62
|
}
|
|
62
63
|
createProcess() {
|
|
63
|
-
return (0, import_node_child_process.fork)(new URL(
|
|
64
|
+
return (0, import_node_child_process.fork)(new URL("./inference_proc_lazy_main.js", importMetaUrl), [
|
|
64
65
|
JSON.stringify(this.#runners)
|
|
65
66
|
]);
|
|
66
67
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ipc/inference_proc_executor.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChildProcess } from 'node:child_process';\nimport { fork } from 'node:child_process';\nimport { randomUUID } from 'node:crypto';\nimport { log } from '../log.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { IPCMessage } from './message.js';\nimport { SupervisedProc } from './supervised_proc.js';\n\nclass PendingInference {\n promise = new Promise<{ requestId: string; data: unknown; error?: Error }>((resolve) => {\n this.resolve = resolve;\n });\n resolve(arg: { requestId: string; data: unknown; error?: Error }) {\n arg;\n }\n}\n\nexport class InferenceProcExecutor extends SupervisedProc implements InferenceExecutor {\n #runners: { [id: string]: string };\n #activeRequests: { [id: string]: PendingInference } = {};\n #logger = log();\n\n constructor({\n runners,\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n }: {\n runners: { [id: string]: string };\n initializeTimeout: number;\n closeTimeout: number;\n memoryWarnMB: number;\n memoryLimitMB: number;\n pingInterval: number;\n pingTimeout: number;\n highPingThreshold: number;\n }) {\n super(\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n );\n this.#runners = runners;\n }\n\n createProcess(): ChildProcess {\n return fork(new URL(
|
|
1
|
+
{"version":3,"sources":["../../src/ipc/inference_proc_executor.ts","../../../node_modules/.pnpm/tsup@8.4.0_@microsoft+api-extractor@7.43.7_@types+node@22.5.5__postcss@8.4.38_tsx@4.19.2_typescript@5.4.5/node_modules/tsup/assets/cjs_shims.js"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChildProcess } from 'node:child_process';\nimport { fork } from 'node:child_process';\nimport { randomUUID } from 'node:crypto';\nimport { log } from '../log.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { IPCMessage } from './message.js';\nimport { SupervisedProc } from './supervised_proc.js';\n\nclass PendingInference {\n promise = new Promise<{ requestId: string; data: unknown; error?: Error }>((resolve) => {\n this.resolve = resolve;\n });\n resolve(arg: { requestId: string; data: unknown; error?: Error }) {\n arg;\n }\n}\n\nexport class InferenceProcExecutor extends SupervisedProc implements InferenceExecutor {\n #runners: { [id: string]: string };\n #activeRequests: { [id: string]: PendingInference } = {};\n #logger = log();\n\n constructor({\n runners,\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n }: {\n runners: { [id: string]: string };\n initializeTimeout: number;\n closeTimeout: number;\n memoryWarnMB: number;\n memoryLimitMB: number;\n pingInterval: number;\n pingTimeout: number;\n highPingThreshold: number;\n }) {\n super(\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n );\n this.#runners = runners;\n }\n\n createProcess(): ChildProcess {\n return fork(new URL('./inference_proc_lazy_main.js', import.meta.url), [\n JSON.stringify(this.#runners),\n ]);\n }\n\n async mainTask(proc: ChildProcess) {\n proc.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'inferenceResponse':\n const res = this.#activeRequests[msg.value.requestId];\n delete this.#activeRequests[msg.value.requestId];\n if (!res) {\n this.#logger\n .child({ requestId: msg.value.requestId })\n .warn('received unexpected inference response');\n return;\n }\n\n res.resolve(msg.value);\n }\n });\n }\n\n async doInference(method: string, data: unknown): Promise<unknown> {\n const requestId = 'inference_req_' + randomUUID();\n const fut = new PendingInference();\n this.proc!.send({ case: 'inferenceRequest', value: { requestId, method, data } });\n this.#activeRequests[requestId] = fut;\n\n const res = await fut.promise;\n if (res.error) {\n throw new Error(`inference of ${method} failed: ${res.error}`);\n }\n return res.data;\n }\n}\n","// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () =>\n typeof document === 'undefined'\n ? new URL(`file:${__filename}`).href\n : (document.currentScript && document.currentScript.src) ||\n new URL('main.js', document.baseURI).href\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACKA,IAAM,mBAAmB,MACvB,OAAO,aAAa,cAChB,IAAI,IAAI,QAAQ,UAAU,EAAE,EAAE,OAC7B,SAAS,iBAAiB,SAAS,cA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AChD,UAAM,MAAM,IAAI,iBAAiB;AACjC,SAAK,KAAM,KAAK,EAAE,MAAM,oBAAoB,OAAO,EAAE,WAAW,QAAQ,KAAK,EAAE,CAAC;AAChF,SAAK,gBAAgB,SAAS,IAAI;AAElC,UAAM,MAAM,MAAM,IAAI;AACtB,QAAI,IAAI,OAAO;AACb,YAAM,IAAI,MAAM,gBAAgB,MAAM,YAAY,IAAI,KAAK,EAAE;AAAA,IAC/D;AACA,WAAO,IAAI;AAAA,EACb;AACF;","names":[]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import type { ChildProcess } from 'node:child_process';
|
|
3
|
+
import type { InferenceExecutor } from './inference_executor.js';
|
|
4
|
+
import { SupervisedProc } from './supervised_proc.js';
|
|
5
|
+
export declare class InferenceProcExecutor extends SupervisedProc implements InferenceExecutor {
|
|
6
|
+
#private;
|
|
7
|
+
constructor({ runners, initializeTimeout, closeTimeout, memoryWarnMB, memoryLimitMB, pingInterval, pingTimeout, highPingThreshold, }: {
|
|
8
|
+
runners: {
|
|
9
|
+
[id: string]: string;
|
|
10
|
+
};
|
|
11
|
+
initializeTimeout: number;
|
|
12
|
+
closeTimeout: number;
|
|
13
|
+
memoryWarnMB: number;
|
|
14
|
+
memoryLimitMB: number;
|
|
15
|
+
pingInterval: number;
|
|
16
|
+
pingTimeout: number;
|
|
17
|
+
highPingThreshold: number;
|
|
18
|
+
});
|
|
19
|
+
createProcess(): ChildProcess;
|
|
20
|
+
mainTask(proc: ChildProcess): Promise<void>;
|
|
21
|
+
doInference(method: string, data: unknown): Promise<unknown>;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=inference_proc_executor.d.ts.map
|
|
@@ -36,7 +36,7 @@ class InferenceProcExecutor extends SupervisedProc {
|
|
|
36
36
|
this.#runners = runners;
|
|
37
37
|
}
|
|
38
38
|
createProcess() {
|
|
39
|
-
return fork(new URL(
|
|
39
|
+
return fork(new URL("./inference_proc_lazy_main.js", import.meta.url), [
|
|
40
40
|
JSON.stringify(this.#runners)
|
|
41
41
|
]);
|
|
42
42
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ipc/inference_proc_executor.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChildProcess } from 'node:child_process';\nimport { fork } from 'node:child_process';\nimport { randomUUID } from 'node:crypto';\nimport { log } from '../log.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { IPCMessage } from './message.js';\nimport { SupervisedProc } from './supervised_proc.js';\n\nclass PendingInference {\n promise = new Promise<{ requestId: string; data: unknown; error?: Error }>((resolve) => {\n this.resolve = resolve;\n });\n resolve(arg: { requestId: string; data: unknown; error?: Error }) {\n arg;\n }\n}\n\nexport class InferenceProcExecutor extends SupervisedProc implements InferenceExecutor {\n #runners: { [id: string]: string };\n #activeRequests: { [id: string]: PendingInference } = {};\n #logger = log();\n\n constructor({\n runners,\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n }: {\n runners: { [id: string]: string };\n initializeTimeout: number;\n closeTimeout: number;\n memoryWarnMB: number;\n memoryLimitMB: number;\n pingInterval: number;\n pingTimeout: number;\n highPingThreshold: number;\n }) {\n super(\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n );\n this.#runners = runners;\n }\n\n createProcess(): ChildProcess {\n return fork(new URL(
|
|
1
|
+
{"version":3,"sources":["../../src/ipc/inference_proc_executor.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChildProcess } from 'node:child_process';\nimport { fork } from 'node:child_process';\nimport { randomUUID } from 'node:crypto';\nimport { log } from '../log.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { IPCMessage } from './message.js';\nimport { SupervisedProc } from './supervised_proc.js';\n\nclass PendingInference {\n promise = new Promise<{ requestId: string; data: unknown; error?: Error }>((resolve) => {\n this.resolve = resolve;\n });\n resolve(arg: { requestId: string; data: unknown; error?: Error }) {\n arg;\n }\n}\n\nexport class InferenceProcExecutor extends SupervisedProc implements InferenceExecutor {\n #runners: { [id: string]: string };\n #activeRequests: { [id: string]: PendingInference } = {};\n #logger = log();\n\n constructor({\n runners,\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n }: {\n runners: { [id: string]: string };\n initializeTimeout: number;\n closeTimeout: number;\n memoryWarnMB: number;\n memoryLimitMB: number;\n pingInterval: number;\n pingTimeout: number;\n highPingThreshold: number;\n }) {\n super(\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n );\n this.#runners = runners;\n }\n\n createProcess(): ChildProcess {\n return fork(new URL('./inference_proc_lazy_main.js', import.meta.url), [\n JSON.stringify(this.#runners),\n ]);\n }\n\n async mainTask(proc: ChildProcess) {\n proc.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'inferenceResponse':\n const res = this.#activeRequests[msg.value.requestId];\n delete this.#activeRequests[msg.value.requestId];\n if (!res) {\n this.#logger\n .child({ requestId: msg.value.requestId })\n .warn('received unexpected inference response');\n return;\n }\n\n res.resolve(msg.value);\n }\n });\n }\n\n async doInference(method: string, data: unknown): Promise<unknown> {\n const requestId = 'inference_req_' + randomUUID();\n const fut = new PendingInference();\n this.proc!.send({ case: 'inferenceRequest', value: { requestId, method, data } });\n this.#activeRequests[requestId] = fut;\n\n const res = await fut.promise;\n if (res.error) {\n throw new Error(`inference of ${method} failed: ${res.error}`);\n }\n return res.data;\n }\n}\n"],"mappings":"AAIA,SAAS,YAAY;AACrB,SAAS,kBAAkB;AAC3B,SAAS,W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AE,CAAC;AAChF,SAAK,gBAAgB,SAAS,IAAI;AAElC,UAAM,MAAM,MAAM,IAAI;AACtB,QAAI,IAAI,OAAO;AACb,YAAM,IAAI,MAAM,gBAAgB,MAAM,YAAY,IAAI,KAAK,EAAE;AAAA,IAC/D;AACA,WAAO,IAAI;AAAA,EACb;AACF;","names":[]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { RunningJobInfo } from '../job.js';
|
|
2
|
+
export interface JobExecutor {
|
|
3
|
+
started: boolean;
|
|
4
|
+
userArguments: any;
|
|
5
|
+
runningJob: RunningJobInfo | undefined;
|
|
6
|
+
status: JobStatus;
|
|
7
|
+
start(): Promise<void>;
|
|
8
|
+
join(): Promise<void>;
|
|
9
|
+
initialize(): Promise<void>;
|
|
10
|
+
close(): Promise<void>;
|
|
11
|
+
launchJob(info: RunningJobInfo): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
export declare enum JobStatus {
|
|
14
|
+
RUNNING = 0,
|
|
15
|
+
FAILED = 1,
|
|
16
|
+
SUCCESS = 2
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=job_executor.d.ts.map
|
|
@@ -21,11 +21,12 @@ __export(job_proc_executor_exports, {
|
|
|
21
21
|
JobProcExecutor: () => JobProcExecutor
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(job_proc_executor_exports);
|
|
24
|
+
var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
|
|
25
|
+
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
24
26
|
var import_node_child_process = require("node:child_process");
|
|
25
27
|
var import_log = require("../log.cjs");
|
|
26
28
|
var import_job_executor = require("./job_executor.cjs");
|
|
27
29
|
var import_supervised_proc = require("./supervised_proc.cjs");
|
|
28
|
-
const import_meta = {};
|
|
29
30
|
class JobProcExecutor extends import_supervised_proc.SupervisedProc {
|
|
30
31
|
#userArgs;
|
|
31
32
|
#jobStatus;
|
|
@@ -63,7 +64,7 @@ class JobProcExecutor extends import_supervised_proc.SupervisedProc {
|
|
|
63
64
|
return this.#runningJob;
|
|
64
65
|
}
|
|
65
66
|
createProcess() {
|
|
66
|
-
return (0, import_node_child_process.fork)(new URL(
|
|
67
|
+
return (0, import_node_child_process.fork)(new URL("./job_proc_lazy_main.js", importMetaUrl), [this.#agent]);
|
|
67
68
|
}
|
|
68
69
|
async mainTask(proc) {
|
|
69
70
|
proc.on("message", (msg) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ipc/job_proc_executor.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChildProcess } from 'node:child_process';\nimport { fork } from 'node:child_process';\nimport type { RunningJobInfo } from '../job.js';\nimport { log } from '../log.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { JobExecutor } from './job_executor.js';\nimport { JobStatus } from './job_executor.js';\nimport type { IPCMessage } from './message.js';\nimport { SupervisedProc } from './supervised_proc.js';\n\nexport class JobProcExecutor extends SupervisedProc implements JobExecutor {\n #userArgs?: any;\n #jobStatus?: JobStatus;\n #runningJob?: RunningJobInfo;\n #agent: string;\n #inferenceExecutor?: InferenceExecutor;\n #inferenceTasks: Promise<void>[] = [];\n #logger = log();\n\n constructor(\n agent: string,\n inferenceExecutor: InferenceExecutor | undefined,\n initializeTimeout: number,\n closeTimeout: number,\n memoryWarnMB: number,\n memoryLimitMB: number,\n pingInterval: number,\n pingTimeout: number,\n highPingThreshold: number,\n ) {\n super(\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n );\n this.#agent = agent;\n this.#inferenceExecutor = inferenceExecutor;\n }\n\n get status(): JobStatus {\n if (this.#jobStatus) {\n return this.#jobStatus;\n }\n throw new Error('job status not available');\n }\n\n get userArguments(): any {\n return this.#userArgs;\n }\n\n set userArguments(args: any) {\n this.#userArgs = args;\n }\n\n get runningJob(): RunningJobInfo | undefined {\n return this.#runningJob;\n }\n\n createProcess(): ChildProcess {\n return fork(new URL(
|
|
1
|
+
{"version":3,"sources":["../../src/ipc/job_proc_executor.ts","../../../node_modules/.pnpm/tsup@8.4.0_@microsoft+api-extractor@7.43.7_@types+node@22.5.5__postcss@8.4.38_tsx@4.19.2_typescript@5.4.5/node_modules/tsup/assets/cjs_shims.js"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChildProcess } from 'node:child_process';\nimport { fork } from 'node:child_process';\nimport type { RunningJobInfo } from '../job.js';\nimport { log } from '../log.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { JobExecutor } from './job_executor.js';\nimport { JobStatus } from './job_executor.js';\nimport type { IPCMessage } from './message.js';\nimport { SupervisedProc } from './supervised_proc.js';\n\nexport class JobProcExecutor extends SupervisedProc implements JobExecutor {\n #userArgs?: any;\n #jobStatus?: JobStatus;\n #runningJob?: RunningJobInfo;\n #agent: string;\n #inferenceExecutor?: InferenceExecutor;\n #inferenceTasks: Promise<void>[] = [];\n #logger = log();\n\n constructor(\n agent: string,\n inferenceExecutor: InferenceExecutor | undefined,\n initializeTimeout: number,\n closeTimeout: number,\n memoryWarnMB: number,\n memoryLimitMB: number,\n pingInterval: number,\n pingTimeout: number,\n highPingThreshold: number,\n ) {\n super(\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n );\n this.#agent = agent;\n this.#inferenceExecutor = inferenceExecutor;\n }\n\n get status(): JobStatus {\n if (this.#jobStatus) {\n return this.#jobStatus;\n }\n throw new Error('job status not available');\n }\n\n get userArguments(): any {\n return this.#userArgs;\n }\n\n set userArguments(args: any) {\n this.#userArgs = args;\n }\n\n get runningJob(): RunningJobInfo | undefined {\n return this.#runningJob;\n }\n\n createProcess(): ChildProcess {\n return fork(new URL('./job_proc_lazy_main.js', import.meta.url), [this.#agent]);\n }\n\n async mainTask(proc: ChildProcess) {\n proc.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'inferenceRequest':\n this.#inferenceTasks.push(this.#doInferenceTask(proc, msg.value));\n }\n });\n }\n\n async #doInferenceTask(\n proc: ChildProcess,\n req: { method: string; requestId: string; data: unknown },\n ) {\n if (!this.#inferenceExecutor) {\n this.#logger.warn('inference request received but no inference executor');\n proc.send({\n case: 'inferenceResponse',\n value: { requestId: req.requestId, error: new Error('no inference executor') },\n });\n return;\n }\n\n try {\n const data = await this.#inferenceExecutor.doInference(req.method, req.data);\n proc.send({ case: 'inferenceResponse', value: { requestId: req.requestId, data } });\n } catch (error) {\n proc.send({ case: 'inferenceResponse', value: { requestId: req.requestId, error } });\n }\n }\n\n async launchJob(info: RunningJobInfo) {\n if (this.#runningJob) {\n throw Error('process already has a running job');\n }\n if (!this.init.done) {\n throw Error('process not initialized');\n }\n this.#jobStatus = JobStatus.RUNNING;\n this.#runningJob = info;\n\n this.proc!.send({ case: 'startJobRequest', value: { runningJob: info } });\n }\n}\n","// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () =>\n typeof document === 'undefined'\n ? new URL(`file:${__filename}`).href\n : (document.currentScript && document.currentScript.src) ||\n new URL('main.js', document.baseURI).href\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACKA,IAAM,mBAAmB,MACvB,OAAO,aAAa,cAChB,IAAI,IAAI,QAAQ,UAAU,EAAE,EAAE,OAC7B,SAAS,iBAAiB,SAAS,cAAc,OAClD,IAAI,IAAI,WAAW,SAAS,OAAO,EAAE;AAEpC,IAAM,gBAAgC,iCAAiB;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AA,MAC/E,CAAC;AACD;AAAA,IACF;AAEA,QAAI;AACF,YAAM,OAAO,MAAM,KAAK,mBAAmB,YAAY,IAAI,QAAQ,IAAI,IAAI;AAC3E,WAAK,KAAK,EAAE,MAAM,qBAAqB,OAAO,EAAE,WAAW,IAAI,WAAW,KAAK,EAAE,CAAC;AAAA,IACpF,SAAS,OAAO;AACd,WAAK,KAAK,EAAE,MAAM,qBAAqB,OAAO,EAAE,WAAW,IAAI,WAAW,MAAM,EAAE,CAAC;AAAA,IACrF;AAAA,EACF;AAAA,EAEA,MAAM,UAAU,MAAsB;AACpC,QAAI,KAAK,aAAa;AACpB,YAAM,MAAM,mCAAmC;AAAA,IACjD;AACA,QAAI,CAAC,KAAK,KAAK,MAAM;AACnB,YAAM,MAAM,yBAAyB;AAAA,IACvC;AACA,SAAK,aAAa,8BAAU;AAC5B,SAAK,cAAc;AAEnB,SAAK,KAAM,KAAK,EAAE,MAAM,mBAAmB,OAAO,EAAE,YAAY,KAAK,EAAE,CAAC;AAAA,EAC1E;AACF;","names":[]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import type { ChildProcess } from 'node:child_process';
|
|
3
|
+
import type { RunningJobInfo } from '../job.js';
|
|
4
|
+
import type { InferenceExecutor } from './inference_executor.js';
|
|
5
|
+
import type { JobExecutor } from './job_executor.js';
|
|
6
|
+
import { JobStatus } from './job_executor.js';
|
|
7
|
+
import { SupervisedProc } from './supervised_proc.js';
|
|
8
|
+
export declare class JobProcExecutor extends SupervisedProc implements JobExecutor {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(agent: string, inferenceExecutor: InferenceExecutor | undefined, initializeTimeout: number, closeTimeout: number, memoryWarnMB: number, memoryLimitMB: number, pingInterval: number, pingTimeout: number, highPingThreshold: number);
|
|
11
|
+
get status(): JobStatus;
|
|
12
|
+
get userArguments(): any;
|
|
13
|
+
set userArguments(args: any);
|
|
14
|
+
get runningJob(): RunningJobInfo | undefined;
|
|
15
|
+
createProcess(): ChildProcess;
|
|
16
|
+
mainTask(proc: ChildProcess): Promise<void>;
|
|
17
|
+
launchJob(info: RunningJobInfo): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=job_proc_executor.d.ts.map
|
|
@@ -39,7 +39,7 @@ class JobProcExecutor extends SupervisedProc {
|
|
|
39
39
|
return this.#runningJob;
|
|
40
40
|
}
|
|
41
41
|
createProcess() {
|
|
42
|
-
return fork(new URL(
|
|
42
|
+
return fork(new URL("./job_proc_lazy_main.js", import.meta.url), [this.#agent]);
|
|
43
43
|
}
|
|
44
44
|
async mainTask(proc) {
|
|
45
45
|
proc.on("message", (msg) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ipc/job_proc_executor.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChildProcess } from 'node:child_process';\nimport { fork } from 'node:child_process';\nimport type { RunningJobInfo } from '../job.js';\nimport { log } from '../log.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { JobExecutor } from './job_executor.js';\nimport { JobStatus } from './job_executor.js';\nimport type { IPCMessage } from './message.js';\nimport { SupervisedProc } from './supervised_proc.js';\n\nexport class JobProcExecutor extends SupervisedProc implements JobExecutor {\n #userArgs?: any;\n #jobStatus?: JobStatus;\n #runningJob?: RunningJobInfo;\n #agent: string;\n #inferenceExecutor?: InferenceExecutor;\n #inferenceTasks: Promise<void>[] = [];\n #logger = log();\n\n constructor(\n agent: string,\n inferenceExecutor: InferenceExecutor | undefined,\n initializeTimeout: number,\n closeTimeout: number,\n memoryWarnMB: number,\n memoryLimitMB: number,\n pingInterval: number,\n pingTimeout: number,\n highPingThreshold: number,\n ) {\n super(\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n );\n this.#agent = agent;\n this.#inferenceExecutor = inferenceExecutor;\n }\n\n get status(): JobStatus {\n if (this.#jobStatus) {\n return this.#jobStatus;\n }\n throw new Error('job status not available');\n }\n\n get userArguments(): any {\n return this.#userArgs;\n }\n\n set userArguments(args: any) {\n this.#userArgs = args;\n }\n\n get runningJob(): RunningJobInfo | undefined {\n return this.#runningJob;\n }\n\n createProcess(): ChildProcess {\n return fork(new URL(
|
|
1
|
+
{"version":3,"sources":["../../src/ipc/job_proc_executor.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChildProcess } from 'node:child_process';\nimport { fork } from 'node:child_process';\nimport type { RunningJobInfo } from '../job.js';\nimport { log } from '../log.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { JobExecutor } from './job_executor.js';\nimport { JobStatus } from './job_executor.js';\nimport type { IPCMessage } from './message.js';\nimport { SupervisedProc } from './supervised_proc.js';\n\nexport class JobProcExecutor extends SupervisedProc implements JobExecutor {\n #userArgs?: any;\n #jobStatus?: JobStatus;\n #runningJob?: RunningJobInfo;\n #agent: string;\n #inferenceExecutor?: InferenceExecutor;\n #inferenceTasks: Promise<void>[] = [];\n #logger = log();\n\n constructor(\n agent: string,\n inferenceExecutor: InferenceExecutor | undefined,\n initializeTimeout: number,\n closeTimeout: number,\n memoryWarnMB: number,\n memoryLimitMB: number,\n pingInterval: number,\n pingTimeout: number,\n highPingThreshold: number,\n ) {\n super(\n initializeTimeout,\n closeTimeout,\n memoryWarnMB,\n memoryLimitMB,\n pingInterval,\n pingTimeout,\n highPingThreshold,\n );\n this.#agent = agent;\n this.#inferenceExecutor = inferenceExecutor;\n }\n\n get status(): JobStatus {\n if (this.#jobStatus) {\n return this.#jobStatus;\n }\n throw new Error('job status not available');\n }\n\n get userArguments(): any {\n return this.#userArgs;\n }\n\n set userArguments(args: any) {\n this.#userArgs = args;\n }\n\n get runningJob(): RunningJobInfo | undefined {\n return this.#runningJob;\n }\n\n createProcess(): ChildProcess {\n return fork(new URL('./job_proc_lazy_main.js', import.meta.url), [this.#agent]);\n }\n\n async mainTask(proc: ChildProcess) {\n proc.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'inferenceRequest':\n this.#inferenceTasks.push(this.#doInferenceTask(proc, msg.value));\n }\n });\n }\n\n async #doInferenceTask(\n proc: ChildProcess,\n req: { method: string; requestId: string; data: unknown },\n ) {\n if (!this.#inferenceExecutor) {\n this.#logger.warn('inference request received but no inference executor');\n proc.send({\n case: 'inferenceResponse',\n value: { requestId: req.requestId, error: new Error('no inference executor') },\n });\n return;\n }\n\n try {\n const data = await this.#inferenceExecutor.doInference(req.method, req.data);\n proc.send({ case: 'inferenceResponse', value: { requestId: req.requestId, data } });\n } catch (error) {\n proc.send({ case: 'inferenceResponse', value: { requestId: req.requestId, error } });\n }\n }\n\n async launchJob(info: RunningJobInfo) {\n if (this.#runningJob) {\n throw Error('process already has a running job');\n }\n if (!this.init.done) {\n throw Error('process not initialized');\n }\n this.#jobStatus = JobStatus.RUNNING;\n this.#runningJob = info;\n\n this.proc!.send({ case: 'startJobRequest', value: { runningJob: info } });\n }\n}\n"],"mappings":"AAIA,SAAS,YAAY;AAErB,SAAS,WAAW;AAGpB,SAAS,iBAAiB;AAE1B,SAAS,sBAAsB;AAExB,MAAM,wBAAwB,eAAsC;AAAA,EACzE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,kBAAmC,CAAC;AAAA,EACpC,UAAU,IAAI;AAAA,EAEd,YACE,OACA,mBACA,mBACA,cACA,cACA,eACA,cACA,aACA,mBACA;AACA;AAAA,MACE;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AACA,SAAK,SAAS;AACd,SAAK,qBAAqB;AAAA,EAC5B;AAAA,EAEA,IAAI,SAAoB;AACtB,QAAI,KAAK,YAAY;AACnB,aAAO,KAAK;AAAA,IACd;AAC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AM,yBAAyB;AAAA,IACvC;AACA,SAAK,aAAa,UAAU;AAC5B,SAAK,cAAc;AAEnB,SAAK,KAAM,KAAK,EAAE,MAAM,mBAAmB,OAAO,EAAE,YAAY,KAAK,EAAE,CAAC;AAAA,EAC1E;AACF;","names":[]}
|
|
@@ -35,7 +35,7 @@ class InfClient {
|
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
async doInference(method, data) {
|
|
38
|
-
const requestId = "inference_job_" + import_node_crypto.randomUUID;
|
|
38
|
+
const requestId = "inference_job_" + (0, import_node_crypto.randomUUID)();
|
|
39
39
|
process.send({ case: "inferenceRequest", value: { requestId, method, data } });
|
|
40
40
|
this.#requests[requestId] = new PendingInference();
|
|
41
41
|
const resp = await this.#requests[requestId].promise;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ipc/job_proc_lazy_main.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Room, RoomEvent } from '@livekit/rtc-node';\nimport { randomUUID } from 'node:crypto';\nimport { EventEmitter, once } from 'node:events';\nimport { pathToFileURL } from 'node:url';\nimport type { Logger } from 'pino';\nimport { type Agent, isAgent } from '../generator.js';\nimport { CurrentJobContext, JobContext, JobProcess, type RunningJobInfo } from '../job.js';\nimport { initializeLogger, log } from '../log.js';\nimport { Future } from '../utils.js';\nimport { defaultInitializeProcessFunc } from '../worker.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { IPCMessage } from './message.js';\n\nconst ORPHANED_TIMEOUT = 15 * 1000;\n\ntype JobTask = {\n ctx: JobContext;\n task: Promise<void>;\n};\n\nclass PendingInference {\n promise = new Promise<{ requestId: string; data: unknown; error?: Error }>((resolve) => {\n this.resolve = resolve; // this is how JavaScript lets you resolve promises externally\n });\n resolve(arg: { requestId: string; data: unknown; error?: Error }) {\n arg; // useless call to counteract TypeScript E6133\n }\n}\n\nclass InfClient implements InferenceExecutor {\n #requests: { [id: string]: PendingInference } = {};\n\n constructor() {\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'inferenceResponse':\n const fut = this.#requests[msg.value.requestId];\n delete this.#requests[msg.value.requestId];\n if (!fut) {\n log().child({ resp: msg.value }).warn('received unexpected inference response');\n return;\n }\n fut.resolve(msg.value);\n break;\n }\n });\n }\n\n async doInference(method: string, data: unknown): Promise<unknown> {\n const requestId = 'inference_job_' + randomUUID;\n process.send!({ case: 'inferenceRequest', value: { requestId, method, data } });\n this.#requests[requestId] = new PendingInference();\n const resp = await this.#requests[requestId]!.promise;\n if (resp.error) {\n throw new Error(`inference of ${method} failed: ${resp.error.message}`);\n }\n return resp.data;\n }\n}\n\nconst startJob = (\n proc: JobProcess,\n func: (ctx: JobContext) => Promise<void>,\n info: RunningJobInfo,\n closeEvent: EventEmitter,\n logger: Logger,\n joinFuture: Future,\n): JobTask => {\n let connect = false;\n let shutdown = false;\n\n const room = new Room();\n room.on(RoomEvent.Disconnected, () => {\n if (!shutdown) {\n closeEvent.emit('close', false);\n }\n });\n\n const onConnect = () => {\n connect = true;\n };\n const onShutdown = (reason: string) => {\n shutdown = true;\n closeEvent.emit('close', true, reason);\n };\n\n const ctx = new JobContext(proc, info, room, onConnect, onShutdown, new InfClient());\n new CurrentJobContext(ctx);\n\n const task = new Promise<void>(async () => {\n const unconnectedTimeout = setTimeout(() => {\n if (!(connect || shutdown)) {\n logger.warn(\n 'room not connect after job_entry was called after 10 seconds, ',\n 'did you forget to call ctx.connect()?',\n );\n }\n }, 10000);\n func(ctx).finally(() => clearTimeout(unconnectedTimeout));\n\n await once(closeEvent, 'close').then((close) => {\n logger.debug('shutting down');\n shutdown = true;\n process.send!({ case: 'exiting', value: { reason: close[1] } });\n });\n\n await room.disconnect();\n logger.debug('disconnected from room');\n\n const shutdownTasks = [];\n for (const callback of ctx.shutdownCallbacks) {\n shutdownTasks.push(callback());\n }\n await Promise.all(shutdownTasks).catch((error) =>\n logger.error('error while shutting down the job', error),\n );\n\n process.send!({ case: 'done' });\n logger.info('job completed.');\n joinFuture.resolve();\n });\n\n return { ctx, task };\n};\n\n(async () => {\n if (process.send) {\n const join = new Future();\n\n // process.argv:\n // [0] `node'\n // [1] import.meta.filename\n // [2] import.meta.filename of function containing entry file\n const moduleFile = process.argv[2];\n const agent: Agent = await import(pathToFileURL(moduleFile!).pathname).then((module) => {\n const agent = module.default;\n if (agent === undefined || !isAgent(agent)) {\n throw new Error(`Unable to load agent: Missing or invalid default export in ${moduleFile}`);\n }\n return agent;\n });\n if (!agent.prewarm) {\n agent.prewarm = defaultInitializeProcessFunc;\n }\n\n // don't do anything on C-c\n // this is handled in cli, triggering a termination of all child processes at once.\n process.on('SIGINT', () => {\n logger.info('SIGINT received in job proc');\n });\n\n // don't do anything on SIGTERM\n // Render uses SIGTERM in autoscale, this ensures the processes are properly drained if needed\n process.on('SIGTERM', () => {\n logger.info('SIGTERM received in job proc');\n });\n\n await once(process, 'message').then(([msg]: IPCMessage[]) => {\n msg = msg!;\n if (msg.case !== 'initializeRequest') {\n throw new Error('first message must be InitializeRequest');\n }\n initializeLogger(msg.value.loggerOptions);\n });\n const proc = new JobProcess();\n let logger = log().child({ pid: proc.pid });\n\n process.on('unhandledRejection', (reason) => {\n logger.error(reason);\n });\n\n logger.debug('initializing job runner');\n agent.prewarm(proc);\n logger.debug('job runner initialized');\n process.send({ case: 'initializeResponse' });\n\n let job: JobTask | undefined = undefined;\n const closeEvent = new EventEmitter();\n\n const orphanedTimeout = setTimeout(() => {\n logger.warn('job process orphaned, shutting down.');\n join.resolve();\n }, ORPHANED_TIMEOUT);\n\n const messageHandler = (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pingRequest': {\n orphanedTimeout.refresh();\n process.send!({\n case: 'pongResponse',\n value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },\n });\n break;\n }\n case 'startJobRequest': {\n if (job) {\n throw new Error('job task already running');\n }\n\n logger = logger.child({ jobID: msg.value.runningJob.job.id });\n\n job = startJob(proc, agent.entry, msg.value.runningJob, closeEvent, logger, join);\n logger.debug('job started');\n break;\n }\n case 'shutdownRequest': {\n if (!job) {\n join.resolve();\n }\n closeEvent.emit('close', 'shutdownRequest');\n clearTimeout(orphanedTimeout);\n process.off('message', messageHandler);\n }\n }\n };\n\n process.on('message', messageHandler);\n\n await join.await;\n\n logger.info('Job process shutdown');\n process.exit(0);\n }\n})();\n"],"mappings":";AAGA,sBAAgC;AAChC,yBAA2B;AAC3B,yBAAmC;AACnC,sBAA8B;AAE9B,uBAAoC;AACpC,iBAA+E;AAC/E,iBAAsC;AACtC,mBAAuB;AACvB,oBAA6C;AAI7C,MAAM,mBAAmB,KAAK;AAO9B,MAAM,iBAAiB;AAAA,EACrB,UAAU,IAAI,QAA6D,CAAC,YAAY;AACtF,SAAK,UAAU;AAAA,EACjB,CAAC;AAAA,EACD,QAAQ,KAA0D;AAChE;AAAA,EACF;AACF;AAEA,MAAM,UAAuC;AAAA,EAC3C,YAAgD,CAAC;AAAA,EAEjD,cAAc;AACZ,YAAQ,GAAG,WAAW,CAAC,QAAoB;AACzC,cAAQ,IAAI,MAAM;AAAA,QAChB,KAAK;AACH,gBAAM,MAAM,KAAK,UAAU,IAAI,MAAM,SAAS;AAC9C,iBAAO,KAAK,UAAU,IAAI,MAAM,SAAS;AACzC,cAAI,CAAC,KAAK;AACR,gCAAI,EAAE,MAAM,EAAE,MAAM,IAAI,MAAM,CAAC,EAAE,KAAK,wCAAwC;AAC9E;AAAA,UACF;AACA,cAAI,QAAQ,IAAI,KAAK;AACrB;AAAA,MACJ;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,YAAY,QAAgB,MAAiC;AACjE,UAAM,YAAY,mBAAmB;AACrC,YAAQ,KAAM,EAAE,MAAM,oBAAoB,OAAO,EAAE,WAAW,QAAQ,KAAK,EAAE,CAAC;AAC9E,SAAK,UAAU,SAAS,IAAI,IAAI,iBAAiB;AACjD,UAAM,OAAO,MAA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-Identifier: Apache-2.0\nimport { Room, RoomEvent } from '@livekit/rtc-node';\nimport { randomUUID } from 'node:crypto';\nimport { EventEmitter, once } from 'node:events';\nimport { pathToFileURL } from 'node:url';\nimport type { Logger } from 'pino';\nimport { type Agent, isAgent } from '../generator.js';\nimport { CurrentJobContext, JobContext, JobProcess, type RunningJobInfo } from '../job.js';\nimport { initializeLogger, log } from '../log.js';\nimport { Future } from '../utils.js';\nimport { defaultInitializeProcessFunc } from '../worker.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { IPCMessage } from './message.js';\n\nconst ORPHANED_TIMEOUT = 15 * 1000;\n\ntype JobTask = {\n ctx: JobContext;\n task: Promise<void>;\n};\n\nclass PendingInference {\n promise = new Promise<{ requestId: string; data: unknown; error?: Error }>((resolve) => {\n this.resolve = resolve; // this is how JavaScript lets you resolve promises externally\n });\n resolve(arg: { requestId: string; data: unknown; error?: Error }) {\n arg; // useless call to counteract TypeScript E6133\n }\n}\n\nclass InfClient implements InferenceExecutor {\n #requests: { [id: string]: PendingInference } = {};\n\n constructor() {\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'inferenceResponse':\n const fut = this.#requests[msg.value.requestId];\n delete this.#requests[msg.value.requestId];\n if (!fut) {\n log().child({ resp: msg.value }).warn('received unexpected inference response');\n return;\n }\n fut.resolve(msg.value);\n break;\n }\n });\n }\n\n async doInference(method: string, data: unknown): Promise<unknown> {\n const requestId = 'inference_job_' + randomUUID();\n process.send!({ case: 'inferenceRequest', value: { requestId, method, data } });\n this.#requests[requestId] = new PendingInference();\n const resp = await this.#requests[requestId]!.promise;\n if (resp.error) {\n throw new Error(`inference of ${method} failed: ${resp.error.message}`);\n }\n return resp.data;\n }\n}\n\nconst startJob = (\n proc: JobProcess,\n func: (ctx: JobContext) => Promise<void>,\n info: RunningJobInfo,\n closeEvent: EventEmitter,\n logger: Logger,\n joinFuture: Future,\n): JobTask => {\n let connect = false;\n let shutdown = false;\n\n const room = new Room();\n room.on(RoomEvent.Disconnected, () => {\n if (!shutdown) {\n closeEvent.emit('close', false);\n }\n });\n\n const onConnect = () => {\n connect = true;\n };\n const onShutdown = (reason: string) => {\n shutdown = true;\n closeEvent.emit('close', true, reason);\n };\n\n const ctx = new JobContext(proc, info, room, onConnect, onShutdown, new InfClient());\n new CurrentJobContext(ctx);\n\n const task = new Promise<void>(async () => {\n const unconnectedTimeout = setTimeout(() => {\n if (!(connect || shutdown)) {\n logger.warn(\n 'room not connect after job_entry was called after 10 seconds, ',\n 'did you forget to call ctx.connect()?',\n );\n }\n }, 10000);\n func(ctx).finally(() => clearTimeout(unconnectedTimeout));\n\n await once(closeEvent, 'close').then((close) => {\n logger.debug('shutting down');\n shutdown = true;\n process.send!({ case: 'exiting', value: { reason: close[1] } });\n });\n\n await room.disconnect();\n logger.debug('disconnected from room');\n\n const shutdownTasks = [];\n for (const callback of ctx.shutdownCallbacks) {\n shutdownTasks.push(callback());\n }\n await Promise.all(shutdownTasks).catch((error) =>\n logger.error('error while shutting down the job', error),\n );\n\n process.send!({ case: 'done' });\n logger.info('job completed.');\n joinFuture.resolve();\n });\n\n return { ctx, task };\n};\n\n(async () => {\n if (process.send) {\n const join = new Future();\n\n // process.argv:\n // [0] `node'\n // [1] import.meta.filename\n // [2] import.meta.filename of function containing entry file\n const moduleFile = process.argv[2];\n const agent: Agent = await import(pathToFileURL(moduleFile!).pathname).then((module) => {\n const agent = module.default;\n if (agent === undefined || !isAgent(agent)) {\n throw new Error(`Unable to load agent: Missing or invalid default export in ${moduleFile}`);\n }\n return agent;\n });\n if (!agent.prewarm) {\n agent.prewarm = defaultInitializeProcessFunc;\n }\n\n // don't do anything on C-c\n // this is handled in cli, triggering a termination of all child processes at once.\n process.on('SIGINT', () => {\n logger.info('SIGINT received in job proc');\n });\n\n // don't do anything on SIGTERM\n // Render uses SIGTERM in autoscale, this ensures the processes are properly drained if needed\n process.on('SIGTERM', () => {\n logger.info('SIGTERM received in job proc');\n });\n\n await once(process, 'message').then(([msg]: IPCMessage[]) => {\n msg = msg!;\n if (msg.case !== 'initializeRequest') {\n throw new Error('first message must be InitializeRequest');\n }\n initializeLogger(msg.value.loggerOptions);\n });\n const proc = new JobProcess();\n let logger = log().child({ pid: proc.pid });\n\n process.on('unhandledRejection', (reason) => {\n logger.error(reason);\n });\n\n logger.debug('initializing job runner');\n agent.prewarm(proc);\n logger.debug('job runner initialized');\n process.send({ case: 'initializeResponse' });\n\n let job: JobTask | undefined = undefined;\n const closeEvent = new EventEmitter();\n\n const orphanedTimeout = setTimeout(() => {\n logger.warn('job process orphaned, shutting down.');\n join.resolve();\n }, ORPHANED_TIMEOUT);\n\n const messageHandler = (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pingRequest': {\n orphanedTimeout.refresh();\n process.send!({\n case: 'pongResponse',\n value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },\n });\n break;\n }\n case 'startJobRequest': {\n if (job) {\n throw new Error('job task already running');\n }\n\n logger = logger.child({ jobID: msg.value.runningJob.job.id });\n\n job = startJob(proc, agent.entry, msg.value.runningJob, closeEvent, logger, join);\n logger.debug('job started');\n break;\n }\n case 'shutdownRequest': {\n if (!job) {\n join.resolve();\n }\n closeEvent.emit('close', 'shutdownRequest');\n clearTimeout(orphanedTimeout);\n process.off('message', messageHandler);\n }\n }\n };\n\n process.on('message', messageHandler);\n\n await join.await;\n\n logger.info('Job process shutdown');\n process.exit(0);\n }\n})();\n"],"mappings":";AAGA,sBAAgC;AAChC,yBAA2B;AAC3B,yBAAmC;AACnC,sBAA8B;AAE9B,uBAAoC;AACpC,iBAA+E;AAC/E,iBAAsC;AACtC,mBAAuB;AACvB,oBAA6C;AAI7C,MAAM,mBAAmB,KAAK;AAO9B,MAAM,iBAAiB;AAAA,EACrB,UAAU,IAAI,QAA6D,CAAC,YAAY;AACtF,SAAK,UAAU;AAAA,EACjB,CAAC;AAAA,EACD,QAAQ,KAA0D;AAChE;AAAA,EACF;AACF;AAEA,MAAM,UAAuC;AAAA,E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O,MAAM,EAAE,OAAO,IAAI,MAAM,WAAW,IAAI,GAAG,CAAC;AAE5D,gBAAM,SAAS,MAAM,MAAM,OAAO,IAAI,MAAM,YAAY,YAAY,QAAQ,IAAI;AAChF,iBAAO,MAAM,aAAa;AAC1B;AAAA,QACF;AAAA,QACA,KAAK,mBAAmB;AACtB,cAAI,CAAC,KAAK;AACR,iBAAK,QAAQ;AAAA,UACf;AACA,qBAAW,KAAK,SAAS,iBAAiB;AAC1C,uBAAa,eAAe;AAC5B,kBAAQ,IAAI,WAAW,cAAc;AAAA,QACvC;AAAA,MACF;AAAA,IACF;AAEA,YAAQ,GAAG,WAAW,cAAc;AAEpC,UAAM,KAAK;AAEX,WAAO,KAAK,sBAAsB;AAClC,YAAQ,KAAK,CAAC;AAAA,EAChB;AACF,GAAG;","names":["module","agent"]}
|
|
@@ -34,7 +34,7 @@ class InfClient {
|
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
36
|
async doInference(method, data) {
|
|
37
|
-
const requestId = "inference_job_" + randomUUID;
|
|
37
|
+
const requestId = "inference_job_" + randomUUID();
|
|
38
38
|
process.send({ case: "inferenceRequest", value: { requestId, method, data } });
|
|
39
39
|
this.#requests[requestId] = new PendingInference();
|
|
40
40
|
const resp = await this.#requests[requestId].promise;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ipc/job_proc_lazy_main.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Room, RoomEvent } from '@livekit/rtc-node';\nimport { randomUUID } from 'node:crypto';\nimport { EventEmitter, once } from 'node:events';\nimport { pathToFileURL } from 'node:url';\nimport type { Logger } from 'pino';\nimport { type Agent, isAgent } from '../generator.js';\nimport { CurrentJobContext, JobContext, JobProcess, type RunningJobInfo } from '../job.js';\nimport { initializeLogger, log } from '../log.js';\nimport { Future } from '../utils.js';\nimport { defaultInitializeProcessFunc } from '../worker.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { IPCMessage } from './message.js';\n\nconst ORPHANED_TIMEOUT = 15 * 1000;\n\ntype JobTask = {\n ctx: JobContext;\n task: Promise<void>;\n};\n\nclass PendingInference {\n promise = new Promise<{ requestId: string; data: unknown; error?: Error }>((resolve) => {\n this.resolve = resolve; // this is how JavaScript lets you resolve promises externally\n });\n resolve(arg: { requestId: string; data: unknown; error?: Error }) {\n arg; // useless call to counteract TypeScript E6133\n }\n}\n\nclass InfClient implements InferenceExecutor {\n #requests: { [id: string]: PendingInference } = {};\n\n constructor() {\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'inferenceResponse':\n const fut = this.#requests[msg.value.requestId];\n delete this.#requests[msg.value.requestId];\n if (!fut) {\n log().child({ resp: msg.value }).warn('received unexpected inference response');\n return;\n }\n fut.resolve(msg.value);\n break;\n }\n });\n }\n\n async doInference(method: string, data: unknown): Promise<unknown> {\n const requestId = 'inference_job_' + randomUUID;\n process.send!({ case: 'inferenceRequest', value: { requestId, method, data } });\n this.#requests[requestId] = new PendingInference();\n const resp = await this.#requests[requestId]!.promise;\n if (resp.error) {\n throw new Error(`inference of ${method} failed: ${resp.error.message}`);\n }\n return resp.data;\n }\n}\n\nconst startJob = (\n proc: JobProcess,\n func: (ctx: JobContext) => Promise<void>,\n info: RunningJobInfo,\n closeEvent: EventEmitter,\n logger: Logger,\n joinFuture: Future,\n): JobTask => {\n let connect = false;\n let shutdown = false;\n\n const room = new Room();\n room.on(RoomEvent.Disconnected, () => {\n if (!shutdown) {\n closeEvent.emit('close', false);\n }\n });\n\n const onConnect = () => {\n connect = true;\n };\n const onShutdown = (reason: string) => {\n shutdown = true;\n closeEvent.emit('close', true, reason);\n };\n\n const ctx = new JobContext(proc, info, room, onConnect, onShutdown, new InfClient());\n new CurrentJobContext(ctx);\n\n const task = new Promise<void>(async () => {\n const unconnectedTimeout = setTimeout(() => {\n if (!(connect || shutdown)) {\n logger.warn(\n 'room not connect after job_entry was called after 10 seconds, ',\n 'did you forget to call ctx.connect()?',\n );\n }\n }, 10000);\n func(ctx).finally(() => clearTimeout(unconnectedTimeout));\n\n await once(closeEvent, 'close').then((close) => {\n logger.debug('shutting down');\n shutdown = true;\n process.send!({ case: 'exiting', value: { reason: close[1] } });\n });\n\n await room.disconnect();\n logger.debug('disconnected from room');\n\n const shutdownTasks = [];\n for (const callback of ctx.shutdownCallbacks) {\n shutdownTasks.push(callback());\n }\n await Promise.all(shutdownTasks).catch((error) =>\n logger.error('error while shutting down the job', error),\n );\n\n process.send!({ case: 'done' });\n logger.info('job completed.');\n joinFuture.resolve();\n });\n\n return { ctx, task };\n};\n\n(async () => {\n if (process.send) {\n const join = new Future();\n\n // process.argv:\n // [0] `node'\n // [1] import.meta.filename\n // [2] import.meta.filename of function containing entry file\n const moduleFile = process.argv[2];\n const agent: Agent = await import(pathToFileURL(moduleFile!).pathname).then((module) => {\n const agent = module.default;\n if (agent === undefined || !isAgent(agent)) {\n throw new Error(`Unable to load agent: Missing or invalid default export in ${moduleFile}`);\n }\n return agent;\n });\n if (!agent.prewarm) {\n agent.prewarm = defaultInitializeProcessFunc;\n }\n\n // don't do anything on C-c\n // this is handled in cli, triggering a termination of all child processes at once.\n process.on('SIGINT', () => {\n logger.info('SIGINT received in job proc');\n });\n\n // don't do anything on SIGTERM\n // Render uses SIGTERM in autoscale, this ensures the processes are properly drained if needed\n process.on('SIGTERM', () => {\n logger.info('SIGTERM received in job proc');\n });\n\n await once(process, 'message').then(([msg]: IPCMessage[]) => {\n msg = msg!;\n if (msg.case !== 'initializeRequest') {\n throw new Error('first message must be InitializeRequest');\n }\n initializeLogger(msg.value.loggerOptions);\n });\n const proc = new JobProcess();\n let logger = log().child({ pid: proc.pid });\n\n process.on('unhandledRejection', (reason) => {\n logger.error(reason);\n });\n\n logger.debug('initializing job runner');\n agent.prewarm(proc);\n logger.debug('job runner initialized');\n process.send({ case: 'initializeResponse' });\n\n let job: JobTask | undefined = undefined;\n const closeEvent = new EventEmitter();\n\n const orphanedTimeout = setTimeout(() => {\n logger.warn('job process orphaned, shutting down.');\n join.resolve();\n }, ORPHANED_TIMEOUT);\n\n const messageHandler = (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pingRequest': {\n orphanedTimeout.refresh();\n process.send!({\n case: 'pongResponse',\n value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },\n });\n break;\n }\n case 'startJobRequest': {\n if (job) {\n throw new Error('job task already running');\n }\n\n logger = logger.child({ jobID: msg.value.runningJob.job.id });\n\n job = startJob(proc, agent.entry, msg.value.runningJob, closeEvent, logger, join);\n logger.debug('job started');\n break;\n }\n case 'shutdownRequest': {\n if (!job) {\n join.resolve();\n }\n closeEvent.emit('close', 'shutdownRequest');\n clearTimeout(orphanedTimeout);\n process.off('message', messageHandler);\n }\n }\n };\n\n process.on('message', messageHandler);\n\n await join.await;\n\n logger.info('Job process shutdown');\n process.exit(0);\n }\n})();\n"],"mappings":"AAGA,SAAS,MAAM,iBAAiB;AAChC,SAAS,kBAAkB;AAC3B,SAAS,cAAc,YAAY;AACnC,SAAS,qBAAqB;AAE9B,SAAqB,eAAe;AACpC,SAAS,mBAAmB,YAAY,kBAAuC;AAC/E,SAAS,kBAAkB,WAAW;AACtC,SAAS,cAAc;AACvB,SAAS,oCAAoC;AAI7C,MAAM,mBAAmB,KAAK;AAO9B,MAAM,iBAAiB;AAAA,EACrB,UAAU,IAAI,QAA6D,CAAC,YAAY;AACtF,SAAK,UAAU;AAAA,EACjB,CAAC;AAAA,EACD,QAAQ,KAA0D;AAChE;AAAA,EACF;AACF;AAEA,MAAM,UAAuC;AAAA,EAC3C,YAAgD,CAAC;AAAA,EAEjD,cAAc;AACZ,YAAQ,GAAG,WAAW,CAAC,QAAoB;AACzC,cAAQ,IAAI,MAAM;AAAA,QAChB,KAAK;AACH,gBAAM,MAAM,KAAK,UAAU,IAAI,MAAM,SAAS;AAC9C,iBAAO,KAAK,UAAU,IAAI,MAAM,SAAS;AACzC,cAAI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AAAA,UACf;AACA,qBAAW,KAAK,SAAS,iBAAiB;AAC1C,uBAAa,eAAe;AAC5B,kBAAQ,IAAI,WAAW,cAAc;AAAA,QACvC;AAAA,MACF;AAAA,IACF;AAEA,YAAQ,GAAG,WAAW,cAAc;AAEpC,UAAM,KAAK;AAEX,WAAO,KAAK,sBAAsB;AAClC,YAAQ,KAAK,CAAC;AAAA,EAChB;AACF,GAAG;","names":["agent"]}
|
|
1
|
+
{"version":3,"sources":["../../src/ipc/job_proc_lazy_main.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Room, RoomEvent } from '@livekit/rtc-node';\nimport { randomUUID } from 'node:crypto';\nimport { EventEmitter, once } from 'node:events';\nimport { pathToFileURL } from 'node:url';\nimport type { Logger } from 'pino';\nimport { type Agent, isAgent } from '../generator.js';\nimport { CurrentJobContext, JobContext, JobProcess, type RunningJobInfo } from '../job.js';\nimport { initializeLogger, log } from '../log.js';\nimport { Future } from '../utils.js';\nimport { defaultInitializeProcessFunc } from '../worker.js';\nimport type { InferenceExecutor } from './inference_executor.js';\nimport type { IPCMessage } from './message.js';\n\nconst ORPHANED_TIMEOUT = 15 * 1000;\n\ntype JobTask = {\n ctx: JobContext;\n task: Promise<void>;\n};\n\nclass PendingInference {\n promise = new Promise<{ requestId: string; data: unknown; error?: Error }>((resolve) => {\n this.resolve = resolve; // this is how JavaScript lets you resolve promises externally\n });\n resolve(arg: { requestId: string; data: unknown; error?: Error }) {\n arg; // useless call to counteract TypeScript E6133\n }\n}\n\nclass InfClient implements InferenceExecutor {\n #requests: { [id: string]: PendingInference } = {};\n\n constructor() {\n process.on('message', (msg: IPCMessage) => {\n switch (msg.case) {\n case 'inferenceResponse':\n const fut = this.#requests[msg.value.requestId];\n delete this.#requests[msg.value.requestId];\n if (!fut) {\n log().child({ resp: msg.value }).warn('received unexpected inference response');\n return;\n }\n fut.resolve(msg.value);\n break;\n }\n });\n }\n\n async doInference(method: string, data: unknown): Promise<unknown> {\n const requestId = 'inference_job_' + randomUUID();\n process.send!({ case: 'inferenceRequest', value: { requestId, method, data } });\n this.#requests[requestId] = new PendingInference();\n const resp = await this.#requests[requestId]!.promise;\n if (resp.error) {\n throw new Error(`inference of ${method} failed: ${resp.error.message}`);\n }\n return resp.data;\n }\n}\n\nconst startJob = (\n proc: JobProcess,\n func: (ctx: JobContext) => Promise<void>,\n info: RunningJobInfo,\n closeEvent: EventEmitter,\n logger: Logger,\n joinFuture: Future,\n): JobTask => {\n let connect = false;\n let shutdown = false;\n\n const room = new Room();\n room.on(RoomEvent.Disconnected, () => {\n if (!shutdown) {\n closeEvent.emit('close', false);\n }\n });\n\n const onConnect = () => {\n connect = true;\n };\n const onShutdown = (reason: string) => {\n shutdown = true;\n closeEvent.emit('close', true, reason);\n };\n\n const ctx = new JobContext(proc, info, room, onConnect, onShutdown, new InfClient());\n new CurrentJobContext(ctx);\n\n const task = new Promise<void>(async () => {\n const unconnectedTimeout = setTimeout(() => {\n if (!(connect || shutdown)) {\n logger.warn(\n 'room not connect after job_entry was called after 10 seconds, ',\n 'did you forget to call ctx.connect()?',\n );\n }\n }, 10000);\n func(ctx).finally(() => clearTimeout(unconnectedTimeout));\n\n await once(closeEvent, 'close').then((close) => {\n logger.debug('shutting down');\n shutdown = true;\n process.send!({ case: 'exiting', value: { reason: close[1] } });\n });\n\n await room.disconnect();\n logger.debug('disconnected from room');\n\n const shutdownTasks = [];\n for (const callback of ctx.shutdownCallbacks) {\n shutdownTasks.push(callback());\n }\n await Promise.all(shutdownTasks).catch((error) =>\n logger.error('error while shutting down the job', error),\n );\n\n process.send!({ case: 'done' });\n logger.info('job completed.');\n joinFuture.resolve();\n });\n\n return { ctx, task };\n};\n\n(async () => {\n if (process.send) {\n const join = new Future();\n\n // process.argv:\n // [0] `node'\n // [1] import.meta.filename\n // [2] import.meta.filename of function containing entry file\n const moduleFile = process.argv[2];\n const agent: Agent = await import(pathToFileURL(moduleFile!).pathname).then((module) => {\n const agent = module.default;\n if (agent === undefined || !isAgent(agent)) {\n throw new Error(`Unable to load agent: Missing or invalid default export in ${moduleFile}`);\n }\n return agent;\n });\n if (!agent.prewarm) {\n agent.prewarm = defaultInitializeProcessFunc;\n }\n\n // don't do anything on C-c\n // this is handled in cli, triggering a termination of all child processes at once.\n process.on('SIGINT', () => {\n logger.info('SIGINT received in job proc');\n });\n\n // don't do anything on SIGTERM\n // Render uses SIGTERM in autoscale, this ensures the processes are properly drained if needed\n process.on('SIGTERM', () => {\n logger.info('SIGTERM received in job proc');\n });\n\n await once(process, 'message').then(([msg]: IPCMessage[]) => {\n msg = msg!;\n if (msg.case !== 'initializeRequest') {\n throw new Error('first message must be InitializeRequest');\n }\n initializeLogger(msg.value.loggerOptions);\n });\n const proc = new JobProcess();\n let logger = log().child({ pid: proc.pid });\n\n process.on('unhandledRejection', (reason) => {\n logger.error(reason);\n });\n\n logger.debug('initializing job runner');\n agent.prewarm(proc);\n logger.debug('job runner initialized');\n process.send({ case: 'initializeResponse' });\n\n let job: JobTask | undefined = undefined;\n const closeEvent = new EventEmitter();\n\n const orphanedTimeout = setTimeout(() => {\n logger.warn('job process orphaned, shutting down.');\n join.resolve();\n }, ORPHANED_TIMEOUT);\n\n const messageHandler = (msg: IPCMessage) => {\n switch (msg.case) {\n case 'pingRequest': {\n orphanedTimeout.refresh();\n process.send!({\n case: 'pongResponse',\n value: { lastTimestamp: msg.value.timestamp, timestamp: Date.now() },\n });\n break;\n }\n case 'startJobRequest': {\n if (job) {\n throw new Error('job task already running');\n }\n\n logger = logger.child({ jobID: msg.value.runningJob.job.id });\n\n job = startJob(proc, agent.entry, msg.value.runningJob, closeEvent, logger, join);\n logger.debug('job started');\n break;\n }\n case 'shutdownRequest': {\n if (!job) {\n join.resolve();\n }\n closeEvent.emit('close', 'shutdownRequest');\n clearTimeout(orphanedTimeout);\n process.off('message', messageHandler);\n }\n }\n };\n\n process.on('message', messageHandler);\n\n await join.await;\n\n log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