@walkhi/code-relax 0.1.0-beta.50 → 0.1.0-beta.59
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/README.md +105 -105
- package/dist/bin/cleanup-accounts.mjs +63 -63
- package/dist/bin/codex-remote.mjs +403 -403
- package/dist/bin/publish-android-release.mjs +63 -63
- package/dist/bin/release-upload-server.mjs +190 -190
- package/dist/bin/set-relay-admin-password.mjs +11 -0
- package/dist/build-platform.json +1 -1
- package/dist/get-shared-service-url.ps1 +14 -14
- package/dist/shared/code-browser.cjs +159 -159
- package/dist/shared/file-changes.cjs +31 -31
- package/dist/shared/file-citations.cjs +117 -117
- package/dist/shared/file-mentions.cjs +92 -92
- package/dist/shared/message-delivery.cjs +115 -115
- package/dist/shared/p2p-file-channel.cjs +87 -87
- package/dist/shared/release-version.cjs +31 -0
- package/dist/shared/server-requests.cjs +311 -311
- package/dist/shared/timeline-sync.cjs +173 -173
- package/dist/skills/code-relax/SKILL.md +22 -22
- package/dist/src/bounded-process-log.mjs +63 -63
- package/dist/src/celsius-client.mjs +107 -107
- package/dist/src/dots-approval-timeline.mjs +99 -99
- package/dist/src/dots-avatar.mjs +42 -42
- package/dist/src/dots-chat-projection.mjs +50 -50
- package/dist/src/dots-chat-server.mjs +483 -483
- package/dist/src/dots-message-images.mjs +319 -319
- package/dist/src/dots-profile.mjs +77 -77
- package/dist/src/dots-room-activity.mjs +172 -172
- package/dist/src/dots-room-client.mjs +137 -135
- package/dist/src/dots-room-projection.mjs +94 -94
- package/dist/src/dots-schedules.mjs +66 -66
- package/dist/src/dots-summary-tasks.mjs +32 -32
- package/dist/src/dots-task-catalog.mjs +430 -430
- package/dist/src/file-transfer-range.mjs +22 -22
- package/dist/src/file-upload-session.mjs +146 -130
- package/dist/src/hosted-history-app-server.mjs +103 -103
- package/dist/src/hosted-history-cache.mjs +113 -113
- package/dist/src/lan-socket-server.mjs +338 -266
- package/dist/src/managed-app-server.mjs +373 -373
- package/dist/src/managed-endpoint.mjs +25 -25
- package/dist/src/message-upload-files.mjs +109 -109
- package/dist/src/onboarding.mjs +12 -12
- package/dist/src/platform/README.md +1 -1
- package/dist/src/platform/codex-protocol.mjs +46 -46
- package/dist/src/platform/desktop-project-store.mjs +219 -208
- package/dist/src/platform/linux/desktop-host.mjs +87 -87
- package/dist/src/platform/linux/process.mjs +53 -53
- package/dist/src/platform/linux/system.mjs +50 -50
- package/dist/src/platform/linux/user-environment.mjs +9 -9
- package/dist/src/platform/macos/codex-runtime.mjs +125 -125
- package/dist/src/platform/macos/desktop-host.mjs +86 -86
- package/dist/src/platform/macos/user-environment.mjs +9 -9
- package/dist/src/platform/native-package.mjs +58 -58
- package/dist/src/platform/windows/activate-desktop.ps1 +49 -49
- package/dist/src/platform/windows/codex-runtime.mjs +215 -215
- package/dist/src/platform/windows/credential-store.mjs +27 -27
- package/dist/src/platform/windows/credential-store.ps1 +49 -49
- package/dist/src/platform/windows/desktop-host.mjs +65 -65
- package/dist/src/platform/windows/desktop-info.ps1 +38 -38
- package/dist/src/platform/windows/desktop-project-registration.mjs +4 -4
- package/dist/src/platform/windows/desktop-shortcuts.ps1 +14 -14
- package/dist/src/platform/windows/dots-profile.mjs +49 -49
- package/dist/src/platform/windows/launch-worker.mjs +41 -41
- package/dist/src/platform/windows/project-locations.mjs +16 -16
- package/dist/src/platform/windows/service-host.mjs +35 -35
- package/dist/src/platform/windows/user-environment.mjs +52 -52
- package/dist/src/project-management.mjs +58 -58
- package/dist/src/runtime-discovery.mjs +47 -47
- package/dist/src/self-relay/account-cleanup.mjs +30 -30
- package/dist/src/self-relay/admin-auth.mjs +59 -0
- package/dist/src/self-relay/admin-login-page.mjs +7 -0
- package/dist/src/self-relay/admin-page.mjs +87 -80
- package/dist/src/self-relay/admin-server.mjs +49 -18
- package/dist/src/self-relay/admin-style.mjs +58 -14
- package/dist/src/self-relay/admin-workstations-page.mjs +95 -0
- package/dist/src/self-relay/admin-workstations.mjs +143 -0
- package/dist/src/self-relay/connection-reporting.mjs +38 -0
- package/dist/src/self-relay/connector.mjs +444 -439
- package/dist/src/self-relay/demo.mjs +31 -31
- package/dist/src/self-relay/diagnostics.mjs +54 -54
- package/dist/src/self-relay/email-accounts.mjs +183 -183
- package/dist/src/self-relay/file-wire.mjs +13 -13
- package/dist/src/self-relay/lifecycle.mjs +11 -0
- package/dist/src/self-relay/p2p-probe.mjs +168 -168
- package/dist/src/self-relay/phone-connections.mjs +55 -0
- package/dist/src/self-relay/privacy-page.mjs +26 -26
- package/dist/src/self-relay/public-releases.mjs +67 -67
- package/dist/src/self-relay/rtc-file-bridge.mjs +46 -41
- package/dist/src/self-relay/server.mjs +63 -3
- package/dist/src/self-relay/usage-meter.mjs +141 -141
- package/dist/src/server.mjs +23 -9
- package/dist/src/service-doctor.mjs +148 -148
- package/dist/src/service-install.mjs +97 -97
- package/dist/src/service-lifecycle.mjs +139 -139
- package/dist/src/service-update.mjs +6 -3
- package/dist/src/shared-app-server.mjs +683 -666
- package/dist/src/shared-catalog.mjs +132 -132
- package/dist/src/shared-thread-stream.mjs +165 -165
- package/dist/src/usage-reset.mjs +17 -17
- package/dist/src/workstation-downloads.mjs +120 -120
- package/package.json +4 -4
- package/tools/postinstall.mjs +186 -186
|
@@ -1,208 +1,219 @@
|
|
|
1
|
-
import fs from 'node:fs/promises';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { createHash, randomUUID } from 'node:crypto';
|
|
4
|
-
import { setTimeout as delay } from 'node:timers/promises';
|
|
5
|
-
import { processAlive } from '../background-process.mjs';
|
|
6
|
-
|
|
7
|
-
// Desktop state is validated by structure, not by application version.
|
|
8
|
-
// Desktop reads this file once and flushes its complete in-memory snapshot.
|
|
9
|
-
const digest = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
10
|
-
const key = value => process.platform === 'win32'
|
|
11
|
-
? path.win32.normalize(value).replace(/[\\/]+$/, '').toLowerCase() : path.posix.normalize(value).replace(/\/+$/, '');
|
|
12
|
-
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
13
|
-
const fail = (message, details) => { throw Object.assign(new Error(message), { statusCode: 409, details }); };
|
|
14
|
-
|
|
15
|
-
export const desktopProjectRestartWarning = '创建项目需要短暂关闭并重新打开 Codex Desktop,以完成桌面登记。请先保存桌面上未发送的内容;仅由旧桌面实例承接的任务可能中断。手机连接、工作站服务和 Codex 内核保持运行。';
|
|
16
|
-
export const desktopProjectRemovalWarning = '移除项目需要短暂关闭并重新打开 Codex Desktop,以同步项目列表。请先保存桌面上未发送的内容;仅由旧桌面实例承接的任务可能中断。不会删除文件或对话,工作站服务和 Codex 内核保持运行。';
|
|
17
|
-
|
|
18
|
-
function parseState(bytes) {
|
|
19
|
-
const state = JSON.parse(bytes.toString('utf8').replace(/^\uFEFF/, ''));
|
|
20
|
-
if (!object(state) || !object(state['local-projects']) || !Array.isArray(state['project-order'])
|
|
21
|
-
|| state['project-order'].some(id => typeof id !== 'string')) {
|
|
22
|
-
fail('Codex Desktop 项目文件结构无效,未修改桌面文件。');
|
|
23
|
-
}
|
|
24
|
-
return state;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function registerProjectInState(state, project, codexDataRoot) {
|
|
28
|
-
if (!Number.isFinite(project.createdAt) || !Number.isFinite(project.updatedAt)) {
|
|
29
|
-
fail('内核项目时间字段无效,未修改桌面文件。');
|
|
30
|
-
}
|
|
31
|
-
const projects = state['local-projects'];
|
|
32
|
-
const scope = state['app-server-project-id-by-legacy-project-id-by-host']?.[`local:${codexDataRoot}`];
|
|
33
|
-
if (scope !== undefined && !object(scope)) fail('Codex Desktop 项目映射结构无效,未修改桌面文件。');
|
|
34
|
-
for (const [legacyId, nativeId] of Object.entries(scope || {})) {
|
|
35
|
-
if ((legacyId === project.id || nativeId === project.id) && legacyId !== nativeId) {
|
|
36
|
-
fail('该项目已有不同的桌面身份映射,未覆盖已有登记。');
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
const previous = projects[project.id];
|
|
40
|
-
const rootPaths = project.roots.map(root => root.path);
|
|
41
|
-
if (previous && (previous.id !== project.id || previous.name !== project.name || !Array.isArray(previous.rootPaths)
|
|
42
|
-
|| previous.rootPaths.length !== rootPaths.length || previous.rootPaths.some((root, index) => key(root) !== key(rootPaths[index])))) {
|
|
43
|
-
fail('该项目的桌面登记内容已变化,未覆盖已有登记。');
|
|
44
|
-
}
|
|
45
|
-
// With identical native/desktop IDs, Desktop storeProject adopts the native
|
|
46
|
-
// project at startup. No legacy mapping, thread reassignment or import is needed.
|
|
47
|
-
const entry = previous || {
|
|
48
|
-
id: project.id, name: project.name, rootPaths,
|
|
49
|
-
createdAt: project.createdAt * 1000, updatedAt: project.updatedAt * 1000,
|
|
50
|
-
};
|
|
51
|
-
// Put the new project first in both the registry and
|
|
52
|
-
// its explicit display order, including a partial registration resumed later.
|
|
53
|
-
const result = { ...state, 'local-projects': { [project.id]: entry,
|
|
54
|
-
...Object.fromEntries(Object.entries(projects).filter(([id]) => id !== project.id)) } };
|
|
55
|
-
result['project-order'] = [project.id, ...state['project-order'].filter(id => id !== project.id)];
|
|
56
|
-
return result;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function updateProjectInState(state, project, codexDataRoot, previous) {
|
|
60
|
-
const scope = state['app-server-project-id-by-legacy-project-id-by-host']?.[`local:${codexDataRoot}`];
|
|
61
|
-
if (scope !== undefined && !object(scope)) fail('Codex Desktop 项目映射结构无效。');
|
|
62
|
-
const ids = new Set([project.id, ...Object.entries(scope || {}).filter(([, id]) => id === project.id).map(([id]) => id)]);
|
|
63
|
-
const projects = { ...state['local-projects'] };
|
|
64
|
-
const matches = (entry, value) => entry.name === value.name && Array.isArray(entry.rootPaths)
|
|
65
|
-
&& entry.rootPaths.length === value.roots.length && entry.rootPaths.every((root, i) => key(root) === key(value.roots[i].path));
|
|
66
|
-
let found = false;
|
|
67
|
-
for (const id of ids) {
|
|
68
|
-
const entry = projects[id]; if (!entry) continue;
|
|
69
|
-
if (!matches(entry, previous) && !matches(entry, project)) fail('桌面项目已被其他操作修改,请刷新后重新编辑。');
|
|
70
|
-
projects[id] = { ...entry, name: project.name, rootPaths: project.roots.map(root => root.path), updatedAt: project.updatedAt * 1000 };
|
|
71
|
-
found = true;
|
|
72
|
-
}
|
|
73
|
-
return found ? { ...state, 'local-projects': projects } : registerProjectInState(state, project, codexDataRoot);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function removeProjectFromState(state, project, codexDataRoot) {
|
|
77
|
-
const scopeKey = `local:${codexDataRoot}`;
|
|
78
|
-
const mappings = state['app-server-project-id-by-legacy-project-id-by-host'];
|
|
79
|
-
const scope = mappings?.[scopeKey];
|
|
80
|
-
if (scope !== undefined && !object(scope)) fail('Codex Desktop 项目映射结构无效,未修改桌面文件。');
|
|
81
|
-
const ids = new Set([project.id, ...Object.entries(scope || {}).filter(([, id]) => id === project.id).map(([id]) => id)]);
|
|
82
|
-
const result = { ...state, 'local-projects': { ...state['local-projects'] },
|
|
83
|
-
'project-order': state['project-order'].filter(id => !ids.has(id)) };
|
|
84
|
-
for (const id of ids) delete result['local-projects'][id];
|
|
85
|
-
if (state['pinned-project-ids'] !== undefined) {
|
|
86
|
-
if (!Array.isArray(state['pinned-project-ids'])) fail('Codex Desktop 项目置顶结构无效。');
|
|
87
|
-
result['pinned-project-ids'] = state['pinned-project-ids'].filter(id => !ids.has(id));
|
|
88
|
-
}
|
|
89
|
-
if (scope) result['app-server-project-id-by-legacy-project-id-by-host'] = { ...mappings,
|
|
90
|
-
[scopeKey]: Object.fromEntries(Object.entries(scope).filter(([id]) => !ids.has(id))) };
|
|
91
|
-
const assignments = state['thread-project-assignments'];
|
|
92
|
-
if (assignments !== undefined) {
|
|
93
|
-
if (!object(assignments) || state['projectless-thread-ids'] !== undefined && !Array.isArray(state['projectless-thread-ids'])) {
|
|
94
|
-
fail('Codex Desktop 对话项目归属结构无效。');
|
|
95
|
-
}
|
|
96
|
-
const remaining = { ...assignments }, projectless = new Set(state['projectless-thread-ids'] || []);
|
|
97
|
-
for (const [id, assignment] of Object.entries(assignments)) {
|
|
98
|
-
if (assignment?.projectKind !== 'local' || !ids.has(assignment.projectId)) continue;
|
|
99
|
-
delete remaining[id]; projectless.add(id);
|
|
100
|
-
}
|
|
101
|
-
result['thread-project-assignments'] = remaining;
|
|
102
|
-
result['projectless-thread-ids'] = [...projectless];
|
|
103
|
-
}
|
|
104
|
-
return result;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
async function acquireLock(stateRoot, operationId) {
|
|
108
|
-
const file = path.join(stateRoot, 'desktop-launch.lock');
|
|
109
|
-
try {
|
|
110
|
-
const previous = JSON.parse(await fs.readFile(file, 'utf8') || 'null');
|
|
111
|
-
if (previous?.owner === 'project-registration' && Number.isInteger(previous.pid) && !processAlive(previous.pid)) {
|
|
112
|
-
await fs.unlink(file);
|
|
113
|
-
}
|
|
114
|
-
} catch (error) { if (error.code !== 'ENOENT' && !(error instanceof SyntaxError)) throw error; }
|
|
115
|
-
let handle;
|
|
116
|
-
try { handle = await fs.open(file, 'wx'); }
|
|
117
|
-
catch (error) { if (error.code === 'EEXIST') fail('Codex Desktop 正在启动或登记项目,请稍后再试。'); throw error; }
|
|
118
|
-
try { await handle.writeFile(JSON.stringify({ owner: 'project-registration', pid: process.pid, operationId })); }
|
|
119
|
-
catch (error) { await handle.close(); await fs.unlink(file); throw error; }
|
|
120
|
-
return async () => { await handle.close(); await fs.unlink(file); };
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export async function withDesktopProjectRegistration(options, action) {
|
|
124
|
-
const { stateRoot, codexDataRoot, operationId, confirmed, serverUrl,
|
|
125
|
-
host, wait = delay, readyTimeoutMs = 30000 } = options;
|
|
126
|
-
const stateFile = path.join(codexDataRoot, '.codex-global-state.json');
|
|
127
|
-
const removing = options.operation === 'remove';
|
|
128
|
-
const updating = options.operation === 'update';
|
|
129
|
-
const warning = removing ? desktopProjectRemovalWarning : updating ? desktopProjectRestartWarning.replace('创建项目', '编辑项目') : desktopProjectRestartWarning;
|
|
130
|
-
const release = await acquireLock(stateRoot, operationId);
|
|
131
|
-
try {
|
|
132
|
-
try { await fs.access(path.join(stateRoot, 'shared-recovery.lock')); fail('Codex Desktop 正在恢复连接,请稍后操作项目。'); }
|
|
133
|
-
catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
134
|
-
const info = await host.inspect();
|
|
135
|
-
parseState(await fs.readFile(stateFile));
|
|
136
|
-
if (info.running && confirmed !== true) fail(warning, { desktopRestartRequired: true });
|
|
137
|
-
if (info.running && await host.sharedServiceUrl() !== serverUrl) {
|
|
138
|
-
fail('Codex Desktop 尚未接入当前工作站内核,请先完成 Desktop 接入后操作项目。');
|
|
139
|
-
}
|
|
140
|
-
const mutate = async (project, operation, saveOperation, removeNative) => {
|
|
141
|
-
const transform = updating ? (state, value, root) => updateProjectInState(state, value, root, operation.previousProject)
|
|
142
|
-
: removing ? removeProjectFromState : registerProjectInState;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
if (
|
|
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
|
-
}
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
4
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
5
|
+
import { processAlive } from '../background-process.mjs';
|
|
6
|
+
|
|
7
|
+
// Desktop state is validated by structure, not by application version.
|
|
8
|
+
// Desktop reads this file once and flushes its complete in-memory snapshot.
|
|
9
|
+
const digest = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
10
|
+
const key = value => process.platform === 'win32'
|
|
11
|
+
? path.win32.normalize(value).replace(/[\\/]+$/, '').toLowerCase() : path.posix.normalize(value).replace(/\/+$/, '');
|
|
12
|
+
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
13
|
+
const fail = (message, details) => { throw Object.assign(new Error(message), { statusCode: 409, details }); };
|
|
14
|
+
|
|
15
|
+
export const desktopProjectRestartWarning = '创建项目需要短暂关闭并重新打开 Codex Desktop,以完成桌面登记。请先保存桌面上未发送的内容;仅由旧桌面实例承接的任务可能中断。手机连接、工作站服务和 Codex 内核保持运行。';
|
|
16
|
+
export const desktopProjectRemovalWarning = '移除项目需要短暂关闭并重新打开 Codex Desktop,以同步项目列表。请先保存桌面上未发送的内容;仅由旧桌面实例承接的任务可能中断。不会删除文件或对话,工作站服务和 Codex 内核保持运行。';
|
|
17
|
+
|
|
18
|
+
function parseState(bytes) {
|
|
19
|
+
const state = JSON.parse(bytes.toString('utf8').replace(/^\uFEFF/, ''));
|
|
20
|
+
if (!object(state) || !object(state['local-projects']) || !Array.isArray(state['project-order'])
|
|
21
|
+
|| state['project-order'].some(id => typeof id !== 'string')) {
|
|
22
|
+
fail('Codex Desktop 项目文件结构无效,未修改桌面文件。');
|
|
23
|
+
}
|
|
24
|
+
return state;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function registerProjectInState(state, project, codexDataRoot) {
|
|
28
|
+
if (!Number.isFinite(project.createdAt) || !Number.isFinite(project.updatedAt)) {
|
|
29
|
+
fail('内核项目时间字段无效,未修改桌面文件。');
|
|
30
|
+
}
|
|
31
|
+
const projects = state['local-projects'];
|
|
32
|
+
const scope = state['app-server-project-id-by-legacy-project-id-by-host']?.[`local:${codexDataRoot}`];
|
|
33
|
+
if (scope !== undefined && !object(scope)) fail('Codex Desktop 项目映射结构无效,未修改桌面文件。');
|
|
34
|
+
for (const [legacyId, nativeId] of Object.entries(scope || {})) {
|
|
35
|
+
if ((legacyId === project.id || nativeId === project.id) && legacyId !== nativeId) {
|
|
36
|
+
fail('该项目已有不同的桌面身份映射,未覆盖已有登记。');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const previous = projects[project.id];
|
|
40
|
+
const rootPaths = project.roots.map(root => root.path);
|
|
41
|
+
if (previous && (previous.id !== project.id || previous.name !== project.name || !Array.isArray(previous.rootPaths)
|
|
42
|
+
|| previous.rootPaths.length !== rootPaths.length || previous.rootPaths.some((root, index) => key(root) !== key(rootPaths[index])))) {
|
|
43
|
+
fail('该项目的桌面登记内容已变化,未覆盖已有登记。');
|
|
44
|
+
}
|
|
45
|
+
// With identical native/desktop IDs, Desktop storeProject adopts the native
|
|
46
|
+
// project at startup. No legacy mapping, thread reassignment or import is needed.
|
|
47
|
+
const entry = previous || {
|
|
48
|
+
id: project.id, name: project.name, rootPaths,
|
|
49
|
+
createdAt: project.createdAt * 1000, updatedAt: project.updatedAt * 1000,
|
|
50
|
+
};
|
|
51
|
+
// Put the new project first in both the registry and
|
|
52
|
+
// its explicit display order, including a partial registration resumed later.
|
|
53
|
+
const result = { ...state, 'local-projects': { [project.id]: entry,
|
|
54
|
+
...Object.fromEntries(Object.entries(projects).filter(([id]) => id !== project.id)) } };
|
|
55
|
+
result['project-order'] = [project.id, ...state['project-order'].filter(id => id !== project.id)];
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function updateProjectInState(state, project, codexDataRoot, previous) {
|
|
60
|
+
const scope = state['app-server-project-id-by-legacy-project-id-by-host']?.[`local:${codexDataRoot}`];
|
|
61
|
+
if (scope !== undefined && !object(scope)) fail('Codex Desktop 项目映射结构无效。');
|
|
62
|
+
const ids = new Set([project.id, ...Object.entries(scope || {}).filter(([, id]) => id === project.id).map(([id]) => id)]);
|
|
63
|
+
const projects = { ...state['local-projects'] };
|
|
64
|
+
const matches = (entry, value) => entry.name === value.name && Array.isArray(entry.rootPaths)
|
|
65
|
+
&& entry.rootPaths.length === value.roots.length && entry.rootPaths.every((root, i) => key(root) === key(value.roots[i].path));
|
|
66
|
+
let found = false;
|
|
67
|
+
for (const id of ids) {
|
|
68
|
+
const entry = projects[id]; if (!entry) continue;
|
|
69
|
+
if (!matches(entry, previous) && !matches(entry, project)) fail('桌面项目已被其他操作修改,请刷新后重新编辑。');
|
|
70
|
+
projects[id] = { ...entry, name: project.name, rootPaths: project.roots.map(root => root.path), updatedAt: project.updatedAt * 1000 };
|
|
71
|
+
found = true;
|
|
72
|
+
}
|
|
73
|
+
return found ? { ...state, 'local-projects': projects } : registerProjectInState(state, project, codexDataRoot);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function removeProjectFromState(state, project, codexDataRoot) {
|
|
77
|
+
const scopeKey = `local:${codexDataRoot}`;
|
|
78
|
+
const mappings = state['app-server-project-id-by-legacy-project-id-by-host'];
|
|
79
|
+
const scope = mappings?.[scopeKey];
|
|
80
|
+
if (scope !== undefined && !object(scope)) fail('Codex Desktop 项目映射结构无效,未修改桌面文件。');
|
|
81
|
+
const ids = new Set([project.id, ...Object.entries(scope || {}).filter(([, id]) => id === project.id).map(([id]) => id)]);
|
|
82
|
+
const result = { ...state, 'local-projects': { ...state['local-projects'] },
|
|
83
|
+
'project-order': state['project-order'].filter(id => !ids.has(id)) };
|
|
84
|
+
for (const id of ids) delete result['local-projects'][id];
|
|
85
|
+
if (state['pinned-project-ids'] !== undefined) {
|
|
86
|
+
if (!Array.isArray(state['pinned-project-ids'])) fail('Codex Desktop 项目置顶结构无效。');
|
|
87
|
+
result['pinned-project-ids'] = state['pinned-project-ids'].filter(id => !ids.has(id));
|
|
88
|
+
}
|
|
89
|
+
if (scope) result['app-server-project-id-by-legacy-project-id-by-host'] = { ...mappings,
|
|
90
|
+
[scopeKey]: Object.fromEntries(Object.entries(scope).filter(([id]) => !ids.has(id))) };
|
|
91
|
+
const assignments = state['thread-project-assignments'];
|
|
92
|
+
if (assignments !== undefined) {
|
|
93
|
+
if (!object(assignments) || state['projectless-thread-ids'] !== undefined && !Array.isArray(state['projectless-thread-ids'])) {
|
|
94
|
+
fail('Codex Desktop 对话项目归属结构无效。');
|
|
95
|
+
}
|
|
96
|
+
const remaining = { ...assignments }, projectless = new Set(state['projectless-thread-ids'] || []);
|
|
97
|
+
for (const [id, assignment] of Object.entries(assignments)) {
|
|
98
|
+
if (assignment?.projectKind !== 'local' || !ids.has(assignment.projectId)) continue;
|
|
99
|
+
delete remaining[id]; projectless.add(id);
|
|
100
|
+
}
|
|
101
|
+
result['thread-project-assignments'] = remaining;
|
|
102
|
+
result['projectless-thread-ids'] = [...projectless];
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function acquireLock(stateRoot, operationId) {
|
|
108
|
+
const file = path.join(stateRoot, 'desktop-launch.lock');
|
|
109
|
+
try {
|
|
110
|
+
const previous = JSON.parse(await fs.readFile(file, 'utf8') || 'null');
|
|
111
|
+
if (previous?.owner === 'project-registration' && Number.isInteger(previous.pid) && !processAlive(previous.pid)) {
|
|
112
|
+
await fs.unlink(file);
|
|
113
|
+
}
|
|
114
|
+
} catch (error) { if (error.code !== 'ENOENT' && !(error instanceof SyntaxError)) throw error; }
|
|
115
|
+
let handle;
|
|
116
|
+
try { handle = await fs.open(file, 'wx'); }
|
|
117
|
+
catch (error) { if (error.code === 'EEXIST') fail('Codex Desktop 正在启动或登记项目,请稍后再试。'); throw error; }
|
|
118
|
+
try { await handle.writeFile(JSON.stringify({ owner: 'project-registration', pid: process.pid, operationId })); }
|
|
119
|
+
catch (error) { await handle.close(); await fs.unlink(file); throw error; }
|
|
120
|
+
return async () => { await handle.close(); await fs.unlink(file); };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export async function withDesktopProjectRegistration(options, action) {
|
|
124
|
+
const { stateRoot, codexDataRoot, operationId, confirmed, serverUrl,
|
|
125
|
+
host, wait = delay, readyTimeoutMs = 30000 } = options;
|
|
126
|
+
const stateFile = path.join(codexDataRoot, '.codex-global-state.json');
|
|
127
|
+
const removing = options.operation === 'remove';
|
|
128
|
+
const updating = options.operation === 'update';
|
|
129
|
+
const warning = removing ? desktopProjectRemovalWarning : updating ? desktopProjectRestartWarning.replace('创建项目', '编辑项目') : desktopProjectRestartWarning;
|
|
130
|
+
const release = await acquireLock(stateRoot, operationId);
|
|
131
|
+
try {
|
|
132
|
+
try { await fs.access(path.join(stateRoot, 'shared-recovery.lock')); fail('Codex Desktop 正在恢复连接,请稍后操作项目。'); }
|
|
133
|
+
catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
134
|
+
const info = await host.inspect();
|
|
135
|
+
parseState(await fs.readFile(stateFile));
|
|
136
|
+
if (info.running && confirmed !== true) fail(warning, { desktopRestartRequired: true });
|
|
137
|
+
if (info.running && await host.sharedServiceUrl() !== serverUrl) {
|
|
138
|
+
fail('Codex Desktop 尚未接入当前工作站内核,请先完成 Desktop 接入后操作项目。');
|
|
139
|
+
}
|
|
140
|
+
const mutate = async (project, operation, saveOperation, removeNative) => {
|
|
141
|
+
const transform = updating ? (state, value, root) => updateProjectInState(state, value, root, operation.previousProject)
|
|
142
|
+
: removing ? removeProjectFromState : registerProjectInState;
|
|
143
|
+
const verify = async () => {
|
|
144
|
+
const finalState = parseState(await fs.readFile(stateFile));
|
|
145
|
+
const verified = transform(finalState, project, codexDataRoot);
|
|
146
|
+
if (JSON.stringify(verified) !== JSON.stringify(finalState)) throw new Error('Desktop 项目状态未能在最终文件中核对,请检查后继续完成操作。');
|
|
147
|
+
};
|
|
148
|
+
let restartNeeded = operation.desktopWasRunning === true;
|
|
149
|
+
let registrationError;
|
|
150
|
+
const current = await host.inspect();
|
|
151
|
+
if (current.desktop !== info.desktop || current.desktopIdentity !== info.desktopIdentity) fail('Codex Desktop 安装版本发生变化,项目保留,未修改桌面文件。');
|
|
152
|
+
if (current.running) {
|
|
153
|
+
if (confirmed !== true) fail(warning, { desktopRestartRequired: true });
|
|
154
|
+
if (await host.sharedServiceUrl() !== serverUrl) fail('Codex Desktop 内核连接已变化,未关闭桌面。');
|
|
155
|
+
restartNeeded = true;
|
|
156
|
+
operation.desktopWasRunning = true;
|
|
157
|
+
await saveOperation();
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
if (current.running) await host.closeDesktop(info.desktop);
|
|
161
|
+
if ((await host.inspect()).running) fail('Codex Desktop 尚未退出,未修改桌面文件。');
|
|
162
|
+
const original = await fs.readFile(stateFile);
|
|
163
|
+
const state = parseState(original);
|
|
164
|
+
const updated = transform(state, project, codexDataRoot);
|
|
165
|
+
if (JSON.stringify(updated) !== JSON.stringify(state)) {
|
|
166
|
+
const backupDirectory = path.join(stateRoot, 'desktop-project-backups');
|
|
167
|
+
await fs.mkdir(backupDirectory, { recursive: true });
|
|
168
|
+
const backup = path.join(backupDirectory, `${operationId}-${randomUUID()}.json`);
|
|
169
|
+
await fs.writeFile(backup, original, { flag: 'wx', mode: 0o600 });
|
|
170
|
+
operation.desktopBackup = backup;
|
|
171
|
+
await saveOperation();
|
|
172
|
+
const temporary = `${stateFile}.${operationId}.tmp`;
|
|
173
|
+
try {
|
|
174
|
+
const source = original.toString('utf8');
|
|
175
|
+
const replacement = Buffer.from(`${source.startsWith('\uFEFF') ? '\uFEFF' : ''}${JSON.stringify(updated)}${source.endsWith('\n') ? '\n' : ''}`);
|
|
176
|
+
await fs.writeFile(temporary, replacement, { flag: 'wx', mode: 0o600 });
|
|
177
|
+
if ((await host.inspect()).running || digest(await fs.readFile(stateFile)) !== digest(original)) {
|
|
178
|
+
fail('Codex Desktop 状态在登记期间发生变化,未覆盖原文件,请核对后重试。');
|
|
179
|
+
}
|
|
180
|
+
await fs.rename(temporary, stateFile);
|
|
181
|
+
if (digest(await fs.readFile(stateFile)) !== digest(replacement)) throw new Error('Codex Desktop 登记文件未能核对,原状态备份已保留。');
|
|
182
|
+
} finally { await fs.rm(temporary, { force: true }); }
|
|
183
|
+
}
|
|
184
|
+
// Remove only after the desktop registry has been saved and while
|
|
185
|
+
// Desktop is closed. project/delete detaches members, never their files.
|
|
186
|
+
if (removing) await removeNative();
|
|
187
|
+
operation[removing ? 'desktopRemoved' : 'desktopRegistered'] = true;
|
|
188
|
+
await saveOperation();
|
|
189
|
+
// Verify the committed registry while Desktop is still closed. New
|
|
190
|
+
// projects are usable in the kernel without waiting for its UI.
|
|
191
|
+
if (!removing && !updating) await verify();
|
|
192
|
+
} catch (error) { registrationError = error; }
|
|
193
|
+
// Restore the UI even if the registry write failed. This never starts or
|
|
194
|
+
// restarts a kernel and never changes the service's active connections.
|
|
195
|
+
if (restartNeeded) {
|
|
196
|
+
try {
|
|
197
|
+
if (!(await host.inspect()).running) await host.startDesktop(info.desktop, { CODEX_APP_SERVER_WS_URL: serverUrl, CODEX_HOME: codexDataRoot });
|
|
198
|
+
// Creation completes once registration is verified and the OS has
|
|
199
|
+
// accepted launch. Keep update/removal's existing readiness contract.
|
|
200
|
+
if (removing || updating) {
|
|
201
|
+
const deadline = Date.now() + readyTimeoutMs;
|
|
202
|
+
while (!await host.desktopReady()) {
|
|
203
|
+
if (Date.now() >= deadline) throw new Error('Codex Desktop 尚未重新就绪。');
|
|
204
|
+
await wait(500);
|
|
205
|
+
}
|
|
206
|
+
if (await host.sharedServiceUrl() !== serverUrl) throw new Error('Desktop 未接入原工作站内核。');
|
|
207
|
+
}
|
|
208
|
+
} catch (error) {
|
|
209
|
+
throw new Error([registrationError?.message, `${removing ? '项目移除状态已记录' : '项目已保留'},重新打开 Codex Desktop 未完成:${error.message}`].filter(Boolean).join(' '));
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (registrationError) throw registrationError;
|
|
213
|
+
if (removing || updating) await verify();
|
|
214
|
+
return { status: removing ? 'removed' : updating ? 'updated' : 'registered',
|
|
215
|
+
...(removing || updating ? { restarted: restartNeeded } : { restartRequested: restartNeeded }) };
|
|
216
|
+
};
|
|
217
|
+
return await action({ register: mutate, remove: mutate });
|
|
218
|
+
} finally { await release(); }
|
|
219
|
+
}
|
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { startBackground } from '../../background-process.mjs';
|
|
4
|
-
import { systemRequest } from './system.mjs';
|
|
5
|
-
import { assertPlainPath } from '../../skill-install.mjs';
|
|
6
|
-
import { setTimeout as delay } from 'node:timers/promises';
|
|
7
|
-
import { runtimePaths } from '../runtime-paths.mjs';
|
|
8
|
-
import { readRecord } from '../../service-lifecycle.mjs';
|
|
9
|
-
export { residentStartup } from './resident-startup.mjs';
|
|
10
|
-
|
|
11
|
-
import { managedEndpointUrl, readManagedEndpoint } from '../../managed-endpoint.mjs';
|
|
12
|
-
export function createDesktopHost({ request = systemRequest, wait = delay, now = Date.now, timeout = 10000,
|
|
13
|
-
stateRoot = () => runtimePaths().stateRoot, fingerprint = file => {
|
|
14
|
-
const stat = fs.statSync(path.join(file, 'resources/linux-package-metadata.json'));
|
|
15
|
-
return `${stat.ino}:${stat.size}:${stat.mtimeMs}:${stat.ctimeMs}`;
|
|
16
|
-
} } = {}) {
|
|
17
|
-
return {
|
|
18
|
-
desktopIntegration: true,
|
|
19
|
-
async inspect() {
|
|
20
|
-
const url = readManagedEndpoint(stateRoot());
|
|
21
|
-
const info = await request({ operation: 'desktop-info', url });
|
|
22
|
-
return { ...info, managedUrl: url, desktopIdentity: info.desktop ? `${info.desktop}:${info.version}:${info.build}:${fingerprint(info.desktop)}` : null };
|
|
23
|
-
},
|
|
24
|
-
process: pid => request({ operation: 'process', pid }),
|
|
25
|
-
ownsListener: (expected, url) => request({ operation: 'owns-listener', expected, url }),
|
|
26
|
-
start: startBackground,
|
|
27
|
-
async stop(pid, expected) {
|
|
28
|
-
if (!expected || expected.pid !== pid) throw new Error('停止内核前缺少进程身份,未结束任何进程。');
|
|
29
|
-
return request({ operation: 'stop', expected });
|
|
30
|
-
},
|
|
31
|
-
async sharedServiceUrl() {
|
|
32
|
-
const info = await this.inspect();
|
|
33
|
-
return info.connectedToManagedServer ? info.managedUrl : '';
|
|
34
|
-
},
|
|
35
|
-
async startDesktop(bundlePath, environment) {
|
|
36
|
-
const managedUrl = managedEndpointUrl(environment.CODEX_APP_SERVER_WS_URL);
|
|
37
|
-
if (!managedUrl || managedUrl !== readManagedEndpoint(stateRoot())) throw new Error('Desktop 内核地址与工作站不一致。');
|
|
38
|
-
const runtime = readRecord(path.join(stateRoot(), 'codex-runtime.json'));
|
|
39
|
-
const codexHome = environment.CODEX_HOME || runtime?.codexHome;
|
|
40
|
-
if (!codexHome || !path.isAbsolute(codexHome)) throw new Error('缺少已选内核的 CODEX_HOME,请先运行 relax setup。');
|
|
41
|
-
const info = await this.inspect();
|
|
42
|
-
if (info.running) throw new Error('Desktop 仍在运行,未创建第二个实例。');
|
|
43
|
-
if (info.desktop !== bundlePath) throw new Error('Codex Desktop 安装位置已变化,请重新检查。');
|
|
44
|
-
return request({ operation: 'desktop-start', bundlePath,
|
|
45
|
-
environment: { CODEX_HOME: codexHome, CODEX_APP_SERVER_WS_URL: managedUrl } });
|
|
46
|
-
},
|
|
47
|
-
async closeDesktop(bundlePath) {
|
|
48
|
-
const info = await this.inspect();
|
|
49
|
-
if (!info.running) return;
|
|
50
|
-
if (info.desktop !== bundlePath || !info.process) throw new Error('Desktop 进程身份已变化,未请求退出。');
|
|
51
|
-
await request({ operation: 'desktop-close', bundlePath, expected: info.process });
|
|
52
|
-
const deadline = now() + timeout;
|
|
53
|
-
while ((await this.inspect()).running) {
|
|
54
|
-
if (now() >= deadline) throw new Error('Desktop 尚未正常退出,请在 Linux 上处理未保存内容后重试;未强制结束进程。');
|
|
55
|
-
await wait(200);
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
async desktopReady() {
|
|
59
|
-
const info = await this.inspect();
|
|
60
|
-
return info.running && info.finishedLaunching && info.connectedToManagedServer;
|
|
61
|
-
},
|
|
62
|
-
async prepareKernelRestart() {
|
|
63
|
-
const info = await this.inspect();
|
|
64
|
-
if (!info.running || !info.connectedToManagedServer) return null;
|
|
65
|
-
await this.closeDesktop(info.desktop);
|
|
66
|
-
return info.desktop;
|
|
67
|
-
},
|
|
68
|
-
async resumeAfterKernelRestart(bundlePath, environment) {
|
|
69
|
-
if (!bundlePath) return;
|
|
70
|
-
await this.startDesktop(bundlePath, environment);
|
|
71
|
-
const deadline = now() + 30000;
|
|
72
|
-
while (!await this.desktopReady()) {
|
|
73
|
-
if (now() >= deadline) throw new Error('内核已就绪,但 Linux Desktop 尚未重新接入。');
|
|
74
|
-
await wait(500);
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
ready: async url => { try { return (await fetch(url, { signal: AbortSignal.timeout(1000) })).ok; } catch { return false; } },
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
export const desktopHost = createDesktopHost();
|
|
81
|
-
export async function resolveStateRoot(stateRoot) {
|
|
82
|
-
if (process.getuid() === 0) throw new Error('请以普通 Linux 桌面用户安装 Code Relax,不使用 sudo。');
|
|
83
|
-
assertPlainPath(stateRoot);
|
|
84
|
-
fs.mkdirSync(stateRoot, { recursive: true, mode: 0o700 });
|
|
85
|
-
return fs.realpathSync(stateRoot);
|
|
86
|
-
}
|
|
87
|
-
export async function removeDesktopShortcut() { return { removed: false, reason: 'not-used-on-linux' }; }
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { startBackground } from '../../background-process.mjs';
|
|
4
|
+
import { systemRequest } from './system.mjs';
|
|
5
|
+
import { assertPlainPath } from '../../skill-install.mjs';
|
|
6
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
7
|
+
import { runtimePaths } from '../runtime-paths.mjs';
|
|
8
|
+
import { readRecord } from '../../service-lifecycle.mjs';
|
|
9
|
+
export { residentStartup } from './resident-startup.mjs';
|
|
10
|
+
|
|
11
|
+
import { managedEndpointUrl, readManagedEndpoint } from '../../managed-endpoint.mjs';
|
|
12
|
+
export function createDesktopHost({ request = systemRequest, wait = delay, now = Date.now, timeout = 10000,
|
|
13
|
+
stateRoot = () => runtimePaths().stateRoot, fingerprint = file => {
|
|
14
|
+
const stat = fs.statSync(path.join(file, 'resources/linux-package-metadata.json'));
|
|
15
|
+
return `${stat.ino}:${stat.size}:${stat.mtimeMs}:${stat.ctimeMs}`;
|
|
16
|
+
} } = {}) {
|
|
17
|
+
return {
|
|
18
|
+
desktopIntegration: true,
|
|
19
|
+
async inspect() {
|
|
20
|
+
const url = readManagedEndpoint(stateRoot());
|
|
21
|
+
const info = await request({ operation: 'desktop-info', url });
|
|
22
|
+
return { ...info, managedUrl: url, desktopIdentity: info.desktop ? `${info.desktop}:${info.version}:${info.build}:${fingerprint(info.desktop)}` : null };
|
|
23
|
+
},
|
|
24
|
+
process: pid => request({ operation: 'process', pid }),
|
|
25
|
+
ownsListener: (expected, url) => request({ operation: 'owns-listener', expected, url }),
|
|
26
|
+
start: startBackground,
|
|
27
|
+
async stop(pid, expected) {
|
|
28
|
+
if (!expected || expected.pid !== pid) throw new Error('停止内核前缺少进程身份,未结束任何进程。');
|
|
29
|
+
return request({ operation: 'stop', expected });
|
|
30
|
+
},
|
|
31
|
+
async sharedServiceUrl() {
|
|
32
|
+
const info = await this.inspect();
|
|
33
|
+
return info.connectedToManagedServer ? info.managedUrl : '';
|
|
34
|
+
},
|
|
35
|
+
async startDesktop(bundlePath, environment) {
|
|
36
|
+
const managedUrl = managedEndpointUrl(environment.CODEX_APP_SERVER_WS_URL);
|
|
37
|
+
if (!managedUrl || managedUrl !== readManagedEndpoint(stateRoot())) throw new Error('Desktop 内核地址与工作站不一致。');
|
|
38
|
+
const runtime = readRecord(path.join(stateRoot(), 'codex-runtime.json'));
|
|
39
|
+
const codexHome = environment.CODEX_HOME || runtime?.codexHome;
|
|
40
|
+
if (!codexHome || !path.isAbsolute(codexHome)) throw new Error('缺少已选内核的 CODEX_HOME,请先运行 relax setup。');
|
|
41
|
+
const info = await this.inspect();
|
|
42
|
+
if (info.running) throw new Error('Desktop 仍在运行,未创建第二个实例。');
|
|
43
|
+
if (info.desktop !== bundlePath) throw new Error('Codex Desktop 安装位置已变化,请重新检查。');
|
|
44
|
+
return request({ operation: 'desktop-start', bundlePath,
|
|
45
|
+
environment: { CODEX_HOME: codexHome, CODEX_APP_SERVER_WS_URL: managedUrl } });
|
|
46
|
+
},
|
|
47
|
+
async closeDesktop(bundlePath) {
|
|
48
|
+
const info = await this.inspect();
|
|
49
|
+
if (!info.running) return;
|
|
50
|
+
if (info.desktop !== bundlePath || !info.process) throw new Error('Desktop 进程身份已变化,未请求退出。');
|
|
51
|
+
await request({ operation: 'desktop-close', bundlePath, expected: info.process });
|
|
52
|
+
const deadline = now() + timeout;
|
|
53
|
+
while ((await this.inspect()).running) {
|
|
54
|
+
if (now() >= deadline) throw new Error('Desktop 尚未正常退出,请在 Linux 上处理未保存内容后重试;未强制结束进程。');
|
|
55
|
+
await wait(200);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
async desktopReady() {
|
|
59
|
+
const info = await this.inspect();
|
|
60
|
+
return info.running && info.finishedLaunching && info.connectedToManagedServer;
|
|
61
|
+
},
|
|
62
|
+
async prepareKernelRestart() {
|
|
63
|
+
const info = await this.inspect();
|
|
64
|
+
if (!info.running || !info.connectedToManagedServer) return null;
|
|
65
|
+
await this.closeDesktop(info.desktop);
|
|
66
|
+
return info.desktop;
|
|
67
|
+
},
|
|
68
|
+
async resumeAfterKernelRestart(bundlePath, environment) {
|
|
69
|
+
if (!bundlePath) return;
|
|
70
|
+
await this.startDesktop(bundlePath, environment);
|
|
71
|
+
const deadline = now() + 30000;
|
|
72
|
+
while (!await this.desktopReady()) {
|
|
73
|
+
if (now() >= deadline) throw new Error('内核已就绪,但 Linux Desktop 尚未重新接入。');
|
|
74
|
+
await wait(500);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
ready: async url => { try { return (await fetch(url, { signal: AbortSignal.timeout(1000) })).ok; } catch { return false; } },
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export const desktopHost = createDesktopHost();
|
|
81
|
+
export async function resolveStateRoot(stateRoot) {
|
|
82
|
+
if (process.getuid() === 0) throw new Error('请以普通 Linux 桌面用户安装 Code Relax,不使用 sudo。');
|
|
83
|
+
assertPlainPath(stateRoot);
|
|
84
|
+
fs.mkdirSync(stateRoot, { recursive: true, mode: 0o700 });
|
|
85
|
+
return fs.realpathSync(stateRoot);
|
|
86
|
+
}
|
|
87
|
+
export async function removeDesktopShortcut() { return { removed: false, reason: 'not-used-on-linux' }; }
|