scai 0.1.70 → 0.1.71

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/dist/modelSetup.js +49 -37
  2. package/package.json +1 -1
@@ -3,15 +3,17 @@ import * as readline from 'readline';
3
3
  import * as fs from 'fs';
4
4
  import * as path from 'path';
5
5
  import chalk from 'chalk';
6
+ import { platform } from 'os';
6
7
  import { getDbForRepo } from './db/client.js';
7
8
  import { readConfig, writeConfig } from './config.js';
8
9
  import { CONFIG_PATH } from './constants.js';
9
- import { platform } from 'os';
10
10
  // Constants
11
11
  const MODEL_PORT = 11434;
12
12
  const REQUIRED_MODELS = ['llama3', 'mistral'];
13
13
  const OLLAMA_URL = 'https://ollama.com/download';
14
14
  const isYesMode = process.argv.includes('--yes') || process.env.SCAI_YES === '1';
15
+ let ollamaChecked = false;
16
+ let ollamaAvailable = false;
15
17
  // 🧠 Auto init config/db if missing
16
18
  export async function autoInitIfNeeded() {
17
19
  const cfg = readConfig();
@@ -38,20 +40,41 @@ function promptUser(question) {
38
40
  resolve(a.trim());
39
41
  }));
40
42
  }
41
- // 🚀 Ensure Ollama server is running
42
- async function ensureOllamaRunning() {
43
+ // 🧭 Cross-platform browser opener
44
+ function openBrowser(url) {
45
+ const command = platform() === 'win32'
46
+ ? `start ${url}`
47
+ : platform() === 'darwin'
48
+ ? `open ${url}`
49
+ : `xdg-open ${url}`;
50
+ try {
51
+ execSync(command, { stdio: 'ignore' });
52
+ }
53
+ catch {
54
+ console.log(chalk.yellow('🔗 Please manually open:'), url);
55
+ }
56
+ }
57
+ // 🌐 Check if Ollama is running
58
+ async function isOllamaRunning() {
43
59
  try {
44
60
  const res = await fetch(`http://localhost:${MODEL_PORT}`);
45
- if (res.ok) {
46
- console.log(chalk.green('✅ Result:') + ` Ollama is already running on port ${MODEL_PORT}.`);
47
- return;
48
- }
61
+ return res.ok;
49
62
  }
50
63
  catch {
51
- console.log(chalk.yellow('⚙️ Challenge:') + ' Ollama is not running. Attempting to start it...');
64
+ return false;
65
+ }
66
+ }
67
+ // 🚀 Ensure Ollama server is running
68
+ async function ensureOllamaRunning() {
69
+ if (ollamaChecked)
70
+ return;
71
+ ollamaChecked = true;
72
+ if (await isOllamaRunning()) {
73
+ console.log(chalk.green('✅ Ollama is already running.'));
74
+ ollamaAvailable = true;
75
+ return;
52
76
  }
53
- console.log(chalk.yellow('⚙️ Challenge:') + ` Ollama does not appear to be running on port ${MODEL_PORT}.\n` +
54
- chalk.yellow('🚀 Action:') + ' Attempting to start Ollama in the background...');
77
+ console.log(chalk.yellow('⚙️ Ollama is not running. Attempting to start it...'));
55
78
  try {
56
79
  const child = spawn('ollama', ['serve'], {
57
80
  detached: true,
@@ -61,42 +84,30 @@ async function ensureOllamaRunning() {
61
84
  child.on('error', async (err) => {
62
85
  if (err.code === 'ENOENT') {
63
86
  console.log(chalk.red('❌ Ollama is not installed or not in PATH.'));
64
- console.log(chalk.yellow(`📦 Ollama is required to run local AI models.\n`));
65
- console.log(chalk.yellow(`🔗 Download it here: ${OLLAMA_URL}`));
87
+ console.log(chalk.yellow(`📦 Ollama is required to run local AI models.`));
66
88
  const answer = await promptUser('🌐 Would you like to open the download page in your browser? (y/N): ');
67
89
  if (answer.toLowerCase() === 'y') {
68
90
  openBrowser(OLLAMA_URL);
69
91
  }
70
92
  process.exit(1);
71
93
  }
72
- else {
73
- console.error(chalk.red('❌ Failed to start Ollama process:'), err);
74
- process.exit(1);
75
- }
76
94
  });
77
95
  child.unref();
78
96
  await new Promise((res) => setTimeout(res, 3000));
79
- console.log(chalk.green('✅ Result:') + ' Ollama started successfully.');
97
+ if (await isOllamaRunning()) {
98
+ console.log(chalk.green('✅ Ollama started successfully.'));
99
+ ollamaAvailable = true;
100
+ }
101
+ else {
102
+ console.log(chalk.red('❌ Ollama did not start within timeout.'));
103
+ process.exit(1);
104
+ }
80
105
  }
81
- catch (err) {
82
- console.error(chalk.red('❌ Unexpected error while trying to start Ollama:'), err);
106
+ catch {
107
+ console.log(chalk.red('❌ Unexpected error starting Ollama.'));
83
108
  process.exit(1);
84
109
  }
85
110
  }
86
- // 🧭 Cross-platform browser opener
87
- function openBrowser(url) {
88
- const command = platform() === 'win32'
89
- ? `start ${url}`
90
- : platform() === 'darwin'
91
- ? `open ${url}`
92
- : `xdg-open ${url}`;
93
- try {
94
- execSync(command, { stdio: 'ignore' });
95
- }
96
- catch (err) {
97
- console.error(chalk.red('❌ Could not open browser. Please visit:'), url);
98
- }
99
- }
100
111
  // 🧰 List installed models
101
112
  async function getInstalledModels() {
102
113
  try {
@@ -106,13 +117,14 @@ async function getInstalledModels() {
106
117
  .map((line) => line.split(/\s+/)[0].split(':')[0])
107
118
  .filter((model) => REQUIRED_MODELS.includes(model));
108
119
  }
109
- catch (err) {
110
- console.error(chalk.red('❌ Could not fetch installed models:'), err);
120
+ catch {
111
121
  return [];
112
122
  }
113
123
  }
114
124
  // 📥 Download missing models
115
125
  async function ensureModelsDownloaded() {
126
+ if (!ollamaAvailable)
127
+ return;
116
128
  const installed = await getInstalledModels();
117
129
  const missing = REQUIRED_MODELS.filter((m) => !installed.includes(m));
118
130
  if (!missing.length) {
@@ -131,8 +143,8 @@ async function ensureModelsDownloaded() {
131
143
  execSync(`ollama pull ${model}`, { stdio: 'inherit' });
132
144
  console.log(chalk.green(`✅ Pulled ${model}`));
133
145
  }
134
- catch (err) {
135
- console.error(chalk.red(`❌ Failed to pull ${model}:`), err);
146
+ catch {
147
+ console.log(chalk.red(`❌ Failed to pull ${model}.`));
136
148
  process.exit(1);
137
149
  }
138
150
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scai",
3
- "version": "0.1.70",
3
+ "version": "0.1.71",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "scai": "./dist/index.js"