-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeishu-helper.js
More file actions
278 lines (238 loc) · 9.71 KB
/
feishu-helper.js
File metadata and controls
278 lines (238 loc) · 9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
const { fetchWithAuth } = require('../feishu-common/index.js');
var SECRET_PATTERNS = [
/sk-ant-api03-[a-zA-Z0-9\-_]{20,}/,
/ghp_[a-zA-Z0-9]{10,}/,
/xox[baprs]-[a-zA-Z0-9]{10,}/,
/-----BEGIN [A-Z]+ PRIVATE KEY-----/
];
function scanForSecrets(content) {
if (!content) return;
for (var i = 0; i < SECRET_PATTERNS.length; i++) {
if (SECRET_PATTERNS[i].test(content)) {
throw new Error('Aborted send to prevent secret leakage.');
}
}
}
/**
* Parse structured status text into key-value pairs.
* Handles both Chinese and English formats from withOutcomeLine + buildFallbackStatus.
*/
function parseStatusText(text) {
if (!text) return {};
const lines = text.split('\n').map(l => l.trim()).filter(Boolean);
const result = {};
const fullText = lines.join(' ');
const zhGoal = fullText.match(/目标[::]\s*(.+?)(?=[。,]?\s*(?:触发信号|使用基因|影响范围|提交|无可提交|$))/);
const enGoal = fullText.match(/Goal[::]\s*(.+?)(?=[.]?\s*(?:Signals|Gene|Blast radius|Committed|No committable|$))/);
if (zhGoal) { result.goal = zhGoal[1].replace(/[。]$/, '').trim(); result.goalLabel = '目标'; }
else if (enGoal) { result.goal = enGoal[1].replace(/[.]$/, '').trim(); result.goalLabel = 'Goal'; }
const zhSig = fullText.match(/触发信号[::]\s*(.+?)(?=[。]?\s*(?:使用基因|影响范围|提交|无可提交|$))/);
const enSig = fullText.match(/Signals[::]\s*(.+?)(?=[.]?\s*(?:Gene|Blast radius|Committed|No committable|$))/);
if (zhSig) { result.signals = zhSig[1].replace(/[。]$/, '').trim(); result.signalsLabel = '信号'; }
else if (enSig) { result.signals = enSig[1].replace(/[.]$/, '').trim(); result.signalsLabel = 'Signals'; }
const zhGene = fullText.match(/使用基因[::]\s*(.+?)(?=[。]?\s*(?:影响范围|提交|无可提交|$))/);
const enGene = fullText.match(/Gene[::]\s*(.+?)(?=[.]?\s*(?:Blast radius|Committed|No committable|$))/);
if (zhGene) { result.gene = zhGene[1].replace(/[。]$/, '').trim(); result.geneLabel = '基因'; }
else if (enGene) { result.gene = enGene[1].replace(/[.]$/, '').trim(); result.geneLabel = 'Gene'; }
const zhBlast = fullText.match(/影响范围[::]\s*(.+?)(?=[。]?\s*(?:提交|无可提交|$))/);
const enBlast = fullText.match(/Blast radius[::]\s*(.+?)(?=[.]?\s*(?:Committed|No committable|$))/);
if (zhBlast) { result.blast = zhBlast[1].replace(/[。]$/, '').trim(); result.blastLabel = '影响'; }
else if (enBlast) { result.blast = enBlast[1].replace(/[.]$/, '').trim(); result.blastLabel = 'Blast'; }
const zhGit = fullText.match(/提交[::]\s*(.+?)$/);
const enGit = fullText.match(/Committed\s+(.+?)$/);
const noGit = fullText.match(/无可提交代码变更|No committable code diff/);
if (zhGit) { result.git = zhGit[1].replace(/[。]$/, '').trim(); result.gitLabel = 'Git'; }
else if (enGit) { result.git = enGit[1].replace(/[.]$/, '').trim(); result.gitLabel = 'Git'; }
else if (noGit) { result.git = noGit[0]; result.gitLabel = 'Git'; }
return result;
}
function resolveTarget(raw) {
let target = raw;
if (!target && process.env.OPENCLAW_MASTER_ID) {
target = process.env.OPENCLAW_MASTER_ID;
}
if (!target) {
throw new Error("Target ID is required (and OPENCLAW_MASTER_ID env var is not set)");
}
return target;
}
function resolveReceiveIdType(target) {
if (target.startsWith('oc_')) return 'chat_id';
if (target.startsWith('ou_')) return 'open_id';
if (target.includes('@')) return 'email';
return 'open_id';
}
async function postCard(target, cardJson) {
const receiveIdType = resolveReceiveIdType(target);
const payload = {
receive_id: target,
msg_type: 'interactive',
content: JSON.stringify(cardJson)
};
const res = await fetchWithAuth(
'https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=' + receiveIdType,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
}
);
const data = await res.json();
if (data.code !== 0) {
throw new Error('Feishu API Error: ' + data.msg);
}
return data;
}
// v1 card (legacy, kept for backward compat)
async function sendCard({ target: rawTarget, title, text, color, note, cardData }) {
const target = resolveTarget(rawTarget);
var processedText = (text || '').replace(/\\n/g, '\n');
scanForSecrets(processedText);
var elements = [];
if (processedText) {
elements.push({ tag: 'markdown', content: processedText });
}
if (note) {
elements.push({
tag: 'markdown',
content: `<font color='grey'>${String(note)}</font>`
});
}
var card = {
config: { wide_screen_mode: true },
elements: elements
};
if (title) {
card.header = {
title: { tag: 'plain_text', content: title },
template: color || 'blue'
};
} else if (cardData && cardData.header) {
card.header = cardData.header;
}
if (cardData && cardData.elements) {
card.elements = cardData.elements;
}
return postCard(target, card);
}
/**
* Send a v2 structured card for a single evolution cycle report.
*
* Layout:
* Goal (prominent)
* 3 columns: Duration | Files Changed | Lines Added
* [red error block -- only if failed]
* grey footnote: Gene + Signals
* grey footer: uptime / load
*
* @param {Object} opts
* @param {string} opts.target - receive_id
* @param {string} opts.title - card header title (e.g. "Evolution #1234")
* @param {string} opts.color - header template color
* @param {Array} [opts.tags] - header text_tag_list [{text, color}]
* @param {Object} [opts.summary] - { success, duration, filesChanged, linesAdded, errors, errorCount }
* @param {string} [opts.status] - raw evolution status text (parsed for goal/gene/signals)
* @param {string} [opts.footer] - minimal footer text
*/
async function sendStructuredCard(opts) {
const { title, color, tags, summary, status, footer } = opts;
const target = resolveTarget(opts.target);
scanForSecrets(status || '');
const elements = [];
let parsed = {};
if (status) {
parsed = parseStatusText(status);
}
if (parsed.goal) {
elements.push({
tag: 'markdown',
content: `**${parsed.goalLabel || 'Goal'}**: ${parsed.goal}`,
element_id: 'el_goal'
});
} else if (status) {
const cleanedStatus = status.replace(/^(结果|Result)[::]\s*(成功|失败|SUCCESS|FAILED)\s*\n?/i, '').trim();
if (cleanedStatus) {
elements.push({ tag: 'markdown', content: cleanedStatus, element_id: 'el_status' });
}
}
if (summary) {
const dur = summary.duration || '?';
const files = summary.filesChanged != null ? summary.filesChanged : (parsed.blast ? (parsed.blast.match(/(\d+)/) || [])[1] || '0' : '0');
const lines = summary.linesAdded != null ? summary.linesAdded : '0';
elements.push({ tag: 'hr' });
elements.push({
tag: 'column_set',
flex_mode: 'none',
background_style: 'default',
columns: [
{
tag: 'column', width: 'weighted', weight: 1, vertical_align: 'center',
elements: [{
tag: 'markdown', text_align: 'center',
content: `<font color='blue'>${dur}s</font>\nDuration`
}]
},
{
tag: 'column', width: 'weighted', weight: 1, vertical_align: 'center',
elements: [{
tag: 'markdown', text_align: 'center',
content: `<font color='blue'>${files}</font>\nChanged`
}]
},
{
tag: 'column', width: 'weighted', weight: 1, vertical_align: 'center',
elements: [{
tag: 'markdown', text_align: 'center',
content: `<font color='blue'>+${lines}</font>\nLines`
}]
}
]
});
}
if (summary && !summary.success && summary.errors && summary.errors.length > 0) {
elements.push({
tag: 'markdown',
content: `<font color='red'>Error: ${summary.errors[0]}</font>`,
element_id: 'el_error'
});
}
const footnoteParts = [];
if (parsed.gene) footnoteParts.push(`Gene: ${parsed.gene}`);
if (parsed.signals) footnoteParts.push(`Signals: ${parsed.signals}`);
if (footnoteParts.length > 0) {
elements.push({
tag: 'markdown',
content: `<font color='grey'>${footnoteParts.join(' | ')}</font>`,
element_id: 'el_footnote'
});
}
if (footer) {
elements.push({
tag: 'markdown',
content: `<font color='grey'>${footer}</font>`
});
}
const header = {
title: { tag: 'plain_text', content: title || 'Evolution Report' },
template: color || 'blue'
};
if (tags && tags.length > 0) {
header.text_tag_list = tags.slice(0, 3).map((t, i) => ({
tag: 'text_tag',
text: { tag: 'plain_text', content: t.text },
color: t.color || 'neutral',
element_id: `el_tag_${i}`
}));
}
const card = {
schema: '2.0',
config: {
update_multi: true,
width_mode: 'default'
},
header,
body: { elements }
};
return postCard(target, card);
}
module.exports = { sendCard, sendStructuredCard, postCard, parseStatusText };