mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-20 10:45:52 +00:00

* feat: concat usage code (#5657) * feat: dataset parse queue (#5661) * feat: chat usage concat (#5669) * perf: search test usage * feat: chat usage concat * fix: ts * fix: ts * feat: chat node response store (#5675) * feat: chat node response store * limit export * test * add ai generate node (#5506) * add node copilot * apply code * update dynamic input & output * add code test * usage * dynamic input border render * optimize input & output * optimize code * update style * change card to popover * prompt editor basic * prompt editor * handle key down * update prompt * merge * fix * fix * fix * perf: workflow performance (#5677) * feat: chat node response store * limit export * perf: workflow performance * remove log * fix: app template get duplicate (#5682) * fix: dynamic input lock & code param (#5680) * fix: dynamic input lock & code param * fix * fix * feat: multi node data sync & system tool hot-swapping (#5575) * Enhance file upload functionality and system tool integration (#5257) * Enhance file upload functionality and system tool integration * Add supplementary documents and optimize the upload interface * Refactor file plugin types and update upload configurations * Refactor MinIO configuration variables and clean up API plugin handlers for improved readability and consistency * File name change * Refactor SystemTools component layout * fix i18n * fix * fix * fix * optimize app logs sort (#5310) * log keys config modal * multiple select * api * fontsize * code * chatid * fix build * fix * fix component * change name * log keys config * fix * delete unused * fix * chore: minio service class rewrite * chore: s3 plugin upload * feat: system global cache with multi node sync feature * feat: cache * chore: move images * docs: update & remove useless code * chore: resolve merge conflicts * chore: adjust the code * chore: adjust * deps: upgrade @fastgpt-sdk/plugin to 0.1.17 * perf(s3): s3 config * fix: cache syncKey refresh * fix: update @fastgpt-sdk/plugin to v0.1.18 removing mongo definition for fixing vitest * chore: adjust --------- Co-authored-by: Ctrlz <143257420+ctrlz526@users.noreply.github.com> Co-authored-by: heheer <heheer@sealos.io> Co-authored-by: Archer <545436317@qq.com> * perf: s3 api code * fix: toolbox empty when second open modal * feat: http tool set (#5599) * feat: http toolSet manual create front end * feat: http toolSet manual create i18n * feat: http toolSet manual create back end * feat: auth, as tool param, adapt mcp * fix: delete unused httpPlugin * fix: delete FlowNodeTypeEnum.httpPlugin * fix: AppTypeEnum include httpToolSet and httpPlugin * fix * delete console * fix * output schema * fix * fix bg * fix base url * fix --------- Co-authored-by: heheer <zhiyu44@qq.com> * feat: app count * perf: type check * feat: catch error * perf: plugin hot-swapping (#5688) * perf: plugin hot-swapping * chore: adjust code * perf: cite data auth * fix http toolset (#5689) * temp * fix http tool set * fix * template author hide * dynamic IO ui * fix: auth test * fix dynamic input & output (#5690) Co-authored-by: Archer <545436317@qq.com> * fix: dynamic output id * doc * feat: model permission (#5666) * feat(permission): model permission definition & api * chore: support update model's collaborators * feat: remove unauthedmodel when paste and import * fix: type error * fix: test setup global model list * fix: http tool api * chore: update fastgpt-sdk version * chore: remove useless code * chore: myModelList cache * perf: user who is not manager can not configure model permission (FE) * perf: model => Set * feat: getMyModels moved to opensource code; cache the myModelList * fix: type error * fix dynamic input reference select type (#5694) * remove unique index * read file usage * perf: connection error * fix: abort token count * fix: debug usage concat * fix: immer clone object * fix: immer clone object * perf: throw error when error chat * update audit i18n * fix: 修复识别pptx文件后,返回内容顺序错乱问题 (#5696) * fix: pptx sort error * fix prompt editor (#5695) * fix prompt editor * fix * fix: redis cache prefix (#5697) * fix: redis cache prefix * fix: cache * fix: get model collaborator by model.model * feat: hint for model per * rename bucket name * model ui * doc * doc --------- Co-authored-by: heheer <heheer@sealos.io> Co-authored-by: Finley Ge <32237950+FinleyGe@users.noreply.github.com> Co-authored-by: Ctrlz <143257420+ctrlz526@users.noreply.github.com> Co-authored-by: Zeng Qingwen <143274079+fishwww-ww@users.noreply.github.com> Co-authored-by: heheer <zhiyu44@qq.com> Co-authored-by: Deepturn <33342819+Deepturn@users.noreply.github.com>
265 lines
8.4 KiB
TypeScript
265 lines
8.4 KiB
TypeScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
*/
|
|
|
|
import type { CSSProperties } from 'react';
|
|
import { useMemo, useState, useTransition } from 'react';
|
|
import { LexicalComposer } from '@lexical/react/LexicalComposer';
|
|
import { PlainTextPlugin } from '@lexical/react/LexicalPlainTextPlugin';
|
|
import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';
|
|
import { ContentEditable } from '@lexical/react/LexicalContentEditable';
|
|
import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin';
|
|
import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin';
|
|
import { ListPlugin } from '@lexical/react/LexicalListPlugin';
|
|
import { CheckListPlugin } from '@lexical/react/LexicalCheckListPlugin';
|
|
import { TabIndentationPlugin } from '@lexical/react/LexicalTabIndentationPlugin';
|
|
import LexicalErrorBoundary from '@lexical/react/LexicalErrorBoundary';
|
|
import { HeadingNode, QuoteNode } from '@lexical/rich-text';
|
|
import { ListItemNode, ListNode } from '@lexical/list';
|
|
import { CodeHighlightNode, CodeNode } from '@lexical/code';
|
|
import VariableLabelPickerPlugin from './plugins/VariableLabelPickerPlugin';
|
|
import ListDisplayFixPlugin from './plugins/ListDisplayFixPlugin';
|
|
import { Box, Flex } from '@chakra-ui/react';
|
|
import styles from './index.module.scss';
|
|
import VariablePlugin from './plugins/VariablePlugin';
|
|
import { VariableNode } from './plugins/VariablePlugin/node';
|
|
import type { EditorState, LexicalEditor } from 'lexical';
|
|
import OnBlurPlugin from './plugins/OnBlurPlugin';
|
|
import type { FormPropsType } from './type';
|
|
import { type EditorVariableLabelPickerType, type EditorVariablePickerType } from './type';
|
|
import { getNanoid } from '@fastgpt/global/common/string/tools';
|
|
import FocusPlugin from './plugins/FocusPlugin';
|
|
import { textToEditorState } from './utils';
|
|
import { MaxLengthPlugin } from './plugins/MaxLengthPlugin';
|
|
import { VariableLabelNode } from './plugins/VariableLabelPlugin/node';
|
|
import VariableLabelPlugin from './plugins/VariableLabelPlugin';
|
|
import { useDeepCompareEffect } from 'ahooks';
|
|
import VariablePickerPlugin from './plugins/VariablePickerPlugin';
|
|
import MarkdownPlugin from './plugins/MarkdownPlugin';
|
|
import MyIcon from '../../Icon';
|
|
import ListExitPlugin from './plugins/ListExitPlugin';
|
|
import KeyDownPlugin from './plugins/KeyDownPlugin';
|
|
|
|
const Placeholder = ({ children, padding }: { children: React.ReactNode; padding: string }) => (
|
|
<Box
|
|
position={'absolute'}
|
|
top={0}
|
|
left={0}
|
|
right={0}
|
|
bottom={0}
|
|
p={padding}
|
|
pointerEvents={'none'}
|
|
overflow={'hidden'}
|
|
>
|
|
<Box
|
|
color={'myGray.400'}
|
|
fontSize={'mini'}
|
|
userSelect={'none'}
|
|
whiteSpace={'pre-wrap'}
|
|
wordBreak={'break-all'}
|
|
h={'100%'}
|
|
>
|
|
{children}
|
|
</Box>
|
|
</Box>
|
|
);
|
|
|
|
export type EditorProps = {
|
|
isRichText?: boolean;
|
|
variables?: EditorVariablePickerType[];
|
|
variableLabels?: EditorVariableLabelPickerType[];
|
|
value?: string;
|
|
showOpenModal?: boolean;
|
|
minH?: number;
|
|
maxH?: number;
|
|
maxLength?: number;
|
|
placeholder?: string;
|
|
placeholderPadding?: string;
|
|
isInvalid?: boolean;
|
|
onKeyDown?: (e: React.KeyboardEvent) => void;
|
|
ExtensionPopover?: ((e: {
|
|
onChangeText: (text: string) => void;
|
|
iconButtonStyle: Record<string, any>;
|
|
}) => React.ReactNode)[];
|
|
boxStyle?: CSSProperties;
|
|
};
|
|
|
|
export default function Editor({
|
|
isRichText = false,
|
|
minH = 200,
|
|
maxH = 400,
|
|
maxLength,
|
|
showOpenModal = true,
|
|
onOpenModal,
|
|
variables = [],
|
|
variableLabels = [],
|
|
onChange,
|
|
onChangeText,
|
|
onBlur,
|
|
value,
|
|
placeholder = '',
|
|
placeholderPadding = '12px 14px',
|
|
bg = 'white',
|
|
isInvalid,
|
|
onKeyDown,
|
|
ExtensionPopover,
|
|
boxStyle
|
|
}: EditorProps &
|
|
FormPropsType & {
|
|
onOpenModal?: () => void;
|
|
onChange: (editorState: EditorState, editor: LexicalEditor) => void;
|
|
onChangeText?: ((text: string) => void) | undefined;
|
|
onBlur?: (editor: LexicalEditor) => void;
|
|
}) {
|
|
const [key, setKey] = useState(getNanoid(6));
|
|
const [_, startSts] = useTransition();
|
|
const [focus, setFocus] = useState(false);
|
|
const [scrollHeight, setScrollHeight] = useState(0);
|
|
|
|
const initialConfig = {
|
|
namespace: isRichText ? 'richPromptEditor' : 'promptEditor',
|
|
nodes: [
|
|
VariableNode,
|
|
VariableLabelNode,
|
|
HeadingNode,
|
|
ListNode,
|
|
ListItemNode,
|
|
QuoteNode,
|
|
CodeNode,
|
|
CodeHighlightNode
|
|
],
|
|
editorState: textToEditorState(value),
|
|
onError: (error: Error) => {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
useDeepCompareEffect(() => {
|
|
if (focus) return;
|
|
setKey(getNanoid(6));
|
|
}, [value, variables, variableLabels]);
|
|
|
|
const showFullScreenIcon = useMemo(() => {
|
|
return showOpenModal && scrollHeight > maxH;
|
|
}, [showOpenModal, scrollHeight, maxH]);
|
|
|
|
const iconButtonStyle = useMemo(
|
|
() => ({
|
|
position: 'absolute' as const,
|
|
bottom: 1,
|
|
right: showFullScreenIcon ? '34px' : 2,
|
|
zIndex: 10,
|
|
cursor: 'pointer',
|
|
borderRadius: '6px',
|
|
background: 'rgba(255, 255, 255, 0.01)',
|
|
backdropFilter: 'blur(6.6666669845581055px)',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
w: 6,
|
|
h: 6
|
|
}),
|
|
[showFullScreenIcon]
|
|
);
|
|
|
|
return (
|
|
<Box
|
|
className="nowheel"
|
|
position={'relative'}
|
|
width={'full'}
|
|
cursor={'text'}
|
|
color={'myGray.700'}
|
|
bg={focus ? 'white' : bg}
|
|
borderRadius={'md'}
|
|
>
|
|
<LexicalComposer initialConfig={initialConfig} key={key}>
|
|
{/* Text type */}
|
|
{isRichText ? (
|
|
<RichTextPlugin
|
|
contentEditable={
|
|
<ContentEditable
|
|
className={`${isInvalid ? styles.contentEditable_invalid : styles.contentEditable} ${styles.richText}`}
|
|
style={{
|
|
minHeight: `${minH}px`,
|
|
maxHeight: `${maxH}px`,
|
|
...boxStyle
|
|
}}
|
|
onFocus={() => setFocus(true)}
|
|
onBlur={() => setFocus(false)}
|
|
/>
|
|
}
|
|
placeholder={<Placeholder padding={placeholderPadding}>{placeholder}</Placeholder>}
|
|
ErrorBoundary={LexicalErrorBoundary}
|
|
/>
|
|
) : (
|
|
<PlainTextPlugin
|
|
contentEditable={
|
|
<ContentEditable
|
|
className={isInvalid ? styles.contentEditable_invalid : styles.contentEditable}
|
|
style={{
|
|
minHeight: `${minH}px`,
|
|
maxHeight: `${maxH}px`,
|
|
...boxStyle
|
|
}}
|
|
/>
|
|
}
|
|
placeholder={<Placeholder padding={placeholderPadding}>{placeholder}</Placeholder>}
|
|
ErrorBoundary={LexicalErrorBoundary}
|
|
/>
|
|
)}
|
|
|
|
{/* Basic Plugin */}
|
|
<>
|
|
<HistoryPlugin />
|
|
<MaxLengthPlugin maxLength={maxLength || 999999} />
|
|
<FocusPlugin focus={focus} setFocus={setFocus} />
|
|
<KeyDownPlugin onKeyDown={onKeyDown} />
|
|
|
|
<VariablePlugin variables={variables} />
|
|
{variableLabels.length > 0 && (
|
|
<>
|
|
<VariableLabelPlugin variables={variableLabels} />
|
|
<VariableLabelPickerPlugin variables={variableLabels} isFocus={focus} />
|
|
</>
|
|
)}
|
|
{variableLabels.length > 0 && <VariablePickerPlugin variables={variables} />}
|
|
<OnBlurPlugin onBlur={onBlur} />
|
|
<OnChangePlugin
|
|
onChange={(editorState, editor) => {
|
|
const rootElement = editor.getRootElement();
|
|
setScrollHeight(rootElement?.scrollHeight || 0);
|
|
startSts(() => {
|
|
onChange?.(editorState, editor);
|
|
});
|
|
}}
|
|
/>
|
|
|
|
{isRichText && (
|
|
<>
|
|
<ListDisplayFixPlugin />
|
|
<TabIndentationPlugin />
|
|
<ListPlugin />
|
|
<CheckListPlugin />
|
|
<ListExitPlugin />
|
|
<MarkdownPlugin />
|
|
</>
|
|
)}
|
|
</>
|
|
</LexicalComposer>
|
|
|
|
{onChangeText &&
|
|
ExtensionPopover?.map((Item, index) => (
|
|
<Item key={index} iconButtonStyle={iconButtonStyle} onChangeText={onChangeText} />
|
|
))}
|
|
{showFullScreenIcon && (
|
|
<Flex onClick={onOpenModal} {...iconButtonStyle} right={2}>
|
|
<MyIcon name={'common/fullScreenLight'} w={'1rem'} color={'myGray.500'} />
|
|
</Flex>
|
|
)}
|
|
</Box>
|
|
);
|
|
}
|