import React, { useState, useCallback, memo, ReactNode } from 'react'; import { Spinner } from '~/components'; import { useRecoilValue } from 'recoil'; import CodeBlock from './Content/CodeBlock.jsx'; import { Disclosure } from '@headlessui/react'; import { ChevronDownIcon, LucideProps } from 'lucide-react'; import { cn } from '~/utils/'; import store from '~/store'; interface Input { inputStr: string; } interface PluginProps { plugin: { plugin: string; input: string; thought: string; loading?: boolean; outputs?: string; latest?: string; inputs?: Input[]; }; } type PluginsMap = { [pluginKey: string]: string; }; type PluginIconProps = LucideProps & { className?: string; }; function formatInputs(inputs: Input[]) { let output = ''; for (let i = 0; i < inputs.length; i++) { output += `${inputs[i].inputStr}`; if (inputs.length > 1 && i !== inputs.length - 1) { output += ',\n'; } } return output; } const Plugin: React.FC = ({ plugin }) => { const [loading, setLoading] = useState(plugin.loading); const finished = plugin.outputs && plugin.outputs.length > 0; const plugins: PluginsMap = useRecoilValue(store.plugins); const getPluginName = useCallback( (pluginKey: string) => { if (!pluginKey) { return null; } if (pluginKey === 'n/a' || pluginKey === 'self reflection') { return pluginKey; } return plugins[pluginKey] ?? 'self reflection'; }, [plugins], ); if (!plugin || !plugin.latest) { return null; } const latestPlugin = getPluginName(plugin.latest); if (!latestPlugin || (latestPlugin && latestPlugin === 'n/a')) { return null; } if (finished && loading) { setLoading(false); } const generateStatus = (): ReactNode => { if (!loading && latestPlugin === 'self reflection') { return 'Finished'; } else if (latestPlugin === 'self reflection') { return 'I\'m thinking...'; } else { return ( <> {loading ? 'Using' : 'Used'} {latestPlugin} {loading ? '...' : ''} ); } }; return (
{({ open }) => { const iconProps: PluginIconProps = { className: cn(open ? 'rotate-180 transform' : '', 'h-4 w-4'), }; return ( <>
{generateStatus()}
{loading && }
{finished && ( )} ); }}
); }; export default memo(Plugin);