Files
FastGPT/document/content/docs/introduction/guide/dashboard/workflow/sandbox.mdx
Archer fe7abf22a9 New document (#5299)
* add new doc (#5175)

Co-authored-by: dreamer6680 <146868355@qq.com>

* Test docs (#5235)

* fix: change the page of doc

* chore: add new dependencies, update global styles/layout, optimize docs, add Feishu & GitHub icons, update API examples

* fix: docs/index 404 not found

* Update environment variable names, optimize styles, add new API routes, fix component styles, adjust documentation, and update GitHub and Feishu icons

* update readme

* feat: add a linkfastgpt compontent

* feat: update new doc

* fix:remove unuse page and redirect homepage to docs (#5288)

* fix:remove some unuse doc

* fix: redirect homepage to doc

* git ignore

* fix:navbar to index (#5295)

* sidbar

* fix: navtab unlight (#5298)

* doc

---------

Co-authored-by: dreamer6680 <1468683855@qq.com>
Co-authored-by: dreamer6680 <146868355@qq.com>
2025-07-23 21:35:03 +08:00

98 lines
2.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 代码运行
description: FastGPT 代码运行节点介绍
---
![alt text](/imgs/image.png)
## 功能
可用于执行一段简单的 js 代码用于进行一些复杂的数据处理。代码运行在沙盒中无法进行网络请求、dom和异步操作。如需复杂操作需外挂 HTTP 实现。
**注意事项**
- 私有化用户需要部署`fastgpt-sandbox` 镜像,并配置`SANDBOX_URL`环境变量。
- 沙盒最大运行 10s 32M 内存限制。
## 变量输入
可在自定义输入中添加代码运行需要的变量,在代码的 main 函数中,可解构出相同名字的变量。
如上图,自定义输入中有 data1 和 data2 两个变量main 函数中可以解构出相同名字的变量。
## 结果输出
务必返回一个 object 对象
自定义输出中,可以添加变量名来获取 object 对应 key 下的值。例如上图中,返回了一个对象:
```json
{
result: data1,
data2
}
```
他有 2 个 keyresult和 data2(js 缩写key=data2value=data2)。这时候自定义输出中就可以添加 2 个变量来获取对应 key 下的 value。
## 内置 JS 全局变量
### delay 延迟
延迟 1 秒后返回
```js
async function main({data1, data2}){
await delay(1000)
return {
result: "111"
}
}
```
### countToken 统计 token
```js
function main({input}){
return {
result: countToken(input)
}
}
```
![alt text](/imgs/image-1.png)
### strToBase64 字符串转 base64(4.8.11 版本新增)
可用于将 SVG 图片转换为 base64 格式展示。
```js
function main({input}){
return {
/*
param1: input 需要转换的字符串
param2: base64 prefix 前缀
*/
result: strToBase64(input,'data:image/svg+xml;base64,')
}
}
```
![alt text](/imgs/image-2.png)
### createHmac 加密
与 node 中 crypto 的 createHmac 方法一致。
```js
function main({secret}){
const {sign,timestamp} = createHmac('sha256',secret)
return {
sign,timestamp
}
}
```