cloud-pc-templates 1.0.2 → 1.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.
@@ -0,0 +1,147 @@
1
+ const http = require('http');
2
+ const https = require('https');
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const { spawn } = require('child_process');
6
+ const os = require('os');
7
+
8
+ // Function to get masked API key input
9
+ function promptForApiKey() {
10
+ return new Promise((resolve) => {
11
+ process.stdout.write('Enter API Key: ');
12
+
13
+ const stdin = process.stdin;
14
+
15
+ // Handle both TTY and non-TTY environments
16
+ if (stdin.isTTY) {
17
+ stdin.setRawMode(true);
18
+ }
19
+ stdin.resume();
20
+
21
+ let apiKey = '';
22
+ let isFirstInput = true;
23
+
24
+ stdin.on('data', (byte) => {
25
+ const char = byte.toString();
26
+
27
+ if (char === '\n' || char === '\r' || char === '\u0004') {
28
+ // Enter or EOF
29
+ if (stdin.isTTY) {
30
+ stdin.setRawMode(false);
31
+ }
32
+ stdin.pause();
33
+ stdin.removeAllListeners('data');
34
+ console.log('');
35
+ resolve(apiKey);
36
+ } else if (char === '\u0003') {
37
+ // Ctrl+C
38
+ if (stdin.isTTY) {
39
+ stdin.setRawMode(false);
40
+ }
41
+ process.exit();
42
+ } else if (char === '\x7f' || char === '\b') {
43
+ // Backspace
44
+ if (apiKey.length > 0) {
45
+ apiKey = apiKey.slice(0, -1);
46
+ process.stdout.write('\x1b[D\x1b[K');
47
+ }
48
+ } else if (char >= '\x20' && char <= '\x7e') {
49
+ // Printable character
50
+ apiKey += char;
51
+ process.stdout.write('*');
52
+ }
53
+ });
54
+ });
55
+ }
56
+
57
+ // Function to check health endpoint
58
+ function checkHealthEndpoint(endpoint) {
59
+ return new Promise((resolve) => {
60
+ const url = new URL(endpoint);
61
+ const protocol = url.protocol === 'https:' ? https : http;
62
+
63
+ const request = protocol.request(url, { method: 'GET' }, (res) => {
64
+ resolve(res.statusCode === 200);
65
+ });
66
+
67
+ request.on('error', () => {
68
+ resolve(false);
69
+ });
70
+
71
+ request.end();
72
+ });
73
+ }
74
+
75
+ // Function to download and run the proxy
76
+ async function downloadAndRunProxy(endpoint) {
77
+ const url = 'https://raw.githubusercontent.com/devashish234073/cloud-pc-templates-marketplace/refs/heads/main/JS-PROXIES/ollama-proxy.js';
78
+ const tempFile = path.join(os.tmpdir(), 'ollama-proxy.js');
79
+
80
+ // Download the file
81
+ await new Promise((resolve, reject) => {
82
+ const file = fs.createWriteStream(tempFile);
83
+ https.get(url, (res) => {
84
+ res.pipe(file);
85
+ file.on('finish', () => {
86
+ file.close();
87
+ resolve();
88
+ });
89
+ }).on('error', reject);
90
+ });
91
+
92
+ // Get API key from user
93
+ const apiKey = await promptForApiKey();
94
+
95
+ // Run the proxy with API key passed as environment variable
96
+ return new Promise((resolve, reject) => {
97
+ const env = Object.assign({}, process.env, { API_KEY: apiKey });
98
+ const child = spawn('node', [tempFile], { env });
99
+
100
+ child.on('close', (code) => {
101
+ if (code === 0) {
102
+ // Wait a bit for the server to start, then validate
103
+ setTimeout(async () => {
104
+ const isHealthy = await checkHealthEndpoint(endpoint);
105
+ if (isHealthy) {
106
+ console.log('✓ Logged in');
107
+ console.log(` - Endpoint checked: ${endpoint}`);
108
+ } else {
109
+ console.log('✓ Proxy started');
110
+ console.log(` - Endpoint: ${endpoint}`);
111
+ }
112
+ resolve();
113
+ }, 1000);
114
+ } else {
115
+ reject(new Error('Proxy process failed'));
116
+ }
117
+ });
118
+
119
+ child.on('error', reject);
120
+ });
121
+ }
122
+
123
+ // Function to check and login to Ollama Cloud
124
+ async function checkAndLoginOllamaCloud() {
125
+ const endpoint = 'http://localhost:3004/health';
126
+
127
+ try {
128
+ const isHealthy = await checkHealthEndpoint(endpoint);
129
+ if (isHealthy) {
130
+ console.log('✓ Already logged in');
131
+ console.log(` - Endpoint checked: ${endpoint}`);
132
+ return;
133
+ }
134
+
135
+ // Not healthy, download and run proxy
136
+ await downloadAndRunProxy(endpoint);
137
+ } catch (error) {
138
+ console.error('Error during login:', error.message);
139
+ }
140
+ }
141
+
142
+ module.exports = {
143
+ checkAndLoginOllamaCloud,
144
+ checkHealthEndpoint,
145
+ downloadAndRunProxy,
146
+ promptForApiKey
147
+ };
package/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ const { checkAndLoginOllamaCloud } = require('./handlers/ollamacloud');
4
+
3
5
  // Command tree structure
4
6
  const commandTree = {
5
7
  help: {
@@ -53,12 +55,11 @@ function aiDefault() {
53
55
  }
54
56
 
55
57
  // AI Login function
56
- function aiLogin(mode) {
57
- console.log(`✓ AI Login initialized with mode: ${mode}`);
58
+ async function aiLogin(mode) {
58
59
  if (mode === 'ollamacloud') {
59
- console.log(' - Connecting to Ollama Cloud...');
60
- console.log(' - Initializing cloud connection...');
60
+ await checkAndLoginOllamaCloud();
61
61
  } else if (mode === 'ollamalocal') {
62
+ console.log(`✓ AI Login initialized with mode: ${mode}`);
62
63
  console.log(' - Connecting to Ollama Local...');
63
64
  console.log(' - Initializing local connection...');
64
65
  }
@@ -107,8 +108,11 @@ function traverseCommandTree(args, startNode, startPath = []) {
107
108
  if (currentNode.subcommands) {
108
109
  showAvailableOptions(currentNode, path);
109
110
  } else if (currentNode.handler) {
110
- // Execute the handler if available
111
- currentNode.handler();
111
+ // Execute the handler if available (could be async)
112
+ const result = currentNode.handler();
113
+ if (result instanceof Promise) {
114
+ result.catch(err => console.error('Error:', err.message));
115
+ }
112
116
  } else {
113
117
  console.log('Command complete but no action defined');
114
118
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cloud-pc-templates",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {