@yancyyu/openhermit 1.6.15 → 1.6.17
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/bin/hermit.mjs +74 -27
- package/package.json +1 -1
package/bin/hermit.mjs
CHANGED
|
@@ -90,6 +90,7 @@ const daemonRequested = args.includes('--daemon');
|
|
|
90
90
|
const daemonChild = process.env.HERMIT_DAEMON_CHILD === '1';
|
|
91
91
|
const daemonPidPath = path.join(hermitHome, 'openhermit.pid');
|
|
92
92
|
const daemonLogPath = path.join(hermitHome, 'logs', 'openhermit.log');
|
|
93
|
+
const runtimeLogPath = path.join(hermitHome, 'logs', 'openhermit-runtime.log');
|
|
93
94
|
const serverLogPath = path.join(hermitHome, 'logs', 'openhermit-server.log');
|
|
94
95
|
const ccConnectConfigPath =
|
|
95
96
|
process.env.HERMIT_CC_CONNECT_CONFIG ||
|
|
@@ -493,13 +494,61 @@ async function waitForCcConnect(baseUrl, token, timeoutMs = 15_000) {
|
|
|
493
494
|
return false;
|
|
494
495
|
}
|
|
495
496
|
|
|
497
|
+
function appendLog(filePath, chunk) {
|
|
498
|
+
try {
|
|
499
|
+
mkdirSync(path.dirname(filePath), { recursive: true });
|
|
500
|
+
appendFileSync(filePath, chunk);
|
|
501
|
+
} catch {
|
|
502
|
+
// Logging must never block startup.
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function printLogTail(label, filePath) {
|
|
507
|
+
try {
|
|
508
|
+
const content = readFileSync(filePath, 'utf-8');
|
|
509
|
+
const lines = content.trimEnd().split(/\r?\n/).slice(-80);
|
|
510
|
+
console.error(`[openHermit] ${label} log: ${filePath}`);
|
|
511
|
+
if (lines.length > 0) {
|
|
512
|
+
console.error(`[openHermit] Last ${label} log lines:`);
|
|
513
|
+
console.error(lines.join('\n'));
|
|
514
|
+
}
|
|
515
|
+
} catch {
|
|
516
|
+
console.error(`[openHermit] ${label} log: ${filePath}`);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
async function waitForRuntimeReady(baseUrl, token, child, timeoutMs = 30_000) {
|
|
521
|
+
let exitCode = null;
|
|
522
|
+
child.once('exit', (code) => {
|
|
523
|
+
exitCode = code ?? 1;
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
const startedAt = Date.now();
|
|
527
|
+
while (Date.now() - startedAt < timeoutMs) {
|
|
528
|
+
if (exitCode !== null) {
|
|
529
|
+
throw new Error(`Runtime service exited before becoming ready (code ${exitCode})`);
|
|
530
|
+
}
|
|
531
|
+
try {
|
|
532
|
+
const res = await fetch(`${baseUrl}/api/v1/status`, {
|
|
533
|
+
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
|
534
|
+
});
|
|
535
|
+
if (res.ok) return;
|
|
536
|
+
} catch {
|
|
537
|
+
// keep polling
|
|
538
|
+
}
|
|
539
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
throw new Error(`Runtime service did not become ready within ${Math.round(timeoutMs / 1000)}s`);
|
|
543
|
+
}
|
|
544
|
+
|
|
496
545
|
function resolveCcConnectRunner() {
|
|
497
546
|
const pkgPath = require.resolve('cc-connect/package.json');
|
|
498
547
|
return path.join(path.dirname(pkgPath), 'run.js');
|
|
499
548
|
}
|
|
500
549
|
|
|
501
550
|
function resolveTsxLoader() {
|
|
502
|
-
return require.resolve('tsx');
|
|
551
|
+
return pathToFileURL(require.resolve('tsx')).href;
|
|
503
552
|
}
|
|
504
553
|
|
|
505
554
|
function resolveAliasLoaderRegister() {
|
|
@@ -531,11 +580,28 @@ if (!skipCcConnect) {
|
|
|
531
580
|
CC_CONNECT_MANAGEMENT_TOKEN: ccTokens.managementToken,
|
|
532
581
|
CC_CONNECT_BRIDGE_TOKEN: ccTokens.bridgeToken,
|
|
533
582
|
},
|
|
534
|
-
stdio: '
|
|
583
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
535
584
|
});
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
585
|
+
|
|
586
|
+
ccConnectProcess.stdout?.on('data', (chunk) => {
|
|
587
|
+
process.stdout.write(chunk);
|
|
588
|
+
appendLog(runtimeLogPath, chunk);
|
|
589
|
+
});
|
|
590
|
+
ccConnectProcess.stderr?.on('data', (chunk) => {
|
|
591
|
+
process.stderr.write(chunk);
|
|
592
|
+
appendLog(runtimeLogPath, chunk);
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
try {
|
|
596
|
+
await waitForRuntimeReady(ccBaseUrl, ccTokens.managementToken, ccConnectProcess, 30_000);
|
|
597
|
+
} catch (err) {
|
|
598
|
+
console.error(
|
|
599
|
+
`[openHermit] Runtime service failed to start: ${err instanceof Error ? err.message : String(err)}`
|
|
600
|
+
);
|
|
601
|
+
printLogTail('Runtime', runtimeLogPath);
|
|
602
|
+
signalDaemon(ccConnectProcess.pid, 'SIGTERM');
|
|
603
|
+
setTimeout(() => signalDaemon(ccConnectProcess?.pid, 'SIGKILL'), 2_000).unref();
|
|
604
|
+
process.exit(1);
|
|
539
605
|
}
|
|
540
606
|
}
|
|
541
607
|
}
|
|
@@ -577,27 +643,8 @@ if (!existsSync(distRenderererDir) || !existsSync(path.join(distRenderererDir, '
|
|
|
577
643
|
// Start the server
|
|
578
644
|
console.log('[openHermit] Launching server...\n');
|
|
579
645
|
|
|
580
|
-
function appendServerLog(chunk) {
|
|
581
|
-
try {
|
|
582
|
-
mkdirSync(path.dirname(serverLogPath), { recursive: true });
|
|
583
|
-
appendFileSync(serverLogPath, chunk);
|
|
584
|
-
} catch {
|
|
585
|
-
// Logging must never block startup.
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
|
|
589
646
|
function printServerLogTail() {
|
|
590
|
-
|
|
591
|
-
const content = readFileSync(serverLogPath, 'utf-8');
|
|
592
|
-
const lines = content.trimEnd().split(/\r?\n/).slice(-60);
|
|
593
|
-
if (lines.length > 0) {
|
|
594
|
-
console.error(`[openHermit] Server log: ${serverLogPath}`);
|
|
595
|
-
console.error('[openHermit] Last server log lines:');
|
|
596
|
-
console.error(lines.join('\n'));
|
|
597
|
-
}
|
|
598
|
-
} catch {
|
|
599
|
-
console.error(`[openHermit] Server log: ${serverLogPath}`);
|
|
600
|
-
}
|
|
647
|
+
printLogTail('Server', serverLogPath);
|
|
601
648
|
}
|
|
602
649
|
|
|
603
650
|
const serverProcess = spawn(process.execPath, ['--import', resolveAliasLoaderRegister(), '--import', resolveTsxLoader(), 'src/main/server.ts'], {
|
|
@@ -619,12 +666,12 @@ const serverProcess = spawn(process.execPath, ['--import', resolveAliasLoaderReg
|
|
|
619
666
|
|
|
620
667
|
serverProcess.stdout?.on('data', (chunk) => {
|
|
621
668
|
process.stdout.write(chunk);
|
|
622
|
-
|
|
669
|
+
appendLog(serverLogPath, chunk);
|
|
623
670
|
});
|
|
624
671
|
|
|
625
672
|
serverProcess.stderr?.on('data', (chunk) => {
|
|
626
673
|
process.stderr.write(chunk);
|
|
627
|
-
|
|
674
|
+
appendLog(serverLogPath, chunk);
|
|
628
675
|
});
|
|
629
676
|
|
|
630
677
|
serverProcess.on('exit', (code) => {
|