mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-22 20:37:48 +00:00
@@ -1,38 +0,0 @@
|
||||
// import { addLog } from '../../../../common/system/log';
|
||||
// const ivm = require('isolated-vm');
|
||||
|
||||
// export const runJsCode = ({
|
||||
// code,
|
||||
// variables
|
||||
// }: {
|
||||
// code: string;
|
||||
// variables: Record<string, any>;
|
||||
// }) => {
|
||||
// const isolate = new ivm.Isolate({ memoryLimit: 16 });
|
||||
// const context = isolate.createContextSync();
|
||||
// const jail = context.global;
|
||||
|
||||
// return new Promise((resolve, reject) => {
|
||||
// // custom log function
|
||||
// jail.setSync('responseData', function (args: any): any {
|
||||
// if (typeof args === 'object') {
|
||||
// resolve(args);
|
||||
// } else {
|
||||
// reject('Not an invalid response');
|
||||
// }
|
||||
// });
|
||||
|
||||
// // Add global variables
|
||||
// jail.setSync('variables', new ivm.ExternalCopy(variables).copyInto());
|
||||
|
||||
// try {
|
||||
// const scriptCode = `
|
||||
// ${code}
|
||||
// responseData(main(variables))`;
|
||||
// context.evalSync(scriptCode, { timeout: 2000 });
|
||||
// } catch (err) {
|
||||
// addLog.error('Error during script execution:', err);
|
||||
// reject(err);
|
||||
// }
|
||||
// });
|
||||
// };
|
51
packages/service/core/workflow/dispatch/code/run.ts
Normal file
51
packages/service/core/workflow/dispatch/code/run.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import type { ModuleDispatchProps } from '@fastgpt/global/core/workflow/type/index.d';
|
||||
import { NodeInputKeyEnum, NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
|
||||
import { DispatchNodeResultType } from '@fastgpt/global/core/workflow/runtime/type';
|
||||
import axios from 'axios';
|
||||
import { formatHttpError } from '../utils';
|
||||
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
|
||||
|
||||
type RunCodeType = ModuleDispatchProps<{
|
||||
[NodeInputKeyEnum.codeType]: 'js';
|
||||
[NodeInputKeyEnum.code]: string;
|
||||
[key: string]: any;
|
||||
}>;
|
||||
type RunCodeResponse = DispatchNodeResultType<{
|
||||
[NodeOutputKeyEnum.error]?: any;
|
||||
[NodeOutputKeyEnum.rawResponse]?: Record<string, any>;
|
||||
[key: string]: any;
|
||||
}>;
|
||||
|
||||
export const dispatchRunCode = async (props: RunCodeType): Promise<RunCodeResponse> => {
|
||||
const {
|
||||
params: { codeType, code, ...customVariables }
|
||||
} = props;
|
||||
|
||||
const sandBoxRequestUrl = `${process.env.SANDBOX_URL}/sandbox/js`;
|
||||
try {
|
||||
const { data: runResult } = await axios.post<{
|
||||
success: boolean;
|
||||
data: Record<string, any>;
|
||||
}>(sandBoxRequestUrl, {
|
||||
code,
|
||||
variables: customVariables
|
||||
});
|
||||
|
||||
if (runResult.success) {
|
||||
return {
|
||||
[NodeOutputKeyEnum.rawResponse]: runResult.data,
|
||||
[DispatchNodeResponseKeyEnum.nodeResponse]: {
|
||||
customInputs: customVariables,
|
||||
customOutputs: runResult.data
|
||||
},
|
||||
...runResult.data
|
||||
};
|
||||
} else {
|
||||
throw new Error('Run code failed');
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
[NodeOutputKeyEnum.error]: formatHttpError(error)
|
||||
};
|
||||
}
|
||||
};
|
@@ -46,6 +46,7 @@ import { dispatchSystemConfig } from './init/systemConfig';
|
||||
import { dispatchUpdateVariable } from './tools/runUpdateVar';
|
||||
import { addLog } from '../../../common/system/log';
|
||||
import { surrenderProcess } from '../../../common/system/tools';
|
||||
import { dispatchRunCode } from './code/run';
|
||||
|
||||
const callbackMap: Record<FlowNodeTypeEnum, Function> = {
|
||||
[FlowNodeTypeEnum.workflowStart]: dispatchWorkflowStart,
|
||||
@@ -66,6 +67,7 @@ const callbackMap: Record<FlowNodeTypeEnum, Function> = {
|
||||
[FlowNodeTypeEnum.lafModule]: dispatchLafRequest,
|
||||
[FlowNodeTypeEnum.ifElseNode]: dispatchIfElse,
|
||||
[FlowNodeTypeEnum.variableUpdate]: dispatchUpdateVariable,
|
||||
[FlowNodeTypeEnum.code]: dispatchRunCode,
|
||||
|
||||
// none
|
||||
[FlowNodeTypeEnum.systemConfig]: dispatchSystemConfig,
|
||||
|
@@ -9,7 +9,7 @@ import {
|
||||
SseResponseEventEnum
|
||||
} from '@fastgpt/global/core/workflow/runtime/constants';
|
||||
import axios from 'axios';
|
||||
import { valueTypeFormat } from '../utils';
|
||||
import { formatHttpError, valueTypeFormat } from '../utils';
|
||||
import { SERVICE_LOCAL_HOST } from '../../../../common/system/tools';
|
||||
import { addLog } from '../../../../common/system/log';
|
||||
import { DispatchNodeResultType } from '@fastgpt/global/core/workflow/runtime/type';
|
||||
@@ -310,14 +310,3 @@ function removeUndefinedSign(obj: Record<string, any>) {
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
function formatHttpError(error: any) {
|
||||
return {
|
||||
message: error?.message,
|
||||
name: error?.name,
|
||||
method: error?.config?.method,
|
||||
baseURL: error?.config?.baseURL,
|
||||
url: error?.config?.url,
|
||||
code: error?.code,
|
||||
status: error?.status
|
||||
};
|
||||
}
|
||||
|
@@ -3,12 +3,7 @@ import {
|
||||
WorkflowIOValueTypeEnum,
|
||||
NodeOutputKeyEnum
|
||||
} from '@fastgpt/global/core/workflow/constants';
|
||||
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
|
||||
import {
|
||||
RuntimeEdgeItemType,
|
||||
RuntimeNodeItemType
|
||||
} from '@fastgpt/global/core/workflow/runtime/type';
|
||||
import { StoreNodeItemType } from '@fastgpt/global/core/workflow/type/index.d';
|
||||
import { RuntimeEdgeItemType } from '@fastgpt/global/core/workflow/runtime/type';
|
||||
|
||||
export const filterToolNodeIdByEdges = ({
|
||||
nodeId,
|
||||
@@ -91,3 +86,15 @@ export const removeSystemVariable = (variables: Record<string, any>) => {
|
||||
|
||||
return copyVariables;
|
||||
};
|
||||
|
||||
export const formatHttpError = (error: any) => {
|
||||
return {
|
||||
message: error?.message,
|
||||
name: error?.name,
|
||||
method: error?.config?.method,
|
||||
baseURL: error?.config?.baseURL,
|
||||
url: error?.config?.url,
|
||||
code: error?.code,
|
||||
status: error?.status
|
||||
};
|
||||
};
|
||||
|
Reference in New Issue
Block a user