Release v1.01 Enhanced: Vi Control, TUI Gen5, Core Stability
This commit is contained in:
@@ -1,159 +1,119 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* OpenQode Auth Check
|
||||
* Runs qwen auth if not authenticated. Shows URL for manual auth.
|
||||
* Centralized auth for all tools (TUI, Smart Repair, etc.)
|
||||
* OpenQode Auth Check (Centralized)
|
||||
*
|
||||
* Goal: Make Gen5 TUI + Goose use the SAME auth as Qwen CLI (option [5]).
|
||||
* This script intentionally does NOT run the legacy `bin/auth.js` flow.
|
||||
*
|
||||
* Exit codes:
|
||||
* - 0: Qwen CLI present + OAuth creds present
|
||||
* - 1: Qwen CLI missing
|
||||
* - 2: Qwen CLI present but not authenticated
|
||||
*/
|
||||
|
||||
import { spawn } from 'child_process';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const argv = process.argv.slice(2);
|
||||
const quiet = argv.includes('--quiet') || argv.includes('-q');
|
||||
|
||||
const C = {
|
||||
reset: '\x1b[0m',
|
||||
cyan: '\x1b[36m',
|
||||
green: '\x1b[32m',
|
||||
yellow: '\x1b[33m',
|
||||
red: '\x1b[31m',
|
||||
dim: '\x1b[2m',
|
||||
};
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const OPENCODE_ROOT = path.resolve(__dirname, '..');
|
||||
|
||||
// Colors
|
||||
const C = {
|
||||
reset: '\x1b[0m',
|
||||
cyan: '\x1b[36m',
|
||||
green: '\x1b[32m',
|
||||
yellow: '\x1b[33m',
|
||||
red: '\x1b[31m',
|
||||
magenta: '\x1b[35m',
|
||||
bold: '\x1b[1m',
|
||||
dim: '\x1b[2m'
|
||||
const findQwenCliJs = () => {
|
||||
const local = path.join(OPENCODE_ROOT, 'node_modules', '@qwen-code', 'qwen-code', 'cli.js');
|
||||
if (fs.existsSync(local)) return local;
|
||||
const appData = process.env.APPDATA || '';
|
||||
if (appData) {
|
||||
const globalCli = path.join(appData, 'npm', 'node_modules', '@qwen-code', 'qwen-code', 'cli.js');
|
||||
if (fs.existsSync(globalCli)) return globalCli;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
// Get qwen command for current platform
|
||||
const getQwenCommand = () => {
|
||||
const isWin = process.platform === 'win32';
|
||||
if (isWin) {
|
||||
const appData = process.env.APPDATA || '';
|
||||
const cliPath = path.join(appData, 'npm', 'node_modules', '@qwen-code', 'qwen-code', 'cli.js');
|
||||
if (fs.existsSync(cliPath)) {
|
||||
return { command: 'node', args: [cliPath] };
|
||||
}
|
||||
return { command: 'qwen.cmd', args: [] };
|
||||
}
|
||||
return { command: 'qwen', args: [] };
|
||||
};
|
||||
const checkQwenInstalled = () => new Promise((resolve) => {
|
||||
const cliJs = findQwenCliJs();
|
||||
if (cliJs) return resolve(true);
|
||||
|
||||
// Check if authenticated by running a quick test
|
||||
const checkAuth = () => {
|
||||
return new Promise((resolve) => {
|
||||
const { command, args } = getQwenCommand();
|
||||
const child = spawn(command, [...args, '--version'], { shell: false, timeout: 5000 });
|
||||
|
||||
child.on('error', () => resolve({ installed: false }));
|
||||
child.on('close', (code) => {
|
||||
resolve({ installed: code === 0 });
|
||||
});
|
||||
|
||||
setTimeout(() => { child.kill(); resolve({ installed: false }); }, 5000);
|
||||
});
|
||||
};
|
||||
|
||||
// Run qwen auth and show output (including URLs)
|
||||
const runQwenAuth = () => {
|
||||
return new Promise((resolve) => {
|
||||
console.log(C.yellow + '\n Starting Qwen authentication...' + C.reset);
|
||||
console.log(C.dim + ' This will open your browser for login.' + C.reset);
|
||||
console.log(C.dim + ' If browser doesn\'t open, copy the URL shown below.' + C.reset);
|
||||
console.log('');
|
||||
|
||||
const { command, args } = getQwenCommand();
|
||||
const child = spawn(command, [...args, 'auth'], {
|
||||
shell: false,
|
||||
stdio: 'inherit' // Show all output directly to user (includes URL)
|
||||
});
|
||||
|
||||
child.on('error', (err) => {
|
||||
console.log(C.red + `\n Error: ${err.message}` + C.reset);
|
||||
console.log('');
|
||||
console.log(C.yellow + ' To install qwen CLI:' + C.reset);
|
||||
console.log(C.cyan + ' npm install -g @qwen-code/qwen-code' + C.reset);
|
||||
resolve(false);
|
||||
});
|
||||
|
||||
child.on('close', (code) => {
|
||||
if (code === 0) {
|
||||
console.log(C.green + '\n ✅ Authentication successful!' + C.reset);
|
||||
resolve(true);
|
||||
} else {
|
||||
console.log(C.yellow + '\n Authentication may not have completed.' + C.reset);
|
||||
console.log(C.dim + ' You can try again later with: qwen auth' + C.reset);
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Main
|
||||
const main = async () => {
|
||||
console.log('');
|
||||
console.log(C.cyan + ' ╔═══════════════════════════════════════════╗' + C.reset);
|
||||
console.log(C.cyan + ' ║ OpenQode Authentication Check ║' + C.reset);
|
||||
console.log(C.cyan + ' ╚═══════════════════════════════════════════╝' + C.reset);
|
||||
console.log('');
|
||||
console.log(C.dim + ' Checking qwen CLI...' + C.reset);
|
||||
|
||||
const result = await checkAuth();
|
||||
|
||||
if (!result.installed) {
|
||||
console.log(C.yellow + '\n ⚠️ qwen CLI not found.' + C.reset);
|
||||
console.log('');
|
||||
console.log(C.yellow + ' To install:' + C.reset);
|
||||
console.log(C.cyan + ' npm install -g @qwen-code/qwen-code' + C.reset);
|
||||
console.log('');
|
||||
console.log(C.yellow + ' Then authenticate:' + C.reset);
|
||||
console.log(C.cyan + ' qwen auth' + C.reset);
|
||||
console.log('');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(C.green + ' ✅ qwen CLI is installed!' + C.reset);
|
||||
|
||||
// Check for existing tokens
|
||||
const tokenPaths = [
|
||||
path.join(process.env.HOME || process.env.USERPROFILE || '', '.qwen', 'auth.json'),
|
||||
path.join(process.env.HOME || process.env.USERPROFILE || '', '.qwen', 'config.json'),
|
||||
path.join(__dirname, '..', '.qwen-tokens.json'),
|
||||
path.join(__dirname, '..', 'tokens.json'),
|
||||
];
|
||||
|
||||
let hasToken = false;
|
||||
for (const tokenPath of tokenPaths) {
|
||||
try {
|
||||
if (fs.existsSync(tokenPath)) {
|
||||
const data = JSON.parse(fs.readFileSync(tokenPath, 'utf8'));
|
||||
if (data.access_token || data.token || data.api_key) {
|
||||
hasToken = true;
|
||||
console.log(C.green + ' ✅ Found authentication token!' + C.reset);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (e) { /* ignore */ }
|
||||
}
|
||||
|
||||
if (!hasToken) {
|
||||
console.log(C.yellow + '\n No authentication token found.' + C.reset);
|
||||
console.log(C.dim + ' Running qwen auth to authenticate...' + C.reset);
|
||||
|
||||
const success = await runQwenAuth();
|
||||
if (!success) {
|
||||
console.log('');
|
||||
console.log(C.yellow + ' You can use OpenQode, but AI features require authentication.' + C.reset);
|
||||
console.log(C.dim + ' Run "qwen auth" anytime to authenticate.' + C.reset);
|
||||
}
|
||||
} else {
|
||||
console.log(C.dim + ' Ready to use OpenQode!' + C.reset);
|
||||
}
|
||||
|
||||
console.log('');
|
||||
process.exit(0);
|
||||
};
|
||||
|
||||
main().catch(e => {
|
||||
console.error(C.red + `Auth check failed: ${e.message}` + C.reset);
|
||||
process.exit(1);
|
||||
// Fallback to PATH.
|
||||
const command = process.platform === 'win32' ? 'qwen.cmd' : 'qwen';
|
||||
const isWin = process.platform === 'win32';
|
||||
const child = spawn(command, ['--version'], { shell: isWin, timeout: 5000 });
|
||||
child.on('error', () => resolve(false));
|
||||
child.on('close', (code) => resolve(code === 0));
|
||||
setTimeout(() => { try { child.kill(); } catch { } resolve(false); }, 5000);
|
||||
});
|
||||
|
||||
const readOauthCreds = () => {
|
||||
const tokenPath = path.join(os.homedir(), '.qwen', 'oauth_creds.json');
|
||||
if (!fs.existsSync(tokenPath)) return { ok: false, reason: 'missing', tokenPath };
|
||||
try {
|
||||
const data = JSON.parse(fs.readFileSync(tokenPath, 'utf8'));
|
||||
if (!data?.access_token) return { ok: false, reason: 'invalid', tokenPath };
|
||||
const expiry = Number(data?.expiry_date || 0);
|
||||
if (expiry && expiry < Date.now() - 30_000) return { ok: false, reason: 'expired', tokenPath };
|
||||
return { ok: true, tokenPath, expiry };
|
||||
} catch {
|
||||
return { ok: false, reason: 'unreadable', tokenPath };
|
||||
}
|
||||
};
|
||||
|
||||
const main = async () => {
|
||||
if (!quiet) {
|
||||
console.log('');
|
||||
console.log(C.cyan + 'OpenQode Authentication Check' + C.reset);
|
||||
console.log(C.dim + 'Verifies Qwen CLI OAuth (shared across Gen5 + Goose).' + C.reset);
|
||||
console.log('');
|
||||
}
|
||||
|
||||
const installed = await checkQwenInstalled();
|
||||
if (!installed) {
|
||||
if (!quiet) {
|
||||
console.log(C.red + 'qwen CLI not found.' + C.reset);
|
||||
console.log(C.yellow + 'Install:' + C.reset + ' npm install -g @qwen-code/qwen-code');
|
||||
console.log('');
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const creds = readOauthCreds();
|
||||
if (!creds.ok) {
|
||||
if (!quiet) {
|
||||
console.log(C.yellow + 'Qwen CLI is installed but not authenticated yet.' + C.reset);
|
||||
console.log(C.dim + `Expected token file: ${creds.tokenPath}` + C.reset);
|
||||
console.log('');
|
||||
console.log(C.cyan + 'Fix:' + C.reset);
|
||||
console.log(' 1) Run option [5] in OpenQode launcher');
|
||||
console.log(' 2) In Qwen CLI run: /auth');
|
||||
console.log(' 3) Return and retry Gen5/Goose');
|
||||
console.log('');
|
||||
}
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
if (!quiet) {
|
||||
console.log(C.green + 'OK: Qwen CLI + OAuth ready.' + C.reset);
|
||||
console.log(C.dim + `Token: ${creds.tokenPath}` + C.reset);
|
||||
console.log('');
|
||||
}
|
||||
|
||||
process.exit(0);
|
||||
};
|
||||
|
||||
main().catch((e) => {
|
||||
if (!quiet) console.error(C.red + String(e?.message || e) + C.reset);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user