dsh-cothread-mcp 0.1.5 → 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,7 @@
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.
3
5
  ## 0.1.5
4
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.
5
7
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cothread-mcp",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "连接共序 CoThread MCP,支持浏览器授权、项目设置及消息提醒。",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/automation.js CHANGED
@@ -176,24 +176,16 @@ function liveSessionOf(task) {
176
176
  }
177
177
 
178
178
  /**
179
- * 记住的会话还能不能接着用。
179
+ * 记住的会话还能不能接着用:会话已归档就不能复用。
180
180
  *
181
- * 会话已归档就不能复用。会话不在它该在的工作区里也不能:宿主在 session.create 里看到
182
- * workspaceId 时,只复用工作区已经记着的会话,不在其中的会被另建一个——那样记住的还是
183
- * 旧 id,映射与界面上挂的就对不上了。这种情况直接不复用,让宿主另建一个并挂进工作区。
181
+ * 会话没挂进工作区也能复用:ensureSession 会先把它挂进去,宿主随后复用同一个会话,
182
+ * 不用另建一个,记住的 id 也就一直对得上。
184
183
  */
185
184
  function reusableSession(saved, registry) {
186
185
  const sessionId = saved && typeof saved.sessionId === 'string' ? saved.sessionId : ''
187
186
  if (!sessionId) return ''
188
187
  const archived = registry && Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
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
188
+ return archived.includes(sessionId) ? '' : sessionId
197
189
  }
198
190
 
199
191
  /** 把任务接回指定的会话;接不回就保持原样。 */
@@ -246,19 +238,24 @@ async function ensureSession(ctx, sessionId, cwd, title, workspaceId = '') {
246
238
  const archived = registry && Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
247
239
  if (archived.includes(sessionId)) return { ok: false, sessionId, reason: '会话已归档' }
248
240
  let createdId = sessionId
241
+ // 先挂载,再创建。
242
+ // 宿主在 session.create 里带上 workspaceId 时,只复用工作区已经记着的会话;会话没挂上去
243
+ // 就会被另建一个,于是记住的还是旧 id,旧会话继续留在「未分组」里。先挂载,宿主就复用它。
244
+ // 会话还不存在时挂载会失败(读不到它的 header),忽略即可,宿主创建时会自己挂上。
245
+ await attachToWorkspace(ctx, workspaceId, sessionId)
249
246
  try {
250
247
  // 宿主的 session.create 里 workspaceId 与 cwd 互斥:只给 cwd 时,会话只是「拥有」这个
251
248
  // 目录,并不会被加进工作区的会话列表,界面上打开会话就看不到它挂在选中的工作区下。
252
249
  // 给了 workspaceId,宿主会用工作区的路径作 cwd,并把会话正式挂进该工作区。
253
250
  const created = await controller.create(workspaceId ? { sessionId, workspaceId } : { sessionId, cwd })
254
- // 宿主可能复用或另建一个会话,任务要绑的是它真正建出来的那个。
251
+ // 宿主可能另建了一个会话,任务要绑的是它真正建出来的那个。
255
252
  if (created && typeof created.sessionId === 'string' && created.sessionId) createdId = created.sessionId
256
253
  } catch (error) {
257
254
  const message = error && error.message ? error.message : '创建会话失败'
258
255
  // 会话已经存在时不算失败,接着往下走命名。
259
256
  if (!/exist|already/i.test(message)) return { ok: false, sessionId, reason: message }
260
257
  }
261
- // 会话早于本次改动建成、还没挂进工作区时,宿主不会替它补挂,这里补上。
258
+ // 宿主另建了会话时,它自己会挂进工作区;这里再确认一次,幂等。
262
259
  await attachToWorkspace(ctx, workspaceId, createdId)
263
260
  nameSession(ctx, createdId, title)
264
261
  return { ok: true, sessionId: createdId }
@@ -285,6 +282,43 @@ async function attachToWorkspace(ctx, workspaceId, sessionId) {
285
282
  }
286
283
  }
287
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
320
+ }
321
+
288
322
  /** 给自动化会话起个项目名,界面上就能看到它叫什么。 */
289
323
  export function nameSession(ctx, sessionId, title) {
290
324
  if (!title) return ''
@@ -544,6 +578,8 @@ export async function saveAutomation(ctx, options) {
544
578
  const rebound = await rebindTask(schedule, task, remembered)
545
579
  if (rebound !== sessionId) { sessionId = rebound; updated++ }
546
580
  }
581
+ // 早期版本留下的会话还在「未分组」里,先把它们归位到选中的工作区。
582
+ await adoptOrphanSessions(ctx, row.workspaceId, cwd)
547
583
  // 会话当场建出来:保存完就能打开它,任务也已经绑在它上面。
548
584
  let sessionReady = false
549
585
  let sessionName = ''