From e2064419a07abb10076d727c0c28582576b13883 Mon Sep 17 00:00:00 2001 From: QRex-v0 <217984643+QRex-v0@users.noreply.github.com> Date: Thu, 17 Jul 2025 17:06:29 -0700 Subject: [PATCH 1/3] All changes: revert not needed ones later --- script-readme.md | 116 ++++++++++++++++++++++++++++++++++++++++++++ scripts/test.ts | 17 +++++-- src/agent/index.ts | 4 +- src/utils/action.ts | 3 +- 4 files changed, 131 insertions(+), 9 deletions(-) create mode 100644 script-readme.md diff --git a/script-readme.md b/script-readme.md new file mode 100644 index 0000000..e6f9bbc --- /dev/null +++ b/script-readme.md @@ -0,0 +1,116 @@ +# HyperAgent Script Generation Guide + +## Overview + +HyperAgent's `generateScript` feature allows you to automatically generate standalone TypeScript scripts from your agent's task execution. This is invaluable for debugging, reproducing issues, and creating reusable automation scripts. + +## Table of Contents +- [Enabling Script Generation](#enabling-script-generation) +- [How It Works](#how-it-works) +- [Using Generated Scripts](#using-generated-scripts) +- [Example](#example) + +## Enabling Script Generation + +To enable script generation, set `generateScript: true` when initializing HyperAgent: + +```typescript +import { HyperAgent } from "@hyperbrowser/agent"; + +const agent = new HyperAgent({ + debug: true, // Enable `debug` for comprehensive output + browserProvider: "Hyperbrowser", + generateScript: true, // Enable script generation + scriptPath: "scripts/script.ts", // Optional: specify the path; by default it goes to debug folder: debug/{taskId} + tokenLimit: 50000, + hyperbrowserConfig: { + sessionConfig: { + useProxy: true, + }, + }, +}); +``` + +## How It Works + +When `generateScript` is enabled: + +1. **Task Execution**: HyperAgent executes your task normally +2. **Action Recording**: Each action performed by the agent successfully is recorded +3. **Code Generation**: After task completion, HyperAgent generates TypeScript code that reproduces the exact sequence of actions +4. **Script Output**: The generated script is saved to the debug directory or the ${scriptPath} if specified + +## Using Generated Scripts + +### Running a Generated Script + +**Execution**: +```bash +# Ensure your .env file contains necessary API keys +npx ts-node debug/{taskId}/script.ts +``` + +### **Enable Debug Mode** +Strongly suggest use `debug: true` with `generateScript: true` for comprehensive output: +```typescript +const agent = new HyperAgent({ + debug: true, + generateScript: true, + // ... +}); +``` + +## **Example** +Here is an example to summarize "AI agents" related papers (top 3). +Once the job finished, you should get the generated script `debug/{taskId}/script.ts`, then you can run the following command line to reproduce the result: `npx ts-node debug/{taskId}/script.ts` +```typescript +import { HyperAgent } from "../src/agent"; +import dotenv from "dotenv"; +import chalk from "chalk"; +import { AgentOutput, AgentStep} from "../src/types/agent/types"; + +dotenv.config(); + +const agent = new HyperAgent({ + debug: true, + browserProvider: "Hyperbrowser", + tokenLimit: 50000, + generateScript: true, + // scriptPath: "script.ts", + hyperbrowserConfig: { + sessionConfig: { + useProxy: true, + }, + }, +}); + +(async () => { + const result = await agent.executeTask( + `Go to arXiv.org and search for 'AI agents' in abstract. + Find the 3 most recent papers from the search results. + For each paper: + 1. Extract the following information: + - Paper title + - All authors' names + - Summarized abstract in 2-3 sentences + - Submission date + 2. Compile all extracted information in your final response.`, + { + debugOnAgentOutput: (agentOutput: AgentOutput) => { + console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT =====")); + console.dir(agentOutput, { depth: null, colors: true }); + console.log(chalk.cyan.bold("===============") + "\n"); + }, + onStep: (step: AgentStep) => { + console.log("\n" + chalk.cyan.bold(`===== STEP =====`)); + console.log(`Step: ${step.idx}`); + console.dir(step, { depth: null, colors: true }); + console.log(chalk.cyan.bold("===============") + "\n"); + }, + } + ); + await agent.closeAgent(); + console.log(chalk.green.bold("\nResult:")); + console.log(chalk.white(result.output)); +})(); +``` diff --git a/scripts/test.ts b/scripts/test.ts index 30dc180..a9199de 100644 --- a/scripts/test.ts +++ b/scripts/test.ts @@ -8,7 +8,9 @@ dotenv.config(); const agent = new HyperAgent({ debug: true, browserProvider: "Hyperbrowser", - tokenLimit: 50000, + // tokenLimit: 50000, + generateScript: true, + // scriptPath: "script.ts", hyperbrowserConfig: { sessionConfig: { useProxy: true, @@ -18,10 +20,15 @@ const agent = new HyperAgent({ (async () => { const result = await agent.executeTask( - `Go to https://hiveword.com/location-name-generator and use extract action to get the top two countries; - get the capitals of these two countries. - Then go to https://flights.google.com and find the cheapest flight from the first capital to the second capital, - departing on a month from today, and returning on 45 days from today.`, + `Go to arXiv.org and search for 'AI agents' in abstract. + Find the 3 most recent papers from the search results. + For each paper: + 1. Extract the following information: + - Paper title + - All authors' names + - Summarized abstract in 2-3 sentences + - Submission date + 2. Compile all extracted information in your final response.`, { debugOnAgentOutput: (agentOutput: AgentOutput) => { console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT =====")); diff --git a/src/agent/index.ts b/src/agent/index.ts index 4a29788..d00b2f2 100644 --- a/src/agent/index.ts +++ b/src/agent/index.ts @@ -57,8 +57,8 @@ export class HyperAgent { : LocalBrowserProvider; private browserProviderType: T; private actions: Array = [...DEFAULT_ACTIONS]; - private generateScript = false; - private scriptPath?: string; + public generateScript = false; + public scriptPath?: string; private config: HyperAgentConfig; public browser: Browser | null = null; diff --git a/src/utils/action.ts b/src/utils/action.ts index 75da0bf..a9f9140 100644 --- a/src/utils/action.ts +++ b/src/utils/action.ts @@ -2,7 +2,6 @@ import fs from "fs"; import prettier from "prettier"; import { HyperAgentConfig } from "@/types"; -import { ActionContext } from "@/types"; export function initActionScript( actionLogFile: string, @@ -32,7 +31,7 @@ export function initActionScript( agentConfigString = ""; } - fs.appendFileSync( + fs.writeFileSync( actionLogFile, ` /* From 1fb20f6f46253629fad155847b38f6b3690bb312 Mon Sep 17 00:00:00 2001 From: QRex-v0 <217984643+QRex-v0@users.noreply.github.com> Date: Thu, 17 Jul 2025 17:07:44 -0700 Subject: [PATCH 2/3] remove script-readme.md: not needed --- script-readme.md | 116 ----------------------------------------------- 1 file changed, 116 deletions(-) delete mode 100644 script-readme.md diff --git a/script-readme.md b/script-readme.md deleted file mode 100644 index e6f9bbc..0000000 --- a/script-readme.md +++ /dev/null @@ -1,116 +0,0 @@ -# HyperAgent Script Generation Guide - -## Overview - -HyperAgent's `generateScript` feature allows you to automatically generate standalone TypeScript scripts from your agent's task execution. This is invaluable for debugging, reproducing issues, and creating reusable automation scripts. - -## Table of Contents -- [Enabling Script Generation](#enabling-script-generation) -- [How It Works](#how-it-works) -- [Using Generated Scripts](#using-generated-scripts) -- [Example](#example) - -## Enabling Script Generation - -To enable script generation, set `generateScript: true` when initializing HyperAgent: - -```typescript -import { HyperAgent } from "@hyperbrowser/agent"; - -const agent = new HyperAgent({ - debug: true, // Enable `debug` for comprehensive output - browserProvider: "Hyperbrowser", - generateScript: true, // Enable script generation - scriptPath: "scripts/script.ts", // Optional: specify the path; by default it goes to debug folder: debug/{taskId} - tokenLimit: 50000, - hyperbrowserConfig: { - sessionConfig: { - useProxy: true, - }, - }, -}); -``` - -## How It Works - -When `generateScript` is enabled: - -1. **Task Execution**: HyperAgent executes your task normally -2. **Action Recording**: Each action performed by the agent successfully is recorded -3. **Code Generation**: After task completion, HyperAgent generates TypeScript code that reproduces the exact sequence of actions -4. **Script Output**: The generated script is saved to the debug directory or the ${scriptPath} if specified - -## Using Generated Scripts - -### Running a Generated Script - -**Execution**: -```bash -# Ensure your .env file contains necessary API keys -npx ts-node debug/{taskId}/script.ts -``` - -### **Enable Debug Mode** -Strongly suggest use `debug: true` with `generateScript: true` for comprehensive output: -```typescript -const agent = new HyperAgent({ - debug: true, - generateScript: true, - // ... -}); -``` - -## **Example** -Here is an example to summarize "AI agents" related papers (top 3). -Once the job finished, you should get the generated script `debug/{taskId}/script.ts`, then you can run the following command line to reproduce the result: `npx ts-node debug/{taskId}/script.ts` -```typescript -import { HyperAgent } from "../src/agent"; -import dotenv from "dotenv"; -import chalk from "chalk"; -import { AgentOutput, AgentStep} from "../src/types/agent/types"; - -dotenv.config(); - -const agent = new HyperAgent({ - debug: true, - browserProvider: "Hyperbrowser", - tokenLimit: 50000, - generateScript: true, - // scriptPath: "script.ts", - hyperbrowserConfig: { - sessionConfig: { - useProxy: true, - }, - }, -}); - -(async () => { - const result = await agent.executeTask( - `Go to arXiv.org and search for 'AI agents' in abstract. - Find the 3 most recent papers from the search results. - For each paper: - 1. Extract the following information: - - Paper title - - All authors' names - - Summarized abstract in 2-3 sentences - - Submission date - 2. Compile all extracted information in your final response.`, - { - debugOnAgentOutput: (agentOutput: AgentOutput) => { - console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT =====")); - console.dir(agentOutput, { depth: null, colors: true }); - console.log(chalk.cyan.bold("===============") + "\n"); - }, - onStep: (step: AgentStep) => { - console.log("\n" + chalk.cyan.bold(`===== STEP =====`)); - console.log(`Step: ${step.idx}`); - console.dir(step, { depth: null, colors: true }); - console.log(chalk.cyan.bold("===============") + "\n"); - }, - } - ); - await agent.closeAgent(); - console.log(chalk.green.bold("\nResult:")); - console.log(chalk.white(result.output)); -})(); -``` From 6d3240223e0d2471e266caaf8eb0dea220e4835f Mon Sep 17 00:00:00 2001 From: QRex-v0 <217984643+QRex-v0@users.noreply.github.com> Date: Thu, 17 Jul 2025 17:08:44 -0700 Subject: [PATCH 3/3] revert changes to scripts/test.ts --- scripts/test.ts | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/scripts/test.ts b/scripts/test.ts index a9199de..30dc180 100644 --- a/scripts/test.ts +++ b/scripts/test.ts @@ -8,9 +8,7 @@ dotenv.config(); const agent = new HyperAgent({ debug: true, browserProvider: "Hyperbrowser", - // tokenLimit: 50000, - generateScript: true, - // scriptPath: "script.ts", + tokenLimit: 50000, hyperbrowserConfig: { sessionConfig: { useProxy: true, @@ -20,15 +18,10 @@ const agent = new HyperAgent({ (async () => { const result = await agent.executeTask( - `Go to arXiv.org and search for 'AI agents' in abstract. - Find the 3 most recent papers from the search results. - For each paper: - 1. Extract the following information: - - Paper title - - All authors' names - - Summarized abstract in 2-3 sentences - - Submission date - 2. Compile all extracted information in your final response.`, + `Go to https://hiveword.com/location-name-generator and use extract action to get the top two countries; + get the capitals of these two countries. + Then go to https://flights.google.com and find the cheapest flight from the first capital to the second capital, + departing on a month from today, and returning on 45 days from today.`, { debugOnAgentOutput: (agentOutput: AgentOutput) => { console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));