Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

feat(node-services): download voice models when the voice role starts

shoudi zhengc02d76a8cac2
diff --git a/apps/node-services/AGENTS.md b/apps/node-services/AGENTS.md
index ebe71c2a..99e0bd33 100644
--- a/apps/node-services/AGENTS.md
+++ b/apps/node-services/AGENTS.md
@@ -52,7 +52,8 @@
- Packages that hold model data (OCR language data, the Voice VAD binding)
resolve through `src/model-packages.ts`: from node_modules in development
and images, downloaded on first use where the offline runtime lists them in
- `models.json`.
+ `models.json`. The `voice` role starts its download when the role starts,
+ before any provider key exists, so the first worker start does not wait for it.
- Roles log through the `JsonLogger` the host passes to `start*Service`
(`createJsonLogger` from `@linger/service-kit/node`); the `document` role
creates its own inside its worker thread, and the `agent` role keeps its own
diff --git a/apps/node-services/src/voice/service.ts b/apps/node-services/src/voice/service.ts
index 4e481696..17aed044 100644
--- a/apps/node-services/src/voice/service.ts
+++ b/apps/node-services/src/voice/service.ts
@@ -117,8 +117,14 @@ export async function startVoiceService(options: {
// The VAD model's native package, when the runtime ships it separately: the
// workers wait for its first download and find it on NODE_PATH.
const models = modelPackageNames("voice");
- const install =
+ const installModels =
options.installModels ?? (() => installModelPackages(models, env));
+ // Worker starts share one download, so concurrent starts never fetch twice.
+ let installing: Promise<void> | undefined;
+ const install = () =>
+ (installing ??= installModels().finally(() => {
+ installing = undefined;
+ }));
const modelPath = models.length
? {
NODE_PATH: [modelPackagesRoot(env), env.NODE_PATH]
@@ -297,6 +303,12 @@ export async function startVoiceService(options: {
});
return running;
};
+ // Download the models before any key exists, so the first worker start after
+ // a key is saved fits TeamHub's media start timeout. A worker start retries.
+ if (!modelPackagesInstalled(models, env))
+ install().catch((error: unknown) => {
+ options.logger.log("warn", "voice.model_install_failed", { error });
+ });
// TeamHub may still be starting; a failed first reconcile retries on the poll.
void reconcile();
const poll = setInterval(() => void reconcile(), VOICE_RECONCILE_POLL_MS);
diff --git a/apps/node-services/tests/model-packages.test.ts b/apps/node-services/tests/model-packages.test.ts
index 3baea84b..03b0f51b 100644
--- a/apps/node-services/tests/model-packages.test.ts
+++ b/apps/node-services/tests/model-packages.test.ts
@@ -265,6 +265,81 @@ describe("features that need model packages", () => {
await stopping;
});
+ it("download the voice packages at startup before any key exists", async () => {
+ const { startVoiceService } = await import("../src/voice/service.js");
+ const installModels = vi.fn(async () => undefined);
+ const spawnWorker = vi.fn();
+ const service = await startVoiceService({
+ logger: { log: vi.fn() } as unknown as JsonLogger,
+ http: { mount: () => () => {} } as unknown as HttpHost,
+ environment: {
+ ...environment,
+ LINGER_SECRET_SEED: "model-packages-test-seed-0123456789abcdef",
+ },
+ fetch: async () => Response.json({}),
+ spawnWorker,
+ installModels,
+ });
+ await service.reconcile();
+ expect(installModels).toHaveBeenCalledOnce();
+ expect(spawnWorker).not.toHaveBeenCalled();
+ await service.stop();
+ });
+
+ it("log a failed background download and retry it when a key arrives", async () => {
+ const { startVoiceService } = await import("../src/voice/service.js");
+ const models = await load();
+ const installModels = vi
+ .fn<() => Promise<void>>()
+ .mockRejectedValueOnce(new Error("offline"))
+ .mockImplementation(async () => {
+ const directory = path.join(
+ models.modelPackagesRoot(environment),
+ "@livekit/local-inference-linux-x64-gnu",
+ );
+ await mkdir(directory, { recursive: true });
+ writeFileSync(
+ path.join(directory, ".simplex-model-package.json"),
+ JSON.stringify({ integrity: integrity(vad) }),
+ );
+ });
+ const log = vi.fn();
+ let keys: Record<string, string> = {};
+ const spawnWorker = vi.fn(
+ () =>
+ Object.assign(new EventEmitter(), {
+ kill: vi.fn(() => true),
+ }) as unknown as ChildProcess,
+ );
+ const service = await startVoiceService({
+ logger: { log } as unknown as JsonLogger,
+ http: { mount: () => () => {} } as unknown as HttpHost,
+ environment: {
+ ...environment,
+ LINGER_SECRET_SEED: "model-packages-test-seed-0123456789abcdef",
+ },
+ fetch: async () => Response.json(keys),
+ spawnWorker,
+ installModels,
+ });
+ await vi.waitFor(() =>
+ expect(log).toHaveBeenCalledWith(
+ "warn",
+ "voice.model_install_failed",
+ expect.anything(),
+ ),
+ );
+ keys = { xai_api_key: "test" };
+ await service.reconcile();
+ await vi.waitFor(() =>
+ expect(spawnWorker).toHaveBeenCalledWith("live", expect.anything()),
+ );
+ expect(installModels).toHaveBeenCalledTimes(2);
+ const stopping = service.stop();
+ (spawnWorker.mock.results[0]!.value as EventEmitter).emit("exit", 0, null);
+ await stopping;
+ });
+
it("convert documents when the OCR packages cannot install", async () => {
const { WorkerDocumentNormalizer } =
await import("../src/document-normalizer/document-normalizer.js");