可以选择历史对话了
This commit is contained in:
parent
8e761b742c
commit
4bb8fde3ca
2 changed files with 71 additions and 57 deletions
21
src/index.ts
21
src/index.ts
|
|
@ -8,6 +8,8 @@ import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
|
import { createInterface } from 'node:readline/promises';
|
||||||
|
import { stdin as input, stdout as output } from 'node:process';
|
||||||
|
|
||||||
// Handle Ctrl+C gracefully
|
// Handle Ctrl+C gracefully
|
||||||
function handleExit() {
|
function handleExit() {
|
||||||
|
|
@ -382,15 +384,11 @@ async function runChat(queryParts: string[], options: any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main chat loop
|
// Main chat loop
|
||||||
|
const rl = createInterface({ input, output });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
const { userInput } = await inquirer.prompt([
|
const userInput = await rl.question(chalk.green('? ') + 'You > ');
|
||||||
{
|
|
||||||
type: 'input',
|
|
||||||
name: 'userInput',
|
|
||||||
message: 'You >'
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (userInput.toLowerCase() === 'exit' || userInput.toLowerCase() === 'quit') {
|
if (userInput.toLowerCase() === 'exit' || userInput.toLowerCase() === 'quit') {
|
||||||
console.log(chalk.cyan("Goodbye!"));
|
console.log(chalk.cyan("Goodbye!"));
|
||||||
|
|
@ -402,12 +400,9 @@ async function runChat(queryParts: string[], options: any) {
|
||||||
await agent.chat(userInput);
|
await agent.chat(userInput);
|
||||||
}
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
// Check for Inquirer interruption error (Ctrl+C often causes this)
|
console.error(chalk.red("Error in chat loop:"), err);
|
||||||
if (err.message && (err.message.includes('User force closed') || err.message.includes('Prompt was canceled'))) {
|
} finally {
|
||||||
console.log(chalk.cyan("\nGoodbye!"));
|
rl.close();
|
||||||
process.exit(0);
|
|
||||||
}
|
|
||||||
throw err; // Re-throw real errors to be caught by main().catch
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,50 +29,69 @@ export const ScreenshotTool: ToolModule = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
handler: async (args: any, config: any) => {
|
handler: async (args: any, config: any) => {
|
||||||
let browser;
|
let browser;
|
||||||
try {
|
const launchOptions: any = {
|
||||||
// Launch with specific args to improve font rendering
|
headless: true,
|
||||||
browser = await chromium.launch({
|
args: ['--font-render-hinting=none']
|
||||||
headless: true,
|
};
|
||||||
args: ['--font-render-hinting=none']
|
|
||||||
});
|
try {
|
||||||
} catch (error: any) {
|
// Try to launch system Chrome first as it usually has better font support
|
||||||
if (error.message.includes("Executable doesn't exist")) {
|
browser = await chromium.launch({ ...launchOptions, channel: 'chrome' });
|
||||||
return "Error: Playwright browsers are not installed. Please run `npx playwright install chromium` to enable this feature.";
|
} 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({
|
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',
|
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 },
|
viewport: { width: 1280, height: 720 },
|
||||||
locale: 'zh-CN', // Set locale to Chinese to help with font selection and site localization
|
locale: 'zh-CN', // Set locale to Chinese
|
||||||
deviceScaleFactor: 2 // High DPI for better text clarity
|
deviceScaleFactor: 2 // High DPI
|
||||||
});
|
|
||||||
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 page = await context.newPage();
|
||||||
|
|
||||||
return `Successfully captured screenshot of ${args.url} and saved to ${args.outputPath}`;
|
try {
|
||||||
|
console.log(`Navigating to ${args.url} for screenshot...`);
|
||||||
|
await page.goto(args.url, { waitUntil: 'networkidle', timeout: 30000 });
|
||||||
|
|
||||||
} catch (error: any) {
|
// Inject CSS to force common Chinese fonts
|
||||||
return `Error taking screenshot: ${error.message}`;
|
await page.addStyleTag({
|
||||||
} finally {
|
content: `
|
||||||
await browser.close();
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
Loading…
Reference in a new issue