跳到正文
Skillpedia
通用编程7.8k · #13059

large-file-write-via-temp-script

它能做什么

这项技能展示智能体如何保存包含棘手字符的大文件,例如反引号、模板字符串或表情符号。适合保存 TypeScript、TSX 或类似文件,而这些文件在内联传入时会失败的开发者。

适用场景

  • ✓当文件内容包含会破坏内联命令的反引号、${} 模板字符串或表情符号的时候。
  • ✓当内联写入尝试因不明错误失败,或使文件被悄悄清空的时候。
  • ✓当一个大型 TypeScript 或 TSX 文件必须原样保存的时候。

技能链接

https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/large-file-write-via-temp-script/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/large-file-write-via-temp-script/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请把这个 TSX 组件保存到我的项目中。它包含反引号和模板字符串,而内联写入一直出错。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体把一个小型 Python 辅助脚本写入临时文件。文件内容放在该脚本中的原始字符串里,因此特殊字符得以保留;然后智能体运行该辅助脚本,写入目标文件。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Large File Write via Temp Script

Problem

Writing large files — especially TypeScript, TSX, or any content containing Unicode emoji, backticks, ${} template literals, or multi-line heredoc conflicts — can silently fail or truncate content when passed inline.

Failure modes observed:

  • write_file returns [ERROR] unknown error on large payloads
  • node -e '...' with backtick template literals silently empties the target file
  • python3 -c '...' breaks on unescaped quotes or special characters
  • shell_agent may time out or fail when the inline content is too large

Solution: Write a Python Script to a Temp File First

Instead of passing file content inline, write a small Python helper script to /tmp/write_task.py (or similar), then execute it. The helper script holds the target file content as a raw Python string or via base64 decoding.


Approach A — Python Triple-Quoted String (preferred for readability)

Use write_file or a heredoc to create the helper, then run it:

# Step 1: Write the helper script to /tmp
cat > /tmp/write_task.py << 'PYEOF'
import pathlib

content = r"""
// Your TypeScript / file content goes here
// Backticks `like this`, ${template} literals, and emoji 🎉 are all safe
export const MyComponent = () => {
  const msg = `Hello, world!`;
  return <div>{msg}</div>;
};
"""

pathlib.Path("/path/to/target/file.ts").write_text(content.lstrip("\n"), encoding="utf-8")
print("Written successfully.")
PYEOF

# Step 2: Execute the helper
python3 /tmp/write_task.py

Why r"""? A raw triple-quoted string avoids all backslash escaping issues. The only content that cannot appear verbatim is """ itself — if needed, split the string or escape that sequence only.


Approach B — Base64-Encoded Content (most robust for binary/complex content)

When the content contains """ or other Python string edge cases:

# Step 1: Base64-encode your content (done once, outside the target system)
# python3 -c "import base64; print(base64.b64encode(open('file.ts').read().encode()).decode())"

# Step 2: Embed the base64 blob in the helper script
cat > /tmp/write_task.py << 'PYEOF'
import base64, pathlib

b64 = (
    "aW1wb3J0IFJlYWN0IGZyb20gJ3JlYWN0JzsKCmV4cG9ydCBjb25zdCBNeUNvbXBvbmVudCA9"
    "ICgpID0+IHsKICByZXR1cm4gPGRpdj5IZWxsbzwvZGl2PjsKfTsK"
    # ... more lines if needed
)

content = base64.b64decode(b64).decode("utf-8")
pathlib.Path("/path/to/target/file.ts").write_text(content, encoding="utf-8")
print(f"Written {len(content)} bytes successfully.")
PYEOF

python3 /tmp/write_task.py

Approach C — shell_agent Delegation (simplest for agents)

Ask shell_agent to perform the write using the temp-script pattern:

"Write the following TypeScript content to /src/components/MyPanel.ts. Do NOT use node -e or inline Python. Instead write a Python script to /tmp/write_panel.py using a triple-quoted raw string, then execute it."


Decision Guide

Situation Recommended Approach
Content has backticks / ${} template literals Approach A (raw r""")
Content has """ sequences Approach B (base64)
Content is binary or non-UTF-8 Approach B (base64)
Delegating to shell_agent Approach C with explicit instructions
Quick small file Direct write_file (no workaround needed)

Anti-Patterns to Avoid

# ❌ DANGEROUS — backtick template literals will break node -e
node -e "const fs = require('fs'); fs.writeFileSync('out.ts', \`${content}\`);"

# ❌ FRAGILE — breaks on unescaped quotes and special characters
python3 -c "open('out.ts','w').write('${content}')"

# ❌ UNRELIABLE for large payloads — may silently truncate
echo "${large_content}" > out.ts

Verification Step

Always verify the write succeeded:

# Check file exists and has non-zero size
python3 -c "
import pathlib
p = pathlib.Path('/path/to/target/file.ts')
assert p.exists() and p.stat().st_size > 0, f'Write failed: {p}'
print(f'OK — {p.stat().st_size} bytes written to {p}')
"

Notes

  • The temp script approach works for any file type: .ts, .tsx, .js, .json, .md, .py, etc.
  • Always lstrip("\n") the triple-quoted string to avoid a leading blank line.
  • For very large files (>50 KB), prefer base64 to avoid any risk of heredoc conflicts.
  • Clean up temp scripts after use: rm /tmp/write_task.py
常见问题

为什么内联写入会出错?

技能指出,反引号、模板字符串和引号在内联传入时可能被截断或损坏。

什么是原始字符串?

它是一种 Python 字符串,会按输入的原样保留反斜杠和特殊字符。

辅助脚本放在哪里?

技能使用 /tmp 等临时位置,也可以使用类似的临时路径。

详情

作者的原始描述

Reliable fallback technique for writing large or special-character-heavy file content when inline write_file or shell_agent calls fail with unknown errors — uses a Python helper script written to a temp file to avoid escaping and truncation pitfalls.

repo: HKUDS/AgentPilot

热度

GitHub 星标
7.8k
排名
#13059
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能