fix frontend invoke result handling
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
export const CONFIG_URL = import.meta.env.DEV ? "/config.json" : "./config.json";
|
export const CONFIG_URL = import.meta.env.DEV ? "/config.json" : "./config.json";
|
||||||
|
|
||||||
|
export function unwrapInvokeResponse(payload) {
|
||||||
|
return payload && typeof payload === "object" && "result" in payload
|
||||||
|
? payload.result
|
||||||
|
: payload;
|
||||||
|
}
|
||||||
|
|
||||||
export async function requestJson(url, init = {}) {
|
export async function requestJson(url, init = {}) {
|
||||||
const response = await fetch(url, { credentials: "same-origin", ...init });
|
const response = await fetch(url, { credentials: "same-origin", ...init });
|
||||||
const text = await response.text();
|
const text = await response.text();
|
||||||
@@ -19,14 +25,20 @@ export async function loadSession(config) {
|
|||||||
return requestJson(config.endpoints.session);
|
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 requireSession(config) {
|
export async function requireSession(config) {
|
||||||
const session = await loadSession(config);
|
const session = await loadSession(config);
|
||||||
if (!session?.authenticated) {
|
if (!session?.authenticated) {
|
||||||
const loginUrl = config.auth?.loginUrl;
|
const target = signInUrl(config);
|
||||||
if (loginUrl) {
|
if (target) window.location.assign(target);
|
||||||
const next = encodeURIComponent(window.location.href);
|
|
||||||
window.location.assign(`${loginUrl}${loginUrl.includes("?") ? "&" : "?"}next=${next}`);
|
|
||||||
}
|
|
||||||
throw new Error("sign in required");
|
throw new Error("sign in required");
|
||||||
}
|
}
|
||||||
return session;
|
return session;
|
||||||
@@ -34,9 +46,10 @@ export async function requireSession(config) {
|
|||||||
|
|
||||||
export async function callSkill(config, skillName, args) {
|
export async function callSkill(config, skillName, args) {
|
||||||
if (config.auth?.invokeRequiresSession) await requireSession(config);
|
if (config.auth?.invokeRequiresSession) await requireSession(config);
|
||||||
return requestJson(`${config.endpoints.invoke}/${encodeURIComponent(skillName)}`, {
|
const payload = await requestJson(`${config.endpoints.invoke}/${encodeURIComponent(skillName)}`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "content-type": "application/json" },
|
headers: { "content-type": "application/json" },
|
||||||
body: JSON.stringify({ arguments: args }),
|
body: JSON.stringify({ arguments: args }),
|
||||||
});
|
});
|
||||||
|
return unwrapInvokeResponse(payload);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,6 +95,11 @@
|
|||||||
if (!response.ok) throw new Error((data && (data.detail || data.message)) || `request failed: ${response.status}`);
|
if (!response.ok) throw new Error((data && (data.detail || data.message)) || `request failed: ${response.status}`);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
function unwrapInvokeResponse(payload) {
|
||||||
|
return payload && typeof payload === 'object' && 'result' in payload
|
||||||
|
? payload.result
|
||||||
|
: payload;
|
||||||
|
}
|
||||||
function renderSkill() {
|
function renderSkill() {
|
||||||
const skill = (config.skills || []).find((item) => item.name === selected);
|
const skill = (config.skills || []).find((item) => item.name === selected);
|
||||||
skillNameEl.textContent = selected;
|
skillNameEl.textContent = selected;
|
||||||
@@ -125,9 +130,10 @@
|
|||||||
resultEl.textContent = 'Running…';
|
resultEl.textContent = 'Running…';
|
||||||
try {
|
try {
|
||||||
const args = JSON.parse(argsEl.value || '{}');
|
const args = JSON.parse(argsEl.value || '{}');
|
||||||
const data = await requestJson(`${config.endpoints.invoke}/${encodeURIComponent(selected)}`, {
|
const payload = await requestJson(`${config.endpoints.invoke}/${encodeURIComponent(selected)}`, {
|
||||||
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ arguments: args })
|
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ arguments: args })
|
||||||
});
|
});
|
||||||
|
const data = unwrapInvokeResponse(payload);
|
||||||
resultEl.textContent = JSON.stringify(data, null, 2);
|
resultEl.textContent = JSON.stringify(data, null, 2);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
resultEl.textContent = err.message || String(err);
|
resultEl.textContent = err.message || String(err);
|
||||||
|
|||||||
Reference in New Issue
Block a user