Repository navigation
Expand file tree
/
Copy pathstart.sh
More file actions
397 lines (360 loc) · 12.8 KB
/
Copy pathstart.sh
File metadata and controls
397 lines (360 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env bash
# Giraffe AI Launcher - Linux / macOS
#
# Serves the chat (index.html) on http://localhost:8000/ and proxies /v1/*
# to the local LLM named by the provider's endpoint (OpenAI-compatible, no API
# key); with no provider it falls back to http://127.0.0.1:8787.
# The browser talks to the same origin, so no CORS and no insecure flags.
#
# No external dependencies: uses python3 when available; on macOS it falls
# back to the Ruby that ships with the system. Both servers are implemented
# inline below (same role as the PowerShell server inside start.bat).
set -u
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PORT=8000
BACKEND_HOST="127.0.0.1"
BACKEND_PORT=8787
URL="http://localhost:${PORT}/"
SERVER_PID=""
# ---------- Parsing argomenti ----------
# --provider <json> (raw JSON, e.g. from AgentBridge) or an already URL-encoded value:
# the JSON is appended to the opened URL as ?provider=<url-encoded JSON> and the client
# registers/selects that provider on load (see index.html init()).
AUTO_PROVIDER=""
while [[ $# -gt 0 ]]; do
case "$1" in
--provider)
AUTO_PROVIDER="$2"
shift 2
;;
*)
echo "Parametro sconosciuto: $1"
exit 1
;;
esac
done
if [ -n "$AUTO_PROVIDER" ]; then
if [[ "$AUTO_PROVIDER" == \{* ]]; then
# Raw JSON: URL-encode it with python3 or ruby (same interpreters used by the server).
if command -v python3 >/dev/null 2>&1; then
ENCODED_PROVIDER=$(printf '%s' "$AUTO_PROVIDER" | python3 -c "import sys,urllib.parse; print(urllib.parse.quote(sys.stdin.read()))")
elif command -v ruby >/dev/null 2>&1; then
ENCODED_PROVIDER=$(printf '%s' "$AUTO_PROVIDER" | ruby -r uri -e 'puts URI.encode_www_form_component(STDIN.read)')
else
echo "ERRORE: nessun interprete disponibile per codificare il JSON (python3 o ruby)." >&2
exit 1
fi
else
# Already URL-encoded: use as-is.
ENCODED_PROVIDER="$AUTO_PROVIDER"
fi
URL="${URL}?provider=${ENCODED_PROVIDER}"
fi
# ---------- derive the proxy backend from the provider endpoint ----------
# The launcher forwards /v1/* to the local LLM/AgentBridge backend. That backend's
# host:port must come from the provider endpoint the host passed (e.g. AgentBridge on
# http://localhost:5290), NOT a hardcoded port — otherwise the proxy connects to a
# port nothing is listening on and every chat fails with a 502. Falls back to the
# historical default 127.0.0.1:8787 when no provider is passed or it can't be parsed.
UPSTREAM_HOST="127.0.0.1"
UPSTREAM_PORT="8787"
if [ -n "$AUTO_PROVIDER" ]; then
RAW_PROVIDER="$AUTO_PROVIDER"
if [[ "$AUTO_PROVIDER" != \{* ]]; then
# Already URL-encoded: decode before parsing.
if command -v python3 >/dev/null 2>&1; then
RAW_PROVIDER=$(printf '%s' "$AUTO_PROVIDER" | python3 -c "import sys,urllib.parse; print(urllib.parse.unquote(sys.stdin.read()))")
elif command -v ruby >/dev/null 2>&1; then
RAW_PROVIDER=$(printf '%s' "$AUTO_PROVIDER" | ruby -r uri -e 'puts URI.decode_www_form_component(STDIN.read)')
fi
fi
if command -v python3 >/dev/null 2>&1; then
UPSTREAM_LINE=$(printf '%s' "$RAW_PROVIDER" | python3 -c "import sys,json;from urllib.parse import urlparse;e=json.load(sys.stdin).get('endpoint','');u=urlparse(e);print((u.hostname or '')+' '+(str(u.port) if u.port else ''))" 2>/dev/null)
elif command -v ruby >/dev/null 2>&1; then
UPSTREAM_LINE=$(printf '%s' "$RAW_PROVIDER" | ruby -r json -r uri -e 'e=(JSON.parse(STDIN.read)["endpoint"]||"");u=URI.parse(e);puts [(u.host||""),(u.port||"")].join(" ")' 2>/dev/null)
fi
read -r uh up <<< "$UPSTREAM_LINE"
[ -n "$uh" ] && UPSTREAM_HOST="$uh"
[ -n "$up" ] && UPSTREAM_PORT="$up"
fi
# ---------- [0] OS detection ----------
case "$(uname -s)" in
Darwin) OS_NAME="macos" ;;
*) OS_NAME="linux" ;;
esac
echo "========================================"
echo " GIRAFFE AI - LAUNCHER (${OS_NAME})"
echo "========================================"
echo
# ---------- helpers ----------
port_in_use() {
(exec 3<>"/dev/tcp/${BACKEND_HOST}/${PORT}") 2>/dev/null && { exec 3>&- 3<&-; return 0; }
return 1
}
# ---------- [1/4] port check ----------
echo "[1/4] Checking port ${PORT}..."
if port_in_use; then
echo "PORT ${PORT} ALREADY IN USE - Server already running"
else
echo "PORT ${PORT} FREE - Starting server..."
fi
# ---------- [2/4] start server ----------
start_server() {
if command -v python3 >/dev/null 2>&1; then
echo "[2/4] Starting server (python3)..."
UPSTREAM_HOST="$UPSTREAM_HOST" UPSTREAM_PORT="$UPSTREAM_PORT" python3 - "$SCRIPT_DIR" "$PORT" <<'PY' &
import http.client, os, signal, sys, threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import urlparse, unquote
ROOT = sys.argv[1]
PORT = int(sys.argv[2])
BACKEND_HOST = os.environ.get('UPSTREAM_HOST', '127.0.0.1')
BACKEND_PORT = int(os.environ.get('UPSTREAM_PORT', '8787'))
FORWARD_HEADERS = ('content-type', 'authorization', 'accept')
class H(BaseHTTPRequestHandler):
protocol_version = 'HTTP/1.1'
def log_message(self, fmt, *args):
pass
def do_GET(self):
self.route('GET')
def do_POST(self):
self.route('POST')
def route(self, method):
u = urlparse(self.path)
path = unquote(u.path)
if path.startswith('/v1/'):
self.proxy(method, path, u)
else:
self.static(path)
def static(self, path):
if path == '/':
path = '/index.html'
root = os.path.normpath(ROOT)
full = os.path.normpath(os.path.join(ROOT, path.lstrip('/')))
if full != root and not full.startswith(root + os.sep):
self.send_error(403)
return
if not os.path.isfile(full):
body = b'<html><body><h1>404 - File not found</h1></body></html>'
self.send_response(404)
self.send_header('Content-Type', 'text/html')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
self.wfile.write(body)
return
with open(full, 'rb') as f:
data = f.read()
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.send_header('Content-Length', str(len(data)))
self.end_headers()
self.wfile.write(data)
def proxy(self, method, path, u):
length = int(self.headers.get('Content-Length', 0) or 0)
body = self.rfile.read(length) if length else None
headers = {k: v for k, v in self.headers.items() if k.lower() in FORWARD_HEADERS}
target = path + (('?' + u.query) if u.query else '')
conn = http.client.HTTPConnection(BACKEND_HOST, BACKEND_PORT, timeout=600)
try:
conn.request(method, target, body=body, headers=headers)
resp = conn.getresponse()
self.send_response(resp.status)
self.send_header('Content-Type', resp.getheader('Content-Type') or 'application/json')
self.send_header('Transfer-Encoding', 'chunked')
self.end_headers()
try:
while True:
chunk = resp.read(8192)
if not chunk:
break
self.wfile.write(('%x\r\n' % len(chunk)).encode('ascii') + chunk + b'\r\n')
self.wfile.flush()
finally:
self.wfile.write(b'0\r\n\r\n')
self.wfile.flush()
finally:
conn.close()
server = ThreadingHTTPServer(('127.0.0.1', PORT), H)
def _stop(sig, frame):
threading.Thread(target=server.shutdown, daemon=True).start()
signal.signal(signal.SIGTERM, _stop)
signal.signal(signal.SIGINT, _stop)
server.serve_forever()
PY
elif [ "$OS_NAME" = "macos" ] && command -v ruby >/dev/null 2>&1; then
echo "[2/4] Starting server (ruby, macOS built-in)..."
UPSTREAM_HOST="$UPSTREAM_HOST" UPSTREAM_PORT="$UPSTREAM_PORT" ruby - "$SCRIPT_DIR" "$PORT" <<'RB' &
require 'webrick'
require 'socket'
ROOT = File.expand_path(ARGV[0])
PORT = Integer(ARGV[1])
BACKEND = ENV['UPSTREAM_HOST'] || '127.0.0.1'
BACKEND_PORT = (ENV['UPSTREAM_PORT'] || '8787').to_i
class ChunkedReader
def initialize(sock, seed = '')
@sock = sock
@buf = seed.dup
end
def read_line
@buf << @sock.readpartial(16384) until (i = @buf.index("\r\n"))
line = @buf[0...i]
@buf = @buf[(i + 2)..-1] || +''
line
end
def read_exact(n)
@buf << @sock.readpartial(16384) while @buf.bytesize < n
data = @buf[0...n]
@buf = @buf[n..-1] || +''
data
end
# prossimo chunk decodificato; nil a fine stream
def next_chunk
size = read_line.to_i(16)
return nil if size.zero?
data = read_exact(size)
read_line
data
end
end
class Handler < WEBrick::HTTPServlet::AbstractServlet
def service(req, res)
if req.path.start_with?('/v1/')
proxy(req, res)
else
static(req, res)
end
end
def static(req, res)
path = req.path == '/' ? '/index.html' : req.path
full = File.expand_path(File.join(ROOT, path))
if full.start_with?(ROOT + File::SEPARATOR) && File.file?(full)
res.status = 200
res['Content-Type'] = 'text/html'
res.body = File.binread(full)
else
res.status = 404
res['Content-Type'] = 'text/html'
res.body = '<html><body><h1>404 - File not found</h1></body></html>'
end
end
def proxy(req, res)
body = req.body.to_s
request_line = "#{req.request_method} #{req.path} HTTP/1.1\r\n"
headers = "Host: #{BACKEND}:#{BACKEND_PORT}\r\n"
headers << "Content-Type: #{req['content-type'] || 'application/json'}\r\n"
headers << "Content-Length: #{body.bytesize}\r\n" unless body.empty?
headers << "Connection: close\r\n\r\n"
begin
sock = TCPSocket.new(BACKEND, BACKEND_PORT)
rescue StandardError
res.status = 502
res['Content-Type'] = 'application/json'
res.body = '{"error":"backend unreachable"}'
return
end
sock.write(request_line + headers)
sock.write(body) unless body.empty?
head = +''
loop do
head << sock.readpartial(16384)
break if head.include?("\r\n\r\n")
end
sep = head.index("\r\n\r\n")
rest = head[(sep + 4)..-1] || ''
head = head[0...sep]
res.status = head[/\AHTTP\/1\.[01] (\d+)/, 1].to_i
res['Content-Type'] = head[/\r\nContent-Type: ([^\r\n]+)/i, 1] || 'text/event-stream'
chunked = !!(head =~ /transfer-encoding:\s*chunked/i)
if chunked
res.chunked = true
else
len = head[/\r\ncontent-length:\s*(\d+)/i, 1]
res['Content-Length'] = len if len
end
r, w = IO.pipe
Thread.new do
begin
if chunked
cr = ChunkedReader.new(sock, rest)
while (chunk = cr.next_chunk)
w.write(chunk)
end
else
w.write(rest) unless rest.empty?
loop { w.write(sock.readpartial(16384)) }
end
rescue EOFError, IOError, Errno::ECONNRESET, Errno::EPIPE
ensure
begin; sock.close; rescue StandardError; end
w.close
end
end
res.body = r
end
end
server = WEBrick::HTTPServer.new(
BindAddress: '127.0.0.1',
Port: PORT,
Logger: WEBrick::Log.new('/dev/null'),
AccessLog: []
)
server.mount('/', Handler)
trap('TERM') { server.shutdown }
trap('INT') { server.shutdown }
server.start
RB
else
echo "ERROR: no python3 found. Install python3 (Linux) or Xcode Command Line Tools (macOS) and retry." >&2
exit 1
fi
SERVER_PID=$!
}
if ! port_in_use; then
start_server
i=0
while [ "$i" -lt 40 ]; do
port_in_use && break
sleep 0.25
i=$((i + 1))
done
if ! port_in_use; then
echo "ERROR: server did not start on port ${PORT}." >&2
exit 1
fi
echo "SERVER STARTED ON PORT ${PORT} (pid ${SERVER_PID})"
else
echo "[2/4] Server already listening - skipping start"
fi
# ---------- [3/4] open browser ----------
echo
echo "[3/4] Opening browser at ${URL}..."
if [ "$OS_NAME" = "macos" ]; then
if command -v open >/dev/null 2>&1; then
open "$URL"
else
echo "No 'open' command found. Open this URL manually: ${URL}"
fi
else
if command -v xdg-open >/dev/null 2>&1; then
xdg-open "$URL" >/dev/null 2>&1 &
elif command -v x-www-browser >/dev/null 2>&1; then
x-www-browser "$URL" >/dev/null 2>&1 &
elif command -v sensible-browser >/dev/null 2>&1; then
sensible-browser "$URL" >/dev/null 2>&1 &
elif command -v google-chrome >/dev/null 2>&1; then
google-chrome --app="$URL" >/dev/null 2>&1 &
elif command -v chromium >/dev/null 2>&1; then
chromium --app="$URL" >/dev/null 2>&1 &
elif command -v firefox >/dev/null 2>&1; then
firefox "$URL" >/dev/null 2>&1 &
else
echo "No browser launcher found. Open this URL manually: ${URL}"
fi
fi
# ---------- [4/4] done ----------
echo
echo "[4/4] Server running at ${URL}"
if [ -n "$SERVER_PID" ]; then
echo " (server pid ${SERVER_PID}; stop it with: kill ${SERVER_PID})"
fi
echo