| 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(), | |
| }); | |