mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 05:12:39 +00:00
feat: sandbox support str to base64 function;feat: compiance text ui (#2702)
* feat: sandbox strToBase64 function * feat: compiance text ui
This commit is contained in:
5
projects/sandbox/src/sandbox/jsFn/str2Base64.ts
Normal file
5
projects/sandbox/src/sandbox/jsFn/str2Base64.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export const strToBase64 = (str: string, prefix: string) => {
|
||||
const base64_string = Buffer.from(str, 'utf-8').toString('base64');
|
||||
|
||||
return `${prefix}${base64_string}`;
|
||||
};
|
@@ -2,13 +2,18 @@ import { RunCodeDto, RunCodeResponse } from 'src/sandbox/dto/create-sandbox.dto'
|
||||
import IsolatedVM, { ExternalCopy, Isolate, Reference } from 'isolated-vm';
|
||||
import { countToken } from './jsFn/tiktoken';
|
||||
import { timeDelay } from './jsFn/delay';
|
||||
import { strToBase64 } from './jsFn/str2Base64';
|
||||
|
||||
const CustomLogStr = 'CUSTOM_LOG';
|
||||
|
||||
/*
|
||||
Rewrite code to add custom functions: Promise function; Log.
|
||||
*/
|
||||
Rewrite code to add custom functions: Promise function; Log.
|
||||
*/
|
||||
function getFnCode(code: string) {
|
||||
// rewrite log
|
||||
code = code.replace(/console\.log/g, `${CustomLogStr}`);
|
||||
|
||||
// Promise function rewrite
|
||||
const rewriteSystemFn = `
|
||||
const thisDelay = (...args) => global_delay.applySyncPromise(undefined,args)
|
||||
`;
|
||||
@@ -16,9 +21,6 @@ function getFnCode(code: string) {
|
||||
// rewrite delay
|
||||
code = code.replace(/delay\((.*)\)/g, `thisDelay($1)`);
|
||||
|
||||
// rewrite log
|
||||
code = code.replace(/console\.log/g, `${CustomLogStr}`);
|
||||
|
||||
const runCode = `
|
||||
(async() => {
|
||||
try {
|
||||
@@ -35,11 +37,12 @@ function getFnCode(code: string) {
|
||||
return runCode;
|
||||
}
|
||||
|
||||
// Register global function
|
||||
function registerSystemFn(jail: IsolatedVM.Reference<Record<string | number | symbol, any>>) {
|
||||
return Promise.all([
|
||||
// delay
|
||||
jail.set('global_delay', new Reference(timeDelay)),
|
||||
jail.set('countToken', countToken)
|
||||
jail.set('countToken', countToken),
|
||||
jail.set('strToBase64', strToBase64)
|
||||
]);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user