dsh-plugin-lark-bridge 0.1.2 → 0.1.3
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 +44 -22
- package/cordis.patch.yml +2 -8
- package/dist/auth.d.ts +12 -38
- package/dist/auth.d.ts.map +1 -1
- package/dist/bridge.d.ts +6 -4
- package/dist/bridge.d.ts.map +1 -1
- package/dist/cli.d.ts +17 -21
- package/dist/cli.d.ts.map +1 -1
- package/dist/client/api.d.ts +9 -33
- package/dist/client/api.d.ts.map +1 -1
- package/dist/client/auth-overlay.d.ts +54 -6
- package/dist/client/auth-overlay.d.ts.map +1 -1
- package/dist/client/auth-panel.d.ts +17 -5
- package/dist/client/auth-panel.d.ts.map +1 -1
- package/dist/client/config-form.d.ts +2 -11
- package/dist/client/config-form.d.ts.map +1 -1
- package/dist/client/config-page.d.ts +32 -6
- package/dist/client/config-page.d.ts.map +1 -1
- package/dist/client/index.d.ts +2 -8
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/locales.d.ts +23 -41
- package/dist/client/locales.d.ts.map +1 -1
- package/dist/client/status-controller.d.ts +43 -0
- package/dist/client/status-controller.d.ts.map +1 -0
- package/dist/client/terminal-command.d.ts +11 -0
- package/dist/client/terminal-command.d.ts.map +1 -0
- package/dist/client.js +776 -651
- package/dist/config.d.ts +6 -22
- package/dist/config.d.ts.map +1 -1
- package/dist/constants.d.ts +2 -4
- package/dist/constants.d.ts.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +415 -453
- package/dist/message.d.ts +27 -0
- package/dist/message.d.ts.map +1 -0
- package/dist/reminder.d.ts +33 -0
- package/dist/reminder.d.ts.map +1 -0
- package/dist/routes.d.ts +5 -15
- package/dist/routes.d.ts.map +1 -1
- package/dist/sessions.d.ts +12 -13
- package/dist/sessions.d.ts.map +1 -1
- package/dist/state.d.ts +0 -2
- package/dist/state.d.ts.map +1 -1
- package/dist/tools.d.ts.map +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +3 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/workspace-path.d.ts +8 -0
- package/dist/workspace-path.d.ts.map +1 -0
- package/dist/workspace.d.ts +32 -0
- package/dist/workspace.d.ts.map +1 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +6 -5
package/dist/index.mjs
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import { createRequire } from "node:module";
|
|
2
1
|
import { spawn } from "node:child_process";
|
|
3
|
-
import { existsSync } from "node:fs";
|
|
4
|
-
import { dirname, join, resolve } from "node:path";
|
|
5
2
|
import { createInterface } from "node:readline";
|
|
6
3
|
import { homedir } from "node:os";
|
|
4
|
+
import { dirname, join, resolve } from "node:path";
|
|
7
5
|
import Schema from "@deepseek-ai/schemastery";
|
|
6
|
+
import { createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
8
7
|
import { randomUUID } from "node:crypto";
|
|
9
8
|
import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
10
|
-
import { createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
11
9
|
import { SessionId } from "@deepseek-ai/dsh-session";
|
|
12
10
|
//#region src/utils.ts
|
|
13
11
|
/** 零依赖工具函数。 */
|
|
@@ -15,14 +13,24 @@ import { SessionId } from "@deepseek-ai/dsh-session";
|
|
|
15
13
|
function isRecord(value) {
|
|
16
14
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
17
15
|
}
|
|
18
|
-
/** 把毫秒时间戳或 ISO
|
|
16
|
+
/** 把毫秒时间戳或 ISO 文本转成 `YYYY-MM-DD HH:mm:ss`;非法值返回空串。 */
|
|
19
17
|
function formatTime(value, timeZone = "Asia/Shanghai") {
|
|
20
18
|
const date = typeof value === "number" ? new Date(value) : new Date(String(value ?? ""));
|
|
21
19
|
if (Number.isNaN(date.getTime())) return "";
|
|
22
|
-
return
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
return new Intl.DateTimeFormat("sv-SE", {
|
|
21
|
+
timeZone,
|
|
22
|
+
year: "numeric",
|
|
23
|
+
month: "2-digit",
|
|
24
|
+
day: "2-digit",
|
|
25
|
+
hour: "2-digit",
|
|
26
|
+
minute: "2-digit",
|
|
27
|
+
second: "2-digit",
|
|
28
|
+
hourCycle: "h23"
|
|
29
|
+
}).format(date);
|
|
30
|
+
}
|
|
31
|
+
/** 转义属性值中的 `&`、`"` 与 `<`。 */
|
|
32
|
+
function escapeAttribute(value) {
|
|
33
|
+
return value.replaceAll("&", "&").replaceAll("\"", """).replaceAll("<", "<");
|
|
26
34
|
}
|
|
27
35
|
/** 解析毫秒时间戳字符串;非法值返回当前时间。 */
|
|
28
36
|
function parseTimestamp(value) {
|
|
@@ -57,14 +65,6 @@ function splitText(text, limit) {
|
|
|
57
65
|
parts.push(rest);
|
|
58
66
|
return parts;
|
|
59
67
|
}
|
|
60
|
-
/** 规范化路径用于比较;Windows 忽略大小写与斜杠差异。 */
|
|
61
|
-
function samePath(a, b) {
|
|
62
|
-
const normalize = (value) => {
|
|
63
|
-
const slashed = value.replaceAll("\\", "/").replace(/\/+$/, "");
|
|
64
|
-
return process.platform === "win32" ? slashed.toLowerCase() : slashed;
|
|
65
|
-
};
|
|
66
|
-
return normalize(a) === normalize(b);
|
|
67
|
-
}
|
|
68
68
|
/** 压缩并截断一段用于日志的错误文本。 */
|
|
69
69
|
function describeError(error, max = 500) {
|
|
70
70
|
const text = error instanceof Error ? error.message : String(error);
|
|
@@ -73,8 +73,8 @@ function describeError(error, max = 500) {
|
|
|
73
73
|
//#endregion
|
|
74
74
|
//#region src/cli.ts
|
|
75
75
|
/**
|
|
76
|
-
* `lark-cli`
|
|
77
|
-
* 导出 LarkCli、CliError
|
|
76
|
+
* `lark-cli` 适配层:调用本机命令、管理子进程、解析 JSON envelope。
|
|
77
|
+
* 导出 LarkCli、CliError 与进程构造辅助函数。
|
|
78
78
|
*/
|
|
79
79
|
/** CLI 调用失败。 */
|
|
80
80
|
var CliError = class extends Error {
|
|
@@ -89,88 +89,45 @@ var CliError = class extends Error {
|
|
|
89
89
|
this.exitCode = exitCode;
|
|
90
90
|
}
|
|
91
91
|
};
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
92
|
+
/** Windows 命令字符串只允许不需要转义的安全 token。 */
|
|
93
|
+
const WINDOWS_SAFE_TOKEN = /^[A-Za-z0-9._:+-]+$/;
|
|
94
|
+
/** 构造进程参数;Windows 用安全 token 组成单一命令字符串。 */
|
|
95
|
+
function createSpawnSpec(command, args, platform = process.platform) {
|
|
96
|
+
const tokens = [
|
|
97
|
+
command.file,
|
|
98
|
+
...command.args,
|
|
99
|
+
...args
|
|
100
|
+
];
|
|
101
|
+
if (platform !== "win32") return {
|
|
102
|
+
file: command.file,
|
|
103
|
+
args: [...command.args, ...args],
|
|
104
|
+
shell: false
|
|
105
105
|
};
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
file:
|
|
109
|
-
args: [
|
|
106
|
+
for (const token of tokens) if (token === "" || !WINDOWS_SAFE_TOKEN.test(token)) throw new CliError("unsafe-arg", "lark-cli argument contains unsupported Windows characters");
|
|
107
|
+
return {
|
|
108
|
+
file: tokens.join(" "),
|
|
109
|
+
args: [],
|
|
110
|
+
shell: true
|
|
110
111
|
};
|
|
111
112
|
}
|
|
112
|
-
/**
|
|
113
|
-
function
|
|
113
|
+
/** 启动 CLI 子进程,并把同步启动异常归一化为 CliError。 */
|
|
114
|
+
function createCliProcess(command, args, options, platform = process.platform, spawnFn = spawn) {
|
|
115
|
+
const spec = createSpawnSpec(command, args, platform);
|
|
114
116
|
try {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
* @param explicit - 配置中的覆盖路径;空串表示未覆盖。
|
|
125
|
-
* @param env - 环境变量;默认 `process.env`。
|
|
126
|
-
* @param nodePath - 运行入口脚本时使用的 Node/Electron 路径。
|
|
127
|
-
* @returns 可执行的命令与固定参数。
|
|
128
|
-
*/
|
|
129
|
-
function resolveLarkCli(explicit = "", env = process.env, nodePath = process.execPath) {
|
|
130
|
-
const trimmed = explicit.trim();
|
|
131
|
-
if (trimmed !== "") {
|
|
132
|
-
if (/\.(c|m)?js$/i.test(trimmed)) return {
|
|
133
|
-
file: nodePath,
|
|
134
|
-
args: [trimmed]
|
|
135
|
-
};
|
|
136
|
-
return {
|
|
137
|
-
file: trimmed,
|
|
138
|
-
args: []
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
const fromEnv = String(env["LARK_CLI"] ?? "").trim();
|
|
142
|
-
if (fromEnv !== "") {
|
|
143
|
-
if (/\.(c|m)?js$/i.test(fromEnv)) return {
|
|
144
|
-
file: nodePath,
|
|
145
|
-
args: [fromEnv]
|
|
146
|
-
};
|
|
147
|
-
return {
|
|
148
|
-
file: fromEnv,
|
|
149
|
-
args: []
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
const packaged = resolvePackage(nodePath);
|
|
153
|
-
if (packaged !== void 0) return packaged;
|
|
154
|
-
const directories = String(env["PATH"] ?? env["Path"] ?? "").split(WINDOWS ? ";" : ":").filter((item) => item !== "");
|
|
155
|
-
for (const directory of directories) {
|
|
156
|
-
const local = fromPackageDir(join(directory, "node_modules", "@larksuite", "cli"), nodePath);
|
|
157
|
-
if (local !== void 0) return local;
|
|
158
|
-
const direct = join(directory, WINDOWS ? "lark-cli.exe" : "lark-cli");
|
|
159
|
-
if (hasFile(direct)) return {
|
|
160
|
-
file: direct,
|
|
161
|
-
args: []
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
const appData = String(env["APPDATA"] ?? "").trim();
|
|
165
|
-
if (appData !== "") {
|
|
166
|
-
const global = fromPackageDir(join(appData, "npm", "node_modules", "@larksuite", "cli"), nodePath);
|
|
167
|
-
if (global !== void 0) return global;
|
|
117
|
+
return spec.shell ? spawnFn(spec.file, {
|
|
118
|
+
...options,
|
|
119
|
+
shell: true
|
|
120
|
+
}) : spawnFn(spec.file, [...spec.args], {
|
|
121
|
+
...options,
|
|
122
|
+
shell: false
|
|
123
|
+
});
|
|
124
|
+
} catch (error) {
|
|
125
|
+
throw new CliError("spawn-failed", describeError(error));
|
|
168
126
|
}
|
|
169
|
-
return {
|
|
170
|
-
file: "lark-cli",
|
|
171
|
-
args: []
|
|
172
|
-
};
|
|
173
127
|
}
|
|
128
|
+
const WINDOWS = process.platform === "win32";
|
|
129
|
+
const TIMEOUT_DEFAULT = 12e4;
|
|
130
|
+
const VIEWPORT = 10485760;
|
|
174
131
|
/** 杀掉子进程及其后代。 */
|
|
175
132
|
function killChildTree(child) {
|
|
176
133
|
if (child.pid === void 0) {
|
|
@@ -194,9 +151,9 @@ function killChildTree(child) {
|
|
|
194
151
|
}
|
|
195
152
|
child.kill("SIGTERM");
|
|
196
153
|
}
|
|
197
|
-
/**
|
|
154
|
+
/** 默认运行器:按平台安全调用本机命令。 */
|
|
198
155
|
const defaultRunner = (command, args, options) => new Promise((resolvePromise, reject) => {
|
|
199
|
-
const child =
|
|
156
|
+
const child = createCliProcess(command, args, {
|
|
200
157
|
windowsHide: true,
|
|
201
158
|
stdio: [
|
|
202
159
|
"pipe",
|
|
@@ -273,30 +230,19 @@ function parseJsonOutput(text) {
|
|
|
273
230
|
}
|
|
274
231
|
/** 面向插件的 CLI 封装。 */
|
|
275
232
|
var LarkCli = class {
|
|
276
|
-
/**
|
|
277
|
-
command
|
|
233
|
+
/** 固定的本机命令。 */
|
|
234
|
+
command = {
|
|
235
|
+
file: "lark-cli",
|
|
236
|
+
args: []
|
|
237
|
+
};
|
|
278
238
|
runner;
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
* @param explicit - 配置覆盖的可执行文件路径。
|
|
282
|
-
* @param runner - 运行器;测试注入。
|
|
283
|
-
* @param nodePath - 运行入口脚本的 Node 路径。
|
|
284
|
-
*/
|
|
285
|
-
constructor(explicit = "", runner = defaultRunner, nodePath = process.execPath) {
|
|
286
|
-
this.nodePath = nodePath;
|
|
287
|
-
this.command = resolveLarkCli(explicit, process.env, nodePath);
|
|
239
|
+
/** @param runner - 运行器;测试注入。 */
|
|
240
|
+
constructor(runner = defaultRunner) {
|
|
288
241
|
this.runner = runner;
|
|
289
242
|
}
|
|
290
243
|
/** 运行一次调用;非零退出码转为 CliError。 */
|
|
291
244
|
async run(args, options = {}) {
|
|
292
|
-
const
|
|
293
|
-
...options.env ?? {},
|
|
294
|
-
ELECTRON_RUN_AS_NODE: "1"
|
|
295
|
-
} : options.env ?? {};
|
|
296
|
-
const result = await this.runner(this.command, args, {
|
|
297
|
-
...options,
|
|
298
|
-
env
|
|
299
|
-
});
|
|
245
|
+
const result = await this.runner(this.command, args, options);
|
|
300
246
|
if (result.code !== 0 && options.allowFailure !== true) {
|
|
301
247
|
const detail = result.stderr.trim() !== "" ? result.stderr.trim() : result.stdout.trim() !== "" ? result.stdout.trim() : `exit ${result.code}`;
|
|
302
248
|
throw new CliError("exit", `lark-cli ${args.join(" ")} failed: ${detail.slice(0, 800)}`, result.code);
|
|
@@ -313,63 +259,27 @@ var LarkCli = class {
|
|
|
313
259
|
* @returns 子进程与 stdout/stderr 读取器。
|
|
314
260
|
*/
|
|
315
261
|
spawn(args) {
|
|
316
|
-
|
|
317
|
-
...process.env,
|
|
318
|
-
ELECTRON_RUN_AS_NODE: "1"
|
|
319
|
-
} : process.env;
|
|
320
|
-
return spawn(this.command.file, [...this.command.args, ...args], {
|
|
262
|
+
return createCliProcess(this.command, args, {
|
|
321
263
|
windowsHide: true,
|
|
322
264
|
stdio: [
|
|
323
265
|
"pipe",
|
|
324
266
|
"pipe",
|
|
325
267
|
"pipe"
|
|
326
268
|
],
|
|
327
|
-
env
|
|
269
|
+
env: process.env
|
|
328
270
|
});
|
|
329
271
|
}
|
|
330
272
|
};
|
|
331
273
|
//#endregion
|
|
332
|
-
//#region src/constants.ts
|
|
333
|
-
/** 共享常量:插件标识、默认值、工具名与状态目录。 */
|
|
334
|
-
/** 插件标识,与包名、cordis.patch.yml 的 id 一致。 */
|
|
335
|
-
const PLUGIN_LABEL = "dsh-plugin-lark-bridge";
|
|
336
|
-
/** 工具名。 */
|
|
337
|
-
const NOTIFY_TOOL_NAME = "lark_notify";
|
|
338
|
-
/** 桥接会话的默认标题。 */
|
|
339
|
-
const DEFAULT_SESSION_TITLE = "飞书";
|
|
340
|
-
/** 状态目录名,位于 DSH home 下。 */
|
|
341
|
-
const STATE_DIR = "lark-bridge";
|
|
342
|
-
/** 状态文件名。 */
|
|
343
|
-
const STATE_FILE = "state.json";
|
|
344
|
-
/** 单条飞书文本消息的最大字符数。 */
|
|
345
|
-
const MAX_MESSAGE_CHARS = 15e3;
|
|
346
|
-
/** 监听重启的初始与最大退避。 */
|
|
347
|
-
const RESTART_MIN_MS = 2e3;
|
|
348
|
-
const RESTART_MAX_MS = 6e4;
|
|
349
|
-
//#endregion
|
|
350
274
|
//#region src/auth.ts
|
|
351
275
|
/**
|
|
352
|
-
* lark-cli
|
|
353
|
-
* 导出
|
|
276
|
+
* lark-cli 连接状态机:独立探测 CLI、bot 身份与用户身份。
|
|
277
|
+
* 导出 AuthSnapshot 与 AuthManager。
|
|
354
278
|
*/
|
|
355
279
|
/** 把未知值收窄为对象。 */
|
|
356
280
|
function recordOf(value) {
|
|
357
281
|
return typeof value === "object" && value !== null ? value : {};
|
|
358
282
|
}
|
|
359
|
-
/** 读取设备码输出。 */
|
|
360
|
-
function parseLoginFlow(value) {
|
|
361
|
-
const record = recordOf(value);
|
|
362
|
-
const url = record["verification_url"];
|
|
363
|
-
const code = record["device_code"];
|
|
364
|
-
const expires = Number(record["expires_in"]);
|
|
365
|
-
if (typeof url !== "string" || url === "") throw new CliError("invalid-login", "lark-cli did not return a verification URL");
|
|
366
|
-
if (typeof code !== "string" || code === "") throw new CliError("invalid-login", "lark-cli did not return a device code");
|
|
367
|
-
return {
|
|
368
|
-
verificationUrl: url,
|
|
369
|
-
deviceCode: code,
|
|
370
|
-
expiresAt: Date.now() + (Number.isFinite(expires) && expires > 0 ? expires : 600) * 1e3
|
|
371
|
-
};
|
|
372
|
-
}
|
|
373
283
|
/** 读取 auth status 的身份信息。 */
|
|
374
284
|
function statusIdentities(value) {
|
|
375
285
|
const root = recordOf(value);
|
|
@@ -380,7 +290,6 @@ function statusIdentities(value) {
|
|
|
380
290
|
appId: typeof root["appId"] === "string" ? root["appId"] : void 0,
|
|
381
291
|
bot: typeof bot["status"] === "string" ? bot["status"] : void 0,
|
|
382
292
|
user: {
|
|
383
|
-
status: typeof user["status"] === "string" ? user["status"] : void 0,
|
|
384
293
|
openId: typeof user["openId"] === "string" ? user["openId"] : void 0,
|
|
385
294
|
name: typeof user["userName"] === "string" ? user["userName"] : void 0
|
|
386
295
|
}
|
|
@@ -399,55 +308,49 @@ async function probeCli(cli) {
|
|
|
399
308
|
return true;
|
|
400
309
|
}
|
|
401
310
|
}
|
|
402
|
-
/**
|
|
311
|
+
/** 构造检测完成后的基础快照。 */
|
|
312
|
+
function disconnected(checkedAt) {
|
|
313
|
+
return {
|
|
314
|
+
cli: "disconnected",
|
|
315
|
+
bot: "disconnected",
|
|
316
|
+
user: "disconnected",
|
|
317
|
+
ready: false,
|
|
318
|
+
checkedAt,
|
|
319
|
+
appId: void 0,
|
|
320
|
+
userName: void 0,
|
|
321
|
+
ownerOpenId: void 0,
|
|
322
|
+
detail: void 0
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
/** 完整检测与状态发布。 */
|
|
403
326
|
var AuthManager = class {
|
|
404
327
|
deps;
|
|
405
328
|
snapshot;
|
|
406
329
|
checking;
|
|
407
|
-
flow;
|
|
408
|
-
completing;
|
|
409
|
-
cancelled = 0;
|
|
410
330
|
constructor(deps) {
|
|
411
331
|
this.deps = deps;
|
|
412
332
|
this.snapshot = {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
userName: void 0,
|
|
418
|
-
ownerOpenId: void 0,
|
|
419
|
-
checkedAt: 0,
|
|
420
|
-
detail: void 0
|
|
333
|
+
...disconnected(0),
|
|
334
|
+
cli: "checking",
|
|
335
|
+
bot: "checking",
|
|
336
|
+
user: "checking"
|
|
421
337
|
};
|
|
422
338
|
}
|
|
423
339
|
/** 最近一次检测结果。 */
|
|
424
340
|
get current() {
|
|
425
341
|
return this.snapshot;
|
|
426
342
|
}
|
|
427
|
-
/**
|
|
428
|
-
|
|
429
|
-
return this.flow;
|
|
430
|
-
}
|
|
431
|
-
/** 重新检测;并发调用共享同一次检查。 */
|
|
432
|
-
check(options = {}) {
|
|
343
|
+
/** 完整检测;并发调用共享同一次检查。 */
|
|
344
|
+
check() {
|
|
433
345
|
if (this.checking !== void 0) return this.checking;
|
|
434
|
-
this.checking = this.runCheck(
|
|
346
|
+
this.checking = this.runCheck().finally(() => {
|
|
435
347
|
this.checking = void 0;
|
|
436
348
|
});
|
|
437
349
|
return this.checking;
|
|
438
350
|
}
|
|
439
|
-
async runCheck(
|
|
351
|
+
async runCheck() {
|
|
440
352
|
const cli = this.deps.getCli();
|
|
441
|
-
const base =
|
|
442
|
-
stage: "cli-missing",
|
|
443
|
-
ready: false,
|
|
444
|
-
appId: void 0,
|
|
445
|
-
botStatus: void 0,
|
|
446
|
-
userName: void 0,
|
|
447
|
-
ownerOpenId: void 0,
|
|
448
|
-
checkedAt: Date.now(),
|
|
449
|
-
detail: void 0
|
|
450
|
-
};
|
|
353
|
+
const base = disconnected(Date.now());
|
|
451
354
|
let next;
|
|
452
355
|
if (!await probeCli(cli)) next = base;
|
|
453
356
|
else try {
|
|
@@ -455,93 +358,32 @@ var AuthManager = class {
|
|
|
455
358
|
"auth",
|
|
456
359
|
"status",
|
|
457
360
|
"--json",
|
|
458
|
-
|
|
361
|
+
"--verify"
|
|
459
362
|
], { timeoutMs: 6e4 }));
|
|
460
363
|
const owner = identities.user.openId;
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
};
|
|
466
|
-
else if (owner === void 0 || owner === "") next = {
|
|
467
|
-
...base,
|
|
468
|
-
stage: "user-missing",
|
|
469
|
-
appId: identities.appId,
|
|
470
|
-
botStatus: identities.bot,
|
|
471
|
-
userName: identities.user.name
|
|
472
|
-
};
|
|
473
|
-
else next = {
|
|
364
|
+
const botReady = identities.bot === "ready";
|
|
365
|
+
const userReady = owner !== void 0 && owner !== "";
|
|
366
|
+
const ready = botReady && userReady;
|
|
367
|
+
next = {
|
|
474
368
|
...base,
|
|
475
|
-
|
|
476
|
-
|
|
369
|
+
cli: "connected",
|
|
370
|
+
bot: botReady ? "connected" : "disconnected",
|
|
371
|
+
user: userReady ? "connected" : "disconnected",
|
|
372
|
+
ready,
|
|
477
373
|
appId: identities.appId,
|
|
478
|
-
botStatus: identities.bot,
|
|
479
374
|
userName: identities.user.name,
|
|
480
|
-
ownerOpenId: owner
|
|
375
|
+
ownerOpenId: ready ? owner : void 0
|
|
481
376
|
};
|
|
482
377
|
} catch (error) {
|
|
483
378
|
next = {
|
|
484
379
|
...base,
|
|
485
|
-
|
|
380
|
+
cli: "connected",
|
|
486
381
|
detail: describeError(error)
|
|
487
382
|
};
|
|
488
383
|
}
|
|
489
384
|
this.publish(next);
|
|
490
385
|
return next;
|
|
491
386
|
}
|
|
492
|
-
/**
|
|
493
|
-
* 启动设备码授权。
|
|
494
|
-
* @returns 授权链接与设备码;完成后台轮询。
|
|
495
|
-
*/
|
|
496
|
-
async startLogin() {
|
|
497
|
-
const cli = this.deps.getCli();
|
|
498
|
-
const scope = this.deps.loginScope ?? "auth:user.id:read";
|
|
499
|
-
const flow = parseLoginFlow(await cli.json([
|
|
500
|
-
"auth",
|
|
501
|
-
"login",
|
|
502
|
-
"--no-wait",
|
|
503
|
-
"--json",
|
|
504
|
-
"--scope",
|
|
505
|
-
scope
|
|
506
|
-
], { timeoutMs: 6e4 }));
|
|
507
|
-
this.flow = flow;
|
|
508
|
-
this.cancelled += 1;
|
|
509
|
-
const generation = this.cancelled;
|
|
510
|
-
this.deps.logger.info(`lark-bridge: device login started, expires at ${new Date(flow.expiresAt).toISOString()}`);
|
|
511
|
-
this.completing = this.completeLogin(flow, generation);
|
|
512
|
-
return flow;
|
|
513
|
-
}
|
|
514
|
-
/** 后台完成设备码授权;失败时记录并刷新状态。 */
|
|
515
|
-
async completeLogin(flow, generation) {
|
|
516
|
-
const cli = this.deps.getCli();
|
|
517
|
-
const remaining = Math.max(3e4, flow.expiresAt - Date.now() + 15e3);
|
|
518
|
-
try {
|
|
519
|
-
await cli.run([
|
|
520
|
-
"auth",
|
|
521
|
-
"login",
|
|
522
|
-
"--device-code",
|
|
523
|
-
flow.deviceCode,
|
|
524
|
-
"--json"
|
|
525
|
-
], { timeoutMs: remaining });
|
|
526
|
-
if (generation !== this.cancelled) return this.snapshot;
|
|
527
|
-
this.flow = void 0;
|
|
528
|
-
return await this.check({ verify: true });
|
|
529
|
-
} catch (error) {
|
|
530
|
-
if (generation !== this.cancelled) return this.snapshot;
|
|
531
|
-
this.flow = void 0;
|
|
532
|
-
this.deps.logger.warn(`lark-bridge: device login failed: ${describeError(error)}`);
|
|
533
|
-
return this.check();
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
/** 等待后台授权结束;测试与关闭流程使用。 */
|
|
537
|
-
async whenLoginSettled() {
|
|
538
|
-
return this.completing === void 0 ? void 0 : this.completing;
|
|
539
|
-
}
|
|
540
|
-
/** 取消当前授权;后台轮询的结果会被丢弃。 */
|
|
541
|
-
cancelLogin() {
|
|
542
|
-
this.cancelled += 1;
|
|
543
|
-
this.flow = void 0;
|
|
544
|
-
}
|
|
545
387
|
/** 更新快照并通知订阅者。 */
|
|
546
388
|
publish(next) {
|
|
547
389
|
this.snapshot = next;
|
|
@@ -549,22 +391,39 @@ var AuthManager = class {
|
|
|
549
391
|
}
|
|
550
392
|
};
|
|
551
393
|
//#endregion
|
|
394
|
+
//#region src/constants.ts
|
|
395
|
+
/** 共享常量:插件标识、默认值、工具名与状态目录。 */
|
|
396
|
+
/** 插件标识,与包名、cordis.patch.yml 的 id 一致。 */
|
|
397
|
+
const PLUGIN_LABEL = "dsh-plugin-lark-bridge";
|
|
398
|
+
/** 工具名。 */
|
|
399
|
+
const NOTIFY_TOOL_NAME = "lark_notify";
|
|
400
|
+
/** 桥接会话的固定标题。 */
|
|
401
|
+
const SESSION_TITLE = "来自用户的飞书消息";
|
|
402
|
+
/** 状态目录名,位于 DSH home 下。 */
|
|
403
|
+
const STATE_DIR = "lark-bridge";
|
|
404
|
+
/** 状态文件名。 */
|
|
405
|
+
const STATE_FILE = "state.json";
|
|
406
|
+
/** 单条飞书文本消息的最大字符数。 */
|
|
407
|
+
const MAX_MESSAGE_CHARS = 15e3;
|
|
408
|
+
/** 监听重启的初始与最大退避。 */
|
|
409
|
+
const RESTART_MIN_MS = 2e3;
|
|
410
|
+
const RESTART_MAX_MS = 6e4;
|
|
411
|
+
//#endregion
|
|
552
412
|
//#region src/bridge.ts
|
|
553
413
|
/**
|
|
554
414
|
* 飞书事件桥:消费 bot 身份事件、过滤与去重,并把消息注入桥接会话。
|
|
555
|
-
* 导出 EventBridge 与
|
|
415
|
+
* 导出 EventBridge 与 formatLarkMessage。
|
|
556
416
|
*/
|
|
557
|
-
/**
|
|
558
|
-
function
|
|
417
|
+
/** 把一条飞书消息渲染为 `<lark-message>` 块。 */
|
|
418
|
+
function formatLarkMessage(event) {
|
|
559
419
|
const time = formatTime(parseTimestamp(event.timestamp ?? event.create_time));
|
|
560
420
|
const text = typeof event.content === "string" ? event.content.trim() : "";
|
|
561
421
|
const type = String(event.message_type ?? event.msg_type ?? "text");
|
|
562
422
|
const body = text === "" ? `[${type} 消息]` : text;
|
|
563
|
-
const
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
return `${header.join("\n")}\n\n${body}`;
|
|
423
|
+
const attributes = [`message_id="${escapeAttribute(String(event.message_id ?? event.id ?? ""))}"`];
|
|
424
|
+
if (time !== "") attributes.push(`time="${time}"`);
|
|
425
|
+
const safe = body.replaceAll("</lark-message>", "<\\/lark-message>");
|
|
426
|
+
return `<lark-message ${attributes.join(" ")}>\n${safe}\n</lark-message>`;
|
|
568
427
|
}
|
|
569
428
|
/** 返回事件的消息标识。 */
|
|
570
429
|
function messageId(event) {
|
|
@@ -591,9 +450,9 @@ var EventBridge = class {
|
|
|
591
450
|
get running() {
|
|
592
451
|
return this.child !== void 0;
|
|
593
452
|
}
|
|
594
|
-
/**
|
|
453
|
+
/** 鉴权与工作区均有效时启动;否则保持停止。 */
|
|
595
454
|
start() {
|
|
596
|
-
if (this.deps.getAuth().ready !== true) return;
|
|
455
|
+
if (this.deps.getAuth().ready !== true || !this.deps.getWorkspaceReady()) return;
|
|
597
456
|
this.stopped = false;
|
|
598
457
|
this.spawn();
|
|
599
458
|
}
|
|
@@ -606,9 +465,9 @@ var EventBridge = class {
|
|
|
606
465
|
this.child = void 0;
|
|
607
466
|
if (child !== void 0) killChildTree(child);
|
|
608
467
|
}
|
|
609
|
-
/**
|
|
468
|
+
/** 鉴权或工作区状态变化时同步监听。 */
|
|
610
469
|
sync(snapshot) {
|
|
611
|
-
if (snapshot.ready) {
|
|
470
|
+
if (snapshot.ready && this.deps.getWorkspaceReady()) {
|
|
612
471
|
if (!this.running) this.start();
|
|
613
472
|
return;
|
|
614
473
|
}
|
|
@@ -691,7 +550,7 @@ var EventBridge = class {
|
|
|
691
550
|
/** 处理一条事件:过滤、去重、格式化并注入。 */
|
|
692
551
|
async handleEvent(event) {
|
|
693
552
|
const auth = this.deps.getAuth();
|
|
694
|
-
if (auth.ready !== true) return false;
|
|
553
|
+
if (auth.ready !== true || !this.deps.getWorkspaceReady()) return false;
|
|
695
554
|
const owner = auth.ownerOpenId;
|
|
696
555
|
if (owner === void 0 || owner === "") return false;
|
|
697
556
|
if (!acceptEvent(event, owner)) return false;
|
|
@@ -699,9 +558,9 @@ var EventBridge = class {
|
|
|
699
558
|
if (id === "") return false;
|
|
700
559
|
const time = parseTimestamp(event.timestamp ?? event.create_time);
|
|
701
560
|
if (!await this.deps.store.markSeen(id, time)) return false;
|
|
702
|
-
const
|
|
561
|
+
const block = formatLarkMessage(event);
|
|
703
562
|
try {
|
|
704
|
-
const result = await this.deps.sessions.inject(
|
|
563
|
+
const result = await this.deps.sessions.inject(block);
|
|
705
564
|
this.deps.logger.info(`lark-bridge: delivered ${id} to session ${result.sessionId} (created=${String(result.created)})`);
|
|
706
565
|
return true;
|
|
707
566
|
} catch (error) {
|
|
@@ -711,90 +570,165 @@ var EventBridge = class {
|
|
|
711
570
|
}
|
|
712
571
|
};
|
|
713
572
|
//#endregion
|
|
573
|
+
//#region src/workspace-path.ts
|
|
574
|
+
/** 工作区路径:统一 Linux 风格表示、平台比较与 Host 原生路径转换。 */
|
|
575
|
+
/** 判断原始或归一化路径是否使用 Windows 语义。 */
|
|
576
|
+
function isWindowsPath(value) {
|
|
577
|
+
return /^(?:[A-Za-z]:[\\/]|\\\\|\/\/|\/[A-Za-z](?:\/|$))/.test(value);
|
|
578
|
+
}
|
|
579
|
+
/** 统一斜杠、末尾分隔符与 Windows 盘符,例如 `C:\work\demo` 转为 `/c/work/demo`。 */
|
|
580
|
+
function normalizeWorkspacePath(value) {
|
|
581
|
+
const slashed = value.trim().replaceAll("\\", "/");
|
|
582
|
+
if (slashed === "") return "";
|
|
583
|
+
const drive = /^([A-Za-z]):(?:\/+(.*))?$/.exec(slashed);
|
|
584
|
+
if (drive !== null) {
|
|
585
|
+
const rest = (drive[2] ?? "").replace(/\/{2,}/g, "/").replace(/\/+$/, "");
|
|
586
|
+
return `/${drive[1]?.toLowerCase()}${rest === "" ? "" : `/${rest}`}`;
|
|
587
|
+
}
|
|
588
|
+
const unc = slashed.startsWith("//");
|
|
589
|
+
const collapsed = slashed.replace(/\/{2,}/g, "/");
|
|
590
|
+
const withoutTrailing = collapsed.length > 1 ? collapsed.replace(/\/+$/, "") : collapsed;
|
|
591
|
+
return unc ? `/${withoutTrailing}` : withoutTrailing;
|
|
592
|
+
}
|
|
593
|
+
/** 比较工作区路径;Windows 忽略大小写,POSIX 保持大小写敏感。 */
|
|
594
|
+
function sameWorkspacePath(a, b) {
|
|
595
|
+
const left = normalizeWorkspacePath(a);
|
|
596
|
+
const right = normalizeWorkspacePath(b);
|
|
597
|
+
return isWindowsPath(a) || isWindowsPath(b) ? left.toLowerCase() === right.toLowerCase() : left === right;
|
|
598
|
+
}
|
|
599
|
+
/** 在 Host 边界把归一化路径转换为当前平台的原生形式。 */
|
|
600
|
+
function toHostWorkspacePath(value, platform = process.platform) {
|
|
601
|
+
const normalized = normalizeWorkspacePath(value);
|
|
602
|
+
if (platform !== "win32") return normalized;
|
|
603
|
+
const drive = /^\/([A-Za-z])(?:\/(.*))?$/.exec(normalized);
|
|
604
|
+
if (drive !== null) return `${drive[1]?.toUpperCase()}:${drive[2] === void 0 ? "/" : `/${drive[2]}`}`;
|
|
605
|
+
if (normalized.startsWith("//")) return `\\\\${normalized.slice(2).replaceAll("/", "\\")}`;
|
|
606
|
+
return normalized;
|
|
607
|
+
}
|
|
608
|
+
//#endregion
|
|
714
609
|
//#region src/config.ts
|
|
715
|
-
/**
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
*/
|
|
719
|
-
/**
|
|
720
|
-
* 配置 schema。`.volatile()` 字段修改后不重启插件,并出现在配置页。
|
|
721
|
-
* schema 不拒绝未知键,严格校验由 resolveConfig 完成。
|
|
722
|
-
*/
|
|
723
|
-
const Config = Schema.object({
|
|
724
|
-
workspaceRoot: Schema.string().default("").volatile(),
|
|
725
|
-
sessionTitle: Schema.string().default(DEFAULT_SESSION_TITLE).volatile(),
|
|
726
|
-
larkCli: Schema.string().default("").volatile(),
|
|
727
|
-
authCheckOnStartup: Schema.boolean().default(true).volatile()
|
|
728
|
-
});
|
|
610
|
+
/** 插件配置:单工作区 schema、路径归一化、严格校验与旧键兼容。 */
|
|
611
|
+
/** 配置 schema;严格键校验由 resolveConfig 完成。 */
|
|
612
|
+
const Config = Schema.object({ workspaceRoot: Schema.string().default("").volatile() });
|
|
729
613
|
/** 默认配置。 */
|
|
730
|
-
const DEFAULT_CONFIG = Object.freeze({
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
const CONFIG_KEYS = /* @__PURE__ */ new Set([
|
|
738
|
-
"workspaceRoot",
|
|
739
|
-
"sessionTitle",
|
|
614
|
+
const DEFAULT_CONFIG = Object.freeze({ workspaceRoot: "" });
|
|
615
|
+
/** 合法的当前配置键。 */
|
|
616
|
+
const CONFIG_KEYS = /* @__PURE__ */ new Set(["workspaceRoot"]);
|
|
617
|
+
/** 旧版本的配置键:接受并丢弃,避免升级后插件加载失败。 */
|
|
618
|
+
const LEGACY_KEYS = /* @__PURE__ */ new Set([
|
|
619
|
+
"profile",
|
|
620
|
+
"ownerOpenId",
|
|
740
621
|
"larkCli",
|
|
622
|
+
"sessionTitle",
|
|
741
623
|
"authCheckOnStartup"
|
|
742
624
|
]);
|
|
743
|
-
/** 旧版本的配置键:接受并丢弃,避免升级后插件加载失败。 */
|
|
744
|
-
const LEGACY_KEYS = /* @__PURE__ */ new Set(["profile", "ownerOpenId"]);
|
|
745
625
|
/** 把缺省或 null 的输入转为空对象。 */
|
|
746
626
|
function normalizeConfigInput(raw) {
|
|
747
627
|
if (raw === void 0 || raw === null) return {};
|
|
748
628
|
if (!isRecord(raw)) throw new Error(`${PLUGIN_LABEL}: config must be an object`);
|
|
749
629
|
return raw;
|
|
750
630
|
}
|
|
751
|
-
/**
|
|
752
|
-
function stringField(input, key, fallback, validate) {
|
|
753
|
-
const raw = input[key];
|
|
754
|
-
if (raw === void 0 || raw === null) return fallback;
|
|
755
|
-
if (typeof raw !== "string") throw new Error(`${PLUGIN_LABEL}: config ${key} must be a string`);
|
|
756
|
-
const value = raw.trim();
|
|
757
|
-
validate(value);
|
|
758
|
-
return value;
|
|
759
|
-
}
|
|
760
|
-
/** 校验配置并与默认值合并,返回冻结的完整配置。 */
|
|
631
|
+
/** 校验配置并与默认值合并,工作区路径统一为 Linux 风格。 */
|
|
761
632
|
function resolveConfig(raw) {
|
|
762
633
|
const input = normalizeConfigInput(raw);
|
|
763
634
|
for (const key of Object.keys(input)) if (!CONFIG_KEYS.has(key) && !LEGACY_KEYS.has(key)) throw new Error(`${PLUGIN_LABEL}: unknown config key "${key}"`);
|
|
764
|
-
const
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
if (value === "") throw new Error(`${PLUGIN_LABEL}: config sessionTitle must not be empty`);
|
|
768
|
-
});
|
|
769
|
-
config.larkCli = stringField(input, "larkCli", DEFAULT_CONFIG.larkCli, () => {});
|
|
770
|
-
const authCheck = input.authCheckOnStartup;
|
|
771
|
-
if (authCheck !== void 0 && authCheck !== null) {
|
|
772
|
-
if (typeof authCheck !== "boolean") throw new Error(`${PLUGIN_LABEL}: config authCheckOnStartup must be a boolean`);
|
|
773
|
-
config.authCheckOnStartup = authCheck;
|
|
774
|
-
}
|
|
775
|
-
return Object.freeze(config);
|
|
635
|
+
const value = input["workspaceRoot"];
|
|
636
|
+
if (value !== void 0 && value !== null && typeof value !== "string") throw new Error(`${PLUGIN_LABEL}: config workspaceRoot must be a string`);
|
|
637
|
+
return Object.freeze({ workspaceRoot: typeof value === "string" ? normalizeWorkspacePath(value) : DEFAULT_CONFIG.workspaceRoot });
|
|
776
638
|
}
|
|
777
639
|
/** 判断值是否是热重载引用。 */
|
|
778
640
|
function isVolatileRef(value) {
|
|
779
641
|
return isRecord(value) && typeof value.get === "function";
|
|
780
642
|
}
|
|
781
|
-
/**
|
|
643
|
+
/** 展开热重载引用后交给 resolveConfig 校验。 */
|
|
782
644
|
function readConfig(config) {
|
|
783
645
|
if (!isRecord(config)) return resolveConfig(config);
|
|
784
646
|
const values = {};
|
|
785
647
|
for (const [key, value] of Object.entries(config)) values[key] = isVolatileRef(value) ? value.get() : value;
|
|
786
648
|
return resolveConfig(values);
|
|
787
649
|
}
|
|
788
|
-
/**
|
|
789
|
-
* 解析插件状态文件路径。
|
|
790
|
-
* @param home - DSH home;省略时读取 `DSH_HOME`,再回落到 `~/.dsh`。
|
|
791
|
-
* @returns `state.json` 的绝对路径。
|
|
792
|
-
*/
|
|
650
|
+
/** 解析插件状态文件路径。 */
|
|
793
651
|
function statePath(home) {
|
|
794
652
|
const root = home ?? process.env["DSH_HOME"] ?? join(homedir(), ".dsh");
|
|
795
653
|
return join(root, STATE_DIR, STATE_FILE);
|
|
796
654
|
}
|
|
797
655
|
//#endregion
|
|
656
|
+
//#region src/message.ts
|
|
657
|
+
/** 飞书消息块的来源标记。 */
|
|
658
|
+
const LARK_SOURCE = { kind: "lark-bridge" };
|
|
659
|
+
/** 桥接提醒的来源标记。 */
|
|
660
|
+
const REMINDER_SOURCE = {
|
|
661
|
+
kind: "lark-bridge",
|
|
662
|
+
reminder: true
|
|
663
|
+
};
|
|
664
|
+
/** 判断消息是否由本插件注入。 */
|
|
665
|
+
function isLarkMessage(message) {
|
|
666
|
+
return message.role === "user" && message.source.kind === "lark-bridge";
|
|
667
|
+
}
|
|
668
|
+
/** 判断消息是否是一条桥接提醒。 */
|
|
669
|
+
function isReminderMessage(message) {
|
|
670
|
+
if (!isLarkMessage(message)) return false;
|
|
671
|
+
return message.source.kind === "lark-bridge" && message.source.reminder === true;
|
|
672
|
+
}
|
|
673
|
+
//#endregion
|
|
674
|
+
//#region src/reminder.ts
|
|
675
|
+
/** 提醒正文;说明块含义与回复规则。 */
|
|
676
|
+
const BRIDGE_REMINDER = [
|
|
677
|
+
"<lark-message> 块是用户通过飞书远程发送的消息。",
|
|
678
|
+
"用户只能看到 lark_notify 发送的内容,看不到本会话的回复。",
|
|
679
|
+
"- 回复飞书消息时调用 lark_notify,把要发给用户的内容放进 text。",
|
|
680
|
+
"- 用户连续发来多条 <lark-message> 时只回复一次,用一次 lark_notify 覆盖全部内容。",
|
|
681
|
+
"- 会话里的 text 块用户看不到,尽量少输出,说明性文字也放进 lark_notify。",
|
|
682
|
+
"- 其他来源的用户消息按普通方式回复,不调用 lark_notify。"
|
|
683
|
+
].join("\n");
|
|
684
|
+
/** 构造提醒消息。 */
|
|
685
|
+
function createReminderMessage() {
|
|
686
|
+
return createUserMessage({
|
|
687
|
+
content: [{
|
|
688
|
+
type: "text",
|
|
689
|
+
text: reminder(BRIDGE_REMINDER)
|
|
690
|
+
}],
|
|
691
|
+
source: REMINDER_SOURCE
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
/** 表层是否已有桥接提醒。 */
|
|
695
|
+
function surfaceHasReminder(session) {
|
|
696
|
+
for (const seq of session.surface.nodes) {
|
|
697
|
+
const event = session.eventAt(seq);
|
|
698
|
+
if (event?.type === "user/message" && isReminderMessage(event.data)) return true;
|
|
699
|
+
}
|
|
700
|
+
return false;
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* 注册 `agent/pre-step` 补注:桥接会话表层缺少提醒时,把它插到本批第一条块之前。
|
|
704
|
+
* @param deps - 上下文、会话判定与日志。
|
|
705
|
+
* @returns 注销函数。
|
|
706
|
+
*/
|
|
707
|
+
function installReminderGuard(deps) {
|
|
708
|
+
return deps.ctx.on("agent/pre-step", async ({ agent, step }, next) => {
|
|
709
|
+
const decision = await next();
|
|
710
|
+
if (decision.kind === "reject") return decision;
|
|
711
|
+
if (step === 1 && decision.messages.length === 0) return decision;
|
|
712
|
+
const session = agent.session;
|
|
713
|
+
let bridge = false;
|
|
714
|
+
try {
|
|
715
|
+
bridge = await deps.sessions.isBridgeSession(session);
|
|
716
|
+
} catch (error) {
|
|
717
|
+
deps.logger.warn(`lark-bridge: cannot resolve reminder state: ${describeError(error)}`);
|
|
718
|
+
return decision;
|
|
719
|
+
}
|
|
720
|
+
if (!bridge) return decision;
|
|
721
|
+
if (surfaceHasReminder(session)) return decision;
|
|
722
|
+
if (decision.messages.some(isReminderMessage)) return decision;
|
|
723
|
+
const index = decision.messages.findIndex(isLarkMessage);
|
|
724
|
+
const messages = decision.messages.toSpliced(index === -1 ? 0 : index, 0, createReminderMessage());
|
|
725
|
+
return {
|
|
726
|
+
...decision,
|
|
727
|
+
messages
|
|
728
|
+
};
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
//#endregion
|
|
798
732
|
//#region src/errors.ts
|
|
799
733
|
/** 插件可诊断错误:携带稳定错误码,供工具与路由返回。 */
|
|
800
734
|
var BridgeError = class extends Error {
|
|
@@ -845,82 +779,43 @@ function rejectReason(deps, req) {
|
|
|
845
779
|
return describeError(error);
|
|
846
780
|
}
|
|
847
781
|
}
|
|
848
|
-
/**
|
|
849
|
-
|
|
850
|
-
const config = deps.getConfig();
|
|
782
|
+
/** 投影浏览器可读的笼统状态,排除内部身份与诊断信息。 */
|
|
783
|
+
function projectStatus(deps) {
|
|
851
784
|
const snapshot = deps.auth.current;
|
|
852
|
-
const state = await deps.store.load();
|
|
853
|
-
const flow = deps.auth.pendingLogin;
|
|
854
785
|
return {
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
error: deps.sessions.lastError,
|
|
861
|
-
login: flow === void 0 ? null : {
|
|
862
|
-
verificationUrl: flow.verificationUrl,
|
|
863
|
-
expiresAt: flow.expiresAt
|
|
864
|
-
}
|
|
786
|
+
cli: snapshot.cli,
|
|
787
|
+
bot: snapshot.bot,
|
|
788
|
+
user: snapshot.user,
|
|
789
|
+
ready: snapshot.ready,
|
|
790
|
+
checkedAt: snapshot.checkedAt
|
|
865
791
|
};
|
|
866
792
|
}
|
|
867
793
|
/** 执行路由动作并生成响应。 */
|
|
868
794
|
async function handle(deps, path, method) {
|
|
869
795
|
if (method === "GET" && path === `/lark-bridge/status`) return {
|
|
870
796
|
ok: true,
|
|
871
|
-
data:
|
|
797
|
+
data: projectStatus(deps)
|
|
872
798
|
};
|
|
873
799
|
if (method !== "POST") throw new BridgeError("method-not-allowed", "只支持 GET 与 POST");
|
|
874
800
|
if (path === `/lark-bridge/check`) {
|
|
875
|
-
const snapshot = await deps.auth.check(
|
|
801
|
+
const snapshot = await deps.auth.check();
|
|
876
802
|
deps.onAuthChanged?.(snapshot);
|
|
877
|
-
return {
|
|
878
|
-
ok: true,
|
|
879
|
-
data: await projectStatus(deps)
|
|
880
|
-
};
|
|
881
|
-
}
|
|
882
|
-
if (path === `/lark-bridge/auth/start`) {
|
|
883
|
-
const flow = await deps.auth.startLogin();
|
|
884
|
-
deps.auth.whenLoginSettled().then((snapshot) => {
|
|
885
|
-
if (snapshot !== void 0) deps.onAuthChanged?.(snapshot);
|
|
886
|
-
});
|
|
887
803
|
return {
|
|
888
804
|
ok: true,
|
|
889
805
|
data: {
|
|
890
|
-
|
|
891
|
-
|
|
806
|
+
cli: snapshot.cli,
|
|
807
|
+
bot: snapshot.bot,
|
|
808
|
+
user: snapshot.user,
|
|
809
|
+
ready: snapshot.ready,
|
|
810
|
+
checkedAt: snapshot.checkedAt
|
|
892
811
|
}
|
|
893
812
|
};
|
|
894
813
|
}
|
|
895
|
-
if (path === `/lark-bridge/auth/cancel`) {
|
|
896
|
-
deps.auth.cancelLogin();
|
|
897
|
-
return {
|
|
898
|
-
ok: true,
|
|
899
|
-
data: await projectStatus(deps)
|
|
900
|
-
};
|
|
901
|
-
}
|
|
902
|
-
if (path === `/lark-bridge/session/reset`) {
|
|
903
|
-
await deps.sessions.reset();
|
|
904
|
-
return {
|
|
905
|
-
ok: true,
|
|
906
|
-
data: await projectStatus(deps)
|
|
907
|
-
};
|
|
908
|
-
}
|
|
909
814
|
throw new BridgeError("not-found", "未知的桥接接口");
|
|
910
815
|
}
|
|
911
|
-
/**
|
|
912
|
-
* 注册全部桥接路由。
|
|
913
|
-
* @param deps - 服务、存储与状态来源。
|
|
914
|
-
* @returns 注销全部路由的清理函数。
|
|
915
|
-
*/
|
|
816
|
+
/** 注册桥接路由并返回清理函数。 */
|
|
916
817
|
function registerRoutes(deps) {
|
|
917
|
-
const paths = [
|
|
918
|
-
"status",
|
|
919
|
-
"check",
|
|
920
|
-
"auth/start",
|
|
921
|
-
"auth/cancel",
|
|
922
|
-
"session/reset"
|
|
923
|
-
];
|
|
818
|
+
const paths = ["status", "check"];
|
|
924
819
|
const disposers = [];
|
|
925
820
|
for (const suffix of paths) {
|
|
926
821
|
const path = `${ROUTE_PREFIX}/${suffix}`;
|
|
@@ -966,8 +861,6 @@ function registerRoutes(deps) {
|
|
|
966
861
|
* 桥接会话:工作区校验、创建或恢复、标题、挂载、置顶与状态持久化。
|
|
967
862
|
* 导出 SessionManager。
|
|
968
863
|
*/
|
|
969
|
-
/** 飞书来源的消息标记。 */
|
|
970
|
-
const SOURCE = { kind: "lark-bridge" };
|
|
971
864
|
/** 把路径解析为绝对路径。 */
|
|
972
865
|
function absolute(path) {
|
|
973
866
|
return resolve(path);
|
|
@@ -992,16 +885,6 @@ function appendTitle(session, title) {
|
|
|
992
885
|
};
|
|
993
886
|
session.append.call(session, "session/title", data);
|
|
994
887
|
}
|
|
995
|
-
/** 构造用于注入的 system-reminder 用户消息。 */
|
|
996
|
-
function reminderMessage(text) {
|
|
997
|
-
return createUserMessage({
|
|
998
|
-
content: [{
|
|
999
|
-
type: "text",
|
|
1000
|
-
text: reminder(text)
|
|
1001
|
-
}],
|
|
1002
|
-
source: SOURCE
|
|
1003
|
-
});
|
|
1004
|
-
}
|
|
1005
888
|
/** 桥接会话的创建、恢复与注入。 */
|
|
1006
889
|
var SessionManager = class {
|
|
1007
890
|
deps;
|
|
@@ -1043,16 +926,15 @@ var SessionManager = class {
|
|
|
1043
926
|
}
|
|
1044
927
|
};
|
|
1045
928
|
}
|
|
1046
|
-
/**
|
|
1047
|
-
async attach(sessionId,
|
|
1048
|
-
const registry = this.registry();
|
|
1049
|
-
const workspace = await registry.resolveByPath(cwd) ?? await registry.create(cwd);
|
|
929
|
+
/** 把会话挂到已注册工作区。 */
|
|
930
|
+
async attach(sessionId, workspace) {
|
|
1050
931
|
const id = SessionId(sessionId);
|
|
1051
932
|
if (!workspace.sessionIds.includes(id)) await workspace.attachSession(id);
|
|
1052
933
|
}
|
|
1053
934
|
/** 恢复一个可用会话;失败返回 undefined。 */
|
|
1054
935
|
async resume(sessionId) {
|
|
1055
936
|
const id = SessionId(sessionId);
|
|
937
|
+
if (this.registry().archivedSessionIds.includes(id)) return void 0;
|
|
1056
938
|
const live = this.ctx.agents.get(id);
|
|
1057
939
|
if (live !== void 0) return live;
|
|
1058
940
|
try {
|
|
@@ -1068,7 +950,7 @@ var SessionManager = class {
|
|
|
1068
950
|
}
|
|
1069
951
|
}
|
|
1070
952
|
/** 新建会话,设置标题、挂到工作区并置顶。 */
|
|
1071
|
-
async create(cwd, title) {
|
|
953
|
+
async create(cwd, title, workspace) {
|
|
1072
954
|
const sessionId = randomUUID();
|
|
1073
955
|
const handle = await this.ctx.agents.create({
|
|
1074
956
|
sessionId: SessionId(sessionId),
|
|
@@ -1078,11 +960,11 @@ var SessionManager = class {
|
|
|
1078
960
|
appendTitle(child.session, title);
|
|
1079
961
|
}
|
|
1080
962
|
});
|
|
1081
|
-
await this.attach(sessionId,
|
|
963
|
+
await this.attach(sessionId, workspace);
|
|
1082
964
|
await this.registry().pinSession(SessionId(sessionId));
|
|
1083
965
|
await this.deps.store.patch({
|
|
1084
966
|
sessionId,
|
|
1085
|
-
workspaceRoot: cwd,
|
|
967
|
+
workspaceRoot: normalizeWorkspacePath(cwd),
|
|
1086
968
|
title
|
|
1087
969
|
});
|
|
1088
970
|
this.deps.logger.info(`lark-bridge: created session ${sessionId} in ${cwd}`);
|
|
@@ -1109,38 +991,47 @@ var SessionManager = class {
|
|
|
1109
991
|
* @returns 会话标识、会话实例与是否新建。
|
|
1110
992
|
*/
|
|
1111
993
|
async ensure(workspaceRoot) {
|
|
1112
|
-
const cwd =
|
|
1113
|
-
|
|
994
|
+
const cwd = toHostWorkspacePath(workspaceRoot);
|
|
995
|
+
await assertWorkspace(cwd);
|
|
996
|
+
const workspace = await this.registry().resolveByPath(cwd);
|
|
997
|
+
if (workspace === void 0) throw new BridgeError("workspace-unregistered", `工作区未注册:${normalizeWorkspacePath(cwd)}`);
|
|
1114
998
|
const state = await this.deps.store.load();
|
|
1115
|
-
if (state.sessionId !== null && state.workspaceRoot !== null &&
|
|
999
|
+
if (state.sessionId !== null && state.workspaceRoot !== null && sameWorkspacePath(state.workspaceRoot, cwd)) {
|
|
1116
1000
|
const agent = await this.resume(state.sessionId);
|
|
1117
1001
|
if (agent !== void 0) {
|
|
1118
|
-
await this.syncMeta(state.sessionId, agent,
|
|
1002
|
+
await this.syncMeta(state.sessionId, agent, SESSION_TITLE, state.title ?? "来自用户的飞书消息");
|
|
1119
1003
|
return {
|
|
1120
1004
|
sessionId: state.sessionId,
|
|
1121
1005
|
agent,
|
|
1122
|
-
created: false
|
|
1006
|
+
created: false,
|
|
1007
|
+
cwd
|
|
1123
1008
|
};
|
|
1124
1009
|
}
|
|
1125
1010
|
}
|
|
1126
|
-
const created = await this.create(cwd, config.sessionTitle);
|
|
1127
1011
|
return {
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1012
|
+
...await this.create(cwd, SESSION_TITLE, workspace),
|
|
1013
|
+
created: true,
|
|
1014
|
+
cwd
|
|
1131
1015
|
};
|
|
1132
1016
|
}
|
|
1133
1017
|
/**
|
|
1134
|
-
*
|
|
1135
|
-
* @param
|
|
1018
|
+
* 把一段飞书消息块注入桥接会话。
|
|
1019
|
+
* @param block - 已格式化的 `<lark-message>` 块。
|
|
1136
1020
|
* @returns 注入目标与会话是否新建。
|
|
1137
1021
|
*/
|
|
1138
|
-
async inject(
|
|
1022
|
+
async inject(block) {
|
|
1139
1023
|
try {
|
|
1140
1024
|
const config = this.deps.getConfig();
|
|
1141
1025
|
if (config.workspaceRoot === "") throw new BridgeError("workspace-missing", "尚未配置桥接工作区");
|
|
1142
|
-
const { sessionId, agent, created } = await this.ensure(config.workspaceRoot);
|
|
1143
|
-
|
|
1026
|
+
const { sessionId, agent, created, cwd } = await this.ensure(config.workspaceRoot);
|
|
1027
|
+
if (await this.registry().resolveByPath(cwd) === void 0) throw new BridgeError("workspace-unregistered", `工作区未注册:${normalizeWorkspacePath(cwd)}`);
|
|
1028
|
+
agent.followup(createUserMessage({
|
|
1029
|
+
content: [{
|
|
1030
|
+
type: "text",
|
|
1031
|
+
text: block
|
|
1032
|
+
}],
|
|
1033
|
+
source: LARK_SOURCE
|
|
1034
|
+
}));
|
|
1144
1035
|
this.failure = void 0;
|
|
1145
1036
|
return {
|
|
1146
1037
|
sessionId,
|
|
@@ -1150,9 +1041,14 @@ var SessionManager = class {
|
|
|
1150
1041
|
this.fail(error);
|
|
1151
1042
|
}
|
|
1152
1043
|
}
|
|
1153
|
-
/**
|
|
1154
|
-
|
|
1155
|
-
|
|
1044
|
+
/**
|
|
1045
|
+
* 判断会话是否是当前桥接会话。
|
|
1046
|
+
* @param session - 宿主的会话实例。
|
|
1047
|
+
* @returns 状态文件记录的就是该会话时为 true。
|
|
1048
|
+
*/
|
|
1049
|
+
async isBridgeSession(session) {
|
|
1050
|
+
const state = await this.deps.store.load();
|
|
1051
|
+
return state.sessionId !== null && state.sessionId === session.id;
|
|
1156
1052
|
}
|
|
1157
1053
|
};
|
|
1158
1054
|
//#endregion
|
|
@@ -1207,6 +1103,7 @@ var StateStore = class {
|
|
|
1207
1103
|
const parsed = JSON.parse(text);
|
|
1208
1104
|
this.cache = isState(parsed) ? {
|
|
1209
1105
|
...parsed,
|
|
1106
|
+
workspaceRoot: parsed.workspaceRoot === null ? null : normalizeWorkspacePath(parsed.workspaceRoot),
|
|
1210
1107
|
seen: trimSeen(parsed.seen, this.limit)
|
|
1211
1108
|
} : emptyState();
|
|
1212
1109
|
} catch {
|
|
@@ -1234,6 +1131,7 @@ var StateStore = class {
|
|
|
1234
1131
|
}, this.limit),
|
|
1235
1132
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1236
1133
|
};
|
|
1134
|
+
if (next.workspaceRoot !== null) next.workspaceRoot = normalizeWorkspacePath(next.workspaceRoot);
|
|
1237
1135
|
await this.write(next);
|
|
1238
1136
|
this.cache = next;
|
|
1239
1137
|
return next;
|
|
@@ -1282,14 +1180,6 @@ var StateStore = class {
|
|
|
1282
1180
|
return next;
|
|
1283
1181
|
});
|
|
1284
1182
|
}
|
|
1285
|
-
/** 清除会话标识,保留下一次消息重建。 */
|
|
1286
|
-
resetSession() {
|
|
1287
|
-
return this.patch({
|
|
1288
|
-
sessionId: null,
|
|
1289
|
-
workspaceRoot: null,
|
|
1290
|
-
title: null
|
|
1291
|
-
});
|
|
1292
|
-
}
|
|
1293
1183
|
/** 把写操作串到同一条队列,避免并发覆盖。 */
|
|
1294
1184
|
enqueue(action) {
|
|
1295
1185
|
const next = this.queue.then(action, action);
|
|
@@ -1337,7 +1227,7 @@ async function sendNotification(deps, args) {
|
|
|
1337
1227
|
ok: false,
|
|
1338
1228
|
error: {
|
|
1339
1229
|
code: "owner-missing",
|
|
1340
|
-
message: "
|
|
1230
|
+
message: "尚未确定已绑定用户,请在终端登录后重新检测"
|
|
1341
1231
|
}
|
|
1342
1232
|
};
|
|
1343
1233
|
const parts = splitText(text, MAX_MESSAGE_CHARS);
|
|
@@ -1352,10 +1242,10 @@ async function sendNotification(deps, args) {
|
|
|
1352
1242
|
"--user-id",
|
|
1353
1243
|
owner,
|
|
1354
1244
|
"--text",
|
|
1355
|
-
|
|
1245
|
+
"-",
|
|
1356
1246
|
"--idempotency-key",
|
|
1357
1247
|
`${baseKey}-${index}`.slice(0, 50)
|
|
1358
|
-
]);
|
|
1248
|
+
], { input: part });
|
|
1359
1249
|
const record = typeof value === "object" && value !== null ? value : {};
|
|
1360
1250
|
first ??= typeof record["message_id"] === "string" ? record["message_id"] : void 0;
|
|
1361
1251
|
} catch (error) {
|
|
@@ -1440,28 +1330,83 @@ function buildNotifyTool(deps) {
|
|
|
1440
1330
|
};
|
|
1441
1331
|
}
|
|
1442
1332
|
//#endregion
|
|
1333
|
+
//#region src/workspace.ts
|
|
1334
|
+
/** 保存最近一次有效解析,并用序号阻止旧结果覆盖新选择。 */
|
|
1335
|
+
var WorkspaceSelection = class {
|
|
1336
|
+
deps;
|
|
1337
|
+
revision = 0;
|
|
1338
|
+
selectedRoot = "";
|
|
1339
|
+
selectedWorkspace;
|
|
1340
|
+
constructor(deps) {
|
|
1341
|
+
this.deps = deps;
|
|
1342
|
+
}
|
|
1343
|
+
/** 当前配置是否与已解析的注册工作区一致。 */
|
|
1344
|
+
get valid() {
|
|
1345
|
+
return this.selectedWorkspace !== void 0 && sameWorkspacePath(this.selectedRoot, this.deps.getWorkspaceRoot());
|
|
1346
|
+
}
|
|
1347
|
+
/** 当前注册工作区 id。 */
|
|
1348
|
+
get currentId() {
|
|
1349
|
+
return this.valid && this.selectedWorkspace !== void 0 ? String(this.selectedWorkspace.id) : void 0;
|
|
1350
|
+
}
|
|
1351
|
+
/** 使当前缓存和未完成的解析失效。 */
|
|
1352
|
+
reset() {
|
|
1353
|
+
this.revision += 1;
|
|
1354
|
+
this.selectedRoot = "";
|
|
1355
|
+
this.selectedWorkspace = void 0;
|
|
1356
|
+
}
|
|
1357
|
+
/** 目标注册项删除时同步失效。 */
|
|
1358
|
+
invalidate(change) {
|
|
1359
|
+
if (typeof change !== "object" || change === null) return false;
|
|
1360
|
+
const record = change;
|
|
1361
|
+
if (record["domain"] !== "workspace" || record["table"] !== "workspaces" || record["operation"] !== "deleted" || record["key"] !== this.currentId) return false;
|
|
1362
|
+
this.reset();
|
|
1363
|
+
return true;
|
|
1364
|
+
}
|
|
1365
|
+
/** 解析当前配置;只有最新结果可以更新缓存。 */
|
|
1366
|
+
async resolve() {
|
|
1367
|
+
const revision = ++this.revision;
|
|
1368
|
+
const root = normalizeWorkspacePath(this.deps.getWorkspaceRoot());
|
|
1369
|
+
let workspace;
|
|
1370
|
+
if (root !== "") {
|
|
1371
|
+
const registry = this.deps.getRegistry();
|
|
1372
|
+
try {
|
|
1373
|
+
workspace = await registry?.resolveByPath(toHostWorkspacePath(root));
|
|
1374
|
+
} catch {
|
|
1375
|
+
workspace = void 0;
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
const current = revision === this.revision && sameWorkspacePath(root, this.deps.getWorkspaceRoot());
|
|
1379
|
+
if (current) {
|
|
1380
|
+
this.selectedRoot = root;
|
|
1381
|
+
this.selectedWorkspace = workspace;
|
|
1382
|
+
}
|
|
1383
|
+
return {
|
|
1384
|
+
root,
|
|
1385
|
+
workspace,
|
|
1386
|
+
current
|
|
1387
|
+
};
|
|
1388
|
+
}
|
|
1389
|
+
};
|
|
1390
|
+
//#endregion
|
|
1443
1391
|
//#region src/index.ts
|
|
1444
|
-
/**
|
|
1445
|
-
* 插件入口:导出宿主约定的 name、inject、apply 与 Config。
|
|
1446
|
-
* 装配鉴权、事件桥、会话、通知工具与浏览器路由。
|
|
1447
|
-
*/
|
|
1448
1392
|
/** 插件名,与 cordis.patch.yml 的 id 一致。 */
|
|
1449
1393
|
const name = PLUGIN_LABEL;
|
|
1450
1394
|
/** 需要宿主注入的服务。 */
|
|
1451
1395
|
const inject = [
|
|
1452
1396
|
"tools",
|
|
1453
1397
|
"agents",
|
|
1454
|
-
"webServer"
|
|
1398
|
+
"webServer",
|
|
1399
|
+
"workspaceRegistry"
|
|
1455
1400
|
];
|
|
1456
1401
|
/**
|
|
1457
1402
|
* 插件启用时调用:读取配置、装配服务、注册工具与路由、启动鉴权检查。
|
|
1458
|
-
*
|
|
1403
|
+
* 配置字段均为热重载引用,保存后更新配置并重新检测。
|
|
1459
1404
|
*/
|
|
1460
1405
|
function apply(ctx, config) {
|
|
1461
1406
|
let current = readConfig(config);
|
|
1462
1407
|
ctx.logger.info(`[${PLUGIN_LABEL}] apply`);
|
|
1463
1408
|
const store = new StateStore(statePath());
|
|
1464
|
-
|
|
1409
|
+
const cli = new LarkCli();
|
|
1465
1410
|
let lastStage = "";
|
|
1466
1411
|
const sessions = new SessionManager({
|
|
1467
1412
|
ctx,
|
|
@@ -1469,26 +1414,43 @@ function apply(ctx, config) {
|
|
|
1469
1414
|
getConfig: () => current,
|
|
1470
1415
|
logger: ctx.logger
|
|
1471
1416
|
});
|
|
1417
|
+
const uninstallReminder = installReminderGuard({
|
|
1418
|
+
ctx,
|
|
1419
|
+
sessions,
|
|
1420
|
+
logger: ctx.logger
|
|
1421
|
+
});
|
|
1422
|
+
ctx.effect(() => () => uninstallReminder(), `${PLUGIN_LABEL}: reminder guard`);
|
|
1423
|
+
const selection = new WorkspaceSelection({
|
|
1424
|
+
getWorkspaceRoot: () => current.workspaceRoot,
|
|
1425
|
+
getRegistry: () => ctx.get("workspaceRegistry")
|
|
1426
|
+
});
|
|
1472
1427
|
let bridge;
|
|
1428
|
+
let reconcile;
|
|
1473
1429
|
const auth = new AuthManager({
|
|
1474
1430
|
getCli: () => cli,
|
|
1475
1431
|
logger: ctx.logger,
|
|
1476
1432
|
onChange: (snapshot) => {
|
|
1477
|
-
const
|
|
1478
|
-
if (
|
|
1479
|
-
lastStage =
|
|
1480
|
-
ctx.logger.info(`[${PLUGIN_LABEL}] auth
|
|
1433
|
+
const state = snapshot.ready ? "ready" : `${snapshot.cli}/${snapshot.bot}/${snapshot.user}`;
|
|
1434
|
+
if (state !== lastStage) {
|
|
1435
|
+
lastStage = state;
|
|
1436
|
+
ctx.logger.info(`[${PLUGIN_LABEL}] auth state: ${state}`);
|
|
1481
1437
|
}
|
|
1482
|
-
bridge.
|
|
1438
|
+
if (!snapshot.ready) bridge.stop();
|
|
1439
|
+
reconcile();
|
|
1483
1440
|
}
|
|
1484
1441
|
});
|
|
1485
1442
|
bridge = new EventBridge({
|
|
1486
1443
|
getCli: () => cli,
|
|
1487
1444
|
getAuth: () => auth.current,
|
|
1445
|
+
getWorkspaceReady: () => selection.valid,
|
|
1488
1446
|
store,
|
|
1489
1447
|
sessions,
|
|
1490
1448
|
logger: ctx.logger
|
|
1491
1449
|
});
|
|
1450
|
+
reconcile = async () => {
|
|
1451
|
+
if (!auth.current.ready) bridge.stop();
|
|
1452
|
+
if ((await selection.resolve()).current) bridge.sync(auth.current);
|
|
1453
|
+
};
|
|
1492
1454
|
const notifyTool = buildNotifyTool({
|
|
1493
1455
|
getCli: () => cli,
|
|
1494
1456
|
getOwner: () => auth.current.ownerOpenId
|
|
@@ -1504,11 +1466,7 @@ function apply(ctx, config) {
|
|
|
1504
1466
|
webServer,
|
|
1505
1467
|
...connection === void 0 ? {} : { connection },
|
|
1506
1468
|
auth,
|
|
1507
|
-
|
|
1508
|
-
store,
|
|
1509
|
-
getConfig: () => current,
|
|
1510
|
-
logger: ctx.logger,
|
|
1511
|
-
onAuthChanged: (snapshot) => bridge.sync(snapshot)
|
|
1469
|
+
logger: ctx.logger
|
|
1512
1470
|
});
|
|
1513
1471
|
}
|
|
1514
1472
|
ctx.effect(() => () => disposeRoutes?.(), `${PLUGIN_LABEL}: routes`);
|
|
@@ -1519,13 +1477,18 @@ function apply(ctx, config) {
|
|
|
1519
1477
|
ctx.logger.warn(`[${PLUGIN_LABEL}] auth check failed: ${describeError(error)}`);
|
|
1520
1478
|
});
|
|
1521
1479
|
};
|
|
1522
|
-
|
|
1480
|
+
runCheck();
|
|
1481
|
+
ctx.on("domain/changed", (change) => {
|
|
1482
|
+
if (!selection.invalidate(change)) return;
|
|
1483
|
+
bridge.stop();
|
|
1484
|
+
reconcile();
|
|
1485
|
+
});
|
|
1523
1486
|
ctx.on("loader/volatile-update", () => {
|
|
1524
1487
|
try {
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1488
|
+
current = readConfig(config);
|
|
1489
|
+
selection.reset();
|
|
1490
|
+
bridge.stop();
|
|
1491
|
+
reconcile();
|
|
1529
1492
|
ctx.logger.info(`[${PLUGIN_LABEL}] config updated`);
|
|
1530
1493
|
runCheck();
|
|
1531
1494
|
} catch (error) {
|
|
@@ -1533,7 +1496,6 @@ function apply(ctx, config) {
|
|
|
1533
1496
|
}
|
|
1534
1497
|
});
|
|
1535
1498
|
ctx.effect(() => () => bridge.stop(), `${PLUGIN_LABEL}: event bridge`);
|
|
1536
|
-
ctx.effect(() => () => auth.cancelLogin(), `${PLUGIN_LABEL}: login`);
|
|
1537
1499
|
}
|
|
1538
1500
|
//#endregion
|
|
1539
1501
|
export { Config, apply, inject, name };
|