a2a-source-edit: write frontend/src/a2a.js
This commit is contained in:
@@ -5,7 +5,12 @@ export async function requestJson(url, init = {}) {
|
||||
const text = await response.text();
|
||||
const data = text ? JSON.parse(text) : null;
|
||||
if (!response.ok) {
|
||||
const detail = data && (data.detail || data.message);
|
||||
if (response.status === 401) {
|
||||
const err = new Error("session expired or sign-in required");
|
||||
err.status = 401;
|
||||
throw err;
|
||||
}
|
||||
const detail = data && (data.detail || data.message || data.error);
|
||||
throw new Error(detail || `request failed: ${response.status}`);
|
||||
}
|
||||
return data;
|
||||
@@ -20,10 +25,6 @@ export async function loadSession(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;
|
||||
if (!target) return null;
|
||||
const next = encodeURIComponent(
|
||||
@@ -32,20 +33,7 @@ export function signInUrl(config) {
|
||||
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");
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function callSkill(config, skillName, args) {
|
||||
if (config.auth?.invokeRequiresSession) {
|
||||
await requireSession(config);
|
||||
}
|
||||
return requestJson(
|
||||
`${config.endpoints.invoke}/${encodeURIComponent(skillName)}`,
|
||||
{
|
||||
@@ -56,30 +44,42 @@ export async function callSkill(config, skillName, args) {
|
||||
);
|
||||
}
|
||||
|
||||
export function sampleValue(schema) {
|
||||
if (!schema || typeof schema !== "object") return null;
|
||||
if ("default" in schema) return schema.default;
|
||||
if ("const" in schema) return schema.const;
|
||||
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 fixtureInvoices() {
|
||||
return [
|
||||
{ invoice_number: "INV-100", vendor: "Acme", subtotal: 90, tax: 10, total: 100 },
|
||||
{ invoice_number: "INV-100", vendor: "Acme", subtotal: 90, tax: 10, total: 100 },
|
||||
{ invoice_number: "INV-200", vendor: "Beta", subtotal: 50, tax: 5, total: 80 },
|
||||
];
|
||||
}
|
||||
|
||||
export function sampleArgs(skill) {
|
||||
const schema = skill?.input_schema || skill?.inputSchema || {};
|
||||
const value = sampleValue(schema);
|
||||
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
||||
export function fileToBrowserDocument(file) {
|
||||
const maxBytes = 256 * 1024;
|
||||
const accepted = new Set(["application/json", "text/json", "text/csv", "text/plain", ""]);
|
||||
if (file.size > maxBytes) {
|
||||
return Promise.reject(new Error(`Upload ${file.name} exceeds 256 KiB.`));
|
||||
}
|
||||
if (!accepted.has(file.type)) {
|
||||
return Promise.reject(new Error(`Upload ${file.name} must be JSON, CSV, or plain text.`));
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onerror = () => reject(new Error(`Could not read ${file.name}.`));
|
||||
reader.onload = () => {
|
||||
const dataUrl = String(reader.result || "");
|
||||
const comma = dataUrl.indexOf(",");
|
||||
resolve({
|
||||
filename: file.name,
|
||||
media_type: file.type || inferMediaType(file.name),
|
||||
data_base64: comma >= 0 ? dataUrl.slice(comma + 1) : dataUrl,
|
||||
});
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
}
|
||||
|
||||
function inferMediaType(name) {
|
||||
const lower = name.toLowerCase();
|
||||
if (lower.endsWith(".csv")) return "text/csv";
|
||||
if (lower.endsWith(".txt")) return "text/plain";
|
||||
return "application/json";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user