agent-bridge-qwenwork 1.0.6 → 1.0.8

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
@@ -6,8 +6,10 @@
6
6
 
7
7
  `executablePath` 配置保存在独立状态目录,插件升级和重启会继续使用。千问桌面升级后,若路径指向带版本号的目录,插件会从同一安装位置查找最高版本的可执行文件;即使旧版本仍保留,也会启动新版。非版本目录中的固定路径仍优先使用;路径失效时再扫描常见安装目录。重载千问插件后,运行日志会显示实际启动的 `executablePath`。
8
8
 
9
- 后台每 30 秒检查主页面与连接,每 30 分钟发送一条带随机校验码的真实测试消息。首次启动及恢复后也必须完成真实对话,只有千问原样返回本次校验码才判定可用。失败先标记降级,连续两次失败后自动重启千问并重连 CDP,恢复后还必须通过真实回复验证。AUN、飞书和 HTTP 共用请求队列,恢复不修改 AID 或 owner 配置。`autoStart: false` 禁止自动启动和重启。
9
+ 后台每 30 秒检查主页面与 CDP 连接,空闲时每 2 小时最多发送一条带随机校验码的真实测试消息。首次启动及 CDP 恢复后必须完成真实对话,只有千问原样返回本次校验码才判定可用。任务或 `/new` 执行期间不发送检测消息、不执行恢复。普通 API/UI 错误或健康通信失败但 CDP 仍连接时只标记降级;只有 CDP 已断开才允许自动重启千问并重连。AUN、飞书和 HTTP 共用请求队列,恢复不修改 AID 或 owner 配置。`autoStart: false` 禁止自动启动和重启。
10
10
 
11
- 请求超时或明确返回“API不可用”时立即恢复,已经发送的任务不自动重发,避免重复执行。健康接口 `/api/plugins/qwenwork/health` 的 `lastHeartbeat` 表示最近真实对话验证通过时间,同时返回 `restarts` 和 `recovering`;日志可搜索“千问真实对话心跳通过”“千问心跳失败”“千问自动恢复成功”。自动恢复会结束千问进程及其子进程,可能打断该应用中手动进行的任务;daemon 应在已登录的 Windows 用户桌面会话运行。
11
+ 请求超时或明确返回“API不可用”时只结束本次请求,已经发送的任务不自动重发,避免重复执行。健康接口 `/api/plugins/qwenwork/health` 的 `lastHeartbeat` 表示最近真实对话验证通过时间,同时返回 `restarts` 和 `recovering`;日志可搜索“千问真实对话心跳通过”“千问心跳失败”“千问自动恢复成功”。自动恢复只在 CDP 断开且当前无任务时结束千问进程及其子进程;daemon 应在已登录的 Windows 用户桌面会话运行。
12
+
13
+ `/new` 在已确认的空白新任务页可重复执行;有未发送草稿时会拒绝操作并保留内容。发送消息会点击桌面端“发送消息”按钮并确认输入框清空,避免仅按 Enter 后消息仍停留在输入框。真实通信失败后保持降级,普通 CDP 检查通过不会把状态改回就绪,须有真实回复或真实对话心跳通过。
12
14
 
13
15
  真实心跳会消耗模型额度,并在当前千问聊天会话留下检测消息;它通过与用户请求相同的发送和取回回复路径运行。云端故障、账号额度不足或登录过期仍可能需要修复服务或账号,验证失败期间不会报告健康。
@@ -64,12 +64,16 @@ class QwenCdpClient {
64
64
  verifyQwenHeartbeatReply(token, reply);
65
65
  }
66
66
  async createSession(_name) {
67
- const snapshot = `(() => ({ url: location.href, messages: document.querySelectorAll(${JSON.stringify(exports.assistantMessageSelector)}).length, input: Boolean(document.querySelector(${JSON.stringify(exports.inputSelector)})) }))()`;
67
+ 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
68
  const before = await this.evaluate(snapshot);
69
+ if (before.input?.trim())
70
+ throw new Error("千问输入框含有未发送内容,不能新建会话");
71
+ if (before.input !== null && before.messages === 0 && before.blank)
72
+ return;
69
73
  const clicked = await this.evaluate(`(() => { const button = document.querySelector('button[aria-label="新任务"]'); if (!button || button.disabled || button.getAttribute('aria-disabled') === 'true') return false; button.click(); return true; })()`);
70
74
  if (clicked !== true)
71
75
  throw new Error("千问新任务入口未找到或不可用");
72
- await this.waitFor(`(() => { const state = ${snapshot}; return state.input && state.messages === 0 && state.url !== ${JSON.stringify(before.url)}; })()`, "千问新会话状态校验失败");
76
+ await this.waitFor(`(() => { const state = ${snapshot}; return state.input !== null && state.input.trim() === '' && state.messages === 0 && state.url !== ${JSON.stringify(before.url)}; })()`, "千问新会话状态校验失败");
73
77
  }
74
78
  async connect() {
75
79
  if (this.connected())
@@ -132,6 +136,9 @@ class QwenCdpClient {
132
136
  });
133
137
  }
134
138
  async direct(text, timeoutMs = 240_000, signal, expectedReply) {
139
+ 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
+ if (!ready)
141
+ throw new Error("千问输入框含有未发送内容或正在生成回复,未发送新消息");
135
142
  const snapshot = await this.evaluate(`JSON.stringify([...document.querySelectorAll(${JSON.stringify(exports.assistantMessageSelector)})].map(element => ({ id: element.getAttribute('data-message-id'), text: element.innerText })))`);
136
143
  const before = JSON.parse(snapshot || "[]");
137
144
  const beforeIds = new Set(before.map((message) => message.id).filter((id) => Boolean(id)));
@@ -144,12 +151,10 @@ class QwenCdpClient {
144
151
  if (!focused)
145
152
  throw new Error("千问输入框未找到");
146
153
  await this.cdp("Input.insertText", { text });
147
- for (const type of ["keyDown", "keyUp"]) {
148
- await this.cdp("Input.dispatchKeyEvent", {
149
- type, key: "Enter", code: "Enter", text: type === "keyDown" ? "\r" : "", unmodifiedText: type === "keyDown" ? "\r" : "",
150
- windowsVirtualKeyCode: 13, nativeVirtualKeyCode: 13,
151
- });
152
- }
154
+ const sent = await this.evaluate(`(() => { const input = document.querySelector(${JSON.stringify(exports.inputSelector)}); const button = document.querySelector('button[aria-label="发送消息"]'); if (!input || input.innerText.trim() !== ${JSON.stringify(text.trim())} || !button || button.disabled || button.getAttribute('aria-disabled') === 'true') return false; button.click(); return true; })()`);
155
+ if (sent !== true)
156
+ throw new Error("千问发送按钮未就绪或输入校验失败,未发送消息");
157
+ await this.waitFor(`(() => { const input = document.querySelector(${JSON.stringify(exports.inputSelector)}); return Boolean(input && !input.innerText.trim()); })()`, "千问未确认消息已提交");
153
158
  let previousReply = "";
154
159
  let stablePolls = 0;
155
160
  const deadline = Date.now() + timeoutMs;
@@ -8,6 +8,7 @@ const openai_adapter_1 = require("./openai-adapter");
8
8
  const cdp_client_1 = require("./cdp-client");
9
9
  const plugin_1 = require("../../plugin");
10
10
  const desktop_launch_1 = require("../desktop-launch");
11
+ const CHAT_HEARTBEAT_INTERVAL_MS = 2 * 60 * 60_000;
11
12
  exports.qwenworkManifest = {
12
13
  id: "qwenwork",
13
14
  version: "1.0.4",
@@ -47,9 +48,11 @@ class QwenworkPlugin {
47
48
  context;
48
49
  checking = false;
49
50
  failures = 0;
51
+ lastHeartbeatAttempt;
50
52
  lastHeartbeat;
51
53
  restarts = 0;
52
54
  busy = false;
55
+ taskCount = 0;
53
56
  stopped = true;
54
57
  constructor(options = {}) {
55
58
  this.injectedClient = options.client;
@@ -63,6 +66,8 @@ class QwenworkPlugin {
63
66
  return;
64
67
  this.state = "starting";
65
68
  this.stopped = false;
69
+ this.lastHeartbeatAttempt = undefined;
70
+ this.lastHeartbeat = undefined;
66
71
  this.context = context;
67
72
  try {
68
73
  const config = context.config;
@@ -96,7 +101,6 @@ class QwenworkPlugin {
96
101
  await (0, desktop_launch_1.launchAndWait)(executable, [`--remote-debugging-port=${this.activeCdpPort}`], async () => {
97
102
  try {
98
103
  await client.connect();
99
- await client.healthCheck?.();
100
104
  }
101
105
  catch (error) {
102
106
  client.close();
@@ -122,7 +126,7 @@ class QwenworkPlugin {
122
126
  }
123
127
  }
124
128
  }
125
- this.adapter = new openai_adapter_1.OpenAIAdapter((prompt, signal) => this.enqueue(() => this.complete(prompt, asNumber(config.timeoutMs, 240_000), signal)), {
129
+ this.adapter = new openai_adapter_1.OpenAIAdapter((prompt, signal) => this.enqueueTask(() => this.complete(prompt, asNumber(config.timeoutMs, 240_000), signal)), {
126
130
  onLog: (message) => context.logger?.info(message),
127
131
  onExchange: (direction, text, peer, agentAid) => context.logger?.info("千问通信", { direction, text, peer, agentAid }),
128
132
  });
@@ -135,7 +139,7 @@ class QwenworkPlugin {
135
139
  if (this.configuredHttpPort > 0)
136
140
  await this.adapter.listen(this.configuredHttpPort);
137
141
  }
138
- this.state = this.client?.chatHeartbeat ? "starting" : "ready";
142
+ this.state = "ready";
139
143
  this.message = "";
140
144
  this.startHealthWatch();
141
145
  void this.checkHealth();
@@ -166,7 +170,7 @@ class QwenworkPlugin {
166
170
  await this.recovery;
167
171
  if (this.state === "stopped" || (!this.relayTarget && !this.client))
168
172
  throw new Error("qwenwork 插件未就绪");
169
- const text = await this.enqueue(() => this.complete(request.task, request.timeoutMs ?? 240_000));
173
+ const text = await this.enqueueTask(() => this.complete(request.task, request.timeoutMs ?? 240_000));
170
174
  return { text };
171
175
  }
172
176
  async controlSession(request) {
@@ -181,7 +185,7 @@ class QwenworkPlugin {
181
185
  throw new Error("千问办公当前传输不支持新建会话");
182
186
  if (!this.client.connected())
183
187
  throw new Error("千问办公 CDP 未连接");
184
- return this.enqueue(async () => {
188
+ return this.enqueueTask(async () => {
185
189
  await this.client.createSession(request.name);
186
190
  return { text: "千问办公已创建新会话", sessionId };
187
191
  });
@@ -193,17 +197,12 @@ class QwenworkPlugin {
193
197
  }
194
198
  if (!this.client)
195
199
  throw new Error("qwenwork 插件未就绪");
196
- if (this.recovery)
197
- await this.recovery;
198
- this.busy = true;
199
200
  try {
200
- if (this.state !== "ready")
201
- await this.recover();
202
- if (this.state !== "ready")
203
- throw new Error(this.message || "千问正在恢复");
201
+ if (!this.client.connected())
202
+ throw new Error("千问 CDP 已断开,等待空闲后自动恢复");
204
203
  const reply = await this.client.direct(prompt, timeoutMs, signal);
205
204
  if ((0, cdp_client_1.isQwenApiUnavailableReply)(reply))
206
- throw new Error("千问生成接口不可用,已触发后台恢复");
205
+ throw new Error("千问生成接口不可用");
207
206
  this.failures = 0;
208
207
  this.state = "ready";
209
208
  this.message = "";
@@ -211,16 +210,12 @@ class QwenworkPlugin {
211
210
  }
212
211
  catch (error) {
213
212
  if (!signal?.aborted) {
214
- this.failures = 2;
215
213
  this.message = error instanceof Error ? error.message : String(error);
214
+ this.failures = this.client.connected() ? 0 : 2;
216
215
  this.state = "degraded";
217
- await this.recover();
218
216
  }
219
217
  throw error;
220
218
  }
221
- finally {
222
- this.busy = false;
223
- }
224
219
  }
225
220
  registerRoutes(http) {
226
221
  if (!this.adapter)
@@ -251,40 +246,37 @@ class QwenworkPlugin {
251
246
  this.pluginStatus = undefined;
252
247
  }
253
248
  startHealthWatch() {
254
- if (this.relayTarget || !this.client?.healthCheck || this.healthTimer)
249
+ if (this.relayTarget || (!this.client?.healthCheck && !this.client?.chatHeartbeat) || this.healthTimer)
255
250
  return;
256
251
  this.healthTimer = setInterval(() => { void this.checkHealth(); }, this.healthIntervalMs);
257
252
  this.healthTimer.unref();
258
253
  }
259
254
  async checkHealth() {
260
- if (this.checking || this.busy || this.recovery || this.state === "stopped" || !this.client?.healthCheck)
255
+ if (this.checking || this.busy || this.recovery || this.state === "stopped" || (!this.client?.healthCheck && !this.client?.chatHeartbeat))
261
256
  return;
262
257
  const client = this.client;
263
258
  this.checking = true;
264
259
  await this.enqueue(async () => {
265
260
  try {
266
- if (this.stopped)
261
+ if (this.stopped || this.busy)
267
262
  return;
268
- if (this.failures >= 2) {
263
+ if (!client.connected()) {
269
264
  await this.recover();
270
265
  return;
271
266
  }
272
- if (client.chatHeartbeat && this.state === "ready" && this.lastHeartbeat && Date.now() - Date.parse(this.lastHeartbeat) < 30 * 60_000) {
273
- await client.healthCheck?.();
274
- return;
275
- }
276
- if (client.chatHeartbeat)
267
+ if (client.chatHeartbeat && (!this.lastHeartbeatAttempt || Date.now() - Date.parse(this.lastHeartbeatAttempt) >= CHAT_HEARTBEAT_INTERVAL_MS)) {
268
+ this.lastHeartbeatAttempt = new Date().toISOString();
277
269
  await client.chatHeartbeat();
278
- else
270
+ this.lastHeartbeat = new Date().toISOString();
271
+ this.state = "ready";
272
+ this.message = "";
273
+ }
274
+ else {
279
275
  await client.healthCheck?.();
276
+ }
280
277
  if (this.stopped)
281
278
  return;
282
- this.lastHeartbeat = new Date().toISOString();
283
279
  this.failures = 0;
284
- this.state = "ready";
285
- this.message = "";
286
- if (client.chatHeartbeat)
287
- this.context?.logger?.info("千问真实对话心跳通过", { verifiedAt: this.lastHeartbeat });
288
280
  }
289
281
  catch (error) {
290
282
  if (this.stopped)
@@ -292,7 +284,7 @@ class QwenworkPlugin {
292
284
  this.state = "degraded";
293
285
  this.message = error instanceof Error ? error.message : String(error);
294
286
  this.context?.logger?.error(`千问心跳失败:${this.message}`);
295
- if (++this.failures >= 2)
287
+ if (++this.failures >= 2 && !client.connected())
296
288
  await this.recover();
297
289
  }
298
290
  finally {
@@ -303,24 +295,23 @@ class QwenworkPlugin {
303
295
  async recover() {
304
296
  if (this.recovery)
305
297
  return this.recovery;
306
- if (this.stopped)
298
+ if (this.stopped || this.busy)
299
+ return;
300
+ const client = this.client;
301
+ if (!client || client.connected())
307
302
  return;
308
303
  this.recovery = (async () => {
309
304
  this.state = "starting";
310
305
  this.context?.logger?.info("千问自动恢复开始", { reason: this.message });
311
- this.client?.close();
306
+ client.close();
312
307
  if (this.restartDesktop)
313
308
  await this.restartDesktop();
314
309
  else if (this.restart)
315
310
  await this.restart();
316
- const client = this.client;
317
- if (!client)
318
- throw new Error("千问客户端未初始化");
319
311
  await client.connect();
320
312
  if (client.chatHeartbeat)
321
- await client.chatHeartbeat();
322
- else
323
- await client.healthCheck?.();
313
+ this.lastHeartbeatAttempt = new Date().toISOString();
314
+ await this.verifyConnection(client);
324
315
  if (this.stopped) {
325
316
  client.close();
326
317
  return;
@@ -328,8 +319,9 @@ class QwenworkPlugin {
328
319
  this.state = "ready";
329
320
  this.message = "";
330
321
  this.failures = 0;
322
+ if (client.chatHeartbeat)
323
+ this.lastHeartbeat = new Date().toISOString();
331
324
  this.restarts += 1;
332
- this.lastHeartbeat = new Date().toISOString();
333
325
  this.context?.logger?.info("千问自动恢复成功", { restarts: this.restarts });
334
326
  })().catch((error) => {
335
327
  if (this.stopped)
@@ -340,11 +332,25 @@ class QwenworkPlugin {
340
332
  }).finally(() => { this.recovery = undefined; });
341
333
  return this.recovery;
342
334
  }
335
+ async verifyConnection(client) {
336
+ if (client.chatHeartbeat)
337
+ await client.chatHeartbeat();
338
+ else
339
+ await client.healthCheck?.();
340
+ }
343
341
  enqueue(operation) {
344
342
  const run = this.requestQueue.then(operation, operation);
345
343
  this.requestQueue = run.then(() => undefined, () => undefined);
346
344
  return run;
347
345
  }
346
+ enqueueTask(operation) {
347
+ this.taskCount += 1;
348
+ this.busy = true;
349
+ return this.enqueue(operation).finally(() => {
350
+ this.taskCount -= 1;
351
+ this.busy = this.taskCount > 0;
352
+ });
353
+ }
348
354
  }
349
355
  exports.QwenworkPlugin = QwenworkPlugin;
350
356
  function createQwenworkPlugin(options = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-bridge-qwenwork",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
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.6",
3
+ "version": "1.0.8",
4
4
  "type": "agent",
5
5
  "displayName": "千问办公",
6
6
  "entry": "dist/src-ts/plugins/qwenwork/index.js",