a2a-source-edit: write frontend/src/a2a.js
This commit is contained in:
66
frontend/src/a2a.js
Normal file
66
frontend/src/a2a.js
Normal file
@@ -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),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user