mirror of
https://github.com/makecindy/cindy.git
synced 2026-12-03 22:50:26 +08:00
Cindy 是心动网络 (X.D. Network Inc.) 的跨端 AI Agent 客户端。 本次提交包含 Desktop(Electron)、Mobile(Expo)及全部共享 packages 的完整源码,以 Apache-2.0 协议开源。 This is the first public release of Cindy, X.D. Network's cross-platform AI agent client: the Electron desktop app, the Expo mobile app, and all shared workspace packages, licensed under Apache-2.0. - 许可与合规:根 LICENSE(Apache-2.0)+ NOTICE;第三方组件归属声明与 SPDX SBOM 收口在 docs/legal/notices/,由 pnpm licenses:generate 生成。 - 社区文件:README / CONTRIBUTING / CODE_OF_CONDUCT / SECURITY / SUPPORT 中英双语;issue / PR 模板与 CI workflow 在 .github/。 - 协议层以公开 submodule 引入:github.com/makecindy/cindy-protocol。 - 提交前已通过仓库门禁:全量 pnpm test:unit 与涉及 package 的 typecheck。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
81 lines
2.8 KiB
JavaScript
81 lines
2.8 KiB
JavaScript
import { spawnSync } from 'node:child_process';
|
|
import { existsSync } from 'node:fs';
|
|
import { delimiter, join } from 'node:path';
|
|
|
|
const MIN_JAVA_MAJOR = 17;
|
|
|
|
export function resolveJavaRuntimeEnv(baseEnv = process.env) {
|
|
const current = javaMajor(versionForJavaCommand('java', baseEnv));
|
|
if (current >= MIN_JAVA_MAJOR) return { ...baseEnv };
|
|
|
|
for (const javaHome of javaHomeCandidates(baseEnv)) {
|
|
if (!javaHome) continue;
|
|
const javaBin = join(javaHome, 'bin', process.platform === 'win32' ? 'java.exe' : 'java');
|
|
if (!existsSync(javaBin)) continue;
|
|
const major = javaMajor(versionForJavaCommand(javaBin, { ...baseEnv, JAVA_HOME: javaHome }));
|
|
if (major < MIN_JAVA_MAJOR) continue;
|
|
return {
|
|
...baseEnv,
|
|
JAVA_HOME: javaHome,
|
|
PATH: [join(javaHome, 'bin'), baseEnv.PATH].filter(Boolean).join(delimiter),
|
|
};
|
|
}
|
|
|
|
return { ...baseEnv };
|
|
}
|
|
|
|
export function javaRuntimeDetail(env = resolveJavaRuntimeEnv()) {
|
|
const version = versionForJavaCommand('java', env);
|
|
const major = javaMajor(version);
|
|
if (!Number.isFinite(major)) return 'java version unavailable';
|
|
return `Java ${major}${env.JAVA_HOME ? ` at ${env.JAVA_HOME}` : ''}`;
|
|
}
|
|
|
|
function javaHomeCandidates(env) {
|
|
return [
|
|
env.JAVA_HOME,
|
|
macJavaHome('17'),
|
|
homebrewOpenJdk17Home('/opt/homebrew/opt/openjdk@17'),
|
|
homebrewOpenJdk17Home('/usr/local/opt/openjdk@17'),
|
|
brewPrefixOpenJdk17Home(),
|
|
].filter(uniqueTruthy);
|
|
}
|
|
|
|
function macJavaHome(version) {
|
|
if (process.platform !== 'darwin') return null;
|
|
const result = spawnSync('/usr/libexec/java_home', ['-v', version], { encoding: 'utf8' });
|
|
if (result.status !== 0) return null;
|
|
return result.stdout.trim() || null;
|
|
}
|
|
|
|
function homebrewOpenJdk17Home(prefix) {
|
|
const bundledHome = join(prefix, 'libexec', 'openjdk.jdk', 'Contents', 'Home');
|
|
if (existsSync(join(bundledHome, 'bin', 'java'))) return bundledHome;
|
|
if (existsSync(join(prefix, 'bin', 'java'))) return prefix;
|
|
return null;
|
|
}
|
|
|
|
function brewPrefixOpenJdk17Home() {
|
|
const result = spawnSync('brew', ['--prefix', 'openjdk@17'], { encoding: 'utf8' });
|
|
if (result.status !== 0) return null;
|
|
return homebrewOpenJdk17Home(result.stdout.trim());
|
|
}
|
|
|
|
function versionForJavaCommand(command, env) {
|
|
const result = spawnSync(command, ['-version'], { encoding: 'utf8', env });
|
|
if (result.error || result.status !== 0) return '';
|
|
return `${result.stderr}\n${result.stdout}`;
|
|
}
|
|
|
|
function javaMajor(versionOutput) {
|
|
const match = versionOutput.match(/version "(?<version>\d+)(?:\.(?<minor>\d+))?/);
|
|
if (!match?.groups) return Number.NaN;
|
|
const major = Number.parseInt(match.groups.version, 10);
|
|
if (major === 1 && match.groups.minor) return Number.parseInt(match.groups.minor, 10);
|
|
return major;
|
|
}
|
|
|
|
function uniqueTruthy(value, index, array) {
|
|
return !!value && array.indexOf(value) === index;
|
|
}
|