dsh-cothread-mcp 0.1.4 → 0.1.6

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/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.6
4
+ - Fix the automation session still showing up outside the selected workspace. The session was created against the workspace's directory, so it only owned that directory and never joined the workspace's session list; it is now created against the workspace itself, which makes the host attach it there. Sessions left outside every workspace by earlier versions are adopted into the workspace they belong to the next time the project is saved, and the task binds to the session the host actually created.
5
+ ## 0.1.5
6
+ - Fix the automation session not appearing under the selected workspace. The session was created with the workspace's directory as its working directory, but a session only joins a workspace when the host is told the workspace itself, so opening the session showed it outside the workspace that was picked. The session is now created against the workspace, which makes the host attach it to that workspace's session list, and the automation task binds to the session the host actually created.
7
+
3
8
  ## 0.1.4
4
9
  - Fix the automation task update failing with `cannot get property "sessions" without inject`. The session-naming helper read the host's session registry through the context's property access, which Cordis rejects unless the plugin declares the service as an injected dependency; the error escaped the task assembly and left the settings saved but the automation task unchanged. The service is now read the same lenient way as every other host service the plugin uses, so a missing registry only means the session stays unnamed.
5
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cothread-mcp",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "连接共序 CoThread MCP,支持浏览器授权、项目设置及消息提醒。",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/automation.js CHANGED
@@ -115,6 +115,7 @@ export async function readMapping(ctx) {
115
115
  title: typeof value.title === 'string' ? value.title : '',
116
116
  cwd: typeof value.cwd === 'string' ? value.cwd : '',
117
117
  seconds: Number(value.seconds) || 0,
118
+ ...(typeof value.workspaceId === 'string' && value.workspaceId ? { workspaceId: value.workspaceId } : {}),
118
119
  ...(typeof value.sessionId === 'string' && value.sessionId ? { sessionId: value.sessionId } : {}),
119
120
  ...(typeof value.sessionName === 'string' && value.sessionName ? { sessionName: value.sessionName } : {}),
120
121
  }
@@ -174,7 +175,12 @@ function liveSessionOf(task) {
174
175
  return task.target.sessionId || ''
175
176
  }
176
177
 
177
- /** 记住的会话还能不能接着用:会话已归档就不能复用。 */
178
+ /**
179
+ * 记住的会话还能不能接着用:会话已归档就不能复用。
180
+ *
181
+ * 会话没挂进工作区也能复用:ensureSession 会先把它挂进去,宿主随后复用同一个会话,
182
+ * 不用另建一个,记住的 id 也就一直对得上。
183
+ */
178
184
  function reusableSession(saved, registry) {
179
185
  const sessionId = saved && typeof saved.sessionId === 'string' ? saved.sessionId : ''
180
186
  if (!sessionId) return ''
@@ -199,15 +205,17 @@ async function rebindTask(schedule, task, sessionId) {
199
205
  * 不会因为第一次投递才生成会话而多出一个空会话。
200
206
  */
201
207
  async function createTask(ctx, schedule, options) {
202
- const { title, prompt, seconds, cwd, sessionId } = options || {}
208
+ const { title, prompt, seconds, cwd, sessionId, workspaceId } = options || {}
203
209
  const timing = { kind: 'every', every_seconds: seconds }
204
210
  const wanted = sessionId || `session-${randomUUID()}`
205
211
  // 宿主要求「绑定某个会话」时那个会话已经存在,所以先把会话建出来,再让任务绑上去。
206
- const made = await ensureSession(ctx, wanted, cwd, title)
212
+ const made = await ensureSession(ctx, wanted, cwd, title, workspaceId)
207
213
  if (made.ok) {
214
+ // 宿主可能另建了一个会话,任务要绑的是它真正建出来的那个。
215
+ const bound = made.sessionId || wanted
208
216
  try {
209
- const task = await schedule.create({ title, prompt, timing, target: { kind: 'session', sessionId: wanted } })
210
- return { task, sessionId: wanted, sessionName: nameSession(ctx, wanted, title), sessionReady: true }
217
+ const task = await schedule.create({ title, prompt, timing, target: { kind: 'session', sessionId: bound } })
218
+ return { task, sessionId: bound, sessionName: nameSession(ctx, bound, title), sessionReady: true }
211
219
  } catch {
212
220
  // 绑定这个会话不成功时,退回由宿主预留一个新的会话。
213
221
  }
@@ -222,22 +230,93 @@ async function createTask(ctx, schedule, options) {
222
230
  * 任务用「新建会话并复用」时,宿主只是先预留一个会话 id,第一次投递才真正建出来;
223
231
  * 这里提前把它建出来,保存后就能直接打开会话,任务也立刻绑在这个会话上。
224
232
  */
225
- async function ensureSession(ctx, sessionId, cwd, title) {
233
+ async function ensureSession(ctx, sessionId, cwd, title, workspaceId = '') {
226
234
  const controller = serviceOf(ctx, 'sessionController')
227
235
  if (!controller || typeof controller.create !== 'function') return { ok: false, sessionId, reason: '当前 DSH 不支持直接创建会话' }
228
236
  if (!sessionId) return { ok: false, sessionId: '', reason: '没有会话身份' }
229
237
  const registry = serviceOf(ctx, 'workspaceRegistry')
230
238
  const archived = registry && Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
231
239
  if (archived.includes(sessionId)) return { ok: false, sessionId, reason: '会话已归档' }
240
+ let createdId = sessionId
241
+ // 先挂载,再创建。
242
+ // 宿主在 session.create 里带上 workspaceId 时,只复用工作区已经记着的会话;会话没挂上去
243
+ // 就会被另建一个,于是记住的还是旧 id,旧会话继续留在「未分组」里。先挂载,宿主就复用它。
244
+ // 会话还不存在时挂载会失败(读不到它的 header),忽略即可,宿主创建时会自己挂上。
245
+ await attachToWorkspace(ctx, workspaceId, sessionId)
232
246
  try {
233
- await controller.create({ sessionId, cwd })
247
+ // 宿主的 session.create 里 workspaceId 与 cwd 互斥:只给 cwd 时,会话只是「拥有」这个
248
+ // 目录,并不会被加进工作区的会话列表,界面上打开会话就看不到它挂在选中的工作区下。
249
+ // 给了 workspaceId,宿主会用工作区的路径作 cwd,并把会话正式挂进该工作区。
250
+ const created = await controller.create(workspaceId ? { sessionId, workspaceId } : { sessionId, cwd })
251
+ // 宿主可能另建了一个会话,任务要绑的是它真正建出来的那个。
252
+ if (created && typeof created.sessionId === 'string' && created.sessionId) createdId = created.sessionId
234
253
  } catch (error) {
235
254
  const message = error && error.message ? error.message : '创建会话失败'
236
255
  // 会话已经存在时不算失败,接着往下走命名。
237
256
  if (!/exist|already/i.test(message)) return { ok: false, sessionId, reason: message }
238
257
  }
239
- nameSession(ctx, sessionId, title)
240
- return { ok: true, sessionId }
258
+ // 宿主另建了会话时,它自己会挂进工作区;这里再确认一次,幂等。
259
+ await attachToWorkspace(ctx, workspaceId, createdId)
260
+ nameSession(ctx, createdId, title)
261
+ return { ok: true, sessionId: createdId }
262
+ }
263
+
264
+ /**
265
+ * 把会话挂进它该在的工作区。
266
+ *
267
+ * 宿主在 session.create 里带上 workspaceId 时会自己挂载;会话是先前留下的、已经存在,
268
+ * 宿主就不会补这一步,而工作区的会话列表是按挂载记录的,没有它会话就只是「属于」那个目录,
269
+ * 界面上打开会话看不到它挂在选中的工作区下。这里显式挂一次,幂等。
270
+ */
271
+ async function attachToWorkspace(ctx, workspaceId, sessionId) {
272
+ if (!workspaceId || !sessionId) return false
273
+ const registry = serviceOf(ctx, 'workspaceRegistry')
274
+ if (!registry || typeof registry.get !== 'function') return false
275
+ const workspace = registry.get(workspaceId)
276
+ if (!workspace || typeof workspace.attachSession !== 'function') return false
277
+ try {
278
+ await workspace.attachSession(sessionId)
279
+ return true
280
+ } catch {
281
+ return false
282
+ }
283
+ }
284
+
285
+ /**
286
+ * 把还留在「未分组」里的会话归位到它该在的工作区。
287
+ *
288
+ * 早期版本用工作区的目录路径创建会话,没有把会话挂进工作区,那些会话至今留在「未分组」里,
289
+ * 界面上打开它们看不到它们属于所选的工作区。这里按目录把它们找出来补挂上。归档的会话本来
290
+ * 就留在「未分组」,不参与归位。
291
+ */
292
+ async function adoptOrphanSessions(ctx, workspaceId, cwd) {
293
+ if (!workspaceId || !cwd) return 0
294
+ const registry = serviceOf(ctx, 'workspaceRegistry')
295
+ if (!registry || typeof registry.get !== 'function') return 0
296
+ const workspace = registry.get(workspaceId)
297
+ if (!workspace) return 0
298
+ const persistence = serviceOf(ctx, 'sessionPersistence')
299
+ if (!persistence || typeof persistence.list !== 'function') return 0
300
+ const members = Array.isArray(workspace.sessionIds) ? workspace.sessionIds : []
301
+ const archived = Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
302
+ let adopted = 0
303
+ let snapshots = []
304
+ try {
305
+ snapshots = await persistence.list()
306
+ } catch {
307
+ return 0
308
+ }
309
+ for (const snapshot of snapshots) {
310
+ const header = snapshot && snapshot.header ? snapshot.header : undefined
311
+ const id = header && header.id ? header.id : ''
312
+ if (!id || members.includes(id)) continue
313
+ // 归档的会话本来就留在「未分组」,不归位。
314
+ if (archived.includes(id)) continue
315
+ if (!header || header.cwd !== cwd) continue
316
+ // 只归位这个目录下的会话;attachSession 自己会再校验一次目录,不匹配就拒绝。
317
+ if (await attachToWorkspace(ctx, workspaceId, id)) adopted++
318
+ }
319
+ return adopted
241
320
  }
242
321
 
243
322
  /** 给自动化会话起个项目名,界面上就能看到它叫什么。 */
@@ -339,7 +418,7 @@ export async function syncAutomations(ctx, options) {
339
418
  if (await deleteTask(schedule, extra.id)) { deleted++; deletedIds.add(extra.id) }
340
419
  }
341
420
  if (!task) {
342
- const made = await createTask(ctx, schedule, { title, prompt, seconds, cwd, sessionId: reusableSession(mapping[key], registry) })
421
+ const made = await createTask(ctx, schedule, { title, prompt, seconds, cwd, workspaceId: row.workspaceId, sessionId: reusableSession(mapping[key], registry) })
343
422
  task = made.task
344
423
  created++
345
424
  current = seconds
@@ -368,10 +447,12 @@ export async function syncAutomations(ctx, options) {
368
447
  // 会话当场建出来:重启后不用等第一次巡检就能打开。
369
448
  let sessionName = ''
370
449
  if (sessionId) {
371
- await ensureSession(ctx, sessionId, cwd, title)
450
+ const made = await ensureSession(ctx, sessionId, cwd, title, row.workspaceId)
451
+ // 宿主可能另建了会话,后面的映射要记真正在用的那个。
452
+ if (made.sessionId) sessionId = made.sessionId
372
453
  sessionName = nameSession(ctx, sessionId, title)
373
454
  }
374
- next[key] = { id: task.id, title, cwd, seconds: current, ...(sessionId ? { sessionId } : {}), ...(sessionName ? { sessionName } : {}) }
455
+ next[key] = { id: task.id, title, cwd, seconds: current, ...(row.workspaceId ? { workspaceId: row.workspaceId } : {}), ...(sessionId ? { sessionId } : {}), ...(sessionName ? { sessionName } : {}) }
375
456
  tasks.push({
376
457
  key,
377
458
  id: task.id,
@@ -476,7 +557,7 @@ export async function saveAutomation(ctx, options) {
476
557
  let created = 0, updated = 0
477
558
  let createdSession = null
478
559
  if (!task) {
479
- const made = await createTask(ctx, schedule, { title, prompt, seconds, cwd, sessionId: reusableSession(mapping[key], registry) })
560
+ const made = await createTask(ctx, schedule, { title, prompt, seconds, cwd, workspaceId: row.workspaceId, sessionId: reusableSession(mapping[key], registry) })
480
561
  task = made.task
481
562
  createdSession = made
482
563
  created++
@@ -497,6 +578,8 @@ export async function saveAutomation(ctx, options) {
497
578
  const rebound = await rebindTask(schedule, task, remembered)
498
579
  if (rebound !== sessionId) { sessionId = rebound; updated++ }
499
580
  }
581
+ // 早期版本留下的会话还在「未分组」里,先把它们归位到选中的工作区。
582
+ await adoptOrphanSessions(ctx, row.workspaceId, cwd)
500
583
  // 会话当场建出来:保存完就能打开它,任务也已经绑在它上面。
501
584
  let sessionReady = false
502
585
  let sessionName = ''
@@ -504,8 +587,10 @@ export async function saveAutomation(ctx, options) {
504
587
  sessionReady = createdSession.sessionReady
505
588
  sessionName = createdSession.sessionName
506
589
  } else if (sessionId) {
507
- const made = await ensureSession(ctx, sessionId, cwd, title)
590
+ const made = await ensureSession(ctx, sessionId, cwd, title, row.workspaceId)
508
591
  sessionReady = made.ok
592
+ // 宿主可能另建了会话(原来的不在工作区里,不能复用),后面都要用真正建出来的那个。
593
+ if (made.sessionId) sessionId = made.sessionId
509
594
  sessionName = nameSession(ctx, sessionId, title)
510
595
  }
511
596
  const next = {}
@@ -514,7 +599,7 @@ export async function saveAutomation(ctx, options) {
514
599
  if (deletedIds.has(saved.id)) continue
515
600
  next[entryKey] = saved
516
601
  }
517
- next[key] = { id: task.id, title, cwd, seconds, ...(sessionId ? { sessionId } : {}), ...(sessionName ? { sessionName } : {}) }
602
+ next[key] = { id: task.id, title, cwd, seconds, ...(row.workspaceId ? { workspaceId: row.workspaceId } : {}), ...(sessionId ? { sessionId } : {}), ...(sessionName ? { sessionName } : {}) }
518
603
  await writeMapping(ctx, next)
519
604
  return {
520
605
  ok: true,