diff --git a/scripts/podx-tools.sh b/scripts/podx-tools.sh index 123acd1..31b9bfe 100755 --- a/scripts/podx-tools.sh +++ b/scripts/podx-tools.sh @@ -122,10 +122,10 @@ _kb_create() { _kb_id_by_name() { local kb_name="$1" local json; json="$(_owui_get_kb_list)" - # Normalize to a flat list and choose the most recently updated match + # Normalize to a flat list and choose the most recently updated match (robust name matching) python3 - "$kb_name" <<'PY' || true import sys, json -name = sys.argv[1] +want = (sys.argv[1] or "").strip() raw = sys.stdin.read().strip() if not raw: sys.exit(0) @@ -133,7 +133,8 @@ try: d = json.loads(raw) except Exception: sys.exit(0) -if isinstance(d, dict) and "data" in d and isinstance(d["data"], list): +# Support both list and {data:[...]} +if isinstance(d, dict) and isinstance(d.get("data"), list): items = d["data"] elif isinstance(d, list): items = d @@ -147,15 +148,20 @@ def ts(kb): return int(v) return 0 -exact = sorted([kb for kb in items if kb.get("name") == name], key=ts, reverse=True) +# Helper to normalize a KB name for comparison +norm = lambda s: (s or "").strip().casefold() +want_n = norm(want) + +# Try exact match (case/space-insensitive) +exact = [kb for kb in items if norm(kb.get("name")) == want_n] if exact: - print(exact[0].get("id", ""), end=""); sys.exit(0) -iexact = sorted([kb for kb in items if isinstance(kb.get("name"), str) and kb["name"].lower() == name.lower()], key=ts, reverse=True) -if iexact: - print(iexact[0].get("id", ""), end=""); sys.exit(0) -sub = sorted([kb for kb in items if isinstance(kb.get("name"), str) and name.lower() in kb["name"].lower()], key=ts, reverse=True) -if sub: - print(sub[0].get("id", ""), end=""); sys.exit(0) + print(sorted(exact, key=ts, reverse=True)[0].get("id", ""), end=""); sys.exit(0) + +# Then case-insensitive substring match +subs = [kb for kb in items if want_n and want_n in norm(kb.get("name"))] +if subs: + print(sorted(subs, key=ts, reverse=True)[0].get("id", ""), end=""); sys.exit(0) + print("", end="") PY } @@ -177,6 +183,7 @@ 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-upload # upload a file, prints file_id owui-attach "" # upload + attach to KB owui-kb-create "" # create a KB (prints JSON with id) @@ -299,6 +306,21 @@ print(f\"Indexed {n} document(s).\")" echo "$_id" ;; + owui-kb-resolve) + shift || true + name="${1:-}" + if [ -z "$name" ]; then echo "Usage: owui-kb-resolve \"\"" >&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-upload) shift || true file="${1:-}" @@ -361,7 +383,7 @@ print(f\"Indexed {n} document(s).\")" echo "[owui] attaching to KB: $kb_name (id: $KB_ID)" if [ -z "$KB_ID" ]; then echo "KB '$kb_name' not found (or ambiguous)." >&2 - echo "Tip: run './scripts/podx-tools.sh owui-kb-debug \"$kb_name\"' or create one:" >&2 + echo "Tip: run './scripts/podx-tools.sh owui-kb-resolve \"$kb_name\"' to inspect and resolve, or create one:" >&2 echo " ./scripts/podx-tools.sh owui-kb-create \"$kb_name\"" >&2 exit 1 fi