diff --git a/scripts/podx-tools.sh b/scripts/podx-tools.sh index 82c6fdb..0ab8def 100755 --- a/scripts/podx-tools.sh +++ b/scripts/podx-tools.sh @@ -63,6 +63,25 @@ fi : "${OPENWEBUI_URL:=http://localhost:3003}" : "${OPENWEBUI_API_KEY:=}" +# Resolve a working OpenWebUI base URL (fallback from host.docker.internal -> localhost) +_owui_url() { + local u="${OPENWEBUI_URL:-http://localhost:3003}" + # quick health check + local code + code=$(curl -sS -o /dev/null -w "%{http_code}" "$u/api/health" || true) + if [ "$code" != "200" ]; then + if [[ "$u" == *host.docker.internal* ]]; then + local alt="${u/host.docker.internal/localhost}" + code=$(curl -sS -o /dev/null -w "%{http_code}" "$alt/api/health" || true) + if [ "$code" = "200" ]; then + echo "$alt" + return + fi + fi + fi + echo "$u" +} + # ---------- Helpers ---------- _require() { local name="$1" val="${2:-}" @@ -77,7 +96,7 @@ _kb_id_by_name() { _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" _require "OPENWEBUI_URL" "$OPENWEBUI_URL" curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ - "$OPENWEBUI_URL/api/v1/knowledge/list" \ + "$(_owui_url)/api/v1/knowledge/list" \ | python3 - "$kb_name" <<'PY' import sys,json,os name=sys.argv[1] @@ -198,14 +217,14 @@ print(f\"Indexed {n} document(s).\")" # ---- OpenWebUI ---- owui-health) _require "OPENWEBUI_URL" "$OPENWEBUI_URL" - curl -sS -o /dev/null -w "%{http_code}\n" "$OPENWEBUI_URL/api/health" + curl -sS -o /dev/null -w "%{http_code}\n" "$(_owui_url)/api/health" ;; owui-kbs) _require "OPENWEBUI_URL" "$OPENWEBUI_URL" _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ - "$OPENWEBUI_URL/api/v1/knowledge/list" | ppjson + "$(_owui_url)/api/v1/knowledge/list" | ppjson ;; owui-kb-id) @@ -225,7 +244,7 @@ print(f\"Indexed {n} document(s).\")" _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ -F "file=@$file" \ - "$OPENWEBUI_URL/api/v1/files/" | ppjson + "$(_owui_url)/api/v1/files/" | ppjson ;; owui-attach) @@ -236,14 +255,14 @@ print(f\"Indexed {n} document(s).\")" fi _require "OPENWEBUI_URL" "$OPENWEBUI_URL" _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" - FILE_JSON="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -F "file=@$file" "$OPENWEBUI_URL/api/v1/files/")" + FILE_JSON="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -F "file=@$file" "$(_owui_url)/api/v1/files/")" FILE_ID="$(python3 -c 'import sys,json; d=json.loads(sys.stdin.read()); print(d.get("id") or (d.get("data") or {}).get("id",""))' <<<"$FILE_JSON")" if [ -z "$FILE_ID" ]; then echo "Upload failed (no file id)"; echo "$FILE_JSON" | ppjson; exit 1; fi KB_ID="$(_kb_id_by_name "$kb_name")" if [ -z "$KB_ID" ]; then echo "KB '$kb_name' not found"; exit 1; fi curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -H "Content-Type: application/json" \ -d "{\"file_id\":\"$FILE_ID\"}" \ - "$OPENWEBUI_URL/api/v1/knowledge/$KB_ID/file/add" | ppjson + "$(_owui_url)/api/v1/knowledge/$KB_ID/file/add" | ppjson ;; owui-kb-files) @@ -255,7 +274,7 @@ print(f\"Indexed {n} document(s).\")" KB_ID="$(_kb_id_by_name "$kb_name")" if [ -z "$KB_ID" ]; then echo "KB '$kb_name' not found"; exit 1; fi curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ - "$OPENWEBUI_URL/api/v1/knowledge/$KB_ID/files" | ppjson + "$(_owui_url)/api/v1/knowledge/$KB_ID/files" | ppjson ;; owui-batch-attach) @@ -274,12 +293,12 @@ print(f\"Indexed {n} document(s).\")" if [ ${#matched[@]} -eq 0 ]; then echo "No files match: $glob_pat"; exit 1; fi for f in "${matched[@]}"; do echo "[owui] uploading: $f" - FILE_JSON="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -F "file=@$f" "$OPENWEBUI_URL/api/v1/files/")" + FILE_JSON="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -F "file=@$f" "$(_owui_url)/api/v1/files/")" FILE_ID="$(python3 -c 'import sys,json; d=json.loads(sys.stdin.read()); print(d.get("id") or (d.get("data") or {}).get("id",""))' <<<"$FILE_JSON")" if [ -z "$FILE_ID" ]; then echo " upload failed, skipping"; continue; fi curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -H "Content-Type: application/json" \ -d "{\"file_id\":\"$FILE_ID\"}" \ - "$OPENWEBUI_URL/api/v1/knowledge/$KB_ID/file/add" | ppjson + "$(_owui_url)/api/v1/knowledge/$KB_ID/file/add" | ppjson done ;;