UNPKG

@websolutespa/payload-plugin-bowl-llm

Version:

LLM plugin for Bowl PayloadCms plugin

306 lines (305 loc) 9.24 kB
import { ResponseOptions, ResponseSuccess, withCors } from '@websolutespa/payload-utils/server'; import { options } from '../options'; import { appHandler } from './app.handler'; import { errorHandler } from './error.handler'; // post history get export const historyHandler = async (req)=>{ if (req.method === 'OPTIONS') { return ResponseOptions(); } const headers = withCors({ req, cors: '*' }); try { const app = await appHandler(req); if (!app.contents.enableHistory) { throw { status: 401, message: 'Unauthorized: app history disabled' }; } const { payload } = req; if (!payload) { throw { status: 500, message: 'Server Error: Cannot resolve payload' }; } const { query } = req; if (!query) { throw { status: 400, message: 'Bad Request: locale is missing' }; } const { locale } = query; if (!locale) { throw { status: 400, message: 'Bad Request: locale is missing' }; } const defaultLocale = payload.config.localization ? payload.config.localization.defaultLocale : 'en'; const { userId } = req.body; let history = []; if (app && userId) { const response = await payload.find({ collection: options.slug.llmHistory, where: { and: [ { llmApp: { equals: app.id } }, { userId: { equals: userId } } ] }, sort: '-updatedAt', depth: 0, locale: locale, fallbackLocale: defaultLocale, showHiddenFields: true, pagination: false, overrideAccess: true }); const docs = response.docs; history = docs.map((x)=>({ threadId: String(x.llmThread), messageId: String(x.messageId) })); } return ResponseSuccess(history, { headers }); } catch (error) { return errorHandler(error, { headers }); } }; // post history add item export const historyAddHandler = async (req)=>{ if (req.method === 'OPTIONS') { return ResponseOptions(); } const headers = withCors({ req, cors: '*' }); try { const app = await appHandler(req); if (!app.contents.enableHistory) { throw { status: 401, message: 'Unauthorized: app history disabled' }; } const { payload } = req; if (!payload) { throw { status: 500, message: 'Server Error: Cannot resolve payload' }; } const { query } = req; if (!query) { throw { status: 400, message: 'Bad Request: locale is missing' }; } const { locale } = query; if (!locale) { throw { status: 400, message: 'Bad Request: locale is missing' }; } const defaultLocale = payload.config.localization ? payload.config.localization.defaultLocale : 'en'; const { userId, threadId, messageId } = req.body; if (!userId) { throw { status: 400, message: 'Bad Request: userId is missing' }; } if (!threadId) { throw { status: 400, message: 'Bad Request: threadId is missing' }; } if (!messageId) { throw { status: 400, message: 'Bad Request: messageId is missing' }; } if (app && userId) { const response = await payload.find({ collection: options.slug.llmHistory, where: { and: [ { llmApp: { equals: app.id } }, { userId: { equals: userId } }, { llmThread: { equals: threadId } } ] }, depth: 0, locale: locale, fallbackLocale: defaultLocale, showHiddenFields: true, pagination: false, overrideAccess: true }); if (response.docs.length === 1) { const record = response.docs[0]; await payload.update({ collection: options.slug.llmHistory, where: { id: { equals: record.id } }, data: { messageId }, locale: locale, fallbackLocale: defaultLocale, showHiddenFields: true, overrideAccess: true }); } else { await payload.create({ collection: options.slug.llmHistory, data: { llmApp: app.id, userId, llmThread: threadId, messageId: messageId }, locale: locale, fallbackLocale: defaultLocale, showHiddenFields: true, overrideAccess: true }); } } return ResponseSuccess(null, { headers }); } catch (error) { return errorHandler(error, { headers }); } }; // post history remove item export const historyRemoveHandler = async (req)=>{ if (req.method === 'OPTIONS') { return ResponseOptions(); } const headers = withCors({ req, cors: '*' }); try { const app = await appHandler(req); if (!app.contents.enableHistory) { throw { status: 401, message: 'Unauthorized: app history disabled' }; } const { payload } = req; if (!payload) { throw { status: 500, message: 'Server Error: Cannot resolve payload' }; } const { query } = req; if (!query) { throw { status: 400, message: 'Bad Request: locale is missing' }; } const { locale } = query; if (!locale) { throw { status: 400, message: 'Bad Request: locale is missing' }; } const defaultLocale = payload.config.localization ? payload.config.localization.defaultLocale : 'en'; const { userId, threadId, messageId } = req.body; if (!userId) { throw { status: 400, message: 'Bad Request: userId is missing' }; } if (!threadId) { throw { status: 400, message: 'Bad Request: threadId is missing' }; } if (app && userId) { await payload.delete({ collection: options.slug.llmHistory, where: { and: [ { llmApp: { equals: app.id } }, { userId: { equals: userId } }, { llmThread: { equals: threadId } } ] }, depth: 0, locale: locale, fallbackLocale: defaultLocale, showHiddenFields: true, overrideAccess: true }); } return ResponseSuccess(null, { headers }); } catch (error) { return errorHandler(error, { headers }); } }; //# sourceMappingURL=history.handler.js.map