This commit is contained in:
2025-09-08 11:06:59 +02:00
parent 6a1d30e073
commit f2be1b97fa

View File

@@ -105,21 +105,16 @@ _kb_id_by_name() {
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"
curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ local url
"$(_owui_url)/api/v1/knowledge/list" \ url="$(_owui_url)/api/v1/knowledge/list"
| python3 - "$kb_name" <<'PY' curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" "$url" \
import sys,json,os | python3 -c 'import sys,json,os; name=os.environ.get("KB_NAME");
name=sys.argv[1]
try: try:
data=json.load(sys.stdin) data=json.load(sys.stdin)
except Exception: except Exception:
print("", end="") print("", end=""); sys.exit(0)
sys.exit(0) print(next((kb.get("id","") for kb in data if kb.get("name")==name), ""), end="")' \
for kb in data: KB_NAME="$kb_name"
if kb.get("name")==name:
print(kb.get("id",""), end="")
break
PY
} }
_help() { _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")" 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 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")" KB_ID="$(_kb_id_by_name "$kb_name")"
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 echo "KB '$kb_name' not found"; exit 1; fi if [ -z "$KB_ID" ]; then echo "KB '$kb_name' not found"; exit 1; fi
tmp_body="$(_mktemp)"; tmp_code="$(_mktemp)" # Attach: capture headers, body, http code and curl exit
curl -sS -X POST -H "Authorization: Bearer $OPENWEBUI_API_KEY" -H "Content-Type: application/json" \ 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 "{\"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)" 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 echo "$RESP" | ppjson
rm -f "$tmp_body" "$tmp_code" "$tmp_hdrs"
if [ $curl_exit -ne 0 ]; then if [ $curl_exit -ne 0 ]; then
echo "Attach failed: curl exit $curl_exit" >&2; exit $curl_exit echo "Attach failed: curl exit $curl_exit" >&2; exit $curl_exit
fi fi
if [ "$http_code" != "200" ]; then # Some environments report curl(23) sporadically; treat missing code as non-200
echo "Attach failed (HTTP $http_code)" >&2; exit 1 if [ -z "$http_code" ] || [ "$http_code" = "000" ]; then
echo "Attach failed: no HTTP code returned" >&2; exit 1
fi fi
case "$http_code" in
200|201|204)
: # success
;;
*)
echo "Attach failed (HTTP $http_code)" >&2; exit 1
;;
esac
;; ;;
owui-kb-files) owui-kb-files)