agent-bridge-feishu 1.0.0 → 1.0.1
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 +3 -3
- package/dist/src-ts/plugins/feishu/index.js +8 -4
- package/dist/src-ts/plugins/feishu/public/app.js +26 -12
- package/dist/src-ts/plugins/feishu/public/index.html +1 -1
- package/dist/src-ts/plugins/feishu/public/style.css +3 -0
- package/package.json +2 -1
- package/plugin.json +1 -1
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# 飞书通道插件
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
在管理页面打开飞书通道的设置页,点击「创建飞书机器人」。页面会显示二维码和创建链接,授权完成后自动保存凭据并连接。随后在「关联 Agent」表单选择本地 Agent。
|
|
4
4
|
|
|
5
|
-
1. `POST /api/plugins/feishu/setup/begin`,响应中的 `url`
|
|
5
|
+
1. `POST /api/plugins/feishu/setup/begin`,响应中的 `url`(兼容字段 `qr_url`)即飞书授权 URL,`qr_image` 为 PNG 二维码 data URL。
|
|
6
6
|
2. 用户打开 URL 完成授权。
|
|
7
|
-
3.
|
|
7
|
+
3. 按 `interval` 秒间隔轮询 `POST /api/plugins/feishu/setup/poll`,提交第一步返回的 `device_code`;完成后插件会保存 `app_id/app_secret`。凭据不会出现在响应中。
|
|
8
8
|
4. 重载飞书插件,插件通过长连接接收文本消息,并将消息转发到已启动的本地 Agent 插件。
|
|
9
9
|
|
|
10
10
|
也可以直接配置飞书开放平台应用的 `app_id` 和 `app_secret`。通过配置中的 `agent_plugin` 或管理界面的绑定功能指定目标 Agent。
|
|
@@ -3,8 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.FeishuPlugin = void 0;
|
|
4
4
|
const promises_1 = require("node:fs/promises");
|
|
5
5
|
const node_path_1 = require("node:path");
|
|
6
|
+
const qrcode_1 = require("qrcode");
|
|
6
7
|
const client_1 = require("./client");
|
|
7
|
-
const manifest = { id: "feishu", version: "1.0.
|
|
8
|
+
const manifest = { id: "feishu", version: "1.0.1", type: "channel", displayName: "飞书通道", entry: "dist/src-ts/plugins/feishu/index.js", apiVersion: "1", process: "in-process", capabilities: ["channel", "channel.bot.setup", "channel.receive", "channel.send", "health"], configSchema: "config.schema.json", managementPage: "/abweb/channels/feishu" };
|
|
8
9
|
class FeishuPlugin {
|
|
9
10
|
options;
|
|
10
11
|
manifest = manifest;
|
|
@@ -166,7 +167,10 @@ class FeishuPlugin {
|
|
|
166
167
|
throw new Error(result.error_description ? `${result.error}: ${result.error_description}` : result.error);
|
|
167
168
|
if (typeof result.device_code !== "string" || typeof result.verification_uri_complete !== "string")
|
|
168
169
|
throw new Error("飞书授权响应缺少设备码或授权 URL");
|
|
169
|
-
|
|
170
|
+
const url = new URL(result.verification_uri_complete);
|
|
171
|
+
if (url.protocol !== "https:")
|
|
172
|
+
throw new Error("飞书授权 URL 必须使用 HTTPS");
|
|
173
|
+
this.json(response, 200, { device_code: result.device_code, url: url.href, qr_url: url.href, qr_image: await (0, qrcode_1.toDataURL)(url.href, { width: 256, margin: 4 }), interval: Number(result.interval ?? 5), expires_in: Number(result.expire_in ?? 600) });
|
|
170
174
|
}
|
|
171
175
|
catch (error) {
|
|
172
176
|
this.json(response, 502, { error: error instanceof Error ? error.message : String(error) });
|
|
@@ -188,8 +192,8 @@ class FeishuPlugin {
|
|
|
188
192
|
return this.json(response, 200, { status: "completed", app_id: appId, owner_open_id: user?.open_id });
|
|
189
193
|
}
|
|
190
194
|
const errorCode = typeof result.error === "string" ? result.error : "";
|
|
191
|
-
const status =
|
|
192
|
-
return this.json(response, 200, { status, ...(status === "error" ? { error: result.error_description || errorCode || "飞书授权响应无效" } : {}) });
|
|
195
|
+
const status = ["authorization_pending", "slow_down"].includes(errorCode) ? "pending" : errorCode === "access_denied" ? "denied" : errorCode === "expired_token" ? "expired" : "error";
|
|
196
|
+
return this.json(response, 200, { status, ...(status === "error" ? { error: result.error_description || errorCode || "飞书授权响应无效" } : {}), ...(errorCode === "slow_down" ? { slow_down: true } : {}) });
|
|
193
197
|
}
|
|
194
198
|
catch (error) {
|
|
195
199
|
this.json(response, 502, { error: error instanceof Error ? error.message : String(error) });
|
|
@@ -3,10 +3,14 @@
|
|
|
3
3
|
const notice = find("#notice");
|
|
4
4
|
const authState = find("#authorization-state");
|
|
5
5
|
const authLink = find("#authorization-link");
|
|
6
|
+
const authDetails = find("#authorization-details");
|
|
7
|
+
const authQr = find("#authorization-qr");
|
|
8
|
+
const authUrl = find("#authorization-url");
|
|
6
9
|
const credentials = find("#credentials");
|
|
7
10
|
const binding = find("#binding");
|
|
8
11
|
const settings = find("#settings");
|
|
9
12
|
let pollTimer;
|
|
13
|
+
let setup = 0;
|
|
10
14
|
|
|
11
15
|
async function request(path, method = "GET", body) {
|
|
12
16
|
const token = localStorage.getItem("mab-login-token");
|
|
@@ -40,33 +44,43 @@
|
|
|
40
44
|
await request("/api/plugins/feishu/reload", "POST");
|
|
41
45
|
await load();
|
|
42
46
|
}
|
|
43
|
-
async function poll(deviceCode, interval, deadline) {
|
|
44
|
-
if (
|
|
47
|
+
async function poll(deviceCode, interval, deadline, currentSetup) {
|
|
48
|
+
if (currentSetup !== setup) return;
|
|
49
|
+
if (Date.now() >= deadline) { authState.textContent = "授权已过期,请重新开始"; find("#begin").disabled = false; return; }
|
|
45
50
|
try {
|
|
46
51
|
const result = await request("/api/plugins/feishu/setup/poll", "POST", { device_code: deviceCode });
|
|
47
|
-
if (
|
|
48
|
-
if (result.status === "
|
|
49
|
-
if (result.status === "
|
|
50
|
-
if (result.status === "
|
|
52
|
+
if (currentSetup !== setup) return;
|
|
53
|
+
if (result.status === "completed") { authState.textContent = "授权完成,正在连接"; await reconnect(); if (currentSetup === setup) { authState.textContent = "授权完成"; find("#begin").disabled = false; } return; }
|
|
54
|
+
if (result.status === "denied") { authState.textContent = "授权已拒绝"; find("#begin").disabled = false; return; }
|
|
55
|
+
if (result.status === "expired") { authState.textContent = "授权已过期,请重新开始"; find("#begin").disabled = false; return; }
|
|
56
|
+
if (result.status === "error") { authState.textContent = `授权失败:${result.error || "未知错误"}`; find("#begin").disabled = false; return; }
|
|
51
57
|
authState.textContent = "等待飞书授权";
|
|
52
|
-
|
|
53
|
-
|
|
58
|
+
const nextInterval = interval + (result.slow_down ? 5000 : 0);
|
|
59
|
+
pollTimer = setTimeout(() => void poll(deviceCode, nextInterval, deadline, currentSetup), nextInterval);
|
|
60
|
+
} catch (error) { if (currentSetup === setup) { authState.textContent = `授权检查失败:${String(error)}`; find("#begin").disabled = false; } }
|
|
54
61
|
}
|
|
55
62
|
find("#begin").onclick = async () => {
|
|
56
|
-
|
|
63
|
+
const currentSetup = ++setup;
|
|
64
|
+
find("#begin").disabled = true;
|
|
65
|
+
clearTimeout(pollTimer); authLink.hidden = true; authDetails.hidden = true; authState.textContent = "正在申请设备授权";
|
|
57
66
|
try {
|
|
58
67
|
const result = await request("/api/plugins/feishu/setup/begin", "POST");
|
|
68
|
+
if (currentSetup !== setup) return;
|
|
59
69
|
const url = new URL(result.url);
|
|
60
70
|
if (url.protocol !== "https:") throw new Error("飞书授权地址无效");
|
|
61
71
|
authLink.href = url.href; authLink.hidden = false;
|
|
72
|
+
authQr.src = result.qr_image;
|
|
73
|
+
authUrl.textContent = url.href;
|
|
74
|
+
authDetails.hidden = false;
|
|
62
75
|
authState.textContent = "等待飞书授权";
|
|
63
|
-
|
|
64
|
-
|
|
76
|
+
const interval = Math.max(1, Number(result.interval) || 5) * 1000;
|
|
77
|
+
pollTimer = setTimeout(() => void poll(result.device_code, interval, Date.now() + (Number(result.expires_in) || 600) * 1000, currentSetup), interval);
|
|
78
|
+
} catch (error) { if (currentSetup === setup) { authState.textContent = `启动授权失败:${String(error)}`; find("#begin").disabled = false; } }
|
|
65
79
|
};
|
|
66
80
|
credentials.onsubmit = (event) => { event.preventDefault(); void request("/api/plugins/feishu/bot", "POST", { app_id: credentials.elements.app_id.value.trim(), app_secret: credentials.elements.app_secret.value }).then(reconnect).then(() => show("凭据已保存,正在连接")).catch((error) => show(String(error), true)); };
|
|
67
81
|
binding.onsubmit = (event) => { event.preventDefault(); void request("/api/plugins/feishu/bind", "POST", { pluginId: binding.elements.pluginId.value }).then(load).then(() => show("Agent 已关联")).catch((error) => show(String(error), true)); };
|
|
68
82
|
settings.onsubmit = (event) => { event.preventDefault(); void request("/api/plugins/feishu/settings", "POST", { agent_plugin: settings.elements.agent_plugin.value, allow_from: settings.elements.allow_from.value, allow_chats: settings.elements.allow_chats.value }).then(load).then(() => show("配置已保存")).catch((error) => show(String(error), true)); };
|
|
69
83
|
find("#refresh").onclick = () => void load();
|
|
70
|
-
window.addEventListener("pagehide", () => clearTimeout(pollTimer));
|
|
84
|
+
window.addEventListener("pagehide", () => { setup++; clearTimeout(pollTimer); });
|
|
71
85
|
void load();
|
|
72
86
|
})();
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
<header><h1>飞书机器人</h1><button id="refresh" type="button">刷新</button></header>
|
|
7
7
|
<p id="notice" role="status"></p>
|
|
8
8
|
<dl><div><dt>连接状态</dt><dd id="connection">未配置</dd></div><div><dt>App ID</dt><dd id="app-id">未配置</dd></div><div><dt>关联 Agent</dt><dd id="bound-agent">未关联</dd></div></dl>
|
|
9
|
-
<section><h2
|
|
9
|
+
<section><h2>创建设备授权</h2><div class="actions"><button id="begin" type="button">创建飞书机器人</button><a id="authorization-link" target="_blank" rel="noopener noreferrer" hidden>打开飞书创建链接</a></div><div id="authorization-details" hidden><img id="authorization-qr" width="256" height="256" alt="飞书机器人创建授权二维码"><p id="authorization-url"></p></div><p id="authorization-state" role="status"></p></section>
|
|
10
10
|
<form id="credentials"><h2>手动配置凭据</h2><div class="fields"><label>App ID<input name="app_id" required autocomplete="off"></label><label>App Secret<input name="app_secret" type="password" required autocomplete="new-password"></label></div><button type="submit">保存并连接</button></form>
|
|
11
11
|
<form id="binding"><h2>关联 Agent</h2><div class="fields"><label>Agent<select name="pluginId" required></select></label></div><button type="submit">关联</button></form>
|
|
12
12
|
<form id="settings"><h2>通道配置</h2><div class="fields"><label>默认 Agent<select name="agent_plugin"></select></label><label>允许的 open_id<input name="allow_from"></label><label>允许的 chat_id<input name="allow_chats"></label></div><button type="submit">保存配置</button></form>
|
|
@@ -17,6 +17,9 @@ input, select, button { min-height: 34px; padding: 6px 9px; border: 1px solid #b
|
|
|
17
17
|
button { cursor: pointer; }
|
|
18
18
|
button:hover { background: #e5f2ea; }
|
|
19
19
|
.actions { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
|
|
20
|
+
#authorization-details { margin-top: 12px; }
|
|
21
|
+
#authorization-qr { display: block; width: min(256px, 100%); height: auto; aspect-ratio: 1; }
|
|
22
|
+
#authorization-url { overflow-wrap: anywhere; }
|
|
20
23
|
a { color: #176848; }
|
|
21
24
|
.error { color: #a83427; }
|
|
22
25
|
@media (max-width: 620px) { .fields, dl { grid-template-columns: 1fr; } }
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agent-bridge-feishu",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "agent-bridge 的飞书通道插件。",
|
|
5
5
|
"keywords": ["mu-agent-bridge-plugin", "mu-agent-bridge-channel"],
|
|
6
6
|
"main": "dist/src-ts/plugins/feishu/index.js",
|
|
7
7
|
"files": ["dist/", "plugin.json", "config.schema.json", "README.md"],
|
|
8
8
|
"agentBridge": { "plugin": "./plugin.json", "type": "channel" },
|
|
9
9
|
"engines": { "node": ">=22" },
|
|
10
|
+
"dependencies": { "qrcode": "^1.5.4" },
|
|
10
11
|
"publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" }
|
|
11
12
|
}
|