forked from lessweb/deepcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadingText.ts
More file actions
70 lines (57 loc) · 1.9 KB
/
loadingText.ts
File metadata and controls
70 lines (57 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { LlmStreamProgress, SessionEntry } from "../session";
type RunningProcesses = SessionEntry["processes"];
export type LoadingTextInput = {
progress: LlmStreamProgress | null;
processes?: RunningProcesses;
now: number;
};
const STALL_THRESHOLD_MS = 3000;
export function buildLoadingText(input: LoadingTextInput): string {
const { progress, processes, now } = input;
const processText = buildProcessLoadingText(processes, now);
if (processText) {
return processText;
}
if (!progress) {
return "Thinking...";
}
const startedAt = parseTimestamp(progress.startedAt);
if (startedAt === null) {
return "Thinking...";
}
const elapsedMs = Math.max(0, now - startedAt);
if (elapsedMs < STALL_THRESHOLD_MS) {
return "Thinking...";
}
const elapsedSeconds = Math.floor(elapsedMs / 1000);
const tokens = progress.formattedTokens || "0";
return `Thinking... (${elapsedSeconds}s) · ↓ ${tokens} tokens`;
}
function buildProcessLoadingText(processes: RunningProcesses | undefined, now: number): string | null {
if (!processes || processes.size === 0) {
return null;
}
const first = processes.values().next().value as { startTime: string; command: string } | undefined;
if (!first) {
return null;
}
return `(${formatElapsedTime(first.startTime, now)}) ${first.command}`;
}
function formatElapsedTime(startTimeIso: string, now: number): string {
const startTime = parseTimestamp(startTimeIso);
const elapsedMs = startTime === null ? 0 : Math.max(0, now - startTime);
const elapsedSeconds = Math.floor(elapsedMs / 1000);
const minutes = Math.floor(elapsedSeconds / 60);
const seconds = elapsedSeconds % 60;
if (minutes > 0) {
return `${minutes}m${seconds}s`;
}
return `${seconds}s`;
}
function parseTimestamp(value: string): number | null {
const parsed = Date.parse(value);
if (Number.isNaN(parsed)) {
return null;
}
return parsed;
}