dsh-cothread-mcp 0.1.6 → 0.1.8
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 +4 -0
- package/package.json +1 -1
- package/src/automation.js +178 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.8
|
|
4
|
+
- Clean up the duplicate sessions an earlier version left behind. Saving an automation row now archives the same-named sessions it does not use and detaches them from the workspace, so the workspace list shows only the session the task is actually bound to. The cleanup collects the sessions before acting, because detaching one changes the very list it walks.
|
|
5
|
+
## 0.1.7
|
|
6
|
+
- 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.
|
|
3
7
|
## 0.1.6
|
|
4
8
|
- 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
9
|
## 0.1.5
|
package/package.json
CHANGED
package/src/automation.js
CHANGED
|
@@ -237,12 +237,18 @@ async function ensureSession(ctx, sessionId, cwd, title, workspaceId = '') {
|
|
|
237
237
|
const registry = serviceOf(ctx, 'workspaceRegistry')
|
|
238
238
|
const archived = registry && Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
|
|
239
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
|
+
}
|
|
240
251
|
let createdId = sessionId
|
|
241
|
-
// 先挂载,再创建。
|
|
242
|
-
// 宿主在 session.create 里带上 workspaceId 时,只复用工作区已经记着的会话;会话没挂上去
|
|
243
|
-
// 就会被另建一个,于是记住的还是旧 id,旧会话继续留在「未分组」里。先挂载,宿主就复用它。
|
|
244
|
-
// 会话还不存在时挂载会失败(读不到它的 header),忽略即可,宿主创建时会自己挂上。
|
|
245
|
-
await attachToWorkspace(ctx, workspaceId, sessionId)
|
|
246
252
|
try {
|
|
247
253
|
// 宿主的 session.create 里 workspaceId 与 cwd 互斥:只给 cwd 时,会话只是「拥有」这个
|
|
248
254
|
// 目录,并不会被加进工作区的会话列表,界面上打开会话就看不到它挂在选中的工作区下。
|
|
@@ -255,12 +261,161 @@ async function ensureSession(ctx, sessionId, cwd, title, workspaceId = '') {
|
|
|
255
261
|
// 会话已经存在时不算失败,接着往下走命名。
|
|
256
262
|
if (!/exist|already/i.test(message)) return { ok: false, sessionId, reason: message }
|
|
257
263
|
}
|
|
258
|
-
// 宿主另建了会话时,它自己会挂进工作区;这里再确认一次,幂等。
|
|
259
264
|
await attachToWorkspace(ctx, workspaceId, createdId)
|
|
260
265
|
nameSession(ctx, createdId, title)
|
|
261
266
|
return { ok: true, sessionId: createdId }
|
|
262
267
|
}
|
|
263
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
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* 清理这个项目留下的重复会话。
|
|
366
|
+
*
|
|
367
|
+
* 早期版本每保存一次就新建一个会话,工作区里会攒下一堆同名会话,界面上看着很乱,
|
|
368
|
+
* 真正在用的那一个反而不好找。这里把同名、但不是任务当前绑着的那些归档并移出工作区:
|
|
369
|
+
* 归档后界面默认不显示,移出后工作区的会话列表里也不再占位置。
|
|
370
|
+
*/
|
|
371
|
+
async function cleanDuplicateSessions(ctx, workspaceId, cwd, title, keepId) {
|
|
372
|
+
if (!workspaceId || !cwd || !title) return 0
|
|
373
|
+
const registry = serviceOf(ctx, 'workspaceRegistry')
|
|
374
|
+
if (!registry || typeof registry.get !== 'function') return 0
|
|
375
|
+
const workspace = registry.get(workspaceId)
|
|
376
|
+
if (!workspace) return 0
|
|
377
|
+
const query = serviceOf(ctx, 'sessionQuery')
|
|
378
|
+
const sessions = serviceOf(ctx, 'sessions')
|
|
379
|
+
const archived = Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
|
|
380
|
+
// 先把要清的会话找齐,再动手。
|
|
381
|
+
// 边遍历边 detach 会改动工作区那份会话列表,遍历会漏掉元素,清不干净。
|
|
382
|
+
const members = Array.isArray(workspace.sessionIds) ? [...workspace.sessionIds] : []
|
|
383
|
+
const doomed = []
|
|
384
|
+
for (const id of members) {
|
|
385
|
+
if (!id || id === keepId || archived.includes(id)) continue
|
|
386
|
+
const live = sessions && typeof sessions.get === 'function' ? sessions.get(id) : undefined
|
|
387
|
+
let name = ''
|
|
388
|
+
if (live) {
|
|
389
|
+
name = await sessionTitleOf(ctx, live)
|
|
390
|
+
} else if (query && typeof query.observeSession === 'function') {
|
|
391
|
+
try {
|
|
392
|
+
const observation = await query.observeSession(id)
|
|
393
|
+
for (const event of (observation && observation.events) || []) {
|
|
394
|
+
if (event && event.type === 'session/title' && event.title) name = String(event.title)
|
|
395
|
+
}
|
|
396
|
+
} catch {
|
|
397
|
+
// 读不到标题的会话不是这个项目的,不动它。
|
|
398
|
+
continue
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
if (name !== title) continue
|
|
402
|
+
doomed.push(id)
|
|
403
|
+
}
|
|
404
|
+
let cleaned = 0
|
|
405
|
+
for (const id of doomed) {
|
|
406
|
+
const done = await archiveSession(registry, id)
|
|
407
|
+
if (!done.archived) continue
|
|
408
|
+
if (typeof workspace.detachSession === 'function') {
|
|
409
|
+
try {
|
|
410
|
+
await workspace.detachSession(id)
|
|
411
|
+
} catch {
|
|
412
|
+
// 移不出去就只归档,界面默认也不显示它。
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
cleaned++
|
|
416
|
+
}
|
|
417
|
+
return cleaned
|
|
418
|
+
}
|
|
264
419
|
/**
|
|
265
420
|
* 把会话挂进它该在的工作区。
|
|
266
421
|
*
|
|
@@ -418,7 +573,10 @@ export async function syncAutomations(ctx, options) {
|
|
|
418
573
|
if (await deleteTask(schedule, extra.id)) { deleted++; deletedIds.add(extra.id) }
|
|
419
574
|
}
|
|
420
575
|
if (!task) {
|
|
421
|
-
|
|
576
|
+
// 优先绑回那个已经跑过轮次、有内容的同名会话;没有才新建。
|
|
577
|
+
const active = await findActiveSession(ctx, row.workspaceId, cwd, title)
|
|
578
|
+
const rememberedSession = active || reusableSession(mapping[key], registry)
|
|
579
|
+
const made = await createTask(ctx, schedule, { title, prompt, seconds, cwd, workspaceId: row.workspaceId, sessionId: rememberedSession })
|
|
422
580
|
task = made.task
|
|
423
581
|
created++
|
|
424
582
|
current = seconds
|
|
@@ -452,6 +610,8 @@ export async function syncAutomations(ctx, options) {
|
|
|
452
610
|
if (made.sessionId) sessionId = made.sessionId
|
|
453
611
|
sessionName = nameSession(ctx, sessionId, title)
|
|
454
612
|
}
|
|
613
|
+
// 清掉这个项目早期版本留下的重复会话,只留任务绑着的那个。
|
|
614
|
+
await cleanDuplicateSessions(ctx, row.workspaceId, cwd, title, sessionId)
|
|
455
615
|
next[key] = { id: task.id, title, cwd, seconds: current, ...(row.workspaceId ? { workspaceId: row.workspaceId } : {}), ...(sessionId ? { sessionId } : {}), ...(sessionName ? { sessionName } : {}) }
|
|
456
616
|
tasks.push({
|
|
457
617
|
key,
|
|
@@ -578,8 +738,19 @@ export async function saveAutomation(ctx, options) {
|
|
|
578
738
|
const rebound = await rebindTask(schedule, task, remembered)
|
|
579
739
|
if (rebound !== sessionId) { sessionId = rebound; updated++ }
|
|
580
740
|
}
|
|
741
|
+
// 任务现在绑着的是个空白会话,而有内容的是另一个会话:改绑到有内容的那个,
|
|
742
|
+
// 免得巡检跑在一个空会话里,之前的内容留在旁边没人看。
|
|
743
|
+
if (sessionId) {
|
|
744
|
+
const active = await findActiveSession(ctx, row.workspaceId, cwd, title)
|
|
745
|
+
if (active && active !== sessionId) {
|
|
746
|
+
const rebound = await rebindTask(schedule, task, active)
|
|
747
|
+
if (rebound === active) { sessionId = active; updated++ }
|
|
748
|
+
}
|
|
749
|
+
}
|
|
581
750
|
// 早期版本留下的会话还在「未分组」里,先把它们归位到选中的工作区。
|
|
582
751
|
await adoptOrphanSessions(ctx, row.workspaceId, cwd)
|
|
752
|
+
// 早期版本每保存一次就新建一个会话,这里把多余的同名会话清掉,只留任务绑着的那个。
|
|
753
|
+
await cleanDuplicateSessions(ctx, row.workspaceId, cwd, title, sessionId)
|
|
583
754
|
// 会话当场建出来:保存完就能打开它,任务也已经绑在它上面。
|
|
584
755
|
let sessionReady = false
|
|
585
756
|
let sessionName = ''
|