Skip to content
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
1 change: 1 addition & 0 deletions packages/global/core/workflow/runtime/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type ChatDispatchProps = {
id: string; // May be the id of the system plug-in (cannot be used directly to look up the table)
teamId: string;
tmbId: string; // App tmbId
name: string;
isChildApp?: boolean;
};
runningUserInfo: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type InteractiveBasicType = {
memoryMessages: ChatCompletionMessageParam[]; // 这轮工具中,产生的新的 messages
toolCallId: string; // 记录对应 tool 的id,用于后续交互节点可以替换掉 tool 的 response
};

usageId?: string;
};

type InteractiveNodeType = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const dispatchAppRequest = async (props: Props): Promise<Response> => {
...props,
runningAppInfo: {
id: String(appData._id),
name: appData.name,
teamId: String(appData.teamId),
tmbId: String(appData.tmbId)
},
Expand Down
5 changes: 3 additions & 2 deletions packages/service/core/workflow/dispatch/child/runApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { authAppByTmbId } from '../../../../support/permission/app/auth';
import { ReadPermissionVal } from '@fastgpt/global/support/permission/constant';
import { getAppVersionById } from '../../../app/version/controller';
import { parseUrlToFileType } from '@fastgpt/global/common/file/tools';
import { getUserChatInfoAndAuthTeamPoints } from '../../../../support/permission/auth/team';
import { getUserChatInfo } from '../../../../support/user/team/utils';
import { getRunningUserInfoByTmbId } from '../../../../support/user/team/utils';

type Props = ModuleDispatchProps<{
Expand Down Expand Up @@ -99,7 +99,7 @@ export const dispatchRunAppNode = async (props: Props): Promise<Response> => {

// Rewrite children app variables
const systemVariables = filterSystemVariables(variables);
const { externalProvider } = await getUserChatInfoAndAuthTeamPoints(appData.tmbId);
const { externalProvider } = await getUserChatInfo(appData.tmbId);
const childrenRunVariables = {
...systemVariables,
...childrenAppVariables,
Expand Down Expand Up @@ -144,6 +144,7 @@ export const dispatchRunAppNode = async (props: Props): Promise<Response> => {
: {}),
runningAppInfo: {
id: String(appData._id),
name: appData.name,
teamId: String(appData.teamId),
tmbId: String(appData.tmbId),
isChildApp: true
Expand Down
1 change: 0 additions & 1 deletion packages/service/core/workflow/dispatch/dataset/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { type ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type
import { MongoDataset } from '../../../dataset/schema';
import { i18nT } from '../../../../../web/i18n/utils';
import { filterDatasetsByTmbId } from '../../../dataset/utils';
import { ModelTypeEnum } from '@fastgpt/global/core/ai/model';
import { getDatasetSearchToolResponsePrompt } from '../../../../../global/core/ai/prompt/dataset';
import { getNodeErrResponse } from '../utils';

Expand Down
77 changes: 69 additions & 8 deletions packages/service/core/workflow/dispatch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ import { rewriteRuntimeWorkFlow, removeSystemVariable } from './utils';
import { getHandleId } from '@fastgpt/global/core/workflow/utils';
import { callbackMap } from './constants';
import { anyValueDecrypt } from '../../../common/secret/utils';
import { getUserChatInfo } from '../../../support/user/team/utils';
import { checkTeamAIPoints } from '../../../support/permission/teamLimit';
import type { UsageSourceEnum } from '@fastgpt/global/support/wallet/usage/constants';
import { createChatUsageRecord, pushChatItemUsage } from '../../../support/wallet/usage/controller';
import type { RequireOnlyOne } from '@fastgpt/global/common/type/utils';

type Props = Omit<ChatDispatchProps, 'workflowDispatchDeep'> & {
type Props = Omit<ChatDispatchProps, 'workflowDispatchDeep' | 'timezone' | 'externalProvider'> & {
runtimeNodes: RuntimeNodeItemType[];
runtimeEdges: RuntimeEdgeItemType[];
defaultSkipNodeQueue?: WorkflowDebugResponse['skipNodeQueue'];
Expand All @@ -61,8 +66,38 @@ type NodeResponseCompleteType = Omit<NodeResponseType, 'responseData'> & {
};

// Run workflow
export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowResponse> {
const { res, stream, externalProvider } = data;
type WorkflowUsageProps = RequireOnlyOne<{
usageSource: UsageSourceEnum;
concatUsage: (points: number) => any;
usageId: string;
}>;
export async function dispatchWorkFlow({
usageSource,
usageId,
concatUsage,
...data
}: Props & WorkflowUsageProps): Promise<DispatchFlowResponse> {
const { res, stream, runningUserInfo, runningAppInfo, lastInteractive } = data;

await checkTeamAIPoints(runningUserInfo.teamId);
const [{ timezone, externalProvider }, newUsageId] = await Promise.all([
getUserChatInfo(runningUserInfo.tmbId),
(() => {
if (lastInteractive?.usageId) {
return lastInteractive.usageId;
}
if (usageSource) {
return createChatUsageRecord({
appName: runningAppInfo.name,
appId: runningAppInfo.id,
teamId: runningUserInfo.teamId,
tmbId: runningUserInfo.tmbId,
source: usageSource
});
}
return usageId;
})()
]);

let streamCheckTimer: NodeJS.Timeout | null = null;

Expand Down Expand Up @@ -96,15 +131,22 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
// Get default variables
const defaultVariables = {
...externalProvider.externalWorkflowVariables,
...getSystemVariables(data)
...getSystemVariables({
...data,
timezone
})
};

// Init some props
return runWorkflow({
...data,
timezone,
externalProvider,
defaultSkipNodeQueue: data.lastInteractive?.skipNodeQueue || data.defaultSkipNodeQueue,
variables: defaultVariables,
workflowDispatchDeep: 0
workflowDispatchDeep: 0,
usageId: newUsageId,
concatUsage
}).finally(() => {
if (streamCheckTimer) {
clearInterval(streamCheckTimer);
Expand All @@ -116,6 +158,8 @@ type RunWorkflowProps = ChatDispatchProps & {
runtimeNodes: RuntimeNodeItemType[];
runtimeEdges: RuntimeEdgeItemType[];
defaultSkipNodeQueue?: WorkflowDebugResponse['skipNodeQueue'];
usageId?: string;
concatUsage?: (points: number) => any;
};
export const runWorkflow = async (data: RunWorkflowProps): Promise<DispatchFlowResponse> => {
let {
Expand All @@ -129,7 +173,10 @@ export const runWorkflow = async (data: RunWorkflowProps): Promise<DispatchFlowR
retainDatasetCite = true,
version = 'v1',
responseDetail = true,
responseAllData = true
responseAllData = true,
usageId,
concatUsage,
runningUserInfo: { teamId }
} = data;

// Over max depth
Expand Down Expand Up @@ -572,6 +619,17 @@ export const runWorkflow = async (data: RunWorkflowProps): Promise<DispatchFlowR
}

if (nodeDispatchUsages) {
if (usageId) {
pushChatItemUsage({
teamId,
usageId,
nodeUsages: nodeDispatchUsages
});
}
if (concatUsage) {
concatUsage(nodeDispatchUsages.reduce((sum, item) => sum + (item.totalPoints || 0), 0));
}

this.chatNodeUsages = this.chatNodeUsages.concat(nodeDispatchUsages);
}

Expand Down Expand Up @@ -826,7 +884,8 @@ export const runWorkflow = async (data: RunWorkflowProps): Promise<DispatchFlowR
...edge,
status: entryNodeIds.includes(edge.target) ? 'active' : edge.status
})),
nodeOutputs
nodeOutputs,
usageId
};

// Tool call, not need interactive response
Expand Down Expand Up @@ -943,7 +1002,9 @@ const getSystemVariables = ({
uid,
chatConfig,
variables
}: Props): SystemVariablesType => {
}: Props & {
timezone: string;
}): SystemVariablesType => {
// Get global variables(Label -> key; Key -> key)
const variablesConfig = chatConfig?.variables || [];

Expand Down
5 changes: 3 additions & 2 deletions packages/service/core/workflow/dispatch/plugin/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { getPluginRunUserQuery } from '@fastgpt/global/core/workflow/utils';
import type { NodeInputKeyEnum, NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { getChildAppRuntimeById } from '../../../app/plugin/controller';
import { runWorkflow } from '../index';
import { getUserChatInfoAndAuthTeamPoints } from '../../../../support/permission/auth/team';
import { getUserChatInfo } from '../../../../support/user/team/utils';
import { dispatchRunTool } from '../child/runTool';
import type { PluginRuntimeType } from '@fastgpt/global/core/app/plugin/type';

Expand Down Expand Up @@ -111,7 +111,7 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
};
});

const { externalProvider } = await getUserChatInfoAndAuthTeamPoints(runningAppInfo.tmbId);
const { externalProvider } = await getUserChatInfo(runningAppInfo.tmbId);
const runtimeVariables = {
...filterSystemVariables(props.variables),
appId: String(plugin.id),
Expand All @@ -129,6 +129,7 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
: {}),
runningAppInfo: {
id: String(plugin.id),
name: plugin.name,
// 如果系统插件有 teamId 和 tmbId,则使用系统插件的 teamId 和 tmbId(管理员指定了插件作为系统插件)
teamId: plugin.teamId || runningAppInfo.teamId,
tmbId: plugin.tmbId || runningAppInfo.tmbId,
Expand Down
32 changes: 0 additions & 32 deletions packages/service/support/permission/auth/team.ts

This file was deleted.

25 changes: 25 additions & 0 deletions packages/service/support/user/team/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,28 @@ export async function getRunningUserInfoByTmbId(tmbId: string) {

return Promise.reject(TeamErrEnum.notUser);
}

export async function getUserChatInfo(tmbId: string) {
const tmb = await MongoTeamMember.findById(tmbId, 'userId teamId')
.populate<{ user: UserModelSchema; team: TeamSchema }>([
{
path: 'user',
select: 'timezone'
},
{
path: 'team',
select: 'openaiAccount externalWorkflowVariables'
}
])
.lean();

if (!tmb) return Promise.reject(TeamErrEnum.notUser);

return {
timezone: tmb.user.timezone,
externalProvider: {
openaiAccount: tmb.team.openaiAccount,
externalWorkflowVariables: tmb.team.externalWorkflowVariables
}
};
}
Loading
Loading