diff --git a/scripts/podx-tools.sh b/scripts/podx-tools.sh index ea377aa..61c247f 100755 --- a/scripts/podx-tools.sh +++ b/scripts/podx-tools.sh @@ -105,21 +105,16 @@ _kb_id_by_name() { local kb_name="$1" _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" _require "OPENWEBUI_URL" "$OPENWEBUI_URL" - curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ - "$(_owui_url)/api/v1/knowledge/list" \ - | python3 - "$kb_name" <<'PY' -import sys,json,os -name=sys.argv[1] + local url + url="$(_owui_url)/api/v1/knowledge/list" + curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" "$url" \ + | python3 -c 'import sys,json,os; name=os.environ.get("KB_NAME"); try: data=json.load(sys.stdin) except Exception: - print("", end="") - sys.exit(0) -for kb in data: - if kb.get("name")==name: - print(kb.get("id",""), end="") - break -PY + print("", end=""); sys.exit(0) +print(next((kb.get("id","") for kb in data if kb.get("name")==name), ""), end="")' \ + KB_NAME="$kb_name" } _help() { @@ -306,26 +301,43 @@ print(f\"Indexed {n} document(s).\")" FILE_ID="$(python3 -c 'import sys,json; d=json.load(sys.stdin); print(d.get("id") or (d.get("data") or {}).get("id",""))' <<<"$FILE_JSON")" if [ -z "$FILE_ID" ]; then echo "Upload failed (no file id)"; exit 1; fi - # 2) Attach + # 2) Resolve KB and attach KB_ID="$(_kb_id_by_name "$kb_name")" echo "[owui] attaching to KB: $kb_name (id: $KB_ID)" if [ -z "$KB_ID" ]; then echo "KB '$kb_name' not found"; exit 1; fi - tmp_body="$(_mktemp)"; tmp_code="$(_mktemp)" - curl -sS -X POST -H "Authorization: Bearer $OPENWEBUI_API_KEY" -H "Content-Type: application/json" \ + # Attach: capture headers, body, http code and curl exit + tmp_body="$(_mktemp)"; tmp_code="$(_mktemp)"; tmp_hdrs="$(_mktemp)" + curl -sS -X POST \ + -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ + -H "Content-Type: application/json" \ -d "{\"file_id\":\"$FILE_ID\"}" \ - -w "%{http_code}" --output "$tmp_body" "$(_owui_url)/api/v1/knowledge/$KB_ID/file/add" >"$tmp_code" || true + -D "$tmp_hdrs" \ + -w "%{http_code}" --output "$tmp_body" \ + "$(_owui_url)/api/v1/knowledge/$KB_ID/file/add" >"$tmp_code" || true curl_exit=$?; http_code="$(cat "$tmp_code" 2>/dev/null || echo 0)" - RESP="$(cat "$tmp_body")" - rm -f "$tmp_body" "$tmp_code" + echo "[owui] response headers:"; sed -n '1,5p' "$tmp_hdrs" || true + RESP="$(cat "$tmp_body")" echo "$RESP" | ppjson + + rm -f "$tmp_body" "$tmp_code" "$tmp_hdrs" + if [ $curl_exit -ne 0 ]; then echo "Attach failed: curl exit $curl_exit" >&2; exit $curl_exit fi - if [ "$http_code" != "200" ]; then - echo "Attach failed (HTTP $http_code)" >&2; exit 1 + # Some environments report curl(23) sporadically; treat missing code as non-200 + if [ -z "$http_code" ] || [ "$http_code" = "000" ]; then + echo "Attach failed: no HTTP code returned" >&2; exit 1 fi + case "$http_code" in + 200|201|204) + : # success + ;; + *) + echo "Attach failed (HTTP $http_code)" >&2; exit 1 + ;; + esac ;; owui-kb-files)