npm-noxyai 1.0.2 → 1.0.3

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.
Files changed (2) hide show
  1. package/index.js +108 -7
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,4 +1,28 @@
1
- // --- COMMAND: login ---
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require('fs');
4
+ const os = require('os');
5
+ const path = require('path');
6
+
7
+ // Canonical URL to avoid 308 redirects that strip Auth headers
8
+ const BASE_URL = 'https://www.noxyai.com';
9
+ const CONFIG_FILE = path.join(os.homedir(), '.noxyai.json');
10
+
11
+ const args = process.argv.slice(2);
12
+ const command = args[0];
13
+
14
+ function saveToken(token) {
15
+ fs.writeFileSync(CONFIG_FILE, JSON.stringify({ token }));
16
+ }
17
+
18
+ function getToken() {
19
+ if (fs.existsSync(CONFIG_FILE)) {
20
+ const config = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
21
+ return config.token;
22
+ }
23
+ return null;
24
+ }
25
+
2
26
  async function login() {
3
27
  console.log('Initializing login...');
4
28
  try {
@@ -24,10 +48,9 @@ async function login() {
24
48
  clearInterval(pollInterval);
25
49
  saveToken(data.token);
26
50
 
27
- // ASCII ART SUCCESS MESSAGE
28
51
  console.clear();
29
- console.log(`\n\x1b[36m
30
- ___ ___ ___ ___ ___
52
+ console.log('\n\x1b[36m' +
53
+ ` ___ ___ ___ ___ ___
31
54
  /\\__\\ /\\ \\ |\\__\\ |\\__\\ /\\ \\ ___
32
55
  /::| | /::\\ \\ |:| | |:| | /::\\ \\ /\\ \\
33
56
  /:|:| | /:/\\:\\ \\ |:| | |:| | /:/\\:\\ \\ \\:\\ \\
@@ -37,14 +60,14 @@ async function login() {
37
60
  |:/:/ / \\:\\ /:/ / ~~|:|~~| /:/ / \\::/ / \\::/__/
38
61
  |::/ / \\:\\/:/ / |:| | \\/__/ /:/ / \\:\\__\\
39
62
  /:/ / \\::/ / |:| | /:/ / \\/__/
40
- \\/__/ \\/__/ \\|__| \\/__/
41
- \x1b[0m`);
63
+ \\/__/ \\/__/ \\|__| \\/__/ `
64
+ + '\x1b[0m');
42
65
  console.log('\n✅ Login successful! Terminal connected.');
43
66
  console.log('Try running: noxyai chat "Hello, world!"\n');
44
67
  process.exit(0);
45
68
  } else if (data.error) {
46
69
  clearInterval(pollInterval);
47
- console.error(`\n❌ Login failed: ${data.error}`);
70
+ console.error(\`\\n❌ Login failed: \${data.error}\`);
48
71
  process.exit(1);
49
72
  }
50
73
  }, interval * 1000);
@@ -54,3 +77,81 @@ async function login() {
54
77
  }
55
78
  }
56
79
 
80
+ async function chat(prompt) {
81
+ const token = getToken();
82
+ if (!token) {
83
+ console.error('❌ You are not logged in. Please run: noxyai login');
84
+ process.exit(1);
85
+ }
86
+
87
+ if (!prompt) {
88
+ console.error('❌ Please provide a prompt. Example: noxyai chat "Write a python script"');
89
+ process.exit(1);
90
+ }
91
+
92
+ const model = 'auto';
93
+
94
+ try {
95
+ const res = await fetch(\`\${BASE_URL}/api/cli/execute\`, {
96
+ method: 'POST',
97
+ headers: {
98
+ 'Content-Type': 'application/json',
99
+ 'Authorization': \`Bearer \${token}\`
100
+ },
101
+ body: JSON.stringify({ prompt, model })
102
+ });
103
+
104
+ if (!res.ok) {
105
+ const errorText = await res.text();
106
+ console.error(\`\\n❌ API Error: \${errorText}\`);
107
+ process.exit(1);
108
+ }
109
+
110
+ console.log(\`\\n🤖 NoxyAI (\${model}):\\n\`);
111
+
112
+ const reader = res.body.getReader();
113
+ const decoder = new TextDecoder('utf-8');
114
+
115
+ while (true) {
116
+ const { done, value } = await reader.read();
117
+ if (done) break;
118
+
119
+ const chunk = decoder.decode(value, { stream: true });
120
+ const lines = chunk.split('\\n');
121
+
122
+ for (const line of lines) {
123
+ if (line.startsWith('data: ')) {
124
+ const data = line.slice(6).trim();
125
+ if (data === '[DONE]') {
126
+ console.log('\\n');
127
+ process.exit(0);
128
+ }
129
+ try {
130
+ const parsed = JSON.parse(data);
131
+ if (parsed.text) {
132
+ process.stdout.write(parsed.text);
133
+ }
134
+ } catch (e) {}
135
+ }
136
+ }
137
+ }
138
+ } catch (error) {
139
+ console.error('\\n❌ Connection error:', error.message);
140
+ }
141
+ }
142
+
143
+ if (command === 'login') {
144
+ login();
145
+ } else if (command === 'chat') {
146
+ const prompt = args.slice(1).join(' ');
147
+ chat(prompt);
148
+ } else {
149
+ console.log(\`
150
+ Usage:
151
+ noxyai login - Authenticate your terminal
152
+ noxyai chat <prompt> - Chat with NoxyAI
153
+
154
+ Example:
155
+ noxyai chat "How do I reverse a string in JS?"
156
+ \`);
157
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "npm-noxyai",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {