From 4fbdafd9814f7b21508338c946b2b5db09fafcf3 Mon Sep 17 00:00:00 2001 From: a2a-cloud Date: Sat, 18 Jul 2026 05:08:50 +0000 Subject: [PATCH] a2a-source-edit: write frontend/src/a2a.js --- frontend/src/a2a.js | 54 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/frontend/src/a2a.js b/frontend/src/a2a.js index 187029f..498212a 100644 --- a/frontend/src/a2a.js +++ b/frontend/src/a2a.js @@ -5,9 +5,6 @@ export async function requestJson(url, init = {}) { const text = await response.text(); const data = text ? JSON.parse(text) : null; if (!response.ok) { - if (response.status === 401) { - throw new Error("Your session expired. Use the sign-in button and try again."); - } const detail = data && (data.detail || data.message); throw new Error(detail || `request failed: ${response.status}`); } @@ -23,9 +20,15 @@ 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(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}`; } @@ -34,7 +37,7 @@ export async function requireSession(config) { if (!session?.authenticated) { const target = signInUrl(config); if (target) window.location.assign(target); - throw new Error("Sign in required before running QuoteJudge tools."); + throw new Error("sign in required"); } return session; } @@ -43,9 +46,40 @@ export async function callSkill(config, skillName, args) { if (config.auth?.invokeRequiresSession) { await requireSession(config); } - return requestJson(`${config.endpoints.invoke}/${encodeURIComponent(skillName)}`, { - method: "POST", - headers: { "content-type": "application/json" }, - body: JSON.stringify({ arguments: args }), - }); + 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 : {}; }