diff --git a/scripts/podx-tools.sh b/scripts/podx-tools.sh index 2120e43..b99663e 100755 --- a/scripts/podx-tools.sh +++ b/scripts/podx-tools.sh @@ -189,9 +189,11 @@ OpenWebUI: owui-health # check API health (200) owui-kbs # list knowledge bases owui-kb-id "" # print the KB UUID by exact name - owui-kb-resolve "" # debug name->id resolution with raw listing + owui-kb-id-all "" # list all matching KB ids (if duplicates exist) + owui-kb-resolve "" # debug name->id resolution with raw listing owui-upload # upload a file, prints file_id owui-attach "" # upload + attach to KB + owui-attach-id # upload + attach using explicit KB id owui-kb-create "" # create a KB (prints JSON with id) owui-kbs-raw # raw JSON from /knowledge/list owui-batch-attach "" # attach all files matching glob @@ -312,6 +314,29 @@ print(f\"Indexed {n} document(s).\")" echo "$_id" ;; + owui-kb-id-all) + shift || true + name="${1:-}" + if [ -z "$name" ]; then echo "Usage: owui-kb-id-all \"\"" >&2; exit 1; fi + _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" + _require "OPENWEBUI_URL" "$OPENWEBUI_URL" + _owui_get_kb_list | python3 - "$name" <<'PY' || exit 0 +import sys, json +want = (sys.argv[1] or "").strip() +raw = sys.stdin.read().strip() +try: + d = json.loads(raw) +except Exception: + sys.exit(0) +items = d.get("data") if isinstance(d, dict) and isinstance(d.get("data"), list) else (d if isinstance(d, list) else []) +def norm(s): return (s or "").strip().casefold() +want_n = norm(want) +matches = [kb for kb in items if norm(kb.get("name")) == want_n or (want_n and want_n in norm(kb.get("name")))] +for kb in matches: + print(f"{kb.get('id','')}\t{kb.get('name','')}\tcreated_at={kb.get('created_at','')}\tupdated_at={kb.get('updated_at','')}") +PY + ;; + owui-kb-resolve) shift || true name="${1:-}" @@ -457,6 +482,64 @@ PY esac ;; + owui-attach-id) + shift || true + kb_id="${1:-}"; file="${2:-}" + if [ -z "$kb_id" ] || [ -z "$file" ] || [ ! -f "$file" ]; then + echo "Usage: owui-attach-id " >&2; exit 1 + fi + _require "OPENWEBUI_URL" "$OPENWEBUI_URL" + _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" + + # 1) Upload + tmp_body="$(_mktemp)"; tmp_code="$(_mktemp)" + curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ + -F "file=@$file" \ + -w "%{http_code}" --output "$tmp_body" "$(_owui_url)/api/v1/files/" >"$tmp_code" || true + curl_exit=$?; http_code="$(cat "$tmp_code" 2>/dev/null || echo 0)" + FILE_JSON="$(cat "$tmp_body")" + rm -f "$tmp_body" "$tmp_code" + + echo "$FILE_JSON" | ppjson + if [ $curl_exit -ne 0 ]; then + echo "Upload failed: curl exit $curl_exit" >&2; exit $curl_exit + fi + if [ "$http_code" != "200" ]; then + echo "Upload failed (HTTP $http_code)" >&2; exit 1 + fi + + 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 using explicit KB id + 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\"}" \ + -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)" + + 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 [ -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) : ;; + *) echo "Attach failed (HTTP $http_code)" >&2; exit 1 ;; + esac + ;; + owui-kb-files) shift || true kb_name="${1:-}"