Fixing the fixes

This commit is contained in:
2025-09-08 11:23:42 +02:00
parent 9aa439d390
commit f6442a7b06

View File

@@ -101,66 +101,63 @@ _require() {
fi fi
} }
_kb_id_by_name() { _owui_get_kb_list() {
_require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY"
_require "OPENWEBUI_URL" "$OPENWEBUI_URL"
local url; url="$(_owui_url)/api/v1/knowledge/list"
curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" "$url"
}
_kb_create() {
local kb_name="$1" local kb_name="$1"
_require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY"
_require "OPENWEBUI_URL" "$OPENWEBUI_URL" _require "OPENWEBUI_URL" "$OPENWEBUI_URL"
local url curl -sS -X POST \
url="$(_owui_url)/api/v1/knowledge/list" -H "Authorization: Bearer $OPENWEBUI_API_KEY" \
-H "Content-Type: application/json" \
-d "{\"name\":\"$kb_name\",\"description\":\"Created by podx-tools\"}" \
"$(_owui_url)/api/v1/knowledge/create"
}
# Fetch list once _kb_id_by_name() {
local body tmp_code local kb_name="$1"
body="$(_mktemp)"; tmp_code="$(_mktemp)" local json; json="$(_owui_get_kb_list)"
curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ # Normalize to a flat list and choose the most recently updated match
-w "%{http_code}" --output "$body" "$url" >"$tmp_code" || true python3 - "$kb_name" <<'PY' || true
local http_code; http_code="$(cat "$tmp_code" 2>/dev/null || echo 0)"; rm -f "$tmp_code"
# If non-200, return empty
if [ "$http_code" != "200" ]; then
rm -f "$body"; echo ""; return
fi
# Parse with python: prefer exact name match; if multiple, choose by latest updated_at; else try substring match
local id
id="$(python3 - "$kb_name" <"$body" 2>/dev/null <<'PY'
import sys, json import sys, json
name = sys.argv[1] name = sys.argv[1]
raw = sys.stdin.read().strip()
if not raw:
sys.exit(0)
try: try:
data = json.load(sys.stdin) d = json.loads(raw)
except Exception: except Exception:
print("")
sys.exit(0) sys.exit(0)
if isinstance(d, dict) and "data" in d and isinstance(d["data"], list):
items = d["data"]
elif isinstance(d, list):
items = d
else:
items = []
# Normalize to list def ts(kb):
if not isinstance(data, list): for key in ("updated_at", "created_at"):
print("") v = kb.get(key)
sys.exit(0) if isinstance(v, (int, float)):
return int(v)
return 0
candidates = [kb for kb in data if (kb.get('name') or '') == name] exact = sorted([kb for kb in items if kb.get("name") == name], key=ts, reverse=True)
# If nothing exact, try substring (case-insensitive) if exact:
if not candidates: print(exact[0].get("id", ""), end=""); sys.exit(0)
lname = name.lower() iexact = sorted([kb for kb in items if isinstance(kb.get("name"), str) and kb["name"].lower() == name.lower()], key=ts, reverse=True)
candidates = [kb for kb in data if lname in (kb.get('name') or '').lower()] if iexact:
print(iexact[0].get("id", ""), end=""); sys.exit(0)
if not candidates: sub = sorted([kb for kb in items if isinstance(kb.get("name"), str) and name.lower() in kb["name"].lower()], key=ts, reverse=True)
print("") if sub:
sys.exit(0) print(sub[0].get("id", ""), end=""); sys.exit(0)
print("", end="")
# Choose the one with the newest updated_at (numeric seconds); fall back to created_at; else first.
from operator import itemgetter
def ts(kb, key):
try:
return int(kb.get(key) or 0)
except Exception:
return 0
best = sorted(candidates, key=lambda kb: (ts(kb,'updated_at'), ts(kb,'created_at')), reverse=True)[0]
print(best.get('id',''))
PY PY
)"
rm -f "$body"
echo "$id"
} }
_help() { _help() {
@@ -179,12 +176,11 @@ Meilisearch:
OpenWebUI: OpenWebUI:
owui-health # check API health (200) owui-health # check API health (200)
owui-kbs # list knowledge bases owui-kbs # list knowledge bases
owui-kbs-raw # list knowledge bases raw JSON (debug)
owui-kb-debug "<KB Name>" # debug KB id resolution
owui-kb-id "<KB Name>" # print the KB UUID by exact name owui-kb-id "<KB Name>" # print the KB UUID by exact name
owui-upload </abs/path/file> # upload a file, prints file_id owui-upload </abs/path/file> # upload a file, prints file_id
owui-attach "<KB Name>" </abs/path/file> # upload + attach to KB owui-attach "<KB Name>" </abs/path/file> # upload + attach to KB
owui-kb-files "<KB Name>" # list files in KB owui-kb-create "<KB Name>" # create a KB (prints JSON with id)
owui-kbs-raw # raw JSON from /knowledge/list
owui-batch-attach "<KB Name>" <glob> # attach all files matching glob owui-batch-attach "<KB Name>" <glob> # attach all files matching glob
Examples: Examples:
@@ -284,24 +280,14 @@ print(f\"Indexed {n} document(s).\")"
;; ;;
owui-kbs-raw) owui-kbs-raw)
_require "OPENWEBUI_URL" "$OPENWEBUI_URL" _owui_get_kb_list | ppjson
_require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY"
curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \
"$(_owui_url)/api/v1/knowledge/list"
;; ;;
owui-kb-debug) owui-kb-create)
shift || true shift || true
name="${1:-}" name="${1:-}"
if [ -z "$name" ]; then echo "Usage: owui-kb-debug \"<KB Name>\"" >&2; exit 1; fi if [ -z "$name" ]; then echo "Usage: owui-kb-create \"<KB Name>\"" >&2; exit 1; fi
_require "OPENWEBUI_URL" "$OPENWEBUI_URL" _kb_create "$name" | ppjson
_require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY"
echo "[debug] Resolved KB URL: $(_owui_url)"
echo "[debug] Looking for KB name: $name"
echo "[debug] Listing KBs (pretty):"
curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \
"$(_owui_url)/api/v1/knowledge/list" | ppjson
echo "[debug] Resolved KB id: $(_kb_id_by_name "$name")"
;; ;;
owui-kb-id) owui-kb-id)
@@ -375,7 +361,8 @@ print(f\"Indexed {n} document(s).\")"
echo "[owui] attaching to KB: $kb_name (id: $KB_ID)" echo "[owui] attaching to KB: $kb_name (id: $KB_ID)"
if [ -z "$KB_ID" ]; then if [ -z "$KB_ID" ]; then
echo "KB '$kb_name' not found (or ambiguous)." >&2 echo "KB '$kb_name' not found (or ambiguous)." >&2
echo "Tip: run './scripts/podx-tools.sh owui-kb-debug \"$kb_name\"' to inspect the KB list and resolved id." >&2 echo "Tip: run './scripts/podx-tools.sh owui-kb-debug \"$kb_name\"' or create one:" >&2
echo " ./scripts/podx-tools.sh owui-kb-create \"$kb_name\"" >&2
exit 1 exit 1
fi fi
@@ -425,6 +412,21 @@ print(f\"Indexed {n} document(s).\")"
"$(_owui_url)/api/v1/knowledge/$KB_ID/files" | ppjson "$(_owui_url)/api/v1/knowledge/$KB_ID/files" | ppjson
;; ;;
owui-kb-debug)
shift || true
name="${1:-}"
if [ -z "$name" ]; then echo "Usage: owui-kb-debug \"<KB Name>\"" >&2; exit 1; fi
echo "[owui] base URL: $(_owui_url)"
echo "[owui] KBs returned:"
_owui_get_kb_list | ppjson
id="$(_kb_id_by_name "$name")"
if [ -n "$id" ]; then
echo "[owui] resolved id for \"$name\": $id"
else
echo "[owui] could not resolve an id for \"$name\"" >&2
fi
;;
owui-batch-attach) owui-batch-attach)
shift || true shift || true
kb_name="${1:-}"; glob_pat="${2:-}" kb_name="${1:-}"; glob_pat="${2:-}"