@yeaft/webchat-agent 0.0.154 → 0.0.157

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.
Files changed (2) hide show
  1. package/crew.js +50 -2
  2. package/package.json +1 -1
package/crew.js CHANGED
@@ -257,6 +257,7 @@ function sessionToIndexEntry(session) {
257
257
  name: session.name || '',
258
258
  userId: session.userId,
259
259
  username: session.username,
260
+ agentId: session.agentId || null,
260
261
  createdAt: session.createdAt,
261
262
  updatedAt: Date.now()
262
263
  };
@@ -321,6 +322,7 @@ async function saveSessionMeta(session) {
321
322
  updatedAt: Date.now(),
322
323
  userId: session.userId,
323
324
  username: session.username,
325
+ agentId: session.agentId || null,
324
326
  costUsd: session.costUsd,
325
327
  totalInputTokens: session.totalInputTokens,
326
328
  totalOutputTokens: session.totalOutputTokens
@@ -358,8 +360,14 @@ export async function handleListCrewSessions(msg) {
358
360
  const { requestId, _requestClientId } = msg;
359
361
  const index = await loadCrewIndex();
360
362
 
363
+ // 按 agentId 过滤(兼容旧数据:无 agentId 的 session 在所有 agent 中显示)
364
+ const agentId = ctx.CONFIG?.agentName || null;
365
+ const filtered = agentId
366
+ ? index.filter(e => !e.agentId || e.agentId === agentId)
367
+ : index;
368
+
361
369
  // 用活跃 session 更新实时状态
362
- for (const entry of index) {
370
+ for (const entry of filtered) {
363
371
  const active = crewSessions.get(entry.sessionId);
364
372
  if (active) {
365
373
  entry.status = active.status;
@@ -370,7 +378,7 @@ export async function handleListCrewSessions(msg) {
370
378
  type: 'crew_sessions_list',
371
379
  requestId,
372
380
  _requestClientId,
373
- sessions: index
381
+ sessions: filtered
374
382
  });
375
383
  }
376
384
 
@@ -457,6 +465,7 @@ export async function resumeCrewSession(msg) {
457
465
  pendingRoutes: [],
458
466
  userId: userId || meta.userId,
459
467
  username: username || meta.username,
468
+ agentId: meta.agentId || ctx.CONFIG?.agentName || null,
460
469
  createdAt: meta.createdAt || Date.now()
461
470
  };
462
471
  crewSessions.set(sessionId, session);
@@ -492,6 +501,36 @@ export async function resumeCrewSession(msg) {
492
501
  console.log(`[Crew] Session ${sessionId} resumed, waiting for human input`);
493
502
  }
494
503
 
504
+ /**
505
+ * 查找指定 projectDir 的已有 crew session(内存活跃 > 磁盘索引)
506
+ */
507
+ async function findExistingSessionByProjectDir(projectDir) {
508
+ const normalizedDir = projectDir.replace(/\/+$/, '');
509
+
510
+ for (const [, session] of crewSessions) {
511
+ if (session.projectDir.replace(/\/+$/, '') === normalizedDir
512
+ && session.status !== 'completed') {
513
+ return { sessionId: session.id, source: 'active' };
514
+ }
515
+ }
516
+
517
+ const index = await loadCrewIndex();
518
+ const agentId = ctx.CONFIG?.agentName || null;
519
+ const match = index.find(e =>
520
+ e.projectDir.replace(/\/+$/, '') === normalizedDir
521
+ && (!agentId || !e.agentId || e.agentId === agentId)
522
+ && e.status !== 'completed'
523
+ );
524
+
525
+ if (match) {
526
+ const meta = await loadSessionMeta(match.sharedDir);
527
+ if (meta) return { sessionId: match.sessionId, source: 'index' };
528
+ await removeFromCrewIndex(match.sessionId);
529
+ }
530
+
531
+ return null;
532
+ }
533
+
495
534
  // =====================================================================
496
535
  // Session Lifecycle
497
536
  // =====================================================================
@@ -514,6 +553,14 @@ export async function createCrewSession(msg) {
514
553
  username
515
554
  } = msg;
516
555
 
556
+ // 同目录检查:如果 projectDir 已有活跃或可恢复的 session,自动 resume
557
+ const existingSession = await findExistingSessionByProjectDir(projectDir);
558
+ if (existingSession) {
559
+ console.log(`[Crew] Found existing session for ${projectDir}: ${existingSession.sessionId}, auto-resuming`);
560
+ await resumeCrewSession({ sessionId: existingSession.sessionId, userId, username });
561
+ return;
562
+ }
563
+
517
564
  // 展开多实例角色(count > 1 的执行者角色)
518
565
  const roles = expandRoles(rawRoles);
519
566
 
@@ -562,6 +609,7 @@ export async function createCrewSession(msg) {
562
609
  pendingRoutes: [], // [{ fromRole, route }] — 暂停时未完成的路由
563
610
  userId,
564
611
  username,
612
+ agentId: ctx.CONFIG?.agentName || null,
565
613
  createdAt: Date.now()
566
614
  };
567
615
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yeaft/webchat-agent",
3
- "version": "0.0.154",
3
+ "version": "0.0.157",
4
4
  "description": "Remote agent for Yeaft WebChat — connects worker machines to the central server",
5
5
  "main": "index.js",
6
6
  "type": "module",