Repository navigation
Expand file tree
/
Copy pathrooms.js
More file actions
719 lines (666 loc) · 32.2 KB
/
Copy pathrooms.js
File metadata and controls
719 lines (666 loc) · 32.2 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
2
8000
40
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
let roomsPollTimer = 0;
function roomProjectId() {
return state.projectFilter || '';
}
function roomWorkflowChoices() {
return (state.data?.workflows || []).filter(w => catalogGeneric(w)
? w.status !== 'draft'
: w.status === 'published' && (catalogOwned(w.projectId) || catalogShared(w.projectId, w.shared)));
}
function roomWorkflowLabel(w) {
return (w.name || w.id) + (catalogGeneric(w) ? '(通用)' : '');
}
function activeRoom() {
const rs = roomState();
return (rs.items || []).find(item => item.id === rs.activeId) || null;
}
function roomGreeting() {
const hour = new Date().getHours();
const part = hour < 6 ? '凌晨好' : hour < 11 ? '早上好' : hour < 14 ? '中午好' : hour < 18 ? '下午好' : '晚上好';
const user = state.currentUser;
const name = user && !user.isGuest ? (user.chineseName || user.loginName || '') : '';
return name ? part + ',' + name : part;
}
function roomState() {
if (!state.rooms) state.rooms = {items: [], activeId: '', sops: [], active: false, mode: 'chat'};
if (!Array.isArray(state.rooms.sops)) state.rooms.sops = [];
if (state.rooms.mode !== 'issue') state.rooms.mode = 'chat';
return state.rooms;
}
function setRoomMode(mode) {
roomState().mode = mode === 'issue' ? 'issue' : 'chat';
syncRoomComposer();
$('#roomInput')?.focus();
}
function roomRunTerminal(status) {
return status === 'succeeded' || status === 'failed' || status === 'cancelled';
}
function bindRooms() {
const create = $('#newRoom');
const form = $('#roomComposer');
if (!create || create.dataset.bound) return;
create.dataset.bound = '1';
create.>
$('#renameRoom').>
$('#roomModeChat'). => setRoomMode('chat');
$('#roomModeIssue'). => setRoomMode('issue');
form. => {
event.preventDefault();
submitRoomComposer();
};
$('#roomDispatch'). => {
if (roomTargetPicked()) dispatchRoomIssue();
else openRoomMention({targetsOnly: true});
};
const input = $('#roomInput');
input.addEventListener('keydown', event => {
if (roomMentionKey(event)) return;
if (event.key === 'Enter' && !event.shiftKey && !event.isComposing) {
event.preventDefault();
submitRoomComposer();
}
});
input.addEventListener('input', () => {
syncRoomComposer();
updateRoomMention();
});
input.addEventListener('click', updateRoomMention);
document.addEventListener('mousedown', event => {
if (!event.target.closest?.('.room-composer-box')) closeRoomMention();
});
$('#roomSOP').addEventListener('change', event => {
const id = event.target.value;
if (id) toggleRoomSOP(id, true);
event.target.value = '';
});
document.addEventListener('change', event => {
if (event.target?.id === 'projectSwitcher' && state.currentView === 'rooms') loadRooms();
});
}
function toggleRoomSOP(id, force) {
const rs = roomState();
const has = rs.sops.includes(id);
const want = force === undefined ? !has : force;
if (want && !has) {
if (rs.sops.length >= 5) return toast('一次最多派给 5 条 SOP');
rs.sops.push(id);
}
if (!want && has) rs.sops = rs.sops.filter(item => item !== id);
if ($('#roomSOPList')) fillRoomSOPList();
else syncRoomComposer();
}
function roomTargetPicked() {
return roomState().sops.length > 0;
}
function syncRoomComposer() {
const rs = roomState();
const issue = rs.mode === 'issue';
const names = new Map(roomWorkflowChoices().map(w => [w.id, roomWorkflowLabel(w)]));
rs.sops = rs.sops.filter(id => names.has(id));
const picks = $('#roomSOPPicks');
if (picks) {
picks.innerHTML = rs.sops.map(id => `<button type="button" class="room-pick" data-unpick="${escapeHTML(id)}" title="移除">${escapeHTML(names.get(id))}<i>×</i></button>`).join('');
picks.querySelectorAll('[data-unpick]').forEach(button => { button. => toggleRoomSOP(button.dataset.unpick, false); });
}
$('#roomSOP')?.closest('.room-chip')?.classList.toggle('on', rs.sops.length > 0);
$('#roomComposer')?.classList.toggle('is-issue', issue);
$('#roomModeChat')?.classList.toggle('on', !issue);
$('#roomModeIssue')?.classList.toggle('on', issue);
const input = $('#roomInput');
if (input) input.placeholder = issue ? '第一行写 Issue 标题,下面写描述。输入 @ 选择 SOP' : '发消息,输入 @ 提醒同事';
const picked = roomTargetPicked();
const dispatch = $('#roomDispatch');
if (dispatch) {
dispatch.textContent = !picked ? '建单并派发' : rs.sops.length > 1 ? `建单并派发 ${rs.sops.length} 条 SOP` : '建单并派发';
dispatch.title = picked ? '第一行作为 Issue 标题,其余作为描述' : '先选一条 SOP';
}
const hint = $('#roomTargetHint');
if (hint) {
hint.textContent = !picked
? '选择一条或多条 SOP。第一行是标题,回车建单。'
: (rs.sops.length > 1 ? `回车将并行派给 ${rs.sops.length} 条 SOP。` : `回车将建单并派给 SOP「${names.get(rs.sops[0])}」。`);
}
$('#roomSend')?.classList.toggle('ready', !!input?.value.trim());
document.querySelectorAll('[data-room-pick]').forEach(button => {
button.classList.toggle('picked', rs.sops.includes(button.dataset.roomPick));
});
}
async function loadRooms() {
bindRooms();
const rs = roomState();
const projectId = roomProjectId();
fillRoomTargets();
if (!projectId) {
renderRoomList([]);
renderRoomThread(null, []);
renderRoomResources(null);
return;
}
try {
const rooms = await api('/api/rooms?projectId=' + encodeURIComponent(projectId), {silent: true});
rs.items = rooms || [];
if (!rs.items.some(room => room.id === rs.activeId)) rs.activeId = rs.items[0]?.id || '';
renderRoomList(rs.items);
await openRoom(rs.activeId);
} catch (error) {
toast(error.message);
}
}
// Poll faster while a dispatched run is still moving so progress feels live.
// Idle rooms back off to 15s; the server only reloads runs whose revision moved.
function scheduleRoomPoll() {
clearTimeout(roomsPollTimer);
const delay = roomState().active ? 4000 : 15000;
roomsPollTimer = setTimeout(async () => {
if (state.currentView === 'rooms' && roomState().activeId && !document.hidden) await refreshRoomMessages();
if (state.currentView === 'rooms') scheduleRoomPoll();
}, delay);
}
function fillRoomTargets() {
const sop = $('#roomSOP');
if (!sop) return;
sop.innerHTML = '<option value="">添加 SOP…</option>' + roomWorkflowChoices().map(item => `<option value="${escapeHTML(item.id)}">${escapeHTML(roomWorkflowLabel(item))}</option>`).join('');
syncRoomComposer();
}
function renderRoomList(rooms) {
const host = $('#roomList');
const count = $('#roomCount');
if (count) count.textContent = String(rooms.length);
if (!host) return;
if (!rooms.length) {
host.innerHTML = '<div class="room-empty"><b>还没有群</b><p>点右上角 + 建一个群</p></div>';
return;
}
host.innerHTML = rooms.map(room => `<button type="button" class="room-item ${room.id === roomState().activeId ? 'active' : ''}" data-room="${escapeHTML(room.id)}"><i>#</i><span>${escapeHTML(room.name)}</span><small title="群成员(即项目成员)">${(room.members || []).length} 人</small></button>`).join('');
host.querySelectorAll('[data-room]').forEach(button => {
button. => openRoom(button.dataset.room);
});
}
async function openRoom(id, keepScroll) {
const rs = roomState();
rs.activeId = id || '';
renderRoomList(rs.items || []);
const room = (rs.items || []).find(item => item.id === id);
renderRoomResources(room);
const title = $('#roomTitle');
const subtitle = $('#roomSubtitle');
const rename = $('#renameRoom');
if (title) title.textContent = room?.name || '选择一个群';
if (subtitle) subtitle.textContent = room ? `${(room.members || []).length} 位项目成员` : '先在左侧选择或新建一个群';
if (rename) rename.hidden = !room;
if (!id) {
renderRoomThread(null, []);
return;
}
try {
const messages = await api('/api/rooms/' + encodeURIComponent(id) + '/messages', {silent: true});
renderRoomThread(room, messages || [], keepScroll);
} catch (error) {
toast(error.message);
}
scheduleRoomPoll();
}
if (!window.__roomsVisibilityBound) {
window.__roomsVisibilityBound = true;
document.addEventListener('visibilitychange', () => {
if (!document.hidden && state.currentView === 'rooms' && roomState().activeId) {
refreshRoomMessages().then(scheduleRoomPoll);
}
});
}
async function refreshRoomMessages() {
const rs = roomState();
const id = rs.activeId;
const log = $('#roomLog');
if (!id || !log) return;
const near = log.scrollHeight - log.scrollTop - log.clientHeight < 80;
try {
const messages = await api('/api/rooms/' + encodeURIComponent(id) + '/messages', {silent: true});
if (rs.activeId !== id) return;
const room = (rs.items || []).find(item => item.id === id);
renderRoomThread(room, messages || [], !near);
} catch {}
}
function roomResGroup(title, count, body) {
return `<section class="room-res-group"><div class="room-res-head"><span>${escapeHTML(title)}</span><em>${count}</em></div>${body || '<div class="room-res-more">暂无</div>'}</section>`;
}
const ROOM_MEMBER_ROLE_LABELS = {owner: '群主', maintainer: '维护者', member: '成员', viewer: '只读'};
function roomMemberRoleLabel(role) {
const key = String(role || '').trim().toLowerCase();
return ROOM_MEMBER_ROLE_LABELS[key] || role || '成员';
}
function roomResItem(icon, iconClass, name, sub, pick) {
const tag = pick ? 'button' : 'div';
const attrs = pick ? ` type="button" data-room-pick="${escapeHTML(pick)}" title="点击选为派发对象"` : '';
return `<${tag} class="room-res-item ${pick ? '' : 'static'}"${attrs}><span class="room-res-icon ${iconClass}">${escapeHTML(icon)}</span><span class="room-res-text"><b>${escapeHTML(name)}</b>${sub ? `<small>${escapeHTML(sub)}</small>` : ''}</span></${tag}>`;
}
function renderRoomResources(room) {
const host = $('#roomResources');
if (!host) return;
if (!roomProjectId()) {
host.innerHTML = '<div class="room-empty"><b>先选择项目</b><p>选好项目后,这里会列出群成员,SOP 可以搜索后派发。</p></div>';
return;
}
const members = room?.members || [];
const workflows = roomWorkflowChoices();
host.innerHTML = [
roomResGroup('成员', members.length, members.length
? members.map(m => `<div class="room-res-item static"><span class="room-res-text"><b>${escapeHTML(m.name || ('用户 ' + m.userId))}<span class="room-member-tag">${escapeHTML(roomMemberRoleLabel(m.role))}</span></b></span></div>`).join('')
: '<div class="room-res-more">这个项目还没有其他成员</div>'),
roomResGroup('SOP', workflows.length, workflows.length
? `<input id="roomSOPSearch" class="room-sop-search" type="search" placeholder="搜索要派发的 SOP" value="${escapeHTML(roomState().sopQuery || '')}"><div id="roomSOPList"></div>`
: roomNoSOPHint())
].join('');
const search = $('#roomSOPSearch');
if (search) {
search. => {
roomState().sopQuery = search.value;
fillRoomSOPList();
};
}
fillRoomSOPList();
bindRoomGoSOPs(host);
}
function fillRoomSOPList() {
const list = $('#roomSOPList');
if (!list) return;
const rs = roomState();
const query = String(rs.sopQuery || '').trim().toLowerCase();
const picked = new Set(rs.sops);
const all = roomWorkflowChoices();
const rest = all.filter(w => !picked.has(w.id));
const shown = query
? all.filter(w => roomWorkflowLabel(w).toLowerCase().includes(query) || String(w.id).toLowerCase().includes(query))
: (rs.sopExpanded ? all : all.filter(w => picked.has(w.id)).concat(rest.slice(0, 4)));
const hidden = query || rs.sopExpanded ? 0 : Math.max(0, rest.length - 4);
const toggle = !query && rest.length > 4
? `<button type="button" class="room-res-more room-res-expand" data-room-sop-expand="${rs.sopExpanded ? 'collapse' : 'expand'}">${rs.sopExpanded ? '收起' : '展开其余 ' + hidden + ' 条'}</button>`
: '';
list.innerHTML = (shown.length
? shown.map(w => roomResItem('≡', 'sop', roomWorkflowLabel(w), (w.nodes || []).length ? (w.nodes.length + ' 个节点') : '点选后进入建单', w.id)).join('')
: '<div class="room-res-more">没有匹配的 SOP</div>')
+ toggle;
list.querySelectorAll('[data-room-pick]').forEach(button => {
button. => {
setRoomMode('issue');
toggleRoomSOP(button.dataset.roomPick);
};
});
list.querySelector('[data-room-sop-expand]')?.addEventListener('click', event => {
rs.sopExpanded = event.currentTarget.dataset.roomSopExpand === 'expand';
fillRoomSOPList();
});
syncRoomComposer();
}
function renderRoomThread(room, messages, keepScroll) {
const log = $('#roomLog');
if (!log) return;
const previous = log.scrollTop;
const rs = roomState();
rs.active = messages.some(m => m.kind === 'dispatch' && m.runId && !m.runFinal && !roomRunTerminal(m.runStatus || ''));
// Polls usually return the same thread; skip the rebuild so open 产出摘要,
// text selection and scroll position survive.
const signature = (room?.id || '') + '|' + JSON.stringify(messages);
if (signature === rs.threadSignature && log.childElementCount) return;
rs.threadSignature = signature;
if (!room) {
log.innerHTML = `<div class="room-hero"><h2>${escapeHTML(roomGreeting())}</h2><p>${roomProjectId() ? '在左侧新建或选择一个群开始协作' : '先在顶栏选择项目'}</p></div>`;
return;
}
const mine = Number(state.currentUser?.id) || 0;
let prevRun = null;
const dispatchRuns = new Set(messages.filter(m => m.kind === 'dispatch' && m.runId).map(m => m.runId));
const visible = messages.filter(message => {
if (message.kind === 'progress' && message.nodeId && message.nodeStatus === 'succeeded') return false;
if (message.kind === 'progress' && !message.nodeId && dispatchRuns.has(message.runId)) return false;
return true;
});
log.innerHTML = visible.map(message => {
const html = roomMessageHTML(message, mine, prevRun);
if (['dispatch', 'progress', 'approval'].includes(message.kind)) prevRun = message;
else if (message.kind !== 'decision') prevRun = null;
return html;
}).join('') || roomGuideHTML();
log.querySelector('[data-room-try-chat]')?.addEventListener('click', () => setRoomMode('chat'));
log.querySelector('[data-room-try-issue]')?.addEventListener('click', () => {
setRoomMode('issue');
if (!roomTargetPicked()) openRoomMention({targetsOnly: true});
});
bindRoomGoSOPs(log);
log.querySelectorAll('[data-open-issue]').forEach(button => {
button. => openIssueDetail(button.dataset.openIssue);
});
log.querySelectorAll('[data-open-run]').forEach(button => {
button. => { location.hash = '#/runs/' + encodeURIComponent(button.dataset.openRun); };
});
log.querySelectorAll('[data-decide]').forEach(button => {
button. => decideRoomStep(button.dataset.decide, button.dataset.run, button.dataset.node, button.dataset.name);
});
if (keepScroll) log.scrollTop = previous;
else log.scrollTop = log.scrollHeight;
}
function roomRunStatusLabel(status) {
return {running: '运行中', waiting: '等待处理', succeeded: '已完成', failed: '失败', cancelled: '已取消', queued: '排队中'}[status] || status || '排队中';
}
function roomIsSOPAuthor(name) {
return /^SOP「.*」$/.test(String(name || ''));
}
function roomAuthorName(name) {
return roomIsSOPAuthor(name) ? String(name).slice(4, -1) : String(name || '');
}
function roomAvatar(name) {
if (roomIsSOPAuthor(name)) return '<span class="room-avatar sop">SOP</span>';
return `<span class="room-avatar">${escapeHTML(String(name || '?').slice(0, 1))}</span>`;
}
function roomDispatchHTML(message, author) {
const failed = message.dispatchError ? `<p class="room-card-error">派发未成功:${escapeHTML(message.dispatchError)}</p>` : '';
const key = message.issueNumber ? '#' + message.issueNumber + ' ' : '';
let run = '';
if (message.runId) {
const total = message.runTotal || 0;
const done = message.runCompleted || 0;
const pct = total ? Math.round(done * 100 / total) : 0;
const status = message.runStatus || message.runFinal || 'queued';
const current = (message.runCurrent || []).length ? '正在:' + message.runCurrent.join('、') : '';
run = `<div class="room-run ${escapeHTML(status)}"><div class="room-run-head"><b>${escapeHTML(message.workflowName || 'SOP')}</b><span>${escapeHTML(roomRunStatusLabel(status))}${total ? ` · ${done}/${total} 步` : ''}</span></div><div class="room-run-track"><i style="width:${pct}%"></i></div>${current ? `<small>${escapeHTML(current)}</small>` : ''}</div>`;
} else if (!message.dispatchError && !message.workflowId) {
run = '<small class="room-card-note">当时只指定了角色,没有启动 SOP</small>';
}
const head = message.lane ? `${author} 并行派发` : `${author} 建了 Issue`;
const actions = `<div class="room-card-actions"><button type="button" class="button ghost small" data-open-issue="${escapeHTML(message.issueId)}">打开 Issue</button>${message.runId ? `<button type="button" class="button ghost small" data-open-run="${escapeHTML(message.runId)}">查看运行</button>` : ''}</div>`;
return `<article class="room-message room-card ${message.dispatchError ? 'failed' : ''}"><header>${head}</header><b>${escapeHTML(key + (message.issueTitle || 'Issue'))}</b>${message.lane ? '' : `<p>派给 ${escapeHTML(message.targetName || '未指定')}</p>`}${message.body ? `<p>${escapeHTML(message.body)}</p>` : ''}${failed}${run}${actions}</article>`;
}
function roomProgressHTML(message, prev) {
const status = message.nodeStatus || '';
const icon = status === 'succeeded' ? '✓' : status === 'failed' ? '✗' : status === 'cancelled' ? '■' : '•';
const lines = String(message.body || '').split('\n');
const head = lines.shift();
const rest = lines.join('\n').trim();
const detail = rest ? `<details><summary>产出摘要</summary><p>${escapeHTML(rest)}</p></details>` : '';
const final = !message.nodeId;
// Consecutive steps of one run already sit under its card; repeat the
// issue / SOP only when the thread switches to another run or chat.
const sameRun = prev && prev.runId && prev.runId === message.runId;
const sop = roomIsSOPAuthor(message.author);
const where = sameRun ? '' : [message.issueNumber ? '#' + message.issueNumber : '', sop ? '' : message.workflowName || ''].filter(Boolean).join(' · ');
return `<article class="room-progress ${escapeHTML(status)} ${final ? 'final' : ''}">${roomAvatar(message.author)}<div><header><b>${escapeHTML(roomAuthorName(message.author) || 'SOP')}</b>${where ? `<span>${escapeHTML(where)}</span>` : ''}</header><p><i>${icon}</i>${escapeHTML(head)}</p>${detail}</div></article>`;
}
function roomApprovalHTML(message) {
const attrs = `data-run="${escapeHTML(message.runId)}" data-node="${escapeHTML(message.nodeId)}" data-name="${escapeHTML(message.nodeName || message.nodeId)}"`;
if (message.resolved) {
return `<div class="room-decision">${escapeHTML(message.nodeName || '审批')} 已处理 · <button type="button" class="room-link" data-open-run="${escapeHTML(message.runId)}">看运行</button></div>`;
}
const actions = `<div class="room-card-actions"><button type="button" class="button primary small" data-decide="approve" ${attrs}>批准</button>${message.canReject ? `<button type="button" class="button ghost small" data-decide="reject" ${attrs}>打回</button>` : ''}<button type="button" class="button ghost small" data-open-run="${escapeHTML(message.runId)}">看运行</button></div>`;
const where = [message.issueNumber ? '#' + message.issueNumber : '', message.issueTitle || ''].filter(Boolean).join(' ');
return `<article class="room-message room-card room-approval ${message.resolved ? 'resolved' : ''}"><header>${escapeHTML(roomAuthorName(message.author) || 'SOP')} · ${escapeHTML(where)}</header><b>${message.resolved ? '✓' : '⏸'} ${escapeHTML(message.nodeName || '')} ${message.resolved ? '审批已处理' : '等待审批'}</b>${message.resolved ? '' : `<p>${escapeHTML(message.body || '')}</p>`}${actions}</article>`;
}
function roomMessageHTML(message, mine, prev) {
const own = mine > 0 && Number(message.userId) === mine;
const author = escapeHTML(message.author || '群成员');
switch (message.kind) {
case 'dispatch': return roomDispatchHTML(message, author);
case 'progress': return roomProgressHTML(message, prev);
case 'approval': return roomApprovalHTML(message);
case 'decision': return `<div class="room-decision"><b>${author}</b> ${escapeHTML(message.body || '')}</div>`;
}
return `<article class="room-message ${own ? 'mine' : ''}"><header>${author}</header><p>${roomTextHTML(message.body || '')}</p></article>`;
}
// roomTextHTML escapes a chat line and highlights @mentions; mentions of the
// current user get an extra class so they stand out.
function roomTextHTML(text) {
const me = String(state.currentUser?.loginName || '').toLowerCase();
const names = (activeRoom()?.members || []).map(m => escapeHTML(m.name || '')).filter(Boolean)
.sort((a, b) => b.length - a.length)
.map(name => name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
const pattern = new RegExp(`(^|\\s)@(${names.length ? names.join('|') + '|' : ''}[A-Za-z0-9_.-]+)`, 'g');
return escapeHTML(text).replace(pattern, (all, lead, name) => {
const mine = me && name.toLowerCase().startsWith(me);
return `${lead}<span class="room-at${mine ? ' me' : ''}">@${name}</span>`;
});
}
function roomNoSOPHint() {
return '<div class="room-res-more">当前项目还没有可派发的 SOP。<button type="button" class="room-link" data-room-go-sops>去「SOP 流程」新建或复制一条</button></div>';
}
function bindRoomGoSOPs(host) {
host?.querySelectorAll('[data-room-go-sops]').forEach(button => {
button. => switchView('sops');
});
}
function roomGuideHTML() {
const noSOP = !roomWorkflowChoices().length;
return `<div class="room-guide">
<h2>${escapeHTML(roomGreeting())}</h2>
<p class="room-guide-lead">在这里聊天,或者把一条需求派给 SOP。角色写在流程里,不用再选。</p>
<ol>
<li><b>聊天</b><span>回车发送,Shift+Enter 换行。<code>@</code> 用来提醒同事。</span></li>
<li><b>建单</b><span>切到「建单」,选一条或多条 SOP,第一行写标题,回车就派发。</span></li>
</ol>
<div class="room-guide-actions"><button type="button" class="button ghost small" data-room-try-chat>发一条消息</button><button type="button" class="button primary small" data-room-try-issue>建一张单</button>${noSOP ? '<button type="button" class="button ghost small" data-room-go-sops>当前项目还没有 SOP,去流程模板</button>' : ''}</div>
</div>`;
}
function roomMentionState() {
const rs = roomState();
if (!rs.mention) rs.mention = {open: false, items: [], index: 0, start: -1, targetsOnly: false};
return rs.mention;
}
function roomMentionToken(input) {
const before = input.value.slice(0, input.selectionStart);
const match = before.match(/(^|\s)@([^\s@]{0,30})$/);
return match ? {start: before.length - match[2].length - 1, query: match[2]} : null;
}
function roomMentionItems(query, targetsOnly) {
const q = String(query || '').trim().toLowerCase();
const hit = (...values) => !q || values.some(value => String(value || '').toLowerCase().includes(q));
const items = [];
const wantSOP = targetsOnly || roomState().mode === 'issue';
if (wantSOP) {
roomWorkflowChoices().forEach(w => {
if (hit(w.name, w.id)) items.push({kind: 'sop', id: w.id, name: roomWorkflowLabel(w), sub: (w.nodes || []).length ? w.nodes.length + ' 个步骤,派发后立即运行' : '派发后立即运行'});
});
}
if (!targetsOnly) {
(activeRoom()?.members || []).forEach(m => {
if (hit(m.name)) items.push({kind: 'member', id: String(m.userId), name: m.name || '用户 ' + m.userId, sub: roomMemberRoleLabel(m.role)});
});
}
return items.slice(0, 40);
}
function openRoomMention({targetsOnly = false, token = null} = {}) {
const ms = roomMentionState();
ms.open = true;
ms.targetsOnly = targetsOnly;
ms.start = token ? token.start : -1;
ms.items = roomMentionItems(token?.query || '', targetsOnly);
ms.index = 0;
renderRoomMention(token?.query || '');
}
function closeRoomMention() {
const ms = roomMentionState();
ms.targetsOnly = false;
if (!ms.open) return;
ms.open = false;
const box = $('#roomMention');
if (box) box.hidden = true;
}
function updateRoomMention() {
const input = $('#roomInput');
if (!input) return;
const token = roomMentionToken(input);
if (token && (!token.query || roomMentionItems(token.query, false).length)) openRoomMention({token});
else if (!roomMentionState().targetsOnly) closeRoomMention();
}
function renderRoomMention(query) {
const box = $('#roomMention');
const ms = roomMentionState();
if (!box) return;
const groups = [['sop', 'SOP · 建单并运行'], ['member', '成员 · 提醒']];
let html = ms.targetsOnly ? '<div class="room-mention-title">选这条 Issue 要跑的 SOP</div>' : '';
groups.forEach(([kind, label]) => {
const rows = ms.items.map((item, index) => ({item, index})).filter(row => row.item.kind === kind);
if (!rows.length && !(kind === 'sop' && !query && !roomWorkflowChoices().length)) return;
html += `<div class="room-mention-group">${escapeHTML(label)}</div>`;
if (!rows.length) html += roomNoSOPHint();
html += rows.map(({item, index}) => `<button type="button" class="room-mention-item ${index === ms.index ? 'active' : ''}" data-mention="${index}"><span class="room-res-icon ${item.kind === 'member' ? '' : item.kind}">${escapeHTML(item.kind === 'sop' ? '≡' : (item.name || '?').slice(0, 1))}</span><span><b>${escapeHTML(item.name)}</b><small>${escapeHTML(item.sub || '')}</small></span></button>`).join('');
});
if (!ms.items.length && !html.includes('room-mention-group')) html += `<div class="room-mention-empty">没有匹配「${escapeHTML(query)}」的${roomState().mode === 'issue' || ms.targetsOnly ? ' SOP 或成员' : '成员'}</div>`;
box.innerHTML = html;
box.hidden = false;
box.querySelectorAll('[data-mention]').forEach(button => {
button. => event.preventDefault();
button. => pickRoomMention(ms.items[Number(button.dataset.mention)]);
});
bindRoomGoSOPs(box);
box.querySelector('.room-mention-item.active')?.scrollIntoView({block: 'nearest'});
}
function roomMentionKey(event) {
const ms = roomMentionState();
if (!ms.open || event.isComposing) return false;
if (event.key === 'Escape') {
event.preventDefault();
ms.targetsOnly = false;
closeRoomMention();
return true;
}
if (!ms.items.length) return false;
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
event.preventDefault();
ms.index = (ms.index + (event.key === 'ArrowDown' ? 1 : ms.items.length - 1)) % ms.items.length;
renderRoomMention(roomMentionToken($('#roomInput'))?.query || '');
return true;
}
if ((event.key === 'Enter' && !event.shiftKey) || event.key === 'Tab') {
event.preventDefault();
pickRoomMention(ms.items[ms.index]);
return true;
}
return false;
}
function pickRoomMention(item) {
const input = $('#roomInput');
const ms = roomMentionState();
if (!item || !input) return;
if (ms.start >= 0) {
const caret = input.selectionStart;
const insert = item.kind === 'member' ? '@' + item.name + ' ' : '';
input.value = input.value.slice(0, ms.start) + insert + input.value.slice(caret).replace(/^ /, insert ? '' : '');
const next = ms.start + insert.length;
input.setSelectionRange(next, next);
}
if (item.kind === 'sop') {
roomState().mode = 'issue';
toggleRoomSOP(item.id, true);
}
ms.targetsOnly = false;
closeRoomMention();
syncRoomComposer();
input.focus();
if (item.kind === 'sop' && !input.value.trim()) toast('已选好 SOP,第一行写 Issue 标题,回车建单');
}
async function decideRoomStep(action, runId, nodeId, name) {
const id = roomState().activeId;
if (!id) return;
let reason = '';
if (action === 'reject') {
reason = await afPrompt({title: '打回「' + name + '」', message: '说明要改什么,会带给被打回的步骤。', label: '打回原因', placeholder: '例如:边界情况没覆盖', confirmText: '打回'});
if (reason == null) return;
}
try {
await api('/api/rooms/' + encodeURIComponent(id) + '/decisions', {method: 'POST', body: JSON.stringify({runId, nodeId, action, reason: String(reason || '').trim()})});
toast(action === 'approve' ? '已批准' : '已打回');
await refreshRoomMessages();
} catch (error) {
toast(error.message);
}
}
async function renameRoom() {
const room = activeRoom();
if (!room) return toast('先选择一个群');
const name = await afPrompt({title: '重命名协作群', message: '新名称会显示在左侧群列表和顶栏。', label: '群名称', placeholder: '例如:需求值班群', defaultValue: room.name, confirmText: '保存'});
if (name == null) return;
const next = String(name).trim();
if (!next || next === room.name) return;
try {
const updated = await api('/api/rooms/' + encodeURIComponent(room.id), {method: 'PATCH', body: JSON.stringify({name: next})});
room.name = updated.name || next;
renderRoomList(roomState().items || []);
const title = $('#roomTitle');
if (title) title.textContent = room.name;
toast('已重命名');
} catch (error) {
toast(error.message);
}
}
async function createRoom() {
const projectId = roomProjectId();
if (!projectId) return toast('先在顶栏选择项目');
const name = await afPrompt({title: '新建协作群', message: '群名称会显示在当前项目的群列表里。', label: '群名称', placeholder: '例如:需求值班群', confirmText: '创建'});
if (!name || !String(name).trim()) return;
try {
const room = await api('/api/rooms', {method: 'POST', body: JSON.stringify({projectId, name: String(name).trim()})});
roomState().activeId = room.id;
await loadRooms();
toast('已创建协作群');
} catch (error) {
toast(error.message);
}
}
function submitRoomComposer() {
if (roomState().mode === 'issue') dispatchRoomIssue();
else sendRoomMessage();
}
async function sendRoomMessage() {
const id = roomState().activeId;
const input = $('#roomInput');
const body = input?.value.trim();
if (!id) return toast('先选择一个群');
if (!body) return;
try {
await api('/api/rooms/' + encodeURIComponent(id) + '/messages', {method: 'POST', body: JSON.stringify({body})});
input.value = '';
closeRoomMention();
syncRoomComposer();
await openRoom(id);
} catch (error) {
toast(error.message);
}
}
function splitRoomIssueText(text) {
const lines = String(text || '').split('\n');
const title = (lines.shift() || '').trim().slice(0, 200);
return {title, description: lines.join('\n').trim()};
}
async function dispatchRoomIssue() {
const rs = roomState();
const id = rs.activeId;
if (!id) return toast('先选择一个群');
const {title, description} = splitRoomIssueText($('#roomInput')?.value);
const workflowIds = rs.sops.slice();
if (!title) return toast('第一行写 Issue 标题');
if (!workflowIds.length) return toast('请选择要派发的 SOP');
const button = $('#roomDispatch');
button.disabled = true;
try {
const result = await api('/api/rooms/' + encodeURIComponent(id) + '/issues', {method: 'POST', body: JSON.stringify({title, description, workflowIds})});
$('#roomInput').value = '';
rs.sops = [];
rs.mode = 'chat';
closeRoomMention();
syncRoomComposer();
if (result.issue) {
const items = state.data.issues || [];
const index = items.findIndex(issue => issue.id === result.issue.id);
if (index >= 0) items[index] = result.issue;
else items.unshift(result.issue);
state.data.issues = items;
}
await openRoom(id);
const started = (result.tasks || []).length;
toast(result.dispatchError ? 'Issue 已创建,部分派发未成功:' + result.dispatchError : (started ? `已建单并启动 ${started} 条 SOP,进度会显示在卡片上` : 'Issue 已创建,但没有启动 SOP'));
} catch (error) {
toast(error.message);
} finally {
button.disabled = false;
}
}
bindRooms();
if ((state.currentView || activeViewName()) === 'rooms') loadRooms();
You can’t perform that action at this time.