import { useChatRuntime as S } from "../providers/ChatRuntime.es.js";
import { useState as r, useCallback as y, useEffect as p } from "react";
const d = "agent-chat:selected-model", i = {
  get: (t) => typeof window > "u" ? null : localStorage.getItem(t),
  set: (t, n) => {
    typeof window > "u" || localStorage.setItem(t, n);
  }
};
function A() {
  const { apiUrl: t, identity: n } = S(), [m, g] = r([]), [s, l] = r(() => i.get(d) ?? ""), [h, c] = r(!1), f = y(async () => {
    if (n?.authToken) {
      c(!0);
      try {
        const o = await fetch(`${t}/agents/models`, {
          headers: { Authorization: `Bearer ${n.authToken}` }
        });
        if (!o.ok) return;
        const u = await o.json(), a = (Array.isArray(u.data) ? u.data : []).filter((e) => !/embed|rerank/i.test(e.id)).map((e) => ({
          id: e.id,
          name: e.name ?? e.id
        }));
        g(a), a.length > 0 && !a.find((e) => e.id === s) && (l(a[0].id), i.set(d, a[0].id));
      } catch {
      } finally {
        c(!1);
      }
    }
  }, [t, n?.authToken, s]);
  return p(() => {
    f();
  }, [f]), { models: m, selectedModel: s, setSelectedModel: (o) => {
    l(o), i.set(d, o);
  }, loading: h };
}
export {
  A as useModels
};
//# sourceMappingURL=use-models.es.js.map
