Patchin...

This commit is contained in:
2025-09-08 11:52:14 +02:00
parent 733077b527
commit b9eaaef87c

View File

@@ -128,10 +128,15 @@ _kb_create() {
_kb_id_by_name() { _kb_id_by_name() {
local kb_name="$1" local kb_name="$1"
local json; json="$(_owui_get_kb_list)" local json; json="$(_owui_get_kb_list)"
# Normalize to a flat list and choose the most recently updated match (robust name matching) # Robust KB name resolver: Unicode/space-insensitive, prefers most recently updated
python3 - "$kb_name" <<'PY' || true python3 - "$kb_name" <<'PY' || true
import sys, json import sys, json, unicodedata as ud
want = (sys.argv[1] or "").strip()
def norm(s: str) -> str:
s = ud.normalize('NFKC', s or '')
return s.strip().casefold()
want = norm(sys.argv[1] if len(sys.argv) > 1 else '')
raw = sys.stdin.read().strip() raw = sys.stdin.read().strip()
if not raw: if not raw:
sys.exit(0) sys.exit(0)
@@ -139,36 +144,26 @@ try:
d = json.loads(raw) d = json.loads(raw)
except Exception: except Exception:
sys.exit(0) sys.exit(0)
# Support both list and {data:[...]}
if isinstance(d, dict) and isinstance(d.get("data"), list):
items = d["data"]
elif isinstance(d, list):
items = d
else:
items = []
def ts(kb): items = d.get('data') if isinstance(d, dict) and isinstance(d.get('data'), list) else (d if isinstance(d, list) else [])
for key in ("updated_at", "created_at"):
v = kb.get(key)
if isinstance(v, (int, float)):
return int(v)
return 0
# Helper to normalize a KB name for comparison # If only one KB exists and a name was provided, return it
norm = lambda s: (s or "").strip().casefold() if want and isinstance(items, list) and len(items) == 1:
want_n = norm(want) print(items[0].get('id',''), end=''); sys.exit(0)
# Try exact match (case/space-insensitive) # Prefer exact name match (normalized)
exact = [kb for kb in items if norm(kb.get("name")) == want_n] exact = [kb for kb in items if norm(kb.get('name')) == want]
if exact: if exact:
print(sorted(exact, key=ts, reverse=True)[0].get("id", ""), end=""); sys.exit(0) exact.sort(key=lambda kb: int(kb.get('updated_at') or kb.get('created_at') or 0), reverse=True)
print(exact[0].get('id',''), end=''); sys.exit(0)
# Then case-insensitive substring match # Fallback: substring match
subs = [kb for kb in items if want_n and want_n in norm(kb.get("name"))] subs = [kb for kb in items if want and want in norm(kb.get('name'))]
if subs: if subs:
print(sorted(subs, key=ts, reverse=True)[0].get("id", ""), end=""); sys.exit(0) subs.sort(key=lambda kb: int(kb.get('updated_at') or kb.get('created_at') or 0), reverse=True)
print(subs[0].get('id',''), end=''); sys.exit(0)
print("", end="") print('', end='')
PY PY
} }
@@ -191,6 +186,7 @@ OpenWebUI:
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-kb-id-all "<KB Name>" # list all matching KB ids (if duplicates exist) owui-kb-id-all "<KB Name>" # list all matching KB ids (if duplicates exist)
owui-kb-resolve "<KB Name>" # debug name->id resolution with raw listing owui-kb-resolve "<KB Name>" # debug name->id resolution with raw listing
# Name resolution is Unicode/space-insensitive and prefers the most recently updated match.
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-attach-id <KB_ID> </abs/path/file> # upload + attach using explicit KB id owui-attach-id <KB_ID> </abs/path/file> # upload + attach using explicit KB id
@@ -353,25 +349,33 @@ PY
echo "[owui] http_code=$http_code bytes=$bytes" echo "[owui] http_code=$http_code bytes=$bytes"
json="$(cat "$tmp_body")"; rm -f "$tmp_body" json="$(cat "$tmp_body")"; rm -f "$tmp_body"
id="$(python3 - "$name" <<'PY' id="$(python3 - "$name" <<'PY'
import sys, json import sys, json, unicodedata as ud
want = (sys.argv[1] or "").strip()
def norm(s: str) -> str:
s = ud.normalize('NFKC', s or '')
return s.strip().casefold()
want = norm(sys.argv[1] if len(sys.argv) > 1 else '')
raw = sys.stdin.read() raw = sys.stdin.read()
try: try:
d = json.loads(raw) d = json.loads(raw)
except Exception: except Exception:
print("", end=""); sys.exit(0) print('', end=''); sys.exit(0)
items = d.get("data") if isinstance(d, dict) and isinstance(d.get("data"), list) else (d if isinstance(d, list) else []) items = d.get('data') if isinstance(d, dict) and isinstance(d.get('data'), list) else (d if isinstance(d, list) else [])
norm = lambda s: (s or "").strip().casefold()
want_n = norm(want) # Single-KB fallback
def ts(kb): if want and isinstance(items, list) and len(items) == 1:
for k in ("updated_at","created_at"): print(items[0].get('id',''), end=''); sys.exit(0)
v = kb.get(k)
if isinstance(v,(int,float)): return int(v) exact = [kb for kb in items if norm(kb.get('name')) == want]
return 0 subs = [kb for kb in items if want and want in norm(kb.get('name'))]
exact = [kb for kb in items if norm(kb.get("name")) == want_n]
subs = [kb for kb in items if want_n and want_n in norm(kb.get("name"))]
cands = exact or subs cands = exact or subs
print((sorted(cands, key=ts, reverse=True)[0].get("id","")) if cands else "", end="") if cands:
cands.sort(key=lambda kb: int(kb.get('updated_at') or kb.get('created_at') or 0), reverse=True)
print(cands[0].get('id',''), end='')
else:
print('', end='')
PY PY
<<<"$json")" <<<"$json")"
if [ -n "$id" ]; then if [ -n "$id" ]; then