From 4bb8fde3cabce2591d40ef427a5f7ff74b14f70a Mon Sep 17 00:00:00 2001 From: tsingliu <410869548@qq.com> Date: Sat, 7 Feb 2026 21:24:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E9=80=89=E6=8B=A9=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E5=AF=B9=E8=AF=9D=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 21 +++----- src/tools/screenshot.ts | 107 +++++++++++++++++++++++----------------- 2 files changed, 71 insertions(+), 57 deletions(-) diff --git a/src/index.ts b/src/index.ts index 303216d..cef1c9b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,8 @@ import * as fs from 'fs'; import * as path from 'path'; import * as os from 'os'; import { fileURLToPath } from 'url'; +import { createInterface } from 'node:readline/promises'; +import { stdin as input, stdout as output } from 'node:process'; // Handle Ctrl+C gracefully function handleExit() { @@ -382,15 +384,11 @@ async function runChat(queryParts: string[], options: any) { } // Main chat loop + const rl = createInterface({ input, output }); + try { while (true) { - const { userInput } = await inquirer.prompt([ - { - type: 'input', - name: 'userInput', - message: 'You >' - } - ]); + const userInput = await rl.question(chalk.green('? ') + 'You > '); if (userInput.toLowerCase() === 'exit' || userInput.toLowerCase() === 'quit') { console.log(chalk.cyan("Goodbye!")); @@ -402,12 +400,9 @@ async function runChat(queryParts: string[], options: any) { await agent.chat(userInput); } } catch (err: any) { - // Check for Inquirer interruption error (Ctrl+C often causes this) - if (err.message && (err.message.includes('User force closed') || err.message.includes('Prompt was canceled'))) { - console.log(chalk.cyan("\nGoodbye!")); - process.exit(0); - } - throw err; // Re-throw real errors to be caught by main().catch + console.error(chalk.red("Error in chat loop:"), err); + } finally { + rl.close(); } } diff --git a/src/tools/screenshot.ts b/src/tools/screenshot.ts index c884d24..93efda8 100644 --- a/src/tools/screenshot.ts +++ b/src/tools/screenshot.ts @@ -29,50 +29,69 @@ export const ScreenshotTool: ToolModule = { } } }, - handler: async (args: any, config: any) => { - let browser; - try { - // Launch with specific args to improve font rendering - browser = await chromium.launch({ - headless: true, - args: ['--font-render-hinting=none'] - }); - } catch (error: any) { - if (error.message.includes("Executable doesn't exist")) { - return "Error: Playwright browsers are not installed. Please run `npx playwright install chromium` to enable this feature."; + handler: async (args: any, config: any) => { + let browser; + const launchOptions: any = { + headless: true, + args: ['--font-render-hinting=none'] + }; + + try { + // Try to launch system Chrome first as it usually has better font support + browser = await chromium.launch({ ...launchOptions, channel: 'chrome' }); + } catch (e) { + // Fallback to bundled Chromium + try { + browser = await chromium.launch(launchOptions); + } catch (error: any) { + if (error.message.includes("Executable doesn't exist")) { + return "Error: Playwright browsers are not installed. Please run `npx playwright install chromium` to enable this feature."; + } + return `Error launching browser: ${error.message}`; + } } - return `Error launching browser: ${error.message}`; - } - - const context = await browser.newContext({ - userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', - viewport: { width: 1280, height: 720 }, - locale: 'zh-CN', // Set locale to Chinese to help with font selection and site localization - deviceScaleFactor: 2 // High DPI for better text clarity - }); - const page = await context.newPage(); - - try { - console.log(`Navigating to ${args.url} for screenshot...`); - await page.goto(args.url, { waitUntil: 'networkidle', timeout: 30000 }); - - // Wait for fonts to be ready - await page.evaluate(() => document.fonts.ready); - - // Additional small delay for dynamic content - await page.waitForTimeout(1000); - - await page.screenshot({ - path: args.outputPath, - fullPage: args.fullPage || false + + const context = await browser.newContext({ + userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', + viewport: { width: 1280, height: 720 }, + locale: 'zh-CN', // Set locale to Chinese + deviceScaleFactor: 2 // High DPI }); - - return `Successfully captured screenshot of ${args.url} and saved to ${args.outputPath}`; - - } catch (error: any) { - return `Error taking screenshot: ${error.message}`; - } finally { - await browser.close(); + const page = await context.newPage(); + + try { + console.log(`Navigating to ${args.url} for screenshot...`); + await page.goto(args.url, { waitUntil: 'networkidle', timeout: 30000 }); + + // Inject CSS to force common Chinese fonts + await page.addStyleTag({ + content: ` + body, h1, h2, h3, h4, h5, h6, p, span, div, li, a, button, input, textarea { + font-family: "PingFang SC", "Heiti SC", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif !important; + } + ` + }); + + // Wait for fonts to be ready + await page.evaluate(() => document.fonts.ready); + + // Additional small delay for dynamic content and font rendering + await page.waitForTimeout(1000); + + await page.screenshot({ + path: args.outputPath, + fullPage: args.fullPage || false + }); + + return `Successfully captured screenshot of ${args.url} and saved to ${args.outputPath}`; + + } catch (error: any) { + return `Error taking screenshot: ${error.message}`; + } finally { + if (browser) { + await browser.close(); + } + } } - } -}; \ No newline at end of file + }; + \ No newline at end of file