@lnyocly/ai4j 0.0.0-stage → 0.1.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/README.md +19 -2
- package/bin/ai4j.mjs +85 -0
- package/package.json +37 -4
- package/scripts/postinstall.mjs +112 -0
package/README.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@lnyocly/ai4j`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
AI4J CLI & TUI — Java AI Agent Terminal Workbench.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g @lnyocly/ai4j
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
ai4j # Launch interactive AI4J TUI workbench
|
|
15
|
+
ai4j code --prompt "..." # Run coding agent task
|
|
16
|
+
ai4j serve # Start shared local HTTP/SSE daemon
|
|
17
|
+
ai4j doctor # Check JVM, providers, MCP & environment
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Downloads the SHA-256 verified `ai4j-cli.jar` runtime directly from [LnYo-Cly/ai4j Releases](https://github.com/LnYo-Cly/ai4j/releases). Automatically activates JDK 25 LTS optimizations (Virtual Threads, Compact Object Headers, Leyden AOT Cache) when running on JDK 25.
|
package/bin/ai4j.mjs
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
3
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import { join, dirname } from 'node:path';
|
|
6
|
+
|
|
7
|
+
const installDir = process.env.AI4J_INSTALL_DIR || join(homedir(), '.ai4j');
|
|
8
|
+
const runtimeDir = join(installDir, 'runtime');
|
|
9
|
+
const binDir = join(installDir, 'bin');
|
|
10
|
+
const cliJar = process.env.AI4J_CLI_JAR || join(runtimeDir, 'ai4j-cli.jar');
|
|
11
|
+
const tuiBin = process.env.AI4J_TUI_BIN || join(binDir, process.platform === 'win32' ? 'ai4j-tui.exe' : 'ai4j-tui');
|
|
12
|
+
|
|
13
|
+
function resolveJavaBin() {
|
|
14
|
+
const exe = process.platform === 'win32' ? 'java.exe' : 'java';
|
|
15
|
+
const portable = join(runtimeDir, 'jre', 'bin', exe);
|
|
16
|
+
if (existsSync(portable)) return portable;
|
|
17
|
+
if (process.env.JAVA_HOME) {
|
|
18
|
+
const fromEnv = join(process.env.JAVA_HOME, 'bin', exe);
|
|
19
|
+
if (existsSync(fromEnv)) return fromEnv;
|
|
20
|
+
}
|
|
21
|
+
return exe;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function detectJavaMajor(javaBin) {
|
|
25
|
+
try {
|
|
26
|
+
const releaseFile = join(dirname(dirname(javaBin)), 'release');
|
|
27
|
+
if (existsSync(releaseFile)) {
|
|
28
|
+
const m = readFileSync(releaseFile, 'utf8').match(/^JAVA_VERSION="([^"]+)"/m);
|
|
29
|
+
if (m) {
|
|
30
|
+
const v = m[1];
|
|
31
|
+
if (v.startsWith('1.')) return parseInt(v.split('.')[1], 10) || 0;
|
|
32
|
+
return parseInt(v.split('.')[0], 10) || 0;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
} catch {
|
|
36
|
+
// Fall back to -version
|
|
37
|
+
}
|
|
38
|
+
const probe = spawnSync(javaBin, ['-version'], { encoding: 'utf8' });
|
|
39
|
+
const text = `${probe.stdout || ''}\n${probe.stderr || ''}`;
|
|
40
|
+
const m = text.match(/"(1\.)?(\d+)/);
|
|
41
|
+
return m ? parseInt(m[2], 10) : 0;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function run() {
|
|
45
|
+
const args = process.argv.slice(2);
|
|
46
|
+
const wantsTui = args.length === 0 || args[0] === 'tui';
|
|
47
|
+
|
|
48
|
+
if (wantsTui && process.env.AI4J_TUI_BIN && existsSync(tuiBin)) {
|
|
49
|
+
const tuiArgs = args[0] === 'tui' ? args.slice(1) : args;
|
|
50
|
+
const child = spawn(tuiBin, tuiArgs, {
|
|
51
|
+
stdio: 'inherit',
|
|
52
|
+
env: { ...process.env, AI4J_CLI_JAR: cliJar },
|
|
53
|
+
});
|
|
54
|
+
child.on('exit', (code) => process.exit(code ?? 0));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!existsSync(cliJar)) {
|
|
59
|
+
console.error(`[ai4j] Missing ${cliJar}. Re-run \`npm i -g @lnyocly/ai4j\` or download from https://github.com/LnYo-Cly/ai4j/releases`);
|
|
60
|
+
process.exit(1);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const javaBin = resolveJavaBin();
|
|
64
|
+
const major = detectJavaMajor(javaBin);
|
|
65
|
+
const jvmArgs = ['-Dfile.encoding=UTF-8'];
|
|
66
|
+
if (major >= 25) {
|
|
67
|
+
jvmArgs.push('-XX:+UseCompactObjectHeaders', '-XX:G1PeriodicGCInterval=60000', '--enable-native-access=ALL-UNNAMED');
|
|
68
|
+
const aotCache = join(runtimeDir, 'ai4j-cli.aot');
|
|
69
|
+
if (existsSync(aotCache)) jvmArgs.push(`-XX:AOTCache=${aotCache}`);
|
|
70
|
+
}
|
|
71
|
+
if (process.env.AI4J_JVM_OPTS) {
|
|
72
|
+
jvmArgs.push(...process.env.AI4J_JVM_OPTS.trim().split(/\s+/));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const cliArgs = args.length === 0 ? ['tui'] : args;
|
|
76
|
+
const child = spawn(javaBin, [...jvmArgs, '-jar', cliJar, ...cliArgs], { stdio: 'inherit' });
|
|
77
|
+
child.on('error', (err) => {
|
|
78
|
+
console.error(`[ai4j] Failed to start Java (${javaBin}): ${err.message}`);
|
|
79
|
+
console.error('[ai4j] Install Java 8+ (JDK 25 LTS recommended) or run the zero-dependency installer from https://github.com/LnYo-Cly/ai4j');
|
|
80
|
+
process.exit(1);
|
|
81
|
+
});
|
|
82
|
+
child.on('exit', (code) => process.exit(code ?? 0));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
run();
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lnyocly/ai4j",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "AI4J CLI & TUI — Java AI Agent Terminal Workbench (thin launcher powered by GitHub Releases & JDK 25 LTS)",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"ai4j": "bin/ai4j.mjs"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"bin/",
|
|
12
|
+
"scripts/",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"postinstall": "node scripts/postinstall.mjs"
|
|
17
|
+
},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=18.0.0"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/LnYo-Cly/ai4j.git",
|
|
27
|
+
"directory": "packages/ai4j-cli-npm"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/LnYo-Cly/ai4j",
|
|
30
|
+
"keywords": [
|
|
31
|
+
"ai4j",
|
|
32
|
+
"ai-agent",
|
|
33
|
+
"coding-agent",
|
|
34
|
+
"cli",
|
|
35
|
+
"tui",
|
|
36
|
+
"java",
|
|
37
|
+
"mcp"
|
|
38
|
+
]
|
|
39
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { createWriteStream, existsSync, mkdirSync, readFileSync, renameSync, unlinkSync } from 'node:fs';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import { join, dirname } from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { Readable } from 'node:stream';
|
|
8
|
+
import { pipeline } from 'node:stream/promises';
|
|
9
|
+
|
|
10
|
+
if (process.env.AI4J_SKIP_POSTINSTALL === '1') {
|
|
11
|
+
console.log('[ai4j] Skipping postinstall download (AI4J_SKIP_POSTINSTALL=1).');
|
|
12
|
+
process.exit(0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
const pkg = JSON.parse(readFileSync(join(__dirname, '..', 'package.json'), 'utf8'));
|
|
17
|
+
const repo = process.env.AI4J_RELEASE_REPO || 'LnYo-Cly/ai4j';
|
|
18
|
+
const installDir = process.env.AI4J_INSTALL_DIR || join(homedir(), '.ai4j');
|
|
19
|
+
const runtimeDir = join(installDir, 'runtime');
|
|
20
|
+
|
|
21
|
+
async function fetchJson(url) {
|
|
22
|
+
const res = await fetch(url, {
|
|
23
|
+
headers: { 'User-Agent': `ai4j-npm/${pkg.version}`, Accept: 'application/vnd.github+json' },
|
|
24
|
+
});
|
|
25
|
+
if (!res.ok) throw new Error(`HTTP ${res.status} for ${url}`);
|
|
26
|
+
return res.json();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function resolveBaseUrl() {
|
|
30
|
+
const pinned = process.env.AI4J_VERSION;
|
|
31
|
+
if (pinned && pinned !== 'latest') {
|
|
32
|
+
return `https://github.com/${repo}/releases/download/${pinned}`;
|
|
33
|
+
}
|
|
34
|
+
const exactTag = `studio-v${pkg.version}`;
|
|
35
|
+
try {
|
|
36
|
+
const releases = await fetchJson(`https://api.github.com/repos/${repo}/releases?per_page=15`);
|
|
37
|
+
const match =
|
|
38
|
+
releases.find((r) => !r.draft && r.tag_name === exactTag) ||
|
|
39
|
+
releases.find((r) => !r.draft && typeof r.tag_name === 'string' && r.tag_name.startsWith('studio-v'));
|
|
40
|
+
if (match?.tag_name) {
|
|
41
|
+
return `https://github.com/${repo}/releases/download/${match.tag_name}`;
|
|
42
|
+
}
|
|
43
|
+
} catch {
|
|
44
|
+
// Fall back directly to exact tag URL
|
|
45
|
+
}
|
|
46
|
+
return `https://github.com/${repo}/releases/download/${exactTag}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function downloadFile(url, dest) {
|
|
50
|
+
const res = await fetch(url, { headers: { 'User-Agent': `ai4j-npm/${pkg.version}` } });
|
|
51
|
+
if (!res.ok || !res.body) throw new Error(`HTTP ${res.status} downloading ${url}`);
|
|
52
|
+
const tmp = `${dest}.tmp-${Date.now()}`;
|
|
53
|
+
await pipeline(Readable.fromWeb(res.body), createWriteStream(tmp));
|
|
54
|
+
renameSync(tmp, dest);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function sha256File(filePath) {
|
|
58
|
+
return createHash('sha256').update(readFileSync(filePath)).digest('hex');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function parseChecksums(text) {
|
|
62
|
+
const map = new Map();
|
|
63
|
+
for (const line of text.split(/\r?\n/)) {
|
|
64
|
+
const m = line.trim().match(/^([0-9a-fA-F]{64})\s+\*?(.+)$/);
|
|
65
|
+
if (m) map.set(m[2].trim(), m[1].toLowerCase());
|
|
66
|
+
}
|
|
67
|
+
return map;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function verifyAndDownload(baseUrl, assetName, destPath, checksums) {
|
|
71
|
+
if (existsSync(destPath) && checksums.has(assetName)) {
|
|
72
|
+
if (sha256File(destPath) === checksums.get(assetName)) {
|
|
73
|
+
console.log(`[ai4j] Reusing verified ${assetName}`);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
console.log(`[ai4j] Downloading ${assetName}...`);
|
|
78
|
+
await downloadFile(`${baseUrl}/${encodeURIComponent(assetName)}`, destPath);
|
|
79
|
+
const expected = checksums.get(assetName);
|
|
80
|
+
if (expected) {
|
|
81
|
+
const actual = sha256File(destPath);
|
|
82
|
+
if (actual !== expected) {
|
|
83
|
+
try { unlinkSync(destPath); } catch {}
|
|
84
|
+
throw new Error(`SHA-256 mismatch for ${assetName}: expected ${expected}, got ${actual}`);
|
|
85
|
+
}
|
|
86
|
+
console.log(`[ai4j] Verified SHA-256 for ${assetName}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function main() {
|
|
91
|
+
mkdirSync(runtimeDir, { recursive: true });
|
|
92
|
+
|
|
93
|
+
const baseUrl = await resolveBaseUrl();
|
|
94
|
+
let checksums = new Map();
|
|
95
|
+
try {
|
|
96
|
+
const res = await fetch(`${baseUrl}/SHA256SUMS.txt`, {
|
|
97
|
+
headers: { 'User-Agent': `ai4j-npm/${pkg.version}` },
|
|
98
|
+
});
|
|
99
|
+
if (res.ok) checksums = parseChecksums(await res.text());
|
|
100
|
+
} catch {
|
|
101
|
+
// Continue without checksums if manifest unavailable
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
await verifyAndDownload(baseUrl, 'ai4j-cli.jar', join(runtimeDir, 'ai4j-cli.jar'), checksums);
|
|
105
|
+
|
|
106
|
+
console.log('[ai4j] Installation ready. Run `ai4j` or `ai4j doctor` to get started.');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
main().catch((err) => {
|
|
110
|
+
console.warn(`[ai4j] Postinstall download deferred: ${err.message}`);
|
|
111
|
+
console.warn('[ai4j] You can retry later or run the platform installer from https://github.com/LnYo-Cly/ai4j/releases');
|
|
112
|
+
});
|