@max1874/feishu 0.3.0 → 0.3.2

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": "@max1874/feishu",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "description": "OpenClaw Feishu/Lark channel plugin",
6
6
  "license": "MIT",
package/src/docx.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Type } from "@sinclair/typebox";
2
2
  import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
3
3
  import { createFeishuClient } from "./client.js";
4
+ import { resolveFeishuCredentials, mergeFeishuAccountConfig } from "./accounts.js";
4
5
  import { getConversationContext } from "./runtime.js";
5
6
  import type { FeishuConfig } from "./types.js";
6
7
  import type * as Lark from "@larksuiteoapi/node-sdk";
@@ -887,16 +888,50 @@ const WikiSpaceIdSchema = Type.Object({
887
888
  ),
888
889
  });
889
890
 
891
+ /**
892
+ * Resolve a FeishuConfig that has credentials, checking:
893
+ * 1. Top-level appId/appSecret (legacy / pre-v2026.3.2 layout)
894
+ * 2. The defaultAccount entry under accounts
895
+ * 3. The first account that has valid credentials
896
+ */
897
+ function resolveEffectiveFeishuConfig(cfg: FeishuConfig): FeishuConfig | null {
898
+ // 1. Top-level credentials
899
+ if (resolveFeishuCredentials(cfg)) return cfg;
900
+
901
+ // 2. defaultAccount (e.g. "main")
902
+ const defaultId = (cfg as any).defaultAccount as string | undefined;
903
+ if (defaultId && cfg.accounts?.[defaultId]) {
904
+ const merged = mergeFeishuAccountConfig(cfg, defaultId);
905
+ if (resolveFeishuCredentials(merged)) return merged;
906
+ }
907
+
908
+ // 3. First account with credentials
909
+ for (const id of Object.keys(cfg.accounts ?? {})) {
910
+ const merged = mergeFeishuAccountConfig(cfg, id);
911
+ if (resolveFeishuCredentials(merged)) return merged;
912
+ }
913
+
914
+ return null;
915
+ }
916
+
890
917
  // ============ Tool Registration ============
891
918
 
892
919
  export function registerFeishuDocTools(api: OpenClawPluginApi) {
893
920
  const feishuCfg = api.config?.channels?.feishu as FeishuConfig | undefined;
894
- if (!feishuCfg?.appId || !feishuCfg?.appSecret) {
895
- api.logger.debug?.("feishu_doc: Feishu credentials not configured, skipping doc tools");
921
+ if (!feishuCfg) {
922
+ api.logger.debug?.("feishu_doc: Feishu not configured, skipping doc tools");
923
+ return;
924
+ }
925
+
926
+ // Resolve effective config: try top-level credentials first, then fall back
927
+ // to defaultAccount or the first account that has credentials configured.
928
+ const resolvedCfg = resolveEffectiveFeishuConfig(feishuCfg);
929
+ if (!resolvedCfg) {
930
+ api.logger.debug?.("feishu_doc: Feishu credentials not configured (appId, appSecret required), skipping doc tools");
896
931
  return;
897
932
  }
898
933
 
899
- const getClient = () => createFeishuClient(feishuCfg);
934
+ const getClient = () => createFeishuClient(resolvedCfg);
900
935
 
901
936
  // Tool 1: feishu_doc_read
902
937
  api.registerTool(
package/src/monitor.ts CHANGED
@@ -19,6 +19,32 @@ const wsClients = new Map<string, Lark.WSClient>();
19
19
  const httpServers = new Map<string, http.Server>();
20
20
  const botOpenIds = new Map<string, string>();
21
21
 
22
+ // --- Message dedup ---
23
+ // Feishu may deliver the same event multiple times (webhook retries, websocket reconnects).
24
+ // Track seen message_ids for a short window to skip duplicates.
25
+ const DEDUP_TTL_MS = 10 * 60 * 1000; // 10 minutes
26
+ const seenMessageIds = new Map<string, number>();
27
+
28
+ function isDuplicateMessage(messageId: string | undefined): boolean {
29
+ if (!messageId) return false;
30
+ const now = Date.now();
31
+
32
+ // Lazy cleanup: prune expired entries when map grows
33
+ if (seenMessageIds.size > 200) {
34
+ for (const [id, ts] of seenMessageIds) {
35
+ if (now - ts > DEDUP_TTL_MS) seenMessageIds.delete(id);
36
+ }
37
+ }
38
+
39
+ const seenAt = seenMessageIds.get(messageId);
40
+ if (seenAt !== undefined) {
41
+ if (now - seenAt <= DEDUP_TTL_MS) return true;
42
+ // Expired — treat as new message
43
+ }
44
+ seenMessageIds.set(messageId, now);
45
+ return false;
46
+ }
47
+
22
48
  async function fetchBotOpenId(cfg: FeishuConfig): Promise<string | undefined> {
23
49
  try {
24
50
  const result = await probeFeishu(cfg);
@@ -88,6 +114,10 @@ async function monitorWebSocket(params: {
88
114
  "im.message.receive_v1": async (data) => {
89
115
  try {
90
116
  const event = data as unknown as FeishuMessageEvent;
117
+ if (isDuplicateMessage(event.message?.message_id)) {
118
+ log(`feishu[${accountId}]: skipping duplicate message ${event.message.message_id}`);
119
+ return;
120
+ }
91
121
  await handleFeishuMessage({
92
122
  cfg,
93
123
  event,
@@ -199,6 +229,10 @@ async function monitorWebhook(params: {
199
229
  "im.message.receive_v1": async (data) => {
200
230
  try {
201
231
  const event = data as unknown as FeishuMessageEvent;
232
+ if (isDuplicateMessage(event.message?.message_id)) {
233
+ log(`feishu[${accountId}]: skipping duplicate message ${event.message.message_id}`);
234
+ return;
235
+ }
202
236
  await handleFeishuMessage({
203
237
  cfg,
204
238
  event,