agent-bridge-qwenwork 1.0.2 → 1.0.4
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/config.schema.json
CHANGED
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
"subChatId": { "type": "string" },
|
|
9
9
|
"chatId": { "type": "string" },
|
|
10
10
|
"workspace": { "type": "string" },
|
|
11
|
+
"executablePath": { "type": "string", "description": "QwenWorkCN.exe 路径,省略时自动查找" },
|
|
12
|
+
"autoStart": { "type": "boolean", "default": true, "description": "CDP 未连接时自动启动千问办公" },
|
|
13
|
+
"startupTimeoutMs": { "type": "integer", "minimum": 1000, "default": 30000 },
|
|
11
14
|
"timeoutMs": { "type": "integer", "minimum": 1000, "default": 240000 },
|
|
12
15
|
"relayTarget": { "type": "string", "enum": ["dsh"], "description": "将文本原样中继到 DSH,不调用千问 CDP" }
|
|
13
16
|
},
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveRuntimePort = resolveRuntimePort;
|
|
4
|
+
function resolveRuntimePort(config, runtime, pluginId, envName, fallback) {
|
|
5
|
+
const injected = runtime?.ports?.[pluginId];
|
|
6
|
+
if (typeof injected === "number" && Number.isInteger(injected) && injected > 0)
|
|
7
|
+
return injected;
|
|
8
|
+
const configured = config.cdpPort;
|
|
9
|
+
if (typeof configured === "number" && Number.isInteger(configured) && configured > 0)
|
|
10
|
+
return configured;
|
|
11
|
+
const environment = Number(process.env[envName] ?? "");
|
|
12
|
+
return Number.isInteger(environment) && environment > 0 ? environment : fallback;
|
|
13
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.findWindowsExecutable = findWindowsExecutable;
|
|
4
|
+
exports.findExecutableInTree = findExecutableInTree;
|
|
5
|
+
exports.launchAndWait = launchAndWait;
|
|
6
|
+
exports.waitFor = waitFor;
|
|
7
|
+
const promises_1 = require("node:fs/promises");
|
|
8
|
+
const node_fs_1 = require("node:fs");
|
|
9
|
+
const node_path_1 = require("node:path");
|
|
10
|
+
/** 在 Windows 常见安装目录中查找桌面程序,避免依赖单一盘符。 */
|
|
11
|
+
async function findWindowsExecutable(fileName, configured) {
|
|
12
|
+
if (configured?.trim())
|
|
13
|
+
return (0, node_fs_1.existsSync)((0, node_path_1.resolve)(configured)) ? (0, node_path_1.resolve)(configured) : undefined;
|
|
14
|
+
if (process.platform !== "win32")
|
|
15
|
+
return undefined;
|
|
16
|
+
const pattern = /doubao|豆包|qwen|qwwork|千问/i;
|
|
17
|
+
const roots = [...new Set([
|
|
18
|
+
process.env.LOCALAPPDATA && (0, node_path_1.join)(process.env.LOCALAPPDATA, "Programs"),
|
|
19
|
+
process.env.ProgramFiles,
|
|
20
|
+
process.env["ProgramFiles(x86)"],
|
|
21
|
+
...Array.from({ length: 24 }, (_, index) => `${String.fromCharCode(67 + index)}:\\`),
|
|
22
|
+
].filter((value) => typeof value === "string" && (0, node_fs_1.existsSync)(value)))];
|
|
23
|
+
for (const root of roots) {
|
|
24
|
+
const found = await findExecutableInTree(root, fileName, pattern, 6);
|
|
25
|
+
if (found)
|
|
26
|
+
return found;
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
async function findExecutableInTree(directory, fileName, pattern, depth) {
|
|
31
|
+
return search(directory, fileName, pattern, depth, pattern.test(directory));
|
|
32
|
+
}
|
|
33
|
+
async function search(directory, fileName, pattern, depth, insideProduct) {
|
|
34
|
+
try {
|
|
35
|
+
const entries = await (0, promises_1.readdir)(directory, { withFileTypes: true });
|
|
36
|
+
for (const entry of entries)
|
|
37
|
+
if (entry.isFile() && insideProduct && entry.name.toLowerCase() === fileName.toLowerCase())
|
|
38
|
+
return (0, node_path_1.join)(directory, entry.name);
|
|
39
|
+
if (depth <= 0)
|
|
40
|
+
return undefined;
|
|
41
|
+
for (const entry of entries)
|
|
42
|
+
if (entry.isDirectory() && !entry.name.startsWith(".") && (insideProduct || pattern.test(entry.name) || /^(?:Program Files(?: \(x86\))?|Programs)$/i.test(entry.name))) {
|
|
43
|
+
const found = await search((0, node_path_1.join)(directory, entry.name), fileName, pattern, depth - 1, insideProduct || pattern.test(entry.name));
|
|
44
|
+
if (found)
|
|
45
|
+
return found;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch { /* 无权限或目录在扫描期间消失时继续查找 */ }
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
async function launchAndWait(executable, args, connect, timeoutMs, launch = (file, argv) => (0, node_child_process_1.spawn)(file, argv, { detached: true, stdio: "ignore", windowsHide: false })) {
|
|
52
|
+
const child = launch(executable, args);
|
|
53
|
+
child.unref();
|
|
54
|
+
return waitFor(connect, timeoutMs, 250);
|
|
55
|
+
}
|
|
56
|
+
async function waitFor(action, timeoutMs, intervalMs = 500) {
|
|
57
|
+
const deadline = Date.now() + timeoutMs;
|
|
58
|
+
let last;
|
|
59
|
+
while (Date.now() < deadline) {
|
|
60
|
+
try {
|
|
61
|
+
return await action();
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
last = error;
|
|
65
|
+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
throw last instanceof Error ? last : new Error("桌面程序启动超时");
|
|
69
|
+
}
|
|
70
|
+
const node_child_process_1 = require("node:child_process");
|
|
@@ -27,7 +27,7 @@ class QwenCdpClient {
|
|
|
27
27
|
options;
|
|
28
28
|
constructor(options = {}) {
|
|
29
29
|
this.options = {
|
|
30
|
-
port: options.port ?? Number(process.env.QWEN_CDP_PORT ?? "
|
|
30
|
+
port: options.port ?? Number(process.env.QWEN_CDP_PORT ?? "9228"),
|
|
31
31
|
subChatId: options.subChatId ?? process.env.QWEN_SUBCHAT ?? "muj55ep9426gudjw",
|
|
32
32
|
chatId: options.chatId ?? process.env.QWEN_CHAT ?? "muj55ep9u6wbv0eo",
|
|
33
33
|
workspace: options.workspace ?? `${process.env.APPDATA ?? ""}\\QwenWorkCN\\workspace`,
|
|
@@ -40,7 +40,14 @@ class QwenCdpClient {
|
|
|
40
40
|
if (this.socket)
|
|
41
41
|
return;
|
|
42
42
|
const fetchImpl = this.options.fetchImpl ?? fetch;
|
|
43
|
-
|
|
43
|
+
let response;
|
|
44
|
+
try {
|
|
45
|
+
response = await fetchImpl(`http://127.0.0.1:${this.options.port}/json`);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
const code = error?.cause?.code ?? String(error);
|
|
49
|
+
throw new Error(`无法连接千问办公 CDP 端口 ${this.options.port}(请先以 --remote-debugging-port=${this.options.port} 启动千问办公客户端): ${code}`);
|
|
50
|
+
}
|
|
44
51
|
if (!response.ok)
|
|
45
52
|
throw new Error(`CDP /json HTTP ${response.status}`);
|
|
46
53
|
const targets = await response.json();
|
|
@@ -4,16 +4,18 @@ exports.QwenworkPlugin = exports.qwenworkManifest = void 0;
|
|
|
4
4
|
exports.createQwenworkPlugin = createQwenworkPlugin;
|
|
5
5
|
const openai_adapter_1 = require("./openai-adapter");
|
|
6
6
|
const cdp_client_1 = require("./cdp-client");
|
|
7
|
+
const plugin_1 = require("../../plugin");
|
|
8
|
+
const desktop_launch_1 = require("../desktop-launch");
|
|
7
9
|
exports.qwenworkManifest = {
|
|
8
10
|
id: "qwenwork",
|
|
9
|
-
version: "1.0.
|
|
11
|
+
version: "1.0.4",
|
|
10
12
|
displayName: "千问办公",
|
|
11
13
|
entry: "dist/index.js",
|
|
12
14
|
apiVersion: "1",
|
|
13
15
|
process: "in-process",
|
|
14
16
|
capabilities: ["chat", "openai-compatible", "health"],
|
|
15
17
|
configSchema: "config.schema.json",
|
|
16
|
-
defaults: { cdpPort:
|
|
18
|
+
defaults: { cdpPort: 9228, httpPort: 17893 },
|
|
17
19
|
};
|
|
18
20
|
function asNumber(value, fallback) {
|
|
19
21
|
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
@@ -53,7 +55,7 @@ class QwenworkPlugin {
|
|
|
53
55
|
this.relayTarget = relayTarget === "dsh" ? relayTarget : undefined;
|
|
54
56
|
this.callPlugin = context.callPlugin;
|
|
55
57
|
this.pluginStatus = context.pluginStatus;
|
|
56
|
-
this.activeCdpPort =
|
|
58
|
+
this.activeCdpPort = (0, plugin_1.resolveRuntimePort)(config, context.runtime, "qwenwork", "QWEN_CDP_PORT", this.configuredCdpPort ?? exports.qwenworkManifest.defaults.cdpPort);
|
|
57
59
|
if (!this.relayTarget) {
|
|
58
60
|
const client = this.injectedClient ?? new cdp_client_1.QwenCdpClient({
|
|
59
61
|
port: this.activeCdpPort,
|
|
@@ -62,7 +64,23 @@ class QwenworkPlugin {
|
|
|
62
64
|
workspace: typeof config.workspace === "string" ? config.workspace : undefined,
|
|
63
65
|
});
|
|
64
66
|
this.client = client;
|
|
65
|
-
|
|
67
|
+
try {
|
|
68
|
+
await client.connect();
|
|
69
|
+
}
|
|
70
|
+
catch (first) {
|
|
71
|
+
if (config.autoStart === false || this.injectedClient || process.platform !== "win32")
|
|
72
|
+
throw first;
|
|
73
|
+
const executable = await (0, desktop_launch_1.findWindowsExecutable)("QwenWorkCN.exe", typeof config.executablePath === "string" ? config.executablePath : undefined);
|
|
74
|
+
if (!executable)
|
|
75
|
+
throw new Error("未找到 QwenWorkCN.exe,请配置 executablePath");
|
|
76
|
+
context.logger?.info(`千问办公已启动,等待 CDP ${this.activeCdpPort}`);
|
|
77
|
+
try {
|
|
78
|
+
await (0, desktop_launch_1.launchAndWait)(executable, [`--remote-debugging-port=${this.activeCdpPort}`], () => client.connect(), typeof config.startupTimeoutMs === "number" ? config.startupTimeoutMs : 30_000);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
throw new Error(`已启动千问办公,但 CDP ${this.activeCdpPort} 未就绪:${error instanceof Error ? error.message : String(error)}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
66
84
|
}
|
|
67
85
|
this.adapter = new openai_adapter_1.OpenAIAdapter((prompt, signal) => this.enqueue(() => this.complete(prompt, asNumber(config.timeoutMs, 240_000), signal)), {
|
|
68
86
|
onLog: (message) => context.logger?.info(message),
|
package/package.json
CHANGED
package/plugin.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "qwenwork",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"type": "agent",
|
|
5
5
|
"displayName": "千问办公",
|
|
6
6
|
"entry": "dist/src-ts/plugins/qwenwork/index.js",
|
|
@@ -11,5 +11,6 @@
|
|
|
11
11
|
"openai-compatible",
|
|
12
12
|
"health"
|
|
13
13
|
],
|
|
14
|
-
"configSchema": "config.schema.json"
|
|
14
|
+
"configSchema": "config.schema.json",
|
|
15
|
+
"clientDownloadUrl": "https://tongyi.aliyun.com/"
|
|
15
16
|
}
|