From 5c8ab2080fd01d337365e4b94fd3be1a4f7f904a Mon Sep 17 00:00:00 2001 From: a2a-cloud Date: Sat, 18 Jul 2026 03:22:34 +0000 Subject: [PATCH] a2a-source-edit: write frontend/src/a2a.js --- frontend/src/a2a.js | 66 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 frontend/src/a2a.js diff --git a/frontend/src/a2a.js b/frontend/src/a2a.js new file mode 100644 index 0000000..0251692 --- /dev/null +++ b/frontend/src/a2a.js @@ -0,0 +1,66 @@ +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 }; + } + 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; + } + return data; +} + +export async function loadAgentConfig() { + return requestJson(CONFIG_URL); +} + +export async function loadSession(config) { + return requestJson(config.endpoints.session); +} + +export function signInUrl(config) { + const target = config.auth?.authorizeUrl || config.auth?.loginUrl; + if (!target) return null; + const next = encodeURIComponent( + window.location.pathname + window.location.search + window.location.hash, + ); + 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 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.`); + } + 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), + }; +}