#!/usr/bin/env bash # podx-tools.sh — host-side helper for Meili + OpenWebUI # Usage: ./scripts/podx-tools.sh [args...] # Run without args to see help. set -euo pipefail # Determine repo root early (used by temp dir and .env loader) ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" # Writable temp dir (override with PODX_TMP or TMPDIR); default to repo tmp/ : "${PODX_TMP:=${TMPDIR:-$ROOT_DIR/tmp}}" mkdir -p "$PODX_TMP" # Portable mktemp helper that always writes inside $PODX_TMP (macOS/GNU compatible) _mktemp() { # mktemp on macOS doesn't support -p; use path template instead mktemp "$PODX_TMP/podx.XXXXXX" } # ---------- Pretty-print JSON (no jq required) ---------- ppjson() { if command -v python3 >/dev/null 2>&1; then python3 -m json.tool || cat elif command -v jq >/dev/null 2>&1; then jq . elif command -v node >/dev/null 2>&1; then node -e "let d='';process.stdin.on('data',c=>d+=c);process.stdin.on('end',()=>{try{console.log(JSON.stringify(JSON.parse(d),null,2))}catch{console.log(d)}})" || cat else cat fi } # ---------- Load .env from repo root (robust, handles spaces & ${VAR}) ---------- ENV_FILE="$ROOT_DIR/.env" if [ -f "$ENV_FILE" ]; then # Minimal .env parser: # - ignores comments/blank lines # - preserves spaces in values (quotes optional) # - expands ${VAR} references using current environment / previously set keys while IFS= read -r __line || [ -n "$__line" ]; do # trim leading/trailing whitespace __line="${__line#"${__line%%[![:space:]]*}"}" __line="${__line%"${__line##*[![:space:]]}"}" # skip empty or comment [ -z "$__line" ] && continue [ "${__line:0:1}" = "#" ] && continue # split at first '=' case "$__line" in *=*) __key="${__line%%=*}" __val="${__line#*=}" ;; *) continue ;; esac # strip surrounding quotes if present if [[ "$__val" == \"*\" && "$__val" == *\" ]]; then __val="${__val:1:${#__val}-2}" elif [[ "$__val" == \'*\' && "$__val" == *\' ]]; then __val="${__val:1:${#__val}-2}" fi # expand ${VAR} references eval "__val_expanded=\"${__val}\"" export "${__key}=${__val_expanded}" done < "$ENV_FILE" unset __line __key __val __val_expanded fi # ---------- Defaults (can be overridden by .env) ---------- : "${MEILI_URL:=http://localhost:7700}" : "${MEILI_KEY:=${MEILI_MASTER_KEY:-}}" : "${OPENWEBUI_URL:=http://localhost:3003}" : "${OPENWEBUI_URL_HOST:=}" : "${OPENWEBUI_API_KEY:=}" # Resolve a working OpenWebUI base URL (fallback from host.docker.internal -> localhost) _owui_url() { # If a host-only override is provided, prefer it unconditionally local host_u="${OPENWEBUI_URL_HOST:-}" if [ -n "$host_u" ]; then echo "$host_u" return fi # Otherwise use OPENWEBUI_URL (defaulting to localhost), and rewrite # host.docker.internal -> localhost so it always resolves on the host local u="${OPENWEBUI_URL:-http://localhost:3003}" u="${u//host.docker.internal/localhost}" echo "$u" } # ---------- Helpers ---------- _require() { local name="$1" val="${2:-}" if [ -z "$val" ]; then echo "Missing $name (set it in .env or env): $name" >&2 exit 1 fi } _kb_id_by_name() { local kb_name="$1" _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" _require "OPENWEBUI_URL" "$OPENWEBUI_URL" curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ "$(_owui_url)/api/v1/knowledge/list" \ | python3 - "$kb_name" <<'PY' import sys,json,os name=sys.argv[1] try: data=json.load(sys.stdin) except Exception: print("", end="") sys.exit(0) for kb in data: if kb.get("name")==name: print(kb.get("id",""), end="") break PY } _help() { cat <" [limit] # search the 'library' index meili-reindex # index all /transcripts/*.json via worker container OpenWebUI: owui-health # check API health (200) owui-kbs # list knowledge bases owui-kb-id "" # print the KB UUID by exact name owui-upload # upload a file, prints file_id owui-attach "" # upload + attach to KB owui-kb-files "" # list files in KB owui-batch-attach "" # attach all files matching glob Examples: ./scripts/podx-tools.sh meili-health ./scripts/podx-tools.sh meili-search "grand canyon" 10 ./scripts/podx-tools.sh owui-attach "Homelab Library" /mnt/.../episode.txt ./scripts/podx-tools.sh owui-kb-files "Homelab Library" Environment comes from .env at repo root (MEILI_URL/KEY, OPENWEBUI_URL/API_KEY). EOF } # ---------- Commands ---------- cmd="${1:-}" case "$cmd" in ""|-h|--help|help) _help ;; # ---- Meili ---- meili-health) _require "MEILI_URL" "$MEILI_URL" curl -sS "$MEILI_URL/health" | ppjson ;; meili-keys) _require "MEILI_URL" "$MEILI_URL" _require "MEILI_KEY" "$MEILI_KEY" curl -sS -H "Authorization: Bearer $MEILI_KEY" "$MEILI_URL/keys" | ppjson ;; meili-stats) _require "MEILI_URL" "$MEILI_URL" _require "MEILI_KEY" "$MEILI_KEY" curl -sS -H "Authorization: Bearer $MEILI_KEY" "$MEILI_URL/indexes/library/stats" | ppjson ;; meili-tasks) _require "MEILI_URL" "$MEILI_URL" _require "MEILI_KEY" "$MEILI_KEY" curl -sS -H "Authorization: Bearer $MEILI_KEY" "$MEILI_URL/tasks?limit=20&from=0" | ppjson ;; meili-init) _require "MEILI_URL" "$MEILI_URL" _require "MEILI_KEY" "$MEILI_KEY" # Create index if missing (idempotent-ish) curl -sS -X POST -H "Authorization: Bearer $MEILI_KEY" -H "Content-Type: application/json" \ --data '{"uid":"library"}' \ "$MEILI_URL/indexes" | ppjson ;; meili-search) shift || true q="${1:-}" lim="${2:-5}" if [ -z "$q" ]; then echo "Usage: meili-search \"\" [limit]" >&2; exit 1; fi _require "MEILI_URL" "$MEILI_URL" _require "MEILI_KEY" "$MEILI_KEY" curl -sS -X POST -H "Authorization: Bearer $MEILI_KEY" -H "Content-Type: application/json" \ --data "{\"q\":\"$q\",\"limit\":$lim}" \ "$MEILI_URL/indexes/library/search" | ppjson ;; meili-reindex) # Run inside the worker container; index all /transcripts/*.json if ! command -v docker >/dev/null 2>&1; then echo "docker not found on host" >&2; exit 1 fi docker compose exec -T podx-worker sh -lc ' python -c "import json,glob,worker,sys; n=0 for p in glob.glob(\"/transcripts/*.json\"): try: worker.index_meili(__import__(\"pathlib\").Path(p)) print(f\"[meili] indexed {__import__(\"pathlib\").Path(p).name}\") n+=1 except Exception as e: print(f\"[meili] FAILED {p}: {e}\", file=sys.stderr) print(f\"Indexed {n} document(s).\")" ' ;; # ---- OpenWebUI ---- # (debug) show resolved OpenWebUI URL for host-side calls # echo "Using OWUI URL: $(_owui_url)" >&2 owui-health) _require "OPENWEBUI_URL" "$OPENWEBUI_URL" curl -sS -o /dev/null -w "%{http_code}\n" "$(_owui_url)/api/health" ;; owui-kbs) _require "OPENWEBUI_URL" "$OPENWEBUI_URL" _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ "$(_owui_url)/api/v1/knowledge/list" | ppjson ;; owui-kb-id) shift || true name="${1:-}" if [ -z "$name" ]; then echo "Usage: owui-kb-id \"\"" >&2; exit 1; fi _id="$(_kb_id_by_name "$name")" if [ -z "$_id" ]; then echo "KB '$name' not found" >&2; exit 1; fi echo "$_id" ;; owui-upload) shift || true file="${1:-}" if [ -z "$file" ] || [ ! -f "$file" ]; then echo "Usage: owui-upload " >&2; exit 1; fi _require "OPENWEBUI_URL" "$OPENWEBUI_URL" _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" tmp_body="$(_mktemp)"; tmp_code="$(_mktemp)" # write response body to tmp_body; write HTTP code to tmp_code; capture exit code from $? curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ -F "file=@$file" \ -w "%{http_code}" --output "$tmp_body" "$(_owui_url)/api/v1/files/" >"$tmp_code" curl_exit=$? http_code="$(cat "$tmp_code" 2>/dev/null || echo 0)" cat "$tmp_body" | ppjson rm -f "$tmp_body" "$tmp_code" 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 ;; owui-attach) shift || true kb_name="${1:-}"; file="${2:-}" if [ -z "$kb_name" ] || [ -z "$file" ] || [ ! -f "$file" ]; then echo "Usage: owui-attach \"\" " >&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" 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 KB_ID="$(_kb_id_by_name "$kb_name")" if [ -z "$KB_ID" ]; then echo "KB '$kb_name' not found"; exit 1; fi tmp_body="$(_mktemp)"; tmp_code="$(_mktemp)" curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -H "Content-Type: application/json" \ -d "{\"file_id\":\"$FILE_ID\"}" \ -w "%{http_code}" --output "$tmp_body" "$(_owui_url)/api/v1/knowledge/$KB_ID/file/add" >"$tmp_code" curl_exit=$?; http_code="$(cat "$tmp_code" 2>/dev/null || echo 0)" RESP="$(cat "$tmp_body")" rm -f "$tmp_body" "$tmp_code" echo "$RESP" | ppjson if [ $curl_exit -ne 0 ]; then echo "Attach failed: curl exit $curl_exit" >&2; exit $curl_exit fi if [ "$http_code" != "200" ]; then echo "Attach failed (HTTP $http_code)" >&2; exit 1 fi ;; owui-kb-files) shift || true kb_name="${1:-}" if [ -z "$kb_name" ]; then echo "Usage: owui-kb-files \"\"" >&2; exit 1; fi _require "OPENWEBUI_URL" "$OPENWEBUI_URL" _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" KB_ID="$(_kb_id_by_name "$kb_name")" if [ -z "$KB_ID" ]; then echo "KB '$kb_name' not found"; exit 1; fi curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ "$(_owui_url)/api/v1/knowledge/$KB_ID/files" | ppjson ;; owui-batch-attach) shift || true kb_name="${1:-}"; glob_pat="${2:-}" if [ -z "$kb_name" ] || [ -z "$glob_pat" ]; then echo "Usage: owui-batch-attach \"\" " >&2; exit 1 fi KB_ID="$(_kb_id_by_name "$kb_name")" if [ -z "$KB_ID" ]; then echo "KB '$kb_name' not found"; exit 1; fi _require "OPENWEBUI_URL" "$OPENWEBUI_URL" _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" shopt -s nullglob matched=($glob_pat) shopt -u nullglob if [ ${#matched[@]} -eq 0 ]; then echo "No files match: $glob_pat"; exit 1; fi for f in "${matched[@]}"; do echo "[owui] uploading: $f" tmp_body="$(_mktemp)"; tmp_code="$(_mktemp)" curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ -F "file=@$f" \ -w "%{http_code}" --output "$tmp_body" "$(_owui_url)/api/v1/files/" >"$tmp_code" curl_up_exit=$?; code_up="$(cat "$tmp_code" 2>/dev/null || echo 0)" FILE_JSON="$(cat "$tmp_body")" rm -f "$tmp_body" "$tmp_code" if [ $curl_up_exit -ne 0 ] || [ "$code_up" != "200" ]; then echo " upload failed (curl=$curl_up_exit http=$code_up), skipping" echo "$FILE_JSON" | ppjson continue fi FILE_ID="$(python3 -c 'import sys,json; d=json.loads(sys.stdin.read()); print(d.get("id") or (d.get("data") or {}).get("id",""))' <<<"$FILE_JSON")" if [ -z "$FILE_ID" ]; then echo " upload failed (no id), skipping"; echo "$FILE_JSON" | ppjson; continue; fi tmp_body="$(_mktemp)"; tmp_code="$(_mktemp)" curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -H "Content-Type: application/json" \ -d "{\"file_id\":\"$FILE_ID\"}" \ -w "%{http_code}" --output "$tmp_body" "$(_owui_url)/api/v1/knowledge/$KB_ID/file/add" >"$tmp_code" curl_att_exit=$?; code_att="$(cat "$tmp_code" 2>/dev/null || echo 0)" RESP="$(cat "$tmp_body")" rm -f "$tmp_body" "$tmp_code" echo "$RESP" | ppjson if [ "$code_att" != "200" ]; then echo " attach failed (HTTP $code_att)" fi done ;; *) echo "Unknown command: $cmd" >&2 _help exit 1 ;; esac