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

This commit is contained in:
a2a-cloud
2026-07-18 07:39:17 +00:00
parent 46e5407a5a
commit 7f0edc97d8

View File

@@ -6,7 +6,9 @@ export async function requestJson(url, init = {}) {
const data = text ? JSON.parse(text) : null; const data = text ? JSON.parse(text) : null;
if (!response.ok) { if (!response.ok) {
const detail = data && (data.detail || data.message); const detail = data && (data.detail || data.message);
throw new Error(detail || `request failed: ${response.status}`); const err = new Error(detail || `request failed: ${response.status}`);
err.status = response.status;
throw err;
} }
return data; return data;
} }
@@ -20,10 +22,6 @@ 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( const next = encodeURIComponent(
@@ -43,10 +41,11 @@ export async function requireSession(config) {
} }
export async function callSkill(config, skillName, args) { export async function callSkill(config, skillName, args) {
try {
if (config.auth?.invokeRequiresSession) { if (config.auth?.invokeRequiresSession) {
await requireSession(config); await requireSession(config);
} }
return requestJson( const data = await requestJson(
`${config.endpoints.invoke}/${encodeURIComponent(skillName)}`, `${config.endpoints.invoke}/${encodeURIComponent(skillName)}`,
{ {
method: "POST", method: "POST",
@@ -54,32 +53,16 @@ export async function callSkill(config, skillName, args) {
body: JSON.stringify({ arguments: args }), body: JSON.stringify({ arguments: args }),
}, },
); );
if (data?.status === "setup_required") {
throw new Error(data.message || "Setup required before running this workflow.");
} }
return data;
export function sampleValue(schema) { } catch (err) {
if (!schema || typeof schema !== "object") return null; if (err?.status === 401) {
if ("default" in schema) return schema.default; const target = signInUrl(config);
if ("const" in schema) return schema.const; if (target) window.location.assign(target);
if (Array.isArray(schema.enum) && schema.enum.length) return schema.enum[0]; throw new Error("Session expired. Sign in again to run ContractClock.");
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; throw err;
} }
export function sampleArgs(skill) {
const schema = skill?.input_schema || skill?.inputSchema || {};
const value = sampleValue(schema);
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
} }