dsh-cothread-mcp 0.1.4 → 0.1.5

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,8 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.5
4
+ - 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.
5
+
3
6
  ## 0.1.4
4
7
  - 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
8
 
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.5",
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,12 +175,25 @@ function liveSessionOf(task) {
174
175
  return task.target.sessionId || ''
175
176
  }
176
177
 
177
- /** 记住的会话还能不能接着用:会话已归档就不能复用。 */
178
+ /**
179
+ * 记住的会话还能不能接着用。
180
+ *
181
+ * 会话已归档就不能复用。会话不在它该在的工作区里也不能:宿主在 session.create 里看到
182
+ * workspaceId 时,只复用工作区已经记着的会话,不在其中的会被另建一个——那样记住的还是
183
+ * 旧 id,映射与界面上挂的就对不上了。这种情况直接不复用,让宿主另建一个并挂进工作区。
184
+ */
178
185
  function reusableSession(saved, registry) {
179
186
  const sessionId = saved && typeof saved.sessionId === 'string' ? saved.sessionId : ''
180
187
  if (!sessionId) return ''
181
188
  const archived = registry && Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
182
- return archived.includes(sessionId) ? '' : sessionId
189
+ if (archived.includes(sessionId)) return ''
190
+ const workspaceId = saved && typeof saved.workspaceId === 'string' ? saved.workspaceId : ''
191
+ if (workspaceId && registry && typeof registry.get === 'function') {
192
+ const workspace = registry.get(workspaceId)
193
+ const member = workspace && Array.isArray(workspace.sessionIds) ? workspace.sessionIds : null
194
+ if (member && !member.includes(sessionId)) return ''
195
+ }
196
+ return sessionId
183
197
  }
184
198
 
185
199
  /** 把任务接回指定的会话;接不回就保持原样。 */
@@ -199,15 +213,17 @@ async function rebindTask(schedule, task, sessionId) {
199
213
  * 不会因为第一次投递才生成会话而多出一个空会话。
200
214
  */
201
215
  async function createTask(ctx, schedule, options) {
202
- const { title, prompt, seconds, cwd, sessionId } = options || {}
216
+ const { title, prompt, seconds, cwd, sessionId, workspaceId } = options || {}
203
217
  const timing = { kind: 'every', every_seconds: seconds }
204
218
  const wanted = sessionId || `session-${randomUUID()}`
205
219
  // 宿主要求「绑定某个会话」时那个会话已经存在,所以先把会话建出来,再让任务绑上去。
206
- const made = await ensureSession(ctx, wanted, cwd, title)
220
+ const made = await ensureSession(ctx, wanted, cwd, title, workspaceId)
207
221
  if (made.ok) {
222
+ // 宿主可能另建了一个会话,任务要绑的是它真正建出来的那个。
223
+ const bound = made.sessionId || wanted
208
224
  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 }
225
+ const task = await schedule.create({ title, prompt, timing, target: { kind: 'session', sessionId: bound } })
226
+ return { task, sessionId: bound, sessionName: nameSession(ctx, bound, title), sessionReady: true }
211
227
  } catch {
212
228
  // 绑定这个会话不成功时,退回由宿主预留一个新的会话。
213
229
  }
@@ -222,22 +238,51 @@ async function createTask(ctx, schedule, options) {
222
238
  * 任务用「新建会话并复用」时,宿主只是先预留一个会话 id,第一次投递才真正建出来;
223
239
  * 这里提前把它建出来,保存后就能直接打开会话,任务也立刻绑在这个会话上。
224
240
  */
225
- async function ensureSession(ctx, sessionId, cwd, title) {
241
+ async function ensureSession(ctx, sessionId, cwd, title, workspaceId = '') {
226
242
  const controller = serviceOf(ctx, 'sessionController')
227
243
  if (!controller || typeof controller.create !== 'function') return { ok: false, sessionId, reason: '当前 DSH 不支持直接创建会话' }
228
244
  if (!sessionId) return { ok: false, sessionId: '', reason: '没有会话身份' }
229
245
  const registry = serviceOf(ctx, 'workspaceRegistry')
230
246
  const archived = registry && Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
231
247
  if (archived.includes(sessionId)) return { ok: false, sessionId, reason: '会话已归档' }
248
+ let createdId = sessionId
232
249
  try {
233
- await controller.create({ sessionId, cwd })
250
+ // 宿主的 session.create 里 workspaceId 与 cwd 互斥:只给 cwd 时,会话只是「拥有」这个
251
+ // 目录,并不会被加进工作区的会话列表,界面上打开会话就看不到它挂在选中的工作区下。
252
+ // 给了 workspaceId,宿主会用工作区的路径作 cwd,并把会话正式挂进该工作区。
253
+ const created = await controller.create(workspaceId ? { sessionId, workspaceId } : { sessionId, cwd })
254
+ // 宿主可能复用或另建一个会话,任务要绑的是它真正建出来的那个。
255
+ if (created && typeof created.sessionId === 'string' && created.sessionId) createdId = created.sessionId
234
256
  } catch (error) {
235
257
  const message = error && error.message ? error.message : '创建会话失败'
236
258
  // 会话已经存在时不算失败,接着往下走命名。
237
259
  if (!/exist|already/i.test(message)) return { ok: false, sessionId, reason: message }
238
260
  }
239
- nameSession(ctx, sessionId, title)
240
- return { ok: true, sessionId }
261
+ // 会话早于本次改动建成、还没挂进工作区时,宿主不会替它补挂,这里补上。
262
+ await attachToWorkspace(ctx, workspaceId, createdId)
263
+ nameSession(ctx, createdId, title)
264
+ return { ok: true, sessionId: createdId }
265
+ }
266
+
267
+ /**
268
+ * 把会话挂进它该在的工作区。
269
+ *
270
+ * 宿主在 session.create 里带上 workspaceId 时会自己挂载;会话是先前留下的、已经存在,
271
+ * 宿主就不会补这一步,而工作区的会话列表是按挂载记录的,没有它会话就只是「属于」那个目录,
272
+ * 界面上打开会话看不到它挂在选中的工作区下。这里显式挂一次,幂等。
273
+ */
274
+ async function attachToWorkspace(ctx, workspaceId, sessionId) {
275
+ if (!workspaceId || !sessionId) return false
276
+ const registry = serviceOf(ctx, 'workspaceRegistry')
277
+ if (!registry || typeof registry.get !== 'function') return false
278
+ const workspace = registry.get(workspaceId)
279
+ if (!workspace || typeof workspace.attachSession !== 'function') return false
280
+ try {
281
+ await workspace.attachSession(sessionId)
282
+ return true
283
+ } catch {
284
+ return false
285
+ }
241
286
  }
242
287
 
243
288
  /** 给自动化会话起个项目名,界面上就能看到它叫什么。 */
@@ -339,7 +384,7 @@ export async function syncAutomations(ctx, options) {
339
384
  if (await deleteTask(schedule, extra.id)) { deleted++; deletedIds.add(extra.id) }
340
385
  }
341
386
  if (!task) {
342
- const made = await createTask(ctx, schedule, { title, prompt, seconds, cwd, sessionId: reusableSession(mapping[key], registry) })
387
+ const made = await createTask(ctx, schedule, { title, prompt, seconds, cwd, workspaceId: row.workspaceId, sessionId: reusableSession(mapping[key], registry) })
343
388
  task = made.task
344
389
  created++
345
390
  current = seconds
@@ -368,10 +413,12 @@ export async function syncAutomations(ctx, options) {
368
413
  // 会话当场建出来:重启后不用等第一次巡检就能打开。
369
414
  let sessionName = ''
370
415
  if (sessionId) {
371
- await ensureSession(ctx, sessionId, cwd, title)
416
+ const made = await ensureSession(ctx, sessionId, cwd, title, row.workspaceId)
417
+ // 宿主可能另建了会话,后面的映射要记真正在用的那个。
418
+ if (made.sessionId) sessionId = made.sessionId
372
419
  sessionName = nameSession(ctx, sessionId, title)
373
420
  }
374
- next[key] = { id: task.id, title, cwd, seconds: current, ...(sessionId ? { sessionId } : {}), ...(sessionName ? { sessionName } : {}) }
421
+ next[key] = { id: task.id, title, cwd, seconds: current, ...(row.workspaceId ? { workspaceId: row.workspaceId } : {}), ...(sessionId ? { sessionId } : {}), ...(sessionName ? { sessionName } : {}) }
375
422
  tasks.push({
376
423
  key,
377
424
  id: task.id,
@@ -476,7 +523,7 @@ export async function saveAutomation(ctx, options) {
476
523
  let created = 0, updated = 0
477
524
  let createdSession = null
478
525
  if (!task) {
479
- const made = await createTask(ctx, schedule, { title, prompt, seconds, cwd, sessionId: reusableSession(mapping[key], registry) })
526
+ const made = await createTask(ctx, schedule, { title, prompt, seconds, cwd, workspaceId: row.workspaceId, sessionId: reusableSession(mapping[key], registry) })
480
527
  task = made.task
481
528
  createdSession = made
482
529
  created++
@@ -504,8 +551,10 @@ export async function saveAutomation(ctx, options) {
504
551
  sessionReady = createdSession.sessionReady
505
552
  sessionName = createdSession.sessionName
506
553
  } else if (sessionId) {
507
- const made = await ensureSession(ctx, sessionId, cwd, title)
554
+ const made = await ensureSession(ctx, sessionId, cwd, title, row.workspaceId)
508
555
  sessionReady = made.ok
556
+ // 宿主可能另建了会话(原来的不在工作区里,不能复用),后面都要用真正建出来的那个。
557
+ if (made.sessionId) sessionId = made.sessionId
509
558
  sessionName = nameSession(ctx, sessionId, title)
510
559
  }
511
560
  const next = {}
@@ -514,7 +563,7 @@ export async function saveAutomation(ctx, options) {
514
563
  if (deletedIds.has(saved.id)) continue
515
564
  next[entryKey] = saved
516
565
  }
517
- next[key] = { id: task.id, title, cwd, seconds, ...(sessionId ? { sessionId } : {}), ...(sessionName ? { sessionName } : {}) }
566
+ next[key] = { id: task.id, title, cwd, seconds, ...(row.workspaceId ? { workspaceId: row.workspaceId } : {}), ...(sessionId ? { sessionId } : {}), ...(sessionName ? { sessionName } : {}) }
518
567
  await writeMapping(ctx, next)
519
568
  return {
520
569
  ok: true,