@wwp9298/canvas-agent 0.0.0-stage → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,116 @@
1
- # Temporary Holding Version
1
+ # Canvas Agent
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 网页对话通过本地服务运行 Codex,插件通过同一个 MCP 操作画布。工具定义、节点操作和生成流程继续使用网页现有实现。
4
+
5
+ 两种接入方式:
6
+
7
+ | 方式 | 需要浏览器 | 能新建画布 | 适用场景 |
8
+ | --- | --- | --- | --- |
9
+ | 本地桥接(默认) | 是,画布标签要保持打开 | 否 | 看着画布实时协作、需要网页内的 Agent 记忆与技能 |
10
+ | 远程模式(`--remote`) | 否 | 是 | 让 WorkBuddy / Codex 凭密钥直接建画布、改画布、跑生成 |
11
+
12
+ ## 一键授权(推荐)
13
+
14
+ ```sh
15
+ npx -y @wwp9298/canvas-agent@latest login --base-url https://ai.chinatop100.ru
16
+ ```
17
+
18
+ 命令会打开浏览器到站点的「Agent 授权」页:没登录先登录,登录后点「允许并连接」,密钥自动写进本机 `~/.infinite-canvas/agent-key.json`(按站点保存,文件权限 600),页面提示授权成功后回到终端即可。授权默认带「改写画布 + 触发生成」,有效期 90 天,可在网页上随时吊销。
19
+
20
+ 授权页要在**执行命令的同一台电脑**的浏览器里打开(回调只走 `127.0.0.1`)。纯服务器或 CI 环境可设 `CANVAS_AGENT_NO_OPEN=1`,命令只打印链接,不尝试拉起浏览器。
21
+
22
+ 之后配置 MCP 只给站点,密钥自动读取:
23
+
24
+ ```sh
25
+ npx -y @wwp9298/canvas-agent@latest mcp --remote --base-url https://ai.chinatop100.ru
26
+ ```
27
+
28
+ ## 远程模式(手动传密钥)
29
+
30
+ 也可以在网站「Agent 授权」页手动签发一把 `ica_` 开头的密钥(可勾选「改写画布」「触发生成」,可设有效期),然后:
31
+
32
+ ```sh
33
+ npx -y @wwp9298/canvas-agent@latest mcp --remote --base-url https://你的站点 --key ica_xxx
34
+ ```
35
+
36
+ 写进 MCP 客户端配置(WorkBuddy 为 `~/.workbuddy/mcp.json`):
37
+
38
+ ```json
39
+ {
40
+ "mcpServers": {
41
+ "infinite-canvas": {
42
+ "command": "npx",
43
+ "args": ["-y", "@wwp9298/canvas-agent@latest", "mcp", "--remote", "--base-url", "https://你的站点", "--key", "ica_xxx"]
44
+ }
45
+ }
46
+ }
47
+ ```
48
+
49
+ 也可以用环境变量 `CANVAS_AGENT_BASE_URL`、`CANVAS_AGENT_KEY` 代替命令行参数。
50
+
51
+ 远程模式(`login` + `mcp --remote`)从 0.3.0 起提供。本地改完还没发布时,可以直接跑仓库里的代码:
52
+
53
+ ```sh
54
+ node /path/to/infinite-canvas/canvas-agent/index.mjs login --base-url https://你的站点
55
+ node /path/to/infinite-canvas/canvas-agent/index.mjs mcp --remote --base-url https://你的站点
56
+ ```
57
+
58
+ 远程模式的能力:
59
+
60
+ - `create_canvas`:凭空新建画布,返回画布 ID 与 `/canvas/<id>` 链接;
61
+ - `list_canvases` / `get_canvas_summary` / `get_node` / `query_canvas_nodes`:读画布;
62
+ - `create_text_node` / `update_text_node` / `update_node` / `delete_node` / `create_connection` / `delete_connection` / `create_group` / `arrange_nodes`:写画布,走服务端 ops 增量合并;
63
+ - `generate_image` / `generate_video` / `generate_audio` + `get_media_task_status`:生成素材(需要密钥带「触发生成」权限,按密钥所属账号扣点)。
64
+
65
+ 写画布不需要浏览器,也不需要打开任何页面;如果同一个画布正在浏览器里打开,网页会通过增量通道实时看到 Agent 的改动。远程模式不继承网页内 Agent 的会话记忆与技能文件,只提供上述工具。
66
+
67
+ ## 启动
68
+
69
+ 安装 Node.js 22+ 后,在终端执行,npx 会从 npm 获取服务及依赖:
70
+
71
+ ```sh
72
+ npx -y @wwp9298/canvas-agent@latest
73
+ ```
74
+
75
+ 首次启动自动生成 Token,保存在当前用户的 `~/.infinite-canvas/codex-agent.json`;下次启动复用。终端显示 `Local URL` 和 `Connect token`,默认地址为 `http://127.0.0.1:3210`。网页与服务在同一台电脑运行,不需要克隆仓库或手工创建 `.env`;正确 Token 连接后自动记录网站来源。
76
+
77
+ Codex 尚未登录时执行 `npx -y @openai/codex@0.153.4 login` 并完成登录,已有登录态会直接复用。
78
+
79
+ ## 插件自动连接
80
+
81
+ 在 Codex 所在电脑执行:
82
+
83
+ ```sh
84
+ codex plugin marketplace add https://github.com/tigerowo/infinite-canvas.git
85
+ codex plugin add canvas-agent@infinite-canvas
86
+ ```
87
+
88
+ 安装后新建 Codex 对话,说“帮我打开并连接到 Infinite Canvas”。插件优先使用当前对话提供的画布地址或原画布标签;无法确定站点时再询问地址。
89
+
90
+ 插件市场定义在仓库根目录的 `.agents/plugins/marketplace.json`,指向仓库内的 `canvas-agent` 插件目录。市场名 `infinite-canvas` 仅为安装标识;插件由 GitHub 获取,MCP 和连接 Skill 通过 npx 使用 npm 上的服务,无需进入插件缓存安装依赖。
91
+
92
+ 插件会启动服务、自动带入连接信息,并通过 MCP 确认目标画布。已有画布优先复用原浏览器标签;标签不可控时使用 `npx -y @wwp9298/canvas-agent@latest open "<画布URL>" <浏览器> [浏览器参数...]`。浏览器支持 `chrome`、`edge`、`firefox`、`brave` 和 `default`,保持原浏览器及配置;画布地址沿用实际的 `/canvas/[id]` 路由。浏览器的本地网络权限提示由用户允许。
93
+
94
+ 移除插件执行 `codex plugin remove canvas-agent`。
95
+
96
+ ## 直接注册 MCP
97
+
98
+ 与插件安装二选一:
99
+
100
+ ```sh
101
+ codex mcp add infinite-canvas -- npx -y @wwp9298/canvas-agent@latest mcp
102
+ ```
103
+
104
+ 直接注册 MCP 不包含自动打开 Skill,需要先启动服务并连接画布。移除时执行 `codex mcp remove infinite-canvas`。
105
+
106
+ ## 本机配置与范围
107
+
108
+ `npx -y @wwp9298/canvas-agent@latest config` 输出 `{url, token, origins}`,首次尚未启动时会提示先启动服务。HTTP `GET /config` 只返回 `{url, hasToken}`;Token 和包含它的连接链接不要分享。
109
+
110
+ Agent 密钥存在 `~/.infinite-canvas/agent-key.json`,按站点 host 分条保存,格式为 `{version, sites: {"站点host": {baseUrl, key, prefix, name, createdAt}}}`。删除对应站点条目等同于退出登录;多站点可各存一份。
111
+
112
+ `CANVAS_AGENT_TOKEN`、`CANVAS_AGENT_PORT`、`CANVAS_AGENT_ORIGINS` 可选环境变量覆盖 Token、端口和预置来源;更改后重启服务。通常无需设置。
113
+
114
+ 外部 MCP 默认只操作唯一已连接的画布;多个画布连接时,通过 MCP 环境变量 `CANVAS_AGENT_CLIENT_ID` 明确目标。断开连接会结束对应 Codex 进程,已提交的生成任务仍由现有画布流程处理。
115
+
116
+ 默认权限为 `workspace-write` 与 `on-request`,工作目录为实际 Agent 包目录。服务不写 Codex 全局配置文件;其他全局 MCP 是否合并到当前会话尚未联调确认。
package/codex.mjs ADDED
@@ -0,0 +1,91 @@
1
+ import { spawn } from "node:child_process";
2
+ import { createRequire } from "node:module";
3
+ import { dirname, join } from "node:path";
4
+ import { createInterface } from "node:readline";
5
+
6
+ const require = createRequire(import.meta.url);
7
+ const binary = join(dirname(require.resolve("@openai/codex/package.json")), "bin/codex.js");
8
+
9
+ export class CodexClient {
10
+ pending = new Map();
11
+ approvals = new Set();
12
+ nextId = 0;
13
+ stopped = false;
14
+
15
+ constructor(emit) {
16
+ this.emit = emit;
17
+ this.child = spawn(process.execPath, [binary, "app-server"], {
18
+ stdio: ["pipe", "pipe", "pipe"],
19
+ windowsHide: true,
20
+ });
21
+ createInterface({ input: this.child.stdout }).on("line", (line) => {
22
+ try {
23
+ const message = JSON.parse(line);
24
+ if (message.method) {
25
+ if (message.id !== undefined) this.approvals.add(message.id);
26
+ if (message.method === "serverRequest/resolved") this.approvals.delete(message.params.requestId);
27
+ this.emit("rpc", message);
28
+ } else {
29
+ const pending = this.pending.get(message.id);
30
+ if (!pending) return;
31
+ clearTimeout(pending.timer);
32
+ this.pending.delete(message.id);
33
+ if (message.error) pending.reject(new Error(message.error.message));
34
+ else pending.resolve(message.result);
35
+ }
36
+ } catch (error) {
37
+ this.close(new Error("Codex 输出解析失败:" + error.message));
38
+ }
39
+ });
40
+ this.child.stderr.resume();
41
+ this.child.stdin.on("error", (error) => this.close(error));
42
+ this.child.on("error", (error) => this.close(error));
43
+ this.child.on("exit", (code) => this.close(new Error("Codex 已退出:" + code)));
44
+ this.ready = this.request("initialize", {
45
+ clientInfo: { name: "infinite-canvas", title: "Infinite Canvas", version: "0.1.0" },
46
+ capabilities: { experimentalApi: true, requestAttestation: false },
47
+ }).then(() => this.write({ method: "initialized" }));
48
+ }
49
+
50
+ write(message) {
51
+ if (this.stopped) throw new Error("Codex 已断开");
52
+ this.child.stdin.write(JSON.stringify(message) + "\n");
53
+ }
54
+
55
+ request(method, params) {
56
+ return new Promise((resolve, reject) => {
57
+ const id = ++this.nextId;
58
+ const timer = setTimeout(() => {
59
+ this.close(new Error("Codex 请求超时:" + method));
60
+ }, 90000);
61
+ this.pending.set(id, { resolve, reject, timer });
62
+ try { this.write({ id, method, params }); }
63
+ catch (error) {
64
+ clearTimeout(timer);
65
+ this.pending.delete(id);
66
+ reject(error);
67
+ }
68
+ });
69
+ }
70
+
71
+ reply(id, result, error) {
72
+ if (!this.approvals.delete(id)) return;
73
+ this.write(error ? { id, error } : { id, result });
74
+ }
75
+
76
+ close(error = new Error("画布连接已关闭")) {
77
+ if (this.stopped) return;
78
+ this.stopped = true;
79
+ for (const pending of this.pending.values()) {
80
+ clearTimeout(pending.timer);
81
+ pending.reject(error);
82
+ }
83
+ this.pending.clear();
84
+ this.approvals.clear();
85
+ if (process.platform === "win32" && this.child.pid && this.child.exitCode === null && this.child.signalCode === null) {
86
+ spawn("taskkill", ["/PID", String(this.child.pid), "/T", "/F"], { windowsHide: true, stdio: "ignore" })
87
+ .on("error", () => this.child.kill());
88
+ } else this.child.kill();
89
+ this.emit("rpc", { method: "bridge/disconnected", params: { message: error.message } });
90
+ }
91
+ }