diff --git a/frontend/src/a2a.js b/frontend/src/a2a.js index 0251692..498212a 100644 --- a/frontend/src/a2a.js +++ b/frontend/src/a2a.js @@ -3,17 +3,10 @@ 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 = { message: text }; - } + const data = text ? JSON.parse(text) : null; if (!response.ok) { - const detail = data && (data.detail || data.message || data.error); - const err = new Error(detail || `request failed: ${response.status}`); - err.status = response.status; - throw err; + const detail = data && (data.detail || data.message); + throw new Error(detail || `request failed: ${response.status}`); } return data; } @@ -27,6 +20,10 @@ 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( @@ -35,32 +32,54 @@ export function signInUrl(config) { return `${target}${target.includes("?") ? "&" : "?"}next=${next}`; } -export async function callSkill(config, skillName, args) { - return requestJson(`${config.endpoints.invoke}/${encodeURIComponent(skillName)}`, { - method: "POST", - headers: { "content-type": "application/json" }, - body: JSON.stringify({ arguments: args }), - }); +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 fileToBrowserDocument(file, maxBytes = 128000) { - if (file.size > maxBytes) { - throw new Error(`${file.name} is too large. Upload files up to ${Math.round(maxBytes / 1000)} KB.`); +export async function callSkill(config, skillName, args) { + if (config.auth?.invokeRequiresSession) { + await requireSession(config); } - const mediaType = file.type || (file.name.endsWith(".json") ? "application/json" : file.name.endsWith(".csv") ? "text/csv" : "text/plain"); - if (!["text/csv", "text/plain", "application/json"].includes(mediaType)) { - throw new Error(`${file.name} must be CSV, JSON, or plain text.`); - } - const buffer = await file.arrayBuffer(); - let binary = ""; - const bytes = new Uint8Array(buffer); - const chunkSize = 0x8000; - for (let i = 0; i < bytes.length; i += chunkSize) { - binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize)); - } - return { - filename: file.name, - media_type: mediaType, - data_base64: btoa(binary), - }; + return requestJson( + `${config.endpoints.invoke}/${encodeURIComponent(skillName)}`, + { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ arguments: 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 sampleArgs(skill) { + const schema = skill?.input_schema || skill?.inputSchema || {}; + const value = sampleValue(schema); + return value && typeof value === "object" && !Array.isArray(value) ? value : {}; }