MagicExplorer:基于 Cangjie 生态的自主浏览器代理系统项目

基于CangjieMagic(Cangjie Agent DSL)开发的自然语言浏览器Agent系统

Branch2Tags0
FilesLast commitLast update
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago

MagicExplorer: Autonomous Browser Agent System

👉 中文版说明请点击这里

This project is a structured, multi-agent system designed to autonomously complete high-level user goals in the browser using a set of carefully restricted tools and a goal-driven reasoning loop.

The core agents operate by analyzing structured memory, taking rational steps toward goal completion, and utilizing both semantic and visual understanding of the page.


Installation & Usage Guide

Dependencies

This project requires:

  • Node.js
  • Playwright
  • Cangjie compiler
  • CangjieMagic

Option 1: Using Docker

  1. Install Docker.

  2. Change the .env file to reflect your chosen model API key
    Update the API_MODEL variable and set API_KEY_TYPE_USED to the correct API.
    Make sure the model supports Image requests.

    For reference on model and supported APIs , see the CangjieMagic Docs.

  3. Build the image from within the MagicExplorer folder you downloaded:

    docker build -t magicexplorer .
    
  4. Run the container:

    docker run --env-file .env -it magicexplorer
    

    (On Linux, add --add-host=host.docker.internal:host-gateway)


Option 2: Manual Set up

  1. Clone the Repository

    git clone https://gitcode.com/Cangjie-TPC/MagicExplorer.git
    mkdir tmp/ && cd tmp/
    
  2. Install Cangjie Compiler

  3. Install Cangjie Magic

    git clone https://gitcode.com/Cangjie-TPC/CangjieMagic.git -b dev
    cd CangjieMagic/
    echo Building CangjieMagic...
    cjpm build
    cd ..
    
  4. Install Node.js

    cd ../MagicExplorer
    curl -fsSL https://deb.nodesource.com/setup_22.x | sudo bash -
    sudo apt-get install -y nodejs
    sudo npm install -g npm@latest
    sudo npm install && npx playwright install --with-deps
    
  5. Configure Environment Variables

    Make sure that paths in the cjpm.toml are pointing to CangjieMagic on your machine.

    Edit the .env file inside MagicExplorer/:

    example manual setup .env file:

    export OPENAI_API_KEY=YOUR_API_KEY_HERE
    export API_MODEL=openai:gpt-4.1
    export API_KEY_TYPE_USED=OPENAI_API_KEY
    export HOST=localhost
    export PORT=9222
    

    Then run command to load the environmental variables.

    source .env
    

    ⚠️ Make sure your chosen model supports image requests.
    For more info, see CangjieMagic Docs.


IMPORTANT: Chrome Debug Mode

This tool requires a Chrome instance running in debugging mode so it can connect using CDP.

Start Chrome with these flags (command depends on your OS):

macOS / Linux

PathToChrome  --remote-debugging-port=9222   --no-first-run   --no-default-browser-check --user-data-dir=/tmp/MagicExplorer

Windows (PowerShell)

& "C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --no-first-run --no-default-browser-check --user-data-dir="C:\MagicExplorer"

Running the MagicExplorer

Once the setup is done, run:

cjpm run --name MagicExplorer

You will be prompted with your goal you wish for the agent to run.

  • For Docker - Run within the Docker shell
  • For Manual Installation - Run from within the MagicExplorer directory

Changing Agent Information

Currently, all the agent's information is stored within the files folder of the project.
We provided an example email, password, and other necessary details used during testing of various user goals.

To personalize this, you should replace the example information with your own required details that you want the agent to have access to.

  • If you prefer not to store usernames or passwords, you can integrate a tool similar to the existing captcha tool.

    • This allows the user to complete a subtask (e.g., login or verification) and then return control back to the AI once permitted.
    • This is useful for signing into accounts or handling privacy-sensitive actions.
  • Alternatively, since the CDP instance saves cookies and history, you can manually sign into your accounts before running the program.

    • This method is often helpful because most anti-bot detection mechanisms are triggered on login pages.

Currently, information is stored in the file:
/files/data-about-user/personal-information.md


Note on Rebuilding

When using Docker, you must rebuild the image any time you change MagicExplorer code from outside the docker.


Agent Structure

EXECUTOR Agent

The primary decision-making agent. Each cycle, it:

  • Analyzes past memory to avoid repetition and extract insights
  • Uses structured tools (click, scrape, navigate, etc.) to act
  • Performs just one intelligent, justified step toward completing the final goal
  • Delegates subtasks to helper agents via extraAgent (For actions that require an extra tab)
  • Falls back to visual assistance (screenshot) when needed

SCREENSHOTTER Agent

A support agent used only when structural information (via scrape) is not enough. It:

  • Analyzes the visual screenshot of the page
  • Interprets the UI layout with index-based bounding boxes
  • Recommends the next exact index to click, or suggests scraping/scrolling if needed
  • Detects captchas, dead-ends, or missing actions

Tool Interface

Agents interact with the browser only via strictly limited tools:

  • navigate(url) - logged
  • scrape()
  • click(index, inputText, itemDescription) - logged
  • back() - logged
  • screenshot() - logged
  • scroll()
  • uploadFile(fileName) - logged
  • extraAgent(subGoal) - logged
  • passCaptcha()

Every tool annotated 'logged' above is logged in memory after every call. Every decision is explainable. No agent is allowed to hallucinate page structure or rely on hidden context.


Goal Resolution Logic

Each cycle follows a strict 4-step execution plan:

  1. Analyze Memory & Progress
  2. Scrape Page
  3. Decide Action
  4. Act

No loops, no guessing, and no goal completion without observable confirmation.


Project Architecture

At a system level, this project is built on a layered architecture combining a browser interface, agent logic, and coordination infrastructure.

Project Architecture

MCP Server

At the heart of the system is an MCP (Model Context Protocol) server, which mediates all interaction between the agents and the browser environment. It functions as:

  • MCP is the hub that receives tools from the browser architecture
  • A dispatcher for tool invocations (e.g., scrape, click, etc.)

The MCP server receives browser-side commands via a JavaScript class that exposes all browser tools as callable functions. These functions are then called and return values are sent back to the agent.

Cangjie Agent Engine

All agent logic is written in Cangjie.

In Cangjie, each agent is composed as a reactive, goal-sensitive program that:

  • Operates on declarative inputs (goals + memory)
  • Produces exactly one logical action per cycle
  • Utilises Cangjie Magic, a powerful agent infrastructure.

Through Cangjie Magics design, we gain:

  • Agent architectures
  • Ability to receive tools from the mcp servers and create our own cangjie 'wrapper' tools which allow us to log which tools have been called.

JavaScript Browser Interface

The browser itself is controlled using playwright, and all browser interactions are written using javascript.

This JavaScript layer is designed to be agent-agnostic — it simply provides a reactive, remote-control interface to the browser surface.


MagicExplorer: 自主浏览器代理系统

👆 Return to English Version Readme

MagicExplorer 是一个结构化的多智能体系统,旨在通过一套精细控制的工具和目标驱动的推理循环,在浏览器中自主完成复杂的用户任务。

核心智能体会基于结构化记忆进行分析,采取合理步骤达成目标,并结合页面的语义和视觉理解来执行操作。


安装与使用指导

依赖关系

本项目要求:

  • Node.js
  • Playwright
  • Cangjie Compiler
  • Cangjie Agent DSL - CangjieMagic

选项1:使用Docker

  1. 安装 Docker。

  2. 修改 .env 文件,配置所需的模型 API 信息:

    • 更新 API_MODEL 变量
    • 设置 API_KEY_TYPE_USED 为正确的 API 类型
    • 确认所选模型支持图像请求

    有关模型与支持 API 的参考,请查看 CangjieMagic 文档。

  3. 在下载的 MagicExplorer 文件夹中构建镜像:

    docker build -t magicexplorer .
    
  4. 运行容器:

    docker run --env-file .env -it magicexplorer
    

    (Linux用户需额外加上:--add-host=host.docker.internal:host-gateway)


选项2:手动安装

  1. 克隆代码仓

    git clone https://gitcode.com/Cangjie-TPC/MagicExplorer.git
    mkdir tmp/ && cd tmp/
    
  2. 安装仓颉编译器

  3. 安装CangjieMagic

    git clone https://gitcode.com/Cangjie-TPC/CangjieMagic.git -b dev
    cd CangjieMagic/
    echo Building CangjieMagic...
    cjpm build
    cd ..
    
  4. 安装Node.js

    cd ../MagicExplorer
    curl -fsSL https://deb.nodesource.com/setup_22.x | sudo bash -
    sudo apt-get install -y nodejs
    sudo npm install -g npm@latest
    sudo npm install && npx playwright install --with-deps
    
  5. 配置环境变量

    确保cjpm.toml中的路径指向你机器上的CangjieMagic。

    编辑.env文件内MagicExplorer/:

    示例手动设置.env文件:

    OPENAI_API_KEY=YOUR_API_TYPE
    API_MODEL=openai:gpt-4.1
    API_KEY_TYPE_USED=OPENAI_API_KEY
    HOST=localhost
    PORT=9222
    

    设置.env文件后,运行此命令以将所有KEY值对导出为环境变量。

    export $(grep -v '^#'.env | xargs)
    

    或者在当前环境中单独导出每个值。

    鈿狅笍确保您选择的型号支持图像请求。 有关详细信息,请参阅仓颉魔法医生 .


Chrome 调试模式

本工具需要连接处于调试模式下运行的 Chrome 实例(通过 CDP)。

使用以下命令启动 Chrome(依操作系统而定)

macOS / Linux

PathToChrome  --remote-debugging-port=9222   --no-first-run   --no-default-browser-check --user-data-dir=/tmp/MagicExplorer

Windows (PowerShell)

& "C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --no-first-run --no-default-browser-check --user-data-dir="C:\MagicExplorer"

运行 MagicExplorer

完成设置后,运行:

cjpm run --name MagicExplorer

系统会提示你输入希望执行的目标。

  • Docker 环境 - 在 Docker shell 内运行
  • 手动安装 - 在 MagicExplorer 目录内运行

修改Agent信息

目前,所有Agent的信息都存储在项目的 files 文件夹中。 我们提供了示例邮箱、密码,以及在测试各种用户目标时使用的其他必要信息。

要个性化设置,你需要将示例信息替换为你希望代理能够访问的实际信息。

  • 如果你不希望存储用户名或密码,可以集成类似现有验证码工具的方式。

    • 这样,用户可以手动完成某个子任务(例如登录或验证),然后在允许后将控制权交还给 AI。
    • 这在需要登录账号或处理隐私敏感操作时非常有用。
  • 或者,由于 CDP 实例会保存 cookies 和历史记录,你可以在运行程序前手动登录账号。

    • 这种方式通常更有效,因为大多数反机器人检测机制会在 登录页面 被触发。

目前,信息存储在以下文件中: /files/data-about-user/personal-information.md


关于重新构建的说明

在使用 Docker 时,如果你在 Docker 外部修改了 MagicExplorer 的代码,就必须重新构建镜像。


Agent结构

EXECUTOR Agent

核心决策Agent,每个周期会:

  • 分析历史记忆,避免重复操作并提取洞见
  • 调用结构化工具(如 click、scrape、navigate 等)执行动作
  • 采取合理步骤推动最终目标完成
  • 必要时将子任务委派给 extraAgent(用于多标签页操作)
  • 当结构化信息不足时,退回到视觉辅助(screenshot)

SCREENSHOTTER Agent

当 scrape 信息不足时启用:

  • 分析页面截图
  • 基于索引的边界框解释 UI 布局
  • 建议点击的元素索引,或提出 scrape/scroll 动作
  • 识别验证码、死链或缺失操作

工具接口

Agent只能通过严格限定的工具与浏览器交互:

  • navigate(url) - logged
  • scrape()
  • click(index, inputText, itemDescription) - logged
  • back() - logged
  • screenshot() - logged
  • scroll()
  • uploadFile(fileName) - logged
  • extraAgent(subGoal) - logged
  • passCaptcha()

所有带 “logged” 标记的工具调用,都会被记录在内存中,保证决策过程可解释。Agent不能凭空假设页面结构,也不能依赖隐藏上下文。


目标执行逻辑

每个循环遵循严格的四步流程:

  1. 分析记忆与进度
  2. 抓取页面内容 (scrape)
  3. 决定行动
  4. 执行操作

不进行无谓循环,不做盲目猜测。只有当目标得到可观察的确认时,任务才算完成。


系统架构

本项目基于分层架构,结合了浏览器接口、Agent逻辑与协调机制。

Project Architecture

MCP 服务器

系统核心是 MCP(模型上下文协议)服务器,负责Agent与浏览器环境之间的交互:

  • 接收并调度工具调用(如 scrape、click 等)
  • 通过 JavaScript 类暴露浏览器端命令
  • 执行工具调用,并将结果返回Agent

仓颉 Agent 引擎

所有Agent逻辑均由 仓颉 编写:

  • 基于声明式输入(目标 + 记忆)运行
  • 每周期只产生一个逻辑动作
  • 借助 CangjieMagic 提供的强大Agent编程能力

通过 CangjieMagic 的设计,我们可以:

  • 定义Agent架构
  • 从 MCP 服务器获取工具,并创建自定义“包装器”工具以记录调用

JavaScript 浏览器接口

浏览器交互由 Playwright 驱动,全部使用 JavaScript 编写。 该层与Agent逻辑解耦,仅提供一个响应式、远程可控的浏览器操作接口。

Introduction

基于CangjieMagic(Cangjie Agent DSL)开发的自然语言浏览器Agent系统

Customize your domain