agent-bridge-qwenwork 1.0.8 → 1.0.10

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
@@ -10,6 +10,6 @@
10
10
 
11
11
  请求超时或明确返回“API不可用”时只结束本次请求,已经发送的任务不自动重发,避免重复执行。健康接口 `/api/plugins/qwenwork/health` 的 `lastHeartbeat` 表示最近真实对话验证通过时间,同时返回 `restarts` 和 `recovering`;日志可搜索“千问真实对话心跳通过”“千问心跳失败”“千问自动恢复成功”。自动恢复只在 CDP 断开且当前无任务时结束千问进程及其子进程;daemon 应在已登录的 Windows 用户桌面会话运行。
12
12
 
13
- `/new` 在已确认的空白新任务页可重复执行;有未发送草稿时会拒绝操作并保留内容。发送消息会点击桌面端“发送消息”按钮并确认输入框清空,避免仅按 Enter 后消息仍停留在输入框。真实通信失败后保持降级,普通 CDP 检查通过不会把状态改回就绪,须有真实回复或真实对话心跳通过。
13
+ `/new` 在已确认的空白新任务页可重复执行。远程消息和 `/new` 遇到输入框残留时,先将原文、页面地址和时间追加到插件数据目录的 `draft-recovery.jsonl`,再通过编辑器原生删除操作清理;备份失败、内容发生变化或正在生成回复时不清理。后台心跳只清理桥接自己的检测文本,遇到普通用户草稿则跳过。发送消息会点击桌面端“发送消息”按钮并确认输入框清空,避免仅按 Enter 后消息仍停留在输入框。真实通信失败后保持降级,普通 CDP 检查通过不会把状态改回就绪,须有真实回复或真实对话心跳通过。
14
14
 
15
15
  真实心跳会消耗模型额度,并在当前千问聊天会话留下检测消息;它通过与用户请求相同的发送和取回回复路径运行。云端故障、账号额度不足或登录过期仍可能需要修复服务或账号,验证失败期间不会报告健康。
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.recoverCdpDraft = recoverCdpDraft;
4
+ const promises_1 = require("node:fs/promises");
5
+ const node_os_1 = require("node:os");
6
+ const node_path_1 = require("node:path");
7
+ /** 空闲时先备份旧草稿,再通过编辑器原生删除事件清理,避免只修改 DOM。 */
8
+ async function recoverCdpDraft(evaluate, editorSelector, stopSelector, dataDir = (0, node_path_1.join)((0, node_os_1.tmpdir)(), "agent-bridge-draft-recovery")) {
9
+ const state = await evaluate(`(() => ({
10
+ text: document.querySelector(${JSON.stringify(editorSelector)})?.innerText ?? null,
11
+ generating: Boolean(document.querySelector(${JSON.stringify(stopSelector)})),
12
+ url: document.URL ?? ''
13
+ }))()`);
14
+ if (state.generating)
15
+ throw new Error("桌面端正在生成回复,不能清理输入或切换会话");
16
+ if (state.text === null)
17
+ throw new Error("桌面端输入框未就绪");
18
+ if (!state.text?.trim())
19
+ return;
20
+ await (0, promises_1.mkdir)(dataDir, { recursive: true, mode: 0o700 });
21
+ await (0, promises_1.appendFile)((0, node_path_1.join)(dataDir, "draft-recovery.jsonl"), JSON.stringify({ time: new Date().toISOString(), url: state.url, text: state.text }) + "\n", { encoding: "utf8", mode: 0o600 });
22
+ const cleared = await evaluate(`(() => {
23
+ const editor = document.querySelector(${JSON.stringify(editorSelector)});
24
+ const unchanged = () => editor && editor.innerText === ${JSON.stringify(state.text)}
25
+ && !document.querySelector(${JSON.stringify(stopSelector)});
26
+ if (!unchanged()) return false;
27
+ editor.focus();
28
+ if (!unchanged()) return false;
29
+ const selection = window.getSelection();
30
+ if (!selection) return false;
31
+ const range = document.createRange();
32
+ range.selectNodeContents(editor);
33
+ selection.removeAllRanges();
34
+ selection.addRange(range);
35
+ document.execCommand('delete');
36
+ const current = document.querySelector(${JSON.stringify(editorSelector)});
37
+ return Boolean(current && !current.innerText.trim());
38
+ })()`);
39
+ if (cleared !== true)
40
+ throw new Error("旧草稿已备份,但输入内容发生变化或清理失败,保留当前内容");
41
+ }
@@ -5,7 +5,9 @@ exports.freshAssistantMessages = freshAssistantMessages;
5
5
  exports.verifyQwenHeartbeatReply = verifyQwenHeartbeatReply;
6
6
  exports.isQwenHeartbeatReplyComplete = isQwenHeartbeatReplyComplete;
7
7
  exports.isQwenApiUnavailableReply = isQwenApiUnavailableReply;
8
+ exports.isQwenBridgeHealthPrompt = isQwenBridgeHealthPrompt;
8
9
  const node_crypto_1 = require("node:crypto");
10
+ const cdp_draft_1 = require("../cdp-draft");
9
11
  exports.assistantMessageSelector = '[data-message-id][data-part-type="text"]:not([data-conversation-user-text="true"])';
10
12
  exports.inputSelector = '.chat-input-editor-text[contenteditable="true"]';
11
13
  function freshAssistantMessages(beforeIds, messages) {
@@ -22,6 +24,10 @@ function isQwenHeartbeatReplyComplete(reply, expected) {
22
24
  function isQwenApiUnavailableReply(reply) {
23
25
  return /^(?:API|接口)\s*(?:暂时)?不可用[。.!!]?$/i.test(reply.trim());
24
26
  }
27
+ /** 只识别桥接自己生成的健康检测提示,避免删除用户草稿。 */
28
+ function isQwenBridgeHealthPrompt(text) {
29
+ return /^这是后台健康检测。请只回复 MAB_QWEN_[A-Z0-9]+,不要添加任何其它文字。$/.test(text.trim());
30
+ }
25
31
  function wait(ms, signal) {
26
32
  if (signal?.aborted)
27
33
  return Promise.reject(new Error("请求已取消"));
@@ -47,6 +53,7 @@ class QwenCdpClient {
47
53
  workspace: options.workspace ?? `${process.env.APPDATA ?? ""}\\QwenWorkCN\\workspace`,
48
54
  timeoutMs: options.timeoutMs ?? 30_000,
49
55
  fetchImpl: options.fetchImpl,
56
+ dataDir: options.dataDir,
50
57
  };
51
58
  }
52
59
  connected() { return this.socket?.readyState === WebSocket.OPEN; }
@@ -64,6 +71,7 @@ class QwenCdpClient {
64
71
  verifyQwenHeartbeatReply(token, reply);
65
72
  }
66
73
  async createSession(_name) {
74
+ await this.recoverDraft();
67
75
  const snapshot = `(() => ({ url: location.href, messages: document.querySelectorAll('[data-message-id]').length, input: document.querySelector(${JSON.stringify(exports.inputSelector)})?.innerText ?? null, blank: Boolean(document.querySelector('.new-task-model-selector')) }))()`;
68
76
  const before = await this.evaluate(snapshot);
69
77
  if (before.input?.trim())
@@ -136,6 +144,12 @@ class QwenCdpClient {
136
144
  });
137
145
  }
138
146
  async direct(text, timeoutMs = 240_000, signal, expectedReply) {
147
+ if (expectedReply) {
148
+ const draft = await this.evaluate(`document.querySelector(${JSON.stringify(exports.inputSelector)})?.innerText ?? ''`);
149
+ if (typeof draft === "string" && draft.trim() && !isQwenBridgeHealthPrompt(draft))
150
+ throw new Error("千问输入框有用户草稿,跳过后台健康检测");
151
+ }
152
+ await this.recoverDraft();
139
153
  const ready = await this.evaluate(`(() => { const input = document.querySelector(${JSON.stringify(exports.inputSelector)}); return Boolean(input && !input.innerText.trim() && !document.querySelector('button[aria-label="停止生成"],button[aria-label="Stop generating"]')); })()`);
140
154
  if (!ready)
141
155
  throw new Error("千问输入框含有未发送内容或正在生成回复,未发送新消息");
@@ -188,6 +202,9 @@ class QwenCdpClient {
188
202
  this.pending.delete(message.id);
189
203
  pending.resolve(message);
190
204
  }
205
+ recoverDraft() {
206
+ return (0, cdp_draft_1.recoverCdpDraft)(this.evaluate.bind(this), exports.inputSelector, 'button[aria-label="停止生成"],button[aria-label="Stop generating"]', this.options.dataDir);
207
+ }
191
208
  async cdp(method, params = {}, timeoutMs = this.options.timeoutMs) {
192
209
  if (!this.socket)
193
210
  throw new Error("CDP 尚未连接");
@@ -11,7 +11,7 @@ const desktop_launch_1 = require("../desktop-launch");
11
11
  const CHAT_HEARTBEAT_INTERVAL_MS = 2 * 60 * 60_000;
12
12
  exports.qwenworkManifest = {
13
13
  id: "qwenwork",
14
- version: "1.0.4",
14
+ version: "1.0.10",
15
15
  displayName: "千问办公",
16
16
  entry: "dist/index.js",
17
17
  apiVersion: "1",
@@ -83,6 +83,7 @@ class QwenworkPlugin {
83
83
  if (!this.relayTarget) {
84
84
  const client = this.injectedClient ?? new cdp_client_1.QwenCdpClient({
85
85
  port: this.activeCdpPort,
86
+ dataDir: context.dataDir,
86
87
  subChatId: typeof config.subChatId === "string" ? config.subChatId : undefined,
87
88
  chatId: typeof config.chatId === "string" ? config.chatId : undefined,
88
89
  workspace: typeof config.workspace === "string" ? config.workspace : undefined,
@@ -233,6 +234,7 @@ class QwenworkPlugin {
233
234
  this.healthTimer = undefined;
234
235
  this.state = "stopped";
235
236
  this.stopped = true;
237
+ await this.requestQueue;
236
238
  if (this.recovery)
237
239
  await this.recovery;
238
240
  for (const dispose of this.disposers.splice(0))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-bridge-qwenwork",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "agent-bridge 的千问办公插件。",
5
5
  "keywords": [
6
6
  "mu-agent-bridge-plugin",
package/plugin.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "qwenwork",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "type": "agent",
5
5
  "displayName": "千问办公",
6
6
  "entry": "dist/src-ts/plugins/qwenwork/index.js",