-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptionalComponentPaths.js
More file actions
192 lines (170 loc) · 6.12 KB
/
Copy pathoptionalComponentPaths.js
File metadata and controls
192 lines (170 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict';
const fs = require('fs');
const path = require('path');
const COMPONENT_IDS = {
PLAYWRIGHT: 'playwright-chromium',
SD_CUDA: 'sd-cpp-cuda',
SD_CPU: 'sd-cpp-cpu',
WHISPER: 'whisper',
};
function getComponentsRoot(userDataPath) {
return path.join(userDataPath, 'components');
}
function getManifestPath(userDataPath) {
return path.join(getComponentsRoot(userDataPath), 'manifest.json');
}
function getPlaywrightBrowsersDir(userDataPath) {
return path.join(getComponentsRoot(userDataPath), 'playwright-browsers');
}
function getSdCppDir(userDataPath, variant) {
const sub = variant === 'cuda' ? 'cuda' : 'cpu';
return path.join(getComponentsRoot(userDataPath), 'sd-cpp', sub);
}
function getWhisperDir(userDataPath) {
return path.join(getComponentsRoot(userDataPath), 'whisper');
}
function _dirHasChromium(browsersDir) {
if (!browsersDir || !fs.existsSync(browsersDir)) return false;
try {
const entries = fs.readdirSync(browsersDir, { withFileTypes: true });
return entries.some((e) => e.isDirectory() && /chromium/i.test(e.name));
} catch {
return false;
}
}
function _dirHasSdExe(dir) {
if (!dir || !fs.existsSync(dir)) return false;
return fs.existsSync(path.join(dir, 'sd.exe')) || fs.existsSync(path.join(dir, 'sd'));
}
function _bundledPlaywrightDir(resourcesPath) {
if (!resourcesPath) return null;
const p = path.join(resourcesPath, 'playwright-browsers');
return fs.existsSync(p) ? p : null;
}
function _bundledSdDir(resourcesPath, sub) {
if (!resourcesPath) return null;
const candidates = sub === 'cuda'
? [path.join(resourcesPath, 'sd-cpp'), path.join(resourcesPath, 'sd-cpp', 'win-x64-cuda')]
: [path.join(resourcesPath, 'sd-cpp-cpu'), path.join(resourcesPath, 'sd-cpp'), path.join(resourcesPath, 'sd-cpp', 'win-x64-cpu')];
for (const p of candidates) {
if (_dirHasSdExe(p)) return p;
}
return null;
}
function _bundledWhisperDir(resourcesPath) {
if (!resourcesPath) return null;
const p = path.join(resourcesPath, 'whisper');
return fs.existsSync(p) ? p : null;
}
/**
* Resolve Playwright browsers directory — cache first, then legacy bundled path.
*/
function resolvePlaywrightBrowsersPath(userDataPath, resourcesPath) {
const cached = getPlaywrightBrowsersDir(userDataPath);
if (_dirHasChromium(cached)) return cached;
const bundled = _bundledPlaywrightDir(resourcesPath);
if (bundled && _dirHasChromium(bundled)) return bundled;
return cached;
}
function isPlaywrightBrowsersReady(userDataPath, resourcesPath) {
const dir = resolvePlaywrightBrowsersPath(userDataPath, resourcesPath);
return _dirHasChromium(dir);
}
/**
* Resolve sd-cpp binary directory — userData cache first, then bundled extraResources.
*/
function resolveSdCppDir(userDataPath, resourcesPath, variant = 'cuda') {
const cached = getSdCppDir(userDataPath, variant);
if (_dirHasSdExe(cached)) return cached;
const bundled = _bundledSdDir(resourcesPath, variant);
if (bundled) return bundled;
return cached;
}
function isSdCppReady(userDataPath, resourcesPath, variant) {
return _dirHasSdExe(resolveSdCppDir(userDataPath, resourcesPath, variant));
}
function resolveWhisperModelPath(userDataPath, resourcesPath) {
const cached = path.join(getWhisperDir(userDataPath), 'ggml-base.en.bin');
if (fs.existsSync(cached)) return cached;
const bundledRoot = _bundledWhisperDir(resourcesPath);
if (bundledRoot) {
const bundled = path.join(bundledRoot, 'ggml-base.en.bin');
if (fs.existsSync(bundled)) return bundled;
}
const legacy = path.join(userDataPath, 'whisper-models', 'ggml-base.en.bin');
if (fs.existsSync(legacy)) return legacy;
return cached;
}
/** Windows whisper-cli needs ggml/whisper DLLs beside the exe (STATUS_DLL_NOT_FOUND otherwise). */
function hasWhisperRuntime(cliPath) {
if (!cliPath || !fs.existsSync(cliPath)) return false;
if (process.platform !== 'win32') return true;
const dir = path.dirname(cliPath);
try {
return fs.readdirSync(dir).some((name) => {
const lower = name.toLowerCase();
return lower.endsWith('.dll') && (lower.startsWith('ggml') || lower === 'whisper.dll');
});
} catch {
return false;
}
}
function resolveWhisperCliPath(userDataPath, resourcesPath) {
const isWin = process.platform === 'win32';
const names = isWin ? ['whisper-cli.exe', 'whisper.exe', 'main.exe'] : ['whisper-cli', 'whisper', 'main'];
const candidates = [];
const cachedRoot = getWhisperDir(userDataPath);
for (const name of names) {
candidates.push(path.join(cachedRoot, name));
}
const bundledRoot = _bundledWhisperDir(resourcesPath);
if (bundledRoot) {
for (const name of names) {
candidates.push(path.join(bundledRoot, name));
}
const platDir = path.join(bundledRoot, isWin ? 'win32' : process.platform);
for (const name of names) {
candidates.push(path.join(platDir, name));
}
}
for (const p of candidates) {
if (fs.existsSync(p) && hasWhisperRuntime(p)) return p;
}
return null;
}
function isWhisperReady(userDataPath, resourcesPath) {
return !!resolveWhisperCliPath(userDataPath, resourcesPath)
&& fs.existsSync(resolveWhisperModelPath(userDataPath, resourcesPath));
}
function catalogForVariant(installVariant) {
const items = [
{ id: COMPONENT_IDS.PLAYWRIGHT, label: 'Browser tools', bytesEstimate: 350_000_000 },
{ id: COMPONENT_IDS.WHISPER, label: 'Voice transcription', bytesEstimate: 155_000_000 },
];
if (installVariant === 'cuda') {
items.push({ id: COMPONENT_IDS.SD_CUDA, label: 'Image generation (CUDA)', bytesEstimate: 250_000_000 });
items.push({ id: COMPONENT_IDS.SD_CPU, label: 'Image generation (Vulkan)', bytesEstimate: 180_000_000 });
} else {
items.push({ id: COMPONENT_IDS.SD_CPU, label: 'Image generation', bytesEstimate: 180_000_000 });
}
return items;
}
module.exports = {
COMPONENT_IDS,
getComponentsRoot,
getManifestPath,
getPlaywrightBrowsersDir,
getSdCppDir,
getWhisperDir,
resolvePlaywrightBrowsersPath,
isPlaywrightBrowsersReady,
resolveSdCppDir,
isSdCppReady,
resolveWhisperModelPath,
resolveWhisperCliPath,
hasWhisperRuntime,
isWhisperReady,
catalogForVariant,
_dirHasChromium,
_dirHasSdExe,
};