agent-bridge-qwenwork 1.0.31 → 1.0.37
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 +8 -4
- package/config.schema.json +1 -1
- package/dist/src-ts/plugins/agent-task-ability.js +36 -0
- package/dist/src-ts/plugins/desktop-launch.js +15 -2
- package/dist/src-ts/plugins/qwenwork/ability/generated/index.js +7 -0
- package/dist/src-ts/plugins/qwenwork/ability/index.js +5 -0
- package/dist/src-ts/plugins/qwenwork/cdp-client.js +14 -1
- package/dist/src-ts/plugins/qwenwork/index.js +81 -23
- package/package.json +1 -1
- package/plugin.json +1 -1
package/README.md
CHANGED
|
@@ -6,16 +6,20 @@
|
|
|
6
6
|
|
|
7
7
|
`executablePath` 配置保存在独立状态目录,插件升级和重启会继续使用。千问桌面升级后,若路径指向带版本号的目录,插件会从同一安装位置查找最高版本的可执行文件;即使旧版本仍保留,也会启动新版。非版本目录中的固定路径仍优先使用;路径失效时再扫描常见安装目录。重载千问插件后,运行日志会显示实际启动的 `executablePath`。
|
|
8
8
|
|
|
9
|
-
后台每 30
|
|
9
|
+
后台每 30 秒只读取已有 CDP 连接状态,不发送测试聊天、不探测页面、不启动或重启桌面。用户关闭千问后保持离线;收到 Peer AID 消息或其它真实请求时,才按需连接或启动桌面。ABWeb 中显式启动、重载和重启桌面仍可启动程序。AUN、飞书和 HTTP 共用请求队列,恢复不修改 AID 或 owner 配置。`autoStart: false` 禁止请求自动启动桌面。
|
|
10
10
|
|
|
11
11
|
原生历史读取目前明确不支持:已核验的 CDP/tRPC 客户端只有任务提交等操作,没有只读会话枚举或历史读取接口。插件不会猜测 RPC、把配置中的 `chatId`/`subChatId` 当作当前身份,也不会为读取历史而点击、切换或发送内容;`sessionHistory` 的 scope 固定为本插件配置的 CDP 端口。中继、忙碌、恢复中、离线或未就绪时会返回对应拒绝原因。
|
|
12
12
|
|
|
13
|
-
请求超时或明确返回“API不可用”时只结束本次请求,已经发送的任务不自动重发,避免重复执行。健康接口 `/api/plugins/qwenwork/health`
|
|
13
|
+
请求超时或明确返回“API不可用”时只结束本次请求,已经发送的任务不自动重发,避免重复执行。健康接口 `/api/plugins/qwenwork/health` 返回连接状态、`restarts` 和 `recovering`;`lastHeartbeat` 为旧接口兼容字段,当前不会产生心跳。日志可搜索“千问 CDP 自动重连开始”和“千问 CDP 自动重连成功”。daemon 应在已登录的 Windows 用户桌面会话运行。
|
|
14
14
|
|
|
15
|
-
`/new` 在已确认的空白新任务页可重复执行。远程消息和 `/new` 遇到输入框残留时,先将原文、页面地址和时间追加到插件数据目录的 `draft-recovery.jsonl
|
|
15
|
+
`/new` 在已确认的空白新任务页可重复执行。远程消息和 `/new` 遇到输入框残留时,先将原文、页面地址和时间追加到插件数据目录的 `draft-recovery.jsonl`,再通过编辑器原生删除操作清理;备份失败、内容发生变化或正在生成回复时不清理。发送消息会点击桌面端“发送消息”按钮并确认输入框清空,避免仅按 Enter 后消息仍停留在输入框。真实通信失败后保持降级,定时检查不会将状态改回就绪。
|
|
16
16
|
|
|
17
17
|
提交前等待发送按钮就绪;窗口在插入、点击复查或回复轮询期间被最小化时,插件会恢复千问主窗口并继续确认。只有复查确认尚未点击时最多再尝试一次;仅备份并清理原会话内仍与本次请求完全一致的文字,保留用户后来修改的草稿。点击后无法确认提交状态时不会自动重发或清理输入,避免重复执行。
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
云端故障、账号额度不足或登录过期仍可能需要修复服务或账号,重连 CDP 本身不能证明上游模型可用。
|
|
20
20
|
|
|
21
21
|
文件、图片、视频附件经桌面原生上传入口送入本轮任务;回传只采集本轮助手通过“已展示最终文件”工具声明的产物,不扫描普通正文中的任意路径。图片按内容去重,视频以 `video/*` 文件附件交给 AUN,使用分块读取避免大视频导致 CDP 断连。单条最多 8 个附件,单个不超过 32 MiB、合计不超过 64 MiB;实际可处理的格式及内容理解能力由桌面端和模型决定。验证证据见仓库的 `docs/verification/2026-10-20-qwenwork-media.md`。
|
|
22
|
+
|
|
23
|
+
## 远端任务 Ability
|
|
24
|
+
|
|
25
|
+
声明 `agent/task/run`,参数为 `{"task":"需要执行的自然语言任务"}`,在独立新会话中调用现有大模型并返回 `text`。需在 AUN 中验证 Provider 的 Owner、勾选发布此能力,并逐次确认执行;升级不会自动公开。任务可能读写文件、使用工具或访问网络,按目标 Agent 的权限执行。当前 Ability 仅交付文字,图片和文件仍使用原有聊天附件入口;后端返回附件时会明确提示尚未交付。
|
package/config.schema.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"chatId": { "type": "string" },
|
|
10
10
|
"workspace": { "type": "string" },
|
|
11
11
|
"executablePath": { "type": "string", "description": "QwenWorkCN.exe 路径,失效时自动查找新版本" },
|
|
12
|
-
"autoStart": { "type": "boolean", "default": true, "description": "CDP
|
|
12
|
+
"autoStart": { "type": "boolean", "default": true, "description": "激活或收到消息时,CDP 未连接则启动千问办公;定时检查不启动桌面" },
|
|
13
13
|
"startupTimeoutMs": { "type": "integer", "minimum": 1000, "default": 30000 },
|
|
14
14
|
"timeoutMs": { "type": "integer", "minimum": 1000, "default": 240000 },
|
|
15
15
|
"relayTarget": { "type": "string", "enum": ["dsh"], "description": "将文本原样中继到 DSH,不调用千问 CDP" }
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createTaskAbility = createTaskAbility;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
const agent_1 = require("../agent");
|
|
6
|
+
function createTaskAbility(handle) {
|
|
7
|
+
return {
|
|
8
|
+
definition: {
|
|
9
|
+
path: "agent/task/run",
|
|
10
|
+
name: "执行大模型任务",
|
|
11
|
+
summary: "在独立新会话中执行自然语言任务并返回文字;可能使用目标 Agent 的工具、读写文件及访问网络,须逐次确认。图片和文件不通过此接口交付。",
|
|
12
|
+
contract_revision: "1",
|
|
13
|
+
params_schema: { type: "object", required: ["task"], properties: { task: { type: "string", minLength: 1, maxLength: 20_000, pattern: "\\S" } }, additionalProperties: false },
|
|
14
|
+
result_schema: { type: "object", required: ["text"], properties: { text: { type: "string", minLength: 1, maxLength: 60_000 }, attachments_omitted: { type: "integer", minimum: 1 } }, additionalProperties: false },
|
|
15
|
+
effects: ["state.read", "state.write", "data.egress", "data.local_write", "credential.access", "code.execute", "system.interact"],
|
|
16
|
+
auth_required: true, require_confirm: true, idempotency: "keyed",
|
|
17
|
+
allowed_modes: ["explicit"], aggregation: "none", dedupe_retention_seconds: 3600,
|
|
18
|
+
},
|
|
19
|
+
handler: async (params, { signal }) => {
|
|
20
|
+
signal.throwIfAborted();
|
|
21
|
+
const task = params?.task;
|
|
22
|
+
if (!params || typeof params !== "object" || Array.isArray(params) || Object.keys(params).length !== 1 || typeof task !== "string" || !task.trim() || task.length > 20_000)
|
|
23
|
+
throw new Error("任务参数必须仅包含 1 至 20,000 字符的非空 task");
|
|
24
|
+
const response = await (0, agent_1.abortable)(handle({ task, sessionId: (0, node_crypto_1.randomUUID)(), newSession: true, signal, attachmentOutput: false }), signal);
|
|
25
|
+
const omitted = response.attachments?.length || response.images?.length || 0;
|
|
26
|
+
let text = response.text?.trim() ?? "";
|
|
27
|
+
if (omitted)
|
|
28
|
+
text += `${text ? "\n\n" : ""}附件尚未通过本次 Ability 交付(${omitted} 个);请使用现有聊天附件入口获取。`;
|
|
29
|
+
if (!text)
|
|
30
|
+
throw new Error("任务未返回文字或附件,不能确认执行结果");
|
|
31
|
+
if (text.length > 60_000)
|
|
32
|
+
throw new Error("任务返回文字超过 60,000 字符,未截断或宣称交付完成");
|
|
33
|
+
return { text, ...(omitted ? { attachments_omitted: omitted } : {}) };
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.desktopLaunchEnvironment = desktopLaunchEnvironment;
|
|
4
4
|
exports.findWindowsExecutable = findWindowsExecutable;
|
|
5
|
+
exports.isWindowsExecutableRunning = isWindowsExecutableRunning;
|
|
5
6
|
exports.findExecutableInTree = findExecutableInTree;
|
|
6
7
|
exports.findChromeExecutable = findChromeExecutable;
|
|
7
8
|
exports.launchAndWait = launchAndWait;
|
|
@@ -99,6 +100,17 @@ async function findWindowsExecutable(fileName, configured, run = execFileAsync)
|
|
|
99
100
|
}
|
|
100
101
|
return undefined;
|
|
101
102
|
}
|
|
103
|
+
async function isWindowsExecutableRunning(executable, platform = process.platform, run = execFileAsync) {
|
|
104
|
+
if (platform !== "win32")
|
|
105
|
+
return false;
|
|
106
|
+
const target = (0, node_path_1.resolve)(executable).replace(/'/g, "''");
|
|
107
|
+
const script = `$ErrorActionPreference='Stop';$target='${target}';$running=Get-CimInstance Win32_Process | Where-Object { $_.ExecutablePath -ieq $target } | Select-Object -First 1;if($running){'true'}else{'false'}`;
|
|
108
|
+
const { stdout } = await run("powershell.exe", ["-NoProfile", "-NonInteractive", "-EncodedCommand", Buffer.from(script, "utf16le").toString("base64")], { timeout: 10_000, windowsHide: true });
|
|
109
|
+
const result = stdout.trim().toLowerCase();
|
|
110
|
+
if (result !== "true" && result !== "false")
|
|
111
|
+
throw new Error("无法确认桌面进程状态,已跳过自动启动");
|
|
112
|
+
return result === "true";
|
|
113
|
+
}
|
|
102
114
|
async function findRegisteredWindowsExecutable(fileName, run) {
|
|
103
115
|
const target = fileName.replace(/'/g, "''");
|
|
104
116
|
const script = `$ErrorActionPreference='SilentlyContinue';[Console]::OutputEncoding=[Text.Encoding]::UTF8;$target='${target}'
|
|
@@ -223,7 +235,7 @@ async function prepareDesktopPort(executable, preferred, platform = process.plat
|
|
|
223
235
|
await terminate(executable);
|
|
224
236
|
return selectDesktopPort(preferred);
|
|
225
237
|
}
|
|
226
|
-
/**
|
|
238
|
+
/** 清理同一用户、同一安装路径的桌面进程;千问还需接管无参数的单实例主进程。 */
|
|
227
239
|
async function terminateWindowsDesktop(executable, keepPort, platform = process.platform, run = execFileAsync) {
|
|
228
240
|
if (platform !== "win32")
|
|
229
241
|
return [];
|
|
@@ -236,7 +248,8 @@ $ProgressPreference = 'SilentlyContinue'
|
|
|
236
248
|
$target = '${(0, node_path_1.resolve)(executable).replace(/'/g, "''")}'
|
|
237
249
|
$sid = [Security.Principal.WindowsIdentity]::GetCurrent().User.Value
|
|
238
250
|
$all = @(Get-CimInstance Win32_Process)
|
|
239
|
-
$
|
|
251
|
+
$isQwen = [IO.Path]::GetFileName($target) -ieq 'QwenWorkCN.exe'
|
|
252
|
+
$processes = @($all | Where-Object { $_.ExecutablePath -ieq $target -and (($isQwen -and $_.CommandLine -notmatch '(?i)--type=|browser-extension-relay-worker') -or (-not $isQwen -and $_.CommandLine -match '(?i)(?:^|[\\s"])--remote-debugging-(?:port|address)(?:=|\\s|")')) } | Where-Object { (Invoke-CimMethod -InputObject $_ -MethodName GetOwnerSid).Sid -eq $sid })
|
|
240
253
|
$ids = @($processes.ProcessId)
|
|
241
254
|
$roots = @($processes | Where-Object { $ids -notcontains $_.ParentProcessId })
|
|
242
255
|
${keepPort === undefined ? "" : `
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createGeneratedAbilities = createGeneratedAbilities;
|
|
4
|
+
const generated = [];
|
|
5
|
+
function createGeneratedAbilities() {
|
|
6
|
+
return generated.filter(item => item.enabled).map(item => item.ability);
|
|
7
|
+
}
|
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.readinessDefinition = void 0;
|
|
4
4
|
exports.createReadinessAbility = createReadinessAbility;
|
|
5
|
+
exports.createQwenworkAbilities = createQwenworkAbilities;
|
|
6
|
+
const generated_1 = require("./generated");
|
|
5
7
|
const definition = { path: "agent/status/readiness", name: "Agent 就绪状态", summary: "读取 Agent 当前是否就绪。", contract_revision: "1", params_schema: { type: "object", properties: {}, additionalProperties: false }, result_schema: { type: "object", required: ["status"], properties: { status: { type: "string", enum: ["ready", "degraded", "failed"] } }, additionalProperties: false }, effects: ["state.read"], auth_required: true, require_confirm: true, idempotency: "keyed", allowed_modes: ["explicit"], aggregation: "none", dedupe_retention_seconds: 3600 };
|
|
6
8
|
exports.readinessDefinition = definition;
|
|
7
9
|
function createReadinessAbility(health) { return { definition, handler: async (params) => { if (!params || typeof params !== "object" || Array.isArray(params) || Object.keys(params).length)
|
|
8
10
|
throw new Error("readiness 参数必须为空对象"); const status = (await health()).status; return { status: status === "ready" || status === "failed" ? status : "degraded" }; } }; }
|
|
11
|
+
function createQwenworkAbilities(health) {
|
|
12
|
+
return [createReadinessAbility(health), ...(0, generated_1.createGeneratedAbilities)()];
|
|
13
|
+
}
|
|
@@ -141,7 +141,8 @@ class QwenCdpClient {
|
|
|
141
141
|
const clicked = await this.evaluate(`(() => { if (document.querySelector(${JSON.stringify(exports.inputSelector)})?.innerText.trim() || document.querySelector('button[aria-label="停止"],button[aria-label="停止生成"],button[aria-label="Stop generating"]')) throw new Error('千问当前有未发送内容或正在生成回复,不能新建会话'); const button = document.querySelector('button[aria-label="新任务"]'); if (!button || button.disabled || button.getAttribute('aria-disabled') === 'true') return false; button.click(); return true; })()`);
|
|
142
142
|
if (clicked !== true)
|
|
143
143
|
throw new Error("千问新任务入口未找到或不可用");
|
|
144
|
-
|
|
144
|
+
// 千问通过内部状态切换新任务页,URL 不一定变化。
|
|
145
|
+
await this.waitFor(`(() => { const state = ${snapshot}; return state.input !== null && state.input.trim() === '' && state.messages === 0 && state.blank; })()`, "千问新会话状态校验失败");
|
|
145
146
|
return this.currentSessionId();
|
|
146
147
|
}
|
|
147
148
|
async currentSessionId() {
|
|
@@ -237,6 +238,8 @@ class QwenCdpClient {
|
|
|
237
238
|
throw new Error("千问输入框有用户草稿,跳过后台健康检测");
|
|
238
239
|
}
|
|
239
240
|
await this.ensureVisible();
|
|
241
|
+
// 冷启动的 CDP 可早于输入框就绪,只在真实请求中等待页面挂载。
|
|
242
|
+
await this.waitForInputReady();
|
|
240
243
|
if (!preserveDraft)
|
|
241
244
|
await this.recoverDraft();
|
|
242
245
|
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="停止生成"],button[aria-label="Stop generating"]')); })()`);
|
|
@@ -669,5 +672,15 @@ class QwenCdpClient {
|
|
|
669
672
|
}
|
|
670
673
|
throw new Error(message);
|
|
671
674
|
}
|
|
675
|
+
async waitForInputReady() {
|
|
676
|
+
const deadline = Date.now() + this.options.timeoutMs;
|
|
677
|
+
while (Date.now() < deadline) {
|
|
678
|
+
await this.restoreVisible();
|
|
679
|
+
if (await this.evaluate(`Boolean(document.querySelector(${JSON.stringify(exports.inputSelector)}))`))
|
|
680
|
+
return;
|
|
681
|
+
await wait(200);
|
|
682
|
+
}
|
|
683
|
+
throw new Error("千问办公等待输入框就绪超时(任务尚未提交)");
|
|
684
|
+
}
|
|
672
685
|
}
|
|
673
686
|
exports.QwenCdpClient = QwenCdpClient;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.QwenworkPlugin = exports.qwenworkManifest = void 0;
|
|
4
4
|
exports.createQwenworkPlugin = createQwenworkPlugin;
|
|
5
|
+
const agent_task_ability_1 = require("../agent-task-ability");
|
|
5
6
|
const promises_1 = require("node:fs/promises");
|
|
6
7
|
const node_path_1 = require("node:path");
|
|
7
8
|
const agent_1 = require("../../agent");
|
|
@@ -13,7 +14,7 @@ const session_history_1 = require("./session-history");
|
|
|
13
14
|
const ability_1 = require("./ability");
|
|
14
15
|
exports.qwenworkManifest = {
|
|
15
16
|
id: "qwenwork",
|
|
16
|
-
version: "1.0.
|
|
17
|
+
version: "1.0.37",
|
|
17
18
|
displayName: "千问办公",
|
|
18
19
|
entry: "dist/index.js",
|
|
19
20
|
apiVersion: "1",
|
|
@@ -31,7 +32,7 @@ function validModelSessionId(value) {
|
|
|
31
32
|
/** 千问插件由后台守护,所有通道共用请求队列和恢复流程。 */
|
|
32
33
|
class QwenworkPlugin {
|
|
33
34
|
manifest = exports.qwenworkManifest;
|
|
34
|
-
abilities = [(0, ability_1.
|
|
35
|
+
abilities = [...(0, ability_1.createQwenworkAbilities)(() => this.health()), (0, agent_task_ability_1.createTaskAbility)(request => this.handle(request))];
|
|
35
36
|
get modelProvider() {
|
|
36
37
|
if (this.relayTarget || !this.client?.complete || !this.client.createSession || !this.client.activateSession)
|
|
37
38
|
return undefined;
|
|
@@ -108,6 +109,7 @@ class QwenworkPlugin {
|
|
|
108
109
|
this.relayTarget = relayTarget === "dsh" ? relayTarget : undefined;
|
|
109
110
|
this.callPlugin = context.callPlugin;
|
|
110
111
|
this.pluginStatus = context.pluginStatus;
|
|
112
|
+
let startupError = "";
|
|
111
113
|
this.activeCdpPort = (0, plugin_1.resolveRuntimePort)(config, context.runtime, "qwenwork", "QWEN_CDP_PORT", this.configuredCdpPort ?? exports.qwenworkManifest.defaults.cdpPort);
|
|
112
114
|
this.sessionHistory = new session_history_1.QwenworkSessionHistoryProvider({
|
|
113
115
|
scope: `qwenwork:cdp:${this.activeCdpPort}`,
|
|
@@ -127,28 +129,26 @@ class QwenworkPlugin {
|
|
|
127
129
|
workspace: typeof config.workspace === "string" ? config.workspace : undefined,
|
|
128
130
|
});
|
|
129
131
|
this.client = client;
|
|
130
|
-
let executable;
|
|
131
132
|
try {
|
|
132
133
|
await client.connect();
|
|
133
134
|
}
|
|
134
135
|
catch (first) {
|
|
135
|
-
if (
|
|
136
|
+
if (!this.canStartDesktop())
|
|
136
137
|
throw first;
|
|
137
|
-
executable = await (0, desktop_launch_1.findWindowsExecutable)("QwenWorkCN.exe", typeof config.executablePath === "string" ? config.executablePath : undefined);
|
|
138
|
-
if (!executable)
|
|
139
|
-
throw new Error("未找到 QwenWorkCN.exe,请配置 executablePath");
|
|
140
|
-
context.logger?.info(`正在启动千问办公,等待 CDP ${this.activeCdpPort}`, { executablePath: executable });
|
|
141
|
-
this.activeCdpPort = await (0, desktop_launch_1.prepareDesktopPort)(executable, this.activeCdpPort);
|
|
142
|
-
client.setPort?.(this.activeCdpPort);
|
|
143
138
|
try {
|
|
144
|
-
await
|
|
139
|
+
await this.startDesktop(client);
|
|
145
140
|
}
|
|
146
141
|
catch (error) {
|
|
147
|
-
|
|
142
|
+
if (!(error instanceof agent_1.AgentOperationError) || error.code !== "QWEN_CDP_START_FAILED")
|
|
143
|
+
throw error;
|
|
144
|
+
// 保留插件实例,让后续消息仍可按需启动。
|
|
145
|
+
startupError = `千问办公启动未就绪,等待收到消息后重试:${error.message}`;
|
|
146
|
+
client.close();
|
|
147
|
+
context.logger?.error(startupError);
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
|
-
if (config.autoStart !== false && !this.injectedClient && process.platform === "win32") {
|
|
151
|
-
executable
|
|
150
|
+
if (!startupError && config.autoStart !== false && !this.injectedClient && process.platform === "win32") {
|
|
151
|
+
const executable = await (0, desktop_launch_1.findWindowsExecutable)("QwenWorkCN.exe", typeof config.executablePath === "string" ? config.executablePath : undefined);
|
|
152
152
|
if (executable)
|
|
153
153
|
await (0, desktop_launch_1.terminateWindowsDesktop)(executable, this.activeCdpPort);
|
|
154
154
|
}
|
|
@@ -166,8 +166,8 @@ class QwenworkPlugin {
|
|
|
166
166
|
if (this.configuredHttpPort > 0)
|
|
167
167
|
await this.adapter.listen(this.configuredHttpPort);
|
|
168
168
|
}
|
|
169
|
-
this.state = "ready";
|
|
170
|
-
this.message =
|
|
169
|
+
this.state = startupError ? "degraded" : "ready";
|
|
170
|
+
this.message = startupError;
|
|
171
171
|
this.startHealthWatch();
|
|
172
172
|
void this.checkHealth();
|
|
173
173
|
}
|
|
@@ -247,6 +247,7 @@ class QwenworkPlugin {
|
|
|
247
247
|
}
|
|
248
248
|
}
|
|
249
249
|
async completeRequest(request, modelRequest = false) {
|
|
250
|
+
request.signal?.throwIfAborted();
|
|
250
251
|
if (this.relayTarget) {
|
|
251
252
|
return this.callPlugin(this.relayTarget, request);
|
|
252
253
|
}
|
|
@@ -257,6 +258,12 @@ class QwenworkPlugin {
|
|
|
257
258
|
throw new Error("千问 CDP 已断开,模型请求不会启动或重启桌面");
|
|
258
259
|
if (!modelRequest)
|
|
259
260
|
await this.ensureConnected(request.signal);
|
|
261
|
+
if (request.newSession) {
|
|
262
|
+
if (!this.client.createSession)
|
|
263
|
+
throw new Error("千问当前传输不支持隔离任务会话");
|
|
264
|
+
await this.client.createSession();
|
|
265
|
+
}
|
|
266
|
+
request.signal?.throwIfAborted();
|
|
260
267
|
if (request.attachments?.length && (!this.client.attachmentInput || !this.client.complete))
|
|
261
268
|
throw new Error("千问当前客户端不支持附件输入");
|
|
262
269
|
const timeoutMs = request.timeoutMs ?? asNumber(this.context?.config.timeoutMs, 240_000);
|
|
@@ -364,7 +371,7 @@ class QwenworkPlugin {
|
|
|
364
371
|
const config = this.context?.config;
|
|
365
372
|
const executable = configuredExecutable ?? await (0, desktop_launch_1.findWindowsExecutable)("QwenWorkCN.exe", typeof config?.executablePath === "string" ? config.executablePath : undefined);
|
|
366
373
|
if (!executable)
|
|
367
|
-
throw new Error("未找到 QwenWorkCN.exe,请配置 executablePath");
|
|
374
|
+
throw new agent_1.AgentOperationError("DESKTOP_EXECUTABLE_NOT_FOUND", "启动桌面程序", new Error("未找到 QwenWorkCN.exe,请配置 executablePath"));
|
|
368
375
|
const processIds = await (0, desktop_launch_1.terminateWindowsDesktop)(executable);
|
|
369
376
|
this.context?.logger?.info("千问办公重载前已清理旧进程,将以 CDP 模式启动", { processIds, executable });
|
|
370
377
|
}
|
|
@@ -380,9 +387,11 @@ class QwenworkPlugin {
|
|
|
380
387
|
this.checking = true;
|
|
381
388
|
await this.enqueue(async () => {
|
|
382
389
|
try {
|
|
383
|
-
//
|
|
384
|
-
if (!this.stopped && !this.busy && !this.client?.connected())
|
|
385
|
-
|
|
390
|
+
// 用户关闭桌面后保持离线,只在收到消息时恢复连接或启动桌面。
|
|
391
|
+
if (!this.stopped && !this.busy && !this.client?.connected()) {
|
|
392
|
+
this.state = "degraded";
|
|
393
|
+
this.message = this.message || "千问 CDP 已断开,收到消息后将按需连接或启动桌面";
|
|
394
|
+
}
|
|
386
395
|
}
|
|
387
396
|
finally {
|
|
388
397
|
this.checking = false;
|
|
@@ -395,7 +404,7 @@ class QwenworkPlugin {
|
|
|
395
404
|
try {
|
|
396
405
|
await this.reconnect();
|
|
397
406
|
}
|
|
398
|
-
catch { /*
|
|
407
|
+
catch { /* 失败已记录,留待下次请求重试。 */ }
|
|
399
408
|
}
|
|
400
409
|
async ensureConnected(signal) {
|
|
401
410
|
signal?.throwIfAborted();
|
|
@@ -418,7 +427,24 @@ class QwenworkPlugin {
|
|
|
418
427
|
this.state = "starting";
|
|
419
428
|
this.context?.logger?.info("千问 CDP 自动重连开始", { reason: this.message });
|
|
420
429
|
client.close();
|
|
421
|
-
|
|
430
|
+
try {
|
|
431
|
+
await client.connect();
|
|
432
|
+
}
|
|
433
|
+
catch (first) {
|
|
434
|
+
if (this.stopped) {
|
|
435
|
+
client.close();
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
if (!this.canStartDesktop())
|
|
439
|
+
throw first;
|
|
440
|
+
this.context?.logger?.info("千问 CDP 重连失败,准备重启桌面", { error: first instanceof Error ? first.message : String(first) });
|
|
441
|
+
try {
|
|
442
|
+
await this.startDesktop(client);
|
|
443
|
+
}
|
|
444
|
+
catch (restartError) {
|
|
445
|
+
throw new Error(`${first instanceof Error ? first.message : String(first)};千问桌面重启失败:${restartError instanceof Error ? restartError.message : String(restartError)}`);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
422
448
|
await this.verifyConnection(client);
|
|
423
449
|
if (this.stopped) {
|
|
424
450
|
client.close();
|
|
@@ -433,12 +459,44 @@ class QwenworkPlugin {
|
|
|
433
459
|
if (!this.stopped) {
|
|
434
460
|
this.state = "failed";
|
|
435
461
|
this.message = error instanceof Error ? error.message : String(error);
|
|
436
|
-
this.context?.logger?.error(`千问 CDP
|
|
462
|
+
this.context?.logger?.error(`千问 CDP 重连失败,下次收到消息时重试:${this.message}`);
|
|
437
463
|
}
|
|
438
464
|
throw error;
|
|
439
465
|
}).finally(() => { this.recovery = undefined; });
|
|
440
466
|
return this.recovery;
|
|
441
467
|
}
|
|
468
|
+
canStartDesktop() {
|
|
469
|
+
return this.context?.config.autoStart !== false && Boolean(this.restartDesktopHook || (!this.injectedClient && process.platform === "win32"));
|
|
470
|
+
}
|
|
471
|
+
async startDesktop(client) {
|
|
472
|
+
if (this.restartDesktopHook) {
|
|
473
|
+
try {
|
|
474
|
+
await this.restartDesktopHook();
|
|
475
|
+
await client.connect();
|
|
476
|
+
}
|
|
477
|
+
catch (error) {
|
|
478
|
+
throw new agent_1.AgentOperationError("QWEN_CDP_START_FAILED", "启动千问办公 CDP", error);
|
|
479
|
+
}
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
const config = this.context?.config;
|
|
483
|
+
const executable = await (0, desktop_launch_1.findWindowsExecutable)("QwenWorkCN.exe", typeof config?.executablePath === "string" ? config.executablePath : undefined);
|
|
484
|
+
if (!executable)
|
|
485
|
+
throw new agent_1.AgentOperationError("DESKTOP_EXECUTABLE_NOT_FOUND", "启动桌面程序", new Error("未找到 QwenWorkCN.exe,请配置 executablePath"));
|
|
486
|
+
if (this.stopped)
|
|
487
|
+
return;
|
|
488
|
+
this.activeCdpPort = await (0, desktop_launch_1.prepareDesktopPort)(executable, this.activeCdpPort ?? exports.qwenworkManifest.defaults.cdpPort);
|
|
489
|
+
if (this.stopped)
|
|
490
|
+
return;
|
|
491
|
+
client.setPort?.(this.activeCdpPort);
|
|
492
|
+
this.context?.logger?.info(`正在启动千问办公,等待 CDP ${this.activeCdpPort}`, { executablePath: executable });
|
|
493
|
+
try {
|
|
494
|
+
await (0, desktop_launch_1.launchManagedAndWait)(executable, [`--remote-debugging-port=${this.activeCdpPort}`], undefined, () => client.connect(), typeof config?.startupTimeoutMs === "number" ? config.startupTimeoutMs : 30_000, () => { });
|
|
495
|
+
}
|
|
496
|
+
catch (error) {
|
|
497
|
+
throw new agent_1.AgentOperationError("QWEN_CDP_START_FAILED", "启动千问办公 CDP", error);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
442
500
|
async verifyConnection(client) {
|
|
443
501
|
if (!client.connected())
|
|
444
502
|
throw new Error("千问 CDP 连接恢复失败");
|
package/package.json
CHANGED