a2a-source-edit: write frontend/src/a2a.js

This commit is contained in:
a2a-cloud
2026-07-18 05:29:05 +00:00
parent 62faab9c37
commit 281bd722f9

View File

@@ -3,17 +3,10 @@ export const CONFIG_URL = import.meta.env.DEV ? "/config.json" : "./config.json"
export async function requestJson(url, init = {}) { export async function requestJson(url, init = {}) {
const response = await fetch(url, { credentials: "same-origin", ...init }); const response = await fetch(url, { credentials: "same-origin", ...init });
const text = await response.text(); const text = await response.text();
let data = null; const data = text ? JSON.parse(text) : null;
try {
data = text ? JSON.parse(text) : null;
} catch {
data = { message: text };
}
if (!response.ok) { if (!response.ok) {
const detail = data && (data.detail || data.message || data.error); const detail = data && (data.detail || data.message);
const error = new Error(detail || `request failed: ${response.status}`); throw new Error(detail || `request failed: ${response.status}`);
error.status = response.status;
throw error;
} }
return data; return data;
} }
@@ -27,53 +20,66 @@ export async function loadSession(config) {
} }
export function signInUrl(config) { export function signInUrl(config) {
// Always prefer authorizeUrl. The dashboard's login cookie is host-locked to
// the dashboard's own origin, so sending the browser straight to loginUrl
// signs the user in *there* and bounces back here still signed out.
// authorizeUrl hands this origin a code it trades for its own session.
const target = config.auth?.authorizeUrl || config.auth?.loginUrl; const target = config.auth?.authorizeUrl || config.auth?.loginUrl;
if (!target) return null; if (!target) return null;
const next = encodeURIComponent(window.location.pathname + window.location.search + window.location.hash); const next = encodeURIComponent(
window.location.pathname + window.location.search + window.location.hash,
);
return `${target}${target.includes("?") ? "&" : "?"}next=${next}`; return `${target}${target.includes("?") ? "&" : "?"}next=${next}`;
} }
export async function callSkill(config, skillName, args) { export async function requireSession(config) {
return requestJson(`${config.endpoints.invoke}/${encodeURIComponent(skillName)}`, { const session = await loadSession(config);
method: "POST", if (!session?.authenticated) {
headers: { "content-type": "application/json" }, const target = signInUrl(config);
body: JSON.stringify({ arguments: args }), if (target) window.location.assign(target);
}); throw new Error("sign in required");
}
export async function callSkillWithSession(config, skillName, args) {
try {
return await callSkill(config, skillName, args);
} catch (error) {
if (error.status === 401) {
const target = signInUrl(config);
if (target) window.location.assign(target);
throw new Error("Sign in required to run QuoteJudge.");
}
throw error;
} }
return session;
} }
export function fileToBrowserDocument(file) { export async function callSkill(config, skillName, args) {
return new Promise((resolve, reject) => { if (config.auth?.invokeRequiresSession) {
const reader = new FileReader(); await requireSession(config);
reader.onerror = () => reject(new Error(`Could not read ${file.name}`)); }
reader.onload = () => { return requestJson(
const result = String(reader.result || ""); `${config.endpoints.invoke}/${encodeURIComponent(skillName)}`,
const comma = result.indexOf(","); {
resolve({ method: "POST",
filename: file.name, headers: { "content-type": "application/json" },
media_type: file.type || guessMediaType(file.name), body: JSON.stringify({ arguments: args }),
data_base64: comma >= 0 ? result.slice(comma + 1) : result, },
}); );
};
reader.readAsDataURL(file);
});
} }
function guessMediaType(name) { export function sampleValue(schema) {
const lower = name.toLowerCase(); if (!schema || typeof schema !== "object") return null;
if (lower.endsWith(".json")) return "application/json"; if ("default" in schema) return schema.default;
if (lower.endsWith(".csv")) return "text/csv"; if ("const" in schema) return schema.const;
return "text/plain"; if (Array.isArray(schema.enum) && schema.enum.length) return schema.enum[0];
const type = Array.isArray(schema.type)
? schema.type.find((item) => item !== "null") || schema.type[0]
: schema.type;
if (type === "array") return [];
if (type === "boolean") return false;
if (type === "integer" || type === "number") return 0;
if (type === "string") return "";
const props = schema.properties || {};
if (type === "object" || Object.keys(props).length) {
const required = Array.isArray(schema.required) ? schema.required : Object.keys(props);
return Object.fromEntries(
required.map((key) => [key, sampleValue(props[key])]),
);
}
return null;
}
export function sampleArgs(skill) {
const schema = skill?.input_schema || skill?.inputSchema || {};
const value = sampleValue(schema);
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
} }