dsh-cothread-mcp 0.1.5 → 0.1.7

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,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.7
4
+ - Fix the automation task creating a new session on every save, which left a pile of sessions in the workspace and moved the conversation away from the one that had the actual content. The host only reuses a session that has not run a turn yet, so an automation session that had already run was replaced by a fresh one each time. The session is now reused as long as it exists, and the task is bound to the same-named session that already has turns, so the conversation stays where it was.
5
+ ## 0.1.6
6
+ - 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
7
  ## 0.1.5
4
8
  - 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
9
 
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.7",
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
  /** 把任务接回指定的会话;接不回就保持原样。 */
@@ -245,25 +237,130 @@ async function ensureSession(ctx, sessionId, cwd, title, workspaceId = '') {
245
237
  const registry = serviceOf(ctx, 'workspaceRegistry')
246
238
  const archived = registry && Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
247
239
  if (archived.includes(sessionId)) return { ok: false, sessionId, reason: '会话已归档' }
240
+ // 会话已经存在就直接用它,不再请宿主创建。
241
+ // 宿主在 session.create 里只复用「没跑过轮次」的空白会话,自动化会话跑过一轮之后就不可复用了,
242
+ // 每保存一次宿主就另建一个会话,任务改绑到新会话上,之前那个还在工作区里挂着,一次攒一堆。
243
+ // 自动化任务要的是「一直往同一个会话里投递」,宿主在 schedule 那边对 session 目标也是这么做的:
244
+ // 会话存在就能投递,不需要是空白会话。所以这里只确认它在,不重建。
245
+ if (await sessionExists(ctx, sessionId)) {
246
+ // 会话早于本次改动建成、还没挂进工作区时,宿主不会替它补挂,这里补上。
247
+ await attachToWorkspace(ctx, workspaceId, sessionId)
248
+ nameSession(ctx, sessionId, title)
249
+ return { ok: true, sessionId }
250
+ }
248
251
  let createdId = sessionId
249
252
  try {
250
253
  // 宿主的 session.create 里 workspaceId 与 cwd 互斥:只给 cwd 时,会话只是「拥有」这个
251
254
  // 目录,并不会被加进工作区的会话列表,界面上打开会话就看不到它挂在选中的工作区下。
252
255
  // 给了 workspaceId,宿主会用工作区的路径作 cwd,并把会话正式挂进该工作区。
253
256
  const created = await controller.create(workspaceId ? { sessionId, workspaceId } : { sessionId, cwd })
254
- // 宿主可能复用或另建一个会话,任务要绑的是它真正建出来的那个。
257
+ // 宿主可能另建了一个会话,任务要绑的是它真正建出来的那个。
255
258
  if (created && typeof created.sessionId === 'string' && created.sessionId) createdId = created.sessionId
256
259
  } catch (error) {
257
260
  const message = error && error.message ? error.message : '创建会话失败'
258
261
  // 会话已经存在时不算失败,接着往下走命名。
259
262
  if (!/exist|already/i.test(message)) return { ok: false, sessionId, reason: message }
260
263
  }
261
- // 会话早于本次改动建成、还没挂进工作区时,宿主不会替它补挂,这里补上。
262
264
  await attachToWorkspace(ctx, workspaceId, createdId)
263
265
  nameSession(ctx, createdId, title)
264
266
  return { ok: true, sessionId: createdId }
265
267
  }
266
268
 
269
+ /**
270
+ * 找一个已经有内容的同名会话。
271
+ *
272
+ * 自动化任务要的是「一直往同一个会话里投递」。宿主只在会话是空白(没跑过轮次)时才复用,
273
+ * 所以每保存一次就会另建一个会话,之前那个被孤立在工作区里。这里从存储的会话里找出标题相同、
274
+ * 且已经跑过轮次的那个,让任务绑回它。
275
+ */
276
+ async function findActiveSession(ctx, workspaceId, cwd, title) {
277
+ if (!workspaceId || !cwd || !title) return ''
278
+ const registry = serviceOf(ctx, 'workspaceRegistry')
279
+ if (!registry || typeof registry.get !== 'function') return ''
280
+ const workspace = registry.get(workspaceId)
281
+ if (!workspace) return ''
282
+ const query = serviceOf(ctx, 'sessionQuery')
283
+ if (!query || typeof query.observeSession !== 'function') return ''
284
+ const members = Array.isArray(workspace.sessionIds) ? workspace.sessionIds : []
285
+ const archived = Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
286
+ let best = ''
287
+ for (const id of members) {
288
+ if (!id || archived.includes(id)) continue
289
+ const session = serviceOf(ctx, 'sessions')
290
+ const live = session && typeof session.get === 'function' ? session.get(id) : undefined
291
+ if (live) {
292
+ // 在跑的会话直接看它的标题;跑过轮次的才算——空白会话不算,它没有内容可看。
293
+ const name = await sessionTitleOf(ctx, live)
294
+ if (name !== title) continue
295
+ if (!await hasTurn(ctx, live)) continue
296
+ if (best === '' || id < best) best = id
297
+ continue
298
+ }
299
+ let observation
300
+ try {
301
+ observation = await query.observeSession(id)
302
+ } catch {
303
+ continue
304
+ }
305
+ if (!observation) continue
306
+ const events = observation.events || []
307
+ let name = ''
308
+ let started = false
309
+ for (const event of events) {
310
+ if (event && event.type === 'turn/start') started = true
311
+ if (event && event.type === 'session/title' && event.title) name = String(event.title)
312
+ }
313
+ if (!started || name !== title) continue
314
+ if (best === '' || id < best) best = id
315
+ }
316
+ return best
317
+ }
318
+
319
+ /** 这个会话跑过轮次吗。空白会话没有内容,不算。 */
320
+ async function hasTurn(ctx, session) {
321
+ const agents = serviceOf(ctx, 'agents')
322
+ if (agents && typeof agents.turnBoundaryOf === 'function') {
323
+ try {
324
+ const boundary = agents.turnBoundaryOf(session)
325
+ if (boundary && (boundary.lastTurn > 0 || boundary.openTurnStartSeq !== null)) return true
326
+ } catch {
327
+ // 读不到就按下面的事件判断。
328
+ }
329
+ }
330
+ const events = session && typeof session.snapshotEvents === 'function' ? session.snapshotEvents() : []
331
+ for (const event of events) if (event && event.type === 'turn/start') return true
332
+ return false
333
+ }
334
+ /** 读一个会话的标题,读不到就当作没有。 */
335
+ async function sessionTitleOf(ctx, session) {
336
+ const titles = serviceOf(ctx, 'sessionTitle')
337
+ if (!titles || typeof titles.get !== 'function') return ''
338
+ try {
339
+ const current = titles.get(session)
340
+ return current && current.title ? String(current.title) : ''
341
+ } catch {
342
+ return ''
343
+ }
344
+ }
345
+ /**
346
+ * 这个会话还在不在。
347
+ *
348
+ * 宿主里分两种:在跑的(live)与存着的(已存储)。两者都算会话还在——自动化任务要往同一个
349
+ * 会话里持续投递,会话跑过一轮也没关系。宿主给自动化任务投递时对 session 目标也是这么判的。
350
+ */
351
+ async function sessionExists(ctx, sessionId) {
352
+ if (!sessionId) return false
353
+ const sessions = serviceOf(ctx, 'sessions')
354
+ if (sessions && typeof sessions.get === 'function' && sessions.get(sessionId)) return true
355
+ const persistence = serviceOf(ctx, 'sessionPersistence')
356
+ if (!persistence || typeof persistence.stat !== 'function') return false
357
+ try {
358
+ const stored = await persistence.stat(sessionId)
359
+ return stored !== undefined && stored !== null
360
+ } catch {
361
+ return false
362
+ }
363
+ }
267
364
  /**
268
365
  * 把会话挂进它该在的工作区。
269
366
  *
@@ -285,6 +382,43 @@ async function attachToWorkspace(ctx, workspaceId, sessionId) {
285
382
  }
286
383
  }
287
384
 
385
+ /**
386
+ * 把还留在「未分组」里的会话归位到它该在的工作区。
387
+ *
388
+ * 早期版本用工作区的目录路径创建会话,没有把会话挂进工作区,那些会话至今留在「未分组」里,
389
+ * 界面上打开它们看不到它们属于所选的工作区。这里按目录把它们找出来补挂上。归档的会话本来
390
+ * 就留在「未分组」,不参与归位。
391
+ */
392
+ async function adoptOrphanSessions(ctx, workspaceId, cwd) {
393
+ if (!workspaceId || !cwd) return 0
394
+ const registry = serviceOf(ctx, 'workspaceRegistry')
395
+ if (!registry || typeof registry.get !== 'function') return 0
396
+ const workspace = registry.get(workspaceId)
397
+ if (!workspace) return 0
398
+ const persistence = serviceOf(ctx, 'sessionPersistence')
399
+ if (!persistence || typeof persistence.list !== 'function') return 0
400
+ const members = Array.isArray(workspace.sessionIds) ? workspace.sessionIds : []
401
+ const archived = Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
402
+ let adopted = 0
403
+ let snapshots = []
404
+ try {
405
+ snapshots = await persistence.list()
406
+ } catch {
407
+ return 0
408
+ }
409
+ for (const snapshot of snapshots) {
410
+ const header = snapshot && snapshot.header ? snapshot.header : undefined
411
+ const id = header && header.id ? header.id : ''
412
+ if (!id || members.includes(id)) continue
413
+ // 归档的会话本来就留在「未分组」,不归位。
414
+ if (archived.includes(id)) continue
415
+ if (!header || header.cwd !== cwd) continue
416
+ // 只归位这个目录下的会话;attachSession 自己会再校验一次目录,不匹配就拒绝。
417
+ if (await attachToWorkspace(ctx, workspaceId, id)) adopted++
418
+ }
419
+ return adopted
420
+ }
421
+
288
422
  /** 给自动化会话起个项目名,界面上就能看到它叫什么。 */
289
423
  export function nameSession(ctx, sessionId, title) {
290
424
  if (!title) return ''
@@ -384,7 +518,10 @@ export async function syncAutomations(ctx, options) {
384
518
  if (await deleteTask(schedule, extra.id)) { deleted++; deletedIds.add(extra.id) }
385
519
  }
386
520
  if (!task) {
387
- const made = await createTask(ctx, schedule, { title, prompt, seconds, cwd, workspaceId: row.workspaceId, sessionId: reusableSession(mapping[key], registry) })
521
+ // 优先绑回那个已经跑过轮次、有内容的同名会话;没有才新建。
522
+ const active = await findActiveSession(ctx, row.workspaceId, cwd, title)
523
+ const rememberedSession = active || reusableSession(mapping[key], registry)
524
+ const made = await createTask(ctx, schedule, { title, prompt, seconds, cwd, workspaceId: row.workspaceId, sessionId: rememberedSession })
388
525
  task = made.task
389
526
  created++
390
527
  current = seconds
@@ -544,6 +681,17 @@ export async function saveAutomation(ctx, options) {
544
681
  const rebound = await rebindTask(schedule, task, remembered)
545
682
  if (rebound !== sessionId) { sessionId = rebound; updated++ }
546
683
  }
684
+ // 任务现在绑着的是个空白会话,而有内容的是另一个会话:改绑到有内容的那个,
685
+ // 免得巡检跑在一个空会话里,之前的内容留在旁边没人看。
686
+ if (sessionId) {
687
+ const active = await findActiveSession(ctx, row.workspaceId, cwd, title)
688
+ if (active && active !== sessionId) {
689
+ const rebound = await rebindTask(schedule, task, active)
690
+ if (rebound === active) { sessionId = active; updated++ }
691
+ }
692
+ }
693
+ // 早期版本留下的会话还在「未分组」里,先把它们归位到选中的工作区。
694
+ await adoptOrphanSessions(ctx, row.workspaceId, cwd)
547
695
  // 会话当场建出来:保存完就能打开它,任务也已经绑在它上面。
548
696
  let sessionReady = false
549
697
  let sessionName = ''