Files
launch-check-studio-v1/frontend/src/a2a.js
2026-07-18 09:22:39 +00:00

67 lines
2.0 KiB
JavaScript

export const CONFIG_URL = import.meta.env.DEV ? "/config.json" : "./config.json";
export async function requestJson(url, init = {}) {
const response = await fetch(url, { credentials: "same-origin", ...init });
const text = await response.text();
let data = null;
try {
data = text ? JSON.parse(text) : null;
} catch {
data = null;
}
if (!response.ok) {
if (response.status === 401) {
const err = new Error("Session expired. Sign in again to run LaunchCheck.");
err.status = 401;
throw err;
}
const detail = data && (data.detail || data.message || data.code);
throw new Error(detail || `request failed: ${response.status}`);
}
return data;
}
export async function loadAgentConfig() {
return requestJson(CONFIG_URL);
}
export async function loadSession(config) {
return requestJson(config.endpoints.session);
}
export function signInUrl(config) {
const target = config.auth?.authorizeUrl || config.auth?.loginUrl;
if (!target) return null;
const next = encodeURIComponent(window.location.pathname + window.location.search + window.location.hash);
return `${target}${target.includes("?") ? "&" : "?"}next=${next}`;
}
export async function requireSession(config) {
const session = await loadSession(config);
if (!session?.authenticated) {
const target = signInUrl(config);
if (target) window.location.assign(target);
throw new Error("Sign in required to run LaunchCheck.");
}
return session;
}
export async function callSkill(config, skillName, args) {
if (config.auth?.invokeRequiresSession) {
await requireSession(config);
}
try {
return await requestJson(`${config.endpoints.invoke}/${encodeURIComponent(skillName)}`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ arguments: args }),
});
} catch (err) {
if (err.status === 401) {
const target = signInUrl(config);
if (target) window.location.assign(target);
}
throw err;
}
}