c3db79c00d
- core: 工具描述从文件系统加载改为编译时内联生成 - core: 添加 WASM 加载器支持嵌入式 WASM 数据 - core: bash-parser 使用动态导入 web-tree-sitter - server: 添加静态文件托管支持 (--static 参数) - server: 新增 standalone 入口点 (嵌入 Web UI + WASM) - scripts: 添加 build-standalone.ts 构建脚本 - 更新 .gitignore 忽略生成文件
90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
#!/usr/bin/env bun
|
|
/**
|
|
* Generate Tool Descriptions
|
|
*
|
|
* 将 descriptions 目录下的 .txt 文件转换为 TypeScript 模块
|
|
* 用于 Bun 打包时内联工具描述
|
|
*/
|
|
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
const DESCRIPTIONS_DIR = path.join(__dirname, '../src/tools/descriptions');
|
|
const OUTPUT_FILE = path.join(__dirname, '../src/tools/descriptions.generated.ts');
|
|
|
|
interface DescriptionEntry {
|
|
category: string;
|
|
name: string;
|
|
content: string;
|
|
}
|
|
|
|
function collectDescriptions(): DescriptionEntry[] {
|
|
const entries: DescriptionEntry[] = [];
|
|
|
|
const categories = fs.readdirSync(DESCRIPTIONS_DIR);
|
|
for (const category of categories) {
|
|
const categoryPath = path.join(DESCRIPTIONS_DIR, category);
|
|
const stat = fs.statSync(categoryPath);
|
|
|
|
if (stat.isDirectory()) {
|
|
const files = fs.readdirSync(categoryPath);
|
|
for (const file of files) {
|
|
if (file.endsWith('.txt')) {
|
|
const name = file.replace('.txt', '');
|
|
const content = fs.readFileSync(path.join(categoryPath, file), 'utf-8').trim();
|
|
entries.push({ category, name, content });
|
|
}
|
|
}
|
|
} else if (category.endsWith('.txt')) {
|
|
const name = category.replace('.txt', '');
|
|
const content = fs.readFileSync(categoryPath, 'utf-8').trim();
|
|
entries.push({ category: '', name, content });
|
|
}
|
|
}
|
|
|
|
return entries;
|
|
}
|
|
|
|
function escapeString(str: string): string {
|
|
return str
|
|
.replace(/\\/g, '\\\\')
|
|
.replace(/`/g, '\\`')
|
|
.replace(/\$/g, '\\$');
|
|
}
|
|
|
|
function generateCode(entries: DescriptionEntry[]): string {
|
|
const lines: string[] = [
|
|
'// Auto-generated by scripts/generate-descriptions.ts',
|
|
'// Do not edit manually',
|
|
'',
|
|
'export const TOOL_DESCRIPTIONS: Record<string, string> = {',
|
|
];
|
|
|
|
for (const entry of entries) {
|
|
const escapedContent = escapeString(entry.content);
|
|
lines.push(` '${entry.name}': \`${escapedContent}\`,`);
|
|
lines.push('');
|
|
}
|
|
|
|
lines.push('};');
|
|
lines.push('');
|
|
|
|
return lines.join('\n');
|
|
}
|
|
|
|
function main() {
|
|
console.log('Collecting tool descriptions...');
|
|
const entries = collectDescriptions();
|
|
console.log(`Found ${entries.length} descriptions`);
|
|
|
|
console.log('Generating TypeScript code...');
|
|
const code = generateCode(entries);
|
|
|
|
console.log(`Writing to ${OUTPUT_FILE}...`);
|
|
fs.writeFileSync(OUTPUT_FILE, code, 'utf-8');
|
|
|
|
console.log('Done!');
|
|
}
|
|
|
|
main();
|