@@ -119,19 +119,20 @@ export async function checkWhisperDirAndModel(
119
119
/**
120
120
* checkOllamaServerAndModel()
121
121
* ---------------------
122
- * Checks if the Ollama server is running, attempts to start it if not running ,
123
- * and ensures that the specified model is available. If not, it will pull the model .
122
+ * Checks if the Ollama server is running, attempts to start it if not,
123
+ * and ensures the specified model is available (pulling if needed) .
124
124
*
125
125
* @param {string } ollamaHost - The Ollama host
126
126
* @param {string } ollamaPort - The Ollama port
127
- * @param {string } ollamaModelName - The Ollama model name
127
+ * @param {string } ollamaModelName - The Ollama model name (e.g. 'qwen2.5:0.5b')
128
128
* @returns {Promise<void> }
129
129
*/
130
130
export async function checkOllamaServerAndModel (
131
131
ollamaHost : string ,
132
132
ollamaPort : string ,
133
133
ollamaModelName : string
134
134
) : Promise < void > {
135
+ // Helper to check if the Ollama server responds
135
136
async function checkServer ( ) : Promise < boolean > {
136
137
try {
137
138
const serverResponse = await fetch ( `http://${ ollamaHost } :${ ollamaPort } ` )
@@ -141,23 +142,29 @@ export async function checkOllamaServerAndModel(
141
142
}
142
143
}
143
144
145
+ l . info ( `[checkOllamaServerAndModel] Checking server: http://${ ollamaHost } :${ ollamaPort } ` )
146
+
147
+ // 1) Confirm the server is running
144
148
if ( await checkServer ( ) ) {
145
149
l . wait ( '\n Ollama server is already running...' )
146
150
} else {
151
+ // If the Docker-based environment uses 'ollama' as hostname but it's not up, that's likely an error
147
152
if ( ollamaHost === 'ollama' ) {
148
153
throw new Error ( 'Ollama server is not running. Please ensure the Ollama server is running and accessible.' )
149
154
} else {
150
- l . wait ( '\n Ollama server is not running. Attempting to start...' )
155
+ // Attempt to spawn an Ollama server locally
156
+ l . wait ( '\n Ollama server is not running. Attempting to start it locally...' )
151
157
const ollamaProcess = spawn ( 'ollama' , [ 'serve' ] , {
152
158
detached : true ,
153
159
stdio : 'ignore' ,
154
160
} )
155
161
ollamaProcess . unref ( )
156
162
163
+ // Wait up to ~30 seconds for the server to respond
157
164
let attempts = 0
158
165
while ( attempts < 30 ) {
159
166
if ( await checkServer ( ) ) {
160
- l . wait ( ' - Ollama server is now ready.' )
167
+ l . wait ( ' - Ollama server is now ready.\n ' )
161
168
break
162
169
}
163
170
await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) )
@@ -169,17 +176,20 @@ export async function checkOllamaServerAndModel(
169
176
}
170
177
}
171
178
172
- l . wait ( `\n Checking if model is available: ${ ollamaModelName } ` )
179
+ // 2) Confirm the model is available; if not, pull it
180
+ l . wait ( ` Checking if model is available: ${ ollamaModelName } ` )
173
181
try {
174
182
const tagsResponse = await fetch ( `http://${ ollamaHost } :${ ollamaPort } /api/tags` )
175
183
if ( ! tagsResponse . ok ) {
176
184
throw new Error ( `HTTP error! status: ${ tagsResponse . status } ` )
177
185
}
186
+
178
187
const tagsData = ( await tagsResponse . json ( ) ) as OllamaTagsResponse
179
188
const isModelAvailable = tagsData . models . some ( ( m ) => m . name === ollamaModelName )
189
+ l . info ( `[checkOllamaServerAndModel] isModelAvailable=${ isModelAvailable } ` )
180
190
181
191
if ( ! isModelAvailable ) {
182
- l . wait ( `\n Model ${ ollamaModelName } is not available, pulling...` )
192
+ l . wait ( `\n Model ${ ollamaModelName } is NOT available; pulling now ...` )
183
193
const pullResponse = await fetch ( `http://${ ollamaHost } :${ ollamaPort } /api/pull` , {
184
194
method : 'POST' ,
185
195
headers : { 'Content-Type' : 'application/json' } ,
@@ -189,11 +199,13 @@ export async function checkOllamaServerAndModel(
189
199
throw new Error ( `Failed to initiate pull for model ${ ollamaModelName } ` )
190
200
}
191
201
if ( ! pullResponse . body ) {
192
- throw new Error ( 'Response body is null' )
202
+ throw new Error ( 'Response body is null while pulling model. ' )
193
203
}
194
204
195
205
const reader = pullResponse . body . getReader ( )
196
206
const decoder = new TextDecoder ( )
207
+
208
+ // Stream the JSON lines from the server
197
209
while ( true ) {
198
210
const { done, value } = await reader . read ( )
199
211
if ( done ) break
@@ -202,6 +214,8 @@ export async function checkOllamaServerAndModel(
202
214
const lines = chunk . split ( '\n' )
203
215
for ( const line of lines ) {
204
216
if ( line . trim ( ) === '' ) continue
217
+
218
+ // Each line should be a JSON object from the Ollama server
205
219
try {
206
220
const parsedLine = JSON . parse ( line )
207
221
if ( parsedLine . status === 'success' ) {
0 commit comments