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 function unwrapInvokeResponse(payload) {
|
||||
return payload && typeof payload === "object" && "result" in payload
|
||||
? payload.result
|
||||
: payload;
|
||||
}
|
||||
|
||||
export async function requestJson(url, init = {}) {
|
||||
const response = await fetch(url, { credentials: "same-origin", ...init });
|
||||
const text = await response.text();
|
||||
@@ -19,14 +25,20 @@ 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 requireSession(config) {
|
||||
const session = await loadSession(config);
|
||||
if (!session?.authenticated) {
|
||||
const loginUrl = config.auth?.loginUrl;
|
||||
if (loginUrl) {
|
||||
const next = encodeURIComponent(window.location.href);
|
||||
window.location.assign(`${loginUrl}${loginUrl.includes("?") ? "&" : "?"}next=${next}`);
|
||||
}
|
||||
const target = signInUrl(config);
|
||||
if (target) window.location.assign(target);
|
||||
throw new Error("sign in required");
|
||||
}
|
||||
return session;
|
||||
@@ -36,7 +48,7 @@ export async function callSkill(config, skillName, args) {
|
||||
if (config.auth?.invokeRequiresSession) {
|
||||
await requireSession(config);
|
||||
}
|
||||
return requestJson(
|
||||
const payload = await requestJson(
|
||||
`${config.endpoints.invoke}/${encodeURIComponent(skillName)}`,
|
||||
{
|
||||
method: "POST",
|
||||
@@ -44,6 +56,7 @@ export async function callSkill(config, skillName, args) {
|
||||
body: JSON.stringify({ arguments: args }),
|
||||
},
|
||||
);
|
||||
return unwrapInvokeResponse(payload);
|
||||
}
|
||||
|
||||
export function sampleValue(schema) {
|
||||
|
||||
Reference in New Issue
Block a user