@restregular/dsh-team-hub 0.0.0-stage → 0.1.0
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/README.md +10 -2
- package/client.js +809 -0
- package/cordis.patch.yml +4 -0
- package/docs/DETAIL.md +37 -0
- package/icon.svg +15 -0
- package/index.js +499 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +51 -4
package/client.js
ADDED
|
@@ -0,0 +1,809 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @restregular/dsh-team-hub — Client half: canvas-style team board.
|
|
3
|
+
* 整文件包在 IIFE 里,避免与其它插件的顶层 const/let 共享全局词法作用域。
|
|
4
|
+
*/
|
|
5
|
+
(() => {
|
|
6
|
+
const MODULE_ID = '@restregular/dsh-team-hub'
|
|
7
|
+
const loader = window.__ModuleLoader__
|
|
8
|
+
if (!loader || typeof loader.load !== 'function') return
|
|
9
|
+
if (loader.factories?.has?.(MODULE_ID)) return
|
|
10
|
+
|
|
11
|
+
loader.load({
|
|
12
|
+
id: MODULE_ID,
|
|
13
|
+
factory(require) {
|
|
14
|
+
const React = require('react')
|
|
15
|
+
const { useState, useEffect, useRef, useCallback, useMemo } = React
|
|
16
|
+
const h = React.createElement
|
|
17
|
+
const PANEL_ID = 'team-hub'
|
|
18
|
+
const NS = 'teamHub'
|
|
19
|
+
const API = '/team-hub/api'
|
|
20
|
+
const TAB_KIND = 'team-hub-canvas'
|
|
21
|
+
const TAB_ID = '@restregular/dsh-team-hub/canvas'
|
|
22
|
+
let boundWorkspace = null
|
|
23
|
+
let boundWorkspaceTitle = ''
|
|
24
|
+
let sidebarRightSvc = null
|
|
25
|
+
|
|
26
|
+
/* ------------------------------------------------------------- 数据 */
|
|
27
|
+
async function api(path, options = {}) {
|
|
28
|
+
const opts = { ...options }
|
|
29
|
+
if (opts.body && typeof opts.body !== 'string') {
|
|
30
|
+
opts.body = JSON.stringify(opts.body)
|
|
31
|
+
opts.headers = { 'content-type': 'application/json', ...(opts.headers || {}) }
|
|
32
|
+
}
|
|
33
|
+
const res = await fetch(API + path, opts)
|
|
34
|
+
let data
|
|
35
|
+
try { data = await res.json() } catch { throw new Error(`HTTP ${res.status}`) }
|
|
36
|
+
if (!data?.ok) throw new Error(data?.error?.message || `HTTP ${res.status}`)
|
|
37
|
+
return data.value
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const STATUS_LABEL = {
|
|
41
|
+
uninstantiated: '还未实例化', idle: '空闲', working: '工作中',
|
|
42
|
+
done: '已完成', failed: '失败', archived: '已归档',
|
|
43
|
+
}
|
|
44
|
+
function statusOf(agent) { return agent?.sessionId ? (agent.status || 'idle') : 'uninstantiated' }
|
|
45
|
+
function initText(agent) {
|
|
46
|
+
const p = [`你现在是团队中的「${agent.name}」。`]
|
|
47
|
+
if (agent.identity) p.push(`身份:${agent.identity}`)
|
|
48
|
+
if (agent.duties) p.push(`职责边界:${agent.duties}`)
|
|
49
|
+
const perm = agent.permissions || {}
|
|
50
|
+
if (perm.denyTools?.length) p.push(`禁止使用的工具:${perm.denyTools.join('、')}(硬约束,由系统强制执行)。`)
|
|
51
|
+
if (perm.writeFiles?.length) p.push(`允许写入的文件范围:${perm.writeFiles.join('、')}。`)
|
|
52
|
+
if (perm.readFiles?.length) p.push(`可读文件范围:${perm.readFiles.join('、')}。`)
|
|
53
|
+
p.push('请确认已了解上述设定,等待团队协作消息。')
|
|
54
|
+
return p.join('\n')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/* -------------------------------------------------------- 事件解析 */
|
|
58
|
+
function contentText(content) {
|
|
59
|
+
if (!Array.isArray(content)) return ''
|
|
60
|
+
return content.filter((b) => b && typeof b.text === 'string').map((b) => b.text).join('')
|
|
61
|
+
}
|
|
62
|
+
function cleanUserText(text) {
|
|
63
|
+
return String(text || '')
|
|
64
|
+
.replace(/<system-reminder>[\s\S]*?<\/system-reminder>/g, '')
|
|
65
|
+
.replace(/^\s*以下是(?:本项目|与)[\s\S]*?(?:记忆摘要|相关的记忆)/gm, '')
|
|
66
|
+
.trim()
|
|
67
|
+
}
|
|
68
|
+
function parseEvent(ev) {
|
|
69
|
+
if (!ev) return null
|
|
70
|
+
const d = ev.data || {}
|
|
71
|
+
const seq = typeof ev.seq === 'number' ? ev.seq : Date.now()
|
|
72
|
+
switch (ev.type) {
|
|
73
|
+
case 'assistant/message': {
|
|
74
|
+
const text = contentText(d.message?.content)
|
|
75
|
+
return text ? { kind: 'assistant', text, seq } : null
|
|
76
|
+
}
|
|
77
|
+
case 'user/message': {
|
|
78
|
+
const text = cleanUserText(contentText(d.content))
|
|
79
|
+
return text ? { kind: 'user', text, seq } : null
|
|
80
|
+
}
|
|
81
|
+
case 'tool/call': return { kind: 'tool', text: `调用工具 ${d.name || ''}`, seq }
|
|
82
|
+
case 'tool/result': {
|
|
83
|
+
const text = contentText(d.message?.content) || '工具结果'
|
|
84
|
+
return { kind: d.error ? 'error' : 'tool', text, seq }
|
|
85
|
+
}
|
|
86
|
+
case 'turn/end': {
|
|
87
|
+
const reason = d.reason && typeof d.reason === 'object' ? (d.reason.kind || d.reason.reason || 'complete') : d.reason
|
|
88
|
+
return { kind: 'turn', text: `本轮结束${reason ? `(${reason})` : ''}`, seq }
|
|
89
|
+
}
|
|
90
|
+
default: return null
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function useAgentFeed(ctx, sessionId) {
|
|
95
|
+
const [feed, setFeed] = useState([])
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
setFeed([])
|
|
98
|
+
if (!sessionId || !ctx?.sessions?.retain) return
|
|
99
|
+
let ref, off, disposed = false
|
|
100
|
+
const read = () => {
|
|
101
|
+
if (disposed || !ref) return
|
|
102
|
+
const entries = ref.binding?.eventSource?.getSnapshot()?.entries || []
|
|
103
|
+
const items = []
|
|
104
|
+
for (const e of entries) {
|
|
105
|
+
const parsed = parseEvent(e?.event)
|
|
106
|
+
if (parsed) items.push(parsed)
|
|
107
|
+
}
|
|
108
|
+
setFeed(items.slice(-20))
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
ref = ctx.sessions.retain(sessionId, { source: 'teamHub' })
|
|
112
|
+
Promise.resolve(ref?.ready).then(() => {
|
|
113
|
+
if (disposed) { ref?.release?.(); return }
|
|
114
|
+
read()
|
|
115
|
+
off = ref.binding?.eventSource?.subscribe(read)
|
|
116
|
+
}).catch((err) => console.warn('[team-hub] feed ready failed:', err?.message))
|
|
117
|
+
} catch (err) { console.warn('[team-hub] retain failed:', err?.message) }
|
|
118
|
+
return () => { disposed = true; off?.(); ref?.release?.() }
|
|
119
|
+
}, [ctx, sessionId])
|
|
120
|
+
return feed
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function useRemoteNotices(ctx) {
|
|
124
|
+
const [notices, setNotices] = useState({})
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
if (!ctx?.remote?.$on) return
|
|
127
|
+
const push = (kind, payload) => {
|
|
128
|
+
const sid = payload?.agent?.session?.id
|
|
129
|
+
if (!sid) return
|
|
130
|
+
const text = kind === 'approval'
|
|
131
|
+
? `审批请求:${payload.toolName || ''}${payload.reason ? ` — ${payload.reason}` : ''}`
|
|
132
|
+
: `提问:${(payload.questions || []).map((q) => q.question || q.title || '').join(' / ')}`
|
|
133
|
+
setNotices((m) => ({ ...m, [sid]: [...(m[sid] || []), { kind, text, ts: Date.now() }].slice(-8) }))
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
const offA = ctx.remote.$on('approval/request', (p) => push('approval', p))
|
|
137
|
+
const offQ = ctx.remote.$on('user-questions/request', (p) => push('question', p))
|
|
138
|
+
return () => { offA?.(); offQ?.() }
|
|
139
|
+
} catch (err) { console.warn('[team-hub] remote notices:', err?.message) }
|
|
140
|
+
}, [ctx])
|
|
141
|
+
return notices
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/* ----------------------------------------------------------- 图标 */
|
|
145
|
+
function HubIcon({ size = 16, active }) {
|
|
146
|
+
return h('svg', {
|
|
147
|
+
width: size, height: size, viewBox: '0 0 24 24', fill: 'none',
|
|
148
|
+
stroke: 'currentColor', 'stroke-width': active ? 2.2 : 1.8,
|
|
149
|
+
'stroke-linecap': 'round', 'stroke-linejoin': 'round', 'aria-hidden': true,
|
|
150
|
+
},
|
|
151
|
+
h('rect', { x: 3, y: 3, width: 7, height: 7, rx: 2 }),
|
|
152
|
+
h('rect', { x: 14, y: 14, width: 7, height: 7, rx: 2 }),
|
|
153
|
+
h('path', { d: 'M10 6.5h4a3 3 0 0 1 3 3V14' }),
|
|
154
|
+
h('path', { d: 'M6.5 10v4a3 3 0 0 0 3 3H14' }),
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/* ------------------------------------------------------------- 样式 */
|
|
159
|
+
const CSS = `
|
|
160
|
+
.th-root { position:relative; display:flex; flex-direction:column; width:100%; height:100%; min-height:0; overflow:hidden; background:var(--dsw-alias-bg-base); color:var(--dsw-alias-label-primary); font-family:inherit; }
|
|
161
|
+
.th-compact .th-bar { padding:8px 10px; gap:6px; }
|
|
162
|
+
.th-compact .th-title, .th-compact .th-sub { font-size:11px; }
|
|
163
|
+
.th-compact .th-ws-chip { max-width:120px; font-size:10px; }
|
|
164
|
+
.th-compact .th-btn { padding:4px 8px; font-size:11px; }
|
|
165
|
+
.th-compact .th-zoom button { width:20px; height:20px; }
|
|
166
|
+
.th-compact .th-zoom span { min-width:38px; font-size:10px; }
|
|
167
|
+
.th-compact .th-activity { display:none; }
|
|
168
|
+
.th-bar { display:flex; align-items:center; gap:10px; padding:10px 16px; border-bottom:1px solid var(--dsw-alias-border-l1); background:var(--dsw-alias-bg-layer-1); flex:0 0 auto; z-index:5; flex-wrap:wrap; }
|
|
169
|
+
.th-title { font-size:15px; font-weight:650; letter-spacing:.2px; display:flex; align-items:center; gap:8px; }
|
|
170
|
+
.th-title svg { color:var(--dsw-alias-brand-primary); }
|
|
171
|
+
.th-sub { font-size:12px; color:var(--dsw-alias-label-secondary); }
|
|
172
|
+
.th-ws-chip { display:inline-flex; align-items:center; gap:5px; padding:3px 9px; border-radius:999px; font-size:11.5px; border:1px solid var(--dsw-alias-border-l2); color:var(--dsw-alias-label-secondary); background:var(--dsw-alias-bg-layer-2); max-width:240px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
|
|
173
|
+
.th-ws-chip.bound { color:var(--dsw-alias-brand-primary); border-color:var(--dsw-alias-brand-primary); }
|
|
174
|
+
.th-spacer { flex:1 1 auto; }
|
|
175
|
+
.th-btn { appearance:none; border:1px solid var(--dsw-alias-border-l2); background:var(--dsw-alias-bg-layer-2); color:var(--dsw-alias-label-primary); border-radius:8px; padding:6px 12px; font-size:12px; cursor:pointer; transition:background .16s ease, transform .16s ease, border-color .16s ease; }
|
|
176
|
+
.th-btn:hover { background:var(--dsw-alias-interactive-bg-hover); }
|
|
177
|
+
.th-btn:active { transform:scale(.96); }
|
|
178
|
+
.th-btn.primary { background:var(--dsw-alias-button-primary-fill); border-color:transparent; color:var(--dsw-alias-bg-base); }
|
|
179
|
+
.th-btn.danger { color:var(--dsw-alias-state-error-primary); }
|
|
180
|
+
.th-btn:disabled { opacity:.45; cursor:not-allowed; }
|
|
181
|
+
.th-zoom { display:inline-flex; align-items:center; gap:2px; border:1px solid var(--dsw-alias-border-l2); border-radius:8px; overflow:hidden; }
|
|
182
|
+
.th-zoom button { appearance:none; border:0; background:var(--dsw-alias-bg-layer-2); color:var(--dsw-alias-label-primary); width:26px; height:26px; font-size:14px; cursor:pointer; transition:background .16s ease; }
|
|
183
|
+
.th-zoom button:hover { background:var(--dsw-alias-interactive-bg-hover); }
|
|
184
|
+
.th-zoom span { min-width:46px; text-align:center; font-size:11px; color:var(--dsw-alias-label-secondary); font-variant-numeric:tabular-nums; }
|
|
185
|
+
.th-canvas { position:relative; flex:1 1 auto; min-height:0; overflow:auto; background-color:var(--dsw-alias-bg-base); background-image:radial-gradient(var(--dsw-alias-border-l1) 1px, transparent 1px); background-size:22px 22px; cursor:grab; }
|
|
186
|
+
.th-canvas.th-panning { cursor:grabbing; }
|
|
187
|
+
.th-stage { position:relative; }
|
|
188
|
+
.th-world { position:absolute; left:0; top:0; width:2400px; height:1600px; transform-origin:0 0; }
|
|
189
|
+
.th-links { position:absolute; inset:0; width:100%; height:100%; pointer-events:none; overflow:visible; }
|
|
190
|
+
.th-link { stroke:var(--dsw-alias-brand-primary); stroke-width:1.8; opacity:.38; fill:none; }
|
|
191
|
+
.th-packet { fill:var(--dsw-alias-brand-primary); }
|
|
192
|
+
.th-node { position:absolute; width:236px; will-change:transform; transition:transform .18s cubic-bezier(.2,.8,.2,1); }
|
|
193
|
+
.th-node.dragging-node { transition:none; z-index:20; }
|
|
194
|
+
.th-card { position:relative; width:236px; border:1px solid var(--dsw-alias-border-l2); border-radius:14px; background:var(--dsw-alias-bg-layer-1); box-shadow:0 6px 22px rgba(0,0,0,.14); overflow:hidden; transition:box-shadow .18s ease, border-color .18s ease; }
|
|
195
|
+
.th-card:hover { box-shadow:0 10px 32px rgba(0,0,0,.22); border-color:var(--dsw-alias-brand-primary); }
|
|
196
|
+
.th-enter { animation:th-pop .28s ease both; }
|
|
197
|
+
@keyframes th-pop { from { opacity:0; } to { opacity:1; } }
|
|
198
|
+
/* 卡片上方外部的气泡:约两行高,实时内容向上滚屏 */
|
|
199
|
+
.th-bubble-out { position:absolute; left:0; right:0; bottom:100%; margin-bottom:8px; height:46px; border:1px solid var(--dsw-alias-border-l2); border-radius:12px; background:var(--dsw-alias-bg-overlay); box-shadow:0 6px 18px rgba(0,0,0,.16); overflow:hidden; cursor:pointer; transition:border-color .16s ease, opacity .16s ease; }
|
|
200
|
+
.th-bubble-out:hover { border-color:var(--dsw-alias-brand-primary); }
|
|
201
|
+
.th-bubble-out::after { content:''; position:absolute; left:24px; bottom:-6px; width:10px; height:10px; background:var(--dsw-alias-bg-overlay); border-right:1px solid var(--dsw-alias-border-l2); border-bottom:1px solid var(--dsw-alias-border-l2); transform:rotate(45deg); }
|
|
202
|
+
.th-bubble-track { display:flex; flex-direction:column; animation:th-bubble-roll .5s cubic-bezier(.2,.8,.2,1) both; }
|
|
203
|
+
@keyframes th-bubble-roll { from { transform:translateY(100%); opacity:0; } to { transform:translateY(0); opacity:1; } }
|
|
204
|
+
.th-bline { display:flex; align-items:center; gap:6px; height:23px; padding:0 10px; font-size:11px; white-space:nowrap; color:var(--dsw-alias-label-secondary); }
|
|
205
|
+
.th-bline .th-btag { flex:0 0 auto; font-size:9px; padding:1px 5px; border-radius:5px; background:var(--dsw-alias-bg-layer-2); }
|
|
206
|
+
.th-bline.assistant .th-btag { color:var(--dsw-alias-brand-primary); }
|
|
207
|
+
.th-bline.tool .th-btag { color:var(--dsw-alias-state-warn-primary); }
|
|
208
|
+
.th-bline.error .th-btag, .th-bline.approval .th-btag, .th-bline.question .th-btag { color:var(--dsw-alias-state-error-primary); }
|
|
209
|
+
.th-bline .th-btext { overflow:hidden; text-overflow:ellipsis; }
|
|
210
|
+
.th-head { display:flex; align-items:center; gap:9px; padding:11px 12px; cursor:grab; user-select:none; border-bottom:1px solid var(--dsw-alias-border-l1); background:var(--dsw-alias-bg-layer-2); }
|
|
211
|
+
.th-head:active { cursor:grabbing; }
|
|
212
|
+
.th-emoji { font-size:22px; line-height:1; width:28px; text-align:center; }
|
|
213
|
+
.th-name { font-size:13px; font-weight:650; flex:1 1 auto; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
|
|
214
|
+
.th-dot { width:8px; height:8px; border-radius:50%; background:var(--dsw-alias-state-idle-primary); transition:background .3s ease, box-shadow .3s ease; }
|
|
215
|
+
.th-dot.uninstantiated { background:transparent; border:1px dashed var(--dsw-alias-state-idle-primary); opacity:.7; }
|
|
216
|
+
.th-dot.working { background:var(--dsw-alias-state-warn-primary); box-shadow:0 0 0 4px color-mix(in srgb, var(--dsw-alias-state-warn-primary) 18%, transparent); animation:th-pulse 1.4s ease-in-out infinite; }
|
|
217
|
+
.th-dot.done { background:var(--dsw-alias-state-success-primary); }
|
|
218
|
+
.th-dot.failed, .th-dot.archived { background:var(--dsw-alias-state-error-primary); }
|
|
219
|
+
@keyframes th-pulse { 0%,100%{opacity:1} 50%{opacity:.45} }
|
|
220
|
+
.th-body { padding:10px 12px; font-size:11.5px; color:var(--dsw-alias-label-secondary); line-height:1.55; }
|
|
221
|
+
.th-role { display:-webkit-box; -webkit-line-clamp:2; -webkit-box-orient:vertical; overflow:hidden; min-height:34px; }
|
|
222
|
+
.th-last { margin-top:8px; padding-top:8px; border-top:1px dashed var(--dsw-alias-border-l1); color:var(--dsw-alias-label-primary); max-height:54px; overflow:hidden; }
|
|
223
|
+
.th-foot { display:flex; gap:6px; padding:9px 12px; border-top:1px solid var(--dsw-alias-border-l1); }
|
|
224
|
+
.th-foot .th-btn { flex:1 1 auto; padding:5px 6px; font-size:11px; text-align:center; }
|
|
225
|
+
.th-toast { position:absolute; left:50%; bottom:22px; transform:translateX(-50%) translateY(12px); background:var(--dsw-alias-bg-overlay); border:1px solid var(--dsw-alias-border-l2); color:var(--dsw-alias-label-primary); padding:9px 16px; border-radius:10px; font-size:12px; opacity:0; pointer-events:none; transition:opacity .24s ease, transform .24s ease; z-index:50; box-shadow:0 8px 28px rgba(0,0,0,.2); }
|
|
226
|
+
.th-toast.show { opacity:1; transform:translateX(-50%) translateY(0); }
|
|
227
|
+
.th-overlay { position:fixed; inset:0; background:rgba(0,0,0,.42); display:flex; align-items:center; justify-content:center; z-index:100; animation:th-fade .18s ease both; }
|
|
228
|
+
@keyframes th-fade { from{opacity:0} to{opacity:1} }
|
|
229
|
+
.th-modal { width:460px; max-width:92vw; max-height:86vh; overflow:auto; background:var(--dsw-alias-bg-layer-1); border:1px solid var(--dsw-alias-border-l2); border-radius:16px; padding:20px; box-shadow:0 24px 70px rgba(0,0,0,.4); animation:th-rise .24s cubic-bezier(.2,.8,.2,1) both; }
|
|
230
|
+
@keyframes th-rise { from{opacity:0; transform:translateY(18px) scale(.97)} to{opacity:1; transform:none} }
|
|
231
|
+
.th-modal h3 { margin:0 0 14px; font-size:16px; }
|
|
232
|
+
.th-field { display:flex; flex-direction:column; gap:5px; margin-bottom:12px; }
|
|
233
|
+
.th-field label { font-size:11px; color:var(--dsw-alias-label-secondary); }
|
|
234
|
+
.th-bind { background:var(--dsw-alias-bg-layer-2); border:1px solid var(--dsw-alias-border-l1); border-radius:8px; padding:8px 10px; font-size:11.5px; color:var(--dsw-alias-label-secondary); margin-bottom:14px; }
|
|
235
|
+
.th-input, .th-textarea { width:100%; box-sizing:border-box; background:var(--dsw-alias-bg-base); color:var(--dsw-alias-label-primary); border:1px solid var(--dsw-alias-border-l2); border-radius:8px; padding:8px 10px; font-size:12.5px; font-family:inherit; transition:border-color .16s ease; }
|
|
236
|
+
.th-input:focus, .th-textarea:focus { outline:none; border-color:var(--dsw-alias-brand-primary); }
|
|
237
|
+
.th-textarea { min-height:72px; resize:vertical; }
|
|
238
|
+
.th-modal-foot { display:flex; justify-content:flex-end; gap:8px; margin-top:8px; }
|
|
239
|
+
.th-composer { display:flex; align-items:flex-end; gap:8px; padding:12px 18px; border-top:1px solid var(--dsw-alias-border-l1); background:var(--dsw-alias-bg-layer-1); }
|
|
240
|
+
.th-composer .th-cinput { flex:1 1 auto; resize:none; min-height:42px; max-height:130px; border-radius:18px; padding:10px 14px; background:var(--dsw-alias-bg-layer-2); border:1px solid var(--dsw-alias-border-l2); color:var(--dsw-alias-label-primary); font-size:12.5px; font-family:inherit; outline:none; transition:border-color .16s ease; }
|
|
241
|
+
.th-composer .th-cinput:focus { border-color:var(--dsw-alias-brand-primary); }
|
|
242
|
+
.th-composer .th-send { width:38px; height:38px; border-radius:50%; border:0; background:var(--dsw-alias-button-primary-fill); color:var(--dsw-alias-bg-base); cursor:pointer; font-size:15px; flex:0 0 auto; transition:transform .16s ease, opacity .16s ease; }
|
|
243
|
+
.th-composer .th-send:hover { transform:scale(1.06); }
|
|
244
|
+
.th-composer .th-send:disabled { opacity:.4; cursor:not-allowed; }
|
|
245
|
+
.th-activity { position:absolute; right:14px; top:14px; width:260px; max-height:calc(100% - 28px); overflow:auto; background:var(--dsw-alias-bg-layer-1); border:1px solid var(--dsw-alias-border-l1); border-radius:12px; padding:10px 12px; font-size:11px; z-index:4; box-shadow:0 6px 20px rgba(0,0,0,.12); }
|
|
246
|
+
.th-log { padding:6px 0; border-bottom:1px dashed var(--dsw-alias-border-l1); color:var(--dsw-alias-label-secondary); animation:th-slide .3s ease both; }
|
|
247
|
+
@keyframes th-slide { from{opacity:0; transform:translateX(10px)} to{opacity:1; transform:none} }
|
|
248
|
+
.th-log b { color:var(--dsw-alias-label-primary); font-weight:600; }
|
|
249
|
+
.th-empty { position:absolute; inset:0; display:flex; flex-direction:column; align-items:center; justify-content:center; gap:10px; color:var(--dsw-alias-label-secondary); font-size:13px; }
|
|
250
|
+
/* 右侧栏里的紧凑画布 */
|
|
251
|
+
.th-side { display:flex; flex-direction:column; height:100%; min-height:0; font-family:inherit; color:var(--dsw-alias-label-primary); background:var(--dsw-alias-bg-base); }
|
|
252
|
+
.th-side-bar { display:flex; align-items:center; gap:8px; padding:10px 12px; border-bottom:1px solid var(--dsw-alias-border-l1); font-size:12px; flex:0 0 auto; }
|
|
253
|
+
.th-side-list { flex:1 1 auto; min-height:0; overflow:auto; padding:10px 12px; display:flex; flex-direction:column; gap:8px; }
|
|
254
|
+
.th-side-card { border:1px solid var(--dsw-alias-border-l2); border-radius:10px; padding:9px 10px; background:var(--dsw-alias-bg-layer-1); cursor:pointer; transition:border-color .16s ease; }
|
|
255
|
+
.th-side-card:hover { border-color:var(--dsw-alias-brand-primary); }
|
|
256
|
+
.th-side-row { display:flex; align-items:center; gap:8px; }
|
|
257
|
+
.th-side-row .th-name { font-size:12px; }
|
|
258
|
+
.th-side-msg { margin-top:6px; font-size:10.5px; color:var(--dsw-alias-label-secondary); overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
|
|
259
|
+
`
|
|
260
|
+
|
|
261
|
+
function useStyles() {
|
|
262
|
+
useEffect(() => {
|
|
263
|
+
if (document.querySelector('[data-team-hub-style]')) return
|
|
264
|
+
const el = document.createElement('style')
|
|
265
|
+
el.setAttribute('data-team-hub-style', MODULE_ID)
|
|
266
|
+
el.textContent = CSS
|
|
267
|
+
document.head.appendChild(el)
|
|
268
|
+
return () => el.remove()
|
|
269
|
+
}, [])
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/* ----------------------------------------------------------- 组件 */
|
|
273
|
+
const BUBBLE_KIND = {
|
|
274
|
+
assistant: '回复', user: '用户', tool: '工具', error: '错误',
|
|
275
|
+
turn: '完成', approval: '审批', question: '提问',
|
|
276
|
+
}
|
|
277
|
+
const CARD_W = 236
|
|
278
|
+
const CARD_H = 168
|
|
279
|
+
|
|
280
|
+
/** 两卡片最近边中点连线:按中心差选主轴,取对应边的中点。 */
|
|
281
|
+
function edgePoints(a, b) {
|
|
282
|
+
const ac = { x: a.x + CARD_W / 2, y: a.y + CARD_H / 2 }
|
|
283
|
+
const bc = { x: b.x + CARD_W / 2, y: b.y + CARD_H / 2 }
|
|
284
|
+
const dx = bc.x - ac.x, dy = bc.y - ac.y
|
|
285
|
+
if (Math.abs(dx) >= Math.abs(dy)) {
|
|
286
|
+
return dx >= 0
|
|
287
|
+
? { x1: a.x + CARD_W, y1: ac.y, x2: b.x, y2: bc.y }
|
|
288
|
+
: { x1: a.x, y1: ac.y, x2: b.x + CARD_W, y2: bc.y }
|
|
289
|
+
}
|
|
290
|
+
return dy >= 0
|
|
291
|
+
? { x1: ac.x, y1: a.y + CARD_H, x2: bc.x, y2: b.y }
|
|
292
|
+
: { x1: ac.x, y1: a.y, x2: bc.x, y2: b.y + CARD_H }
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function BubbleOut({ items, status, onClick }) {
|
|
296
|
+
const tail = items.slice(-2)
|
|
297
|
+
return h('div', { className: 'th-bubble-out', title: '点击查看原生会话', onClick },
|
|
298
|
+
h('div', { className: 'th-bubble-track', key: tail.map((i) => i.kind + i.seq).join('-') },
|
|
299
|
+
tail.length
|
|
300
|
+
? tail.map((it, i) => h('div', { className: `th-bline ${it.kind}`, key: i },
|
|
301
|
+
h('span', { className: 'th-btag' }, BUBBLE_KIND[it.kind] || it.kind),
|
|
302
|
+
h('span', { className: 'th-btext' }, it.text.slice(0, 46)),
|
|
303
|
+
))
|
|
304
|
+
: h('div', { className: 'th-bline' },
|
|
305
|
+
h('span', { className: 'th-btag' }, STATUS_LABEL[status] || status),
|
|
306
|
+
h('span', { className: 'th-btext' }, status === 'working' ? '正在工作…' : '暂无实时内容'),
|
|
307
|
+
),
|
|
308
|
+
),
|
|
309
|
+
)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function AgentCard({ agent, feed, notices, onDragStart, onMessage, onOpen, onEdit, onRemove, onStatus, onInstantiate }) {
|
|
313
|
+
const status = statusOf(agent)
|
|
314
|
+
const sid = agent.sessionId
|
|
315
|
+
const extra = sid ? (notices[sid] || []) : []
|
|
316
|
+
const all = [...feed, ...extra.map((n) => ({ kind: n.kind, text: n.text, seq: n.ts }))]
|
|
317
|
+
return h('div', {
|
|
318
|
+
className: 'th-node th-enter',
|
|
319
|
+
style: { transform: `translate(${agent.x}px, ${agent.y}px)` },
|
|
320
|
+
'data-agent': agent.id,
|
|
321
|
+
},
|
|
322
|
+
sid ? h(BubbleOut, { items: all, status, onClick: () => onOpen(agent) })
|
|
323
|
+
: h('div', { className: 'th-bubble-out', style: { opacity: .65 }, title: '实例化后显示实时内容' },
|
|
324
|
+
h('div', { className: 'th-bline' },
|
|
325
|
+
h('span', { className: 'th-btag' }, '未实例化'),
|
|
326
|
+
h('span', { className: 'th-btext' }, '点击「实例化」创建会话'),
|
|
327
|
+
),
|
|
328
|
+
),
|
|
329
|
+
h('div', { className: 'th-card' },
|
|
330
|
+
h('div', { className: 'th-head', onPointerDown: (e) => onDragStart(e, agent) },
|
|
331
|
+
h('span', { className: 'th-emoji' }, agent.icon || '🤖'),
|
|
332
|
+
h('span', { className: 'th-name', title: agent.name }, agent.name),
|
|
333
|
+
h('span', { className: `th-dot ${status}`, title: STATUS_LABEL[status] || status }),
|
|
334
|
+
),
|
|
335
|
+
h('div', { className: 'th-body' },
|
|
336
|
+
h('div', { className: 'th-role' }, agent.duties || agent.identity || '暂无职责说明'),
|
|
337
|
+
agent.lastMessage ? h('div', { className: 'th-last', title: agent.lastMessage }, agent.lastMessage) : null,
|
|
338
|
+
),
|
|
339
|
+
!sid
|
|
340
|
+
? h('div', { className: 'th-foot' },
|
|
341
|
+
h('button', { className: 'th-btn primary', onClick: () => onInstantiate(agent) }, '实例化'),
|
|
342
|
+
h('button', { className: 'th-btn', onClick: () => onEdit(agent) }, '编辑'),
|
|
343
|
+
h('button', { className: 'th-btn danger', onClick: () => onRemove(agent) }, '移除'),
|
|
344
|
+
)
|
|
345
|
+
: h('div', null,
|
|
346
|
+
h('div', { className: 'th-foot' },
|
|
347
|
+
h('button', { className: 'th-btn primary', onClick: () => onMessage(agent) }, '发消息'),
|
|
348
|
+
h('button', { className: 'th-btn', onClick: () => onOpen(agent) }, '打开会话'),
|
|
349
|
+
),
|
|
350
|
+
h('div', { className: 'th-foot' },
|
|
351
|
+
h('button', { className: 'th-btn', onClick: () => onStatus(agent) }, '状态'),
|
|
352
|
+
h('button', { className: 'th-btn', onClick: () => onEdit(agent) }, '编辑'),
|
|
353
|
+
h('button', { className: 'th-btn danger', onClick: () => onRemove(agent) }, '移除'),
|
|
354
|
+
),
|
|
355
|
+
),
|
|
356
|
+
),
|
|
357
|
+
)
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function AgentForm({ agent, t, workspaceTitle, onClose, onSave }) {
|
|
361
|
+
const [form, setForm] = useState(agent || { icon: '🤖', name: '', identity: '', duties: '', permissions: { denyTools: '', writeFiles: '', readFiles: '' } })
|
|
362
|
+
const set = (k, v) => setForm((f) => ({ ...f, [k]: v }))
|
|
363
|
+
const setPerm = (k, v) => setForm((f) => ({ ...f, permissions: { ...f.permissions, [k]: v } }))
|
|
364
|
+
const split = (v) => String(v || '').split(/[,,\n]/).map((s) => s.trim()).filter(Boolean)
|
|
365
|
+
return h('div', { className: 'th-overlay', onPointerDown: (e) => { if (e.target === e.currentTarget) onClose() } },
|
|
366
|
+
h('div', { className: 'th-modal' },
|
|
367
|
+
h('h3', null, agent ? t('editAgent') : t('newAgent')),
|
|
368
|
+
!agent && h('div', { className: 'th-bind' }, '🔗 ', t('newAgentHint'), ':', workspaceTitle || t('noWorkspace')),
|
|
369
|
+
h('div', { className: 'th-field' }, h('label', null, t('icon')), h('input', { className: 'th-input', value: form.icon, onChange: (e) => set('icon', e.target.value) })),
|
|
370
|
+
h('div', { className: 'th-field' }, h('label', null, t('name')), h('input', { className: 'th-input', value: form.name, onChange: (e) => set('name', e.target.value), placeholder: '例如:研究员' })),
|
|
371
|
+
h('div', { className: 'th-field' }, h('label', null, t('identity')), h('textarea', { className: 'th-textarea', value: form.identity, onChange: (e) => set('identity', e.target.value) })),
|
|
372
|
+
h('div', { className: 'th-field' }, h('label', null, t('duties')), h('textarea', { className: 'th-textarea', value: form.duties, onChange: (e) => set('duties', e.target.value) })),
|
|
373
|
+
h('div', { className: 'th-field' }, h('label', null, t('denyTools')), h('input', { className: 'th-input', value: form.permissions.denyTools || '', onChange: (e) => setPerm('denyTools', e.target.value), placeholder: 'bash, pwsh(硬约束)' })),
|
|
374
|
+
h('div', { className: 'th-field' }, h('label', null, t('writeFiles')), h('input', { className: 'th-input', value: form.permissions.writeFiles || '', onChange: (e) => setPerm('writeFiles', e.target.value), placeholder: '仅提示词约束' })),
|
|
375
|
+
h('div', { className: 'th-field' }, h('label', null, t('readFiles')), h('input', { className: 'th-input', value: form.permissions.readFiles || '', onChange: (e) => setPerm('readFiles', e.target.value), placeholder: '仅提示词约束' })),
|
|
376
|
+
h('div', { className: 'th-modal-foot' },
|
|
377
|
+
h('button', { className: 'th-btn', onClick: onClose }, t('cancel')),
|
|
378
|
+
h('button', {
|
|
379
|
+
className: 'th-btn primary', disabled: !form.name?.trim(),
|
|
380
|
+
onClick: () => onSave({
|
|
381
|
+
...form,
|
|
382
|
+
permissions: {
|
|
383
|
+
denyTools: split(form.permissions.denyTools),
|
|
384
|
+
writeFiles: split(form.permissions.writeFiles),
|
|
385
|
+
readFiles: split(form.permissions.readFiles),
|
|
386
|
+
},
|
|
387
|
+
}),
|
|
388
|
+
}, t('save')),
|
|
389
|
+
),
|
|
390
|
+
),
|
|
391
|
+
)
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** 发消息弹窗:单个入口(派任务/消息合并),宿主原版 composer 无公开复用 API,做等价输入框。 */
|
|
395
|
+
function Composer({ agent, t, onClose, onSend }) {
|
|
396
|
+
const [text, setText] = useState('')
|
|
397
|
+
return h('div', { className: 'th-overlay', onPointerDown: (e) => { if (e.target === e.currentTarget) onClose() } },
|
|
398
|
+
h('div', { className: 'th-modal' },
|
|
399
|
+
h('h3', null, `${t('sendMessage')} · ${agent.name}`),
|
|
400
|
+
h('div', { className: 'th-composer', style: { border: 0, padding: 0 } },
|
|
401
|
+
h('textarea', {
|
|
402
|
+
className: 'th-cinput', autoFocus: true, value: text,
|
|
403
|
+
onChange: (e) => setText(e.target.value),
|
|
404
|
+
onKeyDown: (e) => { if (e.key === 'Enter' && !e.shiftKey && text.trim()) { e.preventDefault(); onSend(text.trim()) } },
|
|
405
|
+
placeholder: '写一条消息或任务…',
|
|
406
|
+
}),
|
|
407
|
+
h('button', { className: 'th-send', disabled: !text.trim(), onClick: () => onSend(text.trim()) }, '➤'),
|
|
408
|
+
),
|
|
409
|
+
h('div', { className: 'th-modal-foot' }, h('button', { className: 'th-btn', onClick: onClose }, t('cancel'))),
|
|
410
|
+
),
|
|
411
|
+
)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** 右侧栏紧凑画布:与中央画布共享同一份看板数据,点击卡片打开对应原生会话。 */
|
|
415
|
+
/** 右侧栏里渲染的是同一个完整画布(拖拽/缩放/气泡/连线俱全),不是列表。 */
|
|
416
|
+
function SidebarCanvas(props) {
|
|
417
|
+
return h(TeamHubPage, { ...props, compact: true })
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/* --------------------------------------------------------- 主面板 */
|
|
421
|
+
function pickRecentWorkspace(items, sessionsById) {
|
|
422
|
+
let selected, selectedTime = Number.NEGATIVE_INFINITY
|
|
423
|
+
for (const ws of items || []) {
|
|
424
|
+
let latest = Number.NEGATIVE_INFINITY
|
|
425
|
+
for (const id of ws.sessionIds || []) {
|
|
426
|
+
const s = sessionsById?.[id]
|
|
427
|
+
if (s && typeof s.updatedAt === 'number') latest = Math.max(latest, s.updatedAt)
|
|
428
|
+
}
|
|
429
|
+
if (latest === Number.NEGATIVE_INFINITY) latest = Date.parse(ws.createdAt) || 0
|
|
430
|
+
if (!selected || latest > selectedTime) { selected = ws; selectedTime = latest }
|
|
431
|
+
}
|
|
432
|
+
return selected
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function AgentNode({ agent, ctx, notices, ...handlers }) {
|
|
436
|
+
const feed = useAgentFeed(ctx, agent.sessionId)
|
|
437
|
+
return h(AgentCard, { agent, feed, notices, ...handlers })
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function TeamHubPage(props) {
|
|
441
|
+
useStyles()
|
|
442
|
+
const ctx = props.ctx
|
|
443
|
+
const t = props.t || ((k) => k)
|
|
444
|
+
const openSession = props.openSession
|
|
445
|
+
const useWorkspaces = props.useWorkspaces
|
|
446
|
+
const useSessions = props.useSessions
|
|
447
|
+
const recentWs = useWorkspaces ? useWorkspaces((s) => pickRecentWorkspace(s.items, null)) : null
|
|
448
|
+
const sessionByIds = useSessions ? useSessions((s) => s.byId) : null
|
|
449
|
+
const workspace = boundWorkspace
|
|
450
|
+
? (useWorkspaces ? useWorkspaces((s) => s.items.find((w) => w.workspaceId === boundWorkspace)) : null) || { workspaceId: boundWorkspace, title: boundWorkspaceTitle }
|
|
451
|
+
: (recentWs && sessionByIds ? pickRecentWorkspace([recentWs], sessionByIds) : recentWs)
|
|
452
|
+
const workspaceId = workspace?.workspaceId
|
|
453
|
+
const workspaceTitle = workspace?.title
|
|
454
|
+
const notices = useRemoteNotices(ctx)
|
|
455
|
+
|
|
456
|
+
const [board, setBoard] = useState(null)
|
|
457
|
+
const [removedAgents, setRemovedAgents] = useState([])
|
|
458
|
+
const [archivedIds, setArchivedIds] = useState([])
|
|
459
|
+
const [archiveReady, setArchiveReady] = useState(false)
|
|
460
|
+
const [error, setError] = useState('')
|
|
461
|
+
const [toast, setToast] = useState('')
|
|
462
|
+
const [form, setForm] = useState(null)
|
|
463
|
+
const [composer, setComposer] = useState(null)
|
|
464
|
+
const [activity, setActivity] = useState([])
|
|
465
|
+
const [pulses, setPulses] = useState([])
|
|
466
|
+
const [zoom, setZoom] = useState(props.compact ? 0.6 : 1)
|
|
467
|
+
const dragRef = useRef(null)
|
|
468
|
+
const worldRef = useRef(null)
|
|
469
|
+
const canvasRef = useRef(null)
|
|
470
|
+
const zoomRef = useRef(zoom)
|
|
471
|
+
useEffect(() => { zoomRef.current = zoom }, [zoom])
|
|
472
|
+
|
|
473
|
+
const refresh = useCallback(async () => {
|
|
474
|
+
try {
|
|
475
|
+
const q = workspaceId ? `?workspaceId=${encodeURIComponent(workspaceId)}` : ''
|
|
476
|
+
setBoard(await api('/state' + q)); setError('')
|
|
477
|
+
} catch (e) { setError(e.message) }
|
|
478
|
+
}, [workspaceId])
|
|
479
|
+
const refreshRemoved = useCallback(async () => {
|
|
480
|
+
try { setRemovedAgents(workspaceId ? await api('/removed?workspaceId=' + encodeURIComponent(workspaceId)) : []) } catch {}
|
|
481
|
+
}, [workspaceId])
|
|
482
|
+
useEffect(() => { refresh(); refreshRemoved() }, [refresh, refreshRemoved])
|
|
483
|
+
useEffect(() => { if (workspaceId) { refresh(); refreshRemoved() } }, [workspaceId, refresh, refreshRemoved])
|
|
484
|
+
|
|
485
|
+
useEffect(() => {
|
|
486
|
+
const src = ctx?.workspaces?.list
|
|
487
|
+
if (!src) return
|
|
488
|
+
const read = () => {
|
|
489
|
+
const snap = src.getSnapshot()
|
|
490
|
+
if (snap?.phase === 'ready') { setArchivedIds(snap.archivedSessionIds || []); setArchiveReady(true) }
|
|
491
|
+
}
|
|
492
|
+
read()
|
|
493
|
+
const off = src.subscribe(read)
|
|
494
|
+
return () => off?.()
|
|
495
|
+
}, [ctx])
|
|
496
|
+
useEffect(() => {
|
|
497
|
+
if (!archiveReady) return
|
|
498
|
+
for (const a of removedAgents) {
|
|
499
|
+
if (a.sessionId && !archivedIds.includes(a.sessionId)) {
|
|
500
|
+
api('/restore', { method: 'POST', body: { id: a.id } })
|
|
501
|
+
.then(() => { notify(`会话已恢复,Agent「${a.name}」已回到画布`); refresh(); refreshRemoved() })
|
|
502
|
+
.catch(() => {})
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}, [archivedIds, archiveReady, removedAgents])
|
|
506
|
+
|
|
507
|
+
const setZoomClamped = useCallback((next) => setZoom(Math.min(2, Math.max(0.4, Math.round(next * 10) / 10))), [])
|
|
508
|
+
useEffect(() => {
|
|
509
|
+
const el = canvasRef.current
|
|
510
|
+
if (!el) return
|
|
511
|
+
const onWheel = (ev) => {
|
|
512
|
+
if (!ev.ctrlKey && !ev.metaKey) return
|
|
513
|
+
ev.preventDefault()
|
|
514
|
+
setZoomClamped(zoomRef.current + (ev.deltaY < 0 ? 0.1 : -0.1))
|
|
515
|
+
}
|
|
516
|
+
el.addEventListener('wheel', onWheel, { passive: false })
|
|
517
|
+
return () => el.removeEventListener('wheel', onWheel)
|
|
518
|
+
}, [setZoomClamped])
|
|
519
|
+
|
|
520
|
+
// 拖拽空白处平移画布(改 scrollLeft/scrollTop);点在卡片上不触发。
|
|
521
|
+
useEffect(() => {
|
|
522
|
+
const el = canvasRef.current
|
|
523
|
+
if (!el) return
|
|
524
|
+
let pan = null
|
|
525
|
+
const down = (ev) => {
|
|
526
|
+
if (ev.button !== 0) return
|
|
527
|
+
if (ev.target.closest('.th-node')) return // 卡片拖拽交给 onDragStart
|
|
528
|
+
pan = { x: ev.clientX, y: ev.clientY, left: el.scrollLeft, top: el.scrollTop, moved: false }
|
|
529
|
+
el.classList.add('th-panning')
|
|
530
|
+
}
|
|
531
|
+
const move = (ev) => {
|
|
532
|
+
if (!pan) return
|
|
533
|
+
const dx = ev.clientX - pan.x, dy = ev.clientY - pan.y
|
|
534
|
+
if (Math.abs(dx) + Math.abs(dy) > 3) pan.moved = true
|
|
535
|
+
el.scrollLeft = pan.left - dx
|
|
536
|
+
el.scrollTop = pan.top - dy
|
|
537
|
+
}
|
|
538
|
+
const up = () => { pan = null; el.classList.remove('th-panning') }
|
|
539
|
+
el.addEventListener('pointerdown', down)
|
|
540
|
+
window.addEventListener('pointermove', move)
|
|
541
|
+
window.addEventListener('pointerup', up)
|
|
542
|
+
return () => {
|
|
543
|
+
el.removeEventListener('pointerdown', down)
|
|
544
|
+
window.removeEventListener('pointermove', move)
|
|
545
|
+
window.removeEventListener('pointerup', up)
|
|
546
|
+
}
|
|
547
|
+
}, [])
|
|
548
|
+
|
|
549
|
+
const notify = useCallback((msg) => {
|
|
550
|
+
setToast(msg)
|
|
551
|
+
setTimeout(() => setToast((cur) => (cur === msg ? '' : cur)), 2600)
|
|
552
|
+
}, [])
|
|
553
|
+
const log = useCallback((text) => {
|
|
554
|
+
setActivity((a) => [{ id: Math.random().toString(36).slice(2), text, at: new Date().toLocaleTimeString() }, ...a].slice(0, 40))
|
|
555
|
+
}, [])
|
|
556
|
+
|
|
557
|
+
const onDragStart = useCallback((e, agent) => {
|
|
558
|
+
if (e.button !== 0) return
|
|
559
|
+
if (!worldRef.current) return
|
|
560
|
+
const head = e.currentTarget
|
|
561
|
+
const node = head.closest('.th-node') || head
|
|
562
|
+
const pointerId = e.pointerId
|
|
563
|
+
const scale = zoomRef.current
|
|
564
|
+
dragRef.current = {
|
|
565
|
+
id: agent.id, startX: e.clientX, startY: e.clientY,
|
|
566
|
+
originX: agent.x, originY: agent.y, x: agent.x, y: agent.y, moved: false, scale,
|
|
567
|
+
}
|
|
568
|
+
try { head.setPointerCapture(pointerId) } catch {}
|
|
569
|
+
try { node.classList.add('dragging-node') } catch {}
|
|
570
|
+
const move = (ev) => {
|
|
571
|
+
const d = dragRef.current
|
|
572
|
+
if (!d) return
|
|
573
|
+
const dx = (ev.clientX - d.startX) / d.scale
|
|
574
|
+
const dy = (ev.clientY - d.startY) / d.scale
|
|
575
|
+
if (Math.abs(dx) + Math.abs(dy) > 3) d.moved = true
|
|
576
|
+
d.x = Math.max(0, d.originX + dx)
|
|
577
|
+
d.y = Math.max(0, d.originY + dy)
|
|
578
|
+
setBoard((b) => b ? { ...b, agents: b.agents.map((a) => a.id === d.id ? { ...a, x: d.x, y: d.y } : a) } : b)
|
|
579
|
+
}
|
|
580
|
+
const up = async () => {
|
|
581
|
+
const d = dragRef.current
|
|
582
|
+
try { head.releasePointerCapture?.(pointerId) } catch {}
|
|
583
|
+
try { node.classList.remove('dragging-node') } catch {}
|
|
584
|
+
window.removeEventListener('pointermove', move)
|
|
585
|
+
window.removeEventListener('pointerup', up)
|
|
586
|
+
dragRef.current = null
|
|
587
|
+
if (!d?.moved) return
|
|
588
|
+
try { await api('/move', { method: 'POST', body: { id: d.id, x: d.x, y: d.y } }) }
|
|
589
|
+
catch (err) { notify(`保存位置失败: ${err.message}`) }
|
|
590
|
+
}
|
|
591
|
+
window.addEventListener('pointermove', move)
|
|
592
|
+
window.addEventListener('pointerup', up)
|
|
593
|
+
}, [notify])
|
|
594
|
+
|
|
595
|
+
const saveAgent = async (data) => {
|
|
596
|
+
try {
|
|
597
|
+
await api('/agent', { method: 'POST', body: { ...data, workspaceId: data.workspaceId || workspaceId } })
|
|
598
|
+
setForm(null)
|
|
599
|
+
notify(data.id ? '已保存 Agent 卡片' : '已创建 Agent 卡片,点「实例化」建会话')
|
|
600
|
+
log(`保存卡片 <b>${data.name}</b>`)
|
|
601
|
+
await refresh()
|
|
602
|
+
} catch (e) { notify(`失败: ${e.message}`) }
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
const instantiate = async (agent) => {
|
|
606
|
+
const ws = agent.workspaceId || workspaceId
|
|
607
|
+
if (!ws) { notify('未绑定工作区,无法实例化'); return }
|
|
608
|
+
try {
|
|
609
|
+
notify('正在创建会话并发送初始化设定…')
|
|
610
|
+
const sessionId = await ctx.sessions.create({ workspaceId: ws })
|
|
611
|
+
await ctx.sessions.using(sessionId, { source: 'teamHub' }, async (ref) => {
|
|
612
|
+
await ref.ready
|
|
613
|
+
return ref.binding.session.prompt([{ type: 'text', text: initText(agent) }], 'queue', undefined, crypto.randomUUID())
|
|
614
|
+
})
|
|
615
|
+
await api('/agent', { method: 'POST', body: { ...agent, sessionId } })
|
|
616
|
+
log(`<b>${agent.name}</b> 已实例化并绑定会话`)
|
|
617
|
+
notify('已实例化,初始化设定已发送至新会话')
|
|
618
|
+
await refresh()
|
|
619
|
+
} catch (e) { notify(`实例化失败: ${e.message}`) }
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
const removeAgent = async (agent) => {
|
|
623
|
+
if (!window.confirm(`移除 Agent「${agent.name}」?\n其会话将被归档;之后在侧边栏恢复该会话,Agent 会自动回到画布。`)) return
|
|
624
|
+
try {
|
|
625
|
+
if (agent.sessionId) await ctx.uiWorkspace?.archiveSession(agent.sessionId, { stopActivity: true })
|
|
626
|
+
await api(`/agent?id=${encodeURIComponent(agent.id)}`, { method: 'DELETE' })
|
|
627
|
+
log(`移除 <b>${agent.name}</b> 并归档会话`)
|
|
628
|
+
notify('已移除并归档会话')
|
|
629
|
+
await refresh(); await refreshRemoved()
|
|
630
|
+
} catch (e) { notify(`移除失败: ${e.message}`) }
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
const sendMessage = async (agent, text) => {
|
|
634
|
+
setComposer(null)
|
|
635
|
+
try {
|
|
636
|
+
await api('/message', { method: 'POST', body: { from: '团队面板', to: agent.id, text } })
|
|
637
|
+
setPulses((p) => [...p, { id: Math.random().toString(36).slice(2), from: null, to: agent.id }])
|
|
638
|
+
setTimeout(() => setPulses((p) => p.slice(1)), 1200)
|
|
639
|
+
log(`发消息 → <b>${agent.name}</b>`)
|
|
640
|
+
notify('消息已送达')
|
|
641
|
+
await refresh()
|
|
642
|
+
} catch (e) { notify(`失败: ${e.message}`) }
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
const showStatus = async (agent) => {
|
|
646
|
+
try {
|
|
647
|
+
const state = await api(`/status?id=${encodeURIComponent(agent.id)}`)
|
|
648
|
+
notify(state ? `状态:${state.active ? '活跃' : '空闲'} · ${state.running ? '运行中' : '未运行'}` : '状态暂不可用')
|
|
649
|
+
} catch (e) { notify(`失败: ${e.message}`) }
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/** 卡片点击:中央切回原生会话 + 画布缩到右侧栏(彻底复用原生 UI)。 */
|
|
653
|
+
const openInSplit = useCallback((agent) => {
|
|
654
|
+
if (!agent?.sessionId) { notify('请先实例化该 Agent'); return }
|
|
655
|
+
ctx.uiWorkspace?.openSession(agent.sessionId)
|
|
656
|
+
try { sidebarRightSvc?.openTab(TAB_KIND, { params: { sessionId: agent.sessionId } }) }
|
|
657
|
+
catch (err) { console.warn('[team-hub] openTab failed:', err?.message) }
|
|
658
|
+
}, [ctx, notify])
|
|
659
|
+
|
|
660
|
+
const agents = board?.agents || []
|
|
661
|
+
const byId = useMemo(() => Object.fromEntries(agents.map((a) => [a.id, a])), [agents])
|
|
662
|
+
// 连线只来自真实沟通记录(每个 from→to 对只取最近一条),不再全连接。
|
|
663
|
+
const links = useMemo(() => {
|
|
664
|
+
const seen = new Map()
|
|
665
|
+
for (const m of [...(board?.messages || [])].reverse()) {
|
|
666
|
+
const key = `${m.from}->${m.to}`
|
|
667
|
+
if (!seen.has(key) && byId[m.from] && byId[m.to]) seen.set(key, { from: byId[m.from], to: byId[m.to] })
|
|
668
|
+
}
|
|
669
|
+
return [...seen.values()]
|
|
670
|
+
}, [board, byId])
|
|
671
|
+
|
|
672
|
+
return h('div', { className: `th-root ${props.compact ? 'th-compact' : ''}` },
|
|
673
|
+
h('div', { className: 'th-bar' },
|
|
674
|
+
h('span', { className: 'th-title' }, h(HubIcon, { size: 18, active: true }), t('title')),
|
|
675
|
+
h('span', { className: `th-ws-chip ${boundWorkspace ? 'bound' : ''}`, title: workspaceId || '' },
|
|
676
|
+
h('svg', { width: 13, height: 13, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', 'stroke-width': 2, 'aria-hidden': true },
|
|
677
|
+
h('path', { d: 'M3 7a2 2 0 0 1 2-2h5l2 2h7a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z' }),
|
|
678
|
+
),
|
|
679
|
+
boundWorkspace ? (workspaceTitle || t('boundWorkspace')) : t('globalWorkspace'),
|
|
680
|
+
),
|
|
681
|
+
h('span', { className: 'th-sub' }, `${agents.length} 个 Agent · Ctrl+滚轮缩放`),
|
|
682
|
+
h('span', { className: 'th-spacer' }),
|
|
683
|
+
h('div', { className: 'th-zoom' },
|
|
684
|
+
h('button', { title: '缩小', onClick: () => setZoomClamped(zoom - 0.1) }, '−'),
|
|
685
|
+
h('span', { title: 'Ctrl+滚轮缩放' }, `${Math.round(zoom * 100)}%`),
|
|
686
|
+
h('button', { title: '放大', onClick: () => setZoomClamped(zoom + 0.1) }, '+'),
|
|
687
|
+
h('button', { title: '重置', onClick: () => setZoomClamped(1) }, '⟳'),
|
|
688
|
+
),
|
|
689
|
+
h('button', { className: 'th-btn', onClick: () => { refresh(); refreshRemoved() } }, t('refresh')),
|
|
690
|
+
h('button', { className: 'th-btn primary', onClick: () => setForm({}) }, t('newAgent')),
|
|
691
|
+
),
|
|
692
|
+
h('div', { className: 'th-canvas', ref: canvasRef },
|
|
693
|
+
h('div', { className: 'th-stage', style: { width: 2400 * zoom, height: 1600 * zoom } },
|
|
694
|
+
h('div', { className: 'th-world', ref: worldRef, style: { transform: `scale(${zoom})` } },
|
|
695
|
+
h('svg', { className: 'th-links' },
|
|
696
|
+
links.map(({ from, to }) => {
|
|
697
|
+
const p = edgePoints(from, to)
|
|
698
|
+
return h('path', { key: `${from.id}-${to.id}`, className: 'th-link',
|
|
699
|
+
d: `M ${p.x1} ${p.y1} L ${p.x2} ${p.y2}` })
|
|
700
|
+
}),
|
|
701
|
+
pulses.map((pl) => {
|
|
702
|
+
const to = byId[pl.to]
|
|
703
|
+
if (!to) return null
|
|
704
|
+
const x2 = to.x + CARD_W / 2, y2 = to.y - 22
|
|
705
|
+
return h('circle', { key: pl.id, className: 'th-packet', r: 5 },
|
|
706
|
+
h('animateMotion', { dur: '.9s', fill: 'freeze', path: `M 1200 30 Q ${(1200 + x2) / 2} ${Math.min(30, y2) - 80} ${x2} ${y2}` }),
|
|
707
|
+
)
|
|
708
|
+
}),
|
|
709
|
+
),
|
|
710
|
+
agents.length === 0
|
|
711
|
+
? h('div', { className: 'th-empty' }, h('div', { style: { fontSize: 30 } }, '🗂️'), h('div', null, t('empty')), h('button', { className: 'th-btn primary', onClick: () => setForm({}) }, t('newAgent')))
|
|
712
|
+
: agents.map((a) => h(AgentNode, {
|
|
713
|
+
key: a.id, agent: a, ctx, notices,
|
|
714
|
+
onDragStart,
|
|
715
|
+
onMessage: (ag) => setComposer({ agent: ag }),
|
|
716
|
+
onOpen: openInSplit,
|
|
717
|
+
onEdit: (ag) => setForm(ag),
|
|
718
|
+
onRemove: removeAgent,
|
|
719
|
+
onStatus: showStatus,
|
|
720
|
+
onInstantiate: instantiate,
|
|
721
|
+
})),
|
|
722
|
+
activity.length > 0 && h('div', { className: 'th-activity' },
|
|
723
|
+
h('div', { style: { fontWeight: 650, marginBottom: 6 } }, t('activity')),
|
|
724
|
+
activity.map((l) => h('div', { className: 'th-log', key: l.id, dangerouslySetInnerHTML: { __html: `${l.text}<br><span style="opacity:.6">${l.at}</span>` } })),
|
|
725
|
+
),
|
|
726
|
+
),
|
|
727
|
+
),
|
|
728
|
+
),
|
|
729
|
+
h('div', { className: `th-toast ${toast ? 'show' : ''}` }, toast),
|
|
730
|
+
error ? h('div', { className: 'th-toast show', style: { color: 'var(--dsw-alias-state-error-primary)' } }, error) : null,
|
|
731
|
+
form !== null ? h(AgentForm, { agent: form.id ? form : null, t, workspaceTitle, onClose: () => setForm(null), onSave: saveAgent }) : null,
|
|
732
|
+
composer ? h(Composer, { agent: composer.agent, t, onClose: () => setComposer(null), onSend: (text) => sendMessage(composer.agent, text) }) : null,
|
|
733
|
+
)
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/* --------------------------------------------------------- 注册 */
|
|
737
|
+
const DICT = {
|
|
738
|
+
zh: {
|
|
739
|
+
title: '团队中枢', panel: '团队画布', workspaceAction: '在团队中枢打开', refresh: '刷新', newAgent: '新建 Agent',
|
|
740
|
+
boundWorkspace: '已绑定工作区', globalWorkspace: '全局视图(未绑定工作区)',
|
|
741
|
+
editAgent: '编辑 Agent', icon: '图标', name: '名称', identity: '身份设定',
|
|
742
|
+
duties: '职责边界', newAgentHint: '将在此工作区创建卡片,再点「实例化」建会话', noWorkspace: '未检测到工作区',
|
|
743
|
+
denyTools: '禁用工具(硬约束)', writeFiles: '可写文件(提示约束)', readFiles: '可读文件(提示约束)',
|
|
744
|
+
cancel: '取消', save: '保存', sendMessage: '发消息 / 派任务',
|
|
745
|
+
activity: '协作动态', empty: '还没有 Agent,点「新建 Agent」开始搭建你的团队画布',
|
|
746
|
+
},
|
|
747
|
+
en: {
|
|
748
|
+
title: 'Team Hub', panel: 'Team canvas', workspaceAction: 'Open in Team Hub', refresh: 'Refresh', newAgent: 'New agent',
|
|
749
|
+
boundWorkspace: 'Bound workspace', globalWorkspace: 'Global view (no workspace)',
|
|
750
|
+
editAgent: 'Edit agent', icon: 'Icon', name: 'Name', identity: 'Identity',
|
|
751
|
+
duties: 'Responsibilities', newAgentHint: 'Card will be created here; click Instantiate to bind a session',
|
|
752
|
+
noWorkspace: 'No workspace detected', denyTools: 'Denied tools (hard)', writeFiles: 'Writable files (prompt)', readFiles: 'Readable files (prompt)',
|
|
753
|
+
cancel: 'Cancel', save: 'Save', sendMessage: 'Send message / task',
|
|
754
|
+
activity: 'Activity', empty: 'No agents yet — create one to start your board',
|
|
755
|
+
},
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
function apply(ctx) {
|
|
759
|
+
ctx.effect(() => ctx.locale.register(NS, DICT), 'team-hub: dict')
|
|
760
|
+
const t = ctx.locale.bind(NS)
|
|
761
|
+
const openSession = (id) => { if (id) ctx.uiWorkspace?.openSession(id) }
|
|
762
|
+
|
|
763
|
+
// 侧边栏右栏画布:点击卡片后中央切回原生会话、画布缩到右栏。
|
|
764
|
+
ctx.inject(['sidebarRight'], (svc) => { sidebarRightSvc = svc.get('sidebarRight') })
|
|
765
|
+
ctx.inject(['sidebarRightTabs'], (svc) => {
|
|
766
|
+
try {
|
|
767
|
+
const tabs = svc.get('sidebarRightTabs')
|
|
768
|
+
if (!tabs?.register) return
|
|
769
|
+
svc.effect(() => tabs.register({
|
|
770
|
+
id: TAB_ID, kind: TAB_KIND, priority: 'extension',
|
|
771
|
+
title: () => t('panel'),
|
|
772
|
+
guide: [{ id: 'team-hub', order: 45, title: () => t('panel'), description: () => t('workspaceAction') }],
|
|
773
|
+
}))
|
|
774
|
+
} catch (err) { console.warn('[team-hub] sidebar tab register failed:', err?.message) }
|
|
775
|
+
})
|
|
776
|
+
const Side = (props) => h(SidebarCanvas, { ...props, ctx, t, onOpen: (a) => openSession(a.sessionId) })
|
|
777
|
+
ctx.slots.inject('sidebar.right.pane.tab', () => ctx.slots.register({ name: 'sidebar.right.pane.tab', key: TAB_ID }, Side))
|
|
778
|
+
ctx.slots.inject('sidebar.right.pane.tab.title', () => ctx.slots.register({ name: 'sidebar.right.pane.tab.title', key: TAB_ID }, () => h('span', null, t('panel'))))
|
|
779
|
+
|
|
780
|
+
const Page = (props) => h(TeamHubPage, { ...props, ctx, t, openSession })
|
|
781
|
+
ctx.slots.inject('main', () => ctx.slots.register({ name: 'main', key: PANEL_ID, locale: NS }, Page))
|
|
782
|
+
ctx.slots.inject('sidebar.panellist', () => ctx.slots.register({
|
|
783
|
+
name: 'sidebar.panellist', id: PANEL_ID, order: 6, label: () => t('panel'), locale: NS,
|
|
784
|
+
}, HubIcon))
|
|
785
|
+
ctx.slots.inject('contextMenu.icon', () => ctx.slots.register({ name: 'contextMenu.icon', key: 'team-hub.icon' }, () => h(HubIcon, { size: 16 })))
|
|
786
|
+
|
|
787
|
+
ctx.inject(['contextMenus'], (svc) => {
|
|
788
|
+
try {
|
|
789
|
+
const menus = svc.get('contextMenus')
|
|
790
|
+
if (!menus?.register) return
|
|
791
|
+
svc.effect(() => menus.register({
|
|
792
|
+
id: 'team-hub.open', target: 'workspace', order: 50,
|
|
793
|
+
resolve: ({ workspaceId, displayTitle }) => [{
|
|
794
|
+
id: 'open-team-hub', label: t('workspaceAction'), icon: { key: 'team-hub.icon' },
|
|
795
|
+
run: () => {
|
|
796
|
+
boundWorkspace = workspaceId
|
|
797
|
+
boundWorkspaceTitle = displayTitle
|
|
798
|
+
ctx.layout?.selectPanel(PANEL_ID)
|
|
799
|
+
},
|
|
800
|
+
}],
|
|
801
|
+
}))
|
|
802
|
+
} catch (err) { console.warn('[team-hub] 工作区菜单注册失败:', err?.message) }
|
|
803
|
+
})
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
return { apply, inject: ['slots', 'locale', 'layout', 'uiWorkspace', 'sessions', 'workspaces', 'contextMenus', 'remote', 'sidebarRight', 'sidebarRightTabs'] }
|
|
807
|
+
},
|
|
808
|
+
})
|
|
809
|
+
})()
|