Skip to content
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

fix: mobild sso login #2636

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<
...props,
toolNodes,
toolModel,
maxRunToolTimes: 30,
messages: adaptMessages
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,25 @@ export const runToolWithToolChoice = async (
messages: ChatCompletionMessageParam[];
toolNodes: ToolNodeItemType[];
toolModel: LLMModelItemType;
maxRunToolTimes: number;
},
response?: RunToolResponse
): Promise<RunToolResponse> => {
const { messages, toolNodes, toolModel, maxRunToolTimes, ...workflowProps } = props;
const {
toolModel,
toolNodes,
messages,
res,
requestOrigin,
runtimeNodes,
node,
stream,
workflowStreamResponse,
params: { temperature = 0, maxToken = 4000, aiChatVision }
} = props;
} = workflowProps;

if (maxRunToolTimes <= 0 && response) {
return response;
}

const assistantResponses = response?.assistantResponses || [];

const tools: ChatCompletionTool[] = toolNodes.map((item) => {
Expand Down Expand Up @@ -196,7 +200,7 @@ export const runToolWithToolChoice = async (
})();

const toolRunResponse = await dispatchWorkFlow({
...props,
...workflowProps,
isToolCall: true,
runtimeNodes: runtimeNodes.map((item) =>
item.nodeId === toolNode.nodeId
Expand Down Expand Up @@ -252,11 +256,22 @@ export const runToolWithToolChoice = async (
role: ChatCompletionRequestMessageRoleEnum.Assistant,
tool_calls: toolCalls
};
/*
...
user
assistant: tool data
*/
const concatToolMessages = [
...requestMessages,
assistantToolMsgParams
] as ChatCompletionMessageParam[];
const tokens = await countGptMessagesTokens(concatToolMessages, tools);
/*
...
user
assistant: tool data
tool: tool response
*/
const completeMessages = [
...concatToolMessages,
...toolsRunResponse.map((item) => item?.toolMsgParams)
Expand Down Expand Up @@ -314,6 +329,7 @@ export const runToolWithToolChoice = async (
return runToolWithToolChoice(
{
...props,
maxRunToolTimes: maxRunToolTimes - 1,
messages: completeMessages
},
{
Expand Down
11 changes: 8 additions & 3 deletions projects/app/src/pages/login/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect } from 'react';
import React, { useCallback, useEffect, useRef } from 'react';
import { useRouter } from 'next/router';
import { useSystemStore } from '@/web/common/system/useSystemStore';
import type { ResLogin } from '@/global/support/api/userRes.d';
Expand All @@ -20,6 +20,7 @@ const provider = () => {
const router = useRouter();
const { code, state, error } = router.query as { code: string; state: string; error?: string };
const { toast } = useToast();
const loading = useRef(false);

const loginSuccess = useCallback(
(res: ResLogin) => {
Expand All @@ -41,6 +42,10 @@ const provider = () => {

const authCode = useCallback(
async (code: string) => {
if (loading.current) return;

loading.current = true;

if (!loginStore) {
router.replace('/login');
return;
Expand Down Expand Up @@ -73,7 +78,7 @@ const provider = () => {
}, 1000);
}
},
[loginStore, loginSuccess, router, toast]
[loginStore, loginSuccess, router, t, toast]
);

useEffect(() => {
Expand Down Expand Up @@ -103,7 +108,7 @@ const provider = () => {
} else {
authCode(code);
}
}, []);
}, [authCode, code, error, loginStore, router, state, t, toast]);

return <Loading />;
};
Expand Down
41 changes: 34 additions & 7 deletions projects/app/src/pages/login/sso.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useMemo } from 'react';
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import { useRouter } from 'next/router';
import type { ResLogin } from '@/global/support/api/userRes.d';
import { useChatStore } from '@/web/core/chat/context/storeChat';
Expand All @@ -7,15 +7,18 @@ import { clearToken, setToken } from '@/web/support/user/auth';
import { ssoLogin } from '@/web/support/user/api';
import Loading from '@fastgpt/web/components/common/MyLoading';
import { useTranslation } from 'next-i18next';
import { useRequest2 } from '@fastgpt/web/hooks/useRequest';
import { serviceSideProps } from '@/web/common/utils/i18n';
import { useToast } from '@fastgpt/web/hooks/useToast';
import { getErrText } from '@fastgpt/global/common/error/utils';

const provider = () => {
const { t } = useTranslation();
const { setLastChatId, setLastChatAppId } = useChatStore();
const { setUserInfo } = useUserStore();
const router = useRouter();
const { query } = router;
const loading = useRef(false);
const { toast } = useToast();

const loginSuccess = useCallback(
(res: ResLogin) => {
Expand All @@ -30,17 +33,41 @@ const provider = () => {
[setLastChatId, setLastChatAppId, setUserInfo, router]
);

const { run: handleSSO } = useRequest2(() => ssoLogin(query), {
onSuccess: loginSuccess,
errorToast: t('common:support.user.login.error')
});
const handleSSO = useCallback(async () => {
if (loading.current) return;
loading.current = true;

try {
const res = await ssoLogin(query);

if (!res) {
toast({
status: 'warning',
title: t('common:support.user.login.error')
});
return setTimeout(() => {
router.replace('/login');
}, 1000);
}

loginSuccess(res);
} catch (error) {
toast({
status: 'warning',
title: getErrText(error, t('common:support.user.login.error'))
});
setTimeout(() => {
router.replace('/login');
}, 1000);
}
}, [loginSuccess, query, router, t, toast]);

useEffect(() => {
if (query && Object.keys(query).length > 0) {
clearToken();
handleSSO();
}
}, []);
}, [handleSSO, query]);

return <Loading />;
};
Expand Down
Loading