Add internationalization support for help view and implement help rendering logic

- Introduced `help-i18n.js` for managing translations in Chinese and English for the help view.
- Created `help.js` to render the help view, including command categories, workflow diagrams, and CodexLens quick-start.
- Implemented search functionality with debounce for command filtering.
- Added workflow diagram rendering with Cytoscape.js integration.
- Developed tests for write-file verification, ensuring proper handling of small and large JSON files.
This commit is contained in:
catlog22
2025-12-16 22:24:29 +08:00
parent b702791c2c
commit 154a9283b5
21 changed files with 2003 additions and 303 deletions

View File

@@ -10,7 +10,7 @@
import { z } from 'zod';
import type { ToolSchema, ToolResult } from '../types/tool.js';
import { writeFileSync, readFileSync, existsSync, mkdirSync, renameSync } from 'fs';
import { writeFileSync, readFileSync, existsSync, mkdirSync, renameSync, statSync } from 'fs';
import { resolve, isAbsolute, dirname, basename } from 'path';
// Define Zod schema for validation
@@ -66,6 +66,39 @@ function createBackup(filePath: string): string | null {
}
}
/**
* Verify file write operation completed successfully
* @param filePath - Path to written file
* @param expectedBytes - Expected file size in bytes
* @param encoding - File encoding used
* @returns Error message if verification fails, null if successful
*/
function verifyFileWrite(filePath: string, expectedBytes: number, encoding: BufferEncoding): string | null {
// Check 1: File exists
if (!existsSync(filePath)) {
return `File verification failed: file does not exist at ${filePath}`;
}
try {
// Check 2: File size matches expected bytes
const stats = statSync(filePath);
if (stats.size !== expectedBytes) {
return `File verification failed: size mismatch (expected ${expectedBytes}B, actual ${stats.size}B)`;
}
// Check 3: File is readable (for long JSON files)
const readContent = readFileSync(filePath, { encoding });
const actualBytes = Buffer.byteLength(readContent, encoding);
if (actualBytes !== expectedBytes) {
return `File verification failed: content size mismatch after read (expected ${expectedBytes}B, read ${actualBytes}B)`;
}
return null; // Verification passed
} catch (error) {
return `File verification failed: ${(error as Error).message}`;
}
}
// Tool schema for MCP
export const schema: ToolSchema = {
name: 'write_file',
@@ -152,14 +185,23 @@ export async function handler(params: Record<string, unknown>): Promise<ToolResu
writeFileSync(resolvedPath, content, { encoding });
const bytes = Buffer.byteLength(content, encoding);
// Verify write operation completed successfully
const verificationError = verifyFileWrite(resolvedPath, bytes, encoding as BufferEncoding);
if (verificationError) {
return {
success: false,
error: verificationError,
};
}
// Build compact message
let message: string;
if (fileExists) {
message = backupPath
? `Overwrote (${bytes}B, backup: ${basename(backupPath)})`
: `Overwrote (${bytes}B)`;
? `Overwrote (${bytes}B, backup: ${basename(backupPath)}) - verified`
: `Overwrote (${bytes}B) - verified`;
} else {
message = `Created (${bytes}B)`;
message = `Created (${bytes}B) - verified`;
}
return {