Skip to content

Commit

Permalink
perf: app template and sandbox params
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu committed Jul 4, 2024
1 parent 7129d32 commit d75ff68
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
3 changes: 2 additions & 1 deletion packages/web/i18n/en/user.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"permission": {
"Set read permission": "Read permission",
"Set write permission": "Write permission"
"Set write permission": "Write permission",
"Write": "Write"
},
"team": {
"Add manager": "Add manager"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const NodeTemplatesModal = ({ isOpen, onClose }: ModuleTemplateListProps) => {
const [parentId, setParentId] = useState<ParentIdType>('');
const [searchKey, setSearchKey] = useState('');
const { feConfigs } = useSystemStore();
const { basicNodeTemplates, hasToolNode, nodeList } = useContextSelector(
const { basicNodeTemplates, hasToolNode, nodeList, appId } = useContextSelector(
WorkflowContext,
(v) => v
);
Expand Down Expand Up @@ -110,7 +110,7 @@ const NodeTemplatesModal = ({ isOpen, onClose }: ModuleTemplateListProps) => {
parentId,
searchKey,
type: [AppTypeEnum.folder, AppTypeEnum.httpPlugin, AppTypeEnum.plugin]
});
}).then((res) => res.filter((app) => app.id !== appId));
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions projects/app/src/web/core/app/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ export const workflowTemplates: TemplateType = [
renderTypeList: ['custom'],
label: '',
value:
'function main({data1}){\n const result = data1.split("```").filter(item => !!item.trim())\n\n if(result[result.length-1]) {\n return {\n result: result[result.length-1]\n }\n }\n\n return {\n result: \'未截取到翻译内容\'\n }\n}'
"function main({data1}) {\n const codeBlocks = data1.match(/```[\\s\\S]*?```/g);\n\n if (codeBlocks && codeBlocks.length > 0) {\n const lastCodeBlock = codeBlocks[codeBlocks.length - 1];\n const cleanedCodeBlock = lastCodeBlock.replace(/```[a-zA-Z]*|```/g, '').trim();\n \n return {\n result: cleanedCodeBlock\n };\n }\n\n return {\n result: '未截取到代码块内容'\n };\n}\n"
},
{
key: 'data1',
Expand Down Expand Up @@ -1506,7 +1506,7 @@ export const workflowTemplates: TemplateType = [
required: true,
label: '拼接文本',
placeholder: '可通过 {{字段名}} 来引用变量',
value: '------\n\n最终翻译结果如下: \n\n```\n{{result}}\n```'
value: '****** \n\n最终翻译结果如下: \n\n```\n{{result}}\n```'
},
{
renderTypeList: ['reference'],
Expand Down
2 changes: 1 addition & 1 deletion projects/sandbox/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ COPY --from=builder /app/projects/sandbox /app/projects/sandbox

ENV NODE_ENV=production

CMD ["node", "projects/sandbox/dist/main.js"]
CMD ["node", "--no-node-snapshot", "projects/sandbox/dist/main.js"]
2 changes: 1 addition & 1 deletion projects/sandbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"build": "nest build",
"start": "nest start",
"dev": "nest start --watch",
"dev": "NODE_OPTIONS='--no-node-snapshot' nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
Expand Down
63 changes: 32 additions & 31 deletions projects/sandbox/src/worker/runJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,44 @@ const ivm = require('isolated-vm');
parentPort?.on('message', ({ code, variables = {} }: RunCodeDto) => {
const resolve = (data: RunCodeResponse) => workerResponse({ parentPort, type: 'success', data });
const reject = (error: any) => workerResponse({ parentPort, type: 'error', data: error });

const isolate = new ivm.Isolate({ memoryLimit: 32 });
const context = isolate.createContextSync();
const jail = context.global;

// custom function
const logData = [];
const CustomLogStr = 'CUSTOM_LOG';
code = code.replace(/console\.log/g, `${CustomLogStr}`);
jail.setSync(CustomLogStr, function (...args) {
logData.push(
args
.map((item) => (typeof item === 'object' ? JSON.stringify(item, null, 2) : item))
.join(', ')
);
});

jail.setSync('responseData', function (args: any): any {
if (typeof args === 'object') {
resolve({
codeReturn: args,
log: logData.join('\n')
});
} else {
reject('Not an invalid response, must return an object');
}
});

// Add global variables
jail.setSync('variables', new ivm.ExternalCopy(variables).copyInto());

try {
const isolate = new ivm.Isolate({ memoryLimit: 32 });
const context = isolate.createContextSync();
const jail = context.global;

// custom function
const logData = [];
const CustomLogStr = 'CUSTOM_LOG';
code = code.replace(/console\.log/g, `${CustomLogStr}`);
jail.setSync(CustomLogStr, function (...args) {
logData.push(
args
.map((item) => (typeof item === 'object' ? JSON.stringify(item, null, 2) : item))
.join(', ')
);
});

jail.setSync('responseData', function (args: any): any {
if (typeof args === 'object') {
resolve({
codeReturn: args,
log: logData.join('\n')
});
} else {
reject('Not an invalid response, must return an object');
}
});

// Add global variables
jail.setSync('variables', new ivm.ExternalCopy(variables).copyInto());

const scriptCode = `
${code}
responseData(main(variables))`;

context.evalSync(scriptCode, { timeout: 6000 });
} catch (err) {
console.log(err);
reject(err);
}

Expand Down

0 comments on commit d75ff68

Please sign in to comment.