mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-19 10:07:24 +00:00

* fix: push again, user select option button and form input radio content overflow (#5601) * fix: push again, user select option button and form input radio content overflow * fix: use useCallback instead of useMemo, fix unnecessary delete * fix: Move the variable inside the component * fix: do not pass valueLabel to MySelect * ui * del collection api adapt * refactor: inherit permission (#5529) * refactor: permission update conflict check function * refactor(permission): app collaborator update api * refactor(permission): support app update collaborator * feat: support fe permission conflict check * refactor(permission): app permission * refactor(permission): dataset permission * refactor(permission): team permission * chore: fe adjust * fix: type error * fix: audit pagiation * fix: tc * chore: initv4130 * fix: app/dataset auth logic * chore: move code * refactor(permission): remove selfPermission * fix: mock * fix: test * fix: app & dataset auth * fix: inherit * test(inheritPermission): test syncChildrenPermission * prompt editor add list plugin (#5620) * perf: search result (#5608) * fix: table size (#5598) * temp: list value * backspace * optimize code --------- Co-authored-by: Archer <545436317@qq.com> Co-authored-by: 伍闲犬 <whoeverimf5@gmail.com> * fix: fe & member list (#5619) * chore: initv4130 * fix: MemberItemCard * fix: MemberItemCard * chore: fe adjust & init script * perf: test code * doc * fix debug variables (#5617) * perf: search result (#5608) * fix: table size (#5598) * fix debug variables * fix --------- Co-authored-by: Archer <545436317@qq.com> Co-authored-by: 伍闲犬 <whoeverimf5@gmail.com> * perf: member ui * fix: inherit bug (#5624) * refactor(permission): remove getClbsWithInfo, which is useless * fix: app list privateApp * fix: get infos * perf(fe): remove delete icon when it is disable in MemberItemCard * fix: dataset private dataset * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Archer <545436317@qq.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * perf: auto coupon * chore: upgrade script & get infos avatar (#5625) * fix: get infos * chore: initv4130 * feat: support WecomRobot publish, and fix AesKey can not save bug (#5526) * feat: resolve conflicts * fix: add param 'show_publish_wecom' * feat: abstract out WecomCrypto type * doc: wecom robot document * fix: solve instability in AI output * doc: update some pictures * feat: remove functions from request.ts to chat.ts and toolCall.ts * doc: wecom robot doc update * fix * delete unused code * doc: update version and prompt * feat: remove wecom crypto, delete wecom code in workflow * feat: delete unused codes --------- Co-authored-by: heheer <zhiyu44@qq.com> * remove test * rename init shell * feat: collection page store * reload sandbox * pysandbox * remove log * chore: remove useless code (#5629) * chore: remove useless code * fix: checkConflict * perf: support hidden type for RoleList * fix: copy node * update doc * fix(permission): some bug (#5632) * fix: app/dataset list * fix: inherit bug * perf: del app;i18n;save chat * fix: test * i18n * fix: sumper overflow return OwnerRoleVal (#5633) * remove invalid code * fix: scroll * fix: objectId * update next * update package * object id * mock redis * feat: add redis append to resolve wecom stream response (#5643) * feat: resolve conflicts * fix: add param 'show_publish_wecom' * feat: abstract out WecomCrypto type * doc: wecom robot document * fix: solve instability in AI output * doc: update some pictures * feat: remove functions from request.ts to chat.ts and toolCall.ts * doc: wecom robot doc update * fix * delete unused code * doc: update version and prompt * feat: remove wecom crypto, delete wecom code in workflow * feat: delete unused codes * feat: add redis append method --------- Co-authored-by: heheer <zhiyu44@qq.com> * cache per * fix(test): init team sub when creating mocked user (#5646) * fix: button is not vertically centered (#5647) * doc * fix: gridFs objectId (#5649) --------- Co-authored-by: Zeng Qingwen <143274079+fishwww-ww@users.noreply.github.com> Co-authored-by: Finley Ge <32237950+FinleyGe@users.noreply.github.com> Co-authored-by: heheer <heheer@sealos.io> Co-authored-by: 伍闲犬 <whoeverimf5@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: heheer <zhiyu44@qq.com>
251 lines
6.7 KiB
TypeScript
251 lines
6.7 KiB
TypeScript
import { type TeamSchema, type TeamTmbItemType } from '@fastgpt/global/support/user/team/type';
|
|
import { type ClientSession, Types } from '../../../common/mongo';
|
|
import {
|
|
TeamMemberRoleEnum,
|
|
TeamMemberStatusEnum,
|
|
notLeaveStatus
|
|
} from '@fastgpt/global/support/user/team/constant';
|
|
import { MongoTeamMember } from './teamMemberSchema';
|
|
import { MongoTeam } from './teamSchema';
|
|
import { type UpdateTeamProps } from '@fastgpt/global/support/user/team/controller';
|
|
import { getTmbPermission } from '../../permission/controller';
|
|
import { PerResourceTypeEnum } from '@fastgpt/global/support/permission/constant';
|
|
import { TeamPermission } from '@fastgpt/global/support/permission/user/controller';
|
|
import { TeamDefaultRoleVal } from '@fastgpt/global/support/permission/user/constant';
|
|
import { MongoMemberGroupModel } from '../../permission/memberGroup/memberGroupSchema';
|
|
import { mongoSessionRun } from '../../../common/mongo/sessionRun';
|
|
import { DefaultGroupName } from '@fastgpt/global/support/user/team/group/constant';
|
|
import { getAIApi } from '../../../core/ai/config';
|
|
import { createRootOrg } from '../../permission/org/controllers';
|
|
import { refreshSourceAvatar } from '../../../common/file/image/controller';
|
|
|
|
async function getTeamMember(match: Record<string, any>): Promise<TeamTmbItemType> {
|
|
const tmb = await MongoTeamMember.findOne(match).populate<{ team: TeamSchema }>('team').lean();
|
|
if (!tmb) {
|
|
return Promise.reject('member not exist');
|
|
}
|
|
|
|
const role =
|
|
(await getTmbPermission({
|
|
resourceType: PerResourceTypeEnum.team,
|
|
teamId: tmb.teamId,
|
|
tmbId: tmb._id
|
|
})) ?? TeamDefaultRoleVal;
|
|
|
|
return {
|
|
userId: String(tmb.userId),
|
|
teamId: String(tmb.teamId),
|
|
teamAvatar: tmb.team.avatar,
|
|
teamName: tmb.team.name,
|
|
memberName: tmb.name,
|
|
avatar: tmb.avatar,
|
|
balance: tmb.team.balance,
|
|
tmbId: String(tmb._id),
|
|
teamDomain: tmb.team?.teamDomain,
|
|
role: tmb.role,
|
|
status: tmb.status,
|
|
permission: new TeamPermission({
|
|
role,
|
|
isOwner: tmb.role === TeamMemberRoleEnum.owner
|
|
}),
|
|
notificationAccount: tmb.team.notificationAccount,
|
|
|
|
lafAccount: tmb.team.lafAccount,
|
|
openaiAccount: tmb.team.openaiAccount,
|
|
externalWorkflowVariables: tmb.team.externalWorkflowVariables
|
|
};
|
|
}
|
|
|
|
export const getTeamOwner = async (teamId: string) => {
|
|
const tmb = await MongoTeamMember.findOne({
|
|
teamId,
|
|
role: TeamMemberRoleEnum.owner
|
|
}).lean();
|
|
return tmb;
|
|
};
|
|
|
|
export async function getTmbInfoByTmbId({ tmbId }: { tmbId: string }) {
|
|
if (!tmbId) {
|
|
return Promise.reject('tmbId or userId is required');
|
|
}
|
|
return getTeamMember({
|
|
_id: new Types.ObjectId(String(tmbId)),
|
|
status: notLeaveStatus
|
|
});
|
|
}
|
|
|
|
export async function getUserDefaultTeam({ userId }: { userId: string }) {
|
|
if (!userId) {
|
|
return Promise.reject('tmbId or userId is required');
|
|
}
|
|
return getTeamMember({
|
|
userId: new Types.ObjectId(userId)
|
|
});
|
|
}
|
|
|
|
export async function createDefaultTeam({
|
|
userId,
|
|
teamName = 'My Team',
|
|
avatar = '/icon/logo.svg',
|
|
session
|
|
}: {
|
|
userId: string;
|
|
teamName?: string;
|
|
avatar?: string;
|
|
session: ClientSession;
|
|
}) {
|
|
// auth default team
|
|
const tmb = await MongoTeamMember.findOne({
|
|
userId: new Types.ObjectId(userId)
|
|
});
|
|
|
|
if (!tmb) {
|
|
// create team
|
|
const [{ _id: insertedId }] = await MongoTeam.create(
|
|
[
|
|
{
|
|
ownerId: userId,
|
|
name: teamName,
|
|
avatar,
|
|
createTime: new Date()
|
|
}
|
|
],
|
|
{ session }
|
|
);
|
|
// create team member
|
|
const [tmb] = await MongoTeamMember.create(
|
|
[
|
|
{
|
|
teamId: insertedId,
|
|
userId,
|
|
name: 'Owner',
|
|
role: TeamMemberRoleEnum.owner,
|
|
status: TeamMemberStatusEnum.active,
|
|
createTime: new Date()
|
|
}
|
|
],
|
|
{ session }
|
|
);
|
|
// create default group
|
|
await MongoMemberGroupModel.create(
|
|
[
|
|
{
|
|
teamId: tmb.teamId,
|
|
name: DefaultGroupName,
|
|
avatar
|
|
}
|
|
],
|
|
{ session }
|
|
);
|
|
await createRootOrg({ teamId: tmb.teamId, session });
|
|
console.log('create default team, group and root org', userId);
|
|
return tmb;
|
|
} else {
|
|
console.log('default team exist', userId);
|
|
}
|
|
}
|
|
|
|
export async function updateTeam({
|
|
teamId,
|
|
name,
|
|
avatar,
|
|
teamDomain,
|
|
lafAccount,
|
|
openaiAccount,
|
|
externalWorkflowVariable
|
|
}: UpdateTeamProps & { teamId: string }) {
|
|
// auth openai key
|
|
if (openaiAccount?.key) {
|
|
console.log('auth user openai key', openaiAccount?.key);
|
|
const baseUrl = openaiAccount?.baseUrl || 'https://api.openai.com/v1';
|
|
openaiAccount.baseUrl = baseUrl;
|
|
|
|
const ai = getAIApi({
|
|
userKey: openaiAccount
|
|
});
|
|
|
|
const response = await ai.chat.completions.create({
|
|
model: 'gpt-4o-mini',
|
|
max_tokens: 1,
|
|
messages: [{ role: 'user', content: 'hi' }]
|
|
});
|
|
if (response?.choices?.[0]?.message?.content === undefined) {
|
|
return Promise.reject('Key response is empty');
|
|
}
|
|
}
|
|
|
|
return mongoSessionRun(async (session) => {
|
|
const unsetObj = (() => {
|
|
const obj: Record<string, 1> = {};
|
|
if (lafAccount?.pat === '') {
|
|
obj.lafAccount = 1;
|
|
}
|
|
if (openaiAccount?.key === '') {
|
|
obj.openaiAccount = 1;
|
|
}
|
|
if (externalWorkflowVariable) {
|
|
if (externalWorkflowVariable.value === '') {
|
|
obj[`externalWorkflowVariables.${externalWorkflowVariable.key}`] = 1;
|
|
}
|
|
}
|
|
|
|
if (Object.keys(obj).length === 0) {
|
|
return undefined;
|
|
}
|
|
return {
|
|
$unset: obj
|
|
};
|
|
})();
|
|
const setObj = (() => {
|
|
const obj: Record<string, any> = {};
|
|
if (lafAccount?.pat && lafAccount?.appid) {
|
|
obj.lafAccount = lafAccount;
|
|
}
|
|
if (openaiAccount?.key && openaiAccount?.baseUrl) {
|
|
obj.openaiAccount = openaiAccount;
|
|
}
|
|
if (externalWorkflowVariable) {
|
|
if (externalWorkflowVariable.value !== '') {
|
|
obj[`externalWorkflowVariables.${externalWorkflowVariable.key}`] =
|
|
externalWorkflowVariable.value;
|
|
}
|
|
}
|
|
if (Object.keys(obj).length === 0) {
|
|
return undefined;
|
|
}
|
|
return obj;
|
|
})();
|
|
|
|
// This is where we get the old team
|
|
const team = await MongoTeam.findByIdAndUpdate(
|
|
teamId,
|
|
{
|
|
$set: {
|
|
...(name ? { name } : {}),
|
|
...(avatar ? { avatar } : {}),
|
|
...(teamDomain ? { teamDomain } : {}),
|
|
...setObj
|
|
},
|
|
...unsetObj
|
|
},
|
|
{ session }
|
|
);
|
|
|
|
// Update member group avatar
|
|
if (avatar) {
|
|
await MongoMemberGroupModel.updateOne(
|
|
{
|
|
teamId: teamId,
|
|
name: DefaultGroupName
|
|
},
|
|
{
|
|
avatar
|
|
},
|
|
{ session }
|
|
);
|
|
|
|
await refreshSourceAvatar(avatar, team?.avatar, session);
|
|
}
|
|
});
|
|
}
|