@nextclaw/channel-plugin-feishu 0.2.20 → 0.2.21

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nextclaw/channel-plugin-feishu",
3
- "version": "0.2.20",
3
+ "version": "0.2.21",
4
4
  "private": false,
5
5
  "description": "NextClaw Feishu/Lark channel plugin with doc/wiki/drive tools.",
6
6
  "type": "module",
@@ -2,11 +2,18 @@ import type { OpenClawConfig } from "./nextclaw-sdk/feishu.js";
2
2
  import { describe, expect, it, vi } from "vitest";
3
3
 
4
4
  const probeFeishuMock = vi.hoisted(() => vi.fn());
5
+ const monitorFeishuProviderMock = vi.hoisted(() => vi.fn());
6
+ const stopFeishuMonitorMock = vi.hoisted(() => vi.fn());
5
7
 
6
8
  vi.mock("./probe.js", () => ({
7
9
  probeFeishu: probeFeishuMock,
8
10
  }));
9
11
 
12
+ vi.mock("./monitor.js", () => ({
13
+ monitorFeishuProvider: monitorFeishuProviderMock,
14
+ stopFeishuMonitor: stopFeishuMonitorMock,
15
+ }));
16
+
10
17
  import { feishuPlugin } from "./channel.js";
11
18
 
12
19
  describe("feishuPlugin.status.probeAccount", () => {
@@ -45,4 +52,62 @@ describe("feishuPlugin.status.probeAccount", () => {
45
52
  );
46
53
  expect(result).toMatchObject({ ok: true, appId: "cli_main" });
47
54
  });
55
+
56
+ it("starts gateway monitor without blocking service startup", async () => {
57
+ let resolveMonitor!: () => void;
58
+ const monitorDone = new Promise<void>((resolve) => {
59
+ resolveMonitor = resolve;
60
+ });
61
+ monitorFeishuProviderMock.mockReturnValueOnce(monitorDone);
62
+
63
+ const abortController = new AbortController();
64
+ const ctx = {
65
+ cfg: {
66
+ channels: {
67
+ feishu: {
68
+ enabled: true,
69
+ appId: "cli_default",
70
+ appSecret: "secret_default",
71
+ connectionMode: "websocket",
72
+ },
73
+ },
74
+ } as OpenClawConfig,
75
+ accountId: "default",
76
+ abortSignal: abortController.signal,
77
+ setStatus: vi.fn(),
78
+ log: {
79
+ info: vi.fn(),
80
+ error: vi.fn(),
81
+ },
82
+ runtime: {
83
+ log: vi.fn(),
84
+ info: vi.fn(),
85
+ warn: vi.fn(),
86
+ error: vi.fn(),
87
+ debug: vi.fn(),
88
+ },
89
+ };
90
+
91
+ const timeoutToken = Symbol("timeout");
92
+ const started = await Promise.race([
93
+ feishuPlugin.gateway?.startAccount?.(ctx),
94
+ new Promise<symbol>((resolve) => setTimeout(() => resolve(timeoutToken), 20)),
95
+ ]);
96
+
97
+ expect(started).not.toBe(timeoutToken);
98
+ expect(monitorFeishuProviderMock).toHaveBeenCalledWith({
99
+ config: ctx.cfg,
100
+ runtime: ctx.runtime,
101
+ abortSignal: ctx.abortSignal,
102
+ accountId: "default",
103
+ });
104
+ expect(ctx.setStatus).toHaveBeenCalledWith({ accountId: "default", port: null });
105
+ expect(typeof (started as { stop?: () => Promise<void> }).stop).toBe("function");
106
+
107
+ abortController.abort();
108
+ resolveMonitor();
109
+ await (started as { stop?: () => Promise<void> }).stop?.();
110
+
111
+ expect(stopFeishuMonitorMock).toHaveBeenCalledWith("default");
112
+ });
48
113
  });
package/src/channel.ts CHANGED
@@ -351,19 +351,40 @@ export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount> = {
351
351
  },
352
352
  gateway: {
353
353
  startAccount: async (ctx) => {
354
- const { monitorFeishuProvider } = await import("./monitor.js");
354
+ const { monitorFeishuProvider, stopFeishuMonitor } = await import("./monitor.js");
355
355
  const account = resolveFeishuAccount({ cfg: ctx.cfg, accountId: ctx.accountId });
356
+ const accountId = account.accountId;
356
357
  const port = account.config?.webhookPort ?? null;
357
- ctx.setStatus({ accountId: ctx.accountId, port });
358
+ ctx.setStatus({ accountId, port });
358
359
  ctx.log?.info(
359
- `starting feishu[${ctx.accountId}] (mode: ${account.config?.connectionMode ?? "websocket"})`,
360
+ `starting feishu[${accountId}] (mode: ${account.config?.connectionMode ?? "websocket"})`,
360
361
  );
361
- return monitorFeishuProvider({
362
- config: ctx.cfg,
363
- runtime: ctx.runtime,
364
- abortSignal: ctx.abortSignal,
365
- accountId: ctx.accountId,
366
- });
362
+
363
+ // Start the long-running monitor in the background so service startup can continue
364
+ // into ChannelManager.startAll() for other channels.
365
+ const monitorTask = (async () => {
366
+ try {
367
+ await monitorFeishuProvider({
368
+ config: ctx.cfg,
369
+ runtime: ctx.runtime,
370
+ abortSignal: ctx.abortSignal,
371
+ accountId,
372
+ });
373
+ } catch (error) {
374
+ if (ctx.abortSignal?.aborted) {
375
+ return;
376
+ }
377
+ const message = error instanceof Error ? error.stack ?? error.message : String(error);
378
+ ctx.log?.error?.(`feishu[${accountId}]: gateway monitor stopped unexpectedly: ${message}`);
379
+ }
380
+ })();
381
+
382
+ return {
383
+ stop: async () => {
384
+ stopFeishuMonitor(accountId);
385
+ await monitorTask;
386
+ },
387
+ };
367
388
  },
368
389
  },
369
390
  };