@sejuneoh/aitop 0.33.2 → 0.35.0

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.
@@ -6,7 +6,7 @@ import { loadCache } from '../cache.mjs';
6
6
  import { contextWindow } from '../pricing.mjs';
7
7
  import { join, basename } from 'node:path';
8
8
  import { isSessionAlive } from '../procs.mjs';
9
- import { ClaudeTracker, tailCustomTitle } from './claude-tracker.mjs';
9
+ import { ClaudeTracker, tailCustomTitle, tailStopReason } from './claude-tracker.mjs';
10
10
  import { emptyTokens, isLongTool, mergeActivity, MEMBER_FADE_MS, sessionName } from './common.mjs';
11
11
  import { mergeFiles } from './files.mjs';
12
12
  import { debug, debugIo } from '../log.mjs';
@@ -29,30 +29,40 @@ function readJson(p) {
29
29
  }
30
30
  }
31
31
 
32
- function findTranscript(sessionId) {
32
+ // projects/ 아래 프로젝트 폴더 이름 [[root, dirs]]. 읽지 못한 root 는 null 이다.
33
+ function projectDirs() {
34
+ return claudeProjectRoots().map((root) => {
35
+ try {
36
+ return [root, readdirSync(root)];
37
+ } catch (e) {
38
+ debugIo('claude', 'projects read failed', e, root);
39
+ return [root, null];
40
+ }
41
+ });
42
+ }
43
+
44
+ // listing: projectDirs() 결과. 여러 세션을 한 번에 찾을 때 넘기면 projects/ 를 세션마다 다시 읽지 않는다.
45
+ function findTranscript(sessionId, listing = null) {
33
46
  const cached = pathCache.get(sessionId);
34
47
  if (cached && (cached.path ? existsSync(cached.path) : Date.now() - cached.at < 5000)) return cached.path;
35
48
  // 같은 세션 기록이 두 폴더에 있으면 더 최근에 바뀐 쪽 (끝난 세션 목록과 같은 기준). 폴더 하나에서는 처음 찾은 것.
36
49
  let path = null;
37
50
  let best = -Infinity;
38
- for (const root of claudeProjectRoots()) {
39
- try {
40
- for (const dir of readdirSync(root)) {
41
- const p = join(root, dir, `${sessionId}.jsonl`);
42
- let m;
43
- try {
44
- m = statSync(p).mtimeMs;
45
- } catch {
46
- continue;
47
- }
48
- if (m > best) {
49
- path = p;
50
- best = m;
51
- }
52
- break;
51
+ for (const [root, dirs] of listing ?? projectDirs()) {
52
+ if (!dirs) continue;
53
+ for (const dir of dirs) {
54
+ const p = join(root, dir, `${sessionId}.jsonl`);
55
+ let m;
56
+ try {
57
+ m = statSync(p).mtimeMs;
58
+ } catch {
59
+ continue;
53
60
  }
54
- } catch (e) {
55
- debugIo('claude', 'projects read failed', e, root);
61
+ if (m > best) {
62
+ path = p;
63
+ best = m;
64
+ }
65
+ break;
56
66
  }
57
67
  }
58
68
  pathCache.set(sessionId, { path, at: Date.now() });
@@ -468,15 +478,38 @@ function sessionLabel(e, t) {
468
478
  // 이름은 표와 같은 순서: 기록 끝부분의 /rename 이름, 세션 파일 이름, 자리표시 이름이면 작업 폴더 이름 (대화 제목은 기록을 다 읽어야 안다).
469
479
  // 대기 시작은 상태가 바뀐 시각(statusUpdatedAt), 없으면 세션 파일이 바뀐 시각.
470
480
  export function claudeWaiting() {
471
- const out = [];
472
- for (const e of sessionEntries()?.values() ?? []) {
473
- if (!e.alive || !e.waitingFor) continue;
474
- const transcript = findTranscript(e.sessionId);
481
+ return claudeLineCounts().waiting;
482
+ }
483
+
484
+ // --line 용: 오류로 멈춘 살아 있는 Claude 세션 수. 화면과 같이 대기(idle) 세션의 마지막 대화 줄이 API 오류인지 기록 끝만 본다.
485
+ // 세션 파일에 상태가 없는 예전 Claude Code 는 기록 수정 시각으로 상태를 짐작해야 해서 세지 않는다.
486
+ export function claudeStopped() {
487
+ return claudeLineCounts().stopped;
488
+ }
489
+
490
+ // --line 이 쓰는 두 값(응답 대기 목록, 멈춤 수)을 세션 목록 한 번, projects/ 폴더 목록 한 번으로 모은다.
491
+ // 멈춤 수는 지시 대기 세션마다 기록 끝(최대 256KB)을 읽으므로 deadline(Date.now() 기준 ms) 안에서만 센다.
492
+ // 다 세기 전에 시간이 다 되면 stopped 는 null(모름)이고, 줄은 멈춤 칸을 뺀다 (모자라게 센 수를 보이지 않는다).
493
+ export function claudeLineCounts({ deadline = Infinity } = {}) {
494
+ const entries = [...(sessionEntries()?.values() ?? [])].filter((e) => e.alive);
495
+ let listing = null;
496
+ const find = (id) => findTranscript(id, (listing ??= projectDirs()));
497
+ const waiting = [];
498
+ for (const e of entries) {
499
+ if (!e.waitingFor) continue;
500
+ const transcript = find(e.sessionId);
475
501
  const custom = transcript ? tailCustomTitle(transcript) : undefined;
476
502
  const since = [e.statusUpdatedAt, e.updatedAt, e.startedAt].find(Number.isFinite) ?? null;
477
- out.push({ tool: 'claude', name: givenName(e, custom) ?? (isAutoName(e) ? basename(e.cwd ?? '') : String(e.name)), since });
503
+ waiting.push({ tool: 'claude', name: givenName(e, custom) ?? (isAutoName(e) ? basename(e.cwd ?? '') : String(e.name)), since });
478
504
  }
479
- return out;
505
+ let stopped = 0;
506
+ for (const e of entries) {
507
+ if (e.waitingFor || e.status !== 'idle') continue;
508
+ if (Date.now() >= deadline) return { waiting, stopped: null };
509
+ const transcript = find(e.sessionId);
510
+ if (transcript && tailStopReason(transcript)) stopped++;
511
+ }
512
+ return { waiting, stopped };
480
513
  }
481
514
 
482
515
  export function collectClaude({ showEnded, budget, selectedId }) {
package/src/text.mjs CHANGED
@@ -97,6 +97,35 @@ export function truncate(s, max, mark = ellipsis(max)) {
97
97
  // 자른 자리 표시. 폭이 모호한 글자를 2칸으로 세는데 1칸만 남으면 … 대신 . 을 쓴다.
98
98
  export const ellipsis = (room = 2) => (width('…') <= room ? '…' : '.');
99
99
 
100
+ // 글을 폭 w 칸 안의 줄들로 나눈다. 띄어쓰기에서 끊고, 한 낱말이 w 보다 길면 글자 단위로 끊는다.
101
+ // 한글처럼 2칸인 글자도 width 로 세어 어느 줄도 w 를 넘지 않는다. 줄 앞뒤 공백은 지운다.
102
+ export function wrapWords(s, w) {
103
+ const out = [];
104
+ let cur = '';
105
+ for (const word of String(s).split(/\s+/).filter(Boolean)) {
106
+ const joined = cur ? `${cur} ${word}` : word;
107
+ if (width(joined) <= w) {
108
+ cur = joined;
109
+ continue;
110
+ }
111
+ if (cur) out.push(cur);
112
+ cur = '';
113
+ let rest = word;
114
+ while (width(rest) > w) {
115
+ let head = '';
116
+ for (const ch of rest) {
117
+ if (head && width(head + ch) > w) break;
118
+ head += ch;
119
+ }
120
+ out.push(head);
121
+ rest = rest.slice(head.length);
122
+ }
123
+ cur = rest;
124
+ }
125
+ if (cur || !out.length) out.push(cur);
126
+ return out;
127
+ }
128
+
100
129
  export function pad(s, n, align = 'left') {
101
130
  const t = truncate(s, n);
102
131
  const gap = ' '.repeat(Math.max(0, n - width(t)));