@websolutespa/payload-plugin-bowl-llm
Version:
LLM plugin for Bowl PayloadCms plugin
180 lines (179 loc) • 5.35 kB
JavaScript
import { HttpStatus } from '@websolutespa/payload-utils';
import { addDataAndFileToRequest } from 'payload';
import { options } from '../options';
const appHandler = async (req)=>{
const { payload } = req;
if (!payload) {
throw {
status: 500,
message: 'Server Error: Cannot resolve payload'
};
}
const { routeParams } = req;
if (!routeParams) {
throw {
status: 400,
message: 'Bad Request: params is missing'
};
}
const { locale, appKey } = routeParams;
if (!locale) {
throw {
status: 400,
message: 'Bad Request: locale is missing'
};
}
if (!appKey) {
throw {
status: 400,
message: 'Bad Request: appKey is missing'
};
}
const defaultLocale = payload.config.localization ? payload.config.localization.defaultLocale : 'en';
const response = await payload.find({
collection: options.slug.llmApp,
where: {
'settings.credentials.appKey': {
equals: appKey
}
},
depth: 3,
locale: locale,
fallbackLocale: defaultLocale,
showHiddenFields: true,
pagination: false,
overrideAccess: true
});
if (!response.docs.length) {
throw {
status: 404,
message: 'Not Found: application not found'
};
}
const app = response.docs[0];
if (!app.settings.credentials.previewEnabled) {
throw {
status: 401,
message: 'Unauthorized: granted permissions expired'
};
}
// console.log('params', locale, appKey, app);
return app;
};
export const previewEndpointHandler = async (req)=>{
try {
await addDataAndFileToRequest(req);
const app = await appHandler(req);
const title = app.contents.collapsedWelcomeText || app.settings.credentials.appKey;
const locale = req.routeParams.locale;
const html = getHtml(locale, title, app);
return new Response(html, {
headers: new Headers({
'Content-Type': 'text/html'
}),
status: HttpStatus.OK
});
} catch (error) {
console.log('previewEndpointHandler.error', error);
const defaultLocale = req.payload?.config.localization ? req.payload.config.localization.defaultLocale : 'en';
const title = `${error.status}: ${error.message || 'Error'}`;
const html = getHtml(defaultLocale, title);
return new Response(html, {
headers: new Headers({
'Content-Type': 'text/html'
}),
status: HttpStatus.OK
});
}
};
function getHtml(locale, title, app) {
const serverURL = process.env.PAYLOAD_PUBLIC_SERVER_URL || 'http://localhost:4000';
const basePath = process.env.PAYLOAD_PUBLIC_BASE_PATH || '';
const plugin = {
name: '@websolutespa/bom-llm',
version: 'latest',
namespace: app?.plugin?.name ? 'llmDefault' : 'bomLlm',
...app?.plugin
};
return `
<html lang="${locale}">
<head>
<title>${title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
${getStyle()}
${app ? `
<link rel="stylesheet" href="https://esm.sh/${plugin.name}@${plugin.version}/dist/index.css">
<script type="module" src="https://esm.sh/${plugin.name}@${plugin.version}/dist/umd/index.js"></script>
<script>
const onDOMContentLoaded = () => {
const searchParams = new URLSearchParams(window.location.search);
const llm = ${plugin.namespace}({
appKey: "${app.settings.credentials.appKey}",
apiKey: "${app.settings.credentials.apiKey}",
threadId: searchParams.get("llmThreadId"),
preview: true,
opened: true,
dismissable: false,
skipCustomIntro: true,
endpoint: "${serverURL}${basePath}",
});
};
if (document.readyState === "loading") {
// Loading hasn't finished yet
document.addEventListener("DOMContentLoaded", onDOMContentLoaded);
} else {
// DOMContentLoaded has already fired
onDOMContentLoaded();
}
</script>
` : ''}
</head>
<body>
<div class="bowl-llm__wrapper">
<div class="bowl-llm__title" aria-hidden>${title}</div>
</div>
</body>
</html>
`;
}
function getStyle() {
return `
<style>
html, body {
margin: 0;
height: 100vh;
}
html {
overflow: hidden;
font-family: sans-serif;
font-size: 16px;
}
body {
// min-height: 150vh;
line-height: 1.5;
background: #141414;
color: #fff;
}
.bowl-llm__wrapper {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
row-gap: 1em;
padding: 0 3em;
min-height: 100vh;
font-size: Max(12px, Min(64px, 3vw));
line-height: 1.2;
}
.bowl-llm__title {
font-size: 2em;
text-align: center;
margin: 0;
mix-blend-mode: luminosity;
opacity: 0.1;
}
</style>
`;
}
//# sourceMappingURL=preview.handler.js.map