|
| 1 | +import type * as Lark from "@larksuiteoapi/node-sdk"; |
| 2 | +import type { OpenClawPluginApi } from "openclaw/plugin-sdk"; |
| 3 | +import { listEnabledFeishuAccounts } from "./accounts.js"; |
| 4 | +import { FeishuChatSchema, type FeishuChatParams } from "./chat-schema.js"; |
| 5 | +import { createFeishuClient } from "./client.js"; |
| 6 | +import { resolveToolsConfig } from "./tools-config.js"; |
| 7 | + |
| 8 | +function json(data: unknown) { |
| 9 | + return { |
| 10 | + content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }], |
| 11 | + details: data, |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +async function getChatInfo(client: Lark.Client, chatId: string) { |
| 16 | + const res = await client.im.chat.get({ path: { chat_id: chatId } }); |
| 17 | + if (res.code !== 0) { |
| 18 | + throw new Error(res.msg); |
| 19 | + } |
| 20 | + |
| 21 | + const chat = res.data; |
| 22 | + return { |
| 23 | + chat_id: chatId, |
| 24 | + name: chat?.name, |
| 25 | + description: chat?.description, |
| 26 | + owner_id: chat?.owner_id, |
| 27 | + tenant_key: chat?.tenant_key, |
| 28 | + user_count: chat?.user_count, |
| 29 | + chat_mode: chat?.chat_mode, |
| 30 | + chat_type: chat?.chat_type, |
| 31 | + join_message_visibility: chat?.join_message_visibility, |
| 32 | + leave_message_visibility: chat?.leave_message_visibility, |
| 33 | + membership_approval: chat?.membership_approval, |
| 34 | + moderation_permission: chat?.moderation_permission, |
| 35 | + avatar: chat?.avatar, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +async function getChatMembers( |
| 40 | + client: Lark.Client, |
| 41 | + chatId: string, |
| 42 | + pageSize?: number, |
| 43 | + pageToken?: string, |
| 44 | + memberIdType?: "open_id" | "user_id" | "union_id", |
| 45 | +) { |
| 46 | + const page_size = pageSize ? Math.max(1, Math.min(100, pageSize)) : 50; |
| 47 | + const res = await client.im.chatMembers.get({ |
| 48 | + path: { chat_id: chatId }, |
| 49 | + params: { |
| 50 | + page_size, |
| 51 | + page_token: pageToken, |
| 52 | + member_id_type: memberIdType ?? "open_id", |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + if (res.code !== 0) { |
| 57 | + throw new Error(res.msg); |
| 58 | + } |
| 59 | + |
| 60 | + return { |
| 61 | + chat_id: chatId, |
| 62 | + has_more: res.data?.has_more, |
| 63 | + page_token: res.data?.page_token, |
| 64 | + members: |
| 65 | + res.data?.items?.map((item) => ({ |
| 66 | + member_id: item.member_id, |
| 67 | + name: item.name, |
| 68 | + tenant_key: item.tenant_key, |
| 69 | + member_id_type: item.member_id_type, |
| 70 | + })) ?? [], |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +export function registerFeishuChatTools(api: OpenClawPluginApi) { |
| 75 | + if (!api.config) { |
| 76 | + api.logger.debug?.("feishu_chat: No config available, skipping chat tools"); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + const accounts = listEnabledFeishuAccounts(api.config); |
| 81 | + if (accounts.length === 0) { |
| 82 | + api.logger.debug?.("feishu_chat: No Feishu accounts configured, skipping chat tools"); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const firstAccount = accounts[0]; |
| 87 | + const toolsCfg = resolveToolsConfig(firstAccount.config.tools); |
| 88 | + if (!toolsCfg.chat) { |
| 89 | + api.logger.debug?.("feishu_chat: chat tool disabled in config"); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + const getClient = () => createFeishuClient(firstAccount); |
| 94 | + |
| 95 | + api.registerTool( |
| 96 | + { |
| 97 | + name: "feishu_chat", |
| 98 | + label: "Feishu Chat", |
| 99 | + description: "Feishu chat operations. Actions: members, info", |
| 100 | + parameters: FeishuChatSchema, |
| 101 | + async execute(_toolCallId, params) { |
| 102 | + const p = params as FeishuChatParams; |
| 103 | + try { |
| 104 | + const client = getClient(); |
| 105 | + switch (p.action) { |
| 106 | + case "members": |
| 107 | + return json( |
| 108 | + await getChatMembers( |
| 109 | + client, |
| 110 | + p.chat_id, |
| 111 | + p.page_size, |
| 112 | + p.page_token, |
| 113 | + p.member_id_type, |
| 114 | + ), |
| 115 | + ); |
| 116 | + case "info": |
| 117 | + return json(await getChatInfo(client, p.chat_id)); |
| 118 | + default: |
| 119 | + return json({ error: `Unknown action: ${String(p.action)}` }); |
| 120 | + } |
| 121 | + } catch (err) { |
| 122 | + return json({ error: err instanceof Error ? err.message : String(err) }); |
| 123 | + } |
| 124 | + }, |
| 125 | + }, |
| 126 | + { name: "feishu_chat" }, |
| 127 | + ); |
| 128 | + |
| 129 | + api.logger.info?.("feishu_chat: Registered feishu_chat tool"); |
| 130 | +} |
0 commit comments