unicode-safe-file-writing
它能做什么
这个技能说明当文本中含有表情符号或特殊符号导致文件保存失败时的变通方法。适合 AI 智能体在保存此类文件时遇到困难的人。
适用场景
- ✓当保存文件因未知错误失败,且文本中含有表情符号或特殊符号时。
- ✓当你需要正确保存含有箭头或破折号等符号的文件时。
- ✓当你想用纯文本替代文件工具无法处理的符号时。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/unicode-safe-file-writing/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我保存这个文件,它含有表情符号和箭头,但一直保存失败。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体通过命令行写入文件,按原样传递文本。也可以先把符号替换为纯文本版本。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Unicode-Safe File Writing
Problem
The write_file tool may fail with 'unknown error' when the file content
contains multi-byte Unicode characters such as:
- Emoji (e.g.
🚀,✅,❌) - Special typographic symbols (e.g.
→,•,—,©) - Non-ASCII characters outside the Latin-1 range
This is a known encoding limitation of write_file.
Solution
Fall back to run_shell and write the file using a shell heredoc
(cat > file << 'EOF' ... EOF). The single-quoted delimiter 'EOF'
prevents the shell from interpreting any special characters inside the
block.
Step-by-Step Recovery
Step 1 — Detect the failure
If write_file returns an error such as:
Error: unknown error
and the content contains non-ASCII characters, assume a Unicode encoding issue and proceed to the heredoc fallback.
Step 2 — Strip or replace problematic characters (if needed)
Before embedding content in the heredoc, decide whether to:
- Keep the characters as-is (works if the shell/terminal is UTF-8 aware)
- Replace emoji/symbols with ASCII equivalents (safest, most portable)
- Remove them entirely
Common replacements:
| Original | Replacement |
|---|---|
✅ |
[OK] |
❌ |
[ERROR] |
🚀 |
[START] |
→ |
-> |
• |
- |
— |
-- |
Step 3 — Write the file via heredoc
Use run_shell with the following pattern:
cat > path/to/file.ext << 'EOF'
...file content here (with Unicode stripped/replaced if needed)...
EOF
Key details:
- Use
'EOF'(single-quoted) as the heredoc delimiter — this disables all shell variable expansion and special character interpretation inside the block. - Do not indent the closing
EOF(it must start at column 0). - For deeply nested content, choose a delimiter that cannot appear in the
content itself (e.g.
'HEREDOC_END','FILEEND').
Example
Failing call
write_file(
path="src/components/StatusPanel.ts",
content="// Status icons\nconst icons = { ok: '✅', err: '❌', run: '🚀' };\n"
)
# → Error: unknown error
Recovery via heredoc
cat > src/components/StatusPanel.ts << 'EOF'
// Status icons
const icons = { ok: '[OK]', err: '[ERROR]', run: '[START]' };
EOF
Or, if UTF-8 output is acceptable in the environment:
cat > src/components/StatusPanel.ts << 'EOF'
// Status icons
const icons = { ok: '✅', err: '❌', run: '🚀' };
EOF
Multi-File Scenario
When writing several files and one fails, write the others with write_file
as normal and apply the heredoc fallback only to the failing file(s):
# Write multiple files that contain Unicode
cat > src/utils/icons.ts << 'EOF'
export const ICONS = {
success: '[OK]',
failure: '[FAIL]',
pending: '[...]',
};
EOF
cat > src/utils/labels.ts << 'EOF'
export const LABELS = {
title: 'Dashboard',
subtitle: 'Real-time status',
};
EOF
Important Notes
- Always prefer
write_filefirst — only fall back to the heredoc approach whenwrite_filefails. - Verify after writing — use
run_shellwithcat path/to/fileorhead -5 path/to/fileto confirm the content was written correctly. - Directory creation —
write_fileauto-creates parent directories; the heredoc approach does not. Pre-create directories withmkdir -p path/to/dirif needed. - Large files — heredocs work well for files of any practical size; there is no meaningful shell limit.
- Escaping backslashes — inside a single-quoted heredoc, backslashes are treated literally (no escaping needed), which is usually desirable.
常见问题
表情符号会保留吗?
你可以保留它们,大多数终端都支持;也可以为了安全起见替换成纯文本。
失败的原因是什么?
技能指出,普通的文件写入工具可能无法处理多字节字符。
使用这个方法安全吗?
它只写入你指定的文件,是一个简单的保存步骤。
详情
作者的原始描述
How to reliably write files containing multi-byte Unicode (emoji, special symbols) when write_file fails with 'unknown error' by falling back to run_shell with a heredoc.
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #13087
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。