From d6a41df2d05c2b19cf80a72de8e6cba74c594be3 Mon Sep 17 00:00:00 2001 From: IndieKKY Date: Sat, 20 May 2023 09:52:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89prompt=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/biz/Body.tsx | 5 +- src/biz/SegmentCard.tsx | 5 +- src/biz/Settings.tsx | 29 +++++++++- src/const.tsx | 102 ++++++++++++++++++++++++++++++++++ src/hooks/useTranslate.ts | 112 ++++++++------------------------------ src/typings.d.ts | 4 ++ 6 files changed, 162 insertions(+), 95 deletions(-) diff --git a/src/biz/Body.tsx b/src/biz/Body.tsx index 5f1cb82..07b24a6 100644 --- a/src/biz/Body.tsx +++ b/src/biz/Body.tsx @@ -31,7 +31,6 @@ const Body = () => { const floatKeyPointsSegIdx = useAppSelector(state => state.env.floatKeyPointsSegIdx) const translateEnable = useAppSelector(state => state.env.envData.translateEnable) const summarizeEnable = useAppSelector(state => state.env.envData.summarizeEnable) - const title = useAppSelector(state => state.env.title) const {addSummarizeTask} = useTranslate() const bodyRef = useRef() const curOffsetTop = useAppSelector(state => state.env.curOffsetTop) @@ -71,11 +70,11 @@ const Body = () => { } if (segments_.length < SUMMARIZE_ALL_THRESHOLD || confirm(`确定总结${segments_.length}个段落?`)) { for (const segment of segments_) { - addSummarizeTask(title, curSummaryType, segment).catch(console.error) + addSummarizeTask(curSummaryType, segment).catch(console.error) } toast.success(`已添加${segments_.length}个总结任务!`) } - }, [addSummarizeTask, apiKey, curSummaryType, dispatch, segments, title]) + }, [addSummarizeTask, apiKey, curSummaryType, dispatch, segments]) const onFoldAll = useCallback(() => { dispatch(setFoldAll(!foldAll)) diff --git a/src/biz/SegmentCard.tsx b/src/biz/SegmentCard.tsx index 62549b7..850c3c9 100644 --- a/src/biz/SegmentCard.tsx +++ b/src/biz/SegmentCard.tsx @@ -68,18 +68,17 @@ const Summarize = (props: { const dispatch = useAppDispatch() const apiKey = useAppSelector(state => state.env.envData.apiKey) const fontSize = useAppSelector(state => state.env.envData.fontSize) - const title = useAppSelector(state => state.env.title) const curSummaryType = useAppSelector(state => state.env.tempData.curSummaryType) const {addSummarizeTask} = useTranslate() const onGenerate = useCallback(() => { if (apiKey) { - addSummarizeTask(title, curSummaryType, segment).catch(console.error) + addSummarizeTask(curSummaryType, segment).catch(console.error) } else { dispatch(setPage(PAGE_SETTINGS)) toast.error('需要先设置ApiKey!') } - }, [addSummarizeTask, apiKey, curSummaryType, dispatch, segment, title]) + }, [addSummarizeTask, apiKey, curSummaryType, dispatch, segment]) const onCopy = useCallback(() => { if (summary != null) { diff --git a/src/biz/Settings.tsx b/src/biz/Settings.tsx index fe02548..908984a 100644 --- a/src/biz/Settings.tsx +++ b/src/biz/Settings.tsx @@ -6,6 +6,8 @@ import { LANGUAGE_DEFAULT, LANGUAGES, PAGE_MAIN, + PROMPT_DEFAULTS, + PROMPT_TYPES, SERVER_URL_THIRD, SUMMARIZE_LANGUAGE_DEFAULT, TRANSLATE_FETCH_DEFAULT, @@ -69,8 +71,10 @@ const Settings = () => { const [wordsValue, setWordsValue] = useState(envData.words??WORDS_DEFAULT) const [fetchAmountValue, setFetchAmountValue] = useState(envData.fetchAmount??TRANSLATE_FETCH_DEFAULT) const [moreFold, {toggle: toggleMoreFold}] = useBoolean(true) + const [promptsFold, {toggle: togglePromptsFold}] = useBoolean(true) const fold = useAppSelector(state => state.env.fold) const totalHeight = useAppSelector(state => state.env.totalHeight) + const [promptsValue, setPromptsValue] = useState<{[key: string]: string}>(envData.prompts??{}) const wordsList = useMemo(() => { const list = [] for (let i = WORDS_MIN; i <= WORDS_MAX; i += WORDS_STEP) { @@ -106,10 +110,11 @@ const Settings = () => { words: wordsValue, fetchAmount: fetchAmountValue, fontSize: fontSizeValue, + prompts: promptsValue, })) dispatch(setPage(PAGE_MAIN)) toast.success('保存成功') - }, [fontSizeValue, apiKeyValue, autoExpandValue, dispatch, fetchAmountValue, hideOnDisableAutoTranslateValue, languageValue, serverUrlValue, summarizeEnableValue, summarizeFloatValue, summarizeLanguageValue, themeValue, transDisplayValue, translateEnableValue, wordsValue]) + }, [promptsValue, fontSizeValue, apiKeyValue, autoExpandValue, dispatch, fetchAmountValue, hideOnDisableAutoTranslateValue, languageValue, serverUrlValue, summarizeEnableValue, summarizeFloatValue, summarizeLanguageValue, themeValue, transDisplayValue, translateEnableValue, wordsValue]) const onCancel = useCallback(() => { dispatch(setPage(PAGE_MAIN)) @@ -202,6 +207,28 @@ const Settings = () => {
  • 支持其他第三方代理,有问题可加群交流
  • } +
    + {promptsFold?'点击查看提示词':'点击折叠提示词'} +
    + {!promptsFold &&
    + {PROMPT_TYPES.map((item, idx) => +
    {item.name}
    +
    { + setPromptsValue({ + ...promptsValue, + // @ts-expect-error + [item.type]: PROMPT_DEFAULTS[item.type]??'' + }) + }}>点击填充默认
    +
    } htmlFor={`prompt-${item.type}`}> +