Skip to content

Commit 493ab0c

Browse files
committed
Improve handling of llama-server start problems
At first launch of codesigned and notarized app llama-server seems to start very slowly. Let's handle this situation more gracefully, try waiting and inform user of the status
1 parent cea5834 commit 493ab0c

4 files changed

Lines changed: 88 additions & 7 deletions

File tree

AIChat.app/Contents/Resources/Scripts/aichat.init.sh

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ source "$OMC_APP_BUNDLE_PATH/Contents/Resources/Scripts/aichat.library.sh"
44

55
echo "[$(/usr/bin/basename "$0")]"
66

7+
webui_dir_path="$OMC_APP_BUNDLE_PATH/Contents/Resources/WebUI"
8+
79
register_started_server()
810
{
911
local host_pid="$1"
@@ -90,7 +92,9 @@ stop_orphaned_servers()
9092

9193
wait_for_server_response()
9294
{
95+
local result=0
9396
local seconds_count=0
97+
9498
while true; do
9599
# wait until we get a response from server
96100
/usr/bin/curl "http://localhost:$port_num/slots" > /dev/null 2>&1
@@ -100,24 +104,38 @@ wait_for_server_response()
100104
break
101105
fi
102106

103-
# or 5 seconds pass
107+
# or 20 seconds pass
104108
seconds_count=$((seconds_count + 1))
105-
if [ "$seconds_count" -ge 5 ]; then
106-
echo "warning: timed out after $seconds_count seconds while waiting for server response"
109+
if [ "$seconds_count" -ge 20 ]; then
110+
local message=$(echo "Timed out after $seconds_count seconds while waiting for llama-server response.\n\nPlease try again")
111+
echo "$message"
112+
"$alert" --level "stop" --title "$APPLET_NAME" --ok "OK" "$message"
113+
result=13
107114
break
115+
elif [ "$seconds_count" -eq 5 ]; then
116+
echo "$dialog $OMC_NIB_DLG_GUID 2 file://${webui_dir_path}/start_slow.html"
117+
"$dialog" "$OMC_NIB_DLG_GUID" 2 "file://${webui_dir_path}/start_slow.html"
108118
fi
109119

110120
sleep 1
111121
done
122+
123+
return "$result"
112124
}
113125

114126
report_server_launch_failure()
115127
{
116128
local message=$(echo "llama-server failed to launch! \n\nVerify if the selected large language model is supported by llama.cpp engine.")
117129
echo "$message"
118130
"$alert" --level "stop" --title "$APPLET_NAME" --ok "OK" "$message"
131+
return 11
119132
}
120133

134+
135+
echo "$dialog $OMC_NIB_DLG_GUID 2 file://${webui_dir_path}/start.html"
136+
"$dialog" "$OMC_NIB_DLG_GUID" 2 "file://${webui_dir_path}/start.html"
137+
echo ""
138+
121139
echo "OMC_CURRENT_COMMAND_GUID: ${OMC_CURRENT_COMMAND_GUID}"
122140
echo "OMC_NIB_DLG_GUID: ${OMC_NIB_DLG_GUID}"
123141
echo "OMC_FRONT_PROCESS_ID: ${OMC_FRONT_PROCESS_ID}"
@@ -148,13 +166,14 @@ stop_orphaned_servers
148166

149167
# /usr/bin/curl "http://localhost:$port_num/slots" > /dev/null 2>&1
150168

169+
server_result=0
170+
151171
echo "Check if the required llama-server with selected model is already running"
152172
running_process=$(/bin/ps -U $USER | /usr/bin/grep -E "$OMC_APP_BUNDLE_PATH/Contents/Support/Llama.cpp/llama-server" | /usr/bin/grep -E "$port_num" | /usr/bin/grep -E "$AICHAT_MODEL_PATH")
153173

154174
if [ $? != 0 ]; then
155175
echo "Starting llama-server..."
156176
# start the server
157-
webui_dir_path="$OMC_APP_BUNDLE_PATH/Contents/Resources/WebUI"
158177
"$OMC_APP_BUNDLE_PATH/Contents/Support/Llama.cpp/llama-server" --host 127.0.0.1 --port $port_num --path "$webui_dir_path" --model "$AICHAT_MODEL_PATH" &
159178
llama_server_pid=$!
160179
if [ "$llama_server_pid" != "" ]; then
@@ -164,23 +183,33 @@ if [ $? != 0 ]; then
164183
if [ "$server_process_exists" != 0 ]; then
165184
# server exited. most likely something wrong with selected gguf model
166185
report_server_launch_failure
186+
server_result=$?
167187
else
168188
# server process running, check if it is responsive
169189
wait_for_server_response
190+
server_result=$?
170191

171192
echo "Register server with pid $llama_server_pid"
172193
register_started_server "${OMC_FRONT_PROCESS_ID}" "${llama_server_pid}" "$AICHAT_MODEL_PATH"
173194
fi
174195
else
175196
report_server_launch_failure
197+
server_result=$?
176198
fi
177199

178200
else
179201
llama_server_pid=$(echo "$running_process" | /usr/bin/grep -E --only-matching '^ *[[:digit:]]+ ' | /usr/bin/tr -d ' ')
180202
echo "llama-server already running with pid: $running_process"
203+
server_result=0
181204
fi
182205

183-
echo ""
184-
echo "$dialog $OMC_NIB_DLG_GUID 2 http://localhost:$port_num/"
185-
"$dialog" "$OMC_NIB_DLG_GUID" 2 "http://localhost:$port_num/"
206+
if [ "$server_result" = 0 ]; then
207+
echo ""
208+
echo "$dialog $OMC_NIB_DLG_GUID 2 http://localhost:$port_num/"
209+
"$dialog" "$OMC_NIB_DLG_GUID" 2 "http://localhost:$port_num/"
210+
else
211+
echo ""
212+
echo "$dialog $OMC_NIB_DLG_GUID 2 file://${webui_dir_path}/start_error.html?port=$port_num"
213+
"$dialog" "$OMC_NIB_DLG_GUID" 2 "file://${webui_dir_path}/start_error.html?port=$port_num"
214+
fi
186215

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Starting Local Llama Server</title>
6+
<body>
7+
<div style="display: block; margin-top: 200px; text-align: center; height: 300vh; align-items: center; justify-content: center;">
8+
<p>Starting local Llama server...</p>
9+
</div>
10+
</body>
11+
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Local Llama Server Error</title>
6+
<script>
7+
window.onload = function () {
8+
const currentURL = window.location.href;
9+
const url = new URL(currentURL);
10+
const params = new URLSearchParams(url.search);
11+
port = params.get('port');
12+
13+
// Check if port exists and construct the URL
14+
if (!port) {
15+
port = 8080
16+
}
17+
const constructedURL = `http://localhost:${port}/`;
18+
document.getElementById('server_link').innerHTML = `<a href="${constructedURL}">Try loading anyway</a>`;
19+
};
20+
</script>
21+
<body>
22+
<div style="display: block; margin-top: 200px; text-align: center; height: 300vh; align-items: center; justify-content: center;">
23+
<h3>Local Llama server error</h3>
24+
<p>There was a problem starting local Llama server</p>
25+
</div>
26+
<div id="server_link" style="display: block; margin-top: 200px; text-align: center; height: 300vh; align-items: center; justify-content: center;"></div>
27+
</body>
28+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Starting Local Llama Server</title>
6+
<body>
7+
<div style="display: block; margin-top: 200px; text-align: center; height: 300vh; align-items: center; justify-content: center;">
8+
<p>It is taking longer than expected to start the local Llama server.</br></p>
9+
<p>This may happen on the first launch.</br></p>
10+
<p>Hang on...</br></p>
11+
</div>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)