@nimiplatform/app-tools 0.1.3 → 0.2.0
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/LICENSE +198 -6
- package/README.md +83 -27
- package/bin/nimi-app.mjs +328 -45
- package/lib/app-access-declaration.mjs +49 -0
- package/lib/app-doctor-update.mjs +411 -149
- package/lib/app-pack.mjs +401 -0
- package/lib/app-project-lifecycle.mjs +1141 -0
- package/lib/app-scaffold-capabilities.mjs +769 -0
- package/lib/app-scaffold-default-source.mjs +21 -0
- package/lib/app-scaffold-profiles.mjs +1093 -0
- package/lib/app-scaffold.mjs +1266 -813
- package/lib/index.mjs +194 -42
- package/lib/simulator-conformance-ast.mjs +498 -0
- package/lib/simulator-conformance-css.mjs +116 -0
- package/lib/simulator-conformance-graph.mjs +757 -0
- package/lib/simulator-conformance.d.mts +14 -0
- package/lib/simulator-conformance.mjs +203 -0
- package/lib/simulator-css-profile.d.mts +27 -0
- package/lib/simulator-css-profile.mjs +531 -0
- package/lib/simulator-kit-export-resolution.mjs +70 -0
- package/lib/simulator-manifest.mjs +334 -0
- package/package.json +66 -3
- package/scripts/dev-shell.mjs +465 -0
- package/scripts/sync-app-source.mjs +300 -0
- package/scripts/sync-scaffold-versions.mjs +91 -0
- package/templates/app-source/LICENSE +21 -0
- package/templates/app-source/src/ai-studio-core/ai-config.ts +100 -0
- package/templates/app-source/src/ai-studio-core/ai-studio-core.css +338 -0
- package/templates/app-source/src/ai-studio-core/contexts.tsx +61 -0
- package/templates/app-source/src/ai-studio-core/history-policy.ts +447 -0
- package/templates/app-source/src/ai-studio-core/history.ts +824 -0
- package/templates/app-source/src/ai-studio-core/host-context.tsx +79 -0
- package/templates/app-source/src/ai-studio-core/index.ts +214 -0
- package/templates/app-source/src/ai-studio-core/messages/en.json +310 -0
- package/templates/app-source/src/ai-studio-core/messages/index.ts +47 -0
- package/templates/app-source/src/ai-studio-core/messages/zh.json +310 -0
- package/templates/app-source/src/ai-studio-core/module-registration.ts +109 -0
- package/templates/app-source/src/ai-studio-core/non-success-presentation.ts +65 -0
- package/templates/app-source/src/ai-studio-core/parameter-fields.tsx +247 -0
- package/templates/app-source/src/ai-studio-core/parameters.ts +110 -0
- package/templates/app-source/src/ai-studio-core/prompt-drafts.ts +69 -0
- package/templates/app-source/src/ai-studio-core/run-target.ts +123 -0
- package/templates/app-source/src/ai-studio-core/runtime-dispatcher.ts +35 -0
- package/templates/app-source/src/ai-studio-core/runtime-types.ts +86 -0
- package/templates/app-source/src/ai-studio-core/runtime.ts +306 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-admission.ts +38 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-ai-config.tsx +30 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-artifact-preview.ts +40 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-composer.tsx +257 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-history.tsx +676 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-input.ts +19 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-output.tsx +291 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-result.tsx +413 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-run.ts +174 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-studio-result.tsx +449 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-surface.tsx +45 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing.tsx +494 -0
- package/templates/app-source/src/ai-studio-core/studio-directives.ts +33 -0
- package/templates/app-source/src/ai-studio-core/workspace.tsx +381 -0
- package/templates/app-source/src/product-modules/agent-center/index.tsx +27 -0
- package/templates/app-source/src/product-modules/agent-conversation/index.tsx +19 -0
- package/templates/app-source/src/product-modules/agent-realtime/index.tsx +29 -0
- package/templates/app-source/src/product-modules/kit-recipes/gallery.tsx +142 -0
- package/templates/app-source/src/product-modules/kit-recipes/index.tsx +19 -0
- package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-data-recipes.tsx +336 -0
- package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-demos.tsx +165 -0
- package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-recipes.tsx +682 -0
- package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-surface.tsx +341 -0
- package/templates/app-source/src/studio-modules/studio-create/descriptors.ts +26 -0
- package/templates/app-source/src/studio-modules/studio-create/index.ts +14 -0
- package/templates/app-source/src/studio-modules/studio-create/messages/en.json +67 -0
- package/templates/app-source/src/studio-modules/studio-create/messages/index.ts +5 -0
- package/templates/app-source/src/studio-modules/studio-create/messages/zh.json +67 -0
- package/templates/app-source/src/studio-modules/studio-create/parameter-panel.tsx +153 -0
- package/templates/app-source/src/studio-modules/studio-create/parameters.ts +70 -0
- package/templates/app-source/src/studio-modules/studio-create/registration.ts +46 -0
- package/templates/app-source/src/studio-modules/studio-create/runtime.ts +339 -0
- package/templates/app-source/src/studio-modules/studio-media/descriptors.ts +22 -0
- package/templates/app-source/src/studio-modules/studio-media/index.ts +12 -0
- package/templates/app-source/src/studio-modules/studio-media/messages/en.json +82 -0
- package/templates/app-source/src/studio-modules/studio-media/messages/index.ts +5 -0
- package/templates/app-source/src/studio-modules/studio-media/messages/zh.json +82 -0
- package/templates/app-source/src/studio-modules/studio-media/parameter-panel.tsx +283 -0
- package/templates/app-source/src/studio-modules/studio-media/parameters.ts +94 -0
- package/templates/app-source/src/studio-modules/studio-media/registration.ts +44 -0
- package/templates/app-source/src/studio-modules/studio-media/runtime.ts +113 -0
- package/templates/app-source/src/studio-modules/studio-voice/descriptors.ts +28 -0
- package/templates/app-source/src/studio-modules/studio-voice/index.ts +16 -0
- package/templates/app-source/src/studio-modules/studio-voice/messages/en.json +99 -0
- package/templates/app-source/src/studio-modules/studio-voice/messages/index.ts +5 -0
- package/templates/app-source/src/studio-modules/studio-voice/messages/zh.json +99 -0
- package/templates/app-source/src/studio-modules/studio-voice/parameter-panel.tsx +284 -0
- package/templates/app-source/src/studio-modules/studio-voice/parameters.ts +116 -0
- package/templates/app-source/src/studio-modules/studio-voice/registration.ts +59 -0
- package/templates/app-source/src/studio-modules/studio-voice/runtime.ts +347 -0
- package/templates/app-source/src/workbench-core/empty-state.tsx +31 -0
- package/templates/app-source/src/workbench-core/index.ts +17 -0
- package/templates/app-source/src/workbench-core/runtime-gate.tsx +131 -0
- package/templates/app-source/src/workbench-core/workbench-core.css +255 -0
- package/templates/app-source/src/workbench-core/workbench-core.tsx +140 -0
- package/templates/app-source.manifest.json +333 -0
- package/templates/default-starter/AGENTS.md +33 -0
- package/templates/default-starter/LICENSE +21 -0
- package/templates/default-starter/README.md +41 -0
- package/templates/default-starter/SECURITY.md +5 -0
- package/templates/default-starter/index.html +12 -0
- package/templates/default-starter/scripts/validate.mjs +26 -0
- package/templates/default-starter/src/shell/App.tsx +27 -0
- package/templates/default-starter/src/shell/auth/app-identity.ts +3 -0
- package/templates/default-starter/src/shell/auth/local-app-client.ts +18 -0
- package/templates/default-starter/src/shell/auth/runtime-platform.ts +110 -0
- package/templates/default-starter/src/shell/authenticated-shell.tsx +20 -0
- package/templates/default-starter/src/shell/workbench-target-adapter.ts +40 -0
- package/templates/default-starter/src/styles.css +26 -0
- package/templates/default-starter/src-tauri/Cargo.toml +17 -0
- package/templates/default-starter/src-tauri/build.rs +5 -0
- package/templates/default-starter/src-tauri/capabilities/default.json +7 -0
- package/templates/default-starter/src-tauri/tauri.conf.json +26 -0
- package/templates/default-starter/tsconfig.json +18 -0
- package/templates/default-starter.manifest.json +99 -0
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { lstat, readFile, realpath } from 'node:fs/promises';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import process from 'node:process';
|
|
7
|
+
import { parseEnv } from 'node:util';
|
|
8
|
+
import { parse as parseYaml } from 'yaml';
|
|
9
|
+
|
|
10
|
+
const DESCRIPTOR_RELATIVE_PATH = ['.nimi', 'run', 'desktop', 'local-development', 'presence.v1.json'];
|
|
11
|
+
const MAX_HEARTBEAT_AGE_MS = 10_000;
|
|
12
|
+
const REQUEST_TIMEOUT_MS = 5_000;
|
|
13
|
+
const STATUS_POLL_MS = 350;
|
|
14
|
+
const AUTO_CDP_PORT = 0;
|
|
15
|
+
const CDP_PORT_ENV_KEY = 'NIMI_APP_DEV_CDP_PORT';
|
|
16
|
+
const TERMINAL_STATES = new Set([
|
|
17
|
+
'cleanup-failed',
|
|
18
|
+
'denied',
|
|
19
|
+
'failed',
|
|
20
|
+
'project-changed',
|
|
21
|
+
'revoked',
|
|
22
|
+
'stopped',
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
export async function runDevShell(cwd, options = {}) {
|
|
26
|
+
const shell = normalizeShell(options.shell || 'electron');
|
|
27
|
+
assertLocalDevelopmentPlatform(process.platform, shell);
|
|
28
|
+
const projectRoot = await canonicalProjectRoot(cwd, options.dir);
|
|
29
|
+
const cdpPort = await resolveRequestedCdpPort(projectRoot, options);
|
|
30
|
+
const appId = await readAppId(projectRoot);
|
|
31
|
+
const descriptorPath = options.descriptorPath
|
|
32
|
+
? path.resolve(options.descriptorPath)
|
|
33
|
+
: path.join(homedir(), ...DESCRIPTOR_RELATIVE_PATH);
|
|
34
|
+
const descriptor = await readPresenceDescriptor(descriptorPath, options.now?.() ?? Date.now());
|
|
35
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
36
|
+
if (typeof fetchImpl !== 'function') {
|
|
37
|
+
throw new DevShellError('local-development-launcher-unavailable', 'This Node.js runtime does not provide fetch.');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const startIntent = {
|
|
41
|
+
schemaVersion: 1,
|
|
42
|
+
appId,
|
|
43
|
+
projectRoot,
|
|
44
|
+
shell,
|
|
45
|
+
...(cdpPort === undefined ? {} : { cdpPort }),
|
|
46
|
+
};
|
|
47
|
+
const start = await postJson(fetchImpl, descriptor.endpoint, '/v1/start', startIntent);
|
|
48
|
+
const initial = parseBridgeRun(start);
|
|
49
|
+
if (!initial.runId) {
|
|
50
|
+
throw bridgeError(start, initial);
|
|
51
|
+
}
|
|
52
|
+
const output = options.output ?? process.stdout;
|
|
53
|
+
const errorOutput = options.errorOutput ?? process.stderr;
|
|
54
|
+
const signal = options.signal;
|
|
55
|
+
let lastState = '';
|
|
56
|
+
let lastLogSequence = 0;
|
|
57
|
+
let cancelling = false;
|
|
58
|
+
|
|
59
|
+
const cancel = async () => {
|
|
60
|
+
if (cancelling) return;
|
|
61
|
+
cancelling = true;
|
|
62
|
+
try {
|
|
63
|
+
await postJson(fetchImpl, descriptor.endpoint, '/v1/cancel', {
|
|
64
|
+
schemaVersion: 1,
|
|
65
|
+
runId: initial.runId,
|
|
66
|
+
});
|
|
67
|
+
} catch {
|
|
68
|
+
// Desktop owns final process cleanup. Signal handling never invents success.
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const removeSignalHandlers = installSignalHandlers(cancel, options.installSignalHandlers !== false);
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
let current = initial;
|
|
75
|
+
for (;;) {
|
|
76
|
+
printStatusTransition(output, current, lastState);
|
|
77
|
+
if (current.state !== lastState) lastState = current.state;
|
|
78
|
+
lastLogSequence = printNewLogs(output, current, lastLogSequence);
|
|
79
|
+
if (TERMINAL_STATES.has(current.state)) {
|
|
80
|
+
if (current.state === 'stopped' && cancelling) return current;
|
|
81
|
+
throw new DevShellError(
|
|
82
|
+
current.reasonCode || `local-development-${current.state}`,
|
|
83
|
+
current.message || `Nimi local development ended in state ${current.state}.`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
if (signal?.aborted) {
|
|
87
|
+
await cancel();
|
|
88
|
+
return current;
|
|
89
|
+
}
|
|
90
|
+
await delay(STATUS_POLL_MS, signal);
|
|
91
|
+
const response = await postJson(fetchImpl, descriptor.endpoint, '/v1/status', {
|
|
92
|
+
schemaVersion: 1,
|
|
93
|
+
runId: initial.runId,
|
|
94
|
+
});
|
|
95
|
+
current = parseBridgeRun(response);
|
|
96
|
+
}
|
|
97
|
+
} catch (error) {
|
|
98
|
+
if (signal?.aborted) {
|
|
99
|
+
await cancel();
|
|
100
|
+
return initial;
|
|
101
|
+
}
|
|
102
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
103
|
+
errorOutput.write(`[nimi-app dev] ${message}\n`);
|
|
104
|
+
throw error;
|
|
105
|
+
} finally {
|
|
106
|
+
removeSignalHandlers();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function assertLocalDevelopmentPlatform(platform, shell) {
|
|
111
|
+
if (shell !== 'electron') {
|
|
112
|
+
throw new DevShellError(
|
|
113
|
+
'local-development-platform-unsupported',
|
|
114
|
+
'Nimi local development accepts only the Desktop-supervised Electron carrier.',
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
if (platform === 'win32' || platform === 'darwin') return;
|
|
118
|
+
throw new DevShellError(
|
|
119
|
+
'local-development-platform-unsupported',
|
|
120
|
+
'Nimi protected local development is not admitted on this platform.',
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export class DevShellError extends Error {
|
|
125
|
+
constructor(reasonCode, message) {
|
|
126
|
+
super(message);
|
|
127
|
+
this.name = 'DevShellError';
|
|
128
|
+
this.reasonCode = reasonCode;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function canonicalProjectRoot(cwd, requestedDir) {
|
|
133
|
+
const candidate = path.resolve(cwd, String(requestedDir || '.'));
|
|
134
|
+
const canonical = await realpath(candidate).catch(() => null);
|
|
135
|
+
if (!canonical) {
|
|
136
|
+
throw new DevShellError('local-development-project-changed', `Project directory does not exist: ${candidate}`);
|
|
137
|
+
}
|
|
138
|
+
const metadata = await lstat(canonical);
|
|
139
|
+
if (!metadata.isDirectory()) {
|
|
140
|
+
throw new DevShellError('local-development-project-changed', `Project root is not a directory: ${candidate}`);
|
|
141
|
+
}
|
|
142
|
+
return canonical;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function readAppId(projectRoot) {
|
|
146
|
+
let raw;
|
|
147
|
+
try {
|
|
148
|
+
raw = await readFile(path.join(projectRoot, 'nimi.app.yaml'), 'utf8');
|
|
149
|
+
} catch {
|
|
150
|
+
throw new DevShellError('local-development-project-changed', 'nimi.app.yaml is required.');
|
|
151
|
+
}
|
|
152
|
+
let document;
|
|
153
|
+
try {
|
|
154
|
+
document = parseYaml(raw);
|
|
155
|
+
} catch {
|
|
156
|
+
throw new DevShellError('local-development-project-changed', 'nimi.app.yaml is not valid YAML.');
|
|
157
|
+
}
|
|
158
|
+
const appId = typeof document?.app_id === 'string' ? document.app_id : '';
|
|
159
|
+
if (!isExactIdentifier(appId)) {
|
|
160
|
+
throw new DevShellError('local-development-project-changed', 'nimi.app.yaml requires an exact app_id.');
|
|
161
|
+
}
|
|
162
|
+
return appId;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function readPresenceDescriptor(descriptorPath, now) {
|
|
166
|
+
try {
|
|
167
|
+
await assertRegularFileWithoutSymlinkAncestry(descriptorPath);
|
|
168
|
+
const raw = JSON.parse(await readFile(descriptorPath, 'utf8'));
|
|
169
|
+
if (!isPlainObject(raw) || Object.keys(raw).sort().join(',') !== [
|
|
170
|
+
'desktopAppId',
|
|
171
|
+
'desktopPid',
|
|
172
|
+
'endpoint',
|
|
173
|
+
'lastHeartbeatAt',
|
|
174
|
+
'schemaVersion',
|
|
175
|
+
'startedAt',
|
|
176
|
+
].sort().join(',')) {
|
|
177
|
+
throw new Error('shape');
|
|
178
|
+
}
|
|
179
|
+
if (raw.schemaVersion !== 1 || raw.desktopAppId !== 'nimi.desktop') throw new Error('identity');
|
|
180
|
+
if (!Number.isSafeInteger(raw.desktopPid) || raw.desktopPid <= 0) throw new Error('pid');
|
|
181
|
+
const heartbeat = Date.parse(requireText(raw.lastHeartbeatAt));
|
|
182
|
+
if (!Number.isFinite(heartbeat) || now - heartbeat > MAX_HEARTBEAT_AGE_MS || heartbeat - now > 5_000) {
|
|
183
|
+
throw new Error('heartbeat');
|
|
184
|
+
}
|
|
185
|
+
return { endpoint: normalizeLoopbackEndpoint(raw.endpoint) };
|
|
186
|
+
} catch {
|
|
187
|
+
throw new DevShellError(
|
|
188
|
+
'local-development-desktop-not-running',
|
|
189
|
+
'Nimi is not running. Open Nimi, sign in, and run this command again.',
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function assertRegularFileWithoutSymlinkAncestry(filePath) {
|
|
195
|
+
const resolved = path.resolve(filePath);
|
|
196
|
+
const parsed = path.parse(resolved);
|
|
197
|
+
let current = parsed.root;
|
|
198
|
+
for (const segment of path.relative(parsed.root, resolved).split(path.sep)) {
|
|
199
|
+
if (!segment) continue;
|
|
200
|
+
current = path.join(current, segment);
|
|
201
|
+
const metadata = await lstat(current);
|
|
202
|
+
if (metadata.isSymbolicLink()) throw new Error('symlink');
|
|
203
|
+
}
|
|
204
|
+
const metadata = await lstat(resolved);
|
|
205
|
+
if (!metadata.isFile()) throw new Error('not-file');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function postJson(fetchImpl, endpoint, route, body) {
|
|
209
|
+
let response;
|
|
210
|
+
try {
|
|
211
|
+
response = await fetchImpl(`${endpoint}${route}`, {
|
|
212
|
+
method: 'POST',
|
|
213
|
+
headers: { 'Content-Type': 'application/json' },
|
|
214
|
+
body: JSON.stringify(body),
|
|
215
|
+
redirect: 'error',
|
|
216
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
217
|
+
});
|
|
218
|
+
} catch {
|
|
219
|
+
throw new DevShellError(
|
|
220
|
+
'local-development-desktop-not-running',
|
|
221
|
+
'The Nimi development supervisor is unavailable.',
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
if (!response || response.status !== 200) {
|
|
225
|
+
throw new DevShellError('local-development-launcher-unavailable', 'Nimi rejected the development intent transport.');
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
return await response.json();
|
|
229
|
+
} catch {
|
|
230
|
+
throw new DevShellError('local-development-launcher-unavailable', 'Nimi returned an invalid development response.');
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function parseBridgeRun(response) {
|
|
235
|
+
if (!isPlainObject(response)) throw new DevShellError('local-development-launcher-unavailable', 'Invalid Desktop response.');
|
|
236
|
+
if (response.status === 'error') {
|
|
237
|
+
throw bridgeError(response, null);
|
|
238
|
+
}
|
|
239
|
+
const run = response.run;
|
|
240
|
+
if (response.status !== 'ok' || !isPlainObject(run) || run.schemaVersion !== 1) {
|
|
241
|
+
throw new DevShellError('local-development-launcher-unavailable', 'Invalid Desktop run status.');
|
|
242
|
+
}
|
|
243
|
+
const expectedKeys = [
|
|
244
|
+
'appId',
|
|
245
|
+
'canonicalProjectRoot',
|
|
246
|
+
...(run.cdpPort === undefined ? [] : ['cdpPort']),
|
|
247
|
+
'displayName',
|
|
248
|
+
'hostGeneration',
|
|
249
|
+
'logSequence',
|
|
250
|
+
'logs',
|
|
251
|
+
'message',
|
|
252
|
+
'rendererOrigin',
|
|
253
|
+
'retryable',
|
|
254
|
+
'runId',
|
|
255
|
+
'schemaVersion',
|
|
256
|
+
'shell',
|
|
257
|
+
'state',
|
|
258
|
+
...(run.reasonCode === undefined ? [] : ['reasonCode']),
|
|
259
|
+
].sort();
|
|
260
|
+
if (Object.keys(run).sort().join(',') !== expectedKeys.join(',')) {
|
|
261
|
+
throw new DevShellError(
|
|
262
|
+
'local-development-launcher-unavailable',
|
|
263
|
+
'Desktop run status contains forbidden fields.',
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
const state = requireText(run.state);
|
|
267
|
+
const runId = requireText(run.runId);
|
|
268
|
+
if (!isSelector(runId, 'dev-run') || !isExactIdentifier(run.appId) || run.shell !== 'electron') {
|
|
269
|
+
throw new DevShellError('local-development-launcher-unavailable', 'Desktop run identity validation failed.');
|
|
270
|
+
}
|
|
271
|
+
const logs = Array.isArray(run.logs)
|
|
272
|
+
? run.logs.map((entry) => parseLog(entry)).filter(Boolean)
|
|
273
|
+
: [];
|
|
274
|
+
return {
|
|
275
|
+
state,
|
|
276
|
+
runId,
|
|
277
|
+
appId: run.appId,
|
|
278
|
+
shell: run.shell,
|
|
279
|
+
message: requireText(run.message),
|
|
280
|
+
reasonCode: typeof run.reasonCode === 'string' ? run.reasonCode : '',
|
|
281
|
+
hostGeneration: Number.isSafeInteger(run.hostGeneration) ? run.hostGeneration : 0,
|
|
282
|
+
cdpPort: run.cdpPort === undefined ? undefined : reportedCdpPort(run.cdpPort),
|
|
283
|
+
logs,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function parseLog(value) {
|
|
288
|
+
if (!isPlainObject(value) || !Number.isSafeInteger(value.sequence) || value.sequence <= 0) return null;
|
|
289
|
+
if (typeof value.stream !== 'string' || typeof value.message !== 'string') return null;
|
|
290
|
+
return { sequence: value.sequence, stream: value.stream, message: value.message };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function bridgeError(response, run) {
|
|
294
|
+
const reasonCode = typeof response?.reasonCode === 'string'
|
|
295
|
+
? response.reasonCode
|
|
296
|
+
: run?.reasonCode || 'local-development-launcher-unavailable';
|
|
297
|
+
return new DevShellError(reasonCode, `Nimi rejected local development (${reasonCode}).`);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function printStatusTransition(output, status, previousState) {
|
|
301
|
+
if (status.state === previousState) return;
|
|
302
|
+
const generation = status.hostGeneration > 0 ? ` · host ${status.hostGeneration}` : '';
|
|
303
|
+
const cdp = status.cdpPort === undefined ? '' : ` · CDP http://127.0.0.1:${status.cdpPort}`;
|
|
304
|
+
output.write(`[nimi-app dev] ${status.state}${generation}${cdp}: ${status.message}\n`);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function printNewLogs(output, status, previousSequence) {
|
|
308
|
+
let sequence = previousSequence;
|
|
309
|
+
for (const entry of status.logs) {
|
|
310
|
+
if (entry.sequence <= previousSequence) continue;
|
|
311
|
+
sequence = Math.max(sequence, entry.sequence);
|
|
312
|
+
if (entry.message) output.write(`[${entry.stream}] ${entry.message}\n`);
|
|
313
|
+
}
|
|
314
|
+
return sequence;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function installSignalHandlers(cancel, enabled) {
|
|
318
|
+
if (!enabled) return () => {};
|
|
319
|
+
const handlers = new Map();
|
|
320
|
+
for (const signal of ['SIGINT', 'SIGTERM']) {
|
|
321
|
+
const handler = () => { void cancel(); };
|
|
322
|
+
handlers.set(signal, handler);
|
|
323
|
+
process.on(signal, handler);
|
|
324
|
+
}
|
|
325
|
+
return () => {
|
|
326
|
+
for (const [signal, handler] of handlers) process.off(signal, handler);
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function normalizeShell(value) {
|
|
331
|
+
if (value !== 'electron') {
|
|
332
|
+
throw new DevShellError(
|
|
333
|
+
'local-development-platform-unsupported',
|
|
334
|
+
'--shell must be electron; Tauri is not an admitted local-development carrier.',
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
return value;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function normalizeCdpPort(value) {
|
|
341
|
+
const raw = typeof value === 'number' ? String(value) : value;
|
|
342
|
+
if (typeof raw !== 'string'
|
|
343
|
+
|| raw.trim() !== raw
|
|
344
|
+
|| !/^(?:0|[1-9][0-9]*)$/u.test(raw)) {
|
|
345
|
+
throw new DevShellError(
|
|
346
|
+
'local-development-cdp-port-invalid',
|
|
347
|
+
'--cdp-port must be a canonical decimal integer from 1024 through 65535.',
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
const port = Number(raw);
|
|
351
|
+
if (!Number.isSafeInteger(port) || port < 1024 || port > 65535) {
|
|
352
|
+
throw new DevShellError(
|
|
353
|
+
'local-development-cdp-port-invalid',
|
|
354
|
+
'--cdp-port must be a canonical decimal integer from 1024 through 65535.',
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
return port;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
async function resolveRequestedCdpPort(projectRoot, options) {
|
|
361
|
+
if (options.noCdp) {
|
|
362
|
+
if (options.cdpPort !== undefined && options.cdpPort !== null && options.cdpPort !== '') {
|
|
363
|
+
throw new DevShellError(
|
|
364
|
+
'local-development-cdp-configuration-conflict',
|
|
365
|
+
'--cdp-port and --no-cdp cannot be combined.',
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
return undefined;
|
|
369
|
+
}
|
|
370
|
+
if (options.cdpPort !== undefined && options.cdpPort !== null && options.cdpPort !== '') {
|
|
371
|
+
return normalizeCdpPort(options.cdpPort);
|
|
372
|
+
}
|
|
373
|
+
const configured = await readProjectCdpPort(projectRoot);
|
|
374
|
+
return configured === undefined ? AUTO_CDP_PORT : normalizeCdpPort(configured);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
async function readProjectCdpPort(projectRoot) {
|
|
378
|
+
let raw;
|
|
379
|
+
try {
|
|
380
|
+
raw = await readFile(path.join(projectRoot, '.env'), 'utf8');
|
|
381
|
+
} catch (error) {
|
|
382
|
+
if (error?.code === 'ENOENT') return undefined;
|
|
383
|
+
throw new DevShellError(
|
|
384
|
+
'local-development-cdp-port-invalid',
|
|
385
|
+
`Unable to read ${CDP_PORT_ENV_KEY} from the project .env.`,
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
let declarations;
|
|
389
|
+
try {
|
|
390
|
+
declarations = parseEnv(raw);
|
|
391
|
+
} catch {
|
|
392
|
+
throw new DevShellError(
|
|
393
|
+
'local-development-cdp-port-invalid',
|
|
394
|
+
`The project .env is invalid while reading ${CDP_PORT_ENV_KEY}.`,
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
return declarations[CDP_PORT_ENV_KEY];
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function reportedCdpPort(value) {
|
|
401
|
+
if (!Number.isSafeInteger(value) || value < 1024 || value > 65535) {
|
|
402
|
+
throw new DevShellError(
|
|
403
|
+
'local-development-launcher-unavailable',
|
|
404
|
+
'Desktop returned an invalid Electron CDP port.',
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
return value;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function normalizeLoopbackEndpoint(value) {
|
|
411
|
+
const parsed = new URL(requireText(value));
|
|
412
|
+
const hostname = parsed.hostname.toLowerCase();
|
|
413
|
+
if (
|
|
414
|
+
parsed.protocol !== 'http:'
|
|
415
|
+
|| !['127.0.0.1', '[::1]', '::1'].includes(hostname)
|
|
416
|
+
|| !parsed.port
|
|
417
|
+
|| parsed.username
|
|
418
|
+
|| parsed.password
|
|
419
|
+
|| parsed.search
|
|
420
|
+
|| parsed.hash
|
|
421
|
+
|| (parsed.pathname !== '/' && parsed.pathname !== '')
|
|
422
|
+
) {
|
|
423
|
+
throw new Error('endpoint');
|
|
424
|
+
}
|
|
425
|
+
return parsed.origin;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function isExactIdentifier(value) {
|
|
429
|
+
return typeof value === 'string'
|
|
430
|
+
&& value.length > 0
|
|
431
|
+
&& value.length <= 160
|
|
432
|
+
&& /^[a-zA-Z0-9](?:[a-zA-Z0-9._-]*[a-zA-Z0-9])?$/.test(value);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function isSelector(value, prefix) {
|
|
436
|
+
return typeof value === 'string'
|
|
437
|
+
&& value.startsWith(`${prefix}-`)
|
|
438
|
+
&& value.length <= 160
|
|
439
|
+
&& /^[a-zA-Z0-9_-]+$/.test(value);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function requireText(value) {
|
|
443
|
+
if (typeof value !== 'string' || !value || value.trim() !== value) throw new Error('text');
|
|
444
|
+
return value;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function isPlainObject(value) {
|
|
448
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function delay(ms, signal) {
|
|
452
|
+
if (!signal) return new Promise((resolve) => setTimeout(resolve, ms));
|
|
453
|
+
return new Promise((resolve, reject) => {
|
|
454
|
+
if (signal.aborted) return resolve();
|
|
455
|
+
const timer = setTimeout(() => {
|
|
456
|
+
signal.removeEventListener('abort', onAbort);
|
|
457
|
+
resolve();
|
|
458
|
+
}, ms);
|
|
459
|
+
const onAbort = () => {
|
|
460
|
+
clearTimeout(timer);
|
|
461
|
+
resolve();
|
|
462
|
+
};
|
|
463
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
464
|
+
});
|
|
465
|
+
}
|