@restregular/dsh-team-hub 0.0.0-stage → 0.2.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 +934 -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/cordis.patch.yml
ADDED
package/docs/DETAIL.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Team Hub · 团队中枢
|
|
2
|
+
|
|
3
|
+
画布式 Agent 协作看板,让同一工作区里不同会话的 Agent 像一个团队一样协作。
|
|
4
|
+
|
|
5
|
+
## 入口
|
|
6
|
+
|
|
7
|
+
- 在工作区列表的某个工作区行点「…」→「在团队中枢打开」,整个中央会话区会切换成该工作区绑定的团队画布;顶部显示当前绑定的工作区。
|
|
8
|
+
- 侧边栏也有一个全局「团队画布」入口。
|
|
9
|
+
- 点击卡片上的气泡或「打开会话」,中央区切回该 Agent 的**原生会话界面**,同时团队画布缩到**右侧边栏**——侧栏里是同一个可拖拽、可缩放的完整画布,不是列表;跨刷新保持。
|
|
10
|
+
|
|
11
|
+
## 能做什么
|
|
12
|
+
|
|
13
|
+
- **团队画布**:无限延伸的画布,每个会话对应一张 Agent 卡片,可自由拖拽调整位置;拖拽空白处平移画布(普通滚轮也可平移),`Ctrl + 滚轮` 以鼠标位置为锚点缩放(40%~200%),按钮也可缩放。画布背景为透明方格,鼠标指针带深色描边在深浅背景下均清晰。主画布与右侧栏画布共享同一份看板数据与同一套交互。
|
|
14
|
+
- **卡片管理**:新建 / 编辑 / 移除 Agent。新建的卡片默认「还未实例化」。
|
|
15
|
+
- **实例化**:未实例化的卡片点「实例化」,在绑定工作区自动创建新会话、与卡片绑定,并把该 Agent 的身份 / 职责 / 权限设定作为初始化消息自动发进新会话。
|
|
16
|
+
- **移除与会话恢复**:移除 Agent 时自动归档其会话;之后在侧边栏恢复该会话,Agent 卡片自动回到画布。
|
|
17
|
+
- **实时气泡**:卡片上方外侧有约两行高的聊天气泡,滚动展示该会话的最新内容——助手回复、用户消息、工具调用与结果、回合结束、审批请求、用户提问,各用不同样式,并展示运行状态。气泡跟随新内容向上滚动。
|
|
18
|
+
- **协作连线**:卡片之间默认没有连线;只有当某个 Agent 通过沟通工具给另一个 Agent 发消息后,才会把两者用线连起来(取两卡最近边的中点),并用动画展示消息传递过程。
|
|
19
|
+
- **发消息 / 派任务**:合并为一个入口,对目标 Agent 的会话发送消息。
|
|
20
|
+
- **权限边界**:禁用工具由 `ctx.tools.guard()` 代码层硬拦截;身份、职责、可读写文件范围写进对应 Agent 上下文作为提示词软约束。
|
|
21
|
+
|
|
22
|
+
## 给 Agent 的工具
|
|
23
|
+
|
|
24
|
+
- `team_hub_list_agents`:列出全部 Agent。
|
|
25
|
+
- `team_hub_find_agent`:按名称或 id 查找某个 Agent,Agent 之间可用名字互相查找。
|
|
26
|
+
- `team_hub_send`:给另一个 Agent 发消息 / 派任务(同一入口,按名称或 id 指定目标),会在画布上形成一条协作连线与传递动画。
|
|
27
|
+
- `team_hub_status`:查会话状态。
|
|
28
|
+
- `team_hub_reply`:读最近回复。
|
|
29
|
+
- `team_hub_create_agent`:新建卡片。
|
|
30
|
+
|
|
31
|
+
## 数据位置
|
|
32
|
+
|
|
33
|
+
看板状态保存在 `~/.dsh/team-hub/board.json`,重启后保留。移除的 Agent 保留在同一文件的 `removed` 列表里,以便会话恢复时还原。
|
|
34
|
+
|
|
35
|
+
## 显示说明
|
|
36
|
+
|
|
37
|
+
DSH 不提供把原生会话渲染或 composer 直接复用进第三方弹窗的公开接口;因此采用「中央区切回原生会话 + 画布进右侧栏」的分屏方案,彻底复用原生会话 UI。
|
package/icon.svg
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36" width="36" height="36">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="thg" x1="0" y1="0" x2="1" y2="1">
|
|
4
|
+
<stop offset="0" stop-color="#4f8cff"/>
|
|
5
|
+
<stop offset="1" stop-color="#7b5cff"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<rect x="1" y="1" width="34" height="34" rx="9" fill="url(#thg)"/>
|
|
9
|
+
<g fill="none" stroke="#fff" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
|
|
10
|
+
<rect x="8" y="8" width="9" height="9" rx="2.5"/>
|
|
11
|
+
<rect x="19" y="19" width="9" height="9" rx="2.5"/>
|
|
12
|
+
<path d="M17 12.5h4a3.5 3.5 0 0 1 3.5 3.5V19"/>
|
|
13
|
+
<path d="M12.5 17v3.5A3.5 3.5 0 0 0 16 24h3"/>
|
|
14
|
+
</g>
|
|
15
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @restregular/dsh-team-hub — Host half.
|
|
3
|
+
*
|
|
4
|
+
* 四个半边,任一失败只记日志、绝不把异常抛回 cordis(desktop fail-loud:
|
|
5
|
+
* 一个 entry 失败会拖垮整个应用):
|
|
6
|
+
* 1) ctx.teamHub 服务:看板状态的读写与 JSON 持久化;
|
|
7
|
+
* 2) HTTP API /team-hub/api:给 Client 画布面板读写状态(带宿主围栏鉴权);
|
|
8
|
+
* 3) Agent 工具:列出 Agent、派任务/发消息、查状态、取回复、建卡片;
|
|
9
|
+
* 4) 权限:tools.guard 硬拦截 + per-agent inject 软约束。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { randomUUID } from 'node:crypto'
|
|
13
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
14
|
+
import { dirname, join } from 'node:path'
|
|
15
|
+
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
16
|
+
import { createUserMessage } from '@deepseek-ai/dsh-llm'
|
|
17
|
+
|
|
18
|
+
const NS = 'dsh-team-hub'
|
|
19
|
+
const log = (...a) => console.log(`[${NS}]`, ...a)
|
|
20
|
+
const warn = (...a) => console.warn(`[${NS}]`, ...a)
|
|
21
|
+
|
|
22
|
+
function part(label, fn) {
|
|
23
|
+
try { fn() } catch (error) { warn(`${label} 未能加载(已忽略):`, error?.message ?? String(error)) }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/* ------------------------------------------------------------------ 存储 */
|
|
27
|
+
|
|
28
|
+
function boardPath() {
|
|
29
|
+
const base = process.env.DSH_HOME
|
|
30
|
+
|| (process.env.USERPROFILE ? join(process.env.USERPROFILE, '.dsh') : null)
|
|
31
|
+
|| (process.env.HOME ? join(process.env.HOME, '.dsh') : null)
|
|
32
|
+
if (!base) throw new Error('无法定位 DSH_HOME 用户目录')
|
|
33
|
+
return join(base, 'team-hub', 'board.json')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function defaultBoard() {
|
|
37
|
+
return { version: 1, agents: [], removed: [], messages: [], tasks: [], updatedAt: 0 }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function loadBoard(file) {
|
|
41
|
+
try {
|
|
42
|
+
if (!existsSync(file)) return defaultBoard()
|
|
43
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8'))
|
|
44
|
+
if (!parsed || typeof parsed !== 'object') return defaultBoard()
|
|
45
|
+
return {
|
|
46
|
+
version: 1,
|
|
47
|
+
agents: Array.isArray(parsed.agents) ? parsed.agents : [],
|
|
48
|
+
removed: Array.isArray(parsed.removed) ? parsed.removed : [],
|
|
49
|
+
messages: Array.isArray(parsed.messages) ? parsed.messages : [],
|
|
50
|
+
tasks: Array.isArray(parsed.tasks) ? parsed.tasks : [],
|
|
51
|
+
updatedAt: Number(parsed.updatedAt) || 0,
|
|
52
|
+
}
|
|
53
|
+
} catch (error) {
|
|
54
|
+
warn('看板读取失败,使用空看板:', error?.message)
|
|
55
|
+
return defaultBoard()
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function createStore() {
|
|
60
|
+
const file = boardPath()
|
|
61
|
+
let board = loadBoard(file)
|
|
62
|
+
const save = () => {
|
|
63
|
+
try {
|
|
64
|
+
mkdirSync(dirname(file), { recursive: true })
|
|
65
|
+
board.updatedAt = Date.now()
|
|
66
|
+
writeFileSync(file, JSON.stringify(board, null, 2), 'utf8')
|
|
67
|
+
} catch (error) { warn('看板写入失败:', error?.message) }
|
|
68
|
+
}
|
|
69
|
+
const snapshot = (workspaceId) => {
|
|
70
|
+
const copy = JSON.parse(JSON.stringify(board))
|
|
71
|
+
if (workspaceId) {
|
|
72
|
+
copy.agents = copy.agents.filter((a) => a.workspaceId === workspaceId)
|
|
73
|
+
const ids = new Set(copy.agents.map((a) => a.id))
|
|
74
|
+
copy.messages = copy.messages.filter((m) => ids.has(m.from) || ids.has(m.to))
|
|
75
|
+
copy.tasks = copy.tasks.filter((t) => ids.has(t.from) || ids.has(t.to))
|
|
76
|
+
}
|
|
77
|
+
return copy
|
|
78
|
+
}
|
|
79
|
+
const findAgent = (id) => board.agents.find((a) => a.id === id)
|
|
80
|
+
const findBySession = (sessionId) => board.agents.find((a) => a.sessionId === sessionId)
|
|
81
|
+
return {
|
|
82
|
+
file, snapshot, save, findAgent, findBySession,
|
|
83
|
+
upsertAgent(input) {
|
|
84
|
+
const id = String(input.id || randomUUID())
|
|
85
|
+
const existing = findAgent(id)
|
|
86
|
+
const agent = {
|
|
87
|
+
id,
|
|
88
|
+
workspaceId: input.workspaceId ?? existing?.workspaceId ?? null,
|
|
89
|
+
sessionId: input.sessionId ?? existing?.sessionId ?? null,
|
|
90
|
+
icon: input.icon ?? existing?.icon ?? '🤖',
|
|
91
|
+
name: String(input.name ?? existing?.name ?? 'Agent').slice(0, 80),
|
|
92
|
+
identity: String(input.identity ?? existing?.identity ?? '').slice(0, 2000),
|
|
93
|
+
duties: String(input.duties ?? existing?.duties ?? '').slice(0, 4000),
|
|
94
|
+
x: Number.isFinite(input.x) ? input.x : (existing?.x ?? 80),
|
|
95
|
+
y: Number.isFinite(input.y) ? input.y : (existing?.y ?? 80),
|
|
96
|
+
permissions: {
|
|
97
|
+
denyTools: Array.isArray(input.permissions?.denyTools) ? input.permissions.denyTools : (existing?.permissions?.denyTools ?? []),
|
|
98
|
+
writeFiles: Array.isArray(input.permissions?.writeFiles) ? input.permissions.writeFiles : (existing?.permissions?.writeFiles ?? []),
|
|
99
|
+
readFiles: Array.isArray(input.permissions?.readFiles) ? input.permissions.readFiles : (existing?.permissions?.readFiles ?? []),
|
|
100
|
+
},
|
|
101
|
+
status: existing?.status ?? 'idle',
|
|
102
|
+
lastMessage: existing?.lastMessage ?? '',
|
|
103
|
+
updatedAt: Date.now(),
|
|
104
|
+
}
|
|
105
|
+
if (existing) Object.assign(existing, agent)
|
|
106
|
+
else board.agents.push(agent)
|
|
107
|
+
save()
|
|
108
|
+
return agent
|
|
109
|
+
},
|
|
110
|
+
moveAgent({ id, x, y }) {
|
|
111
|
+
const agent = findAgent(id)
|
|
112
|
+
if (!agent) return null
|
|
113
|
+
agent.x = Number(x) || 0
|
|
114
|
+
agent.y = Number(y) || 0
|
|
115
|
+
agent.updatedAt = Date.now()
|
|
116
|
+
save()
|
|
117
|
+
return agent
|
|
118
|
+
},
|
|
119
|
+
/** 移除 = 软删除:卡片移入 removed 列表,保留 sessionId 以便会话恢复时还原。 */
|
|
120
|
+
removeAgent(id) {
|
|
121
|
+
const i = board.agents.findIndex((a) => a.id === id)
|
|
122
|
+
if (i < 0) return false
|
|
123
|
+
const [agent] = board.agents.splice(i, 1)
|
|
124
|
+
board.removed = Array.isArray(board.removed) ? board.removed : []
|
|
125
|
+
board.removed.push({ ...agent, removedAt: Date.now() })
|
|
126
|
+
if (board.removed.length > 500) board.removed.splice(0, board.removed.length - 500)
|
|
127
|
+
board.messages = board.messages.filter((m) => m.from !== id && m.to !== id)
|
|
128
|
+
board.tasks = board.tasks.filter((t) => t.from !== id && t.to !== id)
|
|
129
|
+
save()
|
|
130
|
+
return true
|
|
131
|
+
},
|
|
132
|
+
restoreAgent(id) {
|
|
133
|
+
board.removed = Array.isArray(board.removed) ? board.removed : []
|
|
134
|
+
const i = board.removed.findIndex((a) => a.id === id)
|
|
135
|
+
if (i < 0) return false
|
|
136
|
+
const [agent] = board.removed.splice(i, 1)
|
|
137
|
+
delete agent.removedAt
|
|
138
|
+
agent.status = agent.sessionId ? 'idle' : 'uninstantiated'
|
|
139
|
+
agent.updatedAt = Date.now()
|
|
140
|
+
if (!board.agents.some((a) => a.id === id)) board.agents.push(agent)
|
|
141
|
+
save()
|
|
142
|
+
return agent
|
|
143
|
+
},
|
|
144
|
+
removedAgents(workspaceId) {
|
|
145
|
+
const list = Array.isArray(board.removed) ? board.removed : []
|
|
146
|
+
return workspaceId ? list.filter((a) => a.workspaceId === workspaceId) : list.slice()
|
|
147
|
+
},
|
|
148
|
+
addMessage({ from, to, text, kind = 'message' }) {
|
|
149
|
+
const record = { id: randomUUID(), from, to, text: String(text).slice(0, 8000), kind, createdAt: Date.now(), status: 'sent' }
|
|
150
|
+
board.messages.push(record)
|
|
151
|
+
if (board.messages.length > 500) board.messages.splice(0, board.messages.length - 500)
|
|
152
|
+
const target = findAgent(to)
|
|
153
|
+
if (target) { target.lastMessage = record.text; target.status = 'working'; target.updatedAt = Date.now() }
|
|
154
|
+
save()
|
|
155
|
+
return record
|
|
156
|
+
},
|
|
157
|
+
addTask({ from, to, text }) {
|
|
158
|
+
const task = { id: randomUUID(), from, to, text: String(text).slice(0, 8000), status: 'queued', createdAt: Date.now() }
|
|
159
|
+
board.tasks.push(task)
|
|
160
|
+
if (board.tasks.length > 500) board.tasks.splice(0, board.tasks.length - 500)
|
|
161
|
+
const target = findAgent(to)
|
|
162
|
+
if (target) { target.lastMessage = text; target.status = 'working'; target.updatedAt = Date.now() }
|
|
163
|
+
save()
|
|
164
|
+
return task
|
|
165
|
+
},
|
|
166
|
+
markDone(sessionId, text) {
|
|
167
|
+
const agent = findBySession(sessionId)
|
|
168
|
+
if (!agent) return false
|
|
169
|
+
agent.status = 'done'
|
|
170
|
+
agent.lastMessage = String(text ?? '').slice(0, 8000)
|
|
171
|
+
agent.updatedAt = Date.now()
|
|
172
|
+
save()
|
|
173
|
+
return true
|
|
174
|
+
},
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/* --------------------------------------------------------- 会话调用辅助 */
|
|
179
|
+
|
|
180
|
+
function abortSignal() {
|
|
181
|
+
const c = new AbortController()
|
|
182
|
+
setTimeout(() => c.abort(), 30_000).unref?.()
|
|
183
|
+
return c.signal
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function sessionPrompt(ctx, agent, text) {
|
|
187
|
+
const controller = ctx.get('sessionController')
|
|
188
|
+
if (!controller || typeof controller.prompt !== 'function') throw new Error('sessionController 不可用')
|
|
189
|
+
const res = await controller.prompt({
|
|
190
|
+
requestId: randomUUID(),
|
|
191
|
+
sessionId: agent.sessionId,
|
|
192
|
+
mode: 'queue',
|
|
193
|
+
content: [{ type: 'text', text }],
|
|
194
|
+
clientTimeZone: 'Asia/Shanghai',
|
|
195
|
+
}, abortSignal())
|
|
196
|
+
if (res && res.ok === false) throw new Error(res.error?.message || 'session/prompt 失败')
|
|
197
|
+
return true
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function sessionState(ctx, sessionId) {
|
|
201
|
+
const controller = ctx.get('sessionController')
|
|
202
|
+
if (!controller || typeof controller.state !== 'function') return null
|
|
203
|
+
const res = await controller.state({ sessionId }, abortSignal())
|
|
204
|
+
if (res && res.ok === false) return null
|
|
205
|
+
return res?.value ?? res ?? null
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function sessionReply(ctx, sessionId, maxMessages = 12) {
|
|
209
|
+
const controller = ctx.get('sessionController')
|
|
210
|
+
if (!controller || typeof controller.wait !== 'function') return ''
|
|
211
|
+
const res = await controller.wait({ sessionId, timeoutMs: 8000, maxMessages }, abortSignal())
|
|
212
|
+
if (res && res.ok === false) return ''
|
|
213
|
+
const value = res?.value ?? res
|
|
214
|
+
const records = value?.output?.records ?? []
|
|
215
|
+
for (const record of records) {
|
|
216
|
+
const content = record?.event?.data?.message?.content
|
|
217
|
+
if (record?.event?.type !== 'assistant/message' || !Array.isArray(content)) continue
|
|
218
|
+
const text = content.filter((b) => b?.type === 'text').map((b) => b.text).join('')
|
|
219
|
+
if (text) return text.slice(0, 8000)
|
|
220
|
+
}
|
|
221
|
+
return ''
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function boundariesOf(agent) {
|
|
225
|
+
const parts = [`你是团队中的「${agent.name}」。`]
|
|
226
|
+
if (agent.identity) parts.push(`身份:${agent.identity}`)
|
|
227
|
+
if (agent.duties) parts.push(`职责边界:${agent.duties}`)
|
|
228
|
+
const p = agent.permissions ?? {}
|
|
229
|
+
if (p.denyTools?.length) parts.push(`禁止使用的工具:${p.denyTools.join('、')}(这是硬约束,由系统强制执行)。`)
|
|
230
|
+
if (p.writeFiles?.length) parts.push(`允许写入的文件范围:${p.writeFiles.join('、')}。`)
|
|
231
|
+
if (p.readFiles?.length) parts.push(`可读文件范围:${p.readFiles.join('、')}。`)
|
|
232
|
+
return parts.join('\n')
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/* --------------------------------------------------------------- HTTP API */
|
|
236
|
+
|
|
237
|
+
function refuseRequest(ctx, req) {
|
|
238
|
+
let connection
|
|
239
|
+
try { connection = ctx.get('connection') } catch { connection = undefined }
|
|
240
|
+
const gate = connection?.requestRejection
|
|
241
|
+
if (typeof gate === 'function') {
|
|
242
|
+
try {
|
|
243
|
+
const status = gate.call(connection, req)
|
|
244
|
+
return typeof status === 'number' ? status : undefined
|
|
245
|
+
} catch { return 403 }
|
|
246
|
+
}
|
|
247
|
+
return undefined
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function writeJson(res, body, status = 200) {
|
|
251
|
+
try {
|
|
252
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' })
|
|
253
|
+
res.end(JSON.stringify(body))
|
|
254
|
+
} catch { /* 客户端断开 */ }
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async function readBody(req) {
|
|
258
|
+
const chunks = []
|
|
259
|
+
for await (const chunk of req) chunks.push(chunk)
|
|
260
|
+
const raw = Buffer.concat(chunks).toString('utf8')
|
|
261
|
+
if (!raw) return {}
|
|
262
|
+
try { return JSON.parse(raw) } catch { throw new Error('请求体不是合法 JSON') }
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function mountApi(ctx, store) {
|
|
266
|
+
ctx.inject(['webServer'], (svcCtx) => {
|
|
267
|
+
const webServer = svcCtx.get('webServer')
|
|
268
|
+
if (!webServer || typeof webServer.register !== 'function') {
|
|
269
|
+
warn('webServer 不可用,面板 API 不注册')
|
|
270
|
+
return
|
|
271
|
+
}
|
|
272
|
+
svcCtx.effect(() => webServer.register({
|
|
273
|
+
kind: 'prefix',
|
|
274
|
+
path: '/team-hub/api',
|
|
275
|
+
handler: async (req, res) => {
|
|
276
|
+
const refusal = refuseRequest(ctx, req)
|
|
277
|
+
if (refusal) return writeJson(res, { ok: false, error: { message: refusal === 401 ? '请先登录 DSH' : '不受信任的请求' } }, refusal)
|
|
278
|
+
try {
|
|
279
|
+
const url = new URL(req.url || '/', 'http://localhost')
|
|
280
|
+
const path = url.pathname.replace(/^\/team-hub\/api/, '') || '/state'
|
|
281
|
+
const method = (req.method || 'GET').toUpperCase()
|
|
282
|
+
|
|
283
|
+
if (path === '/state' && method === 'GET') {
|
|
284
|
+
return writeJson(res, { ok: true, value: store.snapshot(url.searchParams.get('workspaceId') || undefined), file: store.file })
|
|
285
|
+
}
|
|
286
|
+
if (path === '/agent' && method === 'POST') return writeJson(res, { ok: true, value: store.upsertAgent(await readBody(req)) })
|
|
287
|
+
if (path === '/agent' && method === 'DELETE') {
|
|
288
|
+
const id = url.searchParams.get('id')
|
|
289
|
+
return writeJson(res, { ok: true, value: store.removeAgent(id) })
|
|
290
|
+
}
|
|
291
|
+
if (path === '/removed' && method === 'GET') {
|
|
292
|
+
return writeJson(res, { ok: true, value: store.removedAgents(url.searchParams.get('workspaceId') || undefined) })
|
|
293
|
+
}
|
|
294
|
+
if (path === '/restore' && method === 'POST') {
|
|
295
|
+
const body = await readBody(req)
|
|
296
|
+
return writeJson(res, { ok: true, value: store.restoreAgent(body.id) })
|
|
297
|
+
}
|
|
298
|
+
if (path === '/move' && method === 'POST') return writeJson(res, { ok: true, value: store.moveAgent(await readBody(req)) })
|
|
299
|
+
if (path === '/task' && method === 'POST') {
|
|
300
|
+
const body = await readBody(req)
|
|
301
|
+
const target = store.findAgent(body.to)
|
|
302
|
+
if (!target?.sessionId) return writeJson(res, { ok: false, error: { message: '目标 Agent 未绑定会话' } }, 400)
|
|
303
|
+
const task = store.addTask(body)
|
|
304
|
+
try { await sessionPrompt(ctx, target, `【团队任务·来自 ${body.from ?? '面板'}】\n${body.text}\n\n完成后简述结果。`) }
|
|
305
|
+
catch (error) { task.status = 'failed'; store.save() }
|
|
306
|
+
return writeJson(res, { ok: true, value: task })
|
|
307
|
+
}
|
|
308
|
+
if (path === '/message' && method === 'POST') {
|
|
309
|
+
const body = await readBody(req)
|
|
310
|
+
const target = store.findAgent(body.to)
|
|
311
|
+
if (!target?.sessionId) return writeJson(res, { ok: false, error: { message: '目标 Agent 未绑定会话' } }, 400)
|
|
312
|
+
const message = store.addMessage(body)
|
|
313
|
+
try { await sessionPrompt(ctx, target, `【团队消息·来自 ${body.from ?? '面板'}】\n${body.text}`) }
|
|
314
|
+
catch (error) { message.status = 'failed'; store.save() }
|
|
315
|
+
return writeJson(res, { ok: true, value: message })
|
|
316
|
+
}
|
|
317
|
+
if (path === '/status' && method === 'GET') {
|
|
318
|
+
const agent = store.findAgent(url.searchParams.get('id'))
|
|
319
|
+
if (!agent?.sessionId) return writeJson(res, { ok: true, value: null })
|
|
320
|
+
return writeJson(res, { ok: true, value: await sessionState(ctx, agent.sessionId) })
|
|
321
|
+
}
|
|
322
|
+
if (path === '/reply' && method === 'GET') {
|
|
323
|
+
const agent = store.findAgent(url.searchParams.get('id'))
|
|
324
|
+
if (!agent?.sessionId) return writeJson(res, { ok: true, value: '' })
|
|
325
|
+
return writeJson(res, { ok: true, value: await sessionReply(ctx, agent.sessionId) })
|
|
326
|
+
}
|
|
327
|
+
return writeJson(res, { ok: false, error: { message: '未知接口' } }, 404)
|
|
328
|
+
} catch (error) {
|
|
329
|
+
writeJson(res, { ok: false, error: { message: error?.message || String(error) } }, 500)
|
|
330
|
+
}
|
|
331
|
+
},
|
|
332
|
+
}))
|
|
333
|
+
log('面板 API 已注册 /team-hub/api')
|
|
334
|
+
})
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/* ----------------------------------------------------------------- 工具 */
|
|
338
|
+
|
|
339
|
+
const textOut = (value) => [{ type: 'text', text: value }]
|
|
340
|
+
const jsonOut = { schema: { type: 'string' }, render: (_args, value) => textOut(value) }
|
|
341
|
+
const agentLine = (a) => `${a.name}${a.sessionId ? ` (${a.sessionId})` : ''}${a.status ? ` [${a.status}]` : ''}`
|
|
342
|
+
|
|
343
|
+
function registerTools(ctx, store) {
|
|
344
|
+
/** 按 id 或名称解析 Agent(名称支持精确与包含匹配,方便 Agent 之间用名字互找)。 */
|
|
345
|
+
const resolveAgent = (ref) => {
|
|
346
|
+
if (!ref) return undefined
|
|
347
|
+
const key = String(ref).trim()
|
|
348
|
+
const agents = store.snapshot().agents
|
|
349
|
+
return agents.find((a) => a.id === key)
|
|
350
|
+
|| agents.find((a) => a.name === key)
|
|
351
|
+
|| agents.find((a) => a.name.includes(key))
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const tools = [
|
|
355
|
+
defineTool({
|
|
356
|
+
name: 'team_hub_list_agents',
|
|
357
|
+
description: '列出团队画布上的 Agent 及其会话、状态与职责,便于决定把任务派给谁。',
|
|
358
|
+
parameters: {},
|
|
359
|
+
output: jsonOut,
|
|
360
|
+
async execute() {
|
|
361
|
+
const agents = store.snapshot().agents
|
|
362
|
+
return agents.length ? agents.map(agentLine).join('\n') : '团队看板目前没有 Agent。'
|
|
363
|
+
},
|
|
364
|
+
}),
|
|
365
|
+
defineTool({
|
|
366
|
+
name: 'team_hub_find_agent',
|
|
367
|
+
description: '按名称或 id 查找某个 Agent,返回它的 id、会话、状态与职责边界。Agent 之间可用名字互相查找。',
|
|
368
|
+
parameters: { name: { type: 'string', required: true, description: 'Agent 名称或 id,支持部分匹配' } },
|
|
369
|
+
output: jsonOut,
|
|
370
|
+
async execute(args) {
|
|
371
|
+
const found = resolveAgent(args.name)
|
|
372
|
+
if (!found) return `未找到名为「${args.name}」的 Agent。可用 team_hub_list_agents 查看全部。`
|
|
373
|
+
return agentLine(found) + (found.duties ? `\n职责:${found.duties}` : '')
|
|
374
|
+
},
|
|
375
|
+
}),
|
|
376
|
+
defineTool({
|
|
377
|
+
name: 'team_hub_send',
|
|
378
|
+
description: '给另一个 Agent 发送消息或派发任务(两者同一入口):消息会排队进入目标会话,并在画布上显示两卡片之间的传递动画。可用名称或 id 指定目标。',
|
|
379
|
+
parameters: {
|
|
380
|
+
name: { type: 'string', description: '目标 Agent 的名称或 id(名称支持部分匹配)' },
|
|
381
|
+
agentId: { type: 'string', description: '目标 Agent 的 id(与 name 二选一;都填优先 name)' },
|
|
382
|
+
text: { type: 'string', required: true, description: '消息或任务正文' },
|
|
383
|
+
},
|
|
384
|
+
output: jsonOut,
|
|
385
|
+
async execute(args, exec) {
|
|
386
|
+
const target = resolveAgent(args.name || args.agentId)
|
|
387
|
+
if (!target) return `未找到 Agent: ${args.name || args.agentId}`
|
|
388
|
+
if (!target.sessionId) return `Agent ${target.name} 还未实例化,无法接收消息`
|
|
389
|
+
// 用调用方的会话解析出真实发送者,画布才能把两个 Agent 连起来。
|
|
390
|
+
const callerSession = exec?.agent?.session?.id
|
|
391
|
+
const caller = callerSession && store.findBySession(callerSession)
|
|
392
|
+
const record = store.addMessage({ from: caller?.id || 'agent', to: target.id, text: args.text, kind: 'message' })
|
|
393
|
+
try {
|
|
394
|
+
await sessionPrompt(ctx, target, `【团队消息·来自 ${caller?.name || '团队'}】\n${args.text}`)
|
|
395
|
+
return `已发送消息给 ${target.name},记录 ${record.id}。`
|
|
396
|
+
} catch (error) { record.status = 'failed'; store.save(); return `发送失败: ${error?.message}` }
|
|
397
|
+
},
|
|
398
|
+
}),
|
|
399
|
+
defineTool({
|
|
400
|
+
name: 'team_hub_status',
|
|
401
|
+
description: '查询某个 Agent 会话的运行状态(是否运行中、队列等)。',
|
|
402
|
+
parameters: { name: { type: 'string', description: 'Agent 名称或 id' }, agentId: { type: 'string', description: 'Agent id(与 name 二选一)' } },
|
|
403
|
+
output: jsonOut,
|
|
404
|
+
async execute(args) {
|
|
405
|
+
const agent = resolveAgent(args.name || args.agentId)
|
|
406
|
+
if (!agent?.sessionId) return '未找到该 Agent 或其未绑定会话'
|
|
407
|
+
const state = await sessionState(ctx, agent.sessionId)
|
|
408
|
+
return state ? JSON.stringify(state) : '状态暂不可用'
|
|
409
|
+
},
|
|
410
|
+
}),
|
|
411
|
+
defineTool({
|
|
412
|
+
name: 'team_hub_reply',
|
|
413
|
+
description: '读取某个 Agent 会话最近一条最终回复;会等待约 8 秒。',
|
|
414
|
+
parameters: { name: { type: 'string', description: 'Agent 名称或 id' }, agentId: { type: 'string', description: 'Agent id(与 name 二选一)' }, maxMessages: { type: 'number', description: '最多读取几条记录,默认 12' } },
|
|
415
|
+
output: jsonOut,
|
|
416
|
+
async execute(args) {
|
|
417
|
+
const agent = resolveAgent(args.name || args.agentId)
|
|
418
|
+
if (!agent?.sessionId) return '未找到该 Agent 或其未绑定会话'
|
|
419
|
+
return await sessionReply(ctx, agent.sessionId, args.maxMessages) || '暂无回复'
|
|
420
|
+
},
|
|
421
|
+
}),
|
|
422
|
+
defineTool({
|
|
423
|
+
name: 'team_hub_create_agent',
|
|
424
|
+
description: '在团队画布上新建一张 Agent 卡片,可指定图标、名称、身份与职责边界。',
|
|
425
|
+
parameters: {
|
|
426
|
+
name: { type: 'string', required: true, description: 'Agent 名称' },
|
|
427
|
+
icon: { type: 'string', description: '一个 emoji 图标,默认 🤖' },
|
|
428
|
+
identity: { type: 'string', description: '身份设定' },
|
|
429
|
+
duties: { type: 'string', description: '职责边界' },
|
|
430
|
+
sessionId: { type: 'string', description: '要绑定的会话 id(可选)' },
|
|
431
|
+
},
|
|
432
|
+
output: jsonOut,
|
|
433
|
+
async execute(args) {
|
|
434
|
+
const agent = store.upsertAgent(args)
|
|
435
|
+
return `已创建 Agent ${agent.name},id=${agent.id}。可再绑定会话或在面板里操作。`
|
|
436
|
+
},
|
|
437
|
+
}),
|
|
438
|
+
]
|
|
439
|
+
|
|
440
|
+
ctx.inject(['tools'], (svcCtx) => {
|
|
441
|
+
const toolsService = svcCtx.get('tools')
|
|
442
|
+
if (!toolsService?.register) return
|
|
443
|
+
svcCtx.effect(() => {
|
|
444
|
+
const disposers = tools.map((tool) => toolsService.register(tool))
|
|
445
|
+
return () => disposers.forEach((d) => d?.())
|
|
446
|
+
})
|
|
447
|
+
})
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/* ------------------------------------------------------------- 权限与入口 */
|
|
451
|
+
|
|
452
|
+
function registerPolicies(ctx, store) {
|
|
453
|
+
// 硬约束:按当前会话对应的 Agent 配置,拦截被禁用的工具。
|
|
454
|
+
ctx.inject(['tools'], (svcCtx) => {
|
|
455
|
+
const tools = svcCtx.get('tools')
|
|
456
|
+
if (!tools?.guard) return
|
|
457
|
+
svcCtx.effect(() => tools.guard((execution) => {
|
|
458
|
+
try {
|
|
459
|
+
const sessionId = execution?.agent?.session?.id
|
|
460
|
+
const agent = sessionId && store.findBySession(sessionId)
|
|
461
|
+
const name = execution?.name ?? execution?.tool?.name ?? execution?.call?.name
|
|
462
|
+
if (agent?.permissions?.denyTools?.includes(name)) {
|
|
463
|
+
return { reason: `团队权限策略禁止 Agent「${agent.name}」使用工具 ${name}` }
|
|
464
|
+
}
|
|
465
|
+
return undefined
|
|
466
|
+
} catch { return undefined }
|
|
467
|
+
}))
|
|
468
|
+
})
|
|
469
|
+
|
|
470
|
+
// 软约束:在 agent/created 里给对应会话的 Agent 注入身份与职责边界。
|
|
471
|
+
// agent.ctx 上的 effect 随 agent 卸载回收;同时在插件 effect 里保留 disposer。
|
|
472
|
+
ctx.effect(() => {
|
|
473
|
+
const disposers = new Map()
|
|
474
|
+
const off = ctx.on('agent/created', (agent) => {
|
|
475
|
+
try {
|
|
476
|
+
const sessionId = agent?.session?.id
|
|
477
|
+
const config = sessionId && store.findBySession(sessionId)
|
|
478
|
+
if (!config) return
|
|
479
|
+
const text = boundariesOf(config)
|
|
480
|
+
const message = createUserMessage({
|
|
481
|
+
content: [{ type: 'text', text }],
|
|
482
|
+
source: { kind: 'team-hub', form: 'boundary' },
|
|
483
|
+
})
|
|
484
|
+
const d = agent.ctx.effect(() => agent.inject(message))
|
|
485
|
+
disposers.set(sessionId, d)
|
|
486
|
+
} catch (error) { warn('注入边界失败:', error?.message) }
|
|
487
|
+
})
|
|
488
|
+
return () => { off?.(); for (const d of disposers.values()) try { d?.() } catch {} }
|
|
489
|
+
})
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
export function apply(ctx) {
|
|
493
|
+
const store = createStore()
|
|
494
|
+
part('服务', () => ctx.provide('teamHub', store))
|
|
495
|
+
part('HTTP API', () => mountApi(ctx, store))
|
|
496
|
+
part('工具', () => registerTools(ctx, store))
|
|
497
|
+
part('权限', () => registerPolicies(ctx, store))
|
|
498
|
+
log(`已加载;看板文件 ${store.file}`)
|
|
499
|
+
}
|
package/locale/en.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@restregular/dsh-team-hub",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "DSH 团队中枢:画布式 Agent 协作看板,支持跨会话派发任务、消息传递与权限边界管理。",
|
|
5
|
+
"icon": "./icon.svg",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./client": "./client.js",
|
|
10
|
+
"./locale/en.json": "./locale/en.json",
|
|
11
|
+
"./locale/zh.json": "./locale/zh.json",
|
|
12
|
+
"./package.json": "./package.json"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"index.js",
|
|
16
|
+
"client.js",
|
|
17
|
+
"cordis.patch.yml",
|
|
18
|
+
"locale/en.json",
|
|
19
|
+
"locale/zh.json",
|
|
20
|
+
"docs/DETAIL.md",
|
|
21
|
+
"icon.svg",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"dsh": {
|
|
25
|
+
"bundle": { "patch": "./cordis.patch.yml" },
|
|
26
|
+
"client": {
|
|
27
|
+
"platform": "web",
|
|
28
|
+
"immediately": true,
|
|
29
|
+
"inject": [
|
|
30
|
+
"@deepseek-ai/dsh-client-ui-slots",
|
|
31
|
+
"@deepseek-ai/dsh-api-session-controller",
|
|
32
|
+
"@deepseek-ai/dsh-api-workspace-controller"
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@deepseek-ai/cordis": "*",
|
|
38
|
+
"@deepseek-ai/dsh-tools": "*",
|
|
39
|
+
"@deepseek-ai/dsh-llm": "*"
|
|
40
|
+
},
|
|
41
|
+
"peerDependenciesMeta": {
|
|
42
|
+
"@deepseek-ai/cordis": { "optional": true },
|
|
43
|
+
"@deepseek-ai/dsh-tools": { "optional": true },
|
|
44
|
+
"@deepseek-ai/dsh-llm": { "optional": true }
|
|
45
|
+
},
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/RestRegular/dsh-team-hub"
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": { "access": "public" },
|
|
51
|
+
"keywords": ["dsh-plugin", "deepseek-harness", "agent-team", "canvas", "collaboration"],
|
|
52
|
+
"license": "MIT"
|
|
53
|
+
}
|