-
-
Couldn't load subscription status.
- Fork 13.9k
feat(chatinput): Add input translation to English feature #9807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { LanguagesIcon } from 'lucide-react'; | ||
| import { memo, useState } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import { useChatInputStore } from '../../store'; | ||
| import Action from '../components/Action'; | ||
| import { useInputTranslate } from './useInputTranslate'; | ||
|
|
||
| const InputTranslate = memo(() => { | ||
| const { t } = useTranslation('chat'); | ||
| const [isTranslating, setIsTranslating] = useState(false); | ||
| const editor = useChatInputStore((s) => s.editor); | ||
| const { translateToEnglish } = useInputTranslate(); | ||
|
|
||
| const handleTranslate = async () => { | ||
| if (!editor || isTranslating) return; | ||
|
|
||
| const content = editor.getDocument('markdown') as string; | ||
| if (!content?.trim()) return; | ||
|
|
||
| setIsTranslating(true); | ||
| try { | ||
| const translatedContent = await translateToEnglish(content); | ||
| if (translatedContent && translatedContent !== content) { | ||
| editor.setDocument('markdown', translatedContent); | ||
| } | ||
| } catch (error) { | ||
| console.error('Translation failed:', error); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Logging errors to console may not be sufficient for user feedback. Please add a user-facing notification or UI message when translation fails to ensure users are aware of the error. Suggested implementation: setErrorMessage('');
setIsTranslating(true);
try {
const translatedContent = await translateToEnglish(content);
if (translatedContent && translatedContent !== content) {
editor.setDocument('markdown', translatedContent);
}
} catch (error) {
console.error('Translation failed:', error);
setErrorMessage('Translation failed. Please try again.');
} finally {
setIsTranslating(false);
}
}; return (
<>
<Action
icon={LanguagesIcon}
loading={isTranslating}
onClick={handleTranslate}
title={t('input.translateToEnglish')}
/>
{errorMessage && (
<div style={{ color: 'red', marginTop: 8 }}>
{errorMessage}
</div>
)}
</> const [errorMessage, setErrorMessage] = React.useState('');
const content = editor.getDocument('markdown') as string;
if (!content?.trim()) return; |
||
| } finally { | ||
| setIsTranslating(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Action | ||
| icon={LanguagesIcon} | ||
| loading={isTranslating} | ||
| onClick={handleTranslate} | ||
| title={t('input.translateToEnglish')} | ||
| /> | ||
| ); | ||
| }); | ||
|
|
||
| InputTranslate.displayName = 'InputTranslate'; | ||
|
|
||
| export default InputTranslate; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { chainTranslate } from '@lobechat/prompts'; | ||
| import { TraceNameMap } from '@lobechat/types'; | ||
| import { useCallback } from 'react'; | ||
|
|
||
| import { chatService } from '@/services/chat'; | ||
| import { useChatStore } from '@/store/chat'; | ||
| import { useUserStore } from '@/store/user'; | ||
| import { systemAgentSelectors } from '@/store/user/selectors'; | ||
| import { merge } from '@/utils/merge'; | ||
|
|
||
| export const useInputTranslate = () => { | ||
| const getCurrentTracePayload = useChatStore((s) => s.getCurrentTracePayload); | ||
|
|
||
| const translateToEnglish = useCallback( | ||
| async (content: string): Promise<string> => { | ||
| return new Promise((resolve, reject) => { | ||
| // Get current translation settings | ||
| const translationSetting = systemAgentSelectors.translation(useUserStore.getState()); | ||
|
|
||
| let translatedContent = ''; | ||
|
|
||
| chatService | ||
| .fetchPresetTaskResult({ | ||
| onFinish: (result) => { | ||
| if (result && typeof result === 'string') { | ||
| resolve(result); | ||
| } else { | ||
| resolve(translatedContent); | ||
| } | ||
| }, | ||
| onMessageHandle: (chunk) => { | ||
| if (chunk.type === 'text') { | ||
| translatedContent += chunk.text; | ||
| } | ||
| }, | ||
| params: merge(translationSetting, chainTranslate(content, 'en-US')), | ||
| trace: getCurrentTracePayload({ traceName: TraceNameMap.Translator }), | ||
| }) | ||
| .catch((error) => { | ||
| reject(error); | ||
|
Comment on lines
+22
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The hook wraps Useful? React with 👍 / 👎. |
||
| }); | ||
| }); | ||
| }, | ||
| [getCurrentTracePayload], | ||
| ); | ||
|
|
||
| return { | ||
| translateToEnglish, | ||
| }; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Check for translatedContent equality may not handle whitespace or formatting changes.
Normalize both strings before comparison to avoid false negatives due to formatting or whitespace differences.