1
>
/*---------------------------------------------------------------------------------------------
foundryLocalRuntime.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import * as fs from 'fs';
7
>
import * as os from 'os';
8
>
import { dirname, join } from '../../../base/common/path.js';
9
>
import { format2 } from '../../../base/common/strings.js';
10
>
import { CancellationToken } from '../../../base/common/cancellation.js';
11
>
import { CancellationError } from '../../../base/common/errors.js';
12
>
13
>
/**
14
>
* On-demand provisioning of the Foundry Local native runtime used by on-device
15
>
* dictation.
16
>
*
17
>
* `foundry-local-sdk` ships a prebuilt N-API addon (`foundry_local_napi.node`)
18
>
* and native core libraries (Foundry Local Core + ONNX Runtime + ONNX Runtime
19
>
* GenAI). The addon requires a newer glibc than VS Code's minimum supported
20
>
* Linux distros, so we deliberately do NOT bundle any of this native payload
21
>
* with the product (see `build/gulpfile.vscode.ts`). Instead we republish a
22
>
* per-target tarball of the addon + core libraries to VS Code's CDN at build
23
>
* time (see `build/dictation-runtime/`) and download it here, at runtime, only
24
>
* on supported platforms, into a per-user writable cache — keeping the shipped
25
>
* package's glibc floor intact and avoiding any runtime dependency on the npm
26
>
* registry or NuGet.
27
>
*
28
>
* The SDK loader (`dist/detail/coreInterop.js`) is patched during
29
>
* `postinstall` to honor `VSCODE_FOUNDRY_LOCAL_NATIVE_DIR`, pointing it at the
30
>
* cache directory this module populates. The tarball's internal layout mirrors
31
>
* the SDK's own package layout so the patched resolution is a trivial path join:
32
>
*
33
>
* <cacheRoot>/<version>/prebuilds/<target>/foundry_local_napi.node
34
>
* <cacheRoot>/<version>/foundry-local-core/<target>/<core libraries>
35
>
*
36
>
* NOTE: the single CDN download leg honors the standard proxy environment
37
>
* variables (`HTTPS_PROXY`/`HTTP_PROXY`/`ALL_PROXY`, with `NO_PROXY`). VS Code's
38
>
* `http.proxy`/`http.noProxy` settings are applied as these same environment
39
>
* variables before provisioning (see `LocalTranscriptionService.start`), so a
40
>
* proxy configured only in VS Code is honored here and by the native model
41
>
* download too; `http.proxyAuthorization` (Basic) is folded into the proxy URL
42
>
* and `http.proxyStrictSSL === false` disables TLS verification for this leg.
43
>
* TLS-intercepting proxies otherwise rely on the CA being in the OS trust store.
44
>
*/
45
>
46
>
/**
47
>
* Platforms (`<process.platform>-<process.arch>`) for which Foundry Local ships
48
>
* a native addon + core libraries. Mirrors the SDK installer's RID map.
49
>
*/
50
>
export const FOUNDRY_LOCAL_SUPPORTED_PLATFORMS: ReadonlySet<string> = new Set([
51
>
'darwin-arm64',
52
>
'linux-x64',
53
>
'linux-arm64',
54
>
'win32-x64',
55
>
'win32-arm64',
56
>
]);
57
>
58
>
/** The current host platform key, or `undefined` if Foundry Local can't run here. */
59
>
export function foundryLocalPlatformKey(): string | undefined {
60
const key = `${process.platform}-${process.arch}`;
61
return FOUNDRY_LOCAL_SUPPORTED_PLATFORMS.has(key) ? key : undefined;
62
}
64
>
/** Whether on-device dictation's native runtime can run on this host. */
65
>
export function isFoundryLocalRuntimeSupported(): boolean {
66
return foundryLocalPlatformKey() !== undefined;
67
}
69
>
/** Progress callback invoked while the native runtime is being fetched. */
70
>
export type FoundryLocalRuntimeProgress = (message: string) => void;
71
>
72
>
/** Where the native runtime tarball is published (from `product.dictationRuntime`). */
73
>
export interface IFoundryLocalRuntimeDownload {
74
>
/** CDN URL template with a `{target}` placeholder for the host platform key. */
75
>
readonly urlTemplate: string;
76
>
/** The published runtime version (the pinned `foundry-local-sdk` version). */
77
>
readonly version: string;
78
>
}
79
>
80
>
/** De-dupes concurrent provisioning requests targeting the same cache dir. */
81
>
const inFlight = new Map<string, Promise<string>>();
82
>
83
>
/** Abort a download after this long without any connection/response progress. */
84
>
const DOWNLOAD_INACTIVITY_TIMEOUT_MS = 60_000;
85
>
86
>
/**
87
>
* Ensure the Foundry Local native runtime (addon + core libraries) is present
88
>
* in `<cacheRoot>`, downloading the per-target CDN tarball if necessary. Returns
89
>
* the versioned override directory to set as `VSCODE_FOUNDRY_LOCAL_NATIVE_DIR`
90
>
* before loading the SDK.
91
>
*
92
>
* Idempotent: once a version is fully provisioned a per-platform `.complete`
93
>
* marker is written and subsequent calls return immediately (after verifying the
94
>
* payload) without touching the network.
95
>
*/
96
export async function ensureFoundryLocalRuntime(cacheRoot: string, download: IFoundryLocalRuntimeDownload, token: CancellationToken, onProgress?: FoundryLocalRuntimeProgress): Promise<string> {
97
const platformKey = foundryLocalPlatformKey();