Skip to main content
Glama
README.md
# bridge-ai — 让 Codex 直接操作 Illustrator 2022

一个 **零依赖** 的本地桥:Codex 通过 MCP 工具调用 Adobe Illustrator 2022 的 COM 自动化接口,完成绘图、置入图片、图像描摹(矢量化)、改色、裁剪、导出,还支持 **中文语音指令**(说一句话就画图/改图)。整个过程 **不需要打开 Illustrator 界面**,也不需要装插件、Python 或第三方 npm 包。

- 通信链路:Codex ⇄ `src/mcp-server.mjs`(MCP stdio)⇄ `src/worker.ps1`(常驻 PowerShell COM)⇄ Illustrator 2022
- 绘图/改图的全部逻辑都在 `src/jsx/lib.jsx`(ExtendScript,ES3)里,由 Node 组装后交给 COM 执行
- MCP 长驻进程会把脚本库缓存进 Illustrator 的 `$.global`(v0.3.1 起),首条命令之后只发送 ~150 字节的分派语句,不再重复解析 ~51 KB 脚本
- 坐标统一为 **左上角原点、y 向下**,和网页/Canvas 一致;内部自动转换成 Illustrator 的左下角坐标系

## 快速开始

### 1. 环境要求

| 项目 | 要求 |
| --- | --- |
| 系统 | Windows 10/11 |
| Illustrator | Adobe Illustrator 2022(已验证 26.5.0,COM 已注册) |
| Node.js | 18+(已验证 v26.13.1) |
| PowerShell | PowerShell 7 推荐;没有时桥会回退到 Windows PowerShell 5.1 |
| Codex | Codex CLI / 桌面版,支持 MCP |

> Illustrator 可以在后台运行;桥会在第一次调用时自动启动它。启动后窗口可能闪一下,桥会尽量最小化(只对“由桥启动”的实例)。

### 2. 注册到 Codex

把整个文件夹解压到任意位置(路径可包含空格、中文),在该文件夹执行:

```powershell
pwsh -ExecutionPolicy Bypass -File install.ps1
```

等价于先用 `codex mcp add` 注册当前目录的 MCP server:

```powershell
$server = Join-Path $PWD 'src\mcp-server.mjs'
codex mcp add illustrator -- (Get-Command node).Source $server
```

注册后 **重启 Codex 桌面应用**(或开一个新的 `codex` 会话)以加载工具。检查:

```powershell
codex mcp get illustrator
```

如果以后移动了文件夹,重新执行一次 `install.ps1` 更新路径;MCP 配置里存的是绝对路径。

首次调用如果 Codex 询问是否允许 MCP 工具,选择允许 `illustrator` 即可;也可以在 Codex 的 MCP 设置里把它设为自动允许。

### 3. 验证

```powershell
.\ai.cmd status
```

应返回 `ok: true`、`app: Adobe Illustrator`、`version: 26.5.0` 以及当前文档列表。

## 在 Codex 里怎么用

注册成功后,Codex 会看到 27 个 `ai_*` 工具。直接用自然语言下指令即可,例如:

```text
用 illustrator 工具画一张 1080x1080 的海报:
深蓝背景,圆角渐变卡片,白色标题「AI 造图」,副标题 Codex × Illustrator 2022,
几颗星星和圆点,导出到 output/codex-poster.png
```

```text
把 output/logo.png 用 illustrator 描摹成矢量,保留米白背景,
用 5 色调色板(#111827 #ff4d6d #22c55e #f59e0b #3b82f6),导出 output/logo-vector.png
```

```text
打开 output/photo.jpg,裁成 1600x900(从 x=100,y=80 开始),
右下角叠加一个半透明深色矩形和白色文字,导出 output/photo-cover.png
```

Codex 会自己组合 `ai_draw` / `ai_place_image` / `ai_trace_image` / `ai_recolor` / `ai_crop` / `ai_select` / `ai_modify` / `ai_transform` / `ai_export` 等工具;你不需要在 Illustrator 里点任何东西。

### 工具一览

| 工具 | 作用 |
| --- | --- |
| `ai_status` | 查看 Illustrator 是否在运行、版本、打开的文档 |
| `ai_new_document` | 新建 RGB/CMYK 文档,设置尺寸、单位、背景 |
| `ai_draw` | 按 JSON scene 绘图(矩形、圆、线、多边形、星形、路径、文字、渐变、图片) |
| `ai_place_image` | 置入 PNG/JPG,支持缩放、contain/cover、透明度、混合模式、嵌入 |
| `ai_trace_image` | 图像描摹并展开为可编辑路径,支持预设、`ignoreWhite`、调色板改色、preserve |
| `ai_recolor` | 对整篇或指定名称的对象做调色板映射(nearest / index) |
| `ai_crop` | 用画板裁剪(支持 `to:"content"`),文档尺寸随之变化 |
| `ai_export` | 导出 PNG/JPEG/SVG/PDF/AI,支持 1x/2x/指定像素宽 |
| `ai_open` / `ai_save` | 打开文件 / 另存为 `.ai`、`.pdf`、`.svg` 等 |
| `ai_run_jsx` | 逃生舱:执行任意 ExtendScript |
| `ai_close` / `ai_quit` | 关闭文档 / 退出 Illustrator(MCP 会话能记住是否由桥启动;CLI 跨命令退出请用 `ai quit --force`) |
| `ai_voice` | 语音/文本中文指令:说一句话(或给文本)自动画图、改色、移动、缩放、旋转、导出 |

### 编辑已有图稿(v0.2.0 新增)

以下 13 个工具用于打开 `.ai` / `.svg` / `.pdf` 等已有图稿后直接修改对象、图层与样式:

| 工具 | 作用 |
| --- | --- |
| `ai_select` | 按名称/序号/类型选中对象,批量选择或取消选择 |
| `ai_modify` | 改名称、填充/描边色、描边粗细、透明度、混合模式、位置、宽高、旋转 |
| `ai_delete` | 删除指定对象 |
| `ai_duplicate` | 复制对象并按 dx/dy 偏移 |
| `ai_transform` | 旋转、缩放、平移选中或指定对象 |
| `ai_layers` | 查看图层,锁定/隐藏/重命名图层 |
| `ai_group` / `ai_ungroup` | 把指定对象编组 / 解散编组 |
| `ai_effect` | 投影、描边化等常用效果 |
| `ai_pathfinder` | 路径查找器(unite / minus / intersect / exclude 等) |
| `ai_gradient` | 给已有对象上线性/径向渐变 |
| `ai_mask` | 按矩形裁切生成剪贴蒙版 |
| `ai_text_style` | 改文字大小、字体、行距、字距、颜色、对齐 |

所有工具都返回 JSON 文本;`inline:true` 时还会把导出的 PNG/JPEG 作为 MCP image content 返回,Codex 可以直接“看到”结果。

### 语音指令(v0.3.0 新增)

`ai_voice` 接受一段**中文语音**(WAV/MP3/MP4/M4A/WebM,或 base64)或**中文文本**,自动识别意图并执行画图/改图。识别用 Windows 自带 `System.Speech`(离线、零依赖),也支持 OpenAI Whisper 或本地 whisper CLI。

典型指令(语音或文字都行):

| 说 | 效果 |
| --- | --- |
| 「画一个红色的圆形」 | 新建 800×600 画布,画一个红色圆 |
| 「画一个蓝色矩形并写上你好世界」 | 画矩形 + 加上文字对象 |
| 「把圆形改成蓝色」 | 对名称含「圆形」的对象改色 |
| 「把标题字号改成 72」 | 改文字大小 |
| 「把矩形放大到 150%」 | 缩放对象 |
| 「把矩形向右移动 100」 | 平移对象 |
| 「把矩形旋转 45 度」 | 旋转对象 |
| 「导出一张图片」 | 导出当前文档为 PNG |

语音参数(MCP `ai_voice` 的 arguments):

- `audio`:音频文件路径;`text`:直接用文字(跳过识别);`audioBase64` + `format`
- `language`:默认 `zh-CN`
- `provider`:`auto`(默认,按环境自动选)| `windows`(Windows 语音识别)| `openai`(Whisper API)| `whisper`(本地 CLI)
- `execute`:`false` 时只识别不执行(调试用)
- `doc` / `out` / `scale`:作用文档、导出路径、导出倍率(触发导出类指令时生效)

环境变量(OpenAI / 本地 whisper 需要):

```text
AI_BRIDGE_ASR_PROVIDER     # auto | windows | openai | whisper
AI_BRIDGE_ASR_API_KEY      # openai 用(或 OPENAI_API_KEY)
AI_BRIDGE_ASR_BASE_URL     # 默认 https://api.openai.com/v1
AI_BRIDGE_FFMPEG           # ffmpeg 可执行文件路径(把非 WAV 转成 WAV 再识别)
```

> Windows 自带识别对短中文短语可能把「画」听成「挂/瓜」、「圆形」听成「缘起/原型」;桥内置了同音字纠错 + 备选结果回退,实测 TTS 合成的「画一个绿色的圆形」能正确识别并画出绿色圆。

## 命令行用法(不经过 Codex)

`ai.cmd` 是同一套能力的 CLI,适合调试、脚本化、批处理:

```powershell
.\ai.cmd status
.\ai.cmd new --width 1080 --height 1080 --background "#0b1f33"
.\ai.cmd draw examples\poster.scene.json --out output\poster.png --scale 1
.\ai.cmd place --image C:\Temp\photo.jpg --x 100 --y 80 --width 1000 --height 640 --fit contain
.\ai.cmd trace --image output\logo.png --preset 高色 --ignoreWhite --palette "#111827,#ff4d6d,#22c55e" --preserve "#f2efe6" --out output\logo-vector.png
.\ai.cmd recolor --palette "#111827,#ff4d6d,#22c55e" --preserve "#ffffff"
.\ai.cmd crop --x 100 --y 100 --width 800 --height 600
.\ai.cmd export --out output\result.png --scale 2
.\ai.cmd save --out output\result.ai
.\ai.cmd open C:\Temp\photo.jpg
.\ai.cmd jsx --file path\to\snippet.jsx
.\ai.cmd close --all
.\ai.cmd quit --force

# 编辑已有图稿(v0.2.0 新增)
.\ai.cmd select --name "标题"
.\ai.cmd modify --name "标题" --opacity 60 --fill "#ff4d6d"
.\ai.cmd transform --name "标题" --rotate 15 --scale 120
.\ai.cmd layers
.\ai.cmd group --name "标题" --groupName "标题组"
.\ai.cmd ungroup --name "标题组"
.\ai.cmd effect --name "标题" --type shadow --dx 4 --dy 4
.\ai.cmd select --names "a,b"
.\ai.cmd pathfinder --operation unite
.\ai.cmd gradient --name "背景" --type linear --angle 45 --stops "#ff4d6d,#3b82f6"
.\ai.cmd mask --x 100 --y 100 --width 800 --height 600
.\ai.cmd text-style --name "标题" --size 72 --font MicrosoftYaHei-Bold
.\ai.cmd duplicate --name "标题" --dx 20 --dy 0
.\ai.cmd delete --name "标题"

# 语音指令(v0.3.0 新增):--text 用文字,--audio 用音频文件
.\ai.cmd voice --text "画一个红色的圆形" --out output\voice-circle.png
.\ai.cmd voice --audio C:\Temp\命令.wav
.\ai.cmd voice --audio C:\Temp\命令.mp3 --provider openai   # 需要 AI_BRIDGE_ASR_API_KEY
```

运行 `.\ai.cmd help` 查看完整参数。

## 绘图 scene 格式

`ai_draw` 接受一个 JSON(也可以给 `.json` 文件路径):

```json
{
  "canvas": { "width": 1080, "height": 1080, "unit": "px", "background": "#0b1f33" },
  "elements": [
    { "type": "rect", "x": 60, "y": 60, "width": 960, "height": 960, "radius": 48,
      "fill": { "type": "linear", "angle": 45,
                "stops": [{ "offset": 0, "color": "#ff4d6d" }, { "offset": 1, "color": "#3b82f6" }] } },
    { "type": "ellipse", "x": 100, "y": 100, "width": 160, "height": 160, "fill": "#ffd166" },
    { "type": "star", "cx": 800, "cy": 220, "points": 5, "outer": 90, "inner": 36, "rotation": 0, "fill": "#ffffff" },
    { "type": "text", "text": "AI 造图", "x": 120, "y": 420, "size": 96,
      "font": "MicrosoftYaHei-Bold", "color": "#ffffff", "align": "left", "letterSpacing": 2 },
    { "type": "text", "text": "Codex × Illustrator 2022", "x": 120, "y": 560, "size": 40, "color": "#dbe4ff" },
    { "type": "line", "x1": 120, "y1": 620, "x2": 700, "y2": 620, "stroke": "#ffffff", "strokeWidth": 2, "opacity": 60 },
    { "type": "image", "path": "C:/Temp/photo.jpg", "x": 120, "y": 680, "width": 840, "height": 300,
      "fit": "cover", "opacity": 90, "blendMode": "multiply" }
  ]
}
```

坐标/单位规则:

- `x,y` 是元素 **左上角**;`star` 用 `cx,cy`(中心);`line` 用 `x1,y1,x2,y2`
- `canvas.unit` 可为 `px`(默认)、`pt`、`mm`、`cm`、`in`;文档尺寸按该单位建立
- 文字 `y` 是文字框顶部,`align` 为 `left|center|right`,会自动按测量宽度换算 left
- 颜色支持 `#rrggbb`、`[r,g,b]`、`{r,g,b}`、CMYK 数组;`null` 表示无填充
- 渐变:`{"type":"linear"|"radial","angle":45,"stops":[{"offset":0,"color":"#..."},...]}`
- 圆角矩形用 `radius`;多边形 `points` 是边数;星形 `points` 是角数
- 元素可选 `name`、`opacity`(0-100)、`blendMode`(如 `multiply`、`screen`、`overlay`)
- 图片在 scene 里可用 `path`,相对路径按当前工作目录解析

## 改图工作流

### 置入/嵌入图片

```powershell
.\ai.cmd place --image C:\Temp\photo.jpg --x 100 --y 80 --width 1000 --height 640 --fit contain
```

- `fit`:`stretch`(默认,铺满)、`contain`(完整显示)、`cover`(填满并裁切)
- `embed:true`(默认)会把图片嵌入文档;否则保留链接
- 同一张图被其他文档占用时,桥内部会改用“打开→复制到目标文档”的方式,避免 COM 的置入锁问题

### 描摹成矢量 + 改色

```powershell
.\ai.cmd trace --image output\logo.png --preset 高色 --ignoreWhite `
  --palette "#111827,#ff4d6d,#22c55e,#f59e0b,#3b82f6,#ffffff" `
  --preserve "#f2efe6" --out output\logo-vector.png
```

- `--preset` 可用本地化名称:`高色`、`低色`、`黑白`、`灰度`、`轮廓`、`3 色`、`6 色`、`16 色`、`剪影`、`线稿图`、`技术绘图` 等
- `--ignoreWhite` 跳过白色背景(做 logo/图标时通常要开)
- `--palette` 对展开后的路径做最近色映射;`--preserve` 的颜色保持不变(默认容差 12,可用 `--preserveTolerance` 调)
- 描摹结果是一组可编辑的 `PathItem`,不是位图

### 裁剪 / 改画板

```powershell
.\ai.cmd crop --x 100 --y 100 --width 800 --height 600
.\ai.cmd crop --to content
```

`crop` 调整的是 1 号画板;文档 `width/height` 会跟随画板,导出时 `artBoardClipping` 生效。

### 导出

```powershell
.\ai.cmd export --out output\result.png --scale 2
.\ai.cmd export --out output\result.jpg --format jpeg --quality 90
.\ai.cmd export --out output\result.svg --format svg
.\ai.cmd export --out output\result.pdf --format pdf
.\ai.cmd save   --out output\result.ai
```

PNG 默认透明背景;需要白底时在 scene 里给 `canvas.background`。

### 选择 / 修改 / 变换 / 图层 / 编组 / 效果 / 路径查找器 / 渐变 / 蒙版 / 文字样式

先 `open` 已有图稿,再按名称或序号定位对象后修改。完整工作流示例:

```powershell
.\ai.cmd open C:\Temp\poster.ai
.\ai.cmd select --name "标题"
.\ai.cmd text-style --name "标题" --size 72 --fill "#111827"
.\ai.cmd modify --name "标题" --x 120 --y 90 --opacity 100
.\ai.cmd gradient --name "背景" --type linear --angle 45 --stops "#ff4d6d,#3b82f6"
.\ai.cmd transform --name "标题" --rotate 15 --scale 120
.\ai.cmd group --name "标题" --groupName "标题组"
.\ai.cmd export --out output\edited-poster.png --scale 2
```

- 选择支持 `--name`(单个对象名)、`--names "a,b"`(批量选择)、`--index`(同类型中的序号)、`--deselect`(清空选区),也可用 `--typename` 缩小匹配范围
- `modify` 的 `--fill "none"`(或 JSON `fillColor:null`)表示去掉填充;`--fill "#rrggbb"` 改纯色
- `pathfinder` 不传 `--items` 时作用于当前选区:先 `select --names "a,b"` 再 `pathfinder --operation unite`;`mask` 也作用于指定对象或当前操作对象
- 改完务必 `export` 自检,必要时 `save` 回写 `.ai`

## 已验证的结果

以下图片都是桥真实生成/改写的(`output/` 目录):

| 文件 | 说明 |
| --- | --- |
| `output/poster.png` | `ai_draw` 生成的海报:渐变卡片、圆角、星形、中文标题 |
| `output/mcp-smoke.png` | 通过 MCP 协议(initialize → tools/call)生成的 640×360 验证图 |
| `output/placed.png` | 置入图片 + contain 缩放 |
| `output/cover-demo.png` | 图片置入 + 半透明蒙版 + 文字叠加的合成示例 |
| `output/logo.png` → `output/logo-final.png` | 矢量 logo 原图 → 描摹 + 调色板改色(保留米白背景) |
| `output/cropped.png` | 画板裁剪后的导出结果 |
| `output/voice-asr.png` | 语音指令(TTS 合成的 WAV → 识别 → 画图)生成的绿色圆 |
| `output/poster.ai` | 另存为可编辑的 `.ai` 文件 |

![poster](output/poster.png)

![logo](output/logo.png) → ![logo vector recolored](output/logo-final.png)

实测环境:Illustrator 26.5.0、Node v26.13.1、PowerShell 7.6.9、Codex CLI 0.171.0-alpha.13.2。
另外用 `codex exec` 真实调用过 `illustrator/ai_status`,Codex 返回了 Illustrator 26.5.0 和打开的文档数,说明 MCP 注册链路可用。

## 稳定性与错误处理

- **COM 启动失败**:Illustrator 刚被强杀时可能返回 `0x8007000E`;worker 会重试(或下次调用自动恢复),无需手工处理
- **超时**:Node 端默认 180 秒(`AI_BRIDGE_TIMEOUT` 可改);超时会杀掉并重建 worker,不会卡死 Codex
- **不抢用户实例**:MCP 会话中,如果 Illustrator 在桥启动前已存在,`ai_quit` 只释放 COM 连接、不退出软件;`force:true` 才退出。CLI 每条命令是独立 worker,无法跨进程记住“是谁启动的”,退出请显式 `ai quit --force`(会先对所有文档 `DONOTSAVECHANGES` 关闭,再退出)。
- **退出顺序**:先对所有文档 `DONOTSAVECHANGES`,再 `Quit()`;未保存的测试稿不会弹保存框
- **文件占用**:置入图片时若文件已在别的文档中打开,桥会自动走复制路径,避免 “Unable to set placed item's file”
- **ExtendScript 限制**:ES3,无 `JSON` 对象、不能用 `const/let`;桥内置了序列化器,命令参数由 Node 注入
- **调试**:`AI_BRIDGE_DEBUG=1` 输出 worker 协议日志;`AI_BRIDGE_LOG=path` 写 worker 日志

## 限制

- 只支持 **Windows + Illustrator 2022**(其他版本 COM ProgID 相同的话可试,但未验证)
- 不提供 Photoshop 滤镜、AI 生成式填充、PDF 文字重排等能力;这些需要别的引擎
- 图像描摹的调色板是“最近色映射”,不会做语义分区;复杂照片建议用 `高保真度照片` 预设并少改色
- 文字使用系统已安装字体;跨机器打开 `.ai` 时字体可能替换
- `preserve` 按 RGB 距离匹配;描摹后的颜色可能有 1-3 级偏移,默认容差 12 已覆盖常见情况
- 白色背景优先用 `ignoreWhite` 去掉;如果希望白色图形(如白星星)保持白色,把 `#ffffff` 放进 `palette`,不要放进 `preserve`(描摹展开后白色背景路径可能位于顶层,preserve 它会把画面盖住)
- MCP 工具调用是串行的(同一时刻一个 COM 操作),大批量任务请拆成多次调用

## 文件结构

```text
bridge-ai/
  ai.cmd                     # CLI 入口(双击/命令行)
  CHANGELOG.md              # 版本变更记录
  LICENSE                   # MIT 许可证
  .gitignore
  package.json
  src/
    mcp-server.mjs           # MCP stdio server(Codex 入口)
    bridge.mjs               # Node ⇄ PowerShell worker 的 JSONL 客户端
    worker.ps1               # 常驻 COM worker(保持 Illustrator 连接)
    ops.mjs                  # 高层操作 + JSX 组装
    cli.mjs                  # 命令行
    voice.mjs                # 中文语音/文字指令解析与执行
    asr.mjs                  # 语音识别(Windows / OpenAI / whisper)
    asr-windows.ps1          # Windows System.Speech 识别子进程
    jsx/lib.jsx              # 全部绘图/改图 ExtendScript
  examples/                  # scene 示例
  output/                    # 生成的图片/文档
  scripts/
    install-codex.ps1        # 注册 MCP 到 Codex
    package.ps1              # 生成带根目录的发布 zip + SHA256
    test-mcp.mjs             # MCP 冒烟测试
```

## 打包发布

在仓库根目录执行,会生成带 `bridge-ai/` 根目录的 zip、SHA256,并自动排除 `output/`、`dist/` 和测试产物:

```powershell
pwsh -NoProfile -File scripts\package.ps1
```

输出位于 `dist/bridge-ai-v<version>.zip`。把 zip 解压到任意目录后,进入解压出的 `bridge-ai/` 运行 `install.ps1` 即可;移动目录后需要重新运行一次。

## 安全说明

桥只在本机通过 stdio + COM 工作,不监听端口、不联网。`ai_run_jsx` 能执行任意 ExtendScript(包括读写本机文件),只应在你信任的 Codex 会话中使用;不需要时可以不给 Codex 该工具或在提示词里禁止。