Skip to content

Commit

Permalink
4.8.10 test (#2601)
Browse files Browse the repository at this point in the history
* perf: workflow children run params

* feat: workflow userId

* fix: ui size

* perf: Markdown whitespace and ai images split

* fix: openai sdk ts
  • Loading branch information
c121914yu authored Sep 3, 2024
1 parent 761e35c commit 9a57e94
Show file tree
Hide file tree
Showing 43 changed files with 319 additions and 289 deletions.
23 changes: 17 additions & 6 deletions packages/global/core/chat/adapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,23 @@ export const GPTMessages2Chats = (
obj === ChatRoleEnum.System &&
item.role === ChatCompletionRequestMessageRoleEnum.System
) {
value.push({
type: ChatItemValueTypeEnum.text,
text: {
content: item.content
}
});
if (Array.isArray(item.content)) {
item.content.forEach((item) => [
value.push({
type: ChatItemValueTypeEnum.text,
text: {
content: item.text
}
})
]);
} else {
value.push({
type: ChatItemValueTypeEnum.text,
text: {
content: item.content
}
});
}
} else if (
obj === ChatRoleEnum.Human &&
item.role === ChatCompletionRequestMessageRoleEnum.User
Expand Down
15 changes: 11 additions & 4 deletions packages/global/core/workflow/runtime/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ export type ChatDispatchProps = {
res?: NextApiResponse;
requestOrigin?: string;
mode: 'test' | 'chat' | 'debug';
teamId: string; // App teamId
tmbId: string; // App tmbId
user: UserModelSchema;
app: AppDetailType | AppSchema;

runningAppInfo: {
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
};
uid: string; // Who run this workflow

chatId?: string;
responseChatItemId?: string;
histories: ChatItemType[];
Expand All @@ -50,10 +55,12 @@ export type ModuleDispatchProps<T> = ChatDispatchProps & {
};

export type SystemVariablesType = {
userId: string;
appId: string;
chatId?: string;
responseChatItemId?: string;
histories: ChatItemType[];
cTime: string;
};

/* node props */
Expand All @@ -69,7 +76,7 @@ export type RuntimeNodeItemType = {
inputs: FlowNodeInputItemType[];
outputs: FlowNodeOutputItemType[];

pluginId?: string;
pluginId?: string; // workflow id / plugin id
};

export type PluginRuntimeType = {
Expand Down
4 changes: 2 additions & 2 deletions packages/global/core/workflow/template/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { RunAppModule } from './system/abandoned/runApp/index';
import { PluginInputModule } from './system/pluginInput';
import { PluginOutputModule } from './system/pluginOutput';
import { RunPluginModule } from './system/runPlugin';
import { RunAppPluginModule } from './system/runAppPlugin';
import { RunAppNode } from './system/runApp';
import { AiQueryExtension } from './system/queryExtension';

import type { FlowNodeTemplateType } from '../type/node';
Expand Down Expand Up @@ -73,6 +73,6 @@ export const moduleTemplatesFlat: FlowNodeTemplateType[] = [
),
EmptyNode,
RunPluginModule,
RunAppPluginModule,
RunAppNode,
RunAppModule
];
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FlowNodeTypeEnum } from '../../node/constant';
import { FlowNodeTemplateType } from '../../type/node';
import { getHandleConfig } from '../utils';

export const RunAppPluginModule: FlowNodeTemplateType = {
export const RunAppNode: FlowNodeTemplateType = {
id: FlowNodeTypeEnum.appModule,
templateType: FlowNodeTemplateTypeEnum.other,
flowNodeType: FlowNodeTypeEnum.appModule,
Expand Down
21 changes: 0 additions & 21 deletions packages/global/core/workflow/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,27 +326,6 @@ export const updatePluginInputByVariables = (
);
};

/* Remove pluginInput variables from global variables
(completions api: Plugin input get value from global variables)
*/
export const removePluginInputVariables = (
variables: Record<string, any>,
nodes: RuntimeNodeItemType[]
) => {
const pluginInputNode = nodes.find((node) => node.flowNodeType === FlowNodeTypeEnum.pluginInput);

if (!pluginInputNode) return variables;
return Object.keys(variables).reduce(
(acc, key) => {
if (!pluginInputNode.inputs.find((input) => input.key === key)) {
acc[key] = variables[key];
}
return acc;
},
{} as Record<string, any>
);
};

// replace {{$xx.xx$}} variables for text
export function replaceEditorVariable({
text,
Expand Down
2 changes: 1 addition & 1 deletion packages/global/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"jschardet": "3.1.1",
"nanoid": "^4.0.1",
"next": "14.2.5",
"openai": "4.53.0",
"openai": "4.57.0",
"openapi-types": "^12.1.3",
"timezones-list": "^3.0.2"
},
Expand Down
54 changes: 15 additions & 39 deletions packages/service/core/chat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,46 +121,22 @@ export const loadRequestMessages = async ({
const imageRegex =
/(https?:\/\/[^\s/$.?#].[^\s]*\.(?:png|jpe?g|gif|webp|bmp|tiff?|svg|ico|heic|avif))/i;

const result: { type: 'text' | 'image'; value: string }[] = [];
let lastIndex = 0;
let match;

// 使用正则表达式查找所有匹配项
while ((match = imageRegex.exec(input.slice(lastIndex))) !== null) {
const textBefore = input.slice(lastIndex, lastIndex + match.index);

// 如果图片URL前有文本,添加文本部分
if (textBefore) {
result.push({ type: 'text', value: textBefore });
}

// 添加图片URL
result.push({ type: 'image', value: match[0] });

lastIndex += match.index + match[0].length;
}

// 添加剩余的文本(如果有的话)
if (lastIndex < input.length) {
result.push({ type: 'text', value: input.slice(lastIndex) });
}

return result
.map((item) => {
if (item.type === 'text') {
return { type: 'text', text: item.value };
}
if (item.type === 'image') {
return {
type: 'image_url',
image_url: {
url: item.value
}
};
const result: ChatCompletionContentPart[] = [];

// 提取所有HTTPS图片URL并添加到result开头
const httpsImages = input.match(imageRegex) || [];
httpsImages.forEach((url) => {
result.push({
type: 'image_url',
image_url: {
url: url
}
return { type: 'text', text: item.value };
})
.filter(Boolean) as ChatCompletionContentPart[];
});
});

// 添加原始input作为文本
result.push({ type: 'text', text: input });
return result;
}
// Load image
const parseUserContent = async (content: string | ChatCompletionContentPart[]) => {
Expand Down
10 changes: 7 additions & 3 deletions packages/service/core/workflow/dispatch/abandoned/runApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Response = DispatchNodeResultType<{

export const dispatchAppRequest = async (props: Props): Promise<Response> => {
const {
app: workflowApp,
runningAppInfo,
workflowStreamResponse,
histories,
query,
Expand All @@ -45,7 +45,7 @@ export const dispatchAppRequest = async (props: Props): Promise<Response> => {
// 检查该工作流的tmb是否有调用该app的权限(不是校验对话的人,是否有权限)
const { app: appData } = await authAppByTmbId({
appId: app.id,
tmbId: workflowApp.tmbId,
tmbId: runningAppInfo.tmbId,
per: ReadPermissionVal
});

Expand All @@ -61,7 +61,11 @@ export const dispatchAppRequest = async (props: Props): Promise<Response> => {

const { flowResponses, flowUsages, assistantResponses } = await dispatchWorkFlow({
...props,
app: appData,
runningAppInfo: {
id: String(appData._id),
teamId: String(appData.teamId),
tmbId: String(appData.tmbId)
},
runtimeNodes: storeNodes2RuntimeNodes(
appData.modules,
getWorkflowEntryNodeIds(appData.modules)
Expand Down
2 changes: 1 addition & 1 deletion packages/service/core/workflow/dispatch/dataset/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function dispatchDatasetSearch(
props: DatasetSearchProps
): Promise<DatasetSearchResponse> {
const {
teamId,
runningAppInfo: { teamId },
histories,
node,
params: {
Expand Down
13 changes: 8 additions & 5 deletions packages/service/core/workflow/dispatch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
import { NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import type {
ChatDispatchProps,
ModuleDispatchProps
ModuleDispatchProps,
SystemVariablesType
} from '@fastgpt/global/core/workflow/runtime/type';
import type { RuntimeNodeItemType } from '@fastgpt/global/core/workflow/runtime/type.d';
import type {
Expand Down Expand Up @@ -554,13 +555,15 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
/* get system variable */
export function getSystemVariable({
user,
app,
runningAppInfo,
chatId,
responseChatItemId,
histories = []
}: Props) {
histories = [],
uid
}: Props): SystemVariablesType {
return {
appId: String(app._id),
userId: uid,
appId: String(runningAppInfo.id),
chatId,
responseChatItemId,
histories,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type UserSelectResponse = DispatchNodeResultType<{
export const dispatchUserSelect = async (props: Props): Promise<UserSelectResponse> => {
const {
workflowStreamResponse,
app: { _id: appId },
runningAppInfo: { id: appId },
chatId,
node: { nodeId, isEntry },
params: { description, userSelectOptions },
Expand Down
25 changes: 16 additions & 9 deletions packages/service/core/workflow/dispatch/plugin/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { authPluginByTmbId } from '../../../../support/permission/app/auth';
import { ReadPermissionVal } from '@fastgpt/global/support/permission/constant';
import { computedPluginUsage } from '../../../app/plugin/utils';
import { filterSystemVariables } from '../utils';
import { getPluginRunUserQuery } from '../../utils';

type RunPluginProps = ModuleDispatchProps<{
[key: string]: any;
Expand All @@ -22,9 +23,8 @@ type RunPluginResponse = DispatchNodeResultType<{}>;
export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPluginResponse> => {
const {
node: { pluginId },
app: workflowApp,
runningAppInfo,
mode,
teamId,
params: data // Plugin input
} = props;

Expand All @@ -33,9 +33,9 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
}

// auth plugin
await authPluginByTmbId({
const pluginData = await authPluginByTmbId({
appId: pluginId,
tmbId: workflowApp.tmbId,
tmbId: runningAppInfo.tmbId,
per: ReadPermissionVal
});

Expand All @@ -61,14 +61,21 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
showStatus: false
};
});
const runtimeVariables = {
...filterSystemVariables(props.variables),
appId: String(plugin.id)
};

const { flowResponses, flowUsages, assistantResponses } = await dispatchWorkFlow({
...props,

variables: {
...filterSystemVariables(props.variables),
appId: String(plugin.id)
runningAppInfo: {
id: String(plugin.id),
teamId: plugin.teamId || '',
tmbId: pluginData?.tmbId || ''
},
variables: runtimeVariables,
query: getPluginRunUserQuery(plugin.nodes, runtimeVariables).value,
chatConfig: {},
runtimeNodes,
runtimeEdges: initWorkflowEdgeStatus(plugin.edges)
});
Expand All @@ -90,7 +97,7 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
totalPoints: usagePoints,
pluginOutput: output?.pluginOutput,
pluginDetail:
mode === 'test' && plugin.teamId === teamId
mode === 'test' && plugin.teamId === runningAppInfo.teamId
? flowResponses.filter((item) => {
const filterArr = [FlowNodeTypeEnum.pluginOutput];
return !filterArr.includes(item.moduleType as any);
Expand Down
Loading

0 comments on commit 9a57e94

Please sign in to comment.