Install
openclaw skills install utf8-encoder自动处理多平台中文编码问题的发布基础设施,集成防卡顿策略,支持Discord、GitHub等平台,遵循'防止勤务干扰'原则。
openclaw skills install utf8-encoder核心定位:不是手动调用的编码工具,而是自动运行的发布流程基础设施。遵循Gemini专业评审建议:作为"底层律令",防止"勤务干扰",整合验证机制。
解决的核心问题:PowerShell控制台使用GB2312编码显示UTF-8内容,导致控制台输出乱码,误导开发者认为发布失败。实际HTTP数据正确,但显示误导导致重复发布浪费API token。
Gemini评审核心洞察:
基础设施特性:
已验证的平台:
npm install -g utf8-encoder-tool
npm install utf8-encoder-tool
# 或
yarn add utf8-encoder-tool
git clone https://github.com/mrpulorx2025-source/utf8-encoder-skill
cd utf8-encoder-skill
npm install
// 导入基础设施类
const { UTF8Infrastructure } = require('utf8-encoder-tool');
const infrastructure = new UTF8Infrastructure();
// 自动集成中间件,无需手动处理编码
const middleware = infrastructure.integrateAsMiddleware();
// 智能检测是否需要编码处理
const check = infrastructure.shouldProcess("中文测试内容");
if (check.needsProcessing) {
console.debug('🔄 基础设施自动处理编码问题');
console.debug(`检测原因: ${check.reasons.join(', ')}`);
// 遵循"防止勤务干扰"原则:不汇报编码处理细节,直接推进主线任务
}
// 带重试的发送(整合三次尝试法则)
const result = await middleware.sendToDiscordWithRetry(
process.env.DISCORD_WEBHOOK,
'消息内容',
{ username: 'UTF8-Infrastructure' }
);
// 批量发布到多个平台
const platforms = [
{
type: 'discord',
url: process.env.DISCORD_WEBHOOK,
options: { username: 'UTF8-Bot' }
},
{
type: 'github',
token: process.env.GITHUB_TOKEN,
filename: 'content.md',
description: '基础设施发布',
isPublic: false
}
];
const results = await middleware.publishToMultiplePlatforms(platforms, '发布内容');
console.log(`发布结果: ${results.filter(r => r.success).length}/${results.length} 成功`);
integrateAsMiddleware()系统级集成,支持批量发布getInfrastructureStatus()实时监控基础设施状态const { UTF8Encoder } = require('utf8-encoder-tool');
const encoder = new UTF8Encoder();
const text = "中文测试 Chinese Test 🎯";
const utf8Text = encoder.ensureUTF8(text);
// 确保文本是有效的UTF-8编码
const byteLength = encoder.calculateUTF8ByteLength(text);
console.log(`字符数: ${text.length}, UTF-8字节数: ${byteLength}`);
// 用于设置HTTP请求的Content-Length头
const content = encoder.readFileUTF8('./chinese-content.md');
console.log(`文件内容: ${content.substring(0, 100)}...`);
// 自动验证中文字符,统计数量
const payload = encoder.createUTF8JSONPayload({
message: "中文内容",
timestamp: new Date().toISOString(),
author: "丞相"
}, true); // true表示美化输出
const result = await encoder.sendToDiscord(
'https://discord.com/api/webhooks/...',
'Discord消息:中文测试 🎯',
{
username: 'UTF8-Bot',
avatar_url: 'https://example.com/avatar.png'
}
);
console.log(result.success ? '✅ 发送成功' : '❌ 发送失败');
const result = await encoder.createGitHubGist(
'ghp_your_token_here',
'# GitHub Gist测试\n\n中文内容正常显示测试。',
'test.md',
'UTF-8编码测试Gist',
true // 公开
);
if (result.gistUrl) {
console.log(`Gist已创建: ${result.gistUrl}`);
}
const validation = encoder.validateNoGarbledChars(text);
if (validation.valid) {
console.log('✅ 文本无乱码字符');
} else {
console.log(`❌ 发现${validation.garbledCount}个乱码字符`);
console.log('乱码字符:', validation.garbledChars);
}
const UTF8Encoder = require('utf8-encoder-tool');
const encoder = new UTF8Encoder();
const fs = require('fs');
async function publishResearch() {
// 1. 读取调研内容
const researchContent = encoder.readFileUTF8('./research.md');
// 2. 验证内容
const validation = encoder.validateNoGarbledChars(researchContent);
if (!validation.valid) {
throw new Error(`调研内容包含乱码: ${validation.garbledChars.join(', ')}`);
}
// 3. 发送到Discord(拆分长消息)
const discordResult = await encoder.sendToDiscord(
process.env.DISCORD_WEBHOOK,
researchContent.substring(0, 1900) // Discord消息限制
);
// 4. 创建GitHub Gist
const gistResult = await encoder.createGitHubGist(
process.env.GITHUB_TOKEN,
researchContent,
'research.md',
'用户调研 - UTF-8测试',
true
);
// 5. 生成报告
const report = encoder.generateTestReport([discordResult, gistResult]);
fs.writeFileSync('./publish-report.md', report, 'utf8');
console.log('🎉 调研发布完成!');
}
publishResearch().catch(console.error);
# PowerShell脚本:通过Node.js确保UTF-8编码
$nodeScript = @'
const UTF8Encoder = require('utf8-encoder-tool');
const encoder = new UTF8Encoder();
const text = process.argv[2];
const utf8Text = encoder.ensureUTF8(text);
console.log(utf8Text);
'@
# 保存临时脚本
$nodeScript | Out-File -FilePath "temp-utf8.js" -Encoding UTF8
# 执行并获取结果
$inputText = "PowerShell中的中文测试"
$result = node temp-utf8.js "$inputText"
Write-Host "UTF-8编码结果: $result"
# 清理
Remove-Item "temp-utf8.js"
// Express.js中间件:确保请求体UTF-8编码
const UTF8Encoder = require('utf8-encoder-tool');
const encoder = new UTF8Encoder();
function utf8Middleware(req, res, next) {
// 确保请求体文本是UTF-8编码
if (req.body && typeof req.body.text === 'string') {
req.body.text = encoder.ensureUTF8(req.body.text);
// 验证无乱码
const validation = encoder.validateNoGarbledChars(req.body.text);
if (!validation.valid) {
return res.status(400).json({
error: '请求包含乱码字符',
garbledChars: validation.garbledChars
});
}
}
// 设置响应头
res.setHeader('Content-Type', 'application/json; charset=utf-8');
next();
}
// 使用中间件
app.use(express.json());
app.use(utf8Middleware);
'utf8'Buffer.byteLength(text, 'utf8')而非text.length// 关键:Buffer.byteLength计算UTF-8字节长度
const postData = JSON.stringify(payload);
const contentLength = Buffer.byteLength(postData, 'utf8');
// 关键:设置正确的Content-Type头
headers['Content-Type'] = 'application/json; charset=utf-8';
headers['Content-Length'] = contentLength;
A:控制台使用GB2312编码显示UTF-8内容。解决方案:不依赖控制台输出,通过web_fetch工具验证实际网页显示。
A:使用encoder.validateNoGarbledChars(text)检测乱码字符,或直接访问生成的Gist/Issue查看实际显示。
A:支持中文、日文、韩文、Emoji、特殊符号。使用正则表达式/[\u4e00-\u9fa5]/检测中文字符。
A:平台API可能不处理编码问题。本工具确保:
A:极小。主要开销是Buffer.byteLength计算,对于普通文本(<10KB)可忽略不计。
ensureUTF8、calculateUTF8ByteLength等核心函数UTF8Encoder类中添加新方法(如sendToReddit)async sendToReddit(token, subreddit, title, content, options = {}) {
const payload = {
title: this.ensureUTF8(title),
text: this.ensureUTF8(content),
sr: subreddit,
kind: 'self',
...options
};
// Reddit API特定的头和处理逻辑
// ...
}
MIT License - 详见LICENSE文件
核心教训:编码问题反复消耗token实属不该。本工具通过一次性测试+验证+发布流程,避免重复浪费,提高发布成功率。
丞相谨记:控制台显示 ≠ 实际数据,必须独立验证网页显示!