-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateVariant.js
More file actions
218 lines (195 loc) · 6 KB
/
Copy pathupdateVariant.js
File metadata and controls
218 lines (195 loc) · 6 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
'use strict';
const fs = require('fs');
const path = require('path');
const GITHUB_OWNER = 'FileShot';
const GITHUB_REPO = 'guide-3.0';
const FULL_IDENTITY = {
edition: 'full',
appId: 'com.guide-ide.desktop',
productName: 'guIDE',
userDataName: 'guide-ide',
appUserModelId: 'com.guide-ide.desktop',
tagline: null,
};
const LITE_IDENTITY = {
edition: 'lite',
appId: 'com.guide-ide.lite',
productName: 'guIDE Lite',
userDataName: 'guide-ide-lite',
appUserModelId: 'com.guide-ide.lite',
tagline: 'guIDE Minus — not Plus',
};
function _resourcesDir() {
return process.resourcesPath || null;
}
function _readInstallManifest() {
const resources = _resourcesDir();
if (!resources) return null;
const manifestPath = path.join(resources, 'install-variant.json');
try {
return JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
} catch {
return null;
}
}
/** Channel embedded by electron-builder into app-update.yml (cuda builds). */
function _readAppUpdateYmlChannel() {
const resources = _resourcesDir();
if (!resources) return null;
const ymlPath = path.join(resources, 'app-update.yml');
try {
const text = fs.readFileSync(ymlPath, 'utf8');
const match = text.match(/^channel:\s*(\S+)/m);
return match ? match[1] : null;
} catch {
return null;
}
}
function _llamaBackendRoots() {
const resources = _resourcesDir();
if (!resources) return [];
return [
path.join(resources, 'app.asar.unpacked', 'node_modules', '@node-llama-cpp'),
path.join(resources, 'app', 'node_modules', '@node-llama-cpp'),
];
}
function _hasCudaBackendBinaries() {
const cudaNames = process.platform === 'win32'
? ['win-x64-cuda']
: process.platform === 'linux'
? ['linux-x64-cuda']
: [];
for (const root of _llamaBackendRoots()) {
for (const name of cudaNames) {
if (fs.existsSync(path.join(root, name))) return true;
}
}
return false;
}
/**
* Product edition: full (bundled extras) or lite (add-on downloads).
* @returns {'full' | 'lite'}
*/
function getInstallEdition() {
const manifest = _readInstallManifest();
if (manifest?.edition === 'lite') return 'lite';
return 'full';
}
/**
* Display / identity metadata for the running install.
*/
function getProductIdentity() {
const manifest = _readInstallManifest();
if (manifest?.edition === 'lite') {
return {
...LITE_IDENTITY,
appId: manifest.appId || LITE_IDENTITY.appId,
productName: manifest.productName || LITE_IDENTITY.productName,
userDataName: manifest.userDataName || LITE_IDENTITY.userDataName,
appUserModelId: manifest.appUserModelId || LITE_IDENTITY.appUserModelId,
tagline: manifest.tagline || LITE_IDENTITY.tagline,
};
}
return { ...FULL_IDENTITY };
}
/**
* Apply Lite vs Full identity before any userData path is read.
* Call once at process start (packaged Lite must not share Full AppData).
* @param {import('electron').App} electronApp
*/
function applyProductIdentity(electronApp) {
const identity = getProductIdentity();
if (identity.edition === 'lite') {
try {
electronApp.setName(identity.userDataName);
} catch (_) {}
try {
const appData = electronApp.getPath('appData');
electronApp.setPath('userData', path.join(appData, identity.userDataName));
} catch (_) {}
}
if (process.platform === 'win32') {
try {
electronApp.setAppUserModelId(identity.appUserModelId);
} catch (_) {}
}
return identity;
}
/**
* Installed GPU variant: cpu (Vulkan/CPU inference) or cuda (NVIDIA CUDA).
* Priority: build manifest → app-update.yml channel → bundled CUDA binaries.
* @returns {'cuda' | 'cpu'}
*/
function getInstallVariant() {
const manifest = _readInstallManifest();
if (manifest?.gpuBackend === 'cuda' || manifest?.gpuBackend === 'cpu') {
return manifest.gpuBackend;
}
if (manifest?.variant === 'cuda' || manifest?.variant === 'cpu') {
return manifest.variant;
}
const ymlChannel = _readAppUpdateYmlChannel();
if (ymlChannel === 'cuda' || ymlChannel === 'lite-cuda') return 'cuda';
if (_hasCudaBackendBinaries()) return 'cuda';
return 'cpu';
}
/**
* electron-updater channel.
* null / unset → latest.yml (CPU). 'cuda' → cuda.yml. 'lite-cuda' → lite-cuda.yml.
* @returns {string|null}
*/
function getUpdateChannel() {
const manifest = _readInstallManifest();
if (manifest?.channel && manifest.channel !== 'latest') {
return manifest.channel;
}
if (manifest?.edition === 'lite' && getInstallVariant() === 'cuda') return 'lite-cuda';
if (manifest?.variant === 'cuda') return 'cuda';
const ymlChannel = _readAppUpdateYmlChannel();
if (ymlChannel) return ymlChannel;
return getInstallVariant() === 'cuda' ? 'cuda' : null;
}
/** Reject feed artifacts that do not match this install. */
function isUpdateArtifactCompatible(updateInfo, installVariant = getInstallVariant()) {
if (!updateInfo) return true;
const names = [];
if (typeof updateInfo.path === 'string') names.push(updateInfo.path);
if (Array.isArray(updateInfo.files)) {
for (const f of updateInfo.files) {
if (f?.url) names.push(f.url);
}
}
const blob = names.join(' ').toLowerCase();
if (!blob) return true;
const edition = getInstallEdition();
const looksLite = blob.includes('-lite-');
if (edition === 'lite' && !looksLite && (blob.includes('-cuda-') || blob.includes('-cpu-'))) {
return false;
}
if (edition === 'full' && looksLite) return false;
const looksCpu = blob.includes('-cpu-');
const looksCuda = blob.includes('-cuda-');
if (installVariant === 'cuda' && looksCpu && !looksCuda) return false;
if (installVariant === 'cpu' && looksCuda && !looksCpu) return false;
return true;
}
function getGithubFeedConfig() {
return {
provider: 'github',
owner: GITHUB_OWNER,
repo: GITHUB_REPO,
};
}
module.exports = {
GITHUB_OWNER,
GITHUB_REPO,
FULL_IDENTITY,
LITE_IDENTITY,
getInstallEdition,
getProductIdentity,
applyProductIdentity,
getInstallVariant,
getUpdateChannel,
getGithubFeedConfig,
isUpdateArtifactCompatible,
};