UNPKG

@websolutespa/payload-plugin-bowl-llm

Version:

LLM plugin for Bowl PayloadCms plugin

77 lines (76 loc) 2.96 kB
'use client'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { SelectInput, useField, useFormFields, useTranslation } from '@payloadcms/ui'; import * as React from 'react'; import { KbFileNoToolOption } from '../../api/consts'; export const AppTool = ({ path = '', field })=>{ const { t } = useTranslation(); const llmApp = useFormFields(([fields])=>fields['llmApp']); const { value, setValue } = useField({ path }); const [options, setOptions] = React.useState([]); const llmAppValue = llmApp?.value; React.useEffect(()=>{ const fetchOptions = async ()=>{ try { const url = `${process.env.PAYLOAD_PUBLIC_BASE_PATH || ''}/api/llm/appTools?llmApp=${llmAppValue}`; const response = await fetch(url, { method: 'GET', headers: { 'Content-Type': 'application/json' } }); if (response.ok) { const toolsOpts = await response.json(); toolsOpts.unshift({ label: t('general:kbFileNoToolOption'), value: KbFileNoToolOption }); setOptions(toolsOpts); if (toolsOpts.length == 2) { // if there is only one tool, select it setValue(toolsOpts[1].value); } else if (!toolsOpts.some((option)=>option.value === value)) { // reset selected value if it's not in the new options setValue(toolsOpts[0].value); } } else { setOptions([ { label: (await response.json()).message, value: '' } ]); } } catch (error) { console.error('Error fetching data:', error); } }; if (llmAppValue) { fetchOptions(); } }, [ llmAppValue ]); console.log('options: ', options); return options?.filter((option)=>option.value !== '').length !== 0 && /*#__PURE__*/ _jsxs("div", { className: "field-type select", children: [ /*#__PURE__*/ _jsx("label", { className: "field-label", htmlFor: `field-${path}`.replace(/\./g, '__'), children: "App tool" }), /*#__PURE__*/ _jsx(SelectInput, { path: path, name: path, options: options, value: value, onChange: (option)=>Array.isArray(option) ? null : setValue(option.value), readOnly: field.admin?.readOnly }) ] }); }; //# sourceMappingURL=AppTool.js.map