Skip to content

Commit

Permalink
fix: emb page cannot save lng;feat: Add classify error log (#3367)
Browse files Browse the repository at this point in the history
* fix: emb page cannot save lng

* perf: i18n check
  • Loading branch information
c121914yu authored Dec 11, 2024
1 parent 096afef commit 048f5a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { chatValue2RuntimePrompt } from '@fastgpt/global/core/chat/adapt';
import { getHandleId } from '@fastgpt/global/core/workflow/utils';
import { loadRequestMessages } from '../../../chat/utils';
import { llmCompletionsBodyFormat } from '../../../ai/utils';
import { addLog } from '../../../../common/system/log';

type Props = ModuleDispatchProps<{
[NodeInputKeyEnum.aiModel]: string;
Expand Down Expand Up @@ -65,7 +66,7 @@ export const dispatchClassifyQuestion = async (props: Props): Promise<CQResponse
return {
[NodeOutputKeyEnum.cqResult]: result.value,
[DispatchNodeResponseKeyEnum.skipHandleId]: agents
.filter((item) => item.key !== arg?.type)
.filter((item) => item.key !== result.key)
.map((item) => getHandleId(nodeId, 'source', item.key)),
[DispatchNodeResponseKeyEnum.nodeResponse]: {
totalPoints: user.openaiAccount?.key ? 0 : totalPoints,
Expand Down Expand Up @@ -142,6 +143,10 @@ const completions = async ({
agents.find((item) => answer.includes(item.value))?.key ||
'';

if (!id) {
addLog.warn('Classify error', { answer });
}

return {
tokens: await countMessagesTokens(messages),
arg: { type: id }
Expand Down
38 changes: 23 additions & 15 deletions packages/web/hooks/useI18n.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import Cookies, { CookieAttributes } from 'js-cookie';
import Cookies from 'js-cookie';
import { useTranslation } from 'next-i18next';
import { LangEnum } from '../../../projects/app/src/web/common/utils/i18n';

const setCookie = (key: string, value: string, options?: CookieAttributes) => {
Cookies.set(key, value, options);
const LANG_KEY = 'NEXT_LOCALE';
const isInIframe = () => {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
};
const getCookie = (key: string) => {
return Cookies.get(key);
const setLang = (value: string) => {
if (isInIframe()) {
localStorage.setItem(LANG_KEY, value);
} else {
// 不在 iframe 中,同时使用 Cookie 和 localStorage
Cookies.set(LANG_KEY, value, { expires: 30 });
localStorage.setItem(LANG_KEY, value);
}
};
const getLang = () => {
return localStorage.getItem(LANG_KEY) || Cookies.get(LANG_KEY);
};

const LANG_KEY = 'NEXT_LOCALE';

export const useI18nLng = () => {
const { i18n } = useTranslation();
Expand All @@ -26,23 +38,19 @@ export const useI18nLng = () => {

const onChangeLng = async (lng: string) => {
const lang = languageMap[lng] || 'en';
const prevLang = getLang();

setCookie(LANG_KEY, lang, {
expires: 30
});

const currentLng = i18n?.language;
if (!currentLng) return;
setLang(lang);

await i18n?.changeLanguage?.(lang);
if (currentLng !== lang) {
if (prevLang && prevLang !== lang) {
window?.location?.reload?.();
}
};

const setUserDefaultLng = () => {
if (!navigator || !localStorage) return;
if (getCookie(LANG_KEY)) return onChangeLng(getCookie(LANG_KEY) as string);
if (getLang()) return onChangeLng(getLang() as string);

const lang = languageMap[navigator.language] || 'en';

Expand Down

0 comments on commit 048f5a2

Please sign in to comment.