@websolutespa/payload-plugin-bowl-llm
Version:
LLM plugin for Bowl PayloadCms plugin
82 lines (81 loc) • 2.85 kB
JavaScript
import { isRole, isTenant, roles } from '@websolutespa/payload-plugin-bowl';
import payload from 'payload';
import { options } from '../options';
export const LlmPrompt = {
type: 'withCollection',
slug: options.slug.llmPrompt,
admin: {
group: options.group.llm,
defaultColumns: [
'title',
'updatedAt',
'llmApp'
],
useAsTitle: 'name'
},
hooks: {
beforeChange: [
async ({ data })=>{
// add the updated date to the name, shown in the relationships dropdown
let updatedAt = data.updatedAt || new Date().toISOString();
updatedAt = new Date(updatedAt).toLocaleString('it-IT');
let llmApp = data.llmApp;
if (llmApp) {
const app = await payload.findByID({
collection: options.slug.llmApp,
id: data.llmApp
});
if (app) {
llmApp = app.name;
}
}
return {
...data,
name: `[${updatedAt}] ${data.title}`
};
}
]
},
access: {
create: (args)=>isRole(roles.Admin, options.roles.LlmContributor)(args),
read: (args)=>isRole(roles.Admin)(args) || isRole(options.roles.LlmContributor)(args) && isTenant(options.slug.llmPrompt)(args),
update: (args)=>isRole(roles.Admin)(args) || isRole(options.roles.LlmContributor)(args) && isTenant(options.slug.llmPrompt)(args),
delete: (args)=>isRole(roles.Admin)(args) || isRole(options.roles.LlmContributor)(args) && isTenant(options.slug.llmPrompt)(args)
},
fields: [
{
type: 'withTitle',
required: true,
localized: false
},
{
// showed in relationships dropdown
name: 'name',
type: 'withText',
admin: {
hidden: true
}
},
{
name: 'llmApp',
type: 'relationship',
relationTo: options.slug.llmApp,
required: true,
defaultValue: async ()=>{
try {
// get the app id from the url (used when creating a new prompt within an app)
const regex = /\/admin\/collections\/llmApp\/([\w\d]+)(\?.*)?$/;
const match = window.location.pathname.match(regex);
return match ? match[1] : undefined;
} catch (error) {
return undefined;
}
}
},
{
name: 'systemMessage',
type: 'textarea'
}
]
};
//# sourceMappingURL=LlmPrompt.js.map