@cloudverse/aix-cli 1.1.0 → 1.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/dist/http.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export declare function setTokenOverride(token: string | null): void;
2
+ export declare function setBaseUrl(url: string | null): void;
2
3
  export interface HttpConfig {
3
4
  baseUrl: string;
4
5
  token: string | null;
package/dist/http.js CHANGED
@@ -1,11 +1,15 @@
1
1
  const DEFAULT_BASE_URL = 'https://aix.cloudverse.ai';
2
2
  let tokenOverride = null;
3
+ let baseUrlOverride = null;
3
4
  export function setTokenOverride(token) {
4
5
  tokenOverride = token;
5
6
  }
7
+ export function setBaseUrl(url) {
8
+ baseUrlOverride = url ? url.replace(/\/$/, '') : null;
9
+ }
6
10
  export function getConfig() {
7
11
  return {
8
- baseUrl: (process.env.AIX_API_URL || DEFAULT_BASE_URL).replace(/\/$/, ''),
12
+ baseUrl: baseUrlOverride || (process.env.AIX_API_URL || DEFAULT_BASE_URL).replace(/\/$/, ''),
9
13
  token: tokenOverride || process.env.AIX_TOKEN || null,
10
14
  };
11
15
  }
@@ -19,17 +23,31 @@ export async function request(method, path, body) {
19
23
  if (config.token) {
20
24
  headers['Authorization'] = `Bearer ${config.token}`;
21
25
  }
22
- const res = await fetch(url, {
23
- method,
24
- headers,
25
- body: body ? JSON.stringify(body) : undefined,
26
- });
27
- let data;
26
+ let res;
28
27
  try {
29
- data = await res.json();
28
+ res = await fetch(url, {
29
+ method,
30
+ headers,
31
+ body: body ? JSON.stringify(body) : undefined,
32
+ });
33
+ }
34
+ catch (err) {
35
+ throw new Error(`Cannot connect to ${config.baseUrl}: ${err.message || err}`);
36
+ }
37
+ let data;
38
+ const contentType = res.headers.get('content-type') || '';
39
+ if (contentType.includes('application/json')) {
40
+ try {
41
+ data = await res.json();
42
+ }
43
+ catch {
44
+ data = { error: `Failed to parse JSON response (HTTP ${res.status})` };
45
+ }
30
46
  }
31
- catch {
32
- data = { error: `HTTP ${res.status}: ${res.statusText}` };
47
+ else {
48
+ const text = await res.text().catch(() => '');
49
+ data = { error: `Server returned non-JSON response (HTTP ${res.status})`, body: text.slice(0, 200) };
50
+ return { ok: false, status: res.status, data };
33
51
  }
34
52
  return { ok: res.ok, status: res.status, data };
35
53
  }
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { Command } from 'commander';
3
- import { request, exitWithError, getConfig } from './http.js';
3
+ import { request, exitWithError, getConfig, setBaseUrl } from './http.js';
4
4
  import { formatDecision, formatExplanation, formatHealth, formatPolicy, formatCatalogStatus } from './formatters.js';
5
5
  import { loadTokens, saveTokens, clearTokens, refreshIfNeeded } from './auth.js';
6
6
  const program = new Command();
@@ -13,21 +13,14 @@ program
13
13
  .description('Authenticate with AIX via browser-based device login')
14
14
  .option('--api-url <url>', 'AIX server URL (default: https://aix.cloudverse.ai)')
15
15
  .action(async (opts) => {
16
+ if (opts.apiUrl) {
17
+ setBaseUrl(opts.apiUrl);
18
+ }
16
19
  const config = getConfig();
17
- const baseUrl = opts.apiUrl || config.baseUrl;
20
+ const baseUrl = config.baseUrl;
18
21
  console.log('\n\x1b[1mAIX Device Login\x1b[0m');
19
22
  console.log('─'.repeat(50));
20
- const startRes = await request('POST', '/api/auth/device/start', {
21
- cli_version: '1.0.0',
22
- });
23
- if (!startRes.ok) {
24
- exitWithError('Failed to start device login', JSON.stringify(startRes.data, null, 2));
25
- }
26
- const { device_code, user_code, verification_uri, verification_uri_complete, interval, expires_in } = startRes.data;
27
- console.log(`\n Your verification code:\n`);
28
- console.log(` \x1b[1m\x1b[36m${user_code}\x1b[0m\n`);
29
- console.log(` Opening browser to:\n`);
30
- console.log(` \x1b[4m${verification_uri_complete}\x1b[0m\n`);
23
+ console.log(` Server: ${baseUrl}\n`);
31
24
  const openBrowser = async (url) => {
32
25
  const { exec } = await import('child_process');
33
26
  const platform = process.platform;
@@ -41,8 +34,28 @@ program
41
34
  }
42
35
  });
43
36
  };
44
- await openBrowser(verification_uri_complete);
45
- console.log(` Waiting for approval (expires in ${Math.floor(expires_in / 60)} minutes)...\n`);
37
+ let startRes;
38
+ try {
39
+ startRes = await request('POST', '/api/auth/device/start', {
40
+ cli_version: '1.1.1',
41
+ });
42
+ }
43
+ catch (err) {
44
+ exitWithError(`Cannot connect to AIX server at ${baseUrl}`, `Network error: ${err.message}\n Try: aix login --api-url https://your-server-url`);
45
+ }
46
+ if (!startRes.ok) {
47
+ exitWithError('Failed to start device login', JSON.stringify(startRes.data, null, 2));
48
+ }
49
+ const { device_code, user_code, interval, expires_in } = startRes.data;
50
+ if (!user_code || !device_code) {
51
+ exitWithError('Unexpected response from server (missing device login fields)', `Server may not support device login, or the URL is incorrect.\n Server: ${baseUrl}\n Response: ${JSON.stringify(startRes.data, null, 2)}\n Try: aix login --api-url https://your-server-url`);
52
+ }
53
+ const authUrl = `${baseUrl}/cli-auth?user_code=${user_code}`;
54
+ console.log(' Opening browser to authenticate...\n');
55
+ await openBrowser(authUrl);
56
+ console.log(` If the browser didn't open, visit:\n`);
57
+ console.log(` \x1b[4m${authUrl}\x1b[0m\n`);
58
+ console.log(' Waiting for approval...\n');
46
59
  const deadline = Date.now() + expires_in * 1000;
47
60
  let pollInterval = interval * 1000;
48
61
  while (Date.now() < deadline) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudverse/aix-cli",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "AIX Brain Decision Engine CLI - decision-only surface for AI workload routing",
5
5
  "type": "module",
6
6
  "bin": {