Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 12x 30x 1x 5x 5x 1x 1x 1x 3x 3x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 3x 6x 6x 6x 1x 5x 5x 5x 5x 5x 2x 2x 2x 3x 5x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 20x 20x 20x 20x 3x 3x 3x 3x 20x 20x 20x 20x 20x 2x 2x 1x 1x 1x 2x 20x 1x 1x 1x 20x 2x 2x 2x 2x 2x 16x 20x 4x 4x 4x 20x 1x 13x 13x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | const BASE = "/api";
let token = "";
// Current user info populated after auth
export let currentUser = { username: "", role: "", pages: [] };
export function setCurrentUser(u) {
currentUser = u;
}
export function setToken(t) {
token = t;
if (t) localStorage.setItem("token", t);
else localStorage.removeItem("token");
}
export function getToken() {
return token;
}
// Refresh token is cookie-managed server-side.
export function setRefreshToken() {}
let refreshPromise = null;
function getLegacyRefreshToken() {
return localStorage.getItem("refresh_token") || "";
}
function clearLegacyRefreshToken() {
localStorage.removeItem("refresh_token");
}
async function requestRefresh(body) {
const r = await fetch(BASE + "/auth/refresh", {
method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "same-origin",
body: body ? JSON.stringify(body) : undefined,
});
if (!r.ok) return false;
const data = await r.json();
setToken(data.access_token || "");
return !!data.access_token;
}
export async function tryRefreshSession() {
if (refreshPromise) return refreshPromise;
refreshPromise = (async () => {
try {
const cookieRefreshed = await requestRefresh();
if (cookieRefreshed) {
clearLegacyRefreshToken();
return true;
}
const legacyRefreshToken = getLegacyRefreshToken();
if (!legacyRefreshToken) return false;
const legacyRefreshed = await requestRefresh({ refresh_token: legacyRefreshToken });
if (legacyRefreshed) {
clearLegacyRefreshToken();
return true;
}
clearLegacyRefreshToken();
return false;
} catch {
return false;
} finally {
refreshPromise = null;
}
})();
return refreshPromise;
}
async function request(path, opts = {}) {
const headers = opts.headers || {};
if (token) headers["Authorization"] = `Bearer ${token}`;
if (opts.json) {
headers["Content-Type"] = "application/json";
opts.body = JSON.stringify(opts.json);
delete opts.json;
}
const fetchOpts = { ...opts, headers };
if (path.startsWith("/auth/")) fetchOpts.credentials = "same-origin";
try {
let r = await fetch(BASE + path, fetchOpts);
if (r.status === 401 && !path.includes("/auth/refresh")) {
const refreshed = await tryRefreshSession();
if (refreshed) {
headers["Authorization"] = `Bearer ${token}`;
r = await fetch(BASE + path, fetchOpts);
}
}
if (r.status === 401) {
setToken("");
throw new Error("Unauthorized");
}
if (!r.ok) {
const error = new Error(`HTTP ${r.status}`);
error.status = r.status;
try { error.body = await r.json(); } catch {}
throw error;
}
return r.json();
} catch (e) {
console.error(`API ${opts.method || "GET"} ${path}:`, e);
throw e;
}
}
export function get(path) {
return request(path);
}
export function post(path, data) {
return request(path, { method: "POST", json: data });
}
export function del(path) {
return request(path, { method: "DELETE" });
}
export async function upload(path, file) {
const headers = {};
if (token) headers["Authorization"] = `Bearer ${token}`;
const form = new FormData();
form.append("file", file);
const r = await fetch(`${BASE}/files/upload?path=${encodeURIComponent(path)}`, {
method: "POST",
headers,
body: form,
});
if (!r.ok) throw new Error(`HTTP ${r.status}`);
return r.json();
}
export function login(creds) {
return request("/auth/login", { method: "POST", json: creds });
}
export function checkAuth() {
return request("/auth/me");
}
export function logout() {
return request("/auth/logout", { method: "POST" });
}
export function getPreferences() {
return get("/auth/preferences");
}
export function savePreferences(data) {
return request("/auth/preferences", { method: "PUT", json: data });
}
export function getLiteLLMHealth() {
return get("/litellm/health");
}
export function getCcConnectHealth() {
return get("/cc-connect/health");
}
export function startCcConnect() {
return post("/cc-connect/start");
}
export function stopCcConnect() {
return post("/cc-connect/stop");
}
export function getInfoEngineItems(params = {}) {
const query = new URLSearchParams();
if (params.limit !== undefined) query.set("limit", String(params.limit));
if (params.offset !== undefined) query.set("offset", String(params.offset));
if (params.source) query.set("source", params.source);
if (params.tag) query.set("tag", params.tag);
if (params.since) query.set("since", params.since);
const suffix = query.toString() ? `?${query.toString()}` : "";
return get(`/info-engine/items${suffix}`);
}
export function getInfoEngineItem(id) {
return get(`/info-engine/items/${id}`);
}
export function put(path, data) {
return request(path, { method: "PUT", json: data });
}
export async function download(path, filename) {
const headers = {};
if (token) headers["Authorization"] = `Bearer ${token}`;
try {
// Get a temporary download token
const tokenResponse = await fetch(`${BASE}/files/download-token?path=${encodeURIComponent(path)}`, {
method: "POST",
headers,
});
if (!tokenResponse.ok) {
const errorText = await tokenResponse.text();
console.error(`Download token failed: ${tokenResponse.status} - ${errorText}`);
throw new Error(`Download failed: ${tokenResponse.status}`);
}
const { token: downloadToken } = await tokenResponse.json();
// Use hidden iframe for download - avoids page navigation and popup blockers
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = `${BASE}/files/download?token=${downloadToken}`;
document.body.appendChild(iframe);
// Clean up iframe after download starts
setTimeout(() => {
document.body.removeChild(iframe);
}, 5000);
} catch (e) {
console.error("Download error:", e);
throw e;
}
}
|