File size: 2,442 Bytes
f52d137 |
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 |
import { getPageSignFromUrl } from "./oauth";
export const preloadDefaultMcp = async (): Promise<File | null> => {
try {
const res = await fetch("/demo-mcp.json", { cache: "no-store" });
if (!res.ok) return null;
const blob = await res.blob();
return new File([blob], "demo-mcp.json", { type: "application/json" });
} catch (err) {
console.error(err);
return null;
}
};
export const readFileAsText = (file: File): Promise<string> => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onerror = () => reject(new Error("File reading failed"));
reader.onload = () => resolve(String(reader.result || ""));
reader.readAsText(file);
});
};
export interface StartDemoPayload {
model: string;
provider: string;
user: string;
mcp: string | null;
sign: string | null;
}
export const startDemo = async (
payload: StartDemoPayload,
accessToken: string | null,
): Promise<{ iframe_url: string; health_url: string }> => {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (accessToken) headers["Authorization"] = `Bearer ${accessToken}`;
const res = await fetch("/api/start", {
method: "POST",
headers,
body: JSON.stringify(payload),
});
if (!res.ok) {
let message = `Error while starting (${res.status}${res.statusText ? ` ${res.statusText}` : ""})`;
try {
const clone = res.clone();
try {
const data = await res.json();
const parts = [];
if (data && data.error) parts.push(String(data.error));
if (data && data.detail) parts.push(String(data.detail));
if (parts.length)
message = `Error while starting: ${parts.join(" — ")}`;
} catch (_e) {
const txt = await clone.text();
if (txt && txt.trim())
message = `Error while starting: ${txt.trim().slice(0, 300)}`;
}
} catch (_ignore) {}
throw new Error(message);
}
const data = await res.json();
if (!data || !data.iframe_url || !data.health_url) {
throw new Error("Invalid server response - missing required URLs.");
}
return data;
};
export const createStartDemoPayload = (
model: string,
provider: string,
loginLabel: string,
mcpText: string | null,
): StartDemoPayload => ({
model: model.trim(),
provider: provider.trim(),
user: loginLabel,
mcp: mcpText,
sign: getPageSignFromUrl(),
});
|