mirror of
https://github.com/labring/FastGPT.git
synced 2026-04-27 02:08:10 +08:00
95f0166275
* sandbox version * add sandbox log * update lock * fix * fix: sandbox * doc * add console * i18n
76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
import { SandboxCodeTypeEnum } from '@fastgpt/global/core/workflow/template/system/sandbox/constants';
|
|
import type { AxiosInstance } from 'axios';
|
|
import axios from 'axios';
|
|
import { getLogger, LogCategories } from '../../common/logger';
|
|
const logger = getLogger(LogCategories.MODULE.WORKFLOW.CODE_SANDBOX);
|
|
|
|
export type SanndboxPackagesResponse = {
|
|
js: string[];
|
|
python: string[];
|
|
builtinGlobals: string[];
|
|
};
|
|
|
|
export class CodeSandbox {
|
|
private readonly client: AxiosInstance;
|
|
|
|
constructor() {
|
|
const baseUrl = process.env.SANDBOX_URL || '';
|
|
const token = process.env.SANDBOX_TOKEN || '';
|
|
|
|
this.client = axios.create({
|
|
baseURL: `${baseUrl.replace(/\/$/, '')}/sandbox`,
|
|
timeout: 180000,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: token ? `Bearer ${token}` : undefined
|
|
}
|
|
});
|
|
|
|
this.client.interceptors.response.use(
|
|
(response) => {
|
|
const data = response.data;
|
|
if (!data.success) {
|
|
logger.warn('Request code sandbox failed', { data });
|
|
return Promise.reject(new Error(data.message || 'Request code sandbox failed'));
|
|
}
|
|
return response.data;
|
|
},
|
|
(error) => {
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
}
|
|
|
|
async getPackages() {
|
|
const { data } = await this.client.get<SanndboxPackagesResponse>('/modules');
|
|
return data;
|
|
}
|
|
|
|
async runCode({
|
|
codeType,
|
|
code,
|
|
variables
|
|
}: {
|
|
codeType: string;
|
|
code: string;
|
|
variables: Record<string, any>;
|
|
}) {
|
|
const url = (() => {
|
|
if (codeType == SandboxCodeTypeEnum.py) {
|
|
return `/python`;
|
|
} else {
|
|
return `/js`;
|
|
}
|
|
})();
|
|
|
|
const { data } = await this.client.post<{
|
|
codeReturn: Record<string, any>;
|
|
log: string;
|
|
}>(url, { code, variables });
|
|
|
|
return data;
|
|
}
|
|
}
|
|
|
|
export const codeSandbox = new CodeSandbox();
|