dsh-science-plugin 0.0.0-stage → 0.14.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/LICENSE +21 -0
- package/README.md +78 -2
- package/agents/_README.md +5 -0
- package/agents/analyst.md +27 -0
- package/agents/figure-expert.md +35 -0
- package/agents/literature-scout.md +32 -0
- package/agents/presentation.md +78 -0
- package/agents/reviewer.md +32 -0
- package/agents/simulator.md +30 -0
- package/agents/topic-designer.md +26 -0
- package/agents/trainer.md +36 -0
- package/agents/writer.md +44 -0
- package/assets/mascots/analyst.svg +33 -0
- package/assets/mascots/designer.svg +33 -0
- package/assets/mascots/figure.svg +35 -0
- package/assets/mascots/idle.svg +9 -0
- package/assets/mascots/lead.svg +33 -0
- package/assets/mascots/presentation.svg +40 -0
- package/assets/mascots/reviewer.svg +31 -0
- package/assets/mascots/scout.svg +31 -0
- package/assets/mascots/simulator.svg +33 -0
- package/assets/mascots/thinking.svg +23 -0
- package/assets/mascots/trainer.svg +32 -0
- package/assets/mascots/working.svg +9 -0
- package/assets/mascots/writer.svg +33 -0
- package/client/client.js +3425 -0
- package/client/client.js.map +7 -0
- package/cordis.patch.yml +12 -0
- package/docs/DEVLOG.md +864 -0
- package/docs/SPEC.md +452 -0
- package/docs/recipes/agent-teams-science.md +46 -0
- package/docs/research/2026-11-12-academic-figure-skill-/350/257/204/344/274/260.md +33 -0
- package/docs/research/2026-11-12-/347/224/237/347/211/251/347/247/221/347/240/224/350/203/275/345/212/233/344/270/216/345/267/245/344/275/234/345/217/260/351/233/206/346/210/220/350/260/203/347/240/224.md +169 -0
- package/docs/research/2026-11-12-/347/247/221/347/240/224/347/273/230/345/233/276/345/274/200/346/272/220/346/226/271/346/241/210/350/260/203/347/240/224.md +52 -0
- package/docs/research/2026-11-13-/345/257/271/350/257/235/351/251/261/345/212/250/347/247/221/347/240/224/345/267/245/344/275/234/345/217/260/344/272/247/345/223/201/347/255/226/347/225/245/350/260/203/347/240/224.md +319 -0
- package/docs/research/2026-11-13-/350/267/257/347/272/277B/346/212/200/346/234/257/345/217/257/350/241/214/346/200/247/344/270/216/344/275/216/347/274/272/351/231/267/350/256/276/350/256/241/350/260/203/347/240/224.md +65 -0
- package/docs/superpowers/specs/2026-11-12-/347/247/221/347/240/224/345/267/245/344/275/234/345/217/260/351/207/215/346/236/204/350/256/276/350/256/241.md +364 -0
- package/docs/superpowers/specs/2026-11-13-/345/257/271/350/257/235/344/274/230/345/205/210/347/247/221/347/240/224/345/215/241/347/211/207/345/267/245/344/275/234/345/217/260/351/207/215/346/236/204/350/256/276/350/256/241.md +607 -0
- package/icon.svg +6 -0
- package/lib/action-cards.js +194 -0
- package/lib/artifact-index.js +131 -0
- package/lib/blueprint.js +1487 -0
- package/lib/dag.js +69 -0
- package/lib/db.js +386 -0
- package/lib/figureqa.js +290 -0
- package/lib/figures.js +732 -0
- package/lib/httpdata-auth.js +91 -0
- package/lib/httpdata.js +862 -0
- package/lib/index.js +329 -0
- package/lib/lit.js +100 -0
- package/lib/paperspine.js +287 -0
- package/lib/project-adapter.js +259 -0
- package/lib/project-catalog.js +227 -0
- package/lib/project-context.js +282 -0
- package/lib/project-domain.js +849 -0
- package/lib/project-remote.js +348 -0
- package/lib/project-store.js +653 -0
- package/lib/rail-team.js +47 -0
- package/lib/resources.js +94 -0
- package/lib/reviewcov.js +238 -0
- package/lib/reviewer.js +638 -0
- package/lib/reviewruns.js +95 -0
- package/lib/roles.js +41 -0
- package/lib/router.js +133 -0
- package/lib/skeleton-context.js +181 -0
- package/lib/skillmenu.js +993 -0
- package/lib/ssh.js +637 -0
- package/lib/status.js +138 -0
- package/lib/store.js +50 -0
- package/lib/team.js +945 -0
- package/lib/vendor/ssh2.bundle.cjs +20124 -0
- package/lib/workspace-key.js +64 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +71 -6
- package/skills/sci-autoresearch/SKILL.md +41 -0
- package/skills/sci-figures/SKILL.md +91 -0
- package/skills/sci-figures/references/journal-requirements.md +22 -0
- package/skills/sci-figures/references/r-figures.md +106 -0
- package/skills/sci-figures/scripts/figure_export.py +45 -0
- package/skills/sci-figures/scripts/style_presets.py +82 -0
- package/skills/sci-hypothesis/SKILL.md +37 -0
- package/skills/sci-polish/SKILL.md +28 -0
- package/skills/sci-remote-jobs/SKILL.md +48 -0
- package/skills/sci-simulation/SKILL.md +70 -0
- package/skills/sci-simulation/references/tool-dossiers/_TEMPLATE.md +25 -0
- package/skills/sci-simulation/references/tool-dossiers/ase.md +42 -0
- package/skills/sci-simulation/references/tool-dossiers/gaussian.md +42 -0
- package/skills/sci-simulation/references/tool-dossiers/gromacs.md +37 -0
- package/skills/sci-simulation/references/tool-dossiers/lammps.md +34 -0
- package/skills/sci-simulation/references/tool-dossiers/quantum-espresso.md +34 -0
- package/skills/sci-simulation/references/universal-tool-onboarding.md +117 -0
- package/skills/sci-slides/SKILL.md +73 -0
- package/skills/sci-slides/scripts/make_deck.py +332 -0
- package/skills/sci-writing/SKILL.md +33 -0
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
// dsh-science — 项目 Remote 流(Project Remote,T0.3 spike → 阶段 3/4 正式消费)
|
|
2
|
+
//
|
|
3
|
+
// 设计真源 §10:项目状态使用独立的、按 session 鉴权的 Remote get/observe/act 流;
|
|
4
|
+
// DSH 原生 session projection 只折叠官方 durable events。typert Remote 需要官方
|
|
5
|
+
// 生成器产物(第三方插件无受支持注册面),因此公开 seam 是 webServer 路由 +
|
|
6
|
+
// connection.requestRejection 浏览器会话栅栏(与 /state、agent-teams 同款鉴权):
|
|
7
|
+
// GET /plugins/dsh-science/project/get?sessionId=…&knownRevision=… 快照/未变更
|
|
8
|
+
// GET /plugins/dsh-science/project/observe?sessionId=… SSE 基线+变更帧
|
|
9
|
+
// POST /plugins/dsh-science/project/act 白名单确定性动作
|
|
10
|
+
// GET /plugins/dsh-science/project/catalog?sessionId=… 工作区课题摘要
|
|
11
|
+
// POST /plugins/dsh-science/project/open 打开已绑定 root,不改绑
|
|
12
|
+
// scope 只由服务端解析(lib/project-context.js);客户端不能提交 projectId。
|
|
13
|
+
|
|
14
|
+
import { createHash } from 'node:crypto'
|
|
15
|
+
import { registerStateRouteShared } from './httpdata-auth.js'
|
|
16
|
+
import { resolveProjectContext, assertPaperWriteAllowed, ensureProject } from './project-context.js'
|
|
17
|
+
import { catalogForSession, openCatalogProject } from './project-catalog.js'
|
|
18
|
+
import { authorizeArtifactRead, locateProjectPath } from './artifact-index.js'
|
|
19
|
+
import { railTeamOf } from './rail-team.js'
|
|
20
|
+
import { StoreError } from './project-store.js'
|
|
21
|
+
import { confirmObjectRevision } from './project-domain.js'
|
|
22
|
+
|
|
23
|
+
function log(msg) {
|
|
24
|
+
console.log(`[dsh-science project-remote] ${msg}`)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const HEARTBEAT_MS = 15000
|
|
28
|
+
|
|
29
|
+
// ---- 客户端白名单动作(act)---------------------------------------------------
|
|
30
|
+
// 设计 §10.1:act 只接受确认/拒绝/暂停/停止/重试等确定性动作并在内部转 ProjectCommand;
|
|
31
|
+
// 语义变化没有通用 mutate 入口。冻结动作集:
|
|
32
|
+
// project.ensure-binding — 首次绑定/新建项目(走 ensureProject 统一规则:teammate→needs-binding)
|
|
33
|
+
// paper.confirm-revision — 对象级确认(经领域层 CAS:expectedRevision + expectedObjectRevision + expectedHash 必填)
|
|
34
|
+
const ACTIONS = new Set(['project.ensure-binding', 'paper.confirm-revision'])
|
|
35
|
+
|
|
36
|
+
function sendJson(res, code, payload) {
|
|
37
|
+
res.writeHead(code, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
38
|
+
res.end(JSON.stringify(payload))
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function registerProjectRemote(ctx, { store }) {
|
|
42
|
+
// ---- 动作实现(内部转 ProjectCommand)---------------------------------------
|
|
43
|
+
function runAction(sessionId, ref, body) {
|
|
44
|
+
const actionId = String(body.actionId || '')
|
|
45
|
+
if (!ACTIONS.has(actionId)) {
|
|
46
|
+
return { status: 400, payload: { kind: 'invalid', reason: `未知动作 ${actionId || '(空)'}` } }
|
|
47
|
+
}
|
|
48
|
+
const commandId = String(body.commandId || '')
|
|
49
|
+
if (!commandId) return { status: 400, payload: { kind: 'invalid', reason: 'commandId 必填(幂等键)' } }
|
|
50
|
+
|
|
51
|
+
if (actionId === 'project.ensure-binding') {
|
|
52
|
+
// M1:与 ensureProject 同一实现——teammate 在队长未绑定时返回 needs-binding,
|
|
53
|
+
// fork 隔离,绝不替队长建项目。
|
|
54
|
+
const ensured = ensureProject(ctx, sessionId, { store, title: String((body.payload && body.payload.title) || '') })
|
|
55
|
+
if (!ensured.ok) {
|
|
56
|
+
const status = ensured.code === 'fork-isolated' ? 403 : 409
|
|
57
|
+
return { status, payload: { kind: ensured.code === 'fork-isolated' ? 'forbidden' : 'needs-binding', reason: ensured.message || ensured.code } }
|
|
58
|
+
}
|
|
59
|
+
if (ensured.created) {
|
|
60
|
+
return { status: 200, payload: { kind: 'applied', receipt: { projectId: ensured.ref.projectId, bound: true, revision: 0 } } }
|
|
61
|
+
}
|
|
62
|
+
return { status: 200, payload: { kind: 'already-applied', receipt: { projectId: ensured.ref.projectId, bound: true } } }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// paper.confirm-revision:对象级 CAS 确认(领域层唯一实现,Action Card 同源)
|
|
66
|
+
const gate = assertPaperWriteAllowed(ref)
|
|
67
|
+
if (!gate.ok) {
|
|
68
|
+
return { status: 403, payload: { kind: 'forbidden', reason: gate.code === 'fork-isolated' ? 'fork 与父项目隔离' : '当前会话没有论文写权限' } }
|
|
69
|
+
}
|
|
70
|
+
// L6/T4:项目级 expectedRevision、对象级 expectedObjectRevision 与 expectedHash 均必填(§8.5)
|
|
71
|
+
if (body.expectedRevision == null) {
|
|
72
|
+
return { status: 400, payload: { kind: 'invalid', reason: 'expectedRevision 必填(确认绑定当前项目 revision)' } }
|
|
73
|
+
}
|
|
74
|
+
const payload = body.payload || {}
|
|
75
|
+
if (payload.expectedObjectRevision == null) {
|
|
76
|
+
return { status: 400, payload: { kind: 'invalid', reason: 'payload.expectedObjectRevision 必填(确认绑定当前对象修订)' } }
|
|
77
|
+
}
|
|
78
|
+
if (payload.expectedHash == null || String(payload.expectedHash).trim() === '') {
|
|
79
|
+
return { status: 400, payload: { kind: 'invalid', reason: 'payload.expectedHash 必填(确认绑定当前对象内容哈希)' } }
|
|
80
|
+
}
|
|
81
|
+
const options = { mutationId: commandId, expectedRevision: body.expectedRevision, source: 'ui-action' }
|
|
82
|
+
let result = confirmObjectRevision(store, ref, payload, options)
|
|
83
|
+
// §8.5:项目 revision 因无关对象改变、而目标对象 revision/hash 未变时,handler 重新读取目标
|
|
84
|
+
// 对象后允许(不因无关更新让卡片无故失效);目标对象变化仍由 object-conflict 拒绝。
|
|
85
|
+
if (result.kind === 'conflict' && result.code === 'conflict') {
|
|
86
|
+
result = confirmObjectRevision(store, ref, payload, { ...options, expectedRevision: null })
|
|
87
|
+
}
|
|
88
|
+
if (result.kind === 'applied') return { status: 200, payload: { kind: 'applied', receipt: result.receipt } }
|
|
89
|
+
if (result.kind === 'already-applied') return { status: 200, payload: { kind: 'already-applied', receipt: result.receipt } }
|
|
90
|
+
if (result.kind === 'conflict') {
|
|
91
|
+
return {
|
|
92
|
+
status: 409,
|
|
93
|
+
payload: {
|
|
94
|
+
kind: 'conflict',
|
|
95
|
+
code: result.code || null,
|
|
96
|
+
reason: result.reason,
|
|
97
|
+
currentRevision: result.currentRevision,
|
|
98
|
+
objectRevision: result.objectRevision,
|
|
99
|
+
currentHash: result.currentHash,
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return { status: 400, payload: { kind: 'invalid', reason: result.reason } }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function scopeOf(sessionId) {
|
|
107
|
+
return resolveProjectContext(ctx, sessionId, { store })
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const handlers = {
|
|
111
|
+
async catalog(req, res, url) {
|
|
112
|
+
const sessionId = url.searchParams.get('sessionId')
|
|
113
|
+
const result = catalogForSession(ctx, sessionId, { store })
|
|
114
|
+
return sendJson(res, result.status, result.body)
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
async open(req, res, url, body) {
|
|
118
|
+
const sessionId = body && body.sessionId
|
|
119
|
+
const before = sessionId && store.bindingOfRootSession(sessionId)
|
|
120
|
+
const result = openCatalogProject(ctx, sessionId, body, { store })
|
|
121
|
+
const after = sessionId && store.bindingOfRootSession(sessionId)
|
|
122
|
+
if ((before && before.projectId) !== (after && after.projectId)) {
|
|
123
|
+
return sendJson(res, 500, { error: 'binding-changed' })
|
|
124
|
+
}
|
|
125
|
+
return sendJson(res, result.status, result.body)
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
async artifact(req, res, url) {
|
|
129
|
+
const scope = scopeOf(url.searchParams.get('sessionId'))
|
|
130
|
+
if (!scope.ok || !scope.ref.projectId || !scope.ref.capabilities.read) return sendJson(res, 404, { error: 'not-found' })
|
|
131
|
+
const snap = store.snapshotOf(scope.ref.projectId)
|
|
132
|
+
const artifactId = url.searchParams.get('artifactId')
|
|
133
|
+
const record = snap && snap.snapshot && snap.snapshot.artifactIndex
|
|
134
|
+
? (snap.snapshot.artifactIndex.items || []).find((item) => item.id === artifactId)
|
|
135
|
+
: null
|
|
136
|
+
const decision = authorizeArtifactRead({
|
|
137
|
+
record,
|
|
138
|
+
projectId: scope.ref.projectId,
|
|
139
|
+
hostAllows: scope.ref.capabilities.read === true,
|
|
140
|
+
locate: (item) => locateProjectPath(item.pathRef, {
|
|
141
|
+
root: typeof ctx.get === 'function' && ctx.get('workspace') ? ctx.get('workspace').root : '',
|
|
142
|
+
exists: () => false,
|
|
143
|
+
versionOf: () => '',
|
|
144
|
+
}),
|
|
145
|
+
})
|
|
146
|
+
if (!decision.ok) return sendJson(res, decision.status, { error: decision.reason, diagnostic: decision.diagnostic || '' })
|
|
147
|
+
return sendJson(res, 200, { ok: true, artifactId: decision.artifactId, summary: decision.summary })
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
async get(req, res, url) {
|
|
151
|
+
const sessionId = url.searchParams.get('sessionId')
|
|
152
|
+
const scope = scopeOf(sessionId)
|
|
153
|
+
if (!scope.ok) return sendJson(res, 404, { error: scope.code })
|
|
154
|
+
const ref = scope.ref
|
|
155
|
+
if (!ref.projectId) return sendJson(res, 200, { project: null, ref: publicRef(ref) })
|
|
156
|
+
if (!ref.capabilities.read) return sendJson(res, 403, { error: 'forbidden' })
|
|
157
|
+
const snap = store.snapshotOf(ref.projectId)
|
|
158
|
+
if (snap && snap.corrupt) {
|
|
159
|
+
// M8:行级损坏 → 结构化只读恢复错误,不泄漏 SQL/路径
|
|
160
|
+
return sendJson(res, 500, { error: 'snapshot-corrupt', revision: snap.revision })
|
|
161
|
+
}
|
|
162
|
+
const known = url.searchParams.get('knownRevision')
|
|
163
|
+
if (known != null && Number(known) === snap.revision) {
|
|
164
|
+
return sendJson(res, 200, { notModified: true, revision: snap.revision, projectId: ref.projectId })
|
|
165
|
+
}
|
|
166
|
+
return sendJson(res, 200, {
|
|
167
|
+
project: { projectId: ref.projectId, revision: snap.revision, snapshot: snap.snapshot },
|
|
168
|
+
team: railTeamOf(ref, { store }),
|
|
169
|
+
ref: publicRef(ref),
|
|
170
|
+
})
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
async observe(req, res, url) {
|
|
174
|
+
const sessionId = url.searchParams.get('sessionId')
|
|
175
|
+
const scope = scopeOf(sessionId)
|
|
176
|
+
if (!scope.ok) return sendJson(res, 404, { error: scope.code })
|
|
177
|
+
const ref = scope.ref
|
|
178
|
+
res.writeHead(200, {
|
|
179
|
+
'content-type': 'text/event-stream; charset=utf-8',
|
|
180
|
+
'cache-control': 'no-store',
|
|
181
|
+
connection: 'keep-alive',
|
|
182
|
+
'x-accel-buffering': 'no',
|
|
183
|
+
})
|
|
184
|
+
let closed = false
|
|
185
|
+
const cleanupFns = []
|
|
186
|
+
const cleanup = () => {
|
|
187
|
+
if (closed) return
|
|
188
|
+
closed = true
|
|
189
|
+
for (const fn of cleanupFns) { try { fn() } catch { /* ignore */ } }
|
|
190
|
+
try { res.end() } catch { /* ignore */ }
|
|
191
|
+
}
|
|
192
|
+
const send = (event, data) => {
|
|
193
|
+
if (closed) return false
|
|
194
|
+
try { res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`); return true } catch { cleanup(); return false }
|
|
195
|
+
}
|
|
196
|
+
let currentProjectId = ref.projectId
|
|
197
|
+
let lastRevision = null
|
|
198
|
+
if (currentProjectId && ref.capabilities.read) {
|
|
199
|
+
const snap = store.snapshotOf(currentProjectId)
|
|
200
|
+
lastRevision = snap && !snap.corrupt ? snap.revision : null
|
|
201
|
+
send('baseline', { projectId: currentProjectId, revision: lastRevision, ref: publicRef(ref), ...(snap && snap.corrupt ? { streamFailure: 'snapshot-corrupt' } : {}) })
|
|
202
|
+
} else {
|
|
203
|
+
send('baseline', { projectId: null, revision: null, ref: publicRef(ref) })
|
|
204
|
+
}
|
|
205
|
+
const unsubscribe = store.subscribe((frame) => {
|
|
206
|
+
if (frame.projectId !== currentProjectId) return
|
|
207
|
+
// gap 检测:客户端按 revision 判断缺口;服务端始终发送新帧,缺口由客户端补读 baseline。
|
|
208
|
+
send('change', frame)
|
|
209
|
+
lastRevision = frame.projectRevision
|
|
210
|
+
})
|
|
211
|
+
cleanupFns.push(unsubscribe)
|
|
212
|
+
const heartbeat = setInterval(() => {
|
|
213
|
+
if (closed) return
|
|
214
|
+
try { res.write(': ping\n\n') } catch { cleanup() }
|
|
215
|
+
}, HEARTBEAT_MS)
|
|
216
|
+
// M5/M7:兜底轮询(1s)同时覆盖 绑定建立 与 跨实例/丢帧的 revision 前进;
|
|
217
|
+
// 回调整体 try/catch——插件卸载关闭 DB 后不得让未捕获异常击穿进程。
|
|
218
|
+
const rebinding = setInterval(() => {
|
|
219
|
+
if (closed) { clearInterval(rebinding); return }
|
|
220
|
+
try {
|
|
221
|
+
const scope2 = scopeOf(sessionId)
|
|
222
|
+
if (!scope2.ok || !scope2.ref) {
|
|
223
|
+
// M7:会话已下线 → 明确流失败并结束,不再保留僵尸连接
|
|
224
|
+
send('streamFailure', { reason: 'session-unavailable' })
|
|
225
|
+
clearInterval(heartbeat)
|
|
226
|
+
clearInterval(rebinding)
|
|
227
|
+
cleanup()
|
|
228
|
+
return
|
|
229
|
+
}
|
|
230
|
+
const ref2 = scope2.ref
|
|
231
|
+
if (ref2.projectId && ref2.projectId !== currentProjectId) {
|
|
232
|
+
currentProjectId = ref2.projectId
|
|
233
|
+
const rev = store.revisionOf(currentProjectId)
|
|
234
|
+
lastRevision = rev
|
|
235
|
+
if (ref2.capabilities.read || ref.capabilities.read) {
|
|
236
|
+
send('reset', { projectId: currentProjectId, revision: rev, ref: publicRef(ref2) })
|
|
237
|
+
}
|
|
238
|
+
return
|
|
239
|
+
}
|
|
240
|
+
// M5:同项目 revision 兜底——跨实例写入/丢帧后补发 change 帧
|
|
241
|
+
if (currentProjectId && ref2.capabilities.read) {
|
|
242
|
+
const rev = store.revisionOf(currentProjectId)
|
|
243
|
+
if (rev != null && lastRevision != null && rev > lastRevision) {
|
|
244
|
+
send('change', { projectId: currentProjectId, projectRevision: rev, changedKeys: [], operationId: 'resync', updatedAt: Date.now() })
|
|
245
|
+
lastRevision = rev
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
} catch (err) {
|
|
249
|
+
// 插件卸载/DB 关闭等:结束流并清理(M7)
|
|
250
|
+
log(`observe poll failed: ${err && err.message}`)
|
|
251
|
+
clearInterval(heartbeat)
|
|
252
|
+
clearInterval(rebinding)
|
|
253
|
+
cleanup()
|
|
254
|
+
}
|
|
255
|
+
}, 1000)
|
|
256
|
+
cleanupFns.push(() => { clearInterval(heartbeat); clearInterval(rebinding) })
|
|
257
|
+
req.on('close', cleanup)
|
|
258
|
+
},
|
|
259
|
+
|
|
260
|
+
async act(req, res, url, body) {
|
|
261
|
+
const sessionId = body && body.sessionId
|
|
262
|
+
const scope = scopeOf(sessionId)
|
|
263
|
+
if (!scope.ok) return sendJson(res, 404, { error: scope.code })
|
|
264
|
+
const outcome = runAction(sessionId, scope.ref, body || {})
|
|
265
|
+
return sendJson(res, outcome.status, outcome.payload)
|
|
266
|
+
},
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function publicRef(ref) {
|
|
270
|
+
return {
|
|
271
|
+
projectId: ref.projectId,
|
|
272
|
+
rootSessionId: ref.rootSessionId,
|
|
273
|
+
relationship: ref.relationship,
|
|
274
|
+
capabilities: ref.capabilities,
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
registerStateRouteShared(ctx, [
|
|
279
|
+
{ path: '/plugins/dsh-science/project/get', handler: (req, res, url) => handlers.get(req, res, url) },
|
|
280
|
+
{ path: '/plugins/dsh-science/project/artifact', handler: (req, res, url) => handlers.artifact(req, res, url) },
|
|
281
|
+
{ path: '/plugins/dsh-science/project/catalog', handler: (req, res, url) => handlers.catalog(req, res, url) },
|
|
282
|
+
{
|
|
283
|
+
path: '/plugins/dsh-science/project/open',
|
|
284
|
+
handler: async (req, res, url) => {
|
|
285
|
+
const contentType = String((req.headers && req.headers['content-type']) || '')
|
|
286
|
+
if (contentType && !/application\/json/i.test(contentType)) {
|
|
287
|
+
return sendJson(res, 415, { error: 'unsupported content-type' })
|
|
288
|
+
}
|
|
289
|
+
const body = await readJsonBody(req)
|
|
290
|
+
if (!body) return sendJson(res, 400, { error: 'invalid JSON body' })
|
|
291
|
+
return handlers.open(req, res, url, body)
|
|
292
|
+
},
|
|
293
|
+
method: 'POST',
|
|
294
|
+
failClosed: true,
|
|
295
|
+
},
|
|
296
|
+
{ path: '/plugins/dsh-science/project/observe', handler: (req, res, url) => handlers.observe(req, res, url) },
|
|
297
|
+
{
|
|
298
|
+
path: '/plugins/dsh-science/project/act',
|
|
299
|
+
handler: async (req, res, url) => {
|
|
300
|
+
// L13:防御纵深——只接受 JSON 请求体(跨站表单/sendBeacon 不是 application/json)
|
|
301
|
+
const contentType = String((req.headers && req.headers['content-type']) || '')
|
|
302
|
+
if (contentType && !/application\/(json|text\/plain)/i.test(contentType)) {
|
|
303
|
+
return sendJson(res, 415, { error: 'unsupported content-type' })
|
|
304
|
+
}
|
|
305
|
+
const body = await readJsonBody(req)
|
|
306
|
+
if (!body) return sendJson(res, 400, { error: 'invalid JSON body' })
|
|
307
|
+
return handlers.act(req, res, url, body)
|
|
308
|
+
},
|
|
309
|
+
method: 'POST',
|
|
310
|
+
// L3:act 是写端点——connection 栅栏缺失时 fail-closed(401),不做回环放行
|
|
311
|
+
failClosed: true,
|
|
312
|
+
},
|
|
313
|
+
])
|
|
314
|
+
log('project remote registered: get/observe/act/catalog/open under /plugins/dsh-science/project/*')
|
|
315
|
+
return handlers
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function readJsonBody(req, maxBytes = 64 * 1024) {
|
|
319
|
+
return new Promise((resolveP) => {
|
|
320
|
+
const chunks = []
|
|
321
|
+
let size = 0
|
|
322
|
+
let done = false
|
|
323
|
+
const finish = (ok) => {
|
|
324
|
+
if (done) return
|
|
325
|
+
done = true
|
|
326
|
+
if (!ok) {
|
|
327
|
+
// L6:超限即断开,避免继续空读
|
|
328
|
+
try { req.destroy() } catch { /* ignore */ }
|
|
329
|
+
return resolveP(null)
|
|
330
|
+
}
|
|
331
|
+
try {
|
|
332
|
+
resolveP(JSON.parse(Buffer.concat(chunks).toString('utf8')))
|
|
333
|
+
} catch {
|
|
334
|
+
resolveP(null)
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
req.on('data', (c) => {
|
|
338
|
+
size += c.length
|
|
339
|
+
if (size > maxBytes) {
|
|
340
|
+
finish(false)
|
|
341
|
+
return
|
|
342
|
+
}
|
|
343
|
+
chunks.push(c)
|
|
344
|
+
})
|
|
345
|
+
req.on('end', () => finish(true))
|
|
346
|
+
req.on('error', () => finish(false))
|
|
347
|
+
})
|
|
348
|
+
}
|