mirror of
https://github.com/langgenius/dify-docs.git
synced 2026-04-03 14:38:36 +07:00
* fix redirect language code prefixes * rename: cn -> zh, jp -> ja * remove hardcoded ja / zh references * remove hardcoded 'english' references * renamed variable names and dict keys to language agnostic names * fix: add missing language helper methods to PRAnalyzer - Add get_language_directory() method - Initialize source_language and target_languages from config - Fixes AttributeError when generating mixed PR errors 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * test: kitchen sink workflow validation v2 This PR validates the translation workflow after config-driven refactoring: Changes: - Add new test file: test-workflow-validation.mdx - Modify existing file: introduction.mdx - Update docs.json navigation Tests: - New file translation (add workflow) - Existing file translation (update workflow) - Navigation sync across languages - Config-driven language codes (zh/ja instead of cn/jp) - Source language abstraction (no hardcoded "English") Expected workflow behavior: 1. Detect changes in en/ directory 2. Translate new file to zh and ja 3. Update modified file translations 4. Sync docs.json for all language sections 5. Commit translated files automatically 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * fix: update workflow paths to use zh/ja instead of cn/jp Aligns workflow trigger paths with the zh/ja language directory rename. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Revert "fix: update workflow paths to use zh/ja instead of cn/jp" This reverts commit9587b7cc5d. * Revert "test: kitchen sink workflow validation v2" This reverts commit4abdd69fd2. * fix: update workflow paths in doc analyze workflow to use zh/ja instead of cn/jp * Refactor/language codes (#240) * test: kitchen sink workflow validation v2 This PR validates the translation workflow after config-driven refactoring: Changes: - Add new test file: test-workflow-validation.mdx - Modify existing file: introduction.mdx - Update docs.json navigation Tests: - New file translation (add workflow) - Existing file translation (update workflow) - Navigation sync across languages - Config-driven language codes (zh/ja instead of cn/jp) - Source language abstraction (no hardcoded "English") Expected workflow behavior: 1. Detect changes in en/ directory 2. Translate new file to zh and ja 3. Update modified file translations 4. Sync docs.json for all language sections 5. Commit translated files automatically 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * fix: update workflow paths to use zh/ja instead of cn/jp Aligns workflow trigger paths with the zh/ja language directory rename. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Revert "fix: update workflow paths to use zh/ja instead of cn/jp" This reverts commit9587b7cc5d. * Revert "test: kitchen sink workflow validation v2" This reverts commit4abdd69fd2. * fix: update workflow paths in doc analyze workflow to use zh/ja instead of cn/jp --------- Co-authored-by: Claude <noreply@anthropic.com> * fix: update workflow files to use 'source' instead of 'english' After refactoring the PR analyzer to use 'source' for source language PRs (instead of hardcoded 'english'), the workflow files need to match. Changes: - sync_docs_analyze.yml: pr_type == 'source' (was 'english') - sync_docs_update.yml: PR_TYPE != 'source' check - Updated all comments from "English" to "source language" - Updated all pr_type values in JSON from "english" to "source" This ensures the workflows trigger correctly with the refactored config-driven language system. Related: language code refactoring (cn/jp → zh/ja) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix/workflow source language references (#245) * test: kitchen sink workflow validation v2 This PR validates the translation workflow after config-driven refactoring: Changes: - Add new test file: test-workflow-validation.mdx - Modify existing file: introduction.mdx - Update docs.json navigation Tests: - New file translation (add workflow) - Existing file translation (update workflow) - Navigation sync across languages - Config-driven language codes (zh/ja instead of cn/jp) - Source language abstraction (no hardcoded "English") Expected workflow behavior: 1. Detect changes in en/ directory 2. Translate new file to zh and ja 3. Update modified file translations 4. Sync docs.json for all language sections 5. Commit translated files automatically 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * fix: update workflow paths to use zh/ja instead of cn/jp Aligns workflow trigger paths with the zh/ja language directory rename. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Revert "fix: update workflow paths to use zh/ja instead of cn/jp" This reverts commit9587b7cc5d. * Revert "test: kitchen sink workflow validation v2" This reverts commit4abdd69fd2. * fix: update workflow paths in doc analyze workflow to use zh/ja instead of cn/jp * fix: update workflow files to use 'source' instead of 'english' After refactoring the PR analyzer to use 'source' for source language PRs (instead of hardcoded 'english'), the workflow files need to match. Changes: - sync_docs_analyze.yml: pr_type == 'source' (was 'english') - sync_docs_update.yml: PR_TYPE != 'source' check - Updated all comments from "English" to "source language" - Updated all pr_type values in JSON from "english" to "source" This ensures the workflows trigger correctly with the refactored config-driven language system. Related: language code refactoring (cn/jp → zh/ja) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com> * fix * fix docs.json language codes * rename previous version docs: cn -> zh, jp -> ja * rm duplicate redirect entires --------- Co-authored-by: Claude <noreply@anthropic.com>
137 lines
4.5 KiB
Plaintext
137 lines
4.5 KiB
Plaintext
---
|
||
title: "代码"
|
||
icon: "code"
|
||
---
|
||
|
||
<Note> ⚠️ 本文档由 AI 自动翻译。如有任何不准确之处,请参考[英文原版](/en/use-dify/nodes/code)。</Note>
|
||
|
||
|
||
代码节点执行自定义 Python 或 JavaScript 来处理工作流中复杂的数据转换、计算和逻辑。当预设节点无法满足你的特定处理需求时可以使用它。
|
||
|
||
<Frame caption="代码节点配置界面">
|
||
<img src="https://assets-docs.dify.ai/dify-enterprise-mintlify/en/guides/workflow/node/9969aa1bc1912aebe366f5d8f5dde296.png" alt="Code node interface" />
|
||
</Frame>
|
||
|
||
## 配置
|
||
|
||
定义**输入变量**以访问工作流中其他节点的数据,然后在代码中引用这些变量。你的函数必须返回一个包含你已声明的**输出变量**的字典。
|
||
|
||
```python
|
||
def main(input_variable: str) -> dict:
|
||
# Process the input
|
||
result = input_variable.upper()
|
||
return {
|
||
'output_variable': result
|
||
}
|
||
```
|
||
|
||
## 语言支持
|
||
|
||
根据你的需求和熟悉程度在 **Python** 和 **JavaScript** 之间进行选择。两种语言都在安全沙箱中运行,并可访问用于数据处理的常用库。
|
||
|
||
<Tabs>
|
||
<Tab title="Python">
|
||
Python 包含标准库,如 `json`、`math`、`datetime` 和 `re`。非常适合数据分析、数学运算和文本处理。
|
||
|
||
```python
|
||
def main(data: list) -> dict:
|
||
import json
|
||
import math
|
||
|
||
average = sum(data) / len(data)
|
||
return {'result': math.ceil(average)}
|
||
```
|
||
</Tab>
|
||
|
||
<Tab title="JavaScript">
|
||
JavaScript 提供标准内置对象和方法。适合 JSON 操作和字符串操作。
|
||
|
||
```javascript
|
||
function main(data) {
|
||
const processed = data.map(item => item.toUpperCase());
|
||
return { result: processed };
|
||
}
|
||
```
|
||
</Tab>
|
||
</Tabs>
|
||
|
||
## 错误处理和重试
|
||
|
||
为失败的代码执行配置自动重试行为,并定义代码遇到错误时的回退策略。
|
||
|
||
<Frame caption="错误处理配置选项">
|
||
<img src="https://assets-docs.dify.ai/2024/12/58f392734ce44b22cd8c160faf28cd14.png" alt="Code Error handling" />
|
||
</Frame>
|
||
|
||
**重试设置**允许最多 10 次自动重试,并可配置间隔(最大 5000ms)。启用此功能来处理临时处理问题。
|
||
|
||
**错误处理**让你在代码执行失败时定义回退路径,使你的工作流即使在代码遇到问题时也能继续运行。
|
||
|
||
<Frame caption="重试配置界面">
|
||
<img src="https://assets-docs.dify.ai/2024/12/9fdd5525a91dc925b79b89272893becf.png" alt="Retry settings" />
|
||
</Frame>
|
||
|
||
## 输出验证和限制
|
||
|
||
代码输出会自动验证并有严格限制:
|
||
- **字符串**:最大长度 80,000 个字符,空字节会被移除
|
||
- **数字**:范围从 -999999999 到 999999999,浮点数限制为 10 位小数
|
||
- **对象/数组**:最大深度 5 级,以防止复杂的嵌套结构
|
||
|
||
这些限制确保工作流的性能并防止内存问题。
|
||
|
||
## 安全考虑
|
||
|
||
代码在严格的沙箱中执行,阻止文件系统访问、网络请求和系统命令。这在提供编程灵活性的同时保持安全性。
|
||
|
||
出于安全原因,某些操作会被自动阻止。避免尝试访问系统文件或执行可能危险的操作:
|
||
|
||
<Frame caption="Cloudflare WAF 安全过滤">
|
||
<img src="https://assets-docs.dify.ai/2024/12/ad4dc065c4c567c150ab7fa7bfd123a3.png" alt="Cloudflare WAF blocking" />
|
||
</Frame>
|
||
|
||
如果你的代码无法保存,请检查浏览器的网络选项卡 - 安全过滤器可能正在阻止潜在危险的操作。
|
||
|
||
## 依赖支持
|
||
|
||
代码节点支持 Python 和 JavaScript 的外部依赖:
|
||
|
||
```python
|
||
# Python: Import numpy, pandas, requests, etc.
|
||
import numpy as np
|
||
import pandas as pd
|
||
|
||
def main(data: list) -> dict:
|
||
df = pd.DataFrame(data)
|
||
return {'mean': float(np.mean(df['values']))}
|
||
```
|
||
|
||
```javascript
|
||
// JavaScript: Import lodash, moment, etc.
|
||
const _ = require('lodash');
|
||
|
||
function main(data) {
|
||
return { unique: _.uniq(data) };
|
||
}
|
||
```
|
||
|
||
依赖已预装在沙箱环境中。请检查你的 Dify 安装中的可用包列表。
|
||
|
||
## 自托管设启动沙箱服务以进行安全的代码执行:
|
||
|
||
```bash
|
||
docker-compose -f docker-compose.middleware.yaml up -d
|
||
```
|
||
|
||
沙箱服务需要 Docker,并将代码执行与你的主系统隔离以确保安全。
|
||
|
||
## 常见用例
|
||
|
||
**数据解析** - 从 API 或其他节点返回的复杂 JSON 结构中提取特定值。
|
||
|
||
**数学运算** - 执行预设节点无法处理的计算、统计分析或数据聚合。
|
||
|
||
**数据转换** - 转换数据格式、组合多个数据源或重构信息以供下游处理。
|
||
|
||
**条件逻辑** - 实现超越简单 if-else 条件的复杂决策逻辑。
|