@vui-design/openclaw-plugin-feishu-progress 0.2.1 → 0.2.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/package.json +1 -1
- package/src/feishu-client.ts +14 -38
- package/src/index.ts +6 -4
package/package.json
CHANGED
package/src/feishu-client.ts
CHANGED
|
@@ -35,7 +35,13 @@ export class FeishuClient {
|
|
|
35
35
|
}),
|
|
36
36
|
}
|
|
37
37
|
);
|
|
38
|
-
|
|
38
|
+
if (!res.ok) {
|
|
39
|
+
throw new Error(`飞书获取 token 失败: HTTP ${res.status}`);
|
|
40
|
+
}
|
|
41
|
+
const data = (await res.json()) as { tenant_access_token: string; expire: number; code: number; msg: string };
|
|
42
|
+
if (data.code !== 0) {
|
|
43
|
+
throw new Error(`飞书获取 token 失败: ${data.msg} (code=${data.code})`);
|
|
44
|
+
}
|
|
39
45
|
// 提前 5 分钟过期,避免边界问题
|
|
40
46
|
this.tokenCache = {
|
|
41
47
|
token: data.tenant_access_token,
|
|
@@ -44,27 +50,6 @@ export class FeishuClient {
|
|
|
44
50
|
return this.tokenCache.token;
|
|
45
51
|
}
|
|
46
52
|
|
|
47
|
-
async sendCard(chatId: string, card: object): Promise<string> {
|
|
48
|
-
const token = await this.getToken();
|
|
49
|
-
const res = await fetch(
|
|
50
|
-
`${this.baseUrl}/open-apis/im/v1/messages?receive_id_type=chat_id`,
|
|
51
|
-
{
|
|
52
|
-
method: 'POST',
|
|
53
|
-
headers: {
|
|
54
|
-
Authorization: `Bearer ${token}`,
|
|
55
|
-
'Content-Type': 'application/json',
|
|
56
|
-
},
|
|
57
|
-
body: JSON.stringify({
|
|
58
|
-
receive_id: chatId,
|
|
59
|
-
msg_type: 'interactive',
|
|
60
|
-
content: JSON.stringify(card),
|
|
61
|
-
}),
|
|
62
|
-
}
|
|
63
|
-
);
|
|
64
|
-
const data = (await res.json()) as { data: { message_id: string } };
|
|
65
|
-
return data.data.message_id;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
53
|
async sendText(chatId: string, text: string): Promise<string> {
|
|
69
54
|
const token = await this.getToken();
|
|
70
55
|
const res = await fetch(
|
|
@@ -82,22 +67,13 @@ export class FeishuClient {
|
|
|
82
67
|
}),
|
|
83
68
|
}
|
|
84
69
|
);
|
|
85
|
-
|
|
70
|
+
if (!res.ok) {
|
|
71
|
+
throw new Error(`飞书发送消息失败: HTTP ${res.status}`);
|
|
72
|
+
}
|
|
73
|
+
const data = (await res.json()) as { code: number; msg: string; data: { message_id: string } };
|
|
74
|
+
if (data.code !== 0) {
|
|
75
|
+
throw new Error(`飞书发送消息失败: ${data.msg} (code=${data.code})`);
|
|
76
|
+
}
|
|
86
77
|
return data.data.message_id;
|
|
87
78
|
}
|
|
88
|
-
|
|
89
|
-
async updateCard(messageId: string, card: object): Promise<void> {
|
|
90
|
-
const token = await this.getToken();
|
|
91
|
-
await fetch(`${this.baseUrl}/open-apis/im/v1/messages/${messageId}`, {
|
|
92
|
-
method: 'PATCH',
|
|
93
|
-
headers: {
|
|
94
|
-
Authorization: `Bearer ${token}`,
|
|
95
|
-
'Content-Type': 'application/json',
|
|
96
|
-
},
|
|
97
|
-
body: JSON.stringify({
|
|
98
|
-
msg_type: 'interactive',
|
|
99
|
-
content: JSON.stringify(card),
|
|
100
|
-
}),
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
79
|
}
|
package/src/index.ts
CHANGED
|
@@ -47,34 +47,36 @@ export default {
|
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
// ── 1. 收到用户消息时,记录当前会话对应的飞书 chat_id ────────────────────
|
|
50
|
-
// ctx: PluginHookMessageContext → 有 channelId / accountId / conversationId,无 sessionKey
|
|
51
50
|
api.on('message_received', async (_event: any, ctx: any) => {
|
|
51
|
+
api.logger.info(`[feishu-progress][DEBUG] message_received: channelId=${ctx.channelId} accountId=${ctx.accountId} conversationId=${ctx.conversationId}`);
|
|
52
52
|
if (ctx.channelId !== 'feishu') return;
|
|
53
53
|
if (!ctx.conversationId) return;
|
|
54
54
|
const key = ctx.accountId ?? 'default';
|
|
55
55
|
sessionChatMap.set(key, ctx.conversationId);
|
|
56
|
+
api.logger.info(`[feishu-progress][DEBUG] stored: key=${key} chatId=${ctx.conversationId}`);
|
|
56
57
|
});
|
|
57
58
|
|
|
58
59
|
// ── 2. 每次工具调用前,自动推送进度到飞书 ──────────────────────────────
|
|
59
|
-
// ctx: PluginHookToolContext → 有 agentId / sessionKey / runId,无 conversationId
|
|
60
|
-
// agentId 与 message_received 的 accountId 通过 bindings 对应,用作关联 key
|
|
61
60
|
api.on('before_tool_call', async (event: any, ctx: any) => {
|
|
61
|
+
api.logger.info(`[feishu-progress][DEBUG] before_tool_call: tool=${event.toolName} agentId=${ctx.agentId} sessionKey=${ctx.sessionKey}`);
|
|
62
62
|
const key = ctx.agentId ?? ctx.sessionKey ?? 'default';
|
|
63
63
|
const chatId = sessionChatMap.get(key);
|
|
64
|
+
api.logger.info(`[feishu-progress][DEBUG] lookup: key=${key} chatId=${chatId} mapSize=${sessionChatMap.size}`);
|
|
64
65
|
if (!chatId) return;
|
|
65
66
|
|
|
66
67
|
const label = toolLabel(event.toolName);
|
|
67
68
|
try {
|
|
68
69
|
await client.sendText(chatId, `🔧 正在执行:${label}`);
|
|
70
|
+
api.logger.info(`[feishu-progress][DEBUG] sendText OK: chatId=${chatId} label=${label}`);
|
|
69
71
|
} catch (err: any) {
|
|
70
72
|
api.logger?.warn(`[feishu-progress] 推送失败: ${err?.message}`);
|
|
71
73
|
}
|
|
72
74
|
});
|
|
73
75
|
|
|
74
76
|
// ── 3. 任务结束后清理 session 映射 ───────────────────────────────────────
|
|
75
|
-
// ctx: PluginHookAgentContext → 有 agentId / sessionKey
|
|
76
77
|
api.on('agent_end', async (_event: any, ctx: any) => {
|
|
77
78
|
const key = ctx.agentId ?? ctx.sessionKey ?? 'default';
|
|
79
|
+
api.logger.info(`[feishu-progress][DEBUG] agent_end: key=${key}`);
|
|
78
80
|
sessionChatMap.delete(key);
|
|
79
81
|
});
|
|
80
82
|
},
|